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.67 2006/02/10 01:44:26 stevesk Exp $");
30 #include <sys/types.h>
39 #include <openssl/dh.h>
45 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
48 #define TARGET_OS_MAC 1
53 #include "auth-options.h"
62 #include "monitor_mm.h"
63 #include "monitor_wrap.h"
64 #include "monitor_fdpass.h"
74 static Gssctxt *gsscontext = NULL;
78 extern ServerOptions options;
79 extern u_int utmp_len;
80 extern Newkeys *current_keys[];
81 extern z_stream incoming_stream;
82 extern z_stream outgoing_stream;
83 extern u_char session_id[];
84 extern Buffer input, output;
85 extern Buffer auth_debug;
86 extern int auth_debug_init;
87 extern Buffer loginmsg;
89 /* State exported from the child */
112 /* Functions on the monitor that answer unprivileged requests */
114 int mm_answer_moduli(int, Buffer *);
115 int mm_answer_sign(int, Buffer *);
116 int mm_answer_pwnamallow(int, Buffer *);
117 int mm_answer_auth2_read_banner(int, Buffer *);
118 int mm_answer_authserv(int, Buffer *);
119 int mm_answer_authpassword(int, Buffer *);
120 int mm_answer_bsdauthquery(int, Buffer *);
121 int mm_answer_bsdauthrespond(int, Buffer *);
122 int mm_answer_skeyquery(int, Buffer *);
123 int mm_answer_skeyrespond(int, Buffer *);
124 int mm_answer_keyallowed(int, Buffer *);
125 int mm_answer_keyverify(int, Buffer *);
126 int mm_answer_pty(int, Buffer *);
127 int mm_answer_pty_cleanup(int, Buffer *);
128 int mm_answer_term(int, Buffer *);
129 int mm_answer_rsa_keyallowed(int, Buffer *);
130 int mm_answer_rsa_challenge(int, Buffer *);
131 int mm_answer_rsa_response(int, Buffer *);
132 int mm_answer_sesskey(int, Buffer *);
133 int mm_answer_sessid(int, Buffer *);
136 int mm_answer_pam_start(int, Buffer *);
137 int mm_answer_pam_account(int, Buffer *);
138 int mm_answer_pam_init_ctx(int, Buffer *);
139 int mm_answer_pam_query(int, Buffer *);
140 int mm_answer_pam_respond(int, Buffer *);
141 int mm_answer_pam_free_ctx(int, Buffer *);
145 int mm_answer_gss_setup_ctx(int, Buffer *);
146 int mm_answer_gss_accept_ctx(int, Buffer *);
147 int mm_answer_gss_userok(int, Buffer *);
148 int mm_answer_gss_checkmic(int, Buffer *);
151 #ifdef SSH_AUDIT_EVENTS
152 int mm_answer_audit_event(int, Buffer *);
153 int mm_answer_audit_command(int, Buffer *);
156 static Authctxt *authctxt;
157 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
159 /* local state for key verify */
160 static u_char *key_blob = NULL;
161 static u_int key_bloblen = 0;
162 static int key_blobtype = MM_NOKEY;
163 static char *hostbased_cuser = NULL;
164 static char *hostbased_chost = NULL;
165 static char *auth_method = "unknown";
166 static u_int session_id2_len = 0;
167 static u_char *session_id2 = NULL;
168 static pid_t monitor_child_pid;
171 enum monitor_reqtype type;
173 int (*f)(int, Buffer *);
176 #define MON_ISAUTH 0x0004 /* Required for Authentication */
177 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
178 #define MON_ONCE 0x0010 /* Disable after calling */
180 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
182 #define MON_PERMIT 0x1000 /* Request is permitted */
184 struct mon_table mon_dispatch_proto20[] = {
185 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
186 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
187 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
188 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
189 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
190 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
192 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
193 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
194 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
195 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
196 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
197 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
199 #ifdef SSH_AUDIT_EVENTS
200 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
203 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
204 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
207 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
208 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
210 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
211 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
213 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
214 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
215 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
216 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
221 struct mon_table mon_dispatch_postauth20[] = {
222 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
223 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
224 {MONITOR_REQ_PTY, 0, mm_answer_pty},
225 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
226 {MONITOR_REQ_TERM, 0, mm_answer_term},
227 #ifdef SSH_AUDIT_EVENTS
228 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
229 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
234 struct mon_table mon_dispatch_proto15[] = {
235 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
236 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
237 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
238 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
239 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
240 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
241 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
242 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
244 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
245 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
248 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
249 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
252 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
253 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
254 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
255 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
256 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
257 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
259 #ifdef SSH_AUDIT_EVENTS
260 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
265 struct mon_table mon_dispatch_postauth15[] = {
266 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
267 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
268 {MONITOR_REQ_TERM, 0, mm_answer_term},
269 #ifdef SSH_AUDIT_EVENTS
270 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
271 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
276 struct mon_table *mon_dispatch;
278 /* Specifies if a certain message is allowed at the moment */
281 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
283 while (ent->f != NULL) {
284 if (ent->type == type) {
285 ent->flags &= ~MON_PERMIT;
286 ent->flags |= permit ? MON_PERMIT : 0;
294 monitor_permit_authentications(int permit)
296 struct mon_table *ent = mon_dispatch;
298 while (ent->f != NULL) {
299 if (ent->flags & MON_AUTH) {
300 ent->flags &= ~MON_PERMIT;
301 ent->flags |= permit ? MON_PERMIT : 0;
308 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
310 struct mon_table *ent;
311 int authenticated = 0;
313 debug3("preauth child monitor started");
315 authctxt = _authctxt;
316 memset(authctxt, 0, sizeof(*authctxt));
318 authctxt->loginmsg = &loginmsg;
321 mon_dispatch = mon_dispatch_proto20;
323 /* Permit requests for moduli and signatures */
324 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
325 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
327 mon_dispatch = mon_dispatch_proto15;
329 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
332 /* The first few requests do not require asynchronous access */
333 while (!authenticated) {
334 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
336 if (!(ent->flags & MON_AUTHDECIDE))
337 fatal("%s: unexpected authentication from %d",
338 __func__, ent->type);
339 if (authctxt->pw->pw_uid == 0 &&
340 !auth_root_allowed(auth_method))
343 /* PAM needs to perform account checks after auth */
344 if (options.use_pam && authenticated) {
348 mm_request_receive_expect(pmonitor->m_sendfd,
349 MONITOR_REQ_PAM_ACCOUNT, &m);
350 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
356 if (ent->flags & MON_AUTHDECIDE) {
357 auth_log(authctxt, authenticated, auth_method,
358 compat20 ? " ssh2" : "");
360 authctxt->failures++;
364 if (!authctxt->valid)
365 fatal("%s: authenticated invalid user", __func__);
367 debug("%s: %s has been authenticated by privileged process",
368 __func__, authctxt->user);
370 mm_get_keystate(pmonitor);
374 monitor_set_child_handler(pid_t pid)
376 monitor_child_pid = pid;
380 monitor_child_handler(int sig)
382 kill(monitor_child_pid, sig);
386 monitor_child_postauth(struct monitor *pmonitor)
388 monitor_set_child_handler(pmonitor->m_pid);
389 signal(SIGHUP, &monitor_child_handler);
390 signal(SIGTERM, &monitor_child_handler);
393 mon_dispatch = mon_dispatch_postauth20;
395 /* Permit requests for moduli and signatures */
396 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
397 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
398 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
400 mon_dispatch = mon_dispatch_postauth15;
401 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
404 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
405 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
409 monitor_read(pmonitor, mon_dispatch, NULL);
413 monitor_sync(struct monitor *pmonitor)
415 if (options.compression) {
416 /* The member allocation is not visible, so sync it */
417 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
422 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
423 struct mon_table **pent)
431 mm_request_receive(pmonitor->m_sendfd, &m);
432 type = buffer_get_char(&m);
434 debug3("%s: checking request %d", __func__, type);
436 while (ent->f != NULL) {
437 if (ent->type == type)
442 if (ent->f != NULL) {
443 if (!(ent->flags & MON_PERMIT))
444 fatal("%s: unpermitted request %d", __func__,
446 ret = (*ent->f)(pmonitor->m_sendfd, &m);
449 /* The child may use this request only once, disable it */
450 if (ent->flags & MON_ONCE) {
451 debug2("%s: %d used once, disabling now", __func__,
453 ent->flags &= ~MON_PERMIT;
462 fatal("%s: unsupported request: %d", __func__, type);
468 /* allowed key state */
470 monitor_allowed_key(u_char *blob, u_int bloblen)
472 /* make sure key is allowed */
473 if (key_blob == NULL || key_bloblen != bloblen ||
474 memcmp(key_blob, blob, key_bloblen))
480 monitor_reset_key_state(void)
483 if (key_blob != NULL)
485 if (hostbased_cuser != NULL)
486 xfree(hostbased_cuser);
487 if (hostbased_chost != NULL)
488 xfree(hostbased_chost);
491 key_blobtype = MM_NOKEY;
492 hostbased_cuser = NULL;
493 hostbased_chost = NULL;
497 mm_answer_moduli(int sock, Buffer *m)
502 min = buffer_get_int(m);
503 want = buffer_get_int(m);
504 max = buffer_get_int(m);
506 debug3("%s: got parameters: %d %d %d",
507 __func__, min, want, max);
508 /* We need to check here, too, in case the child got corrupted */
509 if (max < min || want < min || max < want)
510 fatal("%s: bad parameters: %d %d %d",
511 __func__, min, want, max);
515 dh = choose_dh(min, want, max);
517 buffer_put_char(m, 0);
520 /* Send first bignum */
521 buffer_put_char(m, 1);
522 buffer_put_bignum2(m, dh->p);
523 buffer_put_bignum2(m, dh->g);
527 mm_request_send(sock, MONITOR_ANS_MODULI, m);
532 mm_answer_sign(int sock, Buffer *m)
537 u_int siglen, datlen;
540 debug3("%s", __func__);
542 keyid = buffer_get_int(m);
543 p = buffer_get_string(m, &datlen);
546 fatal("%s: data length incorrect: %u", __func__, datlen);
548 /* save session id, it will be passed on the first call */
549 if (session_id2_len == 0) {
550 session_id2_len = datlen;
551 session_id2 = xmalloc(session_id2_len);
552 memcpy(session_id2, p, session_id2_len);
555 if ((key = get_hostkey_by_index(keyid)) == NULL)
556 fatal("%s: no hostkey from index %d", __func__, keyid);
557 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
558 fatal("%s: key_sign failed", __func__);
560 debug3("%s: signature %p(%u)", __func__, signature, siglen);
563 buffer_put_string(m, signature, siglen);
568 mm_request_send(sock, MONITOR_ANS_SIGN, m);
570 /* Turn on permissions for getpwnam */
571 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
576 /* Retrieves the password entry and also checks if the user is permitted */
579 mm_answer_pwnamallow(int sock, Buffer *m)
582 struct passwd *pwent;
585 debug3("%s", __func__);
587 if (authctxt->attempt++ != 0)
588 fatal("%s: multiple attempts for getpwnam", __func__);
590 username = buffer_get_string(m, NULL);
592 pwent = getpwnamallow(username);
594 authctxt->user = xstrdup(username);
595 setproctitle("%s [priv]", pwent ? username : "unknown");
601 buffer_put_char(m, 0);
602 authctxt->pw = fakepw();
607 authctxt->pw = pwent;
610 buffer_put_char(m, 1);
611 buffer_put_string(m, pwent, sizeof(struct passwd));
612 buffer_put_cstring(m, pwent->pw_name);
613 buffer_put_cstring(m, "*");
614 buffer_put_cstring(m, pwent->pw_gecos);
615 #ifdef HAVE_PW_CLASS_IN_PASSWD
616 buffer_put_cstring(m, pwent->pw_class);
618 buffer_put_cstring(m, pwent->pw_dir);
619 buffer_put_cstring(m, pwent->pw_shell);
622 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
623 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
625 /* For SSHv1 allow authentication now */
627 monitor_permit_authentications(1);
629 /* Allow service/style information on the auth context */
630 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
631 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
636 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
638 #ifdef SSH_AUDIT_EVENTS
639 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
645 int mm_answer_auth2_read_banner(int sock, Buffer *m)
650 banner = auth2_read_banner();
651 buffer_put_cstring(m, banner != NULL ? banner : "");
652 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
661 mm_answer_authserv(int sock, Buffer *m)
663 monitor_permit_authentications(1);
665 authctxt->service = buffer_get_string(m, NULL);
666 authctxt->style = buffer_get_string(m, NULL);
667 debug3("%s: service=%s, style=%s",
668 __func__, authctxt->service, authctxt->style);
670 if (strlen(authctxt->style) == 0) {
671 xfree(authctxt->style);
672 authctxt->style = NULL;
679 mm_answer_authpassword(int sock, Buffer *m)
681 static int call_count;
686 passwd = buffer_get_string(m, &plen);
687 /* Only authenticate if the context is valid */
688 authenticated = options.password_authentication &&
689 auth_password(authctxt, passwd);
690 memset(passwd, 0, strlen(passwd));
694 buffer_put_int(m, authenticated);
696 debug3("%s: sending result %d", __func__, authenticated);
697 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
700 if (plen == 0 && call_count == 1)
701 auth_method = "none";
703 auth_method = "password";
705 /* Causes monitor loop to terminate if authenticated */
706 return (authenticated);
711 mm_answer_bsdauthquery(int sock, Buffer *m)
713 char *name, *infotxt;
719 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
720 &prompts, &echo_on) < 0 ? 0 : 1;
723 buffer_put_int(m, success);
725 buffer_put_cstring(m, prompts[0]);
727 debug3("%s: sending challenge success: %u", __func__, success);
728 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
741 mm_answer_bsdauthrespond(int sock, Buffer *m)
746 if (authctxt->as == 0)
747 fatal("%s: no bsd auth session", __func__);
749 response = buffer_get_string(m, NULL);
750 authok = options.challenge_response_authentication &&
751 auth_userresponse(authctxt->as, response, 0);
753 debug3("%s: <%s> = <%d>", __func__, response, authok);
757 buffer_put_int(m, authok);
759 debug3("%s: sending authenticated: %d", __func__, authok);
760 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
762 auth_method = "bsdauth";
764 return (authok != 0);
770 mm_answer_skeyquery(int sock, Buffer *m)
773 char challenge[1024];
776 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
777 sizeof(challenge)) < 0 ? 0 : 1;
780 buffer_put_int(m, success);
782 buffer_put_cstring(m, challenge);
784 debug3("%s: sending challenge success: %u", __func__, success);
785 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
791 mm_answer_skeyrespond(int sock, Buffer *m)
796 response = buffer_get_string(m, NULL);
798 authok = (options.challenge_response_authentication &&
800 skey_haskey(authctxt->pw->pw_name) == 0 &&
801 skey_passcheck(authctxt->pw->pw_name, response) != -1);
806 buffer_put_int(m, authok);
808 debug3("%s: sending authenticated: %d", __func__, authok);
809 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
811 auth_method = "skey";
813 return (authok != 0);
819 mm_answer_pam_start(int sock, Buffer *m)
821 if (!options.use_pam)
822 fatal("UsePAM not set, but ended up in %s anyway", __func__);
826 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
832 mm_answer_pam_account(int sock, Buffer *m)
836 if (!options.use_pam)
837 fatal("UsePAM not set, but ended up in %s anyway", __func__);
839 ret = do_pam_account();
841 buffer_put_int(m, ret);
842 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
844 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
849 static void *sshpam_ctxt, *sshpam_authok;
850 extern KbdintDevice sshpam_device;
853 mm_answer_pam_init_ctx(int sock, Buffer *m)
856 debug3("%s", __func__);
857 authctxt->user = buffer_get_string(m, NULL);
858 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
859 sshpam_authok = NULL;
861 if (sshpam_ctxt != NULL) {
862 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
863 buffer_put_int(m, 1);
865 buffer_put_int(m, 0);
867 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
872 mm_answer_pam_query(int sock, Buffer *m)
874 char *name, *info, **prompts;
875 u_int i, num, *echo_on;
878 debug3("%s", __func__);
879 sshpam_authok = NULL;
880 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
881 if (ret == 0 && num == 0)
882 sshpam_authok = sshpam_ctxt;
883 if (num > 1 || name == NULL || info == NULL)
886 buffer_put_int(m, ret);
887 buffer_put_cstring(m, name);
889 buffer_put_cstring(m, info);
891 buffer_put_int(m, num);
892 for (i = 0; i < num; ++i) {
893 buffer_put_cstring(m, prompts[i]);
895 buffer_put_int(m, echo_on[i]);
901 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
906 mm_answer_pam_respond(int sock, Buffer *m)
912 debug3("%s", __func__);
913 sshpam_authok = NULL;
914 num = buffer_get_int(m);
916 resp = xmalloc(num * sizeof(char *));
917 for (i = 0; i < num; ++i)
918 resp[i] = buffer_get_string(m, NULL);
919 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
920 for (i = 0; i < num; ++i)
924 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
927 buffer_put_int(m, ret);
928 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
929 auth_method = "keyboard-interactive/pam";
931 sshpam_authok = sshpam_ctxt;
936 mm_answer_pam_free_ctx(int sock, Buffer *m)
939 debug3("%s", __func__);
940 (sshpam_device.free_ctx)(sshpam_ctxt);
942 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
943 return (sshpam_authok == sshpam_ctxt);
948 mm_append_debug(Buffer *m)
950 if (auth_debug_init && buffer_len(&auth_debug)) {
951 debug3("%s: Appending debug messages for child", __func__);
952 buffer_append(m, buffer_ptr(&auth_debug),
953 buffer_len(&auth_debug));
954 buffer_clear(&auth_debug);
959 mm_answer_keyallowed(int sock, Buffer *m)
965 enum mm_keytype type = 0;
968 debug3("%s entering", __func__);
970 type = buffer_get_int(m);
971 cuser = buffer_get_string(m, NULL);
972 chost = buffer_get_string(m, NULL);
973 blob = buffer_get_string(m, &bloblen);
975 key = key_from_blob(blob, bloblen);
977 if ((compat20 && type == MM_RSAHOSTKEY) ||
978 (!compat20 && type != MM_RSAHOSTKEY))
979 fatal("%s: key type and protocol mismatch", __func__);
981 debug3("%s: key_from_blob: %p", __func__, key);
983 if (key != NULL && authctxt->valid) {
986 allowed = options.pubkey_authentication &&
987 user_key_allowed(authctxt->pw, key);
990 allowed = options.hostbased_authentication &&
991 hostbased_key_allowed(authctxt->pw,
995 key->type = KEY_RSA1; /* XXX */
996 allowed = options.rhosts_rsa_authentication &&
997 auth_rhosts_rsa_key_allowed(authctxt->pw,
1001 fatal("%s: unknown key type %d", __func__, type);
1008 /* clear temporarily storage (used by verify) */
1009 monitor_reset_key_state();
1012 /* Save temporarily for comparison in verify */
1014 key_bloblen = bloblen;
1015 key_blobtype = type;
1016 hostbased_cuser = cuser;
1017 hostbased_chost = chost;
1020 debug3("%s: key %p is %s",
1021 __func__, key, allowed ? "allowed" : "disallowed");
1024 buffer_put_int(m, allowed);
1025 buffer_put_int(m, forced_command != NULL);
1029 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1031 if (type == MM_RSAHOSTKEY)
1032 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1038 monitor_valid_userblob(u_char *data, u_int datalen)
1046 buffer_append(&b, data, datalen);
1048 if (datafellows & SSH_OLD_SESSIONID) {
1050 len = buffer_len(&b);
1051 if ((session_id2 == NULL) ||
1052 (len < session_id2_len) ||
1053 (memcmp(p, session_id2, session_id2_len) != 0))
1055 buffer_consume(&b, session_id2_len);
1057 p = buffer_get_string(&b, &len);
1058 if ((session_id2 == NULL) ||
1059 (len != session_id2_len) ||
1060 (memcmp(p, session_id2, session_id2_len) != 0))
1064 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1066 p = buffer_get_string(&b, NULL);
1067 if (strcmp(authctxt->user, p) != 0) {
1068 logit("wrong user name passed to monitor: expected %s != %.100s",
1073 buffer_skip_string(&b);
1074 if (datafellows & SSH_BUG_PKAUTH) {
1075 if (!buffer_get_char(&b))
1078 p = buffer_get_string(&b, NULL);
1079 if (strcmp("publickey", p) != 0)
1082 if (!buffer_get_char(&b))
1084 buffer_skip_string(&b);
1086 buffer_skip_string(&b);
1087 if (buffer_len(&b) != 0)
1094 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1103 buffer_append(&b, data, datalen);
1105 p = buffer_get_string(&b, &len);
1106 if ((session_id2 == NULL) ||
1107 (len != session_id2_len) ||
1108 (memcmp(p, session_id2, session_id2_len) != 0))
1112 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1114 p = buffer_get_string(&b, NULL);
1115 if (strcmp(authctxt->user, p) != 0) {
1116 logit("wrong user name passed to monitor: expected %s != %.100s",
1121 buffer_skip_string(&b); /* service */
1122 p = buffer_get_string(&b, NULL);
1123 if (strcmp(p, "hostbased") != 0)
1126 buffer_skip_string(&b); /* pkalg */
1127 buffer_skip_string(&b); /* pkblob */
1129 /* verify client host, strip trailing dot if necessary */
1130 p = buffer_get_string(&b, NULL);
1131 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1133 if (strcmp(p, chost) != 0)
1137 /* verify client user */
1138 p = buffer_get_string(&b, NULL);
1139 if (strcmp(p, cuser) != 0)
1143 if (buffer_len(&b) != 0)
1150 mm_answer_keyverify(int sock, Buffer *m)
1153 u_char *signature, *data, *blob;
1154 u_int signaturelen, datalen, bloblen;
1158 blob = buffer_get_string(m, &bloblen);
1159 signature = buffer_get_string(m, &signaturelen);
1160 data = buffer_get_string(m, &datalen);
1162 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1163 !monitor_allowed_key(blob, bloblen))
1164 fatal("%s: bad key, not previously allowed", __func__);
1166 key = key_from_blob(blob, bloblen);
1168 fatal("%s: bad public key blob", __func__);
1170 switch (key_blobtype) {
1172 valid_data = monitor_valid_userblob(data, datalen);
1175 valid_data = monitor_valid_hostbasedblob(data, datalen,
1176 hostbased_cuser, hostbased_chost);
1183 fatal("%s: bad signature data blob", __func__);
1185 verified = key_verify(key, signature, signaturelen, data, datalen);
1186 debug3("%s: key %p signature %s",
1187 __func__, key, verified ? "verified" : "unverified");
1194 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1196 monitor_reset_key_state();
1199 buffer_put_int(m, verified);
1200 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1206 mm_record_login(Session *s, struct passwd *pw)
1209 struct sockaddr_storage from;
1212 * Get IP address of client. If the connection is not a socket, let
1213 * the address be 0.0.0.0.
1215 memset(&from, 0, sizeof(from));
1216 fromlen = sizeof(from);
1217 if (packet_connection_is_on_socket()) {
1218 if (getpeername(packet_get_connection_in(),
1219 (struct sockaddr *) & from, &fromlen) < 0) {
1220 debug("getpeername: %.100s", strerror(errno));
1224 /* Record that there was a login on that tty from the remote host. */
1225 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1226 get_remote_name_or_ip(utmp_len, options.use_dns),
1227 (struct sockaddr *)&from, fromlen);
1231 mm_session_close(Session *s)
1233 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1234 if (s->ttyfd != -1) {
1235 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1236 session_pty_cleanup2(s);
1242 mm_answer_pty(int sock, Buffer *m)
1244 extern struct monitor *pmonitor;
1248 debug3("%s entering", __func__);
1254 s->authctxt = authctxt;
1255 s->pw = authctxt->pw;
1256 s->pid = pmonitor->m_pid;
1257 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1260 pty_setowner(authctxt->pw, s->tty);
1262 buffer_put_int(m, 1);
1263 buffer_put_cstring(m, s->tty);
1265 /* We need to trick ttyslot */
1266 if (dup2(s->ttyfd, 0) == -1)
1267 fatal("%s: dup2", __func__);
1269 mm_record_login(s, authctxt->pw);
1271 /* Now we can close the file descriptor again */
1274 /* send messages generated by record_login */
1275 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1276 buffer_clear(&loginmsg);
1278 mm_request_send(sock, MONITOR_ANS_PTY, m);
1280 mm_send_fd(sock, s->ptyfd);
1281 mm_send_fd(sock, s->ttyfd);
1283 /* make sure nothing uses fd 0 */
1284 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1285 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1287 error("%s: fd0 %d != 0", __func__, fd0);
1289 /* slave is not needed */
1291 s->ttyfd = s->ptyfd;
1292 /* no need to dup() because nobody closes ptyfd */
1293 s->ptymaster = s->ptyfd;
1295 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1301 mm_session_close(s);
1302 buffer_put_int(m, 0);
1303 mm_request_send(sock, MONITOR_ANS_PTY, m);
1308 mm_answer_pty_cleanup(int sock, Buffer *m)
1313 debug3("%s entering", __func__);
1315 tty = buffer_get_string(m, NULL);
1316 if ((s = session_by_tty(tty)) != NULL)
1317 mm_session_close(s);
1324 mm_answer_sesskey(int sock, Buffer *m)
1329 /* Turn off permissions */
1330 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1332 if ((p = BN_new()) == NULL)
1333 fatal("%s: BN_new", __func__);
1335 buffer_get_bignum2(m, p);
1337 rsafail = ssh1_session_key(p);
1340 buffer_put_int(m, rsafail);
1341 buffer_put_bignum2(m, p);
1345 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1347 /* Turn on permissions for sessid passing */
1348 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1354 mm_answer_sessid(int sock, Buffer *m)
1358 debug3("%s entering", __func__);
1360 if (buffer_len(m) != 16)
1361 fatal("%s: bad ssh1 session id", __func__);
1362 for (i = 0; i < 16; i++)
1363 session_id[i] = buffer_get_char(m);
1365 /* Turn on permissions for getpwnam */
1366 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1372 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1376 u_char *blob = NULL;
1380 debug3("%s entering", __func__);
1382 if (options.rsa_authentication && authctxt->valid) {
1383 if ((client_n = BN_new()) == NULL)
1384 fatal("%s: BN_new", __func__);
1385 buffer_get_bignum2(m, client_n);
1386 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1387 BN_clear_free(client_n);
1390 buffer_put_int(m, allowed);
1391 buffer_put_int(m, forced_command != NULL);
1393 /* clear temporarily storage (used by generate challenge) */
1394 monitor_reset_key_state();
1396 if (allowed && key != NULL) {
1397 key->type = KEY_RSA; /* cheat for key_to_blob */
1398 if (key_to_blob(key, &blob, &blen) == 0)
1399 fatal("%s: key_to_blob failed", __func__);
1400 buffer_put_string(m, blob, blen);
1402 /* Save temporarily for comparison in verify */
1405 key_blobtype = MM_RSAUSERKEY;
1412 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1414 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1415 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1420 mm_answer_rsa_challenge(int sock, Buffer *m)
1426 debug3("%s entering", __func__);
1428 if (!authctxt->valid)
1429 fatal("%s: authctxt not valid", __func__);
1430 blob = buffer_get_string(m, &blen);
1431 if (!monitor_allowed_key(blob, blen))
1432 fatal("%s: bad key, not previously allowed", __func__);
1433 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1434 fatal("%s: key type mismatch", __func__);
1435 if ((key = key_from_blob(blob, blen)) == NULL)
1436 fatal("%s: received bad key", __func__);
1439 BN_clear_free(ssh1_challenge);
1440 ssh1_challenge = auth_rsa_generate_challenge(key);
1443 buffer_put_bignum2(m, ssh1_challenge);
1445 debug3("%s sending reply", __func__);
1446 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1448 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1456 mm_answer_rsa_response(int sock, Buffer *m)
1459 u_char *blob, *response;
1463 debug3("%s entering", __func__);
1465 if (!authctxt->valid)
1466 fatal("%s: authctxt not valid", __func__);
1467 if (ssh1_challenge == NULL)
1468 fatal("%s: no ssh1_challenge", __func__);
1470 blob = buffer_get_string(m, &blen);
1471 if (!monitor_allowed_key(blob, blen))
1472 fatal("%s: bad key, not previously allowed", __func__);
1473 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1474 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1475 if ((key = key_from_blob(blob, blen)) == NULL)
1476 fatal("%s: received bad key", __func__);
1477 response = buffer_get_string(m, &len);
1479 fatal("%s: received bad response to challenge", __func__);
1480 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1486 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1489 BN_clear_free(ssh1_challenge);
1490 ssh1_challenge = NULL;
1491 monitor_reset_key_state();
1494 buffer_put_int(m, success);
1495 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1501 mm_answer_term(int sock, Buffer *req)
1503 extern struct monitor *pmonitor;
1506 debug3("%s: tearing down sessions", __func__);
1508 /* The child is terminating */
1509 session_destroy_all(&mm_session_close);
1511 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1515 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1517 /* Terminate process */
1521 #ifdef SSH_AUDIT_EVENTS
1522 /* Report that an audit event occurred */
1524 mm_answer_audit_event(int socket, Buffer *m)
1526 ssh_audit_event_t event;
1528 debug3("%s entering", __func__);
1530 event = buffer_get_int(m);
1532 case SSH_AUTH_FAIL_PUBKEY:
1533 case SSH_AUTH_FAIL_HOSTBASED:
1534 case SSH_AUTH_FAIL_GSSAPI:
1535 case SSH_LOGIN_EXCEED_MAXTRIES:
1536 case SSH_LOGIN_ROOT_DENIED:
1537 case SSH_CONNECTION_CLOSE:
1538 case SSH_INVALID_USER:
1542 fatal("Audit event type %d not permitted", event);
1549 mm_answer_audit_command(int socket, Buffer *m)
1554 debug3("%s entering", __func__);
1555 cmd = buffer_get_string(m, &len);
1556 /* sanity check command, if so how? */
1557 audit_run_command(cmd);
1561 #endif /* SSH_AUDIT_EVENTS */
1564 monitor_apply_keystate(struct monitor *pmonitor)
1567 set_newkeys(MODE_IN);
1568 set_newkeys(MODE_OUT);
1570 packet_set_protocol_flags(child_state.ssh1protoflags);
1571 packet_set_encryption_key(child_state.ssh1key,
1572 child_state.ssh1keylen, child_state.ssh1cipher);
1573 xfree(child_state.ssh1key);
1576 /* for rc4 and other stateful ciphers */
1577 packet_set_keycontext(MODE_OUT, child_state.keyout);
1578 xfree(child_state.keyout);
1579 packet_set_keycontext(MODE_IN, child_state.keyin);
1580 xfree(child_state.keyin);
1583 packet_set_iv(MODE_OUT, child_state.ivout);
1584 xfree(child_state.ivout);
1585 packet_set_iv(MODE_IN, child_state.ivin);
1586 xfree(child_state.ivin);
1589 memcpy(&incoming_stream, &child_state.incoming,
1590 sizeof(incoming_stream));
1591 memcpy(&outgoing_stream, &child_state.outgoing,
1592 sizeof(outgoing_stream));
1594 /* Update with new address */
1595 if (options.compression)
1596 mm_init_compression(pmonitor->m_zlib);
1598 /* Network I/O buffers */
1599 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1600 buffer_clear(&input);
1601 buffer_append(&input, child_state.input, child_state.ilen);
1602 memset(child_state.input, 0, child_state.ilen);
1603 xfree(child_state.input);
1605 buffer_clear(&output);
1606 buffer_append(&output, child_state.output, child_state.olen);
1607 memset(child_state.output, 0, child_state.olen);
1608 xfree(child_state.output);
1612 mm_get_kex(Buffer *m)
1618 kex = xmalloc(sizeof(*kex));
1619 memset(kex, 0, sizeof(*kex));
1620 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1621 if ((session_id2 == NULL) ||
1622 (kex->session_id_len != session_id2_len) ||
1623 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1624 fatal("mm_get_get: internal error: bad session id");
1625 kex->we_need = buffer_get_int(m);
1626 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1627 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1628 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1630 kex->hostkey_type = buffer_get_int(m);
1631 kex->kex_type = buffer_get_int(m);
1632 blob = buffer_get_string(m, &bloblen);
1633 buffer_init(&kex->my);
1634 buffer_append(&kex->my, blob, bloblen);
1636 blob = buffer_get_string(m, &bloblen);
1637 buffer_init(&kex->peer);
1638 buffer_append(&kex->peer, blob, bloblen);
1641 kex->flags = buffer_get_int(m);
1642 kex->client_version_string = buffer_get_string(m, NULL);
1643 kex->server_version_string = buffer_get_string(m, NULL);
1644 kex->load_host_key=&get_hostkey_by_type;
1645 kex->host_key_index=&get_hostkey_index;
1650 /* This function requries careful sanity checking */
1653 mm_get_keystate(struct monitor *pmonitor)
1657 u_int bloblen, plen;
1658 u_int32_t seqnr, packets;
1661 debug3("%s: Waiting for new keys", __func__);
1664 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1666 child_state.ssh1protoflags = buffer_get_int(&m);
1667 child_state.ssh1cipher = buffer_get_int(&m);
1668 child_state.ssh1key = buffer_get_string(&m,
1669 &child_state.ssh1keylen);
1670 child_state.ivout = buffer_get_string(&m,
1671 &child_state.ivoutlen);
1672 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1675 /* Get the Kex for rekeying */
1676 *pmonitor->m_pkex = mm_get_kex(&m);
1679 blob = buffer_get_string(&m, &bloblen);
1680 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1683 debug3("%s: Waiting for second key", __func__);
1684 blob = buffer_get_string(&m, &bloblen);
1685 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1688 /* Now get sequence numbers for the packets */
1689 seqnr = buffer_get_int(&m);
1690 blocks = buffer_get_int64(&m);
1691 packets = buffer_get_int(&m);
1692 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1693 seqnr = buffer_get_int(&m);
1694 blocks = buffer_get_int64(&m);
1695 packets = buffer_get_int(&m);
1696 packet_set_state(MODE_IN, seqnr, blocks, packets);
1699 /* Get the key context */
1700 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1701 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1703 debug3("%s: Getting compression state", __func__);
1704 /* Get compression state */
1705 p = buffer_get_string(&m, &plen);
1706 if (plen != sizeof(child_state.outgoing))
1707 fatal("%s: bad request size", __func__);
1708 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1711 p = buffer_get_string(&m, &plen);
1712 if (plen != sizeof(child_state.incoming))
1713 fatal("%s: bad request size", __func__);
1714 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1717 /* Network I/O buffers */
1718 debug3("%s: Getting Network I/O buffers", __func__);
1719 child_state.input = buffer_get_string(&m, &child_state.ilen);
1720 child_state.output = buffer_get_string(&m, &child_state.olen);
1726 /* Allocation functions for zlib */
1728 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1730 size_t len = (size_t) size * ncount;
1733 if (len == 0 || ncount > SIZE_T_MAX / size)
1734 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1736 address = mm_malloc(mm, len);
1742 mm_zfree(struct mm_master *mm, void *address)
1744 mm_free(mm, address);
1748 mm_init_compression(struct mm_master *mm)
1750 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1751 outgoing_stream.zfree = (free_func)mm_zfree;
1752 outgoing_stream.opaque = mm;
1754 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1755 incoming_stream.zfree = (free_func)mm_zfree;
1756 incoming_stream.opaque = mm;
1761 #define FD_CLOSEONEXEC(x) do { \
1762 if (fcntl(x, F_SETFD, 1) == -1) \
1763 fatal("fcntl(%d, F_SETFD)", x); \
1767 monitor_socketpair(int *pair)
1769 #ifdef HAVE_SOCKETPAIR
1770 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1771 fatal("%s: socketpair", __func__);
1773 fatal("%s: UsePrivilegeSeparation=yes not supported",
1776 FD_CLOSEONEXEC(pair[0]);
1777 FD_CLOSEONEXEC(pair[1]);
1780 #define MM_MEMSIZE 65536
1785 struct monitor *mon;
1788 mon = xmalloc(sizeof(*mon));
1791 monitor_socketpair(pair);
1793 mon->m_recvfd = pair[0];
1794 mon->m_sendfd = pair[1];
1796 /* Used to share zlib space across processes */
1797 if (options.compression) {
1798 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1799 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1801 /* Compression needs to share state across borders */
1802 mm_init_compression(mon->m_zlib);
1809 monitor_reinit(struct monitor *mon)
1813 monitor_socketpair(pair);
1815 mon->m_recvfd = pair[0];
1816 mon->m_sendfd = pair[1];
1821 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1827 goid.elements = buffer_get_string(m, &len);
1830 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1832 xfree(goid.elements);
1835 buffer_put_int(m, major);
1837 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1839 /* Now we have a context, enable the step */
1840 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1846 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1849 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1850 OM_uint32 major, minor;
1851 OM_uint32 flags = 0; /* GSI needs this */
1854 in.value = buffer_get_string(m, &len);
1856 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1860 buffer_put_int(m, major);
1861 buffer_put_string(m, out.value, out.length);
1862 buffer_put_int(m, flags);
1863 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1865 gss_release_buffer(&minor, &out);
1867 if (major == GSS_S_COMPLETE) {
1868 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1869 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1870 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1876 mm_answer_gss_checkmic(int sock, Buffer *m)
1878 gss_buffer_desc gssbuf, mic;
1882 gssbuf.value = buffer_get_string(m, &len);
1883 gssbuf.length = len;
1884 mic.value = buffer_get_string(m, &len);
1887 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1889 xfree(gssbuf.value);
1893 buffer_put_int(m, ret);
1895 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1897 if (!GSS_ERROR(ret))
1898 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1904 mm_answer_gss_userok(int sock, Buffer *m)
1908 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1911 buffer_put_int(m, authenticated);
1913 debug3("%s: sending result %d", __func__, authenticated);
1914 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1916 auth_method = "gssapi-with-mic";
1918 /* Monitor loop will terminate if authenticated */
1919 return (authenticated);