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