xref: /netbsd-src/crypto/external/bsd/openssh/dist/monitor_wrap.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /*	$NetBSD: monitor_wrap.c,v 1.25 2019/04/20 17:16:40 christos Exp $	*/
2 /* $OpenBSD: monitor_wrap.c,v 1.112 2019/01/21 09:54:11 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.25 2019/04/20 17:16:40 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 "sshbuf.h"
53 #include "sshkey.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 "monitor.h"
63 #ifdef GSSAPI
64 #include "ssh-gss.h"
65 #endif
66 #include "monitor_wrap.h"
67 #include "atomicio.h"
68 #include "monitor_fdpass.h"
69 #ifdef USE_PAM
70 #include "misc.h"
71 #include "servconf.h"
72 #include <security/pam_appl.h>
73 #endif
74 #include "misc.h"
75 
76 #include "channels.h"
77 #include "session.h"
78 #include "servconf.h"
79 
80 #include "ssherr.h"
81 
82 /* Imports */
83 extern struct monitor *pmonitor;
84 extern struct sshbuf *loginmsg;
85 extern ServerOptions options;
86 
87 void
88 mm_log_handler(LogLevel level, const char *msg, void *ctx)
89 {
90 	struct sshbuf *log_msg;
91 	struct monitor *mon = (struct monitor *)ctx;
92 	int r;
93 	size_t len;
94 
95 	if (mon->m_log_sendfd == -1)
96 		fatal("%s: no log channel", __func__);
97 
98 	if ((log_msg = sshbuf_new()) == NULL)
99 		fatal("%s: sshbuf_new failed", __func__);
100 
101 	if ((r = sshbuf_put_u32(log_msg, 0)) != 0 || /* length; filled below */
102 	    (r = sshbuf_put_u32(log_msg, level)) != 0 ||
103 	    (r = sshbuf_put_cstring(log_msg, msg)) != 0)
104 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
105 	if ((len = sshbuf_len(log_msg)) < 4 || len > 0xffffffff)
106 		fatal("%s: bad length %zu", __func__, len);
107 	POKE_U32(sshbuf_mutable_ptr(log_msg), len - 4);
108 	if (atomicio(vwrite, mon->m_log_sendfd,
109 	    sshbuf_mutable_ptr(log_msg), len) != len)
110 		fatal("%s: write: %s", __func__, strerror(errno));
111 	sshbuf_free(log_msg);
112 }
113 
114 int
115 mm_is_monitor(void)
116 {
117 	/*
118 	 * m_pid is only set in the privileged part, and
119 	 * points to the unprivileged child.
120 	 */
121 	return (pmonitor && pmonitor->m_pid > 0);
122 }
123 
124 void
125 mm_request_send(int sock, enum monitor_reqtype type, struct sshbuf *m)
126 {
127 	size_t mlen = sshbuf_len(m);
128 	u_char buf[5];
129 
130 	debug3("%s entering: type %d", __func__, type);
131 
132 	if (mlen >= 0xffffffff)
133 		fatal("%s: bad length %zu", __func__, mlen);
134 	POKE_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, sshbuf_mutable_ptr(m), mlen) != mlen)
139 		fatal("%s: write: %s", __func__, strerror(errno));
140 }
141 
142 void
143 mm_request_receive(int sock, struct sshbuf *m)
144 {
145 	u_char buf[4], *p = NULL;
146 	u_int msg_len;
147 	int r;
148 
149 	debug3("%s entering", __func__);
150 
151 	if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
152 		if (errno == EPIPE)
153 			cleanup_exit(254);
154 		fatal("%s: read: %s", __func__, strerror(errno));
155 	}
156 	msg_len = PEEK_U32(buf);
157 	if (msg_len > 256 * 1024)
158 		fatal("%s: read: bad msg_len %d", __func__, msg_len);
159 	sshbuf_reset(m);
160 	if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
161 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
162 	if (atomicio(read, sock, p, 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, struct sshbuf *m)
168 {
169 	u_char rtype;
170 	int r;
171 
172 	debug3("%s entering: type %d", __func__, type);
173 
174 	mm_request_receive(sock, m);
175 	if ((r = sshbuf_get_u8(m, &rtype)) != 0)
176 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
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 r;
188 	u_char success = 0;
189 	struct sshbuf *m;
190 
191 	if ((m = sshbuf_new()) == NULL)
192 		fatal("%s: sshbuf_new failed", __func__);
193 	if ((r = sshbuf_put_u32(m, min)) != 0 ||
194 	    (r = sshbuf_put_u32(m, nbits)) != 0 ||
195 	    (r = sshbuf_put_u32(m, max)) != 0)
196 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
197 
198 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, m);
199 
200 	debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
201 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, m);
202 
203 	if ((r = sshbuf_get_u8(m, &success)) != 0)
204 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
205 	if (success == 0)
206 		fatal("%s: MONITOR_ANS_MODULI failed", __func__);
207 
208 	if ((r = sshbuf_get_bignum2(m, &p)) != 0 ||
209 	    (r = sshbuf_get_bignum2(m, &g)) != 0)
210 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
211 
212 	debug3("%s: remaining %zu", __func__, sshbuf_len(m));
213 	sshbuf_free(m);
214 
215 	return (dh_new_group(g, p));
216 }
217 #endif
218 
219 int
220 mm_sshkey_sign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
221     const u_char *data, size_t datalen, const char *hostkey_alg, u_int compat)
222 {
223 	struct kex *kex = *pmonitor->m_pkex;
224 	struct sshbuf *m;
225 	u_int ndx = kex->host_key_index(key, 0, ssh);
226 	int r;
227 
228 	debug3("%s entering", __func__);
229 
230 	if ((m = sshbuf_new()) == NULL)
231 		fatal("%s: sshbuf_new failed", __func__);
232 	if ((r = sshbuf_put_u32(m, ndx)) != 0 ||
233 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
234 	    (r = sshbuf_put_cstring(m, hostkey_alg)) != 0 ||
235 	    (r = sshbuf_put_u32(m, compat)) != 0)
236 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
237 
238 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, m);
239 
240 	debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
241 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, m);
242 	if ((r = sshbuf_get_string(m, sigp, lenp)) != 0)
243 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
244 	sshbuf_free(m);
245 
246 	return (0);
247 }
248 
249 struct passwd *
250 mm_getpwnamallow(struct ssh *ssh, const char *username)
251 {
252 	struct sshbuf *m;
253 	struct passwd *pw;
254 	size_t len;
255 	u_int i;
256 	ServerOptions *newopts;
257 	int r;
258 	u_char ok;
259 	const u_char *p;
260 
261 	debug3("%s entering", __func__);
262 
263 	if ((m = sshbuf_new()) == NULL)
264 		fatal("%s: sshbuf_new failed", __func__);
265 	if ((r = sshbuf_put_cstring(m, username)) != 0)
266 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
267 
268 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, m);
269 
270 	debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
271 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, m);
272 
273 	if ((r = sshbuf_get_u8(m, &ok)) != 0)
274 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
275 	if (ok == 0) {
276 		pw = NULL;
277 		goto out;
278 	}
279 
280 	/* XXX don't like passing struct passwd like this */
281 	pw = xcalloc(sizeof(*pw), 1);
282 	if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
283 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
284 	if (len != sizeof(*pw))
285 		fatal("%s: struct passwd size mismatch", __func__);
286 	memcpy(pw, p, sizeof(*pw));
287 
288 	if ((r = sshbuf_get_cstring(m, &pw->pw_name, NULL)) != 0 ||
289 	    (r = sshbuf_get_cstring(m, &pw->pw_passwd, NULL)) != 0 ||
290 	    (r = sshbuf_get_cstring(m, &pw->pw_gecos, NULL)) != 0 ||
291 	    (r = sshbuf_get_cstring(m, &pw->pw_class, NULL)) != 0 ||
292 	    (r = sshbuf_get_cstring(m, &pw->pw_dir, NULL)) != 0 ||
293 	    (r = sshbuf_get_cstring(m, &pw->pw_shell, NULL)) != 0)
294 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
295 
296 out:
297 	/* copy options block as a Match directive may have changed some */
298 	if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
299 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
300 	if (len != sizeof(*newopts))
301 		fatal("%s: option block size mismatch", __func__);
302 	newopts = xcalloc(sizeof(*newopts), 1);
303 	memcpy(newopts, p, sizeof(*newopts));
304 
305 #define M_CP_STROPT(x) do { \
306 		if (newopts->x != NULL) { \
307 			if ((r = sshbuf_get_cstring(m, \
308 			    &newopts->x, NULL)) != 0) \
309 				fatal("%s: buffer error: %s", \
310 				    __func__, ssh_err(r)); \
311 		} \
312 	} while (0)
313 #define M_CP_STRARRAYOPT(x, nx) do { \
314 		newopts->x = newopts->nx == 0 ? \
315 		    NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
316 		for (i = 0; i < newopts->nx; i++) { \
317 			if ((r = sshbuf_get_cstring(m, \
318 			    &newopts->x[i], NULL)) != 0) \
319 				fatal("%s: buffer error: %s", \
320 				    __func__, ssh_err(r)); \
321 		} \
322 	} while (0)
323 	/* See comment in servconf.h */
324 	COPY_MATCH_STRING_OPTS();
325 #undef M_CP_STROPT
326 #undef M_CP_STRARRAYOPT
327 
328 	copy_set_server_options(&options, newopts, 1);
329 	log_change_level(options.log_level);
330 	process_permitopen(ssh, &options);
331 	free(newopts);
332 
333 	sshbuf_free(m);
334 
335 	return (pw);
336 }
337 
338 char *
339 mm_auth2_read_banner(void)
340 {
341 	struct sshbuf *m;
342 	char *banner;
343 	int r;
344 
345 	debug3("%s entering", __func__);
346 
347 	if ((m = sshbuf_new()) == NULL)
348 		fatal("%s: sshbuf_new failed", __func__);
349 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, m);
350 	sshbuf_reset(m);
351 
352 	mm_request_receive_expect(pmonitor->m_recvfd,
353 	    MONITOR_ANS_AUTH2_READ_BANNER, m);
354 	if ((r = sshbuf_get_cstring(m, &banner, NULL)) != 0)
355 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
356 	sshbuf_free(m);
357 
358 	/* treat empty banner as missing banner */
359 	if (strlen(banner) == 0) {
360 		free(banner);
361 		banner = NULL;
362 	}
363 	return (banner);
364 }
365 
366 /* Inform the privileged process about service and style */
367 
368 void
369 mm_inform_authserv(char *service, char *style)
370 {
371 	struct sshbuf *m;
372 	int r;
373 
374 	debug3("%s entering", __func__);
375 
376 	if ((m = sshbuf_new()) == NULL)
377 		fatal("%s: sshbuf_new failed", __func__);
378 	if ((r = sshbuf_put_cstring(m, service)) != 0 ||
379 	    (r = sshbuf_put_cstring(m, style ? style : "")) != 0)
380 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
381 
382 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, m);
383 
384 	sshbuf_free(m);
385 }
386 
387 /* Do the password authentication */
388 int
389 mm_auth_password(struct ssh *ssh, const char *password)
390 {
391 	struct sshbuf *m;
392 	int r;
393 	u_int authenticated = 0;
394 
395 	debug3("%s entering", __func__);
396 
397 	if ((m = sshbuf_new()) == NULL)
398 		fatal("%s: sshbuf_new failed", __func__);
399 	if ((r = sshbuf_put_cstring(m, password)) != 0)
400 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
401 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, m);
402 
403 	debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
404 	mm_request_receive_expect(pmonitor->m_recvfd,
405 	    MONITOR_ANS_AUTHPASSWORD, m);
406 
407 	if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
408 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
409 
410 	sshbuf_free(m);
411 
412 	debug3("%s: user %sauthenticated",
413 	    __func__, authenticated ? "" : "not ");
414 	return (authenticated);
415 }
416 
417 int
418 mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
419     int pubkey_auth_attempt, struct sshauthopt **authoptp)
420 {
421 	return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
422 	    pubkey_auth_attempt, authoptp));
423 }
424 
425 int
426 mm_hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
427     const char *user, const char *host, struct sshkey *key)
428 {
429 	return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL));
430 }
431 
432 int
433 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
434     struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp)
435 {
436 	struct sshbuf *m;
437 	int r;
438 	u_int allowed = 0;
439 	struct sshauthopt *opts = NULL;
440 
441 	debug3("%s entering", __func__);
442 
443 	if (authoptp != NULL)
444 		*authoptp = NULL;
445 
446 	if ((m = sshbuf_new()) == NULL)
447 		fatal("%s: sshbuf_new failed", __func__);
448 	if ((r = sshbuf_put_u32(m, type)) != 0 ||
449 	    (r = sshbuf_put_cstring(m, user ? user : "")) != 0 ||
450 	    (r = sshbuf_put_cstring(m, host ? host : "")) != 0 ||
451 	    (r = sshkey_puts(key, m)) != 0 ||
452 	    (r = sshbuf_put_u32(m, pubkey_auth_attempt)) != 0)
453 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
454 
455 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, m);
456 
457 	debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
458 	mm_request_receive_expect(pmonitor->m_recvfd,
459 	    MONITOR_ANS_KEYALLOWED, m);
460 
461 	if ((r = sshbuf_get_u32(m, &allowed)) != 0)
462 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
463 	if (allowed && type == MM_USERKEY) {
464 		if ((r = sshauthopt_deserialise(m, &opts)) != 0)
465 			fatal("%s: sshauthopt_deserialise: %s",
466 			    __func__, ssh_err(r));
467 	}
468 	sshbuf_free(m);
469 
470 	if (authoptp != NULL) {
471 		*authoptp = opts;
472 		opts = NULL;
473 	}
474 	sshauthopt_free(opts);
475 
476 	return allowed;
477 }
478 
479 /*
480  * This key verify needs to send the key type along, because the
481  * privileged parent makes the decision if the key is allowed
482  * for authentication.
483  */
484 
485 int
486 mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
487     const u_char *data, size_t datalen, const char *sigalg, u_int compat)
488 {
489 	struct sshbuf *m;
490 	u_int encoded_ret = 0;
491 	int r;
492 
493 	debug3("%s entering", __func__);
494 
495 
496 	if ((m = sshbuf_new()) == NULL)
497 		fatal("%s: sshbuf_new failed", __func__);
498 	if ((r = sshkey_puts(key, m)) != 0 ||
499 	    (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
500 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
501 	    (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
502 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
503 
504 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, m);
505 
506 	debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
507 	mm_request_receive_expect(pmonitor->m_recvfd,
508 	    MONITOR_ANS_KEYVERIFY, m);
509 
510 	if ((r = sshbuf_get_u32(m, &encoded_ret)) != 0)
511 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
512 
513 	sshbuf_free(m);
514 
515 	if (encoded_ret != 0)
516 		return SSH_ERR_SIGNATURE_INVALID;
517 	return 0;
518 }
519 
520 void
521 mm_send_keystate(struct ssh *ssh, struct monitor *monitor)
522 {
523 	struct sshbuf *m;
524 	int r;
525 
526 	if ((m = sshbuf_new()) == NULL)
527 		fatal("%s: sshbuf_new failed", __func__);
528 	if ((r = ssh_packet_get_state(ssh, m)) != 0)
529 		fatal("%s: get_state failed: %s",
530 		    __func__, ssh_err(r));
531 	mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
532 	debug3("%s: Finished sending state", __func__);
533 	sshbuf_free(m);
534 }
535 
536 int
537 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
538 {
539 	struct sshbuf *m;
540 	char *p, *msg;
541 	u_int success = 0;
542 	int tmp1 = -1, tmp2 = -1, r;
543 
544 	/* Kludge: ensure there are fds free to receive the pty/tty */
545 	if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
546 	    (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
547 		error("%s: cannot allocate fds for pty", __func__);
548 		if (tmp1 > 0)
549 			close(tmp1);
550 		if (tmp2 > 0)
551 			close(tmp2);
552 		return 0;
553 	}
554 	close(tmp1);
555 	close(tmp2);
556 
557 	if ((m = sshbuf_new()) == NULL)
558 		fatal("%s: sshbuf_new failed", __func__);
559 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, m);
560 
561 	debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
562 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, m);
563 
564 	if ((r = sshbuf_get_u32(m, &success)) != 0)
565 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
566 	if (success == 0) {
567 		debug3("%s: pty alloc failed", __func__);
568 		sshbuf_free(m);
569 		return (0);
570 	}
571 	if ((r = sshbuf_get_cstring(m, &p, NULL)) != 0 ||
572 	    (r = sshbuf_get_cstring(m, &msg, NULL)) != 0)
573 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
574 	sshbuf_free(m);
575 
576 	strlcpy(namebuf, p, namebuflen); /* Possible truncation */
577 	free(p);
578 
579 	if ((r = sshbuf_put(loginmsg, msg, strlen(msg))) != 0)
580 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
581 	free(msg);
582 
583 	if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
584 	    (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
585 		fatal("%s: receive fds failed", __func__);
586 
587 	/* Success */
588 	return (1);
589 }
590 
591 void
592 mm_session_pty_cleanup2(Session *s)
593 {
594 	struct sshbuf *m;
595 	int r;
596 
597 	if (s->ttyfd == -1)
598 		return;
599 	if ((m = sshbuf_new()) == NULL)
600 		fatal("%s: sshbuf_new failed", __func__);
601 	if ((r = sshbuf_put_cstring(m, s->tty)) != 0)
602 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
603 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, m);
604 	sshbuf_free(m);
605 
606 	/* closed dup'ed master */
607 	if (s->ptymaster != -1 && close(s->ptymaster) < 0)
608 		error("close(s->ptymaster/%d): %s",
609 		    s->ptymaster, strerror(errno));
610 
611 	/* unlink pty from session */
612 	s->ttyfd = -1;
613 }
614 
615 #ifdef USE_PAM
616 void
617 mm_start_pam(struct ssh *ssh)
618 {
619 	struct sshbuf *m;
620 
621 	debug3("%s entering", __func__);
622 	if (!options.use_pam)
623 		fatal("UsePAM=no, but ended up in %s anyway", __func__);
624 
625 	if ((m = sshbuf_new()) == NULL)
626 		fatal("%s: sshbuf_new failed", __func__);
627 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, m);
628 
629 	sshbuf_free(m);
630 }
631 
632 u_int
633 mm_do_pam_account(void)
634 {
635 	struct sshbuf *m;
636 	int r;
637 	u_int ret;
638 	size_t len;
639 	char *msg;
640 
641 	debug3("%s entering", __func__);
642 	if (!options.use_pam)
643 		fatal("UsePAM=no, but ended up in %s anyway", __func__);
644 
645 	if ((m = sshbuf_new()) == NULL)
646 		fatal("%s: sshbuf_new failed", __func__);
647 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, m);
648 
649 	mm_request_receive_expect(pmonitor->m_recvfd,
650 	    MONITOR_ANS_PAM_ACCOUNT, m);
651 	if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
652 	    (r = sshbuf_get_cstring(m, &msg, &len)) != 0)
653 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
654 	sshbuf_put_cstring(loginmsg, msg);
655 	free(msg);
656 
657 	sshbuf_free(m);
658 
659 	debug3("%s returning %d", __func__, ret);
660 
661 	return (ret);
662 }
663 
664 void *
665 mm_sshpam_init_ctx(Authctxt *authctxt)
666 {
667 	struct sshbuf *m;
668 	u_int success;
669 	int r;
670 
671 	debug3("%s", __func__);
672 	if ((m = sshbuf_new()) == NULL)
673 		fatal("%s: sshbuf_new failed", __func__);
674 	sshbuf_put_cstring(m, authctxt->user);
675 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, m);
676 	debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
677 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, m);
678 	if ((r = sshbuf_get_u32(m, &success)) != 0)
679 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
680 	if (success == 0) {
681 		debug3("%s: pam_init_ctx failed", __func__);
682 		sshbuf_free(m);
683 		return (NULL);
684 	}
685 	sshbuf_free(m);
686 	return (authctxt);
687 }
688 
689 int
690 mm_sshpam_query(void *ctx, char **name, char **info,
691     u_int *num, char ***prompts, u_int **echo_on)
692 {
693 	struct sshbuf *m;
694 	u_int i, ret;
695 	int r;
696 
697 	debug3("%s", __func__);
698 	if ((m = sshbuf_new()) == NULL)
699 		fatal("%s: sshbuf_new failed", __func__);
700 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, m);
701 	debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
702 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, m);
703 	if ((r = sshbuf_get_u32(m, &ret)) != 0)
704 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
705 	debug3("%s: pam_query returned %d", __func__, ret);
706 	if ((r = sshbuf_get_cstring(m, name, NULL)) != 0 ||
707 	    (r = sshbuf_get_cstring(m, info, NULL)) != 0 ||
708 	    (r = sshbuf_get_u32(m, num)) != 0)
709 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
710 	if (*num > PAM_MAX_NUM_MSG)
711 		fatal("%s: received %u PAM messages, expected <= %u",
712 		    __func__, *num, PAM_MAX_NUM_MSG);
713 	*prompts = xcalloc((*num + 1), sizeof(char *));
714 	*echo_on = xcalloc((*num + 1), sizeof(u_int));
715 	for (i = 0; i < *num; ++i) {
716 		if ((r = sshbuf_get_cstring(m, &(*prompts)[i], NULL)) != 0 ||
717 		    (r = sshbuf_get_u32(m, (echo_on)[i])) != 0)
718 			fatal("%s: buffer error: %s", __func__, ssh_err(r));
719 	}
720 	sshbuf_free(m);
721 	return (ret);
722 }
723 
724 int
725 mm_sshpam_respond(void *ctx, u_int num, char **resp)
726 {
727 	struct sshbuf *m;
728 	int r;
729 	u_int i;
730 	u_int ret;
731 
732 	debug3("%s", __func__);
733 	if ((m = sshbuf_new()) == NULL)
734 		fatal("%s: sshbuf_new failed", __func__);
735 	sshbuf_put_u32(m, num);
736 	for (i = 0; i < num; ++i)
737 		sshbuf_put_cstring(m, resp[i]);
738 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, m);
739 	debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
740 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, m);
741 	if ((r = sshbuf_get_u32(m, &ret)) != 0)
742 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
743 	debug3("%s: pam_respond returned %d", __func__, ret);
744 	sshbuf_free(m);
745 	return (ret);
746 }
747 
748 void
749 mm_sshpam_free_ctx(void *ctxtp)
750 {
751 	struct sshbuf *m;
752 
753 	debug3("%s", __func__);
754 	if ((m = sshbuf_new()) == NULL)
755 		fatal("%s: sshbuf_new failed", __func__);
756 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, m);
757 	debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
758 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, m);
759 	sshbuf_free(m);
760 }
761 #endif /* USE_PAM */
762 
763 /* Request process termination */
764 
765 void
766 mm_terminate(void)
767 {
768 	struct sshbuf *m;
769 
770 	if ((m = sshbuf_new()) == NULL)
771 		fatal("%s: sshbuf_new failed", __func__);
772 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, m);
773 	sshbuf_free(m);
774 }
775 
776 #if defined(BSD_AUTH) || defined(SKEY)
777 static void
778 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
779     char ***prompts, u_int **echo_on)
780 {
781 	*name = xstrdup("");
782 	*infotxt = xstrdup("");
783 	*numprompts = 1;
784 	*prompts = xcalloc(*numprompts, sizeof(char *));
785 	*echo_on = xcalloc(*numprompts, sizeof(u_int));
786 	(*echo_on)[0] = 0;
787 }
788 
789 #ifdef BSD_AUTH
790 int
791 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
792    u_int *numprompts, char ***prompts, u_int **echo_on)
793 {
794 	struct sshbuf *m;
795 	u_int success;
796 	char *challenge;
797 	int r;
798 
799 	debug3("%s: entering", __func__);
800 
801 	if ((m = sshbuf_new()) == NULL)
802 		fatal("%s: sshbuf_new failed", __func__);
803 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, m);
804 
805 	mm_request_receive_expect(pmonitor->m_recvfd,
806 	    MONITOR_ANS_BSDAUTHQUERY, m);
807 	if ((r = sshbuf_get_u32(m, &success)) != 0)
808 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
809 	if (success == 0) {
810 		debug3("%s: no challenge", __func__);
811 		sshbuf_free(m);
812 		return (-1);
813 	}
814 
815 	/* Get the challenge, and format the response */
816 	if ((r = sshbuf_get_cstring(m, &challenge, NULL)) != 0)
817 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
818 	sshbuf_free(m);
819 
820 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
821 	(*prompts)[0] = challenge;
822 
823 	debug3("%s: received challenge: %s", __func__, challenge);
824 
825 	return (0);
826 }
827 
828 int
829 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
830 {
831 	struct sshbuf *m;
832 	int r, authok;
833 
834 	debug3("%s: entering", __func__);
835 	if (numresponses != 1)
836 		return (-1);
837 
838 	if ((m = sshbuf_new()) == NULL)
839 		fatal("%s: sshbuf_new failed", __func__);
840 	if ((r = sshbuf_put_cstring(m, responses[0])) != 0)
841 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
842 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, m);
843 
844 	mm_request_receive_expect(pmonitor->m_recvfd,
845 	    MONITOR_ANS_BSDAUTHRESPOND, m);
846 
847 	if ((r = sshbuf_get_u32(m, &authok)) != 0)
848 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
849 	sshbuf_free(m);
850 
851 	return ((authok == 0) ? -1 : 0);
852 }
853 #endif
854 
855 #ifdef SKEY
856 int
857 mm_skey_query(void *ctx, char **name, char **infotxt,
858    u_int *numprompts, char ***prompts, u_int **echo_on)
859 {
860 	struct sshbuf m;
861 	u_int success;
862 	char *challenge;
863 
864 	debug3("%s: entering", __func__);
865 
866 	sshbuf_new(&m);
867 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
868 
869 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
870 	    &m);
871 	success = sshbuf_get_int(&m);
872 	if (success == 0) {
873 		debug3("%s: no challenge", __func__);
874 		sshbuf_free(&m);
875 		return (-1);
876 	}
877 
878 	/* Get the challenge, and format the response */
879 	challenge  = sshbuf_get_string(&m, NULL);
880 	sshbuf_free(&m);
881 
882 	debug3("%s: received challenge: %s", __func__, challenge);
883 
884 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
885 
886 	xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
887 	free(challenge);
888 
889 	return (0);
890 }
891 
892 int
893 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
894 {
895 	struct sshbuf m;
896 	int authok;
897 
898 	debug3("%s: entering", __func__);
899 	if (numresponses != 1)
900 		return (-1);
901 
902 	sshbuf_new(&m);
903 	sshbuf_put_cstring(&m, responses[0]);
904 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
905 
906 	mm_request_receive_expect(pmonitor->m_recvfd,
907 	    MONITOR_ANS_SKEYRESPOND, &m);
908 
909 	authok = sshbuf_get_int(&m);
910 	sshbuf_free(&m);
911 
912 	return ((authok == 0) ? -1 : 0);
913 }
914 #endif /* SKEY */
915 #endif /* BSDAUTH || SKEY */
916 
917 #ifdef GSSAPI
918 OM_uint32
919 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
920 {
921 	struct sshbuf *m;
922 	OM_uint32 major;
923 	int r;
924 
925 	/* Client doesn't get to see the context */
926 	*ctx = NULL;
927 
928 	if ((m = sshbuf_new()) == NULL)
929 		fatal("%s: sshbuf_new failed", __func__);
930 	if ((r = sshbuf_put_string(m, goid->elements, goid->length)) != 0)
931 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
932 
933 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, m);
934 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, m);
935 
936 	if ((r = sshbuf_get_u32(m, &major)) != 0)
937 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
938 
939 	sshbuf_free(m);
940 	return (major);
941 }
942 
943 OM_uint32
944 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
945     gss_buffer_desc *out, OM_uint32 *flagsp)
946 {
947 	struct sshbuf *m;
948 	OM_uint32 major;
949 	u_int flags;
950 	int r;
951 
952 	if ((m = sshbuf_new()) == NULL)
953 		fatal("%s: sshbuf_new failed", __func__);
954 	if ((r = sshbuf_put_string(m, in->value, in->length)) != 0)
955 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
956 
957 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, m);
958 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, m);
959 
960 	if ((r = sshbuf_get_u32(m, &major)) != 0 ||
961 	    (r = ssh_gssapi_get_buffer_desc(m, out)) != 0)
962 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
963 	if (flagsp != NULL) {
964 		if ((r = sshbuf_get_u32(m, &flags)) != 0)
965 			fatal("%s: buffer error: %s", __func__, ssh_err(r));
966 		*flagsp = flags;
967 	}
968 
969 	sshbuf_free(m);
970 
971 	return (major);
972 }
973 
974 OM_uint32
975 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
976 {
977 	struct sshbuf *m;
978 	OM_uint32 major;
979 	int r;
980 
981 	if ((m = sshbuf_new()) == NULL)
982 		fatal("%s: sshbuf_new failed", __func__);
983 	if ((r = sshbuf_put_string(m, gssbuf->value, gssbuf->length)) != 0 ||
984 	    (r = sshbuf_put_string(m, gssmic->value, gssmic->length)) != 0)
985 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
986 
987 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, m);
988 	mm_request_receive_expect(pmonitor->m_recvfd,
989 	    MONITOR_ANS_GSSCHECKMIC, m);
990 
991 	if ((r = sshbuf_get_u32(m, &major)) != 0)
992 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
993 	sshbuf_free(m);
994 	return(major);
995 }
996 
997 int
998 mm_ssh_gssapi_userok(char *user)
999 {
1000 	struct sshbuf *m;
1001 	int r;
1002 	u_int authenticated = 0;
1003 
1004 	if ((m = sshbuf_new()) == NULL)
1005 		fatal("%s: sshbuf_new failed", __func__);
1006 
1007 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m);
1008 	mm_request_receive_expect(pmonitor->m_recvfd,
1009 	    MONITOR_ANS_GSSUSEROK, m);
1010 
1011 	if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
1012 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
1013 
1014 	sshbuf_free(m);
1015 	debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1016 	return (authenticated);
1017 }
1018 #endif /* GSSAPI */
1019 
1020 #ifdef KRB5
1021 int
1022 mm_auth_krb5(void *ctx, void *argp, char **userp, void *resp)
1023 {
1024 	krb5_data *tkt, *reply;
1025 	struct sshbuf *m;
1026 	u_int success;
1027 	int r;
1028 
1029 	debug3("%s entering", __func__);
1030 	tkt = (krb5_data *) argp;
1031 	reply = (krb5_data *) resp;
1032 
1033 	if ((m = sshbuf_new()) == NULL)
1034 		fatal("%s: sshbuf_new failed", __func__);
1035 	sshbuf_put_string(m, tkt->data, tkt->length);
1036 
1037 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KRB5, m);
1038 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KRB5, m);
1039 
1040 	if ((r = sshbuf_get_u32(m, &success)) != 0)
1041 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
1042 	if (success) {
1043 		size_t len;
1044 		u_char *data;
1045 
1046 		if ((r = sshbuf_get_cstring(m, userp, NULL)) != 0 ||
1047 		    (r = sshbuf_get_string(m, &data, &len)) != 0)
1048 			fatal("%s: buffer error: %s", __func__, ssh_err(r));
1049 		reply->data = data;
1050 		reply->length = len;
1051 	} else {
1052 		memset(reply, 0, sizeof(*reply));
1053 		*userp = NULL;
1054 	}
1055 
1056 	sshbuf_free(m);
1057 	return (success);
1058 }
1059 #endif
1060