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