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.37 2003/04/02 09:48:07 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 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;
148 enum monitor_reqtype type;
150 int (*f)(int, Buffer *);
153 #define MON_ISAUTH 0x0004 /* Required for Authentication */
154 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
155 #define MON_ONCE 0x0010 /* Disable after calling */
157 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
159 #define MON_PERMIT 0x1000 /* Request is permitted */
161 struct mon_table mon_dispatch_proto20[] = {
162 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
163 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
164 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
165 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
166 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
167 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
169 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
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},
188 struct mon_table mon_dispatch_postauth20[] = {
189 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
190 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
191 {MONITOR_REQ_PTY, 0, mm_answer_pty},
192 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
193 {MONITOR_REQ_TERM, 0, mm_answer_term},
197 struct mon_table mon_dispatch_proto15[] = {
198 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
199 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
200 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
201 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
202 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
203 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
204 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
205 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
207 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
208 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
211 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
212 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
215 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
216 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
217 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
218 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
219 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
222 {MONITOR_REQ_KRB4, MON_ONCE|MON_AUTH, mm_answer_krb4},
225 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
230 struct mon_table mon_dispatch_postauth15[] = {
231 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
232 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
233 {MONITOR_REQ_TERM, 0, mm_answer_term},
237 struct mon_table *mon_dispatch;
239 /* Specifies if a certain message is allowed at the moment */
242 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
244 while (ent->f != NULL) {
245 if (ent->type == type) {
246 ent->flags &= ~MON_PERMIT;
247 ent->flags |= permit ? MON_PERMIT : 0;
255 monitor_permit_authentications(int permit)
257 struct mon_table *ent = mon_dispatch;
259 while (ent->f != NULL) {
260 if (ent->flags & MON_AUTH) {
261 ent->flags &= ~MON_PERMIT;
262 ent->flags |= permit ? MON_PERMIT : 0;
269 monitor_child_preauth(struct monitor *pmonitor)
271 struct mon_table *ent;
272 int authenticated = 0;
274 debug3("preauth child monitor started");
277 mon_dispatch = mon_dispatch_proto20;
279 /* Permit requests for moduli and signatures */
280 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
281 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
283 mon_dispatch = mon_dispatch_proto15;
285 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
288 authctxt = authctxt_new();
290 /* The first few requests do not require asynchronous access */
291 while (!authenticated) {
292 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
294 if (!(ent->flags & MON_AUTHDECIDE))
295 fatal("%s: unexpected authentication from %d",
296 __func__, ent->type);
297 if (authctxt->pw->pw_uid == 0 &&
298 !auth_root_allowed(auth_method))
302 if (ent->flags & MON_AUTHDECIDE) {
303 auth_log(authctxt, authenticated, auth_method,
304 compat20 ? " ssh2" : "");
306 authctxt->failures++;
310 if (!authctxt->valid)
311 fatal("%s: authenticated invalid user", __func__);
313 debug("%s: %s has been authenticated by privileged process",
314 __func__, authctxt->user);
316 mm_get_keystate(pmonitor);
322 monitor_child_postauth(struct monitor *pmonitor)
325 mon_dispatch = mon_dispatch_postauth20;
327 /* Permit requests for moduli and signatures */
328 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
329 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
330 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
333 mon_dispatch = mon_dispatch_postauth15;
334 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
337 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
338 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
342 monitor_read(pmonitor, mon_dispatch, NULL);
346 monitor_sync(struct monitor *pmonitor)
348 if (options.compression) {
349 /* The member allocation is not visible, so sync it */
350 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
355 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
356 struct mon_table **pent)
364 mm_request_receive(pmonitor->m_sendfd, &m);
365 type = buffer_get_char(&m);
367 debug3("%s: checking request %d", __func__, type);
369 while (ent->f != NULL) {
370 if (ent->type == type)
375 if (ent->f != NULL) {
376 if (!(ent->flags & MON_PERMIT))
377 fatal("%s: unpermitted request %d", __func__,
379 ret = (*ent->f)(pmonitor->m_sendfd, &m);
382 /* The child may use this request only once, disable it */
383 if (ent->flags & MON_ONCE) {
384 debug2("%s: %d used once, disabling now", __func__,
386 ent->flags &= ~MON_PERMIT;
395 fatal("%s: unsupported request: %d", __func__, type);
401 /* allowed key state */
403 monitor_allowed_key(u_char *blob, u_int bloblen)
405 /* make sure key is allowed */
406 if (key_blob == NULL || key_bloblen != bloblen ||
407 memcmp(key_blob, blob, key_bloblen))
413 monitor_reset_key_state(void)
416 if (key_blob != NULL)
418 if (hostbased_cuser != NULL)
419 xfree(hostbased_cuser);
420 if (hostbased_chost != NULL)
421 xfree(hostbased_chost);
424 key_blobtype = MM_NOKEY;
425 hostbased_cuser = NULL;
426 hostbased_chost = NULL;
430 mm_answer_moduli(int socket, Buffer *m)
435 min = buffer_get_int(m);
436 want = buffer_get_int(m);
437 max = buffer_get_int(m);
439 debug3("%s: got parameters: %d %d %d",
440 __func__, min, want, max);
441 /* We need to check here, too, in case the child got corrupted */
442 if (max < min || want < min || max < want)
443 fatal("%s: bad parameters: %d %d %d",
444 __func__, min, want, max);
448 dh = choose_dh(min, want, max);
450 buffer_put_char(m, 0);
453 /* Send first bignum */
454 buffer_put_char(m, 1);
455 buffer_put_bignum2(m, dh->p);
456 buffer_put_bignum2(m, dh->g);
460 mm_request_send(socket, MONITOR_ANS_MODULI, m);
465 mm_answer_sign(int socket, Buffer *m)
470 u_int siglen, datlen;
473 debug3("%s", __func__);
475 keyid = buffer_get_int(m);
476 p = buffer_get_string(m, &datlen);
479 fatal("%s: data length incorrect: %u", __func__, datlen);
481 /* save session id, it will be passed on the first call */
482 if (session_id2_len == 0) {
483 session_id2_len = datlen;
484 session_id2 = xmalloc(session_id2_len);
485 memcpy(session_id2, p, session_id2_len);
488 if ((key = get_hostkey_by_index(keyid)) == NULL)
489 fatal("%s: no hostkey from index %d", __func__, keyid);
490 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
491 fatal("%s: key_sign failed", __func__);
493 debug3("%s: signature %p(%u)", __func__, signature, siglen);
496 buffer_put_string(m, signature, siglen);
501 mm_request_send(socket, MONITOR_ANS_SIGN, m);
503 /* Turn on permissions for getpwnam */
504 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
509 /* Retrieves the password entry and also checks if the user is permitted */
512 mm_answer_pwnamallow(int socket, Buffer *m)
515 struct passwd *pwent;
518 debug3("%s", __func__);
520 if (authctxt->attempt++ != 0)
521 fatal("%s: multiple attempts for getpwnam", __func__);
523 login = buffer_get_string(m, NULL);
525 pwent = getpwnamallow(login);
527 authctxt->user = xstrdup(login);
528 setproctitle("%s [priv]", pwent ? login : "unknown");
534 buffer_put_char(m, 0);
539 authctxt->pw = pwent;
542 buffer_put_char(m, 1);
543 buffer_put_string(m, pwent, sizeof(struct passwd));
544 buffer_put_cstring(m, pwent->pw_name);
545 buffer_put_cstring(m, "*");
546 buffer_put_cstring(m, pwent->pw_gecos);
547 #ifdef HAVE_PW_CLASS_IN_PASSWD
548 buffer_put_cstring(m, pwent->pw_class);
550 buffer_put_cstring(m, pwent->pw_dir);
551 buffer_put_cstring(m, pwent->pw_shell);
554 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
555 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
557 /* For SSHv1 allow authentication now */
559 monitor_permit_authentications(1);
561 /* Allow service/style information on the auth context */
562 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
563 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
567 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
573 int mm_answer_auth2_read_banner(int socket, Buffer *m)
578 banner = auth2_read_banner();
579 buffer_put_cstring(m, banner != NULL ? banner : "");
580 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
589 mm_answer_authserv(int socket, Buffer *m)
591 monitor_permit_authentications(1);
593 authctxt->service = buffer_get_string(m, NULL);
594 authctxt->style = buffer_get_string(m, NULL);
595 debug3("%s: service=%s, style=%s",
596 __func__, authctxt->service, authctxt->style);
598 if (strlen(authctxt->style) == 0) {
599 xfree(authctxt->style);
600 authctxt->style = NULL;
607 mm_answer_authpassword(int socket, Buffer *m)
609 static int call_count;
614 passwd = buffer_get_string(m, &plen);
615 /* Only authenticate if the context is valid */
616 authenticated = options.password_authentication &&
617 auth_password(authctxt, passwd) && authctxt->valid;
618 memset(passwd, 0, strlen(passwd));
622 buffer_put_int(m, authenticated);
624 debug3("%s: sending result %d", __func__, authenticated);
625 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
628 if (plen == 0 && call_count == 1)
629 auth_method = "none";
631 auth_method = "password";
633 /* Causes monitor loop to terminate if authenticated */
634 return (authenticated);
639 mm_answer_bsdauthquery(int socket, Buffer *m)
641 char *name, *infotxt;
647 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
648 &prompts, &echo_on) < 0 ? 0 : 1;
651 buffer_put_int(m, success);
653 buffer_put_cstring(m, prompts[0]);
655 debug3("%s: sending challenge success: %u", __func__, success);
656 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
669 mm_answer_bsdauthrespond(int socket, Buffer *m)
674 if (authctxt->as == 0)
675 fatal("%s: no bsd auth session", __func__);
677 response = buffer_get_string(m, NULL);
678 authok = options.challenge_response_authentication &&
679 auth_userresponse(authctxt->as, response, 0);
681 debug3("%s: <%s> = <%d>", __func__, response, authok);
685 buffer_put_int(m, authok);
687 debug3("%s: sending authenticated: %d", __func__, authok);
688 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
690 auth_method = "bsdauth";
692 return (authok != 0);
698 mm_answer_skeyquery(int socket, Buffer *m)
701 char challenge[1024];
704 success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
707 buffer_put_int(m, success);
709 buffer_put_cstring(m, challenge);
711 debug3("%s: sending challenge success: %u", __func__, success);
712 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
718 mm_answer_skeyrespond(int socket, Buffer *m)
723 response = buffer_get_string(m, NULL);
725 authok = (options.challenge_response_authentication &&
727 skey_haskey(authctxt->pw->pw_name) == 0 &&
728 skey_passcheck(authctxt->pw->pw_name, response) != -1);
733 buffer_put_int(m, authok);
735 debug3("%s: sending authenticated: %d", __func__, authok);
736 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
738 auth_method = "skey";
740 return (authok != 0);
746 mm_answer_pam_start(int socket, Buffer *m)
750 user = buffer_get_string(m, NULL);
759 static void *sshpam_ctxt, *sshpam_authok;
760 extern KbdintDevice sshpam_device;
763 mm_answer_pam_init_ctx(int socket, Buffer *m)
766 debug3("%s", __func__);
767 authctxt->user = buffer_get_string(m, NULL);
768 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
769 sshpam_authok = NULL;
771 if (sshpam_ctxt != NULL) {
772 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
773 buffer_put_int(m, 1);
775 buffer_put_int(m, 0);
777 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
782 mm_answer_pam_query(int socket, Buffer *m)
784 char *name, *info, **prompts;
788 debug3("%s", __func__);
789 sshpam_authok = NULL;
790 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
791 if (ret == 0 && num == 0)
792 sshpam_authok = sshpam_ctxt;
793 if (num > 1 || name == NULL || info == NULL)
796 buffer_put_int(m, ret);
797 buffer_put_cstring(m, name);
799 buffer_put_cstring(m, info);
801 buffer_put_int(m, num);
802 for (i = 0; i < num; ++i) {
803 buffer_put_cstring(m, prompts[i]);
805 buffer_put_int(m, echo_on[i]);
811 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
816 mm_answer_pam_respond(int socket, Buffer *m)
822 debug3("%s", __func__);
823 sshpam_authok = NULL;
824 num = buffer_get_int(m);
826 resp = xmalloc(num * sizeof(char *));
827 for (i = 0; i < num; ++i)
828 resp[i] = buffer_get_string(m, NULL);
829 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
830 for (i = 0; i < num; ++i)
834 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
837 buffer_put_int(m, ret);
838 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
839 auth_method = "keyboard-interactive/pam";
841 sshpam_authok = sshpam_ctxt;
846 mm_answer_pam_free_ctx(int socket, Buffer *m)
849 debug3("%s", __func__);
850 (sshpam_device.free_ctx)(sshpam_ctxt);
852 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
853 return (sshpam_authok == sshpam_ctxt);
858 mm_append_debug(Buffer *m)
860 if (auth_debug_init && buffer_len(&auth_debug)) {
861 debug3("%s: Appending debug messages for child", __func__);
862 buffer_append(m, buffer_ptr(&auth_debug),
863 buffer_len(&auth_debug));
864 buffer_clear(&auth_debug);
869 mm_answer_keyallowed(int socket, Buffer *m)
875 enum mm_keytype type = 0;
878 debug3("%s entering", __func__);
880 type = buffer_get_int(m);
881 cuser = buffer_get_string(m, NULL);
882 chost = buffer_get_string(m, NULL);
883 blob = buffer_get_string(m, &bloblen);
885 key = key_from_blob(blob, bloblen);
887 if ((compat20 && type == MM_RSAHOSTKEY) ||
888 (!compat20 && type != MM_RSAHOSTKEY))
889 fatal("%s: key type and protocol mismatch", __func__);
891 debug3("%s: key_from_blob: %p", __func__, key);
893 if (key != NULL && authctxt->pw != NULL) {
896 allowed = options.pubkey_authentication &&
897 user_key_allowed(authctxt->pw, key);
900 allowed = options.hostbased_authentication &&
901 hostbased_key_allowed(authctxt->pw,
905 key->type = KEY_RSA1; /* XXX */
906 allowed = options.rhosts_rsa_authentication &&
907 auth_rhosts_rsa_key_allowed(authctxt->pw,
911 fatal("%s: unknown key type %d", __func__, type);
918 /* clear temporarily storage (used by verify) */
919 monitor_reset_key_state();
922 /* Save temporarily for comparison in verify */
924 key_bloblen = bloblen;
926 hostbased_cuser = cuser;
927 hostbased_chost = chost;
930 debug3("%s: key %p is %s",
931 __func__, key, allowed ? "allowed" : "disallowed");
934 buffer_put_int(m, allowed);
935 buffer_put_int(m, forced_command != NULL);
939 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
941 if (type == MM_RSAHOSTKEY)
942 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
948 monitor_valid_userblob(u_char *data, u_int datalen)
956 buffer_append(&b, data, datalen);
958 if (datafellows & SSH_OLD_SESSIONID) {
960 len = buffer_len(&b);
961 if ((session_id2 == NULL) ||
962 (len < session_id2_len) ||
963 (memcmp(p, session_id2, session_id2_len) != 0))
965 buffer_consume(&b, session_id2_len);
967 p = buffer_get_string(&b, &len);
968 if ((session_id2 == NULL) ||
969 (len != session_id2_len) ||
970 (memcmp(p, session_id2, session_id2_len) != 0))
974 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
976 p = buffer_get_string(&b, NULL);
977 if (strcmp(authctxt->user, p) != 0) {
978 logit("wrong user name passed to monitor: expected %s != %.100s",
983 buffer_skip_string(&b);
984 if (datafellows & SSH_BUG_PKAUTH) {
985 if (!buffer_get_char(&b))
988 p = buffer_get_string(&b, NULL);
989 if (strcmp("publickey", p) != 0)
992 if (!buffer_get_char(&b))
994 buffer_skip_string(&b);
996 buffer_skip_string(&b);
997 if (buffer_len(&b) != 0)
1004 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1013 buffer_append(&b, data, datalen);
1015 p = buffer_get_string(&b, &len);
1016 if ((session_id2 == NULL) ||
1017 (len != session_id2_len) ||
1018 (memcmp(p, session_id2, session_id2_len) != 0))
1022 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1024 p = buffer_get_string(&b, NULL);
1025 if (strcmp(authctxt->user, p) != 0) {
1026 logit("wrong user name passed to monitor: expected %s != %.100s",
1031 buffer_skip_string(&b); /* service */
1032 p = buffer_get_string(&b, NULL);
1033 if (strcmp(p, "hostbased") != 0)
1036 buffer_skip_string(&b); /* pkalg */
1037 buffer_skip_string(&b); /* pkblob */
1039 /* verify client host, strip trailing dot if necessary */
1040 p = buffer_get_string(&b, NULL);
1041 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1043 if (strcmp(p, chost) != 0)
1047 /* verify client user */
1048 p = buffer_get_string(&b, NULL);
1049 if (strcmp(p, cuser) != 0)
1053 if (buffer_len(&b) != 0)
1060 mm_answer_keyverify(int socket, Buffer *m)
1063 u_char *signature, *data, *blob;
1064 u_int signaturelen, datalen, bloblen;
1068 blob = buffer_get_string(m, &bloblen);
1069 signature = buffer_get_string(m, &signaturelen);
1070 data = buffer_get_string(m, &datalen);
1072 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1073 !monitor_allowed_key(blob, bloblen))
1074 fatal("%s: bad key, not previously allowed", __func__);
1076 key = key_from_blob(blob, bloblen);
1078 fatal("%s: bad public key blob", __func__);
1080 switch (key_blobtype) {
1082 valid_data = monitor_valid_userblob(data, datalen);
1085 valid_data = monitor_valid_hostbasedblob(data, datalen,
1086 hostbased_cuser, hostbased_chost);
1093 fatal("%s: bad signature data blob", __func__);
1095 verified = key_verify(key, signature, signaturelen, data, datalen);
1096 debug3("%s: key %p signature %s",
1097 __func__, key, verified ? "verified" : "unverified");
1104 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1106 monitor_reset_key_state();
1109 buffer_put_int(m, verified);
1110 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1116 mm_record_login(Session *s, struct passwd *pw)
1119 struct sockaddr_storage from;
1122 * Get IP address of client. If the connection is not a socket, let
1123 * the address be 0.0.0.0.
1125 memset(&from, 0, sizeof(from));
1126 fromlen = sizeof(from);
1127 if (packet_connection_is_on_socket()) {
1128 if (getpeername(packet_get_connection_in(),
1129 (struct sockaddr *) & from, &fromlen) < 0) {
1130 debug("getpeername: %.100s", strerror(errno));
1134 /* Record that there was a login on that tty from the remote host. */
1135 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1136 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
1137 (struct sockaddr *)&from, fromlen);
1141 mm_session_close(Session *s)
1143 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
1144 if (s->ttyfd != -1) {
1145 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1146 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1147 session_pty_cleanup2(s);
1153 mm_answer_pty(int socket, Buffer *m)
1155 extern struct monitor *pmonitor;
1159 debug3("%s entering", __func__);
1165 s->authctxt = authctxt;
1166 s->pw = authctxt->pw;
1167 s->pid = pmonitor->m_pid;
1168 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1171 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1172 pty_setowner(authctxt->pw, s->tty);
1174 buffer_put_int(m, 1);
1175 buffer_put_cstring(m, s->tty);
1176 mm_request_send(socket, MONITOR_ANS_PTY, m);
1178 mm_send_fd(socket, s->ptyfd);
1179 mm_send_fd(socket, s->ttyfd);
1181 /* We need to trick ttyslot */
1182 if (dup2(s->ttyfd, 0) == -1)
1183 fatal("%s: dup2", __func__);
1185 mm_record_login(s, authctxt->pw);
1187 /* Now we can close the file descriptor again */
1190 /* make sure nothing uses fd 0 */
1191 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1192 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1194 error("%s: fd0 %d != 0", __func__, fd0);
1196 /* slave is not needed */
1198 s->ttyfd = s->ptyfd;
1199 /* no need to dup() because nobody closes ptyfd */
1200 s->ptymaster = s->ptyfd;
1202 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1208 mm_session_close(s);
1209 buffer_put_int(m, 0);
1210 mm_request_send(socket, MONITOR_ANS_PTY, m);
1215 mm_answer_pty_cleanup(int socket, Buffer *m)
1220 debug3("%s entering", __func__);
1222 tty = buffer_get_string(m, NULL);
1223 if ((s = session_by_tty(tty)) != NULL)
1224 mm_session_close(s);
1231 mm_answer_sesskey(int socket, Buffer *m)
1236 /* Turn off permissions */
1237 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1239 if ((p = BN_new()) == NULL)
1240 fatal("%s: BN_new", __func__);
1242 buffer_get_bignum2(m, p);
1244 rsafail = ssh1_session_key(p);
1247 buffer_put_int(m, rsafail);
1248 buffer_put_bignum2(m, p);
1252 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1254 /* Turn on permissions for sessid passing */
1255 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1261 mm_answer_sessid(int socket, Buffer *m)
1265 debug3("%s entering", __func__);
1267 if (buffer_len(m) != 16)
1268 fatal("%s: bad ssh1 session id", __func__);
1269 for (i = 0; i < 16; i++)
1270 session_id[i] = buffer_get_char(m);
1272 /* Turn on permissions for getpwnam */
1273 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1279 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1283 u_char *blob = NULL;
1287 debug3("%s entering", __func__);
1289 if (options.rsa_authentication && authctxt->valid) {
1290 if ((client_n = BN_new()) == NULL)
1291 fatal("%s: BN_new", __func__);
1292 buffer_get_bignum2(m, client_n);
1293 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1294 BN_clear_free(client_n);
1297 buffer_put_int(m, allowed);
1298 buffer_put_int(m, forced_command != NULL);
1300 /* clear temporarily storage (used by generate challenge) */
1301 monitor_reset_key_state();
1303 if (allowed && key != NULL) {
1304 key->type = KEY_RSA; /* cheat for key_to_blob */
1305 if (key_to_blob(key, &blob, &blen) == 0)
1306 fatal("%s: key_to_blob failed", __func__);
1307 buffer_put_string(m, blob, blen);
1309 /* Save temporarily for comparison in verify */
1312 key_blobtype = MM_RSAUSERKEY;
1319 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1321 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1322 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1327 mm_answer_rsa_challenge(int socket, Buffer *m)
1333 debug3("%s entering", __func__);
1335 if (!authctxt->valid)
1336 fatal("%s: authctxt not valid", __func__);
1337 blob = buffer_get_string(m, &blen);
1338 if (!monitor_allowed_key(blob, blen))
1339 fatal("%s: bad key, not previously allowed", __func__);
1340 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1341 fatal("%s: key type mismatch", __func__);
1342 if ((key = key_from_blob(blob, blen)) == NULL)
1343 fatal("%s: received bad key", __func__);
1346 BN_clear_free(ssh1_challenge);
1347 ssh1_challenge = auth_rsa_generate_challenge(key);
1350 buffer_put_bignum2(m, ssh1_challenge);
1352 debug3("%s sending reply", __func__);
1353 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1355 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1363 mm_answer_rsa_response(int socket, Buffer *m)
1366 u_char *blob, *response;
1370 debug3("%s entering", __func__);
1372 if (!authctxt->valid)
1373 fatal("%s: authctxt not valid", __func__);
1374 if (ssh1_challenge == NULL)
1375 fatal("%s: no ssh1_challenge", __func__);
1377 blob = buffer_get_string(m, &blen);
1378 if (!monitor_allowed_key(blob, blen))
1379 fatal("%s: bad key, not previously allowed", __func__);
1380 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1381 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1382 if ((key = key_from_blob(blob, blen)) == NULL)
1383 fatal("%s: received bad key", __func__);
1384 response = buffer_get_string(m, &len);
1386 fatal("%s: received bad response to challenge", __func__);
1387 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1393 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1396 BN_clear_free(ssh1_challenge);
1397 ssh1_challenge = NULL;
1398 monitor_reset_key_state();
1401 buffer_put_int(m, success);
1402 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1409 mm_answer_krb4(int socket, Buffer *m)
1411 KTEXT_ST auth, reply;
1416 reply.length = auth.length = 0;
1418 p = buffer_get_string(m, &alen);
1419 if (alen >= MAX_KTXT_LEN)
1420 fatal("%s: auth too large", __func__);
1421 memcpy(auth.dat, p, alen);
1426 success = options.kerberos_authentication &&
1428 auth_krb4(authctxt, &auth, &client, &reply);
1430 memset(auth.dat, 0, alen);
1432 buffer_put_int(m, success);
1435 buffer_put_cstring(m, client);
1436 buffer_put_string(m, reply.dat, reply.length);
1440 memset(reply.dat, 0, reply.length);
1443 debug3("%s: sending result %d", __func__, success);
1444 mm_request_send(socket, MONITOR_ANS_KRB4, m);
1446 auth_method = "kerberos";
1448 /* Causes monitor loop to terminate if authenticated */
1455 mm_answer_krb5(int socket, Buffer *m)
1457 krb5_data tkt, reply;
1462 /* use temporary var to avoid size issues on 64bit arch */
1463 tkt.data = buffer_get_string(m, &len);
1466 success = options.kerberos_authentication &&
1468 auth_krb5(authctxt, &tkt, &client_user, &reply);
1474 buffer_put_int(m, success);
1477 buffer_put_cstring(m, client_user);
1478 buffer_put_string(m, reply.data, reply.length);
1484 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1491 mm_answer_term(int socket, Buffer *req)
1493 extern struct monitor *pmonitor;
1496 debug3("%s: tearing down sessions", __func__);
1498 /* The child is terminating */
1499 session_destroy_all(&mm_session_close);
1501 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1505 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1507 /* Terminate process */
1512 monitor_apply_keystate(struct monitor *pmonitor)
1515 set_newkeys(MODE_IN);
1516 set_newkeys(MODE_OUT);
1518 packet_set_protocol_flags(child_state.ssh1protoflags);
1519 packet_set_encryption_key(child_state.ssh1key,
1520 child_state.ssh1keylen, child_state.ssh1cipher);
1521 xfree(child_state.ssh1key);
1524 /* for rc4 and other stateful ciphers */
1525 packet_set_keycontext(MODE_OUT, child_state.keyout);
1526 xfree(child_state.keyout);
1527 packet_set_keycontext(MODE_IN, child_state.keyin);
1528 xfree(child_state.keyin);
1531 packet_set_iv(MODE_OUT, child_state.ivout);
1532 xfree(child_state.ivout);
1533 packet_set_iv(MODE_IN, child_state.ivin);
1534 xfree(child_state.ivin);
1537 memcpy(&incoming_stream, &child_state.incoming,
1538 sizeof(incoming_stream));
1539 memcpy(&outgoing_stream, &child_state.outgoing,
1540 sizeof(outgoing_stream));
1542 /* Update with new address */
1543 if (options.compression)
1544 mm_init_compression(pmonitor->m_zlib);
1546 /* Network I/O buffers */
1547 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1548 buffer_clear(&input);
1549 buffer_append(&input, child_state.input, child_state.ilen);
1550 memset(child_state.input, 0, child_state.ilen);
1551 xfree(child_state.input);
1553 buffer_clear(&output);
1554 buffer_append(&output, child_state.output, child_state.olen);
1555 memset(child_state.output, 0, child_state.olen);
1556 xfree(child_state.output);
1560 mm_get_kex(Buffer *m)
1566 kex = xmalloc(sizeof(*kex));
1567 memset(kex, 0, sizeof(*kex));
1568 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1569 if ((session_id2 == NULL) ||
1570 (kex->session_id_len != session_id2_len) ||
1571 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1572 fatal("mm_get_get: internal error: bad session id");
1573 kex->we_need = buffer_get_int(m);
1574 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1575 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1577 kex->hostkey_type = buffer_get_int(m);
1578 kex->kex_type = buffer_get_int(m);
1579 blob = buffer_get_string(m, &bloblen);
1580 buffer_init(&kex->my);
1581 buffer_append(&kex->my, blob, bloblen);
1583 blob = buffer_get_string(m, &bloblen);
1584 buffer_init(&kex->peer);
1585 buffer_append(&kex->peer, blob, bloblen);
1588 kex->flags = buffer_get_int(m);
1589 kex->client_version_string = buffer_get_string(m, NULL);
1590 kex->server_version_string = buffer_get_string(m, NULL);
1591 kex->load_host_key=&get_hostkey_by_type;
1592 kex->host_key_index=&get_hostkey_index;
1597 /* This function requries careful sanity checking */
1600 mm_get_keystate(struct monitor *pmonitor)
1604 u_int bloblen, plen;
1605 u_int32_t seqnr, packets;
1608 debug3("%s: Waiting for new keys", __func__);
1611 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1613 child_state.ssh1protoflags = buffer_get_int(&m);
1614 child_state.ssh1cipher = buffer_get_int(&m);
1615 child_state.ssh1key = buffer_get_string(&m,
1616 &child_state.ssh1keylen);
1617 child_state.ivout = buffer_get_string(&m,
1618 &child_state.ivoutlen);
1619 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1622 /* Get the Kex for rekeying */
1623 *pmonitor->m_pkex = mm_get_kex(&m);
1626 blob = buffer_get_string(&m, &bloblen);
1627 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1630 debug3("%s: Waiting for second key", __func__);
1631 blob = buffer_get_string(&m, &bloblen);
1632 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1635 /* Now get sequence numbers for the packets */
1636 seqnr = buffer_get_int(&m);
1637 blocks = buffer_get_int64(&m);
1638 packets = buffer_get_int(&m);
1639 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1640 seqnr = buffer_get_int(&m);
1641 blocks = buffer_get_int64(&m);
1642 packets = buffer_get_int(&m);
1643 packet_set_state(MODE_IN, seqnr, blocks, packets);
1646 /* Get the key context */
1647 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1648 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1650 debug3("%s: Getting compression state", __func__);
1651 /* Get compression state */
1652 p = buffer_get_string(&m, &plen);
1653 if (plen != sizeof(child_state.outgoing))
1654 fatal("%s: bad request size", __func__);
1655 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1658 p = buffer_get_string(&m, &plen);
1659 if (plen != sizeof(child_state.incoming))
1660 fatal("%s: bad request size", __func__);
1661 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1664 /* Network I/O buffers */
1665 debug3("%s: Getting Network I/O buffers", __func__);
1666 child_state.input = buffer_get_string(&m, &child_state.ilen);
1667 child_state.output = buffer_get_string(&m, &child_state.olen);
1673 /* Allocation functions for zlib */
1675 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1677 size_t len = (size_t) size * ncount;
1680 if (len == 0 || ncount > SIZE_T_MAX / size)
1681 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1683 address = mm_malloc(mm, len);
1689 mm_zfree(struct mm_master *mm, void *address)
1691 mm_free(mm, address);
1695 mm_init_compression(struct mm_master *mm)
1697 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1698 outgoing_stream.zfree = (free_func)mm_zfree;
1699 outgoing_stream.opaque = mm;
1701 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1702 incoming_stream.zfree = (free_func)mm_zfree;
1703 incoming_stream.opaque = mm;
1708 #define FD_CLOSEONEXEC(x) do { \
1709 if (fcntl(x, F_SETFD, 1) == -1) \
1710 fatal("fcntl(%d, F_SETFD)", x); \
1714 monitor_socketpair(int *pair)
1716 #ifdef HAVE_SOCKETPAIR
1717 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1718 fatal("%s: socketpair", __func__);
1720 fatal("%s: UsePrivilegeSeparation=yes not supported",
1723 FD_CLOSEONEXEC(pair[0]);
1724 FD_CLOSEONEXEC(pair[1]);
1727 #define MM_MEMSIZE 65536
1732 struct monitor *mon;
1735 mon = xmalloc(sizeof(*mon));
1737 monitor_socketpair(pair);
1739 mon->m_recvfd = pair[0];
1740 mon->m_sendfd = pair[1];
1742 /* Used to share zlib space across processes */
1743 if (options.compression) {
1744 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1745 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1747 /* Compression needs to share state across borders */
1748 mm_init_compression(mon->m_zlib);
1755 monitor_reinit(struct monitor *mon)
1759 monitor_socketpair(pair);
1761 mon->m_recvfd = pair[0];
1762 mon->m_sendfd = pair[1];