xref: /netbsd-src/crypto/external/bsd/openssh/dist/monitor_wrap.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: monitor_wrap.c,v 1.3 2009/12/27 01:40:47 christos Exp $	*/
2 /* $OpenBSD: monitor_wrap.c,v 1.68 2009/06/22 05:39:28 dtucker Exp $ */
3 /*
4  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
5  * Copyright 2002 Markus Friedl <markus@openbsd.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "includes.h"
30 __RCSID("$NetBSD: monitor_wrap.c,v 1.3 2009/12/27 01:40:47 christos Exp $");
31 #include <sys/types.h>
32 #include <sys/uio.h>
33 #include <sys/queue.h>
34 
35 #include <errno.h>
36 #include <pwd.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include <openssl/bn.h>
43 #include <openssl/dh.h>
44 #include <openssl/evp.h>
45 
46 #include "xmalloc.h"
47 #include "ssh.h"
48 #include "dh.h"
49 #include "buffer.h"
50 #include "key.h"
51 #include "cipher.h"
52 #include "kex.h"
53 #include "hostfile.h"
54 #include "auth.h"
55 #include "auth-options.h"
56 #include "packet.h"
57 #include "mac.h"
58 #include "log.h"
59 #include <zlib.h>
60 #include "monitor.h"
61 #ifdef GSSAPI
62 #include "ssh-gss.h"
63 #endif
64 #include "monitor_wrap.h"
65 #include "atomicio.h"
66 #include "monitor_fdpass.h"
67 #ifdef USE_PAM
68 #include "servconf.h"
69 #include <security/pam_appl.h>
70 #endif
71 #include "misc.h"
72 #include "schnorr.h"
73 #include "jpake.h"
74 
75 #include "channels.h"
76 #include "session.h"
77 #include "servconf.h"
78 #include "roaming.h"
79 
80 /* Imports */
81 extern int compat20;
82 extern z_stream incoming_stream;
83 extern z_stream outgoing_stream;
84 extern struct monitor *pmonitor;
85 extern Buffer loginmsg;
86 extern ServerOptions options;
87 
88 int
89 mm_is_monitor(void)
90 {
91 	/*
92 	 * m_pid is only set in the privileged part, and
93 	 * points to the unprivileged child.
94 	 */
95 	return (pmonitor && pmonitor->m_pid > 0);
96 }
97 
98 void
99 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
100 {
101 	u_int mlen = buffer_len(m);
102 	u_char buf[5];
103 
104 	debug3("%s entering: type %d", __func__, type);
105 
106 	put_u32(buf, mlen + 1);
107 	buf[4] = (u_char) type;		/* 1st byte of payload is mesg-type */
108 	if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
109 		fatal("%s: write: %s", __func__, strerror(errno));
110 	if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
111 		fatal("%s: write: %s", __func__, strerror(errno));
112 }
113 
114 void
115 mm_request_receive(int sock, Buffer *m)
116 {
117 	u_char buf[4];
118 	u_int msg_len;
119 
120 	debug3("%s entering", __func__);
121 
122 	if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
123 		if (errno == EPIPE)
124 			cleanup_exit(255);
125 		fatal("%s: read: %s", __func__, strerror(errno));
126 	}
127 	msg_len = get_u32(buf);
128 	if (msg_len > 256 * 1024)
129 		fatal("%s: read: bad msg_len %d", __func__, msg_len);
130 	buffer_clear(m);
131 	buffer_append_space(m, msg_len);
132 	if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
133 		fatal("%s: read: %s", __func__, strerror(errno));
134 }
135 
136 void
137 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
138 {
139 	u_char rtype;
140 
141 	debug3("%s entering: type %d", __func__, type);
142 
143 	mm_request_receive(sock, m);
144 	rtype = buffer_get_char(m);
145 	if (rtype != type)
146 		fatal("%s: read: rtype %d != type %d", __func__,
147 		    rtype, type);
148 }
149 
150 DH *
151 mm_choose_dh(int min, int nbits, int max)
152 {
153 	BIGNUM *p, *g;
154 	int success = 0;
155 	Buffer m;
156 
157 	buffer_init(&m);
158 	buffer_put_int(&m, min);
159 	buffer_put_int(&m, nbits);
160 	buffer_put_int(&m, max);
161 
162 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
163 
164 	debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
165 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
166 
167 	success = buffer_get_char(&m);
168 	if (success == 0)
169 		fatal("%s: MONITOR_ANS_MODULI failed", __func__);
170 
171 	if ((p = BN_new()) == NULL)
172 		fatal("%s: BN_new failed", __func__);
173 	if ((g = BN_new()) == NULL)
174 		fatal("%s: BN_new failed", __func__);
175 	buffer_get_bignum2(&m, p);
176 	buffer_get_bignum2(&m, g);
177 
178 	debug3("%s: remaining %d", __func__, buffer_len(&m));
179 	buffer_free(&m);
180 
181 	return (dh_new_group(g, p));
182 }
183 
184 int
185 mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
186 {
187 	Kex *kex = *pmonitor->m_pkex;
188 	Buffer m;
189 
190 	debug3("%s entering", __func__);
191 
192 	buffer_init(&m);
193 	buffer_put_int(&m, kex->host_key_index(key));
194 	buffer_put_string(&m, data, datalen);
195 
196 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
197 
198 	debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
199 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
200 	*sigp  = buffer_get_string(&m, lenp);
201 	buffer_free(&m);
202 
203 	return (0);
204 }
205 
206 struct passwd *
207 mm_getpwnamallow(const char *username)
208 {
209 	Buffer m;
210 	struct passwd *pw;
211 	u_int len;
212 	ServerOptions *newopts;
213 
214 	debug3("%s entering", __func__);
215 
216 	buffer_init(&m);
217 	buffer_put_cstring(&m, username);
218 
219 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
220 
221 	debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
222 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
223 
224 	if (buffer_get_char(&m) == 0) {
225 		pw = NULL;
226 		goto out;
227 	}
228 	pw = buffer_get_string(&m, &len);
229 	if (len != sizeof(struct passwd))
230 		fatal("%s: struct passwd size mismatch", __func__);
231 	pw->pw_name = buffer_get_string(&m, NULL);
232 	pw->pw_passwd = buffer_get_string(&m, NULL);
233 	pw->pw_gecos = buffer_get_string(&m, NULL);
234 	pw->pw_class = buffer_get_string(&m, NULL);
235 	pw->pw_dir = buffer_get_string(&m, NULL);
236 	pw->pw_shell = buffer_get_string(&m, NULL);
237 
238 out:
239 	/* copy options block as a Match directive may have changed some */
240 	newopts = buffer_get_string(&m, &len);
241 	if (len != sizeof(*newopts))
242 		fatal("%s: option block size mismatch", __func__);
243 	if (newopts->banner != NULL)
244 		newopts->banner = buffer_get_string(&m, NULL);
245 	copy_set_server_options(&options, newopts, 1);
246 	xfree(newopts);
247 
248 	buffer_free(&m);
249 
250 	return (pw);
251 }
252 
253 char *
254 mm_auth2_read_banner(void)
255 {
256 	Buffer m;
257 	char *banner;
258 
259 	debug3("%s entering", __func__);
260 
261 	buffer_init(&m);
262 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
263 	buffer_clear(&m);
264 
265 	mm_request_receive_expect(pmonitor->m_recvfd,
266 	    MONITOR_ANS_AUTH2_READ_BANNER, &m);
267 	banner = buffer_get_string(&m, NULL);
268 	buffer_free(&m);
269 
270 	/* treat empty banner as missing banner */
271 	if (strlen(banner) == 0) {
272 		xfree(banner);
273 		banner = NULL;
274 	}
275 	return (banner);
276 }
277 
278 /* Inform the privileged process about service and style */
279 
280 void
281 mm_inform_authserv(char *service, char *style)
282 {
283 	Buffer m;
284 
285 	debug3("%s entering", __func__);
286 
287 	buffer_init(&m);
288 	buffer_put_cstring(&m, service);
289 	buffer_put_cstring(&m, style ? style : "");
290 
291 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
292 
293 	buffer_free(&m);
294 }
295 
296 /* Do the password authentication */
297 int
298 mm_auth_password(Authctxt *authctxt, char *password)
299 {
300 	Buffer m;
301 	int authenticated = 0;
302 
303 	debug3("%s entering", __func__);
304 
305 	buffer_init(&m);
306 	buffer_put_cstring(&m, password);
307 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
308 
309 	debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
310 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
311 
312 	authenticated = buffer_get_int(&m);
313 
314 	buffer_free(&m);
315 
316 	debug3("%s: user %sauthenticated",
317 	    __func__, authenticated ? "" : "not ");
318 	return (authenticated);
319 }
320 
321 int
322 mm_user_key_allowed(struct passwd *pw, Key *key)
323 {
324 	return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
325 }
326 
327 int
328 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
329     Key *key)
330 {
331 	return (mm_key_allowed(MM_HOSTKEY, user, host, key));
332 }
333 
334 int
335 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
336     char *host, Key *key)
337 {
338 	int ret;
339 
340 	key->type = KEY_RSA; /* XXX hack for key_to_blob */
341 	ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
342 	key->type = KEY_RSA1;
343 	return (ret);
344 }
345 
346 static void
347 mm_send_debug(Buffer *m)
348 {
349 	char *msg;
350 
351 	while (buffer_len(m)) {
352 		msg = buffer_get_string(m, NULL);
353 		debug3("%s: Sending debug: %s", __func__, msg);
354 		packet_send_debug("%s", msg);
355 		xfree(msg);
356 	}
357 }
358 
359 int
360 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
361 {
362 	Buffer m;
363 	u_char *blob;
364 	u_int len;
365 	int allowed = 0, have_forced = 0;
366 
367 	debug3("%s entering", __func__);
368 
369 	/* Convert the key to a blob and the pass it over */
370 	if (!key_to_blob(key, &blob, &len))
371 		return (0);
372 
373 	buffer_init(&m);
374 	buffer_put_int(&m, type);
375 	buffer_put_cstring(&m, user ? user : "");
376 	buffer_put_cstring(&m, host ? host : "");
377 	buffer_put_string(&m, blob, len);
378 	xfree(blob);
379 
380 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
381 
382 	debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
383 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
384 
385 	allowed = buffer_get_int(&m);
386 
387 	/* fake forced command */
388 	auth_clear_options();
389 	have_forced = buffer_get_int(&m);
390 	forced_command = have_forced ? xstrdup("true") : NULL;
391 
392 	/* Send potential debug messages */
393 	mm_send_debug(&m);
394 
395 	buffer_free(&m);
396 
397 	return (allowed);
398 }
399 
400 /*
401  * This key verify needs to send the key type along, because the
402  * privileged parent makes the decision if the key is allowed
403  * for authentication.
404  */
405 
406 int
407 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
408 {
409 	Buffer m;
410 	u_char *blob;
411 	u_int len;
412 	int verified = 0;
413 
414 	debug3("%s entering", __func__);
415 
416 	/* Convert the key to a blob and the pass it over */
417 	if (!key_to_blob(key, &blob, &len))
418 		return (0);
419 
420 	buffer_init(&m);
421 	buffer_put_string(&m, blob, len);
422 	buffer_put_string(&m, sig, siglen);
423 	buffer_put_string(&m, data, datalen);
424 	xfree(blob);
425 
426 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
427 
428 	debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
429 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
430 
431 	verified = buffer_get_int(&m);
432 
433 	buffer_free(&m);
434 
435 	return (verified);
436 }
437 
438 /* Export key state after authentication */
439 Newkeys *
440 mm_newkeys_from_blob(u_char *blob, int blen)
441 {
442 	Buffer b;
443 	u_int len;
444 	Newkeys *newkey = NULL;
445 	Enc *enc;
446 	Mac *mac;
447 	Comp *comp;
448 
449 	debug3("%s: %p(%d)", __func__, blob, blen);
450 #ifdef DEBUG_PK
451 	dump_base64(stderr, blob, blen);
452 #endif
453 	buffer_init(&b);
454 	buffer_append(&b, blob, blen);
455 
456 	newkey = xmalloc(sizeof(*newkey));
457 	enc = &newkey->enc;
458 	mac = &newkey->mac;
459 	comp = &newkey->comp;
460 
461 	/* Enc structure */
462 	enc->name = buffer_get_string(&b, NULL);
463 	buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
464 	enc->enabled = buffer_get_int(&b);
465 	enc->block_size = buffer_get_int(&b);
466 	enc->key = buffer_get_string(&b, &enc->key_len);
467 	enc->iv = buffer_get_string(&b, &len);
468 	if (len != enc->block_size)
469 		fatal("%s: bad ivlen: expected %u != %u", __func__,
470 		    enc->block_size, len);
471 
472 	if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
473 		fatal("%s: bad cipher name %s or pointer %p", __func__,
474 		    enc->name, enc->cipher);
475 
476 	/* Mac structure */
477 	mac->name = buffer_get_string(&b, NULL);
478 	if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
479 		fatal("%s: can not setup mac %s", __func__, mac->name);
480 	mac->enabled = buffer_get_int(&b);
481 	mac->key = buffer_get_string(&b, &len);
482 	if (len > mac->key_len)
483 		fatal("%s: bad mac key length: %u > %d", __func__, len,
484 		    mac->key_len);
485 	mac->key_len = len;
486 
487 	/* Comp structure */
488 	comp->type = buffer_get_int(&b);
489 	comp->enabled = buffer_get_int(&b);
490 	comp->name = buffer_get_string(&b, NULL);
491 
492 	len = buffer_len(&b);
493 	if (len != 0)
494 		error("newkeys_from_blob: remaining bytes in blob %u", len);
495 	buffer_free(&b);
496 	return (newkey);
497 }
498 
499 int
500 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
501 {
502 	Buffer b;
503 	int len;
504 	Enc *enc;
505 	Mac *mac;
506 	Comp *comp;
507 	Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
508 
509 	debug3("%s: converting %p", __func__, newkey);
510 
511 	if (newkey == NULL) {
512 		error("%s: newkey == NULL", __func__);
513 		return 0;
514 	}
515 	enc = &newkey->enc;
516 	mac = &newkey->mac;
517 	comp = &newkey->comp;
518 
519 	buffer_init(&b);
520 	/* Enc structure */
521 	buffer_put_cstring(&b, enc->name);
522 	/* The cipher struct is constant and shared, you export pointer */
523 	buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
524 	buffer_put_int(&b, enc->enabled);
525 	buffer_put_int(&b, enc->block_size);
526 	buffer_put_string(&b, enc->key, enc->key_len);
527 	packet_get_keyiv(mode, enc->iv, enc->block_size);
528 	buffer_put_string(&b, enc->iv, enc->block_size);
529 
530 	/* Mac structure */
531 	buffer_put_cstring(&b, mac->name);
532 	buffer_put_int(&b, mac->enabled);
533 	buffer_put_string(&b, mac->key, mac->key_len);
534 
535 	/* Comp structure */
536 	buffer_put_int(&b, comp->type);
537 	buffer_put_int(&b, comp->enabled);
538 	buffer_put_cstring(&b, comp->name);
539 
540 	len = buffer_len(&b);
541 	if (lenp != NULL)
542 		*lenp = len;
543 	if (blobp != NULL) {
544 		*blobp = xmalloc(len);
545 		memcpy(*blobp, buffer_ptr(&b), len);
546 	}
547 	memset(buffer_ptr(&b), 0, len);
548 	buffer_free(&b);
549 	return len;
550 }
551 
552 static void
553 mm_send_kex(Buffer *m, Kex *kex)
554 {
555 	buffer_put_string(m, kex->session_id, kex->session_id_len);
556 	buffer_put_int(m, kex->we_need);
557 	buffer_put_int(m, kex->hostkey_type);
558 	buffer_put_int(m, kex->kex_type);
559 	buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
560 	buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
561 	buffer_put_int(m, kex->flags);
562 	buffer_put_cstring(m, kex->client_version_string);
563 	buffer_put_cstring(m, kex->server_version_string);
564 }
565 
566 void
567 mm_send_keystate(struct monitor *monitor)
568 {
569 	Buffer m, *input, *output;
570 	u_char *blob, *p;
571 	u_int bloblen, plen;
572 	u_int32_t seqnr, packets;
573 	u_int64_t blocks, bytes;
574 
575 	buffer_init(&m);
576 
577 	if (!compat20) {
578 		u_char iv[24];
579 		u_char *key;
580 		u_int ivlen, keylen;
581 
582 		buffer_put_int(&m, packet_get_protocol_flags());
583 
584 		buffer_put_int(&m, packet_get_ssh1_cipher());
585 
586 		debug3("%s: Sending ssh1 KEY+IV", __func__);
587 		keylen = packet_get_encryption_key(NULL);
588 		key = xmalloc(keylen+1);	/* add 1 if keylen == 0 */
589 		keylen = packet_get_encryption_key(key);
590 		buffer_put_string(&m, key, keylen);
591 		memset(key, 0, keylen);
592 		xfree(key);
593 
594 		ivlen = packet_get_keyiv_len(MODE_OUT);
595 		packet_get_keyiv(MODE_OUT, iv, ivlen);
596 		buffer_put_string(&m, iv, ivlen);
597 		ivlen = packet_get_keyiv_len(MODE_OUT);
598 		packet_get_keyiv(MODE_IN, iv, ivlen);
599 		buffer_put_string(&m, iv, ivlen);
600 		goto skip;
601 	} else {
602 		/* Kex for rekeying */
603 		mm_send_kex(&m, *monitor->m_pkex);
604 	}
605 
606 	debug3("%s: Sending new keys: %p %p",
607 	    __func__, packet_get_newkeys(MODE_OUT),
608 	    packet_get_newkeys(MODE_IN));
609 
610 	/* Keys from Kex */
611 	if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
612 		fatal("%s: conversion of newkeys failed", __func__);
613 
614 	buffer_put_string(&m, blob, bloblen);
615 	xfree(blob);
616 
617 	if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
618 		fatal("%s: conversion of newkeys failed", __func__);
619 
620 	buffer_put_string(&m, blob, bloblen);
621 	xfree(blob);
622 
623 	packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
624 	buffer_put_int(&m, seqnr);
625 	buffer_put_int64(&m, blocks);
626 	buffer_put_int(&m, packets);
627 	buffer_put_int64(&m, bytes);
628 	packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
629 	buffer_put_int(&m, seqnr);
630 	buffer_put_int64(&m, blocks);
631 	buffer_put_int(&m, packets);
632 	buffer_put_int64(&m, bytes);
633 
634 	debug3("%s: New keys have been sent", __func__);
635  skip:
636 	/* More key context */
637 	plen = packet_get_keycontext(MODE_OUT, NULL);
638 	p = xmalloc(plen+1);
639 	packet_get_keycontext(MODE_OUT, p);
640 	buffer_put_string(&m, p, plen);
641 	xfree(p);
642 
643 	plen = packet_get_keycontext(MODE_IN, NULL);
644 	p = xmalloc(plen+1);
645 	packet_get_keycontext(MODE_IN, p);
646 	buffer_put_string(&m, p, plen);
647 	xfree(p);
648 
649 	/* Compression state */
650 	debug3("%s: Sending compression state", __func__);
651 	buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
652 	buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
653 
654 	/* Network I/O buffers */
655 	input = (Buffer *)packet_get_input();
656 	output = (Buffer *)packet_get_output();
657 	buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
658 	buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
659 
660 	/* Roaming */
661 	if (compat20) {
662 		buffer_put_int64(&m, get_sent_bytes());
663 		buffer_put_int64(&m, get_recv_bytes());
664 	}
665 
666 	mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
667 	debug3("%s: Finished sending state", __func__);
668 
669 	buffer_free(&m);
670 }
671 
672 int
673 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
674 {
675 	Buffer m;
676 	char *p, *msg;
677 	int success = 0, tmp1 = -1, tmp2 = -1;
678 
679 	/* Kludge: ensure there are fds free to receive the pty/tty */
680 	if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
681 	    (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
682 		error("%s: cannot allocate fds for pty", __func__);
683 		if (tmp1 > 0)
684 			close(tmp1);
685 		if (tmp2 > 0)
686 			close(tmp2);
687 		return 0;
688 	}
689 	close(tmp1);
690 	close(tmp2);
691 
692 	buffer_init(&m);
693 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
694 
695 	debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
696 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
697 
698 	success = buffer_get_int(&m);
699 	if (success == 0) {
700 		debug3("%s: pty alloc failed", __func__);
701 		buffer_free(&m);
702 		return (0);
703 	}
704 	p = buffer_get_string(&m, NULL);
705 	msg = buffer_get_string(&m, NULL);
706 	buffer_free(&m);
707 
708 	strlcpy(namebuf, p, namebuflen); /* Possible truncation */
709 	xfree(p);
710 
711 	buffer_append(&loginmsg, msg, strlen(msg));
712 	xfree(msg);
713 
714 	if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
715 	    (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
716 		fatal("%s: receive fds failed", __func__);
717 
718 	/* Success */
719 	return (1);
720 }
721 
722 void
723 mm_session_pty_cleanup2(Session *s)
724 {
725 	Buffer m;
726 
727 	if (s->ttyfd == -1)
728 		return;
729 	buffer_init(&m);
730 	buffer_put_cstring(&m, s->tty);
731 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
732 	buffer_free(&m);
733 
734 	/* closed dup'ed master */
735 	if (s->ptymaster != -1 && close(s->ptymaster) < 0)
736 		error("close(s->ptymaster/%d): %s",
737 		    s->ptymaster, strerror(errno));
738 
739 	/* unlink pty from session */
740 	s->ttyfd = -1;
741 }
742 
743 #ifdef USE_PAM
744 void
745 mm_start_pam(Authctxt *authctxt)
746 {
747 	Buffer m;
748 
749 	debug3("%s entering", __func__);
750 	if (!options.use_pam)
751 		fatal("UsePAM=no, but ended up in %s anyway", __func__);
752 
753 	buffer_init(&m);
754 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
755 
756 	buffer_free(&m);
757 }
758 
759 u_int
760 mm_do_pam_account(void)
761 {
762 	Buffer m;
763 	u_int ret;
764 	char *msg;
765 
766 	debug3("%s entering", __func__);
767 	if (!options.use_pam)
768 		fatal("UsePAM=no, but ended up in %s anyway", __func__);
769 
770 	buffer_init(&m);
771 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
772 
773 	mm_request_receive_expect(pmonitor->m_recvfd,
774 	    MONITOR_ANS_PAM_ACCOUNT, &m);
775 	ret = buffer_get_int(&m);
776 	msg = buffer_get_string(&m, NULL);
777 	buffer_append(&loginmsg, msg, strlen(msg));
778 	xfree(msg);
779 
780 	buffer_free(&m);
781 
782 	debug3("%s returning %d", __func__, ret);
783 
784 	return (ret);
785 }
786 
787 void *
788 mm_sshpam_init_ctx(Authctxt *authctxt)
789 {
790 	Buffer m;
791 	int success;
792 
793 	debug3("%s", __func__);
794 	buffer_init(&m);
795 	buffer_put_cstring(&m, authctxt->user);
796 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
797 	debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
798 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
799 	success = buffer_get_int(&m);
800 	if (success == 0) {
801 		debug3("%s: pam_init_ctx failed", __func__);
802 		buffer_free(&m);
803 		return (NULL);
804 	}
805 	buffer_free(&m);
806 	return (authctxt);
807 }
808 
809 int
810 mm_sshpam_query(void *ctx, char **name, char **info,
811     u_int *num, char ***prompts, u_int **echo_on)
812 {
813 	Buffer m;
814 	u_int i;
815 	int ret;
816 
817 	debug3("%s", __func__);
818 	buffer_init(&m);
819 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
820 	debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
821 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
822 	ret = buffer_get_int(&m);
823 	debug3("%s: pam_query returned %d", __func__, ret);
824 	*name = buffer_get_string(&m, NULL);
825 	*info = buffer_get_string(&m, NULL);
826 	*num = buffer_get_int(&m);
827 	if (*num > PAM_MAX_NUM_MSG)
828 		fatal("%s: received %u PAM messages, expected <= %u",
829 		    __func__, *num, PAM_MAX_NUM_MSG);
830 	*prompts = xcalloc((*num + 1), sizeof(char *));
831 	*echo_on = xcalloc((*num + 1), sizeof(u_int));
832 	for (i = 0; i < *num; ++i) {
833 		(*prompts)[i] = buffer_get_string(&m, NULL);
834 		(*echo_on)[i] = buffer_get_int(&m);
835 	}
836 	buffer_free(&m);
837 	return (ret);
838 }
839 
840 int
841 mm_sshpam_respond(void *ctx, u_int num, char **resp)
842 {
843 	Buffer m;
844 	u_int i;
845 	int ret;
846 
847 	debug3("%s", __func__);
848 	buffer_init(&m);
849 	buffer_put_int(&m, num);
850 	for (i = 0; i < num; ++i)
851 		buffer_put_cstring(&m, resp[i]);
852 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
853 	debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
854 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
855 	ret = buffer_get_int(&m);
856 	debug3("%s: pam_respond returned %d", __func__, ret);
857 	buffer_free(&m);
858 	return (ret);
859 }
860 
861 void
862 mm_sshpam_free_ctx(void *ctxtp)
863 {
864 	Buffer m;
865 
866 	debug3("%s", __func__);
867 	buffer_init(&m);
868 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
869 	debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
870 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
871 	buffer_free(&m);
872 }
873 #endif /* USE_PAM */
874 
875 /* Request process termination */
876 
877 void
878 mm_terminate(void)
879 {
880 	Buffer m;
881 
882 	buffer_init(&m);
883 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
884 	buffer_free(&m);
885 }
886 
887 int
888 mm_ssh1_session_key(BIGNUM *num)
889 {
890 	int rsafail;
891 	Buffer m;
892 
893 	buffer_init(&m);
894 	buffer_put_bignum2(&m, num);
895 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
896 
897 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
898 
899 	rsafail = buffer_get_int(&m);
900 	buffer_get_bignum2(&m, num);
901 
902 	buffer_free(&m);
903 
904 	return (rsafail);
905 }
906 
907 #if defined(BSD_AUTH) || defined(SKEY)
908 static void
909 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
910     char ***prompts, u_int **echo_on)
911 {
912 	*name = xstrdup("");
913 	*infotxt = xstrdup("");
914 	*numprompts = 1;
915 	*prompts = xcalloc(*numprompts, sizeof(char *));
916 	*echo_on = xcalloc(*numprompts, sizeof(u_int));
917 	(*echo_on)[0] = 0;
918 }
919 #endif
920 
921 #ifdef BSD_AUTH
922 int
923 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
924    u_int *numprompts, char ***prompts, u_int **echo_on)
925 {
926 	Buffer m;
927 	u_int success;
928 	char *challenge;
929 
930 	debug3("%s: entering", __func__);
931 
932 	buffer_init(&m);
933 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
934 
935 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
936 	    &m);
937 	success = buffer_get_int(&m);
938 	if (success == 0) {
939 		debug3("%s: no challenge", __func__);
940 		buffer_free(&m);
941 		return (-1);
942 	}
943 
944 	/* Get the challenge, and format the response */
945 	challenge  = buffer_get_string(&m, NULL);
946 	buffer_free(&m);
947 
948 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
949 	(*prompts)[0] = challenge;
950 
951 	debug3("%s: received challenge: %s", __func__, challenge);
952 
953 	return (0);
954 }
955 
956 int
957 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
958 {
959 	Buffer m;
960 	int authok;
961 
962 	debug3("%s: entering", __func__);
963 	if (numresponses != 1)
964 		return (-1);
965 
966 	buffer_init(&m);
967 	buffer_put_cstring(&m, responses[0]);
968 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
969 
970 	mm_request_receive_expect(pmonitor->m_recvfd,
971 	    MONITOR_ANS_BSDAUTHRESPOND, &m);
972 
973 	authok = buffer_get_int(&m);
974 	buffer_free(&m);
975 
976 	return ((authok == 0) ? -1 : 0);
977 }
978 #endif
979 
980 #ifdef SKEY
981 int
982 mm_skey_query(void *ctx, char **name, char **infotxt,
983    u_int *numprompts, char ***prompts, u_int **echo_on)
984 {
985 	Buffer m;
986 	u_int success;
987 	char *challenge;
988 
989 	debug3("%s: entering", __func__);
990 
991 	buffer_init(&m);
992 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
993 
994 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
995 	    &m);
996 	success = buffer_get_int(&m);
997 	if (success == 0) {
998 		debug3("%s: no challenge", __func__);
999 		buffer_free(&m);
1000 		return (-1);
1001 	}
1002 
1003 	/* Get the challenge, and format the response */
1004 	challenge  = buffer_get_string(&m, NULL);
1005 	buffer_free(&m);
1006 
1007 	debug3("%s: received challenge: %s", __func__, challenge);
1008 
1009 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
1010 
1011 	xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
1012 	xfree(challenge);
1013 
1014 	return (0);
1015 }
1016 
1017 int
1018 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1019 {
1020 	Buffer m;
1021 	int authok;
1022 
1023 	debug3("%s: entering", __func__);
1024 	if (numresponses != 1)
1025 		return (-1);
1026 
1027 	buffer_init(&m);
1028 	buffer_put_cstring(&m, responses[0]);
1029 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1030 
1031 	mm_request_receive_expect(pmonitor->m_recvfd,
1032 	    MONITOR_ANS_SKEYRESPOND, &m);
1033 
1034 	authok = buffer_get_int(&m);
1035 	buffer_free(&m);
1036 
1037 	return ((authok == 0) ? -1 : 0);
1038 }
1039 #endif /* SKEY */
1040 
1041 void
1042 mm_ssh1_session_id(u_char session_id[16])
1043 {
1044 	Buffer m;
1045 	int i;
1046 
1047 	debug3("%s entering", __func__);
1048 
1049 	buffer_init(&m);
1050 	for (i = 0; i < 16; i++)
1051 		buffer_put_char(&m, session_id[i]);
1052 
1053 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1054 	buffer_free(&m);
1055 }
1056 
1057 int
1058 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1059 {
1060 	Buffer m;
1061 	Key *key;
1062 	u_char *blob;
1063 	u_int blen;
1064 	int allowed = 0, have_forced = 0;
1065 
1066 	debug3("%s entering", __func__);
1067 
1068 	buffer_init(&m);
1069 	buffer_put_bignum2(&m, client_n);
1070 
1071 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1072 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1073 
1074 	allowed = buffer_get_int(&m);
1075 
1076 	/* fake forced command */
1077 	auth_clear_options();
1078 	have_forced = buffer_get_int(&m);
1079 	forced_command = have_forced ? xstrdup("true") : NULL;
1080 
1081 	if (allowed && rkey != NULL) {
1082 		blob = buffer_get_string(&m, &blen);
1083 		if ((key = key_from_blob(blob, blen)) == NULL)
1084 			fatal("%s: key_from_blob failed", __func__);
1085 		*rkey = key;
1086 		xfree(blob);
1087 	}
1088 	mm_send_debug(&m);
1089 	buffer_free(&m);
1090 
1091 	return (allowed);
1092 }
1093 
1094 BIGNUM *
1095 mm_auth_rsa_generate_challenge(Key *key)
1096 {
1097 	Buffer m;
1098 	BIGNUM *challenge;
1099 	u_char *blob;
1100 	u_int blen;
1101 
1102 	debug3("%s entering", __func__);
1103 
1104 	if ((challenge = BN_new()) == NULL)
1105 		fatal("%s: BN_new failed", __func__);
1106 
1107 	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
1108 	if (key_to_blob(key, &blob, &blen) == 0)
1109 		fatal("%s: key_to_blob failed", __func__);
1110 	key->type = KEY_RSA1;
1111 
1112 	buffer_init(&m);
1113 	buffer_put_string(&m, blob, blen);
1114 	xfree(blob);
1115 
1116 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1117 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1118 
1119 	buffer_get_bignum2(&m, challenge);
1120 	buffer_free(&m);
1121 
1122 	return (challenge);
1123 }
1124 
1125 int
1126 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1127 {
1128 	Buffer m;
1129 	u_char *blob;
1130 	u_int blen;
1131 	int success = 0;
1132 
1133 	debug3("%s entering", __func__);
1134 
1135 	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
1136 	if (key_to_blob(key, &blob, &blen) == 0)
1137 		fatal("%s: key_to_blob failed", __func__);
1138 	key->type = KEY_RSA1;
1139 
1140 	buffer_init(&m);
1141 	buffer_put_string(&m, blob, blen);
1142 	buffer_put_string(&m, response, 16);
1143 	xfree(blob);
1144 
1145 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1146 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1147 
1148 	success = buffer_get_int(&m);
1149 	buffer_free(&m);
1150 
1151 	return (success);
1152 }
1153 
1154 #ifdef GSSAPI
1155 OM_uint32
1156 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1157 {
1158 	Buffer m;
1159 	OM_uint32 major;
1160 
1161 	/* Client doesn't get to see the context */
1162 	*ctx = NULL;
1163 
1164 	buffer_init(&m);
1165 	buffer_put_string(&m, goid->elements, goid->length);
1166 
1167 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1168 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1169 
1170 	major = buffer_get_int(&m);
1171 
1172 	buffer_free(&m);
1173 	return (major);
1174 }
1175 
1176 OM_uint32
1177 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1178     gss_buffer_desc *out, OM_uint32 *flags)
1179 {
1180 	Buffer m;
1181 	OM_uint32 major;
1182 	u_int len;
1183 
1184 	buffer_init(&m);
1185 	buffer_put_string(&m, in->value, in->length);
1186 
1187 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1188 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1189 
1190 	major = buffer_get_int(&m);
1191 	out->value = buffer_get_string(&m, &len);
1192 	out->length = len;
1193 	if (flags)
1194 		*flags = buffer_get_int(&m);
1195 
1196 	buffer_free(&m);
1197 
1198 	return (major);
1199 }
1200 
1201 OM_uint32
1202 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1203 {
1204 	Buffer m;
1205 	OM_uint32 major;
1206 
1207 	buffer_init(&m);
1208 	buffer_put_string(&m, gssbuf->value, gssbuf->length);
1209 	buffer_put_string(&m, gssmic->value, gssmic->length);
1210 
1211 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1212 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1213 	    &m);
1214 
1215 	major = buffer_get_int(&m);
1216 	buffer_free(&m);
1217 	return(major);
1218 }
1219 
1220 int
1221 mm_ssh_gssapi_userok(char *user)
1222 {
1223 	Buffer m;
1224 	int authenticated = 0;
1225 
1226 	buffer_init(&m);
1227 
1228 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1229 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1230 				  &m);
1231 
1232 	authenticated = buffer_get_int(&m);
1233 
1234 	buffer_free(&m);
1235 	debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1236 	return (authenticated);
1237 }
1238 #endif /* GSSAPI */
1239 
1240 #ifdef JPAKE
1241 void
1242 mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
1243     char **hash_scheme, char **salt)
1244 {
1245 	Buffer m;
1246 
1247 	debug3("%s entering", __func__);
1248 
1249 	buffer_init(&m);
1250 	mm_request_send(pmonitor->m_recvfd,
1251 	    MONITOR_REQ_JPAKE_GET_PWDATA, &m);
1252 
1253 	debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
1254 	mm_request_receive_expect(pmonitor->m_recvfd,
1255 	    MONITOR_ANS_JPAKE_GET_PWDATA, &m);
1256 
1257 	*hash_scheme = buffer_get_string(&m, NULL);
1258 	*salt = buffer_get_string(&m, NULL);
1259 
1260 	buffer_free(&m);
1261 }
1262 
1263 void
1264 mm_jpake_step1(struct modp_group *grp,
1265     u_char **id, u_int *id_len,
1266     BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
1267     u_char **priv1_proof, u_int *priv1_proof_len,
1268     u_char **priv2_proof, u_int *priv2_proof_len)
1269 {
1270 	Buffer m;
1271 
1272 	debug3("%s entering", __func__);
1273 
1274 	buffer_init(&m);
1275 	mm_request_send(pmonitor->m_recvfd,
1276 	    MONITOR_REQ_JPAKE_STEP1, &m);
1277 
1278 	debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
1279 	mm_request_receive_expect(pmonitor->m_recvfd,
1280 	    MONITOR_ANS_JPAKE_STEP1, &m);
1281 
1282 	if ((*priv1 = BN_new()) == NULL ||
1283 	    (*priv2 = BN_new()) == NULL ||
1284 	    (*g_priv1 = BN_new()) == NULL ||
1285 	    (*g_priv2 = BN_new()) == NULL)
1286 		fatal("%s: BN_new", __func__);
1287 
1288 	*id = buffer_get_string(&m, id_len);
1289 	/* priv1 and priv2 are, well, private */
1290 	buffer_get_bignum2(&m, *g_priv1);
1291 	buffer_get_bignum2(&m, *g_priv2);
1292 	*priv1_proof = buffer_get_string(&m, priv1_proof_len);
1293 	*priv2_proof = buffer_get_string(&m, priv2_proof_len);
1294 
1295 	buffer_free(&m);
1296 }
1297 
1298 void
1299 mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
1300     BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
1301     const u_char *theirid, u_int theirid_len,
1302     const u_char *myid, u_int myid_len,
1303     const u_char *theirpub1_proof, u_int theirpub1_proof_len,
1304     const u_char *theirpub2_proof, u_int theirpub2_proof_len,
1305     BIGNUM **newpub,
1306     u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
1307 {
1308 	Buffer m;
1309 
1310 	debug3("%s entering", __func__);
1311 
1312 	buffer_init(&m);
1313 	/* monitor already has all bignums except theirpub1, theirpub2 */
1314 	buffer_put_bignum2(&m, theirpub1);
1315 	buffer_put_bignum2(&m, theirpub2);
1316 	/* monitor already knows our id */
1317 	buffer_put_string(&m, theirid, theirid_len);
1318 	buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
1319 	buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
1320 
1321 	mm_request_send(pmonitor->m_recvfd,
1322 	    MONITOR_REQ_JPAKE_STEP2, &m);
1323 
1324 	debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
1325 	mm_request_receive_expect(pmonitor->m_recvfd,
1326 	    MONITOR_ANS_JPAKE_STEP2, &m);
1327 
1328 	if ((*newpub = BN_new()) == NULL)
1329 		fatal("%s: BN_new", __func__);
1330 
1331 	buffer_get_bignum2(&m, *newpub);
1332 	*newpub_exponent_proof = buffer_get_string(&m,
1333 	    newpub_exponent_proof_len);
1334 
1335 	buffer_free(&m);
1336 }
1337 
1338 void
1339 mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
1340     BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
1341     BIGNUM *theirpub1, BIGNUM *theirpub2,
1342     const u_char *my_id, u_int my_id_len,
1343     const u_char *their_id, u_int their_id_len,
1344     const u_char *sess_id, u_int sess_id_len,
1345     const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
1346     BIGNUM **k,
1347     u_char **confirm_hash, u_int *confirm_hash_len)
1348 {
1349 	Buffer m;
1350 
1351 	debug3("%s entering", __func__);
1352 
1353 	buffer_init(&m);
1354 	/* monitor already has all bignums except step2_val */
1355 	buffer_put_bignum2(&m, step2_val);
1356 	/* monitor already knows all the ids */
1357 	buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
1358 
1359 	mm_request_send(pmonitor->m_recvfd,
1360 	    MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
1361 
1362 	debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
1363 	mm_request_receive_expect(pmonitor->m_recvfd,
1364 	    MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
1365 
1366 	/* 'k' is sensitive and stays in the monitor */
1367 	*confirm_hash = buffer_get_string(&m, confirm_hash_len);
1368 
1369 	buffer_free(&m);
1370 }
1371 
1372 int
1373 mm_jpake_check_confirm(const BIGNUM *k,
1374     const u_char *peer_id, u_int peer_id_len,
1375     const u_char *sess_id, u_int sess_id_len,
1376     const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
1377 {
1378 	Buffer m;
1379 	int success = 0;
1380 
1381 	debug3("%s entering", __func__);
1382 
1383 	buffer_init(&m);
1384 	/* k is dummy in slave, ignored */
1385 	/* monitor knows all the ids */
1386 	buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
1387 	mm_request_send(pmonitor->m_recvfd,
1388 	    MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
1389 
1390 	debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
1391 	mm_request_receive_expect(pmonitor->m_recvfd,
1392 	    MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
1393 
1394 	success = buffer_get_int(&m);
1395 	buffer_free(&m);
1396 
1397 	debug3("%s: success = %d", __func__, success);
1398 	return success;
1399 }
1400 #endif /* JPAKE */
1401 
1402 #ifdef KRB4
1403 int
1404 mm_auth_krb4(Authctxt *authctxt, void *_auth, char **client, void *_reply)
1405 {
1406 	KTEXT auth, reply;
1407  	Buffer m;
1408 	u_int rlen;
1409 	int success = 0;
1410 	char *p;
1411 
1412 	debug3("%s entering", __func__);
1413 	auth = _auth;
1414 	reply = _reply;
1415 
1416 	buffer_init(&m);
1417 	buffer_put_string(&m, auth->dat, auth->length);
1418 
1419 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KRB4, &m);
1420 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KRB4, &m);
1421 
1422 	success = buffer_get_int(&m);
1423 	if (success) {
1424 		*client = buffer_get_string(&m, NULL);
1425 		p = buffer_get_string(&m, &rlen);
1426 		if (rlen >= MAX_KTXT_LEN)
1427 			fatal("%s: reply from monitor too large", __func__);
1428 		reply->length = rlen;
1429 		memcpy(reply->dat, p, rlen);
1430 		memset(p, 0, rlen);
1431 		xfree(p);
1432 	}
1433 	buffer_free(&m);
1434 	return (success);
1435 }
1436 #endif
1437 
1438 #ifdef KRB5
1439 int
1440 mm_auth_krb5(void *ctx, void *argp, char **userp, void *resp)
1441 {
1442 	krb5_data *tkt, *reply;
1443 	Buffer m;
1444 	int success;
1445 
1446 	debug3("%s entering", __func__);
1447 	tkt = (krb5_data *) argp;
1448 	reply = (krb5_data *) resp;
1449 
1450 	buffer_init(&m);
1451 	buffer_put_string(&m, tkt->data, tkt->length);
1452 
1453 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KRB5, &m);
1454 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KRB5, &m);
1455 
1456 	success = buffer_get_int(&m);
1457 	if (success) {
1458 		u_int len;
1459 
1460 		*userp = buffer_get_string(&m, NULL);
1461 		reply->data = buffer_get_string(&m, &len);
1462 		reply->length = len;
1463 	} else {
1464 		memset(reply, 0, sizeof(*reply));
1465 		*userp = NULL;
1466 	}
1467 
1468 	buffer_free(&m);
1469 	return (success);
1470 }
1471 #endif
1472