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.68 2006/02/20 17:02:44 stevesk Exp $");
30 #include <sys/types.h>
40 #include <openssl/dh.h>
46 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
49 #define TARGET_OS_MAC 1
54 #include "auth-options.h"
63 #include "monitor_mm.h"
64 #include "monitor_wrap.h"
65 #include "monitor_fdpass.h"
75 static Gssctxt *gsscontext = NULL;
79 extern ServerOptions options;
80 extern u_int utmp_len;
81 extern Newkeys *current_keys[];
82 extern z_stream incoming_stream;
83 extern z_stream outgoing_stream;
84 extern u_char session_id[];
85 extern Buffer input, output;
86 extern Buffer auth_debug;
87 extern int auth_debug_init;
88 extern Buffer loginmsg;
90 /* State exported from the child */
113 /* Functions on the monitor that answer unprivileged requests */
115 int mm_answer_moduli(int, Buffer *);
116 int mm_answer_sign(int, Buffer *);
117 int mm_answer_pwnamallow(int, Buffer *);
118 int mm_answer_auth2_read_banner(int, Buffer *);
119 int mm_answer_authserv(int, Buffer *);
120 int mm_answer_authpassword(int, Buffer *);
121 int mm_answer_bsdauthquery(int, Buffer *);
122 int mm_answer_bsdauthrespond(int, Buffer *);
123 int mm_answer_skeyquery(int, Buffer *);
124 int mm_answer_skeyrespond(int, Buffer *);
125 int mm_answer_keyallowed(int, Buffer *);
126 int mm_answer_keyverify(int, Buffer *);
127 int mm_answer_pty(int, Buffer *);
128 int mm_answer_pty_cleanup(int, Buffer *);
129 int mm_answer_term(int, Buffer *);
130 int mm_answer_rsa_keyallowed(int, Buffer *);
131 int mm_answer_rsa_challenge(int, Buffer *);
132 int mm_answer_rsa_response(int, Buffer *);
133 int mm_answer_sesskey(int, Buffer *);
134 int mm_answer_sessid(int, Buffer *);
137 int mm_answer_pam_start(int, Buffer *);
138 int mm_answer_pam_account(int, Buffer *);
139 int mm_answer_pam_init_ctx(int, Buffer *);
140 int mm_answer_pam_query(int, Buffer *);
141 int mm_answer_pam_respond(int, Buffer *);
142 int mm_answer_pam_free_ctx(int, Buffer *);
146 int mm_answer_gss_setup_ctx(int, Buffer *);
147 int mm_answer_gss_accept_ctx(int, Buffer *);
148 int mm_answer_gss_userok(int, Buffer *);
149 int mm_answer_gss_checkmic(int, Buffer *);
152 #ifdef SSH_AUDIT_EVENTS
153 int mm_answer_audit_event(int, Buffer *);
154 int mm_answer_audit_command(int, Buffer *);
157 static Authctxt *authctxt;
158 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
160 /* local state for key verify */
161 static u_char *key_blob = NULL;
162 static u_int key_bloblen = 0;
163 static int key_blobtype = MM_NOKEY;
164 static char *hostbased_cuser = NULL;
165 static char *hostbased_chost = NULL;
166 static char *auth_method = "unknown";
167 static u_int session_id2_len = 0;
168 static u_char *session_id2 = NULL;
169 static pid_t monitor_child_pid;
172 enum monitor_reqtype type;
174 int (*f)(int, Buffer *);
177 #define MON_ISAUTH 0x0004 /* Required for Authentication */
178 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
179 #define MON_ONCE 0x0010 /* Disable after calling */
181 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
183 #define MON_PERMIT 0x1000 /* Request is permitted */
185 struct mon_table mon_dispatch_proto20[] = {
186 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
187 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
188 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
189 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
190 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
191 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
193 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
194 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
195 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
196 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
197 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
198 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
200 #ifdef SSH_AUDIT_EVENTS
201 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
204 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
205 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
208 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
209 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
211 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
212 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
214 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
215 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
216 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
217 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
222 struct mon_table mon_dispatch_postauth20[] = {
223 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
224 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
225 {MONITOR_REQ_PTY, 0, mm_answer_pty},
226 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
227 {MONITOR_REQ_TERM, 0, mm_answer_term},
228 #ifdef SSH_AUDIT_EVENTS
229 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
230 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
235 struct mon_table mon_dispatch_proto15[] = {
236 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
237 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
238 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
239 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
240 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
241 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
242 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
243 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
245 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
246 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
249 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
250 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
253 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
254 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
255 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
256 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
257 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
258 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
260 #ifdef SSH_AUDIT_EVENTS
261 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
266 struct mon_table mon_dispatch_postauth15[] = {
267 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
268 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
269 {MONITOR_REQ_TERM, 0, mm_answer_term},
270 #ifdef SSH_AUDIT_EVENTS
271 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
272 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
277 struct mon_table *mon_dispatch;
279 /* Specifies if a certain message is allowed at the moment */
282 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
284 while (ent->f != NULL) {
285 if (ent->type == type) {
286 ent->flags &= ~MON_PERMIT;
287 ent->flags |= permit ? MON_PERMIT : 0;
295 monitor_permit_authentications(int permit)
297 struct mon_table *ent = mon_dispatch;
299 while (ent->f != NULL) {
300 if (ent->flags & MON_AUTH) {
301 ent->flags &= ~MON_PERMIT;
302 ent->flags |= permit ? MON_PERMIT : 0;
309 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
311 struct mon_table *ent;
312 int authenticated = 0;
314 debug3("preauth child monitor started");
316 authctxt = _authctxt;
317 memset(authctxt, 0, sizeof(*authctxt));
319 authctxt->loginmsg = &loginmsg;
322 mon_dispatch = mon_dispatch_proto20;
324 /* Permit requests for moduli and signatures */
325 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
326 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
328 mon_dispatch = mon_dispatch_proto15;
330 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
333 /* The first few requests do not require asynchronous access */
334 while (!authenticated) {
335 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
337 if (!(ent->flags & MON_AUTHDECIDE))
338 fatal("%s: unexpected authentication from %d",
339 __func__, ent->type);
340 if (authctxt->pw->pw_uid == 0 &&
341 !auth_root_allowed(auth_method))
344 /* PAM needs to perform account checks after auth */
345 if (options.use_pam && authenticated) {
349 mm_request_receive_expect(pmonitor->m_sendfd,
350 MONITOR_REQ_PAM_ACCOUNT, &m);
351 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
357 if (ent->flags & MON_AUTHDECIDE) {
358 auth_log(authctxt, authenticated, auth_method,
359 compat20 ? " ssh2" : "");
361 authctxt->failures++;
365 if (!authctxt->valid)
366 fatal("%s: authenticated invalid user", __func__);
368 debug("%s: %s has been authenticated by privileged process",
369 __func__, authctxt->user);
371 mm_get_keystate(pmonitor);
375 monitor_set_child_handler(pid_t pid)
377 monitor_child_pid = pid;
381 monitor_child_handler(int sig)
383 kill(monitor_child_pid, sig);
387 monitor_child_postauth(struct monitor *pmonitor)
389 monitor_set_child_handler(pmonitor->m_pid);
390 signal(SIGHUP, &monitor_child_handler);
391 signal(SIGTERM, &monitor_child_handler);
394 mon_dispatch = mon_dispatch_postauth20;
396 /* Permit requests for moduli and signatures */
397 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
398 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
399 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
401 mon_dispatch = mon_dispatch_postauth15;
402 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
405 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
406 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
410 monitor_read(pmonitor, mon_dispatch, NULL);
414 monitor_sync(struct monitor *pmonitor)
416 if (options.compression) {
417 /* The member allocation is not visible, so sync it */
418 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
423 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
424 struct mon_table **pent)
432 mm_request_receive(pmonitor->m_sendfd, &m);
433 type = buffer_get_char(&m);
435 debug3("%s: checking request %d", __func__, type);
437 while (ent->f != NULL) {
438 if (ent->type == type)
443 if (ent->f != NULL) {
444 if (!(ent->flags & MON_PERMIT))
445 fatal("%s: unpermitted request %d", __func__,
447 ret = (*ent->f)(pmonitor->m_sendfd, &m);
450 /* The child may use this request only once, disable it */
451 if (ent->flags & MON_ONCE) {
452 debug2("%s: %d used once, disabling now", __func__,
454 ent->flags &= ~MON_PERMIT;
463 fatal("%s: unsupported request: %d", __func__, type);
469 /* allowed key state */
471 monitor_allowed_key(u_char *blob, u_int bloblen)
473 /* make sure key is allowed */
474 if (key_blob == NULL || key_bloblen != bloblen ||
475 memcmp(key_blob, blob, key_bloblen))
481 monitor_reset_key_state(void)
484 if (key_blob != NULL)
486 if (hostbased_cuser != NULL)
487 xfree(hostbased_cuser);
488 if (hostbased_chost != NULL)
489 xfree(hostbased_chost);
492 key_blobtype = MM_NOKEY;
493 hostbased_cuser = NULL;
494 hostbased_chost = NULL;
498 mm_answer_moduli(int sock, Buffer *m)
503 min = buffer_get_int(m);
504 want = buffer_get_int(m);
505 max = buffer_get_int(m);
507 debug3("%s: got parameters: %d %d %d",
508 __func__, min, want, max);
509 /* We need to check here, too, in case the child got corrupted */
510 if (max < min || want < min || max < want)
511 fatal("%s: bad parameters: %d %d %d",
512 __func__, min, want, max);
516 dh = choose_dh(min, want, max);
518 buffer_put_char(m, 0);
521 /* Send first bignum */
522 buffer_put_char(m, 1);
523 buffer_put_bignum2(m, dh->p);
524 buffer_put_bignum2(m, dh->g);
528 mm_request_send(sock, MONITOR_ANS_MODULI, m);
533 mm_answer_sign(int sock, Buffer *m)
538 u_int siglen, datlen;
541 debug3("%s", __func__);
543 keyid = buffer_get_int(m);
544 p = buffer_get_string(m, &datlen);
547 fatal("%s: data length incorrect: %u", __func__, datlen);
549 /* save session id, it will be passed on the first call */
550 if (session_id2_len == 0) {
551 session_id2_len = datlen;
552 session_id2 = xmalloc(session_id2_len);
553 memcpy(session_id2, p, session_id2_len);
556 if ((key = get_hostkey_by_index(keyid)) == NULL)
557 fatal("%s: no hostkey from index %d", __func__, keyid);
558 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
559 fatal("%s: key_sign failed", __func__);
561 debug3("%s: signature %p(%u)", __func__, signature, siglen);
564 buffer_put_string(m, signature, siglen);
569 mm_request_send(sock, MONITOR_ANS_SIGN, m);
571 /* Turn on permissions for getpwnam */
572 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
577 /* Retrieves the password entry and also checks if the user is permitted */
580 mm_answer_pwnamallow(int sock, Buffer *m)
583 struct passwd *pwent;
586 debug3("%s", __func__);
588 if (authctxt->attempt++ != 0)
589 fatal("%s: multiple attempts for getpwnam", __func__);
591 username = buffer_get_string(m, NULL);
593 pwent = getpwnamallow(username);
595 authctxt->user = xstrdup(username);
596 setproctitle("%s [priv]", pwent ? username : "unknown");
602 buffer_put_char(m, 0);
603 authctxt->pw = fakepw();
608 authctxt->pw = pwent;
611 buffer_put_char(m, 1);
612 buffer_put_string(m, pwent, sizeof(struct passwd));
613 buffer_put_cstring(m, pwent->pw_name);
614 buffer_put_cstring(m, "*");
615 buffer_put_cstring(m, pwent->pw_gecos);
616 #ifdef HAVE_PW_CLASS_IN_PASSWD
617 buffer_put_cstring(m, pwent->pw_class);
619 buffer_put_cstring(m, pwent->pw_dir);
620 buffer_put_cstring(m, pwent->pw_shell);
623 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
624 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
626 /* For SSHv1 allow authentication now */
628 monitor_permit_authentications(1);
630 /* Allow service/style information on the auth context */
631 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
632 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
637 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
639 #ifdef SSH_AUDIT_EVENTS
640 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
646 int mm_answer_auth2_read_banner(int sock, Buffer *m)
651 banner = auth2_read_banner();
652 buffer_put_cstring(m, banner != NULL ? banner : "");
653 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
662 mm_answer_authserv(int sock, Buffer *m)
664 monitor_permit_authentications(1);
666 authctxt->service = buffer_get_string(m, NULL);
667 authctxt->style = buffer_get_string(m, NULL);
668 debug3("%s: service=%s, style=%s",
669 __func__, authctxt->service, authctxt->style);
671 if (strlen(authctxt->style) == 0) {
672 xfree(authctxt->style);
673 authctxt->style = NULL;
680 mm_answer_authpassword(int sock, Buffer *m)
682 static int call_count;
687 passwd = buffer_get_string(m, &plen);
688 /* Only authenticate if the context is valid */
689 authenticated = options.password_authentication &&
690 auth_password(authctxt, passwd);
691 memset(passwd, 0, strlen(passwd));
695 buffer_put_int(m, authenticated);
697 debug3("%s: sending result %d", __func__, authenticated);
698 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
701 if (plen == 0 && call_count == 1)
702 auth_method = "none";
704 auth_method = "password";
706 /* Causes monitor loop to terminate if authenticated */
707 return (authenticated);
712 mm_answer_bsdauthquery(int sock, Buffer *m)
714 char *name, *infotxt;
720 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
721 &prompts, &echo_on) < 0 ? 0 : 1;
724 buffer_put_int(m, success);
726 buffer_put_cstring(m, prompts[0]);
728 debug3("%s: sending challenge success: %u", __func__, success);
729 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
742 mm_answer_bsdauthrespond(int sock, Buffer *m)
747 if (authctxt->as == 0)
748 fatal("%s: no bsd auth session", __func__);
750 response = buffer_get_string(m, NULL);
751 authok = options.challenge_response_authentication &&
752 auth_userresponse(authctxt->as, response, 0);
754 debug3("%s: <%s> = <%d>", __func__, response, authok);
758 buffer_put_int(m, authok);
760 debug3("%s: sending authenticated: %d", __func__, authok);
761 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
763 auth_method = "bsdauth";
765 return (authok != 0);
771 mm_answer_skeyquery(int sock, Buffer *m)
774 char challenge[1024];
777 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
778 sizeof(challenge)) < 0 ? 0 : 1;
781 buffer_put_int(m, success);
783 buffer_put_cstring(m, challenge);
785 debug3("%s: sending challenge success: %u", __func__, success);
786 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
792 mm_answer_skeyrespond(int sock, Buffer *m)
797 response = buffer_get_string(m, NULL);
799 authok = (options.challenge_response_authentication &&
801 skey_haskey(authctxt->pw->pw_name) == 0 &&
802 skey_passcheck(authctxt->pw->pw_name, response) != -1);
807 buffer_put_int(m, authok);
809 debug3("%s: sending authenticated: %d", __func__, authok);
810 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
812 auth_method = "skey";
814 return (authok != 0);
820 mm_answer_pam_start(int sock, Buffer *m)
822 if (!options.use_pam)
823 fatal("UsePAM not set, but ended up in %s anyway", __func__);
827 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
833 mm_answer_pam_account(int sock, Buffer *m)
837 if (!options.use_pam)
838 fatal("UsePAM not set, but ended up in %s anyway", __func__);
840 ret = do_pam_account();
842 buffer_put_int(m, ret);
843 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
845 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
850 static void *sshpam_ctxt, *sshpam_authok;
851 extern KbdintDevice sshpam_device;
854 mm_answer_pam_init_ctx(int sock, Buffer *m)
857 debug3("%s", __func__);
858 authctxt->user = buffer_get_string(m, NULL);
859 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
860 sshpam_authok = NULL;
862 if (sshpam_ctxt != NULL) {
863 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
864 buffer_put_int(m, 1);
866 buffer_put_int(m, 0);
868 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
873 mm_answer_pam_query(int sock, Buffer *m)
875 char *name, *info, **prompts;
876 u_int i, num, *echo_on;
879 debug3("%s", __func__);
880 sshpam_authok = NULL;
881 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
882 if (ret == 0 && num == 0)
883 sshpam_authok = sshpam_ctxt;
884 if (num > 1 || name == NULL || info == NULL)
887 buffer_put_int(m, ret);
888 buffer_put_cstring(m, name);
890 buffer_put_cstring(m, info);
892 buffer_put_int(m, num);
893 for (i = 0; i < num; ++i) {
894 buffer_put_cstring(m, prompts[i]);
896 buffer_put_int(m, echo_on[i]);
902 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
907 mm_answer_pam_respond(int sock, Buffer *m)
913 debug3("%s", __func__);
914 sshpam_authok = NULL;
915 num = buffer_get_int(m);
917 resp = xmalloc(num * sizeof(char *));
918 for (i = 0; i < num; ++i)
919 resp[i] = buffer_get_string(m, NULL);
920 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
921 for (i = 0; i < num; ++i)
925 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
928 buffer_put_int(m, ret);
929 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
930 auth_method = "keyboard-interactive/pam";
932 sshpam_authok = sshpam_ctxt;
937 mm_answer_pam_free_ctx(int sock, Buffer *m)
940 debug3("%s", __func__);
941 (sshpam_device.free_ctx)(sshpam_ctxt);
943 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
944 return (sshpam_authok == sshpam_ctxt);
949 mm_append_debug(Buffer *m)
951 if (auth_debug_init && buffer_len(&auth_debug)) {
952 debug3("%s: Appending debug messages for child", __func__);
953 buffer_append(m, buffer_ptr(&auth_debug),
954 buffer_len(&auth_debug));
955 buffer_clear(&auth_debug);
960 mm_answer_keyallowed(int sock, Buffer *m)
966 enum mm_keytype type = 0;
969 debug3("%s entering", __func__);
971 type = buffer_get_int(m);
972 cuser = buffer_get_string(m, NULL);
973 chost = buffer_get_string(m, NULL);
974 blob = buffer_get_string(m, &bloblen);
976 key = key_from_blob(blob, bloblen);
978 if ((compat20 && type == MM_RSAHOSTKEY) ||
979 (!compat20 && type != MM_RSAHOSTKEY))
980 fatal("%s: key type and protocol mismatch", __func__);
982 debug3("%s: key_from_blob: %p", __func__, key);
984 if (key != NULL && authctxt->valid) {
987 allowed = options.pubkey_authentication &&
988 user_key_allowed(authctxt->pw, key);
991 allowed = options.hostbased_authentication &&
992 hostbased_key_allowed(authctxt->pw,
996 key->type = KEY_RSA1; /* XXX */
997 allowed = options.rhosts_rsa_authentication &&
998 auth_rhosts_rsa_key_allowed(authctxt->pw,
1002 fatal("%s: unknown key type %d", __func__, type);
1009 /* clear temporarily storage (used by verify) */
1010 monitor_reset_key_state();
1013 /* Save temporarily for comparison in verify */
1015 key_bloblen = bloblen;
1016 key_blobtype = type;
1017 hostbased_cuser = cuser;
1018 hostbased_chost = chost;
1021 debug3("%s: key %p is %s",
1022 __func__, key, allowed ? "allowed" : "disallowed");
1025 buffer_put_int(m, allowed);
1026 buffer_put_int(m, forced_command != NULL);
1030 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1032 if (type == MM_RSAHOSTKEY)
1033 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1039 monitor_valid_userblob(u_char *data, u_int datalen)
1047 buffer_append(&b, data, datalen);
1049 if (datafellows & SSH_OLD_SESSIONID) {
1051 len = buffer_len(&b);
1052 if ((session_id2 == NULL) ||
1053 (len < session_id2_len) ||
1054 (memcmp(p, session_id2, session_id2_len) != 0))
1056 buffer_consume(&b, session_id2_len);
1058 p = buffer_get_string(&b, &len);
1059 if ((session_id2 == NULL) ||
1060 (len != session_id2_len) ||
1061 (memcmp(p, session_id2, session_id2_len) != 0))
1065 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1067 p = buffer_get_string(&b, NULL);
1068 if (strcmp(authctxt->user, p) != 0) {
1069 logit("wrong user name passed to monitor: expected %s != %.100s",
1074 buffer_skip_string(&b);
1075 if (datafellows & SSH_BUG_PKAUTH) {
1076 if (!buffer_get_char(&b))
1079 p = buffer_get_string(&b, NULL);
1080 if (strcmp("publickey", p) != 0)
1083 if (!buffer_get_char(&b))
1085 buffer_skip_string(&b);
1087 buffer_skip_string(&b);
1088 if (buffer_len(&b) != 0)
1095 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1104 buffer_append(&b, data, datalen);
1106 p = buffer_get_string(&b, &len);
1107 if ((session_id2 == NULL) ||
1108 (len != session_id2_len) ||
1109 (memcmp(p, session_id2, session_id2_len) != 0))
1113 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1115 p = buffer_get_string(&b, NULL);
1116 if (strcmp(authctxt->user, p) != 0) {
1117 logit("wrong user name passed to monitor: expected %s != %.100s",
1122 buffer_skip_string(&b); /* service */
1123 p = buffer_get_string(&b, NULL);
1124 if (strcmp(p, "hostbased") != 0)
1127 buffer_skip_string(&b); /* pkalg */
1128 buffer_skip_string(&b); /* pkblob */
1130 /* verify client host, strip trailing dot if necessary */
1131 p = buffer_get_string(&b, NULL);
1132 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1134 if (strcmp(p, chost) != 0)
1138 /* verify client user */
1139 p = buffer_get_string(&b, NULL);
1140 if (strcmp(p, cuser) != 0)
1144 if (buffer_len(&b) != 0)
1151 mm_answer_keyverify(int sock, Buffer *m)
1154 u_char *signature, *data, *blob;
1155 u_int signaturelen, datalen, bloblen;
1159 blob = buffer_get_string(m, &bloblen);
1160 signature = buffer_get_string(m, &signaturelen);
1161 data = buffer_get_string(m, &datalen);
1163 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1164 !monitor_allowed_key(blob, bloblen))
1165 fatal("%s: bad key, not previously allowed", __func__);
1167 key = key_from_blob(blob, bloblen);
1169 fatal("%s: bad public key blob", __func__);
1171 switch (key_blobtype) {
1173 valid_data = monitor_valid_userblob(data, datalen);
1176 valid_data = monitor_valid_hostbasedblob(data, datalen,
1177 hostbased_cuser, hostbased_chost);
1184 fatal("%s: bad signature data blob", __func__);
1186 verified = key_verify(key, signature, signaturelen, data, datalen);
1187 debug3("%s: key %p signature %s",
1188 __func__, key, verified ? "verified" : "unverified");
1195 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1197 monitor_reset_key_state();
1200 buffer_put_int(m, verified);
1201 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1207 mm_record_login(Session *s, struct passwd *pw)
1210 struct sockaddr_storage from;
1213 * Get IP address of client. If the connection is not a socket, let
1214 * the address be 0.0.0.0.
1216 memset(&from, 0, sizeof(from));
1217 fromlen = sizeof(from);
1218 if (packet_connection_is_on_socket()) {
1219 if (getpeername(packet_get_connection_in(),
1220 (struct sockaddr *) & from, &fromlen) < 0) {
1221 debug("getpeername: %.100s", strerror(errno));
1225 /* Record that there was a login on that tty from the remote host. */
1226 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1227 get_remote_name_or_ip(utmp_len, options.use_dns),
1228 (struct sockaddr *)&from, fromlen);
1232 mm_session_close(Session *s)
1234 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1235 if (s->ttyfd != -1) {
1236 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1237 session_pty_cleanup2(s);
1243 mm_answer_pty(int sock, Buffer *m)
1245 extern struct monitor *pmonitor;
1249 debug3("%s entering", __func__);
1255 s->authctxt = authctxt;
1256 s->pw = authctxt->pw;
1257 s->pid = pmonitor->m_pid;
1258 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1261 pty_setowner(authctxt->pw, s->tty);
1263 buffer_put_int(m, 1);
1264 buffer_put_cstring(m, s->tty);
1266 /* We need to trick ttyslot */
1267 if (dup2(s->ttyfd, 0) == -1)
1268 fatal("%s: dup2", __func__);
1270 mm_record_login(s, authctxt->pw);
1272 /* Now we can close the file descriptor again */
1275 /* send messages generated by record_login */
1276 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1277 buffer_clear(&loginmsg);
1279 mm_request_send(sock, MONITOR_ANS_PTY, m);
1281 mm_send_fd(sock, s->ptyfd);
1282 mm_send_fd(sock, s->ttyfd);
1284 /* make sure nothing uses fd 0 */
1285 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1286 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1288 error("%s: fd0 %d != 0", __func__, fd0);
1290 /* slave is not needed */
1292 s->ttyfd = s->ptyfd;
1293 /* no need to dup() because nobody closes ptyfd */
1294 s->ptymaster = s->ptyfd;
1296 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1302 mm_session_close(s);
1303 buffer_put_int(m, 0);
1304 mm_request_send(sock, MONITOR_ANS_PTY, m);
1309 mm_answer_pty_cleanup(int sock, Buffer *m)
1314 debug3("%s entering", __func__);
1316 tty = buffer_get_string(m, NULL);
1317 if ((s = session_by_tty(tty)) != NULL)
1318 mm_session_close(s);
1325 mm_answer_sesskey(int sock, Buffer *m)
1330 /* Turn off permissions */
1331 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1333 if ((p = BN_new()) == NULL)
1334 fatal("%s: BN_new", __func__);
1336 buffer_get_bignum2(m, p);
1338 rsafail = ssh1_session_key(p);
1341 buffer_put_int(m, rsafail);
1342 buffer_put_bignum2(m, p);
1346 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1348 /* Turn on permissions for sessid passing */
1349 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1355 mm_answer_sessid(int sock, Buffer *m)
1359 debug3("%s entering", __func__);
1361 if (buffer_len(m) != 16)
1362 fatal("%s: bad ssh1 session id", __func__);
1363 for (i = 0; i < 16; i++)
1364 session_id[i] = buffer_get_char(m);
1366 /* Turn on permissions for getpwnam */
1367 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1373 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1377 u_char *blob = NULL;
1381 debug3("%s entering", __func__);
1383 if (options.rsa_authentication && authctxt->valid) {
1384 if ((client_n = BN_new()) == NULL)
1385 fatal("%s: BN_new", __func__);
1386 buffer_get_bignum2(m, client_n);
1387 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1388 BN_clear_free(client_n);
1391 buffer_put_int(m, allowed);
1392 buffer_put_int(m, forced_command != NULL);
1394 /* clear temporarily storage (used by generate challenge) */
1395 monitor_reset_key_state();
1397 if (allowed && key != NULL) {
1398 key->type = KEY_RSA; /* cheat for key_to_blob */
1399 if (key_to_blob(key, &blob, &blen) == 0)
1400 fatal("%s: key_to_blob failed", __func__);
1401 buffer_put_string(m, blob, blen);
1403 /* Save temporarily for comparison in verify */
1406 key_blobtype = MM_RSAUSERKEY;
1413 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1415 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1416 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1421 mm_answer_rsa_challenge(int sock, Buffer *m)
1427 debug3("%s entering", __func__);
1429 if (!authctxt->valid)
1430 fatal("%s: authctxt not valid", __func__);
1431 blob = buffer_get_string(m, &blen);
1432 if (!monitor_allowed_key(blob, blen))
1433 fatal("%s: bad key, not previously allowed", __func__);
1434 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1435 fatal("%s: key type mismatch", __func__);
1436 if ((key = key_from_blob(blob, blen)) == NULL)
1437 fatal("%s: received bad key", __func__);
1440 BN_clear_free(ssh1_challenge);
1441 ssh1_challenge = auth_rsa_generate_challenge(key);
1444 buffer_put_bignum2(m, ssh1_challenge);
1446 debug3("%s sending reply", __func__);
1447 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1449 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1457 mm_answer_rsa_response(int sock, Buffer *m)
1460 u_char *blob, *response;
1464 debug3("%s entering", __func__);
1466 if (!authctxt->valid)
1467 fatal("%s: authctxt not valid", __func__);
1468 if (ssh1_challenge == NULL)
1469 fatal("%s: no ssh1_challenge", __func__);
1471 blob = buffer_get_string(m, &blen);
1472 if (!monitor_allowed_key(blob, blen))
1473 fatal("%s: bad key, not previously allowed", __func__);
1474 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1475 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1476 if ((key = key_from_blob(blob, blen)) == NULL)
1477 fatal("%s: received bad key", __func__);
1478 response = buffer_get_string(m, &len);
1480 fatal("%s: received bad response to challenge", __func__);
1481 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1487 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1490 BN_clear_free(ssh1_challenge);
1491 ssh1_challenge = NULL;
1492 monitor_reset_key_state();
1495 buffer_put_int(m, success);
1496 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1502 mm_answer_term(int sock, Buffer *req)
1504 extern struct monitor *pmonitor;
1507 debug3("%s: tearing down sessions", __func__);
1509 /* The child is terminating */
1510 session_destroy_all(&mm_session_close);
1512 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1516 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1518 /* Terminate process */
1522 #ifdef SSH_AUDIT_EVENTS
1523 /* Report that an audit event occurred */
1525 mm_answer_audit_event(int socket, Buffer *m)
1527 ssh_audit_event_t event;
1529 debug3("%s entering", __func__);
1531 event = buffer_get_int(m);
1533 case SSH_AUTH_FAIL_PUBKEY:
1534 case SSH_AUTH_FAIL_HOSTBASED:
1535 case SSH_AUTH_FAIL_GSSAPI:
1536 case SSH_LOGIN_EXCEED_MAXTRIES:
1537 case SSH_LOGIN_ROOT_DENIED:
1538 case SSH_CONNECTION_CLOSE:
1539 case SSH_INVALID_USER:
1543 fatal("Audit event type %d not permitted", event);
1550 mm_answer_audit_command(int socket, Buffer *m)
1555 debug3("%s entering", __func__);
1556 cmd = buffer_get_string(m, &len);
1557 /* sanity check command, if so how? */
1558 audit_run_command(cmd);
1562 #endif /* SSH_AUDIT_EVENTS */
1565 monitor_apply_keystate(struct monitor *pmonitor)
1568 set_newkeys(MODE_IN);
1569 set_newkeys(MODE_OUT);
1571 packet_set_protocol_flags(child_state.ssh1protoflags);
1572 packet_set_encryption_key(child_state.ssh1key,
1573 child_state.ssh1keylen, child_state.ssh1cipher);
1574 xfree(child_state.ssh1key);
1577 /* for rc4 and other stateful ciphers */
1578 packet_set_keycontext(MODE_OUT, child_state.keyout);
1579 xfree(child_state.keyout);
1580 packet_set_keycontext(MODE_IN, child_state.keyin);
1581 xfree(child_state.keyin);
1584 packet_set_iv(MODE_OUT, child_state.ivout);
1585 xfree(child_state.ivout);
1586 packet_set_iv(MODE_IN, child_state.ivin);
1587 xfree(child_state.ivin);
1590 memcpy(&incoming_stream, &child_state.incoming,
1591 sizeof(incoming_stream));
1592 memcpy(&outgoing_stream, &child_state.outgoing,
1593 sizeof(outgoing_stream));
1595 /* Update with new address */
1596 if (options.compression)
1597 mm_init_compression(pmonitor->m_zlib);
1599 /* Network I/O buffers */
1600 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1601 buffer_clear(&input);
1602 buffer_append(&input, child_state.input, child_state.ilen);
1603 memset(child_state.input, 0, child_state.ilen);
1604 xfree(child_state.input);
1606 buffer_clear(&output);
1607 buffer_append(&output, child_state.output, child_state.olen);
1608 memset(child_state.output, 0, child_state.olen);
1609 xfree(child_state.output);
1613 mm_get_kex(Buffer *m)
1619 kex = xmalloc(sizeof(*kex));
1620 memset(kex, 0, sizeof(*kex));
1621 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1622 if ((session_id2 == NULL) ||
1623 (kex->session_id_len != session_id2_len) ||
1624 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1625 fatal("mm_get_get: internal error: bad session id");
1626 kex->we_need = buffer_get_int(m);
1627 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1628 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1629 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1631 kex->hostkey_type = buffer_get_int(m);
1632 kex->kex_type = buffer_get_int(m);
1633 blob = buffer_get_string(m, &bloblen);
1634 buffer_init(&kex->my);
1635 buffer_append(&kex->my, blob, bloblen);
1637 blob = buffer_get_string(m, &bloblen);
1638 buffer_init(&kex->peer);
1639 buffer_append(&kex->peer, blob, bloblen);
1642 kex->flags = buffer_get_int(m);
1643 kex->client_version_string = buffer_get_string(m, NULL);
1644 kex->server_version_string = buffer_get_string(m, NULL);
1645 kex->load_host_key=&get_hostkey_by_type;
1646 kex->host_key_index=&get_hostkey_index;
1651 /* This function requries careful sanity checking */
1654 mm_get_keystate(struct monitor *pmonitor)
1658 u_int bloblen, plen;
1659 u_int32_t seqnr, packets;
1662 debug3("%s: Waiting for new keys", __func__);
1665 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1667 child_state.ssh1protoflags = buffer_get_int(&m);
1668 child_state.ssh1cipher = buffer_get_int(&m);
1669 child_state.ssh1key = buffer_get_string(&m,
1670 &child_state.ssh1keylen);
1671 child_state.ivout = buffer_get_string(&m,
1672 &child_state.ivoutlen);
1673 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1676 /* Get the Kex for rekeying */
1677 *pmonitor->m_pkex = mm_get_kex(&m);
1680 blob = buffer_get_string(&m, &bloblen);
1681 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1684 debug3("%s: Waiting for second key", __func__);
1685 blob = buffer_get_string(&m, &bloblen);
1686 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1689 /* Now get sequence numbers for the packets */
1690 seqnr = buffer_get_int(&m);
1691 blocks = buffer_get_int64(&m);
1692 packets = buffer_get_int(&m);
1693 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1694 seqnr = buffer_get_int(&m);
1695 blocks = buffer_get_int64(&m);
1696 packets = buffer_get_int(&m);
1697 packet_set_state(MODE_IN, seqnr, blocks, packets);
1700 /* Get the key context */
1701 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1702 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1704 debug3("%s: Getting compression state", __func__);
1705 /* Get compression state */
1706 p = buffer_get_string(&m, &plen);
1707 if (plen != sizeof(child_state.outgoing))
1708 fatal("%s: bad request size", __func__);
1709 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1712 p = buffer_get_string(&m, &plen);
1713 if (plen != sizeof(child_state.incoming))
1714 fatal("%s: bad request size", __func__);
1715 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1718 /* Network I/O buffers */
1719 debug3("%s: Getting Network I/O buffers", __func__);
1720 child_state.input = buffer_get_string(&m, &child_state.ilen);
1721 child_state.output = buffer_get_string(&m, &child_state.olen);
1727 /* Allocation functions for zlib */
1729 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1731 size_t len = (size_t) size * ncount;
1734 if (len == 0 || ncount > SIZE_T_MAX / size)
1735 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1737 address = mm_malloc(mm, len);
1743 mm_zfree(struct mm_master *mm, void *address)
1745 mm_free(mm, address);
1749 mm_init_compression(struct mm_master *mm)
1751 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1752 outgoing_stream.zfree = (free_func)mm_zfree;
1753 outgoing_stream.opaque = mm;
1755 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1756 incoming_stream.zfree = (free_func)mm_zfree;
1757 incoming_stream.opaque = mm;
1762 #define FD_CLOSEONEXEC(x) do { \
1763 if (fcntl(x, F_SETFD, 1) == -1) \
1764 fatal("fcntl(%d, F_SETFD)", x); \
1768 monitor_socketpair(int *pair)
1770 #ifdef HAVE_SOCKETPAIR
1771 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1772 fatal("%s: socketpair", __func__);
1774 fatal("%s: UsePrivilegeSeparation=yes not supported",
1777 FD_CLOSEONEXEC(pair[0]);
1778 FD_CLOSEONEXEC(pair[1]);
1781 #define MM_MEMSIZE 65536
1786 struct monitor *mon;
1789 mon = xmalloc(sizeof(*mon));
1792 monitor_socketpair(pair);
1794 mon->m_recvfd = pair[0];
1795 mon->m_sendfd = pair[1];
1797 /* Used to share zlib space across processes */
1798 if (options.compression) {
1799 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1800 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1802 /* Compression needs to share state across borders */
1803 mm_init_compression(mon->m_zlib);
1810 monitor_reinit(struct monitor *mon)
1814 monitor_socketpair(pair);
1816 mon->m_recvfd = pair[0];
1817 mon->m_sendfd = pair[1];
1822 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1828 goid.elements = buffer_get_string(m, &len);
1831 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1833 xfree(goid.elements);
1836 buffer_put_int(m, major);
1838 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1840 /* Now we have a context, enable the step */
1841 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1847 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1850 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1851 OM_uint32 major, minor;
1852 OM_uint32 flags = 0; /* GSI needs this */
1855 in.value = buffer_get_string(m, &len);
1857 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1861 buffer_put_int(m, major);
1862 buffer_put_string(m, out.value, out.length);
1863 buffer_put_int(m, flags);
1864 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1866 gss_release_buffer(&minor, &out);
1868 if (major == GSS_S_COMPLETE) {
1869 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1870 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1871 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1877 mm_answer_gss_checkmic(int sock, Buffer *m)
1879 gss_buffer_desc gssbuf, mic;
1883 gssbuf.value = buffer_get_string(m, &len);
1884 gssbuf.length = len;
1885 mic.value = buffer_get_string(m, &len);
1888 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1890 xfree(gssbuf.value);
1894 buffer_put_int(m, ret);
1896 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1898 if (!GSS_ERROR(ret))
1899 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1905 mm_answer_gss_userok(int sock, Buffer *m)
1909 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1912 buffer_put_int(m, authenticated);
1914 debug3("%s: sending result %d", __func__, authenticated);
1915 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1917 auth_method = "gssapi-with-mic";
1919 /* Monitor loop will terminate if authenticated */
1920 return (authenticated);