blob: 39bf7705c10fec5a8c2c78973c60d3c2cb5e7a60 [file] [log] [blame]
djm@openbsd.org3a00a922019-01-19 21:41:18 +00001/* $OpenBSD: monitor.c,v 1.190 2019/01/19 21:41:18 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 Miller9cf6d072006-03-15 11:29:24 +110032#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000033
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100035#include <fcntl.h>
djm@openbsd.org1a31d022016-05-02 08:49:03 +000036#include <limits.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110037#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110038#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110039#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100040#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110041#include <signal.h>
Damien Miller9af21972015-02-24 09:04:32 +110042#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000043#include <stdint.h>
Damien Miller9af21972015-02-24 09:04:32 +110044#endif
Damien Millere7a1e5c2006-08-05 11:34:19 +100045#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100046#include <string.h>
Damien Miller86687062014-07-02 15:28:02 +100047#include <stdarg.h>
48#include <stdio.h>
Damien Miller607aede2006-09-01 15:48:19 +100049#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100050#ifdef HAVE_POLL_H
51#include <poll.h>
52#else
53# ifdef HAVE_SYS_POLL_H
54# include <sys/poll.h>
55# endif
56#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000057
Damien Miller1f0311c2014-05-15 14:24:09 +100058#ifdef WITH_OPENSSL
Damien Miller03e20032006-03-15 11:16:59 +110059#include <openssl/dh.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100060#endif
Damien Miller03e20032006-03-15 11:16:59 +110061
Damien Miller48f54b92018-09-13 12:13:50 +100062#include "openbsd-compat/sys-tree.h"
Damien Millerb84886b2008-05-19 15:05:07 +100063#include "openbsd-compat/sys-queue.h"
Damien Miller48f54b92018-09-13 12:13:50 +100064#include "openbsd-compat/openssl-compat.h"
65
Damien Miller8f0bf232011-06-20 14:42:23 +100066#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100067#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000068#include "ssh.h"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +000069#include "sshkey.h"
markus@openbsd.org235c7c42018-07-09 21:53:45 +000070#include "sshbuf.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000072#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100073#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000074#include "kex.h"
75#include "dh.h"
Darren Tucker01558b72016-07-18 09:33:25 +100076#include "auth-pam.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000077#include "packet.h"
78#include "auth-options.h"
79#include "sshpty.h"
80#include "channels.h"
81#include "session.h"
82#include "sshlogin.h"
83#include "canohost.h"
84#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100085#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000086#include "servconf.h"
87#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +100088#ifdef GSSAPI
89#include "ssh-gss.h"
90#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000091#include "monitor_wrap.h"
92#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093#include "compat.h"
94#include "ssh2.h"
Damien Miller85b45e02013-07-20 13:21:52 +100095#include "authfd.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +000096#include "match.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +000097#include "ssherr.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000098
djm@openbsd.org0fa174e2019-01-19 21:31:32 +000099#include "opacket.h" /* XXX */
100extern struct ssh *active_state; /* XXX */
101
Darren Tucker0efd1552003-08-26 11:49:55 +1000102#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000103static Gssctxt *gsscontext = NULL;
104#endif
105
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000106/* Imports */
107extern ServerOptions options;
108extern u_int utmp_len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000109extern u_char session_id[];
markus@openbsd.org2808d182018-07-09 21:26:02 +0000110extern struct sshbuf *loginmsg;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000111extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000112
113/* State exported from the child */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000114static struct sshbuf *child_state;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000115
Damien Miller469954d2003-06-18 20:25:33 +1000116/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000117
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000118int mm_answer_moduli(int, struct sshbuf *);
119int mm_answer_sign(int, struct sshbuf *);
120int mm_answer_pwnamallow(int, struct sshbuf *);
121int mm_answer_auth2_read_banner(int, struct sshbuf *);
122int mm_answer_authserv(int, struct sshbuf *);
123int mm_answer_authpassword(int, struct sshbuf *);
124int mm_answer_bsdauthquery(int, struct sshbuf *);
125int mm_answer_bsdauthrespond(int, struct sshbuf *);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000126int mm_answer_keyallowed(int, struct sshbuf *);
127int mm_answer_keyverify(int, struct sshbuf *);
128int mm_answer_pty(int, struct sshbuf *);
129int mm_answer_pty_cleanup(int, struct sshbuf *);
130int mm_answer_term(int, struct sshbuf *);
131int mm_answer_rsa_keyallowed(int, struct sshbuf *);
132int mm_answer_rsa_challenge(int, struct sshbuf *);
133int mm_answer_rsa_response(int, struct sshbuf *);
134int mm_answer_sesskey(int, struct sshbuf *);
135int mm_answer_sessid(int, struct sshbuf *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000136
Damien Miller79418552002-04-23 20:28:48 +1000137#ifdef USE_PAM
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000138int mm_answer_pam_start(int, struct sshbuf *);
139int mm_answer_pam_account(int, struct sshbuf *);
140int mm_answer_pam_init_ctx(int, struct sshbuf *);
141int mm_answer_pam_query(int, struct sshbuf *);
142int mm_answer_pam_respond(int, struct sshbuf *);
143int mm_answer_pam_free_ctx(int, struct sshbuf *);
Damien Miller79418552002-04-23 20:28:48 +1000144#endif
145
Darren Tucker0efd1552003-08-26 11:49:55 +1000146#ifdef GSSAPI
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000147int mm_answer_gss_setup_ctx(int, struct sshbuf *);
148int mm_answer_gss_accept_ctx(int, struct sshbuf *);
149int mm_answer_gss_userok(int, struct sshbuf *);
150int mm_answer_gss_checkmic(int, struct sshbuf *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000151#endif
Damien Miller25162f22002-09-12 09:47:29 +1000152
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100153#ifdef SSH_AUDIT_EVENTS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000154int mm_answer_audit_event(int, struct sshbuf *);
155int mm_answer_audit_command(int, struct sshbuf *);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100156#endif
157
Damien Miller8f0bf232011-06-20 14:42:23 +1000158static int monitor_read_log(struct monitor *);
159
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000160static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000161
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000162/* local state for key verify */
163static u_char *key_blob = NULL;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000164static size_t key_bloblen = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000165static int key_blobtype = MM_NOKEY;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000166static struct sshauthopt *key_opts = NULL;
Damien Millera10f5612002-09-12 09:49:15 +1000167static char *hostbased_cuser = NULL;
168static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000169static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100170static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000171static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000172static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000173static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000174
175struct mon_table {
176 enum monitor_reqtype type;
177 int flags;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000178 int (*f)(int, struct sshbuf *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000179};
180
181#define MON_ISAUTH 0x0004 /* Required for Authentication */
182#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
183#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100184#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000185
186#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
187
188#define MON_PERMIT 0x1000 /* Request is permitted */
189
190struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000191#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000192 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000193#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000194 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
195 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
196 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000197 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000198 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000199#ifdef USE_PAM
200 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000201 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Millerb38b95f2016-08-29 11:47:07 +1000202 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
203 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
204 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000205 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000206#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100207#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100208 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100209#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000210#ifdef BSD_AUTH
211 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100212 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000213#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000214 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
215 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000216#ifdef GSSAPI
217 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
djm@openbsd.orgb33ad6d2016-09-05 13:57:31 +0000218 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
219 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
220 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000221#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000222 {0, 0, NULL}
223};
224
225struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000226#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000227 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000228#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000229 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
230 {MONITOR_REQ_PTY, 0, mm_answer_pty},
231 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
232 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100233#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100234 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
235 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
236#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000237 {0, 0, NULL}
238};
239
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000240struct mon_table *mon_dispatch;
241
242/* Specifies if a certain message is allowed at the moment */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000243static void
244monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
245{
246 while (ent->f != NULL) {
247 if (ent->type == type) {
248 ent->flags &= ~MON_PERMIT;
249 ent->flags |= permit ? MON_PERMIT : 0;
250 return;
251 }
252 ent++;
253 }
254}
255
256static void
257monitor_permit_authentications(int permit)
258{
259 struct mon_table *ent = mon_dispatch;
260
261 while (ent->f != NULL) {
262 if (ent->flags & MON_AUTH) {
263 ent->flags &= ~MON_PERMIT;
264 ent->flags |= permit ? MON_PERMIT : 0;
265 }
266 ent++;
267 }
268}
269
Darren Tucker3e33cec2003-10-02 16:12:36 +1000270void
271monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000272{
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000273 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000274 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100275 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000276
277 debug3("preauth child monitor started");
278
tb@openbsd.org34843802018-02-05 05:37:46 +0000279 if (pmonitor->m_recvfd >= 0)
280 close(pmonitor->m_recvfd);
281 if (pmonitor->m_log_sendfd >= 0)
282 close(pmonitor->m_log_sendfd);
Damien Miller8f0bf232011-06-20 14:42:23 +1000283 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
284
Darren Tucker3e33cec2003-10-02 16:12:36 +1000285 authctxt = _authctxt;
286 memset(authctxt, 0, sizeof(*authctxt));
djm@openbsd.org7c856852018-03-03 03:15:51 +0000287 ssh->authctxt = authctxt;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000288
Damien Miller120a1ec2018-07-10 19:39:52 +1000289 authctxt->loginmsg = loginmsg;
Darren Tuckerde0de392005-03-31 23:52:04 +1000290
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000291 mon_dispatch = mon_dispatch_proto20;
292 /* Permit requests for moduli and signatures */
293 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
294 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000295
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000296 /* The first few requests do not require asynchronous access */
297 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100298 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100299 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100300 auth_submethod = NULL;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000301 auth2_authctxt_reset_info(authctxt);
302
Darren Tuckerfbba7352006-11-07 23:16:08 +1100303 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100304
305 /* Special handling for multiple required authentications */
306 if (options.num_auth_methods != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100307 if (authenticated &&
308 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000309 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100310 debug3("%s: method %s: partial", __func__,
311 auth_method);
312 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100313 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100314 }
315 }
316
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000317 if (authenticated) {
318 if (!(ent->flags & MON_AUTHDECIDE))
319 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000320 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000321 if (authctxt->pw->pw_uid == 0 &&
djm@openbsd.org7c856852018-03-03 03:15:51 +0000322 !auth_root_allowed(ssh, auth_method))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000323 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000324#ifdef USE_PAM
325 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100326 if (options.use_pam && authenticated) {
Damien Miller120a1ec2018-07-10 19:39:52 +1000327 struct sshbuf *m;
Damien Miller1f499fd2003-08-25 13:08:49 +1000328
Damien Miller120a1ec2018-07-10 19:39:52 +1000329 if ((m = sshbuf_new()) == NULL)
330 fatal("%s: sshbuf_new failed",
331 __func__);
Damien Millera8e06ce2003-11-21 23:48:55 +1100332 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller120a1ec2018-07-10 19:39:52 +1000333 MONITOR_REQ_PAM_ACCOUNT, m);
334 authenticated = mm_answer_pam_account(
335 pmonitor->m_sendfd, m);
336 sshbuf_free(m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000337 }
338#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000339 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100340 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
djm@openbsd.org3a00a922019-01-19 21:41:18 +0000341 auth_log(ssh, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000342 auth_method, auth_submethod);
djm@openbsd.org94885382015-06-22 23:42:16 +0000343 if (!partial && !authenticated)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000344 authctxt->failures++;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000345 if (authenticated || partial) {
346 auth2_update_session_info(authctxt,
347 auth_method, auth_submethod);
348 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000349 }
350 }
351
352 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000353 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100354 if (strcmp(auth_method, "unknown") == 0)
355 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356
357 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000358 __func__, authctxt->user);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000359 ssh->authctxt = NULL;
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000360 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000361
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000362 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000363
Damien Miller6a1937e2012-12-12 10:44:38 +1100364 /* Drain any buffered messages from the child */
365 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
366 ;
367
tb@openbsd.org34843802018-02-05 05:37:46 +0000368 if (pmonitor->m_recvfd >= 0)
369 close(pmonitor->m_recvfd);
370 if (pmonitor->m_log_sendfd >= 0)
371 close(pmonitor->m_log_sendfd);
Damien Miller8f0bf232011-06-20 14:42:23 +1000372 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000373}
374
Damien Millerbe64d432003-05-14 19:31:12 +1000375static void
376monitor_set_child_handler(pid_t pid)
377{
378 monitor_child_pid = pid;
379}
380
381static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000382monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000383{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000384 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000385}
386
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000387void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000388monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000389{
Damien Miller8f0bf232011-06-20 14:42:23 +1000390 close(pmonitor->m_recvfd);
391 pmonitor->m_recvfd = -1;
392
Damien Millerbe64d432003-05-14 19:31:12 +1000393 monitor_set_child_handler(pmonitor->m_pid);
394 signal(SIGHUP, &monitor_child_handler);
395 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000396 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000397#ifdef SIGXFSZ
398 signal(SIGXFSZ, SIG_IGN);
399#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000400
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000401 mon_dispatch = mon_dispatch_postauth20;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000402
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000403 /* Permit requests for moduli and signatures */
404 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
405 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
406 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
407
djm@openbsd.org7c856852018-03-03 03:15:51 +0000408 if (auth_opts->permit_pty_flag) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000409 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
410 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
411 }
412
413 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000414 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000415}
416
Damien Miller8f0bf232011-06-20 14:42:23 +1000417static int
418monitor_read_log(struct monitor *pmonitor)
419{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000420 struct sshbuf *logmsg;
Damien Miller8f0bf232011-06-20 14:42:23 +1000421 u_int len, level;
422 char *msg;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000423 u_char *p;
424 int r;
Damien Miller8f0bf232011-06-20 14:42:23 +1000425
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000426 if ((logmsg = sshbuf_new()) == NULL)
427 fatal("%s: sshbuf_new", __func__);
Damien Miller8f0bf232011-06-20 14:42:23 +1000428
429 /* Read length */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000430 if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
431 fatal("%s: reserve: %s", __func__, ssh_err(r));
432 if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
Damien Miller8f0bf232011-06-20 14:42:23 +1000433 if (errno == EPIPE) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000434 sshbuf_free(logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000435 debug("%s: child log fd closed", __func__);
436 close(pmonitor->m_log_recvfd);
437 pmonitor->m_log_recvfd = -1;
438 return -1;
439 }
440 fatal("%s: log fd read: %s", __func__, strerror(errno));
441 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000442 if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
443 fatal("%s: get len: %s", __func__, ssh_err(r));
Damien Miller8f0bf232011-06-20 14:42:23 +1000444 if (len <= 4 || len > 8192)
445 fatal("%s: invalid log message length %u", __func__, len);
446
447 /* Read severity, message */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000448 sshbuf_reset(logmsg);
449 if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
450 fatal("%s: reserve: %s", __func__, ssh_err(r));
451 if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
Damien Miller8f0bf232011-06-20 14:42:23 +1000452 fatal("%s: log fd read: %s", __func__, strerror(errno));
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000453 if ((r = sshbuf_get_u32(logmsg, &level)) != 0 ||
454 (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
455 fatal("%s: decode: %s", __func__, ssh_err(r));
Damien Miller8f0bf232011-06-20 14:42:23 +1000456
457 /* Log it */
Damien Miller8f0bf232011-06-20 14:42:23 +1000458 if (log_level_name(level) == NULL)
459 fatal("%s: invalid log level %u (corrupted message?)",
460 __func__, level);
461 do_log2(level, "%s [preauth]", msg);
462
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000463 sshbuf_free(logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000464 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000465
466 return 0;
467}
468
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000469int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000470monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000471 struct mon_table **pent)
472{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000473 struct sshbuf *m;
474 int r, ret;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000475 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000476 struct pollfd pfd[2];
477
478 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100479 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000480 pfd[0].fd = pmonitor->m_sendfd;
481 pfd[0].events = POLLIN;
482 pfd[1].fd = pmonitor->m_log_recvfd;
483 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000484 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
485 if (errno == EINTR || errno == EAGAIN)
486 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000487 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000488 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000489 if (pfd[1].revents) {
490 /*
491 * Drain all log messages before processing next
492 * monitor request.
493 */
494 monitor_read_log(pmonitor);
495 continue;
496 }
497 if (pfd[0].revents)
498 break; /* Continues below */
499 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000500
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000501 if ((m = sshbuf_new()) == NULL)
502 fatal("%s: sshbuf_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000503
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000504 mm_request_receive(pmonitor->m_sendfd, m);
505 if ((r = sshbuf_get_u8(m, &type)) != 0)
506 fatal("%s: decode: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000507
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000508 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000509
510 while (ent->f != NULL) {
511 if (ent->type == type)
512 break;
513 ent++;
514 }
515
516 if (ent->f != NULL) {
517 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000518 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000519 type);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000520 ret = (*ent->f)(pmonitor->m_sendfd, m);
521 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000522
523 /* The child may use this request only once, disable it */
524 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000525 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000526 type);
527 ent->flags &= ~MON_PERMIT;
528 }
529
530 if (pent != NULL)
531 *pent = ent;
532
533 return ret;
534 }
535
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000536 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000537
538 /* NOTREACHED */
539 return (-1);
540}
541
542/* allowed key state */
543static int
544monitor_allowed_key(u_char *blob, u_int bloblen)
545{
546 /* make sure key is allowed */
547 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000548 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000549 return (0);
550 return (1);
551}
552
553static void
554monitor_reset_key_state(void)
555{
556 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000557 free(key_blob);
558 free(hostbased_cuser);
559 free(hostbased_chost);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000560 sshauthopt_free(key_opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561 key_blob = NULL;
562 key_bloblen = 0;
563 key_blobtype = MM_NOKEY;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000564 key_opts = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000565 hostbased_cuser = NULL;
566 hostbased_chost = NULL;
567}
568
Damien Miller1f0311c2014-05-15 14:24:09 +1000569#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000570int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000571mm_answer_moduli(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000572{
573 DH *dh;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000574 const BIGNUM *dh_p, *dh_g;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000575 int r;
576 u_int min, want, max;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000578 if ((r = sshbuf_get_u32(m, &min)) != 0 ||
579 (r = sshbuf_get_u32(m, &want)) != 0 ||
580 (r = sshbuf_get_u32(m, &max)) != 0)
581 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000582
583 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000584 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000585 /* We need to check here, too, in case the child got corrupted */
586 if (max < min || want < min || max < want)
587 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000588 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000589
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000590 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000591
592 dh = choose_dh(min, want, max);
593 if (dh == NULL) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000594 if ((r = sshbuf_put_u8(m, 0)) != 0)
595 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000596 return (0);
597 } else {
598 /* Send first bignum */
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000599 DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000600 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000601 (r = sshbuf_put_bignum2(m, dh_p)) != 0 ||
602 (r = sshbuf_put_bignum2(m, dh_g)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000603 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604
605 DH_free(dh);
606 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000607 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000608 return (0);
609}
Damien Miller1f0311c2014-05-15 14:24:09 +1000610#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000611
612int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000613mm_answer_sign(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000614{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000615 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000616 extern int auth_sock; /* XXX move to state struct? */
617 struct sshkey *key;
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000618 struct sshbuf *sigbuf = NULL;
619 u_char *p = NULL, *signature = NULL;
620 char *alg = NULL;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000621 size_t datlen, siglen, alglen;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000622 int r, is_proof = 0;
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000623 u_int keyid, compat;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000624 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000625
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000626 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000627
djm@openbsd.org141efe42015-01-14 20:05:27 +0000628 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000629 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000630 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0 ||
631 (r = sshbuf_get_u32(m, &compat)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000632 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000633 if (keyid > INT_MAX)
634 fatal("%s: invalid key ID", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000635
Damien Millera63128d2006-03-15 12:08:28 +1100636 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000637 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
638 * SHA384 (48 bytes) and SHA512 (64 bytes).
djm@openbsd.org523463a2015-02-16 22:13:32 +0000639 *
640 * Otherwise, verify the signature request is for a hostkey
641 * proof.
642 *
643 * XXX perform similar check for KEX signature requests too?
644 * it's not trivial, since what is signed is the hash, rather
645 * than the full kex structure...
Damien Millera63128d2006-03-15 12:08:28 +1100646 */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000647 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
648 /*
649 * Construct expected hostkey proof and compare it to what
650 * the client sent us.
651 */
652 if (session_id2_len == 0) /* hostkeys is never first */
653 fatal("%s: bad data length: %zu", __func__, datlen);
654 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
655 fatal("%s: no hostkey for index %d", __func__, keyid);
656 if ((sigbuf = sshbuf_new()) == NULL)
657 fatal("%s: sshbuf_new", __func__);
djm@openbsd.org44732de2015-02-20 22:17:21 +0000658 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
659 (r = sshbuf_put_string(sigbuf, session_id2,
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000660 session_id2_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000661 (r = sshkey_puts(key, sigbuf)) != 0)
662 fatal("%s: couldn't prepare private key "
663 "proof buffer: %s", __func__, ssh_err(r));
664 if (datlen != sshbuf_len(sigbuf) ||
665 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
666 fatal("%s: bad data length: %zu, hostkey proof len %zu",
667 __func__, datlen, sshbuf_len(sigbuf));
668 sshbuf_free(sigbuf);
669 is_proof = 1;
670 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000671
Ben Lindstromf67e0772002-06-06 20:58:19 +0000672 /* save session id, it will be passed on the first call */
673 if (session_id2_len == 0) {
674 session_id2_len = datlen;
675 session_id2 = xmalloc(session_id2_len);
676 memcpy(session_id2, p, session_id2_len);
677 }
678
Damien Miller85b45e02013-07-20 13:21:52 +1000679 if ((key = get_hostkey_by_index(keyid)) != NULL) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000680 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000681 compat)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000682 fatal("%s: sshkey_sign failed: %s",
683 __func__, ssh_err(r));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000684 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000685 auth_sock > 0) {
686 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000687 p, datlen, alg, compat)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000688 fatal("%s: ssh_agent_sign failed: %s",
689 __func__, ssh_err(r));
690 }
Damien Miller85b45e02013-07-20 13:21:52 +1000691 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000692 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000693
djm@openbsd.org523463a2015-02-16 22:13:32 +0000694 debug3("%s: %s signature %p(%zu)", __func__,
695 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000696
djm@openbsd.org141efe42015-01-14 20:05:27 +0000697 sshbuf_reset(m);
698 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
699 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000700
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000701 free(alg);
Darren Tuckera627d422013-06-02 07:31:17 +1000702 free(p);
703 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000704
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000705 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000706
707 /* Turn on permissions for getpwnam */
708 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
709
710 return (0);
711}
712
713/* Retrieves the password entry and also checks if the user is permitted */
714
715int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000716mm_answer_pwnamallow(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000717{
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000718 struct ssh *ssh = active_state; /* XXX */
Darren Tuckerb09b6772004-06-22 15:06:46 +1000719 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000720 struct passwd *pwent;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000721 int r, allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000722 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000723
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000724 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725
726 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000727 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000729 if ((r = sshbuf_get_cstring(m, &username, NULL)) != 0)
730 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000731
djm@openbsd.org3a00a922019-01-19 21:41:18 +0000732 pwent = getpwnamallow(ssh, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000733
Darren Tuckerb09b6772004-06-22 15:06:46 +1000734 authctxt->user = xstrdup(username);
735 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000736 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000737
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000738 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000739
740 if (pwent == NULL) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000741 if ((r = sshbuf_put_u8(m, 0)) != 0)
742 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf96d1832003-11-18 22:01:48 +1100743 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000744 goto out;
745 }
746
747 allowed = 1;
748 authctxt->pw = pwent;
749 authctxt->valid = 1;
750
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000751 /* XXX don't sent pwent to unpriv; send fake class/dir/shell too */
752 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
753 (r = sshbuf_put_string(m, pwent, sizeof(*pwent))) != 0 ||
754 (r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
755 (r = sshbuf_put_cstring(m, "*")) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000756#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000757 (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000758#endif
759#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000760 (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
Kevin Steves7e147602002-03-22 18:07:17 +0000761#endif
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000762 (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
763 (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
764 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100765
766 out:
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000767 ssh_packet_set_log_preamble(ssh, "%suser %s",
768 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000769 if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
770 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf2e407e2011-05-20 19:04:14 +1000771
772#define M_CP_STROPT(x) do { \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000773 if (options.x != NULL) { \
774 if ((r = sshbuf_put_cstring(m, options.x)) != 0) \
775 fatal("%s: buffer error: %s", \
776 __func__, ssh_err(r)); \
777 } \
Damien Millerf2e407e2011-05-20 19:04:14 +1000778 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000779#define M_CP_STRARRAYOPT(x, nx) do { \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000780 for (i = 0; i < options.nx; i++) { \
781 if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
782 fatal("%s: buffer error: %s", \
783 __func__, ssh_err(r)); \
784 } \
Damien Millerd8478b62011-05-29 21:39:36 +1000785 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000786 /* See comment in servconf.h */
787 COPY_MATCH_STRING_OPTS();
788#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000789#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100790
791 /* Create valid auth method lists */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000792 if (auth2_setup_methods_lists(authctxt) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100793 /*
794 * The monitor will continue long enough to let the child
795 * run to it's packet_disconnect(), but it must not allow any
796 * authentication to succeed.
797 */
798 debug("%s: no valid authentication method lists", __func__);
799 }
800
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000801 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000802 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000803
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000804 /* Allow service/style information on the auth context */
805 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
806 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
807
Damien Millera33501b2002-05-08 12:24:42 +1000808#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000809 if (options.use_pam)
810 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000811#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000812
813 return (0);
814}
815
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000816int mm_answer_auth2_read_banner(int sock, struct sshbuf *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000817{
818 char *banner;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000819 int r;
Damien Miller5ad9fd92002-05-13 11:07:41 +1000820
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000821 sshbuf_reset(m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000822 banner = auth2_read_banner();
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000823 if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
824 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000825 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000826 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000827
828 return (0);
829}
830
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000831int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000832mm_answer_authserv(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000833{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000834 int r;
835
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000836 monitor_permit_authentications(1);
837
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000838 if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
839 (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
840 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000841 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000842 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000843
844 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000845 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000846 authctxt->style = NULL;
847 }
848
849 return (0);
850}
851
djm@openbsd.orge76135e2018-11-16 02:43:56 +0000852/*
853 * Check that the key type appears in the supplied pattern list, ignoring
854 * mismatches in the signature algorithm. (Signature algorithm checks are
855 * performed in the unprivileged authentication code).
856 * Returns 1 on success, 0 otherwise.
857 */
858static int
859key_base_type_match(const char *method, const struct sshkey *key,
860 const char *list)
861{
862 char *s, *l, *ol = xstrdup(list);
863 int found = 0;
864
865 l = ol;
866 for ((s = strsep(&l, ",")); s && *s != '\0'; (s = strsep(&l, ","))) {
867 if (sshkey_type_from_name(s) == key->type) {
868 found = 1;
869 break;
870 }
871 }
872 if (!found) {
873 error("%s key type %s is not in permitted list %s", method,
874 sshkey_ssh_name(key), list);
875 }
876
877 free(ol);
878 return found;
879}
880
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000881int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000882mm_answer_authpassword(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000883{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000884 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000885 static int call_count;
886 char *passwd;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000887 int r, authenticated;
888 size_t plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000889
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000890 if (!options.password_authentication)
891 fatal("%s: password authentication not enabled", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000892 if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
893 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000894 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000895 authenticated = options.password_authentication &&
djm@openbsd.org7c856852018-03-03 03:15:51 +0000896 auth_password(ssh, passwd);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000897 explicit_bzero(passwd, plen);
Darren Tuckera627d422013-06-02 07:31:17 +1000898 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000899
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000900 sshbuf_reset(m);
901 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
902 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker01558b72016-07-18 09:33:25 +1000903#ifdef USE_PAM
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000904 if ((r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0)
905 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker01558b72016-07-18 09:33:25 +1000906#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000907
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000908 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000909 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000910
911 call_count++;
912 if (plen == 0 && call_count == 1)
913 auth_method = "none";
914 else
915 auth_method = "password";
916
917 /* Causes monitor loop to terminate if authenticated */
918 return (authenticated);
919}
920
921#ifdef BSD_AUTH
922int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000923mm_answer_bsdauthquery(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000924{
925 char *name, *infotxt;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000926 u_int numprompts, *echo_on, success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000927 char **prompts;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000928 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000929
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000930 if (!options.kbd_interactive_authentication)
931 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb7df3af2003-02-24 11:55:46 +1100932 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
933 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000934
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000935 sshbuf_reset(m);
936 if ((r = sshbuf_put_u32(m, success)) != 0)
937 fatal("%s: buffer error: %s", __func__, ssh_err(r));
938 if (success) {
939 if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
940 fatal("%s: buffer error: %s", __func__, ssh_err(r));
941 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000942
Damien Millerb7df3af2003-02-24 11:55:46 +1100943 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000944 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000945
Damien Millerb7df3af2003-02-24 11:55:46 +1100946 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000947 free(name);
948 free(infotxt);
949 free(prompts);
950 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000951 }
952
953 return (0);
954}
955
956int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000957mm_answer_bsdauthrespond(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000958{
959 char *response;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000960 int r, authok;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000962 if (!options.kbd_interactive_authentication)
963 fatal("%s: kbd-int authentication not enabled", __func__);
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +0000964 if (authctxt->as == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000965 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000966
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000967 if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
968 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000969 authok = options.challenge_response_authentication &&
970 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000971 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000972 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000973 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000974
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000975 sshbuf_reset(m);
976 if ((r = sshbuf_put_u32(m, authok)) != 0)
977 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000978
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000979 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000980 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000981
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000982 auth_method = "keyboard-interactive";
983 auth_submethod = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000984
985 return (authok != 0);
986}
987#endif
988
Damien Miller79418552002-04-23 20:28:48 +1000989#ifdef USE_PAM
990int
Damien Miller120a1ec2018-07-10 19:39:52 +1000991mm_answer_pam_start(int sock, struct sshbuf *m)
Damien Miller79418552002-04-23 20:28:48 +1000992{
Damien Miller4e448a32003-05-14 15:11:48 +1000993 if (!options.use_pam)
994 fatal("UsePAM not set, but ended up in %s anyway", __func__);
995
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100996 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000997
Damien Miller1f499fd2003-08-25 13:08:49 +1000998 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
Damien Miller775f8a22016-08-31 10:48:07 +1000999 if (options.kbd_interactive_authentication)
1000 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller1f499fd2003-08-25 13:08:49 +10001001
Damien Miller79418552002-04-23 20:28:48 +10001002 return (0);
1003}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001004
Damien Miller1f499fd2003-08-25 13:08:49 +10001005int
Damien Miller120a1ec2018-07-10 19:39:52 +10001006mm_answer_pam_account(int sock, struct sshbuf *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001007{
1008 u_int ret;
Damien Miller120a1ec2018-07-10 19:39:52 +10001009 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +11001010
Damien Miller1f499fd2003-08-25 13:08:49 +10001011 if (!options.use_pam)
Damien Miller775f8a22016-08-31 10:48:07 +10001012 fatal("%s: PAM not enabled", __func__);
Damien Miller1f499fd2003-08-25 13:08:49 +10001013
1014 ret = do_pam_account();
1015
Damien Miller120a1ec2018-07-10 19:39:52 +10001016 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1017 (r = sshbuf_put_stringb(m, loginmsg)) != 0)
1018 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller1f499fd2003-08-25 13:08:49 +10001019
Darren Tucker7eda5922004-06-22 13:22:50 +10001020 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001021
1022 return (ret);
1023}
1024
Damien Miller4f9f42a2003-05-10 19:28:02 +10001025static void *sshpam_ctxt, *sshpam_authok;
1026extern KbdintDevice sshpam_device;
1027
1028int
Damien Miller120a1ec2018-07-10 19:39:52 +10001029mm_answer_pam_init_ctx(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001030{
Damien Miller120a1ec2018-07-10 19:39:52 +10001031 u_int ok = 0;
1032 int r;
1033
Damien Miller4f9f42a2003-05-10 19:28:02 +10001034 debug3("%s", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001035 if (!options.kbd_interactive_authentication)
1036 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001037 if (sshpam_ctxt != NULL)
1038 fatal("%s: already called", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001039 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1040 sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001041 sshbuf_reset(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001042 if (sshpam_ctxt != NULL) {
1043 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
Damien Millerb38b95f2016-08-29 11:47:07 +10001044 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
Damien Miller120a1ec2018-07-10 19:39:52 +10001045 ok = 1;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001046 }
Damien Miller120a1ec2018-07-10 19:39:52 +10001047 if ((r = sshbuf_put_u32(m, ok)) != 0)
1048 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker7eda5922004-06-22 13:22:50 +10001049 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001050 return (0);
1051}
1052
1053int
Damien Miller120a1ec2018-07-10 19:39:52 +10001054mm_answer_pam_query(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001055{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001056 char *name = NULL, *info = NULL, **prompts = NULL;
1057 u_int i, num = 0, *echo_on = 0;
Damien Miller120a1ec2018-07-10 19:39:52 +10001058 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001059
1060 debug3("%s", __func__);
1061 sshpam_authok = NULL;
Damien Millerb38b95f2016-08-29 11:47:07 +10001062 if (sshpam_ctxt == NULL)
1063 fatal("%s: no context", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001064 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1065 &num, &prompts, &echo_on);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001066 if (ret == 0 && num == 0)
1067 sshpam_authok = sshpam_ctxt;
1068 if (num > 1 || name == NULL || info == NULL)
Damien Millerb38b95f2016-08-29 11:47:07 +10001069 fatal("sshpam_device.query failed");
1070 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
Damien Miller120a1ec2018-07-10 19:39:52 +10001071 sshbuf_reset(m);
1072 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1073 (r = sshbuf_put_cstring(m, name)) != 0 ||
1074 (r = sshbuf_put_cstring(m, info)) != 0 ||
1075 (r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0 ||
1076 (r = sshbuf_put_u32(m, num)) != 0)
1077 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +10001078 free(name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001079 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001080 for (i = 0; i < num; ++i) {
Damien Miller120a1ec2018-07-10 19:39:52 +10001081 if ((r = sshbuf_put_cstring(m, prompts[i])) != 0 ||
1082 (r = sshbuf_put_u32(m, echo_on[i])) != 0)
1083 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +10001084 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001085 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001086 free(prompts);
1087 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001088 auth_method = "keyboard-interactive";
1089 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001090 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001091 return (0);
1092}
1093
1094int
Damien Miller120a1ec2018-07-10 19:39:52 +10001095mm_answer_pam_respond(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001096{
1097 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001098 u_int i, num;
Damien Miller120a1ec2018-07-10 19:39:52 +10001099 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001100
1101 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001102 if (sshpam_ctxt == NULL)
1103 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001104 sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001105 if ((r = sshbuf_get_u32(m, &num)) != 0)
1106 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001107 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001108 resp = xcalloc(num, sizeof(char *));
Damien Miller120a1ec2018-07-10 19:39:52 +10001109 for (i = 0; i < num; ++i) {
1110 if ((r = sshbuf_get_cstring(m, &(resp[i]), NULL)) != 0)
1111 fatal("%s: buffer error: %s",
1112 __func__, ssh_err(r));
1113 }
Damien Miller4f9f42a2003-05-10 19:28:02 +10001114 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1115 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001116 free(resp[i]);
1117 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001118 } else {
1119 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1120 }
Damien Miller120a1ec2018-07-10 19:39:52 +10001121 sshbuf_reset(m);
1122 if ((r = sshbuf_put_u32(m, ret)) != 0)
1123 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker7eda5922004-06-22 13:22:50 +10001124 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001125 auth_method = "keyboard-interactive";
1126 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001127 if (ret == 0)
1128 sshpam_authok = sshpam_ctxt;
1129 return (0);
1130}
1131
1132int
Damien Miller120a1ec2018-07-10 19:39:52 +10001133mm_answer_pam_free_ctx(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001134{
Damien Miller5e75f512015-08-11 13:34:12 +10001135 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001136
1137 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001138 if (sshpam_ctxt == NULL)
1139 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001140 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001141 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001142 sshbuf_reset(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001143 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Millerb38b95f2016-08-29 11:47:07 +10001144 /* Allow another attempt */
1145 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller15b05cf2012-12-03 09:53:20 +11001146 auth_method = "keyboard-interactive";
1147 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001148 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001149}
Damien Miller79418552002-04-23 20:28:48 +10001150#endif
1151
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001152int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001153mm_answer_keyallowed(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001154{
djm@openbsd.org7c856852018-03-03 03:15:51 +00001155 struct ssh *ssh = active_state; /* XXX */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001156 struct sshkey *key = NULL;
Damien Millera10f5612002-09-12 09:49:15 +10001157 char *cuser, *chost;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001158 u_int pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001159 enum mm_keytype type = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001160 int r, allowed = 0;
1161 struct sshauthopt *opts = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001162
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001163 debug3("%s entering", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001164 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1165 (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
1166 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
1167 (r = sshkey_froms(m, &key)) != 0 ||
1168 (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
1169 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001170
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001171 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001172
Damien Miller3e3b5142003-11-17 21:13:40 +11001173 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001174 /* These should not make it past the privsep child */
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001175 if (sshkey_type_plain(key->type) == KEY_RSA &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001176 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1177 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1178
Darren Tucker47eede72005-03-14 23:08:12 +11001179 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001180 case MM_USERKEY:
Damien Miller7a8f5b32006-03-31 23:14:23 +11001181 auth_method = "publickey";
djm@openbsd.org7c856852018-03-03 03:15:51 +00001182 if (!options.pubkey_authentication)
1183 break;
1184 if (auth2_key_already_used(authctxt, key))
1185 break;
djm@openbsd.orge76135e2018-11-16 02:43:56 +00001186 if (!key_base_type_match(auth_method, key,
1187 options.pubkey_key_types))
djm@openbsd.org7c856852018-03-03 03:15:51 +00001188 break;
1189 allowed = user_key_allowed(ssh, authctxt->pw, key,
1190 pubkey_auth_attempt, &opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001191 break;
1192 case MM_HOSTKEY:
djm@openbsd.org7c856852018-03-03 03:15:51 +00001193 auth_method = "hostbased";
1194 if (!options.hostbased_authentication)
1195 break;
1196 if (auth2_key_already_used(authctxt, key))
1197 break;
djm@openbsd.orge76135e2018-11-16 02:43:56 +00001198 if (!key_base_type_match(auth_method, key,
1199 options.hostbased_key_types))
djm@openbsd.org7c856852018-03-03 03:15:51 +00001200 break;
1201 allowed = hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202 cuser, chost, key);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001203 auth2_record_info(authctxt,
Damien Miller20bdcd72013-07-18 16:10:09 +10001204 "client user \"%.100s\", client host \"%.100s\"",
1205 cuser, chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001206 break;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001207 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001208 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001209 break;
1210 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001211 }
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001212
djm@openbsd.org7c856852018-03-03 03:15:51 +00001213 debug3("%s: %s authentication%s: %s key is %s", __func__,
1214 auth_method, pubkey_auth_attempt ? "" : " test",
1215 (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
1216 allowed ? "allowed" : "not allowed");
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001217
djm@openbsd.org8f574952017-06-24 06:34:38 +00001218 auth2_record_key(authctxt, 0, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001219
1220 /* clear temporarily storage (used by verify) */
1221 monitor_reset_key_state();
1222
1223 if (allowed) {
1224 /* Save temporarily for comparison in verify */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001225 if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
1226 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001227 key_blobtype = type;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001228 key_opts = opts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001229 hostbased_cuser = cuser;
1230 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001231 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001232 /* Log failed attempt */
djm@openbsd.org3a00a922019-01-19 21:41:18 +00001233 auth_log(ssh, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001234 free(cuser);
1235 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001236 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001237 sshkey_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001238
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001239 sshbuf_reset(m);
1240 if ((r = sshbuf_put_u32(m, allowed)) != 0)
1241 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org7c856852018-03-03 03:15:51 +00001242 if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
1243 fatal("%s: sshauthopt_serialise: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001244 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001245
djm@openbsd.org7c856852018-03-03 03:15:51 +00001246 if (!allowed)
1247 sshauthopt_free(opts);
1248
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001249 return (0);
1250}
1251
1252static int
1253monitor_valid_userblob(u_char *data, u_int datalen)
1254{
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001255 struct sshbuf *b;
1256 const u_char *p;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001257 char *userstyle, *cp;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001258 size_t len;
1259 u_char type;
1260 int r, fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001261
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001262 if ((b = sshbuf_new()) == NULL)
1263 fatal("%s: sshbuf_new", __func__);
1264 if ((r = sshbuf_put(b, data, datalen)) != 0)
1265 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001266
1267 if (datafellows & SSH_OLD_SESSIONID) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001268 p = sshbuf_ptr(b);
1269 len = sshbuf_len(b);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001270 if ((session_id2 == NULL) ||
1271 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001272 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001273 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001274 if ((r = sshbuf_consume(b, session_id2_len)) != 0)
1275 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001276 } else {
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001277 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1278 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromf67e0772002-06-06 20:58:19 +00001279 if ((session_id2 == NULL) ||
1280 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001281 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001282 fail++;
1283 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001284 if ((r = sshbuf_get_u8(b, &type)) != 0)
1285 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1286 if (type != SSH2_MSG_USERAUTH_REQUEST)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001287 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001288 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1289 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4ce189d2013-04-23 15:17:52 +10001290 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1291 authctxt->style ? ":" : "",
1292 authctxt->style ? authctxt->style : "");
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001293 if (strcmp(userstyle, cp) != 0) {
1294 logit("wrong user name passed to monitor: "
1295 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001296 fail++;
1297 }
Darren Tuckera627d422013-06-02 07:31:17 +10001298 free(userstyle);
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001299 free(cp);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001300 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1301 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1302 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001303 if (strcmp("publickey", cp) != 0)
1304 fail++;
1305 free(cp);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001306 if ((r = sshbuf_get_u8(b, &type)) != 0)
1307 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1308 if (type == 0)
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001309 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001310 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1311 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1312 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1313 if (sshbuf_len(b) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001314 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001315 sshbuf_free(b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001316 return (fail == 0);
1317}
1318
1319static int
Damien Millera10f5612002-09-12 09:49:15 +10001320monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1321 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001322{
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001323 struct sshbuf *b;
1324 const u_char *p;
1325 char *cp, *userstyle;
1326 size_t len;
1327 int r, fail = 0;
1328 u_char type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001329
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001330 if ((b = sshbuf_new()) == NULL)
1331 fatal("%s: sshbuf_new", __func__);
1332 if ((r = sshbuf_put(b, data, datalen)) != 0 ||
1333 (r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1334 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001335
Ben Lindstromf67e0772002-06-06 20:58:19 +00001336 if ((session_id2 == NULL) ||
1337 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001338 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001339 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001340
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001341 if ((r = sshbuf_get_u8(b, &type)) != 0)
1342 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1343 if (type != SSH2_MSG_USERAUTH_REQUEST)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001344 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001345 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1346 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4ce189d2013-04-23 15:17:52 +10001347 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1348 authctxt->style ? ":" : "",
1349 authctxt->style ? authctxt->style : "");
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001350 if (strcmp(userstyle, cp) != 0) {
1351 logit("wrong user name passed to monitor: "
1352 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001353 fail++;
1354 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001355 free(userstyle);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001356 free(cp);
1357 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1358 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1359 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1360 if (strcmp(cp, "hostbased") != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001361 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001362 free(cp);
1363 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1364 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1365 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001366
1367 /* verify client host, strip trailing dot if necessary */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001368 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1369 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1370 if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
1371 cp[len - 1] = '\0';
1372 if (strcmp(cp, chost) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001373 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001374 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001375
1376 /* verify client user */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001377 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1378 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1379 if (strcmp(cp, cuser) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001380 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001381 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001382
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001383 if (sshbuf_len(b) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001384 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001385 sshbuf_free(b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001386 return (fail == 0);
1387}
1388
1389int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001390mm_answer_keyverify(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001391{
djm@openbsd.org7c856852018-03-03 03:15:51 +00001392 struct ssh *ssh = active_state; /* XXX */
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001393 struct sshkey *key;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001394 u_char *signature, *data, *blob;
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001395 char *sigalg;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001396 size_t signaturelen, datalen, bloblen;
1397 int r, ret, valid_data = 0, encoded_ret;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001398
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001399 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1400 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001401 (r = sshbuf_get_string(m, &data, &datalen)) != 0 ||
1402 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001403 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001404
1405 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001406 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001407 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001408
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001409 /* Empty signature algorithm means NULL. */
1410 if (*sigalg == '\0') {
1411 free(sigalg);
1412 sigalg = NULL;
1413 }
1414
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001415 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1416 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1417 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001418
1419 switch (key_blobtype) {
1420 case MM_USERKEY:
1421 valid_data = monitor_valid_userblob(data, datalen);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001422 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001423 break;
1424 case MM_HOSTKEY:
1425 valid_data = monitor_valid_hostbasedblob(data, datalen,
1426 hostbased_cuser, hostbased_chost);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001427 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428 break;
1429 default:
1430 valid_data = 0;
1431 break;
1432 }
1433 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001434 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001435
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001436 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001437 sigalg, active_state->compat);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001438 debug3("%s: %s %p signature %s", __func__, auth_method, key,
1439 (ret == 0) ? "verified" : "unverified");
1440 auth2_record_key(authctxt, ret == 0, key);
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001441
Darren Tuckera627d422013-06-02 07:31:17 +10001442 free(blob);
1443 free(signature);
1444 free(data);
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001445 free(sigalg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001446
djm@openbsd.org7c856852018-03-03 03:15:51 +00001447 if (key_blobtype == MM_USERKEY)
1448 auth_activate_options(ssh, key_opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001449 monitor_reset_key_state();
1450
djm@openbsd.org8f574952017-06-24 06:34:38 +00001451 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001452 sshbuf_reset(m);
1453
1454 /* encode ret != 0 as positive integer, since we're sending u32 */
1455 encoded_ret = (ret != 0);
1456 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1457 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001458 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001459
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001460 return ret == 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001461}
1462
1463static void
1464mm_record_login(Session *s, struct passwd *pw)
1465{
djm@openbsd.org95767262016-03-07 19:02:43 +00001466 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001467 socklen_t fromlen;
1468 struct sockaddr_storage from;
1469
1470 /*
1471 * Get IP address of client. If the connection is not a socket, let
1472 * the address be 0.0.0.0.
1473 */
1474 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001475 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001476 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001477 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001478 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001479 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001480 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001481 }
1482 }
1483 /* Record that there was a login on that tty from the remote host. */
1484 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +00001485 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001486 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001487}
1488
1489static void
1490mm_session_close(Session *s)
1491{
Damien Miller04bd8b02003-05-25 14:38:33 +10001492 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001493 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001494 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001495 session_pty_cleanup2(s);
1496 }
Damien Miller7207f642008-05-19 15:34:50 +10001497 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001498}
1499
1500int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001501mm_answer_pty(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001502{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001503 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001504 Session *s;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001505 int r, res, fd0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001506
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001507 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001508
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001509 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001510 s = session_new();
1511 if (s == NULL)
1512 goto error;
1513 s->authctxt = authctxt;
1514 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001515 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001516 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1517 if (res == 0)
1518 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001519 pty_setowner(authctxt->pw, s->tty);
1520
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001521 if ((r = sshbuf_put_u32(m, 1)) != 0 ||
1522 (r = sshbuf_put_cstring(m, s->tty)) != 0)
1523 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001524
1525 /* We need to trick ttyslot */
1526 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001527 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001528
1529 mm_record_login(s, authctxt->pw);
1530
1531 /* Now we can close the file descriptor again */
1532 close(0);
1533
Darren Tucker09991742004-07-17 17:05:14 +10001534 /* send messages generated by record_login */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001535 if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
1536 fatal("%s: put login message: %s", __func__, ssh_err(r));
1537 sshbuf_reset(loginmsg);
Darren Tucker09991742004-07-17 17:05:14 +10001538
1539 mm_request_send(sock, MONITOR_ANS_PTY, m);
1540
Damien Miller54fd7cf2007-09-17 12:04:08 +10001541 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1542 mm_send_fd(sock, s->ttyfd) == -1)
1543 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001544
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001545 /* make sure nothing uses fd 0 */
1546 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001547 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001548 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001549 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001550
1551 /* slave is not needed */
1552 close(s->ttyfd);
1553 s->ttyfd = s->ptyfd;
1554 /* no need to dup() because nobody closes ptyfd */
1555 s->ptymaster = s->ptyfd;
1556
Damien Miller9ab00b42006-08-05 12:40:11 +10001557 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001558
1559 return (0);
1560
1561 error:
1562 if (s != NULL)
1563 mm_session_close(s);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001564 if ((r = sshbuf_put_u32(m, 0)) != 0)
1565 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001566 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001567 return (0);
1568}
1569
1570int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001571mm_answer_pty_cleanup(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001572{
1573 Session *s;
1574 char *tty;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001575 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001576
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001577 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001578
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001579 if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
1580 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001581 if ((s = session_by_tty(tty)) != NULL)
1582 mm_session_close(s);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001583 sshbuf_reset(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001584 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001585 return (0);
1586}
1587
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001588int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001589mm_answer_term(int sock, struct sshbuf *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001590{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001591 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001592 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001593 int res, status;
1594
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001595 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001596
1597 /* The child is terminating */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001598 session_destroy_all(ssh, &mm_session_close);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001599
Darren Tucker52358d62008-03-11 22:58:25 +11001600#ifdef USE_PAM
1601 if (options.use_pam)
1602 sshpam_cleanup();
1603#endif
1604
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001605 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001606 if (errno != EINTR)
1607 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001608
1609 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1610
1611 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001612 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001613}
1614
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001615#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001616/* Report that an audit event occurred */
1617int
Damien Miller120a1ec2018-07-10 19:39:52 +10001618mm_answer_audit_event(int socket, struct sshbuf *m)
Darren Tucker269a1ea2005-02-03 00:20:53 +11001619{
Damien Miller120a1ec2018-07-10 19:39:52 +10001620 u_int n;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001621 ssh_audit_event_t event;
Damien Miller120a1ec2018-07-10 19:39:52 +10001622 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001623
1624 debug3("%s entering", __func__);
1625
Damien Miller120a1ec2018-07-10 19:39:52 +10001626 if ((r = sshbuf_get_u32(m, &n)) != 0)
1627 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1628 event = (ssh_audit_event_t)n;
1629 switch (event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001630 case SSH_AUTH_FAIL_PUBKEY:
1631 case SSH_AUTH_FAIL_HOSTBASED:
1632 case SSH_AUTH_FAIL_GSSAPI:
1633 case SSH_LOGIN_EXCEED_MAXTRIES:
1634 case SSH_LOGIN_ROOT_DENIED:
1635 case SSH_CONNECTION_CLOSE:
1636 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001637 audit_event(event);
1638 break;
1639 default:
1640 fatal("Audit event type %d not permitted", event);
1641 }
1642
1643 return (0);
1644}
1645
1646int
Damien Miller120a1ec2018-07-10 19:39:52 +10001647mm_answer_audit_command(int socket, struct sshbuf *m)
Darren Tucker269a1ea2005-02-03 00:20:53 +11001648{
Darren Tucker269a1ea2005-02-03 00:20:53 +11001649 char *cmd;
Damien Miller120a1ec2018-07-10 19:39:52 +10001650 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001651
1652 debug3("%s entering", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +10001653 if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
1654 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker269a1ea2005-02-03 00:20:53 +11001655 /* sanity check command, if so how? */
1656 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001657 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001658 return (0);
1659}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001660#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001661
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001662void
markus@openbsd.org1e0cdf82017-05-31 08:09:45 +00001663monitor_clear_keystate(struct monitor *pmonitor)
1664{
1665 struct ssh *ssh = active_state; /* XXX */
1666
1667 ssh_clear_newkeys(ssh, MODE_IN);
1668 ssh_clear_newkeys(ssh, MODE_OUT);
1669 sshbuf_free(child_state);
1670 child_state = NULL;
1671}
1672
1673void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001674monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001675{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001676 struct ssh *ssh = active_state; /* XXX */
1677 struct kex *kex;
1678 int r;
1679
1680 debug3("%s: packet_set_state", __func__);
1681 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1682 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1683 sshbuf_free(child_state);
1684 child_state = NULL;
1685
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +00001686 if ((kex = ssh->kex) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001687 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001688#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001689 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1690 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +00001691 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1692 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1693 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001694 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1695 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001696# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001697 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001698# endif
Damien Miller773dda22015-01-30 23:10:17 +11001699#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001700 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1701 kex->load_host_public_key=&get_hostkey_public_by_type;
1702 kex->load_host_private_key=&get_hostkey_private_by_type;
1703 kex->host_key_index=&get_hostkey_index;
1704 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001705 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001706}
1707
1708/* This function requries careful sanity checking */
1709
1710void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001711mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001712{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001713 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001714
markus@openbsd.org091c3022015-01-19 19:52:16 +00001715 if ((child_state = sshbuf_new()) == NULL)
1716 fatal("%s: sshbuf_new failed", __func__);
1717 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1718 child_state);
1719 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001720}
1721
1722
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001723/* XXX */
1724
1725#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001726 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001727 fatal("fcntl(%d, F_SETFD)", x); \
1728} while (0)
1729
1730static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001731monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001732{
Damien Miller8f0bf232011-06-20 14:42:23 +10001733 int pair[2];
markus@openbsd.org84008602017-05-31 10:04:29 +00001734#ifdef SO_ZEROIZE
1735 int on = 1;
1736#endif
Damien Miller8f0bf232011-06-20 14:42:23 +10001737
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001738 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001739 fatal("%s: socketpair: %s", __func__, strerror(errno));
markus@openbsd.org84008602017-05-31 10:04:29 +00001740#ifdef SO_ZEROIZE
1741 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1742 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1743 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1744 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1745#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001746 FD_CLOSEONEXEC(pair[0]);
1747 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001748 mon->m_recvfd = pair[0];
1749 mon->m_sendfd = pair[1];
1750
1751 if (do_logfds) {
1752 if (pipe(pair) == -1)
1753 fatal("%s: pipe: %s", __func__, strerror(errno));
1754 FD_CLOSEONEXEC(pair[0]);
1755 FD_CLOSEONEXEC(pair[1]);
1756 mon->m_log_recvfd = pair[0];
1757 mon->m_log_sendfd = pair[1];
1758 } else
1759 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001760}
1761
1762#define MM_MEMSIZE 65536
1763
1764struct monitor *
1765monitor_init(void)
1766{
1767 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001768
Damien Miller07d86be2006-03-26 14:19:21 +11001769 mon = xcalloc(1, sizeof(*mon));
Damien Miller8f0bf232011-06-20 14:42:23 +10001770 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001771
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001772 return mon;
1773}
1774
1775void
1776monitor_reinit(struct monitor *mon)
1777{
Damien Miller8f0bf232011-06-20 14:42:23 +10001778 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001779}
Darren Tucker0efd1552003-08-26 11:49:55 +10001780
1781#ifdef GSSAPI
1782int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001783mm_answer_gss_setup_ctx(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001784{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001785 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001786 OM_uint32 major;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001787 size_t len;
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001788 u_char *p;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001789 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +10001790
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001791 if (!options.gss_authentication)
1792 fatal("%s: GSSAPI authentication not enabled", __func__);
1793
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001794 if ((r = sshbuf_get_string(m, &p, &len)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001795 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001796 goid.elements = p;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001797 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001798
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001799 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001800
Darren Tuckera627d422013-06-02 07:31:17 +10001801 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001802
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001803 sshbuf_reset(m);
1804 if ((r = sshbuf_put_u32(m, major)) != 0)
1805 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001806
Damien Miller6fd6def2005-11-05 15:07:05 +11001807 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001808
1809 /* Now we have a context, enable the step */
1810 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1811
1812 return (0);
1813}
1814
1815int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001816mm_answer_gss_accept_ctx(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001817{
1818 gss_buffer_desc in;
1819 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001820 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001821 OM_uint32 flags = 0; /* GSI needs this */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001822 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +10001823
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001824 if (!options.gss_authentication)
1825 fatal("%s: GSSAPI authentication not enabled", __func__);
1826
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001827 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001828 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001829 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001830 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001831
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001832 sshbuf_reset(m);
1833 if ((r = sshbuf_put_u32(m, major)) != 0 ||
1834 (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
1835 (r = sshbuf_put_u32(m, flags)) != 0)
1836 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001837 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001838
1839 gss_release_buffer(&minor, &out);
1840
Damien Miller6fd6def2005-11-05 15:07:05 +11001841 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001842 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1843 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001844 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001845 }
1846 return (0);
1847}
1848
1849int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001850mm_answer_gss_checkmic(int sock, struct sshbuf *m)
Damien Miller0425d402003-11-17 22:18:21 +11001851{
1852 gss_buffer_desc gssbuf, mic;
1853 OM_uint32 ret;
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001854 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +11001855
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001856 if (!options.gss_authentication)
1857 fatal("%s: GSSAPI authentication not enabled", __func__);
1858
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001859 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
1860 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001861 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller787b2ec2003-11-21 23:56:47 +11001862
Damien Miller0425d402003-11-17 22:18:21 +11001863 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001864
Darren Tuckera627d422013-06-02 07:31:17 +10001865 free(gssbuf.value);
1866 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001867
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001868 sshbuf_reset(m);
1869 if ((r = sshbuf_put_u32(m, ret)) != 0)
1870 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller787b2ec2003-11-21 23:56:47 +11001871
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001872 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001873
Damien Miller0425d402003-11-17 22:18:21 +11001874 if (!GSS_ERROR(ret))
1875 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001876
Damien Miller0425d402003-11-17 22:18:21 +11001877 return (0);
1878}
1879
1880int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001881mm_answer_gss_userok(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001882{
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001883 int r, authenticated;
djm@openbsd.org8f574952017-06-24 06:34:38 +00001884 const char *displayname;
Darren Tucker0efd1552003-08-26 11:49:55 +10001885
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001886 if (!options.gss_authentication)
1887 fatal("%s: GSSAPI authentication not enabled", __func__);
1888
Darren Tucker0efd1552003-08-26 11:49:55 +10001889 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1890
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001891 sshbuf_reset(m);
1892 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1893 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001894
1895 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001896 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001897
Damien Miller6fd6def2005-11-05 15:07:05 +11001898 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001899
djm@openbsd.org8f574952017-06-24 06:34:38 +00001900 if ((displayname = ssh_gssapi_displayname()) != NULL)
1901 auth2_record_info(authctxt, "%s", displayname);
1902
Darren Tucker0efd1552003-08-26 11:49:55 +10001903 /* Monitor loop will terminate if authenticated */
1904 return (authenticated);
1905}
1906#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001907