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