monitor_wrap.c revision 296781
1/* $OpenBSD: monitor_wrap.c,v 1.87 2016/01/14 16:17:40 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 "includes.h"
29
30#include <sys/types.h>
31#include <sys/uio.h>
32
33#include <errno.h>
34#include <pwd.h>
35#include <signal.h>
36#include <stdarg.h>
37#include <stdio.h>
38#include <string.h>
39#include <unistd.h>
40
41#ifdef WITH_OPENSSL
42#include <openssl/bn.h>
43#include <openssl/dh.h>
44#include <openssl/evp.h>
45#endif
46
47#include "openbsd-compat/sys-queue.h"
48#include "xmalloc.h"
49#include "ssh.h"
50#ifdef WITH_OPENSSL
51#include "dh.h"
52#endif
53#include "buffer.h"
54#include "key.h"
55#include "cipher.h"
56#include "kex.h"
57#include "hostfile.h"
58#include "auth.h"
59#include "auth-options.h"
60#include "packet.h"
61#include "mac.h"
62#include "log.h"
63#ifdef TARGET_OS_MAC    /* XXX Broken krb5 headers on Mac */
64#undef TARGET_OS_MAC
65#include "zlib.h"
66#define TARGET_OS_MAC 1
67#else
68#include "zlib.h"
69#endif
70#include "monitor.h"
71#ifdef GSSAPI
72#include "ssh-gss.h"
73#endif
74#include "monitor_wrap.h"
75#include "atomicio.h"
76#include "monitor_fdpass.h"
77#include "misc.h"
78#include "uuencode.h"
79
80#include "channels.h"
81#include "session.h"
82#include "servconf.h"
83
84#include "ssherr.h"
85
86/* Imports */
87extern int compat20;
88extern z_stream incoming_stream;
89extern z_stream outgoing_stream;
90extern struct monitor *pmonitor;
91extern Buffer loginmsg;
92extern ServerOptions options;
93
94void
95mm_log_handler(LogLevel level, const char *msg, void *ctx)
96{
97	Buffer log_msg;
98	struct monitor *mon = (struct monitor *)ctx;
99
100	if (mon->m_log_sendfd == -1)
101		fatal("%s: no log channel", __func__);
102
103	buffer_init(&log_msg);
104	/*
105	 * Placeholder for packet length. Will be filled in with the actual
106	 * packet length once the packet has been constucted. This saves
107	 * fragile math.
108	 */
109	buffer_put_int(&log_msg, 0);
110
111	buffer_put_int(&log_msg, level);
112	buffer_put_cstring(&log_msg, msg);
113	put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4);
114	if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg),
115	    buffer_len(&log_msg)) != buffer_len(&log_msg))
116		fatal("%s: write: %s", __func__, strerror(errno));
117	buffer_free(&log_msg);
118}
119
120int
121mm_is_monitor(void)
122{
123	/*
124	 * m_pid is only set in the privileged part, and
125	 * points to the unprivileged child.
126	 */
127	return (pmonitor && pmonitor->m_pid > 0);
128}
129
130void
131mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
132{
133	u_int mlen = buffer_len(m);
134	u_char buf[5];
135
136	debug3("%s entering: type %d", __func__, type);
137
138	put_u32(buf, mlen + 1);
139	buf[4] = (u_char) type;		/* 1st byte of payload is mesg-type */
140	if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
141		fatal("%s: write: %s", __func__, strerror(errno));
142	if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
143		fatal("%s: write: %s", __func__, strerror(errno));
144}
145
146void
147mm_request_receive(int sock, Buffer *m)
148{
149	u_char buf[4];
150	u_int msg_len;
151
152	debug3("%s entering", __func__);
153
154	if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
155		if (errno == EPIPE)
156			cleanup_exit(255);
157		fatal("%s: read: %s", __func__, strerror(errno));
158	}
159	msg_len = get_u32(buf);
160	if (msg_len > 256 * 1024)
161		fatal("%s: read: bad msg_len %d", __func__, msg_len);
162	buffer_clear(m);
163	buffer_append_space(m, msg_len);
164	if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
165		fatal("%s: read: %s", __func__, strerror(errno));
166}
167
168void
169mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
170{
171	u_char rtype;
172
173	debug3("%s entering: type %d", __func__, type);
174
175	mm_request_receive(sock, m);
176	rtype = buffer_get_char(m);
177	if (rtype != type)
178		fatal("%s: read: rtype %d != type %d", __func__,
179		    rtype, type);
180}
181
182#ifdef WITH_OPENSSL
183DH *
184mm_choose_dh(int min, int nbits, int max)
185{
186	BIGNUM *p, *g;
187	int success = 0;
188	Buffer m;
189
190	buffer_init(&m);
191	buffer_put_int(&m, min);
192	buffer_put_int(&m, nbits);
193	buffer_put_int(&m, max);
194
195	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
196
197	debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
198	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
199
200	success = buffer_get_char(&m);
201	if (success == 0)
202		fatal("%s: MONITOR_ANS_MODULI failed", __func__);
203
204	if ((p = BN_new()) == NULL)
205		fatal("%s: BN_new failed", __func__);
206	if ((g = BN_new()) == NULL)
207		fatal("%s: BN_new failed", __func__);
208	buffer_get_bignum2(&m, p);
209	buffer_get_bignum2(&m, g);
210
211	debug3("%s: remaining %d", __func__, buffer_len(&m));
212	buffer_free(&m);
213
214	return (dh_new_group(g, p));
215}
216#endif
217
218int
219mm_key_sign(Key *key, u_char **sigp, u_int *lenp,
220    const u_char *data, u_int datalen, const char *hostkey_alg)
221{
222	struct kex *kex = *pmonitor->m_pkex;
223	Buffer m;
224
225	debug3("%s entering", __func__);
226
227	buffer_init(&m);
228	buffer_put_int(&m, kex->host_key_index(key, 0, active_state));
229	buffer_put_string(&m, data, datalen);
230	buffer_put_cstring(&m, hostkey_alg);
231
232	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
233
234	debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
235	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
236	*sigp  = buffer_get_string(&m, lenp);
237	buffer_free(&m);
238
239	return (0);
240}
241
242struct passwd *
243mm_getpwnamallow(const char *username)
244{
245	Buffer m;
246	struct passwd *pw;
247	u_int len, i;
248	ServerOptions *newopts;
249
250	debug3("%s entering", __func__);
251
252	buffer_init(&m);
253	buffer_put_cstring(&m, username);
254
255	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
256
257	debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
258	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
259
260	if (buffer_get_char(&m) == 0) {
261		pw = NULL;
262		goto out;
263	}
264	pw = buffer_get_string(&m, &len);
265	if (len != sizeof(struct passwd))
266		fatal("%s: struct passwd size mismatch", __func__);
267	pw->pw_name = buffer_get_string(&m, NULL);
268	pw->pw_passwd = buffer_get_string(&m, NULL);
269#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
270	pw->pw_gecos = buffer_get_string(&m, NULL);
271#endif
272#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
273	pw->pw_class = buffer_get_string(&m, NULL);
274#endif
275	pw->pw_dir = buffer_get_string(&m, NULL);
276	pw->pw_shell = buffer_get_string(&m, NULL);
277
278out:
279	/* copy options block as a Match directive may have changed some */
280	newopts = buffer_get_string(&m, &len);
281	if (len != sizeof(*newopts))
282		fatal("%s: option block size mismatch", __func__);
283
284#define M_CP_STROPT(x) do { \
285		if (newopts->x != NULL) \
286			newopts->x = buffer_get_string(&m, NULL); \
287	} while (0)
288#define M_CP_STRARRAYOPT(x, nx) do { \
289		for (i = 0; i < newopts->nx; i++) \
290			newopts->x[i] = buffer_get_string(&m, NULL); \
291	} while (0)
292	/* See comment in servconf.h */
293	COPY_MATCH_STRING_OPTS();
294#undef M_CP_STROPT
295#undef M_CP_STRARRAYOPT
296
297	copy_set_server_options(&options, newopts, 1);
298	free(newopts);
299
300	buffer_free(&m);
301
302	return (pw);
303}
304
305char *
306mm_auth2_read_banner(void)
307{
308	Buffer m;
309	char *banner;
310
311	debug3("%s entering", __func__);
312
313	buffer_init(&m);
314	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
315	buffer_clear(&m);
316
317	mm_request_receive_expect(pmonitor->m_recvfd,
318	    MONITOR_ANS_AUTH2_READ_BANNER, &m);
319	banner = buffer_get_string(&m, NULL);
320	buffer_free(&m);
321
322	/* treat empty banner as missing banner */
323	if (strlen(banner) == 0) {
324		free(banner);
325		banner = NULL;
326	}
327	return (banner);
328}
329
330/* Inform the privileged process about service and style */
331
332void
333mm_inform_authserv(char *service, char *style)
334{
335	Buffer m;
336
337	debug3("%s entering", __func__);
338
339	buffer_init(&m);
340	buffer_put_cstring(&m, service);
341	buffer_put_cstring(&m, style ? style : "");
342
343	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
344
345	buffer_free(&m);
346}
347
348/* Do the password authentication */
349int
350mm_auth_password(Authctxt *authctxt, char *password)
351{
352	Buffer m;
353	int authenticated = 0;
354
355	debug3("%s entering", __func__);
356
357	buffer_init(&m);
358	buffer_put_cstring(&m, password);
359	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
360
361	debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
362	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
363
364	authenticated = buffer_get_int(&m);
365
366	buffer_free(&m);
367
368	debug3("%s: user %sauthenticated",
369	    __func__, authenticated ? "" : "not ");
370	return (authenticated);
371}
372
373int
374mm_user_key_allowed(struct passwd *pw, Key *key, int pubkey_auth_attempt)
375{
376	return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
377	    pubkey_auth_attempt));
378}
379
380int
381mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
382    Key *key)
383{
384	return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0));
385}
386
387int
388mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
389    char *host, Key *key)
390{
391	int ret;
392
393	key->type = KEY_RSA; /* XXX hack for key_to_blob */
394	ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key, 0);
395	key->type = KEY_RSA1;
396	return (ret);
397}
398
399int
400mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key,
401    int pubkey_auth_attempt)
402{
403	Buffer m;
404	u_char *blob;
405	u_int len;
406	int allowed = 0, have_forced = 0;
407
408	debug3("%s entering", __func__);
409
410	/* Convert the key to a blob and the pass it over */
411	if (!key_to_blob(key, &blob, &len))
412		return (0);
413
414	buffer_init(&m);
415	buffer_put_int(&m, type);
416	buffer_put_cstring(&m, user ? user : "");
417	buffer_put_cstring(&m, host ? host : "");
418	buffer_put_string(&m, blob, len);
419	buffer_put_int(&m, pubkey_auth_attempt);
420	free(blob);
421
422	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
423
424	debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
425	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
426
427	allowed = buffer_get_int(&m);
428
429	/* fake forced command */
430	auth_clear_options();
431	have_forced = buffer_get_int(&m);
432	forced_command = have_forced ? xstrdup("true") : NULL;
433
434	buffer_free(&m);
435
436	return (allowed);
437}
438
439/*
440 * This key verify needs to send the key type along, because the
441 * privileged parent makes the decision if the key is allowed
442 * for authentication.
443 */
444
445int
446mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
447{
448	Buffer m;
449	u_char *blob;
450	u_int len;
451	int verified = 0;
452
453	debug3("%s entering", __func__);
454
455	/* Convert the key to a blob and the pass it over */
456	if (!key_to_blob(key, &blob, &len))
457		return (0);
458
459	buffer_init(&m);
460	buffer_put_string(&m, blob, len);
461	buffer_put_string(&m, sig, siglen);
462	buffer_put_string(&m, data, datalen);
463	free(blob);
464
465	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
466
467	debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
468	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
469
470	verified = buffer_get_int(&m);
471
472	buffer_free(&m);
473
474	return (verified);
475}
476
477void
478mm_send_keystate(struct monitor *monitor)
479{
480	struct ssh *ssh = active_state;		/* XXX */
481	struct sshbuf *m;
482	int r;
483
484	if ((m = sshbuf_new()) == NULL)
485		fatal("%s: sshbuf_new failed", __func__);
486	if ((r = ssh_packet_get_state(ssh, m)) != 0)
487		fatal("%s: get_state failed: %s",
488		    __func__, ssh_err(r));
489	mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
490	debug3("%s: Finished sending state", __func__);
491	sshbuf_free(m);
492}
493
494int
495mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
496{
497	Buffer m;
498	char *p, *msg;
499	int success = 0, tmp1 = -1, tmp2 = -1;
500
501	/* Kludge: ensure there are fds free to receive the pty/tty */
502	if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
503	    (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
504		error("%s: cannot allocate fds for pty", __func__);
505		if (tmp1 > 0)
506			close(tmp1);
507		if (tmp2 > 0)
508			close(tmp2);
509		return 0;
510	}
511	close(tmp1);
512	close(tmp2);
513
514	buffer_init(&m);
515	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
516
517	debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
518	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
519
520	success = buffer_get_int(&m);
521	if (success == 0) {
522		debug3("%s: pty alloc failed", __func__);
523		buffer_free(&m);
524		return (0);
525	}
526	p = buffer_get_string(&m, NULL);
527	msg = buffer_get_string(&m, NULL);
528	buffer_free(&m);
529
530	strlcpy(namebuf, p, namebuflen); /* Possible truncation */
531	free(p);
532
533	buffer_append(&loginmsg, msg, strlen(msg));
534	free(msg);
535
536	if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
537	    (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
538		fatal("%s: receive fds failed", __func__);
539
540	/* Success */
541	return (1);
542}
543
544void
545mm_session_pty_cleanup2(Session *s)
546{
547	Buffer m;
548
549	if (s->ttyfd == -1)
550		return;
551	buffer_init(&m);
552	buffer_put_cstring(&m, s->tty);
553	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
554	buffer_free(&m);
555
556	/* closed dup'ed master */
557	if (s->ptymaster != -1 && close(s->ptymaster) < 0)
558		error("close(s->ptymaster/%d): %s",
559		    s->ptymaster, strerror(errno));
560
561	/* unlink pty from session */
562	s->ttyfd = -1;
563}
564
565#ifdef USE_PAM
566void
567mm_start_pam(Authctxt *authctxt)
568{
569	Buffer m;
570
571	debug3("%s entering", __func__);
572	if (!options.use_pam)
573		fatal("UsePAM=no, but ended up in %s anyway", __func__);
574
575	buffer_init(&m);
576	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
577
578	buffer_free(&m);
579}
580
581u_int
582mm_do_pam_account(void)
583{
584	Buffer m;
585	u_int ret;
586	char *msg;
587
588	debug3("%s entering", __func__);
589	if (!options.use_pam)
590		fatal("UsePAM=no, but ended up in %s anyway", __func__);
591
592	buffer_init(&m);
593	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
594
595	mm_request_receive_expect(pmonitor->m_recvfd,
596	    MONITOR_ANS_PAM_ACCOUNT, &m);
597	ret = buffer_get_int(&m);
598	msg = buffer_get_string(&m, NULL);
599	buffer_append(&loginmsg, msg, strlen(msg));
600	free(msg);
601
602	buffer_free(&m);
603
604	debug3("%s returning %d", __func__, ret);
605
606	return (ret);
607}
608
609void *
610mm_sshpam_init_ctx(Authctxt *authctxt)
611{
612	Buffer m;
613	int success;
614
615	debug3("%s", __func__);
616	buffer_init(&m);
617	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
618	debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
619	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
620	success = buffer_get_int(&m);
621	if (success == 0) {
622		debug3("%s: pam_init_ctx failed", __func__);
623		buffer_free(&m);
624		return (NULL);
625	}
626	buffer_free(&m);
627	return (authctxt);
628}
629
630int
631mm_sshpam_query(void *ctx, char **name, char **info,
632    u_int *num, char ***prompts, u_int **echo_on)
633{
634	Buffer m;
635	u_int i;
636	int ret;
637
638	debug3("%s", __func__);
639	buffer_init(&m);
640	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
641	debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
642	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
643	ret = buffer_get_int(&m);
644	debug3("%s: pam_query returned %d", __func__, ret);
645	*name = buffer_get_string(&m, NULL);
646	*info = buffer_get_string(&m, NULL);
647	*num = buffer_get_int(&m);
648	if (*num > PAM_MAX_NUM_MSG)
649		fatal("%s: recieved %u PAM messages, expected <= %u",
650		    __func__, *num, PAM_MAX_NUM_MSG);
651	*prompts = xcalloc((*num + 1), sizeof(char *));
652	*echo_on = xcalloc((*num + 1), sizeof(u_int));
653	for (i = 0; i < *num; ++i) {
654		(*prompts)[i] = buffer_get_string(&m, NULL);
655		(*echo_on)[i] = buffer_get_int(&m);
656	}
657	buffer_free(&m);
658	return (ret);
659}
660
661int
662mm_sshpam_respond(void *ctx, u_int num, char **resp)
663{
664	Buffer m;
665	u_int i;
666	int ret;
667
668	debug3("%s", __func__);
669	buffer_init(&m);
670	buffer_put_int(&m, num);
671	for (i = 0; i < num; ++i)
672		buffer_put_cstring(&m, resp[i]);
673	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
674	debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
675	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
676	ret = buffer_get_int(&m);
677	debug3("%s: pam_respond returned %d", __func__, ret);
678	buffer_free(&m);
679	return (ret);
680}
681
682void
683mm_sshpam_free_ctx(void *ctxtp)
684{
685	Buffer m;
686
687	debug3("%s", __func__);
688	buffer_init(&m);
689	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
690	debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
691	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
692	buffer_free(&m);
693}
694#endif /* USE_PAM */
695
696/* Request process termination */
697
698void
699mm_terminate(void)
700{
701	Buffer m;
702
703	buffer_init(&m);
704	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
705	buffer_free(&m);
706}
707
708#ifdef WITH_SSH1
709int
710mm_ssh1_session_key(BIGNUM *num)
711{
712	int rsafail;
713	Buffer m;
714
715	buffer_init(&m);
716	buffer_put_bignum2(&m, num);
717	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
718
719	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
720
721	rsafail = buffer_get_int(&m);
722	buffer_get_bignum2(&m, num);
723
724	buffer_free(&m);
725
726	return (rsafail);
727}
728#endif
729
730static void
731mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
732    char ***prompts, u_int **echo_on)
733{
734	*name = xstrdup("");
735	*infotxt = xstrdup("");
736	*numprompts = 1;
737	*prompts = xcalloc(*numprompts, sizeof(char *));
738	*echo_on = xcalloc(*numprompts, sizeof(u_int));
739	(*echo_on)[0] = 0;
740}
741
742int
743mm_bsdauth_query(void *ctx, char **name, char **infotxt,
744   u_int *numprompts, char ***prompts, u_int **echo_on)
745{
746	Buffer m;
747	u_int success;
748	char *challenge;
749
750	debug3("%s: entering", __func__);
751
752	buffer_init(&m);
753	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
754
755	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
756	    &m);
757	success = buffer_get_int(&m);
758	if (success == 0) {
759		debug3("%s: no challenge", __func__);
760		buffer_free(&m);
761		return (-1);
762	}
763
764	/* Get the challenge, and format the response */
765	challenge  = buffer_get_string(&m, NULL);
766	buffer_free(&m);
767
768	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
769	(*prompts)[0] = challenge;
770
771	debug3("%s: received challenge: %s", __func__, challenge);
772
773	return (0);
774}
775
776int
777mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
778{
779	Buffer m;
780	int authok;
781
782	debug3("%s: entering", __func__);
783	if (numresponses != 1)
784		return (-1);
785
786	buffer_init(&m);
787	buffer_put_cstring(&m, responses[0]);
788	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
789
790	mm_request_receive_expect(pmonitor->m_recvfd,
791	    MONITOR_ANS_BSDAUTHRESPOND, &m);
792
793	authok = buffer_get_int(&m);
794	buffer_free(&m);
795
796	return ((authok == 0) ? -1 : 0);
797}
798
799#ifdef SKEY
800int
801mm_skey_query(void *ctx, char **name, char **infotxt,
802   u_int *numprompts, char ***prompts, u_int **echo_on)
803{
804	Buffer m;
805	u_int success;
806	char *challenge;
807
808	debug3("%s: entering", __func__);
809
810	buffer_init(&m);
811	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
812
813	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
814	    &m);
815	success = buffer_get_int(&m);
816	if (success == 0) {
817		debug3("%s: no challenge", __func__);
818		buffer_free(&m);
819		return (-1);
820	}
821
822	/* Get the challenge, and format the response */
823	challenge  = buffer_get_string(&m, NULL);
824	buffer_free(&m);
825
826	debug3("%s: received challenge: %s", __func__, challenge);
827
828	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
829
830	xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
831	free(challenge);
832
833	return (0);
834}
835
836int
837mm_skey_respond(void *ctx, u_int numresponses, char **responses)
838{
839	Buffer m;
840	int authok;
841
842	debug3("%s: entering", __func__);
843	if (numresponses != 1)
844		return (-1);
845
846	buffer_init(&m);
847	buffer_put_cstring(&m, responses[0]);
848	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
849
850	mm_request_receive_expect(pmonitor->m_recvfd,
851	    MONITOR_ANS_SKEYRESPOND, &m);
852
853	authok = buffer_get_int(&m);
854	buffer_free(&m);
855
856	return ((authok == 0) ? -1 : 0);
857}
858#endif /* SKEY */
859
860void
861mm_ssh1_session_id(u_char session_id[16])
862{
863	Buffer m;
864	int i;
865
866	debug3("%s entering", __func__);
867
868	buffer_init(&m);
869	for (i = 0; i < 16; i++)
870		buffer_put_char(&m, session_id[i]);
871
872	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
873	buffer_free(&m);
874}
875
876#ifdef WITH_SSH1
877int
878mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
879{
880	Buffer m;
881	Key *key;
882	u_char *blob;
883	u_int blen;
884	int allowed = 0, have_forced = 0;
885
886	debug3("%s entering", __func__);
887
888	buffer_init(&m);
889	buffer_put_bignum2(&m, client_n);
890
891	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
892	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
893
894	allowed = buffer_get_int(&m);
895
896	/* fake forced command */
897	auth_clear_options();
898	have_forced = buffer_get_int(&m);
899	forced_command = have_forced ? xstrdup("true") : NULL;
900
901	if (allowed && rkey != NULL) {
902		blob = buffer_get_string(&m, &blen);
903		if ((key = key_from_blob(blob, blen)) == NULL)
904			fatal("%s: key_from_blob failed", __func__);
905		*rkey = key;
906		free(blob);
907	}
908	buffer_free(&m);
909
910	return (allowed);
911}
912
913BIGNUM *
914mm_auth_rsa_generate_challenge(Key *key)
915{
916	Buffer m;
917	BIGNUM *challenge;
918	u_char *blob;
919	u_int blen;
920
921	debug3("%s entering", __func__);
922
923	if ((challenge = BN_new()) == NULL)
924		fatal("%s: BN_new failed", __func__);
925
926	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
927	if (key_to_blob(key, &blob, &blen) == 0)
928		fatal("%s: key_to_blob failed", __func__);
929	key->type = KEY_RSA1;
930
931	buffer_init(&m);
932	buffer_put_string(&m, blob, blen);
933	free(blob);
934
935	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
936	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
937
938	buffer_get_bignum2(&m, challenge);
939	buffer_free(&m);
940
941	return (challenge);
942}
943
944int
945mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
946{
947	Buffer m;
948	u_char *blob;
949	u_int blen;
950	int success = 0;
951
952	debug3("%s entering", __func__);
953
954	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
955	if (key_to_blob(key, &blob, &blen) == 0)
956		fatal("%s: key_to_blob failed", __func__);
957	key->type = KEY_RSA1;
958
959	buffer_init(&m);
960	buffer_put_string(&m, blob, blen);
961	buffer_put_string(&m, response, 16);
962	free(blob);
963
964	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
965	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
966
967	success = buffer_get_int(&m);
968	buffer_free(&m);
969
970	return (success);
971}
972#endif
973
974#ifdef SSH_AUDIT_EVENTS
975void
976mm_audit_event(ssh_audit_event_t event)
977{
978	Buffer m;
979
980	debug3("%s entering", __func__);
981
982	buffer_init(&m);
983	buffer_put_int(&m, event);
984
985	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
986	buffer_free(&m);
987}
988
989void
990mm_audit_run_command(const char *command)
991{
992	Buffer m;
993
994	debug3("%s entering command %s", __func__, command);
995
996	buffer_init(&m);
997	buffer_put_cstring(&m, command);
998
999	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1000	buffer_free(&m);
1001}
1002#endif /* SSH_AUDIT_EVENTS */
1003
1004#ifdef GSSAPI
1005OM_uint32
1006mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1007{
1008	Buffer m;
1009	OM_uint32 major;
1010
1011	/* Client doesn't get to see the context */
1012	*ctx = NULL;
1013
1014	buffer_init(&m);
1015	buffer_put_string(&m, goid->elements, goid->length);
1016
1017	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1018	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1019
1020	major = buffer_get_int(&m);
1021
1022	buffer_free(&m);
1023	return (major);
1024}
1025
1026OM_uint32
1027mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1028    gss_buffer_desc *out, OM_uint32 *flags)
1029{
1030	Buffer m;
1031	OM_uint32 major;
1032	u_int len;
1033
1034	buffer_init(&m);
1035	buffer_put_string(&m, in->value, in->length);
1036
1037	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1038	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1039
1040	major = buffer_get_int(&m);
1041	out->value = buffer_get_string(&m, &len);
1042	out->length = len;
1043	if (flags)
1044		*flags = buffer_get_int(&m);
1045
1046	buffer_free(&m);
1047
1048	return (major);
1049}
1050
1051OM_uint32
1052mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1053{
1054	Buffer m;
1055	OM_uint32 major;
1056
1057	buffer_init(&m);
1058	buffer_put_string(&m, gssbuf->value, gssbuf->length);
1059	buffer_put_string(&m, gssmic->value, gssmic->length);
1060
1061	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1062	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1063	    &m);
1064
1065	major = buffer_get_int(&m);
1066	buffer_free(&m);
1067	return(major);
1068}
1069
1070int
1071mm_ssh_gssapi_userok(char *user)
1072{
1073	Buffer m;
1074	int authenticated = 0;
1075
1076	buffer_init(&m);
1077
1078	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1079	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1080				  &m);
1081
1082	authenticated = buffer_get_int(&m);
1083
1084	buffer_free(&m);
1085	debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1086	return (authenticated);
1087}
1088#endif /* GSSAPI */
1089
1090