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.7 2002/03/24 23:20:00 stevesk 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 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
148 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
149 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
152 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
153 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
155 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
156 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
160 struct mon_table mon_dispatch_postauth20[] = {
161 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
162 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
163 {MONITOR_REQ_PTY, 0, mm_answer_pty},
164 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
165 {MONITOR_REQ_TERM, 0, mm_answer_term},
169 struct mon_table mon_dispatch_proto15[] = {
170 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
171 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
172 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
173 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
174 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
175 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
176 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
177 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
179 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
180 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
183 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
184 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
189 struct mon_table mon_dispatch_postauth15[] = {
190 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
191 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
192 {MONITOR_REQ_TERM, 0, mm_answer_term},
196 struct mon_table *mon_dispatch;
198 /* Specifies if a certain message is allowed at the moment */
201 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
203 while (ent->f != NULL) {
204 if (ent->type == type) {
205 ent->flags &= ~MON_PERMIT;
206 ent->flags |= permit ? MON_PERMIT : 0;
214 monitor_permit_authentications(int permit)
216 struct mon_table *ent = mon_dispatch;
218 while (ent->f != NULL) {
219 if (ent->flags & MON_AUTH) {
220 ent->flags &= ~MON_PERMIT;
221 ent->flags |= permit ? MON_PERMIT : 0;
228 monitor_child_preauth(struct monitor *monitor)
230 struct mon_table *ent;
231 int authenticated = 0;
233 debug3("preauth child monitor started");
236 mon_dispatch = mon_dispatch_proto20;
238 /* Permit requests for moduli and signatures */
239 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
240 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
242 mon_dispatch = mon_dispatch_proto15;
244 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
247 authctxt = authctxt_new();
249 /* The first few requests do not require asynchronous access */
250 while (!authenticated) {
251 authenticated = monitor_read(monitor, mon_dispatch, &ent);
253 if (!(ent->flags & MON_AUTHDECIDE))
254 fatal("%s: unexpected authentication from %d",
255 __FUNCTION__, ent->type);
256 if (authctxt->pw->pw_uid == 0 &&
257 !auth_root_allowed(auth_method))
261 if (ent->flags & MON_AUTHDECIDE) {
262 auth_log(authctxt, authenticated, auth_method,
263 compat20 ? " ssh2" : "");
265 authctxt->failures++;
269 if (!authctxt->valid)
270 fatal("%s: authenticated invalid user", __FUNCTION__);
272 debug("%s: %s has been authenticated by privileged process",
273 __FUNCTION__, authctxt->user);
275 mm_get_keystate(monitor);
281 monitor_child_postauth(struct monitor *monitor)
284 mon_dispatch = mon_dispatch_postauth20;
286 /* Permit requests for moduli and signatures */
287 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
288 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
289 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
292 mon_dispatch = mon_dispatch_postauth15;
293 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
296 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
297 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
301 monitor_read(monitor, mon_dispatch, NULL);
305 monitor_sync(struct monitor *monitor)
307 /* The member allocation is not visible, so sync it */
308 mm_share_sync(&monitor->m_zlib, &monitor->m_zback);
312 monitor_read(struct monitor *monitor, struct mon_table *ent,
313 struct mon_table **pent)
321 mm_request_receive(monitor->m_sendfd, &m);
322 type = buffer_get_char(&m);
324 debug3("%s: checking request %d", __FUNCTION__, type);
326 while (ent->f != NULL) {
327 if (ent->type == type)
332 if (ent->f != NULL) {
333 if (!(ent->flags & MON_PERMIT))
334 fatal("%s: unpermitted request %d", __FUNCTION__,
336 ret = (*ent->f)(monitor->m_sendfd, &m);
339 /* The child may use this request only once, disable it */
340 if (ent->flags & MON_ONCE) {
341 debug2("%s: %d used once, disabling now", __FUNCTION__,
343 ent->flags &= ~MON_PERMIT;
352 fatal("%s: unsupported request: %d", __FUNCTION__, type);
358 /* allowed key state */
360 monitor_allowed_key(u_char *blob, u_int bloblen)
362 /* make sure key is allowed */
363 if (key_blob == NULL || key_bloblen != bloblen ||
364 memcmp(key_blob, blob, key_bloblen))
370 monitor_reset_key_state(void)
373 if (key_blob != NULL)
375 if (hostbased_cuser != NULL)
376 xfree(hostbased_cuser);
377 if (hostbased_chost != NULL)
378 xfree(hostbased_chost);
381 key_blobtype = MM_NOKEY;
382 hostbased_cuser = NULL;
383 hostbased_chost = NULL;
387 mm_answer_moduli(int socket, Buffer *m)
392 min = buffer_get_int(m);
393 want = buffer_get_int(m);
394 max = buffer_get_int(m);
396 debug3("%s: got parameters: %d %d %d",
397 __FUNCTION__, min, want, max);
398 /* We need to check here, too, in case the child got corrupted */
399 if (max < min || want < min || max < want)
400 fatal("%s: bad parameters: %d %d %d",
401 __FUNCTION__, min, want, max);
405 dh = choose_dh(min, want, max);
407 buffer_put_char(m, 0);
410 /* Send first bignum */
411 buffer_put_char(m, 1);
412 buffer_put_bignum2(m, dh->p);
413 buffer_put_bignum2(m, dh->g);
417 mm_request_send(socket, MONITOR_ANS_MODULI, m);
422 mm_answer_sign(int socket, Buffer *m)
427 u_int siglen, datlen;
430 debug3("%s", __FUNCTION__);
432 keyid = buffer_get_int(m);
433 p = buffer_get_string(m, &datlen);
436 fatal("%s: data length incorrect: %d", __FUNCTION__, datlen);
438 if ((key = get_hostkey_by_index(keyid)) == NULL)
439 fatal("%s: no hostkey from index %d", __FUNCTION__, keyid);
440 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
441 fatal("%s: key_sign failed", __FUNCTION__);
443 debug3("%s: signature %p(%d)", __FUNCTION__, signature, siglen);
446 buffer_put_string(m, signature, siglen);
451 mm_request_send(socket, MONITOR_ANS_SIGN, m);
453 /* Turn on permissions for getpwnam */
454 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
459 /* Retrieves the password entry and also checks if the user is permitted */
462 mm_answer_pwnamallow(int socket, Buffer *m)
465 struct passwd *pwent;
468 debug3("%s", __FUNCTION__);
470 if (authctxt->attempt++ != 0)
471 fatal("%s: multiple attempts for getpwnam", __FUNCTION__);
473 login = buffer_get_string(m, NULL);
475 pwent = getpwnamallow(login);
477 authctxt->user = xstrdup(login);
478 setproctitle("%s [priv]", pwent ? login : "unknown");
484 buffer_put_char(m, 0);
489 authctxt->pw = pwent;
492 buffer_put_char(m, 1);
493 buffer_put_string(m, pwent, sizeof(struct passwd));
494 buffer_put_cstring(m, pwent->pw_name);
495 buffer_put_cstring(m, "*");
496 buffer_put_cstring(m, pwent->pw_gecos);
497 #ifdef HAVE_PW_CLASS_IN_PASSWD
498 buffer_put_cstring(m, pwent->pw_class);
500 buffer_put_cstring(m, pwent->pw_dir);
501 buffer_put_cstring(m, pwent->pw_shell);
504 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __FUNCTION__, allowed);
505 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
507 /* For SSHv1 allow authentication now */
509 monitor_permit_authentications(1);
511 /* Allow service/style information on the auth context */
512 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
519 mm_answer_authserv(int socket, Buffer *m)
521 monitor_permit_authentications(1);
523 authctxt->service = buffer_get_string(m, NULL);
524 authctxt->style = buffer_get_string(m, NULL);
525 debug3("%s: service=%s, style=%s",
526 __FUNCTION__, authctxt->service, authctxt->style);
528 if (strlen(authctxt->style) == 0) {
529 xfree(authctxt->style);
530 authctxt->style = NULL;
537 mm_answer_authpassword(int socket, Buffer *m)
539 static int call_count;
541 int authenticated, plen;
543 passwd = buffer_get_string(m, &plen);
544 /* Only authenticate if the context is valid */
545 authenticated = authctxt->valid && auth_password(authctxt, passwd);
546 memset(passwd, 0, strlen(passwd));
550 buffer_put_int(m, authenticated);
552 debug3("%s: sending result %d", __FUNCTION__, authenticated);
553 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
556 if (plen == 0 && call_count == 1)
557 auth_method = "none";
559 auth_method = "password";
561 /* Causes monitor loop to terminate if authenticated */
562 return (authenticated);
567 mm_answer_bsdauthquery(int socket, Buffer *m)
569 char *name, *infotxt;
575 res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
579 buffer_put_int(m, res);
581 buffer_put_cstring(m, prompts[0]);
583 debug3("%s: sending challenge res: %d", __FUNCTION__, res);
584 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
597 mm_answer_bsdauthrespond(int socket, Buffer *m)
602 if (authctxt->as == 0)
603 fatal("%s: no bsd auth session", __FUNCTION__);
605 response = buffer_get_string(m, NULL);
606 authok = auth_userresponse(authctxt->as, response, 0);
608 debug3("%s: <%s> = <%d>", __FUNCTION__, response, authok);
612 buffer_put_int(m, authok);
614 debug3("%s: sending authenticated: %d", __FUNCTION__, authok);
615 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
617 auth_method = "bsdauth";
619 return (authok != 0);
625 mm_answer_skeyquery(int socket, Buffer *m)
628 char challenge[1024];
631 res = skeychallenge(&skey, authctxt->user, challenge);
634 buffer_put_int(m, res);
636 buffer_put_cstring(m, challenge);
638 debug3("%s: sending challenge res: %d", __FUNCTION__, res);
639 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
645 mm_answer_skeyrespond(int socket, Buffer *m)
650 response = buffer_get_string(m, NULL);
652 authok = (authctxt->valid &&
653 skey_haskey(authctxt->pw->pw_name) == 0 &&
654 skey_passcheck(authctxt->pw->pw_name, response) != -1);
659 buffer_put_int(m, authok);
661 debug3("%s: sending authenticated: %d", __FUNCTION__, authok);
662 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
664 auth_method = "skey";
666 return (authok != 0);
671 mm_append_debug(Buffer *m)
673 if (auth_debug_init && buffer_len(&auth_debug)) {
674 debug3("%s: Appending debug messages for child", __FUNCTION__);
675 buffer_append(m, buffer_ptr(&auth_debug),
676 buffer_len(&auth_debug));
677 buffer_clear(&auth_debug);
682 mm_answer_keyallowed(int socket, Buffer *m)
685 u_char *cuser, *chost, *blob;
687 enum mm_keytype type = 0;
690 debug3("%s entering", __FUNCTION__);
692 type = buffer_get_int(m);
693 cuser = buffer_get_string(m, NULL);
694 chost = buffer_get_string(m, NULL);
695 blob = buffer_get_string(m, &bloblen);
697 key = key_from_blob(blob, bloblen);
699 if ((compat20 && type == MM_RSAHOSTKEY) ||
700 (!compat20 && type != MM_RSAHOSTKEY))
701 fatal("%s: key type and protocol mismatch", __FUNCTION__);
703 debug3("%s: key_from_blob: %p", __FUNCTION__, key);
705 if (key != NULL && authctxt->pw != NULL) {
708 allowed = user_key_allowed(authctxt->pw, key);
711 allowed = hostbased_key_allowed(authctxt->pw,
715 key->type = KEY_RSA1; /* XXX */
716 allowed = auth_rhosts_rsa_key_allowed(authctxt->pw,
720 fatal("%s: unknown key type %d", __FUNCTION__, type);
726 /* clear temporarily storage (used by verify) */
727 monitor_reset_key_state();
730 /* Save temporarily for comparison in verify */
732 key_bloblen = bloblen;
734 hostbased_cuser = cuser;
735 hostbased_chost = chost;
738 debug3("%s: key %p is %s",
739 __FUNCTION__, key, allowed ? "allowed" : "disallowed");
742 buffer_put_int(m, allowed);
746 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
748 if (type == MM_RSAHOSTKEY)
749 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
755 monitor_valid_userblob(u_char *data, u_int datalen)
761 int session_id2_len = 20 /*XXX should get from [net] */;
764 buffer_append(&b, data, datalen);
766 if (datafellows & SSH_OLD_SESSIONID) {
767 buffer_consume(&b, session_id2_len);
769 xfree(buffer_get_string(&b, &len));
770 if (len != session_id2_len)
773 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
775 p = buffer_get_string(&b, NULL);
776 if (strcmp(authctxt->user, p) != 0) {
777 log("wrong user name passed to monitor: expected %s != %.100s",
782 buffer_skip_string(&b);
783 if (datafellows & SSH_BUG_PKAUTH) {
784 if (!buffer_get_char(&b))
787 p = buffer_get_string(&b, NULL);
788 if (strcmp("publickey", p) != 0)
791 if (!buffer_get_char(&b))
793 buffer_skip_string(&b);
795 buffer_skip_string(&b);
796 if (buffer_len(&b) != 0)
803 monitor_valid_hostbasedblob(u_char *data, u_int datalen, u_char *cuser,
810 int session_id2_len = 20 /*XXX should get from [net] */;
813 buffer_append(&b, data, datalen);
815 xfree(buffer_get_string(&b, &len));
816 if (len != session_id2_len)
818 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
820 p = buffer_get_string(&b, NULL);
821 if (strcmp(authctxt->user, p) != 0) {
822 log("wrong user name passed to monitor: expected %s != %.100s",
827 buffer_skip_string(&b); /* service */
828 p = buffer_get_string(&b, NULL);
829 if (strcmp(p, "hostbased") != 0)
832 buffer_skip_string(&b); /* pkalg */
833 buffer_skip_string(&b); /* pkblob */
835 /* verify client host, strip trailing dot if necessary */
836 p = buffer_get_string(&b, NULL);
837 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
839 if (strcmp(p, chost) != 0)
843 /* verify client user */
844 p = buffer_get_string(&b, NULL);
845 if (strcmp(p, cuser) != 0)
849 if (buffer_len(&b) != 0)
856 mm_answer_keyverify(int socket, Buffer *m)
859 u_char *signature, *data, *blob;
860 u_int signaturelen, datalen, bloblen;
864 blob = buffer_get_string(m, &bloblen);
865 signature = buffer_get_string(m, &signaturelen);
866 data = buffer_get_string(m, &datalen);
868 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
869 monitor_allowed_key(blob, bloblen) == NULL)
870 fatal("%s: bad key, not previously allowed", __FUNCTION__);
872 key = key_from_blob(blob, bloblen);
874 fatal("%s: bad public key blob", __FUNCTION__);
876 switch (key_blobtype) {
878 valid_data = monitor_valid_userblob(data, datalen);
881 valid_data = monitor_valid_hostbasedblob(data, datalen,
882 hostbased_cuser, hostbased_chost);
889 fatal("%s: bad signature data blob", __FUNCTION__);
891 verified = key_verify(key, signature, signaturelen, data, datalen);
892 debug3("%s: key %p signature %s",
893 __FUNCTION__, key, verified ? "verified" : "unverified");
900 monitor_reset_key_state();
903 buffer_put_int(m, verified);
904 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
906 auth_method = "publickey";
912 mm_record_login(Session *s, struct passwd *pw)
915 struct sockaddr_storage from;
918 * Get IP address of client. If the connection is not a socket, let
919 * the address be 0.0.0.0.
921 memset(&from, 0, sizeof(from));
922 if (packet_connection_is_on_socket()) {
923 fromlen = sizeof(from);
924 if (getpeername(packet_get_connection_in(),
925 (struct sockaddr *) & from, &fromlen) < 0) {
926 debug("getpeername: %.100s", strerror(errno));
930 /* Record that there was a login on that tty from the remote host. */
931 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
932 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
933 (struct sockaddr *)&from);
937 mm_session_close(Session *s)
939 debug3("%s: session %d pid %d", __FUNCTION__, s->self, s->pid);
940 if (s->ttyfd != -1) {
941 debug3("%s: tty %s ptyfd %d", __FUNCTION__, s->tty, s->ptyfd);
942 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
943 session_pty_cleanup2(s);
949 mm_answer_pty(int socket, Buffer *m)
951 extern struct monitor *monitor;
955 debug3("%s entering", __FUNCTION__);
961 s->authctxt = authctxt;
962 s->pw = authctxt->pw;
963 s->pid = monitor->m_pid;
964 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
967 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
968 pty_setowner(authctxt->pw, s->tty);
970 buffer_put_int(m, 1);
971 buffer_put_cstring(m, s->tty);
972 mm_request_send(socket, MONITOR_ANS_PTY, m);
974 mm_send_fd(socket, s->ptyfd);
975 mm_send_fd(socket, s->ttyfd);
977 /* We need to trick ttyslot */
978 if (dup2(s->ttyfd, 0) == -1)
979 fatal("%s: dup2", __FUNCTION__);
981 mm_record_login(s, authctxt->pw);
983 /* Now we can close the file descriptor again */
986 /* make sure nothing uses fd 0 */
987 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
988 fatal("%s: open(/dev/null): %s", __FUNCTION__, strerror(errno));
990 error("%s: fd0 %d != 0", __FUNCTION__, fd0);
992 /* slave is not needed */
995 /* no need to dup() because nobody closes ptyfd */
996 s->ptymaster = s->ptyfd;
998 debug3("%s: tty %s ptyfd %d", __FUNCTION__, s->tty, s->ttyfd);
1004 mm_session_close(s);
1005 buffer_put_int(m, 0);
1006 mm_request_send(socket, MONITOR_ANS_PTY, m);
1011 mm_answer_pty_cleanup(int socket, Buffer *m)
1016 debug3("%s entering", __FUNCTION__);
1018 tty = buffer_get_string(m, NULL);
1019 if ((s = session_by_tty(tty)) != NULL)
1020 mm_session_close(s);
1027 mm_answer_sesskey(int socket, Buffer *m)
1032 /* Turn off permissions */
1033 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1035 if ((p = BN_new()) == NULL)
1036 fatal("%s: BN_new", __FUNCTION__);
1038 buffer_get_bignum2(m, p);
1040 rsafail = ssh1_session_key(p);
1043 buffer_put_int(m, rsafail);
1044 buffer_put_bignum2(m, p);
1048 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1050 /* Turn on permissions for sessid passing */
1051 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1057 mm_answer_sessid(int socket, Buffer *m)
1061 debug3("%s entering", __FUNCTION__);
1063 if (buffer_len(m) != 16)
1064 fatal("%s: bad ssh1 session id", __FUNCTION__);
1065 for (i = 0; i < 16; i++)
1066 session_id[i] = buffer_get_char(m);
1068 /* Turn on permissions for getpwnam */
1069 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1075 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1079 u_char *blob = NULL;
1083 debug3("%s entering", __FUNCTION__);
1085 if (authctxt->valid) {
1086 if ((client_n = BN_new()) == NULL)
1087 fatal("%s: BN_new", __FUNCTION__);
1088 buffer_get_bignum2(m, client_n);
1089 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1090 BN_clear_free(client_n);
1093 buffer_put_int(m, allowed);
1095 /* clear temporarily storage (used by generate challenge) */
1096 monitor_reset_key_state();
1098 if (allowed && key != NULL) {
1099 key->type = KEY_RSA; /* cheat for key_to_blob */
1100 if (key_to_blob(key, &blob, &blen) == 0)
1101 fatal("%s: key_to_blob failed", __FUNCTION__);
1102 buffer_put_string(m, blob, blen);
1104 /* Save temporarily for comparison in verify */
1107 key_blobtype = MM_RSAUSERKEY;
1113 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1115 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1116 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1121 mm_answer_rsa_challenge(int socket, Buffer *m)
1127 debug3("%s entering", __FUNCTION__);
1129 if (!authctxt->valid)
1130 fatal("%s: authctxt not valid", __FUNCTION__);
1131 blob = buffer_get_string(m, &blen);
1132 if (!monitor_allowed_key(blob, blen))
1133 fatal("%s: bad key, not previously allowed", __FUNCTION__);
1134 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1135 fatal("%s: key type mismatch", __FUNCTION__);
1136 if ((key = key_from_blob(blob, blen)) == NULL)
1137 fatal("%s: received bad key", __FUNCTION__);
1140 BN_clear_free(ssh1_challenge);
1141 ssh1_challenge = auth_rsa_generate_challenge(key);
1144 buffer_put_bignum2(m, ssh1_challenge);
1146 debug3("%s sending reply", __FUNCTION__);
1147 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1149 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1154 mm_answer_rsa_response(int socket, Buffer *m)
1157 u_char *blob, *response;
1161 debug3("%s entering", __FUNCTION__);
1163 if (!authctxt->valid)
1164 fatal("%s: authctxt not valid", __FUNCTION__);
1165 if (ssh1_challenge == NULL)
1166 fatal("%s: no ssh1_challenge", __FUNCTION__);
1168 blob = buffer_get_string(m, &blen);
1169 if (!monitor_allowed_key(blob, blen))
1170 fatal("%s: bad key, not previously allowed", __FUNCTION__);
1171 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1172 fatal("%s: key type mismatch: %d", __FUNCTION__, key_blobtype);
1173 if ((key = key_from_blob(blob, blen)) == NULL)
1174 fatal("%s: received bad key", __FUNCTION__);
1175 response = buffer_get_string(m, &len);
1177 fatal("%s: received bad response to challenge", __FUNCTION__);
1178 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1183 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1186 BN_clear_free(ssh1_challenge);
1187 ssh1_challenge = NULL;
1188 monitor_reset_key_state();
1191 buffer_put_int(m, success);
1192 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1198 mm_answer_term(int socket, Buffer *req)
1200 extern struct monitor *monitor;
1203 debug3("%s: tearing down sessions", __FUNCTION__);
1205 /* The child is terminating */
1206 session_destroy_all(&mm_session_close);
1208 if (waitpid(monitor->m_pid, &status, 0) == -1)
1211 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1213 /* Terminate process */
1218 monitor_apply_keystate(struct monitor *monitor)
1221 set_newkeys(MODE_IN);
1222 set_newkeys(MODE_OUT);
1224 u_char key[SSH_SESSION_KEY_LENGTH];
1226 memset(key, 'a', sizeof(key));
1227 packet_set_protocol_flags(child_state.ssh1protoflags);
1228 packet_set_encryption_key(key, SSH_SESSION_KEY_LENGTH,
1229 child_state.ssh1cipher);
1232 packet_set_keycontext(MODE_OUT, child_state.keyout);
1233 xfree(child_state.keyout);
1234 packet_set_keycontext(MODE_IN, child_state.keyin);
1235 xfree(child_state.keyin);
1238 packet_set_iv(MODE_OUT, child_state.ivout);
1239 xfree(child_state.ivout);
1240 packet_set_iv(MODE_IN, child_state.ivin);
1241 xfree(child_state.ivin);
1244 memcpy(&incoming_stream, &child_state.incoming,
1245 sizeof(incoming_stream));
1246 memcpy(&outgoing_stream, &child_state.outgoing,
1247 sizeof(outgoing_stream));
1249 /* Update with new address */
1250 mm_init_compression(monitor->m_zlib);
1252 /* Network I/O buffers */
1253 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1254 buffer_clear(&input);
1255 buffer_append(&input, child_state.input, child_state.ilen);
1256 memset(child_state.input, 0, child_state.ilen);
1257 xfree(child_state.input);
1259 buffer_clear(&output);
1260 buffer_append(&output, child_state.output, child_state.olen);
1261 memset(child_state.output, 0, child_state.olen);
1262 xfree(child_state.output);
1266 mm_get_kex(Buffer *m)
1272 kex = xmalloc(sizeof(*kex));
1273 memset(kex, 0, sizeof(*kex));
1274 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1275 kex->we_need = buffer_get_int(m);
1277 kex->hostkey_type = buffer_get_int(m);
1278 kex->kex_type = buffer_get_int(m);
1279 blob = buffer_get_string(m, &bloblen);
1280 buffer_init(&kex->my);
1281 buffer_append(&kex->my, blob, bloblen);
1283 blob = buffer_get_string(m, &bloblen);
1284 buffer_init(&kex->peer);
1285 buffer_append(&kex->peer, blob, bloblen);
1288 kex->flags = buffer_get_int(m);
1289 kex->client_version_string = buffer_get_string(m, NULL);
1290 kex->server_version_string = buffer_get_string(m, NULL);
1291 kex->load_host_key=&get_hostkey_by_type;
1292 kex->host_key_index=&get_hostkey_index;
1297 /* This function requries careful sanity checking */
1300 mm_get_keystate(struct monitor *monitor)
1304 u_int bloblen, plen;
1306 debug3("%s: Waiting for new keys", __FUNCTION__);
1309 mm_request_receive_expect(monitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1311 child_state.ssh1protoflags = buffer_get_int(&m);
1312 child_state.ssh1cipher = buffer_get_int(&m);
1313 child_state.ivout = buffer_get_string(&m,
1314 &child_state.ivoutlen);
1315 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1318 /* Get the Kex for rekeying */
1319 *monitor->m_pkex = mm_get_kex(&m);
1322 blob = buffer_get_string(&m, &bloblen);
1323 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1326 debug3("%s: Waiting for second key", __FUNCTION__);
1327 blob = buffer_get_string(&m, &bloblen);
1328 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1331 /* Now get sequence numbers for the packets */
1332 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1333 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1336 /* Get the key context */
1337 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1338 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1340 debug3("%s: Getting compression state", __FUNCTION__);
1341 /* Get compression state */
1342 p = buffer_get_string(&m, &plen);
1343 if (plen != sizeof(child_state.outgoing))
1344 fatal("%s: bad request size", __FUNCTION__);
1345 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1348 p = buffer_get_string(&m, &plen);
1349 if (plen != sizeof(child_state.incoming))
1350 fatal("%s: bad request size", __FUNCTION__);
1351 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1354 /* Network I/O buffers */
1355 debug3("%s: Getting Network I/O buffers", __FUNCTION__);
1356 child_state.input = buffer_get_string(&m, &child_state.ilen);
1357 child_state.output = buffer_get_string(&m, &child_state.olen);
1363 /* Allocation functions for zlib */
1365 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1369 address = mm_malloc(mm, size * ncount);
1375 mm_zfree(struct mm_master *mm, void *address)
1377 mm_free(mm, address);
1381 mm_init_compression(struct mm_master *mm)
1383 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1384 outgoing_stream.zfree = (free_func)mm_zfree;
1385 outgoing_stream.opaque = mm;
1387 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1388 incoming_stream.zfree = (free_func)mm_zfree;
1389 incoming_stream.opaque = mm;
1394 #define FD_CLOSEONEXEC(x) do { \
1395 if (fcntl(x, F_SETFD, 1) == -1) \
1396 fatal("fcntl(%d, F_SETFD)", x); \
1400 monitor_socketpair(int *pair)
1402 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1403 fatal("%s: socketpair", __FUNCTION__);
1404 FD_CLOSEONEXEC(pair[0]);
1405 FD_CLOSEONEXEC(pair[1]);
1408 #define MM_MEMSIZE 65536
1413 struct monitor *mon;
1416 mon = xmalloc(sizeof(*mon));
1418 monitor_socketpair(pair);
1420 mon->m_recvfd = pair[0];
1421 mon->m_sendfd = pair[1];
1423 /* Used to share zlib space across processes */
1424 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1425 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1427 /* Compression needs to share state across borders */
1428 mm_init_compression(mon->m_zlib);
1434 monitor_reinit(struct monitor *mon)
1438 monitor_socketpair(pair);
1440 mon->m_recvfd = pair[0];
1441 mon->m_sendfd = pair[1];