xref: /openssh-portable/monitor_wrap.c (revision 7715a3b1)
1 /* $OpenBSD: monitor_wrap.c,v 1.121 2020/10/18 11:32:01 djm Exp $ */
2 /*
3  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4  * Copyright 2002 Markus Friedl <markus@openbsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "includes.h"
29 
30 #include <sys/types.h>
31 #include <sys/uio.h>
32 
33 #include <errno.h>
34 #include <pwd.h>
35 #include <signal.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #ifdef WITH_OPENSSL
42 #include <openssl/bn.h>
43 #include <openssl/dh.h>
44 #include <openssl/evp.h>
45 #endif
46 
47 #include "openbsd-compat/sys-queue.h"
48 #include "xmalloc.h"
49 #include "ssh.h"
50 #ifdef WITH_OPENSSL
51 #include "dh.h"
52 #endif
53 #include "sshbuf.h"
54 #include "sshkey.h"
55 #include "cipher.h"
56 #include "kex.h"
57 #include "hostfile.h"
58 #include "auth.h"
59 #include "auth-options.h"
60 #include "packet.h"
61 #include "mac.h"
62 #include "log.h"
63 #include "auth-pam.h"
64 #include "monitor.h"
65 #ifdef GSSAPI
66 #include "ssh-gss.h"
67 #endif
68 #include "monitor_wrap.h"
69 #include "atomicio.h"
70 #include "monitor_fdpass.h"
71 #include "misc.h"
72 
73 #include "channels.h"
74 #include "session.h"
75 #include "servconf.h"
76 
77 #include "ssherr.h"
78 
79 /* Imports */
80 extern struct monitor *pmonitor;
81 extern struct sshbuf *loginmsg;
82 extern ServerOptions options;
83 
84 void
85 mm_log_handler(const char *file, const char *func, int line,
86     LogLevel level, const char *msg, void *ctx)
87 {
88 	struct sshbuf *log_msg;
89 	struct monitor *mon = (struct monitor *)ctx;
90 	int r;
91 	size_t len;
92 
93 	if (mon->m_log_sendfd == -1)
94 		fatal_f("no log channel");
95 
96 	if ((log_msg = sshbuf_new()) == NULL)
97 		fatal_f("sshbuf_new failed");
98 
99 	if ((r = sshbuf_put_u32(log_msg, 0)) != 0 || /* length; filled below */
100 	    (r = sshbuf_put_cstring(log_msg, file)) != 0 ||
101 	    (r = sshbuf_put_cstring(log_msg, func)) != 0 ||
102 	    (r = sshbuf_put_u32(log_msg, (u_int)line)) != 0 ||
103 	    (r = sshbuf_put_u32(log_msg, level)) != 0 ||
104 	    (r = sshbuf_put_cstring(log_msg, msg)) != 0)
105 		fatal_fr(r, "assemble");
106 	if ((len = sshbuf_len(log_msg)) < 4 || len > 0xffffffff)
107 		fatal_f("bad length %zu", len);
108 	POKE_U32(sshbuf_mutable_ptr(log_msg), len - 4);
109 	if (atomicio(vwrite, mon->m_log_sendfd,
110 	    sshbuf_mutable_ptr(log_msg), len) != len)
111 		fatal_f("write: %s", strerror(errno));
112 	sshbuf_free(log_msg);
113 }
114 
115 int
116 mm_is_monitor(void)
117 {
118 	/*
119 	 * m_pid is only set in the privileged part, and
120 	 * points to the unprivileged child.
121 	 */
122 	return (pmonitor && pmonitor->m_pid > 0);
123 }
124 
125 void
126 mm_request_send(int sock, enum monitor_reqtype type, struct sshbuf *m)
127 {
128 	size_t mlen = sshbuf_len(m);
129 	u_char buf[5];
130 
131 	debug3_f("entering, type %d", type);
132 
133 	if (mlen >= 0xffffffff)
134 		fatal_f("bad length %zu", mlen);
135 	POKE_U32(buf, mlen + 1);
136 	buf[4] = (u_char) type;		/* 1st byte of payload is mesg-type */
137 	if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
138 		fatal_f("write: %s", strerror(errno));
139 	if (atomicio(vwrite, sock, sshbuf_mutable_ptr(m), mlen) != mlen)
140 		fatal_f("write: %s", strerror(errno));
141 }
142 
143 void
144 mm_request_receive(int sock, struct sshbuf *m)
145 {
146 	u_char buf[4], *p = NULL;
147 	u_int msg_len;
148 	int r;
149 
150 	debug3_f("entering");
151 
152 	if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
153 		if (errno == EPIPE)
154 			cleanup_exit(255);
155 		fatal_f("read: %s", strerror(errno));
156 	}
157 	msg_len = PEEK_U32(buf);
158 	if (msg_len > 256 * 1024)
159 		fatal_f("read: bad msg_len %d", msg_len);
160 	sshbuf_reset(m);
161 	if ((r = sshbuf_reserve(m, msg_len, &p)) != 0)
162 		fatal_fr(r, "reserve");
163 	if (atomicio(read, sock, p, msg_len) != msg_len)
164 		fatal_f("read: %s", strerror(errno));
165 }
166 
167 void
168 mm_request_receive_expect(int sock, enum monitor_reqtype type, struct sshbuf *m)
169 {
170 	u_char rtype;
171 	int r;
172 
173 	debug3_f("entering, type %d", type);
174 
175 	mm_request_receive(sock, m);
176 	if ((r = sshbuf_get_u8(m, &rtype)) != 0)
177 		fatal_fr(r, "parse");
178 	if (rtype != type)
179 		fatal_f("read: rtype %d != type %d", rtype, type);
180 }
181 
182 #ifdef WITH_OPENSSL
183 DH *
184 mm_choose_dh(int min, int nbits, int max)
185 {
186 	BIGNUM *p, *g;
187 	int r;
188 	u_char success = 0;
189 	struct sshbuf *m;
190 
191 	if ((m = sshbuf_new()) == NULL)
192 		fatal_f("sshbuf_new failed");
193 	if ((r = sshbuf_put_u32(m, min)) != 0 ||
194 	    (r = sshbuf_put_u32(m, nbits)) != 0 ||
195 	    (r = sshbuf_put_u32(m, max)) != 0)
196 		fatal_fr(r, "assemble");
197 
198 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, m);
199 
200 	debug3_f("waiting for MONITOR_ANS_MODULI");
201 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, m);
202 
203 	if ((r = sshbuf_get_u8(m, &success)) != 0)
204 		fatal_fr(r, "parse success");
205 	if (success == 0)
206 		fatal_f("MONITOR_ANS_MODULI failed");
207 
208 	if ((r = sshbuf_get_bignum2(m, &p)) != 0 ||
209 	    (r = sshbuf_get_bignum2(m, &g)) != 0)
210 		fatal_fr(r, "parse group");
211 
212 	debug3_f("remaining %zu", sshbuf_len(m));
213 	sshbuf_free(m);
214 
215 	return (dh_new_group(g, p));
216 }
217 #endif
218 
219 int
220 mm_sshkey_sign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
221     const u_char *data, size_t datalen, const char *hostkey_alg,
222     const char *sk_provider, const char *sk_pin, u_int compat)
223 {
224 	struct kex *kex = *pmonitor->m_pkex;
225 	struct sshbuf *m;
226 	u_int ndx = kex->host_key_index(key, 0, ssh);
227 	int r;
228 
229 	debug3_f("entering");
230 	if ((m = sshbuf_new()) == NULL)
231 		fatal_f("sshbuf_new failed");
232 	if ((r = sshbuf_put_u32(m, ndx)) != 0 ||
233 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
234 	    (r = sshbuf_put_cstring(m, hostkey_alg)) != 0 ||
235 	    (r = sshbuf_put_u32(m, compat)) != 0)
236 		fatal_fr(r, "assemble");
237 
238 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, m);
239 
240 	debug3_f("waiting for MONITOR_ANS_SIGN");
241 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, m);
242 	if ((r = sshbuf_get_string(m, sigp, lenp)) != 0)
243 		fatal_fr(r, "parse");
244 	sshbuf_free(m);
245 
246 	return (0);
247 }
248 
249 struct passwd *
250 mm_getpwnamallow(struct ssh *ssh, const char *username)
251 {
252 	struct sshbuf *m;
253 	struct passwd *pw;
254 	size_t len;
255 	u_int i;
256 	ServerOptions *newopts;
257 	int r;
258 	u_char ok;
259 	const u_char *p;
260 
261 	debug3_f("entering");
262 
263 	if ((m = sshbuf_new()) == NULL)
264 		fatal_f("sshbuf_new failed");
265 	if ((r = sshbuf_put_cstring(m, username)) != 0)
266 		fatal_fr(r, "assemble");
267 
268 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, m);
269 
270 	debug3_f("waiting for MONITOR_ANS_PWNAM");
271 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, m);
272 
273 	if ((r = sshbuf_get_u8(m, &ok)) != 0)
274 		fatal_fr(r, "parse success");
275 	if (ok == 0) {
276 		pw = NULL;
277 		goto out;
278 	}
279 
280 	/* XXX don't like passing struct passwd like this */
281 	pw = xcalloc(sizeof(*pw), 1);
282 	if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
283 		fatal_fr(r, "parse");
284 	if (len != sizeof(*pw))
285 		fatal_f("struct passwd size mismatch");
286 	memcpy(pw, p, sizeof(*pw));
287 
288 	if ((r = sshbuf_get_cstring(m, &pw->pw_name, NULL)) != 0 ||
289 	    (r = sshbuf_get_cstring(m, &pw->pw_passwd, NULL)) != 0 ||
290 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
291 	    (r = sshbuf_get_cstring(m, &pw->pw_gecos, NULL)) != 0 ||
292 #endif
293 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
294 	    (r = sshbuf_get_cstring(m, &pw->pw_class, NULL)) != 0 ||
295 #endif
296 	    (r = sshbuf_get_cstring(m, &pw->pw_dir, NULL)) != 0 ||
297 	    (r = sshbuf_get_cstring(m, &pw->pw_shell, NULL)) != 0)
298 		fatal_fr(r, "parse pw");
299 
300 out:
301 	/* copy options block as a Match directive may have changed some */
302 	if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
303 		fatal_fr(r, "parse opts");
304 	if (len != sizeof(*newopts))
305 		fatal_f("option block size mismatch");
306 	newopts = xcalloc(sizeof(*newopts), 1);
307 	memcpy(newopts, p, sizeof(*newopts));
308 
309 #define M_CP_STROPT(x) do { \
310 		if (newopts->x != NULL && \
311 		    (r = sshbuf_get_cstring(m, &newopts->x, NULL)) != 0) \
312 			fatal_fr(r, "parse %s", #x); \
313 	} while (0)
314 #define M_CP_STRARRAYOPT(x, nx) do { \
315 		newopts->x = newopts->nx == 0 ? \
316 		    NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
317 		for (i = 0; i < newopts->nx; i++) { \
318 			if ((r = sshbuf_get_cstring(m, \
319 			    &newopts->x[i], NULL)) != 0) \
320 				fatal_fr(r, "parse %s", #x); \
321 		} \
322 	} while (0)
323 	/* See comment in servconf.h */
324 	COPY_MATCH_STRING_OPTS();
325 #undef M_CP_STROPT
326 #undef M_CP_STRARRAYOPT
327 
328 	copy_set_server_options(&options, newopts, 1);
329 	log_change_level(options.log_level);
330 	log_verbose_reset();
331 	for (i = 0; i < options.num_log_verbose; i++)
332 		log_verbose_add(options.log_verbose[i]);
333 	process_permitopen(ssh, &options);
334 	free(newopts);
335 
336 	sshbuf_free(m);
337 
338 	return (pw);
339 }
340 
341 char *
342 mm_auth2_read_banner(void)
343 {
344 	struct sshbuf *m;
345 	char *banner;
346 	int r;
347 
348 	debug3_f("entering");
349 
350 	if ((m = sshbuf_new()) == NULL)
351 		fatal_f("sshbuf_new failed");
352 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, m);
353 	sshbuf_reset(m);
354 
355 	mm_request_receive_expect(pmonitor->m_recvfd,
356 	    MONITOR_ANS_AUTH2_READ_BANNER, m);
357 	if ((r = sshbuf_get_cstring(m, &banner, NULL)) != 0)
358 		fatal_fr(r, "parse");
359 	sshbuf_free(m);
360 
361 	/* treat empty banner as missing banner */
362 	if (strlen(banner) == 0) {
363 		free(banner);
364 		banner = NULL;
365 	}
366 	return (banner);
367 }
368 
369 /* Inform the privileged process about service and style */
370 
371 void
372 mm_inform_authserv(char *service, char *style)
373 {
374 	struct sshbuf *m;
375 	int r;
376 
377 	debug3_f("entering");
378 
379 	if ((m = sshbuf_new()) == NULL)
380 		fatal_f("sshbuf_new failed");
381 	if ((r = sshbuf_put_cstring(m, service)) != 0 ||
382 	    (r = sshbuf_put_cstring(m, style ? style : "")) != 0)
383 		fatal_fr(r, "assemble");
384 
385 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, m);
386 
387 	sshbuf_free(m);
388 }
389 
390 /* Do the password authentication */
391 int
392 mm_auth_password(struct ssh *ssh, char *password)
393 {
394 	struct sshbuf *m;
395 	int r, authenticated = 0;
396 #ifdef USE_PAM
397 	u_int maxtries = 0;
398 #endif
399 
400 	debug3_f("entering");
401 
402 	if ((m = sshbuf_new()) == NULL)
403 		fatal_f("sshbuf_new failed");
404 	if ((r = sshbuf_put_cstring(m, password)) != 0)
405 		fatal_fr(r, "assemble");
406 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, m);
407 
408 	debug3_f("waiting for MONITOR_ANS_AUTHPASSWORD");
409 	mm_request_receive_expect(pmonitor->m_recvfd,
410 	    MONITOR_ANS_AUTHPASSWORD, m);
411 
412 	if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
413 		fatal_fr(r, "parse");
414 #ifdef USE_PAM
415 	if ((r = sshbuf_get_u32(m, &maxtries)) != 0)
416 		fatal_fr(r, "parse PAM");
417 	if (maxtries > INT_MAX)
418 		fatal_fr(r, "bad maxtries");
419 	sshpam_set_maxtries_reached(maxtries);
420 #endif
421 
422 	sshbuf_free(m);
423 
424 	debug3_f("user %sauthenticated", authenticated ? "" : "not ");
425 	return (authenticated);
426 }
427 
428 int
429 mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
430     int pubkey_auth_attempt, struct sshauthopt **authoptp)
431 {
432 	return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
433 	    pubkey_auth_attempt, authoptp));
434 }
435 
436 int
437 mm_hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
438     const char *user, const char *host, struct sshkey *key)
439 {
440 	return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL));
441 }
442 
443 int
444 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
445     struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp)
446 {
447 	struct sshbuf *m;
448 	int r, allowed = 0;
449 	struct sshauthopt *opts = NULL;
450 
451 	debug3_f("entering");
452 
453 	if (authoptp != NULL)
454 		*authoptp = NULL;
455 
456 	if ((m = sshbuf_new()) == NULL)
457 		fatal_f("sshbuf_new failed");
458 	if ((r = sshbuf_put_u32(m, type)) != 0 ||
459 	    (r = sshbuf_put_cstring(m, user ? user : "")) != 0 ||
460 	    (r = sshbuf_put_cstring(m, host ? host : "")) != 0 ||
461 	    (r = sshkey_puts(key, m)) != 0 ||
462 	    (r = sshbuf_put_u32(m, pubkey_auth_attempt)) != 0)
463 		fatal_fr(r, "assemble");
464 
465 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, m);
466 
467 	debug3_f("waiting for MONITOR_ANS_KEYALLOWED");
468 	mm_request_receive_expect(pmonitor->m_recvfd,
469 	    MONITOR_ANS_KEYALLOWED, m);
470 
471 	if ((r = sshbuf_get_u32(m, &allowed)) != 0)
472 		fatal_fr(r, "parse");
473 	if (allowed && type == MM_USERKEY &&
474 	    (r = sshauthopt_deserialise(m, &opts)) != 0)
475 		fatal_fr(r, "sshauthopt_deserialise");
476 	sshbuf_free(m);
477 
478 	if (authoptp != NULL) {
479 		*authoptp = opts;
480 		opts = NULL;
481 	}
482 	sshauthopt_free(opts);
483 
484 	return allowed;
485 }
486 
487 /*
488  * This key verify needs to send the key type along, because the
489  * privileged parent makes the decision if the key is allowed
490  * for authentication.
491  */
492 
493 int
494 mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
495     const u_char *data, size_t datalen, const char *sigalg, u_int compat,
496     struct sshkey_sig_details **sig_detailsp)
497 {
498 	struct sshbuf *m;
499 	u_int encoded_ret = 0;
500 	int r;
501 	u_char sig_details_present, flags;
502 	u_int counter;
503 
504 	debug3_f("entering");
505 
506 	if (sig_detailsp != NULL)
507 		*sig_detailsp = NULL;
508 	if ((m = sshbuf_new()) == NULL)
509 		fatal_f("sshbuf_new failed");
510 	if ((r = sshkey_puts(key, m)) != 0 ||
511 	    (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
512 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
513 	    (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
514 		fatal_fr(r, "assemble");
515 
516 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, m);
517 
518 	debug3_f("waiting for MONITOR_ANS_KEYVERIFY");
519 	mm_request_receive_expect(pmonitor->m_recvfd,
520 	    MONITOR_ANS_KEYVERIFY, m);
521 
522 	if ((r = sshbuf_get_u32(m, &encoded_ret)) != 0 ||
523 	    (r = sshbuf_get_u8(m, &sig_details_present)) != 0)
524 		fatal_fr(r, "parse");
525 	if (sig_details_present && encoded_ret == 0) {
526 		if ((r = sshbuf_get_u32(m, &counter)) != 0 ||
527 		    (r = sshbuf_get_u8(m, &flags)) != 0)
528 			fatal_fr(r, "parse sig_details");
529 		if (sig_detailsp != NULL) {
530 			*sig_detailsp = xcalloc(1, sizeof(**sig_detailsp));
531 			(*sig_detailsp)->sk_counter = counter;
532 			(*sig_detailsp)->sk_flags = flags;
533 		}
534 	}
535 
536 	sshbuf_free(m);
537 
538 	if (encoded_ret != 0)
539 		return SSH_ERR_SIGNATURE_INVALID;
540 	return 0;
541 }
542 
543 void
544 mm_send_keystate(struct ssh *ssh, struct monitor *monitor)
545 {
546 	struct sshbuf *m;
547 	int r;
548 
549 	if ((m = sshbuf_new()) == NULL)
550 		fatal_f("sshbuf_new failed");
551 	if ((r = ssh_packet_get_state(ssh, m)) != 0)
552 		fatal_fr(r, "ssh_packet_get_state");
553 	mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
554 	debug3_f("Finished sending state");
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_f("cannot allocate fds for pty");
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_f("sshbuf_new failed");
580 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, m);
581 
582 	debug3_f("waiting for MONITOR_ANS_PTY");
583 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, m);
584 
585 	if ((r = sshbuf_get_u32(m, &success)) != 0)
586 		fatal_fr(r, "parse success");
587 	if (success == 0) {
588 		debug3_f("pty alloc failed");
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_fr(r, "parse");
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_fr(r, "put loginmsg");
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_f("receive fds failed");
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_f("sshbuf_new failed");
622 	if ((r = sshbuf_put_cstring(m, s->tty)) != 0)
623 		fatal_fr(r, "assmble");
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) == -1)
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(struct ssh *ssh)
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_f("sshbuf_new failed");
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_f("entering");
823 
824 	if ((m = sshbuf_new()) == NULL)
825 		fatal_f("sshbuf_new failed");
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_fr(r, "parse success");
832 	if (success == 0) {
833 		debug3_f("no challenge");
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_fr(r, "parse challenge");
841 	sshbuf_free(m);
842 
843 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
844 	(*prompts)[0] = challenge;
845 
846 	debug3_f("received challenge: %s", 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_f("entering");
858 	if (numresponses != 1)
859 		return (-1);
860 
861 	if ((m = sshbuf_new()) == NULL)
862 		fatal_f("sshbuf_new failed");
863 	if ((r = sshbuf_put_cstring(m, responses[0])) != 0)
864 		fatal_fr(r, "assemble");
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_fr(r, "parse");
872 	sshbuf_free(m);
873 
874 	return ((authok == 0) ? -1 : 0);
875 }
876 
877 #ifdef SSH_AUDIT_EVENTS
878 void
879 mm_audit_event(struct ssh *ssh, ssh_audit_event_t event)
880 {
881 	struct sshbuf *m;
882 	int r;
883 
884 	debug3("%s entering", __func__);
885 
886 	if ((m = sshbuf_new()) == NULL)
887 		fatal("%s: sshbuf_new failed", __func__);
888 	if ((r = sshbuf_put_u32(m, event)) != 0)
889 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
890 
891 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, m);
892 	sshbuf_free(m);
893 }
894 
895 void
896 mm_audit_run_command(const char *command)
897 {
898 	struct sshbuf *m;
899 	int r;
900 
901 	debug3("%s entering command %s", __func__, command);
902 
903 	if ((m = sshbuf_new()) == NULL)
904 		fatal("%s: sshbuf_new failed", __func__);
905 	if ((r = sshbuf_put_cstring(m, command)) != 0)
906 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
907 
908 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, m);
909 	sshbuf_free(m);
910 }
911 #endif /* SSH_AUDIT_EVENTS */
912 
913 #ifdef GSSAPI
914 OM_uint32
915 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
916 {
917 	struct sshbuf *m;
918 	OM_uint32 major;
919 	int r;
920 
921 	/* Client doesn't get to see the context */
922 	*ctx = NULL;
923 
924 	if ((m = sshbuf_new()) == NULL)
925 		fatal_f("sshbuf_new failed");
926 	if ((r = sshbuf_put_string(m, goid->elements, goid->length)) != 0)
927 		fatal_fr(r, "assemble");
928 
929 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, m);
930 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, m);
931 
932 	if ((r = sshbuf_get_u32(m, &major)) != 0)
933 		fatal_fr(r, "parse");
934 
935 	sshbuf_free(m);
936 	return (major);
937 }
938 
939 OM_uint32
940 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
941     gss_buffer_desc *out, OM_uint32 *flagsp)
942 {
943 	struct sshbuf *m;
944 	OM_uint32 major;
945 	u_int flags;
946 	int r;
947 
948 	if ((m = sshbuf_new()) == NULL)
949 		fatal_f("sshbuf_new failed");
950 	if ((r = sshbuf_put_string(m, in->value, in->length)) != 0)
951 		fatal_fr(r, "assemble");
952 
953 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, m);
954 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, m);
955 
956 	if ((r = sshbuf_get_u32(m, &major)) != 0 ||
957 	    (r = ssh_gssapi_get_buffer_desc(m, out)) != 0)
958 		fatal_fr(r, "parse");
959 	if (flagsp != NULL) {
960 		if ((r = sshbuf_get_u32(m, &flags)) != 0)
961 			fatal_fr(r, "parse flags");
962 		*flagsp = flags;
963 	}
964 
965 	sshbuf_free(m);
966 
967 	return (major);
968 }
969 
970 OM_uint32
971 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
972 {
973 	struct sshbuf *m;
974 	OM_uint32 major;
975 	int r;
976 
977 	if ((m = sshbuf_new()) == NULL)
978 		fatal_f("sshbuf_new failed");
979 	if ((r = sshbuf_put_string(m, gssbuf->value, gssbuf->length)) != 0 ||
980 	    (r = sshbuf_put_string(m, gssmic->value, gssmic->length)) != 0)
981 		fatal_fr(r, "assemble");
982 
983 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, m);
984 	mm_request_receive_expect(pmonitor->m_recvfd,
985 	    MONITOR_ANS_GSSCHECKMIC, m);
986 
987 	if ((r = sshbuf_get_u32(m, &major)) != 0)
988 		fatal_fr(r, "parse");
989 	sshbuf_free(m);
990 	return(major);
991 }
992 
993 int
994 mm_ssh_gssapi_userok(char *user)
995 {
996 	struct sshbuf *m;
997 	int r, authenticated = 0;
998 
999 	if ((m = sshbuf_new()) == NULL)
1000 		fatal_f("sshbuf_new failed");
1001 
1002 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m);
1003 	mm_request_receive_expect(pmonitor->m_recvfd,
1004 	    MONITOR_ANS_GSSUSEROK, m);
1005 
1006 	if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
1007 		fatal_fr(r, "parse");
1008 
1009 	sshbuf_free(m);
1010 	debug3_f("user %sauthenticated", authenticated ? "" : "not ");
1011 	return (authenticated);
1012 }
1013 #endif /* GSSAPI */
1014