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.23 2002/08/02 14:43:15 millert 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 *);
123 static Authctxt *authctxt;
124 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
126 /* local state for key verify */
127 static u_char *key_blob = NULL;
128 static u_int key_bloblen = 0;
129 static int key_blobtype = MM_NOKEY;
130 static u_char *hostbased_cuser = NULL;
131 static u_char *hostbased_chost = NULL;
132 static char *auth_method = "unknown";
133 static int session_id2_len = 0;
134 static u_char *session_id2 = NULL;
137 enum monitor_reqtype type;
139 int (*f)(int, Buffer *);
142 #define MON_ISAUTH 0x0004 /* Required for Authentication */
143 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
144 #define MON_ONCE 0x0010 /* Disable after calling */
146 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
148 #define MON_PERMIT 0x1000 /* Request is permitted */
150 struct mon_table mon_dispatch_proto20[] = {
151 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
152 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
153 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
154 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
155 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
156 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
158 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
161 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
162 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
165 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
166 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
168 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
169 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
173 struct mon_table mon_dispatch_postauth20[] = {
174 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
175 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
176 {MONITOR_REQ_PTY, 0, mm_answer_pty},
177 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
178 {MONITOR_REQ_TERM, 0, mm_answer_term},
182 struct mon_table mon_dispatch_proto15[] = {
183 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
184 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
185 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
186 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
187 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
188 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
189 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
190 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
192 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
193 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
196 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
197 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
200 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
205 struct mon_table mon_dispatch_postauth15[] = {
206 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
207 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
208 {MONITOR_REQ_TERM, 0, mm_answer_term},
212 struct mon_table *mon_dispatch;
214 /* Specifies if a certain message is allowed at the moment */
217 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
219 while (ent->f != NULL) {
220 if (ent->type == type) {
221 ent->flags &= ~MON_PERMIT;
222 ent->flags |= permit ? MON_PERMIT : 0;
230 monitor_permit_authentications(int permit)
232 struct mon_table *ent = mon_dispatch;
234 while (ent->f != NULL) {
235 if (ent->flags & MON_AUTH) {
236 ent->flags &= ~MON_PERMIT;
237 ent->flags |= permit ? MON_PERMIT : 0;
244 monitor_child_preauth(struct monitor *pmonitor)
246 struct mon_table *ent;
247 int authenticated = 0;
249 debug3("preauth child monitor started");
252 mon_dispatch = mon_dispatch_proto20;
254 /* Permit requests for moduli and signatures */
255 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
256 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
258 mon_dispatch = mon_dispatch_proto15;
260 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
263 authctxt = authctxt_new();
265 /* The first few requests do not require asynchronous access */
266 while (!authenticated) {
267 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
269 if (!(ent->flags & MON_AUTHDECIDE))
270 fatal("%s: unexpected authentication from %d",
271 __func__, ent->type);
272 if (authctxt->pw->pw_uid == 0 &&
273 !auth_root_allowed(auth_method))
276 if (!do_pam_account(authctxt->pw->pw_name, NULL))
281 if (ent->flags & MON_AUTHDECIDE) {
282 auth_log(authctxt, authenticated, auth_method,
283 compat20 ? " ssh2" : "");
285 authctxt->failures++;
289 if (!authctxt->valid)
290 fatal("%s: authenticated invalid user", __func__);
292 debug("%s: %s has been authenticated by privileged process",
293 __func__, authctxt->user);
295 mm_get_keystate(pmonitor);
301 monitor_child_postauth(struct monitor *pmonitor)
304 mon_dispatch = mon_dispatch_postauth20;
306 /* Permit requests for moduli and signatures */
307 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
308 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
309 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
312 mon_dispatch = mon_dispatch_postauth15;
313 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
316 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
317 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
321 monitor_read(pmonitor, mon_dispatch, NULL);
325 monitor_sync(struct monitor *pmonitor)
327 if (options.compression) {
328 /* The member allocation is not visible, so sync it */
329 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
334 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
335 struct mon_table **pent)
343 mm_request_receive(pmonitor->m_sendfd, &m);
344 type = buffer_get_char(&m);
346 debug3("%s: checking request %d", __func__, type);
348 while (ent->f != NULL) {
349 if (ent->type == type)
354 if (ent->f != NULL) {
355 if (!(ent->flags & MON_PERMIT))
356 fatal("%s: unpermitted request %d", __func__,
358 ret = (*ent->f)(pmonitor->m_sendfd, &m);
361 /* The child may use this request only once, disable it */
362 if (ent->flags & MON_ONCE) {
363 debug2("%s: %d used once, disabling now", __func__,
365 ent->flags &= ~MON_PERMIT;
374 fatal("%s: unsupported request: %d", __func__, type);
380 /* allowed key state */
382 monitor_allowed_key(u_char *blob, u_int bloblen)
384 /* make sure key is allowed */
385 if (key_blob == NULL || key_bloblen != bloblen ||
386 memcmp(key_blob, blob, key_bloblen))
392 monitor_reset_key_state(void)
395 if (key_blob != NULL)
397 if (hostbased_cuser != NULL)
398 xfree(hostbased_cuser);
399 if (hostbased_chost != NULL)
400 xfree(hostbased_chost);
403 key_blobtype = MM_NOKEY;
404 hostbased_cuser = NULL;
405 hostbased_chost = NULL;
409 mm_answer_moduli(int socket, Buffer *m)
414 min = buffer_get_int(m);
415 want = buffer_get_int(m);
416 max = buffer_get_int(m);
418 debug3("%s: got parameters: %d %d %d",
419 __func__, min, want, max);
420 /* We need to check here, too, in case the child got corrupted */
421 if (max < min || want < min || max < want)
422 fatal("%s: bad parameters: %d %d %d",
423 __func__, min, want, max);
427 dh = choose_dh(min, want, max);
429 buffer_put_char(m, 0);
432 /* Send first bignum */
433 buffer_put_char(m, 1);
434 buffer_put_bignum2(m, dh->p);
435 buffer_put_bignum2(m, dh->g);
439 mm_request_send(socket, MONITOR_ANS_MODULI, m);
444 mm_answer_sign(int socket, Buffer *m)
449 u_int siglen, datlen;
452 debug3("%s", __func__);
454 keyid = buffer_get_int(m);
455 p = buffer_get_string(m, &datlen);
458 fatal("%s: data length incorrect: %u", __func__, datlen);
460 /* save session id, it will be passed on the first call */
461 if (session_id2_len == 0) {
462 session_id2_len = datlen;
463 session_id2 = xmalloc(session_id2_len);
464 memcpy(session_id2, p, session_id2_len);
467 if ((key = get_hostkey_by_index(keyid)) == NULL)
468 fatal("%s: no hostkey from index %d", __func__, keyid);
469 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
470 fatal("%s: key_sign failed", __func__);
472 debug3("%s: signature %p(%u)", __func__, signature, siglen);
475 buffer_put_string(m, signature, siglen);
480 mm_request_send(socket, MONITOR_ANS_SIGN, m);
482 /* Turn on permissions for getpwnam */
483 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
488 /* Retrieves the password entry and also checks if the user is permitted */
491 mm_answer_pwnamallow(int socket, Buffer *m)
494 struct passwd *pwent;
497 debug3("%s", __func__);
499 if (authctxt->attempt++ != 0)
500 fatal("%s: multiple attempts for getpwnam", __func__);
502 login = buffer_get_string(m, NULL);
504 pwent = getpwnamallow(login);
506 authctxt->user = xstrdup(login);
507 setproctitle("%s [priv]", pwent ? login : "unknown");
513 buffer_put_char(m, 0);
518 authctxt->pw = pwent;
521 buffer_put_char(m, 1);
522 buffer_put_string(m, pwent, sizeof(struct passwd));
523 buffer_put_cstring(m, pwent->pw_name);
524 buffer_put_cstring(m, "*");
525 buffer_put_cstring(m, pwent->pw_gecos);
526 #ifdef HAVE_PW_CLASS_IN_PASSWD
527 buffer_put_cstring(m, pwent->pw_class);
529 buffer_put_cstring(m, pwent->pw_dir);
530 buffer_put_cstring(m, pwent->pw_shell);
533 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
534 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
536 /* For SSHv1 allow authentication now */
538 monitor_permit_authentications(1);
540 /* Allow service/style information on the auth context */
541 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
542 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
546 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
552 int mm_answer_auth2_read_banner(int socket, Buffer *m)
557 banner = auth2_read_banner();
558 buffer_put_cstring(m, banner != NULL ? banner : "");
559 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
568 mm_answer_authserv(int socket, Buffer *m)
570 monitor_permit_authentications(1);
572 authctxt->service = buffer_get_string(m, NULL);
573 authctxt->style = buffer_get_string(m, NULL);
574 debug3("%s: service=%s, style=%s",
575 __func__, authctxt->service, authctxt->style);
577 if (strlen(authctxt->style) == 0) {
578 xfree(authctxt->style);
579 authctxt->style = NULL;
586 mm_answer_authpassword(int socket, Buffer *m)
588 static int call_count;
593 passwd = buffer_get_string(m, &plen);
594 /* Only authenticate if the context is valid */
595 authenticated = options.password_authentication &&
596 authctxt->valid && auth_password(authctxt, passwd);
597 memset(passwd, 0, strlen(passwd));
601 buffer_put_int(m, authenticated);
603 debug3("%s: sending result %d", __func__, authenticated);
604 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
607 if (plen == 0 && call_count == 1)
608 auth_method = "none";
610 auth_method = "password";
612 /* Causes monitor loop to terminate if authenticated */
613 return (authenticated);
618 mm_answer_bsdauthquery(int socket, Buffer *m)
620 char *name, *infotxt;
626 res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
630 buffer_put_int(m, res);
632 buffer_put_cstring(m, prompts[0]);
634 debug3("%s: sending challenge res: %d", __func__, res);
635 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
648 mm_answer_bsdauthrespond(int socket, Buffer *m)
653 if (authctxt->as == 0)
654 fatal("%s: no bsd auth session", __func__);
656 response = buffer_get_string(m, NULL);
657 authok = options.challenge_response_authentication &&
658 auth_userresponse(authctxt->as, response, 0);
660 debug3("%s: <%s> = <%d>", __func__, response, authok);
664 buffer_put_int(m, authok);
666 debug3("%s: sending authenticated: %d", __func__, authok);
667 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
669 auth_method = "bsdauth";
671 return (authok != 0);
677 mm_answer_skeyquery(int socket, Buffer *m)
680 char challenge[1024];
683 res = skeychallenge(&skey, authctxt->user, challenge);
686 buffer_put_int(m, res);
688 buffer_put_cstring(m, challenge);
690 debug3("%s: sending challenge res: %d", __func__, res);
691 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
697 mm_answer_skeyrespond(int socket, Buffer *m)
702 response = buffer_get_string(m, NULL);
704 authok = (options.challenge_response_authentication &&
706 skey_haskey(authctxt->pw->pw_name) == 0 &&
707 skey_passcheck(authctxt->pw->pw_name, response) != -1);
712 buffer_put_int(m, authok);
714 debug3("%s: sending authenticated: %d", __func__, authok);
715 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
717 auth_method = "skey";
719 return (authok != 0);
725 mm_answer_pam_start(int socket, Buffer *m)
729 user = buffer_get_string(m, NULL);
740 mm_append_debug(Buffer *m)
742 if (auth_debug_init && buffer_len(&auth_debug)) {
743 debug3("%s: Appending debug messages for child", __func__);
744 buffer_append(m, buffer_ptr(&auth_debug),
745 buffer_len(&auth_debug));
746 buffer_clear(&auth_debug);
751 mm_answer_keyallowed(int socket, Buffer *m)
754 u_char *cuser, *chost, *blob;
756 enum mm_keytype type = 0;
759 debug3("%s entering", __func__);
761 type = buffer_get_int(m);
762 cuser = buffer_get_string(m, NULL);
763 chost = buffer_get_string(m, NULL);
764 blob = buffer_get_string(m, &bloblen);
766 key = key_from_blob(blob, bloblen);
768 if ((compat20 && type == MM_RSAHOSTKEY) ||
769 (!compat20 && type != MM_RSAHOSTKEY))
770 fatal("%s: key type and protocol mismatch", __func__);
772 debug3("%s: key_from_blob: %p", __func__, key);
774 if (key != NULL && authctxt->pw != NULL) {
777 allowed = options.pubkey_authentication &&
778 user_key_allowed(authctxt->pw, key);
781 allowed = options.hostbased_authentication &&
782 hostbased_key_allowed(authctxt->pw,
786 key->type = KEY_RSA1; /* XXX */
787 allowed = options.rhosts_rsa_authentication &&
788 auth_rhosts_rsa_key_allowed(authctxt->pw,
792 fatal("%s: unknown key type %d", __func__, type);
798 /* clear temporarily storage (used by verify) */
799 monitor_reset_key_state();
802 /* Save temporarily for comparison in verify */
804 key_bloblen = bloblen;
806 hostbased_cuser = cuser;
807 hostbased_chost = chost;
810 debug3("%s: key %p is %s",
811 __func__, key, allowed ? "allowed" : "disallowed");
814 buffer_put_int(m, allowed);
818 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
820 if (type == MM_RSAHOSTKEY)
821 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
827 monitor_valid_userblob(u_char *data, u_int datalen)
835 buffer_append(&b, data, datalen);
837 if (datafellows & SSH_OLD_SESSIONID) {
839 len = buffer_len(&b);
840 if ((session_id2 == NULL) ||
841 (len < session_id2_len) ||
842 (memcmp(p, session_id2, session_id2_len) != 0))
844 buffer_consume(&b, session_id2_len);
846 p = buffer_get_string(&b, &len);
847 if ((session_id2 == NULL) ||
848 (len != session_id2_len) ||
849 (memcmp(p, session_id2, session_id2_len) != 0))
853 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
855 p = buffer_get_string(&b, NULL);
856 if (strcmp(authctxt->user, p) != 0) {
857 log("wrong user name passed to monitor: expected %s != %.100s",
862 buffer_skip_string(&b);
863 if (datafellows & SSH_BUG_PKAUTH) {
864 if (!buffer_get_char(&b))
867 p = buffer_get_string(&b, NULL);
868 if (strcmp("publickey", p) != 0)
871 if (!buffer_get_char(&b))
873 buffer_skip_string(&b);
875 buffer_skip_string(&b);
876 if (buffer_len(&b) != 0)
883 monitor_valid_hostbasedblob(u_char *data, u_int datalen, u_char *cuser,
892 buffer_append(&b, data, datalen);
894 p = buffer_get_string(&b, &len);
895 if ((session_id2 == NULL) ||
896 (len != session_id2_len) ||
897 (memcmp(p, session_id2, session_id2_len) != 0))
901 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
903 p = buffer_get_string(&b, NULL);
904 if (strcmp(authctxt->user, p) != 0) {
905 log("wrong user name passed to monitor: expected %s != %.100s",
910 buffer_skip_string(&b); /* service */
911 p = buffer_get_string(&b, NULL);
912 if (strcmp(p, "hostbased") != 0)
915 buffer_skip_string(&b); /* pkalg */
916 buffer_skip_string(&b); /* pkblob */
918 /* verify client host, strip trailing dot if necessary */
919 p = buffer_get_string(&b, NULL);
920 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
922 if (strcmp(p, chost) != 0)
926 /* verify client user */
927 p = buffer_get_string(&b, NULL);
928 if (strcmp(p, cuser) != 0)
932 if (buffer_len(&b) != 0)
939 mm_answer_keyverify(int socket, Buffer *m)
942 u_char *signature, *data, *blob;
943 u_int signaturelen, datalen, bloblen;
947 blob = buffer_get_string(m, &bloblen);
948 signature = buffer_get_string(m, &signaturelen);
949 data = buffer_get_string(m, &datalen);
951 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
952 !monitor_allowed_key(blob, bloblen))
953 fatal("%s: bad key, not previously allowed", __func__);
955 key = key_from_blob(blob, bloblen);
957 fatal("%s: bad public key blob", __func__);
959 switch (key_blobtype) {
961 valid_data = monitor_valid_userblob(data, datalen);
964 valid_data = monitor_valid_hostbasedblob(data, datalen,
965 hostbased_cuser, hostbased_chost);
972 fatal("%s: bad signature data blob", __func__);
974 verified = key_verify(key, signature, signaturelen, data, datalen);
975 debug3("%s: key %p signature %s",
976 __func__, key, verified ? "verified" : "unverified");
983 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
985 monitor_reset_key_state();
988 buffer_put_int(m, verified);
989 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
995 mm_record_login(Session *s, struct passwd *pw)
998 struct sockaddr_storage from;
1001 * Get IP address of client. If the connection is not a socket, let
1002 * the address be 0.0.0.0.
1004 memset(&from, 0, sizeof(from));
1005 if (packet_connection_is_on_socket()) {
1006 fromlen = sizeof(from);
1007 if (getpeername(packet_get_connection_in(),
1008 (struct sockaddr *) & from, &fromlen) < 0) {
1009 debug("getpeername: %.100s", strerror(errno));
1013 /* Record that there was a login on that tty from the remote host. */
1014 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1015 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
1016 (struct sockaddr *)&from);
1020 mm_session_close(Session *s)
1022 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
1023 if (s->ttyfd != -1) {
1024 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1025 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1026 session_pty_cleanup2(s);
1032 mm_answer_pty(int socket, Buffer *m)
1034 extern struct monitor *pmonitor;
1038 debug3("%s entering", __func__);
1044 s->authctxt = authctxt;
1045 s->pw = authctxt->pw;
1046 s->pid = pmonitor->m_pid;
1047 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1050 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1051 pty_setowner(authctxt->pw, s->tty);
1053 buffer_put_int(m, 1);
1054 buffer_put_cstring(m, s->tty);
1055 mm_request_send(socket, MONITOR_ANS_PTY, m);
1057 mm_send_fd(socket, s->ptyfd);
1058 mm_send_fd(socket, s->ttyfd);
1060 /* We need to trick ttyslot */
1061 if (dup2(s->ttyfd, 0) == -1)
1062 fatal("%s: dup2", __func__);
1064 mm_record_login(s, authctxt->pw);
1066 /* Now we can close the file descriptor again */
1069 /* make sure nothing uses fd 0 */
1070 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1071 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1073 error("%s: fd0 %d != 0", __func__, fd0);
1075 /* slave is not needed */
1077 s->ttyfd = s->ptyfd;
1078 /* no need to dup() because nobody closes ptyfd */
1079 s->ptymaster = s->ptyfd;
1081 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1087 mm_session_close(s);
1088 buffer_put_int(m, 0);
1089 mm_request_send(socket, MONITOR_ANS_PTY, m);
1094 mm_answer_pty_cleanup(int socket, Buffer *m)
1099 debug3("%s entering", __func__);
1101 tty = buffer_get_string(m, NULL);
1102 if ((s = session_by_tty(tty)) != NULL)
1103 mm_session_close(s);
1110 mm_answer_sesskey(int socket, Buffer *m)
1115 /* Turn off permissions */
1116 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1118 if ((p = BN_new()) == NULL)
1119 fatal("%s: BN_new", __func__);
1121 buffer_get_bignum2(m, p);
1123 rsafail = ssh1_session_key(p);
1126 buffer_put_int(m, rsafail);
1127 buffer_put_bignum2(m, p);
1131 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1133 /* Turn on permissions for sessid passing */
1134 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1140 mm_answer_sessid(int socket, Buffer *m)
1144 debug3("%s entering", __func__);
1146 if (buffer_len(m) != 16)
1147 fatal("%s: bad ssh1 session id", __func__);
1148 for (i = 0; i < 16; i++)
1149 session_id[i] = buffer_get_char(m);
1151 /* Turn on permissions for getpwnam */
1152 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1158 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1162 u_char *blob = NULL;
1166 debug3("%s entering", __func__);
1168 if (options.rsa_authentication && authctxt->valid) {
1169 if ((client_n = BN_new()) == NULL)
1170 fatal("%s: BN_new", __func__);
1171 buffer_get_bignum2(m, client_n);
1172 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1173 BN_clear_free(client_n);
1176 buffer_put_int(m, allowed);
1178 /* clear temporarily storage (used by generate challenge) */
1179 monitor_reset_key_state();
1181 if (allowed && key != NULL) {
1182 key->type = KEY_RSA; /* cheat for key_to_blob */
1183 if (key_to_blob(key, &blob, &blen) == 0)
1184 fatal("%s: key_to_blob failed", __func__);
1185 buffer_put_string(m, blob, blen);
1187 /* Save temporarily for comparison in verify */
1190 key_blobtype = MM_RSAUSERKEY;
1196 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1198 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1199 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1204 mm_answer_rsa_challenge(int socket, Buffer *m)
1210 debug3("%s entering", __func__);
1212 if (!authctxt->valid)
1213 fatal("%s: authctxt not valid", __func__);
1214 blob = buffer_get_string(m, &blen);
1215 if (!monitor_allowed_key(blob, blen))
1216 fatal("%s: bad key, not previously allowed", __func__);
1217 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1218 fatal("%s: key type mismatch", __func__);
1219 if ((key = key_from_blob(blob, blen)) == NULL)
1220 fatal("%s: received bad key", __func__);
1223 BN_clear_free(ssh1_challenge);
1224 ssh1_challenge = auth_rsa_generate_challenge(key);
1227 buffer_put_bignum2(m, ssh1_challenge);
1229 debug3("%s sending reply", __func__);
1230 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1232 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1237 mm_answer_rsa_response(int socket, Buffer *m)
1240 u_char *blob, *response;
1244 debug3("%s entering", __func__);
1246 if (!authctxt->valid)
1247 fatal("%s: authctxt not valid", __func__);
1248 if (ssh1_challenge == NULL)
1249 fatal("%s: no ssh1_challenge", __func__);
1251 blob = buffer_get_string(m, &blen);
1252 if (!monitor_allowed_key(blob, blen))
1253 fatal("%s: bad key, not previously allowed", __func__);
1254 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1255 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1256 if ((key = key_from_blob(blob, blen)) == NULL)
1257 fatal("%s: received bad key", __func__);
1258 response = buffer_get_string(m, &len);
1260 fatal("%s: received bad response to challenge", __func__);
1261 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1266 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1269 BN_clear_free(ssh1_challenge);
1270 ssh1_challenge = NULL;
1271 monitor_reset_key_state();
1274 buffer_put_int(m, success);
1275 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1281 mm_answer_term(int socket, Buffer *req)
1283 extern struct monitor *pmonitor;
1286 debug3("%s: tearing down sessions", __func__);
1288 /* The child is terminating */
1289 session_destroy_all(&mm_session_close);
1291 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1295 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1297 /* Terminate process */
1302 monitor_apply_keystate(struct monitor *pmonitor)
1305 set_newkeys(MODE_IN);
1306 set_newkeys(MODE_OUT);
1308 packet_set_protocol_flags(child_state.ssh1protoflags);
1309 packet_set_encryption_key(child_state.ssh1key,
1310 child_state.ssh1keylen, child_state.ssh1cipher);
1311 xfree(child_state.ssh1key);
1314 /* for rc4 and other stateful ciphers */
1315 packet_set_keycontext(MODE_OUT, child_state.keyout);
1316 xfree(child_state.keyout);
1317 packet_set_keycontext(MODE_IN, child_state.keyin);
1318 xfree(child_state.keyin);
1321 packet_set_iv(MODE_OUT, child_state.ivout);
1322 xfree(child_state.ivout);
1323 packet_set_iv(MODE_IN, child_state.ivin);
1324 xfree(child_state.ivin);
1327 memcpy(&incoming_stream, &child_state.incoming,
1328 sizeof(incoming_stream));
1329 memcpy(&outgoing_stream, &child_state.outgoing,
1330 sizeof(outgoing_stream));
1332 /* Update with new address */
1333 if (options.compression)
1334 mm_init_compression(pmonitor->m_zlib);
1336 /* Network I/O buffers */
1337 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1338 buffer_clear(&input);
1339 buffer_append(&input, child_state.input, child_state.ilen);
1340 memset(child_state.input, 0, child_state.ilen);
1341 xfree(child_state.input);
1343 buffer_clear(&output);
1344 buffer_append(&output, child_state.output, child_state.olen);
1345 memset(child_state.output, 0, child_state.olen);
1346 xfree(child_state.output);
1350 mm_get_kex(Buffer *m)
1356 kex = xmalloc(sizeof(*kex));
1357 memset(kex, 0, sizeof(*kex));
1358 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1359 if ((session_id2 == NULL) ||
1360 (kex->session_id_len != session_id2_len) ||
1361 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1362 fatal("mm_get_get: internal error: bad session id");
1363 kex->we_need = buffer_get_int(m);
1365 kex->hostkey_type = buffer_get_int(m);
1366 kex->kex_type = buffer_get_int(m);
1367 blob = buffer_get_string(m, &bloblen);
1368 buffer_init(&kex->my);
1369 buffer_append(&kex->my, blob, bloblen);
1371 blob = buffer_get_string(m, &bloblen);
1372 buffer_init(&kex->peer);
1373 buffer_append(&kex->peer, blob, bloblen);
1376 kex->flags = buffer_get_int(m);
1377 kex->client_version_string = buffer_get_string(m, NULL);
1378 kex->server_version_string = buffer_get_string(m, NULL);
1379 kex->load_host_key=&get_hostkey_by_type;
1380 kex->host_key_index=&get_hostkey_index;
1385 /* This function requries careful sanity checking */
1388 mm_get_keystate(struct monitor *pmonitor)
1392 u_int bloblen, plen;
1394 debug3("%s: Waiting for new keys", __func__);
1397 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1399 child_state.ssh1protoflags = buffer_get_int(&m);
1400 child_state.ssh1cipher = buffer_get_int(&m);
1401 child_state.ssh1key = buffer_get_string(&m,
1402 &child_state.ssh1keylen);
1403 child_state.ivout = buffer_get_string(&m,
1404 &child_state.ivoutlen);
1405 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1408 /* Get the Kex for rekeying */
1409 *pmonitor->m_pkex = mm_get_kex(&m);
1412 blob = buffer_get_string(&m, &bloblen);
1413 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1416 debug3("%s: Waiting for second key", __func__);
1417 blob = buffer_get_string(&m, &bloblen);
1418 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1421 /* Now get sequence numbers for the packets */
1422 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1423 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1426 /* Get the key context */
1427 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1428 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1430 debug3("%s: Getting compression state", __func__);
1431 /* Get compression state */
1432 p = buffer_get_string(&m, &plen);
1433 if (plen != sizeof(child_state.outgoing))
1434 fatal("%s: bad request size", __func__);
1435 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1438 p = buffer_get_string(&m, &plen);
1439 if (plen != sizeof(child_state.incoming))
1440 fatal("%s: bad request size", __func__);
1441 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1444 /* Network I/O buffers */
1445 debug3("%s: Getting Network I/O buffers", __func__);
1446 child_state.input = buffer_get_string(&m, &child_state.ilen);
1447 child_state.output = buffer_get_string(&m, &child_state.olen);
1453 /* Allocation functions for zlib */
1455 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1457 size_t len = size * ncount;
1460 if (len == 0 || ncount > SIZE_T_MAX / size)
1461 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1463 address = mm_malloc(mm, len);
1469 mm_zfree(struct mm_master *mm, void *address)
1471 mm_free(mm, address);
1475 mm_init_compression(struct mm_master *mm)
1477 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1478 outgoing_stream.zfree = (free_func)mm_zfree;
1479 outgoing_stream.opaque = mm;
1481 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1482 incoming_stream.zfree = (free_func)mm_zfree;
1483 incoming_stream.opaque = mm;
1488 #define FD_CLOSEONEXEC(x) do { \
1489 if (fcntl(x, F_SETFD, 1) == -1) \
1490 fatal("fcntl(%d, F_SETFD)", x); \
1494 monitor_socketpair(int *pair)
1496 #ifdef HAVE_SOCKETPAIR
1497 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1498 fatal("%s: socketpair", __func__);
1500 fatal("%s: UsePrivilegeSeparation=yes not supported",
1503 FD_CLOSEONEXEC(pair[0]);
1504 FD_CLOSEONEXEC(pair[1]);
1507 #define MM_MEMSIZE 65536
1512 struct monitor *mon;
1515 mon = xmalloc(sizeof(*mon));
1517 monitor_socketpair(pair);
1519 mon->m_recvfd = pair[0];
1520 mon->m_sendfd = pair[1];
1522 /* Used to share zlib space across processes */
1523 if (options.compression) {
1524 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1525 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1527 /* Compression needs to share state across borders */
1528 mm_init_compression(mon->m_zlib);
1535 monitor_reinit(struct monitor *mon)
1539 monitor_socketpair(pair);
1541 mon->m_recvfd = pair[0];
1542 mon->m_sendfd = pair[1];