1 /* $OpenBSD: monitor_wrap.c,v 1.112 2019/01/21 09:54:11 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 ((r = sshbuf_get_bignum2(m, &p)) != 0 ||
206 (r = sshbuf_get_bignum2(m, &g)) != 0)
207 fatal("%s: buffer error: %s", __func__, ssh_err(r));
209 debug3("%s: remaining %zu", __func__, sshbuf_len(m));
212 return (dh_new_group(g, p));
217 mm_sshkey_sign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
218 const u_char *data, size_t datalen, const char *hostkey_alg, u_int compat)
220 struct kex *kex = *pmonitor->m_pkex;
222 u_int ndx = kex->host_key_index(key, 0, ssh);
225 debug3("%s entering", __func__);
227 if ((m = sshbuf_new()) == NULL)
228 fatal("%s: sshbuf_new failed", __func__);
229 if ((r = sshbuf_put_u32(m, ndx)) != 0 ||
230 (r = sshbuf_put_string(m, data, datalen)) != 0 ||
231 (r = sshbuf_put_cstring(m, hostkey_alg)) != 0 ||
232 (r = sshbuf_put_u32(m, compat)) != 0)
233 fatal("%s: buffer error: %s", __func__, ssh_err(r));
235 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, m);
237 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
238 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, m);
239 if ((r = sshbuf_get_string(m, sigp, lenp)) != 0)
240 fatal("%s: buffer error: %s", __func__, ssh_err(r));
247 mm_getpwnamallow(struct ssh *ssh, const char *username)
253 ServerOptions *newopts;
258 debug3("%s entering", __func__);
260 if ((m = sshbuf_new()) == NULL)
261 fatal("%s: sshbuf_new failed", __func__);
262 if ((r = sshbuf_put_cstring(m, username)) != 0)
263 fatal("%s: buffer error: %s", __func__, ssh_err(r));
265 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, m);
267 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
268 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, m);
270 if ((r = sshbuf_get_u8(m, &ok)) != 0)
271 fatal("%s: buffer error: %s", __func__, ssh_err(r));
277 /* XXX don't like passing struct passwd like this */
278 pw = xcalloc(sizeof(*pw), 1);
279 if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
280 fatal("%s: buffer error: %s", __func__, ssh_err(r));
281 if (len != sizeof(*pw))
282 fatal("%s: struct passwd size mismatch", __func__);
283 memcpy(pw, p, sizeof(*pw));
285 if ((r = sshbuf_get_cstring(m, &pw->pw_name, NULL)) != 0 ||
286 (r = sshbuf_get_cstring(m, &pw->pw_passwd, NULL)) != 0 ||
287 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
288 (r = sshbuf_get_cstring(m, &pw->pw_gecos, NULL)) != 0 ||
290 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
291 (r = sshbuf_get_cstring(m, &pw->pw_class, NULL)) != 0 ||
293 (r = sshbuf_get_cstring(m, &pw->pw_dir, NULL)) != 0 ||
294 (r = sshbuf_get_cstring(m, &pw->pw_shell, NULL)) != 0)
295 fatal("%s: buffer error: %s", __func__, ssh_err(r));
298 /* copy options block as a Match directive may have changed some */
299 if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
300 fatal("%s: buffer error: %s", __func__, ssh_err(r));
301 if (len != sizeof(*newopts))
302 fatal("%s: option block size mismatch", __func__);
303 newopts = xcalloc(sizeof(*newopts), 1);
304 memcpy(newopts, p, sizeof(*newopts));
306 #define M_CP_STROPT(x) do { \
307 if (newopts->x != NULL) { \
308 if ((r = sshbuf_get_cstring(m, \
309 &newopts->x, NULL)) != 0) \
310 fatal("%s: buffer error: %s", \
311 __func__, ssh_err(r)); \
314 #define M_CP_STRARRAYOPT(x, nx) do { \
315 newopts->x = newopts->nx == 0 ? \
316 NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
317 for (i = 0; i < newopts->nx; i++) { \
318 if ((r = sshbuf_get_cstring(m, \
319 &newopts->x[i], NULL)) != 0) \
320 fatal("%s: buffer error: %s", \
321 __func__, ssh_err(r)); \
324 /* See comment in servconf.h */
325 COPY_MATCH_STRING_OPTS();
327 #undef M_CP_STRARRAYOPT
329 copy_set_server_options(&options, newopts, 1);
330 log_change_level(options.log_level);
331 process_permitopen(ssh, &options);
340 mm_auth2_read_banner(void)
346 debug3("%s entering", __func__);
348 if ((m = sshbuf_new()) == NULL)
349 fatal("%s: sshbuf_new failed", __func__);
350 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, m);
353 mm_request_receive_expect(pmonitor->m_recvfd,
354 MONITOR_ANS_AUTH2_READ_BANNER, m);
355 if ((r = sshbuf_get_cstring(m, &banner, NULL)) != 0)
356 fatal("%s: buffer error: %s", __func__, ssh_err(r));
359 /* treat empty banner as missing banner */
360 if (strlen(banner) == 0) {
367 /* Inform the privileged process about service and style */
370 mm_inform_authserv(char *service, char *style)
375 debug3("%s entering", __func__);
377 if ((m = sshbuf_new()) == NULL)
378 fatal("%s: sshbuf_new failed", __func__);
379 if ((r = sshbuf_put_cstring(m, service)) != 0 ||
380 (r = sshbuf_put_cstring(m, style ? style : "")) != 0)
381 fatal("%s: buffer error: %s", __func__, ssh_err(r));
383 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, m);
388 /* Do the password authentication */
390 mm_auth_password(struct ssh *ssh, char *password)
393 int r, authenticated = 0;
398 debug3("%s entering", __func__);
400 if ((m = sshbuf_new()) == NULL)
401 fatal("%s: sshbuf_new failed", __func__);
402 if ((r = sshbuf_put_cstring(m, password)) != 0)
403 fatal("%s: buffer error: %s", __func__, ssh_err(r));
404 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, m);
406 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
407 mm_request_receive_expect(pmonitor->m_recvfd,
408 MONITOR_ANS_AUTHPASSWORD, m);
410 if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
411 fatal("%s: buffer error: %s", __func__, ssh_err(r));
413 if ((r = sshbuf_get_u32(m, &maxtries)) != 0)
414 fatal("%s: buffer error: %s", __func__, ssh_err(r));
415 if (maxtries > INT_MAX)
416 fatal("%s: bad maxtries %u", __func__, maxtries);
417 sshpam_set_maxtries_reached(maxtries);
422 debug3("%s: user %sauthenticated",
423 __func__, authenticated ? "" : "not ");
424 return (authenticated);
428 mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
429 int pubkey_auth_attempt, struct sshauthopt **authoptp)
431 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
432 pubkey_auth_attempt, authoptp));
436 mm_hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
437 const char *user, const char *host, struct sshkey *key)
439 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL));
443 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
444 struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp)
448 struct sshauthopt *opts = NULL;
450 debug3("%s entering", __func__);
452 if (authoptp != NULL)
455 if ((m = sshbuf_new()) == NULL)
456 fatal("%s: sshbuf_new failed", __func__);
457 if ((r = sshbuf_put_u32(m, type)) != 0 ||
458 (r = sshbuf_put_cstring(m, user ? user : "")) != 0 ||
459 (r = sshbuf_put_cstring(m, host ? host : "")) != 0 ||
460 (r = sshkey_puts(key, m)) != 0 ||
461 (r = sshbuf_put_u32(m, pubkey_auth_attempt)) != 0)
462 fatal("%s: buffer error: %s", __func__, ssh_err(r));
464 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, m);
466 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
467 mm_request_receive_expect(pmonitor->m_recvfd,
468 MONITOR_ANS_KEYALLOWED, m);
470 if ((r = sshbuf_get_u32(m, &allowed)) != 0)
471 fatal("%s: buffer error: %s", __func__, ssh_err(r));
472 if (allowed && type == MM_USERKEY) {
473 if ((r = sshauthopt_deserialise(m, &opts)) != 0)
474 fatal("%s: sshauthopt_deserialise: %s",
475 __func__, ssh_err(r));
479 if (authoptp != NULL) {
483 sshauthopt_free(opts);
489 * This key verify needs to send the key type along, because the
490 * privileged parent makes the decision if the key is allowed
491 * for authentication.
495 mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
496 const u_char *data, size_t datalen, const char *sigalg, u_int compat)
499 u_int encoded_ret = 0;
502 debug3("%s entering", __func__);
505 if ((m = sshbuf_new()) == NULL)
506 fatal("%s: sshbuf_new failed", __func__);
507 if ((r = sshkey_puts(key, m)) != 0 ||
508 (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
509 (r = sshbuf_put_string(m, data, datalen)) != 0 ||
510 (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
511 fatal("%s: buffer error: %s", __func__, ssh_err(r));
513 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, m);
515 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
516 mm_request_receive_expect(pmonitor->m_recvfd,
517 MONITOR_ANS_KEYVERIFY, m);
519 if ((r = sshbuf_get_u32(m, &encoded_ret)) != 0)
520 fatal("%s: buffer error: %s", __func__, ssh_err(r));
524 if (encoded_ret != 0)
525 return SSH_ERR_SIGNATURE_INVALID;
530 mm_send_keystate(struct ssh *ssh, struct monitor *monitor)
535 if ((m = sshbuf_new()) == NULL)
536 fatal("%s: sshbuf_new failed", __func__);
537 if ((r = ssh_packet_get_state(ssh, m)) != 0)
538 fatal("%s: get_state failed: %s",
539 __func__, ssh_err(r));
540 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
541 debug3("%s: Finished sending state", __func__);
546 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
550 int success = 0, tmp1 = -1, tmp2 = -1, r;
552 /* Kludge: ensure there are fds free to receive the pty/tty */
553 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
554 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
555 error("%s: cannot allocate fds for pty", __func__);
565 if ((m = sshbuf_new()) == NULL)
566 fatal("%s: sshbuf_new failed", __func__);
567 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, m);
569 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
570 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, m);
572 if ((r = sshbuf_get_u32(m, &success)) != 0)
573 fatal("%s: buffer error: %s", __func__, ssh_err(r));
575 debug3("%s: pty alloc failed", __func__);
579 if ((r = sshbuf_get_cstring(m, &p, NULL)) != 0 ||
580 (r = sshbuf_get_cstring(m, &msg, NULL)) != 0)
581 fatal("%s: buffer error: %s", __func__, ssh_err(r));
584 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
587 if ((r = sshbuf_put(loginmsg, msg, strlen(msg))) != 0)
588 fatal("%s: buffer error: %s", __func__, ssh_err(r));
591 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
592 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
593 fatal("%s: receive fds failed", __func__);
600 mm_session_pty_cleanup2(Session *s)
607 if ((m = sshbuf_new()) == NULL)
608 fatal("%s: sshbuf_new failed", __func__);
609 if ((r = sshbuf_put_cstring(m, s->tty)) != 0)
610 fatal("%s: buffer error: %s", __func__, ssh_err(r));
611 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, m);
614 /* closed dup'ed master */
615 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
616 error("close(s->ptymaster/%d): %s",
617 s->ptymaster, strerror(errno));
619 /* unlink pty from session */
625 mm_start_pam(struct ssh *ssh)
629 debug3("%s entering", __func__);
630 if (!options.use_pam)
631 fatal("UsePAM=no, but ended up in %s anyway", __func__);
632 if ((m = sshbuf_new()) == NULL)
633 fatal("%s: sshbuf_new failed", __func__);
634 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, m);
640 mm_do_pam_account(void)
648 debug3("%s entering", __func__);
649 if (!options.use_pam)
650 fatal("UsePAM=no, but ended up in %s anyway", __func__);
652 if ((m = sshbuf_new()) == NULL)
653 fatal("%s: sshbuf_new failed", __func__);
654 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, m);
656 mm_request_receive_expect(pmonitor->m_recvfd,
657 MONITOR_ANS_PAM_ACCOUNT, m);
658 if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
659 (r = sshbuf_get_cstring(m, &msg, &msglen)) != 0 ||
660 (r = sshbuf_put(loginmsg, msg, msglen)) != 0)
661 fatal("%s: buffer error: %s", __func__, ssh_err(r));
666 debug3("%s returning %d", __func__, ret);
672 mm_sshpam_init_ctx(Authctxt *authctxt)
677 debug3("%s", __func__);
678 if ((m = sshbuf_new()) == NULL)
679 fatal("%s: sshbuf_new failed", __func__);
680 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, m);
681 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
682 mm_request_receive_expect(pmonitor->m_recvfd,
683 MONITOR_ANS_PAM_INIT_CTX, m);
684 if ((r = sshbuf_get_u32(m, &success)) != 0)
685 fatal("%s: buffer error: %s", __func__, ssh_err(r));
687 debug3("%s: pam_init_ctx failed", __func__);
696 mm_sshpam_query(void *ctx, char **name, char **info,
697 u_int *num, char ***prompts, u_int **echo_on)
703 debug3("%s", __func__);
704 if ((m = sshbuf_new()) == NULL)
705 fatal("%s: sshbuf_new failed", __func__);
706 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, m);
707 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
708 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, m);
709 if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
710 (r = sshbuf_get_cstring(m, name, NULL)) != 0 ||
711 (r = sshbuf_get_cstring(m, info, NULL)) != 0 ||
712 (r = sshbuf_get_u32(m, &n)) != 0 ||
713 (r = sshbuf_get_u32(m, num)) != 0)
714 fatal("%s: buffer error: %s", __func__, ssh_err(r));
715 debug3("%s: pam_query returned %d", __func__, ret);
716 sshpam_set_maxtries_reached(n);
717 if (*num > PAM_MAX_NUM_MSG)
718 fatal("%s: received %u PAM messages, expected <= %u",
719 __func__, *num, PAM_MAX_NUM_MSG);
720 *prompts = xcalloc((*num + 1), sizeof(char *));
721 *echo_on = xcalloc((*num + 1), sizeof(u_int));
722 for (i = 0; i < *num; ++i) {
723 if ((r = sshbuf_get_cstring(m, &((*prompts)[i]), NULL)) != 0 ||
724 (r = sshbuf_get_u32(m, &((*echo_on)[i]))) != 0)
725 fatal("%s: buffer error: %s", __func__, ssh_err(r));
732 mm_sshpam_respond(void *ctx, u_int num, char **resp)
738 debug3("%s", __func__);
739 if ((m = sshbuf_new()) == NULL)
740 fatal("%s: sshbuf_new failed", __func__);
741 if ((r = sshbuf_put_u32(m, num)) != 0)
742 fatal("%s: buffer error: %s", __func__, ssh_err(r));
743 for (i = 0; i < num; ++i) {
744 if ((r = sshbuf_put_cstring(m, resp[i])) != 0)
745 fatal("%s: buffer error: %s", __func__, ssh_err(r));
747 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, m);
748 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
749 mm_request_receive_expect(pmonitor->m_recvfd,
750 MONITOR_ANS_PAM_RESPOND, m);
751 if ((r = sshbuf_get_u32(m, &n)) != 0)
752 fatal("%s: buffer error: %s", __func__, ssh_err(r));
753 ret = (int)n; /* XXX */
754 debug3("%s: pam_respond returned %d", __func__, ret);
760 mm_sshpam_free_ctx(void *ctxtp)
764 debug3("%s", __func__);
765 if ((m = sshbuf_new()) == NULL)
766 fatal("%s: sshbuf_new failed", __func__);
767 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, m);
768 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
769 mm_request_receive_expect(pmonitor->m_recvfd,
770 MONITOR_ANS_PAM_FREE_CTX, m);
775 /* Request process termination */
782 if ((m = sshbuf_new()) == NULL)
783 fatal("%s: sshbuf_new failed", __func__);
784 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, m);
789 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
790 char ***prompts, u_int **echo_on)
793 *infotxt = xstrdup("");
795 *prompts = xcalloc(*numprompts, sizeof(char *));
796 *echo_on = xcalloc(*numprompts, sizeof(u_int));
801 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
802 u_int *numprompts, char ***prompts, u_int **echo_on)
809 debug3("%s: entering", __func__);
811 if ((m = sshbuf_new()) == NULL)
812 fatal("%s: sshbuf_new failed", __func__);
813 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, m);
815 mm_request_receive_expect(pmonitor->m_recvfd,
816 MONITOR_ANS_BSDAUTHQUERY, m);
817 if ((r = sshbuf_get_u32(m, &success)) != 0)
818 fatal("%s: buffer error: %s", __func__, ssh_err(r));
820 debug3("%s: no challenge", __func__);
825 /* Get the challenge, and format the response */
826 if ((r = sshbuf_get_cstring(m, &challenge, NULL)) != 0)
827 fatal("%s: buffer error: %s", __func__, ssh_err(r));
830 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
831 (*prompts)[0] = challenge;
833 debug3("%s: received challenge: %s", __func__, challenge);
839 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
844 debug3("%s: entering", __func__);
845 if (numresponses != 1)
848 if ((m = sshbuf_new()) == NULL)
849 fatal("%s: sshbuf_new failed", __func__);
850 if ((r = sshbuf_put_cstring(m, responses[0])) != 0)
851 fatal("%s: buffer error: %s", __func__, ssh_err(r));
852 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, m);
854 mm_request_receive_expect(pmonitor->m_recvfd,
855 MONITOR_ANS_BSDAUTHRESPOND, m);
857 if ((r = sshbuf_get_u32(m, &authok)) != 0)
858 fatal("%s: buffer error: %s", __func__, ssh_err(r));
861 return ((authok == 0) ? -1 : 0);
864 #ifdef SSH_AUDIT_EVENTS
866 mm_audit_event(struct ssh *ssh, ssh_audit_event_t event)
871 debug3("%s entering", __func__);
873 if ((m = sshbuf_new()) == NULL)
874 fatal("%s: sshbuf_new failed", __func__);
875 if ((r = sshbuf_put_u32(m, event)) != 0)
876 fatal("%s: buffer error: %s", __func__, ssh_err(r));
878 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, m);
883 mm_audit_run_command(const char *command)
888 debug3("%s entering command %s", __func__, command);
890 if ((m = sshbuf_new()) == NULL)
891 fatal("%s: sshbuf_new failed", __func__);
892 if ((r = sshbuf_put_cstring(m, command)) != 0)
893 fatal("%s: buffer error: %s", __func__, ssh_err(r));
895 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, m);
898 #endif /* SSH_AUDIT_EVENTS */
902 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
908 /* Client doesn't get to see the context */
911 if ((m = sshbuf_new()) == NULL)
912 fatal("%s: sshbuf_new failed", __func__);
913 if ((r = sshbuf_put_string(m, goid->elements, goid->length)) != 0)
914 fatal("%s: buffer error: %s", __func__, ssh_err(r));
916 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, m);
917 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, m);
919 if ((r = sshbuf_get_u32(m, &major)) != 0)
920 fatal("%s: buffer error: %s", __func__, ssh_err(r));
927 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
928 gss_buffer_desc *out, OM_uint32 *flagsp)
935 if ((m = sshbuf_new()) == NULL)
936 fatal("%s: sshbuf_new failed", __func__);
937 if ((r = sshbuf_put_string(m, in->value, in->length)) != 0)
938 fatal("%s: buffer error: %s", __func__, ssh_err(r));
940 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, m);
941 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, m);
943 if ((r = sshbuf_get_u32(m, &major)) != 0 ||
944 (r = ssh_gssapi_get_buffer_desc(m, out)) != 0)
945 fatal("%s: buffer error: %s", __func__, ssh_err(r));
946 if (flagsp != NULL) {
947 if ((r = sshbuf_get_u32(m, &flags)) != 0)
948 fatal("%s: buffer error: %s", __func__, ssh_err(r));
958 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
964 if ((m = sshbuf_new()) == NULL)
965 fatal("%s: sshbuf_new failed", __func__);
966 if ((r = sshbuf_put_string(m, gssbuf->value, gssbuf->length)) != 0 ||
967 (r = sshbuf_put_string(m, gssmic->value, gssmic->length)) != 0)
968 fatal("%s: buffer error: %s", __func__, ssh_err(r));
970 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, m);
971 mm_request_receive_expect(pmonitor->m_recvfd,
972 MONITOR_ANS_GSSCHECKMIC, m);
974 if ((r = sshbuf_get_u32(m, &major)) != 0)
975 fatal("%s: buffer error: %s", __func__, ssh_err(r));
981 mm_ssh_gssapi_userok(char *user)
984 int r, authenticated = 0;
986 if ((m = sshbuf_new()) == NULL)
987 fatal("%s: sshbuf_new failed", __func__);
989 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m);
990 mm_request_receive_expect(pmonitor->m_recvfd,
991 MONITOR_ANS_GSSUSEROK, m);
993 if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
994 fatal("%s: buffer error: %s", __func__, ssh_err(r));
997 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
998 return (authenticated);