blob: f5d1b8a05f929862fa2f932f15cb37dd0614042b [file] [log] [blame]
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001/* $OpenBSD: monitor.c,v 1.185 2018/07/11 18:53:29 markus 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"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +000071#include "sshkey.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;
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000633 u_int keyid, compat;
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 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000640 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0 ||
641 (r = sshbuf_get_u32(m, &compat)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000642 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000643 if (keyid > INT_MAX)
644 fatal("%s: invalid key ID", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000645
Damien Millera63128d2006-03-15 12:08:28 +1100646 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000647 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
648 * SHA384 (48 bytes) and SHA512 (64 bytes).
djm@openbsd.org523463a2015-02-16 22:13:32 +0000649 *
650 * Otherwise, verify the signature request is for a hostkey
651 * proof.
652 *
653 * XXX perform similar check for KEX signature requests too?
654 * it's not trivial, since what is signed is the hash, rather
655 * than the full kex structure...
Damien Millera63128d2006-03-15 12:08:28 +1100656 */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000657 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
658 /*
659 * Construct expected hostkey proof and compare it to what
660 * the client sent us.
661 */
662 if (session_id2_len == 0) /* hostkeys is never first */
663 fatal("%s: bad data length: %zu", __func__, datlen);
664 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
665 fatal("%s: no hostkey for index %d", __func__, keyid);
666 if ((sigbuf = sshbuf_new()) == NULL)
667 fatal("%s: sshbuf_new", __func__);
djm@openbsd.org44732de2015-02-20 22:17:21 +0000668 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
669 (r = sshbuf_put_string(sigbuf, session_id2,
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000670 session_id2_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000671 (r = sshkey_puts(key, sigbuf)) != 0)
672 fatal("%s: couldn't prepare private key "
673 "proof buffer: %s", __func__, ssh_err(r));
674 if (datlen != sshbuf_len(sigbuf) ||
675 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
676 fatal("%s: bad data length: %zu, hostkey proof len %zu",
677 __func__, datlen, sshbuf_len(sigbuf));
678 sshbuf_free(sigbuf);
679 is_proof = 1;
680 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000681
Ben Lindstromf67e0772002-06-06 20:58:19 +0000682 /* save session id, it will be passed on the first call */
683 if (session_id2_len == 0) {
684 session_id2_len = datlen;
685 session_id2 = xmalloc(session_id2_len);
686 memcpy(session_id2, p, session_id2_len);
687 }
688
Damien Miller85b45e02013-07-20 13:21:52 +1000689 if ((key = get_hostkey_by_index(keyid)) != NULL) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000690 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000691 compat)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000692 fatal("%s: sshkey_sign failed: %s",
693 __func__, ssh_err(r));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000694 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000695 auth_sock > 0) {
696 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000697 p, datlen, alg, compat)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000698 fatal("%s: ssh_agent_sign failed: %s",
699 __func__, ssh_err(r));
700 }
Damien Miller85b45e02013-07-20 13:21:52 +1000701 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000702 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000703
djm@openbsd.org523463a2015-02-16 22:13:32 +0000704 debug3("%s: %s signature %p(%zu)", __func__,
705 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000706
djm@openbsd.org141efe42015-01-14 20:05:27 +0000707 sshbuf_reset(m);
708 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
709 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000710
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000711 free(alg);
Darren Tuckera627d422013-06-02 07:31:17 +1000712 free(p);
713 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000714
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000715 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000716
717 /* Turn on permissions for getpwnam */
718 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
719
720 return (0);
721}
722
723/* Retrieves the password entry and also checks if the user is permitted */
724
725int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000726mm_answer_pwnamallow(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000727{
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000728 struct ssh *ssh = active_state; /* XXX */
Darren Tuckerb09b6772004-06-22 15:06:46 +1000729 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000730 struct passwd *pwent;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000731 int r, allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000732 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000733
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000734 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000735
736 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000737 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000738
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000739 if ((r = sshbuf_get_cstring(m, &username, NULL)) != 0)
740 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000741
Darren Tuckerb09b6772004-06-22 15:06:46 +1000742 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000743
Darren Tuckerb09b6772004-06-22 15:06:46 +1000744 authctxt->user = xstrdup(username);
745 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000746 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000747
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000748 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000749
750 if (pwent == NULL) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000751 if ((r = sshbuf_put_u8(m, 0)) != 0)
752 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf96d1832003-11-18 22:01:48 +1100753 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000754 goto out;
755 }
756
757 allowed = 1;
758 authctxt->pw = pwent;
759 authctxt->valid = 1;
760
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000761 /* XXX don't sent pwent to unpriv; send fake class/dir/shell too */
762 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
763 (r = sshbuf_put_string(m, pwent, sizeof(*pwent))) != 0 ||
764 (r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
765 (r = sshbuf_put_cstring(m, "*")) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000766#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000767 (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000768#endif
769#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000770 (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
Kevin Steves7e147602002-03-22 18:07:17 +0000771#endif
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000772 (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
773 (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
774 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100775
776 out:
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000777 ssh_packet_set_log_preamble(ssh, "%suser %s",
778 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000779 if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
780 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf2e407e2011-05-20 19:04:14 +1000781
782#define M_CP_STROPT(x) do { \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000783 if (options.x != NULL) { \
784 if ((r = sshbuf_put_cstring(m, options.x)) != 0) \
785 fatal("%s: buffer error: %s", \
786 __func__, ssh_err(r)); \
787 } \
Damien Millerf2e407e2011-05-20 19:04:14 +1000788 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000789#define M_CP_STRARRAYOPT(x, nx) do { \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000790 for (i = 0; i < options.nx; i++) { \
791 if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
792 fatal("%s: buffer error: %s", \
793 __func__, ssh_err(r)); \
794 } \
Damien Millerd8478b62011-05-29 21:39:36 +1000795 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000796 /* See comment in servconf.h */
797 COPY_MATCH_STRING_OPTS();
798#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000799#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100800
801 /* Create valid auth method lists */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000802 if (auth2_setup_methods_lists(authctxt) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100803 /*
804 * The monitor will continue long enough to let the child
805 * run to it's packet_disconnect(), but it must not allow any
806 * authentication to succeed.
807 */
808 debug("%s: no valid authentication method lists", __func__);
809 }
810
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000811 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000812 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000813
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000814 /* Allow service/style information on the auth context */
815 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
816 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
817
Damien Millera33501b2002-05-08 12:24:42 +1000818#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000819 if (options.use_pam)
820 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000821#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000822
823 return (0);
824}
825
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000826int mm_answer_auth2_read_banner(int sock, struct sshbuf *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000827{
828 char *banner;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000829 int r;
Damien Miller5ad9fd92002-05-13 11:07:41 +1000830
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000831 sshbuf_reset(m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000832 banner = auth2_read_banner();
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000833 if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
834 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000835 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000836 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000837
838 return (0);
839}
840
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000841int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000842mm_answer_authserv(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000843{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000844 int r;
845
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000846 monitor_permit_authentications(1);
847
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000848 if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
849 (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
850 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000851 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000852 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000853
854 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000855 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000856 authctxt->style = NULL;
857 }
858
859 return (0);
860}
861
862int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000863mm_answer_authpassword(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000864{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000865 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000866 static int call_count;
867 char *passwd;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000868 int r, authenticated;
869 size_t plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000870
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000871 if (!options.password_authentication)
872 fatal("%s: password authentication not enabled", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000873 if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
874 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000875 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000876 authenticated = options.password_authentication &&
djm@openbsd.org7c856852018-03-03 03:15:51 +0000877 auth_password(ssh, passwd);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000878 explicit_bzero(passwd, plen);
Darren Tuckera627d422013-06-02 07:31:17 +1000879 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000880
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000881 sshbuf_reset(m);
882 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
883 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker01558b72016-07-18 09:33:25 +1000884#ifdef USE_PAM
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000885 if ((r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0)
886 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker01558b72016-07-18 09:33:25 +1000887#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000888
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000889 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000890 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000891
892 call_count++;
893 if (plen == 0 && call_count == 1)
894 auth_method = "none";
895 else
896 auth_method = "password";
897
898 /* Causes monitor loop to terminate if authenticated */
899 return (authenticated);
900}
901
902#ifdef BSD_AUTH
903int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000904mm_answer_bsdauthquery(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000905{
906 char *name, *infotxt;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000907 u_int numprompts, *echo_on, success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000908 char **prompts;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000909 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000910
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000911 if (!options.kbd_interactive_authentication)
912 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb7df3af2003-02-24 11:55:46 +1100913 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
914 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000915
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000916 sshbuf_reset(m);
917 if ((r = sshbuf_put_u32(m, success)) != 0)
918 fatal("%s: buffer error: %s", __func__, ssh_err(r));
919 if (success) {
920 if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
921 fatal("%s: buffer error: %s", __func__, ssh_err(r));
922 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000923
Damien Millerb7df3af2003-02-24 11:55:46 +1100924 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000925 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926
Damien Millerb7df3af2003-02-24 11:55:46 +1100927 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000928 free(name);
929 free(infotxt);
930 free(prompts);
931 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932 }
933
934 return (0);
935}
936
937int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000938mm_answer_bsdauthrespond(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000939{
940 char *response;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000941 int r, authok;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000942
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000943 if (!options.kbd_interactive_authentication)
944 fatal("%s: kbd-int authentication not enabled", __func__);
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +0000945 if (authctxt->as == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000946 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000947
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000948 if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
949 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000950 authok = options.challenge_response_authentication &&
951 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000952 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000953 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000954 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000955
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000956 sshbuf_reset(m);
957 if ((r = sshbuf_put_u32(m, authok)) != 0)
958 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000959
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000960 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000961 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000962
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000963 auth_method = "keyboard-interactive";
964 auth_submethod = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000965
966 return (authok != 0);
967}
968#endif
969
970#ifdef SKEY
971int
Damien Miller120a1ec2018-07-10 19:39:52 +1000972mm_answer_skeyquery(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000973{
974 struct skey skey;
975 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100976 u_int success;
Darren Tucker79c9d352018-07-11 09:54:00 +1000977 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000978
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000979 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
980 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000981
Damien Miller120a1ec2018-07-10 19:39:52 +1000982 sshbuf_reset(m);
983 if ((r = sshbuf_put_u32(m, success)) != 0)
984 fatal("%s: buffer error: %s", __func__, ssh_err(r));
985 if (success) {
986 if ((r = sshbuf_put_cstring(m, challenge)) != 0)
987 fatal("%s: buffer error: %s", __func__, ssh_err(r));
988 }
Damien Millerb7df3af2003-02-24 11:55:46 +1100989 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000990 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000991
992 return (0);
993}
994
995int
Damien Miller120a1ec2018-07-10 19:39:52 +1000996mm_answer_skeyrespond(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000997{
998 char *response;
Damien Miller120a1ec2018-07-10 19:39:52 +1000999 size_t rlen;
Darren Tucker380320b2018-07-11 10:03:34 +10001000 int authok, r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001001
Damien Miller120a1ec2018-07-10 19:39:52 +10001002 if ((r = sshbuf_get_cstring(m, &response, &rlen)) != 0)
1003 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001004
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001005 authok = (options.challenge_response_authentication &&
1006 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001007 skey_haskey(authctxt->pw->pw_name) == 0 &&
1008 skey_passcheck(authctxt->pw->pw_name, response) != -1);
1009
Damien Miller120a1ec2018-07-10 19:39:52 +10001010 freezero(response, rlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001011
Damien Miller120a1ec2018-07-10 19:39:52 +10001012 sshbuf_reset(m);
1013 if ((r = sshbuf_put_u32(m, authok)) != 0)
1014 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001015
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001016 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001017 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001018
Damien Millerac947352015-11-20 12:35:41 +11001019 auth_method = "keyboard-interactive";
1020 auth_submethod = "skey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001021
1022 return (authok != 0);
1023}
1024#endif
1025
Damien Miller79418552002-04-23 20:28:48 +10001026#ifdef USE_PAM
1027int
Damien Miller120a1ec2018-07-10 19:39:52 +10001028mm_answer_pam_start(int sock, struct sshbuf *m)
Damien Miller79418552002-04-23 20:28:48 +10001029{
Damien Miller4e448a32003-05-14 15:11:48 +10001030 if (!options.use_pam)
1031 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1032
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001033 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001034
Damien Miller1f499fd2003-08-25 13:08:49 +10001035 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
Damien Miller775f8a22016-08-31 10:48:07 +10001036 if (options.kbd_interactive_authentication)
1037 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller1f499fd2003-08-25 13:08:49 +10001038
Damien Miller79418552002-04-23 20:28:48 +10001039 return (0);
1040}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001041
Damien Miller1f499fd2003-08-25 13:08:49 +10001042int
Damien Miller120a1ec2018-07-10 19:39:52 +10001043mm_answer_pam_account(int sock, struct sshbuf *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001044{
1045 u_int ret;
Damien Miller120a1ec2018-07-10 19:39:52 +10001046 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +11001047
Damien Miller1f499fd2003-08-25 13:08:49 +10001048 if (!options.use_pam)
Damien Miller775f8a22016-08-31 10:48:07 +10001049 fatal("%s: PAM not enabled", __func__);
Damien Miller1f499fd2003-08-25 13:08:49 +10001050
1051 ret = do_pam_account();
1052
Damien Miller120a1ec2018-07-10 19:39:52 +10001053 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1054 (r = sshbuf_put_stringb(m, loginmsg)) != 0)
1055 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller1f499fd2003-08-25 13:08:49 +10001056
Darren Tucker7eda5922004-06-22 13:22:50 +10001057 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001058
1059 return (ret);
1060}
1061
Damien Miller4f9f42a2003-05-10 19:28:02 +10001062static void *sshpam_ctxt, *sshpam_authok;
1063extern KbdintDevice sshpam_device;
1064
1065int
Damien Miller120a1ec2018-07-10 19:39:52 +10001066mm_answer_pam_init_ctx(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001067{
Damien Miller120a1ec2018-07-10 19:39:52 +10001068 u_int ok = 0;
1069 int r;
1070
Damien Miller4f9f42a2003-05-10 19:28:02 +10001071 debug3("%s", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001072 if (!options.kbd_interactive_authentication)
1073 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001074 if (sshpam_ctxt != NULL)
1075 fatal("%s: already called", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001076 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1077 sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001078 sshbuf_reset(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001079 if (sshpam_ctxt != NULL) {
1080 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
Damien Millerb38b95f2016-08-29 11:47:07 +10001081 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
Damien Miller120a1ec2018-07-10 19:39:52 +10001082 ok = 1;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001083 }
Damien Miller120a1ec2018-07-10 19:39:52 +10001084 if ((r = sshbuf_put_u32(m, ok)) != 0)
1085 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker7eda5922004-06-22 13:22:50 +10001086 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001087 return (0);
1088}
1089
1090int
Damien Miller120a1ec2018-07-10 19:39:52 +10001091mm_answer_pam_query(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001092{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001093 char *name = NULL, *info = NULL, **prompts = NULL;
1094 u_int i, num = 0, *echo_on = 0;
Damien Miller120a1ec2018-07-10 19:39:52 +10001095 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001096
1097 debug3("%s", __func__);
1098 sshpam_authok = NULL;
Damien Millerb38b95f2016-08-29 11:47:07 +10001099 if (sshpam_ctxt == NULL)
1100 fatal("%s: no context", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001101 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1102 &num, &prompts, &echo_on);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001103 if (ret == 0 && num == 0)
1104 sshpam_authok = sshpam_ctxt;
1105 if (num > 1 || name == NULL || info == NULL)
Damien Millerb38b95f2016-08-29 11:47:07 +10001106 fatal("sshpam_device.query failed");
1107 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
Damien Miller120a1ec2018-07-10 19:39:52 +10001108 sshbuf_reset(m);
1109 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1110 (r = sshbuf_put_cstring(m, name)) != 0 ||
1111 (r = sshbuf_put_cstring(m, info)) != 0 ||
1112 (r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0 ||
1113 (r = sshbuf_put_u32(m, num)) != 0)
1114 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +10001115 free(name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001116 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001117 for (i = 0; i < num; ++i) {
Damien Miller120a1ec2018-07-10 19:39:52 +10001118 if ((r = sshbuf_put_cstring(m, prompts[i])) != 0 ||
1119 (r = sshbuf_put_u32(m, echo_on[i])) != 0)
1120 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +10001121 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001122 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001123 free(prompts);
1124 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001125 auth_method = "keyboard-interactive";
1126 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001127 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001128 return (0);
1129}
1130
1131int
Damien Miller120a1ec2018-07-10 19:39:52 +10001132mm_answer_pam_respond(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001133{
1134 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001135 u_int i, num;
Damien Miller120a1ec2018-07-10 19:39:52 +10001136 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001137
1138 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001139 if (sshpam_ctxt == NULL)
1140 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001141 sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001142 if ((r = sshbuf_get_u32(m, &num)) != 0)
1143 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001144 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001145 resp = xcalloc(num, sizeof(char *));
Damien Miller120a1ec2018-07-10 19:39:52 +10001146 for (i = 0; i < num; ++i) {
1147 if ((r = sshbuf_get_cstring(m, &(resp[i]), NULL)) != 0)
1148 fatal("%s: buffer error: %s",
1149 __func__, ssh_err(r));
1150 }
Damien Miller4f9f42a2003-05-10 19:28:02 +10001151 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1152 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001153 free(resp[i]);
1154 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001155 } else {
1156 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1157 }
Damien Miller120a1ec2018-07-10 19:39:52 +10001158 sshbuf_reset(m);
1159 if ((r = sshbuf_put_u32(m, ret)) != 0)
1160 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker7eda5922004-06-22 13:22:50 +10001161 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001162 auth_method = "keyboard-interactive";
1163 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001164 if (ret == 0)
1165 sshpam_authok = sshpam_ctxt;
1166 return (0);
1167}
1168
1169int
Damien Miller120a1ec2018-07-10 19:39:52 +10001170mm_answer_pam_free_ctx(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001171{
Damien Miller5e75f512015-08-11 13:34:12 +10001172 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001173
1174 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001175 if (sshpam_ctxt == NULL)
1176 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001177 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001178 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001179 sshbuf_reset(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001180 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Millerb38b95f2016-08-29 11:47:07 +10001181 /* Allow another attempt */
1182 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller15b05cf2012-12-03 09:53:20 +11001183 auth_method = "keyboard-interactive";
1184 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001185 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001186}
Damien Miller79418552002-04-23 20:28:48 +10001187#endif
1188
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001189int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001190mm_answer_keyallowed(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001191{
djm@openbsd.org7c856852018-03-03 03:15:51 +00001192 struct ssh *ssh = active_state; /* XXX */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001193 struct sshkey *key = NULL;
Damien Millera10f5612002-09-12 09:49:15 +10001194 char *cuser, *chost;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001195 u_int pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001196 enum mm_keytype type = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001197 int r, allowed = 0;
1198 struct sshauthopt *opts = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001199
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001200 debug3("%s entering", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001201 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1202 (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
1203 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
1204 (r = sshkey_froms(m, &key)) != 0 ||
1205 (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
1206 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001207
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001208 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001209
Damien Miller3e3b5142003-11-17 21:13:40 +11001210 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001211 /* These should not make it past the privsep child */
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001212 if (sshkey_type_plain(key->type) == KEY_RSA &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001213 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1214 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1215
Darren Tucker47eede72005-03-14 23:08:12 +11001216 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001217 case MM_USERKEY:
Damien Miller7a8f5b32006-03-31 23:14:23 +11001218 auth_method = "publickey";
djm@openbsd.org7c856852018-03-03 03:15:51 +00001219 if (!options.pubkey_authentication)
1220 break;
1221 if (auth2_key_already_used(authctxt, key))
1222 break;
1223 if (match_pattern_list(sshkey_ssh_name(key),
1224 options.pubkey_key_types, 0) != 1)
1225 break;
1226 allowed = user_key_allowed(ssh, authctxt->pw, key,
1227 pubkey_auth_attempt, &opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001228 break;
1229 case MM_HOSTKEY:
djm@openbsd.org7c856852018-03-03 03:15:51 +00001230 auth_method = "hostbased";
1231 if (!options.hostbased_authentication)
1232 break;
1233 if (auth2_key_already_used(authctxt, key))
1234 break;
1235 if (match_pattern_list(sshkey_ssh_name(key),
1236 options.hostbased_key_types, 0) != 1)
1237 break;
1238 allowed = hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001239 cuser, chost, key);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001240 auth2_record_info(authctxt,
Damien Miller20bdcd72013-07-18 16:10:09 +10001241 "client user \"%.100s\", client host \"%.100s\"",
1242 cuser, chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001243 break;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001244 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001245 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001246 break;
1247 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001248 }
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001249
djm@openbsd.org7c856852018-03-03 03:15:51 +00001250 debug3("%s: %s authentication%s: %s key is %s", __func__,
1251 auth_method, pubkey_auth_attempt ? "" : " test",
1252 (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
1253 allowed ? "allowed" : "not allowed");
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001254
djm@openbsd.org8f574952017-06-24 06:34:38 +00001255 auth2_record_key(authctxt, 0, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001256
1257 /* clear temporarily storage (used by verify) */
1258 monitor_reset_key_state();
1259
1260 if (allowed) {
1261 /* Save temporarily for comparison in verify */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001262 if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
1263 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001264 key_blobtype = type;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001265 key_opts = opts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001266 hostbased_cuser = cuser;
1267 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001268 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001269 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001270 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001271 free(cuser);
1272 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001273 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001274 sshkey_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001275
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001276 sshbuf_reset(m);
1277 if ((r = sshbuf_put_u32(m, allowed)) != 0)
1278 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org7c856852018-03-03 03:15:51 +00001279 if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
1280 fatal("%s: sshauthopt_serialise: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001281 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001282
djm@openbsd.org7c856852018-03-03 03:15:51 +00001283 if (!allowed)
1284 sshauthopt_free(opts);
1285
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001286 return (0);
1287}
1288
1289static int
1290monitor_valid_userblob(u_char *data, u_int datalen)
1291{
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001292 struct sshbuf *b;
1293 const u_char *p;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001294 char *userstyle, *cp;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001295 size_t len;
1296 u_char type;
1297 int r, fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001298
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001299 if ((b = sshbuf_new()) == NULL)
1300 fatal("%s: sshbuf_new", __func__);
1301 if ((r = sshbuf_put(b, data, datalen)) != 0)
1302 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001303
1304 if (datafellows & SSH_OLD_SESSIONID) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001305 p = sshbuf_ptr(b);
1306 len = sshbuf_len(b);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001307 if ((session_id2 == NULL) ||
1308 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001309 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001310 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001311 if ((r = sshbuf_consume(b, session_id2_len)) != 0)
1312 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001313 } else {
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001314 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1315 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromf67e0772002-06-06 20:58:19 +00001316 if ((session_id2 == NULL) ||
1317 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001318 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001319 fail++;
1320 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001321 if ((r = sshbuf_get_u8(b, &type)) != 0)
1322 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1323 if (type != SSH2_MSG_USERAUTH_REQUEST)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001324 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001325 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1326 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4ce189d2013-04-23 15:17:52 +10001327 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1328 authctxt->style ? ":" : "",
1329 authctxt->style ? authctxt->style : "");
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001330 if (strcmp(userstyle, cp) != 0) {
1331 logit("wrong user name passed to monitor: "
1332 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001333 fail++;
1334 }
Darren Tuckera627d422013-06-02 07:31:17 +10001335 free(userstyle);
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001336 free(cp);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001337 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1338 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1339 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001340 if (strcmp("publickey", cp) != 0)
1341 fail++;
1342 free(cp);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001343 if ((r = sshbuf_get_u8(b, &type)) != 0)
1344 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1345 if (type == 0)
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001346 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001347 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1348 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1349 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1350 if (sshbuf_len(b) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001351 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001352 sshbuf_free(b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001353 return (fail == 0);
1354}
1355
1356static int
Damien Millera10f5612002-09-12 09:49:15 +10001357monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1358 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001359{
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001360 struct sshbuf *b;
1361 const u_char *p;
1362 char *cp, *userstyle;
1363 size_t len;
1364 int r, fail = 0;
1365 u_char type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001366
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001367 if ((b = sshbuf_new()) == NULL)
1368 fatal("%s: sshbuf_new", __func__);
1369 if ((r = sshbuf_put(b, data, datalen)) != 0 ||
1370 (r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1371 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001372
Ben Lindstromf67e0772002-06-06 20:58:19 +00001373 if ((session_id2 == NULL) ||
1374 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001375 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001376 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001377
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001378 if ((r = sshbuf_get_u8(b, &type)) != 0)
1379 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1380 if (type != SSH2_MSG_USERAUTH_REQUEST)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001381 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001382 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1383 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4ce189d2013-04-23 15:17:52 +10001384 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1385 authctxt->style ? ":" : "",
1386 authctxt->style ? authctxt->style : "");
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001387 if (strcmp(userstyle, cp) != 0) {
1388 logit("wrong user name passed to monitor: "
1389 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001390 fail++;
1391 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001392 free(userstyle);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001393 free(cp);
1394 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1395 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1396 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1397 if (strcmp(cp, "hostbased") != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001398 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001399 free(cp);
1400 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1401 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1402 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001403
1404 /* verify client host, strip trailing dot if necessary */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001405 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1406 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1407 if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
1408 cp[len - 1] = '\0';
1409 if (strcmp(cp, chost) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001410 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001411 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001412
1413 /* verify client user */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001414 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1415 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1416 if (strcmp(cp, cuser) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001417 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001418 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001419
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001420 if (sshbuf_len(b) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001421 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001422 sshbuf_free(b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001423 return (fail == 0);
1424}
1425
1426int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001427mm_answer_keyverify(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428{
djm@openbsd.org7c856852018-03-03 03:15:51 +00001429 struct ssh *ssh = active_state; /* XXX */
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001430 struct sshkey *key;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001431 u_char *signature, *data, *blob;
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001432 char *sigalg;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001433 size_t signaturelen, datalen, bloblen;
1434 int r, ret, valid_data = 0, encoded_ret;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001435
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001436 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1437 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001438 (r = sshbuf_get_string(m, &data, &datalen)) != 0 ||
1439 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001440 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001441
1442 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001443 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001444 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001445
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001446 /* Empty signature algorithm means NULL. */
1447 if (*sigalg == '\0') {
1448 free(sigalg);
1449 sigalg = NULL;
1450 }
1451
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001452 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1453 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1454 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001455
1456 switch (key_blobtype) {
1457 case MM_USERKEY:
1458 valid_data = monitor_valid_userblob(data, datalen);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001459 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001460 break;
1461 case MM_HOSTKEY:
1462 valid_data = monitor_valid_hostbasedblob(data, datalen,
1463 hostbased_cuser, hostbased_chost);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001464 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001465 break;
1466 default:
1467 valid_data = 0;
1468 break;
1469 }
1470 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001471 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001472
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001473 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001474 sigalg, active_state->compat);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001475 debug3("%s: %s %p signature %s", __func__, auth_method, key,
1476 (ret == 0) ? "verified" : "unverified");
1477 auth2_record_key(authctxt, ret == 0, key);
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001478
Darren Tuckera627d422013-06-02 07:31:17 +10001479 free(blob);
1480 free(signature);
1481 free(data);
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001482 free(sigalg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001483
djm@openbsd.org7c856852018-03-03 03:15:51 +00001484 if (key_blobtype == MM_USERKEY)
1485 auth_activate_options(ssh, key_opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001486 monitor_reset_key_state();
1487
djm@openbsd.org8f574952017-06-24 06:34:38 +00001488 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001489 sshbuf_reset(m);
1490
1491 /* encode ret != 0 as positive integer, since we're sending u32 */
1492 encoded_ret = (ret != 0);
1493 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1494 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001495 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001496
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001497 return ret == 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001498}
1499
1500static void
1501mm_record_login(Session *s, struct passwd *pw)
1502{
djm@openbsd.org95767262016-03-07 19:02:43 +00001503 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001504 socklen_t fromlen;
1505 struct sockaddr_storage from;
1506
1507 /*
1508 * Get IP address of client. If the connection is not a socket, let
1509 * the address be 0.0.0.0.
1510 */
1511 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001512 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001513 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001514 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001515 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001516 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001517 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001518 }
1519 }
1520 /* Record that there was a login on that tty from the remote host. */
1521 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +00001522 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001523 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001524}
1525
1526static void
1527mm_session_close(Session *s)
1528{
Damien Miller04bd8b02003-05-25 14:38:33 +10001529 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001530 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001531 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532 session_pty_cleanup2(s);
1533 }
Damien Miller7207f642008-05-19 15:34:50 +10001534 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001535}
1536
1537int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001538mm_answer_pty(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001539{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001540 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001541 Session *s;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001542 int r, res, fd0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001543
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001544 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001545
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001546 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001547 s = session_new();
1548 if (s == NULL)
1549 goto error;
1550 s->authctxt = authctxt;
1551 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001552 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001553 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1554 if (res == 0)
1555 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001556 pty_setowner(authctxt->pw, s->tty);
1557
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001558 if ((r = sshbuf_put_u32(m, 1)) != 0 ||
1559 (r = sshbuf_put_cstring(m, s->tty)) != 0)
1560 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001561
1562 /* We need to trick ttyslot */
1563 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001564 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001565
1566 mm_record_login(s, authctxt->pw);
1567
1568 /* Now we can close the file descriptor again */
1569 close(0);
1570
Darren Tucker09991742004-07-17 17:05:14 +10001571 /* send messages generated by record_login */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001572 if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
1573 fatal("%s: put login message: %s", __func__, ssh_err(r));
1574 sshbuf_reset(loginmsg);
Darren Tucker09991742004-07-17 17:05:14 +10001575
1576 mm_request_send(sock, MONITOR_ANS_PTY, m);
1577
Damien Miller54fd7cf2007-09-17 12:04:08 +10001578 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1579 mm_send_fd(sock, s->ttyfd) == -1)
1580 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001581
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001582 /* make sure nothing uses fd 0 */
1583 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001584 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001585 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001586 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001587
1588 /* slave is not needed */
1589 close(s->ttyfd);
1590 s->ttyfd = s->ptyfd;
1591 /* no need to dup() because nobody closes ptyfd */
1592 s->ptymaster = s->ptyfd;
1593
Damien Miller9ab00b42006-08-05 12:40:11 +10001594 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001595
1596 return (0);
1597
1598 error:
1599 if (s != NULL)
1600 mm_session_close(s);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001601 if ((r = sshbuf_put_u32(m, 0)) != 0)
1602 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001603 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001604 return (0);
1605}
1606
1607int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001608mm_answer_pty_cleanup(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001609{
1610 Session *s;
1611 char *tty;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001612 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001613
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001614 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001615
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001616 if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
1617 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001618 if ((s = session_by_tty(tty)) != NULL)
1619 mm_session_close(s);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001620 sshbuf_reset(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001621 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001622 return (0);
1623}
1624
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001625int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001626mm_answer_term(int sock, struct sshbuf *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001627{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001628 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001629 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001630 int res, status;
1631
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001632 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001633
1634 /* The child is terminating */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001635 session_destroy_all(ssh, &mm_session_close);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001636
Darren Tucker52358d62008-03-11 22:58:25 +11001637#ifdef USE_PAM
1638 if (options.use_pam)
1639 sshpam_cleanup();
1640#endif
1641
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001642 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001643 if (errno != EINTR)
1644 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001645
1646 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1647
1648 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001649 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001650}
1651
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001652#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001653/* Report that an audit event occurred */
1654int
Damien Miller120a1ec2018-07-10 19:39:52 +10001655mm_answer_audit_event(int socket, struct sshbuf *m)
Darren Tucker269a1ea2005-02-03 00:20:53 +11001656{
Damien Miller120a1ec2018-07-10 19:39:52 +10001657 u_int n;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001658 ssh_audit_event_t event;
Damien Miller120a1ec2018-07-10 19:39:52 +10001659 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001660
1661 debug3("%s entering", __func__);
1662
Damien Miller120a1ec2018-07-10 19:39:52 +10001663 if ((r = sshbuf_get_u32(m, &n)) != 0)
1664 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1665 event = (ssh_audit_event_t)n;
1666 switch (event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001667 case SSH_AUTH_FAIL_PUBKEY:
1668 case SSH_AUTH_FAIL_HOSTBASED:
1669 case SSH_AUTH_FAIL_GSSAPI:
1670 case SSH_LOGIN_EXCEED_MAXTRIES:
1671 case SSH_LOGIN_ROOT_DENIED:
1672 case SSH_CONNECTION_CLOSE:
1673 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001674 audit_event(event);
1675 break;
1676 default:
1677 fatal("Audit event type %d not permitted", event);
1678 }
1679
1680 return (0);
1681}
1682
1683int
Damien Miller120a1ec2018-07-10 19:39:52 +10001684mm_answer_audit_command(int socket, struct sshbuf *m)
Darren Tucker269a1ea2005-02-03 00:20:53 +11001685{
Darren Tucker269a1ea2005-02-03 00:20:53 +11001686 char *cmd;
Damien Miller120a1ec2018-07-10 19:39:52 +10001687 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001688
1689 debug3("%s entering", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +10001690 if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
1691 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker269a1ea2005-02-03 00:20:53 +11001692 /* sanity check command, if so how? */
1693 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001694 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001695 return (0);
1696}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001697#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001698
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001699void
markus@openbsd.org1e0cdf82017-05-31 08:09:45 +00001700monitor_clear_keystate(struct monitor *pmonitor)
1701{
1702 struct ssh *ssh = active_state; /* XXX */
1703
1704 ssh_clear_newkeys(ssh, MODE_IN);
1705 ssh_clear_newkeys(ssh, MODE_OUT);
1706 sshbuf_free(child_state);
1707 child_state = NULL;
1708}
1709
1710void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001711monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001712{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001713 struct ssh *ssh = active_state; /* XXX */
1714 struct kex *kex;
1715 int r;
1716
1717 debug3("%s: packet_set_state", __func__);
1718 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1719 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1720 sshbuf_free(child_state);
1721 child_state = NULL;
1722
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +00001723 if ((kex = ssh->kex) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001724 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001725#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001726 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1727 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +00001728 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1729 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1730 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001731 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1732 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001733# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001734 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001735# endif
Damien Miller773dda22015-01-30 23:10:17 +11001736#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001737 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1738 kex->load_host_public_key=&get_hostkey_public_by_type;
1739 kex->load_host_private_key=&get_hostkey_private_by_type;
1740 kex->host_key_index=&get_hostkey_index;
1741 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001742 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001743}
1744
1745/* This function requries careful sanity checking */
1746
1747void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001748mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001749{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001750 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001751
markus@openbsd.org091c3022015-01-19 19:52:16 +00001752 if ((child_state = sshbuf_new()) == NULL)
1753 fatal("%s: sshbuf_new failed", __func__);
1754 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1755 child_state);
1756 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001757}
1758
1759
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001760/* XXX */
1761
1762#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001763 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001764 fatal("fcntl(%d, F_SETFD)", x); \
1765} while (0)
1766
1767static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001768monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001769{
Damien Miller8f0bf232011-06-20 14:42:23 +10001770 int pair[2];
markus@openbsd.org84008602017-05-31 10:04:29 +00001771#ifdef SO_ZEROIZE
1772 int on = 1;
1773#endif
Damien Miller8f0bf232011-06-20 14:42:23 +10001774
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001775 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001776 fatal("%s: socketpair: %s", __func__, strerror(errno));
markus@openbsd.org84008602017-05-31 10:04:29 +00001777#ifdef SO_ZEROIZE
1778 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1779 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1780 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1781 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1782#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001783 FD_CLOSEONEXEC(pair[0]);
1784 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001785 mon->m_recvfd = pair[0];
1786 mon->m_sendfd = pair[1];
1787
1788 if (do_logfds) {
1789 if (pipe(pair) == -1)
1790 fatal("%s: pipe: %s", __func__, strerror(errno));
1791 FD_CLOSEONEXEC(pair[0]);
1792 FD_CLOSEONEXEC(pair[1]);
1793 mon->m_log_recvfd = pair[0];
1794 mon->m_log_sendfd = pair[1];
1795 } else
1796 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001797}
1798
1799#define MM_MEMSIZE 65536
1800
1801struct monitor *
1802monitor_init(void)
1803{
1804 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001805
Damien Miller07d86be2006-03-26 14:19:21 +11001806 mon = xcalloc(1, sizeof(*mon));
Damien Miller8f0bf232011-06-20 14:42:23 +10001807 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001808
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001809 return mon;
1810}
1811
1812void
1813monitor_reinit(struct monitor *mon)
1814{
Damien Miller8f0bf232011-06-20 14:42:23 +10001815 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001816}
Darren Tucker0efd1552003-08-26 11:49:55 +10001817
1818#ifdef GSSAPI
1819int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001820mm_answer_gss_setup_ctx(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001821{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001822 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001823 OM_uint32 major;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001824 size_t len;
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001825 u_char *p;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001826 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +10001827
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001828 if (!options.gss_authentication)
1829 fatal("%s: GSSAPI authentication not enabled", __func__);
1830
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001831 if ((r = sshbuf_get_string(m, &p, &len)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001832 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001833 goid.elements = p;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001834 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001835
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001836 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001837
Darren Tuckera627d422013-06-02 07:31:17 +10001838 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001839
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001840 sshbuf_reset(m);
1841 if ((r = sshbuf_put_u32(m, major)) != 0)
1842 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001843
Damien Miller6fd6def2005-11-05 15:07:05 +11001844 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001845
1846 /* Now we have a context, enable the step */
1847 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1848
1849 return (0);
1850}
1851
1852int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001853mm_answer_gss_accept_ctx(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001854{
1855 gss_buffer_desc in;
1856 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001857 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001858 OM_uint32 flags = 0; /* GSI needs this */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001859 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +10001860
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001861 if (!options.gss_authentication)
1862 fatal("%s: GSSAPI authentication not enabled", __func__);
1863
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001864 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001865 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001866 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001867 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001868
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001869 sshbuf_reset(m);
1870 if ((r = sshbuf_put_u32(m, major)) != 0 ||
1871 (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
1872 (r = sshbuf_put_u32(m, flags)) != 0)
1873 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001874 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001875
1876 gss_release_buffer(&minor, &out);
1877
Damien Miller6fd6def2005-11-05 15:07:05 +11001878 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001879 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1880 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001881 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001882 }
1883 return (0);
1884}
1885
1886int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001887mm_answer_gss_checkmic(int sock, struct sshbuf *m)
Damien Miller0425d402003-11-17 22:18:21 +11001888{
1889 gss_buffer_desc gssbuf, mic;
1890 OM_uint32 ret;
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001891 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +11001892
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001893 if (!options.gss_authentication)
1894 fatal("%s: GSSAPI authentication not enabled", __func__);
1895
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001896 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
1897 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001898 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller787b2ec2003-11-21 23:56:47 +11001899
Damien Miller0425d402003-11-17 22:18:21 +11001900 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001901
Darren Tuckera627d422013-06-02 07:31:17 +10001902 free(gssbuf.value);
1903 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001904
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001905 sshbuf_reset(m);
1906 if ((r = sshbuf_put_u32(m, ret)) != 0)
1907 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller787b2ec2003-11-21 23:56:47 +11001908
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001909 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001910
Damien Miller0425d402003-11-17 22:18:21 +11001911 if (!GSS_ERROR(ret))
1912 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001913
Damien Miller0425d402003-11-17 22:18:21 +11001914 return (0);
1915}
1916
1917int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001918mm_answer_gss_userok(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001919{
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001920 int r, authenticated;
djm@openbsd.org8f574952017-06-24 06:34:38 +00001921 const char *displayname;
Darren Tucker0efd1552003-08-26 11:49:55 +10001922
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001923 if (!options.gss_authentication)
1924 fatal("%s: GSSAPI authentication not enabled", __func__);
1925
Darren Tucker0efd1552003-08-26 11:49:55 +10001926 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1927
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001928 sshbuf_reset(m);
1929 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1930 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001931
1932 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001933 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001934
Damien Miller6fd6def2005-11-05 15:07:05 +11001935 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001936
djm@openbsd.org8f574952017-06-24 06:34:38 +00001937 if ((displayname = ssh_gssapi_displayname()) != NULL)
1938 auth2_record_info(authctxt, "%s", displayname);
1939
Darren Tucker0efd1552003-08-26 11:49:55 +10001940 /* Monitor loop will terminate if authenticated */
1941 return (authenticated);
1942}
1943#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001944