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