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.41 2003/05/24 09:30:40 djm 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 montior 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_init_ctx(int, Buffer *);
122 int mm_answer_pam_query(int, Buffer *);
123 int mm_answer_pam_respond(int, Buffer *);
124 int mm_answer_pam_free_ctx(int, Buffer *);
128 int mm_answer_krb4(int, Buffer *);
131 int mm_answer_krb5(int, Buffer *);
134 static Authctxt *authctxt;
135 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
137 /* local state for key verify */
138 static u_char *key_blob = NULL;
139 static u_int key_bloblen = 0;
140 static int key_blobtype = MM_NOKEY;
141 static char *hostbased_cuser = NULL;
142 static char *hostbased_chost = NULL;
143 static char *auth_method = "unknown";
144 static int session_id2_len = 0;
145 static u_char *session_id2 = NULL;
146 static pid_t monitor_child_pid;
149 enum monitor_reqtype type;
151 int (*f)(int, Buffer *);
154 #define MON_ISAUTH 0x0004 /* Required for Authentication */
155 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
156 #define MON_ONCE 0x0010 /* Disable after calling */
158 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
160 #define MON_PERMIT 0x1000 /* Request is permitted */
162 struct mon_table mon_dispatch_proto20[] = {
163 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
164 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
165 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
166 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
167 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
168 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
170 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
171 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
172 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
173 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
174 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
177 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
178 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
181 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
182 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
184 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
185 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
187 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
192 struct mon_table mon_dispatch_postauth20[] = {
193 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
194 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
195 {MONITOR_REQ_PTY, 0, mm_answer_pty},
196 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
197 {MONITOR_REQ_TERM, 0, mm_answer_term},
201 struct mon_table mon_dispatch_proto15[] = {
202 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
203 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
204 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
205 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
206 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
207 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
208 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
209 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
211 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
212 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
215 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
216 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
219 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
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_KRB4, MON_ONCE|MON_AUTH, mm_answer_krb4},
229 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
234 struct mon_table mon_dispatch_postauth15[] = {
235 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
236 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
237 {MONITOR_REQ_TERM, 0, mm_answer_term},
241 struct mon_table *mon_dispatch;
243 /* Specifies if a certain message is allowed at the moment */
246 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
248 while (ent->f != NULL) {
249 if (ent->type == type) {
250 ent->flags &= ~MON_PERMIT;
251 ent->flags |= permit ? MON_PERMIT : 0;
259 monitor_permit_authentications(int permit)
261 struct mon_table *ent = mon_dispatch;
263 while (ent->f != NULL) {
264 if (ent->flags & MON_AUTH) {
265 ent->flags &= ~MON_PERMIT;
266 ent->flags |= permit ? MON_PERMIT : 0;
273 monitor_child_preauth(struct monitor *pmonitor)
275 struct mon_table *ent;
276 int authenticated = 0;
278 debug3("preauth child monitor started");
281 mon_dispatch = mon_dispatch_proto20;
283 /* Permit requests for moduli and signatures */
284 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
285 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
287 mon_dispatch = mon_dispatch_proto15;
289 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
292 authctxt = authctxt_new();
294 /* The first few requests do not require asynchronous access */
295 while (!authenticated) {
296 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
298 if (!(ent->flags & MON_AUTHDECIDE))
299 fatal("%s: unexpected authentication from %d",
300 __func__, ent->type);
301 if (authctxt->pw->pw_uid == 0 &&
302 !auth_root_allowed(auth_method))
306 if (ent->flags & MON_AUTHDECIDE) {
307 auth_log(authctxt, authenticated, auth_method,
308 compat20 ? " ssh2" : "");
310 authctxt->failures++;
314 if (!authctxt->valid)
315 fatal("%s: authenticated invalid user", __func__);
317 debug("%s: %s has been authenticated by privileged process",
318 __func__, authctxt->user);
320 mm_get_keystate(pmonitor);
326 monitor_set_child_handler(pid_t pid)
328 monitor_child_pid = pid;
332 monitor_child_handler(int signal)
334 kill(monitor_child_pid, signal);
338 monitor_child_postauth(struct monitor *pmonitor)
340 monitor_set_child_handler(pmonitor->m_pid);
341 signal(SIGHUP, &monitor_child_handler);
342 signal(SIGTERM, &monitor_child_handler);
345 mon_dispatch = mon_dispatch_postauth20;
347 /* Permit requests for moduli and signatures */
348 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
349 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
350 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
353 mon_dispatch = mon_dispatch_postauth15;
354 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
357 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
358 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
362 monitor_read(pmonitor, mon_dispatch, NULL);
366 monitor_sync(struct monitor *pmonitor)
368 if (options.compression) {
369 /* The member allocation is not visible, so sync it */
370 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
375 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
376 struct mon_table **pent)
384 mm_request_receive(pmonitor->m_sendfd, &m);
385 type = buffer_get_char(&m);
387 debug3("%s: checking request %d", __func__, type);
389 while (ent->f != NULL) {
390 if (ent->type == type)
395 if (ent->f != NULL) {
396 if (!(ent->flags & MON_PERMIT))
397 fatal("%s: unpermitted request %d", __func__,
399 ret = (*ent->f)(pmonitor->m_sendfd, &m);
402 /* The child may use this request only once, disable it */
403 if (ent->flags & MON_ONCE) {
404 debug2("%s: %d used once, disabling now", __func__,
406 ent->flags &= ~MON_PERMIT;
415 fatal("%s: unsupported request: %d", __func__, type);
421 /* allowed key state */
423 monitor_allowed_key(u_char *blob, u_int bloblen)
425 /* make sure key is allowed */
426 if (key_blob == NULL || key_bloblen != bloblen ||
427 memcmp(key_blob, blob, key_bloblen))
433 monitor_reset_key_state(void)
436 if (key_blob != NULL)
438 if (hostbased_cuser != NULL)
439 xfree(hostbased_cuser);
440 if (hostbased_chost != NULL)
441 xfree(hostbased_chost);
444 key_blobtype = MM_NOKEY;
445 hostbased_cuser = NULL;
446 hostbased_chost = NULL;
450 mm_answer_moduli(int socket, Buffer *m)
455 min = buffer_get_int(m);
456 want = buffer_get_int(m);
457 max = buffer_get_int(m);
459 debug3("%s: got parameters: %d %d %d",
460 __func__, min, want, max);
461 /* We need to check here, too, in case the child got corrupted */
462 if (max < min || want < min || max < want)
463 fatal("%s: bad parameters: %d %d %d",
464 __func__, min, want, max);
468 dh = choose_dh(min, want, max);
470 buffer_put_char(m, 0);
473 /* Send first bignum */
474 buffer_put_char(m, 1);
475 buffer_put_bignum2(m, dh->p);
476 buffer_put_bignum2(m, dh->g);
480 mm_request_send(socket, MONITOR_ANS_MODULI, m);
485 mm_answer_sign(int socket, Buffer *m)
490 u_int siglen, datlen;
493 debug3("%s", __func__);
495 keyid = buffer_get_int(m);
496 p = buffer_get_string(m, &datlen);
499 fatal("%s: data length incorrect: %u", __func__, datlen);
501 /* save session id, it will be passed on the first call */
502 if (session_id2_len == 0) {
503 session_id2_len = datlen;
504 session_id2 = xmalloc(session_id2_len);
505 memcpy(session_id2, p, session_id2_len);
508 if ((key = get_hostkey_by_index(keyid)) == NULL)
509 fatal("%s: no hostkey from index %d", __func__, keyid);
510 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
511 fatal("%s: key_sign failed", __func__);
513 debug3("%s: signature %p(%u)", __func__, signature, siglen);
516 buffer_put_string(m, signature, siglen);
521 mm_request_send(socket, MONITOR_ANS_SIGN, m);
523 /* Turn on permissions for getpwnam */
524 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
529 /* Retrieves the password entry and also checks if the user is permitted */
532 mm_answer_pwnamallow(int socket, Buffer *m)
535 struct passwd *pwent;
538 debug3("%s", __func__);
540 if (authctxt->attempt++ != 0)
541 fatal("%s: multiple attempts for getpwnam", __func__);
543 login = buffer_get_string(m, NULL);
545 pwent = getpwnamallow(login);
547 authctxt->user = xstrdup(login);
548 setproctitle("%s [priv]", pwent ? login : "unknown");
554 buffer_put_char(m, 0);
559 authctxt->pw = pwent;
562 buffer_put_char(m, 1);
563 buffer_put_string(m, pwent, sizeof(struct passwd));
564 buffer_put_cstring(m, pwent->pw_name);
565 buffer_put_cstring(m, "*");
566 buffer_put_cstring(m, pwent->pw_gecos);
567 #ifdef HAVE_PW_CLASS_IN_PASSWD
568 buffer_put_cstring(m, pwent->pw_class);
570 buffer_put_cstring(m, pwent->pw_dir);
571 buffer_put_cstring(m, pwent->pw_shell);
574 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
575 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
577 /* For SSHv1 allow authentication now */
579 monitor_permit_authentications(1);
581 /* Allow service/style information on the auth context */
582 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
583 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
588 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
594 int mm_answer_auth2_read_banner(int socket, Buffer *m)
599 banner = auth2_read_banner();
600 buffer_put_cstring(m, banner != NULL ? banner : "");
601 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
610 mm_answer_authserv(int socket, Buffer *m)
612 monitor_permit_authentications(1);
614 authctxt->service = buffer_get_string(m, NULL);
615 authctxt->style = buffer_get_string(m, NULL);
616 debug3("%s: service=%s, style=%s",
617 __func__, authctxt->service, authctxt->style);
619 if (strlen(authctxt->style) == 0) {
620 xfree(authctxt->style);
621 authctxt->style = NULL;
628 mm_answer_authpassword(int socket, Buffer *m)
630 static int call_count;
635 passwd = buffer_get_string(m, &plen);
636 /* Only authenticate if the context is valid */
637 authenticated = options.password_authentication &&
638 auth_password(authctxt, passwd) && authctxt->valid;
639 memset(passwd, 0, strlen(passwd));
643 buffer_put_int(m, authenticated);
645 debug3("%s: sending result %d", __func__, authenticated);
646 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
649 if (plen == 0 && call_count == 1)
650 auth_method = "none";
652 auth_method = "password";
654 /* Causes monitor loop to terminate if authenticated */
655 return (authenticated);
660 mm_answer_bsdauthquery(int socket, Buffer *m)
662 char *name, *infotxt;
668 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
669 &prompts, &echo_on) < 0 ? 0 : 1;
672 buffer_put_int(m, success);
674 buffer_put_cstring(m, prompts[0]);
676 debug3("%s: sending challenge success: %u", __func__, success);
677 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
690 mm_answer_bsdauthrespond(int socket, Buffer *m)
695 if (authctxt->as == 0)
696 fatal("%s: no bsd auth session", __func__);
698 response = buffer_get_string(m, NULL);
699 authok = options.challenge_response_authentication &&
700 auth_userresponse(authctxt->as, response, 0);
702 debug3("%s: <%s> = <%d>", __func__, response, authok);
706 buffer_put_int(m, authok);
708 debug3("%s: sending authenticated: %d", __func__, authok);
709 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
711 auth_method = "bsdauth";
713 return (authok != 0);
719 mm_answer_skeyquery(int socket, Buffer *m)
722 char challenge[1024];
725 success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
728 buffer_put_int(m, success);
730 buffer_put_cstring(m, challenge);
732 debug3("%s: sending challenge success: %u", __func__, success);
733 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
739 mm_answer_skeyrespond(int socket, Buffer *m)
744 response = buffer_get_string(m, NULL);
746 authok = (options.challenge_response_authentication &&
748 skey_haskey(authctxt->pw->pw_name) == 0 &&
749 skey_passcheck(authctxt->pw->pw_name, response) != -1);
754 buffer_put_int(m, authok);
756 debug3("%s: sending authenticated: %d", __func__, authok);
757 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
759 auth_method = "skey";
761 return (authok != 0);
767 mm_answer_pam_start(int socket, Buffer *m)
771 if (!options.use_pam)
772 fatal("UsePAM not set, but ended up in %s anyway", __func__);
774 user = buffer_get_string(m, NULL);
783 static void *sshpam_ctxt, *sshpam_authok;
784 extern KbdintDevice sshpam_device;
787 mm_answer_pam_init_ctx(int socket, Buffer *m)
790 debug3("%s", __func__);
791 authctxt->user = buffer_get_string(m, NULL);
792 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
793 sshpam_authok = NULL;
795 if (sshpam_ctxt != NULL) {
796 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
797 buffer_put_int(m, 1);
799 buffer_put_int(m, 0);
801 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
806 mm_answer_pam_query(int socket, Buffer *m)
808 char *name, *info, **prompts;
812 debug3("%s", __func__);
813 sshpam_authok = NULL;
814 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
815 if (ret == 0 && num == 0)
816 sshpam_authok = sshpam_ctxt;
817 if (num > 1 || name == NULL || info == NULL)
820 buffer_put_int(m, ret);
821 buffer_put_cstring(m, name);
823 buffer_put_cstring(m, info);
825 buffer_put_int(m, num);
826 for (i = 0; i < num; ++i) {
827 buffer_put_cstring(m, prompts[i]);
829 buffer_put_int(m, echo_on[i]);
835 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
840 mm_answer_pam_respond(int socket, Buffer *m)
846 debug3("%s", __func__);
847 sshpam_authok = NULL;
848 num = buffer_get_int(m);
850 resp = xmalloc(num * sizeof(char *));
851 for (i = 0; i < num; ++i)
852 resp[i] = buffer_get_string(m, NULL);
853 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
854 for (i = 0; i < num; ++i)
858 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
861 buffer_put_int(m, ret);
862 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
863 auth_method = "keyboard-interactive/pam";
865 sshpam_authok = sshpam_ctxt;
870 mm_answer_pam_free_ctx(int socket, Buffer *m)
873 debug3("%s", __func__);
874 (sshpam_device.free_ctx)(sshpam_ctxt);
876 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
877 return (sshpam_authok == sshpam_ctxt);
882 mm_append_debug(Buffer *m)
884 if (auth_debug_init && buffer_len(&auth_debug)) {
885 debug3("%s: Appending debug messages for child", __func__);
886 buffer_append(m, buffer_ptr(&auth_debug),
887 buffer_len(&auth_debug));
888 buffer_clear(&auth_debug);
893 mm_answer_keyallowed(int socket, Buffer *m)
899 enum mm_keytype type = 0;
902 debug3("%s entering", __func__);
904 type = buffer_get_int(m);
905 cuser = buffer_get_string(m, NULL);
906 chost = buffer_get_string(m, NULL);
907 blob = buffer_get_string(m, &bloblen);
909 key = key_from_blob(blob, bloblen);
911 if ((compat20 && type == MM_RSAHOSTKEY) ||
912 (!compat20 && type != MM_RSAHOSTKEY))
913 fatal("%s: key type and protocol mismatch", __func__);
915 debug3("%s: key_from_blob: %p", __func__, key);
917 if (key != NULL && authctxt->pw != NULL) {
920 allowed = options.pubkey_authentication &&
921 user_key_allowed(authctxt->pw, key);
924 allowed = options.hostbased_authentication &&
925 hostbased_key_allowed(authctxt->pw,
929 key->type = KEY_RSA1; /* XXX */
930 allowed = options.rhosts_rsa_authentication &&
931 auth_rhosts_rsa_key_allowed(authctxt->pw,
935 fatal("%s: unknown key type %d", __func__, type);
942 /* clear temporarily storage (used by verify) */
943 monitor_reset_key_state();
946 /* Save temporarily for comparison in verify */
948 key_bloblen = bloblen;
950 hostbased_cuser = cuser;
951 hostbased_chost = chost;
954 debug3("%s: key %p is %s",
955 __func__, key, allowed ? "allowed" : "disallowed");
958 buffer_put_int(m, allowed);
959 buffer_put_int(m, forced_command != NULL);
963 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
965 if (type == MM_RSAHOSTKEY)
966 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
972 monitor_valid_userblob(u_char *data, u_int datalen)
980 buffer_append(&b, data, datalen);
982 if (datafellows & SSH_OLD_SESSIONID) {
984 len = buffer_len(&b);
985 if ((session_id2 == NULL) ||
986 (len < session_id2_len) ||
987 (memcmp(p, session_id2, session_id2_len) != 0))
989 buffer_consume(&b, session_id2_len);
991 p = buffer_get_string(&b, &len);
992 if ((session_id2 == NULL) ||
993 (len != session_id2_len) ||
994 (memcmp(p, session_id2, session_id2_len) != 0))
998 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1000 p = buffer_get_string(&b, NULL);
1001 if (strcmp(authctxt->user, p) != 0) {
1002 logit("wrong user name passed to monitor: expected %s != %.100s",
1007 buffer_skip_string(&b);
1008 if (datafellows & SSH_BUG_PKAUTH) {
1009 if (!buffer_get_char(&b))
1012 p = buffer_get_string(&b, NULL);
1013 if (strcmp("publickey", p) != 0)
1016 if (!buffer_get_char(&b))
1018 buffer_skip_string(&b);
1020 buffer_skip_string(&b);
1021 if (buffer_len(&b) != 0)
1028 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1037 buffer_append(&b, data, datalen);
1039 p = buffer_get_string(&b, &len);
1040 if ((session_id2 == NULL) ||
1041 (len != session_id2_len) ||
1042 (memcmp(p, session_id2, session_id2_len) != 0))
1046 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1048 p = buffer_get_string(&b, NULL);
1049 if (strcmp(authctxt->user, p) != 0) {
1050 logit("wrong user name passed to monitor: expected %s != %.100s",
1055 buffer_skip_string(&b); /* service */
1056 p = buffer_get_string(&b, NULL);
1057 if (strcmp(p, "hostbased") != 0)
1060 buffer_skip_string(&b); /* pkalg */
1061 buffer_skip_string(&b); /* pkblob */
1063 /* verify client host, strip trailing dot if necessary */
1064 p = buffer_get_string(&b, NULL);
1065 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1067 if (strcmp(p, chost) != 0)
1071 /* verify client user */
1072 p = buffer_get_string(&b, NULL);
1073 if (strcmp(p, cuser) != 0)
1077 if (buffer_len(&b) != 0)
1084 mm_answer_keyverify(int socket, Buffer *m)
1087 u_char *signature, *data, *blob;
1088 u_int signaturelen, datalen, bloblen;
1092 blob = buffer_get_string(m, &bloblen);
1093 signature = buffer_get_string(m, &signaturelen);
1094 data = buffer_get_string(m, &datalen);
1096 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1097 !monitor_allowed_key(blob, bloblen))
1098 fatal("%s: bad key, not previously allowed", __func__);
1100 key = key_from_blob(blob, bloblen);
1102 fatal("%s: bad public key blob", __func__);
1104 switch (key_blobtype) {
1106 valid_data = monitor_valid_userblob(data, datalen);
1109 valid_data = monitor_valid_hostbasedblob(data, datalen,
1110 hostbased_cuser, hostbased_chost);
1117 fatal("%s: bad signature data blob", __func__);
1119 verified = key_verify(key, signature, signaturelen, data, datalen);
1120 debug3("%s: key %p signature %s",
1121 __func__, key, verified ? "verified" : "unverified");
1128 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1130 monitor_reset_key_state();
1133 buffer_put_int(m, verified);
1134 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1140 mm_record_login(Session *s, struct passwd *pw)
1143 struct sockaddr_storage from;
1146 * Get IP address of client. If the connection is not a socket, let
1147 * the address be 0.0.0.0.
1149 memset(&from, 0, sizeof(from));
1150 fromlen = sizeof(from);
1151 if (packet_connection_is_on_socket()) {
1152 if (getpeername(packet_get_connection_in(),
1153 (struct sockaddr *) & from, &fromlen) < 0) {
1154 debug("getpeername: %.100s", strerror(errno));
1158 /* Record that there was a login on that tty from the remote host. */
1159 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1160 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
1161 (struct sockaddr *)&from, fromlen);
1165 mm_session_close(Session *s)
1167 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1168 if (s->ttyfd != -1) {
1169 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1170 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1171 session_pty_cleanup2(s);
1177 mm_answer_pty(int socket, Buffer *m)
1179 extern struct monitor *pmonitor;
1183 debug3("%s entering", __func__);
1189 s->authctxt = authctxt;
1190 s->pw = authctxt->pw;
1191 s->pid = pmonitor->m_pid;
1192 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1195 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1196 pty_setowner(authctxt->pw, s->tty);
1198 buffer_put_int(m, 1);
1199 buffer_put_cstring(m, s->tty);
1200 mm_request_send(socket, MONITOR_ANS_PTY, m);
1202 mm_send_fd(socket, s->ptyfd);
1203 mm_send_fd(socket, s->ttyfd);
1205 /* We need to trick ttyslot */
1206 if (dup2(s->ttyfd, 0) == -1)
1207 fatal("%s: dup2", __func__);
1209 mm_record_login(s, authctxt->pw);
1211 /* Now we can close the file descriptor again */
1214 /* make sure nothing uses fd 0 */
1215 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1216 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1218 error("%s: fd0 %d != 0", __func__, fd0);
1220 /* slave is not needed */
1222 s->ttyfd = s->ptyfd;
1223 /* no need to dup() because nobody closes ptyfd */
1224 s->ptymaster = s->ptyfd;
1226 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1232 mm_session_close(s);
1233 buffer_put_int(m, 0);
1234 mm_request_send(socket, MONITOR_ANS_PTY, m);
1239 mm_answer_pty_cleanup(int socket, Buffer *m)
1244 debug3("%s entering", __func__);
1246 tty = buffer_get_string(m, NULL);
1247 if ((s = session_by_tty(tty)) != NULL)
1248 mm_session_close(s);
1255 mm_answer_sesskey(int socket, Buffer *m)
1260 /* Turn off permissions */
1261 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1263 if ((p = BN_new()) == NULL)
1264 fatal("%s: BN_new", __func__);
1266 buffer_get_bignum2(m, p);
1268 rsafail = ssh1_session_key(p);
1271 buffer_put_int(m, rsafail);
1272 buffer_put_bignum2(m, p);
1276 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1278 /* Turn on permissions for sessid passing */
1279 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1285 mm_answer_sessid(int socket, Buffer *m)
1289 debug3("%s entering", __func__);
1291 if (buffer_len(m) != 16)
1292 fatal("%s: bad ssh1 session id", __func__);
1293 for (i = 0; i < 16; i++)
1294 session_id[i] = buffer_get_char(m);
1296 /* Turn on permissions for getpwnam */
1297 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1303 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1307 u_char *blob = NULL;
1311 debug3("%s entering", __func__);
1313 if (options.rsa_authentication && authctxt->valid) {
1314 if ((client_n = BN_new()) == NULL)
1315 fatal("%s: BN_new", __func__);
1316 buffer_get_bignum2(m, client_n);
1317 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1318 BN_clear_free(client_n);
1321 buffer_put_int(m, allowed);
1322 buffer_put_int(m, forced_command != NULL);
1324 /* clear temporarily storage (used by generate challenge) */
1325 monitor_reset_key_state();
1327 if (allowed && key != NULL) {
1328 key->type = KEY_RSA; /* cheat for key_to_blob */
1329 if (key_to_blob(key, &blob, &blen) == 0)
1330 fatal("%s: key_to_blob failed", __func__);
1331 buffer_put_string(m, blob, blen);
1333 /* Save temporarily for comparison in verify */
1336 key_blobtype = MM_RSAUSERKEY;
1343 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1345 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1346 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1351 mm_answer_rsa_challenge(int socket, Buffer *m)
1357 debug3("%s entering", __func__);
1359 if (!authctxt->valid)
1360 fatal("%s: authctxt not valid", __func__);
1361 blob = buffer_get_string(m, &blen);
1362 if (!monitor_allowed_key(blob, blen))
1363 fatal("%s: bad key, not previously allowed", __func__);
1364 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1365 fatal("%s: key type mismatch", __func__);
1366 if ((key = key_from_blob(blob, blen)) == NULL)
1367 fatal("%s: received bad key", __func__);
1370 BN_clear_free(ssh1_challenge);
1371 ssh1_challenge = auth_rsa_generate_challenge(key);
1374 buffer_put_bignum2(m, ssh1_challenge);
1376 debug3("%s sending reply", __func__);
1377 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1379 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1387 mm_answer_rsa_response(int socket, Buffer *m)
1390 u_char *blob, *response;
1394 debug3("%s entering", __func__);
1396 if (!authctxt->valid)
1397 fatal("%s: authctxt not valid", __func__);
1398 if (ssh1_challenge == NULL)
1399 fatal("%s: no ssh1_challenge", __func__);
1401 blob = buffer_get_string(m, &blen);
1402 if (!monitor_allowed_key(blob, blen))
1403 fatal("%s: bad key, not previously allowed", __func__);
1404 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1405 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1406 if ((key = key_from_blob(blob, blen)) == NULL)
1407 fatal("%s: received bad key", __func__);
1408 response = buffer_get_string(m, &len);
1410 fatal("%s: received bad response to challenge", __func__);
1411 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1417 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1420 BN_clear_free(ssh1_challenge);
1421 ssh1_challenge = NULL;
1422 monitor_reset_key_state();
1425 buffer_put_int(m, success);
1426 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1433 mm_answer_krb4(int socket, Buffer *m)
1435 KTEXT_ST auth, reply;
1440 reply.length = auth.length = 0;
1442 p = buffer_get_string(m, &alen);
1443 if (alen >= MAX_KTXT_LEN)
1444 fatal("%s: auth too large", __func__);
1445 memcpy(auth.dat, p, alen);
1450 success = options.kerberos_authentication &&
1452 auth_krb4(authctxt, &auth, &client, &reply);
1454 memset(auth.dat, 0, alen);
1456 buffer_put_int(m, success);
1459 buffer_put_cstring(m, client);
1460 buffer_put_string(m, reply.dat, reply.length);
1464 memset(reply.dat, 0, reply.length);
1467 debug3("%s: sending result %d", __func__, success);
1468 mm_request_send(socket, MONITOR_ANS_KRB4, m);
1470 auth_method = "kerberos";
1472 /* Causes monitor loop to terminate if authenticated */
1479 mm_answer_krb5(int socket, Buffer *m)
1481 krb5_data tkt, reply;
1486 /* use temporary var to avoid size issues on 64bit arch */
1487 tkt.data = buffer_get_string(m, &len);
1490 success = options.kerberos_authentication &&
1492 auth_krb5(authctxt, &tkt, &client_user, &reply);
1498 buffer_put_int(m, success);
1501 buffer_put_cstring(m, client_user);
1502 buffer_put_string(m, reply.data, reply.length);
1508 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1510 auth_method = "kerberos";
1517 mm_answer_term(int socket, Buffer *req)
1519 extern struct monitor *pmonitor;
1522 debug3("%s: tearing down sessions", __func__);
1524 /* The child is terminating */
1525 session_destroy_all(&mm_session_close);
1527 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1531 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1533 /* Terminate process */
1538 monitor_apply_keystate(struct monitor *pmonitor)
1541 set_newkeys(MODE_IN);
1542 set_newkeys(MODE_OUT);
1544 packet_set_protocol_flags(child_state.ssh1protoflags);
1545 packet_set_encryption_key(child_state.ssh1key,
1546 child_state.ssh1keylen, child_state.ssh1cipher);
1547 xfree(child_state.ssh1key);
1550 /* for rc4 and other stateful ciphers */
1551 packet_set_keycontext(MODE_OUT, child_state.keyout);
1552 xfree(child_state.keyout);
1553 packet_set_keycontext(MODE_IN, child_state.keyin);
1554 xfree(child_state.keyin);
1557 packet_set_iv(MODE_OUT, child_state.ivout);
1558 xfree(child_state.ivout);
1559 packet_set_iv(MODE_IN, child_state.ivin);
1560 xfree(child_state.ivin);
1563 memcpy(&incoming_stream, &child_state.incoming,
1564 sizeof(incoming_stream));
1565 memcpy(&outgoing_stream, &child_state.outgoing,
1566 sizeof(outgoing_stream));
1568 /* Update with new address */
1569 if (options.compression)
1570 mm_init_compression(pmonitor->m_zlib);
1572 /* Network I/O buffers */
1573 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1574 buffer_clear(&input);
1575 buffer_append(&input, child_state.input, child_state.ilen);
1576 memset(child_state.input, 0, child_state.ilen);
1577 xfree(child_state.input);
1579 buffer_clear(&output);
1580 buffer_append(&output, child_state.output, child_state.olen);
1581 memset(child_state.output, 0, child_state.olen);
1582 xfree(child_state.output);
1586 mm_get_kex(Buffer *m)
1592 kex = xmalloc(sizeof(*kex));
1593 memset(kex, 0, sizeof(*kex));
1594 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1595 if ((session_id2 == NULL) ||
1596 (kex->session_id_len != session_id2_len) ||
1597 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1598 fatal("mm_get_get: internal error: bad session id");
1599 kex->we_need = buffer_get_int(m);
1600 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1601 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1603 kex->hostkey_type = buffer_get_int(m);
1604 kex->kex_type = buffer_get_int(m);
1605 blob = buffer_get_string(m, &bloblen);
1606 buffer_init(&kex->my);
1607 buffer_append(&kex->my, blob, bloblen);
1609 blob = buffer_get_string(m, &bloblen);
1610 buffer_init(&kex->peer);
1611 buffer_append(&kex->peer, blob, bloblen);
1614 kex->flags = buffer_get_int(m);
1615 kex->client_version_string = buffer_get_string(m, NULL);
1616 kex->server_version_string = buffer_get_string(m, NULL);
1617 kex->load_host_key=&get_hostkey_by_type;
1618 kex->host_key_index=&get_hostkey_index;
1623 /* This function requries careful sanity checking */
1626 mm_get_keystate(struct monitor *pmonitor)
1630 u_int bloblen, plen;
1631 u_int32_t seqnr, packets;
1634 debug3("%s: Waiting for new keys", __func__);
1637 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1639 child_state.ssh1protoflags = buffer_get_int(&m);
1640 child_state.ssh1cipher = buffer_get_int(&m);
1641 child_state.ssh1key = buffer_get_string(&m,
1642 &child_state.ssh1keylen);
1643 child_state.ivout = buffer_get_string(&m,
1644 &child_state.ivoutlen);
1645 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1648 /* Get the Kex for rekeying */
1649 *pmonitor->m_pkex = mm_get_kex(&m);
1652 blob = buffer_get_string(&m, &bloblen);
1653 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1656 debug3("%s: Waiting for second key", __func__);
1657 blob = buffer_get_string(&m, &bloblen);
1658 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1661 /* Now get sequence numbers for the packets */
1662 seqnr = buffer_get_int(&m);
1663 blocks = buffer_get_int64(&m);
1664 packets = buffer_get_int(&m);
1665 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1666 seqnr = buffer_get_int(&m);
1667 blocks = buffer_get_int64(&m);
1668 packets = buffer_get_int(&m);
1669 packet_set_state(MODE_IN, seqnr, blocks, packets);
1672 /* Get the key context */
1673 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1674 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1676 debug3("%s: Getting compression state", __func__);
1677 /* Get compression state */
1678 p = buffer_get_string(&m, &plen);
1679 if (plen != sizeof(child_state.outgoing))
1680 fatal("%s: bad request size", __func__);
1681 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1684 p = buffer_get_string(&m, &plen);
1685 if (plen != sizeof(child_state.incoming))
1686 fatal("%s: bad request size", __func__);
1687 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1690 /* Network I/O buffers */
1691 debug3("%s: Getting Network I/O buffers", __func__);
1692 child_state.input = buffer_get_string(&m, &child_state.ilen);
1693 child_state.output = buffer_get_string(&m, &child_state.olen);
1699 /* Allocation functions for zlib */
1701 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1703 size_t len = (size_t) size * ncount;
1706 if (len == 0 || ncount > SIZE_T_MAX / size)
1707 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1709 address = mm_malloc(mm, len);
1715 mm_zfree(struct mm_master *mm, void *address)
1717 mm_free(mm, address);
1721 mm_init_compression(struct mm_master *mm)
1723 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1724 outgoing_stream.zfree = (free_func)mm_zfree;
1725 outgoing_stream.opaque = mm;
1727 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1728 incoming_stream.zfree = (free_func)mm_zfree;
1729 incoming_stream.opaque = mm;
1734 #define FD_CLOSEONEXEC(x) do { \
1735 if (fcntl(x, F_SETFD, 1) == -1) \
1736 fatal("fcntl(%d, F_SETFD)", x); \
1740 monitor_socketpair(int *pair)
1742 #ifdef HAVE_SOCKETPAIR
1743 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1744 fatal("%s: socketpair", __func__);
1746 fatal("%s: UsePrivilegeSeparation=yes not supported",
1749 FD_CLOSEONEXEC(pair[0]);
1750 FD_CLOSEONEXEC(pair[1]);
1753 #define MM_MEMSIZE 65536
1758 struct monitor *mon;
1761 mon = xmalloc(sizeof(*mon));
1763 monitor_socketpair(pair);
1765 mon->m_recvfd = pair[0];
1766 mon->m_sendfd = pair[1];
1768 /* Used to share zlib space across processes */
1769 if (options.compression) {
1770 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1771 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1773 /* Compression needs to share state across borders */
1774 mm_init_compression(mon->m_zlib);
1781 monitor_reinit(struct monitor *mon)
1785 monitor_socketpair(pair);
1787 mon->m_recvfd = pair[0];
1788 mon->m_sendfd = pair[1];