MFC:
[dragonfly.git] / crypto / openssh-5 / monitor.c
blob2c89f824c1f990ff5758cdb24bb24b5fc1d87fcd
1 /* $OpenBSD: monitor.c,v 1.94 2007/10/29 04:08:08 dtucker Exp $ */
2 /*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "includes.h"
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include "openbsd-compat/sys-tree.h"
34 #include <sys/wait.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #ifdef HAVE_PATHS_H
39 #include <paths.h>
40 #endif
41 #include <pwd.h>
42 #include <signal.h>
43 #include <stdarg.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
48 #ifdef SKEY
49 #ifdef OPIE
50 #include <opie.h>
51 #define skey opie
52 #define skeychallenge(k, u, c) opiechallenge((k), (u), (c))
53 #define skey_haskey(u) opie_haskey((u))
54 #define skey_passcheck(u, r) opie_passverify((u), (r))
55 #else
56 #include <skey.h>
57 #endif
58 #endif
60 #include <openssl/dh.h>
62 #include "xmalloc.h"
63 #include "ssh.h"
64 #include "key.h"
65 #include "buffer.h"
66 #include "hostfile.h"
67 #include "auth.h"
68 #include "cipher.h"
69 #include "kex.h"
70 #include "dh.h"
71 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
72 #undef TARGET_OS_MAC
73 #include "zlib.h"
74 #define TARGET_OS_MAC 1
75 #else
76 #include "zlib.h"
77 #endif
78 #include "packet.h"
79 #include "auth-options.h"
80 #include "sshpty.h"
81 #include "channels.h"
82 #include "session.h"
83 #include "sshlogin.h"
84 #include "canohost.h"
85 #include "log.h"
86 #include "servconf.h"
87 #include "monitor.h"
88 #include "monitor_mm.h"
89 #ifdef GSSAPI
90 #include "ssh-gss.h"
91 #endif
92 #include "monitor_wrap.h"
93 #include "monitor_fdpass.h"
94 #include "misc.h"
95 #include "compat.h"
96 #include "ssh2.h"
98 #ifdef GSSAPI
99 static Gssctxt *gsscontext = NULL;
100 #endif
102 /* Imports */
103 extern ServerOptions options;
104 extern u_int utmp_len;
105 extern Newkeys *current_keys[];
106 extern z_stream incoming_stream;
107 extern z_stream outgoing_stream;
108 extern u_char session_id[];
109 extern Buffer input, output;
110 extern Buffer auth_debug;
111 extern int auth_debug_init;
112 extern Buffer loginmsg;
114 /* State exported from the child */
116 struct {
117 z_stream incoming;
118 z_stream outgoing;
119 u_char *keyin;
120 u_int keyinlen;
121 u_char *keyout;
122 u_int keyoutlen;
123 u_char *ivin;
124 u_int ivinlen;
125 u_char *ivout;
126 u_int ivoutlen;
127 u_char *ssh1key;
128 u_int ssh1keylen;
129 int ssh1cipher;
130 int ssh1protoflags;
131 u_char *input;
132 u_int ilen;
133 u_char *output;
134 u_int olen;
135 } child_state;
137 /* Functions on the monitor that answer unprivileged requests */
139 int mm_answer_moduli(int, Buffer *);
140 int mm_answer_sign(int, Buffer *);
141 int mm_answer_pwnamallow(int, Buffer *);
142 int mm_answer_auth2_read_banner(int, Buffer *);
143 int mm_answer_authserv(int, Buffer *);
144 int mm_answer_authpassword(int, Buffer *);
145 int mm_answer_bsdauthquery(int, Buffer *);
146 int mm_answer_bsdauthrespond(int, Buffer *);
147 int mm_answer_skeyquery(int, Buffer *);
148 int mm_answer_skeyrespond(int, Buffer *);
149 int mm_answer_keyallowed(int, Buffer *);
150 int mm_answer_keyverify(int, Buffer *);
151 int mm_answer_pty(int, Buffer *);
152 int mm_answer_pty_cleanup(int, Buffer *);
153 int mm_answer_term(int, Buffer *);
154 int mm_answer_rsa_keyallowed(int, Buffer *);
155 int mm_answer_rsa_challenge(int, Buffer *);
156 int mm_answer_rsa_response(int, Buffer *);
157 int mm_answer_sesskey(int, Buffer *);
158 int mm_answer_sessid(int, Buffer *);
160 #ifdef USE_PAM
161 int mm_answer_pam_start(int, Buffer *);
162 int mm_answer_pam_account(int, Buffer *);
163 int mm_answer_pam_init_ctx(int, Buffer *);
164 int mm_answer_pam_query(int, Buffer *);
165 int mm_answer_pam_respond(int, Buffer *);
166 int mm_answer_pam_free_ctx(int, Buffer *);
167 #endif
169 #ifdef GSSAPI
170 int mm_answer_gss_setup_ctx(int, Buffer *);
171 int mm_answer_gss_accept_ctx(int, Buffer *);
172 int mm_answer_gss_userok(int, Buffer *);
173 int mm_answer_gss_checkmic(int, Buffer *);
174 #endif
176 #ifdef SSH_AUDIT_EVENTS
177 int mm_answer_audit_event(int, Buffer *);
178 int mm_answer_audit_command(int, Buffer *);
179 #endif
181 static Authctxt *authctxt;
182 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
184 /* local state for key verify */
185 static u_char *key_blob = NULL;
186 static u_int key_bloblen = 0;
187 static int key_blobtype = MM_NOKEY;
188 static char *hostbased_cuser = NULL;
189 static char *hostbased_chost = NULL;
190 static char *auth_method = "unknown";
191 static u_int session_id2_len = 0;
192 static u_char *session_id2 = NULL;
193 static pid_t monitor_child_pid;
195 struct mon_table {
196 enum monitor_reqtype type;
197 int flags;
198 int (*f)(int, Buffer *);
201 #define MON_ISAUTH 0x0004 /* Required for Authentication */
202 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
203 #define MON_ONCE 0x0010 /* Disable after calling */
204 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
206 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
208 #define MON_PERMIT 0x1000 /* Request is permitted */
210 struct mon_table mon_dispatch_proto20[] = {
211 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
212 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
213 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
214 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
215 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
216 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
217 #ifdef USE_PAM
218 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
219 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
220 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
221 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
222 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
223 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
224 #endif
225 #ifdef SSH_AUDIT_EVENTS
226 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
227 #endif
228 #ifdef BSD_AUTH
229 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
230 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
231 #endif
232 #ifdef SKEY
233 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
234 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
235 #endif
236 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
237 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
238 #ifdef GSSAPI
239 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
240 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
241 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
242 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
243 #endif
244 {0, 0, NULL}
247 struct mon_table mon_dispatch_postauth20[] = {
248 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
249 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
250 {MONITOR_REQ_PTY, 0, mm_answer_pty},
251 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
252 {MONITOR_REQ_TERM, 0, mm_answer_term},
253 #ifdef SSH_AUDIT_EVENTS
254 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
255 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
256 #endif
257 {0, 0, NULL}
260 struct mon_table mon_dispatch_proto15[] = {
261 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
262 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
263 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
264 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
265 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
266 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
267 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
268 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
269 #ifdef BSD_AUTH
270 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
271 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
272 #endif
273 #ifdef SKEY
274 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
275 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
276 #endif
277 #ifdef USE_PAM
278 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
279 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
280 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
281 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
282 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
283 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
284 #endif
285 #ifdef SSH_AUDIT_EVENTS
286 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
287 #endif
288 {0, 0, NULL}
291 struct mon_table mon_dispatch_postauth15[] = {
292 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
293 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
294 {MONITOR_REQ_TERM, 0, mm_answer_term},
295 #ifdef SSH_AUDIT_EVENTS
296 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
297 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
298 #endif
299 {0, 0, NULL}
302 struct mon_table *mon_dispatch;
304 /* Specifies if a certain message is allowed at the moment */
306 static void
307 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
309 while (ent->f != NULL) {
310 if (ent->type == type) {
311 ent->flags &= ~MON_PERMIT;
312 ent->flags |= permit ? MON_PERMIT : 0;
313 return;
315 ent++;
319 static void
320 monitor_permit_authentications(int permit)
322 struct mon_table *ent = mon_dispatch;
324 while (ent->f != NULL) {
325 if (ent->flags & MON_AUTH) {
326 ent->flags &= ~MON_PERMIT;
327 ent->flags |= permit ? MON_PERMIT : 0;
329 ent++;
333 void
334 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
336 struct mon_table *ent;
337 int authenticated = 0;
339 debug3("preauth child monitor started");
341 authctxt = _authctxt;
342 memset(authctxt, 0, sizeof(*authctxt));
344 authctxt->loginmsg = &loginmsg;
346 if (compat20) {
347 mon_dispatch = mon_dispatch_proto20;
349 /* Permit requests for moduli and signatures */
350 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
351 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
352 } else {
353 mon_dispatch = mon_dispatch_proto15;
355 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
358 /* The first few requests do not require asynchronous access */
359 while (!authenticated) {
360 auth_method = "unknown";
361 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
362 if (authenticated) {
363 if (!(ent->flags & MON_AUTHDECIDE))
364 fatal("%s: unexpected authentication from %d",
365 __func__, ent->type);
366 if (authctxt->pw->pw_uid == 0 &&
367 !auth_root_allowed(auth_method))
368 authenticated = 0;
369 #ifdef USE_PAM
370 /* PAM needs to perform account checks after auth */
371 if (options.use_pam && authenticated) {
372 Buffer m;
374 buffer_init(&m);
375 mm_request_receive_expect(pmonitor->m_sendfd,
376 MONITOR_REQ_PAM_ACCOUNT, &m);
377 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
378 buffer_free(&m);
380 #endif
383 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
384 auth_log(authctxt, authenticated, auth_method,
385 compat20 ? " ssh2" : "");
386 if (!authenticated)
387 authctxt->failures++;
391 if (!authctxt->valid)
392 fatal("%s: authenticated invalid user", __func__);
393 if (strcmp(auth_method, "unknown") == 0)
394 fatal("%s: authentication method name unknown", __func__);
396 debug("%s: %s has been authenticated by privileged process",
397 __func__, authctxt->user);
399 mm_get_keystate(pmonitor);
402 static void
403 monitor_set_child_handler(pid_t pid)
405 monitor_child_pid = pid;
408 static void
409 monitor_child_handler(int sig)
411 kill(monitor_child_pid, sig);
414 void
415 monitor_child_postauth(struct monitor *pmonitor)
417 monitor_set_child_handler(pmonitor->m_pid);
418 signal(SIGHUP, &monitor_child_handler);
419 signal(SIGTERM, &monitor_child_handler);
420 signal(SIGINT, &monitor_child_handler);
422 if (compat20) {
423 mon_dispatch = mon_dispatch_postauth20;
425 /* Permit requests for moduli and signatures */
426 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
427 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
428 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
429 } else {
430 mon_dispatch = mon_dispatch_postauth15;
431 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
433 if (!no_pty_flag) {
434 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
435 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
438 for (;;)
439 monitor_read(pmonitor, mon_dispatch, NULL);
442 void
443 monitor_sync(struct monitor *pmonitor)
445 if (options.compression) {
446 /* The member allocation is not visible, so sync it */
447 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
452 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
453 struct mon_table **pent)
455 Buffer m;
456 int ret;
457 u_char type;
459 buffer_init(&m);
461 mm_request_receive(pmonitor->m_sendfd, &m);
462 type = buffer_get_char(&m);
464 debug3("%s: checking request %d", __func__, type);
466 while (ent->f != NULL) {
467 if (ent->type == type)
468 break;
469 ent++;
472 if (ent->f != NULL) {
473 if (!(ent->flags & MON_PERMIT))
474 fatal("%s: unpermitted request %d", __func__,
475 type);
476 ret = (*ent->f)(pmonitor->m_sendfd, &m);
477 buffer_free(&m);
479 /* The child may use this request only once, disable it */
480 if (ent->flags & MON_ONCE) {
481 debug2("%s: %d used once, disabling now", __func__,
482 type);
483 ent->flags &= ~MON_PERMIT;
486 if (pent != NULL)
487 *pent = ent;
489 return ret;
492 fatal("%s: unsupported request: %d", __func__, type);
494 /* NOTREACHED */
495 return (-1);
498 /* allowed key state */
499 static int
500 monitor_allowed_key(u_char *blob, u_int bloblen)
502 /* make sure key is allowed */
503 if (key_blob == NULL || key_bloblen != bloblen ||
504 memcmp(key_blob, blob, key_bloblen))
505 return (0);
506 return (1);
509 static void
510 monitor_reset_key_state(void)
512 /* reset state */
513 if (key_blob != NULL)
514 xfree(key_blob);
515 if (hostbased_cuser != NULL)
516 xfree(hostbased_cuser);
517 if (hostbased_chost != NULL)
518 xfree(hostbased_chost);
519 key_blob = NULL;
520 key_bloblen = 0;
521 key_blobtype = MM_NOKEY;
522 hostbased_cuser = NULL;
523 hostbased_chost = NULL;
527 mm_answer_moduli(int sock, Buffer *m)
529 DH *dh;
530 int min, want, max;
532 min = buffer_get_int(m);
533 want = buffer_get_int(m);
534 max = buffer_get_int(m);
536 debug3("%s: got parameters: %d %d %d",
537 __func__, min, want, max);
538 /* We need to check here, too, in case the child got corrupted */
539 if (max < min || want < min || max < want)
540 fatal("%s: bad parameters: %d %d %d",
541 __func__, min, want, max);
543 buffer_clear(m);
545 dh = choose_dh(min, want, max);
546 if (dh == NULL) {
547 buffer_put_char(m, 0);
548 return (0);
549 } else {
550 /* Send first bignum */
551 buffer_put_char(m, 1);
552 buffer_put_bignum2(m, dh->p);
553 buffer_put_bignum2(m, dh->g);
555 DH_free(dh);
557 mm_request_send(sock, MONITOR_ANS_MODULI, m);
558 return (0);
562 mm_answer_sign(int sock, Buffer *m)
564 Key *key;
565 u_char *p;
566 u_char *signature;
567 u_int siglen, datlen;
568 int keyid;
570 debug3("%s", __func__);
572 keyid = buffer_get_int(m);
573 p = buffer_get_string(m, &datlen);
576 * Supported KEX types will only return SHA1 (20 byte) or
577 * SHA256 (32 byte) hashes
579 if (datlen != 20 && datlen != 32)
580 fatal("%s: data length incorrect: %u", __func__, datlen);
582 /* save session id, it will be passed on the first call */
583 if (session_id2_len == 0) {
584 session_id2_len = datlen;
585 session_id2 = xmalloc(session_id2_len);
586 memcpy(session_id2, p, session_id2_len);
589 if ((key = get_hostkey_by_index(keyid)) == NULL)
590 fatal("%s: no hostkey from index %d", __func__, keyid);
591 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
592 fatal("%s: key_sign failed", __func__);
594 debug3("%s: signature %p(%u)", __func__, signature, siglen);
596 buffer_clear(m);
597 buffer_put_string(m, signature, siglen);
599 xfree(p);
600 xfree(signature);
602 mm_request_send(sock, MONITOR_ANS_SIGN, m);
604 /* Turn on permissions for getpwnam */
605 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
607 return (0);
610 /* Retrieves the password entry and also checks if the user is permitted */
613 mm_answer_pwnamallow(int sock, Buffer *m)
615 char *username;
616 struct passwd *pwent;
617 int allowed = 0;
619 debug3("%s", __func__);
621 if (authctxt->attempt++ != 0)
622 fatal("%s: multiple attempts for getpwnam", __func__);
624 username = buffer_get_string(m, NULL);
626 pwent = getpwnamallow(username);
628 authctxt->user = xstrdup(username);
629 setproctitle("%s [priv]", pwent ? username : "unknown");
630 xfree(username);
632 buffer_clear(m);
634 if (pwent == NULL) {
635 buffer_put_char(m, 0);
636 authctxt->pw = fakepw();
637 goto out;
640 allowed = 1;
641 authctxt->pw = pwent;
642 authctxt->valid = 1;
644 buffer_put_char(m, 1);
645 buffer_put_string(m, pwent, sizeof(struct passwd));
646 buffer_put_cstring(m, pwent->pw_name);
647 buffer_put_cstring(m, "*");
648 buffer_put_cstring(m, pwent->pw_gecos);
649 #ifdef HAVE_PW_CLASS_IN_PASSWD
650 buffer_put_cstring(m, pwent->pw_class);
651 #endif
652 buffer_put_cstring(m, pwent->pw_dir);
653 buffer_put_cstring(m, pwent->pw_shell);
655 out:
656 buffer_put_string(m, &options, sizeof(options));
657 if (options.banner != NULL)
658 buffer_put_cstring(m, options.banner);
659 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
660 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
662 /* For SSHv1 allow authentication now */
663 if (!compat20)
664 monitor_permit_authentications(1);
665 else {
666 /* Allow service/style information on the auth context */
667 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
668 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
671 #ifdef USE_PAM
672 if (options.use_pam)
673 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
674 #endif
676 return (0);
679 int mm_answer_auth2_read_banner(int sock, Buffer *m)
681 char *banner;
683 buffer_clear(m);
684 banner = auth2_read_banner();
685 buffer_put_cstring(m, banner != NULL ? banner : "");
686 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
688 if (banner != NULL)
689 xfree(banner);
691 return (0);
695 mm_answer_authserv(int sock, Buffer *m)
697 monitor_permit_authentications(1);
699 authctxt->service = buffer_get_string(m, NULL);
700 authctxt->style = buffer_get_string(m, NULL);
701 debug3("%s: service=%s, style=%s",
702 __func__, authctxt->service, authctxt->style);
704 if (strlen(authctxt->style) == 0) {
705 xfree(authctxt->style);
706 authctxt->style = NULL;
709 return (0);
713 mm_answer_authpassword(int sock, Buffer *m)
715 static int call_count;
716 char *passwd;
717 int authenticated;
718 u_int plen;
720 passwd = buffer_get_string(m, &plen);
721 /* Only authenticate if the context is valid */
722 authenticated = options.password_authentication &&
723 auth_password(authctxt, passwd);
724 memset(passwd, 0, strlen(passwd));
725 xfree(passwd);
727 buffer_clear(m);
728 buffer_put_int(m, authenticated);
730 debug3("%s: sending result %d", __func__, authenticated);
731 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
733 call_count++;
734 if (plen == 0 && call_count == 1)
735 auth_method = "none";
736 else
737 auth_method = "password";
739 /* Causes monitor loop to terminate if authenticated */
740 return (authenticated);
743 #ifdef BSD_AUTH
745 mm_answer_bsdauthquery(int sock, Buffer *m)
747 char *name, *infotxt;
748 u_int numprompts;
749 u_int *echo_on;
750 char **prompts;
751 u_int success;
753 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
754 &prompts, &echo_on) < 0 ? 0 : 1;
756 buffer_clear(m);
757 buffer_put_int(m, success);
758 if (success)
759 buffer_put_cstring(m, prompts[0]);
761 debug3("%s: sending challenge success: %u", __func__, success);
762 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
764 if (success) {
765 xfree(name);
766 xfree(infotxt);
767 xfree(prompts);
768 xfree(echo_on);
771 return (0);
775 mm_answer_bsdauthrespond(int sock, Buffer *m)
777 char *response;
778 int authok;
780 if (authctxt->as == 0)
781 fatal("%s: no bsd auth session", __func__);
783 response = buffer_get_string(m, NULL);
784 authok = options.challenge_response_authentication &&
785 auth_userresponse(authctxt->as, response, 0);
786 authctxt->as = NULL;
787 debug3("%s: <%s> = <%d>", __func__, response, authok);
788 xfree(response);
790 buffer_clear(m);
791 buffer_put_int(m, authok);
793 debug3("%s: sending authenticated: %d", __func__, authok);
794 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
796 auth_method = "bsdauth";
798 return (authok != 0);
800 #endif
802 #ifdef SKEY
804 mm_answer_skeyquery(int sock, Buffer *m)
806 struct skey skey;
807 char challenge[1024];
808 u_int success;
810 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
811 sizeof(challenge)) < 0 ? 0 : 1;
813 buffer_clear(m);
814 buffer_put_int(m, success);
815 if (success)
816 buffer_put_cstring(m, challenge);
818 debug3("%s: sending challenge success: %u", __func__, success);
819 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
821 return (0);
825 mm_answer_skeyrespond(int sock, Buffer *m)
827 char *response;
828 int authok;
830 response = buffer_get_string(m, NULL);
832 authok = (options.challenge_response_authentication &&
833 authctxt->valid &&
834 skey_haskey(authctxt->pw->pw_name) == 0 &&
835 skey_passcheck(authctxt->pw->pw_name, response) != -1);
837 xfree(response);
839 buffer_clear(m);
840 buffer_put_int(m, authok);
842 debug3("%s: sending authenticated: %d", __func__, authok);
843 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
845 auth_method = "skey";
847 return (authok != 0);
849 #endif
851 #ifdef USE_PAM
853 mm_answer_pam_start(int sock, Buffer *m)
855 if (!options.use_pam)
856 fatal("UsePAM not set, but ended up in %s anyway", __func__);
858 start_pam(authctxt);
860 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
862 return (0);
866 mm_answer_pam_account(int sock, Buffer *m)
868 u_int ret;
870 if (!options.use_pam)
871 fatal("UsePAM not set, but ended up in %s anyway", __func__);
873 ret = do_pam_account();
875 buffer_put_int(m, ret);
876 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
878 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
880 return (ret);
883 static void *sshpam_ctxt, *sshpam_authok;
884 extern KbdintDevice sshpam_device;
887 mm_answer_pam_init_ctx(int sock, Buffer *m)
890 debug3("%s", __func__);
891 authctxt->user = buffer_get_string(m, NULL);
892 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
893 sshpam_authok = NULL;
894 buffer_clear(m);
895 if (sshpam_ctxt != NULL) {
896 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
897 buffer_put_int(m, 1);
898 } else {
899 buffer_put_int(m, 0);
901 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
902 return (0);
906 mm_answer_pam_query(int sock, Buffer *m)
908 char *name, *info, **prompts;
909 u_int i, num, *echo_on;
910 int ret;
912 debug3("%s", __func__);
913 sshpam_authok = NULL;
914 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
915 if (ret == 0 && num == 0)
916 sshpam_authok = sshpam_ctxt;
917 if (num > 1 || name == NULL || info == NULL)
918 ret = -1;
919 buffer_clear(m);
920 buffer_put_int(m, ret);
921 buffer_put_cstring(m, name);
922 xfree(name);
923 buffer_put_cstring(m, info);
924 xfree(info);
925 buffer_put_int(m, num);
926 for (i = 0; i < num; ++i) {
927 buffer_put_cstring(m, prompts[i]);
928 xfree(prompts[i]);
929 buffer_put_int(m, echo_on[i]);
931 if (prompts != NULL)
932 xfree(prompts);
933 if (echo_on != NULL)
934 xfree(echo_on);
935 auth_method = "keyboard-interactive/pam";
936 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
937 return (0);
941 mm_answer_pam_respond(int sock, Buffer *m)
943 char **resp;
944 u_int i, num;
945 int ret;
947 debug3("%s", __func__);
948 sshpam_authok = NULL;
949 num = buffer_get_int(m);
950 if (num > 0) {
951 resp = xcalloc(num, sizeof(char *));
952 for (i = 0; i < num; ++i)
953 resp[i] = buffer_get_string(m, NULL);
954 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
955 for (i = 0; i < num; ++i)
956 xfree(resp[i]);
957 xfree(resp);
958 } else {
959 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
961 buffer_clear(m);
962 buffer_put_int(m, ret);
963 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
964 auth_method = "keyboard-interactive/pam";
965 if (ret == 0)
966 sshpam_authok = sshpam_ctxt;
967 return (0);
971 mm_answer_pam_free_ctx(int sock, Buffer *m)
974 debug3("%s", __func__);
975 (sshpam_device.free_ctx)(sshpam_ctxt);
976 buffer_clear(m);
977 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
978 auth_method = "keyboard-interactive/pam";
979 return (sshpam_authok == sshpam_ctxt);
981 #endif
983 static void
984 mm_append_debug(Buffer *m)
986 if (auth_debug_init && buffer_len(&auth_debug)) {
987 debug3("%s: Appending debug messages for child", __func__);
988 buffer_append(m, buffer_ptr(&auth_debug),
989 buffer_len(&auth_debug));
990 buffer_clear(&auth_debug);
995 mm_answer_keyallowed(int sock, Buffer *m)
997 Key *key;
998 char *cuser, *chost;
999 u_char *blob;
1000 u_int bloblen;
1001 enum mm_keytype type = 0;
1002 int allowed = 0;
1004 debug3("%s entering", __func__);
1006 type = buffer_get_int(m);
1007 cuser = buffer_get_string(m, NULL);
1008 chost = buffer_get_string(m, NULL);
1009 blob = buffer_get_string(m, &bloblen);
1011 key = key_from_blob(blob, bloblen);
1013 if ((compat20 && type == MM_RSAHOSTKEY) ||
1014 (!compat20 && type != MM_RSAHOSTKEY))
1015 fatal("%s: key type and protocol mismatch", __func__);
1017 debug3("%s: key_from_blob: %p", __func__, key);
1019 if (key != NULL && authctxt->valid) {
1020 switch (type) {
1021 case MM_USERKEY:
1022 allowed = options.pubkey_authentication &&
1023 user_key_allowed(authctxt->pw, key);
1024 auth_method = "publickey";
1025 break;
1026 case MM_HOSTKEY:
1027 allowed = options.hostbased_authentication &&
1028 hostbased_key_allowed(authctxt->pw,
1029 cuser, chost, key);
1030 auth_method = "hostbased";
1031 break;
1032 case MM_RSAHOSTKEY:
1033 key->type = KEY_RSA1; /* XXX */
1034 allowed = options.rhosts_rsa_authentication &&
1035 auth_rhosts_rsa_key_allowed(authctxt->pw,
1036 cuser, chost, key);
1037 auth_method = "rsa";
1038 break;
1039 default:
1040 fatal("%s: unknown key type %d", __func__, type);
1041 break;
1044 if (key != NULL)
1045 key_free(key);
1047 /* clear temporarily storage (used by verify) */
1048 monitor_reset_key_state();
1050 if (allowed) {
1051 /* Save temporarily for comparison in verify */
1052 key_blob = blob;
1053 key_bloblen = bloblen;
1054 key_blobtype = type;
1055 hostbased_cuser = cuser;
1056 hostbased_chost = chost;
1057 } else {
1058 /* Log failed attempt */
1059 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1060 xfree(blob);
1061 xfree(cuser);
1062 xfree(chost);
1065 debug3("%s: key %p is %s",
1066 __func__, key, allowed ? "allowed" : "disallowed");
1068 buffer_clear(m);
1069 buffer_put_int(m, allowed);
1070 buffer_put_int(m, forced_command != NULL);
1072 mm_append_debug(m);
1074 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1076 if (type == MM_RSAHOSTKEY)
1077 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1079 return (0);
1082 static int
1083 monitor_valid_userblob(u_char *data, u_int datalen)
1085 Buffer b;
1086 char *p;
1087 u_int len;
1088 int fail = 0;
1090 buffer_init(&b);
1091 buffer_append(&b, data, datalen);
1093 if (datafellows & SSH_OLD_SESSIONID) {
1094 p = buffer_ptr(&b);
1095 len = buffer_len(&b);
1096 if ((session_id2 == NULL) ||
1097 (len < session_id2_len) ||
1098 (memcmp(p, session_id2, session_id2_len) != 0))
1099 fail++;
1100 buffer_consume(&b, session_id2_len);
1101 } else {
1102 p = buffer_get_string(&b, &len);
1103 if ((session_id2 == NULL) ||
1104 (len != session_id2_len) ||
1105 (memcmp(p, session_id2, session_id2_len) != 0))
1106 fail++;
1107 xfree(p);
1109 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1110 fail++;
1111 p = buffer_get_string(&b, NULL);
1112 if (strcmp(authctxt->user, p) != 0) {
1113 logit("wrong user name passed to monitor: expected %s != %.100s",
1114 authctxt->user, p);
1115 fail++;
1117 xfree(p);
1118 buffer_skip_string(&b);
1119 if (datafellows & SSH_BUG_PKAUTH) {
1120 if (!buffer_get_char(&b))
1121 fail++;
1122 } else {
1123 p = buffer_get_string(&b, NULL);
1124 if (strcmp("publickey", p) != 0)
1125 fail++;
1126 xfree(p);
1127 if (!buffer_get_char(&b))
1128 fail++;
1129 buffer_skip_string(&b);
1131 buffer_skip_string(&b);
1132 if (buffer_len(&b) != 0)
1133 fail++;
1134 buffer_free(&b);
1135 return (fail == 0);
1138 static int
1139 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1140 char *chost)
1142 Buffer b;
1143 char *p;
1144 u_int len;
1145 int fail = 0;
1147 buffer_init(&b);
1148 buffer_append(&b, data, datalen);
1150 p = buffer_get_string(&b, &len);
1151 if ((session_id2 == NULL) ||
1152 (len != session_id2_len) ||
1153 (memcmp(p, session_id2, session_id2_len) != 0))
1154 fail++;
1155 xfree(p);
1157 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1158 fail++;
1159 p = buffer_get_string(&b, NULL);
1160 if (strcmp(authctxt->user, p) != 0) {
1161 logit("wrong user name passed to monitor: expected %s != %.100s",
1162 authctxt->user, p);
1163 fail++;
1165 xfree(p);
1166 buffer_skip_string(&b); /* service */
1167 p = buffer_get_string(&b, NULL);
1168 if (strcmp(p, "hostbased") != 0)
1169 fail++;
1170 xfree(p);
1171 buffer_skip_string(&b); /* pkalg */
1172 buffer_skip_string(&b); /* pkblob */
1174 /* verify client host, strip trailing dot if necessary */
1175 p = buffer_get_string(&b, NULL);
1176 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1177 p[len - 1] = '\0';
1178 if (strcmp(p, chost) != 0)
1179 fail++;
1180 xfree(p);
1182 /* verify client user */
1183 p = buffer_get_string(&b, NULL);
1184 if (strcmp(p, cuser) != 0)
1185 fail++;
1186 xfree(p);
1188 if (buffer_len(&b) != 0)
1189 fail++;
1190 buffer_free(&b);
1191 return (fail == 0);
1195 mm_answer_keyverify(int sock, Buffer *m)
1197 Key *key;
1198 u_char *signature, *data, *blob;
1199 u_int signaturelen, datalen, bloblen;
1200 int verified = 0;
1201 int valid_data = 0;
1203 blob = buffer_get_string(m, &bloblen);
1204 signature = buffer_get_string(m, &signaturelen);
1205 data = buffer_get_string(m, &datalen);
1207 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1208 !monitor_allowed_key(blob, bloblen))
1209 fatal("%s: bad key, not previously allowed", __func__);
1211 key = key_from_blob(blob, bloblen);
1212 if (key == NULL)
1213 fatal("%s: bad public key blob", __func__);
1215 switch (key_blobtype) {
1216 case MM_USERKEY:
1217 valid_data = monitor_valid_userblob(data, datalen);
1218 break;
1219 case MM_HOSTKEY:
1220 valid_data = monitor_valid_hostbasedblob(data, datalen,
1221 hostbased_cuser, hostbased_chost);
1222 break;
1223 default:
1224 valid_data = 0;
1225 break;
1227 if (!valid_data)
1228 fatal("%s: bad signature data blob", __func__);
1230 verified = key_verify(key, signature, signaturelen, data, datalen);
1231 debug3("%s: key %p signature %s",
1232 __func__, key, (verified == 1) ? "verified" : "unverified");
1234 key_free(key);
1235 xfree(blob);
1236 xfree(signature);
1237 xfree(data);
1239 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1241 monitor_reset_key_state();
1243 buffer_clear(m);
1244 buffer_put_int(m, verified);
1245 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1247 return (verified == 1);
1250 static void
1251 mm_record_login(Session *s, struct passwd *pw)
1253 socklen_t fromlen;
1254 struct sockaddr_storage from;
1257 * Get IP address of client. If the connection is not a socket, let
1258 * the address be 0.0.0.0.
1260 memset(&from, 0, sizeof(from));
1261 fromlen = sizeof(from);
1262 if (packet_connection_is_on_socket()) {
1263 if (getpeername(packet_get_connection_in(),
1264 (struct sockaddr *)&from, &fromlen) < 0) {
1265 debug("getpeername: %.100s", strerror(errno));
1266 cleanup_exit(255);
1269 /* Record that there was a login on that tty from the remote host. */
1270 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1271 get_remote_name_or_ip(utmp_len, options.use_dns),
1272 (struct sockaddr *)&from, fromlen);
1275 static void
1276 mm_session_close(Session *s)
1278 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1279 if (s->ttyfd != -1) {
1280 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1281 session_pty_cleanup2(s);
1283 s->used = 0;
1287 mm_answer_pty(int sock, Buffer *m)
1289 extern struct monitor *pmonitor;
1290 Session *s;
1291 int res, fd0;
1293 debug3("%s entering", __func__);
1295 buffer_clear(m);
1296 s = session_new();
1297 if (s == NULL)
1298 goto error;
1299 s->authctxt = authctxt;
1300 s->pw = authctxt->pw;
1301 s->pid = pmonitor->m_pid;
1302 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1303 if (res == 0)
1304 goto error;
1305 pty_setowner(authctxt->pw, s->tty);
1307 buffer_put_int(m, 1);
1308 buffer_put_cstring(m, s->tty);
1310 /* We need to trick ttyslot */
1311 if (dup2(s->ttyfd, 0) == -1)
1312 fatal("%s: dup2", __func__);
1314 mm_record_login(s, authctxt->pw);
1316 /* Now we can close the file descriptor again */
1317 close(0);
1319 /* send messages generated by record_login */
1320 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1321 buffer_clear(&loginmsg);
1323 mm_request_send(sock, MONITOR_ANS_PTY, m);
1325 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1326 mm_send_fd(sock, s->ttyfd) == -1)
1327 fatal("%s: send fds failed", __func__);
1329 /* make sure nothing uses fd 0 */
1330 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1331 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1332 if (fd0 != 0)
1333 error("%s: fd0 %d != 0", __func__, fd0);
1335 /* slave is not needed */
1336 close(s->ttyfd);
1337 s->ttyfd = s->ptyfd;
1338 /* no need to dup() because nobody closes ptyfd */
1339 s->ptymaster = s->ptyfd;
1341 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1343 return (0);
1345 error:
1346 if (s != NULL)
1347 mm_session_close(s);
1348 buffer_put_int(m, 0);
1349 mm_request_send(sock, MONITOR_ANS_PTY, m);
1350 return (0);
1354 mm_answer_pty_cleanup(int sock, Buffer *m)
1356 Session *s;
1357 char *tty;
1359 debug3("%s entering", __func__);
1361 tty = buffer_get_string(m, NULL);
1362 if ((s = session_by_tty(tty)) != NULL)
1363 mm_session_close(s);
1364 buffer_clear(m);
1365 xfree(tty);
1366 return (0);
1370 mm_answer_sesskey(int sock, Buffer *m)
1372 BIGNUM *p;
1373 int rsafail;
1375 /* Turn off permissions */
1376 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1378 if ((p = BN_new()) == NULL)
1379 fatal("%s: BN_new", __func__);
1381 buffer_get_bignum2(m, p);
1383 rsafail = ssh1_session_key(p);
1385 buffer_clear(m);
1386 buffer_put_int(m, rsafail);
1387 buffer_put_bignum2(m, p);
1389 BN_clear_free(p);
1391 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1393 /* Turn on permissions for sessid passing */
1394 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1396 return (0);
1400 mm_answer_sessid(int sock, Buffer *m)
1402 int i;
1404 debug3("%s entering", __func__);
1406 if (buffer_len(m) != 16)
1407 fatal("%s: bad ssh1 session id", __func__);
1408 for (i = 0; i < 16; i++)
1409 session_id[i] = buffer_get_char(m);
1411 /* Turn on permissions for getpwnam */
1412 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1414 return (0);
1418 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1420 BIGNUM *client_n;
1421 Key *key = NULL;
1422 u_char *blob = NULL;
1423 u_int blen = 0;
1424 int allowed = 0;
1426 debug3("%s entering", __func__);
1428 auth_method = "rsa";
1429 if (options.rsa_authentication && authctxt->valid) {
1430 if ((client_n = BN_new()) == NULL)
1431 fatal("%s: BN_new", __func__);
1432 buffer_get_bignum2(m, client_n);
1433 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1434 BN_clear_free(client_n);
1436 buffer_clear(m);
1437 buffer_put_int(m, allowed);
1438 buffer_put_int(m, forced_command != NULL);
1440 /* clear temporarily storage (used by generate challenge) */
1441 monitor_reset_key_state();
1443 if (allowed && key != NULL) {
1444 key->type = KEY_RSA; /* cheat for key_to_blob */
1445 if (key_to_blob(key, &blob, &blen) == 0)
1446 fatal("%s: key_to_blob failed", __func__);
1447 buffer_put_string(m, blob, blen);
1449 /* Save temporarily for comparison in verify */
1450 key_blob = blob;
1451 key_bloblen = blen;
1452 key_blobtype = MM_RSAUSERKEY;
1454 if (key != NULL)
1455 key_free(key);
1457 mm_append_debug(m);
1459 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1461 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1462 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1463 return (0);
1467 mm_answer_rsa_challenge(int sock, Buffer *m)
1469 Key *key = NULL;
1470 u_char *blob;
1471 u_int blen;
1473 debug3("%s entering", __func__);
1475 if (!authctxt->valid)
1476 fatal("%s: authctxt not valid", __func__);
1477 blob = buffer_get_string(m, &blen);
1478 if (!monitor_allowed_key(blob, blen))
1479 fatal("%s: bad key, not previously allowed", __func__);
1480 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1481 fatal("%s: key type mismatch", __func__);
1482 if ((key = key_from_blob(blob, blen)) == NULL)
1483 fatal("%s: received bad key", __func__);
1485 if (ssh1_challenge)
1486 BN_clear_free(ssh1_challenge);
1487 ssh1_challenge = auth_rsa_generate_challenge(key);
1489 buffer_clear(m);
1490 buffer_put_bignum2(m, ssh1_challenge);
1492 debug3("%s sending reply", __func__);
1493 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1495 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1497 xfree(blob);
1498 key_free(key);
1499 return (0);
1503 mm_answer_rsa_response(int sock, Buffer *m)
1505 Key *key = NULL;
1506 u_char *blob, *response;
1507 u_int blen, len;
1508 int success;
1510 debug3("%s entering", __func__);
1512 if (!authctxt->valid)
1513 fatal("%s: authctxt not valid", __func__);
1514 if (ssh1_challenge == NULL)
1515 fatal("%s: no ssh1_challenge", __func__);
1517 blob = buffer_get_string(m, &blen);
1518 if (!monitor_allowed_key(blob, blen))
1519 fatal("%s: bad key, not previously allowed", __func__);
1520 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1521 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1522 if ((key = key_from_blob(blob, blen)) == NULL)
1523 fatal("%s: received bad key", __func__);
1524 response = buffer_get_string(m, &len);
1525 if (len != 16)
1526 fatal("%s: received bad response to challenge", __func__);
1527 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1529 xfree(blob);
1530 key_free(key);
1531 xfree(response);
1533 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1535 /* reset state */
1536 BN_clear_free(ssh1_challenge);
1537 ssh1_challenge = NULL;
1538 monitor_reset_key_state();
1540 buffer_clear(m);
1541 buffer_put_int(m, success);
1542 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1544 return (success);
1548 mm_answer_term(int sock, Buffer *req)
1550 extern struct monitor *pmonitor;
1551 int res, status;
1553 debug3("%s: tearing down sessions", __func__);
1555 /* The child is terminating */
1556 session_destroy_all(&mm_session_close);
1558 #ifdef USE_PAM
1559 if (options.use_pam)
1560 sshpam_cleanup();
1561 #endif
1563 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1564 if (errno != EINTR)
1565 exit(1);
1567 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1569 /* Terminate process */
1570 exit(res);
1573 #ifdef SSH_AUDIT_EVENTS
1574 /* Report that an audit event occurred */
1576 mm_answer_audit_event(int socket, Buffer *m)
1578 ssh_audit_event_t event;
1580 debug3("%s entering", __func__);
1582 event = buffer_get_int(m);
1583 switch(event) {
1584 case SSH_AUTH_FAIL_PUBKEY:
1585 case SSH_AUTH_FAIL_HOSTBASED:
1586 case SSH_AUTH_FAIL_GSSAPI:
1587 case SSH_LOGIN_EXCEED_MAXTRIES:
1588 case SSH_LOGIN_ROOT_DENIED:
1589 case SSH_CONNECTION_CLOSE:
1590 case SSH_INVALID_USER:
1591 audit_event(event);
1592 break;
1593 default:
1594 fatal("Audit event type %d not permitted", event);
1597 return (0);
1601 mm_answer_audit_command(int socket, Buffer *m)
1603 u_int len;
1604 char *cmd;
1606 debug3("%s entering", __func__);
1607 cmd = buffer_get_string(m, &len);
1608 /* sanity check command, if so how? */
1609 audit_run_command(cmd);
1610 xfree(cmd);
1611 return (0);
1613 #endif /* SSH_AUDIT_EVENTS */
1615 void
1616 monitor_apply_keystate(struct monitor *pmonitor)
1618 if (compat20) {
1619 set_newkeys(MODE_IN);
1620 set_newkeys(MODE_OUT);
1621 } else {
1622 packet_set_protocol_flags(child_state.ssh1protoflags);
1623 packet_set_encryption_key(child_state.ssh1key,
1624 child_state.ssh1keylen, child_state.ssh1cipher);
1625 xfree(child_state.ssh1key);
1628 /* for rc4 and other stateful ciphers */
1629 packet_set_keycontext(MODE_OUT, child_state.keyout);
1630 xfree(child_state.keyout);
1631 packet_set_keycontext(MODE_IN, child_state.keyin);
1632 xfree(child_state.keyin);
1634 if (!compat20) {
1635 packet_set_iv(MODE_OUT, child_state.ivout);
1636 xfree(child_state.ivout);
1637 packet_set_iv(MODE_IN, child_state.ivin);
1638 xfree(child_state.ivin);
1641 memcpy(&incoming_stream, &child_state.incoming,
1642 sizeof(incoming_stream));
1643 memcpy(&outgoing_stream, &child_state.outgoing,
1644 sizeof(outgoing_stream));
1646 /* Update with new address */
1647 if (options.compression)
1648 mm_init_compression(pmonitor->m_zlib);
1650 /* Network I/O buffers */
1651 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1652 buffer_clear(&input);
1653 buffer_append(&input, child_state.input, child_state.ilen);
1654 memset(child_state.input, 0, child_state.ilen);
1655 xfree(child_state.input);
1657 buffer_clear(&output);
1658 buffer_append(&output, child_state.output, child_state.olen);
1659 memset(child_state.output, 0, child_state.olen);
1660 xfree(child_state.output);
1663 static Kex *
1664 mm_get_kex(Buffer *m)
1666 Kex *kex;
1667 void *blob;
1668 u_int bloblen;
1670 kex = xcalloc(1, sizeof(*kex));
1671 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1672 if ((session_id2 == NULL) ||
1673 (kex->session_id_len != session_id2_len) ||
1674 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1675 fatal("mm_get_get: internal error: bad session id");
1676 kex->we_need = buffer_get_int(m);
1677 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1678 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1679 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1680 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1681 kex->server = 1;
1682 kex->hostkey_type = buffer_get_int(m);
1683 kex->kex_type = buffer_get_int(m);
1684 blob = buffer_get_string(m, &bloblen);
1685 buffer_init(&kex->my);
1686 buffer_append(&kex->my, blob, bloblen);
1687 xfree(blob);
1688 blob = buffer_get_string(m, &bloblen);
1689 buffer_init(&kex->peer);
1690 buffer_append(&kex->peer, blob, bloblen);
1691 xfree(blob);
1692 kex->done = 1;
1693 kex->flags = buffer_get_int(m);
1694 kex->client_version_string = buffer_get_string(m, NULL);
1695 kex->server_version_string = buffer_get_string(m, NULL);
1696 kex->load_host_key=&get_hostkey_by_type;
1697 kex->host_key_index=&get_hostkey_index;
1699 return (kex);
1702 /* This function requries careful sanity checking */
1704 void
1705 mm_get_keystate(struct monitor *pmonitor)
1707 Buffer m;
1708 u_char *blob, *p;
1709 u_int bloblen, plen;
1710 u_int32_t seqnr, packets;
1711 u_int64_t blocks;
1713 debug3("%s: Waiting for new keys", __func__);
1715 buffer_init(&m);
1716 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1717 if (!compat20) {
1718 child_state.ssh1protoflags = buffer_get_int(&m);
1719 child_state.ssh1cipher = buffer_get_int(&m);
1720 child_state.ssh1key = buffer_get_string(&m,
1721 &child_state.ssh1keylen);
1722 child_state.ivout = buffer_get_string(&m,
1723 &child_state.ivoutlen);
1724 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1725 goto skip;
1726 } else {
1727 /* Get the Kex for rekeying */
1728 *pmonitor->m_pkex = mm_get_kex(&m);
1731 blob = buffer_get_string(&m, &bloblen);
1732 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1733 xfree(blob);
1735 debug3("%s: Waiting for second key", __func__);
1736 blob = buffer_get_string(&m, &bloblen);
1737 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1738 xfree(blob);
1740 /* Now get sequence numbers for the packets */
1741 seqnr = buffer_get_int(&m);
1742 blocks = buffer_get_int64(&m);
1743 packets = buffer_get_int(&m);
1744 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1745 seqnr = buffer_get_int(&m);
1746 blocks = buffer_get_int64(&m);
1747 packets = buffer_get_int(&m);
1748 packet_set_state(MODE_IN, seqnr, blocks, packets);
1750 skip:
1751 /* Get the key context */
1752 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1753 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1755 debug3("%s: Getting compression state", __func__);
1756 /* Get compression state */
1757 p = buffer_get_string(&m, &plen);
1758 if (plen != sizeof(child_state.outgoing))
1759 fatal("%s: bad request size", __func__);
1760 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1761 xfree(p);
1763 p = buffer_get_string(&m, &plen);
1764 if (plen != sizeof(child_state.incoming))
1765 fatal("%s: bad request size", __func__);
1766 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1767 xfree(p);
1769 /* Network I/O buffers */
1770 debug3("%s: Getting Network I/O buffers", __func__);
1771 child_state.input = buffer_get_string(&m, &child_state.ilen);
1772 child_state.output = buffer_get_string(&m, &child_state.olen);
1774 buffer_free(&m);
1778 /* Allocation functions for zlib */
1779 void *
1780 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1782 size_t len = (size_t) size * ncount;
1783 void *address;
1785 if (len == 0 || ncount > SIZE_T_MAX / size)
1786 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1788 address = mm_malloc(mm, len);
1790 return (address);
1793 void
1794 mm_zfree(struct mm_master *mm, void *address)
1796 mm_free(mm, address);
1799 void
1800 mm_init_compression(struct mm_master *mm)
1802 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1803 outgoing_stream.zfree = (free_func)mm_zfree;
1804 outgoing_stream.opaque = mm;
1806 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1807 incoming_stream.zfree = (free_func)mm_zfree;
1808 incoming_stream.opaque = mm;
1811 /* XXX */
1813 #define FD_CLOSEONEXEC(x) do { \
1814 if (fcntl(x, F_SETFD, 1) == -1) \
1815 fatal("fcntl(%d, F_SETFD)", x); \
1816 } while (0)
1818 static void
1819 monitor_socketpair(int *pair)
1821 #ifdef HAVE_SOCKETPAIR
1822 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1823 fatal("%s: socketpair", __func__);
1824 #else
1825 fatal("%s: UsePrivilegeSeparation=yes not supported",
1826 __func__);
1827 #endif
1828 FD_CLOSEONEXEC(pair[0]);
1829 FD_CLOSEONEXEC(pair[1]);
1832 #define MM_MEMSIZE 65536
1834 struct monitor *
1835 monitor_init(void)
1837 struct monitor *mon;
1838 int pair[2];
1840 mon = xcalloc(1, sizeof(*mon));
1842 monitor_socketpair(pair);
1844 mon->m_recvfd = pair[0];
1845 mon->m_sendfd = pair[1];
1847 /* Used to share zlib space across processes */
1848 if (options.compression) {
1849 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1850 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1852 /* Compression needs to share state across borders */
1853 mm_init_compression(mon->m_zlib);
1856 return mon;
1859 void
1860 monitor_reinit(struct monitor *mon)
1862 int pair[2];
1864 monitor_socketpair(pair);
1866 mon->m_recvfd = pair[0];
1867 mon->m_sendfd = pair[1];
1870 #ifdef GSSAPI
1872 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1874 gss_OID_desc goid;
1875 OM_uint32 major;
1876 u_int len;
1878 goid.elements = buffer_get_string(m, &len);
1879 goid.length = len;
1881 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1883 xfree(goid.elements);
1885 buffer_clear(m);
1886 buffer_put_int(m, major);
1888 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1890 /* Now we have a context, enable the step */
1891 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1893 return (0);
1897 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1899 gss_buffer_desc in;
1900 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1901 OM_uint32 major, minor;
1902 OM_uint32 flags = 0; /* GSI needs this */
1903 u_int len;
1905 in.value = buffer_get_string(m, &len);
1906 in.length = len;
1907 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1908 xfree(in.value);
1910 buffer_clear(m);
1911 buffer_put_int(m, major);
1912 buffer_put_string(m, out.value, out.length);
1913 buffer_put_int(m, flags);
1914 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1916 gss_release_buffer(&minor, &out);
1918 if (major == GSS_S_COMPLETE) {
1919 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1920 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1921 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1923 return (0);
1927 mm_answer_gss_checkmic(int sock, Buffer *m)
1929 gss_buffer_desc gssbuf, mic;
1930 OM_uint32 ret;
1931 u_int len;
1933 gssbuf.value = buffer_get_string(m, &len);
1934 gssbuf.length = len;
1935 mic.value = buffer_get_string(m, &len);
1936 mic.length = len;
1938 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1940 xfree(gssbuf.value);
1941 xfree(mic.value);
1943 buffer_clear(m);
1944 buffer_put_int(m, ret);
1946 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1948 if (!GSS_ERROR(ret))
1949 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1951 return (0);
1955 mm_answer_gss_userok(int sock, Buffer *m)
1957 int authenticated;
1959 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1961 buffer_clear(m);
1962 buffer_put_int(m, authenticated);
1964 debug3("%s: sending result %d", __func__, authenticated);
1965 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1967 auth_method = "gssapi-with-mic";
1969 /* Monitor loop will terminate if authenticated */
1970 return (authenticated);
1972 #endif /* GSSAPI */