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.29 2002/09/26 11:38:43 markus Exp $");
29 RCSID("$FreeBSD: src/crypto/openssh/monitor.c,v 1.7.2.3 2003/02/03 17:31:07 des Exp $");
30 RCSID("$DragonFly: src/crypto/openssh/Attic/monitor.c,v 1.2 2003/06/17 04:24:36 dillon Exp $");
32 #include <openssl/dh.h>
38 #define skeychallenge(k, u, c) opiechallenge((k), (u), (c))
39 #define skey_haskey(u) opie_haskey((u))
40 #define skey_passcheck(u, r) opie_passverify((u), (r))
52 #include "auth-options.h"
61 #include "monitor_mm.h"
62 #include "monitor_wrap.h"
63 #include "monitor_fdpass.h"
73 extern ServerOptions options;
74 extern u_int utmp_len;
75 extern Newkeys *current_keys[];
76 extern z_stream incoming_stream;
77 extern z_stream outgoing_stream;
78 extern u_char session_id[];
79 extern Buffer input, output;
80 extern Buffer auth_debug;
81 extern int auth_debug_init;
83 /* State exported from the child */
106 /* Functions on the montior that answer unprivileged requests */
108 int mm_answer_moduli(int, Buffer *);
109 int mm_answer_sign(int, Buffer *);
110 int mm_answer_pwnamallow(int, Buffer *);
111 int mm_answer_auth2_read_banner(int, Buffer *);
112 int mm_answer_authserv(int, Buffer *);
113 int mm_answer_authpassword(int, Buffer *);
114 int mm_answer_bsdauthquery(int, Buffer *);
115 int mm_answer_bsdauthrespond(int, Buffer *);
116 int mm_answer_skeyquery(int, Buffer *);
117 int mm_answer_skeyrespond(int, Buffer *);
118 int mm_answer_keyallowed(int, Buffer *);
119 int mm_answer_keyverify(int, Buffer *);
120 int mm_answer_pty(int, Buffer *);
121 int mm_answer_pty_cleanup(int, Buffer *);
122 int mm_answer_term(int, Buffer *);
123 int mm_answer_rsa_keyallowed(int, Buffer *);
124 int mm_answer_rsa_challenge(int, Buffer *);
125 int mm_answer_rsa_response(int, Buffer *);
126 int mm_answer_sesskey(int, Buffer *);
127 int mm_answer_sessid(int, Buffer *);
130 int mm_answer_pam_start(int, Buffer *);
131 int mm_answer_pam_init_ctx(int, Buffer *);
132 int mm_answer_pam_query(int, Buffer *);
133 int mm_answer_pam_respond(int, Buffer *);
134 int mm_answer_pam_free_ctx(int, Buffer *);
138 int mm_answer_krb4(int, Buffer *);
141 int mm_answer_krb5(int, Buffer *);
144 static Authctxt *authctxt;
145 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
147 /* local state for key verify */
148 static u_char *key_blob = NULL;
149 static u_int key_bloblen = 0;
150 static int key_blobtype = MM_NOKEY;
151 static char *hostbased_cuser = NULL;
152 static char *hostbased_chost = NULL;
153 static char *auth_method = "unknown";
154 static int session_id2_len = 0;
155 static u_char *session_id2 = NULL;
158 enum monitor_reqtype type;
160 int (*f)(int, Buffer *);
163 #define MON_ISAUTH 0x0004 /* Required for Authentication */
164 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
165 #define MON_ONCE 0x0010 /* Disable after calling */
167 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
169 #define MON_PERMIT 0x1000 /* Request is permitted */
171 struct mon_table mon_dispatch_proto20[] = {
172 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
173 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
174 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
175 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
176 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
177 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
179 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
180 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
181 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
182 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
183 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
186 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
187 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
190 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
191 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
193 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
194 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
198 struct mon_table mon_dispatch_postauth20[] = {
199 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
200 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
201 {MONITOR_REQ_PTY, 0, mm_answer_pty},
202 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
203 {MONITOR_REQ_TERM, 0, mm_answer_term},
207 struct mon_table mon_dispatch_proto15[] = {
208 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
209 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
210 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
211 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
212 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
213 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
214 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
215 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
217 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
218 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
221 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
222 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
225 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
226 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
227 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
228 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
229 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
232 {MONITOR_REQ_KRB4, MON_ONCE|MON_AUTH, mm_answer_krb4},
235 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
240 struct mon_table mon_dispatch_postauth15[] = {
241 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
242 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
243 {MONITOR_REQ_TERM, 0, mm_answer_term},
247 struct mon_table *mon_dispatch;
249 /* Specifies if a certain message is allowed at the moment */
252 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
254 while (ent->f != NULL) {
255 if (ent->type == type) {
256 ent->flags &= ~MON_PERMIT;
257 ent->flags |= permit ? MON_PERMIT : 0;
265 monitor_permit_authentications(int permit)
267 struct mon_table *ent = mon_dispatch;
269 while (ent->f != NULL) {
270 if (ent->flags & MON_AUTH) {
271 ent->flags &= ~MON_PERMIT;
272 ent->flags |= permit ? MON_PERMIT : 0;
279 monitor_child_preauth(struct monitor *pmonitor)
281 struct mon_table *ent;
282 int authenticated = 0;
284 debug3("preauth child monitor started");
287 mon_dispatch = mon_dispatch_proto20;
289 /* Permit requests for moduli and signatures */
290 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
291 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
293 mon_dispatch = mon_dispatch_proto15;
295 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
298 authctxt = authctxt_new();
300 /* The first few requests do not require asynchronous access */
301 while (!authenticated) {
302 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
304 if (!(ent->flags & MON_AUTHDECIDE))
305 fatal("%s: unexpected authentication from %d",
306 __func__, ent->type);
307 if (authctxt->pw->pw_uid == 0 &&
308 !auth_root_allowed(auth_method))
312 if (ent->flags & MON_AUTHDECIDE) {
313 auth_log(authctxt, authenticated, auth_method,
314 compat20 ? " ssh2" : "");
316 authctxt->failures++;
320 if (!authctxt->valid)
321 fatal("%s: authenticated invalid user", __func__);
323 debug("%s: %s has been authenticated by privileged process",
324 __func__, authctxt->user);
326 mm_get_keystate(pmonitor);
332 monitor_child_postauth(struct monitor *pmonitor)
335 mon_dispatch = mon_dispatch_postauth20;
337 /* Permit requests for moduli and signatures */
338 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
339 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
340 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
343 mon_dispatch = mon_dispatch_postauth15;
344 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
347 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
348 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
352 monitor_read(pmonitor, mon_dispatch, NULL);
356 monitor_sync(struct monitor *pmonitor)
358 if (options.compression) {
359 /* The member allocation is not visible, so sync it */
360 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
365 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
366 struct mon_table **pent)
374 mm_request_receive(pmonitor->m_sendfd, &m);
375 type = buffer_get_char(&m);
377 debug3("%s: checking request %d", __func__, type);
379 while (ent->f != NULL) {
380 if (ent->type == type)
385 if (ent->f != NULL) {
386 if (!(ent->flags & MON_PERMIT))
387 fatal("%s: unpermitted request %d", __func__,
389 ret = (*ent->f)(pmonitor->m_sendfd, &m);
392 /* The child may use this request only once, disable it */
393 if (ent->flags & MON_ONCE) {
394 debug2("%s: %d used once, disabling now", __func__,
396 ent->flags &= ~MON_PERMIT;
405 fatal("%s: unsupported request: %d", __func__, type);
411 /* allowed key state */
413 monitor_allowed_key(u_char *blob, u_int bloblen)
415 /* make sure key is allowed */
416 if (key_blob == NULL || key_bloblen != bloblen ||
417 memcmp(key_blob, blob, key_bloblen))
423 monitor_reset_key_state(void)
426 if (key_blob != NULL)
428 if (hostbased_cuser != NULL)
429 xfree(hostbased_cuser);
430 if (hostbased_chost != NULL)
431 xfree(hostbased_chost);
434 key_blobtype = MM_NOKEY;
435 hostbased_cuser = NULL;
436 hostbased_chost = NULL;
440 mm_answer_moduli(int socket, Buffer *m)
445 min = buffer_get_int(m);
446 want = buffer_get_int(m);
447 max = buffer_get_int(m);
449 debug3("%s: got parameters: %d %d %d",
450 __func__, min, want, max);
451 /* We need to check here, too, in case the child got corrupted */
452 if (max < min || want < min || max < want)
453 fatal("%s: bad parameters: %d %d %d",
454 __func__, min, want, max);
458 dh = choose_dh(min, want, max);
460 buffer_put_char(m, 0);
463 /* Send first bignum */
464 buffer_put_char(m, 1);
465 buffer_put_bignum2(m, dh->p);
466 buffer_put_bignum2(m, dh->g);
470 mm_request_send(socket, MONITOR_ANS_MODULI, m);
475 mm_answer_sign(int socket, Buffer *m)
480 u_int siglen, datlen;
483 debug3("%s", __func__);
485 keyid = buffer_get_int(m);
486 p = buffer_get_string(m, &datlen);
489 fatal("%s: data length incorrect: %u", __func__, datlen);
491 /* save session id, it will be passed on the first call */
492 if (session_id2_len == 0) {
493 session_id2_len = datlen;
494 session_id2 = xmalloc(session_id2_len);
495 memcpy(session_id2, p, session_id2_len);
498 if ((key = get_hostkey_by_index(keyid)) == NULL)
499 fatal("%s: no hostkey from index %d", __func__, keyid);
500 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
501 fatal("%s: key_sign failed", __func__);
503 debug3("%s: signature %p(%u)", __func__, signature, siglen);
506 buffer_put_string(m, signature, siglen);
511 mm_request_send(socket, MONITOR_ANS_SIGN, m);
513 /* Turn on permissions for getpwnam */
514 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
519 /* Retrieves the password entry and also checks if the user is permitted */
522 mm_answer_pwnamallow(int socket, Buffer *m)
525 struct passwd *pwent;
528 debug3("%s", __func__);
530 if (authctxt->attempt++ != 0)
531 fatal("%s: multiple attempts for getpwnam", __func__);
533 login = buffer_get_string(m, NULL);
535 pwent = getpwnamallow(login);
537 authctxt->user = xstrdup(login);
538 setproctitle("%s [priv]", pwent ? login : "unknown");
544 buffer_put_char(m, 0);
549 authctxt->pw = pwent;
552 buffer_put_char(m, 1);
553 buffer_put_string(m, pwent, sizeof(struct passwd));
554 buffer_put_cstring(m, pwent->pw_name);
555 buffer_put_cstring(m, "*");
556 buffer_put_cstring(m, pwent->pw_gecos);
557 #ifdef HAVE_PW_CLASS_IN_PASSWD
558 buffer_put_cstring(m, pwent->pw_class);
560 buffer_put_cstring(m, pwent->pw_dir);
561 buffer_put_cstring(m, pwent->pw_shell);
564 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
565 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
567 /* For SSHv1 allow authentication now */
569 monitor_permit_authentications(1);
571 /* Allow service/style information on the auth context */
572 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
573 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
577 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
583 int mm_answer_auth2_read_banner(int socket, Buffer *m)
588 banner = auth2_read_banner();
589 buffer_put_cstring(m, banner != NULL ? banner : "");
590 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
599 mm_answer_authserv(int socket, Buffer *m)
601 monitor_permit_authentications(1);
603 authctxt->service = buffer_get_string(m, NULL);
604 authctxt->style = buffer_get_string(m, NULL);
605 debug3("%s: service=%s, style=%s",
606 __func__, authctxt->service, authctxt->style);
608 if (strlen(authctxt->style) == 0) {
609 xfree(authctxt->style);
610 authctxt->style = NULL;
617 mm_answer_authpassword(int socket, Buffer *m)
619 static int call_count;
624 passwd = buffer_get_string(m, &plen);
625 /* Only authenticate if the context is valid */
626 authenticated = options.password_authentication &&
627 authctxt->valid && auth_password(authctxt, passwd);
628 memset(passwd, 0, strlen(passwd));
632 buffer_put_int(m, authenticated);
634 debug3("%s: sending result %d", __func__, authenticated);
635 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
638 if (plen == 0 && call_count == 1)
639 auth_method = "none";
641 auth_method = "password";
643 /* Causes monitor loop to terminate if authenticated */
644 return (authenticated);
649 mm_answer_bsdauthquery(int socket, Buffer *m)
651 char *name, *infotxt;
657 res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
661 buffer_put_int(m, res);
663 buffer_put_cstring(m, prompts[0]);
665 debug3("%s: sending challenge res: %d", __func__, res);
666 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
679 mm_answer_bsdauthrespond(int socket, Buffer *m)
684 if (authctxt->as == 0)
685 fatal("%s: no bsd auth session", __func__);
687 response = buffer_get_string(m, NULL);
688 authok = options.challenge_response_authentication &&
689 auth_userresponse(authctxt->as, response, 0);
691 debug3("%s: <%s> = <%d>", __func__, response, authok);
695 buffer_put_int(m, authok);
697 debug3("%s: sending authenticated: %d", __func__, authok);
698 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
700 auth_method = "bsdauth";
702 return (authok != 0);
708 mm_answer_skeyquery(int socket, Buffer *m)
711 char challenge[1024];
714 res = skeychallenge(&skey, authctxt->user, challenge);
717 buffer_put_int(m, res);
719 buffer_put_cstring(m, challenge);
721 debug3("%s: sending challenge res: %d", __func__, res);
722 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
728 mm_answer_skeyrespond(int socket, Buffer *m)
733 response = buffer_get_string(m, NULL);
735 authok = (options.challenge_response_authentication &&
737 skey_haskey(authctxt->pw->pw_name) == 0 &&
738 skey_passcheck(authctxt->pw->pw_name, response) != -1);
743 buffer_put_int(m, authok);
745 debug3("%s: sending authenticated: %d", __func__, authok);
746 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
748 auth_method = "skey";
750 return (authok != 0);
756 mm_answer_pam_start(int socket, Buffer *m)
760 user = buffer_get_string(m, NULL);
769 static void *pam_ctxt, *pam_authok;
770 extern KbdintDevice pam_device;
773 mm_answer_pam_init_ctx(int socket, Buffer *m)
776 debug3("%s", __func__);
777 authctxt->user = buffer_get_string(m, NULL);
778 pam_ctxt = (pam_device.init_ctx)(authctxt);
781 if (pam_ctxt != NULL) {
782 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
783 buffer_put_int(m, 1);
785 buffer_put_int(m, 0);
787 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
792 mm_answer_pam_query(int socket, Buffer *m)
794 char *name, *info, **prompts;
798 debug3("%s", __func__);
800 ret = (pam_device.query)(pam_ctxt, &name, &info, &num, &prompts, &echo_on);
801 if (ret == 0 && num == 0)
802 pam_authok = pam_ctxt;
803 if (num > 1 || name == NULL || info == NULL)
806 buffer_put_int(m, ret);
807 buffer_put_cstring(m, name);
809 buffer_put_cstring(m, info);
811 buffer_put_int(m, num);
812 for (i = 0; i < num; ++i) {
813 buffer_put_cstring(m, prompts[i]);
815 buffer_put_int(m, echo_on[i]);
821 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
826 mm_answer_pam_respond(int socket, Buffer *m)
832 debug3("%s", __func__);
834 num = buffer_get_int(m);
836 resp = xmalloc(num * sizeof(char *));
837 for (i = 0; i < num; ++i)
838 resp[i] = buffer_get_string(m, NULL);
839 ret = (pam_device.respond)(pam_ctxt, num, resp);
840 for (i = 0; i < num; ++i)
844 ret = (pam_device.respond)(pam_ctxt, num, NULL);
847 buffer_put_int(m, ret);
848 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
849 auth_method = "keyboard-interactive/pam";
851 pam_authok = pam_ctxt;
856 mm_answer_pam_free_ctx(int socket, Buffer *m)
859 debug3("%s", __func__);
860 (pam_device.free_ctx)(pam_ctxt);
862 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
863 return (pam_authok == pam_ctxt);
868 mm_append_debug(Buffer *m)
870 if (auth_debug_init && buffer_len(&auth_debug)) {
871 debug3("%s: Appending debug messages for child", __func__);
872 buffer_append(m, buffer_ptr(&auth_debug),
873 buffer_len(&auth_debug));
874 buffer_clear(&auth_debug);
879 mm_answer_keyallowed(int socket, Buffer *m)
885 enum mm_keytype type = 0;
888 debug3("%s entering", __func__);
890 type = buffer_get_int(m);
891 cuser = buffer_get_string(m, NULL);
892 chost = buffer_get_string(m, NULL);
893 blob = buffer_get_string(m, &bloblen);
895 key = key_from_blob(blob, bloblen);
897 if ((compat20 && type == MM_RSAHOSTKEY) ||
898 (!compat20 && type != MM_RSAHOSTKEY))
899 fatal("%s: key type and protocol mismatch", __func__);
901 debug3("%s: key_from_blob: %p", __func__, key);
903 if (key != NULL && authctxt->pw != NULL) {
906 allowed = options.pubkey_authentication &&
907 user_key_allowed(authctxt->pw, key);
910 allowed = options.hostbased_authentication &&
911 hostbased_key_allowed(authctxt->pw,
915 key->type = KEY_RSA1; /* XXX */
916 allowed = options.rhosts_rsa_authentication &&
917 auth_rhosts_rsa_key_allowed(authctxt->pw,
921 fatal("%s: unknown key type %d", __func__, type);
927 /* clear temporarily storage (used by verify) */
928 monitor_reset_key_state();
931 /* Save temporarily for comparison in verify */
933 key_bloblen = bloblen;
935 hostbased_cuser = cuser;
936 hostbased_chost = chost;
939 debug3("%s: key %p is %s",
940 __func__, key, allowed ? "allowed" : "disallowed");
943 buffer_put_int(m, allowed);
947 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
949 if (type == MM_RSAHOSTKEY)
950 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
956 monitor_valid_userblob(u_char *data, u_int datalen)
964 buffer_append(&b, data, datalen);
966 if (datafellows & SSH_OLD_SESSIONID) {
968 len = buffer_len(&b);
969 if ((session_id2 == NULL) ||
970 (len < session_id2_len) ||
971 (memcmp(p, session_id2, session_id2_len) != 0))
973 buffer_consume(&b, session_id2_len);
975 p = buffer_get_string(&b, &len);
976 if ((session_id2 == NULL) ||
977 (len != session_id2_len) ||
978 (memcmp(p, session_id2, session_id2_len) != 0))
982 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
984 p = buffer_get_string(&b, NULL);
985 if (strcmp(authctxt->user, p) != 0) {
986 log("wrong user name passed to monitor: expected %s != %.100s",
991 buffer_skip_string(&b);
992 if (datafellows & SSH_BUG_PKAUTH) {
993 if (!buffer_get_char(&b))
996 p = buffer_get_string(&b, NULL);
997 if (strcmp("publickey", p) != 0)
1000 if (!buffer_get_char(&b))
1002 buffer_skip_string(&b);
1004 buffer_skip_string(&b);
1005 if (buffer_len(&b) != 0)
1012 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1021 buffer_append(&b, data, datalen);
1023 p = buffer_get_string(&b, &len);
1024 if ((session_id2 == NULL) ||
1025 (len != session_id2_len) ||
1026 (memcmp(p, session_id2, session_id2_len) != 0))
1030 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1032 p = buffer_get_string(&b, NULL);
1033 if (strcmp(authctxt->user, p) != 0) {
1034 log("wrong user name passed to monitor: expected %s != %.100s",
1039 buffer_skip_string(&b); /* service */
1040 p = buffer_get_string(&b, NULL);
1041 if (strcmp(p, "hostbased") != 0)
1044 buffer_skip_string(&b); /* pkalg */
1045 buffer_skip_string(&b); /* pkblob */
1047 /* verify client host, strip trailing dot if necessary */
1048 p = buffer_get_string(&b, NULL);
1049 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1051 if (strcmp(p, chost) != 0)
1055 /* verify client user */
1056 p = buffer_get_string(&b, NULL);
1057 if (strcmp(p, cuser) != 0)
1061 if (buffer_len(&b) != 0)
1068 mm_answer_keyverify(int socket, Buffer *m)
1071 u_char *signature, *data, *blob;
1072 u_int signaturelen, datalen, bloblen;
1076 blob = buffer_get_string(m, &bloblen);
1077 signature = buffer_get_string(m, &signaturelen);
1078 data = buffer_get_string(m, &datalen);
1080 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1081 !monitor_allowed_key(blob, bloblen))
1082 fatal("%s: bad key, not previously allowed", __func__);
1084 key = key_from_blob(blob, bloblen);
1086 fatal("%s: bad public key blob", __func__);
1088 switch (key_blobtype) {
1090 valid_data = monitor_valid_userblob(data, datalen);
1093 valid_data = monitor_valid_hostbasedblob(data, datalen,
1094 hostbased_cuser, hostbased_chost);
1101 fatal("%s: bad signature data blob", __func__);
1103 verified = key_verify(key, signature, signaturelen, data, datalen);
1104 debug3("%s: key %p signature %s",
1105 __func__, key, verified ? "verified" : "unverified");
1112 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1114 monitor_reset_key_state();
1117 buffer_put_int(m, verified);
1118 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1124 mm_record_login(Session *s, struct passwd *pw)
1127 struct sockaddr_storage from;
1130 * Get IP address of client. If the connection is not a socket, let
1131 * the address be 0.0.0.0.
1133 memset(&from, 0, sizeof(from));
1134 fromlen = sizeof(from);
1135 if (packet_connection_is_on_socket()) {
1136 if (getpeername(packet_get_connection_in(),
1137 (struct sockaddr *) & from, &fromlen) < 0) {
1138 debug("getpeername: %.100s", strerror(errno));
1142 /* Record that there was a login on that tty from the remote host. */
1143 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1144 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
1145 (struct sockaddr *)&from, fromlen);
1149 mm_session_close(Session *s)
1151 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
1152 if (s->ttyfd != -1) {
1153 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1154 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1155 session_pty_cleanup2(s);
1161 mm_answer_pty(int socket, Buffer *m)
1163 extern struct monitor *pmonitor;
1167 debug3("%s entering", __func__);
1173 s->authctxt = authctxt;
1174 s->pw = authctxt->pw;
1175 s->pid = pmonitor->m_pid;
1176 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1179 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1180 pty_setowner(authctxt->pw, s->tty);
1182 buffer_put_int(m, 1);
1183 buffer_put_cstring(m, s->tty);
1184 mm_request_send(socket, MONITOR_ANS_PTY, m);
1186 mm_send_fd(socket, s->ptyfd);
1187 mm_send_fd(socket, s->ttyfd);
1189 /* We need to trick ttyslot */
1190 if (dup2(s->ttyfd, 0) == -1)
1191 fatal("%s: dup2", __func__);
1193 mm_record_login(s, authctxt->pw);
1195 /* Now we can close the file descriptor again */
1198 /* make sure nothing uses fd 0 */
1199 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1200 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1202 error("%s: fd0 %d != 0", __func__, fd0);
1204 /* slave is not needed */
1206 s->ttyfd = s->ptyfd;
1207 /* no need to dup() because nobody closes ptyfd */
1208 s->ptymaster = s->ptyfd;
1210 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1216 mm_session_close(s);
1217 buffer_put_int(m, 0);
1218 mm_request_send(socket, MONITOR_ANS_PTY, m);
1223 mm_answer_pty_cleanup(int socket, Buffer *m)
1228 debug3("%s entering", __func__);
1230 tty = buffer_get_string(m, NULL);
1231 if ((s = session_by_tty(tty)) != NULL)
1232 mm_session_close(s);
1239 mm_answer_sesskey(int socket, Buffer *m)
1244 /* Turn off permissions */
1245 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1247 if ((p = BN_new()) == NULL)
1248 fatal("%s: BN_new", __func__);
1250 buffer_get_bignum2(m, p);
1252 rsafail = ssh1_session_key(p);
1255 buffer_put_int(m, rsafail);
1256 buffer_put_bignum2(m, p);
1260 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1262 /* Turn on permissions for sessid passing */
1263 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1269 mm_answer_sessid(int socket, Buffer *m)
1273 debug3("%s entering", __func__);
1275 if (buffer_len(m) != 16)
1276 fatal("%s: bad ssh1 session id", __func__);
1277 for (i = 0; i < 16; i++)
1278 session_id[i] = buffer_get_char(m);
1280 /* Turn on permissions for getpwnam */
1281 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1287 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1291 u_char *blob = NULL;
1295 debug3("%s entering", __func__);
1297 if (options.rsa_authentication && authctxt->valid) {
1298 if ((client_n = BN_new()) == NULL)
1299 fatal("%s: BN_new", __func__);
1300 buffer_get_bignum2(m, client_n);
1301 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1302 BN_clear_free(client_n);
1305 buffer_put_int(m, allowed);
1307 /* clear temporarily storage (used by generate challenge) */
1308 monitor_reset_key_state();
1310 if (allowed && key != NULL) {
1311 key->type = KEY_RSA; /* cheat for key_to_blob */
1312 if (key_to_blob(key, &blob, &blen) == 0)
1313 fatal("%s: key_to_blob failed", __func__);
1314 buffer_put_string(m, blob, blen);
1316 /* Save temporarily for comparison in verify */
1319 key_blobtype = MM_RSAUSERKEY;
1325 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1327 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1328 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1333 mm_answer_rsa_challenge(int socket, Buffer *m)
1339 debug3("%s entering", __func__);
1341 if (!authctxt->valid)
1342 fatal("%s: authctxt not valid", __func__);
1343 blob = buffer_get_string(m, &blen);
1344 if (!monitor_allowed_key(blob, blen))
1345 fatal("%s: bad key, not previously allowed", __func__);
1346 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1347 fatal("%s: key type mismatch", __func__);
1348 if ((key = key_from_blob(blob, blen)) == NULL)
1349 fatal("%s: received bad key", __func__);
1352 BN_clear_free(ssh1_challenge);
1353 ssh1_challenge = auth_rsa_generate_challenge(key);
1356 buffer_put_bignum2(m, ssh1_challenge);
1358 debug3("%s sending reply", __func__);
1359 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1361 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1366 mm_answer_rsa_response(int socket, Buffer *m)
1369 u_char *blob, *response;
1373 debug3("%s entering", __func__);
1375 if (!authctxt->valid)
1376 fatal("%s: authctxt not valid", __func__);
1377 if (ssh1_challenge == NULL)
1378 fatal("%s: no ssh1_challenge", __func__);
1380 blob = buffer_get_string(m, &blen);
1381 if (!monitor_allowed_key(blob, blen))
1382 fatal("%s: bad key, not previously allowed", __func__);
1383 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1384 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1385 if ((key = key_from_blob(blob, blen)) == NULL)
1386 fatal("%s: received bad key", __func__);
1387 response = buffer_get_string(m, &len);
1389 fatal("%s: received bad response to challenge", __func__);
1390 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1395 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1398 BN_clear_free(ssh1_challenge);
1399 ssh1_challenge = NULL;
1400 monitor_reset_key_state();
1403 buffer_put_int(m, success);
1404 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1411 mm_answer_krb4(int socket, Buffer *m)
1413 KTEXT_ST auth, reply;
1418 reply.length = auth.length = 0;
1420 p = buffer_get_string(m, &alen);
1421 if (alen >= MAX_KTXT_LEN)
1422 fatal("%s: auth too large", __func__);
1423 memcpy(auth.dat, p, alen);
1428 success = options.kerberos_authentication &&
1430 auth_krb4(authctxt, &auth, &client, &reply);
1432 memset(auth.dat, 0, alen);
1434 buffer_put_int(m, success);
1437 buffer_put_cstring(m, client);
1438 buffer_put_string(m, reply.dat, reply.length);
1442 memset(reply.dat, 0, reply.length);
1445 debug3("%s: sending result %d", __func__, success);
1446 mm_request_send(socket, MONITOR_ANS_KRB4, m);
1448 auth_method = "kerberos";
1450 /* Causes monitor loop to terminate if authenticated */
1457 mm_answer_krb5(int socket, Buffer *m)
1459 krb5_data tkt, reply;
1464 /* use temporary var to avoid size issues on 64bit arch */
1465 tkt.data = buffer_get_string(m, &len);
1468 success = options.kerberos_authentication &&
1470 auth_krb5(authctxt, &tkt, &client_user, &reply);
1476 buffer_put_int(m, success);
1479 buffer_put_cstring(m, client_user);
1480 buffer_put_string(m, reply.data, reply.length);
1486 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1493 mm_answer_term(int socket, Buffer *req)
1495 extern struct monitor *pmonitor;
1498 debug3("%s: tearing down sessions", __func__);
1500 /* The child is terminating */
1501 session_destroy_all(&mm_session_close);
1503 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1507 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1509 /* Terminate process */
1514 monitor_apply_keystate(struct monitor *pmonitor)
1517 set_newkeys(MODE_IN);
1518 set_newkeys(MODE_OUT);
1520 packet_set_protocol_flags(child_state.ssh1protoflags);
1521 packet_set_encryption_key(child_state.ssh1key,
1522 child_state.ssh1keylen, child_state.ssh1cipher);
1523 xfree(child_state.ssh1key);
1526 /* for rc4 and other stateful ciphers */
1527 packet_set_keycontext(MODE_OUT, child_state.keyout);
1528 xfree(child_state.keyout);
1529 packet_set_keycontext(MODE_IN, child_state.keyin);
1530 xfree(child_state.keyin);
1533 packet_set_iv(MODE_OUT, child_state.ivout);
1534 xfree(child_state.ivout);
1535 packet_set_iv(MODE_IN, child_state.ivin);
1536 xfree(child_state.ivin);
1539 memcpy(&incoming_stream, &child_state.incoming,
1540 sizeof(incoming_stream));
1541 memcpy(&outgoing_stream, &child_state.outgoing,
1542 sizeof(outgoing_stream));
1544 /* Update with new address */
1545 if (options.compression)
1546 mm_init_compression(pmonitor->m_zlib);
1548 /* Network I/O buffers */
1549 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1550 buffer_clear(&input);
1551 buffer_append(&input, child_state.input, child_state.ilen);
1552 memset(child_state.input, 0, child_state.ilen);
1553 xfree(child_state.input);
1555 buffer_clear(&output);
1556 buffer_append(&output, child_state.output, child_state.olen);
1557 memset(child_state.output, 0, child_state.olen);
1558 xfree(child_state.output);
1562 mm_get_kex(Buffer *m)
1568 kex = xmalloc(sizeof(*kex));
1569 memset(kex, 0, sizeof(*kex));
1570 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1571 if ((session_id2 == NULL) ||
1572 (kex->session_id_len != session_id2_len) ||
1573 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1574 fatal("mm_get_get: internal error: bad session id");
1575 kex->we_need = buffer_get_int(m);
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;
1606 debug3("%s: Waiting for new keys", __func__);
1609 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1611 child_state.ssh1protoflags = buffer_get_int(&m);
1612 child_state.ssh1cipher = buffer_get_int(&m);
1613 child_state.ssh1key = buffer_get_string(&m,
1614 &child_state.ssh1keylen);
1615 child_state.ivout = buffer_get_string(&m,
1616 &child_state.ivoutlen);
1617 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1620 /* Get the Kex for rekeying */
1621 *pmonitor->m_pkex = mm_get_kex(&m);
1624 blob = buffer_get_string(&m, &bloblen);
1625 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1628 debug3("%s: Waiting for second key", __func__);
1629 blob = buffer_get_string(&m, &bloblen);
1630 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1633 /* Now get sequence numbers for the packets */
1634 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1635 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1638 /* Get the key context */
1639 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1640 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1642 debug3("%s: Getting compression state", __func__);
1643 /* Get compression state */
1644 p = buffer_get_string(&m, &plen);
1645 if (plen != sizeof(child_state.outgoing))
1646 fatal("%s: bad request size", __func__);
1647 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1650 p = buffer_get_string(&m, &plen);
1651 if (plen != sizeof(child_state.incoming))
1652 fatal("%s: bad request size", __func__);
1653 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1656 /* Network I/O buffers */
1657 debug3("%s: Getting Network I/O buffers", __func__);
1658 child_state.input = buffer_get_string(&m, &child_state.ilen);
1659 child_state.output = buffer_get_string(&m, &child_state.olen);
1665 /* Allocation functions for zlib */
1667 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1669 size_t len = size * ncount;
1672 if (len == 0 || ncount > SIZE_T_MAX / size)
1673 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1675 address = mm_malloc(mm, len);
1681 mm_zfree(struct mm_master *mm, void *address)
1683 mm_free(mm, address);
1687 mm_init_compression(struct mm_master *mm)
1689 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1690 outgoing_stream.zfree = (free_func)mm_zfree;
1691 outgoing_stream.opaque = mm;
1693 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1694 incoming_stream.zfree = (free_func)mm_zfree;
1695 incoming_stream.opaque = mm;
1700 #define FD_CLOSEONEXEC(x) do { \
1701 if (fcntl(x, F_SETFD, 1) == -1) \
1702 fatal("fcntl(%d, F_SETFD)", x); \
1706 monitor_socketpair(int *pair)
1708 #ifdef HAVE_SOCKETPAIR
1709 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1710 fatal("%s: socketpair", __func__);
1712 fatal("%s: UsePrivilegeSeparation=yes not supported",
1715 FD_CLOSEONEXEC(pair[0]);
1716 FD_CLOSEONEXEC(pair[1]);
1719 #define MM_MEMSIZE 65536
1724 struct monitor *mon;
1727 mon = xmalloc(sizeof(*mon));
1729 monitor_socketpair(pair);
1731 mon->m_recvfd = pair[0];
1732 mon->m_sendfd = pair[1];
1734 /* Used to share zlib space across processes */
1735 if (options.compression) {
1736 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1737 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1739 /* Compression needs to share state across borders */
1740 mm_init_compression(mon->m_zlib);
1747 monitor_reinit(struct monitor *mon)
1751 monitor_socketpair(pair);
1753 mon->m_recvfd = pair[0];
1754 mon->m_sendfd = pair[1];