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);
683 setproctitle("%s [priv]", pwent ? username : "unknown");
689 buffer_put_char(m, 0);
690 authctxt->pw = fakepw();
695 authctxt->pw = pwent;
698 buffer_put_char(m, 1);
699 buffer_put_string(m, pwent, sizeof(struct passwd));
700 buffer_put_cstring(m, pwent->pw_name);
701 buffer_put_cstring(m, "*");
702 buffer_put_cstring(m, pwent->pw_gecos);
703 #ifdef HAVE_PW_CLASS_IN_PASSWD
704 buffer_put_cstring(m, pwent->pw_class);
706 buffer_put_cstring(m, pwent->pw_dir);
707 buffer_put_cstring(m, pwent->pw_shell);
710 buffer_put_string(m, &options, sizeof(options));
711 if (options.banner != NULL)
712 buffer_put_cstring(m, options.banner);
713 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
714 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
716 /* For SSHv1 allow authentication now */
718 monitor_permit_authentications(1);
720 /* Allow service/style information on the auth context */
721 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
722 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
727 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
733 int mm_answer_auth2_read_banner(int sock, Buffer *m)
738 banner = auth2_read_banner();
739 buffer_put_cstring(m, banner != NULL ? banner : "");
740 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
749 mm_answer_authserv(int sock, Buffer *m)
751 monitor_permit_authentications(1);
753 authctxt->service = buffer_get_string(m, NULL);
754 authctxt->style = buffer_get_string(m, NULL);
755 debug3("%s: service=%s, style=%s",
756 __func__, authctxt->service, authctxt->style);
758 if (strlen(authctxt->style) == 0) {
759 xfree(authctxt->style);
760 authctxt->style = NULL;
767 mm_answer_authpassword(int sock, Buffer *m)
769 static int call_count;
774 passwd = buffer_get_string(m, &plen);
775 /* Only authenticate if the context is valid */
776 authenticated = options.password_authentication &&
777 auth_password(authctxt, passwd);
778 memset(passwd, 0, strlen(passwd));
782 buffer_put_int(m, authenticated);
784 debug3("%s: sending result %d", __func__, authenticated);
785 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
788 if (plen == 0 && call_count == 1)
789 auth_method = "none";
791 auth_method = "password";
793 /* Causes monitor loop to terminate if authenticated */
794 return (authenticated);
799 mm_answer_bsdauthquery(int sock, Buffer *m)
801 char *name, *infotxt;
807 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
808 &prompts, &echo_on) < 0 ? 0 : 1;
811 buffer_put_int(m, success);
813 buffer_put_cstring(m, prompts[0]);
815 debug3("%s: sending challenge success: %u", __func__, success);
816 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
829 mm_answer_bsdauthrespond(int sock, Buffer *m)
834 if (authctxt->as == 0)
835 fatal("%s: no bsd auth session", __func__);
837 response = buffer_get_string(m, NULL);
838 authok = options.challenge_response_authentication &&
839 auth_userresponse(authctxt->as, response, 0);
841 debug3("%s: <%s> = <%d>", __func__, response, authok);
845 buffer_put_int(m, authok);
847 debug3("%s: sending authenticated: %d", __func__, authok);
848 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
850 auth_method = "bsdauth";
852 return (authok != 0);
858 mm_answer_skeyquery(int sock, Buffer *m)
861 char challenge[1024];
864 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
865 sizeof(challenge)) < 0 ? 0 : 1;
868 buffer_put_int(m, success);
870 buffer_put_cstring(m, challenge);
872 debug3("%s: sending challenge success: %u", __func__, success);
873 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
879 mm_answer_skeyrespond(int sock, Buffer *m)
884 response = buffer_get_string(m, NULL);
886 authok = (options.challenge_response_authentication &&
888 skey_haskey(authctxt->pw->pw_name) == 0 &&
889 skey_passcheck(authctxt->pw->pw_name, response) != -1);
894 buffer_put_int(m, authok);
896 debug3("%s: sending authenticated: %d", __func__, authok);
897 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
899 auth_method = "skey";
901 return (authok != 0);
907 mm_answer_pam_start(int sock, Buffer *m)
909 if (!options.use_pam)
910 fatal("UsePAM not set, but ended up in %s anyway", __func__);
914 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
920 mm_answer_pam_account(int sock, Buffer *m)
924 if (!options.use_pam)
925 fatal("UsePAM not set, but ended up in %s anyway", __func__);
927 ret = do_pam_account();
929 buffer_put_int(m, ret);
930 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
932 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
937 static void *sshpam_ctxt, *sshpam_authok;
938 extern KbdintDevice sshpam_device;
941 mm_answer_pam_init_ctx(int sock, Buffer *m)
944 debug3("%s", __func__);
945 authctxt->user = buffer_get_string(m, NULL);
946 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
947 sshpam_authok = NULL;
949 if (sshpam_ctxt != NULL) {
950 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
951 buffer_put_int(m, 1);
953 buffer_put_int(m, 0);
955 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
960 mm_answer_pam_query(int sock, Buffer *m)
962 char *name, *info, **prompts;
963 u_int i, num, *echo_on;
966 debug3("%s", __func__);
967 sshpam_authok = NULL;
968 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
969 if (ret == 0 && num == 0)
970 sshpam_authok = sshpam_ctxt;
971 if (num > 1 || name == NULL || info == NULL)
974 buffer_put_int(m, ret);
975 buffer_put_cstring(m, name);
977 buffer_put_cstring(m, info);
979 buffer_put_int(m, num);
980 for (i = 0; i < num; ++i) {
981 buffer_put_cstring(m, prompts[i]);
983 buffer_put_int(m, echo_on[i]);
989 auth_method = "keyboard-interactive/pam";
990 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
995 mm_answer_pam_respond(int sock, Buffer *m)
1001 debug3("%s", __func__);
1002 sshpam_authok = NULL;
1003 num = buffer_get_int(m);
1005 resp = xcalloc(num, sizeof(char *));
1006 for (i = 0; i < num; ++i)
1007 resp[i] = buffer_get_string(m, NULL);
1008 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1009 for (i = 0; i < num; ++i)
1013 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1016 buffer_put_int(m, ret);
1017 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1018 auth_method = "keyboard-interactive/pam";
1020 sshpam_authok = sshpam_ctxt;
1025 mm_answer_pam_free_ctx(int sock, Buffer *m)
1028 debug3("%s", __func__);
1029 (sshpam_device.free_ctx)(sshpam_ctxt);
1031 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1032 auth_method = "keyboard-interactive/pam";
1033 return (sshpam_authok == sshpam_ctxt);
1038 mm_append_debug(Buffer *m)
1040 if (auth_debug_init && buffer_len(&auth_debug)) {
1041 debug3("%s: Appending debug messages for child", __func__);
1042 buffer_append(m, buffer_ptr(&auth_debug),
1043 buffer_len(&auth_debug));
1044 buffer_clear(&auth_debug);
1049 mm_answer_keyallowed(int sock, Buffer *m)
1052 char *cuser, *chost;
1055 enum mm_keytype type = 0;
1058 debug3("%s entering", __func__);
1060 type = buffer_get_int(m);
1061 cuser = buffer_get_string(m, NULL);
1062 chost = buffer_get_string(m, NULL);
1063 blob = buffer_get_string(m, &bloblen);
1065 key = key_from_blob(blob, bloblen);
1067 if ((compat20 && type == MM_RSAHOSTKEY) ||
1068 (!compat20 && type != MM_RSAHOSTKEY))
1069 fatal("%s: key type and protocol mismatch", __func__);
1071 debug3("%s: key_from_blob: %p", __func__, key);
1073 if (key != NULL && authctxt->valid) {
1076 allowed = options.pubkey_authentication &&
1077 user_key_allowed(authctxt->pw, key);
1078 auth_method = "publickey";
1079 if (options.pubkey_authentication && allowed != 1)
1080 auth_clear_options();
1083 allowed = options.hostbased_authentication &&
1084 hostbased_key_allowed(authctxt->pw,
1086 auth_method = "hostbased";
1089 key->type = KEY_RSA1; /* XXX */
1090 allowed = options.rhosts_rsa_authentication &&
1091 auth_rhosts_rsa_key_allowed(authctxt->pw,
1093 if (options.rhosts_rsa_authentication && allowed != 1)
1094 auth_clear_options();
1095 auth_method = "rsa";
1098 fatal("%s: unknown key type %d", __func__, type);
1105 /* clear temporarily storage (used by verify) */
1106 monitor_reset_key_state();
1109 /* Save temporarily for comparison in verify */
1111 key_bloblen = bloblen;
1112 key_blobtype = type;
1113 hostbased_cuser = cuser;
1114 hostbased_chost = chost;
1116 /* Log failed attempt */
1117 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1123 debug3("%s: key %p is %s",
1124 __func__, key, allowed ? "allowed" : "not allowed");
1127 buffer_put_int(m, allowed);
1128 buffer_put_int(m, forced_command != NULL);
1132 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1134 if (type == MM_RSAHOSTKEY)
1135 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1141 monitor_valid_userblob(u_char *data, u_int datalen)
1149 buffer_append(&b, data, datalen);
1151 if (datafellows & SSH_OLD_SESSIONID) {
1153 len = buffer_len(&b);
1154 if ((session_id2 == NULL) ||
1155 (len < session_id2_len) ||
1156 (memcmp(p, session_id2, session_id2_len) != 0))
1158 buffer_consume(&b, session_id2_len);
1160 p = buffer_get_string(&b, &len);
1161 if ((session_id2 == NULL) ||
1162 (len != session_id2_len) ||
1163 (memcmp(p, session_id2, session_id2_len) != 0))
1167 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1169 p = buffer_get_string(&b, NULL);
1170 if (strcmp(authctxt->user, p) != 0) {
1171 logit("wrong user name passed to monitor: expected %s != %.100s",
1176 buffer_skip_string(&b);
1177 if (datafellows & SSH_BUG_PKAUTH) {
1178 if (!buffer_get_char(&b))
1181 p = buffer_get_string(&b, NULL);
1182 if (strcmp("publickey", p) != 0)
1185 if (!buffer_get_char(&b))
1187 buffer_skip_string(&b);
1189 buffer_skip_string(&b);
1190 if (buffer_len(&b) != 0)
1197 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1206 buffer_append(&b, data, datalen);
1208 p = buffer_get_string(&b, &len);
1209 if ((session_id2 == NULL) ||
1210 (len != session_id2_len) ||
1211 (memcmp(p, session_id2, session_id2_len) != 0))
1215 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1217 p = buffer_get_string(&b, NULL);
1218 if (strcmp(authctxt->user, p) != 0) {
1219 logit("wrong user name passed to monitor: expected %s != %.100s",
1224 buffer_skip_string(&b); /* service */
1225 p = buffer_get_string(&b, NULL);
1226 if (strcmp(p, "hostbased") != 0)
1229 buffer_skip_string(&b); /* pkalg */
1230 buffer_skip_string(&b); /* pkblob */
1232 /* verify client host, strip trailing dot if necessary */
1233 p = buffer_get_string(&b, NULL);
1234 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1236 if (strcmp(p, chost) != 0)
1240 /* verify client user */
1241 p = buffer_get_string(&b, NULL);
1242 if (strcmp(p, cuser) != 0)
1246 if (buffer_len(&b) != 0)
1253 mm_answer_keyverify(int sock, Buffer *m)
1256 u_char *signature, *data, *blob;
1257 u_int signaturelen, datalen, bloblen;
1261 blob = buffer_get_string(m, &bloblen);
1262 signature = buffer_get_string(m, &signaturelen);
1263 data = buffer_get_string(m, &datalen);
1265 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1266 !monitor_allowed_key(blob, bloblen))
1267 fatal("%s: bad key, not previously allowed", __func__);
1269 key = key_from_blob(blob, bloblen);
1271 fatal("%s: bad public key blob", __func__);
1273 switch (key_blobtype) {
1275 valid_data = monitor_valid_userblob(data, datalen);
1278 valid_data = monitor_valid_hostbasedblob(data, datalen,
1279 hostbased_cuser, hostbased_chost);
1286 fatal("%s: bad signature data blob", __func__);
1288 verified = key_verify(key, signature, signaturelen, data, datalen);
1289 debug3("%s: key %p signature %s",
1290 __func__, key, (verified == 1) ? "verified" : "unverified");
1297 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1299 monitor_reset_key_state();
1302 buffer_put_int(m, verified);
1303 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1305 return (verified == 1);
1309 mm_record_login(Session *s, struct passwd *pw)
1312 struct sockaddr_storage from;
1315 * Get IP address of client. If the connection is not a socket, let
1316 * the address be 0.0.0.0.
1318 memset(&from, 0, sizeof(from));
1319 fromlen = sizeof(from);
1320 if (packet_connection_is_on_socket()) {
1321 if (getpeername(packet_get_connection_in(),
1322 (struct sockaddr *)&from, &fromlen) < 0) {
1323 debug("getpeername: %.100s", strerror(errno));
1327 /* Record that there was a login on that tty from the remote host. */
1328 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1329 get_remote_name_or_ip(utmp_len, options.use_dns),
1330 (struct sockaddr *)&from, fromlen);
1334 mm_session_close(Session *s)
1336 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1337 if (s->ttyfd != -1) {
1338 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1339 session_pty_cleanup2(s);
1341 session_unused(s->self);
1345 mm_answer_pty(int sock, Buffer *m)
1347 extern struct monitor *pmonitor;
1351 debug3("%s entering", __func__);
1357 s->authctxt = authctxt;
1358 s->pw = authctxt->pw;
1359 s->pid = pmonitor->m_pid;
1360 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1363 pty_setowner(authctxt->pw, s->tty);
1365 buffer_put_int(m, 1);
1366 buffer_put_cstring(m, s->tty);
1368 /* We need to trick ttyslot */
1369 if (dup2(s->ttyfd, 0) == -1)
1370 fatal("%s: dup2", __func__);
1372 mm_record_login(s, authctxt->pw);
1374 /* Now we can close the file descriptor again */
1377 /* send messages generated by record_login */
1378 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1379 buffer_clear(&loginmsg);
1381 mm_request_send(sock, MONITOR_ANS_PTY, m);
1383 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1384 mm_send_fd(sock, s->ttyfd) == -1)
1385 fatal("%s: send fds failed", __func__);
1387 /* make sure nothing uses fd 0 */
1388 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1389 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1391 error("%s: fd0 %d != 0", __func__, fd0);
1393 /* slave is not needed */
1395 s->ttyfd = s->ptyfd;
1396 /* no need to dup() because nobody closes ptyfd */
1397 s->ptymaster = s->ptyfd;
1399 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1405 mm_session_close(s);
1406 buffer_put_int(m, 0);
1407 mm_request_send(sock, MONITOR_ANS_PTY, m);
1412 mm_answer_pty_cleanup(int sock, Buffer *m)
1417 debug3("%s entering", __func__);
1419 tty = buffer_get_string(m, NULL);
1420 if ((s = session_by_tty(tty)) != NULL)
1421 mm_session_close(s);
1428 mm_answer_sesskey(int sock, Buffer *m)
1433 /* Turn off permissions */
1434 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1436 if ((p = BN_new()) == NULL)
1437 fatal("%s: BN_new", __func__);
1439 buffer_get_bignum2(m, p);
1441 rsafail = ssh1_session_key(p);
1444 buffer_put_int(m, rsafail);
1445 buffer_put_bignum2(m, p);
1449 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1451 /* Turn on permissions for sessid passing */
1452 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1458 mm_answer_sessid(int sock, Buffer *m)
1462 debug3("%s entering", __func__);
1464 if (buffer_len(m) != 16)
1465 fatal("%s: bad ssh1 session id", __func__);
1466 for (i = 0; i < 16; i++)
1467 session_id[i] = buffer_get_char(m);
1469 /* Turn on permissions for getpwnam */
1470 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1476 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1480 u_char *blob = NULL;
1484 debug3("%s entering", __func__);
1486 auth_method = "rsa";
1487 if (options.rsa_authentication && authctxt->valid) {
1488 if ((client_n = BN_new()) == NULL)
1489 fatal("%s: BN_new", __func__);
1490 buffer_get_bignum2(m, client_n);
1491 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1492 BN_clear_free(client_n);
1495 buffer_put_int(m, allowed);
1496 buffer_put_int(m, forced_command != NULL);
1498 /* clear temporarily storage (used by generate challenge) */
1499 monitor_reset_key_state();
1501 if (allowed && key != NULL) {
1502 key->type = KEY_RSA; /* cheat for key_to_blob */
1503 if (key_to_blob(key, &blob, &blen) == 0)
1504 fatal("%s: key_to_blob failed", __func__);
1505 buffer_put_string(m, blob, blen);
1507 /* Save temporarily for comparison in verify */
1510 key_blobtype = MM_RSAUSERKEY;
1517 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1519 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1520 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1525 mm_answer_rsa_challenge(int sock, Buffer *m)
1531 debug3("%s entering", __func__);
1533 if (!authctxt->valid)
1534 fatal("%s: authctxt not valid", __func__);
1535 blob = buffer_get_string(m, &blen);
1536 if (!monitor_allowed_key(blob, blen))
1537 fatal("%s: bad key, not previously allowed", __func__);
1538 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1539 fatal("%s: key type mismatch", __func__);
1540 if ((key = key_from_blob(blob, blen)) == NULL)
1541 fatal("%s: received bad key", __func__);
1542 if (key->type != KEY_RSA)
1543 fatal("%s: received bad key type %d", __func__, key->type);
1544 key->type = KEY_RSA1;
1546 BN_clear_free(ssh1_challenge);
1547 ssh1_challenge = auth_rsa_generate_challenge(key);
1550 buffer_put_bignum2(m, ssh1_challenge);
1552 debug3("%s sending reply", __func__);
1553 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1555 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1563 mm_answer_rsa_response(int sock, Buffer *m)
1566 u_char *blob, *response;
1570 debug3("%s entering", __func__);
1572 if (!authctxt->valid)
1573 fatal("%s: authctxt not valid", __func__);
1574 if (ssh1_challenge == NULL)
1575 fatal("%s: no ssh1_challenge", __func__);
1577 blob = buffer_get_string(m, &blen);
1578 if (!monitor_allowed_key(blob, blen))
1579 fatal("%s: bad key, not previously allowed", __func__);
1580 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1581 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1582 if ((key = key_from_blob(blob, blen)) == NULL)
1583 fatal("%s: received bad key", __func__);
1584 response = buffer_get_string(m, &len);
1586 fatal("%s: received bad response to challenge", __func__);
1587 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1593 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1596 BN_clear_free(ssh1_challenge);
1597 ssh1_challenge = NULL;
1598 monitor_reset_key_state();
1601 buffer_put_int(m, success);
1602 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1608 mm_answer_term(int sock, Buffer *req)
1610 extern struct monitor *pmonitor;
1613 debug3("%s: tearing down sessions", __func__);
1615 /* The child is terminating */
1616 session_destroy_all(&mm_session_close);
1619 if (options.use_pam)
1623 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1627 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1629 /* Terminate process */
1633 #ifdef SSH_AUDIT_EVENTS
1634 /* Report that an audit event occurred */
1636 mm_answer_audit_event(int socket, Buffer *m)
1638 ssh_audit_event_t event;
1640 debug3("%s entering", __func__);
1642 event = buffer_get_int(m);
1644 case SSH_AUTH_FAIL_PUBKEY:
1645 case SSH_AUTH_FAIL_HOSTBASED:
1646 case SSH_AUTH_FAIL_GSSAPI:
1647 case SSH_LOGIN_EXCEED_MAXTRIES:
1648 case SSH_LOGIN_ROOT_DENIED:
1649 case SSH_CONNECTION_CLOSE:
1650 case SSH_INVALID_USER:
1654 fatal("Audit event type %d not permitted", event);
1661 mm_answer_audit_command(int socket, Buffer *m)
1666 debug3("%s entering", __func__);
1667 cmd = buffer_get_string(m, &len);
1668 /* sanity check command, if so how? */
1669 audit_run_command(cmd);
1673 #endif /* SSH_AUDIT_EVENTS */
1676 monitor_apply_keystate(struct monitor *pmonitor)
1679 set_newkeys(MODE_IN);
1680 set_newkeys(MODE_OUT);
1682 packet_set_protocol_flags(child_state.ssh1protoflags);
1683 packet_set_encryption_key(child_state.ssh1key,
1684 child_state.ssh1keylen, child_state.ssh1cipher);
1685 xfree(child_state.ssh1key);
1688 /* for rc4 and other stateful ciphers */
1689 packet_set_keycontext(MODE_OUT, child_state.keyout);
1690 xfree(child_state.keyout);
1691 packet_set_keycontext(MODE_IN, child_state.keyin);
1692 xfree(child_state.keyin);
1695 packet_set_iv(MODE_OUT, child_state.ivout);
1696 xfree(child_state.ivout);
1697 packet_set_iv(MODE_IN, child_state.ivin);
1698 xfree(child_state.ivin);
1701 memcpy(&incoming_stream, &child_state.incoming,
1702 sizeof(incoming_stream));
1703 memcpy(&outgoing_stream, &child_state.outgoing,
1704 sizeof(outgoing_stream));
1706 /* Update with new address */
1707 if (options.compression)
1708 mm_init_compression(pmonitor->m_zlib);
1710 /* Network I/O buffers */
1711 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1712 buffer_clear(&input);
1713 buffer_append(&input, child_state.input, child_state.ilen);
1714 memset(child_state.input, 0, child_state.ilen);
1715 xfree(child_state.input);
1717 buffer_clear(&output);
1718 buffer_append(&output, child_state.output, child_state.olen);
1719 memset(child_state.output, 0, child_state.olen);
1720 xfree(child_state.output);
1724 mm_get_kex(Buffer *m)
1730 kex = xcalloc(1, sizeof(*kex));
1731 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1732 if ((session_id2 == NULL) ||
1733 (kex->session_id_len != session_id2_len) ||
1734 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1735 fatal("mm_get_get: internal error: bad session id");
1736 kex->we_need = buffer_get_int(m);
1737 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1738 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1739 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1740 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1742 if (options.gss_keyex) {
1743 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
1744 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
1745 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
1749 kex->hostkey_type = buffer_get_int(m);
1750 kex->kex_type = buffer_get_int(m);
1751 blob = buffer_get_string(m, &bloblen);
1752 buffer_init(&kex->my);
1753 buffer_append(&kex->my, blob, bloblen);
1755 blob = buffer_get_string(m, &bloblen);
1756 buffer_init(&kex->peer);
1757 buffer_append(&kex->peer, blob, bloblen);
1760 kex->flags = buffer_get_int(m);
1761 kex->client_version_string = buffer_get_string(m, NULL);
1762 kex->server_version_string = buffer_get_string(m, NULL);
1763 kex->load_host_key=&get_hostkey_by_type;
1764 kex->host_key_index=&get_hostkey_index;
1769 /* This function requries careful sanity checking */
1772 mm_get_keystate(struct monitor *pmonitor)
1776 u_int bloblen, plen;
1777 u_int32_t seqnr, packets;
1778 u_int64_t blocks, bytes;
1780 debug3("%s: Waiting for new keys", __func__);
1783 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1785 child_state.ssh1protoflags = buffer_get_int(&m);
1786 child_state.ssh1cipher = buffer_get_int(&m);
1787 child_state.ssh1key = buffer_get_string(&m,
1788 &child_state.ssh1keylen);
1789 child_state.ivout = buffer_get_string(&m,
1790 &child_state.ivoutlen);
1791 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1794 /* Get the Kex for rekeying */
1795 *pmonitor->m_pkex = mm_get_kex(&m);
1798 blob = buffer_get_string(&m, &bloblen);
1799 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1802 debug3("%s: Waiting for second key", __func__);
1803 blob = buffer_get_string(&m, &bloblen);
1804 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1807 /* Now get sequence numbers for the packets */
1808 seqnr = buffer_get_int(&m);
1809 blocks = buffer_get_int64(&m);
1810 packets = buffer_get_int(&m);
1811 bytes = buffer_get_int64(&m);
1812 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
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_IN, seqnr, blocks, packets, bytes);
1820 /* Get the key context */
1821 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1822 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1824 debug3("%s: Getting compression state", __func__);
1825 /* Get compression state */
1826 p = buffer_get_string(&m, &plen);
1827 if (plen != sizeof(child_state.outgoing))
1828 fatal("%s: bad request size", __func__);
1829 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1832 p = buffer_get_string(&m, &plen);
1833 if (plen != sizeof(child_state.incoming))
1834 fatal("%s: bad request size", __func__);
1835 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1838 /* Network I/O buffers */
1839 debug3("%s: Getting Network I/O buffers", __func__);
1840 child_state.input = buffer_get_string(&m, &child_state.ilen);
1841 child_state.output = buffer_get_string(&m, &child_state.olen);
1847 /* Allocation functions for zlib */
1849 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1851 size_t len = (size_t) size * ncount;
1854 if (len == 0 || ncount > SIZE_T_MAX / size)
1855 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1857 address = mm_malloc(mm, len);
1863 mm_zfree(struct mm_master *mm, void *address)
1865 mm_free(mm, address);
1869 mm_init_compression(struct mm_master *mm)
1871 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1872 outgoing_stream.zfree = (free_func)mm_zfree;
1873 outgoing_stream.opaque = mm;
1875 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1876 incoming_stream.zfree = (free_func)mm_zfree;
1877 incoming_stream.opaque = mm;
1882 #define FD_CLOSEONEXEC(x) do { \
1883 if (fcntl(x, F_SETFD, 1) == -1) \
1884 fatal("fcntl(%d, F_SETFD)", x); \
1888 monitor_socketpair(int *pair)
1890 #ifdef HAVE_SOCKETPAIR
1891 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1892 fatal("%s: socketpair", __func__);
1894 fatal("%s: UsePrivilegeSeparation=yes not supported",
1897 FD_CLOSEONEXEC(pair[0]);
1898 FD_CLOSEONEXEC(pair[1]);
1901 #define MM_MEMSIZE 65536
1906 struct monitor *mon;
1909 mon = xcalloc(1, sizeof(*mon));
1911 monitor_socketpair(pair);
1913 mon->m_recvfd = pair[0];
1914 mon->m_sendfd = pair[1];
1916 /* Used to share zlib space across processes */
1917 if (options.compression) {
1918 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1919 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1921 /* Compression needs to share state across borders */
1922 mm_init_compression(mon->m_zlib);
1929 monitor_reinit(struct monitor *mon)
1933 monitor_socketpair(pair);
1935 mon->m_recvfd = pair[0];
1936 mon->m_sendfd = pair[1];
1941 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1947 if (!options.gss_authentication && !options.gss_keyex)
1948 fatal("In GSSAPI monitor when GSSAPI is disabled");
1950 goid.elements = buffer_get_string(m, &len);
1953 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1955 xfree(goid.elements);
1958 buffer_put_int(m, major);
1960 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1962 /* Now we have a context, enable the step */
1963 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1969 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1972 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1973 OM_uint32 major, minor;
1974 OM_uint32 flags = 0; /* GSI needs this */
1977 if (!options.gss_authentication && !options.gss_keyex)
1978 fatal("In GSSAPI monitor when GSSAPI is disabled");
1980 in.value = buffer_get_string(m, &len);
1982 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1986 buffer_put_int(m, major);
1987 buffer_put_string(m, out.value, out.length);
1988 buffer_put_int(m, flags);
1989 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1991 gss_release_buffer(&minor, &out);
1993 if (major == GSS_S_COMPLETE) {
1994 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1995 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1996 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
1997 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1998 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
2004 mm_answer_gss_checkmic(int sock, Buffer *m)
2006 gss_buffer_desc gssbuf, mic;
2010 if (!options.gss_authentication && !options.gss_keyex)
2011 fatal("In GSSAPI monitor when GSSAPI is disabled");
2013 gssbuf.value = buffer_get_string(m, &len);
2014 gssbuf.length = len;
2015 mic.value = buffer_get_string(m, &len);
2018 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
2020 xfree(gssbuf.value);
2024 buffer_put_int(m, ret);
2026 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
2028 if (!GSS_ERROR(ret))
2029 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2035 mm_answer_gss_userok(int sock, Buffer *m)
2039 if (!options.gss_authentication && !options.gss_keyex)
2040 fatal("In GSSAPI monitor when GSSAPI is disabled");
2042 authenticated = authctxt->valid &&
2043 ssh_gssapi_userok(authctxt->user, authctxt->pw);
2046 buffer_put_int(m, authenticated);
2048 debug3("%s: sending result %d", __func__, authenticated);
2049 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
2051 auth_method = "gssapi-with-mic";
2053 /* Monitor loop will terminate if authenticated */
2054 return (authenticated);
2058 mm_answer_gss_error(int socket, Buffer *m) {
2059 OM_uint32 major,minor;
2062 msg=ssh_gssapi_last_error(gsscontext,&major,&minor);
2064 buffer_put_int(m,major);
2065 buffer_put_int(m,minor);
2066 buffer_put_cstring(m,msg);
2068 mm_request_send(socket,MONITOR_ANS_GSSERR,m);
2076 mm_answer_gss_indicate_mechs(int socket, Buffer *m) {
2077 OM_uint32 major,minor;
2078 gss_OID_set mech_set;
2081 major=gss_indicate_mechs(&minor, &mech_set);
2084 buffer_put_int(m, major);
2085 buffer_put_int(m, mech_set->count);
2086 for (i=0; i < mech_set->count; i++) {
2087 buffer_put_string(m, mech_set->elements[i].elements,
2088 mech_set->elements[i].length);
2091 #if !defined(MECHGLUE) /* mechglue memory management bug ??? */
2092 gss_release_oid_set(&minor,&mech_set);
2095 mm_request_send(socket,MONITOR_ANS_GSSMECHS,m);
2101 mm_answer_gss_localname(int socket, Buffer *m) {
2104 ssh_gssapi_localname(&name);
2108 buffer_put_cstring(m, name);
2109 debug3("%s: sending result %s", __func__, name);
2112 buffer_put_cstring(m, "");
2113 debug3("%s: sending result \"\"", __func__);
2116 mm_request_send(socket, MONITOR_ANS_GSSLOCALNAME, m);
2122 mm_answer_gss_sign(int socket, Buffer *m)
2124 gss_buffer_desc data;
2125 gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
2126 OM_uint32 major, minor;
2129 if (!options.gss_authentication && !options.gss_keyex)
2130 fatal("In GSSAPI monitor when GSSAPI is disabled");
2132 data.value = buffer_get_string(m, &len);
2134 if (data.length != 20)
2135 fatal("%s: data length incorrect: %d", __func__,
2138 /* Save the session ID on the first time around */
2139 if (session_id2_len == 0) {
2140 session_id2_len = data.length;
2141 session_id2 = xmalloc(session_id2_len);
2142 memcpy(session_id2, data.value, session_id2_len);
2144 major = ssh_gssapi_sign(gsscontext, &data, &hash);
2149 buffer_put_int(m, major);
2150 buffer_put_string(m, hash.value, hash.length);
2152 mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
2154 gss_release_buffer(&minor, &hash);
2156 /* Turn on getpwnam permissions */
2157 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
2159 /* And credential updating, for when rekeying */
2160 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUPCREDS, 1);
2166 mm_answer_gss_updatecreds(int socket, Buffer *m) {
2167 ssh_gssapi_ccache store;
2170 store.filename = buffer_get_string(m, NULL);
2171 store.envvar = buffer_get_string(m, NULL);
2172 store.envval = buffer_get_string(m, NULL);
2174 ok = ssh_gssapi_update_creds(&store);
2176 xfree(store.filename);
2177 xfree(store.envvar);
2178 xfree(store.envval);
2181 buffer_put_int(m, ok);
2183 mm_request_send(socket, MONITOR_ANS_GSSUPCREDS, m);
2192 mm_answer_jpake_step1(int sock, Buffer *m)
2194 struct jpake_ctx *pctx;
2195 u_char *x3_proof, *x4_proof;
2196 u_int x3_proof_len, x4_proof_len;
2198 if (!options.zero_knowledge_password_authentication)
2199 fatal("zero_knowledge_password_authentication disabled");
2201 if (authctxt->jpake_ctx != NULL)
2202 fatal("%s: authctxt->jpake_ctx already set (%p)",
2203 __func__, authctxt->jpake_ctx);
2204 authctxt->jpake_ctx = pctx = jpake_new();
2206 jpake_step1(pctx->grp,
2207 &pctx->server_id, &pctx->server_id_len,
2208 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2209 &x3_proof, &x3_proof_len,
2210 &x4_proof, &x4_proof_len);
2212 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2216 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2217 buffer_put_bignum2(m, pctx->g_x3);
2218 buffer_put_bignum2(m, pctx->g_x4);
2219 buffer_put_string(m, x3_proof, x3_proof_len);
2220 buffer_put_string(m, x4_proof, x4_proof_len);
2222 debug3("%s: sending step1", __func__);
2223 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2225 bzero(x3_proof, x3_proof_len);
2226 bzero(x4_proof, x4_proof_len);
2230 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2231 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2237 mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2239 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2240 char *hash_scheme, *salt;
2243 fatal("%s: pctx == NULL", __func__);
2245 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2248 /* pctx->s is sensitive, not returned to slave */
2249 buffer_put_cstring(m, hash_scheme);
2250 buffer_put_cstring(m, salt);
2252 debug3("%s: sending pwdata", __func__);
2253 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2255 bzero(hash_scheme, strlen(hash_scheme));
2256 bzero(salt, strlen(salt));
2260 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2266 mm_answer_jpake_step2(int sock, Buffer *m)
2268 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2269 u_char *x1_proof, *x2_proof, *x4_s_proof;
2270 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2273 fatal("%s: pctx == NULL", __func__);
2275 if ((pctx->g_x1 = BN_new()) == NULL ||
2276 (pctx->g_x2 = BN_new()) == NULL)
2277 fatal("%s: BN_new", __func__);
2278 buffer_get_bignum2(m, pctx->g_x1);
2279 buffer_get_bignum2(m, pctx->g_x2);
2280 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2281 x1_proof = buffer_get_string(m, &x1_proof_len);
2282 x2_proof = buffer_get_string(m, &x2_proof_len);
2284 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2285 pctx->g_x1, pctx->g_x2, pctx->x4,
2286 pctx->client_id, pctx->client_id_len,
2287 pctx->server_id, pctx->server_id_len,
2288 x1_proof, x1_proof_len,
2289 x2_proof, x2_proof_len,
2291 &x4_s_proof, &x4_s_proof_len);
2293 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2295 bzero(x1_proof, x1_proof_len);
2296 bzero(x2_proof, x2_proof_len);
2302 buffer_put_bignum2(m, pctx->b);
2303 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2305 debug3("%s: sending step2", __func__);
2306 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2308 bzero(x4_s_proof, x4_s_proof_len);
2311 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2317 mm_answer_jpake_key_confirm(int sock, Buffer *m)
2319 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2321 u_int x2_s_proof_len;
2324 fatal("%s: pctx == NULL", __func__);
2326 if ((pctx->a = BN_new()) == NULL)
2327 fatal("%s: BN_new", __func__);
2328 buffer_get_bignum2(m, pctx->a);
2329 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2331 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2332 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2333 pctx->server_id, pctx->server_id_len,
2334 pctx->client_id, pctx->client_id_len,
2335 session_id2, session_id2_len,
2336 x2_s_proof, x2_s_proof_len,
2338 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2340 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2342 bzero(x2_s_proof, x2_s_proof_len);
2345 /* pctx->k is sensitive, not sent */
2346 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2348 debug3("%s: sending confirmation hash", __func__);
2349 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2351 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2357 mm_answer_jpake_check_confirm(int sock, Buffer *m)
2359 int authenticated = 0;
2360 u_char *peer_confirm_hash;
2361 u_int peer_confirm_hash_len;
2362 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2365 fatal("%s: pctx == NULL", __func__);
2367 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2369 authenticated = jpake_check_confirm(pctx->k,
2370 pctx->client_id, pctx->client_id_len,
2371 session_id2, session_id2_len,
2372 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2374 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2376 bzero(peer_confirm_hash, peer_confirm_hash_len);
2377 xfree(peer_confirm_hash);
2380 buffer_put_int(m, authenticated);
2382 debug3("%s: sending result %d", __func__, authenticated);
2383 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2385 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2387 auth_method = "jpake-01@openssh.com";
2388 return authenticated;