xref: /netbsd-src/crypto/external/bsd/openssh/dist/monitor_wrap.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: monitor_wrap.c,v 1.16 2016/12/25 00:07:47 christos Exp $	*/
2 /* $OpenBSD: monitor_wrap.c,v 1.89 2016/08/13 17:47:41 markus Exp $ */
3 
4 /*
5  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
6  * Copyright 2002 Markus Friedl <markus@openbsd.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "includes.h"
31 __RCSID("$NetBSD: monitor_wrap.c,v 1.16 2016/12/25 00:07:47 christos Exp $");
32 #include <sys/types.h>
33 #include <sys/uio.h>
34 #include <sys/queue.h>
35 
36 #include <errno.h>
37 #include <pwd.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #ifdef WITH_OPENSSL
44 #include <openssl/bn.h>
45 #include <openssl/dh.h>
46 #endif
47 
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 <zlib.h>
64 #include "monitor.h"
65 #ifdef GSSAPI
66 #include "ssh-gss.h"
67 #endif
68 #include "monitor_wrap.h"
69 #include "atomicio.h"
70 #include "monitor_fdpass.h"
71 #ifdef USE_PAM
72 #include "misc.h"
73 #include "servconf.h"
74 #include <security/pam_appl.h>
75 #endif
76 #include "misc.h"
77 #include "uuencode.h"
78 
79 #include "channels.h"
80 #include "session.h"
81 #include "servconf.h"
82 
83 #include "ssherr.h"
84 
85 /* Imports */
86 extern z_stream incoming_stream;
87 extern z_stream outgoing_stream;
88 extern struct monitor *pmonitor;
89 extern Buffer loginmsg;
90 extern ServerOptions options;
91 
92 void
93 mm_log_handler(LogLevel level, const char *msg, void *ctx)
94 {
95 	Buffer log_msg;
96 	struct monitor *mon = (struct monitor *)ctx;
97 
98 	if (mon->m_log_sendfd == -1)
99 		fatal("%s: no log channel", __func__);
100 
101 	buffer_init(&log_msg);
102 	/*
103 	 * Placeholder for packet length. Will be filled in with the actual
104 	 * packet length once the packet has been constucted. This saves
105 	 * fragile math.
106 	 */
107 	buffer_put_int(&log_msg, 0);
108 
109 	buffer_put_int(&log_msg, level);
110 	buffer_put_cstring(&log_msg, msg);
111 	put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4);
112 	if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg),
113 	    buffer_len(&log_msg)) != buffer_len(&log_msg))
114 		fatal("%s: write: %s", __func__, strerror(errno));
115 	buffer_free(&log_msg);
116 }
117 
118 int
119 mm_is_monitor(void)
120 {
121 	/*
122 	 * m_pid is only set in the privileged part, and
123 	 * points to the unprivileged child.
124 	 */
125 	return (pmonitor && pmonitor->m_pid > 0);
126 }
127 
128 void
129 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
130 {
131 	u_int mlen = buffer_len(m);
132 	u_char buf[5];
133 
134 	debug3("%s entering: type %d", __func__, type);
135 
136 	put_u32(buf, mlen + 1);
137 	buf[4] = (u_char) type;		/* 1st byte of payload is mesg-type */
138 	if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
139 		fatal("%s: write: %s", __func__, strerror(errno));
140 	if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
141 		fatal("%s: write: %s", __func__, strerror(errno));
142 }
143 
144 void
145 mm_request_receive(int sock, Buffer *m)
146 {
147 	u_char buf[4];
148 	u_int msg_len;
149 
150 	debug3("%s entering", __func__);
151 
152 	if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
153 		if (errno == EPIPE)
154 			cleanup_exit(255);
155 		fatal("%s: read: %s", __func__, strerror(errno));
156 	}
157 	msg_len = get_u32(buf);
158 	if (msg_len > 256 * 1024)
159 		fatal("%s: read: bad msg_len %d", __func__, msg_len);
160 	buffer_clear(m);
161 	buffer_append_space(m, msg_len);
162 	if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
163 		fatal("%s: read: %s", __func__, strerror(errno));
164 }
165 
166 void
167 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
168 {
169 	u_char rtype;
170 
171 	debug3("%s entering: type %d", __func__, type);
172 
173 	mm_request_receive(sock, m);
174 	rtype = buffer_get_char(m);
175 	if (rtype != type)
176 		fatal("%s: read: rtype %d != type %d", __func__,
177 		    rtype, type);
178 }
179 
180 #ifdef WITH_OPENSSL
181 DH *
182 mm_choose_dh(int min, int nbits, int max)
183 {
184 	BIGNUM *p, *g;
185 	int success = 0;
186 	Buffer m;
187 
188 	buffer_init(&m);
189 	buffer_put_int(&m, min);
190 	buffer_put_int(&m, nbits);
191 	buffer_put_int(&m, max);
192 
193 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
194 
195 	debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
196 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
197 
198 	success = buffer_get_char(&m);
199 	if (success == 0)
200 		fatal("%s: MONITOR_ANS_MODULI failed", __func__);
201 
202 	if ((p = BN_new()) == NULL)
203 		fatal("%s: BN_new failed", __func__);
204 	if ((g = BN_new()) == NULL)
205 		fatal("%s: BN_new failed", __func__);
206 	buffer_get_bignum2(&m, p);
207 	buffer_get_bignum2(&m, g);
208 
209 	debug3("%s: remaining %d", __func__, buffer_len(&m));
210 	buffer_free(&m);
211 
212 	return (dh_new_group(g, p));
213 }
214 #endif
215 
216 int
217 mm_key_sign(Key *key, u_char **sigp, u_int *lenp,
218     const u_char *data, u_int datalen, const char *hostkey_alg)
219 {
220 	struct kex *kex = *pmonitor->m_pkex;
221 	Buffer m;
222 
223 	debug3("%s entering", __func__);
224 
225 	buffer_init(&m);
226 	buffer_put_int(&m, kex->host_key_index(key, 0, active_state));
227 	buffer_put_string(&m, data, datalen);
228 	buffer_put_cstring(&m, hostkey_alg);
229 
230 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
231 
232 	debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
233 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
234 	*sigp  = buffer_get_string(&m, lenp);
235 	buffer_free(&m);
236 
237 	return (0);
238 }
239 
240 struct passwd *
241 mm_getpwnamallow(const char *username)
242 {
243 	Buffer m;
244 	struct passwd *pw;
245 	u_int len, i;
246 	ServerOptions *newopts;
247 
248 	debug3("%s entering", __func__);
249 
250 	buffer_init(&m);
251 	buffer_put_cstring(&m, username);
252 
253 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
254 
255 	debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
256 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
257 
258 	if (buffer_get_char(&m) == 0) {
259 		pw = NULL;
260 		goto out;
261 	}
262 	pw = buffer_get_string(&m, &len);
263 	if (len != sizeof(struct passwd))
264 		fatal("%s: struct passwd size mismatch", __func__);
265 	pw->pw_name = buffer_get_string(&m, NULL);
266 	pw->pw_passwd = buffer_get_string(&m, NULL);
267 	pw->pw_gecos = buffer_get_string(&m, NULL);
268 	pw->pw_class = buffer_get_string(&m, NULL);
269 	pw->pw_dir = buffer_get_string(&m, NULL);
270 	pw->pw_shell = buffer_get_string(&m, NULL);
271 
272 out:
273 	/* copy options block as a Match directive may have changed some */
274 	newopts = buffer_get_string(&m, &len);
275 	if (len != sizeof(*newopts))
276 		fatal("%s: option block size mismatch", __func__);
277 
278 #define M_CP_STROPT(x) do { \
279 		if (newopts->x != NULL) \
280 			newopts->x = buffer_get_string(&m, NULL); \
281 	} while (0)
282 #define M_CP_STRARRAYOPT(x, nx) do { \
283 		for (i = 0; i < newopts->nx; i++) \
284 			newopts->x[i] = buffer_get_string(&m, NULL); \
285 	} while (0)
286 	/* See comment in servconf.h */
287 	COPY_MATCH_STRING_OPTS();
288 #undef M_CP_STROPT
289 #undef M_CP_STRARRAYOPT
290 
291 	copy_set_server_options(&options, newopts, 1);
292 	free(newopts);
293 
294 	buffer_free(&m);
295 
296 	return (pw);
297 }
298 
299 char *
300 mm_auth2_read_banner(void)
301 {
302 	Buffer m;
303 	char *banner;
304 
305 	debug3("%s entering", __func__);
306 
307 	buffer_init(&m);
308 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
309 	buffer_clear(&m);
310 
311 	mm_request_receive_expect(pmonitor->m_recvfd,
312 	    MONITOR_ANS_AUTH2_READ_BANNER, &m);
313 	banner = buffer_get_string(&m, NULL);
314 	buffer_free(&m);
315 
316 	/* treat empty banner as missing banner */
317 	if (strlen(banner) == 0) {
318 		free(banner);
319 		banner = NULL;
320 	}
321 	return (banner);
322 }
323 
324 /* Inform the privileged process about service and style */
325 
326 void
327 mm_inform_authserv(char *service, char *style)
328 {
329 	Buffer m;
330 
331 	debug3("%s entering", __func__);
332 
333 	buffer_init(&m);
334 	buffer_put_cstring(&m, service);
335 	buffer_put_cstring(&m, style ? style : "");
336 
337 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
338 
339 	buffer_free(&m);
340 }
341 
342 /* Do the password authentication */
343 int
344 mm_auth_password(Authctxt *authctxt, const char *password)
345 {
346 	Buffer m;
347 	int authenticated = 0;
348 
349 	debug3("%s entering", __func__);
350 
351 	buffer_init(&m);
352 	buffer_put_cstring(&m, password);
353 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
354 
355 	debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
356 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
357 
358 	authenticated = buffer_get_int(&m);
359 
360 	buffer_free(&m);
361 
362 	debug3("%s: user %sauthenticated",
363 	    __func__, authenticated ? "" : "not ");
364 	return (authenticated);
365 }
366 
367 int
368 mm_user_key_allowed(struct passwd *pw, Key *key, int pubkey_auth_attempt)
369 {
370 	return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
371 	    pubkey_auth_attempt));
372 }
373 
374 int
375 mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host,
376     Key *key)
377 {
378 	return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0));
379 }
380 
381 int
382 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
383     Key *key, int pubkey_auth_attempt)
384 {
385 	Buffer m;
386 	u_char *blob;
387 	u_int len;
388 	int allowed = 0, have_forced = 0;
389 
390 	debug3("%s entering", __func__);
391 
392 	/* Convert the key to a blob and the pass it over */
393 	if (!key_to_blob(key, &blob, &len))
394 		return (0);
395 
396 	buffer_init(&m);
397 	buffer_put_int(&m, type);
398 	buffer_put_cstring(&m, user ? user : "");
399 	buffer_put_cstring(&m, host ? host : "");
400 	buffer_put_string(&m, blob, len);
401 	buffer_put_int(&m, pubkey_auth_attempt);
402 	free(blob);
403 
404 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
405 
406 	debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
407 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
408 
409 	allowed = buffer_get_int(&m);
410 
411 	/* fake forced command */
412 	auth_clear_options();
413 	have_forced = buffer_get_int(&m);
414 	forced_command = have_forced ? xstrdup("true") : NULL;
415 
416 	buffer_free(&m);
417 
418 	return (allowed);
419 }
420 
421 /*
422  * This key verify needs to send the key type along, because the
423  * privileged parent makes the decision if the key is allowed
424  * for authentication.
425  */
426 
427 int
428 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
429 {
430 	Buffer m;
431 	u_char *blob;
432 	u_int len;
433 	int verified = 0;
434 
435 	debug3("%s entering", __func__);
436 
437 	/* Convert the key to a blob and the pass it over */
438 	if (!key_to_blob(key, &blob, &len))
439 		return (0);
440 
441 	buffer_init(&m);
442 	buffer_put_string(&m, blob, len);
443 	buffer_put_string(&m, sig, siglen);
444 	buffer_put_string(&m, data, datalen);
445 	free(blob);
446 
447 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
448 
449 	debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
450 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
451 
452 	verified = buffer_get_int(&m);
453 
454 	buffer_free(&m);
455 
456 	return (verified);
457 }
458 
459 void
460 mm_send_keystate(struct monitor *monitor)
461 {
462 	struct ssh *ssh = active_state;		/* XXX */
463 	struct sshbuf *m;
464 	int r;
465 
466 	if ((m = sshbuf_new()) == NULL)
467 		fatal("%s: sshbuf_new failed", __func__);
468 	if ((r = ssh_packet_get_state(ssh, m)) != 0)
469 		fatal("%s: get_state failed: %s",
470 		    __func__, ssh_err(r));
471 	mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
472 	debug3("%s: Finished sending state", __func__);
473 	sshbuf_free(m);
474 }
475 
476 int
477 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
478 {
479 	Buffer m;
480 	char *p, *msg;
481 	int success = 0, tmp1 = -1, tmp2 = -1;
482 
483 	/* Kludge: ensure there are fds free to receive the pty/tty */
484 	if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
485 	    (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
486 		error("%s: cannot allocate fds for pty", __func__);
487 		if (tmp1 > 0)
488 			close(tmp1);
489 		if (tmp2 > 0)
490 			close(tmp2);
491 		return 0;
492 	}
493 	close(tmp1);
494 	close(tmp2);
495 
496 	buffer_init(&m);
497 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
498 
499 	debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
500 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
501 
502 	success = buffer_get_int(&m);
503 	if (success == 0) {
504 		debug3("%s: pty alloc failed", __func__);
505 		buffer_free(&m);
506 		return (0);
507 	}
508 	p = buffer_get_string(&m, NULL);
509 	msg = buffer_get_string(&m, NULL);
510 	buffer_free(&m);
511 
512 	strlcpy(namebuf, p, namebuflen); /* Possible truncation */
513 	free(p);
514 
515 	buffer_append(&loginmsg, msg, strlen(msg));
516 	free(msg);
517 
518 	if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
519 	    (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
520 		fatal("%s: receive fds failed", __func__);
521 
522 	/* Success */
523 	return (1);
524 }
525 
526 void
527 mm_session_pty_cleanup2(Session *s)
528 {
529 	Buffer m;
530 
531 	if (s->ttyfd == -1)
532 		return;
533 	buffer_init(&m);
534 	buffer_put_cstring(&m, s->tty);
535 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
536 	buffer_free(&m);
537 
538 	/* closed dup'ed master */
539 	if (s->ptymaster != -1 && close(s->ptymaster) < 0)
540 		error("close(s->ptymaster/%d): %s",
541 		    s->ptymaster, strerror(errno));
542 
543 	/* unlink pty from session */
544 	s->ttyfd = -1;
545 }
546 
547 #ifdef USE_PAM
548 void
549 mm_start_pam(Authctxt *authctxt)
550 {
551 	Buffer m;
552 
553 	debug3("%s entering", __func__);
554 	if (!options.use_pam)
555 		fatal("UsePAM=no, but ended up in %s anyway", __func__);
556 
557 	buffer_init(&m);
558 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
559 
560 	buffer_free(&m);
561 }
562 
563 u_int
564 mm_do_pam_account(void)
565 {
566 	Buffer m;
567 	u_int ret;
568 	char *msg;
569 
570 	debug3("%s entering", __func__);
571 	if (!options.use_pam)
572 		fatal("UsePAM=no, but ended up in %s anyway", __func__);
573 
574 	buffer_init(&m);
575 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
576 
577 	mm_request_receive_expect(pmonitor->m_recvfd,
578 	    MONITOR_ANS_PAM_ACCOUNT, &m);
579 	ret = buffer_get_int(&m);
580 	msg = buffer_get_string(&m, NULL);
581 	buffer_append(&loginmsg, msg, strlen(msg));
582 	free(msg);
583 
584 	buffer_free(&m);
585 
586 	debug3("%s returning %d", __func__, ret);
587 
588 	return (ret);
589 }
590 
591 void *
592 mm_sshpam_init_ctx(Authctxt *authctxt)
593 {
594 	Buffer m;
595 	int success;
596 
597 	debug3("%s", __func__);
598 	buffer_init(&m);
599 	buffer_put_cstring(&m, authctxt->user);
600 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
601 	debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
602 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
603 	success = buffer_get_int(&m);
604 	if (success == 0) {
605 		debug3("%s: pam_init_ctx failed", __func__);
606 		buffer_free(&m);
607 		return (NULL);
608 	}
609 	buffer_free(&m);
610 	return (authctxt);
611 }
612 
613 int
614 mm_sshpam_query(void *ctx, char **name, char **info,
615     u_int *num, char ***prompts, u_int **echo_on)
616 {
617 	Buffer m;
618 	u_int i;
619 	int ret;
620 
621 	debug3("%s", __func__);
622 	buffer_init(&m);
623 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
624 	debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
625 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
626 	ret = buffer_get_int(&m);
627 	debug3("%s: pam_query returned %d", __func__, ret);
628 	*name = buffer_get_string(&m, NULL);
629 	*info = buffer_get_string(&m, NULL);
630 	*num = buffer_get_int(&m);
631 	if (*num > PAM_MAX_NUM_MSG)
632 		fatal("%s: received %u PAM messages, expected <= %u",
633 		    __func__, *num, PAM_MAX_NUM_MSG);
634 	*prompts = xcalloc((*num + 1), sizeof(char *));
635 	*echo_on = xcalloc((*num + 1), sizeof(u_int));
636 	for (i = 0; i < *num; ++i) {
637 		(*prompts)[i] = buffer_get_string(&m, NULL);
638 		(*echo_on)[i] = buffer_get_int(&m);
639 	}
640 	buffer_free(&m);
641 	return (ret);
642 }
643 
644 int
645 mm_sshpam_respond(void *ctx, u_int num, char **resp)
646 {
647 	Buffer m;
648 	u_int i;
649 	int ret;
650 
651 	debug3("%s", __func__);
652 	buffer_init(&m);
653 	buffer_put_int(&m, num);
654 	for (i = 0; i < num; ++i)
655 		buffer_put_cstring(&m, resp[i]);
656 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
657 	debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
658 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
659 	ret = buffer_get_int(&m);
660 	debug3("%s: pam_respond returned %d", __func__, ret);
661 	buffer_free(&m);
662 	return (ret);
663 }
664 
665 void
666 mm_sshpam_free_ctx(void *ctxtp)
667 {
668 	Buffer m;
669 
670 	debug3("%s", __func__);
671 	buffer_init(&m);
672 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
673 	debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
674 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
675 	buffer_free(&m);
676 }
677 #endif /* USE_PAM */
678 
679 /* Request process termination */
680 
681 void
682 mm_terminate(void)
683 {
684 	Buffer m;
685 
686 	buffer_init(&m);
687 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
688 	buffer_free(&m);
689 }
690 
691 #if defined(BSD_AUTH) || defined(SKEY)
692 static void
693 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
694     char ***prompts, u_int **echo_on)
695 {
696 	*name = xstrdup("");
697 	*infotxt = xstrdup("");
698 	*numprompts = 1;
699 	*prompts = xcalloc(*numprompts, sizeof(char *));
700 	*echo_on = xcalloc(*numprompts, sizeof(u_int));
701 	(*echo_on)[0] = 0;
702 }
703 
704 #ifdef BSD_AUTH
705 int
706 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
707    u_int *numprompts, char ***prompts, u_int **echo_on)
708 {
709 	Buffer m;
710 	u_int success;
711 	char *challenge;
712 
713 	debug3("%s: entering", __func__);
714 
715 	buffer_init(&m);
716 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
717 
718 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
719 	    &m);
720 	success = buffer_get_int(&m);
721 	if (success == 0) {
722 		debug3("%s: no challenge", __func__);
723 		buffer_free(&m);
724 		return (-1);
725 	}
726 
727 	/* Get the challenge, and format the response */
728 	challenge  = buffer_get_string(&m, NULL);
729 	buffer_free(&m);
730 
731 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
732 	(*prompts)[0] = challenge;
733 
734 	debug3("%s: received challenge: %s", __func__, challenge);
735 
736 	return (0);
737 }
738 
739 int
740 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
741 {
742 	Buffer m;
743 	int authok;
744 
745 	debug3("%s: entering", __func__);
746 	if (numresponses != 1)
747 		return (-1);
748 
749 	buffer_init(&m);
750 	buffer_put_cstring(&m, responses[0]);
751 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
752 
753 	mm_request_receive_expect(pmonitor->m_recvfd,
754 	    MONITOR_ANS_BSDAUTHRESPOND, &m);
755 
756 	authok = buffer_get_int(&m);
757 	buffer_free(&m);
758 
759 	return ((authok == 0) ? -1 : 0);
760 }
761 #endif
762 
763 #ifdef SKEY
764 int
765 mm_skey_query(void *ctx, char **name, char **infotxt,
766    u_int *numprompts, char ***prompts, u_int **echo_on)
767 {
768 	Buffer m;
769 	u_int success;
770 	char *challenge;
771 
772 	debug3("%s: entering", __func__);
773 
774 	buffer_init(&m);
775 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
776 
777 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
778 	    &m);
779 	success = buffer_get_int(&m);
780 	if (success == 0) {
781 		debug3("%s: no challenge", __func__);
782 		buffer_free(&m);
783 		return (-1);
784 	}
785 
786 	/* Get the challenge, and format the response */
787 	challenge  = buffer_get_string(&m, NULL);
788 	buffer_free(&m);
789 
790 	debug3("%s: received challenge: %s", __func__, challenge);
791 
792 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
793 
794 	xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
795 	free(challenge);
796 
797 	return (0);
798 }
799 
800 int
801 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
802 {
803 	Buffer m;
804 	int authok;
805 
806 	debug3("%s: entering", __func__);
807 	if (numresponses != 1)
808 		return (-1);
809 
810 	buffer_init(&m);
811 	buffer_put_cstring(&m, responses[0]);
812 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
813 
814 	mm_request_receive_expect(pmonitor->m_recvfd,
815 	    MONITOR_ANS_SKEYRESPOND, &m);
816 
817 	authok = buffer_get_int(&m);
818 	buffer_free(&m);
819 
820 	return ((authok == 0) ? -1 : 0);
821 }
822 #endif /* SKEY */
823 #endif /* BSDAUTH || SKEY */
824 
825 #ifdef GSSAPI
826 OM_uint32
827 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
828 {
829 	Buffer m;
830 	OM_uint32 major;
831 
832 	/* Client doesn't get to see the context */
833 	*ctx = NULL;
834 
835 	buffer_init(&m);
836 	buffer_put_string(&m, goid->elements, goid->length);
837 
838 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
839 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
840 
841 	major = buffer_get_int(&m);
842 
843 	buffer_free(&m);
844 	return (major);
845 }
846 
847 OM_uint32
848 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
849     gss_buffer_desc *out, OM_uint32 *flags)
850 {
851 	Buffer m;
852 	OM_uint32 major;
853 	u_int len;
854 
855 	buffer_init(&m);
856 	buffer_put_string(&m, in->value, in->length);
857 
858 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
859 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
860 
861 	major = buffer_get_int(&m);
862 	out->value = buffer_get_string(&m, &len);
863 	out->length = len;
864 	if (flags)
865 		*flags = buffer_get_int(&m);
866 
867 	buffer_free(&m);
868 
869 	return (major);
870 }
871 
872 OM_uint32
873 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
874 {
875 	Buffer m;
876 	OM_uint32 major;
877 
878 	buffer_init(&m);
879 	buffer_put_string(&m, gssbuf->value, gssbuf->length);
880 	buffer_put_string(&m, gssmic->value, gssmic->length);
881 
882 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
883 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
884 	    &m);
885 
886 	major = buffer_get_int(&m);
887 	buffer_free(&m);
888 	return(major);
889 }
890 
891 int
892 mm_ssh_gssapi_userok(char *user)
893 {
894 	Buffer m;
895 	int authenticated = 0;
896 
897 	buffer_init(&m);
898 
899 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
900 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
901 				  &m);
902 
903 	authenticated = buffer_get_int(&m);
904 
905 	buffer_free(&m);
906 	debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
907 	return (authenticated);
908 }
909 #endif /* GSSAPI */
910 
911 #ifdef KRB5
912 int
913 mm_auth_krb5(void *ctx, void *argp, char **userp, void *resp)
914 {
915 	krb5_data *tkt, *reply;
916 	Buffer m;
917 	int success;
918 
919 	debug3("%s entering", __func__);
920 	tkt = (krb5_data *) argp;
921 	reply = (krb5_data *) resp;
922 
923 	buffer_init(&m);
924 	buffer_put_string(&m, tkt->data, tkt->length);
925 
926 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KRB5, &m);
927 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KRB5, &m);
928 
929 	success = buffer_get_int(&m);
930 	if (success) {
931 		u_int len;
932 
933 		*userp = buffer_get_string(&m, NULL);
934 		reply->data = buffer_get_string(&m, &len);
935 		reply->length = len;
936 	} else {
937 		memset(reply, 0, sizeof(*reply));
938 		*userp = NULL;
939 	}
940 
941 	buffer_free(&m);
942 	return (success);
943 }
944 #endif
945