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