blob: 69212aaf330b58e5d9bf49d3b131c1cfd765579e [file] [log] [blame]
djm@openbsd.org66bf74a2017-10-02 19:33:20 +00001/* $OpenBSD: monitor_wrap.c,v 1.94 2017/10/02 19:33:20 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"
Damien Millereb8b60e2010-08-31 22:41:14 +100079#include "uuencode.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000080
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000081#include "channels.h"
82#include "session.h"
Darren Tucker1629c072007-02-19 22:25:37 +110083#include "servconf.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 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000088extern z_stream incoming_stream;
89extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000090extern struct monitor *pmonitor;
Darren Tucker09991742004-07-17 17:05:14 +100091extern Buffer loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100092extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093
Damien Miller8f0bf232011-06-20 14:42:23 +100094void
95mm_log_handler(LogLevel level, const char *msg, void *ctx)
96{
97 Buffer log_msg;
98 struct monitor *mon = (struct monitor *)ctx;
99
100 if (mon->m_log_sendfd == -1)
101 fatal("%s: no log channel", __func__);
102
103 buffer_init(&log_msg);
104 /*
105 * Placeholder for packet length. Will be filled in with the actual
106 * packet length once the packet has been constucted. This saves
107 * fragile math.
108 */
109 buffer_put_int(&log_msg, 0);
110
111 buffer_put_int(&log_msg, level);
112 buffer_put_cstring(&log_msg, msg);
113 put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4);
114 if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg),
115 buffer_len(&log_msg)) != buffer_len(&log_msg))
116 fatal("%s: write: %s", __func__, strerror(errno));
117 buffer_free(&log_msg);
118}
119
Darren Tucker3e33cec2003-10-02 16:12:36 +1000120int
121mm_is_monitor(void)
122{
123 /*
124 * m_pid is only set in the privileged part, and
125 * points to the unprivileged child.
126 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +1100127 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000128}
129
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000130void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000131mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000132{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000133 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +0000134 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000135
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000136 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000137
Damien Miller3f941882006-03-31 23:13:02 +1100138 put_u32(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000139 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000140 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +1000141 fatal("%s: write: %s", __func__, strerror(errno));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000142 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +1000143 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000144}
145
146void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000147mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000148{
149 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000150 u_int msg_len;
151
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000152 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000153
Damien Millerb253cc42005-05-26 12:23:44 +1000154 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
Damien Miller4d24b3b2015-03-20 09:11:59 +1100155 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000156 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000157 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000158 }
Damien Miller3f941882006-03-31 23:13:02 +1100159 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000160 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000161 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000162 buffer_clear(m);
163 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000164 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
165 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000166}
167
168void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000169mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000170{
171 u_char rtype;
172
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000173 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000174
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000175 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000176 rtype = buffer_get_char(m);
177 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000178 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000179 rtype, type);
180}
181
Damien Miller1f0311c2014-05-15 14:24:09 +1000182#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000183DH *
184mm_choose_dh(int min, int nbits, int max)
185{
186 BIGNUM *p, *g;
187 int success = 0;
188 Buffer m;
189
190 buffer_init(&m);
191 buffer_put_int(&m, min);
192 buffer_put_int(&m, nbits);
193 buffer_put_int(&m, max);
194
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000195 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000196
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000197 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000198 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000199
200 success = buffer_get_char(&m);
201 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000202 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000203
204 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000205 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000207 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000208 buffer_get_bignum2(&m, p);
209 buffer_get_bignum2(&m, g);
210
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000211 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000212 buffer_free(&m);
213
214 return (dh_new_group(g, p));
215}
Damien Miller1f0311c2014-05-15 14:24:09 +1000216#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000217
218int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000219mm_key_sign(struct sshkey *key, u_char **sigp, u_int *lenp,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000220 const u_char *data, u_int datalen, const char *hostkey_alg)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000221{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000222 struct kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000223 Buffer m;
224
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000225 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000226
227 buffer_init(&m);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000228 buffer_put_int(&m, kex->host_key_index(key, 0, active_state));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000229 buffer_put_string(&m, data, datalen);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000230 buffer_put_cstring(&m, hostkey_alg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000231
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{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000245 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000246 Buffer m;
247 struct passwd *pw;
Damien Millerd8478b62011-05-29 21:39:36 +1000248 u_int len, i;
Darren Tucker1629c072007-02-19 22:25:37 +1100249 ServerOptions *newopts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000250
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000251 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000252
253 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000254 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000255
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000256 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000257
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000258 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000259 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000260
261 if (buffer_get_char(&m) == 0) {
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100262 pw = NULL;
263 goto out;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000264 }
Darren Tucker1629c072007-02-19 22:25:37 +1100265 pw = buffer_get_string(&m, &len);
266 if (len != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000267 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000268 pw->pw_name = buffer_get_string(&m, NULL);
269 pw->pw_passwd = buffer_get_string(&m, NULL);
Damien Miller6332da22013-04-23 14:25:52 +1000270#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000271 pw->pw_gecos = buffer_get_string(&m, NULL);
Damien Miller6332da22013-04-23 14:25:52 +1000272#endif
273#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000274 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000275#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000276 pw->pw_dir = buffer_get_string(&m, NULL);
277 pw->pw_shell = buffer_get_string(&m, NULL);
Darren Tucker1629c072007-02-19 22:25:37 +1100278
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100279out:
Darren Tucker1629c072007-02-19 22:25:37 +1100280 /* copy options block as a Match directive may have changed some */
281 newopts = buffer_get_string(&m, &len);
282 if (len != sizeof(*newopts))
283 fatal("%s: option block size mismatch", __func__);
Damien Millerf2e407e2011-05-20 19:04:14 +1000284
285#define M_CP_STROPT(x) do { \
286 if (newopts->x != NULL) \
287 newopts->x = buffer_get_string(&m, NULL); \
288 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000289#define M_CP_STRARRAYOPT(x, nx) do { \
290 for (i = 0; i < newopts->nx; i++) \
291 newopts->x[i] = buffer_get_string(&m, NULL); \
292 } while (0)
djm@openbsd.org66bf74a2017-10-02 19:33:20 +0000293#define M_CP_STRARRAYOPT_ALLOC(x, nx) do { \
294 newopts->x = newopts->nx == 0 ? \
295 NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
296 M_CP_STRARRAYOPT(x, nx); \
297 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000298 /* See comment in servconf.h */
299 COPY_MATCH_STRING_OPTS();
300#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000301#undef M_CP_STRARRAYOPT
djm@openbsd.org66bf74a2017-10-02 19:33:20 +0000302#undef M_CP_STRARRAYOPT_ALLOC
Damien Millerf2e407e2011-05-20 19:04:14 +1000303
Darren Tucker1629c072007-02-19 22:25:37 +1100304 copy_set_server_options(&options, newopts, 1);
djm@openbsd.org54cd41a2017-05-17 01:24:17 +0000305 log_change_level(options.log_level);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000306 process_permitopen(ssh, &options);
Darren Tuckera627d422013-06-02 07:31:17 +1000307 free(newopts);
Darren Tucker1629c072007-02-19 22:25:37 +1100308
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000309 buffer_free(&m);
310
311 return (pw);
312}
313
Darren Tucker7eb3de02003-10-15 15:56:58 +1000314char *
315mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000316{
317 Buffer m;
318 char *banner;
319
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000320 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000321
322 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000323 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000324 buffer_clear(&m);
325
Darren Tucker7eb3de02003-10-15 15:56:58 +1000326 mm_request_receive_expect(pmonitor->m_recvfd,
327 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000328 banner = buffer_get_string(&m, NULL);
329 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000330
Darren Tucker7eb3de02003-10-15 15:56:58 +1000331 /* treat empty banner as missing banner */
332 if (strlen(banner) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000333 free(banner);
Darren Tucker7eb3de02003-10-15 15:56:58 +1000334 banner = NULL;
335 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000336 return (banner);
337}
338
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000339/* Inform the privileged process about service and style */
340
341void
342mm_inform_authserv(char *service, char *style)
343{
344 Buffer m;
345
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000346 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000347
348 buffer_init(&m);
349 buffer_put_cstring(&m, service);
350 buffer_put_cstring(&m, style ? style : "");
351
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000352 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000353
354 buffer_free(&m);
355}
356
357/* Do the password authentication */
358int
359mm_auth_password(Authctxt *authctxt, char *password)
360{
361 Buffer m;
362 int authenticated = 0;
363
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000364 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000365
366 buffer_init(&m);
367 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000368 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000369
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000370 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000371 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372
373 authenticated = buffer_get_int(&m);
Darren Tucker01558b72016-07-18 09:33:25 +1000374#ifdef USE_PAM
375 sshpam_set_maxtries_reached(buffer_get_int(&m));
376#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000377
378 buffer_free(&m);
379
380 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000381 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000382 return (authenticated);
383}
384
385int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000386mm_user_key_allowed(struct passwd *pw, struct sshkey *key,
387 int pubkey_auth_attempt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000388{
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000389 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
390 pubkey_auth_attempt));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000391}
392
393int
djm@openbsd.org95767262016-03-07 19:02:43 +0000394mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host,
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000395 struct sshkey *key)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000396{
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000397 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000398}
399
400int
djm@openbsd.org95767262016-03-07 19:02:43 +0000401mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000402 struct sshkey *key, int pubkey_auth_attempt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000403{
404 Buffer m;
405 u_char *blob;
406 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100407 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000408
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000409 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000410
411 /* Convert the key to a blob and the pass it over */
412 if (!key_to_blob(key, &blob, &len))
413 return (0);
414
415 buffer_init(&m);
416 buffer_put_int(&m, type);
417 buffer_put_cstring(&m, user ? user : "");
418 buffer_put_cstring(&m, host ? host : "");
419 buffer_put_string(&m, blob, len);
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000420 buffer_put_int(&m, pubkey_auth_attempt);
Darren Tuckera627d422013-06-02 07:31:17 +1000421 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000422
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000423 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000424
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000425 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000426 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000427
428 allowed = buffer_get_int(&m);
429
Damien Miller06ebedf2003-02-24 12:03:38 +1100430 /* fake forced command */
431 auth_clear_options();
432 have_forced = buffer_get_int(&m);
433 forced_command = have_forced ? xstrdup("true") : NULL;
434
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000435 buffer_free(&m);
436
437 return (allowed);
438}
439
440/*
441 * This key verify needs to send the key type along, because the
442 * privileged parent makes the decision if the key is allowed
443 * for authentication.
444 */
445
446int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000447mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
448 const u_char *data, size_t datalen, u_int compat)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000449{
450 Buffer m;
451 u_char *blob;
452 u_int len;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000453 u_int encoded_ret = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000454
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000455 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000456
457 /* Convert the key to a blob and the pass it over */
458 if (!key_to_blob(key, &blob, &len))
459 return (0);
460
461 buffer_init(&m);
462 buffer_put_string(&m, blob, len);
463 buffer_put_string(&m, sig, siglen);
464 buffer_put_string(&m, data, datalen);
Darren Tuckera627d422013-06-02 07:31:17 +1000465 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000466
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000467 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000468
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000469 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000470 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000471
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000472 encoded_ret = buffer_get_int(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000473
474 buffer_free(&m);
475
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000476 if (encoded_ret != 0)
477 return SSH_ERR_SIGNATURE_INVALID;
478 return 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000479}
480
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000481void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000482mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000483{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000484 struct ssh *ssh = active_state; /* XXX */
485 struct sshbuf *m;
486 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000487
markus@openbsd.org091c3022015-01-19 19:52:16 +0000488 if ((m = sshbuf_new()) == NULL)
489 fatal("%s: sshbuf_new failed", __func__);
490 if ((r = ssh_packet_get_state(ssh, m)) != 0)
491 fatal("%s: get_state failed: %s",
492 __func__, ssh_err(r));
493 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000494 debug3("%s: Finished sending state", __func__);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000495 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000496}
497
498int
Damien Miller71a73672006-03-26 14:04:36 +1100499mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000500{
501 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000502 char *p, *msg;
Damien Miller7207f642008-05-19 15:34:50 +1000503 int success = 0, tmp1 = -1, tmp2 = -1;
504
505 /* Kludge: ensure there are fds free to receive the pty/tty */
506 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
507 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
508 error("%s: cannot allocate fds for pty", __func__);
509 if (tmp1 > 0)
510 close(tmp1);
511 if (tmp2 > 0)
512 close(tmp2);
513 return 0;
514 }
515 close(tmp1);
516 close(tmp2);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000517
518 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000519 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000520
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000521 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000522 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523
524 success = buffer_get_int(&m);
525 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000526 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000527 buffer_free(&m);
528 return (0);
529 }
530 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000531 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000532 buffer_free(&m);
533
534 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
Darren Tuckera627d422013-06-02 07:31:17 +1000535 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000536
Darren Tucker09991742004-07-17 17:05:14 +1000537 buffer_append(&loginmsg, msg, strlen(msg));
Darren Tuckera627d422013-06-02 07:31:17 +1000538 free(msg);
Darren Tucker09991742004-07-17 17:05:14 +1000539
Damien Miller54fd7cf2007-09-17 12:04:08 +1000540 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
541 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
542 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000543
544 /* Success */
545 return (1);
546}
547
548void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000549mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000550{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000551 Buffer m;
552
553 if (s->ttyfd == -1)
554 return;
555 buffer_init(&m);
556 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000557 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000558 buffer_free(&m);
559
560 /* closed dup'ed master */
Damien Miller7207f642008-05-19 15:34:50 +1000561 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
562 error("close(s->ptymaster/%d): %s",
563 s->ptymaster, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000564
565 /* unlink pty from session */
566 s->ttyfd = -1;
567}
568
Damien Miller79418552002-04-23 20:28:48 +1000569#ifdef USE_PAM
570void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100571mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000572{
573 Buffer m;
574
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000575 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000576 if (!options.use_pam)
577 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000578
579 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000580 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000581
582 buffer_free(&m);
583}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000584
Damien Miller1f499fd2003-08-25 13:08:49 +1000585u_int
586mm_do_pam_account(void)
587{
588 Buffer m;
589 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000590 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000591
592 debug3("%s entering", __func__);
593 if (!options.use_pam)
594 fatal("UsePAM=no, but ended up in %s anyway", __func__);
595
596 buffer_init(&m);
597 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
598
Damien Millera8e06ce2003-11-21 23:48:55 +1100599 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000600 MONITOR_ANS_PAM_ACCOUNT, &m);
601 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000602 msg = buffer_get_string(&m, NULL);
603 buffer_append(&loginmsg, msg, strlen(msg));
Darren Tuckerf60845f2013-06-02 08:07:31 +1000604 free(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000605
606 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100607
Damien Miller1f499fd2003-08-25 13:08:49 +1000608 debug3("%s returning %d", __func__, ret);
609
610 return (ret);
611}
612
Damien Miller4f9f42a2003-05-10 19:28:02 +1000613void *
614mm_sshpam_init_ctx(Authctxt *authctxt)
615{
616 Buffer m;
617 int success;
618
619 debug3("%s", __func__);
620 buffer_init(&m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000621 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
622 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
623 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
624 success = buffer_get_int(&m);
625 if (success == 0) {
626 debug3("%s: pam_init_ctx failed", __func__);
627 buffer_free(&m);
628 return (NULL);
629 }
630 buffer_free(&m);
631 return (authctxt);
632}
633
634int
635mm_sshpam_query(void *ctx, char **name, char **info,
636 u_int *num, char ***prompts, u_int **echo_on)
637{
638 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000639 u_int i;
640 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000641
642 debug3("%s", __func__);
643 buffer_init(&m);
644 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
645 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
646 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
647 ret = buffer_get_int(&m);
648 debug3("%s: pam_query returned %d", __func__, ret);
649 *name = buffer_get_string(&m, NULL);
650 *info = buffer_get_string(&m, NULL);
Darren Tucker01558b72016-07-18 09:33:25 +1000651 sshpam_set_maxtries_reached(buffer_get_int(&m));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000652 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000653 if (*num > PAM_MAX_NUM_MSG)
654 fatal("%s: recieved %u PAM messages, expected <= %u",
655 __func__, *num, PAM_MAX_NUM_MSG);
656 *prompts = xcalloc((*num + 1), sizeof(char *));
657 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000658 for (i = 0; i < *num; ++i) {
659 (*prompts)[i] = buffer_get_string(&m, NULL);
660 (*echo_on)[i] = buffer_get_int(&m);
661 }
662 buffer_free(&m);
663 return (ret);
664}
665
666int
667mm_sshpam_respond(void *ctx, u_int num, char **resp)
668{
669 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000670 u_int i;
671 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000672
673 debug3("%s", __func__);
674 buffer_init(&m);
675 buffer_put_int(&m, num);
676 for (i = 0; i < num; ++i)
677 buffer_put_cstring(&m, resp[i]);
678 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
679 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
680 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
681 ret = buffer_get_int(&m);
682 debug3("%s: pam_respond returned %d", __func__, ret);
683 buffer_free(&m);
684 return (ret);
685}
686
687void
688mm_sshpam_free_ctx(void *ctxtp)
689{
690 Buffer m;
691
692 debug3("%s", __func__);
693 buffer_init(&m);
694 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
695 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
696 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
697 buffer_free(&m);
698}
Damien Miller79418552002-04-23 20:28:48 +1000699#endif /* USE_PAM */
700
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000701/* Request process termination */
702
703void
704mm_terminate(void)
705{
706 Buffer m;
707
708 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000709 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000710 buffer_free(&m);
711}
712
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000713static void
714mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
715 char ***prompts, u_int **echo_on)
716{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000717 *name = xstrdup("");
718 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000719 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100720 *prompts = xcalloc(*numprompts, sizeof(char *));
721 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000722 (*echo_on)[0] = 0;
723}
724
725int
726mm_bsdauth_query(void *ctx, char **name, char **infotxt,
727 u_int *numprompts, char ***prompts, u_int **echo_on)
728{
729 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100730 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000731 char *challenge;
732
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000733 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000734
735 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000736 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000737
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000738 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000739 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100740 success = buffer_get_int(&m);
741 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000742 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000743 buffer_free(&m);
744 return (-1);
745 }
746
747 /* Get the challenge, and format the response */
748 challenge = buffer_get_string(&m, NULL);
749 buffer_free(&m);
750
751 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
752 (*prompts)[0] = challenge;
753
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000754 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000755
756 return (0);
757}
758
759int
760mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
761{
762 Buffer m;
763 int authok;
764
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000765 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000766 if (numresponses != 1)
767 return (-1);
768
769 buffer_init(&m);
770 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000771 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000772
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000773 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000774 MONITOR_ANS_BSDAUTHRESPOND, &m);
775
776 authok = buffer_get_int(&m);
777 buffer_free(&m);
778
779 return ((authok == 0) ? -1 : 0);
780}
781
Darren Tucker042e2e82004-07-08 23:09:42 +1000782#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000783int
784mm_skey_query(void *ctx, char **name, char **infotxt,
785 u_int *numprompts, char ***prompts, u_int **echo_on)
786{
787 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100788 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +1000789 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000790
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000791 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000792
793 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000794 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000795
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000796 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000797 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100798 success = buffer_get_int(&m);
799 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000800 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000801 buffer_free(&m);
802 return (-1);
803 }
804
805 /* Get the challenge, and format the response */
806 challenge = buffer_get_string(&m, NULL);
807 buffer_free(&m);
808
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000809 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000810
811 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
812
Damien Miller07d86be2006-03-26 14:19:21 +1100813 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Darren Tuckerf60845f2013-06-02 08:07:31 +1000814 free(challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000815
816 return (0);
817}
818
819int
820mm_skey_respond(void *ctx, u_int numresponses, char **responses)
821{
822 Buffer m;
823 int authok;
824
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000825 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000826 if (numresponses != 1)
827 return (-1);
828
829 buffer_init(&m);
830 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000831 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000832
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000833 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000834 MONITOR_ANS_SKEYRESPOND, &m);
835
836 authok = buffer_get_int(&m);
837 buffer_free(&m);
838
839 return ((authok == 0) ? -1 : 0);
840}
Darren Tucker042e2e82004-07-08 23:09:42 +1000841#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000842
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100843#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100844void
845mm_audit_event(ssh_audit_event_t event)
846{
847 Buffer m;
848
849 debug3("%s entering", __func__);
850
851 buffer_init(&m);
852 buffer_put_int(&m, event);
853
854 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
855 buffer_free(&m);
856}
857
858void
859mm_audit_run_command(const char *command)
860{
861 Buffer m;
862
863 debug3("%s entering command %s", __func__, command);
864
865 buffer_init(&m);
866 buffer_put_cstring(&m, command);
867
868 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
869 buffer_free(&m);
870}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100871#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +1100872
Darren Tucker0efd1552003-08-26 11:49:55 +1000873#ifdef GSSAPI
874OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000875mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +1000876{
877 Buffer m;
878 OM_uint32 major;
879
880 /* Client doesn't get to see the context */
881 *ctx = NULL;
882
883 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000884 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000885
886 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
887 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
888
889 major = buffer_get_int(&m);
890
891 buffer_free(&m);
892 return (major);
893}
894
895OM_uint32
896mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
897 gss_buffer_desc *out, OM_uint32 *flags)
898{
899 Buffer m;
900 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000901 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000902
903 buffer_init(&m);
904 buffer_put_string(&m, in->value, in->length);
905
906 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
907 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
908
909 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +1000910 out->value = buffer_get_string(&m, &len);
911 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000912 if (flags)
913 *flags = buffer_get_int(&m);
914
915 buffer_free(&m);
916
917 return (major);
918}
919
Damien Miller0425d402003-11-17 22:18:21 +1100920OM_uint32
921mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
922{
923 Buffer m;
924 OM_uint32 major;
925
926 buffer_init(&m);
927 buffer_put_string(&m, gssbuf->value, gssbuf->length);
928 buffer_put_string(&m, gssmic->value, gssmic->length);
929
930 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
931 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
932 &m);
933
934 major = buffer_get_int(&m);
935 buffer_free(&m);
936 return(major);
937}
938
Darren Tucker0efd1552003-08-26 11:49:55 +1000939int
940mm_ssh_gssapi_userok(char *user)
941{
942 Buffer m;
943 int authenticated = 0;
944
945 buffer_init(&m);
946
947 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
948 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
949 &m);
950
951 authenticated = buffer_get_int(&m);
952
953 buffer_free(&m);
954 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
955 return (authenticated);
956}
957#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +1100958