blob: cce318bc574629de8b39413d275945d6c9bde30a [file] [log] [blame]
markus@openbsd.org25aae352018-01-08 15:14:44 +00001/* $OpenBSD: monitor_wrap.c,v 1.98 2018/01/08 15:14:44 markus 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"
Darren Tucker01558b72016-07-18 09:33:25 +100063#include "auth-pam.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000064#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
65#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000066#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000067#define TARGET_OS_MAC 1
68#else
69#include "zlib.h"
70#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000071#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +100072#ifdef GSSAPI
73#include "ssh-gss.h"
74#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000075#include "monitor_wrap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000076#include "atomicio.h"
77#include "monitor_fdpass.h"
Damien Miller3f941882006-03-31 23:13:02 +110078#include "misc.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"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000083
markus@openbsd.org091c3022015-01-19 19:52:16 +000084#include "ssherr.h"
85
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000086/* Imports */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000087extern z_stream incoming_stream;
88extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000089extern struct monitor *pmonitor;
Darren Tucker09991742004-07-17 17:05:14 +100090extern Buffer loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100091extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000092
Damien Miller8f0bf232011-06-20 14:42:23 +100093void
94mm_log_handler(LogLevel level, const char *msg, void *ctx)
95{
96 Buffer log_msg;
97 struct monitor *mon = (struct monitor *)ctx;
98
99 if (mon->m_log_sendfd == -1)
100 fatal("%s: no log channel", __func__);
101
102 buffer_init(&log_msg);
103 /*
104 * Placeholder for packet length. Will be filled in with the actual
105 * packet length once the packet has been constucted. This saves
106 * fragile math.
107 */
108 buffer_put_int(&log_msg, 0);
109
110 buffer_put_int(&log_msg, level);
111 buffer_put_cstring(&log_msg, msg);
112 put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4);
113 if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg),
114 buffer_len(&log_msg)) != buffer_len(&log_msg))
115 fatal("%s: write: %s", __func__, strerror(errno));
116 buffer_free(&log_msg);
117}
118
Darren Tucker3e33cec2003-10-02 16:12:36 +1000119int
120mm_is_monitor(void)
121{
122 /*
123 * m_pid is only set in the privileged part, and
124 * points to the unprivileged child.
125 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +1100126 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000127}
128
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000129void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000130mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000131{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000132 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +0000133 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000134
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000135 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000136
Damien Miller3f941882006-03-31 23:13:02 +1100137 put_u32(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000138 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000139 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +1000140 fatal("%s: write: %s", __func__, strerror(errno));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000141 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +1000142 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000143}
144
145void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000146mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000147{
148 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000149 u_int msg_len;
150
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000151 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000152
Damien Millerb253cc42005-05-26 12:23:44 +1000153 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
Damien Miller4d24b3b2015-03-20 09:11:59 +1100154 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000155 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000156 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000157 }
Damien Miller3f941882006-03-31 23:13:02 +1100158 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000159 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000160 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000161 buffer_clear(m);
162 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000163 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
164 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000165}
166
167void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000168mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000169{
170 u_char rtype;
171
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000172 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000173
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000174 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000175 rtype = buffer_get_char(m);
176 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000177 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000178 rtype, type);
179}
180
Damien Miller1f0311c2014-05-15 14:24:09 +1000181#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000182DH *
183mm_choose_dh(int min, int nbits, int max)
184{
185 BIGNUM *p, *g;
186 int success = 0;
187 Buffer m;
188
189 buffer_init(&m);
190 buffer_put_int(&m, min);
191 buffer_put_int(&m, nbits);
192 buffer_put_int(&m, max);
193
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000194 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000195
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000196 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000197 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000198
199 success = buffer_get_char(&m);
200 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000201 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202
203 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000204 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000205 if ((g = 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 buffer_get_bignum2(&m, p);
208 buffer_get_bignum2(&m, g);
209
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000210 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000211 buffer_free(&m);
212
213 return (dh_new_group(g, p));
214}
Damien Miller1f0311c2014-05-15 14:24:09 +1000215#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000216
217int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000218mm_key_sign(struct sshkey *key, u_char **sigp, u_int *lenp,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000219 const u_char *data, u_int datalen, const char *hostkey_alg)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000220{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000221 struct kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000222 Buffer m;
223
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000224 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000225
226 buffer_init(&m);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000227 buffer_put_int(&m, kex->host_key_index(key, 0, active_state));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000228 buffer_put_string(&m, data, datalen);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000229 buffer_put_cstring(&m, hostkey_alg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000230
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000231 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000232
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000233 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000234 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000235 *sigp = buffer_get_string(&m, lenp);
236 buffer_free(&m);
237
238 return (0);
239}
240
241struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000242mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000243{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000244 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000245 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 { \
djm@openbsd.org66bf74a2017-10-02 19:33:20 +0000289 newopts->x = newopts->nx == 0 ? \
290 NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000291 for (i = 0; i < newopts->nx; i++) \
292 newopts->x[i] = buffer_get_string(&m, NULL); \
djm@openbsd.org66bf74a2017-10-02 19:33:20 +0000293 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000294 /* See comment in servconf.h */
295 COPY_MATCH_STRING_OPTS();
296#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000297#undef M_CP_STRARRAYOPT
Damien Millerf2e407e2011-05-20 19:04:14 +1000298
Darren Tucker1629c072007-02-19 22:25:37 +1100299 copy_set_server_options(&options, newopts, 1);
djm@openbsd.org54cd41a2017-05-17 01:24:17 +0000300 log_change_level(options.log_level);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000301 process_permitopen(ssh, &options);
Darren Tuckera627d422013-06-02 07:31:17 +1000302 free(newopts);
Darren Tucker1629c072007-02-19 22:25:37 +1100303
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000304 buffer_free(&m);
305
306 return (pw);
307}
308
Darren Tucker7eb3de02003-10-15 15:56:58 +1000309char *
310mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000311{
312 Buffer m;
313 char *banner;
314
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000315 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000316
317 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000318 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000319 buffer_clear(&m);
320
Darren Tucker7eb3de02003-10-15 15:56:58 +1000321 mm_request_receive_expect(pmonitor->m_recvfd,
322 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000323 banner = buffer_get_string(&m, NULL);
324 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000325
Darren Tucker7eb3de02003-10-15 15:56:58 +1000326 /* treat empty banner as missing banner */
327 if (strlen(banner) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000328 free(banner);
Darren Tucker7eb3de02003-10-15 15:56:58 +1000329 banner = NULL;
330 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000331 return (banner);
332}
333
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000334/* Inform the privileged process about service and style */
335
336void
337mm_inform_authserv(char *service, char *style)
338{
339 Buffer m;
340
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000341 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000342
343 buffer_init(&m);
344 buffer_put_cstring(&m, service);
345 buffer_put_cstring(&m, style ? style : "");
346
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000347 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348
349 buffer_free(&m);
350}
351
352/* Do the password authentication */
353int
354mm_auth_password(Authctxt *authctxt, char *password)
355{
356 Buffer m;
357 int authenticated = 0;
358
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000359 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000360
361 buffer_init(&m);
362 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000363 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000364
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000365 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000366 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000367
368 authenticated = buffer_get_int(&m);
Darren Tucker01558b72016-07-18 09:33:25 +1000369#ifdef USE_PAM
370 sshpam_set_maxtries_reached(buffer_get_int(&m));
371#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372
373 buffer_free(&m);
374
375 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000376 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000377 return (authenticated);
378}
379
380int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000381mm_user_key_allowed(struct passwd *pw, struct sshkey *key,
382 int pubkey_auth_attempt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000383{
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000384 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
385 pubkey_auth_attempt));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000386}
387
388int
djm@openbsd.org95767262016-03-07 19:02:43 +0000389mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host,
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000390 struct sshkey *key)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000391{
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000392 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000393}
394
395int
djm@openbsd.org95767262016-03-07 19:02:43 +0000396mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000397 struct sshkey *key, int pubkey_auth_attempt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000398{
399 Buffer m;
400 u_char *blob;
401 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100402 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000403
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000404 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000405
406 /* Convert the key to a blob and the pass it over */
407 if (!key_to_blob(key, &blob, &len))
408 return (0);
409
410 buffer_init(&m);
411 buffer_put_int(&m, type);
412 buffer_put_cstring(&m, user ? user : "");
413 buffer_put_cstring(&m, host ? host : "");
414 buffer_put_string(&m, blob, len);
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000415 buffer_put_int(&m, pubkey_auth_attempt);
Darren Tuckera627d422013-06-02 07:31:17 +1000416 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000417
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000418 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000419
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000420 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000421 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000422
423 allowed = buffer_get_int(&m);
424
Damien Miller06ebedf2003-02-24 12:03:38 +1100425 /* fake forced command */
426 auth_clear_options();
427 have_forced = buffer_get_int(&m);
428 forced_command = have_forced ? xstrdup("true") : NULL;
429
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000430 buffer_free(&m);
431
432 return (allowed);
433}
434
435/*
436 * This key verify needs to send the key type along, because the
437 * privileged parent makes the decision if the key is allowed
438 * for authentication.
439 */
440
441int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000442mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000443 const u_char *data, size_t datalen, const char *sigalg, u_int compat)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000444{
445 Buffer m;
446 u_char *blob;
447 u_int len;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000448 u_int encoded_ret = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000449
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000450 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000451
452 /* Convert the key to a blob and the pass it over */
453 if (!key_to_blob(key, &blob, &len))
454 return (0);
455
456 buffer_init(&m);
457 buffer_put_string(&m, blob, len);
458 buffer_put_string(&m, sig, siglen);
459 buffer_put_string(&m, data, datalen);
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +0000460 buffer_put_cstring(&m, sigalg == NULL ? "" : sigalg);
Darren Tuckera627d422013-06-02 07:31:17 +1000461 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000462
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000463 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000464
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000465 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000466 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000467
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000468 encoded_ret = buffer_get_int(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000469
470 buffer_free(&m);
471
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000472 if (encoded_ret != 0)
473 return SSH_ERR_SIGNATURE_INVALID;
474 return 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000475}
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);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000617 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
618 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
619 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
620 success = buffer_get_int(&m);
621 if (success == 0) {
622 debug3("%s: pam_init_ctx failed", __func__);
623 buffer_free(&m);
624 return (NULL);
625 }
626 buffer_free(&m);
627 return (authctxt);
628}
629
630int
631mm_sshpam_query(void *ctx, char **name, char **info,
632 u_int *num, char ***prompts, u_int **echo_on)
633{
634 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000635 u_int i;
636 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000637
638 debug3("%s", __func__);
639 buffer_init(&m);
640 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
641 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
642 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
643 ret = buffer_get_int(&m);
644 debug3("%s: pam_query returned %d", __func__, ret);
645 *name = buffer_get_string(&m, NULL);
646 *info = buffer_get_string(&m, NULL);
Darren Tucker01558b72016-07-18 09:33:25 +1000647 sshpam_set_maxtries_reached(buffer_get_int(&m));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000648 *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
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000709static void
710mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
711 char ***prompts, u_int **echo_on)
712{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000713 *name = xstrdup("");
714 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000715 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100716 *prompts = xcalloc(*numprompts, sizeof(char *));
717 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000718 (*echo_on)[0] = 0;
719}
720
721int
722mm_bsdauth_query(void *ctx, char **name, char **infotxt,
723 u_int *numprompts, char ***prompts, u_int **echo_on)
724{
725 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100726 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000727 char *challenge;
728
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000729 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000730
731 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000732 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000733
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000734 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000735 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100736 success = buffer_get_int(&m);
737 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000738 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000739 buffer_free(&m);
740 return (-1);
741 }
742
743 /* Get the challenge, and format the response */
744 challenge = buffer_get_string(&m, NULL);
745 buffer_free(&m);
746
747 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
748 (*prompts)[0] = challenge;
749
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000750 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000751
752 return (0);
753}
754
755int
756mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
757{
758 Buffer m;
759 int authok;
760
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000761 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000762 if (numresponses != 1)
763 return (-1);
764
765 buffer_init(&m);
766 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000767 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000768
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000769 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000770 MONITOR_ANS_BSDAUTHRESPOND, &m);
771
772 authok = buffer_get_int(&m);
773 buffer_free(&m);
774
775 return ((authok == 0) ? -1 : 0);
776}
777
Darren Tucker042e2e82004-07-08 23:09:42 +1000778#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000779int
780mm_skey_query(void *ctx, char **name, char **infotxt,
781 u_int *numprompts, char ***prompts, u_int **echo_on)
782{
783 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100784 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +1000785 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000786
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000787 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000788
789 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000790 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000791
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000792 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000793 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100794 success = buffer_get_int(&m);
795 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000796 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000797 buffer_free(&m);
798 return (-1);
799 }
800
801 /* Get the challenge, and format the response */
802 challenge = buffer_get_string(&m, NULL);
803 buffer_free(&m);
804
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000805 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000806
807 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
808
Damien Miller07d86be2006-03-26 14:19:21 +1100809 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Darren Tuckerf60845f2013-06-02 08:07:31 +1000810 free(challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000811
812 return (0);
813}
814
815int
816mm_skey_respond(void *ctx, u_int numresponses, char **responses)
817{
818 Buffer m;
819 int authok;
820
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000821 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000822 if (numresponses != 1)
823 return (-1);
824
825 buffer_init(&m);
826 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000827 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000828
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000829 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000830 MONITOR_ANS_SKEYRESPOND, &m);
831
832 authok = buffer_get_int(&m);
833 buffer_free(&m);
834
835 return ((authok == 0) ? -1 : 0);
836}
Darren Tucker042e2e82004-07-08 23:09:42 +1000837#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000838
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100839#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100840void
841mm_audit_event(ssh_audit_event_t event)
842{
843 Buffer m;
844
845 debug3("%s entering", __func__);
846
847 buffer_init(&m);
848 buffer_put_int(&m, event);
849
850 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
851 buffer_free(&m);
852}
853
854void
855mm_audit_run_command(const char *command)
856{
857 Buffer m;
858
859 debug3("%s entering command %s", __func__, command);
860
861 buffer_init(&m);
862 buffer_put_cstring(&m, command);
863
864 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
865 buffer_free(&m);
866}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100867#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +1100868
Darren Tucker0efd1552003-08-26 11:49:55 +1000869#ifdef GSSAPI
870OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000871mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +1000872{
873 Buffer m;
874 OM_uint32 major;
875
876 /* Client doesn't get to see the context */
877 *ctx = NULL;
878
879 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000880 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000881
882 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
883 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
884
885 major = buffer_get_int(&m);
886
887 buffer_free(&m);
888 return (major);
889}
890
891OM_uint32
892mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
893 gss_buffer_desc *out, OM_uint32 *flags)
894{
895 Buffer m;
896 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000897 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000898
899 buffer_init(&m);
900 buffer_put_string(&m, in->value, in->length);
901
902 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
903 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
904
905 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +1000906 out->value = buffer_get_string(&m, &len);
907 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000908 if (flags)
909 *flags = buffer_get_int(&m);
910
911 buffer_free(&m);
912
913 return (major);
914}
915
Damien Miller0425d402003-11-17 22:18:21 +1100916OM_uint32
917mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
918{
919 Buffer m;
920 OM_uint32 major;
921
922 buffer_init(&m);
923 buffer_put_string(&m, gssbuf->value, gssbuf->length);
924 buffer_put_string(&m, gssmic->value, gssmic->length);
925
926 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
927 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
928 &m);
929
930 major = buffer_get_int(&m);
931 buffer_free(&m);
932 return(major);
933}
934
Darren Tucker0efd1552003-08-26 11:49:55 +1000935int
936mm_ssh_gssapi_userok(char *user)
937{
938 Buffer m;
939 int authenticated = 0;
940
941 buffer_init(&m);
942
943 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
944 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
945 &m);
946
947 authenticated = buffer_get_int(&m);
948
949 buffer_free(&m);
950 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
951 return (authenticated);
952}
953#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +1100954