blob: b0dbb3f7074e3dcd2e3362c805d70b977428715e [file] [log] [blame]
markus@openbsd.org128343b2015-01-13 19:31:40 +00001/* $OpenBSD: monitor_wrap.c,v 1.81 2015/01/13 19:31:40 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"
Ben Lindstrom036768e2004-04-08 16:12:30 +000063#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
64#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000065#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000066#define TARGET_OS_MAC 1
67#else
68#include "zlib.h"
69#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#ifdef GSSAPI
72#include "ssh-gss.h"
73#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000074#include "monitor_wrap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000075#include "atomicio.h"
76#include "monitor_fdpass.h"
Damien Miller3f941882006-03-31 23:13:02 +110077#include "misc.h"
Damien Millereb8b60e2010-08-31 22:41:14 +100078#include "uuencode.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000079
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000080#include "channels.h"
81#include "session.h"
Darren Tucker1629c072007-02-19 22:25:37 +110082#include "servconf.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +100083#include "roaming.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000084
85/* Imports */
86extern int compat20;
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 Miller72ef7c12015-01-15 02:21:31 +1100154 if (errno == EPIPE) {
155 error("%s: socket closed", __func__);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000156 cleanup_exit(255);
Damien Miller72ef7c12015-01-15 02:21:31 +1100157 }
Damien Millerb253cc42005-05-26 12:23:44 +1000158 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000159 }
Damien Miller3f941882006-03-31 23:13:02 +1100160 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000161 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000162 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000163 buffer_clear(m);
164 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000165 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
166 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000167}
168
169void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000170mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171{
172 u_char rtype;
173
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000174 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000175
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000176 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000177 rtype = buffer_get_char(m);
178 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000179 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000180 rtype, type);
181}
182
Damien Miller1f0311c2014-05-15 14:24:09 +1000183#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000184DH *
185mm_choose_dh(int min, int nbits, int max)
186{
187 BIGNUM *p, *g;
188 int success = 0;
189 Buffer m;
190
191 buffer_init(&m);
192 buffer_put_int(&m, min);
193 buffer_put_int(&m, nbits);
194 buffer_put_int(&m, max);
195
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000196 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000197
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000198 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000199 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000200
201 success = buffer_get_char(&m);
202 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000203 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000204
205 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000206 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000207 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000208 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000209 buffer_get_bignum2(&m, p);
210 buffer_get_bignum2(&m, g);
211
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000212 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000213 buffer_free(&m);
214
215 return (dh_new_group(g, p));
216}
Damien Miller1f0311c2014-05-15 14:24:09 +1000217#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000218
219int
220mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
221{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000222 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);
228 buffer_put_int(&m, kex->host_key_index(key));
229 buffer_put_string(&m, data, datalen);
230
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{
244 Buffer m;
245 struct passwd *pw;
Damien Millerd8478b62011-05-29 21:39:36 +1000246 u_int len, i;
Darren Tucker1629c072007-02-19 22:25:37 +1100247 ServerOptions *newopts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000248
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000249 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000250
251 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000252 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000253
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000254 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000255
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000256 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000257 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000258
259 if (buffer_get_char(&m) == 0) {
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100260 pw = NULL;
261 goto out;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000262 }
Darren Tucker1629c072007-02-19 22:25:37 +1100263 pw = buffer_get_string(&m, &len);
264 if (len != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000265 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000266 pw->pw_name = buffer_get_string(&m, NULL);
267 pw->pw_passwd = buffer_get_string(&m, NULL);
Damien Miller6332da22013-04-23 14:25:52 +1000268#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000269 pw->pw_gecos = buffer_get_string(&m, NULL);
Damien Miller6332da22013-04-23 14:25:52 +1000270#endif
271#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000272 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000273#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000274 pw->pw_dir = buffer_get_string(&m, NULL);
275 pw->pw_shell = buffer_get_string(&m, NULL);
Darren Tucker1629c072007-02-19 22:25:37 +1100276
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100277out:
Darren Tucker1629c072007-02-19 22:25:37 +1100278 /* copy options block as a Match directive may have changed some */
279 newopts = buffer_get_string(&m, &len);
280 if (len != sizeof(*newopts))
281 fatal("%s: option block size mismatch", __func__);
Damien Millerf2e407e2011-05-20 19:04:14 +1000282
283#define M_CP_STROPT(x) do { \
284 if (newopts->x != NULL) \
285 newopts->x = buffer_get_string(&m, NULL); \
286 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000287#define M_CP_STRARRAYOPT(x, nx) do { \
288 for (i = 0; i < newopts->nx; i++) \
289 newopts->x[i] = buffer_get_string(&m, NULL); \
290 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000291 /* See comment in servconf.h */
292 COPY_MATCH_STRING_OPTS();
293#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000294#undef M_CP_STRARRAYOPT
Damien Millerf2e407e2011-05-20 19:04:14 +1000295
Darren Tucker1629c072007-02-19 22:25:37 +1100296 copy_set_server_options(&options, newopts, 1);
Darren Tuckera627d422013-06-02 07:31:17 +1000297 free(newopts);
Darren Tucker1629c072007-02-19 22:25:37 +1100298
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000299 buffer_free(&m);
300
301 return (pw);
302}
303
Darren Tucker7eb3de02003-10-15 15:56:58 +1000304char *
305mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000306{
307 Buffer m;
308 char *banner;
309
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000310 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000311
312 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000313 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000314 buffer_clear(&m);
315
Darren Tucker7eb3de02003-10-15 15:56:58 +1000316 mm_request_receive_expect(pmonitor->m_recvfd,
317 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000318 banner = buffer_get_string(&m, NULL);
319 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000320
Darren Tucker7eb3de02003-10-15 15:56:58 +1000321 /* treat empty banner as missing banner */
322 if (strlen(banner) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000323 free(banner);
Darren Tucker7eb3de02003-10-15 15:56:58 +1000324 banner = NULL;
325 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000326 return (banner);
327}
328
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000329/* Inform the privileged process about service and style */
330
331void
332mm_inform_authserv(char *service, char *style)
333{
334 Buffer m;
335
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000336 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000337
338 buffer_init(&m);
339 buffer_put_cstring(&m, service);
340 buffer_put_cstring(&m, style ? style : "");
341
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000342 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000343
344 buffer_free(&m);
345}
346
347/* Do the password authentication */
348int
349mm_auth_password(Authctxt *authctxt, char *password)
350{
351 Buffer m;
352 int authenticated = 0;
353
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000354 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000355
356 buffer_init(&m);
357 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000358 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000359
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000360 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000361 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000362
363 authenticated = buffer_get_int(&m);
364
365 buffer_free(&m);
366
367 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000368 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000369 return (authenticated);
370}
371
372int
373mm_user_key_allowed(struct passwd *pw, Key *key)
374{
375 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
376}
377
378int
379mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
380 Key *key)
381{
382 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
383}
384
385int
386mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
387 char *host, Key *key)
388{
389 int ret;
390
391 key->type = KEY_RSA; /* XXX hack for key_to_blob */
392 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
393 key->type = KEY_RSA1;
394 return (ret);
395}
396
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000397int
398mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
399{
400 Buffer m;
401 u_char *blob;
402 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100403 int allowed = 0, have_forced = 0;
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
407 /* Convert the key to a blob and the pass it over */
408 if (!key_to_blob(key, &blob, &len))
409 return (0);
410
411 buffer_init(&m);
412 buffer_put_int(&m, type);
413 buffer_put_cstring(&m, user ? user : "");
414 buffer_put_cstring(&m, host ? host : "");
415 buffer_put_string(&m, blob, len);
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
442mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
443{
444 Buffer m;
445 u_char *blob;
446 u_int len;
447 int verified = 0;
448
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000449 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000450
451 /* Convert the key to a blob and the pass it over */
452 if (!key_to_blob(key, &blob, &len))
453 return (0);
454
455 buffer_init(&m);
456 buffer_put_string(&m, blob, len);
457 buffer_put_string(&m, sig, siglen);
458 buffer_put_string(&m, data, datalen);
Darren Tuckera627d422013-06-02 07:31:17 +1000459 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000460
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000461 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000462
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000463 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000464 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000465
466 verified = buffer_get_int(&m);
467
468 buffer_free(&m);
469
470 return (verified);
471}
472
473/* Export key state after authentication */
474Newkeys *
475mm_newkeys_from_blob(u_char *blob, int blen)
476{
477 Buffer b;
478 u_int len;
479 Newkeys *newkey = NULL;
480 Enc *enc;
481 Mac *mac;
482 Comp *comp;
483
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000484 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000485#ifdef DEBUG_PK
486 dump_base64(stderr, blob, blen);
487#endif
488 buffer_init(&b);
489 buffer_append(&b, blob, blen);
490
Damien Miller49c145c2013-11-07 13:35:39 +1100491 newkey = xcalloc(1, sizeof(*newkey));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000492 enc = &newkey->enc;
493 mac = &newkey->mac;
494 comp = &newkey->comp;
495
496 /* Enc structure */
497 enc->name = buffer_get_string(&b, NULL);
498 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
499 enc->enabled = buffer_get_int(&b);
500 enc->block_size = buffer_get_int(&b);
501 enc->key = buffer_get_string(&b, &enc->key_len);
Damien Miller1d75abf2013-01-09 16:12:19 +1100502 enc->iv = buffer_get_string(&b, &enc->iv_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000503
504 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000505 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000506 enc->name, enc->cipher);
507
508 /* Mac structure */
Damien Miller1d75abf2013-01-09 16:12:19 +1100509 if (cipher_authlen(enc->cipher) == 0) {
510 mac->name = buffer_get_string(&b, NULL);
markus@openbsd.org128343b2015-01-13 19:31:40 +0000511 if (mac->name == NULL || mac_setup(mac, mac->name) != 0)
Damien Miller1d75abf2013-01-09 16:12:19 +1100512 fatal("%s: can not setup mac %s", __func__, mac->name);
513 mac->enabled = buffer_get_int(&b);
514 mac->key = buffer_get_string(&b, &len);
515 if (len > mac->key_len)
516 fatal("%s: bad mac key length: %u > %d", __func__, len,
517 mac->key_len);
518 mac->key_len = len;
519 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000520
521 /* Comp structure */
522 comp->type = buffer_get_int(&b);
523 comp->enabled = buffer_get_int(&b);
524 comp->name = buffer_get_string(&b, NULL);
525
526 len = buffer_len(&b);
527 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000528 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000529 buffer_free(&b);
530 return (newkey);
531}
532
533int
534mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
535{
536 Buffer b;
537 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000538 Enc *enc;
539 Mac *mac;
540 Comp *comp;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000541 Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000542
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000543 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000544
545 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000546 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000547 return 0;
548 }
549 enc = &newkey->enc;
550 mac = &newkey->mac;
551 comp = &newkey->comp;
552
553 buffer_init(&b);
554 /* Enc structure */
555 buffer_put_cstring(&b, enc->name);
556 /* The cipher struct is constant and shared, you export pointer */
557 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
558 buffer_put_int(&b, enc->enabled);
559 buffer_put_int(&b, enc->block_size);
560 buffer_put_string(&b, enc->key, enc->key_len);
Damien Miller1d75abf2013-01-09 16:12:19 +1100561 packet_get_keyiv(mode, enc->iv, enc->iv_len);
562 buffer_put_string(&b, enc->iv, enc->iv_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000563
564 /* Mac structure */
Damien Miller1d75abf2013-01-09 16:12:19 +1100565 if (cipher_authlen(enc->cipher) == 0) {
566 buffer_put_cstring(&b, mac->name);
567 buffer_put_int(&b, mac->enabled);
568 buffer_put_string(&b, mac->key, mac->key_len);
569 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000570
571 /* Comp structure */
572 buffer_put_int(&b, comp->type);
573 buffer_put_int(&b, comp->enabled);
574 buffer_put_cstring(&b, comp->name);
575
576 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577 if (lenp != NULL)
578 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000579 if (blobp != NULL) {
580 *blobp = xmalloc(len);
581 memcpy(*blobp, buffer_ptr(&b), len);
582 }
Damien Millera5103f42014-02-04 11:20:14 +1100583 explicit_bzero(buffer_ptr(&b), len);
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000584 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000585 return len;
586}
587
588static void
589mm_send_kex(Buffer *m, Kex *kex)
590{
591 buffer_put_string(m, kex->session_id, kex->session_id_len);
592 buffer_put_int(m, kex->we_need);
593 buffer_put_int(m, kex->hostkey_type);
594 buffer_put_int(m, kex->kex_type);
595 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
596 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
597 buffer_put_int(m, kex->flags);
598 buffer_put_cstring(m, kex->client_version_string);
599 buffer_put_cstring(m, kex->server_version_string);
600}
601
602void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000603mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000605 Buffer m, *input, *output;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000606 u_char *blob, *p;
607 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000608 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000609 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000610
611 buffer_init(&m);
612
613 if (!compat20) {
614 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000615 u_char *key;
616 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000617
618 buffer_put_int(&m, packet_get_protocol_flags());
619
620 buffer_put_int(&m, packet_get_ssh1_cipher());
621
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000622 debug3("%s: Sending ssh1 KEY+IV", __func__);
623 keylen = packet_get_encryption_key(NULL);
624 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
625 keylen = packet_get_encryption_key(key);
626 buffer_put_string(&m, key, keylen);
Damien Millera5103f42014-02-04 11:20:14 +1100627 explicit_bzero(key, keylen);
Darren Tuckera627d422013-06-02 07:31:17 +1000628 free(key);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000629
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000630 ivlen = packet_get_keyiv_len(MODE_OUT);
631 packet_get_keyiv(MODE_OUT, iv, ivlen);
632 buffer_put_string(&m, iv, ivlen);
Darren Tucker3a7c0412012-10-05 10:51:59 +1000633 ivlen = packet_get_keyiv_len(MODE_IN);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000634 packet_get_keyiv(MODE_IN, iv, ivlen);
635 buffer_put_string(&m, iv, ivlen);
636 goto skip;
637 } else {
638 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000639 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000640 }
641
642 debug3("%s: Sending new keys: %p %p",
Darren Tuckerf7288d72009-06-21 18:12:20 +1000643 __func__, packet_get_newkeys(MODE_OUT),
644 packet_get_newkeys(MODE_IN));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000645
646 /* Keys from Kex */
647 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000648 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000649
650 buffer_put_string(&m, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +1000651 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000652
653 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000654 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000655
656 buffer_put_string(&m, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +1000657 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000658
Damien Millerb61f3fc2008-07-11 17:36:48 +1000659 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000660 buffer_put_int(&m, seqnr);
661 buffer_put_int64(&m, blocks);
662 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000663 buffer_put_int64(&m, bytes);
664 packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000665 buffer_put_int(&m, seqnr);
666 buffer_put_int64(&m, blocks);
667 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000668 buffer_put_int64(&m, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000670 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000671 skip:
672 /* More key context */
673 plen = packet_get_keycontext(MODE_OUT, NULL);
674 p = xmalloc(plen+1);
675 packet_get_keycontext(MODE_OUT, p);
676 buffer_put_string(&m, p, plen);
Darren Tuckera627d422013-06-02 07:31:17 +1000677 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000678
679 plen = packet_get_keycontext(MODE_IN, NULL);
680 p = xmalloc(plen+1);
681 packet_get_keycontext(MODE_IN, p);
682 buffer_put_string(&m, p, plen);
Darren Tuckera627d422013-06-02 07:31:17 +1000683 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000684
685 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000686 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000687 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
688 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
689
690 /* Network I/O buffers */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000691 input = (Buffer *)packet_get_input();
692 output = (Buffer *)packet_get_output();
693 buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
694 buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000695
Darren Tuckerc5564e12009-06-21 18:53:53 +1000696 /* Roaming */
697 if (compat20) {
698 buffer_put_int64(&m, get_sent_bytes());
699 buffer_put_int64(&m, get_recv_bytes());
700 }
701
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000702 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000703 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000704
705 buffer_free(&m);
706}
707
708int
Damien Miller71a73672006-03-26 14:04:36 +1100709mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000710{
711 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000712 char *p, *msg;
Damien Miller7207f642008-05-19 15:34:50 +1000713 int success = 0, tmp1 = -1, tmp2 = -1;
714
715 /* Kludge: ensure there are fds free to receive the pty/tty */
716 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
717 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
718 error("%s: cannot allocate fds for pty", __func__);
719 if (tmp1 > 0)
720 close(tmp1);
721 if (tmp2 > 0)
722 close(tmp2);
723 return 0;
724 }
725 close(tmp1);
726 close(tmp2);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000727
728 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000729 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000730
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000731 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000732 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000733
734 success = buffer_get_int(&m);
735 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000736 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000737 buffer_free(&m);
738 return (0);
739 }
740 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000741 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000742 buffer_free(&m);
743
744 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
Darren Tuckera627d422013-06-02 07:31:17 +1000745 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000746
Darren Tucker09991742004-07-17 17:05:14 +1000747 buffer_append(&loginmsg, msg, strlen(msg));
Darren Tuckera627d422013-06-02 07:31:17 +1000748 free(msg);
Darren Tucker09991742004-07-17 17:05:14 +1000749
Damien Miller54fd7cf2007-09-17 12:04:08 +1000750 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
751 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
752 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000753
754 /* Success */
755 return (1);
756}
757
758void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000759mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000760{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000761 Buffer m;
762
763 if (s->ttyfd == -1)
764 return;
765 buffer_init(&m);
766 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000767 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000768 buffer_free(&m);
769
770 /* closed dup'ed master */
Damien Miller7207f642008-05-19 15:34:50 +1000771 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
772 error("close(s->ptymaster/%d): %s",
773 s->ptymaster, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000774
775 /* unlink pty from session */
776 s->ttyfd = -1;
777}
778
Damien Miller79418552002-04-23 20:28:48 +1000779#ifdef USE_PAM
780void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100781mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000782{
783 Buffer m;
784
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000785 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000786 if (!options.use_pam)
787 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000788
789 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000790 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000791
792 buffer_free(&m);
793}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000794
Damien Miller1f499fd2003-08-25 13:08:49 +1000795u_int
796mm_do_pam_account(void)
797{
798 Buffer m;
799 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000800 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000801
802 debug3("%s entering", __func__);
803 if (!options.use_pam)
804 fatal("UsePAM=no, but ended up in %s anyway", __func__);
805
806 buffer_init(&m);
807 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
808
Damien Millera8e06ce2003-11-21 23:48:55 +1100809 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000810 MONITOR_ANS_PAM_ACCOUNT, &m);
811 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000812 msg = buffer_get_string(&m, NULL);
813 buffer_append(&loginmsg, msg, strlen(msg));
Darren Tuckerf60845f2013-06-02 08:07:31 +1000814 free(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000815
816 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100817
Damien Miller1f499fd2003-08-25 13:08:49 +1000818 debug3("%s returning %d", __func__, ret);
819
820 return (ret);
821}
822
Damien Miller4f9f42a2003-05-10 19:28:02 +1000823void *
824mm_sshpam_init_ctx(Authctxt *authctxt)
825{
826 Buffer m;
827 int success;
828
829 debug3("%s", __func__);
830 buffer_init(&m);
831 buffer_put_cstring(&m, authctxt->user);
832 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
833 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
834 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
835 success = buffer_get_int(&m);
836 if (success == 0) {
837 debug3("%s: pam_init_ctx failed", __func__);
838 buffer_free(&m);
839 return (NULL);
840 }
841 buffer_free(&m);
842 return (authctxt);
843}
844
845int
846mm_sshpam_query(void *ctx, char **name, char **info,
847 u_int *num, char ***prompts, u_int **echo_on)
848{
849 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000850 u_int i;
851 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000852
853 debug3("%s", __func__);
854 buffer_init(&m);
855 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
856 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
857 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
858 ret = buffer_get_int(&m);
859 debug3("%s: pam_query returned %d", __func__, ret);
860 *name = buffer_get_string(&m, NULL);
861 *info = buffer_get_string(&m, NULL);
862 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000863 if (*num > PAM_MAX_NUM_MSG)
864 fatal("%s: recieved %u PAM messages, expected <= %u",
865 __func__, *num, PAM_MAX_NUM_MSG);
866 *prompts = xcalloc((*num + 1), sizeof(char *));
867 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000868 for (i = 0; i < *num; ++i) {
869 (*prompts)[i] = buffer_get_string(&m, NULL);
870 (*echo_on)[i] = buffer_get_int(&m);
871 }
872 buffer_free(&m);
873 return (ret);
874}
875
876int
877mm_sshpam_respond(void *ctx, u_int num, char **resp)
878{
879 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000880 u_int i;
881 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000882
883 debug3("%s", __func__);
884 buffer_init(&m);
885 buffer_put_int(&m, num);
886 for (i = 0; i < num; ++i)
887 buffer_put_cstring(&m, resp[i]);
888 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
889 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
890 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
891 ret = buffer_get_int(&m);
892 debug3("%s: pam_respond returned %d", __func__, ret);
893 buffer_free(&m);
894 return (ret);
895}
896
897void
898mm_sshpam_free_ctx(void *ctxtp)
899{
900 Buffer m;
901
902 debug3("%s", __func__);
903 buffer_init(&m);
904 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
905 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
906 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
907 buffer_free(&m);
908}
Damien Miller79418552002-04-23 20:28:48 +1000909#endif /* USE_PAM */
910
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000911/* Request process termination */
912
913void
914mm_terminate(void)
915{
916 Buffer m;
917
918 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000919 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000920 buffer_free(&m);
921}
922
Damien Miller1f0311c2014-05-15 14:24:09 +1000923#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000924int
925mm_ssh1_session_key(BIGNUM *num)
926{
927 int rsafail;
928 Buffer m;
929
930 buffer_init(&m);
931 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000932 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000933
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000934 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000935
936 rsafail = buffer_get_int(&m);
937 buffer_get_bignum2(&m, num);
938
939 buffer_free(&m);
940
941 return (rsafail);
942}
Damien Miller1f0311c2014-05-15 14:24:09 +1000943#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000944
945static void
946mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
947 char ***prompts, u_int **echo_on)
948{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000949 *name = xstrdup("");
950 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000951 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100952 *prompts = xcalloc(*numprompts, sizeof(char *));
953 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000954 (*echo_on)[0] = 0;
955}
956
957int
958mm_bsdauth_query(void *ctx, char **name, char **infotxt,
959 u_int *numprompts, char ***prompts, u_int **echo_on)
960{
961 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100962 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000963 char *challenge;
964
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000965 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000966
967 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000968 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000969
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000970 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000971 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100972 success = buffer_get_int(&m);
973 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000974 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000975 buffer_free(&m);
976 return (-1);
977 }
978
979 /* Get the challenge, and format the response */
980 challenge = buffer_get_string(&m, NULL);
981 buffer_free(&m);
982
983 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
984 (*prompts)[0] = challenge;
985
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000986 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000987
988 return (0);
989}
990
991int
992mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
993{
994 Buffer m;
995 int authok;
996
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000997 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000998 if (numresponses != 1)
999 return (-1);
1000
1001 buffer_init(&m);
1002 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001003 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001004
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001005 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001006 MONITOR_ANS_BSDAUTHRESPOND, &m);
1007
1008 authok = buffer_get_int(&m);
1009 buffer_free(&m);
1010
1011 return ((authok == 0) ? -1 : 0);
1012}
1013
Darren Tucker042e2e82004-07-08 23:09:42 +10001014#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001015int
1016mm_skey_query(void *ctx, char **name, char **infotxt,
1017 u_int *numprompts, char ***prompts, u_int **echo_on)
1018{
1019 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +11001020 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +10001021 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001022
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001023 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001024
1025 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001026 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001027
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001028 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001029 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +11001030 success = buffer_get_int(&m);
1031 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001032 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001033 buffer_free(&m);
1034 return (-1);
1035 }
1036
1037 /* Get the challenge, and format the response */
1038 challenge = buffer_get_string(&m, NULL);
1039 buffer_free(&m);
1040
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001041 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001042
1043 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
1044
Damien Miller07d86be2006-03-26 14:19:21 +11001045 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001046 free(challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001047
1048 return (0);
1049}
1050
1051int
1052mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1053{
1054 Buffer m;
1055 int authok;
1056
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001057 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001058 if (numresponses != 1)
1059 return (-1);
1060
1061 buffer_init(&m);
1062 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001063 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001064
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001065 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001066 MONITOR_ANS_SKEYRESPOND, &m);
1067
1068 authok = buffer_get_int(&m);
1069 buffer_free(&m);
1070
1071 return ((authok == 0) ? -1 : 0);
1072}
Darren Tucker042e2e82004-07-08 23:09:42 +10001073#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001074
1075void
1076mm_ssh1_session_id(u_char session_id[16])
1077{
1078 Buffer m;
1079 int i;
1080
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001081 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001082
1083 buffer_init(&m);
1084 for (i = 0; i < 16; i++)
1085 buffer_put_char(&m, session_id[i]);
1086
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001087 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001088 buffer_free(&m);
1089}
1090
Damien Miller1f0311c2014-05-15 14:24:09 +10001091#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001092int
1093mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1094{
1095 Buffer m;
1096 Key *key;
1097 u_char *blob;
1098 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001099 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001100
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001101 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001102
1103 buffer_init(&m);
1104 buffer_put_bignum2(&m, client_n);
1105
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001106 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1107 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001108
1109 allowed = buffer_get_int(&m);
1110
Damien Miller06ebedf2003-02-24 12:03:38 +11001111 /* fake forced command */
1112 auth_clear_options();
1113 have_forced = buffer_get_int(&m);
1114 forced_command = have_forced ? xstrdup("true") : NULL;
1115
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001116 if (allowed && rkey != NULL) {
1117 blob = buffer_get_string(&m, &blen);
1118 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001119 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001120 *rkey = key;
Darren Tuckera627d422013-06-02 07:31:17 +10001121 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001122 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001123 buffer_free(&m);
1124
1125 return (allowed);
1126}
1127
1128BIGNUM *
1129mm_auth_rsa_generate_challenge(Key *key)
1130{
1131 Buffer m;
1132 BIGNUM *challenge;
1133 u_char *blob;
1134 u_int blen;
1135
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001136 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001137
1138 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001139 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001140
1141 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1142 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001143 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001144 key->type = KEY_RSA1;
1145
1146 buffer_init(&m);
1147 buffer_put_string(&m, blob, blen);
Darren Tuckera627d422013-06-02 07:31:17 +10001148 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001149
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001150 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1151 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001152
1153 buffer_get_bignum2(&m, challenge);
1154 buffer_free(&m);
1155
1156 return (challenge);
1157}
1158
1159int
1160mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1161{
1162 Buffer m;
1163 u_char *blob;
1164 u_int blen;
1165 int success = 0;
1166
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001167 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001168
1169 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1170 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001171 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001172 key->type = KEY_RSA1;
1173
1174 buffer_init(&m);
1175 buffer_put_string(&m, blob, blen);
1176 buffer_put_string(&m, response, 16);
Darren Tuckera627d422013-06-02 07:31:17 +10001177 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001178
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001179 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1180 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001181
1182 success = buffer_get_int(&m);
1183 buffer_free(&m);
1184
1185 return (success);
1186}
Damien Miller1f0311c2014-05-15 14:24:09 +10001187#endif
Damien Miller25162f22002-09-12 09:47:29 +10001188
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001189#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001190void
1191mm_audit_event(ssh_audit_event_t event)
1192{
1193 Buffer m;
1194
1195 debug3("%s entering", __func__);
1196
1197 buffer_init(&m);
1198 buffer_put_int(&m, event);
1199
1200 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1201 buffer_free(&m);
1202}
1203
1204void
1205mm_audit_run_command(const char *command)
1206{
1207 Buffer m;
1208
1209 debug3("%s entering command %s", __func__, command);
1210
1211 buffer_init(&m);
1212 buffer_put_cstring(&m, command);
1213
1214 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1215 buffer_free(&m);
1216}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001217#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001218
Darren Tucker0efd1552003-08-26 11:49:55 +10001219#ifdef GSSAPI
1220OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001221mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001222{
1223 Buffer m;
1224 OM_uint32 major;
1225
1226 /* Client doesn't get to see the context */
1227 *ctx = NULL;
1228
1229 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001230 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001231
1232 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1233 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1234
1235 major = buffer_get_int(&m);
1236
1237 buffer_free(&m);
1238 return (major);
1239}
1240
1241OM_uint32
1242mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1243 gss_buffer_desc *out, OM_uint32 *flags)
1244{
1245 Buffer m;
1246 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001247 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001248
1249 buffer_init(&m);
1250 buffer_put_string(&m, in->value, in->length);
1251
1252 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1253 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1254
1255 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001256 out->value = buffer_get_string(&m, &len);
1257 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001258 if (flags)
1259 *flags = buffer_get_int(&m);
1260
1261 buffer_free(&m);
1262
1263 return (major);
1264}
1265
Damien Miller0425d402003-11-17 22:18:21 +11001266OM_uint32
1267mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1268{
1269 Buffer m;
1270 OM_uint32 major;
1271
1272 buffer_init(&m);
1273 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1274 buffer_put_string(&m, gssmic->value, gssmic->length);
1275
1276 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1277 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1278 &m);
1279
1280 major = buffer_get_int(&m);
1281 buffer_free(&m);
1282 return(major);
1283}
1284
Darren Tucker0efd1552003-08-26 11:49:55 +10001285int
1286mm_ssh_gssapi_userok(char *user)
1287{
1288 Buffer m;
1289 int authenticated = 0;
1290
1291 buffer_init(&m);
1292
1293 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1294 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1295 &m);
1296
1297 authenticated = buffer_get_int(&m);
1298
1299 buffer_free(&m);
1300 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1301 return (authenticated);
1302}
1303#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001304