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