blob: f517da482ce501f8d4af7ac219e59e553336d90b [file] [log] [blame]
djm@openbsd.org66bf74a2017-10-02 19:33:20 +00001/* $OpenBSD: monitor.c,v 1.174 2017/10/02 19:33:20 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)
djm@openbsd.org66bf74a2017-10-02 19:33:20 +0000763#define M_CP_STRARRAYOPT_ALLOC(x, nx) M_CP_STRARRAYOPT(x, nx)
Damien Millerf2e407e2011-05-20 19:04:14 +1000764 /* See comment in servconf.h */
765 COPY_MATCH_STRING_OPTS();
766#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000767#undef M_CP_STRARRAYOPT
djm@openbsd.org66bf74a2017-10-02 19:33:20 +0000768#undef M_CP_STRARRAYOPT_ALLOC
Damien Millera6e3f012012-11-04 23:21:40 +1100769
770 /* Create valid auth method lists */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000771 if (auth2_setup_methods_lists(authctxt) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100772 /*
773 * The monitor will continue long enough to let the child
774 * run to it's packet_disconnect(), but it must not allow any
775 * authentication to succeed.
776 */
777 debug("%s: no valid authentication method lists", __func__);
778 }
779
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000780 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000781 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000782
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000783 /* Allow service/style information on the auth context */
784 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
785 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
786
Damien Millera33501b2002-05-08 12:24:42 +1000787#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000788 if (options.use_pam)
789 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000790#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000791
792 return (0);
793}
794
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000795int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000796{
797 char *banner;
798
799 buffer_clear(m);
800 banner = auth2_read_banner();
801 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000802 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000803 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000804
805 return (0);
806}
807
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000808int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000809mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000810{
811 monitor_permit_authentications(1);
812
813 authctxt->service = buffer_get_string(m, NULL);
814 authctxt->style = buffer_get_string(m, NULL);
815 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000816 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000817
818 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000819 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000820 authctxt->style = NULL;
821 }
822
823 return (0);
824}
825
826int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000827mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000828{
829 static int call_count;
830 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000831 int authenticated;
832 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000833
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000834 if (!options.password_authentication)
835 fatal("%s: password authentication not enabled", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000836 passwd = buffer_get_string(m, &plen);
837 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000838 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000839 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100840 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000841 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000842
843 buffer_clear(m);
844 buffer_put_int(m, authenticated);
Darren Tucker01558b72016-07-18 09:33:25 +1000845#ifdef USE_PAM
846 buffer_put_int(m, sshpam_get_maxtries_reached());
847#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000848
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000849 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000850 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000851
852 call_count++;
853 if (plen == 0 && call_count == 1)
854 auth_method = "none";
855 else
856 auth_method = "password";
857
858 /* Causes monitor loop to terminate if authenticated */
859 return (authenticated);
860}
861
862#ifdef BSD_AUTH
863int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000864mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000865{
866 char *name, *infotxt;
867 u_int numprompts;
868 u_int *echo_on;
869 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100870 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000871
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000872 if (!options.kbd_interactive_authentication)
873 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb7df3af2003-02-24 11:55:46 +1100874 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
875 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000876
877 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100878 buffer_put_int(m, success);
879 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000880 buffer_put_cstring(m, prompts[0]);
881
Damien Millerb7df3af2003-02-24 11:55:46 +1100882 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000883 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000884
Damien Millerb7df3af2003-02-24 11:55:46 +1100885 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000886 free(name);
887 free(infotxt);
888 free(prompts);
889 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000890 }
891
892 return (0);
893}
894
895int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000896mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000897{
898 char *response;
899 int authok;
900
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000901 if (!options.kbd_interactive_authentication)
902 fatal("%s: kbd-int authentication not enabled", __func__);
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +0000903 if (authctxt->as == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000904 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000905
906 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000907 authok = options.challenge_response_authentication &&
908 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000910 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000911 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000912
913 buffer_clear(m);
914 buffer_put_int(m, authok);
915
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000916 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000917 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000918
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000919 auth_method = "keyboard-interactive";
920 auth_submethod = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000921
922 return (authok != 0);
923}
924#endif
925
926#ifdef SKEY
927int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000928mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000929{
930 struct skey skey;
931 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100932 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000933
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000934 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
935 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000936
937 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100938 buffer_put_int(m, success);
939 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000940 buffer_put_cstring(m, challenge);
941
Damien Millerb7df3af2003-02-24 11:55:46 +1100942 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000943 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000944
945 return (0);
946}
947
948int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000949mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000950{
951 char *response;
952 int authok;
953
954 response = buffer_get_string(m, NULL);
955
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000956 authok = (options.challenge_response_authentication &&
957 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000958 skey_haskey(authctxt->pw->pw_name) == 0 &&
959 skey_passcheck(authctxt->pw->pw_name, response) != -1);
960
Darren Tuckerf60845f2013-06-02 08:07:31 +1000961 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000962
963 buffer_clear(m);
964 buffer_put_int(m, authok);
965
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000966 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000967 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000968
Damien Millerac947352015-11-20 12:35:41 +1100969 auth_method = "keyboard-interactive";
970 auth_submethod = "skey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000971
972 return (authok != 0);
973}
974#endif
975
Damien Miller79418552002-04-23 20:28:48 +1000976#ifdef USE_PAM
977int
Darren Tucker7eda5922004-06-22 13:22:50 +1000978mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000979{
Damien Miller4e448a32003-05-14 15:11:48 +1000980 if (!options.use_pam)
981 fatal("UsePAM not set, but ended up in %s anyway", __func__);
982
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100983 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000984
Damien Miller1f499fd2003-08-25 13:08:49 +1000985 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
Damien Miller775f8a22016-08-31 10:48:07 +1000986 if (options.kbd_interactive_authentication)
987 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller1f499fd2003-08-25 13:08:49 +1000988
Damien Miller79418552002-04-23 20:28:48 +1000989 return (0);
990}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000991
Damien Miller1f499fd2003-08-25 13:08:49 +1000992int
Darren Tucker7eda5922004-06-22 13:22:50 +1000993mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000994{
995 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100996
Damien Miller1f499fd2003-08-25 13:08:49 +1000997 if (!options.use_pam)
Damien Miller775f8a22016-08-31 10:48:07 +1000998 fatal("%s: PAM not enabled", __func__);
Damien Miller1f499fd2003-08-25 13:08:49 +1000999
1000 ret = do_pam_account();
1001
1002 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001003 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001004
Darren Tucker7eda5922004-06-22 13:22:50 +10001005 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001006
1007 return (ret);
1008}
1009
Damien Miller4f9f42a2003-05-10 19:28:02 +10001010static void *sshpam_ctxt, *sshpam_authok;
1011extern KbdintDevice sshpam_device;
1012
1013int
Darren Tucker7eda5922004-06-22 13:22:50 +10001014mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001015{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001016 debug3("%s", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001017 if (!options.kbd_interactive_authentication)
1018 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001019 if (sshpam_ctxt != NULL)
1020 fatal("%s: already called", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001021 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1022 sshpam_authok = NULL;
1023 buffer_clear(m);
1024 if (sshpam_ctxt != NULL) {
1025 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
Damien Millerb38b95f2016-08-29 11:47:07 +10001026 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001027 buffer_put_int(m, 1);
1028 } else {
1029 buffer_put_int(m, 0);
1030 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001031 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001032 return (0);
1033}
1034
1035int
Darren Tucker7eda5922004-06-22 13:22:50 +10001036mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001037{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001038 char *name = NULL, *info = NULL, **prompts = NULL;
1039 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001040 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001041
1042 debug3("%s", __func__);
1043 sshpam_authok = NULL;
Damien Millerb38b95f2016-08-29 11:47:07 +10001044 if (sshpam_ctxt == NULL)
1045 fatal("%s: no context", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001046 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1047 &num, &prompts, &echo_on);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001048 if (ret == 0 && num == 0)
1049 sshpam_authok = sshpam_ctxt;
1050 if (num > 1 || name == NULL || info == NULL)
Damien Millerb38b95f2016-08-29 11:47:07 +10001051 fatal("sshpam_device.query failed");
1052 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001053 buffer_clear(m);
1054 buffer_put_int(m, ret);
1055 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001056 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001057 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001058 free(info);
Darren Tucker01558b72016-07-18 09:33:25 +10001059 buffer_put_int(m, sshpam_get_maxtries_reached());
Damien Miller4f9f42a2003-05-10 19:28:02 +10001060 buffer_put_int(m, num);
1061 for (i = 0; i < num; ++i) {
1062 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001063 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001064 buffer_put_int(m, echo_on[i]);
1065 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001066 free(prompts);
1067 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001068 auth_method = "keyboard-interactive";
1069 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001070 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001071 return (0);
1072}
1073
1074int
Darren Tucker7eda5922004-06-22 13:22:50 +10001075mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001076{
1077 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001078 u_int i, num;
1079 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001080
1081 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001082 if (sshpam_ctxt == NULL)
1083 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001084 sshpam_authok = NULL;
1085 num = buffer_get_int(m);
1086 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001087 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001088 for (i = 0; i < num; ++i)
1089 resp[i] = buffer_get_string(m, NULL);
1090 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1091 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001092 free(resp[i]);
1093 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001094 } else {
1095 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1096 }
1097 buffer_clear(m);
1098 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001099 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001100 auth_method = "keyboard-interactive";
1101 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001102 if (ret == 0)
1103 sshpam_authok = sshpam_ctxt;
1104 return (0);
1105}
1106
1107int
Darren Tucker7eda5922004-06-22 13:22:50 +10001108mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001109{
Damien Miller5e75f512015-08-11 13:34:12 +10001110 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001111
1112 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001113 if (sshpam_ctxt == NULL)
1114 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001115 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001116 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001117 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001118 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Millerb38b95f2016-08-29 11:47:07 +10001119 /* Allow another attempt */
1120 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller15b05cf2012-12-03 09:53:20 +11001121 auth_method = "keyboard-interactive";
1122 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001123 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001124}
Damien Miller79418552002-04-23 20:28:48 +10001125#endif
1126
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001127int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001128mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001129{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001130 struct sshkey *key;
Damien Millera10f5612002-09-12 09:49:15 +10001131 char *cuser, *chost;
1132 u_char *blob;
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001133 u_int bloblen, pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001134 enum mm_keytype type = 0;
1135 int allowed = 0;
1136
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001137 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001138
1139 type = buffer_get_int(m);
1140 cuser = buffer_get_string(m, NULL);
1141 chost = buffer_get_string(m, NULL);
1142 blob = buffer_get_string(m, &bloblen);
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001143 pubkey_auth_attempt = buffer_get_int(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001144
1145 key = key_from_blob(blob, bloblen);
1146
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001147 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001148
Damien Miller3e3b5142003-11-17 21:13:40 +11001149 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001150 /* These should not make it past the privsep child */
1151 if (key_type_plain(key->type) == KEY_RSA &&
1152 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1153 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1154
Darren Tucker47eede72005-03-14 23:08:12 +11001155 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001156 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001157 allowed = options.pubkey_authentication &&
djm@openbsd.org8f574952017-06-24 06:34:38 +00001158 !auth2_key_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001159 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001160 options.pubkey_key_types, 0) == 1 &&
1161 user_key_allowed(authctxt->pw, key,
1162 pubkey_auth_attempt);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001163 auth_method = "publickey";
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001164 if (options.pubkey_authentication &&
1165 (!pubkey_auth_attempt || allowed != 1))
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001166 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001167 break;
1168 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001169 allowed = options.hostbased_authentication &&
djm@openbsd.org8f574952017-06-24 06:34:38 +00001170 !auth2_key_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001171 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001172 options.hostbased_key_types, 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001173 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001174 cuser, chost, key);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001175 auth2_record_info(authctxt,
Damien Miller20bdcd72013-07-18 16:10:09 +10001176 "client user \"%.100s\", client host \"%.100s\"",
1177 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001178 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001179 break;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001180 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001181 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001182 break;
1183 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001184 }
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001185
djm@openbsd.org8f574952017-06-24 06:34:38 +00001186 debug3("%s: key is %s", __func__, allowed ? "allowed" : "not allowed");
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001187
djm@openbsd.org8f574952017-06-24 06:34:38 +00001188 auth2_record_key(authctxt, 0, key);
1189 sshkey_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001190
1191 /* clear temporarily storage (used by verify) */
1192 monitor_reset_key_state();
1193
1194 if (allowed) {
1195 /* Save temporarily for comparison in verify */
1196 key_blob = blob;
1197 key_bloblen = bloblen;
1198 key_blobtype = type;
1199 hostbased_cuser = cuser;
1200 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001201 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001202 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001203 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001204 free(blob);
1205 free(cuser);
1206 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001207 }
1208
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001209 buffer_clear(m);
1210 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001211 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001212
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001213 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001214
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001215 return (0);
1216}
1217
1218static int
1219monitor_valid_userblob(u_char *data, u_int datalen)
1220{
1221 Buffer b;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001222 u_char *p;
1223 char *userstyle, *cp;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001224 u_int len;
1225 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001226
1227 buffer_init(&b);
1228 buffer_append(&b, data, datalen);
1229
1230 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001231 p = buffer_ptr(&b);
1232 len = buffer_len(&b);
1233 if ((session_id2 == NULL) ||
1234 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001235 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001236 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001237 buffer_consume(&b, session_id2_len);
1238 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001239 p = buffer_get_string(&b, &len);
1240 if ((session_id2 == NULL) ||
1241 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001242 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001243 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001244 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001245 }
1246 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1247 fail++;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001248 cp = buffer_get_cstring(&b, NULL);
Damien Miller4ce189d2013-04-23 15:17:52 +10001249 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1250 authctxt->style ? ":" : "",
1251 authctxt->style ? authctxt->style : "");
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001252 if (strcmp(userstyle, cp) != 0) {
1253 logit("wrong user name passed to monitor: "
1254 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001255 fail++;
1256 }
Darren Tuckera627d422013-06-02 07:31:17 +10001257 free(userstyle);
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001258 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001259 buffer_skip_string(&b);
1260 if (datafellows & SSH_BUG_PKAUTH) {
1261 if (!buffer_get_char(&b))
1262 fail++;
1263 } else {
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001264 cp = buffer_get_cstring(&b, NULL);
1265 if (strcmp("publickey", cp) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001266 fail++;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001267 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001268 if (!buffer_get_char(&b))
1269 fail++;
1270 buffer_skip_string(&b);
1271 }
1272 buffer_skip_string(&b);
1273 if (buffer_len(&b) != 0)
1274 fail++;
1275 buffer_free(&b);
1276 return (fail == 0);
1277}
1278
1279static int
Damien Millera10f5612002-09-12 09:49:15 +10001280monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1281 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001282{
1283 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001284 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001285 u_int len;
1286 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001287
1288 buffer_init(&b);
1289 buffer_append(&b, data, datalen);
1290
Ben Lindstromf67e0772002-06-06 20:58:19 +00001291 p = buffer_get_string(&b, &len);
1292 if ((session_id2 == NULL) ||
1293 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001294 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001296 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001297
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001298 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1299 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001300 p = buffer_get_cstring(&b, NULL);
1301 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1302 authctxt->style ? ":" : "",
1303 authctxt->style ? authctxt->style : "");
1304 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001305 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001306 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001307 fail++;
1308 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001309 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001310 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001311 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001312 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001313 if (strcmp(p, "hostbased") != 0)
1314 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001315 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001316 buffer_skip_string(&b); /* pkalg */
1317 buffer_skip_string(&b); /* pkblob */
1318
1319 /* verify client host, strip trailing dot if necessary */
1320 p = buffer_get_string(&b, NULL);
1321 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1322 p[len - 1] = '\0';
1323 if (strcmp(p, chost) != 0)
1324 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001325 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001326
1327 /* verify client user */
1328 p = buffer_get_string(&b, NULL);
1329 if (strcmp(p, cuser) != 0)
1330 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001331 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001332
1333 if (buffer_len(&b) != 0)
1334 fail++;
1335 buffer_free(&b);
1336 return (fail == 0);
1337}
1338
1339int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001340mm_answer_keyverify(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001341{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001342 struct sshkey *key;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001343 u_char *signature, *data, *blob;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001344 size_t signaturelen, datalen, bloblen;
1345 int r, ret, valid_data = 0, encoded_ret;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001346
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001347 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1348 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
1349 (r = sshbuf_get_string(m, &data, &datalen)) != 0)
1350 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001351
1352 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001353 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001354 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001355
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001356 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1357 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1358 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001359
1360 switch (key_blobtype) {
1361 case MM_USERKEY:
1362 valid_data = monitor_valid_userblob(data, datalen);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001363 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001364 break;
1365 case MM_HOSTKEY:
1366 valid_data = monitor_valid_hostbasedblob(data, datalen,
1367 hostbased_cuser, hostbased_chost);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001368 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001369 break;
1370 default:
1371 valid_data = 0;
1372 break;
1373 }
1374 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001375 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001376
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001377 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
1378 active_state->compat);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001379 debug3("%s: %s %p signature %s", __func__, auth_method, key,
1380 (ret == 0) ? "verified" : "unverified");
1381 auth2_record_key(authctxt, ret == 0, key);
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001382
Darren Tuckera627d422013-06-02 07:31:17 +10001383 free(blob);
1384 free(signature);
1385 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001386
1387 monitor_reset_key_state();
1388
djm@openbsd.org8f574952017-06-24 06:34:38 +00001389 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001390 sshbuf_reset(m);
1391
1392 /* encode ret != 0 as positive integer, since we're sending u32 */
1393 encoded_ret = (ret != 0);
1394 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1395 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001396 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001397
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001398 return ret == 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001399}
1400
1401static void
1402mm_record_login(Session *s, struct passwd *pw)
1403{
djm@openbsd.org95767262016-03-07 19:02:43 +00001404 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001405 socklen_t fromlen;
1406 struct sockaddr_storage from;
1407
1408 /*
1409 * Get IP address of client. If the connection is not a socket, let
1410 * the address be 0.0.0.0.
1411 */
1412 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001413 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001414 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001415 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001416 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001417 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001418 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001419 }
1420 }
1421 /* Record that there was a login on that tty from the remote host. */
1422 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +00001423 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001424 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001425}
1426
1427static void
1428mm_session_close(Session *s)
1429{
Damien Miller04bd8b02003-05-25 14:38:33 +10001430 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001431 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001432 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001433 session_pty_cleanup2(s);
1434 }
Damien Miller7207f642008-05-19 15:34:50 +10001435 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001436}
1437
1438int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001439mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001440{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001441 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001442 Session *s;
1443 int res, fd0;
1444
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001445 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001446
1447 buffer_clear(m);
1448 s = session_new();
1449 if (s == NULL)
1450 goto error;
1451 s->authctxt = authctxt;
1452 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001453 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001454 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1455 if (res == 0)
1456 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457 pty_setowner(authctxt->pw, s->tty);
1458
1459 buffer_put_int(m, 1);
1460 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001461
1462 /* We need to trick ttyslot */
1463 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001464 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001465
1466 mm_record_login(s, authctxt->pw);
1467
1468 /* Now we can close the file descriptor again */
1469 close(0);
1470
Darren Tucker09991742004-07-17 17:05:14 +10001471 /* send messages generated by record_login */
1472 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1473 buffer_clear(&loginmsg);
1474
1475 mm_request_send(sock, MONITOR_ANS_PTY, m);
1476
Damien Miller54fd7cf2007-09-17 12:04:08 +10001477 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1478 mm_send_fd(sock, s->ttyfd) == -1)
1479 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001480
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001481 /* make sure nothing uses fd 0 */
1482 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001483 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001484 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001485 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001486
1487 /* slave is not needed */
1488 close(s->ttyfd);
1489 s->ttyfd = s->ptyfd;
1490 /* no need to dup() because nobody closes ptyfd */
1491 s->ptymaster = s->ptyfd;
1492
Damien Miller9ab00b42006-08-05 12:40:11 +10001493 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001494
1495 return (0);
1496
1497 error:
1498 if (s != NULL)
1499 mm_session_close(s);
1500 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001501 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001502 return (0);
1503}
1504
1505int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001506mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001507{
1508 Session *s;
1509 char *tty;
1510
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001511 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001512
1513 tty = buffer_get_string(m, NULL);
1514 if ((s = session_by_tty(tty)) != NULL)
1515 mm_session_close(s);
1516 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001517 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001518 return (0);
1519}
1520
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001521int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001522mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001523{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001524 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001525 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001526 int res, status;
1527
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001528 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001529
1530 /* The child is terminating */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001531 session_destroy_all(ssh, &mm_session_close);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532
Darren Tucker52358d62008-03-11 22:58:25 +11001533#ifdef USE_PAM
1534 if (options.use_pam)
1535 sshpam_cleanup();
1536#endif
1537
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001538 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001539 if (errno != EINTR)
1540 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001541
1542 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1543
1544 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001545 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001546}
1547
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001548#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001549/* Report that an audit event occurred */
1550int
1551mm_answer_audit_event(int socket, Buffer *m)
1552{
1553 ssh_audit_event_t event;
1554
1555 debug3("%s entering", __func__);
1556
1557 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001558 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001559 case SSH_AUTH_FAIL_PUBKEY:
1560 case SSH_AUTH_FAIL_HOSTBASED:
1561 case SSH_AUTH_FAIL_GSSAPI:
1562 case SSH_LOGIN_EXCEED_MAXTRIES:
1563 case SSH_LOGIN_ROOT_DENIED:
1564 case SSH_CONNECTION_CLOSE:
1565 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001566 audit_event(event);
1567 break;
1568 default:
1569 fatal("Audit event type %d not permitted", event);
1570 }
1571
1572 return (0);
1573}
1574
1575int
1576mm_answer_audit_command(int socket, Buffer *m)
1577{
1578 u_int len;
1579 char *cmd;
1580
1581 debug3("%s entering", __func__);
1582 cmd = buffer_get_string(m, &len);
1583 /* sanity check command, if so how? */
1584 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001585 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001586 return (0);
1587}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001588#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001589
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001590void
markus@openbsd.org1e0cdf82017-05-31 08:09:45 +00001591monitor_clear_keystate(struct monitor *pmonitor)
1592{
1593 struct ssh *ssh = active_state; /* XXX */
1594
1595 ssh_clear_newkeys(ssh, MODE_IN);
1596 ssh_clear_newkeys(ssh, MODE_OUT);
1597 sshbuf_free(child_state);
1598 child_state = NULL;
1599}
1600
1601void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001602monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001603{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001604 struct ssh *ssh = active_state; /* XXX */
1605 struct kex *kex;
1606 int r;
1607
1608 debug3("%s: packet_set_state", __func__);
1609 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1610 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1611 sshbuf_free(child_state);
1612 child_state = NULL;
1613
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +00001614 if ((kex = ssh->kex) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001615 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001616#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001617 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1618 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +00001619 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1620 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1621 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001622 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1623 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001624# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001625 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001626# endif
Damien Miller773dda22015-01-30 23:10:17 +11001627#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001628 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1629 kex->load_host_public_key=&get_hostkey_public_by_type;
1630 kex->load_host_private_key=&get_hostkey_private_by_type;
1631 kex->host_key_index=&get_hostkey_index;
1632 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001633 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001634}
1635
1636/* This function requries careful sanity checking */
1637
1638void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001639mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001640{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001641 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001642
markus@openbsd.org091c3022015-01-19 19:52:16 +00001643 if ((child_state = sshbuf_new()) == NULL)
1644 fatal("%s: sshbuf_new failed", __func__);
1645 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1646 child_state);
1647 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001648}
1649
1650
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001651/* XXX */
1652
1653#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001654 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001655 fatal("fcntl(%d, F_SETFD)", x); \
1656} while (0)
1657
1658static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001659monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001660{
Damien Miller8f0bf232011-06-20 14:42:23 +10001661 int pair[2];
markus@openbsd.org84008602017-05-31 10:04:29 +00001662#ifdef SO_ZEROIZE
1663 int on = 1;
1664#endif
Damien Miller8f0bf232011-06-20 14:42:23 +10001665
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001666 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001667 fatal("%s: socketpair: %s", __func__, strerror(errno));
markus@openbsd.org84008602017-05-31 10:04:29 +00001668#ifdef SO_ZEROIZE
1669 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1670 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1671 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1672 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1673#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001674 FD_CLOSEONEXEC(pair[0]);
1675 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001676 mon->m_recvfd = pair[0];
1677 mon->m_sendfd = pair[1];
1678
1679 if (do_logfds) {
1680 if (pipe(pair) == -1)
1681 fatal("%s: pipe: %s", __func__, strerror(errno));
1682 FD_CLOSEONEXEC(pair[0]);
1683 FD_CLOSEONEXEC(pair[1]);
1684 mon->m_log_recvfd = pair[0];
1685 mon->m_log_sendfd = pair[1];
1686 } else
1687 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001688}
1689
1690#define MM_MEMSIZE 65536
1691
1692struct monitor *
1693monitor_init(void)
1694{
1695 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001696
Damien Miller07d86be2006-03-26 14:19:21 +11001697 mon = xcalloc(1, sizeof(*mon));
Damien Miller8f0bf232011-06-20 14:42:23 +10001698 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001699
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001700 return mon;
1701}
1702
1703void
1704monitor_reinit(struct monitor *mon)
1705{
Damien Miller8f0bf232011-06-20 14:42:23 +10001706 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001707}
Darren Tucker0efd1552003-08-26 11:49:55 +10001708
1709#ifdef GSSAPI
1710int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001711mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001712{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001713 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001714 OM_uint32 major;
1715 u_int len;
1716
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001717 if (!options.gss_authentication)
1718 fatal("%s: GSSAPI authentication not enabled", __func__);
1719
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001720 goid.elements = buffer_get_string(m, &len);
1721 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001722
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001723 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001724
Darren Tuckera627d422013-06-02 07:31:17 +10001725 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001726
1727 buffer_clear(m);
1728 buffer_put_int(m, major);
1729
Damien Miller6fd6def2005-11-05 15:07:05 +11001730 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001731
1732 /* Now we have a context, enable the step */
1733 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1734
1735 return (0);
1736}
1737
1738int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001739mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001740{
1741 gss_buffer_desc in;
1742 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001743 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001744 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001745 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001746
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001747 if (!options.gss_authentication)
1748 fatal("%s: GSSAPI authentication not enabled", __func__);
1749
Darren Tucker600ad8d2003-08-26 12:10:48 +10001750 in.value = buffer_get_string(m, &len);
1751 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001752 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001753 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001754
1755 buffer_clear(m);
1756 buffer_put_int(m, major);
1757 buffer_put_string(m, out.value, out.length);
1758 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001759 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001760
1761 gss_release_buffer(&minor, &out);
1762
Damien Miller6fd6def2005-11-05 15:07:05 +11001763 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001764 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1765 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001766 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001767 }
1768 return (0);
1769}
1770
1771int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001772mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001773{
1774 gss_buffer_desc gssbuf, mic;
1775 OM_uint32 ret;
1776 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001777
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001778 if (!options.gss_authentication)
1779 fatal("%s: GSSAPI authentication not enabled", __func__);
1780
Damien Miller0425d402003-11-17 22:18:21 +11001781 gssbuf.value = buffer_get_string(m, &len);
1782 gssbuf.length = len;
1783 mic.value = buffer_get_string(m, &len);
1784 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001785
Damien Miller0425d402003-11-17 22:18:21 +11001786 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001787
Darren Tuckera627d422013-06-02 07:31:17 +10001788 free(gssbuf.value);
1789 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001790
Damien Miller0425d402003-11-17 22:18:21 +11001791 buffer_clear(m);
1792 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001793
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001794 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001795
Damien Miller0425d402003-11-17 22:18:21 +11001796 if (!GSS_ERROR(ret))
1797 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001798
Damien Miller0425d402003-11-17 22:18:21 +11001799 return (0);
1800}
1801
1802int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001803mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001804{
1805 int authenticated;
djm@openbsd.org8f574952017-06-24 06:34:38 +00001806 const char *displayname;
Darren Tucker0efd1552003-08-26 11:49:55 +10001807
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001808 if (!options.gss_authentication)
1809 fatal("%s: GSSAPI authentication not enabled", __func__);
1810
Darren Tucker0efd1552003-08-26 11:49:55 +10001811 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1812
1813 buffer_clear(m);
1814 buffer_put_int(m, authenticated);
1815
1816 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001817 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001818
Damien Miller6fd6def2005-11-05 15:07:05 +11001819 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001820
djm@openbsd.org8f574952017-06-24 06:34:38 +00001821 if ((displayname = ssh_gssapi_displayname()) != NULL)
1822 auth2_record_info(authctxt, "%s", displayname);
1823
Darren Tucker0efd1552003-08-26 11:49:55 +10001824 /* Monitor loop will terminate if authenticated */
1825 return (authenticated);
1826}
1827#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001828