xref: /netbsd-src/crypto/external/bsd/openssh/dist/monitor_wrap.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: monitor_wrap.c,v 1.8 2013/11/08 19:18:25 christos Exp $	*/
2 /* $OpenBSD: monitor_wrap.c,v 1.76.2.1 2013/11/08 00:25:26 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.8 2013/11/08 19:18:25 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 	free(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 		free(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 	free(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 	free(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 = xcalloc(1, 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, &enc->iv_len);
491 
492 	if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
493 		fatal("%s: bad cipher name %s or pointer %p", __func__,
494 		    enc->name, enc->cipher);
495 
496 	/* Mac structure */
497 	if (cipher_authlen(enc->cipher) == 0) {
498 		mac->name = buffer_get_string(&b, NULL);
499 		if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
500 			fatal("%s: can not setup mac %s", __func__, mac->name);
501 		mac->enabled = buffer_get_int(&b);
502 		mac->key = buffer_get_string(&b, &len);
503 		if (len > mac->key_len)
504 			fatal("%s: bad mac key length: %u > %d", __func__, len,
505 			    mac->key_len);
506 		mac->key_len = len;
507 	}
508 
509 	/* Comp structure */
510 	comp->type = buffer_get_int(&b);
511 	comp->enabled = buffer_get_int(&b);
512 	comp->name = buffer_get_string(&b, NULL);
513 
514 	len = buffer_len(&b);
515 	if (len != 0)
516 		error("newkeys_from_blob: remaining bytes in blob %u", len);
517 	buffer_free(&b);
518 	return (newkey);
519 }
520 
521 int
522 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
523 {
524 	Buffer b;
525 	int len;
526 	Enc *enc;
527 	Mac *mac;
528 	Comp *comp;
529 	Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
530 
531 	debug3("%s: converting %p", __func__, newkey);
532 
533 	if (newkey == NULL) {
534 		error("%s: newkey == NULL", __func__);
535 		return 0;
536 	}
537 	enc = &newkey->enc;
538 	mac = &newkey->mac;
539 	comp = &newkey->comp;
540 
541 	buffer_init(&b);
542 	/* Enc structure */
543 	buffer_put_cstring(&b, enc->name);
544 	/* The cipher struct is constant and shared, you export pointer */
545 	buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
546 	buffer_put_int(&b, enc->enabled);
547 	buffer_put_int(&b, enc->block_size);
548 	buffer_put_string(&b, enc->key, enc->key_len);
549 	packet_get_keyiv(mode, enc->iv, enc->iv_len);
550 	buffer_put_string(&b, enc->iv, enc->iv_len);
551 
552 	/* Mac structure */
553 	if (cipher_authlen(enc->cipher) == 0) {
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 
559 	/* Comp structure */
560 	buffer_put_int(&b, comp->type);
561 	buffer_put_int(&b, comp->enabled);
562 	buffer_put_cstring(&b, comp->name);
563 
564 	len = buffer_len(&b);
565 	if (lenp != NULL)
566 		*lenp = len;
567 	if (blobp != NULL) {
568 		*blobp = xmalloc(len);
569 		memcpy(*blobp, buffer_ptr(&b), len);
570 	}
571 	memset(buffer_ptr(&b), 0, len);
572 	buffer_free(&b);
573 	return len;
574 }
575 
576 static void
577 mm_send_kex(Buffer *m, Kex *kex)
578 {
579 	buffer_put_string(m, kex->session_id, kex->session_id_len);
580 	buffer_put_int(m, kex->we_need);
581 	buffer_put_int(m, kex->hostkey_type);
582 	buffer_put_int(m, kex->kex_type);
583 	buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
584 	buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
585 	buffer_put_int(m, kex->flags);
586 	buffer_put_cstring(m, kex->client_version_string);
587 	buffer_put_cstring(m, kex->server_version_string);
588 }
589 
590 void
591 mm_send_keystate(struct monitor *monitor)
592 {
593 	Buffer m, *input, *output;
594 	u_char *blob, *p;
595 	u_int bloblen, plen;
596 	u_int32_t seqnr, packets;
597 	u_int64_t blocks, bytes;
598 
599 	buffer_init(&m);
600 
601 	if (!compat20) {
602 		u_char iv[24];
603 		u_char *key;
604 		u_int ivlen, keylen;
605 
606 		buffer_put_int(&m, packet_get_protocol_flags());
607 
608 		buffer_put_int(&m, packet_get_ssh1_cipher());
609 
610 		debug3("%s: Sending ssh1 KEY+IV", __func__);
611 		keylen = packet_get_encryption_key(NULL);
612 		key = xmalloc(keylen+1);	/* add 1 if keylen == 0 */
613 		keylen = packet_get_encryption_key(key);
614 		buffer_put_string(&m, key, keylen);
615 		memset(key, 0, keylen);
616 		free(key);
617 
618 		ivlen = packet_get_keyiv_len(MODE_OUT);
619 		packet_get_keyiv(MODE_OUT, iv, ivlen);
620 		buffer_put_string(&m, iv, ivlen);
621 		ivlen = packet_get_keyiv_len(MODE_IN);
622 		packet_get_keyiv(MODE_IN, iv, ivlen);
623 		buffer_put_string(&m, iv, ivlen);
624 		goto skip;
625 	} else {
626 		/* Kex for rekeying */
627 		mm_send_kex(&m, *monitor->m_pkex);
628 	}
629 
630 	debug3("%s: Sending new keys: %p %p",
631 	    __func__, packet_get_newkeys(MODE_OUT),
632 	    packet_get_newkeys(MODE_IN));
633 
634 	/* Keys from Kex */
635 	if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
636 		fatal("%s: conversion of newkeys failed", __func__);
637 
638 	buffer_put_string(&m, blob, bloblen);
639 	free(blob);
640 
641 	if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
642 		fatal("%s: conversion of newkeys failed", __func__);
643 
644 	buffer_put_string(&m, blob, bloblen);
645 	free(blob);
646 
647 	packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
648 	buffer_put_int(&m, seqnr);
649 	buffer_put_int64(&m, blocks);
650 	buffer_put_int(&m, packets);
651 	buffer_put_int64(&m, bytes);
652 	packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
653 	buffer_put_int(&m, seqnr);
654 	buffer_put_int64(&m, blocks);
655 	buffer_put_int(&m, packets);
656 	buffer_put_int64(&m, bytes);
657 
658 	debug3("%s: New keys have been sent", __func__);
659  skip:
660 	/* More key context */
661 	plen = packet_get_keycontext(MODE_OUT, NULL);
662 	p = xmalloc(plen+1);
663 	packet_get_keycontext(MODE_OUT, p);
664 	buffer_put_string(&m, p, plen);
665 	free(p);
666 
667 	plen = packet_get_keycontext(MODE_IN, NULL);
668 	p = xmalloc(plen+1);
669 	packet_get_keycontext(MODE_IN, p);
670 	buffer_put_string(&m, p, plen);
671 	free(p);
672 
673 	/* Compression state */
674 	debug3("%s: Sending compression state", __func__);
675 	buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
676 	buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
677 
678 	/* Network I/O buffers */
679 	input = (Buffer *)packet_get_input();
680 	output = (Buffer *)packet_get_output();
681 	buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
682 	buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
683 
684 	/* Roaming */
685 	if (compat20) {
686 		buffer_put_int64(&m, get_sent_bytes());
687 		buffer_put_int64(&m, get_recv_bytes());
688 	}
689 
690 	mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
691 	debug3("%s: Finished sending state", __func__);
692 
693 	buffer_free(&m);
694 }
695 
696 int
697 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
698 {
699 	Buffer m;
700 	char *p, *msg;
701 	int success = 0, tmp1 = -1, tmp2 = -1;
702 
703 	/* Kludge: ensure there are fds free to receive the pty/tty */
704 	if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
705 	    (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
706 		error("%s: cannot allocate fds for pty", __func__);
707 		if (tmp1 > 0)
708 			close(tmp1);
709 		if (tmp2 > 0)
710 			close(tmp2);
711 		return 0;
712 	}
713 	close(tmp1);
714 	close(tmp2);
715 
716 	buffer_init(&m);
717 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
718 
719 	debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
720 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
721 
722 	success = buffer_get_int(&m);
723 	if (success == 0) {
724 		debug3("%s: pty alloc failed", __func__);
725 		buffer_free(&m);
726 		return (0);
727 	}
728 	p = buffer_get_string(&m, NULL);
729 	msg = buffer_get_string(&m, NULL);
730 	buffer_free(&m);
731 
732 	strlcpy(namebuf, p, namebuflen); /* Possible truncation */
733 	free(p);
734 
735 	buffer_append(&loginmsg, msg, strlen(msg));
736 	free(msg);
737 
738 	if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
739 	    (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
740 		fatal("%s: receive fds failed", __func__);
741 
742 	/* Success */
743 	return (1);
744 }
745 
746 void
747 mm_session_pty_cleanup2(Session *s)
748 {
749 	Buffer m;
750 
751 	if (s->ttyfd == -1)
752 		return;
753 	buffer_init(&m);
754 	buffer_put_cstring(&m, s->tty);
755 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
756 	buffer_free(&m);
757 
758 	/* closed dup'ed master */
759 	if (s->ptymaster != -1 && close(s->ptymaster) < 0)
760 		error("close(s->ptymaster/%d): %s",
761 		    s->ptymaster, strerror(errno));
762 
763 	/* unlink pty from session */
764 	s->ttyfd = -1;
765 }
766 
767 #ifdef USE_PAM
768 void
769 mm_start_pam(Authctxt *authctxt)
770 {
771 	Buffer m;
772 
773 	debug3("%s entering", __func__);
774 	if (!options.use_pam)
775 		fatal("UsePAM=no, but ended up in %s anyway", __func__);
776 
777 	buffer_init(&m);
778 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
779 
780 	buffer_free(&m);
781 }
782 
783 u_int
784 mm_do_pam_account(void)
785 {
786 	Buffer m;
787 	u_int ret;
788 	char *msg;
789 
790 	debug3("%s entering", __func__);
791 	if (!options.use_pam)
792 		fatal("UsePAM=no, but ended up in %s anyway", __func__);
793 
794 	buffer_init(&m);
795 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
796 
797 	mm_request_receive_expect(pmonitor->m_recvfd,
798 	    MONITOR_ANS_PAM_ACCOUNT, &m);
799 	ret = buffer_get_int(&m);
800 	msg = buffer_get_string(&m, NULL);
801 	buffer_append(&loginmsg, msg, strlen(msg));
802 	free(msg);
803 
804 	buffer_free(&m);
805 
806 	debug3("%s returning %d", __func__, ret);
807 
808 	return (ret);
809 }
810 
811 void *
812 mm_sshpam_init_ctx(Authctxt *authctxt)
813 {
814 	Buffer m;
815 	int success;
816 
817 	debug3("%s", __func__);
818 	buffer_init(&m);
819 	buffer_put_cstring(&m, authctxt->user);
820 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
821 	debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
822 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
823 	success = buffer_get_int(&m);
824 	if (success == 0) {
825 		debug3("%s: pam_init_ctx failed", __func__);
826 		buffer_free(&m);
827 		return (NULL);
828 	}
829 	buffer_free(&m);
830 	return (authctxt);
831 }
832 
833 int
834 mm_sshpam_query(void *ctx, char **name, char **info,
835     u_int *num, char ***prompts, u_int **echo_on)
836 {
837 	Buffer m;
838 	u_int i;
839 	int ret;
840 
841 	debug3("%s", __func__);
842 	buffer_init(&m);
843 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
844 	debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
845 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
846 	ret = buffer_get_int(&m);
847 	debug3("%s: pam_query returned %d", __func__, ret);
848 	*name = buffer_get_string(&m, NULL);
849 	*info = buffer_get_string(&m, NULL);
850 	*num = buffer_get_int(&m);
851 	if (*num > PAM_MAX_NUM_MSG)
852 		fatal("%s: received %u PAM messages, expected <= %u",
853 		    __func__, *num, PAM_MAX_NUM_MSG);
854 	*prompts = xcalloc((*num + 1), sizeof(char *));
855 	*echo_on = xcalloc((*num + 1), sizeof(u_int));
856 	for (i = 0; i < *num; ++i) {
857 		(*prompts)[i] = buffer_get_string(&m, NULL);
858 		(*echo_on)[i] = buffer_get_int(&m);
859 	}
860 	buffer_free(&m);
861 	return (ret);
862 }
863 
864 int
865 mm_sshpam_respond(void *ctx, u_int num, char **resp)
866 {
867 	Buffer m;
868 	u_int i;
869 	int ret;
870 
871 	debug3("%s", __func__);
872 	buffer_init(&m);
873 	buffer_put_int(&m, num);
874 	for (i = 0; i < num; ++i)
875 		buffer_put_cstring(&m, resp[i]);
876 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
877 	debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
878 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
879 	ret = buffer_get_int(&m);
880 	debug3("%s: pam_respond returned %d", __func__, ret);
881 	buffer_free(&m);
882 	return (ret);
883 }
884 
885 void
886 mm_sshpam_free_ctx(void *ctxtp)
887 {
888 	Buffer m;
889 
890 	debug3("%s", __func__);
891 	buffer_init(&m);
892 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
893 	debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
894 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
895 	buffer_free(&m);
896 }
897 #endif /* USE_PAM */
898 
899 /* Request process termination */
900 
901 void
902 mm_terminate(void)
903 {
904 	Buffer m;
905 
906 	buffer_init(&m);
907 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
908 	buffer_free(&m);
909 }
910 
911 int
912 mm_ssh1_session_key(BIGNUM *num)
913 {
914 	int rsafail;
915 	Buffer m;
916 
917 	buffer_init(&m);
918 	buffer_put_bignum2(&m, num);
919 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
920 
921 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
922 
923 	rsafail = buffer_get_int(&m);
924 	buffer_get_bignum2(&m, num);
925 
926 	buffer_free(&m);
927 
928 	return (rsafail);
929 }
930 
931 #if defined(BSD_AUTH) || defined(SKEY)
932 static void
933 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
934     char ***prompts, u_int **echo_on)
935 {
936 	*name = xstrdup("");
937 	*infotxt = xstrdup("");
938 	*numprompts = 1;
939 	*prompts = xcalloc(*numprompts, sizeof(char *));
940 	*echo_on = xcalloc(*numprompts, sizeof(u_int));
941 	(*echo_on)[0] = 0;
942 }
943 #endif
944 
945 #ifdef BSD_AUTH
946 int
947 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
948    u_int *numprompts, char ***prompts, u_int **echo_on)
949 {
950 	Buffer m;
951 	u_int success;
952 	char *challenge;
953 
954 	debug3("%s: entering", __func__);
955 
956 	buffer_init(&m);
957 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
958 
959 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
960 	    &m);
961 	success = buffer_get_int(&m);
962 	if (success == 0) {
963 		debug3("%s: no challenge", __func__);
964 		buffer_free(&m);
965 		return (-1);
966 	}
967 
968 	/* Get the challenge, and format the response */
969 	challenge  = buffer_get_string(&m, NULL);
970 	buffer_free(&m);
971 
972 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
973 	(*prompts)[0] = challenge;
974 
975 	debug3("%s: received challenge: %s", __func__, challenge);
976 
977 	return (0);
978 }
979 
980 int
981 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
982 {
983 	Buffer m;
984 	int authok;
985 
986 	debug3("%s: entering", __func__);
987 	if (numresponses != 1)
988 		return (-1);
989 
990 	buffer_init(&m);
991 	buffer_put_cstring(&m, responses[0]);
992 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
993 
994 	mm_request_receive_expect(pmonitor->m_recvfd,
995 	    MONITOR_ANS_BSDAUTHRESPOND, &m);
996 
997 	authok = buffer_get_int(&m);
998 	buffer_free(&m);
999 
1000 	return ((authok == 0) ? -1 : 0);
1001 }
1002 #endif
1003 
1004 #ifdef SKEY
1005 int
1006 mm_skey_query(void *ctx, char **name, char **infotxt,
1007    u_int *numprompts, char ***prompts, u_int **echo_on)
1008 {
1009 	Buffer m;
1010 	u_int success;
1011 	char *challenge;
1012 
1013 	debug3("%s: entering", __func__);
1014 
1015 	buffer_init(&m);
1016 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
1017 
1018 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
1019 	    &m);
1020 	success = buffer_get_int(&m);
1021 	if (success == 0) {
1022 		debug3("%s: no challenge", __func__);
1023 		buffer_free(&m);
1024 		return (-1);
1025 	}
1026 
1027 	/* Get the challenge, and format the response */
1028 	challenge  = buffer_get_string(&m, NULL);
1029 	buffer_free(&m);
1030 
1031 	debug3("%s: received challenge: %s", __func__, challenge);
1032 
1033 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
1034 
1035 	xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
1036 	free(challenge);
1037 
1038 	return (0);
1039 }
1040 
1041 int
1042 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1043 {
1044 	Buffer m;
1045 	int authok;
1046 
1047 	debug3("%s: entering", __func__);
1048 	if (numresponses != 1)
1049 		return (-1);
1050 
1051 	buffer_init(&m);
1052 	buffer_put_cstring(&m, responses[0]);
1053 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1054 
1055 	mm_request_receive_expect(pmonitor->m_recvfd,
1056 	    MONITOR_ANS_SKEYRESPOND, &m);
1057 
1058 	authok = buffer_get_int(&m);
1059 	buffer_free(&m);
1060 
1061 	return ((authok == 0) ? -1 : 0);
1062 }
1063 #endif /* SKEY */
1064 
1065 void
1066 mm_ssh1_session_id(u_char session_id[16])
1067 {
1068 	Buffer m;
1069 	int i;
1070 
1071 	debug3("%s entering", __func__);
1072 
1073 	buffer_init(&m);
1074 	for (i = 0; i < 16; i++)
1075 		buffer_put_char(&m, session_id[i]);
1076 
1077 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1078 	buffer_free(&m);
1079 }
1080 
1081 int
1082 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1083 {
1084 	Buffer m;
1085 	Key *key;
1086 	u_char *blob;
1087 	u_int blen;
1088 	int allowed = 0, have_forced = 0;
1089 
1090 	debug3("%s entering", __func__);
1091 
1092 	buffer_init(&m);
1093 	buffer_put_bignum2(&m, client_n);
1094 
1095 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1096 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1097 
1098 	allowed = buffer_get_int(&m);
1099 
1100 	/* fake forced command */
1101 	auth_clear_options();
1102 	have_forced = buffer_get_int(&m);
1103 	forced_command = have_forced ? xstrdup("true") : NULL;
1104 
1105 	if (allowed && rkey != NULL) {
1106 		blob = buffer_get_string(&m, &blen);
1107 		if ((key = key_from_blob(blob, blen)) == NULL)
1108 			fatal("%s: key_from_blob failed", __func__);
1109 		*rkey = key;
1110 		free(blob);
1111 	}
1112 	buffer_free(&m);
1113 
1114 	return (allowed);
1115 }
1116 
1117 BIGNUM *
1118 mm_auth_rsa_generate_challenge(Key *key)
1119 {
1120 	Buffer m;
1121 	BIGNUM *challenge;
1122 	u_char *blob;
1123 	u_int blen;
1124 
1125 	debug3("%s entering", __func__);
1126 
1127 	if ((challenge = BN_new()) == NULL)
1128 		fatal("%s: BN_new failed", __func__);
1129 
1130 	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
1131 	if (key_to_blob(key, &blob, &blen) == 0)
1132 		fatal("%s: key_to_blob failed", __func__);
1133 	key->type = KEY_RSA1;
1134 
1135 	buffer_init(&m);
1136 	buffer_put_string(&m, blob, blen);
1137 	free(blob);
1138 
1139 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1140 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1141 
1142 	buffer_get_bignum2(&m, challenge);
1143 	buffer_free(&m);
1144 
1145 	return (challenge);
1146 }
1147 
1148 int
1149 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1150 {
1151 	Buffer m;
1152 	u_char *blob;
1153 	u_int blen;
1154 	int success = 0;
1155 
1156 	debug3("%s entering", __func__);
1157 
1158 	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
1159 	if (key_to_blob(key, &blob, &blen) == 0)
1160 		fatal("%s: key_to_blob failed", __func__);
1161 	key->type = KEY_RSA1;
1162 
1163 	buffer_init(&m);
1164 	buffer_put_string(&m, blob, blen);
1165 	buffer_put_string(&m, response, 16);
1166 	free(blob);
1167 
1168 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1169 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1170 
1171 	success = buffer_get_int(&m);
1172 	buffer_free(&m);
1173 
1174 	return (success);
1175 }
1176 
1177 #ifdef GSSAPI
1178 OM_uint32
1179 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1180 {
1181 	Buffer m;
1182 	OM_uint32 major;
1183 
1184 	/* Client doesn't get to see the context */
1185 	*ctx = NULL;
1186 
1187 	buffer_init(&m);
1188 	buffer_put_string(&m, goid->elements, goid->length);
1189 
1190 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1191 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1192 
1193 	major = buffer_get_int(&m);
1194 
1195 	buffer_free(&m);
1196 	return (major);
1197 }
1198 
1199 OM_uint32
1200 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1201     gss_buffer_desc *out, OM_uint32 *flags)
1202 {
1203 	Buffer m;
1204 	OM_uint32 major;
1205 	u_int len;
1206 
1207 	buffer_init(&m);
1208 	buffer_put_string(&m, in->value, in->length);
1209 
1210 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1211 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1212 
1213 	major = buffer_get_int(&m);
1214 	out->value = buffer_get_string(&m, &len);
1215 	out->length = len;
1216 	if (flags)
1217 		*flags = buffer_get_int(&m);
1218 
1219 	buffer_free(&m);
1220 
1221 	return (major);
1222 }
1223 
1224 OM_uint32
1225 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1226 {
1227 	Buffer m;
1228 	OM_uint32 major;
1229 
1230 	buffer_init(&m);
1231 	buffer_put_string(&m, gssbuf->value, gssbuf->length);
1232 	buffer_put_string(&m, gssmic->value, gssmic->length);
1233 
1234 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1235 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1236 	    &m);
1237 
1238 	major = buffer_get_int(&m);
1239 	buffer_free(&m);
1240 	return(major);
1241 }
1242 
1243 int
1244 mm_ssh_gssapi_userok(char *user)
1245 {
1246 	Buffer m;
1247 	int authenticated = 0;
1248 
1249 	buffer_init(&m);
1250 
1251 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1252 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1253 				  &m);
1254 
1255 	authenticated = buffer_get_int(&m);
1256 
1257 	buffer_free(&m);
1258 	debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1259 	return (authenticated);
1260 }
1261 #endif /* GSSAPI */
1262 
1263 #ifdef JPAKE
1264 void
1265 mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
1266     char **hash_scheme, char **salt)
1267 {
1268 	Buffer m;
1269 
1270 	debug3("%s entering", __func__);
1271 
1272 	buffer_init(&m);
1273 	mm_request_send(pmonitor->m_recvfd,
1274 	    MONITOR_REQ_JPAKE_GET_PWDATA, &m);
1275 
1276 	debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
1277 	mm_request_receive_expect(pmonitor->m_recvfd,
1278 	    MONITOR_ANS_JPAKE_GET_PWDATA, &m);
1279 
1280 	*hash_scheme = buffer_get_string(&m, NULL);
1281 	*salt = buffer_get_string(&m, NULL);
1282 
1283 	buffer_free(&m);
1284 }
1285 
1286 void
1287 mm_jpake_step1(struct modp_group *grp,
1288     u_char **id, u_int *id_len,
1289     BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
1290     u_char **priv1_proof, u_int *priv1_proof_len,
1291     u_char **priv2_proof, u_int *priv2_proof_len)
1292 {
1293 	Buffer m;
1294 
1295 	debug3("%s entering", __func__);
1296 
1297 	buffer_init(&m);
1298 	mm_request_send(pmonitor->m_recvfd,
1299 	    MONITOR_REQ_JPAKE_STEP1, &m);
1300 
1301 	debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
1302 	mm_request_receive_expect(pmonitor->m_recvfd,
1303 	    MONITOR_ANS_JPAKE_STEP1, &m);
1304 
1305 	if ((*priv1 = BN_new()) == NULL ||
1306 	    (*priv2 = BN_new()) == NULL ||
1307 	    (*g_priv1 = BN_new()) == NULL ||
1308 	    (*g_priv2 = BN_new()) == NULL)
1309 		fatal("%s: BN_new", __func__);
1310 
1311 	*id = buffer_get_string(&m, id_len);
1312 	/* priv1 and priv2 are, well, private */
1313 	buffer_get_bignum2(&m, *g_priv1);
1314 	buffer_get_bignum2(&m, *g_priv2);
1315 	*priv1_proof = buffer_get_string(&m, priv1_proof_len);
1316 	*priv2_proof = buffer_get_string(&m, priv2_proof_len);
1317 
1318 	buffer_free(&m);
1319 }
1320 
1321 void
1322 mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
1323     BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
1324     const u_char *theirid, u_int theirid_len,
1325     const u_char *myid, u_int myid_len,
1326     const u_char *theirpub1_proof, u_int theirpub1_proof_len,
1327     const u_char *theirpub2_proof, u_int theirpub2_proof_len,
1328     BIGNUM **newpub,
1329     u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
1330 {
1331 	Buffer m;
1332 
1333 	debug3("%s entering", __func__);
1334 
1335 	buffer_init(&m);
1336 	/* monitor already has all bignums except theirpub1, theirpub2 */
1337 	buffer_put_bignum2(&m, theirpub1);
1338 	buffer_put_bignum2(&m, theirpub2);
1339 	/* monitor already knows our id */
1340 	buffer_put_string(&m, theirid, theirid_len);
1341 	buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
1342 	buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
1343 
1344 	mm_request_send(pmonitor->m_recvfd,
1345 	    MONITOR_REQ_JPAKE_STEP2, &m);
1346 
1347 	debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
1348 	mm_request_receive_expect(pmonitor->m_recvfd,
1349 	    MONITOR_ANS_JPAKE_STEP2, &m);
1350 
1351 	if ((*newpub = BN_new()) == NULL)
1352 		fatal("%s: BN_new", __func__);
1353 
1354 	buffer_get_bignum2(&m, *newpub);
1355 	*newpub_exponent_proof = buffer_get_string(&m,
1356 	    newpub_exponent_proof_len);
1357 
1358 	buffer_free(&m);
1359 }
1360 
1361 void
1362 mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
1363     BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
1364     BIGNUM *theirpub1, BIGNUM *theirpub2,
1365     const u_char *my_id, u_int my_id_len,
1366     const u_char *their_id, u_int their_id_len,
1367     const u_char *sess_id, u_int sess_id_len,
1368     const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
1369     BIGNUM **k,
1370     u_char **confirm_hash, u_int *confirm_hash_len)
1371 {
1372 	Buffer m;
1373 
1374 	debug3("%s entering", __func__);
1375 
1376 	buffer_init(&m);
1377 	/* monitor already has all bignums except step2_val */
1378 	buffer_put_bignum2(&m, step2_val);
1379 	/* monitor already knows all the ids */
1380 	buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
1381 
1382 	mm_request_send(pmonitor->m_recvfd,
1383 	    MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
1384 
1385 	debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
1386 	mm_request_receive_expect(pmonitor->m_recvfd,
1387 	    MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
1388 
1389 	/* 'k' is sensitive and stays in the monitor */
1390 	*confirm_hash = buffer_get_string(&m, confirm_hash_len);
1391 
1392 	buffer_free(&m);
1393 }
1394 
1395 int
1396 mm_jpake_check_confirm(const BIGNUM *k,
1397     const u_char *peer_id, u_int peer_id_len,
1398     const u_char *sess_id, u_int sess_id_len,
1399     const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
1400 {
1401 	Buffer m;
1402 	int success = 0;
1403 
1404 	debug3("%s entering", __func__);
1405 
1406 	buffer_init(&m);
1407 	/* k is dummy in slave, ignored */
1408 	/* monitor knows all the ids */
1409 	buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
1410 	mm_request_send(pmonitor->m_recvfd,
1411 	    MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
1412 
1413 	debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
1414 	mm_request_receive_expect(pmonitor->m_recvfd,
1415 	    MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
1416 
1417 	success = buffer_get_int(&m);
1418 	buffer_free(&m);
1419 
1420 	debug3("%s: success = %d", __func__, success);
1421 	return success;
1422 }
1423 #endif /* JPAKE */
1424 
1425 #ifdef KRB4
1426 int
1427 mm_auth_krb4(Authctxt *authctxt, void *_auth, char **client, void *_reply)
1428 {
1429 	KTEXT auth, reply;
1430  	Buffer m;
1431 	u_int rlen;
1432 	int success = 0;
1433 	char *p;
1434 
1435 	debug3("%s entering", __func__);
1436 	auth = _auth;
1437 	reply = _reply;
1438 
1439 	buffer_init(&m);
1440 	buffer_put_string(&m, auth->dat, auth->length);
1441 
1442 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KRB4, &m);
1443 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KRB4, &m);
1444 
1445 	success = buffer_get_int(&m);
1446 	if (success) {
1447 		*client = buffer_get_string(&m, NULL);
1448 		p = buffer_get_string(&m, &rlen);
1449 		if (rlen >= MAX_KTXT_LEN)
1450 			fatal("%s: reply from monitor too large", __func__);
1451 		reply->length = rlen;
1452 		memcpy(reply->dat, p, rlen);
1453 		memset(p, 0, rlen);
1454 		free(p);
1455 	}
1456 	buffer_free(&m);
1457 	return (success);
1458 }
1459 #endif
1460 
1461 #ifdef KRB5
1462 int
1463 mm_auth_krb5(void *ctx, void *argp, char **userp, void *resp)
1464 {
1465 	krb5_data *tkt, *reply;
1466 	Buffer m;
1467 	int success;
1468 
1469 	debug3("%s entering", __func__);
1470 	tkt = (krb5_data *) argp;
1471 	reply = (krb5_data *) resp;
1472 
1473 	buffer_init(&m);
1474 	buffer_put_string(&m, tkt->data, tkt->length);
1475 
1476 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KRB5, &m);
1477 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KRB5, &m);
1478 
1479 	success = buffer_get_int(&m);
1480 	if (success) {
1481 		u_int len;
1482 
1483 		*userp = buffer_get_string(&m, NULL);
1484 		reply->data = buffer_get_string(&m, &len);
1485 		reply->length = len;
1486 	} else {
1487 		memset(reply, 0, sizeof(*reply));
1488 		*userp = NULL;
1489 	}
1490 
1491 	buffer_free(&m);
1492 	return (success);
1493 }
1494 #endif
1495