blob: 9ec60b6afd6bf4e374d2dfbfff55a2b2898e17ee [file] [log] [blame]
Darren Tucker39972492006-07-12 22:22:46 +10001/* $OpenBSD: monitor_wrap.c,v 1.47 2006/07/11 20:07:25 stevesk Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029
Damien Miller9f2abc42006-07-10 20:53:08 +100030#include <sys/types.h>
31
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000032#include <openssl/bn.h>
33#include <openssl/dh.h>
34
Darren Tucker39972492006-07-12 22:22:46 +100035#include <errno.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100036#include <pwd.h>
37
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000038#include "ssh.h"
39#include "dh.h"
40#include "kex.h"
41#include "auth.h"
Damien Miller06ebedf2003-02-24 12:03:38 +110042#include "auth-options.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000043#include "buffer.h"
44#include "bufaux.h"
45#include "packet.h"
46#include "mac.h"
47#include "log.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000048#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
49#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000050#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000051#define TARGET_OS_MAC 1
52#else
53#include "zlib.h"
54#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000055#include "monitor.h"
56#include "monitor_wrap.h"
57#include "xmalloc.h"
58#include "atomicio.h"
59#include "monitor_fdpass.h"
Damien Miller3f941882006-03-31 23:13:02 +110060#include "misc.h"
Damien Miller4e448a32003-05-14 15:11:48 +100061#include "servconf.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000062
63#include "auth.h"
64#include "channels.h"
65#include "session.h"
66
Darren Tucker0efd1552003-08-26 11:49:55 +100067#ifdef GSSAPI
68#include "ssh-gss.h"
69#endif
70
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000071/* Imports */
72extern int compat20;
73extern Newkeys *newkeys[];
74extern z_stream incoming_stream;
75extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000076extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000077extern Buffer input, output;
Darren Tucker09991742004-07-17 17:05:14 +100078extern Buffer loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100079extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000080
Darren Tucker3e33cec2003-10-02 16:12:36 +100081int
82mm_is_monitor(void)
83{
84 /*
85 * m_pid is only set in the privileged part, and
86 * points to the unprivileged child.
87 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +110088 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +100089}
90
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000091void
Darren Tucker3f9fdc72004-06-22 12:56:01 +100092mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000094 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +000095 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000096
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000097 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000098
Damien Miller3f941882006-03-31 23:13:02 +110099 put_u32(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000100 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000101 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +1000102 fatal("%s: write: %s", __func__, strerror(errno));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000103 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +1000104 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105}
106
107void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000108mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000109{
110 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000111 u_int msg_len;
112
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000113 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000114
Damien Millerb253cc42005-05-26 12:23:44 +1000115 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
116 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000117 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000118 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000119 }
Damien Miller3f941882006-03-31 23:13:02 +1100120 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000121 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000122 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000123 buffer_clear(m);
124 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000125 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
126 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000127}
128
129void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000130mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000131{
132 u_char rtype;
133
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000134 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000135
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000136 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000137 rtype = buffer_get_char(m);
138 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000139 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000140 rtype, type);
141}
142
143DH *
144mm_choose_dh(int min, int nbits, int max)
145{
146 BIGNUM *p, *g;
147 int success = 0;
148 Buffer m;
149
150 buffer_init(&m);
151 buffer_put_int(&m, min);
152 buffer_put_int(&m, nbits);
153 buffer_put_int(&m, max);
154
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000155 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000156
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000157 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000158 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000159
160 success = buffer_get_char(&m);
161 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000162 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000163
164 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000165 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000166 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000167 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000168 buffer_get_bignum2(&m, p);
169 buffer_get_bignum2(&m, g);
170
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000171 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000172 buffer_free(&m);
173
174 return (dh_new_group(g, p));
175}
176
177int
178mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
179{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000180 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000181 Buffer m;
182
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000183 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000184
185 buffer_init(&m);
186 buffer_put_int(&m, kex->host_key_index(key));
187 buffer_put_string(&m, data, datalen);
188
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000189 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000190
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000191 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000192 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000193 *sigp = buffer_get_string(&m, lenp);
194 buffer_free(&m);
195
196 return (0);
197}
198
199struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000200mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000201{
202 Buffer m;
203 struct passwd *pw;
204 u_int pwlen;
205
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000206 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000207
208 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000209 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000210
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000211 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000212
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000213 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000214 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000215
216 if (buffer_get_char(&m) == 0) {
217 buffer_free(&m);
218 return (NULL);
219 }
220 pw = buffer_get_string(&m, &pwlen);
221 if (pwlen != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000222 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000223 pw->pw_name = buffer_get_string(&m, NULL);
224 pw->pw_passwd = buffer_get_string(&m, NULL);
225 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000226#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000227 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000228#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000229 pw->pw_dir = buffer_get_string(&m, NULL);
230 pw->pw_shell = buffer_get_string(&m, NULL);
231 buffer_free(&m);
232
233 return (pw);
234}
235
Darren Tucker7eb3de02003-10-15 15:56:58 +1000236char *
237mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000238{
239 Buffer m;
240 char *banner;
241
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000242 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000243
244 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000245 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000246 buffer_clear(&m);
247
Darren Tucker7eb3de02003-10-15 15:56:58 +1000248 mm_request_receive_expect(pmonitor->m_recvfd,
249 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000250 banner = buffer_get_string(&m, NULL);
251 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000252
Darren Tucker7eb3de02003-10-15 15:56:58 +1000253 /* treat empty banner as missing banner */
254 if (strlen(banner) == 0) {
255 xfree(banner);
256 banner = NULL;
257 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000258 return (banner);
259}
260
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000261/* Inform the privileged process about service and style */
262
263void
264mm_inform_authserv(char *service, char *style)
265{
266 Buffer m;
267
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000268 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000269
270 buffer_init(&m);
271 buffer_put_cstring(&m, service);
272 buffer_put_cstring(&m, style ? style : "");
273
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000274 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000275
276 buffer_free(&m);
277}
278
279/* Do the password authentication */
280int
281mm_auth_password(Authctxt *authctxt, char *password)
282{
283 Buffer m;
284 int authenticated = 0;
285
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000286 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000287
288 buffer_init(&m);
289 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000290 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000291
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000292 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000293 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000294
295 authenticated = buffer_get_int(&m);
296
297 buffer_free(&m);
298
299 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000300 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000301 return (authenticated);
302}
303
304int
305mm_user_key_allowed(struct passwd *pw, Key *key)
306{
307 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
308}
309
310int
311mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
312 Key *key)
313{
314 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
315}
316
317int
318mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
319 char *host, Key *key)
320{
321 int ret;
322
323 key->type = KEY_RSA; /* XXX hack for key_to_blob */
324 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
325 key->type = KEY_RSA1;
326 return (ret);
327}
328
329static void
330mm_send_debug(Buffer *m)
331{
332 char *msg;
333
334 while (buffer_len(m)) {
335 msg = buffer_get_string(m, NULL);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000336 debug3("%s: Sending debug: %s", __func__, msg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000337 packet_send_debug("%s", msg);
338 xfree(msg);
339 }
340}
341
342int
343mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
344{
345 Buffer m;
346 u_char *blob;
347 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100348 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000349
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000350 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000351
352 /* Convert the key to a blob and the pass it over */
353 if (!key_to_blob(key, &blob, &len))
354 return (0);
355
356 buffer_init(&m);
357 buffer_put_int(&m, type);
358 buffer_put_cstring(&m, user ? user : "");
359 buffer_put_cstring(&m, host ? host : "");
360 buffer_put_string(&m, blob, len);
361 xfree(blob);
362
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000363 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000364
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000365 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000366 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000367
368 allowed = buffer_get_int(&m);
369
Damien Miller06ebedf2003-02-24 12:03:38 +1100370 /* fake forced command */
371 auth_clear_options();
372 have_forced = buffer_get_int(&m);
373 forced_command = have_forced ? xstrdup("true") : NULL;
374
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000375 /* Send potential debug messages */
376 mm_send_debug(&m);
377
378 buffer_free(&m);
379
380 return (allowed);
381}
382
383/*
384 * This key verify needs to send the key type along, because the
385 * privileged parent makes the decision if the key is allowed
386 * for authentication.
387 */
388
389int
390mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
391{
392 Buffer m;
393 u_char *blob;
394 u_int len;
395 int verified = 0;
396
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000397 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000398
399 /* Convert the key to a blob and the pass it over */
400 if (!key_to_blob(key, &blob, &len))
401 return (0);
402
403 buffer_init(&m);
404 buffer_put_string(&m, blob, len);
405 buffer_put_string(&m, sig, siglen);
406 buffer_put_string(&m, data, datalen);
407 xfree(blob);
408
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000409 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000410
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000411 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000412 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000413
414 verified = buffer_get_int(&m);
415
416 buffer_free(&m);
417
418 return (verified);
419}
420
421/* Export key state after authentication */
422Newkeys *
423mm_newkeys_from_blob(u_char *blob, int blen)
424{
425 Buffer b;
426 u_int len;
427 Newkeys *newkey = NULL;
428 Enc *enc;
429 Mac *mac;
430 Comp *comp;
431
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000432 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000433#ifdef DEBUG_PK
434 dump_base64(stderr, blob, blen);
435#endif
436 buffer_init(&b);
437 buffer_append(&b, blob, blen);
438
439 newkey = xmalloc(sizeof(*newkey));
440 enc = &newkey->enc;
441 mac = &newkey->mac;
442 comp = &newkey->comp;
443
444 /* Enc structure */
445 enc->name = buffer_get_string(&b, NULL);
446 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
447 enc->enabled = buffer_get_int(&b);
448 enc->block_size = buffer_get_int(&b);
449 enc->key = buffer_get_string(&b, &enc->key_len);
450 enc->iv = buffer_get_string(&b, &len);
451 if (len != enc->block_size)
Ben Lindstrom08512492002-06-27 00:23:02 +0000452 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000453 enc->block_size, len);
454
455 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000456 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000457 enc->name, enc->cipher);
458
459 /* Mac structure */
460 mac->name = buffer_get_string(&b, NULL);
461 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000462 fatal("%s: can not init mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000463 mac->enabled = buffer_get_int(&b);
464 mac->key = buffer_get_string(&b, &len);
465 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000466 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000467 mac->key_len);
468 mac->key_len = len;
469
470 /* Comp structure */
471 comp->type = buffer_get_int(&b);
472 comp->enabled = buffer_get_int(&b);
473 comp->name = buffer_get_string(&b, NULL);
474
475 len = buffer_len(&b);
476 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000477 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000478 buffer_free(&b);
479 return (newkey);
480}
481
482int
483mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
484{
485 Buffer b;
486 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000487 Enc *enc;
488 Mac *mac;
489 Comp *comp;
490 Newkeys *newkey = newkeys[mode];
491
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000492 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000493
494 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000495 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000496 return 0;
497 }
498 enc = &newkey->enc;
499 mac = &newkey->mac;
500 comp = &newkey->comp;
501
502 buffer_init(&b);
503 /* Enc structure */
504 buffer_put_cstring(&b, enc->name);
505 /* The cipher struct is constant and shared, you export pointer */
506 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
507 buffer_put_int(&b, enc->enabled);
508 buffer_put_int(&b, enc->block_size);
509 buffer_put_string(&b, enc->key, enc->key_len);
510 packet_get_keyiv(mode, enc->iv, enc->block_size);
511 buffer_put_string(&b, enc->iv, enc->block_size);
512
513 /* Mac structure */
514 buffer_put_cstring(&b, mac->name);
515 buffer_put_int(&b, mac->enabled);
516 buffer_put_string(&b, mac->key, mac->key_len);
517
518 /* Comp structure */
519 buffer_put_int(&b, comp->type);
520 buffer_put_int(&b, comp->enabled);
521 buffer_put_cstring(&b, comp->name);
522
523 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000524 if (lenp != NULL)
525 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000526 if (blobp != NULL) {
527 *blobp = xmalloc(len);
528 memcpy(*blobp, buffer_ptr(&b), len);
529 }
530 memset(buffer_ptr(&b), 0, len);
531 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000532 return len;
533}
534
535static void
536mm_send_kex(Buffer *m, Kex *kex)
537{
538 buffer_put_string(m, kex->session_id, kex->session_id_len);
539 buffer_put_int(m, kex->we_need);
540 buffer_put_int(m, kex->hostkey_type);
541 buffer_put_int(m, kex->kex_type);
542 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
543 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
544 buffer_put_int(m, kex->flags);
545 buffer_put_cstring(m, kex->client_version_string);
546 buffer_put_cstring(m, kex->server_version_string);
547}
548
549void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000550mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000551{
552 Buffer m;
553 u_char *blob, *p;
554 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000555 u_int32_t seqnr, packets;
556 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000557
558 buffer_init(&m);
559
560 if (!compat20) {
561 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000562 u_char *key;
563 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000564
565 buffer_put_int(&m, packet_get_protocol_flags());
566
567 buffer_put_int(&m, packet_get_ssh1_cipher());
568
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000569 debug3("%s: Sending ssh1 KEY+IV", __func__);
570 keylen = packet_get_encryption_key(NULL);
571 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
572 keylen = packet_get_encryption_key(key);
573 buffer_put_string(&m, key, keylen);
574 memset(key, 0, keylen);
575 xfree(key);
576
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577 ivlen = packet_get_keyiv_len(MODE_OUT);
578 packet_get_keyiv(MODE_OUT, iv, ivlen);
579 buffer_put_string(&m, iv, ivlen);
580 ivlen = packet_get_keyiv_len(MODE_OUT);
581 packet_get_keyiv(MODE_IN, iv, ivlen);
582 buffer_put_string(&m, iv, ivlen);
583 goto skip;
584 } else {
585 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000586 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587 }
588
589 debug3("%s: Sending new keys: %p %p",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000590 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000591
592 /* Keys from Kex */
593 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000594 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000595
596 buffer_put_string(&m, blob, bloblen);
597 xfree(blob);
598
599 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000600 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000601
602 buffer_put_string(&m, blob, bloblen);
603 xfree(blob);
604
Damien Millera5539d22003-04-09 20:50:06 +1000605 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
606 buffer_put_int(&m, seqnr);
607 buffer_put_int64(&m, blocks);
608 buffer_put_int(&m, packets);
Damien Millerb1ecd9c2003-04-09 20:51:24 +1000609 packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
Damien Millera5539d22003-04-09 20:50:06 +1000610 buffer_put_int(&m, seqnr);
611 buffer_put_int64(&m, blocks);
612 buffer_put_int(&m, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000613
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000614 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000615 skip:
616 /* More key context */
617 plen = packet_get_keycontext(MODE_OUT, NULL);
618 p = xmalloc(plen+1);
619 packet_get_keycontext(MODE_OUT, p);
620 buffer_put_string(&m, p, plen);
621 xfree(p);
622
623 plen = packet_get_keycontext(MODE_IN, NULL);
624 p = xmalloc(plen+1);
625 packet_get_keycontext(MODE_IN, p);
626 buffer_put_string(&m, p, plen);
627 xfree(p);
628
629 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000630 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000631 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
632 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
633
634 /* Network I/O buffers */
635 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
636 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
637
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000638 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000639 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000640
641 buffer_free(&m);
642}
643
644int
Damien Miller71a73672006-03-26 14:04:36 +1100645mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000646{
647 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000648 char *p, *msg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000649 int success = 0;
650
651 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000652 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000653
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000654 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000655 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000656
657 success = buffer_get_int(&m);
658 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000659 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000660 buffer_free(&m);
661 return (0);
662 }
663 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000664 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000665 buffer_free(&m);
666
667 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
668 xfree(p);
669
Darren Tucker09991742004-07-17 17:05:14 +1000670 buffer_append(&loginmsg, msg, strlen(msg));
671 xfree(msg);
672
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000673 *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
674 *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000675
676 /* Success */
677 return (1);
678}
679
680void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000681mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000682{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000683 Buffer m;
684
685 if (s->ttyfd == -1)
686 return;
687 buffer_init(&m);
688 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000689 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000690 buffer_free(&m);
691
692 /* closed dup'ed master */
693 if (close(s->ptymaster) < 0)
694 error("close(s->ptymaster): %s", strerror(errno));
695
696 /* unlink pty from session */
697 s->ttyfd = -1;
698}
699
Damien Miller79418552002-04-23 20:28:48 +1000700#ifdef USE_PAM
701void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100702mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000703{
704 Buffer m;
705
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000706 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000707 if (!options.use_pam)
708 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000709
710 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000711 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000712
713 buffer_free(&m);
714}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000715
Damien Miller1f499fd2003-08-25 13:08:49 +1000716u_int
717mm_do_pam_account(void)
718{
719 Buffer m;
720 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000721 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000722
723 debug3("%s entering", __func__);
724 if (!options.use_pam)
725 fatal("UsePAM=no, but ended up in %s anyway", __func__);
726
727 buffer_init(&m);
728 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
729
Damien Millera8e06ce2003-11-21 23:48:55 +1100730 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000731 MONITOR_ANS_PAM_ACCOUNT, &m);
732 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000733 msg = buffer_get_string(&m, NULL);
734 buffer_append(&loginmsg, msg, strlen(msg));
735 xfree(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000736
737 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100738
Damien Miller1f499fd2003-08-25 13:08:49 +1000739 debug3("%s returning %d", __func__, ret);
740
741 return (ret);
742}
743
Damien Miller4f9f42a2003-05-10 19:28:02 +1000744void *
745mm_sshpam_init_ctx(Authctxt *authctxt)
746{
747 Buffer m;
748 int success;
749
750 debug3("%s", __func__);
751 buffer_init(&m);
752 buffer_put_cstring(&m, authctxt->user);
753 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
754 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
755 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
756 success = buffer_get_int(&m);
757 if (success == 0) {
758 debug3("%s: pam_init_ctx failed", __func__);
759 buffer_free(&m);
760 return (NULL);
761 }
762 buffer_free(&m);
763 return (authctxt);
764}
765
766int
767mm_sshpam_query(void *ctx, char **name, char **info,
768 u_int *num, char ***prompts, u_int **echo_on)
769{
770 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000771 u_int i;
772 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000773
774 debug3("%s", __func__);
775 buffer_init(&m);
776 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
777 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
778 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
779 ret = buffer_get_int(&m);
780 debug3("%s: pam_query returned %d", __func__, ret);
781 *name = buffer_get_string(&m, NULL);
782 *info = buffer_get_string(&m, NULL);
783 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000784 if (*num > PAM_MAX_NUM_MSG)
785 fatal("%s: recieved %u PAM messages, expected <= %u",
786 __func__, *num, PAM_MAX_NUM_MSG);
787 *prompts = xcalloc((*num + 1), sizeof(char *));
788 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000789 for (i = 0; i < *num; ++i) {
790 (*prompts)[i] = buffer_get_string(&m, NULL);
791 (*echo_on)[i] = buffer_get_int(&m);
792 }
793 buffer_free(&m);
794 return (ret);
795}
796
797int
798mm_sshpam_respond(void *ctx, u_int num, char **resp)
799{
800 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000801 u_int i;
802 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000803
804 debug3("%s", __func__);
805 buffer_init(&m);
806 buffer_put_int(&m, num);
807 for (i = 0; i < num; ++i)
808 buffer_put_cstring(&m, resp[i]);
809 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
810 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
811 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
812 ret = buffer_get_int(&m);
813 debug3("%s: pam_respond returned %d", __func__, ret);
814 buffer_free(&m);
815 return (ret);
816}
817
818void
819mm_sshpam_free_ctx(void *ctxtp)
820{
821 Buffer m;
822
823 debug3("%s", __func__);
824 buffer_init(&m);
825 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
826 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
827 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
828 buffer_free(&m);
829}
Damien Miller79418552002-04-23 20:28:48 +1000830#endif /* USE_PAM */
831
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000832/* Request process termination */
833
834void
835mm_terminate(void)
836{
837 Buffer m;
838
839 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000840 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000841 buffer_free(&m);
842}
843
844int
845mm_ssh1_session_key(BIGNUM *num)
846{
847 int rsafail;
848 Buffer m;
849
850 buffer_init(&m);
851 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000852 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000853
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000854 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000855
856 rsafail = buffer_get_int(&m);
857 buffer_get_bignum2(&m, num);
858
859 buffer_free(&m);
860
861 return (rsafail);
862}
863
864static void
865mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
866 char ***prompts, u_int **echo_on)
867{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000868 *name = xstrdup("");
869 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000870 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100871 *prompts = xcalloc(*numprompts, sizeof(char *));
872 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000873 (*echo_on)[0] = 0;
874}
875
876int
877mm_bsdauth_query(void *ctx, char **name, char **infotxt,
878 u_int *numprompts, char ***prompts, u_int **echo_on)
879{
880 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100881 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000882 char *challenge;
883
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000884 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000885
886 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000887 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000888
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000889 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000890 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100891 success = buffer_get_int(&m);
892 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000893 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000894 buffer_free(&m);
895 return (-1);
896 }
897
898 /* Get the challenge, and format the response */
899 challenge = buffer_get_string(&m, NULL);
900 buffer_free(&m);
901
902 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
903 (*prompts)[0] = challenge;
904
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000905 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000906
907 return (0);
908}
909
910int
911mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
912{
913 Buffer m;
914 int authok;
915
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000916 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000917 if (numresponses != 1)
918 return (-1);
919
920 buffer_init(&m);
921 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000922 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000923
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000924 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000925 MONITOR_ANS_BSDAUTHRESPOND, &m);
926
927 authok = buffer_get_int(&m);
928 buffer_free(&m);
929
930 return ((authok == 0) ? -1 : 0);
931}
932
Darren Tucker042e2e82004-07-08 23:09:42 +1000933#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000934int
935mm_skey_query(void *ctx, char **name, char **infotxt,
936 u_int *numprompts, char ***prompts, u_int **echo_on)
937{
938 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100939 int len;
940 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000941 char *p, *challenge;
942
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000943 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000944
945 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000946 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000947
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000948 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000949 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100950 success = buffer_get_int(&m);
951 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000952 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000953 buffer_free(&m);
954 return (-1);
955 }
956
957 /* Get the challenge, and format the response */
958 challenge = buffer_get_string(&m, NULL);
959 buffer_free(&m);
960
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000961 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000962
963 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
964
Damien Miller07d86be2006-03-26 14:19:21 +1100965 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000966 xfree(challenge);
967
968 return (0);
969}
970
971int
972mm_skey_respond(void *ctx, u_int numresponses, char **responses)
973{
974 Buffer m;
975 int authok;
976
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000977 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000978 if (numresponses != 1)
979 return (-1);
980
981 buffer_init(&m);
982 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000983 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000984
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000985 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000986 MONITOR_ANS_SKEYRESPOND, &m);
987
988 authok = buffer_get_int(&m);
989 buffer_free(&m);
990
991 return ((authok == 0) ? -1 : 0);
992}
Darren Tucker042e2e82004-07-08 23:09:42 +1000993#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000994
995void
996mm_ssh1_session_id(u_char session_id[16])
997{
998 Buffer m;
999 int i;
1000
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001001 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001002
1003 buffer_init(&m);
1004 for (i = 0; i < 16; i++)
1005 buffer_put_char(&m, session_id[i]);
1006
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001007 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001008 buffer_free(&m);
1009}
1010
1011int
1012mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1013{
1014 Buffer m;
1015 Key *key;
1016 u_char *blob;
1017 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001018 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001019
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001020 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001021
1022 buffer_init(&m);
1023 buffer_put_bignum2(&m, client_n);
1024
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001025 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1026 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001027
1028 allowed = buffer_get_int(&m);
1029
Damien Miller06ebedf2003-02-24 12:03:38 +11001030 /* fake forced command */
1031 auth_clear_options();
1032 have_forced = buffer_get_int(&m);
1033 forced_command = have_forced ? xstrdup("true") : NULL;
1034
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001035 if (allowed && rkey != NULL) {
1036 blob = buffer_get_string(&m, &blen);
1037 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001038 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001039 *rkey = key;
1040 xfree(blob);
1041 }
1042 mm_send_debug(&m);
1043 buffer_free(&m);
1044
1045 return (allowed);
1046}
1047
1048BIGNUM *
1049mm_auth_rsa_generate_challenge(Key *key)
1050{
1051 Buffer m;
1052 BIGNUM *challenge;
1053 u_char *blob;
1054 u_int blen;
1055
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001056 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001057
1058 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001059 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001060
1061 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1062 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001063 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001064 key->type = KEY_RSA1;
1065
1066 buffer_init(&m);
1067 buffer_put_string(&m, blob, blen);
1068 xfree(blob);
1069
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001070 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1071 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001072
1073 buffer_get_bignum2(&m, challenge);
1074 buffer_free(&m);
1075
1076 return (challenge);
1077}
1078
1079int
1080mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1081{
1082 Buffer m;
1083 u_char *blob;
1084 u_int blen;
1085 int success = 0;
1086
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001087 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001088
1089 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1090 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001091 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001092 key->type = KEY_RSA1;
1093
1094 buffer_init(&m);
1095 buffer_put_string(&m, blob, blen);
1096 buffer_put_string(&m, response, 16);
1097 xfree(blob);
1098
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001099 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1100 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001101
1102 success = buffer_get_int(&m);
1103 buffer_free(&m);
1104
1105 return (success);
1106}
Damien Miller25162f22002-09-12 09:47:29 +10001107
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001108#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001109void
1110mm_audit_event(ssh_audit_event_t event)
1111{
1112 Buffer m;
1113
1114 debug3("%s entering", __func__);
1115
1116 buffer_init(&m);
1117 buffer_put_int(&m, event);
1118
1119 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1120 buffer_free(&m);
1121}
1122
1123void
1124mm_audit_run_command(const char *command)
1125{
1126 Buffer m;
1127
1128 debug3("%s entering command %s", __func__, command);
1129
1130 buffer_init(&m);
1131 buffer_put_cstring(&m, command);
1132
1133 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1134 buffer_free(&m);
1135}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001136#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001137
Darren Tucker0efd1552003-08-26 11:49:55 +10001138#ifdef GSSAPI
1139OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001140mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001141{
1142 Buffer m;
1143 OM_uint32 major;
1144
1145 /* Client doesn't get to see the context */
1146 *ctx = NULL;
1147
1148 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001149 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001150
1151 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1152 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1153
1154 major = buffer_get_int(&m);
1155
1156 buffer_free(&m);
1157 return (major);
1158}
1159
1160OM_uint32
1161mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1162 gss_buffer_desc *out, OM_uint32 *flags)
1163{
1164 Buffer m;
1165 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001166 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001167
1168 buffer_init(&m);
1169 buffer_put_string(&m, in->value, in->length);
1170
1171 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1172 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1173
1174 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001175 out->value = buffer_get_string(&m, &len);
1176 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001177 if (flags)
1178 *flags = buffer_get_int(&m);
1179
1180 buffer_free(&m);
1181
1182 return (major);
1183}
1184
Damien Miller0425d402003-11-17 22:18:21 +11001185OM_uint32
1186mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1187{
1188 Buffer m;
1189 OM_uint32 major;
1190
1191 buffer_init(&m);
1192 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1193 buffer_put_string(&m, gssmic->value, gssmic->length);
1194
1195 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1196 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1197 &m);
1198
1199 major = buffer_get_int(&m);
1200 buffer_free(&m);
1201 return(major);
1202}
1203
Darren Tucker0efd1552003-08-26 11:49:55 +10001204int
1205mm_ssh_gssapi_userok(char *user)
1206{
1207 Buffer m;
1208 int authenticated = 0;
1209
1210 buffer_init(&m);
1211
1212 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1213 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1214 &m);
1215
1216 authenticated = buffer_get_int(&m);
1217
1218 buffer_free(&m);
1219 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1220 return (authenticated);
1221}
1222#endif /* GSSAPI */