1 /* $OpenBSD: monitor.c,v 1.90 2007/02/19 10:45:58 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 #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 *);
168 #ifdef SSH_AUDIT_EVENTS
169 int mm_answer_audit_event(int, Buffer *);
170 int mm_answer_audit_command(int, Buffer *);
173 static Authctxt *authctxt;
174 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
176 /* local state for key verify */
177 static u_char *key_blob = NULL;
178 static u_int key_bloblen = 0;
179 static int key_blobtype = MM_NOKEY;
180 static char *hostbased_cuser = NULL;
181 static char *hostbased_chost = NULL;
182 static char *auth_method = "unknown";
183 static u_int session_id2_len = 0;
184 static u_char *session_id2 = NULL;
185 static pid_t monitor_child_pid;
188 enum monitor_reqtype type;
190 int (*f)(int, Buffer *);
193 #define MON_ISAUTH 0x0004 /* Required for Authentication */
194 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
195 #define MON_ONCE 0x0010 /* Disable after calling */
196 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
198 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
200 #define MON_PERMIT 0x1000 /* Request is permitted */
202 struct mon_table mon_dispatch_proto20[] = {
203 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
204 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
205 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
206 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
207 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
208 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
210 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
211 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
212 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
213 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
214 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
215 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
217 #ifdef SSH_AUDIT_EVENTS
218 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
221 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
222 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
225 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
226 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
228 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
229 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
231 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
232 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
233 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
234 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
239 struct mon_table mon_dispatch_postauth20[] = {
240 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
241 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
242 {MONITOR_REQ_PTY, 0, mm_answer_pty},
243 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
244 {MONITOR_REQ_TERM, 0, mm_answer_term},
245 #ifdef SSH_AUDIT_EVENTS
246 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
247 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
252 struct mon_table mon_dispatch_proto15[] = {
253 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
254 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
255 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
256 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
257 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
258 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
259 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
260 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
262 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
263 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
266 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
267 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
270 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
271 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
272 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
273 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
274 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
275 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
277 #ifdef SSH_AUDIT_EVENTS
278 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
283 struct mon_table mon_dispatch_postauth15[] = {
284 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
285 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
286 {MONITOR_REQ_TERM, 0, mm_answer_term},
287 #ifdef SSH_AUDIT_EVENTS
288 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
289 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
294 struct mon_table *mon_dispatch;
296 /* Specifies if a certain message is allowed at the moment */
299 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
301 while (ent->f != NULL) {
302 if (ent->type == type) {
303 ent->flags &= ~MON_PERMIT;
304 ent->flags |= permit ? MON_PERMIT : 0;
312 monitor_permit_authentications(int permit)
314 struct mon_table *ent = mon_dispatch;
316 while (ent->f != NULL) {
317 if (ent->flags & MON_AUTH) {
318 ent->flags &= ~MON_PERMIT;
319 ent->flags |= permit ? MON_PERMIT : 0;
326 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
328 struct mon_table *ent;
329 int authenticated = 0;
331 debug3("preauth child monitor started");
333 authctxt = _authctxt;
334 memset(authctxt, 0, sizeof(*authctxt));
336 authctxt->loginmsg = &loginmsg;
339 mon_dispatch = mon_dispatch_proto20;
341 /* Permit requests for moduli and signatures */
342 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
343 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
345 mon_dispatch = mon_dispatch_proto15;
347 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
350 /* The first few requests do not require asynchronous access */
351 while (!authenticated) {
352 auth_method = "unknown";
353 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
355 if (!(ent->flags & MON_AUTHDECIDE))
356 fatal("%s: unexpected authentication from %d",
357 __func__, ent->type);
358 if (authctxt->pw->pw_uid == 0 &&
359 !auth_root_allowed(auth_method))
362 /* PAM needs to perform account checks after auth */
363 if (options.use_pam && authenticated) {
367 mm_request_receive_expect(pmonitor->m_sendfd,
368 MONITOR_REQ_PAM_ACCOUNT, &m);
369 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
375 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
376 auth_log(authctxt, authenticated, auth_method,
377 compat20 ? " ssh2" : "");
379 authctxt->failures++;
383 if (!authctxt->valid)
384 fatal("%s: authenticated invalid user", __func__);
385 if (strcmp(auth_method, "unknown") == 0)
386 fatal("%s: authentication method name unknown", __func__);
388 debug("%s: %s has been authenticated by privileged process",
389 __func__, authctxt->user);
391 mm_get_keystate(pmonitor);
395 monitor_set_child_handler(pid_t pid)
397 monitor_child_pid = pid;
401 monitor_child_handler(int sig)
403 kill(monitor_child_pid, sig);
407 monitor_child_postauth(struct monitor *pmonitor)
409 monitor_set_child_handler(pmonitor->m_pid);
410 signal(SIGHUP, &monitor_child_handler);
411 signal(SIGTERM, &monitor_child_handler);
414 mon_dispatch = mon_dispatch_postauth20;
416 /* Permit requests for moduli and signatures */
417 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
418 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
419 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
421 mon_dispatch = mon_dispatch_postauth15;
422 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
425 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
426 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
430 monitor_read(pmonitor, mon_dispatch, NULL);
434 monitor_sync(struct monitor *pmonitor)
436 if (options.compression) {
437 /* The member allocation is not visible, so sync it */
438 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
443 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
444 struct mon_table **pent)
452 mm_request_receive(pmonitor->m_sendfd, &m);
453 type = buffer_get_char(&m);
455 debug3("%s: checking request %d", __func__, type);
457 while (ent->f != NULL) {
458 if (ent->type == type)
463 if (ent->f != NULL) {
464 if (!(ent->flags & MON_PERMIT))
465 fatal("%s: unpermitted request %d", __func__,
467 ret = (*ent->f)(pmonitor->m_sendfd, &m);
470 /* The child may use this request only once, disable it */
471 if (ent->flags & MON_ONCE) {
472 debug2("%s: %d used once, disabling now", __func__,
474 ent->flags &= ~MON_PERMIT;
483 fatal("%s: unsupported request: %d", __func__, type);
489 /* allowed key state */
491 monitor_allowed_key(u_char *blob, u_int bloblen)
493 /* make sure key is allowed */
494 if (key_blob == NULL || key_bloblen != bloblen ||
495 memcmp(key_blob, blob, key_bloblen))
501 monitor_reset_key_state(void)
504 if (key_blob != NULL)
506 if (hostbased_cuser != NULL)
507 xfree(hostbased_cuser);
508 if (hostbased_chost != NULL)
509 xfree(hostbased_chost);
512 key_blobtype = MM_NOKEY;
513 hostbased_cuser = NULL;
514 hostbased_chost = NULL;
518 mm_answer_moduli(int sock, Buffer *m)
523 min = buffer_get_int(m);
524 want = buffer_get_int(m);
525 max = buffer_get_int(m);
527 debug3("%s: got parameters: %d %d %d",
528 __func__, min, want, max);
529 /* We need to check here, too, in case the child got corrupted */
530 if (max < min || want < min || max < want)
531 fatal("%s: bad parameters: %d %d %d",
532 __func__, min, want, max);
536 dh = choose_dh(min, want, max);
538 buffer_put_char(m, 0);
541 /* Send first bignum */
542 buffer_put_char(m, 1);
543 buffer_put_bignum2(m, dh->p);
544 buffer_put_bignum2(m, dh->g);
548 mm_request_send(sock, MONITOR_ANS_MODULI, m);
553 mm_answer_sign(int sock, Buffer *m)
558 u_int siglen, datlen;
561 debug3("%s", __func__);
563 keyid = buffer_get_int(m);
564 p = buffer_get_string(m, &datlen);
567 * Supported KEX types will only return SHA1 (20 byte) or
568 * SHA256 (32 byte) hashes
570 if (datlen != 20 && datlen != 32)
571 fatal("%s: data length incorrect: %u", __func__, datlen);
573 /* save session id, it will be passed on the first call */
574 if (session_id2_len == 0) {
575 session_id2_len = datlen;
576 session_id2 = xmalloc(session_id2_len);
577 memcpy(session_id2, p, session_id2_len);
580 if ((key = get_hostkey_by_index(keyid)) == NULL)
581 fatal("%s: no hostkey from index %d", __func__, keyid);
582 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
583 fatal("%s: key_sign failed", __func__);
585 debug3("%s: signature %p(%u)", __func__, signature, siglen);
588 buffer_put_string(m, signature, siglen);
593 mm_request_send(sock, MONITOR_ANS_SIGN, m);
595 /* Turn on permissions for getpwnam */
596 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
601 /* Retrieves the password entry and also checks if the user is permitted */
604 mm_answer_pwnamallow(int sock, Buffer *m)
607 struct passwd *pwent;
610 debug3("%s", __func__);
612 if (authctxt->attempt++ != 0)
613 fatal("%s: multiple attempts for getpwnam", __func__);
615 username = buffer_get_string(m, NULL);
617 pwent = getpwnamallow(username);
619 authctxt->user = xstrdup(username);
620 setproctitle("%s [priv]", pwent ? username : "unknown");
626 buffer_put_char(m, 0);
627 authctxt->pw = fakepw();
632 authctxt->pw = pwent;
635 buffer_put_char(m, 1);
636 buffer_put_string(m, pwent, sizeof(struct passwd));
637 buffer_put_cstring(m, pwent->pw_name);
638 buffer_put_cstring(m, "*");
639 buffer_put_cstring(m, pwent->pw_gecos);
640 #ifdef HAVE_PW_CLASS_IN_PASSWD
641 buffer_put_cstring(m, pwent->pw_class);
643 buffer_put_cstring(m, pwent->pw_dir);
644 buffer_put_cstring(m, pwent->pw_shell);
645 buffer_put_string(m, &options, sizeof(options));
646 if (options.banner != NULL)
647 buffer_put_cstring(m, options.banner);
650 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
651 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
653 /* For SSHv1 allow authentication now */
655 monitor_permit_authentications(1);
657 /* Allow service/style information on the auth context */
658 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
659 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
664 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
670 int mm_answer_auth2_read_banner(int sock, Buffer *m)
675 banner = auth2_read_banner();
676 buffer_put_cstring(m, banner != NULL ? banner : "");
677 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
686 mm_answer_authserv(int sock, Buffer *m)
688 monitor_permit_authentications(1);
690 authctxt->service = buffer_get_string(m, NULL);
691 authctxt->style = buffer_get_string(m, NULL);
692 debug3("%s: service=%s, style=%s",
693 __func__, authctxt->service, authctxt->style);
695 if (strlen(authctxt->style) == 0) {
696 xfree(authctxt->style);
697 authctxt->style = NULL;
704 mm_answer_authpassword(int sock, Buffer *m)
706 static int call_count;
711 passwd = buffer_get_string(m, &plen);
712 /* Only authenticate if the context is valid */
713 authenticated = options.password_authentication &&
714 auth_password(authctxt, passwd);
715 memset(passwd, 0, strlen(passwd));
719 buffer_put_int(m, authenticated);
721 debug3("%s: sending result %d", __func__, authenticated);
722 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
725 if (plen == 0 && call_count == 1)
726 auth_method = "none";
728 auth_method = "password";
730 /* Causes monitor loop to terminate if authenticated */
731 return (authenticated);
736 mm_answer_bsdauthquery(int sock, Buffer *m)
738 char *name, *infotxt;
744 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
745 &prompts, &echo_on) < 0 ? 0 : 1;
748 buffer_put_int(m, success);
750 buffer_put_cstring(m, prompts[0]);
752 debug3("%s: sending challenge success: %u", __func__, success);
753 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
766 mm_answer_bsdauthrespond(int sock, Buffer *m)
771 if (authctxt->as == 0)
772 fatal("%s: no bsd auth session", __func__);
774 response = buffer_get_string(m, NULL);
775 authok = options.challenge_response_authentication &&
776 auth_userresponse(authctxt->as, response, 0);
778 debug3("%s: <%s> = <%d>", __func__, response, authok);
782 buffer_put_int(m, authok);
784 debug3("%s: sending authenticated: %d", __func__, authok);
785 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
787 auth_method = "bsdauth";
789 return (authok != 0);
795 mm_answer_skeyquery(int sock, Buffer *m)
798 char challenge[1024];
801 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
802 sizeof(challenge)) < 0 ? 0 : 1;
805 buffer_put_int(m, success);
807 buffer_put_cstring(m, challenge);
809 debug3("%s: sending challenge success: %u", __func__, success);
810 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
816 mm_answer_skeyrespond(int sock, Buffer *m)
821 response = buffer_get_string(m, NULL);
823 authok = (options.challenge_response_authentication &&
825 skey_haskey(authctxt->pw->pw_name) == 0 &&
826 skey_passcheck(authctxt->pw->pw_name, response) != -1);
831 buffer_put_int(m, authok);
833 debug3("%s: sending authenticated: %d", __func__, authok);
834 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
836 auth_method = "skey";
838 return (authok != 0);
844 mm_answer_pam_start(int sock, Buffer *m)
846 if (!options.use_pam)
847 fatal("UsePAM not set, but ended up in %s anyway", __func__);
851 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
857 mm_answer_pam_account(int sock, Buffer *m)
861 if (!options.use_pam)
862 fatal("UsePAM not set, but ended up in %s anyway", __func__);
864 ret = do_pam_account();
866 buffer_put_int(m, ret);
867 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
869 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
874 static void *sshpam_ctxt, *sshpam_authok;
875 extern KbdintDevice sshpam_device;
878 mm_answer_pam_init_ctx(int sock, Buffer *m)
881 debug3("%s", __func__);
882 authctxt->user = buffer_get_string(m, NULL);
883 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
884 sshpam_authok = NULL;
886 if (sshpam_ctxt != NULL) {
887 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
888 buffer_put_int(m, 1);
890 buffer_put_int(m, 0);
892 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
897 mm_answer_pam_query(int sock, Buffer *m)
899 char *name, *info, **prompts;
900 u_int i, num, *echo_on;
903 debug3("%s", __func__);
904 sshpam_authok = NULL;
905 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
906 if (ret == 0 && num == 0)
907 sshpam_authok = sshpam_ctxt;
908 if (num > 1 || name == NULL || info == NULL)
911 buffer_put_int(m, ret);
912 buffer_put_cstring(m, name);
914 buffer_put_cstring(m, info);
916 buffer_put_int(m, num);
917 for (i = 0; i < num; ++i) {
918 buffer_put_cstring(m, prompts[i]);
920 buffer_put_int(m, echo_on[i]);
926 auth_method = "keyboard-interactive/pam";
927 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
932 mm_answer_pam_respond(int sock, Buffer *m)
938 debug3("%s", __func__);
939 sshpam_authok = NULL;
940 num = buffer_get_int(m);
942 resp = xcalloc(num, sizeof(char *));
943 for (i = 0; i < num; ++i)
944 resp[i] = buffer_get_string(m, NULL);
945 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
946 for (i = 0; i < num; ++i)
950 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
953 buffer_put_int(m, ret);
954 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
955 auth_method = "keyboard-interactive/pam";
957 sshpam_authok = sshpam_ctxt;
962 mm_answer_pam_free_ctx(int sock, Buffer *m)
965 debug3("%s", __func__);
966 (sshpam_device.free_ctx)(sshpam_ctxt);
968 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
969 auth_method = "keyboard-interactive/pam";
970 return (sshpam_authok == sshpam_ctxt);
975 mm_append_debug(Buffer *m)
977 if (auth_debug_init && buffer_len(&auth_debug)) {
978 debug3("%s: Appending debug messages for child", __func__);
979 buffer_append(m, buffer_ptr(&auth_debug),
980 buffer_len(&auth_debug));
981 buffer_clear(&auth_debug);
986 mm_answer_keyallowed(int sock, Buffer *m)
992 enum mm_keytype type = 0;
995 debug3("%s entering", __func__);
997 type = buffer_get_int(m);
998 cuser = buffer_get_string(m, NULL);
999 chost = buffer_get_string(m, NULL);
1000 blob = buffer_get_string(m, &bloblen);
1002 key = key_from_blob(blob, bloblen);
1004 if ((compat20 && type == MM_RSAHOSTKEY) ||
1005 (!compat20 && type != MM_RSAHOSTKEY))
1006 fatal("%s: key type and protocol mismatch", __func__);
1008 debug3("%s: key_from_blob: %p", __func__, key);
1010 if (key != NULL && authctxt->valid) {
1013 allowed = options.pubkey_authentication &&
1014 user_key_allowed(authctxt->pw, key);
1015 auth_method = "publickey";
1018 allowed = options.hostbased_authentication &&
1019 hostbased_key_allowed(authctxt->pw,
1021 auth_method = "hostbased";
1024 key->type = KEY_RSA1; /* XXX */
1025 allowed = options.rhosts_rsa_authentication &&
1026 auth_rhosts_rsa_key_allowed(authctxt->pw,
1028 auth_method = "rsa";
1031 fatal("%s: unknown key type %d", __func__, type);
1038 /* clear temporarily storage (used by verify) */
1039 monitor_reset_key_state();
1042 /* Save temporarily for comparison in verify */
1044 key_bloblen = bloblen;
1045 key_blobtype = type;
1046 hostbased_cuser = cuser;
1047 hostbased_chost = chost;
1049 /* Log failed attempt */
1050 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1056 debug3("%s: key %p is %s",
1057 __func__, key, allowed ? "allowed" : "disallowed");
1060 buffer_put_int(m, allowed);
1061 buffer_put_int(m, forced_command != NULL);
1065 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1067 if (type == MM_RSAHOSTKEY)
1068 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1074 monitor_valid_userblob(u_char *data, u_int datalen)
1082 buffer_append(&b, data, datalen);
1084 if (datafellows & SSH_OLD_SESSIONID) {
1086 len = buffer_len(&b);
1087 if ((session_id2 == NULL) ||
1088 (len < session_id2_len) ||
1089 (memcmp(p, session_id2, session_id2_len) != 0))
1091 buffer_consume(&b, session_id2_len);
1093 p = buffer_get_string(&b, &len);
1094 if ((session_id2 == NULL) ||
1095 (len != session_id2_len) ||
1096 (memcmp(p, session_id2, session_id2_len) != 0))
1100 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1102 p = buffer_get_string(&b, NULL);
1103 if (strcmp(authctxt->user, p) != 0) {
1104 logit("wrong user name passed to monitor: expected %s != %.100s",
1109 buffer_skip_string(&b);
1110 if (datafellows & SSH_BUG_PKAUTH) {
1111 if (!buffer_get_char(&b))
1114 p = buffer_get_string(&b, NULL);
1115 if (strcmp("publickey", p) != 0)
1118 if (!buffer_get_char(&b))
1120 buffer_skip_string(&b);
1122 buffer_skip_string(&b);
1123 if (buffer_len(&b) != 0)
1130 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1139 buffer_append(&b, data, datalen);
1141 p = buffer_get_string(&b, &len);
1142 if ((session_id2 == NULL) ||
1143 (len != session_id2_len) ||
1144 (memcmp(p, session_id2, session_id2_len) != 0))
1148 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1150 p = buffer_get_string(&b, NULL);
1151 if (strcmp(authctxt->user, p) != 0) {
1152 logit("wrong user name passed to monitor: expected %s != %.100s",
1157 buffer_skip_string(&b); /* service */
1158 p = buffer_get_string(&b, NULL);
1159 if (strcmp(p, "hostbased") != 0)
1162 buffer_skip_string(&b); /* pkalg */
1163 buffer_skip_string(&b); /* pkblob */
1165 /* verify client host, strip trailing dot if necessary */
1166 p = buffer_get_string(&b, NULL);
1167 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1169 if (strcmp(p, chost) != 0)
1173 /* verify client user */
1174 p = buffer_get_string(&b, NULL);
1175 if (strcmp(p, cuser) != 0)
1179 if (buffer_len(&b) != 0)
1186 mm_answer_keyverify(int sock, Buffer *m)
1189 u_char *signature, *data, *blob;
1190 u_int signaturelen, datalen, bloblen;
1194 blob = buffer_get_string(m, &bloblen);
1195 signature = buffer_get_string(m, &signaturelen);
1196 data = buffer_get_string(m, &datalen);
1198 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1199 !monitor_allowed_key(blob, bloblen))
1200 fatal("%s: bad key, not previously allowed", __func__);
1202 key = key_from_blob(blob, bloblen);
1204 fatal("%s: bad public key blob", __func__);
1206 switch (key_blobtype) {
1208 valid_data = monitor_valid_userblob(data, datalen);
1211 valid_data = monitor_valid_hostbasedblob(data, datalen,
1212 hostbased_cuser, hostbased_chost);
1219 fatal("%s: bad signature data blob", __func__);
1221 verified = key_verify(key, signature, signaturelen, data, datalen);
1222 debug3("%s: key %p signature %s",
1223 __func__, key, (verified == 1) ? "verified" : "unverified");
1230 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1232 monitor_reset_key_state();
1235 buffer_put_int(m, verified);
1236 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1238 return (verified == 1);
1242 mm_record_login(Session *s, struct passwd *pw)
1245 struct sockaddr_storage from;
1248 * Get IP address of client. If the connection is not a socket, let
1249 * the address be 0.0.0.0.
1251 memset(&from, 0, sizeof(from));
1252 fromlen = sizeof(from);
1253 if (packet_connection_is_on_socket()) {
1254 if (getpeername(packet_get_connection_in(),
1255 (struct sockaddr *)&from, &fromlen) < 0) {
1256 debug("getpeername: %.100s", strerror(errno));
1260 /* Record that there was a login on that tty from the remote host. */
1261 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1262 get_remote_name_or_ip(utmp_len, options.use_dns),
1263 (struct sockaddr *)&from, fromlen);
1267 mm_session_close(Session *s)
1269 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1270 if (s->ttyfd != -1) {
1271 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1272 session_pty_cleanup2(s);
1278 mm_answer_pty(int sock, Buffer *m)
1280 extern struct monitor *pmonitor;
1284 debug3("%s entering", __func__);
1290 s->authctxt = authctxt;
1291 s->pw = authctxt->pw;
1292 s->pid = pmonitor->m_pid;
1293 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1296 pty_setowner(authctxt->pw, s->tty);
1298 buffer_put_int(m, 1);
1299 buffer_put_cstring(m, s->tty);
1301 /* We need to trick ttyslot */
1302 if (dup2(s->ttyfd, 0) == -1)
1303 fatal("%s: dup2", __func__);
1305 mm_record_login(s, authctxt->pw);
1307 /* Now we can close the file descriptor again */
1310 /* send messages generated by record_login */
1311 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1312 buffer_clear(&loginmsg);
1314 mm_request_send(sock, MONITOR_ANS_PTY, m);
1316 mm_send_fd(sock, s->ptyfd);
1317 mm_send_fd(sock, s->ttyfd);
1319 /* make sure nothing uses fd 0 */
1320 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1321 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1323 error("%s: fd0 %d != 0", __func__, fd0);
1325 /* slave is not needed */
1327 s->ttyfd = s->ptyfd;
1328 /* no need to dup() because nobody closes ptyfd */
1329 s->ptymaster = s->ptyfd;
1331 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1337 mm_session_close(s);
1338 buffer_put_int(m, 0);
1339 mm_request_send(sock, MONITOR_ANS_PTY, m);
1344 mm_answer_pty_cleanup(int sock, Buffer *m)
1349 debug3("%s entering", __func__);
1351 tty = buffer_get_string(m, NULL);
1352 if ((s = session_by_tty(tty)) != NULL)
1353 mm_session_close(s);
1360 mm_answer_sesskey(int sock, Buffer *m)
1365 /* Turn off permissions */
1366 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1368 if ((p = BN_new()) == NULL)
1369 fatal("%s: BN_new", __func__);
1371 buffer_get_bignum2(m, p);
1373 rsafail = ssh1_session_key(p);
1376 buffer_put_int(m, rsafail);
1377 buffer_put_bignum2(m, p);
1381 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1383 /* Turn on permissions for sessid passing */
1384 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1390 mm_answer_sessid(int sock, Buffer *m)
1394 debug3("%s entering", __func__);
1396 if (buffer_len(m) != 16)
1397 fatal("%s: bad ssh1 session id", __func__);
1398 for (i = 0; i < 16; i++)
1399 session_id[i] = buffer_get_char(m);
1401 /* Turn on permissions for getpwnam */
1402 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1408 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1412 u_char *blob = NULL;
1416 debug3("%s entering", __func__);
1418 auth_method = "rsa";
1419 if (options.rsa_authentication && authctxt->valid) {
1420 if ((client_n = BN_new()) == NULL)
1421 fatal("%s: BN_new", __func__);
1422 buffer_get_bignum2(m, client_n);
1423 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1424 BN_clear_free(client_n);
1427 buffer_put_int(m, allowed);
1428 buffer_put_int(m, forced_command != NULL);
1430 /* clear temporarily storage (used by generate challenge) */
1431 monitor_reset_key_state();
1433 if (allowed && key != NULL) {
1434 key->type = KEY_RSA; /* cheat for key_to_blob */
1435 if (key_to_blob(key, &blob, &blen) == 0)
1436 fatal("%s: key_to_blob failed", __func__);
1437 buffer_put_string(m, blob, blen);
1439 /* Save temporarily for comparison in verify */
1442 key_blobtype = MM_RSAUSERKEY;
1449 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1451 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1452 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1457 mm_answer_rsa_challenge(int sock, Buffer *m)
1463 debug3("%s entering", __func__);
1465 if (!authctxt->valid)
1466 fatal("%s: authctxt not valid", __func__);
1467 blob = buffer_get_string(m, &blen);
1468 if (!monitor_allowed_key(blob, blen))
1469 fatal("%s: bad key, not previously allowed", __func__);
1470 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1471 fatal("%s: key type mismatch", __func__);
1472 if ((key = key_from_blob(blob, blen)) == NULL)
1473 fatal("%s: received bad key", __func__);
1476 BN_clear_free(ssh1_challenge);
1477 ssh1_challenge = auth_rsa_generate_challenge(key);
1480 buffer_put_bignum2(m, ssh1_challenge);
1482 debug3("%s sending reply", __func__);
1483 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1485 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1493 mm_answer_rsa_response(int sock, Buffer *m)
1496 u_char *blob, *response;
1500 debug3("%s entering", __func__);
1502 if (!authctxt->valid)
1503 fatal("%s: authctxt not valid", __func__);
1504 if (ssh1_challenge == NULL)
1505 fatal("%s: no ssh1_challenge", __func__);
1507 blob = buffer_get_string(m, &blen);
1508 if (!monitor_allowed_key(blob, blen))
1509 fatal("%s: bad key, not previously allowed", __func__);
1510 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1511 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1512 if ((key = key_from_blob(blob, blen)) == NULL)
1513 fatal("%s: received bad key", __func__);
1514 response = buffer_get_string(m, &len);
1516 fatal("%s: received bad response to challenge", __func__);
1517 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1523 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1526 BN_clear_free(ssh1_challenge);
1527 ssh1_challenge = NULL;
1528 monitor_reset_key_state();
1531 buffer_put_int(m, success);
1532 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1538 mm_answer_term(int sock, Buffer *req)
1540 extern struct monitor *pmonitor;
1543 debug3("%s: tearing down sessions", __func__);
1545 /* The child is terminating */
1546 session_destroy_all(&mm_session_close);
1548 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1552 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1554 /* Terminate process */
1558 #ifdef SSH_AUDIT_EVENTS
1559 /* Report that an audit event occurred */
1561 mm_answer_audit_event(int socket, Buffer *m)
1563 ssh_audit_event_t event;
1565 debug3("%s entering", __func__);
1567 event = buffer_get_int(m);
1569 case SSH_AUTH_FAIL_PUBKEY:
1570 case SSH_AUTH_FAIL_HOSTBASED:
1571 case SSH_AUTH_FAIL_GSSAPI:
1572 case SSH_LOGIN_EXCEED_MAXTRIES:
1573 case SSH_LOGIN_ROOT_DENIED:
1574 case SSH_CONNECTION_CLOSE:
1575 case SSH_INVALID_USER:
1579 fatal("Audit event type %d not permitted", event);
1586 mm_answer_audit_command(int socket, Buffer *m)
1591 debug3("%s entering", __func__);
1592 cmd = buffer_get_string(m, &len);
1593 /* sanity check command, if so how? */
1594 audit_run_command(cmd);
1598 #endif /* SSH_AUDIT_EVENTS */
1601 monitor_apply_keystate(struct monitor *pmonitor)
1604 set_newkeys(MODE_IN);
1605 set_newkeys(MODE_OUT);
1607 packet_set_protocol_flags(child_state.ssh1protoflags);
1608 packet_set_encryption_key(child_state.ssh1key,
1609 child_state.ssh1keylen, child_state.ssh1cipher);
1610 xfree(child_state.ssh1key);
1613 /* for rc4 and other stateful ciphers */
1614 packet_set_keycontext(MODE_OUT, child_state.keyout);
1615 xfree(child_state.keyout);
1616 packet_set_keycontext(MODE_IN, child_state.keyin);
1617 xfree(child_state.keyin);
1620 packet_set_iv(MODE_OUT, child_state.ivout);
1621 xfree(child_state.ivout);
1622 packet_set_iv(MODE_IN, child_state.ivin);
1623 xfree(child_state.ivin);
1626 memcpy(&incoming_stream, &child_state.incoming,
1627 sizeof(incoming_stream));
1628 memcpy(&outgoing_stream, &child_state.outgoing,
1629 sizeof(outgoing_stream));
1631 /* Update with new address */
1632 if (options.compression)
1633 mm_init_compression(pmonitor->m_zlib);
1635 /* Network I/O buffers */
1636 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1637 buffer_clear(&input);
1638 buffer_append(&input, child_state.input, child_state.ilen);
1639 memset(child_state.input, 0, child_state.ilen);
1640 xfree(child_state.input);
1642 buffer_clear(&output);
1643 buffer_append(&output, child_state.output, child_state.olen);
1644 memset(child_state.output, 0, child_state.olen);
1645 xfree(child_state.output);
1649 mm_get_kex(Buffer *m)
1655 kex = xcalloc(1, sizeof(*kex));
1656 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1657 if ((session_id2 == NULL) ||
1658 (kex->session_id_len != session_id2_len) ||
1659 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1660 fatal("mm_get_get: internal error: bad session id");
1661 kex->we_need = buffer_get_int(m);
1662 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1663 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1664 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1665 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1667 kex->hostkey_type = buffer_get_int(m);
1668 kex->kex_type = buffer_get_int(m);
1669 blob = buffer_get_string(m, &bloblen);
1670 buffer_init(&kex->my);
1671 buffer_append(&kex->my, blob, bloblen);
1673 blob = buffer_get_string(m, &bloblen);
1674 buffer_init(&kex->peer);
1675 buffer_append(&kex->peer, blob, bloblen);
1678 kex->flags = buffer_get_int(m);
1679 kex->client_version_string = buffer_get_string(m, NULL);
1680 kex->server_version_string = buffer_get_string(m, NULL);
1681 kex->load_host_key=&get_hostkey_by_type;
1682 kex->host_key_index=&get_hostkey_index;
1687 /* This function requries careful sanity checking */
1690 mm_get_keystate(struct monitor *pmonitor)
1694 u_int bloblen, plen;
1695 u_int32_t seqnr, packets;
1698 debug3("%s: Waiting for new keys", __func__);
1701 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1703 child_state.ssh1protoflags = buffer_get_int(&m);
1704 child_state.ssh1cipher = buffer_get_int(&m);
1705 child_state.ssh1key = buffer_get_string(&m,
1706 &child_state.ssh1keylen);
1707 child_state.ivout = buffer_get_string(&m,
1708 &child_state.ivoutlen);
1709 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1712 /* Get the Kex for rekeying */
1713 *pmonitor->m_pkex = mm_get_kex(&m);
1716 blob = buffer_get_string(&m, &bloblen);
1717 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1720 debug3("%s: Waiting for second key", __func__);
1721 blob = buffer_get_string(&m, &bloblen);
1722 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1725 /* Now get sequence numbers for the packets */
1726 seqnr = buffer_get_int(&m);
1727 blocks = buffer_get_int64(&m);
1728 packets = buffer_get_int(&m);
1729 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1730 seqnr = buffer_get_int(&m);
1731 blocks = buffer_get_int64(&m);
1732 packets = buffer_get_int(&m);
1733 packet_set_state(MODE_IN, seqnr, blocks, packets);
1736 /* Get the key context */
1737 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1738 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1740 debug3("%s: Getting compression state", __func__);
1741 /* Get compression state */
1742 p = buffer_get_string(&m, &plen);
1743 if (plen != sizeof(child_state.outgoing))
1744 fatal("%s: bad request size", __func__);
1745 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1748 p = buffer_get_string(&m, &plen);
1749 if (plen != sizeof(child_state.incoming))
1750 fatal("%s: bad request size", __func__);
1751 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1754 /* Network I/O buffers */
1755 debug3("%s: Getting Network I/O buffers", __func__);
1756 child_state.input = buffer_get_string(&m, &child_state.ilen);
1757 child_state.output = buffer_get_string(&m, &child_state.olen);
1763 /* Allocation functions for zlib */
1765 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1767 size_t len = (size_t) size * ncount;
1770 if (len == 0 || ncount > SIZE_T_MAX / size)
1771 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1773 address = mm_malloc(mm, len);
1779 mm_zfree(struct mm_master *mm, void *address)
1781 mm_free(mm, address);
1785 mm_init_compression(struct mm_master *mm)
1787 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1788 outgoing_stream.zfree = (free_func)mm_zfree;
1789 outgoing_stream.opaque = mm;
1791 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1792 incoming_stream.zfree = (free_func)mm_zfree;
1793 incoming_stream.opaque = mm;
1798 #define FD_CLOSEONEXEC(x) do { \
1799 if (fcntl(x, F_SETFD, 1) == -1) \
1800 fatal("fcntl(%d, F_SETFD)", x); \
1804 monitor_socketpair(int *pair)
1806 #ifdef HAVE_SOCKETPAIR
1807 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1808 fatal("%s: socketpair", __func__);
1810 fatal("%s: UsePrivilegeSeparation=yes not supported",
1813 FD_CLOSEONEXEC(pair[0]);
1814 FD_CLOSEONEXEC(pair[1]);
1817 #define MM_MEMSIZE 65536
1822 struct monitor *mon;
1825 mon = xcalloc(1, sizeof(*mon));
1827 monitor_socketpair(pair);
1829 mon->m_recvfd = pair[0];
1830 mon->m_sendfd = pair[1];
1832 /* Used to share zlib space across processes */
1833 if (options.compression) {
1834 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1835 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1837 /* Compression needs to share state across borders */
1838 mm_init_compression(mon->m_zlib);
1845 monitor_reinit(struct monitor *mon)
1849 monitor_socketpair(pair);
1851 mon->m_recvfd = pair[0];
1852 mon->m_sendfd = pair[1];
1857 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1863 goid.elements = buffer_get_string(m, &len);
1866 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1868 xfree(goid.elements);
1871 buffer_put_int(m, major);
1873 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1875 /* Now we have a context, enable the step */
1876 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1882 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1885 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1886 OM_uint32 major, minor;
1887 OM_uint32 flags = 0; /* GSI needs this */
1890 in.value = buffer_get_string(m, &len);
1892 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1896 buffer_put_int(m, major);
1897 buffer_put_string(m, out.value, out.length);
1898 buffer_put_int(m, flags);
1899 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1901 gss_release_buffer(&minor, &out);
1903 if (major == GSS_S_COMPLETE) {
1904 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1905 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1906 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1912 mm_answer_gss_checkmic(int sock, Buffer *m)
1914 gss_buffer_desc gssbuf, mic;
1918 gssbuf.value = buffer_get_string(m, &len);
1919 gssbuf.length = len;
1920 mic.value = buffer_get_string(m, &len);
1923 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1925 xfree(gssbuf.value);
1929 buffer_put_int(m, ret);
1931 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1933 if (!GSS_ERROR(ret))
1934 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1940 mm_answer_gss_userok(int sock, Buffer *m)
1944 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1947 buffer_put_int(m, authenticated);
1949 debug3("%s: sending result %d", __func__, authenticated);
1950 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1952 auth_method = "gssapi-with-mic";
1954 /* Monitor loop will terminate if authenticated */
1955 return (authenticated);