- (djm) [openbsd-compat/glob.c]
[openssh-git.git] / monitor.c
blob3799d8862ecbb59ff901dff390f3193be6d68ae7
1 /* $OpenBSD: monitor.c,v 1.82 2006/07/22 20:48:23 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>
41 #include <string.h>
43 #ifdef SKEY
44 #include <skey.h>
45 #endif
47 #include <openssl/dh.h>
49 #include "ssh.h"
50 #include "auth.h"
51 #include "kex.h"
52 #include "dh.h"
53 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
54 #undef TARGET_OS_MAC
55 #include "zlib.h"
56 #define TARGET_OS_MAC 1
57 #else
58 #include "zlib.h"
59 #endif
60 #include "packet.h"
61 #include "auth-options.h"
62 #include "sshpty.h"
63 #include "channels.h"
64 #include "session.h"
65 #include "sshlogin.h"
66 #include "canohost.h"
67 #include "log.h"
68 #include "servconf.h"
69 #include "monitor.h"
70 #include "monitor_mm.h"
71 #include "monitor_wrap.h"
72 #include "monitor_fdpass.h"
73 #include "xmalloc.h"
74 #include "misc.h"
75 #include "buffer.h"
76 #include "bufaux.h"
77 #include "compat.h"
78 #include "ssh2.h"
80 #ifdef GSSAPI
81 #include "ssh-gss.h"
82 static Gssctxt *gsscontext = NULL;
83 #endif
85 /* Imports */
86 extern ServerOptions options;
87 extern u_int utmp_len;
88 extern Newkeys *current_keys[];
89 extern z_stream incoming_stream;
90 extern z_stream outgoing_stream;
91 extern u_char session_id[];
92 extern Buffer input, output;
93 extern Buffer auth_debug;
94 extern int auth_debug_init;
95 extern Buffer loginmsg;
97 /* State exported from the child */
99 struct {
100 z_stream incoming;
101 z_stream outgoing;
102 u_char *keyin;
103 u_int keyinlen;
104 u_char *keyout;
105 u_int keyoutlen;
106 u_char *ivin;
107 u_int ivinlen;
108 u_char *ivout;
109 u_int ivoutlen;
110 u_char *ssh1key;
111 u_int ssh1keylen;
112 int ssh1cipher;
113 int ssh1protoflags;
114 u_char *input;
115 u_int ilen;
116 u_char *output;
117 u_int olen;
118 } child_state;
120 /* Functions on the monitor that answer unprivileged requests */
122 int mm_answer_moduli(int, Buffer *);
123 int mm_answer_sign(int, Buffer *);
124 int mm_answer_pwnamallow(int, Buffer *);
125 int mm_answer_auth2_read_banner(int, Buffer *);
126 int mm_answer_authserv(int, Buffer *);
127 int mm_answer_authpassword(int, Buffer *);
128 int mm_answer_bsdauthquery(int, Buffer *);
129 int mm_answer_bsdauthrespond(int, Buffer *);
130 int mm_answer_skeyquery(int, Buffer *);
131 int mm_answer_skeyrespond(int, Buffer *);
132 int mm_answer_keyallowed(int, Buffer *);
133 int mm_answer_keyverify(int, Buffer *);
134 int mm_answer_pty(int, Buffer *);
135 int mm_answer_pty_cleanup(int, Buffer *);
136 int mm_answer_term(int, Buffer *);
137 int mm_answer_rsa_keyallowed(int, Buffer *);
138 int mm_answer_rsa_challenge(int, Buffer *);
139 int mm_answer_rsa_response(int, Buffer *);
140 int mm_answer_sesskey(int, Buffer *);
141 int mm_answer_sessid(int, Buffer *);
143 #ifdef USE_PAM
144 int mm_answer_pam_start(int, Buffer *);
145 int mm_answer_pam_account(int, Buffer *);
146 int mm_answer_pam_init_ctx(int, Buffer *);
147 int mm_answer_pam_query(int, Buffer *);
148 int mm_answer_pam_respond(int, Buffer *);
149 int mm_answer_pam_free_ctx(int, Buffer *);
150 #endif
152 #ifdef GSSAPI
153 int mm_answer_gss_setup_ctx(int, Buffer *);
154 int mm_answer_gss_accept_ctx(int, Buffer *);
155 int mm_answer_gss_userok(int, Buffer *);
156 int mm_answer_gss_checkmic(int, Buffer *);
157 #endif
159 #ifdef SSH_AUDIT_EVENTS
160 int mm_answer_audit_event(int, Buffer *);
161 int mm_answer_audit_command(int, Buffer *);
162 #endif
164 static Authctxt *authctxt;
165 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
167 /* local state for key verify */
168 static u_char *key_blob = NULL;
169 static u_int key_bloblen = 0;
170 static int key_blobtype = MM_NOKEY;
171 static char *hostbased_cuser = NULL;
172 static char *hostbased_chost = NULL;
173 static char *auth_method = "unknown";
174 static u_int session_id2_len = 0;
175 static u_char *session_id2 = NULL;
176 static pid_t monitor_child_pid;
178 struct mon_table {
179 enum monitor_reqtype type;
180 int flags;
181 int (*f)(int, Buffer *);
184 #define MON_ISAUTH 0x0004 /* Required for Authentication */
185 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
186 #define MON_ONCE 0x0010 /* Disable after calling */
187 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
189 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
191 #define MON_PERMIT 0x1000 /* Request is permitted */
193 struct mon_table mon_dispatch_proto20[] = {
194 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
195 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
196 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
197 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
198 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
199 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
200 #ifdef USE_PAM
201 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
202 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
203 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
204 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
205 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
206 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
207 #endif
208 #ifdef SSH_AUDIT_EVENTS
209 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
210 #endif
211 #ifdef BSD_AUTH
212 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
213 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
214 #endif
215 #ifdef SKEY
216 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
217 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
218 #endif
219 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
220 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
221 #ifdef GSSAPI
222 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
223 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
224 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
225 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
226 #endif
227 {0, 0, NULL}
230 struct mon_table mon_dispatch_postauth20[] = {
231 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
232 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
233 {MONITOR_REQ_PTY, 0, mm_answer_pty},
234 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
235 {MONITOR_REQ_TERM, 0, mm_answer_term},
236 #ifdef SSH_AUDIT_EVENTS
237 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
238 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
239 #endif
240 {0, 0, NULL}
243 struct mon_table mon_dispatch_proto15[] = {
244 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
245 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
246 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
247 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
248 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
249 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
250 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
251 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
252 #ifdef BSD_AUTH
253 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
254 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
255 #endif
256 #ifdef SKEY
257 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
258 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
259 #endif
260 #ifdef USE_PAM
261 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
262 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
263 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
264 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
265 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
266 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
267 #endif
268 #ifdef SSH_AUDIT_EVENTS
269 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
270 #endif
271 {0, 0, NULL}
274 struct mon_table mon_dispatch_postauth15[] = {
275 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
276 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
277 {MONITOR_REQ_TERM, 0, mm_answer_term},
278 #ifdef SSH_AUDIT_EVENTS
279 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
280 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
281 #endif
282 {0, 0, NULL}
285 struct mon_table *mon_dispatch;
287 /* Specifies if a certain message is allowed at the moment */
289 static void
290 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
292 while (ent->f != NULL) {
293 if (ent->type == type) {
294 ent->flags &= ~MON_PERMIT;
295 ent->flags |= permit ? MON_PERMIT : 0;
296 return;
298 ent++;
302 static void
303 monitor_permit_authentications(int permit)
305 struct mon_table *ent = mon_dispatch;
307 while (ent->f != NULL) {
308 if (ent->flags & MON_AUTH) {
309 ent->flags &= ~MON_PERMIT;
310 ent->flags |= permit ? MON_PERMIT : 0;
312 ent++;
316 void
317 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
319 struct mon_table *ent;
320 int authenticated = 0;
322 debug3("preauth child monitor started");
324 authctxt = _authctxt;
325 memset(authctxt, 0, sizeof(*authctxt));
327 authctxt->loginmsg = &loginmsg;
329 if (compat20) {
330 mon_dispatch = mon_dispatch_proto20;
332 /* Permit requests for moduli and signatures */
333 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
334 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
335 } else {
336 mon_dispatch = mon_dispatch_proto15;
338 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
341 /* The first few requests do not require asynchronous access */
342 while (!authenticated) {
343 auth_method = "unknown";
344 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
345 if (authenticated) {
346 if (!(ent->flags & MON_AUTHDECIDE))
347 fatal("%s: unexpected authentication from %d",
348 __func__, ent->type);
349 if (authctxt->pw->pw_uid == 0 &&
350 !auth_root_allowed(auth_method))
351 authenticated = 0;
352 #ifdef USE_PAM
353 /* PAM needs to perform account checks after auth */
354 if (options.use_pam && authenticated) {
355 Buffer m;
357 buffer_init(&m);
358 mm_request_receive_expect(pmonitor->m_sendfd,
359 MONITOR_REQ_PAM_ACCOUNT, &m);
360 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
361 buffer_free(&m);
363 #endif
366 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
367 auth_log(authctxt, authenticated, auth_method,
368 compat20 ? " ssh2" : "");
369 if (!authenticated)
370 authctxt->failures++;
374 if (!authctxt->valid)
375 fatal("%s: authenticated invalid user", __func__);
376 if (strcmp(auth_method, "unknown") == 0)
377 fatal("%s: authentication method name unknown", __func__);
379 debug("%s: %s has been authenticated by privileged process",
380 __func__, authctxt->user);
382 mm_get_keystate(pmonitor);
385 static void
386 monitor_set_child_handler(pid_t pid)
388 monitor_child_pid = pid;
391 static void
392 monitor_child_handler(int sig)
394 kill(monitor_child_pid, sig);
397 void
398 monitor_child_postauth(struct monitor *pmonitor)
400 monitor_set_child_handler(pmonitor->m_pid);
401 signal(SIGHUP, &monitor_child_handler);
402 signal(SIGTERM, &monitor_child_handler);
404 if (compat20) {
405 mon_dispatch = mon_dispatch_postauth20;
407 /* Permit requests for moduli and signatures */
408 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
409 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
410 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
411 } else {
412 mon_dispatch = mon_dispatch_postauth15;
413 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
415 if (!no_pty_flag) {
416 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
417 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
420 for (;;)
421 monitor_read(pmonitor, mon_dispatch, NULL);
424 void
425 monitor_sync(struct monitor *pmonitor)
427 if (options.compression) {
428 /* The member allocation is not visible, so sync it */
429 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
434 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
435 struct mon_table **pent)
437 Buffer m;
438 int ret;
439 u_char type;
441 buffer_init(&m);
443 mm_request_receive(pmonitor->m_sendfd, &m);
444 type = buffer_get_char(&m);
446 debug3("%s: checking request %d", __func__, type);
448 while (ent->f != NULL) {
449 if (ent->type == type)
450 break;
451 ent++;
454 if (ent->f != NULL) {
455 if (!(ent->flags & MON_PERMIT))
456 fatal("%s: unpermitted request %d", __func__,
457 type);
458 ret = (*ent->f)(pmonitor->m_sendfd, &m);
459 buffer_free(&m);
461 /* The child may use this request only once, disable it */
462 if (ent->flags & MON_ONCE) {
463 debug2("%s: %d used once, disabling now", __func__,
464 type);
465 ent->flags &= ~MON_PERMIT;
468 if (pent != NULL)
469 *pent = ent;
471 return ret;
474 fatal("%s: unsupported request: %d", __func__, type);
476 /* NOTREACHED */
477 return (-1);
480 /* allowed key state */
481 static int
482 monitor_allowed_key(u_char *blob, u_int bloblen)
484 /* make sure key is allowed */
485 if (key_blob == NULL || key_bloblen != bloblen ||
486 memcmp(key_blob, blob, key_bloblen))
487 return (0);
488 return (1);
491 static void
492 monitor_reset_key_state(void)
494 /* reset state */
495 if (key_blob != NULL)
496 xfree(key_blob);
497 if (hostbased_cuser != NULL)
498 xfree(hostbased_cuser);
499 if (hostbased_chost != NULL)
500 xfree(hostbased_chost);
501 key_blob = NULL;
502 key_bloblen = 0;
503 key_blobtype = MM_NOKEY;
504 hostbased_cuser = NULL;
505 hostbased_chost = NULL;
509 mm_answer_moduli(int sock, Buffer *m)
511 DH *dh;
512 int min, want, max;
514 min = buffer_get_int(m);
515 want = buffer_get_int(m);
516 max = buffer_get_int(m);
518 debug3("%s: got parameters: %d %d %d",
519 __func__, min, want, max);
520 /* We need to check here, too, in case the child got corrupted */
521 if (max < min || want < min || max < want)
522 fatal("%s: bad parameters: %d %d %d",
523 __func__, min, want, max);
525 buffer_clear(m);
527 dh = choose_dh(min, want, max);
528 if (dh == NULL) {
529 buffer_put_char(m, 0);
530 return (0);
531 } else {
532 /* Send first bignum */
533 buffer_put_char(m, 1);
534 buffer_put_bignum2(m, dh->p);
535 buffer_put_bignum2(m, dh->g);
537 DH_free(dh);
539 mm_request_send(sock, MONITOR_ANS_MODULI, m);
540 return (0);
544 mm_answer_sign(int sock, Buffer *m)
546 Key *key;
547 u_char *p;
548 u_char *signature;
549 u_int siglen, datlen;
550 int keyid;
552 debug3("%s", __func__);
554 keyid = buffer_get_int(m);
555 p = buffer_get_string(m, &datlen);
558 * Supported KEX types will only return SHA1 (20 byte) or
559 * SHA256 (32 byte) hashes
561 if (datlen != 20 && datlen != 32)
562 fatal("%s: data length incorrect: %u", __func__, datlen);
564 /* save session id, it will be passed on the first call */
565 if (session_id2_len == 0) {
566 session_id2_len = datlen;
567 session_id2 = xmalloc(session_id2_len);
568 memcpy(session_id2, p, session_id2_len);
571 if ((key = get_hostkey_by_index(keyid)) == NULL)
572 fatal("%s: no hostkey from index %d", __func__, keyid);
573 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
574 fatal("%s: key_sign failed", __func__);
576 debug3("%s: signature %p(%u)", __func__, signature, siglen);
578 buffer_clear(m);
579 buffer_put_string(m, signature, siglen);
581 xfree(p);
582 xfree(signature);
584 mm_request_send(sock, MONITOR_ANS_SIGN, m);
586 /* Turn on permissions for getpwnam */
587 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
589 return (0);
592 /* Retrieves the password entry and also checks if the user is permitted */
595 mm_answer_pwnamallow(int sock, Buffer *m)
597 char *username;
598 struct passwd *pwent;
599 int allowed = 0;
601 debug3("%s", __func__);
603 if (authctxt->attempt++ != 0)
604 fatal("%s: multiple attempts for getpwnam", __func__);
606 username = buffer_get_string(m, NULL);
608 pwent = getpwnamallow(username);
610 authctxt->user = xstrdup(username);
611 setproctitle("%s [priv]", pwent ? username : "unknown");
612 xfree(username);
614 buffer_clear(m);
616 if (pwent == NULL) {
617 buffer_put_char(m, 0);
618 authctxt->pw = fakepw();
619 goto out;
622 allowed = 1;
623 authctxt->pw = pwent;
624 authctxt->valid = 1;
626 buffer_put_char(m, 1);
627 buffer_put_string(m, pwent, sizeof(struct passwd));
628 buffer_put_cstring(m, pwent->pw_name);
629 buffer_put_cstring(m, "*");
630 buffer_put_cstring(m, pwent->pw_gecos);
631 #ifdef HAVE_PW_CLASS_IN_PASSWD
632 buffer_put_cstring(m, pwent->pw_class);
633 #endif
634 buffer_put_cstring(m, pwent->pw_dir);
635 buffer_put_cstring(m, pwent->pw_shell);
637 out:
638 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
639 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
641 /* For SSHv1 allow authentication now */
642 if (!compat20)
643 monitor_permit_authentications(1);
644 else {
645 /* Allow service/style information on the auth context */
646 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
647 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
650 #ifdef USE_PAM
651 if (options.use_pam)
652 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
653 #endif
654 #ifdef SSH_AUDIT_EVENTS
655 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
656 #endif
658 return (0);
661 int mm_answer_auth2_read_banner(int sock, Buffer *m)
663 char *banner;
665 buffer_clear(m);
666 banner = auth2_read_banner();
667 buffer_put_cstring(m, banner != NULL ? banner : "");
668 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
670 if (banner != NULL)
671 xfree(banner);
673 return (0);
677 mm_answer_authserv(int sock, Buffer *m)
679 monitor_permit_authentications(1);
681 authctxt->service = buffer_get_string(m, NULL);
682 authctxt->style = buffer_get_string(m, NULL);
683 debug3("%s: service=%s, style=%s",
684 __func__, authctxt->service, authctxt->style);
686 if (strlen(authctxt->style) == 0) {
687 xfree(authctxt->style);
688 authctxt->style = NULL;
691 return (0);
695 mm_answer_authpassword(int sock, Buffer *m)
697 static int call_count;
698 char *passwd;
699 int authenticated;
700 u_int plen;
702 passwd = buffer_get_string(m, &plen);
703 /* Only authenticate if the context is valid */
704 authenticated = options.password_authentication &&
705 auth_password(authctxt, passwd);
706 memset(passwd, 0, strlen(passwd));
707 xfree(passwd);
709 buffer_clear(m);
710 buffer_put_int(m, authenticated);
712 debug3("%s: sending result %d", __func__, authenticated);
713 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
715 call_count++;
716 if (plen == 0 && call_count == 1)
717 auth_method = "none";
718 else
719 auth_method = "password";
721 /* Causes monitor loop to terminate if authenticated */
722 return (authenticated);
725 #ifdef BSD_AUTH
727 mm_answer_bsdauthquery(int sock, Buffer *m)
729 char *name, *infotxt;
730 u_int numprompts;
731 u_int *echo_on;
732 char **prompts;
733 u_int success;
735 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
736 &prompts, &echo_on) < 0 ? 0 : 1;
738 buffer_clear(m);
739 buffer_put_int(m, success);
740 if (success)
741 buffer_put_cstring(m, prompts[0]);
743 debug3("%s: sending challenge success: %u", __func__, success);
744 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
746 if (success) {
747 xfree(name);
748 xfree(infotxt);
749 xfree(prompts);
750 xfree(echo_on);
753 return (0);
757 mm_answer_bsdauthrespond(int sock, Buffer *m)
759 char *response;
760 int authok;
762 if (authctxt->as == 0)
763 fatal("%s: no bsd auth session", __func__);
765 response = buffer_get_string(m, NULL);
766 authok = options.challenge_response_authentication &&
767 auth_userresponse(authctxt->as, response, 0);
768 authctxt->as = NULL;
769 debug3("%s: <%s> = <%d>", __func__, response, authok);
770 xfree(response);
772 buffer_clear(m);
773 buffer_put_int(m, authok);
775 debug3("%s: sending authenticated: %d", __func__, authok);
776 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
778 auth_method = "bsdauth";
780 return (authok != 0);
782 #endif
784 #ifdef SKEY
786 mm_answer_skeyquery(int sock, Buffer *m)
788 struct skey skey;
789 char challenge[1024];
790 u_int success;
792 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
793 sizeof(challenge)) < 0 ? 0 : 1;
795 buffer_clear(m);
796 buffer_put_int(m, success);
797 if (success)
798 buffer_put_cstring(m, challenge);
800 debug3("%s: sending challenge success: %u", __func__, success);
801 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
803 return (0);
807 mm_answer_skeyrespond(int sock, Buffer *m)
809 char *response;
810 int authok;
812 response = buffer_get_string(m, NULL);
814 authok = (options.challenge_response_authentication &&
815 authctxt->valid &&
816 skey_haskey(authctxt->pw->pw_name) == 0 &&
817 skey_passcheck(authctxt->pw->pw_name, response) != -1);
819 xfree(response);
821 buffer_clear(m);
822 buffer_put_int(m, authok);
824 debug3("%s: sending authenticated: %d", __func__, authok);
825 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
827 auth_method = "skey";
829 return (authok != 0);
831 #endif
833 #ifdef USE_PAM
835 mm_answer_pam_start(int sock, Buffer *m)
837 if (!options.use_pam)
838 fatal("UsePAM not set, but ended up in %s anyway", __func__);
840 start_pam(authctxt);
842 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
844 return (0);
848 mm_answer_pam_account(int sock, Buffer *m)
850 u_int ret;
852 if (!options.use_pam)
853 fatal("UsePAM not set, but ended up in %s anyway", __func__);
855 ret = do_pam_account();
857 buffer_put_int(m, ret);
858 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
860 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
862 return (ret);
865 static void *sshpam_ctxt, *sshpam_authok;
866 extern KbdintDevice sshpam_device;
869 mm_answer_pam_init_ctx(int sock, Buffer *m)
872 debug3("%s", __func__);
873 authctxt->user = buffer_get_string(m, NULL);
874 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
875 sshpam_authok = NULL;
876 buffer_clear(m);
877 if (sshpam_ctxt != NULL) {
878 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
879 buffer_put_int(m, 1);
880 } else {
881 buffer_put_int(m, 0);
883 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
884 return (0);
888 mm_answer_pam_query(int sock, Buffer *m)
890 char *name, *info, **prompts;
891 u_int i, num, *echo_on;
892 int ret;
894 debug3("%s", __func__);
895 sshpam_authok = NULL;
896 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
897 if (ret == 0 && num == 0)
898 sshpam_authok = sshpam_ctxt;
899 if (num > 1 || name == NULL || info == NULL)
900 ret = -1;
901 buffer_clear(m);
902 buffer_put_int(m, ret);
903 buffer_put_cstring(m, name);
904 xfree(name);
905 buffer_put_cstring(m, info);
906 xfree(info);
907 buffer_put_int(m, num);
908 for (i = 0; i < num; ++i) {
909 buffer_put_cstring(m, prompts[i]);
910 xfree(prompts[i]);
911 buffer_put_int(m, echo_on[i]);
913 if (prompts != NULL)
914 xfree(prompts);
915 if (echo_on != NULL)
916 xfree(echo_on);
917 auth_method = "keyboard-interactive/pam";
918 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
919 return (0);
923 mm_answer_pam_respond(int sock, Buffer *m)
925 char **resp;
926 u_int i, num;
927 int ret;
929 debug3("%s", __func__);
930 sshpam_authok = NULL;
931 num = buffer_get_int(m);
932 if (num > 0) {
933 resp = xcalloc(num, sizeof(char *));
934 for (i = 0; i < num; ++i)
935 resp[i] = buffer_get_string(m, NULL);
936 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
937 for (i = 0; i < num; ++i)
938 xfree(resp[i]);
939 xfree(resp);
940 } else {
941 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
943 buffer_clear(m);
944 buffer_put_int(m, ret);
945 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
946 auth_method = "keyboard-interactive/pam";
947 if (ret == 0)
948 sshpam_authok = sshpam_ctxt;
949 return (0);
953 mm_answer_pam_free_ctx(int sock, Buffer *m)
956 debug3("%s", __func__);
957 (sshpam_device.free_ctx)(sshpam_ctxt);
958 buffer_clear(m);
959 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
960 auth_method = "keyboard-interactive/pam";
961 return (sshpam_authok == sshpam_ctxt);
963 #endif
965 static void
966 mm_append_debug(Buffer *m)
968 if (auth_debug_init && buffer_len(&auth_debug)) {
969 debug3("%s: Appending debug messages for child", __func__);
970 buffer_append(m, buffer_ptr(&auth_debug),
971 buffer_len(&auth_debug));
972 buffer_clear(&auth_debug);
977 mm_answer_keyallowed(int sock, Buffer *m)
979 Key *key;
980 char *cuser, *chost;
981 u_char *blob;
982 u_int bloblen;
983 enum mm_keytype type = 0;
984 int allowed = 0;
986 debug3("%s entering", __func__);
988 type = buffer_get_int(m);
989 cuser = buffer_get_string(m, NULL);
990 chost = buffer_get_string(m, NULL);
991 blob = buffer_get_string(m, &bloblen);
993 key = key_from_blob(blob, bloblen);
995 if ((compat20 && type == MM_RSAHOSTKEY) ||
996 (!compat20 && type != MM_RSAHOSTKEY))
997 fatal("%s: key type and protocol mismatch", __func__);
999 debug3("%s: key_from_blob: %p", __func__, key);
1001 if (key != NULL && authctxt->valid) {
1002 switch (type) {
1003 case MM_USERKEY:
1004 allowed = options.pubkey_authentication &&
1005 user_key_allowed(authctxt->pw, key);
1006 auth_method = "publickey";
1007 break;
1008 case MM_HOSTKEY:
1009 allowed = options.hostbased_authentication &&
1010 hostbased_key_allowed(authctxt->pw,
1011 cuser, chost, key);
1012 auth_method = "hostbased";
1013 break;
1014 case MM_RSAHOSTKEY:
1015 key->type = KEY_RSA1; /* XXX */
1016 allowed = options.rhosts_rsa_authentication &&
1017 auth_rhosts_rsa_key_allowed(authctxt->pw,
1018 cuser, chost, key);
1019 auth_method = "rsa";
1020 break;
1021 default:
1022 fatal("%s: unknown key type %d", __func__, type);
1023 break;
1026 if (key != NULL)
1027 key_free(key);
1029 /* clear temporarily storage (used by verify) */
1030 monitor_reset_key_state();
1032 if (allowed) {
1033 /* Save temporarily for comparison in verify */
1034 key_blob = blob;
1035 key_bloblen = bloblen;
1036 key_blobtype = type;
1037 hostbased_cuser = cuser;
1038 hostbased_chost = chost;
1039 } else {
1040 /* Log failed attempt */
1041 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1042 xfree(blob);
1043 xfree(cuser);
1044 xfree(chost);
1047 debug3("%s: key %p is %s",
1048 __func__, key, allowed ? "allowed" : "disallowed");
1050 buffer_clear(m);
1051 buffer_put_int(m, allowed);
1052 buffer_put_int(m, forced_command != NULL);
1054 mm_append_debug(m);
1056 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1058 if (type == MM_RSAHOSTKEY)
1059 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1061 return (0);
1064 static int
1065 monitor_valid_userblob(u_char *data, u_int datalen)
1067 Buffer b;
1068 char *p;
1069 u_int len;
1070 int fail = 0;
1072 buffer_init(&b);
1073 buffer_append(&b, data, datalen);
1075 if (datafellows & SSH_OLD_SESSIONID) {
1076 p = buffer_ptr(&b);
1077 len = buffer_len(&b);
1078 if ((session_id2 == NULL) ||
1079 (len < session_id2_len) ||
1080 (memcmp(p, session_id2, session_id2_len) != 0))
1081 fail++;
1082 buffer_consume(&b, session_id2_len);
1083 } else {
1084 p = buffer_get_string(&b, &len);
1085 if ((session_id2 == NULL) ||
1086 (len != session_id2_len) ||
1087 (memcmp(p, session_id2, session_id2_len) != 0))
1088 fail++;
1089 xfree(p);
1091 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1092 fail++;
1093 p = buffer_get_string(&b, NULL);
1094 if (strcmp(authctxt->user, p) != 0) {
1095 logit("wrong user name passed to monitor: expected %s != %.100s",
1096 authctxt->user, p);
1097 fail++;
1099 xfree(p);
1100 buffer_skip_string(&b);
1101 if (datafellows & SSH_BUG_PKAUTH) {
1102 if (!buffer_get_char(&b))
1103 fail++;
1104 } else {
1105 p = buffer_get_string(&b, NULL);
1106 if (strcmp("publickey", p) != 0)
1107 fail++;
1108 xfree(p);
1109 if (!buffer_get_char(&b))
1110 fail++;
1111 buffer_skip_string(&b);
1113 buffer_skip_string(&b);
1114 if (buffer_len(&b) != 0)
1115 fail++;
1116 buffer_free(&b);
1117 return (fail == 0);
1120 static int
1121 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1122 char *chost)
1124 Buffer b;
1125 char *p;
1126 u_int len;
1127 int fail = 0;
1129 buffer_init(&b);
1130 buffer_append(&b, data, datalen);
1132 p = buffer_get_string(&b, &len);
1133 if ((session_id2 == NULL) ||
1134 (len != session_id2_len) ||
1135 (memcmp(p, session_id2, session_id2_len) != 0))
1136 fail++;
1137 xfree(p);
1139 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1140 fail++;
1141 p = buffer_get_string(&b, NULL);
1142 if (strcmp(authctxt->user, p) != 0) {
1143 logit("wrong user name passed to monitor: expected %s != %.100s",
1144 authctxt->user, p);
1145 fail++;
1147 xfree(p);
1148 buffer_skip_string(&b); /* service */
1149 p = buffer_get_string(&b, NULL);
1150 if (strcmp(p, "hostbased") != 0)
1151 fail++;
1152 xfree(p);
1153 buffer_skip_string(&b); /* pkalg */
1154 buffer_skip_string(&b); /* pkblob */
1156 /* verify client host, strip trailing dot if necessary */
1157 p = buffer_get_string(&b, NULL);
1158 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1159 p[len - 1] = '\0';
1160 if (strcmp(p, chost) != 0)
1161 fail++;
1162 xfree(p);
1164 /* verify client user */
1165 p = buffer_get_string(&b, NULL);
1166 if (strcmp(p, cuser) != 0)
1167 fail++;
1168 xfree(p);
1170 if (buffer_len(&b) != 0)
1171 fail++;
1172 buffer_free(&b);
1173 return (fail == 0);
1177 mm_answer_keyverify(int sock, Buffer *m)
1179 Key *key;
1180 u_char *signature, *data, *blob;
1181 u_int signaturelen, datalen, bloblen;
1182 int verified = 0;
1183 int valid_data = 0;
1185 blob = buffer_get_string(m, &bloblen);
1186 signature = buffer_get_string(m, &signaturelen);
1187 data = buffer_get_string(m, &datalen);
1189 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1190 !monitor_allowed_key(blob, bloblen))
1191 fatal("%s: bad key, not previously allowed", __func__);
1193 key = key_from_blob(blob, bloblen);
1194 if (key == NULL)
1195 fatal("%s: bad public key blob", __func__);
1197 switch (key_blobtype) {
1198 case MM_USERKEY:
1199 valid_data = monitor_valid_userblob(data, datalen);
1200 break;
1201 case MM_HOSTKEY:
1202 valid_data = monitor_valid_hostbasedblob(data, datalen,
1203 hostbased_cuser, hostbased_chost);
1204 break;
1205 default:
1206 valid_data = 0;
1207 break;
1209 if (!valid_data)
1210 fatal("%s: bad signature data blob", __func__);
1212 verified = key_verify(key, signature, signaturelen, data, datalen);
1213 debug3("%s: key %p signature %s",
1214 __func__, key, verified ? "verified" : "unverified");
1216 key_free(key);
1217 xfree(blob);
1218 xfree(signature);
1219 xfree(data);
1221 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1223 monitor_reset_key_state();
1225 buffer_clear(m);
1226 buffer_put_int(m, verified);
1227 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1229 return (verified);
1232 static void
1233 mm_record_login(Session *s, struct passwd *pw)
1235 socklen_t fromlen;
1236 struct sockaddr_storage from;
1239 * Get IP address of client. If the connection is not a socket, let
1240 * the address be 0.0.0.0.
1242 memset(&from, 0, sizeof(from));
1243 fromlen = sizeof(from);
1244 if (packet_connection_is_on_socket()) {
1245 if (getpeername(packet_get_connection_in(),
1246 (struct sockaddr *)&from, &fromlen) < 0) {
1247 debug("getpeername: %.100s", strerror(errno));
1248 cleanup_exit(255);
1251 /* Record that there was a login on that tty from the remote host. */
1252 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1253 get_remote_name_or_ip(utmp_len, options.use_dns),
1254 (struct sockaddr *)&from, fromlen);
1257 static void
1258 mm_session_close(Session *s)
1260 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1261 if (s->ttyfd != -1) {
1262 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1263 session_pty_cleanup2(s);
1265 s->used = 0;
1269 mm_answer_pty(int sock, Buffer *m)
1271 extern struct monitor *pmonitor;
1272 Session *s;
1273 int res, fd0;
1275 debug3("%s entering", __func__);
1277 buffer_clear(m);
1278 s = session_new();
1279 if (s == NULL)
1280 goto error;
1281 s->authctxt = authctxt;
1282 s->pw = authctxt->pw;
1283 s->pid = pmonitor->m_pid;
1284 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1285 if (res == 0)
1286 goto error;
1287 pty_setowner(authctxt->pw, s->tty);
1289 buffer_put_int(m, 1);
1290 buffer_put_cstring(m, s->tty);
1292 /* We need to trick ttyslot */
1293 if (dup2(s->ttyfd, 0) == -1)
1294 fatal("%s: dup2", __func__);
1296 mm_record_login(s, authctxt->pw);
1298 /* Now we can close the file descriptor again */
1299 close(0);
1301 /* send messages generated by record_login */
1302 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1303 buffer_clear(&loginmsg);
1305 mm_request_send(sock, MONITOR_ANS_PTY, m);
1307 mm_send_fd(sock, s->ptyfd);
1308 mm_send_fd(sock, s->ttyfd);
1310 /* make sure nothing uses fd 0 */
1311 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1312 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1313 if (fd0 != 0)
1314 error("%s: fd0 %d != 0", __func__, fd0);
1316 /* slave is not needed */
1317 close(s->ttyfd);
1318 s->ttyfd = s->ptyfd;
1319 /* no need to dup() because nobody closes ptyfd */
1320 s->ptymaster = s->ptyfd;
1322 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1324 return (0);
1326 error:
1327 if (s != NULL)
1328 mm_session_close(s);
1329 buffer_put_int(m, 0);
1330 mm_request_send(sock, MONITOR_ANS_PTY, m);
1331 return (0);
1335 mm_answer_pty_cleanup(int sock, Buffer *m)
1337 Session *s;
1338 char *tty;
1340 debug3("%s entering", __func__);
1342 tty = buffer_get_string(m, NULL);
1343 if ((s = session_by_tty(tty)) != NULL)
1344 mm_session_close(s);
1345 buffer_clear(m);
1346 xfree(tty);
1347 return (0);
1351 mm_answer_sesskey(int sock, Buffer *m)
1353 BIGNUM *p;
1354 int rsafail;
1356 /* Turn off permissions */
1357 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1359 if ((p = BN_new()) == NULL)
1360 fatal("%s: BN_new", __func__);
1362 buffer_get_bignum2(m, p);
1364 rsafail = ssh1_session_key(p);
1366 buffer_clear(m);
1367 buffer_put_int(m, rsafail);
1368 buffer_put_bignum2(m, p);
1370 BN_clear_free(p);
1372 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1374 /* Turn on permissions for sessid passing */
1375 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1377 return (0);
1381 mm_answer_sessid(int sock, Buffer *m)
1383 int i;
1385 debug3("%s entering", __func__);
1387 if (buffer_len(m) != 16)
1388 fatal("%s: bad ssh1 session id", __func__);
1389 for (i = 0; i < 16; i++)
1390 session_id[i] = buffer_get_char(m);
1392 /* Turn on permissions for getpwnam */
1393 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1395 return (0);
1399 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1401 BIGNUM *client_n;
1402 Key *key = NULL;
1403 u_char *blob = NULL;
1404 u_int blen = 0;
1405 int allowed = 0;
1407 debug3("%s entering", __func__);
1409 auth_method = "rsa";
1410 if (options.rsa_authentication && authctxt->valid) {
1411 if ((client_n = BN_new()) == NULL)
1412 fatal("%s: BN_new", __func__);
1413 buffer_get_bignum2(m, client_n);
1414 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1415 BN_clear_free(client_n);
1417 buffer_clear(m);
1418 buffer_put_int(m, allowed);
1419 buffer_put_int(m, forced_command != NULL);
1421 /* clear temporarily storage (used by generate challenge) */
1422 monitor_reset_key_state();
1424 if (allowed && key != NULL) {
1425 key->type = KEY_RSA; /* cheat for key_to_blob */
1426 if (key_to_blob(key, &blob, &blen) == 0)
1427 fatal("%s: key_to_blob failed", __func__);
1428 buffer_put_string(m, blob, blen);
1430 /* Save temporarily for comparison in verify */
1431 key_blob = blob;
1432 key_bloblen = blen;
1433 key_blobtype = MM_RSAUSERKEY;
1435 if (key != NULL)
1436 key_free(key);
1438 mm_append_debug(m);
1440 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1442 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1443 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1444 return (0);
1448 mm_answer_rsa_challenge(int sock, Buffer *m)
1450 Key *key = NULL;
1451 u_char *blob;
1452 u_int blen;
1454 debug3("%s entering", __func__);
1456 if (!authctxt->valid)
1457 fatal("%s: authctxt not valid", __func__);
1458 blob = buffer_get_string(m, &blen);
1459 if (!monitor_allowed_key(blob, blen))
1460 fatal("%s: bad key, not previously allowed", __func__);
1461 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1462 fatal("%s: key type mismatch", __func__);
1463 if ((key = key_from_blob(blob, blen)) == NULL)
1464 fatal("%s: received bad key", __func__);
1466 if (ssh1_challenge)
1467 BN_clear_free(ssh1_challenge);
1468 ssh1_challenge = auth_rsa_generate_challenge(key);
1470 buffer_clear(m);
1471 buffer_put_bignum2(m, ssh1_challenge);
1473 debug3("%s sending reply", __func__);
1474 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1476 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1478 xfree(blob);
1479 key_free(key);
1480 return (0);
1484 mm_answer_rsa_response(int sock, Buffer *m)
1486 Key *key = NULL;
1487 u_char *blob, *response;
1488 u_int blen, len;
1489 int success;
1491 debug3("%s entering", __func__);
1493 if (!authctxt->valid)
1494 fatal("%s: authctxt not valid", __func__);
1495 if (ssh1_challenge == NULL)
1496 fatal("%s: no ssh1_challenge", __func__);
1498 blob = buffer_get_string(m, &blen);
1499 if (!monitor_allowed_key(blob, blen))
1500 fatal("%s: bad key, not previously allowed", __func__);
1501 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1502 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1503 if ((key = key_from_blob(blob, blen)) == NULL)
1504 fatal("%s: received bad key", __func__);
1505 response = buffer_get_string(m, &len);
1506 if (len != 16)
1507 fatal("%s: received bad response to challenge", __func__);
1508 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1510 xfree(blob);
1511 key_free(key);
1512 xfree(response);
1514 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1516 /* reset state */
1517 BN_clear_free(ssh1_challenge);
1518 ssh1_challenge = NULL;
1519 monitor_reset_key_state();
1521 buffer_clear(m);
1522 buffer_put_int(m, success);
1523 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1525 return (success);
1529 mm_answer_term(int sock, Buffer *req)
1531 extern struct monitor *pmonitor;
1532 int res, status;
1534 debug3("%s: tearing down sessions", __func__);
1536 /* The child is terminating */
1537 session_destroy_all(&mm_session_close);
1539 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1540 if (errno != EINTR)
1541 exit(1);
1543 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1545 /* Terminate process */
1546 exit(res);
1549 #ifdef SSH_AUDIT_EVENTS
1550 /* Report that an audit event occurred */
1552 mm_answer_audit_event(int socket, Buffer *m)
1554 ssh_audit_event_t event;
1556 debug3("%s entering", __func__);
1558 event = buffer_get_int(m);
1559 switch(event) {
1560 case SSH_AUTH_FAIL_PUBKEY:
1561 case SSH_AUTH_FAIL_HOSTBASED:
1562 case SSH_AUTH_FAIL_GSSAPI:
1563 case SSH_LOGIN_EXCEED_MAXTRIES:
1564 case SSH_LOGIN_ROOT_DENIED:
1565 case SSH_CONNECTION_CLOSE:
1566 case SSH_INVALID_USER:
1567 audit_event(event);
1568 break;
1569 default:
1570 fatal("Audit event type %d not permitted", event);
1573 return (0);
1577 mm_answer_audit_command(int socket, Buffer *m)
1579 u_int len;
1580 char *cmd;
1582 debug3("%s entering", __func__);
1583 cmd = buffer_get_string(m, &len);
1584 /* sanity check command, if so how? */
1585 audit_run_command(cmd);
1586 xfree(cmd);
1587 return (0);
1589 #endif /* SSH_AUDIT_EVENTS */
1591 void
1592 monitor_apply_keystate(struct monitor *pmonitor)
1594 if (compat20) {
1595 set_newkeys(MODE_IN);
1596 set_newkeys(MODE_OUT);
1597 } else {
1598 packet_set_protocol_flags(child_state.ssh1protoflags);
1599 packet_set_encryption_key(child_state.ssh1key,
1600 child_state.ssh1keylen, child_state.ssh1cipher);
1601 xfree(child_state.ssh1key);
1604 /* for rc4 and other stateful ciphers */
1605 packet_set_keycontext(MODE_OUT, child_state.keyout);
1606 xfree(child_state.keyout);
1607 packet_set_keycontext(MODE_IN, child_state.keyin);
1608 xfree(child_state.keyin);
1610 if (!compat20) {
1611 packet_set_iv(MODE_OUT, child_state.ivout);
1612 xfree(child_state.ivout);
1613 packet_set_iv(MODE_IN, child_state.ivin);
1614 xfree(child_state.ivin);
1617 memcpy(&incoming_stream, &child_state.incoming,
1618 sizeof(incoming_stream));
1619 memcpy(&outgoing_stream, &child_state.outgoing,
1620 sizeof(outgoing_stream));
1622 /* Update with new address */
1623 if (options.compression)
1624 mm_init_compression(pmonitor->m_zlib);
1626 /* Network I/O buffers */
1627 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1628 buffer_clear(&input);
1629 buffer_append(&input, child_state.input, child_state.ilen);
1630 memset(child_state.input, 0, child_state.ilen);
1631 xfree(child_state.input);
1633 buffer_clear(&output);
1634 buffer_append(&output, child_state.output, child_state.olen);
1635 memset(child_state.output, 0, child_state.olen);
1636 xfree(child_state.output);
1639 static Kex *
1640 mm_get_kex(Buffer *m)
1642 Kex *kex;
1643 void *blob;
1644 u_int bloblen;
1646 kex = xcalloc(1, sizeof(*kex));
1647 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1648 if ((session_id2 == NULL) ||
1649 (kex->session_id_len != session_id2_len) ||
1650 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1651 fatal("mm_get_get: internal error: bad session id");
1652 kex->we_need = buffer_get_int(m);
1653 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1654 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1655 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1656 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1657 kex->server = 1;
1658 kex->hostkey_type = buffer_get_int(m);
1659 kex->kex_type = buffer_get_int(m);
1660 blob = buffer_get_string(m, &bloblen);
1661 buffer_init(&kex->my);
1662 buffer_append(&kex->my, blob, bloblen);
1663 xfree(blob);
1664 blob = buffer_get_string(m, &bloblen);
1665 buffer_init(&kex->peer);
1666 buffer_append(&kex->peer, blob, bloblen);
1667 xfree(blob);
1668 kex->done = 1;
1669 kex->flags = buffer_get_int(m);
1670 kex->client_version_string = buffer_get_string(m, NULL);
1671 kex->server_version_string = buffer_get_string(m, NULL);
1672 kex->load_host_key=&get_hostkey_by_type;
1673 kex->host_key_index=&get_hostkey_index;
1675 return (kex);
1678 /* This function requries careful sanity checking */
1680 void
1681 mm_get_keystate(struct monitor *pmonitor)
1683 Buffer m;
1684 u_char *blob, *p;
1685 u_int bloblen, plen;
1686 u_int32_t seqnr, packets;
1687 u_int64_t blocks;
1689 debug3("%s: Waiting for new keys", __func__);
1691 buffer_init(&m);
1692 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1693 if (!compat20) {
1694 child_state.ssh1protoflags = buffer_get_int(&m);
1695 child_state.ssh1cipher = buffer_get_int(&m);
1696 child_state.ssh1key = buffer_get_string(&m,
1697 &child_state.ssh1keylen);
1698 child_state.ivout = buffer_get_string(&m,
1699 &child_state.ivoutlen);
1700 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1701 goto skip;
1702 } else {
1703 /* Get the Kex for rekeying */
1704 *pmonitor->m_pkex = mm_get_kex(&m);
1707 blob = buffer_get_string(&m, &bloblen);
1708 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1709 xfree(blob);
1711 debug3("%s: Waiting for second key", __func__);
1712 blob = buffer_get_string(&m, &bloblen);
1713 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1714 xfree(blob);
1716 /* Now get sequence numbers for the packets */
1717 seqnr = buffer_get_int(&m);
1718 blocks = buffer_get_int64(&m);
1719 packets = buffer_get_int(&m);
1720 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1721 seqnr = buffer_get_int(&m);
1722 blocks = buffer_get_int64(&m);
1723 packets = buffer_get_int(&m);
1724 packet_set_state(MODE_IN, seqnr, blocks, packets);
1726 skip:
1727 /* Get the key context */
1728 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1729 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1731 debug3("%s: Getting compression state", __func__);
1732 /* Get compression state */
1733 p = buffer_get_string(&m, &plen);
1734 if (plen != sizeof(child_state.outgoing))
1735 fatal("%s: bad request size", __func__);
1736 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1737 xfree(p);
1739 p = buffer_get_string(&m, &plen);
1740 if (plen != sizeof(child_state.incoming))
1741 fatal("%s: bad request size", __func__);
1742 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1743 xfree(p);
1745 /* Network I/O buffers */
1746 debug3("%s: Getting Network I/O buffers", __func__);
1747 child_state.input = buffer_get_string(&m, &child_state.ilen);
1748 child_state.output = buffer_get_string(&m, &child_state.olen);
1750 buffer_free(&m);
1754 /* Allocation functions for zlib */
1755 void *
1756 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1758 size_t len = (size_t) size * ncount;
1759 void *address;
1761 if (len == 0 || ncount > SIZE_T_MAX / size)
1762 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1764 address = mm_malloc(mm, len);
1766 return (address);
1769 void
1770 mm_zfree(struct mm_master *mm, void *address)
1772 mm_free(mm, address);
1775 void
1776 mm_init_compression(struct mm_master *mm)
1778 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1779 outgoing_stream.zfree = (free_func)mm_zfree;
1780 outgoing_stream.opaque = mm;
1782 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1783 incoming_stream.zfree = (free_func)mm_zfree;
1784 incoming_stream.opaque = mm;
1787 /* XXX */
1789 #define FD_CLOSEONEXEC(x) do { \
1790 if (fcntl(x, F_SETFD, 1) == -1) \
1791 fatal("fcntl(%d, F_SETFD)", x); \
1792 } while (0)
1794 static void
1795 monitor_socketpair(int *pair)
1797 #ifdef HAVE_SOCKETPAIR
1798 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1799 fatal("%s: socketpair", __func__);
1800 #else
1801 fatal("%s: UsePrivilegeSeparation=yes not supported",
1802 __func__);
1803 #endif
1804 FD_CLOSEONEXEC(pair[0]);
1805 FD_CLOSEONEXEC(pair[1]);
1808 #define MM_MEMSIZE 65536
1810 struct monitor *
1811 monitor_init(void)
1813 struct monitor *mon;
1814 int pair[2];
1816 mon = xcalloc(1, sizeof(*mon));
1818 monitor_socketpair(pair);
1820 mon->m_recvfd = pair[0];
1821 mon->m_sendfd = pair[1];
1823 /* Used to share zlib space across processes */
1824 if (options.compression) {
1825 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1826 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1828 /* Compression needs to share state across borders */
1829 mm_init_compression(mon->m_zlib);
1832 return mon;
1835 void
1836 monitor_reinit(struct monitor *mon)
1838 int pair[2];
1840 monitor_socketpair(pair);
1842 mon->m_recvfd = pair[0];
1843 mon->m_sendfd = pair[1];
1846 #ifdef GSSAPI
1848 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1850 gss_OID_desc goid;
1851 OM_uint32 major;
1852 u_int len;
1854 goid.elements = buffer_get_string(m, &len);
1855 goid.length = len;
1857 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1859 xfree(goid.elements);
1861 buffer_clear(m);
1862 buffer_put_int(m, major);
1864 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1866 /* Now we have a context, enable the step */
1867 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1869 return (0);
1873 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1875 gss_buffer_desc in;
1876 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1877 OM_uint32 major, minor;
1878 OM_uint32 flags = 0; /* GSI needs this */
1879 u_int len;
1881 in.value = buffer_get_string(m, &len);
1882 in.length = len;
1883 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1884 xfree(in.value);
1886 buffer_clear(m);
1887 buffer_put_int(m, major);
1888 buffer_put_string(m, out.value, out.length);
1889 buffer_put_int(m, flags);
1890 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1892 gss_release_buffer(&minor, &out);
1894 if (major == GSS_S_COMPLETE) {
1895 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1896 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1897 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1899 return (0);
1903 mm_answer_gss_checkmic(int sock, Buffer *m)
1905 gss_buffer_desc gssbuf, mic;
1906 OM_uint32 ret;
1907 u_int len;
1909 gssbuf.value = buffer_get_string(m, &len);
1910 gssbuf.length = len;
1911 mic.value = buffer_get_string(m, &len);
1912 mic.length = len;
1914 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1916 xfree(gssbuf.value);
1917 xfree(mic.value);
1919 buffer_clear(m);
1920 buffer_put_int(m, ret);
1922 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1924 if (!GSS_ERROR(ret))
1925 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1927 return (0);
1931 mm_answer_gss_userok(int sock, Buffer *m)
1933 int authenticated;
1935 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1937 buffer_clear(m);
1938 buffer_put_int(m, authenticated);
1940 debug3("%s: sending result %d", __func__, authenticated);
1941 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1943 auth_method = "gssapi-with-mic";
1945 /* Monitor loop will terminate if authenticated */
1946 return (authenticated);
1948 #endif /* GSSAPI */