1 /* $OpenBSD: monitor.c,v 1.96 2008/05/08 12:21:16 djm 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>
33 #include "openbsd-compat/sys-tree.h"
52 #include <openssl/dh.h>
54 #include "openbsd-compat/sys-queue.h"
64 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
67 #define TARGET_OS_MAC 1
72 #include "auth-options.h"
81 #include "monitor_mm.h"
85 #include "monitor_wrap.h"
86 #include "monitor_fdpass.h"
92 static Gssctxt *gsscontext = NULL;
96 extern ServerOptions options;
97 extern u_int utmp_len;
98 extern Newkeys *current_keys[];
99 extern z_stream incoming_stream;
100 extern z_stream outgoing_stream;
101 extern u_char session_id[];
102 extern Buffer input, output;
103 extern Buffer auth_debug;
104 extern int auth_debug_init;
105 extern Buffer loginmsg;
107 /* State exported from the child */
130 /* Functions on the monitor that answer unprivileged requests */
132 int mm_answer_moduli(int, Buffer *);
133 int mm_answer_sign(int, Buffer *);
134 int mm_answer_pwnamallow(int, Buffer *);
135 int mm_answer_auth2_read_banner(int, Buffer *);
136 int mm_answer_authserv(int, Buffer *);
137 int mm_answer_authpassword(int, Buffer *);
138 int mm_answer_bsdauthquery(int, Buffer *);
139 int mm_answer_bsdauthrespond(int, Buffer *);
140 int mm_answer_skeyquery(int, Buffer *);
141 int mm_answer_skeyrespond(int, Buffer *);
142 int mm_answer_keyallowed(int, Buffer *);
143 int mm_answer_keyverify(int, Buffer *);
144 int mm_answer_pty(int, Buffer *);
145 int mm_answer_pty_cleanup(int, Buffer *);
146 int mm_answer_term(int, Buffer *);
147 int mm_answer_rsa_keyallowed(int, Buffer *);
148 int mm_answer_rsa_challenge(int, Buffer *);
149 int mm_answer_rsa_response(int, Buffer *);
150 int mm_answer_sesskey(int, Buffer *);
151 int mm_answer_sessid(int, Buffer *);
154 int mm_answer_pam_start(int, Buffer *);
155 int mm_answer_pam_account(int, Buffer *);
156 int mm_answer_pam_init_ctx(int, Buffer *);
157 int mm_answer_pam_query(int, Buffer *);
158 int mm_answer_pam_respond(int, Buffer *);
159 int mm_answer_pam_free_ctx(int, Buffer *);
163 int mm_answer_gss_setup_ctx(int, Buffer *);
164 int mm_answer_gss_accept_ctx(int, Buffer *);
165 int mm_answer_gss_userok(int, Buffer *);
166 int mm_answer_gss_checkmic(int, Buffer *);
169 #ifdef SSH_AUDIT_EVENTS
170 int mm_answer_audit_event(int, Buffer *);
171 int mm_answer_audit_command(int, Buffer *);
174 static Authctxt *authctxt;
175 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
177 /* local state for key verify */
178 static u_char *key_blob = NULL;
179 static u_int key_bloblen = 0;
180 static int key_blobtype = MM_NOKEY;
181 static char *hostbased_cuser = NULL;
182 static char *hostbased_chost = NULL;
183 static char *auth_method = "unknown";
184 static u_int session_id2_len = 0;
185 static u_char *session_id2 = NULL;
186 static pid_t monitor_child_pid;
189 enum monitor_reqtype type;
191 int (*f)(int, Buffer *);
194 #define MON_ISAUTH 0x0004 /* Required for Authentication */
195 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
196 #define MON_ONCE 0x0010 /* Disable after calling */
197 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
199 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
201 #define MON_PERMIT 0x1000 /* Request is permitted */
203 struct mon_table mon_dispatch_proto20[] = {
204 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
205 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
206 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
207 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
208 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
209 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
211 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
212 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
213 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
214 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
215 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
216 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
218 #ifdef SSH_AUDIT_EVENTS
219 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
222 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
223 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
226 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
227 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
229 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
230 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
232 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
233 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
234 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
235 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
240 struct mon_table mon_dispatch_postauth20[] = {
241 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
242 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
243 {MONITOR_REQ_PTY, 0, mm_answer_pty},
244 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
245 {MONITOR_REQ_TERM, 0, mm_answer_term},
246 #ifdef SSH_AUDIT_EVENTS
247 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
248 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
253 struct mon_table mon_dispatch_proto15[] = {
254 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
255 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
256 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
257 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
258 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
259 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
260 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
261 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
263 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
264 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
267 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
268 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
271 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
272 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
273 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
274 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
275 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
276 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
278 #ifdef SSH_AUDIT_EVENTS
279 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
284 struct mon_table mon_dispatch_postauth15[] = {
285 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
286 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
287 {MONITOR_REQ_TERM, 0, mm_answer_term},
288 #ifdef SSH_AUDIT_EVENTS
289 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
290 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
295 struct mon_table *mon_dispatch;
297 /* Specifies if a certain message is allowed at the moment */
300 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
302 while (ent->f != NULL) {
303 if (ent->type == type) {
304 ent->flags &= ~MON_PERMIT;
305 ent->flags |= permit ? MON_PERMIT : 0;
313 monitor_permit_authentications(int permit)
315 struct mon_table *ent = mon_dispatch;
317 while (ent->f != NULL) {
318 if (ent->flags & MON_AUTH) {
319 ent->flags &= ~MON_PERMIT;
320 ent->flags |= permit ? MON_PERMIT : 0;
327 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
329 struct mon_table *ent;
330 int authenticated = 0;
332 debug3("preauth child monitor started");
334 authctxt = _authctxt;
335 memset(authctxt, 0, sizeof(*authctxt));
337 authctxt->loginmsg = &loginmsg;
340 mon_dispatch = mon_dispatch_proto20;
342 /* Permit requests for moduli and signatures */
343 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
344 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
346 mon_dispatch = mon_dispatch_proto15;
348 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
351 /* The first few requests do not require asynchronous access */
352 while (!authenticated) {
353 auth_method = "unknown";
354 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
356 if (!(ent->flags & MON_AUTHDECIDE))
357 fatal("%s: unexpected authentication from %d",
358 __func__, ent->type);
359 if (authctxt->pw->pw_uid == 0 &&
360 !auth_root_allowed(auth_method))
363 /* PAM needs to perform account checks after auth */
364 if (options.use_pam && authenticated) {
368 mm_request_receive_expect(pmonitor->m_sendfd,
369 MONITOR_REQ_PAM_ACCOUNT, &m);
370 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
376 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
377 auth_log(authctxt, authenticated, auth_method,
378 compat20 ? " ssh2" : "");
380 authctxt->failures++;
384 if (!authctxt->valid)
385 fatal("%s: authenticated invalid user", __func__);
386 if (strcmp(auth_method, "unknown") == 0)
387 fatal("%s: authentication method name unknown", __func__);
389 debug("%s: %s has been authenticated by privileged process",
390 __func__, authctxt->user);
392 mm_get_keystate(pmonitor);
396 monitor_set_child_handler(pid_t pid)
398 monitor_child_pid = pid;
402 monitor_child_handler(int sig)
404 kill(monitor_child_pid, sig);
408 monitor_child_postauth(struct monitor *pmonitor)
410 monitor_set_child_handler(pmonitor->m_pid);
411 signal(SIGHUP, &monitor_child_handler);
412 signal(SIGTERM, &monitor_child_handler);
413 signal(SIGINT, &monitor_child_handler);
416 mon_dispatch = mon_dispatch_postauth20;
418 /* Permit requests for moduli and signatures */
419 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
420 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
421 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
423 mon_dispatch = mon_dispatch_postauth15;
424 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
427 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
428 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
432 monitor_read(pmonitor, mon_dispatch, NULL);
436 monitor_sync(struct monitor *pmonitor)
438 if (options.compression) {
439 /* The member allocation is not visible, so sync it */
440 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
445 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
446 struct mon_table **pent)
454 mm_request_receive(pmonitor->m_sendfd, &m);
455 type = buffer_get_char(&m);
457 debug3("%s: checking request %d", __func__, type);
459 while (ent->f != NULL) {
460 if (ent->type == type)
465 if (ent->f != NULL) {
466 if (!(ent->flags & MON_PERMIT))
467 fatal("%s: unpermitted request %d", __func__,
469 ret = (*ent->f)(pmonitor->m_sendfd, &m);
472 /* The child may use this request only once, disable it */
473 if (ent->flags & MON_ONCE) {
474 debug2("%s: %d used once, disabling now", __func__,
476 ent->flags &= ~MON_PERMIT;
485 fatal("%s: unsupported request: %d", __func__, type);
491 /* allowed key state */
493 monitor_allowed_key(u_char *blob, u_int bloblen)
495 /* make sure key is allowed */
496 if (key_blob == NULL || key_bloblen != bloblen ||
497 memcmp(key_blob, blob, key_bloblen))
503 monitor_reset_key_state(void)
506 if (key_blob != NULL)
508 if (hostbased_cuser != NULL)
509 xfree(hostbased_cuser);
510 if (hostbased_chost != NULL)
511 xfree(hostbased_chost);
514 key_blobtype = MM_NOKEY;
515 hostbased_cuser = NULL;
516 hostbased_chost = NULL;
520 mm_answer_moduli(int sock, Buffer *m)
525 min = buffer_get_int(m);
526 want = buffer_get_int(m);
527 max = buffer_get_int(m);
529 debug3("%s: got parameters: %d %d %d",
530 __func__, min, want, max);
531 /* We need to check here, too, in case the child got corrupted */
532 if (max < min || want < min || max < want)
533 fatal("%s: bad parameters: %d %d %d",
534 __func__, min, want, max);
538 dh = choose_dh(min, want, max);
540 buffer_put_char(m, 0);
543 /* Send first bignum */
544 buffer_put_char(m, 1);
545 buffer_put_bignum2(m, dh->p);
546 buffer_put_bignum2(m, dh->g);
550 mm_request_send(sock, MONITOR_ANS_MODULI, m);
555 mm_answer_sign(int sock, Buffer *m)
560 u_int siglen, datlen;
563 debug3("%s", __func__);
565 keyid = buffer_get_int(m);
566 p = buffer_get_string(m, &datlen);
569 * Supported KEX types will only return SHA1 (20 byte) or
570 * SHA256 (32 byte) hashes
572 if (datlen != 20 && datlen != 32)
573 fatal("%s: data length incorrect: %u", __func__, datlen);
575 /* save session id, it will be passed on the first call */
576 if (session_id2_len == 0) {
577 session_id2_len = datlen;
578 session_id2 = xmalloc(session_id2_len);
579 memcpy(session_id2, p, session_id2_len);
582 if ((key = get_hostkey_by_index(keyid)) == NULL)
583 fatal("%s: no hostkey from index %d", __func__, keyid);
584 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
585 fatal("%s: key_sign failed", __func__);
587 debug3("%s: signature %p(%u)", __func__, signature, siglen);
590 buffer_put_string(m, signature, siglen);
595 mm_request_send(sock, MONITOR_ANS_SIGN, m);
597 /* Turn on permissions for getpwnam */
598 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
603 /* Retrieves the password entry and also checks if the user is permitted */
606 mm_answer_pwnamallow(int sock, Buffer *m)
609 struct passwd *pwent;
612 debug3("%s", __func__);
614 if (authctxt->attempt++ != 0)
615 fatal("%s: multiple attempts for getpwnam", __func__);
617 username = buffer_get_string(m, NULL);
619 pwent = getpwnamallow(username);
621 authctxt->user = xstrdup(username);
622 setproctitle("%s [priv]", pwent ? username : "unknown");
628 buffer_put_char(m, 0);
629 authctxt->pw = fakepw();
634 authctxt->pw = pwent;
637 buffer_put_char(m, 1);
638 buffer_put_string(m, pwent, sizeof(struct passwd));
639 buffer_put_cstring(m, pwent->pw_name);
640 buffer_put_cstring(m, "*");
641 buffer_put_cstring(m, pwent->pw_gecos);
642 #ifdef HAVE_PW_CLASS_IN_PASSWD
643 buffer_put_cstring(m, pwent->pw_class);
645 buffer_put_cstring(m, pwent->pw_dir);
646 buffer_put_cstring(m, pwent->pw_shell);
649 buffer_put_string(m, &options, sizeof(options));
650 if (options.banner != NULL)
651 buffer_put_cstring(m, options.banner);
652 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
653 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
655 /* For SSHv1 allow authentication now */
657 monitor_permit_authentications(1);
659 /* Allow service/style information on the auth context */
660 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
661 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
666 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
672 int mm_answer_auth2_read_banner(int sock, Buffer *m)
677 banner = auth2_read_banner();
678 buffer_put_cstring(m, banner != NULL ? banner : "");
679 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
688 mm_answer_authserv(int sock, Buffer *m)
690 monitor_permit_authentications(1);
692 authctxt->service = buffer_get_string(m, NULL);
693 authctxt->style = buffer_get_string(m, NULL);
694 debug3("%s: service=%s, style=%s",
695 __func__, authctxt->service, authctxt->style);
697 if (strlen(authctxt->style) == 0) {
698 xfree(authctxt->style);
699 authctxt->style = NULL;
706 mm_answer_authpassword(int sock, Buffer *m)
708 static int call_count;
713 passwd = buffer_get_string(m, &plen);
714 /* Only authenticate if the context is valid */
715 authenticated = options.password_authentication &&
716 auth_password(authctxt, passwd);
717 memset(passwd, 0, strlen(passwd));
721 buffer_put_int(m, authenticated);
723 debug3("%s: sending result %d", __func__, authenticated);
724 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
727 if (plen == 0 && call_count == 1)
728 auth_method = "none";
730 auth_method = "password";
732 /* Causes monitor loop to terminate if authenticated */
733 return (authenticated);
738 mm_answer_bsdauthquery(int sock, Buffer *m)
740 char *name, *infotxt;
746 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
747 &prompts, &echo_on) < 0 ? 0 : 1;
750 buffer_put_int(m, success);
752 buffer_put_cstring(m, prompts[0]);
754 debug3("%s: sending challenge success: %u", __func__, success);
755 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
768 mm_answer_bsdauthrespond(int sock, Buffer *m)
773 if (authctxt->as == 0)
774 fatal("%s: no bsd auth session", __func__);
776 response = buffer_get_string(m, NULL);
777 authok = options.challenge_response_authentication &&
778 auth_userresponse(authctxt->as, response, 0);
780 debug3("%s: <%s> = <%d>", __func__, response, authok);
784 buffer_put_int(m, authok);
786 debug3("%s: sending authenticated: %d", __func__, authok);
787 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
789 auth_method = "bsdauth";
791 return (authok != 0);
797 mm_answer_skeyquery(int sock, Buffer *m)
800 char challenge[1024];
803 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
804 sizeof(challenge)) < 0 ? 0 : 1;
807 buffer_put_int(m, success);
809 buffer_put_cstring(m, challenge);
811 debug3("%s: sending challenge success: %u", __func__, success);
812 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
818 mm_answer_skeyrespond(int sock, Buffer *m)
823 response = buffer_get_string(m, NULL);
825 authok = (options.challenge_response_authentication &&
827 skey_haskey(authctxt->pw->pw_name) == 0 &&
828 skey_passcheck(authctxt->pw->pw_name, response) != -1);
833 buffer_put_int(m, authok);
835 debug3("%s: sending authenticated: %d", __func__, authok);
836 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
838 auth_method = "skey";
840 return (authok != 0);
846 mm_answer_pam_start(int sock, Buffer *m)
848 if (!options.use_pam)
849 fatal("UsePAM not set, but ended up in %s anyway", __func__);
853 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
859 mm_answer_pam_account(int sock, Buffer *m)
863 if (!options.use_pam)
864 fatal("UsePAM not set, but ended up in %s anyway", __func__);
866 ret = do_pam_account();
868 buffer_put_int(m, ret);
869 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
871 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
876 static void *sshpam_ctxt, *sshpam_authok;
877 extern KbdintDevice sshpam_device;
880 mm_answer_pam_init_ctx(int sock, Buffer *m)
883 debug3("%s", __func__);
884 authctxt->user = buffer_get_string(m, NULL);
885 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
886 sshpam_authok = NULL;
888 if (sshpam_ctxt != NULL) {
889 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
890 buffer_put_int(m, 1);
892 buffer_put_int(m, 0);
894 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
899 mm_answer_pam_query(int sock, Buffer *m)
901 char *name, *info, **prompts;
902 u_int i, num, *echo_on;
905 debug3("%s", __func__);
906 sshpam_authok = NULL;
907 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
908 if (ret == 0 && num == 0)
909 sshpam_authok = sshpam_ctxt;
910 if (num > 1 || name == NULL || info == NULL)
913 buffer_put_int(m, ret);
914 buffer_put_cstring(m, name);
916 buffer_put_cstring(m, info);
918 buffer_put_int(m, num);
919 for (i = 0; i < num; ++i) {
920 buffer_put_cstring(m, prompts[i]);
922 buffer_put_int(m, echo_on[i]);
928 auth_method = "keyboard-interactive/pam";
929 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
934 mm_answer_pam_respond(int sock, Buffer *m)
940 debug3("%s", __func__);
941 sshpam_authok = NULL;
942 num = buffer_get_int(m);
944 resp = xcalloc(num, sizeof(char *));
945 for (i = 0; i < num; ++i)
946 resp[i] = buffer_get_string(m, NULL);
947 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
948 for (i = 0; i < num; ++i)
952 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
955 buffer_put_int(m, ret);
956 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
957 auth_method = "keyboard-interactive/pam";
959 sshpam_authok = sshpam_ctxt;
964 mm_answer_pam_free_ctx(int sock, Buffer *m)
967 debug3("%s", __func__);
968 (sshpam_device.free_ctx)(sshpam_ctxt);
970 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
971 auth_method = "keyboard-interactive/pam";
972 return (sshpam_authok == sshpam_ctxt);
977 mm_append_debug(Buffer *m)
979 if (auth_debug_init && buffer_len(&auth_debug)) {
980 debug3("%s: Appending debug messages for child", __func__);
981 buffer_append(m, buffer_ptr(&auth_debug),
982 buffer_len(&auth_debug));
983 buffer_clear(&auth_debug);
988 mm_answer_keyallowed(int sock, Buffer *m)
994 enum mm_keytype type = 0;
997 debug3("%s entering", __func__);
999 type = buffer_get_int(m);
1000 cuser = buffer_get_string(m, NULL);
1001 chost = buffer_get_string(m, NULL);
1002 blob = buffer_get_string(m, &bloblen);
1004 key = key_from_blob(blob, bloblen);
1006 if ((compat20 && type == MM_RSAHOSTKEY) ||
1007 (!compat20 && type != MM_RSAHOSTKEY))
1008 fatal("%s: key type and protocol mismatch", __func__);
1010 debug3("%s: key_from_blob: %p", __func__, key);
1012 if (key != NULL && authctxt->valid) {
1015 allowed = options.pubkey_authentication &&
1016 user_key_allowed(authctxt->pw, key);
1017 auth_method = "publickey";
1020 allowed = options.hostbased_authentication &&
1021 hostbased_key_allowed(authctxt->pw,
1023 auth_method = "hostbased";
1026 key->type = KEY_RSA1; /* XXX */
1027 allowed = options.rhosts_rsa_authentication &&
1028 auth_rhosts_rsa_key_allowed(authctxt->pw,
1030 auth_method = "rsa";
1033 fatal("%s: unknown key type %d", __func__, type);
1040 /* clear temporarily storage (used by verify) */
1041 monitor_reset_key_state();
1044 /* Save temporarily for comparison in verify */
1046 key_bloblen = bloblen;
1047 key_blobtype = type;
1048 hostbased_cuser = cuser;
1049 hostbased_chost = chost;
1051 /* Log failed attempt */
1052 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1058 debug3("%s: key %p is %s",
1059 __func__, key, allowed ? "allowed" : "disallowed");
1062 buffer_put_int(m, allowed);
1063 buffer_put_int(m, forced_command != NULL);
1067 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1069 if (type == MM_RSAHOSTKEY)
1070 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1076 monitor_valid_userblob(u_char *data, u_int datalen)
1084 buffer_append(&b, data, datalen);
1086 if (datafellows & SSH_OLD_SESSIONID) {
1088 len = buffer_len(&b);
1089 if ((session_id2 == NULL) ||
1090 (len < session_id2_len) ||
1091 (memcmp(p, session_id2, session_id2_len) != 0))
1093 buffer_consume(&b, session_id2_len);
1095 p = buffer_get_string(&b, &len);
1096 if ((session_id2 == NULL) ||
1097 (len != session_id2_len) ||
1098 (memcmp(p, session_id2, session_id2_len) != 0))
1102 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1104 p = buffer_get_string(&b, NULL);
1105 if (strcmp(authctxt->user, p) != 0) {
1106 logit("wrong user name passed to monitor: expected %s != %.100s",
1111 buffer_skip_string(&b);
1112 if (datafellows & SSH_BUG_PKAUTH) {
1113 if (!buffer_get_char(&b))
1116 p = buffer_get_string(&b, NULL);
1117 if (strcmp("publickey", p) != 0)
1120 if (!buffer_get_char(&b))
1122 buffer_skip_string(&b);
1124 buffer_skip_string(&b);
1125 if (buffer_len(&b) != 0)
1132 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1141 buffer_append(&b, data, datalen);
1143 p = buffer_get_string(&b, &len);
1144 if ((session_id2 == NULL) ||
1145 (len != session_id2_len) ||
1146 (memcmp(p, session_id2, session_id2_len) != 0))
1150 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1152 p = buffer_get_string(&b, NULL);
1153 if (strcmp(authctxt->user, p) != 0) {
1154 logit("wrong user name passed to monitor: expected %s != %.100s",
1159 buffer_skip_string(&b); /* service */
1160 p = buffer_get_string(&b, NULL);
1161 if (strcmp(p, "hostbased") != 0)
1164 buffer_skip_string(&b); /* pkalg */
1165 buffer_skip_string(&b); /* pkblob */
1167 /* verify client host, strip trailing dot if necessary */
1168 p = buffer_get_string(&b, NULL);
1169 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1171 if (strcmp(p, chost) != 0)
1175 /* verify client user */
1176 p = buffer_get_string(&b, NULL);
1177 if (strcmp(p, cuser) != 0)
1181 if (buffer_len(&b) != 0)
1188 mm_answer_keyverify(int sock, Buffer *m)
1191 u_char *signature, *data, *blob;
1192 u_int signaturelen, datalen, bloblen;
1196 blob = buffer_get_string(m, &bloblen);
1197 signature = buffer_get_string(m, &signaturelen);
1198 data = buffer_get_string(m, &datalen);
1200 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1201 !monitor_allowed_key(blob, bloblen))
1202 fatal("%s: bad key, not previously allowed", __func__);
1204 key = key_from_blob(blob, bloblen);
1206 fatal("%s: bad public key blob", __func__);
1208 switch (key_blobtype) {
1210 valid_data = monitor_valid_userblob(data, datalen);
1213 valid_data = monitor_valid_hostbasedblob(data, datalen,
1214 hostbased_cuser, hostbased_chost);
1221 fatal("%s: bad signature data blob", __func__);
1223 verified = key_verify(key, signature, signaturelen, data, datalen);
1224 debug3("%s: key %p signature %s",
1225 __func__, key, (verified == 1) ? "verified" : "unverified");
1232 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1234 monitor_reset_key_state();
1237 buffer_put_int(m, verified);
1238 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1240 return (verified == 1);
1244 mm_record_login(Session *s, struct passwd *pw)
1247 struct sockaddr_storage from;
1250 * Get IP address of client. If the connection is not a socket, let
1251 * the address be 0.0.0.0.
1253 memset(&from, 0, sizeof(from));
1254 fromlen = sizeof(from);
1255 if (packet_connection_is_on_socket()) {
1256 if (getpeername(packet_get_connection_in(),
1257 (struct sockaddr *)&from, &fromlen) < 0) {
1258 debug("getpeername: %.100s", strerror(errno));
1262 /* Record that there was a login on that tty from the remote host. */
1263 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1264 get_remote_name_or_ip(utmp_len, options.use_dns),
1265 (struct sockaddr *)&from, fromlen);
1269 mm_session_close(Session *s)
1271 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1272 if (s->ttyfd != -1) {
1273 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1274 session_pty_cleanup2(s);
1276 session_unused(s->self);
1280 mm_answer_pty(int sock, Buffer *m)
1282 extern struct monitor *pmonitor;
1286 debug3("%s entering", __func__);
1292 s->authctxt = authctxt;
1293 s->pw = authctxt->pw;
1294 s->pid = pmonitor->m_pid;
1295 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1298 pty_setowner(authctxt->pw, s->tty);
1300 buffer_put_int(m, 1);
1301 buffer_put_cstring(m, s->tty);
1303 /* We need to trick ttyslot */
1304 if (dup2(s->ttyfd, 0) == -1)
1305 fatal("%s: dup2", __func__);
1307 mm_record_login(s, authctxt->pw);
1309 /* Now we can close the file descriptor again */
1312 /* send messages generated by record_login */
1313 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1314 buffer_clear(&loginmsg);
1316 mm_request_send(sock, MONITOR_ANS_PTY, m);
1318 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1319 mm_send_fd(sock, s->ttyfd) == -1)
1320 fatal("%s: send fds failed", __func__);
1322 /* make sure nothing uses fd 0 */
1323 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1324 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1326 error("%s: fd0 %d != 0", __func__, fd0);
1328 /* slave is not needed */
1330 s->ttyfd = s->ptyfd;
1331 /* no need to dup() because nobody closes ptyfd */
1332 s->ptymaster = s->ptyfd;
1334 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1340 mm_session_close(s);
1341 buffer_put_int(m, 0);
1342 mm_request_send(sock, MONITOR_ANS_PTY, m);
1347 mm_answer_pty_cleanup(int sock, Buffer *m)
1352 debug3("%s entering", __func__);
1354 tty = buffer_get_string(m, NULL);
1355 if ((s = session_by_tty(tty)) != NULL)
1356 mm_session_close(s);
1363 mm_answer_sesskey(int sock, Buffer *m)
1368 /* Turn off permissions */
1369 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1371 if ((p = BN_new()) == NULL)
1372 fatal("%s: BN_new", __func__);
1374 buffer_get_bignum2(m, p);
1376 rsafail = ssh1_session_key(p);
1379 buffer_put_int(m, rsafail);
1380 buffer_put_bignum2(m, p);
1384 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1386 /* Turn on permissions for sessid passing */
1387 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1393 mm_answer_sessid(int sock, Buffer *m)
1397 debug3("%s entering", __func__);
1399 if (buffer_len(m) != 16)
1400 fatal("%s: bad ssh1 session id", __func__);
1401 for (i = 0; i < 16; i++)
1402 session_id[i] = buffer_get_char(m);
1404 /* Turn on permissions for getpwnam */
1405 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1411 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1415 u_char *blob = NULL;
1419 debug3("%s entering", __func__);
1421 auth_method = "rsa";
1422 if (options.rsa_authentication && authctxt->valid) {
1423 if ((client_n = BN_new()) == NULL)
1424 fatal("%s: BN_new", __func__);
1425 buffer_get_bignum2(m, client_n);
1426 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1427 BN_clear_free(client_n);
1430 buffer_put_int(m, allowed);
1431 buffer_put_int(m, forced_command != NULL);
1433 /* clear temporarily storage (used by generate challenge) */
1434 monitor_reset_key_state();
1436 if (allowed && key != NULL) {
1437 key->type = KEY_RSA; /* cheat for key_to_blob */
1438 if (key_to_blob(key, &blob, &blen) == 0)
1439 fatal("%s: key_to_blob failed", __func__);
1440 buffer_put_string(m, blob, blen);
1442 /* Save temporarily for comparison in verify */
1445 key_blobtype = MM_RSAUSERKEY;
1452 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1454 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1455 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1460 mm_answer_rsa_challenge(int sock, Buffer *m)
1466 debug3("%s entering", __func__);
1468 if (!authctxt->valid)
1469 fatal("%s: authctxt not valid", __func__);
1470 blob = buffer_get_string(m, &blen);
1471 if (!monitor_allowed_key(blob, blen))
1472 fatal("%s: bad key, not previously allowed", __func__);
1473 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1474 fatal("%s: key type mismatch", __func__);
1475 if ((key = key_from_blob(blob, blen)) == NULL)
1476 fatal("%s: received bad key", __func__);
1479 BN_clear_free(ssh1_challenge);
1480 ssh1_challenge = auth_rsa_generate_challenge(key);
1483 buffer_put_bignum2(m, ssh1_challenge);
1485 debug3("%s sending reply", __func__);
1486 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1488 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1496 mm_answer_rsa_response(int sock, Buffer *m)
1499 u_char *blob, *response;
1503 debug3("%s entering", __func__);
1505 if (!authctxt->valid)
1506 fatal("%s: authctxt not valid", __func__);
1507 if (ssh1_challenge == NULL)
1508 fatal("%s: no ssh1_challenge", __func__);
1510 blob = buffer_get_string(m, &blen);
1511 if (!monitor_allowed_key(blob, blen))
1512 fatal("%s: bad key, not previously allowed", __func__);
1513 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1514 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1515 if ((key = key_from_blob(blob, blen)) == NULL)
1516 fatal("%s: received bad key", __func__);
1517 response = buffer_get_string(m, &len);
1519 fatal("%s: received bad response to challenge", __func__);
1520 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1526 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1529 BN_clear_free(ssh1_challenge);
1530 ssh1_challenge = NULL;
1531 monitor_reset_key_state();
1534 buffer_put_int(m, success);
1535 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1541 mm_answer_term(int sock, Buffer *req)
1543 extern struct monitor *pmonitor;
1546 debug3("%s: tearing down sessions", __func__);
1548 /* The child is terminating */
1549 session_destroy_all(&mm_session_close);
1552 if (options.use_pam)
1556 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1560 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1562 /* Terminate process */
1566 #ifdef SSH_AUDIT_EVENTS
1567 /* Report that an audit event occurred */
1569 mm_answer_audit_event(int socket, Buffer *m)
1571 ssh_audit_event_t event;
1573 debug3("%s entering", __func__);
1575 event = buffer_get_int(m);
1577 case SSH_AUTH_FAIL_PUBKEY:
1578 case SSH_AUTH_FAIL_HOSTBASED:
1579 case SSH_AUTH_FAIL_GSSAPI:
1580 case SSH_LOGIN_EXCEED_MAXTRIES:
1581 case SSH_LOGIN_ROOT_DENIED:
1582 case SSH_CONNECTION_CLOSE:
1583 case SSH_INVALID_USER:
1587 fatal("Audit event type %d not permitted", event);
1594 mm_answer_audit_command(int socket, Buffer *m)
1599 debug3("%s entering", __func__);
1600 cmd = buffer_get_string(m, &len);
1601 /* sanity check command, if so how? */
1602 audit_run_command(cmd);
1606 #endif /* SSH_AUDIT_EVENTS */
1609 monitor_apply_keystate(struct monitor *pmonitor)
1612 set_newkeys(MODE_IN);
1613 set_newkeys(MODE_OUT);
1615 packet_set_protocol_flags(child_state.ssh1protoflags);
1616 packet_set_encryption_key(child_state.ssh1key,
1617 child_state.ssh1keylen, child_state.ssh1cipher);
1618 xfree(child_state.ssh1key);
1621 /* for rc4 and other stateful ciphers */
1622 packet_set_keycontext(MODE_OUT, child_state.keyout);
1623 xfree(child_state.keyout);
1624 packet_set_keycontext(MODE_IN, child_state.keyin);
1625 xfree(child_state.keyin);
1628 packet_set_iv(MODE_OUT, child_state.ivout);
1629 xfree(child_state.ivout);
1630 packet_set_iv(MODE_IN, child_state.ivin);
1631 xfree(child_state.ivin);
1634 memcpy(&incoming_stream, &child_state.incoming,
1635 sizeof(incoming_stream));
1636 memcpy(&outgoing_stream, &child_state.outgoing,
1637 sizeof(outgoing_stream));
1639 /* Update with new address */
1640 if (options.compression)
1641 mm_init_compression(pmonitor->m_zlib);
1643 /* Network I/O buffers */
1644 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1645 buffer_clear(&input);
1646 buffer_append(&input, child_state.input, child_state.ilen);
1647 memset(child_state.input, 0, child_state.ilen);
1648 xfree(child_state.input);
1650 buffer_clear(&output);
1651 buffer_append(&output, child_state.output, child_state.olen);
1652 memset(child_state.output, 0, child_state.olen);
1653 xfree(child_state.output);
1657 mm_get_kex(Buffer *m)
1663 kex = xcalloc(1, sizeof(*kex));
1664 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1665 if ((session_id2 == NULL) ||
1666 (kex->session_id_len != session_id2_len) ||
1667 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1668 fatal("mm_get_get: internal error: bad session id");
1669 kex->we_need = buffer_get_int(m);
1670 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1671 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1672 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1673 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1675 kex->hostkey_type = buffer_get_int(m);
1676 kex->kex_type = buffer_get_int(m);
1677 blob = buffer_get_string(m, &bloblen);
1678 buffer_init(&kex->my);
1679 buffer_append(&kex->my, blob, bloblen);
1681 blob = buffer_get_string(m, &bloblen);
1682 buffer_init(&kex->peer);
1683 buffer_append(&kex->peer, blob, bloblen);
1686 kex->flags = buffer_get_int(m);
1687 kex->client_version_string = buffer_get_string(m, NULL);
1688 kex->server_version_string = buffer_get_string(m, NULL);
1689 kex->load_host_key=&get_hostkey_by_type;
1690 kex->host_key_index=&get_hostkey_index;
1695 /* This function requries careful sanity checking */
1698 mm_get_keystate(struct monitor *pmonitor)
1702 u_int bloblen, plen;
1703 u_int32_t seqnr, packets;
1706 debug3("%s: Waiting for new keys", __func__);
1709 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1711 child_state.ssh1protoflags = buffer_get_int(&m);
1712 child_state.ssh1cipher = buffer_get_int(&m);
1713 child_state.ssh1key = buffer_get_string(&m,
1714 &child_state.ssh1keylen);
1715 child_state.ivout = buffer_get_string(&m,
1716 &child_state.ivoutlen);
1717 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1720 /* Get the Kex for rekeying */
1721 *pmonitor->m_pkex = mm_get_kex(&m);
1724 blob = buffer_get_string(&m, &bloblen);
1725 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1728 debug3("%s: Waiting for second key", __func__);
1729 blob = buffer_get_string(&m, &bloblen);
1730 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1733 /* Now get sequence numbers for the packets */
1734 seqnr = buffer_get_int(&m);
1735 blocks = buffer_get_int64(&m);
1736 packets = buffer_get_int(&m);
1737 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1738 seqnr = buffer_get_int(&m);
1739 blocks = buffer_get_int64(&m);
1740 packets = buffer_get_int(&m);
1741 packet_set_state(MODE_IN, seqnr, blocks, packets);
1744 /* Get the key context */
1745 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1746 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1748 debug3("%s: Getting compression state", __func__);
1749 /* Get compression state */
1750 p = buffer_get_string(&m, &plen);
1751 if (plen != sizeof(child_state.outgoing))
1752 fatal("%s: bad request size", __func__);
1753 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1756 p = buffer_get_string(&m, &plen);
1757 if (plen != sizeof(child_state.incoming))
1758 fatal("%s: bad request size", __func__);
1759 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1762 /* Network I/O buffers */
1763 debug3("%s: Getting Network I/O buffers", __func__);
1764 child_state.input = buffer_get_string(&m, &child_state.ilen);
1765 child_state.output = buffer_get_string(&m, &child_state.olen);
1771 /* Allocation functions for zlib */
1773 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1775 size_t len = (size_t) size * ncount;
1778 if (len == 0 || ncount > SIZE_T_MAX / size)
1779 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1781 address = mm_malloc(mm, len);
1787 mm_zfree(struct mm_master *mm, void *address)
1789 mm_free(mm, address);
1793 mm_init_compression(struct mm_master *mm)
1795 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1796 outgoing_stream.zfree = (free_func)mm_zfree;
1797 outgoing_stream.opaque = mm;
1799 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1800 incoming_stream.zfree = (free_func)mm_zfree;
1801 incoming_stream.opaque = mm;
1806 #define FD_CLOSEONEXEC(x) do { \
1807 if (fcntl(x, F_SETFD, 1) == -1) \
1808 fatal("fcntl(%d, F_SETFD)", x); \
1812 monitor_socketpair(int *pair)
1814 #ifdef HAVE_SOCKETPAIR
1815 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1816 fatal("%s: socketpair", __func__);
1818 fatal("%s: UsePrivilegeSeparation=yes not supported",
1821 FD_CLOSEONEXEC(pair[0]);
1822 FD_CLOSEONEXEC(pair[1]);
1825 #define MM_MEMSIZE 65536
1830 struct monitor *mon;
1833 mon = xcalloc(1, sizeof(*mon));
1835 monitor_socketpair(pair);
1837 mon->m_recvfd = pair[0];
1838 mon->m_sendfd = pair[1];
1840 /* Used to share zlib space across processes */
1841 if (options.compression) {
1842 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1843 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1845 /* Compression needs to share state across borders */
1846 mm_init_compression(mon->m_zlib);
1853 monitor_reinit(struct monitor *mon)
1857 monitor_socketpair(pair);
1859 mon->m_recvfd = pair[0];
1860 mon->m_sendfd = pair[1];
1865 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1871 goid.elements = buffer_get_string(m, &len);
1874 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1876 xfree(goid.elements);
1879 buffer_put_int(m, major);
1881 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1883 /* Now we have a context, enable the step */
1884 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1890 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1893 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1894 OM_uint32 major, minor;
1895 OM_uint32 flags = 0; /* GSI needs this */
1898 in.value = buffer_get_string(m, &len);
1900 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1904 buffer_put_int(m, major);
1905 buffer_put_string(m, out.value, out.length);
1906 buffer_put_int(m, flags);
1907 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1909 gss_release_buffer(&minor, &out);
1911 if (major == GSS_S_COMPLETE) {
1912 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1913 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1914 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1920 mm_answer_gss_checkmic(int sock, Buffer *m)
1922 gss_buffer_desc gssbuf, mic;
1926 gssbuf.value = buffer_get_string(m, &len);
1927 gssbuf.length = len;
1928 mic.value = buffer_get_string(m, &len);
1931 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1933 xfree(gssbuf.value);
1937 buffer_put_int(m, ret);
1939 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1941 if (!GSS_ERROR(ret))
1942 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1948 mm_answer_gss_userok(int sock, Buffer *m)
1952 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1955 buffer_put_int(m, authenticated);
1957 debug3("%s: sending result %d", __func__, authenticated);
1958 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1960 auth_method = "gssapi-with-mic";
1962 /* Monitor loop will terminate if authenticated */
1963 return (authenticated);