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