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"
51 #include <openssl/dh.h>
62 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
65 #define TARGET_OS_MAC 1
70 #include "auth-options.h"
79 #include "monitor_mm.h"
83 #include "monitor_wrap.h"
84 #include "monitor_fdpass.h"
90 static Gssctxt *gsscontext = NULL;
94 extern ServerOptions options;
95 extern u_int utmp_len;
96 extern Newkeys *current_keys[];
97 extern z_stream incoming_stream;
98 extern z_stream outgoing_stream;
99 extern u_char session_id[];
100 extern Buffer input, output;
101 extern Buffer auth_debug;
102 extern int auth_debug_init;
103 extern Buffer loginmsg;
105 /* State exported from the child */
128 /* Functions on the monitor that answer unprivileged requests */
130 int mm_answer_moduli(int, Buffer *);
131 int mm_answer_sign(int, Buffer *);
132 int mm_answer_pwnamallow(int, Buffer *);
133 int mm_answer_auth2_read_banner(int, Buffer *);
134 int mm_answer_authserv(int, Buffer *);
135 int mm_answer_authpassword(int, Buffer *);
136 int mm_answer_bsdauthquery(int, Buffer *);
137 int mm_answer_bsdauthrespond(int, Buffer *);
138 int mm_answer_skeyquery(int, Buffer *);
139 int mm_answer_skeyrespond(int, Buffer *);
140 int mm_answer_keyallowed(int, Buffer *);
141 int mm_answer_keyverify(int, Buffer *);
142 int mm_answer_pty(int, Buffer *);
143 int mm_answer_pty_cleanup(int, Buffer *);
144 int mm_answer_term(int, Buffer *);
145 int mm_answer_rsa_keyallowed(int, Buffer *);
146 int mm_answer_rsa_challenge(int, Buffer *);
147 int mm_answer_rsa_response(int, Buffer *);
148 int mm_answer_sesskey(int, Buffer *);
149 int mm_answer_sessid(int, Buffer *);
152 int mm_answer_pam_start(int, Buffer *);
153 int mm_answer_pam_account(int, Buffer *);
154 int mm_answer_pam_init_ctx(int, Buffer *);
155 int mm_answer_pam_query(int, Buffer *);
156 int mm_answer_pam_respond(int, Buffer *);
157 int mm_answer_pam_free_ctx(int, Buffer *);
161 int mm_answer_gss_setup_ctx(int, Buffer *);
162 int mm_answer_gss_accept_ctx(int, Buffer *);
163 int mm_answer_gss_userok(int, Buffer *);
164 int mm_answer_gss_checkmic(int, Buffer *);
167 #ifdef SSH_AUDIT_EVENTS
168 int mm_answer_audit_event(int, Buffer *);
169 int mm_answer_audit_command(int, Buffer *);
172 static Authctxt *authctxt;
173 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
175 /* local state for key verify */
176 static u_char *key_blob = NULL;
177 static u_int key_bloblen = 0;
178 static int key_blobtype = MM_NOKEY;
179 static char *hostbased_cuser = NULL;
180 static char *hostbased_chost = NULL;
181 static char *auth_method = "unknown";
182 static u_int session_id2_len = 0;
183 static u_char *session_id2 = NULL;
184 static pid_t monitor_child_pid;
187 enum monitor_reqtype type;
189 int (*f)(int, Buffer *);
192 #define MON_ISAUTH 0x0004 /* Required for Authentication */
193 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
194 #define MON_ONCE 0x0010 /* Disable after calling */
195 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
197 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
199 #define MON_PERMIT 0x1000 /* Request is permitted */
201 struct mon_table mon_dispatch_proto20[] = {
202 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
203 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
204 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
205 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
206 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
207 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
209 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
210 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
211 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
212 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
213 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
214 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
216 #ifdef SSH_AUDIT_EVENTS
217 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
220 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
221 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
224 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
225 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
227 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
228 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
230 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
231 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
232 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
233 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
238 struct mon_table mon_dispatch_postauth20[] = {
239 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
240 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
241 {MONITOR_REQ_PTY, 0, mm_answer_pty},
242 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
243 {MONITOR_REQ_TERM, 0, mm_answer_term},
244 #ifdef SSH_AUDIT_EVENTS
245 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
246 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
251 struct mon_table mon_dispatch_proto15[] = {
252 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
253 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
254 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
255 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
256 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
257 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
258 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
259 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
261 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
262 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
265 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
266 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
269 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
270 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
271 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
272 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
273 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
274 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
276 #ifdef SSH_AUDIT_EVENTS
277 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
282 struct mon_table mon_dispatch_postauth15[] = {
283 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
284 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
285 {MONITOR_REQ_TERM, 0, mm_answer_term},
286 #ifdef SSH_AUDIT_EVENTS
287 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
288 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
293 struct mon_table *mon_dispatch;
295 /* Specifies if a certain message is allowed at the moment */
298 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
300 while (ent->f != NULL) {
301 if (ent->type == type) {
302 ent->flags &= ~MON_PERMIT;
303 ent->flags |= permit ? MON_PERMIT : 0;
311 monitor_permit_authentications(int permit)
313 struct mon_table *ent = mon_dispatch;
315 while (ent->f != NULL) {
316 if (ent->flags & MON_AUTH) {
317 ent->flags &= ~MON_PERMIT;
318 ent->flags |= permit ? MON_PERMIT : 0;
325 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
327 struct mon_table *ent;
328 int authenticated = 0;
330 debug3("preauth child monitor started");
332 authctxt = _authctxt;
333 memset(authctxt, 0, sizeof(*authctxt));
335 authctxt->loginmsg = &loginmsg;
338 mon_dispatch = mon_dispatch_proto20;
340 /* Permit requests for moduli and signatures */
341 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
342 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
344 mon_dispatch = mon_dispatch_proto15;
346 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
349 /* The first few requests do not require asynchronous access */
350 while (!authenticated) {
351 auth_method = "unknown";
352 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
354 if (!(ent->flags & MON_AUTHDECIDE))
355 fatal("%s: unexpected authentication from %d",
356 __func__, ent->type);
357 if (authctxt->pw->pw_uid == 0 &&
358 !auth_root_allowed(auth_method))
361 /* PAM needs to perform account checks after auth */
362 if (options.use_pam && authenticated) {
366 mm_request_receive_expect(pmonitor->m_sendfd,
367 MONITOR_REQ_PAM_ACCOUNT, &m);
368 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
374 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
375 auth_log(authctxt, authenticated, auth_method,
376 compat20 ? " ssh2" : "");
378 authctxt->failures++;
382 if (!authctxt->valid)
383 fatal("%s: authenticated invalid user", __func__);
384 if (strcmp(auth_method, "unknown") == 0)
385 fatal("%s: authentication method name unknown", __func__);
387 debug("%s: %s has been authenticated by privileged process",
388 __func__, authctxt->user);
390 mm_get_keystate(pmonitor);
394 monitor_set_child_handler(pid_t pid)
396 monitor_child_pid = pid;
400 monitor_child_handler(int sig)
402 kill(monitor_child_pid, sig);
406 monitor_child_postauth(struct monitor *pmonitor)
408 monitor_set_child_handler(pmonitor->m_pid);
409 signal(SIGHUP, &monitor_child_handler);
410 signal(SIGTERM, &monitor_child_handler);
413 mon_dispatch = mon_dispatch_postauth20;
415 /* Permit requests for moduli and signatures */
416 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
417 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
418 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
420 mon_dispatch = mon_dispatch_postauth15;
421 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
424 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
425 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
429 monitor_read(pmonitor, mon_dispatch, NULL);
433 monitor_sync(struct monitor *pmonitor)
435 if (options.compression) {
436 /* The member allocation is not visible, so sync it */
437 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
442 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
443 struct mon_table **pent)
451 mm_request_receive(pmonitor->m_sendfd, &m);
452 type = buffer_get_char(&m);
454 debug3("%s: checking request %d", __func__, type);
456 while (ent->f != NULL) {
457 if (ent->type == type)
462 if (ent->f != NULL) {
463 if (!(ent->flags & MON_PERMIT))
464 fatal("%s: unpermitted request %d", __func__,
466 ret = (*ent->f)(pmonitor->m_sendfd, &m);
469 /* The child may use this request only once, disable it */
470 if (ent->flags & MON_ONCE) {
471 debug2("%s: %d used once, disabling now", __func__,
473 ent->flags &= ~MON_PERMIT;
482 fatal("%s: unsupported request: %d", __func__, type);
488 /* allowed key state */
490 monitor_allowed_key(u_char *blob, u_int bloblen)
492 /* make sure key is allowed */
493 if (key_blob == NULL || key_bloblen != bloblen ||
494 memcmp(key_blob, blob, key_bloblen))
500 monitor_reset_key_state(void)
503 if (key_blob != NULL)
505 if (hostbased_cuser != NULL)
506 xfree(hostbased_cuser);
507 if (hostbased_chost != NULL)
508 xfree(hostbased_chost);
511 key_blobtype = MM_NOKEY;
512 hostbased_cuser = NULL;
513 hostbased_chost = NULL;
517 mm_answer_moduli(int sock, Buffer *m)
522 min = buffer_get_int(m);
523 want = buffer_get_int(m);
524 max = buffer_get_int(m);
526 debug3("%s: got parameters: %d %d %d",
527 __func__, min, want, max);
528 /* We need to check here, too, in case the child got corrupted */
529 if (max < min || want < min || max < want)
530 fatal("%s: bad parameters: %d %d %d",
531 __func__, min, want, max);
535 dh = choose_dh(min, want, max);
537 buffer_put_char(m, 0);
540 /* Send first bignum */
541 buffer_put_char(m, 1);
542 buffer_put_bignum2(m, dh->p);
543 buffer_put_bignum2(m, dh->g);
547 mm_request_send(sock, MONITOR_ANS_MODULI, m);
552 mm_answer_sign(int sock, Buffer *m)
557 u_int siglen, datlen;
560 debug3("%s", __func__);
562 keyid = buffer_get_int(m);
563 p = buffer_get_string(m, &datlen);
566 * Supported KEX types will only return SHA1 (20 byte) or
567 * SHA256 (32 byte) hashes
569 if (datlen != 20 && datlen != 32)
570 fatal("%s: data length incorrect: %u", __func__, datlen);
572 /* save session id, it will be passed on the first call */
573 if (session_id2_len == 0) {
574 session_id2_len = datlen;
575 session_id2 = xmalloc(session_id2_len);
576 memcpy(session_id2, p, session_id2_len);
579 if ((key = get_hostkey_by_index(keyid)) == NULL)
580 fatal("%s: no hostkey from index %d", __func__, keyid);
581 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
582 fatal("%s: key_sign failed", __func__);
584 debug3("%s: signature %p(%u)", __func__, signature, siglen);
587 buffer_put_string(m, signature, siglen);
592 mm_request_send(sock, MONITOR_ANS_SIGN, m);
594 /* Turn on permissions for getpwnam */
595 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
600 /* Retrieves the password entry and also checks if the user is permitted */
603 mm_answer_pwnamallow(int sock, Buffer *m)
606 struct passwd *pwent;
609 debug3("%s", __func__);
611 if (authctxt->attempt++ != 0)
612 fatal("%s: multiple attempts for getpwnam", __func__);
614 username = buffer_get_string(m, NULL);
616 pwent = getpwnamallow(username);
618 authctxt->user = xstrdup(username);
619 setproctitle("%s [priv]", pwent ? username : "unknown");
625 buffer_put_char(m, 0);
626 authctxt->pw = fakepw();
631 authctxt->pw = pwent;
634 buffer_put_char(m, 1);
635 buffer_put_string(m, pwent, sizeof(struct passwd));
636 buffer_put_cstring(m, pwent->pw_name);
637 buffer_put_cstring(m, "*");
638 buffer_put_cstring(m, pwent->pw_gecos);
639 #ifdef HAVE_PW_CLASS_IN_PASSWD
640 buffer_put_cstring(m, pwent->pw_class);
642 buffer_put_cstring(m, pwent->pw_dir);
643 buffer_put_cstring(m, pwent->pw_shell);
646 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
647 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
649 /* For SSHv1 allow authentication now */
651 monitor_permit_authentications(1);
653 /* Allow service/style information on the auth context */
654 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
655 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
660 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
662 #ifdef SSH_AUDIT_EVENTS
663 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
669 int mm_answer_auth2_read_banner(int sock, Buffer *m)
674 banner = auth2_read_banner();
675 buffer_put_cstring(m, banner != NULL ? banner : "");
676 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
685 mm_answer_authserv(int sock, Buffer *m)
687 monitor_permit_authentications(1);
689 authctxt->service = buffer_get_string(m, NULL);
690 authctxt->style = buffer_get_string(m, NULL);
691 debug3("%s: service=%s, style=%s",
692 __func__, authctxt->service, authctxt->style);
694 if (strlen(authctxt->style) == 0) {
695 xfree(authctxt->style);
696 authctxt->style = NULL;
703 mm_answer_authpassword(int sock, Buffer *m)
705 static int call_count;
710 passwd = buffer_get_string(m, &plen);
711 /* Only authenticate if the context is valid */
712 authenticated = options.password_authentication &&
713 auth_password(authctxt, passwd);
714 memset(passwd, 0, strlen(passwd));
718 buffer_put_int(m, authenticated);
720 debug3("%s: sending result %d", __func__, authenticated);
721 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
724 if (plen == 0 && call_count == 1)
725 auth_method = "none";
727 auth_method = "password";
729 /* Causes monitor loop to terminate if authenticated */
730 return (authenticated);
735 mm_answer_bsdauthquery(int sock, Buffer *m)
737 char *name, *infotxt;
743 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
744 &prompts, &echo_on) < 0 ? 0 : 1;
747 buffer_put_int(m, success);
749 buffer_put_cstring(m, prompts[0]);
751 debug3("%s: sending challenge success: %u", __func__, success);
752 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
765 mm_answer_bsdauthrespond(int sock, Buffer *m)
770 if (authctxt->as == 0)
771 fatal("%s: no bsd auth session", __func__);
773 response = buffer_get_string(m, NULL);
774 authok = options.challenge_response_authentication &&
775 auth_userresponse(authctxt->as, response, 0);
777 debug3("%s: <%s> = <%d>", __func__, response, authok);
781 buffer_put_int(m, authok);
783 debug3("%s: sending authenticated: %d", __func__, authok);
784 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
786 auth_method = "bsdauth";
788 return (authok != 0);
794 mm_answer_skeyquery(int sock, Buffer *m)
797 char challenge[1024];
800 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
801 sizeof(challenge)) < 0 ? 0 : 1;
804 buffer_put_int(m, success);
806 buffer_put_cstring(m, challenge);
808 debug3("%s: sending challenge success: %u", __func__, success);
809 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
815 mm_answer_skeyrespond(int sock, Buffer *m)
820 response = buffer_get_string(m, NULL);
822 authok = (options.challenge_response_authentication &&
824 skey_haskey(authctxt->pw->pw_name) == 0 &&
825 skey_passcheck(authctxt->pw->pw_name, response) != -1);
830 buffer_put_int(m, authok);
832 debug3("%s: sending authenticated: %d", __func__, authok);
833 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
835 auth_method = "skey";
837 return (authok != 0);
843 mm_answer_pam_start(int sock, Buffer *m)
845 if (!options.use_pam)
846 fatal("UsePAM not set, but ended up in %s anyway", __func__);
850 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
856 mm_answer_pam_account(int sock, Buffer *m)
860 if (!options.use_pam)
861 fatal("UsePAM not set, but ended up in %s anyway", __func__);
863 ret = do_pam_account();
865 buffer_put_int(m, ret);
866 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
868 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
873 static void *sshpam_ctxt, *sshpam_authok;
874 extern KbdintDevice sshpam_device;
877 mm_answer_pam_init_ctx(int sock, Buffer *m)
880 debug3("%s", __func__);
881 authctxt->user = buffer_get_string(m, NULL);
882 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
883 sshpam_authok = NULL;
885 if (sshpam_ctxt != NULL) {
886 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
887 buffer_put_int(m, 1);
889 buffer_put_int(m, 0);
891 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
896 mm_answer_pam_query(int sock, Buffer *m)
898 char *name, *info, **prompts;
899 u_int i, num, *echo_on;
902 debug3("%s", __func__);
903 sshpam_authok = NULL;
904 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
905 if (ret == 0 && num == 0)
906 sshpam_authok = sshpam_ctxt;
907 if (num > 1 || name == NULL || info == NULL)
910 buffer_put_int(m, ret);
911 buffer_put_cstring(m, name);
913 buffer_put_cstring(m, info);
915 buffer_put_int(m, num);
916 for (i = 0; i < num; ++i) {
917 buffer_put_cstring(m, prompts[i]);
919 buffer_put_int(m, echo_on[i]);
925 auth_method = "keyboard-interactive/pam";
926 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
931 mm_answer_pam_respond(int sock, Buffer *m)
937 debug3("%s", __func__);
938 sshpam_authok = NULL;
939 num = buffer_get_int(m);
941 resp = xcalloc(num, sizeof(char *));
942 for (i = 0; i < num; ++i)
943 resp[i] = buffer_get_string(m, NULL);
944 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
945 for (i = 0; i < num; ++i)
949 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
952 buffer_put_int(m, ret);
953 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
954 auth_method = "keyboard-interactive/pam";
956 sshpam_authok = sshpam_ctxt;
961 mm_answer_pam_free_ctx(int sock, Buffer *m)
964 debug3("%s", __func__);
965 (sshpam_device.free_ctx)(sshpam_ctxt);
967 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
968 auth_method = "keyboard-interactive/pam";
969 return (sshpam_authok == sshpam_ctxt);
974 mm_append_debug(Buffer *m)
976 if (auth_debug_init && buffer_len(&auth_debug)) {
977 debug3("%s: Appending debug messages for child", __func__);
978 buffer_append(m, buffer_ptr(&auth_debug),
979 buffer_len(&auth_debug));
980 buffer_clear(&auth_debug);
985 mm_answer_keyallowed(int sock, Buffer *m)
991 enum mm_keytype type = 0;
994 debug3("%s entering", __func__);
996 type = buffer_get_int(m);
997 cuser = buffer_get_string(m, NULL);
998 chost = buffer_get_string(m, NULL);
999 blob = buffer_get_string(m, &bloblen);
1001 key = key_from_blob(blob, bloblen);
1003 if ((compat20 && type == MM_RSAHOSTKEY) ||
1004 (!compat20 && type != MM_RSAHOSTKEY))
1005 fatal("%s: key type and protocol mismatch", __func__);
1007 debug3("%s: key_from_blob: %p", __func__, key);
1009 if (key != NULL && authctxt->valid) {
1012 allowed = options.pubkey_authentication &&
1013 user_key_allowed(authctxt->pw, key);
1014 auth_method = "publickey";
1017 allowed = options.hostbased_authentication &&
1018 hostbased_key_allowed(authctxt->pw,
1020 auth_method = "hostbased";
1023 key->type = KEY_RSA1; /* XXX */
1024 allowed = options.rhosts_rsa_authentication &&
1025 auth_rhosts_rsa_key_allowed(authctxt->pw,
1027 auth_method = "rsa";
1030 fatal("%s: unknown key type %d", __func__, type);
1037 /* clear temporarily storage (used by verify) */
1038 monitor_reset_key_state();
1041 /* Save temporarily for comparison in verify */
1043 key_bloblen = bloblen;
1044 key_blobtype = type;
1045 hostbased_cuser = cuser;
1046 hostbased_chost = chost;
1048 /* Log failed attempt */
1049 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1055 debug3("%s: key %p is %s",
1056 __func__, key, allowed ? "allowed" : "disallowed");
1059 buffer_put_int(m, allowed);
1060 buffer_put_int(m, forced_command != NULL);
1064 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1066 if (type == MM_RSAHOSTKEY)
1067 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1073 monitor_valid_userblob(u_char *data, u_int datalen)
1081 buffer_append(&b, data, datalen);
1083 if (datafellows & SSH_OLD_SESSIONID) {
1085 len = buffer_len(&b);
1086 if ((session_id2 == NULL) ||
1087 (len < session_id2_len) ||
1088 (memcmp(p, session_id2, session_id2_len) != 0))
1090 buffer_consume(&b, session_id2_len);
1092 p = buffer_get_string(&b, &len);
1093 if ((session_id2 == NULL) ||
1094 (len != session_id2_len) ||
1095 (memcmp(p, session_id2, session_id2_len) != 0))
1099 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1101 p = buffer_get_string(&b, NULL);
1102 if (strcmp(authctxt->user, p) != 0) {
1103 logit("wrong user name passed to monitor: expected %s != %.100s",
1108 buffer_skip_string(&b);
1109 if (datafellows & SSH_BUG_PKAUTH) {
1110 if (!buffer_get_char(&b))
1113 p = buffer_get_string(&b, NULL);
1114 if (strcmp("publickey", p) != 0)
1117 if (!buffer_get_char(&b))
1119 buffer_skip_string(&b);
1121 buffer_skip_string(&b);
1122 if (buffer_len(&b) != 0)
1129 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1138 buffer_append(&b, data, datalen);
1140 p = buffer_get_string(&b, &len);
1141 if ((session_id2 == NULL) ||
1142 (len != session_id2_len) ||
1143 (memcmp(p, session_id2, session_id2_len) != 0))
1147 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1149 p = buffer_get_string(&b, NULL);
1150 if (strcmp(authctxt->user, p) != 0) {
1151 logit("wrong user name passed to monitor: expected %s != %.100s",
1156 buffer_skip_string(&b); /* service */
1157 p = buffer_get_string(&b, NULL);
1158 if (strcmp(p, "hostbased") != 0)
1161 buffer_skip_string(&b); /* pkalg */
1162 buffer_skip_string(&b); /* pkblob */
1164 /* verify client host, strip trailing dot if necessary */
1165 p = buffer_get_string(&b, NULL);
1166 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1168 if (strcmp(p, chost) != 0)
1172 /* verify client user */
1173 p = buffer_get_string(&b, NULL);
1174 if (strcmp(p, cuser) != 0)
1178 if (buffer_len(&b) != 0)
1185 mm_answer_keyverify(int sock, Buffer *m)
1188 u_char *signature, *data, *blob;
1189 u_int signaturelen, datalen, bloblen;
1193 blob = buffer_get_string(m, &bloblen);
1194 signature = buffer_get_string(m, &signaturelen);
1195 data = buffer_get_string(m, &datalen);
1197 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1198 !monitor_allowed_key(blob, bloblen))
1199 fatal("%s: bad key, not previously allowed", __func__);
1201 key = key_from_blob(blob, bloblen);
1203 fatal("%s: bad public key blob", __func__);
1205 switch (key_blobtype) {
1207 valid_data = monitor_valid_userblob(data, datalen);
1210 valid_data = monitor_valid_hostbasedblob(data, datalen,
1211 hostbased_cuser, hostbased_chost);
1218 fatal("%s: bad signature data blob", __func__);
1220 verified = key_verify(key, signature, signaturelen, data, datalen);
1221 debug3("%s: key %p signature %s",
1222 __func__, key, verified ? "verified" : "unverified");
1229 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1231 monitor_reset_key_state();
1234 buffer_put_int(m, verified);
1235 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1241 mm_record_login(Session *s, struct passwd *pw)
1244 struct sockaddr_storage from;
1247 * Get IP address of client. If the connection is not a socket, let
1248 * the address be 0.0.0.0.
1250 memset(&from, 0, sizeof(from));
1251 fromlen = sizeof(from);
1252 if (packet_connection_is_on_socket()) {
1253 if (getpeername(packet_get_connection_in(),
1254 (struct sockaddr *)&from, &fromlen) < 0) {
1255 debug("getpeername: %.100s", strerror(errno));
1259 /* Record that there was a login on that tty from the remote host. */
1260 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1261 get_remote_name_or_ip(utmp_len, options.use_dns),
1262 (struct sockaddr *)&from, fromlen);
1266 mm_session_close(Session *s)
1268 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1269 if (s->ttyfd != -1) {
1270 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1271 session_pty_cleanup2(s);
1277 mm_answer_pty(int sock, Buffer *m)
1279 extern struct monitor *pmonitor;
1283 debug3("%s entering", __func__);
1289 s->authctxt = authctxt;
1290 s->pw = authctxt->pw;
1291 s->pid = pmonitor->m_pid;
1292 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1295 pty_setowner(authctxt->pw, s->tty);
1297 buffer_put_int(m, 1);
1298 buffer_put_cstring(m, s->tty);
1300 /* We need to trick ttyslot */
1301 if (dup2(s->ttyfd, 0) == -1)
1302 fatal("%s: dup2", __func__);
1304 mm_record_login(s, authctxt->pw);
1306 /* Now we can close the file descriptor again */
1309 /* send messages generated by record_login */
1310 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1311 buffer_clear(&loginmsg);
1313 mm_request_send(sock, MONITOR_ANS_PTY, m);
1315 mm_send_fd(sock, s->ptyfd);
1316 mm_send_fd(sock, s->ttyfd);
1318 /* make sure nothing uses fd 0 */
1319 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1320 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1322 error("%s: fd0 %d != 0", __func__, fd0);
1324 /* slave is not needed */
1326 s->ttyfd = s->ptyfd;
1327 /* no need to dup() because nobody closes ptyfd */
1328 s->ptymaster = s->ptyfd;
1330 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1336 mm_session_close(s);
1337 buffer_put_int(m, 0);
1338 mm_request_send(sock, MONITOR_ANS_PTY, m);
1343 mm_answer_pty_cleanup(int sock, Buffer *m)
1348 debug3("%s entering", __func__);
1350 tty = buffer_get_string(m, NULL);
1351 if ((s = session_by_tty(tty)) != NULL)
1352 mm_session_close(s);
1359 mm_answer_sesskey(int sock, Buffer *m)
1364 /* Turn off permissions */
1365 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1367 if ((p = BN_new()) == NULL)
1368 fatal("%s: BN_new", __func__);
1370 buffer_get_bignum2(m, p);
1372 rsafail = ssh1_session_key(p);
1375 buffer_put_int(m, rsafail);
1376 buffer_put_bignum2(m, p);
1380 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1382 /* Turn on permissions for sessid passing */
1383 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1389 mm_answer_sessid(int sock, Buffer *m)
1393 debug3("%s entering", __func__);
1395 if (buffer_len(m) != 16)
1396 fatal("%s: bad ssh1 session id", __func__);
1397 for (i = 0; i < 16; i++)
1398 session_id[i] = buffer_get_char(m);
1400 /* Turn on permissions for getpwnam */
1401 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1407 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1411 u_char *blob = NULL;
1415 debug3("%s entering", __func__);
1417 auth_method = "rsa";
1418 if (options.rsa_authentication && authctxt->valid) {
1419 if ((client_n = BN_new()) == NULL)
1420 fatal("%s: BN_new", __func__);
1421 buffer_get_bignum2(m, client_n);
1422 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1423 BN_clear_free(client_n);
1426 buffer_put_int(m, allowed);
1427 buffer_put_int(m, forced_command != NULL);
1429 /* clear temporarily storage (used by generate challenge) */
1430 monitor_reset_key_state();
1432 if (allowed && key != NULL) {
1433 key->type = KEY_RSA; /* cheat for key_to_blob */
1434 if (key_to_blob(key, &blob, &blen) == 0)
1435 fatal("%s: key_to_blob failed", __func__);
1436 buffer_put_string(m, blob, blen);
1438 /* Save temporarily for comparison in verify */
1441 key_blobtype = MM_RSAUSERKEY;
1448 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1450 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1451 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1456 mm_answer_rsa_challenge(int sock, Buffer *m)
1462 debug3("%s entering", __func__);
1464 if (!authctxt->valid)
1465 fatal("%s: authctxt not valid", __func__);
1466 blob = buffer_get_string(m, &blen);
1467 if (!monitor_allowed_key(blob, blen))
1468 fatal("%s: bad key, not previously allowed", __func__);
1469 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1470 fatal("%s: key type mismatch", __func__);
1471 if ((key = key_from_blob(blob, blen)) == NULL)
1472 fatal("%s: received bad key", __func__);
1475 BN_clear_free(ssh1_challenge);
1476 ssh1_challenge = auth_rsa_generate_challenge(key);
1479 buffer_put_bignum2(m, ssh1_challenge);
1481 debug3("%s sending reply", __func__);
1482 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1484 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1492 mm_answer_rsa_response(int sock, Buffer *m)
1495 u_char *blob, *response;
1499 debug3("%s entering", __func__);
1501 if (!authctxt->valid)
1502 fatal("%s: authctxt not valid", __func__);
1503 if (ssh1_challenge == NULL)
1504 fatal("%s: no ssh1_challenge", __func__);
1506 blob = buffer_get_string(m, &blen);
1507 if (!monitor_allowed_key(blob, blen))
1508 fatal("%s: bad key, not previously allowed", __func__);
1509 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1510 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1511 if ((key = key_from_blob(blob, blen)) == NULL)
1512 fatal("%s: received bad key", __func__);
1513 response = buffer_get_string(m, &len);
1515 fatal("%s: received bad response to challenge", __func__);
1516 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1522 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1525 BN_clear_free(ssh1_challenge);
1526 ssh1_challenge = NULL;
1527 monitor_reset_key_state();
1530 buffer_put_int(m, success);
1531 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1537 mm_answer_term(int sock, Buffer *req)
1539 extern struct monitor *pmonitor;
1542 debug3("%s: tearing down sessions", __func__);
1544 /* The child is terminating */
1545 session_destroy_all(&mm_session_close);
1547 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1551 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1553 /* Terminate process */
1557 #ifdef SSH_AUDIT_EVENTS
1558 /* Report that an audit event occurred */
1560 mm_answer_audit_event(int socket, Buffer *m)
1562 ssh_audit_event_t event;
1564 debug3("%s entering", __func__);
1566 event = buffer_get_int(m);
1568 case SSH_AUTH_FAIL_PUBKEY:
1569 case SSH_AUTH_FAIL_HOSTBASED:
1570 case SSH_AUTH_FAIL_GSSAPI:
1571 case SSH_LOGIN_EXCEED_MAXTRIES:
1572 case SSH_LOGIN_ROOT_DENIED:
1573 case SSH_CONNECTION_CLOSE:
1574 case SSH_INVALID_USER:
1578 fatal("Audit event type %d not permitted", event);
1585 mm_answer_audit_command(int socket, Buffer *m)
1590 debug3("%s entering", __func__);
1591 cmd = buffer_get_string(m, &len);
1592 /* sanity check command, if so how? */
1593 audit_run_command(cmd);
1597 #endif /* SSH_AUDIT_EVENTS */
1600 monitor_apply_keystate(struct monitor *pmonitor)
1603 set_newkeys(MODE_IN);
1604 set_newkeys(MODE_OUT);
1606 packet_set_protocol_flags(child_state.ssh1protoflags);
1607 packet_set_encryption_key(child_state.ssh1key,
1608 child_state.ssh1keylen, child_state.ssh1cipher);
1609 xfree(child_state.ssh1key);
1612 /* for rc4 and other stateful ciphers */
1613 packet_set_keycontext(MODE_OUT, child_state.keyout);
1614 xfree(child_state.keyout);
1615 packet_set_keycontext(MODE_IN, child_state.keyin);
1616 xfree(child_state.keyin);
1619 packet_set_iv(MODE_OUT, child_state.ivout);
1620 xfree(child_state.ivout);
1621 packet_set_iv(MODE_IN, child_state.ivin);
1622 xfree(child_state.ivin);
1625 memcpy(&incoming_stream, &child_state.incoming,
1626 sizeof(incoming_stream));
1627 memcpy(&outgoing_stream, &child_state.outgoing,
1628 sizeof(outgoing_stream));
1630 /* Update with new address */
1631 if (options.compression)
1632 mm_init_compression(pmonitor->m_zlib);
1634 /* Network I/O buffers */
1635 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1636 buffer_clear(&input);
1637 buffer_append(&input, child_state.input, child_state.ilen);
1638 memset(child_state.input, 0, child_state.ilen);
1639 xfree(child_state.input);
1641 buffer_clear(&output);
1642 buffer_append(&output, child_state.output, child_state.olen);
1643 memset(child_state.output, 0, child_state.olen);
1644 xfree(child_state.output);
1648 mm_get_kex(Buffer *m)
1654 kex = xcalloc(1, sizeof(*kex));
1655 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1656 if ((session_id2 == NULL) ||
1657 (kex->session_id_len != session_id2_len) ||
1658 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1659 fatal("mm_get_get: internal error: bad session id");
1660 kex->we_need = buffer_get_int(m);
1661 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1662 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1663 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1664 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1666 kex->hostkey_type = buffer_get_int(m);
1667 kex->kex_type = buffer_get_int(m);
1668 blob = buffer_get_string(m, &bloblen);
1669 buffer_init(&kex->my);
1670 buffer_append(&kex->my, blob, bloblen);
1672 blob = buffer_get_string(m, &bloblen);
1673 buffer_init(&kex->peer);
1674 buffer_append(&kex->peer, blob, bloblen);
1677 kex->flags = buffer_get_int(m);
1678 kex->client_version_string = buffer_get_string(m, NULL);
1679 kex->server_version_string = buffer_get_string(m, NULL);
1680 kex->load_host_key=&get_hostkey_by_type;
1681 kex->host_key_index=&get_hostkey_index;
1686 /* This function requries careful sanity checking */
1689 mm_get_keystate(struct monitor *pmonitor)
1693 u_int bloblen, plen;
1694 u_int32_t seqnr, packets;
1697 debug3("%s: Waiting for new keys", __func__);
1700 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1702 child_state.ssh1protoflags = buffer_get_int(&m);
1703 child_state.ssh1cipher = buffer_get_int(&m);
1704 child_state.ssh1key = buffer_get_string(&m,
1705 &child_state.ssh1keylen);
1706 child_state.ivout = buffer_get_string(&m,
1707 &child_state.ivoutlen);
1708 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1711 /* Get the Kex for rekeying */
1712 *pmonitor->m_pkex = mm_get_kex(&m);
1715 blob = buffer_get_string(&m, &bloblen);
1716 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1719 debug3("%s: Waiting for second key", __func__);
1720 blob = buffer_get_string(&m, &bloblen);
1721 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1724 /* Now get sequence numbers for the packets */
1725 seqnr = buffer_get_int(&m);
1726 blocks = buffer_get_int64(&m);
1727 packets = buffer_get_int(&m);
1728 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1729 seqnr = buffer_get_int(&m);
1730 blocks = buffer_get_int64(&m);
1731 packets = buffer_get_int(&m);
1732 packet_set_state(MODE_IN, seqnr, blocks, packets);
1735 /* Get the key context */
1736 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1737 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1739 debug3("%s: Getting compression state", __func__);
1740 /* Get compression state */
1741 p = buffer_get_string(&m, &plen);
1742 if (plen != sizeof(child_state.outgoing))
1743 fatal("%s: bad request size", __func__);
1744 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1747 p = buffer_get_string(&m, &plen);
1748 if (plen != sizeof(child_state.incoming))
1749 fatal("%s: bad request size", __func__);
1750 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1753 /* Network I/O buffers */
1754 debug3("%s: Getting Network I/O buffers", __func__);
1755 child_state.input = buffer_get_string(&m, &child_state.ilen);
1756 child_state.output = buffer_get_string(&m, &child_state.olen);
1762 /* Allocation functions for zlib */
1764 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1766 size_t len = (size_t) size * ncount;
1769 if (len == 0 || ncount > SIZE_T_MAX / size)
1770 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1772 address = mm_malloc(mm, len);
1778 mm_zfree(struct mm_master *mm, void *address)
1780 mm_free(mm, address);
1784 mm_init_compression(struct mm_master *mm)
1786 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1787 outgoing_stream.zfree = (free_func)mm_zfree;
1788 outgoing_stream.opaque = mm;
1790 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1791 incoming_stream.zfree = (free_func)mm_zfree;
1792 incoming_stream.opaque = mm;
1797 #define FD_CLOSEONEXEC(x) do { \
1798 if (fcntl(x, F_SETFD, 1) == -1) \
1799 fatal("fcntl(%d, F_SETFD)", x); \
1803 monitor_socketpair(int *pair)
1805 #ifdef HAVE_SOCKETPAIR
1806 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1807 fatal("%s: socketpair", __func__);
1809 fatal("%s: UsePrivilegeSeparation=yes not supported",
1812 FD_CLOSEONEXEC(pair[0]);
1813 FD_CLOSEONEXEC(pair[1]);
1816 #define MM_MEMSIZE 65536
1821 struct monitor *mon;
1824 mon = xcalloc(1, sizeof(*mon));
1826 monitor_socketpair(pair);
1828 mon->m_recvfd = pair[0];
1829 mon->m_sendfd = pair[1];
1831 /* Used to share zlib space across processes */
1832 if (options.compression) {
1833 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1834 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1836 /* Compression needs to share state across borders */
1837 mm_init_compression(mon->m_zlib);
1844 monitor_reinit(struct monitor *mon)
1848 monitor_socketpair(pair);
1850 mon->m_recvfd = pair[0];
1851 mon->m_sendfd = pair[1];
1856 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1862 goid.elements = buffer_get_string(m, &len);
1865 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1867 xfree(goid.elements);
1870 buffer_put_int(m, major);
1872 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1874 /* Now we have a context, enable the step */
1875 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1881 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1884 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1885 OM_uint32 major, minor;
1886 OM_uint32 flags = 0; /* GSI needs this */
1889 in.value = buffer_get_string(m, &len);
1891 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1895 buffer_put_int(m, major);
1896 buffer_put_string(m, out.value, out.length);
1897 buffer_put_int(m, flags);
1898 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1900 gss_release_buffer(&minor, &out);
1902 if (major == GSS_S_COMPLETE) {
1903 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1904 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1905 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1911 mm_answer_gss_checkmic(int sock, Buffer *m)
1913 gss_buffer_desc gssbuf, mic;
1917 gssbuf.value = buffer_get_string(m, &len);
1918 gssbuf.length = len;
1919 mic.value = buffer_get_string(m, &len);
1922 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1924 xfree(gssbuf.value);
1928 buffer_put_int(m, ret);
1930 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1932 if (!GSS_ERROR(ret))
1933 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1939 mm_answer_gss_userok(int sock, Buffer *m)
1943 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1946 buffer_put_int(m, authenticated);
1948 debug3("%s: sending result %d", __func__, authenticated);
1949 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1951 auth_method = "gssapi-with-mic";
1953 /* Monitor loop will terminate if authenticated */
1954 return (authenticated);