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 *);
117 int mm_answer_pam_start(int, Buffer *);
120 static Authctxt *authctxt;
121 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
123 /* local state for key verify */
124 static u_char *key_blob = NULL;
125 static u_int key_bloblen = 0;
126 static int key_blobtype = MM_NOKEY;
127 static u_char *hostbased_cuser = NULL;
128 static u_char *hostbased_chost = NULL;
129 static char *auth_method = "unknown";
132 enum monitor_reqtype type;
134 int (*f)(int, Buffer *);
137 #define MON_ISAUTH 0x0004 /* Required for Authentication */
138 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
139 #define MON_ONCE 0x0010 /* Disable after calling */
141 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
143 #define MON_PERMIT 0x1000 /* Request is permitted */
145 struct mon_table mon_dispatch_proto20[] = {
146 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
147 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
148 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
149 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
150 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
152 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
155 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
156 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
159 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
160 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
162 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
163 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
167 struct mon_table mon_dispatch_postauth20[] = {
168 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
169 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
170 {MONITOR_REQ_PTY, 0, mm_answer_pty},
171 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
172 {MONITOR_REQ_TERM, 0, mm_answer_term},
176 struct mon_table mon_dispatch_proto15[] = {
177 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
178 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
179 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
180 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
181 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
182 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
183 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
184 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
186 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
189 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
190 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
193 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
194 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
197 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
202 struct mon_table mon_dispatch_postauth15[] = {
203 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
204 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
205 {MONITOR_REQ_TERM, 0, mm_answer_term},
209 struct mon_table *mon_dispatch;
211 /* Specifies if a certain message is allowed at the moment */
214 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
216 while (ent->f != NULL) {
217 if (ent->type == type) {
218 ent->flags &= ~MON_PERMIT;
219 ent->flags |= permit ? MON_PERMIT : 0;
227 monitor_permit_authentications(int permit)
229 struct mon_table *ent = mon_dispatch;
231 while (ent->f != NULL) {
232 if (ent->flags & MON_AUTH) {
233 ent->flags &= ~MON_PERMIT;
234 ent->flags |= permit ? MON_PERMIT : 0;
241 monitor_child_preauth(struct monitor *monitor)
243 struct mon_table *ent;
244 int authenticated = 0;
246 debug3("preauth child monitor started");
249 mon_dispatch = mon_dispatch_proto20;
251 /* Permit requests for moduli and signatures */
252 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
253 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
255 mon_dispatch = mon_dispatch_proto15;
257 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
260 authctxt = authctxt_new();
262 /* The first few requests do not require asynchronous access */
263 while (!authenticated) {
264 authenticated = monitor_read(monitor, mon_dispatch, &ent);
266 if (!(ent->flags & MON_AUTHDECIDE))
267 fatal("%s: unexpected authentication from %d",
268 __FUNCTION__, ent->type);
269 if (authctxt->pw->pw_uid == 0 &&
270 !auth_root_allowed(auth_method))
273 if (!do_pam_account(authctxt->pw->pw_name, NULL))
278 if (ent->flags & MON_AUTHDECIDE) {
279 auth_log(authctxt, authenticated, auth_method,
280 compat20 ? " ssh2" : "");
282 authctxt->failures++;
286 if (!authctxt->valid)
287 fatal("%s: authenticated invalid user", __FUNCTION__);
289 debug("%s: %s has been authenticated by privileged process",
290 __FUNCTION__, authctxt->user);
292 mm_get_keystate(monitor);
298 monitor_child_postauth(struct monitor *monitor)
301 mon_dispatch = mon_dispatch_postauth20;
303 /* Permit requests for moduli and signatures */
304 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
305 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
306 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
309 mon_dispatch = mon_dispatch_postauth15;
310 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
313 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
314 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
318 monitor_read(monitor, mon_dispatch, NULL);
322 monitor_sync(struct monitor *monitor)
324 /* The member allocation is not visible, so sync it */
325 mm_share_sync(&monitor->m_zlib, &monitor->m_zback);
329 monitor_read(struct monitor *monitor, struct mon_table *ent,
330 struct mon_table **pent)
338 mm_request_receive(monitor->m_sendfd, &m);
339 type = buffer_get_char(&m);
341 debug3("%s: checking request %d", __FUNCTION__, type);
343 while (ent->f != NULL) {
344 if (ent->type == type)
349 if (ent->f != NULL) {
350 if (!(ent->flags & MON_PERMIT))
351 fatal("%s: unpermitted request %d", __FUNCTION__,
353 ret = (*ent->f)(monitor->m_sendfd, &m);
356 /* The child may use this request only once, disable it */
357 if (ent->flags & MON_ONCE) {
358 debug2("%s: %d used once, disabling now", __FUNCTION__,
360 ent->flags &= ~MON_PERMIT;
369 fatal("%s: unsupported request: %d", __FUNCTION__, type);
375 /* allowed key state */
377 monitor_allowed_key(u_char *blob, u_int bloblen)
379 /* make sure key is allowed */
380 if (key_blob == NULL || key_bloblen != bloblen ||
381 memcmp(key_blob, blob, key_bloblen))
387 monitor_reset_key_state(void)
390 if (key_blob != NULL)
392 if (hostbased_cuser != NULL)
393 xfree(hostbased_cuser);
394 if (hostbased_chost != NULL)
395 xfree(hostbased_chost);
398 key_blobtype = MM_NOKEY;
399 hostbased_cuser = NULL;
400 hostbased_chost = NULL;
404 mm_answer_moduli(int socket, Buffer *m)
409 min = buffer_get_int(m);
410 want = buffer_get_int(m);
411 max = buffer_get_int(m);
413 debug3("%s: got parameters: %d %d %d",
414 __FUNCTION__, min, want, max);
415 /* We need to check here, too, in case the child got corrupted */
416 if (max < min || want < min || max < want)
417 fatal("%s: bad parameters: %d %d %d",
418 __FUNCTION__, min, want, max);
422 dh = choose_dh(min, want, max);
424 buffer_put_char(m, 0);
427 /* Send first bignum */
428 buffer_put_char(m, 1);
429 buffer_put_bignum2(m, dh->p);
430 buffer_put_bignum2(m, dh->g);
434 mm_request_send(socket, MONITOR_ANS_MODULI, m);
439 mm_answer_sign(int socket, Buffer *m)
444 u_int siglen, datlen;
447 debug3("%s", __FUNCTION__);
449 keyid = buffer_get_int(m);
450 p = buffer_get_string(m, &datlen);
453 fatal("%s: data length incorrect: %d", __FUNCTION__, datlen);
455 if ((key = get_hostkey_by_index(keyid)) == NULL)
456 fatal("%s: no hostkey from index %d", __FUNCTION__, keyid);
457 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
458 fatal("%s: key_sign failed", __FUNCTION__);
460 debug3("%s: signature %p(%d)", __FUNCTION__, signature, siglen);
463 buffer_put_string(m, signature, siglen);
468 mm_request_send(socket, MONITOR_ANS_SIGN, m);
470 /* Turn on permissions for getpwnam */
471 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
476 /* Retrieves the password entry and also checks if the user is permitted */
479 mm_answer_pwnamallow(int socket, Buffer *m)
482 struct passwd *pwent;
485 debug3("%s", __FUNCTION__);
487 if (authctxt->attempt++ != 0)
488 fatal("%s: multiple attempts for getpwnam", __FUNCTION__);
490 login = buffer_get_string(m, NULL);
492 pwent = getpwnamallow(login);
494 authctxt->user = xstrdup(login);
495 setproctitle("%s [priv]", pwent ? login : "unknown");
501 buffer_put_char(m, 0);
506 authctxt->pw = pwent;
509 buffer_put_char(m, 1);
510 buffer_put_string(m, pwent, sizeof(struct passwd));
511 buffer_put_cstring(m, pwent->pw_name);
512 buffer_put_cstring(m, "*");
513 buffer_put_cstring(m, pwent->pw_gecos);
514 #ifdef HAVE_PW_CLASS_IN_PASSWD
515 buffer_put_cstring(m, pwent->pw_class);
517 buffer_put_cstring(m, pwent->pw_dir);
518 buffer_put_cstring(m, pwent->pw_shell);
521 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __FUNCTION__, allowed);
522 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
524 /* For SSHv1 allow authentication now */
526 monitor_permit_authentications(1);
528 /* Allow service/style information on the auth context */
529 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
532 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
539 mm_answer_authserv(int socket, Buffer *m)
541 monitor_permit_authentications(1);
543 authctxt->service = buffer_get_string(m, NULL);
544 authctxt->style = buffer_get_string(m, NULL);
545 debug3("%s: service=%s, style=%s",
546 __FUNCTION__, authctxt->service, authctxt->style);
548 if (strlen(authctxt->style) == 0) {
549 xfree(authctxt->style);
550 authctxt->style = NULL;
557 mm_answer_authpassword(int socket, Buffer *m)
559 static int call_count;
561 int authenticated, plen;
563 passwd = buffer_get_string(m, &plen);
564 /* Only authenticate if the context is valid */
565 authenticated = authctxt->valid && auth_password(authctxt, passwd);
566 memset(passwd, 0, strlen(passwd));
570 buffer_put_int(m, authenticated);
572 debug3("%s: sending result %d", __FUNCTION__, authenticated);
573 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
576 if (plen == 0 && call_count == 1)
577 auth_method = "none";
579 auth_method = "password";
581 /* Causes monitor loop to terminate if authenticated */
582 return (authenticated);
587 mm_answer_bsdauthquery(int socket, Buffer *m)
589 char *name, *infotxt;
595 res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
599 buffer_put_int(m, res);
601 buffer_put_cstring(m, prompts[0]);
603 debug3("%s: sending challenge res: %d", __FUNCTION__, res);
604 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
617 mm_answer_bsdauthrespond(int socket, Buffer *m)
622 if (authctxt->as == 0)
623 fatal("%s: no bsd auth session", __FUNCTION__);
625 response = buffer_get_string(m, NULL);
626 authok = auth_userresponse(authctxt->as, response, 0);
628 debug3("%s: <%s> = <%d>", __FUNCTION__, response, authok);
632 buffer_put_int(m, authok);
634 debug3("%s: sending authenticated: %d", __FUNCTION__, authok);
635 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
637 auth_method = "bsdauth";
639 return (authok != 0);
645 mm_answer_skeyquery(int socket, Buffer *m)
648 char challenge[1024];
651 res = skeychallenge(&skey, authctxt->user, challenge);
654 buffer_put_int(m, res);
656 buffer_put_cstring(m, challenge);
658 debug3("%s: sending challenge res: %d", __FUNCTION__, res);
659 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
665 mm_answer_skeyrespond(int socket, Buffer *m)
670 response = buffer_get_string(m, NULL);
672 authok = (authctxt->valid &&
673 skey_haskey(authctxt->pw->pw_name) == 0 &&
674 skey_passcheck(authctxt->pw->pw_name, response) != -1);
679 buffer_put_int(m, authok);
681 debug3("%s: sending authenticated: %d", __FUNCTION__, authok);
682 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
684 auth_method = "skey";
686 return (authok != 0);
692 mm_answer_pam_start(int socket, Buffer *m)
696 user = buffer_get_string(m, NULL);
707 mm_append_debug(Buffer *m)
709 if (auth_debug_init && buffer_len(&auth_debug)) {
710 debug3("%s: Appending debug messages for child", __FUNCTION__);
711 buffer_append(m, buffer_ptr(&auth_debug),
712 buffer_len(&auth_debug));
713 buffer_clear(&auth_debug);
718 mm_answer_keyallowed(int socket, Buffer *m)
721 u_char *cuser, *chost, *blob;
723 enum mm_keytype type = 0;
726 debug3("%s entering", __FUNCTION__);
728 type = buffer_get_int(m);
729 cuser = buffer_get_string(m, NULL);
730 chost = buffer_get_string(m, NULL);
731 blob = buffer_get_string(m, &bloblen);
733 key = key_from_blob(blob, bloblen);
735 if ((compat20 && type == MM_RSAHOSTKEY) ||
736 (!compat20 && type != MM_RSAHOSTKEY))
737 fatal("%s: key type and protocol mismatch", __FUNCTION__);
739 debug3("%s: key_from_blob: %p", __FUNCTION__, key);
741 if (key != NULL && authctxt->pw != NULL) {
744 allowed = user_key_allowed(authctxt->pw, key);
747 allowed = hostbased_key_allowed(authctxt->pw,
751 key->type = KEY_RSA1; /* XXX */
752 allowed = auth_rhosts_rsa_key_allowed(authctxt->pw,
756 fatal("%s: unknown key type %d", __FUNCTION__, type);
762 /* clear temporarily storage (used by verify) */
763 monitor_reset_key_state();
766 /* Save temporarily for comparison in verify */
768 key_bloblen = bloblen;
770 hostbased_cuser = cuser;
771 hostbased_chost = chost;
774 debug3("%s: key %p is %s",
775 __FUNCTION__, key, allowed ? "allowed" : "disallowed");
778 buffer_put_int(m, allowed);
782 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
784 if (type == MM_RSAHOSTKEY)
785 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
791 monitor_valid_userblob(u_char *data, u_int datalen)
797 int session_id2_len = 20 /*XXX should get from [net] */;
800 buffer_append(&b, data, datalen);
802 if (datafellows & SSH_OLD_SESSIONID) {
803 buffer_consume(&b, session_id2_len);
805 xfree(buffer_get_string(&b, &len));
806 if (len != session_id2_len)
809 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
811 p = buffer_get_string(&b, NULL);
812 if (strcmp(authctxt->user, p) != 0) {
813 log("wrong user name passed to monitor: expected %s != %.100s",
818 buffer_skip_string(&b);
819 if (datafellows & SSH_BUG_PKAUTH) {
820 if (!buffer_get_char(&b))
823 p = buffer_get_string(&b, NULL);
824 if (strcmp("publickey", p) != 0)
827 if (!buffer_get_char(&b))
829 buffer_skip_string(&b);
831 buffer_skip_string(&b);
832 if (buffer_len(&b) != 0)
839 monitor_valid_hostbasedblob(u_char *data, u_int datalen, u_char *cuser,
846 int session_id2_len = 20 /*XXX should get from [net] */;
849 buffer_append(&b, data, datalen);
851 xfree(buffer_get_string(&b, &len));
852 if (len != session_id2_len)
854 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
856 p = buffer_get_string(&b, NULL);
857 if (strcmp(authctxt->user, p) != 0) {
858 log("wrong user name passed to monitor: expected %s != %.100s",
863 buffer_skip_string(&b); /* service */
864 p = buffer_get_string(&b, NULL);
865 if (strcmp(p, "hostbased") != 0)
868 buffer_skip_string(&b); /* pkalg */
869 buffer_skip_string(&b); /* pkblob */
871 /* verify client host, strip trailing dot if necessary */
872 p = buffer_get_string(&b, NULL);
873 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
875 if (strcmp(p, chost) != 0)
879 /* verify client user */
880 p = buffer_get_string(&b, NULL);
881 if (strcmp(p, cuser) != 0)
885 if (buffer_len(&b) != 0)
892 mm_answer_keyverify(int socket, Buffer *m)
895 u_char *signature, *data, *blob;
896 u_int signaturelen, datalen, bloblen;
900 blob = buffer_get_string(m, &bloblen);
901 signature = buffer_get_string(m, &signaturelen);
902 data = buffer_get_string(m, &datalen);
904 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
905 !monitor_allowed_key(blob, bloblen))
906 fatal("%s: bad key, not previously allowed", __FUNCTION__);
908 key = key_from_blob(blob, bloblen);
910 fatal("%s: bad public key blob", __FUNCTION__);
912 switch (key_blobtype) {
914 valid_data = monitor_valid_userblob(data, datalen);
917 valid_data = monitor_valid_hostbasedblob(data, datalen,
918 hostbased_cuser, hostbased_chost);
925 fatal("%s: bad signature data blob", __FUNCTION__);
927 verified = key_verify(key, signature, signaturelen, data, datalen);
928 debug3("%s: key %p signature %s",
929 __FUNCTION__, key, verified ? "verified" : "unverified");
936 monitor_reset_key_state();
939 buffer_put_int(m, verified);
940 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
942 auth_method = "publickey";
948 mm_record_login(Session *s, struct passwd *pw)
951 struct sockaddr_storage from;
954 * Get IP address of client. If the connection is not a socket, let
955 * the address be 0.0.0.0.
957 memset(&from, 0, sizeof(from));
958 if (packet_connection_is_on_socket()) {
959 fromlen = sizeof(from);
960 if (getpeername(packet_get_connection_in(),
961 (struct sockaddr *) & from, &fromlen) < 0) {
962 debug("getpeername: %.100s", strerror(errno));
966 /* Record that there was a login on that tty from the remote host. */
967 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
968 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
969 (struct sockaddr *)&from);
973 mm_session_close(Session *s)
975 debug3("%s: session %d pid %d", __FUNCTION__, s->self, s->pid);
976 if (s->ttyfd != -1) {
977 debug3("%s: tty %s ptyfd %d", __FUNCTION__, s->tty, s->ptyfd);
978 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
979 session_pty_cleanup2(s);
985 mm_answer_pty(int socket, Buffer *m)
987 extern struct monitor *monitor;
991 debug3("%s entering", __FUNCTION__);
997 s->authctxt = authctxt;
998 s->pw = authctxt->pw;
999 s->pid = monitor->m_pid;
1000 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1003 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1004 pty_setowner(authctxt->pw, s->tty);
1006 buffer_put_int(m, 1);
1007 buffer_put_cstring(m, s->tty);
1008 mm_request_send(socket, MONITOR_ANS_PTY, m);
1010 mm_send_fd(socket, s->ptyfd);
1011 mm_send_fd(socket, s->ttyfd);
1013 /* We need to trick ttyslot */
1014 if (dup2(s->ttyfd, 0) == -1)
1015 fatal("%s: dup2", __FUNCTION__);
1017 mm_record_login(s, authctxt->pw);
1019 /* Now we can close the file descriptor again */
1022 /* make sure nothing uses fd 0 */
1023 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1024 fatal("%s: open(/dev/null): %s", __FUNCTION__, strerror(errno));
1026 error("%s: fd0 %d != 0", __FUNCTION__, fd0);
1028 /* slave is not needed */
1030 s->ttyfd = s->ptyfd;
1031 /* no need to dup() because nobody closes ptyfd */
1032 s->ptymaster = s->ptyfd;
1034 debug3("%s: tty %s ptyfd %d", __FUNCTION__, s->tty, s->ttyfd);
1040 mm_session_close(s);
1041 buffer_put_int(m, 0);
1042 mm_request_send(socket, MONITOR_ANS_PTY, m);
1047 mm_answer_pty_cleanup(int socket, Buffer *m)
1052 debug3("%s entering", __FUNCTION__);
1054 tty = buffer_get_string(m, NULL);
1055 if ((s = session_by_tty(tty)) != NULL)
1056 mm_session_close(s);
1063 mm_answer_sesskey(int socket, Buffer *m)
1068 /* Turn off permissions */
1069 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1071 if ((p = BN_new()) == NULL)
1072 fatal("%s: BN_new", __FUNCTION__);
1074 buffer_get_bignum2(m, p);
1076 rsafail = ssh1_session_key(p);
1079 buffer_put_int(m, rsafail);
1080 buffer_put_bignum2(m, p);
1084 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1086 /* Turn on permissions for sessid passing */
1087 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1093 mm_answer_sessid(int socket, Buffer *m)
1097 debug3("%s entering", __FUNCTION__);
1099 if (buffer_len(m) != 16)
1100 fatal("%s: bad ssh1 session id", __FUNCTION__);
1101 for (i = 0; i < 16; i++)
1102 session_id[i] = buffer_get_char(m);
1104 /* Turn on permissions for getpwnam */
1105 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1111 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1115 u_char *blob = NULL;
1119 debug3("%s entering", __FUNCTION__);
1121 if (authctxt->valid) {
1122 if ((client_n = BN_new()) == NULL)
1123 fatal("%s: BN_new", __FUNCTION__);
1124 buffer_get_bignum2(m, client_n);
1125 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1126 BN_clear_free(client_n);
1129 buffer_put_int(m, allowed);
1131 /* clear temporarily storage (used by generate challenge) */
1132 monitor_reset_key_state();
1134 if (allowed && key != NULL) {
1135 key->type = KEY_RSA; /* cheat for key_to_blob */
1136 if (key_to_blob(key, &blob, &blen) == 0)
1137 fatal("%s: key_to_blob failed", __FUNCTION__);
1138 buffer_put_string(m, blob, blen);
1140 /* Save temporarily for comparison in verify */
1143 key_blobtype = MM_RSAUSERKEY;
1149 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1151 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1152 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1157 mm_answer_rsa_challenge(int socket, Buffer *m)
1163 debug3("%s entering", __FUNCTION__);
1165 if (!authctxt->valid)
1166 fatal("%s: authctxt not valid", __FUNCTION__);
1167 blob = buffer_get_string(m, &blen);
1168 if (!monitor_allowed_key(blob, blen))
1169 fatal("%s: bad key, not previously allowed", __FUNCTION__);
1170 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1171 fatal("%s: key type mismatch", __FUNCTION__);
1172 if ((key = key_from_blob(blob, blen)) == NULL)
1173 fatal("%s: received bad key", __FUNCTION__);
1176 BN_clear_free(ssh1_challenge);
1177 ssh1_challenge = auth_rsa_generate_challenge(key);
1180 buffer_put_bignum2(m, ssh1_challenge);
1182 debug3("%s sending reply", __FUNCTION__);
1183 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1185 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1190 mm_answer_rsa_response(int socket, Buffer *m)
1193 u_char *blob, *response;
1197 debug3("%s entering", __FUNCTION__);
1199 if (!authctxt->valid)
1200 fatal("%s: authctxt not valid", __FUNCTION__);
1201 if (ssh1_challenge == NULL)
1202 fatal("%s: no ssh1_challenge", __FUNCTION__);
1204 blob = buffer_get_string(m, &blen);
1205 if (!monitor_allowed_key(blob, blen))
1206 fatal("%s: bad key, not previously allowed", __FUNCTION__);
1207 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1208 fatal("%s: key type mismatch: %d", __FUNCTION__, key_blobtype);
1209 if ((key = key_from_blob(blob, blen)) == NULL)
1210 fatal("%s: received bad key", __FUNCTION__);
1211 response = buffer_get_string(m, &len);
1213 fatal("%s: received bad response to challenge", __FUNCTION__);
1214 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1219 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1222 BN_clear_free(ssh1_challenge);
1223 ssh1_challenge = NULL;
1224 monitor_reset_key_state();
1227 buffer_put_int(m, success);
1228 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1234 mm_answer_term(int socket, Buffer *req)
1236 extern struct monitor *monitor;
1239 debug3("%s: tearing down sessions", __FUNCTION__);
1241 /* The child is terminating */
1242 session_destroy_all(&mm_session_close);
1244 while (waitpid(monitor->m_pid, &status, 0) == -1)
1248 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1250 /* Terminate process */
1255 monitor_apply_keystate(struct monitor *monitor)
1258 set_newkeys(MODE_IN);
1259 set_newkeys(MODE_OUT);
1261 u_char key[SSH_SESSION_KEY_LENGTH];
1263 memset(key, 'a', sizeof(key));
1264 packet_set_protocol_flags(child_state.ssh1protoflags);
1265 packet_set_encryption_key(key, SSH_SESSION_KEY_LENGTH,
1266 child_state.ssh1cipher);
1269 packet_set_keycontext(MODE_OUT, child_state.keyout);
1270 xfree(child_state.keyout);
1271 packet_set_keycontext(MODE_IN, child_state.keyin);
1272 xfree(child_state.keyin);
1275 packet_set_iv(MODE_OUT, child_state.ivout);
1276 xfree(child_state.ivout);
1277 packet_set_iv(MODE_IN, child_state.ivin);
1278 xfree(child_state.ivin);
1281 memcpy(&incoming_stream, &child_state.incoming,
1282 sizeof(incoming_stream));
1283 memcpy(&outgoing_stream, &child_state.outgoing,
1284 sizeof(outgoing_stream));
1286 /* Update with new address */
1287 mm_init_compression(monitor->m_zlib);
1289 /* Network I/O buffers */
1290 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1291 buffer_clear(&input);
1292 buffer_append(&input, child_state.input, child_state.ilen);
1293 memset(child_state.input, 0, child_state.ilen);
1294 xfree(child_state.input);
1296 buffer_clear(&output);
1297 buffer_append(&output, child_state.output, child_state.olen);
1298 memset(child_state.output, 0, child_state.olen);
1299 xfree(child_state.output);
1303 mm_get_kex(Buffer *m)
1309 kex = xmalloc(sizeof(*kex));
1310 memset(kex, 0, sizeof(*kex));
1311 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1312 kex->we_need = buffer_get_int(m);
1314 kex->hostkey_type = buffer_get_int(m);
1315 kex->kex_type = buffer_get_int(m);
1316 blob = buffer_get_string(m, &bloblen);
1317 buffer_init(&kex->my);
1318 buffer_append(&kex->my, blob, bloblen);
1320 blob = buffer_get_string(m, &bloblen);
1321 buffer_init(&kex->peer);
1322 buffer_append(&kex->peer, blob, bloblen);
1325 kex->flags = buffer_get_int(m);
1326 kex->client_version_string = buffer_get_string(m, NULL);
1327 kex->server_version_string = buffer_get_string(m, NULL);
1328 kex->load_host_key=&get_hostkey_by_type;
1329 kex->host_key_index=&get_hostkey_index;
1334 /* This function requries careful sanity checking */
1337 mm_get_keystate(struct monitor *monitor)
1341 u_int bloblen, plen;
1343 debug3("%s: Waiting for new keys", __FUNCTION__);
1346 mm_request_receive_expect(monitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1348 child_state.ssh1protoflags = buffer_get_int(&m);
1349 child_state.ssh1cipher = buffer_get_int(&m);
1350 child_state.ivout = buffer_get_string(&m,
1351 &child_state.ivoutlen);
1352 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1355 /* Get the Kex for rekeying */
1356 *monitor->m_pkex = mm_get_kex(&m);
1359 blob = buffer_get_string(&m, &bloblen);
1360 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1363 debug3("%s: Waiting for second key", __FUNCTION__);
1364 blob = buffer_get_string(&m, &bloblen);
1365 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1368 /* Now get sequence numbers for the packets */
1369 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1370 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1373 /* Get the key context */
1374 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1375 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1377 debug3("%s: Getting compression state", __FUNCTION__);
1378 /* Get compression state */
1379 p = buffer_get_string(&m, &plen);
1380 if (plen != sizeof(child_state.outgoing))
1381 fatal("%s: bad request size", __FUNCTION__);
1382 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1385 p = buffer_get_string(&m, &plen);
1386 if (plen != sizeof(child_state.incoming))
1387 fatal("%s: bad request size", __FUNCTION__);
1388 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1391 /* Network I/O buffers */
1392 debug3("%s: Getting Network I/O buffers", __FUNCTION__);
1393 child_state.input = buffer_get_string(&m, &child_state.ilen);
1394 child_state.output = buffer_get_string(&m, &child_state.olen);
1400 /* Allocation functions for zlib */
1402 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1406 address = mm_malloc(mm, size * ncount);
1412 mm_zfree(struct mm_master *mm, void *address)
1414 mm_free(mm, address);
1418 mm_init_compression(struct mm_master *mm)
1420 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1421 outgoing_stream.zfree = (free_func)mm_zfree;
1422 outgoing_stream.opaque = mm;
1424 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1425 incoming_stream.zfree = (free_func)mm_zfree;
1426 incoming_stream.opaque = mm;
1431 #define FD_CLOSEONEXEC(x) do { \
1432 if (fcntl(x, F_SETFD, 1) == -1) \
1433 fatal("fcntl(%d, F_SETFD)", x); \
1437 monitor_socketpair(int *pair)
1439 #ifdef HAVE_SOCKETPAIR
1440 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1441 fatal("%s: socketpair", __FUNCTION__);
1443 fatal("%s: UsePrivilegeSeparation=yes not supported",
1446 FD_CLOSEONEXEC(pair[0]);
1447 FD_CLOSEONEXEC(pair[1]);
1450 #define MM_MEMSIZE 65536
1455 struct monitor *mon;
1458 mon = xmalloc(sizeof(*mon));
1460 monitor_socketpair(pair);
1462 mon->m_recvfd = pair[0];
1463 mon->m_sendfd = pair[1];
1465 /* Used to share zlib space across processes */
1466 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1467 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1469 /* Compression needs to share state across borders */
1470 mm_init_compression(mon->m_zlib);
1476 monitor_reinit(struct monitor *mon)
1480 monitor_socketpair(pair);
1482 mon->m_recvfd = pair[0];
1483 mon->m_sendfd = pair[1];