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.28 2002/09/24 08:46:04 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 */
96 /* Functions on the montior that answer unprivileged requests */
98 int mm_answer_moduli(int, Buffer *);
99 int mm_answer_sign(int, Buffer *);
100 int mm_answer_pwnamallow(int, Buffer *);
101 int mm_answer_auth2_read_banner(int, Buffer *);
102 int mm_answer_authserv(int, Buffer *);
103 int mm_answer_authpassword(int, Buffer *);
104 int mm_answer_bsdauthquery(int, Buffer *);
105 int mm_answer_bsdauthrespond(int, Buffer *);
106 int mm_answer_skeyquery(int, Buffer *);
107 int mm_answer_skeyrespond(int, Buffer *);
108 int mm_answer_keyallowed(int, Buffer *);
109 int mm_answer_keyverify(int, Buffer *);
110 int mm_answer_pty(int, Buffer *);
111 int mm_answer_pty_cleanup(int, Buffer *);
112 int mm_answer_term(int, Buffer *);
113 int mm_answer_rsa_keyallowed(int, Buffer *);
114 int mm_answer_rsa_challenge(int, Buffer *);
115 int mm_answer_rsa_response(int, Buffer *);
116 int mm_answer_sesskey(int, Buffer *);
117 int mm_answer_sessid(int, Buffer *);
120 int mm_answer_pam_start(int, Buffer *);
124 int mm_answer_krb5(int, Buffer *);
127 static Authctxt *authctxt;
128 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
130 /* local state for key verify */
131 static u_char *key_blob = NULL;
132 static u_int key_bloblen = 0;
133 static int key_blobtype = MM_NOKEY;
134 static char *hostbased_cuser = NULL;
135 static char *hostbased_chost = NULL;
136 static char *auth_method = "unknown";
137 static int session_id2_len = 0;
138 static u_char *session_id2 = NULL;
141 enum monitor_reqtype type;
143 int (*f)(int, Buffer *);
146 #define MON_ISAUTH 0x0004 /* Required for Authentication */
147 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
148 #define MON_ONCE 0x0010 /* Disable after calling */
150 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
152 #define MON_PERMIT 0x1000 /* Request is permitted */
154 struct mon_table mon_dispatch_proto20[] = {
155 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
156 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
157 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
158 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
159 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
160 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
162 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
165 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
166 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
169 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
170 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
172 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
173 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
177 struct mon_table mon_dispatch_postauth20[] = {
178 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
179 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
180 {MONITOR_REQ_PTY, 0, mm_answer_pty},
181 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
182 {MONITOR_REQ_TERM, 0, mm_answer_term},
186 struct mon_table mon_dispatch_proto15[] = {
187 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
188 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
189 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
190 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
191 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
192 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
193 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
194 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
196 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
197 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
200 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
201 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
204 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
207 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
212 struct mon_table mon_dispatch_postauth15[] = {
213 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
214 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
215 {MONITOR_REQ_TERM, 0, mm_answer_term},
219 struct mon_table *mon_dispatch;
221 /* Specifies if a certain message is allowed at the moment */
224 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
226 while (ent->f != NULL) {
227 if (ent->type == type) {
228 ent->flags &= ~MON_PERMIT;
229 ent->flags |= permit ? MON_PERMIT : 0;
237 monitor_permit_authentications(int permit)
239 struct mon_table *ent = mon_dispatch;
241 while (ent->f != NULL) {
242 if (ent->flags & MON_AUTH) {
243 ent->flags &= ~MON_PERMIT;
244 ent->flags |= permit ? MON_PERMIT : 0;
251 monitor_child_preauth(struct monitor *pmonitor)
253 struct mon_table *ent;
254 int authenticated = 0;
256 debug3("preauth child monitor started");
259 mon_dispatch = mon_dispatch_proto20;
261 /* Permit requests for moduli and signatures */
262 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
263 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
265 mon_dispatch = mon_dispatch_proto15;
267 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
270 authctxt = authctxt_new();
272 /* The first few requests do not require asynchronous access */
273 while (!authenticated) {
274 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
276 if (!(ent->flags & MON_AUTHDECIDE))
277 fatal("%s: unexpected authentication from %d",
278 __func__, ent->type);
279 if (authctxt->pw->pw_uid == 0 &&
280 !auth_root_allowed(auth_method))
283 if (!do_pam_account(authctxt->pw->pw_name, NULL))
288 if (ent->flags & MON_AUTHDECIDE) {
289 auth_log(authctxt, authenticated, auth_method,
290 compat20 ? " ssh2" : "");
292 authctxt->failures++;
296 if (!authctxt->valid)
297 fatal("%s: authenticated invalid user", __func__);
299 debug("%s: %s has been authenticated by privileged process",
300 __func__, authctxt->user);
302 mm_get_keystate(pmonitor);
308 monitor_child_postauth(struct monitor *pmonitor)
311 mon_dispatch = mon_dispatch_postauth20;
313 /* Permit requests for moduli and signatures */
314 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
315 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
316 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
319 mon_dispatch = mon_dispatch_postauth15;
320 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
323 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
324 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
328 monitor_read(pmonitor, mon_dispatch, NULL);
332 monitor_sync(struct monitor *pmonitor)
334 if (options.compression) {
335 /* The member allocation is not visible, so sync it */
336 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
341 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
342 struct mon_table **pent)
350 mm_request_receive(pmonitor->m_sendfd, &m);
351 type = buffer_get_char(&m);
353 debug3("%s: checking request %d", __func__, type);
355 while (ent->f != NULL) {
356 if (ent->type == type)
361 if (ent->f != NULL) {
362 if (!(ent->flags & MON_PERMIT))
363 fatal("%s: unpermitted request %d", __func__,
365 ret = (*ent->f)(pmonitor->m_sendfd, &m);
368 /* The child may use this request only once, disable it */
369 if (ent->flags & MON_ONCE) {
370 debug2("%s: %d used once, disabling now", __func__,
372 ent->flags &= ~MON_PERMIT;
381 fatal("%s: unsupported request: %d", __func__, type);
387 /* allowed key state */
389 monitor_allowed_key(u_char *blob, u_int bloblen)
391 /* make sure key is allowed */
392 if (key_blob == NULL || key_bloblen != bloblen ||
393 memcmp(key_blob, blob, key_bloblen))
399 monitor_reset_key_state(void)
402 if (key_blob != NULL)
404 if (hostbased_cuser != NULL)
405 xfree(hostbased_cuser);
406 if (hostbased_chost != NULL)
407 xfree(hostbased_chost);
410 key_blobtype = MM_NOKEY;
411 hostbased_cuser = NULL;
412 hostbased_chost = NULL;
416 mm_answer_moduli(int socket, Buffer *m)
421 min = buffer_get_int(m);
422 want = buffer_get_int(m);
423 max = buffer_get_int(m);
425 debug3("%s: got parameters: %d %d %d",
426 __func__, min, want, max);
427 /* We need to check here, too, in case the child got corrupted */
428 if (max < min || want < min || max < want)
429 fatal("%s: bad parameters: %d %d %d",
430 __func__, min, want, max);
434 dh = choose_dh(min, want, max);
436 buffer_put_char(m, 0);
439 /* Send first bignum */
440 buffer_put_char(m, 1);
441 buffer_put_bignum2(m, dh->p);
442 buffer_put_bignum2(m, dh->g);
446 mm_request_send(socket, MONITOR_ANS_MODULI, m);
451 mm_answer_sign(int socket, Buffer *m)
456 u_int siglen, datlen;
459 debug3("%s", __func__);
461 keyid = buffer_get_int(m);
462 p = buffer_get_string(m, &datlen);
465 fatal("%s: data length incorrect: %u", __func__, datlen);
467 /* save session id, it will be passed on the first call */
468 if (session_id2_len == 0) {
469 session_id2_len = datlen;
470 session_id2 = xmalloc(session_id2_len);
471 memcpy(session_id2, p, session_id2_len);
474 if ((key = get_hostkey_by_index(keyid)) == NULL)
475 fatal("%s: no hostkey from index %d", __func__, keyid);
476 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
477 fatal("%s: key_sign failed", __func__);
479 debug3("%s: signature %p(%u)", __func__, signature, siglen);
482 buffer_put_string(m, signature, siglen);
487 mm_request_send(socket, MONITOR_ANS_SIGN, m);
489 /* Turn on permissions for getpwnam */
490 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
495 /* Retrieves the password entry and also checks if the user is permitted */
498 mm_answer_pwnamallow(int socket, Buffer *m)
501 struct passwd *pwent;
504 debug3("%s", __func__);
506 if (authctxt->attempt++ != 0)
507 fatal("%s: multiple attempts for getpwnam", __func__);
509 login = buffer_get_string(m, NULL);
511 pwent = getpwnamallow(login);
513 authctxt->user = xstrdup(login);
514 setproctitle("%s [priv]", pwent ? login : "unknown");
520 buffer_put_char(m, 0);
525 authctxt->pw = pwent;
528 buffer_put_char(m, 1);
529 buffer_put_string(m, pwent, sizeof(struct passwd));
530 buffer_put_cstring(m, pwent->pw_name);
531 buffer_put_cstring(m, "*");
532 buffer_put_cstring(m, pwent->pw_gecos);
533 #ifdef HAVE_PW_CLASS_IN_PASSWD
534 buffer_put_cstring(m, pwent->pw_class);
536 buffer_put_cstring(m, pwent->pw_dir);
537 buffer_put_cstring(m, pwent->pw_shell);
540 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
541 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
543 /* For SSHv1 allow authentication now */
545 monitor_permit_authentications(1);
547 /* Allow service/style information on the auth context */
548 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
549 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
553 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
559 int mm_answer_auth2_read_banner(int socket, Buffer *m)
564 banner = auth2_read_banner();
565 buffer_put_cstring(m, banner != NULL ? banner : "");
566 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
575 mm_answer_authserv(int socket, Buffer *m)
577 monitor_permit_authentications(1);
579 authctxt->service = buffer_get_string(m, NULL);
580 authctxt->style = buffer_get_string(m, NULL);
581 debug3("%s: service=%s, style=%s",
582 __func__, authctxt->service, authctxt->style);
584 if (strlen(authctxt->style) == 0) {
585 xfree(authctxt->style);
586 authctxt->style = NULL;
593 mm_answer_authpassword(int socket, Buffer *m)
595 static int call_count;
600 passwd = buffer_get_string(m, &plen);
601 /* Only authenticate if the context is valid */
602 authenticated = options.password_authentication &&
603 authctxt->valid && auth_password(authctxt, passwd);
604 memset(passwd, 0, strlen(passwd));
608 buffer_put_int(m, authenticated);
610 debug3("%s: sending result %d", __func__, authenticated);
611 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
614 if (plen == 0 && call_count == 1)
615 auth_method = "none";
617 auth_method = "password";
619 /* Causes monitor loop to terminate if authenticated */
620 return (authenticated);
625 mm_answer_bsdauthquery(int socket, Buffer *m)
627 char *name, *infotxt;
633 res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
637 buffer_put_int(m, res);
639 buffer_put_cstring(m, prompts[0]);
641 debug3("%s: sending challenge res: %d", __func__, res);
642 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
655 mm_answer_bsdauthrespond(int socket, Buffer *m)
660 if (authctxt->as == 0)
661 fatal("%s: no bsd auth session", __func__);
663 response = buffer_get_string(m, NULL);
664 authok = options.challenge_response_authentication &&
665 auth_userresponse(authctxt->as, response, 0);
667 debug3("%s: <%s> = <%d>", __func__, response, authok);
671 buffer_put_int(m, authok);
673 debug3("%s: sending authenticated: %d", __func__, authok);
674 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
676 auth_method = "bsdauth";
678 return (authok != 0);
684 mm_answer_skeyquery(int socket, Buffer *m)
687 char challenge[1024];
690 res = skeychallenge(&skey, authctxt->user, challenge);
693 buffer_put_int(m, res);
695 buffer_put_cstring(m, challenge);
697 debug3("%s: sending challenge res: %d", __func__, res);
698 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
704 mm_answer_skeyrespond(int socket, Buffer *m)
709 response = buffer_get_string(m, NULL);
711 authok = (options.challenge_response_authentication &&
713 skey_haskey(authctxt->pw->pw_name) == 0 &&
714 skey_passcheck(authctxt->pw->pw_name, response) != -1);
719 buffer_put_int(m, authok);
721 debug3("%s: sending authenticated: %d", __func__, authok);
722 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
724 auth_method = "skey";
726 return (authok != 0);
732 mm_answer_pam_start(int socket, Buffer *m)
736 user = buffer_get_string(m, NULL);
747 mm_append_debug(Buffer *m)
749 if (auth_debug_init && buffer_len(&auth_debug)) {
750 debug3("%s: Appending debug messages for child", __func__);
751 buffer_append(m, buffer_ptr(&auth_debug),
752 buffer_len(&auth_debug));
753 buffer_clear(&auth_debug);
758 mm_answer_keyallowed(int socket, Buffer *m)
764 enum mm_keytype type = 0;
767 debug3("%s entering", __func__);
769 type = buffer_get_int(m);
770 cuser = buffer_get_string(m, NULL);
771 chost = buffer_get_string(m, NULL);
772 blob = buffer_get_string(m, &bloblen);
774 key = key_from_blob(blob, bloblen);
776 if ((compat20 && type == MM_RSAHOSTKEY) ||
777 (!compat20 && type != MM_RSAHOSTKEY))
778 fatal("%s: key type and protocol mismatch", __func__);
780 debug3("%s: key_from_blob: %p", __func__, key);
782 if (key != NULL && authctxt->pw != NULL) {
785 allowed = options.pubkey_authentication &&
786 user_key_allowed(authctxt->pw, key);
789 allowed = options.hostbased_authentication &&
790 hostbased_key_allowed(authctxt->pw,
794 key->type = KEY_RSA1; /* XXX */
795 allowed = options.rhosts_rsa_authentication &&
796 auth_rhosts_rsa_key_allowed(authctxt->pw,
800 fatal("%s: unknown key type %d", __func__, type);
806 /* clear temporarily storage (used by verify) */
807 monitor_reset_key_state();
810 /* Save temporarily for comparison in verify */
812 key_bloblen = bloblen;
814 hostbased_cuser = cuser;
815 hostbased_chost = chost;
818 debug3("%s: key %p is %s",
819 __func__, key, allowed ? "allowed" : "disallowed");
822 buffer_put_int(m, allowed);
826 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
828 if (type == MM_RSAHOSTKEY)
829 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
835 monitor_valid_userblob(u_char *data, u_int datalen)
843 buffer_append(&b, data, datalen);
845 if (datafellows & SSH_OLD_SESSIONID) {
847 len = buffer_len(&b);
848 if ((session_id2 == NULL) ||
849 (len < session_id2_len) ||
850 (memcmp(p, session_id2, session_id2_len) != 0))
852 buffer_consume(&b, session_id2_len);
854 p = buffer_get_string(&b, &len);
855 if ((session_id2 == NULL) ||
856 (len != session_id2_len) ||
857 (memcmp(p, session_id2, session_id2_len) != 0))
861 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
863 p = buffer_get_string(&b, NULL);
864 if (strcmp(authctxt->user, p) != 0) {
865 log("wrong user name passed to monitor: expected %s != %.100s",
870 buffer_skip_string(&b);
871 if (datafellows & SSH_BUG_PKAUTH) {
872 if (!buffer_get_char(&b))
875 p = buffer_get_string(&b, NULL);
876 if (strcmp("publickey", p) != 0)
879 if (!buffer_get_char(&b))
881 buffer_skip_string(&b);
883 buffer_skip_string(&b);
884 if (buffer_len(&b) != 0)
891 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
900 buffer_append(&b, data, datalen);
902 p = buffer_get_string(&b, &len);
903 if ((session_id2 == NULL) ||
904 (len != session_id2_len) ||
905 (memcmp(p, session_id2, session_id2_len) != 0))
909 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
911 p = buffer_get_string(&b, NULL);
912 if (strcmp(authctxt->user, p) != 0) {
913 log("wrong user name passed to monitor: expected %s != %.100s",
918 buffer_skip_string(&b); /* service */
919 p = buffer_get_string(&b, NULL);
920 if (strcmp(p, "hostbased") != 0)
923 buffer_skip_string(&b); /* pkalg */
924 buffer_skip_string(&b); /* pkblob */
926 /* verify client host, strip trailing dot if necessary */
927 p = buffer_get_string(&b, NULL);
928 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
930 if (strcmp(p, chost) != 0)
934 /* verify client user */
935 p = buffer_get_string(&b, NULL);
936 if (strcmp(p, cuser) != 0)
940 if (buffer_len(&b) != 0)
947 mm_answer_keyverify(int socket, Buffer *m)
950 u_char *signature, *data, *blob;
951 u_int signaturelen, datalen, bloblen;
955 blob = buffer_get_string(m, &bloblen);
956 signature = buffer_get_string(m, &signaturelen);
957 data = buffer_get_string(m, &datalen);
959 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
960 !monitor_allowed_key(blob, bloblen))
961 fatal("%s: bad key, not previously allowed", __func__);
963 key = key_from_blob(blob, bloblen);
965 fatal("%s: bad public key blob", __func__);
967 switch (key_blobtype) {
969 valid_data = monitor_valid_userblob(data, datalen);
972 valid_data = monitor_valid_hostbasedblob(data, datalen,
973 hostbased_cuser, hostbased_chost);
980 fatal("%s: bad signature data blob", __func__);
982 verified = key_verify(key, signature, signaturelen, data, datalen);
983 debug3("%s: key %p signature %s",
984 __func__, key, verified ? "verified" : "unverified");
991 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
993 monitor_reset_key_state();
996 buffer_put_int(m, verified);
997 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1003 mm_record_login(Session *s, struct passwd *pw)
1006 struct sockaddr_storage from;
1009 * Get IP address of client. If the connection is not a socket, let
1010 * the address be 0.0.0.0.
1012 memset(&from, 0, sizeof(from));
1013 fromlen = sizeof(from);
1014 if (packet_connection_is_on_socket()) {
1015 if (getpeername(packet_get_connection_in(),
1016 (struct sockaddr *) & from, &fromlen) < 0) {
1017 debug("getpeername: %.100s", strerror(errno));
1021 /* Record that there was a login on that tty from the remote host. */
1022 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1023 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
1024 (struct sockaddr *)&from, fromlen);
1028 mm_session_close(Session *s)
1030 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
1031 if (s->ttyfd != -1) {
1032 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1033 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1034 session_pty_cleanup2(s);
1040 mm_answer_pty(int socket, Buffer *m)
1042 extern struct monitor *pmonitor;
1046 debug3("%s entering", __func__);
1052 s->authctxt = authctxt;
1053 s->pw = authctxt->pw;
1054 s->pid = pmonitor->m_pid;
1055 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1058 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1059 pty_setowner(authctxt->pw, s->tty);
1061 buffer_put_int(m, 1);
1062 buffer_put_cstring(m, s->tty);
1063 mm_request_send(socket, MONITOR_ANS_PTY, m);
1065 mm_send_fd(socket, s->ptyfd);
1066 mm_send_fd(socket, s->ttyfd);
1068 /* We need to trick ttyslot */
1069 if (dup2(s->ttyfd, 0) == -1)
1070 fatal("%s: dup2", __func__);
1072 mm_record_login(s, authctxt->pw);
1074 /* Now we can close the file descriptor again */
1077 /* make sure nothing uses fd 0 */
1078 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1079 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1081 error("%s: fd0 %d != 0", __func__, fd0);
1083 /* slave is not needed */
1085 s->ttyfd = s->ptyfd;
1086 /* no need to dup() because nobody closes ptyfd */
1087 s->ptymaster = s->ptyfd;
1089 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1095 mm_session_close(s);
1096 buffer_put_int(m, 0);
1097 mm_request_send(socket, MONITOR_ANS_PTY, m);
1102 mm_answer_pty_cleanup(int socket, Buffer *m)
1107 debug3("%s entering", __func__);
1109 tty = buffer_get_string(m, NULL);
1110 if ((s = session_by_tty(tty)) != NULL)
1111 mm_session_close(s);
1118 mm_answer_sesskey(int socket, Buffer *m)
1123 /* Turn off permissions */
1124 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1126 if ((p = BN_new()) == NULL)
1127 fatal("%s: BN_new", __func__);
1129 buffer_get_bignum2(m, p);
1131 rsafail = ssh1_session_key(p);
1134 buffer_put_int(m, rsafail);
1135 buffer_put_bignum2(m, p);
1139 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1141 /* Turn on permissions for sessid passing */
1142 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1148 mm_answer_sessid(int socket, Buffer *m)
1152 debug3("%s entering", __func__);
1154 if (buffer_len(m) != 16)
1155 fatal("%s: bad ssh1 session id", __func__);
1156 for (i = 0; i < 16; i++)
1157 session_id[i] = buffer_get_char(m);
1159 /* Turn on permissions for getpwnam */
1160 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1166 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1170 u_char *blob = NULL;
1174 debug3("%s entering", __func__);
1176 if (options.rsa_authentication && authctxt->valid) {
1177 if ((client_n = BN_new()) == NULL)
1178 fatal("%s: BN_new", __func__);
1179 buffer_get_bignum2(m, client_n);
1180 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1181 BN_clear_free(client_n);
1184 buffer_put_int(m, allowed);
1186 /* clear temporarily storage (used by generate challenge) */
1187 monitor_reset_key_state();
1189 if (allowed && key != NULL) {
1190 key->type = KEY_RSA; /* cheat for key_to_blob */
1191 if (key_to_blob(key, &blob, &blen) == 0)
1192 fatal("%s: key_to_blob failed", __func__);
1193 buffer_put_string(m, blob, blen);
1195 /* Save temporarily for comparison in verify */
1198 key_blobtype = MM_RSAUSERKEY;
1204 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1206 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1207 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1212 mm_answer_rsa_challenge(int socket, Buffer *m)
1218 debug3("%s entering", __func__);
1220 if (!authctxt->valid)
1221 fatal("%s: authctxt not valid", __func__);
1222 blob = buffer_get_string(m, &blen);
1223 if (!monitor_allowed_key(blob, blen))
1224 fatal("%s: bad key, not previously allowed", __func__);
1225 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1226 fatal("%s: key type mismatch", __func__);
1227 if ((key = key_from_blob(blob, blen)) == NULL)
1228 fatal("%s: received bad key", __func__);
1231 BN_clear_free(ssh1_challenge);
1232 ssh1_challenge = auth_rsa_generate_challenge(key);
1235 buffer_put_bignum2(m, ssh1_challenge);
1237 debug3("%s sending reply", __func__);
1238 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1240 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1245 mm_answer_rsa_response(int socket, Buffer *m)
1248 u_char *blob, *response;
1252 debug3("%s entering", __func__);
1254 if (!authctxt->valid)
1255 fatal("%s: authctxt not valid", __func__);
1256 if (ssh1_challenge == NULL)
1257 fatal("%s: no ssh1_challenge", __func__);
1259 blob = buffer_get_string(m, &blen);
1260 if (!monitor_allowed_key(blob, blen))
1261 fatal("%s: bad key, not previously allowed", __func__);
1262 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1263 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1264 if ((key = key_from_blob(blob, blen)) == NULL)
1265 fatal("%s: received bad key", __func__);
1266 response = buffer_get_string(m, &len);
1268 fatal("%s: received bad response to challenge", __func__);
1269 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1274 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1277 BN_clear_free(ssh1_challenge);
1278 ssh1_challenge = NULL;
1279 monitor_reset_key_state();
1282 buffer_put_int(m, success);
1283 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1291 mm_answer_krb5(int socket, Buffer *m)
1293 krb5_data tkt, reply;
1298 /* use temporary var to avoid size issues on 64bit arch */
1299 tkt.data = buffer_get_string(m, &len);
1302 success = options.kerberos_authentication &&
1304 auth_krb5(authctxt, &tkt, &client_user, &reply);
1310 buffer_put_int(m, success);
1313 buffer_put_cstring(m, client_user);
1314 buffer_put_string(m, reply.data, reply.length);
1320 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1327 mm_answer_term(int socket, Buffer *req)
1329 extern struct monitor *pmonitor;
1332 debug3("%s: tearing down sessions", __func__);
1334 /* The child is terminating */
1335 session_destroy_all(&mm_session_close);
1337 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1341 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1343 /* Terminate process */
1348 monitor_apply_keystate(struct monitor *pmonitor)
1351 set_newkeys(MODE_IN);
1352 set_newkeys(MODE_OUT);
1354 packet_set_protocol_flags(child_state.ssh1protoflags);
1355 packet_set_encryption_key(child_state.ssh1key,
1356 child_state.ssh1keylen, child_state.ssh1cipher);
1357 xfree(child_state.ssh1key);
1360 /* for rc4 and other stateful ciphers */
1361 packet_set_keycontext(MODE_OUT, child_state.keyout);
1362 xfree(child_state.keyout);
1363 packet_set_keycontext(MODE_IN, child_state.keyin);
1364 xfree(child_state.keyin);
1367 packet_set_iv(MODE_OUT, child_state.ivout);
1368 xfree(child_state.ivout);
1369 packet_set_iv(MODE_IN, child_state.ivin);
1370 xfree(child_state.ivin);
1373 memcpy(&incoming_stream, &child_state.incoming,
1374 sizeof(incoming_stream));
1375 memcpy(&outgoing_stream, &child_state.outgoing,
1376 sizeof(outgoing_stream));
1378 /* Update with new address */
1379 if (options.compression)
1380 mm_init_compression(pmonitor->m_zlib);
1382 /* Network I/O buffers */
1383 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1384 buffer_clear(&input);
1385 buffer_append(&input, child_state.input, child_state.ilen);
1386 memset(child_state.input, 0, child_state.ilen);
1387 xfree(child_state.input);
1389 buffer_clear(&output);
1390 buffer_append(&output, child_state.output, child_state.olen);
1391 memset(child_state.output, 0, child_state.olen);
1392 xfree(child_state.output);
1396 mm_get_kex(Buffer *m)
1402 kex = xmalloc(sizeof(*kex));
1403 memset(kex, 0, sizeof(*kex));
1404 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1405 if ((session_id2 == NULL) ||
1406 (kex->session_id_len != session_id2_len) ||
1407 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1408 fatal("mm_get_get: internal error: bad session id");
1409 kex->we_need = buffer_get_int(m);
1411 kex->hostkey_type = buffer_get_int(m);
1412 kex->kex_type = buffer_get_int(m);
1413 blob = buffer_get_string(m, &bloblen);
1414 buffer_init(&kex->my);
1415 buffer_append(&kex->my, blob, bloblen);
1417 blob = buffer_get_string(m, &bloblen);
1418 buffer_init(&kex->peer);
1419 buffer_append(&kex->peer, blob, bloblen);
1422 kex->flags = buffer_get_int(m);
1423 kex->client_version_string = buffer_get_string(m, NULL);
1424 kex->server_version_string = buffer_get_string(m, NULL);
1425 kex->load_host_key=&get_hostkey_by_type;
1426 kex->host_key_index=&get_hostkey_index;
1431 /* This function requries careful sanity checking */
1434 mm_get_keystate(struct monitor *pmonitor)
1438 u_int bloblen, plen;
1440 debug3("%s: Waiting for new keys", __func__);
1443 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1445 child_state.ssh1protoflags = buffer_get_int(&m);
1446 child_state.ssh1cipher = buffer_get_int(&m);
1447 child_state.ssh1key = buffer_get_string(&m,
1448 &child_state.ssh1keylen);
1449 child_state.ivout = buffer_get_string(&m,
1450 &child_state.ivoutlen);
1451 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1454 /* Get the Kex for rekeying */
1455 *pmonitor->m_pkex = mm_get_kex(&m);
1458 blob = buffer_get_string(&m, &bloblen);
1459 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1462 debug3("%s: Waiting for second key", __func__);
1463 blob = buffer_get_string(&m, &bloblen);
1464 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1467 /* Now get sequence numbers for the packets */
1468 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1469 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1472 /* Get the key context */
1473 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1474 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1476 debug3("%s: Getting compression state", __func__);
1477 /* Get compression state */
1478 p = buffer_get_string(&m, &plen);
1479 if (plen != sizeof(child_state.outgoing))
1480 fatal("%s: bad request size", __func__);
1481 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1484 p = buffer_get_string(&m, &plen);
1485 if (plen != sizeof(child_state.incoming))
1486 fatal("%s: bad request size", __func__);
1487 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1490 /* Network I/O buffers */
1491 debug3("%s: Getting Network I/O buffers", __func__);
1492 child_state.input = buffer_get_string(&m, &child_state.ilen);
1493 child_state.output = buffer_get_string(&m, &child_state.olen);
1499 /* Allocation functions for zlib */
1501 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1503 size_t len = size * ncount;
1506 if (len == 0 || ncount > SIZE_T_MAX / size)
1507 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1509 address = mm_malloc(mm, len);
1515 mm_zfree(struct mm_master *mm, void *address)
1517 mm_free(mm, address);
1521 mm_init_compression(struct mm_master *mm)
1523 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1524 outgoing_stream.zfree = (free_func)mm_zfree;
1525 outgoing_stream.opaque = mm;
1527 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1528 incoming_stream.zfree = (free_func)mm_zfree;
1529 incoming_stream.opaque = mm;
1534 #define FD_CLOSEONEXEC(x) do { \
1535 if (fcntl(x, F_SETFD, 1) == -1) \
1536 fatal("fcntl(%d, F_SETFD)", x); \
1540 monitor_socketpair(int *pair)
1542 #ifdef HAVE_SOCKETPAIR
1543 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1544 fatal("%s: socketpair", __func__);
1546 fatal("%s: UsePrivilegeSeparation=yes not supported",
1549 FD_CLOSEONEXEC(pair[0]);
1550 FD_CLOSEONEXEC(pair[1]);
1553 #define MM_MEMSIZE 65536
1558 struct monitor *mon;
1561 mon = xmalloc(sizeof(*mon));
1563 monitor_socketpair(pair);
1565 mon->m_recvfd = pair[0];
1566 mon->m_sendfd = pair[1];
1568 /* Used to share zlib space across processes */
1569 if (options.compression) {
1570 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1571 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1573 /* Compression needs to share state across borders */
1574 mm_init_compression(mon->m_zlib);
1581 monitor_reinit(struct monitor *mon)
1585 monitor_socketpair(pair);
1587 mon->m_recvfd = pair[0];
1588 mon->m_sendfd = pair[1];