blob: 22b1fe8545d314b5936b382dbc11208f6aa68a01 [file] [log] [blame]
Damien Miller9f2abc42006-07-10 20:53:08 +10001/* $OpenBSD: monitor_wrap.c,v 1.46 2006/07/06 16:03:53 stevesk 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>
31
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000032#include <openssl/bn.h>
33#include <openssl/dh.h>
34
Damien Miller9f2abc42006-07-10 20:53:08 +100035#include <pwd.h>
36
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000037#include "ssh.h"
38#include "dh.h"
39#include "kex.h"
40#include "auth.h"
Damien Miller06ebedf2003-02-24 12:03:38 +110041#include "auth-options.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000042#include "buffer.h"
43#include "bufaux.h"
44#include "packet.h"
45#include "mac.h"
46#include "log.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000047#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
48#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000049#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000050#define TARGET_OS_MAC 1
51#else
52#include "zlib.h"
53#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000054#include "monitor.h"
55#include "monitor_wrap.h"
56#include "xmalloc.h"
57#include "atomicio.h"
58#include "monitor_fdpass.h"
Damien Miller3f941882006-03-31 23:13:02 +110059#include "misc.h"
Damien Miller4e448a32003-05-14 15:11:48 +100060#include "servconf.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000061
62#include "auth.h"
63#include "channels.h"
64#include "session.h"
65
Darren Tucker0efd1552003-08-26 11:49:55 +100066#ifdef GSSAPI
67#include "ssh-gss.h"
68#endif
69
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070/* Imports */
71extern int compat20;
72extern Newkeys *newkeys[];
73extern z_stream incoming_stream;
74extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000075extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000076extern Buffer input, output;
Darren Tucker09991742004-07-17 17:05:14 +100077extern Buffer loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100078extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000079
Darren Tucker3e33cec2003-10-02 16:12:36 +100080int
81mm_is_monitor(void)
82{
83 /*
84 * m_pid is only set in the privileged part, and
85 * points to the unprivileged child.
86 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +110087 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +100088}
89
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000090void
Darren Tucker3f9fdc72004-06-22 12:56:01 +100091mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000092{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +000094 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000096 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000097
Damien Miller3f941882006-03-31 23:13:02 +110098 put_u32(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +000099 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000100 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +1000101 fatal("%s: write: %s", __func__, strerror(errno));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000102 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +1000103 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000104}
105
106void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000107mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000108{
109 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000110 u_int msg_len;
111
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000112 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000113
Damien Millerb253cc42005-05-26 12:23:44 +1000114 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
115 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000116 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000117 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000118 }
Damien Miller3f941882006-03-31 23:13:02 +1100119 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000120 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000121 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000122 buffer_clear(m);
123 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000124 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
125 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000126}
127
128void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000129mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000130{
131 u_char rtype;
132
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000133 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000134
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000135 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000136 rtype = buffer_get_char(m);
137 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000138 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000139 rtype, type);
140}
141
142DH *
143mm_choose_dh(int min, int nbits, int max)
144{
145 BIGNUM *p, *g;
146 int success = 0;
147 Buffer m;
148
149 buffer_init(&m);
150 buffer_put_int(&m, min);
151 buffer_put_int(&m, nbits);
152 buffer_put_int(&m, max);
153
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000154 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000155
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000156 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000157 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000158
159 success = buffer_get_char(&m);
160 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000161 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000162
163 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000164 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000165 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000166 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000167 buffer_get_bignum2(&m, p);
168 buffer_get_bignum2(&m, g);
169
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000170 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171 buffer_free(&m);
172
173 return (dh_new_group(g, p));
174}
175
176int
177mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
178{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000179 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000180 Buffer m;
181
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000182 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000183
184 buffer_init(&m);
185 buffer_put_int(&m, kex->host_key_index(key));
186 buffer_put_string(&m, data, datalen);
187
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000188 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000189
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000190 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000191 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000192 *sigp = buffer_get_string(&m, lenp);
193 buffer_free(&m);
194
195 return (0);
196}
197
198struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000199mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000200{
201 Buffer m;
202 struct passwd *pw;
203 u_int pwlen;
204
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000205 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206
207 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000208 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000209
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000210 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000211
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000212 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000213 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000214
215 if (buffer_get_char(&m) == 0) {
216 buffer_free(&m);
217 return (NULL);
218 }
219 pw = buffer_get_string(&m, &pwlen);
220 if (pwlen != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000221 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000222 pw->pw_name = buffer_get_string(&m, NULL);
223 pw->pw_passwd = buffer_get_string(&m, NULL);
224 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000225#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000226 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000227#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000228 pw->pw_dir = buffer_get_string(&m, NULL);
229 pw->pw_shell = buffer_get_string(&m, NULL);
230 buffer_free(&m);
231
232 return (pw);
233}
234
Darren Tucker7eb3de02003-10-15 15:56:58 +1000235char *
236mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000237{
238 Buffer m;
239 char *banner;
240
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000241 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000242
243 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000244 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000245 buffer_clear(&m);
246
Darren Tucker7eb3de02003-10-15 15:56:58 +1000247 mm_request_receive_expect(pmonitor->m_recvfd,
248 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000249 banner = buffer_get_string(&m, NULL);
250 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000251
Darren Tucker7eb3de02003-10-15 15:56:58 +1000252 /* treat empty banner as missing banner */
253 if (strlen(banner) == 0) {
254 xfree(banner);
255 banner = NULL;
256 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000257 return (banner);
258}
259
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000260/* Inform the privileged process about service and style */
261
262void
263mm_inform_authserv(char *service, char *style)
264{
265 Buffer m;
266
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000267 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000268
269 buffer_init(&m);
270 buffer_put_cstring(&m, service);
271 buffer_put_cstring(&m, style ? style : "");
272
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000273 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000274
275 buffer_free(&m);
276}
277
278/* Do the password authentication */
279int
280mm_auth_password(Authctxt *authctxt, char *password)
281{
282 Buffer m;
283 int authenticated = 0;
284
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000285 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000286
287 buffer_init(&m);
288 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000289 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000290
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000291 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000292 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000293
294 authenticated = buffer_get_int(&m);
295
296 buffer_free(&m);
297
298 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000299 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000300 return (authenticated);
301}
302
303int
304mm_user_key_allowed(struct passwd *pw, Key *key)
305{
306 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
307}
308
309int
310mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
311 Key *key)
312{
313 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
314}
315
316int
317mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
318 char *host, Key *key)
319{
320 int ret;
321
322 key->type = KEY_RSA; /* XXX hack for key_to_blob */
323 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
324 key->type = KEY_RSA1;
325 return (ret);
326}
327
328static void
329mm_send_debug(Buffer *m)
330{
331 char *msg;
332
333 while (buffer_len(m)) {
334 msg = buffer_get_string(m, NULL);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000335 debug3("%s: Sending debug: %s", __func__, msg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000336 packet_send_debug("%s", msg);
337 xfree(msg);
338 }
339}
340
341int
342mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
343{
344 Buffer m;
345 u_char *blob;
346 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100347 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000349 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000350
351 /* Convert the key to a blob and the pass it over */
352 if (!key_to_blob(key, &blob, &len))
353 return (0);
354
355 buffer_init(&m);
356 buffer_put_int(&m, type);
357 buffer_put_cstring(&m, user ? user : "");
358 buffer_put_cstring(&m, host ? host : "");
359 buffer_put_string(&m, blob, len);
360 xfree(blob);
361
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000362 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000363
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000364 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000365 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000366
367 allowed = buffer_get_int(&m);
368
Damien Miller06ebedf2003-02-24 12:03:38 +1100369 /* fake forced command */
370 auth_clear_options();
371 have_forced = buffer_get_int(&m);
372 forced_command = have_forced ? xstrdup("true") : NULL;
373
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000374 /* Send potential debug messages */
375 mm_send_debug(&m);
376
377 buffer_free(&m);
378
379 return (allowed);
380}
381
382/*
383 * This key verify needs to send the key type along, because the
384 * privileged parent makes the decision if the key is allowed
385 * for authentication.
386 */
387
388int
389mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
390{
391 Buffer m;
392 u_char *blob;
393 u_int len;
394 int verified = 0;
395
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000396 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000397
398 /* Convert the key to a blob and the pass it over */
399 if (!key_to_blob(key, &blob, &len))
400 return (0);
401
402 buffer_init(&m);
403 buffer_put_string(&m, blob, len);
404 buffer_put_string(&m, sig, siglen);
405 buffer_put_string(&m, data, datalen);
406 xfree(blob);
407
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000408 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000409
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000410 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000411 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000412
413 verified = buffer_get_int(&m);
414
415 buffer_free(&m);
416
417 return (verified);
418}
419
420/* Export key state after authentication */
421Newkeys *
422mm_newkeys_from_blob(u_char *blob, int blen)
423{
424 Buffer b;
425 u_int len;
426 Newkeys *newkey = NULL;
427 Enc *enc;
428 Mac *mac;
429 Comp *comp;
430
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000431 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000432#ifdef DEBUG_PK
433 dump_base64(stderr, blob, blen);
434#endif
435 buffer_init(&b);
436 buffer_append(&b, blob, blen);
437
438 newkey = xmalloc(sizeof(*newkey));
439 enc = &newkey->enc;
440 mac = &newkey->mac;
441 comp = &newkey->comp;
442
443 /* Enc structure */
444 enc->name = buffer_get_string(&b, NULL);
445 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
446 enc->enabled = buffer_get_int(&b);
447 enc->block_size = buffer_get_int(&b);
448 enc->key = buffer_get_string(&b, &enc->key_len);
449 enc->iv = buffer_get_string(&b, &len);
450 if (len != enc->block_size)
Ben Lindstrom08512492002-06-27 00:23:02 +0000451 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000452 enc->block_size, len);
453
454 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000455 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000456 enc->name, enc->cipher);
457
458 /* Mac structure */
459 mac->name = buffer_get_string(&b, NULL);
460 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000461 fatal("%s: can not init mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000462 mac->enabled = buffer_get_int(&b);
463 mac->key = buffer_get_string(&b, &len);
464 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000465 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000466 mac->key_len);
467 mac->key_len = len;
468
469 /* Comp structure */
470 comp->type = buffer_get_int(&b);
471 comp->enabled = buffer_get_int(&b);
472 comp->name = buffer_get_string(&b, NULL);
473
474 len = buffer_len(&b);
475 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000476 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000477 buffer_free(&b);
478 return (newkey);
479}
480
481int
482mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
483{
484 Buffer b;
485 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000486 Enc *enc;
487 Mac *mac;
488 Comp *comp;
489 Newkeys *newkey = newkeys[mode];
490
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000491 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000492
493 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000494 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000495 return 0;
496 }
497 enc = &newkey->enc;
498 mac = &newkey->mac;
499 comp = &newkey->comp;
500
501 buffer_init(&b);
502 /* Enc structure */
503 buffer_put_cstring(&b, enc->name);
504 /* The cipher struct is constant and shared, you export pointer */
505 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
506 buffer_put_int(&b, enc->enabled);
507 buffer_put_int(&b, enc->block_size);
508 buffer_put_string(&b, enc->key, enc->key_len);
509 packet_get_keyiv(mode, enc->iv, enc->block_size);
510 buffer_put_string(&b, enc->iv, enc->block_size);
511
512 /* Mac structure */
513 buffer_put_cstring(&b, mac->name);
514 buffer_put_int(&b, mac->enabled);
515 buffer_put_string(&b, mac->key, mac->key_len);
516
517 /* Comp structure */
518 buffer_put_int(&b, comp->type);
519 buffer_put_int(&b, comp->enabled);
520 buffer_put_cstring(&b, comp->name);
521
522 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523 if (lenp != NULL)
524 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000525 if (blobp != NULL) {
526 *blobp = xmalloc(len);
527 memcpy(*blobp, buffer_ptr(&b), len);
528 }
529 memset(buffer_ptr(&b), 0, len);
530 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000531 return len;
532}
533
534static void
535mm_send_kex(Buffer *m, Kex *kex)
536{
537 buffer_put_string(m, kex->session_id, kex->session_id_len);
538 buffer_put_int(m, kex->we_need);
539 buffer_put_int(m, kex->hostkey_type);
540 buffer_put_int(m, kex->kex_type);
541 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
542 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
543 buffer_put_int(m, kex->flags);
544 buffer_put_cstring(m, kex->client_version_string);
545 buffer_put_cstring(m, kex->server_version_string);
546}
547
548void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000549mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000550{
551 Buffer m;
552 u_char *blob, *p;
553 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000554 u_int32_t seqnr, packets;
555 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000556
557 buffer_init(&m);
558
559 if (!compat20) {
560 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000561 u_char *key;
562 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000563
564 buffer_put_int(&m, packet_get_protocol_flags());
565
566 buffer_put_int(&m, packet_get_ssh1_cipher());
567
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000568 debug3("%s: Sending ssh1 KEY+IV", __func__);
569 keylen = packet_get_encryption_key(NULL);
570 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
571 keylen = packet_get_encryption_key(key);
572 buffer_put_string(&m, key, keylen);
573 memset(key, 0, keylen);
574 xfree(key);
575
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000576 ivlen = packet_get_keyiv_len(MODE_OUT);
577 packet_get_keyiv(MODE_OUT, iv, ivlen);
578 buffer_put_string(&m, iv, ivlen);
579 ivlen = packet_get_keyiv_len(MODE_OUT);
580 packet_get_keyiv(MODE_IN, iv, ivlen);
581 buffer_put_string(&m, iv, ivlen);
582 goto skip;
583 } else {
584 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000585 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586 }
587
588 debug3("%s: Sending new keys: %p %p",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000589 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000590
591 /* Keys from Kex */
592 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000593 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594
595 buffer_put_string(&m, blob, bloblen);
596 xfree(blob);
597
598 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000599 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000600
601 buffer_put_string(&m, blob, bloblen);
602 xfree(blob);
603
Damien Millera5539d22003-04-09 20:50:06 +1000604 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
605 buffer_put_int(&m, seqnr);
606 buffer_put_int64(&m, blocks);
607 buffer_put_int(&m, packets);
Damien Millerb1ecd9c2003-04-09 20:51:24 +1000608 packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
Damien Millera5539d22003-04-09 20:50:06 +1000609 buffer_put_int(&m, seqnr);
610 buffer_put_int64(&m, blocks);
611 buffer_put_int(&m, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000612
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000613 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000614 skip:
615 /* More key context */
616 plen = packet_get_keycontext(MODE_OUT, NULL);
617 p = xmalloc(plen+1);
618 packet_get_keycontext(MODE_OUT, p);
619 buffer_put_string(&m, p, plen);
620 xfree(p);
621
622 plen = packet_get_keycontext(MODE_IN, NULL);
623 p = xmalloc(plen+1);
624 packet_get_keycontext(MODE_IN, p);
625 buffer_put_string(&m, p, plen);
626 xfree(p);
627
628 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000629 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000630 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
631 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
632
633 /* Network I/O buffers */
634 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
635 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
636
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000637 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000638 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000639
640 buffer_free(&m);
641}
642
643int
Damien Miller71a73672006-03-26 14:04:36 +1100644mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000645{
646 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000647 char *p, *msg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000648 int success = 0;
649
650 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000651 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000652
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000653 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000654 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000655
656 success = buffer_get_int(&m);
657 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000658 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000659 buffer_free(&m);
660 return (0);
661 }
662 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000663 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000664 buffer_free(&m);
665
666 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
667 xfree(p);
668
Darren Tucker09991742004-07-17 17:05:14 +1000669 buffer_append(&loginmsg, msg, strlen(msg));
670 xfree(msg);
671
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000672 *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
673 *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000674
675 /* Success */
676 return (1);
677}
678
679void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000680mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000681{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000682 Buffer m;
683
684 if (s->ttyfd == -1)
685 return;
686 buffer_init(&m);
687 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000688 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000689 buffer_free(&m);
690
691 /* closed dup'ed master */
692 if (close(s->ptymaster) < 0)
693 error("close(s->ptymaster): %s", strerror(errno));
694
695 /* unlink pty from session */
696 s->ttyfd = -1;
697}
698
Damien Miller79418552002-04-23 20:28:48 +1000699#ifdef USE_PAM
700void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100701mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000702{
703 Buffer m;
704
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000705 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000706 if (!options.use_pam)
707 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000708
709 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000710 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000711
712 buffer_free(&m);
713}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000714
Damien Miller1f499fd2003-08-25 13:08:49 +1000715u_int
716mm_do_pam_account(void)
717{
718 Buffer m;
719 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000720 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000721
722 debug3("%s entering", __func__);
723 if (!options.use_pam)
724 fatal("UsePAM=no, but ended up in %s anyway", __func__);
725
726 buffer_init(&m);
727 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
728
Damien Millera8e06ce2003-11-21 23:48:55 +1100729 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000730 MONITOR_ANS_PAM_ACCOUNT, &m);
731 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000732 msg = buffer_get_string(&m, NULL);
733 buffer_append(&loginmsg, msg, strlen(msg));
734 xfree(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000735
736 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100737
Damien Miller1f499fd2003-08-25 13:08:49 +1000738 debug3("%s returning %d", __func__, ret);
739
740 return (ret);
741}
742
Damien Miller4f9f42a2003-05-10 19:28:02 +1000743void *
744mm_sshpam_init_ctx(Authctxt *authctxt)
745{
746 Buffer m;
747 int success;
748
749 debug3("%s", __func__);
750 buffer_init(&m);
751 buffer_put_cstring(&m, authctxt->user);
752 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
753 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
754 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
755 success = buffer_get_int(&m);
756 if (success == 0) {
757 debug3("%s: pam_init_ctx failed", __func__);
758 buffer_free(&m);
759 return (NULL);
760 }
761 buffer_free(&m);
762 return (authctxt);
763}
764
765int
766mm_sshpam_query(void *ctx, char **name, char **info,
767 u_int *num, char ***prompts, u_int **echo_on)
768{
769 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000770 u_int i;
771 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000772
773 debug3("%s", __func__);
774 buffer_init(&m);
775 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
776 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
777 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
778 ret = buffer_get_int(&m);
779 debug3("%s: pam_query returned %d", __func__, ret);
780 *name = buffer_get_string(&m, NULL);
781 *info = buffer_get_string(&m, NULL);
782 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000783 if (*num > PAM_MAX_NUM_MSG)
784 fatal("%s: recieved %u PAM messages, expected <= %u",
785 __func__, *num, PAM_MAX_NUM_MSG);
786 *prompts = xcalloc((*num + 1), sizeof(char *));
787 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000788 for (i = 0; i < *num; ++i) {
789 (*prompts)[i] = buffer_get_string(&m, NULL);
790 (*echo_on)[i] = buffer_get_int(&m);
791 }
792 buffer_free(&m);
793 return (ret);
794}
795
796int
797mm_sshpam_respond(void *ctx, u_int num, char **resp)
798{
799 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000800 u_int i;
801 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000802
803 debug3("%s", __func__);
804 buffer_init(&m);
805 buffer_put_int(&m, num);
806 for (i = 0; i < num; ++i)
807 buffer_put_cstring(&m, resp[i]);
808 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
809 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
810 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
811 ret = buffer_get_int(&m);
812 debug3("%s: pam_respond returned %d", __func__, ret);
813 buffer_free(&m);
814 return (ret);
815}
816
817void
818mm_sshpam_free_ctx(void *ctxtp)
819{
820 Buffer m;
821
822 debug3("%s", __func__);
823 buffer_init(&m);
824 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
825 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
826 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
827 buffer_free(&m);
828}
Damien Miller79418552002-04-23 20:28:48 +1000829#endif /* USE_PAM */
830
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000831/* Request process termination */
832
833void
834mm_terminate(void)
835{
836 Buffer m;
837
838 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000839 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000840 buffer_free(&m);
841}
842
843int
844mm_ssh1_session_key(BIGNUM *num)
845{
846 int rsafail;
847 Buffer m;
848
849 buffer_init(&m);
850 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000851 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000852
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000853 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000854
855 rsafail = buffer_get_int(&m);
856 buffer_get_bignum2(&m, num);
857
858 buffer_free(&m);
859
860 return (rsafail);
861}
862
863static void
864mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
865 char ***prompts, u_int **echo_on)
866{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000867 *name = xstrdup("");
868 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000869 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100870 *prompts = xcalloc(*numprompts, sizeof(char *));
871 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000872 (*echo_on)[0] = 0;
873}
874
875int
876mm_bsdauth_query(void *ctx, char **name, char **infotxt,
877 u_int *numprompts, char ***prompts, u_int **echo_on)
878{
879 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100880 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000881 char *challenge;
882
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000883 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000884
885 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000886 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000887
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000888 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000889 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100890 success = buffer_get_int(&m);
891 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000892 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000893 buffer_free(&m);
894 return (-1);
895 }
896
897 /* Get the challenge, and format the response */
898 challenge = buffer_get_string(&m, NULL);
899 buffer_free(&m);
900
901 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
902 (*prompts)[0] = challenge;
903
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000904 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000905
906 return (0);
907}
908
909int
910mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
911{
912 Buffer m;
913 int authok;
914
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000915 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000916 if (numresponses != 1)
917 return (-1);
918
919 buffer_init(&m);
920 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000921 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000922
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000923 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000924 MONITOR_ANS_BSDAUTHRESPOND, &m);
925
926 authok = buffer_get_int(&m);
927 buffer_free(&m);
928
929 return ((authok == 0) ? -1 : 0);
930}
931
Darren Tucker042e2e82004-07-08 23:09:42 +1000932#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000933int
934mm_skey_query(void *ctx, char **name, char **infotxt,
935 u_int *numprompts, char ***prompts, u_int **echo_on)
936{
937 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100938 int len;
939 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000940 char *p, *challenge;
941
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000942 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000943
944 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000945 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000946
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000947 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000948 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100949 success = buffer_get_int(&m);
950 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000951 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000952 buffer_free(&m);
953 return (-1);
954 }
955
956 /* Get the challenge, and format the response */
957 challenge = buffer_get_string(&m, NULL);
958 buffer_free(&m);
959
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000960 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961
962 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
963
Damien Miller07d86be2006-03-26 14:19:21 +1100964 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000965 xfree(challenge);
966
967 return (0);
968}
969
970int
971mm_skey_respond(void *ctx, u_int numresponses, char **responses)
972{
973 Buffer m;
974 int authok;
975
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000976 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000977 if (numresponses != 1)
978 return (-1);
979
980 buffer_init(&m);
981 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000982 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000983
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000984 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000985 MONITOR_ANS_SKEYRESPOND, &m);
986
987 authok = buffer_get_int(&m);
988 buffer_free(&m);
989
990 return ((authok == 0) ? -1 : 0);
991}
Darren Tucker042e2e82004-07-08 23:09:42 +1000992#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000993
994void
995mm_ssh1_session_id(u_char session_id[16])
996{
997 Buffer m;
998 int i;
999
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001000 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001001
1002 buffer_init(&m);
1003 for (i = 0; i < 16; i++)
1004 buffer_put_char(&m, session_id[i]);
1005
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001006 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001007 buffer_free(&m);
1008}
1009
1010int
1011mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1012{
1013 Buffer m;
1014 Key *key;
1015 u_char *blob;
1016 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001017 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001018
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001019 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001020
1021 buffer_init(&m);
1022 buffer_put_bignum2(&m, client_n);
1023
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001024 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1025 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001026
1027 allowed = buffer_get_int(&m);
1028
Damien Miller06ebedf2003-02-24 12:03:38 +11001029 /* fake forced command */
1030 auth_clear_options();
1031 have_forced = buffer_get_int(&m);
1032 forced_command = have_forced ? xstrdup("true") : NULL;
1033
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001034 if (allowed && rkey != NULL) {
1035 blob = buffer_get_string(&m, &blen);
1036 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001037 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001038 *rkey = key;
1039 xfree(blob);
1040 }
1041 mm_send_debug(&m);
1042 buffer_free(&m);
1043
1044 return (allowed);
1045}
1046
1047BIGNUM *
1048mm_auth_rsa_generate_challenge(Key *key)
1049{
1050 Buffer m;
1051 BIGNUM *challenge;
1052 u_char *blob;
1053 u_int blen;
1054
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001055 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001056
1057 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001058 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001059
1060 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1061 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001062 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001063 key->type = KEY_RSA1;
1064
1065 buffer_init(&m);
1066 buffer_put_string(&m, blob, blen);
1067 xfree(blob);
1068
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001069 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1070 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001071
1072 buffer_get_bignum2(&m, challenge);
1073 buffer_free(&m);
1074
1075 return (challenge);
1076}
1077
1078int
1079mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1080{
1081 Buffer m;
1082 u_char *blob;
1083 u_int blen;
1084 int success = 0;
1085
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001086 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001087
1088 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1089 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001090 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001091 key->type = KEY_RSA1;
1092
1093 buffer_init(&m);
1094 buffer_put_string(&m, blob, blen);
1095 buffer_put_string(&m, response, 16);
1096 xfree(blob);
1097
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001098 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1099 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001100
1101 success = buffer_get_int(&m);
1102 buffer_free(&m);
1103
1104 return (success);
1105}
Damien Miller25162f22002-09-12 09:47:29 +10001106
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001107#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001108void
1109mm_audit_event(ssh_audit_event_t event)
1110{
1111 Buffer m;
1112
1113 debug3("%s entering", __func__);
1114
1115 buffer_init(&m);
1116 buffer_put_int(&m, event);
1117
1118 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1119 buffer_free(&m);
1120}
1121
1122void
1123mm_audit_run_command(const char *command)
1124{
1125 Buffer m;
1126
1127 debug3("%s entering command %s", __func__, command);
1128
1129 buffer_init(&m);
1130 buffer_put_cstring(&m, command);
1131
1132 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1133 buffer_free(&m);
1134}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001135#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001136
Darren Tucker0efd1552003-08-26 11:49:55 +10001137#ifdef GSSAPI
1138OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001139mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001140{
1141 Buffer m;
1142 OM_uint32 major;
1143
1144 /* Client doesn't get to see the context */
1145 *ctx = NULL;
1146
1147 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001148 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001149
1150 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1151 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1152
1153 major = buffer_get_int(&m);
1154
1155 buffer_free(&m);
1156 return (major);
1157}
1158
1159OM_uint32
1160mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1161 gss_buffer_desc *out, OM_uint32 *flags)
1162{
1163 Buffer m;
1164 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001165 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001166
1167 buffer_init(&m);
1168 buffer_put_string(&m, in->value, in->length);
1169
1170 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1171 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1172
1173 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001174 out->value = buffer_get_string(&m, &len);
1175 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001176 if (flags)
1177 *flags = buffer_get_int(&m);
1178
1179 buffer_free(&m);
1180
1181 return (major);
1182}
1183
Damien Miller0425d402003-11-17 22:18:21 +11001184OM_uint32
1185mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1186{
1187 Buffer m;
1188 OM_uint32 major;
1189
1190 buffer_init(&m);
1191 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1192 buffer_put_string(&m, gssmic->value, gssmic->length);
1193
1194 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1195 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1196 &m);
1197
1198 major = buffer_get_int(&m);
1199 buffer_free(&m);
1200 return(major);
1201}
1202
Darren Tucker0efd1552003-08-26 11:49:55 +10001203int
1204mm_ssh_gssapi_userok(char *user)
1205{
1206 Buffer m;
1207 int authenticated = 0;
1208
1209 buffer_init(&m);
1210
1211 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1212 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1213 &m);
1214
1215 authenticated = buffer_get_int(&m);
1216
1217 buffer_free(&m);
1218 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1219 return (authenticated);
1220}
1221#endif /* GSSAPI */