blob: 8662d13e039c61e3e8d64e01bae65021cd940f4a [file] [log] [blame]
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001/* $OpenBSD: monitor.c,v 1.184 2018/07/10 09:13:30 djm Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100031#include <sys/socket.h>
Damien Millerd7834352006-08-05 12:39:39 +100032#include "openbsd-compat/sys-tree.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110033#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000034
Darren Tucker39972492006-07-12 22:22:46 +100035#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100036#include <fcntl.h>
djm@openbsd.org1a31d022016-05-02 08:49:03 +000037#include <limits.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110038#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110039#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110040#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100041#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110042#include <signal.h>
Damien Miller9af21972015-02-24 09:04:32 +110043#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000044#include <stdint.h>
Damien Miller9af21972015-02-24 09:04:32 +110045#endif
Damien Millere7a1e5c2006-08-05 11:34:19 +100046#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100047#include <string.h>
Damien Miller86687062014-07-02 15:28:02 +100048#include <stdarg.h>
49#include <stdio.h>
Damien Miller607aede2006-09-01 15:48:19 +100050#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100051#ifdef HAVE_POLL_H
52#include <poll.h>
53#else
54# ifdef HAVE_SYS_POLL_H
55# include <sys/poll.h>
56# endif
57#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000058
59#ifdef SKEY
60#include <skey.h>
61#endif
62
Damien Miller1f0311c2014-05-15 14:24:09 +100063#ifdef WITH_OPENSSL
Damien Miller03e20032006-03-15 11:16:59 +110064#include <openssl/dh.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100065#endif
Damien Miller03e20032006-03-15 11:16:59 +110066
Damien Millerb84886b2008-05-19 15:05:07 +100067#include "openbsd-compat/sys-queue.h"
Damien Miller8f0bf232011-06-20 14:42:23 +100068#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100069#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#include "key.h"
markus@openbsd.org235c7c42018-07-09 21:53:45 +000072#include "sshbuf.h"
Damien Millerd7834352006-08-05 12:39:39 +100073#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000074#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100075#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000076#include "kex.h"
77#include "dh.h"
Darren Tucker01558b72016-07-18 09:33:25 +100078#include "auth-pam.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000079#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
80#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000081#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000082#define TARGET_OS_MAC 1
83#else
84#include "zlib.h"
85#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000086#include "packet.h"
87#include "auth-options.h"
88#include "sshpty.h"
89#include "channels.h"
90#include "session.h"
91#include "sshlogin.h"
92#include "canohost.h"
93#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100094#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095#include "servconf.h"
96#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +100097#ifdef GSSAPI
98#include "ssh-gss.h"
99#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000100#include "monitor_wrap.h"
101#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000102#include "compat.h"
103#include "ssh2.h"
Damien Miller85b45e02013-07-20 13:21:52 +1000104#include "authfd.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000105#include "match.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000106#include "ssherr.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107
Darren Tucker0efd1552003-08-26 11:49:55 +1000108#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000109static Gssctxt *gsscontext = NULL;
110#endif
111
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000112/* Imports */
113extern ServerOptions options;
114extern u_int utmp_len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000115extern u_char session_id[];
markus@openbsd.org2808d182018-07-09 21:26:02 +0000116extern struct sshbuf *loginmsg;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000117extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000118
119/* State exported from the child */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000120static struct sshbuf *child_state;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000121
Damien Miller469954d2003-06-18 20:25:33 +1000122/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000123
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000124int mm_answer_moduli(int, struct sshbuf *);
125int mm_answer_sign(int, struct sshbuf *);
126int mm_answer_pwnamallow(int, struct sshbuf *);
127int mm_answer_auth2_read_banner(int, struct sshbuf *);
128int mm_answer_authserv(int, struct sshbuf *);
129int mm_answer_authpassword(int, struct sshbuf *);
130int mm_answer_bsdauthquery(int, struct sshbuf *);
131int mm_answer_bsdauthrespond(int, struct sshbuf *);
132int mm_answer_skeyquery(int, struct sshbuf *);
133int mm_answer_skeyrespond(int, struct sshbuf *);
134int mm_answer_keyallowed(int, struct sshbuf *);
135int mm_answer_keyverify(int, struct sshbuf *);
136int mm_answer_pty(int, struct sshbuf *);
137int mm_answer_pty_cleanup(int, struct sshbuf *);
138int mm_answer_term(int, struct sshbuf *);
139int mm_answer_rsa_keyallowed(int, struct sshbuf *);
140int mm_answer_rsa_challenge(int, struct sshbuf *);
141int mm_answer_rsa_response(int, struct sshbuf *);
142int mm_answer_sesskey(int, struct sshbuf *);
143int mm_answer_sessid(int, struct sshbuf *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000144
Damien Miller79418552002-04-23 20:28:48 +1000145#ifdef USE_PAM
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000146int mm_answer_pam_start(int, struct sshbuf *);
147int mm_answer_pam_account(int, struct sshbuf *);
148int mm_answer_pam_init_ctx(int, struct sshbuf *);
149int mm_answer_pam_query(int, struct sshbuf *);
150int mm_answer_pam_respond(int, struct sshbuf *);
151int mm_answer_pam_free_ctx(int, struct sshbuf *);
Damien Miller79418552002-04-23 20:28:48 +1000152#endif
153
Darren Tucker0efd1552003-08-26 11:49:55 +1000154#ifdef GSSAPI
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000155int mm_answer_gss_setup_ctx(int, struct sshbuf *);
156int mm_answer_gss_accept_ctx(int, struct sshbuf *);
157int mm_answer_gss_userok(int, struct sshbuf *);
158int mm_answer_gss_checkmic(int, struct sshbuf *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000159#endif
Damien Miller25162f22002-09-12 09:47:29 +1000160
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100161#ifdef SSH_AUDIT_EVENTS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000162int mm_answer_audit_event(int, struct sshbuf *);
163int mm_answer_audit_command(int, struct sshbuf *);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100164#endif
165
Damien Miller8f0bf232011-06-20 14:42:23 +1000166static int monitor_read_log(struct monitor *);
167
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000168static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000169
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000170/* local state for key verify */
171static u_char *key_blob = NULL;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000172static size_t key_bloblen = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000173static int key_blobtype = MM_NOKEY;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000174static struct sshauthopt *key_opts = NULL;
Damien Millera10f5612002-09-12 09:49:15 +1000175static char *hostbased_cuser = NULL;
176static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000177static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100178static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000179static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000180static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000181static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000182
183struct mon_table {
184 enum monitor_reqtype type;
185 int flags;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000186 int (*f)(int, struct sshbuf *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000187};
188
189#define MON_ISAUTH 0x0004 /* Required for Authentication */
190#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
191#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100192#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000193
194#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
195
196#define MON_PERMIT 0x1000 /* Request is permitted */
197
198struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000199#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000200 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000201#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
203 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
204 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000205 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000207#ifdef USE_PAM
208 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000209 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Millerb38b95f2016-08-29 11:47:07 +1000210 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
211 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
212 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000213 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000214#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100215#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100216 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100217#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000218#ifdef BSD_AUTH
219 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100220 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000221#endif
222#ifdef SKEY
223 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
224 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
225#endif
226 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
227 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000228#ifdef GSSAPI
229 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
djm@openbsd.orgb33ad6d2016-09-05 13:57:31 +0000230 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
231 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
232 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000233#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000234 {0, 0, NULL}
235};
236
237struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000238#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000239 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000240#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000241 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
242 {MONITOR_REQ_PTY, 0, mm_answer_pty},
243 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
244 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100245#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100246 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
247 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
248#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000249 {0, 0, NULL}
250};
251
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000252struct mon_table *mon_dispatch;
253
254/* Specifies if a certain message is allowed at the moment */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000255static void
256monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
257{
258 while (ent->f != NULL) {
259 if (ent->type == type) {
260 ent->flags &= ~MON_PERMIT;
261 ent->flags |= permit ? MON_PERMIT : 0;
262 return;
263 }
264 ent++;
265 }
266}
267
268static void
269monitor_permit_authentications(int permit)
270{
271 struct mon_table *ent = mon_dispatch;
272
273 while (ent->f != NULL) {
274 if (ent->flags & MON_AUTH) {
275 ent->flags &= ~MON_PERMIT;
276 ent->flags |= permit ? MON_PERMIT : 0;
277 }
278 ent++;
279 }
280}
281
Darren Tucker3e33cec2003-10-02 16:12:36 +1000282void
283monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000284{
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000285 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000286 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100287 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000288
289 debug3("preauth child monitor started");
290
tb@openbsd.org34843802018-02-05 05:37:46 +0000291 if (pmonitor->m_recvfd >= 0)
292 close(pmonitor->m_recvfd);
293 if (pmonitor->m_log_sendfd >= 0)
294 close(pmonitor->m_log_sendfd);
Damien Miller8f0bf232011-06-20 14:42:23 +1000295 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
296
Darren Tucker3e33cec2003-10-02 16:12:36 +1000297 authctxt = _authctxt;
298 memset(authctxt, 0, sizeof(*authctxt));
djm@openbsd.org7c856852018-03-03 03:15:51 +0000299 ssh->authctxt = authctxt;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000300
Damien Miller120a1ec2018-07-10 19:39:52 +1000301 authctxt->loginmsg = loginmsg;
Darren Tuckerde0de392005-03-31 23:52:04 +1000302
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000303 mon_dispatch = mon_dispatch_proto20;
304 /* Permit requests for moduli and signatures */
305 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
306 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000307
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000308 /* The first few requests do not require asynchronous access */
309 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100310 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100311 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100312 auth_submethod = NULL;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000313 auth2_authctxt_reset_info(authctxt);
314
Darren Tuckerfbba7352006-11-07 23:16:08 +1100315 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100316
317 /* Special handling for multiple required authentications */
318 if (options.num_auth_methods != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100319 if (authenticated &&
320 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000321 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100322 debug3("%s: method %s: partial", __func__,
323 auth_method);
324 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100325 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100326 }
327 }
328
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000329 if (authenticated) {
330 if (!(ent->flags & MON_AUTHDECIDE))
331 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000332 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000333 if (authctxt->pw->pw_uid == 0 &&
djm@openbsd.org7c856852018-03-03 03:15:51 +0000334 !auth_root_allowed(ssh, auth_method))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000335 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000336#ifdef USE_PAM
337 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100338 if (options.use_pam && authenticated) {
Damien Miller120a1ec2018-07-10 19:39:52 +1000339 struct sshbuf *m;
Damien Miller1f499fd2003-08-25 13:08:49 +1000340
Damien Miller120a1ec2018-07-10 19:39:52 +1000341 if ((m = sshbuf_new()) == NULL)
342 fatal("%s: sshbuf_new failed",
343 __func__);
Damien Millera8e06ce2003-11-21 23:48:55 +1100344 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller120a1ec2018-07-10 19:39:52 +1000345 MONITOR_REQ_PAM_ACCOUNT, m);
346 authenticated = mm_answer_pam_account(
347 pmonitor->m_sendfd, m);
348 sshbuf_free(m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000349 }
350#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000351 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100352 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100353 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000354 auth_method, auth_submethod);
djm@openbsd.org94885382015-06-22 23:42:16 +0000355 if (!partial && !authenticated)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356 authctxt->failures++;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000357 if (authenticated || partial) {
358 auth2_update_session_info(authctxt,
359 auth_method, auth_submethod);
360 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000361 }
362 }
363
364 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000365 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100366 if (strcmp(auth_method, "unknown") == 0)
367 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000368
369 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000370 __func__, authctxt->user);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000371 ssh->authctxt = NULL;
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000372 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000373
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000374 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000375
Damien Miller6a1937e2012-12-12 10:44:38 +1100376 /* Drain any buffered messages from the child */
377 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
378 ;
379
tb@openbsd.org34843802018-02-05 05:37:46 +0000380 if (pmonitor->m_recvfd >= 0)
381 close(pmonitor->m_recvfd);
382 if (pmonitor->m_log_sendfd >= 0)
383 close(pmonitor->m_log_sendfd);
Damien Miller8f0bf232011-06-20 14:42:23 +1000384 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000385}
386
Damien Millerbe64d432003-05-14 19:31:12 +1000387static void
388monitor_set_child_handler(pid_t pid)
389{
390 monitor_child_pid = pid;
391}
392
393static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000394monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000395{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000396 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000397}
398
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000399void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000400monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000401{
Damien Miller8f0bf232011-06-20 14:42:23 +1000402 close(pmonitor->m_recvfd);
403 pmonitor->m_recvfd = -1;
404
Damien Millerbe64d432003-05-14 19:31:12 +1000405 monitor_set_child_handler(pmonitor->m_pid);
406 signal(SIGHUP, &monitor_child_handler);
407 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000408 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000409#ifdef SIGXFSZ
410 signal(SIGXFSZ, SIG_IGN);
411#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000412
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000413 mon_dispatch = mon_dispatch_postauth20;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000414
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000415 /* Permit requests for moduli and signatures */
416 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
417 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
418 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
419
djm@openbsd.org7c856852018-03-03 03:15:51 +0000420 if (auth_opts->permit_pty_flag) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000421 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
422 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
423 }
424
425 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000426 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000427}
428
Damien Miller8f0bf232011-06-20 14:42:23 +1000429static int
430monitor_read_log(struct monitor *pmonitor)
431{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000432 struct sshbuf *logmsg;
Damien Miller8f0bf232011-06-20 14:42:23 +1000433 u_int len, level;
434 char *msg;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000435 u_char *p;
436 int r;
Damien Miller8f0bf232011-06-20 14:42:23 +1000437
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000438 if ((logmsg = sshbuf_new()) == NULL)
439 fatal("%s: sshbuf_new", __func__);
Damien Miller8f0bf232011-06-20 14:42:23 +1000440
441 /* Read length */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000442 if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
443 fatal("%s: reserve: %s", __func__, ssh_err(r));
444 if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
Damien Miller8f0bf232011-06-20 14:42:23 +1000445 if (errno == EPIPE) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000446 sshbuf_free(logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000447 debug("%s: child log fd closed", __func__);
448 close(pmonitor->m_log_recvfd);
449 pmonitor->m_log_recvfd = -1;
450 return -1;
451 }
452 fatal("%s: log fd read: %s", __func__, strerror(errno));
453 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000454 if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
455 fatal("%s: get len: %s", __func__, ssh_err(r));
Damien Miller8f0bf232011-06-20 14:42:23 +1000456 if (len <= 4 || len > 8192)
457 fatal("%s: invalid log message length %u", __func__, len);
458
459 /* Read severity, message */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000460 sshbuf_reset(logmsg);
461 if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
462 fatal("%s: reserve: %s", __func__, ssh_err(r));
463 if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
Damien Miller8f0bf232011-06-20 14:42:23 +1000464 fatal("%s: log fd read: %s", __func__, strerror(errno));
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000465 if ((r = sshbuf_get_u32(logmsg, &level)) != 0 ||
466 (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
467 fatal("%s: decode: %s", __func__, ssh_err(r));
Damien Miller8f0bf232011-06-20 14:42:23 +1000468
469 /* Log it */
Damien Miller8f0bf232011-06-20 14:42:23 +1000470 if (log_level_name(level) == NULL)
471 fatal("%s: invalid log level %u (corrupted message?)",
472 __func__, level);
473 do_log2(level, "%s [preauth]", msg);
474
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000475 sshbuf_free(logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000476 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000477
478 return 0;
479}
480
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000481int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000482monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000483 struct mon_table **pent)
484{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000485 struct sshbuf *m;
486 int r, ret;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000487 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000488 struct pollfd pfd[2];
489
490 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100491 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000492 pfd[0].fd = pmonitor->m_sendfd;
493 pfd[0].events = POLLIN;
494 pfd[1].fd = pmonitor->m_log_recvfd;
495 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000496 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
497 if (errno == EINTR || errno == EAGAIN)
498 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000499 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000500 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000501 if (pfd[1].revents) {
502 /*
503 * Drain all log messages before processing next
504 * monitor request.
505 */
506 monitor_read_log(pmonitor);
507 continue;
508 }
509 if (pfd[0].revents)
510 break; /* Continues below */
511 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000512
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000513 if ((m = sshbuf_new()) == NULL)
514 fatal("%s: sshbuf_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000515
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000516 mm_request_receive(pmonitor->m_sendfd, m);
517 if ((r = sshbuf_get_u8(m, &type)) != 0)
518 fatal("%s: decode: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000519
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000520 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000521
522 while (ent->f != NULL) {
523 if (ent->type == type)
524 break;
525 ent++;
526 }
527
528 if (ent->f != NULL) {
529 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000530 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000531 type);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000532 ret = (*ent->f)(pmonitor->m_sendfd, m);
533 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000534
535 /* The child may use this request only once, disable it */
536 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000537 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000538 type);
539 ent->flags &= ~MON_PERMIT;
540 }
541
542 if (pent != NULL)
543 *pent = ent;
544
545 return ret;
546 }
547
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000548 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000549
550 /* NOTREACHED */
551 return (-1);
552}
553
554/* allowed key state */
555static int
556monitor_allowed_key(u_char *blob, u_int bloblen)
557{
558 /* make sure key is allowed */
559 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000560 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561 return (0);
562 return (1);
563}
564
565static void
566monitor_reset_key_state(void)
567{
568 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000569 free(key_blob);
570 free(hostbased_cuser);
571 free(hostbased_chost);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000572 sshauthopt_free(key_opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000573 key_blob = NULL;
574 key_bloblen = 0;
575 key_blobtype = MM_NOKEY;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000576 key_opts = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577 hostbased_cuser = NULL;
578 hostbased_chost = NULL;
579}
580
Damien Miller1f0311c2014-05-15 14:24:09 +1000581#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000582int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000583mm_answer_moduli(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000584{
585 DH *dh;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000586 int r;
587 u_int min, want, max;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000588
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000589 if ((r = sshbuf_get_u32(m, &min)) != 0 ||
590 (r = sshbuf_get_u32(m, &want)) != 0 ||
591 (r = sshbuf_get_u32(m, &max)) != 0)
592 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000593
594 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000595 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000596 /* We need to check here, too, in case the child got corrupted */
597 if (max < min || want < min || max < want)
598 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000599 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000600
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000601 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000602
603 dh = choose_dh(min, want, max);
604 if (dh == NULL) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000605 if ((r = sshbuf_put_u8(m, 0)) != 0)
606 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000607 return (0);
608 } else {
609 /* Send first bignum */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000610 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
611 (r = sshbuf_put_bignum2(m, dh->p)) != 0 ||
612 (r = sshbuf_put_bignum2(m, dh->g)) != 0)
613 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000614
615 DH_free(dh);
616 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000617 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000618 return (0);
619}
Damien Miller1f0311c2014-05-15 14:24:09 +1000620#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000621
622int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000623mm_answer_sign(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000624{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000625 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000626 extern int auth_sock; /* XXX move to state struct? */
627 struct sshkey *key;
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000628 struct sshbuf *sigbuf = NULL;
629 u_char *p = NULL, *signature = NULL;
630 char *alg = NULL;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000631 size_t datlen, siglen, alglen;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000632 int r, is_proof = 0;
633 u_int keyid;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000634 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000635
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000636 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000637
djm@openbsd.org141efe42015-01-14 20:05:27 +0000638 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000639 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
640 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000641 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000642 if (keyid > INT_MAX)
643 fatal("%s: invalid key ID", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000644
Damien Millera63128d2006-03-15 12:08:28 +1100645 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000646 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
647 * SHA384 (48 bytes) and SHA512 (64 bytes).
djm@openbsd.org523463a2015-02-16 22:13:32 +0000648 *
649 * Otherwise, verify the signature request is for a hostkey
650 * proof.
651 *
652 * XXX perform similar check for KEX signature requests too?
653 * it's not trivial, since what is signed is the hash, rather
654 * than the full kex structure...
Damien Millera63128d2006-03-15 12:08:28 +1100655 */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000656 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
657 /*
658 * Construct expected hostkey proof and compare it to what
659 * the client sent us.
660 */
661 if (session_id2_len == 0) /* hostkeys is never first */
662 fatal("%s: bad data length: %zu", __func__, datlen);
663 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
664 fatal("%s: no hostkey for index %d", __func__, keyid);
665 if ((sigbuf = sshbuf_new()) == NULL)
666 fatal("%s: sshbuf_new", __func__);
djm@openbsd.org44732de2015-02-20 22:17:21 +0000667 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
668 (r = sshbuf_put_string(sigbuf, session_id2,
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000669 session_id2_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000670 (r = sshkey_puts(key, sigbuf)) != 0)
671 fatal("%s: couldn't prepare private key "
672 "proof buffer: %s", __func__, ssh_err(r));
673 if (datlen != sshbuf_len(sigbuf) ||
674 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
675 fatal("%s: bad data length: %zu, hostkey proof len %zu",
676 __func__, datlen, sshbuf_len(sigbuf));
677 sshbuf_free(sigbuf);
678 is_proof = 1;
679 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000680
Ben Lindstromf67e0772002-06-06 20:58:19 +0000681 /* save session id, it will be passed on the first call */
682 if (session_id2_len == 0) {
683 session_id2_len = datlen;
684 session_id2 = xmalloc(session_id2_len);
685 memcpy(session_id2, p, session_id2_len);
686 }
687
Damien Miller85b45e02013-07-20 13:21:52 +1000688 if ((key = get_hostkey_by_index(keyid)) != NULL) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000689 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000690 datafellows)) != 0)
691 fatal("%s: sshkey_sign failed: %s",
692 __func__, ssh_err(r));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000693 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000694 auth_sock > 0) {
695 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000696 p, datlen, alg, datafellows)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000697 fatal("%s: ssh_agent_sign failed: %s",
698 __func__, ssh_err(r));
699 }
Damien Miller85b45e02013-07-20 13:21:52 +1000700 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000701 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000702
djm@openbsd.org523463a2015-02-16 22:13:32 +0000703 debug3("%s: %s signature %p(%zu)", __func__,
704 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000705
djm@openbsd.org141efe42015-01-14 20:05:27 +0000706 sshbuf_reset(m);
707 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
708 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000709
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000710 free(alg);
Darren Tuckera627d422013-06-02 07:31:17 +1000711 free(p);
712 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000713
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000714 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000715
716 /* Turn on permissions for getpwnam */
717 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
718
719 return (0);
720}
721
722/* Retrieves the password entry and also checks if the user is permitted */
723
724int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000725mm_answer_pwnamallow(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000726{
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000727 struct ssh *ssh = active_state; /* XXX */
Darren Tuckerb09b6772004-06-22 15:06:46 +1000728 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000729 struct passwd *pwent;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000730 int r, allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000731 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000732
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000733 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000734
735 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000736 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000737
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000738 if ((r = sshbuf_get_cstring(m, &username, NULL)) != 0)
739 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000740
Darren Tuckerb09b6772004-06-22 15:06:46 +1000741 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000742
Darren Tuckerb09b6772004-06-22 15:06:46 +1000743 authctxt->user = xstrdup(username);
744 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000745 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000746
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000747 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000748
749 if (pwent == NULL) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000750 if ((r = sshbuf_put_u8(m, 0)) != 0)
751 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf96d1832003-11-18 22:01:48 +1100752 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000753 goto out;
754 }
755
756 allowed = 1;
757 authctxt->pw = pwent;
758 authctxt->valid = 1;
759
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000760 /* XXX don't sent pwent to unpriv; send fake class/dir/shell too */
761 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
762 (r = sshbuf_put_string(m, pwent, sizeof(*pwent))) != 0 ||
763 (r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
764 (r = sshbuf_put_cstring(m, "*")) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000765#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000766 (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000767#endif
768#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000769 (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
Kevin Steves7e147602002-03-22 18:07:17 +0000770#endif
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000771 (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
772 (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
773 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100774
775 out:
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000776 ssh_packet_set_log_preamble(ssh, "%suser %s",
777 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000778 if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
779 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf2e407e2011-05-20 19:04:14 +1000780
781#define M_CP_STROPT(x) do { \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000782 if (options.x != NULL) { \
783 if ((r = sshbuf_put_cstring(m, options.x)) != 0) \
784 fatal("%s: buffer error: %s", \
785 __func__, ssh_err(r)); \
786 } \
Damien Millerf2e407e2011-05-20 19:04:14 +1000787 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000788#define M_CP_STRARRAYOPT(x, nx) do { \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000789 for (i = 0; i < options.nx; i++) { \
790 if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
791 fatal("%s: buffer error: %s", \
792 __func__, ssh_err(r)); \
793 } \
Damien Millerd8478b62011-05-29 21:39:36 +1000794 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000795 /* See comment in servconf.h */
796 COPY_MATCH_STRING_OPTS();
797#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000798#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100799
800 /* Create valid auth method lists */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000801 if (auth2_setup_methods_lists(authctxt) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100802 /*
803 * The monitor will continue long enough to let the child
804 * run to it's packet_disconnect(), but it must not allow any
805 * authentication to succeed.
806 */
807 debug("%s: no valid authentication method lists", __func__);
808 }
809
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000810 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000811 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000812
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000813 /* Allow service/style information on the auth context */
814 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
815 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
816
Damien Millera33501b2002-05-08 12:24:42 +1000817#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000818 if (options.use_pam)
819 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000820#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000821
822 return (0);
823}
824
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000825int mm_answer_auth2_read_banner(int sock, struct sshbuf *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000826{
827 char *banner;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000828 int r;
Damien Miller5ad9fd92002-05-13 11:07:41 +1000829
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000830 sshbuf_reset(m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000831 banner = auth2_read_banner();
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000832 if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
833 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000834 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000835 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000836
837 return (0);
838}
839
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000840int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000841mm_answer_authserv(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000842{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000843 int r;
844
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000845 monitor_permit_authentications(1);
846
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000847 if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
848 (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
849 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000850 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000851 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000852
853 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000854 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000855 authctxt->style = NULL;
856 }
857
858 return (0);
859}
860
861int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000862mm_answer_authpassword(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000863{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000864 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000865 static int call_count;
866 char *passwd;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000867 int r, authenticated;
868 size_t plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000869
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000870 if (!options.password_authentication)
871 fatal("%s: password authentication not enabled", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000872 if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
873 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000874 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000875 authenticated = options.password_authentication &&
djm@openbsd.org7c856852018-03-03 03:15:51 +0000876 auth_password(ssh, passwd);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000877 explicit_bzero(passwd, plen);
Darren Tuckera627d422013-06-02 07:31:17 +1000878 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000879
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000880 sshbuf_reset(m);
881 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
882 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker01558b72016-07-18 09:33:25 +1000883#ifdef USE_PAM
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000884 if ((r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0)
885 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker01558b72016-07-18 09:33:25 +1000886#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000887
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000888 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000889 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000890
891 call_count++;
892 if (plen == 0 && call_count == 1)
893 auth_method = "none";
894 else
895 auth_method = "password";
896
897 /* Causes monitor loop to terminate if authenticated */
898 return (authenticated);
899}
900
901#ifdef BSD_AUTH
902int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000903mm_answer_bsdauthquery(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000904{
905 char *name, *infotxt;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000906 u_int numprompts, *echo_on, success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000907 char **prompts;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000908 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000910 if (!options.kbd_interactive_authentication)
911 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb7df3af2003-02-24 11:55:46 +1100912 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
913 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000914
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000915 sshbuf_reset(m);
916 if ((r = sshbuf_put_u32(m, success)) != 0)
917 fatal("%s: buffer error: %s", __func__, ssh_err(r));
918 if (success) {
919 if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
920 fatal("%s: buffer error: %s", __func__, ssh_err(r));
921 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000922
Damien Millerb7df3af2003-02-24 11:55:46 +1100923 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000924 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000925
Damien Millerb7df3af2003-02-24 11:55:46 +1100926 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000927 free(name);
928 free(infotxt);
929 free(prompts);
930 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000931 }
932
933 return (0);
934}
935
936int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000937mm_answer_bsdauthrespond(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000938{
939 char *response;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000940 int r, authok;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000941
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000942 if (!options.kbd_interactive_authentication)
943 fatal("%s: kbd-int authentication not enabled", __func__);
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +0000944 if (authctxt->as == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000945 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000946
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000947 if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
948 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000949 authok = options.challenge_response_authentication &&
950 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000951 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000952 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000953 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000954
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000955 sshbuf_reset(m);
956 if ((r = sshbuf_put_u32(m, authok)) != 0)
957 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000958
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000959 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000960 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000962 auth_method = "keyboard-interactive";
963 auth_submethod = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000964
965 return (authok != 0);
966}
967#endif
968
969#ifdef SKEY
970int
Damien Miller120a1ec2018-07-10 19:39:52 +1000971mm_answer_skeyquery(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972{
973 struct skey skey;
974 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100975 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000977 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
978 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000979
Damien Miller120a1ec2018-07-10 19:39:52 +1000980 sshbuf_reset(m);
981 if ((r = sshbuf_put_u32(m, success)) != 0)
982 fatal("%s: buffer error: %s", __func__, ssh_err(r));
983 if (success) {
984 if ((r = sshbuf_put_cstring(m, challenge)) != 0)
985 fatal("%s: buffer error: %s", __func__, ssh_err(r));
986 }
Damien Millerb7df3af2003-02-24 11:55:46 +1100987 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000988 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000989
990 return (0);
991}
992
993int
Damien Miller120a1ec2018-07-10 19:39:52 +1000994mm_answer_skeyrespond(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000995{
996 char *response;
Damien Miller120a1ec2018-07-10 19:39:52 +1000997 size_t rlen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000998 int authok;
999
Damien Miller120a1ec2018-07-10 19:39:52 +10001000 if ((r = sshbuf_get_cstring(m, &response, &rlen)) != 0)
1001 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001002
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001003 authok = (options.challenge_response_authentication &&
1004 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005 skey_haskey(authctxt->pw->pw_name) == 0 &&
1006 skey_passcheck(authctxt->pw->pw_name, response) != -1);
1007
Damien Miller120a1ec2018-07-10 19:39:52 +10001008 freezero(response, rlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001009
Damien Miller120a1ec2018-07-10 19:39:52 +10001010 sshbuf_reset(m);
1011 if ((r = sshbuf_put_u32(m, authok)) != 0)
1012 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001013
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001014 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001015 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001016
Damien Millerac947352015-11-20 12:35:41 +11001017 auth_method = "keyboard-interactive";
1018 auth_submethod = "skey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001019
1020 return (authok != 0);
1021}
1022#endif
1023
Damien Miller79418552002-04-23 20:28:48 +10001024#ifdef USE_PAM
1025int
Damien Miller120a1ec2018-07-10 19:39:52 +10001026mm_answer_pam_start(int sock, struct sshbuf *m)
Damien Miller79418552002-04-23 20:28:48 +10001027{
Damien Miller4e448a32003-05-14 15:11:48 +10001028 if (!options.use_pam)
1029 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1030
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001031 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001032
Damien Miller1f499fd2003-08-25 13:08:49 +10001033 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
Damien Miller775f8a22016-08-31 10:48:07 +10001034 if (options.kbd_interactive_authentication)
1035 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller1f499fd2003-08-25 13:08:49 +10001036
Damien Miller79418552002-04-23 20:28:48 +10001037 return (0);
1038}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001039
Damien Miller1f499fd2003-08-25 13:08:49 +10001040int
Damien Miller120a1ec2018-07-10 19:39:52 +10001041mm_answer_pam_account(int sock, struct sshbuf *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001042{
1043 u_int ret;
Damien Miller120a1ec2018-07-10 19:39:52 +10001044 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +11001045
Damien Miller1f499fd2003-08-25 13:08:49 +10001046 if (!options.use_pam)
Damien Miller775f8a22016-08-31 10:48:07 +10001047 fatal("%s: PAM not enabled", __func__);
Damien Miller1f499fd2003-08-25 13:08:49 +10001048
1049 ret = do_pam_account();
1050
Damien Miller120a1ec2018-07-10 19:39:52 +10001051 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1052 (r = sshbuf_put_stringb(m, loginmsg)) != 0)
1053 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller1f499fd2003-08-25 13:08:49 +10001054
Darren Tucker7eda5922004-06-22 13:22:50 +10001055 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001056
1057 return (ret);
1058}
1059
Damien Miller4f9f42a2003-05-10 19:28:02 +10001060static void *sshpam_ctxt, *sshpam_authok;
1061extern KbdintDevice sshpam_device;
1062
1063int
Damien Miller120a1ec2018-07-10 19:39:52 +10001064mm_answer_pam_init_ctx(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001065{
Damien Miller120a1ec2018-07-10 19:39:52 +10001066 u_int ok = 0;
1067 int r;
1068
Damien Miller4f9f42a2003-05-10 19:28:02 +10001069 debug3("%s", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001070 if (!options.kbd_interactive_authentication)
1071 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001072 if (sshpam_ctxt != NULL)
1073 fatal("%s: already called", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001074 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1075 sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001076 sshbuf_reset(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001077 if (sshpam_ctxt != NULL) {
1078 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
Damien Millerb38b95f2016-08-29 11:47:07 +10001079 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
Damien Miller120a1ec2018-07-10 19:39:52 +10001080 ok = 1;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001081 }
Damien Miller120a1ec2018-07-10 19:39:52 +10001082 if ((r = sshbuf_put_u32(m, ok)) != 0)
1083 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker7eda5922004-06-22 13:22:50 +10001084 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001085 return (0);
1086}
1087
1088int
Damien Miller120a1ec2018-07-10 19:39:52 +10001089mm_answer_pam_query(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001090{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001091 char *name = NULL, *info = NULL, **prompts = NULL;
1092 u_int i, num = 0, *echo_on = 0;
Damien Miller120a1ec2018-07-10 19:39:52 +10001093 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001094
1095 debug3("%s", __func__);
1096 sshpam_authok = NULL;
Damien Millerb38b95f2016-08-29 11:47:07 +10001097 if (sshpam_ctxt == NULL)
1098 fatal("%s: no context", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001099 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1100 &num, &prompts, &echo_on);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001101 if (ret == 0 && num == 0)
1102 sshpam_authok = sshpam_ctxt;
1103 if (num > 1 || name == NULL || info == NULL)
Damien Millerb38b95f2016-08-29 11:47:07 +10001104 fatal("sshpam_device.query failed");
1105 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
Damien Miller120a1ec2018-07-10 19:39:52 +10001106 sshbuf_reset(m);
1107 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1108 (r = sshbuf_put_cstring(m, name)) != 0 ||
1109 (r = sshbuf_put_cstring(m, info)) != 0 ||
1110 (r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0 ||
1111 (r = sshbuf_put_u32(m, num)) != 0)
1112 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +10001113 free(name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001114 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001115 for (i = 0; i < num; ++i) {
Damien Miller120a1ec2018-07-10 19:39:52 +10001116 if ((r = sshbuf_put_cstring(m, prompts[i])) != 0 ||
1117 (r = sshbuf_put_u32(m, echo_on[i])) != 0)
1118 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +10001119 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001120 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001121 free(prompts);
1122 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001123 auth_method = "keyboard-interactive";
1124 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001125 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001126 return (0);
1127}
1128
1129int
Damien Miller120a1ec2018-07-10 19:39:52 +10001130mm_answer_pam_respond(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001131{
1132 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001133 u_int i, num;
Damien Miller120a1ec2018-07-10 19:39:52 +10001134 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001135
1136 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001137 if (sshpam_ctxt == NULL)
1138 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001139 sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001140 if ((r = sshbuf_get_u32(m, &num)) != 0)
1141 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001142 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001143 resp = xcalloc(num, sizeof(char *));
Damien Miller120a1ec2018-07-10 19:39:52 +10001144 for (i = 0; i < num; ++i) {
1145 if ((r = sshbuf_get_cstring(m, &(resp[i]), NULL)) != 0)
1146 fatal("%s: buffer error: %s",
1147 __func__, ssh_err(r));
1148 }
Damien Miller4f9f42a2003-05-10 19:28:02 +10001149 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1150 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001151 free(resp[i]);
1152 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001153 } else {
1154 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1155 }
Damien Miller120a1ec2018-07-10 19:39:52 +10001156 sshbuf_reset(m);
1157 if ((r = sshbuf_put_u32(m, ret)) != 0)
1158 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker7eda5922004-06-22 13:22:50 +10001159 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001160 auth_method = "keyboard-interactive";
1161 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001162 if (ret == 0)
1163 sshpam_authok = sshpam_ctxt;
1164 return (0);
1165}
1166
1167int
Damien Miller120a1ec2018-07-10 19:39:52 +10001168mm_answer_pam_free_ctx(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001169{
Damien Miller5e75f512015-08-11 13:34:12 +10001170 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001171
1172 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001173 if (sshpam_ctxt == NULL)
1174 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001175 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001176 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001177 sshbuf_reset(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001178 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Millerb38b95f2016-08-29 11:47:07 +10001179 /* Allow another attempt */
1180 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller15b05cf2012-12-03 09:53:20 +11001181 auth_method = "keyboard-interactive";
1182 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001183 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001184}
Damien Miller79418552002-04-23 20:28:48 +10001185#endif
1186
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001187int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001188mm_answer_keyallowed(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001189{
djm@openbsd.org7c856852018-03-03 03:15:51 +00001190 struct ssh *ssh = active_state; /* XXX */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001191 struct sshkey *key = NULL;
Damien Millera10f5612002-09-12 09:49:15 +10001192 char *cuser, *chost;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001193 u_int pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001194 enum mm_keytype type = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001195 int r, allowed = 0;
1196 struct sshauthopt *opts = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001197
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001198 debug3("%s entering", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001199 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1200 (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
1201 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
1202 (r = sshkey_froms(m, &key)) != 0 ||
1203 (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
1204 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001205
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001206 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001207
Damien Miller3e3b5142003-11-17 21:13:40 +11001208 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001209 /* These should not make it past the privsep child */
1210 if (key_type_plain(key->type) == KEY_RSA &&
1211 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1212 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1213
Darren Tucker47eede72005-03-14 23:08:12 +11001214 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001215 case MM_USERKEY:
Damien Miller7a8f5b32006-03-31 23:14:23 +11001216 auth_method = "publickey";
djm@openbsd.org7c856852018-03-03 03:15:51 +00001217 if (!options.pubkey_authentication)
1218 break;
1219 if (auth2_key_already_used(authctxt, key))
1220 break;
1221 if (match_pattern_list(sshkey_ssh_name(key),
1222 options.pubkey_key_types, 0) != 1)
1223 break;
1224 allowed = user_key_allowed(ssh, authctxt->pw, key,
1225 pubkey_auth_attempt, &opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001226 break;
1227 case MM_HOSTKEY:
djm@openbsd.org7c856852018-03-03 03:15:51 +00001228 auth_method = "hostbased";
1229 if (!options.hostbased_authentication)
1230 break;
1231 if (auth2_key_already_used(authctxt, key))
1232 break;
1233 if (match_pattern_list(sshkey_ssh_name(key),
1234 options.hostbased_key_types, 0) != 1)
1235 break;
1236 allowed = hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001237 cuser, chost, key);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001238 auth2_record_info(authctxt,
Damien Miller20bdcd72013-07-18 16:10:09 +10001239 "client user \"%.100s\", client host \"%.100s\"",
1240 cuser, chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001241 break;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001242 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001243 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001244 break;
1245 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001246 }
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001247
djm@openbsd.org7c856852018-03-03 03:15:51 +00001248 debug3("%s: %s authentication%s: %s key is %s", __func__,
1249 auth_method, pubkey_auth_attempt ? "" : " test",
1250 (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
1251 allowed ? "allowed" : "not allowed");
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001252
djm@openbsd.org8f574952017-06-24 06:34:38 +00001253 auth2_record_key(authctxt, 0, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001254
1255 /* clear temporarily storage (used by verify) */
1256 monitor_reset_key_state();
1257
1258 if (allowed) {
1259 /* Save temporarily for comparison in verify */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001260 if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
1261 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001262 key_blobtype = type;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001263 key_opts = opts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001264 hostbased_cuser = cuser;
1265 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001266 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001267 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001268 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001269 free(cuser);
1270 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001271 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001272 sshkey_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001273
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001274 sshbuf_reset(m);
1275 if ((r = sshbuf_put_u32(m, allowed)) != 0)
1276 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org7c856852018-03-03 03:15:51 +00001277 if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
1278 fatal("%s: sshauthopt_serialise: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001279 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001280
djm@openbsd.org7c856852018-03-03 03:15:51 +00001281 if (!allowed)
1282 sshauthopt_free(opts);
1283
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001284 return (0);
1285}
1286
1287static int
1288monitor_valid_userblob(u_char *data, u_int datalen)
1289{
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001290 struct sshbuf *b;
1291 const u_char *p;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001292 char *userstyle, *cp;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001293 size_t len;
1294 u_char type;
1295 int r, fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001296
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001297 if ((b = sshbuf_new()) == NULL)
1298 fatal("%s: sshbuf_new", __func__);
1299 if ((r = sshbuf_put(b, data, datalen)) != 0)
1300 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001301
1302 if (datafellows & SSH_OLD_SESSIONID) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001303 p = sshbuf_ptr(b);
1304 len = sshbuf_len(b);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001305 if ((session_id2 == NULL) ||
1306 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001307 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001308 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001309 if ((r = sshbuf_consume(b, session_id2_len)) != 0)
1310 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001311 } else {
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001312 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1313 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromf67e0772002-06-06 20:58:19 +00001314 if ((session_id2 == NULL) ||
1315 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001316 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001317 fail++;
1318 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001319 if ((r = sshbuf_get_u8(b, &type)) != 0)
1320 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1321 if (type != SSH2_MSG_USERAUTH_REQUEST)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001322 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001323 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1324 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4ce189d2013-04-23 15:17:52 +10001325 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1326 authctxt->style ? ":" : "",
1327 authctxt->style ? authctxt->style : "");
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001328 if (strcmp(userstyle, cp) != 0) {
1329 logit("wrong user name passed to monitor: "
1330 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001331 fail++;
1332 }
Darren Tuckera627d422013-06-02 07:31:17 +10001333 free(userstyle);
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001334 free(cp);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001335 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1336 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1337 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001338 if (strcmp("publickey", cp) != 0)
1339 fail++;
1340 free(cp);
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 == 0)
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001344 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001345 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1346 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1347 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1348 if (sshbuf_len(b) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001349 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001350 sshbuf_free(b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001351 return (fail == 0);
1352}
1353
1354static int
Damien Millera10f5612002-09-12 09:49:15 +10001355monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1356 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001357{
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001358 struct sshbuf *b;
1359 const u_char *p;
1360 char *cp, *userstyle;
1361 size_t len;
1362 int r, fail = 0;
1363 u_char type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001364
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001365 if ((b = sshbuf_new()) == NULL)
1366 fatal("%s: sshbuf_new", __func__);
1367 if ((r = sshbuf_put(b, data, datalen)) != 0 ||
1368 (r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1369 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001370
Ben Lindstromf67e0772002-06-06 20:58:19 +00001371 if ((session_id2 == NULL) ||
1372 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001373 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001374 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001375
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001376 if ((r = sshbuf_get_u8(b, &type)) != 0)
1377 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1378 if (type != SSH2_MSG_USERAUTH_REQUEST)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001379 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001380 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1381 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4ce189d2013-04-23 15:17:52 +10001382 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1383 authctxt->style ? ":" : "",
1384 authctxt->style ? authctxt->style : "");
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001385 if (strcmp(userstyle, cp) != 0) {
1386 logit("wrong user name passed to monitor: "
1387 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001388 fail++;
1389 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001390 free(userstyle);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001391 free(cp);
1392 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1393 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1394 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1395 if (strcmp(cp, "hostbased") != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001396 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001397 free(cp);
1398 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1399 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1400 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001401
1402 /* verify client host, strip trailing dot if necessary */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001403 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1404 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1405 if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
1406 cp[len - 1] = '\0';
1407 if (strcmp(cp, chost) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001408 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001409 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001410
1411 /* verify client user */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001412 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1413 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1414 if (strcmp(cp, cuser) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001415 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001416 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001417
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001418 if (sshbuf_len(b) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001419 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001420 sshbuf_free(b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001421 return (fail == 0);
1422}
1423
1424int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001425mm_answer_keyverify(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001426{
djm@openbsd.org7c856852018-03-03 03:15:51 +00001427 struct ssh *ssh = active_state; /* XXX */
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001428 struct sshkey *key;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001429 u_char *signature, *data, *blob;
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001430 char *sigalg;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001431 size_t signaturelen, datalen, bloblen;
1432 int r, ret, valid_data = 0, encoded_ret;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001433
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001434 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1435 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001436 (r = sshbuf_get_string(m, &data, &datalen)) != 0 ||
1437 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001438 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001439
1440 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001441 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001442 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001443
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001444 /* Empty signature algorithm means NULL. */
1445 if (*sigalg == '\0') {
1446 free(sigalg);
1447 sigalg = NULL;
1448 }
1449
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001450 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1451 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1452 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001453
1454 switch (key_blobtype) {
1455 case MM_USERKEY:
1456 valid_data = monitor_valid_userblob(data, datalen);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001457 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001458 break;
1459 case MM_HOSTKEY:
1460 valid_data = monitor_valid_hostbasedblob(data, datalen,
1461 hostbased_cuser, hostbased_chost);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001462 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001463 break;
1464 default:
1465 valid_data = 0;
1466 break;
1467 }
1468 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001469 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001470
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001471 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001472 sigalg, active_state->compat);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001473 debug3("%s: %s %p signature %s", __func__, auth_method, key,
1474 (ret == 0) ? "verified" : "unverified");
1475 auth2_record_key(authctxt, ret == 0, key);
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001476
Darren Tuckera627d422013-06-02 07:31:17 +10001477 free(blob);
1478 free(signature);
1479 free(data);
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001480 free(sigalg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001481
djm@openbsd.org7c856852018-03-03 03:15:51 +00001482 if (key_blobtype == MM_USERKEY)
1483 auth_activate_options(ssh, key_opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001484 monitor_reset_key_state();
1485
djm@openbsd.org8f574952017-06-24 06:34:38 +00001486 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001487 sshbuf_reset(m);
1488
1489 /* encode ret != 0 as positive integer, since we're sending u32 */
1490 encoded_ret = (ret != 0);
1491 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1492 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001493 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001494
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001495 return ret == 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001496}
1497
1498static void
1499mm_record_login(Session *s, struct passwd *pw)
1500{
djm@openbsd.org95767262016-03-07 19:02:43 +00001501 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001502 socklen_t fromlen;
1503 struct sockaddr_storage from;
1504
1505 /*
1506 * Get IP address of client. If the connection is not a socket, let
1507 * the address be 0.0.0.0.
1508 */
1509 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001510 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001511 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001512 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001513 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001514 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001515 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001516 }
1517 }
1518 /* Record that there was a login on that tty from the remote host. */
1519 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +00001520 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001521 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001522}
1523
1524static void
1525mm_session_close(Session *s)
1526{
Damien Miller04bd8b02003-05-25 14:38:33 +10001527 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001528 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001529 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001530 session_pty_cleanup2(s);
1531 }
Damien Miller7207f642008-05-19 15:34:50 +10001532 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001533}
1534
1535int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001536mm_answer_pty(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001537{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001538 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001539 Session *s;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001540 int r, res, fd0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001541
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001542 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001543
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001544 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001545 s = session_new();
1546 if (s == NULL)
1547 goto error;
1548 s->authctxt = authctxt;
1549 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001550 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001551 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1552 if (res == 0)
1553 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001554 pty_setowner(authctxt->pw, s->tty);
1555
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001556 if ((r = sshbuf_put_u32(m, 1)) != 0 ||
1557 (r = sshbuf_put_cstring(m, s->tty)) != 0)
1558 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001559
1560 /* We need to trick ttyslot */
1561 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001562 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001563
1564 mm_record_login(s, authctxt->pw);
1565
1566 /* Now we can close the file descriptor again */
1567 close(0);
1568
Darren Tucker09991742004-07-17 17:05:14 +10001569 /* send messages generated by record_login */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001570 if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
1571 fatal("%s: put login message: %s", __func__, ssh_err(r));
1572 sshbuf_reset(loginmsg);
Darren Tucker09991742004-07-17 17:05:14 +10001573
1574 mm_request_send(sock, MONITOR_ANS_PTY, m);
1575
Damien Miller54fd7cf2007-09-17 12:04:08 +10001576 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1577 mm_send_fd(sock, s->ttyfd) == -1)
1578 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001579
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001580 /* make sure nothing uses fd 0 */
1581 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001582 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001583 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001584 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001585
1586 /* slave is not needed */
1587 close(s->ttyfd);
1588 s->ttyfd = s->ptyfd;
1589 /* no need to dup() because nobody closes ptyfd */
1590 s->ptymaster = s->ptyfd;
1591
Damien Miller9ab00b42006-08-05 12:40:11 +10001592 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001593
1594 return (0);
1595
1596 error:
1597 if (s != NULL)
1598 mm_session_close(s);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001599 if ((r = sshbuf_put_u32(m, 0)) != 0)
1600 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001601 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001602 return (0);
1603}
1604
1605int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001606mm_answer_pty_cleanup(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001607{
1608 Session *s;
1609 char *tty;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001610 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001611
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001612 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001613
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001614 if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
1615 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001616 if ((s = session_by_tty(tty)) != NULL)
1617 mm_session_close(s);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001618 sshbuf_reset(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001619 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001620 return (0);
1621}
1622
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001623int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001624mm_answer_term(int sock, struct sshbuf *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001625{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001626 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001627 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001628 int res, status;
1629
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001630 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001631
1632 /* The child is terminating */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001633 session_destroy_all(ssh, &mm_session_close);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001634
Darren Tucker52358d62008-03-11 22:58:25 +11001635#ifdef USE_PAM
1636 if (options.use_pam)
1637 sshpam_cleanup();
1638#endif
1639
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001640 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001641 if (errno != EINTR)
1642 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001643
1644 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1645
1646 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001647 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001648}
1649
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001650#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001651/* Report that an audit event occurred */
1652int
Damien Miller120a1ec2018-07-10 19:39:52 +10001653mm_answer_audit_event(int socket, struct sshbuf *m)
Darren Tucker269a1ea2005-02-03 00:20:53 +11001654{
Damien Miller120a1ec2018-07-10 19:39:52 +10001655 u_int n;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001656 ssh_audit_event_t event;
Damien Miller120a1ec2018-07-10 19:39:52 +10001657 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001658
1659 debug3("%s entering", __func__);
1660
Damien Miller120a1ec2018-07-10 19:39:52 +10001661 if ((r = sshbuf_get_u32(m, &n)) != 0)
1662 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1663 event = (ssh_audit_event_t)n;
1664 switch (event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001665 case SSH_AUTH_FAIL_PUBKEY:
1666 case SSH_AUTH_FAIL_HOSTBASED:
1667 case SSH_AUTH_FAIL_GSSAPI:
1668 case SSH_LOGIN_EXCEED_MAXTRIES:
1669 case SSH_LOGIN_ROOT_DENIED:
1670 case SSH_CONNECTION_CLOSE:
1671 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001672 audit_event(event);
1673 break;
1674 default:
1675 fatal("Audit event type %d not permitted", event);
1676 }
1677
1678 return (0);
1679}
1680
1681int
Damien Miller120a1ec2018-07-10 19:39:52 +10001682mm_answer_audit_command(int socket, struct sshbuf *m)
Darren Tucker269a1ea2005-02-03 00:20:53 +11001683{
Darren Tucker269a1ea2005-02-03 00:20:53 +11001684 char *cmd;
Damien Miller120a1ec2018-07-10 19:39:52 +10001685 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001686
1687 debug3("%s entering", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +10001688 if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
1689 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker269a1ea2005-02-03 00:20:53 +11001690 /* sanity check command, if so how? */
1691 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001692 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001693 return (0);
1694}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001695#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001696
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001697void
markus@openbsd.org1e0cdf82017-05-31 08:09:45 +00001698monitor_clear_keystate(struct monitor *pmonitor)
1699{
1700 struct ssh *ssh = active_state; /* XXX */
1701
1702 ssh_clear_newkeys(ssh, MODE_IN);
1703 ssh_clear_newkeys(ssh, MODE_OUT);
1704 sshbuf_free(child_state);
1705 child_state = NULL;
1706}
1707
1708void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001709monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001710{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001711 struct ssh *ssh = active_state; /* XXX */
1712 struct kex *kex;
1713 int r;
1714
1715 debug3("%s: packet_set_state", __func__);
1716 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1717 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1718 sshbuf_free(child_state);
1719 child_state = NULL;
1720
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +00001721 if ((kex = ssh->kex) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001722 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001723#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001724 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1725 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +00001726 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1727 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1728 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001729 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1730 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001731# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001732 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001733# endif
Damien Miller773dda22015-01-30 23:10:17 +11001734#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001735 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1736 kex->load_host_public_key=&get_hostkey_public_by_type;
1737 kex->load_host_private_key=&get_hostkey_private_by_type;
1738 kex->host_key_index=&get_hostkey_index;
1739 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001740 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001741}
1742
1743/* This function requries careful sanity checking */
1744
1745void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001746mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001747{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001748 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001749
markus@openbsd.org091c3022015-01-19 19:52:16 +00001750 if ((child_state = sshbuf_new()) == NULL)
1751 fatal("%s: sshbuf_new failed", __func__);
1752 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1753 child_state);
1754 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001755}
1756
1757
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001758/* XXX */
1759
1760#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001761 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001762 fatal("fcntl(%d, F_SETFD)", x); \
1763} while (0)
1764
1765static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001766monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001767{
Damien Miller8f0bf232011-06-20 14:42:23 +10001768 int pair[2];
markus@openbsd.org84008602017-05-31 10:04:29 +00001769#ifdef SO_ZEROIZE
1770 int on = 1;
1771#endif
Damien Miller8f0bf232011-06-20 14:42:23 +10001772
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001773 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001774 fatal("%s: socketpair: %s", __func__, strerror(errno));
markus@openbsd.org84008602017-05-31 10:04:29 +00001775#ifdef SO_ZEROIZE
1776 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1777 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1778 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1779 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1780#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001781 FD_CLOSEONEXEC(pair[0]);
1782 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001783 mon->m_recvfd = pair[0];
1784 mon->m_sendfd = pair[1];
1785
1786 if (do_logfds) {
1787 if (pipe(pair) == -1)
1788 fatal("%s: pipe: %s", __func__, strerror(errno));
1789 FD_CLOSEONEXEC(pair[0]);
1790 FD_CLOSEONEXEC(pair[1]);
1791 mon->m_log_recvfd = pair[0];
1792 mon->m_log_sendfd = pair[1];
1793 } else
1794 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001795}
1796
1797#define MM_MEMSIZE 65536
1798
1799struct monitor *
1800monitor_init(void)
1801{
1802 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001803
Damien Miller07d86be2006-03-26 14:19:21 +11001804 mon = xcalloc(1, sizeof(*mon));
Damien Miller8f0bf232011-06-20 14:42:23 +10001805 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001806
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001807 return mon;
1808}
1809
1810void
1811monitor_reinit(struct monitor *mon)
1812{
Damien Miller8f0bf232011-06-20 14:42:23 +10001813 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001814}
Darren Tucker0efd1552003-08-26 11:49:55 +10001815
1816#ifdef GSSAPI
1817int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001818mm_answer_gss_setup_ctx(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001819{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001820 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001821 OM_uint32 major;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001822 size_t len;
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001823 u_char *p;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001824 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +10001825
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001826 if (!options.gss_authentication)
1827 fatal("%s: GSSAPI authentication not enabled", __func__);
1828
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001829 if ((r = sshbuf_get_string(m, &p, &len)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001830 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001831 goid.elements = p;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001832 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001833
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001834 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001835
Darren Tuckera627d422013-06-02 07:31:17 +10001836 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001837
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001838 sshbuf_reset(m);
1839 if ((r = sshbuf_put_u32(m, major)) != 0)
1840 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001841
Damien Miller6fd6def2005-11-05 15:07:05 +11001842 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001843
1844 /* Now we have a context, enable the step */
1845 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1846
1847 return (0);
1848}
1849
1850int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001851mm_answer_gss_accept_ctx(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001852{
1853 gss_buffer_desc in;
1854 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001855 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001856 OM_uint32 flags = 0; /* GSI needs this */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001857 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +10001858
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001859 if (!options.gss_authentication)
1860 fatal("%s: GSSAPI authentication not enabled", __func__);
1861
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001862 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001863 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001864 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001865 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001866
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001867 sshbuf_reset(m);
1868 if ((r = sshbuf_put_u32(m, major)) != 0 ||
1869 (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
1870 (r = sshbuf_put_u32(m, flags)) != 0)
1871 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001872 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001873
1874 gss_release_buffer(&minor, &out);
1875
Damien Miller6fd6def2005-11-05 15:07:05 +11001876 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001877 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1878 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001879 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001880 }
1881 return (0);
1882}
1883
1884int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001885mm_answer_gss_checkmic(int sock, struct sshbuf *m)
Damien Miller0425d402003-11-17 22:18:21 +11001886{
1887 gss_buffer_desc gssbuf, mic;
1888 OM_uint32 ret;
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001889 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +11001890
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001891 if (!options.gss_authentication)
1892 fatal("%s: GSSAPI authentication not enabled", __func__);
1893
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001894 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
1895 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001896 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller787b2ec2003-11-21 23:56:47 +11001897
Damien Miller0425d402003-11-17 22:18:21 +11001898 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001899
Darren Tuckera627d422013-06-02 07:31:17 +10001900 free(gssbuf.value);
1901 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001902
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001903 sshbuf_reset(m);
1904 if ((r = sshbuf_put_u32(m, ret)) != 0)
1905 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller787b2ec2003-11-21 23:56:47 +11001906
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001907 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001908
Damien Miller0425d402003-11-17 22:18:21 +11001909 if (!GSS_ERROR(ret))
1910 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001911
Damien Miller0425d402003-11-17 22:18:21 +11001912 return (0);
1913}
1914
1915int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001916mm_answer_gss_userok(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001917{
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001918 int r, authenticated;
djm@openbsd.org8f574952017-06-24 06:34:38 +00001919 const char *displayname;
Darren Tucker0efd1552003-08-26 11:49:55 +10001920
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001921 if (!options.gss_authentication)
1922 fatal("%s: GSSAPI authentication not enabled", __func__);
1923
Darren Tucker0efd1552003-08-26 11:49:55 +10001924 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1925
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001926 sshbuf_reset(m);
1927 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1928 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001929
1930 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001931 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001932
Damien Miller6fd6def2005-11-05 15:07:05 +11001933 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001934
djm@openbsd.org8f574952017-06-24 06:34:38 +00001935 if ((displayname = ssh_gssapi_displayname()) != NULL)
1936 auth2_record_info(authctxt, "%s", displayname);
1937
Darren Tucker0efd1552003-08-26 11:49:55 +10001938 /* Monitor loop will terminate if authenticated */
1939 return (authenticated);
1940}
1941#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001942