1 /* $OpenBSD: monitor.c,v 1.94 2007/10/29 04:08:08 dtucker 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 #define skeychallenge(k, u, c) opiechallenge((k), (u), (c))
53 #define skey_haskey(u) opie_haskey((u))
54 #define skey_passcheck(u, r) opie_passverify((u), (r))
60 #include <openssl/dh.h>
71 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
74 #define TARGET_OS_MAC 1
79 #include "auth-options.h"
88 #include "monitor_mm.h"
92 #include "monitor_wrap.h"
93 #include "monitor_fdpass.h"
99 static Gssctxt *gsscontext = NULL;
103 extern ServerOptions options;
104 extern u_int utmp_len;
105 extern Newkeys *current_keys[];
106 extern z_stream incoming_stream;
107 extern z_stream outgoing_stream;
108 extern u_char session_id[];
109 extern Buffer input, output;
110 extern Buffer auth_debug;
111 extern int auth_debug_init;
112 extern Buffer loginmsg;
114 /* State exported from the child */
137 /* Functions on the monitor that answer unprivileged requests */
139 int mm_answer_moduli(int, Buffer *);
140 int mm_answer_sign(int, Buffer *);
141 int mm_answer_pwnamallow(int, Buffer *);
142 int mm_answer_auth2_read_banner(int, Buffer *);
143 int mm_answer_authserv(int, Buffer *);
144 int mm_answer_authpassword(int, Buffer *);
145 int mm_answer_bsdauthquery(int, Buffer *);
146 int mm_answer_bsdauthrespond(int, Buffer *);
147 int mm_answer_skeyquery(int, Buffer *);
148 int mm_answer_skeyrespond(int, Buffer *);
149 int mm_answer_keyallowed(int, Buffer *);
150 int mm_answer_keyverify(int, Buffer *);
151 int mm_answer_pty(int, Buffer *);
152 int mm_answer_pty_cleanup(int, Buffer *);
153 int mm_answer_term(int, Buffer *);
154 int mm_answer_rsa_keyallowed(int, Buffer *);
155 int mm_answer_rsa_challenge(int, Buffer *);
156 int mm_answer_rsa_response(int, Buffer *);
157 int mm_answer_sesskey(int, Buffer *);
158 int mm_answer_sessid(int, Buffer *);
161 int mm_answer_pam_start(int, Buffer *);
162 int mm_answer_pam_account(int, Buffer *);
163 int mm_answer_pam_init_ctx(int, Buffer *);
164 int mm_answer_pam_query(int, Buffer *);
165 int mm_answer_pam_respond(int, Buffer *);
166 int mm_answer_pam_free_ctx(int, Buffer *);
170 int mm_answer_gss_setup_ctx(int, Buffer *);
171 int mm_answer_gss_accept_ctx(int, Buffer *);
172 int mm_answer_gss_userok(int, Buffer *);
173 int mm_answer_gss_checkmic(int, Buffer *);
176 #ifdef SSH_AUDIT_EVENTS
177 int mm_answer_audit_event(int, Buffer *);
178 int mm_answer_audit_command(int, Buffer *);
181 static Authctxt *authctxt;
182 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
184 /* local state for key verify */
185 static u_char *key_blob = NULL;
186 static u_int key_bloblen = 0;
187 static int key_blobtype = MM_NOKEY;
188 static char *hostbased_cuser = NULL;
189 static char *hostbased_chost = NULL;
190 static char *auth_method = "unknown";
191 static u_int session_id2_len = 0;
192 static u_char *session_id2 = NULL;
193 static pid_t monitor_child_pid;
196 enum monitor_reqtype type;
198 int (*f)(int, Buffer *);
201 #define MON_ISAUTH 0x0004 /* Required for Authentication */
202 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
203 #define MON_ONCE 0x0010 /* Disable after calling */
204 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
206 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
208 #define MON_PERMIT 0x1000 /* Request is permitted */
210 struct mon_table mon_dispatch_proto20[] = {
211 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
212 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
213 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
214 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
215 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
216 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
218 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
219 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
220 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
221 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
222 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
223 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
225 #ifdef SSH_AUDIT_EVENTS
226 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
229 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
230 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
233 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
234 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
236 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
237 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
239 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
240 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
241 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
242 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
247 struct mon_table mon_dispatch_postauth20[] = {
248 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
249 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
250 {MONITOR_REQ_PTY, 0, mm_answer_pty},
251 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
252 {MONITOR_REQ_TERM, 0, mm_answer_term},
253 #ifdef SSH_AUDIT_EVENTS
254 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
255 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
260 struct mon_table mon_dispatch_proto15[] = {
261 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
262 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
263 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
264 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
265 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
266 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
267 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
268 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
270 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
271 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
274 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
275 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
278 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
279 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
280 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
281 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
282 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
283 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
285 #ifdef SSH_AUDIT_EVENTS
286 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
291 struct mon_table mon_dispatch_postauth15[] = {
292 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
293 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
294 {MONITOR_REQ_TERM, 0, mm_answer_term},
295 #ifdef SSH_AUDIT_EVENTS
296 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
297 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
302 struct mon_table *mon_dispatch;
304 /* Specifies if a certain message is allowed at the moment */
307 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
309 while (ent->f != NULL) {
310 if (ent->type == type) {
311 ent->flags &= ~MON_PERMIT;
312 ent->flags |= permit ? MON_PERMIT : 0;
320 monitor_permit_authentications(int permit)
322 struct mon_table *ent = mon_dispatch;
324 while (ent->f != NULL) {
325 if (ent->flags & MON_AUTH) {
326 ent->flags &= ~MON_PERMIT;
327 ent->flags |= permit ? MON_PERMIT : 0;
334 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
336 struct mon_table *ent;
337 int authenticated = 0;
339 debug3("preauth child monitor started");
341 authctxt = _authctxt;
342 memset(authctxt, 0, sizeof(*authctxt));
344 authctxt->loginmsg = &loginmsg;
347 mon_dispatch = mon_dispatch_proto20;
349 /* Permit requests for moduli and signatures */
350 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
351 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
353 mon_dispatch = mon_dispatch_proto15;
355 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
358 /* The first few requests do not require asynchronous access */
359 while (!authenticated) {
360 auth_method = "unknown";
361 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
363 if (!(ent->flags & MON_AUTHDECIDE))
364 fatal("%s: unexpected authentication from %d",
365 __func__, ent->type);
366 if (authctxt->pw->pw_uid == 0 &&
367 !auth_root_allowed(auth_method))
370 /* PAM needs to perform account checks after auth */
371 if (options.use_pam && authenticated) {
375 mm_request_receive_expect(pmonitor->m_sendfd,
376 MONITOR_REQ_PAM_ACCOUNT, &m);
377 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
383 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
384 auth_log(authctxt, authenticated, auth_method,
385 compat20 ? " ssh2" : "");
387 authctxt->failures++;
391 if (!authctxt->valid)
392 fatal("%s: authenticated invalid user", __func__);
393 if (strcmp(auth_method, "unknown") == 0)
394 fatal("%s: authentication method name unknown", __func__);
396 debug("%s: %s has been authenticated by privileged process",
397 __func__, authctxt->user);
399 mm_get_keystate(pmonitor);
403 monitor_set_child_handler(pid_t pid)
405 monitor_child_pid = pid;
409 monitor_child_handler(int sig)
411 kill(monitor_child_pid, sig);
415 monitor_child_postauth(struct monitor *pmonitor)
417 monitor_set_child_handler(pmonitor->m_pid);
418 signal(SIGHUP, &monitor_child_handler);
419 signal(SIGTERM, &monitor_child_handler);
420 signal(SIGINT, &monitor_child_handler);
423 mon_dispatch = mon_dispatch_postauth20;
425 /* Permit requests for moduli and signatures */
426 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
427 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
428 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
430 mon_dispatch = mon_dispatch_postauth15;
431 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
434 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
435 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
439 monitor_read(pmonitor, mon_dispatch, NULL);
443 monitor_sync(struct monitor *pmonitor)
445 if (options.compression) {
446 /* The member allocation is not visible, so sync it */
447 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
452 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
453 struct mon_table **pent)
461 mm_request_receive(pmonitor->m_sendfd, &m);
462 type = buffer_get_char(&m);
464 debug3("%s: checking request %d", __func__, type);
466 while (ent->f != NULL) {
467 if (ent->type == type)
472 if (ent->f != NULL) {
473 if (!(ent->flags & MON_PERMIT))
474 fatal("%s: unpermitted request %d", __func__,
476 ret = (*ent->f)(pmonitor->m_sendfd, &m);
479 /* The child may use this request only once, disable it */
480 if (ent->flags & MON_ONCE) {
481 debug2("%s: %d used once, disabling now", __func__,
483 ent->flags &= ~MON_PERMIT;
492 fatal("%s: unsupported request: %d", __func__, type);
498 /* allowed key state */
500 monitor_allowed_key(u_char *blob, u_int bloblen)
502 /* make sure key is allowed */
503 if (key_blob == NULL || key_bloblen != bloblen ||
504 memcmp(key_blob, blob, key_bloblen))
510 monitor_reset_key_state(void)
513 if (key_blob != NULL)
515 if (hostbased_cuser != NULL)
516 xfree(hostbased_cuser);
517 if (hostbased_chost != NULL)
518 xfree(hostbased_chost);
521 key_blobtype = MM_NOKEY;
522 hostbased_cuser = NULL;
523 hostbased_chost = NULL;
527 mm_answer_moduli(int sock, Buffer *m)
532 min = buffer_get_int(m);
533 want = buffer_get_int(m);
534 max = buffer_get_int(m);
536 debug3("%s: got parameters: %d %d %d",
537 __func__, min, want, max);
538 /* We need to check here, too, in case the child got corrupted */
539 if (max < min || want < min || max < want)
540 fatal("%s: bad parameters: %d %d %d",
541 __func__, min, want, max);
545 dh = choose_dh(min, want, max);
547 buffer_put_char(m, 0);
550 /* Send first bignum */
551 buffer_put_char(m, 1);
552 buffer_put_bignum2(m, dh->p);
553 buffer_put_bignum2(m, dh->g);
557 mm_request_send(sock, MONITOR_ANS_MODULI, m);
562 mm_answer_sign(int sock, Buffer *m)
567 u_int siglen, datlen;
570 debug3("%s", __func__);
572 keyid = buffer_get_int(m);
573 p = buffer_get_string(m, &datlen);
576 * Supported KEX types will only return SHA1 (20 byte) or
577 * SHA256 (32 byte) hashes
579 if (datlen != 20 && datlen != 32)
580 fatal("%s: data length incorrect: %u", __func__, datlen);
582 /* save session id, it will be passed on the first call */
583 if (session_id2_len == 0) {
584 session_id2_len = datlen;
585 session_id2 = xmalloc(session_id2_len);
586 memcpy(session_id2, p, session_id2_len);
589 if ((key = get_hostkey_by_index(keyid)) == NULL)
590 fatal("%s: no hostkey from index %d", __func__, keyid);
591 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
592 fatal("%s: key_sign failed", __func__);
594 debug3("%s: signature %p(%u)", __func__, signature, siglen);
597 buffer_put_string(m, signature, siglen);
602 mm_request_send(sock, MONITOR_ANS_SIGN, m);
604 /* Turn on permissions for getpwnam */
605 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
610 /* Retrieves the password entry and also checks if the user is permitted */
613 mm_answer_pwnamallow(int sock, Buffer *m)
616 struct passwd *pwent;
619 debug3("%s", __func__);
621 if (authctxt->attempt++ != 0)
622 fatal("%s: multiple attempts for getpwnam", __func__);
624 username = buffer_get_string(m, NULL);
626 pwent = getpwnamallow(username);
628 authctxt->user = xstrdup(username);
629 setproctitle("%s [priv]", pwent ? username : "unknown");
635 buffer_put_char(m, 0);
636 authctxt->pw = fakepw();
641 authctxt->pw = pwent;
644 buffer_put_char(m, 1);
645 buffer_put_string(m, pwent, sizeof(struct passwd));
646 buffer_put_cstring(m, pwent->pw_name);
647 buffer_put_cstring(m, "*");
648 buffer_put_cstring(m, pwent->pw_gecos);
649 #ifdef HAVE_PW_CLASS_IN_PASSWD
650 buffer_put_cstring(m, pwent->pw_class);
652 buffer_put_cstring(m, pwent->pw_dir);
653 buffer_put_cstring(m, pwent->pw_shell);
656 buffer_put_string(m, &options, sizeof(options));
657 if (options.banner != NULL)
658 buffer_put_cstring(m, options.banner);
659 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
660 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
662 /* For SSHv1 allow authentication now */
664 monitor_permit_authentications(1);
666 /* Allow service/style information on the auth context */
667 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
668 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
673 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
679 int mm_answer_auth2_read_banner(int sock, Buffer *m)
684 banner = auth2_read_banner();
685 buffer_put_cstring(m, banner != NULL ? banner : "");
686 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
695 mm_answer_authserv(int sock, Buffer *m)
697 monitor_permit_authentications(1);
699 authctxt->service = buffer_get_string(m, NULL);
700 authctxt->style = buffer_get_string(m, NULL);
701 debug3("%s: service=%s, style=%s",
702 __func__, authctxt->service, authctxt->style);
704 if (strlen(authctxt->style) == 0) {
705 xfree(authctxt->style);
706 authctxt->style = NULL;
713 mm_answer_authpassword(int sock, Buffer *m)
715 static int call_count;
720 passwd = buffer_get_string(m, &plen);
721 /* Only authenticate if the context is valid */
722 authenticated = options.password_authentication &&
723 auth_password(authctxt, passwd);
724 memset(passwd, 0, strlen(passwd));
728 buffer_put_int(m, authenticated);
730 debug3("%s: sending result %d", __func__, authenticated);
731 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
734 if (plen == 0 && call_count == 1)
735 auth_method = "none";
737 auth_method = "password";
739 /* Causes monitor loop to terminate if authenticated */
740 return (authenticated);
745 mm_answer_bsdauthquery(int sock, Buffer *m)
747 char *name, *infotxt;
753 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
754 &prompts, &echo_on) < 0 ? 0 : 1;
757 buffer_put_int(m, success);
759 buffer_put_cstring(m, prompts[0]);
761 debug3("%s: sending challenge success: %u", __func__, success);
762 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
775 mm_answer_bsdauthrespond(int sock, Buffer *m)
780 if (authctxt->as == 0)
781 fatal("%s: no bsd auth session", __func__);
783 response = buffer_get_string(m, NULL);
784 authok = options.challenge_response_authentication &&
785 auth_userresponse(authctxt->as, response, 0);
787 debug3("%s: <%s> = <%d>", __func__, response, authok);
791 buffer_put_int(m, authok);
793 debug3("%s: sending authenticated: %d", __func__, authok);
794 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
796 auth_method = "bsdauth";
798 return (authok != 0);
804 mm_answer_skeyquery(int sock, Buffer *m)
807 char challenge[1024];
810 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
811 sizeof(challenge)) < 0 ? 0 : 1;
814 buffer_put_int(m, success);
816 buffer_put_cstring(m, challenge);
818 debug3("%s: sending challenge success: %u", __func__, success);
819 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
825 mm_answer_skeyrespond(int sock, Buffer *m)
830 response = buffer_get_string(m, NULL);
832 authok = (options.challenge_response_authentication &&
834 skey_haskey(authctxt->pw->pw_name) == 0 &&
835 skey_passcheck(authctxt->pw->pw_name, response) != -1);
840 buffer_put_int(m, authok);
842 debug3("%s: sending authenticated: %d", __func__, authok);
843 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
845 auth_method = "skey";
847 return (authok != 0);
853 mm_answer_pam_start(int sock, Buffer *m)
855 if (!options.use_pam)
856 fatal("UsePAM not set, but ended up in %s anyway", __func__);
860 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
866 mm_answer_pam_account(int sock, Buffer *m)
870 if (!options.use_pam)
871 fatal("UsePAM not set, but ended up in %s anyway", __func__);
873 ret = do_pam_account();
875 buffer_put_int(m, ret);
876 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
878 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
883 static void *sshpam_ctxt, *sshpam_authok;
884 extern KbdintDevice sshpam_device;
887 mm_answer_pam_init_ctx(int sock, Buffer *m)
890 debug3("%s", __func__);
891 authctxt->user = buffer_get_string(m, NULL);
892 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
893 sshpam_authok = NULL;
895 if (sshpam_ctxt != NULL) {
896 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
897 buffer_put_int(m, 1);
899 buffer_put_int(m, 0);
901 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
906 mm_answer_pam_query(int sock, Buffer *m)
908 char *name, *info, **prompts;
909 u_int i, num, *echo_on;
912 debug3("%s", __func__);
913 sshpam_authok = NULL;
914 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
915 if (ret == 0 && num == 0)
916 sshpam_authok = sshpam_ctxt;
917 if (num > 1 || name == NULL || info == NULL)
920 buffer_put_int(m, ret);
921 buffer_put_cstring(m, name);
923 buffer_put_cstring(m, info);
925 buffer_put_int(m, num);
926 for (i = 0; i < num; ++i) {
927 buffer_put_cstring(m, prompts[i]);
929 buffer_put_int(m, echo_on[i]);
935 auth_method = "keyboard-interactive/pam";
936 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
941 mm_answer_pam_respond(int sock, Buffer *m)
947 debug3("%s", __func__);
948 sshpam_authok = NULL;
949 num = buffer_get_int(m);
951 resp = xcalloc(num, sizeof(char *));
952 for (i = 0; i < num; ++i)
953 resp[i] = buffer_get_string(m, NULL);
954 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
955 for (i = 0; i < num; ++i)
959 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
962 buffer_put_int(m, ret);
963 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
964 auth_method = "keyboard-interactive/pam";
966 sshpam_authok = sshpam_ctxt;
971 mm_answer_pam_free_ctx(int sock, Buffer *m)
974 debug3("%s", __func__);
975 (sshpam_device.free_ctx)(sshpam_ctxt);
977 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
978 auth_method = "keyboard-interactive/pam";
979 return (sshpam_authok == sshpam_ctxt);
984 mm_append_debug(Buffer *m)
986 if (auth_debug_init && buffer_len(&auth_debug)) {
987 debug3("%s: Appending debug messages for child", __func__);
988 buffer_append(m, buffer_ptr(&auth_debug),
989 buffer_len(&auth_debug));
990 buffer_clear(&auth_debug);
995 mm_answer_keyallowed(int sock, Buffer *m)
1001 enum mm_keytype type = 0;
1004 debug3("%s entering", __func__);
1006 type = buffer_get_int(m);
1007 cuser = buffer_get_string(m, NULL);
1008 chost = buffer_get_string(m, NULL);
1009 blob = buffer_get_string(m, &bloblen);
1011 key = key_from_blob(blob, bloblen);
1013 if ((compat20 && type == MM_RSAHOSTKEY) ||
1014 (!compat20 && type != MM_RSAHOSTKEY))
1015 fatal("%s: key type and protocol mismatch", __func__);
1017 debug3("%s: key_from_blob: %p", __func__, key);
1019 if (key != NULL && authctxt->valid) {
1022 allowed = options.pubkey_authentication &&
1023 user_key_allowed(authctxt->pw, key);
1024 auth_method = "publickey";
1027 allowed = options.hostbased_authentication &&
1028 hostbased_key_allowed(authctxt->pw,
1030 auth_method = "hostbased";
1033 key->type = KEY_RSA1; /* XXX */
1034 allowed = options.rhosts_rsa_authentication &&
1035 auth_rhosts_rsa_key_allowed(authctxt->pw,
1037 auth_method = "rsa";
1040 fatal("%s: unknown key type %d", __func__, type);
1047 /* clear temporarily storage (used by verify) */
1048 monitor_reset_key_state();
1051 /* Save temporarily for comparison in verify */
1053 key_bloblen = bloblen;
1054 key_blobtype = type;
1055 hostbased_cuser = cuser;
1056 hostbased_chost = chost;
1058 /* Log failed attempt */
1059 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1065 debug3("%s: key %p is %s",
1066 __func__, key, allowed ? "allowed" : "disallowed");
1069 buffer_put_int(m, allowed);
1070 buffer_put_int(m, forced_command != NULL);
1074 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1076 if (type == MM_RSAHOSTKEY)
1077 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1083 monitor_valid_userblob(u_char *data, u_int datalen)
1091 buffer_append(&b, data, datalen);
1093 if (datafellows & SSH_OLD_SESSIONID) {
1095 len = buffer_len(&b);
1096 if ((session_id2 == NULL) ||
1097 (len < session_id2_len) ||
1098 (memcmp(p, session_id2, session_id2_len) != 0))
1100 buffer_consume(&b, session_id2_len);
1102 p = buffer_get_string(&b, &len);
1103 if ((session_id2 == NULL) ||
1104 (len != session_id2_len) ||
1105 (memcmp(p, session_id2, session_id2_len) != 0))
1109 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1111 p = buffer_get_string(&b, NULL);
1112 if (strcmp(authctxt->user, p) != 0) {
1113 logit("wrong user name passed to monitor: expected %s != %.100s",
1118 buffer_skip_string(&b);
1119 if (datafellows & SSH_BUG_PKAUTH) {
1120 if (!buffer_get_char(&b))
1123 p = buffer_get_string(&b, NULL);
1124 if (strcmp("publickey", p) != 0)
1127 if (!buffer_get_char(&b))
1129 buffer_skip_string(&b);
1131 buffer_skip_string(&b);
1132 if (buffer_len(&b) != 0)
1139 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1148 buffer_append(&b, data, datalen);
1150 p = buffer_get_string(&b, &len);
1151 if ((session_id2 == NULL) ||
1152 (len != session_id2_len) ||
1153 (memcmp(p, session_id2, session_id2_len) != 0))
1157 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1159 p = buffer_get_string(&b, NULL);
1160 if (strcmp(authctxt->user, p) != 0) {
1161 logit("wrong user name passed to monitor: expected %s != %.100s",
1166 buffer_skip_string(&b); /* service */
1167 p = buffer_get_string(&b, NULL);
1168 if (strcmp(p, "hostbased") != 0)
1171 buffer_skip_string(&b); /* pkalg */
1172 buffer_skip_string(&b); /* pkblob */
1174 /* verify client host, strip trailing dot if necessary */
1175 p = buffer_get_string(&b, NULL);
1176 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1178 if (strcmp(p, chost) != 0)
1182 /* verify client user */
1183 p = buffer_get_string(&b, NULL);
1184 if (strcmp(p, cuser) != 0)
1188 if (buffer_len(&b) != 0)
1195 mm_answer_keyverify(int sock, Buffer *m)
1198 u_char *signature, *data, *blob;
1199 u_int signaturelen, datalen, bloblen;
1203 blob = buffer_get_string(m, &bloblen);
1204 signature = buffer_get_string(m, &signaturelen);
1205 data = buffer_get_string(m, &datalen);
1207 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1208 !monitor_allowed_key(blob, bloblen))
1209 fatal("%s: bad key, not previously allowed", __func__);
1211 key = key_from_blob(blob, bloblen);
1213 fatal("%s: bad public key blob", __func__);
1215 switch (key_blobtype) {
1217 valid_data = monitor_valid_userblob(data, datalen);
1220 valid_data = monitor_valid_hostbasedblob(data, datalen,
1221 hostbased_cuser, hostbased_chost);
1228 fatal("%s: bad signature data blob", __func__);
1230 verified = key_verify(key, signature, signaturelen, data, datalen);
1231 debug3("%s: key %p signature %s",
1232 __func__, key, (verified == 1) ? "verified" : "unverified");
1239 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1241 monitor_reset_key_state();
1244 buffer_put_int(m, verified);
1245 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1247 return (verified == 1);
1251 mm_record_login(Session *s, struct passwd *pw)
1254 struct sockaddr_storage from;
1257 * Get IP address of client. If the connection is not a socket, let
1258 * the address be 0.0.0.0.
1260 memset(&from, 0, sizeof(from));
1261 fromlen = sizeof(from);
1262 if (packet_connection_is_on_socket()) {
1263 if (getpeername(packet_get_connection_in(),
1264 (struct sockaddr *)&from, &fromlen) < 0) {
1265 debug("getpeername: %.100s", strerror(errno));
1269 /* Record that there was a login on that tty from the remote host. */
1270 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1271 get_remote_name_or_ip(utmp_len, options.use_dns),
1272 (struct sockaddr *)&from, fromlen);
1276 mm_session_close(Session *s)
1278 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1279 if (s->ttyfd != -1) {
1280 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1281 session_pty_cleanup2(s);
1287 mm_answer_pty(int sock, Buffer *m)
1289 extern struct monitor *pmonitor;
1293 debug3("%s entering", __func__);
1299 s->authctxt = authctxt;
1300 s->pw = authctxt->pw;
1301 s->pid = pmonitor->m_pid;
1302 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1305 pty_setowner(authctxt->pw, s->tty);
1307 buffer_put_int(m, 1);
1308 buffer_put_cstring(m, s->tty);
1310 /* We need to trick ttyslot */
1311 if (dup2(s->ttyfd, 0) == -1)
1312 fatal("%s: dup2", __func__);
1314 mm_record_login(s, authctxt->pw);
1316 /* Now we can close the file descriptor again */
1319 /* send messages generated by record_login */
1320 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1321 buffer_clear(&loginmsg);
1323 mm_request_send(sock, MONITOR_ANS_PTY, m);
1325 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1326 mm_send_fd(sock, s->ttyfd) == -1)
1327 fatal("%s: send fds failed", __func__);
1329 /* make sure nothing uses fd 0 */
1330 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1331 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1333 error("%s: fd0 %d != 0", __func__, fd0);
1335 /* slave is not needed */
1337 s->ttyfd = s->ptyfd;
1338 /* no need to dup() because nobody closes ptyfd */
1339 s->ptymaster = s->ptyfd;
1341 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1347 mm_session_close(s);
1348 buffer_put_int(m, 0);
1349 mm_request_send(sock, MONITOR_ANS_PTY, m);
1354 mm_answer_pty_cleanup(int sock, Buffer *m)
1359 debug3("%s entering", __func__);
1361 tty = buffer_get_string(m, NULL);
1362 if ((s = session_by_tty(tty)) != NULL)
1363 mm_session_close(s);
1370 mm_answer_sesskey(int sock, Buffer *m)
1375 /* Turn off permissions */
1376 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1378 if ((p = BN_new()) == NULL)
1379 fatal("%s: BN_new", __func__);
1381 buffer_get_bignum2(m, p);
1383 rsafail = ssh1_session_key(p);
1386 buffer_put_int(m, rsafail);
1387 buffer_put_bignum2(m, p);
1391 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1393 /* Turn on permissions for sessid passing */
1394 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1400 mm_answer_sessid(int sock, Buffer *m)
1404 debug3("%s entering", __func__);
1406 if (buffer_len(m) != 16)
1407 fatal("%s: bad ssh1 session id", __func__);
1408 for (i = 0; i < 16; i++)
1409 session_id[i] = buffer_get_char(m);
1411 /* Turn on permissions for getpwnam */
1412 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1418 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1422 u_char *blob = NULL;
1426 debug3("%s entering", __func__);
1428 auth_method = "rsa";
1429 if (options.rsa_authentication && authctxt->valid) {
1430 if ((client_n = BN_new()) == NULL)
1431 fatal("%s: BN_new", __func__);
1432 buffer_get_bignum2(m, client_n);
1433 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1434 BN_clear_free(client_n);
1437 buffer_put_int(m, allowed);
1438 buffer_put_int(m, forced_command != NULL);
1440 /* clear temporarily storage (used by generate challenge) */
1441 monitor_reset_key_state();
1443 if (allowed && key != NULL) {
1444 key->type = KEY_RSA; /* cheat for key_to_blob */
1445 if (key_to_blob(key, &blob, &blen) == 0)
1446 fatal("%s: key_to_blob failed", __func__);
1447 buffer_put_string(m, blob, blen);
1449 /* Save temporarily for comparison in verify */
1452 key_blobtype = MM_RSAUSERKEY;
1459 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1461 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1462 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1467 mm_answer_rsa_challenge(int sock, Buffer *m)
1473 debug3("%s entering", __func__);
1475 if (!authctxt->valid)
1476 fatal("%s: authctxt not valid", __func__);
1477 blob = buffer_get_string(m, &blen);
1478 if (!monitor_allowed_key(blob, blen))
1479 fatal("%s: bad key, not previously allowed", __func__);
1480 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1481 fatal("%s: key type mismatch", __func__);
1482 if ((key = key_from_blob(blob, blen)) == NULL)
1483 fatal("%s: received bad key", __func__);
1486 BN_clear_free(ssh1_challenge);
1487 ssh1_challenge = auth_rsa_generate_challenge(key);
1490 buffer_put_bignum2(m, ssh1_challenge);
1492 debug3("%s sending reply", __func__);
1493 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1495 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1503 mm_answer_rsa_response(int sock, Buffer *m)
1506 u_char *blob, *response;
1510 debug3("%s entering", __func__);
1512 if (!authctxt->valid)
1513 fatal("%s: authctxt not valid", __func__);
1514 if (ssh1_challenge == NULL)
1515 fatal("%s: no ssh1_challenge", __func__);
1517 blob = buffer_get_string(m, &blen);
1518 if (!monitor_allowed_key(blob, blen))
1519 fatal("%s: bad key, not previously allowed", __func__);
1520 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1521 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1522 if ((key = key_from_blob(blob, blen)) == NULL)
1523 fatal("%s: received bad key", __func__);
1524 response = buffer_get_string(m, &len);
1526 fatal("%s: received bad response to challenge", __func__);
1527 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1533 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1536 BN_clear_free(ssh1_challenge);
1537 ssh1_challenge = NULL;
1538 monitor_reset_key_state();
1541 buffer_put_int(m, success);
1542 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1548 mm_answer_term(int sock, Buffer *req)
1550 extern struct monitor *pmonitor;
1553 debug3("%s: tearing down sessions", __func__);
1555 /* The child is terminating */
1556 session_destroy_all(&mm_session_close);
1559 if (options.use_pam)
1563 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1567 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1569 /* Terminate process */
1573 #ifdef SSH_AUDIT_EVENTS
1574 /* Report that an audit event occurred */
1576 mm_answer_audit_event(int socket, Buffer *m)
1578 ssh_audit_event_t event;
1580 debug3("%s entering", __func__);
1582 event = buffer_get_int(m);
1584 case SSH_AUTH_FAIL_PUBKEY:
1585 case SSH_AUTH_FAIL_HOSTBASED:
1586 case SSH_AUTH_FAIL_GSSAPI:
1587 case SSH_LOGIN_EXCEED_MAXTRIES:
1588 case SSH_LOGIN_ROOT_DENIED:
1589 case SSH_CONNECTION_CLOSE:
1590 case SSH_INVALID_USER:
1594 fatal("Audit event type %d not permitted", event);
1601 mm_answer_audit_command(int socket, Buffer *m)
1606 debug3("%s entering", __func__);
1607 cmd = buffer_get_string(m, &len);
1608 /* sanity check command, if so how? */
1609 audit_run_command(cmd);
1613 #endif /* SSH_AUDIT_EVENTS */
1616 monitor_apply_keystate(struct monitor *pmonitor)
1619 set_newkeys(MODE_IN);
1620 set_newkeys(MODE_OUT);
1622 packet_set_protocol_flags(child_state.ssh1protoflags);
1623 packet_set_encryption_key(child_state.ssh1key,
1624 child_state.ssh1keylen, child_state.ssh1cipher);
1625 xfree(child_state.ssh1key);
1628 /* for rc4 and other stateful ciphers */
1629 packet_set_keycontext(MODE_OUT, child_state.keyout);
1630 xfree(child_state.keyout);
1631 packet_set_keycontext(MODE_IN, child_state.keyin);
1632 xfree(child_state.keyin);
1635 packet_set_iv(MODE_OUT, child_state.ivout);
1636 xfree(child_state.ivout);
1637 packet_set_iv(MODE_IN, child_state.ivin);
1638 xfree(child_state.ivin);
1641 memcpy(&incoming_stream, &child_state.incoming,
1642 sizeof(incoming_stream));
1643 memcpy(&outgoing_stream, &child_state.outgoing,
1644 sizeof(outgoing_stream));
1646 /* Update with new address */
1647 if (options.compression)
1648 mm_init_compression(pmonitor->m_zlib);
1650 /* Network I/O buffers */
1651 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1652 buffer_clear(&input);
1653 buffer_append(&input, child_state.input, child_state.ilen);
1654 memset(child_state.input, 0, child_state.ilen);
1655 xfree(child_state.input);
1657 buffer_clear(&output);
1658 buffer_append(&output, child_state.output, child_state.olen);
1659 memset(child_state.output, 0, child_state.olen);
1660 xfree(child_state.output);
1664 mm_get_kex(Buffer *m)
1670 kex = xcalloc(1, sizeof(*kex));
1671 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1672 if ((session_id2 == NULL) ||
1673 (kex->session_id_len != session_id2_len) ||
1674 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1675 fatal("mm_get_get: internal error: bad session id");
1676 kex->we_need = buffer_get_int(m);
1677 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1678 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1679 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1680 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1682 kex->hostkey_type = buffer_get_int(m);
1683 kex->kex_type = buffer_get_int(m);
1684 blob = buffer_get_string(m, &bloblen);
1685 buffer_init(&kex->my);
1686 buffer_append(&kex->my, blob, bloblen);
1688 blob = buffer_get_string(m, &bloblen);
1689 buffer_init(&kex->peer);
1690 buffer_append(&kex->peer, blob, bloblen);
1693 kex->flags = buffer_get_int(m);
1694 kex->client_version_string = buffer_get_string(m, NULL);
1695 kex->server_version_string = buffer_get_string(m, NULL);
1696 kex->load_host_key=&get_hostkey_by_type;
1697 kex->host_key_index=&get_hostkey_index;
1702 /* This function requries careful sanity checking */
1705 mm_get_keystate(struct monitor *pmonitor)
1709 u_int bloblen, plen;
1710 u_int32_t seqnr, packets;
1713 debug3("%s: Waiting for new keys", __func__);
1716 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1718 child_state.ssh1protoflags = buffer_get_int(&m);
1719 child_state.ssh1cipher = buffer_get_int(&m);
1720 child_state.ssh1key = buffer_get_string(&m,
1721 &child_state.ssh1keylen);
1722 child_state.ivout = buffer_get_string(&m,
1723 &child_state.ivoutlen);
1724 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1727 /* Get the Kex for rekeying */
1728 *pmonitor->m_pkex = mm_get_kex(&m);
1731 blob = buffer_get_string(&m, &bloblen);
1732 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1735 debug3("%s: Waiting for second key", __func__);
1736 blob = buffer_get_string(&m, &bloblen);
1737 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1740 /* Now get sequence numbers for the packets */
1741 seqnr = buffer_get_int(&m);
1742 blocks = buffer_get_int64(&m);
1743 packets = buffer_get_int(&m);
1744 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1745 seqnr = buffer_get_int(&m);
1746 blocks = buffer_get_int64(&m);
1747 packets = buffer_get_int(&m);
1748 packet_set_state(MODE_IN, seqnr, blocks, packets);
1751 /* Get the key context */
1752 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1753 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1755 debug3("%s: Getting compression state", __func__);
1756 /* Get compression state */
1757 p = buffer_get_string(&m, &plen);
1758 if (plen != sizeof(child_state.outgoing))
1759 fatal("%s: bad request size", __func__);
1760 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1763 p = buffer_get_string(&m, &plen);
1764 if (plen != sizeof(child_state.incoming))
1765 fatal("%s: bad request size", __func__);
1766 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1769 /* Network I/O buffers */
1770 debug3("%s: Getting Network I/O buffers", __func__);
1771 child_state.input = buffer_get_string(&m, &child_state.ilen);
1772 child_state.output = buffer_get_string(&m, &child_state.olen);
1778 /* Allocation functions for zlib */
1780 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1782 size_t len = (size_t) size * ncount;
1785 if (len == 0 || ncount > SIZE_T_MAX / size)
1786 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1788 address = mm_malloc(mm, len);
1794 mm_zfree(struct mm_master *mm, void *address)
1796 mm_free(mm, address);
1800 mm_init_compression(struct mm_master *mm)
1802 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1803 outgoing_stream.zfree = (free_func)mm_zfree;
1804 outgoing_stream.opaque = mm;
1806 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1807 incoming_stream.zfree = (free_func)mm_zfree;
1808 incoming_stream.opaque = mm;
1813 #define FD_CLOSEONEXEC(x) do { \
1814 if (fcntl(x, F_SETFD, 1) == -1) \
1815 fatal("fcntl(%d, F_SETFD)", x); \
1819 monitor_socketpair(int *pair)
1821 #ifdef HAVE_SOCKETPAIR
1822 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1823 fatal("%s: socketpair", __func__);
1825 fatal("%s: UsePrivilegeSeparation=yes not supported",
1828 FD_CLOSEONEXEC(pair[0]);
1829 FD_CLOSEONEXEC(pair[1]);
1832 #define MM_MEMSIZE 65536
1837 struct monitor *mon;
1840 mon = xcalloc(1, sizeof(*mon));
1842 monitor_socketpair(pair);
1844 mon->m_recvfd = pair[0];
1845 mon->m_sendfd = pair[1];
1847 /* Used to share zlib space across processes */
1848 if (options.compression) {
1849 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1850 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1852 /* Compression needs to share state across borders */
1853 mm_init_compression(mon->m_zlib);
1860 monitor_reinit(struct monitor *mon)
1864 monitor_socketpair(pair);
1866 mon->m_recvfd = pair[0];
1867 mon->m_sendfd = pair[1];
1872 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1878 goid.elements = buffer_get_string(m, &len);
1881 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1883 xfree(goid.elements);
1886 buffer_put_int(m, major);
1888 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1890 /* Now we have a context, enable the step */
1891 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1897 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1900 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1901 OM_uint32 major, minor;
1902 OM_uint32 flags = 0; /* GSI needs this */
1905 in.value = buffer_get_string(m, &len);
1907 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1911 buffer_put_int(m, major);
1912 buffer_put_string(m, out.value, out.length);
1913 buffer_put_int(m, flags);
1914 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1916 gss_release_buffer(&minor, &out);
1918 if (major == GSS_S_COMPLETE) {
1919 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1920 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1921 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1927 mm_answer_gss_checkmic(int sock, Buffer *m)
1929 gss_buffer_desc gssbuf, mic;
1933 gssbuf.value = buffer_get_string(m, &len);
1934 gssbuf.length = len;
1935 mic.value = buffer_get_string(m, &len);
1938 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1940 xfree(gssbuf.value);
1944 buffer_put_int(m, ret);
1946 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1948 if (!GSS_ERROR(ret))
1949 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1955 mm_answer_gss_userok(int sock, Buffer *m)
1959 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1962 buffer_put_int(m, authenticated);
1964 debug3("%s: sending result %d", __func__, authenticated);
1965 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1967 auth_method = "gssapi-with-mic";
1969 /* Monitor loop will terminate if authenticated */
1970 return (authenticated);