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.9 2002/03/30 18:51:15 markus Exp $");
30 #include <openssl/dh.h>
42 #include "auth-options.h"
51 #include "monitor_mm.h"
52 #include "monitor_wrap.h"
53 #include "monitor_fdpass.h"
63 extern ServerOptions options;
64 extern u_int utmp_len;
65 extern Newkeys *current_keys[];
66 extern z_stream incoming_stream;
67 extern z_stream outgoing_stream;
68 extern u_char session_id[];
69 extern Buffer input, output;
70 extern Buffer auth_debug;
71 extern int auth_debug_init;
73 /* State exported from the child */
94 /* Functions on the montior that answer unprivileged requests */
96 int mm_answer_moduli(int, Buffer *);
97 int mm_answer_sign(int, Buffer *);
98 int mm_answer_pwnamallow(int, Buffer *);
99 int mm_answer_authserv(int, Buffer *);
100 int mm_answer_authpassword(int, Buffer *);
101 int mm_answer_bsdauthquery(int, Buffer *);
102 int mm_answer_bsdauthrespond(int, Buffer *);
103 int mm_answer_skeyquery(int, Buffer *);
104 int mm_answer_skeyrespond(int, Buffer *);
105 int mm_answer_keyallowed(int, Buffer *);
106 int mm_answer_keyverify(int, Buffer *);
107 int mm_answer_pty(int, Buffer *);
108 int mm_answer_pty_cleanup(int, Buffer *);
109 int mm_answer_term(int, Buffer *);
110 int mm_answer_rsa_keyallowed(int, Buffer *);
111 int mm_answer_rsa_challenge(int, Buffer *);
112 int mm_answer_rsa_response(int, Buffer *);
113 int mm_answer_sesskey(int, Buffer *);
114 int mm_answer_sessid(int, Buffer *);
116 static Authctxt *authctxt;
117 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
119 /* local state for key verify */
120 static u_char *key_blob = NULL;
121 static u_int key_bloblen = 0;
122 static int key_blobtype = MM_NOKEY;
123 static u_char *hostbased_cuser = NULL;
124 static u_char *hostbased_chost = NULL;
125 static char *auth_method = "unknown";
128 enum monitor_reqtype type;
130 int (*f)(int, Buffer *);
133 #define MON_ISAUTH 0x0004 /* Required for Authentication */
134 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
135 #define MON_ONCE 0x0010 /* Disable after calling */
137 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
139 #define MON_PERMIT 0x1000 /* Request is permitted */
141 struct mon_table mon_dispatch_proto20[] = {
142 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
143 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
144 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
145 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
146 #if !defined(USE_PAM)
147 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
150 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
151 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
154 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
155 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
157 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
158 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
162 struct mon_table mon_dispatch_postauth20[] = {
163 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
164 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
165 {MONITOR_REQ_PTY, 0, mm_answer_pty},
166 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
167 {MONITOR_REQ_TERM, 0, mm_answer_term},
171 struct mon_table mon_dispatch_proto15[] = {
172 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
173 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
174 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
175 #if !defined(USE_PAM)
176 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
178 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
179 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
180 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
181 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
183 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
184 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
187 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
188 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
193 struct mon_table mon_dispatch_postauth15[] = {
194 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
195 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
196 {MONITOR_REQ_TERM, 0, mm_answer_term},
200 struct mon_table *mon_dispatch;
202 /* Specifies if a certain message is allowed at the moment */
205 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
207 while (ent->f != NULL) {
208 if (ent->type == type) {
209 ent->flags &= ~MON_PERMIT;
210 ent->flags |= permit ? MON_PERMIT : 0;
218 monitor_permit_authentications(int permit)
220 struct mon_table *ent = mon_dispatch;
222 while (ent->f != NULL) {
223 if (ent->flags & MON_AUTH) {
224 ent->flags &= ~MON_PERMIT;
225 ent->flags |= permit ? MON_PERMIT : 0;
232 monitor_child_preauth(struct monitor *monitor)
234 struct mon_table *ent;
235 int authenticated = 0;
237 debug3("preauth child monitor started");
240 mon_dispatch = mon_dispatch_proto20;
242 /* Permit requests for moduli and signatures */
243 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
244 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
246 mon_dispatch = mon_dispatch_proto15;
248 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
251 authctxt = authctxt_new();
253 /* The first few requests do not require asynchronous access */
254 while (!authenticated) {
255 authenticated = monitor_read(monitor, mon_dispatch, &ent);
257 if (!(ent->flags & MON_AUTHDECIDE))
258 fatal("%s: unexpected authentication from %d",
259 __FUNCTION__, ent->type);
260 if (authctxt->pw->pw_uid == 0 &&
261 !auth_root_allowed(auth_method))
265 if (ent->flags & MON_AUTHDECIDE) {
266 auth_log(authctxt, authenticated, auth_method,
267 compat20 ? " ssh2" : "");
269 authctxt->failures++;
273 if (!authctxt->valid)
274 fatal("%s: authenticated invalid user", __FUNCTION__);
276 debug("%s: %s has been authenticated by privileged process",
277 __FUNCTION__, authctxt->user);
279 mm_get_keystate(monitor);
285 monitor_child_postauth(struct monitor *monitor)
288 mon_dispatch = mon_dispatch_postauth20;
290 /* Permit requests for moduli and signatures */
291 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
292 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
293 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
296 mon_dispatch = mon_dispatch_postauth15;
297 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
300 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
301 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
305 monitor_read(monitor, mon_dispatch, NULL);
309 monitor_sync(struct monitor *monitor)
311 /* The member allocation is not visible, so sync it */
312 mm_share_sync(&monitor->m_zlib, &monitor->m_zback);
316 monitor_read(struct monitor *monitor, struct mon_table *ent,
317 struct mon_table **pent)
325 mm_request_receive(monitor->m_sendfd, &m);
326 type = buffer_get_char(&m);
328 debug3("%s: checking request %d", __FUNCTION__, type);
330 while (ent->f != NULL) {
331 if (ent->type == type)
336 if (ent->f != NULL) {
337 if (!(ent->flags & MON_PERMIT))
338 fatal("%s: unpermitted request %d", __FUNCTION__,
340 ret = (*ent->f)(monitor->m_sendfd, &m);
343 /* The child may use this request only once, disable it */
344 if (ent->flags & MON_ONCE) {
345 debug2("%s: %d used once, disabling now", __FUNCTION__,
347 ent->flags &= ~MON_PERMIT;
356 fatal("%s: unsupported request: %d", __FUNCTION__, type);
362 /* allowed key state */
364 monitor_allowed_key(u_char *blob, u_int bloblen)
366 /* make sure key is allowed */
367 if (key_blob == NULL || key_bloblen != bloblen ||
368 memcmp(key_blob, blob, key_bloblen))
374 monitor_reset_key_state(void)
377 if (key_blob != NULL)
379 if (hostbased_cuser != NULL)
380 xfree(hostbased_cuser);
381 if (hostbased_chost != NULL)
382 xfree(hostbased_chost);
385 key_blobtype = MM_NOKEY;
386 hostbased_cuser = NULL;
387 hostbased_chost = NULL;
391 mm_answer_moduli(int socket, Buffer *m)
396 min = buffer_get_int(m);
397 want = buffer_get_int(m);
398 max = buffer_get_int(m);
400 debug3("%s: got parameters: %d %d %d",
401 __FUNCTION__, min, want, max);
402 /* We need to check here, too, in case the child got corrupted */
403 if (max < min || want < min || max < want)
404 fatal("%s: bad parameters: %d %d %d",
405 __FUNCTION__, min, want, max);
409 dh = choose_dh(min, want, max);
411 buffer_put_char(m, 0);
414 /* Send first bignum */
415 buffer_put_char(m, 1);
416 buffer_put_bignum2(m, dh->p);
417 buffer_put_bignum2(m, dh->g);
421 mm_request_send(socket, MONITOR_ANS_MODULI, m);
426 mm_answer_sign(int socket, Buffer *m)
431 u_int siglen, datlen;
434 debug3("%s", __FUNCTION__);
436 keyid = buffer_get_int(m);
437 p = buffer_get_string(m, &datlen);
440 fatal("%s: data length incorrect: %d", __FUNCTION__, datlen);
442 if ((key = get_hostkey_by_index(keyid)) == NULL)
443 fatal("%s: no hostkey from index %d", __FUNCTION__, keyid);
444 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
445 fatal("%s: key_sign failed", __FUNCTION__);
447 debug3("%s: signature %p(%d)", __FUNCTION__, signature, siglen);
450 buffer_put_string(m, signature, siglen);
455 mm_request_send(socket, MONITOR_ANS_SIGN, m);
457 /* Turn on permissions for getpwnam */
458 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
463 /* Retrieves the password entry and also checks if the user is permitted */
466 mm_answer_pwnamallow(int socket, Buffer *m)
469 struct passwd *pwent;
472 debug3("%s", __FUNCTION__);
474 if (authctxt->attempt++ != 0)
475 fatal("%s: multiple attempts for getpwnam", __FUNCTION__);
477 login = buffer_get_string(m, NULL);
479 pwent = getpwnamallow(login);
481 authctxt->user = xstrdup(login);
482 setproctitle("%s [priv]", pwent ? login : "unknown");
488 buffer_put_char(m, 0);
493 authctxt->pw = pwent;
496 buffer_put_char(m, 1);
497 buffer_put_string(m, pwent, sizeof(struct passwd));
498 buffer_put_cstring(m, pwent->pw_name);
499 buffer_put_cstring(m, "*");
500 buffer_put_cstring(m, pwent->pw_gecos);
501 #ifdef HAVE_PW_CLASS_IN_PASSWD
502 buffer_put_cstring(m, pwent->pw_class);
504 buffer_put_cstring(m, pwent->pw_dir);
505 buffer_put_cstring(m, pwent->pw_shell);
508 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __FUNCTION__, allowed);
509 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
511 /* For SSHv1 allow authentication now */
513 monitor_permit_authentications(1);
515 /* Allow service/style information on the auth context */
516 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
523 mm_answer_authserv(int socket, Buffer *m)
525 monitor_permit_authentications(1);
527 authctxt->service = buffer_get_string(m, NULL);
528 authctxt->style = buffer_get_string(m, NULL);
529 debug3("%s: service=%s, style=%s",
530 __FUNCTION__, authctxt->service, authctxt->style);
532 if (strlen(authctxt->style) == 0) {
533 xfree(authctxt->style);
534 authctxt->style = NULL;
540 #if !defined(USE_PAM)
542 mm_answer_authpassword(int socket, Buffer *m)
544 static int call_count;
546 int authenticated, plen;
548 passwd = buffer_get_string(m, &plen);
549 /* Only authenticate if the context is valid */
550 authenticated = authctxt->valid && auth_password(authctxt, passwd);
551 memset(passwd, 0, strlen(passwd));
555 buffer_put_int(m, authenticated);
557 debug3("%s: sending result %d", __FUNCTION__, authenticated);
558 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
561 if (plen == 0 && call_count == 1)
562 auth_method = "none";
564 auth_method = "password";
566 /* Causes monitor loop to terminate if authenticated */
567 return (authenticated);
573 mm_answer_bsdauthquery(int socket, Buffer *m)
575 char *name, *infotxt;
581 res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
585 buffer_put_int(m, res);
587 buffer_put_cstring(m, prompts[0]);
589 debug3("%s: sending challenge res: %d", __FUNCTION__, res);
590 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
603 mm_answer_bsdauthrespond(int socket, Buffer *m)
608 if (authctxt->as == 0)
609 fatal("%s: no bsd auth session", __FUNCTION__);
611 response = buffer_get_string(m, NULL);
612 authok = auth_userresponse(authctxt->as, response, 0);
614 debug3("%s: <%s> = <%d>", __FUNCTION__, response, authok);
618 buffer_put_int(m, authok);
620 debug3("%s: sending authenticated: %d", __FUNCTION__, authok);
621 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
623 auth_method = "bsdauth";
625 return (authok != 0);
631 mm_answer_skeyquery(int socket, Buffer *m)
634 char challenge[1024];
637 res = skeychallenge(&skey, authctxt->user, challenge);
640 buffer_put_int(m, res);
642 buffer_put_cstring(m, challenge);
644 debug3("%s: sending challenge res: %d", __FUNCTION__, res);
645 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
651 mm_answer_skeyrespond(int socket, Buffer *m)
656 response = buffer_get_string(m, NULL);
658 authok = (authctxt->valid &&
659 skey_haskey(authctxt->pw->pw_name) == 0 &&
660 skey_passcheck(authctxt->pw->pw_name, response) != -1);
665 buffer_put_int(m, authok);
667 debug3("%s: sending authenticated: %d", __FUNCTION__, authok);
668 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
670 auth_method = "skey";
672 return (authok != 0);
677 mm_append_debug(Buffer *m)
679 if (auth_debug_init && buffer_len(&auth_debug)) {
680 debug3("%s: Appending debug messages for child", __FUNCTION__);
681 buffer_append(m, buffer_ptr(&auth_debug),
682 buffer_len(&auth_debug));
683 buffer_clear(&auth_debug);
688 mm_answer_keyallowed(int socket, Buffer *m)
691 u_char *cuser, *chost, *blob;
693 enum mm_keytype type = 0;
696 debug3("%s entering", __FUNCTION__);
698 type = buffer_get_int(m);
699 cuser = buffer_get_string(m, NULL);
700 chost = buffer_get_string(m, NULL);
701 blob = buffer_get_string(m, &bloblen);
703 key = key_from_blob(blob, bloblen);
705 if ((compat20 && type == MM_RSAHOSTKEY) ||
706 (!compat20 && type != MM_RSAHOSTKEY))
707 fatal("%s: key type and protocol mismatch", __FUNCTION__);
709 debug3("%s: key_from_blob: %p", __FUNCTION__, key);
711 if (key != NULL && authctxt->pw != NULL) {
714 allowed = user_key_allowed(authctxt->pw, key);
717 allowed = hostbased_key_allowed(authctxt->pw,
721 key->type = KEY_RSA1; /* XXX */
722 allowed = auth_rhosts_rsa_key_allowed(authctxt->pw,
726 fatal("%s: unknown key type %d", __FUNCTION__, type);
732 /* clear temporarily storage (used by verify) */
733 monitor_reset_key_state();
736 /* Save temporarily for comparison in verify */
738 key_bloblen = bloblen;
740 hostbased_cuser = cuser;
741 hostbased_chost = chost;
744 debug3("%s: key %p is %s",
745 __FUNCTION__, key, allowed ? "allowed" : "disallowed");
748 buffer_put_int(m, allowed);
752 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
754 if (type == MM_RSAHOSTKEY)
755 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
761 monitor_valid_userblob(u_char *data, u_int datalen)
767 int session_id2_len = 20 /*XXX should get from [net] */;
770 buffer_append(&b, data, datalen);
772 if (datafellows & SSH_OLD_SESSIONID) {
773 buffer_consume(&b, session_id2_len);
775 xfree(buffer_get_string(&b, &len));
776 if (len != session_id2_len)
779 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
781 p = buffer_get_string(&b, NULL);
782 if (strcmp(authctxt->user, p) != 0) {
783 log("wrong user name passed to monitor: expected %s != %.100s",
788 buffer_skip_string(&b);
789 if (datafellows & SSH_BUG_PKAUTH) {
790 if (!buffer_get_char(&b))
793 p = buffer_get_string(&b, NULL);
794 if (strcmp("publickey", p) != 0)
797 if (!buffer_get_char(&b))
799 buffer_skip_string(&b);
801 buffer_skip_string(&b);
802 if (buffer_len(&b) != 0)
809 monitor_valid_hostbasedblob(u_char *data, u_int datalen, u_char *cuser,
816 int session_id2_len = 20 /*XXX should get from [net] */;
819 buffer_append(&b, data, datalen);
821 xfree(buffer_get_string(&b, &len));
822 if (len != session_id2_len)
824 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
826 p = buffer_get_string(&b, NULL);
827 if (strcmp(authctxt->user, p) != 0) {
828 log("wrong user name passed to monitor: expected %s != %.100s",
833 buffer_skip_string(&b); /* service */
834 p = buffer_get_string(&b, NULL);
835 if (strcmp(p, "hostbased") != 0)
838 buffer_skip_string(&b); /* pkalg */
839 buffer_skip_string(&b); /* pkblob */
841 /* verify client host, strip trailing dot if necessary */
842 p = buffer_get_string(&b, NULL);
843 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
845 if (strcmp(p, chost) != 0)
849 /* verify client user */
850 p = buffer_get_string(&b, NULL);
851 if (strcmp(p, cuser) != 0)
855 if (buffer_len(&b) != 0)
862 mm_answer_keyverify(int socket, Buffer *m)
865 u_char *signature, *data, *blob;
866 u_int signaturelen, datalen, bloblen;
870 blob = buffer_get_string(m, &bloblen);
871 signature = buffer_get_string(m, &signaturelen);
872 data = buffer_get_string(m, &datalen);
874 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
875 !monitor_allowed_key(blob, bloblen))
876 fatal("%s: bad key, not previously allowed", __FUNCTION__);
878 key = key_from_blob(blob, bloblen);
880 fatal("%s: bad public key blob", __FUNCTION__);
882 switch (key_blobtype) {
884 valid_data = monitor_valid_userblob(data, datalen);
887 valid_data = monitor_valid_hostbasedblob(data, datalen,
888 hostbased_cuser, hostbased_chost);
895 fatal("%s: bad signature data blob", __FUNCTION__);
897 verified = key_verify(key, signature, signaturelen, data, datalen);
898 debug3("%s: key %p signature %s",
899 __FUNCTION__, key, verified ? "verified" : "unverified");
906 monitor_reset_key_state();
909 buffer_put_int(m, verified);
910 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
912 auth_method = "publickey";
918 mm_record_login(Session *s, struct passwd *pw)
921 struct sockaddr_storage from;
924 * Get IP address of client. If the connection is not a socket, let
925 * the address be 0.0.0.0.
927 memset(&from, 0, sizeof(from));
928 if (packet_connection_is_on_socket()) {
929 fromlen = sizeof(from);
930 if (getpeername(packet_get_connection_in(),
931 (struct sockaddr *) & from, &fromlen) < 0) {
932 debug("getpeername: %.100s", strerror(errno));
936 /* Record that there was a login on that tty from the remote host. */
937 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
938 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
939 (struct sockaddr *)&from);
943 mm_session_close(Session *s)
945 debug3("%s: session %d pid %d", __FUNCTION__, s->self, s->pid);
946 if (s->ttyfd != -1) {
947 debug3("%s: tty %s ptyfd %d", __FUNCTION__, s->tty, s->ptyfd);
948 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
949 session_pty_cleanup2(s);
955 mm_answer_pty(int socket, Buffer *m)
957 extern struct monitor *monitor;
961 debug3("%s entering", __FUNCTION__);
967 s->authctxt = authctxt;
968 s->pw = authctxt->pw;
969 s->pid = monitor->m_pid;
970 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
973 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
974 pty_setowner(authctxt->pw, s->tty);
976 buffer_put_int(m, 1);
977 buffer_put_cstring(m, s->tty);
978 mm_request_send(socket, MONITOR_ANS_PTY, m);
980 mm_send_fd(socket, s->ptyfd);
981 mm_send_fd(socket, s->ttyfd);
983 /* We need to trick ttyslot */
984 if (dup2(s->ttyfd, 0) == -1)
985 fatal("%s: dup2", __FUNCTION__);
987 mm_record_login(s, authctxt->pw);
989 /* Now we can close the file descriptor again */
992 /* make sure nothing uses fd 0 */
993 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
994 fatal("%s: open(/dev/null): %s", __FUNCTION__, strerror(errno));
996 error("%s: fd0 %d != 0", __FUNCTION__, fd0);
998 /* slave is not needed */
1000 s->ttyfd = s->ptyfd;
1001 /* no need to dup() because nobody closes ptyfd */
1002 s->ptymaster = s->ptyfd;
1004 debug3("%s: tty %s ptyfd %d", __FUNCTION__, s->tty, s->ttyfd);
1010 mm_session_close(s);
1011 buffer_put_int(m, 0);
1012 mm_request_send(socket, MONITOR_ANS_PTY, m);
1017 mm_answer_pty_cleanup(int socket, Buffer *m)
1022 debug3("%s entering", __FUNCTION__);
1024 tty = buffer_get_string(m, NULL);
1025 if ((s = session_by_tty(tty)) != NULL)
1026 mm_session_close(s);
1033 mm_answer_sesskey(int socket, Buffer *m)
1038 /* Turn off permissions */
1039 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1041 if ((p = BN_new()) == NULL)
1042 fatal("%s: BN_new", __FUNCTION__);
1044 buffer_get_bignum2(m, p);
1046 rsafail = ssh1_session_key(p);
1049 buffer_put_int(m, rsafail);
1050 buffer_put_bignum2(m, p);
1054 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1056 /* Turn on permissions for sessid passing */
1057 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1063 mm_answer_sessid(int socket, Buffer *m)
1067 debug3("%s entering", __FUNCTION__);
1069 if (buffer_len(m) != 16)
1070 fatal("%s: bad ssh1 session id", __FUNCTION__);
1071 for (i = 0; i < 16; i++)
1072 session_id[i] = buffer_get_char(m);
1074 /* Turn on permissions for getpwnam */
1075 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1081 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1085 u_char *blob = NULL;
1089 debug3("%s entering", __FUNCTION__);
1091 if (authctxt->valid) {
1092 if ((client_n = BN_new()) == NULL)
1093 fatal("%s: BN_new", __FUNCTION__);
1094 buffer_get_bignum2(m, client_n);
1095 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1096 BN_clear_free(client_n);
1099 buffer_put_int(m, allowed);
1101 /* clear temporarily storage (used by generate challenge) */
1102 monitor_reset_key_state();
1104 if (allowed && key != NULL) {
1105 key->type = KEY_RSA; /* cheat for key_to_blob */
1106 if (key_to_blob(key, &blob, &blen) == 0)
1107 fatal("%s: key_to_blob failed", __FUNCTION__);
1108 buffer_put_string(m, blob, blen);
1110 /* Save temporarily for comparison in verify */
1113 key_blobtype = MM_RSAUSERKEY;
1119 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1121 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1122 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1127 mm_answer_rsa_challenge(int socket, Buffer *m)
1133 debug3("%s entering", __FUNCTION__);
1135 if (!authctxt->valid)
1136 fatal("%s: authctxt not valid", __FUNCTION__);
1137 blob = buffer_get_string(m, &blen);
1138 if (!monitor_allowed_key(blob, blen))
1139 fatal("%s: bad key, not previously allowed", __FUNCTION__);
1140 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1141 fatal("%s: key type mismatch", __FUNCTION__);
1142 if ((key = key_from_blob(blob, blen)) == NULL)
1143 fatal("%s: received bad key", __FUNCTION__);
1146 BN_clear_free(ssh1_challenge);
1147 ssh1_challenge = auth_rsa_generate_challenge(key);
1150 buffer_put_bignum2(m, ssh1_challenge);
1152 debug3("%s sending reply", __FUNCTION__);
1153 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1155 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1160 mm_answer_rsa_response(int socket, Buffer *m)
1163 u_char *blob, *response;
1167 debug3("%s entering", __FUNCTION__);
1169 if (!authctxt->valid)
1170 fatal("%s: authctxt not valid", __FUNCTION__);
1171 if (ssh1_challenge == NULL)
1172 fatal("%s: no ssh1_challenge", __FUNCTION__);
1174 blob = buffer_get_string(m, &blen);
1175 if (!monitor_allowed_key(blob, blen))
1176 fatal("%s: bad key, not previously allowed", __FUNCTION__);
1177 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1178 fatal("%s: key type mismatch: %d", __FUNCTION__, key_blobtype);
1179 if ((key = key_from_blob(blob, blen)) == NULL)
1180 fatal("%s: received bad key", __FUNCTION__);
1181 response = buffer_get_string(m, &len);
1183 fatal("%s: received bad response to challenge", __FUNCTION__);
1184 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1189 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1192 BN_clear_free(ssh1_challenge);
1193 ssh1_challenge = NULL;
1194 monitor_reset_key_state();
1197 buffer_put_int(m, success);
1198 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1204 mm_answer_term(int socket, Buffer *req)
1206 extern struct monitor *monitor;
1209 debug3("%s: tearing down sessions", __FUNCTION__);
1211 /* The child is terminating */
1212 session_destroy_all(&mm_session_close);
1214 while (waitpid(monitor->m_pid, &status, 0) == -1)
1218 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1220 /* Terminate process */
1225 monitor_apply_keystate(struct monitor *monitor)
1228 set_newkeys(MODE_IN);
1229 set_newkeys(MODE_OUT);
1231 u_char key[SSH_SESSION_KEY_LENGTH];
1233 memset(key, 'a', sizeof(key));
1234 packet_set_protocol_flags(child_state.ssh1protoflags);
1235 packet_set_encryption_key(key, SSH_SESSION_KEY_LENGTH,
1236 child_state.ssh1cipher);
1239 packet_set_keycontext(MODE_OUT, child_state.keyout);
1240 xfree(child_state.keyout);
1241 packet_set_keycontext(MODE_IN, child_state.keyin);
1242 xfree(child_state.keyin);
1245 packet_set_iv(MODE_OUT, child_state.ivout);
1246 xfree(child_state.ivout);
1247 packet_set_iv(MODE_IN, child_state.ivin);
1248 xfree(child_state.ivin);
1251 memcpy(&incoming_stream, &child_state.incoming,
1252 sizeof(incoming_stream));
1253 memcpy(&outgoing_stream, &child_state.outgoing,
1254 sizeof(outgoing_stream));
1256 /* Update with new address */
1257 mm_init_compression(monitor->m_zlib);
1259 /* Network I/O buffers */
1260 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1261 buffer_clear(&input);
1262 buffer_append(&input, child_state.input, child_state.ilen);
1263 memset(child_state.input, 0, child_state.ilen);
1264 xfree(child_state.input);
1266 buffer_clear(&output);
1267 buffer_append(&output, child_state.output, child_state.olen);
1268 memset(child_state.output, 0, child_state.olen);
1269 xfree(child_state.output);
1273 mm_get_kex(Buffer *m)
1279 kex = xmalloc(sizeof(*kex));
1280 memset(kex, 0, sizeof(*kex));
1281 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1282 kex->we_need = buffer_get_int(m);
1284 kex->hostkey_type = buffer_get_int(m);
1285 kex->kex_type = buffer_get_int(m);
1286 blob = buffer_get_string(m, &bloblen);
1287 buffer_init(&kex->my);
1288 buffer_append(&kex->my, blob, bloblen);
1290 blob = buffer_get_string(m, &bloblen);
1291 buffer_init(&kex->peer);
1292 buffer_append(&kex->peer, blob, bloblen);
1295 kex->flags = buffer_get_int(m);
1296 kex->client_version_string = buffer_get_string(m, NULL);
1297 kex->server_version_string = buffer_get_string(m, NULL);
1298 kex->load_host_key=&get_hostkey_by_type;
1299 kex->host_key_index=&get_hostkey_index;
1304 /* This function requries careful sanity checking */
1307 mm_get_keystate(struct monitor *monitor)
1311 u_int bloblen, plen;
1313 debug3("%s: Waiting for new keys", __FUNCTION__);
1316 mm_request_receive_expect(monitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1318 child_state.ssh1protoflags = buffer_get_int(&m);
1319 child_state.ssh1cipher = buffer_get_int(&m);
1320 child_state.ivout = buffer_get_string(&m,
1321 &child_state.ivoutlen);
1322 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1325 /* Get the Kex for rekeying */
1326 *monitor->m_pkex = mm_get_kex(&m);
1329 blob = buffer_get_string(&m, &bloblen);
1330 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1333 debug3("%s: Waiting for second key", __FUNCTION__);
1334 blob = buffer_get_string(&m, &bloblen);
1335 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1338 /* Now get sequence numbers for the packets */
1339 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1340 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1343 /* Get the key context */
1344 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1345 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1347 debug3("%s: Getting compression state", __FUNCTION__);
1348 /* Get compression state */
1349 p = buffer_get_string(&m, &plen);
1350 if (plen != sizeof(child_state.outgoing))
1351 fatal("%s: bad request size", __FUNCTION__);
1352 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1355 p = buffer_get_string(&m, &plen);
1356 if (plen != sizeof(child_state.incoming))
1357 fatal("%s: bad request size", __FUNCTION__);
1358 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1361 /* Network I/O buffers */
1362 debug3("%s: Getting Network I/O buffers", __FUNCTION__);
1363 child_state.input = buffer_get_string(&m, &child_state.ilen);
1364 child_state.output = buffer_get_string(&m, &child_state.olen);
1370 /* Allocation functions for zlib */
1372 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1376 address = mm_malloc(mm, size * ncount);
1382 mm_zfree(struct mm_master *mm, void *address)
1384 mm_free(mm, address);
1388 mm_init_compression(struct mm_master *mm)
1390 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1391 outgoing_stream.zfree = (free_func)mm_zfree;
1392 outgoing_stream.opaque = mm;
1394 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1395 incoming_stream.zfree = (free_func)mm_zfree;
1396 incoming_stream.opaque = mm;
1401 #define FD_CLOSEONEXEC(x) do { \
1402 if (fcntl(x, F_SETFD, 1) == -1) \
1403 fatal("fcntl(%d, F_SETFD)", x); \
1407 monitor_socketpair(int *pair)
1409 #ifdef HAVE_SOCKETPAIR
1410 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1411 fatal("%s: socketpair", __FUNCTION__);
1413 fatal("%s: UsePrivilegeSeparation=yes not supported",
1416 FD_CLOSEONEXEC(pair[0]);
1417 FD_CLOSEONEXEC(pair[1]);
1420 #define MM_MEMSIZE 65536
1425 struct monitor *mon;
1428 mon = xmalloc(sizeof(*mon));
1430 monitor_socketpair(pair);
1432 mon->m_recvfd = pair[0];
1433 mon->m_sendfd = pair[1];
1435 /* Used to share zlib space across processes */
1436 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1437 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1439 /* Compression needs to share state across borders */
1440 mm_init_compression(mon->m_zlib);
1446 monitor_reinit(struct monitor *mon)
1450 monitor_socketpair(pair);
1452 mon->m_recvfd = pair[0];
1453 mon->m_sendfd = pair[1];