blob: 531b2993ab6bc8d3dcdca3d3a60c73939b6dd78d [file] [log] [blame]
djm@openbsd.org6ad86482018-07-20 03:46:34 +00001/* $OpenBSD: monitor.c,v 1.186 2018/07/20 03:46:34 djm Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100031#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110032#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000033
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100035#include <fcntl.h>
djm@openbsd.org1a31d022016-05-02 08:49:03 +000036#include <limits.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110037#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110038#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110039#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100040#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110041#include <signal.h>
Damien Miller9af21972015-02-24 09:04:32 +110042#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000043#include <stdint.h>
Damien Miller9af21972015-02-24 09:04:32 +110044#endif
Damien Millere7a1e5c2006-08-05 11:34:19 +100045#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100046#include <string.h>
Damien Miller86687062014-07-02 15:28:02 +100047#include <stdarg.h>
48#include <stdio.h>
Damien Miller607aede2006-09-01 15:48:19 +100049#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100050#ifdef HAVE_POLL_H
51#include <poll.h>
52#else
53# ifdef HAVE_SYS_POLL_H
54# include <sys/poll.h>
55# endif
56#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000057
Damien Miller1f0311c2014-05-15 14:24:09 +100058#ifdef WITH_OPENSSL
Damien Miller03e20032006-03-15 11:16:59 +110059#include <openssl/dh.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100060#endif
Damien Miller03e20032006-03-15 11:16:59 +110061
Damien Miller48f54b92018-09-13 12:13:50 +100062#include "openbsd-compat/sys-tree.h"
Damien Millerb84886b2008-05-19 15:05:07 +100063#include "openbsd-compat/sys-queue.h"
Damien Miller48f54b92018-09-13 12:13:50 +100064#include "openbsd-compat/openssl-compat.h"
65
Damien Miller8f0bf232011-06-20 14:42:23 +100066#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100067#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000068#include "ssh.h"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +000069#include "sshkey.h"
markus@openbsd.org235c7c42018-07-09 21:53:45 +000070#include "sshbuf.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000072#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100073#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000074#include "kex.h"
75#include "dh.h"
Darren Tucker01558b72016-07-18 09:33:25 +100076#include "auth-pam.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000077#include "packet.h"
78#include "auth-options.h"
79#include "sshpty.h"
80#include "channels.h"
81#include "session.h"
82#include "sshlogin.h"
83#include "canohost.h"
84#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100085#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000086#include "servconf.h"
87#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +100088#ifdef GSSAPI
89#include "ssh-gss.h"
90#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000091#include "monitor_wrap.h"
92#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093#include "compat.h"
94#include "ssh2.h"
Damien Miller85b45e02013-07-20 13:21:52 +100095#include "authfd.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +000096#include "match.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +000097#include "ssherr.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000098
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
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000115int mm_answer_moduli(int, struct sshbuf *);
116int mm_answer_sign(int, struct sshbuf *);
117int mm_answer_pwnamallow(int, struct sshbuf *);
118int mm_answer_auth2_read_banner(int, struct sshbuf *);
119int mm_answer_authserv(int, struct sshbuf *);
120int mm_answer_authpassword(int, struct sshbuf *);
121int mm_answer_bsdauthquery(int, struct sshbuf *);
122int mm_answer_bsdauthrespond(int, struct sshbuf *);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000123int mm_answer_keyallowed(int, struct sshbuf *);
124int mm_answer_keyverify(int, struct sshbuf *);
125int mm_answer_pty(int, struct sshbuf *);
126int mm_answer_pty_cleanup(int, struct sshbuf *);
127int mm_answer_term(int, struct sshbuf *);
128int mm_answer_rsa_keyallowed(int, struct sshbuf *);
129int mm_answer_rsa_challenge(int, struct sshbuf *);
130int mm_answer_rsa_response(int, struct sshbuf *);
131int mm_answer_sesskey(int, struct sshbuf *);
132int mm_answer_sessid(int, struct sshbuf *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000133
Damien Miller79418552002-04-23 20:28:48 +1000134#ifdef USE_PAM
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000135int mm_answer_pam_start(int, struct sshbuf *);
136int mm_answer_pam_account(int, struct sshbuf *);
137int mm_answer_pam_init_ctx(int, struct sshbuf *);
138int mm_answer_pam_query(int, struct sshbuf *);
139int mm_answer_pam_respond(int, struct sshbuf *);
140int mm_answer_pam_free_ctx(int, struct sshbuf *);
Damien Miller79418552002-04-23 20:28:48 +1000141#endif
142
Darren Tucker0efd1552003-08-26 11:49:55 +1000143#ifdef GSSAPI
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000144int mm_answer_gss_setup_ctx(int, struct sshbuf *);
145int mm_answer_gss_accept_ctx(int, struct sshbuf *);
146int mm_answer_gss_userok(int, struct sshbuf *);
147int mm_answer_gss_checkmic(int, struct sshbuf *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000148#endif
Damien Miller25162f22002-09-12 09:47:29 +1000149
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100150#ifdef SSH_AUDIT_EVENTS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000151int mm_answer_audit_event(int, struct sshbuf *);
152int mm_answer_audit_command(int, struct sshbuf *);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100153#endif
154
Damien Miller8f0bf232011-06-20 14:42:23 +1000155static int monitor_read_log(struct monitor *);
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;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000175 int (*f)(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
187struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000188#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000189 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000190#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000191 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
192 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
193 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000194 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000195 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000196#ifdef USE_PAM
197 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000198 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Millerb38b95f2016-08-29 11:47:07 +1000199 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
200 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
201 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000202 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000203#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100204#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100205 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100206#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000207#ifdef BSD_AUTH
208 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100209 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000210#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000211 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
212 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000213#ifdef GSSAPI
214 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
djm@openbsd.orgb33ad6d2016-09-05 13:57:31 +0000215 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
216 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
217 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000218#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000219 {0, 0, NULL}
220};
221
222struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000223#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000224 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000225#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000226 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
227 {MONITOR_REQ_PTY, 0, mm_answer_pty},
228 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
229 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100230#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100231 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
232 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
233#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000234 {0, 0, NULL}
235};
236
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000237struct mon_table *mon_dispatch;
238
239/* Specifies if a certain message is allowed at the moment */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000240static void
241monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
242{
243 while (ent->f != NULL) {
244 if (ent->type == type) {
245 ent->flags &= ~MON_PERMIT;
246 ent->flags |= permit ? MON_PERMIT : 0;
247 return;
248 }
249 ent++;
250 }
251}
252
253static void
254monitor_permit_authentications(int permit)
255{
256 struct mon_table *ent = mon_dispatch;
257
258 while (ent->f != NULL) {
259 if (ent->flags & MON_AUTH) {
260 ent->flags &= ~MON_PERMIT;
261 ent->flags |= permit ? MON_PERMIT : 0;
262 }
263 ent++;
264 }
265}
266
Darren Tucker3e33cec2003-10-02 16:12:36 +1000267void
268monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000269{
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000270 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000271 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100272 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000273
274 debug3("preauth child monitor started");
275
tb@openbsd.org34843802018-02-05 05:37:46 +0000276 if (pmonitor->m_recvfd >= 0)
277 close(pmonitor->m_recvfd);
278 if (pmonitor->m_log_sendfd >= 0)
279 close(pmonitor->m_log_sendfd);
Damien Miller8f0bf232011-06-20 14:42:23 +1000280 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
281
Darren Tucker3e33cec2003-10-02 16:12:36 +1000282 authctxt = _authctxt;
283 memset(authctxt, 0, sizeof(*authctxt));
djm@openbsd.org7c856852018-03-03 03:15:51 +0000284 ssh->authctxt = authctxt;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000285
Damien Miller120a1ec2018-07-10 19:39:52 +1000286 authctxt->loginmsg = loginmsg;
Darren Tuckerde0de392005-03-31 23:52:04 +1000287
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000288 mon_dispatch = mon_dispatch_proto20;
289 /* Permit requests for moduli and signatures */
290 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
291 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000292
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000293 /* The first few requests do not require asynchronous access */
294 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100295 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100296 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100297 auth_submethod = NULL;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000298 auth2_authctxt_reset_info(authctxt);
299
Darren Tuckerfbba7352006-11-07 23:16:08 +1100300 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100301
302 /* Special handling for multiple required authentications */
303 if (options.num_auth_methods != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100304 if (authenticated &&
305 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000306 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100307 debug3("%s: method %s: partial", __func__,
308 auth_method);
309 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100310 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100311 }
312 }
313
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000314 if (authenticated) {
315 if (!(ent->flags & MON_AUTHDECIDE))
316 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000317 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000318 if (authctxt->pw->pw_uid == 0 &&
djm@openbsd.org7c856852018-03-03 03:15:51 +0000319 !auth_root_allowed(ssh, auth_method))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000320 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000321#ifdef USE_PAM
322 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100323 if (options.use_pam && authenticated) {
Damien Miller120a1ec2018-07-10 19:39:52 +1000324 struct sshbuf *m;
Damien Miller1f499fd2003-08-25 13:08:49 +1000325
Damien Miller120a1ec2018-07-10 19:39:52 +1000326 if ((m = sshbuf_new()) == NULL)
327 fatal("%s: sshbuf_new failed",
328 __func__);
Damien Millera8e06ce2003-11-21 23:48:55 +1100329 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller120a1ec2018-07-10 19:39:52 +1000330 MONITOR_REQ_PAM_ACCOUNT, m);
331 authenticated = mm_answer_pam_account(
332 pmonitor->m_sendfd, m);
333 sshbuf_free(m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000334 }
335#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000336 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100337 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100338 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000339 auth_method, auth_submethod);
djm@openbsd.org94885382015-06-22 23:42:16 +0000340 if (!partial && !authenticated)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000341 authctxt->failures++;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000342 if (authenticated || partial) {
343 auth2_update_session_info(authctxt,
344 auth_method, auth_submethod);
345 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000346 }
347 }
348
349 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000350 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100351 if (strcmp(auth_method, "unknown") == 0)
352 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000353
354 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000355 __func__, authctxt->user);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000356 ssh->authctxt = NULL;
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000357 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000358
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000359 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000360
Damien Miller6a1937e2012-12-12 10:44:38 +1100361 /* Drain any buffered messages from the child */
362 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
363 ;
364
tb@openbsd.org34843802018-02-05 05:37:46 +0000365 if (pmonitor->m_recvfd >= 0)
366 close(pmonitor->m_recvfd);
367 if (pmonitor->m_log_sendfd >= 0)
368 close(pmonitor->m_log_sendfd);
Damien Miller8f0bf232011-06-20 14:42:23 +1000369 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370}
371
Damien Millerbe64d432003-05-14 19:31:12 +1000372static void
373monitor_set_child_handler(pid_t pid)
374{
375 monitor_child_pid = pid;
376}
377
378static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000379monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000380{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000381 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000382}
383
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000384void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000385monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000386{
Damien Miller8f0bf232011-06-20 14:42:23 +1000387 close(pmonitor->m_recvfd);
388 pmonitor->m_recvfd = -1;
389
Damien Millerbe64d432003-05-14 19:31:12 +1000390 monitor_set_child_handler(pmonitor->m_pid);
391 signal(SIGHUP, &monitor_child_handler);
392 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000393 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000394#ifdef SIGXFSZ
395 signal(SIGXFSZ, SIG_IGN);
396#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000397
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000398 mon_dispatch = mon_dispatch_postauth20;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000399
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000400 /* Permit requests for moduli and signatures */
401 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
402 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
403 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
404
djm@openbsd.org7c856852018-03-03 03:15:51 +0000405 if (auth_opts->permit_pty_flag) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000406 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
407 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
408 }
409
410 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000411 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000412}
413
Damien Miller8f0bf232011-06-20 14:42:23 +1000414static int
415monitor_read_log(struct monitor *pmonitor)
416{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000417 struct sshbuf *logmsg;
Damien Miller8f0bf232011-06-20 14:42:23 +1000418 u_int len, level;
419 char *msg;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000420 u_char *p;
421 int r;
Damien Miller8f0bf232011-06-20 14:42:23 +1000422
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000423 if ((logmsg = sshbuf_new()) == NULL)
424 fatal("%s: sshbuf_new", __func__);
Damien Miller8f0bf232011-06-20 14:42:23 +1000425
426 /* Read length */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000427 if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
428 fatal("%s: reserve: %s", __func__, ssh_err(r));
429 if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
Damien Miller8f0bf232011-06-20 14:42:23 +1000430 if (errno == EPIPE) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000431 sshbuf_free(logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000432 debug("%s: child log fd closed", __func__);
433 close(pmonitor->m_log_recvfd);
434 pmonitor->m_log_recvfd = -1;
435 return -1;
436 }
437 fatal("%s: log fd read: %s", __func__, strerror(errno));
438 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000439 if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
440 fatal("%s: get len: %s", __func__, ssh_err(r));
Damien Miller8f0bf232011-06-20 14:42:23 +1000441 if (len <= 4 || len > 8192)
442 fatal("%s: invalid log message length %u", __func__, len);
443
444 /* Read severity, message */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000445 sshbuf_reset(logmsg);
446 if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
447 fatal("%s: reserve: %s", __func__, ssh_err(r));
448 if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
Damien Miller8f0bf232011-06-20 14:42:23 +1000449 fatal("%s: log fd read: %s", __func__, strerror(errno));
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000450 if ((r = sshbuf_get_u32(logmsg, &level)) != 0 ||
451 (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
452 fatal("%s: decode: %s", __func__, ssh_err(r));
Damien Miller8f0bf232011-06-20 14:42:23 +1000453
454 /* Log it */
Damien Miller8f0bf232011-06-20 14:42:23 +1000455 if (log_level_name(level) == NULL)
456 fatal("%s: invalid log level %u (corrupted message?)",
457 __func__, level);
458 do_log2(level, "%s [preauth]", msg);
459
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000460 sshbuf_free(logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000461 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000462
463 return 0;
464}
465
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000466int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000467monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000468 struct mon_table **pent)
469{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000470 struct sshbuf *m;
471 int r, ret;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000472 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000473 struct pollfd pfd[2];
474
475 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100476 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000477 pfd[0].fd = pmonitor->m_sendfd;
478 pfd[0].events = POLLIN;
479 pfd[1].fd = pmonitor->m_log_recvfd;
480 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000481 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
482 if (errno == EINTR || errno == EAGAIN)
483 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000484 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000485 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000486 if (pfd[1].revents) {
487 /*
488 * Drain all log messages before processing next
489 * monitor request.
490 */
491 monitor_read_log(pmonitor);
492 continue;
493 }
494 if (pfd[0].revents)
495 break; /* Continues below */
496 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000497
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000498 if ((m = sshbuf_new()) == NULL)
499 fatal("%s: sshbuf_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000500
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000501 mm_request_receive(pmonitor->m_sendfd, m);
502 if ((r = sshbuf_get_u8(m, &type)) != 0)
503 fatal("%s: decode: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000504
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000505 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000506
507 while (ent->f != NULL) {
508 if (ent->type == type)
509 break;
510 ent++;
511 }
512
513 if (ent->f != NULL) {
514 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000515 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000516 type);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000517 ret = (*ent->f)(pmonitor->m_sendfd, m);
518 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000519
520 /* The child may use this request only once, disable it */
521 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000522 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523 type);
524 ent->flags &= ~MON_PERMIT;
525 }
526
527 if (pent != NULL)
528 *pent = ent;
529
530 return ret;
531 }
532
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000533 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000534
535 /* NOTREACHED */
536 return (-1);
537}
538
539/* allowed key state */
540static int
541monitor_allowed_key(u_char *blob, u_int bloblen)
542{
543 /* make sure key is allowed */
544 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000545 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000546 return (0);
547 return (1);
548}
549
550static void
551monitor_reset_key_state(void)
552{
553 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000554 free(key_blob);
555 free(hostbased_cuser);
556 free(hostbased_chost);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000557 sshauthopt_free(key_opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000558 key_blob = NULL;
559 key_bloblen = 0;
560 key_blobtype = MM_NOKEY;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000561 key_opts = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000562 hostbased_cuser = NULL;
563 hostbased_chost = NULL;
564}
565
Damien Miller1f0311c2014-05-15 14:24:09 +1000566#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000567int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000568mm_answer_moduli(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000569{
570 DH *dh;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000571 const BIGNUM *dh_p, *dh_g;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000572 int r;
573 u_int min, want, max;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000574
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000575 if ((r = sshbuf_get_u32(m, &min)) != 0 ||
576 (r = sshbuf_get_u32(m, &want)) != 0 ||
577 (r = sshbuf_get_u32(m, &max)) != 0)
578 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000579
580 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000581 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000582 /* We need to check here, too, in case the child got corrupted */
583 if (max < min || want < min || max < want)
584 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000585 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000587 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000588
589 dh = choose_dh(min, want, max);
590 if (dh == NULL) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000591 if ((r = sshbuf_put_u8(m, 0)) != 0)
592 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000593 return (0);
594 } else {
595 /* Send first bignum */
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000596 DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000597 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000598 (r = sshbuf_put_bignum2(m, dh_p)) != 0 ||
599 (r = sshbuf_put_bignum2(m, dh_g)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000600 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000601
602 DH_free(dh);
603 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000604 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605 return (0);
606}
Damien Miller1f0311c2014-05-15 14:24:09 +1000607#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000608
609int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000610mm_answer_sign(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000611{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000612 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000613 extern int auth_sock; /* XXX move to state struct? */
614 struct sshkey *key;
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000615 struct sshbuf *sigbuf = NULL;
616 u_char *p = NULL, *signature = NULL;
617 char *alg = NULL;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000618 size_t datlen, siglen, alglen;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000619 int r, is_proof = 0;
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000620 u_int keyid, compat;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000621 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000622
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000623 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000624
djm@openbsd.org141efe42015-01-14 20:05:27 +0000625 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000626 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000627 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0 ||
628 (r = sshbuf_get_u32(m, &compat)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000629 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000630 if (keyid > INT_MAX)
631 fatal("%s: invalid key ID", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000632
Damien Millera63128d2006-03-15 12:08:28 +1100633 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000634 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
635 * SHA384 (48 bytes) and SHA512 (64 bytes).
djm@openbsd.org523463a2015-02-16 22:13:32 +0000636 *
637 * Otherwise, verify the signature request is for a hostkey
638 * proof.
639 *
640 * XXX perform similar check for KEX signature requests too?
641 * it's not trivial, since what is signed is the hash, rather
642 * than the full kex structure...
Damien Millera63128d2006-03-15 12:08:28 +1100643 */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000644 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
645 /*
646 * Construct expected hostkey proof and compare it to what
647 * the client sent us.
648 */
649 if (session_id2_len == 0) /* hostkeys is never first */
650 fatal("%s: bad data length: %zu", __func__, datlen);
651 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
652 fatal("%s: no hostkey for index %d", __func__, keyid);
653 if ((sigbuf = sshbuf_new()) == NULL)
654 fatal("%s: sshbuf_new", __func__);
djm@openbsd.org44732de2015-02-20 22:17:21 +0000655 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
656 (r = sshbuf_put_string(sigbuf, session_id2,
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000657 session_id2_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000658 (r = sshkey_puts(key, sigbuf)) != 0)
659 fatal("%s: couldn't prepare private key "
660 "proof buffer: %s", __func__, ssh_err(r));
661 if (datlen != sshbuf_len(sigbuf) ||
662 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
663 fatal("%s: bad data length: %zu, hostkey proof len %zu",
664 __func__, datlen, sshbuf_len(sigbuf));
665 sshbuf_free(sigbuf);
666 is_proof = 1;
667 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000668
Ben Lindstromf67e0772002-06-06 20:58:19 +0000669 /* save session id, it will be passed on the first call */
670 if (session_id2_len == 0) {
671 session_id2_len = datlen;
672 session_id2 = xmalloc(session_id2_len);
673 memcpy(session_id2, p, session_id2_len);
674 }
675
Damien Miller85b45e02013-07-20 13:21:52 +1000676 if ((key = get_hostkey_by_index(keyid)) != NULL) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000677 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000678 compat)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000679 fatal("%s: sshkey_sign failed: %s",
680 __func__, ssh_err(r));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000681 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000682 auth_sock > 0) {
683 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000684 p, datlen, alg, compat)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000685 fatal("%s: ssh_agent_sign failed: %s",
686 __func__, ssh_err(r));
687 }
Damien Miller85b45e02013-07-20 13:21:52 +1000688 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000689 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000690
djm@openbsd.org523463a2015-02-16 22:13:32 +0000691 debug3("%s: %s signature %p(%zu)", __func__,
692 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000693
djm@openbsd.org141efe42015-01-14 20:05:27 +0000694 sshbuf_reset(m);
695 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
696 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000697
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000698 free(alg);
Darren Tuckera627d422013-06-02 07:31:17 +1000699 free(p);
700 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000701
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000702 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000703
704 /* Turn on permissions for getpwnam */
705 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
706
707 return (0);
708}
709
710/* Retrieves the password entry and also checks if the user is permitted */
711
712int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000713mm_answer_pwnamallow(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000714{
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000715 struct ssh *ssh = active_state; /* XXX */
Darren Tuckerb09b6772004-06-22 15:06:46 +1000716 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000717 struct passwd *pwent;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000718 int r, allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000719 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000720
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000721 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000722
723 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000724 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000726 if ((r = sshbuf_get_cstring(m, &username, NULL)) != 0)
727 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728
Darren Tuckerb09b6772004-06-22 15:06:46 +1000729 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000730
Darren Tuckerb09b6772004-06-22 15:06:46 +1000731 authctxt->user = xstrdup(username);
732 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000733 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000734
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000735 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000736
737 if (pwent == NULL) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000738 if ((r = sshbuf_put_u8(m, 0)) != 0)
739 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf96d1832003-11-18 22:01:48 +1100740 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000741 goto out;
742 }
743
744 allowed = 1;
745 authctxt->pw = pwent;
746 authctxt->valid = 1;
747
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000748 /* XXX don't sent pwent to unpriv; send fake class/dir/shell too */
749 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
750 (r = sshbuf_put_string(m, pwent, sizeof(*pwent))) != 0 ||
751 (r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
752 (r = sshbuf_put_cstring(m, "*")) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000753#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000754 (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000755#endif
756#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000757 (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
Kevin Steves7e147602002-03-22 18:07:17 +0000758#endif
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000759 (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
760 (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
761 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100762
763 out:
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000764 ssh_packet_set_log_preamble(ssh, "%suser %s",
765 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000766 if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
767 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf2e407e2011-05-20 19:04:14 +1000768
769#define M_CP_STROPT(x) do { \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000770 if (options.x != NULL) { \
771 if ((r = sshbuf_put_cstring(m, options.x)) != 0) \
772 fatal("%s: buffer error: %s", \
773 __func__, ssh_err(r)); \
774 } \
Damien Millerf2e407e2011-05-20 19:04:14 +1000775 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000776#define M_CP_STRARRAYOPT(x, nx) do { \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000777 for (i = 0; i < options.nx; i++) { \
778 if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
779 fatal("%s: buffer error: %s", \
780 __func__, ssh_err(r)); \
781 } \
Damien Millerd8478b62011-05-29 21:39:36 +1000782 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000783 /* See comment in servconf.h */
784 COPY_MATCH_STRING_OPTS();
785#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000786#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100787
788 /* Create valid auth method lists */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000789 if (auth2_setup_methods_lists(authctxt) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100790 /*
791 * The monitor will continue long enough to let the child
792 * run to it's packet_disconnect(), but it must not allow any
793 * authentication to succeed.
794 */
795 debug("%s: no valid authentication method lists", __func__);
796 }
797
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000798 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000799 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000800
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000801 /* Allow service/style information on the auth context */
802 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
803 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
804
Damien Millera33501b2002-05-08 12:24:42 +1000805#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000806 if (options.use_pam)
807 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000808#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000809
810 return (0);
811}
812
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000813int mm_answer_auth2_read_banner(int sock, struct sshbuf *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000814{
815 char *banner;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000816 int r;
Damien Miller5ad9fd92002-05-13 11:07:41 +1000817
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000818 sshbuf_reset(m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000819 banner = auth2_read_banner();
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000820 if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
821 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000822 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000823 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000824
825 return (0);
826}
827
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000828int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000829mm_answer_authserv(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000830{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000831 int r;
832
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000833 monitor_permit_authentications(1);
834
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000835 if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
836 (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
837 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000838 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000839 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000840
841 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000842 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000843 authctxt->style = NULL;
844 }
845
846 return (0);
847}
848
849int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000850mm_answer_authpassword(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000851{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000852 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000853 static int call_count;
854 char *passwd;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000855 int r, authenticated;
856 size_t plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000857
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000858 if (!options.password_authentication)
859 fatal("%s: password authentication not enabled", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000860 if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
861 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000862 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000863 authenticated = options.password_authentication &&
djm@openbsd.org7c856852018-03-03 03:15:51 +0000864 auth_password(ssh, passwd);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000865 explicit_bzero(passwd, plen);
Darren Tuckera627d422013-06-02 07:31:17 +1000866 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000867
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000868 sshbuf_reset(m);
869 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
870 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker01558b72016-07-18 09:33:25 +1000871#ifdef USE_PAM
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000872 if ((r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0)
873 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker01558b72016-07-18 09:33:25 +1000874#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000875
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000876 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000877 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000878
879 call_count++;
880 if (plen == 0 && call_count == 1)
881 auth_method = "none";
882 else
883 auth_method = "password";
884
885 /* Causes monitor loop to terminate if authenticated */
886 return (authenticated);
887}
888
889#ifdef BSD_AUTH
890int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000891mm_answer_bsdauthquery(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000892{
893 char *name, *infotxt;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000894 u_int numprompts, *echo_on, success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000895 char **prompts;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000896 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000897
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000898 if (!options.kbd_interactive_authentication)
899 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb7df3af2003-02-24 11:55:46 +1100900 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
901 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000902
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000903 sshbuf_reset(m);
904 if ((r = sshbuf_put_u32(m, success)) != 0)
905 fatal("%s: buffer error: %s", __func__, ssh_err(r));
906 if (success) {
907 if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
908 fatal("%s: buffer error: %s", __func__, ssh_err(r));
909 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000910
Damien Millerb7df3af2003-02-24 11:55:46 +1100911 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000912 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000913
Damien Millerb7df3af2003-02-24 11:55:46 +1100914 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000915 free(name);
916 free(infotxt);
917 free(prompts);
918 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000919 }
920
921 return (0);
922}
923
924int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000925mm_answer_bsdauthrespond(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926{
927 char *response;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000928 int r, authok;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000929
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000930 if (!options.kbd_interactive_authentication)
931 fatal("%s: kbd-int authentication not enabled", __func__);
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +0000932 if (authctxt->as == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000933 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000934
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000935 if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
936 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000937 authok = options.challenge_response_authentication &&
938 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000939 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000940 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000941 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000942
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000943 sshbuf_reset(m);
944 if ((r = sshbuf_put_u32(m, authok)) != 0)
945 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000946
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000947 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000948 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000949
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000950 auth_method = "keyboard-interactive";
951 auth_submethod = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000952
953 return (authok != 0);
954}
955#endif
956
Damien Miller79418552002-04-23 20:28:48 +1000957#ifdef USE_PAM
958int
Damien Miller120a1ec2018-07-10 19:39:52 +1000959mm_answer_pam_start(int sock, struct sshbuf *m)
Damien Miller79418552002-04-23 20:28:48 +1000960{
Damien Miller4e448a32003-05-14 15:11:48 +1000961 if (!options.use_pam)
962 fatal("UsePAM not set, but ended up in %s anyway", __func__);
963
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100964 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000965
Damien Miller1f499fd2003-08-25 13:08:49 +1000966 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
Damien Miller775f8a22016-08-31 10:48:07 +1000967 if (options.kbd_interactive_authentication)
968 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller1f499fd2003-08-25 13:08:49 +1000969
Damien Miller79418552002-04-23 20:28:48 +1000970 return (0);
971}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000972
Damien Miller1f499fd2003-08-25 13:08:49 +1000973int
Damien Miller120a1ec2018-07-10 19:39:52 +1000974mm_answer_pam_account(int sock, struct sshbuf *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000975{
976 u_int ret;
Damien Miller120a1ec2018-07-10 19:39:52 +1000977 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +1100978
Damien Miller1f499fd2003-08-25 13:08:49 +1000979 if (!options.use_pam)
Damien Miller775f8a22016-08-31 10:48:07 +1000980 fatal("%s: PAM not enabled", __func__);
Damien Miller1f499fd2003-08-25 13:08:49 +1000981
982 ret = do_pam_account();
983
Damien Miller120a1ec2018-07-10 19:39:52 +1000984 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
985 (r = sshbuf_put_stringb(m, loginmsg)) != 0)
986 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller1f499fd2003-08-25 13:08:49 +1000987
Darren Tucker7eda5922004-06-22 13:22:50 +1000988 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000989
990 return (ret);
991}
992
Damien Miller4f9f42a2003-05-10 19:28:02 +1000993static void *sshpam_ctxt, *sshpam_authok;
994extern KbdintDevice sshpam_device;
995
996int
Damien Miller120a1ec2018-07-10 19:39:52 +1000997mm_answer_pam_init_ctx(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000998{
Damien Miller120a1ec2018-07-10 19:39:52 +1000999 u_int ok = 0;
1000 int r;
1001
Damien Miller4f9f42a2003-05-10 19:28:02 +10001002 debug3("%s", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001003 if (!options.kbd_interactive_authentication)
1004 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001005 if (sshpam_ctxt != NULL)
1006 fatal("%s: already called", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001007 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1008 sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001009 sshbuf_reset(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001010 if (sshpam_ctxt != NULL) {
1011 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
Damien Millerb38b95f2016-08-29 11:47:07 +10001012 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
Damien Miller120a1ec2018-07-10 19:39:52 +10001013 ok = 1;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001014 }
Damien Miller120a1ec2018-07-10 19:39:52 +10001015 if ((r = sshbuf_put_u32(m, ok)) != 0)
1016 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker7eda5922004-06-22 13:22:50 +10001017 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001018 return (0);
1019}
1020
1021int
Damien Miller120a1ec2018-07-10 19:39:52 +10001022mm_answer_pam_query(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001023{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001024 char *name = NULL, *info = NULL, **prompts = NULL;
1025 u_int i, num = 0, *echo_on = 0;
Damien Miller120a1ec2018-07-10 19:39:52 +10001026 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001027
1028 debug3("%s", __func__);
1029 sshpam_authok = NULL;
Damien Millerb38b95f2016-08-29 11:47:07 +10001030 if (sshpam_ctxt == NULL)
1031 fatal("%s: no context", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001032 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1033 &num, &prompts, &echo_on);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001034 if (ret == 0 && num == 0)
1035 sshpam_authok = sshpam_ctxt;
1036 if (num > 1 || name == NULL || info == NULL)
Damien Millerb38b95f2016-08-29 11:47:07 +10001037 fatal("sshpam_device.query failed");
1038 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
Damien Miller120a1ec2018-07-10 19:39:52 +10001039 sshbuf_reset(m);
1040 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1041 (r = sshbuf_put_cstring(m, name)) != 0 ||
1042 (r = sshbuf_put_cstring(m, info)) != 0 ||
1043 (r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0 ||
1044 (r = sshbuf_put_u32(m, num)) != 0)
1045 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +10001046 free(name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001047 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001048 for (i = 0; i < num; ++i) {
Damien Miller120a1ec2018-07-10 19:39:52 +10001049 if ((r = sshbuf_put_cstring(m, prompts[i])) != 0 ||
1050 (r = sshbuf_put_u32(m, echo_on[i])) != 0)
1051 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +10001052 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001053 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001054 free(prompts);
1055 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001056 auth_method = "keyboard-interactive";
1057 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001058 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001059 return (0);
1060}
1061
1062int
Damien Miller120a1ec2018-07-10 19:39:52 +10001063mm_answer_pam_respond(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001064{
1065 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001066 u_int i, num;
Damien Miller120a1ec2018-07-10 19:39:52 +10001067 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001068
1069 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001070 if (sshpam_ctxt == NULL)
1071 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001072 sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001073 if ((r = sshbuf_get_u32(m, &num)) != 0)
1074 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001075 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001076 resp = xcalloc(num, sizeof(char *));
Damien Miller120a1ec2018-07-10 19:39:52 +10001077 for (i = 0; i < num; ++i) {
1078 if ((r = sshbuf_get_cstring(m, &(resp[i]), NULL)) != 0)
1079 fatal("%s: buffer error: %s",
1080 __func__, ssh_err(r));
1081 }
Damien Miller4f9f42a2003-05-10 19:28:02 +10001082 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1083 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001084 free(resp[i]);
1085 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001086 } else {
1087 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1088 }
Damien Miller120a1ec2018-07-10 19:39:52 +10001089 sshbuf_reset(m);
1090 if ((r = sshbuf_put_u32(m, ret)) != 0)
1091 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker7eda5922004-06-22 13:22:50 +10001092 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001093 auth_method = "keyboard-interactive";
1094 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001095 if (ret == 0)
1096 sshpam_authok = sshpam_ctxt;
1097 return (0);
1098}
1099
1100int
Damien Miller120a1ec2018-07-10 19:39:52 +10001101mm_answer_pam_free_ctx(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001102{
Damien Miller5e75f512015-08-11 13:34:12 +10001103 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001104
1105 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001106 if (sshpam_ctxt == NULL)
1107 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001108 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001109 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001110 sshbuf_reset(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001111 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Millerb38b95f2016-08-29 11:47:07 +10001112 /* Allow another attempt */
1113 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller15b05cf2012-12-03 09:53:20 +11001114 auth_method = "keyboard-interactive";
1115 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001116 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001117}
Damien Miller79418552002-04-23 20:28:48 +10001118#endif
1119
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001120int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001121mm_answer_keyallowed(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001122{
djm@openbsd.org7c856852018-03-03 03:15:51 +00001123 struct ssh *ssh = active_state; /* XXX */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001124 struct sshkey *key = NULL;
Damien Millera10f5612002-09-12 09:49:15 +10001125 char *cuser, *chost;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001126 u_int pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001127 enum mm_keytype type = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001128 int r, allowed = 0;
1129 struct sshauthopt *opts = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001130
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001131 debug3("%s entering", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001132 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1133 (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
1134 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
1135 (r = sshkey_froms(m, &key)) != 0 ||
1136 (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
1137 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001138
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001139 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001140
Damien Miller3e3b5142003-11-17 21:13:40 +11001141 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001142 /* These should not make it past the privsep child */
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001143 if (sshkey_type_plain(key->type) == KEY_RSA &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001144 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1145 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1146
Darren Tucker47eede72005-03-14 23:08:12 +11001147 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001148 case MM_USERKEY:
Damien Miller7a8f5b32006-03-31 23:14:23 +11001149 auth_method = "publickey";
djm@openbsd.org7c856852018-03-03 03:15:51 +00001150 if (!options.pubkey_authentication)
1151 break;
1152 if (auth2_key_already_used(authctxt, key))
1153 break;
1154 if (match_pattern_list(sshkey_ssh_name(key),
1155 options.pubkey_key_types, 0) != 1)
1156 break;
1157 allowed = user_key_allowed(ssh, authctxt->pw, key,
1158 pubkey_auth_attempt, &opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001159 break;
1160 case MM_HOSTKEY:
djm@openbsd.org7c856852018-03-03 03:15:51 +00001161 auth_method = "hostbased";
1162 if (!options.hostbased_authentication)
1163 break;
1164 if (auth2_key_already_used(authctxt, key))
1165 break;
1166 if (match_pattern_list(sshkey_ssh_name(key),
1167 options.hostbased_key_types, 0) != 1)
1168 break;
1169 allowed = hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001170 cuser, chost, key);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001171 auth2_record_info(authctxt,
Damien Miller20bdcd72013-07-18 16:10:09 +10001172 "client user \"%.100s\", client host \"%.100s\"",
1173 cuser, chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001174 break;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001175 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001176 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001177 break;
1178 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001179 }
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001180
djm@openbsd.org7c856852018-03-03 03:15:51 +00001181 debug3("%s: %s authentication%s: %s key is %s", __func__,
1182 auth_method, pubkey_auth_attempt ? "" : " test",
1183 (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
1184 allowed ? "allowed" : "not allowed");
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001185
djm@openbsd.org8f574952017-06-24 06:34:38 +00001186 auth2_record_key(authctxt, 0, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001187
1188 /* clear temporarily storage (used by verify) */
1189 monitor_reset_key_state();
1190
1191 if (allowed) {
1192 /* Save temporarily for comparison in verify */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001193 if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
1194 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001195 key_blobtype = type;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001196 key_opts = opts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001197 hostbased_cuser = cuser;
1198 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001199 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001200 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001201 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001202 free(cuser);
1203 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001204 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001205 sshkey_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001206
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001207 sshbuf_reset(m);
1208 if ((r = sshbuf_put_u32(m, allowed)) != 0)
1209 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org7c856852018-03-03 03:15:51 +00001210 if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
1211 fatal("%s: sshauthopt_serialise: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001212 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001213
djm@openbsd.org7c856852018-03-03 03:15:51 +00001214 if (!allowed)
1215 sshauthopt_free(opts);
1216
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001217 return (0);
1218}
1219
1220static int
1221monitor_valid_userblob(u_char *data, u_int datalen)
1222{
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001223 struct sshbuf *b;
1224 const u_char *p;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001225 char *userstyle, *cp;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001226 size_t len;
1227 u_char type;
1228 int r, fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001229
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001230 if ((b = sshbuf_new()) == NULL)
1231 fatal("%s: sshbuf_new", __func__);
1232 if ((r = sshbuf_put(b, data, datalen)) != 0)
1233 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001234
1235 if (datafellows & SSH_OLD_SESSIONID) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001236 p = sshbuf_ptr(b);
1237 len = sshbuf_len(b);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001238 if ((session_id2 == NULL) ||
1239 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001240 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001241 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001242 if ((r = sshbuf_consume(b, session_id2_len)) != 0)
1243 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001244 } else {
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001245 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1246 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromf67e0772002-06-06 20:58:19 +00001247 if ((session_id2 == NULL) ||
1248 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001249 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001250 fail++;
1251 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001252 if ((r = sshbuf_get_u8(b, &type)) != 0)
1253 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1254 if (type != SSH2_MSG_USERAUTH_REQUEST)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001255 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001256 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1257 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4ce189d2013-04-23 15:17:52 +10001258 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1259 authctxt->style ? ":" : "",
1260 authctxt->style ? authctxt->style : "");
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001261 if (strcmp(userstyle, cp) != 0) {
1262 logit("wrong user name passed to monitor: "
1263 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001264 fail++;
1265 }
Darren Tuckera627d422013-06-02 07:31:17 +10001266 free(userstyle);
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001267 free(cp);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001268 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1269 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1270 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001271 if (strcmp("publickey", cp) != 0)
1272 fail++;
1273 free(cp);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001274 if ((r = sshbuf_get_u8(b, &type)) != 0)
1275 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1276 if (type == 0)
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001277 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001278 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1279 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1280 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1281 if (sshbuf_len(b) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001282 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001283 sshbuf_free(b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001284 return (fail == 0);
1285}
1286
1287static int
Damien Millera10f5612002-09-12 09:49:15 +10001288monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1289 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001290{
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001291 struct sshbuf *b;
1292 const u_char *p;
1293 char *cp, *userstyle;
1294 size_t len;
1295 int r, fail = 0;
1296 u_char type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001297
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001298 if ((b = sshbuf_new()) == NULL)
1299 fatal("%s: sshbuf_new", __func__);
1300 if ((r = sshbuf_put(b, data, datalen)) != 0 ||
1301 (r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1302 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001303
Ben Lindstromf67e0772002-06-06 20:58:19 +00001304 if ((session_id2 == NULL) ||
1305 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001306 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001307 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001308
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001309 if ((r = sshbuf_get_u8(b, &type)) != 0)
1310 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1311 if (type != SSH2_MSG_USERAUTH_REQUEST)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001312 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001313 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1314 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4ce189d2013-04-23 15:17:52 +10001315 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1316 authctxt->style ? ":" : "",
1317 authctxt->style ? authctxt->style : "");
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001318 if (strcmp(userstyle, cp) != 0) {
1319 logit("wrong user name passed to monitor: "
1320 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001321 fail++;
1322 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001323 free(userstyle);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001324 free(cp);
1325 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1326 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1327 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1328 if (strcmp(cp, "hostbased") != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001329 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001330 free(cp);
1331 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1332 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1333 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001334
1335 /* verify client host, strip trailing dot if necessary */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001336 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1337 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1338 if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
1339 cp[len - 1] = '\0';
1340 if (strcmp(cp, chost) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001341 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001342 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001343
1344 /* verify client user */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001345 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1346 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1347 if (strcmp(cp, cuser) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001348 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001349 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001350
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001351 if (sshbuf_len(b) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001352 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001353 sshbuf_free(b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001354 return (fail == 0);
1355}
1356
1357int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001358mm_answer_keyverify(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001359{
djm@openbsd.org7c856852018-03-03 03:15:51 +00001360 struct ssh *ssh = active_state; /* XXX */
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001361 struct sshkey *key;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001362 u_char *signature, *data, *blob;
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001363 char *sigalg;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001364 size_t signaturelen, datalen, bloblen;
1365 int r, ret, valid_data = 0, encoded_ret;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001366
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001367 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1368 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001369 (r = sshbuf_get_string(m, &data, &datalen)) != 0 ||
1370 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001371 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001372
1373 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001374 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001375 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001376
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001377 /* Empty signature algorithm means NULL. */
1378 if (*sigalg == '\0') {
1379 free(sigalg);
1380 sigalg = NULL;
1381 }
1382
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001383 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1384 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1385 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001386
1387 switch (key_blobtype) {
1388 case MM_USERKEY:
1389 valid_data = monitor_valid_userblob(data, datalen);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001390 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001391 break;
1392 case MM_HOSTKEY:
1393 valid_data = monitor_valid_hostbasedblob(data, datalen,
1394 hostbased_cuser, hostbased_chost);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001395 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001396 break;
1397 default:
1398 valid_data = 0;
1399 break;
1400 }
1401 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001402 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001403
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001404 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001405 sigalg, active_state->compat);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001406 debug3("%s: %s %p signature %s", __func__, auth_method, key,
1407 (ret == 0) ? "verified" : "unverified");
1408 auth2_record_key(authctxt, ret == 0, key);
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001409
Darren Tuckera627d422013-06-02 07:31:17 +10001410 free(blob);
1411 free(signature);
1412 free(data);
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001413 free(sigalg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001414
djm@openbsd.org7c856852018-03-03 03:15:51 +00001415 if (key_blobtype == MM_USERKEY)
1416 auth_activate_options(ssh, key_opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001417 monitor_reset_key_state();
1418
djm@openbsd.org8f574952017-06-24 06:34:38 +00001419 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001420 sshbuf_reset(m);
1421
1422 /* encode ret != 0 as positive integer, since we're sending u32 */
1423 encoded_ret = (ret != 0);
1424 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1425 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001426 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001427
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001428 return ret == 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001429}
1430
1431static void
1432mm_record_login(Session *s, struct passwd *pw)
1433{
djm@openbsd.org95767262016-03-07 19:02:43 +00001434 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001435 socklen_t fromlen;
1436 struct sockaddr_storage from;
1437
1438 /*
1439 * Get IP address of client. If the connection is not a socket, let
1440 * the address be 0.0.0.0.
1441 */
1442 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001443 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001444 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001445 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001446 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001447 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001448 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001449 }
1450 }
1451 /* Record that there was a login on that tty from the remote host. */
1452 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +00001453 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001454 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001455}
1456
1457static void
1458mm_session_close(Session *s)
1459{
Damien Miller04bd8b02003-05-25 14:38:33 +10001460 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001461 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001462 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001463 session_pty_cleanup2(s);
1464 }
Damien Miller7207f642008-05-19 15:34:50 +10001465 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001466}
1467
1468int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001469mm_answer_pty(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001470{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001471 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001472 Session *s;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001473 int r, res, fd0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001474
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001475 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001476
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001477 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001478 s = session_new();
1479 if (s == NULL)
1480 goto error;
1481 s->authctxt = authctxt;
1482 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001483 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001484 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1485 if (res == 0)
1486 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001487 pty_setowner(authctxt->pw, s->tty);
1488
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001489 if ((r = sshbuf_put_u32(m, 1)) != 0 ||
1490 (r = sshbuf_put_cstring(m, s->tty)) != 0)
1491 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492
1493 /* We need to trick ttyslot */
1494 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001495 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001496
1497 mm_record_login(s, authctxt->pw);
1498
1499 /* Now we can close the file descriptor again */
1500 close(0);
1501
Darren Tucker09991742004-07-17 17:05:14 +10001502 /* send messages generated by record_login */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001503 if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
1504 fatal("%s: put login message: %s", __func__, ssh_err(r));
1505 sshbuf_reset(loginmsg);
Darren Tucker09991742004-07-17 17:05:14 +10001506
1507 mm_request_send(sock, MONITOR_ANS_PTY, m);
1508
Damien Miller54fd7cf2007-09-17 12:04:08 +10001509 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1510 mm_send_fd(sock, s->ttyfd) == -1)
1511 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001512
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001513 /* make sure nothing uses fd 0 */
1514 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001515 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001516 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001517 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001518
1519 /* slave is not needed */
1520 close(s->ttyfd);
1521 s->ttyfd = s->ptyfd;
1522 /* no need to dup() because nobody closes ptyfd */
1523 s->ptymaster = s->ptyfd;
1524
Damien Miller9ab00b42006-08-05 12:40:11 +10001525 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001526
1527 return (0);
1528
1529 error:
1530 if (s != NULL)
1531 mm_session_close(s);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001532 if ((r = sshbuf_put_u32(m, 0)) != 0)
1533 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001534 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001535 return (0);
1536}
1537
1538int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001539mm_answer_pty_cleanup(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001540{
1541 Session *s;
1542 char *tty;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001543 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001544
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001545 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001546
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001547 if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
1548 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001549 if ((s = session_by_tty(tty)) != NULL)
1550 mm_session_close(s);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001551 sshbuf_reset(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001552 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001553 return (0);
1554}
1555
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001556int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001557mm_answer_term(int sock, struct sshbuf *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001558{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001559 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001560 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001561 int res, status;
1562
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001563 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001564
1565 /* The child is terminating */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001566 session_destroy_all(ssh, &mm_session_close);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001567
Darren Tucker52358d62008-03-11 22:58:25 +11001568#ifdef USE_PAM
1569 if (options.use_pam)
1570 sshpam_cleanup();
1571#endif
1572
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001573 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001574 if (errno != EINTR)
1575 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001576
1577 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1578
1579 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001580 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001581}
1582
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001583#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001584/* Report that an audit event occurred */
1585int
Damien Miller120a1ec2018-07-10 19:39:52 +10001586mm_answer_audit_event(int socket, struct sshbuf *m)
Darren Tucker269a1ea2005-02-03 00:20:53 +11001587{
Damien Miller120a1ec2018-07-10 19:39:52 +10001588 u_int n;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001589 ssh_audit_event_t event;
Damien Miller120a1ec2018-07-10 19:39:52 +10001590 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001591
1592 debug3("%s entering", __func__);
1593
Damien Miller120a1ec2018-07-10 19:39:52 +10001594 if ((r = sshbuf_get_u32(m, &n)) != 0)
1595 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1596 event = (ssh_audit_event_t)n;
1597 switch (event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001598 case SSH_AUTH_FAIL_PUBKEY:
1599 case SSH_AUTH_FAIL_HOSTBASED:
1600 case SSH_AUTH_FAIL_GSSAPI:
1601 case SSH_LOGIN_EXCEED_MAXTRIES:
1602 case SSH_LOGIN_ROOT_DENIED:
1603 case SSH_CONNECTION_CLOSE:
1604 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001605 audit_event(event);
1606 break;
1607 default:
1608 fatal("Audit event type %d not permitted", event);
1609 }
1610
1611 return (0);
1612}
1613
1614int
Damien Miller120a1ec2018-07-10 19:39:52 +10001615mm_answer_audit_command(int socket, struct sshbuf *m)
Darren Tucker269a1ea2005-02-03 00:20:53 +11001616{
Darren Tucker269a1ea2005-02-03 00:20:53 +11001617 char *cmd;
Damien Miller120a1ec2018-07-10 19:39:52 +10001618 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001619
1620 debug3("%s entering", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +10001621 if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
1622 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker269a1ea2005-02-03 00:20:53 +11001623 /* sanity check command, if so how? */
1624 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001625 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001626 return (0);
1627}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001628#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001629
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001630void
markus@openbsd.org1e0cdf82017-05-31 08:09:45 +00001631monitor_clear_keystate(struct monitor *pmonitor)
1632{
1633 struct ssh *ssh = active_state; /* XXX */
1634
1635 ssh_clear_newkeys(ssh, MODE_IN);
1636 ssh_clear_newkeys(ssh, MODE_OUT);
1637 sshbuf_free(child_state);
1638 child_state = NULL;
1639}
1640
1641void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001642monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001643{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001644 struct ssh *ssh = active_state; /* XXX */
1645 struct kex *kex;
1646 int r;
1647
1648 debug3("%s: packet_set_state", __func__);
1649 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1650 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1651 sshbuf_free(child_state);
1652 child_state = NULL;
1653
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +00001654 if ((kex = ssh->kex) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001655 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001656#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001657 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1658 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +00001659 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1660 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1661 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001662 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1663 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001664# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001665 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001666# endif
Damien Miller773dda22015-01-30 23:10:17 +11001667#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001668 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1669 kex->load_host_public_key=&get_hostkey_public_by_type;
1670 kex->load_host_private_key=&get_hostkey_private_by_type;
1671 kex->host_key_index=&get_hostkey_index;
1672 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001673 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001674}
1675
1676/* This function requries careful sanity checking */
1677
1678void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001679mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001680{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001681 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001682
markus@openbsd.org091c3022015-01-19 19:52:16 +00001683 if ((child_state = sshbuf_new()) == NULL)
1684 fatal("%s: sshbuf_new failed", __func__);
1685 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1686 child_state);
1687 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001688}
1689
1690
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001691/* XXX */
1692
1693#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001694 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001695 fatal("fcntl(%d, F_SETFD)", x); \
1696} while (0)
1697
1698static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001699monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001700{
Damien Miller8f0bf232011-06-20 14:42:23 +10001701 int pair[2];
markus@openbsd.org84008602017-05-31 10:04:29 +00001702#ifdef SO_ZEROIZE
1703 int on = 1;
1704#endif
Damien Miller8f0bf232011-06-20 14:42:23 +10001705
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001706 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001707 fatal("%s: socketpair: %s", __func__, strerror(errno));
markus@openbsd.org84008602017-05-31 10:04:29 +00001708#ifdef SO_ZEROIZE
1709 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1710 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1711 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1712 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1713#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001714 FD_CLOSEONEXEC(pair[0]);
1715 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001716 mon->m_recvfd = pair[0];
1717 mon->m_sendfd = pair[1];
1718
1719 if (do_logfds) {
1720 if (pipe(pair) == -1)
1721 fatal("%s: pipe: %s", __func__, strerror(errno));
1722 FD_CLOSEONEXEC(pair[0]);
1723 FD_CLOSEONEXEC(pair[1]);
1724 mon->m_log_recvfd = pair[0];
1725 mon->m_log_sendfd = pair[1];
1726 } else
1727 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001728}
1729
1730#define MM_MEMSIZE 65536
1731
1732struct monitor *
1733monitor_init(void)
1734{
1735 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001736
Damien Miller07d86be2006-03-26 14:19:21 +11001737 mon = xcalloc(1, sizeof(*mon));
Damien Miller8f0bf232011-06-20 14:42:23 +10001738 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001739
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001740 return mon;
1741}
1742
1743void
1744monitor_reinit(struct monitor *mon)
1745{
Damien Miller8f0bf232011-06-20 14:42:23 +10001746 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001747}
Darren Tucker0efd1552003-08-26 11:49:55 +10001748
1749#ifdef GSSAPI
1750int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001751mm_answer_gss_setup_ctx(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001752{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001753 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001754 OM_uint32 major;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001755 size_t len;
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001756 u_char *p;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001757 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +10001758
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001759 if (!options.gss_authentication)
1760 fatal("%s: GSSAPI authentication not enabled", __func__);
1761
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001762 if ((r = sshbuf_get_string(m, &p, &len)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001763 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001764 goid.elements = p;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001765 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001766
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001767 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001768
Darren Tuckera627d422013-06-02 07:31:17 +10001769 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001770
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001771 sshbuf_reset(m);
1772 if ((r = sshbuf_put_u32(m, major)) != 0)
1773 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001774
Damien Miller6fd6def2005-11-05 15:07:05 +11001775 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001776
1777 /* Now we have a context, enable the step */
1778 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1779
1780 return (0);
1781}
1782
1783int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001784mm_answer_gss_accept_ctx(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001785{
1786 gss_buffer_desc in;
1787 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001788 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001789 OM_uint32 flags = 0; /* GSI needs this */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001790 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +10001791
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001792 if (!options.gss_authentication)
1793 fatal("%s: GSSAPI authentication not enabled", __func__);
1794
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001795 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001796 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001797 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001798 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001799
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001800 sshbuf_reset(m);
1801 if ((r = sshbuf_put_u32(m, major)) != 0 ||
1802 (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
1803 (r = sshbuf_put_u32(m, flags)) != 0)
1804 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001805 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001806
1807 gss_release_buffer(&minor, &out);
1808
Damien Miller6fd6def2005-11-05 15:07:05 +11001809 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001810 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1811 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001812 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001813 }
1814 return (0);
1815}
1816
1817int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001818mm_answer_gss_checkmic(int sock, struct sshbuf *m)
Damien Miller0425d402003-11-17 22:18:21 +11001819{
1820 gss_buffer_desc gssbuf, mic;
1821 OM_uint32 ret;
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001822 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +11001823
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001824 if (!options.gss_authentication)
1825 fatal("%s: GSSAPI authentication not enabled", __func__);
1826
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001827 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
1828 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001829 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller787b2ec2003-11-21 23:56:47 +11001830
Damien Miller0425d402003-11-17 22:18:21 +11001831 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001832
Darren Tuckera627d422013-06-02 07:31:17 +10001833 free(gssbuf.value);
1834 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001835
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001836 sshbuf_reset(m);
1837 if ((r = sshbuf_put_u32(m, ret)) != 0)
1838 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller787b2ec2003-11-21 23:56:47 +11001839
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001840 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001841
Damien Miller0425d402003-11-17 22:18:21 +11001842 if (!GSS_ERROR(ret))
1843 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001844
Damien Miller0425d402003-11-17 22:18:21 +11001845 return (0);
1846}
1847
1848int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001849mm_answer_gss_userok(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001850{
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001851 int r, authenticated;
djm@openbsd.org8f574952017-06-24 06:34:38 +00001852 const char *displayname;
Darren Tucker0efd1552003-08-26 11:49:55 +10001853
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001854 if (!options.gss_authentication)
1855 fatal("%s: GSSAPI authentication not enabled", __func__);
1856
Darren Tucker0efd1552003-08-26 11:49:55 +10001857 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1858
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001859 sshbuf_reset(m);
1860 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1861 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001862
1863 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001864 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001865
Damien Miller6fd6def2005-11-05 15:07:05 +11001866 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001867
djm@openbsd.org8f574952017-06-24 06:34:38 +00001868 if ((displayname = ssh_gssapi_displayname()) != NULL)
1869 auth2_record_info(authctxt, "%s", displayname);
1870
Darren Tucker0efd1552003-08-26 11:49:55 +10001871 /* Monitor loop will terminate if authenticated */
1872 return (authenticated);
1873}
1874#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001875