blob: 2dfe0c41e172b5395e79f4c0d24f261a0553e8f3 [file] [log] [blame]
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001/* $OpenBSD: monitor.c,v 1.164 2016/08/30 07:50:21 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"
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100031#include <sys/socket.h>
Damien Millerd7834352006-08-05 12:39:39 +100032#include "openbsd-compat/sys-tree.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110033#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000034
Darren Tucker39972492006-07-12 22:22:46 +100035#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100036#include <fcntl.h>
djm@openbsd.org1a31d022016-05-02 08:49:03 +000037#include <limits.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110038#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110039#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110040#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100041#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110042#include <signal.h>
Damien Miller9af21972015-02-24 09:04:32 +110043#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000044#include <stdint.h>
Damien Miller9af21972015-02-24 09:04:32 +110045#endif
Damien Millere7a1e5c2006-08-05 11:34:19 +100046#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100047#include <string.h>
Damien Miller86687062014-07-02 15:28:02 +100048#include <stdarg.h>
49#include <stdio.h>
Damien Miller607aede2006-09-01 15:48:19 +100050#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100051#ifdef HAVE_POLL_H
52#include <poll.h>
53#else
54# ifdef HAVE_SYS_POLL_H
55# include <sys/poll.h>
56# endif
57#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000058
59#ifdef SKEY
60#include <skey.h>
61#endif
62
Damien Miller1f0311c2014-05-15 14:24:09 +100063#ifdef WITH_OPENSSL
Damien Miller03e20032006-03-15 11:16:59 +110064#include <openssl/dh.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100065#endif
Damien Miller03e20032006-03-15 11:16:59 +110066
Damien Millerb84886b2008-05-19 15:05:07 +100067#include "openbsd-compat/sys-queue.h"
Damien Miller8f0bf232011-06-20 14:42:23 +100068#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100069#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#include "key.h"
72#include "buffer.h"
73#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000074#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100075#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000076#include "kex.h"
77#include "dh.h"
Darren Tucker01558b72016-07-18 09:33:25 +100078#include "auth-pam.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000079#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
80#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000081#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000082#define TARGET_OS_MAC 1
83#else
84#include "zlib.h"
85#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000086#include "packet.h"
87#include "auth-options.h"
88#include "sshpty.h"
89#include "channels.h"
90#include "session.h"
91#include "sshlogin.h"
92#include "canohost.h"
93#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100094#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095#include "servconf.h"
96#include "monitor.h"
97#include "monitor_mm.h"
Damien Millerd7834352006-08-05 12:39:39 +100098#ifdef GSSAPI
99#include "ssh-gss.h"
100#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101#include "monitor_wrap.h"
102#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000103#include "compat.h"
104#include "ssh2.h"
Damien Miller85b45e02013-07-20 13:21:52 +1000105#include "authfd.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000106#include "match.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000107#include "ssherr.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000108
Darren Tucker0efd1552003-08-26 11:49:55 +1000109#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000110static Gssctxt *gsscontext = NULL;
111#endif
112
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000113/* Imports */
114extern ServerOptions options;
115extern u_int utmp_len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000116extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000117extern Buffer auth_debug;
118extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000119extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000120
121/* State exported from the child */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000122static struct sshbuf *child_state;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000123
Damien Miller469954d2003-06-18 20:25:33 +1000124/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000125
126int mm_answer_moduli(int, Buffer *);
127int mm_answer_sign(int, Buffer *);
128int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000129int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000130int mm_answer_authserv(int, Buffer *);
131int mm_answer_authpassword(int, Buffer *);
132int mm_answer_bsdauthquery(int, Buffer *);
133int mm_answer_bsdauthrespond(int, Buffer *);
134int mm_answer_skeyquery(int, Buffer *);
135int mm_answer_skeyrespond(int, Buffer *);
136int mm_answer_keyallowed(int, Buffer *);
137int mm_answer_keyverify(int, Buffer *);
138int mm_answer_pty(int, Buffer *);
139int mm_answer_pty_cleanup(int, Buffer *);
140int mm_answer_term(int, Buffer *);
141int mm_answer_rsa_keyallowed(int, Buffer *);
142int mm_answer_rsa_challenge(int, Buffer *);
143int mm_answer_rsa_response(int, Buffer *);
144int mm_answer_sesskey(int, Buffer *);
145int mm_answer_sessid(int, Buffer *);
146
Damien Miller79418552002-04-23 20:28:48 +1000147#ifdef USE_PAM
148int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000149int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000150int mm_answer_pam_init_ctx(int, Buffer *);
151int mm_answer_pam_query(int, Buffer *);
152int mm_answer_pam_respond(int, Buffer *);
153int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000154#endif
155
Darren Tucker0efd1552003-08-26 11:49:55 +1000156#ifdef GSSAPI
157int mm_answer_gss_setup_ctx(int, Buffer *);
158int mm_answer_gss_accept_ctx(int, Buffer *);
159int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100160int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000161#endif
Damien Miller25162f22002-09-12 09:47:29 +1000162
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100163#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100164int mm_answer_audit_event(int, Buffer *);
165int mm_answer_audit_command(int, Buffer *);
166#endif
167
Damien Miller8f0bf232011-06-20 14:42:23 +1000168static int monitor_read_log(struct monitor *);
169
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000170static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000171
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000172/* local state for key verify */
173static u_char *key_blob = NULL;
174static u_int key_bloblen = 0;
175static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000176static char *hostbased_cuser = NULL;
177static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000178static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100179static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000180static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000181static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000182static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000183
184struct mon_table {
185 enum monitor_reqtype type;
186 int flags;
187 int (*f)(int, Buffer *);
188};
189
190#define MON_ISAUTH 0x0004 /* Required for Authentication */
191#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
192#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100193#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000194
195#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
196
197#define MON_PERMIT 0x1000 /* Request is permitted */
198
199struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000200#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000201 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000202#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000203 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
204 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
205 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000206 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000207 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000208#ifdef USE_PAM
209 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000210 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Millerb38b95f2016-08-29 11:47:07 +1000211 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
212 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
213 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000214 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000215#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100216#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100217 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100218#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000219#ifdef BSD_AUTH
220 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100221 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000222#endif
223#ifdef SKEY
224 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
225 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
226#endif
227 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
228 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000229#ifdef GSSAPI
230 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
231 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
232 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100233 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000234#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000235 {0, 0, NULL}
236};
237
238struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000239#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000240 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000241#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000242 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
243 {MONITOR_REQ_PTY, 0, mm_answer_pty},
244 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
245 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100246#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100247 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
248 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
249#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000250 {0, 0, NULL}
251};
252
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000253struct mon_table *mon_dispatch;
254
255/* Specifies if a certain message is allowed at the moment */
256
257static void
258monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
259{
260 while (ent->f != NULL) {
261 if (ent->type == type) {
262 ent->flags &= ~MON_PERMIT;
263 ent->flags |= permit ? MON_PERMIT : 0;
264 return;
265 }
266 ent++;
267 }
268}
269
270static void
271monitor_permit_authentications(int permit)
272{
273 struct mon_table *ent = mon_dispatch;
274
275 while (ent->f != NULL) {
276 if (ent->flags & MON_AUTH) {
277 ent->flags &= ~MON_PERMIT;
278 ent->flags |= permit ? MON_PERMIT : 0;
279 }
280 ent++;
281 }
282}
283
Darren Tucker3e33cec2003-10-02 16:12:36 +1000284void
285monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000286{
287 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100288 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000289
290 debug3("preauth child monitor started");
291
Damien Miller8f0bf232011-06-20 14:42:23 +1000292 close(pmonitor->m_recvfd);
293 close(pmonitor->m_log_sendfd);
294 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
295
Darren Tucker3e33cec2003-10-02 16:12:36 +1000296 authctxt = _authctxt;
297 memset(authctxt, 0, sizeof(*authctxt));
298
Darren Tuckerde0de392005-03-31 23:52:04 +1000299 authctxt->loginmsg = &loginmsg;
300
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000301 mon_dispatch = mon_dispatch_proto20;
302 /* Permit requests for moduli and signatures */
303 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
304 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000305
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000306 /* The first few requests do not require asynchronous access */
307 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100308 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100309 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100310 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100311 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100312
313 /* Special handling for multiple required authentications */
314 if (options.num_auth_methods != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100315 if (authenticated &&
316 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000317 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100318 debug3("%s: method %s: partial", __func__,
319 auth_method);
320 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100321 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100322 }
323 }
324
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000325 if (authenticated) {
326 if (!(ent->flags & MON_AUTHDECIDE))
327 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000328 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000329 if (authctxt->pw->pw_uid == 0 &&
330 !auth_root_allowed(auth_method))
331 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000332#ifdef USE_PAM
333 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100334 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000335 Buffer m;
336
337 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100338 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000339 MONITOR_REQ_PAM_ACCOUNT, &m);
340 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
341 buffer_free(&m);
342 }
343#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000344 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100345 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100346 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000347 auth_method, auth_submethod);
djm@openbsd.org94885382015-06-22 23:42:16 +0000348 if (!partial && !authenticated)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000349 authctxt->failures++;
350 }
351 }
352
353 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000354 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100355 if (strcmp(auth_method, "unknown") == 0)
356 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357
358 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000359 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000360
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000361 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000362
Damien Miller6a1937e2012-12-12 10:44:38 +1100363 /* Drain any buffered messages from the child */
364 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
365 ;
366
Damien Miller8f0bf232011-06-20 14:42:23 +1000367 close(pmonitor->m_sendfd);
368 close(pmonitor->m_log_recvfd);
369 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370}
371
Damien Millerbe64d432003-05-14 19:31:12 +1000372static void
373monitor_set_child_handler(pid_t pid)
374{
375 monitor_child_pid = pid;
376}
377
378static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000379monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000380{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000381 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000382}
383
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000384void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000385monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000386{
Damien Miller8f0bf232011-06-20 14:42:23 +1000387 close(pmonitor->m_recvfd);
388 pmonitor->m_recvfd = -1;
389
Damien Millerbe64d432003-05-14 19:31:12 +1000390 monitor_set_child_handler(pmonitor->m_pid);
391 signal(SIGHUP, &monitor_child_handler);
392 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000393 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000394#ifdef SIGXFSZ
395 signal(SIGXFSZ, SIG_IGN);
396#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000397
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000398 mon_dispatch = mon_dispatch_postauth20;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000399
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000400 /* Permit requests for moduli and signatures */
401 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
402 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
403 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
404
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000405 if (!no_pty_flag) {
406 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
407 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
408 }
409
410 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000411 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000412}
413
414void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000415monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000416{
Damien Miller2d6b8332002-06-21 15:59:49 +1000417 if (options.compression) {
418 /* The member allocation is not visible, so sync it */
419 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
420 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000421}
422
markus@openbsd.org091c3022015-01-19 19:52:16 +0000423/* Allocation functions for zlib */
424static void *
425mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
426{
deraadt@openbsd.orge774e5e2015-08-21 23:29:31 +0000427 if (size == 0 || ncount == 0 || ncount > SIZE_MAX / size)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000428 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
429
deraadt@openbsd.orge774e5e2015-08-21 23:29:31 +0000430 return mm_malloc(mm, size * ncount);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000431}
432
433static void
434mm_zfree(struct mm_master *mm, void *address)
435{
436 mm_free(mm, address);
437}
438
Damien Miller8f0bf232011-06-20 14:42:23 +1000439static int
440monitor_read_log(struct monitor *pmonitor)
441{
442 Buffer logmsg;
443 u_int len, level;
444 char *msg;
445
446 buffer_init(&logmsg);
447
448 /* Read length */
449 buffer_append_space(&logmsg, 4);
450 if (atomicio(read, pmonitor->m_log_recvfd,
451 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
452 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100453 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000454 debug("%s: child log fd closed", __func__);
455 close(pmonitor->m_log_recvfd);
456 pmonitor->m_log_recvfd = -1;
457 return -1;
458 }
459 fatal("%s: log fd read: %s", __func__, strerror(errno));
460 }
461 len = buffer_get_int(&logmsg);
462 if (len <= 4 || len > 8192)
463 fatal("%s: invalid log message length %u", __func__, len);
464
465 /* Read severity, message */
466 buffer_clear(&logmsg);
467 buffer_append_space(&logmsg, len);
468 if (atomicio(read, pmonitor->m_log_recvfd,
469 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
470 fatal("%s: log fd read: %s", __func__, strerror(errno));
471
472 /* Log it */
473 level = buffer_get_int(&logmsg);
474 msg = buffer_get_string(&logmsg, NULL);
475 if (log_level_name(level) == NULL)
476 fatal("%s: invalid log level %u (corrupted message?)",
477 __func__, level);
478 do_log2(level, "%s [preauth]", msg);
479
480 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000481 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000482
483 return 0;
484}
485
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000486int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000487monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000488 struct mon_table **pent)
489{
490 Buffer m;
491 int ret;
492 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000493 struct pollfd pfd[2];
494
495 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100496 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000497 pfd[0].fd = pmonitor->m_sendfd;
498 pfd[0].events = POLLIN;
499 pfd[1].fd = pmonitor->m_log_recvfd;
500 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000501 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
502 if (errno == EINTR || errno == EAGAIN)
503 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000504 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000505 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000506 if (pfd[1].revents) {
507 /*
508 * Drain all log messages before processing next
509 * monitor request.
510 */
511 monitor_read_log(pmonitor);
512 continue;
513 }
514 if (pfd[0].revents)
515 break; /* Continues below */
516 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000517
518 buffer_init(&m);
519
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000520 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000521 type = buffer_get_char(&m);
522
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000523 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000524
525 while (ent->f != NULL) {
526 if (ent->type == type)
527 break;
528 ent++;
529 }
530
531 if (ent->f != NULL) {
532 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000533 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000534 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000535 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000536 buffer_free(&m);
537
538 /* The child may use this request only once, disable it */
539 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000540 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000541 type);
542 ent->flags &= ~MON_PERMIT;
543 }
544
545 if (pent != NULL)
546 *pent = ent;
547
548 return ret;
549 }
550
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000551 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000552
553 /* NOTREACHED */
554 return (-1);
555}
556
557/* allowed key state */
558static int
559monitor_allowed_key(u_char *blob, u_int bloblen)
560{
561 /* make sure key is allowed */
562 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000563 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000564 return (0);
565 return (1);
566}
567
568static void
569monitor_reset_key_state(void)
570{
571 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000572 free(key_blob);
573 free(hostbased_cuser);
574 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000575 key_blob = NULL;
576 key_bloblen = 0;
577 key_blobtype = MM_NOKEY;
578 hostbased_cuser = NULL;
579 hostbased_chost = NULL;
580}
581
Damien Miller1f0311c2014-05-15 14:24:09 +1000582#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000583int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000584mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000585{
586 DH *dh;
587 int min, want, max;
588
589 min = buffer_get_int(m);
590 want = buffer_get_int(m);
591 max = buffer_get_int(m);
592
593 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000594 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000595 /* We need to check here, too, in case the child got corrupted */
596 if (max < min || want < min || max < want)
597 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000598 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000599
600 buffer_clear(m);
601
602 dh = choose_dh(min, want, max);
603 if (dh == NULL) {
604 buffer_put_char(m, 0);
605 return (0);
606 } else {
607 /* Send first bignum */
608 buffer_put_char(m, 1);
609 buffer_put_bignum2(m, dh->p);
610 buffer_put_bignum2(m, dh->g);
611
612 DH_free(dh);
613 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000614 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000615 return (0);
616}
Damien Miller1f0311c2014-05-15 14:24:09 +1000617#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000618
619int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000620mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000621{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000622 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000623 extern int auth_sock; /* XXX move to state struct? */
624 struct sshkey *key;
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000625 struct sshbuf *sigbuf = NULL;
626 u_char *p = NULL, *signature = NULL;
627 char *alg = NULL;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000628 size_t datlen, siglen, alglen;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000629 int r, is_proof = 0;
630 u_int keyid;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000631 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000632
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000633 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000634
djm@openbsd.org141efe42015-01-14 20:05:27 +0000635 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000636 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
637 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000638 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000639 if (keyid > INT_MAX)
640 fatal("%s: invalid key ID", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000641
Damien Millera63128d2006-03-15 12:08:28 +1100642 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000643 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
644 * SHA384 (48 bytes) and SHA512 (64 bytes).
djm@openbsd.org523463a2015-02-16 22:13:32 +0000645 *
646 * Otherwise, verify the signature request is for a hostkey
647 * proof.
648 *
649 * XXX perform similar check for KEX signature requests too?
650 * it's not trivial, since what is signed is the hash, rather
651 * than the full kex structure...
Damien Millera63128d2006-03-15 12:08:28 +1100652 */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000653 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
654 /*
655 * Construct expected hostkey proof and compare it to what
656 * the client sent us.
657 */
658 if (session_id2_len == 0) /* hostkeys is never first */
659 fatal("%s: bad data length: %zu", __func__, datlen);
660 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
661 fatal("%s: no hostkey for index %d", __func__, keyid);
662 if ((sigbuf = sshbuf_new()) == NULL)
663 fatal("%s: sshbuf_new", __func__);
djm@openbsd.org44732de2015-02-20 22:17:21 +0000664 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
665 (r = sshbuf_put_string(sigbuf, session_id2,
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000666 session_id2_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000667 (r = sshkey_puts(key, sigbuf)) != 0)
668 fatal("%s: couldn't prepare private key "
669 "proof buffer: %s", __func__, ssh_err(r));
670 if (datlen != sshbuf_len(sigbuf) ||
671 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
672 fatal("%s: bad data length: %zu, hostkey proof len %zu",
673 __func__, datlen, sshbuf_len(sigbuf));
674 sshbuf_free(sigbuf);
675 is_proof = 1;
676 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000677
Ben Lindstromf67e0772002-06-06 20:58:19 +0000678 /* save session id, it will be passed on the first call */
679 if (session_id2_len == 0) {
680 session_id2_len = datlen;
681 session_id2 = xmalloc(session_id2_len);
682 memcpy(session_id2, p, session_id2_len);
683 }
684
Damien Miller85b45e02013-07-20 13:21:52 +1000685 if ((key = get_hostkey_by_index(keyid)) != NULL) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000686 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000687 datafellows)) != 0)
688 fatal("%s: sshkey_sign failed: %s",
689 __func__, ssh_err(r));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000690 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000691 auth_sock > 0) {
692 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000693 p, datlen, alg, datafellows)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000694 fatal("%s: ssh_agent_sign failed: %s",
695 __func__, ssh_err(r));
696 }
Damien Miller85b45e02013-07-20 13:21:52 +1000697 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000698 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000699
djm@openbsd.org523463a2015-02-16 22:13:32 +0000700 debug3("%s: %s signature %p(%zu)", __func__,
701 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000702
djm@openbsd.org141efe42015-01-14 20:05:27 +0000703 sshbuf_reset(m);
704 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
705 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000706
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000707 free(alg);
Darren Tuckera627d422013-06-02 07:31:17 +1000708 free(p);
709 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000710
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000711 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000712
713 /* Turn on permissions for getpwnam */
714 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
715
716 return (0);
717}
718
719/* Retrieves the password entry and also checks if the user is permitted */
720
721int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000722mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000723{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000724 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725 struct passwd *pwent;
726 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000727 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000729 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000730
731 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000732 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000733
Darren Tuckerb09b6772004-06-22 15:06:46 +1000734 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000735
Darren Tuckerb09b6772004-06-22 15:06:46 +1000736 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000737
Darren Tuckerb09b6772004-06-22 15:06:46 +1000738 authctxt->user = xstrdup(username);
739 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000740 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000741
742 buffer_clear(m);
743
744 if (pwent == NULL) {
745 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100746 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000747 goto out;
748 }
749
750 allowed = 1;
751 authctxt->pw = pwent;
752 authctxt->valid = 1;
753
754 buffer_put_char(m, 1);
755 buffer_put_string(m, pwent, sizeof(struct passwd));
756 buffer_put_cstring(m, pwent->pw_name);
757 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000758#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000759 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000760#endif
761#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000762 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000763#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000764 buffer_put_cstring(m, pwent->pw_dir);
765 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100766
767 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100768 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000769
770#define M_CP_STROPT(x) do { \
771 if (options.x != NULL) \
772 buffer_put_cstring(m, options.x); \
773 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000774#define M_CP_STRARRAYOPT(x, nx) do { \
775 for (i = 0; i < options.nx; i++) \
776 buffer_put_cstring(m, options.x[i]); \
777 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000778 /* See comment in servconf.h */
779 COPY_MATCH_STRING_OPTS();
780#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000781#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100782
783 /* Create valid auth method lists */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000784 if (auth2_setup_methods_lists(authctxt) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100785 /*
786 * The monitor will continue long enough to let the child
787 * run to it's packet_disconnect(), but it must not allow any
788 * authentication to succeed.
789 */
790 debug("%s: no valid authentication method lists", __func__);
791 }
792
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000793 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000794 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000795
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000796 /* Allow service/style information on the auth context */
797 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
798 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
799
Damien Millera33501b2002-05-08 12:24:42 +1000800#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000801 if (options.use_pam)
802 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000803#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000804
805 return (0);
806}
807
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000808int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000809{
810 char *banner;
811
812 buffer_clear(m);
813 banner = auth2_read_banner();
814 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000815 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000816 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000817
818 return (0);
819}
820
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000821int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000822mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000823{
824 monitor_permit_authentications(1);
825
826 authctxt->service = buffer_get_string(m, NULL);
827 authctxt->style = buffer_get_string(m, NULL);
828 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000829 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000830
831 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000832 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000833 authctxt->style = NULL;
834 }
835
836 return (0);
837}
838
839int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000840mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000841{
842 static int call_count;
843 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000844 int authenticated;
845 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000846
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000847 if (!options.password_authentication)
848 fatal("%s: password authentication not enabled", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000849 passwd = buffer_get_string(m, &plen);
850 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000851 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000852 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100853 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000854 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000855
856 buffer_clear(m);
857 buffer_put_int(m, authenticated);
Darren Tucker01558b72016-07-18 09:33:25 +1000858#ifdef USE_PAM
859 buffer_put_int(m, sshpam_get_maxtries_reached());
860#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000861
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000862 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000863 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000864
865 call_count++;
866 if (plen == 0 && call_count == 1)
867 auth_method = "none";
868 else
869 auth_method = "password";
870
871 /* Causes monitor loop to terminate if authenticated */
872 return (authenticated);
873}
874
875#ifdef BSD_AUTH
876int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000877mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000878{
879 char *name, *infotxt;
880 u_int numprompts;
881 u_int *echo_on;
882 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100883 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000884
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000885 if (!options.kbd_interactive_authentication)
886 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb7df3af2003-02-24 11:55:46 +1100887 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
888 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000889
890 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100891 buffer_put_int(m, success);
892 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000893 buffer_put_cstring(m, prompts[0]);
894
Damien Millerb7df3af2003-02-24 11:55:46 +1100895 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000896 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000897
Damien Millerb7df3af2003-02-24 11:55:46 +1100898 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000899 free(name);
900 free(infotxt);
901 free(prompts);
902 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000903 }
904
905 return (0);
906}
907
908int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000909mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000910{
911 char *response;
912 int authok;
913
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000914 if (!options.kbd_interactive_authentication)
915 fatal("%s: kbd-int authentication not enabled", __func__);
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +0000916 if (authctxt->as == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000917 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000918
919 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000920 authok = options.challenge_response_authentication &&
921 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000922 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000923 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000924 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000925
926 buffer_clear(m);
927 buffer_put_int(m, authok);
928
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000929 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000930 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000931
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000932 auth_method = "keyboard-interactive";
933 auth_submethod = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000934
935 return (authok != 0);
936}
937#endif
938
939#ifdef SKEY
940int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000941mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000942{
943 struct skey skey;
944 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100945 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000946
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000947 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
948 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000949
950 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100951 buffer_put_int(m, success);
952 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000953 buffer_put_cstring(m, challenge);
954
Damien Millerb7df3af2003-02-24 11:55:46 +1100955 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000956 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000957
958 return (0);
959}
960
961int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000962mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000963{
964 char *response;
965 int authok;
966
967 response = buffer_get_string(m, NULL);
968
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000969 authok = (options.challenge_response_authentication &&
970 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000971 skey_haskey(authctxt->pw->pw_name) == 0 &&
972 skey_passcheck(authctxt->pw->pw_name, response) != -1);
973
Darren Tuckerf60845f2013-06-02 08:07:31 +1000974 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000975
976 buffer_clear(m);
977 buffer_put_int(m, authok);
978
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000979 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000980 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000981
Damien Millerac947352015-11-20 12:35:41 +1100982 auth_method = "keyboard-interactive";
983 auth_submethod = "skey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000984
985 return (authok != 0);
986}
987#endif
988
Damien Miller79418552002-04-23 20:28:48 +1000989#ifdef USE_PAM
990int
Darren Tucker7eda5922004-06-22 13:22:50 +1000991mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000992{
Damien Miller4e448a32003-05-14 15:11:48 +1000993 if (!options.use_pam)
994 fatal("UsePAM not set, but ended up in %s anyway", __func__);
995
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100996 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000997
Damien Miller1f499fd2003-08-25 13:08:49 +1000998 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
Damien Millerb38b95f2016-08-29 11:47:07 +1000999 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller1f499fd2003-08-25 13:08:49 +10001000
Damien Miller79418552002-04-23 20:28:48 +10001001 return (0);
1002}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001003
Damien Miller1f499fd2003-08-25 13:08:49 +10001004int
Darren Tucker7eda5922004-06-22 13:22:50 +10001005mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001006{
1007 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001008
Damien Miller1f499fd2003-08-25 13:08:49 +10001009 if (!options.use_pam)
1010 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1011
1012 ret = do_pam_account();
1013
1014 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001015 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001016
Darren Tucker7eda5922004-06-22 13:22:50 +10001017 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001018
1019 return (ret);
1020}
1021
Damien Miller4f9f42a2003-05-10 19:28:02 +10001022static void *sshpam_ctxt, *sshpam_authok;
1023extern KbdintDevice sshpam_device;
1024
1025int
Darren Tucker7eda5922004-06-22 13:22:50 +10001026mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001027{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001028 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001029 if (sshpam_ctxt != NULL)
1030 fatal("%s: already called", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001031 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1032 sshpam_authok = NULL;
1033 buffer_clear(m);
1034 if (sshpam_ctxt != NULL) {
1035 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
Damien Millerb38b95f2016-08-29 11:47:07 +10001036 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001037 buffer_put_int(m, 1);
1038 } else {
1039 buffer_put_int(m, 0);
1040 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001041 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001042 return (0);
1043}
1044
1045int
Darren Tucker7eda5922004-06-22 13:22:50 +10001046mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001047{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001048 char *name = NULL, *info = NULL, **prompts = NULL;
1049 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001050 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001051
1052 debug3("%s", __func__);
1053 sshpam_authok = NULL;
Damien Millerb38b95f2016-08-29 11:47:07 +10001054 if (sshpam_ctxt == NULL)
1055 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001056 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1057 if (ret == 0 && num == 0)
1058 sshpam_authok = sshpam_ctxt;
1059 if (num > 1 || name == NULL || info == NULL)
Damien Millerb38b95f2016-08-29 11:47:07 +10001060 fatal("sshpam_device.query failed");
1061 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001062 buffer_clear(m);
1063 buffer_put_int(m, ret);
1064 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001065 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001066 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001067 free(info);
Darren Tucker01558b72016-07-18 09:33:25 +10001068 buffer_put_int(m, sshpam_get_maxtries_reached());
Damien Miller4f9f42a2003-05-10 19:28:02 +10001069 buffer_put_int(m, num);
1070 for (i = 0; i < num; ++i) {
1071 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001072 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001073 buffer_put_int(m, echo_on[i]);
1074 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001075 free(prompts);
1076 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001077 auth_method = "keyboard-interactive";
1078 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001079 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001080 return (0);
1081}
1082
1083int
Darren Tucker7eda5922004-06-22 13:22:50 +10001084mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001085{
1086 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001087 u_int i, num;
1088 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001089
1090 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001091 if (sshpam_ctxt == NULL)
1092 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001093 sshpam_authok = NULL;
1094 num = buffer_get_int(m);
1095 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001096 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001097 for (i = 0; i < num; ++i)
1098 resp[i] = buffer_get_string(m, NULL);
1099 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1100 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001101 free(resp[i]);
1102 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001103 } else {
1104 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1105 }
1106 buffer_clear(m);
1107 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001108 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001109 auth_method = "keyboard-interactive";
1110 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001111 if (ret == 0)
1112 sshpam_authok = sshpam_ctxt;
1113 return (0);
1114}
1115
1116int
Darren Tucker7eda5922004-06-22 13:22:50 +10001117mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001118{
Damien Miller5e75f512015-08-11 13:34:12 +10001119 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001120
1121 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001122 if (sshpam_ctxt == NULL)
1123 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001124 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001125 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001126 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001127 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Millerb38b95f2016-08-29 11:47:07 +10001128 /* Allow another attempt */
1129 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller15b05cf2012-12-03 09:53:20 +11001130 auth_method = "keyboard-interactive";
1131 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001132 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001133}
Damien Miller79418552002-04-23 20:28:48 +10001134#endif
1135
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001136int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001137mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001138{
1139 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001140 char *cuser, *chost;
1141 u_char *blob;
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001142 u_int bloblen, pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001143 enum mm_keytype type = 0;
1144 int allowed = 0;
1145
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001146 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001147
1148 type = buffer_get_int(m);
1149 cuser = buffer_get_string(m, NULL);
1150 chost = buffer_get_string(m, NULL);
1151 blob = buffer_get_string(m, &bloblen);
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001152 pubkey_auth_attempt = buffer_get_int(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001153
1154 key = key_from_blob(blob, bloblen);
1155
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001156 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001157
Damien Miller3e3b5142003-11-17 21:13:40 +11001158 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001159 /* These should not make it past the privsep child */
1160 if (key_type_plain(key->type) == KEY_RSA &&
1161 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1162 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1163
Darren Tucker47eede72005-03-14 23:08:12 +11001164 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001165 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001166 allowed = options.pubkey_authentication &&
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001167 !auth2_userkey_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001168 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001169 options.pubkey_key_types, 0) == 1 &&
1170 user_key_allowed(authctxt->pw, key,
1171 pubkey_auth_attempt);
Damien Miller20bdcd72013-07-18 16:10:09 +10001172 pubkey_auth_info(authctxt, key, NULL);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001173 auth_method = "publickey";
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001174 if (options.pubkey_authentication &&
1175 (!pubkey_auth_attempt || allowed != 1))
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001176 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001177 break;
1178 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001179 allowed = options.hostbased_authentication &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001180 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001181 options.hostbased_key_types, 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001182 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001183 cuser, chost, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001184 pubkey_auth_info(authctxt, key,
1185 "client user \"%.100s\", client host \"%.100s\"",
1186 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001187 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001188 break;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001189 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001190 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001191 break;
1192 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001193 }
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001194
1195 debug3("%s: key %p is %s",
1196 __func__, key, allowed ? "allowed" : "not allowed");
1197
Damien Miller00111382003-03-10 11:21:17 +11001198 if (key != NULL)
1199 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001200
1201 /* clear temporarily storage (used by verify) */
1202 monitor_reset_key_state();
1203
1204 if (allowed) {
1205 /* Save temporarily for comparison in verify */
1206 key_blob = blob;
1207 key_bloblen = bloblen;
1208 key_blobtype = type;
1209 hostbased_cuser = cuser;
1210 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001211 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001212 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001213 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001214 free(blob);
1215 free(cuser);
1216 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001217 }
1218
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001219 buffer_clear(m);
1220 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001221 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001222
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001223 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001224
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001225 return (0);
1226}
1227
1228static int
1229monitor_valid_userblob(u_char *data, u_int datalen)
1230{
1231 Buffer b;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001232 u_char *p;
1233 char *userstyle, *cp;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001234 u_int len;
1235 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001236
1237 buffer_init(&b);
1238 buffer_append(&b, data, datalen);
1239
1240 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001241 p = buffer_ptr(&b);
1242 len = buffer_len(&b);
1243 if ((session_id2 == NULL) ||
1244 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001245 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001246 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001247 buffer_consume(&b, session_id2_len);
1248 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001249 p = buffer_get_string(&b, &len);
1250 if ((session_id2 == NULL) ||
1251 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001252 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001253 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001254 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001255 }
1256 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1257 fail++;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001258 cp = buffer_get_cstring(&b, NULL);
Damien Miller4ce189d2013-04-23 15:17:52 +10001259 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1260 authctxt->style ? ":" : "",
1261 authctxt->style ? authctxt->style : "");
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001262 if (strcmp(userstyle, cp) != 0) {
1263 logit("wrong user name passed to monitor: "
1264 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001265 fail++;
1266 }
Darren Tuckera627d422013-06-02 07:31:17 +10001267 free(userstyle);
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001268 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001269 buffer_skip_string(&b);
1270 if (datafellows & SSH_BUG_PKAUTH) {
1271 if (!buffer_get_char(&b))
1272 fail++;
1273 } else {
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001274 cp = buffer_get_cstring(&b, NULL);
1275 if (strcmp("publickey", cp) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001276 fail++;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001277 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001278 if (!buffer_get_char(&b))
1279 fail++;
1280 buffer_skip_string(&b);
1281 }
1282 buffer_skip_string(&b);
1283 if (buffer_len(&b) != 0)
1284 fail++;
1285 buffer_free(&b);
1286 return (fail == 0);
1287}
1288
1289static int
Damien Millera10f5612002-09-12 09:49:15 +10001290monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1291 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001292{
1293 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001294 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295 u_int len;
1296 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001297
1298 buffer_init(&b);
1299 buffer_append(&b, data, datalen);
1300
Ben Lindstromf67e0772002-06-06 20:58:19 +00001301 p = buffer_get_string(&b, &len);
1302 if ((session_id2 == NULL) ||
1303 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001304 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001305 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001306 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001307
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001308 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1309 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001310 p = buffer_get_cstring(&b, NULL);
1311 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1312 authctxt->style ? ":" : "",
1313 authctxt->style ? authctxt->style : "");
1314 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001315 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001316 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001317 fail++;
1318 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001319 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001320 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001321 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001322 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001323 if (strcmp(p, "hostbased") != 0)
1324 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001325 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001326 buffer_skip_string(&b); /* pkalg */
1327 buffer_skip_string(&b); /* pkblob */
1328
1329 /* verify client host, strip trailing dot if necessary */
1330 p = buffer_get_string(&b, NULL);
1331 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1332 p[len - 1] = '\0';
1333 if (strcmp(p, chost) != 0)
1334 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001335 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001336
1337 /* verify client user */
1338 p = buffer_get_string(&b, NULL);
1339 if (strcmp(p, cuser) != 0)
1340 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001341 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001342
1343 if (buffer_len(&b) != 0)
1344 fail++;
1345 buffer_free(&b);
1346 return (fail == 0);
1347}
1348
1349int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001350mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001351{
1352 Key *key;
1353 u_char *signature, *data, *blob;
1354 u_int signaturelen, datalen, bloblen;
1355 int verified = 0;
1356 int valid_data = 0;
1357
1358 blob = buffer_get_string(m, &bloblen);
1359 signature = buffer_get_string(m, &signaturelen);
1360 data = buffer_get_string(m, &datalen);
1361
1362 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001363 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001364 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001365
1366 key = key_from_blob(blob, bloblen);
1367 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001368 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001369
1370 switch (key_blobtype) {
1371 case MM_USERKEY:
1372 valid_data = monitor_valid_userblob(data, datalen);
1373 break;
1374 case MM_HOSTKEY:
1375 valid_data = monitor_valid_hostbasedblob(data, datalen,
1376 hostbased_cuser, hostbased_chost);
1377 break;
1378 default:
1379 valid_data = 0;
1380 break;
1381 }
1382 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001383 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001384
1385 verified = key_verify(key, signature, signaturelen, data, datalen);
1386 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001387 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001388
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001389 /* If auth was successful then record key to ensure it isn't reused */
djm@openbsd.orgf54d8ac2015-09-04 04:44:08 +00001390 if (verified == 1 && key_blobtype == MM_USERKEY)
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001391 auth2_record_userkey(authctxt, key);
1392 else
1393 key_free(key);
1394
Darren Tuckera627d422013-06-02 07:31:17 +10001395 free(blob);
1396 free(signature);
1397 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001398
Ben Lindstrome1c09122002-06-23 00:38:24 +00001399 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1400
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001401 monitor_reset_key_state();
1402
1403 buffer_clear(m);
1404 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001405 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001406
Darren Tuckerfbba7352006-11-07 23:16:08 +11001407 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001408}
1409
1410static void
1411mm_record_login(Session *s, struct passwd *pw)
1412{
djm@openbsd.org95767262016-03-07 19:02:43 +00001413 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001414 socklen_t fromlen;
1415 struct sockaddr_storage from;
1416
1417 /*
1418 * Get IP address of client. If the connection is not a socket, let
1419 * the address be 0.0.0.0.
1420 */
1421 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001422 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001423 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001424 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001425 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001426 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001427 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428 }
1429 }
1430 /* Record that there was a login on that tty from the remote host. */
1431 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +00001432 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001433 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434}
1435
1436static void
1437mm_session_close(Session *s)
1438{
Damien Miller04bd8b02003-05-25 14:38:33 +10001439 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001440 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001441 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001442 session_pty_cleanup2(s);
1443 }
Damien Miller7207f642008-05-19 15:34:50 +10001444 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001445}
1446
1447int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001448mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001449{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001450 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001451 Session *s;
1452 int res, fd0;
1453
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001454 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001455
1456 buffer_clear(m);
1457 s = session_new();
1458 if (s == NULL)
1459 goto error;
1460 s->authctxt = authctxt;
1461 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001462 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001463 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1464 if (res == 0)
1465 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001466 pty_setowner(authctxt->pw, s->tty);
1467
1468 buffer_put_int(m, 1);
1469 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001470
1471 /* We need to trick ttyslot */
1472 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001473 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001474
1475 mm_record_login(s, authctxt->pw);
1476
1477 /* Now we can close the file descriptor again */
1478 close(0);
1479
Darren Tucker09991742004-07-17 17:05:14 +10001480 /* send messages generated by record_login */
1481 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1482 buffer_clear(&loginmsg);
1483
1484 mm_request_send(sock, MONITOR_ANS_PTY, m);
1485
Damien Miller54fd7cf2007-09-17 12:04:08 +10001486 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1487 mm_send_fd(sock, s->ttyfd) == -1)
1488 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001489
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001490 /* make sure nothing uses fd 0 */
1491 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001492 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001493 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001494 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001495
1496 /* slave is not needed */
1497 close(s->ttyfd);
1498 s->ttyfd = s->ptyfd;
1499 /* no need to dup() because nobody closes ptyfd */
1500 s->ptymaster = s->ptyfd;
1501
Damien Miller9ab00b42006-08-05 12:40:11 +10001502 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001503
1504 return (0);
1505
1506 error:
1507 if (s != NULL)
1508 mm_session_close(s);
1509 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001510 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001511 return (0);
1512}
1513
1514int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001515mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001516{
1517 Session *s;
1518 char *tty;
1519
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001520 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001521
1522 tty = buffer_get_string(m, NULL);
1523 if ((s = session_by_tty(tty)) != NULL)
1524 mm_session_close(s);
1525 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001526 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001527 return (0);
1528}
1529
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001530int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001531mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001533 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001534 int res, status;
1535
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001536 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001537
1538 /* The child is terminating */
1539 session_destroy_all(&mm_session_close);
1540
Darren Tucker52358d62008-03-11 22:58:25 +11001541#ifdef USE_PAM
1542 if (options.use_pam)
1543 sshpam_cleanup();
1544#endif
1545
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001546 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001547 if (errno != EINTR)
1548 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001549
1550 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1551
1552 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001553 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001554}
1555
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001556#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001557/* Report that an audit event occurred */
1558int
1559mm_answer_audit_event(int socket, Buffer *m)
1560{
1561 ssh_audit_event_t event;
1562
1563 debug3("%s entering", __func__);
1564
1565 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001566 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001567 case SSH_AUTH_FAIL_PUBKEY:
1568 case SSH_AUTH_FAIL_HOSTBASED:
1569 case SSH_AUTH_FAIL_GSSAPI:
1570 case SSH_LOGIN_EXCEED_MAXTRIES:
1571 case SSH_LOGIN_ROOT_DENIED:
1572 case SSH_CONNECTION_CLOSE:
1573 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001574 audit_event(event);
1575 break;
1576 default:
1577 fatal("Audit event type %d not permitted", event);
1578 }
1579
1580 return (0);
1581}
1582
1583int
1584mm_answer_audit_command(int socket, Buffer *m)
1585{
1586 u_int len;
1587 char *cmd;
1588
1589 debug3("%s entering", __func__);
1590 cmd = buffer_get_string(m, &len);
1591 /* sanity check command, if so how? */
1592 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001593 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001594 return (0);
1595}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001596#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001597
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001598void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001599monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001600{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001601 struct ssh *ssh = active_state; /* XXX */
1602 struct kex *kex;
1603 int r;
1604
1605 debug3("%s: packet_set_state", __func__);
1606 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1607 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1608 sshbuf_free(child_state);
1609 child_state = NULL;
1610
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +00001611 if ((kex = ssh->kex) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001612 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001613#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001614 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1615 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +00001616 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1617 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1618 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001619 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1620 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001621# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001622 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001623# endif
Damien Miller773dda22015-01-30 23:10:17 +11001624#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001625 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1626 kex->load_host_public_key=&get_hostkey_public_by_type;
1627 kex->load_host_private_key=&get_hostkey_private_by_type;
1628 kex->host_key_index=&get_hostkey_index;
1629 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001630 }
1631
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001632 /* Update with new address */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001633 if (options.compression) {
1634 ssh_packet_set_compress_hooks(ssh, pmonitor->m_zlib,
1635 (ssh_packet_comp_alloc_func *)mm_zalloc,
1636 (ssh_packet_comp_free_func *)mm_zfree);
1637 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001638}
1639
1640/* This function requries careful sanity checking */
1641
1642void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001643mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001644{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001645 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001646
markus@openbsd.org091c3022015-01-19 19:52:16 +00001647 if ((child_state = sshbuf_new()) == NULL)
1648 fatal("%s: sshbuf_new failed", __func__);
1649 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1650 child_state);
1651 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001652}
1653
1654
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001655/* XXX */
1656
1657#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001658 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001659 fatal("fcntl(%d, F_SETFD)", x); \
1660} while (0)
1661
1662static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001663monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001664{
Damien Miller8f0bf232011-06-20 14:42:23 +10001665 int pair[2];
1666
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001667 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001668 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001669 FD_CLOSEONEXEC(pair[0]);
1670 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001671 mon->m_recvfd = pair[0];
1672 mon->m_sendfd = pair[1];
1673
1674 if (do_logfds) {
1675 if (pipe(pair) == -1)
1676 fatal("%s: pipe: %s", __func__, strerror(errno));
1677 FD_CLOSEONEXEC(pair[0]);
1678 FD_CLOSEONEXEC(pair[1]);
1679 mon->m_log_recvfd = pair[0];
1680 mon->m_log_sendfd = pair[1];
1681 } else
1682 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001683}
1684
1685#define MM_MEMSIZE 65536
1686
1687struct monitor *
1688monitor_init(void)
1689{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001690 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001691 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001692
Damien Miller07d86be2006-03-26 14:19:21 +11001693 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001694
Damien Miller8f0bf232011-06-20 14:42:23 +10001695 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001696
1697 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001698 if (options.compression) {
1699 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1700 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001701
Damien Miller2d6b8332002-06-21 15:59:49 +10001702 /* Compression needs to share state across borders */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001703 ssh_packet_set_compress_hooks(ssh, mon->m_zlib,
1704 (ssh_packet_comp_alloc_func *)mm_zalloc,
1705 (ssh_packet_comp_free_func *)mm_zfree);
Damien Miller2d6b8332002-06-21 15:59:49 +10001706 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001707
1708 return mon;
1709}
1710
1711void
1712monitor_reinit(struct monitor *mon)
1713{
Damien Miller8f0bf232011-06-20 14:42:23 +10001714 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001715}
Darren Tucker0efd1552003-08-26 11:49:55 +10001716
1717#ifdef GSSAPI
1718int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001719mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001720{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001721 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001722 OM_uint32 major;
1723 u_int len;
1724
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001725 if (!options.gss_authentication)
1726 fatal("%s: GSSAPI authentication not enabled", __func__);
1727
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001728 goid.elements = buffer_get_string(m, &len);
1729 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001730
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001731 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001732
Darren Tuckera627d422013-06-02 07:31:17 +10001733 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001734
1735 buffer_clear(m);
1736 buffer_put_int(m, major);
1737
Damien Miller6fd6def2005-11-05 15:07:05 +11001738 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001739
1740 /* Now we have a context, enable the step */
1741 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1742
1743 return (0);
1744}
1745
1746int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001747mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001748{
1749 gss_buffer_desc in;
1750 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001751 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001752 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001753 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001754
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001755 if (!options.gss_authentication)
1756 fatal("%s: GSSAPI authentication not enabled", __func__);
1757
Darren Tucker600ad8d2003-08-26 12:10:48 +10001758 in.value = buffer_get_string(m, &len);
1759 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001760 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001761 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001762
1763 buffer_clear(m);
1764 buffer_put_int(m, major);
1765 buffer_put_string(m, out.value, out.length);
1766 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001767 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001768
1769 gss_release_buffer(&minor, &out);
1770
Damien Miller6fd6def2005-11-05 15:07:05 +11001771 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001772 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1773 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001774 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001775 }
1776 return (0);
1777}
1778
1779int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001780mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001781{
1782 gss_buffer_desc gssbuf, mic;
1783 OM_uint32 ret;
1784 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001785
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001786 if (!options.gss_authentication)
1787 fatal("%s: GSSAPI authentication not enabled", __func__);
1788
Damien Miller0425d402003-11-17 22:18:21 +11001789 gssbuf.value = buffer_get_string(m, &len);
1790 gssbuf.length = len;
1791 mic.value = buffer_get_string(m, &len);
1792 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001793
Damien Miller0425d402003-11-17 22:18:21 +11001794 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001795
Darren Tuckera627d422013-06-02 07:31:17 +10001796 free(gssbuf.value);
1797 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001798
Damien Miller0425d402003-11-17 22:18:21 +11001799 buffer_clear(m);
1800 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001801
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001802 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001803
Damien Miller0425d402003-11-17 22:18:21 +11001804 if (!GSS_ERROR(ret))
1805 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001806
Damien Miller0425d402003-11-17 22:18:21 +11001807 return (0);
1808}
1809
1810int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001811mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001812{
1813 int authenticated;
1814
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001815 if (!options.gss_authentication)
1816 fatal("%s: GSSAPI authentication not enabled", __func__);
1817
Darren Tucker0efd1552003-08-26 11:49:55 +10001818 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1819
1820 buffer_clear(m);
1821 buffer_put_int(m, authenticated);
1822
1823 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001824 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001825
Damien Miller6fd6def2005-11-05 15:07:05 +11001826 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001827
1828 /* Monitor loop will terminate if authenticated */
1829 return (authenticated);
1830}
1831#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001832