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