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