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