blob: bdb4e8552424b07eb8aa3f3682e1b0edcc50ebe1 [file] [log] [blame]
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001/* $OpenBSD: monitor.c,v 1.173 2017/09/12 06:32:07 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"
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
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;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000311 auth2_authctxt_reset_info(authctxt);
312
Darren Tuckerfbba7352006-11-07 23:16:08 +1100313 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100314
315 /* Special handling for multiple required authentications */
316 if (options.num_auth_methods != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100317 if (authenticated &&
318 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000319 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100320 debug3("%s: method %s: partial", __func__,
321 auth_method);
322 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100323 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100324 }
325 }
326
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000327 if (authenticated) {
328 if (!(ent->flags & MON_AUTHDECIDE))
329 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000330 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000331 if (authctxt->pw->pw_uid == 0 &&
332 !auth_root_allowed(auth_method))
333 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000334#ifdef USE_PAM
335 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100336 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000337 Buffer m;
338
339 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100340 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000341 MONITOR_REQ_PAM_ACCOUNT, &m);
342 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
343 buffer_free(&m);
344 }
345#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000346 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100347 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100348 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000349 auth_method, auth_submethod);
djm@openbsd.org94885382015-06-22 23:42:16 +0000350 if (!partial && !authenticated)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000351 authctxt->failures++;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000352 if (authenticated || partial) {
353 auth2_update_session_info(authctxt,
354 auth_method, auth_submethod);
355 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356 }
357 }
358
359 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000360 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100361 if (strcmp(auth_method, "unknown") == 0)
362 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000363
364 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000365 __func__, authctxt->user);
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000366 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000367
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000368 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000369
Damien Miller6a1937e2012-12-12 10:44:38 +1100370 /* Drain any buffered messages from the child */
371 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
372 ;
373
Damien Miller8f0bf232011-06-20 14:42:23 +1000374 close(pmonitor->m_sendfd);
375 close(pmonitor->m_log_recvfd);
376 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000377}
378
Damien Millerbe64d432003-05-14 19:31:12 +1000379static void
380monitor_set_child_handler(pid_t pid)
381{
382 monitor_child_pid = pid;
383}
384
385static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000386monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000387{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000388 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000389}
390
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000391void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000392monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000393{
Damien Miller8f0bf232011-06-20 14:42:23 +1000394 close(pmonitor->m_recvfd);
395 pmonitor->m_recvfd = -1;
396
Damien Millerbe64d432003-05-14 19:31:12 +1000397 monitor_set_child_handler(pmonitor->m_pid);
398 signal(SIGHUP, &monitor_child_handler);
399 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000400 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000401#ifdef SIGXFSZ
402 signal(SIGXFSZ, SIG_IGN);
403#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000404
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000405 mon_dispatch = mon_dispatch_postauth20;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000406
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000407 /* Permit requests for moduli and signatures */
408 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
409 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
410 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
411
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000412 if (!no_pty_flag) {
413 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
414 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
415 }
416
417 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000418 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000419}
420
Damien Miller8f0bf232011-06-20 14:42:23 +1000421static int
422monitor_read_log(struct monitor *pmonitor)
423{
424 Buffer logmsg;
425 u_int len, level;
426 char *msg;
427
428 buffer_init(&logmsg);
429
430 /* Read length */
431 buffer_append_space(&logmsg, 4);
432 if (atomicio(read, pmonitor->m_log_recvfd,
433 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
434 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100435 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000436 debug("%s: child log fd closed", __func__);
437 close(pmonitor->m_log_recvfd);
438 pmonitor->m_log_recvfd = -1;
439 return -1;
440 }
441 fatal("%s: log fd read: %s", __func__, strerror(errno));
442 }
443 len = buffer_get_int(&logmsg);
444 if (len <= 4 || len > 8192)
445 fatal("%s: invalid log message length %u", __func__, len);
446
447 /* Read severity, message */
448 buffer_clear(&logmsg);
449 buffer_append_space(&logmsg, len);
450 if (atomicio(read, pmonitor->m_log_recvfd,
451 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
452 fatal("%s: log fd read: %s", __func__, strerror(errno));
453
454 /* Log it */
455 level = buffer_get_int(&logmsg);
456 msg = buffer_get_string(&logmsg, NULL);
457 if (log_level_name(level) == NULL)
458 fatal("%s: invalid log level %u (corrupted message?)",
459 __func__, level);
460 do_log2(level, "%s [preauth]", msg);
461
462 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000463 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000464
465 return 0;
466}
467
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000468int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000469monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000470 struct mon_table **pent)
471{
472 Buffer m;
473 int ret;
474 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000475 struct pollfd pfd[2];
476
477 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100478 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000479 pfd[0].fd = pmonitor->m_sendfd;
480 pfd[0].events = POLLIN;
481 pfd[1].fd = pmonitor->m_log_recvfd;
482 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000483 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
484 if (errno == EINTR || errno == EAGAIN)
485 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000486 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000487 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000488 if (pfd[1].revents) {
489 /*
490 * Drain all log messages before processing next
491 * monitor request.
492 */
493 monitor_read_log(pmonitor);
494 continue;
495 }
496 if (pfd[0].revents)
497 break; /* Continues below */
498 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000499
500 buffer_init(&m);
501
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000502 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000503 type = buffer_get_char(&m);
504
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000505 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000506
507 while (ent->f != NULL) {
508 if (ent->type == type)
509 break;
510 ent++;
511 }
512
513 if (ent->f != NULL) {
514 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000515 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000516 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000517 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000518 buffer_free(&m);
519
520 /* The child may use this request only once, disable it */
521 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000522 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523 type);
524 ent->flags &= ~MON_PERMIT;
525 }
526
527 if (pent != NULL)
528 *pent = ent;
529
530 return ret;
531 }
532
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000533 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000534
535 /* NOTREACHED */
536 return (-1);
537}
538
539/* allowed key state */
540static int
541monitor_allowed_key(u_char *blob, u_int bloblen)
542{
543 /* make sure key is allowed */
544 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000545 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000546 return (0);
547 return (1);
548}
549
550static void
551monitor_reset_key_state(void)
552{
553 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000554 free(key_blob);
555 free(hostbased_cuser);
556 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000557 key_blob = NULL;
558 key_bloblen = 0;
559 key_blobtype = MM_NOKEY;
560 hostbased_cuser = NULL;
561 hostbased_chost = NULL;
562}
563
Damien Miller1f0311c2014-05-15 14:24:09 +1000564#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000565int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000566mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000567{
568 DH *dh;
569 int min, want, max;
570
571 min = buffer_get_int(m);
572 want = buffer_get_int(m);
573 max = buffer_get_int(m);
574
575 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000576 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577 /* We need to check here, too, in case the child got corrupted */
578 if (max < min || want < min || max < want)
579 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000580 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000581
582 buffer_clear(m);
583
584 dh = choose_dh(min, want, max);
585 if (dh == NULL) {
586 buffer_put_char(m, 0);
587 return (0);
588 } else {
589 /* Send first bignum */
590 buffer_put_char(m, 1);
591 buffer_put_bignum2(m, dh->p);
592 buffer_put_bignum2(m, dh->g);
593
594 DH_free(dh);
595 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000596 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597 return (0);
598}
Damien Miller1f0311c2014-05-15 14:24:09 +1000599#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000600
601int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000602mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000603{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000604 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000605 extern int auth_sock; /* XXX move to state struct? */
606 struct sshkey *key;
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000607 struct sshbuf *sigbuf = NULL;
608 u_char *p = NULL, *signature = NULL;
609 char *alg = NULL;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000610 size_t datlen, siglen, alglen;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000611 int r, is_proof = 0;
612 u_int keyid;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000613 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000614
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000615 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000616
djm@openbsd.org141efe42015-01-14 20:05:27 +0000617 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000618 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
619 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000620 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000621 if (keyid > INT_MAX)
622 fatal("%s: invalid key ID", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000623
Damien Millera63128d2006-03-15 12:08:28 +1100624 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000625 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
626 * SHA384 (48 bytes) and SHA512 (64 bytes).
djm@openbsd.org523463a2015-02-16 22:13:32 +0000627 *
628 * Otherwise, verify the signature request is for a hostkey
629 * proof.
630 *
631 * XXX perform similar check for KEX signature requests too?
632 * it's not trivial, since what is signed is the hash, rather
633 * than the full kex structure...
Damien Millera63128d2006-03-15 12:08:28 +1100634 */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000635 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
636 /*
637 * Construct expected hostkey proof and compare it to what
638 * the client sent us.
639 */
640 if (session_id2_len == 0) /* hostkeys is never first */
641 fatal("%s: bad data length: %zu", __func__, datlen);
642 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
643 fatal("%s: no hostkey for index %d", __func__, keyid);
644 if ((sigbuf = sshbuf_new()) == NULL)
645 fatal("%s: sshbuf_new", __func__);
djm@openbsd.org44732de2015-02-20 22:17:21 +0000646 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
647 (r = sshbuf_put_string(sigbuf, session_id2,
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000648 session_id2_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000649 (r = sshkey_puts(key, sigbuf)) != 0)
650 fatal("%s: couldn't prepare private key "
651 "proof buffer: %s", __func__, ssh_err(r));
652 if (datlen != sshbuf_len(sigbuf) ||
653 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
654 fatal("%s: bad data length: %zu, hostkey proof len %zu",
655 __func__, datlen, sshbuf_len(sigbuf));
656 sshbuf_free(sigbuf);
657 is_proof = 1;
658 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000659
Ben Lindstromf67e0772002-06-06 20:58:19 +0000660 /* save session id, it will be passed on the first call */
661 if (session_id2_len == 0) {
662 session_id2_len = datlen;
663 session_id2 = xmalloc(session_id2_len);
664 memcpy(session_id2, p, session_id2_len);
665 }
666
Damien Miller85b45e02013-07-20 13:21:52 +1000667 if ((key = get_hostkey_by_index(keyid)) != NULL) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000668 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000669 datafellows)) != 0)
670 fatal("%s: sshkey_sign failed: %s",
671 __func__, ssh_err(r));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000672 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000673 auth_sock > 0) {
674 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000675 p, datlen, alg, datafellows)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000676 fatal("%s: ssh_agent_sign failed: %s",
677 __func__, ssh_err(r));
678 }
Damien Miller85b45e02013-07-20 13:21:52 +1000679 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000680 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000681
djm@openbsd.org523463a2015-02-16 22:13:32 +0000682 debug3("%s: %s signature %p(%zu)", __func__,
683 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000684
djm@openbsd.org141efe42015-01-14 20:05:27 +0000685 sshbuf_reset(m);
686 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
687 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000688
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000689 free(alg);
Darren Tuckera627d422013-06-02 07:31:17 +1000690 free(p);
691 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000692
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000693 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000694
695 /* Turn on permissions for getpwnam */
696 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
697
698 return (0);
699}
700
701/* Retrieves the password entry and also checks if the user is permitted */
702
703int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000704mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000705{
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000706 struct ssh *ssh = active_state; /* XXX */
Darren Tuckerb09b6772004-06-22 15:06:46 +1000707 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000708 struct passwd *pwent;
709 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000710 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000711
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000712 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000713
714 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000715 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000716
Darren Tuckerb09b6772004-06-22 15:06:46 +1000717 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000718
Darren Tuckerb09b6772004-06-22 15:06:46 +1000719 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000720
Darren Tuckerb09b6772004-06-22 15:06:46 +1000721 authctxt->user = xstrdup(username);
722 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000723 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000724
725 buffer_clear(m);
726
727 if (pwent == NULL) {
728 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100729 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000730 goto out;
731 }
732
733 allowed = 1;
734 authctxt->pw = pwent;
735 authctxt->valid = 1;
736
737 buffer_put_char(m, 1);
738 buffer_put_string(m, pwent, sizeof(struct passwd));
739 buffer_put_cstring(m, pwent->pw_name);
740 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000741#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000742 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000743#endif
744#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000745 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000746#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000747 buffer_put_cstring(m, pwent->pw_dir);
748 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100749
750 out:
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000751 ssh_packet_set_log_preamble(ssh, "%suser %s",
752 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
Darren Tucker1629c072007-02-19 22:25:37 +1100753 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000754
755#define M_CP_STROPT(x) do { \
756 if (options.x != NULL) \
757 buffer_put_cstring(m, options.x); \
758 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000759#define M_CP_STRARRAYOPT(x, nx) do { \
760 for (i = 0; i < options.nx; i++) \
761 buffer_put_cstring(m, options.x[i]); \
762 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000763 /* See comment in servconf.h */
764 COPY_MATCH_STRING_OPTS();
765#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000766#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100767
768 /* Create valid auth method lists */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000769 if (auth2_setup_methods_lists(authctxt) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100770 /*
771 * The monitor will continue long enough to let the child
772 * run to it's packet_disconnect(), but it must not allow any
773 * authentication to succeed.
774 */
775 debug("%s: no valid authentication method lists", __func__);
776 }
777
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000778 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000779 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000780
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000781 /* Allow service/style information on the auth context */
782 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
783 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
784
Damien Millera33501b2002-05-08 12:24:42 +1000785#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000786 if (options.use_pam)
787 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000788#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000789
790 return (0);
791}
792
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000793int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000794{
795 char *banner;
796
797 buffer_clear(m);
798 banner = auth2_read_banner();
799 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000800 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000801 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000802
803 return (0);
804}
805
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000806int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000807mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000808{
809 monitor_permit_authentications(1);
810
811 authctxt->service = buffer_get_string(m, NULL);
812 authctxt->style = buffer_get_string(m, NULL);
813 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000814 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000815
816 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000817 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000818 authctxt->style = NULL;
819 }
820
821 return (0);
822}
823
824int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000825mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000826{
827 static int call_count;
828 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000829 int authenticated;
830 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000831
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000832 if (!options.password_authentication)
833 fatal("%s: password authentication not enabled", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000834 passwd = buffer_get_string(m, &plen);
835 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000836 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000837 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100838 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000839 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000840
841 buffer_clear(m);
842 buffer_put_int(m, authenticated);
Darren Tucker01558b72016-07-18 09:33:25 +1000843#ifdef USE_PAM
844 buffer_put_int(m, sshpam_get_maxtries_reached());
845#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000846
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000847 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000848 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000849
850 call_count++;
851 if (plen == 0 && call_count == 1)
852 auth_method = "none";
853 else
854 auth_method = "password";
855
856 /* Causes monitor loop to terminate if authenticated */
857 return (authenticated);
858}
859
860#ifdef BSD_AUTH
861int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000862mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000863{
864 char *name, *infotxt;
865 u_int numprompts;
866 u_int *echo_on;
867 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100868 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000869
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000870 if (!options.kbd_interactive_authentication)
871 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb7df3af2003-02-24 11:55:46 +1100872 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
873 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000874
875 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100876 buffer_put_int(m, success);
877 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000878 buffer_put_cstring(m, prompts[0]);
879
Damien Millerb7df3af2003-02-24 11:55:46 +1100880 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000881 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000882
Damien Millerb7df3af2003-02-24 11:55:46 +1100883 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000884 free(name);
885 free(infotxt);
886 free(prompts);
887 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000888 }
889
890 return (0);
891}
892
893int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000894mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000895{
896 char *response;
897 int authok;
898
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000899 if (!options.kbd_interactive_authentication)
900 fatal("%s: kbd-int authentication not enabled", __func__);
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +0000901 if (authctxt->as == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000902 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000903
904 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000905 authok = options.challenge_response_authentication &&
906 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000907 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000908 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000909 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000910
911 buffer_clear(m);
912 buffer_put_int(m, authok);
913
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000914 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000915 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000916
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000917 auth_method = "keyboard-interactive";
918 auth_submethod = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000919
920 return (authok != 0);
921}
922#endif
923
924#ifdef SKEY
925int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000926mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000927{
928 struct skey skey;
929 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100930 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000931
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000932 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
933 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000934
935 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100936 buffer_put_int(m, success);
937 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000938 buffer_put_cstring(m, challenge);
939
Damien Millerb7df3af2003-02-24 11:55:46 +1100940 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000941 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000942
943 return (0);
944}
945
946int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000947mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000948{
949 char *response;
950 int authok;
951
952 response = buffer_get_string(m, NULL);
953
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000954 authok = (options.challenge_response_authentication &&
955 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000956 skey_haskey(authctxt->pw->pw_name) == 0 &&
957 skey_passcheck(authctxt->pw->pw_name, response) != -1);
958
Darren Tuckerf60845f2013-06-02 08:07:31 +1000959 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000960
961 buffer_clear(m);
962 buffer_put_int(m, authok);
963
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000964 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000965 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000966
Damien Millerac947352015-11-20 12:35:41 +1100967 auth_method = "keyboard-interactive";
968 auth_submethod = "skey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000969
970 return (authok != 0);
971}
972#endif
973
Damien Miller79418552002-04-23 20:28:48 +1000974#ifdef USE_PAM
975int
Darren Tucker7eda5922004-06-22 13:22:50 +1000976mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000977{
Damien Miller4e448a32003-05-14 15:11:48 +1000978 if (!options.use_pam)
979 fatal("UsePAM not set, but ended up in %s anyway", __func__);
980
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100981 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000982
Damien Miller1f499fd2003-08-25 13:08:49 +1000983 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
Damien Miller775f8a22016-08-31 10:48:07 +1000984 if (options.kbd_interactive_authentication)
985 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller1f499fd2003-08-25 13:08:49 +1000986
Damien Miller79418552002-04-23 20:28:48 +1000987 return (0);
988}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000989
Damien Miller1f499fd2003-08-25 13:08:49 +1000990int
Darren Tucker7eda5922004-06-22 13:22:50 +1000991mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000992{
993 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100994
Damien Miller1f499fd2003-08-25 13:08:49 +1000995 if (!options.use_pam)
Damien Miller775f8a22016-08-31 10:48:07 +1000996 fatal("%s: PAM not enabled", __func__);
Damien Miller1f499fd2003-08-25 13:08:49 +1000997
998 ret = do_pam_account();
999
1000 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001001 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001002
Darren Tucker7eda5922004-06-22 13:22:50 +10001003 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001004
1005 return (ret);
1006}
1007
Damien Miller4f9f42a2003-05-10 19:28:02 +10001008static void *sshpam_ctxt, *sshpam_authok;
1009extern KbdintDevice sshpam_device;
1010
1011int
Darren Tucker7eda5922004-06-22 13:22:50 +10001012mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001013{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001014 debug3("%s", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001015 if (!options.kbd_interactive_authentication)
1016 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001017 if (sshpam_ctxt != NULL)
1018 fatal("%s: already called", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001019 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1020 sshpam_authok = NULL;
1021 buffer_clear(m);
1022 if (sshpam_ctxt != NULL) {
1023 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
Damien Millerb38b95f2016-08-29 11:47:07 +10001024 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001025 buffer_put_int(m, 1);
1026 } else {
1027 buffer_put_int(m, 0);
1028 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001029 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001030 return (0);
1031}
1032
1033int
Darren Tucker7eda5922004-06-22 13:22:50 +10001034mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001035{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001036 char *name = NULL, *info = NULL, **prompts = NULL;
1037 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001038 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001039
1040 debug3("%s", __func__);
1041 sshpam_authok = NULL;
Damien Millerb38b95f2016-08-29 11:47:07 +10001042 if (sshpam_ctxt == NULL)
1043 fatal("%s: no context", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001044 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1045 &num, &prompts, &echo_on);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001046 if (ret == 0 && num == 0)
1047 sshpam_authok = sshpam_ctxt;
1048 if (num > 1 || name == NULL || info == NULL)
Damien Millerb38b95f2016-08-29 11:47:07 +10001049 fatal("sshpam_device.query failed");
1050 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001051 buffer_clear(m);
1052 buffer_put_int(m, ret);
1053 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001054 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001055 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001056 free(info);
Darren Tucker01558b72016-07-18 09:33:25 +10001057 buffer_put_int(m, sshpam_get_maxtries_reached());
Damien Miller4f9f42a2003-05-10 19:28:02 +10001058 buffer_put_int(m, num);
1059 for (i = 0; i < num; ++i) {
1060 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001061 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001062 buffer_put_int(m, echo_on[i]);
1063 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001064 free(prompts);
1065 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001066 auth_method = "keyboard-interactive";
1067 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001068 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001069 return (0);
1070}
1071
1072int
Darren Tucker7eda5922004-06-22 13:22:50 +10001073mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001074{
1075 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001076 u_int i, num;
1077 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001078
1079 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001080 if (sshpam_ctxt == NULL)
1081 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001082 sshpam_authok = NULL;
1083 num = buffer_get_int(m);
1084 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001085 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001086 for (i = 0; i < num; ++i)
1087 resp[i] = buffer_get_string(m, NULL);
1088 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1089 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001090 free(resp[i]);
1091 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001092 } else {
1093 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1094 }
1095 buffer_clear(m);
1096 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001097 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001098 auth_method = "keyboard-interactive";
1099 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001100 if (ret == 0)
1101 sshpam_authok = sshpam_ctxt;
1102 return (0);
1103}
1104
1105int
Darren Tucker7eda5922004-06-22 13:22:50 +10001106mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001107{
Damien Miller5e75f512015-08-11 13:34:12 +10001108 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001109
1110 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001111 if (sshpam_ctxt == NULL)
1112 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001113 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001114 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001115 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001116 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Millerb38b95f2016-08-29 11:47:07 +10001117 /* Allow another attempt */
1118 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller15b05cf2012-12-03 09:53:20 +11001119 auth_method = "keyboard-interactive";
1120 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001121 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001122}
Damien Miller79418552002-04-23 20:28:48 +10001123#endif
1124
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001125int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001126mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001127{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001128 struct sshkey *key;
Damien Millera10f5612002-09-12 09:49:15 +10001129 char *cuser, *chost;
1130 u_char *blob;
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001131 u_int bloblen, pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001132 enum mm_keytype type = 0;
1133 int allowed = 0;
1134
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001135 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001136
1137 type = buffer_get_int(m);
1138 cuser = buffer_get_string(m, NULL);
1139 chost = buffer_get_string(m, NULL);
1140 blob = buffer_get_string(m, &bloblen);
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001141 pubkey_auth_attempt = buffer_get_int(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001142
1143 key = key_from_blob(blob, bloblen);
1144
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001145 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001146
Damien Miller3e3b5142003-11-17 21:13:40 +11001147 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001148 /* These should not make it past the privsep child */
1149 if (key_type_plain(key->type) == KEY_RSA &&
1150 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1151 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1152
Darren Tucker47eede72005-03-14 23:08:12 +11001153 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001154 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001155 allowed = options.pubkey_authentication &&
djm@openbsd.org8f574952017-06-24 06:34:38 +00001156 !auth2_key_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001157 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001158 options.pubkey_key_types, 0) == 1 &&
1159 user_key_allowed(authctxt->pw, key,
1160 pubkey_auth_attempt);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001161 auth_method = "publickey";
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001162 if (options.pubkey_authentication &&
1163 (!pubkey_auth_attempt || allowed != 1))
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001164 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001165 break;
1166 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001167 allowed = options.hostbased_authentication &&
djm@openbsd.org8f574952017-06-24 06:34:38 +00001168 !auth2_key_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001169 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001170 options.hostbased_key_types, 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001171 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001172 cuser, chost, key);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001173 auth2_record_info(authctxt,
Damien Miller20bdcd72013-07-18 16:10:09 +10001174 "client user \"%.100s\", client host \"%.100s\"",
1175 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001176 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001177 break;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001178 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001179 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001180 break;
1181 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001182 }
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001183
djm@openbsd.org8f574952017-06-24 06:34:38 +00001184 debug3("%s: key is %s", __func__, allowed ? "allowed" : "not allowed");
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001185
djm@openbsd.org8f574952017-06-24 06:34:38 +00001186 auth2_record_key(authctxt, 0, key);
1187 sshkey_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001188
1189 /* clear temporarily storage (used by verify) */
1190 monitor_reset_key_state();
1191
1192 if (allowed) {
1193 /* Save temporarily for comparison in verify */
1194 key_blob = blob;
1195 key_bloblen = bloblen;
1196 key_blobtype = type;
1197 hostbased_cuser = cuser;
1198 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001199 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001200 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001201 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001202 free(blob);
1203 free(cuser);
1204 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001205 }
1206
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001207 buffer_clear(m);
1208 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001209 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001210
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001211 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001212
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001213 return (0);
1214}
1215
1216static int
1217monitor_valid_userblob(u_char *data, u_int datalen)
1218{
1219 Buffer b;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001220 u_char *p;
1221 char *userstyle, *cp;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001222 u_int len;
1223 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001224
1225 buffer_init(&b);
1226 buffer_append(&b, data, datalen);
1227
1228 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001229 p = buffer_ptr(&b);
1230 len = buffer_len(&b);
1231 if ((session_id2 == NULL) ||
1232 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001233 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001234 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001235 buffer_consume(&b, session_id2_len);
1236 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001237 p = buffer_get_string(&b, &len);
1238 if ((session_id2 == NULL) ||
1239 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001240 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001241 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001242 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001243 }
1244 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1245 fail++;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001246 cp = buffer_get_cstring(&b, NULL);
Damien Miller4ce189d2013-04-23 15:17:52 +10001247 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1248 authctxt->style ? ":" : "",
1249 authctxt->style ? authctxt->style : "");
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001250 if (strcmp(userstyle, cp) != 0) {
1251 logit("wrong user name passed to monitor: "
1252 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001253 fail++;
1254 }
Darren Tuckera627d422013-06-02 07:31:17 +10001255 free(userstyle);
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001256 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001257 buffer_skip_string(&b);
1258 if (datafellows & SSH_BUG_PKAUTH) {
1259 if (!buffer_get_char(&b))
1260 fail++;
1261 } else {
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001262 cp = buffer_get_cstring(&b, NULL);
1263 if (strcmp("publickey", cp) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001264 fail++;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001265 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001266 if (!buffer_get_char(&b))
1267 fail++;
1268 buffer_skip_string(&b);
1269 }
1270 buffer_skip_string(&b);
1271 if (buffer_len(&b) != 0)
1272 fail++;
1273 buffer_free(&b);
1274 return (fail == 0);
1275}
1276
1277static int
Damien Millera10f5612002-09-12 09:49:15 +10001278monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1279 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001280{
1281 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001282 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001283 u_int len;
1284 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001285
1286 buffer_init(&b);
1287 buffer_append(&b, data, datalen);
1288
Ben Lindstromf67e0772002-06-06 20:58:19 +00001289 p = buffer_get_string(&b, &len);
1290 if ((session_id2 == NULL) ||
1291 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001292 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001294 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001295
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001296 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1297 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001298 p = buffer_get_cstring(&b, NULL);
1299 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1300 authctxt->style ? ":" : "",
1301 authctxt->style ? authctxt->style : "");
1302 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001303 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001304 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001305 fail++;
1306 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001307 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001308 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001309 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001310 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001311 if (strcmp(p, "hostbased") != 0)
1312 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001313 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001314 buffer_skip_string(&b); /* pkalg */
1315 buffer_skip_string(&b); /* pkblob */
1316
1317 /* verify client host, strip trailing dot if necessary */
1318 p = buffer_get_string(&b, NULL);
1319 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1320 p[len - 1] = '\0';
1321 if (strcmp(p, chost) != 0)
1322 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001323 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001324
1325 /* verify client user */
1326 p = buffer_get_string(&b, NULL);
1327 if (strcmp(p, cuser) != 0)
1328 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001329 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001330
1331 if (buffer_len(&b) != 0)
1332 fail++;
1333 buffer_free(&b);
1334 return (fail == 0);
1335}
1336
1337int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001338mm_answer_keyverify(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001339{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001340 struct sshkey *key;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001341 u_char *signature, *data, *blob;
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 ||
1347 (r = sshbuf_get_string(m, &data, &datalen)) != 0)
1348 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001349
1350 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001351 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001352 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001353
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001354 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1355 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1356 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001357
1358 switch (key_blobtype) {
1359 case MM_USERKEY:
1360 valid_data = monitor_valid_userblob(data, datalen);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001361 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001362 break;
1363 case MM_HOSTKEY:
1364 valid_data = monitor_valid_hostbasedblob(data, datalen,
1365 hostbased_cuser, hostbased_chost);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001366 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001367 break;
1368 default:
1369 valid_data = 0;
1370 break;
1371 }
1372 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001373 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001374
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001375 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
1376 active_state->compat);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001377 debug3("%s: %s %p signature %s", __func__, auth_method, key,
1378 (ret == 0) ? "verified" : "unverified");
1379 auth2_record_key(authctxt, ret == 0, key);
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001380
Darren Tuckera627d422013-06-02 07:31:17 +10001381 free(blob);
1382 free(signature);
1383 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001384
1385 monitor_reset_key_state();
1386
djm@openbsd.org8f574952017-06-24 06:34:38 +00001387 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001388 sshbuf_reset(m);
1389
1390 /* encode ret != 0 as positive integer, since we're sending u32 */
1391 encoded_ret = (ret != 0);
1392 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1393 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001394 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001395
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001396 return ret == 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001397}
1398
1399static void
1400mm_record_login(Session *s, struct passwd *pw)
1401{
djm@openbsd.org95767262016-03-07 19:02:43 +00001402 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001403 socklen_t fromlen;
1404 struct sockaddr_storage from;
1405
1406 /*
1407 * Get IP address of client. If the connection is not a socket, let
1408 * the address be 0.0.0.0.
1409 */
1410 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001411 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001412 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001413 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001414 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001415 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001416 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001417 }
1418 }
1419 /* Record that there was a login on that tty from the remote host. */
1420 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +00001421 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001422 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001423}
1424
1425static void
1426mm_session_close(Session *s)
1427{
Damien Miller04bd8b02003-05-25 14:38:33 +10001428 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001429 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001430 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001431 session_pty_cleanup2(s);
1432 }
Damien Miller7207f642008-05-19 15:34:50 +10001433 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434}
1435
1436int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001437mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001438{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001439 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001440 Session *s;
1441 int res, fd0;
1442
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001443 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001444
1445 buffer_clear(m);
1446 s = session_new();
1447 if (s == NULL)
1448 goto error;
1449 s->authctxt = authctxt;
1450 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001451 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001452 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1453 if (res == 0)
1454 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001455 pty_setowner(authctxt->pw, s->tty);
1456
1457 buffer_put_int(m, 1);
1458 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001459
1460 /* We need to trick ttyslot */
1461 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001462 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001463
1464 mm_record_login(s, authctxt->pw);
1465
1466 /* Now we can close the file descriptor again */
1467 close(0);
1468
Darren Tucker09991742004-07-17 17:05:14 +10001469 /* send messages generated by record_login */
1470 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1471 buffer_clear(&loginmsg);
1472
1473 mm_request_send(sock, MONITOR_ANS_PTY, m);
1474
Damien Miller54fd7cf2007-09-17 12:04:08 +10001475 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1476 mm_send_fd(sock, s->ttyfd) == -1)
1477 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001478
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001479 /* make sure nothing uses fd 0 */
1480 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001481 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001482 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001483 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001484
1485 /* slave is not needed */
1486 close(s->ttyfd);
1487 s->ttyfd = s->ptyfd;
1488 /* no need to dup() because nobody closes ptyfd */
1489 s->ptymaster = s->ptyfd;
1490
Damien Miller9ab00b42006-08-05 12:40:11 +10001491 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492
1493 return (0);
1494
1495 error:
1496 if (s != NULL)
1497 mm_session_close(s);
1498 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001499 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001500 return (0);
1501}
1502
1503int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001504mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001505{
1506 Session *s;
1507 char *tty;
1508
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001509 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001510
1511 tty = buffer_get_string(m, NULL);
1512 if ((s = session_by_tty(tty)) != NULL)
1513 mm_session_close(s);
1514 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001515 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001516 return (0);
1517}
1518
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001519int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001520mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001521{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001522 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001523 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001524 int res, status;
1525
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001526 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001527
1528 /* The child is terminating */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001529 session_destroy_all(ssh, &mm_session_close);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001530
Darren Tucker52358d62008-03-11 22:58:25 +11001531#ifdef USE_PAM
1532 if (options.use_pam)
1533 sshpam_cleanup();
1534#endif
1535
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001536 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001537 if (errno != EINTR)
1538 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001539
1540 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1541
1542 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001543 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001544}
1545
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001546#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001547/* Report that an audit event occurred */
1548int
1549mm_answer_audit_event(int socket, Buffer *m)
1550{
1551 ssh_audit_event_t event;
1552
1553 debug3("%s entering", __func__);
1554
1555 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001556 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001557 case SSH_AUTH_FAIL_PUBKEY:
1558 case SSH_AUTH_FAIL_HOSTBASED:
1559 case SSH_AUTH_FAIL_GSSAPI:
1560 case SSH_LOGIN_EXCEED_MAXTRIES:
1561 case SSH_LOGIN_ROOT_DENIED:
1562 case SSH_CONNECTION_CLOSE:
1563 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001564 audit_event(event);
1565 break;
1566 default:
1567 fatal("Audit event type %d not permitted", event);
1568 }
1569
1570 return (0);
1571}
1572
1573int
1574mm_answer_audit_command(int socket, Buffer *m)
1575{
1576 u_int len;
1577 char *cmd;
1578
1579 debug3("%s entering", __func__);
1580 cmd = buffer_get_string(m, &len);
1581 /* sanity check command, if so how? */
1582 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001583 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001584 return (0);
1585}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001586#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001587
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001588void
markus@openbsd.org1e0cdf82017-05-31 08:09:45 +00001589monitor_clear_keystate(struct monitor *pmonitor)
1590{
1591 struct ssh *ssh = active_state; /* XXX */
1592
1593 ssh_clear_newkeys(ssh, MODE_IN);
1594 ssh_clear_newkeys(ssh, MODE_OUT);
1595 sshbuf_free(child_state);
1596 child_state = NULL;
1597}
1598
1599void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001600monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001601{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001602 struct ssh *ssh = active_state; /* XXX */
1603 struct kex *kex;
1604 int r;
1605
1606 debug3("%s: packet_set_state", __func__);
1607 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1608 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1609 sshbuf_free(child_state);
1610 child_state = NULL;
1611
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +00001612 if ((kex = ssh->kex) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001613 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001614#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001615 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1616 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +00001617 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1618 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1619 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001620 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1621 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001622# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001623 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001624# endif
Damien Miller773dda22015-01-30 23:10:17 +11001625#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001626 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1627 kex->load_host_public_key=&get_hostkey_public_by_type;
1628 kex->load_host_private_key=&get_hostkey_private_by_type;
1629 kex->host_key_index=&get_hostkey_index;
1630 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001631 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001632}
1633
1634/* This function requries careful sanity checking */
1635
1636void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001637mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001638{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001639 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001640
markus@openbsd.org091c3022015-01-19 19:52:16 +00001641 if ((child_state = sshbuf_new()) == NULL)
1642 fatal("%s: sshbuf_new failed", __func__);
1643 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1644 child_state);
1645 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001646}
1647
1648
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001649/* XXX */
1650
1651#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001652 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001653 fatal("fcntl(%d, F_SETFD)", x); \
1654} while (0)
1655
1656static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001657monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001658{
Damien Miller8f0bf232011-06-20 14:42:23 +10001659 int pair[2];
markus@openbsd.org84008602017-05-31 10:04:29 +00001660#ifdef SO_ZEROIZE
1661 int on = 1;
1662#endif
Damien Miller8f0bf232011-06-20 14:42:23 +10001663
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001664 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001665 fatal("%s: socketpair: %s", __func__, strerror(errno));
markus@openbsd.org84008602017-05-31 10:04:29 +00001666#ifdef SO_ZEROIZE
1667 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1668 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1669 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1670 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1671#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001672 FD_CLOSEONEXEC(pair[0]);
1673 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001674 mon->m_recvfd = pair[0];
1675 mon->m_sendfd = pair[1];
1676
1677 if (do_logfds) {
1678 if (pipe(pair) == -1)
1679 fatal("%s: pipe: %s", __func__, strerror(errno));
1680 FD_CLOSEONEXEC(pair[0]);
1681 FD_CLOSEONEXEC(pair[1]);
1682 mon->m_log_recvfd = pair[0];
1683 mon->m_log_sendfd = pair[1];
1684 } else
1685 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001686}
1687
1688#define MM_MEMSIZE 65536
1689
1690struct monitor *
1691monitor_init(void)
1692{
1693 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001694
Damien Miller07d86be2006-03-26 14:19:21 +11001695 mon = xcalloc(1, sizeof(*mon));
Damien Miller8f0bf232011-06-20 14:42:23 +10001696 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001697
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001698 return mon;
1699}
1700
1701void
1702monitor_reinit(struct monitor *mon)
1703{
Damien Miller8f0bf232011-06-20 14:42:23 +10001704 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001705}
Darren Tucker0efd1552003-08-26 11:49:55 +10001706
1707#ifdef GSSAPI
1708int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001709mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001710{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001711 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001712 OM_uint32 major;
1713 u_int len;
1714
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001715 if (!options.gss_authentication)
1716 fatal("%s: GSSAPI authentication not enabled", __func__);
1717
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001718 goid.elements = buffer_get_string(m, &len);
1719 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001720
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001721 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001722
Darren Tuckera627d422013-06-02 07:31:17 +10001723 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001724
1725 buffer_clear(m);
1726 buffer_put_int(m, major);
1727
Damien Miller6fd6def2005-11-05 15:07:05 +11001728 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001729
1730 /* Now we have a context, enable the step */
1731 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1732
1733 return (0);
1734}
1735
1736int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001737mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001738{
1739 gss_buffer_desc in;
1740 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001741 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001742 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001743 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001744
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001745 if (!options.gss_authentication)
1746 fatal("%s: GSSAPI authentication not enabled", __func__);
1747
Darren Tucker600ad8d2003-08-26 12:10:48 +10001748 in.value = buffer_get_string(m, &len);
1749 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001750 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001751 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001752
1753 buffer_clear(m);
1754 buffer_put_int(m, major);
1755 buffer_put_string(m, out.value, out.length);
1756 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001757 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001758
1759 gss_release_buffer(&minor, &out);
1760
Damien Miller6fd6def2005-11-05 15:07:05 +11001761 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001762 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1763 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001764 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001765 }
1766 return (0);
1767}
1768
1769int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001770mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001771{
1772 gss_buffer_desc gssbuf, mic;
1773 OM_uint32 ret;
1774 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001775
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001776 if (!options.gss_authentication)
1777 fatal("%s: GSSAPI authentication not enabled", __func__);
1778
Damien Miller0425d402003-11-17 22:18:21 +11001779 gssbuf.value = buffer_get_string(m, &len);
1780 gssbuf.length = len;
1781 mic.value = buffer_get_string(m, &len);
1782 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001783
Damien Miller0425d402003-11-17 22:18:21 +11001784 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001785
Darren Tuckera627d422013-06-02 07:31:17 +10001786 free(gssbuf.value);
1787 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001788
Damien Miller0425d402003-11-17 22:18:21 +11001789 buffer_clear(m);
1790 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001791
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001792 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001793
Damien Miller0425d402003-11-17 22:18:21 +11001794 if (!GSS_ERROR(ret))
1795 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001796
Damien Miller0425d402003-11-17 22:18:21 +11001797 return (0);
1798}
1799
1800int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001801mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001802{
1803 int authenticated;
djm@openbsd.org8f574952017-06-24 06:34:38 +00001804 const char *displayname;
Darren Tucker0efd1552003-08-26 11:49:55 +10001805
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001806 if (!options.gss_authentication)
1807 fatal("%s: GSSAPI authentication not enabled", __func__);
1808
Darren Tucker0efd1552003-08-26 11:49:55 +10001809 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1810
1811 buffer_clear(m);
1812 buffer_put_int(m, authenticated);
1813
1814 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001815 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001816
Damien Miller6fd6def2005-11-05 15:07:05 +11001817 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001818
djm@openbsd.org8f574952017-06-24 06:34:38 +00001819 if ((displayname = ssh_gssapi_displayname()) != NULL)
1820 auth2_record_info(authctxt, "%s", displayname);
1821
Darren Tucker0efd1552003-08-26 11:49:55 +10001822 /* Monitor loop will terminate if authenticated */
1823 return (authenticated);
1824}
1825#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001826