xref: /dragonfly/crypto/openssh/monitor_wrap.c (revision 7bcb6caf)
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.
6  *
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.
15  *
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.
26  */
27 
28 #include "includes.h"
29 
30 #include <sys/types.h>
31 #include <sys/uio.h>
32 
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>
40 
41 #ifdef WITH_OPENSSL
42 #include <openssl/bn.h>
43 #include <openssl/dh.h>
44 #include <openssl/evp.h>
45 #endif
46 
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"
80 
81 #include "channels.h"
82 #include "session.h"
83 #include "servconf.h"
84 
85 #include "ssherr.h"
86 
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;
93 
94 void
95 mm_log_handler(LogLevel level, const char *msg, void *ctx)
96 {
97 	Buffer log_msg;
98 	struct monitor *mon = (struct monitor *)ctx;
99 
100 	if (mon->m_log_sendfd == -1)
101 		fatal("%s: no log channel", __func__);
102 
103 	buffer_init(&log_msg);
104 	/*
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.
108 	 */
109 	buffer_put_int(&log_msg, 0);
110 
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);
118 }
119 
120 int
121 mm_is_monitor(void)
122 {
123 	/*
124 	 * m_pid is only set in the privileged part, and
125 	 * points to the unprivileged child.
126 	 */
127 	return (pmonitor && pmonitor->m_pid > 0);
128 }
129 
130 void
131 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
132 {
133 	u_int mlen = buffer_len(m);
134 	u_char buf[5];
135 
136 	debug3("%s entering: type %d", __func__, type);
137 
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));
144 }
145 
146 void
147 mm_request_receive(int sock, Buffer *m)
148 {
149 	u_char buf[4];
150 	u_int msg_len;
151 
152 	debug3("%s entering", __func__);
153 
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));
158 	}
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));
166 }
167 
168 void
169 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
170 {
171 	u_char rtype;
172 
173 	debug3("%s entering: type %d", __func__, type);
174 
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);
180 }
181 
182 #ifdef WITH_OPENSSL
183 DH *
184 mm_choose_dh(int min, int nbits, int max)
185 {
186 	BIGNUM *p, *g;
187 	int success = 0;
188 	Buffer m;
189 
190 	buffer_init(&m);
191 	buffer_put_int(&m, min);
192 	buffer_put_int(&m, nbits);
193 	buffer_put_int(&m, max);
194 
195 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
196 
197 	debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
198 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
199 
200 	success = buffer_get_char(&m);
201 	if (success == 0)
202 		fatal("%s: MONITOR_ANS_MODULI failed", __func__);
203 
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);
210 
211 	debug3("%s: remaining %d", __func__, buffer_len(&m));
212 	buffer_free(&m);
213 
214 	return (dh_new_group(g, p));
215 }
216 #endif
217 
218 int
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)
221 {
222 	struct kex *kex = *pmonitor->m_pkex;
223 	Buffer m;
224 
225 	debug3("%s entering", __func__);
226 
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);
231 
232 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
233 
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);
238 
239 	return (0);
240 }
241 
242 struct passwd *
243 mm_getpwnamallow(const char *username)
244 {
245 	struct ssh *ssh = active_state;		/* XXX */
246 	Buffer m;
247 	struct passwd *pw;
248 	u_int len, i;
249 	ServerOptions *newopts;
250 
251 	debug3("%s entering", __func__);
252 
253 	buffer_init(&m);
254 	buffer_put_cstring(&m, username);
255 
256 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
257 
258 	debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
259 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
260 
261 	if (buffer_get_char(&m) == 0) {
262 		pw = NULL;
263 		goto out;
264 	}
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);
278 
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__);
284 
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
303 
304 	copy_set_server_options(&options, newopts, 1);
305 	log_change_level(options.log_level);
306 	process_permitopen(ssh, &options);
307 	free(newopts);
308 
309 	buffer_free(&m);
310 
311 	return (pw);
312 }
313 
314 char *
315 mm_auth2_read_banner(void)
316 {
317 	Buffer m;
318 	char *banner;
319 
320 	debug3("%s entering", __func__);
321 
322 	buffer_init(&m);
323 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
324 	buffer_clear(&m);
325 
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);
330 
331 	/* treat empty banner as missing banner */
332 	if (strlen(banner) == 0) {
333 		free(banner);
334 		banner = NULL;
335 	}
336 	return (banner);
337 }
338 
339 /* Inform the privileged process about service and style */
340 
341 void
342 mm_inform_authserv(char *service, char *style)
343 {
344 	Buffer m;
345 
346 	debug3("%s entering", __func__);
347 
348 	buffer_init(&m);
349 	buffer_put_cstring(&m, service);
350 	buffer_put_cstring(&m, style ? style : "");
351 
352 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
353 
354 	buffer_free(&m);
355 }
356 
357 /* Do the password authentication */
358 int
359 mm_auth_password(Authctxt *authctxt, char *password)
360 {
361 	Buffer m;
362 	int authenticated = 0;
363 
364 	debug3("%s entering", __func__);
365 
366 	buffer_init(&m);
367 	buffer_put_cstring(&m, password);
368 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
369 
370 	debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
371 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
372 
373 	authenticated = buffer_get_int(&m);
374 #ifdef USE_PAM
375 	sshpam_set_maxtries_reached(buffer_get_int(&m));
376 #endif
377 
378 	buffer_free(&m);
379 
380 	debug3("%s: user %sauthenticated",
381 	    __func__, authenticated ? "" : "not ");
382 	return (authenticated);
383 }
384 
385 int
386 mm_user_key_allowed(struct passwd *pw, struct sshkey *key,
387     int pubkey_auth_attempt)
388 {
389 	return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
390 	    pubkey_auth_attempt));
391 }
392 
393 int
394 mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host,
395     struct sshkey *key)
396 {
397 	return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0));
398 }
399 
400 int
401 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
402     struct sshkey *key, int pubkey_auth_attempt)
403 {
404 	Buffer m;
405 	u_char *blob;
406 	u_int len;
407 	int allowed = 0, have_forced = 0;
408 
409 	debug3("%s entering", __func__);
410 
411 	/* Convert the key to a blob and the pass it over */
412 	if (!key_to_blob(key, &blob, &len))
413 		return (0);
414 
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);
422 
423 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
424 
425 	debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
426 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
427 
428 	allowed = buffer_get_int(&m);
429 
430 	/* fake forced command */
431 	auth_clear_options();
432 	have_forced = buffer_get_int(&m);
433 	forced_command = have_forced ? xstrdup("true") : NULL;
434 
435 	buffer_free(&m);
436 
437 	return (allowed);
438 }
439 
440 /*
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.
444  */
445 
446 int
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)
449 {
450 	Buffer m;
451 	u_char *blob;
452 	u_int len;
453 	u_int encoded_ret = 0;
454 
455 	debug3("%s entering", __func__);
456 
457 	/* Convert the key to a blob and the pass it over */
458 	if (!key_to_blob(key, &blob, &len))
459 		return (0);
460 
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);
466 
467 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
468 
469 	debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
470 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
471 
472 	encoded_ret = buffer_get_int(&m);
473 
474 	buffer_free(&m);
475 
476 	if (encoded_ret != 0)
477 		return SSH_ERR_SIGNATURE_INVALID;
478 	return 0;
479 }
480 
481 void
482 mm_send_keystate(struct monitor *monitor)
483 {
484 	struct ssh *ssh = active_state;		/* XXX */
485 	struct sshbuf *m;
486 	int r;
487 
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);
496 }
497 
498 int
499 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
500 {
501 	Buffer m;
502 	char *p, *msg;
503 	int success = 0, tmp1 = -1, tmp2 = -1;
504 
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;
514 	}
515 	close(tmp1);
516 	close(tmp2);
517 
518 	buffer_init(&m);
519 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
520 
521 	debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
522 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
523 
524 	success = buffer_get_int(&m);
525 	if (success == 0) {
526 		debug3("%s: pty alloc failed", __func__);
527 		buffer_free(&m);
528 		return (0);
529 	}
530 	p = buffer_get_string(&m, NULL);
531 	msg = buffer_get_string(&m, NULL);
532 	buffer_free(&m);
533 
534 	strlcpy(namebuf, p, namebuflen); /* Possible truncation */
535 	free(p);
536 
537 	buffer_append(&loginmsg, msg, strlen(msg));
538 	free(msg);
539 
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__);
543 
544 	/* Success */
545 	return (1);
546 }
547 
548 void
549 mm_session_pty_cleanup2(Session *s)
550 {
551 	Buffer m;
552 
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);
559 
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));
564 
565 	/* unlink pty from session */
566 	s->ttyfd = -1;
567 }
568 
569 #ifdef USE_PAM
570 void
571 mm_start_pam(Authctxt *authctxt)
572 {
573 	Buffer m;
574 
575 	debug3("%s entering", __func__);
576 	if (!options.use_pam)
577 		fatal("UsePAM=no, but ended up in %s anyway", __func__);
578 
579 	buffer_init(&m);
580 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
581 
582 	buffer_free(&m);
583 }
584 
585 u_int
586 mm_do_pam_account(void)
587 {
588 	Buffer m;
589 	u_int ret;
590 	char *msg;
591 
592 	debug3("%s entering", __func__);
593 	if (!options.use_pam)
594 		fatal("UsePAM=no, but ended up in %s anyway", __func__);
595 
596 	buffer_init(&m);
597 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
598 
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);
605 
606 	buffer_free(&m);
607 
608 	debug3("%s returning %d", __func__, ret);
609 
610 	return (ret);
611 }
612 
613 void *
614 mm_sshpam_init_ctx(Authctxt *authctxt)
615 {
616 	Buffer m;
617 	int success;
618 
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);
629 	}
630 	buffer_free(&m);
631 	return (authctxt);
632 }
633 
634 int
635 mm_sshpam_query(void *ctx, char **name, char **info,
636     u_int *num, char ***prompts, u_int **echo_on)
637 {
638 	Buffer m;
639 	u_int i;
640 	int ret;
641 
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);
661 	}
662 	buffer_free(&m);
663 	return (ret);
664 }
665 
666 int
667 mm_sshpam_respond(void *ctx, u_int num, char **resp)
668 {
669 	Buffer m;
670 	u_int i;
671 	int ret;
672 
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);
685 }
686 
687 void
688 mm_sshpam_free_ctx(void *ctxtp)
689 {
690 	Buffer m;
691 
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);
698 }
699 #endif /* USE_PAM */
700 
701 /* Request process termination */
702 
703 void
704 mm_terminate(void)
705 {
706 	Buffer m;
707 
708 	buffer_init(&m);
709 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
710 	buffer_free(&m);
711 }
712 
713 static void
714 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
715     char ***prompts, u_int **echo_on)
716 {
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;
723 }
724 
725 int
726 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
727    u_int *numprompts, char ***prompts, u_int **echo_on)
728 {
729 	Buffer m;
730 	u_int success;
731 	char *challenge;
732 
733 	debug3("%s: entering", __func__);
734 
735 	buffer_init(&m);
736 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
737 
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);
745 	}
746 
747 	/* Get the challenge, and format the response */
748 	challenge  = buffer_get_string(&m, NULL);
749 	buffer_free(&m);
750 
751 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
752 	(*prompts)[0] = challenge;
753 
754 	debug3("%s: received challenge: %s", __func__, challenge);
755 
756 	return (0);
757 }
758 
759 int
760 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
761 {
762 	Buffer m;
763 	int authok;
764 
765 	debug3("%s: entering", __func__);
766 	if (numresponses != 1)
767 		return (-1);
768 
769 	buffer_init(&m);
770 	buffer_put_cstring(&m, responses[0]);
771 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
772 
773 	mm_request_receive_expect(pmonitor->m_recvfd,
774 	    MONITOR_ANS_BSDAUTHRESPOND, &m);
775 
776 	authok = buffer_get_int(&m);
777 	buffer_free(&m);
778 
779 	return ((authok == 0) ? -1 : 0);
780 }
781 
782 #ifdef SKEY
783 int
784 mm_skey_query(void *ctx, char **name, char **infotxt,
785    u_int *numprompts, char ***prompts, u_int **echo_on)
786 {
787 	Buffer m;
788 	u_int success;
789 	char *challenge;
790 
791 	debug3("%s: entering", __func__);
792 
793 	buffer_init(&m);
794 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
795 
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);
803 	}
804 
805 	/* Get the challenge, and format the response */
806 	challenge  = buffer_get_string(&m, NULL);
807 	buffer_free(&m);
808 
809 	debug3("%s: received challenge: %s", __func__, challenge);
810 
811 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
812 
813 	xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
814 	free(challenge);
815 
816 	return (0);
817 }
818 
819 int
820 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
821 {
822 	Buffer m;
823 	int authok;
824 
825 	debug3("%s: entering", __func__);
826 	if (numresponses != 1)
827 		return (-1);
828 
829 	buffer_init(&m);
830 	buffer_put_cstring(&m, responses[0]);
831 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
832 
833 	mm_request_receive_expect(pmonitor->m_recvfd,
834 	    MONITOR_ANS_SKEYRESPOND, &m);
835 
836 	authok = buffer_get_int(&m);
837 	buffer_free(&m);
838 
839 	return ((authok == 0) ? -1 : 0);
840 }
841 #endif /* SKEY */
842 
843 #ifdef SSH_AUDIT_EVENTS
844 void
845 mm_audit_event(ssh_audit_event_t event)
846 {
847 	Buffer m;
848 
849 	debug3("%s entering", __func__);
850 
851 	buffer_init(&m);
852 	buffer_put_int(&m, event);
853 
854 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
855 	buffer_free(&m);
856 }
857 
858 void
859 mm_audit_run_command(const char *command)
860 {
861 	Buffer m;
862 
863 	debug3("%s entering command %s", __func__, command);
864 
865 	buffer_init(&m);
866 	buffer_put_cstring(&m, command);
867 
868 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
869 	buffer_free(&m);
870 }
871 #endif /* SSH_AUDIT_EVENTS */
872 
873 #ifdef GSSAPI
874 OM_uint32
875 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
876 {
877 	Buffer m;
878 	OM_uint32 major;
879 
880 	/* Client doesn't get to see the context */
881 	*ctx = NULL;
882 
883 	buffer_init(&m);
884 	buffer_put_string(&m, goid->elements, goid->length);
885 
886 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
887 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
888 
889 	major = buffer_get_int(&m);
890 
891 	buffer_free(&m);
892 	return (major);
893 }
894 
895 OM_uint32
896 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
897     gss_buffer_desc *out, OM_uint32 *flags)
898 {
899 	Buffer m;
900 	OM_uint32 major;
901 	u_int len;
902 
903 	buffer_init(&m);
904 	buffer_put_string(&m, in->value, in->length);
905 
906 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
907 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
908 
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);
914 
915 	buffer_free(&m);
916 
917 	return (major);
918 }
919 
920 OM_uint32
921 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
922 {
923 	Buffer m;
924 	OM_uint32 major;
925 
926 	buffer_init(&m);
927 	buffer_put_string(&m, gssbuf->value, gssbuf->length);
928 	buffer_put_string(&m, gssmic->value, gssmic->length);
929 
930 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
931 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
932 	    &m);
933 
934 	major = buffer_get_int(&m);
935 	buffer_free(&m);
936 	return(major);
937 }
938 
939 int
940 mm_ssh_gssapi_userok(char *user)
941 {
942 	Buffer m;
943 	int authenticated = 0;
944 
945 	buffer_init(&m);
946 
947 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
948 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
949 				  &m);
950 
951 	authenticated = buffer_get_int(&m);
952 
953 	buffer_free(&m);
954 	debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
955 	return (authenticated);
956 }
957 #endif /* GSSAPI */
958 
959