blob: afa612f4eb5dcc4d4f2b66f9ca681fa2f9a0b4ba [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"
Damien Millerb253cc42005-05-26 12:23:44 +100028RCSID("$OpenBSD: monitor_wrap.c,v 1.40 2005/05/24 17:32:43 avsm Exp $");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029
30#include <openssl/bn.h>
31#include <openssl/dh.h>
32
33#include "ssh.h"
34#include "dh.h"
35#include "kex.h"
36#include "auth.h"
Damien Miller06ebedf2003-02-24 12:03:38 +110037#include "auth-options.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000038#include "buffer.h"
39#include "bufaux.h"
40#include "packet.h"
41#include "mac.h"
42#include "log.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000043#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
44#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000045#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000046#define TARGET_OS_MAC 1
47#else
48#include "zlib.h"
49#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000050#include "monitor.h"
51#include "monitor_wrap.h"
52#include "xmalloc.h"
53#include "atomicio.h"
54#include "monitor_fdpass.h"
55#include "getput.h"
Damien Miller4e448a32003-05-14 15:11:48 +100056#include "servconf.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000057
58#include "auth.h"
59#include "channels.h"
60#include "session.h"
61
Darren Tucker0efd1552003-08-26 11:49:55 +100062#ifdef GSSAPI
63#include "ssh-gss.h"
64#endif
65
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000066/* Imports */
67extern int compat20;
68extern Newkeys *newkeys[];
69extern z_stream incoming_stream;
70extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000071extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000072extern Buffer input, output;
Darren Tucker09991742004-07-17 17:05:14 +100073extern Buffer loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100074extern ServerOptions options;
Darren Tucker77fc29e2004-09-11 23:07:03 +100075extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000076
Darren Tucker3e33cec2003-10-02 16:12:36 +100077int
78mm_is_monitor(void)
79{
80 /*
81 * m_pid is only set in the privileged part, and
82 * points to the unprivileged child.
83 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +110084 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +100085}
86
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000087void
Darren Tucker3f9fdc72004-06-22 12:56:01 +100088mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000089{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000090 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +000091 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000092
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000093 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000094
95 PUT_32BIT(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +000096 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +100097 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +100098 fatal("%s: write: %s", __func__, strerror(errno));
Darren Tucker3f9fdc72004-06-22 12:56:01 +100099 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +1000100 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101}
102
103void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000104mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105{
106 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107 u_int msg_len;
108
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000109 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000110
Damien Millerb253cc42005-05-26 12:23:44 +1000111 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
112 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000113 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000114 fatal("%s: read: %s", __func__, strerror(errno));
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);
Damien Millerb253cc42005-05-26 12:23:44 +1000121 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
122 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000123}
124
125void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000126mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000127{
128 u_char rtype;
129
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000130 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000131
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000132 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000133 rtype = buffer_get_char(m);
134 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000135 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000136 rtype, type);
137}
138
139DH *
140mm_choose_dh(int min, int nbits, int max)
141{
142 BIGNUM *p, *g;
143 int success = 0;
144 Buffer m;
145
146 buffer_init(&m);
147 buffer_put_int(&m, min);
148 buffer_put_int(&m, nbits);
149 buffer_put_int(&m, max);
150
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000151 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000152
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000153 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000154 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000155
156 success = buffer_get_char(&m);
157 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000158 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000159
160 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000161 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000162 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000163 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000164 buffer_get_bignum2(&m, p);
165 buffer_get_bignum2(&m, g);
166
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000167 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000168 buffer_free(&m);
169
170 return (dh_new_group(g, p));
171}
172
173int
174mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
175{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000176 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000177 Buffer m;
178
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000179 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000180
181 buffer_init(&m);
182 buffer_put_int(&m, kex->host_key_index(key));
183 buffer_put_string(&m, data, datalen);
184
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000185 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000186
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000187 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000188 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000189 *sigp = buffer_get_string(&m, lenp);
190 buffer_free(&m);
191
192 return (0);
193}
194
195struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000196mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000197{
198 Buffer m;
199 struct passwd *pw;
200 u_int pwlen;
201
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000202 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000203
204 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000205 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000207 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000208
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000209 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000210 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000211
212 if (buffer_get_char(&m) == 0) {
213 buffer_free(&m);
214 return (NULL);
215 }
216 pw = buffer_get_string(&m, &pwlen);
217 if (pwlen != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000218 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000219 pw->pw_name = buffer_get_string(&m, NULL);
220 pw->pw_passwd = buffer_get_string(&m, NULL);
221 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000222#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000223 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000224#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000225 pw->pw_dir = buffer_get_string(&m, NULL);
226 pw->pw_shell = buffer_get_string(&m, NULL);
227 buffer_free(&m);
228
229 return (pw);
230}
231
Darren Tucker7eb3de02003-10-15 15:56:58 +1000232char *
233mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000234{
235 Buffer m;
236 char *banner;
237
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000238 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000239
240 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000241 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000242 buffer_clear(&m);
243
Darren Tucker7eb3de02003-10-15 15:56:58 +1000244 mm_request_receive_expect(pmonitor->m_recvfd,
245 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000246 banner = buffer_get_string(&m, NULL);
247 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000248
Darren Tucker7eb3de02003-10-15 15:56:58 +1000249 /* treat empty banner as missing banner */
250 if (strlen(banner) == 0) {
251 xfree(banner);
252 banner = NULL;
253 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000254 return (banner);
255}
256
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000257/* Inform the privileged process about service and style */
258
259void
260mm_inform_authserv(char *service, char *style)
261{
262 Buffer m;
263
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000264 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000265
266 buffer_init(&m);
267 buffer_put_cstring(&m, service);
268 buffer_put_cstring(&m, style ? style : "");
269
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000270 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000271
272 buffer_free(&m);
273}
274
275/* Do the password authentication */
276int
277mm_auth_password(Authctxt *authctxt, char *password)
278{
279 Buffer m;
280 int authenticated = 0;
281
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000282 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000283
284 buffer_init(&m);
285 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000286 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000287
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000288 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000289 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000290
291 authenticated = buffer_get_int(&m);
292
293 buffer_free(&m);
294
295 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000296 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000297 return (authenticated);
298}
299
300int
301mm_user_key_allowed(struct passwd *pw, Key *key)
302{
303 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
304}
305
306int
307mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
308 Key *key)
309{
310 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
311}
312
313int
314mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
315 char *host, Key *key)
316{
317 int ret;
318
319 key->type = KEY_RSA; /* XXX hack for key_to_blob */
320 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
321 key->type = KEY_RSA1;
322 return (ret);
323}
324
325static void
326mm_send_debug(Buffer *m)
327{
328 char *msg;
329
330 while (buffer_len(m)) {
331 msg = buffer_get_string(m, NULL);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000332 debug3("%s: Sending debug: %s", __func__, msg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000333 packet_send_debug("%s", msg);
334 xfree(msg);
335 }
336}
337
338int
339mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
340{
341 Buffer m;
342 u_char *blob;
343 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100344 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000345
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000346 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000347
348 /* Convert the key to a blob and the pass it over */
349 if (!key_to_blob(key, &blob, &len))
350 return (0);
351
352 buffer_init(&m);
353 buffer_put_int(&m, type);
354 buffer_put_cstring(&m, user ? user : "");
355 buffer_put_cstring(&m, host ? host : "");
356 buffer_put_string(&m, blob, len);
357 xfree(blob);
358
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000359 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000360
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000361 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000362 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000363
364 allowed = buffer_get_int(&m);
365
Damien Miller06ebedf2003-02-24 12:03:38 +1100366 /* fake forced command */
367 auth_clear_options();
368 have_forced = buffer_get_int(&m);
369 forced_command = have_forced ? xstrdup("true") : NULL;
370
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000371 /* Send potential debug messages */
372 mm_send_debug(&m);
373
374 buffer_free(&m);
375
376 return (allowed);
377}
378
379/*
380 * This key verify needs to send the key type along, because the
381 * privileged parent makes the decision if the key is allowed
382 * for authentication.
383 */
384
385int
386mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
387{
388 Buffer m;
389 u_char *blob;
390 u_int len;
391 int verified = 0;
392
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000393 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000394
395 /* Convert the key to a blob and the pass it over */
396 if (!key_to_blob(key, &blob, &len))
397 return (0);
398
399 buffer_init(&m);
400 buffer_put_string(&m, blob, len);
401 buffer_put_string(&m, sig, siglen);
402 buffer_put_string(&m, data, datalen);
403 xfree(blob);
404
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000405 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000406
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000407 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000408 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000409
410 verified = buffer_get_int(&m);
411
412 buffer_free(&m);
413
414 return (verified);
415}
416
417/* Export key state after authentication */
418Newkeys *
419mm_newkeys_from_blob(u_char *blob, int blen)
420{
421 Buffer b;
422 u_int len;
423 Newkeys *newkey = NULL;
424 Enc *enc;
425 Mac *mac;
426 Comp *comp;
427
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000428 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000429#ifdef DEBUG_PK
430 dump_base64(stderr, blob, blen);
431#endif
432 buffer_init(&b);
433 buffer_append(&b, blob, blen);
434
435 newkey = xmalloc(sizeof(*newkey));
436 enc = &newkey->enc;
437 mac = &newkey->mac;
438 comp = &newkey->comp;
439
440 /* Enc structure */
441 enc->name = buffer_get_string(&b, NULL);
442 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
443 enc->enabled = buffer_get_int(&b);
444 enc->block_size = buffer_get_int(&b);
445 enc->key = buffer_get_string(&b, &enc->key_len);
446 enc->iv = buffer_get_string(&b, &len);
447 if (len != enc->block_size)
Ben Lindstrom08512492002-06-27 00:23:02 +0000448 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000449 enc->block_size, len);
450
451 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000452 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000453 enc->name, enc->cipher);
454
455 /* Mac structure */
456 mac->name = buffer_get_string(&b, NULL);
457 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000458 fatal("%s: can not init mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000459 mac->enabled = buffer_get_int(&b);
460 mac->key = buffer_get_string(&b, &len);
461 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000462 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000463 mac->key_len);
464 mac->key_len = len;
465
466 /* Comp structure */
467 comp->type = buffer_get_int(&b);
468 comp->enabled = buffer_get_int(&b);
469 comp->name = buffer_get_string(&b, NULL);
470
471 len = buffer_len(&b);
472 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000473 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000474 buffer_free(&b);
475 return (newkey);
476}
477
478int
479mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
480{
481 Buffer b;
482 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000483 Enc *enc;
484 Mac *mac;
485 Comp *comp;
486 Newkeys *newkey = newkeys[mode];
487
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000488 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000489
490 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000491 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000492 return 0;
493 }
494 enc = &newkey->enc;
495 mac = &newkey->mac;
496 comp = &newkey->comp;
497
498 buffer_init(&b);
499 /* Enc structure */
500 buffer_put_cstring(&b, enc->name);
501 /* The cipher struct is constant and shared, you export pointer */
502 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
503 buffer_put_int(&b, enc->enabled);
504 buffer_put_int(&b, enc->block_size);
505 buffer_put_string(&b, enc->key, enc->key_len);
506 packet_get_keyiv(mode, enc->iv, enc->block_size);
507 buffer_put_string(&b, enc->iv, enc->block_size);
508
509 /* Mac structure */
510 buffer_put_cstring(&b, mac->name);
511 buffer_put_int(&b, mac->enabled);
512 buffer_put_string(&b, mac->key, mac->key_len);
513
514 /* Comp structure */
515 buffer_put_int(&b, comp->type);
516 buffer_put_int(&b, comp->enabled);
517 buffer_put_cstring(&b, comp->name);
518
519 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000520 if (lenp != NULL)
521 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000522 if (blobp != NULL) {
523 *blobp = xmalloc(len);
524 memcpy(*blobp, buffer_ptr(&b), len);
525 }
526 memset(buffer_ptr(&b), 0, len);
527 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000528 return len;
529}
530
531static void
532mm_send_kex(Buffer *m, Kex *kex)
533{
534 buffer_put_string(m, kex->session_id, kex->session_id_len);
535 buffer_put_int(m, kex->we_need);
536 buffer_put_int(m, kex->hostkey_type);
537 buffer_put_int(m, kex->kex_type);
538 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
539 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
540 buffer_put_int(m, kex->flags);
541 buffer_put_cstring(m, kex->client_version_string);
542 buffer_put_cstring(m, kex->server_version_string);
543}
544
545void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000546mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000547{
548 Buffer m;
549 u_char *blob, *p;
550 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000551 u_int32_t seqnr, packets;
552 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000553
554 buffer_init(&m);
555
556 if (!compat20) {
557 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000558 u_char *key;
559 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000560
561 buffer_put_int(&m, packet_get_protocol_flags());
562
563 buffer_put_int(&m, packet_get_ssh1_cipher());
564
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000565 debug3("%s: Sending ssh1 KEY+IV", __func__);
566 keylen = packet_get_encryption_key(NULL);
567 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
568 keylen = packet_get_encryption_key(key);
569 buffer_put_string(&m, key, keylen);
570 memset(key, 0, keylen);
571 xfree(key);
572
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000573 ivlen = packet_get_keyiv_len(MODE_OUT);
574 packet_get_keyiv(MODE_OUT, iv, ivlen);
575 buffer_put_string(&m, iv, ivlen);
576 ivlen = packet_get_keyiv_len(MODE_OUT);
577 packet_get_keyiv(MODE_IN, iv, ivlen);
578 buffer_put_string(&m, iv, ivlen);
579 goto skip;
580 } else {
581 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000582 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000583 }
584
585 debug3("%s: Sending new keys: %p %p",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000586 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587
588 /* Keys from Kex */
589 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000590 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000591
592 buffer_put_string(&m, blob, bloblen);
593 xfree(blob);
594
595 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000596 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597
598 buffer_put_string(&m, blob, bloblen);
599 xfree(blob);
600
Damien Millera5539d22003-04-09 20:50:06 +1000601 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
602 buffer_put_int(&m, seqnr);
603 buffer_put_int64(&m, blocks);
604 buffer_put_int(&m, packets);
Damien Millerb1ecd9c2003-04-09 20:51:24 +1000605 packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
Damien Millera5539d22003-04-09 20:50:06 +1000606 buffer_put_int(&m, seqnr);
607 buffer_put_int64(&m, blocks);
608 buffer_put_int(&m, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000610 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000611 skip:
612 /* More key context */
613 plen = packet_get_keycontext(MODE_OUT, NULL);
614 p = xmalloc(plen+1);
615 packet_get_keycontext(MODE_OUT, p);
616 buffer_put_string(&m, p, plen);
617 xfree(p);
618
619 plen = packet_get_keycontext(MODE_IN, NULL);
620 p = xmalloc(plen+1);
621 packet_get_keycontext(MODE_IN, p);
622 buffer_put_string(&m, p, plen);
623 xfree(p);
624
625 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000626 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000627 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
628 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
629
630 /* Network I/O buffers */
631 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
632 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
633
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000634 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000635 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000636
637 buffer_free(&m);
638}
639
640int
641mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
642{
643 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000644 char *p, *msg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000645 int success = 0;
646
647 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000648 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000649
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000650 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000651 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000652
653 success = buffer_get_int(&m);
654 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000655 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000656 buffer_free(&m);
657 return (0);
658 }
659 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000660 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000661 buffer_free(&m);
662
663 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
664 xfree(p);
665
Darren Tucker09991742004-07-17 17:05:14 +1000666 buffer_append(&loginmsg, msg, strlen(msg));
667 xfree(msg);
668
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000669 *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
670 *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000671
672 /* Success */
673 return (1);
674}
675
676void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000677mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000678{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000679 Buffer m;
680
681 if (s->ttyfd == -1)
682 return;
683 buffer_init(&m);
684 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000685 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000686 buffer_free(&m);
687
688 /* closed dup'ed master */
689 if (close(s->ptymaster) < 0)
690 error("close(s->ptymaster): %s", strerror(errno));
691
692 /* unlink pty from session */
693 s->ttyfd = -1;
694}
695
Damien Miller79418552002-04-23 20:28:48 +1000696#ifdef USE_PAM
697void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100698mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000699{
700 Buffer m;
701
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000702 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000703 if (!options.use_pam)
704 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000705
706 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000707 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000708
709 buffer_free(&m);
710}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000711
Damien Miller1f499fd2003-08-25 13:08:49 +1000712u_int
713mm_do_pam_account(void)
714{
715 Buffer m;
716 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000717 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000718
719 debug3("%s entering", __func__);
720 if (!options.use_pam)
721 fatal("UsePAM=no, but ended up in %s anyway", __func__);
722
723 buffer_init(&m);
724 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
725
Damien Millera8e06ce2003-11-21 23:48:55 +1100726 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000727 MONITOR_ANS_PAM_ACCOUNT, &m);
728 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000729 msg = buffer_get_string(&m, NULL);
730 buffer_append(&loginmsg, msg, strlen(msg));
731 xfree(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000732
733 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100734
Damien Miller1f499fd2003-08-25 13:08:49 +1000735 debug3("%s returning %d", __func__, ret);
736
737 return (ret);
738}
739
Damien Miller4f9f42a2003-05-10 19:28:02 +1000740void *
741mm_sshpam_init_ctx(Authctxt *authctxt)
742{
743 Buffer m;
744 int success;
745
746 debug3("%s", __func__);
747 buffer_init(&m);
748 buffer_put_cstring(&m, authctxt->user);
749 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
750 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
751 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
752 success = buffer_get_int(&m);
753 if (success == 0) {
754 debug3("%s: pam_init_ctx failed", __func__);
755 buffer_free(&m);
756 return (NULL);
757 }
758 buffer_free(&m);
759 return (authctxt);
760}
761
762int
763mm_sshpam_query(void *ctx, char **name, char **info,
764 u_int *num, char ***prompts, u_int **echo_on)
765{
766 Buffer m;
767 int i, ret;
768
769 debug3("%s", __func__);
770 buffer_init(&m);
771 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
772 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
773 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
774 ret = buffer_get_int(&m);
775 debug3("%s: pam_query returned %d", __func__, ret);
776 *name = buffer_get_string(&m, NULL);
777 *info = buffer_get_string(&m, NULL);
778 *num = buffer_get_int(&m);
779 *prompts = xmalloc((*num + 1) * sizeof(char *));
780 *echo_on = xmalloc((*num + 1) * sizeof(u_int));
781 for (i = 0; i < *num; ++i) {
782 (*prompts)[i] = buffer_get_string(&m, NULL);
783 (*echo_on)[i] = buffer_get_int(&m);
784 }
785 buffer_free(&m);
786 return (ret);
787}
788
789int
790mm_sshpam_respond(void *ctx, u_int num, char **resp)
791{
792 Buffer m;
793 int i, ret;
794
795 debug3("%s", __func__);
796 buffer_init(&m);
797 buffer_put_int(&m, num);
798 for (i = 0; i < num; ++i)
799 buffer_put_cstring(&m, resp[i]);
800 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
801 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
802 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
803 ret = buffer_get_int(&m);
804 debug3("%s: pam_respond returned %d", __func__, ret);
805 buffer_free(&m);
806 return (ret);
807}
808
809void
810mm_sshpam_free_ctx(void *ctxtp)
811{
812 Buffer m;
813
814 debug3("%s", __func__);
815 buffer_init(&m);
816 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
817 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
818 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
819 buffer_free(&m);
820}
Damien Miller79418552002-04-23 20:28:48 +1000821#endif /* USE_PAM */
822
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000823/* Request process termination */
824
825void
826mm_terminate(void)
827{
828 Buffer m;
829
830 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000831 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000832 buffer_free(&m);
833}
834
835int
836mm_ssh1_session_key(BIGNUM *num)
837{
838 int rsafail;
839 Buffer m;
840
841 buffer_init(&m);
842 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000843 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000844
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000845 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000846
847 rsafail = buffer_get_int(&m);
848 buffer_get_bignum2(&m, num);
849
850 buffer_free(&m);
851
852 return (rsafail);
853}
854
855static void
856mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
857 char ***prompts, u_int **echo_on)
858{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000859 *name = xstrdup("");
860 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000861 *numprompts = 1;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000862 *prompts = xmalloc(*numprompts * sizeof(char *));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000863 *echo_on = xmalloc(*numprompts * sizeof(u_int));
864 (*echo_on)[0] = 0;
865}
866
867int
868mm_bsdauth_query(void *ctx, char **name, char **infotxt,
869 u_int *numprompts, char ***prompts, u_int **echo_on)
870{
871 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100872 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000873 char *challenge;
874
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000875 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000876
877 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000878 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000879
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000880 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000881 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100882 success = buffer_get_int(&m);
883 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000884 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000885 buffer_free(&m);
886 return (-1);
887 }
888
889 /* Get the challenge, and format the response */
890 challenge = buffer_get_string(&m, NULL);
891 buffer_free(&m);
892
893 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
894 (*prompts)[0] = challenge;
895
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000896 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000897
898 return (0);
899}
900
901int
902mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
903{
904 Buffer m;
905 int authok;
906
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000907 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000908 if (numresponses != 1)
909 return (-1);
910
911 buffer_init(&m);
912 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000913 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000914
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000915 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000916 MONITOR_ANS_BSDAUTHRESPOND, &m);
917
918 authok = buffer_get_int(&m);
919 buffer_free(&m);
920
921 return ((authok == 0) ? -1 : 0);
922}
923
Darren Tucker042e2e82004-07-08 23:09:42 +1000924#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000925int
926mm_skey_query(void *ctx, char **name, char **infotxt,
927 u_int *numprompts, char ***prompts, u_int **echo_on)
928{
929 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100930 int len;
931 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932 char *p, *challenge;
933
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000934 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000935
936 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000937 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000938
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000939 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000940 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100941 success = buffer_get_int(&m);
942 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000943 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000944 buffer_free(&m);
945 return (-1);
946 }
947
948 /* Get the challenge, and format the response */
949 challenge = buffer_get_string(&m, NULL);
950 buffer_free(&m);
951
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000952 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000953
954 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
955
956 len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
957 p = xmalloc(len);
958 strlcpy(p, challenge, len);
959 strlcat(p, SKEY_PROMPT, len);
960 (*prompts)[0] = p;
961 xfree(challenge);
962
963 return (0);
964}
965
966int
967mm_skey_respond(void *ctx, u_int numresponses, char **responses)
968{
969 Buffer m;
970 int authok;
971
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000972 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000973 if (numresponses != 1)
974 return (-1);
975
976 buffer_init(&m);
977 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000978 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000979
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000980 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000981 MONITOR_ANS_SKEYRESPOND, &m);
982
983 authok = buffer_get_int(&m);
984 buffer_free(&m);
985
986 return ((authok == 0) ? -1 : 0);
987}
Darren Tucker042e2e82004-07-08 23:09:42 +1000988#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000989
990void
991mm_ssh1_session_id(u_char session_id[16])
992{
993 Buffer m;
994 int i;
995
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000996 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000997
998 buffer_init(&m);
999 for (i = 0; i < 16; i++)
1000 buffer_put_char(&m, session_id[i]);
1001
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001002 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001003 buffer_free(&m);
1004}
1005
1006int
1007mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1008{
1009 Buffer m;
1010 Key *key;
1011 u_char *blob;
1012 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001013 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001014
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001015 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001016
1017 buffer_init(&m);
1018 buffer_put_bignum2(&m, client_n);
1019
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001020 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1021 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001022
1023 allowed = buffer_get_int(&m);
1024
Damien Miller06ebedf2003-02-24 12:03:38 +11001025 /* fake forced command */
1026 auth_clear_options();
1027 have_forced = buffer_get_int(&m);
1028 forced_command = have_forced ? xstrdup("true") : NULL;
1029
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001030 if (allowed && rkey != NULL) {
1031 blob = buffer_get_string(&m, &blen);
1032 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001033 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001034 *rkey = key;
1035 xfree(blob);
1036 }
1037 mm_send_debug(&m);
1038 buffer_free(&m);
1039
1040 return (allowed);
1041}
1042
1043BIGNUM *
1044mm_auth_rsa_generate_challenge(Key *key)
1045{
1046 Buffer m;
1047 BIGNUM *challenge;
1048 u_char *blob;
1049 u_int blen;
1050
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001051 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001052
1053 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001054 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001055
1056 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1057 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001058 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001059 key->type = KEY_RSA1;
1060
1061 buffer_init(&m);
1062 buffer_put_string(&m, blob, blen);
1063 xfree(blob);
1064
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001065 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1066 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001067
1068 buffer_get_bignum2(&m, challenge);
1069 buffer_free(&m);
1070
1071 return (challenge);
1072}
1073
1074int
1075mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1076{
1077 Buffer m;
1078 u_char *blob;
1079 u_int blen;
1080 int success = 0;
1081
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001082 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001083
1084 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1085 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001086 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001087 key->type = KEY_RSA1;
1088
1089 buffer_init(&m);
1090 buffer_put_string(&m, blob, blen);
1091 buffer_put_string(&m, response, 16);
1092 xfree(blob);
1093
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001094 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1095 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001096
1097 success = buffer_get_int(&m);
1098 buffer_free(&m);
1099
1100 return (success);
1101}
Damien Miller25162f22002-09-12 09:47:29 +10001102
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001103#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001104void
1105mm_audit_event(ssh_audit_event_t event)
1106{
1107 Buffer m;
1108
1109 debug3("%s entering", __func__);
1110
1111 buffer_init(&m);
1112 buffer_put_int(&m, event);
1113
1114 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1115 buffer_free(&m);
1116}
1117
1118void
1119mm_audit_run_command(const char *command)
1120{
1121 Buffer m;
1122
1123 debug3("%s entering command %s", __func__, command);
1124
1125 buffer_init(&m);
1126 buffer_put_cstring(&m, command);
1127
1128 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1129 buffer_free(&m);
1130}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001131#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001132
Darren Tucker0efd1552003-08-26 11:49:55 +10001133#ifdef GSSAPI
1134OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001135mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001136{
1137 Buffer m;
1138 OM_uint32 major;
1139
1140 /* Client doesn't get to see the context */
1141 *ctx = NULL;
1142
1143 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001144 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001145
1146 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1147 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1148
1149 major = buffer_get_int(&m);
1150
1151 buffer_free(&m);
1152 return (major);
1153}
1154
1155OM_uint32
1156mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1157 gss_buffer_desc *out, OM_uint32 *flags)
1158{
1159 Buffer m;
1160 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001161 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001162
1163 buffer_init(&m);
1164 buffer_put_string(&m, in->value, in->length);
1165
1166 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1167 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1168
1169 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001170 out->value = buffer_get_string(&m, &len);
1171 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001172 if (flags)
1173 *flags = buffer_get_int(&m);
1174
1175 buffer_free(&m);
1176
1177 return (major);
1178}
1179
Damien Miller0425d402003-11-17 22:18:21 +11001180OM_uint32
1181mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1182{
1183 Buffer m;
1184 OM_uint32 major;
1185
1186 buffer_init(&m);
1187 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1188 buffer_put_string(&m, gssmic->value, gssmic->length);
1189
1190 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1191 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1192 &m);
1193
1194 major = buffer_get_int(&m);
1195 buffer_free(&m);
1196 return(major);
1197}
1198
Darren Tucker0efd1552003-08-26 11:49:55 +10001199int
1200mm_ssh_gssapi_userok(char *user)
1201{
1202 Buffer m;
1203 int authenticated = 0;
1204
1205 buffer_init(&m);
1206
1207 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1208 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1209 &m);
1210
1211 authenticated = buffer_get_int(&m);
1212
1213 buffer_free(&m);
1214 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1215 return (authenticated);
1216}
1217#endif /* GSSAPI */