1 /* $OpenBSD: monitor.c,v 1.84 2006/07/26 13:57:17 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/param.h>
32 #include <sys/socket.h>
49 #include <openssl/dh.h>
55 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
58 #define TARGET_OS_MAC 1
63 #include "auth-options.h"
72 #include "monitor_mm.h"
73 #include "monitor_wrap.h"
74 #include "monitor_fdpass.h"
84 static Gssctxt *gsscontext = NULL;
88 extern ServerOptions options;
89 extern u_int utmp_len;
90 extern Newkeys *current_keys[];
91 extern z_stream incoming_stream;
92 extern z_stream outgoing_stream;
93 extern u_char session_id[];
94 extern Buffer input, output;
95 extern Buffer auth_debug;
96 extern int auth_debug_init;
97 extern Buffer loginmsg;
99 /* State exported from the child */
122 /* Functions on the monitor that answer unprivileged requests */
124 int mm_answer_moduli(int, Buffer *);
125 int mm_answer_sign(int, Buffer *);
126 int mm_answer_pwnamallow(int, Buffer *);
127 int mm_answer_auth2_read_banner(int, Buffer *);
128 int mm_answer_authserv(int, Buffer *);
129 int mm_answer_authpassword(int, Buffer *);
130 int mm_answer_bsdauthquery(int, Buffer *);
131 int mm_answer_bsdauthrespond(int, Buffer *);
132 int mm_answer_skeyquery(int, Buffer *);
133 int mm_answer_skeyrespond(int, Buffer *);
134 int mm_answer_keyallowed(int, Buffer *);
135 int mm_answer_keyverify(int, Buffer *);
136 int mm_answer_pty(int, Buffer *);
137 int mm_answer_pty_cleanup(int, Buffer *);
138 int mm_answer_term(int, Buffer *);
139 int mm_answer_rsa_keyallowed(int, Buffer *);
140 int mm_answer_rsa_challenge(int, Buffer *);
141 int mm_answer_rsa_response(int, Buffer *);
142 int mm_answer_sesskey(int, Buffer *);
143 int mm_answer_sessid(int, Buffer *);
146 int mm_answer_pam_start(int, Buffer *);
147 int mm_answer_pam_account(int, Buffer *);
148 int mm_answer_pam_init_ctx(int, Buffer *);
149 int mm_answer_pam_query(int, Buffer *);
150 int mm_answer_pam_respond(int, Buffer *);
151 int mm_answer_pam_free_ctx(int, Buffer *);
155 int mm_answer_gss_setup_ctx(int, Buffer *);
156 int mm_answer_gss_accept_ctx(int, Buffer *);
157 int mm_answer_gss_userok(int, Buffer *);
158 int mm_answer_gss_checkmic(int, Buffer *);
161 #ifdef SSH_AUDIT_EVENTS
162 int mm_answer_audit_event(int, Buffer *);
163 int mm_answer_audit_command(int, Buffer *);
166 static Authctxt *authctxt;
167 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
169 /* local state for key verify */
170 static u_char *key_blob = NULL;
171 static u_int key_bloblen = 0;
172 static int key_blobtype = MM_NOKEY;
173 static char *hostbased_cuser = NULL;
174 static char *hostbased_chost = NULL;
175 static char *auth_method = "unknown";
176 static u_int session_id2_len = 0;
177 static u_char *session_id2 = NULL;
178 static pid_t monitor_child_pid;
181 enum monitor_reqtype type;
183 int (*f)(int, Buffer *);
186 #define MON_ISAUTH 0x0004 /* Required for Authentication */
187 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
188 #define MON_ONCE 0x0010 /* Disable after calling */
189 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
191 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
193 #define MON_PERMIT 0x1000 /* Request is permitted */
195 struct mon_table mon_dispatch_proto20[] = {
196 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
197 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
198 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
199 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
200 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
201 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
203 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
204 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
205 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
206 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
207 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
208 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
210 #ifdef SSH_AUDIT_EVENTS
211 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
214 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
215 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
218 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
219 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
221 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
222 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
224 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
225 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
226 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
227 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
232 struct mon_table mon_dispatch_postauth20[] = {
233 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
234 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
235 {MONITOR_REQ_PTY, 0, mm_answer_pty},
236 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
237 {MONITOR_REQ_TERM, 0, mm_answer_term},
238 #ifdef SSH_AUDIT_EVENTS
239 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
240 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
245 struct mon_table mon_dispatch_proto15[] = {
246 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
247 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
248 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
249 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
250 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
251 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
252 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
253 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
255 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
256 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
259 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
260 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
263 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
264 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
265 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
266 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
267 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
268 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
270 #ifdef SSH_AUDIT_EVENTS
271 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
276 struct mon_table mon_dispatch_postauth15[] = {
277 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
278 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
279 {MONITOR_REQ_TERM, 0, mm_answer_term},
280 #ifdef SSH_AUDIT_EVENTS
281 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
282 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
287 struct mon_table *mon_dispatch;
289 /* Specifies if a certain message is allowed at the moment */
292 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
294 while (ent->f != NULL) {
295 if (ent->type == type) {
296 ent->flags &= ~MON_PERMIT;
297 ent->flags |= permit ? MON_PERMIT : 0;
305 monitor_permit_authentications(int permit)
307 struct mon_table *ent = mon_dispatch;
309 while (ent->f != NULL) {
310 if (ent->flags & MON_AUTH) {
311 ent->flags &= ~MON_PERMIT;
312 ent->flags |= permit ? MON_PERMIT : 0;
319 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
321 struct mon_table *ent;
322 int authenticated = 0;
324 debug3("preauth child monitor started");
326 authctxt = _authctxt;
327 memset(authctxt, 0, sizeof(*authctxt));
329 authctxt->loginmsg = &loginmsg;
332 mon_dispatch = mon_dispatch_proto20;
334 /* Permit requests for moduli and signatures */
335 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
336 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
338 mon_dispatch = mon_dispatch_proto15;
340 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
343 /* The first few requests do not require asynchronous access */
344 while (!authenticated) {
345 auth_method = "unknown";
346 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
348 if (!(ent->flags & MON_AUTHDECIDE))
349 fatal("%s: unexpected authentication from %d",
350 __func__, ent->type);
351 if (authctxt->pw->pw_uid == 0 &&
352 !auth_root_allowed(auth_method))
355 /* PAM needs to perform account checks after auth */
356 if (options.use_pam && authenticated) {
360 mm_request_receive_expect(pmonitor->m_sendfd,
361 MONITOR_REQ_PAM_ACCOUNT, &m);
362 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
368 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
369 auth_log(authctxt, authenticated, auth_method,
370 compat20 ? " ssh2" : "");
372 authctxt->failures++;
376 if (!authctxt->valid)
377 fatal("%s: authenticated invalid user", __func__);
378 if (strcmp(auth_method, "unknown") == 0)
379 fatal("%s: authentication method name unknown", __func__);
381 debug("%s: %s has been authenticated by privileged process",
382 __func__, authctxt->user);
384 mm_get_keystate(pmonitor);
388 monitor_set_child_handler(pid_t pid)
390 monitor_child_pid = pid;
394 monitor_child_handler(int sig)
396 kill(monitor_child_pid, sig);
400 monitor_child_postauth(struct monitor *pmonitor)
402 monitor_set_child_handler(pmonitor->m_pid);
403 signal(SIGHUP, &monitor_child_handler);
404 signal(SIGTERM, &monitor_child_handler);
407 mon_dispatch = mon_dispatch_postauth20;
409 /* Permit requests for moduli and signatures */
410 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
411 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
412 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
414 mon_dispatch = mon_dispatch_postauth15;
415 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
418 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
419 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
423 monitor_read(pmonitor, mon_dispatch, NULL);
427 monitor_sync(struct monitor *pmonitor)
429 if (options.compression) {
430 /* The member allocation is not visible, so sync it */
431 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
436 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
437 struct mon_table **pent)
445 mm_request_receive(pmonitor->m_sendfd, &m);
446 type = buffer_get_char(&m);
448 debug3("%s: checking request %d", __func__, type);
450 while (ent->f != NULL) {
451 if (ent->type == type)
456 if (ent->f != NULL) {
457 if (!(ent->flags & MON_PERMIT))
458 fatal("%s: unpermitted request %d", __func__,
460 ret = (*ent->f)(pmonitor->m_sendfd, &m);
463 /* The child may use this request only once, disable it */
464 if (ent->flags & MON_ONCE) {
465 debug2("%s: %d used once, disabling now", __func__,
467 ent->flags &= ~MON_PERMIT;
476 fatal("%s: unsupported request: %d", __func__, type);
482 /* allowed key state */
484 monitor_allowed_key(u_char *blob, u_int bloblen)
486 /* make sure key is allowed */
487 if (key_blob == NULL || key_bloblen != bloblen ||
488 memcmp(key_blob, blob, key_bloblen))
494 monitor_reset_key_state(void)
497 if (key_blob != NULL)
499 if (hostbased_cuser != NULL)
500 xfree(hostbased_cuser);
501 if (hostbased_chost != NULL)
502 xfree(hostbased_chost);
505 key_blobtype = MM_NOKEY;
506 hostbased_cuser = NULL;
507 hostbased_chost = NULL;
511 mm_answer_moduli(int sock, Buffer *m)
516 min = buffer_get_int(m);
517 want = buffer_get_int(m);
518 max = buffer_get_int(m);
520 debug3("%s: got parameters: %d %d %d",
521 __func__, min, want, max);
522 /* We need to check here, too, in case the child got corrupted */
523 if (max < min || want < min || max < want)
524 fatal("%s: bad parameters: %d %d %d",
525 __func__, min, want, max);
529 dh = choose_dh(min, want, max);
531 buffer_put_char(m, 0);
534 /* Send first bignum */
535 buffer_put_char(m, 1);
536 buffer_put_bignum2(m, dh->p);
537 buffer_put_bignum2(m, dh->g);
541 mm_request_send(sock, MONITOR_ANS_MODULI, m);
546 mm_answer_sign(int sock, Buffer *m)
551 u_int siglen, datlen;
554 debug3("%s", __func__);
556 keyid = buffer_get_int(m);
557 p = buffer_get_string(m, &datlen);
560 * Supported KEX types will only return SHA1 (20 byte) or
561 * SHA256 (32 byte) hashes
563 if (datlen != 20 && datlen != 32)
564 fatal("%s: data length incorrect: %u", __func__, datlen);
566 /* save session id, it will be passed on the first call */
567 if (session_id2_len == 0) {
568 session_id2_len = datlen;
569 session_id2 = xmalloc(session_id2_len);
570 memcpy(session_id2, p, session_id2_len);
573 if ((key = get_hostkey_by_index(keyid)) == NULL)
574 fatal("%s: no hostkey from index %d", __func__, keyid);
575 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
576 fatal("%s: key_sign failed", __func__);
578 debug3("%s: signature %p(%u)", __func__, signature, siglen);
581 buffer_put_string(m, signature, siglen);
586 mm_request_send(sock, MONITOR_ANS_SIGN, m);
588 /* Turn on permissions for getpwnam */
589 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
594 /* Retrieves the password entry and also checks if the user is permitted */
597 mm_answer_pwnamallow(int sock, Buffer *m)
600 struct passwd *pwent;
603 debug3("%s", __func__);
605 if (authctxt->attempt++ != 0)
606 fatal("%s: multiple attempts for getpwnam", __func__);
608 username = buffer_get_string(m, NULL);
610 pwent = getpwnamallow(username);
612 authctxt->user = xstrdup(username);
613 setproctitle("%s [priv]", pwent ? username : "unknown");
619 buffer_put_char(m, 0);
620 authctxt->pw = fakepw();
625 authctxt->pw = pwent;
628 buffer_put_char(m, 1);
629 buffer_put_string(m, pwent, sizeof(struct passwd));
630 buffer_put_cstring(m, pwent->pw_name);
631 buffer_put_cstring(m, "*");
632 buffer_put_cstring(m, pwent->pw_gecos);
633 #ifdef HAVE_PW_CLASS_IN_PASSWD
634 buffer_put_cstring(m, pwent->pw_class);
636 buffer_put_cstring(m, pwent->pw_dir);
637 buffer_put_cstring(m, pwent->pw_shell);
640 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
641 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
643 /* For SSHv1 allow authentication now */
645 monitor_permit_authentications(1);
647 /* Allow service/style information on the auth context */
648 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
649 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
654 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
656 #ifdef SSH_AUDIT_EVENTS
657 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
663 int mm_answer_auth2_read_banner(int sock, Buffer *m)
668 banner = auth2_read_banner();
669 buffer_put_cstring(m, banner != NULL ? banner : "");
670 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
679 mm_answer_authserv(int sock, Buffer *m)
681 monitor_permit_authentications(1);
683 authctxt->service = buffer_get_string(m, NULL);
684 authctxt->style = buffer_get_string(m, NULL);
685 debug3("%s: service=%s, style=%s",
686 __func__, authctxt->service, authctxt->style);
688 if (strlen(authctxt->style) == 0) {
689 xfree(authctxt->style);
690 authctxt->style = NULL;
697 mm_answer_authpassword(int sock, Buffer *m)
699 static int call_count;
704 passwd = buffer_get_string(m, &plen);
705 /* Only authenticate if the context is valid */
706 authenticated = options.password_authentication &&
707 auth_password(authctxt, passwd);
708 memset(passwd, 0, strlen(passwd));
712 buffer_put_int(m, authenticated);
714 debug3("%s: sending result %d", __func__, authenticated);
715 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
718 if (plen == 0 && call_count == 1)
719 auth_method = "none";
721 auth_method = "password";
723 /* Causes monitor loop to terminate if authenticated */
724 return (authenticated);
729 mm_answer_bsdauthquery(int sock, Buffer *m)
731 char *name, *infotxt;
737 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
738 &prompts, &echo_on) < 0 ? 0 : 1;
741 buffer_put_int(m, success);
743 buffer_put_cstring(m, prompts[0]);
745 debug3("%s: sending challenge success: %u", __func__, success);
746 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
759 mm_answer_bsdauthrespond(int sock, Buffer *m)
764 if (authctxt->as == 0)
765 fatal("%s: no bsd auth session", __func__);
767 response = buffer_get_string(m, NULL);
768 authok = options.challenge_response_authentication &&
769 auth_userresponse(authctxt->as, response, 0);
771 debug3("%s: <%s> = <%d>", __func__, response, authok);
775 buffer_put_int(m, authok);
777 debug3("%s: sending authenticated: %d", __func__, authok);
778 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
780 auth_method = "bsdauth";
782 return (authok != 0);
788 mm_answer_skeyquery(int sock, Buffer *m)
791 char challenge[1024];
794 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
795 sizeof(challenge)) < 0 ? 0 : 1;
798 buffer_put_int(m, success);
800 buffer_put_cstring(m, challenge);
802 debug3("%s: sending challenge success: %u", __func__, success);
803 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
809 mm_answer_skeyrespond(int sock, Buffer *m)
814 response = buffer_get_string(m, NULL);
816 authok = (options.challenge_response_authentication &&
818 skey_haskey(authctxt->pw->pw_name) == 0 &&
819 skey_passcheck(authctxt->pw->pw_name, response) != -1);
824 buffer_put_int(m, authok);
826 debug3("%s: sending authenticated: %d", __func__, authok);
827 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
829 auth_method = "skey";
831 return (authok != 0);
837 mm_answer_pam_start(int sock, Buffer *m)
839 if (!options.use_pam)
840 fatal("UsePAM not set, but ended up in %s anyway", __func__);
844 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
850 mm_answer_pam_account(int sock, Buffer *m)
854 if (!options.use_pam)
855 fatal("UsePAM not set, but ended up in %s anyway", __func__);
857 ret = do_pam_account();
859 buffer_put_int(m, ret);
860 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
862 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
867 static void *sshpam_ctxt, *sshpam_authok;
868 extern KbdintDevice sshpam_device;
871 mm_answer_pam_init_ctx(int sock, Buffer *m)
874 debug3("%s", __func__);
875 authctxt->user = buffer_get_string(m, NULL);
876 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
877 sshpam_authok = NULL;
879 if (sshpam_ctxt != NULL) {
880 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
881 buffer_put_int(m, 1);
883 buffer_put_int(m, 0);
885 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
890 mm_answer_pam_query(int sock, Buffer *m)
892 char *name, *info, **prompts;
893 u_int i, num, *echo_on;
896 debug3("%s", __func__);
897 sshpam_authok = NULL;
898 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
899 if (ret == 0 && num == 0)
900 sshpam_authok = sshpam_ctxt;
901 if (num > 1 || name == NULL || info == NULL)
904 buffer_put_int(m, ret);
905 buffer_put_cstring(m, name);
907 buffer_put_cstring(m, info);
909 buffer_put_int(m, num);
910 for (i = 0; i < num; ++i) {
911 buffer_put_cstring(m, prompts[i]);
913 buffer_put_int(m, echo_on[i]);
919 auth_method = "keyboard-interactive/pam";
920 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
925 mm_answer_pam_respond(int sock, Buffer *m)
931 debug3("%s", __func__);
932 sshpam_authok = NULL;
933 num = buffer_get_int(m);
935 resp = xcalloc(num, sizeof(char *));
936 for (i = 0; i < num; ++i)
937 resp[i] = buffer_get_string(m, NULL);
938 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
939 for (i = 0; i < num; ++i)
943 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
946 buffer_put_int(m, ret);
947 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
948 auth_method = "keyboard-interactive/pam";
950 sshpam_authok = sshpam_ctxt;
955 mm_answer_pam_free_ctx(int sock, Buffer *m)
958 debug3("%s", __func__);
959 (sshpam_device.free_ctx)(sshpam_ctxt);
961 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
962 auth_method = "keyboard-interactive/pam";
963 return (sshpam_authok == sshpam_ctxt);
968 mm_append_debug(Buffer *m)
970 if (auth_debug_init && buffer_len(&auth_debug)) {
971 debug3("%s: Appending debug messages for child", __func__);
972 buffer_append(m, buffer_ptr(&auth_debug),
973 buffer_len(&auth_debug));
974 buffer_clear(&auth_debug);
979 mm_answer_keyallowed(int sock, Buffer *m)
985 enum mm_keytype type = 0;
988 debug3("%s entering", __func__);
990 type = buffer_get_int(m);
991 cuser = buffer_get_string(m, NULL);
992 chost = buffer_get_string(m, NULL);
993 blob = buffer_get_string(m, &bloblen);
995 key = key_from_blob(blob, bloblen);
997 if ((compat20 && type == MM_RSAHOSTKEY) ||
998 (!compat20 && type != MM_RSAHOSTKEY))
999 fatal("%s: key type and protocol mismatch", __func__);
1001 debug3("%s: key_from_blob: %p", __func__, key);
1003 if (key != NULL && authctxt->valid) {
1006 allowed = options.pubkey_authentication &&
1007 user_key_allowed(authctxt->pw, key);
1008 auth_method = "publickey";
1011 allowed = options.hostbased_authentication &&
1012 hostbased_key_allowed(authctxt->pw,
1014 auth_method = "hostbased";
1017 key->type = KEY_RSA1; /* XXX */
1018 allowed = options.rhosts_rsa_authentication &&
1019 auth_rhosts_rsa_key_allowed(authctxt->pw,
1021 auth_method = "rsa";
1024 fatal("%s: unknown key type %d", __func__, type);
1031 /* clear temporarily storage (used by verify) */
1032 monitor_reset_key_state();
1035 /* Save temporarily for comparison in verify */
1037 key_bloblen = bloblen;
1038 key_blobtype = type;
1039 hostbased_cuser = cuser;
1040 hostbased_chost = chost;
1042 /* Log failed attempt */
1043 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1049 debug3("%s: key %p is %s",
1050 __func__, key, allowed ? "allowed" : "disallowed");
1053 buffer_put_int(m, allowed);
1054 buffer_put_int(m, forced_command != NULL);
1058 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1060 if (type == MM_RSAHOSTKEY)
1061 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1067 monitor_valid_userblob(u_char *data, u_int datalen)
1075 buffer_append(&b, data, datalen);
1077 if (datafellows & SSH_OLD_SESSIONID) {
1079 len = buffer_len(&b);
1080 if ((session_id2 == NULL) ||
1081 (len < session_id2_len) ||
1082 (memcmp(p, session_id2, session_id2_len) != 0))
1084 buffer_consume(&b, session_id2_len);
1086 p = buffer_get_string(&b, &len);
1087 if ((session_id2 == NULL) ||
1088 (len != session_id2_len) ||
1089 (memcmp(p, session_id2, session_id2_len) != 0))
1093 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1095 p = buffer_get_string(&b, NULL);
1096 if (strcmp(authctxt->user, p) != 0) {
1097 logit("wrong user name passed to monitor: expected %s != %.100s",
1102 buffer_skip_string(&b);
1103 if (datafellows & SSH_BUG_PKAUTH) {
1104 if (!buffer_get_char(&b))
1107 p = buffer_get_string(&b, NULL);
1108 if (strcmp("publickey", p) != 0)
1111 if (!buffer_get_char(&b))
1113 buffer_skip_string(&b);
1115 buffer_skip_string(&b);
1116 if (buffer_len(&b) != 0)
1123 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1132 buffer_append(&b, data, datalen);
1134 p = buffer_get_string(&b, &len);
1135 if ((session_id2 == NULL) ||
1136 (len != session_id2_len) ||
1137 (memcmp(p, session_id2, session_id2_len) != 0))
1141 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1143 p = buffer_get_string(&b, NULL);
1144 if (strcmp(authctxt->user, p) != 0) {
1145 logit("wrong user name passed to monitor: expected %s != %.100s",
1150 buffer_skip_string(&b); /* service */
1151 p = buffer_get_string(&b, NULL);
1152 if (strcmp(p, "hostbased") != 0)
1155 buffer_skip_string(&b); /* pkalg */
1156 buffer_skip_string(&b); /* pkblob */
1158 /* verify client host, strip trailing dot if necessary */
1159 p = buffer_get_string(&b, NULL);
1160 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1162 if (strcmp(p, chost) != 0)
1166 /* verify client user */
1167 p = buffer_get_string(&b, NULL);
1168 if (strcmp(p, cuser) != 0)
1172 if (buffer_len(&b) != 0)
1179 mm_answer_keyverify(int sock, Buffer *m)
1182 u_char *signature, *data, *blob;
1183 u_int signaturelen, datalen, bloblen;
1187 blob = buffer_get_string(m, &bloblen);
1188 signature = buffer_get_string(m, &signaturelen);
1189 data = buffer_get_string(m, &datalen);
1191 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1192 !monitor_allowed_key(blob, bloblen))
1193 fatal("%s: bad key, not previously allowed", __func__);
1195 key = key_from_blob(blob, bloblen);
1197 fatal("%s: bad public key blob", __func__);
1199 switch (key_blobtype) {
1201 valid_data = monitor_valid_userblob(data, datalen);
1204 valid_data = monitor_valid_hostbasedblob(data, datalen,
1205 hostbased_cuser, hostbased_chost);
1212 fatal("%s: bad signature data blob", __func__);
1214 verified = key_verify(key, signature, signaturelen, data, datalen);
1215 debug3("%s: key %p signature %s",
1216 __func__, key, verified ? "verified" : "unverified");
1223 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1225 monitor_reset_key_state();
1228 buffer_put_int(m, verified);
1229 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1235 mm_record_login(Session *s, struct passwd *pw)
1238 struct sockaddr_storage from;
1241 * Get IP address of client. If the connection is not a socket, let
1242 * the address be 0.0.0.0.
1244 memset(&from, 0, sizeof(from));
1245 fromlen = sizeof(from);
1246 if (packet_connection_is_on_socket()) {
1247 if (getpeername(packet_get_connection_in(),
1248 (struct sockaddr *)&from, &fromlen) < 0) {
1249 debug("getpeername: %.100s", strerror(errno));
1253 /* Record that there was a login on that tty from the remote host. */
1254 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1255 get_remote_name_or_ip(utmp_len, options.use_dns),
1256 (struct sockaddr *)&from, fromlen);
1260 mm_session_close(Session *s)
1262 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1263 if (s->ttyfd != -1) {
1264 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1265 session_pty_cleanup2(s);
1271 mm_answer_pty(int sock, Buffer *m)
1273 extern struct monitor *pmonitor;
1277 debug3("%s entering", __func__);
1283 s->authctxt = authctxt;
1284 s->pw = authctxt->pw;
1285 s->pid = pmonitor->m_pid;
1286 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1289 pty_setowner(authctxt->pw, s->tty);
1291 buffer_put_int(m, 1);
1292 buffer_put_cstring(m, s->tty);
1294 /* We need to trick ttyslot */
1295 if (dup2(s->ttyfd, 0) == -1)
1296 fatal("%s: dup2", __func__);
1298 mm_record_login(s, authctxt->pw);
1300 /* Now we can close the file descriptor again */
1303 /* send messages generated by record_login */
1304 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1305 buffer_clear(&loginmsg);
1307 mm_request_send(sock, MONITOR_ANS_PTY, m);
1309 mm_send_fd(sock, s->ptyfd);
1310 mm_send_fd(sock, s->ttyfd);
1312 /* make sure nothing uses fd 0 */
1313 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1314 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1316 error("%s: fd0 %d != 0", __func__, fd0);
1318 /* slave is not needed */
1320 s->ttyfd = s->ptyfd;
1321 /* no need to dup() because nobody closes ptyfd */
1322 s->ptymaster = s->ptyfd;
1324 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1330 mm_session_close(s);
1331 buffer_put_int(m, 0);
1332 mm_request_send(sock, MONITOR_ANS_PTY, m);
1337 mm_answer_pty_cleanup(int sock, Buffer *m)
1342 debug3("%s entering", __func__);
1344 tty = buffer_get_string(m, NULL);
1345 if ((s = session_by_tty(tty)) != NULL)
1346 mm_session_close(s);
1353 mm_answer_sesskey(int sock, Buffer *m)
1358 /* Turn off permissions */
1359 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1361 if ((p = BN_new()) == NULL)
1362 fatal("%s: BN_new", __func__);
1364 buffer_get_bignum2(m, p);
1366 rsafail = ssh1_session_key(p);
1369 buffer_put_int(m, rsafail);
1370 buffer_put_bignum2(m, p);
1374 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1376 /* Turn on permissions for sessid passing */
1377 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1383 mm_answer_sessid(int sock, Buffer *m)
1387 debug3("%s entering", __func__);
1389 if (buffer_len(m) != 16)
1390 fatal("%s: bad ssh1 session id", __func__);
1391 for (i = 0; i < 16; i++)
1392 session_id[i] = buffer_get_char(m);
1394 /* Turn on permissions for getpwnam */
1395 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1401 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1405 u_char *blob = NULL;
1409 debug3("%s entering", __func__);
1411 auth_method = "rsa";
1412 if (options.rsa_authentication && authctxt->valid) {
1413 if ((client_n = BN_new()) == NULL)
1414 fatal("%s: BN_new", __func__);
1415 buffer_get_bignum2(m, client_n);
1416 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1417 BN_clear_free(client_n);
1420 buffer_put_int(m, allowed);
1421 buffer_put_int(m, forced_command != NULL);
1423 /* clear temporarily storage (used by generate challenge) */
1424 monitor_reset_key_state();
1426 if (allowed && key != NULL) {
1427 key->type = KEY_RSA; /* cheat for key_to_blob */
1428 if (key_to_blob(key, &blob, &blen) == 0)
1429 fatal("%s: key_to_blob failed", __func__);
1430 buffer_put_string(m, blob, blen);
1432 /* Save temporarily for comparison in verify */
1435 key_blobtype = MM_RSAUSERKEY;
1442 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1444 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1445 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1450 mm_answer_rsa_challenge(int sock, Buffer *m)
1456 debug3("%s entering", __func__);
1458 if (!authctxt->valid)
1459 fatal("%s: authctxt not valid", __func__);
1460 blob = buffer_get_string(m, &blen);
1461 if (!monitor_allowed_key(blob, blen))
1462 fatal("%s: bad key, not previously allowed", __func__);
1463 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1464 fatal("%s: key type mismatch", __func__);
1465 if ((key = key_from_blob(blob, blen)) == NULL)
1466 fatal("%s: received bad key", __func__);
1469 BN_clear_free(ssh1_challenge);
1470 ssh1_challenge = auth_rsa_generate_challenge(key);
1473 buffer_put_bignum2(m, ssh1_challenge);
1475 debug3("%s sending reply", __func__);
1476 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1478 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1486 mm_answer_rsa_response(int sock, Buffer *m)
1489 u_char *blob, *response;
1493 debug3("%s entering", __func__);
1495 if (!authctxt->valid)
1496 fatal("%s: authctxt not valid", __func__);
1497 if (ssh1_challenge == NULL)
1498 fatal("%s: no ssh1_challenge", __func__);
1500 blob = buffer_get_string(m, &blen);
1501 if (!monitor_allowed_key(blob, blen))
1502 fatal("%s: bad key, not previously allowed", __func__);
1503 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1504 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1505 if ((key = key_from_blob(blob, blen)) == NULL)
1506 fatal("%s: received bad key", __func__);
1507 response = buffer_get_string(m, &len);
1509 fatal("%s: received bad response to challenge", __func__);
1510 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1516 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1519 BN_clear_free(ssh1_challenge);
1520 ssh1_challenge = NULL;
1521 monitor_reset_key_state();
1524 buffer_put_int(m, success);
1525 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1531 mm_answer_term(int sock, Buffer *req)
1533 extern struct monitor *pmonitor;
1536 debug3("%s: tearing down sessions", __func__);
1538 /* The child is terminating */
1539 session_destroy_all(&mm_session_close);
1541 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1545 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1547 /* Terminate process */
1551 #ifdef SSH_AUDIT_EVENTS
1552 /* Report that an audit event occurred */
1554 mm_answer_audit_event(int socket, Buffer *m)
1556 ssh_audit_event_t event;
1558 debug3("%s entering", __func__);
1560 event = buffer_get_int(m);
1562 case SSH_AUTH_FAIL_PUBKEY:
1563 case SSH_AUTH_FAIL_HOSTBASED:
1564 case SSH_AUTH_FAIL_GSSAPI:
1565 case SSH_LOGIN_EXCEED_MAXTRIES:
1566 case SSH_LOGIN_ROOT_DENIED:
1567 case SSH_CONNECTION_CLOSE:
1568 case SSH_INVALID_USER:
1572 fatal("Audit event type %d not permitted", event);
1579 mm_answer_audit_command(int socket, Buffer *m)
1584 debug3("%s entering", __func__);
1585 cmd = buffer_get_string(m, &len);
1586 /* sanity check command, if so how? */
1587 audit_run_command(cmd);
1591 #endif /* SSH_AUDIT_EVENTS */
1594 monitor_apply_keystate(struct monitor *pmonitor)
1597 set_newkeys(MODE_IN);
1598 set_newkeys(MODE_OUT);
1600 packet_set_protocol_flags(child_state.ssh1protoflags);
1601 packet_set_encryption_key(child_state.ssh1key,
1602 child_state.ssh1keylen, child_state.ssh1cipher);
1603 xfree(child_state.ssh1key);
1606 /* for rc4 and other stateful ciphers */
1607 packet_set_keycontext(MODE_OUT, child_state.keyout);
1608 xfree(child_state.keyout);
1609 packet_set_keycontext(MODE_IN, child_state.keyin);
1610 xfree(child_state.keyin);
1613 packet_set_iv(MODE_OUT, child_state.ivout);
1614 xfree(child_state.ivout);
1615 packet_set_iv(MODE_IN, child_state.ivin);
1616 xfree(child_state.ivin);
1619 memcpy(&incoming_stream, &child_state.incoming,
1620 sizeof(incoming_stream));
1621 memcpy(&outgoing_stream, &child_state.outgoing,
1622 sizeof(outgoing_stream));
1624 /* Update with new address */
1625 if (options.compression)
1626 mm_init_compression(pmonitor->m_zlib);
1628 /* Network I/O buffers */
1629 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1630 buffer_clear(&input);
1631 buffer_append(&input, child_state.input, child_state.ilen);
1632 memset(child_state.input, 0, child_state.ilen);
1633 xfree(child_state.input);
1635 buffer_clear(&output);
1636 buffer_append(&output, child_state.output, child_state.olen);
1637 memset(child_state.output, 0, child_state.olen);
1638 xfree(child_state.output);
1642 mm_get_kex(Buffer *m)
1648 kex = xcalloc(1, sizeof(*kex));
1649 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1650 if ((session_id2 == NULL) ||
1651 (kex->session_id_len != session_id2_len) ||
1652 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1653 fatal("mm_get_get: internal error: bad session id");
1654 kex->we_need = buffer_get_int(m);
1655 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1656 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1657 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1658 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1660 kex->hostkey_type = buffer_get_int(m);
1661 kex->kex_type = buffer_get_int(m);
1662 blob = buffer_get_string(m, &bloblen);
1663 buffer_init(&kex->my);
1664 buffer_append(&kex->my, blob, bloblen);
1666 blob = buffer_get_string(m, &bloblen);
1667 buffer_init(&kex->peer);
1668 buffer_append(&kex->peer, blob, bloblen);
1671 kex->flags = buffer_get_int(m);
1672 kex->client_version_string = buffer_get_string(m, NULL);
1673 kex->server_version_string = buffer_get_string(m, NULL);
1674 kex->load_host_key=&get_hostkey_by_type;
1675 kex->host_key_index=&get_hostkey_index;
1680 /* This function requries careful sanity checking */
1683 mm_get_keystate(struct monitor *pmonitor)
1687 u_int bloblen, plen;
1688 u_int32_t seqnr, packets;
1691 debug3("%s: Waiting for new keys", __func__);
1694 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1696 child_state.ssh1protoflags = buffer_get_int(&m);
1697 child_state.ssh1cipher = buffer_get_int(&m);
1698 child_state.ssh1key = buffer_get_string(&m,
1699 &child_state.ssh1keylen);
1700 child_state.ivout = buffer_get_string(&m,
1701 &child_state.ivoutlen);
1702 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1705 /* Get the Kex for rekeying */
1706 *pmonitor->m_pkex = mm_get_kex(&m);
1709 blob = buffer_get_string(&m, &bloblen);
1710 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1713 debug3("%s: Waiting for second key", __func__);
1714 blob = buffer_get_string(&m, &bloblen);
1715 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1718 /* Now get sequence numbers for the packets */
1719 seqnr = buffer_get_int(&m);
1720 blocks = buffer_get_int64(&m);
1721 packets = buffer_get_int(&m);
1722 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1723 seqnr = buffer_get_int(&m);
1724 blocks = buffer_get_int64(&m);
1725 packets = buffer_get_int(&m);
1726 packet_set_state(MODE_IN, seqnr, blocks, packets);
1729 /* Get the key context */
1730 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1731 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1733 debug3("%s: Getting compression state", __func__);
1734 /* Get compression state */
1735 p = buffer_get_string(&m, &plen);
1736 if (plen != sizeof(child_state.outgoing))
1737 fatal("%s: bad request size", __func__);
1738 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1741 p = buffer_get_string(&m, &plen);
1742 if (plen != sizeof(child_state.incoming))
1743 fatal("%s: bad request size", __func__);
1744 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1747 /* Network I/O buffers */
1748 debug3("%s: Getting Network I/O buffers", __func__);
1749 child_state.input = buffer_get_string(&m, &child_state.ilen);
1750 child_state.output = buffer_get_string(&m, &child_state.olen);
1756 /* Allocation functions for zlib */
1758 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1760 size_t len = (size_t) size * ncount;
1763 if (len == 0 || ncount > SIZE_T_MAX / size)
1764 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1766 address = mm_malloc(mm, len);
1772 mm_zfree(struct mm_master *mm, void *address)
1774 mm_free(mm, address);
1778 mm_init_compression(struct mm_master *mm)
1780 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1781 outgoing_stream.zfree = (free_func)mm_zfree;
1782 outgoing_stream.opaque = mm;
1784 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1785 incoming_stream.zfree = (free_func)mm_zfree;
1786 incoming_stream.opaque = mm;
1791 #define FD_CLOSEONEXEC(x) do { \
1792 if (fcntl(x, F_SETFD, 1) == -1) \
1793 fatal("fcntl(%d, F_SETFD)", x); \
1797 monitor_socketpair(int *pair)
1799 #ifdef HAVE_SOCKETPAIR
1800 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1801 fatal("%s: socketpair", __func__);
1803 fatal("%s: UsePrivilegeSeparation=yes not supported",
1806 FD_CLOSEONEXEC(pair[0]);
1807 FD_CLOSEONEXEC(pair[1]);
1810 #define MM_MEMSIZE 65536
1815 struct monitor *mon;
1818 mon = xcalloc(1, sizeof(*mon));
1820 monitor_socketpair(pair);
1822 mon->m_recvfd = pair[0];
1823 mon->m_sendfd = pair[1];
1825 /* Used to share zlib space across processes */
1826 if (options.compression) {
1827 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1828 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1830 /* Compression needs to share state across borders */
1831 mm_init_compression(mon->m_zlib);
1838 monitor_reinit(struct monitor *mon)
1842 monitor_socketpair(pair);
1844 mon->m_recvfd = pair[0];
1845 mon->m_sendfd = pair[1];
1850 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1856 goid.elements = buffer_get_string(m, &len);
1859 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1861 xfree(goid.elements);
1864 buffer_put_int(m, major);
1866 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1868 /* Now we have a context, enable the step */
1869 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1875 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1878 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1879 OM_uint32 major, minor;
1880 OM_uint32 flags = 0; /* GSI needs this */
1883 in.value = buffer_get_string(m, &len);
1885 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1889 buffer_put_int(m, major);
1890 buffer_put_string(m, out.value, out.length);
1891 buffer_put_int(m, flags);
1892 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1894 gss_release_buffer(&minor, &out);
1896 if (major == GSS_S_COMPLETE) {
1897 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1898 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1899 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1905 mm_answer_gss_checkmic(int sock, Buffer *m)
1907 gss_buffer_desc gssbuf, mic;
1911 gssbuf.value = buffer_get_string(m, &len);
1912 gssbuf.length = len;
1913 mic.value = buffer_get_string(m, &len);
1916 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1918 xfree(gssbuf.value);
1922 buffer_put_int(m, ret);
1924 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1926 if (!GSS_ERROR(ret))
1927 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1933 mm_answer_gss_userok(int sock, Buffer *m)
1937 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1940 buffer_put_int(m, authenticated);
1942 debug3("%s: sending result %d", __func__, authenticated);
1943 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1945 auth_method = "gssapi-with-mic";
1947 /* Monitor loop will terminate if authenticated */
1948 return (authenticated);