1 /* $OpenBSD: monitor.c,v 1.88 2006/08/12 20:46:46 miod 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>
63 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
66 #define TARGET_OS_MAC 1
71 #include "auth-options.h"
80 #include "monitor_mm.h"
84 #include "monitor_wrap.h"
85 #include "monitor_fdpass.h"
91 static Gssctxt *gsscontext = NULL;
95 extern ServerOptions options;
96 extern u_int utmp_len;
97 extern Newkeys *current_keys[];
98 extern z_stream incoming_stream;
99 extern z_stream outgoing_stream;
100 extern u_char session_id[];
101 extern Buffer input, output;
102 extern Buffer auth_debug;
103 extern int auth_debug_init;
104 extern Buffer loginmsg;
106 /* State exported from the child */
129 /* Functions on the monitor that answer unprivileged requests */
131 int mm_answer_moduli(int, Buffer *);
132 int mm_answer_sign(int, Buffer *);
133 int mm_answer_pwnamallow(int, Buffer *);
134 int mm_answer_auth2_read_banner(int, Buffer *);
135 int mm_answer_authserv(int, Buffer *);
136 int mm_answer_authpassword(int, Buffer *);
137 int mm_answer_bsdauthquery(int, Buffer *);
138 int mm_answer_bsdauthrespond(int, Buffer *);
139 int mm_answer_skeyquery(int, Buffer *);
140 int mm_answer_skeyrespond(int, Buffer *);
141 int mm_answer_keyallowed(int, Buffer *);
142 int mm_answer_keyverify(int, Buffer *);
143 int mm_answer_pty(int, Buffer *);
144 int mm_answer_pty_cleanup(int, Buffer *);
145 int mm_answer_term(int, Buffer *);
146 int mm_answer_rsa_keyallowed(int, Buffer *);
147 int mm_answer_rsa_challenge(int, Buffer *);
148 int mm_answer_rsa_response(int, Buffer *);
149 int mm_answer_sesskey(int, Buffer *);
150 int mm_answer_sessid(int, Buffer *);
153 int mm_answer_pam_start(int, Buffer *);
154 int mm_answer_pam_account(int, Buffer *);
155 int mm_answer_pam_init_ctx(int, Buffer *);
156 int mm_answer_pam_query(int, Buffer *);
157 int mm_answer_pam_respond(int, Buffer *);
158 int mm_answer_pam_free_ctx(int, Buffer *);
162 int mm_answer_gss_setup_ctx(int, Buffer *);
163 int mm_answer_gss_accept_ctx(int, Buffer *);
164 int mm_answer_gss_userok(int, Buffer *);
165 int mm_answer_gss_checkmic(int, Buffer *);
166 int mm_answer_gss_sign(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},
236 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
241 struct mon_table mon_dispatch_postauth20[] = {
243 {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
244 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
245 {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
247 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
248 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
249 {MONITOR_REQ_PTY, 0, mm_answer_pty},
250 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
251 {MONITOR_REQ_TERM, 0, mm_answer_term},
252 #ifdef SSH_AUDIT_EVENTS
253 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
254 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
259 struct mon_table mon_dispatch_proto15[] = {
260 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
261 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
262 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
263 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
264 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
265 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
266 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
267 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
269 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
270 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
273 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
274 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
277 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
278 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
279 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
280 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
281 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
282 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
284 #ifdef SSH_AUDIT_EVENTS
285 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
290 struct mon_table mon_dispatch_postauth15[] = {
291 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
292 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
293 {MONITOR_REQ_TERM, 0, mm_answer_term},
294 #ifdef SSH_AUDIT_EVENTS
295 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
296 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
301 struct mon_table *mon_dispatch;
303 /* Specifies if a certain message is allowed at the moment */
306 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
308 while (ent->f != NULL) {
309 if (ent->type == type) {
310 ent->flags &= ~MON_PERMIT;
311 ent->flags |= permit ? MON_PERMIT : 0;
319 monitor_permit_authentications(int permit)
321 struct mon_table *ent = mon_dispatch;
323 while (ent->f != NULL) {
324 if (ent->flags & MON_AUTH) {
325 ent->flags &= ~MON_PERMIT;
326 ent->flags |= permit ? MON_PERMIT : 0;
333 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
335 struct mon_table *ent;
336 int authenticated = 0;
338 debug3("preauth child monitor started");
340 authctxt = _authctxt;
341 memset(authctxt, 0, sizeof(*authctxt));
343 authctxt->loginmsg = &loginmsg;
346 mon_dispatch = mon_dispatch_proto20;
348 /* Permit requests for moduli and signatures */
349 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
350 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
352 /* and for the GSSAPI key exchange */
353 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
356 mon_dispatch = mon_dispatch_proto15;
358 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
361 /* The first few requests do not require asynchronous access */
362 while (!authenticated) {
363 auth_method = "unknown";
364 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
366 if (!(ent->flags & MON_AUTHDECIDE))
367 fatal("%s: unexpected authentication from %d",
368 __func__, ent->type);
369 if (authctxt->pw->pw_uid == 0 &&
370 !auth_root_allowed(auth_method))
373 /* PAM needs to perform account checks after auth */
374 if (options.use_pam && authenticated) {
378 mm_request_receive_expect(pmonitor->m_sendfd,
379 MONITOR_REQ_PAM_ACCOUNT, &m);
380 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
386 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
387 auth_log(authctxt, authenticated, auth_method,
388 compat20 ? " ssh2" : "");
390 authctxt->failures++;
394 if (!authctxt->valid)
395 fatal("%s: authenticated invalid user", __func__);
396 if (strcmp(auth_method, "unknown") == 0)
397 fatal("%s: authentication method name unknown", __func__);
399 debug("%s: %s has been authenticated by privileged process",
400 __func__, authctxt->user);
402 mm_get_keystate(pmonitor);
406 monitor_set_child_handler(pid_t pid)
408 monitor_child_pid = pid;
412 monitor_child_handler(int sig)
414 kill(monitor_child_pid, sig);
418 monitor_child_postauth(struct monitor *pmonitor)
420 monitor_set_child_handler(pmonitor->m_pid);
421 signal(SIGHUP, &monitor_child_handler);
422 signal(SIGTERM, &monitor_child_handler);
425 mon_dispatch = mon_dispatch_postauth20;
427 /* Permit requests for moduli and signatures */
428 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
429 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
430 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
432 /* and for the GSSAPI key exchange */
433 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
436 mon_dispatch = mon_dispatch_postauth15;
437 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
440 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
441 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
445 monitor_read(pmonitor, mon_dispatch, NULL);
449 monitor_sync(struct monitor *pmonitor)
451 if (options.compression) {
452 /* The member allocation is not visible, so sync it */
453 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
458 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
459 struct mon_table **pent)
467 mm_request_receive(pmonitor->m_sendfd, &m);
468 type = buffer_get_char(&m);
470 debug3("%s: checking request %d", __func__, type);
472 while (ent->f != NULL) {
473 if (ent->type == type)
478 if (ent->f != NULL) {
479 if (!(ent->flags & MON_PERMIT))
480 fatal("%s: unpermitted request %d", __func__,
482 ret = (*ent->f)(pmonitor->m_sendfd, &m);
485 /* The child may use this request only once, disable it */
486 if (ent->flags & MON_ONCE) {
487 debug2("%s: %d used once, disabling now", __func__,
489 ent->flags &= ~MON_PERMIT;
498 fatal("%s: unsupported request: %d", __func__, type);
504 /* allowed key state */
506 monitor_allowed_key(u_char *blob, u_int bloblen)
508 /* make sure key is allowed */
509 if (key_blob == NULL || key_bloblen != bloblen ||
510 memcmp(key_blob, blob, key_bloblen))
516 monitor_reset_key_state(void)
519 if (key_blob != NULL)
521 if (hostbased_cuser != NULL)
522 xfree(hostbased_cuser);
523 if (hostbased_chost != NULL)
524 xfree(hostbased_chost);
527 key_blobtype = MM_NOKEY;
528 hostbased_cuser = NULL;
529 hostbased_chost = NULL;
533 mm_answer_moduli(int sock, Buffer *m)
538 min = buffer_get_int(m);
539 want = buffer_get_int(m);
540 max = buffer_get_int(m);
542 debug3("%s: got parameters: %d %d %d",
543 __func__, min, want, max);
544 /* We need to check here, too, in case the child got corrupted */
545 if (max < min || want < min || max < want)
546 fatal("%s: bad parameters: %d %d %d",
547 __func__, min, want, max);
551 dh = choose_dh(min, want, max);
553 buffer_put_char(m, 0);
556 /* Send first bignum */
557 buffer_put_char(m, 1);
558 buffer_put_bignum2(m, dh->p);
559 buffer_put_bignum2(m, dh->g);
563 mm_request_send(sock, MONITOR_ANS_MODULI, m);
568 mm_answer_sign(int sock, Buffer *m)
573 u_int siglen, datlen;
576 debug3("%s", __func__);
578 keyid = buffer_get_int(m);
579 p = buffer_get_string(m, &datlen);
582 * Supported KEX types will only return SHA1 (20 byte) or
583 * SHA256 (32 byte) hashes
585 if (datlen != 20 && datlen != 32)
586 fatal("%s: data length incorrect: %u", __func__, datlen);
588 /* save session id, it will be passed on the first call */
589 if (session_id2_len == 0) {
590 session_id2_len = datlen;
591 session_id2 = xmalloc(session_id2_len);
592 memcpy(session_id2, p, session_id2_len);
595 if ((key = get_hostkey_by_index(keyid)) == NULL)
596 fatal("%s: no hostkey from index %d", __func__, keyid);
597 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
598 fatal("%s: key_sign failed", __func__);
600 debug3("%s: signature %p(%u)", __func__, signature, siglen);
603 buffer_put_string(m, signature, siglen);
608 mm_request_send(sock, MONITOR_ANS_SIGN, m);
610 /* Turn on permissions for getpwnam */
611 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
616 /* Retrieves the password entry and also checks if the user is permitted */
619 mm_answer_pwnamallow(int sock, Buffer *m)
622 struct passwd *pwent;
625 debug3("%s", __func__);
627 if (authctxt->attempt++ != 0)
628 fatal("%s: multiple attempts for getpwnam", __func__);
630 username = buffer_get_string(m, NULL);
632 pwent = getpwnamallow(username);
634 authctxt->user = xstrdup(username);
635 setproctitle("%s [priv]", pwent ? username : "unknown");
641 buffer_put_char(m, 0);
642 authctxt->pw = fakepw();
647 authctxt->pw = pwent;
650 buffer_put_char(m, 1);
651 buffer_put_string(m, pwent, sizeof(struct passwd));
652 buffer_put_cstring(m, pwent->pw_name);
653 buffer_put_cstring(m, "*");
654 buffer_put_cstring(m, pwent->pw_gecos);
655 #ifdef HAVE_PW_CLASS_IN_PASSWD
656 buffer_put_cstring(m, pwent->pw_class);
658 buffer_put_cstring(m, pwent->pw_dir);
659 buffer_put_cstring(m, pwent->pw_shell);
662 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
663 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
665 /* For SSHv1 allow authentication now */
667 monitor_permit_authentications(1);
669 /* Allow service/style information on the auth context */
670 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
671 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
676 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
682 int mm_answer_auth2_read_banner(int sock, Buffer *m)
687 banner = auth2_read_banner();
688 buffer_put_cstring(m, banner != NULL ? banner : "");
689 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
698 mm_answer_authserv(int sock, Buffer *m)
700 monitor_permit_authentications(1);
702 authctxt->service = buffer_get_string(m, NULL);
703 authctxt->style = buffer_get_string(m, NULL);
704 debug3("%s: service=%s, style=%s",
705 __func__, authctxt->service, authctxt->style);
707 if (strlen(authctxt->style) == 0) {
708 xfree(authctxt->style);
709 authctxt->style = NULL;
716 mm_answer_authpassword(int sock, Buffer *m)
718 static int call_count;
723 passwd = buffer_get_string(m, &plen);
724 /* Only authenticate if the context is valid */
725 authenticated = options.password_authentication &&
726 auth_password(authctxt, passwd);
727 memset(passwd, 0, strlen(passwd));
731 buffer_put_int(m, authenticated);
733 debug3("%s: sending result %d", __func__, authenticated);
734 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
737 if (plen == 0 && call_count == 1)
738 auth_method = "none";
740 auth_method = "password";
742 /* Causes monitor loop to terminate if authenticated */
743 return (authenticated);
748 mm_answer_bsdauthquery(int sock, Buffer *m)
750 char *name, *infotxt;
756 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
757 &prompts, &echo_on) < 0 ? 0 : 1;
760 buffer_put_int(m, success);
762 buffer_put_cstring(m, prompts[0]);
764 debug3("%s: sending challenge success: %u", __func__, success);
765 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
778 mm_answer_bsdauthrespond(int sock, Buffer *m)
783 if (authctxt->as == 0)
784 fatal("%s: no bsd auth session", __func__);
786 response = buffer_get_string(m, NULL);
787 authok = options.challenge_response_authentication &&
788 auth_userresponse(authctxt->as, response, 0);
790 debug3("%s: <%s> = <%d>", __func__, response, authok);
794 buffer_put_int(m, authok);
796 debug3("%s: sending authenticated: %d", __func__, authok);
797 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
799 auth_method = "bsdauth";
801 return (authok != 0);
807 mm_answer_skeyquery(int sock, Buffer *m)
810 char challenge[1024];
813 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
814 sizeof(challenge)) < 0 ? 0 : 1;
817 buffer_put_int(m, success);
819 buffer_put_cstring(m, challenge);
821 debug3("%s: sending challenge success: %u", __func__, success);
822 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
828 mm_answer_skeyrespond(int sock, Buffer *m)
833 response = buffer_get_string(m, NULL);
835 authok = (options.challenge_response_authentication &&
837 skey_haskey(authctxt->pw->pw_name) == 0 &&
838 skey_passcheck(authctxt->pw->pw_name, response) != -1);
843 buffer_put_int(m, authok);
845 debug3("%s: sending authenticated: %d", __func__, authok);
846 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
848 auth_method = "skey";
850 return (authok != 0);
856 mm_answer_pam_start(int sock, Buffer *m)
858 if (!options.use_pam)
859 fatal("UsePAM not set, but ended up in %s anyway", __func__);
863 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
869 mm_answer_pam_account(int sock, Buffer *m)
873 if (!options.use_pam)
874 fatal("UsePAM not set, but ended up in %s anyway", __func__);
876 ret = do_pam_account();
878 buffer_put_int(m, ret);
879 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
881 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
886 static void *sshpam_ctxt, *sshpam_authok;
887 extern KbdintDevice sshpam_device;
890 mm_answer_pam_init_ctx(int sock, Buffer *m)
893 debug3("%s", __func__);
894 authctxt->user = buffer_get_string(m, NULL);
895 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
896 sshpam_authok = NULL;
898 if (sshpam_ctxt != NULL) {
899 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
900 buffer_put_int(m, 1);
902 buffer_put_int(m, 0);
904 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
909 mm_answer_pam_query(int sock, Buffer *m)
911 char *name, *info, **prompts;
912 u_int i, num, *echo_on;
915 debug3("%s", __func__);
916 sshpam_authok = NULL;
917 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
918 if (ret == 0 && num == 0)
919 sshpam_authok = sshpam_ctxt;
920 if (num > 1 || name == NULL || info == NULL)
923 buffer_put_int(m, ret);
924 buffer_put_cstring(m, name);
926 buffer_put_cstring(m, info);
928 buffer_put_int(m, num);
929 for (i = 0; i < num; ++i) {
930 buffer_put_cstring(m, prompts[i]);
932 buffer_put_int(m, echo_on[i]);
938 auth_method = "keyboard-interactive/pam";
939 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
944 mm_answer_pam_respond(int sock, Buffer *m)
950 debug3("%s", __func__);
951 sshpam_authok = NULL;
952 num = buffer_get_int(m);
954 resp = xcalloc(num, sizeof(char *));
955 for (i = 0; i < num; ++i)
956 resp[i] = buffer_get_string(m, NULL);
957 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
958 for (i = 0; i < num; ++i)
962 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
965 buffer_put_int(m, ret);
966 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
967 auth_method = "keyboard-interactive/pam";
969 sshpam_authok = sshpam_ctxt;
974 mm_answer_pam_free_ctx(int sock, Buffer *m)
977 debug3("%s", __func__);
978 (sshpam_device.free_ctx)(sshpam_ctxt);
980 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
981 auth_method = "keyboard-interactive/pam";
982 return (sshpam_authok == sshpam_ctxt);
987 mm_append_debug(Buffer *m)
989 if (auth_debug_init && buffer_len(&auth_debug)) {
990 debug3("%s: Appending debug messages for child", __func__);
991 buffer_append(m, buffer_ptr(&auth_debug),
992 buffer_len(&auth_debug));
993 buffer_clear(&auth_debug);
998 mm_answer_keyallowed(int sock, Buffer *m)
1001 char *cuser, *chost;
1004 enum mm_keytype type = 0;
1007 debug3("%s entering", __func__);
1009 type = buffer_get_int(m);
1010 cuser = buffer_get_string(m, NULL);
1011 chost = buffer_get_string(m, NULL);
1012 blob = buffer_get_string(m, &bloblen);
1014 key = key_from_blob(blob, bloblen);
1016 if ((compat20 && type == MM_RSAHOSTKEY) ||
1017 (!compat20 && type != MM_RSAHOSTKEY))
1018 fatal("%s: key type and protocol mismatch", __func__);
1020 debug3("%s: key_from_blob: %p", __func__, key);
1022 if (key != NULL && authctxt->valid) {
1025 allowed = options.pubkey_authentication &&
1026 user_key_allowed(authctxt->pw, key);
1027 auth_method = "publickey";
1030 allowed = options.hostbased_authentication &&
1031 hostbased_key_allowed(authctxt->pw,
1033 auth_method = "hostbased";
1036 key->type = KEY_RSA1; /* XXX */
1037 allowed = options.rhosts_rsa_authentication &&
1038 auth_rhosts_rsa_key_allowed(authctxt->pw,
1040 auth_method = "rsa";
1043 fatal("%s: unknown key type %d", __func__, type);
1050 /* clear temporarily storage (used by verify) */
1051 monitor_reset_key_state();
1054 /* Save temporarily for comparison in verify */
1056 key_bloblen = bloblen;
1057 key_blobtype = type;
1058 hostbased_cuser = cuser;
1059 hostbased_chost = chost;
1061 /* Log failed attempt */
1062 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1068 debug3("%s: key %p is %s",
1069 __func__, key, allowed ? "allowed" : "disallowed");
1072 buffer_put_int(m, allowed);
1073 buffer_put_int(m, forced_command != NULL);
1077 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1079 if (type == MM_RSAHOSTKEY)
1080 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1086 monitor_valid_userblob(u_char *data, u_int datalen)
1094 buffer_append(&b, data, datalen);
1096 if (datafellows & SSH_OLD_SESSIONID) {
1098 len = buffer_len(&b);
1099 if ((session_id2 == NULL) ||
1100 (len < session_id2_len) ||
1101 (memcmp(p, session_id2, session_id2_len) != 0))
1103 buffer_consume(&b, session_id2_len);
1105 p = buffer_get_string(&b, &len);
1106 if ((session_id2 == NULL) ||
1107 (len != session_id2_len) ||
1108 (memcmp(p, session_id2, session_id2_len) != 0))
1112 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1114 p = buffer_get_string(&b, NULL);
1115 if (strcmp(authctxt->user, p) != 0) {
1116 logit("wrong user name passed to monitor: expected %s != %.100s",
1121 buffer_skip_string(&b);
1122 if (datafellows & SSH_BUG_PKAUTH) {
1123 if (!buffer_get_char(&b))
1126 p = buffer_get_string(&b, NULL);
1127 if (strcmp("publickey", p) != 0)
1130 if (!buffer_get_char(&b))
1132 buffer_skip_string(&b);
1134 buffer_skip_string(&b);
1135 if (buffer_len(&b) != 0)
1142 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1151 buffer_append(&b, data, datalen);
1153 p = buffer_get_string(&b, &len);
1154 if ((session_id2 == NULL) ||
1155 (len != session_id2_len) ||
1156 (memcmp(p, session_id2, session_id2_len) != 0))
1160 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1162 p = buffer_get_string(&b, NULL);
1163 if (strcmp(authctxt->user, p) != 0) {
1164 logit("wrong user name passed to monitor: expected %s != %.100s",
1169 buffer_skip_string(&b); /* service */
1170 p = buffer_get_string(&b, NULL);
1171 if (strcmp(p, "hostbased") != 0)
1174 buffer_skip_string(&b); /* pkalg */
1175 buffer_skip_string(&b); /* pkblob */
1177 /* verify client host, strip trailing dot if necessary */
1178 p = buffer_get_string(&b, NULL);
1179 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1181 if (strcmp(p, chost) != 0)
1185 /* verify client user */
1186 p = buffer_get_string(&b, NULL);
1187 if (strcmp(p, cuser) != 0)
1191 if (buffer_len(&b) != 0)
1198 mm_answer_keyverify(int sock, Buffer *m)
1201 u_char *signature, *data, *blob;
1202 u_int signaturelen, datalen, bloblen;
1206 blob = buffer_get_string(m, &bloblen);
1207 signature = buffer_get_string(m, &signaturelen);
1208 data = buffer_get_string(m, &datalen);
1210 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1211 !monitor_allowed_key(blob, bloblen))
1212 fatal("%s: bad key, not previously allowed", __func__);
1214 key = key_from_blob(blob, bloblen);
1216 fatal("%s: bad public key blob", __func__);
1218 switch (key_blobtype) {
1220 valid_data = monitor_valid_userblob(data, datalen);
1223 valid_data = monitor_valid_hostbasedblob(data, datalen,
1224 hostbased_cuser, hostbased_chost);
1231 fatal("%s: bad signature data blob", __func__);
1233 verified = key_verify(key, signature, signaturelen, data, datalen);
1234 debug3("%s: key %p signature %s",
1235 __func__, key, verified ? "verified" : "unverified");
1242 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1244 monitor_reset_key_state();
1247 buffer_put_int(m, verified);
1248 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1254 mm_record_login(Session *s, struct passwd *pw)
1257 struct sockaddr_storage from;
1260 * Get IP address of client. If the connection is not a socket, let
1261 * the address be 0.0.0.0.
1263 memset(&from, 0, sizeof(from));
1264 fromlen = sizeof(from);
1265 if (packet_connection_is_on_socket()) {
1266 if (getpeername(packet_get_connection_in(),
1267 (struct sockaddr *)&from, &fromlen) < 0) {
1268 debug("getpeername: %.100s", strerror(errno));
1272 /* Record that there was a login on that tty from the remote host. */
1273 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1274 get_remote_name_or_ip(utmp_len, options.use_dns),
1275 (struct sockaddr *)&from, fromlen);
1279 mm_session_close(Session *s)
1281 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1282 if (s->ttyfd != -1) {
1283 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1284 session_pty_cleanup2(s);
1290 mm_answer_pty(int sock, Buffer *m)
1292 extern struct monitor *pmonitor;
1296 debug3("%s entering", __func__);
1302 s->authctxt = authctxt;
1303 s->pw = authctxt->pw;
1304 s->pid = pmonitor->m_pid;
1305 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1308 pty_setowner(authctxt->pw, s->tty);
1310 buffer_put_int(m, 1);
1311 buffer_put_cstring(m, s->tty);
1313 /* We need to trick ttyslot */
1314 if (dup2(s->ttyfd, 0) == -1)
1315 fatal("%s: dup2", __func__);
1317 mm_record_login(s, authctxt->pw);
1319 /* Now we can close the file descriptor again */
1322 /* send messages generated by record_login */
1323 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1324 buffer_clear(&loginmsg);
1326 mm_request_send(sock, MONITOR_ANS_PTY, m);
1328 mm_send_fd(sock, s->ptyfd);
1329 mm_send_fd(sock, s->ttyfd);
1331 /* make sure nothing uses fd 0 */
1332 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1333 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1335 error("%s: fd0 %d != 0", __func__, fd0);
1337 /* slave is not needed */
1339 s->ttyfd = s->ptyfd;
1340 /* no need to dup() because nobody closes ptyfd */
1341 s->ptymaster = s->ptyfd;
1343 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1349 mm_session_close(s);
1350 buffer_put_int(m, 0);
1351 mm_request_send(sock, MONITOR_ANS_PTY, m);
1356 mm_answer_pty_cleanup(int sock, Buffer *m)
1361 debug3("%s entering", __func__);
1363 tty = buffer_get_string(m, NULL);
1364 if ((s = session_by_tty(tty)) != NULL)
1365 mm_session_close(s);
1372 mm_answer_sesskey(int sock, Buffer *m)
1377 /* Turn off permissions */
1378 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1380 if ((p = BN_new()) == NULL)
1381 fatal("%s: BN_new", __func__);
1383 buffer_get_bignum2(m, p);
1385 rsafail = ssh1_session_key(p);
1388 buffer_put_int(m, rsafail);
1389 buffer_put_bignum2(m, p);
1393 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1395 /* Turn on permissions for sessid passing */
1396 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1402 mm_answer_sessid(int sock, Buffer *m)
1406 debug3("%s entering", __func__);
1408 if (buffer_len(m) != 16)
1409 fatal("%s: bad ssh1 session id", __func__);
1410 for (i = 0; i < 16; i++)
1411 session_id[i] = buffer_get_char(m);
1413 /* Turn on permissions for getpwnam */
1414 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1420 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1424 u_char *blob = NULL;
1428 debug3("%s entering", __func__);
1430 auth_method = "rsa";
1431 if (options.rsa_authentication && authctxt->valid) {
1432 if ((client_n = BN_new()) == NULL)
1433 fatal("%s: BN_new", __func__);
1434 buffer_get_bignum2(m, client_n);
1435 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1436 BN_clear_free(client_n);
1439 buffer_put_int(m, allowed);
1440 buffer_put_int(m, forced_command != NULL);
1442 /* clear temporarily storage (used by generate challenge) */
1443 monitor_reset_key_state();
1445 if (allowed && key != NULL) {
1446 key->type = KEY_RSA; /* cheat for key_to_blob */
1447 if (key_to_blob(key, &blob, &blen) == 0)
1448 fatal("%s: key_to_blob failed", __func__);
1449 buffer_put_string(m, blob, blen);
1451 /* Save temporarily for comparison in verify */
1454 key_blobtype = MM_RSAUSERKEY;
1461 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1463 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1464 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1469 mm_answer_rsa_challenge(int sock, Buffer *m)
1475 debug3("%s entering", __func__);
1477 if (!authctxt->valid)
1478 fatal("%s: authctxt not valid", __func__);
1479 blob = buffer_get_string(m, &blen);
1480 if (!monitor_allowed_key(blob, blen))
1481 fatal("%s: bad key, not previously allowed", __func__);
1482 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1483 fatal("%s: key type mismatch", __func__);
1484 if ((key = key_from_blob(blob, blen)) == NULL)
1485 fatal("%s: received bad key", __func__);
1488 BN_clear_free(ssh1_challenge);
1489 ssh1_challenge = auth_rsa_generate_challenge(key);
1492 buffer_put_bignum2(m, ssh1_challenge);
1494 debug3("%s sending reply", __func__);
1495 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1497 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1505 mm_answer_rsa_response(int sock, Buffer *m)
1508 u_char *blob, *response;
1512 debug3("%s entering", __func__);
1514 if (!authctxt->valid)
1515 fatal("%s: authctxt not valid", __func__);
1516 if (ssh1_challenge == NULL)
1517 fatal("%s: no ssh1_challenge", __func__);
1519 blob = buffer_get_string(m, &blen);
1520 if (!monitor_allowed_key(blob, blen))
1521 fatal("%s: bad key, not previously allowed", __func__);
1522 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1523 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1524 if ((key = key_from_blob(blob, blen)) == NULL)
1525 fatal("%s: received bad key", __func__);
1526 response = buffer_get_string(m, &len);
1528 fatal("%s: received bad response to challenge", __func__);
1529 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1535 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1538 BN_clear_free(ssh1_challenge);
1539 ssh1_challenge = NULL;
1540 monitor_reset_key_state();
1543 buffer_put_int(m, success);
1544 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1550 mm_answer_term(int sock, Buffer *req)
1552 extern struct monitor *pmonitor;
1555 debug3("%s: tearing down sessions", __func__);
1557 /* The child is terminating */
1558 session_destroy_all(&mm_session_close);
1560 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1564 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1566 /* Terminate process */
1570 #ifdef SSH_AUDIT_EVENTS
1571 /* Report that an audit event occurred */
1573 mm_answer_audit_event(int socket, Buffer *m)
1575 ssh_audit_event_t event;
1577 debug3("%s entering", __func__);
1579 event = buffer_get_int(m);
1581 case SSH_AUTH_FAIL_PUBKEY:
1582 case SSH_AUTH_FAIL_HOSTBASED:
1583 case SSH_AUTH_FAIL_GSSAPI:
1584 case SSH_LOGIN_EXCEED_MAXTRIES:
1585 case SSH_LOGIN_ROOT_DENIED:
1586 case SSH_CONNECTION_CLOSE:
1587 case SSH_INVALID_USER:
1591 fatal("Audit event type %d not permitted", event);
1598 mm_answer_audit_command(int socket, Buffer *m)
1603 debug3("%s entering", __func__);
1604 cmd = buffer_get_string(m, &len);
1605 /* sanity check command, if so how? */
1606 audit_run_command(cmd);
1610 #endif /* SSH_AUDIT_EVENTS */
1613 monitor_apply_keystate(struct monitor *pmonitor)
1616 set_newkeys(MODE_IN);
1617 set_newkeys(MODE_OUT);
1619 packet_set_protocol_flags(child_state.ssh1protoflags);
1620 packet_set_encryption_key(child_state.ssh1key,
1621 child_state.ssh1keylen, child_state.ssh1cipher);
1622 xfree(child_state.ssh1key);
1625 /* for rc4 and other stateful ciphers */
1626 packet_set_keycontext(MODE_OUT, child_state.keyout);
1627 xfree(child_state.keyout);
1628 packet_set_keycontext(MODE_IN, child_state.keyin);
1629 xfree(child_state.keyin);
1632 packet_set_iv(MODE_OUT, child_state.ivout);
1633 xfree(child_state.ivout);
1634 packet_set_iv(MODE_IN, child_state.ivin);
1635 xfree(child_state.ivin);
1638 memcpy(&incoming_stream, &child_state.incoming,
1639 sizeof(incoming_stream));
1640 memcpy(&outgoing_stream, &child_state.outgoing,
1641 sizeof(outgoing_stream));
1643 /* Update with new address */
1644 if (options.compression)
1645 mm_init_compression(pmonitor->m_zlib);
1647 /* Network I/O buffers */
1648 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1649 buffer_clear(&input);
1650 buffer_append(&input, child_state.input, child_state.ilen);
1651 memset(child_state.input, 0, child_state.ilen);
1652 xfree(child_state.input);
1654 buffer_clear(&output);
1655 buffer_append(&output, child_state.output, child_state.olen);
1656 memset(child_state.output, 0, child_state.olen);
1657 xfree(child_state.output);
1661 mm_get_kex(Buffer *m)
1667 kex = xcalloc(1, sizeof(*kex));
1668 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1669 if ((session_id2 == NULL) ||
1670 (kex->session_id_len != session_id2_len) ||
1671 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1672 fatal("mm_get_get: internal error: bad session id");
1673 kex->we_need = buffer_get_int(m);
1674 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1675 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1676 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1677 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1679 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
1680 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
1681 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
1684 kex->hostkey_type = buffer_get_int(m);
1685 kex->kex_type = buffer_get_int(m);
1686 blob = buffer_get_string(m, &bloblen);
1687 buffer_init(&kex->my);
1688 buffer_append(&kex->my, blob, bloblen);
1690 blob = buffer_get_string(m, &bloblen);
1691 buffer_init(&kex->peer);
1692 buffer_append(&kex->peer, blob, bloblen);
1695 kex->flags = buffer_get_int(m);
1696 kex->client_version_string = buffer_get_string(m, NULL);
1697 kex->server_version_string = buffer_get_string(m, NULL);
1698 kex->load_host_key=&get_hostkey_by_type;
1699 kex->host_key_index=&get_hostkey_index;
1704 /* This function requries careful sanity checking */
1707 mm_get_keystate(struct monitor *pmonitor)
1711 u_int bloblen, plen;
1712 u_int32_t seqnr, packets;
1715 debug3("%s: Waiting for new keys", __func__);
1718 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1720 child_state.ssh1protoflags = buffer_get_int(&m);
1721 child_state.ssh1cipher = buffer_get_int(&m);
1722 child_state.ssh1key = buffer_get_string(&m,
1723 &child_state.ssh1keylen);
1724 child_state.ivout = buffer_get_string(&m,
1725 &child_state.ivoutlen);
1726 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1729 /* Get the Kex for rekeying */
1730 *pmonitor->m_pkex = mm_get_kex(&m);
1733 blob = buffer_get_string(&m, &bloblen);
1734 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1737 debug3("%s: Waiting for second key", __func__);
1738 blob = buffer_get_string(&m, &bloblen);
1739 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1742 /* Now get sequence numbers for the packets */
1743 seqnr = buffer_get_int(&m);
1744 blocks = buffer_get_int64(&m);
1745 packets = buffer_get_int(&m);
1746 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1747 seqnr = buffer_get_int(&m);
1748 blocks = buffer_get_int64(&m);
1749 packets = buffer_get_int(&m);
1750 packet_set_state(MODE_IN, seqnr, blocks, packets);
1753 /* Get the key context */
1754 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1755 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1757 debug3("%s: Getting compression state", __func__);
1758 /* Get compression state */
1759 p = buffer_get_string(&m, &plen);
1760 if (plen != sizeof(child_state.outgoing))
1761 fatal("%s: bad request size", __func__);
1762 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1765 p = buffer_get_string(&m, &plen);
1766 if (plen != sizeof(child_state.incoming))
1767 fatal("%s: bad request size", __func__);
1768 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1771 /* Network I/O buffers */
1772 debug3("%s: Getting Network I/O buffers", __func__);
1773 child_state.input = buffer_get_string(&m, &child_state.ilen);
1774 child_state.output = buffer_get_string(&m, &child_state.olen);
1780 /* Allocation functions for zlib */
1782 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1784 size_t len = (size_t) size * ncount;
1787 if (len == 0 || ncount > SIZE_T_MAX / size)
1788 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1790 address = mm_malloc(mm, len);
1796 mm_zfree(struct mm_master *mm, void *address)
1798 mm_free(mm, address);
1802 mm_init_compression(struct mm_master *mm)
1804 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1805 outgoing_stream.zfree = (free_func)mm_zfree;
1806 outgoing_stream.opaque = mm;
1808 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1809 incoming_stream.zfree = (free_func)mm_zfree;
1810 incoming_stream.opaque = mm;
1815 #define FD_CLOSEONEXEC(x) do { \
1816 if (fcntl(x, F_SETFD, 1) == -1) \
1817 fatal("fcntl(%d, F_SETFD)", x); \
1821 monitor_socketpair(int *pair)
1823 #ifdef HAVE_SOCKETPAIR
1824 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1825 fatal("%s: socketpair", __func__);
1827 fatal("%s: UsePrivilegeSeparation=yes not supported",
1830 FD_CLOSEONEXEC(pair[0]);
1831 FD_CLOSEONEXEC(pair[1]);
1834 #define MM_MEMSIZE 65536
1839 struct monitor *mon;
1842 mon = xcalloc(1, sizeof(*mon));
1844 monitor_socketpair(pair);
1846 mon->m_recvfd = pair[0];
1847 mon->m_sendfd = pair[1];
1849 /* Used to share zlib space across processes */
1850 if (options.compression) {
1851 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1852 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1854 /* Compression needs to share state across borders */
1855 mm_init_compression(mon->m_zlib);
1862 monitor_reinit(struct monitor *mon)
1866 monitor_socketpair(pair);
1868 mon->m_recvfd = pair[0];
1869 mon->m_sendfd = pair[1];
1874 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1880 goid.elements = buffer_get_string(m, &len);
1883 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1885 xfree(goid.elements);
1888 buffer_put_int(m, major);
1890 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1892 /* Now we have a context, enable the step */
1893 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1899 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1902 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1903 OM_uint32 major, minor;
1904 OM_uint32 flags = 0; /* GSI needs this */
1907 in.value = buffer_get_string(m, &len);
1909 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1913 buffer_put_int(m, major);
1914 buffer_put_string(m, out.value, out.length);
1915 buffer_put_int(m, flags);
1916 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1918 gss_release_buffer(&minor, &out);
1920 if (major == GSS_S_COMPLETE) {
1921 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1922 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1923 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1924 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
1930 mm_answer_gss_checkmic(int sock, Buffer *m)
1932 gss_buffer_desc gssbuf, mic;
1936 gssbuf.value = buffer_get_string(m, &len);
1937 gssbuf.length = len;
1938 mic.value = buffer_get_string(m, &len);
1941 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1943 xfree(gssbuf.value);
1947 buffer_put_int(m, ret);
1949 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1951 if (!GSS_ERROR(ret))
1952 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1958 mm_answer_gss_userok(int sock, Buffer *m)
1962 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1965 buffer_put_int(m, authenticated);
1967 debug3("%s: sending result %d", __func__, authenticated);
1968 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1970 auth_method = "gssapi-with-mic";
1972 /* Monitor loop will terminate if authenticated */
1973 return (authenticated);
1977 mm_answer_gss_sign(int socket, Buffer *m)
1979 gss_buffer_desc data;
1980 gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
1981 OM_uint32 major, minor;
1984 data.value = buffer_get_string(m, &len);
1986 if (data.length != 20)
1987 fatal("%s: data length incorrect: %d", __func__, data.length);
1989 /* Save the session ID on the first time around */
1990 if (session_id2_len == 0) {
1991 session_id2_len = data.length;
1992 session_id2 = xmalloc(session_id2_len);
1993 memcpy(session_id2, data.value, session_id2_len);
1995 major = ssh_gssapi_sign(gsscontext, &data, &hash);
2000 buffer_put_int(m, major);
2001 buffer_put_string(m, hash.value, hash.length);
2003 mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
2005 gss_release_buffer(&minor, &hash);
2007 /* Turn on getpwnam permissions */
2008 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);