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