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.11 2002/05/15 15:47:49 mouring 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_auth2_read_banner(int, Buffer *);
100 int mm_answer_authserv(int, Buffer *);
101 int mm_answer_authpassword(int, Buffer *);
102 int mm_answer_bsdauthquery(int, Buffer *);
103 int mm_answer_bsdauthrespond(int, Buffer *);
104 int mm_answer_skeyquery(int, Buffer *);
105 int mm_answer_skeyrespond(int, Buffer *);
106 int mm_answer_keyallowed(int, Buffer *);
107 int mm_answer_keyverify(int, Buffer *);
108 int mm_answer_pty(int, Buffer *);
109 int mm_answer_pty_cleanup(int, Buffer *);
110 int mm_answer_term(int, Buffer *);
111 int mm_answer_rsa_keyallowed(int, Buffer *);
112 int mm_answer_rsa_challenge(int, Buffer *);
113 int mm_answer_rsa_response(int, Buffer *);
114 int mm_answer_sesskey(int, Buffer *);
115 int mm_answer_sessid(int, Buffer *);
118 int mm_answer_pam_start(int, Buffer *);
121 static Authctxt *authctxt;
122 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
124 /* local state for key verify */
125 static u_char *key_blob = NULL;
126 static u_int key_bloblen = 0;
127 static int key_blobtype = MM_NOKEY;
128 static u_char *hostbased_cuser = NULL;
129 static u_char *hostbased_chost = NULL;
130 static char *auth_method = "unknown";
133 enum monitor_reqtype type;
135 int (*f)(int, Buffer *);
138 #define MON_ISAUTH 0x0004 /* Required for Authentication */
139 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
140 #define MON_ONCE 0x0010 /* Disable after calling */
142 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
144 #define MON_PERMIT 0x1000 /* Request is permitted */
146 struct mon_table mon_dispatch_proto20[] = {
147 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
148 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
149 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
150 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
151 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
152 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
154 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
157 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
158 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
161 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
162 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
164 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
165 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
169 struct mon_table mon_dispatch_postauth20[] = {
170 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
171 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
172 {MONITOR_REQ_PTY, 0, mm_answer_pty},
173 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
174 {MONITOR_REQ_TERM, 0, mm_answer_term},
178 struct mon_table mon_dispatch_proto15[] = {
179 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
180 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
181 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
182 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
183 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
184 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
185 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
186 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
188 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
191 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
192 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
195 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
196 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
199 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
204 struct mon_table mon_dispatch_postauth15[] = {
205 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
206 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
207 {MONITOR_REQ_TERM, 0, mm_answer_term},
211 struct mon_table *mon_dispatch;
213 /* Specifies if a certain message is allowed at the moment */
216 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
218 while (ent->f != NULL) {
219 if (ent->type == type) {
220 ent->flags &= ~MON_PERMIT;
221 ent->flags |= permit ? MON_PERMIT : 0;
229 monitor_permit_authentications(int permit)
231 struct mon_table *ent = mon_dispatch;
233 while (ent->f != NULL) {
234 if (ent->flags & MON_AUTH) {
235 ent->flags &= ~MON_PERMIT;
236 ent->flags |= permit ? MON_PERMIT : 0;
243 monitor_child_preauth(struct monitor *pmonitor)
245 struct mon_table *ent;
246 int authenticated = 0;
248 debug3("preauth child monitor started");
251 mon_dispatch = mon_dispatch_proto20;
253 /* Permit requests for moduli and signatures */
254 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
255 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
257 mon_dispatch = mon_dispatch_proto15;
259 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
262 authctxt = authctxt_new();
264 /* The first few requests do not require asynchronous access */
265 while (!authenticated) {
266 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
268 if (!(ent->flags & MON_AUTHDECIDE))
269 fatal("%s: unexpected authentication from %d",
270 __FUNCTION__, ent->type);
271 if (authctxt->pw->pw_uid == 0 &&
272 !auth_root_allowed(auth_method))
275 if (!do_pam_account(authctxt->pw->pw_name, NULL))
280 if (ent->flags & MON_AUTHDECIDE) {
281 auth_log(authctxt, authenticated, auth_method,
282 compat20 ? " ssh2" : "");
284 authctxt->failures++;
288 if (!authctxt->valid)
289 fatal("%s: authenticated invalid user", __FUNCTION__);
291 debug("%s: %s has been authenticated by privileged process",
292 __FUNCTION__, authctxt->user);
294 mm_get_keystate(pmonitor);
300 monitor_child_postauth(struct monitor *pmonitor)
303 mon_dispatch = mon_dispatch_postauth20;
305 /* Permit requests for moduli and signatures */
306 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
307 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
308 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
311 mon_dispatch = mon_dispatch_postauth15;
312 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
315 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
316 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
320 monitor_read(pmonitor, mon_dispatch, NULL);
324 monitor_sync(struct monitor *pmonitor)
326 /* The member allocation is not visible, so sync it */
327 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
331 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
332 struct mon_table **pent)
340 mm_request_receive(pmonitor->m_sendfd, &m);
341 type = buffer_get_char(&m);
343 debug3("%s: checking request %d", __FUNCTION__, type);
345 while (ent->f != NULL) {
346 if (ent->type == type)
351 if (ent->f != NULL) {
352 if (!(ent->flags & MON_PERMIT))
353 fatal("%s: unpermitted request %d", __FUNCTION__,
355 ret = (*ent->f)(pmonitor->m_sendfd, &m);
358 /* The child may use this request only once, disable it */
359 if (ent->flags & MON_ONCE) {
360 debug2("%s: %d used once, disabling now", __FUNCTION__,
362 ent->flags &= ~MON_PERMIT;
371 fatal("%s: unsupported request: %d", __FUNCTION__, type);
377 /* allowed key state */
379 monitor_allowed_key(u_char *blob, u_int bloblen)
381 /* make sure key is allowed */
382 if (key_blob == NULL || key_bloblen != bloblen ||
383 memcmp(key_blob, blob, key_bloblen))
389 monitor_reset_key_state(void)
392 if (key_blob != NULL)
394 if (hostbased_cuser != NULL)
395 xfree(hostbased_cuser);
396 if (hostbased_chost != NULL)
397 xfree(hostbased_chost);
400 key_blobtype = MM_NOKEY;
401 hostbased_cuser = NULL;
402 hostbased_chost = NULL;
406 mm_answer_moduli(int socket, Buffer *m)
411 min = buffer_get_int(m);
412 want = buffer_get_int(m);
413 max = buffer_get_int(m);
415 debug3("%s: got parameters: %d %d %d",
416 __FUNCTION__, min, want, max);
417 /* We need to check here, too, in case the child got corrupted */
418 if (max < min || want < min || max < want)
419 fatal("%s: bad parameters: %d %d %d",
420 __FUNCTION__, min, want, max);
424 dh = choose_dh(min, want, max);
426 buffer_put_char(m, 0);
429 /* Send first bignum */
430 buffer_put_char(m, 1);
431 buffer_put_bignum2(m, dh->p);
432 buffer_put_bignum2(m, dh->g);
436 mm_request_send(socket, MONITOR_ANS_MODULI, m);
441 mm_answer_sign(int socket, Buffer *m)
446 u_int siglen, datlen;
449 debug3("%s", __FUNCTION__);
451 keyid = buffer_get_int(m);
452 p = buffer_get_string(m, &datlen);
455 fatal("%s: data length incorrect: %d", __FUNCTION__, datlen);
457 if ((key = get_hostkey_by_index(keyid)) == NULL)
458 fatal("%s: no hostkey from index %d", __FUNCTION__, keyid);
459 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
460 fatal("%s: key_sign failed", __FUNCTION__);
462 debug3("%s: signature %p(%d)", __FUNCTION__, signature, siglen);
465 buffer_put_string(m, signature, siglen);
470 mm_request_send(socket, MONITOR_ANS_SIGN, m);
472 /* Turn on permissions for getpwnam */
473 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
478 /* Retrieves the password entry and also checks if the user is permitted */
481 mm_answer_pwnamallow(int socket, Buffer *m)
484 struct passwd *pwent;
487 debug3("%s", __FUNCTION__);
489 if (authctxt->attempt++ != 0)
490 fatal("%s: multiple attempts for getpwnam", __FUNCTION__);
492 login = buffer_get_string(m, NULL);
494 pwent = getpwnamallow(login);
496 authctxt->user = xstrdup(login);
497 setproctitle("%s [priv]", pwent ? login : "unknown");
503 buffer_put_char(m, 0);
508 authctxt->pw = pwent;
511 buffer_put_char(m, 1);
512 buffer_put_string(m, pwent, sizeof(struct passwd));
513 buffer_put_cstring(m, pwent->pw_name);
514 buffer_put_cstring(m, "*");
515 buffer_put_cstring(m, pwent->pw_gecos);
516 #ifdef HAVE_PW_CLASS_IN_PASSWD
517 buffer_put_cstring(m, pwent->pw_class);
519 buffer_put_cstring(m, pwent->pw_dir);
520 buffer_put_cstring(m, pwent->pw_shell);
523 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __FUNCTION__, allowed);
524 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
526 /* For SSHv1 allow authentication now */
528 monitor_permit_authentications(1);
530 /* Allow service/style information on the auth context */
531 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
532 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
536 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
542 int mm_answer_auth2_read_banner(int socket, Buffer *m)
547 banner = auth2_read_banner();
548 buffer_put_cstring(m, banner != NULL ? banner : "");
549 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
558 mm_answer_authserv(int socket, Buffer *m)
560 monitor_permit_authentications(1);
562 authctxt->service = buffer_get_string(m, NULL);
563 authctxt->style = buffer_get_string(m, NULL);
564 debug3("%s: service=%s, style=%s",
565 __FUNCTION__, authctxt->service, authctxt->style);
567 if (strlen(authctxt->style) == 0) {
568 xfree(authctxt->style);
569 authctxt->style = NULL;
576 mm_answer_authpassword(int socket, Buffer *m)
578 static int call_count;
580 int authenticated, plen;
582 passwd = buffer_get_string(m, &plen);
583 /* Only authenticate if the context is valid */
584 authenticated = authctxt->valid && auth_password(authctxt, passwd);
585 memset(passwd, 0, strlen(passwd));
589 buffer_put_int(m, authenticated);
591 debug3("%s: sending result %d", __FUNCTION__, authenticated);
592 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
595 if (plen == 0 && call_count == 1)
596 auth_method = "none";
598 auth_method = "password";
600 /* Causes monitor loop to terminate if authenticated */
601 return (authenticated);
606 mm_answer_bsdauthquery(int socket, Buffer *m)
608 char *name, *infotxt;
614 res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
618 buffer_put_int(m, res);
620 buffer_put_cstring(m, prompts[0]);
622 debug3("%s: sending challenge res: %d", __FUNCTION__, res);
623 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
636 mm_answer_bsdauthrespond(int socket, Buffer *m)
641 if (authctxt->as == 0)
642 fatal("%s: no bsd auth session", __FUNCTION__);
644 response = buffer_get_string(m, NULL);
645 authok = auth_userresponse(authctxt->as, response, 0);
647 debug3("%s: <%s> = <%d>", __FUNCTION__, response, authok);
651 buffer_put_int(m, authok);
653 debug3("%s: sending authenticated: %d", __FUNCTION__, authok);
654 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
656 auth_method = "bsdauth";
658 return (authok != 0);
664 mm_answer_skeyquery(int socket, Buffer *m)
667 char challenge[1024];
670 res = skeychallenge(&skey, authctxt->user, challenge);
673 buffer_put_int(m, res);
675 buffer_put_cstring(m, challenge);
677 debug3("%s: sending challenge res: %d", __FUNCTION__, res);
678 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
684 mm_answer_skeyrespond(int socket, Buffer *m)
689 response = buffer_get_string(m, NULL);
691 authok = (authctxt->valid &&
692 skey_haskey(authctxt->pw->pw_name) == 0 &&
693 skey_passcheck(authctxt->pw->pw_name, response) != -1);
698 buffer_put_int(m, authok);
700 debug3("%s: sending authenticated: %d", __FUNCTION__, authok);
701 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
703 auth_method = "skey";
705 return (authok != 0);
711 mm_answer_pam_start(int socket, Buffer *m)
715 user = buffer_get_string(m, NULL);
726 mm_append_debug(Buffer *m)
728 if (auth_debug_init && buffer_len(&auth_debug)) {
729 debug3("%s: Appending debug messages for child", __FUNCTION__);
730 buffer_append(m, buffer_ptr(&auth_debug),
731 buffer_len(&auth_debug));
732 buffer_clear(&auth_debug);
737 mm_answer_keyallowed(int socket, Buffer *m)
740 u_char *cuser, *chost, *blob;
742 enum mm_keytype type = 0;
745 debug3("%s entering", __FUNCTION__);
747 type = buffer_get_int(m);
748 cuser = buffer_get_string(m, NULL);
749 chost = buffer_get_string(m, NULL);
750 blob = buffer_get_string(m, &bloblen);
752 key = key_from_blob(blob, bloblen);
754 if ((compat20 && type == MM_RSAHOSTKEY) ||
755 (!compat20 && type != MM_RSAHOSTKEY))
756 fatal("%s: key type and protocol mismatch", __FUNCTION__);
758 debug3("%s: key_from_blob: %p", __FUNCTION__, key);
760 if (key != NULL && authctxt->pw != NULL) {
763 allowed = user_key_allowed(authctxt->pw, key);
766 allowed = hostbased_key_allowed(authctxt->pw,
770 key->type = KEY_RSA1; /* XXX */
771 allowed = auth_rhosts_rsa_key_allowed(authctxt->pw,
775 fatal("%s: unknown key type %d", __FUNCTION__, type);
781 /* clear temporarily storage (used by verify) */
782 monitor_reset_key_state();
785 /* Save temporarily for comparison in verify */
787 key_bloblen = bloblen;
789 hostbased_cuser = cuser;
790 hostbased_chost = chost;
793 debug3("%s: key %p is %s",
794 __FUNCTION__, key, allowed ? "allowed" : "disallowed");
797 buffer_put_int(m, allowed);
801 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
803 if (type == MM_RSAHOSTKEY)
804 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
810 monitor_valid_userblob(u_char *data, u_int datalen)
816 int session_id2_len = 20 /*XXX should get from [net] */;
819 buffer_append(&b, data, datalen);
821 if (datafellows & SSH_OLD_SESSIONID) {
822 buffer_consume(&b, session_id2_len);
824 xfree(buffer_get_string(&b, &len));
825 if (len != session_id2_len)
828 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
830 p = buffer_get_string(&b, NULL);
831 if (strcmp(authctxt->user, p) != 0) {
832 log("wrong user name passed to monitor: expected %s != %.100s",
837 buffer_skip_string(&b);
838 if (datafellows & SSH_BUG_PKAUTH) {
839 if (!buffer_get_char(&b))
842 p = buffer_get_string(&b, NULL);
843 if (strcmp("publickey", p) != 0)
846 if (!buffer_get_char(&b))
848 buffer_skip_string(&b);
850 buffer_skip_string(&b);
851 if (buffer_len(&b) != 0)
858 monitor_valid_hostbasedblob(u_char *data, u_int datalen, u_char *cuser,
865 int session_id2_len = 20 /*XXX should get from [net] */;
868 buffer_append(&b, data, datalen);
870 xfree(buffer_get_string(&b, &len));
871 if (len != session_id2_len)
873 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
875 p = buffer_get_string(&b, NULL);
876 if (strcmp(authctxt->user, p) != 0) {
877 log("wrong user name passed to monitor: expected %s != %.100s",
882 buffer_skip_string(&b); /* service */
883 p = buffer_get_string(&b, NULL);
884 if (strcmp(p, "hostbased") != 0)
887 buffer_skip_string(&b); /* pkalg */
888 buffer_skip_string(&b); /* pkblob */
890 /* verify client host, strip trailing dot if necessary */
891 p = buffer_get_string(&b, NULL);
892 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
894 if (strcmp(p, chost) != 0)
898 /* verify client user */
899 p = buffer_get_string(&b, NULL);
900 if (strcmp(p, cuser) != 0)
904 if (buffer_len(&b) != 0)
911 mm_answer_keyverify(int socket, Buffer *m)
914 u_char *signature, *data, *blob;
915 u_int signaturelen, datalen, bloblen;
919 blob = buffer_get_string(m, &bloblen);
920 signature = buffer_get_string(m, &signaturelen);
921 data = buffer_get_string(m, &datalen);
923 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
924 !monitor_allowed_key(blob, bloblen))
925 fatal("%s: bad key, not previously allowed", __FUNCTION__);
927 key = key_from_blob(blob, bloblen);
929 fatal("%s: bad public key blob", __FUNCTION__);
931 switch (key_blobtype) {
933 valid_data = monitor_valid_userblob(data, datalen);
936 valid_data = monitor_valid_hostbasedblob(data, datalen,
937 hostbased_cuser, hostbased_chost);
944 fatal("%s: bad signature data blob", __FUNCTION__);
946 verified = key_verify(key, signature, signaturelen, data, datalen);
947 debug3("%s: key %p signature %s",
948 __FUNCTION__, key, verified ? "verified" : "unverified");
955 monitor_reset_key_state();
958 buffer_put_int(m, verified);
959 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
961 auth_method = "publickey";
967 mm_record_login(Session *s, struct passwd *pw)
970 struct sockaddr_storage from;
973 * Get IP address of client. If the connection is not a socket, let
974 * the address be 0.0.0.0.
976 memset(&from, 0, sizeof(from));
977 if (packet_connection_is_on_socket()) {
978 fromlen = sizeof(from);
979 if (getpeername(packet_get_connection_in(),
980 (struct sockaddr *) & from, &fromlen) < 0) {
981 debug("getpeername: %.100s", strerror(errno));
985 /* Record that there was a login on that tty from the remote host. */
986 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
987 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
988 (struct sockaddr *)&from);
992 mm_session_close(Session *s)
994 debug3("%s: session %d pid %d", __FUNCTION__, s->self, s->pid);
995 if (s->ttyfd != -1) {
996 debug3("%s: tty %s ptyfd %d", __FUNCTION__, s->tty, s->ptyfd);
997 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
998 session_pty_cleanup2(s);
1004 mm_answer_pty(int socket, Buffer *m)
1006 extern struct monitor *pmonitor;
1010 debug3("%s entering", __FUNCTION__);
1016 s->authctxt = authctxt;
1017 s->pw = authctxt->pw;
1018 s->pid = pmonitor->m_pid;
1019 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1022 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1023 pty_setowner(authctxt->pw, s->tty);
1025 buffer_put_int(m, 1);
1026 buffer_put_cstring(m, s->tty);
1027 mm_request_send(socket, MONITOR_ANS_PTY, m);
1029 mm_send_fd(socket, s->ptyfd);
1030 mm_send_fd(socket, s->ttyfd);
1032 /* We need to trick ttyslot */
1033 if (dup2(s->ttyfd, 0) == -1)
1034 fatal("%s: dup2", __FUNCTION__);
1036 mm_record_login(s, authctxt->pw);
1038 /* Now we can close the file descriptor again */
1041 /* make sure nothing uses fd 0 */
1042 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1043 fatal("%s: open(/dev/null): %s", __FUNCTION__, strerror(errno));
1045 error("%s: fd0 %d != 0", __FUNCTION__, fd0);
1047 /* slave is not needed */
1049 s->ttyfd = s->ptyfd;
1050 /* no need to dup() because nobody closes ptyfd */
1051 s->ptymaster = s->ptyfd;
1053 debug3("%s: tty %s ptyfd %d", __FUNCTION__, s->tty, s->ttyfd);
1059 mm_session_close(s);
1060 buffer_put_int(m, 0);
1061 mm_request_send(socket, MONITOR_ANS_PTY, m);
1066 mm_answer_pty_cleanup(int socket, Buffer *m)
1071 debug3("%s entering", __FUNCTION__);
1073 tty = buffer_get_string(m, NULL);
1074 if ((s = session_by_tty(tty)) != NULL)
1075 mm_session_close(s);
1082 mm_answer_sesskey(int socket, Buffer *m)
1087 /* Turn off permissions */
1088 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1090 if ((p = BN_new()) == NULL)
1091 fatal("%s: BN_new", __FUNCTION__);
1093 buffer_get_bignum2(m, p);
1095 rsafail = ssh1_session_key(p);
1098 buffer_put_int(m, rsafail);
1099 buffer_put_bignum2(m, p);
1103 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1105 /* Turn on permissions for sessid passing */
1106 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1112 mm_answer_sessid(int socket, Buffer *m)
1116 debug3("%s entering", __FUNCTION__);
1118 if (buffer_len(m) != 16)
1119 fatal("%s: bad ssh1 session id", __FUNCTION__);
1120 for (i = 0; i < 16; i++)
1121 session_id[i] = buffer_get_char(m);
1123 /* Turn on permissions for getpwnam */
1124 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1130 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1134 u_char *blob = NULL;
1138 debug3("%s entering", __FUNCTION__);
1140 if (authctxt->valid) {
1141 if ((client_n = BN_new()) == NULL)
1142 fatal("%s: BN_new", __FUNCTION__);
1143 buffer_get_bignum2(m, client_n);
1144 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1145 BN_clear_free(client_n);
1148 buffer_put_int(m, allowed);
1150 /* clear temporarily storage (used by generate challenge) */
1151 monitor_reset_key_state();
1153 if (allowed && key != NULL) {
1154 key->type = KEY_RSA; /* cheat for key_to_blob */
1155 if (key_to_blob(key, &blob, &blen) == 0)
1156 fatal("%s: key_to_blob failed", __FUNCTION__);
1157 buffer_put_string(m, blob, blen);
1159 /* Save temporarily for comparison in verify */
1162 key_blobtype = MM_RSAUSERKEY;
1168 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1170 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1171 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1176 mm_answer_rsa_challenge(int socket, Buffer *m)
1182 debug3("%s entering", __FUNCTION__);
1184 if (!authctxt->valid)
1185 fatal("%s: authctxt not valid", __FUNCTION__);
1186 blob = buffer_get_string(m, &blen);
1187 if (!monitor_allowed_key(blob, blen))
1188 fatal("%s: bad key, not previously allowed", __FUNCTION__);
1189 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1190 fatal("%s: key type mismatch", __FUNCTION__);
1191 if ((key = key_from_blob(blob, blen)) == NULL)
1192 fatal("%s: received bad key", __FUNCTION__);
1195 BN_clear_free(ssh1_challenge);
1196 ssh1_challenge = auth_rsa_generate_challenge(key);
1199 buffer_put_bignum2(m, ssh1_challenge);
1201 debug3("%s sending reply", __FUNCTION__);
1202 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1204 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1209 mm_answer_rsa_response(int socket, Buffer *m)
1212 u_char *blob, *response;
1216 debug3("%s entering", __FUNCTION__);
1218 if (!authctxt->valid)
1219 fatal("%s: authctxt not valid", __FUNCTION__);
1220 if (ssh1_challenge == NULL)
1221 fatal("%s: no ssh1_challenge", __FUNCTION__);
1223 blob = buffer_get_string(m, &blen);
1224 if (!monitor_allowed_key(blob, blen))
1225 fatal("%s: bad key, not previously allowed", __FUNCTION__);
1226 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1227 fatal("%s: key type mismatch: %d", __FUNCTION__, key_blobtype);
1228 if ((key = key_from_blob(blob, blen)) == NULL)
1229 fatal("%s: received bad key", __FUNCTION__);
1230 response = buffer_get_string(m, &len);
1232 fatal("%s: received bad response to challenge", __FUNCTION__);
1233 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1238 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1241 BN_clear_free(ssh1_challenge);
1242 ssh1_challenge = NULL;
1243 monitor_reset_key_state();
1246 buffer_put_int(m, success);
1247 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1253 mm_answer_term(int socket, Buffer *req)
1255 extern struct monitor *pmonitor;
1258 debug3("%s: tearing down sessions", __FUNCTION__);
1260 /* The child is terminating */
1261 session_destroy_all(&mm_session_close);
1263 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1267 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1269 /* Terminate process */
1274 monitor_apply_keystate(struct monitor *pmonitor)
1277 set_newkeys(MODE_IN);
1278 set_newkeys(MODE_OUT);
1280 u_char key[SSH_SESSION_KEY_LENGTH];
1282 memset(key, 'a', sizeof(key));
1283 packet_set_protocol_flags(child_state.ssh1protoflags);
1284 packet_set_encryption_key(key, SSH_SESSION_KEY_LENGTH,
1285 child_state.ssh1cipher);
1288 packet_set_keycontext(MODE_OUT, child_state.keyout);
1289 xfree(child_state.keyout);
1290 packet_set_keycontext(MODE_IN, child_state.keyin);
1291 xfree(child_state.keyin);
1294 packet_set_iv(MODE_OUT, child_state.ivout);
1295 xfree(child_state.ivout);
1296 packet_set_iv(MODE_IN, child_state.ivin);
1297 xfree(child_state.ivin);
1300 memcpy(&incoming_stream, &child_state.incoming,
1301 sizeof(incoming_stream));
1302 memcpy(&outgoing_stream, &child_state.outgoing,
1303 sizeof(outgoing_stream));
1305 /* Update with new address */
1306 mm_init_compression(pmonitor->m_zlib);
1308 /* Network I/O buffers */
1309 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1310 buffer_clear(&input);
1311 buffer_append(&input, child_state.input, child_state.ilen);
1312 memset(child_state.input, 0, child_state.ilen);
1313 xfree(child_state.input);
1315 buffer_clear(&output);
1316 buffer_append(&output, child_state.output, child_state.olen);
1317 memset(child_state.output, 0, child_state.olen);
1318 xfree(child_state.output);
1322 mm_get_kex(Buffer *m)
1328 kex = xmalloc(sizeof(*kex));
1329 memset(kex, 0, sizeof(*kex));
1330 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1331 kex->we_need = buffer_get_int(m);
1333 kex->hostkey_type = buffer_get_int(m);
1334 kex->kex_type = buffer_get_int(m);
1335 blob = buffer_get_string(m, &bloblen);
1336 buffer_init(&kex->my);
1337 buffer_append(&kex->my, blob, bloblen);
1339 blob = buffer_get_string(m, &bloblen);
1340 buffer_init(&kex->peer);
1341 buffer_append(&kex->peer, blob, bloblen);
1344 kex->flags = buffer_get_int(m);
1345 kex->client_version_string = buffer_get_string(m, NULL);
1346 kex->server_version_string = buffer_get_string(m, NULL);
1347 kex->load_host_key=&get_hostkey_by_type;
1348 kex->host_key_index=&get_hostkey_index;
1353 /* This function requries careful sanity checking */
1356 mm_get_keystate(struct monitor *pmonitor)
1360 u_int bloblen, plen;
1362 debug3("%s: Waiting for new keys", __FUNCTION__);
1365 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1367 child_state.ssh1protoflags = buffer_get_int(&m);
1368 child_state.ssh1cipher = buffer_get_int(&m);
1369 child_state.ivout = buffer_get_string(&m,
1370 &child_state.ivoutlen);
1371 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1374 /* Get the Kex for rekeying */
1375 *pmonitor->m_pkex = mm_get_kex(&m);
1378 blob = buffer_get_string(&m, &bloblen);
1379 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1382 debug3("%s: Waiting for second key", __FUNCTION__);
1383 blob = buffer_get_string(&m, &bloblen);
1384 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1387 /* Now get sequence numbers for the packets */
1388 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1389 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1392 /* Get the key context */
1393 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1394 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1396 debug3("%s: Getting compression state", __FUNCTION__);
1397 /* Get compression state */
1398 p = buffer_get_string(&m, &plen);
1399 if (plen != sizeof(child_state.outgoing))
1400 fatal("%s: bad request size", __FUNCTION__);
1401 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1404 p = buffer_get_string(&m, &plen);
1405 if (plen != sizeof(child_state.incoming))
1406 fatal("%s: bad request size", __FUNCTION__);
1407 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1410 /* Network I/O buffers */
1411 debug3("%s: Getting Network I/O buffers", __FUNCTION__);
1412 child_state.input = buffer_get_string(&m, &child_state.ilen);
1413 child_state.output = buffer_get_string(&m, &child_state.olen);
1419 /* Allocation functions for zlib */
1421 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1425 address = mm_malloc(mm, size * ncount);
1431 mm_zfree(struct mm_master *mm, void *address)
1433 mm_free(mm, address);
1437 mm_init_compression(struct mm_master *mm)
1439 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1440 outgoing_stream.zfree = (free_func)mm_zfree;
1441 outgoing_stream.opaque = mm;
1443 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1444 incoming_stream.zfree = (free_func)mm_zfree;
1445 incoming_stream.opaque = mm;
1450 #define FD_CLOSEONEXEC(x) do { \
1451 if (fcntl(x, F_SETFD, 1) == -1) \
1452 fatal("fcntl(%d, F_SETFD)", x); \
1456 monitor_socketpair(int *pair)
1458 #ifdef HAVE_SOCKETPAIR
1459 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1460 fatal("%s: socketpair", __FUNCTION__);
1462 fatal("%s: UsePrivilegeSeparation=yes not supported",
1465 FD_CLOSEONEXEC(pair[0]);
1466 FD_CLOSEONEXEC(pair[1]);
1469 #define MM_MEMSIZE 65536
1474 struct monitor *mon;
1477 mon = xmalloc(sizeof(*mon));
1479 monitor_socketpair(pair);
1481 mon->m_recvfd = pair[0];
1482 mon->m_sendfd = pair[1];
1484 /* Used to share zlib space across processes */
1485 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1486 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1488 /* Compression needs to share state across borders */
1489 mm_init_compression(mon->m_zlib);
1495 monitor_reinit(struct monitor *mon)
1499 monitor_socketpair(pair);
1501 mon->m_recvfd = pair[0];
1502 mon->m_sendfd = pair[1];