blob: 332652895cb98111b7e08c61fbe1239a37333b00 [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);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000779 if (*num > PAM_MAX_NUM_MSG)
780 fatal("%s: recieved %u PAM messages, expected <= %u",
781 __func__, *num, PAM_MAX_NUM_MSG);
782 *prompts = xcalloc((*num + 1), sizeof(char *));
783 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000784 for (i = 0; i < *num; ++i) {
785 (*prompts)[i] = buffer_get_string(&m, NULL);
786 (*echo_on)[i] = buffer_get_int(&m);
787 }
788 buffer_free(&m);
789 return (ret);
790}
791
792int
793mm_sshpam_respond(void *ctx, u_int num, char **resp)
794{
795 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000796 u_int i;
797 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000798
799 debug3("%s", __func__);
800 buffer_init(&m);
801 buffer_put_int(&m, num);
802 for (i = 0; i < num; ++i)
803 buffer_put_cstring(&m, resp[i]);
804 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
805 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
806 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
807 ret = buffer_get_int(&m);
808 debug3("%s: pam_respond returned %d", __func__, ret);
809 buffer_free(&m);
810 return (ret);
811}
812
813void
814mm_sshpam_free_ctx(void *ctxtp)
815{
816 Buffer m;
817
818 debug3("%s", __func__);
819 buffer_init(&m);
820 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
821 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
822 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
823 buffer_free(&m);
824}
Damien Miller79418552002-04-23 20:28:48 +1000825#endif /* USE_PAM */
826
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000827/* Request process termination */
828
829void
830mm_terminate(void)
831{
832 Buffer m;
833
834 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000835 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000836 buffer_free(&m);
837}
838
839int
840mm_ssh1_session_key(BIGNUM *num)
841{
842 int rsafail;
843 Buffer m;
844
845 buffer_init(&m);
846 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000847 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000848
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000849 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000850
851 rsafail = buffer_get_int(&m);
852 buffer_get_bignum2(&m, num);
853
854 buffer_free(&m);
855
856 return (rsafail);
857}
858
859static void
860mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
861 char ***prompts, u_int **echo_on)
862{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000863 *name = xstrdup("");
864 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000865 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100866 *prompts = xcalloc(*numprompts, sizeof(char *));
867 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000868 (*echo_on)[0] = 0;
869}
870
871int
872mm_bsdauth_query(void *ctx, char **name, char **infotxt,
873 u_int *numprompts, char ***prompts, u_int **echo_on)
874{
875 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100876 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000877 char *challenge;
878
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000879 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000880
881 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000882 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000883
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000884 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000885 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100886 success = buffer_get_int(&m);
887 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000888 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000889 buffer_free(&m);
890 return (-1);
891 }
892
893 /* Get the challenge, and format the response */
894 challenge = buffer_get_string(&m, NULL);
895 buffer_free(&m);
896
897 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
898 (*prompts)[0] = challenge;
899
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000900 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000901
902 return (0);
903}
904
905int
906mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
907{
908 Buffer m;
909 int authok;
910
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000911 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000912 if (numresponses != 1)
913 return (-1);
914
915 buffer_init(&m);
916 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000917 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000918
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000919 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000920 MONITOR_ANS_BSDAUTHRESPOND, &m);
921
922 authok = buffer_get_int(&m);
923 buffer_free(&m);
924
925 return ((authok == 0) ? -1 : 0);
926}
927
Darren Tucker042e2e82004-07-08 23:09:42 +1000928#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000929int
930mm_skey_query(void *ctx, char **name, char **infotxt,
931 u_int *numprompts, char ***prompts, u_int **echo_on)
932{
933 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100934 int len;
935 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000936 char *p, *challenge;
937
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000938 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000939
940 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000941 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000942
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000943 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000944 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100945 success = buffer_get_int(&m);
946 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000947 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000948 buffer_free(&m);
949 return (-1);
950 }
951
952 /* Get the challenge, and format the response */
953 challenge = buffer_get_string(&m, NULL);
954 buffer_free(&m);
955
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000956 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000957
958 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
959
Damien Miller07d86be2006-03-26 14:19:21 +1100960 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961 xfree(challenge);
962
963 return (0);
964}
965
966int
967mm_skey_respond(void *ctx, u_int numresponses, char **responses)
968{
969 Buffer m;
970 int authok;
971
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000972 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000973 if (numresponses != 1)
974 return (-1);
975
976 buffer_init(&m);
977 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000978 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000979
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000980 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000981 MONITOR_ANS_SKEYRESPOND, &m);
982
983 authok = buffer_get_int(&m);
984 buffer_free(&m);
985
986 return ((authok == 0) ? -1 : 0);
987}
Darren Tucker042e2e82004-07-08 23:09:42 +1000988#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000989
990void
991mm_ssh1_session_id(u_char session_id[16])
992{
993 Buffer m;
994 int i;
995
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000996 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000997
998 buffer_init(&m);
999 for (i = 0; i < 16; i++)
1000 buffer_put_char(&m, session_id[i]);
1001
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001002 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001003 buffer_free(&m);
1004}
1005
1006int
1007mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1008{
1009 Buffer m;
1010 Key *key;
1011 u_char *blob;
1012 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001013 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001014
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001015 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001016
1017 buffer_init(&m);
1018 buffer_put_bignum2(&m, client_n);
1019
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001020 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1021 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001022
1023 allowed = buffer_get_int(&m);
1024
Damien Miller06ebedf2003-02-24 12:03:38 +11001025 /* fake forced command */
1026 auth_clear_options();
1027 have_forced = buffer_get_int(&m);
1028 forced_command = have_forced ? xstrdup("true") : NULL;
1029
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001030 if (allowed && rkey != NULL) {
1031 blob = buffer_get_string(&m, &blen);
1032 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001033 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001034 *rkey = key;
1035 xfree(blob);
1036 }
1037 mm_send_debug(&m);
1038 buffer_free(&m);
1039
1040 return (allowed);
1041}
1042
1043BIGNUM *
1044mm_auth_rsa_generate_challenge(Key *key)
1045{
1046 Buffer m;
1047 BIGNUM *challenge;
1048 u_char *blob;
1049 u_int blen;
1050
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001051 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001052
1053 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001054 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001055
1056 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1057 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001058 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001059 key->type = KEY_RSA1;
1060
1061 buffer_init(&m);
1062 buffer_put_string(&m, blob, blen);
1063 xfree(blob);
1064
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001065 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1066 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001067
1068 buffer_get_bignum2(&m, challenge);
1069 buffer_free(&m);
1070
1071 return (challenge);
1072}
1073
1074int
1075mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1076{
1077 Buffer m;
1078 u_char *blob;
1079 u_int blen;
1080 int success = 0;
1081
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001082 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001083
1084 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1085 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001086 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001087 key->type = KEY_RSA1;
1088
1089 buffer_init(&m);
1090 buffer_put_string(&m, blob, blen);
1091 buffer_put_string(&m, response, 16);
1092 xfree(blob);
1093
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001094 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1095 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001096
1097 success = buffer_get_int(&m);
1098 buffer_free(&m);
1099
1100 return (success);
1101}
Damien Miller25162f22002-09-12 09:47:29 +10001102
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001103#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001104void
1105mm_audit_event(ssh_audit_event_t event)
1106{
1107 Buffer m;
1108
1109 debug3("%s entering", __func__);
1110
1111 buffer_init(&m);
1112 buffer_put_int(&m, event);
1113
1114 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1115 buffer_free(&m);
1116}
1117
1118void
1119mm_audit_run_command(const char *command)
1120{
1121 Buffer m;
1122
1123 debug3("%s entering command %s", __func__, command);
1124
1125 buffer_init(&m);
1126 buffer_put_cstring(&m, command);
1127
1128 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1129 buffer_free(&m);
1130}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001131#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001132
Darren Tucker0efd1552003-08-26 11:49:55 +10001133#ifdef GSSAPI
1134OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001135mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001136{
1137 Buffer m;
1138 OM_uint32 major;
1139
1140 /* Client doesn't get to see the context */
1141 *ctx = NULL;
1142
1143 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001144 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001145
1146 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1147 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1148
1149 major = buffer_get_int(&m);
1150
1151 buffer_free(&m);
1152 return (major);
1153}
1154
1155OM_uint32
1156mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1157 gss_buffer_desc *out, OM_uint32 *flags)
1158{
1159 Buffer m;
1160 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001161 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001162
1163 buffer_init(&m);
1164 buffer_put_string(&m, in->value, in->length);
1165
1166 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1167 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1168
1169 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001170 out->value = buffer_get_string(&m, &len);
1171 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001172 if (flags)
1173 *flags = buffer_get_int(&m);
1174
1175 buffer_free(&m);
1176
1177 return (major);
1178}
1179
Damien Miller0425d402003-11-17 22:18:21 +11001180OM_uint32
1181mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1182{
1183 Buffer m;
1184 OM_uint32 major;
1185
1186 buffer_init(&m);
1187 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1188 buffer_put_string(&m, gssmic->value, gssmic->length);
1189
1190 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1191 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1192 &m);
1193
1194 major = buffer_get_int(&m);
1195 buffer_free(&m);
1196 return(major);
1197}
1198
Darren Tucker0efd1552003-08-26 11:49:55 +10001199int
1200mm_ssh_gssapi_userok(char *user)
1201{
1202 Buffer m;
1203 int authenticated = 0;
1204
1205 buffer_init(&m);
1206
1207 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1208 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1209 &m);
1210
1211 authenticated = buffer_get_int(&m);
1212
1213 buffer_free(&m);
1214 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1215 return (authenticated);
1216}
1217#endif /* GSSAPI */