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.45 2003/07/22 13:35:22 markus Exp $");
30 #include <openssl/dh.h>
42 #include "auth-options.h"
51 #include "monitor_mm.h"
52 #include "monitor_wrap.h"
53 #include "monitor_fdpass.h"
63 extern ServerOptions options;
64 extern u_int utmp_len;
65 extern Newkeys *current_keys[];
66 extern z_stream incoming_stream;
67 extern z_stream outgoing_stream;
68 extern u_char session_id[];
69 extern Buffer input, output;
70 extern Buffer auth_debug;
71 extern int auth_debug_init;
73 /* State exported from the child */
96 /* Functions on the monitor that answer unprivileged requests */
98 int mm_answer_moduli(int, Buffer *);
99 int mm_answer_sign(int, Buffer *);
100 int mm_answer_pwnamallow(int, Buffer *);
101 int mm_answer_auth2_read_banner(int, Buffer *);
102 int mm_answer_authserv(int, Buffer *);
103 int mm_answer_authpassword(int, Buffer *);
104 int mm_answer_bsdauthquery(int, Buffer *);
105 int mm_answer_bsdauthrespond(int, Buffer *);
106 int mm_answer_skeyquery(int, Buffer *);
107 int mm_answer_skeyrespond(int, Buffer *);
108 int mm_answer_keyallowed(int, Buffer *);
109 int mm_answer_keyverify(int, Buffer *);
110 int mm_answer_pty(int, Buffer *);
111 int mm_answer_pty_cleanup(int, Buffer *);
112 int mm_answer_term(int, Buffer *);
113 int mm_answer_rsa_keyallowed(int, Buffer *);
114 int mm_answer_rsa_challenge(int, Buffer *);
115 int mm_answer_rsa_response(int, Buffer *);
116 int mm_answer_sesskey(int, Buffer *);
117 int mm_answer_sessid(int, Buffer *);
120 int mm_answer_pam_start(int, Buffer *);
121 int mm_answer_pam_account(int, Buffer *);
122 int mm_answer_pam_init_ctx(int, Buffer *);
123 int mm_answer_pam_query(int, Buffer *);
124 int mm_answer_pam_respond(int, Buffer *);
125 int mm_answer_pam_free_ctx(int, Buffer *);
129 int mm_answer_krb5(int, Buffer *);
132 static Authctxt *authctxt;
133 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
135 /* local state for key verify */
136 static u_char *key_blob = NULL;
137 static u_int key_bloblen = 0;
138 static int key_blobtype = MM_NOKEY;
139 static char *hostbased_cuser = NULL;
140 static char *hostbased_chost = NULL;
141 static char *auth_method = "unknown";
142 static u_int session_id2_len = 0;
143 static u_char *session_id2 = NULL;
144 static pid_t monitor_child_pid;
147 enum monitor_reqtype type;
149 int (*f)(int, Buffer *);
152 #define MON_ISAUTH 0x0004 /* Required for Authentication */
153 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
154 #define MON_ONCE 0x0010 /* Disable after calling */
156 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
158 #define MON_PERMIT 0x1000 /* Request is permitted */
160 struct mon_table mon_dispatch_proto20[] = {
161 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
162 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
163 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
164 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
165 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
166 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
168 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
169 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
170 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
171 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
172 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
173 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
176 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
177 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
180 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
181 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
183 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
184 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
186 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
191 struct mon_table mon_dispatch_postauth20[] = {
192 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
193 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
194 {MONITOR_REQ_PTY, 0, mm_answer_pty},
195 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
196 {MONITOR_REQ_TERM, 0, mm_answer_term},
200 struct mon_table mon_dispatch_proto15[] = {
201 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
202 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
203 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
204 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
205 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
206 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
207 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
208 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
210 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
211 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
214 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
215 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
218 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
219 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
220 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
221 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
222 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
223 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
226 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
231 struct mon_table mon_dispatch_postauth15[] = {
232 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
233 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
234 {MONITOR_REQ_TERM, 0, mm_answer_term},
238 struct mon_table *mon_dispatch;
240 /* Specifies if a certain message is allowed at the moment */
243 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
245 while (ent->f != NULL) {
246 if (ent->type == type) {
247 ent->flags &= ~MON_PERMIT;
248 ent->flags |= permit ? MON_PERMIT : 0;
256 monitor_permit_authentications(int permit)
258 struct mon_table *ent = mon_dispatch;
260 while (ent->f != NULL) {
261 if (ent->flags & MON_AUTH) {
262 ent->flags &= ~MON_PERMIT;
263 ent->flags |= permit ? MON_PERMIT : 0;
270 monitor_child_preauth(struct monitor *pmonitor)
272 struct mon_table *ent;
273 int authenticated = 0;
275 debug3("preauth child monitor started");
278 mon_dispatch = mon_dispatch_proto20;
280 /* Permit requests for moduli and signatures */
281 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
282 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
284 mon_dispatch = mon_dispatch_proto15;
286 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
289 authctxt = authctxt_new();
291 /* The first few requests do not require asynchronous access */
292 while (!authenticated) {
293 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
295 if (!(ent->flags & MON_AUTHDECIDE))
296 fatal("%s: unexpected authentication from %d",
297 __func__, ent->type);
298 if (authctxt->pw->pw_uid == 0 &&
299 !auth_root_allowed(auth_method))
302 /* PAM needs to perform account checks after auth */
303 if (options.use_pam) {
307 mm_request_receive_expect(pmonitor->m_sendfd,
308 MONITOR_REQ_PAM_ACCOUNT, &m);
309 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
315 if (ent->flags & MON_AUTHDECIDE) {
316 auth_log(authctxt, authenticated, auth_method,
317 compat20 ? " ssh2" : "");
319 authctxt->failures++;
323 if (!authctxt->valid)
324 fatal("%s: authenticated invalid user", __func__);
326 debug("%s: %s has been authenticated by privileged process",
327 __func__, authctxt->user);
329 mm_get_keystate(pmonitor);
335 monitor_set_child_handler(pid_t pid)
337 monitor_child_pid = pid;
341 monitor_child_handler(int signal)
343 kill(monitor_child_pid, signal);
347 monitor_child_postauth(struct monitor *pmonitor)
349 monitor_set_child_handler(pmonitor->m_pid);
350 signal(SIGHUP, &monitor_child_handler);
351 signal(SIGTERM, &monitor_child_handler);
354 mon_dispatch = mon_dispatch_postauth20;
356 /* Permit requests for moduli and signatures */
357 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
358 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
359 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
362 mon_dispatch = mon_dispatch_postauth15;
363 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
366 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
367 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
371 monitor_read(pmonitor, mon_dispatch, NULL);
375 monitor_sync(struct monitor *pmonitor)
377 if (options.compression) {
378 /* The member allocation is not visible, so sync it */
379 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
384 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
385 struct mon_table **pent)
393 mm_request_receive(pmonitor->m_sendfd, &m);
394 type = buffer_get_char(&m);
396 debug3("%s: checking request %d", __func__, type);
398 while (ent->f != NULL) {
399 if (ent->type == type)
404 if (ent->f != NULL) {
405 if (!(ent->flags & MON_PERMIT))
406 fatal("%s: unpermitted request %d", __func__,
408 ret = (*ent->f)(pmonitor->m_sendfd, &m);
411 /* The child may use this request only once, disable it */
412 if (ent->flags & MON_ONCE) {
413 debug2("%s: %d used once, disabling now", __func__,
415 ent->flags &= ~MON_PERMIT;
424 fatal("%s: unsupported request: %d", __func__, type);
430 /* allowed key state */
432 monitor_allowed_key(u_char *blob, u_int bloblen)
434 /* make sure key is allowed */
435 if (key_blob == NULL || key_bloblen != bloblen ||
436 memcmp(key_blob, blob, key_bloblen))
442 monitor_reset_key_state(void)
445 if (key_blob != NULL)
447 if (hostbased_cuser != NULL)
448 xfree(hostbased_cuser);
449 if (hostbased_chost != NULL)
450 xfree(hostbased_chost);
453 key_blobtype = MM_NOKEY;
454 hostbased_cuser = NULL;
455 hostbased_chost = NULL;
459 mm_answer_moduli(int socket, Buffer *m)
464 min = buffer_get_int(m);
465 want = buffer_get_int(m);
466 max = buffer_get_int(m);
468 debug3("%s: got parameters: %d %d %d",
469 __func__, min, want, max);
470 /* We need to check here, too, in case the child got corrupted */
471 if (max < min || want < min || max < want)
472 fatal("%s: bad parameters: %d %d %d",
473 __func__, min, want, max);
477 dh = choose_dh(min, want, max);
479 buffer_put_char(m, 0);
482 /* Send first bignum */
483 buffer_put_char(m, 1);
484 buffer_put_bignum2(m, dh->p);
485 buffer_put_bignum2(m, dh->g);
489 mm_request_send(socket, MONITOR_ANS_MODULI, m);
494 mm_answer_sign(int socket, Buffer *m)
499 u_int siglen, datlen;
502 debug3("%s", __func__);
504 keyid = buffer_get_int(m);
505 p = buffer_get_string(m, &datlen);
508 fatal("%s: data length incorrect: %u", __func__, datlen);
510 /* save session id, it will be passed on the first call */
511 if (session_id2_len == 0) {
512 session_id2_len = datlen;
513 session_id2 = xmalloc(session_id2_len);
514 memcpy(session_id2, p, session_id2_len);
517 if ((key = get_hostkey_by_index(keyid)) == NULL)
518 fatal("%s: no hostkey from index %d", __func__, keyid);
519 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
520 fatal("%s: key_sign failed", __func__);
522 debug3("%s: signature %p(%u)", __func__, signature, siglen);
525 buffer_put_string(m, signature, siglen);
530 mm_request_send(socket, MONITOR_ANS_SIGN, m);
532 /* Turn on permissions for getpwnam */
533 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
538 /* Retrieves the password entry and also checks if the user is permitted */
541 mm_answer_pwnamallow(int socket, Buffer *m)
544 struct passwd *pwent;
547 debug3("%s", __func__);
549 if (authctxt->attempt++ != 0)
550 fatal("%s: multiple attempts for getpwnam", __func__);
552 login = buffer_get_string(m, NULL);
554 pwent = getpwnamallow(login);
556 authctxt->user = xstrdup(login);
557 setproctitle("%s [priv]", pwent ? login : "unknown");
563 buffer_put_char(m, 0);
568 authctxt->pw = pwent;
571 buffer_put_char(m, 1);
572 buffer_put_string(m, pwent, sizeof(struct passwd));
573 buffer_put_cstring(m, pwent->pw_name);
574 buffer_put_cstring(m, "*");
575 buffer_put_cstring(m, pwent->pw_gecos);
576 #ifdef HAVE_PW_CLASS_IN_PASSWD
577 buffer_put_cstring(m, pwent->pw_class);
579 buffer_put_cstring(m, pwent->pw_dir);
580 buffer_put_cstring(m, pwent->pw_shell);
583 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
584 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
586 /* For SSHv1 allow authentication now */
588 monitor_permit_authentications(1);
590 /* Allow service/style information on the auth context */
591 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
592 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
597 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
603 int mm_answer_auth2_read_banner(int socket, Buffer *m)
608 banner = auth2_read_banner();
609 buffer_put_cstring(m, banner != NULL ? banner : "");
610 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
619 mm_answer_authserv(int socket, Buffer *m)
621 monitor_permit_authentications(1);
623 authctxt->service = buffer_get_string(m, NULL);
624 authctxt->style = buffer_get_string(m, NULL);
625 debug3("%s: service=%s, style=%s",
626 __func__, authctxt->service, authctxt->style);
628 if (strlen(authctxt->style) == 0) {
629 xfree(authctxt->style);
630 authctxt->style = NULL;
637 mm_answer_authpassword(int socket, Buffer *m)
639 static int call_count;
644 passwd = buffer_get_string(m, &plen);
645 /* Only authenticate if the context is valid */
646 authenticated = options.password_authentication &&
647 auth_password(authctxt, passwd) && authctxt->valid;
648 memset(passwd, 0, strlen(passwd));
652 buffer_put_int(m, authenticated);
654 debug3("%s: sending result %d", __func__, authenticated);
655 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
658 if (plen == 0 && call_count == 1)
659 auth_method = "none";
661 auth_method = "password";
663 /* Causes monitor loop to terminate if authenticated */
664 return (authenticated);
669 mm_answer_bsdauthquery(int socket, Buffer *m)
671 char *name, *infotxt;
677 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
678 &prompts, &echo_on) < 0 ? 0 : 1;
681 buffer_put_int(m, success);
683 buffer_put_cstring(m, prompts[0]);
685 debug3("%s: sending challenge success: %u", __func__, success);
686 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
699 mm_answer_bsdauthrespond(int socket, Buffer *m)
704 if (authctxt->as == 0)
705 fatal("%s: no bsd auth session", __func__);
707 response = buffer_get_string(m, NULL);
708 authok = options.challenge_response_authentication &&
709 auth_userresponse(authctxt->as, response, 0);
711 debug3("%s: <%s> = <%d>", __func__, response, authok);
715 buffer_put_int(m, authok);
717 debug3("%s: sending authenticated: %d", __func__, authok);
718 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
720 auth_method = "bsdauth";
722 return (authok != 0);
728 mm_answer_skeyquery(int socket, Buffer *m)
731 char challenge[1024];
734 success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
737 buffer_put_int(m, success);
739 buffer_put_cstring(m, challenge);
741 debug3("%s: sending challenge success: %u", __func__, success);
742 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
748 mm_answer_skeyrespond(int socket, Buffer *m)
753 response = buffer_get_string(m, NULL);
755 authok = (options.challenge_response_authentication &&
757 skey_haskey(authctxt->pw->pw_name) == 0 &&
758 skey_passcheck(authctxt->pw->pw_name, response) != -1);
763 buffer_put_int(m, authok);
765 debug3("%s: sending authenticated: %d", __func__, authok);
766 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
768 auth_method = "skey";
770 return (authok != 0);
776 mm_answer_pam_start(int socket, Buffer *m)
780 if (!options.use_pam)
781 fatal("UsePAM not set, but ended up in %s anyway", __func__);
783 user = buffer_get_string(m, NULL);
789 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
795 mm_answer_pam_account(int socket, Buffer *m)
799 if (!options.use_pam)
800 fatal("UsePAM not set, but ended up in %s anyway", __func__);
802 ret = do_pam_account();
804 buffer_put_int(m, ret);
806 mm_request_send(socket, MONITOR_ANS_PAM_ACCOUNT, m);
811 static void *sshpam_ctxt, *sshpam_authok;
812 extern KbdintDevice sshpam_device;
815 mm_answer_pam_init_ctx(int socket, Buffer *m)
818 debug3("%s", __func__);
819 authctxt->user = buffer_get_string(m, NULL);
820 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
821 sshpam_authok = NULL;
823 if (sshpam_ctxt != NULL) {
824 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
825 buffer_put_int(m, 1);
827 buffer_put_int(m, 0);
829 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
834 mm_answer_pam_query(int socket, Buffer *m)
836 char *name, *info, **prompts;
840 debug3("%s", __func__);
841 sshpam_authok = NULL;
842 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
843 if (ret == 0 && num == 0)
844 sshpam_authok = sshpam_ctxt;
845 if (num > 1 || name == NULL || info == NULL)
848 buffer_put_int(m, ret);
849 buffer_put_cstring(m, name);
851 buffer_put_cstring(m, info);
853 buffer_put_int(m, num);
854 for (i = 0; i < num; ++i) {
855 buffer_put_cstring(m, prompts[i]);
857 buffer_put_int(m, echo_on[i]);
863 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
868 mm_answer_pam_respond(int socket, Buffer *m)
874 debug3("%s", __func__);
875 sshpam_authok = NULL;
876 num = buffer_get_int(m);
878 resp = xmalloc(num * sizeof(char *));
879 for (i = 0; i < num; ++i)
880 resp[i] = buffer_get_string(m, NULL);
881 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
882 for (i = 0; i < num; ++i)
886 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
889 buffer_put_int(m, ret);
890 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
891 auth_method = "keyboard-interactive/pam";
893 sshpam_authok = sshpam_ctxt;
898 mm_answer_pam_free_ctx(int socket, Buffer *m)
901 debug3("%s", __func__);
902 (sshpam_device.free_ctx)(sshpam_ctxt);
904 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
905 return (sshpam_authok == sshpam_ctxt);
910 mm_append_debug(Buffer *m)
912 if (auth_debug_init && buffer_len(&auth_debug)) {
913 debug3("%s: Appending debug messages for child", __func__);
914 buffer_append(m, buffer_ptr(&auth_debug),
915 buffer_len(&auth_debug));
916 buffer_clear(&auth_debug);
921 mm_answer_keyallowed(int socket, Buffer *m)
927 enum mm_keytype type = 0;
930 debug3("%s entering", __func__);
932 type = buffer_get_int(m);
933 cuser = buffer_get_string(m, NULL);
934 chost = buffer_get_string(m, NULL);
935 blob = buffer_get_string(m, &bloblen);
937 key = key_from_blob(blob, bloblen);
939 if ((compat20 && type == MM_RSAHOSTKEY) ||
940 (!compat20 && type != MM_RSAHOSTKEY))
941 fatal("%s: key type and protocol mismatch", __func__);
943 debug3("%s: key_from_blob: %p", __func__, key);
945 if (key != NULL && authctxt->pw != NULL) {
948 allowed = options.pubkey_authentication &&
949 user_key_allowed(authctxt->pw, key);
952 allowed = options.hostbased_authentication &&
953 hostbased_key_allowed(authctxt->pw,
957 key->type = KEY_RSA1; /* XXX */
958 allowed = options.rhosts_rsa_authentication &&
959 auth_rhosts_rsa_key_allowed(authctxt->pw,
963 fatal("%s: unknown key type %d", __func__, type);
970 /* clear temporarily storage (used by verify) */
971 monitor_reset_key_state();
974 /* Save temporarily for comparison in verify */
976 key_bloblen = bloblen;
978 hostbased_cuser = cuser;
979 hostbased_chost = chost;
982 debug3("%s: key %p is %s",
983 __func__, key, allowed ? "allowed" : "disallowed");
986 buffer_put_int(m, allowed);
987 buffer_put_int(m, forced_command != NULL);
991 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
993 if (type == MM_RSAHOSTKEY)
994 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1000 monitor_valid_userblob(u_char *data, u_int datalen)
1008 buffer_append(&b, data, datalen);
1010 if (datafellows & SSH_OLD_SESSIONID) {
1012 len = buffer_len(&b);
1013 if ((session_id2 == NULL) ||
1014 (len < session_id2_len) ||
1015 (memcmp(p, session_id2, session_id2_len) != 0))
1017 buffer_consume(&b, session_id2_len);
1019 p = buffer_get_string(&b, &len);
1020 if ((session_id2 == NULL) ||
1021 (len != session_id2_len) ||
1022 (memcmp(p, session_id2, session_id2_len) != 0))
1026 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1028 p = buffer_get_string(&b, NULL);
1029 if (strcmp(authctxt->user, p) != 0) {
1030 logit("wrong user name passed to monitor: expected %s != %.100s",
1035 buffer_skip_string(&b);
1036 if (datafellows & SSH_BUG_PKAUTH) {
1037 if (!buffer_get_char(&b))
1040 p = buffer_get_string(&b, NULL);
1041 if (strcmp("publickey", p) != 0)
1044 if (!buffer_get_char(&b))
1046 buffer_skip_string(&b);
1048 buffer_skip_string(&b);
1049 if (buffer_len(&b) != 0)
1056 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1065 buffer_append(&b, data, datalen);
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); /* service */
1084 p = buffer_get_string(&b, NULL);
1085 if (strcmp(p, "hostbased") != 0)
1088 buffer_skip_string(&b); /* pkalg */
1089 buffer_skip_string(&b); /* pkblob */
1091 /* verify client host, strip trailing dot if necessary */
1092 p = buffer_get_string(&b, NULL);
1093 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1095 if (strcmp(p, chost) != 0)
1099 /* verify client user */
1100 p = buffer_get_string(&b, NULL);
1101 if (strcmp(p, cuser) != 0)
1105 if (buffer_len(&b) != 0)
1112 mm_answer_keyverify(int socket, Buffer *m)
1115 u_char *signature, *data, *blob;
1116 u_int signaturelen, datalen, bloblen;
1120 blob = buffer_get_string(m, &bloblen);
1121 signature = buffer_get_string(m, &signaturelen);
1122 data = buffer_get_string(m, &datalen);
1124 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1125 !monitor_allowed_key(blob, bloblen))
1126 fatal("%s: bad key, not previously allowed", __func__);
1128 key = key_from_blob(blob, bloblen);
1130 fatal("%s: bad public key blob", __func__);
1132 switch (key_blobtype) {
1134 valid_data = monitor_valid_userblob(data, datalen);
1137 valid_data = monitor_valid_hostbasedblob(data, datalen,
1138 hostbased_cuser, hostbased_chost);
1145 fatal("%s: bad signature data blob", __func__);
1147 verified = key_verify(key, signature, signaturelen, data, datalen);
1148 debug3("%s: key %p signature %s",
1149 __func__, key, verified ? "verified" : "unverified");
1156 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1158 monitor_reset_key_state();
1161 buffer_put_int(m, verified);
1162 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1168 mm_record_login(Session *s, struct passwd *pw)
1171 struct sockaddr_storage from;
1174 * Get IP address of client. If the connection is not a socket, let
1175 * the address be 0.0.0.0.
1177 memset(&from, 0, sizeof(from));
1178 fromlen = sizeof(from);
1179 if (packet_connection_is_on_socket()) {
1180 if (getpeername(packet_get_connection_in(),
1181 (struct sockaddr *) & from, &fromlen) < 0) {
1182 debug("getpeername: %.100s", strerror(errno));
1186 /* Record that there was a login on that tty from the remote host. */
1187 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1188 get_remote_name_or_ip(utmp_len, options.use_dns),
1189 (struct sockaddr *)&from, fromlen);
1193 mm_session_close(Session *s)
1195 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1196 if (s->ttyfd != -1) {
1197 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1198 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1199 session_pty_cleanup2(s);
1205 mm_answer_pty(int socket, Buffer *m)
1207 extern struct monitor *pmonitor;
1211 debug3("%s entering", __func__);
1217 s->authctxt = authctxt;
1218 s->pw = authctxt->pw;
1219 s->pid = pmonitor->m_pid;
1220 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1223 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1224 pty_setowner(authctxt->pw, s->tty);
1226 buffer_put_int(m, 1);
1227 buffer_put_cstring(m, s->tty);
1228 mm_request_send(socket, MONITOR_ANS_PTY, m);
1230 mm_send_fd(socket, s->ptyfd);
1231 mm_send_fd(socket, s->ttyfd);
1233 /* We need to trick ttyslot */
1234 if (dup2(s->ttyfd, 0) == -1)
1235 fatal("%s: dup2", __func__);
1237 mm_record_login(s, authctxt->pw);
1239 /* Now we can close the file descriptor again */
1242 /* make sure nothing uses fd 0 */
1243 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1244 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1246 error("%s: fd0 %d != 0", __func__, fd0);
1248 /* slave is not needed */
1250 s->ttyfd = s->ptyfd;
1251 /* no need to dup() because nobody closes ptyfd */
1252 s->ptymaster = s->ptyfd;
1254 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1260 mm_session_close(s);
1261 buffer_put_int(m, 0);
1262 mm_request_send(socket, MONITOR_ANS_PTY, m);
1267 mm_answer_pty_cleanup(int socket, Buffer *m)
1272 debug3("%s entering", __func__);
1274 tty = buffer_get_string(m, NULL);
1275 if ((s = session_by_tty(tty)) != NULL)
1276 mm_session_close(s);
1283 mm_answer_sesskey(int socket, Buffer *m)
1288 /* Turn off permissions */
1289 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1291 if ((p = BN_new()) == NULL)
1292 fatal("%s: BN_new", __func__);
1294 buffer_get_bignum2(m, p);
1296 rsafail = ssh1_session_key(p);
1299 buffer_put_int(m, rsafail);
1300 buffer_put_bignum2(m, p);
1304 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1306 /* Turn on permissions for sessid passing */
1307 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1313 mm_answer_sessid(int socket, Buffer *m)
1317 debug3("%s entering", __func__);
1319 if (buffer_len(m) != 16)
1320 fatal("%s: bad ssh1 session id", __func__);
1321 for (i = 0; i < 16; i++)
1322 session_id[i] = buffer_get_char(m);
1324 /* Turn on permissions for getpwnam */
1325 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1331 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1335 u_char *blob = NULL;
1339 debug3("%s entering", __func__);
1341 if (options.rsa_authentication && authctxt->valid) {
1342 if ((client_n = BN_new()) == NULL)
1343 fatal("%s: BN_new", __func__);
1344 buffer_get_bignum2(m, client_n);
1345 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1346 BN_clear_free(client_n);
1349 buffer_put_int(m, allowed);
1350 buffer_put_int(m, forced_command != NULL);
1352 /* clear temporarily storage (used by generate challenge) */
1353 monitor_reset_key_state();
1355 if (allowed && key != NULL) {
1356 key->type = KEY_RSA; /* cheat for key_to_blob */
1357 if (key_to_blob(key, &blob, &blen) == 0)
1358 fatal("%s: key_to_blob failed", __func__);
1359 buffer_put_string(m, blob, blen);
1361 /* Save temporarily for comparison in verify */
1364 key_blobtype = MM_RSAUSERKEY;
1371 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1373 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1374 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1379 mm_answer_rsa_challenge(int socket, Buffer *m)
1385 debug3("%s entering", __func__);
1387 if (!authctxt->valid)
1388 fatal("%s: authctxt not valid", __func__);
1389 blob = buffer_get_string(m, &blen);
1390 if (!monitor_allowed_key(blob, blen))
1391 fatal("%s: bad key, not previously allowed", __func__);
1392 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1393 fatal("%s: key type mismatch", __func__);
1394 if ((key = key_from_blob(blob, blen)) == NULL)
1395 fatal("%s: received bad key", __func__);
1398 BN_clear_free(ssh1_challenge);
1399 ssh1_challenge = auth_rsa_generate_challenge(key);
1402 buffer_put_bignum2(m, ssh1_challenge);
1404 debug3("%s sending reply", __func__);
1405 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1407 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1415 mm_answer_rsa_response(int socket, Buffer *m)
1418 u_char *blob, *response;
1422 debug3("%s entering", __func__);
1424 if (!authctxt->valid)
1425 fatal("%s: authctxt not valid", __func__);
1426 if (ssh1_challenge == NULL)
1427 fatal("%s: no ssh1_challenge", __func__);
1429 blob = buffer_get_string(m, &blen);
1430 if (!monitor_allowed_key(blob, blen))
1431 fatal("%s: bad key, not previously allowed", __func__);
1432 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1433 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1434 if ((key = key_from_blob(blob, blen)) == NULL)
1435 fatal("%s: received bad key", __func__);
1436 response = buffer_get_string(m, &len);
1438 fatal("%s: received bad response to challenge", __func__);
1439 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1445 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1448 BN_clear_free(ssh1_challenge);
1449 ssh1_challenge = NULL;
1450 monitor_reset_key_state();
1453 buffer_put_int(m, success);
1454 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1461 mm_answer_krb5(int socket, Buffer *m)
1463 krb5_data tkt, reply;
1468 /* use temporary var to avoid size issues on 64bit arch */
1469 tkt.data = buffer_get_string(m, &len);
1472 success = options.kerberos_authentication &&
1474 auth_krb5(authctxt, &tkt, &client_user, &reply);
1480 buffer_put_int(m, success);
1483 buffer_put_cstring(m, client_user);
1484 buffer_put_string(m, reply.data, reply.length);
1490 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1492 auth_method = "kerberos";
1499 mm_answer_term(int socket, Buffer *req)
1501 extern struct monitor *pmonitor;
1504 debug3("%s: tearing down sessions", __func__);
1506 /* The child is terminating */
1507 session_destroy_all(&mm_session_close);
1509 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1513 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1515 /* Terminate process */
1520 monitor_apply_keystate(struct monitor *pmonitor)
1523 set_newkeys(MODE_IN);
1524 set_newkeys(MODE_OUT);
1526 packet_set_protocol_flags(child_state.ssh1protoflags);
1527 packet_set_encryption_key(child_state.ssh1key,
1528 child_state.ssh1keylen, child_state.ssh1cipher);
1529 xfree(child_state.ssh1key);
1532 /* for rc4 and other stateful ciphers */
1533 packet_set_keycontext(MODE_OUT, child_state.keyout);
1534 xfree(child_state.keyout);
1535 packet_set_keycontext(MODE_IN, child_state.keyin);
1536 xfree(child_state.keyin);
1539 packet_set_iv(MODE_OUT, child_state.ivout);
1540 xfree(child_state.ivout);
1541 packet_set_iv(MODE_IN, child_state.ivin);
1542 xfree(child_state.ivin);
1545 memcpy(&incoming_stream, &child_state.incoming,
1546 sizeof(incoming_stream));
1547 memcpy(&outgoing_stream, &child_state.outgoing,
1548 sizeof(outgoing_stream));
1550 /* Update with new address */
1551 if (options.compression)
1552 mm_init_compression(pmonitor->m_zlib);
1554 /* Network I/O buffers */
1555 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1556 buffer_clear(&input);
1557 buffer_append(&input, child_state.input, child_state.ilen);
1558 memset(child_state.input, 0, child_state.ilen);
1559 xfree(child_state.input);
1561 buffer_clear(&output);
1562 buffer_append(&output, child_state.output, child_state.olen);
1563 memset(child_state.output, 0, child_state.olen);
1564 xfree(child_state.output);
1568 mm_get_kex(Buffer *m)
1574 kex = xmalloc(sizeof(*kex));
1575 memset(kex, 0, sizeof(*kex));
1576 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1577 if ((session_id2 == NULL) ||
1578 (kex->session_id_len != session_id2_len) ||
1579 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1580 fatal("mm_get_get: internal error: bad session id");
1581 kex->we_need = buffer_get_int(m);
1582 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1583 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1585 kex->hostkey_type = buffer_get_int(m);
1586 kex->kex_type = buffer_get_int(m);
1587 blob = buffer_get_string(m, &bloblen);
1588 buffer_init(&kex->my);
1589 buffer_append(&kex->my, blob, bloblen);
1591 blob = buffer_get_string(m, &bloblen);
1592 buffer_init(&kex->peer);
1593 buffer_append(&kex->peer, blob, bloblen);
1596 kex->flags = buffer_get_int(m);
1597 kex->client_version_string = buffer_get_string(m, NULL);
1598 kex->server_version_string = buffer_get_string(m, NULL);
1599 kex->load_host_key=&get_hostkey_by_type;
1600 kex->host_key_index=&get_hostkey_index;
1605 /* This function requries careful sanity checking */
1608 mm_get_keystate(struct monitor *pmonitor)
1612 u_int bloblen, plen;
1613 u_int32_t seqnr, packets;
1616 debug3("%s: Waiting for new keys", __func__);
1619 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1621 child_state.ssh1protoflags = buffer_get_int(&m);
1622 child_state.ssh1cipher = buffer_get_int(&m);
1623 child_state.ssh1key = buffer_get_string(&m,
1624 &child_state.ssh1keylen);
1625 child_state.ivout = buffer_get_string(&m,
1626 &child_state.ivoutlen);
1627 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1630 /* Get the Kex for rekeying */
1631 *pmonitor->m_pkex = mm_get_kex(&m);
1634 blob = buffer_get_string(&m, &bloblen);
1635 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1638 debug3("%s: Waiting for second key", __func__);
1639 blob = buffer_get_string(&m, &bloblen);
1640 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1643 /* Now get sequence numbers for the packets */
1644 seqnr = buffer_get_int(&m);
1645 blocks = buffer_get_int64(&m);
1646 packets = buffer_get_int(&m);
1647 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1648 seqnr = buffer_get_int(&m);
1649 blocks = buffer_get_int64(&m);
1650 packets = buffer_get_int(&m);
1651 packet_set_state(MODE_IN, seqnr, blocks, packets);
1654 /* Get the key context */
1655 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1656 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1658 debug3("%s: Getting compression state", __func__);
1659 /* Get compression state */
1660 p = buffer_get_string(&m, &plen);
1661 if (plen != sizeof(child_state.outgoing))
1662 fatal("%s: bad request size", __func__);
1663 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1666 p = buffer_get_string(&m, &plen);
1667 if (plen != sizeof(child_state.incoming))
1668 fatal("%s: bad request size", __func__);
1669 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1672 /* Network I/O buffers */
1673 debug3("%s: Getting Network I/O buffers", __func__);
1674 child_state.input = buffer_get_string(&m, &child_state.ilen);
1675 child_state.output = buffer_get_string(&m, &child_state.olen);
1681 /* Allocation functions for zlib */
1683 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1685 size_t len = (size_t) size * ncount;
1688 if (len == 0 || ncount > SIZE_T_MAX / size)
1689 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1691 address = mm_malloc(mm, len);
1697 mm_zfree(struct mm_master *mm, void *address)
1699 mm_free(mm, address);
1703 mm_init_compression(struct mm_master *mm)
1705 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1706 outgoing_stream.zfree = (free_func)mm_zfree;
1707 outgoing_stream.opaque = mm;
1709 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1710 incoming_stream.zfree = (free_func)mm_zfree;
1711 incoming_stream.opaque = mm;
1716 #define FD_CLOSEONEXEC(x) do { \
1717 if (fcntl(x, F_SETFD, 1) == -1) \
1718 fatal("fcntl(%d, F_SETFD)", x); \
1722 monitor_socketpair(int *pair)
1724 #ifdef HAVE_SOCKETPAIR
1725 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1726 fatal("%s: socketpair", __func__);
1728 fatal("%s: UsePrivilegeSeparation=yes not supported",
1731 FD_CLOSEONEXEC(pair[0]);
1732 FD_CLOSEONEXEC(pair[1]);
1735 #define MM_MEMSIZE 65536
1740 struct monitor *mon;
1743 mon = xmalloc(sizeof(*mon));
1745 monitor_socketpair(pair);
1747 mon->m_recvfd = pair[0];
1748 mon->m_sendfd = pair[1];
1750 /* Used to share zlib space across processes */
1751 if (options.compression) {
1752 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1753 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1755 /* Compression needs to share state across borders */
1756 mm_init_compression(mon->m_zlib);
1763 monitor_reinit(struct monitor *mon)
1767 monitor_socketpair(pair);
1769 mon->m_recvfd = pair[0];
1770 mon->m_sendfd = pair[1];