1 /* $OpenBSD: monitor.c,v 1.186 2018/07/20 03:46:34 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>
53 # ifdef HAVE_SYS_POLL_H
54 # include <sys/poll.h>
59 #include <openssl/dh.h>
62 #include "openbsd-compat/sys-tree.h"
63 #include "openbsd-compat/sys-queue.h"
64 #include "openbsd-compat/openssl-compat.h"
78 #include "auth-options.h"
91 #include "monitor_wrap.h"
92 #include "monitor_fdpass.h"
100 static Gssctxt *gsscontext = NULL;
104 extern ServerOptions options;
105 extern u_int utmp_len;
106 extern u_char session_id[];
107 extern struct sshbuf *loginmsg;
108 extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
110 /* State exported from the child */
111 static struct sshbuf *child_state;
113 /* Functions on the monitor that answer unprivileged requests */
115 int mm_answer_moduli(int, struct sshbuf *);
116 int mm_answer_sign(int, struct sshbuf *);
117 #ifdef HAVE_LOGIN_CAP
118 int mm_answer_login_getpwclass(int, struct sshbuf *);
120 int mm_answer_pwnamallow(int, struct sshbuf *);
121 int mm_answer_auth2_read_banner(int, struct sshbuf *);
122 int mm_answer_authserv(int, struct sshbuf *);
123 int mm_answer_authpassword(int, struct sshbuf *);
124 int mm_answer_bsdauthquery(int, struct sshbuf *);
125 int mm_answer_bsdauthrespond(int, struct sshbuf *);
126 int mm_answer_keyallowed(int, struct sshbuf *);
127 int mm_answer_keyverify(int, struct sshbuf *);
128 int mm_answer_pty(int, struct sshbuf *);
129 int mm_answer_pty_cleanup(int, struct sshbuf *);
130 int mm_answer_term(int, struct sshbuf *);
131 int mm_answer_rsa_keyallowed(int, struct sshbuf *);
132 int mm_answer_rsa_challenge(int, struct sshbuf *);
133 int mm_answer_rsa_response(int, struct sshbuf *);
134 int mm_answer_sesskey(int, struct sshbuf *);
135 int mm_answer_sessid(int, struct sshbuf *);
138 int mm_answer_pam_start(int, struct sshbuf *);
139 int mm_answer_pam_account(int, struct sshbuf *);
140 int mm_answer_pam_init_ctx(int, struct sshbuf *);
141 int mm_answer_pam_query(int, struct sshbuf *);
142 int mm_answer_pam_respond(int, struct sshbuf *);
143 int mm_answer_pam_free_ctx(int, struct sshbuf *);
147 int mm_answer_gss_setup_ctx(int, struct sshbuf *);
148 int mm_answer_gss_accept_ctx(int, struct sshbuf *);
149 int mm_answer_gss_userok(int, struct sshbuf *);
150 int mm_answer_gss_checkmic(int, struct sshbuf *);
153 #ifdef SSH_AUDIT_EVENTS
154 int mm_answer_audit_event(int, struct sshbuf *);
155 int mm_answer_audit_command(int, struct sshbuf *);
158 static int monitor_read_log(struct monitor *);
160 static Authctxt *authctxt;
162 /* local state for key verify */
163 static u_char *key_blob = NULL;
164 static size_t key_bloblen = 0;
165 static int key_blobtype = MM_NOKEY;
166 static struct sshauthopt *key_opts = NULL;
167 static char *hostbased_cuser = NULL;
168 static char *hostbased_chost = NULL;
169 static char *auth_method = "unknown";
170 static char *auth_submethod = NULL;
171 static u_int session_id2_len = 0;
172 static u_char *session_id2 = NULL;
173 static pid_t monitor_child_pid;
176 enum monitor_reqtype type;
178 int (*f)(int, struct sshbuf *);
181 #define MON_ISAUTH 0x0004 /* Required for Authentication */
182 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
183 #define MON_ONCE 0x0010 /* Disable after calling */
184 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
186 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
188 #define MON_PERMIT 0x1000 /* Request is permitted */
190 struct mon_table mon_dispatch_proto20[] = {
192 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
194 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
195 #ifdef HAVE_LOGIN_CAP
196 {MONITOR_REQ_GETPWCLASS, MON_ISAUTH, mm_answer_login_getpwclass},
198 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
199 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
200 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
201 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
203 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
204 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
205 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
206 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
207 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
208 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
210 #ifdef SSH_AUDIT_EVENTS
211 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
214 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
215 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
217 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
218 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
220 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
221 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
222 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
223 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
228 struct mon_table mon_dispatch_postauth20[] = {
230 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
232 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
233 {MONITOR_REQ_PTY, 0, mm_answer_pty},
234 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
235 {MONITOR_REQ_TERM, 0, mm_answer_term},
236 #ifdef SSH_AUDIT_EVENTS
237 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
238 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
243 struct mon_table *mon_dispatch;
245 /* Specifies if a certain message is allowed at the moment */
247 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
249 while (ent->f != NULL) {
250 if (ent->type == type) {
251 ent->flags &= ~MON_PERMIT;
252 ent->flags |= permit ? MON_PERMIT : 0;
260 monitor_permit_authentications(int permit)
262 struct mon_table *ent = mon_dispatch;
264 while (ent->f != NULL) {
265 if (ent->flags & MON_AUTH) {
266 ent->flags &= ~MON_PERMIT;
267 ent->flags |= permit ? MON_PERMIT : 0;
274 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
276 struct ssh *ssh = active_state; /* XXX */
277 struct mon_table *ent;
278 int authenticated = 0, partial = 0;
280 debug3("preauth child monitor started");
282 if (pmonitor->m_recvfd >= 0)
283 close(pmonitor->m_recvfd);
284 if (pmonitor->m_log_sendfd >= 0)
285 close(pmonitor->m_log_sendfd);
286 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
288 authctxt = _authctxt;
289 memset(authctxt, 0, sizeof(*authctxt));
290 ssh->authctxt = authctxt;
292 authctxt->loginmsg = loginmsg;
294 mon_dispatch = mon_dispatch_proto20;
295 /* Permit requests for moduli and signatures */
296 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
297 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
299 /* The first few requests do not require asynchronous access */
300 while (!authenticated) {
302 auth_method = "unknown";
303 auth_submethod = NULL;
304 auth2_authctxt_reset_info(authctxt);
306 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
308 /* Special handling for multiple required authentications */
309 if (options.num_auth_methods != 0) {
311 !auth2_update_methods_lists(authctxt,
312 auth_method, auth_submethod)) {
313 debug3("%s: method %s: partial", __func__,
321 if (!(ent->flags & MON_AUTHDECIDE))
322 fatal("%s: unexpected authentication from %d",
323 __func__, ent->type);
324 if (authctxt->pw->pw_uid == 0 &&
325 !auth_root_allowed(ssh, auth_method))
328 /* PAM needs to perform account checks after auth */
329 if (options.use_pam && authenticated) {
332 if ((m = sshbuf_new()) == NULL)
333 fatal("%s: sshbuf_new failed",
335 mm_request_receive_expect(pmonitor->m_sendfd,
336 MONITOR_REQ_PAM_ACCOUNT, m);
337 authenticated = mm_answer_pam_account(
338 pmonitor->m_sendfd, m);
343 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
344 auth_log(authctxt, authenticated, partial,
345 auth_method, auth_submethod);
346 if (!partial && !authenticated)
347 authctxt->failures++;
348 if (authenticated || partial) {
349 auth2_update_session_info(authctxt,
350 auth_method, auth_submethod);
355 if (!authctxt->valid)
356 fatal("%s: authenticated invalid user", __func__);
357 if (strcmp(auth_method, "unknown") == 0)
358 fatal("%s: authentication method name unknown", __func__);
360 debug("%s: %s has been authenticated by privileged process",
361 __func__, authctxt->user);
362 ssh->authctxt = NULL;
363 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
365 mm_get_keystate(pmonitor);
367 /* Drain any buffered messages from the child */
368 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
371 if (pmonitor->m_recvfd >= 0)
372 close(pmonitor->m_recvfd);
373 if (pmonitor->m_log_sendfd >= 0)
374 close(pmonitor->m_log_sendfd);
375 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
379 monitor_set_child_handler(pid_t pid)
381 monitor_child_pid = pid;
385 monitor_child_handler(int sig)
387 kill(monitor_child_pid, sig);
391 monitor_child_postauth(struct monitor *pmonitor)
393 close(pmonitor->m_recvfd);
394 pmonitor->m_recvfd = -1;
396 monitor_set_child_handler(pmonitor->m_pid);
397 signal(SIGHUP, &monitor_child_handler);
398 signal(SIGTERM, &monitor_child_handler);
399 signal(SIGINT, &monitor_child_handler);
401 signal(SIGXFSZ, SIG_IGN);
404 mon_dispatch = mon_dispatch_postauth20;
406 /* Permit requests for moduli and signatures */
407 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
408 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
409 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
411 if (auth_opts->permit_pty_flag) {
412 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
413 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
417 monitor_read(pmonitor, mon_dispatch, NULL);
421 monitor_read_log(struct monitor *pmonitor)
423 struct sshbuf *logmsg;
429 if ((logmsg = sshbuf_new()) == NULL)
430 fatal("%s: sshbuf_new", __func__);
433 if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
434 fatal("%s: reserve: %s", __func__, ssh_err(r));
435 if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
436 if (errno == EPIPE) {
438 debug("%s: child log fd closed", __func__);
439 close(pmonitor->m_log_recvfd);
440 pmonitor->m_log_recvfd = -1;
443 fatal("%s: log fd read: %s", __func__, strerror(errno));
445 if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
446 fatal("%s: get len: %s", __func__, ssh_err(r));
447 if (len <= 4 || len > 8192)
448 fatal("%s: invalid log message length %u", __func__, len);
450 /* Read severity, message */
451 sshbuf_reset(logmsg);
452 if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
453 fatal("%s: reserve: %s", __func__, ssh_err(r));
454 if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
455 fatal("%s: log fd read: %s", __func__, strerror(errno));
456 if ((r = sshbuf_get_u32(logmsg, &level)) != 0 ||
457 (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
458 fatal("%s: decode: %s", __func__, ssh_err(r));
461 if (log_level_name(level) == NULL)
462 fatal("%s: invalid log level %u (corrupted message?)",
464 do_log2(level, "%s [preauth]", msg);
473 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
474 struct mon_table **pent)
479 struct pollfd pfd[2];
482 memset(&pfd, 0, sizeof(pfd));
483 pfd[0].fd = pmonitor->m_sendfd;
484 pfd[0].events = POLLIN;
485 pfd[1].fd = pmonitor->m_log_recvfd;
486 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
487 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
488 if (errno == EINTR || errno == EAGAIN)
490 fatal("%s: poll: %s", __func__, strerror(errno));
492 if (pfd[1].revents) {
494 * Drain all log messages before processing next
497 monitor_read_log(pmonitor);
501 break; /* Continues below */
504 if ((m = sshbuf_new()) == NULL)
505 fatal("%s: sshbuf_new", __func__);
507 mm_request_receive(pmonitor->m_sendfd, m);
508 if ((r = sshbuf_get_u8(m, &type)) != 0)
509 fatal("%s: decode: %s", __func__, ssh_err(r));
511 debug3("%s: checking request %d", __func__, type);
513 while (ent->f != NULL) {
514 if (ent->type == type)
519 if (ent->f != NULL) {
520 if (!(ent->flags & MON_PERMIT))
521 fatal("%s: unpermitted request %d", __func__,
523 ret = (*ent->f)(pmonitor->m_sendfd, m);
526 /* The child may use this request only once, disable it */
527 if (ent->flags & MON_ONCE) {
528 debug2("%s: %d used once, disabling now", __func__,
530 ent->flags &= ~MON_PERMIT;
539 fatal("%s: unsupported request: %d", __func__, type);
545 /* allowed key state */
547 monitor_allowed_key(u_char *blob, u_int bloblen)
549 /* make sure key is allowed */
550 if (key_blob == NULL || key_bloblen != bloblen ||
551 timingsafe_bcmp(key_blob, blob, key_bloblen))
557 monitor_reset_key_state(void)
561 free(hostbased_cuser);
562 free(hostbased_chost);
563 sshauthopt_free(key_opts);
566 key_blobtype = MM_NOKEY;
568 hostbased_cuser = NULL;
569 hostbased_chost = NULL;
574 mm_answer_moduli(int sock, struct sshbuf *m)
577 const BIGNUM *dh_p, *dh_g;
579 u_int min, want, max;
581 if ((r = sshbuf_get_u32(m, &min)) != 0 ||
582 (r = sshbuf_get_u32(m, &want)) != 0 ||
583 (r = sshbuf_get_u32(m, &max)) != 0)
584 fatal("%s: buffer error: %s", __func__, ssh_err(r));
586 debug3("%s: got parameters: %d %d %d",
587 __func__, min, want, max);
588 /* We need to check here, too, in case the child got corrupted */
589 if (max < min || want < min || max < want)
590 fatal("%s: bad parameters: %d %d %d",
591 __func__, min, want, max);
595 dh = choose_dh(min, want, max);
597 if ((r = sshbuf_put_u8(m, 0)) != 0)
598 fatal("%s: buffer error: %s", __func__, ssh_err(r));
601 /* Send first bignum */
602 DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
603 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
604 (r = sshbuf_put_bignum2(m, dh_p)) != 0 ||
605 (r = sshbuf_put_bignum2(m, dh_g)) != 0)
606 fatal("%s: buffer error: %s", __func__, ssh_err(r));
610 mm_request_send(sock, MONITOR_ANS_MODULI, m);
616 mm_answer_sign(int sock, struct sshbuf *m)
618 struct ssh *ssh = active_state; /* XXX */
619 extern int auth_sock; /* XXX move to state struct? */
621 struct sshbuf *sigbuf = NULL;
622 u_char *p = NULL, *signature = NULL;
624 size_t datlen, siglen, alglen;
627 const char proof_req[] = "hostkeys-prove-00@openssh.com";
629 debug3("%s", __func__);
631 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
632 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
633 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0 ||
634 (r = sshbuf_get_u32(m, &compat)) != 0)
635 fatal("%s: buffer error: %s", __func__, ssh_err(r));
637 fatal("%s: invalid key ID", __func__);
640 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
641 * SHA384 (48 bytes) and SHA512 (64 bytes).
643 * Otherwise, verify the signature request is for a hostkey
646 * XXX perform similar check for KEX signature requests too?
647 * it's not trivial, since what is signed is the hash, rather
648 * than the full kex structure...
650 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
652 * Construct expected hostkey proof and compare it to what
653 * the client sent us.
655 if (session_id2_len == 0) /* hostkeys is never first */
656 fatal("%s: bad data length: %zu", __func__, datlen);
657 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
658 fatal("%s: no hostkey for index %d", __func__, keyid);
659 if ((sigbuf = sshbuf_new()) == NULL)
660 fatal("%s: sshbuf_new", __func__);
661 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
662 (r = sshbuf_put_string(sigbuf, session_id2,
663 session_id2_len)) != 0 ||
664 (r = sshkey_puts(key, sigbuf)) != 0)
665 fatal("%s: couldn't prepare private key "
666 "proof buffer: %s", __func__, ssh_err(r));
667 if (datlen != sshbuf_len(sigbuf) ||
668 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
669 fatal("%s: bad data length: %zu, hostkey proof len %zu",
670 __func__, datlen, sshbuf_len(sigbuf));
675 /* save session id, it will be passed on the first call */
676 if (session_id2_len == 0) {
677 session_id2_len = datlen;
678 session_id2 = xmalloc(session_id2_len);
679 memcpy(session_id2, p, session_id2_len);
682 if ((key = get_hostkey_by_index(keyid)) != NULL) {
683 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
685 fatal("%s: sshkey_sign failed: %s",
686 __func__, ssh_err(r));
687 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
689 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
690 p, datlen, alg, compat)) != 0) {
691 fatal("%s: ssh_agent_sign failed: %s",
692 __func__, ssh_err(r));
695 fatal("%s: no hostkey from index %d", __func__, keyid);
697 debug3("%s: %s signature %p(%zu)", __func__,
698 is_proof ? "KEX" : "hostkey proof", signature, siglen);
701 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
702 fatal("%s: buffer error: %s", __func__, ssh_err(r));
708 mm_request_send(sock, MONITOR_ANS_SIGN, m);
710 /* Turn on permissions for getpwnam */
711 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
716 #ifdef HAVE_LOGIN_CAP
718 mm_answer_login_getpwclass(int sock, struct sshbuf *m)
725 debug3("%s", __func__);
727 pw = sshbuf_get_passwd(m);
729 fatal("%s: receive get struct passwd failed", __func__);
731 lc = login_getpwclass(pw);
736 if (r = sshbuf_put_u8(m, 0) != 0)
737 fatal("%s: buffer error: %s", __func__, ssh_err(r));
741 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
742 (r = sshbuf_put_cstring(m, lc->lc_class)) != 0 ||
743 (r = sshbuf_put_cstring(m, lc->lc_cap)) != 0 ||
744 (r = sshbuf_put_cstring(m, lc->lc_style)) != 0)
745 fatal("%s: buffer error: %s", __func__, ssh_err(r));
749 debug3("%s: sending MONITOR_ANS_GETPWCLASS", __func__);
750 mm_request_send(sock, MONITOR_ANS_GETPWCLASS, m);
752 sshbuf_free_passwd(pw);
758 /* Retrieves the password entry and also checks if the user is permitted */
761 mm_answer_pwnamallow(int sock, struct sshbuf *m)
763 struct ssh *ssh = active_state; /* XXX */
765 struct passwd *pwent;
769 debug3("%s", __func__);
771 if (authctxt->attempt++ != 0)
772 fatal("%s: multiple attempts for getpwnam", __func__);
774 if ((r = sshbuf_get_cstring(m, &username, NULL)) != 0)
775 fatal("%s: buffer error: %s", __func__, ssh_err(r));
777 pwent = getpwnamallow(username);
779 authctxt->user = xstrdup(username);
780 setproctitle("%s [priv]", pwent ? username : "unknown");
786 if ((r = sshbuf_put_u8(m, 0)) != 0)
787 fatal("%s: buffer error: %s", __func__, ssh_err(r));
788 authctxt->pw = fakepw();
793 authctxt->pw = pwent;
796 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
797 (r = sshbuf_put_passwd(m, pwent)) != 0)
798 fatal("%s: buffer error: %s", __func__, ssh_err(r));
801 ssh_packet_set_log_preamble(ssh, "%suser %s",
802 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
803 if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
804 fatal("%s: buffer error: %s", __func__, ssh_err(r));
806 #define M_CP_STROPT(x) do { \
807 if (options.x != NULL) { \
808 if ((r = sshbuf_put_cstring(m, options.x)) != 0) \
809 fatal("%s: buffer error: %s", \
810 __func__, ssh_err(r)); \
813 #define M_CP_STRARRAYOPT(x, nx) do { \
814 for (i = 0; i < options.nx; i++) { \
815 if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
816 fatal("%s: buffer error: %s", \
817 __func__, ssh_err(r)); \
820 /* See comment in servconf.h */
821 COPY_MATCH_STRING_OPTS();
823 #undef M_CP_STRARRAYOPT
825 /* Create valid auth method lists */
826 if (auth2_setup_methods_lists(authctxt) != 0) {
828 * The monitor will continue long enough to let the child
829 * run to it's packet_disconnect(), but it must not allow any
830 * authentication to succeed.
832 debug("%s: no valid authentication method lists", __func__);
835 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
836 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
838 /* Allow service/style information on the auth context */
839 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
840 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
844 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
850 int mm_answer_auth2_read_banner(int sock, struct sshbuf *m)
856 banner = auth2_read_banner();
857 if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
858 fatal("%s: buffer error: %s", __func__, ssh_err(r));
859 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
866 mm_answer_authserv(int sock, struct sshbuf *m)
870 monitor_permit_authentications(1);
872 if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
873 (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
874 fatal("%s: buffer error: %s", __func__, ssh_err(r));
875 debug3("%s: service=%s, style=%s",
876 __func__, authctxt->service, authctxt->style);
878 if (strlen(authctxt->style) == 0) {
879 free(authctxt->style);
880 authctxt->style = NULL;
887 mm_answer_authpassword(int sock, struct sshbuf *m)
889 struct ssh *ssh = active_state; /* XXX */
890 static int call_count;
892 int r, authenticated;
895 if (!options.password_authentication)
896 fatal("%s: password authentication not enabled", __func__);
897 if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
898 fatal("%s: buffer error: %s", __func__, ssh_err(r));
899 /* Only authenticate if the context is valid */
900 authenticated = options.password_authentication &&
901 auth_password(ssh, passwd);
902 explicit_bzero(passwd, plen);
906 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
907 fatal("%s: buffer error: %s", __func__, ssh_err(r));
909 if ((r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0)
910 fatal("%s: buffer error: %s", __func__, ssh_err(r));
913 debug3("%s: sending result %d", __func__, authenticated);
914 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
917 if (plen == 0 && call_count == 1)
918 auth_method = "none";
920 auth_method = "password";
922 /* Causes monitor loop to terminate if authenticated */
923 return (authenticated);
928 mm_answer_bsdauthquery(int sock, struct sshbuf *m)
930 char *name, *infotxt;
931 u_int numprompts, *echo_on, success;
935 if (!options.kbd_interactive_authentication)
936 fatal("%s: kbd-int authentication not enabled", __func__);
937 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
938 &prompts, &echo_on) < 0 ? 0 : 1;
941 if ((r = sshbuf_put_u32(m, success)) != 0)
942 fatal("%s: buffer error: %s", __func__, ssh_err(r));
944 if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
945 fatal("%s: buffer error: %s", __func__, ssh_err(r));
948 debug3("%s: sending challenge success: %u", __func__, success);
949 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
962 mm_answer_bsdauthrespond(int sock, struct sshbuf *m)
967 if (!options.kbd_interactive_authentication)
968 fatal("%s: kbd-int authentication not enabled", __func__);
969 if (authctxt->as == NULL)
970 fatal("%s: no bsd auth session", __func__);
972 if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
973 fatal("%s: buffer error: %s", __func__, ssh_err(r));
974 authok = options.challenge_response_authentication &&
975 auth_userresponse(authctxt->as, response, 0);
977 debug3("%s: <%s> = <%d>", __func__, response, authok);
981 if ((r = sshbuf_put_u32(m, authok)) != 0)
982 fatal("%s: buffer error: %s", __func__, ssh_err(r));
984 debug3("%s: sending authenticated: %d", __func__, authok);
985 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
987 auth_method = "keyboard-interactive";
988 auth_submethod = "bsdauth";
990 return (authok != 0);
996 mm_answer_pam_start(int sock, struct sshbuf *m)
998 if (!options.use_pam)
999 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1001 start_pam(authctxt);
1003 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1004 if (options.kbd_interactive_authentication)
1005 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
1011 mm_answer_pam_account(int sock, struct sshbuf *m)
1016 if (!options.use_pam)
1017 fatal("%s: PAM not enabled", __func__);
1019 ret = do_pam_account();
1021 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1022 (r = sshbuf_put_stringb(m, loginmsg)) != 0)
1023 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1025 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
1030 static void *sshpam_ctxt, *sshpam_authok;
1031 extern KbdintDevice sshpam_device;
1034 mm_answer_pam_init_ctx(int sock, struct sshbuf *m)
1039 debug3("%s", __func__);
1040 if (!options.kbd_interactive_authentication)
1041 fatal("%s: kbd-int authentication not enabled", __func__);
1042 if (sshpam_ctxt != NULL)
1043 fatal("%s: already called", __func__);
1044 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1045 sshpam_authok = NULL;
1047 if (sshpam_ctxt != NULL) {
1048 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1049 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
1052 if ((r = sshbuf_put_u32(m, ok)) != 0)
1053 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1054 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
1059 mm_answer_pam_query(int sock, struct sshbuf *m)
1061 char *name = NULL, *info = NULL, **prompts = NULL;
1062 u_int i, num = 0, *echo_on = 0;
1065 debug3("%s", __func__);
1066 sshpam_authok = NULL;
1067 if (sshpam_ctxt == NULL)
1068 fatal("%s: no context", __func__);
1069 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1070 &num, &prompts, &echo_on);
1071 if (ret == 0 && num == 0)
1072 sshpam_authok = sshpam_ctxt;
1073 if (num > 1 || name == NULL || info == NULL)
1074 fatal("sshpam_device.query failed");
1075 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
1077 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1078 (r = sshbuf_put_cstring(m, name)) != 0 ||
1079 (r = sshbuf_put_cstring(m, info)) != 0 ||
1080 (r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0 ||
1081 (r = sshbuf_put_u32(m, num)) != 0)
1082 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1085 for (i = 0; i < num; ++i) {
1086 if ((r = sshbuf_put_cstring(m, prompts[i])) != 0 ||
1087 (r = sshbuf_put_u32(m, echo_on[i])) != 0)
1088 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1093 auth_method = "keyboard-interactive";
1094 auth_submethod = "pam";
1095 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
1100 mm_answer_pam_respond(int sock, struct sshbuf *m)
1106 debug3("%s", __func__);
1107 if (sshpam_ctxt == NULL)
1108 fatal("%s: no context", __func__);
1109 sshpam_authok = NULL;
1110 if ((r = sshbuf_get_u32(m, &num)) != 0)
1111 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1113 resp = xcalloc(num, sizeof(char *));
1114 for (i = 0; i < num; ++i) {
1115 if ((r = sshbuf_get_cstring(m, &(resp[i]), NULL)) != 0)
1116 fatal("%s: buffer error: %s",
1117 __func__, ssh_err(r));
1119 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1120 for (i = 0; i < num; ++i)
1124 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1127 if ((r = sshbuf_put_u32(m, ret)) != 0)
1128 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1129 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1130 auth_method = "keyboard-interactive";
1131 auth_submethod = "pam";
1133 sshpam_authok = sshpam_ctxt;
1138 mm_answer_pam_free_ctx(int sock, struct sshbuf *m)
1140 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
1142 debug3("%s", __func__);
1143 if (sshpam_ctxt == NULL)
1144 fatal("%s: no context", __func__);
1145 (sshpam_device.free_ctx)(sshpam_ctxt);
1146 sshpam_ctxt = sshpam_authok = NULL;
1148 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1149 /* Allow another attempt */
1150 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
1151 auth_method = "keyboard-interactive";
1152 auth_submethod = "pam";
1158 mm_answer_keyallowed(int sock, struct sshbuf *m)
1160 struct ssh *ssh = active_state; /* XXX */
1161 struct sshkey *key = NULL;
1162 char *cuser, *chost;
1163 u_int pubkey_auth_attempt;
1164 enum mm_keytype type = 0;
1166 struct sshauthopt *opts = NULL;
1168 debug3("%s entering", __func__);
1169 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1170 (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
1171 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
1172 (r = sshkey_froms(m, &key)) != 0 ||
1173 (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
1174 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1176 debug3("%s: key_from_blob: %p", __func__, key);
1178 if (key != NULL && authctxt->valid) {
1179 /* These should not make it past the privsep child */
1180 if (sshkey_type_plain(key->type) == KEY_RSA &&
1181 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1182 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1186 auth_method = "publickey";
1187 if (!options.pubkey_authentication)
1189 if (auth2_key_already_used(authctxt, key))
1191 if (match_pattern_list(sshkey_ssh_name(key),
1192 options.pubkey_key_types, 0) != 1)
1194 allowed = user_key_allowed(ssh, authctxt->pw, key,
1195 pubkey_auth_attempt, &opts);
1198 auth_method = "hostbased";
1199 if (!options.hostbased_authentication)
1201 if (auth2_key_already_used(authctxt, key))
1203 if (match_pattern_list(sshkey_ssh_name(key),
1204 options.hostbased_key_types, 0) != 1)
1206 allowed = hostbased_key_allowed(authctxt->pw,
1208 auth2_record_info(authctxt,
1209 "client user \"%.100s\", client host \"%.100s\"",
1213 fatal("%s: unknown key type %d", __func__, type);
1218 debug3("%s: %s authentication%s: %s key is %s", __func__,
1219 auth_method, pubkey_auth_attempt ? "" : " test",
1220 (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
1221 allowed ? "allowed" : "not allowed");
1223 auth2_record_key(authctxt, 0, key);
1225 /* clear temporarily storage (used by verify) */
1226 monitor_reset_key_state();
1229 /* Save temporarily for comparison in verify */
1230 if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
1231 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1232 key_blobtype = type;
1234 hostbased_cuser = cuser;
1235 hostbased_chost = chost;
1237 /* Log failed attempt */
1238 auth_log(authctxt, 0, 0, auth_method, NULL);
1245 if ((r = sshbuf_put_u32(m, allowed)) != 0)
1246 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1247 if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
1248 fatal("%s: sshauthopt_serialise: %s", __func__, ssh_err(r));
1249 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1252 sshauthopt_free(opts);
1258 monitor_valid_userblob(u_char *data, u_int datalen)
1262 char *userstyle, *cp;
1267 if ((b = sshbuf_new()) == NULL)
1268 fatal("%s: sshbuf_new", __func__);
1269 if ((r = sshbuf_put(b, data, datalen)) != 0)
1270 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1272 if (datafellows & SSH_OLD_SESSIONID) {
1274 len = sshbuf_len(b);
1275 if ((session_id2 == NULL) ||
1276 (len < session_id2_len) ||
1277 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1279 if ((r = sshbuf_consume(b, session_id2_len)) != 0)
1280 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1282 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1283 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1284 if ((session_id2 == NULL) ||
1285 (len != session_id2_len) ||
1286 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1289 if ((r = sshbuf_get_u8(b, &type)) != 0)
1290 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1291 if (type != SSH2_MSG_USERAUTH_REQUEST)
1293 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1294 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1295 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1296 authctxt->style ? ":" : "",
1297 authctxt->style ? authctxt->style : "");
1298 if (strcmp(userstyle, cp) != 0) {
1299 logit("wrong user name passed to monitor: "
1300 "expected %s != %.100s", userstyle, cp);
1305 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1306 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1307 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1308 if (strcmp("publickey", cp) != 0)
1311 if ((r = sshbuf_get_u8(b, &type)) != 0)
1312 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1315 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1316 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1317 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1318 if (sshbuf_len(b) != 0)
1325 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1330 char *cp, *userstyle;
1335 if ((b = sshbuf_new()) == NULL)
1336 fatal("%s: sshbuf_new", __func__);
1337 if ((r = sshbuf_put(b, data, datalen)) != 0 ||
1338 (r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1339 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1341 if ((session_id2 == NULL) ||
1342 (len != session_id2_len) ||
1343 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1346 if ((r = sshbuf_get_u8(b, &type)) != 0)
1347 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1348 if (type != SSH2_MSG_USERAUTH_REQUEST)
1350 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1351 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1352 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1353 authctxt->style ? ":" : "",
1354 authctxt->style ? authctxt->style : "");
1355 if (strcmp(userstyle, cp) != 0) {
1356 logit("wrong user name passed to monitor: "
1357 "expected %s != %.100s", userstyle, cp);
1362 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1363 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1364 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1365 if (strcmp(cp, "hostbased") != 0)
1368 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1369 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1370 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1372 /* verify client host, strip trailing dot if necessary */
1373 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1374 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1375 if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
1377 if (strcmp(cp, chost) != 0)
1381 /* verify client user */
1382 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1383 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1384 if (strcmp(cp, cuser) != 0)
1388 if (sshbuf_len(b) != 0)
1395 mm_answer_keyverify(int sock, struct sshbuf *m)
1397 struct ssh *ssh = active_state; /* XXX */
1399 u_char *signature, *data, *blob;
1401 size_t signaturelen, datalen, bloblen;
1402 int r, ret, valid_data = 0, encoded_ret;
1404 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1405 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
1406 (r = sshbuf_get_string(m, &data, &datalen)) != 0 ||
1407 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
1408 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1410 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1411 !monitor_allowed_key(blob, bloblen))
1412 fatal("%s: bad key, not previously allowed", __func__);
1414 /* Empty signature algorithm means NULL. */
1415 if (*sigalg == '\0') {
1420 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1421 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1422 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
1424 switch (key_blobtype) {
1426 valid_data = monitor_valid_userblob(data, datalen);
1427 auth_method = "publickey";
1430 valid_data = monitor_valid_hostbasedblob(data, datalen,
1431 hostbased_cuser, hostbased_chost);
1432 auth_method = "hostbased";
1439 fatal("%s: bad signature data blob", __func__);
1441 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
1442 sigalg, active_state->compat);
1443 debug3("%s: %s %p signature %s", __func__, auth_method, key,
1444 (ret == 0) ? "verified" : "unverified");
1445 auth2_record_key(authctxt, ret == 0, key);
1452 if (key_blobtype == MM_USERKEY)
1453 auth_activate_options(ssh, key_opts);
1454 monitor_reset_key_state();
1459 /* encode ret != 0 as positive integer, since we're sending u32 */
1460 encoded_ret = (ret != 0);
1461 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1462 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1463 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1469 mm_record_login(Session *s, struct passwd *pw)
1471 struct ssh *ssh = active_state; /* XXX */
1473 struct sockaddr_storage from;
1476 * Get IP address of client. If the connection is not a socket, let
1477 * the address be 0.0.0.0.
1479 memset(&from, 0, sizeof(from));
1480 fromlen = sizeof(from);
1481 if (packet_connection_is_on_socket()) {
1482 if (getpeername(packet_get_connection_in(),
1483 (struct sockaddr *)&from, &fromlen) < 0) {
1484 debug("getpeername: %.100s", strerror(errno));
1488 /* Record that there was a login on that tty from the remote host. */
1489 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1490 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
1491 (struct sockaddr *)&from, fromlen);
1495 mm_session_close(Session *s)
1497 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1498 if (s->ttyfd != -1) {
1499 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1500 session_pty_cleanup2(s);
1502 session_unused(s->self);
1506 mm_answer_pty(int sock, struct sshbuf *m)
1508 extern struct monitor *pmonitor;
1512 debug3("%s entering", __func__);
1518 s->authctxt = authctxt;
1519 s->pw = authctxt->pw;
1520 s->pid = pmonitor->m_pid;
1521 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1524 pty_setowner(authctxt->pw, s->tty);
1526 if ((r = sshbuf_put_u32(m, 1)) != 0 ||
1527 (r = sshbuf_put_cstring(m, s->tty)) != 0)
1528 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1530 /* We need to trick ttyslot */
1531 if (dup2(s->ttyfd, 0) == -1)
1532 fatal("%s: dup2", __func__);
1534 mm_record_login(s, authctxt->pw);
1536 /* Now we can close the file descriptor again */
1539 /* send messages generated by record_login */
1540 if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
1541 fatal("%s: put login message: %s", __func__, ssh_err(r));
1542 sshbuf_reset(loginmsg);
1544 mm_request_send(sock, MONITOR_ANS_PTY, m);
1546 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1547 mm_send_fd(sock, s->ttyfd) == -1)
1548 fatal("%s: send fds failed", __func__);
1550 /* make sure nothing uses fd 0 */
1551 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1552 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1554 error("%s: fd0 %d != 0", __func__, fd0);
1556 /* slave is not needed */
1558 s->ttyfd = s->ptyfd;
1559 /* no need to dup() because nobody closes ptyfd */
1560 s->ptymaster = s->ptyfd;
1562 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1568 mm_session_close(s);
1569 if ((r = sshbuf_put_u32(m, 0)) != 0)
1570 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1571 mm_request_send(sock, MONITOR_ANS_PTY, m);
1576 mm_answer_pty_cleanup(int sock, struct sshbuf *m)
1582 debug3("%s entering", __func__);
1584 if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
1585 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1586 if ((s = session_by_tty(tty)) != NULL)
1587 mm_session_close(s);
1594 mm_answer_term(int sock, struct sshbuf *req)
1596 struct ssh *ssh = active_state; /* XXX */
1597 extern struct monitor *pmonitor;
1600 debug3("%s: tearing down sessions", __func__);
1602 /* The child is terminating */
1603 session_destroy_all(ssh, &mm_session_close);
1606 if (options.use_pam)
1610 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1614 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1616 /* Terminate process */
1620 #ifdef SSH_AUDIT_EVENTS
1621 /* Report that an audit event occurred */
1623 mm_answer_audit_event(int socket, struct sshbuf *m)
1626 ssh_audit_event_t event;
1629 debug3("%s entering", __func__);
1631 if ((r = sshbuf_get_u32(m, &n)) != 0)
1632 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1633 event = (ssh_audit_event_t)n;
1635 case SSH_AUTH_FAIL_PUBKEY:
1636 case SSH_AUTH_FAIL_HOSTBASED:
1637 case SSH_AUTH_FAIL_GSSAPI:
1638 case SSH_LOGIN_EXCEED_MAXTRIES:
1639 case SSH_LOGIN_ROOT_DENIED:
1640 case SSH_CONNECTION_CLOSE:
1641 case SSH_INVALID_USER:
1645 fatal("Audit event type %d not permitted", event);
1652 mm_answer_audit_command(int socket, struct sshbuf *m)
1657 debug3("%s entering", __func__);
1658 if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
1659 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1660 /* sanity check command, if so how? */
1661 audit_run_command(cmd);
1665 #endif /* SSH_AUDIT_EVENTS */
1668 monitor_clear_keystate(struct monitor *pmonitor)
1670 struct ssh *ssh = active_state; /* XXX */
1672 ssh_clear_newkeys(ssh, MODE_IN);
1673 ssh_clear_newkeys(ssh, MODE_OUT);
1674 sshbuf_free(child_state);
1679 monitor_apply_keystate(struct monitor *pmonitor)
1681 struct ssh *ssh = active_state; /* XXX */
1685 debug3("%s: packet_set_state", __func__);
1686 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1687 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1688 sshbuf_free(child_state);
1691 if ((kex = ssh->kex) != NULL) {
1692 /* XXX set callbacks */
1694 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1695 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1696 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1697 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1698 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
1699 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1700 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1701 # ifdef OPENSSL_HAS_ECC
1702 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1704 #endif /* WITH_OPENSSL */
1705 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1706 kex->load_host_public_key=&get_hostkey_public_by_type;
1707 kex->load_host_private_key=&get_hostkey_private_by_type;
1708 kex->host_key_index=&get_hostkey_index;
1709 kex->sign = sshd_hostkey_sign;
1713 /* This function requries careful sanity checking */
1716 mm_get_keystate(struct monitor *pmonitor)
1718 debug3("%s: Waiting for new keys", __func__);
1720 if ((child_state = sshbuf_new()) == NULL)
1721 fatal("%s: sshbuf_new failed", __func__);
1722 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1724 debug3("%s: GOT new keys", __func__);
1730 #define FD_CLOSEONEXEC(x) do { \
1731 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1732 fatal("fcntl(%d, F_SETFD)", x); \
1736 monitor_openfds(struct monitor *mon, int do_logfds)
1743 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1744 fatal("%s: socketpair: %s", __func__, strerror(errno));
1746 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1747 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1748 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1749 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1751 FD_CLOSEONEXEC(pair[0]);
1752 FD_CLOSEONEXEC(pair[1]);
1753 mon->m_recvfd = pair[0];
1754 mon->m_sendfd = pair[1];
1757 if (pipe(pair) == -1)
1758 fatal("%s: pipe: %s", __func__, strerror(errno));
1759 FD_CLOSEONEXEC(pair[0]);
1760 FD_CLOSEONEXEC(pair[1]);
1761 mon->m_log_recvfd = pair[0];
1762 mon->m_log_sendfd = pair[1];
1764 mon->m_log_recvfd = mon->m_log_sendfd = -1;
1767 #define MM_MEMSIZE 65536
1772 struct monitor *mon;
1774 mon = xcalloc(1, sizeof(*mon));
1775 monitor_openfds(mon, 1);
1781 monitor_reinit(struct monitor *mon)
1783 monitor_openfds(mon, 0);
1788 mm_answer_gss_setup_ctx(int sock, struct sshbuf *m)
1796 if (!options.gss_authentication)
1797 fatal("%s: GSSAPI authentication not enabled", __func__);
1799 if ((r = sshbuf_get_string(m, &p, &len)) != 0)
1800 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1804 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1806 free(goid.elements);
1809 if ((r = sshbuf_put_u32(m, major)) != 0)
1810 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1812 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1814 /* Now we have a context, enable the step */
1815 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1821 mm_answer_gss_accept_ctx(int sock, struct sshbuf *m)
1824 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1825 OM_uint32 major, minor;
1826 OM_uint32 flags = 0; /* GSI needs this */
1829 if (!options.gss_authentication)
1830 fatal("%s: GSSAPI authentication not enabled", __func__);
1832 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
1833 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1834 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1838 if ((r = sshbuf_put_u32(m, major)) != 0 ||
1839 (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
1840 (r = sshbuf_put_u32(m, flags)) != 0)
1841 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1842 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1844 gss_release_buffer(&minor, &out);
1846 if (major == GSS_S_COMPLETE) {
1847 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1848 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1849 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1855 mm_answer_gss_checkmic(int sock, struct sshbuf *m)
1857 gss_buffer_desc gssbuf, mic;
1861 if (!options.gss_authentication)
1862 fatal("%s: GSSAPI authentication not enabled", __func__);
1864 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
1865 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
1866 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1868 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1874 if ((r = sshbuf_put_u32(m, ret)) != 0)
1875 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1877 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1879 if (!GSS_ERROR(ret))
1880 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1886 mm_answer_gss_userok(int sock, struct sshbuf *m)
1888 int r, authenticated;
1889 const char *displayname;
1891 if (!options.gss_authentication)
1892 fatal("%s: GSSAPI authentication not enabled", __func__);
1894 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1897 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1898 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1900 debug3("%s: sending result %d", __func__, authenticated);
1901 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1903 auth_method = "gssapi-with-mic";
1905 if ((displayname = ssh_gssapi_displayname()) != NULL)
1906 auth2_record_info(authctxt, "%s", displayname);
1908 /* Monitor loop will terminate if authenticated */
1909 return (authenticated);