1 /* $OpenBSD: monitor.c,v 1.174 2017/10/02 19:33:20 djm Exp $ */
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include "openbsd-compat/sys-tree.h"
54 # ifdef HAVE_SYS_POLL_H
55 # include <sys/poll.h>
64 #include <openssl/dh.h>
67 #include "openbsd-compat/sys-queue.h"
79 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
82 #define TARGET_OS_MAC 1
87 #include "auth-options.h"
100 #include "monitor_wrap.h"
101 #include "monitor_fdpass.h"
109 static Gssctxt *gsscontext = NULL;
113 extern ServerOptions options;
114 extern u_int utmp_len;
115 extern u_char session_id[];
116 extern Buffer auth_debug;
117 extern int auth_debug_init;
118 extern Buffer loginmsg;
120 /* State exported from the child */
121 static struct sshbuf *child_state;
123 /* Functions on the monitor that answer unprivileged requests */
125 int mm_answer_moduli(int, Buffer *);
126 int mm_answer_sign(int, Buffer *);
127 int mm_answer_pwnamallow(int, Buffer *);
128 int mm_answer_auth2_read_banner(int, Buffer *);
129 int mm_answer_authserv(int, Buffer *);
130 int mm_answer_authpassword(int, Buffer *);
131 int mm_answer_bsdauthquery(int, Buffer *);
132 int mm_answer_bsdauthrespond(int, Buffer *);
133 int mm_answer_skeyquery(int, Buffer *);
134 int mm_answer_skeyrespond(int, Buffer *);
135 int mm_answer_keyallowed(int, Buffer *);
136 int mm_answer_keyverify(int, Buffer *);
137 int mm_answer_pty(int, Buffer *);
138 int mm_answer_pty_cleanup(int, Buffer *);
139 int mm_answer_term(int, Buffer *);
140 int mm_answer_rsa_keyallowed(int, Buffer *);
141 int mm_answer_rsa_challenge(int, Buffer *);
142 int mm_answer_rsa_response(int, Buffer *);
143 int mm_answer_sesskey(int, Buffer *);
144 int mm_answer_sessid(int, Buffer *);
147 int mm_answer_pam_start(int, Buffer *);
148 int mm_answer_pam_account(int, Buffer *);
149 int mm_answer_pam_init_ctx(int, Buffer *);
150 int mm_answer_pam_query(int, Buffer *);
151 int mm_answer_pam_respond(int, Buffer *);
152 int mm_answer_pam_free_ctx(int, Buffer *);
156 int mm_answer_gss_setup_ctx(int, Buffer *);
157 int mm_answer_gss_accept_ctx(int, Buffer *);
158 int mm_answer_gss_userok(int, Buffer *);
159 int mm_answer_gss_checkmic(int, Buffer *);
162 #ifdef SSH_AUDIT_EVENTS
163 int mm_answer_audit_event(int, Buffer *);
164 int mm_answer_audit_command(int, Buffer *);
167 static int monitor_read_log(struct monitor *);
169 static Authctxt *authctxt;
171 /* local state for key verify */
172 static u_char *key_blob = NULL;
173 static u_int key_bloblen = 0;
174 static int key_blobtype = MM_NOKEY;
175 static char *hostbased_cuser = NULL;
176 static char *hostbased_chost = NULL;
177 static char *auth_method = "unknown";
178 static char *auth_submethod = NULL;
179 static u_int session_id2_len = 0;
180 static u_char *session_id2 = NULL;
181 static pid_t monitor_child_pid;
184 enum monitor_reqtype type;
186 int (*f)(int, Buffer *);
189 #define MON_ISAUTH 0x0004 /* Required for Authentication */
190 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
191 #define MON_ONCE 0x0010 /* Disable after calling */
192 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
194 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
196 #define MON_PERMIT 0x1000 /* Request is permitted */
198 struct mon_table mon_dispatch_proto20[] = {
200 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
202 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
203 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
204 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
205 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
206 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
208 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
209 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
210 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
211 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
212 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
213 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
215 #ifdef SSH_AUDIT_EVENTS
216 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
219 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
220 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
223 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
224 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
226 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
227 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
229 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
230 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
231 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
232 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
237 struct mon_table mon_dispatch_postauth20[] = {
239 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
241 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
242 {MONITOR_REQ_PTY, 0, mm_answer_pty},
243 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
244 {MONITOR_REQ_TERM, 0, mm_answer_term},
245 #ifdef SSH_AUDIT_EVENTS
246 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
247 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
252 struct mon_table *mon_dispatch;
254 /* Specifies if a certain message is allowed at the moment */
257 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
259 while (ent->f != NULL) {
260 if (ent->type == type) {
261 ent->flags &= ~MON_PERMIT;
262 ent->flags |= permit ? MON_PERMIT : 0;
270 monitor_permit_authentications(int permit)
272 struct mon_table *ent = mon_dispatch;
274 while (ent->f != NULL) {
275 if (ent->flags & MON_AUTH) {
276 ent->flags &= ~MON_PERMIT;
277 ent->flags |= permit ? MON_PERMIT : 0;
284 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
286 struct ssh *ssh = active_state; /* XXX */
287 struct mon_table *ent;
288 int authenticated = 0, partial = 0;
290 debug3("preauth child monitor started");
292 close(pmonitor->m_recvfd);
293 close(pmonitor->m_log_sendfd);
294 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
296 authctxt = _authctxt;
297 memset(authctxt, 0, sizeof(*authctxt));
299 authctxt->loginmsg = &loginmsg;
301 mon_dispatch = mon_dispatch_proto20;
302 /* Permit requests for moduli and signatures */
303 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
304 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
306 /* The first few requests do not require asynchronous access */
307 while (!authenticated) {
309 auth_method = "unknown";
310 auth_submethod = NULL;
311 auth2_authctxt_reset_info(authctxt);
313 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
315 /* Special handling for multiple required authentications */
316 if (options.num_auth_methods != 0) {
318 !auth2_update_methods_lists(authctxt,
319 auth_method, auth_submethod)) {
320 debug3("%s: method %s: partial", __func__,
328 if (!(ent->flags & MON_AUTHDECIDE))
329 fatal("%s: unexpected authentication from %d",
330 __func__, ent->type);
331 if (authctxt->pw->pw_uid == 0 &&
332 !auth_root_allowed(auth_method))
335 /* PAM needs to perform account checks after auth */
336 if (options.use_pam && authenticated) {
340 mm_request_receive_expect(pmonitor->m_sendfd,
341 MONITOR_REQ_PAM_ACCOUNT, &m);
342 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
347 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
348 auth_log(authctxt, authenticated, partial,
349 auth_method, auth_submethod);
350 if (!partial && !authenticated)
351 authctxt->failures++;
352 if (authenticated || partial) {
353 auth2_update_session_info(authctxt,
354 auth_method, auth_submethod);
359 if (!authctxt->valid)
360 fatal("%s: authenticated invalid user", __func__);
361 if (strcmp(auth_method, "unknown") == 0)
362 fatal("%s: authentication method name unknown", __func__);
364 debug("%s: %s has been authenticated by privileged process",
365 __func__, authctxt->user);
366 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
368 mm_get_keystate(pmonitor);
370 /* Drain any buffered messages from the child */
371 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
374 close(pmonitor->m_sendfd);
375 close(pmonitor->m_log_recvfd);
376 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
380 monitor_set_child_handler(pid_t pid)
382 monitor_child_pid = pid;
386 monitor_child_handler(int sig)
388 kill(monitor_child_pid, sig);
392 monitor_child_postauth(struct monitor *pmonitor)
394 close(pmonitor->m_recvfd);
395 pmonitor->m_recvfd = -1;
397 monitor_set_child_handler(pmonitor->m_pid);
398 signal(SIGHUP, &monitor_child_handler);
399 signal(SIGTERM, &monitor_child_handler);
400 signal(SIGINT, &monitor_child_handler);
402 signal(SIGXFSZ, SIG_IGN);
405 mon_dispatch = mon_dispatch_postauth20;
407 /* Permit requests for moduli and signatures */
408 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
409 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
410 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
413 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
414 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
418 monitor_read(pmonitor, mon_dispatch, NULL);
422 monitor_read_log(struct monitor *pmonitor)
428 buffer_init(&logmsg);
431 buffer_append_space(&logmsg, 4);
432 if (atomicio(read, pmonitor->m_log_recvfd,
433 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
434 if (errno == EPIPE) {
435 buffer_free(&logmsg);
436 debug("%s: child log fd closed", __func__);
437 close(pmonitor->m_log_recvfd);
438 pmonitor->m_log_recvfd = -1;
441 fatal("%s: log fd read: %s", __func__, strerror(errno));
443 len = buffer_get_int(&logmsg);
444 if (len <= 4 || len > 8192)
445 fatal("%s: invalid log message length %u", __func__, len);
447 /* Read severity, message */
448 buffer_clear(&logmsg);
449 buffer_append_space(&logmsg, len);
450 if (atomicio(read, pmonitor->m_log_recvfd,
451 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
452 fatal("%s: log fd read: %s", __func__, strerror(errno));
455 level = buffer_get_int(&logmsg);
456 msg = buffer_get_string(&logmsg, NULL);
457 if (log_level_name(level) == NULL)
458 fatal("%s: invalid log level %u (corrupted message?)",
460 do_log2(level, "%s [preauth]", msg);
462 buffer_free(&logmsg);
469 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
470 struct mon_table **pent)
475 struct pollfd pfd[2];
478 memset(&pfd, 0, sizeof(pfd));
479 pfd[0].fd = pmonitor->m_sendfd;
480 pfd[0].events = POLLIN;
481 pfd[1].fd = pmonitor->m_log_recvfd;
482 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
483 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
484 if (errno == EINTR || errno == EAGAIN)
486 fatal("%s: poll: %s", __func__, strerror(errno));
488 if (pfd[1].revents) {
490 * Drain all log messages before processing next
493 monitor_read_log(pmonitor);
497 break; /* Continues below */
502 mm_request_receive(pmonitor->m_sendfd, &m);
503 type = buffer_get_char(&m);
505 debug3("%s: checking request %d", __func__, type);
507 while (ent->f != NULL) {
508 if (ent->type == type)
513 if (ent->f != NULL) {
514 if (!(ent->flags & MON_PERMIT))
515 fatal("%s: unpermitted request %d", __func__,
517 ret = (*ent->f)(pmonitor->m_sendfd, &m);
520 /* The child may use this request only once, disable it */
521 if (ent->flags & MON_ONCE) {
522 debug2("%s: %d used once, disabling now", __func__,
524 ent->flags &= ~MON_PERMIT;
533 fatal("%s: unsupported request: %d", __func__, type);
539 /* allowed key state */
541 monitor_allowed_key(u_char *blob, u_int bloblen)
543 /* make sure key is allowed */
544 if (key_blob == NULL || key_bloblen != bloblen ||
545 timingsafe_bcmp(key_blob, blob, key_bloblen))
551 monitor_reset_key_state(void)
555 free(hostbased_cuser);
556 free(hostbased_chost);
559 key_blobtype = MM_NOKEY;
560 hostbased_cuser = NULL;
561 hostbased_chost = NULL;
566 mm_answer_moduli(int sock, Buffer *m)
571 min = buffer_get_int(m);
572 want = buffer_get_int(m);
573 max = buffer_get_int(m);
575 debug3("%s: got parameters: %d %d %d",
576 __func__, min, want, max);
577 /* We need to check here, too, in case the child got corrupted */
578 if (max < min || want < min || max < want)
579 fatal("%s: bad parameters: %d %d %d",
580 __func__, min, want, max);
584 dh = choose_dh(min, want, max);
586 buffer_put_char(m, 0);
589 /* Send first bignum */
590 buffer_put_char(m, 1);
591 buffer_put_bignum2(m, dh->p);
592 buffer_put_bignum2(m, dh->g);
596 mm_request_send(sock, MONITOR_ANS_MODULI, m);
602 mm_answer_sign(int sock, Buffer *m)
604 struct ssh *ssh = active_state; /* XXX */
605 extern int auth_sock; /* XXX move to state struct? */
607 struct sshbuf *sigbuf = NULL;
608 u_char *p = NULL, *signature = NULL;
610 size_t datlen, siglen, alglen;
613 const char proof_req[] = "hostkeys-prove-00@openssh.com";
615 debug3("%s", __func__);
617 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
618 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
619 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
620 fatal("%s: buffer error: %s", __func__, ssh_err(r));
622 fatal("%s: invalid key ID", __func__);
625 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
626 * SHA384 (48 bytes) and SHA512 (64 bytes).
628 * Otherwise, verify the signature request is for a hostkey
631 * XXX perform similar check for KEX signature requests too?
632 * it's not trivial, since what is signed is the hash, rather
633 * than the full kex structure...
635 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
637 * Construct expected hostkey proof and compare it to what
638 * the client sent us.
640 if (session_id2_len == 0) /* hostkeys is never first */
641 fatal("%s: bad data length: %zu", __func__, datlen);
642 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
643 fatal("%s: no hostkey for index %d", __func__, keyid);
644 if ((sigbuf = sshbuf_new()) == NULL)
645 fatal("%s: sshbuf_new", __func__);
646 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
647 (r = sshbuf_put_string(sigbuf, session_id2,
648 session_id2_len)) != 0 ||
649 (r = sshkey_puts(key, sigbuf)) != 0)
650 fatal("%s: couldn't prepare private key "
651 "proof buffer: %s", __func__, ssh_err(r));
652 if (datlen != sshbuf_len(sigbuf) ||
653 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
654 fatal("%s: bad data length: %zu, hostkey proof len %zu",
655 __func__, datlen, sshbuf_len(sigbuf));
660 /* save session id, it will be passed on the first call */
661 if (session_id2_len == 0) {
662 session_id2_len = datlen;
663 session_id2 = xmalloc(session_id2_len);
664 memcpy(session_id2, p, session_id2_len);
667 if ((key = get_hostkey_by_index(keyid)) != NULL) {
668 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
670 fatal("%s: sshkey_sign failed: %s",
671 __func__, ssh_err(r));
672 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
674 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
675 p, datlen, alg, datafellows)) != 0) {
676 fatal("%s: ssh_agent_sign failed: %s",
677 __func__, ssh_err(r));
680 fatal("%s: no hostkey from index %d", __func__, keyid);
682 debug3("%s: %s signature %p(%zu)", __func__,
683 is_proof ? "KEX" : "hostkey proof", signature, siglen);
686 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
687 fatal("%s: buffer error: %s", __func__, ssh_err(r));
693 mm_request_send(sock, MONITOR_ANS_SIGN, m);
695 /* Turn on permissions for getpwnam */
696 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
701 /* Retrieves the password entry and also checks if the user is permitted */
704 mm_answer_pwnamallow(int sock, Buffer *m)
706 struct ssh *ssh = active_state; /* XXX */
708 struct passwd *pwent;
712 debug3("%s", __func__);
714 if (authctxt->attempt++ != 0)
715 fatal("%s: multiple attempts for getpwnam", __func__);
717 username = buffer_get_string(m, NULL);
719 pwent = getpwnamallow(username);
721 authctxt->user = xstrdup(username);
722 setproctitle("%s [priv]", pwent ? username : "unknown");
728 buffer_put_char(m, 0);
729 authctxt->pw = fakepw();
734 authctxt->pw = pwent;
737 buffer_put_char(m, 1);
738 buffer_put_string(m, pwent, sizeof(struct passwd));
739 buffer_put_cstring(m, pwent->pw_name);
740 buffer_put_cstring(m, "*");
741 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
742 buffer_put_cstring(m, pwent->pw_gecos);
744 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
745 buffer_put_cstring(m, pwent->pw_class);
747 buffer_put_cstring(m, pwent->pw_dir);
748 buffer_put_cstring(m, pwent->pw_shell);
751 ssh_packet_set_log_preamble(ssh, "%suser %s",
752 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
753 buffer_put_string(m, &options, sizeof(options));
755 #define M_CP_STROPT(x) do { \
756 if (options.x != NULL) \
757 buffer_put_cstring(m, options.x); \
759 #define M_CP_STRARRAYOPT(x, nx) do { \
760 for (i = 0; i < options.nx; i++) \
761 buffer_put_cstring(m, options.x[i]); \
763 #define M_CP_STRARRAYOPT_ALLOC(x, nx) M_CP_STRARRAYOPT(x, nx)
764 /* See comment in servconf.h */
765 COPY_MATCH_STRING_OPTS();
767 #undef M_CP_STRARRAYOPT
768 #undef M_CP_STRARRAYOPT_ALLOC
770 /* Create valid auth method lists */
771 if (auth2_setup_methods_lists(authctxt) != 0) {
773 * The monitor will continue long enough to let the child
774 * run to it's packet_disconnect(), but it must not allow any
775 * authentication to succeed.
777 debug("%s: no valid authentication method lists", __func__);
780 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
781 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
783 /* Allow service/style information on the auth context */
784 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
785 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
789 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
795 int mm_answer_auth2_read_banner(int sock, Buffer *m)
800 banner = auth2_read_banner();
801 buffer_put_cstring(m, banner != NULL ? banner : "");
802 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
809 mm_answer_authserv(int sock, Buffer *m)
811 monitor_permit_authentications(1);
813 authctxt->service = buffer_get_string(m, NULL);
814 authctxt->style = buffer_get_string(m, NULL);
815 debug3("%s: service=%s, style=%s",
816 __func__, authctxt->service, authctxt->style);
818 if (strlen(authctxt->style) == 0) {
819 free(authctxt->style);
820 authctxt->style = NULL;
827 mm_answer_authpassword(int sock, Buffer *m)
829 static int call_count;
834 if (!options.password_authentication)
835 fatal("%s: password authentication not enabled", __func__);
836 passwd = buffer_get_string(m, &plen);
837 /* Only authenticate if the context is valid */
838 authenticated = options.password_authentication &&
839 auth_password(authctxt, passwd);
840 explicit_bzero(passwd, strlen(passwd));
844 buffer_put_int(m, authenticated);
846 buffer_put_int(m, sshpam_get_maxtries_reached());
849 debug3("%s: sending result %d", __func__, authenticated);
850 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
853 if (plen == 0 && call_count == 1)
854 auth_method = "none";
856 auth_method = "password";
858 /* Causes monitor loop to terminate if authenticated */
859 return (authenticated);
864 mm_answer_bsdauthquery(int sock, Buffer *m)
866 char *name, *infotxt;
872 if (!options.kbd_interactive_authentication)
873 fatal("%s: kbd-int authentication not enabled", __func__);
874 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
875 &prompts, &echo_on) < 0 ? 0 : 1;
878 buffer_put_int(m, success);
880 buffer_put_cstring(m, prompts[0]);
882 debug3("%s: sending challenge success: %u", __func__, success);
883 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
896 mm_answer_bsdauthrespond(int sock, Buffer *m)
901 if (!options.kbd_interactive_authentication)
902 fatal("%s: kbd-int authentication not enabled", __func__);
903 if (authctxt->as == NULL)
904 fatal("%s: no bsd auth session", __func__);
906 response = buffer_get_string(m, NULL);
907 authok = options.challenge_response_authentication &&
908 auth_userresponse(authctxt->as, response, 0);
910 debug3("%s: <%s> = <%d>", __func__, response, authok);
914 buffer_put_int(m, authok);
916 debug3("%s: sending authenticated: %d", __func__, authok);
917 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
919 auth_method = "keyboard-interactive";
920 auth_submethod = "bsdauth";
922 return (authok != 0);
928 mm_answer_skeyquery(int sock, Buffer *m)
931 char challenge[1024];
934 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
935 sizeof(challenge)) < 0 ? 0 : 1;
938 buffer_put_int(m, success);
940 buffer_put_cstring(m, challenge);
942 debug3("%s: sending challenge success: %u", __func__, success);
943 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
949 mm_answer_skeyrespond(int sock, Buffer *m)
954 response = buffer_get_string(m, NULL);
956 authok = (options.challenge_response_authentication &&
958 skey_haskey(authctxt->pw->pw_name) == 0 &&
959 skey_passcheck(authctxt->pw->pw_name, response) != -1);
964 buffer_put_int(m, authok);
966 debug3("%s: sending authenticated: %d", __func__, authok);
967 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
969 auth_method = "keyboard-interactive";
970 auth_submethod = "skey";
972 return (authok != 0);
978 mm_answer_pam_start(int sock, Buffer *m)
980 if (!options.use_pam)
981 fatal("UsePAM not set, but ended up in %s anyway", __func__);
985 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
986 if (options.kbd_interactive_authentication)
987 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
993 mm_answer_pam_account(int sock, Buffer *m)
997 if (!options.use_pam)
998 fatal("%s: PAM not enabled", __func__);
1000 ret = do_pam_account();
1002 buffer_put_int(m, ret);
1003 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1005 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
1010 static void *sshpam_ctxt, *sshpam_authok;
1011 extern KbdintDevice sshpam_device;
1014 mm_answer_pam_init_ctx(int sock, Buffer *m)
1016 debug3("%s", __func__);
1017 if (!options.kbd_interactive_authentication)
1018 fatal("%s: kbd-int authentication not enabled", __func__);
1019 if (sshpam_ctxt != NULL)
1020 fatal("%s: already called", __func__);
1021 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1022 sshpam_authok = NULL;
1024 if (sshpam_ctxt != NULL) {
1025 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1026 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
1027 buffer_put_int(m, 1);
1029 buffer_put_int(m, 0);
1031 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
1036 mm_answer_pam_query(int sock, Buffer *m)
1038 char *name = NULL, *info = NULL, **prompts = NULL;
1039 u_int i, num = 0, *echo_on = 0;
1042 debug3("%s", __func__);
1043 sshpam_authok = NULL;
1044 if (sshpam_ctxt == NULL)
1045 fatal("%s: no context", __func__);
1046 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1047 &num, &prompts, &echo_on);
1048 if (ret == 0 && num == 0)
1049 sshpam_authok = sshpam_ctxt;
1050 if (num > 1 || name == NULL || info == NULL)
1051 fatal("sshpam_device.query failed");
1052 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
1054 buffer_put_int(m, ret);
1055 buffer_put_cstring(m, name);
1057 buffer_put_cstring(m, info);
1059 buffer_put_int(m, sshpam_get_maxtries_reached());
1060 buffer_put_int(m, num);
1061 for (i = 0; i < num; ++i) {
1062 buffer_put_cstring(m, prompts[i]);
1064 buffer_put_int(m, echo_on[i]);
1068 auth_method = "keyboard-interactive";
1069 auth_submethod = "pam";
1070 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
1075 mm_answer_pam_respond(int sock, Buffer *m)
1081 debug3("%s", __func__);
1082 if (sshpam_ctxt == NULL)
1083 fatal("%s: no context", __func__);
1084 sshpam_authok = NULL;
1085 num = buffer_get_int(m);
1087 resp = xcalloc(num, sizeof(char *));
1088 for (i = 0; i < num; ++i)
1089 resp[i] = buffer_get_string(m, NULL);
1090 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1091 for (i = 0; i < num; ++i)
1095 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1098 buffer_put_int(m, ret);
1099 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1100 auth_method = "keyboard-interactive";
1101 auth_submethod = "pam";
1103 sshpam_authok = sshpam_ctxt;
1108 mm_answer_pam_free_ctx(int sock, Buffer *m)
1110 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
1112 debug3("%s", __func__);
1113 if (sshpam_ctxt == NULL)
1114 fatal("%s: no context", __func__);
1115 (sshpam_device.free_ctx)(sshpam_ctxt);
1116 sshpam_ctxt = sshpam_authok = NULL;
1118 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1119 /* Allow another attempt */
1120 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
1121 auth_method = "keyboard-interactive";
1122 auth_submethod = "pam";
1128 mm_answer_keyallowed(int sock, Buffer *m)
1131 char *cuser, *chost;
1133 u_int bloblen, pubkey_auth_attempt;
1134 enum mm_keytype type = 0;
1137 debug3("%s entering", __func__);
1139 type = buffer_get_int(m);
1140 cuser = buffer_get_string(m, NULL);
1141 chost = buffer_get_string(m, NULL);
1142 blob = buffer_get_string(m, &bloblen);
1143 pubkey_auth_attempt = buffer_get_int(m);
1145 key = key_from_blob(blob, bloblen);
1147 debug3("%s: key_from_blob: %p", __func__, key);
1149 if (key != NULL && authctxt->valid) {
1150 /* These should not make it past the privsep child */
1151 if (key_type_plain(key->type) == KEY_RSA &&
1152 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1153 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1157 allowed = options.pubkey_authentication &&
1158 !auth2_key_already_used(authctxt, key) &&
1159 match_pattern_list(sshkey_ssh_name(key),
1160 options.pubkey_key_types, 0) == 1 &&
1161 user_key_allowed(authctxt->pw, key,
1162 pubkey_auth_attempt);
1163 auth_method = "publickey";
1164 if (options.pubkey_authentication &&
1165 (!pubkey_auth_attempt || allowed != 1))
1166 auth_clear_options();
1169 allowed = options.hostbased_authentication &&
1170 !auth2_key_already_used(authctxt, key) &&
1171 match_pattern_list(sshkey_ssh_name(key),
1172 options.hostbased_key_types, 0) == 1 &&
1173 hostbased_key_allowed(authctxt->pw,
1175 auth2_record_info(authctxt,
1176 "client user \"%.100s\", client host \"%.100s\"",
1178 auth_method = "hostbased";
1181 fatal("%s: unknown key type %d", __func__, type);
1186 debug3("%s: key is %s", __func__, allowed ? "allowed" : "not allowed");
1188 auth2_record_key(authctxt, 0, key);
1191 /* clear temporarily storage (used by verify) */
1192 monitor_reset_key_state();
1195 /* Save temporarily for comparison in verify */
1197 key_bloblen = bloblen;
1198 key_blobtype = type;
1199 hostbased_cuser = cuser;
1200 hostbased_chost = chost;
1202 /* Log failed attempt */
1203 auth_log(authctxt, 0, 0, auth_method, NULL);
1210 buffer_put_int(m, allowed);
1211 buffer_put_int(m, forced_command != NULL);
1213 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1219 monitor_valid_userblob(u_char *data, u_int datalen)
1223 char *userstyle, *cp;
1228 buffer_append(&b, data, datalen);
1230 if (datafellows & SSH_OLD_SESSIONID) {
1232 len = buffer_len(&b);
1233 if ((session_id2 == NULL) ||
1234 (len < session_id2_len) ||
1235 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1237 buffer_consume(&b, session_id2_len);
1239 p = buffer_get_string(&b, &len);
1240 if ((session_id2 == NULL) ||
1241 (len != session_id2_len) ||
1242 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1246 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1248 cp = buffer_get_cstring(&b, NULL);
1249 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1250 authctxt->style ? ":" : "",
1251 authctxt->style ? authctxt->style : "");
1252 if (strcmp(userstyle, cp) != 0) {
1253 logit("wrong user name passed to monitor: "
1254 "expected %s != %.100s", userstyle, cp);
1259 buffer_skip_string(&b);
1260 if (datafellows & SSH_BUG_PKAUTH) {
1261 if (!buffer_get_char(&b))
1264 cp = buffer_get_cstring(&b, NULL);
1265 if (strcmp("publickey", cp) != 0)
1268 if (!buffer_get_char(&b))
1270 buffer_skip_string(&b);
1272 buffer_skip_string(&b);
1273 if (buffer_len(&b) != 0)
1280 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1284 char *p, *userstyle;
1289 buffer_append(&b, data, datalen);
1291 p = buffer_get_string(&b, &len);
1292 if ((session_id2 == NULL) ||
1293 (len != session_id2_len) ||
1294 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1298 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1300 p = buffer_get_cstring(&b, NULL);
1301 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1302 authctxt->style ? ":" : "",
1303 authctxt->style ? authctxt->style : "");
1304 if (strcmp(userstyle, p) != 0) {
1305 logit("wrong user name passed to monitor: expected %s != %.100s",
1311 buffer_skip_string(&b); /* service */
1312 p = buffer_get_cstring(&b, NULL);
1313 if (strcmp(p, "hostbased") != 0)
1316 buffer_skip_string(&b); /* pkalg */
1317 buffer_skip_string(&b); /* pkblob */
1319 /* verify client host, strip trailing dot if necessary */
1320 p = buffer_get_string(&b, NULL);
1321 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1323 if (strcmp(p, chost) != 0)
1327 /* verify client user */
1328 p = buffer_get_string(&b, NULL);
1329 if (strcmp(p, cuser) != 0)
1333 if (buffer_len(&b) != 0)
1340 mm_answer_keyverify(int sock, struct sshbuf *m)
1343 u_char *signature, *data, *blob;
1344 size_t signaturelen, datalen, bloblen;
1345 int r, ret, valid_data = 0, encoded_ret;
1347 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1348 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
1349 (r = sshbuf_get_string(m, &data, &datalen)) != 0)
1350 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1352 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1353 !monitor_allowed_key(blob, bloblen))
1354 fatal("%s: bad key, not previously allowed", __func__);
1356 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1357 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1358 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
1360 switch (key_blobtype) {
1362 valid_data = monitor_valid_userblob(data, datalen);
1363 auth_method = "publickey";
1366 valid_data = monitor_valid_hostbasedblob(data, datalen,
1367 hostbased_cuser, hostbased_chost);
1368 auth_method = "hostbased";
1375 fatal("%s: bad signature data blob", __func__);
1377 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
1378 active_state->compat);
1379 debug3("%s: %s %p signature %s", __func__, auth_method, key,
1380 (ret == 0) ? "verified" : "unverified");
1381 auth2_record_key(authctxt, ret == 0, key);
1387 monitor_reset_key_state();
1392 /* encode ret != 0 as positive integer, since we're sending u32 */
1393 encoded_ret = (ret != 0);
1394 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1395 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1396 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1402 mm_record_login(Session *s, struct passwd *pw)
1404 struct ssh *ssh = active_state; /* XXX */
1406 struct sockaddr_storage from;
1409 * Get IP address of client. If the connection is not a socket, let
1410 * the address be 0.0.0.0.
1412 memset(&from, 0, sizeof(from));
1413 fromlen = sizeof(from);
1414 if (packet_connection_is_on_socket()) {
1415 if (getpeername(packet_get_connection_in(),
1416 (struct sockaddr *)&from, &fromlen) < 0) {
1417 debug("getpeername: %.100s", strerror(errno));
1421 /* Record that there was a login on that tty from the remote host. */
1422 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1423 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
1424 (struct sockaddr *)&from, fromlen);
1428 mm_session_close(Session *s)
1430 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1431 if (s->ttyfd != -1) {
1432 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1433 session_pty_cleanup2(s);
1435 session_unused(s->self);
1439 mm_answer_pty(int sock, Buffer *m)
1441 extern struct monitor *pmonitor;
1445 debug3("%s entering", __func__);
1451 s->authctxt = authctxt;
1452 s->pw = authctxt->pw;
1453 s->pid = pmonitor->m_pid;
1454 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1457 pty_setowner(authctxt->pw, s->tty);
1459 buffer_put_int(m, 1);
1460 buffer_put_cstring(m, s->tty);
1462 /* We need to trick ttyslot */
1463 if (dup2(s->ttyfd, 0) == -1)
1464 fatal("%s: dup2", __func__);
1466 mm_record_login(s, authctxt->pw);
1468 /* Now we can close the file descriptor again */
1471 /* send messages generated by record_login */
1472 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1473 buffer_clear(&loginmsg);
1475 mm_request_send(sock, MONITOR_ANS_PTY, m);
1477 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1478 mm_send_fd(sock, s->ttyfd) == -1)
1479 fatal("%s: send fds failed", __func__);
1481 /* make sure nothing uses fd 0 */
1482 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1483 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1485 error("%s: fd0 %d != 0", __func__, fd0);
1487 /* slave is not needed */
1489 s->ttyfd = s->ptyfd;
1490 /* no need to dup() because nobody closes ptyfd */
1491 s->ptymaster = s->ptyfd;
1493 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1499 mm_session_close(s);
1500 buffer_put_int(m, 0);
1501 mm_request_send(sock, MONITOR_ANS_PTY, m);
1506 mm_answer_pty_cleanup(int sock, Buffer *m)
1511 debug3("%s entering", __func__);
1513 tty = buffer_get_string(m, NULL);
1514 if ((s = session_by_tty(tty)) != NULL)
1515 mm_session_close(s);
1522 mm_answer_term(int sock, Buffer *req)
1524 struct ssh *ssh = active_state; /* XXX */
1525 extern struct monitor *pmonitor;
1528 debug3("%s: tearing down sessions", __func__);
1530 /* The child is terminating */
1531 session_destroy_all(ssh, &mm_session_close);
1534 if (options.use_pam)
1538 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1542 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1544 /* Terminate process */
1548 #ifdef SSH_AUDIT_EVENTS
1549 /* Report that an audit event occurred */
1551 mm_answer_audit_event(int socket, Buffer *m)
1553 ssh_audit_event_t event;
1555 debug3("%s entering", __func__);
1557 event = buffer_get_int(m);
1559 case SSH_AUTH_FAIL_PUBKEY:
1560 case SSH_AUTH_FAIL_HOSTBASED:
1561 case SSH_AUTH_FAIL_GSSAPI:
1562 case SSH_LOGIN_EXCEED_MAXTRIES:
1563 case SSH_LOGIN_ROOT_DENIED:
1564 case SSH_CONNECTION_CLOSE:
1565 case SSH_INVALID_USER:
1569 fatal("Audit event type %d not permitted", event);
1576 mm_answer_audit_command(int socket, Buffer *m)
1581 debug3("%s entering", __func__);
1582 cmd = buffer_get_string(m, &len);
1583 /* sanity check command, if so how? */
1584 audit_run_command(cmd);
1588 #endif /* SSH_AUDIT_EVENTS */
1591 monitor_clear_keystate(struct monitor *pmonitor)
1593 struct ssh *ssh = active_state; /* XXX */
1595 ssh_clear_newkeys(ssh, MODE_IN);
1596 ssh_clear_newkeys(ssh, MODE_OUT);
1597 sshbuf_free(child_state);
1602 monitor_apply_keystate(struct monitor *pmonitor)
1604 struct ssh *ssh = active_state; /* XXX */
1608 debug3("%s: packet_set_state", __func__);
1609 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1610 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1611 sshbuf_free(child_state);
1614 if ((kex = ssh->kex) != NULL) {
1615 /* XXX set callbacks */
1617 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1618 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1619 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1620 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1621 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
1622 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1623 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1624 # ifdef OPENSSL_HAS_ECC
1625 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1627 #endif /* WITH_OPENSSL */
1628 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1629 kex->load_host_public_key=&get_hostkey_public_by_type;
1630 kex->load_host_private_key=&get_hostkey_private_by_type;
1631 kex->host_key_index=&get_hostkey_index;
1632 kex->sign = sshd_hostkey_sign;
1636 /* This function requries careful sanity checking */
1639 mm_get_keystate(struct monitor *pmonitor)
1641 debug3("%s: Waiting for new keys", __func__);
1643 if ((child_state = sshbuf_new()) == NULL)
1644 fatal("%s: sshbuf_new failed", __func__);
1645 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1647 debug3("%s: GOT new keys", __func__);
1653 #define FD_CLOSEONEXEC(x) do { \
1654 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1655 fatal("fcntl(%d, F_SETFD)", x); \
1659 monitor_openfds(struct monitor *mon, int do_logfds)
1666 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1667 fatal("%s: socketpair: %s", __func__, strerror(errno));
1669 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1670 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1671 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1672 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1674 FD_CLOSEONEXEC(pair[0]);
1675 FD_CLOSEONEXEC(pair[1]);
1676 mon->m_recvfd = pair[0];
1677 mon->m_sendfd = pair[1];
1680 if (pipe(pair) == -1)
1681 fatal("%s: pipe: %s", __func__, strerror(errno));
1682 FD_CLOSEONEXEC(pair[0]);
1683 FD_CLOSEONEXEC(pair[1]);
1684 mon->m_log_recvfd = pair[0];
1685 mon->m_log_sendfd = pair[1];
1687 mon->m_log_recvfd = mon->m_log_sendfd = -1;
1690 #define MM_MEMSIZE 65536
1695 struct monitor *mon;
1697 mon = xcalloc(1, sizeof(*mon));
1698 monitor_openfds(mon, 1);
1704 monitor_reinit(struct monitor *mon)
1706 monitor_openfds(mon, 0);
1711 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1717 if (!options.gss_authentication)
1718 fatal("%s: GSSAPI authentication not enabled", __func__);
1720 goid.elements = buffer_get_string(m, &len);
1723 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1725 free(goid.elements);
1728 buffer_put_int(m, major);
1730 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1732 /* Now we have a context, enable the step */
1733 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1739 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1742 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1743 OM_uint32 major, minor;
1744 OM_uint32 flags = 0; /* GSI needs this */
1747 if (!options.gss_authentication)
1748 fatal("%s: GSSAPI authentication not enabled", __func__);
1750 in.value = buffer_get_string(m, &len);
1752 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1756 buffer_put_int(m, major);
1757 buffer_put_string(m, out.value, out.length);
1758 buffer_put_int(m, flags);
1759 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1761 gss_release_buffer(&minor, &out);
1763 if (major == GSS_S_COMPLETE) {
1764 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1765 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1766 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1772 mm_answer_gss_checkmic(int sock, Buffer *m)
1774 gss_buffer_desc gssbuf, mic;
1778 if (!options.gss_authentication)
1779 fatal("%s: GSSAPI authentication not enabled", __func__);
1781 gssbuf.value = buffer_get_string(m, &len);
1782 gssbuf.length = len;
1783 mic.value = buffer_get_string(m, &len);
1786 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1792 buffer_put_int(m, ret);
1794 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1796 if (!GSS_ERROR(ret))
1797 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1803 mm_answer_gss_userok(int sock, Buffer *m)
1806 const char *displayname;
1808 if (!options.gss_authentication)
1809 fatal("%s: GSSAPI authentication not enabled", __func__);
1811 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1814 buffer_put_int(m, authenticated);
1816 debug3("%s: sending result %d", __func__, authenticated);
1817 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1819 auth_method = "gssapi-with-mic";
1821 if ((displayname = ssh_gssapi_displayname()) != NULL)
1822 auth2_record_info(authctxt, "%s", displayname);
1824 /* Monitor loop will terminate if authenticated */
1825 return (authenticated);