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.18 2002/06/26 13:20:57 deraadt 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"
64 static Gssctxt *gsscontext = NULL;
68 extern ServerOptions options;
69 extern u_int utmp_len;
70 extern Newkeys *current_keys[];
71 extern z_stream incoming_stream;
72 extern z_stream outgoing_stream;
73 extern u_char session_id[];
74 extern Buffer input, output;
75 extern Buffer auth_debug;
76 extern int auth_debug_init;
78 /* State exported from the child */
101 /* Functions on the montior that answer unprivileged requests */
103 int mm_answer_moduli(int, Buffer *);
104 int mm_answer_sign(int, Buffer *);
105 int mm_answer_pwnamallow(int, Buffer *);
106 int mm_answer_auth2_read_banner(int, Buffer *);
107 int mm_answer_authserv(int, Buffer *);
108 int mm_answer_authpassword(int, Buffer *);
109 int mm_answer_bsdauthquery(int, Buffer *);
110 int mm_answer_bsdauthrespond(int, Buffer *);
111 int mm_answer_skeyquery(int, Buffer *);
112 int mm_answer_skeyrespond(int, Buffer *);
113 int mm_answer_keyallowed(int, Buffer *);
114 int mm_answer_keyverify(int, Buffer *);
115 int mm_answer_pty(int, Buffer *);
116 int mm_answer_pty_cleanup(int, Buffer *);
117 int mm_answer_term(int, Buffer *);
118 int mm_answer_rsa_keyallowed(int, Buffer *);
119 int mm_answer_rsa_challenge(int, Buffer *);
120 int mm_answer_rsa_response(int, Buffer *);
121 int mm_answer_sesskey(int, Buffer *);
122 int mm_answer_sessid(int, Buffer *);
125 int mm_answer_pam_start(int, Buffer *);
129 int mm_answer_gss_setup_ctx(int, Buffer *);
130 int mm_answer_gss_accept_ctx(int, Buffer *);
131 int mm_answer_gss_userok(int, Buffer *);
132 int mm_answer_gss_localname(int, Buffer *);
133 int mm_answer_gss_sign(int, Buffer *);
137 int mm_answer_gsi_gridmap(int, Buffer *);
140 static Authctxt *authctxt;
141 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
143 /* local state for key verify */
144 static u_char *key_blob = NULL;
145 static u_int key_bloblen = 0;
146 static int key_blobtype = MM_NOKEY;
147 static u_char *hostbased_cuser = NULL;
148 static u_char *hostbased_chost = NULL;
149 static char *auth_method = "unknown";
150 static int session_id2_len = 0;
151 static u_char *session_id2 = NULL;
154 enum monitor_reqtype type;
156 int (*f)(int, Buffer *);
159 #define MON_ISAUTH 0x0004 /* Required for Authentication */
160 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
161 #define MON_ONCE 0x0010 /* Disable after calling */
163 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
165 #define MON_PERMIT 0x1000 /* Request is permitted */
167 struct mon_table mon_dispatch_proto20[] = {
168 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
169 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
170 {MONITOR_REQ_PWNAM, MON_AUTH, mm_answer_pwnamallow},
171 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
172 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
173 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
175 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
178 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
179 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
182 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
183 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
186 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
187 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
188 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
189 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
190 {MONITOR_REQ_GSSLOCALNAME, MON_AUTH, mm_answer_gss_localname},
192 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
193 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
197 struct mon_table mon_dispatch_postauth20[] = {
199 {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
200 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
201 {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
203 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
204 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
205 {MONITOR_REQ_PTY, 0, mm_answer_pty},
206 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
207 {MONITOR_REQ_TERM, 0, mm_answer_term},
211 struct mon_table mon_dispatch_proto15[] = {
212 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
213 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
214 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
215 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
216 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
217 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
218 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
219 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
221 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
222 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
225 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
226 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
229 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
230 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
231 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
232 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
235 {MONITOR_REQ_GSIGRIDMAP, MON_PERMIT, mm_answer_gsi_gridmap},
238 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
243 struct mon_table mon_dispatch_postauth15[] = {
245 {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
246 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
247 {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
249 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
250 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
251 {MONITOR_REQ_TERM, 0, mm_answer_term},
255 struct mon_table *mon_dispatch;
257 /* Specifies if a certain message is allowed at the moment */
260 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
262 while (ent->f != NULL) {
263 if (ent->type == type) {
264 ent->flags &= ~MON_PERMIT;
265 ent->flags |= permit ? MON_PERMIT : 0;
273 monitor_permit_authentications(int permit)
275 struct mon_table *ent = mon_dispatch;
277 while (ent->f != NULL) {
278 if (ent->flags & MON_AUTH) {
279 ent->flags &= ~MON_PERMIT;
280 ent->flags |= permit ? MON_PERMIT : 0;
287 monitor_child_preauth(struct monitor *pmonitor)
289 struct mon_table *ent;
290 int authenticated = 0;
292 debug3("preauth child monitor started");
295 mon_dispatch = mon_dispatch_proto20;
297 /* Permit requests for moduli and signatures */
298 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
299 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
301 /* and for the GSSAPI key exchange */
302 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
303 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
304 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
307 mon_dispatch = mon_dispatch_proto15;
309 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
311 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
314 monitor_permit(mon_dispatch, MONITOR_REQ_GSIGRIDMAP, 1);
318 authctxt = authctxt_new();
320 /* The first few requests do not require asynchronous access */
321 while (!authenticated) {
322 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
324 if (!(ent->flags & MON_AUTHDECIDE))
325 fatal("%s: unexpected authentication from %d",
326 __func__, ent->type);
327 if (authctxt->pw->pw_uid == 0 &&
328 !auth_root_allowed(auth_method))
331 if (!do_pam_account(authctxt->pw->pw_name, NULL))
336 if (ent->flags & MON_AUTHDECIDE) {
337 auth_log(authctxt, authenticated, auth_method,
338 compat20 ? " ssh2" : "");
340 authctxt->failures++;
344 if (!authctxt->valid)
345 fatal("%s: authenticated invalid user", __func__);
347 debug("%s: %s has been authenticated by privileged process",
348 __func__, authctxt->user);
350 mm_get_keystate(pmonitor);
356 monitor_child_postauth(struct monitor *pmonitor)
359 mon_dispatch = mon_dispatch_postauth20;
361 /* Permit requests for moduli and signatures */
362 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
363 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
364 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
367 mon_dispatch = mon_dispatch_postauth15;
368 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
371 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
372 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
376 monitor_read(pmonitor, mon_dispatch, NULL);
380 monitor_sync(struct monitor *pmonitor)
382 if (options.compression) {
383 /* The member allocation is not visible, so sync it */
384 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
389 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
390 struct mon_table **pent)
398 mm_request_receive(pmonitor->m_sendfd, &m);
399 type = buffer_get_char(&m);
401 debug3("%s: checking request %d", __func__, type);
403 while (ent->f != NULL) {
404 if (ent->type == type)
409 if (ent->f != NULL) {
410 if (!(ent->flags & MON_PERMIT))
411 fatal("%s: unpermitted request %d", __func__,
413 ret = (*ent->f)(pmonitor->m_sendfd, &m);
416 /* The child may use this request only once, disable it */
417 if (ent->flags & MON_ONCE) {
418 debug2("%s: %d used once, disabling now", __func__,
420 ent->flags &= ~MON_PERMIT;
429 fatal("%s: unsupported request: %d", __func__, type);
435 /* allowed key state */
437 monitor_allowed_key(u_char *blob, u_int bloblen)
439 /* make sure key is allowed */
440 if (key_blob == NULL || key_bloblen != bloblen ||
441 memcmp(key_blob, blob, key_bloblen))
447 monitor_reset_key_state(void)
450 if (key_blob != NULL)
452 if (hostbased_cuser != NULL)
453 xfree(hostbased_cuser);
454 if (hostbased_chost != NULL)
455 xfree(hostbased_chost);
458 key_blobtype = MM_NOKEY;
459 hostbased_cuser = NULL;
460 hostbased_chost = NULL;
464 mm_answer_moduli(int socket, Buffer *m)
469 min = buffer_get_int(m);
470 want = buffer_get_int(m);
471 max = buffer_get_int(m);
473 debug3("%s: got parameters: %d %d %d",
474 __func__, min, want, max);
475 /* We need to check here, too, in case the child got corrupted */
476 if (max < min || want < min || max < want)
477 fatal("%s: bad parameters: %d %d %d",
478 __func__, min, want, max);
482 dh = choose_dh(min, want, max);
484 buffer_put_char(m, 0);
487 /* Send first bignum */
488 buffer_put_char(m, 1);
489 buffer_put_bignum2(m, dh->p);
490 buffer_put_bignum2(m, dh->g);
494 mm_request_send(socket, MONITOR_ANS_MODULI, m);
499 mm_answer_sign(int socket, Buffer *m)
504 u_int siglen, datlen;
507 debug3("%s", __func__);
509 keyid = buffer_get_int(m);
510 p = buffer_get_string(m, &datlen);
513 fatal("%s: data length incorrect: %d", __func__, datlen);
515 /* save session id, it will be passed on the first call */
516 if (session_id2_len == 0) {
517 session_id2_len = datlen;
518 session_id2 = xmalloc(session_id2_len);
519 memcpy(session_id2, p, session_id2_len);
522 if ((key = get_hostkey_by_index(keyid)) == NULL)
523 fatal("%s: no hostkey from index %d", __func__, keyid);
524 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
525 fatal("%s: key_sign failed", __func__);
527 debug3("%s: signature %p(%d)", __func__, signature, siglen);
530 buffer_put_string(m, signature, siglen);
535 mm_request_send(socket, MONITOR_ANS_SIGN, m);
537 /* Turn on permissions for getpwnam */
538 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
543 /* Retrieves the password entry and also checks if the user is permitted */
546 mm_answer_pwnamallow(int socket, Buffer *m)
549 struct passwd *pwent;
552 debug3("%s", __func__);
554 login = buffer_get_string(m, NULL);
556 pwent = getpwnamallow(login);
558 if (authctxt->user) xfree(authctxt->user);
559 authctxt->user = xstrdup(login);
560 setproctitle("%s [priv]", pwent ? login : "unknown");
566 buffer_put_char(m, 0);
571 authctxt->pw = pwent;
574 buffer_put_char(m, 1);
575 buffer_put_string(m, pwent, sizeof(struct passwd));
576 buffer_put_cstring(m, pwent->pw_name);
577 buffer_put_cstring(m, "*");
578 buffer_put_cstring(m, pwent->pw_gecos);
579 #ifdef HAVE_PW_CLASS_IN_PASSWD
580 buffer_put_cstring(m, pwent->pw_class);
582 buffer_put_cstring(m, pwent->pw_dir);
583 buffer_put_cstring(m, pwent->pw_shell);
586 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
587 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
589 /* For SSHv1 allow authentication now */
591 monitor_permit_authentications(1);
593 /* Allow service/style information on the auth context */
594 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
595 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
599 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
605 int mm_answer_auth2_read_banner(int socket, Buffer *m)
610 banner = auth2_read_banner();
611 buffer_put_cstring(m, banner != NULL ? banner : "");
612 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
621 mm_answer_authserv(int socket, Buffer *m)
623 monitor_permit_authentications(1);
625 authctxt->service = buffer_get_string(m, NULL);
626 authctxt->style = buffer_get_string(m, NULL);
627 debug3("%s: service=%s, style=%s",
628 __func__, authctxt->service, authctxt->style);
630 if (strlen(authctxt->style) == 0) {
631 xfree(authctxt->style);
632 authctxt->style = NULL;
639 mm_answer_authpassword(int socket, Buffer *m)
641 static int call_count;
643 int authenticated, plen;
645 passwd = buffer_get_string(m, &plen);
646 /* Only authenticate if the context is valid */
647 authenticated = options.password_authentication &&
648 authctxt->valid && auth_password(authctxt, passwd);
649 memset(passwd, 0, strlen(passwd));
653 buffer_put_int(m, authenticated);
655 debug3("%s: sending result %d", __func__, authenticated);
656 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
659 if (plen == 0 && call_count == 1)
660 auth_method = "none";
662 auth_method = "password";
664 /* Causes monitor loop to terminate if authenticated */
665 return (authenticated);
670 mm_answer_bsdauthquery(int socket, Buffer *m)
672 char *name, *infotxt;
678 res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
682 buffer_put_int(m, res);
684 buffer_put_cstring(m, prompts[0]);
686 debug3("%s: sending challenge res: %d", __func__, res);
687 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
700 mm_answer_bsdauthrespond(int socket, Buffer *m)
705 if (authctxt->as == 0)
706 fatal("%s: no bsd auth session", __func__);
708 response = buffer_get_string(m, NULL);
709 authok = options.challenge_response_authentication &&
710 auth_userresponse(authctxt->as, response, 0);
712 debug3("%s: <%s> = <%d>", __func__, response, authok);
716 buffer_put_int(m, authok);
718 debug3("%s: sending authenticated: %d", __func__, authok);
719 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
721 auth_method = "bsdauth";
723 return (authok != 0);
729 mm_answer_skeyquery(int socket, Buffer *m)
732 char challenge[1024];
735 res = skeychallenge(&skey, authctxt->user, challenge);
738 buffer_put_int(m, res);
740 buffer_put_cstring(m, challenge);
742 debug3("%s: sending challenge res: %d", __func__, res);
743 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
749 mm_answer_skeyrespond(int socket, Buffer *m)
754 response = buffer_get_string(m, NULL);
756 authok = (options.challenge_response_authentication &&
758 skey_haskey(authctxt->pw->pw_name) == 0 &&
759 skey_passcheck(authctxt->pw->pw_name, response) != -1);
764 buffer_put_int(m, authok);
766 debug3("%s: sending authenticated: %d", __func__, authok);
767 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
769 auth_method = "skey";
771 return (authok != 0);
777 mm_answer_pam_start(int socket, Buffer *m)
781 user = buffer_get_string(m, NULL);
792 mm_append_debug(Buffer *m)
794 if (auth_debug_init && buffer_len(&auth_debug)) {
795 debug3("%s: Appending debug messages for child", __func__);
796 buffer_append(m, buffer_ptr(&auth_debug),
797 buffer_len(&auth_debug));
798 buffer_clear(&auth_debug);
803 mm_answer_keyallowed(int socket, Buffer *m)
806 u_char *cuser, *chost, *blob;
808 enum mm_keytype type = 0;
811 debug3("%s entering", __func__);
813 type = buffer_get_int(m);
814 cuser = buffer_get_string(m, NULL);
815 chost = buffer_get_string(m, NULL);
816 blob = buffer_get_string(m, &bloblen);
818 key = key_from_blob(blob, bloblen);
820 if ((compat20 && type == MM_RSAHOSTKEY) ||
821 (!compat20 && type != MM_RSAHOSTKEY))
822 fatal("%s: key type and protocol mismatch", __func__);
824 debug3("%s: key_from_blob: %p", __func__, key);
826 if (key != NULL && authctxt->pw != NULL) {
829 allowed = options.pubkey_authentication &&
830 user_key_allowed(authctxt->pw, key);
833 allowed = options.hostbased_authentication &&
834 hostbased_key_allowed(authctxt->pw,
838 key->type = KEY_RSA1; /* XXX */
839 allowed = options.rhosts_rsa_authentication &&
840 auth_rhosts_rsa_key_allowed(authctxt->pw,
844 fatal("%s: unknown key type %d", __func__, type);
850 /* clear temporarily storage (used by verify) */
851 monitor_reset_key_state();
854 /* Save temporarily for comparison in verify */
856 key_bloblen = bloblen;
858 hostbased_cuser = cuser;
859 hostbased_chost = chost;
862 debug3("%s: key %p is %s",
863 __func__, key, allowed ? "allowed" : "disallowed");
866 buffer_put_int(m, allowed);
870 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
872 if (type == MM_RSAHOSTKEY)
873 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
879 monitor_valid_userblob(u_char *data, u_int datalen)
887 buffer_append(&b, data, datalen);
889 if (datafellows & SSH_OLD_SESSIONID) {
891 len = buffer_len(&b);
892 if ((session_id2 == NULL) ||
893 (len < session_id2_len) ||
894 (memcmp(p, session_id2, session_id2_len) != 0))
896 buffer_consume(&b, session_id2_len);
898 p = buffer_get_string(&b, &len);
899 if ((session_id2 == NULL) ||
900 (len != session_id2_len) ||
901 (memcmp(p, session_id2, session_id2_len) != 0))
905 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
907 p = buffer_get_string(&b, NULL);
908 if (strcmp(authctxt->user, p) != 0) {
909 log("wrong user name passed to monitor: expected %s != %.100s",
914 buffer_skip_string(&b);
915 if (datafellows & SSH_BUG_PKAUTH) {
916 if (!buffer_get_char(&b))
919 p = buffer_get_string(&b, NULL);
920 if (strcmp("publickey", p) != 0)
923 if (!buffer_get_char(&b))
925 buffer_skip_string(&b);
927 buffer_skip_string(&b);
928 if (buffer_len(&b) != 0)
935 monitor_valid_hostbasedblob(u_char *data, u_int datalen, u_char *cuser,
944 buffer_append(&b, data, datalen);
946 p = buffer_get_string(&b, &len);
947 if ((session_id2 == NULL) ||
948 (len != session_id2_len) ||
949 (memcmp(p, session_id2, session_id2_len) != 0))
953 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
955 p = buffer_get_string(&b, NULL);
956 if (strcmp(authctxt->user, p) != 0) {
957 log("wrong user name passed to monitor: expected %s != %.100s",
962 buffer_skip_string(&b); /* service */
963 p = buffer_get_string(&b, NULL);
964 if (strcmp(p, "hostbased") != 0)
967 buffer_skip_string(&b); /* pkalg */
968 buffer_skip_string(&b); /* pkblob */
970 /* verify client host, strip trailing dot if necessary */
971 p = buffer_get_string(&b, NULL);
972 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
974 if (strcmp(p, chost) != 0)
978 /* verify client user */
979 p = buffer_get_string(&b, NULL);
980 if (strcmp(p, cuser) != 0)
984 if (buffer_len(&b) != 0)
991 mm_answer_keyverify(int socket, Buffer *m)
994 u_char *signature, *data, *blob;
995 u_int signaturelen, datalen, bloblen;
999 blob = buffer_get_string(m, &bloblen);
1000 signature = buffer_get_string(m, &signaturelen);
1001 data = buffer_get_string(m, &datalen);
1003 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1004 !monitor_allowed_key(blob, bloblen))
1005 fatal("%s: bad key, not previously allowed", __func__);
1007 key = key_from_blob(blob, bloblen);
1009 fatal("%s: bad public key blob", __func__);
1011 switch (key_blobtype) {
1013 valid_data = monitor_valid_userblob(data, datalen);
1016 valid_data = monitor_valid_hostbasedblob(data, datalen,
1017 hostbased_cuser, hostbased_chost);
1024 fatal("%s: bad signature data blob", __func__);
1026 verified = key_verify(key, signature, signaturelen, data, datalen);
1027 debug3("%s: key %p signature %s",
1028 __func__, key, verified ? "verified" : "unverified");
1035 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1037 monitor_reset_key_state();
1040 buffer_put_int(m, verified);
1041 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1047 mm_record_login(Session *s, struct passwd *pw)
1050 struct sockaddr_storage from;
1053 * Get IP address of client. If the connection is not a socket, let
1054 * the address be 0.0.0.0.
1056 memset(&from, 0, sizeof(from));
1057 if (packet_connection_is_on_socket()) {
1058 fromlen = sizeof(from);
1059 if (getpeername(packet_get_connection_in(),
1060 (struct sockaddr *) & from, &fromlen) < 0) {
1061 debug("getpeername: %.100s", strerror(errno));
1065 /* Record that there was a login on that tty from the remote host. */
1066 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1067 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
1068 (struct sockaddr *)&from);
1072 mm_session_close(Session *s)
1074 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
1075 if (s->ttyfd != -1) {
1076 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1077 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1078 session_pty_cleanup2(s);
1084 mm_answer_pty(int socket, Buffer *m)
1086 extern struct monitor *pmonitor;
1090 debug3("%s entering", __func__);
1096 s->authctxt = authctxt;
1097 s->pw = authctxt->pw;
1098 s->pid = pmonitor->m_pid;
1099 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1102 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1103 pty_setowner(authctxt->pw, s->tty);
1105 buffer_put_int(m, 1);
1106 buffer_put_cstring(m, s->tty);
1107 mm_request_send(socket, MONITOR_ANS_PTY, m);
1109 mm_send_fd(socket, s->ptyfd);
1110 mm_send_fd(socket, s->ttyfd);
1112 /* We need to trick ttyslot */
1113 if (dup2(s->ttyfd, 0) == -1)
1114 fatal("%s: dup2", __func__);
1116 mm_record_login(s, authctxt->pw);
1118 /* Now we can close the file descriptor again */
1121 /* make sure nothing uses fd 0 */
1122 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1123 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1125 error("%s: fd0 %d != 0", __func__, fd0);
1127 /* slave is not needed */
1129 s->ttyfd = s->ptyfd;
1130 /* no need to dup() because nobody closes ptyfd */
1131 s->ptymaster = s->ptyfd;
1133 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1139 mm_session_close(s);
1140 buffer_put_int(m, 0);
1141 mm_request_send(socket, MONITOR_ANS_PTY, m);
1146 mm_answer_pty_cleanup(int socket, Buffer *m)
1151 debug3("%s entering", __func__);
1153 tty = buffer_get_string(m, NULL);
1154 if ((s = session_by_tty(tty)) != NULL)
1155 mm_session_close(s);
1162 mm_answer_sesskey(int socket, Buffer *m)
1167 /* Turn off permissions */
1168 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1170 if ((p = BN_new()) == NULL)
1171 fatal("%s: BN_new", __func__);
1173 buffer_get_bignum2(m, p);
1175 rsafail = ssh1_session_key(p);
1178 buffer_put_int(m, rsafail);
1179 buffer_put_bignum2(m, p);
1183 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1185 /* Turn on permissions for sessid passing */
1186 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1192 mm_answer_sessid(int socket, Buffer *m)
1196 debug3("%s entering", __func__);
1198 if (buffer_len(m) != 16)
1199 fatal("%s: bad ssh1 session id", __func__);
1200 for (i = 0; i < 16; i++)
1201 session_id[i] = buffer_get_char(m);
1203 /* Turn on permissions for getpwnam */
1204 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1210 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1214 u_char *blob = NULL;
1218 debug3("%s entering", __func__);
1220 if (options.rsa_authentication && authctxt->valid) {
1221 if ((client_n = BN_new()) == NULL)
1222 fatal("%s: BN_new", __func__);
1223 buffer_get_bignum2(m, client_n);
1224 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1225 BN_clear_free(client_n);
1228 buffer_put_int(m, allowed);
1230 /* clear temporarily storage (used by generate challenge) */
1231 monitor_reset_key_state();
1233 if (allowed && key != NULL) {
1234 key->type = KEY_RSA; /* cheat for key_to_blob */
1235 if (key_to_blob(key, &blob, &blen) == 0)
1236 fatal("%s: key_to_blob failed", __func__);
1237 buffer_put_string(m, blob, blen);
1239 /* Save temporarily for comparison in verify */
1242 key_blobtype = MM_RSAUSERKEY;
1248 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1250 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1251 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1256 mm_answer_rsa_challenge(int socket, Buffer *m)
1262 debug3("%s entering", __func__);
1264 if (!authctxt->valid)
1265 fatal("%s: authctxt not valid", __func__);
1266 blob = buffer_get_string(m, &blen);
1267 if (!monitor_allowed_key(blob, blen))
1268 fatal("%s: bad key, not previously allowed", __func__);
1269 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1270 fatal("%s: key type mismatch", __func__);
1271 if ((key = key_from_blob(blob, blen)) == NULL)
1272 fatal("%s: received bad key", __func__);
1275 BN_clear_free(ssh1_challenge);
1276 ssh1_challenge = auth_rsa_generate_challenge(key);
1279 buffer_put_bignum2(m, ssh1_challenge);
1281 debug3("%s sending reply", __func__);
1282 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1284 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1289 mm_answer_rsa_response(int socket, Buffer *m)
1292 u_char *blob, *response;
1296 debug3("%s entering", __func__);
1298 if (!authctxt->valid)
1299 fatal("%s: authctxt not valid", __func__);
1300 if (ssh1_challenge == NULL)
1301 fatal("%s: no ssh1_challenge", __func__);
1303 blob = buffer_get_string(m, &blen);
1304 if (!monitor_allowed_key(blob, blen))
1305 fatal("%s: bad key, not previously allowed", __func__);
1306 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1307 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1308 if ((key = key_from_blob(blob, blen)) == NULL)
1309 fatal("%s: received bad key", __func__);
1310 response = buffer_get_string(m, &len);
1312 fatal("%s: received bad response to challenge", __func__);
1313 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1318 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1321 BN_clear_free(ssh1_challenge);
1322 ssh1_challenge = NULL;
1323 monitor_reset_key_state();
1326 buffer_put_int(m, success);
1327 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1333 mm_answer_term(int socket, Buffer *req)
1335 extern struct monitor *pmonitor;
1338 debug3("%s: tearing down sessions", __func__);
1340 /* The child is terminating */
1341 session_destroy_all(&mm_session_close);
1343 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1347 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1349 /* Terminate process */
1354 monitor_apply_keystate(struct monitor *pmonitor)
1357 set_newkeys(MODE_IN);
1358 set_newkeys(MODE_OUT);
1360 packet_set_protocol_flags(child_state.ssh1protoflags);
1361 packet_set_encryption_key(child_state.ssh1key,
1362 child_state.ssh1keylen, child_state.ssh1cipher);
1363 xfree(child_state.ssh1key);
1366 /* for rc4 and other stateful ciphers */
1367 packet_set_keycontext(MODE_OUT, child_state.keyout);
1368 xfree(child_state.keyout);
1369 packet_set_keycontext(MODE_IN, child_state.keyin);
1370 xfree(child_state.keyin);
1373 packet_set_iv(MODE_OUT, child_state.ivout);
1374 xfree(child_state.ivout);
1375 packet_set_iv(MODE_IN, child_state.ivin);
1376 xfree(child_state.ivin);
1379 memcpy(&incoming_stream, &child_state.incoming,
1380 sizeof(incoming_stream));
1381 memcpy(&outgoing_stream, &child_state.outgoing,
1382 sizeof(outgoing_stream));
1384 /* Update with new address */
1385 if (options.compression)
1386 mm_init_compression(pmonitor->m_zlib);
1388 /* Network I/O buffers */
1389 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1390 buffer_clear(&input);
1391 buffer_append(&input, child_state.input, child_state.ilen);
1392 memset(child_state.input, 0, child_state.ilen);
1393 xfree(child_state.input);
1395 buffer_clear(&output);
1396 buffer_append(&output, child_state.output, child_state.olen);
1397 memset(child_state.output, 0, child_state.olen);
1398 xfree(child_state.output);
1402 mm_get_kex(Buffer *m)
1408 kex = xmalloc(sizeof(*kex));
1409 memset(kex, 0, sizeof(*kex));
1410 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1411 if ((session_id2 == NULL) ||
1412 (kex->session_id_len != session_id2_len) ||
1413 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1414 fatal("mm_get_get: internal error: bad session id");
1415 kex->we_need = buffer_get_int(m);
1417 kex->hostkey_type = buffer_get_int(m);
1418 kex->kex_type = buffer_get_int(m);
1419 blob = buffer_get_string(m, &bloblen);
1420 buffer_init(&kex->my);
1421 buffer_append(&kex->my, blob, bloblen);
1423 blob = buffer_get_string(m, &bloblen);
1424 buffer_init(&kex->peer);
1425 buffer_append(&kex->peer, blob, bloblen);
1428 kex->flags = buffer_get_int(m);
1429 kex->client_version_string = buffer_get_string(m, NULL);
1430 kex->server_version_string = buffer_get_string(m, NULL);
1431 kex->load_host_key=&get_hostkey_by_type;
1432 kex->host_key_index=&get_hostkey_index;
1437 /* This function requries careful sanity checking */
1440 mm_get_keystate(struct monitor *pmonitor)
1444 u_int bloblen, plen;
1446 debug3("%s: Waiting for new keys", __func__);
1449 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1451 child_state.ssh1protoflags = buffer_get_int(&m);
1452 child_state.ssh1cipher = buffer_get_int(&m);
1453 child_state.ssh1key = buffer_get_string(&m,
1454 &child_state.ssh1keylen);
1455 child_state.ivout = buffer_get_string(&m,
1456 &child_state.ivoutlen);
1457 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1460 /* Get the Kex for rekeying */
1461 *pmonitor->m_pkex = mm_get_kex(&m);
1464 blob = buffer_get_string(&m, &bloblen);
1465 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1468 debug3("%s: Waiting for second key", __func__);
1469 blob = buffer_get_string(&m, &bloblen);
1470 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1473 /* Now get sequence numbers for the packets */
1474 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1475 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1478 /* Get the key context */
1479 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1480 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1482 debug3("%s: Getting compression state", __func__);
1483 /* Get compression state */
1484 p = buffer_get_string(&m, &plen);
1485 if (plen != sizeof(child_state.outgoing))
1486 fatal("%s: bad request size", __func__);
1487 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1490 p = buffer_get_string(&m, &plen);
1491 if (plen != sizeof(child_state.incoming))
1492 fatal("%s: bad request size", __func__);
1493 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1496 /* Network I/O buffers */
1497 debug3("%s: Getting Network I/O buffers", __func__);
1498 child_state.input = buffer_get_string(&m, &child_state.ilen);
1499 child_state.output = buffer_get_string(&m, &child_state.olen);
1505 /* Allocation functions for zlib */
1507 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1509 int len = size * ncount;
1513 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1515 address = mm_malloc(mm, len);
1521 mm_zfree(struct mm_master *mm, void *address)
1523 mm_free(mm, address);
1527 mm_init_compression(struct mm_master *mm)
1529 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1530 outgoing_stream.zfree = (free_func)mm_zfree;
1531 outgoing_stream.opaque = mm;
1533 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1534 incoming_stream.zfree = (free_func)mm_zfree;
1535 incoming_stream.opaque = mm;
1540 #define FD_CLOSEONEXEC(x) do { \
1541 if (fcntl(x, F_SETFD, 1) == -1) \
1542 fatal("fcntl(%d, F_SETFD)", x); \
1546 monitor_socketpair(int *pair)
1548 #ifdef HAVE_SOCKETPAIR
1549 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1550 fatal("%s: socketpair", __func__);
1552 fatal("%s: UsePrivilegeSeparation=yes not supported",
1555 FD_CLOSEONEXEC(pair[0]);
1556 FD_CLOSEONEXEC(pair[1]);
1559 #define MM_MEMSIZE 65536
1564 struct monitor *mon;
1567 mon = xmalloc(sizeof(*mon));
1569 monitor_socketpair(pair);
1571 mon->m_recvfd = pair[0];
1572 mon->m_sendfd = pair[1];
1574 /* Used to share zlib space across processes */
1575 if (options.compression) {
1576 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1577 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1579 /* Compression needs to share state across borders */
1580 mm_init_compression(mon->m_zlib);
1587 monitor_reinit(struct monitor *mon)
1591 monitor_socketpair(pair);
1593 mon->m_recvfd = pair[0];
1594 mon->m_sendfd = pair[1];
1600 mm_answer_gss_setup_ctx(int socket, Buffer *m) {
1605 oid.elements=buffer_get_string(m,&len);
1608 major=ssh_gssapi_server_ctx(&gsscontext,&oid);
1610 xfree(oid.elements);
1613 buffer_put_int(m,major);
1615 mm_request_send(socket,MONITOR_ANS_GSSSETUP,m);
1621 mm_answer_gss_accept_ctx(int socket, Buffer *m) {
1622 gss_buffer_desc in,out;
1623 OM_uint32 major,minor;
1624 OM_uint32 flags = 0; /* GSI needs this */
1626 in.value = buffer_get_string(m,&in.length);
1627 major=ssh_gssapi_accept_ctx(gsscontext,&in,&out,&flags);
1631 buffer_put_int(m, major);
1632 buffer_put_string(m, out.value, out.length);
1633 buffer_put_int(m, flags);
1634 mm_request_send(socket,MONITOR_ANS_GSSSTEP,m);
1636 gss_release_buffer(&minor, &out);
1641 mm_answer_gss_userok(int socket, Buffer *m) {
1644 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1647 buffer_put_int(m, authenticated);
1649 debug3("%s: sending result %d", __func__, authenticated);
1650 mm_request_send(socket, MONITOR_ANS_GSSUSEROK, m);
1652 auth_method="gssapi";
1654 /* Monitor loop will terminate if authenticated */
1655 return(authenticated);
1658 mm_answer_gss_localname(int socket, Buffer *m) {
1661 ssh_gssapi_localname(&name);
1665 buffer_put_cstring(m, name);
1666 debug3("%s: sending result %s", __func__, name);
1669 buffer_put_cstring(m, "");
1670 debug3("%s: sending result \"\"", __func__);
1673 mm_request_send(socket, MONITOR_ANS_GSSLOCALNAME, m);
1679 mm_answer_gss_sign(int socket, Buffer *m) {
1680 gss_buffer_desc data,hash;
1681 OM_uint32 major,minor;
1683 data.value = buffer_get_string(m,&data.length);
1684 if (data.length != 16) { /* HACK - i.e. we are using SSHv1 */
1685 if (data.length != 20)
1686 fatal("%s: data length incorrect: %d", __func__, data.length);
1688 /* Save the session ID - only first time round */
1689 if (session_id2_len == 0) {
1690 session_id2_len=data.length;
1691 session_id2 = xmalloc(session_id2_len);
1692 memcpy(session_id2, data.value, session_id2_len);
1695 major=ssh_gssapi_sign(gsscontext, &data, &hash);
1700 buffer_put_int(m, major);
1701 buffer_put_string(m, hash.value, hash.length);
1703 mm_request_send(socket,MONITOR_ANS_GSSSIGN,m);
1705 gss_release_buffer(&minor,&hash);
1707 /* Turn on permissions for getpwnam */
1708 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1718 mm_answer_gsi_gridmap(int socket, Buffer *m) {
1719 char *subject, *name;
1721 subject = buffer_get_string(m, NULL);
1723 gsi_gridmap(subject, &name);
1727 buffer_put_cstring(m, name);
1728 debug3("%s: sending result %s", __func__, name);
1731 buffer_put_cstring(m, "");
1732 debug3("%s: sending result \"\"", __func__);
1735 mm_request_send(socket, MONITOR_ANS_GSIGRIDMAP, m);