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