1 /* $OpenBSD: monitor.c,v 1.128 2013/11/04 11:51:16 markus 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"
50 # ifdef HAVE_SYS_POLL_H
51 # include <sys/poll.h>
59 #include <openssl/dh.h>
61 #include "openbsd-compat/sys-queue.h"
72 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
75 #define TARGET_OS_MAC 1
80 #include "auth-options.h"
89 #include "monitor_mm.h"
93 #include "monitor_wrap.h"
94 #include "monitor_fdpass.h"
103 static Gssctxt *gsscontext = NULL;
107 extern ServerOptions options;
108 extern u_int utmp_len;
109 extern Newkeys *current_keys[];
110 extern z_stream incoming_stream;
111 extern z_stream outgoing_stream;
112 extern u_char session_id[];
113 extern Buffer auth_debug;
114 extern int auth_debug_init;
115 extern Buffer loginmsg;
117 /* State exported from the child */
138 u_int64_t sent_bytes;
139 u_int64_t recv_bytes;
142 /* Functions on the monitor that answer unprivileged requests */
144 int mm_answer_moduli(int, Buffer *);
145 int mm_answer_sign(int, Buffer *);
146 int mm_answer_pwnamallow(int, Buffer *);
147 int mm_answer_auth2_read_banner(int, Buffer *);
148 int mm_answer_authserv(int, Buffer *);
149 int mm_answer_authpassword(int, Buffer *);
150 int mm_answer_bsdauthquery(int, Buffer *);
151 int mm_answer_bsdauthrespond(int, Buffer *);
152 int mm_answer_skeyquery(int, Buffer *);
153 int mm_answer_skeyrespond(int, Buffer *);
154 int mm_answer_keyallowed(int, Buffer *);
155 int mm_answer_keyverify(int, Buffer *);
156 int mm_answer_pty(int, Buffer *);
157 int mm_answer_pty_cleanup(int, Buffer *);
158 int mm_answer_term(int, Buffer *);
159 int mm_answer_rsa_keyallowed(int, Buffer *);
160 int mm_answer_rsa_challenge(int, Buffer *);
161 int mm_answer_rsa_response(int, Buffer *);
162 int mm_answer_sesskey(int, Buffer *);
163 int mm_answer_sessid(int, Buffer *);
164 int mm_answer_jpake_get_pwdata(int, Buffer *);
165 int mm_answer_jpake_step1(int, Buffer *);
166 int mm_answer_jpake_step2(int, Buffer *);
167 int mm_answer_jpake_key_confirm(int, Buffer *);
168 int mm_answer_jpake_check_confirm(int, Buffer *);
171 int mm_answer_pam_start(int, Buffer *);
172 int mm_answer_pam_account(int, Buffer *);
173 int mm_answer_pam_init_ctx(int, Buffer *);
174 int mm_answer_pam_query(int, Buffer *);
175 int mm_answer_pam_respond(int, Buffer *);
176 int mm_answer_pam_free_ctx(int, Buffer *);
180 int mm_answer_gss_setup_ctx(int, Buffer *);
181 int mm_answer_gss_accept_ctx(int, Buffer *);
182 int mm_answer_gss_userok(int, Buffer *);
183 int mm_answer_gss_checkmic(int, Buffer *);
186 #ifdef SSH_AUDIT_EVENTS
187 int mm_answer_audit_event(int, Buffer *);
188 int mm_answer_audit_command(int, Buffer *);
191 static int monitor_read_log(struct monitor *);
193 static Authctxt *authctxt;
194 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
196 /* local state for key verify */
197 static u_char *key_blob = NULL;
198 static u_int key_bloblen = 0;
199 static int key_blobtype = MM_NOKEY;
200 static char *hostbased_cuser = NULL;
201 static char *hostbased_chost = NULL;
202 static char *auth_method = "unknown";
203 static char *auth_submethod = NULL;
204 static u_int session_id2_len = 0;
205 static u_char *session_id2 = NULL;
206 static pid_t monitor_child_pid;
209 enum monitor_reqtype type;
211 int (*f)(int, Buffer *);
214 #define MON_ISAUTH 0x0004 /* Required for Authentication */
215 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
216 #define MON_ONCE 0x0010 /* Disable after calling */
217 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
219 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
221 #define MON_PERMIT 0x1000 /* Request is permitted */
223 struct mon_table mon_dispatch_proto20[] = {
224 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
225 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
226 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
227 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
228 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
229 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
231 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
232 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
233 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
234 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
235 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
236 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
238 #ifdef SSH_AUDIT_EVENTS
239 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
242 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
243 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
246 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
247 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
249 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
250 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
252 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
253 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
254 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
255 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
258 {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
259 {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
260 {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
261 {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
262 {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
267 struct mon_table mon_dispatch_postauth20[] = {
268 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
269 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
270 {MONITOR_REQ_PTY, 0, mm_answer_pty},
271 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
272 {MONITOR_REQ_TERM, 0, mm_answer_term},
273 #ifdef SSH_AUDIT_EVENTS
274 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
275 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
280 struct mon_table mon_dispatch_proto15[] = {
281 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
282 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
283 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
284 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
285 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
286 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
287 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
288 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
290 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
291 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
294 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
295 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
298 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
299 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
300 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
301 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
302 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
303 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
305 #ifdef SSH_AUDIT_EVENTS
306 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
311 struct mon_table mon_dispatch_postauth15[] = {
312 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
313 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
314 {MONITOR_REQ_TERM, 0, mm_answer_term},
315 #ifdef SSH_AUDIT_EVENTS
316 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
317 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
322 struct mon_table *mon_dispatch;
324 /* Specifies if a certain message is allowed at the moment */
327 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
329 while (ent->f != NULL) {
330 if (ent->type == type) {
331 ent->flags &= ~MON_PERMIT;
332 ent->flags |= permit ? MON_PERMIT : 0;
340 monitor_permit_authentications(int permit)
342 struct mon_table *ent = mon_dispatch;
344 while (ent->f != NULL) {
345 if (ent->flags & MON_AUTH) {
346 ent->flags &= ~MON_PERMIT;
347 ent->flags |= permit ? MON_PERMIT : 0;
354 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
356 struct mon_table *ent;
357 int authenticated = 0, partial = 0;
359 debug3("preauth child monitor started");
361 close(pmonitor->m_recvfd);
362 close(pmonitor->m_log_sendfd);
363 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
365 authctxt = _authctxt;
366 memset(authctxt, 0, sizeof(*authctxt));
368 authctxt->loginmsg = &loginmsg;
371 mon_dispatch = mon_dispatch_proto20;
373 /* Permit requests for moduli and signatures */
374 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
375 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
377 mon_dispatch = mon_dispatch_proto15;
379 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
382 /* The first few requests do not require asynchronous access */
383 while (!authenticated) {
385 auth_method = "unknown";
386 auth_submethod = NULL;
387 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
389 /* Special handling for multiple required authentications */
390 if (options.num_auth_methods != 0) {
392 fatal("AuthenticationMethods is not supported"
393 "with SSH protocol 1");
395 !auth2_update_methods_lists(authctxt,
396 auth_method, auth_submethod)) {
397 debug3("%s: method %s: partial", __func__,
405 if (!(ent->flags & MON_AUTHDECIDE))
406 fatal("%s: unexpected authentication from %d",
407 __func__, ent->type);
408 if (authctxt->pw->pw_uid == 0 &&
409 !auth_root_allowed(auth_method))
412 /* PAM needs to perform account checks after auth */
413 if (options.use_pam && authenticated) {
417 mm_request_receive_expect(pmonitor->m_sendfd,
418 MONITOR_REQ_PAM_ACCOUNT, &m);
419 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
424 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
425 auth_log(authctxt, authenticated, partial,
426 auth_method, auth_submethod);
428 authctxt->failures++;
431 /* Cleanup JPAKE context after authentication */
432 if (ent->flags & MON_AUTHDECIDE) {
433 if (authctxt->jpake_ctx != NULL) {
434 jpake_free(authctxt->jpake_ctx);
435 authctxt->jpake_ctx = NULL;
441 if (!authctxt->valid)
442 fatal("%s: authenticated invalid user", __func__);
443 if (strcmp(auth_method, "unknown") == 0)
444 fatal("%s: authentication method name unknown", __func__);
446 debug("%s: %s has been authenticated by privileged process",
447 __func__, authctxt->user);
449 mm_get_keystate(pmonitor);
451 /* Drain any buffered messages from the child */
452 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
455 close(pmonitor->m_sendfd);
456 close(pmonitor->m_log_recvfd);
457 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
461 monitor_set_child_handler(pid_t pid)
463 monitor_child_pid = pid;
467 monitor_child_handler(int sig)
469 kill(monitor_child_pid, sig);
473 monitor_child_postauth(struct monitor *pmonitor)
475 close(pmonitor->m_recvfd);
476 pmonitor->m_recvfd = -1;
478 monitor_set_child_handler(pmonitor->m_pid);
479 signal(SIGHUP, &monitor_child_handler);
480 signal(SIGTERM, &monitor_child_handler);
481 signal(SIGINT, &monitor_child_handler);
484 mon_dispatch = mon_dispatch_postauth20;
486 /* Permit requests for moduli and signatures */
487 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
488 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
489 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
491 mon_dispatch = mon_dispatch_postauth15;
492 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
495 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
496 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
500 monitor_read(pmonitor, mon_dispatch, NULL);
504 monitor_sync(struct monitor *pmonitor)
506 if (options.compression) {
507 /* The member allocation is not visible, so sync it */
508 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
513 monitor_read_log(struct monitor *pmonitor)
519 buffer_init(&logmsg);
522 buffer_append_space(&logmsg, 4);
523 if (atomicio(read, pmonitor->m_log_recvfd,
524 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
525 if (errno == EPIPE) {
526 buffer_free(&logmsg);
527 debug("%s: child log fd closed", __func__);
528 close(pmonitor->m_log_recvfd);
529 pmonitor->m_log_recvfd = -1;
532 fatal("%s: log fd read: %s", __func__, strerror(errno));
534 len = buffer_get_int(&logmsg);
535 if (len <= 4 || len > 8192)
536 fatal("%s: invalid log message length %u", __func__, len);
538 /* Read severity, message */
539 buffer_clear(&logmsg);
540 buffer_append_space(&logmsg, len);
541 if (atomicio(read, pmonitor->m_log_recvfd,
542 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
543 fatal("%s: log fd read: %s", __func__, strerror(errno));
546 level = buffer_get_int(&logmsg);
547 msg = buffer_get_string(&logmsg, NULL);
548 if (log_level_name(level) == NULL)
549 fatal("%s: invalid log level %u (corrupted message?)",
551 do_log2(level, "%s [preauth]", msg);
553 buffer_free(&logmsg);
560 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
561 struct mon_table **pent)
566 struct pollfd pfd[2];
569 bzero(&pfd, sizeof(pfd));
570 pfd[0].fd = pmonitor->m_sendfd;
571 pfd[0].events = POLLIN;
572 pfd[1].fd = pmonitor->m_log_recvfd;
573 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
574 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
575 if (errno == EINTR || errno == EAGAIN)
577 fatal("%s: poll: %s", __func__, strerror(errno));
579 if (pfd[1].revents) {
581 * Drain all log messages before processing next
584 monitor_read_log(pmonitor);
588 break; /* Continues below */
593 mm_request_receive(pmonitor->m_sendfd, &m);
594 type = buffer_get_char(&m);
596 debug3("%s: checking request %d", __func__, type);
598 while (ent->f != NULL) {
599 if (ent->type == type)
604 if (ent->f != NULL) {
605 if (!(ent->flags & MON_PERMIT))
606 fatal("%s: unpermitted request %d", __func__,
608 ret = (*ent->f)(pmonitor->m_sendfd, &m);
611 /* The child may use this request only once, disable it */
612 if (ent->flags & MON_ONCE) {
613 debug2("%s: %d used once, disabling now", __func__,
615 ent->flags &= ~MON_PERMIT;
624 fatal("%s: unsupported request: %d", __func__, type);
630 /* allowed key state */
632 monitor_allowed_key(u_char *blob, u_int bloblen)
634 /* make sure key is allowed */
635 if (key_blob == NULL || key_bloblen != bloblen ||
636 timingsafe_bcmp(key_blob, blob, key_bloblen))
642 monitor_reset_key_state(void)
646 free(hostbased_cuser);
647 free(hostbased_chost);
650 key_blobtype = MM_NOKEY;
651 hostbased_cuser = NULL;
652 hostbased_chost = NULL;
656 mm_answer_moduli(int sock, Buffer *m)
661 min = buffer_get_int(m);
662 want = buffer_get_int(m);
663 max = buffer_get_int(m);
665 debug3("%s: got parameters: %d %d %d",
666 __func__, min, want, max);
667 /* We need to check here, too, in case the child got corrupted */
668 if (max < min || want < min || max < want)
669 fatal("%s: bad parameters: %d %d %d",
670 __func__, min, want, max);
674 dh = choose_dh(min, want, max);
676 buffer_put_char(m, 0);
679 /* Send first bignum */
680 buffer_put_char(m, 1);
681 buffer_put_bignum2(m, dh->p);
682 buffer_put_bignum2(m, dh->g);
686 mm_request_send(sock, MONITOR_ANS_MODULI, m);
690 extern AuthenticationConnection *auth_conn;
693 mm_answer_sign(int sock, Buffer *m)
698 u_int siglen, datlen;
701 debug3("%s", __func__);
703 keyid = buffer_get_int(m);
704 p = buffer_get_string(m, &datlen);
707 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
708 * SHA384 (48 bytes) and SHA512 (64 bytes).
710 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
711 fatal("%s: data length incorrect: %u", __func__, datlen);
713 /* save session id, it will be passed on the first call */
714 if (session_id2_len == 0) {
715 session_id2_len = datlen;
716 session_id2 = xmalloc(session_id2_len);
717 memcpy(session_id2, p, session_id2_len);
720 if ((key = get_hostkey_by_index(keyid)) != NULL) {
721 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
722 fatal("%s: key_sign failed", __func__);
723 } else if ((key = get_hostkey_public_by_index(keyid)) != NULL &&
725 if (ssh_agent_sign(auth_conn, key, &signature, &siglen, p,
727 fatal("%s: ssh_agent_sign failed", __func__);
729 fatal("%s: no hostkey from index %d", __func__, keyid);
731 debug3("%s: signature %p(%u)", __func__, signature, siglen);
734 buffer_put_string(m, signature, siglen);
739 mm_request_send(sock, MONITOR_ANS_SIGN, m);
741 /* Turn on permissions for getpwnam */
742 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
747 /* Retrieves the password entry and also checks if the user is permitted */
750 mm_answer_pwnamallow(int sock, Buffer *m)
753 struct passwd *pwent;
757 debug3("%s", __func__);
759 if (authctxt->attempt++ != 0)
760 fatal("%s: multiple attempts for getpwnam", __func__);
762 username = buffer_get_string(m, NULL);
764 pwent = getpwnamallow(username);
766 authctxt->user = xstrdup(username);
767 setproctitle("%s [priv]", pwent ? username : "unknown");
773 buffer_put_char(m, 0);
774 authctxt->pw = fakepw();
779 authctxt->pw = pwent;
782 buffer_put_char(m, 1);
783 buffer_put_string(m, pwent, sizeof(struct passwd));
784 buffer_put_cstring(m, pwent->pw_name);
785 buffer_put_cstring(m, "*");
786 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
787 buffer_put_cstring(m, pwent->pw_gecos);
789 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
790 buffer_put_cstring(m, pwent->pw_class);
792 buffer_put_cstring(m, pwent->pw_dir);
793 buffer_put_cstring(m, pwent->pw_shell);
796 buffer_put_string(m, &options, sizeof(options));
798 #define M_CP_STROPT(x) do { \
799 if (options.x != NULL) \
800 buffer_put_cstring(m, options.x); \
802 #define M_CP_STRARRAYOPT(x, nx) do { \
803 for (i = 0; i < options.nx; i++) \
804 buffer_put_cstring(m, options.x[i]); \
806 /* See comment in servconf.h */
807 COPY_MATCH_STRING_OPTS();
809 #undef M_CP_STRARRAYOPT
811 /* Create valid auth method lists */
812 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
814 * The monitor will continue long enough to let the child
815 * run to it's packet_disconnect(), but it must not allow any
816 * authentication to succeed.
818 debug("%s: no valid authentication method lists", __func__);
821 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
822 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
824 /* For SSHv1 allow authentication now */
826 monitor_permit_authentications(1);
828 /* Allow service/style information on the auth context */
829 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
830 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
834 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
840 int mm_answer_auth2_read_banner(int sock, Buffer *m)
845 banner = auth2_read_banner();
846 buffer_put_cstring(m, banner != NULL ? banner : "");
847 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
854 mm_answer_authserv(int sock, Buffer *m)
856 monitor_permit_authentications(1);
858 authctxt->service = buffer_get_string(m, NULL);
859 authctxt->style = buffer_get_string(m, NULL);
860 debug3("%s: service=%s, style=%s",
861 __func__, authctxt->service, authctxt->style);
863 if (strlen(authctxt->style) == 0) {
864 free(authctxt->style);
865 authctxt->style = NULL;
872 mm_answer_authpassword(int sock, Buffer *m)
874 static int call_count;
879 passwd = buffer_get_string(m, &plen);
880 /* Only authenticate if the context is valid */
881 authenticated = options.password_authentication &&
882 auth_password(authctxt, passwd);
883 memset(passwd, 0, strlen(passwd));
887 buffer_put_int(m, authenticated);
889 debug3("%s: sending result %d", __func__, authenticated);
890 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
893 if (plen == 0 && call_count == 1)
894 auth_method = "none";
896 auth_method = "password";
898 /* Causes monitor loop to terminate if authenticated */
899 return (authenticated);
904 mm_answer_bsdauthquery(int sock, Buffer *m)
906 char *name, *infotxt;
912 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
913 &prompts, &echo_on) < 0 ? 0 : 1;
916 buffer_put_int(m, success);
918 buffer_put_cstring(m, prompts[0]);
920 debug3("%s: sending challenge success: %u", __func__, success);
921 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
934 mm_answer_bsdauthrespond(int sock, Buffer *m)
939 if (authctxt->as == 0)
940 fatal("%s: no bsd auth session", __func__);
942 response = buffer_get_string(m, NULL);
943 authok = options.challenge_response_authentication &&
944 auth_userresponse(authctxt->as, response, 0);
946 debug3("%s: <%s> = <%d>", __func__, response, authok);
950 buffer_put_int(m, authok);
952 debug3("%s: sending authenticated: %d", __func__, authok);
953 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
956 auth_method = "keyboard-interactive";
957 auth_submethod = "bsdauth";
959 auth_method = "bsdauth";
961 return (authok != 0);
967 mm_answer_skeyquery(int sock, Buffer *m)
970 char challenge[1024];
973 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
974 sizeof(challenge)) < 0 ? 0 : 1;
977 buffer_put_int(m, success);
979 buffer_put_cstring(m, challenge);
981 debug3("%s: sending challenge success: %u", __func__, success);
982 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
988 mm_answer_skeyrespond(int sock, Buffer *m)
993 response = buffer_get_string(m, NULL);
995 authok = (options.challenge_response_authentication &&
997 skey_haskey(authctxt->pw->pw_name) == 0 &&
998 skey_passcheck(authctxt->pw->pw_name, response) != -1);
1003 buffer_put_int(m, authok);
1005 debug3("%s: sending authenticated: %d", __func__, authok);
1006 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
1008 auth_method = "skey";
1010 return (authok != 0);
1016 mm_answer_pam_start(int sock, Buffer *m)
1018 if (!options.use_pam)
1019 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1021 start_pam(authctxt);
1023 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1029 mm_answer_pam_account(int sock, Buffer *m)
1033 if (!options.use_pam)
1034 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1036 ret = do_pam_account();
1038 buffer_put_int(m, ret);
1039 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1041 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
1046 static void *sshpam_ctxt, *sshpam_authok;
1047 extern KbdintDevice sshpam_device;
1050 mm_answer_pam_init_ctx(int sock, Buffer *m)
1053 debug3("%s", __func__);
1054 authctxt->user = buffer_get_string(m, NULL);
1055 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1056 sshpam_authok = NULL;
1058 if (sshpam_ctxt != NULL) {
1059 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1060 buffer_put_int(m, 1);
1062 buffer_put_int(m, 0);
1064 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
1069 mm_answer_pam_query(int sock, Buffer *m)
1071 char *name = NULL, *info = NULL, **prompts = NULL;
1072 u_int i, num = 0, *echo_on = 0;
1075 debug3("%s", __func__);
1076 sshpam_authok = NULL;
1077 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1078 if (ret == 0 && num == 0)
1079 sshpam_authok = sshpam_ctxt;
1080 if (num > 1 || name == NULL || info == NULL)
1083 buffer_put_int(m, ret);
1084 buffer_put_cstring(m, name);
1086 buffer_put_cstring(m, info);
1088 buffer_put_int(m, num);
1089 for (i = 0; i < num; ++i) {
1090 buffer_put_cstring(m, prompts[i]);
1092 buffer_put_int(m, echo_on[i]);
1096 auth_method = "keyboard-interactive";
1097 auth_submethod = "pam";
1098 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
1103 mm_answer_pam_respond(int sock, Buffer *m)
1109 debug3("%s", __func__);
1110 sshpam_authok = NULL;
1111 num = buffer_get_int(m);
1113 resp = xcalloc(num, sizeof(char *));
1114 for (i = 0; i < num; ++i)
1115 resp[i] = buffer_get_string(m, NULL);
1116 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1117 for (i = 0; i < num; ++i)
1121 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1124 buffer_put_int(m, ret);
1125 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1126 auth_method = "keyboard-interactive";
1127 auth_submethod = "pam";
1129 sshpam_authok = sshpam_ctxt;
1134 mm_answer_pam_free_ctx(int sock, Buffer *m)
1137 debug3("%s", __func__);
1138 (sshpam_device.free_ctx)(sshpam_ctxt);
1140 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1141 auth_method = "keyboard-interactive";
1142 auth_submethod = "pam";
1143 return (sshpam_authok == sshpam_ctxt);
1148 mm_answer_keyallowed(int sock, Buffer *m)
1151 char *cuser, *chost;
1154 enum mm_keytype type = 0;
1157 debug3("%s entering", __func__);
1159 type = buffer_get_int(m);
1160 cuser = buffer_get_string(m, NULL);
1161 chost = buffer_get_string(m, NULL);
1162 blob = buffer_get_string(m, &bloblen);
1164 key = key_from_blob(blob, bloblen);
1166 if ((compat20 && type == MM_RSAHOSTKEY) ||
1167 (!compat20 && type != MM_RSAHOSTKEY))
1168 fatal("%s: key type and protocol mismatch", __func__);
1170 debug3("%s: key_from_blob: %p", __func__, key);
1172 if (key != NULL && authctxt->valid) {
1175 allowed = options.pubkey_authentication &&
1176 user_key_allowed(authctxt->pw, key);
1177 pubkey_auth_info(authctxt, key, NULL);
1178 auth_method = "publickey";
1179 if (options.pubkey_authentication && allowed != 1)
1180 auth_clear_options();
1183 allowed = options.hostbased_authentication &&
1184 hostbased_key_allowed(authctxt->pw,
1186 pubkey_auth_info(authctxt, key,
1187 "client user \"%.100s\", client host \"%.100s\"",
1189 auth_method = "hostbased";
1192 key->type = KEY_RSA1; /* XXX */
1193 allowed = options.rhosts_rsa_authentication &&
1194 auth_rhosts_rsa_key_allowed(authctxt->pw,
1196 if (options.rhosts_rsa_authentication && allowed != 1)
1197 auth_clear_options();
1198 auth_method = "rsa";
1201 fatal("%s: unknown key type %d", __func__, type);
1208 /* clear temporarily storage (used by verify) */
1209 monitor_reset_key_state();
1212 /* Save temporarily for comparison in verify */
1214 key_bloblen = bloblen;
1215 key_blobtype = type;
1216 hostbased_cuser = cuser;
1217 hostbased_chost = chost;
1219 /* Log failed attempt */
1220 auth_log(authctxt, 0, 0, auth_method, NULL);
1226 debug3("%s: key %p is %s",
1227 __func__, key, allowed ? "allowed" : "not allowed");
1230 buffer_put_int(m, allowed);
1231 buffer_put_int(m, forced_command != NULL);
1233 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1235 if (type == MM_RSAHOSTKEY)
1236 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1242 monitor_valid_userblob(u_char *data, u_int datalen)
1245 char *p, *userstyle;
1250 buffer_append(&b, data, datalen);
1252 if (datafellows & SSH_OLD_SESSIONID) {
1254 len = buffer_len(&b);
1255 if ((session_id2 == NULL) ||
1256 (len < session_id2_len) ||
1257 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1259 buffer_consume(&b, session_id2_len);
1261 p = buffer_get_string(&b, &len);
1262 if ((session_id2 == NULL) ||
1263 (len != session_id2_len) ||
1264 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1268 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1270 p = buffer_get_cstring(&b, NULL);
1271 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1272 authctxt->style ? ":" : "",
1273 authctxt->style ? authctxt->style : "");
1274 if (strcmp(userstyle, p) != 0) {
1275 logit("wrong user name passed to monitor: expected %s != %.100s",
1281 buffer_skip_string(&b);
1282 if (datafellows & SSH_BUG_PKAUTH) {
1283 if (!buffer_get_char(&b))
1286 p = buffer_get_cstring(&b, NULL);
1287 if (strcmp("publickey", p) != 0)
1290 if (!buffer_get_char(&b))
1292 buffer_skip_string(&b);
1294 buffer_skip_string(&b);
1295 if (buffer_len(&b) != 0)
1302 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1306 char *p, *userstyle;
1311 buffer_append(&b, data, datalen);
1313 p = buffer_get_string(&b, &len);
1314 if ((session_id2 == NULL) ||
1315 (len != session_id2_len) ||
1316 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1320 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1322 p = buffer_get_cstring(&b, NULL);
1323 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1324 authctxt->style ? ":" : "",
1325 authctxt->style ? authctxt->style : "");
1326 if (strcmp(userstyle, p) != 0) {
1327 logit("wrong user name passed to monitor: expected %s != %.100s",
1333 buffer_skip_string(&b); /* service */
1334 p = buffer_get_cstring(&b, NULL);
1335 if (strcmp(p, "hostbased") != 0)
1338 buffer_skip_string(&b); /* pkalg */
1339 buffer_skip_string(&b); /* pkblob */
1341 /* verify client host, strip trailing dot if necessary */
1342 p = buffer_get_string(&b, NULL);
1343 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1345 if (strcmp(p, chost) != 0)
1349 /* verify client user */
1350 p = buffer_get_string(&b, NULL);
1351 if (strcmp(p, cuser) != 0)
1355 if (buffer_len(&b) != 0)
1362 mm_answer_keyverify(int sock, Buffer *m)
1365 u_char *signature, *data, *blob;
1366 u_int signaturelen, datalen, bloblen;
1370 blob = buffer_get_string(m, &bloblen);
1371 signature = buffer_get_string(m, &signaturelen);
1372 data = buffer_get_string(m, &datalen);
1374 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1375 !monitor_allowed_key(blob, bloblen))
1376 fatal("%s: bad key, not previously allowed", __func__);
1378 key = key_from_blob(blob, bloblen);
1380 fatal("%s: bad public key blob", __func__);
1382 switch (key_blobtype) {
1384 valid_data = monitor_valid_userblob(data, datalen);
1387 valid_data = monitor_valid_hostbasedblob(data, datalen,
1388 hostbased_cuser, hostbased_chost);
1395 fatal("%s: bad signature data blob", __func__);
1397 verified = key_verify(key, signature, signaturelen, data, datalen);
1398 debug3("%s: key %p signature %s",
1399 __func__, key, (verified == 1) ? "verified" : "unverified");
1406 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1408 monitor_reset_key_state();
1411 buffer_put_int(m, verified);
1412 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1414 return (verified == 1);
1418 mm_record_login(Session *s, struct passwd *pw)
1421 struct sockaddr_storage from;
1424 * Get IP address of client. If the connection is not a socket, let
1425 * the address be 0.0.0.0.
1427 memset(&from, 0, sizeof(from));
1428 fromlen = sizeof(from);
1429 if (packet_connection_is_on_socket()) {
1430 if (getpeername(packet_get_connection_in(),
1431 (struct sockaddr *)&from, &fromlen) < 0) {
1432 debug("getpeername: %.100s", strerror(errno));
1436 /* Record that there was a login on that tty from the remote host. */
1437 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1438 get_remote_name_or_ip(utmp_len, options.use_dns),
1439 (struct sockaddr *)&from, fromlen);
1443 mm_session_close(Session *s)
1445 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1446 if (s->ttyfd != -1) {
1447 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1448 session_pty_cleanup2(s);
1450 session_unused(s->self);
1454 mm_answer_pty(int sock, Buffer *m)
1456 extern struct monitor *pmonitor;
1460 debug3("%s entering", __func__);
1466 s->authctxt = authctxt;
1467 s->pw = authctxt->pw;
1468 s->pid = pmonitor->m_pid;
1469 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1472 pty_setowner(authctxt->pw, s->tty);
1474 buffer_put_int(m, 1);
1475 buffer_put_cstring(m, s->tty);
1477 /* We need to trick ttyslot */
1478 if (dup2(s->ttyfd, 0) == -1)
1479 fatal("%s: dup2", __func__);
1481 mm_record_login(s, authctxt->pw);
1483 /* Now we can close the file descriptor again */
1486 /* send messages generated by record_login */
1487 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1488 buffer_clear(&loginmsg);
1490 mm_request_send(sock, MONITOR_ANS_PTY, m);
1492 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1493 mm_send_fd(sock, s->ttyfd) == -1)
1494 fatal("%s: send fds failed", __func__);
1496 /* make sure nothing uses fd 0 */
1497 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1498 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1500 error("%s: fd0 %d != 0", __func__, fd0);
1502 /* slave is not needed */
1504 s->ttyfd = s->ptyfd;
1505 /* no need to dup() because nobody closes ptyfd */
1506 s->ptymaster = s->ptyfd;
1508 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1514 mm_session_close(s);
1515 buffer_put_int(m, 0);
1516 mm_request_send(sock, MONITOR_ANS_PTY, m);
1521 mm_answer_pty_cleanup(int sock, Buffer *m)
1526 debug3("%s entering", __func__);
1528 tty = buffer_get_string(m, NULL);
1529 if ((s = session_by_tty(tty)) != NULL)
1530 mm_session_close(s);
1537 mm_answer_sesskey(int sock, Buffer *m)
1542 /* Turn off permissions */
1543 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1545 if ((p = BN_new()) == NULL)
1546 fatal("%s: BN_new", __func__);
1548 buffer_get_bignum2(m, p);
1550 rsafail = ssh1_session_key(p);
1553 buffer_put_int(m, rsafail);
1554 buffer_put_bignum2(m, p);
1558 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1560 /* Turn on permissions for sessid passing */
1561 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1567 mm_answer_sessid(int sock, Buffer *m)
1571 debug3("%s entering", __func__);
1573 if (buffer_len(m) != 16)
1574 fatal("%s: bad ssh1 session id", __func__);
1575 for (i = 0; i < 16; i++)
1576 session_id[i] = buffer_get_char(m);
1578 /* Turn on permissions for getpwnam */
1579 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1585 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1589 u_char *blob = NULL;
1593 debug3("%s entering", __func__);
1595 auth_method = "rsa";
1596 if (options.rsa_authentication && authctxt->valid) {
1597 if ((client_n = BN_new()) == NULL)
1598 fatal("%s: BN_new", __func__);
1599 buffer_get_bignum2(m, client_n);
1600 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1601 BN_clear_free(client_n);
1604 buffer_put_int(m, allowed);
1605 buffer_put_int(m, forced_command != NULL);
1607 /* clear temporarily storage (used by generate challenge) */
1608 monitor_reset_key_state();
1610 if (allowed && key != NULL) {
1611 key->type = KEY_RSA; /* cheat for key_to_blob */
1612 if (key_to_blob(key, &blob, &blen) == 0)
1613 fatal("%s: key_to_blob failed", __func__);
1614 buffer_put_string(m, blob, blen);
1616 /* Save temporarily for comparison in verify */
1619 key_blobtype = MM_RSAUSERKEY;
1624 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1626 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1627 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1632 mm_answer_rsa_challenge(int sock, Buffer *m)
1638 debug3("%s entering", __func__);
1640 if (!authctxt->valid)
1641 fatal("%s: authctxt not valid", __func__);
1642 blob = buffer_get_string(m, &blen);
1643 if (!monitor_allowed_key(blob, blen))
1644 fatal("%s: bad key, not previously allowed", __func__);
1645 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1646 fatal("%s: key type mismatch", __func__);
1647 if ((key = key_from_blob(blob, blen)) == NULL)
1648 fatal("%s: received bad key", __func__);
1649 if (key->type != KEY_RSA)
1650 fatal("%s: received bad key type %d", __func__, key->type);
1651 key->type = KEY_RSA1;
1653 BN_clear_free(ssh1_challenge);
1654 ssh1_challenge = auth_rsa_generate_challenge(key);
1657 buffer_put_bignum2(m, ssh1_challenge);
1659 debug3("%s sending reply", __func__);
1660 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1662 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1670 mm_answer_rsa_response(int sock, Buffer *m)
1673 u_char *blob, *response;
1677 debug3("%s entering", __func__);
1679 if (!authctxt->valid)
1680 fatal("%s: authctxt not valid", __func__);
1681 if (ssh1_challenge == NULL)
1682 fatal("%s: no ssh1_challenge", __func__);
1684 blob = buffer_get_string(m, &blen);
1685 if (!monitor_allowed_key(blob, blen))
1686 fatal("%s: bad key, not previously allowed", __func__);
1687 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1688 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1689 if ((key = key_from_blob(blob, blen)) == NULL)
1690 fatal("%s: received bad key", __func__);
1691 response = buffer_get_string(m, &len);
1693 fatal("%s: received bad response to challenge", __func__);
1694 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1700 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1703 BN_clear_free(ssh1_challenge);
1704 ssh1_challenge = NULL;
1705 monitor_reset_key_state();
1708 buffer_put_int(m, success);
1709 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1715 mm_answer_term(int sock, Buffer *req)
1717 extern struct monitor *pmonitor;
1720 debug3("%s: tearing down sessions", __func__);
1722 /* The child is terminating */
1723 session_destroy_all(&mm_session_close);
1726 if (options.use_pam)
1730 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1734 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1736 /* Terminate process */
1740 #ifdef SSH_AUDIT_EVENTS
1741 /* Report that an audit event occurred */
1743 mm_answer_audit_event(int socket, Buffer *m)
1745 ssh_audit_event_t event;
1747 debug3("%s entering", __func__);
1749 event = buffer_get_int(m);
1751 case SSH_AUTH_FAIL_PUBKEY:
1752 case SSH_AUTH_FAIL_HOSTBASED:
1753 case SSH_AUTH_FAIL_GSSAPI:
1754 case SSH_LOGIN_EXCEED_MAXTRIES:
1755 case SSH_LOGIN_ROOT_DENIED:
1756 case SSH_CONNECTION_CLOSE:
1757 case SSH_INVALID_USER:
1761 fatal("Audit event type %d not permitted", event);
1768 mm_answer_audit_command(int socket, Buffer *m)
1773 debug3("%s entering", __func__);
1774 cmd = buffer_get_string(m, &len);
1775 /* sanity check command, if so how? */
1776 audit_run_command(cmd);
1780 #endif /* SSH_AUDIT_EVENTS */
1783 monitor_apply_keystate(struct monitor *pmonitor)
1786 set_newkeys(MODE_IN);
1787 set_newkeys(MODE_OUT);
1789 packet_set_protocol_flags(child_state.ssh1protoflags);
1790 packet_set_encryption_key(child_state.ssh1key,
1791 child_state.ssh1keylen, child_state.ssh1cipher);
1792 free(child_state.ssh1key);
1795 /* for rc4 and other stateful ciphers */
1796 packet_set_keycontext(MODE_OUT, child_state.keyout);
1797 free(child_state.keyout);
1798 packet_set_keycontext(MODE_IN, child_state.keyin);
1799 free(child_state.keyin);
1802 packet_set_iv(MODE_OUT, child_state.ivout);
1803 free(child_state.ivout);
1804 packet_set_iv(MODE_IN, child_state.ivin);
1805 free(child_state.ivin);
1808 memcpy(&incoming_stream, &child_state.incoming,
1809 sizeof(incoming_stream));
1810 memcpy(&outgoing_stream, &child_state.outgoing,
1811 sizeof(outgoing_stream));
1813 /* Update with new address */
1814 if (options.compression)
1815 mm_init_compression(pmonitor->m_zlib);
1817 if (options.rekey_limit || options.rekey_interval)
1818 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
1819 (time_t)options.rekey_interval);
1821 /* Network I/O buffers */
1822 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1823 buffer_clear(packet_get_input());
1824 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
1825 memset(child_state.input, 0, child_state.ilen);
1826 free(child_state.input);
1828 buffer_clear(packet_get_output());
1829 buffer_append(packet_get_output(), child_state.output,
1831 memset(child_state.output, 0, child_state.olen);
1832 free(child_state.output);
1836 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
1840 mm_get_kex(Buffer *m)
1846 kex = xcalloc(1, sizeof(*kex));
1847 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1848 if (session_id2 == NULL ||
1849 kex->session_id_len != session_id2_len ||
1850 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
1851 fatal("mm_get_get: internal error: bad session id");
1852 kex->we_need = buffer_get_int(m);
1853 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1854 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1855 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1856 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1857 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1858 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1860 kex->hostkey_type = buffer_get_int(m);
1861 kex->kex_type = buffer_get_int(m);
1862 blob = buffer_get_string(m, &bloblen);
1863 buffer_init(&kex->my);
1864 buffer_append(&kex->my, blob, bloblen);
1866 blob = buffer_get_string(m, &bloblen);
1867 buffer_init(&kex->peer);
1868 buffer_append(&kex->peer, blob, bloblen);
1871 kex->flags = buffer_get_int(m);
1872 kex->client_version_string = buffer_get_string(m, NULL);
1873 kex->server_version_string = buffer_get_string(m, NULL);
1874 kex->load_host_public_key=&get_hostkey_public_by_type;
1875 kex->load_host_private_key=&get_hostkey_private_by_type;
1876 kex->host_key_index=&get_hostkey_index;
1877 kex->sign = sshd_hostkey_sign;
1882 /* This function requries careful sanity checking */
1885 mm_get_keystate(struct monitor *pmonitor)
1889 u_int bloblen, plen;
1890 u_int32_t seqnr, packets;
1891 u_int64_t blocks, bytes;
1893 debug3("%s: Waiting for new keys", __func__);
1896 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1898 child_state.ssh1protoflags = buffer_get_int(&m);
1899 child_state.ssh1cipher = buffer_get_int(&m);
1900 child_state.ssh1key = buffer_get_string(&m,
1901 &child_state.ssh1keylen);
1902 child_state.ivout = buffer_get_string(&m,
1903 &child_state.ivoutlen);
1904 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1907 /* Get the Kex for rekeying */
1908 *pmonitor->m_pkex = mm_get_kex(&m);
1911 blob = buffer_get_string(&m, &bloblen);
1912 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1915 debug3("%s: Waiting for second key", __func__);
1916 blob = buffer_get_string(&m, &bloblen);
1917 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1920 /* Now get sequence numbers for the packets */
1921 seqnr = buffer_get_int(&m);
1922 blocks = buffer_get_int64(&m);
1923 packets = buffer_get_int(&m);
1924 bytes = buffer_get_int64(&m);
1925 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
1926 seqnr = buffer_get_int(&m);
1927 blocks = buffer_get_int64(&m);
1928 packets = buffer_get_int(&m);
1929 bytes = buffer_get_int64(&m);
1930 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
1933 /* Get the key context */
1934 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1935 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1937 debug3("%s: Getting compression state", __func__);
1938 /* Get compression state */
1939 p = buffer_get_string(&m, &plen);
1940 if (plen != sizeof(child_state.outgoing))
1941 fatal("%s: bad request size", __func__);
1942 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1945 p = buffer_get_string(&m, &plen);
1946 if (plen != sizeof(child_state.incoming))
1947 fatal("%s: bad request size", __func__);
1948 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1951 /* Network I/O buffers */
1952 debug3("%s: Getting Network I/O buffers", __func__);
1953 child_state.input = buffer_get_string(&m, &child_state.ilen);
1954 child_state.output = buffer_get_string(&m, &child_state.olen);
1958 child_state.sent_bytes = buffer_get_int64(&m);
1959 child_state.recv_bytes = buffer_get_int64(&m);
1966 /* Allocation functions for zlib */
1968 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1970 size_t len = (size_t) size * ncount;
1973 if (len == 0 || ncount > SIZE_T_MAX / size)
1974 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1976 address = mm_malloc(mm, len);
1982 mm_zfree(struct mm_master *mm, void *address)
1984 mm_free(mm, address);
1988 mm_init_compression(struct mm_master *mm)
1990 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1991 outgoing_stream.zfree = (free_func)mm_zfree;
1992 outgoing_stream.opaque = mm;
1994 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1995 incoming_stream.zfree = (free_func)mm_zfree;
1996 incoming_stream.opaque = mm;
2001 #define FD_CLOSEONEXEC(x) do { \
2002 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
2003 fatal("fcntl(%d, F_SETFD)", x); \
2007 monitor_openfds(struct monitor *mon, int do_logfds)
2011 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
2012 fatal("%s: socketpair: %s", __func__, strerror(errno));
2013 FD_CLOSEONEXEC(pair[0]);
2014 FD_CLOSEONEXEC(pair[1]);
2015 mon->m_recvfd = pair[0];
2016 mon->m_sendfd = pair[1];
2019 if (pipe(pair) == -1)
2020 fatal("%s: pipe: %s", __func__, strerror(errno));
2021 FD_CLOSEONEXEC(pair[0]);
2022 FD_CLOSEONEXEC(pair[1]);
2023 mon->m_log_recvfd = pair[0];
2024 mon->m_log_sendfd = pair[1];
2026 mon->m_log_recvfd = mon->m_log_sendfd = -1;
2029 #define MM_MEMSIZE 65536
2034 struct monitor *mon;
2036 mon = xcalloc(1, sizeof(*mon));
2038 monitor_openfds(mon, 1);
2040 /* Used to share zlib space across processes */
2041 if (options.compression) {
2042 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
2043 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
2045 /* Compression needs to share state across borders */
2046 mm_init_compression(mon->m_zlib);
2053 monitor_reinit(struct monitor *mon)
2055 monitor_openfds(mon, 0);
2060 mm_answer_gss_setup_ctx(int sock, Buffer *m)
2066 goid.elements = buffer_get_string(m, &len);
2069 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
2071 free(goid.elements);
2074 buffer_put_int(m, major);
2076 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
2078 /* Now we have a context, enable the step */
2079 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
2085 mm_answer_gss_accept_ctx(int sock, Buffer *m)
2088 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
2089 OM_uint32 major, minor;
2090 OM_uint32 flags = 0; /* GSI needs this */
2093 in.value = buffer_get_string(m, &len);
2095 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
2099 buffer_put_int(m, major);
2100 buffer_put_string(m, out.value, out.length);
2101 buffer_put_int(m, flags);
2102 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
2104 gss_release_buffer(&minor, &out);
2106 if (major == GSS_S_COMPLETE) {
2107 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2108 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2109 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
2115 mm_answer_gss_checkmic(int sock, Buffer *m)
2117 gss_buffer_desc gssbuf, mic;
2121 gssbuf.value = buffer_get_string(m, &len);
2122 gssbuf.length = len;
2123 mic.value = buffer_get_string(m, &len);
2126 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
2132 buffer_put_int(m, ret);
2134 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
2136 if (!GSS_ERROR(ret))
2137 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2143 mm_answer_gss_userok(int sock, Buffer *m)
2147 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2150 buffer_put_int(m, authenticated);
2152 debug3("%s: sending result %d", __func__, authenticated);
2153 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
2155 auth_method = "gssapi-with-mic";
2157 /* Monitor loop will terminate if authenticated */
2158 return (authenticated);
2164 mm_answer_jpake_step1(int sock, Buffer *m)
2166 struct jpake_ctx *pctx;
2167 u_char *x3_proof, *x4_proof;
2168 u_int x3_proof_len, x4_proof_len;
2170 if (!options.zero_knowledge_password_authentication)
2171 fatal("zero_knowledge_password_authentication disabled");
2173 if (authctxt->jpake_ctx != NULL)
2174 fatal("%s: authctxt->jpake_ctx already set (%p)",
2175 __func__, authctxt->jpake_ctx);
2176 authctxt->jpake_ctx = pctx = jpake_new();
2178 jpake_step1(pctx->grp,
2179 &pctx->server_id, &pctx->server_id_len,
2180 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2181 &x3_proof, &x3_proof_len,
2182 &x4_proof, &x4_proof_len);
2184 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2188 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2189 buffer_put_bignum2(m, pctx->g_x3);
2190 buffer_put_bignum2(m, pctx->g_x4);
2191 buffer_put_string(m, x3_proof, x3_proof_len);
2192 buffer_put_string(m, x4_proof, x4_proof_len);
2194 debug3("%s: sending step1", __func__);
2195 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2197 bzero(x3_proof, x3_proof_len);
2198 bzero(x4_proof, x4_proof_len);
2202 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2203 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2209 mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2211 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2212 char *hash_scheme, *salt;
2215 fatal("%s: pctx == NULL", __func__);
2217 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2220 /* pctx->s is sensitive, not returned to slave */
2221 buffer_put_cstring(m, hash_scheme);
2222 buffer_put_cstring(m, salt);
2224 debug3("%s: sending pwdata", __func__);
2225 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2227 bzero(hash_scheme, strlen(hash_scheme));
2228 bzero(salt, strlen(salt));
2232 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2238 mm_answer_jpake_step2(int sock, Buffer *m)
2240 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2241 u_char *x1_proof, *x2_proof, *x4_s_proof;
2242 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2245 fatal("%s: pctx == NULL", __func__);
2247 if ((pctx->g_x1 = BN_new()) == NULL ||
2248 (pctx->g_x2 = BN_new()) == NULL)
2249 fatal("%s: BN_new", __func__);
2250 buffer_get_bignum2(m, pctx->g_x1);
2251 buffer_get_bignum2(m, pctx->g_x2);
2252 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2253 x1_proof = buffer_get_string(m, &x1_proof_len);
2254 x2_proof = buffer_get_string(m, &x2_proof_len);
2256 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2257 pctx->g_x1, pctx->g_x2, pctx->x4,
2258 pctx->client_id, pctx->client_id_len,
2259 pctx->server_id, pctx->server_id_len,
2260 x1_proof, x1_proof_len,
2261 x2_proof, x2_proof_len,
2263 &x4_s_proof, &x4_s_proof_len);
2265 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2267 bzero(x1_proof, x1_proof_len);
2268 bzero(x2_proof, x2_proof_len);
2274 buffer_put_bignum2(m, pctx->b);
2275 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2277 debug3("%s: sending step2", __func__);
2278 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2280 bzero(x4_s_proof, x4_s_proof_len);
2283 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2289 mm_answer_jpake_key_confirm(int sock, Buffer *m)
2291 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2293 u_int x2_s_proof_len;
2296 fatal("%s: pctx == NULL", __func__);
2298 if ((pctx->a = BN_new()) == NULL)
2299 fatal("%s: BN_new", __func__);
2300 buffer_get_bignum2(m, pctx->a);
2301 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2303 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2304 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2305 pctx->server_id, pctx->server_id_len,
2306 pctx->client_id, pctx->client_id_len,
2307 session_id2, session_id2_len,
2308 x2_s_proof, x2_s_proof_len,
2310 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2312 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2314 bzero(x2_s_proof, x2_s_proof_len);
2317 /* pctx->k is sensitive, not sent */
2318 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2320 debug3("%s: sending confirmation hash", __func__);
2321 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2323 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2329 mm_answer_jpake_check_confirm(int sock, Buffer *m)
2331 int authenticated = 0;
2332 u_char *peer_confirm_hash;
2333 u_int peer_confirm_hash_len;
2334 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2337 fatal("%s: pctx == NULL", __func__);
2339 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2341 authenticated = jpake_check_confirm(pctx->k,
2342 pctx->client_id, pctx->client_id_len,
2343 session_id2, session_id2_len,
2344 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2346 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2348 bzero(peer_confirm_hash, peer_confirm_hash_len);
2349 free(peer_confirm_hash);
2352 buffer_put_int(m, authenticated);
2354 debug3("%s: sending result %d", __func__, authenticated);
2355 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2357 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2359 auth_method = "jpake-01@openssh.com";
2360 return authenticated;