]> andersk Git - gssapi-openssh.git/blame_incremental - openssh/monitor.c
fix another AIX build problem
[gssapi-openssh.git] / openssh / monitor.c
... / ...
CommitLineData
1/* $OpenBSD: monitor.c,v 1.101 2009/02/12 03:26:22 djm Exp $ */
2/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
29
30#include <sys/types.h>
31#include <sys/param.h>
32#include <sys/socket.h>
33#include "openbsd-compat/sys-tree.h"
34#include <sys/wait.h>
35
36#include <errno.h>
37#include <fcntl.h>
38#ifdef HAVE_PATHS_H
39#include <paths.h>
40#endif
41#include <pwd.h>
42#include <signal.h>
43#include <stdarg.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47
48#ifdef SKEY
49#include <skey.h>
50#endif
51
52#include <openssl/dh.h>
53
54#include "openbsd-compat/sys-queue.h"
55#include "xmalloc.h"
56#include "ssh.h"
57#include "key.h"
58#include "buffer.h"
59#include "hostfile.h"
60#include "auth.h"
61#include "cipher.h"
62#include "kex.h"
63#include "dh.h"
64#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
65#undef TARGET_OS_MAC
66#include "zlib.h"
67#define TARGET_OS_MAC 1
68#else
69#include "zlib.h"
70#endif
71#include "packet.h"
72#include "auth-options.h"
73#include "sshpty.h"
74#include "channels.h"
75#include "session.h"
76#include "sshlogin.h"
77#include "canohost.h"
78#include "log.h"
79#include "servconf.h"
80#include "monitor.h"
81#include "monitor_mm.h"
82#ifdef GSSAPI
83#include "ssh-gss.h"
84#endif
85#include "monitor_wrap.h"
86#include "monitor_fdpass.h"
87#include "misc.h"
88#include "compat.h"
89#include "ssh2.h"
90#include "jpake.h"
91
92#ifdef GSSAPI
93static Gssctxt *gsscontext = NULL;
94#endif
95
96/* Imports */
97extern ServerOptions options;
98extern u_int utmp_len;
99extern Newkeys *current_keys[];
100extern z_stream incoming_stream;
101extern z_stream outgoing_stream;
102extern u_char session_id[];
103extern Buffer input, output;
104extern Buffer auth_debug;
105extern int auth_debug_init;
106extern Buffer loginmsg;
107
108/* State exported from the child */
109
110struct {
111 z_stream incoming;
112 z_stream outgoing;
113 u_char *keyin;
114 u_int keyinlen;
115 u_char *keyout;
116 u_int keyoutlen;
117 u_char *ivin;
118 u_int ivinlen;
119 u_char *ivout;
120 u_int ivoutlen;
121 u_char *ssh1key;
122 u_int ssh1keylen;
123 int ssh1cipher;
124 int ssh1protoflags;
125 u_char *input;
126 u_int ilen;
127 u_char *output;
128 u_int olen;
129} child_state;
130
131/* Functions on the monitor that answer unprivileged requests */
132
133int mm_answer_moduli(int, Buffer *);
134int mm_answer_sign(int, Buffer *);
135int mm_answer_pwnamallow(int, Buffer *);
136int mm_answer_auth2_read_banner(int, Buffer *);
137int mm_answer_authserv(int, Buffer *);
138int mm_answer_authpassword(int, Buffer *);
139int mm_answer_bsdauthquery(int, Buffer *);
140int mm_answer_bsdauthrespond(int, Buffer *);
141int mm_answer_skeyquery(int, Buffer *);
142int mm_answer_skeyrespond(int, Buffer *);
143int mm_answer_keyallowed(int, Buffer *);
144int mm_answer_keyverify(int, Buffer *);
145int mm_answer_pty(int, Buffer *);
146int mm_answer_pty_cleanup(int, Buffer *);
147int mm_answer_term(int, Buffer *);
148int mm_answer_rsa_keyallowed(int, Buffer *);
149int mm_answer_rsa_challenge(int, Buffer *);
150int mm_answer_rsa_response(int, Buffer *);
151int mm_answer_sesskey(int, Buffer *);
152int mm_answer_sessid(int, Buffer *);
153int mm_answer_jpake_get_pwdata(int, Buffer *);
154int mm_answer_jpake_step1(int, Buffer *);
155int mm_answer_jpake_step2(int, Buffer *);
156int mm_answer_jpake_key_confirm(int, Buffer *);
157int mm_answer_jpake_check_confirm(int, Buffer *);
158
159#ifdef USE_PAM
160int mm_answer_pam_start(int, Buffer *);
161int mm_answer_pam_account(int, Buffer *);
162int mm_answer_pam_init_ctx(int, Buffer *);
163int mm_answer_pam_query(int, Buffer *);
164int mm_answer_pam_respond(int, Buffer *);
165int mm_answer_pam_free_ctx(int, Buffer *);
166#endif
167
168#ifdef GSSAPI
169int mm_answer_gss_setup_ctx(int, Buffer *);
170int mm_answer_gss_accept_ctx(int, Buffer *);
171int mm_answer_gss_userok(int, Buffer *);
172int mm_answer_gss_checkmic(int, Buffer *);
173int mm_answer_gss_sign(int, Buffer *);
174int mm_answer_gss_error(int, Buffer *);
175int mm_answer_gss_indicate_mechs(int, Buffer *);
176int mm_answer_gss_localname(int, Buffer *);
177#endif
178
179#ifdef SSH_AUDIT_EVENTS
180int mm_answer_audit_event(int, Buffer *);
181int mm_answer_audit_command(int, Buffer *);
182#endif
183
184static Authctxt *authctxt;
185static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
186
187/* local state for key verify */
188static u_char *key_blob = NULL;
189static u_int key_bloblen = 0;
190static int key_blobtype = MM_NOKEY;
191static char *hostbased_cuser = NULL;
192static char *hostbased_chost = NULL;
193static char *auth_method = "unknown";
194static u_int session_id2_len = 0;
195static u_char *session_id2 = NULL;
196static pid_t monitor_child_pid;
197
198struct mon_table {
199 enum monitor_reqtype type;
200 int flags;
201 int (*f)(int, Buffer *);
202};
203
204#define MON_ISAUTH 0x0004 /* Required for Authentication */
205#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
206#define MON_ONCE 0x0010 /* Disable after calling */
207#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
208
209#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
210
211#define MON_PERMIT 0x1000 /* Request is permitted */
212
213struct mon_table mon_dispatch_proto20[] = {
214 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
215 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
216 {MONITOR_REQ_PWNAM, MON_AUTH, mm_answer_pwnamallow},
217 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
218 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
219 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
220#ifdef USE_PAM
221 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
222 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
223 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
224 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
225 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
226 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
227#endif
228#ifdef SSH_AUDIT_EVENTS
229 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
230#endif
231#ifdef BSD_AUTH
232 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
233 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
234#endif
235#ifdef SKEY
236 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
237 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
238#endif
239 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
240 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
241#ifdef GSSAPI
242 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
243 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
244 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
245 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
246 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
247 {MONITOR_REQ_GSSERR, MON_ISAUTH | MON_ONCE, mm_answer_gss_error},
248 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
249 {MONITOR_REQ_GSSLOCALNAME, MON_ISAUTH, mm_answer_gss_localname},
250#endif
251#ifdef JPAKE
252 {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
253 {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
254 {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
255 {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
256 {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
257#endif
258 {0, 0, NULL}
259};
260
261struct mon_table mon_dispatch_postauth20[] = {
262#ifdef GSSAPI
263 {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
264 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
265 {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
266 {MONITOR_REQ_GSSERR, 0, mm_answer_gss_error},
267 {MONITOR_REQ_GSSMECHS, 0, mm_answer_gss_indicate_mechs},
268#endif
269 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
270 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
271 {MONITOR_REQ_PTY, 0, mm_answer_pty},
272 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
273 {MONITOR_REQ_TERM, 0, mm_answer_term},
274#ifdef SSH_AUDIT_EVENTS
275 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
276 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
277#endif
278 {0, 0, NULL}
279};
280
281struct mon_table mon_dispatch_proto15[] = {
282 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
283 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
284 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
285 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
286 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
287 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
288 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
289 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
290#ifdef BSD_AUTH
291 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
292 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
293#endif
294#ifdef SKEY
295 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
296 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
297#endif
298#ifdef GSSAPI
299 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
300 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
301 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
302 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
303 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
304#endif
305#ifdef USE_PAM
306 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
307 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
308 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
309 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
310 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
311 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
312#endif
313#ifdef SSH_AUDIT_EVENTS
314 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
315#endif
316 {0, 0, NULL}
317};
318
319struct mon_table mon_dispatch_postauth15[] = {
320 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
321 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
322 {MONITOR_REQ_TERM, 0, mm_answer_term},
323#ifdef SSH_AUDIT_EVENTS
324 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
325 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
326#endif
327 {0, 0, NULL}
328};
329
330struct mon_table *mon_dispatch;
331
332/* Specifies if a certain message is allowed at the moment */
333
334static void
335monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
336{
337 while (ent->f != NULL) {
338 if (ent->type == type) {
339 ent->flags &= ~MON_PERMIT;
340 ent->flags |= permit ? MON_PERMIT : 0;
341 return;
342 }
343 ent++;
344 }
345}
346
347static void
348monitor_permit_authentications(int permit)
349{
350 struct mon_table *ent = mon_dispatch;
351
352 while (ent->f != NULL) {
353 if (ent->flags & MON_AUTH) {
354 ent->flags &= ~MON_PERMIT;
355 ent->flags |= permit ? MON_PERMIT : 0;
356 }
357 ent++;
358 }
359}
360
361void
362monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
363{
364 struct mon_table *ent;
365 int authenticated = 0;
366
367 debug3("preauth child monitor started");
368
369 authctxt = _authctxt;
370 memset(authctxt, 0, sizeof(*authctxt));
371
372 authctxt->loginmsg = &loginmsg;
373
374 if (compat20) {
375 mon_dispatch = mon_dispatch_proto20;
376
377 /* Permit requests for moduli and signatures */
378 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
379 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
380#ifdef GSSAPI
381 /* and for the GSSAPI key exchange */
382 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
383 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
384 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS, 1);
385#endif
386 } else {
387 mon_dispatch = mon_dispatch_proto15;
388
389 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
390 }
391
392 /* The first few requests do not require asynchronous access */
393 while (!authenticated) {
394 auth_method = "unknown";
395 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
396 if (authenticated) {
397 if (!(ent->flags & MON_AUTHDECIDE))
398 fatal("%s: unexpected authentication from %d",
399 __func__, ent->type);
400 if (authctxt->pw->pw_uid == 0 &&
401 !auth_root_allowed(auth_method))
402 authenticated = 0;
403#ifdef USE_PAM
404 /* PAM needs to perform account checks after auth */
405 if (options.use_pam && authenticated) {
406 Buffer m;
407
408 buffer_init(&m);
409 mm_request_receive_expect(pmonitor->m_sendfd,
410 MONITOR_REQ_PAM_ACCOUNT, &m);
411 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
412 buffer_free(&m);
413 }
414#endif
415 }
416
417 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
418 auth_log(authctxt, authenticated, auth_method,
419 compat20 ? " ssh2" : "");
420 if (!authenticated)
421 authctxt->failures++;
422 }
423#ifdef JPAKE
424 /* Cleanup JPAKE context after authentication */
425 if (ent->flags & MON_AUTHDECIDE) {
426 if (authctxt->jpake_ctx != NULL) {
427 jpake_free(authctxt->jpake_ctx);
428 authctxt->jpake_ctx = NULL;
429 }
430 }
431#endif
432 }
433
434 if (!authctxt->valid)
435 fatal("%s: authenticated invalid user", __func__);
436 if (strcmp(auth_method, "unknown") == 0)
437 fatal("%s: authentication method name unknown", __func__);
438
439 debug("%s: %s has been authenticated by privileged process",
440 __func__, authctxt->user);
441
442 mm_get_keystate(pmonitor);
443}
444
445static void
446monitor_set_child_handler(pid_t pid)
447{
448 monitor_child_pid = pid;
449}
450
451static void
452monitor_child_handler(int sig)
453{
454 kill(monitor_child_pid, sig);
455}
456
457void
458monitor_child_postauth(struct monitor *pmonitor)
459{
460 monitor_set_child_handler(pmonitor->m_pid);
461 signal(SIGHUP, &monitor_child_handler);
462 signal(SIGTERM, &monitor_child_handler);
463 signal(SIGINT, &monitor_child_handler);
464
465 if (compat20) {
466 mon_dispatch = mon_dispatch_postauth20;
467
468 /* Permit requests for moduli and signatures */
469 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
470 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
471 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
472
473#ifdef GSSAPI
474 /* and for the GSSAPI key exchange */
475 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS,1);
476 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP,1);
477 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR,1);
478#endif
479
480 } else {
481 mon_dispatch = mon_dispatch_postauth15;
482 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
483 }
484#ifdef GSSAPI
485 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
486#endif
487 if (!no_pty_flag) {
488 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
489 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
490 }
491
492 for (;;)
493 monitor_read(pmonitor, mon_dispatch, NULL);
494}
495
496void
497monitor_sync(struct monitor *pmonitor)
498{
499 if (options.compression) {
500 /* The member allocation is not visible, so sync it */
501 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
502 }
503}
504
505int
506monitor_read(struct monitor *pmonitor, struct mon_table *ent,
507 struct mon_table **pent)
508{
509 Buffer m;
510 int ret;
511 u_char type;
512
513 buffer_init(&m);
514
515 mm_request_receive(pmonitor->m_sendfd, &m);
516 type = buffer_get_char(&m);
517
518 debug3("%s: checking request %d", __func__, type);
519
520 while (ent->f != NULL) {
521 if (ent->type == type)
522 break;
523 ent++;
524 }
525
526 if (ent->f != NULL) {
527 if (!(ent->flags & MON_PERMIT))
528 fatal("%s: unpermitted request %d", __func__,
529 type);
530 ret = (*ent->f)(pmonitor->m_sendfd, &m);
531 buffer_free(&m);
532
533 /* The child may use this request only once, disable it */
534 if (ent->flags & MON_ONCE) {
535 debug2("%s: %d used once, disabling now", __func__,
536 type);
537 ent->flags &= ~MON_PERMIT;
538 }
539
540 if (pent != NULL)
541 *pent = ent;
542
543 return ret;
544 }
545
546 fatal("%s: unsupported request: %d", __func__, type);
547
548 /* NOTREACHED */
549 return (-1);
550}
551
552/* allowed key state */
553static int
554monitor_allowed_key(u_char *blob, u_int bloblen)
555{
556 /* make sure key is allowed */
557 if (key_blob == NULL || key_bloblen != bloblen ||
558 memcmp(key_blob, blob, key_bloblen))
559 return (0);
560 return (1);
561}
562
563static void
564monitor_reset_key_state(void)
565{
566 /* reset state */
567 if (key_blob != NULL)
568 xfree(key_blob);
569 if (hostbased_cuser != NULL)
570 xfree(hostbased_cuser);
571 if (hostbased_chost != NULL)
572 xfree(hostbased_chost);
573 key_blob = NULL;
574 key_bloblen = 0;
575 key_blobtype = MM_NOKEY;
576 hostbased_cuser = NULL;
577 hostbased_chost = NULL;
578}
579
580int
581mm_answer_moduli(int sock, Buffer *m)
582{
583 DH *dh;
584 int min, want, max;
585
586 min = buffer_get_int(m);
587 want = buffer_get_int(m);
588 max = buffer_get_int(m);
589
590 debug3("%s: got parameters: %d %d %d",
591 __func__, min, want, max);
592 /* We need to check here, too, in case the child got corrupted */
593 if (max < min || want < min || max < want)
594 fatal("%s: bad parameters: %d %d %d",
595 __func__, min, want, max);
596
597 buffer_clear(m);
598
599 dh = choose_dh(min, want, max);
600 if (dh == NULL) {
601 buffer_put_char(m, 0);
602 return (0);
603 } else {
604 /* Send first bignum */
605 buffer_put_char(m, 1);
606 buffer_put_bignum2(m, dh->p);
607 buffer_put_bignum2(m, dh->g);
608
609 DH_free(dh);
610 }
611 mm_request_send(sock, MONITOR_ANS_MODULI, m);
612 return (0);
613}
614
615int
616mm_answer_sign(int sock, Buffer *m)
617{
618 Key *key;
619 u_char *p;
620 u_char *signature;
621 u_int siglen, datlen;
622 int keyid;
623
624 debug3("%s", __func__);
625
626 keyid = buffer_get_int(m);
627 p = buffer_get_string(m, &datlen);
628
629 /*
630 * Supported KEX types will only return SHA1 (20 byte) or
631 * SHA256 (32 byte) hashes
632 */
633 if (datlen != 20 && datlen != 32)
634 fatal("%s: data length incorrect: %u", __func__, datlen);
635
636 /* save session id, it will be passed on the first call */
637 if (session_id2_len == 0) {
638 session_id2_len = datlen;
639 session_id2 = xmalloc(session_id2_len);
640 memcpy(session_id2, p, session_id2_len);
641 }
642
643 if ((key = get_hostkey_by_index(keyid)) == NULL)
644 fatal("%s: no hostkey from index %d", __func__, keyid);
645 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
646 fatal("%s: key_sign failed", __func__);
647
648 debug3("%s: signature %p(%u)", __func__, signature, siglen);
649
650 buffer_clear(m);
651 buffer_put_string(m, signature, siglen);
652
653 xfree(p);
654 xfree(signature);
655
656 mm_request_send(sock, MONITOR_ANS_SIGN, m);
657
658 /* Turn on permissions for getpwnam */
659 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
660
661 return (0);
662}
663
664/* Retrieves the password entry and also checks if the user is permitted */
665
666int
667mm_answer_pwnamallow(int sock, Buffer *m)
668{
669 char *username;
670 struct passwd *pwent;
671 int allowed = 0;
672
673 debug3("%s", __func__);
674
675 username = buffer_get_string(m, NULL);
676
677 pwent = getpwnamallow(username);
678
679 if (authctxt->user) xfree(authctxt->user);
680 authctxt->user = xstrdup(username);
681 setproctitle("%s [priv]", pwent ? username : "unknown");
682 xfree(username);
683
684 buffer_clear(m);
685
686 if (pwent == NULL) {
687 buffer_put_char(m, 0);
688 authctxt->pw = fakepw();
689 goto out;
690 }
691
692 allowed = 1;
693 authctxt->pw = pwent;
694 authctxt->valid = 1;
695
696 buffer_put_char(m, 1);
697 buffer_put_string(m, pwent, sizeof(struct passwd));
698 buffer_put_cstring(m, pwent->pw_name);
699 buffer_put_cstring(m, "*");
700 buffer_put_cstring(m, pwent->pw_gecos);
701#ifdef HAVE_PW_CLASS_IN_PASSWD
702 buffer_put_cstring(m, pwent->pw_class);
703#endif
704 buffer_put_cstring(m, pwent->pw_dir);
705 buffer_put_cstring(m, pwent->pw_shell);
706
707 out:
708 buffer_put_string(m, &options, sizeof(options));
709 if (options.banner != NULL)
710 buffer_put_cstring(m, options.banner);
711 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
712 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
713
714 /* For SSHv1 allow authentication now */
715 if (!compat20)
716 monitor_permit_authentications(1);
717 else {
718 /* Allow service/style information on the auth context */
719 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
720 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
721 }
722
723#ifdef USE_PAM
724 if (options.use_pam)
725 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
726#endif
727
728 return (0);
729}
730
731int mm_answer_auth2_read_banner(int sock, Buffer *m)
732{
733 char *banner;
734
735 buffer_clear(m);
736 banner = auth2_read_banner();
737 buffer_put_cstring(m, banner != NULL ? banner : "");
738 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
739
740 if (banner != NULL)
741 xfree(banner);
742
743 return (0);
744}
745
746int
747mm_answer_authserv(int sock, Buffer *m)
748{
749 monitor_permit_authentications(1);
750
751 authctxt->service = buffer_get_string(m, NULL);
752 authctxt->style = buffer_get_string(m, NULL);
753 debug3("%s: service=%s, style=%s",
754 __func__, authctxt->service, authctxt->style);
755
756 if (strlen(authctxt->style) == 0) {
757 xfree(authctxt->style);
758 authctxt->style = NULL;
759 }
760
761 return (0);
762}
763
764int
765mm_answer_authpassword(int sock, Buffer *m)
766{
767 static int call_count;
768 char *passwd;
769 int authenticated;
770 u_int plen;
771
772 passwd = buffer_get_string(m, &plen);
773 /* Only authenticate if the context is valid */
774 authenticated = options.password_authentication &&
775 auth_password(authctxt, passwd);
776 memset(passwd, 0, strlen(passwd));
777 xfree(passwd);
778
779 buffer_clear(m);
780 buffer_put_int(m, authenticated);
781
782 debug3("%s: sending result %d", __func__, authenticated);
783 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
784
785 call_count++;
786 if (plen == 0 && call_count == 1)
787 auth_method = "none";
788 else
789 auth_method = "password";
790
791 /* Causes monitor loop to terminate if authenticated */
792 return (authenticated);
793}
794
795#ifdef BSD_AUTH
796int
797mm_answer_bsdauthquery(int sock, Buffer *m)
798{
799 char *name, *infotxt;
800 u_int numprompts;
801 u_int *echo_on;
802 char **prompts;
803 u_int success;
804
805 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
806 &prompts, &echo_on) < 0 ? 0 : 1;
807
808 buffer_clear(m);
809 buffer_put_int(m, success);
810 if (success)
811 buffer_put_cstring(m, prompts[0]);
812
813 debug3("%s: sending challenge success: %u", __func__, success);
814 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
815
816 if (success) {
817 xfree(name);
818 xfree(infotxt);
819 xfree(prompts);
820 xfree(echo_on);
821 }
822
823 return (0);
824}
825
826int
827mm_answer_bsdauthrespond(int sock, Buffer *m)
828{
829 char *response;
830 int authok;
831
832 if (authctxt->as == 0)
833 fatal("%s: no bsd auth session", __func__);
834
835 response = buffer_get_string(m, NULL);
836 authok = options.challenge_response_authentication &&
837 auth_userresponse(authctxt->as, response, 0);
838 authctxt->as = NULL;
839 debug3("%s: <%s> = <%d>", __func__, response, authok);
840 xfree(response);
841
842 buffer_clear(m);
843 buffer_put_int(m, authok);
844
845 debug3("%s: sending authenticated: %d", __func__, authok);
846 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
847
848 auth_method = "bsdauth";
849
850 return (authok != 0);
851}
852#endif
853
854#ifdef SKEY
855int
856mm_answer_skeyquery(int sock, Buffer *m)
857{
858 struct skey skey;
859 char challenge[1024];
860 u_int success;
861
862 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
863 sizeof(challenge)) < 0 ? 0 : 1;
864
865 buffer_clear(m);
866 buffer_put_int(m, success);
867 if (success)
868 buffer_put_cstring(m, challenge);
869
870 debug3("%s: sending challenge success: %u", __func__, success);
871 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
872
873 return (0);
874}
875
876int
877mm_answer_skeyrespond(int sock, Buffer *m)
878{
879 char *response;
880 int authok;
881
882 response = buffer_get_string(m, NULL);
883
884 authok = (options.challenge_response_authentication &&
885 authctxt->valid &&
886 skey_haskey(authctxt->pw->pw_name) == 0 &&
887 skey_passcheck(authctxt->pw->pw_name, response) != -1);
888
889 xfree(response);
890
891 buffer_clear(m);
892 buffer_put_int(m, authok);
893
894 debug3("%s: sending authenticated: %d", __func__, authok);
895 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
896
897 auth_method = "skey";
898
899 return (authok != 0);
900}
901#endif
902
903#ifdef USE_PAM
904int
905mm_answer_pam_start(int sock, Buffer *m)
906{
907 if (!options.use_pam)
908 fatal("UsePAM not set, but ended up in %s anyway", __func__);
909
910 start_pam(authctxt);
911
912 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
913
914 return (0);
915}
916
917int
918mm_answer_pam_account(int sock, Buffer *m)
919{
920 u_int ret;
921
922 if (!options.use_pam)
923 fatal("UsePAM not set, but ended up in %s anyway", __func__);
924
925 ret = do_pam_account();
926
927 buffer_put_int(m, ret);
928 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
929
930 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
931
932 return (ret);
933}
934
935static void *sshpam_ctxt, *sshpam_authok;
936extern KbdintDevice sshpam_device;
937
938int
939mm_answer_pam_init_ctx(int sock, Buffer *m)
940{
941
942 debug3("%s", __func__);
943 authctxt->user = buffer_get_string(m, NULL);
944 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
945 sshpam_authok = NULL;
946 buffer_clear(m);
947 if (sshpam_ctxt != NULL) {
948 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
949 buffer_put_int(m, 1);
950 } else {
951 buffer_put_int(m, 0);
952 }
953 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
954 return (0);
955}
956
957int
958mm_answer_pam_query(int sock, Buffer *m)
959{
960 char *name, *info, **prompts;
961 u_int i, num, *echo_on;
962 int ret;
963
964 debug3("%s", __func__);
965 sshpam_authok = NULL;
966 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
967 if (ret == 0 && num == 0)
968 sshpam_authok = sshpam_ctxt;
969 if (num > 1 || name == NULL || info == NULL)
970 ret = -1;
971 buffer_clear(m);
972 buffer_put_int(m, ret);
973 buffer_put_cstring(m, name);
974 xfree(name);
975 buffer_put_cstring(m, info);
976 xfree(info);
977 buffer_put_int(m, num);
978 for (i = 0; i < num; ++i) {
979 buffer_put_cstring(m, prompts[i]);
980 xfree(prompts[i]);
981 buffer_put_int(m, echo_on[i]);
982 }
983 if (prompts != NULL)
984 xfree(prompts);
985 if (echo_on != NULL)
986 xfree(echo_on);
987 auth_method = "keyboard-interactive/pam";
988 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
989 return (0);
990}
991
992int
993mm_answer_pam_respond(int sock, Buffer *m)
994{
995 char **resp;
996 u_int i, num;
997 int ret;
998
999 debug3("%s", __func__);
1000 sshpam_authok = NULL;
1001 num = buffer_get_int(m);
1002 if (num > 0) {
1003 resp = xcalloc(num, sizeof(char *));
1004 for (i = 0; i < num; ++i)
1005 resp[i] = buffer_get_string(m, NULL);
1006 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1007 for (i = 0; i < num; ++i)
1008 xfree(resp[i]);
1009 xfree(resp);
1010 } else {
1011 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1012 }
1013 buffer_clear(m);
1014 buffer_put_int(m, ret);
1015 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1016 auth_method = "keyboard-interactive/pam";
1017 if (ret == 0)
1018 sshpam_authok = sshpam_ctxt;
1019 return (0);
1020}
1021
1022int
1023mm_answer_pam_free_ctx(int sock, Buffer *m)
1024{
1025
1026 debug3("%s", __func__);
1027 (sshpam_device.free_ctx)(sshpam_ctxt);
1028 buffer_clear(m);
1029 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1030 auth_method = "keyboard-interactive/pam";
1031 return (sshpam_authok == sshpam_ctxt);
1032}
1033#endif
1034
1035static void
1036mm_append_debug(Buffer *m)
1037{
1038 if (auth_debug_init && buffer_len(&auth_debug)) {
1039 debug3("%s: Appending debug messages for child", __func__);
1040 buffer_append(m, buffer_ptr(&auth_debug),
1041 buffer_len(&auth_debug));
1042 buffer_clear(&auth_debug);
1043 }
1044}
1045
1046int
1047mm_answer_keyallowed(int sock, Buffer *m)
1048{
1049 Key *key;
1050 char *cuser, *chost;
1051 u_char *blob;
1052 u_int bloblen;
1053 enum mm_keytype type = 0;
1054 int allowed = 0;
1055
1056 debug3("%s entering", __func__);
1057
1058 type = buffer_get_int(m);
1059 cuser = buffer_get_string(m, NULL);
1060 chost = buffer_get_string(m, NULL);
1061 blob = buffer_get_string(m, &bloblen);
1062
1063 key = key_from_blob(blob, bloblen);
1064
1065 if ((compat20 && type == MM_RSAHOSTKEY) ||
1066 (!compat20 && type != MM_RSAHOSTKEY))
1067 fatal("%s: key type and protocol mismatch", __func__);
1068
1069 debug3("%s: key_from_blob: %p", __func__, key);
1070
1071 if (key != NULL && authctxt->valid) {
1072 switch (type) {
1073 case MM_USERKEY:
1074 allowed = options.pubkey_authentication &&
1075 user_key_allowed(authctxt->pw, key);
1076 auth_method = "publickey";
1077 if (options.pubkey_authentication && allowed != 1)
1078 auth_clear_options();
1079 break;
1080 case MM_HOSTKEY:
1081 allowed = options.hostbased_authentication &&
1082 hostbased_key_allowed(authctxt->pw,
1083 cuser, chost, key);
1084 auth_method = "hostbased";
1085 break;
1086 case MM_RSAHOSTKEY:
1087 key->type = KEY_RSA1; /* XXX */
1088 allowed = options.rhosts_rsa_authentication &&
1089 auth_rhosts_rsa_key_allowed(authctxt->pw,
1090 cuser, chost, key);
1091 if (options.rhosts_rsa_authentication && allowed != 1)
1092 auth_clear_options();
1093 auth_method = "rsa";
1094 break;
1095 default:
1096 fatal("%s: unknown key type %d", __func__, type);
1097 break;
1098 }
1099 }
1100 if (key != NULL)
1101 key_free(key);
1102
1103 /* clear temporarily storage (used by verify) */
1104 monitor_reset_key_state();
1105
1106 if (allowed) {
1107 /* Save temporarily for comparison in verify */
1108 key_blob = blob;
1109 key_bloblen = bloblen;
1110 key_blobtype = type;
1111 hostbased_cuser = cuser;
1112 hostbased_chost = chost;
1113 } else {
1114 /* Log failed attempt */
1115 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1116 xfree(blob);
1117 xfree(cuser);
1118 xfree(chost);
1119 }
1120
1121 debug3("%s: key %p is %s",
1122 __func__, key, allowed ? "allowed" : "not allowed");
1123
1124 buffer_clear(m);
1125 buffer_put_int(m, allowed);
1126 buffer_put_int(m, forced_command != NULL);
1127
1128 mm_append_debug(m);
1129
1130 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1131
1132 if (type == MM_RSAHOSTKEY)
1133 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1134
1135 return (0);
1136}
1137
1138static int
1139monitor_valid_userblob(u_char *data, u_int datalen)
1140{
1141 Buffer b;
1142 char *p;
1143 u_int len;
1144 int fail = 0;
1145
1146 buffer_init(&b);
1147 buffer_append(&b, data, datalen);
1148
1149 if (datafellows & SSH_OLD_SESSIONID) {
1150 p = buffer_ptr(&b);
1151 len = buffer_len(&b);
1152 if ((session_id2 == NULL) ||
1153 (len < session_id2_len) ||
1154 (memcmp(p, session_id2, session_id2_len) != 0))
1155 fail++;
1156 buffer_consume(&b, session_id2_len);
1157 } else {
1158 p = buffer_get_string(&b, &len);
1159 if ((session_id2 == NULL) ||
1160 (len != session_id2_len) ||
1161 (memcmp(p, session_id2, session_id2_len) != 0))
1162 fail++;
1163 xfree(p);
1164 }
1165 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1166 fail++;
1167 p = buffer_get_string(&b, NULL);
1168 if (strcmp(authctxt->user, p) != 0) {
1169 logit("wrong user name passed to monitor: expected %s != %.100s",
1170 authctxt->user, p);
1171 fail++;
1172 }
1173 xfree(p);
1174 buffer_skip_string(&b);
1175 if (datafellows & SSH_BUG_PKAUTH) {
1176 if (!buffer_get_char(&b))
1177 fail++;
1178 } else {
1179 p = buffer_get_string(&b, NULL);
1180 if (strcmp("publickey", p) != 0)
1181 fail++;
1182 xfree(p);
1183 if (!buffer_get_char(&b))
1184 fail++;
1185 buffer_skip_string(&b);
1186 }
1187 buffer_skip_string(&b);
1188 if (buffer_len(&b) != 0)
1189 fail++;
1190 buffer_free(&b);
1191 return (fail == 0);
1192}
1193
1194static int
1195monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1196 char *chost)
1197{
1198 Buffer b;
1199 char *p;
1200 u_int len;
1201 int fail = 0;
1202
1203 buffer_init(&b);
1204 buffer_append(&b, data, datalen);
1205
1206 p = buffer_get_string(&b, &len);
1207 if ((session_id2 == NULL) ||
1208 (len != session_id2_len) ||
1209 (memcmp(p, session_id2, session_id2_len) != 0))
1210 fail++;
1211 xfree(p);
1212
1213 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1214 fail++;
1215 p = buffer_get_string(&b, NULL);
1216 if (strcmp(authctxt->user, p) != 0) {
1217 logit("wrong user name passed to monitor: expected %s != %.100s",
1218 authctxt->user, p);
1219 fail++;
1220 }
1221 xfree(p);
1222 buffer_skip_string(&b); /* service */
1223 p = buffer_get_string(&b, NULL);
1224 if (strcmp(p, "hostbased") != 0)
1225 fail++;
1226 xfree(p);
1227 buffer_skip_string(&b); /* pkalg */
1228 buffer_skip_string(&b); /* pkblob */
1229
1230 /* verify client host, strip trailing dot if necessary */
1231 p = buffer_get_string(&b, NULL);
1232 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1233 p[len - 1] = '\0';
1234 if (strcmp(p, chost) != 0)
1235 fail++;
1236 xfree(p);
1237
1238 /* verify client user */
1239 p = buffer_get_string(&b, NULL);
1240 if (strcmp(p, cuser) != 0)
1241 fail++;
1242 xfree(p);
1243
1244 if (buffer_len(&b) != 0)
1245 fail++;
1246 buffer_free(&b);
1247 return (fail == 0);
1248}
1249
1250int
1251mm_answer_keyverify(int sock, Buffer *m)
1252{
1253 Key *key;
1254 u_char *signature, *data, *blob;
1255 u_int signaturelen, datalen, bloblen;
1256 int verified = 0;
1257 int valid_data = 0;
1258
1259 blob = buffer_get_string(m, &bloblen);
1260 signature = buffer_get_string(m, &signaturelen);
1261 data = buffer_get_string(m, &datalen);
1262
1263 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1264 !monitor_allowed_key(blob, bloblen))
1265 fatal("%s: bad key, not previously allowed", __func__);
1266
1267 key = key_from_blob(blob, bloblen);
1268 if (key == NULL)
1269 fatal("%s: bad public key blob", __func__);
1270
1271 switch (key_blobtype) {
1272 case MM_USERKEY:
1273 valid_data = monitor_valid_userblob(data, datalen);
1274 break;
1275 case MM_HOSTKEY:
1276 valid_data = monitor_valid_hostbasedblob(data, datalen,
1277 hostbased_cuser, hostbased_chost);
1278 break;
1279 default:
1280 valid_data = 0;
1281 break;
1282 }
1283 if (!valid_data)
1284 fatal("%s: bad signature data blob", __func__);
1285
1286 verified = key_verify(key, signature, signaturelen, data, datalen);
1287 debug3("%s: key %p signature %s",
1288 __func__, key, (verified == 1) ? "verified" : "unverified");
1289
1290 key_free(key);
1291 xfree(blob);
1292 xfree(signature);
1293 xfree(data);
1294
1295 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1296
1297 monitor_reset_key_state();
1298
1299 buffer_clear(m);
1300 buffer_put_int(m, verified);
1301 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1302
1303 return (verified == 1);
1304}
1305
1306static void
1307mm_record_login(Session *s, struct passwd *pw)
1308{
1309 socklen_t fromlen;
1310 struct sockaddr_storage from;
1311
1312 /*
1313 * Get IP address of client. If the connection is not a socket, let
1314 * the address be 0.0.0.0.
1315 */
1316 memset(&from, 0, sizeof(from));
1317 fromlen = sizeof(from);
1318 if (packet_connection_is_on_socket()) {
1319 if (getpeername(packet_get_connection_in(),
1320 (struct sockaddr *)&from, &fromlen) < 0) {
1321 debug("getpeername: %.100s", strerror(errno));
1322 cleanup_exit(255);
1323 }
1324 }
1325 /* Record that there was a login on that tty from the remote host. */
1326 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1327 get_remote_name_or_ip(utmp_len, options.use_dns),
1328 (struct sockaddr *)&from, fromlen);
1329}
1330
1331static void
1332mm_session_close(Session *s)
1333{
1334 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1335 if (s->ttyfd != -1) {
1336 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1337 session_pty_cleanup2(s);
1338 }
1339 session_unused(s->self);
1340}
1341
1342int
1343mm_answer_pty(int sock, Buffer *m)
1344{
1345 extern struct monitor *pmonitor;
1346 Session *s;
1347 int res, fd0;
1348
1349 debug3("%s entering", __func__);
1350
1351 buffer_clear(m);
1352 s = session_new();
1353 if (s == NULL)
1354 goto error;
1355 s->authctxt = authctxt;
1356 s->pw = authctxt->pw;
1357 s->pid = pmonitor->m_pid;
1358 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1359 if (res == 0)
1360 goto error;
1361 pty_setowner(authctxt->pw, s->tty);
1362
1363 buffer_put_int(m, 1);
1364 buffer_put_cstring(m, s->tty);
1365
1366 /* We need to trick ttyslot */
1367 if (dup2(s->ttyfd, 0) == -1)
1368 fatal("%s: dup2", __func__);
1369
1370 mm_record_login(s, authctxt->pw);
1371
1372 /* Now we can close the file descriptor again */
1373 close(0);
1374
1375 /* send messages generated by record_login */
1376 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1377 buffer_clear(&loginmsg);
1378
1379 mm_request_send(sock, MONITOR_ANS_PTY, m);
1380
1381 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1382 mm_send_fd(sock, s->ttyfd) == -1)
1383 fatal("%s: send fds failed", __func__);
1384
1385 /* make sure nothing uses fd 0 */
1386 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1387 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1388 if (fd0 != 0)
1389 error("%s: fd0 %d != 0", __func__, fd0);
1390
1391 /* slave is not needed */
1392 close(s->ttyfd);
1393 s->ttyfd = s->ptyfd;
1394 /* no need to dup() because nobody closes ptyfd */
1395 s->ptymaster = s->ptyfd;
1396
1397 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1398
1399 return (0);
1400
1401 error:
1402 if (s != NULL)
1403 mm_session_close(s);
1404 buffer_put_int(m, 0);
1405 mm_request_send(sock, MONITOR_ANS_PTY, m);
1406 return (0);
1407}
1408
1409int
1410mm_answer_pty_cleanup(int sock, Buffer *m)
1411{
1412 Session *s;
1413 char *tty;
1414
1415 debug3("%s entering", __func__);
1416
1417 tty = buffer_get_string(m, NULL);
1418 if ((s = session_by_tty(tty)) != NULL)
1419 mm_session_close(s);
1420 buffer_clear(m);
1421 xfree(tty);
1422 return (0);
1423}
1424
1425int
1426mm_answer_sesskey(int sock, Buffer *m)
1427{
1428 BIGNUM *p;
1429 int rsafail;
1430
1431 /* Turn off permissions */
1432 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1433
1434 if ((p = BN_new()) == NULL)
1435 fatal("%s: BN_new", __func__);
1436
1437 buffer_get_bignum2(m, p);
1438
1439 rsafail = ssh1_session_key(p);
1440
1441 buffer_clear(m);
1442 buffer_put_int(m, rsafail);
1443 buffer_put_bignum2(m, p);
1444
1445 BN_clear_free(p);
1446
1447 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1448
1449 /* Turn on permissions for sessid passing */
1450 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1451
1452 return (0);
1453}
1454
1455int
1456mm_answer_sessid(int sock, Buffer *m)
1457{
1458 int i;
1459
1460 debug3("%s entering", __func__);
1461
1462 if (buffer_len(m) != 16)
1463 fatal("%s: bad ssh1 session id", __func__);
1464 for (i = 0; i < 16; i++)
1465 session_id[i] = buffer_get_char(m);
1466
1467 /* Turn on permissions for getpwnam */
1468 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1469
1470 return (0);
1471}
1472
1473int
1474mm_answer_rsa_keyallowed(int sock, Buffer *m)
1475{
1476 BIGNUM *client_n;
1477 Key *key = NULL;
1478 u_char *blob = NULL;
1479 u_int blen = 0;
1480 int allowed = 0;
1481
1482 debug3("%s entering", __func__);
1483
1484 auth_method = "rsa";
1485 if (options.rsa_authentication && authctxt->valid) {
1486 if ((client_n = BN_new()) == NULL)
1487 fatal("%s: BN_new", __func__);
1488 buffer_get_bignum2(m, client_n);
1489 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1490 BN_clear_free(client_n);
1491 }
1492 buffer_clear(m);
1493 buffer_put_int(m, allowed);
1494 buffer_put_int(m, forced_command != NULL);
1495
1496 /* clear temporarily storage (used by generate challenge) */
1497 monitor_reset_key_state();
1498
1499 if (allowed && key != NULL) {
1500 key->type = KEY_RSA; /* cheat for key_to_blob */
1501 if (key_to_blob(key, &blob, &blen) == 0)
1502 fatal("%s: key_to_blob failed", __func__);
1503 buffer_put_string(m, blob, blen);
1504
1505 /* Save temporarily for comparison in verify */
1506 key_blob = blob;
1507 key_bloblen = blen;
1508 key_blobtype = MM_RSAUSERKEY;
1509 }
1510 if (key != NULL)
1511 key_free(key);
1512
1513 mm_append_debug(m);
1514
1515 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1516
1517 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1518 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1519 return (0);
1520}
1521
1522int
1523mm_answer_rsa_challenge(int sock, Buffer *m)
1524{
1525 Key *key = NULL;
1526 u_char *blob;
1527 u_int blen;
1528
1529 debug3("%s entering", __func__);
1530
1531 if (!authctxt->valid)
1532 fatal("%s: authctxt not valid", __func__);
1533 blob = buffer_get_string(m, &blen);
1534 if (!monitor_allowed_key(blob, blen))
1535 fatal("%s: bad key, not previously allowed", __func__);
1536 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1537 fatal("%s: key type mismatch", __func__);
1538 if ((key = key_from_blob(blob, blen)) == NULL)
1539 fatal("%s: received bad key", __func__);
1540 if (key->type != KEY_RSA)
1541 fatal("%s: received bad key type %d", __func__, key->type);
1542 key->type = KEY_RSA1;
1543 if (ssh1_challenge)
1544 BN_clear_free(ssh1_challenge);
1545 ssh1_challenge = auth_rsa_generate_challenge(key);
1546
1547 buffer_clear(m);
1548 buffer_put_bignum2(m, ssh1_challenge);
1549
1550 debug3("%s sending reply", __func__);
1551 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1552
1553 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1554
1555 xfree(blob);
1556 key_free(key);
1557 return (0);
1558}
1559
1560int
1561mm_answer_rsa_response(int sock, Buffer *m)
1562{
1563 Key *key = NULL;
1564 u_char *blob, *response;
1565 u_int blen, len;
1566 int success;
1567
1568 debug3("%s entering", __func__);
1569
1570 if (!authctxt->valid)
1571 fatal("%s: authctxt not valid", __func__);
1572 if (ssh1_challenge == NULL)
1573 fatal("%s: no ssh1_challenge", __func__);
1574
1575 blob = buffer_get_string(m, &blen);
1576 if (!monitor_allowed_key(blob, blen))
1577 fatal("%s: bad key, not previously allowed", __func__);
1578 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1579 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1580 if ((key = key_from_blob(blob, blen)) == NULL)
1581 fatal("%s: received bad key", __func__);
1582 response = buffer_get_string(m, &len);
1583 if (len != 16)
1584 fatal("%s: received bad response to challenge", __func__);
1585 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1586
1587 xfree(blob);
1588 key_free(key);
1589 xfree(response);
1590
1591 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1592
1593 /* reset state */
1594 BN_clear_free(ssh1_challenge);
1595 ssh1_challenge = NULL;
1596 monitor_reset_key_state();
1597
1598 buffer_clear(m);
1599 buffer_put_int(m, success);
1600 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1601
1602 return (success);
1603}
1604
1605int
1606mm_answer_term(int sock, Buffer *req)
1607{
1608 extern struct monitor *pmonitor;
1609 int res, status;
1610
1611 debug3("%s: tearing down sessions", __func__);
1612
1613 /* The child is terminating */
1614 session_destroy_all(&mm_session_close);
1615
1616#ifdef USE_PAM
1617 if (options.use_pam)
1618 sshpam_cleanup();
1619#endif
1620
1621 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1622 if (errno != EINTR)
1623 exit(1);
1624
1625 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1626
1627 /* Terminate process */
1628 exit(res);
1629}
1630
1631#ifdef SSH_AUDIT_EVENTS
1632/* Report that an audit event occurred */
1633int
1634mm_answer_audit_event(int socket, Buffer *m)
1635{
1636 ssh_audit_event_t event;
1637
1638 debug3("%s entering", __func__);
1639
1640 event = buffer_get_int(m);
1641 switch(event) {
1642 case SSH_AUTH_FAIL_PUBKEY:
1643 case SSH_AUTH_FAIL_HOSTBASED:
1644 case SSH_AUTH_FAIL_GSSAPI:
1645 case SSH_LOGIN_EXCEED_MAXTRIES:
1646 case SSH_LOGIN_ROOT_DENIED:
1647 case SSH_CONNECTION_CLOSE:
1648 case SSH_INVALID_USER:
1649 audit_event(event);
1650 break;
1651 default:
1652 fatal("Audit event type %d not permitted", event);
1653 }
1654
1655 return (0);
1656}
1657
1658int
1659mm_answer_audit_command(int socket, Buffer *m)
1660{
1661 u_int len;
1662 char *cmd;
1663
1664 debug3("%s entering", __func__);
1665 cmd = buffer_get_string(m, &len);
1666 /* sanity check command, if so how? */
1667 audit_run_command(cmd);
1668 xfree(cmd);
1669 return (0);
1670}
1671#endif /* SSH_AUDIT_EVENTS */
1672
1673void
1674monitor_apply_keystate(struct monitor *pmonitor)
1675{
1676 if (compat20) {
1677 set_newkeys(MODE_IN);
1678 set_newkeys(MODE_OUT);
1679 } else {
1680 packet_set_protocol_flags(child_state.ssh1protoflags);
1681 packet_set_encryption_key(child_state.ssh1key,
1682 child_state.ssh1keylen, child_state.ssh1cipher);
1683 xfree(child_state.ssh1key);
1684 }
1685
1686 /* for rc4 and other stateful ciphers */
1687 packet_set_keycontext(MODE_OUT, child_state.keyout);
1688 xfree(child_state.keyout);
1689 packet_set_keycontext(MODE_IN, child_state.keyin);
1690 xfree(child_state.keyin);
1691
1692 if (!compat20) {
1693 packet_set_iv(MODE_OUT, child_state.ivout);
1694 xfree(child_state.ivout);
1695 packet_set_iv(MODE_IN, child_state.ivin);
1696 xfree(child_state.ivin);
1697 }
1698
1699 memcpy(&incoming_stream, &child_state.incoming,
1700 sizeof(incoming_stream));
1701 memcpy(&outgoing_stream, &child_state.outgoing,
1702 sizeof(outgoing_stream));
1703
1704 /* Update with new address */
1705 if (options.compression)
1706 mm_init_compression(pmonitor->m_zlib);
1707
1708 /* Network I/O buffers */
1709 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1710 buffer_clear(&input);
1711 buffer_append(&input, child_state.input, child_state.ilen);
1712 memset(child_state.input, 0, child_state.ilen);
1713 xfree(child_state.input);
1714
1715 buffer_clear(&output);
1716 buffer_append(&output, child_state.output, child_state.olen);
1717 memset(child_state.output, 0, child_state.olen);
1718 xfree(child_state.output);
1719}
1720
1721static Kex *
1722mm_get_kex(Buffer *m)
1723{
1724 Kex *kex;
1725 void *blob;
1726 u_int bloblen;
1727
1728 kex = xcalloc(1, sizeof(*kex));
1729 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1730 if ((session_id2 == NULL) ||
1731 (kex->session_id_len != session_id2_len) ||
1732 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1733 fatal("mm_get_get: internal error: bad session id");
1734 kex->we_need = buffer_get_int(m);
1735 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1736 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1737 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1738 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1739#ifdef GSSAPI
1740 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
1741 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
1742 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
1743#endif
1744 kex->server = 1;
1745 kex->hostkey_type = buffer_get_int(m);
1746 kex->kex_type = buffer_get_int(m);
1747 blob = buffer_get_string(m, &bloblen);
1748 buffer_init(&kex->my);
1749 buffer_append(&kex->my, blob, bloblen);
1750 xfree(blob);
1751 blob = buffer_get_string(m, &bloblen);
1752 buffer_init(&kex->peer);
1753 buffer_append(&kex->peer, blob, bloblen);
1754 xfree(blob);
1755 kex->done = 1;
1756 kex->flags = buffer_get_int(m);
1757 kex->client_version_string = buffer_get_string(m, NULL);
1758 kex->server_version_string = buffer_get_string(m, NULL);
1759 kex->load_host_key=&get_hostkey_by_type;
1760 kex->host_key_index=&get_hostkey_index;
1761
1762 return (kex);
1763}
1764
1765/* This function requries careful sanity checking */
1766
1767void
1768mm_get_keystate(struct monitor *pmonitor)
1769{
1770 Buffer m;
1771 u_char *blob, *p;
1772 u_int bloblen, plen;
1773 u_int32_t seqnr, packets;
1774 u_int64_t blocks, bytes;
1775
1776 debug3("%s: Waiting for new keys", __func__);
1777
1778 buffer_init(&m);
1779 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1780 if (!compat20) {
1781 child_state.ssh1protoflags = buffer_get_int(&m);
1782 child_state.ssh1cipher = buffer_get_int(&m);
1783 child_state.ssh1key = buffer_get_string(&m,
1784 &child_state.ssh1keylen);
1785 child_state.ivout = buffer_get_string(&m,
1786 &child_state.ivoutlen);
1787 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1788 goto skip;
1789 } else {
1790 /* Get the Kex for rekeying */
1791 *pmonitor->m_pkex = mm_get_kex(&m);
1792 }
1793
1794 blob = buffer_get_string(&m, &bloblen);
1795 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1796 xfree(blob);
1797
1798 debug3("%s: Waiting for second key", __func__);
1799 blob = buffer_get_string(&m, &bloblen);
1800 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1801 xfree(blob);
1802
1803 /* Now get sequence numbers for the packets */
1804 seqnr = buffer_get_int(&m);
1805 blocks = buffer_get_int64(&m);
1806 packets = buffer_get_int(&m);
1807 bytes = buffer_get_int64(&m);
1808 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
1809 seqnr = buffer_get_int(&m);
1810 blocks = buffer_get_int64(&m);
1811 packets = buffer_get_int(&m);
1812 bytes = buffer_get_int64(&m);
1813 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
1814
1815 skip:
1816 /* Get the key context */
1817 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1818 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1819
1820 debug3("%s: Getting compression state", __func__);
1821 /* Get compression state */
1822 p = buffer_get_string(&m, &plen);
1823 if (plen != sizeof(child_state.outgoing))
1824 fatal("%s: bad request size", __func__);
1825 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1826 xfree(p);
1827
1828 p = buffer_get_string(&m, &plen);
1829 if (plen != sizeof(child_state.incoming))
1830 fatal("%s: bad request size", __func__);
1831 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1832 xfree(p);
1833
1834 /* Network I/O buffers */
1835 debug3("%s: Getting Network I/O buffers", __func__);
1836 child_state.input = buffer_get_string(&m, &child_state.ilen);
1837 child_state.output = buffer_get_string(&m, &child_state.olen);
1838
1839 buffer_free(&m);
1840}
1841
1842
1843/* Allocation functions for zlib */
1844void *
1845mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1846{
1847 size_t len = (size_t) size * ncount;
1848 void *address;
1849
1850 if (len == 0 || ncount > SIZE_T_MAX / size)
1851 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1852
1853 address = mm_malloc(mm, len);
1854
1855 return (address);
1856}
1857
1858void
1859mm_zfree(struct mm_master *mm, void *address)
1860{
1861 mm_free(mm, address);
1862}
1863
1864void
1865mm_init_compression(struct mm_master *mm)
1866{
1867 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1868 outgoing_stream.zfree = (free_func)mm_zfree;
1869 outgoing_stream.opaque = mm;
1870
1871 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1872 incoming_stream.zfree = (free_func)mm_zfree;
1873 incoming_stream.opaque = mm;
1874}
1875
1876/* XXX */
1877
1878#define FD_CLOSEONEXEC(x) do { \
1879 if (fcntl(x, F_SETFD, 1) == -1) \
1880 fatal("fcntl(%d, F_SETFD)", x); \
1881} while (0)
1882
1883static void
1884monitor_socketpair(int *pair)
1885{
1886#ifdef HAVE_SOCKETPAIR
1887 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1888 fatal("%s: socketpair", __func__);
1889#else
1890 fatal("%s: UsePrivilegeSeparation=yes not supported",
1891 __func__);
1892#endif
1893 FD_CLOSEONEXEC(pair[0]);
1894 FD_CLOSEONEXEC(pair[1]);
1895}
1896
1897#define MM_MEMSIZE 65536
1898
1899struct monitor *
1900monitor_init(void)
1901{
1902 struct monitor *mon;
1903 int pair[2];
1904
1905 mon = xcalloc(1, sizeof(*mon));
1906
1907 monitor_socketpair(pair);
1908
1909 mon->m_recvfd = pair[0];
1910 mon->m_sendfd = pair[1];
1911
1912 /* Used to share zlib space across processes */
1913 if (options.compression) {
1914 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1915 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1916
1917 /* Compression needs to share state across borders */
1918 mm_init_compression(mon->m_zlib);
1919 }
1920
1921 return mon;
1922}
1923
1924void
1925monitor_reinit(struct monitor *mon)
1926{
1927 int pair[2];
1928
1929 monitor_socketpair(pair);
1930
1931 mon->m_recvfd = pair[0];
1932 mon->m_sendfd = pair[1];
1933}
1934
1935#ifdef GSSAPI
1936int
1937mm_answer_gss_setup_ctx(int sock, Buffer *m)
1938{
1939 gss_OID_desc goid;
1940 OM_uint32 major;
1941 u_int len;
1942
1943 goid.elements = buffer_get_string(m, &len);
1944 goid.length = len;
1945
1946 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1947
1948 xfree(goid.elements);
1949
1950 buffer_clear(m);
1951 buffer_put_int(m, major);
1952
1953 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1954
1955 /* Now we have a context, enable the step */
1956 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1957
1958 return (0);
1959}
1960
1961int
1962mm_answer_gss_accept_ctx(int sock, Buffer *m)
1963{
1964 gss_buffer_desc in;
1965 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1966 OM_uint32 major, minor;
1967 OM_uint32 flags = 0; /* GSI needs this */
1968 u_int len;
1969
1970 in.value = buffer_get_string(m, &len);
1971 in.length = len;
1972 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1973 xfree(in.value);
1974
1975 buffer_clear(m);
1976 buffer_put_int(m, major);
1977 buffer_put_string(m, out.value, out.length);
1978 buffer_put_int(m, flags);
1979 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1980
1981 gss_release_buffer(&minor, &out);
1982
1983 if (major == GSS_S_COMPLETE) {
1984 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1985 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1986 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
1987 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1988 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
1989 }
1990 return (0);
1991}
1992
1993int
1994mm_answer_gss_checkmic(int sock, Buffer *m)
1995{
1996 gss_buffer_desc gssbuf, mic;
1997 OM_uint32 ret;
1998 u_int len;
1999
2000 gssbuf.value = buffer_get_string(m, &len);
2001 gssbuf.length = len;
2002 mic.value = buffer_get_string(m, &len);
2003 mic.length = len;
2004
2005 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
2006
2007 xfree(gssbuf.value);
2008 xfree(mic.value);
2009
2010 buffer_clear(m);
2011 buffer_put_int(m, ret);
2012
2013 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
2014
2015 if (!GSS_ERROR(ret))
2016 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2017
2018 return (0);
2019}
2020
2021int
2022mm_answer_gss_userok(int sock, Buffer *m)
2023{
2024 int authenticated;
2025
2026 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2027
2028 buffer_clear(m);
2029 buffer_put_int(m, authenticated);
2030
2031 debug3("%s: sending result %d", __func__, authenticated);
2032 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
2033
2034 auth_method = "gssapi-with-mic";
2035
2036 /* Monitor loop will terminate if authenticated */
2037 return (authenticated);
2038}
2039
2040int
2041mm_answer_gss_sign(int socket, Buffer *m)
2042{
2043 gss_buffer_desc data;
2044 gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
2045 OM_uint32 major, minor;
2046 u_int len;
2047
2048 data.value = buffer_get_string(m, &len);
2049 data.length = len;
2050 if (data.length != 20)
2051 fatal("%s: data length incorrect: %d", __func__, (int)data.length);
2052
2053 /* Save the session ID on the first time around */
2054 if (session_id2_len == 0) {
2055 session_id2_len = data.length;
2056 session_id2 = xmalloc(session_id2_len);
2057 memcpy(session_id2, data.value, session_id2_len);
2058 }
2059 major = ssh_gssapi_sign(gsscontext, &data, &hash);
2060
2061 xfree(data.value);
2062
2063 buffer_clear(m);
2064 buffer_put_int(m, major);
2065 buffer_put_string(m, hash.value, hash.length);
2066
2067 mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
2068
2069 gss_release_buffer(&minor, &hash);
2070
2071 /* Turn on getpwnam permissions */
2072 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
2073
2074 return (0);
2075}
2076
2077int
2078mm_answer_gss_error(int socket, Buffer *m) {
2079 OM_uint32 major,minor;
2080 char *msg;
2081
2082 msg=ssh_gssapi_last_error(gsscontext,&major,&minor);
2083 buffer_clear(m);
2084 buffer_put_int(m,major);
2085 buffer_put_int(m,minor);
2086 buffer_put_cstring(m,msg);
2087
2088 mm_request_send(socket,MONITOR_ANS_GSSERR,m);
2089
2090 xfree(msg);
2091
2092 return(0);
2093}
2094
2095int
2096mm_answer_gss_indicate_mechs(int socket, Buffer *m) {
2097 OM_uint32 major,minor;
2098 gss_OID_set mech_set;
2099 size_t i;
2100
2101 major=gss_indicate_mechs(&minor, &mech_set);
2102
2103 buffer_clear(m);
2104 buffer_put_int(m, major);
2105 buffer_put_int(m, mech_set->count);
2106 for (i=0; i < mech_set->count; i++) {
2107 buffer_put_string(m, mech_set->elements[i].elements,
2108 mech_set->elements[i].length);
2109 }
2110
2111#if !defined(MECHGLUE) /* mechglue memory management bug ??? */
2112 gss_release_oid_set(&minor,&mech_set);
2113#endif
2114
2115 mm_request_send(socket,MONITOR_ANS_GSSMECHS,m);
2116
2117 return(0);
2118}
2119
2120int
2121mm_answer_gss_localname(int socket, Buffer *m) {
2122 char *name;
2123
2124 ssh_gssapi_localname(&name);
2125
2126 buffer_clear(m);
2127 if (name) {
2128 buffer_put_cstring(m, name);
2129 debug3("%s: sending result %s", __func__, name);
2130 xfree(name);
2131 } else {
2132 buffer_put_cstring(m, "");
2133 debug3("%s: sending result \"\"", __func__);
2134 }
2135
2136 mm_request_send(socket, MONITOR_ANS_GSSLOCALNAME, m);
2137
2138 return(0);
2139}
2140#endif /* GSSAPI */
2141
2142#ifdef JPAKE
2143int
2144mm_answer_jpake_step1(int sock, Buffer *m)
2145{
2146 struct jpake_ctx *pctx;
2147 u_char *x3_proof, *x4_proof;
2148 u_int x3_proof_len, x4_proof_len;
2149
2150 if (!options.zero_knowledge_password_authentication)
2151 fatal("zero_knowledge_password_authentication disabled");
2152
2153 if (authctxt->jpake_ctx != NULL)
2154 fatal("%s: authctxt->jpake_ctx already set (%p)",
2155 __func__, authctxt->jpake_ctx);
2156 authctxt->jpake_ctx = pctx = jpake_new();
2157
2158 jpake_step1(pctx->grp,
2159 &pctx->server_id, &pctx->server_id_len,
2160 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2161 &x3_proof, &x3_proof_len,
2162 &x4_proof, &x4_proof_len);
2163
2164 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2165
2166 buffer_clear(m);
2167
2168 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2169 buffer_put_bignum2(m, pctx->g_x3);
2170 buffer_put_bignum2(m, pctx->g_x4);
2171 buffer_put_string(m, x3_proof, x3_proof_len);
2172 buffer_put_string(m, x4_proof, x4_proof_len);
2173
2174 debug3("%s: sending step1", __func__);
2175 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2176
2177 bzero(x3_proof, x3_proof_len);
2178 bzero(x4_proof, x4_proof_len);
2179 xfree(x3_proof);
2180 xfree(x4_proof);
2181
2182 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2183 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2184
2185 return 0;
2186}
2187
2188int
2189mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2190{
2191 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2192 char *hash_scheme, *salt;
2193
2194 if (pctx == NULL)
2195 fatal("%s: pctx == NULL", __func__);
2196
2197 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2198
2199 buffer_clear(m);
2200 /* pctx->s is sensitive, not returned to slave */
2201 buffer_put_cstring(m, hash_scheme);
2202 buffer_put_cstring(m, salt);
2203
2204 debug3("%s: sending pwdata", __func__);
2205 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2206
2207 bzero(hash_scheme, strlen(hash_scheme));
2208 bzero(salt, strlen(salt));
2209 xfree(hash_scheme);
2210 xfree(salt);
2211
2212 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2213
2214 return 0;
2215}
2216
2217int
2218mm_answer_jpake_step2(int sock, Buffer *m)
2219{
2220 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2221 u_char *x1_proof, *x2_proof, *x4_s_proof;
2222 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2223
2224 if (pctx == NULL)
2225 fatal("%s: pctx == NULL", __func__);
2226
2227 if ((pctx->g_x1 = BN_new()) == NULL ||
2228 (pctx->g_x2 = BN_new()) == NULL)
2229 fatal("%s: BN_new", __func__);
2230 buffer_get_bignum2(m, pctx->g_x1);
2231 buffer_get_bignum2(m, pctx->g_x2);
2232 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2233 x1_proof = buffer_get_string(m, &x1_proof_len);
2234 x2_proof = buffer_get_string(m, &x2_proof_len);
2235
2236 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2237 pctx->g_x1, pctx->g_x2, pctx->x4,
2238 pctx->client_id, pctx->client_id_len,
2239 pctx->server_id, pctx->server_id_len,
2240 x1_proof, x1_proof_len,
2241 x2_proof, x2_proof_len,
2242 &pctx->b,
2243 &x4_s_proof, &x4_s_proof_len);
2244
2245 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2246
2247 bzero(x1_proof, x1_proof_len);
2248 bzero(x2_proof, x2_proof_len);
2249 xfree(x1_proof);
2250 xfree(x2_proof);
2251
2252 buffer_clear(m);
2253
2254 buffer_put_bignum2(m, pctx->b);
2255 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2256
2257 debug3("%s: sending step2", __func__);
2258 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2259
2260 bzero(x4_s_proof, x4_s_proof_len);
2261 xfree(x4_s_proof);
2262
2263 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2264
2265 return 0;
2266}
2267
2268int
2269mm_answer_jpake_key_confirm(int sock, Buffer *m)
2270{
2271 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2272 u_char *x2_s_proof;
2273 u_int x2_s_proof_len;
2274
2275 if (pctx == NULL)
2276 fatal("%s: pctx == NULL", __func__);
2277
2278 if ((pctx->a = BN_new()) == NULL)
2279 fatal("%s: BN_new", __func__);
2280 buffer_get_bignum2(m, pctx->a);
2281 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2282
2283 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2284 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2285 pctx->server_id, pctx->server_id_len,
2286 pctx->client_id, pctx->client_id_len,
2287 session_id2, session_id2_len,
2288 x2_s_proof, x2_s_proof_len,
2289 &pctx->k,
2290 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2291
2292 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2293
2294 bzero(x2_s_proof, x2_s_proof_len);
2295 buffer_clear(m);
2296
2297 /* pctx->k is sensitive, not sent */
2298 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2299
2300 debug3("%s: sending confirmation hash", __func__);
2301 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2302
2303 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2304
2305 return 0;
2306}
2307
2308int
2309mm_answer_jpake_check_confirm(int sock, Buffer *m)
2310{
2311 int authenticated = 0;
2312 u_char *peer_confirm_hash;
2313 u_int peer_confirm_hash_len;
2314 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2315
2316 if (pctx == NULL)
2317 fatal("%s: pctx == NULL", __func__);
2318
2319 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2320
2321 authenticated = jpake_check_confirm(pctx->k,
2322 pctx->client_id, pctx->client_id_len,
2323 session_id2, session_id2_len,
2324 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2325
2326 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2327
2328 bzero(peer_confirm_hash, peer_confirm_hash_len);
2329 xfree(peer_confirm_hash);
2330
2331 buffer_clear(m);
2332 buffer_put_int(m, authenticated);
2333
2334 debug3("%s: sending result %d", __func__, authenticated);
2335 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2336
2337 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2338
2339 auth_method = "jpake-01@openssh.com";
2340 return authenticated;
2341}
2342
2343#endif /* JPAKE */
This page took 0.065369 seconds and 5 git commands to generate.