xref: /openbsd-src/usr.bin/ssh/monitor_wrap.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /* $OpenBSD: monitor_wrap.c,v 1.107 2018/07/20 03:46:34 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 <sys/types.h>
29 #include <sys/uio.h>
30 #include <sys/queue.h>
31 
32 #include <errno.h>
33 #include <pwd.h>
34 #include <signal.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38 
39 #ifdef WITH_OPENSSL
40 #include <openssl/bn.h>
41 #include <openssl/dh.h>
42 #endif
43 
44 #include "xmalloc.h"
45 #include "ssh.h"
46 #ifdef WITH_OPENSSL
47 #include "dh.h"
48 #endif
49 #include "sshbuf.h"
50 #include "sshkey.h"
51 #include "cipher.h"
52 #include "kex.h"
53 #include "hostfile.h"
54 #include "auth.h"
55 #include "auth-options.h"
56 #include "packet.h"
57 #include "mac.h"
58 #include "log.h"
59 #include "monitor.h"
60 #ifdef GSSAPI
61 #include "ssh-gss.h"
62 #endif
63 #include "monitor_wrap.h"
64 #include "atomicio.h"
65 #include "monitor_fdpass.h"
66 #include "misc.h"
67 
68 #include "channels.h"
69 #include "session.h"
70 #include "servconf.h"
71 
72 #include "ssherr.h"
73 
74 /* Imports */
75 extern struct monitor *pmonitor;
76 extern struct sshbuf *loginmsg;
77 extern ServerOptions options;
78 
79 void
80 mm_log_handler(LogLevel level, const char *msg, void *ctx)
81 {
82 	struct sshbuf *log_msg;
83 	struct monitor *mon = (struct monitor *)ctx;
84 	int r;
85 	size_t len;
86 
87 	if (mon->m_log_sendfd == -1)
88 		fatal("%s: no log channel", __func__);
89 
90 	if ((log_msg = sshbuf_new()) == NULL)
91 		fatal("%s: sshbuf_new failed", __func__);
92 
93 	if ((r = sshbuf_put_u32(log_msg, 0)) != 0 || /* length; filled below */
94 	    (r = sshbuf_put_u32(log_msg, level)) != 0 ||
95 	    (r = sshbuf_put_cstring(log_msg, msg)) != 0)
96 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
97 	if ((len = sshbuf_len(log_msg)) < 4 || len > 0xffffffff)
98 		fatal("%s: bad length %zu", __func__, len);
99 	POKE_U32(sshbuf_mutable_ptr(log_msg), len - 4);
100 	if (atomicio(vwrite, mon->m_log_sendfd,
101 	    sshbuf_mutable_ptr(log_msg), len) != len)
102 		fatal("%s: write: %s", __func__, strerror(errno));
103 	sshbuf_free(log_msg);
104 }
105 
106 int
107 mm_is_monitor(void)
108 {
109 	/*
110 	 * m_pid is only set in the privileged part, and
111 	 * points to the unprivileged child.
112 	 */
113 	return (pmonitor && pmonitor->m_pid > 0);
114 }
115 
116 void
117 mm_request_send(int sock, enum monitor_reqtype type, struct sshbuf *m)
118 {
119 	size_t mlen = sshbuf_len(m);
120 	u_char buf[5];
121 
122 	debug3("%s entering: type %d", __func__, type);
123 
124 	if (mlen >= 0xffffffff)
125 		fatal("%s: bad length %zu", __func__, mlen);
126 	POKE_U32(buf, mlen + 1);
127 	buf[4] = (u_char) type;		/* 1st byte of payload is mesg-type */
128 	if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
129 		fatal("%s: write: %s", __func__, strerror(errno));
130 	if (atomicio(vwrite, sock, sshbuf_mutable_ptr(m), mlen) != mlen)
131 		fatal("%s: write: %s", __func__, strerror(errno));
132 }
133 
134 void
135 mm_request_receive(int sock, struct sshbuf *m)
136 {
137 	u_char buf[4], *p = NULL;
138 	u_int msg_len;
139 	int r;
140 
141 	debug3("%s entering", __func__);
142 
143 	if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
144 		if (errno == EPIPE)
145 			cleanup_exit(255);
146 		fatal("%s: read: %s", __func__, strerror(errno));
147 	}
148 	msg_len = PEEK_U32(buf);
149 	if (msg_len > 256 * 1024)
150 		fatal("%s: read: bad msg_len %d", __func__, msg_len);
151 	sshbuf_reset(m);
152 	if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
153 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
154 	if (atomicio(read, sock, p, msg_len) != msg_len)
155 		fatal("%s: read: %s", __func__, strerror(errno));
156 }
157 
158 void
159 mm_request_receive_expect(int sock, enum monitor_reqtype type, struct sshbuf *m)
160 {
161 	u_char rtype;
162 	int r;
163 
164 	debug3("%s entering: type %d", __func__, type);
165 
166 	mm_request_receive(sock, m);
167 	if ((r = sshbuf_get_u8(m, &rtype)) != 0)
168 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
169 	if (rtype != type)
170 		fatal("%s: read: rtype %d != type %d", __func__,
171 		    rtype, type);
172 }
173 
174 #ifdef WITH_OPENSSL
175 DH *
176 mm_choose_dh(int min, int nbits, int max)
177 {
178 	BIGNUM *p, *g;
179 	int r;
180 	u_char success = 0;
181 	struct sshbuf *m;
182 
183 	if ((m = sshbuf_new()) == NULL)
184 		fatal("%s: sshbuf_new failed", __func__);
185 	if ((r = sshbuf_put_u32(m, min)) != 0 ||
186 	    (r = sshbuf_put_u32(m, nbits)) != 0 ||
187 	    (r = sshbuf_put_u32(m, max)) != 0)
188 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
189 
190 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, m);
191 
192 	debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
193 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, m);
194 
195 	if ((r = sshbuf_get_u8(m, &success)) != 0)
196 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
197 	if (success == 0)
198 		fatal("%s: MONITOR_ANS_MODULI failed", __func__);
199 
200 	if ((p = BN_new()) == NULL)
201 		fatal("%s: BN_new failed", __func__);
202 	if ((g = BN_new()) == NULL)
203 		fatal("%s: BN_new failed", __func__);
204 	if ((r = sshbuf_get_bignum2(m, p)) != 0 ||
205 	    (r = sshbuf_get_bignum2(m, g)) != 0)
206 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
207 
208 	debug3("%s: remaining %zu", __func__, sshbuf_len(m));
209 	sshbuf_free(m);
210 
211 	return (dh_new_group(g, p));
212 }
213 #endif
214 
215 int
216 mm_sshkey_sign(struct sshkey *key, u_char **sigp, size_t *lenp,
217     const u_char *data, size_t datalen, const char *hostkey_alg, u_int compat)
218 {
219 	struct kex *kex = *pmonitor->m_pkex;
220 	struct sshbuf *m;
221 	u_int ndx = kex->host_key_index(key, 0, active_state);
222 	int r;
223 
224 	debug3("%s entering", __func__);
225 
226 	if ((m = sshbuf_new()) == NULL)
227 		fatal("%s: sshbuf_new failed", __func__);
228 	if ((r = sshbuf_put_u32(m, ndx)) != 0 ||
229 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
230 	    (r = sshbuf_put_cstring(m, hostkey_alg)) != 0 ||
231 	    (r = sshbuf_put_u32(m, compat)) != 0)
232 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
233 
234 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, m);
235 
236 	debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
237 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, m);
238 	if ((r = sshbuf_get_string(m, sigp, lenp)) != 0)
239 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
240 	sshbuf_free(m);
241 
242 	return (0);
243 }
244 
245 struct passwd *
246 mm_getpwnamallow(const char *username)
247 {
248 	struct ssh *ssh = active_state;		/* XXX */
249 	struct sshbuf *m;
250 	struct passwd *pw;
251 	size_t len;
252 	u_int i;
253 	ServerOptions *newopts;
254 	int r;
255 	u_char ok;
256 	const u_char *p;
257 
258 	debug3("%s entering", __func__);
259 
260 	if ((m = sshbuf_new()) == NULL)
261 		fatal("%s: sshbuf_new failed", __func__);
262 	if ((r = sshbuf_put_cstring(m, username)) != 0)
263 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
264 
265 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, m);
266 
267 	debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
268 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, m);
269 
270 	if ((r = sshbuf_get_u8(m, &ok)) != 0)
271 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
272 	if (ok == 0) {
273 		pw = NULL;
274 		goto out;
275 	}
276 
277 	/* XXX don't like passing struct passwd like this */
278 	pw = xcalloc(sizeof(*pw), 1);
279 	if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
280 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
281 	if (len != sizeof(*pw))
282 		fatal("%s: struct passwd size mismatch", __func__);
283 	memcpy(pw, p, sizeof(*pw));
284 
285 	if ((r = sshbuf_get_cstring(m, &pw->pw_name, NULL)) != 0 ||
286 	    (r = sshbuf_get_cstring(m, &pw->pw_passwd, NULL)) != 0 ||
287 	    (r = sshbuf_get_cstring(m, &pw->pw_gecos, NULL)) != 0 ||
288 	    (r = sshbuf_get_cstring(m, &pw->pw_class, NULL)) != 0 ||
289 	    (r = sshbuf_get_cstring(m, &pw->pw_dir, NULL)) != 0 ||
290 	    (r = sshbuf_get_cstring(m, &pw->pw_shell, NULL)) != 0)
291 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
292 
293 out:
294 	/* copy options block as a Match directive may have changed some */
295 	if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
296 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
297 	if (len != sizeof(*newopts))
298 		fatal("%s: option block size mismatch", __func__);
299 	newopts = xcalloc(sizeof(*newopts), 1);
300 	memcpy(newopts, p, sizeof(*newopts));
301 
302 #define M_CP_STROPT(x) do { \
303 		if (newopts->x != NULL) { \
304 			if ((r = sshbuf_get_cstring(m, \
305 			    &newopts->x, NULL)) != 0) \
306 				fatal("%s: buffer error: %s", \
307 				    __func__, ssh_err(r)); \
308 		} \
309 	} while (0)
310 #define M_CP_STRARRAYOPT(x, nx) do { \
311 		newopts->x = newopts->nx == 0 ? \
312 		    NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
313 		for (i = 0; i < newopts->nx; i++) { \
314 			if ((r = sshbuf_get_cstring(m, \
315 			    &newopts->x[i], NULL)) != 0) \
316 				fatal("%s: buffer error: %s", \
317 				    __func__, ssh_err(r)); \
318 		} \
319 	} while (0)
320 	/* See comment in servconf.h */
321 	COPY_MATCH_STRING_OPTS();
322 #undef M_CP_STROPT
323 #undef M_CP_STRARRAYOPT
324 
325 	copy_set_server_options(&options, newopts, 1);
326 	log_change_level(options.log_level);
327 	process_permitopen(ssh, &options);
328 	free(newopts);
329 
330 	sshbuf_free(m);
331 
332 	return (pw);
333 }
334 
335 char *
336 mm_auth2_read_banner(void)
337 {
338 	struct sshbuf *m;
339 	char *banner;
340 	int r;
341 
342 	debug3("%s entering", __func__);
343 
344 	if ((m = sshbuf_new()) == NULL)
345 		fatal("%s: sshbuf_new failed", __func__);
346 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, m);
347 	sshbuf_reset(m);
348 
349 	mm_request_receive_expect(pmonitor->m_recvfd,
350 	    MONITOR_ANS_AUTH2_READ_BANNER, m);
351 	if ((r = sshbuf_get_cstring(m, &banner, NULL)) != 0)
352 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
353 	sshbuf_free(m);
354 
355 	/* treat empty banner as missing banner */
356 	if (strlen(banner) == 0) {
357 		free(banner);
358 		banner = NULL;
359 	}
360 	return (banner);
361 }
362 
363 /* Inform the privileged process about service and style */
364 
365 void
366 mm_inform_authserv(char *service, char *style)
367 {
368 	struct sshbuf *m;
369 	int r;
370 
371 	debug3("%s entering", __func__);
372 
373 	if ((m = sshbuf_new()) == NULL)
374 		fatal("%s: sshbuf_new failed", __func__);
375 	if ((r = sshbuf_put_cstring(m, service)) != 0 ||
376 	    (r = sshbuf_put_cstring(m, style ? style : "")) != 0)
377 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
378 
379 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, m);
380 
381 	sshbuf_free(m);
382 }
383 
384 /* Do the password authentication */
385 int
386 mm_auth_password(struct ssh *ssh, char *password)
387 {
388 	struct sshbuf *m;
389 	int r, authenticated = 0;
390 
391 	debug3("%s entering", __func__);
392 
393 	if ((m = sshbuf_new()) == NULL)
394 		fatal("%s: sshbuf_new failed", __func__);
395 	if ((r = sshbuf_put_cstring(m, password)) != 0)
396 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
397 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, m);
398 
399 	debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
400 	mm_request_receive_expect(pmonitor->m_recvfd,
401 	    MONITOR_ANS_AUTHPASSWORD, m);
402 
403 	if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
404 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
405 
406 	sshbuf_free(m);
407 
408 	debug3("%s: user %sauthenticated",
409 	    __func__, authenticated ? "" : "not ");
410 	return (authenticated);
411 }
412 
413 int
414 mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
415     int pubkey_auth_attempt, struct sshauthopt **authoptp)
416 {
417 	return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
418 	    pubkey_auth_attempt, authoptp));
419 }
420 
421 int
422 mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host,
423     struct sshkey *key)
424 {
425 	return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL));
426 }
427 
428 int
429 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
430     struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp)
431 {
432 	struct sshbuf *m;
433 	int r, allowed = 0;
434 	struct sshauthopt *opts = NULL;
435 
436 	debug3("%s entering", __func__);
437 
438 	if (authoptp != NULL)
439 		*authoptp = NULL;
440 
441 	if ((m = sshbuf_new()) == NULL)
442 		fatal("%s: sshbuf_new failed", __func__);
443 	if ((r = sshbuf_put_u32(m, type)) != 0 ||
444 	    (r = sshbuf_put_cstring(m, user ? user : "")) != 0 ||
445 	    (r = sshbuf_put_cstring(m, host ? host : "")) != 0 ||
446 	    (r = sshkey_puts(key, m)) != 0 ||
447 	    (r = sshbuf_put_u32(m, pubkey_auth_attempt)) != 0)
448 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
449 
450 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, m);
451 
452 	debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
453 	mm_request_receive_expect(pmonitor->m_recvfd,
454 	    MONITOR_ANS_KEYALLOWED, m);
455 
456 	if ((r = sshbuf_get_u32(m, &allowed)) != 0)
457 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
458 	if (allowed && type == MM_USERKEY) {
459 		if ((r = sshauthopt_deserialise(m, &opts)) != 0)
460 			fatal("%s: sshauthopt_deserialise: %s",
461 			    __func__, ssh_err(r));
462 	}
463 	sshbuf_free(m);
464 
465 	if (authoptp != NULL) {
466 		*authoptp = opts;
467 		opts = NULL;
468 	}
469 	sshauthopt_free(opts);
470 
471 	return allowed;
472 }
473 
474 /*
475  * This key verify needs to send the key type along, because the
476  * privileged parent makes the decision if the key is allowed
477  * for authentication.
478  */
479 
480 int
481 mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
482     const u_char *data, size_t datalen, const char *sigalg, u_int compat)
483 {
484 	struct sshbuf *m;
485 	u_int encoded_ret = 0;
486 	int r;
487 
488 	debug3("%s entering", __func__);
489 
490 
491 	if ((m = sshbuf_new()) == NULL)
492 		fatal("%s: sshbuf_new failed", __func__);
493 	if ((r = sshkey_puts(key, m)) != 0 ||
494 	    (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
495 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
496 	    (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
497 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
498 
499 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, m);
500 
501 	debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
502 	mm_request_receive_expect(pmonitor->m_recvfd,
503 	    MONITOR_ANS_KEYVERIFY, m);
504 
505 	if ((r = sshbuf_get_u32(m, &encoded_ret)) != 0)
506 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
507 
508 	sshbuf_free(m);
509 
510 	if (encoded_ret != 0)
511 		return SSH_ERR_SIGNATURE_INVALID;
512 	return 0;
513 }
514 
515 void
516 mm_send_keystate(struct monitor *monitor)
517 {
518 	struct ssh *ssh = active_state;		/* XXX */
519 	struct sshbuf *m;
520 	int r;
521 
522 	if ((m = sshbuf_new()) == NULL)
523 		fatal("%s: sshbuf_new failed", __func__);
524 	if ((r = ssh_packet_get_state(ssh, m)) != 0)
525 		fatal("%s: get_state failed: %s",
526 		    __func__, ssh_err(r));
527 	mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
528 	debug3("%s: Finished sending state", __func__);
529 	sshbuf_free(m);
530 }
531 
532 int
533 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
534 {
535 	struct sshbuf *m;
536 	char *p, *msg;
537 	int success = 0, tmp1 = -1, tmp2 = -1, r;
538 
539 	/* Kludge: ensure there are fds free to receive the pty/tty */
540 	if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
541 	    (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
542 		error("%s: cannot allocate fds for pty", __func__);
543 		if (tmp1 > 0)
544 			close(tmp1);
545 		if (tmp2 > 0)
546 			close(tmp2);
547 		return 0;
548 	}
549 	close(tmp1);
550 	close(tmp2);
551 
552 	if ((m = sshbuf_new()) == NULL)
553 		fatal("%s: sshbuf_new failed", __func__);
554 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, m);
555 
556 	debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
557 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, m);
558 
559 	if ((r = sshbuf_get_u32(m, &success)) != 0)
560 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
561 	if (success == 0) {
562 		debug3("%s: pty alloc failed", __func__);
563 		sshbuf_free(m);
564 		return (0);
565 	}
566 	if ((r = sshbuf_get_cstring(m, &p, NULL)) != 0 ||
567 	    (r = sshbuf_get_cstring(m, &msg, NULL)) != 0)
568 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
569 	sshbuf_free(m);
570 
571 	strlcpy(namebuf, p, namebuflen); /* Possible truncation */
572 	free(p);
573 
574 	if ((r = sshbuf_put(loginmsg, msg, strlen(msg))) != 0)
575 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
576 	free(msg);
577 
578 	if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
579 	    (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
580 		fatal("%s: receive fds failed", __func__);
581 
582 	/* Success */
583 	return (1);
584 }
585 
586 void
587 mm_session_pty_cleanup2(Session *s)
588 {
589 	struct sshbuf *m;
590 	int r;
591 
592 	if (s->ttyfd == -1)
593 		return;
594 	if ((m = sshbuf_new()) == NULL)
595 		fatal("%s: sshbuf_new failed", __func__);
596 	if ((r = sshbuf_put_cstring(m, s->tty)) != 0)
597 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
598 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, m);
599 	sshbuf_free(m);
600 
601 	/* closed dup'ed master */
602 	if (s->ptymaster != -1 && close(s->ptymaster) < 0)
603 		error("close(s->ptymaster/%d): %s",
604 		    s->ptymaster, strerror(errno));
605 
606 	/* unlink pty from session */
607 	s->ttyfd = -1;
608 }
609 
610 /* Request process termination */
611 
612 void
613 mm_terminate(void)
614 {
615 	struct sshbuf *m;
616 
617 	if ((m = sshbuf_new()) == NULL)
618 		fatal("%s: sshbuf_new failed", __func__);
619 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, m);
620 	sshbuf_free(m);
621 }
622 
623 static void
624 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
625     char ***prompts, u_int **echo_on)
626 {
627 	*name = xstrdup("");
628 	*infotxt = xstrdup("");
629 	*numprompts = 1;
630 	*prompts = xcalloc(*numprompts, sizeof(char *));
631 	*echo_on = xcalloc(*numprompts, sizeof(u_int));
632 	(*echo_on)[0] = 0;
633 }
634 
635 int
636 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
637    u_int *numprompts, char ***prompts, u_int **echo_on)
638 {
639 	struct sshbuf *m;
640 	u_int success;
641 	char *challenge;
642 	int r;
643 
644 	debug3("%s: entering", __func__);
645 
646 	if ((m = sshbuf_new()) == NULL)
647 		fatal("%s: sshbuf_new failed", __func__);
648 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, m);
649 
650 	mm_request_receive_expect(pmonitor->m_recvfd,
651 	    MONITOR_ANS_BSDAUTHQUERY, m);
652 	if ((r = sshbuf_get_u32(m, &success)) != 0)
653 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
654 	if (success == 0) {
655 		debug3("%s: no challenge", __func__);
656 		sshbuf_free(m);
657 		return (-1);
658 	}
659 
660 	/* Get the challenge, and format the response */
661 	if ((r = sshbuf_get_cstring(m, &challenge, NULL)) != 0)
662 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
663 	sshbuf_free(m);
664 
665 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
666 	(*prompts)[0] = challenge;
667 
668 	debug3("%s: received challenge: %s", __func__, challenge);
669 
670 	return (0);
671 }
672 
673 int
674 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
675 {
676 	struct sshbuf *m;
677 	int r, authok;
678 
679 	debug3("%s: entering", __func__);
680 	if (numresponses != 1)
681 		return (-1);
682 
683 	if ((m = sshbuf_new()) == NULL)
684 		fatal("%s: sshbuf_new failed", __func__);
685 	if ((r = sshbuf_put_cstring(m, responses[0])) != 0)
686 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
687 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, m);
688 
689 	mm_request_receive_expect(pmonitor->m_recvfd,
690 	    MONITOR_ANS_BSDAUTHRESPOND, m);
691 
692 	if ((r = sshbuf_get_u32(m, &authok)) != 0)
693 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
694 	sshbuf_free(m);
695 
696 	return ((authok == 0) ? -1 : 0);
697 }
698 
699 #ifdef GSSAPI
700 OM_uint32
701 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
702 {
703 	struct sshbuf *m;
704 	OM_uint32 major;
705 	int r;
706 
707 	/* Client doesn't get to see the context */
708 	*ctx = NULL;
709 
710 	if ((m = sshbuf_new()) == NULL)
711 		fatal("%s: sshbuf_new failed", __func__);
712 	if ((r = sshbuf_put_string(m, goid->elements, goid->length)) != 0)
713 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
714 
715 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, m);
716 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, m);
717 
718 	if ((r = sshbuf_get_u32(m, &major)) != 0)
719 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
720 
721 	sshbuf_free(m);
722 	return (major);
723 }
724 
725 OM_uint32
726 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
727     gss_buffer_desc *out, OM_uint32 *flagsp)
728 {
729 	struct sshbuf *m;
730 	OM_uint32 major;
731 	u_int flags;
732 	int r;
733 
734 	if ((m = sshbuf_new()) == NULL)
735 		fatal("%s: sshbuf_new failed", __func__);
736 	if ((r = sshbuf_put_string(m, in->value, in->length)) != 0)
737 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
738 
739 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, m);
740 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, m);
741 
742 	if ((r = sshbuf_get_u32(m, &major)) != 0 ||
743 	    (r = ssh_gssapi_get_buffer_desc(m, out)) != 0)
744 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
745 	if (flagsp != NULL) {
746 		if ((r = sshbuf_get_u32(m, &flags)) != 0)
747 			fatal("%s: buffer error: %s", __func__, ssh_err(r));
748 		*flagsp = flags;
749 	}
750 
751 	sshbuf_free(m);
752 
753 	return (major);
754 }
755 
756 OM_uint32
757 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
758 {
759 	struct sshbuf *m;
760 	OM_uint32 major;
761 	int r;
762 
763 	if ((m = sshbuf_new()) == NULL)
764 		fatal("%s: sshbuf_new failed", __func__);
765 	if ((r = sshbuf_put_string(m, gssbuf->value, gssbuf->length)) != 0 ||
766 	    (r = sshbuf_put_string(m, gssmic->value, gssmic->length)) != 0)
767 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
768 
769 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, m);
770 	mm_request_receive_expect(pmonitor->m_recvfd,
771 	    MONITOR_ANS_GSSCHECKMIC, m);
772 
773 	if ((r = sshbuf_get_u32(m, &major)) != 0)
774 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
775 	sshbuf_free(m);
776 	return(major);
777 }
778 
779 int
780 mm_ssh_gssapi_userok(char *user)
781 {
782 	struct sshbuf *m;
783 	int r, authenticated = 0;
784 
785 	if ((m = sshbuf_new()) == NULL)
786 		fatal("%s: sshbuf_new failed", __func__);
787 
788 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m);
789 	mm_request_receive_expect(pmonitor->m_recvfd,
790 	    MONITOR_ANS_GSSUSEROK, m);
791 
792 	if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
793 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
794 
795 	sshbuf_free(m);
796 	debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
797 	return (authenticated);
798 }
799 #endif /* GSSAPI */
800