1 /* $OpenBSD: monitor.c,v 1.79 2006/07/08 21:48:53 stevesk Exp $ */
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
30 #include <sys/types.h>
31 #include <sys/socket.h>
44 #include <openssl/dh.h>
50 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
53 #define TARGET_OS_MAC 1
58 #include "auth-options.h"
67 #include "monitor_mm.h"
68 #include "monitor_wrap.h"
69 #include "monitor_fdpass.h"
79 static Gssctxt *gsscontext = NULL;
83 extern ServerOptions options;
84 extern u_int utmp_len;
85 extern Newkeys *current_keys[];
86 extern z_stream incoming_stream;
87 extern z_stream outgoing_stream;
88 extern u_char session_id[];
89 extern Buffer input, output;
90 extern Buffer auth_debug;
91 extern int auth_debug_init;
92 extern Buffer loginmsg;
94 /* State exported from the child */
117 /* Functions on the monitor that answer unprivileged requests */
119 int mm_answer_moduli(int, Buffer *);
120 int mm_answer_sign(int, Buffer *);
121 int mm_answer_pwnamallow(int, Buffer *);
122 int mm_answer_auth2_read_banner(int, Buffer *);
123 int mm_answer_authserv(int, Buffer *);
124 int mm_answer_authpassword(int, Buffer *);
125 int mm_answer_bsdauthquery(int, Buffer *);
126 int mm_answer_bsdauthrespond(int, Buffer *);
127 int mm_answer_skeyquery(int, Buffer *);
128 int mm_answer_skeyrespond(int, Buffer *);
129 int mm_answer_keyallowed(int, Buffer *);
130 int mm_answer_keyverify(int, Buffer *);
131 int mm_answer_pty(int, Buffer *);
132 int mm_answer_pty_cleanup(int, Buffer *);
133 int mm_answer_term(int, Buffer *);
134 int mm_answer_rsa_keyallowed(int, Buffer *);
135 int mm_answer_rsa_challenge(int, Buffer *);
136 int mm_answer_rsa_response(int, Buffer *);
137 int mm_answer_sesskey(int, Buffer *);
138 int mm_answer_sessid(int, Buffer *);
141 int mm_answer_pam_start(int, Buffer *);
142 int mm_answer_pam_account(int, Buffer *);
143 int mm_answer_pam_init_ctx(int, Buffer *);
144 int mm_answer_pam_query(int, Buffer *);
145 int mm_answer_pam_respond(int, Buffer *);
146 int mm_answer_pam_free_ctx(int, Buffer *);
150 int mm_answer_gss_setup_ctx(int, Buffer *);
151 int mm_answer_gss_accept_ctx(int, Buffer *);
152 int mm_answer_gss_userok(int, Buffer *);
153 int mm_answer_gss_checkmic(int, Buffer *);
156 #ifdef SSH_AUDIT_EVENTS
157 int mm_answer_audit_event(int, Buffer *);
158 int mm_answer_audit_command(int, Buffer *);
161 static Authctxt *authctxt;
162 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
164 /* local state for key verify */
165 static u_char *key_blob = NULL;
166 static u_int key_bloblen = 0;
167 static int key_blobtype = MM_NOKEY;
168 static char *hostbased_cuser = NULL;
169 static char *hostbased_chost = NULL;
170 static char *auth_method = "unknown";
171 static u_int session_id2_len = 0;
172 static u_char *session_id2 = NULL;
173 static pid_t monitor_child_pid;
176 enum monitor_reqtype type;
178 int (*f)(int, Buffer *);
181 #define MON_ISAUTH 0x0004 /* Required for Authentication */
182 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
183 #define MON_ONCE 0x0010 /* Disable after calling */
184 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
186 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
188 #define MON_PERMIT 0x1000 /* Request is permitted */
190 struct mon_table mon_dispatch_proto20[] = {
191 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
192 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
193 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
194 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
195 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
196 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
198 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
199 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
200 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
201 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
202 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
203 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
205 #ifdef SSH_AUDIT_EVENTS
206 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
209 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
210 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
213 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
214 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
216 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
217 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
219 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
220 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
221 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
222 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
227 struct mon_table mon_dispatch_postauth20[] = {
228 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
229 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
230 {MONITOR_REQ_PTY, 0, mm_answer_pty},
231 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
232 {MONITOR_REQ_TERM, 0, mm_answer_term},
233 #ifdef SSH_AUDIT_EVENTS
234 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
235 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
240 struct mon_table mon_dispatch_proto15[] = {
241 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
242 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
243 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
244 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
245 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
246 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
247 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
248 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
250 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
251 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
254 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
255 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
258 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
259 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
260 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
261 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
262 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
263 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
265 #ifdef SSH_AUDIT_EVENTS
266 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
271 struct mon_table mon_dispatch_postauth15[] = {
272 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
273 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
274 {MONITOR_REQ_TERM, 0, mm_answer_term},
275 #ifdef SSH_AUDIT_EVENTS
276 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
277 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
282 struct mon_table *mon_dispatch;
284 /* Specifies if a certain message is allowed at the moment */
287 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
289 while (ent->f != NULL) {
290 if (ent->type == type) {
291 ent->flags &= ~MON_PERMIT;
292 ent->flags |= permit ? MON_PERMIT : 0;
300 monitor_permit_authentications(int permit)
302 struct mon_table *ent = mon_dispatch;
304 while (ent->f != NULL) {
305 if (ent->flags & MON_AUTH) {
306 ent->flags &= ~MON_PERMIT;
307 ent->flags |= permit ? MON_PERMIT : 0;
314 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
316 struct mon_table *ent;
317 int authenticated = 0;
319 debug3("preauth child monitor started");
321 authctxt = _authctxt;
322 memset(authctxt, 0, sizeof(*authctxt));
324 authctxt->loginmsg = &loginmsg;
327 mon_dispatch = mon_dispatch_proto20;
329 /* Permit requests for moduli and signatures */
330 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
331 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
333 mon_dispatch = mon_dispatch_proto15;
335 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
338 /* The first few requests do not require asynchronous access */
339 while (!authenticated) {
340 auth_method = "unknown";
341 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
343 if (!(ent->flags & MON_AUTHDECIDE))
344 fatal("%s: unexpected authentication from %d",
345 __func__, ent->type);
346 if (authctxt->pw->pw_uid == 0 &&
347 !auth_root_allowed(auth_method))
350 /* PAM needs to perform account checks after auth */
351 if (options.use_pam && authenticated) {
355 mm_request_receive_expect(pmonitor->m_sendfd,
356 MONITOR_REQ_PAM_ACCOUNT, &m);
357 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
363 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
364 auth_log(authctxt, authenticated, auth_method,
365 compat20 ? " ssh2" : "");
367 authctxt->failures++;
371 if (!authctxt->valid)
372 fatal("%s: authenticated invalid user", __func__);
373 if (strcmp(auth_method, "unknown") == 0)
374 fatal("%s: authentication method name unknown", __func__);
376 debug("%s: %s has been authenticated by privileged process",
377 __func__, authctxt->user);
379 mm_get_keystate(pmonitor);
383 monitor_set_child_handler(pid_t pid)
385 monitor_child_pid = pid;
389 monitor_child_handler(int sig)
391 kill(monitor_child_pid, sig);
395 monitor_child_postauth(struct monitor *pmonitor)
397 monitor_set_child_handler(pmonitor->m_pid);
398 signal(SIGHUP, &monitor_child_handler);
399 signal(SIGTERM, &monitor_child_handler);
402 mon_dispatch = mon_dispatch_postauth20;
404 /* Permit requests for moduli and signatures */
405 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
406 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
407 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
409 mon_dispatch = mon_dispatch_postauth15;
410 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
413 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
414 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
418 monitor_read(pmonitor, mon_dispatch, NULL);
422 monitor_sync(struct monitor *pmonitor)
424 if (options.compression) {
425 /* The member allocation is not visible, so sync it */
426 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
431 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
432 struct mon_table **pent)
440 mm_request_receive(pmonitor->m_sendfd, &m);
441 type = buffer_get_char(&m);
443 debug3("%s: checking request %d", __func__, type);
445 while (ent->f != NULL) {
446 if (ent->type == type)
451 if (ent->f != NULL) {
452 if (!(ent->flags & MON_PERMIT))
453 fatal("%s: unpermitted request %d", __func__,
455 ret = (*ent->f)(pmonitor->m_sendfd, &m);
458 /* The child may use this request only once, disable it */
459 if (ent->flags & MON_ONCE) {
460 debug2("%s: %d used once, disabling now", __func__,
462 ent->flags &= ~MON_PERMIT;
471 fatal("%s: unsupported request: %d", __func__, type);
477 /* allowed key state */
479 monitor_allowed_key(u_char *blob, u_int bloblen)
481 /* make sure key is allowed */
482 if (key_blob == NULL || key_bloblen != bloblen ||
483 memcmp(key_blob, blob, key_bloblen))
489 monitor_reset_key_state(void)
492 if (key_blob != NULL)
494 if (hostbased_cuser != NULL)
495 xfree(hostbased_cuser);
496 if (hostbased_chost != NULL)
497 xfree(hostbased_chost);
500 key_blobtype = MM_NOKEY;
501 hostbased_cuser = NULL;
502 hostbased_chost = NULL;
506 mm_answer_moduli(int sock, Buffer *m)
511 min = buffer_get_int(m);
512 want = buffer_get_int(m);
513 max = buffer_get_int(m);
515 debug3("%s: got parameters: %d %d %d",
516 __func__, min, want, max);
517 /* We need to check here, too, in case the child got corrupted */
518 if (max < min || want < min || max < want)
519 fatal("%s: bad parameters: %d %d %d",
520 __func__, min, want, max);
524 dh = choose_dh(min, want, max);
526 buffer_put_char(m, 0);
529 /* Send first bignum */
530 buffer_put_char(m, 1);
531 buffer_put_bignum2(m, dh->p);
532 buffer_put_bignum2(m, dh->g);
536 mm_request_send(sock, MONITOR_ANS_MODULI, m);
541 mm_answer_sign(int sock, Buffer *m)
546 u_int siglen, datlen;
549 debug3("%s", __func__);
551 keyid = buffer_get_int(m);
552 p = buffer_get_string(m, &datlen);
555 * Supported KEX types will only return SHA1 (20 byte) or
556 * SHA256 (32 byte) hashes
558 if (datlen != 20 && datlen != 32)
559 fatal("%s: data length incorrect: %u", __func__, datlen);
561 /* save session id, it will be passed on the first call */
562 if (session_id2_len == 0) {
563 session_id2_len = datlen;
564 session_id2 = xmalloc(session_id2_len);
565 memcpy(session_id2, p, session_id2_len);
568 if ((key = get_hostkey_by_index(keyid)) == NULL)
569 fatal("%s: no hostkey from index %d", __func__, keyid);
570 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
571 fatal("%s: key_sign failed", __func__);
573 debug3("%s: signature %p(%u)", __func__, signature, siglen);
576 buffer_put_string(m, signature, siglen);
581 mm_request_send(sock, MONITOR_ANS_SIGN, m);
583 /* Turn on permissions for getpwnam */
584 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
589 /* Retrieves the password entry and also checks if the user is permitted */
592 mm_answer_pwnamallow(int sock, Buffer *m)
595 struct passwd *pwent;
598 debug3("%s", __func__);
600 if (authctxt->attempt++ != 0)
601 fatal("%s: multiple attempts for getpwnam", __func__);
603 username = buffer_get_string(m, NULL);
605 pwent = getpwnamallow(username);
607 authctxt->user = xstrdup(username);
608 setproctitle("%s [priv]", pwent ? username : "unknown");
614 buffer_put_char(m, 0);
615 authctxt->pw = fakepw();
620 authctxt->pw = pwent;
623 buffer_put_char(m, 1);
624 buffer_put_string(m, pwent, sizeof(struct passwd));
625 buffer_put_cstring(m, pwent->pw_name);
626 buffer_put_cstring(m, "*");
627 buffer_put_cstring(m, pwent->pw_gecos);
628 #ifdef HAVE_PW_CLASS_IN_PASSWD
629 buffer_put_cstring(m, pwent->pw_class);
631 buffer_put_cstring(m, pwent->pw_dir);
632 buffer_put_cstring(m, pwent->pw_shell);
635 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
636 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
638 /* For SSHv1 allow authentication now */
640 monitor_permit_authentications(1);
642 /* Allow service/style information on the auth context */
643 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
644 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
649 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
651 #ifdef SSH_AUDIT_EVENTS
652 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
658 int mm_answer_auth2_read_banner(int sock, Buffer *m)
663 banner = auth2_read_banner();
664 buffer_put_cstring(m, banner != NULL ? banner : "");
665 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
674 mm_answer_authserv(int sock, Buffer *m)
676 monitor_permit_authentications(1);
678 authctxt->service = buffer_get_string(m, NULL);
679 authctxt->style = buffer_get_string(m, NULL);
680 debug3("%s: service=%s, style=%s",
681 __func__, authctxt->service, authctxt->style);
683 if (strlen(authctxt->style) == 0) {
684 xfree(authctxt->style);
685 authctxt->style = NULL;
692 mm_answer_authpassword(int sock, Buffer *m)
694 static int call_count;
699 passwd = buffer_get_string(m, &plen);
700 /* Only authenticate if the context is valid */
701 authenticated = options.password_authentication &&
702 auth_password(authctxt, passwd);
703 memset(passwd, 0, strlen(passwd));
707 buffer_put_int(m, authenticated);
709 debug3("%s: sending result %d", __func__, authenticated);
710 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
713 if (plen == 0 && call_count == 1)
714 auth_method = "none";
716 auth_method = "password";
718 /* Causes monitor loop to terminate if authenticated */
719 return (authenticated);
724 mm_answer_bsdauthquery(int sock, Buffer *m)
726 char *name, *infotxt;
732 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
733 &prompts, &echo_on) < 0 ? 0 : 1;
736 buffer_put_int(m, success);
738 buffer_put_cstring(m, prompts[0]);
740 debug3("%s: sending challenge success: %u", __func__, success);
741 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
754 mm_answer_bsdauthrespond(int sock, Buffer *m)
759 if (authctxt->as == 0)
760 fatal("%s: no bsd auth session", __func__);
762 response = buffer_get_string(m, NULL);
763 authok = options.challenge_response_authentication &&
764 auth_userresponse(authctxt->as, response, 0);
766 debug3("%s: <%s> = <%d>", __func__, response, authok);
770 buffer_put_int(m, authok);
772 debug3("%s: sending authenticated: %d", __func__, authok);
773 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
775 auth_method = "bsdauth";
777 return (authok != 0);
783 mm_answer_skeyquery(int sock, Buffer *m)
786 char challenge[1024];
789 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
790 sizeof(challenge)) < 0 ? 0 : 1;
793 buffer_put_int(m, success);
795 buffer_put_cstring(m, challenge);
797 debug3("%s: sending challenge success: %u", __func__, success);
798 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
804 mm_answer_skeyrespond(int sock, Buffer *m)
809 response = buffer_get_string(m, NULL);
811 authok = (options.challenge_response_authentication &&
813 skey_haskey(authctxt->pw->pw_name) == 0 &&
814 skey_passcheck(authctxt->pw->pw_name, response) != -1);
819 buffer_put_int(m, authok);
821 debug3("%s: sending authenticated: %d", __func__, authok);
822 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
824 auth_method = "skey";
826 return (authok != 0);
832 mm_answer_pam_start(int sock, Buffer *m)
834 if (!options.use_pam)
835 fatal("UsePAM not set, but ended up in %s anyway", __func__);
839 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
845 mm_answer_pam_account(int sock, Buffer *m)
849 if (!options.use_pam)
850 fatal("UsePAM not set, but ended up in %s anyway", __func__);
852 ret = do_pam_account();
854 buffer_put_int(m, ret);
855 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
857 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
862 static void *sshpam_ctxt, *sshpam_authok;
863 extern KbdintDevice sshpam_device;
866 mm_answer_pam_init_ctx(int sock, Buffer *m)
869 debug3("%s", __func__);
870 authctxt->user = buffer_get_string(m, NULL);
871 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
872 sshpam_authok = NULL;
874 if (sshpam_ctxt != NULL) {
875 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
876 buffer_put_int(m, 1);
878 buffer_put_int(m, 0);
880 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
885 mm_answer_pam_query(int sock, Buffer *m)
887 char *name, *info, **prompts;
888 u_int i, num, *echo_on;
891 debug3("%s", __func__);
892 sshpam_authok = NULL;
893 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
894 if (ret == 0 && num == 0)
895 sshpam_authok = sshpam_ctxt;
896 if (num > 1 || name == NULL || info == NULL)
899 buffer_put_int(m, ret);
900 buffer_put_cstring(m, name);
902 buffer_put_cstring(m, info);
904 buffer_put_int(m, num);
905 for (i = 0; i < num; ++i) {
906 buffer_put_cstring(m, prompts[i]);
908 buffer_put_int(m, echo_on[i]);
914 auth_method = "keyboard-interactive/pam";
915 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
920 mm_answer_pam_respond(int sock, Buffer *m)
926 debug3("%s", __func__);
927 sshpam_authok = NULL;
928 num = buffer_get_int(m);
930 resp = xcalloc(num, sizeof(char *));
931 for (i = 0; i < num; ++i)
932 resp[i] = buffer_get_string(m, NULL);
933 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
934 for (i = 0; i < num; ++i)
938 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
941 buffer_put_int(m, ret);
942 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
943 auth_method = "keyboard-interactive/pam";
945 sshpam_authok = sshpam_ctxt;
950 mm_answer_pam_free_ctx(int sock, Buffer *m)
953 debug3("%s", __func__);
954 (sshpam_device.free_ctx)(sshpam_ctxt);
956 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
957 auth_method = "keyboard-interactive/pam";
958 return (sshpam_authok == sshpam_ctxt);
963 mm_append_debug(Buffer *m)
965 if (auth_debug_init && buffer_len(&auth_debug)) {
966 debug3("%s: Appending debug messages for child", __func__);
967 buffer_append(m, buffer_ptr(&auth_debug),
968 buffer_len(&auth_debug));
969 buffer_clear(&auth_debug);
974 mm_answer_keyallowed(int sock, Buffer *m)
980 enum mm_keytype type = 0;
983 debug3("%s entering", __func__);
985 type = buffer_get_int(m);
986 cuser = buffer_get_string(m, NULL);
987 chost = buffer_get_string(m, NULL);
988 blob = buffer_get_string(m, &bloblen);
990 key = key_from_blob(blob, bloblen);
992 if ((compat20 && type == MM_RSAHOSTKEY) ||
993 (!compat20 && type != MM_RSAHOSTKEY))
994 fatal("%s: key type and protocol mismatch", __func__);
996 debug3("%s: key_from_blob: %p", __func__, key);
998 if (key != NULL && authctxt->valid) {
1001 allowed = options.pubkey_authentication &&
1002 user_key_allowed(authctxt->pw, key);
1003 auth_method = "publickey";
1006 allowed = options.hostbased_authentication &&
1007 hostbased_key_allowed(authctxt->pw,
1009 auth_method = "hostbased";
1012 key->type = KEY_RSA1; /* XXX */
1013 allowed = options.rhosts_rsa_authentication &&
1014 auth_rhosts_rsa_key_allowed(authctxt->pw,
1016 auth_method = "rsa";
1019 fatal("%s: unknown key type %d", __func__, type);
1026 /* clear temporarily storage (used by verify) */
1027 monitor_reset_key_state();
1030 /* Save temporarily for comparison in verify */
1032 key_bloblen = bloblen;
1033 key_blobtype = type;
1034 hostbased_cuser = cuser;
1035 hostbased_chost = chost;
1037 /* Log failed attempt */
1038 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1044 debug3("%s: key %p is %s",
1045 __func__, key, allowed ? "allowed" : "disallowed");
1048 buffer_put_int(m, allowed);
1049 buffer_put_int(m, forced_command != NULL);
1053 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1055 if (type == MM_RSAHOSTKEY)
1056 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1062 monitor_valid_userblob(u_char *data, u_int datalen)
1070 buffer_append(&b, data, datalen);
1072 if (datafellows & SSH_OLD_SESSIONID) {
1074 len = buffer_len(&b);
1075 if ((session_id2 == NULL) ||
1076 (len < session_id2_len) ||
1077 (memcmp(p, session_id2, session_id2_len) != 0))
1079 buffer_consume(&b, session_id2_len);
1081 p = buffer_get_string(&b, &len);
1082 if ((session_id2 == NULL) ||
1083 (len != session_id2_len) ||
1084 (memcmp(p, session_id2, session_id2_len) != 0))
1088 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1090 p = buffer_get_string(&b, NULL);
1091 if (strcmp(authctxt->user, p) != 0) {
1092 logit("wrong user name passed to monitor: expected %s != %.100s",
1097 buffer_skip_string(&b);
1098 if (datafellows & SSH_BUG_PKAUTH) {
1099 if (!buffer_get_char(&b))
1102 p = buffer_get_string(&b, NULL);
1103 if (strcmp("publickey", p) != 0)
1106 if (!buffer_get_char(&b))
1108 buffer_skip_string(&b);
1110 buffer_skip_string(&b);
1111 if (buffer_len(&b) != 0)
1118 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1127 buffer_append(&b, data, datalen);
1129 p = buffer_get_string(&b, &len);
1130 if ((session_id2 == NULL) ||
1131 (len != session_id2_len) ||
1132 (memcmp(p, session_id2, session_id2_len) != 0))
1136 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1138 p = buffer_get_string(&b, NULL);
1139 if (strcmp(authctxt->user, p) != 0) {
1140 logit("wrong user name passed to monitor: expected %s != %.100s",
1145 buffer_skip_string(&b); /* service */
1146 p = buffer_get_string(&b, NULL);
1147 if (strcmp(p, "hostbased") != 0)
1150 buffer_skip_string(&b); /* pkalg */
1151 buffer_skip_string(&b); /* pkblob */
1153 /* verify client host, strip trailing dot if necessary */
1154 p = buffer_get_string(&b, NULL);
1155 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1157 if (strcmp(p, chost) != 0)
1161 /* verify client user */
1162 p = buffer_get_string(&b, NULL);
1163 if (strcmp(p, cuser) != 0)
1167 if (buffer_len(&b) != 0)
1174 mm_answer_keyverify(int sock, Buffer *m)
1177 u_char *signature, *data, *blob;
1178 u_int signaturelen, datalen, bloblen;
1182 blob = buffer_get_string(m, &bloblen);
1183 signature = buffer_get_string(m, &signaturelen);
1184 data = buffer_get_string(m, &datalen);
1186 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1187 !monitor_allowed_key(blob, bloblen))
1188 fatal("%s: bad key, not previously allowed", __func__);
1190 key = key_from_blob(blob, bloblen);
1192 fatal("%s: bad public key blob", __func__);
1194 switch (key_blobtype) {
1196 valid_data = monitor_valid_userblob(data, datalen);
1199 valid_data = monitor_valid_hostbasedblob(data, datalen,
1200 hostbased_cuser, hostbased_chost);
1207 fatal("%s: bad signature data blob", __func__);
1209 verified = key_verify(key, signature, signaturelen, data, datalen);
1210 debug3("%s: key %p signature %s",
1211 __func__, key, verified ? "verified" : "unverified");
1218 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1220 monitor_reset_key_state();
1223 buffer_put_int(m, verified);
1224 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1230 mm_record_login(Session *s, struct passwd *pw)
1233 struct sockaddr_storage from;
1236 * Get IP address of client. If the connection is not a socket, let
1237 * the address be 0.0.0.0.
1239 memset(&from, 0, sizeof(from));
1240 fromlen = sizeof(from);
1241 if (packet_connection_is_on_socket()) {
1242 if (getpeername(packet_get_connection_in(),
1243 (struct sockaddr *)&from, &fromlen) < 0) {
1244 debug("getpeername: %.100s", strerror(errno));
1248 /* Record that there was a login on that tty from the remote host. */
1249 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1250 get_remote_name_or_ip(utmp_len, options.use_dns),
1251 (struct sockaddr *)&from, fromlen);
1255 mm_session_close(Session *s)
1257 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1258 if (s->ttyfd != -1) {
1259 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1260 session_pty_cleanup2(s);
1266 mm_answer_pty(int sock, Buffer *m)
1268 extern struct monitor *pmonitor;
1272 debug3("%s entering", __func__);
1278 s->authctxt = authctxt;
1279 s->pw = authctxt->pw;
1280 s->pid = pmonitor->m_pid;
1281 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1284 pty_setowner(authctxt->pw, s->tty);
1286 buffer_put_int(m, 1);
1287 buffer_put_cstring(m, s->tty);
1289 /* We need to trick ttyslot */
1290 if (dup2(s->ttyfd, 0) == -1)
1291 fatal("%s: dup2", __func__);
1293 mm_record_login(s, authctxt->pw);
1295 /* Now we can close the file descriptor again */
1298 /* send messages generated by record_login */
1299 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1300 buffer_clear(&loginmsg);
1302 mm_request_send(sock, MONITOR_ANS_PTY, m);
1304 mm_send_fd(sock, s->ptyfd);
1305 mm_send_fd(sock, s->ttyfd);
1307 /* make sure nothing uses fd 0 */
1308 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1309 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1311 error("%s: fd0 %d != 0", __func__, fd0);
1313 /* slave is not needed */
1315 s->ttyfd = s->ptyfd;
1316 /* no need to dup() because nobody closes ptyfd */
1317 s->ptymaster = s->ptyfd;
1319 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1325 mm_session_close(s);
1326 buffer_put_int(m, 0);
1327 mm_request_send(sock, MONITOR_ANS_PTY, m);
1332 mm_answer_pty_cleanup(int sock, Buffer *m)
1337 debug3("%s entering", __func__);
1339 tty = buffer_get_string(m, NULL);
1340 if ((s = session_by_tty(tty)) != NULL)
1341 mm_session_close(s);
1348 mm_answer_sesskey(int sock, Buffer *m)
1353 /* Turn off permissions */
1354 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1356 if ((p = BN_new()) == NULL)
1357 fatal("%s: BN_new", __func__);
1359 buffer_get_bignum2(m, p);
1361 rsafail = ssh1_session_key(p);
1364 buffer_put_int(m, rsafail);
1365 buffer_put_bignum2(m, p);
1369 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1371 /* Turn on permissions for sessid passing */
1372 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1378 mm_answer_sessid(int sock, Buffer *m)
1382 debug3("%s entering", __func__);
1384 if (buffer_len(m) != 16)
1385 fatal("%s: bad ssh1 session id", __func__);
1386 for (i = 0; i < 16; i++)
1387 session_id[i] = buffer_get_char(m);
1389 /* Turn on permissions for getpwnam */
1390 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1396 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1400 u_char *blob = NULL;
1404 debug3("%s entering", __func__);
1406 auth_method = "rsa";
1407 if (options.rsa_authentication && authctxt->valid) {
1408 if ((client_n = BN_new()) == NULL)
1409 fatal("%s: BN_new", __func__);
1410 buffer_get_bignum2(m, client_n);
1411 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1412 BN_clear_free(client_n);
1415 buffer_put_int(m, allowed);
1416 buffer_put_int(m, forced_command != NULL);
1418 /* clear temporarily storage (used by generate challenge) */
1419 monitor_reset_key_state();
1421 if (allowed && key != NULL) {
1422 key->type = KEY_RSA; /* cheat for key_to_blob */
1423 if (key_to_blob(key, &blob, &blen) == 0)
1424 fatal("%s: key_to_blob failed", __func__);
1425 buffer_put_string(m, blob, blen);
1427 /* Save temporarily for comparison in verify */
1430 key_blobtype = MM_RSAUSERKEY;
1437 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1439 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1440 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1445 mm_answer_rsa_challenge(int sock, Buffer *m)
1451 debug3("%s entering", __func__);
1453 if (!authctxt->valid)
1454 fatal("%s: authctxt not valid", __func__);
1455 blob = buffer_get_string(m, &blen);
1456 if (!monitor_allowed_key(blob, blen))
1457 fatal("%s: bad key, not previously allowed", __func__);
1458 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1459 fatal("%s: key type mismatch", __func__);
1460 if ((key = key_from_blob(blob, blen)) == NULL)
1461 fatal("%s: received bad key", __func__);
1464 BN_clear_free(ssh1_challenge);
1465 ssh1_challenge = auth_rsa_generate_challenge(key);
1468 buffer_put_bignum2(m, ssh1_challenge);
1470 debug3("%s sending reply", __func__);
1471 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1473 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1481 mm_answer_rsa_response(int sock, Buffer *m)
1484 u_char *blob, *response;
1488 debug3("%s entering", __func__);
1490 if (!authctxt->valid)
1491 fatal("%s: authctxt not valid", __func__);
1492 if (ssh1_challenge == NULL)
1493 fatal("%s: no ssh1_challenge", __func__);
1495 blob = buffer_get_string(m, &blen);
1496 if (!monitor_allowed_key(blob, blen))
1497 fatal("%s: bad key, not previously allowed", __func__);
1498 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1499 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1500 if ((key = key_from_blob(blob, blen)) == NULL)
1501 fatal("%s: received bad key", __func__);
1502 response = buffer_get_string(m, &len);
1504 fatal("%s: received bad response to challenge", __func__);
1505 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1511 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1514 BN_clear_free(ssh1_challenge);
1515 ssh1_challenge = NULL;
1516 monitor_reset_key_state();
1519 buffer_put_int(m, success);
1520 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1526 mm_answer_term(int sock, Buffer *req)
1528 extern struct monitor *pmonitor;
1531 debug3("%s: tearing down sessions", __func__);
1533 /* The child is terminating */
1534 session_destroy_all(&mm_session_close);
1536 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1540 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1542 /* Terminate process */
1546 #ifdef SSH_AUDIT_EVENTS
1547 /* Report that an audit event occurred */
1549 mm_answer_audit_event(int socket, Buffer *m)
1551 ssh_audit_event_t event;
1553 debug3("%s entering", __func__);
1555 event = buffer_get_int(m);
1557 case SSH_AUTH_FAIL_PUBKEY:
1558 case SSH_AUTH_FAIL_HOSTBASED:
1559 case SSH_AUTH_FAIL_GSSAPI:
1560 case SSH_LOGIN_EXCEED_MAXTRIES:
1561 case SSH_LOGIN_ROOT_DENIED:
1562 case SSH_CONNECTION_CLOSE:
1563 case SSH_INVALID_USER:
1567 fatal("Audit event type %d not permitted", event);
1574 mm_answer_audit_command(int socket, Buffer *m)
1579 debug3("%s entering", __func__);
1580 cmd = buffer_get_string(m, &len);
1581 /* sanity check command, if so how? */
1582 audit_run_command(cmd);
1586 #endif /* SSH_AUDIT_EVENTS */
1589 monitor_apply_keystate(struct monitor *pmonitor)
1592 set_newkeys(MODE_IN);
1593 set_newkeys(MODE_OUT);
1595 packet_set_protocol_flags(child_state.ssh1protoflags);
1596 packet_set_encryption_key(child_state.ssh1key,
1597 child_state.ssh1keylen, child_state.ssh1cipher);
1598 xfree(child_state.ssh1key);
1601 /* for rc4 and other stateful ciphers */
1602 packet_set_keycontext(MODE_OUT, child_state.keyout);
1603 xfree(child_state.keyout);
1604 packet_set_keycontext(MODE_IN, child_state.keyin);
1605 xfree(child_state.keyin);
1608 packet_set_iv(MODE_OUT, child_state.ivout);
1609 xfree(child_state.ivout);
1610 packet_set_iv(MODE_IN, child_state.ivin);
1611 xfree(child_state.ivin);
1614 memcpy(&incoming_stream, &child_state.incoming,
1615 sizeof(incoming_stream));
1616 memcpy(&outgoing_stream, &child_state.outgoing,
1617 sizeof(outgoing_stream));
1619 /* Update with new address */
1620 if (options.compression)
1621 mm_init_compression(pmonitor->m_zlib);
1623 /* Network I/O buffers */
1624 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1625 buffer_clear(&input);
1626 buffer_append(&input, child_state.input, child_state.ilen);
1627 memset(child_state.input, 0, child_state.ilen);
1628 xfree(child_state.input);
1630 buffer_clear(&output);
1631 buffer_append(&output, child_state.output, child_state.olen);
1632 memset(child_state.output, 0, child_state.olen);
1633 xfree(child_state.output);
1637 mm_get_kex(Buffer *m)
1643 kex = xcalloc(1, sizeof(*kex));
1644 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1645 if ((session_id2 == NULL) ||
1646 (kex->session_id_len != session_id2_len) ||
1647 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1648 fatal("mm_get_get: internal error: bad session id");
1649 kex->we_need = buffer_get_int(m);
1650 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1651 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1652 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1653 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1655 kex->hostkey_type = buffer_get_int(m);
1656 kex->kex_type = buffer_get_int(m);
1657 blob = buffer_get_string(m, &bloblen);
1658 buffer_init(&kex->my);
1659 buffer_append(&kex->my, blob, bloblen);
1661 blob = buffer_get_string(m, &bloblen);
1662 buffer_init(&kex->peer);
1663 buffer_append(&kex->peer, blob, bloblen);
1666 kex->flags = buffer_get_int(m);
1667 kex->client_version_string = buffer_get_string(m, NULL);
1668 kex->server_version_string = buffer_get_string(m, NULL);
1669 kex->load_host_key=&get_hostkey_by_type;
1670 kex->host_key_index=&get_hostkey_index;
1675 /* This function requries careful sanity checking */
1678 mm_get_keystate(struct monitor *pmonitor)
1682 u_int bloblen, plen;
1683 u_int32_t seqnr, packets;
1686 debug3("%s: Waiting for new keys", __func__);
1689 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1691 child_state.ssh1protoflags = buffer_get_int(&m);
1692 child_state.ssh1cipher = buffer_get_int(&m);
1693 child_state.ssh1key = buffer_get_string(&m,
1694 &child_state.ssh1keylen);
1695 child_state.ivout = buffer_get_string(&m,
1696 &child_state.ivoutlen);
1697 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1700 /* Get the Kex for rekeying */
1701 *pmonitor->m_pkex = mm_get_kex(&m);
1704 blob = buffer_get_string(&m, &bloblen);
1705 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1708 debug3("%s: Waiting for second key", __func__);
1709 blob = buffer_get_string(&m, &bloblen);
1710 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1713 /* Now get sequence numbers for the packets */
1714 seqnr = buffer_get_int(&m);
1715 blocks = buffer_get_int64(&m);
1716 packets = buffer_get_int(&m);
1717 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1718 seqnr = buffer_get_int(&m);
1719 blocks = buffer_get_int64(&m);
1720 packets = buffer_get_int(&m);
1721 packet_set_state(MODE_IN, seqnr, blocks, packets);
1724 /* Get the key context */
1725 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1726 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1728 debug3("%s: Getting compression state", __func__);
1729 /* Get compression state */
1730 p = buffer_get_string(&m, &plen);
1731 if (plen != sizeof(child_state.outgoing))
1732 fatal("%s: bad request size", __func__);
1733 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1736 p = buffer_get_string(&m, &plen);
1737 if (plen != sizeof(child_state.incoming))
1738 fatal("%s: bad request size", __func__);
1739 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1742 /* Network I/O buffers */
1743 debug3("%s: Getting Network I/O buffers", __func__);
1744 child_state.input = buffer_get_string(&m, &child_state.ilen);
1745 child_state.output = buffer_get_string(&m, &child_state.olen);
1751 /* Allocation functions for zlib */
1753 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1755 size_t len = (size_t) size * ncount;
1758 if (len == 0 || ncount > SIZE_T_MAX / size)
1759 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1761 address = mm_malloc(mm, len);
1767 mm_zfree(struct mm_master *mm, void *address)
1769 mm_free(mm, address);
1773 mm_init_compression(struct mm_master *mm)
1775 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1776 outgoing_stream.zfree = (free_func)mm_zfree;
1777 outgoing_stream.opaque = mm;
1779 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1780 incoming_stream.zfree = (free_func)mm_zfree;
1781 incoming_stream.opaque = mm;
1786 #define FD_CLOSEONEXEC(x) do { \
1787 if (fcntl(x, F_SETFD, 1) == -1) \
1788 fatal("fcntl(%d, F_SETFD)", x); \
1792 monitor_socketpair(int *pair)
1794 #ifdef HAVE_SOCKETPAIR
1795 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1796 fatal("%s: socketpair", __func__);
1798 fatal("%s: UsePrivilegeSeparation=yes not supported",
1801 FD_CLOSEONEXEC(pair[0]);
1802 FD_CLOSEONEXEC(pair[1]);
1805 #define MM_MEMSIZE 65536
1810 struct monitor *mon;
1813 mon = xcalloc(1, sizeof(*mon));
1815 monitor_socketpair(pair);
1817 mon->m_recvfd = pair[0];
1818 mon->m_sendfd = pair[1];
1820 /* Used to share zlib space across processes */
1821 if (options.compression) {
1822 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1823 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1825 /* Compression needs to share state across borders */
1826 mm_init_compression(mon->m_zlib);
1833 monitor_reinit(struct monitor *mon)
1837 monitor_socketpair(pair);
1839 mon->m_recvfd = pair[0];
1840 mon->m_sendfd = pair[1];
1845 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1851 goid.elements = buffer_get_string(m, &len);
1854 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1856 xfree(goid.elements);
1859 buffer_put_int(m, major);
1861 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1863 /* Now we have a context, enable the step */
1864 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1870 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1873 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1874 OM_uint32 major, minor;
1875 OM_uint32 flags = 0; /* GSI needs this */
1878 in.value = buffer_get_string(m, &len);
1880 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1884 buffer_put_int(m, major);
1885 buffer_put_string(m, out.value, out.length);
1886 buffer_put_int(m, flags);
1887 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1889 gss_release_buffer(&minor, &out);
1891 if (major == GSS_S_COMPLETE) {
1892 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1893 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1894 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1900 mm_answer_gss_checkmic(int sock, Buffer *m)
1902 gss_buffer_desc gssbuf, mic;
1906 gssbuf.value = buffer_get_string(m, &len);
1907 gssbuf.length = len;
1908 mic.value = buffer_get_string(m, &len);
1911 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1913 xfree(gssbuf.value);
1917 buffer_put_int(m, ret);
1919 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1921 if (!GSS_ERROR(ret))
1922 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1928 mm_answer_gss_userok(int sock, Buffer *m)
1932 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1935 buffer_put_int(m, authenticated);
1937 debug3("%s: sending result %d", __func__, authenticated);
1938 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1940 auth_method = "gssapi-with-mic";
1942 /* Monitor loop will terminate if authenticated */
1943 return (authenticated);