blob: b1f489f793677802535eaa4a22921999b7351234 [file] [log] [blame]
djm@openbsd.org7c856852018-03-03 03:15:51 +00001/* $OpenBSD: monitor_wrap.c,v 1.99 2018/03/03 03:15: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"
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
djm@openbsd.org7c856852018-03-03 03:15:51 +0000354mm_auth_password(struct ssh *ssh, char *password)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000355{
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
djm@openbsd.org7c856852018-03-03 03:15:51 +0000381mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
382 int pubkey_auth_attempt, struct sshauthopt **authoptp)
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,
djm@openbsd.org7c856852018-03-03 03:15:51 +0000385 pubkey_auth_attempt, authoptp));
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.org7c856852018-03-03 03:15:51 +0000392 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL));
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,
djm@openbsd.org7c856852018-03-03 03:15:51 +0000397 struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000398{
399 Buffer m;
400 u_char *blob;
401 u_int len;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000402 int r, allowed = 0;
403 struct sshauthopt *opts = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000404
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000405 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000406
djm@openbsd.org7c856852018-03-03 03:15:51 +0000407 if (authoptp != NULL)
408 *authoptp = NULL;
409
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000410 /* Convert the key to a blob and the pass it over */
411 if (!key_to_blob(key, &blob, &len))
djm@openbsd.org7c856852018-03-03 03:15:51 +0000412 return 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000413
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__);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000425 mm_request_receive_expect(pmonitor->m_recvfd,
426 MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000427
428 allowed = buffer_get_int(&m);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000429 if (allowed && type == MM_USERKEY) {
430 if ((r = sshauthopt_deserialise(&m, &opts)) != 0)
431 fatal("%s: sshauthopt_deserialise: %s",
432 __func__, ssh_err(r));
433 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000434 buffer_free(&m);
435
djm@openbsd.org7c856852018-03-03 03:15:51 +0000436 if (authoptp != NULL) {
437 *authoptp = opts;
438 opts = NULL;
439 }
440 sshauthopt_free(opts);
441
442 return allowed;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000443}
444
445/*
446 * This key verify needs to send the key type along, because the
447 * privileged parent makes the decision if the key is allowed
448 * for authentication.
449 */
450
451int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000452mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000453 const u_char *data, size_t datalen, const char *sigalg, u_int compat)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000454{
455 Buffer m;
456 u_char *blob;
457 u_int len;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000458 u_int encoded_ret = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000459
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000460 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000461
462 /* Convert the key to a blob and the pass it over */
463 if (!key_to_blob(key, &blob, &len))
464 return (0);
465
466 buffer_init(&m);
467 buffer_put_string(&m, blob, len);
468 buffer_put_string(&m, sig, siglen);
469 buffer_put_string(&m, data, datalen);
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +0000470 buffer_put_cstring(&m, sigalg == NULL ? "" : sigalg);
Darren Tuckera627d422013-06-02 07:31:17 +1000471 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000472
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000473 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000474
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000475 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000476 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000477
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000478 encoded_ret = buffer_get_int(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000479
480 buffer_free(&m);
481
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000482 if (encoded_ret != 0)
483 return SSH_ERR_SIGNATURE_INVALID;
484 return 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000485}
486
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000487void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000488mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000489{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000490 struct ssh *ssh = active_state; /* XXX */
491 struct sshbuf *m;
492 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000493
markus@openbsd.org091c3022015-01-19 19:52:16 +0000494 if ((m = sshbuf_new()) == NULL)
495 fatal("%s: sshbuf_new failed", __func__);
496 if ((r = ssh_packet_get_state(ssh, m)) != 0)
497 fatal("%s: get_state failed: %s",
498 __func__, ssh_err(r));
499 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000500 debug3("%s: Finished sending state", __func__);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000501 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000502}
503
504int
Damien Miller71a73672006-03-26 14:04:36 +1100505mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000506{
507 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000508 char *p, *msg;
Damien Miller7207f642008-05-19 15:34:50 +1000509 int success = 0, tmp1 = -1, tmp2 = -1;
510
511 /* Kludge: ensure there are fds free to receive the pty/tty */
512 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
513 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
514 error("%s: cannot allocate fds for pty", __func__);
515 if (tmp1 > 0)
516 close(tmp1);
517 if (tmp2 > 0)
518 close(tmp2);
519 return 0;
520 }
521 close(tmp1);
522 close(tmp2);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523
524 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000525 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000526
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000527 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000528 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000529
530 success = buffer_get_int(&m);
531 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000532 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000533 buffer_free(&m);
534 return (0);
535 }
536 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000537 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000538 buffer_free(&m);
539
540 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
Darren Tuckera627d422013-06-02 07:31:17 +1000541 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000542
Darren Tucker09991742004-07-17 17:05:14 +1000543 buffer_append(&loginmsg, msg, strlen(msg));
Darren Tuckera627d422013-06-02 07:31:17 +1000544 free(msg);
Darren Tucker09991742004-07-17 17:05:14 +1000545
Damien Miller54fd7cf2007-09-17 12:04:08 +1000546 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
547 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
548 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000549
550 /* Success */
551 return (1);
552}
553
554void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000555mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000556{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000557 Buffer m;
558
559 if (s->ttyfd == -1)
560 return;
561 buffer_init(&m);
562 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000563 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000564 buffer_free(&m);
565
566 /* closed dup'ed master */
Damien Miller7207f642008-05-19 15:34:50 +1000567 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
568 error("close(s->ptymaster/%d): %s",
569 s->ptymaster, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000570
571 /* unlink pty from session */
572 s->ttyfd = -1;
573}
574
Damien Miller79418552002-04-23 20:28:48 +1000575#ifdef USE_PAM
576void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100577mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000578{
579 Buffer m;
580
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000581 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000582 if (!options.use_pam)
583 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000584
585 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000586 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000587
588 buffer_free(&m);
589}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000590
Damien Miller1f499fd2003-08-25 13:08:49 +1000591u_int
592mm_do_pam_account(void)
593{
594 Buffer m;
595 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000596 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000597
598 debug3("%s entering", __func__);
599 if (!options.use_pam)
600 fatal("UsePAM=no, but ended up in %s anyway", __func__);
601
602 buffer_init(&m);
603 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
604
Damien Millera8e06ce2003-11-21 23:48:55 +1100605 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000606 MONITOR_ANS_PAM_ACCOUNT, &m);
607 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000608 msg = buffer_get_string(&m, NULL);
609 buffer_append(&loginmsg, msg, strlen(msg));
Darren Tuckerf60845f2013-06-02 08:07:31 +1000610 free(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000611
612 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100613
Damien Miller1f499fd2003-08-25 13:08:49 +1000614 debug3("%s returning %d", __func__, ret);
615
616 return (ret);
617}
618
Damien Miller4f9f42a2003-05-10 19:28:02 +1000619void *
620mm_sshpam_init_ctx(Authctxt *authctxt)
621{
622 Buffer m;
623 int success;
624
625 debug3("%s", __func__);
626 buffer_init(&m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000627 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
628 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
629 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
630 success = buffer_get_int(&m);
631 if (success == 0) {
632 debug3("%s: pam_init_ctx failed", __func__);
633 buffer_free(&m);
634 return (NULL);
635 }
636 buffer_free(&m);
637 return (authctxt);
638}
639
640int
641mm_sshpam_query(void *ctx, char **name, char **info,
642 u_int *num, char ***prompts, u_int **echo_on)
643{
644 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000645 u_int i;
646 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000647
648 debug3("%s", __func__);
649 buffer_init(&m);
650 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
651 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
652 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
653 ret = buffer_get_int(&m);
654 debug3("%s: pam_query returned %d", __func__, ret);
655 *name = buffer_get_string(&m, NULL);
656 *info = buffer_get_string(&m, NULL);
Darren Tucker01558b72016-07-18 09:33:25 +1000657 sshpam_set_maxtries_reached(buffer_get_int(&m));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000658 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000659 if (*num > PAM_MAX_NUM_MSG)
Damien Miller10479cc2018-04-10 10:19:02 +1000660 fatal("%s: received %u PAM messages, expected <= %u",
Darren Tuckerd8093e42006-05-04 16:24:34 +1000661 __func__, *num, PAM_MAX_NUM_MSG);
662 *prompts = xcalloc((*num + 1), sizeof(char *));
663 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000664 for (i = 0; i < *num; ++i) {
665 (*prompts)[i] = buffer_get_string(&m, NULL);
666 (*echo_on)[i] = buffer_get_int(&m);
667 }
668 buffer_free(&m);
669 return (ret);
670}
671
672int
673mm_sshpam_respond(void *ctx, u_int num, char **resp)
674{
675 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000676 u_int i;
677 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000678
679 debug3("%s", __func__);
680 buffer_init(&m);
681 buffer_put_int(&m, num);
682 for (i = 0; i < num; ++i)
683 buffer_put_cstring(&m, resp[i]);
684 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
685 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
686 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
687 ret = buffer_get_int(&m);
688 debug3("%s: pam_respond returned %d", __func__, ret);
689 buffer_free(&m);
690 return (ret);
691}
692
693void
694mm_sshpam_free_ctx(void *ctxtp)
695{
696 Buffer m;
697
698 debug3("%s", __func__);
699 buffer_init(&m);
700 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
701 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
702 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
703 buffer_free(&m);
704}
Damien Miller79418552002-04-23 20:28:48 +1000705#endif /* USE_PAM */
706
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000707/* Request process termination */
708
709void
710mm_terminate(void)
711{
712 Buffer m;
713
714 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000715 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000716 buffer_free(&m);
717}
718
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000719static void
720mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
721 char ***prompts, u_int **echo_on)
722{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000723 *name = xstrdup("");
724 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100726 *prompts = xcalloc(*numprompts, sizeof(char *));
727 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728 (*echo_on)[0] = 0;
729}
730
731int
732mm_bsdauth_query(void *ctx, char **name, char **infotxt,
733 u_int *numprompts, char ***prompts, u_int **echo_on)
734{
735 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100736 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000737 char *challenge;
738
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000739 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000740
741 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000742 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000743
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000744 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000745 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100746 success = buffer_get_int(&m);
747 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000748 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000749 buffer_free(&m);
750 return (-1);
751 }
752
753 /* Get the challenge, and format the response */
754 challenge = buffer_get_string(&m, NULL);
755 buffer_free(&m);
756
757 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
758 (*prompts)[0] = challenge;
759
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000760 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000761
762 return (0);
763}
764
765int
766mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
767{
768 Buffer m;
769 int authok;
770
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000771 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000772 if (numresponses != 1)
773 return (-1);
774
775 buffer_init(&m);
776 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000777 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000778
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000779 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000780 MONITOR_ANS_BSDAUTHRESPOND, &m);
781
782 authok = buffer_get_int(&m);
783 buffer_free(&m);
784
785 return ((authok == 0) ? -1 : 0);
786}
787
Darren Tucker042e2e82004-07-08 23:09:42 +1000788#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000789int
790mm_skey_query(void *ctx, char **name, char **infotxt,
791 u_int *numprompts, char ***prompts, u_int **echo_on)
792{
793 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100794 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +1000795 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000796
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000797 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000798
799 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000800 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000801
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000802 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000803 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100804 success = buffer_get_int(&m);
805 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000806 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000807 buffer_free(&m);
808 return (-1);
809 }
810
811 /* Get the challenge, and format the response */
812 challenge = buffer_get_string(&m, NULL);
813 buffer_free(&m);
814
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000815 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000816
817 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
818
Damien Miller07d86be2006-03-26 14:19:21 +1100819 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Darren Tuckerf60845f2013-06-02 08:07:31 +1000820 free(challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000821
822 return (0);
823}
824
825int
826mm_skey_respond(void *ctx, u_int numresponses, char **responses)
827{
828 Buffer m;
829 int authok;
830
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000831 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000832 if (numresponses != 1)
833 return (-1);
834
835 buffer_init(&m);
836 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000837 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000838
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000839 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000840 MONITOR_ANS_SKEYRESPOND, &m);
841
842 authok = buffer_get_int(&m);
843 buffer_free(&m);
844
845 return ((authok == 0) ? -1 : 0);
846}
Darren Tucker042e2e82004-07-08 23:09:42 +1000847#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000848
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100849#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100850void
851mm_audit_event(ssh_audit_event_t event)
852{
853 Buffer m;
854
855 debug3("%s entering", __func__);
856
857 buffer_init(&m);
858 buffer_put_int(&m, event);
859
860 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
861 buffer_free(&m);
862}
863
864void
865mm_audit_run_command(const char *command)
866{
867 Buffer m;
868
869 debug3("%s entering command %s", __func__, command);
870
871 buffer_init(&m);
872 buffer_put_cstring(&m, command);
873
874 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
875 buffer_free(&m);
876}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100877#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +1100878
Darren Tucker0efd1552003-08-26 11:49:55 +1000879#ifdef GSSAPI
880OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000881mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +1000882{
883 Buffer m;
884 OM_uint32 major;
885
886 /* Client doesn't get to see the context */
887 *ctx = NULL;
888
889 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000890 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000891
892 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
893 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
894
895 major = buffer_get_int(&m);
896
897 buffer_free(&m);
898 return (major);
899}
900
901OM_uint32
902mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
903 gss_buffer_desc *out, OM_uint32 *flags)
904{
905 Buffer m;
906 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000907 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000908
909 buffer_init(&m);
910 buffer_put_string(&m, in->value, in->length);
911
912 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
913 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
914
915 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +1000916 out->value = buffer_get_string(&m, &len);
917 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000918 if (flags)
919 *flags = buffer_get_int(&m);
920
921 buffer_free(&m);
922
923 return (major);
924}
925
Damien Miller0425d402003-11-17 22:18:21 +1100926OM_uint32
927mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
928{
929 Buffer m;
930 OM_uint32 major;
931
932 buffer_init(&m);
933 buffer_put_string(&m, gssbuf->value, gssbuf->length);
934 buffer_put_string(&m, gssmic->value, gssmic->length);
935
936 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
937 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
938 &m);
939
940 major = buffer_get_int(&m);
941 buffer_free(&m);
942 return(major);
943}
944
Darren Tucker0efd1552003-08-26 11:49:55 +1000945int
946mm_ssh_gssapi_userok(char *user)
947{
948 Buffer m;
949 int authenticated = 0;
950
951 buffer_init(&m);
952
953 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
954 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
955 &m);
956
957 authenticated = buffer_get_int(&m);
958
959 buffer_free(&m);
960 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
961 return (authenticated);
962}
963#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +1100964