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_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_krb5(int, Buffer *);
131 static Authctxt *authctxt;
132 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
134 /* local state for key verify */
135 static u_char *key_blob = NULL;
136 static u_int key_bloblen = 0;
137 static int key_blobtype = MM_NOKEY;
138 static char *hostbased_cuser = NULL;
139 static char *hostbased_chost = NULL;
140 static char *auth_method = "unknown";
141 static u_int session_id2_len = 0;
142 static u_char *session_id2 = NULL;
143 static pid_t monitor_child_pid;
146 enum monitor_reqtype type;
148 int (*f)(int, Buffer *);
151 #define MON_ISAUTH 0x0004 /* Required for Authentication */
152 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
153 #define MON_ONCE 0x0010 /* Disable after calling */
155 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
157 #define MON_PERMIT 0x1000 /* Request is permitted */
159 struct mon_table mon_dispatch_proto20[] = {
160 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
161 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
162 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
163 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
164 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
165 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
167 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
168 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
169 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
170 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
171 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
174 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
175 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
178 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
179 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
181 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
182 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
184 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
189 struct mon_table mon_dispatch_postauth20[] = {
190 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
191 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
192 {MONITOR_REQ_PTY, 0, mm_answer_pty},
193 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
194 {MONITOR_REQ_TERM, 0, mm_answer_term},
198 struct mon_table mon_dispatch_proto15[] = {
199 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
200 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
201 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
202 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
203 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
204 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
205 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
206 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
208 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
209 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
212 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
213 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
216 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
217 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
218 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
219 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
220 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
223 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
228 struct mon_table mon_dispatch_postauth15[] = {
229 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
230 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
231 {MONITOR_REQ_TERM, 0, mm_answer_term},
235 struct mon_table *mon_dispatch;
237 /* Specifies if a certain message is allowed at the moment */
240 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
242 while (ent->f != NULL) {
243 if (ent->type == type) {
244 ent->flags &= ~MON_PERMIT;
245 ent->flags |= permit ? MON_PERMIT : 0;
253 monitor_permit_authentications(int permit)
255 struct mon_table *ent = mon_dispatch;
257 while (ent->f != NULL) {
258 if (ent->flags & MON_AUTH) {
259 ent->flags &= ~MON_PERMIT;
260 ent->flags |= permit ? MON_PERMIT : 0;
267 monitor_child_preauth(struct monitor *pmonitor)
269 struct mon_table *ent;
270 int authenticated = 0;
272 debug3("preauth child monitor started");
275 mon_dispatch = mon_dispatch_proto20;
277 /* Permit requests for moduli and signatures */
278 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
279 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
281 mon_dispatch = mon_dispatch_proto15;
283 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
286 authctxt = authctxt_new();
288 /* The first few requests do not require asynchronous access */
289 while (!authenticated) {
290 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
292 if (!(ent->flags & MON_AUTHDECIDE))
293 fatal("%s: unexpected authentication from %d",
294 __func__, ent->type);
295 if (authctxt->pw->pw_uid == 0 &&
296 !auth_root_allowed(auth_method))
300 if (ent->flags & MON_AUTHDECIDE) {
301 auth_log(authctxt, authenticated, auth_method,
302 compat20 ? " ssh2" : "");
304 authctxt->failures++;
308 if (!authctxt->valid)
309 fatal("%s: authenticated invalid user", __func__);
311 debug("%s: %s has been authenticated by privileged process",
312 __func__, authctxt->user);
314 mm_get_keystate(pmonitor);
320 monitor_set_child_handler(pid_t pid)
322 monitor_child_pid = pid;
326 monitor_child_handler(int signal)
328 kill(monitor_child_pid, signal);
332 monitor_child_postauth(struct monitor *pmonitor)
334 monitor_set_child_handler(pmonitor->m_pid);
335 signal(SIGHUP, &monitor_child_handler);
336 signal(SIGTERM, &monitor_child_handler);
339 mon_dispatch = mon_dispatch_postauth20;
341 /* Permit requests for moduli and signatures */
342 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
343 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
344 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
347 mon_dispatch = mon_dispatch_postauth15;
348 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
351 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
352 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
356 monitor_read(pmonitor, mon_dispatch, NULL);
360 monitor_sync(struct monitor *pmonitor)
362 if (options.compression) {
363 /* The member allocation is not visible, so sync it */
364 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
369 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
370 struct mon_table **pent)
378 mm_request_receive(pmonitor->m_sendfd, &m);
379 type = buffer_get_char(&m);
381 debug3("%s: checking request %d", __func__, type);
383 while (ent->f != NULL) {
384 if (ent->type == type)
389 if (ent->f != NULL) {
390 if (!(ent->flags & MON_PERMIT))
391 fatal("%s: unpermitted request %d", __func__,
393 ret = (*ent->f)(pmonitor->m_sendfd, &m);
396 /* The child may use this request only once, disable it */
397 if (ent->flags & MON_ONCE) {
398 debug2("%s: %d used once, disabling now", __func__,
400 ent->flags &= ~MON_PERMIT;
409 fatal("%s: unsupported request: %d", __func__, type);
415 /* allowed key state */
417 monitor_allowed_key(u_char *blob, u_int bloblen)
419 /* make sure key is allowed */
420 if (key_blob == NULL || key_bloblen != bloblen ||
421 memcmp(key_blob, blob, key_bloblen))
427 monitor_reset_key_state(void)
430 if (key_blob != NULL)
432 if (hostbased_cuser != NULL)
433 xfree(hostbased_cuser);
434 if (hostbased_chost != NULL)
435 xfree(hostbased_chost);
438 key_blobtype = MM_NOKEY;
439 hostbased_cuser = NULL;
440 hostbased_chost = NULL;
444 mm_answer_moduli(int socket, Buffer *m)
449 min = buffer_get_int(m);
450 want = buffer_get_int(m);
451 max = buffer_get_int(m);
453 debug3("%s: got parameters: %d %d %d",
454 __func__, min, want, max);
455 /* We need to check here, too, in case the child got corrupted */
456 if (max < min || want < min || max < want)
457 fatal("%s: bad parameters: %d %d %d",
458 __func__, min, want, max);
462 dh = choose_dh(min, want, max);
464 buffer_put_char(m, 0);
467 /* Send first bignum */
468 buffer_put_char(m, 1);
469 buffer_put_bignum2(m, dh->p);
470 buffer_put_bignum2(m, dh->g);
474 mm_request_send(socket, MONITOR_ANS_MODULI, m);
479 mm_answer_sign(int socket, Buffer *m)
484 u_int siglen, datlen;
487 debug3("%s", __func__);
489 keyid = buffer_get_int(m);
490 p = buffer_get_string(m, &datlen);
493 fatal("%s: data length incorrect: %u", __func__, datlen);
495 /* save session id, it will be passed on the first call */
496 if (session_id2_len == 0) {
497 session_id2_len = datlen;
498 session_id2 = xmalloc(session_id2_len);
499 memcpy(session_id2, p, session_id2_len);
502 if ((key = get_hostkey_by_index(keyid)) == NULL)
503 fatal("%s: no hostkey from index %d", __func__, keyid);
504 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
505 fatal("%s: key_sign failed", __func__);
507 debug3("%s: signature %p(%u)", __func__, signature, siglen);
510 buffer_put_string(m, signature, siglen);
515 mm_request_send(socket, MONITOR_ANS_SIGN, m);
517 /* Turn on permissions for getpwnam */
518 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
523 /* Retrieves the password entry and also checks if the user is permitted */
526 mm_answer_pwnamallow(int socket, Buffer *m)
529 struct passwd *pwent;
532 debug3("%s", __func__);
534 if (authctxt->attempt++ != 0)
535 fatal("%s: multiple attempts for getpwnam", __func__);
537 login = buffer_get_string(m, NULL);
539 pwent = getpwnamallow(login);
541 authctxt->user = xstrdup(login);
542 setproctitle("%s [priv]", pwent ? login : "unknown");
548 buffer_put_char(m, 0);
553 authctxt->pw = pwent;
556 buffer_put_char(m, 1);
557 buffer_put_string(m, pwent, sizeof(struct passwd));
558 buffer_put_cstring(m, pwent->pw_name);
559 buffer_put_cstring(m, "*");
560 buffer_put_cstring(m, pwent->pw_gecos);
561 #ifdef HAVE_PW_CLASS_IN_PASSWD
562 buffer_put_cstring(m, pwent->pw_class);
564 buffer_put_cstring(m, pwent->pw_dir);
565 buffer_put_cstring(m, pwent->pw_shell);
568 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
569 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
571 /* For SSHv1 allow authentication now */
573 monitor_permit_authentications(1);
575 /* Allow service/style information on the auth context */
576 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
577 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
582 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
588 int mm_answer_auth2_read_banner(int socket, Buffer *m)
593 banner = auth2_read_banner();
594 buffer_put_cstring(m, banner != NULL ? banner : "");
595 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
604 mm_answer_authserv(int socket, Buffer *m)
606 monitor_permit_authentications(1);
608 authctxt->service = buffer_get_string(m, NULL);
609 authctxt->style = buffer_get_string(m, NULL);
610 debug3("%s: service=%s, style=%s",
611 __func__, authctxt->service, authctxt->style);
613 if (strlen(authctxt->style) == 0) {
614 xfree(authctxt->style);
615 authctxt->style = NULL;
622 mm_answer_authpassword(int socket, Buffer *m)
624 static int call_count;
629 passwd = buffer_get_string(m, &plen);
630 /* Only authenticate if the context is valid */
631 authenticated = options.password_authentication &&
632 auth_password(authctxt, passwd) && authctxt->valid;
633 memset(passwd, 0, strlen(passwd));
637 buffer_put_int(m, authenticated);
639 debug3("%s: sending result %d", __func__, authenticated);
640 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
643 if (plen == 0 && call_count == 1)
644 auth_method = "none";
646 auth_method = "password";
648 /* Causes monitor loop to terminate if authenticated */
649 return (authenticated);
654 mm_answer_bsdauthquery(int socket, Buffer *m)
656 char *name, *infotxt;
662 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
663 &prompts, &echo_on) < 0 ? 0 : 1;
666 buffer_put_int(m, success);
668 buffer_put_cstring(m, prompts[0]);
670 debug3("%s: sending challenge success: %u", __func__, success);
671 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
684 mm_answer_bsdauthrespond(int socket, Buffer *m)
689 if (authctxt->as == 0)
690 fatal("%s: no bsd auth session", __func__);
692 response = buffer_get_string(m, NULL);
693 authok = options.challenge_response_authentication &&
694 auth_userresponse(authctxt->as, response, 0);
696 debug3("%s: <%s> = <%d>", __func__, response, authok);
700 buffer_put_int(m, authok);
702 debug3("%s: sending authenticated: %d", __func__, authok);
703 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
705 auth_method = "bsdauth";
707 return (authok != 0);
713 mm_answer_skeyquery(int socket, Buffer *m)
716 char challenge[1024];
719 success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
722 buffer_put_int(m, success);
724 buffer_put_cstring(m, challenge);
726 debug3("%s: sending challenge success: %u", __func__, success);
727 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
733 mm_answer_skeyrespond(int socket, Buffer *m)
738 response = buffer_get_string(m, NULL);
740 authok = (options.challenge_response_authentication &&
742 skey_haskey(authctxt->pw->pw_name) == 0 &&
743 skey_passcheck(authctxt->pw->pw_name, response) != -1);
748 buffer_put_int(m, authok);
750 debug3("%s: sending authenticated: %d", __func__, authok);
751 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
753 auth_method = "skey";
755 return (authok != 0);
761 mm_answer_pam_start(int socket, Buffer *m)
765 if (!options.use_pam)
766 fatal("UsePAM not set, but ended up in %s anyway", __func__);
768 user = buffer_get_string(m, NULL);
777 static void *sshpam_ctxt, *sshpam_authok;
778 extern KbdintDevice sshpam_device;
781 mm_answer_pam_init_ctx(int socket, Buffer *m)
784 debug3("%s", __func__);
785 authctxt->user = buffer_get_string(m, NULL);
786 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
787 sshpam_authok = NULL;
789 if (sshpam_ctxt != NULL) {
790 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
791 buffer_put_int(m, 1);
793 buffer_put_int(m, 0);
795 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
800 mm_answer_pam_query(int socket, Buffer *m)
802 char *name, *info, **prompts;
806 debug3("%s", __func__);
807 sshpam_authok = NULL;
808 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
809 if (ret == 0 && num == 0)
810 sshpam_authok = sshpam_ctxt;
811 if (num > 1 || name == NULL || info == NULL)
814 buffer_put_int(m, ret);
815 buffer_put_cstring(m, name);
817 buffer_put_cstring(m, info);
819 buffer_put_int(m, num);
820 for (i = 0; i < num; ++i) {
821 buffer_put_cstring(m, prompts[i]);
823 buffer_put_int(m, echo_on[i]);
829 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
834 mm_answer_pam_respond(int socket, Buffer *m)
840 debug3("%s", __func__);
841 sshpam_authok = NULL;
842 num = buffer_get_int(m);
844 resp = xmalloc(num * sizeof(char *));
845 for (i = 0; i < num; ++i)
846 resp[i] = buffer_get_string(m, NULL);
847 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
848 for (i = 0; i < num; ++i)
852 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
855 buffer_put_int(m, ret);
856 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
857 auth_method = "keyboard-interactive/pam";
859 sshpam_authok = sshpam_ctxt;
864 mm_answer_pam_free_ctx(int socket, Buffer *m)
867 debug3("%s", __func__);
868 (sshpam_device.free_ctx)(sshpam_ctxt);
870 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
871 return (sshpam_authok == sshpam_ctxt);
876 mm_append_debug(Buffer *m)
878 if (auth_debug_init && buffer_len(&auth_debug)) {
879 debug3("%s: Appending debug messages for child", __func__);
880 buffer_append(m, buffer_ptr(&auth_debug),
881 buffer_len(&auth_debug));
882 buffer_clear(&auth_debug);
887 mm_answer_keyallowed(int socket, Buffer *m)
893 enum mm_keytype type = 0;
896 debug3("%s entering", __func__);
898 type = buffer_get_int(m);
899 cuser = buffer_get_string(m, NULL);
900 chost = buffer_get_string(m, NULL);
901 blob = buffer_get_string(m, &bloblen);
903 key = key_from_blob(blob, bloblen);
905 if ((compat20 && type == MM_RSAHOSTKEY) ||
906 (!compat20 && type != MM_RSAHOSTKEY))
907 fatal("%s: key type and protocol mismatch", __func__);
909 debug3("%s: key_from_blob: %p", __func__, key);
911 if (key != NULL && authctxt->pw != NULL) {
914 allowed = options.pubkey_authentication &&
915 user_key_allowed(authctxt->pw, key);
918 allowed = options.hostbased_authentication &&
919 hostbased_key_allowed(authctxt->pw,
923 key->type = KEY_RSA1; /* XXX */
924 allowed = options.rhosts_rsa_authentication &&
925 auth_rhosts_rsa_key_allowed(authctxt->pw,
929 fatal("%s: unknown key type %d", __func__, type);
936 /* clear temporarily storage (used by verify) */
937 monitor_reset_key_state();
940 /* Save temporarily for comparison in verify */
942 key_bloblen = bloblen;
944 hostbased_cuser = cuser;
945 hostbased_chost = chost;
948 debug3("%s: key %p is %s",
949 __func__, key, allowed ? "allowed" : "disallowed");
952 buffer_put_int(m, allowed);
953 buffer_put_int(m, forced_command != NULL);
957 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
959 if (type == MM_RSAHOSTKEY)
960 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
966 monitor_valid_userblob(u_char *data, u_int datalen)
974 buffer_append(&b, data, datalen);
976 if (datafellows & SSH_OLD_SESSIONID) {
978 len = buffer_len(&b);
979 if ((session_id2 == NULL) ||
980 (len < session_id2_len) ||
981 (memcmp(p, session_id2, session_id2_len) != 0))
983 buffer_consume(&b, session_id2_len);
985 p = buffer_get_string(&b, &len);
986 if ((session_id2 == NULL) ||
987 (len != session_id2_len) ||
988 (memcmp(p, session_id2, session_id2_len) != 0))
992 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
994 p = buffer_get_string(&b, NULL);
995 if (strcmp(authctxt->user, p) != 0) {
996 logit("wrong user name passed to monitor: expected %s != %.100s",
1001 buffer_skip_string(&b);
1002 if (datafellows & SSH_BUG_PKAUTH) {
1003 if (!buffer_get_char(&b))
1006 p = buffer_get_string(&b, NULL);
1007 if (strcmp("publickey", p) != 0)
1010 if (!buffer_get_char(&b))
1012 buffer_skip_string(&b);
1014 buffer_skip_string(&b);
1015 if (buffer_len(&b) != 0)
1022 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1031 buffer_append(&b, data, datalen);
1033 p = buffer_get_string(&b, &len);
1034 if ((session_id2 == NULL) ||
1035 (len != session_id2_len) ||
1036 (memcmp(p, session_id2, session_id2_len) != 0))
1040 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1042 p = buffer_get_string(&b, NULL);
1043 if (strcmp(authctxt->user, p) != 0) {
1044 logit("wrong user name passed to monitor: expected %s != %.100s",
1049 buffer_skip_string(&b); /* service */
1050 p = buffer_get_string(&b, NULL);
1051 if (strcmp(p, "hostbased") != 0)
1054 buffer_skip_string(&b); /* pkalg */
1055 buffer_skip_string(&b); /* pkblob */
1057 /* verify client host, strip trailing dot if necessary */
1058 p = buffer_get_string(&b, NULL);
1059 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1061 if (strcmp(p, chost) != 0)
1065 /* verify client user */
1066 p = buffer_get_string(&b, NULL);
1067 if (strcmp(p, cuser) != 0)
1071 if (buffer_len(&b) != 0)
1078 mm_answer_keyverify(int socket, Buffer *m)
1081 u_char *signature, *data, *blob;
1082 u_int signaturelen, datalen, bloblen;
1086 blob = buffer_get_string(m, &bloblen);
1087 signature = buffer_get_string(m, &signaturelen);
1088 data = buffer_get_string(m, &datalen);
1090 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1091 !monitor_allowed_key(blob, bloblen))
1092 fatal("%s: bad key, not previously allowed", __func__);
1094 key = key_from_blob(blob, bloblen);
1096 fatal("%s: bad public key blob", __func__);
1098 switch (key_blobtype) {
1100 valid_data = monitor_valid_userblob(data, datalen);
1103 valid_data = monitor_valid_hostbasedblob(data, datalen,
1104 hostbased_cuser, hostbased_chost);
1111 fatal("%s: bad signature data blob", __func__);
1113 verified = key_verify(key, signature, signaturelen, data, datalen);
1114 debug3("%s: key %p signature %s",
1115 __func__, key, verified ? "verified" : "unverified");
1122 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1124 monitor_reset_key_state();
1127 buffer_put_int(m, verified);
1128 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1134 mm_record_login(Session *s, struct passwd *pw)
1137 struct sockaddr_storage from;
1140 * Get IP address of client. If the connection is not a socket, let
1141 * the address be 0.0.0.0.
1143 memset(&from, 0, sizeof(from));
1144 fromlen = sizeof(from);
1145 if (packet_connection_is_on_socket()) {
1146 if (getpeername(packet_get_connection_in(),
1147 (struct sockaddr *) & from, &fromlen) < 0) {
1148 debug("getpeername: %.100s", strerror(errno));
1152 /* Record that there was a login on that tty from the remote host. */
1153 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1154 get_remote_name_or_ip(utmp_len, options.use_dns),
1155 (struct sockaddr *)&from, fromlen);
1159 mm_session_close(Session *s)
1161 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1162 if (s->ttyfd != -1) {
1163 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1164 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1165 session_pty_cleanup2(s);
1171 mm_answer_pty(int socket, Buffer *m)
1173 extern struct monitor *pmonitor;
1177 debug3("%s entering", __func__);
1183 s->authctxt = authctxt;
1184 s->pw = authctxt->pw;
1185 s->pid = pmonitor->m_pid;
1186 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1189 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1190 pty_setowner(authctxt->pw, s->tty);
1192 buffer_put_int(m, 1);
1193 buffer_put_cstring(m, s->tty);
1194 mm_request_send(socket, MONITOR_ANS_PTY, m);
1196 mm_send_fd(socket, s->ptyfd);
1197 mm_send_fd(socket, s->ttyfd);
1199 /* We need to trick ttyslot */
1200 if (dup2(s->ttyfd, 0) == -1)
1201 fatal("%s: dup2", __func__);
1203 mm_record_login(s, authctxt->pw);
1205 /* Now we can close the file descriptor again */
1208 /* make sure nothing uses fd 0 */
1209 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1210 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1212 error("%s: fd0 %d != 0", __func__, fd0);
1214 /* slave is not needed */
1216 s->ttyfd = s->ptyfd;
1217 /* no need to dup() because nobody closes ptyfd */
1218 s->ptymaster = s->ptyfd;
1220 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1226 mm_session_close(s);
1227 buffer_put_int(m, 0);
1228 mm_request_send(socket, MONITOR_ANS_PTY, m);
1233 mm_answer_pty_cleanup(int socket, Buffer *m)
1238 debug3("%s entering", __func__);
1240 tty = buffer_get_string(m, NULL);
1241 if ((s = session_by_tty(tty)) != NULL)
1242 mm_session_close(s);
1249 mm_answer_sesskey(int socket, Buffer *m)
1254 /* Turn off permissions */
1255 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1257 if ((p = BN_new()) == NULL)
1258 fatal("%s: BN_new", __func__);
1260 buffer_get_bignum2(m, p);
1262 rsafail = ssh1_session_key(p);
1265 buffer_put_int(m, rsafail);
1266 buffer_put_bignum2(m, p);
1270 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1272 /* Turn on permissions for sessid passing */
1273 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1279 mm_answer_sessid(int socket, Buffer *m)
1283 debug3("%s entering", __func__);
1285 if (buffer_len(m) != 16)
1286 fatal("%s: bad ssh1 session id", __func__);
1287 for (i = 0; i < 16; i++)
1288 session_id[i] = buffer_get_char(m);
1290 /* Turn on permissions for getpwnam */
1291 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1297 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1301 u_char *blob = NULL;
1305 debug3("%s entering", __func__);
1307 if (options.rsa_authentication && authctxt->valid) {
1308 if ((client_n = BN_new()) == NULL)
1309 fatal("%s: BN_new", __func__);
1310 buffer_get_bignum2(m, client_n);
1311 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1312 BN_clear_free(client_n);
1315 buffer_put_int(m, allowed);
1316 buffer_put_int(m, forced_command != NULL);
1318 /* clear temporarily storage (used by generate challenge) */
1319 monitor_reset_key_state();
1321 if (allowed && key != NULL) {
1322 key->type = KEY_RSA; /* cheat for key_to_blob */
1323 if (key_to_blob(key, &blob, &blen) == 0)
1324 fatal("%s: key_to_blob failed", __func__);
1325 buffer_put_string(m, blob, blen);
1327 /* Save temporarily for comparison in verify */
1330 key_blobtype = MM_RSAUSERKEY;
1337 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1339 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1340 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1345 mm_answer_rsa_challenge(int socket, Buffer *m)
1351 debug3("%s entering", __func__);
1353 if (!authctxt->valid)
1354 fatal("%s: authctxt not valid", __func__);
1355 blob = buffer_get_string(m, &blen);
1356 if (!monitor_allowed_key(blob, blen))
1357 fatal("%s: bad key, not previously allowed", __func__);
1358 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1359 fatal("%s: key type mismatch", __func__);
1360 if ((key = key_from_blob(blob, blen)) == NULL)
1361 fatal("%s: received bad key", __func__);
1364 BN_clear_free(ssh1_challenge);
1365 ssh1_challenge = auth_rsa_generate_challenge(key);
1368 buffer_put_bignum2(m, ssh1_challenge);
1370 debug3("%s sending reply", __func__);
1371 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1373 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1381 mm_answer_rsa_response(int socket, Buffer *m)
1384 u_char *blob, *response;
1388 debug3("%s entering", __func__);
1390 if (!authctxt->valid)
1391 fatal("%s: authctxt not valid", __func__);
1392 if (ssh1_challenge == NULL)
1393 fatal("%s: no ssh1_challenge", __func__);
1395 blob = buffer_get_string(m, &blen);
1396 if (!monitor_allowed_key(blob, blen))
1397 fatal("%s: bad key, not previously allowed", __func__);
1398 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1399 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1400 if ((key = key_from_blob(blob, blen)) == NULL)
1401 fatal("%s: received bad key", __func__);
1402 response = buffer_get_string(m, &len);
1404 fatal("%s: received bad response to challenge", __func__);
1405 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1411 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1414 BN_clear_free(ssh1_challenge);
1415 ssh1_challenge = NULL;
1416 monitor_reset_key_state();
1419 buffer_put_int(m, success);
1420 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1427 mm_answer_krb5(int socket, Buffer *m)
1429 krb5_data tkt, reply;
1434 /* use temporary var to avoid size issues on 64bit arch */
1435 tkt.data = buffer_get_string(m, &len);
1438 success = options.kerberos_authentication &&
1440 auth_krb5(authctxt, &tkt, &client_user, &reply);
1446 buffer_put_int(m, success);
1449 buffer_put_cstring(m, client_user);
1450 buffer_put_string(m, reply.data, reply.length);
1456 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1458 auth_method = "kerberos";
1465 mm_answer_term(int socket, Buffer *req)
1467 extern struct monitor *pmonitor;
1470 debug3("%s: tearing down sessions", __func__);
1472 /* The child is terminating */
1473 session_destroy_all(&mm_session_close);
1475 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1479 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1481 /* Terminate process */
1486 monitor_apply_keystate(struct monitor *pmonitor)
1489 set_newkeys(MODE_IN);
1490 set_newkeys(MODE_OUT);
1492 packet_set_protocol_flags(child_state.ssh1protoflags);
1493 packet_set_encryption_key(child_state.ssh1key,
1494 child_state.ssh1keylen, child_state.ssh1cipher);
1495 xfree(child_state.ssh1key);
1498 /* for rc4 and other stateful ciphers */
1499 packet_set_keycontext(MODE_OUT, child_state.keyout);
1500 xfree(child_state.keyout);
1501 packet_set_keycontext(MODE_IN, child_state.keyin);
1502 xfree(child_state.keyin);
1505 packet_set_iv(MODE_OUT, child_state.ivout);
1506 xfree(child_state.ivout);
1507 packet_set_iv(MODE_IN, child_state.ivin);
1508 xfree(child_state.ivin);
1511 memcpy(&incoming_stream, &child_state.incoming,
1512 sizeof(incoming_stream));
1513 memcpy(&outgoing_stream, &child_state.outgoing,
1514 sizeof(outgoing_stream));
1516 /* Update with new address */
1517 if (options.compression)
1518 mm_init_compression(pmonitor->m_zlib);
1520 /* Network I/O buffers */
1521 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1522 buffer_clear(&input);
1523 buffer_append(&input, child_state.input, child_state.ilen);
1524 memset(child_state.input, 0, child_state.ilen);
1525 xfree(child_state.input);
1527 buffer_clear(&output);
1528 buffer_append(&output, child_state.output, child_state.olen);
1529 memset(child_state.output, 0, child_state.olen);
1530 xfree(child_state.output);
1534 mm_get_kex(Buffer *m)
1540 kex = xmalloc(sizeof(*kex));
1541 memset(kex, 0, sizeof(*kex));
1542 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1543 if ((session_id2 == NULL) ||
1544 (kex->session_id_len != session_id2_len) ||
1545 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1546 fatal("mm_get_get: internal error: bad session id");
1547 kex->we_need = buffer_get_int(m);
1548 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1549 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1551 kex->hostkey_type = buffer_get_int(m);
1552 kex->kex_type = buffer_get_int(m);
1553 blob = buffer_get_string(m, &bloblen);
1554 buffer_init(&kex->my);
1555 buffer_append(&kex->my, blob, bloblen);
1557 blob = buffer_get_string(m, &bloblen);
1558 buffer_init(&kex->peer);
1559 buffer_append(&kex->peer, blob, bloblen);
1562 kex->flags = buffer_get_int(m);
1563 kex->client_version_string = buffer_get_string(m, NULL);
1564 kex->server_version_string = buffer_get_string(m, NULL);
1565 kex->load_host_key=&get_hostkey_by_type;
1566 kex->host_key_index=&get_hostkey_index;
1571 /* This function requries careful sanity checking */
1574 mm_get_keystate(struct monitor *pmonitor)
1578 u_int bloblen, plen;
1579 u_int32_t seqnr, packets;
1582 debug3("%s: Waiting for new keys", __func__);
1585 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1587 child_state.ssh1protoflags = buffer_get_int(&m);
1588 child_state.ssh1cipher = buffer_get_int(&m);
1589 child_state.ssh1key = buffer_get_string(&m,
1590 &child_state.ssh1keylen);
1591 child_state.ivout = buffer_get_string(&m,
1592 &child_state.ivoutlen);
1593 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1596 /* Get the Kex for rekeying */
1597 *pmonitor->m_pkex = mm_get_kex(&m);
1600 blob = buffer_get_string(&m, &bloblen);
1601 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1604 debug3("%s: Waiting for second key", __func__);
1605 blob = buffer_get_string(&m, &bloblen);
1606 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1609 /* Now get sequence numbers for the packets */
1610 seqnr = buffer_get_int(&m);
1611 blocks = buffer_get_int64(&m);
1612 packets = buffer_get_int(&m);
1613 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1614 seqnr = buffer_get_int(&m);
1615 blocks = buffer_get_int64(&m);
1616 packets = buffer_get_int(&m);
1617 packet_set_state(MODE_IN, seqnr, blocks, packets);
1620 /* Get the key context */
1621 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1622 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1624 debug3("%s: Getting compression state", __func__);
1625 /* Get compression state */
1626 p = buffer_get_string(&m, &plen);
1627 if (plen != sizeof(child_state.outgoing))
1628 fatal("%s: bad request size", __func__);
1629 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1632 p = buffer_get_string(&m, &plen);
1633 if (plen != sizeof(child_state.incoming))
1634 fatal("%s: bad request size", __func__);
1635 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1638 /* Network I/O buffers */
1639 debug3("%s: Getting Network I/O buffers", __func__);
1640 child_state.input = buffer_get_string(&m, &child_state.ilen);
1641 child_state.output = buffer_get_string(&m, &child_state.olen);
1647 /* Allocation functions for zlib */
1649 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1651 size_t len = (size_t) size * ncount;
1654 if (len == 0 || ncount > SIZE_T_MAX / size)
1655 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1657 address = mm_malloc(mm, len);
1663 mm_zfree(struct mm_master *mm, void *address)
1665 mm_free(mm, address);
1669 mm_init_compression(struct mm_master *mm)
1671 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1672 outgoing_stream.zfree = (free_func)mm_zfree;
1673 outgoing_stream.opaque = mm;
1675 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1676 incoming_stream.zfree = (free_func)mm_zfree;
1677 incoming_stream.opaque = mm;
1682 #define FD_CLOSEONEXEC(x) do { \
1683 if (fcntl(x, F_SETFD, 1) == -1) \
1684 fatal("fcntl(%d, F_SETFD)", x); \
1688 monitor_socketpair(int *pair)
1690 #ifdef HAVE_SOCKETPAIR
1691 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1692 fatal("%s: socketpair", __func__);
1694 fatal("%s: UsePrivilegeSeparation=yes not supported",
1697 FD_CLOSEONEXEC(pair[0]);
1698 FD_CLOSEONEXEC(pair[1]);
1701 #define MM_MEMSIZE 65536
1706 struct monitor *mon;
1709 mon = xmalloc(sizeof(*mon));
1711 monitor_socketpair(pair);
1713 mon->m_recvfd = pair[0];
1714 mon->m_sendfd = pair[1];
1716 /* Used to share zlib space across processes */
1717 if (options.compression) {
1718 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1719 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1721 /* Compression needs to share state across borders */
1722 mm_init_compression(mon->m_zlib);
1729 monitor_reinit(struct monitor *mon)
1733 monitor_socketpair(pair);
1735 mon->m_recvfd = pair[0];
1736 mon->m_sendfd = pair[1];