xref: /openbsd-src/usr.bin/ssh/monitor_wrap.c (revision d874cce4b1d9fe6b41c9e4f2117a77d8a4a37b92)
1 /* $OpenBSD: monitor_wrap.c,v 1.62 2008/05/08 12:21:16 djm Exp $ */
2 /*
3  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4  * Copyright 2002 Markus Friedl <markus@openbsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/uio.h>
30 #include <sys/queue.h>
31 
32 #include <openssl/bn.h>
33 #include <openssl/dh.h>
34 
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;
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);
614 	buffer_put_int(&m, seqnr);
615 	buffer_put_int64(&m, blocks);
616 	buffer_put_int(&m, packets);
617 	packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
618 	buffer_put_int(&m, seqnr);
619 	buffer_put_int64(&m, blocks);
620 	buffer_put_int(&m, packets);
621 
622 	debug3("%s: New keys have been sent", __func__);
623  skip:
624 	/* More key context */
625 	plen = packet_get_keycontext(MODE_OUT, NULL);
626 	p = xmalloc(plen+1);
627 	packet_get_keycontext(MODE_OUT, p);
628 	buffer_put_string(&m, p, plen);
629 	xfree(p);
630 
631 	plen = packet_get_keycontext(MODE_IN, NULL);
632 	p = xmalloc(plen+1);
633 	packet_get_keycontext(MODE_IN, p);
634 	buffer_put_string(&m, p, plen);
635 	xfree(p);
636 
637 	/* Compression state */
638 	debug3("%s: Sending compression state", __func__);
639 	buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
640 	buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
641 
642 	/* Network I/O buffers */
643 	buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
644 	buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
645 
646 	mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
647 	debug3("%s: Finished sending state", __func__);
648 
649 	buffer_free(&m);
650 }
651 
652 int
653 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
654 {
655 	Buffer m;
656 	char *p, *msg;
657 	int success = 0, tmp1 = -1, tmp2 = -1;
658 
659 	/* Kludge: ensure there are fds free to receive the pty/tty */
660 	if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
661 	    (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
662 		error("%s: cannot allocate fds for pty", __func__);
663 		if (tmp1 > 0)
664 			close(tmp1);
665 		if (tmp2 > 0)
666 			close(tmp2);
667 		return 0;
668 	}
669 	close(tmp1);
670 	close(tmp2);
671 
672 	buffer_init(&m);
673 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
674 
675 	debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
676 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
677 
678 	success = buffer_get_int(&m);
679 	if (success == 0) {
680 		debug3("%s: pty alloc failed", __func__);
681 		buffer_free(&m);
682 		return (0);
683 	}
684 	p = buffer_get_string(&m, NULL);
685 	msg = buffer_get_string(&m, NULL);
686 	buffer_free(&m);
687 
688 	strlcpy(namebuf, p, namebuflen); /* Possible truncation */
689 	xfree(p);
690 
691 	buffer_append(&loginmsg, msg, strlen(msg));
692 	xfree(msg);
693 
694 	if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
695 	    (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
696 		fatal("%s: receive fds failed", __func__);
697 
698 	/* Success */
699 	return (1);
700 }
701 
702 void
703 mm_session_pty_cleanup2(Session *s)
704 {
705 	Buffer m;
706 
707 	if (s->ttyfd == -1)
708 		return;
709 	buffer_init(&m);
710 	buffer_put_cstring(&m, s->tty);
711 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
712 	buffer_free(&m);
713 
714 	/* closed dup'ed master */
715 	if (s->ptymaster != -1 && close(s->ptymaster) < 0)
716 		error("close(s->ptymaster/%d): %s",
717 		    s->ptymaster, strerror(errno));
718 
719 	/* unlink pty from session */
720 	s->ttyfd = -1;
721 }
722 
723 /* Request process termination */
724 
725 void
726 mm_terminate(void)
727 {
728 	Buffer m;
729 
730 	buffer_init(&m);
731 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
732 	buffer_free(&m);
733 }
734 
735 int
736 mm_ssh1_session_key(BIGNUM *num)
737 {
738 	int rsafail;
739 	Buffer m;
740 
741 	buffer_init(&m);
742 	buffer_put_bignum2(&m, num);
743 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
744 
745 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
746 
747 	rsafail = buffer_get_int(&m);
748 	buffer_get_bignum2(&m, num);
749 
750 	buffer_free(&m);
751 
752 	return (rsafail);
753 }
754 
755 static void
756 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
757     char ***prompts, u_int **echo_on)
758 {
759 	*name = xstrdup("");
760 	*infotxt = xstrdup("");
761 	*numprompts = 1;
762 	*prompts = xcalloc(*numprompts, sizeof(char *));
763 	*echo_on = xcalloc(*numprompts, sizeof(u_int));
764 	(*echo_on)[0] = 0;
765 }
766 
767 int
768 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
769    u_int *numprompts, char ***prompts, u_int **echo_on)
770 {
771 	Buffer m;
772 	u_int success;
773 	char *challenge;
774 
775 	debug3("%s: entering", __func__);
776 
777 	buffer_init(&m);
778 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
779 
780 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
781 	    &m);
782 	success = buffer_get_int(&m);
783 	if (success == 0) {
784 		debug3("%s: no challenge", __func__);
785 		buffer_free(&m);
786 		return (-1);
787 	}
788 
789 	/* Get the challenge, and format the response */
790 	challenge  = buffer_get_string(&m, NULL);
791 	buffer_free(&m);
792 
793 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
794 	(*prompts)[0] = challenge;
795 
796 	debug3("%s: received challenge: %s", __func__, challenge);
797 
798 	return (0);
799 }
800 
801 int
802 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
803 {
804 	Buffer m;
805 	int authok;
806 
807 	debug3("%s: entering", __func__);
808 	if (numresponses != 1)
809 		return (-1);
810 
811 	buffer_init(&m);
812 	buffer_put_cstring(&m, responses[0]);
813 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
814 
815 	mm_request_receive_expect(pmonitor->m_recvfd,
816 	    MONITOR_ANS_BSDAUTHRESPOND, &m);
817 
818 	authok = buffer_get_int(&m);
819 	buffer_free(&m);
820 
821 	return ((authok == 0) ? -1 : 0);
822 }
823 
824 
825 void
826 mm_ssh1_session_id(u_char session_id[16])
827 {
828 	Buffer m;
829 	int i;
830 
831 	debug3("%s entering", __func__);
832 
833 	buffer_init(&m);
834 	for (i = 0; i < 16; i++)
835 		buffer_put_char(&m, session_id[i]);
836 
837 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
838 	buffer_free(&m);
839 }
840 
841 int
842 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
843 {
844 	Buffer m;
845 	Key *key;
846 	u_char *blob;
847 	u_int blen;
848 	int allowed = 0, have_forced = 0;
849 
850 	debug3("%s entering", __func__);
851 
852 	buffer_init(&m);
853 	buffer_put_bignum2(&m, client_n);
854 
855 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
856 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
857 
858 	allowed = buffer_get_int(&m);
859 
860 	/* fake forced command */
861 	auth_clear_options();
862 	have_forced = buffer_get_int(&m);
863 	forced_command = have_forced ? xstrdup("true") : NULL;
864 
865 	if (allowed && rkey != NULL) {
866 		blob = buffer_get_string(&m, &blen);
867 		if ((key = key_from_blob(blob, blen)) == NULL)
868 			fatal("%s: key_from_blob failed", __func__);
869 		*rkey = key;
870 		xfree(blob);
871 	}
872 	mm_send_debug(&m);
873 	buffer_free(&m);
874 
875 	return (allowed);
876 }
877 
878 BIGNUM *
879 mm_auth_rsa_generate_challenge(Key *key)
880 {
881 	Buffer m;
882 	BIGNUM *challenge;
883 	u_char *blob;
884 	u_int blen;
885 
886 	debug3("%s entering", __func__);
887 
888 	if ((challenge = BN_new()) == NULL)
889 		fatal("%s: BN_new failed", __func__);
890 
891 	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
892 	if (key_to_blob(key, &blob, &blen) == 0)
893 		fatal("%s: key_to_blob failed", __func__);
894 	key->type = KEY_RSA1;
895 
896 	buffer_init(&m);
897 	buffer_put_string(&m, blob, blen);
898 	xfree(blob);
899 
900 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
901 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
902 
903 	buffer_get_bignum2(&m, challenge);
904 	buffer_free(&m);
905 
906 	return (challenge);
907 }
908 
909 int
910 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
911 {
912 	Buffer m;
913 	u_char *blob;
914 	u_int blen;
915 	int success = 0;
916 
917 	debug3("%s entering", __func__);
918 
919 	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
920 	if (key_to_blob(key, &blob, &blen) == 0)
921 		fatal("%s: key_to_blob failed", __func__);
922 	key->type = KEY_RSA1;
923 
924 	buffer_init(&m);
925 	buffer_put_string(&m, blob, blen);
926 	buffer_put_string(&m, response, 16);
927 	xfree(blob);
928 
929 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
930 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
931 
932 	success = buffer_get_int(&m);
933 	buffer_free(&m);
934 
935 	return (success);
936 }
937 
938 #ifdef GSSAPI
939 OM_uint32
940 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
941 {
942 	Buffer m;
943 	OM_uint32 major;
944 
945 	/* Client doesn't get to see the context */
946 	*ctx = NULL;
947 
948 	buffer_init(&m);
949 	buffer_put_string(&m, goid->elements, goid->length);
950 
951 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
952 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
953 
954 	major = buffer_get_int(&m);
955 
956 	buffer_free(&m);
957 	return (major);
958 }
959 
960 OM_uint32
961 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
962     gss_buffer_desc *out, OM_uint32 *flags)
963 {
964 	Buffer m;
965 	OM_uint32 major;
966 	u_int len;
967 
968 	buffer_init(&m);
969 	buffer_put_string(&m, in->value, in->length);
970 
971 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
972 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
973 
974 	major = buffer_get_int(&m);
975 	out->value = buffer_get_string(&m, &len);
976 	out->length = len;
977 	if (flags)
978 		*flags = buffer_get_int(&m);
979 
980 	buffer_free(&m);
981 
982 	return (major);
983 }
984 
985 OM_uint32
986 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
987 {
988 	Buffer m;
989 	OM_uint32 major;
990 
991 	buffer_init(&m);
992 	buffer_put_string(&m, gssbuf->value, gssbuf->length);
993 	buffer_put_string(&m, gssmic->value, gssmic->length);
994 
995 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
996 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
997 	    &m);
998 
999 	major = buffer_get_int(&m);
1000 	buffer_free(&m);
1001 	return(major);
1002 }
1003 
1004 int
1005 mm_ssh_gssapi_userok(char *user)
1006 {
1007 	Buffer m;
1008 	int authenticated = 0;
1009 
1010 	buffer_init(&m);
1011 
1012 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1013 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1014 				  &m);
1015 
1016 	authenticated = buffer_get_int(&m);
1017 
1018 	buffer_free(&m);
1019 	debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1020 	return (authenticated);
1021 }
1022 #endif /* GSSAPI */
1023