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