- jmc@cvs.openbsd.org 2006/07/18 07:50:40
[openssh-git.git] / monitor.c
bloba5263d7466db6447ead0d67046c3f955289b56dd
1 /* $OpenBSD: monitor.c,v 1.81 2006/07/11 20:07:25 stevesk 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/socket.h>
32 #include <sys/wait.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #ifdef HAVE_PATHS_H
37 #include <paths.h>
38 #endif
39 #include <pwd.h>
40 #include <signal.h>
42 #ifdef SKEY
43 #include <skey.h>
44 #endif
46 #include <openssl/dh.h>
48 #include "ssh.h"
49 #include "auth.h"
50 #include "kex.h"
51 #include "dh.h"
52 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
53 #undef TARGET_OS_MAC
54 #include "zlib.h"
55 #define TARGET_OS_MAC 1
56 #else
57 #include "zlib.h"
58 #endif
59 #include "packet.h"
60 #include "auth-options.h"
61 #include "sshpty.h"
62 #include "channels.h"
63 #include "session.h"
64 #include "sshlogin.h"
65 #include "canohost.h"
66 #include "log.h"
67 #include "servconf.h"
68 #include "monitor.h"
69 #include "monitor_mm.h"
70 #include "monitor_wrap.h"
71 #include "monitor_fdpass.h"
72 #include "xmalloc.h"
73 #include "misc.h"
74 #include "buffer.h"
75 #include "bufaux.h"
76 #include "compat.h"
77 #include "ssh2.h"
79 #ifdef GSSAPI
80 #include "ssh-gss.h"
81 static Gssctxt *gsscontext = NULL;
82 #endif
84 /* Imports */
85 extern ServerOptions options;
86 extern u_int utmp_len;
87 extern Newkeys *current_keys[];
88 extern z_stream incoming_stream;
89 extern z_stream outgoing_stream;
90 extern u_char session_id[];
91 extern Buffer input, output;
92 extern Buffer auth_debug;
93 extern int auth_debug_init;
94 extern Buffer loginmsg;
96 /* State exported from the child */
98 struct {
99 z_stream incoming;
100 z_stream outgoing;
101 u_char *keyin;
102 u_int keyinlen;
103 u_char *keyout;
104 u_int keyoutlen;
105 u_char *ivin;
106 u_int ivinlen;
107 u_char *ivout;
108 u_int ivoutlen;
109 u_char *ssh1key;
110 u_int ssh1keylen;
111 int ssh1cipher;
112 int ssh1protoflags;
113 u_char *input;
114 u_int ilen;
115 u_char *output;
116 u_int olen;
117 } child_state;
119 /* Functions on the monitor that answer unprivileged requests */
121 int mm_answer_moduli(int, Buffer *);
122 int mm_answer_sign(int, Buffer *);
123 int mm_answer_pwnamallow(int, Buffer *);
124 int mm_answer_auth2_read_banner(int, Buffer *);
125 int mm_answer_authserv(int, Buffer *);
126 int mm_answer_authpassword(int, Buffer *);
127 int mm_answer_bsdauthquery(int, Buffer *);
128 int mm_answer_bsdauthrespond(int, Buffer *);
129 int mm_answer_skeyquery(int, Buffer *);
130 int mm_answer_skeyrespond(int, Buffer *);
131 int mm_answer_keyallowed(int, Buffer *);
132 int mm_answer_keyverify(int, Buffer *);
133 int mm_answer_pty(int, Buffer *);
134 int mm_answer_pty_cleanup(int, Buffer *);
135 int mm_answer_term(int, Buffer *);
136 int mm_answer_rsa_keyallowed(int, Buffer *);
137 int mm_answer_rsa_challenge(int, Buffer *);
138 int mm_answer_rsa_response(int, Buffer *);
139 int mm_answer_sesskey(int, Buffer *);
140 int mm_answer_sessid(int, Buffer *);
142 #ifdef USE_PAM
143 int mm_answer_pam_start(int, Buffer *);
144 int mm_answer_pam_account(int, Buffer *);
145 int mm_answer_pam_init_ctx(int, Buffer *);
146 int mm_answer_pam_query(int, Buffer *);
147 int mm_answer_pam_respond(int, Buffer *);
148 int mm_answer_pam_free_ctx(int, Buffer *);
149 #endif
151 #ifdef GSSAPI
152 int mm_answer_gss_setup_ctx(int, Buffer *);
153 int mm_answer_gss_accept_ctx(int, Buffer *);
154 int mm_answer_gss_userok(int, Buffer *);
155 int mm_answer_gss_checkmic(int, Buffer *);
156 #endif
158 #ifdef SSH_AUDIT_EVENTS
159 int mm_answer_audit_event(int, Buffer *);
160 int mm_answer_audit_command(int, Buffer *);
161 #endif
163 static Authctxt *authctxt;
164 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
166 /* local state for key verify */
167 static u_char *key_blob = NULL;
168 static u_int key_bloblen = 0;
169 static int key_blobtype = MM_NOKEY;
170 static char *hostbased_cuser = NULL;
171 static char *hostbased_chost = NULL;
172 static char *auth_method = "unknown";
173 static u_int session_id2_len = 0;
174 static u_char *session_id2 = NULL;
175 static pid_t monitor_child_pid;
177 struct mon_table {
178 enum monitor_reqtype type;
179 int flags;
180 int (*f)(int, Buffer *);
183 #define MON_ISAUTH 0x0004 /* Required for Authentication */
184 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
185 #define MON_ONCE 0x0010 /* Disable after calling */
186 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
188 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
190 #define MON_PERMIT 0x1000 /* Request is permitted */
192 struct mon_table mon_dispatch_proto20[] = {
193 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
194 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
195 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
196 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
197 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
198 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
199 #ifdef USE_PAM
200 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
201 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
202 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
203 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
204 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
205 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
206 #endif
207 #ifdef SSH_AUDIT_EVENTS
208 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
209 #endif
210 #ifdef BSD_AUTH
211 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
212 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
213 #endif
214 #ifdef SKEY
215 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
216 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
217 #endif
218 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
219 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
220 #ifdef GSSAPI
221 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
222 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
223 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
224 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
225 #endif
226 {0, 0, NULL}
229 struct mon_table mon_dispatch_postauth20[] = {
230 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
231 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
232 {MONITOR_REQ_PTY, 0, mm_answer_pty},
233 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
234 {MONITOR_REQ_TERM, 0, mm_answer_term},
235 #ifdef SSH_AUDIT_EVENTS
236 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
237 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
238 #endif
239 {0, 0, NULL}
242 struct mon_table mon_dispatch_proto15[] = {
243 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
244 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
245 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
246 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
247 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
248 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
249 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
250 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
251 #ifdef BSD_AUTH
252 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
253 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
254 #endif
255 #ifdef SKEY
256 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
257 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
258 #endif
259 #ifdef USE_PAM
260 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
261 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
262 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
263 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
264 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
265 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
266 #endif
267 #ifdef SSH_AUDIT_EVENTS
268 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
269 #endif
270 {0, 0, NULL}
273 struct mon_table mon_dispatch_postauth15[] = {
274 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
275 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
276 {MONITOR_REQ_TERM, 0, mm_answer_term},
277 #ifdef SSH_AUDIT_EVENTS
278 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
279 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
280 #endif
281 {0, 0, NULL}
284 struct mon_table *mon_dispatch;
286 /* Specifies if a certain message is allowed at the moment */
288 static void
289 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
291 while (ent->f != NULL) {
292 if (ent->type == type) {
293 ent->flags &= ~MON_PERMIT;
294 ent->flags |= permit ? MON_PERMIT : 0;
295 return;
297 ent++;
301 static void
302 monitor_permit_authentications(int permit)
304 struct mon_table *ent = mon_dispatch;
306 while (ent->f != NULL) {
307 if (ent->flags & MON_AUTH) {
308 ent->flags &= ~MON_PERMIT;
309 ent->flags |= permit ? MON_PERMIT : 0;
311 ent++;
315 void
316 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
318 struct mon_table *ent;
319 int authenticated = 0;
321 debug3("preauth child monitor started");
323 authctxt = _authctxt;
324 memset(authctxt, 0, sizeof(*authctxt));
326 authctxt->loginmsg = &loginmsg;
328 if (compat20) {
329 mon_dispatch = mon_dispatch_proto20;
331 /* Permit requests for moduli and signatures */
332 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
333 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
334 } else {
335 mon_dispatch = mon_dispatch_proto15;
337 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
340 /* The first few requests do not require asynchronous access */
341 while (!authenticated) {
342 auth_method = "unknown";
343 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
344 if (authenticated) {
345 if (!(ent->flags & MON_AUTHDECIDE))
346 fatal("%s: unexpected authentication from %d",
347 __func__, ent->type);
348 if (authctxt->pw->pw_uid == 0 &&
349 !auth_root_allowed(auth_method))
350 authenticated = 0;
351 #ifdef USE_PAM
352 /* PAM needs to perform account checks after auth */
353 if (options.use_pam && authenticated) {
354 Buffer m;
356 buffer_init(&m);
357 mm_request_receive_expect(pmonitor->m_sendfd,
358 MONITOR_REQ_PAM_ACCOUNT, &m);
359 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
360 buffer_free(&m);
362 #endif
365 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
366 auth_log(authctxt, authenticated, auth_method,
367 compat20 ? " ssh2" : "");
368 if (!authenticated)
369 authctxt->failures++;
373 if (!authctxt->valid)
374 fatal("%s: authenticated invalid user", __func__);
375 if (strcmp(auth_method, "unknown") == 0)
376 fatal("%s: authentication method name unknown", __func__);
378 debug("%s: %s has been authenticated by privileged process",
379 __func__, authctxt->user);
381 mm_get_keystate(pmonitor);
384 static void
385 monitor_set_child_handler(pid_t pid)
387 monitor_child_pid = pid;
390 static void
391 monitor_child_handler(int sig)
393 kill(monitor_child_pid, sig);
396 void
397 monitor_child_postauth(struct monitor *pmonitor)
399 monitor_set_child_handler(pmonitor->m_pid);
400 signal(SIGHUP, &monitor_child_handler);
401 signal(SIGTERM, &monitor_child_handler);
403 if (compat20) {
404 mon_dispatch = mon_dispatch_postauth20;
406 /* Permit requests for moduli and signatures */
407 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
408 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
409 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
410 } else {
411 mon_dispatch = mon_dispatch_postauth15;
412 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
414 if (!no_pty_flag) {
415 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
416 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
419 for (;;)
420 monitor_read(pmonitor, mon_dispatch, NULL);
423 void
424 monitor_sync(struct monitor *pmonitor)
426 if (options.compression) {
427 /* The member allocation is not visible, so sync it */
428 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
433 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
434 struct mon_table **pent)
436 Buffer m;
437 int ret;
438 u_char type;
440 buffer_init(&m);
442 mm_request_receive(pmonitor->m_sendfd, &m);
443 type = buffer_get_char(&m);
445 debug3("%s: checking request %d", __func__, type);
447 while (ent->f != NULL) {
448 if (ent->type == type)
449 break;
450 ent++;
453 if (ent->f != NULL) {
454 if (!(ent->flags & MON_PERMIT))
455 fatal("%s: unpermitted request %d", __func__,
456 type);
457 ret = (*ent->f)(pmonitor->m_sendfd, &m);
458 buffer_free(&m);
460 /* The child may use this request only once, disable it */
461 if (ent->flags & MON_ONCE) {
462 debug2("%s: %d used once, disabling now", __func__,
463 type);
464 ent->flags &= ~MON_PERMIT;
467 if (pent != NULL)
468 *pent = ent;
470 return ret;
473 fatal("%s: unsupported request: %d", __func__, type);
475 /* NOTREACHED */
476 return (-1);
479 /* allowed key state */
480 static int
481 monitor_allowed_key(u_char *blob, u_int bloblen)
483 /* make sure key is allowed */
484 if (key_blob == NULL || key_bloblen != bloblen ||
485 memcmp(key_blob, blob, key_bloblen))
486 return (0);
487 return (1);
490 static void
491 monitor_reset_key_state(void)
493 /* reset state */
494 if (key_blob != NULL)
495 xfree(key_blob);
496 if (hostbased_cuser != NULL)
497 xfree(hostbased_cuser);
498 if (hostbased_chost != NULL)
499 xfree(hostbased_chost);
500 key_blob = NULL;
501 key_bloblen = 0;
502 key_blobtype = MM_NOKEY;
503 hostbased_cuser = NULL;
504 hostbased_chost = NULL;
508 mm_answer_moduli(int sock, Buffer *m)
510 DH *dh;
511 int min, want, max;
513 min = buffer_get_int(m);
514 want = buffer_get_int(m);
515 max = buffer_get_int(m);
517 debug3("%s: got parameters: %d %d %d",
518 __func__, min, want, max);
519 /* We need to check here, too, in case the child got corrupted */
520 if (max < min || want < min || max < want)
521 fatal("%s: bad parameters: %d %d %d",
522 __func__, min, want, max);
524 buffer_clear(m);
526 dh = choose_dh(min, want, max);
527 if (dh == NULL) {
528 buffer_put_char(m, 0);
529 return (0);
530 } else {
531 /* Send first bignum */
532 buffer_put_char(m, 1);
533 buffer_put_bignum2(m, dh->p);
534 buffer_put_bignum2(m, dh->g);
536 DH_free(dh);
538 mm_request_send(sock, MONITOR_ANS_MODULI, m);
539 return (0);
543 mm_answer_sign(int sock, Buffer *m)
545 Key *key;
546 u_char *p;
547 u_char *signature;
548 u_int siglen, datlen;
549 int keyid;
551 debug3("%s", __func__);
553 keyid = buffer_get_int(m);
554 p = buffer_get_string(m, &datlen);
557 * Supported KEX types will only return SHA1 (20 byte) or
558 * SHA256 (32 byte) hashes
560 if (datlen != 20 && datlen != 32)
561 fatal("%s: data length incorrect: %u", __func__, datlen);
563 /* save session id, it will be passed on the first call */
564 if (session_id2_len == 0) {
565 session_id2_len = datlen;
566 session_id2 = xmalloc(session_id2_len);
567 memcpy(session_id2, p, session_id2_len);
570 if ((key = get_hostkey_by_index(keyid)) == NULL)
571 fatal("%s: no hostkey from index %d", __func__, keyid);
572 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
573 fatal("%s: key_sign failed", __func__);
575 debug3("%s: signature %p(%u)", __func__, signature, siglen);
577 buffer_clear(m);
578 buffer_put_string(m, signature, siglen);
580 xfree(p);
581 xfree(signature);
583 mm_request_send(sock, MONITOR_ANS_SIGN, m);
585 /* Turn on permissions for getpwnam */
586 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
588 return (0);
591 /* Retrieves the password entry and also checks if the user is permitted */
594 mm_answer_pwnamallow(int sock, Buffer *m)
596 char *username;
597 struct passwd *pwent;
598 int allowed = 0;
600 debug3("%s", __func__);
602 if (authctxt->attempt++ != 0)
603 fatal("%s: multiple attempts for getpwnam", __func__);
605 username = buffer_get_string(m, NULL);
607 pwent = getpwnamallow(username);
609 authctxt->user = xstrdup(username);
610 setproctitle("%s [priv]", pwent ? username : "unknown");
611 xfree(username);
613 buffer_clear(m);
615 if (pwent == NULL) {
616 buffer_put_char(m, 0);
617 authctxt->pw = fakepw();
618 goto out;
621 allowed = 1;
622 authctxt->pw = pwent;
623 authctxt->valid = 1;
625 buffer_put_char(m, 1);
626 buffer_put_string(m, pwent, sizeof(struct passwd));
627 buffer_put_cstring(m, pwent->pw_name);
628 buffer_put_cstring(m, "*");
629 buffer_put_cstring(m, pwent->pw_gecos);
630 #ifdef HAVE_PW_CLASS_IN_PASSWD
631 buffer_put_cstring(m, pwent->pw_class);
632 #endif
633 buffer_put_cstring(m, pwent->pw_dir);
634 buffer_put_cstring(m, pwent->pw_shell);
636 out:
637 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
638 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
640 /* For SSHv1 allow authentication now */
641 if (!compat20)
642 monitor_permit_authentications(1);
643 else {
644 /* Allow service/style information on the auth context */
645 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
646 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
649 #ifdef USE_PAM
650 if (options.use_pam)
651 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
652 #endif
653 #ifdef SSH_AUDIT_EVENTS
654 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
655 #endif
657 return (0);
660 int mm_answer_auth2_read_banner(int sock, Buffer *m)
662 char *banner;
664 buffer_clear(m);
665 banner = auth2_read_banner();
666 buffer_put_cstring(m, banner != NULL ? banner : "");
667 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
669 if (banner != NULL)
670 xfree(banner);
672 return (0);
676 mm_answer_authserv(int sock, Buffer *m)
678 monitor_permit_authentications(1);
680 authctxt->service = buffer_get_string(m, NULL);
681 authctxt->style = buffer_get_string(m, NULL);
682 debug3("%s: service=%s, style=%s",
683 __func__, authctxt->service, authctxt->style);
685 if (strlen(authctxt->style) == 0) {
686 xfree(authctxt->style);
687 authctxt->style = NULL;
690 return (0);
694 mm_answer_authpassword(int sock, Buffer *m)
696 static int call_count;
697 char *passwd;
698 int authenticated;
699 u_int plen;
701 passwd = buffer_get_string(m, &plen);
702 /* Only authenticate if the context is valid */
703 authenticated = options.password_authentication &&
704 auth_password(authctxt, passwd);
705 memset(passwd, 0, strlen(passwd));
706 xfree(passwd);
708 buffer_clear(m);
709 buffer_put_int(m, authenticated);
711 debug3("%s: sending result %d", __func__, authenticated);
712 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
714 call_count++;
715 if (plen == 0 && call_count == 1)
716 auth_method = "none";
717 else
718 auth_method = "password";
720 /* Causes monitor loop to terminate if authenticated */
721 return (authenticated);
724 #ifdef BSD_AUTH
726 mm_answer_bsdauthquery(int sock, Buffer *m)
728 char *name, *infotxt;
729 u_int numprompts;
730 u_int *echo_on;
731 char **prompts;
732 u_int success;
734 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
735 &prompts, &echo_on) < 0 ? 0 : 1;
737 buffer_clear(m);
738 buffer_put_int(m, success);
739 if (success)
740 buffer_put_cstring(m, prompts[0]);
742 debug3("%s: sending challenge success: %u", __func__, success);
743 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
745 if (success) {
746 xfree(name);
747 xfree(infotxt);
748 xfree(prompts);
749 xfree(echo_on);
752 return (0);
756 mm_answer_bsdauthrespond(int sock, Buffer *m)
758 char *response;
759 int authok;
761 if (authctxt->as == 0)
762 fatal("%s: no bsd auth session", __func__);
764 response = buffer_get_string(m, NULL);
765 authok = options.challenge_response_authentication &&
766 auth_userresponse(authctxt->as, response, 0);
767 authctxt->as = NULL;
768 debug3("%s: <%s> = <%d>", __func__, response, authok);
769 xfree(response);
771 buffer_clear(m);
772 buffer_put_int(m, authok);
774 debug3("%s: sending authenticated: %d", __func__, authok);
775 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
777 auth_method = "bsdauth";
779 return (authok != 0);
781 #endif
783 #ifdef SKEY
785 mm_answer_skeyquery(int sock, Buffer *m)
787 struct skey skey;
788 char challenge[1024];
789 u_int success;
791 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
792 sizeof(challenge)) < 0 ? 0 : 1;
794 buffer_clear(m);
795 buffer_put_int(m, success);
796 if (success)
797 buffer_put_cstring(m, challenge);
799 debug3("%s: sending challenge success: %u", __func__, success);
800 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
802 return (0);
806 mm_answer_skeyrespond(int sock, Buffer *m)
808 char *response;
809 int authok;
811 response = buffer_get_string(m, NULL);
813 authok = (options.challenge_response_authentication &&
814 authctxt->valid &&
815 skey_haskey(authctxt->pw->pw_name) == 0 &&
816 skey_passcheck(authctxt->pw->pw_name, response) != -1);
818 xfree(response);
820 buffer_clear(m);
821 buffer_put_int(m, authok);
823 debug3("%s: sending authenticated: %d", __func__, authok);
824 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
826 auth_method = "skey";
828 return (authok != 0);
830 #endif
832 #ifdef USE_PAM
834 mm_answer_pam_start(int sock, Buffer *m)
836 if (!options.use_pam)
837 fatal("UsePAM not set, but ended up in %s anyway", __func__);
839 start_pam(authctxt);
841 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
843 return (0);
847 mm_answer_pam_account(int sock, Buffer *m)
849 u_int ret;
851 if (!options.use_pam)
852 fatal("UsePAM not set, but ended up in %s anyway", __func__);
854 ret = do_pam_account();
856 buffer_put_int(m, ret);
857 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
859 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
861 return (ret);
864 static void *sshpam_ctxt, *sshpam_authok;
865 extern KbdintDevice sshpam_device;
868 mm_answer_pam_init_ctx(int sock, Buffer *m)
871 debug3("%s", __func__);
872 authctxt->user = buffer_get_string(m, NULL);
873 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
874 sshpam_authok = NULL;
875 buffer_clear(m);
876 if (sshpam_ctxt != NULL) {
877 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
878 buffer_put_int(m, 1);
879 } else {
880 buffer_put_int(m, 0);
882 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
883 return (0);
887 mm_answer_pam_query(int sock, Buffer *m)
889 char *name, *info, **prompts;
890 u_int i, num, *echo_on;
891 int ret;
893 debug3("%s", __func__);
894 sshpam_authok = NULL;
895 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
896 if (ret == 0 && num == 0)
897 sshpam_authok = sshpam_ctxt;
898 if (num > 1 || name == NULL || info == NULL)
899 ret = -1;
900 buffer_clear(m);
901 buffer_put_int(m, ret);
902 buffer_put_cstring(m, name);
903 xfree(name);
904 buffer_put_cstring(m, info);
905 xfree(info);
906 buffer_put_int(m, num);
907 for (i = 0; i < num; ++i) {
908 buffer_put_cstring(m, prompts[i]);
909 xfree(prompts[i]);
910 buffer_put_int(m, echo_on[i]);
912 if (prompts != NULL)
913 xfree(prompts);
914 if (echo_on != NULL)
915 xfree(echo_on);
916 auth_method = "keyboard-interactive/pam";
917 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
918 return (0);
922 mm_answer_pam_respond(int sock, Buffer *m)
924 char **resp;
925 u_int i, num;
926 int ret;
928 debug3("%s", __func__);
929 sshpam_authok = NULL;
930 num = buffer_get_int(m);
931 if (num > 0) {
932 resp = xcalloc(num, sizeof(char *));
933 for (i = 0; i < num; ++i)
934 resp[i] = buffer_get_string(m, NULL);
935 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
936 for (i = 0; i < num; ++i)
937 xfree(resp[i]);
938 xfree(resp);
939 } else {
940 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
942 buffer_clear(m);
943 buffer_put_int(m, ret);
944 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
945 auth_method = "keyboard-interactive/pam";
946 if (ret == 0)
947 sshpam_authok = sshpam_ctxt;
948 return (0);
952 mm_answer_pam_free_ctx(int sock, Buffer *m)
955 debug3("%s", __func__);
956 (sshpam_device.free_ctx)(sshpam_ctxt);
957 buffer_clear(m);
958 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
959 auth_method = "keyboard-interactive/pam";
960 return (sshpam_authok == sshpam_ctxt);
962 #endif
964 static void
965 mm_append_debug(Buffer *m)
967 if (auth_debug_init && buffer_len(&auth_debug)) {
968 debug3("%s: Appending debug messages for child", __func__);
969 buffer_append(m, buffer_ptr(&auth_debug),
970 buffer_len(&auth_debug));
971 buffer_clear(&auth_debug);
976 mm_answer_keyallowed(int sock, Buffer *m)
978 Key *key;
979 char *cuser, *chost;
980 u_char *blob;
981 u_int bloblen;
982 enum mm_keytype type = 0;
983 int allowed = 0;
985 debug3("%s entering", __func__);
987 type = buffer_get_int(m);
988 cuser = buffer_get_string(m, NULL);
989 chost = buffer_get_string(m, NULL);
990 blob = buffer_get_string(m, &bloblen);
992 key = key_from_blob(blob, bloblen);
994 if ((compat20 && type == MM_RSAHOSTKEY) ||
995 (!compat20 && type != MM_RSAHOSTKEY))
996 fatal("%s: key type and protocol mismatch", __func__);
998 debug3("%s: key_from_blob: %p", __func__, key);
1000 if (key != NULL && authctxt->valid) {
1001 switch (type) {
1002 case MM_USERKEY:
1003 allowed = options.pubkey_authentication &&
1004 user_key_allowed(authctxt->pw, key);
1005 auth_method = "publickey";
1006 break;
1007 case MM_HOSTKEY:
1008 allowed = options.hostbased_authentication &&
1009 hostbased_key_allowed(authctxt->pw,
1010 cuser, chost, key);
1011 auth_method = "hostbased";
1012 break;
1013 case MM_RSAHOSTKEY:
1014 key->type = KEY_RSA1; /* XXX */
1015 allowed = options.rhosts_rsa_authentication &&
1016 auth_rhosts_rsa_key_allowed(authctxt->pw,
1017 cuser, chost, key);
1018 auth_method = "rsa";
1019 break;
1020 default:
1021 fatal("%s: unknown key type %d", __func__, type);
1022 break;
1025 if (key != NULL)
1026 key_free(key);
1028 /* clear temporarily storage (used by verify) */
1029 monitor_reset_key_state();
1031 if (allowed) {
1032 /* Save temporarily for comparison in verify */
1033 key_blob = blob;
1034 key_bloblen = bloblen;
1035 key_blobtype = type;
1036 hostbased_cuser = cuser;
1037 hostbased_chost = chost;
1038 } else {
1039 /* Log failed attempt */
1040 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1041 xfree(blob);
1042 xfree(cuser);
1043 xfree(chost);
1046 debug3("%s: key %p is %s",
1047 __func__, key, allowed ? "allowed" : "disallowed");
1049 buffer_clear(m);
1050 buffer_put_int(m, allowed);
1051 buffer_put_int(m, forced_command != NULL);
1053 mm_append_debug(m);
1055 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1057 if (type == MM_RSAHOSTKEY)
1058 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1060 return (0);
1063 static int
1064 monitor_valid_userblob(u_char *data, u_int datalen)
1066 Buffer b;
1067 char *p;
1068 u_int len;
1069 int fail = 0;
1071 buffer_init(&b);
1072 buffer_append(&b, data, datalen);
1074 if (datafellows & SSH_OLD_SESSIONID) {
1075 p = buffer_ptr(&b);
1076 len = buffer_len(&b);
1077 if ((session_id2 == NULL) ||
1078 (len < session_id2_len) ||
1079 (memcmp(p, session_id2, session_id2_len) != 0))
1080 fail++;
1081 buffer_consume(&b, session_id2_len);
1082 } else {
1083 p = buffer_get_string(&b, &len);
1084 if ((session_id2 == NULL) ||
1085 (len != session_id2_len) ||
1086 (memcmp(p, session_id2, session_id2_len) != 0))
1087 fail++;
1088 xfree(p);
1090 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1091 fail++;
1092 p = buffer_get_string(&b, NULL);
1093 if (strcmp(authctxt->user, p) != 0) {
1094 logit("wrong user name passed to monitor: expected %s != %.100s",
1095 authctxt->user, p);
1096 fail++;
1098 xfree(p);
1099 buffer_skip_string(&b);
1100 if (datafellows & SSH_BUG_PKAUTH) {
1101 if (!buffer_get_char(&b))
1102 fail++;
1103 } else {
1104 p = buffer_get_string(&b, NULL);
1105 if (strcmp("publickey", p) != 0)
1106 fail++;
1107 xfree(p);
1108 if (!buffer_get_char(&b))
1109 fail++;
1110 buffer_skip_string(&b);
1112 buffer_skip_string(&b);
1113 if (buffer_len(&b) != 0)
1114 fail++;
1115 buffer_free(&b);
1116 return (fail == 0);
1119 static int
1120 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1121 char *chost)
1123 Buffer b;
1124 char *p;
1125 u_int len;
1126 int fail = 0;
1128 buffer_init(&b);
1129 buffer_append(&b, data, datalen);
1131 p = buffer_get_string(&b, &len);
1132 if ((session_id2 == NULL) ||
1133 (len != session_id2_len) ||
1134 (memcmp(p, session_id2, session_id2_len) != 0))
1135 fail++;
1136 xfree(p);
1138 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1139 fail++;
1140 p = buffer_get_string(&b, NULL);
1141 if (strcmp(authctxt->user, p) != 0) {
1142 logit("wrong user name passed to monitor: expected %s != %.100s",
1143 authctxt->user, p);
1144 fail++;
1146 xfree(p);
1147 buffer_skip_string(&b); /* service */
1148 p = buffer_get_string(&b, NULL);
1149 if (strcmp(p, "hostbased") != 0)
1150 fail++;
1151 xfree(p);
1152 buffer_skip_string(&b); /* pkalg */
1153 buffer_skip_string(&b); /* pkblob */
1155 /* verify client host, strip trailing dot if necessary */
1156 p = buffer_get_string(&b, NULL);
1157 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1158 p[len - 1] = '\0';
1159 if (strcmp(p, chost) != 0)
1160 fail++;
1161 xfree(p);
1163 /* verify client user */
1164 p = buffer_get_string(&b, NULL);
1165 if (strcmp(p, cuser) != 0)
1166 fail++;
1167 xfree(p);
1169 if (buffer_len(&b) != 0)
1170 fail++;
1171 buffer_free(&b);
1172 return (fail == 0);
1176 mm_answer_keyverify(int sock, Buffer *m)
1178 Key *key;
1179 u_char *signature, *data, *blob;
1180 u_int signaturelen, datalen, bloblen;
1181 int verified = 0;
1182 int valid_data = 0;
1184 blob = buffer_get_string(m, &bloblen);
1185 signature = buffer_get_string(m, &signaturelen);
1186 data = buffer_get_string(m, &datalen);
1188 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1189 !monitor_allowed_key(blob, bloblen))
1190 fatal("%s: bad key, not previously allowed", __func__);
1192 key = key_from_blob(blob, bloblen);
1193 if (key == NULL)
1194 fatal("%s: bad public key blob", __func__);
1196 switch (key_blobtype) {
1197 case MM_USERKEY:
1198 valid_data = monitor_valid_userblob(data, datalen);
1199 break;
1200 case MM_HOSTKEY:
1201 valid_data = monitor_valid_hostbasedblob(data, datalen,
1202 hostbased_cuser, hostbased_chost);
1203 break;
1204 default:
1205 valid_data = 0;
1206 break;
1208 if (!valid_data)
1209 fatal("%s: bad signature data blob", __func__);
1211 verified = key_verify(key, signature, signaturelen, data, datalen);
1212 debug3("%s: key %p signature %s",
1213 __func__, key, verified ? "verified" : "unverified");
1215 key_free(key);
1216 xfree(blob);
1217 xfree(signature);
1218 xfree(data);
1220 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1222 monitor_reset_key_state();
1224 buffer_clear(m);
1225 buffer_put_int(m, verified);
1226 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1228 return (verified);
1231 static void
1232 mm_record_login(Session *s, struct passwd *pw)
1234 socklen_t fromlen;
1235 struct sockaddr_storage from;
1238 * Get IP address of client. If the connection is not a socket, let
1239 * the address be 0.0.0.0.
1241 memset(&from, 0, sizeof(from));
1242 fromlen = sizeof(from);
1243 if (packet_connection_is_on_socket()) {
1244 if (getpeername(packet_get_connection_in(),
1245 (struct sockaddr *)&from, &fromlen) < 0) {
1246 debug("getpeername: %.100s", strerror(errno));
1247 cleanup_exit(255);
1250 /* Record that there was a login on that tty from the remote host. */
1251 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1252 get_remote_name_or_ip(utmp_len, options.use_dns),
1253 (struct sockaddr *)&from, fromlen);
1256 static void
1257 mm_session_close(Session *s)
1259 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1260 if (s->ttyfd != -1) {
1261 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1262 session_pty_cleanup2(s);
1264 s->used = 0;
1268 mm_answer_pty(int sock, Buffer *m)
1270 extern struct monitor *pmonitor;
1271 Session *s;
1272 int res, fd0;
1274 debug3("%s entering", __func__);
1276 buffer_clear(m);
1277 s = session_new();
1278 if (s == NULL)
1279 goto error;
1280 s->authctxt = authctxt;
1281 s->pw = authctxt->pw;
1282 s->pid = pmonitor->m_pid;
1283 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1284 if (res == 0)
1285 goto error;
1286 pty_setowner(authctxt->pw, s->tty);
1288 buffer_put_int(m, 1);
1289 buffer_put_cstring(m, s->tty);
1291 /* We need to trick ttyslot */
1292 if (dup2(s->ttyfd, 0) == -1)
1293 fatal("%s: dup2", __func__);
1295 mm_record_login(s, authctxt->pw);
1297 /* Now we can close the file descriptor again */
1298 close(0);
1300 /* send messages generated by record_login */
1301 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1302 buffer_clear(&loginmsg);
1304 mm_request_send(sock, MONITOR_ANS_PTY, m);
1306 mm_send_fd(sock, s->ptyfd);
1307 mm_send_fd(sock, s->ttyfd);
1309 /* make sure nothing uses fd 0 */
1310 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1311 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1312 if (fd0 != 0)
1313 error("%s: fd0 %d != 0", __func__, fd0);
1315 /* slave is not needed */
1316 close(s->ttyfd);
1317 s->ttyfd = s->ptyfd;
1318 /* no need to dup() because nobody closes ptyfd */
1319 s->ptymaster = s->ptyfd;
1321 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1323 return (0);
1325 error:
1326 if (s != NULL)
1327 mm_session_close(s);
1328 buffer_put_int(m, 0);
1329 mm_request_send(sock, MONITOR_ANS_PTY, m);
1330 return (0);
1334 mm_answer_pty_cleanup(int sock, Buffer *m)
1336 Session *s;
1337 char *tty;
1339 debug3("%s entering", __func__);
1341 tty = buffer_get_string(m, NULL);
1342 if ((s = session_by_tty(tty)) != NULL)
1343 mm_session_close(s);
1344 buffer_clear(m);
1345 xfree(tty);
1346 return (0);
1350 mm_answer_sesskey(int sock, Buffer *m)
1352 BIGNUM *p;
1353 int rsafail;
1355 /* Turn off permissions */
1356 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1358 if ((p = BN_new()) == NULL)
1359 fatal("%s: BN_new", __func__);
1361 buffer_get_bignum2(m, p);
1363 rsafail = ssh1_session_key(p);
1365 buffer_clear(m);
1366 buffer_put_int(m, rsafail);
1367 buffer_put_bignum2(m, p);
1369 BN_clear_free(p);
1371 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1373 /* Turn on permissions for sessid passing */
1374 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1376 return (0);
1380 mm_answer_sessid(int sock, Buffer *m)
1382 int i;
1384 debug3("%s entering", __func__);
1386 if (buffer_len(m) != 16)
1387 fatal("%s: bad ssh1 session id", __func__);
1388 for (i = 0; i < 16; i++)
1389 session_id[i] = buffer_get_char(m);
1391 /* Turn on permissions for getpwnam */
1392 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1394 return (0);
1398 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1400 BIGNUM *client_n;
1401 Key *key = NULL;
1402 u_char *blob = NULL;
1403 u_int blen = 0;
1404 int allowed = 0;
1406 debug3("%s entering", __func__);
1408 auth_method = "rsa";
1409 if (options.rsa_authentication && authctxt->valid) {
1410 if ((client_n = BN_new()) == NULL)
1411 fatal("%s: BN_new", __func__);
1412 buffer_get_bignum2(m, client_n);
1413 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1414 BN_clear_free(client_n);
1416 buffer_clear(m);
1417 buffer_put_int(m, allowed);
1418 buffer_put_int(m, forced_command != NULL);
1420 /* clear temporarily storage (used by generate challenge) */
1421 monitor_reset_key_state();
1423 if (allowed && key != NULL) {
1424 key->type = KEY_RSA; /* cheat for key_to_blob */
1425 if (key_to_blob(key, &blob, &blen) == 0)
1426 fatal("%s: key_to_blob failed", __func__);
1427 buffer_put_string(m, blob, blen);
1429 /* Save temporarily for comparison in verify */
1430 key_blob = blob;
1431 key_bloblen = blen;
1432 key_blobtype = MM_RSAUSERKEY;
1434 if (key != NULL)
1435 key_free(key);
1437 mm_append_debug(m);
1439 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1441 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1442 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1443 return (0);
1447 mm_answer_rsa_challenge(int sock, Buffer *m)
1449 Key *key = NULL;
1450 u_char *blob;
1451 u_int blen;
1453 debug3("%s entering", __func__);
1455 if (!authctxt->valid)
1456 fatal("%s: authctxt not valid", __func__);
1457 blob = buffer_get_string(m, &blen);
1458 if (!monitor_allowed_key(blob, blen))
1459 fatal("%s: bad key, not previously allowed", __func__);
1460 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1461 fatal("%s: key type mismatch", __func__);
1462 if ((key = key_from_blob(blob, blen)) == NULL)
1463 fatal("%s: received bad key", __func__);
1465 if (ssh1_challenge)
1466 BN_clear_free(ssh1_challenge);
1467 ssh1_challenge = auth_rsa_generate_challenge(key);
1469 buffer_clear(m);
1470 buffer_put_bignum2(m, ssh1_challenge);
1472 debug3("%s sending reply", __func__);
1473 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1475 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1477 xfree(blob);
1478 key_free(key);
1479 return (0);
1483 mm_answer_rsa_response(int sock, Buffer *m)
1485 Key *key = NULL;
1486 u_char *blob, *response;
1487 u_int blen, len;
1488 int success;
1490 debug3("%s entering", __func__);
1492 if (!authctxt->valid)
1493 fatal("%s: authctxt not valid", __func__);
1494 if (ssh1_challenge == NULL)
1495 fatal("%s: no ssh1_challenge", __func__);
1497 blob = buffer_get_string(m, &blen);
1498 if (!monitor_allowed_key(blob, blen))
1499 fatal("%s: bad key, not previously allowed", __func__);
1500 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1501 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1502 if ((key = key_from_blob(blob, blen)) == NULL)
1503 fatal("%s: received bad key", __func__);
1504 response = buffer_get_string(m, &len);
1505 if (len != 16)
1506 fatal("%s: received bad response to challenge", __func__);
1507 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1509 xfree(blob);
1510 key_free(key);
1511 xfree(response);
1513 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1515 /* reset state */
1516 BN_clear_free(ssh1_challenge);
1517 ssh1_challenge = NULL;
1518 monitor_reset_key_state();
1520 buffer_clear(m);
1521 buffer_put_int(m, success);
1522 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1524 return (success);
1528 mm_answer_term(int sock, Buffer *req)
1530 extern struct monitor *pmonitor;
1531 int res, status;
1533 debug3("%s: tearing down sessions", __func__);
1535 /* The child is terminating */
1536 session_destroy_all(&mm_session_close);
1538 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1539 if (errno != EINTR)
1540 exit(1);
1542 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1544 /* Terminate process */
1545 exit(res);
1548 #ifdef SSH_AUDIT_EVENTS
1549 /* Report that an audit event occurred */
1551 mm_answer_audit_event(int socket, Buffer *m)
1553 ssh_audit_event_t event;
1555 debug3("%s entering", __func__);
1557 event = buffer_get_int(m);
1558 switch(event) {
1559 case SSH_AUTH_FAIL_PUBKEY:
1560 case SSH_AUTH_FAIL_HOSTBASED:
1561 case SSH_AUTH_FAIL_GSSAPI:
1562 case SSH_LOGIN_EXCEED_MAXTRIES:
1563 case SSH_LOGIN_ROOT_DENIED:
1564 case SSH_CONNECTION_CLOSE:
1565 case SSH_INVALID_USER:
1566 audit_event(event);
1567 break;
1568 default:
1569 fatal("Audit event type %d not permitted", event);
1572 return (0);
1576 mm_answer_audit_command(int socket, Buffer *m)
1578 u_int len;
1579 char *cmd;
1581 debug3("%s entering", __func__);
1582 cmd = buffer_get_string(m, &len);
1583 /* sanity check command, if so how? */
1584 audit_run_command(cmd);
1585 xfree(cmd);
1586 return (0);
1588 #endif /* SSH_AUDIT_EVENTS */
1590 void
1591 monitor_apply_keystate(struct monitor *pmonitor)
1593 if (compat20) {
1594 set_newkeys(MODE_IN);
1595 set_newkeys(MODE_OUT);
1596 } else {
1597 packet_set_protocol_flags(child_state.ssh1protoflags);
1598 packet_set_encryption_key(child_state.ssh1key,
1599 child_state.ssh1keylen, child_state.ssh1cipher);
1600 xfree(child_state.ssh1key);
1603 /* for rc4 and other stateful ciphers */
1604 packet_set_keycontext(MODE_OUT, child_state.keyout);
1605 xfree(child_state.keyout);
1606 packet_set_keycontext(MODE_IN, child_state.keyin);
1607 xfree(child_state.keyin);
1609 if (!compat20) {
1610 packet_set_iv(MODE_OUT, child_state.ivout);
1611 xfree(child_state.ivout);
1612 packet_set_iv(MODE_IN, child_state.ivin);
1613 xfree(child_state.ivin);
1616 memcpy(&incoming_stream, &child_state.incoming,
1617 sizeof(incoming_stream));
1618 memcpy(&outgoing_stream, &child_state.outgoing,
1619 sizeof(outgoing_stream));
1621 /* Update with new address */
1622 if (options.compression)
1623 mm_init_compression(pmonitor->m_zlib);
1625 /* Network I/O buffers */
1626 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1627 buffer_clear(&input);
1628 buffer_append(&input, child_state.input, child_state.ilen);
1629 memset(child_state.input, 0, child_state.ilen);
1630 xfree(child_state.input);
1632 buffer_clear(&output);
1633 buffer_append(&output, child_state.output, child_state.olen);
1634 memset(child_state.output, 0, child_state.olen);
1635 xfree(child_state.output);
1638 static Kex *
1639 mm_get_kex(Buffer *m)
1641 Kex *kex;
1642 void *blob;
1643 u_int bloblen;
1645 kex = xcalloc(1, sizeof(*kex));
1646 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1647 if ((session_id2 == NULL) ||
1648 (kex->session_id_len != session_id2_len) ||
1649 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1650 fatal("mm_get_get: internal error: bad session id");
1651 kex->we_need = buffer_get_int(m);
1652 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1653 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1654 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1655 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1656 kex->server = 1;
1657 kex->hostkey_type = buffer_get_int(m);
1658 kex->kex_type = buffer_get_int(m);
1659 blob = buffer_get_string(m, &bloblen);
1660 buffer_init(&kex->my);
1661 buffer_append(&kex->my, blob, bloblen);
1662 xfree(blob);
1663 blob = buffer_get_string(m, &bloblen);
1664 buffer_init(&kex->peer);
1665 buffer_append(&kex->peer, blob, bloblen);
1666 xfree(blob);
1667 kex->done = 1;
1668 kex->flags = buffer_get_int(m);
1669 kex->client_version_string = buffer_get_string(m, NULL);
1670 kex->server_version_string = buffer_get_string(m, NULL);
1671 kex->load_host_key=&get_hostkey_by_type;
1672 kex->host_key_index=&get_hostkey_index;
1674 return (kex);
1677 /* This function requries careful sanity checking */
1679 void
1680 mm_get_keystate(struct monitor *pmonitor)
1682 Buffer m;
1683 u_char *blob, *p;
1684 u_int bloblen, plen;
1685 u_int32_t seqnr, packets;
1686 u_int64_t blocks;
1688 debug3("%s: Waiting for new keys", __func__);
1690 buffer_init(&m);
1691 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1692 if (!compat20) {
1693 child_state.ssh1protoflags = buffer_get_int(&m);
1694 child_state.ssh1cipher = buffer_get_int(&m);
1695 child_state.ssh1key = buffer_get_string(&m,
1696 &child_state.ssh1keylen);
1697 child_state.ivout = buffer_get_string(&m,
1698 &child_state.ivoutlen);
1699 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1700 goto skip;
1701 } else {
1702 /* Get the Kex for rekeying */
1703 *pmonitor->m_pkex = mm_get_kex(&m);
1706 blob = buffer_get_string(&m, &bloblen);
1707 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1708 xfree(blob);
1710 debug3("%s: Waiting for second key", __func__);
1711 blob = buffer_get_string(&m, &bloblen);
1712 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1713 xfree(blob);
1715 /* Now get sequence numbers for the packets */
1716 seqnr = buffer_get_int(&m);
1717 blocks = buffer_get_int64(&m);
1718 packets = buffer_get_int(&m);
1719 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1720 seqnr = buffer_get_int(&m);
1721 blocks = buffer_get_int64(&m);
1722 packets = buffer_get_int(&m);
1723 packet_set_state(MODE_IN, seqnr, blocks, packets);
1725 skip:
1726 /* Get the key context */
1727 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1728 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1730 debug3("%s: Getting compression state", __func__);
1731 /* Get compression state */
1732 p = buffer_get_string(&m, &plen);
1733 if (plen != sizeof(child_state.outgoing))
1734 fatal("%s: bad request size", __func__);
1735 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1736 xfree(p);
1738 p = buffer_get_string(&m, &plen);
1739 if (plen != sizeof(child_state.incoming))
1740 fatal("%s: bad request size", __func__);
1741 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1742 xfree(p);
1744 /* Network I/O buffers */
1745 debug3("%s: Getting Network I/O buffers", __func__);
1746 child_state.input = buffer_get_string(&m, &child_state.ilen);
1747 child_state.output = buffer_get_string(&m, &child_state.olen);
1749 buffer_free(&m);
1753 /* Allocation functions for zlib */
1754 void *
1755 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1757 size_t len = (size_t) size * ncount;
1758 void *address;
1760 if (len == 0 || ncount > SIZE_T_MAX / size)
1761 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1763 address = mm_malloc(mm, len);
1765 return (address);
1768 void
1769 mm_zfree(struct mm_master *mm, void *address)
1771 mm_free(mm, address);
1774 void
1775 mm_init_compression(struct mm_master *mm)
1777 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1778 outgoing_stream.zfree = (free_func)mm_zfree;
1779 outgoing_stream.opaque = mm;
1781 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1782 incoming_stream.zfree = (free_func)mm_zfree;
1783 incoming_stream.opaque = mm;
1786 /* XXX */
1788 #define FD_CLOSEONEXEC(x) do { \
1789 if (fcntl(x, F_SETFD, 1) == -1) \
1790 fatal("fcntl(%d, F_SETFD)", x); \
1791 } while (0)
1793 static void
1794 monitor_socketpair(int *pair)
1796 #ifdef HAVE_SOCKETPAIR
1797 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1798 fatal("%s: socketpair", __func__);
1799 #else
1800 fatal("%s: UsePrivilegeSeparation=yes not supported",
1801 __func__);
1802 #endif
1803 FD_CLOSEONEXEC(pair[0]);
1804 FD_CLOSEONEXEC(pair[1]);
1807 #define MM_MEMSIZE 65536
1809 struct monitor *
1810 monitor_init(void)
1812 struct monitor *mon;
1813 int pair[2];
1815 mon = xcalloc(1, sizeof(*mon));
1817 monitor_socketpair(pair);
1819 mon->m_recvfd = pair[0];
1820 mon->m_sendfd = pair[1];
1822 /* Used to share zlib space across processes */
1823 if (options.compression) {
1824 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1825 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1827 /* Compression needs to share state across borders */
1828 mm_init_compression(mon->m_zlib);
1831 return mon;
1834 void
1835 monitor_reinit(struct monitor *mon)
1837 int pair[2];
1839 monitor_socketpair(pair);
1841 mon->m_recvfd = pair[0];
1842 mon->m_sendfd = pair[1];
1845 #ifdef GSSAPI
1847 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1849 gss_OID_desc goid;
1850 OM_uint32 major;
1851 u_int len;
1853 goid.elements = buffer_get_string(m, &len);
1854 goid.length = len;
1856 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1858 xfree(goid.elements);
1860 buffer_clear(m);
1861 buffer_put_int(m, major);
1863 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1865 /* Now we have a context, enable the step */
1866 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1868 return (0);
1872 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1874 gss_buffer_desc in;
1875 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1876 OM_uint32 major, minor;
1877 OM_uint32 flags = 0; /* GSI needs this */
1878 u_int len;
1880 in.value = buffer_get_string(m, &len);
1881 in.length = len;
1882 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1883 xfree(in.value);
1885 buffer_clear(m);
1886 buffer_put_int(m, major);
1887 buffer_put_string(m, out.value, out.length);
1888 buffer_put_int(m, flags);
1889 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1891 gss_release_buffer(&minor, &out);
1893 if (major == GSS_S_COMPLETE) {
1894 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1895 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1896 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1898 return (0);
1902 mm_answer_gss_checkmic(int sock, Buffer *m)
1904 gss_buffer_desc gssbuf, mic;
1905 OM_uint32 ret;
1906 u_int len;
1908 gssbuf.value = buffer_get_string(m, &len);
1909 gssbuf.length = len;
1910 mic.value = buffer_get_string(m, &len);
1911 mic.length = len;
1913 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1915 xfree(gssbuf.value);
1916 xfree(mic.value);
1918 buffer_clear(m);
1919 buffer_put_int(m, ret);
1921 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1923 if (!GSS_ERROR(ret))
1924 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1926 return (0);
1930 mm_answer_gss_userok(int sock, Buffer *m)
1932 int authenticated;
1934 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1936 buffer_clear(m);
1937 buffer_put_int(m, authenticated);
1939 debug3("%s: sending result %d", __func__, authenticated);
1940 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1942 auth_method = "gssapi-with-mic";
1944 /* Monitor loop will terminate if authenticated */
1945 return (authenticated);
1947 #endif /* GSSAPI */