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