- stevesk@cvs.openbsd.org 2006/07/02 17:12:58
[openssh-git.git] / monitor.c
blob08919ddfc4eb0464b053000b504577aa277ca10f
1 /* $OpenBSD: monitor.c,v 1.77 2006/03/30 11:40:21 dtucker Exp $ */
2 /*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
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.
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.
28 #include "includes.h"
30 #include <sys/types.h>
31 #include <sys/wait.h>
33 #ifdef HAVE_PATHS_H
34 #include <paths.h>
35 #endif
36 #include <signal.h>
38 #ifdef SKEY
39 #include <skey.h>
40 #endif
42 #include <openssl/dh.h>
44 #include "ssh.h"
45 #include "auth.h"
46 #include "kex.h"
47 #include "dh.h"
48 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
49 #undef TARGET_OS_MAC
50 #include "zlib.h"
51 #define TARGET_OS_MAC 1
52 #else
53 #include "zlib.h"
54 #endif
55 #include "packet.h"
56 #include "auth-options.h"
57 #include "sshpty.h"
58 #include "channels.h"
59 #include "session.h"
60 #include "sshlogin.h"
61 #include "canohost.h"
62 #include "log.h"
63 #include "servconf.h"
64 #include "monitor.h"
65 #include "monitor_mm.h"
66 #include "monitor_wrap.h"
67 #include "monitor_fdpass.h"
68 #include "xmalloc.h"
69 #include "misc.h"
70 #include "buffer.h"
71 #include "bufaux.h"
72 #include "compat.h"
73 #include "ssh2.h"
75 #ifdef GSSAPI
76 #include "ssh-gss.h"
77 static Gssctxt *gsscontext = NULL;
78 #endif
80 /* Imports */
81 extern ServerOptions options;
82 extern u_int utmp_len;
83 extern Newkeys *current_keys[];
84 extern z_stream incoming_stream;
85 extern z_stream outgoing_stream;
86 extern u_char session_id[];
87 extern Buffer input, output;
88 extern Buffer auth_debug;
89 extern int auth_debug_init;
90 extern Buffer loginmsg;
92 /* State exported from the child */
94 struct {
95 z_stream incoming;
96 z_stream outgoing;
97 u_char *keyin;
98 u_int keyinlen;
99 u_char *keyout;
100 u_int keyoutlen;
101 u_char *ivin;
102 u_int ivinlen;
103 u_char *ivout;
104 u_int ivoutlen;
105 u_char *ssh1key;
106 u_int ssh1keylen;
107 int ssh1cipher;
108 int ssh1protoflags;
109 u_char *input;
110 u_int ilen;
111 u_char *output;
112 u_int olen;
113 } child_state;
115 /* Functions on the monitor that answer unprivileged requests */
117 int mm_answer_moduli(int, Buffer *);
118 int mm_answer_sign(int, Buffer *);
119 int mm_answer_pwnamallow(int, Buffer *);
120 int mm_answer_auth2_read_banner(int, Buffer *);
121 int mm_answer_authserv(int, Buffer *);
122 int mm_answer_authpassword(int, Buffer *);
123 int mm_answer_bsdauthquery(int, Buffer *);
124 int mm_answer_bsdauthrespond(int, Buffer *);
125 int mm_answer_skeyquery(int, Buffer *);
126 int mm_answer_skeyrespond(int, Buffer *);
127 int mm_answer_keyallowed(int, Buffer *);
128 int mm_answer_keyverify(int, Buffer *);
129 int mm_answer_pty(int, Buffer *);
130 int mm_answer_pty_cleanup(int, Buffer *);
131 int mm_answer_term(int, Buffer *);
132 int mm_answer_rsa_keyallowed(int, Buffer *);
133 int mm_answer_rsa_challenge(int, Buffer *);
134 int mm_answer_rsa_response(int, Buffer *);
135 int mm_answer_sesskey(int, Buffer *);
136 int mm_answer_sessid(int, Buffer *);
138 #ifdef USE_PAM
139 int mm_answer_pam_start(int, Buffer *);
140 int mm_answer_pam_account(int, Buffer *);
141 int mm_answer_pam_init_ctx(int, Buffer *);
142 int mm_answer_pam_query(int, Buffer *);
143 int mm_answer_pam_respond(int, Buffer *);
144 int mm_answer_pam_free_ctx(int, Buffer *);
145 #endif
147 #ifdef GSSAPI
148 int mm_answer_gss_setup_ctx(int, Buffer *);
149 int mm_answer_gss_accept_ctx(int, Buffer *);
150 int mm_answer_gss_userok(int, Buffer *);
151 int mm_answer_gss_checkmic(int, Buffer *);
152 #endif
154 #ifdef SSH_AUDIT_EVENTS
155 int mm_answer_audit_event(int, Buffer *);
156 int mm_answer_audit_command(int, Buffer *);
157 #endif
159 static Authctxt *authctxt;
160 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
162 /* local state for key verify */
163 static u_char *key_blob = NULL;
164 static u_int key_bloblen = 0;
165 static int key_blobtype = MM_NOKEY;
166 static char *hostbased_cuser = NULL;
167 static char *hostbased_chost = NULL;
168 static char *auth_method = "unknown";
169 static u_int session_id2_len = 0;
170 static u_char *session_id2 = NULL;
171 static pid_t monitor_child_pid;
173 struct mon_table {
174 enum monitor_reqtype type;
175 int flags;
176 int (*f)(int, Buffer *);
179 #define MON_ISAUTH 0x0004 /* Required for Authentication */
180 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
181 #define MON_ONCE 0x0010 /* Disable after calling */
182 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
184 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
186 #define MON_PERMIT 0x1000 /* Request is permitted */
188 struct mon_table mon_dispatch_proto20[] = {
189 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
190 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
191 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
192 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
193 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
194 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
195 #ifdef USE_PAM
196 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
197 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
198 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
199 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
200 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
201 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
202 #endif
203 #ifdef SSH_AUDIT_EVENTS
204 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
205 #endif
206 #ifdef BSD_AUTH
207 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
208 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
209 #endif
210 #ifdef SKEY
211 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
212 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
213 #endif
214 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
215 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
216 #ifdef GSSAPI
217 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
218 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
219 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
220 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
221 #endif
222 {0, 0, NULL}
225 struct mon_table mon_dispatch_postauth20[] = {
226 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
227 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
228 {MONITOR_REQ_PTY, 0, mm_answer_pty},
229 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
230 {MONITOR_REQ_TERM, 0, mm_answer_term},
231 #ifdef SSH_AUDIT_EVENTS
232 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
233 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
234 #endif
235 {0, 0, NULL}
238 struct mon_table mon_dispatch_proto15[] = {
239 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
240 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
241 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
242 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
243 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
244 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
245 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
246 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
247 #ifdef BSD_AUTH
248 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
249 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
250 #endif
251 #ifdef SKEY
252 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
253 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
254 #endif
255 #ifdef USE_PAM
256 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
257 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
258 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
259 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
260 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
261 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
262 #endif
263 #ifdef SSH_AUDIT_EVENTS
264 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
265 #endif
266 {0, 0, NULL}
269 struct mon_table mon_dispatch_postauth15[] = {
270 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
271 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
272 {MONITOR_REQ_TERM, 0, mm_answer_term},
273 #ifdef SSH_AUDIT_EVENTS
274 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
275 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
276 #endif
277 {0, 0, NULL}
280 struct mon_table *mon_dispatch;
282 /* Specifies if a certain message is allowed at the moment */
284 static void
285 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
287 while (ent->f != NULL) {
288 if (ent->type == type) {
289 ent->flags &= ~MON_PERMIT;
290 ent->flags |= permit ? MON_PERMIT : 0;
291 return;
293 ent++;
297 static void
298 monitor_permit_authentications(int permit)
300 struct mon_table *ent = mon_dispatch;
302 while (ent->f != NULL) {
303 if (ent->flags & MON_AUTH) {
304 ent->flags &= ~MON_PERMIT;
305 ent->flags |= permit ? MON_PERMIT : 0;
307 ent++;
311 void
312 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
314 struct mon_table *ent;
315 int authenticated = 0;
317 debug3("preauth child monitor started");
319 authctxt = _authctxt;
320 memset(authctxt, 0, sizeof(*authctxt));
322 authctxt->loginmsg = &loginmsg;
324 if (compat20) {
325 mon_dispatch = mon_dispatch_proto20;
327 /* Permit requests for moduli and signatures */
328 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
329 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
330 } else {
331 mon_dispatch = mon_dispatch_proto15;
333 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
336 /* The first few requests do not require asynchronous access */
337 while (!authenticated) {
338 auth_method = "unknown";
339 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
340 if (authenticated) {
341 if (!(ent->flags & MON_AUTHDECIDE))
342 fatal("%s: unexpected authentication from %d",
343 __func__, ent->type);
344 if (authctxt->pw->pw_uid == 0 &&
345 !auth_root_allowed(auth_method))
346 authenticated = 0;
347 #ifdef USE_PAM
348 /* PAM needs to perform account checks after auth */
349 if (options.use_pam && authenticated) {
350 Buffer m;
352 buffer_init(&m);
353 mm_request_receive_expect(pmonitor->m_sendfd,
354 MONITOR_REQ_PAM_ACCOUNT, &m);
355 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
356 buffer_free(&m);
358 #endif
361 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
362 auth_log(authctxt, authenticated, auth_method,
363 compat20 ? " ssh2" : "");
364 if (!authenticated)
365 authctxt->failures++;
369 if (!authctxt->valid)
370 fatal("%s: authenticated invalid user", __func__);
371 if (strcmp(auth_method, "unknown") == 0)
372 fatal("%s: authentication method name unknown", __func__);
374 debug("%s: %s has been authenticated by privileged process",
375 __func__, authctxt->user);
377 mm_get_keystate(pmonitor);
380 static void
381 monitor_set_child_handler(pid_t pid)
383 monitor_child_pid = pid;
386 static void
387 monitor_child_handler(int sig)
389 kill(monitor_child_pid, sig);
392 void
393 monitor_child_postauth(struct monitor *pmonitor)
395 monitor_set_child_handler(pmonitor->m_pid);
396 signal(SIGHUP, &monitor_child_handler);
397 signal(SIGTERM, &monitor_child_handler);
399 if (compat20) {
400 mon_dispatch = mon_dispatch_postauth20;
402 /* Permit requests for moduli and signatures */
403 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
404 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
405 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
406 } else {
407 mon_dispatch = mon_dispatch_postauth15;
408 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
410 if (!no_pty_flag) {
411 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
412 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
415 for (;;)
416 monitor_read(pmonitor, mon_dispatch, NULL);
419 void
420 monitor_sync(struct monitor *pmonitor)
422 if (options.compression) {
423 /* The member allocation is not visible, so sync it */
424 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
429 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
430 struct mon_table **pent)
432 Buffer m;
433 int ret;
434 u_char type;
436 buffer_init(&m);
438 mm_request_receive(pmonitor->m_sendfd, &m);
439 type = buffer_get_char(&m);
441 debug3("%s: checking request %d", __func__, type);
443 while (ent->f != NULL) {
444 if (ent->type == type)
445 break;
446 ent++;
449 if (ent->f != NULL) {
450 if (!(ent->flags & MON_PERMIT))
451 fatal("%s: unpermitted request %d", __func__,
452 type);
453 ret = (*ent->f)(pmonitor->m_sendfd, &m);
454 buffer_free(&m);
456 /* The child may use this request only once, disable it */
457 if (ent->flags & MON_ONCE) {
458 debug2("%s: %d used once, disabling now", __func__,
459 type);
460 ent->flags &= ~MON_PERMIT;
463 if (pent != NULL)
464 *pent = ent;
466 return ret;
469 fatal("%s: unsupported request: %d", __func__, type);
471 /* NOTREACHED */
472 return (-1);
475 /* allowed key state */
476 static int
477 monitor_allowed_key(u_char *blob, u_int bloblen)
479 /* make sure key is allowed */
480 if (key_blob == NULL || key_bloblen != bloblen ||
481 memcmp(key_blob, blob, key_bloblen))
482 return (0);
483 return (1);
486 static void
487 monitor_reset_key_state(void)
489 /* reset state */
490 if (key_blob != NULL)
491 xfree(key_blob);
492 if (hostbased_cuser != NULL)
493 xfree(hostbased_cuser);
494 if (hostbased_chost != NULL)
495 xfree(hostbased_chost);
496 key_blob = NULL;
497 key_bloblen = 0;
498 key_blobtype = MM_NOKEY;
499 hostbased_cuser = NULL;
500 hostbased_chost = NULL;
504 mm_answer_moduli(int sock, Buffer *m)
506 DH *dh;
507 int min, want, max;
509 min = buffer_get_int(m);
510 want = buffer_get_int(m);
511 max = buffer_get_int(m);
513 debug3("%s: got parameters: %d %d %d",
514 __func__, min, want, max);
515 /* We need to check here, too, in case the child got corrupted */
516 if (max < min || want < min || max < want)
517 fatal("%s: bad parameters: %d %d %d",
518 __func__, min, want, max);
520 buffer_clear(m);
522 dh = choose_dh(min, want, max);
523 if (dh == NULL) {
524 buffer_put_char(m, 0);
525 return (0);
526 } else {
527 /* Send first bignum */
528 buffer_put_char(m, 1);
529 buffer_put_bignum2(m, dh->p);
530 buffer_put_bignum2(m, dh->g);
532 DH_free(dh);
534 mm_request_send(sock, MONITOR_ANS_MODULI, m);
535 return (0);
539 mm_answer_sign(int sock, Buffer *m)
541 Key *key;
542 u_char *p;
543 u_char *signature;
544 u_int siglen, datlen;
545 int keyid;
547 debug3("%s", __func__);
549 keyid = buffer_get_int(m);
550 p = buffer_get_string(m, &datlen);
553 * Supported KEX types will only return SHA1 (20 byte) or
554 * SHA256 (32 byte) hashes
556 if (datlen != 20 && datlen != 32)
557 fatal("%s: data length incorrect: %u", __func__, datlen);
559 /* save session id, it will be passed on the first call */
560 if (session_id2_len == 0) {
561 session_id2_len = datlen;
562 session_id2 = xmalloc(session_id2_len);
563 memcpy(session_id2, p, session_id2_len);
566 if ((key = get_hostkey_by_index(keyid)) == NULL)
567 fatal("%s: no hostkey from index %d", __func__, keyid);
568 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
569 fatal("%s: key_sign failed", __func__);
571 debug3("%s: signature %p(%u)", __func__, signature, siglen);
573 buffer_clear(m);
574 buffer_put_string(m, signature, siglen);
576 xfree(p);
577 xfree(signature);
579 mm_request_send(sock, MONITOR_ANS_SIGN, m);
581 /* Turn on permissions for getpwnam */
582 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
584 return (0);
587 /* Retrieves the password entry and also checks if the user is permitted */
590 mm_answer_pwnamallow(int sock, Buffer *m)
592 char *username;
593 struct passwd *pwent;
594 int allowed = 0;
596 debug3("%s", __func__);
598 if (authctxt->attempt++ != 0)
599 fatal("%s: multiple attempts for getpwnam", __func__);
601 username = buffer_get_string(m, NULL);
603 pwent = getpwnamallow(username);
605 authctxt->user = xstrdup(username);
606 setproctitle("%s [priv]", pwent ? username : "unknown");
607 xfree(username);
609 buffer_clear(m);
611 if (pwent == NULL) {
612 buffer_put_char(m, 0);
613 authctxt->pw = fakepw();
614 goto out;
617 allowed = 1;
618 authctxt->pw = pwent;
619 authctxt->valid = 1;
621 buffer_put_char(m, 1);
622 buffer_put_string(m, pwent, sizeof(struct passwd));
623 buffer_put_cstring(m, pwent->pw_name);
624 buffer_put_cstring(m, "*");
625 buffer_put_cstring(m, pwent->pw_gecos);
626 #ifdef HAVE_PW_CLASS_IN_PASSWD
627 buffer_put_cstring(m, pwent->pw_class);
628 #endif
629 buffer_put_cstring(m, pwent->pw_dir);
630 buffer_put_cstring(m, pwent->pw_shell);
632 out:
633 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
634 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
636 /* For SSHv1 allow authentication now */
637 if (!compat20)
638 monitor_permit_authentications(1);
639 else {
640 /* Allow service/style information on the auth context */
641 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
642 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
645 #ifdef USE_PAM
646 if (options.use_pam)
647 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
648 #endif
649 #ifdef SSH_AUDIT_EVENTS
650 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
651 #endif
653 return (0);
656 int mm_answer_auth2_read_banner(int sock, Buffer *m)
658 char *banner;
660 buffer_clear(m);
661 banner = auth2_read_banner();
662 buffer_put_cstring(m, banner != NULL ? banner : "");
663 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
665 if (banner != NULL)
666 xfree(banner);
668 return (0);
672 mm_answer_authserv(int sock, Buffer *m)
674 monitor_permit_authentications(1);
676 authctxt->service = buffer_get_string(m, NULL);
677 authctxt->style = buffer_get_string(m, NULL);
678 debug3("%s: service=%s, style=%s",
679 __func__, authctxt->service, authctxt->style);
681 if (strlen(authctxt->style) == 0) {
682 xfree(authctxt->style);
683 authctxt->style = NULL;
686 return (0);
690 mm_answer_authpassword(int sock, Buffer *m)
692 static int call_count;
693 char *passwd;
694 int authenticated;
695 u_int plen;
697 passwd = buffer_get_string(m, &plen);
698 /* Only authenticate if the context is valid */
699 authenticated = options.password_authentication &&
700 auth_password(authctxt, passwd);
701 memset(passwd, 0, strlen(passwd));
702 xfree(passwd);
704 buffer_clear(m);
705 buffer_put_int(m, authenticated);
707 debug3("%s: sending result %d", __func__, authenticated);
708 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
710 call_count++;
711 if (plen == 0 && call_count == 1)
712 auth_method = "none";
713 else
714 auth_method = "password";
716 /* Causes monitor loop to terminate if authenticated */
717 return (authenticated);
720 #ifdef BSD_AUTH
722 mm_answer_bsdauthquery(int sock, Buffer *m)
724 char *name, *infotxt;
725 u_int numprompts;
726 u_int *echo_on;
727 char **prompts;
728 u_int success;
730 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
731 &prompts, &echo_on) < 0 ? 0 : 1;
733 buffer_clear(m);
734 buffer_put_int(m, success);
735 if (success)
736 buffer_put_cstring(m, prompts[0]);
738 debug3("%s: sending challenge success: %u", __func__, success);
739 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
741 if (success) {
742 xfree(name);
743 xfree(infotxt);
744 xfree(prompts);
745 xfree(echo_on);
748 return (0);
752 mm_answer_bsdauthrespond(int sock, Buffer *m)
754 char *response;
755 int authok;
757 if (authctxt->as == 0)
758 fatal("%s: no bsd auth session", __func__);
760 response = buffer_get_string(m, NULL);
761 authok = options.challenge_response_authentication &&
762 auth_userresponse(authctxt->as, response, 0);
763 authctxt->as = NULL;
764 debug3("%s: <%s> = <%d>", __func__, response, authok);
765 xfree(response);
767 buffer_clear(m);
768 buffer_put_int(m, authok);
770 debug3("%s: sending authenticated: %d", __func__, authok);
771 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
773 auth_method = "bsdauth";
775 return (authok != 0);
777 #endif
779 #ifdef SKEY
781 mm_answer_skeyquery(int sock, Buffer *m)
783 struct skey skey;
784 char challenge[1024];
785 u_int success;
787 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
788 sizeof(challenge)) < 0 ? 0 : 1;
790 buffer_clear(m);
791 buffer_put_int(m, success);
792 if (success)
793 buffer_put_cstring(m, challenge);
795 debug3("%s: sending challenge success: %u", __func__, success);
796 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
798 return (0);
802 mm_answer_skeyrespond(int sock, Buffer *m)
804 char *response;
805 int authok;
807 response = buffer_get_string(m, NULL);
809 authok = (options.challenge_response_authentication &&
810 authctxt->valid &&
811 skey_haskey(authctxt->pw->pw_name) == 0 &&
812 skey_passcheck(authctxt->pw->pw_name, response) != -1);
814 xfree(response);
816 buffer_clear(m);
817 buffer_put_int(m, authok);
819 debug3("%s: sending authenticated: %d", __func__, authok);
820 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
822 auth_method = "skey";
824 return (authok != 0);
826 #endif
828 #ifdef USE_PAM
830 mm_answer_pam_start(int sock, Buffer *m)
832 if (!options.use_pam)
833 fatal("UsePAM not set, but ended up in %s anyway", __func__);
835 start_pam(authctxt);
837 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
839 return (0);
843 mm_answer_pam_account(int sock, Buffer *m)
845 u_int ret;
847 if (!options.use_pam)
848 fatal("UsePAM not set, but ended up in %s anyway", __func__);
850 ret = do_pam_account();
852 buffer_put_int(m, ret);
853 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
855 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
857 return (ret);
860 static void *sshpam_ctxt, *sshpam_authok;
861 extern KbdintDevice sshpam_device;
864 mm_answer_pam_init_ctx(int sock, Buffer *m)
867 debug3("%s", __func__);
868 authctxt->user = buffer_get_string(m, NULL);
869 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
870 sshpam_authok = NULL;
871 buffer_clear(m);
872 if (sshpam_ctxt != NULL) {
873 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
874 buffer_put_int(m, 1);
875 } else {
876 buffer_put_int(m, 0);
878 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
879 return (0);
883 mm_answer_pam_query(int sock, Buffer *m)
885 char *name, *info, **prompts;
886 u_int i, num, *echo_on;
887 int ret;
889 debug3("%s", __func__);
890 sshpam_authok = NULL;
891 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
892 if (ret == 0 && num == 0)
893 sshpam_authok = sshpam_ctxt;
894 if (num > 1 || name == NULL || info == NULL)
895 ret = -1;
896 buffer_clear(m);
897 buffer_put_int(m, ret);
898 buffer_put_cstring(m, name);
899 xfree(name);
900 buffer_put_cstring(m, info);
901 xfree(info);
902 buffer_put_int(m, num);
903 for (i = 0; i < num; ++i) {
904 buffer_put_cstring(m, prompts[i]);
905 xfree(prompts[i]);
906 buffer_put_int(m, echo_on[i]);
908 if (prompts != NULL)
909 xfree(prompts);
910 if (echo_on != NULL)
911 xfree(echo_on);
912 auth_method = "keyboard-interactive/pam";
913 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
914 return (0);
918 mm_answer_pam_respond(int sock, Buffer *m)
920 char **resp;
921 u_int i, num;
922 int ret;
924 debug3("%s", __func__);
925 sshpam_authok = NULL;
926 num = buffer_get_int(m);
927 if (num > 0) {
928 resp = xcalloc(num, sizeof(char *));
929 for (i = 0; i < num; ++i)
930 resp[i] = buffer_get_string(m, NULL);
931 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
932 for (i = 0; i < num; ++i)
933 xfree(resp[i]);
934 xfree(resp);
935 } else {
936 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
938 buffer_clear(m);
939 buffer_put_int(m, ret);
940 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
941 auth_method = "keyboard-interactive/pam";
942 if (ret == 0)
943 sshpam_authok = sshpam_ctxt;
944 return (0);
948 mm_answer_pam_free_ctx(int sock, Buffer *m)
951 debug3("%s", __func__);
952 (sshpam_device.free_ctx)(sshpam_ctxt);
953 buffer_clear(m);
954 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
955 auth_method = "keyboard-interactive/pam";
956 return (sshpam_authok == sshpam_ctxt);
958 #endif
960 static void
961 mm_append_debug(Buffer *m)
963 if (auth_debug_init && buffer_len(&auth_debug)) {
964 debug3("%s: Appending debug messages for child", __func__);
965 buffer_append(m, buffer_ptr(&auth_debug),
966 buffer_len(&auth_debug));
967 buffer_clear(&auth_debug);
972 mm_answer_keyallowed(int sock, Buffer *m)
974 Key *key;
975 char *cuser, *chost;
976 u_char *blob;
977 u_int bloblen;
978 enum mm_keytype type = 0;
979 int allowed = 0;
981 debug3("%s entering", __func__);
983 type = buffer_get_int(m);
984 cuser = buffer_get_string(m, NULL);
985 chost = buffer_get_string(m, NULL);
986 blob = buffer_get_string(m, &bloblen);
988 key = key_from_blob(blob, bloblen);
990 if ((compat20 && type == MM_RSAHOSTKEY) ||
991 (!compat20 && type != MM_RSAHOSTKEY))
992 fatal("%s: key type and protocol mismatch", __func__);
994 debug3("%s: key_from_blob: %p", __func__, key);
996 if (key != NULL && authctxt->valid) {
997 switch (type) {
998 case MM_USERKEY:
999 allowed = options.pubkey_authentication &&
1000 user_key_allowed(authctxt->pw, key);
1001 auth_method = "publickey";
1002 break;
1003 case MM_HOSTKEY:
1004 allowed = options.hostbased_authentication &&
1005 hostbased_key_allowed(authctxt->pw,
1006 cuser, chost, key);
1007 auth_method = "hostbased";
1008 break;
1009 case MM_RSAHOSTKEY:
1010 key->type = KEY_RSA1; /* XXX */
1011 allowed = options.rhosts_rsa_authentication &&
1012 auth_rhosts_rsa_key_allowed(authctxt->pw,
1013 cuser, chost, key);
1014 auth_method = "rsa";
1015 break;
1016 default:
1017 fatal("%s: unknown key type %d", __func__, type);
1018 break;
1021 if (key != NULL)
1022 key_free(key);
1024 /* clear temporarily storage (used by verify) */
1025 monitor_reset_key_state();
1027 if (allowed) {
1028 /* Save temporarily for comparison in verify */
1029 key_blob = blob;
1030 key_bloblen = bloblen;
1031 key_blobtype = type;
1032 hostbased_cuser = cuser;
1033 hostbased_chost = chost;
1034 } else {
1035 /* Log failed attempt */
1036 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1037 xfree(blob);
1038 xfree(cuser);
1039 xfree(chost);
1042 debug3("%s: key %p is %s",
1043 __func__, key, allowed ? "allowed" : "disallowed");
1045 buffer_clear(m);
1046 buffer_put_int(m, allowed);
1047 buffer_put_int(m, forced_command != NULL);
1049 mm_append_debug(m);
1051 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1053 if (type == MM_RSAHOSTKEY)
1054 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1056 return (0);
1059 static int
1060 monitor_valid_userblob(u_char *data, u_int datalen)
1062 Buffer b;
1063 char *p;
1064 u_int len;
1065 int fail = 0;
1067 buffer_init(&b);
1068 buffer_append(&b, data, datalen);
1070 if (datafellows & SSH_OLD_SESSIONID) {
1071 p = buffer_ptr(&b);
1072 len = buffer_len(&b);
1073 if ((session_id2 == NULL) ||
1074 (len < session_id2_len) ||
1075 (memcmp(p, session_id2, session_id2_len) != 0))
1076 fail++;
1077 buffer_consume(&b, session_id2_len);
1078 } else {
1079 p = buffer_get_string(&b, &len);
1080 if ((session_id2 == NULL) ||
1081 (len != session_id2_len) ||
1082 (memcmp(p, session_id2, session_id2_len) != 0))
1083 fail++;
1084 xfree(p);
1086 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1087 fail++;
1088 p = buffer_get_string(&b, NULL);
1089 if (strcmp(authctxt->user, p) != 0) {
1090 logit("wrong user name passed to monitor: expected %s != %.100s",
1091 authctxt->user, p);
1092 fail++;
1094 xfree(p);
1095 buffer_skip_string(&b);
1096 if (datafellows & SSH_BUG_PKAUTH) {
1097 if (!buffer_get_char(&b))
1098 fail++;
1099 } else {
1100 p = buffer_get_string(&b, NULL);
1101 if (strcmp("publickey", p) != 0)
1102 fail++;
1103 xfree(p);
1104 if (!buffer_get_char(&b))
1105 fail++;
1106 buffer_skip_string(&b);
1108 buffer_skip_string(&b);
1109 if (buffer_len(&b) != 0)
1110 fail++;
1111 buffer_free(&b);
1112 return (fail == 0);
1115 static int
1116 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1117 char *chost)
1119 Buffer b;
1120 char *p;
1121 u_int len;
1122 int fail = 0;
1124 buffer_init(&b);
1125 buffer_append(&b, data, datalen);
1127 p = buffer_get_string(&b, &len);
1128 if ((session_id2 == NULL) ||
1129 (len != session_id2_len) ||
1130 (memcmp(p, session_id2, session_id2_len) != 0))
1131 fail++;
1132 xfree(p);
1134 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1135 fail++;
1136 p = buffer_get_string(&b, NULL);
1137 if (strcmp(authctxt->user, p) != 0) {
1138 logit("wrong user name passed to monitor: expected %s != %.100s",
1139 authctxt->user, p);
1140 fail++;
1142 xfree(p);
1143 buffer_skip_string(&b); /* service */
1144 p = buffer_get_string(&b, NULL);
1145 if (strcmp(p, "hostbased") != 0)
1146 fail++;
1147 xfree(p);
1148 buffer_skip_string(&b); /* pkalg */
1149 buffer_skip_string(&b); /* pkblob */
1151 /* verify client host, strip trailing dot if necessary */
1152 p = buffer_get_string(&b, NULL);
1153 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1154 p[len - 1] = '\0';
1155 if (strcmp(p, chost) != 0)
1156 fail++;
1157 xfree(p);
1159 /* verify client user */
1160 p = buffer_get_string(&b, NULL);
1161 if (strcmp(p, cuser) != 0)
1162 fail++;
1163 xfree(p);
1165 if (buffer_len(&b) != 0)
1166 fail++;
1167 buffer_free(&b);
1168 return (fail == 0);
1172 mm_answer_keyverify(int sock, Buffer *m)
1174 Key *key;
1175 u_char *signature, *data, *blob;
1176 u_int signaturelen, datalen, bloblen;
1177 int verified = 0;
1178 int valid_data = 0;
1180 blob = buffer_get_string(m, &bloblen);
1181 signature = buffer_get_string(m, &signaturelen);
1182 data = buffer_get_string(m, &datalen);
1184 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1185 !monitor_allowed_key(blob, bloblen))
1186 fatal("%s: bad key, not previously allowed", __func__);
1188 key = key_from_blob(blob, bloblen);
1189 if (key == NULL)
1190 fatal("%s: bad public key blob", __func__);
1192 switch (key_blobtype) {
1193 case MM_USERKEY:
1194 valid_data = monitor_valid_userblob(data, datalen);
1195 break;
1196 case MM_HOSTKEY:
1197 valid_data = monitor_valid_hostbasedblob(data, datalen,
1198 hostbased_cuser, hostbased_chost);
1199 break;
1200 default:
1201 valid_data = 0;
1202 break;
1204 if (!valid_data)
1205 fatal("%s: bad signature data blob", __func__);
1207 verified = key_verify(key, signature, signaturelen, data, datalen);
1208 debug3("%s: key %p signature %s",
1209 __func__, key, verified ? "verified" : "unverified");
1211 key_free(key);
1212 xfree(blob);
1213 xfree(signature);
1214 xfree(data);
1216 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1218 monitor_reset_key_state();
1220 buffer_clear(m);
1221 buffer_put_int(m, verified);
1222 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1224 return (verified);
1227 static void
1228 mm_record_login(Session *s, struct passwd *pw)
1230 socklen_t fromlen;
1231 struct sockaddr_storage from;
1234 * Get IP address of client. If the connection is not a socket, let
1235 * the address be 0.0.0.0.
1237 memset(&from, 0, sizeof(from));
1238 fromlen = sizeof(from);
1239 if (packet_connection_is_on_socket()) {
1240 if (getpeername(packet_get_connection_in(),
1241 (struct sockaddr *)&from, &fromlen) < 0) {
1242 debug("getpeername: %.100s", strerror(errno));
1243 cleanup_exit(255);
1246 /* Record that there was a login on that tty from the remote host. */
1247 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1248 get_remote_name_or_ip(utmp_len, options.use_dns),
1249 (struct sockaddr *)&from, fromlen);
1252 static void
1253 mm_session_close(Session *s)
1255 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1256 if (s->ttyfd != -1) {
1257 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1258 session_pty_cleanup2(s);
1260 s->used = 0;
1264 mm_answer_pty(int sock, Buffer *m)
1266 extern struct monitor *pmonitor;
1267 Session *s;
1268 int res, fd0;
1270 debug3("%s entering", __func__);
1272 buffer_clear(m);
1273 s = session_new();
1274 if (s == NULL)
1275 goto error;
1276 s->authctxt = authctxt;
1277 s->pw = authctxt->pw;
1278 s->pid = pmonitor->m_pid;
1279 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1280 if (res == 0)
1281 goto error;
1282 pty_setowner(authctxt->pw, s->tty);
1284 buffer_put_int(m, 1);
1285 buffer_put_cstring(m, s->tty);
1287 /* We need to trick ttyslot */
1288 if (dup2(s->ttyfd, 0) == -1)
1289 fatal("%s: dup2", __func__);
1291 mm_record_login(s, authctxt->pw);
1293 /* Now we can close the file descriptor again */
1294 close(0);
1296 /* send messages generated by record_login */
1297 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1298 buffer_clear(&loginmsg);
1300 mm_request_send(sock, MONITOR_ANS_PTY, m);
1302 mm_send_fd(sock, s->ptyfd);
1303 mm_send_fd(sock, s->ttyfd);
1305 /* make sure nothing uses fd 0 */
1306 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1307 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1308 if (fd0 != 0)
1309 error("%s: fd0 %d != 0", __func__, fd0);
1311 /* slave is not needed */
1312 close(s->ttyfd);
1313 s->ttyfd = s->ptyfd;
1314 /* no need to dup() because nobody closes ptyfd */
1315 s->ptymaster = s->ptyfd;
1317 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1319 return (0);
1321 error:
1322 if (s != NULL)
1323 mm_session_close(s);
1324 buffer_put_int(m, 0);
1325 mm_request_send(sock, MONITOR_ANS_PTY, m);
1326 return (0);
1330 mm_answer_pty_cleanup(int sock, Buffer *m)
1332 Session *s;
1333 char *tty;
1335 debug3("%s entering", __func__);
1337 tty = buffer_get_string(m, NULL);
1338 if ((s = session_by_tty(tty)) != NULL)
1339 mm_session_close(s);
1340 buffer_clear(m);
1341 xfree(tty);
1342 return (0);
1346 mm_answer_sesskey(int sock, Buffer *m)
1348 BIGNUM *p;
1349 int rsafail;
1351 /* Turn off permissions */
1352 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1354 if ((p = BN_new()) == NULL)
1355 fatal("%s: BN_new", __func__);
1357 buffer_get_bignum2(m, p);
1359 rsafail = ssh1_session_key(p);
1361 buffer_clear(m);
1362 buffer_put_int(m, rsafail);
1363 buffer_put_bignum2(m, p);
1365 BN_clear_free(p);
1367 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1369 /* Turn on permissions for sessid passing */
1370 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1372 return (0);
1376 mm_answer_sessid(int sock, Buffer *m)
1378 int i;
1380 debug3("%s entering", __func__);
1382 if (buffer_len(m) != 16)
1383 fatal("%s: bad ssh1 session id", __func__);
1384 for (i = 0; i < 16; i++)
1385 session_id[i] = buffer_get_char(m);
1387 /* Turn on permissions for getpwnam */
1388 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1390 return (0);
1394 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1396 BIGNUM *client_n;
1397 Key *key = NULL;
1398 u_char *blob = NULL;
1399 u_int blen = 0;
1400 int allowed = 0;
1402 debug3("%s entering", __func__);
1404 auth_method = "rsa";
1405 if (options.rsa_authentication && authctxt->valid) {
1406 if ((client_n = BN_new()) == NULL)
1407 fatal("%s: BN_new", __func__);
1408 buffer_get_bignum2(m, client_n);
1409 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1410 BN_clear_free(client_n);
1412 buffer_clear(m);
1413 buffer_put_int(m, allowed);
1414 buffer_put_int(m, forced_command != NULL);
1416 /* clear temporarily storage (used by generate challenge) */
1417 monitor_reset_key_state();
1419 if (allowed && key != NULL) {
1420 key->type = KEY_RSA; /* cheat for key_to_blob */
1421 if (key_to_blob(key, &blob, &blen) == 0)
1422 fatal("%s: key_to_blob failed", __func__);
1423 buffer_put_string(m, blob, blen);
1425 /* Save temporarily for comparison in verify */
1426 key_blob = blob;
1427 key_bloblen = blen;
1428 key_blobtype = MM_RSAUSERKEY;
1430 if (key != NULL)
1431 key_free(key);
1433 mm_append_debug(m);
1435 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1437 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1438 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1439 return (0);
1443 mm_answer_rsa_challenge(int sock, Buffer *m)
1445 Key *key = NULL;
1446 u_char *blob;
1447 u_int blen;
1449 debug3("%s entering", __func__);
1451 if (!authctxt->valid)
1452 fatal("%s: authctxt not valid", __func__);
1453 blob = buffer_get_string(m, &blen);
1454 if (!monitor_allowed_key(blob, blen))
1455 fatal("%s: bad key, not previously allowed", __func__);
1456 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1457 fatal("%s: key type mismatch", __func__);
1458 if ((key = key_from_blob(blob, blen)) == NULL)
1459 fatal("%s: received bad key", __func__);
1461 if (ssh1_challenge)
1462 BN_clear_free(ssh1_challenge);
1463 ssh1_challenge = auth_rsa_generate_challenge(key);
1465 buffer_clear(m);
1466 buffer_put_bignum2(m, ssh1_challenge);
1468 debug3("%s sending reply", __func__);
1469 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1471 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1473 xfree(blob);
1474 key_free(key);
1475 return (0);
1479 mm_answer_rsa_response(int sock, Buffer *m)
1481 Key *key = NULL;
1482 u_char *blob, *response;
1483 u_int blen, len;
1484 int success;
1486 debug3("%s entering", __func__);
1488 if (!authctxt->valid)
1489 fatal("%s: authctxt not valid", __func__);
1490 if (ssh1_challenge == NULL)
1491 fatal("%s: no ssh1_challenge", __func__);
1493 blob = buffer_get_string(m, &blen);
1494 if (!monitor_allowed_key(blob, blen))
1495 fatal("%s: bad key, not previously allowed", __func__);
1496 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1497 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1498 if ((key = key_from_blob(blob, blen)) == NULL)
1499 fatal("%s: received bad key", __func__);
1500 response = buffer_get_string(m, &len);
1501 if (len != 16)
1502 fatal("%s: received bad response to challenge", __func__);
1503 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1505 xfree(blob);
1506 key_free(key);
1507 xfree(response);
1509 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1511 /* reset state */
1512 BN_clear_free(ssh1_challenge);
1513 ssh1_challenge = NULL;
1514 monitor_reset_key_state();
1516 buffer_clear(m);
1517 buffer_put_int(m, success);
1518 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1520 return (success);
1524 mm_answer_term(int sock, Buffer *req)
1526 extern struct monitor *pmonitor;
1527 int res, status;
1529 debug3("%s: tearing down sessions", __func__);
1531 /* The child is terminating */
1532 session_destroy_all(&mm_session_close);
1534 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1535 if (errno != EINTR)
1536 exit(1);
1538 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1540 /* Terminate process */
1541 exit(res);
1544 #ifdef SSH_AUDIT_EVENTS
1545 /* Report that an audit event occurred */
1547 mm_answer_audit_event(int socket, Buffer *m)
1549 ssh_audit_event_t event;
1551 debug3("%s entering", __func__);
1553 event = buffer_get_int(m);
1554 switch(event) {
1555 case SSH_AUTH_FAIL_PUBKEY:
1556 case SSH_AUTH_FAIL_HOSTBASED:
1557 case SSH_AUTH_FAIL_GSSAPI:
1558 case SSH_LOGIN_EXCEED_MAXTRIES:
1559 case SSH_LOGIN_ROOT_DENIED:
1560 case SSH_CONNECTION_CLOSE:
1561 case SSH_INVALID_USER:
1562 audit_event(event);
1563 break;
1564 default:
1565 fatal("Audit event type %d not permitted", event);
1568 return (0);
1572 mm_answer_audit_command(int socket, Buffer *m)
1574 u_int len;
1575 char *cmd;
1577 debug3("%s entering", __func__);
1578 cmd = buffer_get_string(m, &len);
1579 /* sanity check command, if so how? */
1580 audit_run_command(cmd);
1581 xfree(cmd);
1582 return (0);
1584 #endif /* SSH_AUDIT_EVENTS */
1586 void
1587 monitor_apply_keystate(struct monitor *pmonitor)
1589 if (compat20) {
1590 set_newkeys(MODE_IN);
1591 set_newkeys(MODE_OUT);
1592 } else {
1593 packet_set_protocol_flags(child_state.ssh1protoflags);
1594 packet_set_encryption_key(child_state.ssh1key,
1595 child_state.ssh1keylen, child_state.ssh1cipher);
1596 xfree(child_state.ssh1key);
1599 /* for rc4 and other stateful ciphers */
1600 packet_set_keycontext(MODE_OUT, child_state.keyout);
1601 xfree(child_state.keyout);
1602 packet_set_keycontext(MODE_IN, child_state.keyin);
1603 xfree(child_state.keyin);
1605 if (!compat20) {
1606 packet_set_iv(MODE_OUT, child_state.ivout);
1607 xfree(child_state.ivout);
1608 packet_set_iv(MODE_IN, child_state.ivin);
1609 xfree(child_state.ivin);
1612 memcpy(&incoming_stream, &child_state.incoming,
1613 sizeof(incoming_stream));
1614 memcpy(&outgoing_stream, &child_state.outgoing,
1615 sizeof(outgoing_stream));
1617 /* Update with new address */
1618 if (options.compression)
1619 mm_init_compression(pmonitor->m_zlib);
1621 /* Network I/O buffers */
1622 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1623 buffer_clear(&input);
1624 buffer_append(&input, child_state.input, child_state.ilen);
1625 memset(child_state.input, 0, child_state.ilen);
1626 xfree(child_state.input);
1628 buffer_clear(&output);
1629 buffer_append(&output, child_state.output, child_state.olen);
1630 memset(child_state.output, 0, child_state.olen);
1631 xfree(child_state.output);
1634 static Kex *
1635 mm_get_kex(Buffer *m)
1637 Kex *kex;
1638 void *blob;
1639 u_int bloblen;
1641 kex = xcalloc(1, sizeof(*kex));
1642 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1643 if ((session_id2 == NULL) ||
1644 (kex->session_id_len != session_id2_len) ||
1645 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1646 fatal("mm_get_get: internal error: bad session id");
1647 kex->we_need = buffer_get_int(m);
1648 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1649 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1650 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1651 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1652 kex->server = 1;
1653 kex->hostkey_type = buffer_get_int(m);
1654 kex->kex_type = buffer_get_int(m);
1655 blob = buffer_get_string(m, &bloblen);
1656 buffer_init(&kex->my);
1657 buffer_append(&kex->my, blob, bloblen);
1658 xfree(blob);
1659 blob = buffer_get_string(m, &bloblen);
1660 buffer_init(&kex->peer);
1661 buffer_append(&kex->peer, blob, bloblen);
1662 xfree(blob);
1663 kex->done = 1;
1664 kex->flags = buffer_get_int(m);
1665 kex->client_version_string = buffer_get_string(m, NULL);
1666 kex->server_version_string = buffer_get_string(m, NULL);
1667 kex->load_host_key=&get_hostkey_by_type;
1668 kex->host_key_index=&get_hostkey_index;
1670 return (kex);
1673 /* This function requries careful sanity checking */
1675 void
1676 mm_get_keystate(struct monitor *pmonitor)
1678 Buffer m;
1679 u_char *blob, *p;
1680 u_int bloblen, plen;
1681 u_int32_t seqnr, packets;
1682 u_int64_t blocks;
1684 debug3("%s: Waiting for new keys", __func__);
1686 buffer_init(&m);
1687 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1688 if (!compat20) {
1689 child_state.ssh1protoflags = buffer_get_int(&m);
1690 child_state.ssh1cipher = buffer_get_int(&m);
1691 child_state.ssh1key = buffer_get_string(&m,
1692 &child_state.ssh1keylen);
1693 child_state.ivout = buffer_get_string(&m,
1694 &child_state.ivoutlen);
1695 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1696 goto skip;
1697 } else {
1698 /* Get the Kex for rekeying */
1699 *pmonitor->m_pkex = mm_get_kex(&m);
1702 blob = buffer_get_string(&m, &bloblen);
1703 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1704 xfree(blob);
1706 debug3("%s: Waiting for second key", __func__);
1707 blob = buffer_get_string(&m, &bloblen);
1708 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1709 xfree(blob);
1711 /* Now get sequence numbers for the packets */
1712 seqnr = buffer_get_int(&m);
1713 blocks = buffer_get_int64(&m);
1714 packets = buffer_get_int(&m);
1715 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1716 seqnr = buffer_get_int(&m);
1717 blocks = buffer_get_int64(&m);
1718 packets = buffer_get_int(&m);
1719 packet_set_state(MODE_IN, seqnr, blocks, packets);
1721 skip:
1722 /* Get the key context */
1723 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1724 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1726 debug3("%s: Getting compression state", __func__);
1727 /* Get compression state */
1728 p = buffer_get_string(&m, &plen);
1729 if (plen != sizeof(child_state.outgoing))
1730 fatal("%s: bad request size", __func__);
1731 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1732 xfree(p);
1734 p = buffer_get_string(&m, &plen);
1735 if (plen != sizeof(child_state.incoming))
1736 fatal("%s: bad request size", __func__);
1737 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1738 xfree(p);
1740 /* Network I/O buffers */
1741 debug3("%s: Getting Network I/O buffers", __func__);
1742 child_state.input = buffer_get_string(&m, &child_state.ilen);
1743 child_state.output = buffer_get_string(&m, &child_state.olen);
1745 buffer_free(&m);
1749 /* Allocation functions for zlib */
1750 void *
1751 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1753 size_t len = (size_t) size * ncount;
1754 void *address;
1756 if (len == 0 || ncount > SIZE_T_MAX / size)
1757 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1759 address = mm_malloc(mm, len);
1761 return (address);
1764 void
1765 mm_zfree(struct mm_master *mm, void *address)
1767 mm_free(mm, address);
1770 void
1771 mm_init_compression(struct mm_master *mm)
1773 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1774 outgoing_stream.zfree = (free_func)mm_zfree;
1775 outgoing_stream.opaque = mm;
1777 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1778 incoming_stream.zfree = (free_func)mm_zfree;
1779 incoming_stream.opaque = mm;
1782 /* XXX */
1784 #define FD_CLOSEONEXEC(x) do { \
1785 if (fcntl(x, F_SETFD, 1) == -1) \
1786 fatal("fcntl(%d, F_SETFD)", x); \
1787 } while (0)
1789 static void
1790 monitor_socketpair(int *pair)
1792 #ifdef HAVE_SOCKETPAIR
1793 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1794 fatal("%s: socketpair", __func__);
1795 #else
1796 fatal("%s: UsePrivilegeSeparation=yes not supported",
1797 __func__);
1798 #endif
1799 FD_CLOSEONEXEC(pair[0]);
1800 FD_CLOSEONEXEC(pair[1]);
1803 #define MM_MEMSIZE 65536
1805 struct monitor *
1806 monitor_init(void)
1808 struct monitor *mon;
1809 int pair[2];
1811 mon = xcalloc(1, sizeof(*mon));
1813 monitor_socketpair(pair);
1815 mon->m_recvfd = pair[0];
1816 mon->m_sendfd = pair[1];
1818 /* Used to share zlib space across processes */
1819 if (options.compression) {
1820 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1821 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1823 /* Compression needs to share state across borders */
1824 mm_init_compression(mon->m_zlib);
1827 return mon;
1830 void
1831 monitor_reinit(struct monitor *mon)
1833 int pair[2];
1835 monitor_socketpair(pair);
1837 mon->m_recvfd = pair[0];
1838 mon->m_sendfd = pair[1];
1841 #ifdef GSSAPI
1843 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1845 gss_OID_desc goid;
1846 OM_uint32 major;
1847 u_int len;
1849 goid.elements = buffer_get_string(m, &len);
1850 goid.length = len;
1852 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1854 xfree(goid.elements);
1856 buffer_clear(m);
1857 buffer_put_int(m, major);
1859 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1861 /* Now we have a context, enable the step */
1862 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1864 return (0);
1868 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1870 gss_buffer_desc in;
1871 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1872 OM_uint32 major, minor;
1873 OM_uint32 flags = 0; /* GSI needs this */
1874 u_int len;
1876 in.value = buffer_get_string(m, &len);
1877 in.length = len;
1878 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1879 xfree(in.value);
1881 buffer_clear(m);
1882 buffer_put_int(m, major);
1883 buffer_put_string(m, out.value, out.length);
1884 buffer_put_int(m, flags);
1885 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1887 gss_release_buffer(&minor, &out);
1889 if (major == GSS_S_COMPLETE) {
1890 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1891 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1892 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1894 return (0);
1898 mm_answer_gss_checkmic(int sock, Buffer *m)
1900 gss_buffer_desc gssbuf, mic;
1901 OM_uint32 ret;
1902 u_int len;
1904 gssbuf.value = buffer_get_string(m, &len);
1905 gssbuf.length = len;
1906 mic.value = buffer_get_string(m, &len);
1907 mic.length = len;
1909 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1911 xfree(gssbuf.value);
1912 xfree(mic.value);
1914 buffer_clear(m);
1915 buffer_put_int(m, ret);
1917 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1919 if (!GSS_ERROR(ret))
1920 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1922 return (0);
1926 mm_answer_gss_userok(int sock, Buffer *m)
1928 int authenticated;
1930 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1932 buffer_clear(m);
1933 buffer_put_int(m, authenticated);
1935 debug3("%s: sending result %d", __func__, authenticated);
1936 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1938 auth_method = "gssapi-with-mic";
1940 /* Monitor loop will terminate if authenticated */
1941 return (authenticated);
1943 #endif /* GSSAPI */