xref: /openbsd-src/usr.bin/ssh/monitor_wrap.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /* $OpenBSD: monitor_wrap.c,v 1.88 2016/03/07 19:02:43 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 <errno.h>
33 #include <pwd.h>
34 #include <signal.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38 
39 #ifdef WITH_OPENSSL
40 #include <openssl/bn.h>
41 #include <openssl/dh.h>
42 #endif
43 
44 #include "xmalloc.h"
45 #include "ssh.h"
46 #ifdef WITH_OPENSSL
47 #include "dh.h"
48 #endif
49 #include "buffer.h"
50 #include "key.h"
51 #include "cipher.h"
52 #include "kex.h"
53 #include "hostfile.h"
54 #include "auth.h"
55 #include "auth-options.h"
56 #include "packet.h"
57 #include "mac.h"
58 #include "log.h"
59 #include <zlib.h>
60 #include "monitor.h"
61 #ifdef GSSAPI
62 #include "ssh-gss.h"
63 #endif
64 #include "monitor_wrap.h"
65 #include "atomicio.h"
66 #include "monitor_fdpass.h"
67 #include "misc.h"
68 #include "uuencode.h"
69 
70 #include "channels.h"
71 #include "session.h"
72 #include "servconf.h"
73 
74 #include "ssherr.h"
75 
76 /* Imports */
77 extern int compat20;
78 extern z_stream incoming_stream;
79 extern z_stream outgoing_stream;
80 extern struct monitor *pmonitor;
81 extern Buffer loginmsg;
82 extern ServerOptions options;
83 
84 void
85 mm_log_handler(LogLevel level, const char *msg, void *ctx)
86 {
87 	Buffer log_msg;
88 	struct monitor *mon = (struct monitor *)ctx;
89 
90 	if (mon->m_log_sendfd == -1)
91 		fatal("%s: no log channel", __func__);
92 
93 	buffer_init(&log_msg);
94 	/*
95 	 * Placeholder for packet length. Will be filled in with the actual
96 	 * packet length once the packet has been constucted. This saves
97 	 * fragile math.
98 	 */
99 	buffer_put_int(&log_msg, 0);
100 
101 	buffer_put_int(&log_msg, level);
102 	buffer_put_cstring(&log_msg, msg);
103 	put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4);
104 	if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg),
105 	    buffer_len(&log_msg)) != buffer_len(&log_msg))
106 		fatal("%s: write: %s", __func__, strerror(errno));
107 	buffer_free(&log_msg);
108 }
109 
110 int
111 mm_is_monitor(void)
112 {
113 	/*
114 	 * m_pid is only set in the privileged part, and
115 	 * points to the unprivileged child.
116 	 */
117 	return (pmonitor && pmonitor->m_pid > 0);
118 }
119 
120 void
121 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
122 {
123 	u_int mlen = buffer_len(m);
124 	u_char buf[5];
125 
126 	debug3("%s entering: type %d", __func__, type);
127 
128 	put_u32(buf, mlen + 1);
129 	buf[4] = (u_char) type;		/* 1st byte of payload is mesg-type */
130 	if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
131 		fatal("%s: write: %s", __func__, strerror(errno));
132 	if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
133 		fatal("%s: write: %s", __func__, strerror(errno));
134 }
135 
136 void
137 mm_request_receive(int sock, Buffer *m)
138 {
139 	u_char buf[4];
140 	u_int msg_len;
141 
142 	debug3("%s entering", __func__);
143 
144 	if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
145 		if (errno == EPIPE)
146 			cleanup_exit(255);
147 		fatal("%s: read: %s", __func__, strerror(errno));
148 	}
149 	msg_len = get_u32(buf);
150 	if (msg_len > 256 * 1024)
151 		fatal("%s: read: bad msg_len %d", __func__, msg_len);
152 	buffer_clear(m);
153 	buffer_append_space(m, msg_len);
154 	if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
155 		fatal("%s: read: %s", __func__, strerror(errno));
156 }
157 
158 void
159 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
160 {
161 	u_char rtype;
162 
163 	debug3("%s entering: type %d", __func__, type);
164 
165 	mm_request_receive(sock, m);
166 	rtype = buffer_get_char(m);
167 	if (rtype != type)
168 		fatal("%s: read: rtype %d != type %d", __func__,
169 		    rtype, type);
170 }
171 
172 #ifdef WITH_OPENSSL
173 DH *
174 mm_choose_dh(int min, int nbits, int max)
175 {
176 	BIGNUM *p, *g;
177 	int success = 0;
178 	Buffer m;
179 
180 	buffer_init(&m);
181 	buffer_put_int(&m, min);
182 	buffer_put_int(&m, nbits);
183 	buffer_put_int(&m, max);
184 
185 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
186 
187 	debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
188 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
189 
190 	success = buffer_get_char(&m);
191 	if (success == 0)
192 		fatal("%s: MONITOR_ANS_MODULI failed", __func__);
193 
194 	if ((p = BN_new()) == NULL)
195 		fatal("%s: BN_new failed", __func__);
196 	if ((g = BN_new()) == NULL)
197 		fatal("%s: BN_new failed", __func__);
198 	buffer_get_bignum2(&m, p);
199 	buffer_get_bignum2(&m, g);
200 
201 	debug3("%s: remaining %d", __func__, buffer_len(&m));
202 	buffer_free(&m);
203 
204 	return (dh_new_group(g, p));
205 }
206 #endif
207 
208 int
209 mm_key_sign(Key *key, u_char **sigp, u_int *lenp,
210     const u_char *data, u_int datalen, const char *hostkey_alg)
211 {
212 	struct kex *kex = *pmonitor->m_pkex;
213 	Buffer m;
214 
215 	debug3("%s entering", __func__);
216 
217 	buffer_init(&m);
218 	buffer_put_int(&m, kex->host_key_index(key, 0, active_state));
219 	buffer_put_string(&m, data, datalen);
220 	buffer_put_cstring(&m, hostkey_alg);
221 
222 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
223 
224 	debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
225 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
226 	*sigp  = buffer_get_string(&m, lenp);
227 	buffer_free(&m);
228 
229 	return (0);
230 }
231 
232 struct passwd *
233 mm_getpwnamallow(const char *username)
234 {
235 	Buffer m;
236 	struct passwd *pw;
237 	u_int len, i;
238 	ServerOptions *newopts;
239 
240 	debug3("%s entering", __func__);
241 
242 	buffer_init(&m);
243 	buffer_put_cstring(&m, username);
244 
245 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
246 
247 	debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
248 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
249 
250 	if (buffer_get_char(&m) == 0) {
251 		pw = NULL;
252 		goto out;
253 	}
254 	pw = buffer_get_string(&m, &len);
255 	if (len != sizeof(struct passwd))
256 		fatal("%s: struct passwd size mismatch", __func__);
257 	pw->pw_name = buffer_get_string(&m, NULL);
258 	pw->pw_passwd = buffer_get_string(&m, NULL);
259 	pw->pw_gecos = buffer_get_string(&m, NULL);
260 	pw->pw_class = buffer_get_string(&m, NULL);
261 	pw->pw_dir = buffer_get_string(&m, NULL);
262 	pw->pw_shell = buffer_get_string(&m, NULL);
263 
264 out:
265 	/* copy options block as a Match directive may have changed some */
266 	newopts = buffer_get_string(&m, &len);
267 	if (len != sizeof(*newopts))
268 		fatal("%s: option block size mismatch", __func__);
269 
270 #define M_CP_STROPT(x) do { \
271 		if (newopts->x != NULL) \
272 			newopts->x = buffer_get_string(&m, NULL); \
273 	} while (0)
274 #define M_CP_STRARRAYOPT(x, nx) do { \
275 		for (i = 0; i < newopts->nx; i++) \
276 			newopts->x[i] = buffer_get_string(&m, NULL); \
277 	} while (0)
278 	/* See comment in servconf.h */
279 	COPY_MATCH_STRING_OPTS();
280 #undef M_CP_STROPT
281 #undef M_CP_STRARRAYOPT
282 
283 	copy_set_server_options(&options, newopts, 1);
284 	free(newopts);
285 
286 	buffer_free(&m);
287 
288 	return (pw);
289 }
290 
291 char *
292 mm_auth2_read_banner(void)
293 {
294 	Buffer m;
295 	char *banner;
296 
297 	debug3("%s entering", __func__);
298 
299 	buffer_init(&m);
300 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
301 	buffer_clear(&m);
302 
303 	mm_request_receive_expect(pmonitor->m_recvfd,
304 	    MONITOR_ANS_AUTH2_READ_BANNER, &m);
305 	banner = buffer_get_string(&m, NULL);
306 	buffer_free(&m);
307 
308 	/* treat empty banner as missing banner */
309 	if (strlen(banner) == 0) {
310 		free(banner);
311 		banner = NULL;
312 	}
313 	return (banner);
314 }
315 
316 /* Inform the privileged process about service and style */
317 
318 void
319 mm_inform_authserv(char *service, char *style)
320 {
321 	Buffer m;
322 
323 	debug3("%s entering", __func__);
324 
325 	buffer_init(&m);
326 	buffer_put_cstring(&m, service);
327 	buffer_put_cstring(&m, style ? style : "");
328 
329 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
330 
331 	buffer_free(&m);
332 }
333 
334 /* Do the password authentication */
335 int
336 mm_auth_password(Authctxt *authctxt, char *password)
337 {
338 	Buffer m;
339 	int authenticated = 0;
340 
341 	debug3("%s entering", __func__);
342 
343 	buffer_init(&m);
344 	buffer_put_cstring(&m, password);
345 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
346 
347 	debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
348 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
349 
350 	authenticated = buffer_get_int(&m);
351 
352 	buffer_free(&m);
353 
354 	debug3("%s: user %sauthenticated",
355 	    __func__, authenticated ? "" : "not ");
356 	return (authenticated);
357 }
358 
359 int
360 mm_user_key_allowed(struct passwd *pw, Key *key, int pubkey_auth_attempt)
361 {
362 	return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
363 	    pubkey_auth_attempt));
364 }
365 
366 int
367 mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host,
368     Key *key)
369 {
370 	return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0));
371 }
372 
373 int
374 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, const char *user,
375     const char *host, Key *key)
376 {
377 	int ret;
378 
379 	key->type = KEY_RSA; /* XXX hack for key_to_blob */
380 	ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key, 0);
381 	key->type = KEY_RSA1;
382 	return (ret);
383 }
384 
385 int
386 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
387     Key *key, int pubkey_auth_attempt)
388 {
389 	Buffer m;
390 	u_char *blob;
391 	u_int len;
392 	int allowed = 0, have_forced = 0;
393 
394 	debug3("%s entering", __func__);
395 
396 	/* Convert the key to a blob and the pass it over */
397 	if (!key_to_blob(key, &blob, &len))
398 		return (0);
399 
400 	buffer_init(&m);
401 	buffer_put_int(&m, type);
402 	buffer_put_cstring(&m, user ? user : "");
403 	buffer_put_cstring(&m, host ? host : "");
404 	buffer_put_string(&m, blob, len);
405 	buffer_put_int(&m, pubkey_auth_attempt);
406 	free(blob);
407 
408 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
409 
410 	debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
411 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
412 
413 	allowed = buffer_get_int(&m);
414 
415 	/* fake forced command */
416 	auth_clear_options();
417 	have_forced = buffer_get_int(&m);
418 	forced_command = have_forced ? xstrdup("true") : NULL;
419 
420 	buffer_free(&m);
421 
422 	return (allowed);
423 }
424 
425 /*
426  * This key verify needs to send the key type along, because the
427  * privileged parent makes the decision if the key is allowed
428  * for authentication.
429  */
430 
431 int
432 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
433 {
434 	Buffer m;
435 	u_char *blob;
436 	u_int len;
437 	int verified = 0;
438 
439 	debug3("%s entering", __func__);
440 
441 	/* Convert the key to a blob and the pass it over */
442 	if (!key_to_blob(key, &blob, &len))
443 		return (0);
444 
445 	buffer_init(&m);
446 	buffer_put_string(&m, blob, len);
447 	buffer_put_string(&m, sig, siglen);
448 	buffer_put_string(&m, data, datalen);
449 	free(blob);
450 
451 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
452 
453 	debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
454 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
455 
456 	verified = buffer_get_int(&m);
457 
458 	buffer_free(&m);
459 
460 	return (verified);
461 }
462 
463 void
464 mm_send_keystate(struct monitor *monitor)
465 {
466 	struct ssh *ssh = active_state;		/* XXX */
467 	struct sshbuf *m;
468 	int r;
469 
470 	if ((m = sshbuf_new()) == NULL)
471 		fatal("%s: sshbuf_new failed", __func__);
472 	if ((r = ssh_packet_get_state(ssh, m)) != 0)
473 		fatal("%s: get_state failed: %s",
474 		    __func__, ssh_err(r));
475 	mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
476 	debug3("%s: Finished sending state", __func__);
477 	sshbuf_free(m);
478 }
479 
480 int
481 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
482 {
483 	Buffer m;
484 	char *p, *msg;
485 	int success = 0, tmp1 = -1, tmp2 = -1;
486 
487 	/* Kludge: ensure there are fds free to receive the pty/tty */
488 	if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
489 	    (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
490 		error("%s: cannot allocate fds for pty", __func__);
491 		if (tmp1 > 0)
492 			close(tmp1);
493 		if (tmp2 > 0)
494 			close(tmp2);
495 		return 0;
496 	}
497 	close(tmp1);
498 	close(tmp2);
499 
500 	buffer_init(&m);
501 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
502 
503 	debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
504 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
505 
506 	success = buffer_get_int(&m);
507 	if (success == 0) {
508 		debug3("%s: pty alloc failed", __func__);
509 		buffer_free(&m);
510 		return (0);
511 	}
512 	p = buffer_get_string(&m, NULL);
513 	msg = buffer_get_string(&m, NULL);
514 	buffer_free(&m);
515 
516 	strlcpy(namebuf, p, namebuflen); /* Possible truncation */
517 	free(p);
518 
519 	buffer_append(&loginmsg, msg, strlen(msg));
520 	free(msg);
521 
522 	if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
523 	    (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
524 		fatal("%s: receive fds failed", __func__);
525 
526 	/* Success */
527 	return (1);
528 }
529 
530 void
531 mm_session_pty_cleanup2(Session *s)
532 {
533 	Buffer m;
534 
535 	if (s->ttyfd == -1)
536 		return;
537 	buffer_init(&m);
538 	buffer_put_cstring(&m, s->tty);
539 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
540 	buffer_free(&m);
541 
542 	/* closed dup'ed master */
543 	if (s->ptymaster != -1 && close(s->ptymaster) < 0)
544 		error("close(s->ptymaster/%d): %s",
545 		    s->ptymaster, strerror(errno));
546 
547 	/* unlink pty from session */
548 	s->ttyfd = -1;
549 }
550 
551 /* Request process termination */
552 
553 void
554 mm_terminate(void)
555 {
556 	Buffer m;
557 
558 	buffer_init(&m);
559 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
560 	buffer_free(&m);
561 }
562 
563 #ifdef WITH_SSH1
564 int
565 mm_ssh1_session_key(BIGNUM *num)
566 {
567 	int rsafail;
568 	Buffer m;
569 
570 	buffer_init(&m);
571 	buffer_put_bignum2(&m, num);
572 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
573 
574 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
575 
576 	rsafail = buffer_get_int(&m);
577 	buffer_get_bignum2(&m, num);
578 
579 	buffer_free(&m);
580 
581 	return (rsafail);
582 }
583 #endif
584 
585 static void
586 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
587     char ***prompts, u_int **echo_on)
588 {
589 	*name = xstrdup("");
590 	*infotxt = xstrdup("");
591 	*numprompts = 1;
592 	*prompts = xcalloc(*numprompts, sizeof(char *));
593 	*echo_on = xcalloc(*numprompts, sizeof(u_int));
594 	(*echo_on)[0] = 0;
595 }
596 
597 int
598 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
599    u_int *numprompts, char ***prompts, u_int **echo_on)
600 {
601 	Buffer m;
602 	u_int success;
603 	char *challenge;
604 
605 	debug3("%s: entering", __func__);
606 
607 	buffer_init(&m);
608 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
609 
610 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
611 	    &m);
612 	success = buffer_get_int(&m);
613 	if (success == 0) {
614 		debug3("%s: no challenge", __func__);
615 		buffer_free(&m);
616 		return (-1);
617 	}
618 
619 	/* Get the challenge, and format the response */
620 	challenge  = buffer_get_string(&m, NULL);
621 	buffer_free(&m);
622 
623 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
624 	(*prompts)[0] = challenge;
625 
626 	debug3("%s: received challenge: %s", __func__, challenge);
627 
628 	return (0);
629 }
630 
631 int
632 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
633 {
634 	Buffer m;
635 	int authok;
636 
637 	debug3("%s: entering", __func__);
638 	if (numresponses != 1)
639 		return (-1);
640 
641 	buffer_init(&m);
642 	buffer_put_cstring(&m, responses[0]);
643 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
644 
645 	mm_request_receive_expect(pmonitor->m_recvfd,
646 	    MONITOR_ANS_BSDAUTHRESPOND, &m);
647 
648 	authok = buffer_get_int(&m);
649 	buffer_free(&m);
650 
651 	return ((authok == 0) ? -1 : 0);
652 }
653 
654 
655 void
656 mm_ssh1_session_id(u_char session_id[16])
657 {
658 	Buffer m;
659 	int i;
660 
661 	debug3("%s entering", __func__);
662 
663 	buffer_init(&m);
664 	for (i = 0; i < 16; i++)
665 		buffer_put_char(&m, session_id[i]);
666 
667 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
668 	buffer_free(&m);
669 }
670 
671 #ifdef WITH_SSH1
672 int
673 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
674 {
675 	Buffer m;
676 	Key *key;
677 	u_char *blob;
678 	u_int blen;
679 	int allowed = 0, have_forced = 0;
680 
681 	debug3("%s entering", __func__);
682 
683 	buffer_init(&m);
684 	buffer_put_bignum2(&m, client_n);
685 
686 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
687 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
688 
689 	allowed = buffer_get_int(&m);
690 
691 	/* fake forced command */
692 	auth_clear_options();
693 	have_forced = buffer_get_int(&m);
694 	forced_command = have_forced ? xstrdup("true") : NULL;
695 
696 	if (allowed && rkey != NULL) {
697 		blob = buffer_get_string(&m, &blen);
698 		if ((key = key_from_blob(blob, blen)) == NULL)
699 			fatal("%s: key_from_blob failed", __func__);
700 		*rkey = key;
701 		free(blob);
702 	}
703 	buffer_free(&m);
704 
705 	return (allowed);
706 }
707 
708 BIGNUM *
709 mm_auth_rsa_generate_challenge(Key *key)
710 {
711 	Buffer m;
712 	BIGNUM *challenge;
713 	u_char *blob;
714 	u_int blen;
715 
716 	debug3("%s entering", __func__);
717 
718 	if ((challenge = BN_new()) == NULL)
719 		fatal("%s: BN_new failed", __func__);
720 
721 	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
722 	if (key_to_blob(key, &blob, &blen) == 0)
723 		fatal("%s: key_to_blob failed", __func__);
724 	key->type = KEY_RSA1;
725 
726 	buffer_init(&m);
727 	buffer_put_string(&m, blob, blen);
728 	free(blob);
729 
730 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
731 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
732 
733 	buffer_get_bignum2(&m, challenge);
734 	buffer_free(&m);
735 
736 	return (challenge);
737 }
738 
739 int
740 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
741 {
742 	Buffer m;
743 	u_char *blob;
744 	u_int blen;
745 	int success = 0;
746 
747 	debug3("%s entering", __func__);
748 
749 	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
750 	if (key_to_blob(key, &blob, &blen) == 0)
751 		fatal("%s: key_to_blob failed", __func__);
752 	key->type = KEY_RSA1;
753 
754 	buffer_init(&m);
755 	buffer_put_string(&m, blob, blen);
756 	buffer_put_string(&m, response, 16);
757 	free(blob);
758 
759 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
760 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
761 
762 	success = buffer_get_int(&m);
763 	buffer_free(&m);
764 
765 	return (success);
766 }
767 #endif
768 
769 #ifdef GSSAPI
770 OM_uint32
771 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
772 {
773 	Buffer m;
774 	OM_uint32 major;
775 
776 	/* Client doesn't get to see the context */
777 	*ctx = NULL;
778 
779 	buffer_init(&m);
780 	buffer_put_string(&m, goid->elements, goid->length);
781 
782 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
783 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
784 
785 	major = buffer_get_int(&m);
786 
787 	buffer_free(&m);
788 	return (major);
789 }
790 
791 OM_uint32
792 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
793     gss_buffer_desc *out, OM_uint32 *flags)
794 {
795 	Buffer m;
796 	OM_uint32 major;
797 	u_int len;
798 
799 	buffer_init(&m);
800 	buffer_put_string(&m, in->value, in->length);
801 
802 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
803 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
804 
805 	major = buffer_get_int(&m);
806 	out->value = buffer_get_string(&m, &len);
807 	out->length = len;
808 	if (flags)
809 		*flags = buffer_get_int(&m);
810 
811 	buffer_free(&m);
812 
813 	return (major);
814 }
815 
816 OM_uint32
817 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
818 {
819 	Buffer m;
820 	OM_uint32 major;
821 
822 	buffer_init(&m);
823 	buffer_put_string(&m, gssbuf->value, gssbuf->length);
824 	buffer_put_string(&m, gssmic->value, gssmic->length);
825 
826 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
827 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
828 	    &m);
829 
830 	major = buffer_get_int(&m);
831 	buffer_free(&m);
832 	return(major);
833 }
834 
835 int
836 mm_ssh_gssapi_userok(char *user)
837 {
838 	Buffer m;
839 	int authenticated = 0;
840 
841 	buffer_init(&m);
842 
843 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
844 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
845 				  &m);
846 
847 	authenticated = buffer_get_int(&m);
848 
849 	buffer_free(&m);
850 	debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
851 	return (authenticated);
852 }
853 #endif /* GSSAPI */
854 
855