1 /* $OpenBSD: monitor_wrap.c,v 1.107 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>
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"
68 #include "monitor_wrap.h"
70 #include "monitor_fdpass.h"
80 extern struct monitor *pmonitor;
81 extern struct sshbuf *loginmsg;
82 extern ServerOptions options;
85 mm_log_handler(LogLevel level, const char *msg, void *ctx)
87 struct sshbuf *log_msg;
88 struct monitor *mon = (struct monitor *)ctx;
92 if (mon->m_log_sendfd == -1)
93 fatal("%s: no log channel", __func__);
95 if ((log_msg = sshbuf_new()) == NULL)
96 fatal("%s: sshbuf_new failed", __func__);
98 if ((r = sshbuf_put_u32(log_msg, 0)) != 0 || /* length; filled below */
99 (r = sshbuf_put_u32(log_msg, level)) != 0 ||
100 (r = sshbuf_put_cstring(log_msg, msg)) != 0)
101 fatal("%s: buffer error: %s", __func__, ssh_err(r));
102 if ((len = sshbuf_len(log_msg)) < 4 || len > 0xffffffff)
103 fatal("%s: bad length %zu", __func__, len);
104 POKE_U32(sshbuf_mutable_ptr(log_msg), len - 4);
105 if (atomicio(vwrite, mon->m_log_sendfd,
106 sshbuf_mutable_ptr(log_msg), len) != len)
107 fatal("%s: write: %s", __func__, strerror(errno));
108 sshbuf_free(log_msg);
115 * m_pid is only set in the privileged part, and
116 * points to the unprivileged child.
118 return (pmonitor && pmonitor->m_pid > 0);
122 mm_request_send(int sock, enum monitor_reqtype type, struct sshbuf *m)
124 size_t mlen = sshbuf_len(m);
127 debug3("%s entering: type %d", __func__, type);
129 if (mlen >= 0xffffffff)
130 fatal("%s: bad length %zu", __func__, mlen);
131 POKE_U32(buf, mlen + 1);
132 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
133 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
134 fatal("%s: write: %s", __func__, strerror(errno));
135 if (atomicio(vwrite, sock, sshbuf_mutable_ptr(m), mlen) != mlen)
136 fatal("%s: write: %s", __func__, strerror(errno));
140 mm_request_receive(int sock, struct sshbuf *m)
142 u_char buf[4], *p = NULL;
146 debug3("%s entering", __func__);
148 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
151 fatal("%s: read: %s", __func__, strerror(errno));
153 msg_len = PEEK_U32(buf);
154 if (msg_len > 256 * 1024)
155 fatal("%s: read: bad msg_len %d", __func__, msg_len);
157 if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
158 fatal("%s: buffer error: %s", __func__, ssh_err(r));
159 if (atomicio(read, sock, p, msg_len) != msg_len)
160 fatal("%s: read: %s", __func__, strerror(errno));
164 mm_request_receive_expect(int sock, enum monitor_reqtype type, struct sshbuf *m)
169 debug3("%s entering: type %d", __func__, type);
171 mm_request_receive(sock, m);
172 if ((r = sshbuf_get_u8(m, &rtype)) != 0)
173 fatal("%s: buffer error: %s", __func__, ssh_err(r));
175 fatal("%s: read: rtype %d != type %d", __func__,
181 mm_choose_dh(int min, int nbits, int max)
188 if ((m = sshbuf_new()) == NULL)
189 fatal("%s: sshbuf_new failed", __func__);
190 if ((r = sshbuf_put_u32(m, min)) != 0 ||
191 (r = sshbuf_put_u32(m, nbits)) != 0 ||
192 (r = sshbuf_put_u32(m, max)) != 0)
193 fatal("%s: buffer error: %s", __func__, ssh_err(r));
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 if ((r = sshbuf_get_u8(m, &success)) != 0)
201 fatal("%s: buffer error: %s", __func__, ssh_err(r));
203 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
205 if ((p = BN_new()) == NULL)
206 fatal("%s: BN_new failed", __func__);
207 if ((g = BN_new()) == NULL)
208 fatal("%s: BN_new failed", __func__);
209 if ((r = sshbuf_get_bignum2(m, p)) != 0 ||
210 (r = sshbuf_get_bignum2(m, g)) != 0)
211 fatal("%s: buffer error: %s", __func__, ssh_err(r));
213 debug3("%s: remaining %zu", __func__, sshbuf_len(m));
216 return (dh_new_group(g, p));
221 mm_sshkey_sign(struct sshkey *key, u_char **sigp, size_t *lenp,
222 const u_char *data, size_t datalen, const char *hostkey_alg, u_int compat)
224 struct kex *kex = *pmonitor->m_pkex;
226 u_int ndx = kex->host_key_index(key, 0, active_state);
229 debug3("%s entering", __func__);
231 if ((m = sshbuf_new()) == NULL)
232 fatal("%s: sshbuf_new failed", __func__);
233 if ((r = sshbuf_put_u32(m, ndx)) != 0 ||
234 (r = sshbuf_put_string(m, data, datalen)) != 0 ||
235 (r = sshbuf_put_cstring(m, hostkey_alg)) != 0 ||
236 (r = sshbuf_put_u32(m, compat)) != 0)
237 fatal("%s: buffer error: %s", __func__, ssh_err(r));
239 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, m);
241 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
242 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, m);
243 if ((r = sshbuf_get_string(m, sigp, lenp)) != 0)
244 fatal("%s: buffer error: %s", __func__, ssh_err(r));
250 #ifdef HAVE_LOGIN_CAP
252 mm_login_getpwclass(const struct passwd *pwent)
259 debug3("%s entering", __func__);
261 if ((m = sshbuf_new()) == NULL)
262 fatal("%s: sshbuf_new failed", __func__);
263 if ((r = sshbuf_put_passwd(m, pwent)) != 0)
264 fatal("%s: buffer error: %s", __func__, ssh_err(r));
266 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GETPWCLASS, m);
268 debug3("%s: waiting for MONITOR_ANS_GETPWCLASS", __func__);
269 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GETPWCLASS, m);
271 if ((r = sshbuf_get_u8(m, &rc)) != 0)
272 fatal("%s: buffer error: %s", __func__, ssh_err(r));
279 lc = xmalloc(sizeof(*lc));
280 if ((r = sshbuf_get_cstring(m, &lc->lc_class, NULL)) != 0 ||
281 (r = sshbuf_get_cstring(m, &lc->lc_cap, NULL)) != 0 ||
282 (r = sshbuf_get_cstring(m, &lc->lc_style, NULL)) != 0)
283 fatal("%s: buffer error: %s", __func__, ssh_err(r));
292 #ifdef HAVE_LOGIN_CAP
294 mm_login_close(login_cap_t *lc)
306 mm_getpwnamallow(const char *username)
308 struct ssh *ssh = active_state; /* XXX */
313 ServerOptions *newopts;
318 debug3("%s entering", __func__);
320 if ((m = sshbuf_new()) == NULL)
321 fatal("%s: sshbuf_new failed", __func__);
322 if ((r = sshbuf_put_cstring(m, username)) != 0)
323 fatal("%s: buffer error: %s", __func__, ssh_err(r));
325 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, m);
327 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
328 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, m);
330 if ((r = sshbuf_get_u8(m, &ok)) != 0)
331 fatal("%s: buffer error: %s", __func__, ssh_err(r));
337 pw = sshbuf_get_passwd(m);
339 fatal("%s: receive get struct passwd failed", __func__);
342 /* copy options block as a Match directive may have changed some */
343 if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
344 fatal("%s: buffer error: %s", __func__, ssh_err(r));
345 if (len != sizeof(*newopts))
346 fatal("%s: option block size mismatch", __func__);
347 newopts = xcalloc(sizeof(*newopts), 1);
348 memcpy(newopts, p, sizeof(*newopts));
350 #define M_CP_STROPT(x) do { \
351 if (newopts->x != NULL) { \
352 if ((r = sshbuf_get_cstring(m, \
353 &newopts->x, NULL)) != 0) \
354 fatal("%s: buffer error: %s", \
355 __func__, ssh_err(r)); \
358 #define M_CP_STRARRAYOPT(x, nx) do { \
359 newopts->x = newopts->nx == 0 ? \
360 NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
361 for (i = 0; i < newopts->nx; i++) { \
362 if ((r = sshbuf_get_cstring(m, \
363 &newopts->x[i], NULL)) != 0) \
364 fatal("%s: buffer error: %s", \
365 __func__, ssh_err(r)); \
368 /* See comment in servconf.h */
369 COPY_MATCH_STRING_OPTS();
371 #undef M_CP_STRARRAYOPT
373 copy_set_server_options(&options, newopts, 1);
374 log_change_level(options.log_level);
375 process_permitopen(ssh, &options);
384 mm_auth2_read_banner(void)
390 debug3("%s entering", __func__);
392 if ((m = sshbuf_new()) == NULL)
393 fatal("%s: sshbuf_new failed", __func__);
394 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, m);
397 mm_request_receive_expect(pmonitor->m_recvfd,
398 MONITOR_ANS_AUTH2_READ_BANNER, m);
399 if ((r = sshbuf_get_cstring(m, &banner, NULL)) != 0)
400 fatal("%s: buffer error: %s", __func__, ssh_err(r));
403 /* treat empty banner as missing banner */
404 if (strlen(banner) == 0) {
411 /* Inform the privileged process about service and style */
414 mm_inform_authserv(char *service, char *style)
419 debug3("%s entering", __func__);
421 if ((m = sshbuf_new()) == NULL)
422 fatal("%s: sshbuf_new failed", __func__);
423 if ((r = sshbuf_put_cstring(m, service)) != 0 ||
424 (r = sshbuf_put_cstring(m, style ? style : "")) != 0)
425 fatal("%s: buffer error: %s", __func__, ssh_err(r));
427 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, m);
432 /* Do the password authentication */
434 mm_auth_password(struct ssh *ssh, char *password)
437 int r, authenticated = 0;
442 debug3("%s entering", __func__);
444 if ((m = sshbuf_new()) == NULL)
445 fatal("%s: sshbuf_new failed", __func__);
446 if ((r = sshbuf_put_cstring(m, password)) != 0)
447 fatal("%s: buffer error: %s", __func__, ssh_err(r));
448 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, m);
450 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
451 mm_request_receive_expect(pmonitor->m_recvfd,
452 MONITOR_ANS_AUTHPASSWORD, m);
454 if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
455 fatal("%s: buffer error: %s", __func__, ssh_err(r));
457 if ((r = sshbuf_get_u32(m, &maxtries)) != 0)
458 fatal("%s: buffer error: %s", __func__, ssh_err(r));
459 if (maxtries > INT_MAX)
460 fatal("%s: bad maxtries %u", __func__, maxtries);
461 sshpam_set_maxtries_reached(maxtries);
466 debug3("%s: user %sauthenticated",
467 __func__, authenticated ? "" : "not ");
468 return (authenticated);
472 mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
473 int pubkey_auth_attempt, struct sshauthopt **authoptp)
475 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
476 pubkey_auth_attempt, authoptp));
480 mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host,
483 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL));
487 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
488 struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp)
492 struct sshauthopt *opts = NULL;
494 debug3("%s entering", __func__);
496 if (authoptp != NULL)
499 if ((m = sshbuf_new()) == NULL)
500 fatal("%s: sshbuf_new failed", __func__);
501 if ((r = sshbuf_put_u32(m, type)) != 0 ||
502 (r = sshbuf_put_cstring(m, user ? user : "")) != 0 ||
503 (r = sshbuf_put_cstring(m, host ? host : "")) != 0 ||
504 (r = sshkey_puts(key, m)) != 0 ||
505 (r = sshbuf_put_u32(m, pubkey_auth_attempt)) != 0)
506 fatal("%s: buffer error: %s", __func__, ssh_err(r));
508 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, m);
510 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
511 mm_request_receive_expect(pmonitor->m_recvfd,
512 MONITOR_ANS_KEYALLOWED, m);
514 if ((r = sshbuf_get_u32(m, &allowed)) != 0)
515 fatal("%s: buffer error: %s", __func__, ssh_err(r));
516 if (allowed && type == MM_USERKEY) {
517 if ((r = sshauthopt_deserialise(m, &opts)) != 0)
518 fatal("%s: sshauthopt_deserialise: %s",
519 __func__, ssh_err(r));
523 if (authoptp != NULL) {
527 sshauthopt_free(opts);
533 * This key verify needs to send the key type along, because the
534 * privileged parent makes the decision if the key is allowed
535 * for authentication.
539 mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
540 const u_char *data, size_t datalen, const char *sigalg, u_int compat)
543 u_int encoded_ret = 0;
546 debug3("%s entering", __func__);
549 if ((m = sshbuf_new()) == NULL)
550 fatal("%s: sshbuf_new failed", __func__);
551 if ((r = sshkey_puts(key, m)) != 0 ||
552 (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
553 (r = sshbuf_put_string(m, data, datalen)) != 0 ||
554 (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
555 fatal("%s: buffer error: %s", __func__, ssh_err(r));
557 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, m);
559 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
560 mm_request_receive_expect(pmonitor->m_recvfd,
561 MONITOR_ANS_KEYVERIFY, m);
563 if ((r = sshbuf_get_u32(m, &encoded_ret)) != 0)
564 fatal("%s: buffer error: %s", __func__, ssh_err(r));
568 if (encoded_ret != 0)
569 return SSH_ERR_SIGNATURE_INVALID;
574 mm_send_keystate(struct monitor *monitor)
576 struct ssh *ssh = active_state; /* XXX */
580 if ((m = sshbuf_new()) == NULL)
581 fatal("%s: sshbuf_new failed", __func__);
582 if ((r = ssh_packet_get_state(ssh, m)) != 0)
583 fatal("%s: get_state failed: %s",
584 __func__, ssh_err(r));
585 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
586 debug3("%s: Finished sending state", __func__);
591 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
595 int success = 0, tmp1 = -1, tmp2 = -1, r;
597 /* Kludge: ensure there are fds free to receive the pty/tty */
598 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
599 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
600 error("%s: cannot allocate fds for pty", __func__);
610 if ((m = sshbuf_new()) == NULL)
611 fatal("%s: sshbuf_new failed", __func__);
612 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, m);
614 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
615 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, m);
617 if ((r = sshbuf_get_u32(m, &success)) != 0)
618 fatal("%s: buffer error: %s", __func__, ssh_err(r));
620 debug3("%s: pty alloc failed", __func__);
624 if ((r = sshbuf_get_cstring(m, &p, NULL)) != 0 ||
625 (r = sshbuf_get_cstring(m, &msg, NULL)) != 0)
626 fatal("%s: buffer error: %s", __func__, ssh_err(r));
629 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
632 if ((r = sshbuf_put(loginmsg, msg, strlen(msg))) != 0)
633 fatal("%s: buffer error: %s", __func__, ssh_err(r));
636 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
637 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
638 fatal("%s: receive fds failed", __func__);
645 mm_session_pty_cleanup2(Session *s)
652 if ((m = sshbuf_new()) == NULL)
653 fatal("%s: sshbuf_new failed", __func__);
654 if ((r = sshbuf_put_cstring(m, s->tty)) != 0)
655 fatal("%s: buffer error: %s", __func__, ssh_err(r));
656 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, m);
659 /* closed dup'ed master */
660 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
661 error("close(s->ptymaster/%d): %s",
662 s->ptymaster, strerror(errno));
664 /* unlink pty from session */
670 mm_start_pam(Authctxt *authctxt)
674 debug3("%s entering", __func__);
675 if (!options.use_pam)
676 fatal("UsePAM=no, but ended up in %s anyway", __func__);
677 if ((m = sshbuf_new()) == NULL)
678 fatal("%s: sshbuf_new failed", __func__);
679 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, m);
685 mm_do_pam_account(void)
693 debug3("%s entering", __func__);
694 if (!options.use_pam)
695 fatal("UsePAM=no, but ended up in %s anyway", __func__);
697 if ((m = sshbuf_new()) == NULL)
698 fatal("%s: sshbuf_new failed", __func__);
699 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, m);
701 mm_request_receive_expect(pmonitor->m_recvfd,
702 MONITOR_ANS_PAM_ACCOUNT, m);
703 if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
704 (r = sshbuf_get_cstring(m, &msg, &msglen)) != 0 ||
705 (r = sshbuf_put(loginmsg, msg, msglen)) != 0)
706 fatal("%s: buffer error: %s", __func__, ssh_err(r));
711 debug3("%s returning %d", __func__, ret);
717 mm_sshpam_init_ctx(Authctxt *authctxt)
722 debug3("%s", __func__);
723 if ((m = sshbuf_new()) == NULL)
724 fatal("%s: sshbuf_new failed", __func__);
725 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, m);
726 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
727 mm_request_receive_expect(pmonitor->m_recvfd,
728 MONITOR_ANS_PAM_INIT_CTX, m);
729 if ((r = sshbuf_get_u32(m, &success)) != 0)
730 fatal("%s: buffer error: %s", __func__, ssh_err(r));
732 debug3("%s: pam_init_ctx failed", __func__);
741 mm_sshpam_query(void *ctx, char **name, char **info,
742 u_int *num, char ***prompts, u_int **echo_on)
748 debug3("%s", __func__);
749 if ((m = sshbuf_new()) == NULL)
750 fatal("%s: sshbuf_new failed", __func__);
751 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, m);
752 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
753 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, m);
754 if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
755 (r = sshbuf_get_cstring(m, name, NULL)) != 0 ||
756 (r = sshbuf_get_cstring(m, info, NULL)) != 0 ||
757 (r = sshbuf_get_u32(m, &n)) != 0 ||
758 (r = sshbuf_get_u32(m, num)) != 0)
759 fatal("%s: buffer error: %s", __func__, ssh_err(r));
760 debug3("%s: pam_query returned %d", __func__, ret);
761 sshpam_set_maxtries_reached(n);
762 if (*num > PAM_MAX_NUM_MSG)
763 fatal("%s: received %u PAM messages, expected <= %u",
764 __func__, *num, PAM_MAX_NUM_MSG);
765 *prompts = xcalloc((*num + 1), sizeof(char *));
766 *echo_on = xcalloc((*num + 1), sizeof(u_int));
767 for (i = 0; i < *num; ++i) {
768 if ((r = sshbuf_get_cstring(m, &((*prompts)[i]), NULL)) != 0 ||
769 (r = sshbuf_get_u32(m, &((*echo_on)[i]))) != 0)
770 fatal("%s: buffer error: %s", __func__, ssh_err(r));
777 mm_sshpam_respond(void *ctx, u_int num, char **resp)
783 debug3("%s", __func__);
784 if ((m = sshbuf_new()) == NULL)
785 fatal("%s: sshbuf_new failed", __func__);
786 if ((r = sshbuf_put_u32(m, num)) != 0)
787 fatal("%s: buffer error: %s", __func__, ssh_err(r));
788 for (i = 0; i < num; ++i) {
789 if ((r = sshbuf_put_cstring(m, resp[i])) != 0)
790 fatal("%s: buffer error: %s", __func__, ssh_err(r));
792 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, m);
793 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
794 mm_request_receive_expect(pmonitor->m_recvfd,
795 MONITOR_ANS_PAM_RESPOND, m);
796 if ((r = sshbuf_get_u32(m, &n)) != 0)
797 fatal("%s: buffer error: %s", __func__, ssh_err(r));
798 ret = (int)n; /* XXX */
799 debug3("%s: pam_respond returned %d", __func__, ret);
805 mm_sshpam_free_ctx(void *ctxtp)
809 debug3("%s", __func__);
810 if ((m = sshbuf_new()) == NULL)
811 fatal("%s: sshbuf_new failed", __func__);
812 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, m);
813 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
814 mm_request_receive_expect(pmonitor->m_recvfd,
815 MONITOR_ANS_PAM_FREE_CTX, m);
820 /* Request process termination */
827 if ((m = sshbuf_new()) == NULL)
828 fatal("%s: sshbuf_new failed", __func__);
829 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, m);
834 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
835 char ***prompts, u_int **echo_on)
838 *infotxt = xstrdup("");
840 *prompts = xcalloc(*numprompts, sizeof(char *));
841 *echo_on = xcalloc(*numprompts, sizeof(u_int));
846 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
847 u_int *numprompts, char ***prompts, u_int **echo_on)
854 debug3("%s: entering", __func__);
856 if ((m = sshbuf_new()) == NULL)
857 fatal("%s: sshbuf_new failed", __func__);
858 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, m);
860 mm_request_receive_expect(pmonitor->m_recvfd,
861 MONITOR_ANS_BSDAUTHQUERY, m);
862 if ((r = sshbuf_get_u32(m, &success)) != 0)
863 fatal("%s: buffer error: %s", __func__, ssh_err(r));
865 debug3("%s: no challenge", __func__);
870 /* Get the challenge, and format the response */
871 if ((r = sshbuf_get_cstring(m, &challenge, NULL)) != 0)
872 fatal("%s: buffer error: %s", __func__, ssh_err(r));
875 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
876 (*prompts)[0] = challenge;
878 debug3("%s: received challenge: %s", __func__, challenge);
884 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
889 debug3("%s: entering", __func__);
890 if (numresponses != 1)
893 if ((m = sshbuf_new()) == NULL)
894 fatal("%s: sshbuf_new failed", __func__);
895 if ((r = sshbuf_put_cstring(m, responses[0])) != 0)
896 fatal("%s: buffer error: %s", __func__, ssh_err(r));
897 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, m);
899 mm_request_receive_expect(pmonitor->m_recvfd,
900 MONITOR_ANS_BSDAUTHRESPOND, m);
902 if ((r = sshbuf_get_u32(m, &authok)) != 0)
903 fatal("%s: buffer error: %s", __func__, ssh_err(r));
906 return ((authok == 0) ? -1 : 0);
909 #ifdef SSH_AUDIT_EVENTS
911 mm_audit_event(ssh_audit_event_t event)
916 debug3("%s entering", __func__);
918 if ((m = sshbuf_new()) == NULL)
919 fatal("%s: sshbuf_new failed", __func__);
920 if ((r = sshbuf_put_u32(m, event)) != 0)
921 fatal("%s: buffer error: %s", __func__, ssh_err(r));
923 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, m);
928 mm_audit_run_command(const char *command)
933 debug3("%s entering command %s", __func__, command);
935 if ((m = sshbuf_new()) == NULL)
936 fatal("%s: sshbuf_new failed", __func__);
937 if ((r = sshbuf_put_cstring(m, command)) != 0)
938 fatal("%s: buffer error: %s", __func__, ssh_err(r));
940 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, m);
943 #endif /* SSH_AUDIT_EVENTS */
947 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
953 /* Client doesn't get to see the context */
956 if ((m = sshbuf_new()) == NULL)
957 fatal("%s: sshbuf_new failed", __func__);
958 if ((r = sshbuf_put_string(m, goid->elements, goid->length)) != 0)
959 fatal("%s: buffer error: %s", __func__, ssh_err(r));
961 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, m);
962 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, m);
964 if ((r = sshbuf_get_u32(m, &major)) != 0)
965 fatal("%s: buffer error: %s", __func__, ssh_err(r));
972 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
973 gss_buffer_desc *out, OM_uint32 *flagsp)
980 if ((m = sshbuf_new()) == NULL)
981 fatal("%s: sshbuf_new failed", __func__);
982 if ((r = sshbuf_put_string(m, in->value, in->length)) != 0)
983 fatal("%s: buffer error: %s", __func__, ssh_err(r));
985 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, m);
986 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, m);
988 if ((r = sshbuf_get_u32(m, &major)) != 0 ||
989 (r = ssh_gssapi_get_buffer_desc(m, out)) != 0)
990 fatal("%s: buffer error: %s", __func__, ssh_err(r));
991 if (flagsp != NULL) {
992 if ((r = sshbuf_get_u32(m, &flags)) != 0)
993 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1003 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1009 if ((m = sshbuf_new()) == NULL)
1010 fatal("%s: sshbuf_new failed", __func__);
1011 if ((r = sshbuf_put_string(m, gssbuf->value, gssbuf->length)) != 0 ||
1012 (r = sshbuf_put_string(m, gssmic->value, gssmic->length)) != 0)
1013 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1015 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, m);
1016 mm_request_receive_expect(pmonitor->m_recvfd,
1017 MONITOR_ANS_GSSCHECKMIC, m);
1019 if ((r = sshbuf_get_u32(m, &major)) != 0)
1020 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1026 mm_ssh_gssapi_userok(char *user)
1029 int r, authenticated = 0;
1031 if ((m = sshbuf_new()) == NULL)
1032 fatal("%s: sshbuf_new failed", __func__);
1034 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m);
1035 mm_request_receive_expect(pmonitor->m_recvfd,
1036 MONITOR_ANS_GSSUSEROK, m);
1038 if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
1039 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1042 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1043 return (authenticated);