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