blob: e1b6512b42ca193c1c5b2e66b4a88cb1fa5d669b [file] [log] [blame]
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001/*
2 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2002 Markus Friedl <markus@openbsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Darren Tucker09991742004-07-17 17:05:14 +100028RCSID("$OpenBSD: monitor_wrap.c,v 1.39 2004/07/17 05:31:41 dtucker Exp $");
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"
55#include "getput.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;
Darren Tucker77fc29e2004-09-11 23:07:03 +100075extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000076
Darren Tucker3e33cec2003-10-02 16:12:36 +100077int
78mm_is_monitor(void)
79{
80 /*
81 * m_pid is only set in the privileged part, and
82 * points to the unprivileged child.
83 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +110084 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +100085}
86
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000087void
Darren Tucker3f9fdc72004-06-22 12:56:01 +100088mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000089{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000090 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +000091 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000092
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000093 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000094
95 PUT_32BIT(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +000096 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +100097 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000098 fatal("%s: write", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +100099 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000100 fatal("%s: write", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101}
102
103void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000104mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105{
106 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107 u_int msg_len;
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +0000108 ssize_t res;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000109
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000110 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000111
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000112 res = atomicio(read, sock, buf, sizeof(buf));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000113 if (res != sizeof(buf)) {
114 if (res == 0)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000115 cleanup_exit(255);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000116 fatal("%s: read: %ld", __func__, (long)res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000117 }
118 msg_len = GET_32BIT(buf);
119 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000120 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000121 buffer_clear(m);
122 buffer_append_space(m, msg_len);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000123 res = atomicio(read, sock, buffer_ptr(m), msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000124 if (res != msg_len)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000125 fatal("%s: read: %ld != msg_len", __func__, (long)res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000126}
127
128void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000129mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000130{
131 u_char rtype;
132
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000133 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000134
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000135 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000136 rtype = buffer_get_char(m);
137 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000138 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000139 rtype, type);
140}
141
142DH *
143mm_choose_dh(int min, int nbits, int max)
144{
145 BIGNUM *p, *g;
146 int success = 0;
147 Buffer m;
148
149 buffer_init(&m);
150 buffer_put_int(&m, min);
151 buffer_put_int(&m, nbits);
152 buffer_put_int(&m, max);
153
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000154 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000155
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000156 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000157 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000158
159 success = buffer_get_char(&m);
160 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000161 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000162
163 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000164 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000165 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000166 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000167 buffer_get_bignum2(&m, p);
168 buffer_get_bignum2(&m, g);
169
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000170 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171 buffer_free(&m);
172
173 return (dh_new_group(g, p));
174}
175
176int
177mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
178{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000179 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000180 Buffer m;
181
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000182 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000183
184 buffer_init(&m);
185 buffer_put_int(&m, kex->host_key_index(key));
186 buffer_put_string(&m, data, datalen);
187
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000188 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000189
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000190 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000191 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000192 *sigp = buffer_get_string(&m, lenp);
193 buffer_free(&m);
194
195 return (0);
196}
197
198struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000199mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000200{
201 Buffer m;
202 struct passwd *pw;
203 u_int pwlen;
204
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000205 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206
207 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000208 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000209
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000210 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000211
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000212 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000213 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000214
215 if (buffer_get_char(&m) == 0) {
216 buffer_free(&m);
217 return (NULL);
218 }
219 pw = buffer_get_string(&m, &pwlen);
220 if (pwlen != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000221 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000222 pw->pw_name = buffer_get_string(&m, NULL);
223 pw->pw_passwd = buffer_get_string(&m, NULL);
224 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000225#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000226 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000227#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000228 pw->pw_dir = buffer_get_string(&m, NULL);
229 pw->pw_shell = buffer_get_string(&m, NULL);
230 buffer_free(&m);
231
232 return (pw);
233}
234
Darren Tucker7eb3de02003-10-15 15:56:58 +1000235char *
236mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000237{
238 Buffer m;
239 char *banner;
240
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000241 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000242
243 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000244 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000245 buffer_clear(&m);
246
Darren Tucker7eb3de02003-10-15 15:56:58 +1000247 mm_request_receive_expect(pmonitor->m_recvfd,
248 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000249 banner = buffer_get_string(&m, NULL);
250 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000251
Darren Tucker7eb3de02003-10-15 15:56:58 +1000252 /* treat empty banner as missing banner */
253 if (strlen(banner) == 0) {
254 xfree(banner);
255 banner = NULL;
256 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000257 return (banner);
258}
259
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000260/* Inform the privileged process about service and style */
261
262void
263mm_inform_authserv(char *service, char *style)
264{
265 Buffer m;
266
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000267 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000268
269 buffer_init(&m);
270 buffer_put_cstring(&m, service);
271 buffer_put_cstring(&m, style ? style : "");
272
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000273 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000274
275 buffer_free(&m);
276}
277
278/* Do the password authentication */
279int
280mm_auth_password(Authctxt *authctxt, char *password)
281{
282 Buffer m;
283 int authenticated = 0;
284
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000285 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000286
287 buffer_init(&m);
288 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000289 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000290
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000291 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000292 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000293
294 authenticated = buffer_get_int(&m);
295
296 buffer_free(&m);
297
298 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000299 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000300 return (authenticated);
301}
302
303int
304mm_user_key_allowed(struct passwd *pw, Key *key)
305{
306 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
307}
308
309int
310mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
311 Key *key)
312{
313 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
314}
315
316int
317mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
318 char *host, Key *key)
319{
320 int ret;
321
322 key->type = KEY_RSA; /* XXX hack for key_to_blob */
323 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
324 key->type = KEY_RSA1;
325 return (ret);
326}
327
328static void
329mm_send_debug(Buffer *m)
330{
331 char *msg;
332
333 while (buffer_len(m)) {
334 msg = buffer_get_string(m, NULL);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000335 debug3("%s: Sending debug: %s", __func__, msg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000336 packet_send_debug("%s", msg);
337 xfree(msg);
338 }
339}
340
341int
342mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
343{
344 Buffer m;
345 u_char *blob;
346 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100347 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000349 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000350
351 /* Convert the key to a blob and the pass it over */
352 if (!key_to_blob(key, &blob, &len))
353 return (0);
354
355 buffer_init(&m);
356 buffer_put_int(&m, type);
357 buffer_put_cstring(&m, user ? user : "");
358 buffer_put_cstring(&m, host ? host : "");
359 buffer_put_string(&m, blob, len);
360 xfree(blob);
361
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000362 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000363
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000364 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000365 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000366
367 allowed = buffer_get_int(&m);
368
Damien Miller06ebedf2003-02-24 12:03:38 +1100369 /* fake forced command */
370 auth_clear_options();
371 have_forced = buffer_get_int(&m);
372 forced_command = have_forced ? xstrdup("true") : NULL;
373
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000374 /* Send potential debug messages */
375 mm_send_debug(&m);
376
377 buffer_free(&m);
378
379 return (allowed);
380}
381
382/*
383 * This key verify needs to send the key type along, because the
384 * privileged parent makes the decision if the key is allowed
385 * for authentication.
386 */
387
388int
389mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
390{
391 Buffer m;
392 u_char *blob;
393 u_int len;
394 int verified = 0;
395
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000396 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000397
398 /* Convert the key to a blob and the pass it over */
399 if (!key_to_blob(key, &blob, &len))
400 return (0);
401
402 buffer_init(&m);
403 buffer_put_string(&m, blob, len);
404 buffer_put_string(&m, sig, siglen);
405 buffer_put_string(&m, data, datalen);
406 xfree(blob);
407
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000408 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000409
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000410 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000411 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000412
413 verified = buffer_get_int(&m);
414
415 buffer_free(&m);
416
417 return (verified);
418}
419
420/* Export key state after authentication */
421Newkeys *
422mm_newkeys_from_blob(u_char *blob, int blen)
423{
424 Buffer b;
425 u_int len;
426 Newkeys *newkey = NULL;
427 Enc *enc;
428 Mac *mac;
429 Comp *comp;
430
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000431 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000432#ifdef DEBUG_PK
433 dump_base64(stderr, blob, blen);
434#endif
435 buffer_init(&b);
436 buffer_append(&b, blob, blen);
437
438 newkey = xmalloc(sizeof(*newkey));
439 enc = &newkey->enc;
440 mac = &newkey->mac;
441 comp = &newkey->comp;
442
443 /* Enc structure */
444 enc->name = buffer_get_string(&b, NULL);
445 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
446 enc->enabled = buffer_get_int(&b);
447 enc->block_size = buffer_get_int(&b);
448 enc->key = buffer_get_string(&b, &enc->key_len);
449 enc->iv = buffer_get_string(&b, &len);
450 if (len != enc->block_size)
Ben Lindstrom08512492002-06-27 00:23:02 +0000451 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000452 enc->block_size, len);
453
454 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000455 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000456 enc->name, enc->cipher);
457
458 /* Mac structure */
459 mac->name = buffer_get_string(&b, NULL);
460 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000461 fatal("%s: can not init mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000462 mac->enabled = buffer_get_int(&b);
463 mac->key = buffer_get_string(&b, &len);
464 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000465 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000466 mac->key_len);
467 mac->key_len = len;
468
469 /* Comp structure */
470 comp->type = buffer_get_int(&b);
471 comp->enabled = buffer_get_int(&b);
472 comp->name = buffer_get_string(&b, NULL);
473
474 len = buffer_len(&b);
475 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000476 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000477 buffer_free(&b);
478 return (newkey);
479}
480
481int
482mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
483{
484 Buffer b;
485 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000486 Enc *enc;
487 Mac *mac;
488 Comp *comp;
489 Newkeys *newkey = newkeys[mode];
490
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000491 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000492
493 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000494 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000495 return 0;
496 }
497 enc = &newkey->enc;
498 mac = &newkey->mac;
499 comp = &newkey->comp;
500
501 buffer_init(&b);
502 /* Enc structure */
503 buffer_put_cstring(&b, enc->name);
504 /* The cipher struct is constant and shared, you export pointer */
505 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
506 buffer_put_int(&b, enc->enabled);
507 buffer_put_int(&b, enc->block_size);
508 buffer_put_string(&b, enc->key, enc->key_len);
509 packet_get_keyiv(mode, enc->iv, enc->block_size);
510 buffer_put_string(&b, enc->iv, enc->block_size);
511
512 /* Mac structure */
513 buffer_put_cstring(&b, mac->name);
514 buffer_put_int(&b, mac->enabled);
515 buffer_put_string(&b, mac->key, mac->key_len);
516
517 /* Comp structure */
518 buffer_put_int(&b, comp->type);
519 buffer_put_int(&b, comp->enabled);
520 buffer_put_cstring(&b, comp->name);
521
522 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523 if (lenp != NULL)
524 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000525 if (blobp != NULL) {
526 *blobp = xmalloc(len);
527 memcpy(*blobp, buffer_ptr(&b), len);
528 }
529 memset(buffer_ptr(&b), 0, len);
530 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000531 return len;
532}
533
534static void
535mm_send_kex(Buffer *m, Kex *kex)
536{
537 buffer_put_string(m, kex->session_id, kex->session_id_len);
538 buffer_put_int(m, kex->we_need);
539 buffer_put_int(m, kex->hostkey_type);
540 buffer_put_int(m, kex->kex_type);
541 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
542 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
543 buffer_put_int(m, kex->flags);
544 buffer_put_cstring(m, kex->client_version_string);
545 buffer_put_cstring(m, kex->server_version_string);
546}
547
548void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000549mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000550{
551 Buffer m;
552 u_char *blob, *p;
553 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000554 u_int32_t seqnr, packets;
555 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000556
557 buffer_init(&m);
558
559 if (!compat20) {
560 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000561 u_char *key;
562 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000563
564 buffer_put_int(&m, packet_get_protocol_flags());
565
566 buffer_put_int(&m, packet_get_ssh1_cipher());
567
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000568 debug3("%s: Sending ssh1 KEY+IV", __func__);
569 keylen = packet_get_encryption_key(NULL);
570 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
571 keylen = packet_get_encryption_key(key);
572 buffer_put_string(&m, key, keylen);
573 memset(key, 0, keylen);
574 xfree(key);
575
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000576 ivlen = packet_get_keyiv_len(MODE_OUT);
577 packet_get_keyiv(MODE_OUT, iv, ivlen);
578 buffer_put_string(&m, iv, ivlen);
579 ivlen = packet_get_keyiv_len(MODE_OUT);
580 packet_get_keyiv(MODE_IN, iv, ivlen);
581 buffer_put_string(&m, iv, ivlen);
582 goto skip;
583 } else {
584 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000585 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586 }
587
588 debug3("%s: Sending new keys: %p %p",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000589 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000590
591 /* Keys from Kex */
592 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000593 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594
595 buffer_put_string(&m, blob, bloblen);
596 xfree(blob);
597
598 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000599 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000600
601 buffer_put_string(&m, blob, bloblen);
602 xfree(blob);
603
Damien Millera5539d22003-04-09 20:50:06 +1000604 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
605 buffer_put_int(&m, seqnr);
606 buffer_put_int64(&m, blocks);
607 buffer_put_int(&m, packets);
Damien Millerb1ecd9c2003-04-09 20:51:24 +1000608 packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
Damien Millera5539d22003-04-09 20:50:06 +1000609 buffer_put_int(&m, seqnr);
610 buffer_put_int64(&m, blocks);
611 buffer_put_int(&m, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000612
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000613 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000614 skip:
615 /* More key context */
616 plen = packet_get_keycontext(MODE_OUT, NULL);
617 p = xmalloc(plen+1);
618 packet_get_keycontext(MODE_OUT, p);
619 buffer_put_string(&m, p, plen);
620 xfree(p);
621
622 plen = packet_get_keycontext(MODE_IN, NULL);
623 p = xmalloc(plen+1);
624 packet_get_keycontext(MODE_IN, p);
625 buffer_put_string(&m, p, plen);
626 xfree(p);
627
628 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000629 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000630 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
631 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
632
633 /* Network I/O buffers */
634 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
635 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
636
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000637 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000638 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000639
640 buffer_free(&m);
641}
642
643int
644mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
645{
646 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000647 char *p, *msg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000648 int success = 0;
649
650 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000651 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000652
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000653 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000654 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000655
656 success = buffer_get_int(&m);
657 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000658 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000659 buffer_free(&m);
660 return (0);
661 }
662 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000663 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000664 buffer_free(&m);
665
666 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
667 xfree(p);
668
Darren Tucker09991742004-07-17 17:05:14 +1000669 buffer_append(&loginmsg, msg, strlen(msg));
670 xfree(msg);
671
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000672 *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
673 *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000674
675 /* Success */
676 return (1);
677}
678
679void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000680mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000681{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000682 Buffer m;
683
684 if (s->ttyfd == -1)
685 return;
686 buffer_init(&m);
687 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000688 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000689 buffer_free(&m);
690
691 /* closed dup'ed master */
692 if (close(s->ptymaster) < 0)
693 error("close(s->ptymaster): %s", strerror(errno));
694
695 /* unlink pty from session */
696 s->ttyfd = -1;
697}
698
Damien Miller79418552002-04-23 20:28:48 +1000699#ifdef USE_PAM
700void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100701mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000702{
703 Buffer m;
704
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000705 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000706 if (!options.use_pam)
707 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000708
709 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000710 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000711
712 buffer_free(&m);
713}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000714
Damien Miller1f499fd2003-08-25 13:08:49 +1000715u_int
716mm_do_pam_account(void)
717{
718 Buffer m;
719 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000720 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000721
722 debug3("%s entering", __func__);
723 if (!options.use_pam)
724 fatal("UsePAM=no, but ended up in %s anyway", __func__);
725
726 buffer_init(&m);
727 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
728
Damien Millera8e06ce2003-11-21 23:48:55 +1100729 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000730 MONITOR_ANS_PAM_ACCOUNT, &m);
731 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000732 msg = buffer_get_string(&m, NULL);
733 buffer_append(&loginmsg, msg, strlen(msg));
734 xfree(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000735
736 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100737
Damien Miller1f499fd2003-08-25 13:08:49 +1000738 debug3("%s returning %d", __func__, ret);
739
740 return (ret);
741}
742
Damien Miller4f9f42a2003-05-10 19:28:02 +1000743void *
744mm_sshpam_init_ctx(Authctxt *authctxt)
745{
746 Buffer m;
747 int success;
748
749 debug3("%s", __func__);
750 buffer_init(&m);
751 buffer_put_cstring(&m, authctxt->user);
752 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
753 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
754 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
755 success = buffer_get_int(&m);
756 if (success == 0) {
757 debug3("%s: pam_init_ctx failed", __func__);
758 buffer_free(&m);
759 return (NULL);
760 }
761 buffer_free(&m);
762 return (authctxt);
763}
764
765int
766mm_sshpam_query(void *ctx, char **name, char **info,
767 u_int *num, char ***prompts, u_int **echo_on)
768{
769 Buffer m;
770 int i, ret;
771
772 debug3("%s", __func__);
773 buffer_init(&m);
774 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
775 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
776 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
777 ret = buffer_get_int(&m);
778 debug3("%s: pam_query returned %d", __func__, ret);
779 *name = buffer_get_string(&m, NULL);
780 *info = buffer_get_string(&m, NULL);
781 *num = buffer_get_int(&m);
782 *prompts = xmalloc((*num + 1) * sizeof(char *));
783 *echo_on = xmalloc((*num + 1) * sizeof(u_int));
784 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;
796 int i, ret;
797
798 debug3("%s", __func__);
799 buffer_init(&m);
800 buffer_put_int(&m, num);
801 for (i = 0; i < num; ++i)
802 buffer_put_cstring(&m, resp[i]);
803 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
804 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
805 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
806 ret = buffer_get_int(&m);
807 debug3("%s: pam_respond returned %d", __func__, ret);
808 buffer_free(&m);
809 return (ret);
810}
811
812void
813mm_sshpam_free_ctx(void *ctxtp)
814{
815 Buffer m;
816
817 debug3("%s", __func__);
818 buffer_init(&m);
819 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
820 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
821 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
822 buffer_free(&m);
823}
Damien Miller79418552002-04-23 20:28:48 +1000824#endif /* USE_PAM */
825
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000826/* Request process termination */
827
828void
829mm_terminate(void)
830{
831 Buffer m;
832
833 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000834 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000835 buffer_free(&m);
836}
837
838int
839mm_ssh1_session_key(BIGNUM *num)
840{
841 int rsafail;
842 Buffer m;
843
844 buffer_init(&m);
845 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000846 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000847
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000848 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000849
850 rsafail = buffer_get_int(&m);
851 buffer_get_bignum2(&m, num);
852
853 buffer_free(&m);
854
855 return (rsafail);
856}
857
858static void
859mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
860 char ***prompts, u_int **echo_on)
861{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000862 *name = xstrdup("");
863 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000864 *numprompts = 1;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000865 *prompts = xmalloc(*numprompts * sizeof(char *));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000866 *echo_on = xmalloc(*numprompts * sizeof(u_int));
867 (*echo_on)[0] = 0;
868}
869
870int
871mm_bsdauth_query(void *ctx, char **name, char **infotxt,
872 u_int *numprompts, char ***prompts, u_int **echo_on)
873{
874 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100875 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000876 char *challenge;
877
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000878 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000879
880 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000881 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000882
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000883 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000884 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100885 success = buffer_get_int(&m);
886 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000887 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000888 buffer_free(&m);
889 return (-1);
890 }
891
892 /* Get the challenge, and format the response */
893 challenge = buffer_get_string(&m, NULL);
894 buffer_free(&m);
895
896 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
897 (*prompts)[0] = challenge;
898
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000899 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000900
901 return (0);
902}
903
904int
905mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
906{
907 Buffer m;
908 int authok;
909
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000910 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000911 if (numresponses != 1)
912 return (-1);
913
914 buffer_init(&m);
915 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000916 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000917
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000918 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000919 MONITOR_ANS_BSDAUTHRESPOND, &m);
920
921 authok = buffer_get_int(&m);
922 buffer_free(&m);
923
924 return ((authok == 0) ? -1 : 0);
925}
926
Darren Tucker042e2e82004-07-08 23:09:42 +1000927#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000928int
929mm_skey_query(void *ctx, char **name, char **infotxt,
930 u_int *numprompts, char ***prompts, u_int **echo_on)
931{
932 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100933 int len;
934 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000935 char *p, *challenge;
936
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000937 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000938
939 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000940 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000941
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000942 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000943 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100944 success = buffer_get_int(&m);
945 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000946 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000947 buffer_free(&m);
948 return (-1);
949 }
950
951 /* Get the challenge, and format the response */
952 challenge = buffer_get_string(&m, NULL);
953 buffer_free(&m);
954
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000955 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000956
957 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
958
959 len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
960 p = xmalloc(len);
961 strlcpy(p, challenge, len);
962 strlcat(p, SKEY_PROMPT, len);
963 (*prompts)[0] = p;
964 xfree(challenge);
965
966 return (0);
967}
968
969int
970mm_skey_respond(void *ctx, u_int numresponses, char **responses)
971{
972 Buffer m;
973 int authok;
974
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000975 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976 if (numresponses != 1)
977 return (-1);
978
979 buffer_init(&m);
980 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000981 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000982
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000983 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000984 MONITOR_ANS_SKEYRESPOND, &m);
985
986 authok = buffer_get_int(&m);
987 buffer_free(&m);
988
989 return ((authok == 0) ? -1 : 0);
990}
Darren Tucker042e2e82004-07-08 23:09:42 +1000991#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000992
993void
994mm_ssh1_session_id(u_char session_id[16])
995{
996 Buffer m;
997 int i;
998
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000999 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001000
1001 buffer_init(&m);
1002 for (i = 0; i < 16; i++)
1003 buffer_put_char(&m, session_id[i]);
1004
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001005 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001006 buffer_free(&m);
1007}
1008
1009int
1010mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1011{
1012 Buffer m;
1013 Key *key;
1014 u_char *blob;
1015 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001016 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001017
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001018 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001019
1020 buffer_init(&m);
1021 buffer_put_bignum2(&m, client_n);
1022
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001023 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1024 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001025
1026 allowed = buffer_get_int(&m);
1027
Damien Miller06ebedf2003-02-24 12:03:38 +11001028 /* fake forced command */
1029 auth_clear_options();
1030 have_forced = buffer_get_int(&m);
1031 forced_command = have_forced ? xstrdup("true") : NULL;
1032
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001033 if (allowed && rkey != NULL) {
1034 blob = buffer_get_string(&m, &blen);
1035 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001036 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001037 *rkey = key;
1038 xfree(blob);
1039 }
1040 mm_send_debug(&m);
1041 buffer_free(&m);
1042
1043 return (allowed);
1044}
1045
1046BIGNUM *
1047mm_auth_rsa_generate_challenge(Key *key)
1048{
1049 Buffer m;
1050 BIGNUM *challenge;
1051 u_char *blob;
1052 u_int blen;
1053
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001054 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001055
1056 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001057 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001058
1059 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1060 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001061 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001062 key->type = KEY_RSA1;
1063
1064 buffer_init(&m);
1065 buffer_put_string(&m, blob, blen);
1066 xfree(blob);
1067
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001068 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1069 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001070
1071 buffer_get_bignum2(&m, challenge);
1072 buffer_free(&m);
1073
1074 return (challenge);
1075}
1076
1077int
1078mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1079{
1080 Buffer m;
1081 u_char *blob;
1082 u_int blen;
1083 int success = 0;
1084
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001085 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001086
1087 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1088 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001089 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001090 key->type = KEY_RSA1;
1091
1092 buffer_init(&m);
1093 buffer_put_string(&m, blob, blen);
1094 buffer_put_string(&m, response, 16);
1095 xfree(blob);
1096
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001097 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1098 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001099
1100 success = buffer_get_int(&m);
1101 buffer_free(&m);
1102
1103 return (success);
1104}
Damien Miller25162f22002-09-12 09:47:29 +10001105
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001106#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001107void
1108mm_audit_event(ssh_audit_event_t event)
1109{
1110 Buffer m;
1111
1112 debug3("%s entering", __func__);
1113
1114 buffer_init(&m);
1115 buffer_put_int(&m, event);
1116
1117 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1118 buffer_free(&m);
1119}
1120
1121void
1122mm_audit_run_command(const char *command)
1123{
1124 Buffer m;
1125
1126 debug3("%s entering command %s", __func__, command);
1127
1128 buffer_init(&m);
1129 buffer_put_cstring(&m, command);
1130
1131 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1132 buffer_free(&m);
1133}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001134#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001135
Darren Tucker0efd1552003-08-26 11:49:55 +10001136#ifdef GSSAPI
1137OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001138mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001139{
1140 Buffer m;
1141 OM_uint32 major;
1142
1143 /* Client doesn't get to see the context */
1144 *ctx = NULL;
1145
1146 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001147 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001148
1149 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1150 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1151
1152 major = buffer_get_int(&m);
1153
1154 buffer_free(&m);
1155 return (major);
1156}
1157
1158OM_uint32
1159mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1160 gss_buffer_desc *out, OM_uint32 *flags)
1161{
1162 Buffer m;
1163 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001164 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001165
1166 buffer_init(&m);
1167 buffer_put_string(&m, in->value, in->length);
1168
1169 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1170 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1171
1172 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001173 out->value = buffer_get_string(&m, &len);
1174 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001175 if (flags)
1176 *flags = buffer_get_int(&m);
1177
1178 buffer_free(&m);
1179
1180 return (major);
1181}
1182
Damien Miller0425d402003-11-17 22:18:21 +11001183OM_uint32
1184mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1185{
1186 Buffer m;
1187 OM_uint32 major;
1188
1189 buffer_init(&m);
1190 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1191 buffer_put_string(&m, gssmic->value, gssmic->length);
1192
1193 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1194 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1195 &m);
1196
1197 major = buffer_get_int(&m);
1198 buffer_free(&m);
1199 return(major);
1200}
1201
Darren Tucker0efd1552003-08-26 11:49:55 +10001202int
1203mm_ssh_gssapi_userok(char *user)
1204{
1205 Buffer m;
1206 int authenticated = 0;
1207
1208 buffer_init(&m);
1209
1210 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1211 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1212 &m);
1213
1214 authenticated = buffer_get_int(&m);
1215
1216 buffer_free(&m);
1217 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1218 return (authenticated);
1219}
1220#endif /* GSSAPI */