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