blob: fe14604226b90d489f083aa7337850343b07c62d [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 Tuckera47c9bc2003-11-03 20:03:25 +110028RCSID("$OpenBSD: monitor_wrap.c,v 1.34 2003/10/15 09:48:45 markus 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"
43#include "zlib.h"
44#include "monitor.h"
45#include "monitor_wrap.h"
46#include "xmalloc.h"
47#include "atomicio.h"
48#include "monitor_fdpass.h"
49#include "getput.h"
Damien Miller4e448a32003-05-14 15:11:48 +100050#include "servconf.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000051
52#include "auth.h"
53#include "channels.h"
54#include "session.h"
55
Darren Tucker0efd1552003-08-26 11:49:55 +100056#ifdef GSSAPI
57#include "ssh-gss.h"
58#endif
59
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000060/* Imports */
61extern int compat20;
62extern Newkeys *newkeys[];
63extern z_stream incoming_stream;
64extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000065extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000066extern Buffer input, output;
Damien Miller4e448a32003-05-14 15:11:48 +100067extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000068
Darren Tucker3e33cec2003-10-02 16:12:36 +100069int
70mm_is_monitor(void)
71{
72 /*
73 * m_pid is only set in the privileged part, and
74 * points to the unprivileged child.
75 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +110076 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +100077}
78
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000079void
80mm_request_send(int socket, enum monitor_reqtype type, Buffer *m)
81{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000082 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +000083 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000084
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000085 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000086
87 PUT_32BIT(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +000088 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker9f63f222003-07-03 13:46:56 +100089 if (atomicio(vwrite, socket, buf, sizeof(buf)) != sizeof(buf))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000090 fatal("%s: write", __func__);
Darren Tucker9f63f222003-07-03 13:46:56 +100091 if (atomicio(vwrite, socket, buffer_ptr(m), mlen) != mlen)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000092 fatal("%s: write", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093}
94
95void
96mm_request_receive(int socket, Buffer *m)
97{
98 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000099 u_int msg_len;
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +0000100 ssize_t res;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000102 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000103
104 res = atomicio(read, socket, buf, sizeof(buf));
105 if (res != sizeof(buf)) {
106 if (res == 0)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000107 cleanup_exit(255);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000108 fatal("%s: read: %ld", __func__, (long)res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000109 }
110 msg_len = GET_32BIT(buf);
111 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000112 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000113 buffer_clear(m);
114 buffer_append_space(m, msg_len);
115 res = atomicio(read, socket, buffer_ptr(m), msg_len);
116 if (res != msg_len)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000117 fatal("%s: read: %ld != msg_len", __func__, (long)res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000118}
119
120void
121mm_request_receive_expect(int socket, enum monitor_reqtype type, Buffer *m)
122{
123 u_char rtype;
124
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000125 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000126
127 mm_request_receive(socket, m);
128 rtype = buffer_get_char(m);
129 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000130 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000131 rtype, type);
132}
133
134DH *
135mm_choose_dh(int min, int nbits, int max)
136{
137 BIGNUM *p, *g;
138 int success = 0;
139 Buffer m;
140
141 buffer_init(&m);
142 buffer_put_int(&m, min);
143 buffer_put_int(&m, nbits);
144 buffer_put_int(&m, max);
145
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000146 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000147
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000148 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000149 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000150
151 success = buffer_get_char(&m);
152 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000153 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000154
155 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000156 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000157 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000158 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000159 buffer_get_bignum2(&m, p);
160 buffer_get_bignum2(&m, g);
161
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000162 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000163 buffer_free(&m);
164
165 return (dh_new_group(g, p));
166}
167
168int
169mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
170{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000171 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000172 Buffer m;
173
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000174 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000175
176 buffer_init(&m);
177 buffer_put_int(&m, kex->host_key_index(key));
178 buffer_put_string(&m, data, datalen);
179
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000180 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000181
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000182 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000183 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000184 *sigp = buffer_get_string(&m, lenp);
185 buffer_free(&m);
186
187 return (0);
188}
189
190struct passwd *
191mm_getpwnamallow(const char *login)
192{
193 Buffer m;
194 struct passwd *pw;
195 u_int pwlen;
196
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000197 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000198
199 buffer_init(&m);
200 buffer_put_cstring(&m, login);
201
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000202 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000203
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000204 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000205 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206
207 if (buffer_get_char(&m) == 0) {
208 buffer_free(&m);
209 return (NULL);
210 }
211 pw = buffer_get_string(&m, &pwlen);
212 if (pwlen != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000213 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000214 pw->pw_name = buffer_get_string(&m, NULL);
215 pw->pw_passwd = buffer_get_string(&m, NULL);
216 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000217#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000218 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000219#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000220 pw->pw_dir = buffer_get_string(&m, NULL);
221 pw->pw_shell = buffer_get_string(&m, NULL);
222 buffer_free(&m);
223
224 return (pw);
225}
226
Darren Tucker7eb3de02003-10-15 15:56:58 +1000227char *
228mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000229{
230 Buffer m;
231 char *banner;
232
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000233 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000234
235 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000236 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000237 buffer_clear(&m);
238
Darren Tucker7eb3de02003-10-15 15:56:58 +1000239 mm_request_receive_expect(pmonitor->m_recvfd,
240 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000241 banner = buffer_get_string(&m, NULL);
242 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000243
Darren Tucker7eb3de02003-10-15 15:56:58 +1000244 /* treat empty banner as missing banner */
245 if (strlen(banner) == 0) {
246 xfree(banner);
247 banner = NULL;
248 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000249 return (banner);
250}
251
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000252/* Inform the privileged process about service and style */
253
254void
255mm_inform_authserv(char *service, char *style)
256{
257 Buffer m;
258
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000259 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000260
261 buffer_init(&m);
262 buffer_put_cstring(&m, service);
263 buffer_put_cstring(&m, style ? style : "");
264
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000265 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000266
267 buffer_free(&m);
268}
269
270/* Do the password authentication */
271int
272mm_auth_password(Authctxt *authctxt, char *password)
273{
274 Buffer m;
275 int authenticated = 0;
276
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000277 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000278
279 buffer_init(&m);
280 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000281 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000282
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000283 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000284 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000285
286 authenticated = buffer_get_int(&m);
287
288 buffer_free(&m);
289
290 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000291 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000292 return (authenticated);
293}
294
295int
296mm_user_key_allowed(struct passwd *pw, Key *key)
297{
298 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
299}
300
301int
302mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
303 Key *key)
304{
305 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
306}
307
308int
309mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
310 char *host, Key *key)
311{
312 int ret;
313
314 key->type = KEY_RSA; /* XXX hack for key_to_blob */
315 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
316 key->type = KEY_RSA1;
317 return (ret);
318}
319
320static void
321mm_send_debug(Buffer *m)
322{
323 char *msg;
324
325 while (buffer_len(m)) {
326 msg = buffer_get_string(m, NULL);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000327 debug3("%s: Sending debug: %s", __func__, msg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000328 packet_send_debug("%s", msg);
329 xfree(msg);
330 }
331}
332
333int
334mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
335{
336 Buffer m;
337 u_char *blob;
338 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100339 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000340
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000341 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000342
343 /* Convert the key to a blob and the pass it over */
344 if (!key_to_blob(key, &blob, &len))
345 return (0);
346
347 buffer_init(&m);
348 buffer_put_int(&m, type);
349 buffer_put_cstring(&m, user ? user : "");
350 buffer_put_cstring(&m, host ? host : "");
351 buffer_put_string(&m, blob, len);
352 xfree(blob);
353
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000354 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000355
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000356 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000357 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000358
359 allowed = buffer_get_int(&m);
360
Damien Miller06ebedf2003-02-24 12:03:38 +1100361 /* fake forced command */
362 auth_clear_options();
363 have_forced = buffer_get_int(&m);
364 forced_command = have_forced ? xstrdup("true") : NULL;
365
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000366 /* Send potential debug messages */
367 mm_send_debug(&m);
368
369 buffer_free(&m);
370
371 return (allowed);
372}
373
374/*
375 * This key verify needs to send the key type along, because the
376 * privileged parent makes the decision if the key is allowed
377 * for authentication.
378 */
379
380int
381mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
382{
383 Buffer m;
384 u_char *blob;
385 u_int len;
386 int verified = 0;
387
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000388 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000389
390 /* Convert the key to a blob and the pass it over */
391 if (!key_to_blob(key, &blob, &len))
392 return (0);
393
394 buffer_init(&m);
395 buffer_put_string(&m, blob, len);
396 buffer_put_string(&m, sig, siglen);
397 buffer_put_string(&m, data, datalen);
398 xfree(blob);
399
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000400 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000401
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000402 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000403 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000404
405 verified = buffer_get_int(&m);
406
407 buffer_free(&m);
408
409 return (verified);
410}
411
412/* Export key state after authentication */
413Newkeys *
414mm_newkeys_from_blob(u_char *blob, int blen)
415{
416 Buffer b;
417 u_int len;
418 Newkeys *newkey = NULL;
419 Enc *enc;
420 Mac *mac;
421 Comp *comp;
422
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000423 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000424#ifdef DEBUG_PK
425 dump_base64(stderr, blob, blen);
426#endif
427 buffer_init(&b);
428 buffer_append(&b, blob, blen);
429
430 newkey = xmalloc(sizeof(*newkey));
431 enc = &newkey->enc;
432 mac = &newkey->mac;
433 comp = &newkey->comp;
434
435 /* Enc structure */
436 enc->name = buffer_get_string(&b, NULL);
437 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
438 enc->enabled = buffer_get_int(&b);
439 enc->block_size = buffer_get_int(&b);
440 enc->key = buffer_get_string(&b, &enc->key_len);
441 enc->iv = buffer_get_string(&b, &len);
442 if (len != enc->block_size)
Ben Lindstrom08512492002-06-27 00:23:02 +0000443 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000444 enc->block_size, len);
445
446 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000447 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000448 enc->name, enc->cipher);
449
450 /* Mac structure */
451 mac->name = buffer_get_string(&b, NULL);
452 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000453 fatal("%s: can not init mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000454 mac->enabled = buffer_get_int(&b);
455 mac->key = buffer_get_string(&b, &len);
456 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000457 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000458 mac->key_len);
459 mac->key_len = len;
460
461 /* Comp structure */
462 comp->type = buffer_get_int(&b);
463 comp->enabled = buffer_get_int(&b);
464 comp->name = buffer_get_string(&b, NULL);
465
466 len = buffer_len(&b);
467 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000468 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000469 buffer_free(&b);
470 return (newkey);
471}
472
473int
474mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
475{
476 Buffer b;
477 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000478 Enc *enc;
479 Mac *mac;
480 Comp *comp;
481 Newkeys *newkey = newkeys[mode];
482
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000483 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000484
485 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000486 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000487 return 0;
488 }
489 enc = &newkey->enc;
490 mac = &newkey->mac;
491 comp = &newkey->comp;
492
493 buffer_init(&b);
494 /* Enc structure */
495 buffer_put_cstring(&b, enc->name);
496 /* The cipher struct is constant and shared, you export pointer */
497 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
498 buffer_put_int(&b, enc->enabled);
499 buffer_put_int(&b, enc->block_size);
500 buffer_put_string(&b, enc->key, enc->key_len);
501 packet_get_keyiv(mode, enc->iv, enc->block_size);
502 buffer_put_string(&b, enc->iv, enc->block_size);
503
504 /* Mac structure */
505 buffer_put_cstring(&b, mac->name);
506 buffer_put_int(&b, mac->enabled);
507 buffer_put_string(&b, mac->key, mac->key_len);
508
509 /* Comp structure */
510 buffer_put_int(&b, comp->type);
511 buffer_put_int(&b, comp->enabled);
512 buffer_put_cstring(&b, comp->name);
513
514 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000515 if (lenp != NULL)
516 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000517 if (blobp != NULL) {
518 *blobp = xmalloc(len);
519 memcpy(*blobp, buffer_ptr(&b), len);
520 }
521 memset(buffer_ptr(&b), 0, len);
522 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523 return len;
524}
525
526static void
527mm_send_kex(Buffer *m, Kex *kex)
528{
529 buffer_put_string(m, kex->session_id, kex->session_id_len);
530 buffer_put_int(m, kex->we_need);
531 buffer_put_int(m, kex->hostkey_type);
532 buffer_put_int(m, kex->kex_type);
533 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
534 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
535 buffer_put_int(m, kex->flags);
536 buffer_put_cstring(m, kex->client_version_string);
537 buffer_put_cstring(m, kex->server_version_string);
538}
539
540void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000541mm_send_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000542{
543 Buffer m;
544 u_char *blob, *p;
545 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000546 u_int32_t seqnr, packets;
547 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000548
549 buffer_init(&m);
550
551 if (!compat20) {
552 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000553 u_char *key;
554 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000555
556 buffer_put_int(&m, packet_get_protocol_flags());
557
558 buffer_put_int(&m, packet_get_ssh1_cipher());
559
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000560 debug3("%s: Sending ssh1 KEY+IV", __func__);
561 keylen = packet_get_encryption_key(NULL);
562 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
563 keylen = packet_get_encryption_key(key);
564 buffer_put_string(&m, key, keylen);
565 memset(key, 0, keylen);
566 xfree(key);
567
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000568 ivlen = packet_get_keyiv_len(MODE_OUT);
569 packet_get_keyiv(MODE_OUT, iv, ivlen);
570 buffer_put_string(&m, iv, ivlen);
571 ivlen = packet_get_keyiv_len(MODE_OUT);
572 packet_get_keyiv(MODE_IN, iv, ivlen);
573 buffer_put_string(&m, iv, ivlen);
574 goto skip;
575 } else {
576 /* Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000577 mm_send_kex(&m, *pmonitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000578 }
579
580 debug3("%s: Sending new keys: %p %p",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000581 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000582
583 /* Keys from Kex */
584 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000585 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586
587 buffer_put_string(&m, blob, bloblen);
588 xfree(blob);
589
590 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000591 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000592
593 buffer_put_string(&m, blob, bloblen);
594 xfree(blob);
595
Damien Millera5539d22003-04-09 20:50:06 +1000596 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
597 buffer_put_int(&m, seqnr);
598 buffer_put_int64(&m, blocks);
599 buffer_put_int(&m, packets);
Damien Millerb1ecd9c2003-04-09 20:51:24 +1000600 packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
Damien Millera5539d22003-04-09 20:50:06 +1000601 buffer_put_int(&m, seqnr);
602 buffer_put_int64(&m, blocks);
603 buffer_put_int(&m, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000605 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000606 skip:
607 /* More key context */
608 plen = packet_get_keycontext(MODE_OUT, NULL);
609 p = xmalloc(plen+1);
610 packet_get_keycontext(MODE_OUT, p);
611 buffer_put_string(&m, p, plen);
612 xfree(p);
613
614 plen = packet_get_keycontext(MODE_IN, NULL);
615 p = xmalloc(plen+1);
616 packet_get_keycontext(MODE_IN, p);
617 buffer_put_string(&m, p, plen);
618 xfree(p);
619
620 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000621 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000622 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
623 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
624
625 /* Network I/O buffers */
626 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
627 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
628
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000629 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000630 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000631
632 buffer_free(&m);
633}
634
635int
636mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
637{
638 Buffer m;
Damien Millera10f5612002-09-12 09:49:15 +1000639 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000640 int success = 0;
641
642 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000643 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000644
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000645 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000646 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000647
648 success = buffer_get_int(&m);
649 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000650 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000651 buffer_free(&m);
652 return (0);
653 }
654 p = buffer_get_string(&m, NULL);
655 buffer_free(&m);
656
657 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
658 xfree(p);
659
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000660 *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
661 *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000662
663 /* Success */
664 return (1);
665}
666
667void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000668mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000670 Buffer m;
671
672 if (s->ttyfd == -1)
673 return;
674 buffer_init(&m);
675 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000676 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000677 buffer_free(&m);
678
679 /* closed dup'ed master */
680 if (close(s->ptymaster) < 0)
681 error("close(s->ptymaster): %s", strerror(errno));
682
683 /* unlink pty from session */
684 s->ttyfd = -1;
685}
686
Damien Miller79418552002-04-23 20:28:48 +1000687#ifdef USE_PAM
688void
689mm_start_pam(char *user)
690{
691 Buffer m;
692
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000693 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000694 if (!options.use_pam)
695 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000696
697 buffer_init(&m);
698 buffer_put_cstring(&m, user);
699
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000700 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000701
702 buffer_free(&m);
703}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000704
Damien Miller1f499fd2003-08-25 13:08:49 +1000705u_int
706mm_do_pam_account(void)
707{
708 Buffer m;
709 u_int ret;
710
711 debug3("%s entering", __func__);
712 if (!options.use_pam)
713 fatal("UsePAM=no, but ended up in %s anyway", __func__);
714
715 buffer_init(&m);
716 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
717
718 mm_request_receive_expect(pmonitor->m_recvfd,
719 MONITOR_ANS_PAM_ACCOUNT, &m);
720 ret = buffer_get_int(&m);
721
722 buffer_free(&m);
723
724 debug3("%s returning %d", __func__, ret);
725
726 return (ret);
727}
728
Damien Miller4f9f42a2003-05-10 19:28:02 +1000729void *
730mm_sshpam_init_ctx(Authctxt *authctxt)
731{
732 Buffer m;
733 int success;
734
735 debug3("%s", __func__);
736 buffer_init(&m);
737 buffer_put_cstring(&m, authctxt->user);
738 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
739 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
740 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
741 success = buffer_get_int(&m);
742 if (success == 0) {
743 debug3("%s: pam_init_ctx failed", __func__);
744 buffer_free(&m);
745 return (NULL);
746 }
747 buffer_free(&m);
748 return (authctxt);
749}
750
751int
752mm_sshpam_query(void *ctx, char **name, char **info,
753 u_int *num, char ***prompts, u_int **echo_on)
754{
755 Buffer m;
756 int i, ret;
757
758 debug3("%s", __func__);
759 buffer_init(&m);
760 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
761 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
762 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
763 ret = buffer_get_int(&m);
764 debug3("%s: pam_query returned %d", __func__, ret);
765 *name = buffer_get_string(&m, NULL);
766 *info = buffer_get_string(&m, NULL);
767 *num = buffer_get_int(&m);
768 *prompts = xmalloc((*num + 1) * sizeof(char *));
769 *echo_on = xmalloc((*num + 1) * sizeof(u_int));
770 for (i = 0; i < *num; ++i) {
771 (*prompts)[i] = buffer_get_string(&m, NULL);
772 (*echo_on)[i] = buffer_get_int(&m);
773 }
774 buffer_free(&m);
775 return (ret);
776}
777
778int
779mm_sshpam_respond(void *ctx, u_int num, char **resp)
780{
781 Buffer m;
782 int i, ret;
783
784 debug3("%s", __func__);
785 buffer_init(&m);
786 buffer_put_int(&m, num);
787 for (i = 0; i < num; ++i)
788 buffer_put_cstring(&m, resp[i]);
789 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
790 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
791 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
792 ret = buffer_get_int(&m);
793 debug3("%s: pam_respond returned %d", __func__, ret);
794 buffer_free(&m);
795 return (ret);
796}
797
798void
799mm_sshpam_free_ctx(void *ctxtp)
800{
801 Buffer m;
802
803 debug3("%s", __func__);
804 buffer_init(&m);
805 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
806 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
807 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
808 buffer_free(&m);
809}
Damien Miller79418552002-04-23 20:28:48 +1000810#endif /* USE_PAM */
811
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000812/* Request process termination */
813
814void
815mm_terminate(void)
816{
817 Buffer m;
818
819 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000820 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000821 buffer_free(&m);
822}
823
824int
825mm_ssh1_session_key(BIGNUM *num)
826{
827 int rsafail;
828 Buffer m;
829
830 buffer_init(&m);
831 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000832 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000833
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000834 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000835
836 rsafail = buffer_get_int(&m);
837 buffer_get_bignum2(&m, num);
838
839 buffer_free(&m);
840
841 return (rsafail);
842}
843
844static void
845mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
846 char ***prompts, u_int **echo_on)
847{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000848 *name = xstrdup("");
849 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000850 *numprompts = 1;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000851 *prompts = xmalloc(*numprompts * sizeof(char *));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000852 *echo_on = xmalloc(*numprompts * sizeof(u_int));
853 (*echo_on)[0] = 0;
854}
855
856int
857mm_bsdauth_query(void *ctx, char **name, char **infotxt,
858 u_int *numprompts, char ***prompts, u_int **echo_on)
859{
860 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100861 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000862 char *challenge;
863
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000864 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000865
866 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000867 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000868
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000869 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000870 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100871 success = buffer_get_int(&m);
872 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000873 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000874 buffer_free(&m);
875 return (-1);
876 }
877
878 /* Get the challenge, and format the response */
879 challenge = buffer_get_string(&m, NULL);
880 buffer_free(&m);
881
882 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
883 (*prompts)[0] = challenge;
884
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000885 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000886
887 return (0);
888}
889
890int
891mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
892{
893 Buffer m;
894 int authok;
895
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000896 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000897 if (numresponses != 1)
898 return (-1);
899
900 buffer_init(&m);
901 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000902 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000903
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000904 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000905 MONITOR_ANS_BSDAUTHRESPOND, &m);
906
907 authok = buffer_get_int(&m);
908 buffer_free(&m);
909
910 return ((authok == 0) ? -1 : 0);
911}
912
913int
914mm_skey_query(void *ctx, char **name, char **infotxt,
915 u_int *numprompts, char ***prompts, u_int **echo_on)
916{
917 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100918 int len;
919 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000920 char *p, *challenge;
921
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000922 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000923
924 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000925 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000927 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000928 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100929 success = buffer_get_int(&m);
930 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000931 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932 buffer_free(&m);
933 return (-1);
934 }
935
936 /* Get the challenge, and format the response */
937 challenge = buffer_get_string(&m, NULL);
938 buffer_free(&m);
939
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000940 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000941
942 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
943
944 len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
945 p = xmalloc(len);
946 strlcpy(p, challenge, len);
947 strlcat(p, SKEY_PROMPT, len);
948 (*prompts)[0] = p;
949 xfree(challenge);
950
951 return (0);
952}
953
954int
955mm_skey_respond(void *ctx, u_int numresponses, char **responses)
956{
957 Buffer m;
958 int authok;
959
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000960 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961 if (numresponses != 1)
962 return (-1);
963
964 buffer_init(&m);
965 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000966 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000967
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000968 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000969 MONITOR_ANS_SKEYRESPOND, &m);
970
971 authok = buffer_get_int(&m);
972 buffer_free(&m);
973
974 return ((authok == 0) ? -1 : 0);
975}
976
977void
978mm_ssh1_session_id(u_char session_id[16])
979{
980 Buffer m;
981 int i;
982
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000983 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000984
985 buffer_init(&m);
986 for (i = 0; i < 16; i++)
987 buffer_put_char(&m, session_id[i]);
988
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000989 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000990 buffer_free(&m);
991}
992
993int
994mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
995{
996 Buffer m;
997 Key *key;
998 u_char *blob;
999 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001000 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001001
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001002 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001003
1004 buffer_init(&m);
1005 buffer_put_bignum2(&m, client_n);
1006
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001007 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1008 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001009
1010 allowed = buffer_get_int(&m);
1011
Damien Miller06ebedf2003-02-24 12:03:38 +11001012 /* fake forced command */
1013 auth_clear_options();
1014 have_forced = buffer_get_int(&m);
1015 forced_command = have_forced ? xstrdup("true") : NULL;
1016
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001017 if (allowed && rkey != NULL) {
1018 blob = buffer_get_string(&m, &blen);
1019 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001020 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001021 *rkey = key;
1022 xfree(blob);
1023 }
1024 mm_send_debug(&m);
1025 buffer_free(&m);
1026
1027 return (allowed);
1028}
1029
1030BIGNUM *
1031mm_auth_rsa_generate_challenge(Key *key)
1032{
1033 Buffer m;
1034 BIGNUM *challenge;
1035 u_char *blob;
1036 u_int blen;
1037
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001038 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001039
1040 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001041 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001042
1043 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1044 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001045 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001046 key->type = KEY_RSA1;
1047
1048 buffer_init(&m);
1049 buffer_put_string(&m, blob, blen);
1050 xfree(blob);
1051
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001052 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1053 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001054
1055 buffer_get_bignum2(&m, challenge);
1056 buffer_free(&m);
1057
1058 return (challenge);
1059}
1060
1061int
1062mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1063{
1064 Buffer m;
1065 u_char *blob;
1066 u_int blen;
1067 int success = 0;
1068
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001069 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001070
1071 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1072 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001073 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001074 key->type = KEY_RSA1;
1075
1076 buffer_init(&m);
1077 buffer_put_string(&m, blob, blen);
1078 buffer_put_string(&m, response, 16);
1079 xfree(blob);
1080
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001081 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1082 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001083
1084 success = buffer_get_int(&m);
1085 buffer_free(&m);
1086
1087 return (success);
1088}
Damien Miller25162f22002-09-12 09:47:29 +10001089
Darren Tucker0efd1552003-08-26 11:49:55 +10001090#ifdef GSSAPI
1091OM_uint32
1092mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
1093{
1094 Buffer m;
1095 OM_uint32 major;
1096
1097 /* Client doesn't get to see the context */
1098 *ctx = NULL;
1099
1100 buffer_init(&m);
1101 buffer_put_string(&m, oid->elements, oid->length);
1102
1103 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1104 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1105
1106 major = buffer_get_int(&m);
1107
1108 buffer_free(&m);
1109 return (major);
1110}
1111
1112OM_uint32
1113mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1114 gss_buffer_desc *out, OM_uint32 *flags)
1115{
1116 Buffer m;
1117 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001118 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001119
1120 buffer_init(&m);
1121 buffer_put_string(&m, in->value, in->length);
1122
1123 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1124 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1125
1126 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001127 out->value = buffer_get_string(&m, &len);
1128 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001129 if (flags)
1130 *flags = buffer_get_int(&m);
1131
1132 buffer_free(&m);
1133
1134 return (major);
1135}
1136
1137int
1138mm_ssh_gssapi_userok(char *user)
1139{
1140 Buffer m;
1141 int authenticated = 0;
1142
1143 buffer_init(&m);
1144
1145 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1146 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1147 &m);
1148
1149 authenticated = buffer_get_int(&m);
1150
1151 buffer_free(&m);
1152 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1153 return (authenticated);
1154}
1155#endif /* GSSAPI */