blob: 8cfc8cc08f79769b1ed0dbed2d91d0ee61b895cc [file] [log] [blame]
Damien Miller3f941882006-03-31 23:13:02 +11001/* $OpenBSD: monitor_wrap.c,v 1.45 2006/03/30 09:58:15 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
30#include <openssl/bn.h>
31#include <openssl/dh.h>
32
33#include "ssh.h"
34#include "dh.h"
35#include "kex.h"
36#include "auth.h"
Damien Miller06ebedf2003-02-24 12:03:38 +110037#include "auth-options.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000038#include "buffer.h"
39#include "bufaux.h"
40#include "packet.h"
41#include "mac.h"
42#include "log.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000043#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
44#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000045#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000046#define TARGET_OS_MAC 1
47#else
48#include "zlib.h"
49#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000050#include "monitor.h"
51#include "monitor_wrap.h"
52#include "xmalloc.h"
53#include "atomicio.h"
54#include "monitor_fdpass.h"
Damien Miller3f941882006-03-31 23:13:02 +110055#include "misc.h"
Damien Miller4e448a32003-05-14 15:11:48 +100056#include "servconf.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000057
58#include "auth.h"
59#include "channels.h"
60#include "session.h"
61
Darren Tucker0efd1552003-08-26 11:49:55 +100062#ifdef GSSAPI
63#include "ssh-gss.h"
64#endif
65
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000066/* Imports */
67extern int compat20;
68extern Newkeys *newkeys[];
69extern z_stream incoming_stream;
70extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000071extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000072extern Buffer input, output;
Darren Tucker09991742004-07-17 17:05:14 +100073extern Buffer loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100074extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000075
Darren Tucker3e33cec2003-10-02 16:12:36 +100076int
77mm_is_monitor(void)
78{
79 /*
80 * m_pid is only set in the privileged part, and
81 * points to the unprivileged child.
82 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +110083 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +100084}
85
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000086void
Darren Tucker3f9fdc72004-06-22 12:56:01 +100087mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000088{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000089 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +000090 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000091
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000092 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093
Damien Miller3f941882006-03-31 23:13:02 +110094 put_u32(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +000095 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +100096 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +100097 fatal("%s: write: %s", __func__, strerror(errno));
Darren Tucker3f9fdc72004-06-22 12:56:01 +100098 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +100099 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000100}
101
102void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000103mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000104{
105 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000106 u_int msg_len;
107
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000108 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000109
Damien Millerb253cc42005-05-26 12:23:44 +1000110 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
111 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000112 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000113 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000114 }
Damien Miller3f941882006-03-31 23:13:02 +1100115 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000116 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000117 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000118 buffer_clear(m);
119 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000120 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
121 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000122}
123
124void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000125mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000126{
127 u_char rtype;
128
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000129 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000130
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000131 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000132 rtype = buffer_get_char(m);
133 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000134 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000135 rtype, type);
136}
137
138DH *
139mm_choose_dh(int min, int nbits, int max)
140{
141 BIGNUM *p, *g;
142 int success = 0;
143 Buffer m;
144
145 buffer_init(&m);
146 buffer_put_int(&m, min);
147 buffer_put_int(&m, nbits);
148 buffer_put_int(&m, max);
149
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000150 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000151
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000152 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000153 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000154
155 success = buffer_get_char(&m);
156 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000157 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000158
159 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000160 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000161 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000162 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000163 buffer_get_bignum2(&m, p);
164 buffer_get_bignum2(&m, g);
165
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000166 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000167 buffer_free(&m);
168
169 return (dh_new_group(g, p));
170}
171
172int
173mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
174{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000175 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000176 Buffer m;
177
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000178 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000179
180 buffer_init(&m);
181 buffer_put_int(&m, kex->host_key_index(key));
182 buffer_put_string(&m, data, datalen);
183
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000184 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000185
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000186 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000187 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000188 *sigp = buffer_get_string(&m, lenp);
189 buffer_free(&m);
190
191 return (0);
192}
193
194struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000195mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000196{
197 Buffer m;
198 struct passwd *pw;
199 u_int pwlen;
200
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000201 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202
203 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000204 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000205
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000206 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000207
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000208 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000209 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000210
211 if (buffer_get_char(&m) == 0) {
212 buffer_free(&m);
213 return (NULL);
214 }
215 pw = buffer_get_string(&m, &pwlen);
216 if (pwlen != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000217 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000218 pw->pw_name = buffer_get_string(&m, NULL);
219 pw->pw_passwd = buffer_get_string(&m, NULL);
220 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000221#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000222 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000223#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000224 pw->pw_dir = buffer_get_string(&m, NULL);
225 pw->pw_shell = buffer_get_string(&m, NULL);
226 buffer_free(&m);
227
228 return (pw);
229}
230
Darren Tucker7eb3de02003-10-15 15:56:58 +1000231char *
232mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000233{
234 Buffer m;
235 char *banner;
236
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000237 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000238
239 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000240 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000241 buffer_clear(&m);
242
Darren Tucker7eb3de02003-10-15 15:56:58 +1000243 mm_request_receive_expect(pmonitor->m_recvfd,
244 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000245 banner = buffer_get_string(&m, NULL);
246 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000247
Darren Tucker7eb3de02003-10-15 15:56:58 +1000248 /* treat empty banner as missing banner */
249 if (strlen(banner) == 0) {
250 xfree(banner);
251 banner = NULL;
252 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000253 return (banner);
254}
255
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000256/* Inform the privileged process about service and style */
257
258void
259mm_inform_authserv(char *service, char *style)
260{
261 Buffer m;
262
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000263 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000264
265 buffer_init(&m);
266 buffer_put_cstring(&m, service);
267 buffer_put_cstring(&m, style ? style : "");
268
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000269 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000270
271 buffer_free(&m);
272}
273
274/* Do the password authentication */
275int
276mm_auth_password(Authctxt *authctxt, char *password)
277{
278 Buffer m;
279 int authenticated = 0;
280
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000281 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000282
283 buffer_init(&m);
284 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000285 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000286
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000287 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000288 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000289
290 authenticated = buffer_get_int(&m);
291
292 buffer_free(&m);
293
294 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000295 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000296 return (authenticated);
297}
298
299int
300mm_user_key_allowed(struct passwd *pw, Key *key)
301{
302 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
303}
304
305int
306mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
307 Key *key)
308{
309 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
310}
311
312int
313mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
314 char *host, Key *key)
315{
316 int ret;
317
318 key->type = KEY_RSA; /* XXX hack for key_to_blob */
319 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
320 key->type = KEY_RSA1;
321 return (ret);
322}
323
324static void
325mm_send_debug(Buffer *m)
326{
327 char *msg;
328
329 while (buffer_len(m)) {
330 msg = buffer_get_string(m, NULL);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000331 debug3("%s: Sending debug: %s", __func__, msg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000332 packet_send_debug("%s", msg);
333 xfree(msg);
334 }
335}
336
337int
338mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
339{
340 Buffer m;
341 u_char *blob;
342 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100343 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000344
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000345 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000346
347 /* Convert the key to a blob and the pass it over */
348 if (!key_to_blob(key, &blob, &len))
349 return (0);
350
351 buffer_init(&m);
352 buffer_put_int(&m, type);
353 buffer_put_cstring(&m, user ? user : "");
354 buffer_put_cstring(&m, host ? host : "");
355 buffer_put_string(&m, blob, len);
356 xfree(blob);
357
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000358 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000359
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000360 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000361 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000362
363 allowed = buffer_get_int(&m);
364
Damien Miller06ebedf2003-02-24 12:03:38 +1100365 /* fake forced command */
366 auth_clear_options();
367 have_forced = buffer_get_int(&m);
368 forced_command = have_forced ? xstrdup("true") : NULL;
369
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370 /* Send potential debug messages */
371 mm_send_debug(&m);
372
373 buffer_free(&m);
374
375 return (allowed);
376}
377
378/*
379 * This key verify needs to send the key type along, because the
380 * privileged parent makes the decision if the key is allowed
381 * for authentication.
382 */
383
384int
385mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
386{
387 Buffer m;
388 u_char *blob;
389 u_int len;
390 int verified = 0;
391
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000392 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000393
394 /* Convert the key to a blob and the pass it over */
395 if (!key_to_blob(key, &blob, &len))
396 return (0);
397
398 buffer_init(&m);
399 buffer_put_string(&m, blob, len);
400 buffer_put_string(&m, sig, siglen);
401 buffer_put_string(&m, data, datalen);
402 xfree(blob);
403
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000404 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000405
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000406 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000407 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000408
409 verified = buffer_get_int(&m);
410
411 buffer_free(&m);
412
413 return (verified);
414}
415
416/* Export key state after authentication */
417Newkeys *
418mm_newkeys_from_blob(u_char *blob, int blen)
419{
420 Buffer b;
421 u_int len;
422 Newkeys *newkey = NULL;
423 Enc *enc;
424 Mac *mac;
425 Comp *comp;
426
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000427 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000428#ifdef DEBUG_PK
429 dump_base64(stderr, blob, blen);
430#endif
431 buffer_init(&b);
432 buffer_append(&b, blob, blen);
433
434 newkey = xmalloc(sizeof(*newkey));
435 enc = &newkey->enc;
436 mac = &newkey->mac;
437 comp = &newkey->comp;
438
439 /* Enc structure */
440 enc->name = buffer_get_string(&b, NULL);
441 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
442 enc->enabled = buffer_get_int(&b);
443 enc->block_size = buffer_get_int(&b);
444 enc->key = buffer_get_string(&b, &enc->key_len);
445 enc->iv = buffer_get_string(&b, &len);
446 if (len != enc->block_size)
Ben Lindstrom08512492002-06-27 00:23:02 +0000447 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000448 enc->block_size, len);
449
450 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000451 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000452 enc->name, enc->cipher);
453
454 /* Mac structure */
455 mac->name = buffer_get_string(&b, NULL);
456 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000457 fatal("%s: can not init mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000458 mac->enabled = buffer_get_int(&b);
459 mac->key = buffer_get_string(&b, &len);
460 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000461 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000462 mac->key_len);
463 mac->key_len = len;
464
465 /* Comp structure */
466 comp->type = buffer_get_int(&b);
467 comp->enabled = buffer_get_int(&b);
468 comp->name = buffer_get_string(&b, NULL);
469
470 len = buffer_len(&b);
471 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000472 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000473 buffer_free(&b);
474 return (newkey);
475}
476
477int
478mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
479{
480 Buffer b;
481 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000482 Enc *enc;
483 Mac *mac;
484 Comp *comp;
485 Newkeys *newkey = newkeys[mode];
486
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000487 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000488
489 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000490 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000491 return 0;
492 }
493 enc = &newkey->enc;
494 mac = &newkey->mac;
495 comp = &newkey->comp;
496
497 buffer_init(&b);
498 /* Enc structure */
499 buffer_put_cstring(&b, enc->name);
500 /* The cipher struct is constant and shared, you export pointer */
501 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
502 buffer_put_int(&b, enc->enabled);
503 buffer_put_int(&b, enc->block_size);
504 buffer_put_string(&b, enc->key, enc->key_len);
505 packet_get_keyiv(mode, enc->iv, enc->block_size);
506 buffer_put_string(&b, enc->iv, enc->block_size);
507
508 /* Mac structure */
509 buffer_put_cstring(&b, mac->name);
510 buffer_put_int(&b, mac->enabled);
511 buffer_put_string(&b, mac->key, mac->key_len);
512
513 /* Comp structure */
514 buffer_put_int(&b, comp->type);
515 buffer_put_int(&b, comp->enabled);
516 buffer_put_cstring(&b, comp->name);
517
518 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000519 if (lenp != NULL)
520 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000521 if (blobp != NULL) {
522 *blobp = xmalloc(len);
523 memcpy(*blobp, buffer_ptr(&b), len);
524 }
525 memset(buffer_ptr(&b), 0, len);
526 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000527 return len;
528}
529
530static void
531mm_send_kex(Buffer *m, Kex *kex)
532{
533 buffer_put_string(m, kex->session_id, kex->session_id_len);
534 buffer_put_int(m, kex->we_need);
535 buffer_put_int(m, kex->hostkey_type);
536 buffer_put_int(m, kex->kex_type);
537 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
538 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
539 buffer_put_int(m, kex->flags);
540 buffer_put_cstring(m, kex->client_version_string);
541 buffer_put_cstring(m, kex->server_version_string);
542}
543
544void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000545mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000546{
547 Buffer m;
548 u_char *blob, *p;
549 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000550 u_int32_t seqnr, packets;
551 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000552
553 buffer_init(&m);
554
555 if (!compat20) {
556 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000557 u_char *key;
558 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000559
560 buffer_put_int(&m, packet_get_protocol_flags());
561
562 buffer_put_int(&m, packet_get_ssh1_cipher());
563
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000564 debug3("%s: Sending ssh1 KEY+IV", __func__);
565 keylen = packet_get_encryption_key(NULL);
566 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
567 keylen = packet_get_encryption_key(key);
568 buffer_put_string(&m, key, keylen);
569 memset(key, 0, keylen);
570 xfree(key);
571
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000572 ivlen = packet_get_keyiv_len(MODE_OUT);
573 packet_get_keyiv(MODE_OUT, iv, ivlen);
574 buffer_put_string(&m, iv, ivlen);
575 ivlen = packet_get_keyiv_len(MODE_OUT);
576 packet_get_keyiv(MODE_IN, iv, ivlen);
577 buffer_put_string(&m, iv, ivlen);
578 goto skip;
579 } else {
580 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000581 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000582 }
583
584 debug3("%s: Sending new keys: %p %p",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000585 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586
587 /* Keys from Kex */
588 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000589 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000590
591 buffer_put_string(&m, blob, bloblen);
592 xfree(blob);
593
594 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000595 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000596
597 buffer_put_string(&m, blob, bloblen);
598 xfree(blob);
599
Damien Millera5539d22003-04-09 20:50:06 +1000600 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
601 buffer_put_int(&m, seqnr);
602 buffer_put_int64(&m, blocks);
603 buffer_put_int(&m, packets);
Damien Millerb1ecd9c2003-04-09 20:51:24 +1000604 packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
Damien Millera5539d22003-04-09 20:50:06 +1000605 buffer_put_int(&m, seqnr);
606 buffer_put_int64(&m, blocks);
607 buffer_put_int(&m, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000608
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000609 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000610 skip:
611 /* More key context */
612 plen = packet_get_keycontext(MODE_OUT, NULL);
613 p = xmalloc(plen+1);
614 packet_get_keycontext(MODE_OUT, p);
615 buffer_put_string(&m, p, plen);
616 xfree(p);
617
618 plen = packet_get_keycontext(MODE_IN, NULL);
619 p = xmalloc(plen+1);
620 packet_get_keycontext(MODE_IN, p);
621 buffer_put_string(&m, p, plen);
622 xfree(p);
623
624 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000625 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000626 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
627 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
628
629 /* Network I/O buffers */
630 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
631 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
632
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000633 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000634 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000635
636 buffer_free(&m);
637}
638
639int
Damien Miller71a73672006-03-26 14:04:36 +1100640mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000641{
642 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000643 char *p, *msg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000644 int success = 0;
645
646 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000647 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000648
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000649 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000650 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000651
652 success = buffer_get_int(&m);
653 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000654 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000655 buffer_free(&m);
656 return (0);
657 }
658 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000659 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000660 buffer_free(&m);
661
662 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
663 xfree(p);
664
Darren Tucker09991742004-07-17 17:05:14 +1000665 buffer_append(&loginmsg, msg, strlen(msg));
666 xfree(msg);
667
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000668 *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
669 *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000670
671 /* Success */
672 return (1);
673}
674
675void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000676mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000677{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000678 Buffer m;
679
680 if (s->ttyfd == -1)
681 return;
682 buffer_init(&m);
683 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000684 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685 buffer_free(&m);
686
687 /* closed dup'ed master */
688 if (close(s->ptymaster) < 0)
689 error("close(s->ptymaster): %s", strerror(errno));
690
691 /* unlink pty from session */
692 s->ttyfd = -1;
693}
694
Damien Miller79418552002-04-23 20:28:48 +1000695#ifdef USE_PAM
696void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100697mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000698{
699 Buffer m;
700
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000701 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000702 if (!options.use_pam)
703 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000704
705 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000706 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000707
708 buffer_free(&m);
709}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000710
Damien Miller1f499fd2003-08-25 13:08:49 +1000711u_int
712mm_do_pam_account(void)
713{
714 Buffer m;
715 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000716 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000717
718 debug3("%s entering", __func__);
719 if (!options.use_pam)
720 fatal("UsePAM=no, but ended up in %s anyway", __func__);
721
722 buffer_init(&m);
723 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
724
Damien Millera8e06ce2003-11-21 23:48:55 +1100725 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000726 MONITOR_ANS_PAM_ACCOUNT, &m);
727 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000728 msg = buffer_get_string(&m, NULL);
729 buffer_append(&loginmsg, msg, strlen(msg));
730 xfree(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000731
732 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100733
Damien Miller1f499fd2003-08-25 13:08:49 +1000734 debug3("%s returning %d", __func__, ret);
735
736 return (ret);
737}
738
Damien Miller4f9f42a2003-05-10 19:28:02 +1000739void *
740mm_sshpam_init_ctx(Authctxt *authctxt)
741{
742 Buffer m;
743 int success;
744
745 debug3("%s", __func__);
746 buffer_init(&m);
747 buffer_put_cstring(&m, authctxt->user);
748 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
749 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
750 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
751 success = buffer_get_int(&m);
752 if (success == 0) {
753 debug3("%s: pam_init_ctx failed", __func__);
754 buffer_free(&m);
755 return (NULL);
756 }
757 buffer_free(&m);
758 return (authctxt);
759}
760
761int
762mm_sshpam_query(void *ctx, char **name, char **info,
763 u_int *num, char ***prompts, u_int **echo_on)
764{
765 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000766 u_int i;
767 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000768
769 debug3("%s", __func__);
770 buffer_init(&m);
771 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
772 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
773 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
774 ret = buffer_get_int(&m);
775 debug3("%s: pam_query returned %d", __func__, ret);
776 *name = buffer_get_string(&m, NULL);
777 *info = buffer_get_string(&m, NULL);
778 *num = buffer_get_int(&m);
779 *prompts = xmalloc((*num + 1) * sizeof(char *));
780 *echo_on = xmalloc((*num + 1) * sizeof(u_int));
781 for (i = 0; i < *num; ++i) {
782 (*prompts)[i] = buffer_get_string(&m, NULL);
783 (*echo_on)[i] = buffer_get_int(&m);
784 }
785 buffer_free(&m);
786 return (ret);
787}
788
789int
790mm_sshpam_respond(void *ctx, u_int num, char **resp)
791{
792 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000793 u_int i;
794 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000795
796 debug3("%s", __func__);
797 buffer_init(&m);
798 buffer_put_int(&m, num);
799 for (i = 0; i < num; ++i)
800 buffer_put_cstring(&m, resp[i]);
801 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
802 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
803 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
804 ret = buffer_get_int(&m);
805 debug3("%s: pam_respond returned %d", __func__, ret);
806 buffer_free(&m);
807 return (ret);
808}
809
810void
811mm_sshpam_free_ctx(void *ctxtp)
812{
813 Buffer m;
814
815 debug3("%s", __func__);
816 buffer_init(&m);
817 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
818 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
819 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
820 buffer_free(&m);
821}
Damien Miller79418552002-04-23 20:28:48 +1000822#endif /* USE_PAM */
823
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000824/* Request process termination */
825
826void
827mm_terminate(void)
828{
829 Buffer m;
830
831 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000832 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000833 buffer_free(&m);
834}
835
836int
837mm_ssh1_session_key(BIGNUM *num)
838{
839 int rsafail;
840 Buffer m;
841
842 buffer_init(&m);
843 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000844 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000845
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000846 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000847
848 rsafail = buffer_get_int(&m);
849 buffer_get_bignum2(&m, num);
850
851 buffer_free(&m);
852
853 return (rsafail);
854}
855
856static void
857mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
858 char ***prompts, u_int **echo_on)
859{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000860 *name = xstrdup("");
861 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000862 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100863 *prompts = xcalloc(*numprompts, sizeof(char *));
864 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000865 (*echo_on)[0] = 0;
866}
867
868int
869mm_bsdauth_query(void *ctx, char **name, char **infotxt,
870 u_int *numprompts, char ***prompts, u_int **echo_on)
871{
872 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100873 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000874 char *challenge;
875
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000876 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000877
878 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000879 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000880
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000881 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000882 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100883 success = buffer_get_int(&m);
884 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000885 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000886 buffer_free(&m);
887 return (-1);
888 }
889
890 /* Get the challenge, and format the response */
891 challenge = buffer_get_string(&m, NULL);
892 buffer_free(&m);
893
894 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
895 (*prompts)[0] = challenge;
896
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000897 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000898
899 return (0);
900}
901
902int
903mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
904{
905 Buffer m;
906 int authok;
907
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000908 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909 if (numresponses != 1)
910 return (-1);
911
912 buffer_init(&m);
913 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000914 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000915
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000916 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000917 MONITOR_ANS_BSDAUTHRESPOND, &m);
918
919 authok = buffer_get_int(&m);
920 buffer_free(&m);
921
922 return ((authok == 0) ? -1 : 0);
923}
924
Darren Tucker042e2e82004-07-08 23:09:42 +1000925#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926int
927mm_skey_query(void *ctx, char **name, char **infotxt,
928 u_int *numprompts, char ***prompts, u_int **echo_on)
929{
930 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100931 int len;
932 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000933 char *p, *challenge;
934
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000935 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000936
937 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000938 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000939
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000940 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000941 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100942 success = buffer_get_int(&m);
943 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000944 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000945 buffer_free(&m);
946 return (-1);
947 }
948
949 /* Get the challenge, and format the response */
950 challenge = buffer_get_string(&m, NULL);
951 buffer_free(&m);
952
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000953 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000954
955 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
956
Damien Miller07d86be2006-03-26 14:19:21 +1100957 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000958 xfree(challenge);
959
960 return (0);
961}
962
963int
964mm_skey_respond(void *ctx, u_int numresponses, char **responses)
965{
966 Buffer m;
967 int authok;
968
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000969 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000970 if (numresponses != 1)
971 return (-1);
972
973 buffer_init(&m);
974 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000975 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000977 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000978 MONITOR_ANS_SKEYRESPOND, &m);
979
980 authok = buffer_get_int(&m);
981 buffer_free(&m);
982
983 return ((authok == 0) ? -1 : 0);
984}
Darren Tucker042e2e82004-07-08 23:09:42 +1000985#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000986
987void
988mm_ssh1_session_id(u_char session_id[16])
989{
990 Buffer m;
991 int i;
992
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000993 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000994
995 buffer_init(&m);
996 for (i = 0; i < 16; i++)
997 buffer_put_char(&m, session_id[i]);
998
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000999 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001000 buffer_free(&m);
1001}
1002
1003int
1004mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1005{
1006 Buffer m;
1007 Key *key;
1008 u_char *blob;
1009 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001010 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001011
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001012 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001013
1014 buffer_init(&m);
1015 buffer_put_bignum2(&m, client_n);
1016
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001017 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1018 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001019
1020 allowed = buffer_get_int(&m);
1021
Damien Miller06ebedf2003-02-24 12:03:38 +11001022 /* fake forced command */
1023 auth_clear_options();
1024 have_forced = buffer_get_int(&m);
1025 forced_command = have_forced ? xstrdup("true") : NULL;
1026
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001027 if (allowed && rkey != NULL) {
1028 blob = buffer_get_string(&m, &blen);
1029 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001030 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001031 *rkey = key;
1032 xfree(blob);
1033 }
1034 mm_send_debug(&m);
1035 buffer_free(&m);
1036
1037 return (allowed);
1038}
1039
1040BIGNUM *
1041mm_auth_rsa_generate_challenge(Key *key)
1042{
1043 Buffer m;
1044 BIGNUM *challenge;
1045 u_char *blob;
1046 u_int blen;
1047
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001048 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001049
1050 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001051 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001052
1053 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1054 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001055 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001056 key->type = KEY_RSA1;
1057
1058 buffer_init(&m);
1059 buffer_put_string(&m, blob, blen);
1060 xfree(blob);
1061
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001062 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1063 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001064
1065 buffer_get_bignum2(&m, challenge);
1066 buffer_free(&m);
1067
1068 return (challenge);
1069}
1070
1071int
1072mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1073{
1074 Buffer m;
1075 u_char *blob;
1076 u_int blen;
1077 int success = 0;
1078
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001079 debug3("%s entering", __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 buffer_put_string(&m, response, 16);
1089 xfree(blob);
1090
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001091 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1092 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001093
1094 success = buffer_get_int(&m);
1095 buffer_free(&m);
1096
1097 return (success);
1098}
Damien Miller25162f22002-09-12 09:47:29 +10001099
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001100#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001101void
1102mm_audit_event(ssh_audit_event_t event)
1103{
1104 Buffer m;
1105
1106 debug3("%s entering", __func__);
1107
1108 buffer_init(&m);
1109 buffer_put_int(&m, event);
1110
1111 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1112 buffer_free(&m);
1113}
1114
1115void
1116mm_audit_run_command(const char *command)
1117{
1118 Buffer m;
1119
1120 debug3("%s entering command %s", __func__, command);
1121
1122 buffer_init(&m);
1123 buffer_put_cstring(&m, command);
1124
1125 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1126 buffer_free(&m);
1127}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001128#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001129
Darren Tucker0efd1552003-08-26 11:49:55 +10001130#ifdef GSSAPI
1131OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001132mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001133{
1134 Buffer m;
1135 OM_uint32 major;
1136
1137 /* Client doesn't get to see the context */
1138 *ctx = NULL;
1139
1140 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001141 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001142
1143 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1144 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1145
1146 major = buffer_get_int(&m);
1147
1148 buffer_free(&m);
1149 return (major);
1150}
1151
1152OM_uint32
1153mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1154 gss_buffer_desc *out, OM_uint32 *flags)
1155{
1156 Buffer m;
1157 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001158 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001159
1160 buffer_init(&m);
1161 buffer_put_string(&m, in->value, in->length);
1162
1163 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1164 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1165
1166 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001167 out->value = buffer_get_string(&m, &len);
1168 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001169 if (flags)
1170 *flags = buffer_get_int(&m);
1171
1172 buffer_free(&m);
1173
1174 return (major);
1175}
1176
Damien Miller0425d402003-11-17 22:18:21 +11001177OM_uint32
1178mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1179{
1180 Buffer m;
1181 OM_uint32 major;
1182
1183 buffer_init(&m);
1184 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1185 buffer_put_string(&m, gssmic->value, gssmic->length);
1186
1187 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1188 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1189 &m);
1190
1191 major = buffer_get_int(&m);
1192 buffer_free(&m);
1193 return(major);
1194}
1195
Darren Tucker0efd1552003-08-26 11:49:55 +10001196int
1197mm_ssh_gssapi_userok(char *user)
1198{
1199 Buffer m;
1200 int authenticated = 0;
1201
1202 buffer_init(&m);
1203
1204 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1205 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1206 &m);
1207
1208 authenticated = buffer_get_int(&m);
1209
1210 buffer_free(&m);
1211 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1212 return (authenticated);
1213}
1214#endif /* GSSAPI */