blob: 88ff68330ae9097a2255644eff1e324f0b70d510 [file] [log] [blame]
Darren Tuckera627d422013-06-02 07:31:17 +10001/* $OpenBSD: monitor_wrap.c,v 1.76 2013/05/17 00:13:13 djm Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029
Damien Miller9f2abc42006-07-10 20:53:08 +100030#include <sys/types.h>
Darren Tucker1a3d6e72006-08-05 18:46:47 +100031#include <sys/uio.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100032
Darren Tucker39972492006-07-12 22:22:46 +100033#include <errno.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100034#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100035#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100036#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100037#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100038#include <string.h>
Darren Tucker1a3d6e72006-08-05 18:46:47 +100039#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100040
Damien Millerd7834352006-08-05 12:39:39 +100041#include <openssl/bn.h>
42#include <openssl/dh.h>
Damien Miller01ed2272008-11-05 16:20:46 +110043#include <openssl/evp.h>
Damien Millerd7834352006-08-05 12:39:39 +100044
Damien Millerb84886b2008-05-19 15:05:07 +100045#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100046#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000047#include "ssh.h"
48#include "dh.h"
Damien Millerd7834352006-08-05 12:39:39 +100049#include "buffer.h"
50#include "key.h"
51#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000052#include "kex.h"
Damien Millerd7834352006-08-05 12:39:39 +100053#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000054#include "auth.h"
Damien Miller06ebedf2003-02-24 12:03:38 +110055#include "auth-options.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000056#include "packet.h"
57#include "mac.h"
58#include "log.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000059#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
60#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000061#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000062#define TARGET_OS_MAC 1
63#else
64#include "zlib.h"
65#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000066#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +100067#ifdef GSSAPI
68#include "ssh-gss.h"
69#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "monitor_wrap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000071#include "atomicio.h"
72#include "monitor_fdpass.h"
Damien Miller3f941882006-03-31 23:13:02 +110073#include "misc.h"
Damien Millercee85232009-03-06 00:58:22 +110074#include "schnorr.h"
Damien Miller01ed2272008-11-05 16:20:46 +110075#include "jpake.h"
Damien Millereb8b60e2010-08-31 22:41:14 +100076#include "uuencode.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000077
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000078#include "channels.h"
79#include "session.h"
Darren Tucker1629c072007-02-19 22:25:37 +110080#include "servconf.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +100081#include "roaming.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000082
83/* Imports */
84extern int compat20;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000085extern z_stream incoming_stream;
86extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000087extern struct monitor *pmonitor;
Darren Tucker09991742004-07-17 17:05:14 +100088extern Buffer loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100089extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000090
Damien Miller8f0bf232011-06-20 14:42:23 +100091void
92mm_log_handler(LogLevel level, const char *msg, void *ctx)
93{
94 Buffer log_msg;
95 struct monitor *mon = (struct monitor *)ctx;
96
97 if (mon->m_log_sendfd == -1)
98 fatal("%s: no log channel", __func__);
99
100 buffer_init(&log_msg);
101 /*
102 * Placeholder for packet length. Will be filled in with the actual
103 * packet length once the packet has been constucted. This saves
104 * fragile math.
105 */
106 buffer_put_int(&log_msg, 0);
107
108 buffer_put_int(&log_msg, level);
109 buffer_put_cstring(&log_msg, msg);
110 put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4);
111 if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg),
112 buffer_len(&log_msg)) != buffer_len(&log_msg))
113 fatal("%s: write: %s", __func__, strerror(errno));
114 buffer_free(&log_msg);
115}
116
Darren Tucker3e33cec2003-10-02 16:12:36 +1000117int
118mm_is_monitor(void)
119{
120 /*
121 * m_pid is only set in the privileged part, and
122 * points to the unprivileged child.
123 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +1100124 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000125}
126
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000127void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000128mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000129{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000130 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +0000131 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000132
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000133 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000134
Damien Miller3f941882006-03-31 23:13:02 +1100135 put_u32(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000136 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000137 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +1000138 fatal("%s: write: %s", __func__, strerror(errno));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000139 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +1000140 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000141}
142
143void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000144mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000145{
146 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000147 u_int msg_len;
148
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000149 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000150
Damien Millerb253cc42005-05-26 12:23:44 +1000151 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
152 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000153 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000154 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000155 }
Damien Miller3f941882006-03-31 23:13:02 +1100156 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000157 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000158 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000159 buffer_clear(m);
160 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000161 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
162 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000163}
164
165void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000166mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000167{
168 u_char rtype;
169
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000170 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000172 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000173 rtype = buffer_get_char(m);
174 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000175 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000176 rtype, type);
177}
178
179DH *
180mm_choose_dh(int min, int nbits, int max)
181{
182 BIGNUM *p, *g;
183 int success = 0;
184 Buffer m;
185
186 buffer_init(&m);
187 buffer_put_int(&m, min);
188 buffer_put_int(&m, nbits);
189 buffer_put_int(&m, max);
190
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000191 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000192
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000193 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000194 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000195
196 success = buffer_get_char(&m);
197 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000198 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000199
200 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000201 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000203 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000204 buffer_get_bignum2(&m, p);
205 buffer_get_bignum2(&m, g);
206
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000207 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000208 buffer_free(&m);
209
210 return (dh_new_group(g, p));
211}
212
213int
214mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
215{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000216 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000217 Buffer m;
218
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000219 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000220
221 buffer_init(&m);
222 buffer_put_int(&m, kex->host_key_index(key));
223 buffer_put_string(&m, data, datalen);
224
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000225 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000226
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000227 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000228 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000229 *sigp = buffer_get_string(&m, lenp);
230 buffer_free(&m);
231
232 return (0);
233}
234
235struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000236mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000237{
238 Buffer m;
239 struct passwd *pw;
Damien Millerd8478b62011-05-29 21:39:36 +1000240 u_int len, i;
Darren Tucker1629c072007-02-19 22:25:37 +1100241 ServerOptions *newopts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000242
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000243 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000244
245 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000246 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000247
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000248 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000249
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000250 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000251 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000252
253 if (buffer_get_char(&m) == 0) {
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100254 pw = NULL;
255 goto out;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000256 }
Darren Tucker1629c072007-02-19 22:25:37 +1100257 pw = buffer_get_string(&m, &len);
258 if (len != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000259 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000260 pw->pw_name = buffer_get_string(&m, NULL);
261 pw->pw_passwd = buffer_get_string(&m, NULL);
Damien Miller6332da22013-04-23 14:25:52 +1000262#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000263 pw->pw_gecos = buffer_get_string(&m, NULL);
Damien Miller6332da22013-04-23 14:25:52 +1000264#endif
265#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000266 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000267#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000268 pw->pw_dir = buffer_get_string(&m, NULL);
269 pw->pw_shell = buffer_get_string(&m, NULL);
Darren Tucker1629c072007-02-19 22:25:37 +1100270
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100271out:
Darren Tucker1629c072007-02-19 22:25:37 +1100272 /* copy options block as a Match directive may have changed some */
273 newopts = buffer_get_string(&m, &len);
274 if (len != sizeof(*newopts))
275 fatal("%s: option block size mismatch", __func__);
Damien Millerf2e407e2011-05-20 19:04:14 +1000276
277#define M_CP_STROPT(x) do { \
278 if (newopts->x != NULL) \
279 newopts->x = buffer_get_string(&m, NULL); \
280 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000281#define M_CP_STRARRAYOPT(x, nx) do { \
282 for (i = 0; i < newopts->nx; i++) \
283 newopts->x[i] = buffer_get_string(&m, NULL); \
284 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000285 /* See comment in servconf.h */
286 COPY_MATCH_STRING_OPTS();
287#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000288#undef M_CP_STRARRAYOPT
Damien Millerf2e407e2011-05-20 19:04:14 +1000289
Darren Tucker1629c072007-02-19 22:25:37 +1100290 copy_set_server_options(&options, newopts, 1);
Darren Tuckera627d422013-06-02 07:31:17 +1000291 free(newopts);
Darren Tucker1629c072007-02-19 22:25:37 +1100292
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000293 buffer_free(&m);
294
295 return (pw);
296}
297
Darren Tucker7eb3de02003-10-15 15:56:58 +1000298char *
299mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000300{
301 Buffer m;
302 char *banner;
303
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000304 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000305
306 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000307 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000308 buffer_clear(&m);
309
Darren Tucker7eb3de02003-10-15 15:56:58 +1000310 mm_request_receive_expect(pmonitor->m_recvfd,
311 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000312 banner = buffer_get_string(&m, NULL);
313 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000314
Darren Tucker7eb3de02003-10-15 15:56:58 +1000315 /* treat empty banner as missing banner */
316 if (strlen(banner) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000317 free(banner);
Darren Tucker7eb3de02003-10-15 15:56:58 +1000318 banner = NULL;
319 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000320 return (banner);
321}
322
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000323/* Inform the privileged process about service and style */
324
325void
326mm_inform_authserv(char *service, char *style)
327{
328 Buffer m;
329
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000330 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000331
332 buffer_init(&m);
333 buffer_put_cstring(&m, service);
334 buffer_put_cstring(&m, style ? style : "");
335
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000336 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000337
338 buffer_free(&m);
339}
340
341/* Do the password authentication */
342int
343mm_auth_password(Authctxt *authctxt, char *password)
344{
345 Buffer m;
346 int authenticated = 0;
347
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000348 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000349
350 buffer_init(&m);
351 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000352 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000353
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000354 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000355 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356
357 authenticated = buffer_get_int(&m);
358
359 buffer_free(&m);
360
361 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000362 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000363 return (authenticated);
364}
365
366int
367mm_user_key_allowed(struct passwd *pw, Key *key)
368{
369 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
370}
371
372int
373mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
374 Key *key)
375{
376 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
377}
378
379int
380mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
381 char *host, Key *key)
382{
383 int ret;
384
385 key->type = KEY_RSA; /* XXX hack for key_to_blob */
386 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
387 key->type = KEY_RSA1;
388 return (ret);
389}
390
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000391int
392mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
393{
394 Buffer m;
395 u_char *blob;
396 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100397 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000398
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000399 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000400
401 /* Convert the key to a blob and the pass it over */
402 if (!key_to_blob(key, &blob, &len))
403 return (0);
404
405 buffer_init(&m);
406 buffer_put_int(&m, type);
407 buffer_put_cstring(&m, user ? user : "");
408 buffer_put_cstring(&m, host ? host : "");
409 buffer_put_string(&m, blob, len);
Darren Tuckera627d422013-06-02 07:31:17 +1000410 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000411
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000412 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000413
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000414 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000415 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000416
417 allowed = buffer_get_int(&m);
418
Damien Miller06ebedf2003-02-24 12:03:38 +1100419 /* fake forced command */
420 auth_clear_options();
421 have_forced = buffer_get_int(&m);
422 forced_command = have_forced ? xstrdup("true") : NULL;
423
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000424 buffer_free(&m);
425
426 return (allowed);
427}
428
429/*
430 * This key verify needs to send the key type along, because the
431 * privileged parent makes the decision if the key is allowed
432 * for authentication.
433 */
434
435int
436mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
437{
438 Buffer m;
439 u_char *blob;
440 u_int len;
441 int verified = 0;
442
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000443 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000444
445 /* Convert the key to a blob and the pass it over */
446 if (!key_to_blob(key, &blob, &len))
447 return (0);
448
449 buffer_init(&m);
450 buffer_put_string(&m, blob, len);
451 buffer_put_string(&m, sig, siglen);
452 buffer_put_string(&m, data, datalen);
Darren Tuckera627d422013-06-02 07:31:17 +1000453 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000454
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000455 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000456
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000457 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000458 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000459
460 verified = buffer_get_int(&m);
461
462 buffer_free(&m);
463
464 return (verified);
465}
466
467/* Export key state after authentication */
468Newkeys *
469mm_newkeys_from_blob(u_char *blob, int blen)
470{
471 Buffer b;
472 u_int len;
473 Newkeys *newkey = NULL;
474 Enc *enc;
475 Mac *mac;
476 Comp *comp;
477
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000478 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000479#ifdef DEBUG_PK
480 dump_base64(stderr, blob, blen);
481#endif
482 buffer_init(&b);
483 buffer_append(&b, blob, blen);
484
485 newkey = xmalloc(sizeof(*newkey));
486 enc = &newkey->enc;
487 mac = &newkey->mac;
488 comp = &newkey->comp;
489
490 /* Enc structure */
491 enc->name = buffer_get_string(&b, NULL);
492 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
493 enc->enabled = buffer_get_int(&b);
494 enc->block_size = buffer_get_int(&b);
495 enc->key = buffer_get_string(&b, &enc->key_len);
Damien Miller1d75abf2013-01-09 16:12:19 +1100496 enc->iv = buffer_get_string(&b, &enc->iv_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000497
498 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000499 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000500 enc->name, enc->cipher);
501
502 /* Mac structure */
Damien Miller1d75abf2013-01-09 16:12:19 +1100503 if (cipher_authlen(enc->cipher) == 0) {
504 mac->name = buffer_get_string(&b, NULL);
505 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
506 fatal("%s: can not setup mac %s", __func__, mac->name);
507 mac->enabled = buffer_get_int(&b);
508 mac->key = buffer_get_string(&b, &len);
509 if (len > mac->key_len)
510 fatal("%s: bad mac key length: %u > %d", __func__, len,
511 mac->key_len);
512 mac->key_len = len;
513 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000514
515 /* Comp structure */
516 comp->type = buffer_get_int(&b);
517 comp->enabled = buffer_get_int(&b);
518 comp->name = buffer_get_string(&b, NULL);
519
520 len = buffer_len(&b);
521 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000522 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523 buffer_free(&b);
524 return (newkey);
525}
526
527int
528mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
529{
530 Buffer b;
531 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000532 Enc *enc;
533 Mac *mac;
534 Comp *comp;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000535 Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000536
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000537 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000538
539 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000540 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000541 return 0;
542 }
543 enc = &newkey->enc;
544 mac = &newkey->mac;
545 comp = &newkey->comp;
546
547 buffer_init(&b);
548 /* Enc structure */
549 buffer_put_cstring(&b, enc->name);
550 /* The cipher struct is constant and shared, you export pointer */
551 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
552 buffer_put_int(&b, enc->enabled);
553 buffer_put_int(&b, enc->block_size);
554 buffer_put_string(&b, enc->key, enc->key_len);
Damien Miller1d75abf2013-01-09 16:12:19 +1100555 packet_get_keyiv(mode, enc->iv, enc->iv_len);
556 buffer_put_string(&b, enc->iv, enc->iv_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000557
558 /* Mac structure */
Damien Miller1d75abf2013-01-09 16:12:19 +1100559 if (cipher_authlen(enc->cipher) == 0) {
560 buffer_put_cstring(&b, mac->name);
561 buffer_put_int(&b, mac->enabled);
562 buffer_put_string(&b, mac->key, mac->key_len);
563 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000564
565 /* Comp structure */
566 buffer_put_int(&b, comp->type);
567 buffer_put_int(&b, comp->enabled);
568 buffer_put_cstring(&b, comp->name);
569
570 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000571 if (lenp != NULL)
572 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000573 if (blobp != NULL) {
574 *blobp = xmalloc(len);
575 memcpy(*blobp, buffer_ptr(&b), len);
576 }
577 memset(buffer_ptr(&b), 0, len);
578 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000579 return len;
580}
581
582static void
583mm_send_kex(Buffer *m, Kex *kex)
584{
585 buffer_put_string(m, kex->session_id, kex->session_id_len);
586 buffer_put_int(m, kex->we_need);
587 buffer_put_int(m, kex->hostkey_type);
588 buffer_put_int(m, kex->kex_type);
589 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
590 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
591 buffer_put_int(m, kex->flags);
592 buffer_put_cstring(m, kex->client_version_string);
593 buffer_put_cstring(m, kex->server_version_string);
594}
595
596void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000597mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000599 Buffer m, *input, *output;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000600 u_char *blob, *p;
601 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000602 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000603 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604
605 buffer_init(&m);
606
607 if (!compat20) {
608 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000609 u_char *key;
610 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000611
612 buffer_put_int(&m, packet_get_protocol_flags());
613
614 buffer_put_int(&m, packet_get_ssh1_cipher());
615
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000616 debug3("%s: Sending ssh1 KEY+IV", __func__);
617 keylen = packet_get_encryption_key(NULL);
618 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
619 keylen = packet_get_encryption_key(key);
620 buffer_put_string(&m, key, keylen);
621 memset(key, 0, keylen);
Darren Tuckera627d422013-06-02 07:31:17 +1000622 free(key);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000623
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000624 ivlen = packet_get_keyiv_len(MODE_OUT);
625 packet_get_keyiv(MODE_OUT, iv, ivlen);
626 buffer_put_string(&m, iv, ivlen);
Darren Tucker3a7c0412012-10-05 10:51:59 +1000627 ivlen = packet_get_keyiv_len(MODE_IN);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000628 packet_get_keyiv(MODE_IN, iv, ivlen);
629 buffer_put_string(&m, iv, ivlen);
630 goto skip;
631 } else {
632 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000633 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000634 }
635
636 debug3("%s: Sending new keys: %p %p",
Darren Tuckerf7288d72009-06-21 18:12:20 +1000637 __func__, packet_get_newkeys(MODE_OUT),
638 packet_get_newkeys(MODE_IN));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000639
640 /* Keys from Kex */
641 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000642 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000643
644 buffer_put_string(&m, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +1000645 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000646
647 if (!mm_newkeys_to_blob(MODE_IN, &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
Damien Millerb61f3fc2008-07-11 17:36:48 +1000653 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000654 buffer_put_int(&m, seqnr);
655 buffer_put_int64(&m, blocks);
656 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000657 buffer_put_int64(&m, bytes);
658 packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000659 buffer_put_int(&m, seqnr);
660 buffer_put_int64(&m, blocks);
661 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000662 buffer_put_int64(&m, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000664 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000665 skip:
666 /* More key context */
667 plen = packet_get_keycontext(MODE_OUT, NULL);
668 p = xmalloc(plen+1);
669 packet_get_keycontext(MODE_OUT, p);
670 buffer_put_string(&m, p, plen);
Darren Tuckera627d422013-06-02 07:31:17 +1000671 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000672
673 plen = packet_get_keycontext(MODE_IN, NULL);
674 p = xmalloc(plen+1);
675 packet_get_keycontext(MODE_IN, 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 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000680 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000681 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
682 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
683
684 /* Network I/O buffers */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000685 input = (Buffer *)packet_get_input();
686 output = (Buffer *)packet_get_output();
687 buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
688 buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000689
Darren Tuckerc5564e12009-06-21 18:53:53 +1000690 /* Roaming */
691 if (compat20) {
692 buffer_put_int64(&m, get_sent_bytes());
693 buffer_put_int64(&m, get_recv_bytes());
694 }
695
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000696 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000697 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000698
699 buffer_free(&m);
700}
701
702int
Damien Miller71a73672006-03-26 14:04:36 +1100703mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000704{
705 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000706 char *p, *msg;
Damien Miller7207f642008-05-19 15:34:50 +1000707 int success = 0, tmp1 = -1, tmp2 = -1;
708
709 /* Kludge: ensure there are fds free to receive the pty/tty */
710 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
711 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
712 error("%s: cannot allocate fds for pty", __func__);
713 if (tmp1 > 0)
714 close(tmp1);
715 if (tmp2 > 0)
716 close(tmp2);
717 return 0;
718 }
719 close(tmp1);
720 close(tmp2);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000721
722 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000723 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000724
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000725 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000726 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000727
728 success = buffer_get_int(&m);
729 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000730 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000731 buffer_free(&m);
732 return (0);
733 }
734 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000735 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000736 buffer_free(&m);
737
738 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
Darren Tuckera627d422013-06-02 07:31:17 +1000739 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000740
Darren Tucker09991742004-07-17 17:05:14 +1000741 buffer_append(&loginmsg, msg, strlen(msg));
Darren Tuckera627d422013-06-02 07:31:17 +1000742 free(msg);
Darren Tucker09991742004-07-17 17:05:14 +1000743
Damien Miller54fd7cf2007-09-17 12:04:08 +1000744 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
745 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
746 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000747
748 /* Success */
749 return (1);
750}
751
752void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000753mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000754{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000755 Buffer m;
756
757 if (s->ttyfd == -1)
758 return;
759 buffer_init(&m);
760 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000761 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000762 buffer_free(&m);
763
764 /* closed dup'ed master */
Damien Miller7207f642008-05-19 15:34:50 +1000765 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
766 error("close(s->ptymaster/%d): %s",
767 s->ptymaster, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000768
769 /* unlink pty from session */
770 s->ttyfd = -1;
771}
772
Damien Miller79418552002-04-23 20:28:48 +1000773#ifdef USE_PAM
774void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100775mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000776{
777 Buffer m;
778
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000779 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000780 if (!options.use_pam)
781 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000782
783 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000784 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000785
786 buffer_free(&m);
787}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000788
Damien Miller1f499fd2003-08-25 13:08:49 +1000789u_int
790mm_do_pam_account(void)
791{
792 Buffer m;
793 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000794 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000795
796 debug3("%s entering", __func__);
797 if (!options.use_pam)
798 fatal("UsePAM=no, but ended up in %s anyway", __func__);
799
800 buffer_init(&m);
801 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
802
Damien Millera8e06ce2003-11-21 23:48:55 +1100803 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000804 MONITOR_ANS_PAM_ACCOUNT, &m);
805 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000806 msg = buffer_get_string(&m, NULL);
807 buffer_append(&loginmsg, msg, strlen(msg));
Darren Tuckerf60845f2013-06-02 08:07:31 +1000808 free(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000809
810 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100811
Damien Miller1f499fd2003-08-25 13:08:49 +1000812 debug3("%s returning %d", __func__, ret);
813
814 return (ret);
815}
816
Damien Miller4f9f42a2003-05-10 19:28:02 +1000817void *
818mm_sshpam_init_ctx(Authctxt *authctxt)
819{
820 Buffer m;
821 int success;
822
823 debug3("%s", __func__);
824 buffer_init(&m);
825 buffer_put_cstring(&m, authctxt->user);
826 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
827 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
828 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
829 success = buffer_get_int(&m);
830 if (success == 0) {
831 debug3("%s: pam_init_ctx failed", __func__);
832 buffer_free(&m);
833 return (NULL);
834 }
835 buffer_free(&m);
836 return (authctxt);
837}
838
839int
840mm_sshpam_query(void *ctx, char **name, char **info,
841 u_int *num, char ***prompts, u_int **echo_on)
842{
843 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000844 u_int i;
845 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000846
847 debug3("%s", __func__);
848 buffer_init(&m);
849 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
850 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
851 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
852 ret = buffer_get_int(&m);
853 debug3("%s: pam_query returned %d", __func__, ret);
854 *name = buffer_get_string(&m, NULL);
855 *info = buffer_get_string(&m, NULL);
856 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000857 if (*num > PAM_MAX_NUM_MSG)
858 fatal("%s: recieved %u PAM messages, expected <= %u",
859 __func__, *num, PAM_MAX_NUM_MSG);
860 *prompts = xcalloc((*num + 1), sizeof(char *));
861 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000862 for (i = 0; i < *num; ++i) {
863 (*prompts)[i] = buffer_get_string(&m, NULL);
864 (*echo_on)[i] = buffer_get_int(&m);
865 }
866 buffer_free(&m);
867 return (ret);
868}
869
870int
871mm_sshpam_respond(void *ctx, u_int num, char **resp)
872{
873 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000874 u_int i;
875 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000876
877 debug3("%s", __func__);
878 buffer_init(&m);
879 buffer_put_int(&m, num);
880 for (i = 0; i < num; ++i)
881 buffer_put_cstring(&m, resp[i]);
882 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
883 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
884 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
885 ret = buffer_get_int(&m);
886 debug3("%s: pam_respond returned %d", __func__, ret);
887 buffer_free(&m);
888 return (ret);
889}
890
891void
892mm_sshpam_free_ctx(void *ctxtp)
893{
894 Buffer m;
895
896 debug3("%s", __func__);
897 buffer_init(&m);
898 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
899 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
900 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
901 buffer_free(&m);
902}
Damien Miller79418552002-04-23 20:28:48 +1000903#endif /* USE_PAM */
904
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000905/* Request process termination */
906
907void
908mm_terminate(void)
909{
910 Buffer m;
911
912 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000913 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000914 buffer_free(&m);
915}
916
917int
918mm_ssh1_session_key(BIGNUM *num)
919{
920 int rsafail;
921 Buffer m;
922
923 buffer_init(&m);
924 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000925 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000927 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000928
929 rsafail = buffer_get_int(&m);
930 buffer_get_bignum2(&m, num);
931
932 buffer_free(&m);
933
934 return (rsafail);
935}
936
937static void
938mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
939 char ***prompts, u_int **echo_on)
940{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000941 *name = xstrdup("");
942 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000943 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100944 *prompts = xcalloc(*numprompts, sizeof(char *));
945 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000946 (*echo_on)[0] = 0;
947}
948
949int
950mm_bsdauth_query(void *ctx, char **name, char **infotxt,
951 u_int *numprompts, char ***prompts, u_int **echo_on)
952{
953 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100954 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000955 char *challenge;
956
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000957 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000958
959 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000960 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000962 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000963 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100964 success = buffer_get_int(&m);
965 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000966 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000967 buffer_free(&m);
968 return (-1);
969 }
970
971 /* Get the challenge, and format the response */
972 challenge = buffer_get_string(&m, NULL);
973 buffer_free(&m);
974
975 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
976 (*prompts)[0] = challenge;
977
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000978 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000979
980 return (0);
981}
982
983int
984mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
985{
986 Buffer m;
987 int authok;
988
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000989 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000990 if (numresponses != 1)
991 return (-1);
992
993 buffer_init(&m);
994 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000995 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000996
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000997 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000998 MONITOR_ANS_BSDAUTHRESPOND, &m);
999
1000 authok = buffer_get_int(&m);
1001 buffer_free(&m);
1002
1003 return ((authok == 0) ? -1 : 0);
1004}
1005
Darren Tucker042e2e82004-07-08 23:09:42 +10001006#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001007int
1008mm_skey_query(void *ctx, char **name, char **infotxt,
1009 u_int *numprompts, char ***prompts, u_int **echo_on)
1010{
1011 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +11001012 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +10001013 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001014
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001015 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001016
1017 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001018 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001019
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001020 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001021 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +11001022 success = buffer_get_int(&m);
1023 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001024 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001025 buffer_free(&m);
1026 return (-1);
1027 }
1028
1029 /* Get the challenge, and format the response */
1030 challenge = buffer_get_string(&m, NULL);
1031 buffer_free(&m);
1032
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001033 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001034
1035 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
1036
Damien Miller07d86be2006-03-26 14:19:21 +11001037 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001038 free(challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001039
1040 return (0);
1041}
1042
1043int
1044mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1045{
1046 Buffer m;
1047 int authok;
1048
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001049 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001050 if (numresponses != 1)
1051 return (-1);
1052
1053 buffer_init(&m);
1054 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001055 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001056
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001057 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001058 MONITOR_ANS_SKEYRESPOND, &m);
1059
1060 authok = buffer_get_int(&m);
1061 buffer_free(&m);
1062
1063 return ((authok == 0) ? -1 : 0);
1064}
Darren Tucker042e2e82004-07-08 23:09:42 +10001065#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001066
1067void
1068mm_ssh1_session_id(u_char session_id[16])
1069{
1070 Buffer m;
1071 int i;
1072
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001073 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001074
1075 buffer_init(&m);
1076 for (i = 0; i < 16; i++)
1077 buffer_put_char(&m, session_id[i]);
1078
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001079 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001080 buffer_free(&m);
1081}
1082
1083int
1084mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1085{
1086 Buffer m;
1087 Key *key;
1088 u_char *blob;
1089 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001090 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001091
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001092 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001093
1094 buffer_init(&m);
1095 buffer_put_bignum2(&m, client_n);
1096
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001097 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1098 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001099
1100 allowed = buffer_get_int(&m);
1101
Damien Miller06ebedf2003-02-24 12:03:38 +11001102 /* fake forced command */
1103 auth_clear_options();
1104 have_forced = buffer_get_int(&m);
1105 forced_command = have_forced ? xstrdup("true") : NULL;
1106
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001107 if (allowed && rkey != NULL) {
1108 blob = buffer_get_string(&m, &blen);
1109 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001110 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001111 *rkey = key;
Darren Tuckera627d422013-06-02 07:31:17 +10001112 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001113 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001114 buffer_free(&m);
1115
1116 return (allowed);
1117}
1118
1119BIGNUM *
1120mm_auth_rsa_generate_challenge(Key *key)
1121{
1122 Buffer m;
1123 BIGNUM *challenge;
1124 u_char *blob;
1125 u_int blen;
1126
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001127 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001128
1129 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001130 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001131
1132 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1133 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001134 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001135 key->type = KEY_RSA1;
1136
1137 buffer_init(&m);
1138 buffer_put_string(&m, blob, blen);
Darren Tuckera627d422013-06-02 07:31:17 +10001139 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001140
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001141 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1142 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001143
1144 buffer_get_bignum2(&m, challenge);
1145 buffer_free(&m);
1146
1147 return (challenge);
1148}
1149
1150int
1151mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1152{
1153 Buffer m;
1154 u_char *blob;
1155 u_int blen;
1156 int success = 0;
1157
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001158 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001159
1160 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1161 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001162 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001163 key->type = KEY_RSA1;
1164
1165 buffer_init(&m);
1166 buffer_put_string(&m, blob, blen);
1167 buffer_put_string(&m, response, 16);
Darren Tuckera627d422013-06-02 07:31:17 +10001168 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001169
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001170 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1171 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001172
1173 success = buffer_get_int(&m);
1174 buffer_free(&m);
1175
1176 return (success);
1177}
Damien Miller25162f22002-09-12 09:47:29 +10001178
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001179#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001180void
1181mm_audit_event(ssh_audit_event_t event)
1182{
1183 Buffer m;
1184
1185 debug3("%s entering", __func__);
1186
1187 buffer_init(&m);
1188 buffer_put_int(&m, event);
1189
1190 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1191 buffer_free(&m);
1192}
1193
1194void
1195mm_audit_run_command(const char *command)
1196{
1197 Buffer m;
1198
1199 debug3("%s entering command %s", __func__, command);
1200
1201 buffer_init(&m);
1202 buffer_put_cstring(&m, command);
1203
1204 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1205 buffer_free(&m);
1206}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001207#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001208
Darren Tucker0efd1552003-08-26 11:49:55 +10001209#ifdef GSSAPI
1210OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001211mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001212{
1213 Buffer m;
1214 OM_uint32 major;
1215
1216 /* Client doesn't get to see the context */
1217 *ctx = NULL;
1218
1219 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001220 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001221
1222 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1223 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1224
1225 major = buffer_get_int(&m);
1226
1227 buffer_free(&m);
1228 return (major);
1229}
1230
1231OM_uint32
1232mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1233 gss_buffer_desc *out, OM_uint32 *flags)
1234{
1235 Buffer m;
1236 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001237 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001238
1239 buffer_init(&m);
1240 buffer_put_string(&m, in->value, in->length);
1241
1242 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1243 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1244
1245 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001246 out->value = buffer_get_string(&m, &len);
1247 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001248 if (flags)
1249 *flags = buffer_get_int(&m);
1250
1251 buffer_free(&m);
1252
1253 return (major);
1254}
1255
Damien Miller0425d402003-11-17 22:18:21 +11001256OM_uint32
1257mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1258{
1259 Buffer m;
1260 OM_uint32 major;
1261
1262 buffer_init(&m);
1263 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1264 buffer_put_string(&m, gssmic->value, gssmic->length);
1265
1266 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1267 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1268 &m);
1269
1270 major = buffer_get_int(&m);
1271 buffer_free(&m);
1272 return(major);
1273}
1274
Darren Tucker0efd1552003-08-26 11:49:55 +10001275int
1276mm_ssh_gssapi_userok(char *user)
1277{
1278 Buffer m;
1279 int authenticated = 0;
1280
1281 buffer_init(&m);
1282
1283 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1284 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1285 &m);
1286
1287 authenticated = buffer_get_int(&m);
1288
1289 buffer_free(&m);
1290 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1291 return (authenticated);
1292}
1293#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001294
1295#ifdef JPAKE
1296void
1297mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
1298 char **hash_scheme, char **salt)
1299{
1300 Buffer m;
1301
1302 debug3("%s entering", __func__);
1303
1304 buffer_init(&m);
1305 mm_request_send(pmonitor->m_recvfd,
1306 MONITOR_REQ_JPAKE_GET_PWDATA, &m);
1307
1308 debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
1309 mm_request_receive_expect(pmonitor->m_recvfd,
1310 MONITOR_ANS_JPAKE_GET_PWDATA, &m);
1311
1312 *hash_scheme = buffer_get_string(&m, NULL);
1313 *salt = buffer_get_string(&m, NULL);
1314
1315 buffer_free(&m);
1316}
1317
1318void
Damien Millercee85232009-03-06 00:58:22 +11001319mm_jpake_step1(struct modp_group *grp,
Damien Miller01ed2272008-11-05 16:20:46 +11001320 u_char **id, u_int *id_len,
1321 BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
1322 u_char **priv1_proof, u_int *priv1_proof_len,
1323 u_char **priv2_proof, u_int *priv2_proof_len)
1324{
1325 Buffer m;
1326
1327 debug3("%s entering", __func__);
1328
1329 buffer_init(&m);
1330 mm_request_send(pmonitor->m_recvfd,
1331 MONITOR_REQ_JPAKE_STEP1, &m);
1332
1333 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
1334 mm_request_receive_expect(pmonitor->m_recvfd,
1335 MONITOR_ANS_JPAKE_STEP1, &m);
1336
1337 if ((*priv1 = BN_new()) == NULL ||
1338 (*priv2 = BN_new()) == NULL ||
1339 (*g_priv1 = BN_new()) == NULL ||
1340 (*g_priv2 = BN_new()) == NULL)
1341 fatal("%s: BN_new", __func__);
1342
1343 *id = buffer_get_string(&m, id_len);
1344 /* priv1 and priv2 are, well, private */
1345 buffer_get_bignum2(&m, *g_priv1);
1346 buffer_get_bignum2(&m, *g_priv2);
1347 *priv1_proof = buffer_get_string(&m, priv1_proof_len);
1348 *priv2_proof = buffer_get_string(&m, priv2_proof_len);
1349
1350 buffer_free(&m);
1351}
1352
1353void
Damien Millercee85232009-03-06 00:58:22 +11001354mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
Damien Miller01ed2272008-11-05 16:20:46 +11001355 BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
1356 const u_char *theirid, u_int theirid_len,
1357 const u_char *myid, u_int myid_len,
1358 const u_char *theirpub1_proof, u_int theirpub1_proof_len,
1359 const u_char *theirpub2_proof, u_int theirpub2_proof_len,
1360 BIGNUM **newpub,
1361 u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
1362{
1363 Buffer m;
1364
1365 debug3("%s entering", __func__);
1366
1367 buffer_init(&m);
1368 /* monitor already has all bignums except theirpub1, theirpub2 */
1369 buffer_put_bignum2(&m, theirpub1);
1370 buffer_put_bignum2(&m, theirpub2);
1371 /* monitor already knows our id */
1372 buffer_put_string(&m, theirid, theirid_len);
1373 buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
1374 buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
1375
1376 mm_request_send(pmonitor->m_recvfd,
1377 MONITOR_REQ_JPAKE_STEP2, &m);
1378
1379 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
1380 mm_request_receive_expect(pmonitor->m_recvfd,
1381 MONITOR_ANS_JPAKE_STEP2, &m);
1382
1383 if ((*newpub = BN_new()) == NULL)
1384 fatal("%s: BN_new", __func__);
1385
1386 buffer_get_bignum2(&m, *newpub);
1387 *newpub_exponent_proof = buffer_get_string(&m,
1388 newpub_exponent_proof_len);
1389
1390 buffer_free(&m);
1391}
1392
1393void
Damien Millercee85232009-03-06 00:58:22 +11001394mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
Damien Miller01ed2272008-11-05 16:20:46 +11001395 BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
1396 BIGNUM *theirpub1, BIGNUM *theirpub2,
1397 const u_char *my_id, u_int my_id_len,
1398 const u_char *their_id, u_int their_id_len,
1399 const u_char *sess_id, u_int sess_id_len,
1400 const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
1401 BIGNUM **k,
1402 u_char **confirm_hash, u_int *confirm_hash_len)
1403{
1404 Buffer m;
1405
1406 debug3("%s entering", __func__);
1407
1408 buffer_init(&m);
1409 /* monitor already has all bignums except step2_val */
1410 buffer_put_bignum2(&m, step2_val);
1411 /* monitor already knows all the ids */
1412 buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
1413
1414 mm_request_send(pmonitor->m_recvfd,
1415 MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
1416
1417 debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
1418 mm_request_receive_expect(pmonitor->m_recvfd,
1419 MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
1420
1421 /* 'k' is sensitive and stays in the monitor */
1422 *confirm_hash = buffer_get_string(&m, confirm_hash_len);
1423
1424 buffer_free(&m);
1425}
1426
1427int
1428mm_jpake_check_confirm(const BIGNUM *k,
1429 const u_char *peer_id, u_int peer_id_len,
1430 const u_char *sess_id, u_int sess_id_len,
1431 const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
1432{
1433 Buffer m;
1434 int success = 0;
1435
1436 debug3("%s entering", __func__);
1437
1438 buffer_init(&m);
1439 /* k is dummy in slave, ignored */
1440 /* monitor knows all the ids */
1441 buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
1442 mm_request_send(pmonitor->m_recvfd,
1443 MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
1444
1445 debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
1446 mm_request_receive_expect(pmonitor->m_recvfd,
1447 MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
1448
1449 success = buffer_get_int(&m);
1450 buffer_free(&m);
1451
1452 debug3("%s: success = %d", __func__, success);
1453 return success;
1454}
1455#endif /* JPAKE */