blob: e4ac3ccfd825465fd70d2b8fff5f0df4011e0317 [file] [log] [blame]
tb@openbsd.org34843802018-02-05 05:37:46 +00001/* $OpenBSD: monitor.c,v 1.179 2018/02/05 05:37:46 tb 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"
Damien Millerd7834352006-08-05 12:39:39 +100097#ifdef GSSAPI
98#include "ssh-gss.h"
99#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000100#include "monitor_wrap.h"
101#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000102#include "compat.h"
103#include "ssh2.h"
Damien Miller85b45e02013-07-20 13:21:52 +1000104#include "authfd.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000105#include "match.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000106#include "ssherr.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107
Darren Tucker0efd1552003-08-26 11:49:55 +1000108#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000109static Gssctxt *gsscontext = NULL;
110#endif
111
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000112/* Imports */
113extern ServerOptions options;
114extern u_int utmp_len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000115extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000116extern Buffer auth_debug;
117extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000118extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000119
120/* State exported from the child */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000121static struct sshbuf *child_state;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000122
Damien Miller469954d2003-06-18 20:25:33 +1000123/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000124
125int mm_answer_moduli(int, Buffer *);
126int mm_answer_sign(int, Buffer *);
127int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000128int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000129int mm_answer_authserv(int, Buffer *);
130int mm_answer_authpassword(int, Buffer *);
131int mm_answer_bsdauthquery(int, Buffer *);
132int mm_answer_bsdauthrespond(int, Buffer *);
133int mm_answer_skeyquery(int, Buffer *);
134int mm_answer_skeyrespond(int, Buffer *);
135int mm_answer_keyallowed(int, Buffer *);
136int mm_answer_keyverify(int, Buffer *);
137int mm_answer_pty(int, Buffer *);
138int mm_answer_pty_cleanup(int, Buffer *);
139int mm_answer_term(int, Buffer *);
140int mm_answer_rsa_keyallowed(int, Buffer *);
141int mm_answer_rsa_challenge(int, Buffer *);
142int mm_answer_rsa_response(int, Buffer *);
143int mm_answer_sesskey(int, Buffer *);
144int mm_answer_sessid(int, Buffer *);
145
Damien Miller79418552002-04-23 20:28:48 +1000146#ifdef USE_PAM
147int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000148int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000149int mm_answer_pam_init_ctx(int, Buffer *);
150int mm_answer_pam_query(int, Buffer *);
151int mm_answer_pam_respond(int, Buffer *);
152int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000153#endif
154
Darren Tucker0efd1552003-08-26 11:49:55 +1000155#ifdef GSSAPI
156int mm_answer_gss_setup_ctx(int, Buffer *);
157int mm_answer_gss_accept_ctx(int, Buffer *);
158int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100159int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000160#endif
Damien Miller25162f22002-09-12 09:47:29 +1000161
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100162#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100163int mm_answer_audit_event(int, Buffer *);
164int mm_answer_audit_command(int, Buffer *);
165#endif
166
Damien Miller8f0bf232011-06-20 14:42:23 +1000167static int monitor_read_log(struct monitor *);
168
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000169static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000170
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171/* local state for key verify */
172static u_char *key_blob = NULL;
173static u_int key_bloblen = 0;
174static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000175static char *hostbased_cuser = NULL;
176static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000177static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100178static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000179static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000180static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000181static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000182
183struct mon_table {
184 enum monitor_reqtype type;
185 int flags;
186 int (*f)(int, Buffer *);
187};
188
189#define MON_ISAUTH 0x0004 /* Required for Authentication */
190#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
191#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100192#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000193
194#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
195
196#define MON_PERMIT 0x1000 /* Request is permitted */
197
198struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000199#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000200 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000201#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
203 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
204 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000205 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000207#ifdef USE_PAM
208 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000209 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Millerb38b95f2016-08-29 11:47:07 +1000210 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
211 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
212 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000213 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000214#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100215#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100216 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100217#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000218#ifdef BSD_AUTH
219 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100220 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000221#endif
222#ifdef SKEY
223 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
224 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
225#endif
226 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
227 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000228#ifdef GSSAPI
229 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
djm@openbsd.orgb33ad6d2016-09-05 13:57:31 +0000230 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
231 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
232 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000233#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000234 {0, 0, NULL}
235};
236
237struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000238#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000239 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000240#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000241 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
242 {MONITOR_REQ_PTY, 0, mm_answer_pty},
243 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
244 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100245#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100246 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
247 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
248#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000249 {0, 0, NULL}
250};
251
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000252struct mon_table *mon_dispatch;
253
254/* Specifies if a certain message is allowed at the moment */
255
256static void
257monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
258{
259 while (ent->f != NULL) {
260 if (ent->type == type) {
261 ent->flags &= ~MON_PERMIT;
262 ent->flags |= permit ? MON_PERMIT : 0;
263 return;
264 }
265 ent++;
266 }
267}
268
269static void
270monitor_permit_authentications(int permit)
271{
272 struct mon_table *ent = mon_dispatch;
273
274 while (ent->f != NULL) {
275 if (ent->flags & MON_AUTH) {
276 ent->flags &= ~MON_PERMIT;
277 ent->flags |= permit ? MON_PERMIT : 0;
278 }
279 ent++;
280 }
281}
282
Darren Tucker3e33cec2003-10-02 16:12:36 +1000283void
284monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000285{
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000286 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000287 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
tb@openbsd.org34843802018-02-05 05:37:46 +0000292 if (pmonitor->m_recvfd >= 0)
293 close(pmonitor->m_recvfd);
294 if (pmonitor->m_log_sendfd >= 0)
295 close(pmonitor->m_log_sendfd);
Damien Miller8f0bf232011-06-20 14:42:23 +1000296 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
297
Darren Tucker3e33cec2003-10-02 16:12:36 +1000298 authctxt = _authctxt;
299 memset(authctxt, 0, sizeof(*authctxt));
300
Darren Tuckerde0de392005-03-31 23:52:04 +1000301 authctxt->loginmsg = &loginmsg;
302
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000303 mon_dispatch = mon_dispatch_proto20;
304 /* Permit requests for moduli and signatures */
305 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
306 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000307
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000308 /* The first few requests do not require asynchronous access */
309 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100310 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100311 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100312 auth_submethod = NULL;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000313 auth2_authctxt_reset_info(authctxt);
314
Darren Tuckerfbba7352006-11-07 23:16:08 +1100315 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100316
317 /* Special handling for multiple required authentications */
318 if (options.num_auth_methods != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100319 if (authenticated &&
320 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000321 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100322 debug3("%s: method %s: partial", __func__,
323 auth_method);
324 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100325 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100326 }
327 }
328
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000329 if (authenticated) {
330 if (!(ent->flags & MON_AUTHDECIDE))
331 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000332 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000333 if (authctxt->pw->pw_uid == 0 &&
334 !auth_root_allowed(auth_method))
335 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000336#ifdef USE_PAM
337 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100338 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000339 Buffer m;
340
341 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100342 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000343 MONITOR_REQ_PAM_ACCOUNT, &m);
344 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
345 buffer_free(&m);
346 }
347#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100349 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100350 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000351 auth_method, auth_submethod);
djm@openbsd.org94885382015-06-22 23:42:16 +0000352 if (!partial && !authenticated)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000353 authctxt->failures++;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000354 if (authenticated || partial) {
355 auth2_update_session_info(authctxt,
356 auth_method, auth_submethod);
357 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000358 }
359 }
360
361 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000362 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100363 if (strcmp(auth_method, "unknown") == 0)
364 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000365
366 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000367 __func__, authctxt->user);
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000368 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000369
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000370 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000371
Damien Miller6a1937e2012-12-12 10:44:38 +1100372 /* Drain any buffered messages from the child */
373 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
374 ;
375
tb@openbsd.org34843802018-02-05 05:37:46 +0000376 if (pmonitor->m_recvfd >= 0)
377 close(pmonitor->m_recvfd);
378 if (pmonitor->m_log_sendfd >= 0)
379 close(pmonitor->m_log_sendfd);
Damien Miller8f0bf232011-06-20 14:42:23 +1000380 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000381}
382
Damien Millerbe64d432003-05-14 19:31:12 +1000383static void
384monitor_set_child_handler(pid_t pid)
385{
386 monitor_child_pid = pid;
387}
388
389static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000390monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000391{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000392 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000393}
394
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000395void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000396monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000397{
Damien Miller8f0bf232011-06-20 14:42:23 +1000398 close(pmonitor->m_recvfd);
399 pmonitor->m_recvfd = -1;
400
Damien Millerbe64d432003-05-14 19:31:12 +1000401 monitor_set_child_handler(pmonitor->m_pid);
402 signal(SIGHUP, &monitor_child_handler);
403 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000404 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000405#ifdef SIGXFSZ
406 signal(SIGXFSZ, SIG_IGN);
407#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000408
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000409 mon_dispatch = mon_dispatch_postauth20;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000410
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000411 /* Permit requests for moduli and signatures */
412 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
413 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
414 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
415
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000416 if (!no_pty_flag) {
417 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
418 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
419 }
420
421 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000422 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000423}
424
Damien Miller8f0bf232011-06-20 14:42:23 +1000425static int
426monitor_read_log(struct monitor *pmonitor)
427{
428 Buffer logmsg;
429 u_int len, level;
430 char *msg;
431
432 buffer_init(&logmsg);
433
434 /* Read length */
435 buffer_append_space(&logmsg, 4);
436 if (atomicio(read, pmonitor->m_log_recvfd,
437 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
438 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100439 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000440 debug("%s: child log fd closed", __func__);
441 close(pmonitor->m_log_recvfd);
442 pmonitor->m_log_recvfd = -1;
443 return -1;
444 }
445 fatal("%s: log fd read: %s", __func__, strerror(errno));
446 }
447 len = buffer_get_int(&logmsg);
448 if (len <= 4 || len > 8192)
449 fatal("%s: invalid log message length %u", __func__, len);
450
451 /* Read severity, message */
452 buffer_clear(&logmsg);
453 buffer_append_space(&logmsg, len);
454 if (atomicio(read, pmonitor->m_log_recvfd,
455 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
456 fatal("%s: log fd read: %s", __func__, strerror(errno));
457
458 /* Log it */
459 level = buffer_get_int(&logmsg);
460 msg = buffer_get_string(&logmsg, NULL);
461 if (log_level_name(level) == NULL)
462 fatal("%s: invalid log level %u (corrupted message?)",
463 __func__, level);
464 do_log2(level, "%s [preauth]", msg);
465
466 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000467 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000468
469 return 0;
470}
471
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000472int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000473monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000474 struct mon_table **pent)
475{
476 Buffer m;
477 int ret;
478 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000479 struct pollfd pfd[2];
480
481 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100482 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000483 pfd[0].fd = pmonitor->m_sendfd;
484 pfd[0].events = POLLIN;
485 pfd[1].fd = pmonitor->m_log_recvfd;
486 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000487 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
488 if (errno == EINTR || errno == EAGAIN)
489 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000490 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000491 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000492 if (pfd[1].revents) {
493 /*
494 * Drain all log messages before processing next
495 * monitor request.
496 */
497 monitor_read_log(pmonitor);
498 continue;
499 }
500 if (pfd[0].revents)
501 break; /* Continues below */
502 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000503
504 buffer_init(&m);
505
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000506 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000507 type = buffer_get_char(&m);
508
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000509 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000510
511 while (ent->f != NULL) {
512 if (ent->type == type)
513 break;
514 ent++;
515 }
516
517 if (ent->f != NULL) {
518 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000519 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000520 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000521 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000522 buffer_free(&m);
523
524 /* The child may use this request only once, disable it */
525 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000526 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000527 type);
528 ent->flags &= ~MON_PERMIT;
529 }
530
531 if (pent != NULL)
532 *pent = ent;
533
534 return ret;
535 }
536
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000537 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000538
539 /* NOTREACHED */
540 return (-1);
541}
542
543/* allowed key state */
544static int
545monitor_allowed_key(u_char *blob, u_int bloblen)
546{
547 /* make sure key is allowed */
548 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000549 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000550 return (0);
551 return (1);
552}
553
554static void
555monitor_reset_key_state(void)
556{
557 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000558 free(key_blob);
559 free(hostbased_cuser);
560 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561 key_blob = NULL;
562 key_bloblen = 0;
563 key_blobtype = MM_NOKEY;
564 hostbased_cuser = NULL;
565 hostbased_chost = NULL;
566}
567
Damien Miller1f0311c2014-05-15 14:24:09 +1000568#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000569int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000570mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000571{
572 DH *dh;
573 int min, want, max;
574
575 min = buffer_get_int(m);
576 want = buffer_get_int(m);
577 max = buffer_get_int(m);
578
579 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000580 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000581 /* We need to check here, too, in case the child got corrupted */
582 if (max < min || want < min || max < want)
583 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000584 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000585
586 buffer_clear(m);
587
588 dh = choose_dh(min, want, max);
589 if (dh == NULL) {
590 buffer_put_char(m, 0);
591 return (0);
592 } else {
593 /* Send first bignum */
594 buffer_put_char(m, 1);
595 buffer_put_bignum2(m, dh->p);
596 buffer_put_bignum2(m, dh->g);
597
598 DH_free(dh);
599 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000600 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000601 return (0);
602}
Damien Miller1f0311c2014-05-15 14:24:09 +1000603#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604
605int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000606mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000607{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000608 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000609 extern int auth_sock; /* XXX move to state struct? */
610 struct sshkey *key;
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000611 struct sshbuf *sigbuf = NULL;
612 u_char *p = NULL, *signature = NULL;
613 char *alg = NULL;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000614 size_t datlen, siglen, alglen;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000615 int r, is_proof = 0;
616 u_int keyid;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000617 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000618
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000619 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000620
djm@openbsd.org141efe42015-01-14 20:05:27 +0000621 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000622 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
623 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000624 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000625 if (keyid > INT_MAX)
626 fatal("%s: invalid key ID", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000627
Damien Millera63128d2006-03-15 12:08:28 +1100628 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000629 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
630 * SHA384 (48 bytes) and SHA512 (64 bytes).
djm@openbsd.org523463a2015-02-16 22:13:32 +0000631 *
632 * Otherwise, verify the signature request is for a hostkey
633 * proof.
634 *
635 * XXX perform similar check for KEX signature requests too?
636 * it's not trivial, since what is signed is the hash, rather
637 * than the full kex structure...
Damien Millera63128d2006-03-15 12:08:28 +1100638 */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000639 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
640 /*
641 * Construct expected hostkey proof and compare it to what
642 * the client sent us.
643 */
644 if (session_id2_len == 0) /* hostkeys is never first */
645 fatal("%s: bad data length: %zu", __func__, datlen);
646 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
647 fatal("%s: no hostkey for index %d", __func__, keyid);
648 if ((sigbuf = sshbuf_new()) == NULL)
649 fatal("%s: sshbuf_new", __func__);
djm@openbsd.org44732de2015-02-20 22:17:21 +0000650 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
651 (r = sshbuf_put_string(sigbuf, session_id2,
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000652 session_id2_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000653 (r = sshkey_puts(key, sigbuf)) != 0)
654 fatal("%s: couldn't prepare private key "
655 "proof buffer: %s", __func__, ssh_err(r));
656 if (datlen != sshbuf_len(sigbuf) ||
657 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
658 fatal("%s: bad data length: %zu, hostkey proof len %zu",
659 __func__, datlen, sshbuf_len(sigbuf));
660 sshbuf_free(sigbuf);
661 is_proof = 1;
662 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663
Ben Lindstromf67e0772002-06-06 20:58:19 +0000664 /* save session id, it will be passed on the first call */
665 if (session_id2_len == 0) {
666 session_id2_len = datlen;
667 session_id2 = xmalloc(session_id2_len);
668 memcpy(session_id2, p, session_id2_len);
669 }
670
Damien Miller85b45e02013-07-20 13:21:52 +1000671 if ((key = get_hostkey_by_index(keyid)) != NULL) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000672 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000673 datafellows)) != 0)
674 fatal("%s: sshkey_sign failed: %s",
675 __func__, ssh_err(r));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000676 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000677 auth_sock > 0) {
678 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000679 p, datlen, alg, datafellows)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000680 fatal("%s: ssh_agent_sign failed: %s",
681 __func__, ssh_err(r));
682 }
Damien Miller85b45e02013-07-20 13:21:52 +1000683 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000684 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685
djm@openbsd.org523463a2015-02-16 22:13:32 +0000686 debug3("%s: %s signature %p(%zu)", __func__,
687 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000688
djm@openbsd.org141efe42015-01-14 20:05:27 +0000689 sshbuf_reset(m);
690 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
691 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000692
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000693 free(alg);
Darren Tuckera627d422013-06-02 07:31:17 +1000694 free(p);
695 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000696
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000697 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000698
699 /* Turn on permissions for getpwnam */
700 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
701
702 return (0);
703}
704
705/* Retrieves the password entry and also checks if the user is permitted */
706
707int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000708mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000709{
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000710 struct ssh *ssh = active_state; /* XXX */
Darren Tuckerb09b6772004-06-22 15:06:46 +1000711 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000712 struct passwd *pwent;
713 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000714 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000715
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000716 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000717
718 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000719 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000720
Darren Tuckerb09b6772004-06-22 15:06:46 +1000721 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000722
Darren Tuckerb09b6772004-06-22 15:06:46 +1000723 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000724
Darren Tuckerb09b6772004-06-22 15:06:46 +1000725 authctxt->user = xstrdup(username);
726 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000727 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728
729 buffer_clear(m);
730
731 if (pwent == NULL) {
732 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100733 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000734 goto out;
735 }
736
737 allowed = 1;
738 authctxt->pw = pwent;
739 authctxt->valid = 1;
740
741 buffer_put_char(m, 1);
742 buffer_put_string(m, pwent, sizeof(struct passwd));
743 buffer_put_cstring(m, pwent->pw_name);
744 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000745#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000746 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000747#endif
748#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000749 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000750#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000751 buffer_put_cstring(m, pwent->pw_dir);
752 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100753
754 out:
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000755 ssh_packet_set_log_preamble(ssh, "%suser %s",
756 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
Darren Tucker1629c072007-02-19 22:25:37 +1100757 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000758
759#define M_CP_STROPT(x) do { \
760 if (options.x != NULL) \
761 buffer_put_cstring(m, options.x); \
762 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000763#define M_CP_STRARRAYOPT(x, nx) do { \
764 for (i = 0; i < options.nx; i++) \
765 buffer_put_cstring(m, options.x[i]); \
766 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000767 /* See comment in servconf.h */
768 COPY_MATCH_STRING_OPTS();
769#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000770#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100771
772 /* Create valid auth method lists */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000773 if (auth2_setup_methods_lists(authctxt) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100774 /*
775 * The monitor will continue long enough to let the child
776 * run to it's packet_disconnect(), but it must not allow any
777 * authentication to succeed.
778 */
779 debug("%s: no valid authentication method lists", __func__);
780 }
781
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000782 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000783 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000784
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000785 /* Allow service/style information on the auth context */
786 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
787 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
788
Damien Millera33501b2002-05-08 12:24:42 +1000789#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000790 if (options.use_pam)
791 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000792#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000793
794 return (0);
795}
796
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000797int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000798{
799 char *banner;
800
801 buffer_clear(m);
802 banner = auth2_read_banner();
803 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000804 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000805 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000806
807 return (0);
808}
809
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000810int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000811mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000812{
813 monitor_permit_authentications(1);
814
815 authctxt->service = buffer_get_string(m, NULL);
816 authctxt->style = buffer_get_string(m, NULL);
817 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000818 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000819
820 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000821 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000822 authctxt->style = NULL;
823 }
824
825 return (0);
826}
827
828int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000829mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000830{
831 static int call_count;
832 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000833 int authenticated;
834 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000835
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000836 if (!options.password_authentication)
837 fatal("%s: password authentication not enabled", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000838 passwd = buffer_get_string(m, &plen);
839 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000840 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000841 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100842 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000843 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000844
845 buffer_clear(m);
846 buffer_put_int(m, authenticated);
Darren Tucker01558b72016-07-18 09:33:25 +1000847#ifdef USE_PAM
848 buffer_put_int(m, sshpam_get_maxtries_reached());
849#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000850
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000851 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000852 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000853
854 call_count++;
855 if (plen == 0 && call_count == 1)
856 auth_method = "none";
857 else
858 auth_method = "password";
859
860 /* Causes monitor loop to terminate if authenticated */
861 return (authenticated);
862}
863
864#ifdef BSD_AUTH
865int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000866mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000867{
868 char *name, *infotxt;
869 u_int numprompts;
870 u_int *echo_on;
871 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100872 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000873
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000874 if (!options.kbd_interactive_authentication)
875 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb7df3af2003-02-24 11:55:46 +1100876 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
877 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000878
879 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100880 buffer_put_int(m, success);
881 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000882 buffer_put_cstring(m, prompts[0]);
883
Damien Millerb7df3af2003-02-24 11:55:46 +1100884 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000885 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000886
Damien Millerb7df3af2003-02-24 11:55:46 +1100887 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000888 free(name);
889 free(infotxt);
890 free(prompts);
891 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000892 }
893
894 return (0);
895}
896
897int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000898mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000899{
900 char *response;
901 int authok;
902
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000903 if (!options.kbd_interactive_authentication)
904 fatal("%s: kbd-int authentication not enabled", __func__);
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +0000905 if (authctxt->as == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000906 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000907
908 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000909 authok = options.challenge_response_authentication &&
910 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000911 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000912 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000913 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000914
915 buffer_clear(m);
916 buffer_put_int(m, authok);
917
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000918 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000919 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000920
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000921 auth_method = "keyboard-interactive";
922 auth_submethod = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000923
924 return (authok != 0);
925}
926#endif
927
928#ifdef SKEY
929int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000930mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000931{
932 struct skey skey;
933 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100934 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000935
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000936 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
937 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000938
939 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100940 buffer_put_int(m, success);
941 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000942 buffer_put_cstring(m, challenge);
943
Damien Millerb7df3af2003-02-24 11:55:46 +1100944 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000945 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000946
947 return (0);
948}
949
950int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000951mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000952{
953 char *response;
954 int authok;
955
956 response = buffer_get_string(m, NULL);
957
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000958 authok = (options.challenge_response_authentication &&
959 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000960 skey_haskey(authctxt->pw->pw_name) == 0 &&
961 skey_passcheck(authctxt->pw->pw_name, response) != -1);
962
Darren Tuckerf60845f2013-06-02 08:07:31 +1000963 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000964
965 buffer_clear(m);
966 buffer_put_int(m, authok);
967
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000968 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000969 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000970
Damien Millerac947352015-11-20 12:35:41 +1100971 auth_method = "keyboard-interactive";
972 auth_submethod = "skey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000973
974 return (authok != 0);
975}
976#endif
977
Damien Miller79418552002-04-23 20:28:48 +1000978#ifdef USE_PAM
979int
Darren Tucker7eda5922004-06-22 13:22:50 +1000980mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000981{
Damien Miller4e448a32003-05-14 15:11:48 +1000982 if (!options.use_pam)
983 fatal("UsePAM not set, but ended up in %s anyway", __func__);
984
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100985 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000986
Damien Miller1f499fd2003-08-25 13:08:49 +1000987 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
Damien Miller775f8a22016-08-31 10:48:07 +1000988 if (options.kbd_interactive_authentication)
989 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller1f499fd2003-08-25 13:08:49 +1000990
Damien Miller79418552002-04-23 20:28:48 +1000991 return (0);
992}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000993
Damien Miller1f499fd2003-08-25 13:08:49 +1000994int
Darren Tucker7eda5922004-06-22 13:22:50 +1000995mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000996{
997 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100998
Damien Miller1f499fd2003-08-25 13:08:49 +1000999 if (!options.use_pam)
Damien Miller775f8a22016-08-31 10:48:07 +10001000 fatal("%s: PAM not enabled", __func__);
Damien Miller1f499fd2003-08-25 13:08:49 +10001001
1002 ret = do_pam_account();
1003
1004 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001005 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001006
Darren Tucker7eda5922004-06-22 13:22:50 +10001007 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001008
1009 return (ret);
1010}
1011
Damien Miller4f9f42a2003-05-10 19:28:02 +10001012static void *sshpam_ctxt, *sshpam_authok;
1013extern KbdintDevice sshpam_device;
1014
1015int
Darren Tucker7eda5922004-06-22 13:22:50 +10001016mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001017{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001018 debug3("%s", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001019 if (!options.kbd_interactive_authentication)
1020 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001021 if (sshpam_ctxt != NULL)
1022 fatal("%s: already called", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001023 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1024 sshpam_authok = NULL;
1025 buffer_clear(m);
1026 if (sshpam_ctxt != NULL) {
1027 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
Damien Millerb38b95f2016-08-29 11:47:07 +10001028 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001029 buffer_put_int(m, 1);
1030 } else {
1031 buffer_put_int(m, 0);
1032 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001033 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001034 return (0);
1035}
1036
1037int
Darren Tucker7eda5922004-06-22 13:22:50 +10001038mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001039{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001040 char *name = NULL, *info = NULL, **prompts = NULL;
1041 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001042 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001043
1044 debug3("%s", __func__);
1045 sshpam_authok = NULL;
Damien Millerb38b95f2016-08-29 11:47:07 +10001046 if (sshpam_ctxt == NULL)
1047 fatal("%s: no context", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001048 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1049 &num, &prompts, &echo_on);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001050 if (ret == 0 && num == 0)
1051 sshpam_authok = sshpam_ctxt;
1052 if (num > 1 || name == NULL || info == NULL)
Damien Millerb38b95f2016-08-29 11:47:07 +10001053 fatal("sshpam_device.query failed");
1054 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001055 buffer_clear(m);
1056 buffer_put_int(m, ret);
1057 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001058 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001059 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001060 free(info);
Darren Tucker01558b72016-07-18 09:33:25 +10001061 buffer_put_int(m, sshpam_get_maxtries_reached());
Damien Miller4f9f42a2003-05-10 19:28:02 +10001062 buffer_put_int(m, num);
1063 for (i = 0; i < num; ++i) {
1064 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001065 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001066 buffer_put_int(m, echo_on[i]);
1067 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001068 free(prompts);
1069 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001070 auth_method = "keyboard-interactive";
1071 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001072 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001073 return (0);
1074}
1075
1076int
Darren Tucker7eda5922004-06-22 13:22:50 +10001077mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001078{
1079 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001080 u_int i, num;
1081 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001082
1083 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001084 if (sshpam_ctxt == NULL)
1085 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001086 sshpam_authok = NULL;
1087 num = buffer_get_int(m);
1088 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001089 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001090 for (i = 0; i < num; ++i)
1091 resp[i] = buffer_get_string(m, NULL);
1092 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1093 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001094 free(resp[i]);
1095 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001096 } else {
1097 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1098 }
1099 buffer_clear(m);
1100 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001101 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001102 auth_method = "keyboard-interactive";
1103 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001104 if (ret == 0)
1105 sshpam_authok = sshpam_ctxt;
1106 return (0);
1107}
1108
1109int
Darren Tucker7eda5922004-06-22 13:22:50 +10001110mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001111{
Damien Miller5e75f512015-08-11 13:34:12 +10001112 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001113
1114 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001115 if (sshpam_ctxt == NULL)
1116 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001117 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001118 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001119 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001120 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Millerb38b95f2016-08-29 11:47:07 +10001121 /* Allow another attempt */
1122 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller15b05cf2012-12-03 09:53:20 +11001123 auth_method = "keyboard-interactive";
1124 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001125 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001126}
Damien Miller79418552002-04-23 20:28:48 +10001127#endif
1128
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001129int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001130mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001131{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001132 struct sshkey *key;
Damien Millera10f5612002-09-12 09:49:15 +10001133 char *cuser, *chost;
1134 u_char *blob;
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001135 u_int bloblen, pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001136 enum mm_keytype type = 0;
1137 int allowed = 0;
1138
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001139 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001140
1141 type = buffer_get_int(m);
1142 cuser = buffer_get_string(m, NULL);
1143 chost = buffer_get_string(m, NULL);
1144 blob = buffer_get_string(m, &bloblen);
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001145 pubkey_auth_attempt = buffer_get_int(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001146
1147 key = key_from_blob(blob, bloblen);
1148
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001149 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001150
Damien Miller3e3b5142003-11-17 21:13:40 +11001151 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001152 /* These should not make it past the privsep child */
1153 if (key_type_plain(key->type) == KEY_RSA &&
1154 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1155 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1156
Darren Tucker47eede72005-03-14 23:08:12 +11001157 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001158 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001159 allowed = options.pubkey_authentication &&
djm@openbsd.org8f574952017-06-24 06:34:38 +00001160 !auth2_key_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001161 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001162 options.pubkey_key_types, 0) == 1 &&
1163 user_key_allowed(authctxt->pw, key,
1164 pubkey_auth_attempt);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001165 auth_method = "publickey";
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001166 if (options.pubkey_authentication &&
1167 (!pubkey_auth_attempt || allowed != 1))
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001168 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001169 break;
1170 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001171 allowed = options.hostbased_authentication &&
djm@openbsd.org8f574952017-06-24 06:34:38 +00001172 !auth2_key_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001173 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001174 options.hostbased_key_types, 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001175 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001176 cuser, chost, key);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001177 auth2_record_info(authctxt,
Damien Miller20bdcd72013-07-18 16:10:09 +10001178 "client user \"%.100s\", client host \"%.100s\"",
1179 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001180 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001181 break;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001182 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001183 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001184 break;
1185 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001186 }
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001187
djm@openbsd.org8f574952017-06-24 06:34:38 +00001188 debug3("%s: key is %s", __func__, allowed ? "allowed" : "not allowed");
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001189
djm@openbsd.org8f574952017-06-24 06:34:38 +00001190 auth2_record_key(authctxt, 0, key);
1191 sshkey_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001192
1193 /* clear temporarily storage (used by verify) */
1194 monitor_reset_key_state();
1195
1196 if (allowed) {
1197 /* Save temporarily for comparison in verify */
1198 key_blob = blob;
1199 key_bloblen = bloblen;
1200 key_blobtype = type;
1201 hostbased_cuser = cuser;
1202 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001203 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001204 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001205 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001206 free(blob);
1207 free(cuser);
1208 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001209 }
1210
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001211 buffer_clear(m);
1212 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001213 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001214
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001215 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001216
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001217 return (0);
1218}
1219
1220static int
1221monitor_valid_userblob(u_char *data, u_int datalen)
1222{
1223 Buffer b;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001224 u_char *p;
1225 char *userstyle, *cp;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001226 u_int len;
1227 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001228
1229 buffer_init(&b);
1230 buffer_append(&b, data, datalen);
1231
1232 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001233 p = buffer_ptr(&b);
1234 len = buffer_len(&b);
1235 if ((session_id2 == NULL) ||
1236 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001237 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001238 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001239 buffer_consume(&b, session_id2_len);
1240 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001241 p = buffer_get_string(&b, &len);
1242 if ((session_id2 == NULL) ||
1243 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001244 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001245 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001246 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001247 }
1248 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1249 fail++;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001250 cp = buffer_get_cstring(&b, NULL);
Damien Miller4ce189d2013-04-23 15:17:52 +10001251 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1252 authctxt->style ? ":" : "",
1253 authctxt->style ? authctxt->style : "");
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001254 if (strcmp(userstyle, cp) != 0) {
1255 logit("wrong user name passed to monitor: "
1256 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001257 fail++;
1258 }
Darren Tuckera627d422013-06-02 07:31:17 +10001259 free(userstyle);
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001260 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001261 buffer_skip_string(&b);
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001262 cp = buffer_get_cstring(&b, NULL);
1263 if (strcmp("publickey", cp) != 0)
1264 fail++;
1265 free(cp);
1266 if (!buffer_get_char(&b))
1267 fail++;
1268 buffer_skip_string(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001269 buffer_skip_string(&b);
1270 if (buffer_len(&b) != 0)
1271 fail++;
1272 buffer_free(&b);
1273 return (fail == 0);
1274}
1275
1276static int
Damien Millera10f5612002-09-12 09:49:15 +10001277monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1278 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001279{
1280 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001281 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001282 u_int len;
1283 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001284
1285 buffer_init(&b);
1286 buffer_append(&b, data, datalen);
1287
Ben Lindstromf67e0772002-06-06 20:58:19 +00001288 p = buffer_get_string(&b, &len);
1289 if ((session_id2 == NULL) ||
1290 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001291 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001292 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001293 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001294
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1296 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001297 p = buffer_get_cstring(&b, NULL);
1298 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1299 authctxt->style ? ":" : "",
1300 authctxt->style ? authctxt->style : "");
1301 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001302 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001303 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001304 fail++;
1305 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001306 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001307 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001308 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001309 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001310 if (strcmp(p, "hostbased") != 0)
1311 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001312 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001313 buffer_skip_string(&b); /* pkalg */
1314 buffer_skip_string(&b); /* pkblob */
1315
1316 /* verify client host, strip trailing dot if necessary */
1317 p = buffer_get_string(&b, NULL);
1318 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1319 p[len - 1] = '\0';
1320 if (strcmp(p, chost) != 0)
1321 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001322 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001323
1324 /* verify client user */
1325 p = buffer_get_string(&b, NULL);
1326 if (strcmp(p, cuser) != 0)
1327 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001328 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001329
1330 if (buffer_len(&b) != 0)
1331 fail++;
1332 buffer_free(&b);
1333 return (fail == 0);
1334}
1335
1336int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001337mm_answer_keyverify(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001338{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001339 struct sshkey *key;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001340 u_char *signature, *data, *blob;
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001341 char *sigalg;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001342 size_t signaturelen, datalen, bloblen;
1343 int r, ret, valid_data = 0, encoded_ret;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001344
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001345 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1346 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001347 (r = sshbuf_get_string(m, &data, &datalen)) != 0 ||
1348 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001349 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001350
1351 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001352 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001353 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001354
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001355 /* Empty signature algorithm means NULL. */
1356 if (*sigalg == '\0') {
1357 free(sigalg);
1358 sigalg = NULL;
1359 }
1360
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001361 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1362 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1363 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001364
1365 switch (key_blobtype) {
1366 case MM_USERKEY:
1367 valid_data = monitor_valid_userblob(data, datalen);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001368 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001369 break;
1370 case MM_HOSTKEY:
1371 valid_data = monitor_valid_hostbasedblob(data, datalen,
1372 hostbased_cuser, hostbased_chost);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001373 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001374 break;
1375 default:
1376 valid_data = 0;
1377 break;
1378 }
1379 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001380 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001381
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001382 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001383 sigalg, active_state->compat);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001384 debug3("%s: %s %p signature %s", __func__, auth_method, key,
1385 (ret == 0) ? "verified" : "unverified");
1386 auth2_record_key(authctxt, ret == 0, key);
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001387
Darren Tuckera627d422013-06-02 07:31:17 +10001388 free(blob);
1389 free(signature);
1390 free(data);
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001391 free(sigalg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001392
1393 monitor_reset_key_state();
1394
djm@openbsd.org8f574952017-06-24 06:34:38 +00001395 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001396 sshbuf_reset(m);
1397
1398 /* encode ret != 0 as positive integer, since we're sending u32 */
1399 encoded_ret = (ret != 0);
1400 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1401 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001402 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001403
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001404 return ret == 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001405}
1406
1407static void
1408mm_record_login(Session *s, struct passwd *pw)
1409{
djm@openbsd.org95767262016-03-07 19:02:43 +00001410 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001411 socklen_t fromlen;
1412 struct sockaddr_storage from;
1413
1414 /*
1415 * Get IP address of client. If the connection is not a socket, let
1416 * the address be 0.0.0.0.
1417 */
1418 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001419 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001420 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001421 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001422 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001423 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001424 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001425 }
1426 }
1427 /* Record that there was a login on that tty from the remote host. */
1428 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +00001429 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001430 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001431}
1432
1433static void
1434mm_session_close(Session *s)
1435{
Damien Miller04bd8b02003-05-25 14:38:33 +10001436 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001437 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001438 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001439 session_pty_cleanup2(s);
1440 }
Damien Miller7207f642008-05-19 15:34:50 +10001441 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001442}
1443
1444int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001445mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001446{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001447 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001448 Session *s;
1449 int res, fd0;
1450
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001451 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001452
1453 buffer_clear(m);
1454 s = session_new();
1455 if (s == NULL)
1456 goto error;
1457 s->authctxt = authctxt;
1458 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001459 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001460 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1461 if (res == 0)
1462 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001463 pty_setowner(authctxt->pw, s->tty);
1464
1465 buffer_put_int(m, 1);
1466 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001467
1468 /* We need to trick ttyslot */
1469 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001470 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001471
1472 mm_record_login(s, authctxt->pw);
1473
1474 /* Now we can close the file descriptor again */
1475 close(0);
1476
Darren Tucker09991742004-07-17 17:05:14 +10001477 /* send messages generated by record_login */
1478 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1479 buffer_clear(&loginmsg);
1480
1481 mm_request_send(sock, MONITOR_ANS_PTY, m);
1482
Damien Miller54fd7cf2007-09-17 12:04:08 +10001483 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1484 mm_send_fd(sock, s->ttyfd) == -1)
1485 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001486
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001487 /* make sure nothing uses fd 0 */
1488 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001489 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001490 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001491 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492
1493 /* slave is not needed */
1494 close(s->ttyfd);
1495 s->ttyfd = s->ptyfd;
1496 /* no need to dup() because nobody closes ptyfd */
1497 s->ptymaster = s->ptyfd;
1498
Damien Miller9ab00b42006-08-05 12:40:11 +10001499 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001500
1501 return (0);
1502
1503 error:
1504 if (s != NULL)
1505 mm_session_close(s);
1506 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001507 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001508 return (0);
1509}
1510
1511int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001512mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001513{
1514 Session *s;
1515 char *tty;
1516
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001517 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001518
1519 tty = buffer_get_string(m, NULL);
1520 if ((s = session_by_tty(tty)) != NULL)
1521 mm_session_close(s);
1522 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001523 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001524 return (0);
1525}
1526
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001527int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001528mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001529{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001530 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001531 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532 int res, status;
1533
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001534 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001535
1536 /* The child is terminating */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001537 session_destroy_all(ssh, &mm_session_close);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001538
Darren Tucker52358d62008-03-11 22:58:25 +11001539#ifdef USE_PAM
1540 if (options.use_pam)
1541 sshpam_cleanup();
1542#endif
1543
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001544 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001545 if (errno != EINTR)
1546 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001547
1548 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1549
1550 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001551 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001552}
1553
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001554#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001555/* Report that an audit event occurred */
1556int
1557mm_answer_audit_event(int socket, Buffer *m)
1558{
1559 ssh_audit_event_t event;
1560
1561 debug3("%s entering", __func__);
1562
1563 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001564 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001565 case SSH_AUTH_FAIL_PUBKEY:
1566 case SSH_AUTH_FAIL_HOSTBASED:
1567 case SSH_AUTH_FAIL_GSSAPI:
1568 case SSH_LOGIN_EXCEED_MAXTRIES:
1569 case SSH_LOGIN_ROOT_DENIED:
1570 case SSH_CONNECTION_CLOSE:
1571 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001572 audit_event(event);
1573 break;
1574 default:
1575 fatal("Audit event type %d not permitted", event);
1576 }
1577
1578 return (0);
1579}
1580
1581int
1582mm_answer_audit_command(int socket, Buffer *m)
1583{
1584 u_int len;
1585 char *cmd;
1586
1587 debug3("%s entering", __func__);
1588 cmd = buffer_get_string(m, &len);
1589 /* sanity check command, if so how? */
1590 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001591 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001592 return (0);
1593}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001594#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001595
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001596void
markus@openbsd.org1e0cdf82017-05-31 08:09:45 +00001597monitor_clear_keystate(struct monitor *pmonitor)
1598{
1599 struct ssh *ssh = active_state; /* XXX */
1600
1601 ssh_clear_newkeys(ssh, MODE_IN);
1602 ssh_clear_newkeys(ssh, MODE_OUT);
1603 sshbuf_free(child_state);
1604 child_state = NULL;
1605}
1606
1607void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001608monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001609{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001610 struct ssh *ssh = active_state; /* XXX */
1611 struct kex *kex;
1612 int r;
1613
1614 debug3("%s: packet_set_state", __func__);
1615 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1616 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1617 sshbuf_free(child_state);
1618 child_state = NULL;
1619
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +00001620 if ((kex = ssh->kex) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001621 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001622#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001623 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1624 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +00001625 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1626 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1627 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001628 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1629 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001630# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001631 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001632# endif
Damien Miller773dda22015-01-30 23:10:17 +11001633#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001634 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1635 kex->load_host_public_key=&get_hostkey_public_by_type;
1636 kex->load_host_private_key=&get_hostkey_private_by_type;
1637 kex->host_key_index=&get_hostkey_index;
1638 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001639 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001640}
1641
1642/* This function requries careful sanity checking */
1643
1644void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001645mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001646{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001647 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001648
markus@openbsd.org091c3022015-01-19 19:52:16 +00001649 if ((child_state = sshbuf_new()) == NULL)
1650 fatal("%s: sshbuf_new failed", __func__);
1651 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1652 child_state);
1653 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001654}
1655
1656
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001657/* XXX */
1658
1659#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001660 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001661 fatal("fcntl(%d, F_SETFD)", x); \
1662} while (0)
1663
1664static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001665monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001666{
Damien Miller8f0bf232011-06-20 14:42:23 +10001667 int pair[2];
markus@openbsd.org84008602017-05-31 10:04:29 +00001668#ifdef SO_ZEROIZE
1669 int on = 1;
1670#endif
Damien Miller8f0bf232011-06-20 14:42:23 +10001671
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001672 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001673 fatal("%s: socketpair: %s", __func__, strerror(errno));
markus@openbsd.org84008602017-05-31 10:04:29 +00001674#ifdef SO_ZEROIZE
1675 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1676 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1677 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1678 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1679#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001680 FD_CLOSEONEXEC(pair[0]);
1681 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001682 mon->m_recvfd = pair[0];
1683 mon->m_sendfd = pair[1];
1684
1685 if (do_logfds) {
1686 if (pipe(pair) == -1)
1687 fatal("%s: pipe: %s", __func__, strerror(errno));
1688 FD_CLOSEONEXEC(pair[0]);
1689 FD_CLOSEONEXEC(pair[1]);
1690 mon->m_log_recvfd = pair[0];
1691 mon->m_log_sendfd = pair[1];
1692 } else
1693 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001694}
1695
1696#define MM_MEMSIZE 65536
1697
1698struct monitor *
1699monitor_init(void)
1700{
1701 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001702
Damien Miller07d86be2006-03-26 14:19:21 +11001703 mon = xcalloc(1, sizeof(*mon));
Damien Miller8f0bf232011-06-20 14:42:23 +10001704 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001705
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001706 return mon;
1707}
1708
1709void
1710monitor_reinit(struct monitor *mon)
1711{
Damien Miller8f0bf232011-06-20 14:42:23 +10001712 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001713}
Darren Tucker0efd1552003-08-26 11:49:55 +10001714
1715#ifdef GSSAPI
1716int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001717mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001718{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001719 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001720 OM_uint32 major;
1721 u_int len;
1722
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001723 if (!options.gss_authentication)
1724 fatal("%s: GSSAPI authentication not enabled", __func__);
1725
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001726 goid.elements = buffer_get_string(m, &len);
1727 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001728
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001729 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001730
Darren Tuckera627d422013-06-02 07:31:17 +10001731 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001732
1733 buffer_clear(m);
1734 buffer_put_int(m, major);
1735
Damien Miller6fd6def2005-11-05 15:07:05 +11001736 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001737
1738 /* Now we have a context, enable the step */
1739 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1740
1741 return (0);
1742}
1743
1744int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001745mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001746{
1747 gss_buffer_desc in;
1748 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001749 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001750 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001751 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001752
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001753 if (!options.gss_authentication)
1754 fatal("%s: GSSAPI authentication not enabled", __func__);
1755
Darren Tucker600ad8d2003-08-26 12:10:48 +10001756 in.value = buffer_get_string(m, &len);
1757 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001758 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001759 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001760
1761 buffer_clear(m);
1762 buffer_put_int(m, major);
1763 buffer_put_string(m, out.value, out.length);
1764 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001765 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001766
1767 gss_release_buffer(&minor, &out);
1768
Damien Miller6fd6def2005-11-05 15:07:05 +11001769 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001770 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1771 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001772 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001773 }
1774 return (0);
1775}
1776
1777int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001778mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001779{
1780 gss_buffer_desc gssbuf, mic;
1781 OM_uint32 ret;
1782 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001783
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001784 if (!options.gss_authentication)
1785 fatal("%s: GSSAPI authentication not enabled", __func__);
1786
Damien Miller0425d402003-11-17 22:18:21 +11001787 gssbuf.value = buffer_get_string(m, &len);
1788 gssbuf.length = len;
1789 mic.value = buffer_get_string(m, &len);
1790 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001791
Damien Miller0425d402003-11-17 22:18:21 +11001792 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001793
Darren Tuckera627d422013-06-02 07:31:17 +10001794 free(gssbuf.value);
1795 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001796
Damien Miller0425d402003-11-17 22:18:21 +11001797 buffer_clear(m);
1798 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001799
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001800 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001801
Damien Miller0425d402003-11-17 22:18:21 +11001802 if (!GSS_ERROR(ret))
1803 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001804
Damien Miller0425d402003-11-17 22:18:21 +11001805 return (0);
1806}
1807
1808int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001809mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001810{
1811 int authenticated;
djm@openbsd.org8f574952017-06-24 06:34:38 +00001812 const char *displayname;
Darren Tucker0efd1552003-08-26 11:49:55 +10001813
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001814 if (!options.gss_authentication)
1815 fatal("%s: GSSAPI authentication not enabled", __func__);
1816
Darren Tucker0efd1552003-08-26 11:49:55 +10001817 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1818
1819 buffer_clear(m);
1820 buffer_put_int(m, authenticated);
1821
1822 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001823 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001824
Damien Miller6fd6def2005-11-05 15:07:05 +11001825 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001826
djm@openbsd.org8f574952017-06-24 06:34:38 +00001827 if ((displayname = ssh_gssapi_displayname()) != NULL)
1828 auth2_record_info(authctxt, "%s", displayname);
1829
Darren Tucker0efd1552003-08-26 11:49:55 +10001830 /* Monitor loop will terminate if authenticated */
1831 return (authenticated);
1832}
1833#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001834