xref: /openbsd-src/usr.bin/ssh/monitor_wrap.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
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.20 2002/11/21 23:03:51 deraadt Exp $");
29 
30 #include <openssl/bn.h>
31 #include <openssl/dh.h>
32 
33 #include "ssh.h"
34 #include "dh.h"
35 #include "kex.h"
36 #include "auth.h"
37 #include "buffer.h"
38 #include "bufaux.h"
39 #include "packet.h"
40 #include "mac.h"
41 #include "log.h"
42 #include "zlib.h"
43 #include "monitor.h"
44 #include "monitor_wrap.h"
45 #include "xmalloc.h"
46 #include "atomicio.h"
47 #include "monitor_fdpass.h"
48 #include "getput.h"
49 
50 #include "auth.h"
51 #include "channels.h"
52 #include "session.h"
53 
54 /* Imports */
55 extern int compat20;
56 extern Newkeys *newkeys[];
57 extern z_stream incoming_stream;
58 extern z_stream outgoing_stream;
59 extern struct monitor *pmonitor;
60 extern Buffer input, output;
61 
62 void
63 mm_request_send(int socket, enum monitor_reqtype type, Buffer *m)
64 {
65 	u_int mlen = buffer_len(m);
66 	u_char buf[5];
67 
68 	debug3("%s entering: type %d", __func__, type);
69 
70 	PUT_32BIT(buf, mlen + 1);
71 	buf[4] = (u_char) type;		/* 1st byte of payload is mesg-type */
72 	if (atomicio(write, socket, buf, sizeof(buf)) != sizeof(buf))
73 		fatal("%s: write", __func__);
74 	if (atomicio(write, socket, buffer_ptr(m), mlen) != mlen)
75 		fatal("%s: write", __func__);
76 }
77 
78 void
79 mm_request_receive(int socket, Buffer *m)
80 {
81 	u_char buf[4];
82 	u_int msg_len;
83 	ssize_t res;
84 
85 	debug3("%s entering", __func__);
86 
87 	res = atomicio(read, socket, buf, sizeof(buf));
88 	if (res != sizeof(buf)) {
89 		if (res == 0)
90 			fatal_cleanup();
91 		fatal("%s: read: %ld", __func__, (long)res);
92 	}
93 	msg_len = GET_32BIT(buf);
94 	if (msg_len > 256 * 1024)
95 		fatal("%s: read: bad msg_len %d", __func__, msg_len);
96 	buffer_clear(m);
97 	buffer_append_space(m, msg_len);
98 	res = atomicio(read, socket, buffer_ptr(m), msg_len);
99 	if (res != msg_len)
100 		fatal("%s: read: %ld != msg_len", __func__, (long)res);
101 }
102 
103 void
104 mm_request_receive_expect(int socket, enum monitor_reqtype type, Buffer *m)
105 {
106 	u_char rtype;
107 
108 	debug3("%s entering: type %d", __func__, type);
109 
110 	mm_request_receive(socket, m);
111 	rtype = buffer_get_char(m);
112 	if (rtype != type)
113 		fatal("%s: read: rtype %d != type %d", __func__,
114 		    rtype, type);
115 }
116 
117 DH *
118 mm_choose_dh(int min, int nbits, int max)
119 {
120 	BIGNUM *p, *g;
121 	int success = 0;
122 	Buffer m;
123 
124 	buffer_init(&m);
125 	buffer_put_int(&m, min);
126 	buffer_put_int(&m, nbits);
127 	buffer_put_int(&m, max);
128 
129 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
130 
131 	debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
132 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
133 
134 	success = buffer_get_char(&m);
135 	if (success == 0)
136 		fatal("%s: MONITOR_ANS_MODULI failed", __func__);
137 
138 	if ((p = BN_new()) == NULL)
139 		fatal("%s: BN_new failed", __func__);
140 	if ((g = BN_new()) == NULL)
141 		fatal("%s: BN_new failed", __func__);
142 	buffer_get_bignum2(&m, p);
143 	buffer_get_bignum2(&m, g);
144 
145 	debug3("%s: remaining %d", __func__, buffer_len(&m));
146 	buffer_free(&m);
147 
148 	return (dh_new_group(g, p));
149 }
150 
151 int
152 mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
153 {
154 	Kex *kex = *pmonitor->m_pkex;
155 	Buffer m;
156 
157 	debug3("%s entering", __func__);
158 
159 	buffer_init(&m);
160 	buffer_put_int(&m, kex->host_key_index(key));
161 	buffer_put_string(&m, data, datalen);
162 
163 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
164 
165 	debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
166 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
167 	*sigp  = buffer_get_string(&m, lenp);
168 	buffer_free(&m);
169 
170 	return (0);
171 }
172 
173 struct passwd *
174 mm_getpwnamallow(const char *login)
175 {
176 	Buffer m;
177 	struct passwd *pw;
178 	u_int pwlen;
179 
180 	debug3("%s entering", __func__);
181 
182 	buffer_init(&m);
183 	buffer_put_cstring(&m, login);
184 
185 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
186 
187 	debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
188 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
189 
190 	if (buffer_get_char(&m) == 0) {
191 		buffer_free(&m);
192 		return (NULL);
193 	}
194 	pw = buffer_get_string(&m, &pwlen);
195 	if (pwlen != sizeof(struct passwd))
196 		fatal("%s: struct passwd size mismatch", __func__);
197 	pw->pw_name = buffer_get_string(&m, NULL);
198 	pw->pw_passwd = buffer_get_string(&m, NULL);
199 	pw->pw_gecos = buffer_get_string(&m, NULL);
200 	pw->pw_class = buffer_get_string(&m, NULL);
201 	pw->pw_dir = buffer_get_string(&m, NULL);
202 	pw->pw_shell = buffer_get_string(&m, NULL);
203 	buffer_free(&m);
204 
205 	return (pw);
206 }
207 
208 char *mm_auth2_read_banner(void)
209 {
210 	Buffer m;
211 	char *banner;
212 
213 	debug3("%s entering", __func__);
214 
215 	buffer_init(&m);
216 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
217 	buffer_clear(&m);
218 
219 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTH2_READ_BANNER, &m);
220 	banner = buffer_get_string(&m, NULL);
221 	buffer_free(&m);
222 
223 	return (banner);
224 }
225 
226 /* Inform the privileged process about service and style */
227 
228 void
229 mm_inform_authserv(char *service, char *style)
230 {
231 	Buffer m;
232 
233 	debug3("%s entering", __func__);
234 
235 	buffer_init(&m);
236 	buffer_put_cstring(&m, service);
237 	buffer_put_cstring(&m, style ? style : "");
238 
239 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
240 
241 	buffer_free(&m);
242 }
243 
244 /* Do the password authentication */
245 int
246 mm_auth_password(Authctxt *authctxt, char *password)
247 {
248 	Buffer m;
249 	int authenticated = 0;
250 
251 	debug3("%s entering", __func__);
252 
253 	buffer_init(&m);
254 	buffer_put_cstring(&m, password);
255 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
256 
257 	debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
258 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
259 
260 	authenticated = buffer_get_int(&m);
261 
262 	buffer_free(&m);
263 
264 	debug3("%s: user %sauthenticated",
265 	    __func__, authenticated ? "" : "not ");
266 	return (authenticated);
267 }
268 
269 int
270 mm_user_key_allowed(struct passwd *pw, Key *key)
271 {
272 	return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
273 }
274 
275 int
276 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
277     Key *key)
278 {
279 	return (mm_key_allowed(MM_HOSTKEY, user, host, key));
280 }
281 
282 int
283 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
284     char *host, Key *key)
285 {
286 	int ret;
287 
288 	key->type = KEY_RSA; /* XXX hack for key_to_blob */
289 	ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
290 	key->type = KEY_RSA1;
291 	return (ret);
292 }
293 
294 static void
295 mm_send_debug(Buffer *m)
296 {
297 	char *msg;
298 
299 	while (buffer_len(m)) {
300 		msg = buffer_get_string(m, NULL);
301 		debug3("%s: Sending debug: %s", __func__, msg);
302 		packet_send_debug("%s", msg);
303 		xfree(msg);
304 	}
305 }
306 
307 int
308 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
309 {
310 	Buffer m;
311 	u_char *blob;
312 	u_int len;
313 	int allowed = 0;
314 
315 	debug3("%s entering", __func__);
316 
317 	/* Convert the key to a blob and the pass it over */
318 	if (!key_to_blob(key, &blob, &len))
319 		return (0);
320 
321 	buffer_init(&m);
322 	buffer_put_int(&m, type);
323 	buffer_put_cstring(&m, user ? user : "");
324 	buffer_put_cstring(&m, host ? host : "");
325 	buffer_put_string(&m, blob, len);
326 	xfree(blob);
327 
328 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
329 
330 	debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
331 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
332 
333 	allowed = buffer_get_int(&m);
334 
335 	/* Send potential debug messages */
336 	mm_send_debug(&m);
337 
338 	buffer_free(&m);
339 
340 	return (allowed);
341 }
342 
343 /*
344  * This key verify needs to send the key type along, because the
345  * privileged parent makes the decision if the key is allowed
346  * for authentication.
347  */
348 
349 int
350 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
351 {
352 	Buffer m;
353 	u_char *blob;
354 	u_int len;
355 	int verified = 0;
356 
357 	debug3("%s entering", __func__);
358 
359 	/* Convert the key to a blob and the pass it over */
360 	if (!key_to_blob(key, &blob, &len))
361 		return (0);
362 
363 	buffer_init(&m);
364 	buffer_put_string(&m, blob, len);
365 	buffer_put_string(&m, sig, siglen);
366 	buffer_put_string(&m, data, datalen);
367 	xfree(blob);
368 
369 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
370 
371 	debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
372 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
373 
374 	verified = buffer_get_int(&m);
375 
376 	buffer_free(&m);
377 
378 	return (verified);
379 }
380 
381 /* Export key state after authentication */
382 Newkeys *
383 mm_newkeys_from_blob(u_char *blob, int blen)
384 {
385 	Buffer b;
386 	u_int len;
387 	Newkeys *newkey = NULL;
388 	Enc *enc;
389 	Mac *mac;
390 	Comp *comp;
391 
392 	debug3("%s: %p(%d)", __func__, blob, blen);
393 #ifdef DEBUG_PK
394 	dump_base64(stderr, blob, blen);
395 #endif
396 	buffer_init(&b);
397 	buffer_append(&b, blob, blen);
398 
399 	newkey = xmalloc(sizeof(*newkey));
400 	enc = &newkey->enc;
401 	mac = &newkey->mac;
402 	comp = &newkey->comp;
403 
404 	/* Enc structure */
405 	enc->name = buffer_get_string(&b, NULL);
406 	buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
407 	enc->enabled = buffer_get_int(&b);
408 	enc->block_size = buffer_get_int(&b);
409 	enc->key = buffer_get_string(&b, &enc->key_len);
410 	enc->iv = buffer_get_string(&b, &len);
411 	if (len != enc->block_size)
412 		fatal("%s: bad ivlen: expected %u != %u", __func__,
413 		    enc->block_size, len);
414 
415 	if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
416 		fatal("%s: bad cipher name %s or pointer %p", __func__,
417 		    enc->name, enc->cipher);
418 
419 	/* Mac structure */
420 	mac->name = buffer_get_string(&b, NULL);
421 	if (mac->name == NULL || mac_init(mac, mac->name) == -1)
422 		fatal("%s: can not init mac %s", __func__, mac->name);
423 	mac->enabled = buffer_get_int(&b);
424 	mac->key = buffer_get_string(&b, &len);
425 	if (len > mac->key_len)
426 		fatal("%s: bad mac key length: %u > %d", __func__, len,
427 		    mac->key_len);
428 	mac->key_len = len;
429 
430 	/* Comp structure */
431 	comp->type = buffer_get_int(&b);
432 	comp->enabled = buffer_get_int(&b);
433 	comp->name = buffer_get_string(&b, NULL);
434 
435 	len = buffer_len(&b);
436 	if (len != 0)
437 		error("newkeys_from_blob: remaining bytes in blob %u", len);
438 	buffer_free(&b);
439 	return (newkey);
440 }
441 
442 int
443 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
444 {
445 	Buffer b;
446 	int len;
447 	Enc *enc;
448 	Mac *mac;
449 	Comp *comp;
450 	Newkeys *newkey = newkeys[mode];
451 
452 	debug3("%s: converting %p", __func__, newkey);
453 
454 	if (newkey == NULL) {
455 		error("%s: newkey == NULL", __func__);
456 		return 0;
457 	}
458 	enc = &newkey->enc;
459 	mac = &newkey->mac;
460 	comp = &newkey->comp;
461 
462 	buffer_init(&b);
463 	/* Enc structure */
464 	buffer_put_cstring(&b, enc->name);
465 	/* The cipher struct is constant and shared, you export pointer */
466 	buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
467 	buffer_put_int(&b, enc->enabled);
468 	buffer_put_int(&b, enc->block_size);
469 	buffer_put_string(&b, enc->key, enc->key_len);
470 	packet_get_keyiv(mode, enc->iv, enc->block_size);
471 	buffer_put_string(&b, enc->iv, enc->block_size);
472 
473 	/* Mac structure */
474 	buffer_put_cstring(&b, mac->name);
475 	buffer_put_int(&b, mac->enabled);
476 	buffer_put_string(&b, mac->key, mac->key_len);
477 
478 	/* Comp structure */
479 	buffer_put_int(&b, comp->type);
480 	buffer_put_int(&b, comp->enabled);
481 	buffer_put_cstring(&b, comp->name);
482 
483 	len = buffer_len(&b);
484 	if (lenp != NULL)
485 		*lenp = len;
486 	if (blobp != NULL) {
487 		*blobp = xmalloc(len);
488 		memcpy(*blobp, buffer_ptr(&b), len);
489 	}
490 	memset(buffer_ptr(&b), 0, len);
491 	buffer_free(&b);
492 	return len;
493 }
494 
495 static void
496 mm_send_kex(Buffer *m, Kex *kex)
497 {
498 	buffer_put_string(m, kex->session_id, kex->session_id_len);
499 	buffer_put_int(m, kex->we_need);
500 	buffer_put_int(m, kex->hostkey_type);
501 	buffer_put_int(m, kex->kex_type);
502 	buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
503 	buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
504 	buffer_put_int(m, kex->flags);
505 	buffer_put_cstring(m, kex->client_version_string);
506 	buffer_put_cstring(m, kex->server_version_string);
507 }
508 
509 void
510 mm_send_keystate(struct monitor *pmonitor)
511 {
512 	Buffer m;
513 	u_char *blob, *p;
514 	u_int bloblen, plen;
515 
516 	buffer_init(&m);
517 
518 	if (!compat20) {
519 		u_char iv[24];
520 		u_char *key;
521 		u_int ivlen, keylen;
522 
523 		buffer_put_int(&m, packet_get_protocol_flags());
524 
525 		buffer_put_int(&m, packet_get_ssh1_cipher());
526 
527 		debug3("%s: Sending ssh1 KEY+IV", __func__);
528 		keylen = packet_get_encryption_key(NULL);
529 		key = xmalloc(keylen+1);	/* add 1 if keylen == 0 */
530 		keylen = packet_get_encryption_key(key);
531 		buffer_put_string(&m, key, keylen);
532 		memset(key, 0, keylen);
533 		xfree(key);
534 
535 		ivlen = packet_get_keyiv_len(MODE_OUT);
536 		packet_get_keyiv(MODE_OUT, iv, ivlen);
537 		buffer_put_string(&m, iv, ivlen);
538 		ivlen = packet_get_keyiv_len(MODE_OUT);
539 		packet_get_keyiv(MODE_IN, iv, ivlen);
540 		buffer_put_string(&m, iv, ivlen);
541 		goto skip;
542 	} else {
543 		/* Kex for rekeying */
544 		mm_send_kex(&m, *pmonitor->m_pkex);
545 	}
546 
547 	debug3("%s: Sending new keys: %p %p",
548 	    __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
549 
550 	/* Keys from Kex */
551 	if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
552 		fatal("%s: conversion of newkeys failed", __func__);
553 
554 	buffer_put_string(&m, blob, bloblen);
555 	xfree(blob);
556 
557 	if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
558 		fatal("%s: conversion of newkeys failed", __func__);
559 
560 	buffer_put_string(&m, blob, bloblen);
561 	xfree(blob);
562 
563 	buffer_put_int(&m, packet_get_seqnr(MODE_OUT));
564 	buffer_put_int(&m, packet_get_seqnr(MODE_IN));
565 
566 	debug3("%s: New keys have been sent", __func__);
567  skip:
568 	/* More key context */
569 	plen = packet_get_keycontext(MODE_OUT, NULL);
570 	p = xmalloc(plen+1);
571 	packet_get_keycontext(MODE_OUT, p);
572 	buffer_put_string(&m, p, plen);
573 	xfree(p);
574 
575 	plen = packet_get_keycontext(MODE_IN, NULL);
576 	p = xmalloc(plen+1);
577 	packet_get_keycontext(MODE_IN, p);
578 	buffer_put_string(&m, p, plen);
579 	xfree(p);
580 
581 	/* Compression state */
582 	debug3("%s: Sending compression state", __func__);
583 	buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
584 	buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
585 
586 	/* Network I/O buffers */
587 	buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
588 	buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
589 
590 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
591 	debug3("%s: Finished sending state", __func__);
592 
593 	buffer_free(&m);
594 }
595 
596 int
597 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
598 {
599 	Buffer m;
600 	char *p;
601 	int success = 0;
602 
603 	buffer_init(&m);
604 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
605 
606 	debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
607 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
608 
609 	success = buffer_get_int(&m);
610 	if (success == 0) {
611 		debug3("%s: pty alloc failed", __func__);
612 		buffer_free(&m);
613 		return (0);
614 	}
615 	p = buffer_get_string(&m, NULL);
616 	buffer_free(&m);
617 
618 	strlcpy(namebuf, p, namebuflen); /* Possible truncation */
619 	xfree(p);
620 
621 	*ptyfd = mm_receive_fd(pmonitor->m_recvfd);
622 	*ttyfd = mm_receive_fd(pmonitor->m_recvfd);
623 
624 	/* Success */
625 	return (1);
626 }
627 
628 void
629 mm_session_pty_cleanup2(void *session)
630 {
631 	Session *s = session;
632 	Buffer m;
633 
634 	if (s->ttyfd == -1)
635 		return;
636 	buffer_init(&m);
637 	buffer_put_cstring(&m, s->tty);
638 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
639 	buffer_free(&m);
640 
641 	/* closed dup'ed master */
642 	if (close(s->ptymaster) < 0)
643 		error("close(s->ptymaster): %s", strerror(errno));
644 
645 	/* unlink pty from session */
646 	s->ttyfd = -1;
647 }
648 
649 /* Request process termination */
650 
651 void
652 mm_terminate(void)
653 {
654 	Buffer m;
655 
656 	buffer_init(&m);
657 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
658 	buffer_free(&m);
659 }
660 
661 int
662 mm_ssh1_session_key(BIGNUM *num)
663 {
664 	int rsafail;
665 	Buffer m;
666 
667 	buffer_init(&m);
668 	buffer_put_bignum2(&m, num);
669 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
670 
671 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
672 
673 	rsafail = buffer_get_int(&m);
674 	buffer_get_bignum2(&m, num);
675 
676 	buffer_free(&m);
677 
678 	return (rsafail);
679 }
680 
681 static void
682 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
683     char ***prompts, u_int **echo_on)
684 {
685 	*name = xstrdup("");
686 	*infotxt = xstrdup("");
687 	*numprompts = 1;
688 	*prompts = xmalloc(*numprompts * sizeof(char *));
689 	*echo_on = xmalloc(*numprompts * sizeof(u_int));
690 	(*echo_on)[0] = 0;
691 }
692 
693 int
694 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
695    u_int *numprompts, char ***prompts, u_int **echo_on)
696 {
697 	Buffer m;
698 	int res;
699 	char *challenge;
700 
701 	debug3("%s: entering", __func__);
702 
703 	buffer_init(&m);
704 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
705 
706 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
707 	    &m);
708 	res = buffer_get_int(&m);
709 	if (res == -1) {
710 		debug3("%s: no challenge", __func__);
711 		buffer_free(&m);
712 		return (-1);
713 	}
714 
715 	/* Get the challenge, and format the response */
716 	challenge  = buffer_get_string(&m, NULL);
717 	buffer_free(&m);
718 
719 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
720 	(*prompts)[0] = challenge;
721 
722 	debug3("%s: received challenge: %s", __func__, challenge);
723 
724 	return (0);
725 }
726 
727 int
728 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
729 {
730 	Buffer m;
731 	int authok;
732 
733 	debug3("%s: entering", __func__);
734 	if (numresponses != 1)
735 		return (-1);
736 
737 	buffer_init(&m);
738 	buffer_put_cstring(&m, responses[0]);
739 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
740 
741 	mm_request_receive_expect(pmonitor->m_recvfd,
742 	    MONITOR_ANS_BSDAUTHRESPOND, &m);
743 
744 	authok = buffer_get_int(&m);
745 	buffer_free(&m);
746 
747 	return ((authok == 0) ? -1 : 0);
748 }
749 
750 int
751 mm_skey_query(void *ctx, char **name, char **infotxt,
752    u_int *numprompts, char ***prompts, u_int **echo_on)
753 {
754 	Buffer m;
755 	int len, res;
756 	char *p, *challenge;
757 
758 	debug3("%s: entering", __func__);
759 
760 	buffer_init(&m);
761 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
762 
763 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
764 	    &m);
765 	res = buffer_get_int(&m);
766 	if (res == -1) {
767 		debug3("%s: no challenge", __func__);
768 		buffer_free(&m);
769 		return (-1);
770 	}
771 
772 	/* Get the challenge, and format the response */
773 	challenge  = buffer_get_string(&m, NULL);
774 	buffer_free(&m);
775 
776 	debug3("%s: received challenge: %s", __func__, challenge);
777 
778 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
779 
780 	len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
781 	p = xmalloc(len);
782 	strlcpy(p, challenge, len);
783 	strlcat(p, SKEY_PROMPT, len);
784 	(*prompts)[0] = p;
785 	xfree(challenge);
786 
787 	return (0);
788 }
789 
790 int
791 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
792 {
793 	Buffer m;
794 	int authok;
795 
796 	debug3("%s: entering", __func__);
797 	if (numresponses != 1)
798 		return (-1);
799 
800 	buffer_init(&m);
801 	buffer_put_cstring(&m, responses[0]);
802 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
803 
804 	mm_request_receive_expect(pmonitor->m_recvfd,
805 	    MONITOR_ANS_SKEYRESPOND, &m);
806 
807 	authok = buffer_get_int(&m);
808 	buffer_free(&m);
809 
810 	return ((authok == 0) ? -1 : 0);
811 }
812 
813 void
814 mm_ssh1_session_id(u_char session_id[16])
815 {
816 	Buffer m;
817 	int i;
818 
819 	debug3("%s entering", __func__);
820 
821 	buffer_init(&m);
822 	for (i = 0; i < 16; i++)
823 		buffer_put_char(&m, session_id[i]);
824 
825 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
826 	buffer_free(&m);
827 }
828 
829 int
830 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
831 {
832 	Buffer m;
833 	Key *key;
834 	u_char *blob;
835 	u_int blen;
836 	int allowed = 0;
837 
838 	debug3("%s entering", __func__);
839 
840 	buffer_init(&m);
841 	buffer_put_bignum2(&m, client_n);
842 
843 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
844 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
845 
846 	allowed = buffer_get_int(&m);
847 
848 	if (allowed && rkey != NULL) {
849 		blob = buffer_get_string(&m, &blen);
850 		if ((key = key_from_blob(blob, blen)) == NULL)
851 			fatal("%s: key_from_blob failed", __func__);
852 		*rkey = key;
853 		xfree(blob);
854 	}
855 	mm_send_debug(&m);
856 	buffer_free(&m);
857 
858 	return (allowed);
859 }
860 
861 BIGNUM *
862 mm_auth_rsa_generate_challenge(Key *key)
863 {
864 	Buffer m;
865 	BIGNUM *challenge;
866 	u_char *blob;
867 	u_int blen;
868 
869 	debug3("%s entering", __func__);
870 
871 	if ((challenge = BN_new()) == NULL)
872 		fatal("%s: BN_new failed", __func__);
873 
874 	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
875 	if (key_to_blob(key, &blob, &blen) == 0)
876 		fatal("%s: key_to_blob failed", __func__);
877 	key->type = KEY_RSA1;
878 
879 	buffer_init(&m);
880 	buffer_put_string(&m, blob, blen);
881 	xfree(blob);
882 
883 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
884 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
885 
886 	buffer_get_bignum2(&m, challenge);
887 	buffer_free(&m);
888 
889 	return (challenge);
890 }
891 
892 int
893 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
894 {
895 	Buffer m;
896 	u_char *blob;
897 	u_int blen;
898 	int success = 0;
899 
900 	debug3("%s entering", __func__);
901 
902 	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
903 	if (key_to_blob(key, &blob, &blen) == 0)
904 		fatal("%s: key_to_blob failed", __func__);
905 	key->type = KEY_RSA1;
906 
907 	buffer_init(&m);
908 	buffer_put_string(&m, blob, blen);
909 	buffer_put_string(&m, response, 16);
910 	xfree(blob);
911 
912 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
913 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
914 
915 	success = buffer_get_int(&m);
916 	buffer_free(&m);
917 
918 	return (success);
919 }
920 
921 #ifdef KRB4
922 int
923 mm_auth_krb4(Authctxt *authctxt, void *_auth, char **client, void *_reply)
924 {
925 	KTEXT auth, reply;
926  	Buffer m;
927 	u_int rlen;
928 	int success = 0;
929 	char *p;
930 
931 	debug3("%s entering", __func__);
932 	auth = _auth;
933 	reply = _reply;
934 
935 	buffer_init(&m);
936 	buffer_put_string(&m, auth->dat, auth->length);
937 
938 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KRB4, &m);
939 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KRB4, &m);
940 
941 	success = buffer_get_int(&m);
942 	if (success) {
943 		*client = buffer_get_string(&m, NULL);
944 		p = buffer_get_string(&m, &rlen);
945 		if (rlen >= MAX_KTXT_LEN)
946 			fatal("%s: reply from monitor too large", __func__);
947 		reply->length = rlen;
948 		memcpy(reply->dat, p, rlen);
949 		memset(p, 0, rlen);
950 		xfree(p);
951 	}
952 	buffer_free(&m);
953 	return (success);
954 }
955 #endif
956 
957 #ifdef KRB5
958 int
959 mm_auth_krb5(void *ctx, void *argp, char **userp, void *resp)
960 {
961 	krb5_data *tkt, *reply;
962 	Buffer m;
963 	int success;
964 
965 	debug3("%s entering", __func__);
966 	tkt = (krb5_data *) argp;
967 	reply = (krb5_data *) resp;
968 
969 	buffer_init(&m);
970 	buffer_put_string(&m, tkt->data, tkt->length);
971 
972 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KRB5, &m);
973 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KRB5, &m);
974 
975 	success = buffer_get_int(&m);
976 	if (success) {
977 		u_int len;
978 
979 		*userp = buffer_get_string(&m, NULL);
980 		reply->data = buffer_get_string(&m, &len);
981 		reply->length = len;
982 	} else {
983 		memset(reply, 0, sizeof(*reply));
984 		*userp = NULL;
985 	}
986 
987 	buffer_free(&m);
988 	return (success);
989 }
990 #endif
991