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