blob: 82649a7cc859dd3d332c4848d2e592aaa010335e [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 Tucker600ad8d2003-08-26 12:10:48 +100028RCSID("$OpenBSD: monitor_wrap.c,v 1.30 2003/08/24 17:36:52 deraadt 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
69void
70mm_request_send(int socket, enum monitor_reqtype type, Buffer *m)
71{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000072 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +000073 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000074
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000075 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000076
77 PUT_32BIT(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +000078 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker9f63f222003-07-03 13:46:56 +100079 if (atomicio(vwrite, socket, buf, sizeof(buf)) != sizeof(buf))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000080 fatal("%s: write", __func__);
Darren Tucker9f63f222003-07-03 13:46:56 +100081 if (atomicio(vwrite, socket, buffer_ptr(m), mlen) != mlen)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000082 fatal("%s: write", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000083}
84
85void
86mm_request_receive(int socket, Buffer *m)
87{
88 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000089 u_int msg_len;
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +000090 ssize_t res;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000091
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000092 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093
94 res = atomicio(read, socket, buf, sizeof(buf));
95 if (res != sizeof(buf)) {
96 if (res == 0)
97 fatal_cleanup();
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000098 fatal("%s: read: %ld", __func__, (long)res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000099 }
100 msg_len = GET_32BIT(buf);
101 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000102 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000103 buffer_clear(m);
104 buffer_append_space(m, msg_len);
105 res = atomicio(read, socket, buffer_ptr(m), msg_len);
106 if (res != msg_len)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000107 fatal("%s: read: %ld != msg_len", __func__, (long)res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000108}
109
110void
111mm_request_receive_expect(int socket, enum monitor_reqtype type, Buffer *m)
112{
113 u_char rtype;
114
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000115 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000116
117 mm_request_receive(socket, m);
118 rtype = buffer_get_char(m);
119 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000120 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000121 rtype, type);
122}
123
124DH *
125mm_choose_dh(int min, int nbits, int max)
126{
127 BIGNUM *p, *g;
128 int success = 0;
129 Buffer m;
130
131 buffer_init(&m);
132 buffer_put_int(&m, min);
133 buffer_put_int(&m, nbits);
134 buffer_put_int(&m, max);
135
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000136 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000137
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000138 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000139 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000140
141 success = buffer_get_char(&m);
142 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000143 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000144
145 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000146 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000147 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000148 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000149 buffer_get_bignum2(&m, p);
150 buffer_get_bignum2(&m, g);
151
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000152 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000153 buffer_free(&m);
154
155 return (dh_new_group(g, p));
156}
157
158int
159mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
160{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000161 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000162 Buffer m;
163
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000164 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000165
166 buffer_init(&m);
167 buffer_put_int(&m, kex->host_key_index(key));
168 buffer_put_string(&m, data, datalen);
169
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000170 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000172 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000173 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000174 *sigp = buffer_get_string(&m, lenp);
175 buffer_free(&m);
176
177 return (0);
178}
179
180struct passwd *
181mm_getpwnamallow(const char *login)
182{
183 Buffer m;
184 struct passwd *pw;
185 u_int pwlen;
186
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000187 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000188
189 buffer_init(&m);
190 buffer_put_cstring(&m, login);
191
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000192 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000193
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000194 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000195 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000196
197 if (buffer_get_char(&m) == 0) {
198 buffer_free(&m);
199 return (NULL);
200 }
201 pw = buffer_get_string(&m, &pwlen);
202 if (pwlen != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000203 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000204 pw->pw_name = buffer_get_string(&m, NULL);
205 pw->pw_passwd = buffer_get_string(&m, NULL);
206 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000207#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000208 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000209#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000210 pw->pw_dir = buffer_get_string(&m, NULL);
211 pw->pw_shell = buffer_get_string(&m, NULL);
212 buffer_free(&m);
213
214 return (pw);
215}
216
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000217char *mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000218{
219 Buffer m;
220 char *banner;
221
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000222 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000223
224 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000225 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000226 buffer_clear(&m);
227
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000228 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000229 banner = buffer_get_string(&m, NULL);
230 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000231
Damien Miller5ad9fd92002-05-13 11:07:41 +1000232 return (banner);
233}
234
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000235/* Inform the privileged process about service and style */
236
237void
238mm_inform_authserv(char *service, char *style)
239{
240 Buffer m;
241
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000242 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000243
244 buffer_init(&m);
245 buffer_put_cstring(&m, service);
246 buffer_put_cstring(&m, style ? style : "");
247
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000248 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000249
250 buffer_free(&m);
251}
252
253/* Do the password authentication */
254int
255mm_auth_password(Authctxt *authctxt, char *password)
256{
257 Buffer m;
258 int authenticated = 0;
259
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000260 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000261
262 buffer_init(&m);
263 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000264 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000265
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000266 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000267 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000268
269 authenticated = buffer_get_int(&m);
270
271 buffer_free(&m);
272
273 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000274 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000275 return (authenticated);
276}
277
278int
279mm_user_key_allowed(struct passwd *pw, Key *key)
280{
281 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
282}
283
284int
285mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
286 Key *key)
287{
288 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
289}
290
291int
292mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
293 char *host, Key *key)
294{
295 int ret;
296
297 key->type = KEY_RSA; /* XXX hack for key_to_blob */
298 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
299 key->type = KEY_RSA1;
300 return (ret);
301}
302
303static void
304mm_send_debug(Buffer *m)
305{
306 char *msg;
307
308 while (buffer_len(m)) {
309 msg = buffer_get_string(m, NULL);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000310 debug3("%s: Sending debug: %s", __func__, msg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000311 packet_send_debug("%s", msg);
312 xfree(msg);
313 }
314}
315
316int
317mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
318{
319 Buffer m;
320 u_char *blob;
321 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100322 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000323
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000324 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000325
326 /* Convert the key to a blob and the pass it over */
327 if (!key_to_blob(key, &blob, &len))
328 return (0);
329
330 buffer_init(&m);
331 buffer_put_int(&m, type);
332 buffer_put_cstring(&m, user ? user : "");
333 buffer_put_cstring(&m, host ? host : "");
334 buffer_put_string(&m, blob, len);
335 xfree(blob);
336
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000337 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000338
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000339 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000340 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000341
342 allowed = buffer_get_int(&m);
343
Damien Miller06ebedf2003-02-24 12:03:38 +1100344 /* fake forced command */
345 auth_clear_options();
346 have_forced = buffer_get_int(&m);
347 forced_command = have_forced ? xstrdup("true") : NULL;
348
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000349 /* Send potential debug messages */
350 mm_send_debug(&m);
351
352 buffer_free(&m);
353
354 return (allowed);
355}
356
357/*
358 * This key verify needs to send the key type along, because the
359 * privileged parent makes the decision if the key is allowed
360 * for authentication.
361 */
362
363int
364mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
365{
366 Buffer m;
367 u_char *blob;
368 u_int len;
369 int verified = 0;
370
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000371 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372
373 /* Convert the key to a blob and the pass it over */
374 if (!key_to_blob(key, &blob, &len))
375 return (0);
376
377 buffer_init(&m);
378 buffer_put_string(&m, blob, len);
379 buffer_put_string(&m, sig, siglen);
380 buffer_put_string(&m, data, datalen);
381 xfree(blob);
382
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000383 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000384
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000385 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000386 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000387
388 verified = buffer_get_int(&m);
389
390 buffer_free(&m);
391
392 return (verified);
393}
394
395/* Export key state after authentication */
396Newkeys *
397mm_newkeys_from_blob(u_char *blob, int blen)
398{
399 Buffer b;
400 u_int len;
401 Newkeys *newkey = NULL;
402 Enc *enc;
403 Mac *mac;
404 Comp *comp;
405
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000406 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000407#ifdef DEBUG_PK
408 dump_base64(stderr, blob, blen);
409#endif
410 buffer_init(&b);
411 buffer_append(&b, blob, blen);
412
413 newkey = xmalloc(sizeof(*newkey));
414 enc = &newkey->enc;
415 mac = &newkey->mac;
416 comp = &newkey->comp;
417
418 /* Enc structure */
419 enc->name = buffer_get_string(&b, NULL);
420 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
421 enc->enabled = buffer_get_int(&b);
422 enc->block_size = buffer_get_int(&b);
423 enc->key = buffer_get_string(&b, &enc->key_len);
424 enc->iv = buffer_get_string(&b, &len);
425 if (len != enc->block_size)
Ben Lindstrom08512492002-06-27 00:23:02 +0000426 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000427 enc->block_size, len);
428
429 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000430 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000431 enc->name, enc->cipher);
432
433 /* Mac structure */
434 mac->name = buffer_get_string(&b, NULL);
435 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000436 fatal("%s: can not init mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000437 mac->enabled = buffer_get_int(&b);
438 mac->key = buffer_get_string(&b, &len);
439 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000440 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000441 mac->key_len);
442 mac->key_len = len;
443
444 /* Comp structure */
445 comp->type = buffer_get_int(&b);
446 comp->enabled = buffer_get_int(&b);
447 comp->name = buffer_get_string(&b, NULL);
448
449 len = buffer_len(&b);
450 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000451 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000452 buffer_free(&b);
453 return (newkey);
454}
455
456int
457mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
458{
459 Buffer b;
460 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000461 Enc *enc;
462 Mac *mac;
463 Comp *comp;
464 Newkeys *newkey = newkeys[mode];
465
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000466 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000467
468 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000469 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000470 return 0;
471 }
472 enc = &newkey->enc;
473 mac = &newkey->mac;
474 comp = &newkey->comp;
475
476 buffer_init(&b);
477 /* Enc structure */
478 buffer_put_cstring(&b, enc->name);
479 /* The cipher struct is constant and shared, you export pointer */
480 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
481 buffer_put_int(&b, enc->enabled);
482 buffer_put_int(&b, enc->block_size);
483 buffer_put_string(&b, enc->key, enc->key_len);
484 packet_get_keyiv(mode, enc->iv, enc->block_size);
485 buffer_put_string(&b, enc->iv, enc->block_size);
486
487 /* Mac structure */
488 buffer_put_cstring(&b, mac->name);
489 buffer_put_int(&b, mac->enabled);
490 buffer_put_string(&b, mac->key, mac->key_len);
491
492 /* Comp structure */
493 buffer_put_int(&b, comp->type);
494 buffer_put_int(&b, comp->enabled);
495 buffer_put_cstring(&b, comp->name);
496
497 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000498 if (lenp != NULL)
499 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000500 if (blobp != NULL) {
501 *blobp = xmalloc(len);
502 memcpy(*blobp, buffer_ptr(&b), len);
503 }
504 memset(buffer_ptr(&b), 0, len);
505 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000506 return len;
507}
508
509static void
510mm_send_kex(Buffer *m, Kex *kex)
511{
512 buffer_put_string(m, kex->session_id, kex->session_id_len);
513 buffer_put_int(m, kex->we_need);
514 buffer_put_int(m, kex->hostkey_type);
515 buffer_put_int(m, kex->kex_type);
516 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
517 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
518 buffer_put_int(m, kex->flags);
519 buffer_put_cstring(m, kex->client_version_string);
520 buffer_put_cstring(m, kex->server_version_string);
521}
522
523void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000524mm_send_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000525{
526 Buffer m;
527 u_char *blob, *p;
528 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000529 u_int32_t seqnr, packets;
530 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000531
532 buffer_init(&m);
533
534 if (!compat20) {
535 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000536 u_char *key;
537 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000538
539 buffer_put_int(&m, packet_get_protocol_flags());
540
541 buffer_put_int(&m, packet_get_ssh1_cipher());
542
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000543 debug3("%s: Sending ssh1 KEY+IV", __func__);
544 keylen = packet_get_encryption_key(NULL);
545 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
546 keylen = packet_get_encryption_key(key);
547 buffer_put_string(&m, key, keylen);
548 memset(key, 0, keylen);
549 xfree(key);
550
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000551 ivlen = packet_get_keyiv_len(MODE_OUT);
552 packet_get_keyiv(MODE_OUT, iv, ivlen);
553 buffer_put_string(&m, iv, ivlen);
554 ivlen = packet_get_keyiv_len(MODE_OUT);
555 packet_get_keyiv(MODE_IN, iv, ivlen);
556 buffer_put_string(&m, iv, ivlen);
557 goto skip;
558 } else {
559 /* Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000560 mm_send_kex(&m, *pmonitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561 }
562
563 debug3("%s: Sending new keys: %p %p",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000564 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000565
566 /* Keys from Kex */
567 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000568 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000569
570 buffer_put_string(&m, blob, bloblen);
571 xfree(blob);
572
573 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000574 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000575
576 buffer_put_string(&m, blob, bloblen);
577 xfree(blob);
578
Damien Millera5539d22003-04-09 20:50:06 +1000579 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
580 buffer_put_int(&m, seqnr);
581 buffer_put_int64(&m, blocks);
582 buffer_put_int(&m, packets);
Damien Millerb1ecd9c2003-04-09 20:51:24 +1000583 packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
Damien Millera5539d22003-04-09 20:50:06 +1000584 buffer_put_int(&m, seqnr);
585 buffer_put_int64(&m, blocks);
586 buffer_put_int(&m, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000588 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000589 skip:
590 /* More key context */
591 plen = packet_get_keycontext(MODE_OUT, NULL);
592 p = xmalloc(plen+1);
593 packet_get_keycontext(MODE_OUT, p);
594 buffer_put_string(&m, p, plen);
595 xfree(p);
596
597 plen = packet_get_keycontext(MODE_IN, NULL);
598 p = xmalloc(plen+1);
599 packet_get_keycontext(MODE_IN, p);
600 buffer_put_string(&m, p, plen);
601 xfree(p);
602
603 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000604 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
606 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
607
608 /* Network I/O buffers */
609 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
610 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
611
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000612 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000613 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000614
615 buffer_free(&m);
616}
617
618int
619mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
620{
621 Buffer m;
Damien Millera10f5612002-09-12 09:49:15 +1000622 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000623 int success = 0;
624
625 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000626 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000627
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000628 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000629 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000630
631 success = buffer_get_int(&m);
632 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000633 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000634 buffer_free(&m);
635 return (0);
636 }
637 p = buffer_get_string(&m, NULL);
638 buffer_free(&m);
639
640 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
641 xfree(p);
642
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000643 *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
644 *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000645
646 /* Success */
647 return (1);
648}
649
650void
651mm_session_pty_cleanup2(void *session)
652{
653 Session *s = session;
654 Buffer m;
655
656 if (s->ttyfd == -1)
657 return;
658 buffer_init(&m);
659 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000660 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000661 buffer_free(&m);
662
663 /* closed dup'ed master */
664 if (close(s->ptymaster) < 0)
665 error("close(s->ptymaster): %s", strerror(errno));
666
667 /* unlink pty from session */
668 s->ttyfd = -1;
669}
670
Damien Miller79418552002-04-23 20:28:48 +1000671#ifdef USE_PAM
672void
673mm_start_pam(char *user)
674{
675 Buffer m;
676
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000677 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000678 if (!options.use_pam)
679 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000680
681 buffer_init(&m);
682 buffer_put_cstring(&m, user);
683
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000684 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000685
686 buffer_free(&m);
687}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000688
Damien Miller1f499fd2003-08-25 13:08:49 +1000689u_int
690mm_do_pam_account(void)
691{
692 Buffer m;
693 u_int ret;
694
695 debug3("%s entering", __func__);
696 if (!options.use_pam)
697 fatal("UsePAM=no, but ended up in %s anyway", __func__);
698
699 buffer_init(&m);
700 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
701
702 mm_request_receive_expect(pmonitor->m_recvfd,
703 MONITOR_ANS_PAM_ACCOUNT, &m);
704 ret = buffer_get_int(&m);
705
706 buffer_free(&m);
707
708 debug3("%s returning %d", __func__, ret);
709
710 return (ret);
711}
712
Damien Miller4f9f42a2003-05-10 19:28:02 +1000713void *
714mm_sshpam_init_ctx(Authctxt *authctxt)
715{
716 Buffer m;
717 int success;
718
719 debug3("%s", __func__);
720 buffer_init(&m);
721 buffer_put_cstring(&m, authctxt->user);
722 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
723 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
724 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
725 success = buffer_get_int(&m);
726 if (success == 0) {
727 debug3("%s: pam_init_ctx failed", __func__);
728 buffer_free(&m);
729 return (NULL);
730 }
731 buffer_free(&m);
732 return (authctxt);
733}
734
735int
736mm_sshpam_query(void *ctx, char **name, char **info,
737 u_int *num, char ***prompts, u_int **echo_on)
738{
739 Buffer m;
740 int i, ret;
741
742 debug3("%s", __func__);
743 buffer_init(&m);
744 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
745 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
746 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
747 ret = buffer_get_int(&m);
748 debug3("%s: pam_query returned %d", __func__, ret);
749 *name = buffer_get_string(&m, NULL);
750 *info = buffer_get_string(&m, NULL);
751 *num = buffer_get_int(&m);
752 *prompts = xmalloc((*num + 1) * sizeof(char *));
753 *echo_on = xmalloc((*num + 1) * sizeof(u_int));
754 for (i = 0; i < *num; ++i) {
755 (*prompts)[i] = buffer_get_string(&m, NULL);
756 (*echo_on)[i] = buffer_get_int(&m);
757 }
758 buffer_free(&m);
759 return (ret);
760}
761
762int
763mm_sshpam_respond(void *ctx, u_int num, char **resp)
764{
765 Buffer m;
766 int i, ret;
767
768 debug3("%s", __func__);
769 buffer_init(&m);
770 buffer_put_int(&m, num);
771 for (i = 0; i < num; ++i)
772 buffer_put_cstring(&m, resp[i]);
773 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
774 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
775 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
776 ret = buffer_get_int(&m);
777 debug3("%s: pam_respond returned %d", __func__, ret);
778 buffer_free(&m);
779 return (ret);
780}
781
782void
783mm_sshpam_free_ctx(void *ctxtp)
784{
785 Buffer m;
786
787 debug3("%s", __func__);
788 buffer_init(&m);
789 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
790 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
791 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
792 buffer_free(&m);
793}
Damien Miller79418552002-04-23 20:28:48 +1000794#endif /* USE_PAM */
795
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000796/* Request process termination */
797
798void
799mm_terminate(void)
800{
801 Buffer m;
802
803 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000804 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000805 buffer_free(&m);
806}
807
808int
809mm_ssh1_session_key(BIGNUM *num)
810{
811 int rsafail;
812 Buffer m;
813
814 buffer_init(&m);
815 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000816 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000817
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000818 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000819
820 rsafail = buffer_get_int(&m);
821 buffer_get_bignum2(&m, num);
822
823 buffer_free(&m);
824
825 return (rsafail);
826}
827
828static void
829mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
830 char ***prompts, u_int **echo_on)
831{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000832 *name = xstrdup("");
833 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000834 *numprompts = 1;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000835 *prompts = xmalloc(*numprompts * sizeof(char *));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000836 *echo_on = xmalloc(*numprompts * sizeof(u_int));
837 (*echo_on)[0] = 0;
838}
839
840int
841mm_bsdauth_query(void *ctx, char **name, char **infotxt,
842 u_int *numprompts, char ***prompts, u_int **echo_on)
843{
844 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100845 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000846 char *challenge;
847
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000848 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000849
850 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000851 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000852
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000853 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000854 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100855 success = buffer_get_int(&m);
856 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000857 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000858 buffer_free(&m);
859 return (-1);
860 }
861
862 /* Get the challenge, and format the response */
863 challenge = buffer_get_string(&m, NULL);
864 buffer_free(&m);
865
866 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
867 (*prompts)[0] = challenge;
868
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000869 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000870
871 return (0);
872}
873
874int
875mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
876{
877 Buffer m;
878 int authok;
879
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000880 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000881 if (numresponses != 1)
882 return (-1);
883
884 buffer_init(&m);
885 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000886 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000887
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000888 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000889 MONITOR_ANS_BSDAUTHRESPOND, &m);
890
891 authok = buffer_get_int(&m);
892 buffer_free(&m);
893
894 return ((authok == 0) ? -1 : 0);
895}
896
897int
898mm_skey_query(void *ctx, char **name, char **infotxt,
899 u_int *numprompts, char ***prompts, u_int **echo_on)
900{
901 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100902 int len;
903 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000904 char *p, *challenge;
905
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000906 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000907
908 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000909 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000910
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000911 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000912 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100913 success = buffer_get_int(&m);
914 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000915 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000916 buffer_free(&m);
917 return (-1);
918 }
919
920 /* Get the challenge, and format the response */
921 challenge = buffer_get_string(&m, NULL);
922 buffer_free(&m);
923
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000924 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000925
926 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
927
928 len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
929 p = xmalloc(len);
930 strlcpy(p, challenge, len);
931 strlcat(p, SKEY_PROMPT, len);
932 (*prompts)[0] = p;
933 xfree(challenge);
934
935 return (0);
936}
937
938int
939mm_skey_respond(void *ctx, u_int numresponses, char **responses)
940{
941 Buffer m;
942 int authok;
943
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000944 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000945 if (numresponses != 1)
946 return (-1);
947
948 buffer_init(&m);
949 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000950 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000951
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000952 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000953 MONITOR_ANS_SKEYRESPOND, &m);
954
955 authok = buffer_get_int(&m);
956 buffer_free(&m);
957
958 return ((authok == 0) ? -1 : 0);
959}
960
961void
962mm_ssh1_session_id(u_char session_id[16])
963{
964 Buffer m;
965 int i;
966
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000967 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000968
969 buffer_init(&m);
970 for (i = 0; i < 16; i++)
971 buffer_put_char(&m, session_id[i]);
972
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000973 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000974 buffer_free(&m);
975}
976
977int
978mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
979{
980 Buffer m;
981 Key *key;
982 u_char *blob;
983 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +1100984 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000985
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000986 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000987
988 buffer_init(&m);
989 buffer_put_bignum2(&m, client_n);
990
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000991 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
992 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000993
994 allowed = buffer_get_int(&m);
995
Damien Miller06ebedf2003-02-24 12:03:38 +1100996 /* fake forced command */
997 auth_clear_options();
998 have_forced = buffer_get_int(&m);
999 forced_command = have_forced ? xstrdup("true") : NULL;
1000
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001001 if (allowed && rkey != NULL) {
1002 blob = buffer_get_string(&m, &blen);
1003 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001004 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005 *rkey = key;
1006 xfree(blob);
1007 }
1008 mm_send_debug(&m);
1009 buffer_free(&m);
1010
1011 return (allowed);
1012}
1013
1014BIGNUM *
1015mm_auth_rsa_generate_challenge(Key *key)
1016{
1017 Buffer m;
1018 BIGNUM *challenge;
1019 u_char *blob;
1020 u_int blen;
1021
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001022 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001023
1024 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001025 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001026
1027 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1028 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001029 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001030 key->type = KEY_RSA1;
1031
1032 buffer_init(&m);
1033 buffer_put_string(&m, blob, blen);
1034 xfree(blob);
1035
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001036 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1037 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001038
1039 buffer_get_bignum2(&m, challenge);
1040 buffer_free(&m);
1041
1042 return (challenge);
1043}
1044
1045int
1046mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1047{
1048 Buffer m;
1049 u_char *blob;
1050 u_int blen;
1051 int success = 0;
1052
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001053 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001054
1055 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1056 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001057 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001058 key->type = KEY_RSA1;
1059
1060 buffer_init(&m);
1061 buffer_put_string(&m, blob, blen);
1062 buffer_put_string(&m, response, 16);
1063 xfree(blob);
1064
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001065 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1066 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001067
1068 success = buffer_get_int(&m);
1069 buffer_free(&m);
1070
1071 return (success);
1072}
Damien Miller25162f22002-09-12 09:47:29 +10001073
1074#ifdef KRB5
1075int
1076mm_auth_krb5(void *ctx, void *argp, char **userp, void *resp)
1077{
1078 krb5_data *tkt, *reply;
1079 Buffer m;
1080 int success;
1081
1082 debug3("%s entering", __func__);
1083 tkt = (krb5_data *) argp;
1084 reply = (krb5_data *) resp;
1085
1086 buffer_init(&m);
1087 buffer_put_string(&m, tkt->data, tkt->length);
1088
1089 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KRB5, &m);
1090 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KRB5, &m);
1091
1092 success = buffer_get_int(&m);
1093 if (success) {
1094 u_int len;
1095
1096 *userp = buffer_get_string(&m, NULL);
1097 reply->data = buffer_get_string(&m, &len);
1098 reply->length = len;
1099 } else {
1100 memset(reply, 0, sizeof(*reply));
1101 *userp = NULL;
1102 }
1103
1104 buffer_free(&m);
1105 return (success);
1106}
Darren Tucker0efd1552003-08-26 11:49:55 +10001107#endif /* KRB5 */
1108
1109#ifdef GSSAPI
1110OM_uint32
1111mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
1112{
1113 Buffer m;
1114 OM_uint32 major;
1115
1116 /* Client doesn't get to see the context */
1117 *ctx = NULL;
1118
1119 buffer_init(&m);
1120 buffer_put_string(&m, oid->elements, oid->length);
1121
1122 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1123 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1124
1125 major = buffer_get_int(&m);
1126
1127 buffer_free(&m);
1128 return (major);
1129}
1130
1131OM_uint32
1132mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1133 gss_buffer_desc *out, OM_uint32 *flags)
1134{
1135 Buffer m;
1136 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001137 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001138
1139 buffer_init(&m);
1140 buffer_put_string(&m, in->value, in->length);
1141
1142 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1143 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1144
1145 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001146 out->value = buffer_get_string(&m, &len);
1147 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001148 if (flags)
1149 *flags = buffer_get_int(&m);
1150
1151 buffer_free(&m);
1152
1153 return (major);
1154}
1155
1156int
1157mm_ssh_gssapi_userok(char *user)
1158{
1159 Buffer m;
1160 int authenticated = 0;
1161
1162 buffer_init(&m);
1163
1164 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1165 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1166 &m);
1167
1168 authenticated = buffer_get_int(&m);
1169
1170 buffer_free(&m);
1171 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1172 return (authenticated);
1173}
1174#endif /* GSSAPI */