1 /* $OpenBSD: monitor.c,v 1.131 2014/02/02 03:44:31 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.
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include "openbsd-compat/sys-tree.h"
51 # ifdef HAVE_SYS_POLL_H
52 # include <sys/poll.h>
60 #include <openssl/dh.h>
62 #include "openbsd-compat/sys-queue.h"
73 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
76 #define TARGET_OS_MAC 1
81 #include "auth-options.h"
90 #include "monitor_mm.h"
94 #include "monitor_wrap.h"
95 #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 *);
166 int mm_answer_pam_start(int, Buffer *);
167 int mm_answer_pam_account(int, Buffer *);
168 int mm_answer_pam_init_ctx(int, Buffer *);
169 int mm_answer_pam_query(int, Buffer *);
170 int mm_answer_pam_respond(int, Buffer *);
171 int mm_answer_pam_free_ctx(int, Buffer *);
175 int mm_answer_gss_setup_ctx(int, Buffer *);
176 int mm_answer_gss_accept_ctx(int, Buffer *);
177 int mm_answer_gss_userok(int, Buffer *);
178 int mm_answer_gss_checkmic(int, Buffer *);
181 #ifdef SSH_AUDIT_EVENTS
182 int mm_answer_audit_event(int, Buffer *);
183 int mm_answer_audit_command(int, Buffer *);
186 static int monitor_read_log(struct monitor *);
188 static Authctxt *authctxt;
189 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
191 /* local state for key verify */
192 static u_char *key_blob = NULL;
193 static u_int key_bloblen = 0;
194 static int key_blobtype = MM_NOKEY;
195 static char *hostbased_cuser = NULL;
196 static char *hostbased_chost = NULL;
197 static char *auth_method = "unknown";
198 static char *auth_submethod = NULL;
199 static u_int session_id2_len = 0;
200 static u_char *session_id2 = NULL;
201 static pid_t monitor_child_pid;
204 enum monitor_reqtype type;
206 int (*f)(int, Buffer *);
209 #define MON_ISAUTH 0x0004 /* Required for Authentication */
210 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
211 #define MON_ONCE 0x0010 /* Disable after calling */
212 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
214 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
216 #define MON_PERMIT 0x1000 /* Request is permitted */
218 struct mon_table mon_dispatch_proto20[] = {
219 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
220 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
221 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
222 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
223 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
224 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
226 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
227 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
228 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
229 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
230 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
231 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
233 #ifdef SSH_AUDIT_EVENTS
234 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
237 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
238 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
241 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
242 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
244 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
245 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
247 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
248 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
249 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
250 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
255 struct mon_table mon_dispatch_postauth20[] = {
256 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
257 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
258 {MONITOR_REQ_PTY, 0, mm_answer_pty},
259 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
260 {MONITOR_REQ_TERM, 0, mm_answer_term},
261 #ifdef SSH_AUDIT_EVENTS
262 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
263 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
268 struct mon_table mon_dispatch_proto15[] = {
269 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
270 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
271 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
272 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
273 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
274 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
275 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
276 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
278 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
279 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
282 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
283 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
286 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
287 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
288 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
289 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
290 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
291 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
293 #ifdef SSH_AUDIT_EVENTS
294 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
299 struct mon_table mon_dispatch_postauth15[] = {
300 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
301 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
302 {MONITOR_REQ_TERM, 0, mm_answer_term},
303 #ifdef SSH_AUDIT_EVENTS
304 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
305 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
310 struct mon_table *mon_dispatch;
312 /* Specifies if a certain message is allowed at the moment */
315 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
317 while (ent->f != NULL) {
318 if (ent->type == type) {
319 ent->flags &= ~MON_PERMIT;
320 ent->flags |= permit ? MON_PERMIT : 0;
328 monitor_permit_authentications(int permit)
330 struct mon_table *ent = mon_dispatch;
332 while (ent->f != NULL) {
333 if (ent->flags & MON_AUTH) {
334 ent->flags &= ~MON_PERMIT;
335 ent->flags |= permit ? MON_PERMIT : 0;
342 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
344 struct mon_table *ent;
345 int authenticated = 0, partial = 0;
347 debug3("preauth child monitor started");
349 close(pmonitor->m_recvfd);
350 close(pmonitor->m_log_sendfd);
351 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
353 authctxt = _authctxt;
354 memset(authctxt, 0, sizeof(*authctxt));
356 authctxt->loginmsg = &loginmsg;
359 mon_dispatch = mon_dispatch_proto20;
361 /* Permit requests for moduli and signatures */
362 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
363 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
365 mon_dispatch = mon_dispatch_proto15;
367 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
370 /* The first few requests do not require asynchronous access */
371 while (!authenticated) {
373 auth_method = "unknown";
374 auth_submethod = NULL;
375 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
377 /* Special handling for multiple required authentications */
378 if (options.num_auth_methods != 0) {
380 fatal("AuthenticationMethods is not supported"
381 "with SSH protocol 1");
383 !auth2_update_methods_lists(authctxt,
384 auth_method, auth_submethod)) {
385 debug3("%s: method %s: partial", __func__,
393 if (!(ent->flags & MON_AUTHDECIDE))
394 fatal("%s: unexpected authentication from %d",
395 __func__, ent->type);
396 if (authctxt->pw->pw_uid == 0 &&
397 !auth_root_allowed(auth_method))
400 /* PAM needs to perform account checks after auth */
401 if (options.use_pam && authenticated) {
405 mm_request_receive_expect(pmonitor->m_sendfd,
406 MONITOR_REQ_PAM_ACCOUNT, &m);
407 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
412 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
413 auth_log(authctxt, authenticated, partial,
414 auth_method, auth_submethod);
416 authctxt->failures++;
420 if (!authctxt->valid)
421 fatal("%s: authenticated invalid user", __func__);
422 if (strcmp(auth_method, "unknown") == 0)
423 fatal("%s: authentication method name unknown", __func__);
425 debug("%s: %s has been authenticated by privileged process",
426 __func__, authctxt->user);
428 mm_get_keystate(pmonitor);
430 /* Drain any buffered messages from the child */
431 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
434 close(pmonitor->m_sendfd);
435 close(pmonitor->m_log_recvfd);
436 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
440 monitor_set_child_handler(pid_t pid)
442 monitor_child_pid = pid;
446 monitor_child_handler(int sig)
448 kill(monitor_child_pid, sig);
452 monitor_child_postauth(struct monitor *pmonitor)
454 close(pmonitor->m_recvfd);
455 pmonitor->m_recvfd = -1;
457 monitor_set_child_handler(pmonitor->m_pid);
458 signal(SIGHUP, &monitor_child_handler);
459 signal(SIGTERM, &monitor_child_handler);
460 signal(SIGINT, &monitor_child_handler);
463 mon_dispatch = mon_dispatch_postauth20;
465 /* Permit requests for moduli and signatures */
466 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
467 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
468 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
470 mon_dispatch = mon_dispatch_postauth15;
471 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
474 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
475 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
479 monitor_read(pmonitor, mon_dispatch, NULL);
483 monitor_sync(struct monitor *pmonitor)
485 if (options.compression) {
486 /* The member allocation is not visible, so sync it */
487 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
492 monitor_read_log(struct monitor *pmonitor)
498 buffer_init(&logmsg);
501 buffer_append_space(&logmsg, 4);
502 if (atomicio(read, pmonitor->m_log_recvfd,
503 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
504 if (errno == EPIPE) {
505 buffer_free(&logmsg);
506 debug("%s: child log fd closed", __func__);
507 close(pmonitor->m_log_recvfd);
508 pmonitor->m_log_recvfd = -1;
511 fatal("%s: log fd read: %s", __func__, strerror(errno));
513 len = buffer_get_int(&logmsg);
514 if (len <= 4 || len > 8192)
515 fatal("%s: invalid log message length %u", __func__, len);
517 /* Read severity, message */
518 buffer_clear(&logmsg);
519 buffer_append_space(&logmsg, len);
520 if (atomicio(read, pmonitor->m_log_recvfd,
521 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
522 fatal("%s: log fd read: %s", __func__, strerror(errno));
525 level = buffer_get_int(&logmsg);
526 msg = buffer_get_string(&logmsg, NULL);
527 if (log_level_name(level) == NULL)
528 fatal("%s: invalid log level %u (corrupted message?)",
530 do_log2(level, "%s [preauth]", msg);
532 buffer_free(&logmsg);
539 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
540 struct mon_table **pent)
545 struct pollfd pfd[2];
548 memset(&pfd, 0, sizeof(pfd));
549 pfd[0].fd = pmonitor->m_sendfd;
550 pfd[0].events = POLLIN;
551 pfd[1].fd = pmonitor->m_log_recvfd;
552 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
553 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
554 if (errno == EINTR || errno == EAGAIN)
556 fatal("%s: poll: %s", __func__, strerror(errno));
558 if (pfd[1].revents) {
560 * Drain all log messages before processing next
563 monitor_read_log(pmonitor);
567 break; /* Continues below */
572 mm_request_receive(pmonitor->m_sendfd, &m);
573 type = buffer_get_char(&m);
575 debug3("%s: checking request %d", __func__, type);
577 while (ent->f != NULL) {
578 if (ent->type == type)
583 if (ent->f != NULL) {
584 if (!(ent->flags & MON_PERMIT))
585 fatal("%s: unpermitted request %d", __func__,
587 ret = (*ent->f)(pmonitor->m_sendfd, &m);
590 /* The child may use this request only once, disable it */
591 if (ent->flags & MON_ONCE) {
592 debug2("%s: %d used once, disabling now", __func__,
594 ent->flags &= ~MON_PERMIT;
603 fatal("%s: unsupported request: %d", __func__, type);
609 /* allowed key state */
611 monitor_allowed_key(u_char *blob, u_int bloblen)
613 /* make sure key is allowed */
614 if (key_blob == NULL || key_bloblen != bloblen ||
615 timingsafe_bcmp(key_blob, blob, key_bloblen))
621 monitor_reset_key_state(void)
625 free(hostbased_cuser);
626 free(hostbased_chost);
629 key_blobtype = MM_NOKEY;
630 hostbased_cuser = NULL;
631 hostbased_chost = NULL;
635 mm_answer_moduli(int sock, Buffer *m)
640 min = buffer_get_int(m);
641 want = buffer_get_int(m);
642 max = buffer_get_int(m);
644 debug3("%s: got parameters: %d %d %d",
645 __func__, min, want, max);
646 /* We need to check here, too, in case the child got corrupted */
647 if (max < min || want < min || max < want)
648 fatal("%s: bad parameters: %d %d %d",
649 __func__, min, want, max);
653 dh = choose_dh(min, want, max);
655 buffer_put_char(m, 0);
658 /* Send first bignum */
659 buffer_put_char(m, 1);
660 buffer_put_bignum2(m, dh->p);
661 buffer_put_bignum2(m, dh->g);
665 mm_request_send(sock, MONITOR_ANS_MODULI, m);
669 extern AuthenticationConnection *auth_conn;
672 mm_answer_sign(int sock, Buffer *m)
677 u_int siglen, datlen;
680 debug3("%s", __func__);
682 keyid = buffer_get_int(m);
683 p = buffer_get_string(m, &datlen);
686 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
687 * SHA384 (48 bytes) and SHA512 (64 bytes).
689 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
690 fatal("%s: data length incorrect: %u", __func__, datlen);
692 /* save session id, it will be passed on the first call */
693 if (session_id2_len == 0) {
694 session_id2_len = datlen;
695 session_id2 = xmalloc(session_id2_len);
696 memcpy(session_id2, p, session_id2_len);
699 if ((key = get_hostkey_by_index(keyid)) != NULL) {
700 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
701 fatal("%s: key_sign failed", __func__);
702 } else if ((key = get_hostkey_public_by_index(keyid)) != NULL &&
704 if (ssh_agent_sign(auth_conn, key, &signature, &siglen, p,
706 fatal("%s: ssh_agent_sign failed", __func__);
708 fatal("%s: no hostkey from index %d", __func__, keyid);
710 debug3("%s: signature %p(%u)", __func__, signature, siglen);
713 buffer_put_string(m, signature, siglen);
718 mm_request_send(sock, MONITOR_ANS_SIGN, m);
720 /* Turn on permissions for getpwnam */
721 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
726 /* Retrieves the password entry and also checks if the user is permitted */
729 mm_answer_pwnamallow(int sock, Buffer *m)
732 struct passwd *pwent;
736 debug3("%s", __func__);
738 if (authctxt->attempt++ != 0)
739 fatal("%s: multiple attempts for getpwnam", __func__);
741 username = buffer_get_string(m, NULL);
743 pwent = getpwnamallow(username);
745 authctxt->user = xstrdup(username);
746 setproctitle("%s [priv]", pwent ? username : "unknown");
752 buffer_put_char(m, 0);
753 authctxt->pw = fakepw();
758 authctxt->pw = pwent;
761 buffer_put_char(m, 1);
762 buffer_put_string(m, pwent, sizeof(struct passwd));
763 buffer_put_cstring(m, pwent->pw_name);
764 buffer_put_cstring(m, "*");
765 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
766 buffer_put_cstring(m, pwent->pw_gecos);
768 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
769 buffer_put_cstring(m, pwent->pw_class);
771 buffer_put_cstring(m, pwent->pw_dir);
772 buffer_put_cstring(m, pwent->pw_shell);
775 buffer_put_string(m, &options, sizeof(options));
777 #define M_CP_STROPT(x) do { \
778 if (options.x != NULL) \
779 buffer_put_cstring(m, options.x); \
781 #define M_CP_STRARRAYOPT(x, nx) do { \
782 for (i = 0; i < options.nx; i++) \
783 buffer_put_cstring(m, options.x[i]); \
785 /* See comment in servconf.h */
786 COPY_MATCH_STRING_OPTS();
788 #undef M_CP_STRARRAYOPT
790 /* Create valid auth method lists */
791 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
793 * The monitor will continue long enough to let the child
794 * run to it's packet_disconnect(), but it must not allow any
795 * authentication to succeed.
797 debug("%s: no valid authentication method lists", __func__);
800 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
801 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
803 /* For SSHv1 allow authentication now */
805 monitor_permit_authentications(1);
807 /* Allow service/style information on the auth context */
808 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
809 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
813 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
819 int mm_answer_auth2_read_banner(int sock, Buffer *m)
824 banner = auth2_read_banner();
825 buffer_put_cstring(m, banner != NULL ? banner : "");
826 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
833 mm_answer_authserv(int sock, Buffer *m)
835 monitor_permit_authentications(1);
837 authctxt->service = buffer_get_string(m, NULL);
838 authctxt->style = buffer_get_string(m, NULL);
839 debug3("%s: service=%s, style=%s",
840 __func__, authctxt->service, authctxt->style);
842 if (strlen(authctxt->style) == 0) {
843 free(authctxt->style);
844 authctxt->style = NULL;
851 mm_answer_authpassword(int sock, Buffer *m)
853 static int call_count;
858 passwd = buffer_get_string(m, &plen);
859 /* Only authenticate if the context is valid */
860 authenticated = options.password_authentication &&
861 auth_password(authctxt, passwd);
862 explicit_bzero(passwd, strlen(passwd));
866 buffer_put_int(m, authenticated);
868 debug3("%s: sending result %d", __func__, authenticated);
869 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
872 if (plen == 0 && call_count == 1)
873 auth_method = "none";
875 auth_method = "password";
877 /* Causes monitor loop to terminate if authenticated */
878 return (authenticated);
883 mm_answer_bsdauthquery(int sock, Buffer *m)
885 char *name, *infotxt;
891 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
892 &prompts, &echo_on) < 0 ? 0 : 1;
895 buffer_put_int(m, success);
897 buffer_put_cstring(m, prompts[0]);
899 debug3("%s: sending challenge success: %u", __func__, success);
900 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
913 mm_answer_bsdauthrespond(int sock, Buffer *m)
918 if (authctxt->as == 0)
919 fatal("%s: no bsd auth session", __func__);
921 response = buffer_get_string(m, NULL);
922 authok = options.challenge_response_authentication &&
923 auth_userresponse(authctxt->as, response, 0);
925 debug3("%s: <%s> = <%d>", __func__, response, authok);
929 buffer_put_int(m, authok);
931 debug3("%s: sending authenticated: %d", __func__, authok);
932 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
935 auth_method = "keyboard-interactive";
936 auth_submethod = "bsdauth";
938 auth_method = "bsdauth";
940 return (authok != 0);
946 mm_answer_skeyquery(int sock, Buffer *m)
949 char challenge[1024];
952 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
953 sizeof(challenge)) < 0 ? 0 : 1;
956 buffer_put_int(m, success);
958 buffer_put_cstring(m, challenge);
960 debug3("%s: sending challenge success: %u", __func__, success);
961 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
967 mm_answer_skeyrespond(int sock, Buffer *m)
972 response = buffer_get_string(m, NULL);
974 authok = (options.challenge_response_authentication &&
976 skey_haskey(authctxt->pw->pw_name) == 0 &&
977 skey_passcheck(authctxt->pw->pw_name, response) != -1);
982 buffer_put_int(m, authok);
984 debug3("%s: sending authenticated: %d", __func__, authok);
985 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
987 auth_method = "skey";
989 return (authok != 0);
995 mm_answer_pam_start(int sock, Buffer *m)
997 if (!options.use_pam)
998 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1000 start_pam(authctxt);
1002 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1008 mm_answer_pam_account(int sock, Buffer *m)
1012 if (!options.use_pam)
1013 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1015 ret = do_pam_account();
1017 buffer_put_int(m, ret);
1018 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1020 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
1025 static void *sshpam_ctxt, *sshpam_authok;
1026 extern KbdintDevice sshpam_device;
1029 mm_answer_pam_init_ctx(int sock, Buffer *m)
1031 debug3("%s", __func__);
1032 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1033 sshpam_authok = NULL;
1035 if (sshpam_ctxt != NULL) {
1036 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1037 buffer_put_int(m, 1);
1039 buffer_put_int(m, 0);
1041 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
1046 mm_answer_pam_query(int sock, Buffer *m)
1048 char *name = NULL, *info = NULL, **prompts = NULL;
1049 u_int i, num = 0, *echo_on = 0;
1052 debug3("%s", __func__);
1053 sshpam_authok = NULL;
1054 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1055 if (ret == 0 && num == 0)
1056 sshpam_authok = sshpam_ctxt;
1057 if (num > 1 || name == NULL || info == NULL)
1060 buffer_put_int(m, ret);
1061 buffer_put_cstring(m, name);
1063 buffer_put_cstring(m, info);
1065 buffer_put_int(m, num);
1066 for (i = 0; i < num; ++i) {
1067 buffer_put_cstring(m, prompts[i]);
1069 buffer_put_int(m, echo_on[i]);
1073 auth_method = "keyboard-interactive";
1074 auth_submethod = "pam";
1075 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
1080 mm_answer_pam_respond(int sock, Buffer *m)
1086 debug3("%s", __func__);
1087 sshpam_authok = NULL;
1088 num = buffer_get_int(m);
1090 resp = xcalloc(num, sizeof(char *));
1091 for (i = 0; i < num; ++i)
1092 resp[i] = buffer_get_string(m, NULL);
1093 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1094 for (i = 0; i < num; ++i)
1098 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1101 buffer_put_int(m, ret);
1102 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1103 auth_method = "keyboard-interactive";
1104 auth_submethod = "pam";
1106 sshpam_authok = sshpam_ctxt;
1111 mm_answer_pam_free_ctx(int sock, Buffer *m)
1113 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
1115 debug3("%s", __func__);
1116 (sshpam_device.free_ctx)(sshpam_ctxt);
1117 sshpam_ctxt = sshpam_authok = NULL;
1119 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1120 auth_method = "keyboard-interactive";
1121 auth_submethod = "pam";
1127 mm_answer_keyallowed(int sock, Buffer *m)
1130 char *cuser, *chost;
1133 enum mm_keytype type = 0;
1136 debug3("%s entering", __func__);
1138 type = buffer_get_int(m);
1139 cuser = buffer_get_string(m, NULL);
1140 chost = buffer_get_string(m, NULL);
1141 blob = buffer_get_string(m, &bloblen);
1143 key = key_from_blob(blob, bloblen);
1145 if ((compat20 && type == MM_RSAHOSTKEY) ||
1146 (!compat20 && type != MM_RSAHOSTKEY))
1147 fatal("%s: key type and protocol mismatch", __func__);
1149 debug3("%s: key_from_blob: %p", __func__, key);
1151 if (key != NULL && authctxt->valid) {
1154 allowed = options.pubkey_authentication &&
1155 user_key_allowed(authctxt->pw, key);
1156 pubkey_auth_info(authctxt, key, NULL);
1157 auth_method = "publickey";
1158 if (options.pubkey_authentication && allowed != 1)
1159 auth_clear_options();
1162 allowed = options.hostbased_authentication &&
1163 hostbased_key_allowed(authctxt->pw,
1165 pubkey_auth_info(authctxt, key,
1166 "client user \"%.100s\", client host \"%.100s\"",
1168 auth_method = "hostbased";
1171 key->type = KEY_RSA1; /* XXX */
1172 allowed = options.rhosts_rsa_authentication &&
1173 auth_rhosts_rsa_key_allowed(authctxt->pw,
1175 if (options.rhosts_rsa_authentication && allowed != 1)
1176 auth_clear_options();
1177 auth_method = "rsa";
1180 fatal("%s: unknown key type %d", __func__, type);
1187 /* clear temporarily storage (used by verify) */
1188 monitor_reset_key_state();
1191 /* Save temporarily for comparison in verify */
1193 key_bloblen = bloblen;
1194 key_blobtype = type;
1195 hostbased_cuser = cuser;
1196 hostbased_chost = chost;
1198 /* Log failed attempt */
1199 auth_log(authctxt, 0, 0, auth_method, NULL);
1205 debug3("%s: key %p is %s",
1206 __func__, key, allowed ? "allowed" : "not allowed");
1209 buffer_put_int(m, allowed);
1210 buffer_put_int(m, forced_command != NULL);
1212 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1214 if (type == MM_RSAHOSTKEY)
1215 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1221 monitor_valid_userblob(u_char *data, u_int datalen)
1224 char *p, *userstyle;
1229 buffer_append(&b, data, datalen);
1231 if (datafellows & SSH_OLD_SESSIONID) {
1233 len = buffer_len(&b);
1234 if ((session_id2 == NULL) ||
1235 (len < session_id2_len) ||
1236 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1238 buffer_consume(&b, session_id2_len);
1240 p = buffer_get_string(&b, &len);
1241 if ((session_id2 == NULL) ||
1242 (len != session_id2_len) ||
1243 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1247 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1249 p = buffer_get_cstring(&b, NULL);
1250 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1251 authctxt->style ? ":" : "",
1252 authctxt->style ? authctxt->style : "");
1253 if (strcmp(userstyle, p) != 0) {
1254 logit("wrong user name passed to monitor: expected %s != %.100s",
1260 buffer_skip_string(&b);
1261 if (datafellows & SSH_BUG_PKAUTH) {
1262 if (!buffer_get_char(&b))
1265 p = buffer_get_cstring(&b, NULL);
1266 if (strcmp("publickey", p) != 0)
1269 if (!buffer_get_char(&b))
1271 buffer_skip_string(&b);
1273 buffer_skip_string(&b);
1274 if (buffer_len(&b) != 0)
1281 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1285 char *p, *userstyle;
1290 buffer_append(&b, data, datalen);
1292 p = buffer_get_string(&b, &len);
1293 if ((session_id2 == NULL) ||
1294 (len != session_id2_len) ||
1295 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1299 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1301 p = buffer_get_cstring(&b, NULL);
1302 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1303 authctxt->style ? ":" : "",
1304 authctxt->style ? authctxt->style : "");
1305 if (strcmp(userstyle, p) != 0) {
1306 logit("wrong user name passed to monitor: expected %s != %.100s",
1312 buffer_skip_string(&b); /* service */
1313 p = buffer_get_cstring(&b, NULL);
1314 if (strcmp(p, "hostbased") != 0)
1317 buffer_skip_string(&b); /* pkalg */
1318 buffer_skip_string(&b); /* pkblob */
1320 /* verify client host, strip trailing dot if necessary */
1321 p = buffer_get_string(&b, NULL);
1322 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1324 if (strcmp(p, chost) != 0)
1328 /* verify client user */
1329 p = buffer_get_string(&b, NULL);
1330 if (strcmp(p, cuser) != 0)
1334 if (buffer_len(&b) != 0)
1341 mm_answer_keyverify(int sock, Buffer *m)
1344 u_char *signature, *data, *blob;
1345 u_int signaturelen, datalen, bloblen;
1349 blob = buffer_get_string(m, &bloblen);
1350 signature = buffer_get_string(m, &signaturelen);
1351 data = buffer_get_string(m, &datalen);
1353 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1354 !monitor_allowed_key(blob, bloblen))
1355 fatal("%s: bad key, not previously allowed", __func__);
1357 key = key_from_blob(blob, bloblen);
1359 fatal("%s: bad public key blob", __func__);
1361 switch (key_blobtype) {
1363 valid_data = monitor_valid_userblob(data, datalen);
1366 valid_data = monitor_valid_hostbasedblob(data, datalen,
1367 hostbased_cuser, hostbased_chost);
1374 fatal("%s: bad signature data blob", __func__);
1376 verified = key_verify(key, signature, signaturelen, data, datalen);
1377 debug3("%s: key %p signature %s",
1378 __func__, key, (verified == 1) ? "verified" : "unverified");
1385 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1387 monitor_reset_key_state();
1390 buffer_put_int(m, verified);
1391 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1393 return (verified == 1);
1397 mm_record_login(Session *s, struct passwd *pw)
1400 struct sockaddr_storage from;
1403 * Get IP address of client. If the connection is not a socket, let
1404 * the address be 0.0.0.0.
1406 memset(&from, 0, sizeof(from));
1407 fromlen = sizeof(from);
1408 if (packet_connection_is_on_socket()) {
1409 if (getpeername(packet_get_connection_in(),
1410 (struct sockaddr *)&from, &fromlen) < 0) {
1411 debug("getpeername: %.100s", strerror(errno));
1415 /* Record that there was a login on that tty from the remote host. */
1416 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1417 get_remote_name_or_ip(utmp_len, options.use_dns),
1418 (struct sockaddr *)&from, fromlen);
1422 mm_session_close(Session *s)
1424 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1425 if (s->ttyfd != -1) {
1426 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1427 session_pty_cleanup2(s);
1429 session_unused(s->self);
1433 mm_answer_pty(int sock, Buffer *m)
1435 extern struct monitor *pmonitor;
1439 debug3("%s entering", __func__);
1445 s->authctxt = authctxt;
1446 s->pw = authctxt->pw;
1447 s->pid = pmonitor->m_pid;
1448 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1451 pty_setowner(authctxt->pw, s->tty);
1453 buffer_put_int(m, 1);
1454 buffer_put_cstring(m, s->tty);
1456 /* We need to trick ttyslot */
1457 if (dup2(s->ttyfd, 0) == -1)
1458 fatal("%s: dup2", __func__);
1460 mm_record_login(s, authctxt->pw);
1462 /* Now we can close the file descriptor again */
1465 /* send messages generated by record_login */
1466 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1467 buffer_clear(&loginmsg);
1469 mm_request_send(sock, MONITOR_ANS_PTY, m);
1471 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1472 mm_send_fd(sock, s->ttyfd) == -1)
1473 fatal("%s: send fds failed", __func__);
1475 /* make sure nothing uses fd 0 */
1476 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1477 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1479 error("%s: fd0 %d != 0", __func__, fd0);
1481 /* slave is not needed */
1483 s->ttyfd = s->ptyfd;
1484 /* no need to dup() because nobody closes ptyfd */
1485 s->ptymaster = s->ptyfd;
1487 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1493 mm_session_close(s);
1494 buffer_put_int(m, 0);
1495 mm_request_send(sock, MONITOR_ANS_PTY, m);
1500 mm_answer_pty_cleanup(int sock, Buffer *m)
1505 debug3("%s entering", __func__);
1507 tty = buffer_get_string(m, NULL);
1508 if ((s = session_by_tty(tty)) != NULL)
1509 mm_session_close(s);
1516 mm_answer_sesskey(int sock, Buffer *m)
1521 /* Turn off permissions */
1522 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1524 if ((p = BN_new()) == NULL)
1525 fatal("%s: BN_new", __func__);
1527 buffer_get_bignum2(m, p);
1529 rsafail = ssh1_session_key(p);
1532 buffer_put_int(m, rsafail);
1533 buffer_put_bignum2(m, p);
1537 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1539 /* Turn on permissions for sessid passing */
1540 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1546 mm_answer_sessid(int sock, Buffer *m)
1550 debug3("%s entering", __func__);
1552 if (buffer_len(m) != 16)
1553 fatal("%s: bad ssh1 session id", __func__);
1554 for (i = 0; i < 16; i++)
1555 session_id[i] = buffer_get_char(m);
1557 /* Turn on permissions for getpwnam */
1558 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1564 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1568 u_char *blob = NULL;
1572 debug3("%s entering", __func__);
1574 auth_method = "rsa";
1575 if (options.rsa_authentication && authctxt->valid) {
1576 if ((client_n = BN_new()) == NULL)
1577 fatal("%s: BN_new", __func__);
1578 buffer_get_bignum2(m, client_n);
1579 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1580 BN_clear_free(client_n);
1583 buffer_put_int(m, allowed);
1584 buffer_put_int(m, forced_command != NULL);
1586 /* clear temporarily storage (used by generate challenge) */
1587 monitor_reset_key_state();
1589 if (allowed && key != NULL) {
1590 key->type = KEY_RSA; /* cheat for key_to_blob */
1591 if (key_to_blob(key, &blob, &blen) == 0)
1592 fatal("%s: key_to_blob failed", __func__);
1593 buffer_put_string(m, blob, blen);
1595 /* Save temporarily for comparison in verify */
1598 key_blobtype = MM_RSAUSERKEY;
1603 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1605 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1606 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1611 mm_answer_rsa_challenge(int sock, Buffer *m)
1617 debug3("%s entering", __func__);
1619 if (!authctxt->valid)
1620 fatal("%s: authctxt not valid", __func__);
1621 blob = buffer_get_string(m, &blen);
1622 if (!monitor_allowed_key(blob, blen))
1623 fatal("%s: bad key, not previously allowed", __func__);
1624 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1625 fatal("%s: key type mismatch", __func__);
1626 if ((key = key_from_blob(blob, blen)) == NULL)
1627 fatal("%s: received bad key", __func__);
1628 if (key->type != KEY_RSA)
1629 fatal("%s: received bad key type %d", __func__, key->type);
1630 key->type = KEY_RSA1;
1632 BN_clear_free(ssh1_challenge);
1633 ssh1_challenge = auth_rsa_generate_challenge(key);
1636 buffer_put_bignum2(m, ssh1_challenge);
1638 debug3("%s sending reply", __func__);
1639 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1641 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1649 mm_answer_rsa_response(int sock, Buffer *m)
1652 u_char *blob, *response;
1656 debug3("%s entering", __func__);
1658 if (!authctxt->valid)
1659 fatal("%s: authctxt not valid", __func__);
1660 if (ssh1_challenge == NULL)
1661 fatal("%s: no ssh1_challenge", __func__);
1663 blob = buffer_get_string(m, &blen);
1664 if (!monitor_allowed_key(blob, blen))
1665 fatal("%s: bad key, not previously allowed", __func__);
1666 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1667 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1668 if ((key = key_from_blob(blob, blen)) == NULL)
1669 fatal("%s: received bad key", __func__);
1670 response = buffer_get_string(m, &len);
1672 fatal("%s: received bad response to challenge", __func__);
1673 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1679 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1682 BN_clear_free(ssh1_challenge);
1683 ssh1_challenge = NULL;
1684 monitor_reset_key_state();
1687 buffer_put_int(m, success);
1688 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1694 mm_answer_term(int sock, Buffer *req)
1696 extern struct monitor *pmonitor;
1699 debug3("%s: tearing down sessions", __func__);
1701 /* The child is terminating */
1702 session_destroy_all(&mm_session_close);
1705 if (options.use_pam)
1709 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1713 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1715 /* Terminate process */
1719 #ifdef SSH_AUDIT_EVENTS
1720 /* Report that an audit event occurred */
1722 mm_answer_audit_event(int socket, Buffer *m)
1724 ssh_audit_event_t event;
1726 debug3("%s entering", __func__);
1728 event = buffer_get_int(m);
1730 case SSH_AUTH_FAIL_PUBKEY:
1731 case SSH_AUTH_FAIL_HOSTBASED:
1732 case SSH_AUTH_FAIL_GSSAPI:
1733 case SSH_LOGIN_EXCEED_MAXTRIES:
1734 case SSH_LOGIN_ROOT_DENIED:
1735 case SSH_CONNECTION_CLOSE:
1736 case SSH_INVALID_USER:
1740 fatal("Audit event type %d not permitted", event);
1747 mm_answer_audit_command(int socket, Buffer *m)
1752 debug3("%s entering", __func__);
1753 cmd = buffer_get_string(m, &len);
1754 /* sanity check command, if so how? */
1755 audit_run_command(cmd);
1759 #endif /* SSH_AUDIT_EVENTS */
1762 monitor_apply_keystate(struct monitor *pmonitor)
1765 set_newkeys(MODE_IN);
1766 set_newkeys(MODE_OUT);
1768 packet_set_protocol_flags(child_state.ssh1protoflags);
1769 packet_set_encryption_key(child_state.ssh1key,
1770 child_state.ssh1keylen, child_state.ssh1cipher);
1771 free(child_state.ssh1key);
1774 /* for rc4 and other stateful ciphers */
1775 packet_set_keycontext(MODE_OUT, child_state.keyout);
1776 free(child_state.keyout);
1777 packet_set_keycontext(MODE_IN, child_state.keyin);
1778 free(child_state.keyin);
1781 packet_set_iv(MODE_OUT, child_state.ivout);
1782 free(child_state.ivout);
1783 packet_set_iv(MODE_IN, child_state.ivin);
1784 free(child_state.ivin);
1787 memcpy(&incoming_stream, &child_state.incoming,
1788 sizeof(incoming_stream));
1789 memcpy(&outgoing_stream, &child_state.outgoing,
1790 sizeof(outgoing_stream));
1792 /* Update with new address */
1793 if (options.compression)
1794 mm_init_compression(pmonitor->m_zlib);
1796 if (options.rekey_limit || options.rekey_interval)
1797 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
1798 (time_t)options.rekey_interval);
1800 /* Network I/O buffers */
1801 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1802 buffer_clear(packet_get_input());
1803 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
1804 explicit_bzero(child_state.input, child_state.ilen);
1805 free(child_state.input);
1807 buffer_clear(packet_get_output());
1808 buffer_append(packet_get_output(), child_state.output,
1810 explicit_bzero(child_state.output, child_state.olen);
1811 free(child_state.output);
1815 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
1819 mm_get_kex(Buffer *m)
1825 kex = xcalloc(1, sizeof(*kex));
1826 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1827 if (session_id2 == NULL ||
1828 kex->session_id_len != session_id2_len ||
1829 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
1830 fatal("mm_get_get: internal error: bad session id");
1831 kex->we_need = buffer_get_int(m);
1832 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1833 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1834 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1835 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1836 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1837 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1839 kex->hostkey_type = buffer_get_int(m);
1840 kex->kex_type = buffer_get_int(m);
1841 blob = buffer_get_string(m, &bloblen);
1842 buffer_init(&kex->my);
1843 buffer_append(&kex->my, blob, bloblen);
1845 blob = buffer_get_string(m, &bloblen);
1846 buffer_init(&kex->peer);
1847 buffer_append(&kex->peer, blob, bloblen);
1850 kex->flags = buffer_get_int(m);
1851 kex->client_version_string = buffer_get_string(m, NULL);
1852 kex->server_version_string = buffer_get_string(m, NULL);
1853 kex->load_host_public_key=&get_hostkey_public_by_type;
1854 kex->load_host_private_key=&get_hostkey_private_by_type;
1855 kex->host_key_index=&get_hostkey_index;
1856 kex->sign = sshd_hostkey_sign;
1861 /* This function requries careful sanity checking */
1864 mm_get_keystate(struct monitor *pmonitor)
1868 u_int bloblen, plen;
1869 u_int32_t seqnr, packets;
1870 u_int64_t blocks, bytes;
1872 debug3("%s: Waiting for new keys", __func__);
1875 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1877 child_state.ssh1protoflags = buffer_get_int(&m);
1878 child_state.ssh1cipher = buffer_get_int(&m);
1879 child_state.ssh1key = buffer_get_string(&m,
1880 &child_state.ssh1keylen);
1881 child_state.ivout = buffer_get_string(&m,
1882 &child_state.ivoutlen);
1883 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1886 /* Get the Kex for rekeying */
1887 *pmonitor->m_pkex = mm_get_kex(&m);
1890 blob = buffer_get_string(&m, &bloblen);
1891 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1894 debug3("%s: Waiting for second key", __func__);
1895 blob = buffer_get_string(&m, &bloblen);
1896 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1899 /* Now get sequence numbers for the packets */
1900 seqnr = buffer_get_int(&m);
1901 blocks = buffer_get_int64(&m);
1902 packets = buffer_get_int(&m);
1903 bytes = buffer_get_int64(&m);
1904 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
1905 seqnr = buffer_get_int(&m);
1906 blocks = buffer_get_int64(&m);
1907 packets = buffer_get_int(&m);
1908 bytes = buffer_get_int64(&m);
1909 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
1912 /* Get the key context */
1913 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1914 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1916 debug3("%s: Getting compression state", __func__);
1917 /* Get compression state */
1918 p = buffer_get_string(&m, &plen);
1919 if (plen != sizeof(child_state.outgoing))
1920 fatal("%s: bad request size", __func__);
1921 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1924 p = buffer_get_string(&m, &plen);
1925 if (plen != sizeof(child_state.incoming))
1926 fatal("%s: bad request size", __func__);
1927 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1930 /* Network I/O buffers */
1931 debug3("%s: Getting Network I/O buffers", __func__);
1932 child_state.input = buffer_get_string(&m, &child_state.ilen);
1933 child_state.output = buffer_get_string(&m, &child_state.olen);
1937 child_state.sent_bytes = buffer_get_int64(&m);
1938 child_state.recv_bytes = buffer_get_int64(&m);
1945 /* Allocation functions for zlib */
1947 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1949 size_t len = (size_t) size * ncount;
1952 if (len == 0 || ncount > SIZE_T_MAX / size)
1953 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1955 address = mm_malloc(mm, len);
1961 mm_zfree(struct mm_master *mm, void *address)
1963 mm_free(mm, address);
1967 mm_init_compression(struct mm_master *mm)
1969 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1970 outgoing_stream.zfree = (free_func)mm_zfree;
1971 outgoing_stream.opaque = mm;
1973 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1974 incoming_stream.zfree = (free_func)mm_zfree;
1975 incoming_stream.opaque = mm;
1980 #define FD_CLOSEONEXEC(x) do { \
1981 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1982 fatal("fcntl(%d, F_SETFD)", x); \
1986 monitor_openfds(struct monitor *mon, int do_logfds)
1990 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1991 fatal("%s: socketpair: %s", __func__, strerror(errno));
1992 FD_CLOSEONEXEC(pair[0]);
1993 FD_CLOSEONEXEC(pair[1]);
1994 mon->m_recvfd = pair[0];
1995 mon->m_sendfd = pair[1];
1998 if (pipe(pair) == -1)
1999 fatal("%s: pipe: %s", __func__, strerror(errno));
2000 FD_CLOSEONEXEC(pair[0]);
2001 FD_CLOSEONEXEC(pair[1]);
2002 mon->m_log_recvfd = pair[0];
2003 mon->m_log_sendfd = pair[1];
2005 mon->m_log_recvfd = mon->m_log_sendfd = -1;
2008 #define MM_MEMSIZE 65536
2013 struct monitor *mon;
2015 mon = xcalloc(1, sizeof(*mon));
2017 monitor_openfds(mon, 1);
2019 /* Used to share zlib space across processes */
2020 if (options.compression) {
2021 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
2022 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
2024 /* Compression needs to share state across borders */
2025 mm_init_compression(mon->m_zlib);
2032 monitor_reinit(struct monitor *mon)
2034 monitor_openfds(mon, 0);
2039 mm_answer_gss_setup_ctx(int sock, Buffer *m)
2045 goid.elements = buffer_get_string(m, &len);
2048 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
2050 free(goid.elements);
2053 buffer_put_int(m, major);
2055 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
2057 /* Now we have a context, enable the step */
2058 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
2064 mm_answer_gss_accept_ctx(int sock, Buffer *m)
2067 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
2068 OM_uint32 major, minor;
2069 OM_uint32 flags = 0; /* GSI needs this */
2072 in.value = buffer_get_string(m, &len);
2074 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
2078 buffer_put_int(m, major);
2079 buffer_put_string(m, out.value, out.length);
2080 buffer_put_int(m, flags);
2081 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
2083 gss_release_buffer(&minor, &out);
2085 if (major == GSS_S_COMPLETE) {
2086 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2087 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2088 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
2094 mm_answer_gss_checkmic(int sock, Buffer *m)
2096 gss_buffer_desc gssbuf, mic;
2100 gssbuf.value = buffer_get_string(m, &len);
2101 gssbuf.length = len;
2102 mic.value = buffer_get_string(m, &len);
2105 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
2111 buffer_put_int(m, ret);
2113 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
2115 if (!GSS_ERROR(ret))
2116 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2122 mm_answer_gss_userok(int sock, Buffer *m)
2126 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2129 buffer_put_int(m, authenticated);
2131 debug3("%s: sending result %d", __func__, authenticated);
2132 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
2134 auth_method = "gssapi-with-mic";
2136 /* Monitor loop will terminate if authenticated */
2137 return (authenticated);