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.63 2005/03/10 22:01:05 deraadt Exp $");
31 #include <openssl/dh.h>
37 #define skeychallenge(k, u, c) opiechallenge((k), (u), (c))
38 #define skey_haskey(u) opie_haskey((u))
39 #define skey_passcheck(u, r) opie_passverify((u), (r))
49 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
52 #define TARGET_OS_MAC 1
57 #include "auth-options.h"
66 #include "monitor_mm.h"
67 #include "monitor_wrap.h"
68 #include "monitor_fdpass.h"
78 static Gssctxt *gsscontext = NULL;
82 extern ServerOptions options;
83 extern u_int utmp_len;
84 extern Newkeys *current_keys[];
85 extern z_stream incoming_stream;
86 extern z_stream outgoing_stream;
87 extern u_char session_id[];
88 extern Buffer input, output;
89 extern Buffer auth_debug;
90 extern int auth_debug_init;
91 extern Buffer loginmsg;
93 /* State exported from the child */
116 /* Functions on the monitor that answer unprivileged requests */
118 int mm_answer_moduli(int, Buffer *);
119 int mm_answer_sign(int, Buffer *);
120 int mm_answer_pwnamallow(int, Buffer *);
121 int mm_answer_auth2_read_banner(int, Buffer *);
122 int mm_answer_authserv(int, Buffer *);
123 int mm_answer_authpassword(int, Buffer *);
124 int mm_answer_bsdauthquery(int, Buffer *);
125 int mm_answer_bsdauthrespond(int, Buffer *);
126 int mm_answer_skeyquery(int, Buffer *);
127 int mm_answer_skeyrespond(int, Buffer *);
128 int mm_answer_keyallowed(int, Buffer *);
129 int mm_answer_keyverify(int, Buffer *);
130 int mm_answer_pty(int, Buffer *);
131 int mm_answer_pty_cleanup(int, Buffer *);
132 int mm_answer_term(int, Buffer *);
133 int mm_answer_rsa_keyallowed(int, Buffer *);
134 int mm_answer_rsa_challenge(int, Buffer *);
135 int mm_answer_rsa_response(int, Buffer *);
136 int mm_answer_sesskey(int, Buffer *);
137 int mm_answer_sessid(int, Buffer *);
140 int mm_answer_pam_start(int, Buffer *);
141 int mm_answer_pam_account(int, Buffer *);
142 int mm_answer_pam_init_ctx(int, Buffer *);
143 int mm_answer_pam_query(int, Buffer *);
144 int mm_answer_pam_respond(int, Buffer *);
145 int mm_answer_pam_free_ctx(int, Buffer *);
149 int mm_answer_gss_setup_ctx(int, Buffer *);
150 int mm_answer_gss_accept_ctx(int, Buffer *);
151 int mm_answer_gss_userok(int, Buffer *);
152 int mm_answer_gss_checkmic(int, Buffer *);
155 #ifdef SSH_AUDIT_EVENTS
156 int mm_answer_audit_event(int, Buffer *);
157 int mm_answer_audit_command(int, Buffer *);
160 static Authctxt *authctxt;
161 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
163 /* local state for key verify */
164 static u_char *key_blob = NULL;
165 static u_int key_bloblen = 0;
166 static int key_blobtype = MM_NOKEY;
167 static char *hostbased_cuser = NULL;
168 static char *hostbased_chost = NULL;
169 static char *auth_method = "unknown";
170 static u_int session_id2_len = 0;
171 static u_char *session_id2 = NULL;
172 static pid_t monitor_child_pid;
175 enum monitor_reqtype type;
177 int (*f)(int, Buffer *);
180 #define MON_ISAUTH 0x0004 /* Required for Authentication */
181 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
182 #define MON_ONCE 0x0010 /* Disable after calling */
184 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
186 #define MON_PERMIT 0x1000 /* Request is permitted */
188 struct mon_table mon_dispatch_proto20[] = {
189 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
190 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
191 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
192 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
193 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
194 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
196 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
197 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
198 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
199 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
200 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
201 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
203 #ifdef SSH_AUDIT_EVENTS
204 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
207 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
208 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
211 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
212 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
214 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
215 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
217 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
218 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
219 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
220 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
225 struct mon_table mon_dispatch_postauth20[] = {
226 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
227 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
228 {MONITOR_REQ_PTY, 0, mm_answer_pty},
229 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
230 {MONITOR_REQ_TERM, 0, mm_answer_term},
231 #ifdef SSH_AUDIT_EVENTS
232 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
233 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
238 struct mon_table mon_dispatch_proto15[] = {
239 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
240 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
241 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
242 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
243 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
244 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
245 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
246 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
248 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
249 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
252 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
253 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
256 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
257 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
258 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
259 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
260 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
261 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
263 #ifdef SSH_AUDIT_EVENTS
264 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
269 struct mon_table mon_dispatch_postauth15[] = {
270 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
271 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
272 {MONITOR_REQ_TERM, 0, mm_answer_term},
273 #ifdef SSH_AUDIT_EVENTS
274 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
275 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
280 struct mon_table *mon_dispatch;
282 /* Specifies if a certain message is allowed at the moment */
285 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
287 while (ent->f != NULL) {
288 if (ent->type == type) {
289 ent->flags &= ~MON_PERMIT;
290 ent->flags |= permit ? MON_PERMIT : 0;
298 monitor_permit_authentications(int permit)
300 struct mon_table *ent = mon_dispatch;
302 while (ent->f != NULL) {
303 if (ent->flags & MON_AUTH) {
304 ent->flags &= ~MON_PERMIT;
305 ent->flags |= permit ? MON_PERMIT : 0;
312 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
314 struct mon_table *ent;
315 int authenticated = 0;
317 debug3("preauth child monitor started");
319 authctxt = _authctxt;
320 memset(authctxt, 0, sizeof(*authctxt));
322 authctxt->loginmsg = &loginmsg;
325 mon_dispatch = mon_dispatch_proto20;
327 /* Permit requests for moduli and signatures */
328 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
329 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
331 mon_dispatch = mon_dispatch_proto15;
333 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
336 /* The first few requests do not require asynchronous access */
337 while (!authenticated) {
338 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
340 if (!(ent->flags & MON_AUTHDECIDE))
341 fatal("%s: unexpected authentication from %d",
342 __func__, ent->type);
343 if (authctxt->pw->pw_uid == 0 &&
344 !auth_root_allowed(auth_method))
347 /* PAM needs to perform account checks after auth */
348 if (options.use_pam && authenticated) {
352 mm_request_receive_expect(pmonitor->m_sendfd,
353 MONITOR_REQ_PAM_ACCOUNT, &m);
354 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
360 if (ent->flags & MON_AUTHDECIDE) {
361 auth_log(authctxt, authenticated, auth_method,
362 compat20 ? " ssh2" : "");
364 authctxt->failures++;
368 if (!authctxt->valid)
369 fatal("%s: authenticated invalid user", __func__);
371 debug("%s: %s has been authenticated by privileged process",
372 __func__, authctxt->user);
374 mm_get_keystate(pmonitor);
378 monitor_set_child_handler(pid_t pid)
380 monitor_child_pid = pid;
384 monitor_child_handler(int sig)
386 kill(monitor_child_pid, sig);
390 monitor_child_postauth(struct monitor *pmonitor)
392 monitor_set_child_handler(pmonitor->m_pid);
393 signal(SIGHUP, &monitor_child_handler);
394 signal(SIGTERM, &monitor_child_handler);
397 mon_dispatch = mon_dispatch_postauth20;
399 /* Permit requests for moduli and signatures */
400 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
401 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
402 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
404 mon_dispatch = mon_dispatch_postauth15;
405 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
408 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
409 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
413 monitor_read(pmonitor, mon_dispatch, NULL);
417 monitor_sync(struct monitor *pmonitor)
419 if (options.compression) {
420 /* The member allocation is not visible, so sync it */
421 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
426 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
427 struct mon_table **pent)
435 mm_request_receive(pmonitor->m_sendfd, &m);
436 type = buffer_get_char(&m);
438 debug3("%s: checking request %d", __func__, type);
440 while (ent->f != NULL) {
441 if (ent->type == type)
446 if (ent->f != NULL) {
447 if (!(ent->flags & MON_PERMIT))
448 fatal("%s: unpermitted request %d", __func__,
450 ret = (*ent->f)(pmonitor->m_sendfd, &m);
453 /* The child may use this request only once, disable it */
454 if (ent->flags & MON_ONCE) {
455 debug2("%s: %d used once, disabling now", __func__,
457 ent->flags &= ~MON_PERMIT;
466 fatal("%s: unsupported request: %d", __func__, type);
472 /* allowed key state */
474 monitor_allowed_key(u_char *blob, u_int bloblen)
476 /* make sure key is allowed */
477 if (key_blob == NULL || key_bloblen != bloblen ||
478 memcmp(key_blob, blob, key_bloblen))
484 monitor_reset_key_state(void)
487 if (key_blob != NULL)
489 if (hostbased_cuser != NULL)
490 xfree(hostbased_cuser);
491 if (hostbased_chost != NULL)
492 xfree(hostbased_chost);
495 key_blobtype = MM_NOKEY;
496 hostbased_cuser = NULL;
497 hostbased_chost = NULL;
501 mm_answer_moduli(int sock, Buffer *m)
506 min = buffer_get_int(m);
507 want = buffer_get_int(m);
508 max = buffer_get_int(m);
510 debug3("%s: got parameters: %d %d %d",
511 __func__, min, want, max);
512 /* We need to check here, too, in case the child got corrupted */
513 if (max < min || want < min || max < want)
514 fatal("%s: bad parameters: %d %d %d",
515 __func__, min, want, max);
519 dh = choose_dh(min, want, max);
521 buffer_put_char(m, 0);
524 /* Send first bignum */
525 buffer_put_char(m, 1);
526 buffer_put_bignum2(m, dh->p);
527 buffer_put_bignum2(m, dh->g);
531 mm_request_send(sock, MONITOR_ANS_MODULI, m);
536 mm_answer_sign(int sock, Buffer *m)
541 u_int siglen, datlen;
544 debug3("%s", __func__);
546 keyid = buffer_get_int(m);
547 p = buffer_get_string(m, &datlen);
550 fatal("%s: data length incorrect: %u", __func__, datlen);
552 /* save session id, it will be passed on the first call */
553 if (session_id2_len == 0) {
554 session_id2_len = datlen;
555 session_id2 = xmalloc(session_id2_len);
556 memcpy(session_id2, p, session_id2_len);
559 if ((key = get_hostkey_by_index(keyid)) == NULL)
560 fatal("%s: no hostkey from index %d", __func__, keyid);
561 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
562 fatal("%s: key_sign failed", __func__);
564 debug3("%s: signature %p(%u)", __func__, signature, siglen);
567 buffer_put_string(m, signature, siglen);
572 mm_request_send(sock, MONITOR_ANS_SIGN, m);
574 /* Turn on permissions for getpwnam */
575 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
580 /* Retrieves the password entry and also checks if the user is permitted */
583 mm_answer_pwnamallow(int sock, Buffer *m)
586 struct passwd *pwent;
589 debug3("%s", __func__);
591 if (authctxt->attempt++ != 0)
592 fatal("%s: multiple attempts for getpwnam", __func__);
594 username = buffer_get_string(m, NULL);
596 pwent = getpwnamallow(username);
598 authctxt->user = xstrdup(username);
599 setproctitle("%s [priv]", pwent ? username : "unknown");
605 buffer_put_char(m, 0);
606 authctxt->pw = fakepw();
611 authctxt->pw = pwent;
614 buffer_put_char(m, 1);
615 buffer_put_string(m, pwent, sizeof(struct passwd));
616 buffer_put_cstring(m, pwent->pw_name);
617 buffer_put_cstring(m, "*");
618 buffer_put_cstring(m, pwent->pw_gecos);
619 #ifdef HAVE_PW_CLASS_IN_PASSWD
620 buffer_put_cstring(m, pwent->pw_class);
622 buffer_put_cstring(m, pwent->pw_dir);
623 buffer_put_cstring(m, pwent->pw_shell);
626 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
627 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
629 /* For SSHv1 allow authentication now */
631 monitor_permit_authentications(1);
633 /* Allow service/style information on the auth context */
634 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
635 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
640 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
642 #ifdef SSH_AUDIT_EVENTS
643 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
649 int mm_answer_auth2_read_banner(int sock, Buffer *m)
654 banner = auth2_read_banner();
655 buffer_put_cstring(m, banner != NULL ? banner : "");
656 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
665 mm_answer_authserv(int sock, Buffer *m)
667 monitor_permit_authentications(1);
669 authctxt->service = buffer_get_string(m, NULL);
670 authctxt->style = buffer_get_string(m, NULL);
671 debug3("%s: service=%s, style=%s",
672 __func__, authctxt->service, authctxt->style);
674 if (strlen(authctxt->style) == 0) {
675 xfree(authctxt->style);
676 authctxt->style = NULL;
683 mm_answer_authpassword(int sock, Buffer *m)
685 static int call_count;
690 passwd = buffer_get_string(m, &plen);
691 /* Only authenticate if the context is valid */
692 authenticated = options.password_authentication &&
693 auth_password(authctxt, passwd);
694 memset(passwd, 0, strlen(passwd));
698 buffer_put_int(m, authenticated);
700 debug3("%s: sending result %d", __func__, authenticated);
701 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
704 if (plen == 0 && call_count == 1)
705 auth_method = "none";
707 auth_method = "password";
709 /* Causes monitor loop to terminate if authenticated */
710 return (authenticated);
715 mm_answer_bsdauthquery(int sock, Buffer *m)
717 char *name, *infotxt;
723 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
724 &prompts, &echo_on) < 0 ? 0 : 1;
727 buffer_put_int(m, success);
729 buffer_put_cstring(m, prompts[0]);
731 debug3("%s: sending challenge success: %u", __func__, success);
732 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
745 mm_answer_bsdauthrespond(int sock, Buffer *m)
750 if (authctxt->as == 0)
751 fatal("%s: no bsd auth session", __func__);
753 response = buffer_get_string(m, NULL);
754 authok = options.challenge_response_authentication &&
755 auth_userresponse(authctxt->as, response, 0);
757 debug3("%s: <%s> = <%d>", __func__, response, authok);
761 buffer_put_int(m, authok);
763 debug3("%s: sending authenticated: %d", __func__, authok);
764 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
766 auth_method = "bsdauth";
768 return (authok != 0);
774 mm_answer_skeyquery(int sock, Buffer *m)
777 char challenge[1024];
780 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
781 sizeof(challenge)) < 0 ? 0 : 1;
784 buffer_put_int(m, success);
786 buffer_put_cstring(m, challenge);
788 debug3("%s: sending challenge success: %u", __func__, success);
789 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
795 mm_answer_skeyrespond(int sock, Buffer *m)
800 response = buffer_get_string(m, NULL);
802 authok = (options.challenge_response_authentication &&
804 skey_haskey(authctxt->pw->pw_name) == 0 &&
805 skey_passcheck(authctxt->pw->pw_name, response) != -1);
810 buffer_put_int(m, authok);
812 debug3("%s: sending authenticated: %d", __func__, authok);
813 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
815 auth_method = "skey";
817 return (authok != 0);
823 mm_answer_pam_start(int sock, Buffer *m)
825 if (!options.use_pam)
826 fatal("UsePAM not set, but ended up in %s anyway", __func__);
830 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
836 mm_answer_pam_account(int sock, Buffer *m)
840 if (!options.use_pam)
841 fatal("UsePAM not set, but ended up in %s anyway", __func__);
843 ret = do_pam_account();
845 buffer_put_int(m, ret);
846 buffer_append(&loginmsg, "\0", 1);
847 buffer_put_cstring(m, buffer_ptr(&loginmsg));
848 buffer_clear(&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;
1026 debug3("%s: key %p is %s",
1027 __func__, key, allowed ? "allowed" : "disallowed");
1030 buffer_put_int(m, allowed);
1031 buffer_put_int(m, forced_command != NULL);
1035 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1037 if (type == MM_RSAHOSTKEY)
1038 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1044 monitor_valid_userblob(u_char *data, u_int datalen)
1052 buffer_append(&b, data, datalen);
1054 if (datafellows & SSH_OLD_SESSIONID) {
1056 len = buffer_len(&b);
1057 if ((session_id2 == NULL) ||
1058 (len < session_id2_len) ||
1059 (memcmp(p, session_id2, session_id2_len) != 0))
1061 buffer_consume(&b, session_id2_len);
1063 p = buffer_get_string(&b, &len);
1064 if ((session_id2 == NULL) ||
1065 (len != session_id2_len) ||
1066 (memcmp(p, session_id2, session_id2_len) != 0))
1070 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1072 p = buffer_get_string(&b, NULL);
1073 if (strcmp(authctxt->user, p) != 0) {
1074 logit("wrong user name passed to monitor: expected %s != %.100s",
1079 buffer_skip_string(&b);
1080 if (datafellows & SSH_BUG_PKAUTH) {
1081 if (!buffer_get_char(&b))
1084 p = buffer_get_string(&b, NULL);
1085 if (strcmp("publickey", p) != 0)
1088 if (!buffer_get_char(&b))
1090 buffer_skip_string(&b);
1092 buffer_skip_string(&b);
1093 if (buffer_len(&b) != 0)
1100 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1109 buffer_append(&b, data, datalen);
1111 p = buffer_get_string(&b, &len);
1112 if ((session_id2 == NULL) ||
1113 (len != session_id2_len) ||
1114 (memcmp(p, session_id2, session_id2_len) != 0))
1118 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1120 p = buffer_get_string(&b, NULL);
1121 if (strcmp(authctxt->user, p) != 0) {
1122 logit("wrong user name passed to monitor: expected %s != %.100s",
1127 buffer_skip_string(&b); /* service */
1128 p = buffer_get_string(&b, NULL);
1129 if (strcmp(p, "hostbased") != 0)
1132 buffer_skip_string(&b); /* pkalg */
1133 buffer_skip_string(&b); /* pkblob */
1135 /* verify client host, strip trailing dot if necessary */
1136 p = buffer_get_string(&b, NULL);
1137 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1139 if (strcmp(p, chost) != 0)
1143 /* verify client user */
1144 p = buffer_get_string(&b, NULL);
1145 if (strcmp(p, cuser) != 0)
1149 if (buffer_len(&b) != 0)
1156 mm_answer_keyverify(int sock, Buffer *m)
1159 u_char *signature, *data, *blob;
1160 u_int signaturelen, datalen, bloblen;
1164 blob = buffer_get_string(m, &bloblen);
1165 signature = buffer_get_string(m, &signaturelen);
1166 data = buffer_get_string(m, &datalen);
1168 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1169 !monitor_allowed_key(blob, bloblen))
1170 fatal("%s: bad key, not previously allowed", __func__);
1172 key = key_from_blob(blob, bloblen);
1174 fatal("%s: bad public key blob", __func__);
1176 switch (key_blobtype) {
1178 valid_data = monitor_valid_userblob(data, datalen);
1181 valid_data = monitor_valid_hostbasedblob(data, datalen,
1182 hostbased_cuser, hostbased_chost);
1189 fatal("%s: bad signature data blob", __func__);
1191 verified = key_verify(key, signature, signaturelen, data, datalen);
1192 debug3("%s: key %p signature %s",
1193 __func__, key, verified ? "verified" : "unverified");
1200 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1202 monitor_reset_key_state();
1205 buffer_put_int(m, verified);
1206 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1212 mm_record_login(Session *s, struct passwd *pw)
1215 struct sockaddr_storage from;
1218 * Get IP address of client. If the connection is not a socket, let
1219 * the address be 0.0.0.0.
1221 memset(&from, 0, sizeof(from));
1222 fromlen = sizeof(from);
1223 if (packet_connection_is_on_socket()) {
1224 if (getpeername(packet_get_connection_in(),
1225 (struct sockaddr *) & from, &fromlen) < 0) {
1226 debug("getpeername: %.100s", strerror(errno));
1230 /* Record that there was a login on that tty from the remote host. */
1231 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1232 get_remote_name_or_ip(utmp_len, options.use_dns),
1233 (struct sockaddr *)&from, fromlen);
1237 mm_session_close(Session *s)
1239 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1240 if (s->ttyfd != -1) {
1241 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1242 session_pty_cleanup2(s);
1248 mm_answer_pty(int sock, Buffer *m)
1250 extern struct monitor *pmonitor;
1254 debug3("%s entering", __func__);
1260 s->authctxt = authctxt;
1261 s->pw = authctxt->pw;
1262 s->pid = pmonitor->m_pid;
1263 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1266 pty_setowner(authctxt->pw, s->tty);
1268 buffer_put_int(m, 1);
1269 buffer_put_cstring(m, s->tty);
1271 /* We need to trick ttyslot */
1272 if (dup2(s->ttyfd, 0) == -1)
1273 fatal("%s: dup2", __func__);
1275 mm_record_login(s, authctxt->pw);
1277 /* Now we can close the file descriptor again */
1280 /* send messages generated by record_login */
1281 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1282 buffer_clear(&loginmsg);
1284 mm_request_send(sock, MONITOR_ANS_PTY, m);
1286 mm_send_fd(sock, s->ptyfd);
1287 mm_send_fd(sock, s->ttyfd);
1289 /* make sure nothing uses fd 0 */
1290 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1291 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1293 error("%s: fd0 %d != 0", __func__, fd0);
1295 /* slave is not needed */
1297 s->ttyfd = s->ptyfd;
1298 /* no need to dup() because nobody closes ptyfd */
1299 s->ptymaster = s->ptyfd;
1301 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1307 mm_session_close(s);
1308 buffer_put_int(m, 0);
1309 mm_request_send(sock, MONITOR_ANS_PTY, m);
1314 mm_answer_pty_cleanup(int sock, Buffer *m)
1319 debug3("%s entering", __func__);
1321 tty = buffer_get_string(m, NULL);
1322 if ((s = session_by_tty(tty)) != NULL)
1323 mm_session_close(s);
1330 mm_answer_sesskey(int sock, Buffer *m)
1335 /* Turn off permissions */
1336 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1338 if ((p = BN_new()) == NULL)
1339 fatal("%s: BN_new", __func__);
1341 buffer_get_bignum2(m, p);
1343 rsafail = ssh1_session_key(p);
1346 buffer_put_int(m, rsafail);
1347 buffer_put_bignum2(m, p);
1351 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1353 /* Turn on permissions for sessid passing */
1354 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1360 mm_answer_sessid(int sock, Buffer *m)
1364 debug3("%s entering", __func__);
1366 if (buffer_len(m) != 16)
1367 fatal("%s: bad ssh1 session id", __func__);
1368 for (i = 0; i < 16; i++)
1369 session_id[i] = buffer_get_char(m);
1371 /* Turn on permissions for getpwnam */
1372 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1378 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1382 u_char *blob = NULL;
1386 debug3("%s entering", __func__);
1388 if (options.rsa_authentication && authctxt->valid) {
1389 if ((client_n = BN_new()) == NULL)
1390 fatal("%s: BN_new", __func__);
1391 buffer_get_bignum2(m, client_n);
1392 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1393 BN_clear_free(client_n);
1396 buffer_put_int(m, allowed);
1397 buffer_put_int(m, forced_command != NULL);
1399 /* clear temporarily storage (used by generate challenge) */
1400 monitor_reset_key_state();
1402 if (allowed && key != NULL) {
1403 key->type = KEY_RSA; /* cheat for key_to_blob */
1404 if (key_to_blob(key, &blob, &blen) == 0)
1405 fatal("%s: key_to_blob failed", __func__);
1406 buffer_put_string(m, blob, blen);
1408 /* Save temporarily for comparison in verify */
1411 key_blobtype = MM_RSAUSERKEY;
1418 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1420 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1421 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1426 mm_answer_rsa_challenge(int sock, Buffer *m)
1432 debug3("%s entering", __func__);
1434 if (!authctxt->valid)
1435 fatal("%s: authctxt not valid", __func__);
1436 blob = buffer_get_string(m, &blen);
1437 if (!monitor_allowed_key(blob, blen))
1438 fatal("%s: bad key, not previously allowed", __func__);
1439 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1440 fatal("%s: key type mismatch", __func__);
1441 if ((key = key_from_blob(blob, blen)) == NULL)
1442 fatal("%s: received bad key", __func__);
1445 BN_clear_free(ssh1_challenge);
1446 ssh1_challenge = auth_rsa_generate_challenge(key);
1449 buffer_put_bignum2(m, ssh1_challenge);
1451 debug3("%s sending reply", __func__);
1452 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1454 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1462 mm_answer_rsa_response(int sock, Buffer *m)
1465 u_char *blob, *response;
1469 debug3("%s entering", __func__);
1471 if (!authctxt->valid)
1472 fatal("%s: authctxt not valid", __func__);
1473 if (ssh1_challenge == NULL)
1474 fatal("%s: no ssh1_challenge", __func__);
1476 blob = buffer_get_string(m, &blen);
1477 if (!monitor_allowed_key(blob, blen))
1478 fatal("%s: bad key, not previously allowed", __func__);
1479 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1480 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1481 if ((key = key_from_blob(blob, blen)) == NULL)
1482 fatal("%s: received bad key", __func__);
1483 response = buffer_get_string(m, &len);
1485 fatal("%s: received bad response to challenge", __func__);
1486 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1492 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1495 BN_clear_free(ssh1_challenge);
1496 ssh1_challenge = NULL;
1497 monitor_reset_key_state();
1500 buffer_put_int(m, success);
1501 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1507 mm_answer_term(int sock, Buffer *req)
1509 extern struct monitor *pmonitor;
1512 debug3("%s: tearing down sessions", __func__);
1514 /* The child is terminating */
1515 session_destroy_all(&mm_session_close);
1517 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1521 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1523 /* Terminate process */
1527 #ifdef SSH_AUDIT_EVENTS
1528 /* Report that an audit event occurred */
1530 mm_answer_audit_event(int socket, Buffer *m)
1532 ssh_audit_event_t event;
1534 debug3("%s entering", __func__);
1536 event = buffer_get_int(m);
1538 case SSH_AUTH_FAIL_PUBKEY:
1539 case SSH_AUTH_FAIL_HOSTBASED:
1540 case SSH_AUTH_FAIL_GSSAPI:
1541 case SSH_LOGIN_EXCEED_MAXTRIES:
1542 case SSH_LOGIN_ROOT_DENIED:
1543 case SSH_CONNECTION_CLOSE:
1544 case SSH_INVALID_USER:
1548 fatal("Audit event type %d not permitted", event);
1555 mm_answer_audit_command(int socket, Buffer *m)
1560 debug3("%s entering", __func__);
1561 cmd = buffer_get_string(m, &len);
1562 /* sanity check command, if so how? */
1563 audit_run_command(cmd);
1567 #endif /* SSH_AUDIT_EVENTS */
1570 monitor_apply_keystate(struct monitor *pmonitor)
1573 set_newkeys(MODE_IN);
1574 set_newkeys(MODE_OUT);
1576 packet_set_protocol_flags(child_state.ssh1protoflags);
1577 packet_set_encryption_key(child_state.ssh1key,
1578 child_state.ssh1keylen, child_state.ssh1cipher);
1579 xfree(child_state.ssh1key);
1582 /* for rc4 and other stateful ciphers */
1583 packet_set_keycontext(MODE_OUT, child_state.keyout);
1584 xfree(child_state.keyout);
1585 packet_set_keycontext(MODE_IN, child_state.keyin);
1586 xfree(child_state.keyin);
1589 packet_set_iv(MODE_OUT, child_state.ivout);
1590 xfree(child_state.ivout);
1591 packet_set_iv(MODE_IN, child_state.ivin);
1592 xfree(child_state.ivin);
1595 memcpy(&incoming_stream, &child_state.incoming,
1596 sizeof(incoming_stream));
1597 memcpy(&outgoing_stream, &child_state.outgoing,
1598 sizeof(outgoing_stream));
1600 /* Update with new address */
1601 if (options.compression)
1602 mm_init_compression(pmonitor->m_zlib);
1604 /* Network I/O buffers */
1605 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1606 buffer_clear(&input);
1607 buffer_append(&input, child_state.input, child_state.ilen);
1608 memset(child_state.input, 0, child_state.ilen);
1609 xfree(child_state.input);
1611 buffer_clear(&output);
1612 buffer_append(&output, child_state.output, child_state.olen);
1613 memset(child_state.output, 0, child_state.olen);
1614 xfree(child_state.output);
1618 mm_get_kex(Buffer *m)
1624 kex = xmalloc(sizeof(*kex));
1625 memset(kex, 0, sizeof(*kex));
1626 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1627 if ((session_id2 == NULL) ||
1628 (kex->session_id_len != session_id2_len) ||
1629 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1630 fatal("mm_get_get: internal error: bad session id");
1631 kex->we_need = buffer_get_int(m);
1632 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1633 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1634 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1636 kex->hostkey_type = buffer_get_int(m);
1637 kex->kex_type = buffer_get_int(m);
1638 blob = buffer_get_string(m, &bloblen);
1639 buffer_init(&kex->my);
1640 buffer_append(&kex->my, blob, bloblen);
1642 blob = buffer_get_string(m, &bloblen);
1643 buffer_init(&kex->peer);
1644 buffer_append(&kex->peer, blob, bloblen);
1647 kex->flags = buffer_get_int(m);
1648 kex->client_version_string = buffer_get_string(m, NULL);
1649 kex->server_version_string = buffer_get_string(m, NULL);
1650 kex->load_host_key=&get_hostkey_by_type;
1651 kex->host_key_index=&get_hostkey_index;
1656 /* This function requries careful sanity checking */
1659 mm_get_keystate(struct monitor *pmonitor)
1663 u_int bloblen, plen;
1664 u_int32_t seqnr, packets;
1667 debug3("%s: Waiting for new keys", __func__);
1670 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1672 child_state.ssh1protoflags = buffer_get_int(&m);
1673 child_state.ssh1cipher = buffer_get_int(&m);
1674 child_state.ssh1key = buffer_get_string(&m,
1675 &child_state.ssh1keylen);
1676 child_state.ivout = buffer_get_string(&m,
1677 &child_state.ivoutlen);
1678 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1681 /* Get the Kex for rekeying */
1682 *pmonitor->m_pkex = mm_get_kex(&m);
1685 blob = buffer_get_string(&m, &bloblen);
1686 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1689 debug3("%s: Waiting for second key", __func__);
1690 blob = buffer_get_string(&m, &bloblen);
1691 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1694 /* Now get sequence numbers for the packets */
1695 seqnr = buffer_get_int(&m);
1696 blocks = buffer_get_int64(&m);
1697 packets = buffer_get_int(&m);
1698 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1699 seqnr = buffer_get_int(&m);
1700 blocks = buffer_get_int64(&m);
1701 packets = buffer_get_int(&m);
1702 packet_set_state(MODE_IN, seqnr, blocks, packets);
1705 /* Get the key context */
1706 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1707 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1709 debug3("%s: Getting compression state", __func__);
1710 /* Get compression state */
1711 p = buffer_get_string(&m, &plen);
1712 if (plen != sizeof(child_state.outgoing))
1713 fatal("%s: bad request size", __func__);
1714 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1717 p = buffer_get_string(&m, &plen);
1718 if (plen != sizeof(child_state.incoming))
1719 fatal("%s: bad request size", __func__);
1720 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1723 /* Network I/O buffers */
1724 debug3("%s: Getting Network I/O buffers", __func__);
1725 child_state.input = buffer_get_string(&m, &child_state.ilen);
1726 child_state.output = buffer_get_string(&m, &child_state.olen);
1732 /* Allocation functions for zlib */
1734 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1736 size_t len = (size_t) size * ncount;
1739 if (len == 0 || ncount > SIZE_T_MAX / size)
1740 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1742 address = mm_malloc(mm, len);
1748 mm_zfree(struct mm_master *mm, void *address)
1750 mm_free(mm, address);
1754 mm_init_compression(struct mm_master *mm)
1756 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1757 outgoing_stream.zfree = (free_func)mm_zfree;
1758 outgoing_stream.opaque = mm;
1760 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1761 incoming_stream.zfree = (free_func)mm_zfree;
1762 incoming_stream.opaque = mm;
1767 #define FD_CLOSEONEXEC(x) do { \
1768 if (fcntl(x, F_SETFD, 1) == -1) \
1769 fatal("fcntl(%d, F_SETFD)", x); \
1773 monitor_socketpair(int *pair)
1775 #ifdef HAVE_SOCKETPAIR
1776 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1777 fatal("%s: socketpair", __func__);
1779 fatal("%s: UsePrivilegeSeparation=yes not supported",
1782 FD_CLOSEONEXEC(pair[0]);
1783 FD_CLOSEONEXEC(pair[1]);
1786 #define MM_MEMSIZE 65536
1791 struct monitor *mon;
1794 mon = xmalloc(sizeof(*mon));
1797 monitor_socketpair(pair);
1799 mon->m_recvfd = pair[0];
1800 mon->m_sendfd = pair[1];
1802 /* Used to share zlib space across processes */
1803 if (options.compression) {
1804 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1805 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1807 /* Compression needs to share state across borders */
1808 mm_init_compression(mon->m_zlib);
1815 monitor_reinit(struct monitor *mon)
1819 monitor_socketpair(pair);
1821 mon->m_recvfd = pair[0];
1822 mon->m_sendfd = pair[1];
1827 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1833 goid.elements = buffer_get_string(m, &len);
1836 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1838 xfree(goid.elements);
1841 buffer_put_int(m, major);
1843 mm_request_send(sock,MONITOR_ANS_GSSSETUP, m);
1845 /* Now we have a context, enable the step */
1846 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1852 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1855 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1856 OM_uint32 major,minor;
1857 OM_uint32 flags = 0; /* GSI needs this */
1860 in.value = buffer_get_string(m, &len);
1862 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1866 buffer_put_int(m, major);
1867 buffer_put_string(m, out.value, out.length);
1868 buffer_put_int(m, flags);
1869 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1871 gss_release_buffer(&minor, &out);
1873 if (major==GSS_S_COMPLETE) {
1874 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1875 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1876 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1882 mm_answer_gss_checkmic(int sock, Buffer *m)
1884 gss_buffer_desc gssbuf, mic;
1888 gssbuf.value = buffer_get_string(m, &len);
1889 gssbuf.length = len;
1890 mic.value = buffer_get_string(m, &len);
1893 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1895 xfree(gssbuf.value);
1899 buffer_put_int(m, ret);
1901 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1903 if (!GSS_ERROR(ret))
1904 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1910 mm_answer_gss_userok(int sock, Buffer *m)
1914 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1917 buffer_put_int(m, authenticated);
1919 debug3("%s: sending result %d", __func__, authenticated);
1920 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1922 auth_method="gssapi-with-mic";
1924 /* Monitor loop will terminate if authenticated */
1925 return (authenticated);