ena.4: Add a missing space.
[dragonfly.git] / crypto / openssh / monitor_wrap.c
blob69212aaf330b58e5d9bf49d3b131c1cfd765579e
1 /* $OpenBSD: monitor_wrap.c,v 1.94 2017/10/02 19:33:20 djm Exp $ */
2 /*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
28 #include "includes.h"
30 #include <sys/types.h>
31 #include <sys/uio.h>
33 #include <errno.h>
34 #include <pwd.h>
35 #include <signal.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
41 #ifdef WITH_OPENSSL
42 #include <openssl/bn.h>
43 #include <openssl/dh.h>
44 #include <openssl/evp.h>
45 #endif
47 #include "openbsd-compat/sys-queue.h"
48 #include "xmalloc.h"
49 #include "ssh.h"
50 #ifdef WITH_OPENSSL
51 #include "dh.h"
52 #endif
53 #include "buffer.h"
54 #include "key.h"
55 #include "cipher.h"
56 #include "kex.h"
57 #include "hostfile.h"
58 #include "auth.h"
59 #include "auth-options.h"
60 #include "packet.h"
61 #include "mac.h"
62 #include "log.h"
63 #include "auth-pam.h"
64 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
65 #undef TARGET_OS_MAC
66 #include "zlib.h"
67 #define TARGET_OS_MAC 1
68 #else
69 #include "zlib.h"
70 #endif
71 #include "monitor.h"
72 #ifdef GSSAPI
73 #include "ssh-gss.h"
74 #endif
75 #include "monitor_wrap.h"
76 #include "atomicio.h"
77 #include "monitor_fdpass.h"
78 #include "misc.h"
79 #include "uuencode.h"
81 #include "channels.h"
82 #include "session.h"
83 #include "servconf.h"
85 #include "ssherr.h"
87 /* Imports */
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;
94 void
95 mm_log_handler(LogLevel level, const char *msg, void *ctx)
97 Buffer log_msg;
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
107 * fragile math.
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);
121 mm_is_monitor(void)
124 * m_pid is only set in the privileged part, and
125 * points to the unprivileged child.
127 return (pmonitor && pmonitor->m_pid > 0);
130 void
131 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
133 u_int mlen = buffer_len(m);
134 u_char buf[5];
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));
146 void
147 mm_request_receive(int sock, Buffer *m)
149 u_char buf[4];
150 u_int msg_len;
152 debug3("%s entering", __func__);
154 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
155 if (errno == EPIPE)
156 cleanup_exit(255);
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);
162 buffer_clear(m);
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));
168 void
169 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
171 u_char rtype;
173 debug3("%s entering: type %d", __func__, type);
175 mm_request_receive(sock, m);
176 rtype = buffer_get_char(m);
177 if (rtype != type)
178 fatal("%s: read: rtype %d != type %d", __func__,
179 rtype, type);
182 #ifdef WITH_OPENSSL
183 DH *
184 mm_choose_dh(int min, int nbits, int max)
186 BIGNUM *p, *g;
187 int success = 0;
188 Buffer m;
190 buffer_init(&m);
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);
201 if (success == 0)
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));
212 buffer_free(&m);
214 return (dh_new_group(g, p));
216 #endif
219 mm_key_sign(struct sshkey *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;
223 Buffer m;
225 debug3("%s entering", __func__);
227 buffer_init(&m);
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);
237 buffer_free(&m);
239 return (0);
242 struct passwd *
243 mm_getpwnamallow(const char *username)
245 struct ssh *ssh = active_state; /* XXX */
246 Buffer m;
247 struct passwd *pw;
248 u_int len, i;
249 ServerOptions *newopts;
251 debug3("%s entering", __func__);
253 buffer_init(&m);
254 buffer_put_cstring(&m, username);
256 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
258 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
259 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
261 if (buffer_get_char(&m) == 0) {
262 pw = NULL;
263 goto out;
265 pw = buffer_get_string(&m, &len);
266 if (len != sizeof(struct passwd))
267 fatal("%s: struct passwd size mismatch", __func__);
268 pw->pw_name = buffer_get_string(&m, NULL);
269 pw->pw_passwd = buffer_get_string(&m, NULL);
270 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
271 pw->pw_gecos = buffer_get_string(&m, NULL);
272 #endif
273 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
274 pw->pw_class = buffer_get_string(&m, NULL);
275 #endif
276 pw->pw_dir = buffer_get_string(&m, NULL);
277 pw->pw_shell = buffer_get_string(&m, NULL);
279 out:
280 /* copy options block as a Match directive may have changed some */
281 newopts = buffer_get_string(&m, &len);
282 if (len != sizeof(*newopts))
283 fatal("%s: option block size mismatch", __func__);
285 #define M_CP_STROPT(x) do { \
286 if (newopts->x != NULL) \
287 newopts->x = buffer_get_string(&m, NULL); \
288 } while (0)
289 #define M_CP_STRARRAYOPT(x, nx) do { \
290 for (i = 0; i < newopts->nx; i++) \
291 newopts->x[i] = buffer_get_string(&m, NULL); \
292 } while (0)
293 #define M_CP_STRARRAYOPT_ALLOC(x, nx) do { \
294 newopts->x = newopts->nx == 0 ? \
295 NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
296 M_CP_STRARRAYOPT(x, nx); \
297 } while (0)
298 /* See comment in servconf.h */
299 COPY_MATCH_STRING_OPTS();
300 #undef M_CP_STROPT
301 #undef M_CP_STRARRAYOPT
302 #undef M_CP_STRARRAYOPT_ALLOC
304 copy_set_server_options(&options, newopts, 1);
305 log_change_level(options.log_level);
306 process_permitopen(ssh, &options);
307 free(newopts);
309 buffer_free(&m);
311 return (pw);
314 char *
315 mm_auth2_read_banner(void)
317 Buffer m;
318 char *banner;
320 debug3("%s entering", __func__);
322 buffer_init(&m);
323 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
324 buffer_clear(&m);
326 mm_request_receive_expect(pmonitor->m_recvfd,
327 MONITOR_ANS_AUTH2_READ_BANNER, &m);
328 banner = buffer_get_string(&m, NULL);
329 buffer_free(&m);
331 /* treat empty banner as missing banner */
332 if (strlen(banner) == 0) {
333 free(banner);
334 banner = NULL;
336 return (banner);
339 /* Inform the privileged process about service and style */
341 void
342 mm_inform_authserv(char *service, char *style)
344 Buffer m;
346 debug3("%s entering", __func__);
348 buffer_init(&m);
349 buffer_put_cstring(&m, service);
350 buffer_put_cstring(&m, style ? style : "");
352 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
354 buffer_free(&m);
357 /* Do the password authentication */
359 mm_auth_password(Authctxt *authctxt, char *password)
361 Buffer m;
362 int authenticated = 0;
364 debug3("%s entering", __func__);
366 buffer_init(&m);
367 buffer_put_cstring(&m, password);
368 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
370 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
371 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
373 authenticated = buffer_get_int(&m);
374 #ifdef USE_PAM
375 sshpam_set_maxtries_reached(buffer_get_int(&m));
376 #endif
378 buffer_free(&m);
380 debug3("%s: user %sauthenticated",
381 __func__, authenticated ? "" : "not ");
382 return (authenticated);
386 mm_user_key_allowed(struct passwd *pw, struct sshkey *key,
387 int pubkey_auth_attempt)
389 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
390 pubkey_auth_attempt));
394 mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host,
395 struct sshkey *key)
397 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0));
401 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
402 struct sshkey *key, int pubkey_auth_attempt)
404 Buffer m;
405 u_char *blob;
406 u_int len;
407 int allowed = 0, have_forced = 0;
409 debug3("%s entering", __func__);
411 /* Convert the key to a blob and the pass it over */
412 if (!key_to_blob(key, &blob, &len))
413 return (0);
415 buffer_init(&m);
416 buffer_put_int(&m, type);
417 buffer_put_cstring(&m, user ? user : "");
418 buffer_put_cstring(&m, host ? host : "");
419 buffer_put_string(&m, blob, len);
420 buffer_put_int(&m, pubkey_auth_attempt);
421 free(blob);
423 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
425 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
426 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
428 allowed = buffer_get_int(&m);
430 /* fake forced command */
431 auth_clear_options();
432 have_forced = buffer_get_int(&m);
433 forced_command = have_forced ? xstrdup("true") : NULL;
435 buffer_free(&m);
437 return (allowed);
441 * This key verify needs to send the key type along, because the
442 * privileged parent makes the decision if the key is allowed
443 * for authentication.
447 mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
448 const u_char *data, size_t datalen, u_int compat)
450 Buffer m;
451 u_char *blob;
452 u_int len;
453 u_int encoded_ret = 0;
455 debug3("%s entering", __func__);
457 /* Convert the key to a blob and the pass it over */
458 if (!key_to_blob(key, &blob, &len))
459 return (0);
461 buffer_init(&m);
462 buffer_put_string(&m, blob, len);
463 buffer_put_string(&m, sig, siglen);
464 buffer_put_string(&m, data, datalen);
465 free(blob);
467 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
469 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
470 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
472 encoded_ret = buffer_get_int(&m);
474 buffer_free(&m);
476 if (encoded_ret != 0)
477 return SSH_ERR_SIGNATURE_INVALID;
478 return 0;
481 void
482 mm_send_keystate(struct monitor *monitor)
484 struct ssh *ssh = active_state; /* XXX */
485 struct sshbuf *m;
486 int r;
488 if ((m = sshbuf_new()) == NULL)
489 fatal("%s: sshbuf_new failed", __func__);
490 if ((r = ssh_packet_get_state(ssh, m)) != 0)
491 fatal("%s: get_state failed: %s",
492 __func__, ssh_err(r));
493 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
494 debug3("%s: Finished sending state", __func__);
495 sshbuf_free(m);
499 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
501 Buffer m;
502 char *p, *msg;
503 int success = 0, tmp1 = -1, tmp2 = -1;
505 /* Kludge: ensure there are fds free to receive the pty/tty */
506 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
507 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
508 error("%s: cannot allocate fds for pty", __func__);
509 if (tmp1 > 0)
510 close(tmp1);
511 if (tmp2 > 0)
512 close(tmp2);
513 return 0;
515 close(tmp1);
516 close(tmp2);
518 buffer_init(&m);
519 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
521 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
522 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
524 success = buffer_get_int(&m);
525 if (success == 0) {
526 debug3("%s: pty alloc failed", __func__);
527 buffer_free(&m);
528 return (0);
530 p = buffer_get_string(&m, NULL);
531 msg = buffer_get_string(&m, NULL);
532 buffer_free(&m);
534 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
535 free(p);
537 buffer_append(&loginmsg, msg, strlen(msg));
538 free(msg);
540 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
541 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
542 fatal("%s: receive fds failed", __func__);
544 /* Success */
545 return (1);
548 void
549 mm_session_pty_cleanup2(Session *s)
551 Buffer m;
553 if (s->ttyfd == -1)
554 return;
555 buffer_init(&m);
556 buffer_put_cstring(&m, s->tty);
557 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
558 buffer_free(&m);
560 /* closed dup'ed master */
561 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
562 error("close(s->ptymaster/%d): %s",
563 s->ptymaster, strerror(errno));
565 /* unlink pty from session */
566 s->ttyfd = -1;
569 #ifdef USE_PAM
570 void
571 mm_start_pam(Authctxt *authctxt)
573 Buffer m;
575 debug3("%s entering", __func__);
576 if (!options.use_pam)
577 fatal("UsePAM=no, but ended up in %s anyway", __func__);
579 buffer_init(&m);
580 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
582 buffer_free(&m);
585 u_int
586 mm_do_pam_account(void)
588 Buffer m;
589 u_int ret;
590 char *msg;
592 debug3("%s entering", __func__);
593 if (!options.use_pam)
594 fatal("UsePAM=no, but ended up in %s anyway", __func__);
596 buffer_init(&m);
597 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
599 mm_request_receive_expect(pmonitor->m_recvfd,
600 MONITOR_ANS_PAM_ACCOUNT, &m);
601 ret = buffer_get_int(&m);
602 msg = buffer_get_string(&m, NULL);
603 buffer_append(&loginmsg, msg, strlen(msg));
604 free(msg);
606 buffer_free(&m);
608 debug3("%s returning %d", __func__, ret);
610 return (ret);
613 void *
614 mm_sshpam_init_ctx(Authctxt *authctxt)
616 Buffer m;
617 int success;
619 debug3("%s", __func__);
620 buffer_init(&m);
621 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
622 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
623 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
624 success = buffer_get_int(&m);
625 if (success == 0) {
626 debug3("%s: pam_init_ctx failed", __func__);
627 buffer_free(&m);
628 return (NULL);
630 buffer_free(&m);
631 return (authctxt);
635 mm_sshpam_query(void *ctx, char **name, char **info,
636 u_int *num, char ***prompts, u_int **echo_on)
638 Buffer m;
639 u_int i;
640 int ret;
642 debug3("%s", __func__);
643 buffer_init(&m);
644 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
645 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
646 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
647 ret = buffer_get_int(&m);
648 debug3("%s: pam_query returned %d", __func__, ret);
649 *name = buffer_get_string(&m, NULL);
650 *info = buffer_get_string(&m, NULL);
651 sshpam_set_maxtries_reached(buffer_get_int(&m));
652 *num = buffer_get_int(&m);
653 if (*num > PAM_MAX_NUM_MSG)
654 fatal("%s: recieved %u PAM messages, expected <= %u",
655 __func__, *num, PAM_MAX_NUM_MSG);
656 *prompts = xcalloc((*num + 1), sizeof(char *));
657 *echo_on = xcalloc((*num + 1), sizeof(u_int));
658 for (i = 0; i < *num; ++i) {
659 (*prompts)[i] = buffer_get_string(&m, NULL);
660 (*echo_on)[i] = buffer_get_int(&m);
662 buffer_free(&m);
663 return (ret);
667 mm_sshpam_respond(void *ctx, u_int num, char **resp)
669 Buffer m;
670 u_int i;
671 int ret;
673 debug3("%s", __func__);
674 buffer_init(&m);
675 buffer_put_int(&m, num);
676 for (i = 0; i < num; ++i)
677 buffer_put_cstring(&m, resp[i]);
678 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
679 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
680 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
681 ret = buffer_get_int(&m);
682 debug3("%s: pam_respond returned %d", __func__, ret);
683 buffer_free(&m);
684 return (ret);
687 void
688 mm_sshpam_free_ctx(void *ctxtp)
690 Buffer m;
692 debug3("%s", __func__);
693 buffer_init(&m);
694 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
695 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
696 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
697 buffer_free(&m);
699 #endif /* USE_PAM */
701 /* Request process termination */
703 void
704 mm_terminate(void)
706 Buffer m;
708 buffer_init(&m);
709 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
710 buffer_free(&m);
713 static void
714 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
715 char ***prompts, u_int **echo_on)
717 *name = xstrdup("");
718 *infotxt = xstrdup("");
719 *numprompts = 1;
720 *prompts = xcalloc(*numprompts, sizeof(char *));
721 *echo_on = xcalloc(*numprompts, sizeof(u_int));
722 (*echo_on)[0] = 0;
726 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
727 u_int *numprompts, char ***prompts, u_int **echo_on)
729 Buffer m;
730 u_int success;
731 char *challenge;
733 debug3("%s: entering", __func__);
735 buffer_init(&m);
736 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
738 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
739 &m);
740 success = buffer_get_int(&m);
741 if (success == 0) {
742 debug3("%s: no challenge", __func__);
743 buffer_free(&m);
744 return (-1);
747 /* Get the challenge, and format the response */
748 challenge = buffer_get_string(&m, NULL);
749 buffer_free(&m);
751 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
752 (*prompts)[0] = challenge;
754 debug3("%s: received challenge: %s", __func__, challenge);
756 return (0);
760 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
762 Buffer m;
763 int authok;
765 debug3("%s: entering", __func__);
766 if (numresponses != 1)
767 return (-1);
769 buffer_init(&m);
770 buffer_put_cstring(&m, responses[0]);
771 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
773 mm_request_receive_expect(pmonitor->m_recvfd,
774 MONITOR_ANS_BSDAUTHRESPOND, &m);
776 authok = buffer_get_int(&m);
777 buffer_free(&m);
779 return ((authok == 0) ? -1 : 0);
782 #ifdef SKEY
784 mm_skey_query(void *ctx, char **name, char **infotxt,
785 u_int *numprompts, char ***prompts, u_int **echo_on)
787 Buffer m;
788 u_int success;
789 char *challenge;
791 debug3("%s: entering", __func__);
793 buffer_init(&m);
794 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
796 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
797 &m);
798 success = buffer_get_int(&m);
799 if (success == 0) {
800 debug3("%s: no challenge", __func__);
801 buffer_free(&m);
802 return (-1);
805 /* Get the challenge, and format the response */
806 challenge = buffer_get_string(&m, NULL);
807 buffer_free(&m);
809 debug3("%s: received challenge: %s", __func__, challenge);
811 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
813 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
814 free(challenge);
816 return (0);
820 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
822 Buffer m;
823 int authok;
825 debug3("%s: entering", __func__);
826 if (numresponses != 1)
827 return (-1);
829 buffer_init(&m);
830 buffer_put_cstring(&m, responses[0]);
831 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
833 mm_request_receive_expect(pmonitor->m_recvfd,
834 MONITOR_ANS_SKEYRESPOND, &m);
836 authok = buffer_get_int(&m);
837 buffer_free(&m);
839 return ((authok == 0) ? -1 : 0);
841 #endif /* SKEY */
843 #ifdef SSH_AUDIT_EVENTS
844 void
845 mm_audit_event(ssh_audit_event_t event)
847 Buffer m;
849 debug3("%s entering", __func__);
851 buffer_init(&m);
852 buffer_put_int(&m, event);
854 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
855 buffer_free(&m);
858 void
859 mm_audit_run_command(const char *command)
861 Buffer m;
863 debug3("%s entering command %s", __func__, command);
865 buffer_init(&m);
866 buffer_put_cstring(&m, command);
868 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
869 buffer_free(&m);
871 #endif /* SSH_AUDIT_EVENTS */
873 #ifdef GSSAPI
874 OM_uint32
875 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
877 Buffer m;
878 OM_uint32 major;
880 /* Client doesn't get to see the context */
881 *ctx = NULL;
883 buffer_init(&m);
884 buffer_put_string(&m, goid->elements, goid->length);
886 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
887 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
889 major = buffer_get_int(&m);
891 buffer_free(&m);
892 return (major);
895 OM_uint32
896 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
897 gss_buffer_desc *out, OM_uint32 *flags)
899 Buffer m;
900 OM_uint32 major;
901 u_int len;
903 buffer_init(&m);
904 buffer_put_string(&m, in->value, in->length);
906 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
907 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
909 major = buffer_get_int(&m);
910 out->value = buffer_get_string(&m, &len);
911 out->length = len;
912 if (flags)
913 *flags = buffer_get_int(&m);
915 buffer_free(&m);
917 return (major);
920 OM_uint32
921 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
923 Buffer m;
924 OM_uint32 major;
926 buffer_init(&m);
927 buffer_put_string(&m, gssbuf->value, gssbuf->length);
928 buffer_put_string(&m, gssmic->value, gssmic->length);
930 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
931 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
932 &m);
934 major = buffer_get_int(&m);
935 buffer_free(&m);
936 return(major);
940 mm_ssh_gssapi_userok(char *user)
942 Buffer m;
943 int authenticated = 0;
945 buffer_init(&m);
947 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
948 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
949 &m);
951 authenticated = buffer_get_int(&m);
953 buffer_free(&m);
954 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
955 return (authenticated);
957 #endif /* GSSAPI */