blob: e6217b3d42f6130de6849096496504d84281cdd9 [file] [log] [blame]
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001/* $OpenBSD: monitor_wrap.c,v 1.85 2015/05/01 03:23:51 djm 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 Miller1f0311c2014-05-15 14:24:09 +100041#ifdef WITH_OPENSSL
Damien Millerd7834352006-08-05 12:39:39 +100042#include <openssl/bn.h>
43#include <openssl/dh.h>
Damien Miller01ed2272008-11-05 16:20:46 +110044#include <openssl/evp.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100045#endif
Damien Millerd7834352006-08-05 12:39:39 +100046
Damien Millerb84886b2008-05-19 15:05:07 +100047#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100048#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000049#include "ssh.h"
Damien Miller1f0311c2014-05-15 14:24:09 +100050#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000051#include "dh.h"
Damien Miller1f0311c2014-05-15 14:24:09 +100052#endif
Damien Millerd7834352006-08-05 12:39:39 +100053#include "buffer.h"
54#include "key.h"
55#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000056#include "kex.h"
Damien Millerd7834352006-08-05 12:39:39 +100057#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000058#include "auth.h"
Damien Miller06ebedf2003-02-24 12:03:38 +110059#include "auth-options.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000060#include "packet.h"
61#include "mac.h"
62#include "log.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000063#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
64#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000065#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000066#define TARGET_OS_MAC 1
67#else
68#include "zlib.h"
69#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#ifdef GSSAPI
72#include "ssh-gss.h"
73#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000074#include "monitor_wrap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000075#include "atomicio.h"
76#include "monitor_fdpass.h"
Damien Miller3f941882006-03-31 23:13:02 +110077#include "misc.h"
Damien Millereb8b60e2010-08-31 22:41:14 +100078#include "uuencode.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000079
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000080#include "channels.h"
81#include "session.h"
Darren Tucker1629c072007-02-19 22:25:37 +110082#include "servconf.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +100083#include "roaming.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000084
markus@openbsd.org091c3022015-01-19 19:52:16 +000085#include "ssherr.h"
86
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000087/* Imports */
88extern int compat20;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000089extern z_stream incoming_stream;
90extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000091extern struct monitor *pmonitor;
Darren Tucker09991742004-07-17 17:05:14 +100092extern Buffer loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100093extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000094
Damien Miller8f0bf232011-06-20 14:42:23 +100095void
96mm_log_handler(LogLevel level, const char *msg, void *ctx)
97{
98 Buffer log_msg;
99 struct monitor *mon = (struct monitor *)ctx;
100
101 if (mon->m_log_sendfd == -1)
102 fatal("%s: no log channel", __func__);
103
104 buffer_init(&log_msg);
105 /*
106 * Placeholder for packet length. Will be filled in with the actual
107 * packet length once the packet has been constucted. This saves
108 * fragile math.
109 */
110 buffer_put_int(&log_msg, 0);
111
112 buffer_put_int(&log_msg, level);
113 buffer_put_cstring(&log_msg, msg);
114 put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4);
115 if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg),
116 buffer_len(&log_msg)) != buffer_len(&log_msg))
117 fatal("%s: write: %s", __func__, strerror(errno));
118 buffer_free(&log_msg);
119}
120
Darren Tucker3e33cec2003-10-02 16:12:36 +1000121int
122mm_is_monitor(void)
123{
124 /*
125 * m_pid is only set in the privileged part, and
126 * points to the unprivileged child.
127 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +1100128 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000129}
130
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000131void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000132mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000133{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000134 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +0000135 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000136
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000137 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000138
Damien Miller3f941882006-03-31 23:13:02 +1100139 put_u32(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000140 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000141 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +1000142 fatal("%s: write: %s", __func__, strerror(errno));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000143 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +1000144 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000145}
146
147void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000148mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000149{
150 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000151 u_int msg_len;
152
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000153 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000154
Damien Millerb253cc42005-05-26 12:23:44 +1000155 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
Damien Miller4d24b3b2015-03-20 09:11:59 +1100156 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000157 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000158 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000159 }
Damien Miller3f941882006-03-31 23:13:02 +1100160 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000161 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000162 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000163 buffer_clear(m);
164 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000165 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
166 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000167}
168
169void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000170mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171{
172 u_char rtype;
173
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000174 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000175
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000176 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000177 rtype = buffer_get_char(m);
178 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000179 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000180 rtype, type);
181}
182
Damien Miller1f0311c2014-05-15 14:24:09 +1000183#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000184DH *
185mm_choose_dh(int min, int nbits, int max)
186{
187 BIGNUM *p, *g;
188 int success = 0;
189 Buffer m;
190
191 buffer_init(&m);
192 buffer_put_int(&m, min);
193 buffer_put_int(&m, nbits);
194 buffer_put_int(&m, max);
195
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000196 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000197
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000198 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000199 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000200
201 success = buffer_get_char(&m);
202 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000203 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000204
205 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000206 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000207 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000208 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000209 buffer_get_bignum2(&m, p);
210 buffer_get_bignum2(&m, g);
211
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000212 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000213 buffer_free(&m);
214
215 return (dh_new_group(g, p));
216}
Damien Miller1f0311c2014-05-15 14:24:09 +1000217#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000218
219int
djm@openbsd.org523463a2015-02-16 22:13:32 +0000220mm_key_sign(Key *key, u_char **sigp, u_int *lenp,
221 const u_char *data, u_int datalen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000222{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000223 struct kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000224 Buffer m;
225
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000226 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000227
228 buffer_init(&m);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000229 buffer_put_int(&m, kex->host_key_index(key, 0, active_state));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000230 buffer_put_string(&m, data, datalen);
231
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000232 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000233
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000234 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000235 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000236 *sigp = buffer_get_string(&m, lenp);
237 buffer_free(&m);
238
239 return (0);
240}
241
242struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000243mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000244{
245 Buffer m;
246 struct passwd *pw;
Damien Millerd8478b62011-05-29 21:39:36 +1000247 u_int len, i;
Darren Tucker1629c072007-02-19 22:25:37 +1100248 ServerOptions *newopts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000249
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000250 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000251
252 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000253 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000254
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000255 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000256
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000257 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000258 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000259
260 if (buffer_get_char(&m) == 0) {
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100261 pw = NULL;
262 goto out;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000263 }
Darren Tucker1629c072007-02-19 22:25:37 +1100264 pw = buffer_get_string(&m, &len);
265 if (len != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000266 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000267 pw->pw_name = buffer_get_string(&m, NULL);
268 pw->pw_passwd = buffer_get_string(&m, NULL);
Damien Miller6332da22013-04-23 14:25:52 +1000269#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000270 pw->pw_gecos = buffer_get_string(&m, NULL);
Damien Miller6332da22013-04-23 14:25:52 +1000271#endif
272#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000273 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000274#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000275 pw->pw_dir = buffer_get_string(&m, NULL);
276 pw->pw_shell = buffer_get_string(&m, NULL);
Darren Tucker1629c072007-02-19 22:25:37 +1100277
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100278out:
Darren Tucker1629c072007-02-19 22:25:37 +1100279 /* copy options block as a Match directive may have changed some */
280 newopts = buffer_get_string(&m, &len);
281 if (len != sizeof(*newopts))
282 fatal("%s: option block size mismatch", __func__);
Damien Millerf2e407e2011-05-20 19:04:14 +1000283
284#define M_CP_STROPT(x) do { \
285 if (newopts->x != NULL) \
286 newopts->x = buffer_get_string(&m, NULL); \
287 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000288#define M_CP_STRARRAYOPT(x, nx) do { \
289 for (i = 0; i < newopts->nx; i++) \
290 newopts->x[i] = buffer_get_string(&m, NULL); \
291 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000292 /* See comment in servconf.h */
293 COPY_MATCH_STRING_OPTS();
294#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000295#undef M_CP_STRARRAYOPT
Damien Millerf2e407e2011-05-20 19:04:14 +1000296
Darren Tucker1629c072007-02-19 22:25:37 +1100297 copy_set_server_options(&options, newopts, 1);
Darren Tuckera627d422013-06-02 07:31:17 +1000298 free(newopts);
Darren Tucker1629c072007-02-19 22:25:37 +1100299
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000300 buffer_free(&m);
301
302 return (pw);
303}
304
Darren Tucker7eb3de02003-10-15 15:56:58 +1000305char *
306mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000307{
308 Buffer m;
309 char *banner;
310
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000311 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000312
313 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000314 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000315 buffer_clear(&m);
316
Darren Tucker7eb3de02003-10-15 15:56:58 +1000317 mm_request_receive_expect(pmonitor->m_recvfd,
318 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000319 banner = buffer_get_string(&m, NULL);
320 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000321
Darren Tucker7eb3de02003-10-15 15:56:58 +1000322 /* treat empty banner as missing banner */
323 if (strlen(banner) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000324 free(banner);
Darren Tucker7eb3de02003-10-15 15:56:58 +1000325 banner = NULL;
326 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000327 return (banner);
328}
329
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000330/* Inform the privileged process about service and style */
331
332void
333mm_inform_authserv(char *service, char *style)
334{
335 Buffer m;
336
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000337 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000338
339 buffer_init(&m);
340 buffer_put_cstring(&m, service);
341 buffer_put_cstring(&m, style ? style : "");
342
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000343 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000344
345 buffer_free(&m);
346}
347
348/* Do the password authentication */
349int
350mm_auth_password(Authctxt *authctxt, char *password)
351{
352 Buffer m;
353 int authenticated = 0;
354
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000355 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356
357 buffer_init(&m);
358 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000359 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000360
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000361 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000362 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000363
364 authenticated = buffer_get_int(&m);
365
366 buffer_free(&m);
367
368 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000369 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370 return (authenticated);
371}
372
373int
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000374mm_user_key_allowed(struct passwd *pw, Key *key, int pubkey_auth_attempt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000375{
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000376 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
377 pubkey_auth_attempt));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000378}
379
380int
381mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
382 Key *key)
383{
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000384 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000385}
386
387int
388mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
389 char *host, Key *key)
390{
391 int ret;
392
393 key->type = KEY_RSA; /* XXX hack for key_to_blob */
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000394 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000395 key->type = KEY_RSA1;
396 return (ret);
397}
398
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000399int
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000400mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key,
401 int pubkey_auth_attempt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000402{
403 Buffer m;
404 u_char *blob;
405 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100406 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000407
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000408 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000409
410 /* Convert the key to a blob and the pass it over */
411 if (!key_to_blob(key, &blob, &len))
412 return (0);
413
414 buffer_init(&m);
415 buffer_put_int(&m, type);
416 buffer_put_cstring(&m, user ? user : "");
417 buffer_put_cstring(&m, host ? host : "");
418 buffer_put_string(&m, blob, len);
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000419 buffer_put_int(&m, pubkey_auth_attempt);
Darren Tuckera627d422013-06-02 07:31:17 +1000420 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000421
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000422 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000423
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000424 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000425 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000426
427 allowed = buffer_get_int(&m);
428
Damien Miller06ebedf2003-02-24 12:03:38 +1100429 /* fake forced command */
430 auth_clear_options();
431 have_forced = buffer_get_int(&m);
432 forced_command = have_forced ? xstrdup("true") : NULL;
433
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000434 buffer_free(&m);
435
436 return (allowed);
437}
438
439/*
440 * This key verify needs to send the key type along, because the
441 * privileged parent makes the decision if the key is allowed
442 * for authentication.
443 */
444
445int
446mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
447{
448 Buffer m;
449 u_char *blob;
450 u_int len;
451 int verified = 0;
452
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000453 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000454
455 /* Convert the key to a blob and the pass it over */
456 if (!key_to_blob(key, &blob, &len))
457 return (0);
458
459 buffer_init(&m);
460 buffer_put_string(&m, blob, len);
461 buffer_put_string(&m, sig, siglen);
462 buffer_put_string(&m, data, datalen);
Darren Tuckera627d422013-06-02 07:31:17 +1000463 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000464
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000465 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000466
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000467 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000468 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000469
470 verified = buffer_get_int(&m);
471
472 buffer_free(&m);
473
474 return (verified);
475}
476
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000477void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000478mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000479{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000480 struct ssh *ssh = active_state; /* XXX */
481 struct sshbuf *m;
482 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000483
markus@openbsd.org091c3022015-01-19 19:52:16 +0000484 if ((m = sshbuf_new()) == NULL)
485 fatal("%s: sshbuf_new failed", __func__);
486 if ((r = ssh_packet_get_state(ssh, m)) != 0)
487 fatal("%s: get_state failed: %s",
488 __func__, ssh_err(r));
489 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000490 debug3("%s: Finished sending state", __func__);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000491 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000492}
493
494int
Damien Miller71a73672006-03-26 14:04:36 +1100495mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000496{
497 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000498 char *p, *msg;
Damien Miller7207f642008-05-19 15:34:50 +1000499 int success = 0, tmp1 = -1, tmp2 = -1;
500
501 /* Kludge: ensure there are fds free to receive the pty/tty */
502 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
503 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
504 error("%s: cannot allocate fds for pty", __func__);
505 if (tmp1 > 0)
506 close(tmp1);
507 if (tmp2 > 0)
508 close(tmp2);
509 return 0;
510 }
511 close(tmp1);
512 close(tmp2);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000513
514 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000515 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000516
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000517 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000518 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000519
520 success = buffer_get_int(&m);
521 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000522 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523 buffer_free(&m);
524 return (0);
525 }
526 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000527 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000528 buffer_free(&m);
529
530 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
Darren Tuckera627d422013-06-02 07:31:17 +1000531 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000532
Darren Tucker09991742004-07-17 17:05:14 +1000533 buffer_append(&loginmsg, msg, strlen(msg));
Darren Tuckera627d422013-06-02 07:31:17 +1000534 free(msg);
Darren Tucker09991742004-07-17 17:05:14 +1000535
Damien Miller54fd7cf2007-09-17 12:04:08 +1000536 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
537 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
538 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000539
540 /* Success */
541 return (1);
542}
543
544void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000545mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000546{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000547 Buffer m;
548
549 if (s->ttyfd == -1)
550 return;
551 buffer_init(&m);
552 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000553 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000554 buffer_free(&m);
555
556 /* closed dup'ed master */
Damien Miller7207f642008-05-19 15:34:50 +1000557 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
558 error("close(s->ptymaster/%d): %s",
559 s->ptymaster, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000560
561 /* unlink pty from session */
562 s->ttyfd = -1;
563}
564
Damien Miller79418552002-04-23 20:28:48 +1000565#ifdef USE_PAM
566void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100567mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000568{
569 Buffer m;
570
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000571 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000572 if (!options.use_pam)
573 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000574
575 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000576 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000577
578 buffer_free(&m);
579}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000580
Damien Miller1f499fd2003-08-25 13:08:49 +1000581u_int
582mm_do_pam_account(void)
583{
584 Buffer m;
585 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000586 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000587
588 debug3("%s entering", __func__);
589 if (!options.use_pam)
590 fatal("UsePAM=no, but ended up in %s anyway", __func__);
591
592 buffer_init(&m);
593 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
594
Damien Millera8e06ce2003-11-21 23:48:55 +1100595 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000596 MONITOR_ANS_PAM_ACCOUNT, &m);
597 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000598 msg = buffer_get_string(&m, NULL);
599 buffer_append(&loginmsg, msg, strlen(msg));
Darren Tuckerf60845f2013-06-02 08:07:31 +1000600 free(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000601
602 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100603
Damien Miller1f499fd2003-08-25 13:08:49 +1000604 debug3("%s returning %d", __func__, ret);
605
606 return (ret);
607}
608
Damien Miller4f9f42a2003-05-10 19:28:02 +1000609void *
610mm_sshpam_init_ctx(Authctxt *authctxt)
611{
612 Buffer m;
613 int success;
614
615 debug3("%s", __func__);
616 buffer_init(&m);
617 buffer_put_cstring(&m, authctxt->user);
618 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
619 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
620 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
621 success = buffer_get_int(&m);
622 if (success == 0) {
623 debug3("%s: pam_init_ctx failed", __func__);
624 buffer_free(&m);
625 return (NULL);
626 }
627 buffer_free(&m);
628 return (authctxt);
629}
630
631int
632mm_sshpam_query(void *ctx, char **name, char **info,
633 u_int *num, char ***prompts, u_int **echo_on)
634{
635 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000636 u_int i;
637 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000638
639 debug3("%s", __func__);
640 buffer_init(&m);
641 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
642 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
643 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
644 ret = buffer_get_int(&m);
645 debug3("%s: pam_query returned %d", __func__, ret);
646 *name = buffer_get_string(&m, NULL);
647 *info = buffer_get_string(&m, NULL);
648 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000649 if (*num > PAM_MAX_NUM_MSG)
650 fatal("%s: recieved %u PAM messages, expected <= %u",
651 __func__, *num, PAM_MAX_NUM_MSG);
652 *prompts = xcalloc((*num + 1), sizeof(char *));
653 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000654 for (i = 0; i < *num; ++i) {
655 (*prompts)[i] = buffer_get_string(&m, NULL);
656 (*echo_on)[i] = buffer_get_int(&m);
657 }
658 buffer_free(&m);
659 return (ret);
660}
661
662int
663mm_sshpam_respond(void *ctx, u_int num, char **resp)
664{
665 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000666 u_int i;
667 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000668
669 debug3("%s", __func__);
670 buffer_init(&m);
671 buffer_put_int(&m, num);
672 for (i = 0; i < num; ++i)
673 buffer_put_cstring(&m, resp[i]);
674 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
675 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
676 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
677 ret = buffer_get_int(&m);
678 debug3("%s: pam_respond returned %d", __func__, ret);
679 buffer_free(&m);
680 return (ret);
681}
682
683void
684mm_sshpam_free_ctx(void *ctxtp)
685{
686 Buffer m;
687
688 debug3("%s", __func__);
689 buffer_init(&m);
690 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
691 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
692 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
693 buffer_free(&m);
694}
Damien Miller79418552002-04-23 20:28:48 +1000695#endif /* USE_PAM */
696
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000697/* Request process termination */
698
699void
700mm_terminate(void)
701{
702 Buffer m;
703
704 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000705 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000706 buffer_free(&m);
707}
708
Damien Miller1f0311c2014-05-15 14:24:09 +1000709#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000710int
711mm_ssh1_session_key(BIGNUM *num)
712{
713 int rsafail;
714 Buffer m;
715
716 buffer_init(&m);
717 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000718 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000719
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000720 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000721
722 rsafail = buffer_get_int(&m);
723 buffer_get_bignum2(&m, num);
724
725 buffer_free(&m);
726
727 return (rsafail);
728}
Damien Miller1f0311c2014-05-15 14:24:09 +1000729#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000730
731static void
732mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
733 char ***prompts, u_int **echo_on)
734{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000735 *name = xstrdup("");
736 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000737 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100738 *prompts = xcalloc(*numprompts, sizeof(char *));
739 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000740 (*echo_on)[0] = 0;
741}
742
743int
744mm_bsdauth_query(void *ctx, char **name, char **infotxt,
745 u_int *numprompts, char ***prompts, u_int **echo_on)
746{
747 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100748 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000749 char *challenge;
750
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000751 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000752
753 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000754 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000755
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000756 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000757 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100758 success = buffer_get_int(&m);
759 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000760 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000761 buffer_free(&m);
762 return (-1);
763 }
764
765 /* Get the challenge, and format the response */
766 challenge = buffer_get_string(&m, NULL);
767 buffer_free(&m);
768
769 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
770 (*prompts)[0] = challenge;
771
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000772 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000773
774 return (0);
775}
776
777int
778mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
779{
780 Buffer m;
781 int authok;
782
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000783 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000784 if (numresponses != 1)
785 return (-1);
786
787 buffer_init(&m);
788 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000789 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000790
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000791 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000792 MONITOR_ANS_BSDAUTHRESPOND, &m);
793
794 authok = buffer_get_int(&m);
795 buffer_free(&m);
796
797 return ((authok == 0) ? -1 : 0);
798}
799
Darren Tucker042e2e82004-07-08 23:09:42 +1000800#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000801int
802mm_skey_query(void *ctx, char **name, char **infotxt,
803 u_int *numprompts, char ***prompts, u_int **echo_on)
804{
805 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100806 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +1000807 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000808
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000809 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000810
811 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000812 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000813
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000814 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000815 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100816 success = buffer_get_int(&m);
817 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000818 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000819 buffer_free(&m);
820 return (-1);
821 }
822
823 /* Get the challenge, and format the response */
824 challenge = buffer_get_string(&m, NULL);
825 buffer_free(&m);
826
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000827 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000828
829 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
830
Damien Miller07d86be2006-03-26 14:19:21 +1100831 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Darren Tuckerf60845f2013-06-02 08:07:31 +1000832 free(challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000833
834 return (0);
835}
836
837int
838mm_skey_respond(void *ctx, u_int numresponses, char **responses)
839{
840 Buffer m;
841 int authok;
842
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000843 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000844 if (numresponses != 1)
845 return (-1);
846
847 buffer_init(&m);
848 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000849 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000850
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000851 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000852 MONITOR_ANS_SKEYRESPOND, &m);
853
854 authok = buffer_get_int(&m);
855 buffer_free(&m);
856
857 return ((authok == 0) ? -1 : 0);
858}
Darren Tucker042e2e82004-07-08 23:09:42 +1000859#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000860
861void
862mm_ssh1_session_id(u_char session_id[16])
863{
864 Buffer m;
865 int i;
866
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000867 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000868
869 buffer_init(&m);
870 for (i = 0; i < 16; i++)
871 buffer_put_char(&m, session_id[i]);
872
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000873 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000874 buffer_free(&m);
875}
876
Damien Miller1f0311c2014-05-15 14:24:09 +1000877#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000878int
879mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
880{
881 Buffer m;
882 Key *key;
883 u_char *blob;
884 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +1100885 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000886
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000887 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000888
889 buffer_init(&m);
890 buffer_put_bignum2(&m, client_n);
891
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000892 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
893 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000894
895 allowed = buffer_get_int(&m);
896
Damien Miller06ebedf2003-02-24 12:03:38 +1100897 /* fake forced command */
898 auth_clear_options();
899 have_forced = buffer_get_int(&m);
900 forced_command = have_forced ? xstrdup("true") : NULL;
901
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000902 if (allowed && rkey != NULL) {
903 blob = buffer_get_string(&m, &blen);
904 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000905 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000906 *rkey = key;
Darren Tuckera627d422013-06-02 07:31:17 +1000907 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000908 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909 buffer_free(&m);
910
911 return (allowed);
912}
913
914BIGNUM *
915mm_auth_rsa_generate_challenge(Key *key)
916{
917 Buffer m;
918 BIGNUM *challenge;
919 u_char *blob;
920 u_int blen;
921
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000922 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000923
924 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000925 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926
927 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
928 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000929 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000930 key->type = KEY_RSA1;
931
932 buffer_init(&m);
933 buffer_put_string(&m, blob, blen);
Darren Tuckera627d422013-06-02 07:31:17 +1000934 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000935
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000936 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
937 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000938
939 buffer_get_bignum2(&m, challenge);
940 buffer_free(&m);
941
942 return (challenge);
943}
944
945int
946mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
947{
948 Buffer m;
949 u_char *blob;
950 u_int blen;
951 int success = 0;
952
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000953 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000954
955 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
956 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000957 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000958 key->type = KEY_RSA1;
959
960 buffer_init(&m);
961 buffer_put_string(&m, blob, blen);
962 buffer_put_string(&m, response, 16);
Darren Tuckera627d422013-06-02 07:31:17 +1000963 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000964
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000965 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
966 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000967
968 success = buffer_get_int(&m);
969 buffer_free(&m);
970
971 return (success);
972}
Damien Miller1f0311c2014-05-15 14:24:09 +1000973#endif
Damien Miller25162f22002-09-12 09:47:29 +1000974
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100975#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100976void
977mm_audit_event(ssh_audit_event_t event)
978{
979 Buffer m;
980
981 debug3("%s entering", __func__);
982
983 buffer_init(&m);
984 buffer_put_int(&m, event);
985
986 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
987 buffer_free(&m);
988}
989
990void
991mm_audit_run_command(const char *command)
992{
993 Buffer m;
994
995 debug3("%s entering command %s", __func__, command);
996
997 buffer_init(&m);
998 buffer_put_cstring(&m, command);
999
1000 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1001 buffer_free(&m);
1002}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001003#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001004
Darren Tucker0efd1552003-08-26 11:49:55 +10001005#ifdef GSSAPI
1006OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001007mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001008{
1009 Buffer m;
1010 OM_uint32 major;
1011
1012 /* Client doesn't get to see the context */
1013 *ctx = NULL;
1014
1015 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001016 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001017
1018 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1019 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1020
1021 major = buffer_get_int(&m);
1022
1023 buffer_free(&m);
1024 return (major);
1025}
1026
1027OM_uint32
1028mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1029 gss_buffer_desc *out, OM_uint32 *flags)
1030{
1031 Buffer m;
1032 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001033 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001034
1035 buffer_init(&m);
1036 buffer_put_string(&m, in->value, in->length);
1037
1038 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1039 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1040
1041 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001042 out->value = buffer_get_string(&m, &len);
1043 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001044 if (flags)
1045 *flags = buffer_get_int(&m);
1046
1047 buffer_free(&m);
1048
1049 return (major);
1050}
1051
Damien Miller0425d402003-11-17 22:18:21 +11001052OM_uint32
1053mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1054{
1055 Buffer m;
1056 OM_uint32 major;
1057
1058 buffer_init(&m);
1059 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1060 buffer_put_string(&m, gssmic->value, gssmic->length);
1061
1062 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1063 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1064 &m);
1065
1066 major = buffer_get_int(&m);
1067 buffer_free(&m);
1068 return(major);
1069}
1070
Darren Tucker0efd1552003-08-26 11:49:55 +10001071int
1072mm_ssh_gssapi_userok(char *user)
1073{
1074 Buffer m;
1075 int authenticated = 0;
1076
1077 buffer_init(&m);
1078
1079 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1080 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1081 &m);
1082
1083 authenticated = buffer_get_int(&m);
1084
1085 buffer_free(&m);
1086 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1087 return (authenticated);
1088}
1089#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001090