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