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