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