2 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2002 Markus Friedl <markus@openbsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 RCSID("$OpenBSD: monitor.c,v 1.64 2005/10/13 22:24:31 stevesk Exp $");
30 #include <openssl/dh.h>
40 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
43 #define TARGET_OS_MAC 1
48 #include "auth-options.h"
57 #include "monitor_mm.h"
58 #include "monitor_wrap.h"
59 #include "monitor_fdpass.h"
69 static Gssctxt *gsscontext = NULL;
73 extern ServerOptions options;
74 extern u_int utmp_len;
75 extern Newkeys *current_keys[];
76 extern z_stream incoming_stream;
77 extern z_stream outgoing_stream;
78 extern u_char session_id[];
79 extern Buffer input, output;
80 extern Buffer auth_debug;
81 extern int auth_debug_init;
82 extern Buffer loginmsg;
84 /* State exported from the child */
107 /* Functions on the monitor that answer unprivileged requests */
109 int mm_answer_moduli(int, Buffer *);
110 int mm_answer_sign(int, Buffer *);
111 int mm_answer_pwnamallow(int, Buffer *);
112 int mm_answer_auth2_read_banner(int, Buffer *);
113 int mm_answer_authserv(int, Buffer *);
114 int mm_answer_authpassword(int, Buffer *);
115 int mm_answer_bsdauthquery(int, Buffer *);
116 int mm_answer_bsdauthrespond(int, Buffer *);
117 int mm_answer_skeyquery(int, Buffer *);
118 int mm_answer_skeyrespond(int, Buffer *);
119 int mm_answer_keyallowed(int, Buffer *);
120 int mm_answer_keyverify(int, Buffer *);
121 int mm_answer_pty(int, Buffer *);
122 int mm_answer_pty_cleanup(int, Buffer *);
123 int mm_answer_term(int, Buffer *);
124 int mm_answer_rsa_keyallowed(int, Buffer *);
125 int mm_answer_rsa_challenge(int, Buffer *);
126 int mm_answer_rsa_response(int, Buffer *);
127 int mm_answer_sesskey(int, Buffer *);
128 int mm_answer_sessid(int, Buffer *);
131 int mm_answer_pam_start(int, Buffer *);
132 int mm_answer_pam_account(int, Buffer *);
133 int mm_answer_pam_init_ctx(int, Buffer *);
134 int mm_answer_pam_query(int, Buffer *);
135 int mm_answer_pam_respond(int, Buffer *);
136 int mm_answer_pam_free_ctx(int, Buffer *);
140 int mm_answer_gss_setup_ctx(int, Buffer *);
141 int mm_answer_gss_accept_ctx(int, Buffer *);
142 int mm_answer_gss_userok(int, Buffer *);
143 int mm_answer_gss_checkmic(int, Buffer *);
146 #ifdef SSH_AUDIT_EVENTS
147 int mm_answer_audit_event(int, Buffer *);
148 int mm_answer_audit_command(int, Buffer *);
151 static Authctxt *authctxt;
152 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
154 /* local state for key verify */
155 static u_char *key_blob = NULL;
156 static u_int key_bloblen = 0;
157 static int key_blobtype = MM_NOKEY;
158 static char *hostbased_cuser = NULL;
159 static char *hostbased_chost = NULL;
160 static char *auth_method = "unknown";
161 static u_int session_id2_len = 0;
162 static u_char *session_id2 = NULL;
163 static pid_t monitor_child_pid;
166 enum monitor_reqtype type;
168 int (*f)(int, Buffer *);
171 #define MON_ISAUTH 0x0004 /* Required for Authentication */
172 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
173 #define MON_ONCE 0x0010 /* Disable after calling */
175 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
177 #define MON_PERMIT 0x1000 /* Request is permitted */
179 struct mon_table mon_dispatch_proto20[] = {
180 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
181 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
182 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
183 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
184 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
185 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
187 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
188 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
189 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
190 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
191 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
192 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
194 #ifdef SSH_AUDIT_EVENTS
195 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
198 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
199 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
202 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
203 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
205 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
206 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
208 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
209 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
210 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
211 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
216 struct mon_table mon_dispatch_postauth20[] = {
217 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
218 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
219 {MONITOR_REQ_PTY, 0, mm_answer_pty},
220 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
221 {MONITOR_REQ_TERM, 0, mm_answer_term},
222 #ifdef SSH_AUDIT_EVENTS
223 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
224 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
229 struct mon_table mon_dispatch_proto15[] = {
230 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
231 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
232 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
233 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
234 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
235 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
236 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
237 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
239 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
240 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
243 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
244 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
247 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
248 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
249 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
250 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
251 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
252 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
254 #ifdef SSH_AUDIT_EVENTS
255 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
260 struct mon_table mon_dispatch_postauth15[] = {
261 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
262 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
263 {MONITOR_REQ_TERM, 0, mm_answer_term},
264 #ifdef SSH_AUDIT_EVENTS
265 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
266 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
271 struct mon_table *mon_dispatch;
273 /* Specifies if a certain message is allowed at the moment */
276 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
278 while (ent->f != NULL) {
279 if (ent->type == type) {
280 ent->flags &= ~MON_PERMIT;
281 ent->flags |= permit ? MON_PERMIT : 0;
289 monitor_permit_authentications(int permit)
291 struct mon_table *ent = mon_dispatch;
293 while (ent->f != NULL) {
294 if (ent->flags & MON_AUTH) {
295 ent->flags &= ~MON_PERMIT;
296 ent->flags |= permit ? MON_PERMIT : 0;
303 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
305 struct mon_table *ent;
306 int authenticated = 0;
308 debug3("preauth child monitor started");
310 authctxt = _authctxt;
311 memset(authctxt, 0, sizeof(*authctxt));
313 authctxt->loginmsg = &loginmsg;
316 mon_dispatch = mon_dispatch_proto20;
318 /* Permit requests for moduli and signatures */
319 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
320 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
322 mon_dispatch = mon_dispatch_proto15;
324 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
327 /* The first few requests do not require asynchronous access */
328 while (!authenticated) {
329 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
331 if (!(ent->flags & MON_AUTHDECIDE))
332 fatal("%s: unexpected authentication from %d",
333 __func__, ent->type);
334 if (authctxt->pw->pw_uid == 0 &&
335 !auth_root_allowed(auth_method))
338 /* PAM needs to perform account checks after auth */
339 if (options.use_pam && authenticated) {
343 mm_request_receive_expect(pmonitor->m_sendfd,
344 MONITOR_REQ_PAM_ACCOUNT, &m);
345 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
351 if (ent->flags & MON_AUTHDECIDE) {
352 auth_log(authctxt, authenticated, auth_method,
353 compat20 ? " ssh2" : "");
355 authctxt->failures++;
359 if (!authctxt->valid)
360 fatal("%s: authenticated invalid user", __func__);
362 debug("%s: %s has been authenticated by privileged process",
363 __func__, authctxt->user);
365 mm_get_keystate(pmonitor);
369 monitor_set_child_handler(pid_t pid)
371 monitor_child_pid = pid;
375 monitor_child_handler(int sig)
377 kill(monitor_child_pid, sig);
381 monitor_child_postauth(struct monitor *pmonitor)
383 monitor_set_child_handler(pmonitor->m_pid);
384 signal(SIGHUP, &monitor_child_handler);
385 signal(SIGTERM, &monitor_child_handler);
388 mon_dispatch = mon_dispatch_postauth20;
390 /* Permit requests for moduli and signatures */
391 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
392 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
393 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
395 mon_dispatch = mon_dispatch_postauth15;
396 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
399 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
400 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
404 monitor_read(pmonitor, mon_dispatch, NULL);
408 monitor_sync(struct monitor *pmonitor)
410 if (options.compression) {
411 /* The member allocation is not visible, so sync it */
412 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
417 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
418 struct mon_table **pent)
426 mm_request_receive(pmonitor->m_sendfd, &m);
427 type = buffer_get_char(&m);
429 debug3("%s: checking request %d", __func__, type);
431 while (ent->f != NULL) {
432 if (ent->type == type)
437 if (ent->f != NULL) {
438 if (!(ent->flags & MON_PERMIT))
439 fatal("%s: unpermitted request %d", __func__,
441 ret = (*ent->f)(pmonitor->m_sendfd, &m);
444 /* The child may use this request only once, disable it */
445 if (ent->flags & MON_ONCE) {
446 debug2("%s: %d used once, disabling now", __func__,
448 ent->flags &= ~MON_PERMIT;
457 fatal("%s: unsupported request: %d", __func__, type);
463 /* allowed key state */
465 monitor_allowed_key(u_char *blob, u_int bloblen)
467 /* make sure key is allowed */
468 if (key_blob == NULL || key_bloblen != bloblen ||
469 memcmp(key_blob, blob, key_bloblen))
475 monitor_reset_key_state(void)
478 if (key_blob != NULL)
480 if (hostbased_cuser != NULL)
481 xfree(hostbased_cuser);
482 if (hostbased_chost != NULL)
483 xfree(hostbased_chost);
486 key_blobtype = MM_NOKEY;
487 hostbased_cuser = NULL;
488 hostbased_chost = NULL;
492 mm_answer_moduli(int sock, Buffer *m)
497 min = buffer_get_int(m);
498 want = buffer_get_int(m);
499 max = buffer_get_int(m);
501 debug3("%s: got parameters: %d %d %d",
502 __func__, min, want, max);
503 /* We need to check here, too, in case the child got corrupted */
504 if (max < min || want < min || max < want)
505 fatal("%s: bad parameters: %d %d %d",
506 __func__, min, want, max);
510 dh = choose_dh(min, want, max);
512 buffer_put_char(m, 0);
515 /* Send first bignum */
516 buffer_put_char(m, 1);
517 buffer_put_bignum2(m, dh->p);
518 buffer_put_bignum2(m, dh->g);
522 mm_request_send(sock, MONITOR_ANS_MODULI, m);
527 mm_answer_sign(int sock, Buffer *m)
532 u_int siglen, datlen;
535 debug3("%s", __func__);
537 keyid = buffer_get_int(m);
538 p = buffer_get_string(m, &datlen);
541 fatal("%s: data length incorrect: %u", __func__, datlen);
543 /* save session id, it will be passed on the first call */
544 if (session_id2_len == 0) {
545 session_id2_len = datlen;
546 session_id2 = xmalloc(session_id2_len);
547 memcpy(session_id2, p, session_id2_len);
550 if ((key = get_hostkey_by_index(keyid)) == NULL)
551 fatal("%s: no hostkey from index %d", __func__, keyid);
552 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
553 fatal("%s: key_sign failed", __func__);
555 debug3("%s: signature %p(%u)", __func__, signature, siglen);
558 buffer_put_string(m, signature, siglen);
563 mm_request_send(sock, MONITOR_ANS_SIGN, m);
565 /* Turn on permissions for getpwnam */
566 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
571 /* Retrieves the password entry and also checks if the user is permitted */
574 mm_answer_pwnamallow(int sock, Buffer *m)
577 struct passwd *pwent;
580 debug3("%s", __func__);
582 if (authctxt->attempt++ != 0)
583 fatal("%s: multiple attempts for getpwnam", __func__);
585 username = buffer_get_string(m, NULL);
587 pwent = getpwnamallow(username);
589 authctxt->user = xstrdup(username);
590 setproctitle("%s [priv]", pwent ? username : "unknown");
596 buffer_put_char(m, 0);
597 authctxt->pw = fakepw();
602 authctxt->pw = pwent;
605 buffer_put_char(m, 1);
606 buffer_put_string(m, pwent, sizeof(struct passwd));
607 buffer_put_cstring(m, pwent->pw_name);
608 buffer_put_cstring(m, "*");
609 buffer_put_cstring(m, pwent->pw_gecos);
610 #ifdef HAVE_PW_CLASS_IN_PASSWD
611 buffer_put_cstring(m, pwent->pw_class);
613 buffer_put_cstring(m, pwent->pw_dir);
614 buffer_put_cstring(m, pwent->pw_shell);
617 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
618 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
620 /* For SSHv1 allow authentication now */
622 monitor_permit_authentications(1);
624 /* Allow service/style information on the auth context */
625 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
626 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
631 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
633 #ifdef SSH_AUDIT_EVENTS
634 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
640 int mm_answer_auth2_read_banner(int sock, Buffer *m)
645 banner = auth2_read_banner();
646 buffer_put_cstring(m, banner != NULL ? banner : "");
647 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
656 mm_answer_authserv(int sock, Buffer *m)
658 monitor_permit_authentications(1);
660 authctxt->service = buffer_get_string(m, NULL);
661 authctxt->style = buffer_get_string(m, NULL);
662 debug3("%s: service=%s, style=%s",
663 __func__, authctxt->service, authctxt->style);
665 if (strlen(authctxt->style) == 0) {
666 xfree(authctxt->style);
667 authctxt->style = NULL;
674 mm_answer_authpassword(int sock, Buffer *m)
676 static int call_count;
681 passwd = buffer_get_string(m, &plen);
682 /* Only authenticate if the context is valid */
683 authenticated = options.password_authentication &&
684 auth_password(authctxt, passwd);
685 memset(passwd, 0, strlen(passwd));
689 buffer_put_int(m, authenticated);
691 debug3("%s: sending result %d", __func__, authenticated);
692 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
695 if (plen == 0 && call_count == 1)
696 auth_method = "none";
698 auth_method = "password";
700 /* Causes monitor loop to terminate if authenticated */
701 return (authenticated);
706 mm_answer_bsdauthquery(int sock, Buffer *m)
708 char *name, *infotxt;
714 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
715 &prompts, &echo_on) < 0 ? 0 : 1;
718 buffer_put_int(m, success);
720 buffer_put_cstring(m, prompts[0]);
722 debug3("%s: sending challenge success: %u", __func__, success);
723 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
736 mm_answer_bsdauthrespond(int sock, Buffer *m)
741 if (authctxt->as == 0)
742 fatal("%s: no bsd auth session", __func__);
744 response = buffer_get_string(m, NULL);
745 authok = options.challenge_response_authentication &&
746 auth_userresponse(authctxt->as, response, 0);
748 debug3("%s: <%s> = <%d>", __func__, response, authok);
752 buffer_put_int(m, authok);
754 debug3("%s: sending authenticated: %d", __func__, authok);
755 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
757 auth_method = "bsdauth";
759 return (authok != 0);
765 mm_answer_skeyquery(int sock, Buffer *m)
768 char challenge[1024];
771 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
772 sizeof(challenge)) < 0 ? 0 : 1;
775 buffer_put_int(m, success);
777 buffer_put_cstring(m, challenge);
779 debug3("%s: sending challenge success: %u", __func__, success);
780 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
786 mm_answer_skeyrespond(int sock, Buffer *m)
791 response = buffer_get_string(m, NULL);
793 authok = (options.challenge_response_authentication &&
795 skey_haskey(authctxt->pw->pw_name) == 0 &&
796 skey_passcheck(authctxt->pw->pw_name, response) != -1);
801 buffer_put_int(m, authok);
803 debug3("%s: sending authenticated: %d", __func__, authok);
804 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
806 auth_method = "skey";
808 return (authok != 0);
814 mm_answer_pam_start(int sock, Buffer *m)
816 if (!options.use_pam)
817 fatal("UsePAM not set, but ended up in %s anyway", __func__);
821 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
827 mm_answer_pam_account(int sock, Buffer *m)
831 if (!options.use_pam)
832 fatal("UsePAM not set, but ended up in %s anyway", __func__);
834 ret = do_pam_account();
836 buffer_put_int(m, ret);
837 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
839 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
844 static void *sshpam_ctxt, *sshpam_authok;
845 extern KbdintDevice sshpam_device;
848 mm_answer_pam_init_ctx(int sock, Buffer *m)
851 debug3("%s", __func__);
852 authctxt->user = buffer_get_string(m, NULL);
853 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
854 sshpam_authok = NULL;
856 if (sshpam_ctxt != NULL) {
857 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
858 buffer_put_int(m, 1);
860 buffer_put_int(m, 0);
862 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
867 mm_answer_pam_query(int sock, Buffer *m)
869 char *name, *info, **prompts;
870 u_int i, num, *echo_on;
873 debug3("%s", __func__);
874 sshpam_authok = NULL;
875 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
876 if (ret == 0 && num == 0)
877 sshpam_authok = sshpam_ctxt;
878 if (num > 1 || name == NULL || info == NULL)
881 buffer_put_int(m, ret);
882 buffer_put_cstring(m, name);
884 buffer_put_cstring(m, info);
886 buffer_put_int(m, num);
887 for (i = 0; i < num; ++i) {
888 buffer_put_cstring(m, prompts[i]);
890 buffer_put_int(m, echo_on[i]);
896 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
901 mm_answer_pam_respond(int sock, Buffer *m)
907 debug3("%s", __func__);
908 sshpam_authok = NULL;
909 num = buffer_get_int(m);
911 resp = xmalloc(num * sizeof(char *));
912 for (i = 0; i < num; ++i)
913 resp[i] = buffer_get_string(m, NULL);
914 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
915 for (i = 0; i < num; ++i)
919 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
922 buffer_put_int(m, ret);
923 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
924 auth_method = "keyboard-interactive/pam";
926 sshpam_authok = sshpam_ctxt;
931 mm_answer_pam_free_ctx(int sock, Buffer *m)
934 debug3("%s", __func__);
935 (sshpam_device.free_ctx)(sshpam_ctxt);
937 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
938 return (sshpam_authok == sshpam_ctxt);
943 mm_append_debug(Buffer *m)
945 if (auth_debug_init && buffer_len(&auth_debug)) {
946 debug3("%s: Appending debug messages for child", __func__);
947 buffer_append(m, buffer_ptr(&auth_debug),
948 buffer_len(&auth_debug));
949 buffer_clear(&auth_debug);
954 mm_answer_keyallowed(int sock, Buffer *m)
960 enum mm_keytype type = 0;
963 debug3("%s entering", __func__);
965 type = buffer_get_int(m);
966 cuser = buffer_get_string(m, NULL);
967 chost = buffer_get_string(m, NULL);
968 blob = buffer_get_string(m, &bloblen);
970 key = key_from_blob(blob, bloblen);
972 if ((compat20 && type == MM_RSAHOSTKEY) ||
973 (!compat20 && type != MM_RSAHOSTKEY))
974 fatal("%s: key type and protocol mismatch", __func__);
976 debug3("%s: key_from_blob: %p", __func__, key);
978 if (key != NULL && authctxt->valid) {
981 allowed = options.pubkey_authentication &&
982 user_key_allowed(authctxt->pw, key);
985 allowed = options.hostbased_authentication &&
986 hostbased_key_allowed(authctxt->pw,
990 key->type = KEY_RSA1; /* XXX */
991 allowed = options.rhosts_rsa_authentication &&
992 auth_rhosts_rsa_key_allowed(authctxt->pw,
996 fatal("%s: unknown key type %d", __func__, type);
1003 /* clear temporarily storage (used by verify) */
1004 monitor_reset_key_state();
1007 /* Save temporarily for comparison in verify */
1009 key_bloblen = bloblen;
1010 key_blobtype = type;
1011 hostbased_cuser = cuser;
1012 hostbased_chost = chost;
1015 debug3("%s: key %p is %s",
1016 __func__, key, allowed ? "allowed" : "disallowed");
1019 buffer_put_int(m, allowed);
1020 buffer_put_int(m, forced_command != NULL);
1024 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1026 if (type == MM_RSAHOSTKEY)
1027 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1033 monitor_valid_userblob(u_char *data, u_int datalen)
1041 buffer_append(&b, data, datalen);
1043 if (datafellows & SSH_OLD_SESSIONID) {
1045 len = buffer_len(&b);
1046 if ((session_id2 == NULL) ||
1047 (len < session_id2_len) ||
1048 (memcmp(p, session_id2, session_id2_len) != 0))
1050 buffer_consume(&b, session_id2_len);
1052 p = buffer_get_string(&b, &len);
1053 if ((session_id2 == NULL) ||
1054 (len != session_id2_len) ||
1055 (memcmp(p, session_id2, session_id2_len) != 0))
1059 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1061 p = buffer_get_string(&b, NULL);
1062 if (strcmp(authctxt->user, p) != 0) {
1063 logit("wrong user name passed to monitor: expected %s != %.100s",
1068 buffer_skip_string(&b);
1069 if (datafellows & SSH_BUG_PKAUTH) {
1070 if (!buffer_get_char(&b))
1073 p = buffer_get_string(&b, NULL);
1074 if (strcmp("publickey", p) != 0)
1077 if (!buffer_get_char(&b))
1079 buffer_skip_string(&b);
1081 buffer_skip_string(&b);
1082 if (buffer_len(&b) != 0)
1089 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1098 buffer_append(&b, data, datalen);
1100 p = buffer_get_string(&b, &len);
1101 if ((session_id2 == NULL) ||
1102 (len != session_id2_len) ||
1103 (memcmp(p, session_id2, session_id2_len) != 0))
1107 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1109 p = buffer_get_string(&b, NULL);
1110 if (strcmp(authctxt->user, p) != 0) {
1111 logit("wrong user name passed to monitor: expected %s != %.100s",
1116 buffer_skip_string(&b); /* service */
1117 p = buffer_get_string(&b, NULL);
1118 if (strcmp(p, "hostbased") != 0)
1121 buffer_skip_string(&b); /* pkalg */
1122 buffer_skip_string(&b); /* pkblob */
1124 /* verify client host, strip trailing dot if necessary */
1125 p = buffer_get_string(&b, NULL);
1126 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1128 if (strcmp(p, chost) != 0)
1132 /* verify client user */
1133 p = buffer_get_string(&b, NULL);
1134 if (strcmp(p, cuser) != 0)
1138 if (buffer_len(&b) != 0)
1145 mm_answer_keyverify(int sock, Buffer *m)
1148 u_char *signature, *data, *blob;
1149 u_int signaturelen, datalen, bloblen;
1153 blob = buffer_get_string(m, &bloblen);
1154 signature = buffer_get_string(m, &signaturelen);
1155 data = buffer_get_string(m, &datalen);
1157 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1158 !monitor_allowed_key(blob, bloblen))
1159 fatal("%s: bad key, not previously allowed", __func__);
1161 key = key_from_blob(blob, bloblen);
1163 fatal("%s: bad public key blob", __func__);
1165 switch (key_blobtype) {
1167 valid_data = monitor_valid_userblob(data, datalen);
1170 valid_data = monitor_valid_hostbasedblob(data, datalen,
1171 hostbased_cuser, hostbased_chost);
1178 fatal("%s: bad signature data blob", __func__);
1180 verified = key_verify(key, signature, signaturelen, data, datalen);
1181 debug3("%s: key %p signature %s",
1182 __func__, key, verified ? "verified" : "unverified");
1189 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1191 monitor_reset_key_state();
1194 buffer_put_int(m, verified);
1195 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1201 mm_record_login(Session *s, struct passwd *pw)
1204 struct sockaddr_storage from;
1207 * Get IP address of client. If the connection is not a socket, let
1208 * the address be 0.0.0.0.
1210 memset(&from, 0, sizeof(from));
1211 fromlen = sizeof(from);
1212 if (packet_connection_is_on_socket()) {
1213 if (getpeername(packet_get_connection_in(),
1214 (struct sockaddr *) & from, &fromlen) < 0) {
1215 debug("getpeername: %.100s", strerror(errno));
1219 /* Record that there was a login on that tty from the remote host. */
1220 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1221 get_remote_name_or_ip(utmp_len, options.use_dns),
1222 (struct sockaddr *)&from, fromlen);
1226 mm_session_close(Session *s)
1228 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1229 if (s->ttyfd != -1) {
1230 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1231 session_pty_cleanup2(s);
1237 mm_answer_pty(int sock, Buffer *m)
1239 extern struct monitor *pmonitor;
1243 debug3("%s entering", __func__);
1249 s->authctxt = authctxt;
1250 s->pw = authctxt->pw;
1251 s->pid = pmonitor->m_pid;
1252 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1255 pty_setowner(authctxt->pw, s->tty);
1257 buffer_put_int(m, 1);
1258 buffer_put_cstring(m, s->tty);
1260 /* We need to trick ttyslot */
1261 if (dup2(s->ttyfd, 0) == -1)
1262 fatal("%s: dup2", __func__);
1264 mm_record_login(s, authctxt->pw);
1266 /* Now we can close the file descriptor again */
1269 /* send messages generated by record_login */
1270 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1271 buffer_clear(&loginmsg);
1273 mm_request_send(sock, MONITOR_ANS_PTY, m);
1275 mm_send_fd(sock, s->ptyfd);
1276 mm_send_fd(sock, s->ttyfd);
1278 /* make sure nothing uses fd 0 */
1279 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1280 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1282 error("%s: fd0 %d != 0", __func__, fd0);
1284 /* slave is not needed */
1286 s->ttyfd = s->ptyfd;
1287 /* no need to dup() because nobody closes ptyfd */
1288 s->ptymaster = s->ptyfd;
1290 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1296 mm_session_close(s);
1297 buffer_put_int(m, 0);
1298 mm_request_send(sock, MONITOR_ANS_PTY, m);
1303 mm_answer_pty_cleanup(int sock, Buffer *m)
1308 debug3("%s entering", __func__);
1310 tty = buffer_get_string(m, NULL);
1311 if ((s = session_by_tty(tty)) != NULL)
1312 mm_session_close(s);
1319 mm_answer_sesskey(int sock, Buffer *m)
1324 /* Turn off permissions */
1325 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1327 if ((p = BN_new()) == NULL)
1328 fatal("%s: BN_new", __func__);
1330 buffer_get_bignum2(m, p);
1332 rsafail = ssh1_session_key(p);
1335 buffer_put_int(m, rsafail);
1336 buffer_put_bignum2(m, p);
1340 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1342 /* Turn on permissions for sessid passing */
1343 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1349 mm_answer_sessid(int sock, Buffer *m)
1353 debug3("%s entering", __func__);
1355 if (buffer_len(m) != 16)
1356 fatal("%s: bad ssh1 session id", __func__);
1357 for (i = 0; i < 16; i++)
1358 session_id[i] = buffer_get_char(m);
1360 /* Turn on permissions for getpwnam */
1361 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1367 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1371 u_char *blob = NULL;
1375 debug3("%s entering", __func__);
1377 if (options.rsa_authentication && authctxt->valid) {
1378 if ((client_n = BN_new()) == NULL)
1379 fatal("%s: BN_new", __func__);
1380 buffer_get_bignum2(m, client_n);
1381 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1382 BN_clear_free(client_n);
1385 buffer_put_int(m, allowed);
1386 buffer_put_int(m, forced_command != NULL);
1388 /* clear temporarily storage (used by generate challenge) */
1389 monitor_reset_key_state();
1391 if (allowed && key != NULL) {
1392 key->type = KEY_RSA; /* cheat for key_to_blob */
1393 if (key_to_blob(key, &blob, &blen) == 0)
1394 fatal("%s: key_to_blob failed", __func__);
1395 buffer_put_string(m, blob, blen);
1397 /* Save temporarily for comparison in verify */
1400 key_blobtype = MM_RSAUSERKEY;
1407 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1409 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1410 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1415 mm_answer_rsa_challenge(int sock, Buffer *m)
1421 debug3("%s entering", __func__);
1423 if (!authctxt->valid)
1424 fatal("%s: authctxt not valid", __func__);
1425 blob = buffer_get_string(m, &blen);
1426 if (!monitor_allowed_key(blob, blen))
1427 fatal("%s: bad key, not previously allowed", __func__);
1428 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1429 fatal("%s: key type mismatch", __func__);
1430 if ((key = key_from_blob(blob, blen)) == NULL)
1431 fatal("%s: received bad key", __func__);
1434 BN_clear_free(ssh1_challenge);
1435 ssh1_challenge = auth_rsa_generate_challenge(key);
1438 buffer_put_bignum2(m, ssh1_challenge);
1440 debug3("%s sending reply", __func__);
1441 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1443 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1451 mm_answer_rsa_response(int sock, Buffer *m)
1454 u_char *blob, *response;
1458 debug3("%s entering", __func__);
1460 if (!authctxt->valid)
1461 fatal("%s: authctxt not valid", __func__);
1462 if (ssh1_challenge == NULL)
1463 fatal("%s: no ssh1_challenge", __func__);
1465 blob = buffer_get_string(m, &blen);
1466 if (!monitor_allowed_key(blob, blen))
1467 fatal("%s: bad key, not previously allowed", __func__);
1468 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1469 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1470 if ((key = key_from_blob(blob, blen)) == NULL)
1471 fatal("%s: received bad key", __func__);
1472 response = buffer_get_string(m, &len);
1474 fatal("%s: received bad response to challenge", __func__);
1475 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1481 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1484 BN_clear_free(ssh1_challenge);
1485 ssh1_challenge = NULL;
1486 monitor_reset_key_state();
1489 buffer_put_int(m, success);
1490 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1496 mm_answer_term(int sock, Buffer *req)
1498 extern struct monitor *pmonitor;
1501 debug3("%s: tearing down sessions", __func__);
1503 /* The child is terminating */
1504 session_destroy_all(&mm_session_close);
1506 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1510 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1512 /* Terminate process */
1516 #ifdef SSH_AUDIT_EVENTS
1517 /* Report that an audit event occurred */
1519 mm_answer_audit_event(int socket, Buffer *m)
1521 ssh_audit_event_t event;
1523 debug3("%s entering", __func__);
1525 event = buffer_get_int(m);
1527 case SSH_AUTH_FAIL_PUBKEY:
1528 case SSH_AUTH_FAIL_HOSTBASED:
1529 case SSH_AUTH_FAIL_GSSAPI:
1530 case SSH_LOGIN_EXCEED_MAXTRIES:
1531 case SSH_LOGIN_ROOT_DENIED:
1532 case SSH_CONNECTION_CLOSE:
1533 case SSH_INVALID_USER:
1537 fatal("Audit event type %d not permitted", event);
1544 mm_answer_audit_command(int socket, Buffer *m)
1549 debug3("%s entering", __func__);
1550 cmd = buffer_get_string(m, &len);
1551 /* sanity check command, if so how? */
1552 audit_run_command(cmd);
1556 #endif /* SSH_AUDIT_EVENTS */
1559 monitor_apply_keystate(struct monitor *pmonitor)
1562 set_newkeys(MODE_IN);
1563 set_newkeys(MODE_OUT);
1565 packet_set_protocol_flags(child_state.ssh1protoflags);
1566 packet_set_encryption_key(child_state.ssh1key,
1567 child_state.ssh1keylen, child_state.ssh1cipher);
1568 xfree(child_state.ssh1key);
1571 /* for rc4 and other stateful ciphers */
1572 packet_set_keycontext(MODE_OUT, child_state.keyout);
1573 xfree(child_state.keyout);
1574 packet_set_keycontext(MODE_IN, child_state.keyin);
1575 xfree(child_state.keyin);
1578 packet_set_iv(MODE_OUT, child_state.ivout);
1579 xfree(child_state.ivout);
1580 packet_set_iv(MODE_IN, child_state.ivin);
1581 xfree(child_state.ivin);
1584 memcpy(&incoming_stream, &child_state.incoming,
1585 sizeof(incoming_stream));
1586 memcpy(&outgoing_stream, &child_state.outgoing,
1587 sizeof(outgoing_stream));
1589 /* Update with new address */
1590 if (options.compression)
1591 mm_init_compression(pmonitor->m_zlib);
1593 /* Network I/O buffers */
1594 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1595 buffer_clear(&input);
1596 buffer_append(&input, child_state.input, child_state.ilen);
1597 memset(child_state.input, 0, child_state.ilen);
1598 xfree(child_state.input);
1600 buffer_clear(&output);
1601 buffer_append(&output, child_state.output, child_state.olen);
1602 memset(child_state.output, 0, child_state.olen);
1603 xfree(child_state.output);
1607 mm_get_kex(Buffer *m)
1613 kex = xmalloc(sizeof(*kex));
1614 memset(kex, 0, sizeof(*kex));
1615 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1616 if ((session_id2 == NULL) ||
1617 (kex->session_id_len != session_id2_len) ||
1618 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1619 fatal("mm_get_get: internal error: bad session id");
1620 kex->we_need = buffer_get_int(m);
1621 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1622 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1623 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1625 kex->hostkey_type = buffer_get_int(m);
1626 kex->kex_type = buffer_get_int(m);
1627 blob = buffer_get_string(m, &bloblen);
1628 buffer_init(&kex->my);
1629 buffer_append(&kex->my, blob, bloblen);
1631 blob = buffer_get_string(m, &bloblen);
1632 buffer_init(&kex->peer);
1633 buffer_append(&kex->peer, blob, bloblen);
1636 kex->flags = buffer_get_int(m);
1637 kex->client_version_string = buffer_get_string(m, NULL);
1638 kex->server_version_string = buffer_get_string(m, NULL);
1639 kex->load_host_key=&get_hostkey_by_type;
1640 kex->host_key_index=&get_hostkey_index;
1645 /* This function requries careful sanity checking */
1648 mm_get_keystate(struct monitor *pmonitor)
1652 u_int bloblen, plen;
1653 u_int32_t seqnr, packets;
1656 debug3("%s: Waiting for new keys", __func__);
1659 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1661 child_state.ssh1protoflags = buffer_get_int(&m);
1662 child_state.ssh1cipher = buffer_get_int(&m);
1663 child_state.ssh1key = buffer_get_string(&m,
1664 &child_state.ssh1keylen);
1665 child_state.ivout = buffer_get_string(&m,
1666 &child_state.ivoutlen);
1667 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1670 /* Get the Kex for rekeying */
1671 *pmonitor->m_pkex = mm_get_kex(&m);
1674 blob = buffer_get_string(&m, &bloblen);
1675 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1678 debug3("%s: Waiting for second key", __func__);
1679 blob = buffer_get_string(&m, &bloblen);
1680 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1683 /* Now get sequence numbers for the packets */
1684 seqnr = buffer_get_int(&m);
1685 blocks = buffer_get_int64(&m);
1686 packets = buffer_get_int(&m);
1687 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1688 seqnr = buffer_get_int(&m);
1689 blocks = buffer_get_int64(&m);
1690 packets = buffer_get_int(&m);
1691 packet_set_state(MODE_IN, seqnr, blocks, packets);
1694 /* Get the key context */
1695 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1696 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1698 debug3("%s: Getting compression state", __func__);
1699 /* Get compression state */
1700 p = buffer_get_string(&m, &plen);
1701 if (plen != sizeof(child_state.outgoing))
1702 fatal("%s: bad request size", __func__);
1703 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1706 p = buffer_get_string(&m, &plen);
1707 if (plen != sizeof(child_state.incoming))
1708 fatal("%s: bad request size", __func__);
1709 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1712 /* Network I/O buffers */
1713 debug3("%s: Getting Network I/O buffers", __func__);
1714 child_state.input = buffer_get_string(&m, &child_state.ilen);
1715 child_state.output = buffer_get_string(&m, &child_state.olen);
1721 /* Allocation functions for zlib */
1723 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1725 size_t len = (size_t) size * ncount;
1728 if (len == 0 || ncount > SIZE_T_MAX / size)
1729 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1731 address = mm_malloc(mm, len);
1737 mm_zfree(struct mm_master *mm, void *address)
1739 mm_free(mm, address);
1743 mm_init_compression(struct mm_master *mm)
1745 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1746 outgoing_stream.zfree = (free_func)mm_zfree;
1747 outgoing_stream.opaque = mm;
1749 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1750 incoming_stream.zfree = (free_func)mm_zfree;
1751 incoming_stream.opaque = mm;
1756 #define FD_CLOSEONEXEC(x) do { \
1757 if (fcntl(x, F_SETFD, 1) == -1) \
1758 fatal("fcntl(%d, F_SETFD)", x); \
1762 monitor_socketpair(int *pair)
1764 #ifdef HAVE_SOCKETPAIR
1765 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1766 fatal("%s: socketpair", __func__);
1768 fatal("%s: UsePrivilegeSeparation=yes not supported",
1771 FD_CLOSEONEXEC(pair[0]);
1772 FD_CLOSEONEXEC(pair[1]);
1775 #define MM_MEMSIZE 65536
1780 struct monitor *mon;
1783 mon = xmalloc(sizeof(*mon));
1786 monitor_socketpair(pair);
1788 mon->m_recvfd = pair[0];
1789 mon->m_sendfd = pair[1];
1791 /* Used to share zlib space across processes */
1792 if (options.compression) {
1793 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1794 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1796 /* Compression needs to share state across borders */
1797 mm_init_compression(mon->m_zlib);
1804 monitor_reinit(struct monitor *mon)
1808 monitor_socketpair(pair);
1810 mon->m_recvfd = pair[0];
1811 mon->m_sendfd = pair[1];
1816 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1822 goid.elements = buffer_get_string(m, &len);
1825 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1827 xfree(goid.elements);
1830 buffer_put_int(m, major);
1832 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1834 /* Now we have a context, enable the step */
1835 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1841 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1844 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1845 OM_uint32 major, minor;
1846 OM_uint32 flags = 0; /* GSI needs this */
1849 in.value = buffer_get_string(m, &len);
1851 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1855 buffer_put_int(m, major);
1856 buffer_put_string(m, out.value, out.length);
1857 buffer_put_int(m, flags);
1858 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1860 gss_release_buffer(&minor, &out);
1862 if (major == GSS_S_COMPLETE) {
1863 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1864 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1865 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1871 mm_answer_gss_checkmic(int sock, Buffer *m)
1873 gss_buffer_desc gssbuf, mic;
1877 gssbuf.value = buffer_get_string(m, &len);
1878 gssbuf.length = len;
1879 mic.value = buffer_get_string(m, &len);
1882 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1884 xfree(gssbuf.value);
1888 buffer_put_int(m, ret);
1890 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1892 if (!GSS_ERROR(ret))
1893 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1899 mm_answer_gss_userok(int sock, Buffer *m)
1903 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1906 buffer_put_int(m, authenticated);
1908 debug3("%s: sending result %d", __func__, authenticated);
1909 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1911 auth_method = "gssapi-with-mic";
1913 /* Monitor loop will terminate if authenticated */
1914 return (authenticated);