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