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.66 2006/02/08 13:15:44 stevesk Exp $");
36 #include <openssl/dh.h>
42 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
45 #define TARGET_OS_MAC 1
50 #include "auth-options.h"
59 #include "monitor_mm.h"
60 #include "monitor_wrap.h"
61 #include "monitor_fdpass.h"
71 static Gssctxt *gsscontext = NULL;
75 extern ServerOptions options;
76 extern u_int utmp_len;
77 extern Newkeys *current_keys[];
78 extern z_stream incoming_stream;
79 extern z_stream outgoing_stream;
80 extern u_char session_id[];
81 extern Buffer input, output;
82 extern Buffer auth_debug;
83 extern int auth_debug_init;
84 extern Buffer loginmsg;
86 /* State exported from the child */
109 /* Functions on the monitor that answer unprivileged requests */
111 int mm_answer_moduli(int, Buffer *);
112 int mm_answer_sign(int, Buffer *);
113 int mm_answer_pwnamallow(int, Buffer *);
114 int mm_answer_auth2_read_banner(int, Buffer *);
115 int mm_answer_authserv(int, Buffer *);
116 int mm_answer_authpassword(int, Buffer *);
117 int mm_answer_bsdauthquery(int, Buffer *);
118 int mm_answer_bsdauthrespond(int, Buffer *);
119 int mm_answer_skeyquery(int, Buffer *);
120 int mm_answer_skeyrespond(int, Buffer *);
121 int mm_answer_keyallowed(int, Buffer *);
122 int mm_answer_keyverify(int, Buffer *);
123 int mm_answer_pty(int, Buffer *);
124 int mm_answer_pty_cleanup(int, Buffer *);
125 int mm_answer_term(int, Buffer *);
126 int mm_answer_rsa_keyallowed(int, Buffer *);
127 int mm_answer_rsa_challenge(int, Buffer *);
128 int mm_answer_rsa_response(int, Buffer *);
129 int mm_answer_sesskey(int, Buffer *);
130 int mm_answer_sessid(int, Buffer *);
133 int mm_answer_pam_start(int, Buffer *);
134 int mm_answer_pam_account(int, Buffer *);
135 int mm_answer_pam_init_ctx(int, Buffer *);
136 int mm_answer_pam_query(int, Buffer *);
137 int mm_answer_pam_respond(int, Buffer *);
138 int mm_answer_pam_free_ctx(int, Buffer *);
142 int mm_answer_gss_setup_ctx(int, Buffer *);
143 int mm_answer_gss_accept_ctx(int, Buffer *);
144 int mm_answer_gss_userok(int, Buffer *);
145 int mm_answer_gss_checkmic(int, Buffer *);
148 #ifdef SSH_AUDIT_EVENTS
149 int mm_answer_audit_event(int, Buffer *);
150 int mm_answer_audit_command(int, Buffer *);
153 static Authctxt *authctxt;
154 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
156 /* local state for key verify */
157 static u_char *key_blob = NULL;
158 static u_int key_bloblen = 0;
159 static int key_blobtype = MM_NOKEY;
160 static char *hostbased_cuser = NULL;
161 static char *hostbased_chost = NULL;
162 static char *auth_method = "unknown";
163 static u_int session_id2_len = 0;
164 static u_char *session_id2 = NULL;
165 static pid_t monitor_child_pid;
168 enum monitor_reqtype type;
170 int (*f)(int, Buffer *);
173 #define MON_ISAUTH 0x0004 /* Required for Authentication */
174 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
175 #define MON_ONCE 0x0010 /* Disable after calling */
177 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
179 #define MON_PERMIT 0x1000 /* Request is permitted */
181 struct mon_table mon_dispatch_proto20[] = {
182 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
183 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
184 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
185 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
186 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
187 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
189 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
190 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
191 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
192 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
193 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
194 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
196 #ifdef SSH_AUDIT_EVENTS
197 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
200 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
201 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
204 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
205 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
207 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
208 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
210 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
211 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
212 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
213 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
218 struct mon_table mon_dispatch_postauth20[] = {
219 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
220 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
221 {MONITOR_REQ_PTY, 0, mm_answer_pty},
222 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
223 {MONITOR_REQ_TERM, 0, mm_answer_term},
224 #ifdef SSH_AUDIT_EVENTS
225 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
226 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
231 struct mon_table mon_dispatch_proto15[] = {
232 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
233 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
234 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
235 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
236 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
237 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
238 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
239 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
241 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
242 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
245 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
246 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
249 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
250 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
251 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
252 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
253 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
254 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
256 #ifdef SSH_AUDIT_EVENTS
257 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
262 struct mon_table mon_dispatch_postauth15[] = {
263 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
264 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
265 {MONITOR_REQ_TERM, 0, mm_answer_term},
266 #ifdef SSH_AUDIT_EVENTS
267 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
268 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
273 struct mon_table *mon_dispatch;
275 /* Specifies if a certain message is allowed at the moment */
278 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
280 while (ent->f != NULL) {
281 if (ent->type == type) {
282 ent->flags &= ~MON_PERMIT;
283 ent->flags |= permit ? MON_PERMIT : 0;
291 monitor_permit_authentications(int permit)
293 struct mon_table *ent = mon_dispatch;
295 while (ent->f != NULL) {
296 if (ent->flags & MON_AUTH) {
297 ent->flags &= ~MON_PERMIT;
298 ent->flags |= permit ? MON_PERMIT : 0;
305 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
307 struct mon_table *ent;
308 int authenticated = 0;
310 debug3("preauth child monitor started");
312 authctxt = _authctxt;
313 memset(authctxt, 0, sizeof(*authctxt));
315 authctxt->loginmsg = &loginmsg;
318 mon_dispatch = mon_dispatch_proto20;
320 /* Permit requests for moduli and signatures */
321 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
322 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
324 mon_dispatch = mon_dispatch_proto15;
326 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
329 /* The first few requests do not require asynchronous access */
330 while (!authenticated) {
331 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
333 if (!(ent->flags & MON_AUTHDECIDE))
334 fatal("%s: unexpected authentication from %d",
335 __func__, ent->type);
336 if (authctxt->pw->pw_uid == 0 &&
337 !auth_root_allowed(auth_method))
340 /* PAM needs to perform account checks after auth */
341 if (options.use_pam && authenticated) {
345 mm_request_receive_expect(pmonitor->m_sendfd,
346 MONITOR_REQ_PAM_ACCOUNT, &m);
347 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
353 if (ent->flags & MON_AUTHDECIDE) {
354 auth_log(authctxt, authenticated, auth_method,
355 compat20 ? " ssh2" : "");
357 authctxt->failures++;
361 if (!authctxt->valid)
362 fatal("%s: authenticated invalid user", __func__);
364 debug("%s: %s has been authenticated by privileged process",
365 __func__, authctxt->user);
367 mm_get_keystate(pmonitor);
371 monitor_set_child_handler(pid_t pid)
373 monitor_child_pid = pid;
377 monitor_child_handler(int sig)
379 kill(monitor_child_pid, sig);
383 monitor_child_postauth(struct monitor *pmonitor)
385 monitor_set_child_handler(pmonitor->m_pid);
386 signal(SIGHUP, &monitor_child_handler);
387 signal(SIGTERM, &monitor_child_handler);
390 mon_dispatch = mon_dispatch_postauth20;
392 /* Permit requests for moduli and signatures */
393 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
394 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
395 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
397 mon_dispatch = mon_dispatch_postauth15;
398 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
401 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
402 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
406 monitor_read(pmonitor, mon_dispatch, NULL);
410 monitor_sync(struct monitor *pmonitor)
412 if (options.compression) {
413 /* The member allocation is not visible, so sync it */
414 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
419 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
420 struct mon_table **pent)
428 mm_request_receive(pmonitor->m_sendfd, &m);
429 type = buffer_get_char(&m);
431 debug3("%s: checking request %d", __func__, type);
433 while (ent->f != NULL) {
434 if (ent->type == type)
439 if (ent->f != NULL) {
440 if (!(ent->flags & MON_PERMIT))
441 fatal("%s: unpermitted request %d", __func__,
443 ret = (*ent->f)(pmonitor->m_sendfd, &m);
446 /* The child may use this request only once, disable it */
447 if (ent->flags & MON_ONCE) {
448 debug2("%s: %d used once, disabling now", __func__,
450 ent->flags &= ~MON_PERMIT;
459 fatal("%s: unsupported request: %d", __func__, type);
465 /* allowed key state */
467 monitor_allowed_key(u_char *blob, u_int bloblen)
469 /* make sure key is allowed */
470 if (key_blob == NULL || key_bloblen != bloblen ||
471 memcmp(key_blob, blob, key_bloblen))
477 monitor_reset_key_state(void)
480 if (key_blob != NULL)
482 if (hostbased_cuser != NULL)
483 xfree(hostbased_cuser);
484 if (hostbased_chost != NULL)
485 xfree(hostbased_chost);
488 key_blobtype = MM_NOKEY;
489 hostbased_cuser = NULL;
490 hostbased_chost = NULL;
494 mm_answer_moduli(int sock, Buffer *m)
499 min = buffer_get_int(m);
500 want = buffer_get_int(m);
501 max = buffer_get_int(m);
503 debug3("%s: got parameters: %d %d %d",
504 __func__, min, want, max);
505 /* We need to check here, too, in case the child got corrupted */
506 if (max < min || want < min || max < want)
507 fatal("%s: bad parameters: %d %d %d",
508 __func__, min, want, max);
512 dh = choose_dh(min, want, max);
514 buffer_put_char(m, 0);
517 /* Send first bignum */
518 buffer_put_char(m, 1);
519 buffer_put_bignum2(m, dh->p);
520 buffer_put_bignum2(m, dh->g);
524 mm_request_send(sock, MONITOR_ANS_MODULI, m);
529 mm_answer_sign(int sock, Buffer *m)
534 u_int siglen, datlen;
537 debug3("%s", __func__);
539 keyid = buffer_get_int(m);
540 p = buffer_get_string(m, &datlen);
543 fatal("%s: data length incorrect: %u", __func__, datlen);
545 /* save session id, it will be passed on the first call */
546 if (session_id2_len == 0) {
547 session_id2_len = datlen;
548 session_id2 = xmalloc(session_id2_len);
549 memcpy(session_id2, p, session_id2_len);
552 if ((key = get_hostkey_by_index(keyid)) == NULL)
553 fatal("%s: no hostkey from index %d", __func__, keyid);
554 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
555 fatal("%s: key_sign failed", __func__);
557 debug3("%s: signature %p(%u)", __func__, signature, siglen);
560 buffer_put_string(m, signature, siglen);
565 mm_request_send(sock, MONITOR_ANS_SIGN, m);
567 /* Turn on permissions for getpwnam */
568 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
573 /* Retrieves the password entry and also checks if the user is permitted */
576 mm_answer_pwnamallow(int sock, Buffer *m)
579 struct passwd *pwent;
582 debug3("%s", __func__);
584 if (authctxt->attempt++ != 0)
585 fatal("%s: multiple attempts for getpwnam", __func__);
587 username = buffer_get_string(m, NULL);
589 pwent = getpwnamallow(username);
591 authctxt->user = xstrdup(username);
592 setproctitle("%s [priv]", pwent ? username : "unknown");
598 buffer_put_char(m, 0);
599 authctxt->pw = fakepw();
604 authctxt->pw = pwent;
607 buffer_put_char(m, 1);
608 buffer_put_string(m, pwent, sizeof(struct passwd));
609 buffer_put_cstring(m, pwent->pw_name);
610 buffer_put_cstring(m, "*");
611 buffer_put_cstring(m, pwent->pw_gecos);
612 #ifdef HAVE_PW_CLASS_IN_PASSWD
613 buffer_put_cstring(m, pwent->pw_class);
615 buffer_put_cstring(m, pwent->pw_dir);
616 buffer_put_cstring(m, pwent->pw_shell);
619 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
620 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
622 /* For SSHv1 allow authentication now */
624 monitor_permit_authentications(1);
626 /* Allow service/style information on the auth context */
627 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
628 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
633 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
635 #ifdef SSH_AUDIT_EVENTS
636 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
642 int mm_answer_auth2_read_banner(int sock, Buffer *m)
647 banner = auth2_read_banner();
648 buffer_put_cstring(m, banner != NULL ? banner : "");
649 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
658 mm_answer_authserv(int sock, Buffer *m)
660 monitor_permit_authentications(1);
662 authctxt->service = buffer_get_string(m, NULL);
663 authctxt->style = buffer_get_string(m, NULL);
664 debug3("%s: service=%s, style=%s",
665 __func__, authctxt->service, authctxt->style);
667 if (strlen(authctxt->style) == 0) {
668 xfree(authctxt->style);
669 authctxt->style = NULL;
676 mm_answer_authpassword(int sock, Buffer *m)
678 static int call_count;
683 passwd = buffer_get_string(m, &plen);
684 /* Only authenticate if the context is valid */
685 authenticated = options.password_authentication &&
686 auth_password(authctxt, passwd);
687 memset(passwd, 0, strlen(passwd));
691 buffer_put_int(m, authenticated);
693 debug3("%s: sending result %d", __func__, authenticated);
694 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
697 if (plen == 0 && call_count == 1)
698 auth_method = "none";
700 auth_method = "password";
702 /* Causes monitor loop to terminate if authenticated */
703 return (authenticated);
708 mm_answer_bsdauthquery(int sock, Buffer *m)
710 char *name, *infotxt;
716 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
717 &prompts, &echo_on) < 0 ? 0 : 1;
720 buffer_put_int(m, success);
722 buffer_put_cstring(m, prompts[0]);
724 debug3("%s: sending challenge success: %u", __func__, success);
725 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
738 mm_answer_bsdauthrespond(int sock, Buffer *m)
743 if (authctxt->as == 0)
744 fatal("%s: no bsd auth session", __func__);
746 response = buffer_get_string(m, NULL);
747 authok = options.challenge_response_authentication &&
748 auth_userresponse(authctxt->as, response, 0);
750 debug3("%s: <%s> = <%d>", __func__, response, authok);
754 buffer_put_int(m, authok);
756 debug3("%s: sending authenticated: %d", __func__, authok);
757 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
759 auth_method = "bsdauth";
761 return (authok != 0);
767 mm_answer_skeyquery(int sock, Buffer *m)
770 char challenge[1024];
773 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
774 sizeof(challenge)) < 0 ? 0 : 1;
777 buffer_put_int(m, success);
779 buffer_put_cstring(m, challenge);
781 debug3("%s: sending challenge success: %u", __func__, success);
782 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
788 mm_answer_skeyrespond(int sock, Buffer *m)
793 response = buffer_get_string(m, NULL);
795 authok = (options.challenge_response_authentication &&
797 skey_haskey(authctxt->pw->pw_name) == 0 &&
798 skey_passcheck(authctxt->pw->pw_name, response) != -1);
803 buffer_put_int(m, authok);
805 debug3("%s: sending authenticated: %d", __func__, authok);
806 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
808 auth_method = "skey";
810 return (authok != 0);
816 mm_answer_pam_start(int sock, Buffer *m)
818 if (!options.use_pam)
819 fatal("UsePAM not set, but ended up in %s anyway", __func__);
823 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
829 mm_answer_pam_account(int sock, Buffer *m)
833 if (!options.use_pam)
834 fatal("UsePAM not set, but ended up in %s anyway", __func__);
836 ret = do_pam_account();
838 buffer_put_int(m, ret);
839 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
841 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
846 static void *sshpam_ctxt, *sshpam_authok;
847 extern KbdintDevice sshpam_device;
850 mm_answer_pam_init_ctx(int sock, Buffer *m)
853 debug3("%s", __func__);
854 authctxt->user = buffer_get_string(m, NULL);
855 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
856 sshpam_authok = NULL;
858 if (sshpam_ctxt != NULL) {
859 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
860 buffer_put_int(m, 1);
862 buffer_put_int(m, 0);
864 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
869 mm_answer_pam_query(int sock, Buffer *m)
871 char *name, *info, **prompts;
872 u_int i, num, *echo_on;
875 debug3("%s", __func__);
876 sshpam_authok = NULL;
877 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
878 if (ret == 0 && num == 0)
879 sshpam_authok = sshpam_ctxt;
880 if (num > 1 || name == NULL || info == NULL)
883 buffer_put_int(m, ret);
884 buffer_put_cstring(m, name);
886 buffer_put_cstring(m, info);
888 buffer_put_int(m, num);
889 for (i = 0; i < num; ++i) {
890 buffer_put_cstring(m, prompts[i]);
892 buffer_put_int(m, echo_on[i]);
898 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
903 mm_answer_pam_respond(int sock, Buffer *m)
909 debug3("%s", __func__);
910 sshpam_authok = NULL;
911 num = buffer_get_int(m);
913 resp = xmalloc(num * sizeof(char *));
914 for (i = 0; i < num; ++i)
915 resp[i] = buffer_get_string(m, NULL);
916 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
917 for (i = 0; i < num; ++i)
921 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
924 buffer_put_int(m, ret);
925 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
926 auth_method = "keyboard-interactive/pam";
928 sshpam_authok = sshpam_ctxt;
933 mm_answer_pam_free_ctx(int sock, Buffer *m)
936 debug3("%s", __func__);
937 (sshpam_device.free_ctx)(sshpam_ctxt);
939 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
940 return (sshpam_authok == sshpam_ctxt);
945 mm_append_debug(Buffer *m)
947 if (auth_debug_init && buffer_len(&auth_debug)) {
948 debug3("%s: Appending debug messages for child", __func__);
949 buffer_append(m, buffer_ptr(&auth_debug),
950 buffer_len(&auth_debug));
951 buffer_clear(&auth_debug);
956 mm_answer_keyallowed(int sock, Buffer *m)
962 enum mm_keytype type = 0;
965 debug3("%s entering", __func__);
967 type = buffer_get_int(m);
968 cuser = buffer_get_string(m, NULL);
969 chost = buffer_get_string(m, NULL);
970 blob = buffer_get_string(m, &bloblen);
972 key = key_from_blob(blob, bloblen);
974 if ((compat20 && type == MM_RSAHOSTKEY) ||
975 (!compat20 && type != MM_RSAHOSTKEY))
976 fatal("%s: key type and protocol mismatch", __func__);
978 debug3("%s: key_from_blob: %p", __func__, key);
980 if (key != NULL && authctxt->valid) {
983 allowed = options.pubkey_authentication &&
984 user_key_allowed(authctxt->pw, key);
987 allowed = options.hostbased_authentication &&
988 hostbased_key_allowed(authctxt->pw,
992 key->type = KEY_RSA1; /* XXX */
993 allowed = options.rhosts_rsa_authentication &&
994 auth_rhosts_rsa_key_allowed(authctxt->pw,
998 fatal("%s: unknown key type %d", __func__, type);
1005 /* clear temporarily storage (used by verify) */
1006 monitor_reset_key_state();
1009 /* Save temporarily for comparison in verify */
1011 key_bloblen = bloblen;
1012 key_blobtype = type;
1013 hostbased_cuser = cuser;
1014 hostbased_chost = chost;
1017 debug3("%s: key %p is %s",
1018 __func__, key, allowed ? "allowed" : "disallowed");
1021 buffer_put_int(m, allowed);
1022 buffer_put_int(m, forced_command != NULL);
1026 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1028 if (type == MM_RSAHOSTKEY)
1029 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1035 monitor_valid_userblob(u_char *data, u_int datalen)
1043 buffer_append(&b, data, datalen);
1045 if (datafellows & SSH_OLD_SESSIONID) {
1047 len = buffer_len(&b);
1048 if ((session_id2 == NULL) ||
1049 (len < session_id2_len) ||
1050 (memcmp(p, session_id2, session_id2_len) != 0))
1052 buffer_consume(&b, session_id2_len);
1054 p = buffer_get_string(&b, &len);
1055 if ((session_id2 == NULL) ||
1056 (len != session_id2_len) ||
1057 (memcmp(p, session_id2, session_id2_len) != 0))
1061 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1063 p = buffer_get_string(&b, NULL);
1064 if (strcmp(authctxt->user, p) != 0) {
1065 logit("wrong user name passed to monitor: expected %s != %.100s",
1070 buffer_skip_string(&b);
1071 if (datafellows & SSH_BUG_PKAUTH) {
1072 if (!buffer_get_char(&b))
1075 p = buffer_get_string(&b, NULL);
1076 if (strcmp("publickey", p) != 0)
1079 if (!buffer_get_char(&b))
1081 buffer_skip_string(&b);
1083 buffer_skip_string(&b);
1084 if (buffer_len(&b) != 0)
1091 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1100 buffer_append(&b, data, datalen);
1102 p = buffer_get_string(&b, &len);
1103 if ((session_id2 == NULL) ||
1104 (len != session_id2_len) ||
1105 (memcmp(p, session_id2, session_id2_len) != 0))
1109 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1111 p = buffer_get_string(&b, NULL);
1112 if (strcmp(authctxt->user, p) != 0) {
1113 logit("wrong user name passed to monitor: expected %s != %.100s",
1118 buffer_skip_string(&b); /* service */
1119 p = buffer_get_string(&b, NULL);
1120 if (strcmp(p, "hostbased") != 0)
1123 buffer_skip_string(&b); /* pkalg */
1124 buffer_skip_string(&b); /* pkblob */
1126 /* verify client host, strip trailing dot if necessary */
1127 p = buffer_get_string(&b, NULL);
1128 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1130 if (strcmp(p, chost) != 0)
1134 /* verify client user */
1135 p = buffer_get_string(&b, NULL);
1136 if (strcmp(p, cuser) != 0)
1140 if (buffer_len(&b) != 0)
1147 mm_answer_keyverify(int sock, Buffer *m)
1150 u_char *signature, *data, *blob;
1151 u_int signaturelen, datalen, bloblen;
1155 blob = buffer_get_string(m, &bloblen);
1156 signature = buffer_get_string(m, &signaturelen);
1157 data = buffer_get_string(m, &datalen);
1159 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1160 !monitor_allowed_key(blob, bloblen))
1161 fatal("%s: bad key, not previously allowed", __func__);
1163 key = key_from_blob(blob, bloblen);
1165 fatal("%s: bad public key blob", __func__);
1167 switch (key_blobtype) {
1169 valid_data = monitor_valid_userblob(data, datalen);
1172 valid_data = monitor_valid_hostbasedblob(data, datalen,
1173 hostbased_cuser, hostbased_chost);
1180 fatal("%s: bad signature data blob", __func__);
1182 verified = key_verify(key, signature, signaturelen, data, datalen);
1183 debug3("%s: key %p signature %s",
1184 __func__, key, verified ? "verified" : "unverified");
1191 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1193 monitor_reset_key_state();
1196 buffer_put_int(m, verified);
1197 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1203 mm_record_login(Session *s, struct passwd *pw)
1206 struct sockaddr_storage from;
1209 * Get IP address of client. If the connection is not a socket, let
1210 * the address be 0.0.0.0.
1212 memset(&from, 0, sizeof(from));
1213 fromlen = sizeof(from);
1214 if (packet_connection_is_on_socket()) {
1215 if (getpeername(packet_get_connection_in(),
1216 (struct sockaddr *) & from, &fromlen) < 0) {
1217 debug("getpeername: %.100s", strerror(errno));
1221 /* Record that there was a login on that tty from the remote host. */
1222 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1223 get_remote_name_or_ip(utmp_len, options.use_dns),
1224 (struct sockaddr *)&from, fromlen);
1228 mm_session_close(Session *s)
1230 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1231 if (s->ttyfd != -1) {
1232 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1233 session_pty_cleanup2(s);
1239 mm_answer_pty(int sock, Buffer *m)
1241 extern struct monitor *pmonitor;
1245 debug3("%s entering", __func__);
1251 s->authctxt = authctxt;
1252 s->pw = authctxt->pw;
1253 s->pid = pmonitor->m_pid;
1254 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1257 pty_setowner(authctxt->pw, s->tty);
1259 buffer_put_int(m, 1);
1260 buffer_put_cstring(m, s->tty);
1262 /* We need to trick ttyslot */
1263 if (dup2(s->ttyfd, 0) == -1)
1264 fatal("%s: dup2", __func__);
1266 mm_record_login(s, authctxt->pw);
1268 /* Now we can close the file descriptor again */
1271 /* send messages generated by record_login */
1272 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1273 buffer_clear(&loginmsg);
1275 mm_request_send(sock, MONITOR_ANS_PTY, m);
1277 mm_send_fd(sock, s->ptyfd);
1278 mm_send_fd(sock, s->ttyfd);
1280 /* make sure nothing uses fd 0 */
1281 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1282 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1284 error("%s: fd0 %d != 0", __func__, fd0);
1286 /* slave is not needed */
1288 s->ttyfd = s->ptyfd;
1289 /* no need to dup() because nobody closes ptyfd */
1290 s->ptymaster = s->ptyfd;
1292 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1298 mm_session_close(s);
1299 buffer_put_int(m, 0);
1300 mm_request_send(sock, MONITOR_ANS_PTY, m);
1305 mm_answer_pty_cleanup(int sock, Buffer *m)
1310 debug3("%s entering", __func__);
1312 tty = buffer_get_string(m, NULL);
1313 if ((s = session_by_tty(tty)) != NULL)
1314 mm_session_close(s);
1321 mm_answer_sesskey(int sock, Buffer *m)
1326 /* Turn off permissions */
1327 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1329 if ((p = BN_new()) == NULL)
1330 fatal("%s: BN_new", __func__);
1332 buffer_get_bignum2(m, p);
1334 rsafail = ssh1_session_key(p);
1337 buffer_put_int(m, rsafail);
1338 buffer_put_bignum2(m, p);
1342 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1344 /* Turn on permissions for sessid passing */
1345 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1351 mm_answer_sessid(int sock, Buffer *m)
1355 debug3("%s entering", __func__);
1357 if (buffer_len(m) != 16)
1358 fatal("%s: bad ssh1 session id", __func__);
1359 for (i = 0; i < 16; i++)
1360 session_id[i] = buffer_get_char(m);
1362 /* Turn on permissions for getpwnam */
1363 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1369 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1373 u_char *blob = NULL;
1377 debug3("%s entering", __func__);
1379 if (options.rsa_authentication && authctxt->valid) {
1380 if ((client_n = BN_new()) == NULL)
1381 fatal("%s: BN_new", __func__);
1382 buffer_get_bignum2(m, client_n);
1383 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1384 BN_clear_free(client_n);
1387 buffer_put_int(m, allowed);
1388 buffer_put_int(m, forced_command != NULL);
1390 /* clear temporarily storage (used by generate challenge) */
1391 monitor_reset_key_state();
1393 if (allowed && key != NULL) {
1394 key->type = KEY_RSA; /* cheat for key_to_blob */
1395 if (key_to_blob(key, &blob, &blen) == 0)
1396 fatal("%s: key_to_blob failed", __func__);
1397 buffer_put_string(m, blob, blen);
1399 /* Save temporarily for comparison in verify */
1402 key_blobtype = MM_RSAUSERKEY;
1409 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1411 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1412 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1417 mm_answer_rsa_challenge(int sock, Buffer *m)
1423 debug3("%s entering", __func__);
1425 if (!authctxt->valid)
1426 fatal("%s: authctxt not valid", __func__);
1427 blob = buffer_get_string(m, &blen);
1428 if (!monitor_allowed_key(blob, blen))
1429 fatal("%s: bad key, not previously allowed", __func__);
1430 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1431 fatal("%s: key type mismatch", __func__);
1432 if ((key = key_from_blob(blob, blen)) == NULL)
1433 fatal("%s: received bad key", __func__);
1436 BN_clear_free(ssh1_challenge);
1437 ssh1_challenge = auth_rsa_generate_challenge(key);
1440 buffer_put_bignum2(m, ssh1_challenge);
1442 debug3("%s sending reply", __func__);
1443 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1445 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1453 mm_answer_rsa_response(int sock, Buffer *m)
1456 u_char *blob, *response;
1460 debug3("%s entering", __func__);
1462 if (!authctxt->valid)
1463 fatal("%s: authctxt not valid", __func__);
1464 if (ssh1_challenge == NULL)
1465 fatal("%s: no ssh1_challenge", __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: %d", __func__, key_blobtype);
1472 if ((key = key_from_blob(blob, blen)) == NULL)
1473 fatal("%s: received bad key", __func__);
1474 response = buffer_get_string(m, &len);
1476 fatal("%s: received bad response to challenge", __func__);
1477 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1483 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1486 BN_clear_free(ssh1_challenge);
1487 ssh1_challenge = NULL;
1488 monitor_reset_key_state();
1491 buffer_put_int(m, success);
1492 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1498 mm_answer_term(int sock, Buffer *req)
1500 extern struct monitor *pmonitor;
1503 debug3("%s: tearing down sessions", __func__);
1505 /* The child is terminating */
1506 session_destroy_all(&mm_session_close);
1508 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1512 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1514 /* Terminate process */
1518 #ifdef SSH_AUDIT_EVENTS
1519 /* Report that an audit event occurred */
1521 mm_answer_audit_event(int socket, Buffer *m)
1523 ssh_audit_event_t event;
1525 debug3("%s entering", __func__);
1527 event = buffer_get_int(m);
1529 case SSH_AUTH_FAIL_PUBKEY:
1530 case SSH_AUTH_FAIL_HOSTBASED:
1531 case SSH_AUTH_FAIL_GSSAPI:
1532 case SSH_LOGIN_EXCEED_MAXTRIES:
1533 case SSH_LOGIN_ROOT_DENIED:
1534 case SSH_CONNECTION_CLOSE:
1535 case SSH_INVALID_USER:
1539 fatal("Audit event type %d not permitted", event);
1546 mm_answer_audit_command(int socket, Buffer *m)
1551 debug3("%s entering", __func__);
1552 cmd = buffer_get_string(m, &len);
1553 /* sanity check command, if so how? */
1554 audit_run_command(cmd);
1558 #endif /* SSH_AUDIT_EVENTS */
1561 monitor_apply_keystate(struct monitor *pmonitor)
1564 set_newkeys(MODE_IN);
1565 set_newkeys(MODE_OUT);
1567 packet_set_protocol_flags(child_state.ssh1protoflags);
1568 packet_set_encryption_key(child_state.ssh1key,
1569 child_state.ssh1keylen, child_state.ssh1cipher);
1570 xfree(child_state.ssh1key);
1573 /* for rc4 and other stateful ciphers */
1574 packet_set_keycontext(MODE_OUT, child_state.keyout);
1575 xfree(child_state.keyout);
1576 packet_set_keycontext(MODE_IN, child_state.keyin);
1577 xfree(child_state.keyin);
1580 packet_set_iv(MODE_OUT, child_state.ivout);
1581 xfree(child_state.ivout);
1582 packet_set_iv(MODE_IN, child_state.ivin);
1583 xfree(child_state.ivin);
1586 memcpy(&incoming_stream, &child_state.incoming,
1587 sizeof(incoming_stream));
1588 memcpy(&outgoing_stream, &child_state.outgoing,
1589 sizeof(outgoing_stream));
1591 /* Update with new address */
1592 if (options.compression)
1593 mm_init_compression(pmonitor->m_zlib);
1595 /* Network I/O buffers */
1596 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1597 buffer_clear(&input);
1598 buffer_append(&input, child_state.input, child_state.ilen);
1599 memset(child_state.input, 0, child_state.ilen);
1600 xfree(child_state.input);
1602 buffer_clear(&output);
1603 buffer_append(&output, child_state.output, child_state.olen);
1604 memset(child_state.output, 0, child_state.olen);
1605 xfree(child_state.output);
1609 mm_get_kex(Buffer *m)
1615 kex = xmalloc(sizeof(*kex));
1616 memset(kex, 0, sizeof(*kex));
1617 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1618 if ((session_id2 == NULL) ||
1619 (kex->session_id_len != session_id2_len) ||
1620 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1621 fatal("mm_get_get: internal error: bad session id");
1622 kex->we_need = buffer_get_int(m);
1623 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1624 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1625 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1627 kex->hostkey_type = buffer_get_int(m);
1628 kex->kex_type = buffer_get_int(m);
1629 blob = buffer_get_string(m, &bloblen);
1630 buffer_init(&kex->my);
1631 buffer_append(&kex->my, blob, bloblen);
1633 blob = buffer_get_string(m, &bloblen);
1634 buffer_init(&kex->peer);
1635 buffer_append(&kex->peer, blob, bloblen);
1638 kex->flags = buffer_get_int(m);
1639 kex->client_version_string = buffer_get_string(m, NULL);
1640 kex->server_version_string = buffer_get_string(m, NULL);
1641 kex->load_host_key=&get_hostkey_by_type;
1642 kex->host_key_index=&get_hostkey_index;
1647 /* This function requries careful sanity checking */
1650 mm_get_keystate(struct monitor *pmonitor)
1654 u_int bloblen, plen;
1655 u_int32_t seqnr, packets;
1658 debug3("%s: Waiting for new keys", __func__);
1661 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1663 child_state.ssh1protoflags = buffer_get_int(&m);
1664 child_state.ssh1cipher = buffer_get_int(&m);
1665 child_state.ssh1key = buffer_get_string(&m,
1666 &child_state.ssh1keylen);
1667 child_state.ivout = buffer_get_string(&m,
1668 &child_state.ivoutlen);
1669 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1672 /* Get the Kex for rekeying */
1673 *pmonitor->m_pkex = mm_get_kex(&m);
1676 blob = buffer_get_string(&m, &bloblen);
1677 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1680 debug3("%s: Waiting for second key", __func__);
1681 blob = buffer_get_string(&m, &bloblen);
1682 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1685 /* Now get sequence numbers for the packets */
1686 seqnr = buffer_get_int(&m);
1687 blocks = buffer_get_int64(&m);
1688 packets = buffer_get_int(&m);
1689 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1690 seqnr = buffer_get_int(&m);
1691 blocks = buffer_get_int64(&m);
1692 packets = buffer_get_int(&m);
1693 packet_set_state(MODE_IN, seqnr, blocks, packets);
1696 /* Get the key context */
1697 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1698 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1700 debug3("%s: Getting compression state", __func__);
1701 /* Get compression state */
1702 p = buffer_get_string(&m, &plen);
1703 if (plen != sizeof(child_state.outgoing))
1704 fatal("%s: bad request size", __func__);
1705 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1708 p = buffer_get_string(&m, &plen);
1709 if (plen != sizeof(child_state.incoming))
1710 fatal("%s: bad request size", __func__);
1711 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1714 /* Network I/O buffers */
1715 debug3("%s: Getting Network I/O buffers", __func__);
1716 child_state.input = buffer_get_string(&m, &child_state.ilen);
1717 child_state.output = buffer_get_string(&m, &child_state.olen);
1723 /* Allocation functions for zlib */
1725 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1727 size_t len = (size_t) size * ncount;
1730 if (len == 0 || ncount > SIZE_T_MAX / size)
1731 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1733 address = mm_malloc(mm, len);
1739 mm_zfree(struct mm_master *mm, void *address)
1741 mm_free(mm, address);
1745 mm_init_compression(struct mm_master *mm)
1747 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1748 outgoing_stream.zfree = (free_func)mm_zfree;
1749 outgoing_stream.opaque = mm;
1751 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1752 incoming_stream.zfree = (free_func)mm_zfree;
1753 incoming_stream.opaque = mm;
1758 #define FD_CLOSEONEXEC(x) do { \
1759 if (fcntl(x, F_SETFD, 1) == -1) \
1760 fatal("fcntl(%d, F_SETFD)", x); \
1764 monitor_socketpair(int *pair)
1766 #ifdef HAVE_SOCKETPAIR
1767 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1768 fatal("%s: socketpair", __func__);
1770 fatal("%s: UsePrivilegeSeparation=yes not supported",
1773 FD_CLOSEONEXEC(pair[0]);
1774 FD_CLOSEONEXEC(pair[1]);
1777 #define MM_MEMSIZE 65536
1782 struct monitor *mon;
1785 mon = xmalloc(sizeof(*mon));
1788 monitor_socketpair(pair);
1790 mon->m_recvfd = pair[0];
1791 mon->m_sendfd = pair[1];
1793 /* Used to share zlib space across processes */
1794 if (options.compression) {
1795 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1796 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1798 /* Compression needs to share state across borders */
1799 mm_init_compression(mon->m_zlib);
1806 monitor_reinit(struct monitor *mon)
1810 monitor_socketpair(pair);
1812 mon->m_recvfd = pair[0];
1813 mon->m_sendfd = pair[1];
1818 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1824 goid.elements = buffer_get_string(m, &len);
1827 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1829 xfree(goid.elements);
1832 buffer_put_int(m, major);
1834 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1836 /* Now we have a context, enable the step */
1837 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1843 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1846 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1847 OM_uint32 major, minor;
1848 OM_uint32 flags = 0; /* GSI needs this */
1851 in.value = buffer_get_string(m, &len);
1853 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1857 buffer_put_int(m, major);
1858 buffer_put_string(m, out.value, out.length);
1859 buffer_put_int(m, flags);
1860 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1862 gss_release_buffer(&minor, &out);
1864 if (major == GSS_S_COMPLETE) {
1865 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1866 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1867 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1873 mm_answer_gss_checkmic(int sock, Buffer *m)
1875 gss_buffer_desc gssbuf, mic;
1879 gssbuf.value = buffer_get_string(m, &len);
1880 gssbuf.length = len;
1881 mic.value = buffer_get_string(m, &len);
1884 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1886 xfree(gssbuf.value);
1890 buffer_put_int(m, ret);
1892 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1894 if (!GSS_ERROR(ret))
1895 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1901 mm_answer_gss_userok(int sock, Buffer *m)
1905 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1908 buffer_put_int(m, authenticated);
1910 debug3("%s: sending result %d", __func__, authenticated);
1911 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1913 auth_method = "gssapi-with-mic";
1915 /* Monitor loop will terminate if authenticated */
1916 return (authenticated);