blob: 96d10913c61b366c7b6172080bc37fbfa8db2292 [file] [log] [blame]
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001/* $OpenBSD: monitor.c,v 1.198 2019/06/28 13:35:04 deraadt Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100031#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110032#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000033
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100035#include <fcntl.h>
djm@openbsd.org1a31d022016-05-02 08:49:03 +000036#include <limits.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110037#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110038#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110039#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100040#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110041#include <signal.h>
Damien Miller9af21972015-02-24 09:04:32 +110042#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000043#include <stdint.h>
Damien Miller9af21972015-02-24 09:04:32 +110044#endif
Damien Millere7a1e5c2006-08-05 11:34:19 +100045#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100046#include <string.h>
Damien Miller86687062014-07-02 15:28:02 +100047#include <stdarg.h>
48#include <stdio.h>
Damien Miller607aede2006-09-01 15:48:19 +100049#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100050#ifdef HAVE_POLL_H
51#include <poll.h>
52#else
53# ifdef HAVE_SYS_POLL_H
54# include <sys/poll.h>
55# endif
56#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000057
Damien Miller1f0311c2014-05-15 14:24:09 +100058#ifdef WITH_OPENSSL
Damien Miller03e20032006-03-15 11:16:59 +110059#include <openssl/dh.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100060#endif
Damien Miller03e20032006-03-15 11:16:59 +110061
Damien Miller48f54b92018-09-13 12:13:50 +100062#include "openbsd-compat/sys-tree.h"
Damien Millerb84886b2008-05-19 15:05:07 +100063#include "openbsd-compat/sys-queue.h"
Damien Miller48f54b92018-09-13 12:13:50 +100064#include "openbsd-compat/openssl-compat.h"
65
Damien Miller8f0bf232011-06-20 14:42:23 +100066#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100067#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000068#include "ssh.h"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +000069#include "sshkey.h"
markus@openbsd.org235c7c42018-07-09 21:53:45 +000070#include "sshbuf.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000072#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100073#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000074#include "kex.h"
75#include "dh.h"
Darren Tucker01558b72016-07-18 09:33:25 +100076#include "auth-pam.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000077#include "packet.h"
78#include "auth-options.h"
79#include "sshpty.h"
80#include "channels.h"
81#include "session.h"
82#include "sshlogin.h"
83#include "canohost.h"
84#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100085#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000086#include "servconf.h"
87#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +100088#ifdef GSSAPI
89#include "ssh-gss.h"
90#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000091#include "monitor_wrap.h"
92#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093#include "compat.h"
94#include "ssh2.h"
Damien Miller85b45e02013-07-20 13:21:52 +100095#include "authfd.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +000096#include "match.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +000097#include "ssherr.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000098
Darren Tucker0efd1552003-08-26 11:49:55 +100099#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000100static Gssctxt *gsscontext = NULL;
101#endif
102
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000103/* Imports */
104extern ServerOptions options;
105extern u_int utmp_len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000106extern u_char session_id[];
markus@openbsd.org2808d182018-07-09 21:26:02 +0000107extern struct sshbuf *loginmsg;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000108extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000109
110/* State exported from the child */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000111static struct sshbuf *child_state;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000112
Damien Miller469954d2003-06-18 20:25:33 +1000113/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000114
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000115int mm_answer_moduli(struct ssh *, int, struct sshbuf *);
116int mm_answer_sign(struct ssh *, int, struct sshbuf *);
117int mm_answer_pwnamallow(struct ssh *, int, struct sshbuf *);
118int mm_answer_auth2_read_banner(struct ssh *, int, struct sshbuf *);
119int mm_answer_authserv(struct ssh *, int, struct sshbuf *);
120int mm_answer_authpassword(struct ssh *, int, struct sshbuf *);
121int mm_answer_bsdauthquery(struct ssh *, int, struct sshbuf *);
122int mm_answer_bsdauthrespond(struct ssh *, int, struct sshbuf *);
123int mm_answer_skeyquery(struct ssh *, int, struct sshbuf *);
124int mm_answer_skeyrespond(struct ssh *, int, struct sshbuf *);
125int mm_answer_keyallowed(struct ssh *, int, struct sshbuf *);
126int mm_answer_keyverify(struct ssh *, int, struct sshbuf *);
127int mm_answer_pty(struct ssh *, int, struct sshbuf *);
128int mm_answer_pty_cleanup(struct ssh *, int, struct sshbuf *);
129int mm_answer_term(struct ssh *, int, struct sshbuf *);
130int mm_answer_rsa_keyallowed(struct ssh *, int, struct sshbuf *);
131int mm_answer_rsa_challenge(struct ssh *, int, struct sshbuf *);
132int mm_answer_rsa_response(struct ssh *, int, struct sshbuf *);
133int mm_answer_sesskey(struct ssh *, int, struct sshbuf *);
134int mm_answer_sessid(struct ssh *, int, struct sshbuf *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000135
Damien Miller79418552002-04-23 20:28:48 +1000136#ifdef USE_PAM
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000137int mm_answer_pam_start(struct ssh *, int, struct sshbuf *);
138int mm_answer_pam_account(struct ssh *, int, struct sshbuf *);
139int mm_answer_pam_init_ctx(struct ssh *, int, struct sshbuf *);
140int mm_answer_pam_query(struct ssh *, int, struct sshbuf *);
141int mm_answer_pam_respond(struct ssh *, int, struct sshbuf *);
142int mm_answer_pam_free_ctx(struct ssh *, int, struct sshbuf *);
Damien Miller79418552002-04-23 20:28:48 +1000143#endif
144
Darren Tucker0efd1552003-08-26 11:49:55 +1000145#ifdef GSSAPI
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000146int mm_answer_gss_setup_ctx(struct ssh *, int, struct sshbuf *);
147int mm_answer_gss_accept_ctx(struct ssh *, int, struct sshbuf *);
148int mm_answer_gss_userok(struct ssh *, int, struct sshbuf *);
149int mm_answer_gss_checkmic(struct ssh *, int, struct sshbuf *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000150#endif
Damien Miller25162f22002-09-12 09:47:29 +1000151
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100152#ifdef SSH_AUDIT_EVENTS
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000153int mm_answer_audit_event(struct ssh *, int, struct sshbuf *);
154int mm_answer_audit_command(struct ssh *, int, struct sshbuf *);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100155#endif
156
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000157static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000158
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000159/* local state for key verify */
160static u_char *key_blob = NULL;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000161static size_t key_bloblen = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000162static int key_blobtype = MM_NOKEY;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000163static struct sshauthopt *key_opts = NULL;
Damien Millera10f5612002-09-12 09:49:15 +1000164static char *hostbased_cuser = NULL;
165static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000166static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100167static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000168static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000169static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000170static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171
172struct mon_table {
173 enum monitor_reqtype type;
174 int flags;
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000175 int (*f)(struct ssh *, int, struct sshbuf *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000176};
177
178#define MON_ISAUTH 0x0004 /* Required for Authentication */
179#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
180#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100181#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000182
183#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
184
185#define MON_PERMIT 0x1000 /* Request is permitted */
186
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000187static int monitor_read(struct ssh *, struct monitor *, struct mon_table *,
188 struct mon_table **);
189static int monitor_read_log(struct monitor *);
190
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000191struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000192#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000193 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000194#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000195 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
196 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
197 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000198 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000199 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000200#ifdef USE_PAM
201 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000202 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Millerb38b95f2016-08-29 11:47:07 +1000203 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
204 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
205 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000206 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000207#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100208#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100209 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100210#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000211#ifdef BSD_AUTH
212 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100213 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000214#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000215 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
216 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000217#ifdef GSSAPI
218 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
djm@openbsd.orgb33ad6d2016-09-05 13:57:31 +0000219 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
220 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
221 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000222#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000223 {0, 0, NULL}
224};
225
226struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000227#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000228 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000229#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000230 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
231 {MONITOR_REQ_PTY, 0, mm_answer_pty},
232 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
233 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100234#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100235 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
236 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
237#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000238 {0, 0, NULL}
239};
240
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000241struct mon_table *mon_dispatch;
242
243/* Specifies if a certain message is allowed at the moment */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000244static void
245monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
246{
247 while (ent->f != NULL) {
248 if (ent->type == type) {
249 ent->flags &= ~MON_PERMIT;
250 ent->flags |= permit ? MON_PERMIT : 0;
251 return;
252 }
253 ent++;
254 }
255}
256
257static void
258monitor_permit_authentications(int permit)
259{
260 struct mon_table *ent = mon_dispatch;
261
262 while (ent->f != NULL) {
263 if (ent->flags & MON_AUTH) {
264 ent->flags &= ~MON_PERMIT;
265 ent->flags |= permit ? MON_PERMIT : 0;
266 }
267 ent++;
268 }
269}
270
Darren Tucker3e33cec2003-10-02 16:12:36 +1000271void
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000272monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000273{
274 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100275 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000276
277 debug3("preauth child monitor started");
278
tb@openbsd.org34843802018-02-05 05:37:46 +0000279 if (pmonitor->m_recvfd >= 0)
280 close(pmonitor->m_recvfd);
281 if (pmonitor->m_log_sendfd >= 0)
282 close(pmonitor->m_log_sendfd);
Damien Miller8f0bf232011-06-20 14:42:23 +1000283 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
284
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000285 authctxt = (Authctxt *)ssh->authctxt;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000286 memset(authctxt, 0, sizeof(*authctxt));
djm@openbsd.org7c856852018-03-03 03:15:51 +0000287 ssh->authctxt = authctxt;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000288
Damien Miller120a1ec2018-07-10 19:39:52 +1000289 authctxt->loginmsg = loginmsg;
Darren Tuckerde0de392005-03-31 23:52:04 +1000290
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000291 mon_dispatch = mon_dispatch_proto20;
292 /* Permit requests for moduli and signatures */
293 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
294 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000295
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000296 /* The first few requests do not require asynchronous access */
297 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100298 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100299 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100300 auth_submethod = NULL;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000301 auth2_authctxt_reset_info(authctxt);
302
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000303 authenticated = (monitor_read(ssh, pmonitor,
304 mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100305
306 /* Special handling for multiple required authentications */
307 if (options.num_auth_methods != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100308 if (authenticated &&
309 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000310 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100311 debug3("%s: method %s: partial", __func__,
312 auth_method);
313 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100314 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100315 }
316 }
317
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000318 if (authenticated) {
319 if (!(ent->flags & MON_AUTHDECIDE))
320 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000321 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000322 if (authctxt->pw->pw_uid == 0 &&
djm@openbsd.org7c856852018-03-03 03:15:51 +0000323 !auth_root_allowed(ssh, auth_method))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000324 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000325#ifdef USE_PAM
326 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100327 if (options.use_pam && authenticated) {
Damien Miller120a1ec2018-07-10 19:39:52 +1000328 struct sshbuf *m;
Damien Miller1f499fd2003-08-25 13:08:49 +1000329
Damien Miller120a1ec2018-07-10 19:39:52 +1000330 if ((m = sshbuf_new()) == NULL)
331 fatal("%s: sshbuf_new failed",
332 __func__);
Damien Millera8e06ce2003-11-21 23:48:55 +1100333 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller120a1ec2018-07-10 19:39:52 +1000334 MONITOR_REQ_PAM_ACCOUNT, m);
335 authenticated = mm_answer_pam_account(
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000336 ssh, pmonitor->m_sendfd, m);
Damien Miller120a1ec2018-07-10 19:39:52 +1000337 sshbuf_free(m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000338 }
339#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000340 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100341 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
djm@openbsd.org3a00a922019-01-19 21:41:18 +0000342 auth_log(ssh, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000343 auth_method, auth_submethod);
djm@openbsd.org94885382015-06-22 23:42:16 +0000344 if (!partial && !authenticated)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000345 authctxt->failures++;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000346 if (authenticated || partial) {
347 auth2_update_session_info(authctxt,
348 auth_method, auth_submethod);
349 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000350 }
351 }
352
353 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000354 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100355 if (strcmp(auth_method, "unknown") == 0)
356 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357
358 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000359 __func__, authctxt->user);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000360 ssh->authctxt = NULL;
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000361 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000362
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000363 mm_get_keystate(ssh, pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000364
Damien Miller6a1937e2012-12-12 10:44:38 +1100365 /* Drain any buffered messages from the child */
366 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
367 ;
368
tb@openbsd.org34843802018-02-05 05:37:46 +0000369 if (pmonitor->m_recvfd >= 0)
370 close(pmonitor->m_recvfd);
371 if (pmonitor->m_log_sendfd >= 0)
372 close(pmonitor->m_log_sendfd);
Damien Miller8f0bf232011-06-20 14:42:23 +1000373 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000374}
375
Damien Millerbe64d432003-05-14 19:31:12 +1000376static void
377monitor_set_child_handler(pid_t pid)
378{
379 monitor_child_pid = pid;
380}
381
382static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000383monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000384{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000385 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000386}
387
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000388void
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000389monitor_child_postauth(struct ssh *ssh, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000390{
Damien Miller8f0bf232011-06-20 14:42:23 +1000391 close(pmonitor->m_recvfd);
392 pmonitor->m_recvfd = -1;
393
Damien Millerbe64d432003-05-14 19:31:12 +1000394 monitor_set_child_handler(pmonitor->m_pid);
395 signal(SIGHUP, &monitor_child_handler);
396 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000397 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000398#ifdef SIGXFSZ
399 signal(SIGXFSZ, SIG_IGN);
400#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000401
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000402 mon_dispatch = mon_dispatch_postauth20;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000403
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000404 /* Permit requests for moduli and signatures */
405 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
406 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
407 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
408
djm@openbsd.org7c856852018-03-03 03:15:51 +0000409 if (auth_opts->permit_pty_flag) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000410 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
411 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
412 }
413
414 for (;;)
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000415 monitor_read(ssh, pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000416}
417
Damien Miller8f0bf232011-06-20 14:42:23 +1000418static int
419monitor_read_log(struct monitor *pmonitor)
420{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000421 struct sshbuf *logmsg;
Damien Miller8f0bf232011-06-20 14:42:23 +1000422 u_int len, level;
423 char *msg;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000424 u_char *p;
425 int r;
Damien Miller8f0bf232011-06-20 14:42:23 +1000426
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000427 if ((logmsg = sshbuf_new()) == NULL)
428 fatal("%s: sshbuf_new", __func__);
Damien Miller8f0bf232011-06-20 14:42:23 +1000429
430 /* Read length */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000431 if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
432 fatal("%s: reserve: %s", __func__, ssh_err(r));
433 if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
Damien Miller8f0bf232011-06-20 14:42:23 +1000434 if (errno == EPIPE) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000435 sshbuf_free(logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000436 debug("%s: child log fd closed", __func__);
437 close(pmonitor->m_log_recvfd);
438 pmonitor->m_log_recvfd = -1;
439 return -1;
440 }
441 fatal("%s: log fd read: %s", __func__, strerror(errno));
442 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000443 if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
444 fatal("%s: get len: %s", __func__, ssh_err(r));
Damien Miller8f0bf232011-06-20 14:42:23 +1000445 if (len <= 4 || len > 8192)
446 fatal("%s: invalid log message length %u", __func__, len);
447
448 /* Read severity, message */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000449 sshbuf_reset(logmsg);
450 if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
451 fatal("%s: reserve: %s", __func__, ssh_err(r));
452 if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
Damien Miller8f0bf232011-06-20 14:42:23 +1000453 fatal("%s: log fd read: %s", __func__, strerror(errno));
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000454 if ((r = sshbuf_get_u32(logmsg, &level)) != 0 ||
455 (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
456 fatal("%s: decode: %s", __func__, ssh_err(r));
Damien Miller8f0bf232011-06-20 14:42:23 +1000457
458 /* Log it */
Damien Miller8f0bf232011-06-20 14:42:23 +1000459 if (log_level_name(level) == NULL)
460 fatal("%s: invalid log level %u (corrupted message?)",
461 __func__, level);
462 do_log2(level, "%s [preauth]", msg);
463
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000464 sshbuf_free(logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000465 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000466
467 return 0;
468}
469
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000470static int
471monitor_read(struct ssh *ssh, struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000472 struct mon_table **pent)
473{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000474 struct sshbuf *m;
475 int r, ret;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000476 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000477 struct pollfd pfd[2];
478
479 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100480 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000481 pfd[0].fd = pmonitor->m_sendfd;
482 pfd[0].events = POLLIN;
483 pfd[1].fd = pmonitor->m_log_recvfd;
484 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000485 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
486 if (errno == EINTR || errno == EAGAIN)
487 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000488 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000489 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000490 if (pfd[1].revents) {
491 /*
492 * Drain all log messages before processing next
493 * monitor request.
494 */
495 monitor_read_log(pmonitor);
496 continue;
497 }
498 if (pfd[0].revents)
499 break; /* Continues below */
500 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000501
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000502 if ((m = sshbuf_new()) == NULL)
503 fatal("%s: sshbuf_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000504
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000505 mm_request_receive(pmonitor->m_sendfd, m);
506 if ((r = sshbuf_get_u8(m, &type)) != 0)
507 fatal("%s: decode: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000508
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000509 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000510
511 while (ent->f != NULL) {
512 if (ent->type == type)
513 break;
514 ent++;
515 }
516
517 if (ent->f != NULL) {
518 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000519 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000520 type);
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000521 ret = (*ent->f)(ssh, pmonitor->m_sendfd, m);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000522 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523
524 /* The child may use this request only once, disable it */
525 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000526 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000527 type);
528 ent->flags &= ~MON_PERMIT;
529 }
530
531 if (pent != NULL)
532 *pent = ent;
533
534 return ret;
535 }
536
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000537 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000538
539 /* NOTREACHED */
540 return (-1);
541}
542
543/* allowed key state */
544static int
545monitor_allowed_key(u_char *blob, u_int bloblen)
546{
547 /* make sure key is allowed */
548 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000549 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000550 return (0);
551 return (1);
552}
553
554static void
555monitor_reset_key_state(void)
556{
557 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000558 free(key_blob);
559 free(hostbased_cuser);
560 free(hostbased_chost);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000561 sshauthopt_free(key_opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000562 key_blob = NULL;
563 key_bloblen = 0;
564 key_blobtype = MM_NOKEY;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000565 key_opts = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000566 hostbased_cuser = NULL;
567 hostbased_chost = NULL;
568}
569
Damien Miller1f0311c2014-05-15 14:24:09 +1000570#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000571int
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000572mm_answer_moduli(struct ssh *ssh, int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000573{
574 DH *dh;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000575 const BIGNUM *dh_p, *dh_g;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000576 int r;
577 u_int min, want, max;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000578
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000579 if ((r = sshbuf_get_u32(m, &min)) != 0 ||
580 (r = sshbuf_get_u32(m, &want)) != 0 ||
581 (r = sshbuf_get_u32(m, &max)) != 0)
582 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000583
584 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000585 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586 /* We need to check here, too, in case the child got corrupted */
587 if (max < min || want < min || max < want)
588 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000589 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000590
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000591 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000592
593 dh = choose_dh(min, want, max);
594 if (dh == NULL) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000595 if ((r = sshbuf_put_u8(m, 0)) != 0)
596 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597 return (0);
598 } else {
599 /* Send first bignum */
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000600 DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000601 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000602 (r = sshbuf_put_bignum2(m, dh_p)) != 0 ||
603 (r = sshbuf_put_bignum2(m, dh_g)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000604 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605
606 DH_free(dh);
607 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000608 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609 return (0);
610}
Damien Miller1f0311c2014-05-15 14:24:09 +1000611#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000612
613int
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000614mm_answer_sign(struct ssh *ssh, int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000615{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000616 extern int auth_sock; /* XXX move to state struct? */
617 struct sshkey *key;
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000618 struct sshbuf *sigbuf = NULL;
619 u_char *p = NULL, *signature = NULL;
620 char *alg = NULL;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000621 size_t datlen, siglen, alglen;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000622 int r, is_proof = 0;
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000623 u_int keyid, compat;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000624 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000625
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000626 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000627
djm@openbsd.org141efe42015-01-14 20:05:27 +0000628 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000629 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000630 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0 ||
631 (r = sshbuf_get_u32(m, &compat)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000632 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000633 if (keyid > INT_MAX)
634 fatal("%s: invalid key ID", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000635
Damien Millera63128d2006-03-15 12:08:28 +1100636 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000637 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
638 * SHA384 (48 bytes) and SHA512 (64 bytes).
djm@openbsd.org523463a2015-02-16 22:13:32 +0000639 *
640 * Otherwise, verify the signature request is for a hostkey
641 * proof.
642 *
643 * XXX perform similar check for KEX signature requests too?
644 * it's not trivial, since what is signed is the hash, rather
645 * than the full kex structure...
Damien Millera63128d2006-03-15 12:08:28 +1100646 */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000647 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
648 /*
649 * Construct expected hostkey proof and compare it to what
650 * the client sent us.
651 */
652 if (session_id2_len == 0) /* hostkeys is never first */
653 fatal("%s: bad data length: %zu", __func__, datlen);
654 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
655 fatal("%s: no hostkey for index %d", __func__, keyid);
656 if ((sigbuf = sshbuf_new()) == NULL)
657 fatal("%s: sshbuf_new", __func__);
djm@openbsd.org44732de2015-02-20 22:17:21 +0000658 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
659 (r = sshbuf_put_string(sigbuf, session_id2,
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000660 session_id2_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000661 (r = sshkey_puts(key, sigbuf)) != 0)
662 fatal("%s: couldn't prepare private key "
663 "proof buffer: %s", __func__, ssh_err(r));
664 if (datlen != sshbuf_len(sigbuf) ||
665 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
666 fatal("%s: bad data length: %zu, hostkey proof len %zu",
667 __func__, datlen, sshbuf_len(sigbuf));
668 sshbuf_free(sigbuf);
669 is_proof = 1;
670 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000671
Ben Lindstromf67e0772002-06-06 20:58:19 +0000672 /* save session id, it will be passed on the first call */
673 if (session_id2_len == 0) {
674 session_id2_len = datlen;
675 session_id2 = xmalloc(session_id2_len);
676 memcpy(session_id2, p, session_id2_len);
677 }
678
Damien Miller85b45e02013-07-20 13:21:52 +1000679 if ((key = get_hostkey_by_index(keyid)) != NULL) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000680 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000681 compat)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000682 fatal("%s: sshkey_sign failed: %s",
683 __func__, ssh_err(r));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000684 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000685 auth_sock > 0) {
686 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000687 p, datlen, alg, compat)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000688 fatal("%s: ssh_agent_sign failed: %s",
689 __func__, ssh_err(r));
690 }
Damien Miller85b45e02013-07-20 13:21:52 +1000691 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000692 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000693
djm@openbsd.org523463a2015-02-16 22:13:32 +0000694 debug3("%s: %s signature %p(%zu)", __func__,
695 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000696
djm@openbsd.org141efe42015-01-14 20:05:27 +0000697 sshbuf_reset(m);
698 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
699 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000700
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000701 free(alg);
Darren Tuckera627d422013-06-02 07:31:17 +1000702 free(p);
703 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000704
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000705 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000706
707 /* Turn on permissions for getpwnam */
708 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
709
710 return (0);
711}
712
713/* Retrieves the password entry and also checks if the user is permitted */
714
715int
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000716mm_answer_pwnamallow(struct ssh *ssh, int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000717{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000718 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000719 struct passwd *pwent;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000720 int r, allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000721 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000722
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000723 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000724
725 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000726 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000727
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000728 if ((r = sshbuf_get_cstring(m, &username, NULL)) != 0)
729 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000730
djm@openbsd.org3a00a922019-01-19 21:41:18 +0000731 pwent = getpwnamallow(ssh, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000732
Darren Tuckerb09b6772004-06-22 15:06:46 +1000733 authctxt->user = xstrdup(username);
734 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000735 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000736
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000737 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000738
739 if (pwent == NULL) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000740 if ((r = sshbuf_put_u8(m, 0)) != 0)
741 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf96d1832003-11-18 22:01:48 +1100742 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000743 goto out;
744 }
745
746 allowed = 1;
747 authctxt->pw = pwent;
748 authctxt->valid = 1;
749
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000750 /* XXX don't sent pwent to unpriv; send fake class/dir/shell too */
751 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
752 (r = sshbuf_put_string(m, pwent, sizeof(*pwent))) != 0 ||
753 (r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
754 (r = sshbuf_put_cstring(m, "*")) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000755#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000756 (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000757#endif
758#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000759 (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
Kevin Steves7e147602002-03-22 18:07:17 +0000760#endif
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000761 (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
762 (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
763 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100764
765 out:
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000766 ssh_packet_set_log_preamble(ssh, "%suser %s",
767 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000768 if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
769 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf2e407e2011-05-20 19:04:14 +1000770
771#define M_CP_STROPT(x) do { \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000772 if (options.x != NULL) { \
773 if ((r = sshbuf_put_cstring(m, options.x)) != 0) \
774 fatal("%s: buffer error: %s", \
775 __func__, ssh_err(r)); \
776 } \
Damien Millerf2e407e2011-05-20 19:04:14 +1000777 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000778#define M_CP_STRARRAYOPT(x, nx) do { \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000779 for (i = 0; i < options.nx; i++) { \
780 if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
781 fatal("%s: buffer error: %s", \
782 __func__, ssh_err(r)); \
783 } \
Damien Millerd8478b62011-05-29 21:39:36 +1000784 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000785 /* See comment in servconf.h */
786 COPY_MATCH_STRING_OPTS();
787#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000788#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100789
790 /* Create valid auth method lists */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000791 if (auth2_setup_methods_lists(authctxt) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100792 /*
793 * The monitor will continue long enough to let the child
794 * run to it's packet_disconnect(), but it must not allow any
795 * authentication to succeed.
796 */
797 debug("%s: no valid authentication method lists", __func__);
798 }
799
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000800 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000801 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000802
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000803 /* Allow service/style information on the auth context */
804 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
805 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
806
Damien Millera33501b2002-05-08 12:24:42 +1000807#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000808 if (options.use_pam)
809 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000810#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000811
812 return (0);
813}
814
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000815int mm_answer_auth2_read_banner(struct ssh *ssh, int sock, struct sshbuf *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000816{
817 char *banner;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000818 int r;
Damien Miller5ad9fd92002-05-13 11:07:41 +1000819
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000820 sshbuf_reset(m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000821 banner = auth2_read_banner();
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000822 if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
823 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000824 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000825 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000826
827 return (0);
828}
829
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000830int
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000831mm_answer_authserv(struct ssh *ssh, int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000832{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000833 int r;
834
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000835 monitor_permit_authentications(1);
836
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000837 if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
838 (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
839 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000840 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000841 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000842
843 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000844 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000845 authctxt->style = NULL;
846 }
847
848 return (0);
849}
850
djm@openbsd.orge76135e2018-11-16 02:43:56 +0000851/*
852 * Check that the key type appears in the supplied pattern list, ignoring
853 * mismatches in the signature algorithm. (Signature algorithm checks are
854 * performed in the unprivileged authentication code).
855 * Returns 1 on success, 0 otherwise.
856 */
857static int
858key_base_type_match(const char *method, const struct sshkey *key,
859 const char *list)
860{
861 char *s, *l, *ol = xstrdup(list);
862 int found = 0;
863
864 l = ol;
865 for ((s = strsep(&l, ",")); s && *s != '\0'; (s = strsep(&l, ","))) {
866 if (sshkey_type_from_name(s) == key->type) {
867 found = 1;
868 break;
869 }
870 }
871 if (!found) {
872 error("%s key type %s is not in permitted list %s", method,
873 sshkey_ssh_name(key), list);
874 }
875
876 free(ol);
877 return found;
878}
879
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000880int
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000881mm_answer_authpassword(struct ssh *ssh, int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000882{
883 static int call_count;
884 char *passwd;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000885 int r, authenticated;
886 size_t plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000887
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000888 if (!options.password_authentication)
889 fatal("%s: password authentication not enabled", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000890 if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
891 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000892 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000893 authenticated = options.password_authentication &&
djm@openbsd.org7c856852018-03-03 03:15:51 +0000894 auth_password(ssh, passwd);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000895 explicit_bzero(passwd, plen);
Darren Tuckera627d422013-06-02 07:31:17 +1000896 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000897
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000898 sshbuf_reset(m);
899 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
900 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker01558b72016-07-18 09:33:25 +1000901#ifdef USE_PAM
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000902 if ((r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0)
903 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker01558b72016-07-18 09:33:25 +1000904#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000905
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000906 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000907 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000908
909 call_count++;
910 if (plen == 0 && call_count == 1)
911 auth_method = "none";
912 else
913 auth_method = "password";
914
915 /* Causes monitor loop to terminate if authenticated */
916 return (authenticated);
917}
918
919#ifdef BSD_AUTH
920int
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000921mm_answer_bsdauthquery(struct ssh *ssh, int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000922{
923 char *name, *infotxt;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000924 u_int numprompts, *echo_on, success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000925 char **prompts;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000926 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000927
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000928 if (!options.kbd_interactive_authentication)
929 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb7df3af2003-02-24 11:55:46 +1100930 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
931 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000933 sshbuf_reset(m);
934 if ((r = sshbuf_put_u32(m, success)) != 0)
935 fatal("%s: buffer error: %s", __func__, ssh_err(r));
936 if (success) {
937 if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
938 fatal("%s: buffer error: %s", __func__, ssh_err(r));
939 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000940
Damien Millerb7df3af2003-02-24 11:55:46 +1100941 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000942 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000943
Damien Millerb7df3af2003-02-24 11:55:46 +1100944 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000945 free(name);
946 free(infotxt);
947 free(prompts);
948 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000949 }
950
951 return (0);
952}
953
954int
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000955mm_answer_bsdauthrespond(struct ssh *ssh, int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000956{
957 char *response;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000958 int r, authok;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000959
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000960 if (!options.kbd_interactive_authentication)
961 fatal("%s: kbd-int authentication not enabled", __func__);
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +0000962 if (authctxt->as == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000963 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000964
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000965 if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
966 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000967 authok = options.challenge_response_authentication &&
968 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000969 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000970 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000971 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000973 sshbuf_reset(m);
974 if ((r = sshbuf_put_u32(m, authok)) != 0)
975 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000977 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000978 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000979
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000980 auth_method = "keyboard-interactive";
981 auth_submethod = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000982
983 return (authok != 0);
984}
985#endif
986
Damien Miller79418552002-04-23 20:28:48 +1000987#ifdef USE_PAM
988int
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000989mm_answer_pam_start(struct ssh *ssh, int sock, struct sshbuf *m)
Damien Miller79418552002-04-23 20:28:48 +1000990{
Damien Miller4e448a32003-05-14 15:11:48 +1000991 if (!options.use_pam)
992 fatal("UsePAM not set, but ended up in %s anyway", __func__);
993
Damien Miller3f0786b2019-01-20 10:22:18 +1100994 start_pam(ssh);
Damien Miller79418552002-04-23 20:28:48 +1000995
Damien Miller1f499fd2003-08-25 13:08:49 +1000996 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
Damien Miller775f8a22016-08-31 10:48:07 +1000997 if (options.kbd_interactive_authentication)
998 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller1f499fd2003-08-25 13:08:49 +1000999
Damien Miller79418552002-04-23 20:28:48 +10001000 return (0);
1001}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001002
Damien Miller1f499fd2003-08-25 13:08:49 +10001003int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001004mm_answer_pam_account(struct ssh *ssh, int sock, struct sshbuf *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001005{
1006 u_int ret;
Damien Miller120a1ec2018-07-10 19:39:52 +10001007 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +11001008
Damien Miller1f499fd2003-08-25 13:08:49 +10001009 if (!options.use_pam)
Damien Miller775f8a22016-08-31 10:48:07 +10001010 fatal("%s: PAM not enabled", __func__);
Damien Miller1f499fd2003-08-25 13:08:49 +10001011
1012 ret = do_pam_account();
1013
Damien Miller120a1ec2018-07-10 19:39:52 +10001014 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1015 (r = sshbuf_put_stringb(m, loginmsg)) != 0)
1016 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller1f499fd2003-08-25 13:08:49 +10001017
Darren Tucker7eda5922004-06-22 13:22:50 +10001018 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001019
1020 return (ret);
1021}
1022
Damien Miller4f9f42a2003-05-10 19:28:02 +10001023static void *sshpam_ctxt, *sshpam_authok;
1024extern KbdintDevice sshpam_device;
1025
1026int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001027mm_answer_pam_init_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001028{
Damien Miller120a1ec2018-07-10 19:39:52 +10001029 u_int ok = 0;
1030 int r;
1031
Damien Miller4f9f42a2003-05-10 19:28:02 +10001032 debug3("%s", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001033 if (!options.kbd_interactive_authentication)
1034 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001035 if (sshpam_ctxt != NULL)
1036 fatal("%s: already called", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001037 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1038 sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001039 sshbuf_reset(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001040 if (sshpam_ctxt != NULL) {
1041 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
Damien Millerb38b95f2016-08-29 11:47:07 +10001042 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
Damien Miller120a1ec2018-07-10 19:39:52 +10001043 ok = 1;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001044 }
Damien Miller120a1ec2018-07-10 19:39:52 +10001045 if ((r = sshbuf_put_u32(m, ok)) != 0)
1046 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker7eda5922004-06-22 13:22:50 +10001047 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001048 return (0);
1049}
1050
1051int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001052mm_answer_pam_query(struct ssh *ssh, int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001053{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001054 char *name = NULL, *info = NULL, **prompts = NULL;
1055 u_int i, num = 0, *echo_on = 0;
Damien Miller120a1ec2018-07-10 19:39:52 +10001056 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001057
1058 debug3("%s", __func__);
1059 sshpam_authok = NULL;
Damien Millerb38b95f2016-08-29 11:47:07 +10001060 if (sshpam_ctxt == NULL)
1061 fatal("%s: no context", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001062 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1063 &num, &prompts, &echo_on);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001064 if (ret == 0 && num == 0)
1065 sshpam_authok = sshpam_ctxt;
1066 if (num > 1 || name == NULL || info == NULL)
Damien Millerb38b95f2016-08-29 11:47:07 +10001067 fatal("sshpam_device.query failed");
1068 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
Damien Miller120a1ec2018-07-10 19:39:52 +10001069 sshbuf_reset(m);
1070 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1071 (r = sshbuf_put_cstring(m, name)) != 0 ||
1072 (r = sshbuf_put_cstring(m, info)) != 0 ||
1073 (r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0 ||
1074 (r = sshbuf_put_u32(m, num)) != 0)
1075 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +10001076 free(name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001077 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001078 for (i = 0; i < num; ++i) {
Damien Miller120a1ec2018-07-10 19:39:52 +10001079 if ((r = sshbuf_put_cstring(m, prompts[i])) != 0 ||
1080 (r = sshbuf_put_u32(m, echo_on[i])) != 0)
1081 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +10001082 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001083 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001084 free(prompts);
1085 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001086 auth_method = "keyboard-interactive";
1087 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001088 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001089 return (0);
1090}
1091
1092int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001093mm_answer_pam_respond(struct ssh *ssh, int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001094{
1095 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001096 u_int i, num;
Damien Miller120a1ec2018-07-10 19:39:52 +10001097 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001098
1099 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001100 if (sshpam_ctxt == NULL)
1101 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001102 sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001103 if ((r = sshbuf_get_u32(m, &num)) != 0)
1104 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001105 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001106 resp = xcalloc(num, sizeof(char *));
Damien Miller120a1ec2018-07-10 19:39:52 +10001107 for (i = 0; i < num; ++i) {
1108 if ((r = sshbuf_get_cstring(m, &(resp[i]), NULL)) != 0)
1109 fatal("%s: buffer error: %s",
1110 __func__, ssh_err(r));
1111 }
Damien Miller4f9f42a2003-05-10 19:28:02 +10001112 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1113 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001114 free(resp[i]);
1115 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001116 } else {
1117 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1118 }
Damien Miller120a1ec2018-07-10 19:39:52 +10001119 sshbuf_reset(m);
1120 if ((r = sshbuf_put_u32(m, ret)) != 0)
1121 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker7eda5922004-06-22 13:22:50 +10001122 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001123 auth_method = "keyboard-interactive";
1124 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001125 if (ret == 0)
1126 sshpam_authok = sshpam_ctxt;
1127 return (0);
1128}
1129
1130int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001131mm_answer_pam_free_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001132{
Damien Miller5e75f512015-08-11 13:34:12 +10001133 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001134
1135 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001136 if (sshpam_ctxt == NULL)
1137 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001138 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001139 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001140 sshbuf_reset(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001141 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Millerb38b95f2016-08-29 11:47:07 +10001142 /* Allow another attempt */
1143 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller15b05cf2012-12-03 09:53:20 +11001144 auth_method = "keyboard-interactive";
1145 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001146 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001147}
Damien Miller79418552002-04-23 20:28:48 +10001148#endif
1149
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001150int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001151mm_answer_keyallowed(struct ssh *ssh, int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001152{
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001153 struct sshkey *key = NULL;
Damien Millera10f5612002-09-12 09:49:15 +10001154 char *cuser, *chost;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001155 u_int pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001156 enum mm_keytype type = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001157 int r, allowed = 0;
1158 struct sshauthopt *opts = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001159
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001160 debug3("%s entering", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001161 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1162 (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
1163 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
1164 (r = sshkey_froms(m, &key)) != 0 ||
1165 (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
1166 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001167
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001168 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001169
Damien Miller3e3b5142003-11-17 21:13:40 +11001170 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001171 /* These should not make it past the privsep child */
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001172 if (sshkey_type_plain(key->type) == KEY_RSA &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001173 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1174 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1175
Darren Tucker47eede72005-03-14 23:08:12 +11001176 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001177 case MM_USERKEY:
Damien Miller7a8f5b32006-03-31 23:14:23 +11001178 auth_method = "publickey";
djm@openbsd.org7c856852018-03-03 03:15:51 +00001179 if (!options.pubkey_authentication)
1180 break;
1181 if (auth2_key_already_used(authctxt, key))
1182 break;
djm@openbsd.orge76135e2018-11-16 02:43:56 +00001183 if (!key_base_type_match(auth_method, key,
1184 options.pubkey_key_types))
djm@openbsd.org7c856852018-03-03 03:15:51 +00001185 break;
1186 allowed = user_key_allowed(ssh, authctxt->pw, key,
1187 pubkey_auth_attempt, &opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001188 break;
1189 case MM_HOSTKEY:
djm@openbsd.org7c856852018-03-03 03:15:51 +00001190 auth_method = "hostbased";
1191 if (!options.hostbased_authentication)
1192 break;
1193 if (auth2_key_already_used(authctxt, key))
1194 break;
djm@openbsd.orge76135e2018-11-16 02:43:56 +00001195 if (!key_base_type_match(auth_method, key,
1196 options.hostbased_key_types))
djm@openbsd.org7c856852018-03-03 03:15:51 +00001197 break;
djm@openbsd.org04c091f2019-01-19 21:43:56 +00001198 allowed = hostbased_key_allowed(ssh, authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001199 cuser, chost, key);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001200 auth2_record_info(authctxt,
Damien Miller20bdcd72013-07-18 16:10:09 +10001201 "client user \"%.100s\", client host \"%.100s\"",
1202 cuser, chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001203 break;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001204 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001205 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001206 break;
1207 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001208 }
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001209
djm@openbsd.org7c856852018-03-03 03:15:51 +00001210 debug3("%s: %s authentication%s: %s key is %s", __func__,
1211 auth_method, pubkey_auth_attempt ? "" : " test",
1212 (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
1213 allowed ? "allowed" : "not allowed");
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001214
djm@openbsd.org8f574952017-06-24 06:34:38 +00001215 auth2_record_key(authctxt, 0, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001216
1217 /* clear temporarily storage (used by verify) */
1218 monitor_reset_key_state();
1219
1220 if (allowed) {
1221 /* Save temporarily for comparison in verify */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001222 if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
1223 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001224 key_blobtype = type;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001225 key_opts = opts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001226 hostbased_cuser = cuser;
1227 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001228 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001229 /* Log failed attempt */
djm@openbsd.org3a00a922019-01-19 21:41:18 +00001230 auth_log(ssh, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001231 free(cuser);
1232 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001233 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001234 sshkey_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001235
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001236 sshbuf_reset(m);
1237 if ((r = sshbuf_put_u32(m, allowed)) != 0)
1238 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org7c856852018-03-03 03:15:51 +00001239 if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
1240 fatal("%s: sshauthopt_serialise: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001241 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001242
djm@openbsd.org7c856852018-03-03 03:15:51 +00001243 if (!allowed)
1244 sshauthopt_free(opts);
1245
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001246 return (0);
1247}
1248
1249static int
1250monitor_valid_userblob(u_char *data, u_int datalen)
1251{
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001252 struct sshbuf *b;
1253 const u_char *p;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001254 char *userstyle, *cp;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001255 size_t len;
1256 u_char type;
1257 int r, fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001258
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001259 if ((b = sshbuf_new()) == NULL)
1260 fatal("%s: sshbuf_new", __func__);
1261 if ((r = sshbuf_put(b, data, datalen)) != 0)
1262 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001263
1264 if (datafellows & SSH_OLD_SESSIONID) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001265 p = sshbuf_ptr(b);
1266 len = sshbuf_len(b);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001267 if ((session_id2 == NULL) ||
1268 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001269 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001270 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001271 if ((r = sshbuf_consume(b, session_id2_len)) != 0)
1272 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001273 } else {
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001274 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1275 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromf67e0772002-06-06 20:58:19 +00001276 if ((session_id2 == NULL) ||
1277 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001278 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001279 fail++;
1280 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001281 if ((r = sshbuf_get_u8(b, &type)) != 0)
1282 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1283 if (type != SSH2_MSG_USERAUTH_REQUEST)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001284 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001285 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1286 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4ce189d2013-04-23 15:17:52 +10001287 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1288 authctxt->style ? ":" : "",
1289 authctxt->style ? authctxt->style : "");
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001290 if (strcmp(userstyle, cp) != 0) {
1291 logit("wrong user name passed to monitor: "
1292 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293 fail++;
1294 }
Darren Tuckera627d422013-06-02 07:31:17 +10001295 free(userstyle);
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001296 free(cp);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001297 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1298 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1299 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001300 if (strcmp("publickey", cp) != 0)
1301 fail++;
1302 free(cp);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001303 if ((r = sshbuf_get_u8(b, &type)) != 0)
1304 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1305 if (type == 0)
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001306 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001307 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1308 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1309 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1310 if (sshbuf_len(b) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001311 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001312 sshbuf_free(b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001313 return (fail == 0);
1314}
1315
1316static int
Damien Millera10f5612002-09-12 09:49:15 +10001317monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1318 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001319{
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001320 struct sshbuf *b;
1321 const u_char *p;
1322 char *cp, *userstyle;
1323 size_t len;
1324 int r, fail = 0;
1325 u_char type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001326
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001327 if ((b = sshbuf_new()) == NULL)
1328 fatal("%s: sshbuf_new", __func__);
1329 if ((r = sshbuf_put(b, data, datalen)) != 0 ||
1330 (r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1331 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001332
Ben Lindstromf67e0772002-06-06 20:58:19 +00001333 if ((session_id2 == NULL) ||
1334 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001335 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001336 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001337
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001338 if ((r = sshbuf_get_u8(b, &type)) != 0)
1339 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1340 if (type != SSH2_MSG_USERAUTH_REQUEST)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001341 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001342 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1343 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4ce189d2013-04-23 15:17:52 +10001344 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1345 authctxt->style ? ":" : "",
1346 authctxt->style ? authctxt->style : "");
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001347 if (strcmp(userstyle, cp) != 0) {
1348 logit("wrong user name passed to monitor: "
1349 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001350 fail++;
1351 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001352 free(userstyle);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001353 free(cp);
1354 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1355 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1356 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1357 if (strcmp(cp, "hostbased") != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001358 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001359 free(cp);
1360 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1361 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1362 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001363
1364 /* verify client host, strip trailing dot if necessary */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001365 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1366 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1367 if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
1368 cp[len - 1] = '\0';
1369 if (strcmp(cp, chost) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001370 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001371 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001372
1373 /* verify client user */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001374 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1375 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1376 if (strcmp(cp, cuser) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001377 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001378 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001379
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001380 if (sshbuf_len(b) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001381 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001382 sshbuf_free(b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001383 return (fail == 0);
1384}
1385
1386int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001387mm_answer_keyverify(struct ssh *ssh, int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001388{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001389 struct sshkey *key;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001390 u_char *signature, *data, *blob;
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001391 char *sigalg;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001392 size_t signaturelen, datalen, bloblen;
1393 int r, ret, valid_data = 0, encoded_ret;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001394
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001395 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1396 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001397 (r = sshbuf_get_string(m, &data, &datalen)) != 0 ||
1398 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001399 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001400
1401 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001402 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001403 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001404
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001405 /* Empty signature algorithm means NULL. */
1406 if (*sigalg == '\0') {
1407 free(sigalg);
1408 sigalg = NULL;
1409 }
1410
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001411 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1412 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1413 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001414
1415 switch (key_blobtype) {
1416 case MM_USERKEY:
1417 valid_data = monitor_valid_userblob(data, datalen);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001418 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001419 break;
1420 case MM_HOSTKEY:
1421 valid_data = monitor_valid_hostbasedblob(data, datalen,
1422 hostbased_cuser, hostbased_chost);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001423 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001424 break;
1425 default:
1426 valid_data = 0;
1427 break;
1428 }
1429 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001430 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001431
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001432 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001433 sigalg, ssh->compat);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001434 debug3("%s: %s %p signature %s", __func__, auth_method, key,
1435 (ret == 0) ? "verified" : "unverified");
1436 auth2_record_key(authctxt, ret == 0, key);
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001437
Darren Tuckera627d422013-06-02 07:31:17 +10001438 free(blob);
1439 free(signature);
1440 free(data);
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001441 free(sigalg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001442
djm@openbsd.org7c856852018-03-03 03:15:51 +00001443 if (key_blobtype == MM_USERKEY)
1444 auth_activate_options(ssh, key_opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001445 monitor_reset_key_state();
1446
djm@openbsd.org8f574952017-06-24 06:34:38 +00001447 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001448 sshbuf_reset(m);
1449
1450 /* encode ret != 0 as positive integer, since we're sending u32 */
1451 encoded_ret = (ret != 0);
1452 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1453 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001454 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001455
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001456 return ret == 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457}
1458
1459static void
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001460mm_record_login(struct ssh *ssh, Session *s, struct passwd *pw)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001461{
1462 socklen_t fromlen;
1463 struct sockaddr_storage from;
1464
1465 /*
1466 * Get IP address of client. If the connection is not a socket, let
1467 * the address be 0.0.0.0.
1468 */
1469 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001470 fromlen = sizeof(from);
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001471 if (ssh_packet_connection_is_on_socket(ssh)) {
1472 if (getpeername(ssh_packet_get_connection_in(ssh),
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001473 (struct sockaddr *)&from, &fromlen) == -1) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001474 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001475 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001476 }
1477 }
1478 /* Record that there was a login on that tty from the remote host. */
1479 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +00001480 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001481 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001482}
1483
1484static void
1485mm_session_close(Session *s)
1486{
Damien Miller04bd8b02003-05-25 14:38:33 +10001487 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001488 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001489 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001490 session_pty_cleanup2(s);
1491 }
Damien Miller7207f642008-05-19 15:34:50 +10001492 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001493}
1494
1495int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001496mm_answer_pty(struct ssh *ssh, int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001497{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001498 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001499 Session *s;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001500 int r, res, fd0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001501
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001502 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001503
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001504 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001505 s = session_new();
1506 if (s == NULL)
1507 goto error;
1508 s->authctxt = authctxt;
1509 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001510 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001511 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1512 if (res == 0)
1513 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001514 pty_setowner(authctxt->pw, s->tty);
1515
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001516 if ((r = sshbuf_put_u32(m, 1)) != 0 ||
1517 (r = sshbuf_put_cstring(m, s->tty)) != 0)
1518 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001519
1520 /* We need to trick ttyslot */
1521 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001522 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001523
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001524 mm_record_login(ssh, s, authctxt->pw);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001525
1526 /* Now we can close the file descriptor again */
1527 close(0);
1528
Darren Tucker09991742004-07-17 17:05:14 +10001529 /* send messages generated by record_login */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001530 if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
1531 fatal("%s: put login message: %s", __func__, ssh_err(r));
1532 sshbuf_reset(loginmsg);
Darren Tucker09991742004-07-17 17:05:14 +10001533
1534 mm_request_send(sock, MONITOR_ANS_PTY, m);
1535
Damien Miller54fd7cf2007-09-17 12:04:08 +10001536 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1537 mm_send_fd(sock, s->ttyfd) == -1)
1538 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001539
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001540 /* make sure nothing uses fd 0 */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001541 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001542 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001543 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001544 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001545
1546 /* slave is not needed */
1547 close(s->ttyfd);
1548 s->ttyfd = s->ptyfd;
1549 /* no need to dup() because nobody closes ptyfd */
1550 s->ptymaster = s->ptyfd;
1551
Damien Miller9ab00b42006-08-05 12:40:11 +10001552 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001553
1554 return (0);
1555
1556 error:
1557 if (s != NULL)
1558 mm_session_close(s);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001559 if ((r = sshbuf_put_u32(m, 0)) != 0)
1560 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001561 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001562 return (0);
1563}
1564
1565int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001566mm_answer_pty_cleanup(struct ssh *ssh, int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001567{
1568 Session *s;
1569 char *tty;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001570 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001571
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001572 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001573
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001574 if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
1575 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001576 if ((s = session_by_tty(tty)) != NULL)
1577 mm_session_close(s);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001578 sshbuf_reset(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001579 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001580 return (0);
1581}
1582
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001583int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001584mm_answer_term(struct ssh *ssh, int sock, struct sshbuf *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001585{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001586 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001587 int res, status;
1588
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001589 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001590
1591 /* The child is terminating */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001592 session_destroy_all(ssh, &mm_session_close);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001593
Darren Tucker52358d62008-03-11 22:58:25 +11001594#ifdef USE_PAM
1595 if (options.use_pam)
1596 sshpam_cleanup();
1597#endif
1598
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001599 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001600 if (errno != EINTR)
1601 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001602
1603 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1604
1605 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001606 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001607}
1608
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001609#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001610/* Report that an audit event occurred */
1611int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001612mm_answer_audit_event(struct ssh *ssh, int socket, struct sshbuf *m)
Darren Tucker269a1ea2005-02-03 00:20:53 +11001613{
Damien Miller120a1ec2018-07-10 19:39:52 +10001614 u_int n;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001615 ssh_audit_event_t event;
Damien Miller120a1ec2018-07-10 19:39:52 +10001616 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001617
1618 debug3("%s entering", __func__);
1619
Damien Miller120a1ec2018-07-10 19:39:52 +10001620 if ((r = sshbuf_get_u32(m, &n)) != 0)
1621 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1622 event = (ssh_audit_event_t)n;
1623 switch (event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001624 case SSH_AUTH_FAIL_PUBKEY:
1625 case SSH_AUTH_FAIL_HOSTBASED:
1626 case SSH_AUTH_FAIL_GSSAPI:
1627 case SSH_LOGIN_EXCEED_MAXTRIES:
1628 case SSH_LOGIN_ROOT_DENIED:
1629 case SSH_CONNECTION_CLOSE:
1630 case SSH_INVALID_USER:
Damien Miller9b655dc2019-01-20 14:55:27 +11001631 audit_event(ssh, event);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001632 break;
1633 default:
1634 fatal("Audit event type %d not permitted", event);
1635 }
1636
1637 return (0);
1638}
1639
1640int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001641mm_answer_audit_command(struct ssh *ssh, int socket, struct sshbuf *m)
Darren Tucker269a1ea2005-02-03 00:20:53 +11001642{
Darren Tucker269a1ea2005-02-03 00:20:53 +11001643 char *cmd;
Damien Miller120a1ec2018-07-10 19:39:52 +10001644 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001645
1646 debug3("%s entering", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +10001647 if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
1648 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker269a1ea2005-02-03 00:20:53 +11001649 /* sanity check command, if so how? */
1650 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001651 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001652 return (0);
1653}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001654#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001655
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001656void
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001657monitor_clear_keystate(struct ssh *ssh, struct monitor *pmonitor)
markus@openbsd.org1e0cdf82017-05-31 08:09:45 +00001658{
markus@openbsd.org1e0cdf82017-05-31 08:09:45 +00001659 ssh_clear_newkeys(ssh, MODE_IN);
1660 ssh_clear_newkeys(ssh, MODE_OUT);
1661 sshbuf_free(child_state);
1662 child_state = NULL;
1663}
1664
1665void
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001666monitor_apply_keystate(struct ssh *ssh, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001667{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001668 struct kex *kex;
1669 int r;
1670
1671 debug3("%s: packet_set_state", __func__);
1672 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1673 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1674 sshbuf_free(child_state);
1675 child_state = NULL;
1676
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +00001677 if ((kex = ssh->kex) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001678 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001679#ifdef WITH_OPENSSL
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +00001680 kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
1681 kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
1682 kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
1683 kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
1684 kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001685 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1686 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001687# ifdef OPENSSL_HAS_ECC
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +00001688 kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001689# endif
Damien Miller773dda22015-01-30 23:10:17 +11001690#endif /* WITH_OPENSSL */
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +00001691 kex->kex[KEX_C25519_SHA256] = kex_gen_server;
1692 kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_server;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001693 kex->load_host_public_key=&get_hostkey_public_by_type;
1694 kex->load_host_private_key=&get_hostkey_private_by_type;
1695 kex->host_key_index=&get_hostkey_index;
1696 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001697 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001698}
1699
1700/* This function requries careful sanity checking */
1701
1702void
djm@openbsd.org04c091f2019-01-19 21:43:56 +00001703mm_get_keystate(struct ssh *ssh, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001704{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001705 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001706
markus@openbsd.org091c3022015-01-19 19:52:16 +00001707 if ((child_state = sshbuf_new()) == NULL)
1708 fatal("%s: sshbuf_new failed", __func__);
1709 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1710 child_state);
1711 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001712}
1713
1714
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001715/* XXX */
1716
1717#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001718 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001719 fatal("fcntl(%d, F_SETFD)", x); \
1720} while (0)
1721
1722static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001723monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001724{
Damien Miller8f0bf232011-06-20 14:42:23 +10001725 int pair[2];
markus@openbsd.org84008602017-05-31 10:04:29 +00001726#ifdef SO_ZEROIZE
1727 int on = 1;
1728#endif
Damien Miller8f0bf232011-06-20 14:42:23 +10001729
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001730 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001731 fatal("%s: socketpair: %s", __func__, strerror(errno));
markus@openbsd.org84008602017-05-31 10:04:29 +00001732#ifdef SO_ZEROIZE
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001733 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
markus@openbsd.org84008602017-05-31 10:04:29 +00001734 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001735 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
markus@openbsd.org84008602017-05-31 10:04:29 +00001736 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1737#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001738 FD_CLOSEONEXEC(pair[0]);
1739 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001740 mon->m_recvfd = pair[0];
1741 mon->m_sendfd = pair[1];
1742
1743 if (do_logfds) {
1744 if (pipe(pair) == -1)
1745 fatal("%s: pipe: %s", __func__, strerror(errno));
1746 FD_CLOSEONEXEC(pair[0]);
1747 FD_CLOSEONEXEC(pair[1]);
1748 mon->m_log_recvfd = pair[0];
1749 mon->m_log_sendfd = pair[1];
1750 } else
1751 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001752}
1753
1754#define MM_MEMSIZE 65536
1755
1756struct monitor *
1757monitor_init(void)
1758{
1759 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001760
Damien Miller07d86be2006-03-26 14:19:21 +11001761 mon = xcalloc(1, sizeof(*mon));
Damien Miller8f0bf232011-06-20 14:42:23 +10001762 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001763
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001764 return mon;
1765}
1766
1767void
1768monitor_reinit(struct monitor *mon)
1769{
Damien Miller8f0bf232011-06-20 14:42:23 +10001770 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001771}
Darren Tucker0efd1552003-08-26 11:49:55 +10001772
1773#ifdef GSSAPI
1774int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001775mm_answer_gss_setup_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001776{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001777 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001778 OM_uint32 major;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001779 size_t len;
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001780 u_char *p;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001781 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +10001782
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001783 if (!options.gss_authentication)
1784 fatal("%s: GSSAPI authentication not enabled", __func__);
1785
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001786 if ((r = sshbuf_get_string(m, &p, &len)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001787 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001788 goid.elements = p;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001789 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001790
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001791 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001792
Darren Tuckera627d422013-06-02 07:31:17 +10001793 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001794
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001795 sshbuf_reset(m);
1796 if ((r = sshbuf_put_u32(m, major)) != 0)
1797 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001798
Damien Miller6fd6def2005-11-05 15:07:05 +11001799 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001800
1801 /* Now we have a context, enable the step */
1802 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1803
1804 return (0);
1805}
1806
1807int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001808mm_answer_gss_accept_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001809{
1810 gss_buffer_desc in;
1811 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001812 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001813 OM_uint32 flags = 0; /* GSI needs this */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001814 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +10001815
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001816 if (!options.gss_authentication)
1817 fatal("%s: GSSAPI authentication not enabled", __func__);
1818
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001819 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001820 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001821 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001822 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001823
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001824 sshbuf_reset(m);
1825 if ((r = sshbuf_put_u32(m, major)) != 0 ||
1826 (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
1827 (r = sshbuf_put_u32(m, flags)) != 0)
1828 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001829 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001830
1831 gss_release_buffer(&minor, &out);
1832
Damien Miller6fd6def2005-11-05 15:07:05 +11001833 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001834 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1835 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001836 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001837 }
1838 return (0);
1839}
1840
1841int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001842mm_answer_gss_checkmic(struct ssh *ssh, int sock, struct sshbuf *m)
Damien Miller0425d402003-11-17 22:18:21 +11001843{
1844 gss_buffer_desc gssbuf, mic;
1845 OM_uint32 ret;
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001846 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +11001847
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001848 if (!options.gss_authentication)
1849 fatal("%s: GSSAPI authentication not enabled", __func__);
1850
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001851 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
1852 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001853 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller787b2ec2003-11-21 23:56:47 +11001854
Damien Miller0425d402003-11-17 22:18:21 +11001855 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001856
Darren Tuckera627d422013-06-02 07:31:17 +10001857 free(gssbuf.value);
1858 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001859
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001860 sshbuf_reset(m);
1861 if ((r = sshbuf_put_u32(m, ret)) != 0)
1862 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller787b2ec2003-11-21 23:56:47 +11001863
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001864 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001865
Damien Miller0425d402003-11-17 22:18:21 +11001866 if (!GSS_ERROR(ret))
1867 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001868
Damien Miller0425d402003-11-17 22:18:21 +11001869 return (0);
1870}
1871
1872int
djm@openbsd.orgec00f912019-01-19 21:43:07 +00001873mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001874{
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001875 int r, authenticated;
djm@openbsd.org8f574952017-06-24 06:34:38 +00001876 const char *displayname;
Darren Tucker0efd1552003-08-26 11:49:55 +10001877
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001878 if (!options.gss_authentication)
1879 fatal("%s: GSSAPI authentication not enabled", __func__);
1880
Darren Tucker0efd1552003-08-26 11:49:55 +10001881 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1882
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001883 sshbuf_reset(m);
1884 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1885 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001886
1887 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001888 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001889
Damien Miller6fd6def2005-11-05 15:07:05 +11001890 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001891
djm@openbsd.org8f574952017-06-24 06:34:38 +00001892 if ((displayname = ssh_gssapi_displayname()) != NULL)
1893 auth2_record_info(authctxt, "%s", displayname);
1894
Darren Tucker0efd1552003-08-26 11:49:55 +10001895 /* Monitor loop will terminate if authenticated */
1896 return (authenticated);
1897}
1898#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001899