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