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.16 2002/06/21 05:50:51 djm Exp $");
30 #include <openssl/dh.h>
42 #include "auth-options.h"
51 #include "monitor_mm.h"
52 #include "monitor_wrap.h"
53 #include "monitor_fdpass.h"
63 extern ServerOptions options;
64 extern u_int utmp_len;
65 extern Newkeys *current_keys[];
66 extern z_stream incoming_stream;
67 extern z_stream outgoing_stream;
68 extern u_char session_id[];
69 extern Buffer input, output;
70 extern Buffer auth_debug;
71 extern int auth_debug_init;
73 /* State exported from the child */
96 /* Functions on the montior that answer unprivileged requests */
98 int mm_answer_moduli(int, Buffer *);
99 int mm_answer_sign(int, Buffer *);
100 int mm_answer_pwnamallow(int, Buffer *);
101 int mm_answer_auth2_read_banner(int, Buffer *);
102 int mm_answer_authserv(int, Buffer *);
103 int mm_answer_authpassword(int, Buffer *);
104 int mm_answer_bsdauthquery(int, Buffer *);
105 int mm_answer_bsdauthrespond(int, Buffer *);
106 int mm_answer_skeyquery(int, Buffer *);
107 int mm_answer_skeyrespond(int, Buffer *);
108 int mm_answer_keyallowed(int, Buffer *);
109 int mm_answer_keyverify(int, Buffer *);
110 int mm_answer_pty(int, Buffer *);
111 int mm_answer_pty_cleanup(int, Buffer *);
112 int mm_answer_term(int, Buffer *);
113 int mm_answer_rsa_keyallowed(int, Buffer *);
114 int mm_answer_rsa_challenge(int, Buffer *);
115 int mm_answer_rsa_response(int, Buffer *);
116 int mm_answer_sesskey(int, Buffer *);
117 int mm_answer_sessid(int, Buffer *);
120 int mm_answer_pam_start(int, Buffer *);
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_PAM_START, MON_ONCE, mm_answer_pam_start},
195 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
196 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
199 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
200 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
203 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
208 struct mon_table mon_dispatch_postauth15[] = {
209 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
210 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
211 {MONITOR_REQ_TERM, 0, mm_answer_term},
215 struct mon_table *mon_dispatch;
217 /* Specifies if a certain message is allowed at the moment */
220 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
222 while (ent->f != NULL) {
223 if (ent->type == type) {
224 ent->flags &= ~MON_PERMIT;
225 ent->flags |= permit ? MON_PERMIT : 0;
233 monitor_permit_authentications(int permit)
235 struct mon_table *ent = mon_dispatch;
237 while (ent->f != NULL) {
238 if (ent->flags & MON_AUTH) {
239 ent->flags &= ~MON_PERMIT;
240 ent->flags |= permit ? MON_PERMIT : 0;
247 monitor_child_preauth(struct monitor *pmonitor)
249 struct mon_table *ent;
250 int authenticated = 0;
252 debug3("preauth child monitor started");
255 mon_dispatch = mon_dispatch_proto20;
257 /* Permit requests for moduli and signatures */
258 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
259 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
261 mon_dispatch = mon_dispatch_proto15;
263 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
266 authctxt = authctxt_new();
268 /* The first few requests do not require asynchronous access */
269 while (!authenticated) {
270 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
272 if (!(ent->flags & MON_AUTHDECIDE))
273 fatal("%s: unexpected authentication from %d",
274 __func__, ent->type);
275 if (authctxt->pw->pw_uid == 0 &&
276 !auth_root_allowed(auth_method))
279 if (!do_pam_account(authctxt->pw->pw_name, NULL))
284 if (ent->flags & MON_AUTHDECIDE) {
285 auth_log(authctxt, authenticated, auth_method,
286 compat20 ? " ssh2" : "");
288 authctxt->failures++;
292 if (!authctxt->valid)
293 fatal("%s: authenticated invalid user", __func__);
295 debug("%s: %s has been authenticated by privileged process",
296 __func__, authctxt->user);
298 mm_get_keystate(pmonitor);
304 monitor_child_postauth(struct monitor *pmonitor)
307 mon_dispatch = mon_dispatch_postauth20;
309 /* Permit requests for moduli and signatures */
310 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
311 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
312 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
315 mon_dispatch = mon_dispatch_postauth15;
316 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
319 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
320 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
324 monitor_read(pmonitor, mon_dispatch, NULL);
328 monitor_sync(struct monitor *pmonitor)
330 if (options.compression) {
331 /* The member allocation is not visible, so sync it */
332 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
337 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
338 struct mon_table **pent)
346 mm_request_receive(pmonitor->m_sendfd, &m);
347 type = buffer_get_char(&m);
349 debug3("%s: checking request %d", __func__, type);
351 while (ent->f != NULL) {
352 if (ent->type == type)
357 if (ent->f != NULL) {
358 if (!(ent->flags & MON_PERMIT))
359 fatal("%s: unpermitted request %d", __func__,
361 ret = (*ent->f)(pmonitor->m_sendfd, &m);
364 /* The child may use this request only once, disable it */
365 if (ent->flags & MON_ONCE) {
366 debug2("%s: %d used once, disabling now", __func__,
368 ent->flags &= ~MON_PERMIT;
377 fatal("%s: unsupported request: %d", __func__, type);
383 /* allowed key state */
385 monitor_allowed_key(u_char *blob, u_int bloblen)
387 /* make sure key is allowed */
388 if (key_blob == NULL || key_bloblen != bloblen ||
389 memcmp(key_blob, blob, key_bloblen))
395 monitor_reset_key_state(void)
398 if (key_blob != NULL)
400 if (hostbased_cuser != NULL)
401 xfree(hostbased_cuser);
402 if (hostbased_chost != NULL)
403 xfree(hostbased_chost);
406 key_blobtype = MM_NOKEY;
407 hostbased_cuser = NULL;
408 hostbased_chost = NULL;
412 mm_answer_moduli(int socket, Buffer *m)
417 min = buffer_get_int(m);
418 want = buffer_get_int(m);
419 max = buffer_get_int(m);
421 debug3("%s: got parameters: %d %d %d",
422 __func__, min, want, max);
423 /* We need to check here, too, in case the child got corrupted */
424 if (max < min || want < min || max < want)
425 fatal("%s: bad parameters: %d %d %d",
426 __func__, min, want, max);
430 dh = choose_dh(min, want, max);
432 buffer_put_char(m, 0);
435 /* Send first bignum */
436 buffer_put_char(m, 1);
437 buffer_put_bignum2(m, dh->p);
438 buffer_put_bignum2(m, dh->g);
442 mm_request_send(socket, MONITOR_ANS_MODULI, m);
447 mm_answer_sign(int socket, Buffer *m)
452 u_int siglen, datlen;
455 debug3("%s", __func__);
457 keyid = buffer_get_int(m);
458 p = buffer_get_string(m, &datlen);
461 fatal("%s: data length incorrect: %d", __func__, datlen);
463 /* save session id, it will be passed on the first call */
464 if (session_id2_len == 0) {
465 session_id2_len = datlen;
466 session_id2 = xmalloc(session_id2_len);
467 memcpy(session_id2, p, session_id2_len);
470 if ((key = get_hostkey_by_index(keyid)) == NULL)
471 fatal("%s: no hostkey from index %d", __func__, keyid);
472 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
473 fatal("%s: key_sign failed", __func__);
475 debug3("%s: signature %p(%d)", __func__, signature, siglen);
478 buffer_put_string(m, signature, siglen);
483 mm_request_send(socket, MONITOR_ANS_SIGN, m);
485 /* Turn on permissions for getpwnam */
486 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
491 /* Retrieves the password entry and also checks if the user is permitted */
494 mm_answer_pwnamallow(int socket, Buffer *m)
497 struct passwd *pwent;
500 debug3("%s", __func__);
502 if (authctxt->attempt++ != 0)
503 fatal("%s: multiple attempts for getpwnam", __func__);
505 login = buffer_get_string(m, NULL);
507 pwent = getpwnamallow(login);
509 authctxt->user = xstrdup(login);
510 setproctitle("%s [priv]", pwent ? login : "unknown");
516 buffer_put_char(m, 0);
521 authctxt->pw = pwent;
524 buffer_put_char(m, 1);
525 buffer_put_string(m, pwent, sizeof(struct passwd));
526 buffer_put_cstring(m, pwent->pw_name);
527 buffer_put_cstring(m, "*");
528 buffer_put_cstring(m, pwent->pw_gecos);
529 #ifdef HAVE_PW_CLASS_IN_PASSWD
530 buffer_put_cstring(m, pwent->pw_class);
532 buffer_put_cstring(m, pwent->pw_dir);
533 buffer_put_cstring(m, pwent->pw_shell);
536 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
537 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
539 /* For SSHv1 allow authentication now */
541 monitor_permit_authentications(1);
543 /* Allow service/style information on the auth context */
544 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
545 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
549 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
555 int mm_answer_auth2_read_banner(int socket, Buffer *m)
560 banner = auth2_read_banner();
561 buffer_put_cstring(m, banner != NULL ? banner : "");
562 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
571 mm_answer_authserv(int socket, Buffer *m)
573 monitor_permit_authentications(1);
575 authctxt->service = buffer_get_string(m, NULL);
576 authctxt->style = buffer_get_string(m, NULL);
577 debug3("%s: service=%s, style=%s",
578 __func__, authctxt->service, authctxt->style);
580 if (strlen(authctxt->style) == 0) {
581 xfree(authctxt->style);
582 authctxt->style = NULL;
589 mm_answer_authpassword(int socket, Buffer *m)
591 static int call_count;
593 int authenticated, plen;
595 passwd = buffer_get_string(m, &plen);
596 /* Only authenticate if the context is valid */
597 authenticated = options.password_authentication &&
598 authctxt->valid && auth_password(authctxt, passwd);
599 memset(passwd, 0, strlen(passwd));
603 buffer_put_int(m, authenticated);
605 debug3("%s: sending result %d", __func__, authenticated);
606 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
609 if (plen == 0 && call_count == 1)
610 auth_method = "none";
612 auth_method = "password";
614 /* Causes monitor loop to terminate if authenticated */
615 return (authenticated);
620 mm_answer_bsdauthquery(int socket, Buffer *m)
622 char *name, *infotxt;
628 res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
632 buffer_put_int(m, res);
634 buffer_put_cstring(m, prompts[0]);
636 debug3("%s: sending challenge res: %d", __func__, res);
637 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
650 mm_answer_bsdauthrespond(int socket, Buffer *m)
655 if (authctxt->as == 0)
656 fatal("%s: no bsd auth session", __func__);
658 response = buffer_get_string(m, NULL);
659 authok = options.challenge_response_authentication &&
660 auth_userresponse(authctxt->as, response, 0);
662 debug3("%s: <%s> = <%d>", __func__, response, authok);
666 buffer_put_int(m, authok);
668 debug3("%s: sending authenticated: %d", __func__, authok);
669 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
671 auth_method = "bsdauth";
673 return (authok != 0);
679 mm_answer_skeyquery(int socket, Buffer *m)
682 char challenge[1024];
685 res = skeychallenge(&skey, authctxt->user, challenge);
688 buffer_put_int(m, res);
690 buffer_put_cstring(m, challenge);
692 debug3("%s: sending challenge res: %d", __func__, res);
693 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
699 mm_answer_skeyrespond(int socket, Buffer *m)
704 response = buffer_get_string(m, NULL);
706 authok = (options.challenge_response_authentication &&
708 skey_haskey(authctxt->pw->pw_name) == 0 &&
709 skey_passcheck(authctxt->pw->pw_name, response) != -1);
714 buffer_put_int(m, authok);
716 debug3("%s: sending authenticated: %d", __func__, authok);
717 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
719 auth_method = "skey";
721 return (authok != 0);
727 mm_answer_pam_start(int socket, Buffer *m)
731 user = buffer_get_string(m, NULL);
742 mm_append_debug(Buffer *m)
744 if (auth_debug_init && buffer_len(&auth_debug)) {
745 debug3("%s: Appending debug messages for child", __func__);
746 buffer_append(m, buffer_ptr(&auth_debug),
747 buffer_len(&auth_debug));
748 buffer_clear(&auth_debug);
753 mm_answer_keyallowed(int socket, Buffer *m)
756 u_char *cuser, *chost, *blob;
758 enum mm_keytype type = 0;
761 debug3("%s entering", __func__);
763 type = buffer_get_int(m);
764 cuser = buffer_get_string(m, NULL);
765 chost = buffer_get_string(m, NULL);
766 blob = buffer_get_string(m, &bloblen);
768 key = key_from_blob(blob, bloblen);
770 if ((compat20 && type == MM_RSAHOSTKEY) ||
771 (!compat20 && type != MM_RSAHOSTKEY))
772 fatal("%s: key type and protocol mismatch", __func__);
774 debug3("%s: key_from_blob: %p", __func__, key);
776 if (key != NULL && authctxt->pw != NULL) {
779 allowed = options.pubkey_authentication &&
780 user_key_allowed(authctxt->pw, key);
783 allowed = options.hostbased_authentication &&
784 hostbased_key_allowed(authctxt->pw,
788 key->type = KEY_RSA1; /* XXX */
789 allowed = options.rhosts_rsa_authentication &&
790 auth_rhosts_rsa_key_allowed(authctxt->pw,
794 fatal("%s: unknown key type %d", __func__, type);
800 /* clear temporarily storage (used by verify) */
801 monitor_reset_key_state();
804 /* Save temporarily for comparison in verify */
806 key_bloblen = bloblen;
808 hostbased_cuser = cuser;
809 hostbased_chost = chost;
812 debug3("%s: key %p is %s",
813 __func__, key, allowed ? "allowed" : "disallowed");
816 buffer_put_int(m, allowed);
820 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
822 if (type == MM_RSAHOSTKEY)
823 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
829 monitor_valid_userblob(u_char *data, u_int datalen)
837 buffer_append(&b, data, datalen);
839 if (datafellows & SSH_OLD_SESSIONID) {
841 len = buffer_len(&b);
842 if ((session_id2 == NULL) ||
843 (len < session_id2_len) ||
844 (memcmp(p, session_id2, session_id2_len) != 0))
846 buffer_consume(&b, session_id2_len);
848 p = buffer_get_string(&b, &len);
849 if ((session_id2 == NULL) ||
850 (len != session_id2_len) ||
851 (memcmp(p, session_id2, session_id2_len) != 0))
855 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
857 p = buffer_get_string(&b, NULL);
858 if (strcmp(authctxt->user, p) != 0) {
859 log("wrong user name passed to monitor: expected %s != %.100s",
864 buffer_skip_string(&b);
865 if (datafellows & SSH_BUG_PKAUTH) {
866 if (!buffer_get_char(&b))
869 p = buffer_get_string(&b, NULL);
870 if (strcmp("publickey", p) != 0)
873 if (!buffer_get_char(&b))
875 buffer_skip_string(&b);
877 buffer_skip_string(&b);
878 if (buffer_len(&b) != 0)
885 monitor_valid_hostbasedblob(u_char *data, u_int datalen, u_char *cuser,
894 buffer_append(&b, data, datalen);
896 p = buffer_get_string(&b, &len);
897 if ((session_id2 == NULL) ||
898 (len != session_id2_len) ||
899 (memcmp(p, session_id2, session_id2_len) != 0))
903 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
905 p = buffer_get_string(&b, NULL);
906 if (strcmp(authctxt->user, p) != 0) {
907 log("wrong user name passed to monitor: expected %s != %.100s",
912 buffer_skip_string(&b); /* service */
913 p = buffer_get_string(&b, NULL);
914 if (strcmp(p, "hostbased") != 0)
917 buffer_skip_string(&b); /* pkalg */
918 buffer_skip_string(&b); /* pkblob */
920 /* verify client host, strip trailing dot if necessary */
921 p = buffer_get_string(&b, NULL);
922 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
924 if (strcmp(p, chost) != 0)
928 /* verify client user */
929 p = buffer_get_string(&b, NULL);
930 if (strcmp(p, cuser) != 0)
934 if (buffer_len(&b) != 0)
941 mm_answer_keyverify(int socket, Buffer *m)
944 u_char *signature, *data, *blob;
945 u_int signaturelen, datalen, bloblen;
949 blob = buffer_get_string(m, &bloblen);
950 signature = buffer_get_string(m, &signaturelen);
951 data = buffer_get_string(m, &datalen);
953 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
954 !monitor_allowed_key(blob, bloblen))
955 fatal("%s: bad key, not previously allowed", __func__);
957 key = key_from_blob(blob, bloblen);
959 fatal("%s: bad public key blob", __func__);
961 switch (key_blobtype) {
963 valid_data = monitor_valid_userblob(data, datalen);
966 valid_data = monitor_valid_hostbasedblob(data, datalen,
967 hostbased_cuser, hostbased_chost);
974 fatal("%s: bad signature data blob", __func__);
976 verified = key_verify(key, signature, signaturelen, data, datalen);
977 debug3("%s: key %p signature %s",
978 __func__, key, verified ? "verified" : "unverified");
985 monitor_reset_key_state();
988 buffer_put_int(m, verified);
989 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
991 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
997 mm_record_login(Session *s, struct passwd *pw)
1000 struct sockaddr_storage from;
1003 * Get IP address of client. If the connection is not a socket, let
1004 * the address be 0.0.0.0.
1006 memset(&from, 0, sizeof(from));
1007 if (packet_connection_is_on_socket()) {
1008 fromlen = sizeof(from);
1009 if (getpeername(packet_get_connection_in(),
1010 (struct sockaddr *) & from, &fromlen) < 0) {
1011 debug("getpeername: %.100s", strerror(errno));
1015 /* Record that there was a login on that tty from the remote host. */
1016 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1017 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
1018 (struct sockaddr *)&from);
1022 mm_session_close(Session *s)
1024 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
1025 if (s->ttyfd != -1) {
1026 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1027 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1028 session_pty_cleanup2(s);
1034 mm_answer_pty(int socket, Buffer *m)
1036 extern struct monitor *pmonitor;
1040 debug3("%s entering", __func__);
1046 s->authctxt = authctxt;
1047 s->pw = authctxt->pw;
1048 s->pid = pmonitor->m_pid;
1049 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1052 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1053 pty_setowner(authctxt->pw, s->tty);
1055 buffer_put_int(m, 1);
1056 buffer_put_cstring(m, s->tty);
1057 mm_request_send(socket, MONITOR_ANS_PTY, m);
1059 mm_send_fd(socket, s->ptyfd);
1060 mm_send_fd(socket, s->ttyfd);
1062 /* We need to trick ttyslot */
1063 if (dup2(s->ttyfd, 0) == -1)
1064 fatal("%s: dup2", __func__);
1066 mm_record_login(s, authctxt->pw);
1068 /* Now we can close the file descriptor again */
1071 /* make sure nothing uses fd 0 */
1072 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1073 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1075 error("%s: fd0 %d != 0", __func__, fd0);
1077 /* slave is not needed */
1079 s->ttyfd = s->ptyfd;
1080 /* no need to dup() because nobody closes ptyfd */
1081 s->ptymaster = s->ptyfd;
1083 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1089 mm_session_close(s);
1090 buffer_put_int(m, 0);
1091 mm_request_send(socket, MONITOR_ANS_PTY, m);
1096 mm_answer_pty_cleanup(int socket, Buffer *m)
1101 debug3("%s entering", __func__);
1103 tty = buffer_get_string(m, NULL);
1104 if ((s = session_by_tty(tty)) != NULL)
1105 mm_session_close(s);
1112 mm_answer_sesskey(int socket, Buffer *m)
1117 /* Turn off permissions */
1118 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1120 if ((p = BN_new()) == NULL)
1121 fatal("%s: BN_new", __func__);
1123 buffer_get_bignum2(m, p);
1125 rsafail = ssh1_session_key(p);
1128 buffer_put_int(m, rsafail);
1129 buffer_put_bignum2(m, p);
1133 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1135 /* Turn on permissions for sessid passing */
1136 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1142 mm_answer_sessid(int socket, Buffer *m)
1146 debug3("%s entering", __func__);
1148 if (buffer_len(m) != 16)
1149 fatal("%s: bad ssh1 session id", __func__);
1150 for (i = 0; i < 16; i++)
1151 session_id[i] = buffer_get_char(m);
1153 /* Turn on permissions for getpwnam */
1154 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1160 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1164 u_char *blob = NULL;
1168 debug3("%s entering", __func__);
1170 if (options.rsa_authentication && authctxt->valid) {
1171 if ((client_n = BN_new()) == NULL)
1172 fatal("%s: BN_new", __func__);
1173 buffer_get_bignum2(m, client_n);
1174 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1175 BN_clear_free(client_n);
1178 buffer_put_int(m, allowed);
1180 /* clear temporarily storage (used by generate challenge) */
1181 monitor_reset_key_state();
1183 if (allowed && key != NULL) {
1184 key->type = KEY_RSA; /* cheat for key_to_blob */
1185 if (key_to_blob(key, &blob, &blen) == 0)
1186 fatal("%s: key_to_blob failed", __func__);
1187 buffer_put_string(m, blob, blen);
1189 /* Save temporarily for comparison in verify */
1192 key_blobtype = MM_RSAUSERKEY;
1198 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1200 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1201 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1206 mm_answer_rsa_challenge(int socket, Buffer *m)
1212 debug3("%s entering", __func__);
1214 if (!authctxt->valid)
1215 fatal("%s: authctxt not valid", __func__);
1216 blob = buffer_get_string(m, &blen);
1217 if (!monitor_allowed_key(blob, blen))
1218 fatal("%s: bad key, not previously allowed", __func__);
1219 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1220 fatal("%s: key type mismatch", __func__);
1221 if ((key = key_from_blob(blob, blen)) == NULL)
1222 fatal("%s: received bad key", __func__);
1225 BN_clear_free(ssh1_challenge);
1226 ssh1_challenge = auth_rsa_generate_challenge(key);
1229 buffer_put_bignum2(m, ssh1_challenge);
1231 debug3("%s sending reply", __func__);
1232 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1234 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1239 mm_answer_rsa_response(int socket, Buffer *m)
1242 u_char *blob, *response;
1246 debug3("%s entering", __func__);
1248 if (!authctxt->valid)
1249 fatal("%s: authctxt not valid", __func__);
1250 if (ssh1_challenge == NULL)
1251 fatal("%s: no ssh1_challenge", __func__);
1253 blob = buffer_get_string(m, &blen);
1254 if (!monitor_allowed_key(blob, blen))
1255 fatal("%s: bad key, not previously allowed", __func__);
1256 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1257 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1258 if ((key = key_from_blob(blob, blen)) == NULL)
1259 fatal("%s: received bad key", __func__);
1260 response = buffer_get_string(m, &len);
1262 fatal("%s: received bad response to challenge", __func__);
1263 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1268 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1271 BN_clear_free(ssh1_challenge);
1272 ssh1_challenge = NULL;
1273 monitor_reset_key_state();
1276 buffer_put_int(m, success);
1277 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1283 mm_answer_term(int socket, Buffer *req)
1285 extern struct monitor *pmonitor;
1288 debug3("%s: tearing down sessions", __func__);
1290 /* The child is terminating */
1291 session_destroy_all(&mm_session_close);
1293 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1297 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1299 /* Terminate process */
1304 monitor_apply_keystate(struct monitor *pmonitor)
1307 set_newkeys(MODE_IN);
1308 set_newkeys(MODE_OUT);
1310 packet_set_protocol_flags(child_state.ssh1protoflags);
1311 packet_set_encryption_key(child_state.ssh1key,
1312 child_state.ssh1keylen, child_state.ssh1cipher);
1313 xfree(child_state.ssh1key);
1316 /* for rc4 and other stateful ciphers */
1317 packet_set_keycontext(MODE_OUT, child_state.keyout);
1318 xfree(child_state.keyout);
1319 packet_set_keycontext(MODE_IN, child_state.keyin);
1320 xfree(child_state.keyin);
1323 packet_set_iv(MODE_OUT, child_state.ivout);
1324 xfree(child_state.ivout);
1325 packet_set_iv(MODE_IN, child_state.ivin);
1326 xfree(child_state.ivin);
1329 memcpy(&incoming_stream, &child_state.incoming,
1330 sizeof(incoming_stream));
1331 memcpy(&outgoing_stream, &child_state.outgoing,
1332 sizeof(outgoing_stream));
1334 /* Update with new address */
1335 if (options.compression)
1336 mm_init_compression(pmonitor->m_zlib);
1338 /* Network I/O buffers */
1339 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1340 buffer_clear(&input);
1341 buffer_append(&input, child_state.input, child_state.ilen);
1342 memset(child_state.input, 0, child_state.ilen);
1343 xfree(child_state.input);
1345 buffer_clear(&output);
1346 buffer_append(&output, child_state.output, child_state.olen);
1347 memset(child_state.output, 0, child_state.olen);
1348 xfree(child_state.output);
1352 mm_get_kex(Buffer *m)
1358 kex = xmalloc(sizeof(*kex));
1359 memset(kex, 0, sizeof(*kex));
1360 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1361 if ((session_id2 == NULL) ||
1362 (kex->session_id_len != session_id2_len) ||
1363 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1364 fatal("mm_get_get: internal error: bad session id");
1365 kex->we_need = buffer_get_int(m);
1367 kex->hostkey_type = buffer_get_int(m);
1368 kex->kex_type = buffer_get_int(m);
1369 blob = buffer_get_string(m, &bloblen);
1370 buffer_init(&kex->my);
1371 buffer_append(&kex->my, blob, bloblen);
1373 blob = buffer_get_string(m, &bloblen);
1374 buffer_init(&kex->peer);
1375 buffer_append(&kex->peer, blob, bloblen);
1378 kex->flags = buffer_get_int(m);
1379 kex->client_version_string = buffer_get_string(m, NULL);
1380 kex->server_version_string = buffer_get_string(m, NULL);
1381 kex->load_host_key=&get_hostkey_by_type;
1382 kex->host_key_index=&get_hostkey_index;
1387 /* This function requries careful sanity checking */
1390 mm_get_keystate(struct monitor *pmonitor)
1394 u_int bloblen, plen;
1396 debug3("%s: Waiting for new keys", __func__);
1399 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1401 child_state.ssh1protoflags = buffer_get_int(&m);
1402 child_state.ssh1cipher = buffer_get_int(&m);
1403 child_state.ssh1key = buffer_get_string(&m,
1404 &child_state.ssh1keylen);
1405 child_state.ivout = buffer_get_string(&m,
1406 &child_state.ivoutlen);
1407 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1410 /* Get the Kex for rekeying */
1411 *pmonitor->m_pkex = mm_get_kex(&m);
1414 blob = buffer_get_string(&m, &bloblen);
1415 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1418 debug3("%s: Waiting for second key", __func__);
1419 blob = buffer_get_string(&m, &bloblen);
1420 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1423 /* Now get sequence numbers for the packets */
1424 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1425 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1428 /* Get the key context */
1429 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1430 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1432 debug3("%s: Getting compression state", __func__);
1433 /* Get compression state */
1434 p = buffer_get_string(&m, &plen);
1435 if (plen != sizeof(child_state.outgoing))
1436 fatal("%s: bad request size", __func__);
1437 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1440 p = buffer_get_string(&m, &plen);
1441 if (plen != sizeof(child_state.incoming))
1442 fatal("%s: bad request size", __func__);
1443 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1446 /* Network I/O buffers */
1447 debug3("%s: Getting Network I/O buffers", __func__);
1448 child_state.input = buffer_get_string(&m, &child_state.ilen);
1449 child_state.output = buffer_get_string(&m, &child_state.olen);
1455 /* Allocation functions for zlib */
1457 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1461 address = mm_malloc(mm, size * ncount);
1467 mm_zfree(struct mm_master *mm, void *address)
1469 mm_free(mm, address);
1473 mm_init_compression(struct mm_master *mm)
1475 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1476 outgoing_stream.zfree = (free_func)mm_zfree;
1477 outgoing_stream.opaque = mm;
1479 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1480 incoming_stream.zfree = (free_func)mm_zfree;
1481 incoming_stream.opaque = mm;
1486 #define FD_CLOSEONEXEC(x) do { \
1487 if (fcntl(x, F_SETFD, 1) == -1) \
1488 fatal("fcntl(%d, F_SETFD)", x); \
1492 monitor_socketpair(int *pair)
1494 #ifdef HAVE_SOCKETPAIR
1495 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1496 fatal("%s: socketpair", __func__);
1498 fatal("%s: UsePrivilegeSeparation=yes not supported",
1501 FD_CLOSEONEXEC(pair[0]);
1502 FD_CLOSEONEXEC(pair[1]);
1505 #define MM_MEMSIZE 65536
1510 struct monitor *mon;
1513 mon = xmalloc(sizeof(*mon));
1515 monitor_socketpair(pair);
1517 mon->m_recvfd = pair[0];
1518 mon->m_sendfd = pair[1];
1520 /* Used to share zlib space across processes */
1521 if (options.compression) {
1522 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1523 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1525 /* Compression needs to share state across borders */
1526 mm_init_compression(mon->m_zlib);
1533 monitor_reinit(struct monitor *mon)
1537 monitor_socketpair(pair);
1539 mon->m_recvfd = pair[0];
1540 mon->m_sendfd = pair[1];