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