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