blob: 45dc16951e5b9dab321a39a2bff5bdc28a8dc989 [file] [log] [blame]
Damien Miller1f0311c2014-05-15 14:24:09 +10001/* $OpenBSD: monitor_wrap.c,v 1.80 2014/04/29 18:01:49 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)) {
154 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000155 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000156 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000157 }
Damien Miller3f941882006-03-31 23:13:02 +1100158 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000159 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000160 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000161 buffer_clear(m);
162 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000163 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
164 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000165}
166
167void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000168mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000169{
170 u_char rtype;
171
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000172 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000173
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000174 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000175 rtype = buffer_get_char(m);
176 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000177 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000178 rtype, type);
179}
180
Damien Miller1f0311c2014-05-15 14:24:09 +1000181#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000182DH *
183mm_choose_dh(int min, int nbits, int max)
184{
185 BIGNUM *p, *g;
186 int success = 0;
187 Buffer m;
188
189 buffer_init(&m);
190 buffer_put_int(&m, min);
191 buffer_put_int(&m, nbits);
192 buffer_put_int(&m, max);
193
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000194 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000195
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000196 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000197 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000198
199 success = buffer_get_char(&m);
200 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000201 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202
203 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000204 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000205 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000206 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000207 buffer_get_bignum2(&m, p);
208 buffer_get_bignum2(&m, g);
209
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000210 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000211 buffer_free(&m);
212
213 return (dh_new_group(g, p));
214}
Damien Miller1f0311c2014-05-15 14:24:09 +1000215#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000216
217int
218mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
219{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000220 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000221 Buffer m;
222
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000223 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000224
225 buffer_init(&m);
226 buffer_put_int(&m, kex->host_key_index(key));
227 buffer_put_string(&m, data, datalen);
228
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000229 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000230
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000231 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000232 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000233 *sigp = buffer_get_string(&m, lenp);
234 buffer_free(&m);
235
236 return (0);
237}
238
239struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000240mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000241{
242 Buffer m;
243 struct passwd *pw;
Damien Millerd8478b62011-05-29 21:39:36 +1000244 u_int len, i;
Darren Tucker1629c072007-02-19 22:25:37 +1100245 ServerOptions *newopts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000246
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000247 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000248
249 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000250 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000251
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000252 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000253
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000254 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000255 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000256
257 if (buffer_get_char(&m) == 0) {
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100258 pw = NULL;
259 goto out;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000260 }
Darren Tucker1629c072007-02-19 22:25:37 +1100261 pw = buffer_get_string(&m, &len);
262 if (len != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000263 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000264 pw->pw_name = buffer_get_string(&m, NULL);
265 pw->pw_passwd = buffer_get_string(&m, NULL);
Damien Miller6332da22013-04-23 14:25:52 +1000266#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000267 pw->pw_gecos = buffer_get_string(&m, NULL);
Damien Miller6332da22013-04-23 14:25:52 +1000268#endif
269#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000270 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000271#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000272 pw->pw_dir = buffer_get_string(&m, NULL);
273 pw->pw_shell = buffer_get_string(&m, NULL);
Darren Tucker1629c072007-02-19 22:25:37 +1100274
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100275out:
Darren Tucker1629c072007-02-19 22:25:37 +1100276 /* copy options block as a Match directive may have changed some */
277 newopts = buffer_get_string(&m, &len);
278 if (len != sizeof(*newopts))
279 fatal("%s: option block size mismatch", __func__);
Damien Millerf2e407e2011-05-20 19:04:14 +1000280
281#define M_CP_STROPT(x) do { \
282 if (newopts->x != NULL) \
283 newopts->x = buffer_get_string(&m, NULL); \
284 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000285#define M_CP_STRARRAYOPT(x, nx) do { \
286 for (i = 0; i < newopts->nx; i++) \
287 newopts->x[i] = buffer_get_string(&m, NULL); \
288 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000289 /* See comment in servconf.h */
290 COPY_MATCH_STRING_OPTS();
291#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000292#undef M_CP_STRARRAYOPT
Damien Millerf2e407e2011-05-20 19:04:14 +1000293
Darren Tucker1629c072007-02-19 22:25:37 +1100294 copy_set_server_options(&options, newopts, 1);
Darren Tuckera627d422013-06-02 07:31:17 +1000295 free(newopts);
Darren Tucker1629c072007-02-19 22:25:37 +1100296
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000297 buffer_free(&m);
298
299 return (pw);
300}
301
Darren Tucker7eb3de02003-10-15 15:56:58 +1000302char *
303mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000304{
305 Buffer m;
306 char *banner;
307
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000308 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000309
310 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000311 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000312 buffer_clear(&m);
313
Darren Tucker7eb3de02003-10-15 15:56:58 +1000314 mm_request_receive_expect(pmonitor->m_recvfd,
315 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000316 banner = buffer_get_string(&m, NULL);
317 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000318
Darren Tucker7eb3de02003-10-15 15:56:58 +1000319 /* treat empty banner as missing banner */
320 if (strlen(banner) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000321 free(banner);
Darren Tucker7eb3de02003-10-15 15:56:58 +1000322 banner = NULL;
323 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000324 return (banner);
325}
326
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000327/* Inform the privileged process about service and style */
328
329void
330mm_inform_authserv(char *service, char *style)
331{
332 Buffer m;
333
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000334 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000335
336 buffer_init(&m);
337 buffer_put_cstring(&m, service);
338 buffer_put_cstring(&m, style ? style : "");
339
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000340 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000341
342 buffer_free(&m);
343}
344
345/* Do the password authentication */
346int
347mm_auth_password(Authctxt *authctxt, char *password)
348{
349 Buffer m;
350 int authenticated = 0;
351
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000352 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000353
354 buffer_init(&m);
355 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000356 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000358 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000359 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000360
361 authenticated = buffer_get_int(&m);
362
363 buffer_free(&m);
364
365 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000366 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000367 return (authenticated);
368}
369
370int
371mm_user_key_allowed(struct passwd *pw, Key *key)
372{
373 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
374}
375
376int
377mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
378 Key *key)
379{
380 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
381}
382
383int
384mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
385 char *host, Key *key)
386{
387 int ret;
388
389 key->type = KEY_RSA; /* XXX hack for key_to_blob */
390 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
391 key->type = KEY_RSA1;
392 return (ret);
393}
394
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000395int
396mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
397{
398 Buffer m;
399 u_char *blob;
400 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100401 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000402
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000403 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000404
405 /* Convert the key to a blob and the pass it over */
406 if (!key_to_blob(key, &blob, &len))
407 return (0);
408
409 buffer_init(&m);
410 buffer_put_int(&m, type);
411 buffer_put_cstring(&m, user ? user : "");
412 buffer_put_cstring(&m, host ? host : "");
413 buffer_put_string(&m, blob, len);
Darren Tuckera627d422013-06-02 07:31:17 +1000414 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000415
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000416 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000417
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000418 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000419 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000420
421 allowed = buffer_get_int(&m);
422
Damien Miller06ebedf2003-02-24 12:03:38 +1100423 /* fake forced command */
424 auth_clear_options();
425 have_forced = buffer_get_int(&m);
426 forced_command = have_forced ? xstrdup("true") : NULL;
427
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000428 buffer_free(&m);
429
430 return (allowed);
431}
432
433/*
434 * This key verify needs to send the key type along, because the
435 * privileged parent makes the decision if the key is allowed
436 * for authentication.
437 */
438
439int
440mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
441{
442 Buffer m;
443 u_char *blob;
444 u_int len;
445 int verified = 0;
446
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000447 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000448
449 /* Convert the key to a blob and the pass it over */
450 if (!key_to_blob(key, &blob, &len))
451 return (0);
452
453 buffer_init(&m);
454 buffer_put_string(&m, blob, len);
455 buffer_put_string(&m, sig, siglen);
456 buffer_put_string(&m, data, datalen);
Darren Tuckera627d422013-06-02 07:31:17 +1000457 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000458
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000459 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000460
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000461 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000462 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000463
464 verified = buffer_get_int(&m);
465
466 buffer_free(&m);
467
468 return (verified);
469}
470
471/* Export key state after authentication */
472Newkeys *
473mm_newkeys_from_blob(u_char *blob, int blen)
474{
475 Buffer b;
476 u_int len;
477 Newkeys *newkey = NULL;
478 Enc *enc;
479 Mac *mac;
480 Comp *comp;
481
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000482 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000483#ifdef DEBUG_PK
484 dump_base64(stderr, blob, blen);
485#endif
486 buffer_init(&b);
487 buffer_append(&b, blob, blen);
488
Damien Miller49c145c2013-11-07 13:35:39 +1100489 newkey = xcalloc(1, sizeof(*newkey));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000490 enc = &newkey->enc;
491 mac = &newkey->mac;
492 comp = &newkey->comp;
493
494 /* Enc structure */
495 enc->name = buffer_get_string(&b, NULL);
496 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
497 enc->enabled = buffer_get_int(&b);
498 enc->block_size = buffer_get_int(&b);
499 enc->key = buffer_get_string(&b, &enc->key_len);
Damien Miller1d75abf2013-01-09 16:12:19 +1100500 enc->iv = buffer_get_string(&b, &enc->iv_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000501
502 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000503 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000504 enc->name, enc->cipher);
505
506 /* Mac structure */
Damien Miller1d75abf2013-01-09 16:12:19 +1100507 if (cipher_authlen(enc->cipher) == 0) {
508 mac->name = buffer_get_string(&b, NULL);
509 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
510 fatal("%s: can not setup mac %s", __func__, mac->name);
511 mac->enabled = buffer_get_int(&b);
512 mac->key = buffer_get_string(&b, &len);
513 if (len > mac->key_len)
514 fatal("%s: bad mac key length: %u > %d", __func__, len,
515 mac->key_len);
516 mac->key_len = len;
517 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000518
519 /* Comp structure */
520 comp->type = buffer_get_int(&b);
521 comp->enabled = buffer_get_int(&b);
522 comp->name = buffer_get_string(&b, NULL);
523
524 len = buffer_len(&b);
525 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000526 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000527 buffer_free(&b);
528 return (newkey);
529}
530
531int
532mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
533{
534 Buffer b;
535 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000536 Enc *enc;
537 Mac *mac;
538 Comp *comp;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000539 Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000540
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000541 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000542
543 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000544 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000545 return 0;
546 }
547 enc = &newkey->enc;
548 mac = &newkey->mac;
549 comp = &newkey->comp;
550
551 buffer_init(&b);
552 /* Enc structure */
553 buffer_put_cstring(&b, enc->name);
554 /* The cipher struct is constant and shared, you export pointer */
555 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
556 buffer_put_int(&b, enc->enabled);
557 buffer_put_int(&b, enc->block_size);
558 buffer_put_string(&b, enc->key, enc->key_len);
Damien Miller1d75abf2013-01-09 16:12:19 +1100559 packet_get_keyiv(mode, enc->iv, enc->iv_len);
560 buffer_put_string(&b, enc->iv, enc->iv_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561
562 /* Mac structure */
Damien Miller1d75abf2013-01-09 16:12:19 +1100563 if (cipher_authlen(enc->cipher) == 0) {
564 buffer_put_cstring(&b, mac->name);
565 buffer_put_int(&b, mac->enabled);
566 buffer_put_string(&b, mac->key, mac->key_len);
567 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000568
569 /* Comp structure */
570 buffer_put_int(&b, comp->type);
571 buffer_put_int(&b, comp->enabled);
572 buffer_put_cstring(&b, comp->name);
573
574 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000575 if (lenp != NULL)
576 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000577 if (blobp != NULL) {
578 *blobp = xmalloc(len);
579 memcpy(*blobp, buffer_ptr(&b), len);
580 }
Damien Millera5103f42014-02-04 11:20:14 +1100581 explicit_bzero(buffer_ptr(&b), len);
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000582 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000583 return len;
584}
585
586static void
587mm_send_kex(Buffer *m, Kex *kex)
588{
589 buffer_put_string(m, kex->session_id, kex->session_id_len);
590 buffer_put_int(m, kex->we_need);
591 buffer_put_int(m, kex->hostkey_type);
592 buffer_put_int(m, kex->kex_type);
593 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
594 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
595 buffer_put_int(m, kex->flags);
596 buffer_put_cstring(m, kex->client_version_string);
597 buffer_put_cstring(m, kex->server_version_string);
598}
599
600void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000601mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000602{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000603 Buffer m, *input, *output;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604 u_char *blob, *p;
605 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000606 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000607 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000608
609 buffer_init(&m);
610
611 if (!compat20) {
612 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000613 u_char *key;
614 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000615
616 buffer_put_int(&m, packet_get_protocol_flags());
617
618 buffer_put_int(&m, packet_get_ssh1_cipher());
619
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000620 debug3("%s: Sending ssh1 KEY+IV", __func__);
621 keylen = packet_get_encryption_key(NULL);
622 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
623 keylen = packet_get_encryption_key(key);
624 buffer_put_string(&m, key, keylen);
Damien Millera5103f42014-02-04 11:20:14 +1100625 explicit_bzero(key, keylen);
Darren Tuckera627d422013-06-02 07:31:17 +1000626 free(key);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000627
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000628 ivlen = packet_get_keyiv_len(MODE_OUT);
629 packet_get_keyiv(MODE_OUT, iv, ivlen);
630 buffer_put_string(&m, iv, ivlen);
Darren Tucker3a7c0412012-10-05 10:51:59 +1000631 ivlen = packet_get_keyiv_len(MODE_IN);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000632 packet_get_keyiv(MODE_IN, iv, ivlen);
633 buffer_put_string(&m, iv, ivlen);
634 goto skip;
635 } else {
636 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000637 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000638 }
639
640 debug3("%s: Sending new keys: %p %p",
Darren Tuckerf7288d72009-06-21 18:12:20 +1000641 __func__, packet_get_newkeys(MODE_OUT),
642 packet_get_newkeys(MODE_IN));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000643
644 /* Keys from Kex */
645 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000646 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000647
648 buffer_put_string(&m, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +1000649 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000650
651 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000652 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000653
654 buffer_put_string(&m, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +1000655 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000656
Damien Millerb61f3fc2008-07-11 17:36:48 +1000657 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000658 buffer_put_int(&m, seqnr);
659 buffer_put_int64(&m, blocks);
660 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000661 buffer_put_int64(&m, bytes);
662 packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000663 buffer_put_int(&m, seqnr);
664 buffer_put_int64(&m, blocks);
665 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000666 buffer_put_int64(&m, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000667
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000668 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669 skip:
670 /* More key context */
671 plen = packet_get_keycontext(MODE_OUT, NULL);
672 p = xmalloc(plen+1);
673 packet_get_keycontext(MODE_OUT, p);
674 buffer_put_string(&m, p, plen);
Darren Tuckera627d422013-06-02 07:31:17 +1000675 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000676
677 plen = packet_get_keycontext(MODE_IN, NULL);
678 p = xmalloc(plen+1);
679 packet_get_keycontext(MODE_IN, p);
680 buffer_put_string(&m, p, plen);
Darren Tuckera627d422013-06-02 07:31:17 +1000681 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000682
683 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000684 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
686 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
687
688 /* Network I/O buffers */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000689 input = (Buffer *)packet_get_input();
690 output = (Buffer *)packet_get_output();
691 buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
692 buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000693
Darren Tuckerc5564e12009-06-21 18:53:53 +1000694 /* Roaming */
695 if (compat20) {
696 buffer_put_int64(&m, get_sent_bytes());
697 buffer_put_int64(&m, get_recv_bytes());
698 }
699
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000700 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000701 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000702
703 buffer_free(&m);
704}
705
706int
Damien Miller71a73672006-03-26 14:04:36 +1100707mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000708{
709 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000710 char *p, *msg;
Damien Miller7207f642008-05-19 15:34:50 +1000711 int success = 0, tmp1 = -1, tmp2 = -1;
712
713 /* Kludge: ensure there are fds free to receive the pty/tty */
714 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
715 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
716 error("%s: cannot allocate fds for pty", __func__);
717 if (tmp1 > 0)
718 close(tmp1);
719 if (tmp2 > 0)
720 close(tmp2);
721 return 0;
722 }
723 close(tmp1);
724 close(tmp2);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725
726 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000727 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000729 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000730 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000731
732 success = buffer_get_int(&m);
733 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000734 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000735 buffer_free(&m);
736 return (0);
737 }
738 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000739 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000740 buffer_free(&m);
741
742 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
Darren Tuckera627d422013-06-02 07:31:17 +1000743 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000744
Darren Tucker09991742004-07-17 17:05:14 +1000745 buffer_append(&loginmsg, msg, strlen(msg));
Darren Tuckera627d422013-06-02 07:31:17 +1000746 free(msg);
Darren Tucker09991742004-07-17 17:05:14 +1000747
Damien Miller54fd7cf2007-09-17 12:04:08 +1000748 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
749 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
750 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000751
752 /* Success */
753 return (1);
754}
755
756void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000757mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000758{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000759 Buffer m;
760
761 if (s->ttyfd == -1)
762 return;
763 buffer_init(&m);
764 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000765 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000766 buffer_free(&m);
767
768 /* closed dup'ed master */
Damien Miller7207f642008-05-19 15:34:50 +1000769 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
770 error("close(s->ptymaster/%d): %s",
771 s->ptymaster, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000772
773 /* unlink pty from session */
774 s->ttyfd = -1;
775}
776
Damien Miller79418552002-04-23 20:28:48 +1000777#ifdef USE_PAM
778void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100779mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000780{
781 Buffer m;
782
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000783 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000784 if (!options.use_pam)
785 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000786
787 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000788 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000789
790 buffer_free(&m);
791}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000792
Damien Miller1f499fd2003-08-25 13:08:49 +1000793u_int
794mm_do_pam_account(void)
795{
796 Buffer m;
797 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000798 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000799
800 debug3("%s entering", __func__);
801 if (!options.use_pam)
802 fatal("UsePAM=no, but ended up in %s anyway", __func__);
803
804 buffer_init(&m);
805 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
806
Damien Millera8e06ce2003-11-21 23:48:55 +1100807 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000808 MONITOR_ANS_PAM_ACCOUNT, &m);
809 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000810 msg = buffer_get_string(&m, NULL);
811 buffer_append(&loginmsg, msg, strlen(msg));
Darren Tuckerf60845f2013-06-02 08:07:31 +1000812 free(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000813
814 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100815
Damien Miller1f499fd2003-08-25 13:08:49 +1000816 debug3("%s returning %d", __func__, ret);
817
818 return (ret);
819}
820
Damien Miller4f9f42a2003-05-10 19:28:02 +1000821void *
822mm_sshpam_init_ctx(Authctxt *authctxt)
823{
824 Buffer m;
825 int success;
826
827 debug3("%s", __func__);
828 buffer_init(&m);
829 buffer_put_cstring(&m, authctxt->user);
830 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
831 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
832 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
833 success = buffer_get_int(&m);
834 if (success == 0) {
835 debug3("%s: pam_init_ctx failed", __func__);
836 buffer_free(&m);
837 return (NULL);
838 }
839 buffer_free(&m);
840 return (authctxt);
841}
842
843int
844mm_sshpam_query(void *ctx, char **name, char **info,
845 u_int *num, char ***prompts, u_int **echo_on)
846{
847 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000848 u_int i;
849 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000850
851 debug3("%s", __func__);
852 buffer_init(&m);
853 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
854 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
855 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
856 ret = buffer_get_int(&m);
857 debug3("%s: pam_query returned %d", __func__, ret);
858 *name = buffer_get_string(&m, NULL);
859 *info = buffer_get_string(&m, NULL);
860 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000861 if (*num > PAM_MAX_NUM_MSG)
862 fatal("%s: recieved %u PAM messages, expected <= %u",
863 __func__, *num, PAM_MAX_NUM_MSG);
864 *prompts = xcalloc((*num + 1), sizeof(char *));
865 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000866 for (i = 0; i < *num; ++i) {
867 (*prompts)[i] = buffer_get_string(&m, NULL);
868 (*echo_on)[i] = buffer_get_int(&m);
869 }
870 buffer_free(&m);
871 return (ret);
872}
873
874int
875mm_sshpam_respond(void *ctx, u_int num, char **resp)
876{
877 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000878 u_int i;
879 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000880
881 debug3("%s", __func__);
882 buffer_init(&m);
883 buffer_put_int(&m, num);
884 for (i = 0; i < num; ++i)
885 buffer_put_cstring(&m, resp[i]);
886 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
887 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
888 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
889 ret = buffer_get_int(&m);
890 debug3("%s: pam_respond returned %d", __func__, ret);
891 buffer_free(&m);
892 return (ret);
893}
894
895void
896mm_sshpam_free_ctx(void *ctxtp)
897{
898 Buffer m;
899
900 debug3("%s", __func__);
901 buffer_init(&m);
902 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
903 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
904 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
905 buffer_free(&m);
906}
Damien Miller79418552002-04-23 20:28:48 +1000907#endif /* USE_PAM */
908
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909/* Request process termination */
910
911void
912mm_terminate(void)
913{
914 Buffer m;
915
916 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000917 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000918 buffer_free(&m);
919}
920
Damien Miller1f0311c2014-05-15 14:24:09 +1000921#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000922int
923mm_ssh1_session_key(BIGNUM *num)
924{
925 int rsafail;
926 Buffer m;
927
928 buffer_init(&m);
929 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000930 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000931
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000932 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000933
934 rsafail = buffer_get_int(&m);
935 buffer_get_bignum2(&m, num);
936
937 buffer_free(&m);
938
939 return (rsafail);
940}
Damien Miller1f0311c2014-05-15 14:24:09 +1000941#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000942
943static void
944mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
945 char ***prompts, u_int **echo_on)
946{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000947 *name = xstrdup("");
948 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000949 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100950 *prompts = xcalloc(*numprompts, sizeof(char *));
951 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000952 (*echo_on)[0] = 0;
953}
954
955int
956mm_bsdauth_query(void *ctx, char **name, char **infotxt,
957 u_int *numprompts, char ***prompts, u_int **echo_on)
958{
959 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100960 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961 char *challenge;
962
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000963 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000964
965 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000966 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000967
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000968 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000969 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100970 success = buffer_get_int(&m);
971 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000972 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000973 buffer_free(&m);
974 return (-1);
975 }
976
977 /* Get the challenge, and format the response */
978 challenge = buffer_get_string(&m, NULL);
979 buffer_free(&m);
980
981 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
982 (*prompts)[0] = challenge;
983
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000984 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000985
986 return (0);
987}
988
989int
990mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
991{
992 Buffer m;
993 int authok;
994
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000995 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000996 if (numresponses != 1)
997 return (-1);
998
999 buffer_init(&m);
1000 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001001 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001002
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001003 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001004 MONITOR_ANS_BSDAUTHRESPOND, &m);
1005
1006 authok = buffer_get_int(&m);
1007 buffer_free(&m);
1008
1009 return ((authok == 0) ? -1 : 0);
1010}
1011
Darren Tucker042e2e82004-07-08 23:09:42 +10001012#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001013int
1014mm_skey_query(void *ctx, char **name, char **infotxt,
1015 u_int *numprompts, char ***prompts, u_int **echo_on)
1016{
1017 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +11001018 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +10001019 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001020
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001021 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001022
1023 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001024 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001025
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001026 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001027 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +11001028 success = buffer_get_int(&m);
1029 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001030 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001031 buffer_free(&m);
1032 return (-1);
1033 }
1034
1035 /* Get the challenge, and format the response */
1036 challenge = buffer_get_string(&m, NULL);
1037 buffer_free(&m);
1038
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001039 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001040
1041 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
1042
Damien Miller07d86be2006-03-26 14:19:21 +11001043 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001044 free(challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001045
1046 return (0);
1047}
1048
1049int
1050mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1051{
1052 Buffer m;
1053 int authok;
1054
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001055 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001056 if (numresponses != 1)
1057 return (-1);
1058
1059 buffer_init(&m);
1060 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001061 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001062
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001063 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001064 MONITOR_ANS_SKEYRESPOND, &m);
1065
1066 authok = buffer_get_int(&m);
1067 buffer_free(&m);
1068
1069 return ((authok == 0) ? -1 : 0);
1070}
Darren Tucker042e2e82004-07-08 23:09:42 +10001071#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001072
1073void
1074mm_ssh1_session_id(u_char session_id[16])
1075{
1076 Buffer m;
1077 int i;
1078
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001079 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001080
1081 buffer_init(&m);
1082 for (i = 0; i < 16; i++)
1083 buffer_put_char(&m, session_id[i]);
1084
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001085 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001086 buffer_free(&m);
1087}
1088
Damien Miller1f0311c2014-05-15 14:24:09 +10001089#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001090int
1091mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1092{
1093 Buffer m;
1094 Key *key;
1095 u_char *blob;
1096 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001097 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001098
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001099 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001100
1101 buffer_init(&m);
1102 buffer_put_bignum2(&m, client_n);
1103
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001104 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1105 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001106
1107 allowed = buffer_get_int(&m);
1108
Damien Miller06ebedf2003-02-24 12:03:38 +11001109 /* fake forced command */
1110 auth_clear_options();
1111 have_forced = buffer_get_int(&m);
1112 forced_command = have_forced ? xstrdup("true") : NULL;
1113
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001114 if (allowed && rkey != NULL) {
1115 blob = buffer_get_string(&m, &blen);
1116 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001117 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001118 *rkey = key;
Darren Tuckera627d422013-06-02 07:31:17 +10001119 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001120 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001121 buffer_free(&m);
1122
1123 return (allowed);
1124}
1125
1126BIGNUM *
1127mm_auth_rsa_generate_challenge(Key *key)
1128{
1129 Buffer m;
1130 BIGNUM *challenge;
1131 u_char *blob;
1132 u_int blen;
1133
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001134 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001135
1136 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001137 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001138
1139 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1140 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001141 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001142 key->type = KEY_RSA1;
1143
1144 buffer_init(&m);
1145 buffer_put_string(&m, blob, blen);
Darren Tuckera627d422013-06-02 07:31:17 +10001146 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001147
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001148 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1149 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001150
1151 buffer_get_bignum2(&m, challenge);
1152 buffer_free(&m);
1153
1154 return (challenge);
1155}
1156
1157int
1158mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1159{
1160 Buffer m;
1161 u_char *blob;
1162 u_int blen;
1163 int success = 0;
1164
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001165 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001166
1167 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1168 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001169 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001170 key->type = KEY_RSA1;
1171
1172 buffer_init(&m);
1173 buffer_put_string(&m, blob, blen);
1174 buffer_put_string(&m, response, 16);
Darren Tuckera627d422013-06-02 07:31:17 +10001175 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001176
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001177 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1178 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001179
1180 success = buffer_get_int(&m);
1181 buffer_free(&m);
1182
1183 return (success);
1184}
Damien Miller1f0311c2014-05-15 14:24:09 +10001185#endif
Damien Miller25162f22002-09-12 09:47:29 +10001186
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001187#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001188void
1189mm_audit_event(ssh_audit_event_t event)
1190{
1191 Buffer m;
1192
1193 debug3("%s entering", __func__);
1194
1195 buffer_init(&m);
1196 buffer_put_int(&m, event);
1197
1198 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1199 buffer_free(&m);
1200}
1201
1202void
1203mm_audit_run_command(const char *command)
1204{
1205 Buffer m;
1206
1207 debug3("%s entering command %s", __func__, command);
1208
1209 buffer_init(&m);
1210 buffer_put_cstring(&m, command);
1211
1212 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1213 buffer_free(&m);
1214}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001215#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001216
Darren Tucker0efd1552003-08-26 11:49:55 +10001217#ifdef GSSAPI
1218OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001219mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001220{
1221 Buffer m;
1222 OM_uint32 major;
1223
1224 /* Client doesn't get to see the context */
1225 *ctx = NULL;
1226
1227 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001228 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001229
1230 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1231 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1232
1233 major = buffer_get_int(&m);
1234
1235 buffer_free(&m);
1236 return (major);
1237}
1238
1239OM_uint32
1240mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1241 gss_buffer_desc *out, OM_uint32 *flags)
1242{
1243 Buffer m;
1244 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001245 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001246
1247 buffer_init(&m);
1248 buffer_put_string(&m, in->value, in->length);
1249
1250 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1251 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1252
1253 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001254 out->value = buffer_get_string(&m, &len);
1255 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001256 if (flags)
1257 *flags = buffer_get_int(&m);
1258
1259 buffer_free(&m);
1260
1261 return (major);
1262}
1263
Damien Miller0425d402003-11-17 22:18:21 +11001264OM_uint32
1265mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1266{
1267 Buffer m;
1268 OM_uint32 major;
1269
1270 buffer_init(&m);
1271 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1272 buffer_put_string(&m, gssmic->value, gssmic->length);
1273
1274 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1275 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1276 &m);
1277
1278 major = buffer_get_int(&m);
1279 buffer_free(&m);
1280 return(major);
1281}
1282
Darren Tucker0efd1552003-08-26 11:49:55 +10001283int
1284mm_ssh_gssapi_userok(char *user)
1285{
1286 Buffer m;
1287 int authenticated = 0;
1288
1289 buffer_init(&m);
1290
1291 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1292 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1293 &m);
1294
1295 authenticated = buffer_get_int(&m);
1296
1297 buffer_free(&m);
1298 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1299 return (authenticated);
1300}
1301#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001302