blob: e895f19240ae6971bcee8346d5d35d1f5ef2a986 [file] [log] [blame]
Darren Tucker2f8b3d92007-12-02 23:02:15 +11001/* $OpenBSD: monitor_wrap.c,v 1.60 2007/10/29 04:08:08 dtucker 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>
Darren Tucker1a3d6e72006-08-05 18:46:47 +100031#include <sys/uio.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100032
Darren Tucker39972492006-07-12 22:22:46 +100033#include <errno.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100034#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100035#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100036#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100037#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100038#include <string.h>
Darren Tucker1a3d6e72006-08-05 18:46:47 +100039#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100040
Damien Millerd7834352006-08-05 12:39:39 +100041#include <openssl/bn.h>
42#include <openssl/dh.h>
43
44#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000045#include "ssh.h"
46#include "dh.h"
Damien Millerd7834352006-08-05 12:39:39 +100047#include "buffer.h"
48#include "key.h"
49#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000050#include "kex.h"
Damien Millerd7834352006-08-05 12:39:39 +100051#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000052#include "auth.h"
Damien Miller06ebedf2003-02-24 12:03:38 +110053#include "auth-options.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000054#include "packet.h"
55#include "mac.h"
56#include "log.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000057#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
58#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000059#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000060#define TARGET_OS_MAC 1
61#else
62#include "zlib.h"
63#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000064#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +100065#ifdef GSSAPI
66#include "ssh-gss.h"
67#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000068#include "monitor_wrap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000069#include "atomicio.h"
70#include "monitor_fdpass.h"
Damien Miller3f941882006-03-31 23:13:02 +110071#include "misc.h"
Damien Miller4e448a32003-05-14 15:11:48 +100072#include "servconf.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000073
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000074#include "channels.h"
75#include "session.h"
Darren Tucker1629c072007-02-19 22:25:37 +110076#include "servconf.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000077
78/* Imports */
79extern int compat20;
80extern Newkeys *newkeys[];
81extern z_stream incoming_stream;
82extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000083extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000084extern Buffer input, output;
Darren Tucker09991742004-07-17 17:05:14 +100085extern Buffer loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100086extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000087
Darren Tucker3e33cec2003-10-02 16:12:36 +100088int
89mm_is_monitor(void)
90{
91 /*
92 * m_pid is only set in the privileged part, and
93 * points to the unprivileged child.
94 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +110095 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +100096}
97
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000098void
Darren Tucker3f9fdc72004-06-22 12:56:01 +100099mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000100{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +0000102 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000103
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000104 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105
Damien Miller3f941882006-03-31 23:13:02 +1100106 put_u32(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000107 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000108 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +1000109 fatal("%s: write: %s", __func__, strerror(errno));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000110 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +1000111 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000112}
113
114void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000115mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000116{
117 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000118 u_int msg_len;
119
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000120 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000121
Damien Millerb253cc42005-05-26 12:23:44 +1000122 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
123 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000124 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000125 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000126 }
Damien Miller3f941882006-03-31 23:13:02 +1100127 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000128 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000129 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000130 buffer_clear(m);
131 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000132 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
133 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000134}
135
136void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000137mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000138{
139 u_char rtype;
140
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000141 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000142
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000143 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000144 rtype = buffer_get_char(m);
145 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000146 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000147 rtype, type);
148}
149
150DH *
151mm_choose_dh(int min, int nbits, int max)
152{
153 BIGNUM *p, *g;
154 int success = 0;
155 Buffer m;
156
157 buffer_init(&m);
158 buffer_put_int(&m, min);
159 buffer_put_int(&m, nbits);
160 buffer_put_int(&m, max);
161
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000162 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000163
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000164 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000165 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000166
167 success = buffer_get_char(&m);
168 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000169 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000170
171 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000172 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000173 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000174 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000175 buffer_get_bignum2(&m, p);
176 buffer_get_bignum2(&m, g);
177
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000178 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000179 buffer_free(&m);
180
181 return (dh_new_group(g, p));
182}
183
184int
185mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
186{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000187 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000188 Buffer m;
189
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000190 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000191
192 buffer_init(&m);
193 buffer_put_int(&m, kex->host_key_index(key));
194 buffer_put_string(&m, data, datalen);
195
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000196 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000197
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000198 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000199 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000200 *sigp = buffer_get_string(&m, lenp);
201 buffer_free(&m);
202
203 return (0);
204}
205
206struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000207mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000208{
209 Buffer m;
210 struct passwd *pw;
Darren Tucker1629c072007-02-19 22:25:37 +1100211 u_int len;
212 ServerOptions *newopts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000213
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000214 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000215
216 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000217 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000218
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000219 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000220
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000221 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000222 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000223
224 if (buffer_get_char(&m) == 0) {
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100225 pw = NULL;
226 goto out;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000227 }
Darren Tucker1629c072007-02-19 22:25:37 +1100228 pw = buffer_get_string(&m, &len);
229 if (len != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000230 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000231 pw->pw_name = buffer_get_string(&m, NULL);
232 pw->pw_passwd = buffer_get_string(&m, NULL);
233 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000234#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000235 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000236#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000237 pw->pw_dir = buffer_get_string(&m, NULL);
238 pw->pw_shell = buffer_get_string(&m, NULL);
Darren Tucker1629c072007-02-19 22:25:37 +1100239
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100240out:
Darren Tucker1629c072007-02-19 22:25:37 +1100241 /* copy options block as a Match directive may have changed some */
242 newopts = buffer_get_string(&m, &len);
243 if (len != sizeof(*newopts))
244 fatal("%s: option block size mismatch", __func__);
245 if (newopts->banner != NULL)
246 newopts->banner = buffer_get_string(&m, NULL);
247 copy_set_server_options(&options, newopts, 1);
248 xfree(newopts);
249
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000250 buffer_free(&m);
251
252 return (pw);
253}
254
Darren Tucker7eb3de02003-10-15 15:56:58 +1000255char *
256mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000257{
258 Buffer m;
259 char *banner;
260
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000261 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000262
263 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000264 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000265 buffer_clear(&m);
266
Darren Tucker7eb3de02003-10-15 15:56:58 +1000267 mm_request_receive_expect(pmonitor->m_recvfd,
268 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000269 banner = buffer_get_string(&m, NULL);
270 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000271
Darren Tucker7eb3de02003-10-15 15:56:58 +1000272 /* treat empty banner as missing banner */
273 if (strlen(banner) == 0) {
274 xfree(banner);
275 banner = NULL;
276 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000277 return (banner);
278}
279
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000280/* Inform the privileged process about service and style */
281
282void
283mm_inform_authserv(char *service, char *style)
284{
285 Buffer m;
286
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000287 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000288
289 buffer_init(&m);
290 buffer_put_cstring(&m, service);
291 buffer_put_cstring(&m, style ? style : "");
292
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000293 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000294
295 buffer_free(&m);
296}
297
298/* Do the password authentication */
299int
300mm_auth_password(Authctxt *authctxt, char *password)
301{
302 Buffer m;
303 int authenticated = 0;
304
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000305 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000306
307 buffer_init(&m);
308 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000309 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000310
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000311 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000312 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000313
314 authenticated = buffer_get_int(&m);
315
316 buffer_free(&m);
317
318 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000319 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000320 return (authenticated);
321}
322
323int
324mm_user_key_allowed(struct passwd *pw, Key *key)
325{
326 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
327}
328
329int
330mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
331 Key *key)
332{
333 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
334}
335
336int
337mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
338 char *host, Key *key)
339{
340 int ret;
341
342 key->type = KEY_RSA; /* XXX hack for key_to_blob */
343 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
344 key->type = KEY_RSA1;
345 return (ret);
346}
347
348static void
349mm_send_debug(Buffer *m)
350{
351 char *msg;
352
353 while (buffer_len(m)) {
354 msg = buffer_get_string(m, NULL);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000355 debug3("%s: Sending debug: %s", __func__, msg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356 packet_send_debug("%s", msg);
357 xfree(msg);
358 }
359}
360
361int
362mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
363{
364 Buffer m;
365 u_char *blob;
366 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100367 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000368
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000369 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370
371 /* Convert the key to a blob and the pass it over */
372 if (!key_to_blob(key, &blob, &len))
373 return (0);
374
375 buffer_init(&m);
376 buffer_put_int(&m, type);
377 buffer_put_cstring(&m, user ? user : "");
378 buffer_put_cstring(&m, host ? host : "");
379 buffer_put_string(&m, blob, len);
380 xfree(blob);
381
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000382 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000383
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000384 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000385 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000386
387 allowed = buffer_get_int(&m);
388
Damien Miller06ebedf2003-02-24 12:03:38 +1100389 /* fake forced command */
390 auth_clear_options();
391 have_forced = buffer_get_int(&m);
392 forced_command = have_forced ? xstrdup("true") : NULL;
393
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000394 /* Send potential debug messages */
395 mm_send_debug(&m);
396
397 buffer_free(&m);
398
399 return (allowed);
400}
401
402/*
403 * This key verify needs to send the key type along, because the
404 * privileged parent makes the decision if the key is allowed
405 * for authentication.
406 */
407
408int
409mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
410{
411 Buffer m;
412 u_char *blob;
413 u_int len;
414 int verified = 0;
415
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000416 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000417
418 /* Convert the key to a blob and the pass it over */
419 if (!key_to_blob(key, &blob, &len))
420 return (0);
421
422 buffer_init(&m);
423 buffer_put_string(&m, blob, len);
424 buffer_put_string(&m, sig, siglen);
425 buffer_put_string(&m, data, datalen);
426 xfree(blob);
427
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000428 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000429
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000430 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000431 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000432
433 verified = buffer_get_int(&m);
434
435 buffer_free(&m);
436
437 return (verified);
438}
439
440/* Export key state after authentication */
441Newkeys *
442mm_newkeys_from_blob(u_char *blob, int blen)
443{
444 Buffer b;
445 u_int len;
446 Newkeys *newkey = NULL;
447 Enc *enc;
448 Mac *mac;
449 Comp *comp;
450
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000451 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000452#ifdef DEBUG_PK
453 dump_base64(stderr, blob, blen);
454#endif
455 buffer_init(&b);
456 buffer_append(&b, blob, blen);
457
458 newkey = xmalloc(sizeof(*newkey));
459 enc = &newkey->enc;
460 mac = &newkey->mac;
461 comp = &newkey->comp;
462
463 /* Enc structure */
464 enc->name = buffer_get_string(&b, NULL);
465 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
466 enc->enabled = buffer_get_int(&b);
467 enc->block_size = buffer_get_int(&b);
468 enc->key = buffer_get_string(&b, &enc->key_len);
469 enc->iv = buffer_get_string(&b, &len);
470 if (len != enc->block_size)
Ben Lindstrom08512492002-06-27 00:23:02 +0000471 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000472 enc->block_size, len);
473
474 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000475 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000476 enc->name, enc->cipher);
477
478 /* Mac structure */
479 mac->name = buffer_get_string(&b, NULL);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000480 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
Damien Millere45796f2007-06-11 14:01:42 +1000481 fatal("%s: can not setup mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000482 mac->enabled = buffer_get_int(&b);
483 mac->key = buffer_get_string(&b, &len);
484 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000485 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000486 mac->key_len);
487 mac->key_len = len;
488
489 /* Comp structure */
490 comp->type = buffer_get_int(&b);
491 comp->enabled = buffer_get_int(&b);
492 comp->name = buffer_get_string(&b, NULL);
493
494 len = buffer_len(&b);
495 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000496 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000497 buffer_free(&b);
498 return (newkey);
499}
500
501int
502mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
503{
504 Buffer b;
505 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000506 Enc *enc;
507 Mac *mac;
508 Comp *comp;
509 Newkeys *newkey = newkeys[mode];
510
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000511 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000512
513 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000514 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000515 return 0;
516 }
517 enc = &newkey->enc;
518 mac = &newkey->mac;
519 comp = &newkey->comp;
520
521 buffer_init(&b);
522 /* Enc structure */
523 buffer_put_cstring(&b, enc->name);
524 /* The cipher struct is constant and shared, you export pointer */
525 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
526 buffer_put_int(&b, enc->enabled);
527 buffer_put_int(&b, enc->block_size);
528 buffer_put_string(&b, enc->key, enc->key_len);
529 packet_get_keyiv(mode, enc->iv, enc->block_size);
530 buffer_put_string(&b, enc->iv, enc->block_size);
531
532 /* Mac structure */
533 buffer_put_cstring(&b, mac->name);
534 buffer_put_int(&b, mac->enabled);
535 buffer_put_string(&b, mac->key, mac->key_len);
536
537 /* Comp structure */
538 buffer_put_int(&b, comp->type);
539 buffer_put_int(&b, comp->enabled);
540 buffer_put_cstring(&b, comp->name);
541
542 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000543 if (lenp != NULL)
544 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000545 if (blobp != NULL) {
546 *blobp = xmalloc(len);
547 memcpy(*blobp, buffer_ptr(&b), len);
548 }
549 memset(buffer_ptr(&b), 0, len);
550 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000551 return len;
552}
553
554static void
555mm_send_kex(Buffer *m, Kex *kex)
556{
557 buffer_put_string(m, kex->session_id, kex->session_id_len);
558 buffer_put_int(m, kex->we_need);
559 buffer_put_int(m, kex->hostkey_type);
560 buffer_put_int(m, kex->kex_type);
561 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
562 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
563 buffer_put_int(m, kex->flags);
564 buffer_put_cstring(m, kex->client_version_string);
565 buffer_put_cstring(m, kex->server_version_string);
566}
567
568void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000569mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000570{
571 Buffer m;
572 u_char *blob, *p;
573 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000574 u_int32_t seqnr, packets;
575 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000576
577 buffer_init(&m);
578
579 if (!compat20) {
580 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000581 u_char *key;
582 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000583
584 buffer_put_int(&m, packet_get_protocol_flags());
585
586 buffer_put_int(&m, packet_get_ssh1_cipher());
587
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000588 debug3("%s: Sending ssh1 KEY+IV", __func__);
589 keylen = packet_get_encryption_key(NULL);
590 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
591 keylen = packet_get_encryption_key(key);
592 buffer_put_string(&m, key, keylen);
593 memset(key, 0, keylen);
594 xfree(key);
595
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000596 ivlen = packet_get_keyiv_len(MODE_OUT);
597 packet_get_keyiv(MODE_OUT, iv, ivlen);
598 buffer_put_string(&m, iv, ivlen);
599 ivlen = packet_get_keyiv_len(MODE_OUT);
600 packet_get_keyiv(MODE_IN, iv, ivlen);
601 buffer_put_string(&m, iv, ivlen);
602 goto skip;
603 } else {
604 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000605 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000606 }
607
608 debug3("%s: Sending new keys: %p %p",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000609 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000610
611 /* Keys from Kex */
612 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000613 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000614
615 buffer_put_string(&m, blob, bloblen);
616 xfree(blob);
617
618 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000619 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000620
621 buffer_put_string(&m, blob, bloblen);
622 xfree(blob);
623
Damien Millera5539d22003-04-09 20:50:06 +1000624 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
625 buffer_put_int(&m, seqnr);
626 buffer_put_int64(&m, blocks);
627 buffer_put_int(&m, packets);
Damien Millerb1ecd9c2003-04-09 20:51:24 +1000628 packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
Damien Millera5539d22003-04-09 20:50:06 +1000629 buffer_put_int(&m, seqnr);
630 buffer_put_int64(&m, blocks);
631 buffer_put_int(&m, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000632
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000633 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000634 skip:
635 /* More key context */
636 plen = packet_get_keycontext(MODE_OUT, NULL);
637 p = xmalloc(plen+1);
638 packet_get_keycontext(MODE_OUT, p);
639 buffer_put_string(&m, p, plen);
640 xfree(p);
641
642 plen = packet_get_keycontext(MODE_IN, NULL);
643 p = xmalloc(plen+1);
644 packet_get_keycontext(MODE_IN, p);
645 buffer_put_string(&m, p, plen);
646 xfree(p);
647
648 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000649 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000650 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
651 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
652
653 /* Network I/O buffers */
654 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
655 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
656
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000657 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000658 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000659
660 buffer_free(&m);
661}
662
663int
Damien Miller71a73672006-03-26 14:04:36 +1100664mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000665{
666 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000667 char *p, *msg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000668 int success = 0;
669
670 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000671 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000672
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000673 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000674 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000675
676 success = buffer_get_int(&m);
677 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000678 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000679 buffer_free(&m);
680 return (0);
681 }
682 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000683 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000684 buffer_free(&m);
685
686 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
687 xfree(p);
688
Darren Tucker09991742004-07-17 17:05:14 +1000689 buffer_append(&loginmsg, msg, strlen(msg));
690 xfree(msg);
691
Damien Miller54fd7cf2007-09-17 12:04:08 +1000692 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
693 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
694 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000695
696 /* Success */
697 return (1);
698}
699
700void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000701mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000702{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000703 Buffer m;
704
705 if (s->ttyfd == -1)
706 return;
707 buffer_init(&m);
708 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000709 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000710 buffer_free(&m);
711
712 /* closed dup'ed master */
713 if (close(s->ptymaster) < 0)
714 error("close(s->ptymaster): %s", strerror(errno));
715
716 /* unlink pty from session */
717 s->ttyfd = -1;
718}
719
Damien Miller79418552002-04-23 20:28:48 +1000720#ifdef USE_PAM
721void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100722mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000723{
724 Buffer m;
725
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000726 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000727 if (!options.use_pam)
728 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000729
730 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000731 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000732
733 buffer_free(&m);
734}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000735
Damien Miller1f499fd2003-08-25 13:08:49 +1000736u_int
737mm_do_pam_account(void)
738{
739 Buffer m;
740 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000741 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000742
743 debug3("%s entering", __func__);
744 if (!options.use_pam)
745 fatal("UsePAM=no, but ended up in %s anyway", __func__);
746
747 buffer_init(&m);
748 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
749
Damien Millera8e06ce2003-11-21 23:48:55 +1100750 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000751 MONITOR_ANS_PAM_ACCOUNT, &m);
752 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000753 msg = buffer_get_string(&m, NULL);
754 buffer_append(&loginmsg, msg, strlen(msg));
755 xfree(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000756
757 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100758
Damien Miller1f499fd2003-08-25 13:08:49 +1000759 debug3("%s returning %d", __func__, ret);
760
761 return (ret);
762}
763
Damien Miller4f9f42a2003-05-10 19:28:02 +1000764void *
765mm_sshpam_init_ctx(Authctxt *authctxt)
766{
767 Buffer m;
768 int success;
769
770 debug3("%s", __func__);
771 buffer_init(&m);
772 buffer_put_cstring(&m, authctxt->user);
773 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
774 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
775 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
776 success = buffer_get_int(&m);
777 if (success == 0) {
778 debug3("%s: pam_init_ctx failed", __func__);
779 buffer_free(&m);
780 return (NULL);
781 }
782 buffer_free(&m);
783 return (authctxt);
784}
785
786int
787mm_sshpam_query(void *ctx, char **name, char **info,
788 u_int *num, char ***prompts, u_int **echo_on)
789{
790 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000791 u_int i;
792 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000793
794 debug3("%s", __func__);
795 buffer_init(&m);
796 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
797 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
798 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
799 ret = buffer_get_int(&m);
800 debug3("%s: pam_query returned %d", __func__, ret);
801 *name = buffer_get_string(&m, NULL);
802 *info = buffer_get_string(&m, NULL);
803 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000804 if (*num > PAM_MAX_NUM_MSG)
805 fatal("%s: recieved %u PAM messages, expected <= %u",
806 __func__, *num, PAM_MAX_NUM_MSG);
807 *prompts = xcalloc((*num + 1), sizeof(char *));
808 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000809 for (i = 0; i < *num; ++i) {
810 (*prompts)[i] = buffer_get_string(&m, NULL);
811 (*echo_on)[i] = buffer_get_int(&m);
812 }
813 buffer_free(&m);
814 return (ret);
815}
816
817int
818mm_sshpam_respond(void *ctx, u_int num, char **resp)
819{
820 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000821 u_int i;
822 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000823
824 debug3("%s", __func__);
825 buffer_init(&m);
826 buffer_put_int(&m, num);
827 for (i = 0; i < num; ++i)
828 buffer_put_cstring(&m, resp[i]);
829 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
830 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
831 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
832 ret = buffer_get_int(&m);
833 debug3("%s: pam_respond returned %d", __func__, ret);
834 buffer_free(&m);
835 return (ret);
836}
837
838void
839mm_sshpam_free_ctx(void *ctxtp)
840{
841 Buffer m;
842
843 debug3("%s", __func__);
844 buffer_init(&m);
845 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
846 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
847 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
848 buffer_free(&m);
849}
Damien Miller79418552002-04-23 20:28:48 +1000850#endif /* USE_PAM */
851
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000852/* Request process termination */
853
854void
855mm_terminate(void)
856{
857 Buffer m;
858
859 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000860 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000861 buffer_free(&m);
862}
863
864int
865mm_ssh1_session_key(BIGNUM *num)
866{
867 int rsafail;
868 Buffer m;
869
870 buffer_init(&m);
871 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000872 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000873
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000874 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000875
876 rsafail = buffer_get_int(&m);
877 buffer_get_bignum2(&m, num);
878
879 buffer_free(&m);
880
881 return (rsafail);
882}
883
884static void
885mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
886 char ***prompts, u_int **echo_on)
887{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000888 *name = xstrdup("");
889 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000890 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100891 *prompts = xcalloc(*numprompts, sizeof(char *));
892 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000893 (*echo_on)[0] = 0;
894}
895
896int
897mm_bsdauth_query(void *ctx, char **name, char **infotxt,
898 u_int *numprompts, char ***prompts, u_int **echo_on)
899{
900 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100901 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000902 char *challenge;
903
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000904 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000905
906 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000907 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000908
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000909 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000910 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100911 success = buffer_get_int(&m);
912 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000913 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000914 buffer_free(&m);
915 return (-1);
916 }
917
918 /* Get the challenge, and format the response */
919 challenge = buffer_get_string(&m, NULL);
920 buffer_free(&m);
921
922 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
923 (*prompts)[0] = challenge;
924
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000925 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926
927 return (0);
928}
929
930int
931mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
932{
933 Buffer m;
934 int authok;
935
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000936 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000937 if (numresponses != 1)
938 return (-1);
939
940 buffer_init(&m);
941 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000942 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000943
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000944 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000945 MONITOR_ANS_BSDAUTHRESPOND, &m);
946
947 authok = buffer_get_int(&m);
948 buffer_free(&m);
949
950 return ((authok == 0) ? -1 : 0);
951}
952
Darren Tucker042e2e82004-07-08 23:09:42 +1000953#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000954int
955mm_skey_query(void *ctx, char **name, char **infotxt,
956 u_int *numprompts, char ***prompts, u_int **echo_on)
957{
958 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100959 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +1000960 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000962 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000963
964 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000965 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000966
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000967 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000968 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100969 success = buffer_get_int(&m);
970 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000971 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972 buffer_free(&m);
973 return (-1);
974 }
975
976 /* Get the challenge, and format the response */
977 challenge = buffer_get_string(&m, NULL);
978 buffer_free(&m);
979
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000980 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000981
982 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
983
Damien Miller07d86be2006-03-26 14:19:21 +1100984 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000985 xfree(challenge);
986
987 return (0);
988}
989
990int
991mm_skey_respond(void *ctx, u_int numresponses, char **responses)
992{
993 Buffer m;
994 int authok;
995
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000996 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000997 if (numresponses != 1)
998 return (-1);
999
1000 buffer_init(&m);
1001 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001002 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001003
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001004 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005 MONITOR_ANS_SKEYRESPOND, &m);
1006
1007 authok = buffer_get_int(&m);
1008 buffer_free(&m);
1009
1010 return ((authok == 0) ? -1 : 0);
1011}
Darren Tucker042e2e82004-07-08 23:09:42 +10001012#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001013
1014void
1015mm_ssh1_session_id(u_char session_id[16])
1016{
1017 Buffer m;
1018 int i;
1019
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 for (i = 0; i < 16; i++)
1024 buffer_put_char(&m, session_id[i]);
1025
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001026 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001027 buffer_free(&m);
1028}
1029
1030int
1031mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1032{
1033 Buffer m;
1034 Key *key;
1035 u_char *blob;
1036 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001037 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001038
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001039 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001040
1041 buffer_init(&m);
1042 buffer_put_bignum2(&m, client_n);
1043
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001044 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1045 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001046
1047 allowed = buffer_get_int(&m);
1048
Damien Miller06ebedf2003-02-24 12:03:38 +11001049 /* fake forced command */
1050 auth_clear_options();
1051 have_forced = buffer_get_int(&m);
1052 forced_command = have_forced ? xstrdup("true") : NULL;
1053
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001054 if (allowed && rkey != NULL) {
1055 blob = buffer_get_string(&m, &blen);
1056 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001057 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001058 *rkey = key;
1059 xfree(blob);
1060 }
1061 mm_send_debug(&m);
1062 buffer_free(&m);
1063
1064 return (allowed);
1065}
1066
1067BIGNUM *
1068mm_auth_rsa_generate_challenge(Key *key)
1069{
1070 Buffer m;
1071 BIGNUM *challenge;
1072 u_char *blob;
1073 u_int blen;
1074
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001075 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001076
1077 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001078 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001079
1080 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1081 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001082 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001083 key->type = KEY_RSA1;
1084
1085 buffer_init(&m);
1086 buffer_put_string(&m, blob, blen);
1087 xfree(blob);
1088
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001089 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1090 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001091
1092 buffer_get_bignum2(&m, challenge);
1093 buffer_free(&m);
1094
1095 return (challenge);
1096}
1097
1098int
1099mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1100{
1101 Buffer m;
1102 u_char *blob;
1103 u_int blen;
1104 int success = 0;
1105
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001106 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001107
1108 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1109 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001110 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001111 key->type = KEY_RSA1;
1112
1113 buffer_init(&m);
1114 buffer_put_string(&m, blob, blen);
1115 buffer_put_string(&m, response, 16);
1116 xfree(blob);
1117
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001118 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1119 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001120
1121 success = buffer_get_int(&m);
1122 buffer_free(&m);
1123
1124 return (success);
1125}
Damien Miller25162f22002-09-12 09:47:29 +10001126
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001127#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001128void
1129mm_audit_event(ssh_audit_event_t event)
1130{
1131 Buffer m;
1132
1133 debug3("%s entering", __func__);
1134
1135 buffer_init(&m);
1136 buffer_put_int(&m, event);
1137
1138 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1139 buffer_free(&m);
1140}
1141
1142void
1143mm_audit_run_command(const char *command)
1144{
1145 Buffer m;
1146
1147 debug3("%s entering command %s", __func__, command);
1148
1149 buffer_init(&m);
1150 buffer_put_cstring(&m, command);
1151
1152 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1153 buffer_free(&m);
1154}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001155#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001156
Darren Tucker0efd1552003-08-26 11:49:55 +10001157#ifdef GSSAPI
1158OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001159mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001160{
1161 Buffer m;
1162 OM_uint32 major;
1163
1164 /* Client doesn't get to see the context */
1165 *ctx = NULL;
1166
1167 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001168 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001169
1170 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1171 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1172
1173 major = buffer_get_int(&m);
1174
1175 buffer_free(&m);
1176 return (major);
1177}
1178
1179OM_uint32
1180mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1181 gss_buffer_desc *out, OM_uint32 *flags)
1182{
1183 Buffer m;
1184 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001185 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001186
1187 buffer_init(&m);
1188 buffer_put_string(&m, in->value, in->length);
1189
1190 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1191 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1192
1193 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001194 out->value = buffer_get_string(&m, &len);
1195 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001196 if (flags)
1197 *flags = buffer_get_int(&m);
1198
1199 buffer_free(&m);
1200
1201 return (major);
1202}
1203
Damien Miller0425d402003-11-17 22:18:21 +11001204OM_uint32
1205mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1206{
1207 Buffer m;
1208 OM_uint32 major;
1209
1210 buffer_init(&m);
1211 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1212 buffer_put_string(&m, gssmic->value, gssmic->length);
1213
1214 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1215 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1216 &m);
1217
1218 major = buffer_get_int(&m);
1219 buffer_free(&m);
1220 return(major);
1221}
1222
Darren Tucker0efd1552003-08-26 11:49:55 +10001223int
1224mm_ssh_gssapi_userok(char *user)
1225{
1226 Buffer m;
1227 int authenticated = 0;
1228
1229 buffer_init(&m);
1230
1231 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1232 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1233 &m);
1234
1235 authenticated = buffer_get_int(&m);
1236
1237 buffer_free(&m);
1238 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1239 return (authenticated);
1240}
1241#endif /* GSSAPI */