blob: 72fd5c83ca75ccbe129f5cb9a13182352da48035 [file] [log] [blame]
Damien Millerb84886b2008-05-19 15:05:07 +10001/* $OpenBSD: monitor_wrap.c,v 1.61 2008/05/08 12:02:23 djm Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
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"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029
Damien Miller9f2abc42006-07-10 20:53:08 +100030#include <sys/types.h>
Darren Tucker1a3d6e72006-08-05 18:46:47 +100031#include <sys/uio.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100032
Darren Tucker39972492006-07-12 22:22:46 +100033#include <errno.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100034#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100035#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100036#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100037#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100038#include <string.h>
Darren Tucker1a3d6e72006-08-05 18:46:47 +100039#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100040
Damien Millerd7834352006-08-05 12:39:39 +100041#include <openssl/bn.h>
42#include <openssl/dh.h>
43
Damien Millerb84886b2008-05-19 15:05:07 +100044#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100045#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000046#include "ssh.h"
47#include "dh.h"
Damien Millerd7834352006-08-05 12:39:39 +100048#include "buffer.h"
49#include "key.h"
50#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000051#include "kex.h"
Damien Millerd7834352006-08-05 12:39:39 +100052#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000053#include "auth.h"
Damien Miller06ebedf2003-02-24 12:03:38 +110054#include "auth-options.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000055#include "packet.h"
56#include "mac.h"
57#include "log.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000058#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
59#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000060#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000061#define TARGET_OS_MAC 1
62#else
63#include "zlib.h"
64#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000065#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +100066#ifdef GSSAPI
67#include "ssh-gss.h"
68#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000069#include "monitor_wrap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "atomicio.h"
71#include "monitor_fdpass.h"
Damien Miller3f941882006-03-31 23:13:02 +110072#include "misc.h"
Damien Miller4e448a32003-05-14 15:11:48 +100073#include "servconf.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000074
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000075#include "channels.h"
76#include "session.h"
Darren Tucker1629c072007-02-19 22:25:37 +110077#include "servconf.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000078
79/* Imports */
80extern int compat20;
81extern Newkeys *newkeys[];
82extern z_stream incoming_stream;
83extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000084extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000085extern Buffer input, output;
Darren Tucker09991742004-07-17 17:05:14 +100086extern Buffer loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100087extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000088
Darren Tucker3e33cec2003-10-02 16:12:36 +100089int
90mm_is_monitor(void)
91{
92 /*
93 * m_pid is only set in the privileged part, and
94 * points to the unprivileged child.
95 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +110096 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +100097}
98
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000099void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000100mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000102 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +0000103 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000104
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000105 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000106
Damien Miller3f941882006-03-31 23:13:02 +1100107 put_u32(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000108 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000109 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +1000110 fatal("%s: write: %s", __func__, strerror(errno));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000111 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +1000112 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000113}
114
115void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000116mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000117{
118 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000119 u_int msg_len;
120
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000121 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000122
Damien Millerb253cc42005-05-26 12:23:44 +1000123 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
124 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000125 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000126 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000127 }
Damien Miller3f941882006-03-31 23:13:02 +1100128 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000129 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000130 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000131 buffer_clear(m);
132 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000133 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
134 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000135}
136
137void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000138mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000139{
140 u_char rtype;
141
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000142 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000143
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000144 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000145 rtype = buffer_get_char(m);
146 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000147 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000148 rtype, type);
149}
150
151DH *
152mm_choose_dh(int min, int nbits, int max)
153{
154 BIGNUM *p, *g;
155 int success = 0;
156 Buffer m;
157
158 buffer_init(&m);
159 buffer_put_int(&m, min);
160 buffer_put_int(&m, nbits);
161 buffer_put_int(&m, max);
162
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000163 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000164
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000165 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000166 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000167
168 success = buffer_get_char(&m);
169 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000170 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171
172 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000173 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000174 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000175 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000176 buffer_get_bignum2(&m, p);
177 buffer_get_bignum2(&m, g);
178
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000179 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000180 buffer_free(&m);
181
182 return (dh_new_group(g, p));
183}
184
185int
186mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
187{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000188 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000189 Buffer m;
190
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000191 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000192
193 buffer_init(&m);
194 buffer_put_int(&m, kex->host_key_index(key));
195 buffer_put_string(&m, data, datalen);
196
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000197 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000198
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000199 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000200 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000201 *sigp = buffer_get_string(&m, lenp);
202 buffer_free(&m);
203
204 return (0);
205}
206
207struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000208mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000209{
210 Buffer m;
211 struct passwd *pw;
Darren Tucker1629c072007-02-19 22:25:37 +1100212 u_int len;
213 ServerOptions *newopts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000214
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000215 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000216
217 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000218 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000219
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000220 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000221
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000222 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000223 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000224
225 if (buffer_get_char(&m) == 0) {
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100226 pw = NULL;
227 goto out;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000228 }
Darren Tucker1629c072007-02-19 22:25:37 +1100229 pw = buffer_get_string(&m, &len);
230 if (len != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000231 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000232 pw->pw_name = buffer_get_string(&m, NULL);
233 pw->pw_passwd = buffer_get_string(&m, NULL);
234 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000235#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000236 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000237#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000238 pw->pw_dir = buffer_get_string(&m, NULL);
239 pw->pw_shell = buffer_get_string(&m, NULL);
Darren Tucker1629c072007-02-19 22:25:37 +1100240
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100241out:
Darren Tucker1629c072007-02-19 22:25:37 +1100242 /* copy options block as a Match directive may have changed some */
243 newopts = buffer_get_string(&m, &len);
244 if (len != sizeof(*newopts))
245 fatal("%s: option block size mismatch", __func__);
246 if (newopts->banner != NULL)
247 newopts->banner = buffer_get_string(&m, NULL);
248 copy_set_server_options(&options, newopts, 1);
249 xfree(newopts);
250
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000251 buffer_free(&m);
252
253 return (pw);
254}
255
Darren Tucker7eb3de02003-10-15 15:56:58 +1000256char *
257mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000258{
259 Buffer m;
260 char *banner;
261
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000262 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000263
264 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000265 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000266 buffer_clear(&m);
267
Darren Tucker7eb3de02003-10-15 15:56:58 +1000268 mm_request_receive_expect(pmonitor->m_recvfd,
269 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000270 banner = buffer_get_string(&m, NULL);
271 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000272
Darren Tucker7eb3de02003-10-15 15:56:58 +1000273 /* treat empty banner as missing banner */
274 if (strlen(banner) == 0) {
275 xfree(banner);
276 banner = NULL;
277 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000278 return (banner);
279}
280
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000281/* Inform the privileged process about service and style */
282
283void
284mm_inform_authserv(char *service, char *style)
285{
286 Buffer m;
287
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000288 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000289
290 buffer_init(&m);
291 buffer_put_cstring(&m, service);
292 buffer_put_cstring(&m, style ? style : "");
293
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000294 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000295
296 buffer_free(&m);
297}
298
299/* Do the password authentication */
300int
301mm_auth_password(Authctxt *authctxt, char *password)
302{
303 Buffer m;
304 int authenticated = 0;
305
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000306 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000307
308 buffer_init(&m);
309 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000310 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000311
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000312 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000313 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000314
315 authenticated = buffer_get_int(&m);
316
317 buffer_free(&m);
318
319 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000320 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000321 return (authenticated);
322}
323
324int
325mm_user_key_allowed(struct passwd *pw, Key *key)
326{
327 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
328}
329
330int
331mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
332 Key *key)
333{
334 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
335}
336
337int
338mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
339 char *host, Key *key)
340{
341 int ret;
342
343 key->type = KEY_RSA; /* XXX hack for key_to_blob */
344 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
345 key->type = KEY_RSA1;
346 return (ret);
347}
348
349static void
350mm_send_debug(Buffer *m)
351{
352 char *msg;
353
354 while (buffer_len(m)) {
355 msg = buffer_get_string(m, NULL);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000356 debug3("%s: Sending debug: %s", __func__, msg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357 packet_send_debug("%s", msg);
358 xfree(msg);
359 }
360}
361
362int
363mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
364{
365 Buffer m;
366 u_char *blob;
367 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100368 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000369
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000370 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000371
372 /* Convert the key to a blob and the pass it over */
373 if (!key_to_blob(key, &blob, &len))
374 return (0);
375
376 buffer_init(&m);
377 buffer_put_int(&m, type);
378 buffer_put_cstring(&m, user ? user : "");
379 buffer_put_cstring(&m, host ? host : "");
380 buffer_put_string(&m, blob, len);
381 xfree(blob);
382
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000383 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000384
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000385 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000386 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000387
388 allowed = buffer_get_int(&m);
389
Damien Miller06ebedf2003-02-24 12:03:38 +1100390 /* fake forced command */
391 auth_clear_options();
392 have_forced = buffer_get_int(&m);
393 forced_command = have_forced ? xstrdup("true") : NULL;
394
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000395 /* Send potential debug messages */
396 mm_send_debug(&m);
397
398 buffer_free(&m);
399
400 return (allowed);
401}
402
403/*
404 * This key verify needs to send the key type along, because the
405 * privileged parent makes the decision if the key is allowed
406 * for authentication.
407 */
408
409int
410mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
411{
412 Buffer m;
413 u_char *blob;
414 u_int len;
415 int verified = 0;
416
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000417 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000418
419 /* Convert the key to a blob and the pass it over */
420 if (!key_to_blob(key, &blob, &len))
421 return (0);
422
423 buffer_init(&m);
424 buffer_put_string(&m, blob, len);
425 buffer_put_string(&m, sig, siglen);
426 buffer_put_string(&m, data, datalen);
427 xfree(blob);
428
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000429 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000430
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000431 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000432 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000433
434 verified = buffer_get_int(&m);
435
436 buffer_free(&m);
437
438 return (verified);
439}
440
441/* Export key state after authentication */
442Newkeys *
443mm_newkeys_from_blob(u_char *blob, int blen)
444{
445 Buffer b;
446 u_int len;
447 Newkeys *newkey = NULL;
448 Enc *enc;
449 Mac *mac;
450 Comp *comp;
451
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000452 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000453#ifdef DEBUG_PK
454 dump_base64(stderr, blob, blen);
455#endif
456 buffer_init(&b);
457 buffer_append(&b, blob, blen);
458
459 newkey = xmalloc(sizeof(*newkey));
460 enc = &newkey->enc;
461 mac = &newkey->mac;
462 comp = &newkey->comp;
463
464 /* Enc structure */
465 enc->name = buffer_get_string(&b, NULL);
466 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
467 enc->enabled = buffer_get_int(&b);
468 enc->block_size = buffer_get_int(&b);
469 enc->key = buffer_get_string(&b, &enc->key_len);
470 enc->iv = buffer_get_string(&b, &len);
471 if (len != enc->block_size)
Ben Lindstrom08512492002-06-27 00:23:02 +0000472 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000473 enc->block_size, len);
474
475 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000476 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000477 enc->name, enc->cipher);
478
479 /* Mac structure */
480 mac->name = buffer_get_string(&b, NULL);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000481 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
Damien Millere45796f2007-06-11 14:01:42 +1000482 fatal("%s: can not setup mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000483 mac->enabled = buffer_get_int(&b);
484 mac->key = buffer_get_string(&b, &len);
485 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000486 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000487 mac->key_len);
488 mac->key_len = len;
489
490 /* Comp structure */
491 comp->type = buffer_get_int(&b);
492 comp->enabled = buffer_get_int(&b);
493 comp->name = buffer_get_string(&b, NULL);
494
495 len = buffer_len(&b);
496 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000497 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000498 buffer_free(&b);
499 return (newkey);
500}
501
502int
503mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
504{
505 Buffer b;
506 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000507 Enc *enc;
508 Mac *mac;
509 Comp *comp;
510 Newkeys *newkey = newkeys[mode];
511
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000512 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000513
514 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000515 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000516 return 0;
517 }
518 enc = &newkey->enc;
519 mac = &newkey->mac;
520 comp = &newkey->comp;
521
522 buffer_init(&b);
523 /* Enc structure */
524 buffer_put_cstring(&b, enc->name);
525 /* The cipher struct is constant and shared, you export pointer */
526 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
527 buffer_put_int(&b, enc->enabled);
528 buffer_put_int(&b, enc->block_size);
529 buffer_put_string(&b, enc->key, enc->key_len);
530 packet_get_keyiv(mode, enc->iv, enc->block_size);
531 buffer_put_string(&b, enc->iv, enc->block_size);
532
533 /* Mac structure */
534 buffer_put_cstring(&b, mac->name);
535 buffer_put_int(&b, mac->enabled);
536 buffer_put_string(&b, mac->key, mac->key_len);
537
538 /* Comp structure */
539 buffer_put_int(&b, comp->type);
540 buffer_put_int(&b, comp->enabled);
541 buffer_put_cstring(&b, comp->name);
542
543 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000544 if (lenp != NULL)
545 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000546 if (blobp != NULL) {
547 *blobp = xmalloc(len);
548 memcpy(*blobp, buffer_ptr(&b), len);
549 }
550 memset(buffer_ptr(&b), 0, len);
551 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000552 return len;
553}
554
555static void
556mm_send_kex(Buffer *m, Kex *kex)
557{
558 buffer_put_string(m, kex->session_id, kex->session_id_len);
559 buffer_put_int(m, kex->we_need);
560 buffer_put_int(m, kex->hostkey_type);
561 buffer_put_int(m, kex->kex_type);
562 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
563 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
564 buffer_put_int(m, kex->flags);
565 buffer_put_cstring(m, kex->client_version_string);
566 buffer_put_cstring(m, kex->server_version_string);
567}
568
569void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000570mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000571{
572 Buffer m;
573 u_char *blob, *p;
574 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000575 u_int32_t seqnr, packets;
576 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577
578 buffer_init(&m);
579
580 if (!compat20) {
581 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000582 u_char *key;
583 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000584
585 buffer_put_int(&m, packet_get_protocol_flags());
586
587 buffer_put_int(&m, packet_get_ssh1_cipher());
588
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000589 debug3("%s: Sending ssh1 KEY+IV", __func__);
590 keylen = packet_get_encryption_key(NULL);
591 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
592 keylen = packet_get_encryption_key(key);
593 buffer_put_string(&m, key, keylen);
594 memset(key, 0, keylen);
595 xfree(key);
596
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597 ivlen = packet_get_keyiv_len(MODE_OUT);
598 packet_get_keyiv(MODE_OUT, iv, ivlen);
599 buffer_put_string(&m, iv, ivlen);
600 ivlen = packet_get_keyiv_len(MODE_OUT);
601 packet_get_keyiv(MODE_IN, iv, ivlen);
602 buffer_put_string(&m, iv, ivlen);
603 goto skip;
604 } else {
605 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000606 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000607 }
608
609 debug3("%s: Sending new keys: %p %p",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000610 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000611
612 /* Keys from Kex */
613 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000614 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000615
616 buffer_put_string(&m, blob, bloblen);
617 xfree(blob);
618
619 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000620 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000621
622 buffer_put_string(&m, blob, bloblen);
623 xfree(blob);
624
Damien Millera5539d22003-04-09 20:50:06 +1000625 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
626 buffer_put_int(&m, seqnr);
627 buffer_put_int64(&m, blocks);
628 buffer_put_int(&m, packets);
Damien Millerb1ecd9c2003-04-09 20:51:24 +1000629 packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
Damien Millera5539d22003-04-09 20:50:06 +1000630 buffer_put_int(&m, seqnr);
631 buffer_put_int64(&m, blocks);
632 buffer_put_int(&m, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000633
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000634 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000635 skip:
636 /* More key context */
637 plen = packet_get_keycontext(MODE_OUT, NULL);
638 p = xmalloc(plen+1);
639 packet_get_keycontext(MODE_OUT, p);
640 buffer_put_string(&m, p, plen);
641 xfree(p);
642
643 plen = packet_get_keycontext(MODE_IN, NULL);
644 p = xmalloc(plen+1);
645 packet_get_keycontext(MODE_IN, p);
646 buffer_put_string(&m, p, plen);
647 xfree(p);
648
649 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000650 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000651 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
652 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
653
654 /* Network I/O buffers */
655 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
656 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
657
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000658 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000659 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000660
661 buffer_free(&m);
662}
663
664int
Damien Miller71a73672006-03-26 14:04:36 +1100665mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000666{
667 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000668 char *p, *msg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669 int success = 0;
670
671 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000672 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000673
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000674 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000675 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000676
677 success = buffer_get_int(&m);
678 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000679 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000680 buffer_free(&m);
681 return (0);
682 }
683 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000684 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685 buffer_free(&m);
686
687 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
688 xfree(p);
689
Darren Tucker09991742004-07-17 17:05:14 +1000690 buffer_append(&loginmsg, msg, strlen(msg));
691 xfree(msg);
692
Damien Miller54fd7cf2007-09-17 12:04:08 +1000693 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
694 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
695 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000696
697 /* Success */
698 return (1);
699}
700
701void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000702mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000703{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000704 Buffer m;
705
706 if (s->ttyfd == -1)
707 return;
708 buffer_init(&m);
709 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000710 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000711 buffer_free(&m);
712
713 /* closed dup'ed master */
714 if (close(s->ptymaster) < 0)
715 error("close(s->ptymaster): %s", strerror(errno));
716
717 /* unlink pty from session */
718 s->ttyfd = -1;
719}
720
Damien Miller79418552002-04-23 20:28:48 +1000721#ifdef USE_PAM
722void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100723mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000724{
725 Buffer m;
726
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000727 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000728 if (!options.use_pam)
729 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000730
731 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000732 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000733
734 buffer_free(&m);
735}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000736
Damien Miller1f499fd2003-08-25 13:08:49 +1000737u_int
738mm_do_pam_account(void)
739{
740 Buffer m;
741 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000742 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000743
744 debug3("%s entering", __func__);
745 if (!options.use_pam)
746 fatal("UsePAM=no, but ended up in %s anyway", __func__);
747
748 buffer_init(&m);
749 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
750
Damien Millera8e06ce2003-11-21 23:48:55 +1100751 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000752 MONITOR_ANS_PAM_ACCOUNT, &m);
753 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000754 msg = buffer_get_string(&m, NULL);
755 buffer_append(&loginmsg, msg, strlen(msg));
756 xfree(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000757
758 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100759
Damien Miller1f499fd2003-08-25 13:08:49 +1000760 debug3("%s returning %d", __func__, ret);
761
762 return (ret);
763}
764
Damien Miller4f9f42a2003-05-10 19:28:02 +1000765void *
766mm_sshpam_init_ctx(Authctxt *authctxt)
767{
768 Buffer m;
769 int success;
770
771 debug3("%s", __func__);
772 buffer_init(&m);
773 buffer_put_cstring(&m, authctxt->user);
774 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
775 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
776 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
777 success = buffer_get_int(&m);
778 if (success == 0) {
779 debug3("%s: pam_init_ctx failed", __func__);
780 buffer_free(&m);
781 return (NULL);
782 }
783 buffer_free(&m);
784 return (authctxt);
785}
786
787int
788mm_sshpam_query(void *ctx, char **name, char **info,
789 u_int *num, char ***prompts, u_int **echo_on)
790{
791 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000792 u_int i;
793 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000794
795 debug3("%s", __func__);
796 buffer_init(&m);
797 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
798 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
799 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
800 ret = buffer_get_int(&m);
801 debug3("%s: pam_query returned %d", __func__, ret);
802 *name = buffer_get_string(&m, NULL);
803 *info = buffer_get_string(&m, NULL);
804 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000805 if (*num > PAM_MAX_NUM_MSG)
806 fatal("%s: recieved %u PAM messages, expected <= %u",
807 __func__, *num, PAM_MAX_NUM_MSG);
808 *prompts = xcalloc((*num + 1), sizeof(char *));
809 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000810 for (i = 0; i < *num; ++i) {
811 (*prompts)[i] = buffer_get_string(&m, NULL);
812 (*echo_on)[i] = buffer_get_int(&m);
813 }
814 buffer_free(&m);
815 return (ret);
816}
817
818int
819mm_sshpam_respond(void *ctx, u_int num, char **resp)
820{
821 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000822 u_int i;
823 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000824
825 debug3("%s", __func__);
826 buffer_init(&m);
827 buffer_put_int(&m, num);
828 for (i = 0; i < num; ++i)
829 buffer_put_cstring(&m, resp[i]);
830 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
831 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
832 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
833 ret = buffer_get_int(&m);
834 debug3("%s: pam_respond returned %d", __func__, ret);
835 buffer_free(&m);
836 return (ret);
837}
838
839void
840mm_sshpam_free_ctx(void *ctxtp)
841{
842 Buffer m;
843
844 debug3("%s", __func__);
845 buffer_init(&m);
846 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
847 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
848 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
849 buffer_free(&m);
850}
Damien Miller79418552002-04-23 20:28:48 +1000851#endif /* USE_PAM */
852
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000853/* Request process termination */
854
855void
856mm_terminate(void)
857{
858 Buffer m;
859
860 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000861 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000862 buffer_free(&m);
863}
864
865int
866mm_ssh1_session_key(BIGNUM *num)
867{
868 int rsafail;
869 Buffer m;
870
871 buffer_init(&m);
872 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000873 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000874
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000875 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000876
877 rsafail = buffer_get_int(&m);
878 buffer_get_bignum2(&m, num);
879
880 buffer_free(&m);
881
882 return (rsafail);
883}
884
885static void
886mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
887 char ***prompts, u_int **echo_on)
888{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000889 *name = xstrdup("");
890 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000891 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100892 *prompts = xcalloc(*numprompts, sizeof(char *));
893 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000894 (*echo_on)[0] = 0;
895}
896
897int
898mm_bsdauth_query(void *ctx, char **name, char **infotxt,
899 u_int *numprompts, char ***prompts, u_int **echo_on)
900{
901 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100902 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000903 char *challenge;
904
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000905 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000906
907 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000908 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000910 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000911 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100912 success = buffer_get_int(&m);
913 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000914 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000915 buffer_free(&m);
916 return (-1);
917 }
918
919 /* Get the challenge, and format the response */
920 challenge = buffer_get_string(&m, NULL);
921 buffer_free(&m);
922
923 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
924 (*prompts)[0] = challenge;
925
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000926 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000927
928 return (0);
929}
930
931int
932mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
933{
934 Buffer m;
935 int authok;
936
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000937 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000938 if (numresponses != 1)
939 return (-1);
940
941 buffer_init(&m);
942 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000943 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000944
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000945 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000946 MONITOR_ANS_BSDAUTHRESPOND, &m);
947
948 authok = buffer_get_int(&m);
949 buffer_free(&m);
950
951 return ((authok == 0) ? -1 : 0);
952}
953
Darren Tucker042e2e82004-07-08 23:09:42 +1000954#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000955int
956mm_skey_query(void *ctx, char **name, char **infotxt,
957 u_int *numprompts, char ***prompts, u_int **echo_on)
958{
959 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100960 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +1000961 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000962
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000963 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000964
965 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000966 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000967
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000968 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000969 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100970 success = buffer_get_int(&m);
971 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000972 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000973 buffer_free(&m);
974 return (-1);
975 }
976
977 /* Get the challenge, and format the response */
978 challenge = buffer_get_string(&m, NULL);
979 buffer_free(&m);
980
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000981 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000982
983 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
984
Damien Miller07d86be2006-03-26 14:19:21 +1100985 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000986 xfree(challenge);
987
988 return (0);
989}
990
991int
992mm_skey_respond(void *ctx, u_int numresponses, char **responses)
993{
994 Buffer m;
995 int authok;
996
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000997 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000998 if (numresponses != 1)
999 return (-1);
1000
1001 buffer_init(&m);
1002 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001003 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001004
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001005 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001006 MONITOR_ANS_SKEYRESPOND, &m);
1007
1008 authok = buffer_get_int(&m);
1009 buffer_free(&m);
1010
1011 return ((authok == 0) ? -1 : 0);
1012}
Darren Tucker042e2e82004-07-08 23:09:42 +10001013#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001014
1015void
1016mm_ssh1_session_id(u_char session_id[16])
1017{
1018 Buffer m;
1019 int i;
1020
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001021 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001022
1023 buffer_init(&m);
1024 for (i = 0; i < 16; i++)
1025 buffer_put_char(&m, session_id[i]);
1026
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001027 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001028 buffer_free(&m);
1029}
1030
1031int
1032mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1033{
1034 Buffer m;
1035 Key *key;
1036 u_char *blob;
1037 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001038 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001039
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001040 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001041
1042 buffer_init(&m);
1043 buffer_put_bignum2(&m, client_n);
1044
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001045 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1046 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001047
1048 allowed = buffer_get_int(&m);
1049
Damien Miller06ebedf2003-02-24 12:03:38 +11001050 /* fake forced command */
1051 auth_clear_options();
1052 have_forced = buffer_get_int(&m);
1053 forced_command = have_forced ? xstrdup("true") : NULL;
1054
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001055 if (allowed && rkey != NULL) {
1056 blob = buffer_get_string(&m, &blen);
1057 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001058 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001059 *rkey = key;
1060 xfree(blob);
1061 }
1062 mm_send_debug(&m);
1063 buffer_free(&m);
1064
1065 return (allowed);
1066}
1067
1068BIGNUM *
1069mm_auth_rsa_generate_challenge(Key *key)
1070{
1071 Buffer m;
1072 BIGNUM *challenge;
1073 u_char *blob;
1074 u_int blen;
1075
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001076 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001077
1078 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001079 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001080
1081 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1082 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001083 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001084 key->type = KEY_RSA1;
1085
1086 buffer_init(&m);
1087 buffer_put_string(&m, blob, blen);
1088 xfree(blob);
1089
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001090 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1091 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001092
1093 buffer_get_bignum2(&m, challenge);
1094 buffer_free(&m);
1095
1096 return (challenge);
1097}
1098
1099int
1100mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1101{
1102 Buffer m;
1103 u_char *blob;
1104 u_int blen;
1105 int success = 0;
1106
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001107 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001108
1109 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1110 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001111 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001112 key->type = KEY_RSA1;
1113
1114 buffer_init(&m);
1115 buffer_put_string(&m, blob, blen);
1116 buffer_put_string(&m, response, 16);
1117 xfree(blob);
1118
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001119 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1120 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001121
1122 success = buffer_get_int(&m);
1123 buffer_free(&m);
1124
1125 return (success);
1126}
Damien Miller25162f22002-09-12 09:47:29 +10001127
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001128#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001129void
1130mm_audit_event(ssh_audit_event_t event)
1131{
1132 Buffer m;
1133
1134 debug3("%s entering", __func__);
1135
1136 buffer_init(&m);
1137 buffer_put_int(&m, event);
1138
1139 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1140 buffer_free(&m);
1141}
1142
1143void
1144mm_audit_run_command(const char *command)
1145{
1146 Buffer m;
1147
1148 debug3("%s entering command %s", __func__, command);
1149
1150 buffer_init(&m);
1151 buffer_put_cstring(&m, command);
1152
1153 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1154 buffer_free(&m);
1155}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001156#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001157
Darren Tucker0efd1552003-08-26 11:49:55 +10001158#ifdef GSSAPI
1159OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001160mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001161{
1162 Buffer m;
1163 OM_uint32 major;
1164
1165 /* Client doesn't get to see the context */
1166 *ctx = NULL;
1167
1168 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001169 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001170
1171 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1172 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1173
1174 major = buffer_get_int(&m);
1175
1176 buffer_free(&m);
1177 return (major);
1178}
1179
1180OM_uint32
1181mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1182 gss_buffer_desc *out, OM_uint32 *flags)
1183{
1184 Buffer m;
1185 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001186 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001187
1188 buffer_init(&m);
1189 buffer_put_string(&m, in->value, in->length);
1190
1191 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1192 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1193
1194 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001195 out->value = buffer_get_string(&m, &len);
1196 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001197 if (flags)
1198 *flags = buffer_get_int(&m);
1199
1200 buffer_free(&m);
1201
1202 return (major);
1203}
1204
Damien Miller0425d402003-11-17 22:18:21 +11001205OM_uint32
1206mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1207{
1208 Buffer m;
1209 OM_uint32 major;
1210
1211 buffer_init(&m);
1212 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1213 buffer_put_string(&m, gssmic->value, gssmic->length);
1214
1215 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1216 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1217 &m);
1218
1219 major = buffer_get_int(&m);
1220 buffer_free(&m);
1221 return(major);
1222}
1223
Darren Tucker0efd1552003-08-26 11:49:55 +10001224int
1225mm_ssh_gssapi_userok(char *user)
1226{
1227 Buffer m;
1228 int authenticated = 0;
1229
1230 buffer_init(&m);
1231
1232 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1233 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1234 &m);
1235
1236 authenticated = buffer_get_int(&m);
1237
1238 buffer_free(&m);
1239 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1240 return (authenticated);
1241}
1242#endif /* GSSAPI */