1 /* $OpenBSD: monitor.c,v 1.101 2009/02/12 03:26:22 djm Exp $ */
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include "openbsd-compat/sys-tree.h"
52 #include <openssl/dh.h>
54 #include "openbsd-compat/sys-queue.h"
64 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
67 #define TARGET_OS_MAC 1
72 #include "auth-options.h"
81 #include "monitor_mm.h"
85 #include "monitor_wrap.h"
86 #include "monitor_fdpass.h"
93 static Gssctxt *gsscontext = NULL;
97 extern ServerOptions options;
98 extern u_int utmp_len;
99 extern Newkeys *current_keys[];
100 extern z_stream incoming_stream;
101 extern z_stream outgoing_stream;
102 extern u_char session_id[];
103 extern Buffer input, output;
104 extern Buffer auth_debug;
105 extern int auth_debug_init;
106 extern Buffer loginmsg;
108 /* State exported from the child */
131 /* Functions on the monitor that answer unprivileged requests */
133 int mm_answer_moduli(int, Buffer *);
134 int mm_answer_sign(int, Buffer *);
135 int mm_answer_pwnamallow(int, Buffer *);
136 int mm_answer_auth2_read_banner(int, Buffer *);
137 int mm_answer_authserv(int, Buffer *);
138 int mm_answer_authpassword(int, Buffer *);
139 int mm_answer_bsdauthquery(int, Buffer *);
140 int mm_answer_bsdauthrespond(int, Buffer *);
141 int mm_answer_skeyquery(int, Buffer *);
142 int mm_answer_skeyrespond(int, Buffer *);
143 int mm_answer_keyallowed(int, Buffer *);
144 int mm_answer_keyverify(int, Buffer *);
145 int mm_answer_pty(int, Buffer *);
146 int mm_answer_pty_cleanup(int, Buffer *);
147 int mm_answer_term(int, Buffer *);
148 int mm_answer_rsa_keyallowed(int, Buffer *);
149 int mm_answer_rsa_challenge(int, Buffer *);
150 int mm_answer_rsa_response(int, Buffer *);
151 int mm_answer_sesskey(int, Buffer *);
152 int mm_answer_sessid(int, Buffer *);
153 int mm_answer_jpake_get_pwdata(int, Buffer *);
154 int mm_answer_jpake_step1(int, Buffer *);
155 int mm_answer_jpake_step2(int, Buffer *);
156 int mm_answer_jpake_key_confirm(int, Buffer *);
157 int mm_answer_jpake_check_confirm(int, Buffer *);
160 int mm_answer_pam_start(int, Buffer *);
161 int mm_answer_pam_account(int, Buffer *);
162 int mm_answer_pam_init_ctx(int, Buffer *);
163 int mm_answer_pam_query(int, Buffer *);
164 int mm_answer_pam_respond(int, Buffer *);
165 int mm_answer_pam_free_ctx(int, Buffer *);
169 int mm_answer_gss_setup_ctx(int, Buffer *);
170 int mm_answer_gss_accept_ctx(int, Buffer *);
171 int mm_answer_gss_userok(int, Buffer *);
172 int mm_answer_gss_checkmic(int, Buffer *);
173 int mm_answer_gss_sign(int, Buffer *);
174 int mm_answer_gss_error(int, Buffer *);
175 int mm_answer_gss_indicate_mechs(int, Buffer *);
176 int mm_answer_gss_localname(int, Buffer *);
177 int mm_answer_gss_updatecreds(int, Buffer *);
180 #ifdef SSH_AUDIT_EVENTS
181 int mm_answer_audit_event(int, Buffer *);
182 int mm_answer_audit_command(int, Buffer *);
185 static Authctxt *authctxt;
186 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
188 /* local state for key verify */
189 static u_char *key_blob = NULL;
190 static u_int key_bloblen = 0;
191 static int key_blobtype = MM_NOKEY;
192 static char *hostbased_cuser = NULL;
193 static char *hostbased_chost = NULL;
194 static char *auth_method = "unknown";
195 static u_int session_id2_len = 0;
196 static u_char *session_id2 = NULL;
197 static pid_t monitor_child_pid;
200 enum monitor_reqtype type;
202 int (*f)(int, Buffer *);
205 #define MON_ISAUTH 0x0004 /* Required for Authentication */
206 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
207 #define MON_ONCE 0x0010 /* Disable after calling */
208 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
210 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
212 #define MON_PERMIT 0x1000 /* Request is permitted */
214 struct mon_table mon_dispatch_proto20[] = {
215 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
216 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
217 {MONITOR_REQ_PWNAM, MON_AUTH, mm_answer_pwnamallow},
218 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
219 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
220 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
222 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
223 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
224 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
225 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
226 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
227 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
229 #ifdef SSH_AUDIT_EVENTS
230 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
233 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
234 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
237 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
238 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
240 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
241 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
243 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
244 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
245 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
246 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
247 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
248 {MONITOR_REQ_GSSERR, MON_ISAUTH | MON_ONCE, mm_answer_gss_error},
249 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
250 {MONITOR_REQ_GSSLOCALNAME, MON_ISAUTH, mm_answer_gss_localname},
253 {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
254 {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
255 {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
256 {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
257 {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
262 struct mon_table mon_dispatch_postauth20[] = {
264 {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
265 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
266 {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
267 {MONITOR_REQ_GSSERR, 0, mm_answer_gss_error},
268 {MONITOR_REQ_GSSMECHS, 0, mm_answer_gss_indicate_mechs},
269 {MONITOR_REQ_GSSUPCREDS, 0, mm_answer_gss_updatecreds},
271 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
272 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
273 {MONITOR_REQ_PTY, 0, mm_answer_pty},
274 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
275 {MONITOR_REQ_TERM, 0, mm_answer_term},
276 #ifdef SSH_AUDIT_EVENTS
277 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
278 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
283 struct mon_table mon_dispatch_proto15[] = {
284 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
285 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
286 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
287 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
288 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
289 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
290 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
291 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
293 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
294 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
297 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
298 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
301 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
302 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
303 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
304 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
305 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
308 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
309 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
310 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
311 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
312 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
313 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
315 #ifdef SSH_AUDIT_EVENTS
316 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
321 struct mon_table mon_dispatch_postauth15[] = {
322 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
323 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
324 {MONITOR_REQ_TERM, 0, mm_answer_term},
325 #ifdef SSH_AUDIT_EVENTS
326 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
327 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
332 struct mon_table *mon_dispatch;
334 /* Specifies if a certain message is allowed at the moment */
337 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
339 while (ent->f != NULL) {
340 if (ent->type == type) {
341 ent->flags &= ~MON_PERMIT;
342 ent->flags |= permit ? MON_PERMIT : 0;
350 monitor_permit_authentications(int permit)
352 struct mon_table *ent = mon_dispatch;
354 while (ent->f != NULL) {
355 if (ent->flags & MON_AUTH) {
356 ent->flags &= ~MON_PERMIT;
357 ent->flags |= permit ? MON_PERMIT : 0;
364 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
366 struct mon_table *ent;
367 int authenticated = 0;
369 debug3("preauth child monitor started");
371 authctxt = _authctxt;
372 memset(authctxt, 0, sizeof(*authctxt));
374 authctxt->loginmsg = &loginmsg;
377 mon_dispatch = mon_dispatch_proto20;
379 /* Permit requests for moduli and signatures */
380 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
381 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
383 /* and for the GSSAPI key exchange */
384 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
385 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
386 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS, 1);
389 mon_dispatch = mon_dispatch_proto15;
391 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
394 /* The first few requests do not require asynchronous access */
395 while (!authenticated) {
396 auth_method = "unknown";
397 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
399 if (!(ent->flags & MON_AUTHDECIDE))
400 fatal("%s: unexpected authentication from %d",
401 __func__, ent->type);
402 if (authctxt->pw->pw_uid == 0 &&
403 !auth_root_allowed(auth_method))
406 /* PAM needs to perform account checks after auth */
407 if (options.use_pam && authenticated) {
411 mm_request_receive_expect(pmonitor->m_sendfd,
412 MONITOR_REQ_PAM_ACCOUNT, &m);
413 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
419 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
420 auth_log(authctxt, authenticated, auth_method,
421 compat20 ? " ssh2" : "");
423 authctxt->failures++;
426 /* Cleanup JPAKE context after authentication */
427 if (ent->flags & MON_AUTHDECIDE) {
428 if (authctxt->jpake_ctx != NULL) {
429 jpake_free(authctxt->jpake_ctx);
430 authctxt->jpake_ctx = NULL;
436 if (!authctxt->valid)
437 fatal("%s: authenticated invalid user", __func__);
438 if (strcmp(auth_method, "unknown") == 0)
439 fatal("%s: authentication method name unknown", __func__);
441 debug("%s: %s has been authenticated by privileged process",
442 __func__, authctxt->user);
444 mm_get_keystate(pmonitor);
448 monitor_set_child_handler(pid_t pid)
450 monitor_child_pid = pid;
454 monitor_child_handler(int sig)
456 kill(monitor_child_pid, sig);
460 monitor_child_postauth(struct monitor *pmonitor)
462 monitor_set_child_handler(pmonitor->m_pid);
463 signal(SIGHUP, &monitor_child_handler);
464 signal(SIGTERM, &monitor_child_handler);
465 signal(SIGINT, &monitor_child_handler);
468 mon_dispatch = mon_dispatch_postauth20;
470 /* Permit requests for moduli and signatures */
471 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
472 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
473 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
476 /* and for the GSSAPI key exchange */
477 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS,1);
478 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP,1);
479 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR,1);
483 mon_dispatch = mon_dispatch_postauth15;
484 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
487 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
490 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
491 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
495 monitor_read(pmonitor, mon_dispatch, NULL);
499 monitor_sync(struct monitor *pmonitor)
501 if (options.compression) {
502 /* The member allocation is not visible, so sync it */
503 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
508 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
509 struct mon_table **pent)
517 mm_request_receive(pmonitor->m_sendfd, &m);
518 type = buffer_get_char(&m);
520 debug3("%s: checking request %d", __func__, type);
522 while (ent->f != NULL) {
523 if (ent->type == type)
528 if (ent->f != NULL) {
529 if (!(ent->flags & MON_PERMIT))
530 fatal("%s: unpermitted request %d", __func__,
532 ret = (*ent->f)(pmonitor->m_sendfd, &m);
535 /* The child may use this request only once, disable it */
536 if (ent->flags & MON_ONCE) {
537 debug2("%s: %d used once, disabling now", __func__,
539 ent->flags &= ~MON_PERMIT;
548 fatal("%s: unsupported request: %d", __func__, type);
554 /* allowed key state */
556 monitor_allowed_key(u_char *blob, u_int bloblen)
558 /* make sure key is allowed */
559 if (key_blob == NULL || key_bloblen != bloblen ||
560 memcmp(key_blob, blob, key_bloblen))
566 monitor_reset_key_state(void)
569 if (key_blob != NULL)
571 if (hostbased_cuser != NULL)
572 xfree(hostbased_cuser);
573 if (hostbased_chost != NULL)
574 xfree(hostbased_chost);
577 key_blobtype = MM_NOKEY;
578 hostbased_cuser = NULL;
579 hostbased_chost = NULL;
583 mm_answer_moduli(int sock, Buffer *m)
588 min = buffer_get_int(m);
589 want = buffer_get_int(m);
590 max = buffer_get_int(m);
592 debug3("%s: got parameters: %d %d %d",
593 __func__, min, want, max);
594 /* We need to check here, too, in case the child got corrupted */
595 if (max < min || want < min || max < want)
596 fatal("%s: bad parameters: %d %d %d",
597 __func__, min, want, max);
601 dh = choose_dh(min, want, max);
603 buffer_put_char(m, 0);
606 /* Send first bignum */
607 buffer_put_char(m, 1);
608 buffer_put_bignum2(m, dh->p);
609 buffer_put_bignum2(m, dh->g);
613 mm_request_send(sock, MONITOR_ANS_MODULI, m);
618 mm_answer_sign(int sock, Buffer *m)
623 u_int siglen, datlen;
626 debug3("%s", __func__);
628 keyid = buffer_get_int(m);
629 p = buffer_get_string(m, &datlen);
632 * Supported KEX types will only return SHA1 (20 byte) or
633 * SHA256 (32 byte) hashes
635 if (datlen != 20 && datlen != 32)
636 fatal("%s: data length incorrect: %u", __func__, datlen);
638 /* save session id, it will be passed on the first call */
639 if (session_id2_len == 0) {
640 session_id2_len = datlen;
641 session_id2 = xmalloc(session_id2_len);
642 memcpy(session_id2, p, session_id2_len);
645 if ((key = get_hostkey_by_index(keyid)) == NULL)
646 fatal("%s: no hostkey from index %d", __func__, keyid);
647 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
648 fatal("%s: key_sign failed", __func__);
650 debug3("%s: signature %p(%u)", __func__, signature, siglen);
653 buffer_put_string(m, signature, siglen);
658 mm_request_send(sock, MONITOR_ANS_SIGN, m);
660 /* Turn on permissions for getpwnam */
661 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
666 /* Retrieves the password entry and also checks if the user is permitted */
669 mm_answer_pwnamallow(int sock, Buffer *m)
672 struct passwd *pwent;
675 debug3("%s", __func__);
677 username = buffer_get_string(m, NULL);
679 pwent = getpwnamallow(username);
681 if (authctxt->user) xfree(authctxt->user);
682 authctxt->user = xstrdup(username);
684 if (options.permit_pam_user_change)
685 setproctitle("%s [priv]", pwent ? "[pam]" : "unknown");
688 setproctitle("%s [priv]", pwent ? username : "unknown");
694 buffer_put_char(m, 0);
695 authctxt->pw = fakepw();
700 authctxt->pw = pwent;
703 buffer_put_char(m, 1);
704 buffer_put_string(m, pwent, sizeof(struct passwd));
705 buffer_put_cstring(m, pwent->pw_name);
706 buffer_put_cstring(m, "*");
707 buffer_put_cstring(m, pwent->pw_gecos);
708 #ifdef HAVE_PW_CLASS_IN_PASSWD
709 buffer_put_cstring(m, pwent->pw_class);
711 buffer_put_cstring(m, pwent->pw_dir);
712 buffer_put_cstring(m, pwent->pw_shell);
715 buffer_put_string(m, &options, sizeof(options));
716 if (options.banner != NULL)
717 buffer_put_cstring(m, options.banner);
718 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
719 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
721 /* For SSHv1 allow authentication now */
723 monitor_permit_authentications(1);
725 /* Allow service/style information on the auth context */
726 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
727 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
732 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
738 int mm_answer_auth2_read_banner(int sock, Buffer *m)
743 banner = auth2_read_banner();
744 buffer_put_cstring(m, banner != NULL ? banner : "");
745 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
754 mm_answer_authserv(int sock, Buffer *m)
756 monitor_permit_authentications(1);
758 authctxt->service = buffer_get_string(m, NULL);
759 authctxt->style = buffer_get_string(m, NULL);
760 debug3("%s: service=%s, style=%s",
761 __func__, authctxt->service, authctxt->style);
763 if (strlen(authctxt->style) == 0) {
764 xfree(authctxt->style);
765 authctxt->style = NULL;
772 mm_answer_authpassword(int sock, Buffer *m)
774 static int call_count;
779 passwd = buffer_get_string(m, &plen);
780 /* Only authenticate if the context is valid */
781 authenticated = options.password_authentication &&
782 auth_password(authctxt, passwd);
783 memset(passwd, 0, strlen(passwd));
787 buffer_put_int(m, authenticated);
789 debug3("%s: sending result %d", __func__, authenticated);
790 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
793 if (plen == 0 && call_count == 1)
794 auth_method = "none";
796 auth_method = "password";
798 /* Causes monitor loop to terminate if authenticated */
799 return (authenticated);
804 mm_answer_bsdauthquery(int sock, Buffer *m)
806 char *name, *infotxt;
812 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
813 &prompts, &echo_on) < 0 ? 0 : 1;
816 buffer_put_int(m, success);
818 buffer_put_cstring(m, prompts[0]);
820 debug3("%s: sending challenge success: %u", __func__, success);
821 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
834 mm_answer_bsdauthrespond(int sock, Buffer *m)
839 if (authctxt->as == 0)
840 fatal("%s: no bsd auth session", __func__);
842 response = buffer_get_string(m, NULL);
843 authok = options.challenge_response_authentication &&
844 auth_userresponse(authctxt->as, response, 0);
846 debug3("%s: <%s> = <%d>", __func__, response, authok);
850 buffer_put_int(m, authok);
852 debug3("%s: sending authenticated: %d", __func__, authok);
853 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
855 auth_method = "bsdauth";
857 return (authok != 0);
863 mm_answer_skeyquery(int sock, Buffer *m)
866 char challenge[1024];
869 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
870 sizeof(challenge)) < 0 ? 0 : 1;
873 buffer_put_int(m, success);
875 buffer_put_cstring(m, challenge);
877 debug3("%s: sending challenge success: %u", __func__, success);
878 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
884 mm_answer_skeyrespond(int sock, Buffer *m)
889 response = buffer_get_string(m, NULL);
891 authok = (options.challenge_response_authentication &&
893 skey_haskey(authctxt->pw->pw_name) == 0 &&
894 skey_passcheck(authctxt->pw->pw_name, response) != -1);
899 buffer_put_int(m, authok);
901 debug3("%s: sending authenticated: %d", __func__, authok);
902 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
904 auth_method = "skey";
906 return (authok != 0);
912 mm_answer_pam_start(int sock, Buffer *m)
914 if (!options.use_pam)
915 fatal("UsePAM not set, but ended up in %s anyway", __func__);
919 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
925 mm_answer_pam_account(int sock, Buffer *m)
929 if (!options.use_pam)
930 fatal("UsePAM not set, but ended up in %s anyway", __func__);
932 ret = do_pam_account();
934 buffer_put_int(m, ret);
935 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
937 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
942 static void *sshpam_ctxt, *sshpam_authok;
943 extern KbdintDevice sshpam_device;
946 mm_answer_pam_init_ctx(int sock, Buffer *m)
949 debug3("%s", __func__);
950 authctxt->user = buffer_get_string(m, NULL);
951 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
952 sshpam_authok = NULL;
954 if (sshpam_ctxt != NULL) {
955 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
956 buffer_put_int(m, 1);
958 buffer_put_int(m, 0);
960 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
965 mm_answer_pam_query(int sock, Buffer *m)
967 char *name, *info, **prompts;
968 u_int i, num, *echo_on;
971 debug3("%s", __func__);
972 sshpam_authok = NULL;
973 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
974 if (ret == 0 && num == 0)
975 sshpam_authok = sshpam_ctxt;
976 if (num > 1 || name == NULL || info == NULL)
979 buffer_put_int(m, ret);
980 buffer_put_cstring(m, name);
982 buffer_put_cstring(m, info);
984 buffer_put_int(m, num);
985 for (i = 0; i < num; ++i) {
986 buffer_put_cstring(m, prompts[i]);
988 buffer_put_int(m, echo_on[i]);
994 auth_method = "keyboard-interactive/pam";
995 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
1000 mm_answer_pam_respond(int sock, Buffer *m)
1006 debug3("%s", __func__);
1007 sshpam_authok = NULL;
1008 num = buffer_get_int(m);
1010 resp = xcalloc(num, sizeof(char *));
1011 for (i = 0; i < num; ++i)
1012 resp[i] = buffer_get_string(m, NULL);
1013 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1014 for (i = 0; i < num; ++i)
1018 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1021 buffer_put_int(m, ret);
1022 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1023 auth_method = "keyboard-interactive/pam";
1025 sshpam_authok = sshpam_ctxt;
1030 mm_answer_pam_free_ctx(int sock, Buffer *m)
1033 debug3("%s", __func__);
1034 (sshpam_device.free_ctx)(sshpam_ctxt);
1036 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1037 auth_method = "keyboard-interactive/pam";
1038 return (sshpam_authok == sshpam_ctxt);
1043 mm_append_debug(Buffer *m)
1045 if (auth_debug_init && buffer_len(&auth_debug)) {
1046 debug3("%s: Appending debug messages for child", __func__);
1047 buffer_append(m, buffer_ptr(&auth_debug),
1048 buffer_len(&auth_debug));
1049 buffer_clear(&auth_debug);
1054 mm_answer_keyallowed(int sock, Buffer *m)
1057 char *cuser, *chost;
1060 enum mm_keytype type = 0;
1063 debug3("%s entering", __func__);
1065 type = buffer_get_int(m);
1066 cuser = buffer_get_string(m, NULL);
1067 chost = buffer_get_string(m, NULL);
1068 blob = buffer_get_string(m, &bloblen);
1070 key = key_from_blob(blob, bloblen);
1072 if ((compat20 && type == MM_RSAHOSTKEY) ||
1073 (!compat20 && type != MM_RSAHOSTKEY))
1074 fatal("%s: key type and protocol mismatch", __func__);
1076 debug3("%s: key_from_blob: %p", __func__, key);
1078 if (key != NULL && authctxt->valid) {
1081 allowed = options.pubkey_authentication &&
1082 user_key_allowed(authctxt->pw, key);
1083 auth_method = "publickey";
1084 if (options.pubkey_authentication && allowed != 1)
1085 auth_clear_options();
1088 allowed = options.hostbased_authentication &&
1089 hostbased_key_allowed(authctxt->pw,
1091 auth_method = "hostbased";
1094 key->type = KEY_RSA1; /* XXX */
1095 allowed = options.rhosts_rsa_authentication &&
1096 auth_rhosts_rsa_key_allowed(authctxt->pw,
1098 if (options.rhosts_rsa_authentication && allowed != 1)
1099 auth_clear_options();
1100 auth_method = "rsa";
1103 fatal("%s: unknown key type %d", __func__, type);
1110 /* clear temporarily storage (used by verify) */
1111 monitor_reset_key_state();
1114 /* Save temporarily for comparison in verify */
1116 key_bloblen = bloblen;
1117 key_blobtype = type;
1118 hostbased_cuser = cuser;
1119 hostbased_chost = chost;
1121 /* Log failed attempt */
1122 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1128 debug3("%s: key %p is %s",
1129 __func__, key, allowed ? "allowed" : "not allowed");
1132 buffer_put_int(m, allowed);
1133 buffer_put_int(m, forced_command != NULL);
1137 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1139 if (type == MM_RSAHOSTKEY)
1140 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1146 monitor_valid_userblob(u_char *data, u_int datalen)
1154 buffer_append(&b, data, datalen);
1156 if (datafellows & SSH_OLD_SESSIONID) {
1158 len = buffer_len(&b);
1159 if ((session_id2 == NULL) ||
1160 (len < session_id2_len) ||
1161 (memcmp(p, session_id2, session_id2_len) != 0))
1163 buffer_consume(&b, session_id2_len);
1165 p = buffer_get_string(&b, &len);
1166 if ((session_id2 == NULL) ||
1167 (len != session_id2_len) ||
1168 (memcmp(p, session_id2, session_id2_len) != 0))
1172 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1174 p = buffer_get_string(&b, NULL);
1175 if (strcmp(authctxt->user, p) != 0) {
1176 logit("wrong user name passed to monitor: expected %s != %.100s",
1181 buffer_skip_string(&b);
1182 if (datafellows & SSH_BUG_PKAUTH) {
1183 if (!buffer_get_char(&b))
1186 p = buffer_get_string(&b, NULL);
1187 if (strcmp("publickey", p) != 0)
1190 if (!buffer_get_char(&b))
1192 buffer_skip_string(&b);
1194 buffer_skip_string(&b);
1195 if (buffer_len(&b) != 0)
1202 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1211 buffer_append(&b, data, datalen);
1213 p = buffer_get_string(&b, &len);
1214 if ((session_id2 == NULL) ||
1215 (len != session_id2_len) ||
1216 (memcmp(p, session_id2, session_id2_len) != 0))
1220 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1222 p = buffer_get_string(&b, NULL);
1223 if (strcmp(authctxt->user, p) != 0) {
1224 logit("wrong user name passed to monitor: expected %s != %.100s",
1229 buffer_skip_string(&b); /* service */
1230 p = buffer_get_string(&b, NULL);
1231 if (strcmp(p, "hostbased") != 0)
1234 buffer_skip_string(&b); /* pkalg */
1235 buffer_skip_string(&b); /* pkblob */
1237 /* verify client host, strip trailing dot if necessary */
1238 p = buffer_get_string(&b, NULL);
1239 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1241 if (strcmp(p, chost) != 0)
1245 /* verify client user */
1246 p = buffer_get_string(&b, NULL);
1247 if (strcmp(p, cuser) != 0)
1251 if (buffer_len(&b) != 0)
1258 mm_answer_keyverify(int sock, Buffer *m)
1261 u_char *signature, *data, *blob;
1262 u_int signaturelen, datalen, bloblen;
1266 blob = buffer_get_string(m, &bloblen);
1267 signature = buffer_get_string(m, &signaturelen);
1268 data = buffer_get_string(m, &datalen);
1270 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1271 !monitor_allowed_key(blob, bloblen))
1272 fatal("%s: bad key, not previously allowed", __func__);
1274 key = key_from_blob(blob, bloblen);
1276 fatal("%s: bad public key blob", __func__);
1278 switch (key_blobtype) {
1280 valid_data = monitor_valid_userblob(data, datalen);
1283 valid_data = monitor_valid_hostbasedblob(data, datalen,
1284 hostbased_cuser, hostbased_chost);
1291 fatal("%s: bad signature data blob", __func__);
1293 verified = key_verify(key, signature, signaturelen, data, datalen);
1294 debug3("%s: key %p signature %s",
1295 __func__, key, (verified == 1) ? "verified" : "unverified");
1302 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1304 monitor_reset_key_state();
1307 buffer_put_int(m, verified);
1308 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1310 return (verified == 1);
1314 mm_record_login(Session *s, struct passwd *pw)
1317 struct sockaddr_storage from;
1320 * Get IP address of client. If the connection is not a socket, let
1321 * the address be 0.0.0.0.
1323 memset(&from, 0, sizeof(from));
1324 fromlen = sizeof(from);
1325 if (packet_connection_is_on_socket()) {
1326 if (getpeername(packet_get_connection_in(),
1327 (struct sockaddr *)&from, &fromlen) < 0) {
1328 debug("getpeername: %.100s", strerror(errno));
1332 /* Record that there was a login on that tty from the remote host. */
1333 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1334 get_remote_name_or_ip(utmp_len, options.use_dns),
1335 (struct sockaddr *)&from, fromlen);
1339 mm_session_close(Session *s)
1341 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1342 if (s->ttyfd != -1) {
1343 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1344 session_pty_cleanup2(s);
1346 session_unused(s->self);
1350 mm_answer_pty(int sock, Buffer *m)
1352 extern struct monitor *pmonitor;
1356 debug3("%s entering", __func__);
1362 s->authctxt = authctxt;
1363 s->pw = authctxt->pw;
1364 s->pid = pmonitor->m_pid;
1365 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1368 pty_setowner(authctxt->pw, s->tty);
1370 buffer_put_int(m, 1);
1371 buffer_put_cstring(m, s->tty);
1373 /* We need to trick ttyslot */
1374 if (dup2(s->ttyfd, 0) == -1)
1375 fatal("%s: dup2", __func__);
1377 mm_record_login(s, authctxt->pw);
1379 /* Now we can close the file descriptor again */
1382 /* send messages generated by record_login */
1383 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1384 buffer_clear(&loginmsg);
1386 mm_request_send(sock, MONITOR_ANS_PTY, m);
1388 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1389 mm_send_fd(sock, s->ttyfd) == -1)
1390 fatal("%s: send fds failed", __func__);
1392 /* make sure nothing uses fd 0 */
1393 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1394 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1396 error("%s: fd0 %d != 0", __func__, fd0);
1398 /* slave is not needed */
1400 s->ttyfd = s->ptyfd;
1401 /* no need to dup() because nobody closes ptyfd */
1402 s->ptymaster = s->ptyfd;
1404 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1410 mm_session_close(s);
1411 buffer_put_int(m, 0);
1412 mm_request_send(sock, MONITOR_ANS_PTY, m);
1417 mm_answer_pty_cleanup(int sock, Buffer *m)
1422 debug3("%s entering", __func__);
1424 tty = buffer_get_string(m, NULL);
1425 if ((s = session_by_tty(tty)) != NULL)
1426 mm_session_close(s);
1433 mm_answer_sesskey(int sock, Buffer *m)
1438 /* Turn off permissions */
1439 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1441 if ((p = BN_new()) == NULL)
1442 fatal("%s: BN_new", __func__);
1444 buffer_get_bignum2(m, p);
1446 rsafail = ssh1_session_key(p);
1449 buffer_put_int(m, rsafail);
1450 buffer_put_bignum2(m, p);
1454 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1456 /* Turn on permissions for sessid passing */
1457 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1463 mm_answer_sessid(int sock, Buffer *m)
1467 debug3("%s entering", __func__);
1469 if (buffer_len(m) != 16)
1470 fatal("%s: bad ssh1 session id", __func__);
1471 for (i = 0; i < 16; i++)
1472 session_id[i] = buffer_get_char(m);
1474 /* Turn on permissions for getpwnam */
1475 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1481 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1485 u_char *blob = NULL;
1489 debug3("%s entering", __func__);
1491 auth_method = "rsa";
1492 if (options.rsa_authentication && authctxt->valid) {
1493 if ((client_n = BN_new()) == NULL)
1494 fatal("%s: BN_new", __func__);
1495 buffer_get_bignum2(m, client_n);
1496 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1497 BN_clear_free(client_n);
1500 buffer_put_int(m, allowed);
1501 buffer_put_int(m, forced_command != NULL);
1503 /* clear temporarily storage (used by generate challenge) */
1504 monitor_reset_key_state();
1506 if (allowed && key != NULL) {
1507 key->type = KEY_RSA; /* cheat for key_to_blob */
1508 if (key_to_blob(key, &blob, &blen) == 0)
1509 fatal("%s: key_to_blob failed", __func__);
1510 buffer_put_string(m, blob, blen);
1512 /* Save temporarily for comparison in verify */
1515 key_blobtype = MM_RSAUSERKEY;
1522 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1524 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1525 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1530 mm_answer_rsa_challenge(int sock, Buffer *m)
1536 debug3("%s entering", __func__);
1538 if (!authctxt->valid)
1539 fatal("%s: authctxt not valid", __func__);
1540 blob = buffer_get_string(m, &blen);
1541 if (!monitor_allowed_key(blob, blen))
1542 fatal("%s: bad key, not previously allowed", __func__);
1543 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1544 fatal("%s: key type mismatch", __func__);
1545 if ((key = key_from_blob(blob, blen)) == NULL)
1546 fatal("%s: received bad key", __func__);
1547 if (key->type != KEY_RSA)
1548 fatal("%s: received bad key type %d", __func__, key->type);
1549 key->type = KEY_RSA1;
1551 BN_clear_free(ssh1_challenge);
1552 ssh1_challenge = auth_rsa_generate_challenge(key);
1555 buffer_put_bignum2(m, ssh1_challenge);
1557 debug3("%s sending reply", __func__);
1558 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1560 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1568 mm_answer_rsa_response(int sock, Buffer *m)
1571 u_char *blob, *response;
1575 debug3("%s entering", __func__);
1577 if (!authctxt->valid)
1578 fatal("%s: authctxt not valid", __func__);
1579 if (ssh1_challenge == NULL)
1580 fatal("%s: no ssh1_challenge", __func__);
1582 blob = buffer_get_string(m, &blen);
1583 if (!monitor_allowed_key(blob, blen))
1584 fatal("%s: bad key, not previously allowed", __func__);
1585 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1586 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1587 if ((key = key_from_blob(blob, blen)) == NULL)
1588 fatal("%s: received bad key", __func__);
1589 response = buffer_get_string(m, &len);
1591 fatal("%s: received bad response to challenge", __func__);
1592 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1598 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1601 BN_clear_free(ssh1_challenge);
1602 ssh1_challenge = NULL;
1603 monitor_reset_key_state();
1606 buffer_put_int(m, success);
1607 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1613 mm_answer_term(int sock, Buffer *req)
1615 extern struct monitor *pmonitor;
1618 debug3("%s: tearing down sessions", __func__);
1620 /* The child is terminating */
1621 session_destroy_all(&mm_session_close);
1624 if (options.use_pam)
1628 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1632 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1634 /* Terminate process */
1638 #ifdef SSH_AUDIT_EVENTS
1639 /* Report that an audit event occurred */
1641 mm_answer_audit_event(int socket, Buffer *m)
1643 ssh_audit_event_t event;
1645 debug3("%s entering", __func__);
1647 event = buffer_get_int(m);
1649 case SSH_AUTH_FAIL_PUBKEY:
1650 case SSH_AUTH_FAIL_HOSTBASED:
1651 case SSH_AUTH_FAIL_GSSAPI:
1652 case SSH_LOGIN_EXCEED_MAXTRIES:
1653 case SSH_LOGIN_ROOT_DENIED:
1654 case SSH_CONNECTION_CLOSE:
1655 case SSH_INVALID_USER:
1659 fatal("Audit event type %d not permitted", event);
1666 mm_answer_audit_command(int socket, Buffer *m)
1671 debug3("%s entering", __func__);
1672 cmd = buffer_get_string(m, &len);
1673 /* sanity check command, if so how? */
1674 audit_run_command(cmd);
1678 #endif /* SSH_AUDIT_EVENTS */
1681 monitor_apply_keystate(struct monitor *pmonitor)
1684 set_newkeys(MODE_IN);
1685 set_newkeys(MODE_OUT);
1687 packet_set_protocol_flags(child_state.ssh1protoflags);
1688 packet_set_encryption_key(child_state.ssh1key,
1689 child_state.ssh1keylen, child_state.ssh1cipher);
1690 xfree(child_state.ssh1key);
1693 /* for rc4 and other stateful ciphers */
1694 packet_set_keycontext(MODE_OUT, child_state.keyout);
1695 xfree(child_state.keyout);
1696 packet_set_keycontext(MODE_IN, child_state.keyin);
1697 xfree(child_state.keyin);
1700 packet_set_iv(MODE_OUT, child_state.ivout);
1701 xfree(child_state.ivout);
1702 packet_set_iv(MODE_IN, child_state.ivin);
1703 xfree(child_state.ivin);
1706 memcpy(&incoming_stream, &child_state.incoming,
1707 sizeof(incoming_stream));
1708 memcpy(&outgoing_stream, &child_state.outgoing,
1709 sizeof(outgoing_stream));
1711 /* Update with new address */
1712 if (options.compression)
1713 mm_init_compression(pmonitor->m_zlib);
1715 /* Network I/O buffers */
1716 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1717 buffer_clear(&input);
1718 buffer_append(&input, child_state.input, child_state.ilen);
1719 memset(child_state.input, 0, child_state.ilen);
1720 xfree(child_state.input);
1722 buffer_clear(&output);
1723 buffer_append(&output, child_state.output, child_state.olen);
1724 memset(child_state.output, 0, child_state.olen);
1725 xfree(child_state.output);
1729 mm_get_kex(Buffer *m)
1735 kex = xcalloc(1, sizeof(*kex));
1736 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1737 if ((session_id2 == NULL) ||
1738 (kex->session_id_len != session_id2_len) ||
1739 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1740 fatal("mm_get_get: internal error: bad session id");
1741 kex->we_need = buffer_get_int(m);
1742 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1743 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1744 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1745 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1747 if (options.gss_keyex) {
1748 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
1749 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
1750 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
1754 kex->hostkey_type = buffer_get_int(m);
1755 kex->kex_type = buffer_get_int(m);
1756 blob = buffer_get_string(m, &bloblen);
1757 buffer_init(&kex->my);
1758 buffer_append(&kex->my, blob, bloblen);
1760 blob = buffer_get_string(m, &bloblen);
1761 buffer_init(&kex->peer);
1762 buffer_append(&kex->peer, blob, bloblen);
1765 kex->flags = buffer_get_int(m);
1766 kex->client_version_string = buffer_get_string(m, NULL);
1767 kex->server_version_string = buffer_get_string(m, NULL);
1768 kex->load_host_key=&get_hostkey_by_type;
1769 kex->host_key_index=&get_hostkey_index;
1774 /* This function requries careful sanity checking */
1777 mm_get_keystate(struct monitor *pmonitor)
1781 u_int bloblen, plen;
1782 u_int32_t seqnr, packets;
1783 u_int64_t blocks, bytes;
1785 debug3("%s: Waiting for new keys", __func__);
1788 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1790 child_state.ssh1protoflags = buffer_get_int(&m);
1791 child_state.ssh1cipher = buffer_get_int(&m);
1792 child_state.ssh1key = buffer_get_string(&m,
1793 &child_state.ssh1keylen);
1794 child_state.ivout = buffer_get_string(&m,
1795 &child_state.ivoutlen);
1796 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1799 /* Get the Kex for rekeying */
1800 *pmonitor->m_pkex = mm_get_kex(&m);
1803 blob = buffer_get_string(&m, &bloblen);
1804 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1807 debug3("%s: Waiting for second key", __func__);
1808 blob = buffer_get_string(&m, &bloblen);
1809 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1812 /* Now get sequence numbers for the packets */
1813 seqnr = buffer_get_int(&m);
1814 blocks = buffer_get_int64(&m);
1815 packets = buffer_get_int(&m);
1816 bytes = buffer_get_int64(&m);
1817 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
1818 seqnr = buffer_get_int(&m);
1819 blocks = buffer_get_int64(&m);
1820 packets = buffer_get_int(&m);
1821 bytes = buffer_get_int64(&m);
1822 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
1825 /* Get the key context */
1826 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1827 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1829 debug3("%s: Getting compression state", __func__);
1830 /* Get compression state */
1831 p = buffer_get_string(&m, &plen);
1832 if (plen != sizeof(child_state.outgoing))
1833 fatal("%s: bad request size", __func__);
1834 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1837 p = buffer_get_string(&m, &plen);
1838 if (plen != sizeof(child_state.incoming))
1839 fatal("%s: bad request size", __func__);
1840 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1843 /* Network I/O buffers */
1844 debug3("%s: Getting Network I/O buffers", __func__);
1845 child_state.input = buffer_get_string(&m, &child_state.ilen);
1846 child_state.output = buffer_get_string(&m, &child_state.olen);
1852 /* Allocation functions for zlib */
1854 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1856 size_t len = (size_t) size * ncount;
1859 if (len == 0 || ncount > SIZE_T_MAX / size)
1860 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1862 address = mm_malloc(mm, len);
1868 mm_zfree(struct mm_master *mm, void *address)
1870 mm_free(mm, address);
1874 mm_init_compression(struct mm_master *mm)
1876 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1877 outgoing_stream.zfree = (free_func)mm_zfree;
1878 outgoing_stream.opaque = mm;
1880 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1881 incoming_stream.zfree = (free_func)mm_zfree;
1882 incoming_stream.opaque = mm;
1887 #define FD_CLOSEONEXEC(x) do { \
1888 if (fcntl(x, F_SETFD, 1) == -1) \
1889 fatal("fcntl(%d, F_SETFD)", x); \
1893 monitor_socketpair(int *pair)
1895 #ifdef HAVE_SOCKETPAIR
1896 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1897 fatal("%s: socketpair", __func__);
1899 fatal("%s: UsePrivilegeSeparation=yes not supported",
1902 FD_CLOSEONEXEC(pair[0]);
1903 FD_CLOSEONEXEC(pair[1]);
1906 #define MM_MEMSIZE 65536
1911 struct monitor *mon;
1914 mon = xcalloc(1, sizeof(*mon));
1916 monitor_socketpair(pair);
1918 mon->m_recvfd = pair[0];
1919 mon->m_sendfd = pair[1];
1921 /* Used to share zlib space across processes */
1922 if (options.compression) {
1923 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1924 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1926 /* Compression needs to share state across borders */
1927 mm_init_compression(mon->m_zlib);
1934 monitor_reinit(struct monitor *mon)
1938 monitor_socketpair(pair);
1940 mon->m_recvfd = pair[0];
1941 mon->m_sendfd = pair[1];
1946 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1952 if (!options.gss_authentication && !options.gss_keyex)
1953 fatal("In GSSAPI monitor when GSSAPI is disabled");
1955 goid.elements = buffer_get_string(m, &len);
1958 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1960 xfree(goid.elements);
1963 buffer_put_int(m, major);
1965 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1967 /* Now we have a context, enable the step */
1968 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1974 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1977 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1978 OM_uint32 major, minor;
1979 OM_uint32 flags = 0; /* GSI needs this */
1982 if (!options.gss_authentication && !options.gss_keyex)
1983 fatal("In GSSAPI monitor when GSSAPI is disabled");
1985 in.value = buffer_get_string(m, &len);
1987 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1991 buffer_put_int(m, major);
1992 buffer_put_string(m, out.value, out.length);
1993 buffer_put_int(m, flags);
1994 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1996 gss_release_buffer(&minor, &out);
1998 if (major == GSS_S_COMPLETE) {
1999 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2000 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2001 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
2002 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
2003 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
2009 mm_answer_gss_checkmic(int sock, Buffer *m)
2011 gss_buffer_desc gssbuf, mic;
2015 if (!options.gss_authentication && !options.gss_keyex)
2016 fatal("In GSSAPI monitor when GSSAPI is disabled");
2018 gssbuf.value = buffer_get_string(m, &len);
2019 gssbuf.length = len;
2020 mic.value = buffer_get_string(m, &len);
2023 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
2025 xfree(gssbuf.value);
2029 buffer_put_int(m, ret);
2031 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
2033 if (!GSS_ERROR(ret))
2034 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2040 mm_answer_gss_userok(int sock, Buffer *m)
2044 if (!options.gss_authentication && !options.gss_keyex)
2045 fatal("In GSSAPI monitor when GSSAPI is disabled");
2047 authenticated = authctxt->valid &&
2048 ssh_gssapi_userok(authctxt->user, authctxt->pw);
2051 buffer_put_int(m, authenticated);
2053 debug3("%s: sending result %d", __func__, authenticated);
2054 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
2056 auth_method = "gssapi-with-mic";
2058 /* Monitor loop will terminate if authenticated */
2059 return (authenticated);
2063 mm_answer_gss_error(int socket, Buffer *m) {
2064 OM_uint32 major,minor;
2067 msg=ssh_gssapi_last_error(gsscontext,&major,&minor);
2069 buffer_put_int(m,major);
2070 buffer_put_int(m,minor);
2071 buffer_put_cstring(m,msg);
2073 mm_request_send(socket,MONITOR_ANS_GSSERR,m);
2081 mm_answer_gss_indicate_mechs(int socket, Buffer *m) {
2082 OM_uint32 major,minor;
2083 gss_OID_set mech_set;
2086 major=gss_indicate_mechs(&minor, &mech_set);
2089 buffer_put_int(m, major);
2090 buffer_put_int(m, mech_set->count);
2091 for (i=0; i < mech_set->count; i++) {
2092 buffer_put_string(m, mech_set->elements[i].elements,
2093 mech_set->elements[i].length);
2096 #if !defined(MECHGLUE) /* mechglue memory management bug ??? */
2097 gss_release_oid_set(&minor,&mech_set);
2100 mm_request_send(socket,MONITOR_ANS_GSSMECHS,m);
2106 mm_answer_gss_localname(int socket, Buffer *m) {
2109 ssh_gssapi_localname(&name);
2113 buffer_put_cstring(m, name);
2114 debug3("%s: sending result %s", __func__, name);
2117 buffer_put_cstring(m, "");
2118 debug3("%s: sending result \"\"", __func__);
2121 mm_request_send(socket, MONITOR_ANS_GSSLOCALNAME, m);
2127 mm_answer_gss_sign(int socket, Buffer *m)
2129 gss_buffer_desc data;
2130 gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
2131 OM_uint32 major, minor;
2134 if (!options.gss_authentication && !options.gss_keyex)
2135 fatal("In GSSAPI monitor when GSSAPI is disabled");
2137 data.value = buffer_get_string(m, &len);
2139 if (data.length != 20)
2140 fatal("%s: data length incorrect: %d", __func__,
2143 /* Save the session ID on the first time around */
2144 if (session_id2_len == 0) {
2145 session_id2_len = data.length;
2146 session_id2 = xmalloc(session_id2_len);
2147 memcpy(session_id2, data.value, session_id2_len);
2149 major = ssh_gssapi_sign(gsscontext, &data, &hash);
2154 buffer_put_int(m, major);
2155 buffer_put_string(m, hash.value, hash.length);
2157 mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
2159 gss_release_buffer(&minor, &hash);
2161 /* Turn on getpwnam permissions */
2162 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
2164 /* And credential updating, for when rekeying */
2165 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUPCREDS, 1);
2171 mm_answer_gss_updatecreds(int socket, Buffer *m) {
2172 ssh_gssapi_ccache store;
2175 store.filename = buffer_get_string(m, NULL);
2176 store.envvar = buffer_get_string(m, NULL);
2177 store.envval = buffer_get_string(m, NULL);
2179 ok = ssh_gssapi_update_creds(&store);
2181 xfree(store.filename);
2182 xfree(store.envvar);
2183 xfree(store.envval);
2186 buffer_put_int(m, ok);
2188 mm_request_send(socket, MONITOR_ANS_GSSUPCREDS, m);
2197 mm_answer_jpake_step1(int sock, Buffer *m)
2199 struct jpake_ctx *pctx;
2200 u_char *x3_proof, *x4_proof;
2201 u_int x3_proof_len, x4_proof_len;
2203 if (!options.zero_knowledge_password_authentication)
2204 fatal("zero_knowledge_password_authentication disabled");
2206 if (authctxt->jpake_ctx != NULL)
2207 fatal("%s: authctxt->jpake_ctx already set (%p)",
2208 __func__, authctxt->jpake_ctx);
2209 authctxt->jpake_ctx = pctx = jpake_new();
2211 jpake_step1(pctx->grp,
2212 &pctx->server_id, &pctx->server_id_len,
2213 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2214 &x3_proof, &x3_proof_len,
2215 &x4_proof, &x4_proof_len);
2217 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2221 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2222 buffer_put_bignum2(m, pctx->g_x3);
2223 buffer_put_bignum2(m, pctx->g_x4);
2224 buffer_put_string(m, x3_proof, x3_proof_len);
2225 buffer_put_string(m, x4_proof, x4_proof_len);
2227 debug3("%s: sending step1", __func__);
2228 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2230 bzero(x3_proof, x3_proof_len);
2231 bzero(x4_proof, x4_proof_len);
2235 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2236 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2242 mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2244 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2245 char *hash_scheme, *salt;
2248 fatal("%s: pctx == NULL", __func__);
2250 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2253 /* pctx->s is sensitive, not returned to slave */
2254 buffer_put_cstring(m, hash_scheme);
2255 buffer_put_cstring(m, salt);
2257 debug3("%s: sending pwdata", __func__);
2258 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2260 bzero(hash_scheme, strlen(hash_scheme));
2261 bzero(salt, strlen(salt));
2265 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2271 mm_answer_jpake_step2(int sock, Buffer *m)
2273 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2274 u_char *x1_proof, *x2_proof, *x4_s_proof;
2275 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2278 fatal("%s: pctx == NULL", __func__);
2280 if ((pctx->g_x1 = BN_new()) == NULL ||
2281 (pctx->g_x2 = BN_new()) == NULL)
2282 fatal("%s: BN_new", __func__);
2283 buffer_get_bignum2(m, pctx->g_x1);
2284 buffer_get_bignum2(m, pctx->g_x2);
2285 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2286 x1_proof = buffer_get_string(m, &x1_proof_len);
2287 x2_proof = buffer_get_string(m, &x2_proof_len);
2289 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2290 pctx->g_x1, pctx->g_x2, pctx->x4,
2291 pctx->client_id, pctx->client_id_len,
2292 pctx->server_id, pctx->server_id_len,
2293 x1_proof, x1_proof_len,
2294 x2_proof, x2_proof_len,
2296 &x4_s_proof, &x4_s_proof_len);
2298 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2300 bzero(x1_proof, x1_proof_len);
2301 bzero(x2_proof, x2_proof_len);
2307 buffer_put_bignum2(m, pctx->b);
2308 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2310 debug3("%s: sending step2", __func__);
2311 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2313 bzero(x4_s_proof, x4_s_proof_len);
2316 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2322 mm_answer_jpake_key_confirm(int sock, Buffer *m)
2324 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2326 u_int x2_s_proof_len;
2329 fatal("%s: pctx == NULL", __func__);
2331 if ((pctx->a = BN_new()) == NULL)
2332 fatal("%s: BN_new", __func__);
2333 buffer_get_bignum2(m, pctx->a);
2334 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2336 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2337 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2338 pctx->server_id, pctx->server_id_len,
2339 pctx->client_id, pctx->client_id_len,
2340 session_id2, session_id2_len,
2341 x2_s_proof, x2_s_proof_len,
2343 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2345 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2347 bzero(x2_s_proof, x2_s_proof_len);
2350 /* pctx->k is sensitive, not sent */
2351 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2353 debug3("%s: sending confirmation hash", __func__);
2354 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2356 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2362 mm_answer_jpake_check_confirm(int sock, Buffer *m)
2364 int authenticated = 0;
2365 u_char *peer_confirm_hash;
2366 u_int peer_confirm_hash_len;
2367 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2370 fatal("%s: pctx == NULL", __func__);
2372 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2374 authenticated = jpake_check_confirm(pctx->k,
2375 pctx->client_id, pctx->client_id_len,
2376 session_id2, session_id2_len,
2377 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2379 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2381 bzero(peer_confirm_hash, peer_confirm_hash_len);
2382 xfree(peer_confirm_hash);
2385 buffer_put_int(m, authenticated);
2387 debug3("%s: sending result %d", __func__, authenticated);
2388 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2390 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2392 auth_method = "jpake-01@openssh.com";
2393 return authenticated;