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.13 2002/06/04 19:53:40 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_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";
131 static int session_id2_len = 0;
132 static u_char *session_id2 = NULL;
135 enum monitor_reqtype type;
137 int (*f)(int, Buffer *);
140 #define MON_ISAUTH 0x0004 /* Required for Authentication */
141 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
142 #define MON_ONCE 0x0010 /* Disable after calling */
144 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
146 #define MON_PERMIT 0x1000 /* Request is permitted */
148 struct mon_table mon_dispatch_proto20[] = {
149 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
150 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
151 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
152 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
153 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
154 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
156 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
159 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
160 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
163 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
164 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
166 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
167 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
171 struct mon_table mon_dispatch_postauth20[] = {
172 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
173 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
174 {MONITOR_REQ_PTY, 0, mm_answer_pty},
175 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
176 {MONITOR_REQ_TERM, 0, mm_answer_term},
180 struct mon_table mon_dispatch_proto15[] = {
181 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
182 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
183 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
184 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
185 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
186 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
187 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
188 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
190 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
193 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
194 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
197 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
198 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
201 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
206 struct mon_table mon_dispatch_postauth15[] = {
207 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
208 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
209 {MONITOR_REQ_TERM, 0, mm_answer_term},
213 struct mon_table *mon_dispatch;
215 /* Specifies if a certain message is allowed at the moment */
218 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
220 while (ent->f != NULL) {
221 if (ent->type == type) {
222 ent->flags &= ~MON_PERMIT;
223 ent->flags |= permit ? MON_PERMIT : 0;
231 monitor_permit_authentications(int permit)
233 struct mon_table *ent = mon_dispatch;
235 while (ent->f != NULL) {
236 if (ent->flags & MON_AUTH) {
237 ent->flags &= ~MON_PERMIT;
238 ent->flags |= permit ? MON_PERMIT : 0;
245 monitor_child_preauth(struct monitor *pmonitor)
247 struct mon_table *ent;
248 int authenticated = 0;
250 debug3("preauth child monitor started");
253 mon_dispatch = mon_dispatch_proto20;
255 /* Permit requests for moduli and signatures */
256 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
257 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
259 mon_dispatch = mon_dispatch_proto15;
261 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
264 authctxt = authctxt_new();
266 /* The first few requests do not require asynchronous access */
267 while (!authenticated) {
268 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
270 if (!(ent->flags & MON_AUTHDECIDE))
271 fatal("%s: unexpected authentication from %d",
272 __FUNCTION__, ent->type);
273 if (authctxt->pw->pw_uid == 0 &&
274 !auth_root_allowed(auth_method))
277 if (!do_pam_account(authctxt->pw->pw_name, NULL))
282 if (ent->flags & MON_AUTHDECIDE) {
283 auth_log(authctxt, authenticated, auth_method,
284 compat20 ? " ssh2" : "");
286 authctxt->failures++;
290 if (!authctxt->valid)
291 fatal("%s: authenticated invalid user", __FUNCTION__);
293 debug("%s: %s has been authenticated by privileged process",
294 __FUNCTION__, authctxt->user);
296 mm_get_keystate(pmonitor);
302 monitor_child_postauth(struct monitor *pmonitor)
305 mon_dispatch = mon_dispatch_postauth20;
307 /* Permit requests for moduli and signatures */
308 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
309 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
310 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
313 mon_dispatch = mon_dispatch_postauth15;
314 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
317 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
318 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
322 monitor_read(pmonitor, mon_dispatch, NULL);
326 monitor_sync(struct monitor *pmonitor)
328 /* The member allocation is not visible, so sync it */
329 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
333 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
334 struct mon_table **pent)
342 mm_request_receive(pmonitor->m_sendfd, &m);
343 type = buffer_get_char(&m);
345 debug3("%s: checking request %d", __FUNCTION__, type);
347 while (ent->f != NULL) {
348 if (ent->type == type)
353 if (ent->f != NULL) {
354 if (!(ent->flags & MON_PERMIT))
355 fatal("%s: unpermitted request %d", __FUNCTION__,
357 ret = (*ent->f)(pmonitor->m_sendfd, &m);
360 /* The child may use this request only once, disable it */
361 if (ent->flags & MON_ONCE) {
362 debug2("%s: %d used once, disabling now", __FUNCTION__,
364 ent->flags &= ~MON_PERMIT;
373 fatal("%s: unsupported request: %d", __FUNCTION__, type);
379 /* allowed key state */
381 monitor_allowed_key(u_char *blob, u_int bloblen)
383 /* make sure key is allowed */
384 if (key_blob == NULL || key_bloblen != bloblen ||
385 memcmp(key_blob, blob, key_bloblen))
391 monitor_reset_key_state(void)
394 if (key_blob != NULL)
396 if (hostbased_cuser != NULL)
397 xfree(hostbased_cuser);
398 if (hostbased_chost != NULL)
399 xfree(hostbased_chost);
402 key_blobtype = MM_NOKEY;
403 hostbased_cuser = NULL;
404 hostbased_chost = NULL;
408 mm_answer_moduli(int socket, Buffer *m)
413 min = buffer_get_int(m);
414 want = buffer_get_int(m);
415 max = buffer_get_int(m);
417 debug3("%s: got parameters: %d %d %d",
418 __FUNCTION__, min, want, max);
419 /* We need to check here, too, in case the child got corrupted */
420 if (max < min || want < min || max < want)
421 fatal("%s: bad parameters: %d %d %d",
422 __FUNCTION__, min, want, max);
426 dh = choose_dh(min, want, max);
428 buffer_put_char(m, 0);
431 /* Send first bignum */
432 buffer_put_char(m, 1);
433 buffer_put_bignum2(m, dh->p);
434 buffer_put_bignum2(m, dh->g);
438 mm_request_send(socket, MONITOR_ANS_MODULI, m);
443 mm_answer_sign(int socket, Buffer *m)
448 u_int siglen, datlen;
451 debug3("%s", __FUNCTION__);
453 keyid = buffer_get_int(m);
454 p = buffer_get_string(m, &datlen);
457 fatal("%s: data length incorrect: %d", __FUNCTION__, datlen);
459 /* save session id, it will be passed on the first call */
460 if (session_id2_len == 0) {
461 session_id2_len = datlen;
462 session_id2 = xmalloc(session_id2_len);
463 memcpy(session_id2, p, session_id2_len);
466 if ((key = get_hostkey_by_index(keyid)) == NULL)
467 fatal("%s: no hostkey from index %d", __FUNCTION__, keyid);
468 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
469 fatal("%s: key_sign failed", __FUNCTION__);
471 debug3("%s: signature %p(%d)", __FUNCTION__, signature, siglen);
474 buffer_put_string(m, signature, siglen);
479 mm_request_send(socket, MONITOR_ANS_SIGN, m);
481 /* Turn on permissions for getpwnam */
482 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
487 /* Retrieves the password entry and also checks if the user is permitted */
490 mm_answer_pwnamallow(int socket, Buffer *m)
493 struct passwd *pwent;
496 debug3("%s", __FUNCTION__);
498 if (authctxt->attempt++ != 0)
499 fatal("%s: multiple attempts for getpwnam", __FUNCTION__);
501 login = buffer_get_string(m, NULL);
503 pwent = getpwnamallow(login);
505 authctxt->user = xstrdup(login);
506 setproctitle("%s [priv]", pwent ? login : "unknown");
512 buffer_put_char(m, 0);
517 authctxt->pw = pwent;
520 buffer_put_char(m, 1);
521 buffer_put_string(m, pwent, sizeof(struct passwd));
522 buffer_put_cstring(m, pwent->pw_name);
523 buffer_put_cstring(m, "*");
524 buffer_put_cstring(m, pwent->pw_gecos);
525 #ifdef HAVE_PW_CLASS_IN_PASSWD
526 buffer_put_cstring(m, pwent->pw_class);
528 buffer_put_cstring(m, pwent->pw_dir);
529 buffer_put_cstring(m, pwent->pw_shell);
532 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __FUNCTION__, allowed);
533 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
535 /* For SSHv1 allow authentication now */
537 monitor_permit_authentications(1);
539 /* Allow service/style information on the auth context */
540 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
541 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
545 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
551 int mm_answer_auth2_read_banner(int socket, Buffer *m)
556 banner = auth2_read_banner();
557 buffer_put_cstring(m, banner != NULL ? banner : "");
558 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
567 mm_answer_authserv(int socket, Buffer *m)
569 monitor_permit_authentications(1);
571 authctxt->service = buffer_get_string(m, NULL);
572 authctxt->style = buffer_get_string(m, NULL);
573 debug3("%s: service=%s, style=%s",
574 __FUNCTION__, authctxt->service, authctxt->style);
576 if (strlen(authctxt->style) == 0) {
577 xfree(authctxt->style);
578 authctxt->style = NULL;
585 mm_answer_authpassword(int socket, Buffer *m)
587 static int call_count;
589 int authenticated, plen;
591 passwd = buffer_get_string(m, &plen);
592 /* Only authenticate if the context is valid */
593 authenticated = options.password_authentication &&
594 authctxt->valid && auth_password(authctxt, passwd);
595 memset(passwd, 0, strlen(passwd));
599 buffer_put_int(m, authenticated);
601 debug3("%s: sending result %d", __FUNCTION__, authenticated);
602 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
605 if (plen == 0 && call_count == 1)
606 auth_method = "none";
608 auth_method = "password";
610 /* Causes monitor loop to terminate if authenticated */
611 return (authenticated);
616 mm_answer_bsdauthquery(int socket, Buffer *m)
618 char *name, *infotxt;
624 res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
628 buffer_put_int(m, res);
630 buffer_put_cstring(m, prompts[0]);
632 debug3("%s: sending challenge res: %d", __FUNCTION__, res);
633 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
646 mm_answer_bsdauthrespond(int socket, Buffer *m)
651 if (authctxt->as == 0)
652 fatal("%s: no bsd auth session", __FUNCTION__);
654 response = buffer_get_string(m, NULL);
655 authok = options.challenge_response_authentication &&
656 auth_userresponse(authctxt->as, response, 0);
658 debug3("%s: <%s> = <%d>", __FUNCTION__, response, authok);
662 buffer_put_int(m, authok);
664 debug3("%s: sending authenticated: %d", __FUNCTION__, authok);
665 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
667 auth_method = "bsdauth";
669 return (authok != 0);
675 mm_answer_skeyquery(int socket, Buffer *m)
678 char challenge[1024];
681 res = skeychallenge(&skey, authctxt->user, challenge);
684 buffer_put_int(m, res);
686 buffer_put_cstring(m, challenge);
688 debug3("%s: sending challenge res: %d", __FUNCTION__, res);
689 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
695 mm_answer_skeyrespond(int socket, Buffer *m)
700 response = buffer_get_string(m, NULL);
702 authok = (options.challenge_response_authentication &&
704 skey_haskey(authctxt->pw->pw_name) == 0 &&
705 skey_passcheck(authctxt->pw->pw_name, response) != -1);
710 buffer_put_int(m, authok);
712 debug3("%s: sending authenticated: %d", __FUNCTION__, authok);
713 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
715 auth_method = "skey";
717 return (authok != 0);
723 mm_answer_pam_start(int socket, Buffer *m)
727 user = buffer_get_string(m, NULL);
738 mm_append_debug(Buffer *m)
740 if (auth_debug_init && buffer_len(&auth_debug)) {
741 debug3("%s: Appending debug messages for child", __FUNCTION__);
742 buffer_append(m, buffer_ptr(&auth_debug),
743 buffer_len(&auth_debug));
744 buffer_clear(&auth_debug);
749 mm_answer_keyallowed(int socket, Buffer *m)
752 u_char *cuser, *chost, *blob;
754 enum mm_keytype type = 0;
757 debug3("%s entering", __FUNCTION__);
759 type = buffer_get_int(m);
760 cuser = buffer_get_string(m, NULL);
761 chost = buffer_get_string(m, NULL);
762 blob = buffer_get_string(m, &bloblen);
764 key = key_from_blob(blob, bloblen);
766 if ((compat20 && type == MM_RSAHOSTKEY) ||
767 (!compat20 && type != MM_RSAHOSTKEY))
768 fatal("%s: key type and protocol mismatch", __FUNCTION__);
770 debug3("%s: key_from_blob: %p", __FUNCTION__, key);
772 if (key != NULL && authctxt->pw != NULL) {
775 allowed = options.pubkey_authentication &&
776 user_key_allowed(authctxt->pw, key);
779 allowed = options.hostbased_authentication &&
780 hostbased_key_allowed(authctxt->pw,
784 key->type = KEY_RSA1; /* XXX */
785 allowed = options.rhosts_rsa_authentication &&
786 auth_rhosts_rsa_key_allowed(authctxt->pw,
790 fatal("%s: unknown key type %d", __FUNCTION__, type);
796 /* clear temporarily storage (used by verify) */
797 monitor_reset_key_state();
800 /* Save temporarily for comparison in verify */
802 key_bloblen = bloblen;
804 hostbased_cuser = cuser;
805 hostbased_chost = chost;
808 debug3("%s: key %p is %s",
809 __FUNCTION__, key, allowed ? "allowed" : "disallowed");
812 buffer_put_int(m, allowed);
816 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
818 if (type == MM_RSAHOSTKEY)
819 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
825 monitor_valid_userblob(u_char *data, u_int datalen)
833 buffer_append(&b, data, datalen);
835 if (datafellows & SSH_OLD_SESSIONID) {
837 len = buffer_len(&b);
838 if ((session_id2 == NULL) ||
839 (len < session_id2_len) ||
840 (memcmp(p, session_id2, session_id2_len) != 0))
842 buffer_consume(&b, session_id2_len);
844 p = buffer_get_string(&b, &len);
845 if ((session_id2 == NULL) ||
846 (len != session_id2_len) ||
847 (memcmp(p, session_id2, session_id2_len) != 0))
851 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
853 p = buffer_get_string(&b, NULL);
854 if (strcmp(authctxt->user, p) != 0) {
855 log("wrong user name passed to monitor: expected %s != %.100s",
860 buffer_skip_string(&b);
861 if (datafellows & SSH_BUG_PKAUTH) {
862 if (!buffer_get_char(&b))
865 p = buffer_get_string(&b, NULL);
866 if (strcmp("publickey", p) != 0)
869 if (!buffer_get_char(&b))
871 buffer_skip_string(&b);
873 buffer_skip_string(&b);
874 if (buffer_len(&b) != 0)
881 monitor_valid_hostbasedblob(u_char *data, u_int datalen, u_char *cuser,
890 buffer_append(&b, data, datalen);
892 p = buffer_get_string(&b, &len);
893 if ((session_id2 == NULL) ||
894 (len != session_id2_len) ||
895 (memcmp(p, session_id2, session_id2_len) != 0))
899 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
901 p = buffer_get_string(&b, NULL);
902 if (strcmp(authctxt->user, p) != 0) {
903 log("wrong user name passed to monitor: expected %s != %.100s",
908 buffer_skip_string(&b); /* service */
909 p = buffer_get_string(&b, NULL);
910 if (strcmp(p, "hostbased") != 0)
913 buffer_skip_string(&b); /* pkalg */
914 buffer_skip_string(&b); /* pkblob */
916 /* verify client host, strip trailing dot if necessary */
917 p = buffer_get_string(&b, NULL);
918 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
920 if (strcmp(p, chost) != 0)
924 /* verify client user */
925 p = buffer_get_string(&b, NULL);
926 if (strcmp(p, cuser) != 0)
930 if (buffer_len(&b) != 0)
937 mm_answer_keyverify(int socket, Buffer *m)
940 u_char *signature, *data, *blob;
941 u_int signaturelen, datalen, bloblen;
945 blob = buffer_get_string(m, &bloblen);
946 signature = buffer_get_string(m, &signaturelen);
947 data = buffer_get_string(m, &datalen);
949 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
950 !monitor_allowed_key(blob, bloblen))
951 fatal("%s: bad key, not previously allowed", __FUNCTION__);
953 key = key_from_blob(blob, bloblen);
955 fatal("%s: bad public key blob", __FUNCTION__);
957 switch (key_blobtype) {
959 valid_data = monitor_valid_userblob(data, datalen);
962 valid_data = monitor_valid_hostbasedblob(data, datalen,
963 hostbased_cuser, hostbased_chost);
970 fatal("%s: bad signature data blob", __FUNCTION__);
972 verified = key_verify(key, signature, signaturelen, data, datalen);
973 debug3("%s: key %p signature %s",
974 __FUNCTION__, key, verified ? "verified" : "unverified");
981 monitor_reset_key_state();
984 buffer_put_int(m, verified);
985 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
987 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
993 mm_record_login(Session *s, struct passwd *pw)
996 struct sockaddr_storage from;
999 * Get IP address of client. If the connection is not a socket, let
1000 * the address be 0.0.0.0.
1002 memset(&from, 0, sizeof(from));
1003 if (packet_connection_is_on_socket()) {
1004 fromlen = sizeof(from);
1005 if (getpeername(packet_get_connection_in(),
1006 (struct sockaddr *) & from, &fromlen) < 0) {
1007 debug("getpeername: %.100s", strerror(errno));
1011 /* Record that there was a login on that tty from the remote host. */
1012 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1013 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
1014 (struct sockaddr *)&from);
1018 mm_session_close(Session *s)
1020 debug3("%s: session %d pid %d", __FUNCTION__, s->self, s->pid);
1021 if (s->ttyfd != -1) {
1022 debug3("%s: tty %s ptyfd %d", __FUNCTION__, s->tty, s->ptyfd);
1023 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1024 session_pty_cleanup2(s);
1030 mm_answer_pty(int socket, Buffer *m)
1032 extern struct monitor *pmonitor;
1036 debug3("%s entering", __FUNCTION__);
1042 s->authctxt = authctxt;
1043 s->pw = authctxt->pw;
1044 s->pid = pmonitor->m_pid;
1045 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1048 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1049 pty_setowner(authctxt->pw, s->tty);
1051 buffer_put_int(m, 1);
1052 buffer_put_cstring(m, s->tty);
1053 mm_request_send(socket, MONITOR_ANS_PTY, m);
1055 mm_send_fd(socket, s->ptyfd);
1056 mm_send_fd(socket, s->ttyfd);
1058 /* We need to trick ttyslot */
1059 if (dup2(s->ttyfd, 0) == -1)
1060 fatal("%s: dup2", __FUNCTION__);
1062 mm_record_login(s, authctxt->pw);
1064 /* Now we can close the file descriptor again */
1067 /* make sure nothing uses fd 0 */
1068 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1069 fatal("%s: open(/dev/null): %s", __FUNCTION__, strerror(errno));
1071 error("%s: fd0 %d != 0", __FUNCTION__, fd0);
1073 /* slave is not needed */
1075 s->ttyfd = s->ptyfd;
1076 /* no need to dup() because nobody closes ptyfd */
1077 s->ptymaster = s->ptyfd;
1079 debug3("%s: tty %s ptyfd %d", __FUNCTION__, s->tty, s->ttyfd);
1085 mm_session_close(s);
1086 buffer_put_int(m, 0);
1087 mm_request_send(socket, MONITOR_ANS_PTY, m);
1092 mm_answer_pty_cleanup(int socket, Buffer *m)
1097 debug3("%s entering", __FUNCTION__);
1099 tty = buffer_get_string(m, NULL);
1100 if ((s = session_by_tty(tty)) != NULL)
1101 mm_session_close(s);
1108 mm_answer_sesskey(int socket, Buffer *m)
1113 /* Turn off permissions */
1114 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1116 if ((p = BN_new()) == NULL)
1117 fatal("%s: BN_new", __FUNCTION__);
1119 buffer_get_bignum2(m, p);
1121 rsafail = ssh1_session_key(p);
1124 buffer_put_int(m, rsafail);
1125 buffer_put_bignum2(m, p);
1129 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1131 /* Turn on permissions for sessid passing */
1132 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1138 mm_answer_sessid(int socket, Buffer *m)
1142 debug3("%s entering", __FUNCTION__);
1144 if (buffer_len(m) != 16)
1145 fatal("%s: bad ssh1 session id", __FUNCTION__);
1146 for (i = 0; i < 16; i++)
1147 session_id[i] = buffer_get_char(m);
1149 /* Turn on permissions for getpwnam */
1150 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1156 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1160 u_char *blob = NULL;
1164 debug3("%s entering", __FUNCTION__);
1166 if (options.rsa_authentication && authctxt->valid) {
1167 if ((client_n = BN_new()) == NULL)
1168 fatal("%s: BN_new", __FUNCTION__);
1169 buffer_get_bignum2(m, client_n);
1170 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1171 BN_clear_free(client_n);
1174 buffer_put_int(m, allowed);
1176 /* clear temporarily storage (used by generate challenge) */
1177 monitor_reset_key_state();
1179 if (allowed && key != NULL) {
1180 key->type = KEY_RSA; /* cheat for key_to_blob */
1181 if (key_to_blob(key, &blob, &blen) == 0)
1182 fatal("%s: key_to_blob failed", __FUNCTION__);
1183 buffer_put_string(m, blob, blen);
1185 /* Save temporarily for comparison in verify */
1188 key_blobtype = MM_RSAUSERKEY;
1194 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1196 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1197 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1202 mm_answer_rsa_challenge(int socket, Buffer *m)
1208 debug3("%s entering", __FUNCTION__);
1210 if (!authctxt->valid)
1211 fatal("%s: authctxt not valid", __FUNCTION__);
1212 blob = buffer_get_string(m, &blen);
1213 if (!monitor_allowed_key(blob, blen))
1214 fatal("%s: bad key, not previously allowed", __FUNCTION__);
1215 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1216 fatal("%s: key type mismatch", __FUNCTION__);
1217 if ((key = key_from_blob(blob, blen)) == NULL)
1218 fatal("%s: received bad key", __FUNCTION__);
1221 BN_clear_free(ssh1_challenge);
1222 ssh1_challenge = auth_rsa_generate_challenge(key);
1225 buffer_put_bignum2(m, ssh1_challenge);
1227 debug3("%s sending reply", __FUNCTION__);
1228 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1230 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1235 mm_answer_rsa_response(int socket, Buffer *m)
1238 u_char *blob, *response;
1242 debug3("%s entering", __FUNCTION__);
1244 if (!authctxt->valid)
1245 fatal("%s: authctxt not valid", __FUNCTION__);
1246 if (ssh1_challenge == NULL)
1247 fatal("%s: no ssh1_challenge", __FUNCTION__);
1249 blob = buffer_get_string(m, &blen);
1250 if (!monitor_allowed_key(blob, blen))
1251 fatal("%s: bad key, not previously allowed", __FUNCTION__);
1252 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1253 fatal("%s: key type mismatch: %d", __FUNCTION__, key_blobtype);
1254 if ((key = key_from_blob(blob, blen)) == NULL)
1255 fatal("%s: received bad key", __FUNCTION__);
1256 response = buffer_get_string(m, &len);
1258 fatal("%s: received bad response to challenge", __FUNCTION__);
1259 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1264 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1267 BN_clear_free(ssh1_challenge);
1268 ssh1_challenge = NULL;
1269 monitor_reset_key_state();
1272 buffer_put_int(m, success);
1273 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1279 mm_answer_term(int socket, Buffer *req)
1281 extern struct monitor *pmonitor;
1284 debug3("%s: tearing down sessions", __FUNCTION__);
1286 /* The child is terminating */
1287 session_destroy_all(&mm_session_close);
1289 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1293 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1295 /* Terminate process */
1300 monitor_apply_keystate(struct monitor *pmonitor)
1303 set_newkeys(MODE_IN);
1304 set_newkeys(MODE_OUT);
1306 u_char key[SSH_SESSION_KEY_LENGTH];
1308 memset(key, 'a', sizeof(key));
1309 packet_set_protocol_flags(child_state.ssh1protoflags);
1310 packet_set_encryption_key(key, SSH_SESSION_KEY_LENGTH,
1311 child_state.ssh1cipher);
1314 packet_set_keycontext(MODE_OUT, child_state.keyout);
1315 xfree(child_state.keyout);
1316 packet_set_keycontext(MODE_IN, child_state.keyin);
1317 xfree(child_state.keyin);
1320 packet_set_iv(MODE_OUT, child_state.ivout);
1321 xfree(child_state.ivout);
1322 packet_set_iv(MODE_IN, child_state.ivin);
1323 xfree(child_state.ivin);
1326 memcpy(&incoming_stream, &child_state.incoming,
1327 sizeof(incoming_stream));
1328 memcpy(&outgoing_stream, &child_state.outgoing,
1329 sizeof(outgoing_stream));
1331 /* Update with new address */
1332 mm_init_compression(pmonitor->m_zlib);
1334 /* Network I/O buffers */
1335 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1336 buffer_clear(&input);
1337 buffer_append(&input, child_state.input, child_state.ilen);
1338 memset(child_state.input, 0, child_state.ilen);
1339 xfree(child_state.input);
1341 buffer_clear(&output);
1342 buffer_append(&output, child_state.output, child_state.olen);
1343 memset(child_state.output, 0, child_state.olen);
1344 xfree(child_state.output);
1348 mm_get_kex(Buffer *m)
1354 kex = xmalloc(sizeof(*kex));
1355 memset(kex, 0, sizeof(*kex));
1356 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1357 if ((session_id2 == NULL) ||
1358 (kex->session_id_len != session_id2_len) ||
1359 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1360 fatal("mm_get_get: internal error: bad session id");
1361 kex->we_need = buffer_get_int(m);
1363 kex->hostkey_type = buffer_get_int(m);
1364 kex->kex_type = buffer_get_int(m);
1365 blob = buffer_get_string(m, &bloblen);
1366 buffer_init(&kex->my);
1367 buffer_append(&kex->my, blob, bloblen);
1369 blob = buffer_get_string(m, &bloblen);
1370 buffer_init(&kex->peer);
1371 buffer_append(&kex->peer, blob, bloblen);
1374 kex->flags = buffer_get_int(m);
1375 kex->client_version_string = buffer_get_string(m, NULL);
1376 kex->server_version_string = buffer_get_string(m, NULL);
1377 kex->load_host_key=&get_hostkey_by_type;
1378 kex->host_key_index=&get_hostkey_index;
1383 /* This function requries careful sanity checking */
1386 mm_get_keystate(struct monitor *pmonitor)
1390 u_int bloblen, plen;
1392 debug3("%s: Waiting for new keys", __FUNCTION__);
1395 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1397 child_state.ssh1protoflags = buffer_get_int(&m);
1398 child_state.ssh1cipher = buffer_get_int(&m);
1399 child_state.ivout = buffer_get_string(&m,
1400 &child_state.ivoutlen);
1401 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1404 /* Get the Kex for rekeying */
1405 *pmonitor->m_pkex = mm_get_kex(&m);
1408 blob = buffer_get_string(&m, &bloblen);
1409 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1412 debug3("%s: Waiting for second key", __FUNCTION__);
1413 blob = buffer_get_string(&m, &bloblen);
1414 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1417 /* Now get sequence numbers for the packets */
1418 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1419 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1422 /* Get the key context */
1423 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1424 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1426 debug3("%s: Getting compression state", __FUNCTION__);
1427 /* Get compression state */
1428 p = buffer_get_string(&m, &plen);
1429 if (plen != sizeof(child_state.outgoing))
1430 fatal("%s: bad request size", __FUNCTION__);
1431 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1434 p = buffer_get_string(&m, &plen);
1435 if (plen != sizeof(child_state.incoming))
1436 fatal("%s: bad request size", __FUNCTION__);
1437 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1440 /* Network I/O buffers */
1441 debug3("%s: Getting Network I/O buffers", __FUNCTION__);
1442 child_state.input = buffer_get_string(&m, &child_state.ilen);
1443 child_state.output = buffer_get_string(&m, &child_state.olen);
1449 /* Allocation functions for zlib */
1451 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1455 address = mm_malloc(mm, size * ncount);
1461 mm_zfree(struct mm_master *mm, void *address)
1463 mm_free(mm, address);
1467 mm_init_compression(struct mm_master *mm)
1469 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1470 outgoing_stream.zfree = (free_func)mm_zfree;
1471 outgoing_stream.opaque = mm;
1473 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1474 incoming_stream.zfree = (free_func)mm_zfree;
1475 incoming_stream.opaque = mm;
1480 #define FD_CLOSEONEXEC(x) do { \
1481 if (fcntl(x, F_SETFD, 1) == -1) \
1482 fatal("fcntl(%d, F_SETFD)", x); \
1486 monitor_socketpair(int *pair)
1488 #ifdef HAVE_SOCKETPAIR
1489 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1490 fatal("%s: socketpair", __FUNCTION__);
1492 fatal("%s: UsePrivilegeSeparation=yes not supported",
1495 FD_CLOSEONEXEC(pair[0]);
1496 FD_CLOSEONEXEC(pair[1]);
1499 #define MM_MEMSIZE 65536
1504 struct monitor *mon;
1507 mon = xmalloc(sizeof(*mon));
1509 monitor_socketpair(pair);
1511 mon->m_recvfd = pair[0];
1512 mon->m_sendfd = pair[1];
1514 /* Used to share zlib space across processes */
1515 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1516 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1518 /* Compression needs to share state across borders */
1519 mm_init_compression(mon->m_zlib);
1525 monitor_reinit(struct monitor *mon)
1529 monitor_socketpair(pair);
1531 mon->m_recvfd = pair[0];
1532 mon->m_sendfd = pair[1];