1 /* $OpenBSD: monitor.c,v 1.80 2006/07/09 15:15:10 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>
45 #include <openssl/dh.h>
51 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
54 #define TARGET_OS_MAC 1
59 #include "auth-options.h"
68 #include "monitor_mm.h"
69 #include "monitor_wrap.h"
70 #include "monitor_fdpass.h"
80 static Gssctxt *gsscontext = NULL;
84 extern ServerOptions options;
85 extern u_int utmp_len;
86 extern Newkeys *current_keys[];
87 extern z_stream incoming_stream;
88 extern z_stream outgoing_stream;
89 extern u_char session_id[];
90 extern Buffer input, output;
91 extern Buffer auth_debug;
92 extern int auth_debug_init;
93 extern Buffer loginmsg;
95 /* State exported from the child */
118 /* Functions on the monitor that answer unprivileged requests */
120 int mm_answer_moduli(int, Buffer *);
121 int mm_answer_sign(int, Buffer *);
122 int mm_answer_pwnamallow(int, Buffer *);
123 int mm_answer_auth2_read_banner(int, Buffer *);
124 int mm_answer_authserv(int, Buffer *);
125 int mm_answer_authpassword(int, Buffer *);
126 int mm_answer_bsdauthquery(int, Buffer *);
127 int mm_answer_bsdauthrespond(int, Buffer *);
128 int mm_answer_skeyquery(int, Buffer *);
129 int mm_answer_skeyrespond(int, Buffer *);
130 int mm_answer_keyallowed(int, Buffer *);
131 int mm_answer_keyverify(int, Buffer *);
132 int mm_answer_pty(int, Buffer *);
133 int mm_answer_pty_cleanup(int, Buffer *);
134 int mm_answer_term(int, Buffer *);
135 int mm_answer_rsa_keyallowed(int, Buffer *);
136 int mm_answer_rsa_challenge(int, Buffer *);
137 int mm_answer_rsa_response(int, Buffer *);
138 int mm_answer_sesskey(int, Buffer *);
139 int mm_answer_sessid(int, Buffer *);
142 int mm_answer_pam_start(int, Buffer *);
143 int mm_answer_pam_account(int, Buffer *);
144 int mm_answer_pam_init_ctx(int, Buffer *);
145 int mm_answer_pam_query(int, Buffer *);
146 int mm_answer_pam_respond(int, Buffer *);
147 int mm_answer_pam_free_ctx(int, Buffer *);
151 int mm_answer_gss_setup_ctx(int, Buffer *);
152 int mm_answer_gss_accept_ctx(int, Buffer *);
153 int mm_answer_gss_userok(int, Buffer *);
154 int mm_answer_gss_checkmic(int, Buffer *);
157 #ifdef SSH_AUDIT_EVENTS
158 int mm_answer_audit_event(int, Buffer *);
159 int mm_answer_audit_command(int, Buffer *);
162 static Authctxt *authctxt;
163 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
165 /* local state for key verify */
166 static u_char *key_blob = NULL;
167 static u_int key_bloblen = 0;
168 static int key_blobtype = MM_NOKEY;
169 static char *hostbased_cuser = NULL;
170 static char *hostbased_chost = NULL;
171 static char *auth_method = "unknown";
172 static u_int session_id2_len = 0;
173 static u_char *session_id2 = NULL;
174 static pid_t monitor_child_pid;
177 enum monitor_reqtype type;
179 int (*f)(int, Buffer *);
182 #define MON_ISAUTH 0x0004 /* Required for Authentication */
183 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
184 #define MON_ONCE 0x0010 /* Disable after calling */
185 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
187 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
189 #define MON_PERMIT 0x1000 /* Request is permitted */
191 struct mon_table mon_dispatch_proto20[] = {
192 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
193 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
194 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
195 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
196 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
197 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
199 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
200 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
201 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
202 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
203 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
204 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
206 #ifdef SSH_AUDIT_EVENTS
207 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
210 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
211 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
214 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
215 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
217 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
218 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
220 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
221 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
222 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
223 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
228 struct mon_table mon_dispatch_postauth20[] = {
229 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
230 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
231 {MONITOR_REQ_PTY, 0, mm_answer_pty},
232 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
233 {MONITOR_REQ_TERM, 0, mm_answer_term},
234 #ifdef SSH_AUDIT_EVENTS
235 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
236 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
241 struct mon_table mon_dispatch_proto15[] = {
242 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
243 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
244 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
245 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
246 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
247 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
248 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
249 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
251 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
252 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
255 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
256 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
259 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
260 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
261 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
262 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
263 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
264 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
266 #ifdef SSH_AUDIT_EVENTS
267 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
272 struct mon_table mon_dispatch_postauth15[] = {
273 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
274 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
275 {MONITOR_REQ_TERM, 0, mm_answer_term},
276 #ifdef SSH_AUDIT_EVENTS
277 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
278 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
283 struct mon_table *mon_dispatch;
285 /* Specifies if a certain message is allowed at the moment */
288 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
290 while (ent->f != NULL) {
291 if (ent->type == type) {
292 ent->flags &= ~MON_PERMIT;
293 ent->flags |= permit ? MON_PERMIT : 0;
301 monitor_permit_authentications(int permit)
303 struct mon_table *ent = mon_dispatch;
305 while (ent->f != NULL) {
306 if (ent->flags & MON_AUTH) {
307 ent->flags &= ~MON_PERMIT;
308 ent->flags |= permit ? MON_PERMIT : 0;
315 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
317 struct mon_table *ent;
318 int authenticated = 0;
320 debug3("preauth child monitor started");
322 authctxt = _authctxt;
323 memset(authctxt, 0, sizeof(*authctxt));
325 authctxt->loginmsg = &loginmsg;
328 mon_dispatch = mon_dispatch_proto20;
330 /* Permit requests for moduli and signatures */
331 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
332 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
334 mon_dispatch = mon_dispatch_proto15;
336 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
339 /* The first few requests do not require asynchronous access */
340 while (!authenticated) {
341 auth_method = "unknown";
342 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
344 if (!(ent->flags & MON_AUTHDECIDE))
345 fatal("%s: unexpected authentication from %d",
346 __func__, ent->type);
347 if (authctxt->pw->pw_uid == 0 &&
348 !auth_root_allowed(auth_method))
351 /* PAM needs to perform account checks after auth */
352 if (options.use_pam && authenticated) {
356 mm_request_receive_expect(pmonitor->m_sendfd,
357 MONITOR_REQ_PAM_ACCOUNT, &m);
358 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
364 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
365 auth_log(authctxt, authenticated, auth_method,
366 compat20 ? " ssh2" : "");
368 authctxt->failures++;
372 if (!authctxt->valid)
373 fatal("%s: authenticated invalid user", __func__);
374 if (strcmp(auth_method, "unknown") == 0)
375 fatal("%s: authentication method name unknown", __func__);
377 debug("%s: %s has been authenticated by privileged process",
378 __func__, authctxt->user);
380 mm_get_keystate(pmonitor);
384 monitor_set_child_handler(pid_t pid)
386 monitor_child_pid = pid;
390 monitor_child_handler(int sig)
392 kill(monitor_child_pid, sig);
396 monitor_child_postauth(struct monitor *pmonitor)
398 monitor_set_child_handler(pmonitor->m_pid);
399 signal(SIGHUP, &monitor_child_handler);
400 signal(SIGTERM, &monitor_child_handler);
403 mon_dispatch = mon_dispatch_postauth20;
405 /* Permit requests for moduli and signatures */
406 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
407 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
408 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
410 mon_dispatch = mon_dispatch_postauth15;
411 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
414 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
415 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
419 monitor_read(pmonitor, mon_dispatch, NULL);
423 monitor_sync(struct monitor *pmonitor)
425 if (options.compression) {
426 /* The member allocation is not visible, so sync it */
427 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
432 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
433 struct mon_table **pent)
441 mm_request_receive(pmonitor->m_sendfd, &m);
442 type = buffer_get_char(&m);
444 debug3("%s: checking request %d", __func__, type);
446 while (ent->f != NULL) {
447 if (ent->type == type)
452 if (ent->f != NULL) {
453 if (!(ent->flags & MON_PERMIT))
454 fatal("%s: unpermitted request %d", __func__,
456 ret = (*ent->f)(pmonitor->m_sendfd, &m);
459 /* The child may use this request only once, disable it */
460 if (ent->flags & MON_ONCE) {
461 debug2("%s: %d used once, disabling now", __func__,
463 ent->flags &= ~MON_PERMIT;
472 fatal("%s: unsupported request: %d", __func__, type);
478 /* allowed key state */
480 monitor_allowed_key(u_char *blob, u_int bloblen)
482 /* make sure key is allowed */
483 if (key_blob == NULL || key_bloblen != bloblen ||
484 memcmp(key_blob, blob, key_bloblen))
490 monitor_reset_key_state(void)
493 if (key_blob != NULL)
495 if (hostbased_cuser != NULL)
496 xfree(hostbased_cuser);
497 if (hostbased_chost != NULL)
498 xfree(hostbased_chost);
501 key_blobtype = MM_NOKEY;
502 hostbased_cuser = NULL;
503 hostbased_chost = NULL;
507 mm_answer_moduli(int sock, Buffer *m)
512 min = buffer_get_int(m);
513 want = buffer_get_int(m);
514 max = buffer_get_int(m);
516 debug3("%s: got parameters: %d %d %d",
517 __func__, min, want, max);
518 /* We need to check here, too, in case the child got corrupted */
519 if (max < min || want < min || max < want)
520 fatal("%s: bad parameters: %d %d %d",
521 __func__, min, want, max);
525 dh = choose_dh(min, want, max);
527 buffer_put_char(m, 0);
530 /* Send first bignum */
531 buffer_put_char(m, 1);
532 buffer_put_bignum2(m, dh->p);
533 buffer_put_bignum2(m, dh->g);
537 mm_request_send(sock, MONITOR_ANS_MODULI, m);
542 mm_answer_sign(int sock, Buffer *m)
547 u_int siglen, datlen;
550 debug3("%s", __func__);
552 keyid = buffer_get_int(m);
553 p = buffer_get_string(m, &datlen);
556 * Supported KEX types will only return SHA1 (20 byte) or
557 * SHA256 (32 byte) hashes
559 if (datlen != 20 && datlen != 32)
560 fatal("%s: data length incorrect: %u", __func__, datlen);
562 /* save session id, it will be passed on the first call */
563 if (session_id2_len == 0) {
564 session_id2_len = datlen;
565 session_id2 = xmalloc(session_id2_len);
566 memcpy(session_id2, p, session_id2_len);
569 if ((key = get_hostkey_by_index(keyid)) == NULL)
570 fatal("%s: no hostkey from index %d", __func__, keyid);
571 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
572 fatal("%s: key_sign failed", __func__);
574 debug3("%s: signature %p(%u)", __func__, signature, siglen);
577 buffer_put_string(m, signature, siglen);
582 mm_request_send(sock, MONITOR_ANS_SIGN, m);
584 /* Turn on permissions for getpwnam */
585 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
590 /* Retrieves the password entry and also checks if the user is permitted */
593 mm_answer_pwnamallow(int sock, Buffer *m)
596 struct passwd *pwent;
599 debug3("%s", __func__);
601 if (authctxt->attempt++ != 0)
602 fatal("%s: multiple attempts for getpwnam", __func__);
604 username = buffer_get_string(m, NULL);
606 pwent = getpwnamallow(username);
608 authctxt->user = xstrdup(username);
609 setproctitle("%s [priv]", pwent ? username : "unknown");
615 buffer_put_char(m, 0);
616 authctxt->pw = fakepw();
621 authctxt->pw = pwent;
624 buffer_put_char(m, 1);
625 buffer_put_string(m, pwent, sizeof(struct passwd));
626 buffer_put_cstring(m, pwent->pw_name);
627 buffer_put_cstring(m, "*");
628 buffer_put_cstring(m, pwent->pw_gecos);
629 #ifdef HAVE_PW_CLASS_IN_PASSWD
630 buffer_put_cstring(m, pwent->pw_class);
632 buffer_put_cstring(m, pwent->pw_dir);
633 buffer_put_cstring(m, pwent->pw_shell);
636 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
637 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
639 /* For SSHv1 allow authentication now */
641 monitor_permit_authentications(1);
643 /* Allow service/style information on the auth context */
644 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
645 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
650 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
652 #ifdef SSH_AUDIT_EVENTS
653 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
659 int mm_answer_auth2_read_banner(int sock, Buffer *m)
664 banner = auth2_read_banner();
665 buffer_put_cstring(m, banner != NULL ? banner : "");
666 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
675 mm_answer_authserv(int sock, Buffer *m)
677 monitor_permit_authentications(1);
679 authctxt->service = buffer_get_string(m, NULL);
680 authctxt->style = buffer_get_string(m, NULL);
681 debug3("%s: service=%s, style=%s",
682 __func__, authctxt->service, authctxt->style);
684 if (strlen(authctxt->style) == 0) {
685 xfree(authctxt->style);
686 authctxt->style = NULL;
693 mm_answer_authpassword(int sock, Buffer *m)
695 static int call_count;
700 passwd = buffer_get_string(m, &plen);
701 /* Only authenticate if the context is valid */
702 authenticated = options.password_authentication &&
703 auth_password(authctxt, passwd);
704 memset(passwd, 0, strlen(passwd));
708 buffer_put_int(m, authenticated);
710 debug3("%s: sending result %d", __func__, authenticated);
711 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
714 if (plen == 0 && call_count == 1)
715 auth_method = "none";
717 auth_method = "password";
719 /* Causes monitor loop to terminate if authenticated */
720 return (authenticated);
725 mm_answer_bsdauthquery(int sock, Buffer *m)
727 char *name, *infotxt;
733 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
734 &prompts, &echo_on) < 0 ? 0 : 1;
737 buffer_put_int(m, success);
739 buffer_put_cstring(m, prompts[0]);
741 debug3("%s: sending challenge success: %u", __func__, success);
742 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
755 mm_answer_bsdauthrespond(int sock, Buffer *m)
760 if (authctxt->as == 0)
761 fatal("%s: no bsd auth session", __func__);
763 response = buffer_get_string(m, NULL);
764 authok = options.challenge_response_authentication &&
765 auth_userresponse(authctxt->as, response, 0);
767 debug3("%s: <%s> = <%d>", __func__, response, authok);
771 buffer_put_int(m, authok);
773 debug3("%s: sending authenticated: %d", __func__, authok);
774 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
776 auth_method = "bsdauth";
778 return (authok != 0);
784 mm_answer_skeyquery(int sock, Buffer *m)
787 char challenge[1024];
790 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
791 sizeof(challenge)) < 0 ? 0 : 1;
794 buffer_put_int(m, success);
796 buffer_put_cstring(m, challenge);
798 debug3("%s: sending challenge success: %u", __func__, success);
799 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
805 mm_answer_skeyrespond(int sock, Buffer *m)
810 response = buffer_get_string(m, NULL);
812 authok = (options.challenge_response_authentication &&
814 skey_haskey(authctxt->pw->pw_name) == 0 &&
815 skey_passcheck(authctxt->pw->pw_name, response) != -1);
820 buffer_put_int(m, authok);
822 debug3("%s: sending authenticated: %d", __func__, authok);
823 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
825 auth_method = "skey";
827 return (authok != 0);
833 mm_answer_pam_start(int sock, Buffer *m)
835 if (!options.use_pam)
836 fatal("UsePAM not set, but ended up in %s anyway", __func__);
840 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
846 mm_answer_pam_account(int sock, Buffer *m)
850 if (!options.use_pam)
851 fatal("UsePAM not set, but ended up in %s anyway", __func__);
853 ret = do_pam_account();
855 buffer_put_int(m, ret);
856 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
858 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
863 static void *sshpam_ctxt, *sshpam_authok;
864 extern KbdintDevice sshpam_device;
867 mm_answer_pam_init_ctx(int sock, Buffer *m)
870 debug3("%s", __func__);
871 authctxt->user = buffer_get_string(m, NULL);
872 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
873 sshpam_authok = NULL;
875 if (sshpam_ctxt != NULL) {
876 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
877 buffer_put_int(m, 1);
879 buffer_put_int(m, 0);
881 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
886 mm_answer_pam_query(int sock, Buffer *m)
888 char *name, *info, **prompts;
889 u_int i, num, *echo_on;
892 debug3("%s", __func__);
893 sshpam_authok = NULL;
894 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
895 if (ret == 0 && num == 0)
896 sshpam_authok = sshpam_ctxt;
897 if (num > 1 || name == NULL || info == NULL)
900 buffer_put_int(m, ret);
901 buffer_put_cstring(m, name);
903 buffer_put_cstring(m, info);
905 buffer_put_int(m, num);
906 for (i = 0; i < num; ++i) {
907 buffer_put_cstring(m, prompts[i]);
909 buffer_put_int(m, echo_on[i]);
915 auth_method = "keyboard-interactive/pam";
916 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
921 mm_answer_pam_respond(int sock, Buffer *m)
927 debug3("%s", __func__);
928 sshpam_authok = NULL;
929 num = buffer_get_int(m);
931 resp = xcalloc(num, sizeof(char *));
932 for (i = 0; i < num; ++i)
933 resp[i] = buffer_get_string(m, NULL);
934 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
935 for (i = 0; i < num; ++i)
939 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
942 buffer_put_int(m, ret);
943 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
944 auth_method = "keyboard-interactive/pam";
946 sshpam_authok = sshpam_ctxt;
951 mm_answer_pam_free_ctx(int sock, Buffer *m)
954 debug3("%s", __func__);
955 (sshpam_device.free_ctx)(sshpam_ctxt);
957 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
958 auth_method = "keyboard-interactive/pam";
959 return (sshpam_authok == sshpam_ctxt);
964 mm_append_debug(Buffer *m)
966 if (auth_debug_init && buffer_len(&auth_debug)) {
967 debug3("%s: Appending debug messages for child", __func__);
968 buffer_append(m, buffer_ptr(&auth_debug),
969 buffer_len(&auth_debug));
970 buffer_clear(&auth_debug);
975 mm_answer_keyallowed(int sock, Buffer *m)
981 enum mm_keytype type = 0;
984 debug3("%s entering", __func__);
986 type = buffer_get_int(m);
987 cuser = buffer_get_string(m, NULL);
988 chost = buffer_get_string(m, NULL);
989 blob = buffer_get_string(m, &bloblen);
991 key = key_from_blob(blob, bloblen);
993 if ((compat20 && type == MM_RSAHOSTKEY) ||
994 (!compat20 && type != MM_RSAHOSTKEY))
995 fatal("%s: key type and protocol mismatch", __func__);
997 debug3("%s: key_from_blob: %p", __func__, key);
999 if (key != NULL && authctxt->valid) {
1002 allowed = options.pubkey_authentication &&
1003 user_key_allowed(authctxt->pw, key);
1004 auth_method = "publickey";
1007 allowed = options.hostbased_authentication &&
1008 hostbased_key_allowed(authctxt->pw,
1010 auth_method = "hostbased";
1013 key->type = KEY_RSA1; /* XXX */
1014 allowed = options.rhosts_rsa_authentication &&
1015 auth_rhosts_rsa_key_allowed(authctxt->pw,
1017 auth_method = "rsa";
1020 fatal("%s: unknown key type %d", __func__, type);
1027 /* clear temporarily storage (used by verify) */
1028 monitor_reset_key_state();
1031 /* Save temporarily for comparison in verify */
1033 key_bloblen = bloblen;
1034 key_blobtype = type;
1035 hostbased_cuser = cuser;
1036 hostbased_chost = chost;
1038 /* Log failed attempt */
1039 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1045 debug3("%s: key %p is %s",
1046 __func__, key, allowed ? "allowed" : "disallowed");
1049 buffer_put_int(m, allowed);
1050 buffer_put_int(m, forced_command != NULL);
1054 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1056 if (type == MM_RSAHOSTKEY)
1057 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1063 monitor_valid_userblob(u_char *data, u_int datalen)
1071 buffer_append(&b, data, datalen);
1073 if (datafellows & SSH_OLD_SESSIONID) {
1075 len = buffer_len(&b);
1076 if ((session_id2 == NULL) ||
1077 (len < session_id2_len) ||
1078 (memcmp(p, session_id2, session_id2_len) != 0))
1080 buffer_consume(&b, session_id2_len);
1082 p = buffer_get_string(&b, &len);
1083 if ((session_id2 == NULL) ||
1084 (len != session_id2_len) ||
1085 (memcmp(p, session_id2, session_id2_len) != 0))
1089 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1091 p = buffer_get_string(&b, NULL);
1092 if (strcmp(authctxt->user, p) != 0) {
1093 logit("wrong user name passed to monitor: expected %s != %.100s",
1098 buffer_skip_string(&b);
1099 if (datafellows & SSH_BUG_PKAUTH) {
1100 if (!buffer_get_char(&b))
1103 p = buffer_get_string(&b, NULL);
1104 if (strcmp("publickey", p) != 0)
1107 if (!buffer_get_char(&b))
1109 buffer_skip_string(&b);
1111 buffer_skip_string(&b);
1112 if (buffer_len(&b) != 0)
1119 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1128 buffer_append(&b, data, datalen);
1130 p = buffer_get_string(&b, &len);
1131 if ((session_id2 == NULL) ||
1132 (len != session_id2_len) ||
1133 (memcmp(p, session_id2, session_id2_len) != 0))
1137 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1139 p = buffer_get_string(&b, NULL);
1140 if (strcmp(authctxt->user, p) != 0) {
1141 logit("wrong user name passed to monitor: expected %s != %.100s",
1146 buffer_skip_string(&b); /* service */
1147 p = buffer_get_string(&b, NULL);
1148 if (strcmp(p, "hostbased") != 0)
1151 buffer_skip_string(&b); /* pkalg */
1152 buffer_skip_string(&b); /* pkblob */
1154 /* verify client host, strip trailing dot if necessary */
1155 p = buffer_get_string(&b, NULL);
1156 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1158 if (strcmp(p, chost) != 0)
1162 /* verify client user */
1163 p = buffer_get_string(&b, NULL);
1164 if (strcmp(p, cuser) != 0)
1168 if (buffer_len(&b) != 0)
1175 mm_answer_keyverify(int sock, Buffer *m)
1178 u_char *signature, *data, *blob;
1179 u_int signaturelen, datalen, bloblen;
1183 blob = buffer_get_string(m, &bloblen);
1184 signature = buffer_get_string(m, &signaturelen);
1185 data = buffer_get_string(m, &datalen);
1187 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1188 !monitor_allowed_key(blob, bloblen))
1189 fatal("%s: bad key, not previously allowed", __func__);
1191 key = key_from_blob(blob, bloblen);
1193 fatal("%s: bad public key blob", __func__);
1195 switch (key_blobtype) {
1197 valid_data = monitor_valid_userblob(data, datalen);
1200 valid_data = monitor_valid_hostbasedblob(data, datalen,
1201 hostbased_cuser, hostbased_chost);
1208 fatal("%s: bad signature data blob", __func__);
1210 verified = key_verify(key, signature, signaturelen, data, datalen);
1211 debug3("%s: key %p signature %s",
1212 __func__, key, verified ? "verified" : "unverified");
1219 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1221 monitor_reset_key_state();
1224 buffer_put_int(m, verified);
1225 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1231 mm_record_login(Session *s, struct passwd *pw)
1234 struct sockaddr_storage from;
1237 * Get IP address of client. If the connection is not a socket, let
1238 * the address be 0.0.0.0.
1240 memset(&from, 0, sizeof(from));
1241 fromlen = sizeof(from);
1242 if (packet_connection_is_on_socket()) {
1243 if (getpeername(packet_get_connection_in(),
1244 (struct sockaddr *)&from, &fromlen) < 0) {
1245 debug("getpeername: %.100s", strerror(errno));
1249 /* Record that there was a login on that tty from the remote host. */
1250 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1251 get_remote_name_or_ip(utmp_len, options.use_dns),
1252 (struct sockaddr *)&from, fromlen);
1256 mm_session_close(Session *s)
1258 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1259 if (s->ttyfd != -1) {
1260 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1261 session_pty_cleanup2(s);
1267 mm_answer_pty(int sock, Buffer *m)
1269 extern struct monitor *pmonitor;
1273 debug3("%s entering", __func__);
1279 s->authctxt = authctxt;
1280 s->pw = authctxt->pw;
1281 s->pid = pmonitor->m_pid;
1282 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1285 pty_setowner(authctxt->pw, s->tty);
1287 buffer_put_int(m, 1);
1288 buffer_put_cstring(m, s->tty);
1290 /* We need to trick ttyslot */
1291 if (dup2(s->ttyfd, 0) == -1)
1292 fatal("%s: dup2", __func__);
1294 mm_record_login(s, authctxt->pw);
1296 /* Now we can close the file descriptor again */
1299 /* send messages generated by record_login */
1300 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1301 buffer_clear(&loginmsg);
1303 mm_request_send(sock, MONITOR_ANS_PTY, m);
1305 mm_send_fd(sock, s->ptyfd);
1306 mm_send_fd(sock, s->ttyfd);
1308 /* make sure nothing uses fd 0 */
1309 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1310 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1312 error("%s: fd0 %d != 0", __func__, fd0);
1314 /* slave is not needed */
1316 s->ttyfd = s->ptyfd;
1317 /* no need to dup() because nobody closes ptyfd */
1318 s->ptymaster = s->ptyfd;
1320 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1326 mm_session_close(s);
1327 buffer_put_int(m, 0);
1328 mm_request_send(sock, MONITOR_ANS_PTY, m);
1333 mm_answer_pty_cleanup(int sock, Buffer *m)
1338 debug3("%s entering", __func__);
1340 tty = buffer_get_string(m, NULL);
1341 if ((s = session_by_tty(tty)) != NULL)
1342 mm_session_close(s);
1349 mm_answer_sesskey(int sock, Buffer *m)
1354 /* Turn off permissions */
1355 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1357 if ((p = BN_new()) == NULL)
1358 fatal("%s: BN_new", __func__);
1360 buffer_get_bignum2(m, p);
1362 rsafail = ssh1_session_key(p);
1365 buffer_put_int(m, rsafail);
1366 buffer_put_bignum2(m, p);
1370 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1372 /* Turn on permissions for sessid passing */
1373 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1379 mm_answer_sessid(int sock, Buffer *m)
1383 debug3("%s entering", __func__);
1385 if (buffer_len(m) != 16)
1386 fatal("%s: bad ssh1 session id", __func__);
1387 for (i = 0; i < 16; i++)
1388 session_id[i] = buffer_get_char(m);
1390 /* Turn on permissions for getpwnam */
1391 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1397 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1401 u_char *blob = NULL;
1405 debug3("%s entering", __func__);
1407 auth_method = "rsa";
1408 if (options.rsa_authentication && authctxt->valid) {
1409 if ((client_n = BN_new()) == NULL)
1410 fatal("%s: BN_new", __func__);
1411 buffer_get_bignum2(m, client_n);
1412 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1413 BN_clear_free(client_n);
1416 buffer_put_int(m, allowed);
1417 buffer_put_int(m, forced_command != NULL);
1419 /* clear temporarily storage (used by generate challenge) */
1420 monitor_reset_key_state();
1422 if (allowed && key != NULL) {
1423 key->type = KEY_RSA; /* cheat for key_to_blob */
1424 if (key_to_blob(key, &blob, &blen) == 0)
1425 fatal("%s: key_to_blob failed", __func__);
1426 buffer_put_string(m, blob, blen);
1428 /* Save temporarily for comparison in verify */
1431 key_blobtype = MM_RSAUSERKEY;
1438 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1440 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1441 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1446 mm_answer_rsa_challenge(int sock, Buffer *m)
1452 debug3("%s entering", __func__);
1454 if (!authctxt->valid)
1455 fatal("%s: authctxt not valid", __func__);
1456 blob = buffer_get_string(m, &blen);
1457 if (!monitor_allowed_key(blob, blen))
1458 fatal("%s: bad key, not previously allowed", __func__);
1459 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1460 fatal("%s: key type mismatch", __func__);
1461 if ((key = key_from_blob(blob, blen)) == NULL)
1462 fatal("%s: received bad key", __func__);
1465 BN_clear_free(ssh1_challenge);
1466 ssh1_challenge = auth_rsa_generate_challenge(key);
1469 buffer_put_bignum2(m, ssh1_challenge);
1471 debug3("%s sending reply", __func__);
1472 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1474 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1482 mm_answer_rsa_response(int sock, Buffer *m)
1485 u_char *blob, *response;
1489 debug3("%s entering", __func__);
1491 if (!authctxt->valid)
1492 fatal("%s: authctxt not valid", __func__);
1493 if (ssh1_challenge == NULL)
1494 fatal("%s: no ssh1_challenge", __func__);
1496 blob = buffer_get_string(m, &blen);
1497 if (!monitor_allowed_key(blob, blen))
1498 fatal("%s: bad key, not previously allowed", __func__);
1499 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1500 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1501 if ((key = key_from_blob(blob, blen)) == NULL)
1502 fatal("%s: received bad key", __func__);
1503 response = buffer_get_string(m, &len);
1505 fatal("%s: received bad response to challenge", __func__);
1506 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1512 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1515 BN_clear_free(ssh1_challenge);
1516 ssh1_challenge = NULL;
1517 monitor_reset_key_state();
1520 buffer_put_int(m, success);
1521 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1527 mm_answer_term(int sock, Buffer *req)
1529 extern struct monitor *pmonitor;
1532 debug3("%s: tearing down sessions", __func__);
1534 /* The child is terminating */
1535 session_destroy_all(&mm_session_close);
1537 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1541 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1543 /* Terminate process */
1547 #ifdef SSH_AUDIT_EVENTS
1548 /* Report that an audit event occurred */
1550 mm_answer_audit_event(int socket, Buffer *m)
1552 ssh_audit_event_t event;
1554 debug3("%s entering", __func__);
1556 event = buffer_get_int(m);
1558 case SSH_AUTH_FAIL_PUBKEY:
1559 case SSH_AUTH_FAIL_HOSTBASED:
1560 case SSH_AUTH_FAIL_GSSAPI:
1561 case SSH_LOGIN_EXCEED_MAXTRIES:
1562 case SSH_LOGIN_ROOT_DENIED:
1563 case SSH_CONNECTION_CLOSE:
1564 case SSH_INVALID_USER:
1568 fatal("Audit event type %d not permitted", event);
1575 mm_answer_audit_command(int socket, Buffer *m)
1580 debug3("%s entering", __func__);
1581 cmd = buffer_get_string(m, &len);
1582 /* sanity check command, if so how? */
1583 audit_run_command(cmd);
1587 #endif /* SSH_AUDIT_EVENTS */
1590 monitor_apply_keystate(struct monitor *pmonitor)
1593 set_newkeys(MODE_IN);
1594 set_newkeys(MODE_OUT);
1596 packet_set_protocol_flags(child_state.ssh1protoflags);
1597 packet_set_encryption_key(child_state.ssh1key,
1598 child_state.ssh1keylen, child_state.ssh1cipher);
1599 xfree(child_state.ssh1key);
1602 /* for rc4 and other stateful ciphers */
1603 packet_set_keycontext(MODE_OUT, child_state.keyout);
1604 xfree(child_state.keyout);
1605 packet_set_keycontext(MODE_IN, child_state.keyin);
1606 xfree(child_state.keyin);
1609 packet_set_iv(MODE_OUT, child_state.ivout);
1610 xfree(child_state.ivout);
1611 packet_set_iv(MODE_IN, child_state.ivin);
1612 xfree(child_state.ivin);
1615 memcpy(&incoming_stream, &child_state.incoming,
1616 sizeof(incoming_stream));
1617 memcpy(&outgoing_stream, &child_state.outgoing,
1618 sizeof(outgoing_stream));
1620 /* Update with new address */
1621 if (options.compression)
1622 mm_init_compression(pmonitor->m_zlib);
1624 /* Network I/O buffers */
1625 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1626 buffer_clear(&input);
1627 buffer_append(&input, child_state.input, child_state.ilen);
1628 memset(child_state.input, 0, child_state.ilen);
1629 xfree(child_state.input);
1631 buffer_clear(&output);
1632 buffer_append(&output, child_state.output, child_state.olen);
1633 memset(child_state.output, 0, child_state.olen);
1634 xfree(child_state.output);
1638 mm_get_kex(Buffer *m)
1644 kex = xcalloc(1, sizeof(*kex));
1645 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1646 if ((session_id2 == NULL) ||
1647 (kex->session_id_len != session_id2_len) ||
1648 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1649 fatal("mm_get_get: internal error: bad session id");
1650 kex->we_need = buffer_get_int(m);
1651 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1652 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1653 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1654 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1656 kex->hostkey_type = buffer_get_int(m);
1657 kex->kex_type = buffer_get_int(m);
1658 blob = buffer_get_string(m, &bloblen);
1659 buffer_init(&kex->my);
1660 buffer_append(&kex->my, blob, bloblen);
1662 blob = buffer_get_string(m, &bloblen);
1663 buffer_init(&kex->peer);
1664 buffer_append(&kex->peer, blob, bloblen);
1667 kex->flags = buffer_get_int(m);
1668 kex->client_version_string = buffer_get_string(m, NULL);
1669 kex->server_version_string = buffer_get_string(m, NULL);
1670 kex->load_host_key=&get_hostkey_by_type;
1671 kex->host_key_index=&get_hostkey_index;
1676 /* This function requries careful sanity checking */
1679 mm_get_keystate(struct monitor *pmonitor)
1683 u_int bloblen, plen;
1684 u_int32_t seqnr, packets;
1687 debug3("%s: Waiting for new keys", __func__);
1690 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1692 child_state.ssh1protoflags = buffer_get_int(&m);
1693 child_state.ssh1cipher = buffer_get_int(&m);
1694 child_state.ssh1key = buffer_get_string(&m,
1695 &child_state.ssh1keylen);
1696 child_state.ivout = buffer_get_string(&m,
1697 &child_state.ivoutlen);
1698 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1701 /* Get the Kex for rekeying */
1702 *pmonitor->m_pkex = mm_get_kex(&m);
1705 blob = buffer_get_string(&m, &bloblen);
1706 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1709 debug3("%s: Waiting for second key", __func__);
1710 blob = buffer_get_string(&m, &bloblen);
1711 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1714 /* Now get sequence numbers for the packets */
1715 seqnr = buffer_get_int(&m);
1716 blocks = buffer_get_int64(&m);
1717 packets = buffer_get_int(&m);
1718 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1719 seqnr = buffer_get_int(&m);
1720 blocks = buffer_get_int64(&m);
1721 packets = buffer_get_int(&m);
1722 packet_set_state(MODE_IN, seqnr, blocks, packets);
1725 /* Get the key context */
1726 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1727 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1729 debug3("%s: Getting compression state", __func__);
1730 /* Get compression state */
1731 p = buffer_get_string(&m, &plen);
1732 if (plen != sizeof(child_state.outgoing))
1733 fatal("%s: bad request size", __func__);
1734 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1737 p = buffer_get_string(&m, &plen);
1738 if (plen != sizeof(child_state.incoming))
1739 fatal("%s: bad request size", __func__);
1740 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1743 /* Network I/O buffers */
1744 debug3("%s: Getting Network I/O buffers", __func__);
1745 child_state.input = buffer_get_string(&m, &child_state.ilen);
1746 child_state.output = buffer_get_string(&m, &child_state.olen);
1752 /* Allocation functions for zlib */
1754 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1756 size_t len = (size_t) size * ncount;
1759 if (len == 0 || ncount > SIZE_T_MAX / size)
1760 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1762 address = mm_malloc(mm, len);
1768 mm_zfree(struct mm_master *mm, void *address)
1770 mm_free(mm, address);
1774 mm_init_compression(struct mm_master *mm)
1776 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1777 outgoing_stream.zfree = (free_func)mm_zfree;
1778 outgoing_stream.opaque = mm;
1780 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1781 incoming_stream.zfree = (free_func)mm_zfree;
1782 incoming_stream.opaque = mm;
1787 #define FD_CLOSEONEXEC(x) do { \
1788 if (fcntl(x, F_SETFD, 1) == -1) \
1789 fatal("fcntl(%d, F_SETFD)", x); \
1793 monitor_socketpair(int *pair)
1795 #ifdef HAVE_SOCKETPAIR
1796 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1797 fatal("%s: socketpair", __func__);
1799 fatal("%s: UsePrivilegeSeparation=yes not supported",
1802 FD_CLOSEONEXEC(pair[0]);
1803 FD_CLOSEONEXEC(pair[1]);
1806 #define MM_MEMSIZE 65536
1811 struct monitor *mon;
1814 mon = xcalloc(1, sizeof(*mon));
1816 monitor_socketpair(pair);
1818 mon->m_recvfd = pair[0];
1819 mon->m_sendfd = pair[1];
1821 /* Used to share zlib space across processes */
1822 if (options.compression) {
1823 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1824 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1826 /* Compression needs to share state across borders */
1827 mm_init_compression(mon->m_zlib);
1834 monitor_reinit(struct monitor *mon)
1838 monitor_socketpair(pair);
1840 mon->m_recvfd = pair[0];
1841 mon->m_sendfd = pair[1];
1846 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1852 goid.elements = buffer_get_string(m, &len);
1855 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1857 xfree(goid.elements);
1860 buffer_put_int(m, major);
1862 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1864 /* Now we have a context, enable the step */
1865 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1871 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1874 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1875 OM_uint32 major, minor;
1876 OM_uint32 flags = 0; /* GSI needs this */
1879 in.value = buffer_get_string(m, &len);
1881 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1885 buffer_put_int(m, major);
1886 buffer_put_string(m, out.value, out.length);
1887 buffer_put_int(m, flags);
1888 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1890 gss_release_buffer(&minor, &out);
1892 if (major == GSS_S_COMPLETE) {
1893 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1894 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1895 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1901 mm_answer_gss_checkmic(int sock, Buffer *m)
1903 gss_buffer_desc gssbuf, mic;
1907 gssbuf.value = buffer_get_string(m, &len);
1908 gssbuf.length = len;
1909 mic.value = buffer_get_string(m, &len);
1912 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1914 xfree(gssbuf.value);
1918 buffer_put_int(m, ret);
1920 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1922 if (!GSS_ERROR(ret))
1923 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1929 mm_answer_gss_userok(int sock, Buffer *m)
1933 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1936 buffer_put_int(m, authenticated);
1938 debug3("%s: sending result %d", __func__, authenticated);
1939 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1941 auth_method = "gssapi-with-mic";
1943 /* Monitor loop will terminate if authenticated */
1944 return (authenticated);