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 *);
179 #ifdef SSH_AUDIT_EVENTS
180 int mm_answer_audit_event(int, Buffer *);
181 int mm_answer_audit_command(int, Buffer *);
184 static Authctxt *authctxt;
185 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
187 /* local state for key verify */
188 static u_char *key_blob = NULL;
189 static u_int key_bloblen = 0;
190 static int key_blobtype = MM_NOKEY;
191 static char *hostbased_cuser = NULL;
192 static char *hostbased_chost = NULL;
193 static char *auth_method = "unknown";
194 static u_int session_id2_len = 0;
195 static u_char *session_id2 = NULL;
196 static pid_t monitor_child_pid;
199 enum monitor_reqtype type;
201 int (*f)(int, Buffer *);
204 #define MON_ISAUTH 0x0004 /* Required for Authentication */
205 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
206 #define MON_ONCE 0x0010 /* Disable after calling */
207 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
209 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
211 #define MON_PERMIT 0x1000 /* Request is permitted */
213 struct mon_table mon_dispatch_proto20[] = {
214 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
215 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
216 {MONITOR_REQ_PWNAM, MON_AUTH, mm_answer_pwnamallow},
217 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
218 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
219 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
221 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
222 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
223 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
224 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
225 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
226 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
228 #ifdef SSH_AUDIT_EVENTS
229 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
232 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
233 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
236 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
237 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
239 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
240 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
242 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
243 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
244 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
245 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
246 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
247 {MONITOR_REQ_GSSERR, MON_ISAUTH | MON_ONCE, mm_answer_gss_error},
248 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
249 {MONITOR_REQ_GSSLOCALNAME, MON_ISAUTH, mm_answer_gss_localname},
252 {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
253 {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
254 {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
255 {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
256 {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
261 struct mon_table mon_dispatch_postauth20[] = {
263 {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
264 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
265 {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
266 {MONITOR_REQ_GSSERR, 0, mm_answer_gss_error},
267 {MONITOR_REQ_GSSMECHS, 0, mm_answer_gss_indicate_mechs},
269 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
270 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
271 {MONITOR_REQ_PTY, 0, mm_answer_pty},
272 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
273 {MONITOR_REQ_TERM, 0, mm_answer_term},
274 #ifdef SSH_AUDIT_EVENTS
275 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
276 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
281 struct mon_table mon_dispatch_proto15[] = {
282 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
283 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
284 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
285 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
286 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
287 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
288 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
289 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
291 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
292 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
295 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
296 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
299 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
300 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
301 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
302 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
303 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
306 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
307 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
308 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
309 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
310 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
311 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
313 #ifdef SSH_AUDIT_EVENTS
314 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
319 struct mon_table mon_dispatch_postauth15[] = {
320 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
321 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
322 {MONITOR_REQ_TERM, 0, mm_answer_term},
323 #ifdef SSH_AUDIT_EVENTS
324 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
325 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
330 struct mon_table *mon_dispatch;
332 /* Specifies if a certain message is allowed at the moment */
335 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
337 while (ent->f != NULL) {
338 if (ent->type == type) {
339 ent->flags &= ~MON_PERMIT;
340 ent->flags |= permit ? MON_PERMIT : 0;
348 monitor_permit_authentications(int permit)
350 struct mon_table *ent = mon_dispatch;
352 while (ent->f != NULL) {
353 if (ent->flags & MON_AUTH) {
354 ent->flags &= ~MON_PERMIT;
355 ent->flags |= permit ? MON_PERMIT : 0;
362 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
364 struct mon_table *ent;
365 int authenticated = 0;
367 debug3("preauth child monitor started");
369 authctxt = _authctxt;
370 memset(authctxt, 0, sizeof(*authctxt));
372 authctxt->loginmsg = &loginmsg;
375 mon_dispatch = mon_dispatch_proto20;
377 /* Permit requests for moduli and signatures */
378 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
379 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
381 /* and for the GSSAPI key exchange */
382 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
383 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
384 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS, 1);
387 mon_dispatch = mon_dispatch_proto15;
389 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
392 /* The first few requests do not require asynchronous access */
393 while (!authenticated) {
394 auth_method = "unknown";
395 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
397 if (!(ent->flags & MON_AUTHDECIDE))
398 fatal("%s: unexpected authentication from %d",
399 __func__, ent->type);
400 if (authctxt->pw->pw_uid == 0 &&
401 !auth_root_allowed(auth_method))
404 /* PAM needs to perform account checks after auth */
405 if (options.use_pam && authenticated) {
409 mm_request_receive_expect(pmonitor->m_sendfd,
410 MONITOR_REQ_PAM_ACCOUNT, &m);
411 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
417 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
418 auth_log(authctxt, authenticated, auth_method,
419 compat20 ? " ssh2" : "");
421 authctxt->failures++;
424 /* Cleanup JPAKE context after authentication */
425 if (ent->flags & MON_AUTHDECIDE) {
426 if (authctxt->jpake_ctx != NULL) {
427 jpake_free(authctxt->jpake_ctx);
428 authctxt->jpake_ctx = NULL;
434 if (!authctxt->valid)
435 fatal("%s: authenticated invalid user", __func__);
436 if (strcmp(auth_method, "unknown") == 0)
437 fatal("%s: authentication method name unknown", __func__);
439 debug("%s: %s has been authenticated by privileged process",
440 __func__, authctxt->user);
442 mm_get_keystate(pmonitor);
446 monitor_set_child_handler(pid_t pid)
448 monitor_child_pid = pid;
452 monitor_child_handler(int sig)
454 kill(monitor_child_pid, sig);
458 monitor_child_postauth(struct monitor *pmonitor)
460 monitor_set_child_handler(pmonitor->m_pid);
461 signal(SIGHUP, &monitor_child_handler);
462 signal(SIGTERM, &monitor_child_handler);
463 signal(SIGINT, &monitor_child_handler);
466 mon_dispatch = mon_dispatch_postauth20;
468 /* Permit requests for moduli and signatures */
469 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
470 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
471 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
474 /* and for the GSSAPI key exchange */
475 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS,1);
476 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP,1);
477 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR,1);
481 mon_dispatch = mon_dispatch_postauth15;
482 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
485 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
488 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
489 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
493 monitor_read(pmonitor, mon_dispatch, NULL);
497 monitor_sync(struct monitor *pmonitor)
499 if (options.compression) {
500 /* The member allocation is not visible, so sync it */
501 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
506 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
507 struct mon_table **pent)
515 mm_request_receive(pmonitor->m_sendfd, &m);
516 type = buffer_get_char(&m);
518 debug3("%s: checking request %d", __func__, type);
520 while (ent->f != NULL) {
521 if (ent->type == type)
526 if (ent->f != NULL) {
527 if (!(ent->flags & MON_PERMIT))
528 fatal("%s: unpermitted request %d", __func__,
530 ret = (*ent->f)(pmonitor->m_sendfd, &m);
533 /* The child may use this request only once, disable it */
534 if (ent->flags & MON_ONCE) {
535 debug2("%s: %d used once, disabling now", __func__,
537 ent->flags &= ~MON_PERMIT;
546 fatal("%s: unsupported request: %d", __func__, type);
552 /* allowed key state */
554 monitor_allowed_key(u_char *blob, u_int bloblen)
556 /* make sure key is allowed */
557 if (key_blob == NULL || key_bloblen != bloblen ||
558 memcmp(key_blob, blob, key_bloblen))
564 monitor_reset_key_state(void)
567 if (key_blob != NULL)
569 if (hostbased_cuser != NULL)
570 xfree(hostbased_cuser);
571 if (hostbased_chost != NULL)
572 xfree(hostbased_chost);
575 key_blobtype = MM_NOKEY;
576 hostbased_cuser = NULL;
577 hostbased_chost = NULL;
581 mm_answer_moduli(int sock, Buffer *m)
586 min = buffer_get_int(m);
587 want = buffer_get_int(m);
588 max = buffer_get_int(m);
590 debug3("%s: got parameters: %d %d %d",
591 __func__, min, want, max);
592 /* We need to check here, too, in case the child got corrupted */
593 if (max < min || want < min || max < want)
594 fatal("%s: bad parameters: %d %d %d",
595 __func__, min, want, max);
599 dh = choose_dh(min, want, max);
601 buffer_put_char(m, 0);
604 /* Send first bignum */
605 buffer_put_char(m, 1);
606 buffer_put_bignum2(m, dh->p);
607 buffer_put_bignum2(m, dh->g);
611 mm_request_send(sock, MONITOR_ANS_MODULI, m);
616 mm_answer_sign(int sock, Buffer *m)
621 u_int siglen, datlen;
624 debug3("%s", __func__);
626 keyid = buffer_get_int(m);
627 p = buffer_get_string(m, &datlen);
630 * Supported KEX types will only return SHA1 (20 byte) or
631 * SHA256 (32 byte) hashes
633 if (datlen != 20 && datlen != 32)
634 fatal("%s: data length incorrect: %u", __func__, datlen);
636 /* save session id, it will be passed on the first call */
637 if (session_id2_len == 0) {
638 session_id2_len = datlen;
639 session_id2 = xmalloc(session_id2_len);
640 memcpy(session_id2, p, session_id2_len);
643 if ((key = get_hostkey_by_index(keyid)) == NULL)
644 fatal("%s: no hostkey from index %d", __func__, keyid);
645 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
646 fatal("%s: key_sign failed", __func__);
648 debug3("%s: signature %p(%u)", __func__, signature, siglen);
651 buffer_put_string(m, signature, siglen);
656 mm_request_send(sock, MONITOR_ANS_SIGN, m);
658 /* Turn on permissions for getpwnam */
659 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
664 /* Retrieves the password entry and also checks if the user is permitted */
667 mm_answer_pwnamallow(int sock, Buffer *m)
670 struct passwd *pwent;
673 debug3("%s", __func__);
675 username = buffer_get_string(m, NULL);
677 pwent = getpwnamallow(username);
679 if (authctxt->user) xfree(authctxt->user);
680 authctxt->user = xstrdup(username);
681 setproctitle("%s [priv]", pwent ? username : "unknown");
687 buffer_put_char(m, 0);
688 authctxt->pw = fakepw();
693 authctxt->pw = pwent;
696 buffer_put_char(m, 1);
697 buffer_put_string(m, pwent, sizeof(struct passwd));
698 buffer_put_cstring(m, pwent->pw_name);
699 buffer_put_cstring(m, "*");
700 buffer_put_cstring(m, pwent->pw_gecos);
701 #ifdef HAVE_PW_CLASS_IN_PASSWD
702 buffer_put_cstring(m, pwent->pw_class);
704 buffer_put_cstring(m, pwent->pw_dir);
705 buffer_put_cstring(m, pwent->pw_shell);
708 buffer_put_string(m, &options, sizeof(options));
709 if (options.banner != NULL)
710 buffer_put_cstring(m, options.banner);
711 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
712 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
714 /* For SSHv1 allow authentication now */
716 monitor_permit_authentications(1);
718 /* Allow service/style information on the auth context */
719 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
720 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
725 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
731 int mm_answer_auth2_read_banner(int sock, Buffer *m)
736 banner = auth2_read_banner();
737 buffer_put_cstring(m, banner != NULL ? banner : "");
738 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
747 mm_answer_authserv(int sock, Buffer *m)
749 monitor_permit_authentications(1);
751 authctxt->service = buffer_get_string(m, NULL);
752 authctxt->style = buffer_get_string(m, NULL);
753 debug3("%s: service=%s, style=%s",
754 __func__, authctxt->service, authctxt->style);
756 if (strlen(authctxt->style) == 0) {
757 xfree(authctxt->style);
758 authctxt->style = NULL;
765 mm_answer_authpassword(int sock, Buffer *m)
767 static int call_count;
772 passwd = buffer_get_string(m, &plen);
773 /* Only authenticate if the context is valid */
774 authenticated = options.password_authentication &&
775 auth_password(authctxt, passwd);
776 memset(passwd, 0, strlen(passwd));
780 buffer_put_int(m, authenticated);
782 debug3("%s: sending result %d", __func__, authenticated);
783 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
786 if (plen == 0 && call_count == 1)
787 auth_method = "none";
789 auth_method = "password";
791 /* Causes monitor loop to terminate if authenticated */
792 return (authenticated);
797 mm_answer_bsdauthquery(int sock, Buffer *m)
799 char *name, *infotxt;
805 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
806 &prompts, &echo_on) < 0 ? 0 : 1;
809 buffer_put_int(m, success);
811 buffer_put_cstring(m, prompts[0]);
813 debug3("%s: sending challenge success: %u", __func__, success);
814 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
827 mm_answer_bsdauthrespond(int sock, Buffer *m)
832 if (authctxt->as == 0)
833 fatal("%s: no bsd auth session", __func__);
835 response = buffer_get_string(m, NULL);
836 authok = options.challenge_response_authentication &&
837 auth_userresponse(authctxt->as, response, 0);
839 debug3("%s: <%s> = <%d>", __func__, response, authok);
843 buffer_put_int(m, authok);
845 debug3("%s: sending authenticated: %d", __func__, authok);
846 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
848 auth_method = "bsdauth";
850 return (authok != 0);
856 mm_answer_skeyquery(int sock, Buffer *m)
859 char challenge[1024];
862 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
863 sizeof(challenge)) < 0 ? 0 : 1;
866 buffer_put_int(m, success);
868 buffer_put_cstring(m, challenge);
870 debug3("%s: sending challenge success: %u", __func__, success);
871 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
877 mm_answer_skeyrespond(int sock, Buffer *m)
882 response = buffer_get_string(m, NULL);
884 authok = (options.challenge_response_authentication &&
886 skey_haskey(authctxt->pw->pw_name) == 0 &&
887 skey_passcheck(authctxt->pw->pw_name, response) != -1);
892 buffer_put_int(m, authok);
894 debug3("%s: sending authenticated: %d", __func__, authok);
895 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
897 auth_method = "skey";
899 return (authok != 0);
905 mm_answer_pam_start(int sock, Buffer *m)
907 if (!options.use_pam)
908 fatal("UsePAM not set, but ended up in %s anyway", __func__);
912 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
918 mm_answer_pam_account(int sock, Buffer *m)
922 if (!options.use_pam)
923 fatal("UsePAM not set, but ended up in %s anyway", __func__);
925 ret = do_pam_account();
927 buffer_put_int(m, ret);
928 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
930 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
935 static void *sshpam_ctxt, *sshpam_authok;
936 extern KbdintDevice sshpam_device;
939 mm_answer_pam_init_ctx(int sock, Buffer *m)
942 debug3("%s", __func__);
943 authctxt->user = buffer_get_string(m, NULL);
944 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
945 sshpam_authok = NULL;
947 if (sshpam_ctxt != NULL) {
948 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
949 buffer_put_int(m, 1);
951 buffer_put_int(m, 0);
953 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
958 mm_answer_pam_query(int sock, Buffer *m)
960 char *name, *info, **prompts;
961 u_int i, num, *echo_on;
964 debug3("%s", __func__);
965 sshpam_authok = NULL;
966 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
967 if (ret == 0 && num == 0)
968 sshpam_authok = sshpam_ctxt;
969 if (num > 1 || name == NULL || info == NULL)
972 buffer_put_int(m, ret);
973 buffer_put_cstring(m, name);
975 buffer_put_cstring(m, info);
977 buffer_put_int(m, num);
978 for (i = 0; i < num; ++i) {
979 buffer_put_cstring(m, prompts[i]);
981 buffer_put_int(m, echo_on[i]);
987 auth_method = "keyboard-interactive/pam";
988 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
993 mm_answer_pam_respond(int sock, Buffer *m)
999 debug3("%s", __func__);
1000 sshpam_authok = NULL;
1001 num = buffer_get_int(m);
1003 resp = xcalloc(num, sizeof(char *));
1004 for (i = 0; i < num; ++i)
1005 resp[i] = buffer_get_string(m, NULL);
1006 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1007 for (i = 0; i < num; ++i)
1011 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1014 buffer_put_int(m, ret);
1015 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1016 auth_method = "keyboard-interactive/pam";
1018 sshpam_authok = sshpam_ctxt;
1023 mm_answer_pam_free_ctx(int sock, Buffer *m)
1026 debug3("%s", __func__);
1027 (sshpam_device.free_ctx)(sshpam_ctxt);
1029 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1030 auth_method = "keyboard-interactive/pam";
1031 return (sshpam_authok == sshpam_ctxt);
1036 mm_append_debug(Buffer *m)
1038 if (auth_debug_init && buffer_len(&auth_debug)) {
1039 debug3("%s: Appending debug messages for child", __func__);
1040 buffer_append(m, buffer_ptr(&auth_debug),
1041 buffer_len(&auth_debug));
1042 buffer_clear(&auth_debug);
1047 mm_answer_keyallowed(int sock, Buffer *m)
1050 char *cuser, *chost;
1053 enum mm_keytype type = 0;
1056 debug3("%s entering", __func__);
1058 type = buffer_get_int(m);
1059 cuser = buffer_get_string(m, NULL);
1060 chost = buffer_get_string(m, NULL);
1061 blob = buffer_get_string(m, &bloblen);
1063 key = key_from_blob(blob, bloblen);
1065 if ((compat20 && type == MM_RSAHOSTKEY) ||
1066 (!compat20 && type != MM_RSAHOSTKEY))
1067 fatal("%s: key type and protocol mismatch", __func__);
1069 debug3("%s: key_from_blob: %p", __func__, key);
1071 if (key != NULL && authctxt->valid) {
1074 allowed = options.pubkey_authentication &&
1075 user_key_allowed(authctxt->pw, key);
1076 auth_method = "publickey";
1077 if (options.pubkey_authentication && allowed != 1)
1078 auth_clear_options();
1081 allowed = options.hostbased_authentication &&
1082 hostbased_key_allowed(authctxt->pw,
1084 auth_method = "hostbased";
1087 key->type = KEY_RSA1; /* XXX */
1088 allowed = options.rhosts_rsa_authentication &&
1089 auth_rhosts_rsa_key_allowed(authctxt->pw,
1091 if (options.rhosts_rsa_authentication && allowed != 1)
1092 auth_clear_options();
1093 auth_method = "rsa";
1096 fatal("%s: unknown key type %d", __func__, type);
1103 /* clear temporarily storage (used by verify) */
1104 monitor_reset_key_state();
1107 /* Save temporarily for comparison in verify */
1109 key_bloblen = bloblen;
1110 key_blobtype = type;
1111 hostbased_cuser = cuser;
1112 hostbased_chost = chost;
1114 /* Log failed attempt */
1115 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1121 debug3("%s: key %p is %s",
1122 __func__, key, allowed ? "allowed" : "not allowed");
1125 buffer_put_int(m, allowed);
1126 buffer_put_int(m, forced_command != NULL);
1130 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1132 if (type == MM_RSAHOSTKEY)
1133 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1139 monitor_valid_userblob(u_char *data, u_int datalen)
1147 buffer_append(&b, data, datalen);
1149 if (datafellows & SSH_OLD_SESSIONID) {
1151 len = buffer_len(&b);
1152 if ((session_id2 == NULL) ||
1153 (len < session_id2_len) ||
1154 (memcmp(p, session_id2, session_id2_len) != 0))
1156 buffer_consume(&b, session_id2_len);
1158 p = buffer_get_string(&b, &len);
1159 if ((session_id2 == NULL) ||
1160 (len != session_id2_len) ||
1161 (memcmp(p, session_id2, session_id2_len) != 0))
1165 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1167 p = buffer_get_string(&b, NULL);
1168 if (strcmp(authctxt->user, p) != 0) {
1169 logit("wrong user name passed to monitor: expected %s != %.100s",
1174 buffer_skip_string(&b);
1175 if (datafellows & SSH_BUG_PKAUTH) {
1176 if (!buffer_get_char(&b))
1179 p = buffer_get_string(&b, NULL);
1180 if (strcmp("publickey", p) != 0)
1183 if (!buffer_get_char(&b))
1185 buffer_skip_string(&b);
1187 buffer_skip_string(&b);
1188 if (buffer_len(&b) != 0)
1195 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1204 buffer_append(&b, data, datalen);
1206 p = buffer_get_string(&b, &len);
1207 if ((session_id2 == NULL) ||
1208 (len != session_id2_len) ||
1209 (memcmp(p, session_id2, session_id2_len) != 0))
1213 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1215 p = buffer_get_string(&b, NULL);
1216 if (strcmp(authctxt->user, p) != 0) {
1217 logit("wrong user name passed to monitor: expected %s != %.100s",
1222 buffer_skip_string(&b); /* service */
1223 p = buffer_get_string(&b, NULL);
1224 if (strcmp(p, "hostbased") != 0)
1227 buffer_skip_string(&b); /* pkalg */
1228 buffer_skip_string(&b); /* pkblob */
1230 /* verify client host, strip trailing dot if necessary */
1231 p = buffer_get_string(&b, NULL);
1232 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1234 if (strcmp(p, chost) != 0)
1238 /* verify client user */
1239 p = buffer_get_string(&b, NULL);
1240 if (strcmp(p, cuser) != 0)
1244 if (buffer_len(&b) != 0)
1251 mm_answer_keyverify(int sock, Buffer *m)
1254 u_char *signature, *data, *blob;
1255 u_int signaturelen, datalen, bloblen;
1259 blob = buffer_get_string(m, &bloblen);
1260 signature = buffer_get_string(m, &signaturelen);
1261 data = buffer_get_string(m, &datalen);
1263 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1264 !monitor_allowed_key(blob, bloblen))
1265 fatal("%s: bad key, not previously allowed", __func__);
1267 key = key_from_blob(blob, bloblen);
1269 fatal("%s: bad public key blob", __func__);
1271 switch (key_blobtype) {
1273 valid_data = monitor_valid_userblob(data, datalen);
1276 valid_data = monitor_valid_hostbasedblob(data, datalen,
1277 hostbased_cuser, hostbased_chost);
1284 fatal("%s: bad signature data blob", __func__);
1286 verified = key_verify(key, signature, signaturelen, data, datalen);
1287 debug3("%s: key %p signature %s",
1288 __func__, key, (verified == 1) ? "verified" : "unverified");
1295 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1297 monitor_reset_key_state();
1300 buffer_put_int(m, verified);
1301 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1303 return (verified == 1);
1307 mm_record_login(Session *s, struct passwd *pw)
1310 struct sockaddr_storage from;
1313 * Get IP address of client. If the connection is not a socket, let
1314 * the address be 0.0.0.0.
1316 memset(&from, 0, sizeof(from));
1317 fromlen = sizeof(from);
1318 if (packet_connection_is_on_socket()) {
1319 if (getpeername(packet_get_connection_in(),
1320 (struct sockaddr *)&from, &fromlen) < 0) {
1321 debug("getpeername: %.100s", strerror(errno));
1325 /* Record that there was a login on that tty from the remote host. */
1326 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1327 get_remote_name_or_ip(utmp_len, options.use_dns),
1328 (struct sockaddr *)&from, fromlen);
1332 mm_session_close(Session *s)
1334 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1335 if (s->ttyfd != -1) {
1336 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1337 session_pty_cleanup2(s);
1339 session_unused(s->self);
1343 mm_answer_pty(int sock, Buffer *m)
1345 extern struct monitor *pmonitor;
1349 debug3("%s entering", __func__);
1355 s->authctxt = authctxt;
1356 s->pw = authctxt->pw;
1357 s->pid = pmonitor->m_pid;
1358 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1361 pty_setowner(authctxt->pw, s->tty);
1363 buffer_put_int(m, 1);
1364 buffer_put_cstring(m, s->tty);
1366 /* We need to trick ttyslot */
1367 if (dup2(s->ttyfd, 0) == -1)
1368 fatal("%s: dup2", __func__);
1370 mm_record_login(s, authctxt->pw);
1372 /* Now we can close the file descriptor again */
1375 /* send messages generated by record_login */
1376 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1377 buffer_clear(&loginmsg);
1379 mm_request_send(sock, MONITOR_ANS_PTY, m);
1381 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1382 mm_send_fd(sock, s->ttyfd) == -1)
1383 fatal("%s: send fds failed", __func__);
1385 /* make sure nothing uses fd 0 */
1386 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1387 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1389 error("%s: fd0 %d != 0", __func__, fd0);
1391 /* slave is not needed */
1393 s->ttyfd = s->ptyfd;
1394 /* no need to dup() because nobody closes ptyfd */
1395 s->ptymaster = s->ptyfd;
1397 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1403 mm_session_close(s);
1404 buffer_put_int(m, 0);
1405 mm_request_send(sock, MONITOR_ANS_PTY, m);
1410 mm_answer_pty_cleanup(int sock, Buffer *m)
1415 debug3("%s entering", __func__);
1417 tty = buffer_get_string(m, NULL);
1418 if ((s = session_by_tty(tty)) != NULL)
1419 mm_session_close(s);
1426 mm_answer_sesskey(int sock, Buffer *m)
1431 /* Turn off permissions */
1432 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1434 if ((p = BN_new()) == NULL)
1435 fatal("%s: BN_new", __func__);
1437 buffer_get_bignum2(m, p);
1439 rsafail = ssh1_session_key(p);
1442 buffer_put_int(m, rsafail);
1443 buffer_put_bignum2(m, p);
1447 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1449 /* Turn on permissions for sessid passing */
1450 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1456 mm_answer_sessid(int sock, Buffer *m)
1460 debug3("%s entering", __func__);
1462 if (buffer_len(m) != 16)
1463 fatal("%s: bad ssh1 session id", __func__);
1464 for (i = 0; i < 16; i++)
1465 session_id[i] = buffer_get_char(m);
1467 /* Turn on permissions for getpwnam */
1468 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1474 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1478 u_char *blob = NULL;
1482 debug3("%s entering", __func__);
1484 auth_method = "rsa";
1485 if (options.rsa_authentication && authctxt->valid) {
1486 if ((client_n = BN_new()) == NULL)
1487 fatal("%s: BN_new", __func__);
1488 buffer_get_bignum2(m, client_n);
1489 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1490 BN_clear_free(client_n);
1493 buffer_put_int(m, allowed);
1494 buffer_put_int(m, forced_command != NULL);
1496 /* clear temporarily storage (used by generate challenge) */
1497 monitor_reset_key_state();
1499 if (allowed && key != NULL) {
1500 key->type = KEY_RSA; /* cheat for key_to_blob */
1501 if (key_to_blob(key, &blob, &blen) == 0)
1502 fatal("%s: key_to_blob failed", __func__);
1503 buffer_put_string(m, blob, blen);
1505 /* Save temporarily for comparison in verify */
1508 key_blobtype = MM_RSAUSERKEY;
1515 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1517 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1518 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1523 mm_answer_rsa_challenge(int sock, Buffer *m)
1529 debug3("%s entering", __func__);
1531 if (!authctxt->valid)
1532 fatal("%s: authctxt not valid", __func__);
1533 blob = buffer_get_string(m, &blen);
1534 if (!monitor_allowed_key(blob, blen))
1535 fatal("%s: bad key, not previously allowed", __func__);
1536 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1537 fatal("%s: key type mismatch", __func__);
1538 if ((key = key_from_blob(blob, blen)) == NULL)
1539 fatal("%s: received bad key", __func__);
1540 if (key->type != KEY_RSA)
1541 fatal("%s: received bad key type %d", __func__, key->type);
1542 key->type = KEY_RSA1;
1544 BN_clear_free(ssh1_challenge);
1545 ssh1_challenge = auth_rsa_generate_challenge(key);
1548 buffer_put_bignum2(m, ssh1_challenge);
1550 debug3("%s sending reply", __func__);
1551 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1553 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1561 mm_answer_rsa_response(int sock, Buffer *m)
1564 u_char *blob, *response;
1568 debug3("%s entering", __func__);
1570 if (!authctxt->valid)
1571 fatal("%s: authctxt not valid", __func__);
1572 if (ssh1_challenge == NULL)
1573 fatal("%s: no ssh1_challenge", __func__);
1575 blob = buffer_get_string(m, &blen);
1576 if (!monitor_allowed_key(blob, blen))
1577 fatal("%s: bad key, not previously allowed", __func__);
1578 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1579 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1580 if ((key = key_from_blob(blob, blen)) == NULL)
1581 fatal("%s: received bad key", __func__);
1582 response = buffer_get_string(m, &len);
1584 fatal("%s: received bad response to challenge", __func__);
1585 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1591 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1594 BN_clear_free(ssh1_challenge);
1595 ssh1_challenge = NULL;
1596 monitor_reset_key_state();
1599 buffer_put_int(m, success);
1600 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1606 mm_answer_term(int sock, Buffer *req)
1608 extern struct monitor *pmonitor;
1611 debug3("%s: tearing down sessions", __func__);
1613 /* The child is terminating */
1614 session_destroy_all(&mm_session_close);
1617 if (options.use_pam)
1621 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1625 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1627 /* Terminate process */
1631 #ifdef SSH_AUDIT_EVENTS
1632 /* Report that an audit event occurred */
1634 mm_answer_audit_event(int socket, Buffer *m)
1636 ssh_audit_event_t event;
1638 debug3("%s entering", __func__);
1640 event = buffer_get_int(m);
1642 case SSH_AUTH_FAIL_PUBKEY:
1643 case SSH_AUTH_FAIL_HOSTBASED:
1644 case SSH_AUTH_FAIL_GSSAPI:
1645 case SSH_LOGIN_EXCEED_MAXTRIES:
1646 case SSH_LOGIN_ROOT_DENIED:
1647 case SSH_CONNECTION_CLOSE:
1648 case SSH_INVALID_USER:
1652 fatal("Audit event type %d not permitted", event);
1659 mm_answer_audit_command(int socket, Buffer *m)
1664 debug3("%s entering", __func__);
1665 cmd = buffer_get_string(m, &len);
1666 /* sanity check command, if so how? */
1667 audit_run_command(cmd);
1671 #endif /* SSH_AUDIT_EVENTS */
1674 monitor_apply_keystate(struct monitor *pmonitor)
1677 set_newkeys(MODE_IN);
1678 set_newkeys(MODE_OUT);
1680 packet_set_protocol_flags(child_state.ssh1protoflags);
1681 packet_set_encryption_key(child_state.ssh1key,
1682 child_state.ssh1keylen, child_state.ssh1cipher);
1683 xfree(child_state.ssh1key);
1686 /* for rc4 and other stateful ciphers */
1687 packet_set_keycontext(MODE_OUT, child_state.keyout);
1688 xfree(child_state.keyout);
1689 packet_set_keycontext(MODE_IN, child_state.keyin);
1690 xfree(child_state.keyin);
1693 packet_set_iv(MODE_OUT, child_state.ivout);
1694 xfree(child_state.ivout);
1695 packet_set_iv(MODE_IN, child_state.ivin);
1696 xfree(child_state.ivin);
1699 memcpy(&incoming_stream, &child_state.incoming,
1700 sizeof(incoming_stream));
1701 memcpy(&outgoing_stream, &child_state.outgoing,
1702 sizeof(outgoing_stream));
1704 /* Update with new address */
1705 if (options.compression)
1706 mm_init_compression(pmonitor->m_zlib);
1708 /* Network I/O buffers */
1709 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1710 buffer_clear(&input);
1711 buffer_append(&input, child_state.input, child_state.ilen);
1712 memset(child_state.input, 0, child_state.ilen);
1713 xfree(child_state.input);
1715 buffer_clear(&output);
1716 buffer_append(&output, child_state.output, child_state.olen);
1717 memset(child_state.output, 0, child_state.olen);
1718 xfree(child_state.output);
1722 mm_get_kex(Buffer *m)
1728 kex = xcalloc(1, sizeof(*kex));
1729 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1730 if ((session_id2 == NULL) ||
1731 (kex->session_id_len != session_id2_len) ||
1732 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1733 fatal("mm_get_get: internal error: bad session id");
1734 kex->we_need = buffer_get_int(m);
1735 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1736 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1737 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1738 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1740 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
1741 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
1742 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
1745 kex->hostkey_type = buffer_get_int(m);
1746 kex->kex_type = buffer_get_int(m);
1747 blob = buffer_get_string(m, &bloblen);
1748 buffer_init(&kex->my);
1749 buffer_append(&kex->my, blob, bloblen);
1751 blob = buffer_get_string(m, &bloblen);
1752 buffer_init(&kex->peer);
1753 buffer_append(&kex->peer, blob, bloblen);
1756 kex->flags = buffer_get_int(m);
1757 kex->client_version_string = buffer_get_string(m, NULL);
1758 kex->server_version_string = buffer_get_string(m, NULL);
1759 kex->load_host_key=&get_hostkey_by_type;
1760 kex->host_key_index=&get_hostkey_index;
1765 /* This function requries careful sanity checking */
1768 mm_get_keystate(struct monitor *pmonitor)
1772 u_int bloblen, plen;
1773 u_int32_t seqnr, packets;
1774 u_int64_t blocks, bytes;
1776 debug3("%s: Waiting for new keys", __func__);
1779 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1781 child_state.ssh1protoflags = buffer_get_int(&m);
1782 child_state.ssh1cipher = buffer_get_int(&m);
1783 child_state.ssh1key = buffer_get_string(&m,
1784 &child_state.ssh1keylen);
1785 child_state.ivout = buffer_get_string(&m,
1786 &child_state.ivoutlen);
1787 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1790 /* Get the Kex for rekeying */
1791 *pmonitor->m_pkex = mm_get_kex(&m);
1794 blob = buffer_get_string(&m, &bloblen);
1795 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1798 debug3("%s: Waiting for second key", __func__);
1799 blob = buffer_get_string(&m, &bloblen);
1800 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1803 /* Now get sequence numbers for the packets */
1804 seqnr = buffer_get_int(&m);
1805 blocks = buffer_get_int64(&m);
1806 packets = buffer_get_int(&m);
1807 bytes = buffer_get_int64(&m);
1808 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
1809 seqnr = buffer_get_int(&m);
1810 blocks = buffer_get_int64(&m);
1811 packets = buffer_get_int(&m);
1812 bytes = buffer_get_int64(&m);
1813 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
1816 /* Get the key context */
1817 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1818 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1820 debug3("%s: Getting compression state", __func__);
1821 /* Get compression state */
1822 p = buffer_get_string(&m, &plen);
1823 if (plen != sizeof(child_state.outgoing))
1824 fatal("%s: bad request size", __func__);
1825 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1828 p = buffer_get_string(&m, &plen);
1829 if (plen != sizeof(child_state.incoming))
1830 fatal("%s: bad request size", __func__);
1831 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1834 /* Network I/O buffers */
1835 debug3("%s: Getting Network I/O buffers", __func__);
1836 child_state.input = buffer_get_string(&m, &child_state.ilen);
1837 child_state.output = buffer_get_string(&m, &child_state.olen);
1843 /* Allocation functions for zlib */
1845 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1847 size_t len = (size_t) size * ncount;
1850 if (len == 0 || ncount > SIZE_T_MAX / size)
1851 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1853 address = mm_malloc(mm, len);
1859 mm_zfree(struct mm_master *mm, void *address)
1861 mm_free(mm, address);
1865 mm_init_compression(struct mm_master *mm)
1867 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1868 outgoing_stream.zfree = (free_func)mm_zfree;
1869 outgoing_stream.opaque = mm;
1871 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1872 incoming_stream.zfree = (free_func)mm_zfree;
1873 incoming_stream.opaque = mm;
1878 #define FD_CLOSEONEXEC(x) do { \
1879 if (fcntl(x, F_SETFD, 1) == -1) \
1880 fatal("fcntl(%d, F_SETFD)", x); \
1884 monitor_socketpair(int *pair)
1886 #ifdef HAVE_SOCKETPAIR
1887 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1888 fatal("%s: socketpair", __func__);
1890 fatal("%s: UsePrivilegeSeparation=yes not supported",
1893 FD_CLOSEONEXEC(pair[0]);
1894 FD_CLOSEONEXEC(pair[1]);
1897 #define MM_MEMSIZE 65536
1902 struct monitor *mon;
1905 mon = xcalloc(1, sizeof(*mon));
1907 monitor_socketpair(pair);
1909 mon->m_recvfd = pair[0];
1910 mon->m_sendfd = pair[1];
1912 /* Used to share zlib space across processes */
1913 if (options.compression) {
1914 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1915 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1917 /* Compression needs to share state across borders */
1918 mm_init_compression(mon->m_zlib);
1925 monitor_reinit(struct monitor *mon)
1929 monitor_socketpair(pair);
1931 mon->m_recvfd = pair[0];
1932 mon->m_sendfd = pair[1];
1937 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1943 goid.elements = buffer_get_string(m, &len);
1946 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1948 xfree(goid.elements);
1951 buffer_put_int(m, major);
1953 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1955 /* Now we have a context, enable the step */
1956 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1962 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1965 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1966 OM_uint32 major, minor;
1967 OM_uint32 flags = 0; /* GSI needs this */
1970 in.value = buffer_get_string(m, &len);
1972 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1976 buffer_put_int(m, major);
1977 buffer_put_string(m, out.value, out.length);
1978 buffer_put_int(m, flags);
1979 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1981 gss_release_buffer(&minor, &out);
1983 if (major == GSS_S_COMPLETE) {
1984 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1985 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1986 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
1987 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1988 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
1994 mm_answer_gss_checkmic(int sock, Buffer *m)
1996 gss_buffer_desc gssbuf, mic;
2000 gssbuf.value = buffer_get_string(m, &len);
2001 gssbuf.length = len;
2002 mic.value = buffer_get_string(m, &len);
2005 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
2007 xfree(gssbuf.value);
2011 buffer_put_int(m, ret);
2013 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
2015 if (!GSS_ERROR(ret))
2016 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2022 mm_answer_gss_userok(int sock, Buffer *m)
2026 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2029 buffer_put_int(m, authenticated);
2031 debug3("%s: sending result %d", __func__, authenticated);
2032 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
2034 auth_method = "gssapi-with-mic";
2036 /* Monitor loop will terminate if authenticated */
2037 return (authenticated);
2041 mm_answer_gss_sign(int socket, Buffer *m)
2043 gss_buffer_desc data;
2044 gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
2045 OM_uint32 major, minor;
2048 data.value = buffer_get_string(m, &len);
2050 if (data.length != 20)
2051 fatal("%s: data length incorrect: %d", __func__, (int)data.length);
2053 /* Save the session ID on the first time around */
2054 if (session_id2_len == 0) {
2055 session_id2_len = data.length;
2056 session_id2 = xmalloc(session_id2_len);
2057 memcpy(session_id2, data.value, session_id2_len);
2059 major = ssh_gssapi_sign(gsscontext, &data, &hash);
2064 buffer_put_int(m, major);
2065 buffer_put_string(m, hash.value, hash.length);
2067 mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
2069 gss_release_buffer(&minor, &hash);
2071 /* Turn on getpwnam permissions */
2072 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
2078 mm_answer_gss_error(int socket, Buffer *m) {
2079 OM_uint32 major,minor;
2082 msg=ssh_gssapi_last_error(gsscontext,&major,&minor);
2084 buffer_put_int(m,major);
2085 buffer_put_int(m,minor);
2086 buffer_put_cstring(m,msg);
2088 mm_request_send(socket,MONITOR_ANS_GSSERR,m);
2096 mm_answer_gss_indicate_mechs(int socket, Buffer *m) {
2097 OM_uint32 major,minor;
2098 gss_OID_set mech_set;
2101 major=gss_indicate_mechs(&minor, &mech_set);
2104 buffer_put_int(m, major);
2105 buffer_put_int(m, mech_set->count);
2106 for (i=0; i < mech_set->count; i++) {
2107 buffer_put_string(m, mech_set->elements[i].elements,
2108 mech_set->elements[i].length);
2111 #if !defined(MECHGLUE) /* mechglue memory management bug ??? */
2112 gss_release_oid_set(&minor,&mech_set);
2115 mm_request_send(socket,MONITOR_ANS_GSSMECHS,m);
2121 mm_answer_gss_localname(int socket, Buffer *m) {
2124 ssh_gssapi_localname(&name);
2128 buffer_put_cstring(m, name);
2129 debug3("%s: sending result %s", __func__, name);
2132 buffer_put_cstring(m, "");
2133 debug3("%s: sending result \"\"", __func__);
2136 mm_request_send(socket, MONITOR_ANS_GSSLOCALNAME, m);
2144 mm_answer_jpake_step1(int sock, Buffer *m)
2146 struct jpake_ctx *pctx;
2147 u_char *x3_proof, *x4_proof;
2148 u_int x3_proof_len, x4_proof_len;
2150 if (!options.zero_knowledge_password_authentication)
2151 fatal("zero_knowledge_password_authentication disabled");
2153 if (authctxt->jpake_ctx != NULL)
2154 fatal("%s: authctxt->jpake_ctx already set (%p)",
2155 __func__, authctxt->jpake_ctx);
2156 authctxt->jpake_ctx = pctx = jpake_new();
2158 jpake_step1(pctx->grp,
2159 &pctx->server_id, &pctx->server_id_len,
2160 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2161 &x3_proof, &x3_proof_len,
2162 &x4_proof, &x4_proof_len);
2164 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2168 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2169 buffer_put_bignum2(m, pctx->g_x3);
2170 buffer_put_bignum2(m, pctx->g_x4);
2171 buffer_put_string(m, x3_proof, x3_proof_len);
2172 buffer_put_string(m, x4_proof, x4_proof_len);
2174 debug3("%s: sending step1", __func__);
2175 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2177 bzero(x3_proof, x3_proof_len);
2178 bzero(x4_proof, x4_proof_len);
2182 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2183 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2189 mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2191 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2192 char *hash_scheme, *salt;
2195 fatal("%s: pctx == NULL", __func__);
2197 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2200 /* pctx->s is sensitive, not returned to slave */
2201 buffer_put_cstring(m, hash_scheme);
2202 buffer_put_cstring(m, salt);
2204 debug3("%s: sending pwdata", __func__);
2205 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2207 bzero(hash_scheme, strlen(hash_scheme));
2208 bzero(salt, strlen(salt));
2212 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2218 mm_answer_jpake_step2(int sock, Buffer *m)
2220 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2221 u_char *x1_proof, *x2_proof, *x4_s_proof;
2222 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2225 fatal("%s: pctx == NULL", __func__);
2227 if ((pctx->g_x1 = BN_new()) == NULL ||
2228 (pctx->g_x2 = BN_new()) == NULL)
2229 fatal("%s: BN_new", __func__);
2230 buffer_get_bignum2(m, pctx->g_x1);
2231 buffer_get_bignum2(m, pctx->g_x2);
2232 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2233 x1_proof = buffer_get_string(m, &x1_proof_len);
2234 x2_proof = buffer_get_string(m, &x2_proof_len);
2236 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2237 pctx->g_x1, pctx->g_x2, pctx->x4,
2238 pctx->client_id, pctx->client_id_len,
2239 pctx->server_id, pctx->server_id_len,
2240 x1_proof, x1_proof_len,
2241 x2_proof, x2_proof_len,
2243 &x4_s_proof, &x4_s_proof_len);
2245 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2247 bzero(x1_proof, x1_proof_len);
2248 bzero(x2_proof, x2_proof_len);
2254 buffer_put_bignum2(m, pctx->b);
2255 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2257 debug3("%s: sending step2", __func__);
2258 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2260 bzero(x4_s_proof, x4_s_proof_len);
2263 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2269 mm_answer_jpake_key_confirm(int sock, Buffer *m)
2271 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2273 u_int x2_s_proof_len;
2276 fatal("%s: pctx == NULL", __func__);
2278 if ((pctx->a = BN_new()) == NULL)
2279 fatal("%s: BN_new", __func__);
2280 buffer_get_bignum2(m, pctx->a);
2281 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2283 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2284 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2285 pctx->server_id, pctx->server_id_len,
2286 pctx->client_id, pctx->client_id_len,
2287 session_id2, session_id2_len,
2288 x2_s_proof, x2_s_proof_len,
2290 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2292 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2294 bzero(x2_s_proof, x2_s_proof_len);
2297 /* pctx->k is sensitive, not sent */
2298 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2300 debug3("%s: sending confirmation hash", __func__);
2301 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2303 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2309 mm_answer_jpake_check_confirm(int sock, Buffer *m)
2311 int authenticated = 0;
2312 u_char *peer_confirm_hash;
2313 u_int peer_confirm_hash_len;
2314 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2317 fatal("%s: pctx == NULL", __func__);
2319 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2321 authenticated = jpake_check_confirm(pctx->k,
2322 pctx->client_id, pctx->client_id_len,
2323 session_id2, session_id2_len,
2324 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2326 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2328 bzero(peer_confirm_hash, peer_confirm_hash_len);
2329 xfree(peer_confirm_hash);
2332 buffer_put_int(m, authenticated);
2334 debug3("%s: sending result %d", __func__, authenticated);
2335 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2337 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2339 auth_method = "jpake-01@openssh.com";
2340 return authenticated;