1 /* $OpenBSD: monitor.c,v 1.78 2006/07/06 16:03: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>
43 #include <openssl/dh.h>
49 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
52 #define TARGET_OS_MAC 1
57 #include "auth-options.h"
66 #include "monitor_mm.h"
67 #include "monitor_wrap.h"
68 #include "monitor_fdpass.h"
78 static Gssctxt *gsscontext = NULL;
82 extern ServerOptions options;
83 extern u_int utmp_len;
84 extern Newkeys *current_keys[];
85 extern z_stream incoming_stream;
86 extern z_stream outgoing_stream;
87 extern u_char session_id[];
88 extern Buffer input, output;
89 extern Buffer auth_debug;
90 extern int auth_debug_init;
91 extern Buffer loginmsg;
93 /* State exported from the child */
116 /* Functions on the monitor that answer unprivileged requests */
118 int mm_answer_moduli(int, Buffer *);
119 int mm_answer_sign(int, Buffer *);
120 int mm_answer_pwnamallow(int, Buffer *);
121 int mm_answer_auth2_read_banner(int, Buffer *);
122 int mm_answer_authserv(int, Buffer *);
123 int mm_answer_authpassword(int, Buffer *);
124 int mm_answer_bsdauthquery(int, Buffer *);
125 int mm_answer_bsdauthrespond(int, Buffer *);
126 int mm_answer_skeyquery(int, Buffer *);
127 int mm_answer_skeyrespond(int, Buffer *);
128 int mm_answer_keyallowed(int, Buffer *);
129 int mm_answer_keyverify(int, Buffer *);
130 int mm_answer_pty(int, Buffer *);
131 int mm_answer_pty_cleanup(int, Buffer *);
132 int mm_answer_term(int, Buffer *);
133 int mm_answer_rsa_keyallowed(int, Buffer *);
134 int mm_answer_rsa_challenge(int, Buffer *);
135 int mm_answer_rsa_response(int, Buffer *);
136 int mm_answer_sesskey(int, Buffer *);
137 int mm_answer_sessid(int, Buffer *);
140 int mm_answer_pam_start(int, Buffer *);
141 int mm_answer_pam_account(int, Buffer *);
142 int mm_answer_pam_init_ctx(int, Buffer *);
143 int mm_answer_pam_query(int, Buffer *);
144 int mm_answer_pam_respond(int, Buffer *);
145 int mm_answer_pam_free_ctx(int, Buffer *);
149 int mm_answer_gss_setup_ctx(int, Buffer *);
150 int mm_answer_gss_accept_ctx(int, Buffer *);
151 int mm_answer_gss_userok(int, Buffer *);
152 int mm_answer_gss_checkmic(int, Buffer *);
155 #ifdef SSH_AUDIT_EVENTS
156 int mm_answer_audit_event(int, Buffer *);
157 int mm_answer_audit_command(int, Buffer *);
160 static Authctxt *authctxt;
161 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
163 /* local state for key verify */
164 static u_char *key_blob = NULL;
165 static u_int key_bloblen = 0;
166 static int key_blobtype = MM_NOKEY;
167 static char *hostbased_cuser = NULL;
168 static char *hostbased_chost = NULL;
169 static char *auth_method = "unknown";
170 static u_int session_id2_len = 0;
171 static u_char *session_id2 = NULL;
172 static pid_t monitor_child_pid;
175 enum monitor_reqtype type;
177 int (*f)(int, Buffer *);
180 #define MON_ISAUTH 0x0004 /* Required for Authentication */
181 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
182 #define MON_ONCE 0x0010 /* Disable after calling */
183 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
185 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
187 #define MON_PERMIT 0x1000 /* Request is permitted */
189 struct mon_table mon_dispatch_proto20[] = {
190 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
191 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
192 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
193 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
194 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
195 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
197 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
198 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
199 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
200 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
201 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
202 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
204 #ifdef SSH_AUDIT_EVENTS
205 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
208 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
209 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
212 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
213 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
215 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
216 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
218 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
219 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
220 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
221 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
226 struct mon_table mon_dispatch_postauth20[] = {
227 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
228 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
229 {MONITOR_REQ_PTY, 0, mm_answer_pty},
230 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
231 {MONITOR_REQ_TERM, 0, mm_answer_term},
232 #ifdef SSH_AUDIT_EVENTS
233 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
234 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
239 struct mon_table mon_dispatch_proto15[] = {
240 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
241 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
242 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
243 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
244 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
245 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
246 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
247 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
249 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
250 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
253 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
254 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
257 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
258 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
259 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
260 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
261 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
262 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
264 #ifdef SSH_AUDIT_EVENTS
265 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
270 struct mon_table mon_dispatch_postauth15[] = {
271 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
272 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
273 {MONITOR_REQ_TERM, 0, mm_answer_term},
274 #ifdef SSH_AUDIT_EVENTS
275 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
276 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
281 struct mon_table *mon_dispatch;
283 /* Specifies if a certain message is allowed at the moment */
286 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
288 while (ent->f != NULL) {
289 if (ent->type == type) {
290 ent->flags &= ~MON_PERMIT;
291 ent->flags |= permit ? MON_PERMIT : 0;
299 monitor_permit_authentications(int permit)
301 struct mon_table *ent = mon_dispatch;
303 while (ent->f != NULL) {
304 if (ent->flags & MON_AUTH) {
305 ent->flags &= ~MON_PERMIT;
306 ent->flags |= permit ? MON_PERMIT : 0;
313 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
315 struct mon_table *ent;
316 int authenticated = 0;
318 debug3("preauth child monitor started");
320 authctxt = _authctxt;
321 memset(authctxt, 0, sizeof(*authctxt));
323 authctxt->loginmsg = &loginmsg;
326 mon_dispatch = mon_dispatch_proto20;
328 /* Permit requests for moduli and signatures */
329 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
330 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
332 mon_dispatch = mon_dispatch_proto15;
334 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
337 /* The first few requests do not require asynchronous access */
338 while (!authenticated) {
339 auth_method = "unknown";
340 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
342 if (!(ent->flags & MON_AUTHDECIDE))
343 fatal("%s: unexpected authentication from %d",
344 __func__, ent->type);
345 if (authctxt->pw->pw_uid == 0 &&
346 !auth_root_allowed(auth_method))
349 /* PAM needs to perform account checks after auth */
350 if (options.use_pam && authenticated) {
354 mm_request_receive_expect(pmonitor->m_sendfd,
355 MONITOR_REQ_PAM_ACCOUNT, &m);
356 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
362 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
363 auth_log(authctxt, authenticated, auth_method,
364 compat20 ? " ssh2" : "");
366 authctxt->failures++;
370 if (!authctxt->valid)
371 fatal("%s: authenticated invalid user", __func__);
372 if (strcmp(auth_method, "unknown") == 0)
373 fatal("%s: authentication method name unknown", __func__);
375 debug("%s: %s has been authenticated by privileged process",
376 __func__, authctxt->user);
378 mm_get_keystate(pmonitor);
382 monitor_set_child_handler(pid_t pid)
384 monitor_child_pid = pid;
388 monitor_child_handler(int sig)
390 kill(monitor_child_pid, sig);
394 monitor_child_postauth(struct monitor *pmonitor)
396 monitor_set_child_handler(pmonitor->m_pid);
397 signal(SIGHUP, &monitor_child_handler);
398 signal(SIGTERM, &monitor_child_handler);
401 mon_dispatch = mon_dispatch_postauth20;
403 /* Permit requests for moduli and signatures */
404 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
405 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
406 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
408 mon_dispatch = mon_dispatch_postauth15;
409 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
412 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
413 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
417 monitor_read(pmonitor, mon_dispatch, NULL);
421 monitor_sync(struct monitor *pmonitor)
423 if (options.compression) {
424 /* The member allocation is not visible, so sync it */
425 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
430 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
431 struct mon_table **pent)
439 mm_request_receive(pmonitor->m_sendfd, &m);
440 type = buffer_get_char(&m);
442 debug3("%s: checking request %d", __func__, type);
444 while (ent->f != NULL) {
445 if (ent->type == type)
450 if (ent->f != NULL) {
451 if (!(ent->flags & MON_PERMIT))
452 fatal("%s: unpermitted request %d", __func__,
454 ret = (*ent->f)(pmonitor->m_sendfd, &m);
457 /* The child may use this request only once, disable it */
458 if (ent->flags & MON_ONCE) {
459 debug2("%s: %d used once, disabling now", __func__,
461 ent->flags &= ~MON_PERMIT;
470 fatal("%s: unsupported request: %d", __func__, type);
476 /* allowed key state */
478 monitor_allowed_key(u_char *blob, u_int bloblen)
480 /* make sure key is allowed */
481 if (key_blob == NULL || key_bloblen != bloblen ||
482 memcmp(key_blob, blob, key_bloblen))
488 monitor_reset_key_state(void)
491 if (key_blob != NULL)
493 if (hostbased_cuser != NULL)
494 xfree(hostbased_cuser);
495 if (hostbased_chost != NULL)
496 xfree(hostbased_chost);
499 key_blobtype = MM_NOKEY;
500 hostbased_cuser = NULL;
501 hostbased_chost = NULL;
505 mm_answer_moduli(int sock, Buffer *m)
510 min = buffer_get_int(m);
511 want = buffer_get_int(m);
512 max = buffer_get_int(m);
514 debug3("%s: got parameters: %d %d %d",
515 __func__, min, want, max);
516 /* We need to check here, too, in case the child got corrupted */
517 if (max < min || want < min || max < want)
518 fatal("%s: bad parameters: %d %d %d",
519 __func__, min, want, max);
523 dh = choose_dh(min, want, max);
525 buffer_put_char(m, 0);
528 /* Send first bignum */
529 buffer_put_char(m, 1);
530 buffer_put_bignum2(m, dh->p);
531 buffer_put_bignum2(m, dh->g);
535 mm_request_send(sock, MONITOR_ANS_MODULI, m);
540 mm_answer_sign(int sock, Buffer *m)
545 u_int siglen, datlen;
548 debug3("%s", __func__);
550 keyid = buffer_get_int(m);
551 p = buffer_get_string(m, &datlen);
554 * Supported KEX types will only return SHA1 (20 byte) or
555 * SHA256 (32 byte) hashes
557 if (datlen != 20 && datlen != 32)
558 fatal("%s: data length incorrect: %u", __func__, datlen);
560 /* save session id, it will be passed on the first call */
561 if (session_id2_len == 0) {
562 session_id2_len = datlen;
563 session_id2 = xmalloc(session_id2_len);
564 memcpy(session_id2, p, session_id2_len);
567 if ((key = get_hostkey_by_index(keyid)) == NULL)
568 fatal("%s: no hostkey from index %d", __func__, keyid);
569 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
570 fatal("%s: key_sign failed", __func__);
572 debug3("%s: signature %p(%u)", __func__, signature, siglen);
575 buffer_put_string(m, signature, siglen);
580 mm_request_send(sock, MONITOR_ANS_SIGN, m);
582 /* Turn on permissions for getpwnam */
583 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
588 /* Retrieves the password entry and also checks if the user is permitted */
591 mm_answer_pwnamallow(int sock, Buffer *m)
594 struct passwd *pwent;
597 debug3("%s", __func__);
599 if (authctxt->attempt++ != 0)
600 fatal("%s: multiple attempts for getpwnam", __func__);
602 username = buffer_get_string(m, NULL);
604 pwent = getpwnamallow(username);
606 authctxt->user = xstrdup(username);
607 setproctitle("%s [priv]", pwent ? username : "unknown");
613 buffer_put_char(m, 0);
614 authctxt->pw = fakepw();
619 authctxt->pw = pwent;
622 buffer_put_char(m, 1);
623 buffer_put_string(m, pwent, sizeof(struct passwd));
624 buffer_put_cstring(m, pwent->pw_name);
625 buffer_put_cstring(m, "*");
626 buffer_put_cstring(m, pwent->pw_gecos);
627 #ifdef HAVE_PW_CLASS_IN_PASSWD
628 buffer_put_cstring(m, pwent->pw_class);
630 buffer_put_cstring(m, pwent->pw_dir);
631 buffer_put_cstring(m, pwent->pw_shell);
634 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
635 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
637 /* For SSHv1 allow authentication now */
639 monitor_permit_authentications(1);
641 /* Allow service/style information on the auth context */
642 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
643 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
648 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
650 #ifdef SSH_AUDIT_EVENTS
651 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
657 int mm_answer_auth2_read_banner(int sock, Buffer *m)
662 banner = auth2_read_banner();
663 buffer_put_cstring(m, banner != NULL ? banner : "");
664 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
673 mm_answer_authserv(int sock, Buffer *m)
675 monitor_permit_authentications(1);
677 authctxt->service = buffer_get_string(m, NULL);
678 authctxt->style = buffer_get_string(m, NULL);
679 debug3("%s: service=%s, style=%s",
680 __func__, authctxt->service, authctxt->style);
682 if (strlen(authctxt->style) == 0) {
683 xfree(authctxt->style);
684 authctxt->style = NULL;
691 mm_answer_authpassword(int sock, Buffer *m)
693 static int call_count;
698 passwd = buffer_get_string(m, &plen);
699 /* Only authenticate if the context is valid */
700 authenticated = options.password_authentication &&
701 auth_password(authctxt, passwd);
702 memset(passwd, 0, strlen(passwd));
706 buffer_put_int(m, authenticated);
708 debug3("%s: sending result %d", __func__, authenticated);
709 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
712 if (plen == 0 && call_count == 1)
713 auth_method = "none";
715 auth_method = "password";
717 /* Causes monitor loop to terminate if authenticated */
718 return (authenticated);
723 mm_answer_bsdauthquery(int sock, Buffer *m)
725 char *name, *infotxt;
731 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
732 &prompts, &echo_on) < 0 ? 0 : 1;
735 buffer_put_int(m, success);
737 buffer_put_cstring(m, prompts[0]);
739 debug3("%s: sending challenge success: %u", __func__, success);
740 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
753 mm_answer_bsdauthrespond(int sock, Buffer *m)
758 if (authctxt->as == 0)
759 fatal("%s: no bsd auth session", __func__);
761 response = buffer_get_string(m, NULL);
762 authok = options.challenge_response_authentication &&
763 auth_userresponse(authctxt->as, response, 0);
765 debug3("%s: <%s> = <%d>", __func__, response, authok);
769 buffer_put_int(m, authok);
771 debug3("%s: sending authenticated: %d", __func__, authok);
772 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
774 auth_method = "bsdauth";
776 return (authok != 0);
782 mm_answer_skeyquery(int sock, Buffer *m)
785 char challenge[1024];
788 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
789 sizeof(challenge)) < 0 ? 0 : 1;
792 buffer_put_int(m, success);
794 buffer_put_cstring(m, challenge);
796 debug3("%s: sending challenge success: %u", __func__, success);
797 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
803 mm_answer_skeyrespond(int sock, Buffer *m)
808 response = buffer_get_string(m, NULL);
810 authok = (options.challenge_response_authentication &&
812 skey_haskey(authctxt->pw->pw_name) == 0 &&
813 skey_passcheck(authctxt->pw->pw_name, response) != -1);
818 buffer_put_int(m, authok);
820 debug3("%s: sending authenticated: %d", __func__, authok);
821 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
823 auth_method = "skey";
825 return (authok != 0);
831 mm_answer_pam_start(int sock, Buffer *m)
833 if (!options.use_pam)
834 fatal("UsePAM not set, but ended up in %s anyway", __func__);
838 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
844 mm_answer_pam_account(int sock, Buffer *m)
848 if (!options.use_pam)
849 fatal("UsePAM not set, but ended up in %s anyway", __func__);
851 ret = do_pam_account();
853 buffer_put_int(m, ret);
854 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
856 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
861 static void *sshpam_ctxt, *sshpam_authok;
862 extern KbdintDevice sshpam_device;
865 mm_answer_pam_init_ctx(int sock, Buffer *m)
868 debug3("%s", __func__);
869 authctxt->user = buffer_get_string(m, NULL);
870 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
871 sshpam_authok = NULL;
873 if (sshpam_ctxt != NULL) {
874 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
875 buffer_put_int(m, 1);
877 buffer_put_int(m, 0);
879 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
884 mm_answer_pam_query(int sock, Buffer *m)
886 char *name, *info, **prompts;
887 u_int i, num, *echo_on;
890 debug3("%s", __func__);
891 sshpam_authok = NULL;
892 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
893 if (ret == 0 && num == 0)
894 sshpam_authok = sshpam_ctxt;
895 if (num > 1 || name == NULL || info == NULL)
898 buffer_put_int(m, ret);
899 buffer_put_cstring(m, name);
901 buffer_put_cstring(m, info);
903 buffer_put_int(m, num);
904 for (i = 0; i < num; ++i) {
905 buffer_put_cstring(m, prompts[i]);
907 buffer_put_int(m, echo_on[i]);
913 auth_method = "keyboard-interactive/pam";
914 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
919 mm_answer_pam_respond(int sock, Buffer *m)
925 debug3("%s", __func__);
926 sshpam_authok = NULL;
927 num = buffer_get_int(m);
929 resp = xcalloc(num, sizeof(char *));
930 for (i = 0; i < num; ++i)
931 resp[i] = buffer_get_string(m, NULL);
932 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
933 for (i = 0; i < num; ++i)
937 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
940 buffer_put_int(m, ret);
941 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
942 auth_method = "keyboard-interactive/pam";
944 sshpam_authok = sshpam_ctxt;
949 mm_answer_pam_free_ctx(int sock, Buffer *m)
952 debug3("%s", __func__);
953 (sshpam_device.free_ctx)(sshpam_ctxt);
955 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
956 auth_method = "keyboard-interactive/pam";
957 return (sshpam_authok == sshpam_ctxt);
962 mm_append_debug(Buffer *m)
964 if (auth_debug_init && buffer_len(&auth_debug)) {
965 debug3("%s: Appending debug messages for child", __func__);
966 buffer_append(m, buffer_ptr(&auth_debug),
967 buffer_len(&auth_debug));
968 buffer_clear(&auth_debug);
973 mm_answer_keyallowed(int sock, Buffer *m)
979 enum mm_keytype type = 0;
982 debug3("%s entering", __func__);
984 type = buffer_get_int(m);
985 cuser = buffer_get_string(m, NULL);
986 chost = buffer_get_string(m, NULL);
987 blob = buffer_get_string(m, &bloblen);
989 key = key_from_blob(blob, bloblen);
991 if ((compat20 && type == MM_RSAHOSTKEY) ||
992 (!compat20 && type != MM_RSAHOSTKEY))
993 fatal("%s: key type and protocol mismatch", __func__);
995 debug3("%s: key_from_blob: %p", __func__, key);
997 if (key != NULL && authctxt->valid) {
1000 allowed = options.pubkey_authentication &&
1001 user_key_allowed(authctxt->pw, key);
1002 auth_method = "publickey";
1005 allowed = options.hostbased_authentication &&
1006 hostbased_key_allowed(authctxt->pw,
1008 auth_method = "hostbased";
1011 key->type = KEY_RSA1; /* XXX */
1012 allowed = options.rhosts_rsa_authentication &&
1013 auth_rhosts_rsa_key_allowed(authctxt->pw,
1015 auth_method = "rsa";
1018 fatal("%s: unknown key type %d", __func__, type);
1025 /* clear temporarily storage (used by verify) */
1026 monitor_reset_key_state();
1029 /* Save temporarily for comparison in verify */
1031 key_bloblen = bloblen;
1032 key_blobtype = type;
1033 hostbased_cuser = cuser;
1034 hostbased_chost = chost;
1036 /* Log failed attempt */
1037 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1043 debug3("%s: key %p is %s",
1044 __func__, key, allowed ? "allowed" : "disallowed");
1047 buffer_put_int(m, allowed);
1048 buffer_put_int(m, forced_command != NULL);
1052 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1054 if (type == MM_RSAHOSTKEY)
1055 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1061 monitor_valid_userblob(u_char *data, u_int datalen)
1069 buffer_append(&b, data, datalen);
1071 if (datafellows & SSH_OLD_SESSIONID) {
1073 len = buffer_len(&b);
1074 if ((session_id2 == NULL) ||
1075 (len < session_id2_len) ||
1076 (memcmp(p, session_id2, session_id2_len) != 0))
1078 buffer_consume(&b, session_id2_len);
1080 p = buffer_get_string(&b, &len);
1081 if ((session_id2 == NULL) ||
1082 (len != session_id2_len) ||
1083 (memcmp(p, session_id2, session_id2_len) != 0))
1087 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1089 p = buffer_get_string(&b, NULL);
1090 if (strcmp(authctxt->user, p) != 0) {
1091 logit("wrong user name passed to monitor: expected %s != %.100s",
1096 buffer_skip_string(&b);
1097 if (datafellows & SSH_BUG_PKAUTH) {
1098 if (!buffer_get_char(&b))
1101 p = buffer_get_string(&b, NULL);
1102 if (strcmp("publickey", p) != 0)
1105 if (!buffer_get_char(&b))
1107 buffer_skip_string(&b);
1109 buffer_skip_string(&b);
1110 if (buffer_len(&b) != 0)
1117 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1126 buffer_append(&b, data, datalen);
1128 p = buffer_get_string(&b, &len);
1129 if ((session_id2 == NULL) ||
1130 (len != session_id2_len) ||
1131 (memcmp(p, session_id2, session_id2_len) != 0))
1135 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1137 p = buffer_get_string(&b, NULL);
1138 if (strcmp(authctxt->user, p) != 0) {
1139 logit("wrong user name passed to monitor: expected %s != %.100s",
1144 buffer_skip_string(&b); /* service */
1145 p = buffer_get_string(&b, NULL);
1146 if (strcmp(p, "hostbased") != 0)
1149 buffer_skip_string(&b); /* pkalg */
1150 buffer_skip_string(&b); /* pkblob */
1152 /* verify client host, strip trailing dot if necessary */
1153 p = buffer_get_string(&b, NULL);
1154 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1156 if (strcmp(p, chost) != 0)
1160 /* verify client user */
1161 p = buffer_get_string(&b, NULL);
1162 if (strcmp(p, cuser) != 0)
1166 if (buffer_len(&b) != 0)
1173 mm_answer_keyverify(int sock, Buffer *m)
1176 u_char *signature, *data, *blob;
1177 u_int signaturelen, datalen, bloblen;
1181 blob = buffer_get_string(m, &bloblen);
1182 signature = buffer_get_string(m, &signaturelen);
1183 data = buffer_get_string(m, &datalen);
1185 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1186 !monitor_allowed_key(blob, bloblen))
1187 fatal("%s: bad key, not previously allowed", __func__);
1189 key = key_from_blob(blob, bloblen);
1191 fatal("%s: bad public key blob", __func__);
1193 switch (key_blobtype) {
1195 valid_data = monitor_valid_userblob(data, datalen);
1198 valid_data = monitor_valid_hostbasedblob(data, datalen,
1199 hostbased_cuser, hostbased_chost);
1206 fatal("%s: bad signature data blob", __func__);
1208 verified = key_verify(key, signature, signaturelen, data, datalen);
1209 debug3("%s: key %p signature %s",
1210 __func__, key, verified ? "verified" : "unverified");
1217 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1219 monitor_reset_key_state();
1222 buffer_put_int(m, verified);
1223 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1229 mm_record_login(Session *s, struct passwd *pw)
1232 struct sockaddr_storage from;
1235 * Get IP address of client. If the connection is not a socket, let
1236 * the address be 0.0.0.0.
1238 memset(&from, 0, sizeof(from));
1239 fromlen = sizeof(from);
1240 if (packet_connection_is_on_socket()) {
1241 if (getpeername(packet_get_connection_in(),
1242 (struct sockaddr *)&from, &fromlen) < 0) {
1243 debug("getpeername: %.100s", strerror(errno));
1247 /* Record that there was a login on that tty from the remote host. */
1248 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1249 get_remote_name_or_ip(utmp_len, options.use_dns),
1250 (struct sockaddr *)&from, fromlen);
1254 mm_session_close(Session *s)
1256 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1257 if (s->ttyfd != -1) {
1258 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1259 session_pty_cleanup2(s);
1265 mm_answer_pty(int sock, Buffer *m)
1267 extern struct monitor *pmonitor;
1271 debug3("%s entering", __func__);
1277 s->authctxt = authctxt;
1278 s->pw = authctxt->pw;
1279 s->pid = pmonitor->m_pid;
1280 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1283 pty_setowner(authctxt->pw, s->tty);
1285 buffer_put_int(m, 1);
1286 buffer_put_cstring(m, s->tty);
1288 /* We need to trick ttyslot */
1289 if (dup2(s->ttyfd, 0) == -1)
1290 fatal("%s: dup2", __func__);
1292 mm_record_login(s, authctxt->pw);
1294 /* Now we can close the file descriptor again */
1297 /* send messages generated by record_login */
1298 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1299 buffer_clear(&loginmsg);
1301 mm_request_send(sock, MONITOR_ANS_PTY, m);
1303 mm_send_fd(sock, s->ptyfd);
1304 mm_send_fd(sock, s->ttyfd);
1306 /* make sure nothing uses fd 0 */
1307 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1308 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1310 error("%s: fd0 %d != 0", __func__, fd0);
1312 /* slave is not needed */
1314 s->ttyfd = s->ptyfd;
1315 /* no need to dup() because nobody closes ptyfd */
1316 s->ptymaster = s->ptyfd;
1318 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1324 mm_session_close(s);
1325 buffer_put_int(m, 0);
1326 mm_request_send(sock, MONITOR_ANS_PTY, m);
1331 mm_answer_pty_cleanup(int sock, Buffer *m)
1336 debug3("%s entering", __func__);
1338 tty = buffer_get_string(m, NULL);
1339 if ((s = session_by_tty(tty)) != NULL)
1340 mm_session_close(s);
1347 mm_answer_sesskey(int sock, Buffer *m)
1352 /* Turn off permissions */
1353 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1355 if ((p = BN_new()) == NULL)
1356 fatal("%s: BN_new", __func__);
1358 buffer_get_bignum2(m, p);
1360 rsafail = ssh1_session_key(p);
1363 buffer_put_int(m, rsafail);
1364 buffer_put_bignum2(m, p);
1368 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1370 /* Turn on permissions for sessid passing */
1371 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1377 mm_answer_sessid(int sock, Buffer *m)
1381 debug3("%s entering", __func__);
1383 if (buffer_len(m) != 16)
1384 fatal("%s: bad ssh1 session id", __func__);
1385 for (i = 0; i < 16; i++)
1386 session_id[i] = buffer_get_char(m);
1388 /* Turn on permissions for getpwnam */
1389 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1395 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1399 u_char *blob = NULL;
1403 debug3("%s entering", __func__);
1405 auth_method = "rsa";
1406 if (options.rsa_authentication && authctxt->valid) {
1407 if ((client_n = BN_new()) == NULL)
1408 fatal("%s: BN_new", __func__);
1409 buffer_get_bignum2(m, client_n);
1410 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1411 BN_clear_free(client_n);
1414 buffer_put_int(m, allowed);
1415 buffer_put_int(m, forced_command != NULL);
1417 /* clear temporarily storage (used by generate challenge) */
1418 monitor_reset_key_state();
1420 if (allowed && key != NULL) {
1421 key->type = KEY_RSA; /* cheat for key_to_blob */
1422 if (key_to_blob(key, &blob, &blen) == 0)
1423 fatal("%s: key_to_blob failed", __func__);
1424 buffer_put_string(m, blob, blen);
1426 /* Save temporarily for comparison in verify */
1429 key_blobtype = MM_RSAUSERKEY;
1436 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1438 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1439 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1444 mm_answer_rsa_challenge(int sock, Buffer *m)
1450 debug3("%s entering", __func__);
1452 if (!authctxt->valid)
1453 fatal("%s: authctxt not valid", __func__);
1454 blob = buffer_get_string(m, &blen);
1455 if (!monitor_allowed_key(blob, blen))
1456 fatal("%s: bad key, not previously allowed", __func__);
1457 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1458 fatal("%s: key type mismatch", __func__);
1459 if ((key = key_from_blob(blob, blen)) == NULL)
1460 fatal("%s: received bad key", __func__);
1463 BN_clear_free(ssh1_challenge);
1464 ssh1_challenge = auth_rsa_generate_challenge(key);
1467 buffer_put_bignum2(m, ssh1_challenge);
1469 debug3("%s sending reply", __func__);
1470 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1472 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1480 mm_answer_rsa_response(int sock, Buffer *m)
1483 u_char *blob, *response;
1487 debug3("%s entering", __func__);
1489 if (!authctxt->valid)
1490 fatal("%s: authctxt not valid", __func__);
1491 if (ssh1_challenge == NULL)
1492 fatal("%s: no ssh1_challenge", __func__);
1494 blob = buffer_get_string(m, &blen);
1495 if (!monitor_allowed_key(blob, blen))
1496 fatal("%s: bad key, not previously allowed", __func__);
1497 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1498 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1499 if ((key = key_from_blob(blob, blen)) == NULL)
1500 fatal("%s: received bad key", __func__);
1501 response = buffer_get_string(m, &len);
1503 fatal("%s: received bad response to challenge", __func__);
1504 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1510 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1513 BN_clear_free(ssh1_challenge);
1514 ssh1_challenge = NULL;
1515 monitor_reset_key_state();
1518 buffer_put_int(m, success);
1519 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1525 mm_answer_term(int sock, Buffer *req)
1527 extern struct monitor *pmonitor;
1530 debug3("%s: tearing down sessions", __func__);
1532 /* The child is terminating */
1533 session_destroy_all(&mm_session_close);
1535 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1539 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1541 /* Terminate process */
1545 #ifdef SSH_AUDIT_EVENTS
1546 /* Report that an audit event occurred */
1548 mm_answer_audit_event(int socket, Buffer *m)
1550 ssh_audit_event_t event;
1552 debug3("%s entering", __func__);
1554 event = buffer_get_int(m);
1556 case SSH_AUTH_FAIL_PUBKEY:
1557 case SSH_AUTH_FAIL_HOSTBASED:
1558 case SSH_AUTH_FAIL_GSSAPI:
1559 case SSH_LOGIN_EXCEED_MAXTRIES:
1560 case SSH_LOGIN_ROOT_DENIED:
1561 case SSH_CONNECTION_CLOSE:
1562 case SSH_INVALID_USER:
1566 fatal("Audit event type %d not permitted", event);
1573 mm_answer_audit_command(int socket, Buffer *m)
1578 debug3("%s entering", __func__);
1579 cmd = buffer_get_string(m, &len);
1580 /* sanity check command, if so how? */
1581 audit_run_command(cmd);
1585 #endif /* SSH_AUDIT_EVENTS */
1588 monitor_apply_keystate(struct monitor *pmonitor)
1591 set_newkeys(MODE_IN);
1592 set_newkeys(MODE_OUT);
1594 packet_set_protocol_flags(child_state.ssh1protoflags);
1595 packet_set_encryption_key(child_state.ssh1key,
1596 child_state.ssh1keylen, child_state.ssh1cipher);
1597 xfree(child_state.ssh1key);
1600 /* for rc4 and other stateful ciphers */
1601 packet_set_keycontext(MODE_OUT, child_state.keyout);
1602 xfree(child_state.keyout);
1603 packet_set_keycontext(MODE_IN, child_state.keyin);
1604 xfree(child_state.keyin);
1607 packet_set_iv(MODE_OUT, child_state.ivout);
1608 xfree(child_state.ivout);
1609 packet_set_iv(MODE_IN, child_state.ivin);
1610 xfree(child_state.ivin);
1613 memcpy(&incoming_stream, &child_state.incoming,
1614 sizeof(incoming_stream));
1615 memcpy(&outgoing_stream, &child_state.outgoing,
1616 sizeof(outgoing_stream));
1618 /* Update with new address */
1619 if (options.compression)
1620 mm_init_compression(pmonitor->m_zlib);
1622 /* Network I/O buffers */
1623 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1624 buffer_clear(&input);
1625 buffer_append(&input, child_state.input, child_state.ilen);
1626 memset(child_state.input, 0, child_state.ilen);
1627 xfree(child_state.input);
1629 buffer_clear(&output);
1630 buffer_append(&output, child_state.output, child_state.olen);
1631 memset(child_state.output, 0, child_state.olen);
1632 xfree(child_state.output);
1636 mm_get_kex(Buffer *m)
1642 kex = xcalloc(1, sizeof(*kex));
1643 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1644 if ((session_id2 == NULL) ||
1645 (kex->session_id_len != session_id2_len) ||
1646 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1647 fatal("mm_get_get: internal error: bad session id");
1648 kex->we_need = buffer_get_int(m);
1649 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1650 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1651 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1652 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1654 kex->hostkey_type = buffer_get_int(m);
1655 kex->kex_type = buffer_get_int(m);
1656 blob = buffer_get_string(m, &bloblen);
1657 buffer_init(&kex->my);
1658 buffer_append(&kex->my, blob, bloblen);
1660 blob = buffer_get_string(m, &bloblen);
1661 buffer_init(&kex->peer);
1662 buffer_append(&kex->peer, blob, bloblen);
1665 kex->flags = buffer_get_int(m);
1666 kex->client_version_string = buffer_get_string(m, NULL);
1667 kex->server_version_string = buffer_get_string(m, NULL);
1668 kex->load_host_key=&get_hostkey_by_type;
1669 kex->host_key_index=&get_hostkey_index;
1674 /* This function requries careful sanity checking */
1677 mm_get_keystate(struct monitor *pmonitor)
1681 u_int bloblen, plen;
1682 u_int32_t seqnr, packets;
1685 debug3("%s: Waiting for new keys", __func__);
1688 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1690 child_state.ssh1protoflags = buffer_get_int(&m);
1691 child_state.ssh1cipher = buffer_get_int(&m);
1692 child_state.ssh1key = buffer_get_string(&m,
1693 &child_state.ssh1keylen);
1694 child_state.ivout = buffer_get_string(&m,
1695 &child_state.ivoutlen);
1696 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1699 /* Get the Kex for rekeying */
1700 *pmonitor->m_pkex = mm_get_kex(&m);
1703 blob = buffer_get_string(&m, &bloblen);
1704 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1707 debug3("%s: Waiting for second key", __func__);
1708 blob = buffer_get_string(&m, &bloblen);
1709 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1712 /* Now get sequence numbers for the packets */
1713 seqnr = buffer_get_int(&m);
1714 blocks = buffer_get_int64(&m);
1715 packets = buffer_get_int(&m);
1716 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1717 seqnr = buffer_get_int(&m);
1718 blocks = buffer_get_int64(&m);
1719 packets = buffer_get_int(&m);
1720 packet_set_state(MODE_IN, seqnr, blocks, packets);
1723 /* Get the key context */
1724 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1725 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1727 debug3("%s: Getting compression state", __func__);
1728 /* Get compression state */
1729 p = buffer_get_string(&m, &plen);
1730 if (plen != sizeof(child_state.outgoing))
1731 fatal("%s: bad request size", __func__);
1732 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1735 p = buffer_get_string(&m, &plen);
1736 if (plen != sizeof(child_state.incoming))
1737 fatal("%s: bad request size", __func__);
1738 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1741 /* Network I/O buffers */
1742 debug3("%s: Getting Network I/O buffers", __func__);
1743 child_state.input = buffer_get_string(&m, &child_state.ilen);
1744 child_state.output = buffer_get_string(&m, &child_state.olen);
1750 /* Allocation functions for zlib */
1752 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1754 size_t len = (size_t) size * ncount;
1757 if (len == 0 || ncount > SIZE_T_MAX / size)
1758 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1760 address = mm_malloc(mm, len);
1766 mm_zfree(struct mm_master *mm, void *address)
1768 mm_free(mm, address);
1772 mm_init_compression(struct mm_master *mm)
1774 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1775 outgoing_stream.zfree = (free_func)mm_zfree;
1776 outgoing_stream.opaque = mm;
1778 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1779 incoming_stream.zfree = (free_func)mm_zfree;
1780 incoming_stream.opaque = mm;
1785 #define FD_CLOSEONEXEC(x) do { \
1786 if (fcntl(x, F_SETFD, 1) == -1) \
1787 fatal("fcntl(%d, F_SETFD)", x); \
1791 monitor_socketpair(int *pair)
1793 #ifdef HAVE_SOCKETPAIR
1794 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1795 fatal("%s: socketpair", __func__);
1797 fatal("%s: UsePrivilegeSeparation=yes not supported",
1800 FD_CLOSEONEXEC(pair[0]);
1801 FD_CLOSEONEXEC(pair[1]);
1804 #define MM_MEMSIZE 65536
1809 struct monitor *mon;
1812 mon = xcalloc(1, sizeof(*mon));
1814 monitor_socketpair(pair);
1816 mon->m_recvfd = pair[0];
1817 mon->m_sendfd = pair[1];
1819 /* Used to share zlib space across processes */
1820 if (options.compression) {
1821 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1822 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1824 /* Compression needs to share state across borders */
1825 mm_init_compression(mon->m_zlib);
1832 monitor_reinit(struct monitor *mon)
1836 monitor_socketpair(pair);
1838 mon->m_recvfd = pair[0];
1839 mon->m_sendfd = pair[1];
1844 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1850 goid.elements = buffer_get_string(m, &len);
1853 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1855 xfree(goid.elements);
1858 buffer_put_int(m, major);
1860 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1862 /* Now we have a context, enable the step */
1863 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1869 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1872 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1873 OM_uint32 major, minor;
1874 OM_uint32 flags = 0; /* GSI needs this */
1877 in.value = buffer_get_string(m, &len);
1879 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1883 buffer_put_int(m, major);
1884 buffer_put_string(m, out.value, out.length);
1885 buffer_put_int(m, flags);
1886 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1888 gss_release_buffer(&minor, &out);
1890 if (major == GSS_S_COMPLETE) {
1891 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1892 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1893 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1899 mm_answer_gss_checkmic(int sock, Buffer *m)
1901 gss_buffer_desc gssbuf, mic;
1905 gssbuf.value = buffer_get_string(m, &len);
1906 gssbuf.length = len;
1907 mic.value = buffer_get_string(m, &len);
1910 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1912 xfree(gssbuf.value);
1916 buffer_put_int(m, ret);
1918 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1920 if (!GSS_ERROR(ret))
1921 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1927 mm_answer_gss_userok(int sock, Buffer *m)
1931 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1934 buffer_put_int(m, authenticated);
1936 debug3("%s: sending result %d", __func__, authenticated);
1937 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1939 auth_method = "gssapi-with-mic";
1941 /* Monitor loop will terminate if authenticated */
1942 return (authenticated);