blob: 0f5ecaaeea19e6ea35cb65250431db882a97e774 [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 Tuckerb09b6772004-06-22 15:06:46 +100028RCSID("$OpenBSD: monitor_wrap.c,v 1.37 2004/06/22 05:05:45 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;
Damien Miller4e448a32003-05-14 15:11:48 +100073extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000074
Darren Tucker3e33cec2003-10-02 16:12:36 +100075int
76mm_is_monitor(void)
77{
78 /*
79 * m_pid is only set in the privileged part, and
80 * points to the unprivileged child.
81 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +110082 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +100083}
84
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000085void
Darren Tucker3f9fdc72004-06-22 12:56:01 +100086mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000087{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000088 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +000089 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000090
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000091 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000092
93 PUT_32BIT(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +000094 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +100095 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000096 fatal("%s: write", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +100097 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000098 fatal("%s: write", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000099}
100
101void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000102mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000103{
104 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105 u_int msg_len;
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +0000106 ssize_t res;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000108 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000109
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000110 res = atomicio(read, sock, buf, sizeof(buf));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000111 if (res != sizeof(buf)) {
112 if (res == 0)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000113 cleanup_exit(255);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000114 fatal("%s: read: %ld", __func__, (long)res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000115 }
116 msg_len = GET_32BIT(buf);
117 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000118 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000119 buffer_clear(m);
120 buffer_append_space(m, msg_len);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000121 res = atomicio(read, sock, buffer_ptr(m), msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000122 if (res != msg_len)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000123 fatal("%s: read: %ld != msg_len", __func__, (long)res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000124}
125
126void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000127mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000128{
129 u_char rtype;
130
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000131 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000132
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000133 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000134 rtype = buffer_get_char(m);
135 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000136 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000137 rtype, type);
138}
139
140DH *
141mm_choose_dh(int min, int nbits, int max)
142{
143 BIGNUM *p, *g;
144 int success = 0;
145 Buffer m;
146
147 buffer_init(&m);
148 buffer_put_int(&m, min);
149 buffer_put_int(&m, nbits);
150 buffer_put_int(&m, max);
151
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000152 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000153
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000154 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000155 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000156
157 success = buffer_get_char(&m);
158 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000159 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000160
161 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000162 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000163 if ((g = 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 buffer_get_bignum2(&m, p);
166 buffer_get_bignum2(&m, g);
167
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000168 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000169 buffer_free(&m);
170
171 return (dh_new_group(g, p));
172}
173
174int
175mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
176{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000177 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000178 Buffer m;
179
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000180 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000181
182 buffer_init(&m);
183 buffer_put_int(&m, kex->host_key_index(key));
184 buffer_put_string(&m, data, datalen);
185
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000186 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000187
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000188 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000189 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000190 *sigp = buffer_get_string(&m, lenp);
191 buffer_free(&m);
192
193 return (0);
194}
195
196struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000197mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000198{
199 Buffer m;
200 struct passwd *pw;
201 u_int pwlen;
202
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000203 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000204
205 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000206 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000207
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000208 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000209
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000210 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000211 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000212
213 if (buffer_get_char(&m) == 0) {
214 buffer_free(&m);
215 return (NULL);
216 }
217 pw = buffer_get_string(&m, &pwlen);
218 if (pwlen != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000219 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000220 pw->pw_name = buffer_get_string(&m, NULL);
221 pw->pw_passwd = buffer_get_string(&m, NULL);
222 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000223#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000224 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000225#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000226 pw->pw_dir = buffer_get_string(&m, NULL);
227 pw->pw_shell = buffer_get_string(&m, NULL);
228 buffer_free(&m);
229
230 return (pw);
231}
232
Darren Tucker7eb3de02003-10-15 15:56:58 +1000233char *
234mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000235{
236 Buffer m;
237 char *banner;
238
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000239 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000240
241 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000242 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000243 buffer_clear(&m);
244
Darren Tucker7eb3de02003-10-15 15:56:58 +1000245 mm_request_receive_expect(pmonitor->m_recvfd,
246 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000247 banner = buffer_get_string(&m, NULL);
248 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000249
Darren Tucker7eb3de02003-10-15 15:56:58 +1000250 /* treat empty banner as missing banner */
251 if (strlen(banner) == 0) {
252 xfree(banner);
253 banner = NULL;
254 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000255 return (banner);
256}
257
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000258/* Inform the privileged process about service and style */
259
260void
261mm_inform_authserv(char *service, char *style)
262{
263 Buffer m;
264
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000265 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000266
267 buffer_init(&m);
268 buffer_put_cstring(&m, service);
269 buffer_put_cstring(&m, style ? style : "");
270
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000271 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000272
273 buffer_free(&m);
274}
275
276/* Do the password authentication */
277int
278mm_auth_password(Authctxt *authctxt, char *password)
279{
280 Buffer m;
281 int authenticated = 0;
282
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000283 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000284
285 buffer_init(&m);
286 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000287 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000288
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000289 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000290 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000291
292 authenticated = buffer_get_int(&m);
293
294 buffer_free(&m);
295
296 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000297 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000298 return (authenticated);
299}
300
301int
302mm_user_key_allowed(struct passwd *pw, Key *key)
303{
304 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
305}
306
307int
308mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
309 Key *key)
310{
311 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
312}
313
314int
315mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
316 char *host, Key *key)
317{
318 int ret;
319
320 key->type = KEY_RSA; /* XXX hack for key_to_blob */
321 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
322 key->type = KEY_RSA1;
323 return (ret);
324}
325
326static void
327mm_send_debug(Buffer *m)
328{
329 char *msg;
330
331 while (buffer_len(m)) {
332 msg = buffer_get_string(m, NULL);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000333 debug3("%s: Sending debug: %s", __func__, msg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000334 packet_send_debug("%s", msg);
335 xfree(msg);
336 }
337}
338
339int
340mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
341{
342 Buffer m;
343 u_char *blob;
344 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100345 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000346
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000347 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348
349 /* Convert the key to a blob and the pass it over */
350 if (!key_to_blob(key, &blob, &len))
351 return (0);
352
353 buffer_init(&m);
354 buffer_put_int(&m, type);
355 buffer_put_cstring(&m, user ? user : "");
356 buffer_put_cstring(&m, host ? host : "");
357 buffer_put_string(&m, blob, len);
358 xfree(blob);
359
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000360 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000361
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000362 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000363 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000364
365 allowed = buffer_get_int(&m);
366
Damien Miller06ebedf2003-02-24 12:03:38 +1100367 /* fake forced command */
368 auth_clear_options();
369 have_forced = buffer_get_int(&m);
370 forced_command = have_forced ? xstrdup("true") : NULL;
371
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372 /* Send potential debug messages */
373 mm_send_debug(&m);
374
375 buffer_free(&m);
376
377 return (allowed);
378}
379
380/*
381 * This key verify needs to send the key type along, because the
382 * privileged parent makes the decision if the key is allowed
383 * for authentication.
384 */
385
386int
387mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
388{
389 Buffer m;
390 u_char *blob;
391 u_int len;
392 int verified = 0;
393
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000394 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000395
396 /* Convert the key to a blob and the pass it over */
397 if (!key_to_blob(key, &blob, &len))
398 return (0);
399
400 buffer_init(&m);
401 buffer_put_string(&m, blob, len);
402 buffer_put_string(&m, sig, siglen);
403 buffer_put_string(&m, data, datalen);
404 xfree(blob);
405
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000406 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000407
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000408 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000409 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000410
411 verified = buffer_get_int(&m);
412
413 buffer_free(&m);
414
415 return (verified);
416}
417
418/* Export key state after authentication */
419Newkeys *
420mm_newkeys_from_blob(u_char *blob, int blen)
421{
422 Buffer b;
423 u_int len;
424 Newkeys *newkey = NULL;
425 Enc *enc;
426 Mac *mac;
427 Comp *comp;
428
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000429 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000430#ifdef DEBUG_PK
431 dump_base64(stderr, blob, blen);
432#endif
433 buffer_init(&b);
434 buffer_append(&b, blob, blen);
435
436 newkey = xmalloc(sizeof(*newkey));
437 enc = &newkey->enc;
438 mac = &newkey->mac;
439 comp = &newkey->comp;
440
441 /* Enc structure */
442 enc->name = buffer_get_string(&b, NULL);
443 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
444 enc->enabled = buffer_get_int(&b);
445 enc->block_size = buffer_get_int(&b);
446 enc->key = buffer_get_string(&b, &enc->key_len);
447 enc->iv = buffer_get_string(&b, &len);
448 if (len != enc->block_size)
Ben Lindstrom08512492002-06-27 00:23:02 +0000449 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000450 enc->block_size, len);
451
452 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000453 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000454 enc->name, enc->cipher);
455
456 /* Mac structure */
457 mac->name = buffer_get_string(&b, NULL);
458 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000459 fatal("%s: can not init mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000460 mac->enabled = buffer_get_int(&b);
461 mac->key = buffer_get_string(&b, &len);
462 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000463 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000464 mac->key_len);
465 mac->key_len = len;
466
467 /* Comp structure */
468 comp->type = buffer_get_int(&b);
469 comp->enabled = buffer_get_int(&b);
470 comp->name = buffer_get_string(&b, NULL);
471
472 len = buffer_len(&b);
473 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000474 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000475 buffer_free(&b);
476 return (newkey);
477}
478
479int
480mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
481{
482 Buffer b;
483 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000484 Enc *enc;
485 Mac *mac;
486 Comp *comp;
487 Newkeys *newkey = newkeys[mode];
488
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000489 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000490
491 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000492 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000493 return 0;
494 }
495 enc = &newkey->enc;
496 mac = &newkey->mac;
497 comp = &newkey->comp;
498
499 buffer_init(&b);
500 /* Enc structure */
501 buffer_put_cstring(&b, enc->name);
502 /* The cipher struct is constant and shared, you export pointer */
503 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
504 buffer_put_int(&b, enc->enabled);
505 buffer_put_int(&b, enc->block_size);
506 buffer_put_string(&b, enc->key, enc->key_len);
507 packet_get_keyiv(mode, enc->iv, enc->block_size);
508 buffer_put_string(&b, enc->iv, enc->block_size);
509
510 /* Mac structure */
511 buffer_put_cstring(&b, mac->name);
512 buffer_put_int(&b, mac->enabled);
513 buffer_put_string(&b, mac->key, mac->key_len);
514
515 /* Comp structure */
516 buffer_put_int(&b, comp->type);
517 buffer_put_int(&b, comp->enabled);
518 buffer_put_cstring(&b, comp->name);
519
520 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000521 if (lenp != NULL)
522 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000523 if (blobp != NULL) {
524 *blobp = xmalloc(len);
525 memcpy(*blobp, buffer_ptr(&b), len);
526 }
527 memset(buffer_ptr(&b), 0, len);
528 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000529 return len;
530}
531
532static void
533mm_send_kex(Buffer *m, Kex *kex)
534{
535 buffer_put_string(m, kex->session_id, kex->session_id_len);
536 buffer_put_int(m, kex->we_need);
537 buffer_put_int(m, kex->hostkey_type);
538 buffer_put_int(m, kex->kex_type);
539 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
540 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
541 buffer_put_int(m, kex->flags);
542 buffer_put_cstring(m, kex->client_version_string);
543 buffer_put_cstring(m, kex->server_version_string);
544}
545
546void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000547mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000548{
549 Buffer m;
550 u_char *blob, *p;
551 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000552 u_int32_t seqnr, packets;
553 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000554
555 buffer_init(&m);
556
557 if (!compat20) {
558 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000559 u_char *key;
560 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561
562 buffer_put_int(&m, packet_get_protocol_flags());
563
564 buffer_put_int(&m, packet_get_ssh1_cipher());
565
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000566 debug3("%s: Sending ssh1 KEY+IV", __func__);
567 keylen = packet_get_encryption_key(NULL);
568 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
569 keylen = packet_get_encryption_key(key);
570 buffer_put_string(&m, key, keylen);
571 memset(key, 0, keylen);
572 xfree(key);
573
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000574 ivlen = packet_get_keyiv_len(MODE_OUT);
575 packet_get_keyiv(MODE_OUT, iv, ivlen);
576 buffer_put_string(&m, iv, ivlen);
577 ivlen = packet_get_keyiv_len(MODE_OUT);
578 packet_get_keyiv(MODE_IN, iv, ivlen);
579 buffer_put_string(&m, iv, ivlen);
580 goto skip;
581 } else {
582 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000583 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000584 }
585
586 debug3("%s: Sending new keys: %p %p",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000587 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000588
589 /* Keys from Kex */
590 if (!mm_newkeys_to_blob(MODE_OUT, &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
596 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000597 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598
599 buffer_put_string(&m, blob, bloblen);
600 xfree(blob);
601
Damien Millera5539d22003-04-09 20:50:06 +1000602 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
603 buffer_put_int(&m, seqnr);
604 buffer_put_int64(&m, blocks);
605 buffer_put_int(&m, packets);
Damien Millerb1ecd9c2003-04-09 20:51:24 +1000606 packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
Damien Millera5539d22003-04-09 20:50:06 +1000607 buffer_put_int(&m, seqnr);
608 buffer_put_int64(&m, blocks);
609 buffer_put_int(&m, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000610
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000611 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000612 skip:
613 /* More key context */
614 plen = packet_get_keycontext(MODE_OUT, NULL);
615 p = xmalloc(plen+1);
616 packet_get_keycontext(MODE_OUT, p);
617 buffer_put_string(&m, p, plen);
618 xfree(p);
619
620 plen = packet_get_keycontext(MODE_IN, NULL);
621 p = xmalloc(plen+1);
622 packet_get_keycontext(MODE_IN, p);
623 buffer_put_string(&m, p, plen);
624 xfree(p);
625
626 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000627 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000628 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
629 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
630
631 /* Network I/O buffers */
632 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
633 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
634
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000635 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000636 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000637
638 buffer_free(&m);
639}
640
641int
642mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
643{
644 Buffer m;
Damien Millera10f5612002-09-12 09:49:15 +1000645 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000646 int success = 0;
647
648 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000649 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000650
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000651 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000652 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000653
654 success = buffer_get_int(&m);
655 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000656 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000657 buffer_free(&m);
658 return (0);
659 }
660 p = buffer_get_string(&m, NULL);
661 buffer_free(&m);
662
663 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
664 xfree(p);
665
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000666 *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
667 *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000668
669 /* Success */
670 return (1);
671}
672
673void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000674mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000675{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000676 Buffer m;
677
678 if (s->ttyfd == -1)
679 return;
680 buffer_init(&m);
681 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000682 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000683 buffer_free(&m);
684
685 /* closed dup'ed master */
686 if (close(s->ptymaster) < 0)
687 error("close(s->ptymaster): %s", strerror(errno));
688
689 /* unlink pty from session */
690 s->ttyfd = -1;
691}
692
Damien Miller79418552002-04-23 20:28:48 +1000693#ifdef USE_PAM
694void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100695mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000696{
697 Buffer m;
698
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000699 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000700 if (!options.use_pam)
701 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000702
703 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000704 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000705
706 buffer_free(&m);
707}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000708
Damien Miller1f499fd2003-08-25 13:08:49 +1000709u_int
710mm_do_pam_account(void)
711{
712 Buffer m;
713 u_int ret;
714
715 debug3("%s entering", __func__);
716 if (!options.use_pam)
717 fatal("UsePAM=no, but ended up in %s anyway", __func__);
718
719 buffer_init(&m);
720 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
721
Damien Millera8e06ce2003-11-21 23:48:55 +1100722 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000723 MONITOR_ANS_PAM_ACCOUNT, &m);
724 ret = buffer_get_int(&m);
725
726 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100727
Damien Miller1f499fd2003-08-25 13:08:49 +1000728 debug3("%s returning %d", __func__, ret);
729
730 return (ret);
731}
732
Damien Miller4f9f42a2003-05-10 19:28:02 +1000733void *
734mm_sshpam_init_ctx(Authctxt *authctxt)
735{
736 Buffer m;
737 int success;
738
739 debug3("%s", __func__);
740 buffer_init(&m);
741 buffer_put_cstring(&m, authctxt->user);
742 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
743 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
744 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
745 success = buffer_get_int(&m);
746 if (success == 0) {
747 debug3("%s: pam_init_ctx failed", __func__);
748 buffer_free(&m);
749 return (NULL);
750 }
751 buffer_free(&m);
752 return (authctxt);
753}
754
755int
756mm_sshpam_query(void *ctx, char **name, char **info,
757 u_int *num, char ***prompts, u_int **echo_on)
758{
759 Buffer m;
760 int i, ret;
761
762 debug3("%s", __func__);
763 buffer_init(&m);
764 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
765 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
766 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
767 ret = buffer_get_int(&m);
768 debug3("%s: pam_query returned %d", __func__, ret);
769 *name = buffer_get_string(&m, NULL);
770 *info = buffer_get_string(&m, NULL);
771 *num = buffer_get_int(&m);
772 *prompts = xmalloc((*num + 1) * sizeof(char *));
773 *echo_on = xmalloc((*num + 1) * sizeof(u_int));
774 for (i = 0; i < *num; ++i) {
775 (*prompts)[i] = buffer_get_string(&m, NULL);
776 (*echo_on)[i] = buffer_get_int(&m);
777 }
778 buffer_free(&m);
779 return (ret);
780}
781
782int
783mm_sshpam_respond(void *ctx, u_int num, char **resp)
784{
785 Buffer m;
786 int i, ret;
787
788 debug3("%s", __func__);
789 buffer_init(&m);
790 buffer_put_int(&m, num);
791 for (i = 0; i < num; ++i)
792 buffer_put_cstring(&m, resp[i]);
793 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
794 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
795 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
796 ret = buffer_get_int(&m);
797 debug3("%s: pam_respond returned %d", __func__, ret);
798 buffer_free(&m);
799 return (ret);
800}
801
802void
803mm_sshpam_free_ctx(void *ctxtp)
804{
805 Buffer m;
806
807 debug3("%s", __func__);
808 buffer_init(&m);
809 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
810 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
811 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
812 buffer_free(&m);
813}
Damien Miller79418552002-04-23 20:28:48 +1000814#endif /* USE_PAM */
815
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000816/* Request process termination */
817
818void
819mm_terminate(void)
820{
821 Buffer m;
822
823 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000824 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000825 buffer_free(&m);
826}
827
828int
829mm_ssh1_session_key(BIGNUM *num)
830{
831 int rsafail;
832 Buffer m;
833
834 buffer_init(&m);
835 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000836 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000837
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000838 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000839
840 rsafail = buffer_get_int(&m);
841 buffer_get_bignum2(&m, num);
842
843 buffer_free(&m);
844
845 return (rsafail);
846}
847
848static void
849mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
850 char ***prompts, u_int **echo_on)
851{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000852 *name = xstrdup("");
853 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000854 *numprompts = 1;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000855 *prompts = xmalloc(*numprompts * sizeof(char *));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000856 *echo_on = xmalloc(*numprompts * sizeof(u_int));
857 (*echo_on)[0] = 0;
858}
859
860int
861mm_bsdauth_query(void *ctx, char **name, char **infotxt,
862 u_int *numprompts, char ***prompts, u_int **echo_on)
863{
864 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100865 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000866 char *challenge;
867
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000868 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000869
870 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000871 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000872
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000873 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000874 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100875 success = buffer_get_int(&m);
876 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000877 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000878 buffer_free(&m);
879 return (-1);
880 }
881
882 /* Get the challenge, and format the response */
883 challenge = buffer_get_string(&m, NULL);
884 buffer_free(&m);
885
886 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
887 (*prompts)[0] = challenge;
888
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000889 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000890
891 return (0);
892}
893
894int
895mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
896{
897 Buffer m;
898 int authok;
899
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000900 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000901 if (numresponses != 1)
902 return (-1);
903
904 buffer_init(&m);
905 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000906 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000907
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000908 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909 MONITOR_ANS_BSDAUTHRESPOND, &m);
910
911 authok = buffer_get_int(&m);
912 buffer_free(&m);
913
914 return ((authok == 0) ? -1 : 0);
915}
916
917int
918mm_skey_query(void *ctx, char **name, char **infotxt,
919 u_int *numprompts, char ***prompts, u_int **echo_on)
920{
921 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100922 int len;
923 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000924 char *p, *challenge;
925
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000926 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000927
928 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000929 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000930
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000931 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100933 success = buffer_get_int(&m);
934 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000935 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000936 buffer_free(&m);
937 return (-1);
938 }
939
940 /* Get the challenge, and format the response */
941 challenge = buffer_get_string(&m, NULL);
942 buffer_free(&m);
943
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000944 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000945
946 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
947
948 len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
949 p = xmalloc(len);
950 strlcpy(p, challenge, len);
951 strlcat(p, SKEY_PROMPT, len);
952 (*prompts)[0] = p;
953 xfree(challenge);
954
955 return (0);
956}
957
958int
959mm_skey_respond(void *ctx, u_int numresponses, char **responses)
960{
961 Buffer m;
962 int authok;
963
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000964 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000965 if (numresponses != 1)
966 return (-1);
967
968 buffer_init(&m);
969 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000970 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000971
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000972 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000973 MONITOR_ANS_SKEYRESPOND, &m);
974
975 authok = buffer_get_int(&m);
976 buffer_free(&m);
977
978 return ((authok == 0) ? -1 : 0);
979}
980
981void
982mm_ssh1_session_id(u_char session_id[16])
983{
984 Buffer m;
985 int i;
986
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000987 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000988
989 buffer_init(&m);
990 for (i = 0; i < 16; i++)
991 buffer_put_char(&m, session_id[i]);
992
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000993 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000994 buffer_free(&m);
995}
996
997int
998mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
999{
1000 Buffer m;
1001 Key *key;
1002 u_char *blob;
1003 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001004 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001006 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001007
1008 buffer_init(&m);
1009 buffer_put_bignum2(&m, client_n);
1010
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001011 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1012 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001013
1014 allowed = buffer_get_int(&m);
1015
Damien Miller06ebedf2003-02-24 12:03:38 +11001016 /* fake forced command */
1017 auth_clear_options();
1018 have_forced = buffer_get_int(&m);
1019 forced_command = have_forced ? xstrdup("true") : NULL;
1020
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001021 if (allowed && rkey != NULL) {
1022 blob = buffer_get_string(&m, &blen);
1023 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001024 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001025 *rkey = key;
1026 xfree(blob);
1027 }
1028 mm_send_debug(&m);
1029 buffer_free(&m);
1030
1031 return (allowed);
1032}
1033
1034BIGNUM *
1035mm_auth_rsa_generate_challenge(Key *key)
1036{
1037 Buffer m;
1038 BIGNUM *challenge;
1039 u_char *blob;
1040 u_int blen;
1041
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001042 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001043
1044 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001045 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001046
1047 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1048 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001049 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001050 key->type = KEY_RSA1;
1051
1052 buffer_init(&m);
1053 buffer_put_string(&m, blob, blen);
1054 xfree(blob);
1055
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001056 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1057 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001058
1059 buffer_get_bignum2(&m, challenge);
1060 buffer_free(&m);
1061
1062 return (challenge);
1063}
1064
1065int
1066mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1067{
1068 Buffer m;
1069 u_char *blob;
1070 u_int blen;
1071 int success = 0;
1072
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001073 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001074
1075 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1076 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001077 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001078 key->type = KEY_RSA1;
1079
1080 buffer_init(&m);
1081 buffer_put_string(&m, blob, blen);
1082 buffer_put_string(&m, response, 16);
1083 xfree(blob);
1084
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001085 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1086 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001087
1088 success = buffer_get_int(&m);
1089 buffer_free(&m);
1090
1091 return (success);
1092}
Damien Miller25162f22002-09-12 09:47:29 +10001093
Darren Tucker0efd1552003-08-26 11:49:55 +10001094#ifdef GSSAPI
1095OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001096mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001097{
1098 Buffer m;
1099 OM_uint32 major;
1100
1101 /* Client doesn't get to see the context */
1102 *ctx = NULL;
1103
1104 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001105 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001106
1107 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1108 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1109
1110 major = buffer_get_int(&m);
1111
1112 buffer_free(&m);
1113 return (major);
1114}
1115
1116OM_uint32
1117mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1118 gss_buffer_desc *out, OM_uint32 *flags)
1119{
1120 Buffer m;
1121 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001122 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001123
1124 buffer_init(&m);
1125 buffer_put_string(&m, in->value, in->length);
1126
1127 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1128 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1129
1130 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001131 out->value = buffer_get_string(&m, &len);
1132 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001133 if (flags)
1134 *flags = buffer_get_int(&m);
1135
1136 buffer_free(&m);
1137
1138 return (major);
1139}
1140
Damien Miller0425d402003-11-17 22:18:21 +11001141OM_uint32
1142mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1143{
1144 Buffer m;
1145 OM_uint32 major;
1146
1147 buffer_init(&m);
1148 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1149 buffer_put_string(&m, gssmic->value, gssmic->length);
1150
1151 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1152 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1153 &m);
1154
1155 major = buffer_get_int(&m);
1156 buffer_free(&m);
1157 return(major);
1158}
1159
Darren Tucker0efd1552003-08-26 11:49:55 +10001160int
1161mm_ssh_gssapi_userok(char *user)
1162{
1163 Buffer m;
1164 int authenticated = 0;
1165
1166 buffer_init(&m);
1167
1168 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1169 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1170 &m);
1171
1172 authenticated = buffer_get_int(&m);
1173
1174 buffer_free(&m);
1175 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1176 return (authenticated);
1177}
1178#endif /* GSSAPI */