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.
29 #include <sys/types.h>
41 #include <openssl/dh.h>
47 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
50 #define TARGET_OS_MAC 1
55 #include "auth-options.h"
64 #include "monitor_mm.h"
65 #include "monitor_wrap.h"
66 #include "monitor_fdpass.h"
76 static Gssctxt *gsscontext = NULL;
80 extern ServerOptions options;
81 extern u_int utmp_len;
82 extern Newkeys *current_keys[];
83 extern z_stream incoming_stream;
84 extern z_stream outgoing_stream;
85 extern u_char session_id[];
86 extern Buffer input, output;
87 extern Buffer auth_debug;
88 extern int auth_debug_init;
89 extern Buffer loginmsg;
91 /* State exported from the child */
114 /* Functions on the monitor that answer unprivileged requests */
116 int mm_answer_moduli(int, Buffer *);
117 int mm_answer_sign(int, Buffer *);
118 int mm_answer_pwnamallow(int, Buffer *);
119 int mm_answer_auth2_read_banner(int, Buffer *);
120 int mm_answer_authserv(int, Buffer *);
121 int mm_answer_authpassword(int, Buffer *);
122 int mm_answer_bsdauthquery(int, Buffer *);
123 int mm_answer_bsdauthrespond(int, Buffer *);
124 int mm_answer_skeyquery(int, Buffer *);
125 int mm_answer_skeyrespond(int, Buffer *);
126 int mm_answer_keyallowed(int, Buffer *);
127 int mm_answer_keyverify(int, Buffer *);
128 int mm_answer_pty(int, Buffer *);
129 int mm_answer_pty_cleanup(int, Buffer *);
130 int mm_answer_term(int, Buffer *);
131 int mm_answer_rsa_keyallowed(int, Buffer *);
132 int mm_answer_rsa_challenge(int, Buffer *);
133 int mm_answer_rsa_response(int, Buffer *);
134 int mm_answer_sesskey(int, Buffer *);
135 int mm_answer_sessid(int, Buffer *);
138 int mm_answer_pam_start(int, Buffer *);
139 int mm_answer_pam_account(int, Buffer *);
140 int mm_answer_pam_init_ctx(int, Buffer *);
141 int mm_answer_pam_query(int, Buffer *);
142 int mm_answer_pam_respond(int, Buffer *);
143 int mm_answer_pam_free_ctx(int, Buffer *);
147 int mm_answer_gss_setup_ctx(int, Buffer *);
148 int mm_answer_gss_accept_ctx(int, Buffer *);
149 int mm_answer_gss_userok(int, Buffer *);
150 int mm_answer_gss_checkmic(int, Buffer *);
153 #ifdef SSH_AUDIT_EVENTS
154 int mm_answer_audit_event(int, Buffer *);
155 int mm_answer_audit_command(int, Buffer *);
158 static Authctxt *authctxt;
159 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
161 /* local state for key verify */
162 static u_char *key_blob = NULL;
163 static u_int key_bloblen = 0;
164 static int key_blobtype = MM_NOKEY;
165 static char *hostbased_cuser = NULL;
166 static char *hostbased_chost = NULL;
167 static char *auth_method = "unknown";
168 static u_int session_id2_len = 0;
169 static u_char *session_id2 = NULL;
170 static pid_t monitor_child_pid;
173 enum monitor_reqtype type;
175 int (*f)(int, Buffer *);
178 #define MON_ISAUTH 0x0004 /* Required for Authentication */
179 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
180 #define MON_ONCE 0x0010 /* Disable after calling */
182 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
184 #define MON_PERMIT 0x1000 /* Request is permitted */
186 struct mon_table mon_dispatch_proto20[] = {
187 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
188 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
189 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
190 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
191 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
192 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
194 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
195 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
196 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
197 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
198 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
199 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
201 #ifdef SSH_AUDIT_EVENTS
202 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
205 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
206 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
209 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
210 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
212 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
213 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
215 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
216 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
217 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
218 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
223 struct mon_table mon_dispatch_postauth20[] = {
224 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
225 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
226 {MONITOR_REQ_PTY, 0, mm_answer_pty},
227 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
228 {MONITOR_REQ_TERM, 0, mm_answer_term},
229 #ifdef SSH_AUDIT_EVENTS
230 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
231 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
236 struct mon_table mon_dispatch_proto15[] = {
237 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
238 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
239 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
240 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
241 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
242 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
243 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
244 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
246 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
247 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
250 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
251 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
254 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
255 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
256 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
257 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
258 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
259 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
261 #ifdef SSH_AUDIT_EVENTS
262 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
267 struct mon_table mon_dispatch_postauth15[] = {
268 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
269 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
270 {MONITOR_REQ_TERM, 0, mm_answer_term},
271 #ifdef SSH_AUDIT_EVENTS
272 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
273 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
278 struct mon_table *mon_dispatch;
280 /* Specifies if a certain message is allowed at the moment */
283 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
285 while (ent->f != NULL) {
286 if (ent->type == type) {
287 ent->flags &= ~MON_PERMIT;
288 ent->flags |= permit ? MON_PERMIT : 0;
296 monitor_permit_authentications(int permit)
298 struct mon_table *ent = mon_dispatch;
300 while (ent->f != NULL) {
301 if (ent->flags & MON_AUTH) {
302 ent->flags &= ~MON_PERMIT;
303 ent->flags |= permit ? MON_PERMIT : 0;
310 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
312 struct mon_table *ent;
313 int authenticated = 0;
315 debug3("preauth child monitor started");
317 authctxt = _authctxt;
318 memset(authctxt, 0, sizeof(*authctxt));
320 authctxt->loginmsg = &loginmsg;
323 mon_dispatch = mon_dispatch_proto20;
325 /* Permit requests for moduli and signatures */
326 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
327 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
329 mon_dispatch = mon_dispatch_proto15;
331 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
334 /* The first few requests do not require asynchronous access */
335 while (!authenticated) {
336 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
338 if (!(ent->flags & MON_AUTHDECIDE))
339 fatal("%s: unexpected authentication from %d",
340 __func__, ent->type);
341 if (authctxt->pw->pw_uid == 0 &&
342 !auth_root_allowed(auth_method))
345 /* PAM needs to perform account checks after auth */
346 if (options.use_pam && authenticated) {
350 mm_request_receive_expect(pmonitor->m_sendfd,
351 MONITOR_REQ_PAM_ACCOUNT, &m);
352 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
358 if (ent->flags & MON_AUTHDECIDE) {
359 auth_log(authctxt, authenticated, auth_method,
360 compat20 ? " ssh2" : "");
362 authctxt->failures++;
366 if (!authctxt->valid)
367 fatal("%s: authenticated invalid user", __func__);
369 debug("%s: %s has been authenticated by privileged process",
370 __func__, authctxt->user);
372 mm_get_keystate(pmonitor);
376 monitor_set_child_handler(pid_t pid)
378 monitor_child_pid = pid;
382 monitor_child_handler(int sig)
384 kill(monitor_child_pid, sig);
388 monitor_child_postauth(struct monitor *pmonitor)
390 monitor_set_child_handler(pmonitor->m_pid);
391 signal(SIGHUP, &monitor_child_handler);
392 signal(SIGTERM, &monitor_child_handler);
395 mon_dispatch = mon_dispatch_postauth20;
397 /* Permit requests for moduli and signatures */
398 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
399 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
400 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
402 mon_dispatch = mon_dispatch_postauth15;
403 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
406 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
407 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
411 monitor_read(pmonitor, mon_dispatch, NULL);
415 monitor_sync(struct monitor *pmonitor)
417 if (options.compression) {
418 /* The member allocation is not visible, so sync it */
419 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
424 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
425 struct mon_table **pent)
433 mm_request_receive(pmonitor->m_sendfd, &m);
434 type = buffer_get_char(&m);
436 debug3("%s: checking request %d", __func__, type);
438 while (ent->f != NULL) {
439 if (ent->type == type)
444 if (ent->f != NULL) {
445 if (!(ent->flags & MON_PERMIT))
446 fatal("%s: unpermitted request %d", __func__,
448 ret = (*ent->f)(pmonitor->m_sendfd, &m);
451 /* The child may use this request only once, disable it */
452 if (ent->flags & MON_ONCE) {
453 debug2("%s: %d used once, disabling now", __func__,
455 ent->flags &= ~MON_PERMIT;
464 fatal("%s: unsupported request: %d", __func__, type);
470 /* allowed key state */
472 monitor_allowed_key(u_char *blob, u_int bloblen)
474 /* make sure key is allowed */
475 if (key_blob == NULL || key_bloblen != bloblen ||
476 memcmp(key_blob, blob, key_bloblen))
482 monitor_reset_key_state(void)
485 if (key_blob != NULL)
487 if (hostbased_cuser != NULL)
488 xfree(hostbased_cuser);
489 if (hostbased_chost != NULL)
490 xfree(hostbased_chost);
493 key_blobtype = MM_NOKEY;
494 hostbased_cuser = NULL;
495 hostbased_chost = NULL;
499 mm_answer_moduli(int sock, Buffer *m)
504 min = buffer_get_int(m);
505 want = buffer_get_int(m);
506 max = buffer_get_int(m);
508 debug3("%s: got parameters: %d %d %d",
509 __func__, min, want, max);
510 /* We need to check here, too, in case the child got corrupted */
511 if (max < min || want < min || max < want)
512 fatal("%s: bad parameters: %d %d %d",
513 __func__, min, want, max);
517 dh = choose_dh(min, want, max);
519 buffer_put_char(m, 0);
522 /* Send first bignum */
523 buffer_put_char(m, 1);
524 buffer_put_bignum2(m, dh->p);
525 buffer_put_bignum2(m, dh->g);
529 mm_request_send(sock, MONITOR_ANS_MODULI, m);
534 mm_answer_sign(int sock, Buffer *m)
539 u_int siglen, datlen;
542 debug3("%s", __func__);
544 keyid = buffer_get_int(m);
545 p = buffer_get_string(m, &datlen);
548 * Supported KEX types will only return SHA1 (20 byte) or
549 * SHA256 (32 byte) hashes
551 if (datlen != 20 && datlen != 32)
552 fatal("%s: data length incorrect: %u", __func__, datlen);
554 /* save session id, it will be passed on the first call */
555 if (session_id2_len == 0) {
556 session_id2_len = datlen;
557 session_id2 = xmalloc(session_id2_len);
558 memcpy(session_id2, p, session_id2_len);
561 if ((key = get_hostkey_by_index(keyid)) == NULL)
562 fatal("%s: no hostkey from index %d", __func__, keyid);
563 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
564 fatal("%s: key_sign failed", __func__);
566 debug3("%s: signature %p(%u)", __func__, signature, siglen);
569 buffer_put_string(m, signature, siglen);
574 mm_request_send(sock, MONITOR_ANS_SIGN, m);
576 /* Turn on permissions for getpwnam */
577 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
582 /* Retrieves the password entry and also checks if the user is permitted */
585 mm_answer_pwnamallow(int sock, Buffer *m)
588 struct passwd *pwent;
591 debug3("%s", __func__);
593 if (authctxt->attempt++ != 0)
594 fatal("%s: multiple attempts for getpwnam", __func__);
596 username = buffer_get_string(m, NULL);
598 pwent = getpwnamallow(username);
600 authctxt->user = xstrdup(username);
601 setproctitle("%s [priv]", pwent ? username : "unknown");
607 buffer_put_char(m, 0);
608 authctxt->pw = fakepw();
613 authctxt->pw = pwent;
616 buffer_put_char(m, 1);
617 buffer_put_string(m, pwent, sizeof(struct passwd));
618 buffer_put_cstring(m, pwent->pw_name);
619 buffer_put_cstring(m, "*");
620 buffer_put_cstring(m, pwent->pw_gecos);
621 #ifdef HAVE_PW_CLASS_IN_PASSWD
622 buffer_put_cstring(m, pwent->pw_class);
624 buffer_put_cstring(m, pwent->pw_dir);
625 buffer_put_cstring(m, pwent->pw_shell);
628 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
629 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
631 /* For SSHv1 allow authentication now */
633 monitor_permit_authentications(1);
635 /* Allow service/style information on the auth context */
636 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
637 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
642 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
644 #ifdef SSH_AUDIT_EVENTS
645 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
651 int mm_answer_auth2_read_banner(int sock, Buffer *m)
656 banner = auth2_read_banner();
657 buffer_put_cstring(m, banner != NULL ? banner : "");
658 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
667 mm_answer_authserv(int sock, Buffer *m)
669 monitor_permit_authentications(1);
671 authctxt->service = buffer_get_string(m, NULL);
672 authctxt->style = buffer_get_string(m, NULL);
673 debug3("%s: service=%s, style=%s",
674 __func__, authctxt->service, authctxt->style);
676 if (strlen(authctxt->style) == 0) {
677 xfree(authctxt->style);
678 authctxt->style = NULL;
685 mm_answer_authpassword(int sock, Buffer *m)
687 static int call_count;
692 passwd = buffer_get_string(m, &plen);
693 /* Only authenticate if the context is valid */
694 authenticated = options.password_authentication &&
695 auth_password(authctxt, passwd);
696 memset(passwd, 0, strlen(passwd));
700 buffer_put_int(m, authenticated);
702 debug3("%s: sending result %d", __func__, authenticated);
703 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
706 if (plen == 0 && call_count == 1)
707 auth_method = "none";
709 auth_method = "password";
711 /* Causes monitor loop to terminate if authenticated */
712 return (authenticated);
717 mm_answer_bsdauthquery(int sock, Buffer *m)
719 char *name, *infotxt;
725 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
726 &prompts, &echo_on) < 0 ? 0 : 1;
729 buffer_put_int(m, success);
731 buffer_put_cstring(m, prompts[0]);
733 debug3("%s: sending challenge success: %u", __func__, success);
734 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
747 mm_answer_bsdauthrespond(int sock, Buffer *m)
752 if (authctxt->as == 0)
753 fatal("%s: no bsd auth session", __func__);
755 response = buffer_get_string(m, NULL);
756 authok = options.challenge_response_authentication &&
757 auth_userresponse(authctxt->as, response, 0);
759 debug3("%s: <%s> = <%d>", __func__, response, authok);
763 buffer_put_int(m, authok);
765 debug3("%s: sending authenticated: %d", __func__, authok);
766 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
768 auth_method = "bsdauth";
770 return (authok != 0);
776 mm_answer_skeyquery(int sock, Buffer *m)
779 char challenge[1024];
782 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
783 sizeof(challenge)) < 0 ? 0 : 1;
786 buffer_put_int(m, success);
788 buffer_put_cstring(m, challenge);
790 debug3("%s: sending challenge success: %u", __func__, success);
791 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
797 mm_answer_skeyrespond(int sock, Buffer *m)
802 response = buffer_get_string(m, NULL);
804 authok = (options.challenge_response_authentication &&
806 skey_haskey(authctxt->pw->pw_name) == 0 &&
807 skey_passcheck(authctxt->pw->pw_name, response) != -1);
812 buffer_put_int(m, authok);
814 debug3("%s: sending authenticated: %d", __func__, authok);
815 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
817 auth_method = "skey";
819 return (authok != 0);
825 mm_answer_pam_start(int sock, Buffer *m)
827 if (!options.use_pam)
828 fatal("UsePAM not set, but ended up in %s anyway", __func__);
832 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
838 mm_answer_pam_account(int sock, Buffer *m)
842 if (!options.use_pam)
843 fatal("UsePAM not set, but ended up in %s anyway", __func__);
845 ret = do_pam_account();
847 buffer_put_int(m, ret);
848 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
850 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
855 static void *sshpam_ctxt, *sshpam_authok;
856 extern KbdintDevice sshpam_device;
859 mm_answer_pam_init_ctx(int sock, Buffer *m)
862 debug3("%s", __func__);
863 authctxt->user = buffer_get_string(m, NULL);
864 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
865 sshpam_authok = NULL;
867 if (sshpam_ctxt != NULL) {
868 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
869 buffer_put_int(m, 1);
871 buffer_put_int(m, 0);
873 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
878 mm_answer_pam_query(int sock, Buffer *m)
880 char *name, *info, **prompts;
881 u_int i, num, *echo_on;
884 debug3("%s", __func__);
885 sshpam_authok = NULL;
886 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
887 if (ret == 0 && num == 0)
888 sshpam_authok = sshpam_ctxt;
889 if (num > 1 || name == NULL || info == NULL)
892 buffer_put_int(m, ret);
893 buffer_put_cstring(m, name);
895 buffer_put_cstring(m, info);
897 buffer_put_int(m, num);
898 for (i = 0; i < num; ++i) {
899 buffer_put_cstring(m, prompts[i]);
901 buffer_put_int(m, echo_on[i]);
907 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
912 mm_answer_pam_respond(int sock, Buffer *m)
918 debug3("%s", __func__);
919 sshpam_authok = NULL;
920 num = buffer_get_int(m);
922 resp = xmalloc(num * sizeof(char *));
923 for (i = 0; i < num; ++i)
924 resp[i] = buffer_get_string(m, NULL);
925 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
926 for (i = 0; i < num; ++i)
930 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
933 buffer_put_int(m, ret);
934 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
935 auth_method = "keyboard-interactive/pam";
937 sshpam_authok = sshpam_ctxt;
942 mm_answer_pam_free_ctx(int sock, Buffer *m)
945 debug3("%s", __func__);
946 (sshpam_device.free_ctx)(sshpam_ctxt);
948 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
949 return (sshpam_authok == sshpam_ctxt);
954 mm_append_debug(Buffer *m)
956 if (auth_debug_init && buffer_len(&auth_debug)) {
957 debug3("%s: Appending debug messages for child", __func__);
958 buffer_append(m, buffer_ptr(&auth_debug),
959 buffer_len(&auth_debug));
960 buffer_clear(&auth_debug);
965 mm_answer_keyallowed(int sock, Buffer *m)
971 enum mm_keytype type = 0;
974 debug3("%s entering", __func__);
976 type = buffer_get_int(m);
977 cuser = buffer_get_string(m, NULL);
978 chost = buffer_get_string(m, NULL);
979 blob = buffer_get_string(m, &bloblen);
981 key = key_from_blob(blob, bloblen);
983 if ((compat20 && type == MM_RSAHOSTKEY) ||
984 (!compat20 && type != MM_RSAHOSTKEY))
985 fatal("%s: key type and protocol mismatch", __func__);
987 debug3("%s: key_from_blob: %p", __func__, key);
989 if (key != NULL && authctxt->valid) {
992 allowed = options.pubkey_authentication &&
993 user_key_allowed(authctxt->pw, key);
996 allowed = options.hostbased_authentication &&
997 hostbased_key_allowed(authctxt->pw,
1001 key->type = KEY_RSA1; /* XXX */
1002 allowed = options.rhosts_rsa_authentication &&
1003 auth_rhosts_rsa_key_allowed(authctxt->pw,
1007 fatal("%s: unknown key type %d", __func__, type);
1014 /* clear temporarily storage (used by verify) */
1015 monitor_reset_key_state();
1018 /* Save temporarily for comparison in verify */
1020 key_bloblen = bloblen;
1021 key_blobtype = type;
1022 hostbased_cuser = cuser;
1023 hostbased_chost = chost;
1030 debug3("%s: key %p is %s",
1031 __func__, key, allowed ? "allowed" : "disallowed");
1034 buffer_put_int(m, allowed);
1035 buffer_put_int(m, forced_command != NULL);
1039 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1041 if (type == MM_RSAHOSTKEY)
1042 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1048 monitor_valid_userblob(u_char *data, u_int datalen)
1056 buffer_append(&b, data, datalen);
1058 if (datafellows & SSH_OLD_SESSIONID) {
1060 len = buffer_len(&b);
1061 if ((session_id2 == NULL) ||
1062 (len < session_id2_len) ||
1063 (memcmp(p, session_id2, session_id2_len) != 0))
1065 buffer_consume(&b, session_id2_len);
1067 p = buffer_get_string(&b, &len);
1068 if ((session_id2 == NULL) ||
1069 (len != session_id2_len) ||
1070 (memcmp(p, session_id2, session_id2_len) != 0))
1074 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1076 p = buffer_get_string(&b, NULL);
1077 if (strcmp(authctxt->user, p) != 0) {
1078 logit("wrong user name passed to monitor: expected %s != %.100s",
1083 buffer_skip_string(&b);
1084 if (datafellows & SSH_BUG_PKAUTH) {
1085 if (!buffer_get_char(&b))
1088 p = buffer_get_string(&b, NULL);
1089 if (strcmp("publickey", p) != 0)
1092 if (!buffer_get_char(&b))
1094 buffer_skip_string(&b);
1096 buffer_skip_string(&b);
1097 if (buffer_len(&b) != 0)
1104 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1113 buffer_append(&b, data, datalen);
1115 p = buffer_get_string(&b, &len);
1116 if ((session_id2 == NULL) ||
1117 (len != session_id2_len) ||
1118 (memcmp(p, session_id2, session_id2_len) != 0))
1122 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1124 p = buffer_get_string(&b, NULL);
1125 if (strcmp(authctxt->user, p) != 0) {
1126 logit("wrong user name passed to monitor: expected %s != %.100s",
1131 buffer_skip_string(&b); /* service */
1132 p = buffer_get_string(&b, NULL);
1133 if (strcmp(p, "hostbased") != 0)
1136 buffer_skip_string(&b); /* pkalg */
1137 buffer_skip_string(&b); /* pkblob */
1139 /* verify client host, strip trailing dot if necessary */
1140 p = buffer_get_string(&b, NULL);
1141 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1143 if (strcmp(p, chost) != 0)
1147 /* verify client user */
1148 p = buffer_get_string(&b, NULL);
1149 if (strcmp(p, cuser) != 0)
1153 if (buffer_len(&b) != 0)
1160 mm_answer_keyverify(int sock, Buffer *m)
1163 u_char *signature, *data, *blob;
1164 u_int signaturelen, datalen, bloblen;
1168 blob = buffer_get_string(m, &bloblen);
1169 signature = buffer_get_string(m, &signaturelen);
1170 data = buffer_get_string(m, &datalen);
1172 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1173 !monitor_allowed_key(blob, bloblen))
1174 fatal("%s: bad key, not previously allowed", __func__);
1176 key = key_from_blob(blob, bloblen);
1178 fatal("%s: bad public key blob", __func__);
1180 switch (key_blobtype) {
1182 valid_data = monitor_valid_userblob(data, datalen);
1185 valid_data = monitor_valid_hostbasedblob(data, datalen,
1186 hostbased_cuser, hostbased_chost);
1193 fatal("%s: bad signature data blob", __func__);
1195 verified = key_verify(key, signature, signaturelen, data, datalen);
1196 debug3("%s: key %p signature %s",
1197 __func__, key, verified ? "verified" : "unverified");
1204 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1206 monitor_reset_key_state();
1209 buffer_put_int(m, verified);
1210 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1216 mm_record_login(Session *s, struct passwd *pw)
1219 struct sockaddr_storage from;
1222 * Get IP address of client. If the connection is not a socket, let
1223 * the address be 0.0.0.0.
1225 memset(&from, 0, sizeof(from));
1226 fromlen = sizeof(from);
1227 if (packet_connection_is_on_socket()) {
1228 if (getpeername(packet_get_connection_in(),
1229 (struct sockaddr *) & from, &fromlen) < 0) {
1230 debug("getpeername: %.100s", strerror(errno));
1234 /* Record that there was a login on that tty from the remote host. */
1235 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1236 get_remote_name_or_ip(utmp_len, options.use_dns),
1237 (struct sockaddr *)&from, fromlen);
1241 mm_session_close(Session *s)
1243 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1244 if (s->ttyfd != -1) {
1245 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1246 session_pty_cleanup2(s);
1252 mm_answer_pty(int sock, Buffer *m)
1254 extern struct monitor *pmonitor;
1258 debug3("%s entering", __func__);
1264 s->authctxt = authctxt;
1265 s->pw = authctxt->pw;
1266 s->pid = pmonitor->m_pid;
1267 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1270 pty_setowner(authctxt->pw, s->tty);
1272 buffer_put_int(m, 1);
1273 buffer_put_cstring(m, s->tty);
1275 /* We need to trick ttyslot */
1276 if (dup2(s->ttyfd, 0) == -1)
1277 fatal("%s: dup2", __func__);
1279 mm_record_login(s, authctxt->pw);
1281 /* Now we can close the file descriptor again */
1284 /* send messages generated by record_login */
1285 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1286 buffer_clear(&loginmsg);
1288 mm_request_send(sock, MONITOR_ANS_PTY, m);
1290 mm_send_fd(sock, s->ptyfd);
1291 mm_send_fd(sock, s->ttyfd);
1293 /* make sure nothing uses fd 0 */
1294 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1295 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1297 error("%s: fd0 %d != 0", __func__, fd0);
1299 /* slave is not needed */
1301 s->ttyfd = s->ptyfd;
1302 /* no need to dup() because nobody closes ptyfd */
1303 s->ptymaster = s->ptyfd;
1305 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1311 mm_session_close(s);
1312 buffer_put_int(m, 0);
1313 mm_request_send(sock, MONITOR_ANS_PTY, m);
1318 mm_answer_pty_cleanup(int sock, Buffer *m)
1323 debug3("%s entering", __func__);
1325 tty = buffer_get_string(m, NULL);
1326 if ((s = session_by_tty(tty)) != NULL)
1327 mm_session_close(s);
1334 mm_answer_sesskey(int sock, Buffer *m)
1339 /* Turn off permissions */
1340 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1342 if ((p = BN_new()) == NULL)
1343 fatal("%s: BN_new", __func__);
1345 buffer_get_bignum2(m, p);
1347 rsafail = ssh1_session_key(p);
1350 buffer_put_int(m, rsafail);
1351 buffer_put_bignum2(m, p);
1355 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1357 /* Turn on permissions for sessid passing */
1358 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1364 mm_answer_sessid(int sock, Buffer *m)
1368 debug3("%s entering", __func__);
1370 if (buffer_len(m) != 16)
1371 fatal("%s: bad ssh1 session id", __func__);
1372 for (i = 0; i < 16; i++)
1373 session_id[i] = buffer_get_char(m);
1375 /* Turn on permissions for getpwnam */
1376 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1382 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1386 u_char *blob = NULL;
1390 debug3("%s entering", __func__);
1392 if (options.rsa_authentication && authctxt->valid) {
1393 if ((client_n = BN_new()) == NULL)
1394 fatal("%s: BN_new", __func__);
1395 buffer_get_bignum2(m, client_n);
1396 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1397 BN_clear_free(client_n);
1400 buffer_put_int(m, allowed);
1401 buffer_put_int(m, forced_command != NULL);
1403 /* clear temporarily storage (used by generate challenge) */
1404 monitor_reset_key_state();
1406 if (allowed && key != NULL) {
1407 key->type = KEY_RSA; /* cheat for key_to_blob */
1408 if (key_to_blob(key, &blob, &blen) == 0)
1409 fatal("%s: key_to_blob failed", __func__);
1410 buffer_put_string(m, blob, blen);
1412 /* Save temporarily for comparison in verify */
1415 key_blobtype = MM_RSAUSERKEY;
1422 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1424 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1425 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1430 mm_answer_rsa_challenge(int sock, Buffer *m)
1436 debug3("%s entering", __func__);
1438 if (!authctxt->valid)
1439 fatal("%s: authctxt not valid", __func__);
1440 blob = buffer_get_string(m, &blen);
1441 if (!monitor_allowed_key(blob, blen))
1442 fatal("%s: bad key, not previously allowed", __func__);
1443 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1444 fatal("%s: key type mismatch", __func__);
1445 if ((key = key_from_blob(blob, blen)) == NULL)
1446 fatal("%s: received bad key", __func__);
1449 BN_clear_free(ssh1_challenge);
1450 ssh1_challenge = auth_rsa_generate_challenge(key);
1453 buffer_put_bignum2(m, ssh1_challenge);
1455 debug3("%s sending reply", __func__);
1456 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1458 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1466 mm_answer_rsa_response(int sock, Buffer *m)
1469 u_char *blob, *response;
1473 debug3("%s entering", __func__);
1475 if (!authctxt->valid)
1476 fatal("%s: authctxt not valid", __func__);
1477 if (ssh1_challenge == NULL)
1478 fatal("%s: no ssh1_challenge", __func__);
1480 blob = buffer_get_string(m, &blen);
1481 if (!monitor_allowed_key(blob, blen))
1482 fatal("%s: bad key, not previously allowed", __func__);
1483 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1484 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1485 if ((key = key_from_blob(blob, blen)) == NULL)
1486 fatal("%s: received bad key", __func__);
1487 response = buffer_get_string(m, &len);
1489 fatal("%s: received bad response to challenge", __func__);
1490 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1496 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1499 BN_clear_free(ssh1_challenge);
1500 ssh1_challenge = NULL;
1501 monitor_reset_key_state();
1504 buffer_put_int(m, success);
1505 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1511 mm_answer_term(int sock, Buffer *req)
1513 extern struct monitor *pmonitor;
1516 debug3("%s: tearing down sessions", __func__);
1518 /* The child is terminating */
1519 session_destroy_all(&mm_session_close);
1521 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1525 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1527 /* Terminate process */
1531 #ifdef SSH_AUDIT_EVENTS
1532 /* Report that an audit event occurred */
1534 mm_answer_audit_event(int socket, Buffer *m)
1536 ssh_audit_event_t event;
1538 debug3("%s entering", __func__);
1540 event = buffer_get_int(m);
1542 case SSH_AUTH_FAIL_PUBKEY:
1543 case SSH_AUTH_FAIL_HOSTBASED:
1544 case SSH_AUTH_FAIL_GSSAPI:
1545 case SSH_LOGIN_EXCEED_MAXTRIES:
1546 case SSH_LOGIN_ROOT_DENIED:
1547 case SSH_CONNECTION_CLOSE:
1548 case SSH_INVALID_USER:
1552 fatal("Audit event type %d not permitted", event);
1559 mm_answer_audit_command(int socket, Buffer *m)
1564 debug3("%s entering", __func__);
1565 cmd = buffer_get_string(m, &len);
1566 /* sanity check command, if so how? */
1567 audit_run_command(cmd);
1571 #endif /* SSH_AUDIT_EVENTS */
1574 monitor_apply_keystate(struct monitor *pmonitor)
1577 set_newkeys(MODE_IN);
1578 set_newkeys(MODE_OUT);
1580 packet_set_protocol_flags(child_state.ssh1protoflags);
1581 packet_set_encryption_key(child_state.ssh1key,
1582 child_state.ssh1keylen, child_state.ssh1cipher);
1583 xfree(child_state.ssh1key);
1586 /* for rc4 and other stateful ciphers */
1587 packet_set_keycontext(MODE_OUT, child_state.keyout);
1588 xfree(child_state.keyout);
1589 packet_set_keycontext(MODE_IN, child_state.keyin);
1590 xfree(child_state.keyin);
1593 packet_set_iv(MODE_OUT, child_state.ivout);
1594 xfree(child_state.ivout);
1595 packet_set_iv(MODE_IN, child_state.ivin);
1596 xfree(child_state.ivin);
1599 memcpy(&incoming_stream, &child_state.incoming,
1600 sizeof(incoming_stream));
1601 memcpy(&outgoing_stream, &child_state.outgoing,
1602 sizeof(outgoing_stream));
1604 /* Update with new address */
1605 if (options.compression)
1606 mm_init_compression(pmonitor->m_zlib);
1608 /* Network I/O buffers */
1609 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1610 buffer_clear(&input);
1611 buffer_append(&input, child_state.input, child_state.ilen);
1612 memset(child_state.input, 0, child_state.ilen);
1613 xfree(child_state.input);
1615 buffer_clear(&output);
1616 buffer_append(&output, child_state.output, child_state.olen);
1617 memset(child_state.output, 0, child_state.olen);
1618 xfree(child_state.output);
1622 mm_get_kex(Buffer *m)
1628 kex = xmalloc(sizeof(*kex));
1629 memset(kex, 0, sizeof(*kex));
1630 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1631 if ((session_id2 == NULL) ||
1632 (kex->session_id_len != session_id2_len) ||
1633 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1634 fatal("mm_get_get: internal error: bad session id");
1635 kex->we_need = buffer_get_int(m);
1636 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1637 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1638 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1639 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1641 kex->hostkey_type = buffer_get_int(m);
1642 kex->kex_type = buffer_get_int(m);
1643 blob = buffer_get_string(m, &bloblen);
1644 buffer_init(&kex->my);
1645 buffer_append(&kex->my, blob, bloblen);
1647 blob = buffer_get_string(m, &bloblen);
1648 buffer_init(&kex->peer);
1649 buffer_append(&kex->peer, blob, bloblen);
1652 kex->flags = buffer_get_int(m);
1653 kex->client_version_string = buffer_get_string(m, NULL);
1654 kex->server_version_string = buffer_get_string(m, NULL);
1655 kex->load_host_key=&get_hostkey_by_type;
1656 kex->host_key_index=&get_hostkey_index;
1661 /* This function requries careful sanity checking */
1664 mm_get_keystate(struct monitor *pmonitor)
1668 u_int bloblen, plen;
1669 u_int32_t seqnr, packets;
1672 debug3("%s: Waiting for new keys", __func__);
1675 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1677 child_state.ssh1protoflags = buffer_get_int(&m);
1678 child_state.ssh1cipher = buffer_get_int(&m);
1679 child_state.ssh1key = buffer_get_string(&m,
1680 &child_state.ssh1keylen);
1681 child_state.ivout = buffer_get_string(&m,
1682 &child_state.ivoutlen);
1683 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1686 /* Get the Kex for rekeying */
1687 *pmonitor->m_pkex = mm_get_kex(&m);
1690 blob = buffer_get_string(&m, &bloblen);
1691 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1694 debug3("%s: Waiting for second key", __func__);
1695 blob = buffer_get_string(&m, &bloblen);
1696 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1699 /* Now get sequence numbers for the packets */
1700 seqnr = buffer_get_int(&m);
1701 blocks = buffer_get_int64(&m);
1702 packets = buffer_get_int(&m);
1703 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1704 seqnr = buffer_get_int(&m);
1705 blocks = buffer_get_int64(&m);
1706 packets = buffer_get_int(&m);
1707 packet_set_state(MODE_IN, seqnr, blocks, packets);
1710 /* Get the key context */
1711 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1712 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1714 debug3("%s: Getting compression state", __func__);
1715 /* Get compression state */
1716 p = buffer_get_string(&m, &plen);
1717 if (plen != sizeof(child_state.outgoing))
1718 fatal("%s: bad request size", __func__);
1719 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1722 p = buffer_get_string(&m, &plen);
1723 if (plen != sizeof(child_state.incoming))
1724 fatal("%s: bad request size", __func__);
1725 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1728 /* Network I/O buffers */
1729 debug3("%s: Getting Network I/O buffers", __func__);
1730 child_state.input = buffer_get_string(&m, &child_state.ilen);
1731 child_state.output = buffer_get_string(&m, &child_state.olen);
1737 /* Allocation functions for zlib */
1739 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1741 size_t len = (size_t) size * ncount;
1744 if (len == 0 || ncount > SIZE_T_MAX / size)
1745 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1747 address = mm_malloc(mm, len);
1753 mm_zfree(struct mm_master *mm, void *address)
1755 mm_free(mm, address);
1759 mm_init_compression(struct mm_master *mm)
1761 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1762 outgoing_stream.zfree = (free_func)mm_zfree;
1763 outgoing_stream.opaque = mm;
1765 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1766 incoming_stream.zfree = (free_func)mm_zfree;
1767 incoming_stream.opaque = mm;
1772 #define FD_CLOSEONEXEC(x) do { \
1773 if (fcntl(x, F_SETFD, 1) == -1) \
1774 fatal("fcntl(%d, F_SETFD)", x); \
1778 monitor_socketpair(int *pair)
1780 #ifdef HAVE_SOCKETPAIR
1781 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1782 fatal("%s: socketpair", __func__);
1784 fatal("%s: UsePrivilegeSeparation=yes not supported",
1787 FD_CLOSEONEXEC(pair[0]);
1788 FD_CLOSEONEXEC(pair[1]);
1791 #define MM_MEMSIZE 65536
1796 struct monitor *mon;
1799 mon = xmalloc(sizeof(*mon));
1802 monitor_socketpair(pair);
1804 mon->m_recvfd = pair[0];
1805 mon->m_sendfd = pair[1];
1807 /* Used to share zlib space across processes */
1808 if (options.compression) {
1809 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1810 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1812 /* Compression needs to share state across borders */
1813 mm_init_compression(mon->m_zlib);
1820 monitor_reinit(struct monitor *mon)
1824 monitor_socketpair(pair);
1826 mon->m_recvfd = pair[0];
1827 mon->m_sendfd = pair[1];
1832 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1838 goid.elements = buffer_get_string(m, &len);
1841 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1843 xfree(goid.elements);
1846 buffer_put_int(m, major);
1848 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1850 /* Now we have a context, enable the step */
1851 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1857 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1860 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1861 OM_uint32 major, minor;
1862 OM_uint32 flags = 0; /* GSI needs this */
1865 in.value = buffer_get_string(m, &len);
1867 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1871 buffer_put_int(m, major);
1872 buffer_put_string(m, out.value, out.length);
1873 buffer_put_int(m, flags);
1874 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1876 gss_release_buffer(&minor, &out);
1878 if (major == GSS_S_COMPLETE) {
1879 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1880 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1881 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1887 mm_answer_gss_checkmic(int sock, Buffer *m)
1889 gss_buffer_desc gssbuf, mic;
1893 gssbuf.value = buffer_get_string(m, &len);
1894 gssbuf.length = len;
1895 mic.value = buffer_get_string(m, &len);
1898 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1900 xfree(gssbuf.value);
1904 buffer_put_int(m, ret);
1906 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1908 if (!GSS_ERROR(ret))
1909 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1915 mm_answer_gss_userok(int sock, Buffer *m)
1919 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1922 buffer_put_int(m, authenticated);
1924 debug3("%s: sending result %d", __func__, authenticated);
1925 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1927 auth_method = "gssapi-with-mic";
1929 /* Monitor loop will terminate if authenticated */
1930 return (authenticated);