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.55 2004/02/05 05:37:17 dtucker Exp $");
30 #include <openssl/dh.h>
40 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
43 #define TARGET_OS_MAC 1
48 #include "auth-options.h"
57 #include "monitor_mm.h"
58 #include "monitor_wrap.h"
59 #include "monitor_fdpass.h"
70 static Gssctxt *gsscontext = NULL;
74 extern ServerOptions options;
75 extern u_int utmp_len;
76 extern Newkeys *current_keys[];
77 extern z_stream incoming_stream;
78 extern z_stream outgoing_stream;
79 extern u_char session_id[];
80 extern Buffer input, output;
81 extern Buffer auth_debug;
82 extern int auth_debug_init;
84 /* State exported from the child */
107 /* Functions on the monitor that answer unprivileged requests */
109 int mm_answer_moduli(int, Buffer *);
110 int mm_answer_sign(int, Buffer *);
111 int mm_answer_pwnamallow(int, Buffer *);
112 int mm_answer_auth2_read_banner(int, Buffer *);
113 int mm_answer_authserv(int, Buffer *);
114 int mm_answer_authpassword(int, Buffer *);
115 int mm_answer_bsdauthquery(int, Buffer *);
116 int mm_answer_bsdauthrespond(int, Buffer *);
117 int mm_answer_skeyquery(int, Buffer *);
118 int mm_answer_skeyrespond(int, Buffer *);
119 int mm_answer_keyallowed(int, Buffer *);
120 int mm_answer_keyverify(int, Buffer *);
121 int mm_answer_pty(int, Buffer *);
122 int mm_answer_pty_cleanup(int, Buffer *);
123 int mm_answer_term(int, Buffer *);
124 int mm_answer_rsa_keyallowed(int, Buffer *);
125 int mm_answer_rsa_challenge(int, Buffer *);
126 int mm_answer_rsa_response(int, Buffer *);
127 int mm_answer_sesskey(int, Buffer *);
128 int mm_answer_sessid(int, Buffer *);
131 int mm_answer_pam_start(int, Buffer *);
132 int mm_answer_pam_account(int, Buffer *);
133 int mm_answer_pam_init_ctx(int, Buffer *);
134 int mm_answer_pam_query(int, Buffer *);
135 int mm_answer_pam_respond(int, Buffer *);
136 int mm_answer_pam_free_ctx(int, Buffer *);
140 int mm_answer_gss_setup_ctx(int, Buffer *);
141 int mm_answer_gss_accept_ctx(int, Buffer *);
142 int mm_answer_gss_userok(int, Buffer *);
143 int mm_answer_gss_checkmic(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_sign(int, Buffer *);
151 int mm_answer_gss_error(int, Buffer *);
152 int mm_answer_gss_indicate_mechs(int, Buffer *);
153 int mm_answer_gss_localname(int, Buffer *);
156 static Authctxt *authctxt;
157 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
159 /* local state for key verify */
160 static u_char *key_blob = NULL;
161 static u_int key_bloblen = 0;
162 static int key_blobtype = MM_NOKEY;
163 static char *hostbased_cuser = NULL;
164 static char *hostbased_chost = NULL;
165 static char *auth_method = "unknown";
166 static u_int session_id2_len = 0;
167 static u_char *session_id2 = NULL;
168 static pid_t monitor_child_pid;
171 enum monitor_reqtype type;
173 int (*f)(int, Buffer *);
176 #define MON_ISAUTH 0x0004 /* Required for Authentication */
177 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
178 #define MON_ONCE 0x0010 /* Disable after calling */
180 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
182 #define MON_PERMIT 0x1000 /* Request is permitted */
184 struct mon_table mon_dispatch_proto20[] = {
185 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
186 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
187 {MONITOR_REQ_PWNAM, MON_AUTH, mm_answer_pwnamallow},
188 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
189 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
190 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
192 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
193 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
194 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
195 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
196 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
197 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
200 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
201 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
204 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
205 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
207 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
208 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
210 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
211 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
212 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
213 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
214 {MONITOR_REQ_GSSERR, MON_ISAUTH | MON_ONCE, mm_answer_gss_error},
215 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
216 {MONITOR_REQ_GSSLOCALNAME, MON_ISAUTH, mm_answer_gss_localname},
217 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
222 struct mon_table mon_dispatch_postauth20[] = {
224 {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
225 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
226 {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
227 {MONITOR_REQ_GSSERR, 0, mm_answer_gss_error},
228 {MONITOR_REQ_GSSMECHS, 0, mm_answer_gss_indicate_mechs},
230 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
231 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
232 {MONITOR_REQ_PTY, 0, mm_answer_pty},
233 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
234 {MONITOR_REQ_TERM, 0, mm_answer_term},
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_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
257 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
258 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
259 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
260 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
263 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
264 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
265 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
266 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
267 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
268 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
273 struct mon_table mon_dispatch_postauth15[] = {
274 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
275 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
276 {MONITOR_REQ_TERM, 0, mm_answer_term},
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));
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 /* and for the GSSAPI key exchange */
330 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
331 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
332 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS, 1);
335 mon_dispatch = mon_dispatch_proto15;
337 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
340 /* The first few requests do not require asynchronous access */
341 while (!authenticated) {
342 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
344 if (!(ent->flags & MON_AUTHDECIDE))
345 fatal("%s: unexpected authentication from %d",
346 __func__, ent->type);
347 if (authctxt->pw->pw_uid == 0 &&
348 !auth_root_allowed(auth_method))
351 /* PAM needs to perform account checks after auth */
352 if (options.use_pam && authenticated) {
356 mm_request_receive_expect(pmonitor->m_sendfd,
357 MONITOR_REQ_PAM_ACCOUNT, &m);
358 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
364 if (ent->flags & MON_AUTHDECIDE) {
365 auth_log(authctxt, authenticated, auth_method,
366 compat20 ? " ssh2" : "");
368 authctxt->failures++;
372 if (!authctxt->valid)
373 fatal("%s: authenticated invalid user", __func__);
375 debug("%s: %s has been authenticated by privileged process",
376 __func__, authctxt->user);
378 mm_get_keystate(pmonitor);
382 monitor_set_child_handler(pid_t pid)
384 monitor_child_pid = pid;
388 monitor_child_handler(int signal)
390 kill(monitor_child_pid, signal);
394 monitor_child_postauth(struct monitor *pmonitor)
396 monitor_set_child_handler(pmonitor->m_pid);
397 signal(SIGHUP, &monitor_child_handler);
398 signal(SIGTERM, &monitor_child_handler);
401 mon_dispatch = mon_dispatch_postauth20;
403 /* Permit requests for moduli and signatures */
404 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
405 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
406 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
409 /* and for the GSSAPI key exchange */
410 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS,1);
411 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP,1);
412 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR,1);
416 mon_dispatch = mon_dispatch_postauth15;
417 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
420 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
423 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
424 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
428 monitor_read(pmonitor, mon_dispatch, NULL);
432 monitor_sync(struct monitor *pmonitor)
434 if (options.compression) {
435 /* The member allocation is not visible, so sync it */
436 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
441 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
442 struct mon_table **pent)
450 mm_request_receive(pmonitor->m_sendfd, &m);
451 type = buffer_get_char(&m);
453 debug3("%s: checking request %d", __func__, type);
455 while (ent->f != NULL) {
456 if (ent->type == type)
461 if (ent->f != NULL) {
462 if (!(ent->flags & MON_PERMIT))
463 fatal("%s: unpermitted request %d", __func__,
465 ret = (*ent->f)(pmonitor->m_sendfd, &m);
468 /* The child may use this request only once, disable it */
469 if (ent->flags & MON_ONCE) {
470 debug2("%s: %d used once, disabling now", __func__,
472 ent->flags &= ~MON_PERMIT;
481 fatal("%s: unsupported request: %d", __func__, type);
487 /* allowed key state */
489 monitor_allowed_key(u_char *blob, u_int bloblen)
491 /* make sure key is allowed */
492 if (key_blob == NULL || key_bloblen != bloblen ||
493 memcmp(key_blob, blob, key_bloblen))
499 monitor_reset_key_state(void)
502 if (key_blob != NULL)
504 if (hostbased_cuser != NULL)
505 xfree(hostbased_cuser);
506 if (hostbased_chost != NULL)
507 xfree(hostbased_chost);
510 key_blobtype = MM_NOKEY;
511 hostbased_cuser = NULL;
512 hostbased_chost = NULL;
516 mm_answer_moduli(int socket, Buffer *m)
521 min = buffer_get_int(m);
522 want = buffer_get_int(m);
523 max = buffer_get_int(m);
525 debug3("%s: got parameters: %d %d %d",
526 __func__, min, want, max);
527 /* We need to check here, too, in case the child got corrupted */
528 if (max < min || want < min || max < want)
529 fatal("%s: bad parameters: %d %d %d",
530 __func__, min, want, max);
534 dh = choose_dh(min, want, max);
536 buffer_put_char(m, 0);
539 /* Send first bignum */
540 buffer_put_char(m, 1);
541 buffer_put_bignum2(m, dh->p);
542 buffer_put_bignum2(m, dh->g);
546 mm_request_send(socket, MONITOR_ANS_MODULI, m);
551 mm_answer_sign(int socket, Buffer *m)
556 u_int siglen, datlen;
559 debug3("%s", __func__);
561 keyid = buffer_get_int(m);
562 p = buffer_get_string(m, &datlen);
565 fatal("%s: data length incorrect: %u", __func__, datlen);
567 /* save session id, it will be passed on the first call */
568 if (session_id2_len == 0) {
569 session_id2_len = datlen;
570 session_id2 = xmalloc(session_id2_len);
571 memcpy(session_id2, p, session_id2_len);
574 if ((key = get_hostkey_by_index(keyid)) == NULL)
575 fatal("%s: no hostkey from index %d", __func__, keyid);
576 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
577 fatal("%s: key_sign failed", __func__);
579 debug3("%s: signature %p(%u)", __func__, signature, siglen);
582 buffer_put_string(m, signature, siglen);
587 mm_request_send(socket, MONITOR_ANS_SIGN, m);
589 /* Turn on permissions for getpwnam */
590 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
595 /* Retrieves the password entry and also checks if the user is permitted */
598 mm_answer_pwnamallow(int socket, Buffer *m)
601 struct passwd *pwent;
604 debug3("%s", __func__);
606 login = buffer_get_string(m, NULL);
608 pwent = getpwnamallow(login);
610 if (authctxt->user) xfree(authctxt->user);
611 authctxt->user = xstrdup(login);
612 setproctitle("%s [priv]", pwent ? login : "unknown");
618 buffer_put_char(m, 0);
619 authctxt->pw = fakepw();
624 authctxt->pw = pwent;
627 buffer_put_char(m, 1);
628 buffer_put_string(m, pwent, sizeof(struct passwd));
629 buffer_put_cstring(m, pwent->pw_name);
630 buffer_put_cstring(m, "*");
631 buffer_put_cstring(m, pwent->pw_gecos);
632 #ifdef HAVE_PW_CLASS_IN_PASSWD
633 buffer_put_cstring(m, pwent->pw_class);
635 buffer_put_cstring(m, pwent->pw_dir);
636 buffer_put_cstring(m, pwent->pw_shell);
639 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
640 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
642 /* For SSHv1 allow authentication now */
644 monitor_permit_authentications(1);
646 /* Allow service/style information on the auth context */
647 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
648 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
653 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
659 int mm_answer_auth2_read_banner(int socket, Buffer *m)
664 banner = auth2_read_banner();
665 buffer_put_cstring(m, banner != NULL ? banner : "");
666 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
675 mm_answer_authserv(int socket, Buffer *m)
677 monitor_permit_authentications(1);
679 authctxt->service = buffer_get_string(m, NULL);
680 authctxt->style = buffer_get_string(m, NULL);
681 debug3("%s: service=%s, style=%s",
682 __func__, authctxt->service, authctxt->style);
684 if (strlen(authctxt->style) == 0) {
685 xfree(authctxt->style);
686 authctxt->style = NULL;
693 mm_answer_authpassword(int socket, Buffer *m)
695 static int call_count;
700 passwd = buffer_get_string(m, &plen);
701 /* Only authenticate if the context is valid */
702 authenticated = options.password_authentication &&
703 auth_password(authctxt, passwd);
704 memset(passwd, 0, strlen(passwd));
708 buffer_put_int(m, authenticated);
710 debug3("%s: sending result %d", __func__, authenticated);
711 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
714 if (plen == 0 && call_count == 1)
715 auth_method = "none";
717 auth_method = "password";
719 /* Causes monitor loop to terminate if authenticated */
720 return (authenticated);
725 mm_answer_bsdauthquery(int socket, Buffer *m)
727 char *name, *infotxt;
733 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
734 &prompts, &echo_on) < 0 ? 0 : 1;
737 buffer_put_int(m, success);
739 buffer_put_cstring(m, prompts[0]);
741 debug3("%s: sending challenge success: %u", __func__, success);
742 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
755 mm_answer_bsdauthrespond(int socket, Buffer *m)
760 if (authctxt->as == 0)
761 fatal("%s: no bsd auth session", __func__);
763 response = buffer_get_string(m, NULL);
764 authok = options.challenge_response_authentication &&
765 auth_userresponse(authctxt->as, response, 0);
767 debug3("%s: <%s> = <%d>", __func__, response, authok);
771 buffer_put_int(m, authok);
773 debug3("%s: sending authenticated: %d", __func__, authok);
774 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
776 auth_method = "bsdauth";
778 return (authok != 0);
784 mm_answer_skeyquery(int socket, Buffer *m)
787 char challenge[1024];
790 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
791 sizeof(challenge)) < 0 ? 0 : 1;
794 buffer_put_int(m, success);
796 buffer_put_cstring(m, challenge);
798 debug3("%s: sending challenge success: %u", __func__, success);
799 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
805 mm_answer_skeyrespond(int socket, Buffer *m)
810 response = buffer_get_string(m, NULL);
812 authok = (options.challenge_response_authentication &&
814 skey_haskey(authctxt->pw->pw_name) == 0 &&
815 skey_passcheck(authctxt->pw->pw_name, response) != -1);
820 buffer_put_int(m, authok);
822 debug3("%s: sending authenticated: %d", __func__, authok);
823 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
825 auth_method = "skey";
827 return (authok != 0);
833 mm_answer_pam_start(int socket, Buffer *m)
835 if (!options.use_pam)
836 fatal("UsePAM not set, but ended up in %s anyway", __func__);
840 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
846 mm_answer_pam_account(int socket, Buffer *m)
850 if (!options.use_pam)
851 fatal("UsePAM not set, but ended up in %s anyway", __func__);
853 ret = do_pam_account();
855 buffer_put_int(m, ret);
857 mm_request_send(socket, MONITOR_ANS_PAM_ACCOUNT, m);
862 static void *sshpam_ctxt, *sshpam_authok;
863 extern KbdintDevice sshpam_device;
866 mm_answer_pam_init_ctx(int socket, Buffer *m)
869 debug3("%s", __func__);
870 authctxt->user = buffer_get_string(m, NULL);
871 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
872 sshpam_authok = NULL;
874 if (sshpam_ctxt != NULL) {
875 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
876 buffer_put_int(m, 1);
878 buffer_put_int(m, 0);
880 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
885 mm_answer_pam_query(int socket, Buffer *m)
887 char *name, *info, **prompts;
891 debug3("%s", __func__);
892 sshpam_authok = NULL;
893 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
894 if (ret == 0 && num == 0)
895 sshpam_authok = sshpam_ctxt;
896 if (num > 1 || name == NULL || info == NULL)
899 buffer_put_int(m, ret);
900 buffer_put_cstring(m, name);
902 buffer_put_cstring(m, info);
904 buffer_put_int(m, num);
905 for (i = 0; i < num; ++i) {
906 buffer_put_cstring(m, prompts[i]);
908 buffer_put_int(m, echo_on[i]);
914 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
919 mm_answer_pam_respond(int socket, Buffer *m)
925 debug3("%s", __func__);
926 sshpam_authok = NULL;
927 num = buffer_get_int(m);
929 resp = xmalloc(num * sizeof(char *));
930 for (i = 0; i < num; ++i)
931 resp[i] = buffer_get_string(m, NULL);
932 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
933 for (i = 0; i < num; ++i)
937 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
940 buffer_put_int(m, ret);
941 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
942 auth_method = "keyboard-interactive/pam";
944 sshpam_authok = sshpam_ctxt;
949 mm_answer_pam_free_ctx(int socket, Buffer *m)
952 debug3("%s", __func__);
953 (sshpam_device.free_ctx)(sshpam_ctxt);
955 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
956 return (sshpam_authok == sshpam_ctxt);
961 mm_append_debug(Buffer *m)
963 if (auth_debug_init && buffer_len(&auth_debug)) {
964 debug3("%s: Appending debug messages for child", __func__);
965 buffer_append(m, buffer_ptr(&auth_debug),
966 buffer_len(&auth_debug));
967 buffer_clear(&auth_debug);
972 mm_answer_keyallowed(int socket, Buffer *m)
978 enum mm_keytype type = 0;
981 debug3("%s entering", __func__);
983 type = buffer_get_int(m);
984 cuser = buffer_get_string(m, NULL);
985 chost = buffer_get_string(m, NULL);
986 blob = buffer_get_string(m, &bloblen);
988 key = key_from_blob(blob, bloblen);
990 if ((compat20 && type == MM_RSAHOSTKEY) ||
991 (!compat20 && type != MM_RSAHOSTKEY))
992 fatal("%s: key type and protocol mismatch", __func__);
994 debug3("%s: key_from_blob: %p", __func__, key);
996 if (key != NULL && authctxt->valid) {
999 allowed = options.pubkey_authentication &&
1000 user_key_allowed(authctxt->pw, key);
1003 allowed = options.hostbased_authentication &&
1004 hostbased_key_allowed(authctxt->pw,
1008 key->type = KEY_RSA1; /* XXX */
1009 allowed = options.rhosts_rsa_authentication &&
1010 auth_rhosts_rsa_key_allowed(authctxt->pw,
1014 fatal("%s: unknown key type %d", __func__, type);
1021 /* clear temporarily storage (used by verify) */
1022 monitor_reset_key_state();
1025 /* Save temporarily for comparison in verify */
1027 key_bloblen = bloblen;
1028 key_blobtype = type;
1029 hostbased_cuser = cuser;
1030 hostbased_chost = chost;
1033 debug3("%s: key %p is %s",
1034 __func__, key, allowed ? "allowed" : "disallowed");
1037 buffer_put_int(m, allowed);
1038 buffer_put_int(m, forced_command != NULL);
1042 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
1044 if (type == MM_RSAHOSTKEY)
1045 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1051 monitor_valid_userblob(u_char *data, u_int datalen)
1059 buffer_append(&b, data, datalen);
1061 if (datafellows & SSH_OLD_SESSIONID) {
1063 len = buffer_len(&b);
1064 if ((session_id2 == NULL) ||
1065 (len < session_id2_len) ||
1066 (memcmp(p, session_id2, session_id2_len) != 0))
1068 buffer_consume(&b, session_id2_len);
1070 p = buffer_get_string(&b, &len);
1071 if ((session_id2 == NULL) ||
1072 (len != session_id2_len) ||
1073 (memcmp(p, session_id2, session_id2_len) != 0))
1077 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1079 p = buffer_get_string(&b, NULL);
1080 if (strcmp(authctxt->user, p) != 0) {
1081 logit("wrong user name passed to monitor: expected %s != %.100s",
1086 buffer_skip_string(&b);
1087 if (datafellows & SSH_BUG_PKAUTH) {
1088 if (!buffer_get_char(&b))
1091 p = buffer_get_string(&b, NULL);
1092 if (strcmp("publickey", p) != 0)
1095 if (!buffer_get_char(&b))
1097 buffer_skip_string(&b);
1099 buffer_skip_string(&b);
1100 if (buffer_len(&b) != 0)
1107 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1116 buffer_append(&b, data, datalen);
1118 p = buffer_get_string(&b, &len);
1119 if ((session_id2 == NULL) ||
1120 (len != session_id2_len) ||
1121 (memcmp(p, session_id2, session_id2_len) != 0))
1125 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1127 p = buffer_get_string(&b, NULL);
1128 if (strcmp(authctxt->user, p) != 0) {
1129 logit("wrong user name passed to monitor: expected %s != %.100s",
1134 buffer_skip_string(&b); /* service */
1135 p = buffer_get_string(&b, NULL);
1136 if (strcmp(p, "hostbased") != 0)
1139 buffer_skip_string(&b); /* pkalg */
1140 buffer_skip_string(&b); /* pkblob */
1142 /* verify client host, strip trailing dot if necessary */
1143 p = buffer_get_string(&b, NULL);
1144 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1146 if (strcmp(p, chost) != 0)
1150 /* verify client user */
1151 p = buffer_get_string(&b, NULL);
1152 if (strcmp(p, cuser) != 0)
1156 if (buffer_len(&b) != 0)
1163 mm_answer_keyverify(int socket, Buffer *m)
1166 u_char *signature, *data, *blob;
1167 u_int signaturelen, datalen, bloblen;
1171 blob = buffer_get_string(m, &bloblen);
1172 signature = buffer_get_string(m, &signaturelen);
1173 data = buffer_get_string(m, &datalen);
1175 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1176 !monitor_allowed_key(blob, bloblen))
1177 fatal("%s: bad key, not previously allowed", __func__);
1179 key = key_from_blob(blob, bloblen);
1181 fatal("%s: bad public key blob", __func__);
1183 switch (key_blobtype) {
1185 valid_data = monitor_valid_userblob(data, datalen);
1188 valid_data = monitor_valid_hostbasedblob(data, datalen,
1189 hostbased_cuser, hostbased_chost);
1196 fatal("%s: bad signature data blob", __func__);
1198 verified = key_verify(key, signature, signaturelen, data, datalen);
1199 debug3("%s: key %p signature %s",
1200 __func__, key, verified ? "verified" : "unverified");
1207 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1209 monitor_reset_key_state();
1212 buffer_put_int(m, verified);
1213 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1219 mm_record_login(Session *s, struct passwd *pw)
1222 struct sockaddr_storage from;
1225 * Get IP address of client. If the connection is not a socket, let
1226 * the address be 0.0.0.0.
1228 memset(&from, 0, sizeof(from));
1229 fromlen = sizeof(from);
1230 if (packet_connection_is_on_socket()) {
1231 if (getpeername(packet_get_connection_in(),
1232 (struct sockaddr *) & from, &fromlen) < 0) {
1233 debug("getpeername: %.100s", strerror(errno));
1237 /* Record that there was a login on that tty from the remote host. */
1238 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1239 get_remote_name_or_ip(utmp_len, options.use_dns),
1240 (struct sockaddr *)&from, fromlen);
1244 mm_session_close(Session *s)
1246 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1247 if (s->ttyfd != -1) {
1248 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1249 session_pty_cleanup2(s);
1255 mm_answer_pty(int socket, Buffer *m)
1257 extern struct monitor *pmonitor;
1261 debug3("%s entering", __func__);
1267 s->authctxt = authctxt;
1268 s->pw = authctxt->pw;
1269 s->pid = pmonitor->m_pid;
1270 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1273 pty_setowner(authctxt->pw, s->tty);
1275 buffer_put_int(m, 1);
1276 buffer_put_cstring(m, s->tty);
1277 mm_request_send(socket, MONITOR_ANS_PTY, m);
1279 mm_send_fd(socket, s->ptyfd);
1280 mm_send_fd(socket, s->ttyfd);
1282 /* We need to trick ttyslot */
1283 if (dup2(s->ttyfd, 0) == -1)
1284 fatal("%s: dup2", __func__);
1286 mm_record_login(s, authctxt->pw);
1288 /* Now we can close the file descriptor again */
1291 /* make sure nothing uses fd 0 */
1292 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1293 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1295 error("%s: fd0 %d != 0", __func__, fd0);
1297 /* slave is not needed */
1299 s->ttyfd = s->ptyfd;
1300 /* no need to dup() because nobody closes ptyfd */
1301 s->ptymaster = s->ptyfd;
1303 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1309 mm_session_close(s);
1310 buffer_put_int(m, 0);
1311 mm_request_send(socket, MONITOR_ANS_PTY, m);
1316 mm_answer_pty_cleanup(int socket, Buffer *m)
1321 debug3("%s entering", __func__);
1323 tty = buffer_get_string(m, NULL);
1324 if ((s = session_by_tty(tty)) != NULL)
1325 mm_session_close(s);
1332 mm_answer_sesskey(int socket, Buffer *m)
1337 /* Turn off permissions */
1338 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1340 if ((p = BN_new()) == NULL)
1341 fatal("%s: BN_new", __func__);
1343 buffer_get_bignum2(m, p);
1345 rsafail = ssh1_session_key(p);
1348 buffer_put_int(m, rsafail);
1349 buffer_put_bignum2(m, p);
1353 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1355 /* Turn on permissions for sessid passing */
1356 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1362 mm_answer_sessid(int socket, Buffer *m)
1366 debug3("%s entering", __func__);
1368 if (buffer_len(m) != 16)
1369 fatal("%s: bad ssh1 session id", __func__);
1370 for (i = 0; i < 16; i++)
1371 session_id[i] = buffer_get_char(m);
1373 /* Turn on permissions for getpwnam */
1374 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1380 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1384 u_char *blob = NULL;
1388 debug3("%s entering", __func__);
1390 if (options.rsa_authentication && authctxt->valid) {
1391 if ((client_n = BN_new()) == NULL)
1392 fatal("%s: BN_new", __func__);
1393 buffer_get_bignum2(m, client_n);
1394 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1395 BN_clear_free(client_n);
1398 buffer_put_int(m, allowed);
1399 buffer_put_int(m, forced_command != NULL);
1401 /* clear temporarily storage (used by generate challenge) */
1402 monitor_reset_key_state();
1404 if (allowed && key != NULL) {
1405 key->type = KEY_RSA; /* cheat for key_to_blob */
1406 if (key_to_blob(key, &blob, &blen) == 0)
1407 fatal("%s: key_to_blob failed", __func__);
1408 buffer_put_string(m, blob, blen);
1410 /* Save temporarily for comparison in verify */
1413 key_blobtype = MM_RSAUSERKEY;
1420 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1422 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1423 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1428 mm_answer_rsa_challenge(int socket, Buffer *m)
1434 debug3("%s entering", __func__);
1436 if (!authctxt->valid)
1437 fatal("%s: authctxt not valid", __func__);
1438 blob = buffer_get_string(m, &blen);
1439 if (!monitor_allowed_key(blob, blen))
1440 fatal("%s: bad key, not previously allowed", __func__);
1441 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1442 fatal("%s: key type mismatch", __func__);
1443 if ((key = key_from_blob(blob, blen)) == NULL)
1444 fatal("%s: received bad key", __func__);
1447 BN_clear_free(ssh1_challenge);
1448 ssh1_challenge = auth_rsa_generate_challenge(key);
1451 buffer_put_bignum2(m, ssh1_challenge);
1453 debug3("%s sending reply", __func__);
1454 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1456 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1464 mm_answer_rsa_response(int socket, Buffer *m)
1467 u_char *blob, *response;
1471 debug3("%s entering", __func__);
1473 if (!authctxt->valid)
1474 fatal("%s: authctxt not valid", __func__);
1475 if (ssh1_challenge == NULL)
1476 fatal("%s: no ssh1_challenge", __func__);
1478 blob = buffer_get_string(m, &blen);
1479 if (!monitor_allowed_key(blob, blen))
1480 fatal("%s: bad key, not previously allowed", __func__);
1481 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1482 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1483 if ((key = key_from_blob(blob, blen)) == NULL)
1484 fatal("%s: received bad key", __func__);
1485 response = buffer_get_string(m, &len);
1487 fatal("%s: received bad response to challenge", __func__);
1488 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1494 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1497 BN_clear_free(ssh1_challenge);
1498 ssh1_challenge = NULL;
1499 monitor_reset_key_state();
1502 buffer_put_int(m, success);
1503 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1509 mm_answer_term(int socket, Buffer *req)
1511 extern struct monitor *pmonitor;
1514 debug3("%s: tearing down sessions", __func__);
1516 /* The child is terminating */
1517 session_destroy_all(&mm_session_close);
1519 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1523 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1525 /* Terminate process */
1530 monitor_apply_keystate(struct monitor *pmonitor)
1533 set_newkeys(MODE_IN);
1534 set_newkeys(MODE_OUT);
1536 packet_set_protocol_flags(child_state.ssh1protoflags);
1537 packet_set_encryption_key(child_state.ssh1key,
1538 child_state.ssh1keylen, child_state.ssh1cipher);
1539 xfree(child_state.ssh1key);
1542 /* for rc4 and other stateful ciphers */
1543 packet_set_keycontext(MODE_OUT, child_state.keyout);
1544 xfree(child_state.keyout);
1545 packet_set_keycontext(MODE_IN, child_state.keyin);
1546 xfree(child_state.keyin);
1549 packet_set_iv(MODE_OUT, child_state.ivout);
1550 xfree(child_state.ivout);
1551 packet_set_iv(MODE_IN, child_state.ivin);
1552 xfree(child_state.ivin);
1555 memcpy(&incoming_stream, &child_state.incoming,
1556 sizeof(incoming_stream));
1557 memcpy(&outgoing_stream, &child_state.outgoing,
1558 sizeof(outgoing_stream));
1560 /* Update with new address */
1561 if (options.compression)
1562 mm_init_compression(pmonitor->m_zlib);
1564 /* Network I/O buffers */
1565 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1566 buffer_clear(&input);
1567 buffer_append(&input, child_state.input, child_state.ilen);
1568 memset(child_state.input, 0, child_state.ilen);
1569 xfree(child_state.input);
1571 buffer_clear(&output);
1572 buffer_append(&output, child_state.output, child_state.olen);
1573 memset(child_state.output, 0, child_state.olen);
1574 xfree(child_state.output);
1578 mm_get_kex(Buffer *m)
1584 kex = xmalloc(sizeof(*kex));
1585 memset(kex, 0, sizeof(*kex));
1586 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1587 if ((session_id2 == NULL) ||
1588 (kex->session_id_len != session_id2_len) ||
1589 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1590 fatal("mm_get_get: internal error: bad session id");
1591 kex->we_need = buffer_get_int(m);
1592 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1593 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1595 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
1598 kex->hostkey_type = buffer_get_int(m);
1599 kex->kex_type = buffer_get_int(m);
1600 blob = buffer_get_string(m, &bloblen);
1601 buffer_init(&kex->my);
1602 buffer_append(&kex->my, blob, bloblen);
1604 blob = buffer_get_string(m, &bloblen);
1605 buffer_init(&kex->peer);
1606 buffer_append(&kex->peer, blob, bloblen);
1609 kex->flags = buffer_get_int(m);
1610 kex->client_version_string = buffer_get_string(m, NULL);
1611 kex->server_version_string = buffer_get_string(m, NULL);
1612 kex->load_host_key=&get_hostkey_by_type;
1613 kex->host_key_index=&get_hostkey_index;
1618 /* This function requries careful sanity checking */
1621 mm_get_keystate(struct monitor *pmonitor)
1625 u_int bloblen, plen;
1626 u_int32_t seqnr, packets;
1629 debug3("%s: Waiting for new keys", __func__);
1632 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1634 child_state.ssh1protoflags = buffer_get_int(&m);
1635 child_state.ssh1cipher = buffer_get_int(&m);
1636 child_state.ssh1key = buffer_get_string(&m,
1637 &child_state.ssh1keylen);
1638 child_state.ivout = buffer_get_string(&m,
1639 &child_state.ivoutlen);
1640 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1643 /* Get the Kex for rekeying */
1644 *pmonitor->m_pkex = mm_get_kex(&m);
1647 blob = buffer_get_string(&m, &bloblen);
1648 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1651 debug3("%s: Waiting for second key", __func__);
1652 blob = buffer_get_string(&m, &bloblen);
1653 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1656 /* Now get sequence numbers for the packets */
1657 seqnr = buffer_get_int(&m);
1658 blocks = buffer_get_int64(&m);
1659 packets = buffer_get_int(&m);
1660 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1661 seqnr = buffer_get_int(&m);
1662 blocks = buffer_get_int64(&m);
1663 packets = buffer_get_int(&m);
1664 packet_set_state(MODE_IN, seqnr, blocks, packets);
1667 /* Get the key context */
1668 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1669 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1671 debug3("%s: Getting compression state", __func__);
1672 /* Get compression state */
1673 p = buffer_get_string(&m, &plen);
1674 if (plen != sizeof(child_state.outgoing))
1675 fatal("%s: bad request size", __func__);
1676 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1679 p = buffer_get_string(&m, &plen);
1680 if (plen != sizeof(child_state.incoming))
1681 fatal("%s: bad request size", __func__);
1682 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1685 /* Network I/O buffers */
1686 debug3("%s: Getting Network I/O buffers", __func__);
1687 child_state.input = buffer_get_string(&m, &child_state.ilen);
1688 child_state.output = buffer_get_string(&m, &child_state.olen);
1694 /* Allocation functions for zlib */
1696 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1698 size_t len = (size_t) size * ncount;
1701 if (len == 0 || ncount > SIZE_T_MAX / size)
1702 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1704 address = mm_malloc(mm, len);
1710 mm_zfree(struct mm_master *mm, void *address)
1712 mm_free(mm, address);
1716 mm_init_compression(struct mm_master *mm)
1718 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1719 outgoing_stream.zfree = (free_func)mm_zfree;
1720 outgoing_stream.opaque = mm;
1722 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1723 incoming_stream.zfree = (free_func)mm_zfree;
1724 incoming_stream.opaque = mm;
1729 #define FD_CLOSEONEXEC(x) do { \
1730 if (fcntl(x, F_SETFD, 1) == -1) \
1731 fatal("fcntl(%d, F_SETFD)", x); \
1735 monitor_socketpair(int *pair)
1737 #ifdef HAVE_SOCKETPAIR
1738 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1739 fatal("%s: socketpair", __func__);
1741 fatal("%s: UsePrivilegeSeparation=yes not supported",
1744 FD_CLOSEONEXEC(pair[0]);
1745 FD_CLOSEONEXEC(pair[1]);
1748 #define MM_MEMSIZE 65536
1753 struct monitor *mon;
1756 mon = xmalloc(sizeof(*mon));
1759 monitor_socketpair(pair);
1761 mon->m_recvfd = pair[0];
1762 mon->m_sendfd = pair[1];
1764 /* Used to share zlib space across processes */
1765 if (options.compression) {
1766 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1767 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1769 /* Compression needs to share state across borders */
1770 mm_init_compression(mon->m_zlib);
1777 monitor_reinit(struct monitor *mon)
1781 monitor_socketpair(pair);
1783 mon->m_recvfd = pair[0];
1784 mon->m_sendfd = pair[1];
1789 mm_answer_gss_setup_ctx(int socket, Buffer *m)
1795 oid.elements = buffer_get_string(m, &len);
1798 major = ssh_gssapi_server_ctx(&gsscontext, &oid);
1800 xfree(oid.elements);
1803 buffer_put_int(m, major);
1805 mm_request_send(socket,MONITOR_ANS_GSSSETUP, m);
1807 /* Now we have a context, enable the step */
1808 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1814 mm_answer_gss_accept_ctx(int socket, Buffer *m)
1817 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1818 OM_uint32 major,minor;
1819 OM_uint32 flags = 0; /* GSI needs this */
1822 in.value = buffer_get_string(m, &len);
1824 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1828 buffer_put_int(m, major);
1829 buffer_put_string(m, out.value, out.length);
1830 buffer_put_int(m, flags);
1831 mm_request_send(socket, MONITOR_ANS_GSSSTEP, m);
1833 gss_release_buffer(&minor, &out);
1835 if (major==GSS_S_COMPLETE) {
1836 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1837 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1838 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
1839 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1845 mm_answer_gss_checkmic(int socket, Buffer *m)
1847 gss_buffer_desc gssbuf, mic;
1851 gssbuf.value = buffer_get_string(m, &len);
1852 gssbuf.length = len;
1853 mic.value = buffer_get_string(m, &len);
1856 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1858 xfree(gssbuf.value);
1862 buffer_put_int(m, ret);
1864 mm_request_send(socket, MONITOR_ANS_GSSCHECKMIC, m);
1866 if (!GSS_ERROR(ret))
1867 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1873 mm_answer_gss_userok(int socket, Buffer *m)
1877 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1880 buffer_put_int(m, authenticated);
1882 debug3("%s: sending result %d", __func__, authenticated);
1883 mm_request_send(socket, MONITOR_ANS_GSSUSEROK, m);
1885 auth_method="gssapi-with-mic";
1887 /* Monitor loop will terminate if authenticated */
1888 return (authenticated);
1892 mm_answer_gss_sign(int socket, Buffer *m) {
1893 gss_buffer_desc data,hash;
1894 OM_uint32 major,minor;
1897 data.value = buffer_get_string(m, &len);
1899 if (data.length != 20)
1900 fatal("%s: data length incorrect: %d", __func__,
1903 /* Save the session ID - only first time round */
1904 if (session_id2_len == 0) {
1905 session_id2_len=data.length;
1906 session_id2 = xmalloc(session_id2_len);
1907 memcpy(session_id2, data.value, session_id2_len);
1909 major=ssh_gssapi_sign(gsscontext, &data, &hash);
1914 buffer_put_int(m, major);
1915 buffer_put_string(m, hash.value, hash.length);
1917 mm_request_send(socket,MONITOR_ANS_GSSSIGN,m);
1919 gss_release_buffer(&minor,&hash);
1921 /* Turn on permissions for getpwnam */
1922 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1928 mm_answer_gss_error(int socket, Buffer *m) {
1929 OM_uint32 major,minor;
1932 msg=ssh_gssapi_last_error(gsscontext,&major,&minor);
1934 buffer_put_int(m,major);
1935 buffer_put_int(m,minor);
1936 buffer_put_cstring(m,msg);
1938 mm_request_send(socket,MONITOR_ANS_GSSERR,m);
1946 mm_answer_gss_indicate_mechs(int socket, Buffer *m) {
1947 OM_uint32 major,minor;
1948 gss_OID_set mech_set;
1951 major=gss_indicate_mechs(&minor, &mech_set);
1954 buffer_put_int(m, major);
1955 buffer_put_int(m, mech_set->count);
1956 for (i=0; i < mech_set->count; i++) {
1957 buffer_put_string(m, mech_set->elements[i].elements,
1958 mech_set->elements[i].length);
1961 #if !defined(MECHGLUE) /* mechglue memory management bug ??? */
1962 gss_release_oid_set(&minor,&mech_set);
1965 mm_request_send(socket,MONITOR_ANS_GSSMECHS,m);
1971 mm_answer_gss_localname(int socket, Buffer *m) {
1974 ssh_gssapi_localname(&name);
1978 buffer_put_cstring(m, name);
1979 debug3("%s: sending result %s", __func__, name);
1982 buffer_put_cstring(m, "");
1983 debug3("%s: sending result \"\"", __func__);
1986 mm_request_send(socket, MONITOR_ANS_GSSLOCALNAME, m);