1 /* $OpenBSD: monitor_wrap.c,v 1.89 2016/08/13 17:47:41 markus Exp $ */
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
42 #include <openssl/bn.h>
43 #include <openssl/dh.h>
44 #include <openssl/evp.h>
47 #include "openbsd-compat/sys-queue.h"
59 #include "auth-options.h"
64 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
67 #define TARGET_OS_MAC 1
75 #include "monitor_wrap.h"
77 #include "monitor_fdpass.h"
88 extern z_stream incoming_stream;
89 extern z_stream outgoing_stream;
90 extern struct monitor *pmonitor;
91 extern Buffer loginmsg;
92 extern ServerOptions options;
95 mm_log_handler(LogLevel level, const char *msg, void *ctx)
98 struct monitor *mon = (struct monitor *)ctx;
100 if (mon->m_log_sendfd == -1)
101 fatal("%s: no log channel", __func__);
103 buffer_init(&log_msg);
105 * Placeholder for packet length. Will be filled in with the actual
106 * packet length once the packet has been constucted. This saves
109 buffer_put_int(&log_msg, 0);
111 buffer_put_int(&log_msg, level);
112 buffer_put_cstring(&log_msg, msg);
113 put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4);
114 if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg),
115 buffer_len(&log_msg)) != buffer_len(&log_msg))
116 fatal("%s: write: %s", __func__, strerror(errno));
117 buffer_free(&log_msg);
124 * m_pid is only set in the privileged part, and
125 * points to the unprivileged child.
127 return (pmonitor && pmonitor->m_pid > 0);
131 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
133 u_int mlen = buffer_len(m);
136 debug3("%s entering: type %d", __func__, type);
138 put_u32(buf, mlen + 1);
139 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
140 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
141 fatal("%s: write: %s", __func__, strerror(errno));
142 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
143 fatal("%s: write: %s", __func__, strerror(errno));
147 mm_request_receive(int sock, Buffer *m)
152 debug3("%s entering", __func__);
154 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
157 fatal("%s: read: %s", __func__, strerror(errno));
159 msg_len = get_u32(buf);
160 if (msg_len > 256 * 1024)
161 fatal("%s: read: bad msg_len %d", __func__, msg_len);
163 buffer_append_space(m, msg_len);
164 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
165 fatal("%s: read: %s", __func__, strerror(errno));
169 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
173 debug3("%s entering: type %d", __func__, type);
175 mm_request_receive(sock, m);
176 rtype = buffer_get_char(m);
178 fatal("%s: read: rtype %d != type %d", __func__,
184 mm_choose_dh(int min, int nbits, int max)
191 buffer_put_int(&m, min);
192 buffer_put_int(&m, nbits);
193 buffer_put_int(&m, max);
195 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
197 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
198 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
200 success = buffer_get_char(&m);
202 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
204 if ((p = BN_new()) == NULL)
205 fatal("%s: BN_new failed", __func__);
206 if ((g = BN_new()) == NULL)
207 fatal("%s: BN_new failed", __func__);
208 buffer_get_bignum2(&m, p);
209 buffer_get_bignum2(&m, g);
211 debug3("%s: remaining %d", __func__, buffer_len(&m));
214 return (dh_new_group(g, p));
219 mm_key_sign(Key *key, u_char **sigp, u_int *lenp,
220 const u_char *data, u_int datalen, const char *hostkey_alg)
222 struct kex *kex = *pmonitor->m_pkex;
225 debug3("%s entering", __func__);
228 buffer_put_int(&m, kex->host_key_index(key, 0, active_state));
229 buffer_put_string(&m, data, datalen);
230 buffer_put_cstring(&m, hostkey_alg);
232 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
234 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
235 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
236 *sigp = buffer_get_string(&m, lenp);
243 mm_getpwnamallow(const char *username)
248 ServerOptions *newopts;
250 debug3("%s entering", __func__);
253 buffer_put_cstring(&m, username);
255 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
257 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
258 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
260 if (buffer_get_char(&m) == 0) {
264 pw = buffer_get_string(&m, &len);
265 if (len != sizeof(struct passwd))
266 fatal("%s: struct passwd size mismatch", __func__);
267 pw->pw_name = buffer_get_string(&m, NULL);
268 pw->pw_passwd = buffer_get_string(&m, NULL);
269 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
270 pw->pw_gecos = buffer_get_string(&m, NULL);
272 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
273 pw->pw_class = buffer_get_string(&m, NULL);
275 pw->pw_dir = buffer_get_string(&m, NULL);
276 pw->pw_shell = buffer_get_string(&m, NULL);
279 /* copy options block as a Match directive may have changed some */
280 newopts = buffer_get_string(&m, &len);
281 if (len != sizeof(*newopts))
282 fatal("%s: option block size mismatch", __func__);
284 #define M_CP_STROPT(x) do { \
285 if (newopts->x != NULL) \
286 newopts->x = buffer_get_string(&m, NULL); \
288 #define M_CP_STRARRAYOPT(x, nx) do { \
289 for (i = 0; i < newopts->nx; i++) \
290 newopts->x[i] = buffer_get_string(&m, NULL); \
292 /* See comment in servconf.h */
293 COPY_MATCH_STRING_OPTS();
295 #undef M_CP_STRARRAYOPT
297 copy_set_server_options(&options, newopts, 1);
306 mm_auth2_read_banner(void)
311 debug3("%s entering", __func__);
314 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
317 mm_request_receive_expect(pmonitor->m_recvfd,
318 MONITOR_ANS_AUTH2_READ_BANNER, &m);
319 banner = buffer_get_string(&m, NULL);
322 /* treat empty banner as missing banner */
323 if (strlen(banner) == 0) {
330 /* Inform the privileged process about service and style */
333 mm_inform_authserv(char *service, char *style)
337 debug3("%s entering", __func__);
340 buffer_put_cstring(&m, service);
341 buffer_put_cstring(&m, style ? style : "");
343 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
348 /* Do the password authentication */
350 mm_auth_password(Authctxt *authctxt, char *password)
353 int authenticated = 0;
355 debug3("%s entering", __func__);
358 buffer_put_cstring(&m, password);
359 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
361 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
362 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
364 authenticated = buffer_get_int(&m);
366 sshpam_set_maxtries_reached(buffer_get_int(&m));
371 debug3("%s: user %sauthenticated",
372 __func__, authenticated ? "" : "not ");
373 return (authenticated);
377 mm_user_key_allowed(struct passwd *pw, Key *key, int pubkey_auth_attempt)
379 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
380 pubkey_auth_attempt));
384 mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host,
387 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0));
391 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
392 Key *key, int pubkey_auth_attempt)
397 int allowed = 0, have_forced = 0;
399 debug3("%s entering", __func__);
401 /* Convert the key to a blob and the pass it over */
402 if (!key_to_blob(key, &blob, &len))
406 buffer_put_int(&m, type);
407 buffer_put_cstring(&m, user ? user : "");
408 buffer_put_cstring(&m, host ? host : "");
409 buffer_put_string(&m, blob, len);
410 buffer_put_int(&m, pubkey_auth_attempt);
413 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
415 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
416 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
418 allowed = buffer_get_int(&m);
420 /* fake forced command */
421 auth_clear_options();
422 have_forced = buffer_get_int(&m);
423 forced_command = have_forced ? xstrdup("true") : NULL;
431 * This key verify needs to send the key type along, because the
432 * privileged parent makes the decision if the key is allowed
433 * for authentication.
437 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
444 debug3("%s entering", __func__);
446 /* Convert the key to a blob and the pass it over */
447 if (!key_to_blob(key, &blob, &len))
451 buffer_put_string(&m, blob, len);
452 buffer_put_string(&m, sig, siglen);
453 buffer_put_string(&m, data, datalen);
456 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
458 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
459 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
461 verified = buffer_get_int(&m);
469 mm_send_keystate(struct monitor *monitor)
471 struct ssh *ssh = active_state; /* XXX */
475 if ((m = sshbuf_new()) == NULL)
476 fatal("%s: sshbuf_new failed", __func__);
477 if ((r = ssh_packet_get_state(ssh, m)) != 0)
478 fatal("%s: get_state failed: %s",
479 __func__, ssh_err(r));
480 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
481 debug3("%s: Finished sending state", __func__);
486 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
490 int success = 0, tmp1 = -1, tmp2 = -1;
492 /* Kludge: ensure there are fds free to receive the pty/tty */
493 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
494 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
495 error("%s: cannot allocate fds for pty", __func__);
506 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
508 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
509 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
511 success = buffer_get_int(&m);
513 debug3("%s: pty alloc failed", __func__);
517 p = buffer_get_string(&m, NULL);
518 msg = buffer_get_string(&m, NULL);
521 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
524 buffer_append(&loginmsg, msg, strlen(msg));
527 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
528 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
529 fatal("%s: receive fds failed", __func__);
536 mm_session_pty_cleanup2(Session *s)
543 buffer_put_cstring(&m, s->tty);
544 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
547 /* closed dup'ed master */
548 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
549 error("close(s->ptymaster/%d): %s",
550 s->ptymaster, strerror(errno));
552 /* unlink pty from session */
558 mm_start_pam(Authctxt *authctxt)
562 debug3("%s entering", __func__);
563 if (!options.use_pam)
564 fatal("UsePAM=no, but ended up in %s anyway", __func__);
567 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
573 mm_do_pam_account(void)
579 debug3("%s entering", __func__);
580 if (!options.use_pam)
581 fatal("UsePAM=no, but ended up in %s anyway", __func__);
584 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
586 mm_request_receive_expect(pmonitor->m_recvfd,
587 MONITOR_ANS_PAM_ACCOUNT, &m);
588 ret = buffer_get_int(&m);
589 msg = buffer_get_string(&m, NULL);
590 buffer_append(&loginmsg, msg, strlen(msg));
595 debug3("%s returning %d", __func__, ret);
601 mm_sshpam_init_ctx(Authctxt *authctxt)
606 debug3("%s", __func__);
608 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
609 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
610 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
611 success = buffer_get_int(&m);
613 debug3("%s: pam_init_ctx failed", __func__);
622 mm_sshpam_query(void *ctx, char **name, char **info,
623 u_int *num, char ***prompts, u_int **echo_on)
629 debug3("%s", __func__);
631 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
632 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
633 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
634 ret = buffer_get_int(&m);
635 debug3("%s: pam_query returned %d", __func__, ret);
636 *name = buffer_get_string(&m, NULL);
637 *info = buffer_get_string(&m, NULL);
638 sshpam_set_maxtries_reached(buffer_get_int(&m));
639 *num = buffer_get_int(&m);
640 if (*num > PAM_MAX_NUM_MSG)
641 fatal("%s: recieved %u PAM messages, expected <= %u",
642 __func__, *num, PAM_MAX_NUM_MSG);
643 *prompts = xcalloc((*num + 1), sizeof(char *));
644 *echo_on = xcalloc((*num + 1), sizeof(u_int));
645 for (i = 0; i < *num; ++i) {
646 (*prompts)[i] = buffer_get_string(&m, NULL);
647 (*echo_on)[i] = buffer_get_int(&m);
654 mm_sshpam_respond(void *ctx, u_int num, char **resp)
660 debug3("%s", __func__);
662 buffer_put_int(&m, num);
663 for (i = 0; i < num; ++i)
664 buffer_put_cstring(&m, resp[i]);
665 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
666 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
667 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
668 ret = buffer_get_int(&m);
669 debug3("%s: pam_respond returned %d", __func__, ret);
675 mm_sshpam_free_ctx(void *ctxtp)
679 debug3("%s", __func__);
681 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
682 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
683 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
688 /* Request process termination */
696 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
701 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
702 char ***prompts, u_int **echo_on)
705 *infotxt = xstrdup("");
707 *prompts = xcalloc(*numprompts, sizeof(char *));
708 *echo_on = xcalloc(*numprompts, sizeof(u_int));
713 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
714 u_int *numprompts, char ***prompts, u_int **echo_on)
720 debug3("%s: entering", __func__);
723 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
725 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
727 success = buffer_get_int(&m);
729 debug3("%s: no challenge", __func__);
734 /* Get the challenge, and format the response */
735 challenge = buffer_get_string(&m, NULL);
738 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
739 (*prompts)[0] = challenge;
741 debug3("%s: received challenge: %s", __func__, challenge);
747 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
752 debug3("%s: entering", __func__);
753 if (numresponses != 1)
757 buffer_put_cstring(&m, responses[0]);
758 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
760 mm_request_receive_expect(pmonitor->m_recvfd,
761 MONITOR_ANS_BSDAUTHRESPOND, &m);
763 authok = buffer_get_int(&m);
766 return ((authok == 0) ? -1 : 0);
771 mm_skey_query(void *ctx, char **name, char **infotxt,
772 u_int *numprompts, char ***prompts, u_int **echo_on)
778 debug3("%s: entering", __func__);
781 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
783 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
785 success = buffer_get_int(&m);
787 debug3("%s: no challenge", __func__);
792 /* Get the challenge, and format the response */
793 challenge = buffer_get_string(&m, NULL);
796 debug3("%s: received challenge: %s", __func__, challenge);
798 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
800 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
807 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
812 debug3("%s: entering", __func__);
813 if (numresponses != 1)
817 buffer_put_cstring(&m, responses[0]);
818 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
820 mm_request_receive_expect(pmonitor->m_recvfd,
821 MONITOR_ANS_SKEYRESPOND, &m);
823 authok = buffer_get_int(&m);
826 return ((authok == 0) ? -1 : 0);
830 #ifdef SSH_AUDIT_EVENTS
832 mm_audit_event(ssh_audit_event_t event)
836 debug3("%s entering", __func__);
839 buffer_put_int(&m, event);
841 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
846 mm_audit_run_command(const char *command)
850 debug3("%s entering command %s", __func__, command);
853 buffer_put_cstring(&m, command);
855 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
858 #endif /* SSH_AUDIT_EVENTS */
862 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
867 /* Client doesn't get to see the context */
871 buffer_put_string(&m, goid->elements, goid->length);
873 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
874 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
876 major = buffer_get_int(&m);
883 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
884 gss_buffer_desc *out, OM_uint32 *flags)
891 buffer_put_string(&m, in->value, in->length);
893 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
894 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
896 major = buffer_get_int(&m);
897 out->value = buffer_get_string(&m, &len);
900 *flags = buffer_get_int(&m);
908 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
914 buffer_put_string(&m, gssbuf->value, gssbuf->length);
915 buffer_put_string(&m, gssmic->value, gssmic->length);
917 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
918 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
921 major = buffer_get_int(&m);
927 mm_ssh_gssapi_userok(char *user)
930 int authenticated = 0;
934 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
935 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
938 authenticated = buffer_get_int(&m);
941 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
942 return (authenticated);