blob: b30813b4d2f06a27455a108028b58c37d84dc7ca [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 Millerd7834352006-08-05 12:39:39 +100032#include "openbsd-compat/sys-tree.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110033#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000034
Darren Tucker39972492006-07-12 22:22:46 +100035#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100036#include <fcntl.h>
djm@openbsd.org1a31d022016-05-02 08:49:03 +000037#include <limits.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110038#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110039#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110040#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100041#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110042#include <signal.h>
Damien Miller9af21972015-02-24 09:04:32 +110043#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000044#include <stdint.h>
Damien Miller9af21972015-02-24 09:04:32 +110045#endif
Damien Millere7a1e5c2006-08-05 11:34:19 +100046#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100047#include <string.h>
Damien Miller86687062014-07-02 15:28:02 +100048#include <stdarg.h>
49#include <stdio.h>
Damien Miller607aede2006-09-01 15:48:19 +100050#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100051#ifdef HAVE_POLL_H
52#include <poll.h>
53#else
54# ifdef HAVE_SYS_POLL_H
55# include <sys/poll.h>
56# endif
57#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000058
Damien Miller1f0311c2014-05-15 14:24:09 +100059#ifdef WITH_OPENSSL
Damien Miller03e20032006-03-15 11:16:59 +110060#include <openssl/dh.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100061#endif
Damien Miller03e20032006-03-15 11:16:59 +110062
Damien Millerb84886b2008-05-19 15:05:07 +100063#include "openbsd-compat/sys-queue.h"
Damien Miller8f0bf232011-06-20 14:42:23 +100064#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100065#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000066#include "ssh.h"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +000067#include "sshkey.h"
markus@openbsd.org235c7c42018-07-09 21:53:45 +000068#include "sshbuf.h"
Damien Millerd7834352006-08-05 12:39:39 +100069#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000072#include "kex.h"
73#include "dh.h"
Darren Tucker01558b72016-07-18 09:33:25 +100074#include "auth-pam.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000075#include "packet.h"
76#include "auth-options.h"
77#include "sshpty.h"
78#include "channels.h"
79#include "session.h"
80#include "sshlogin.h"
81#include "canohost.h"
82#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100083#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000084#include "servconf.h"
85#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +100086#ifdef GSSAPI
87#include "ssh-gss.h"
88#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000089#include "monitor_wrap.h"
90#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000091#include "compat.h"
92#include "ssh2.h"
Damien Miller85b45e02013-07-20 13:21:52 +100093#include "authfd.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +000094#include "match.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +000095#include "ssherr.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000096
Darren Tucker0efd1552003-08-26 11:49:55 +100097#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +100098static Gssctxt *gsscontext = NULL;
99#endif
100
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101/* Imports */
102extern ServerOptions options;
103extern u_int utmp_len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000104extern u_char session_id[];
markus@openbsd.org2808d182018-07-09 21:26:02 +0000105extern struct sshbuf *loginmsg;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000106extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107
108/* State exported from the child */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000109static struct sshbuf *child_state;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000110
Damien Miller469954d2003-06-18 20:25:33 +1000111/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000112
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000113int mm_answer_moduli(int, struct sshbuf *);
114int mm_answer_sign(int, struct sshbuf *);
115int mm_answer_pwnamallow(int, struct sshbuf *);
116int mm_answer_auth2_read_banner(int, struct sshbuf *);
117int mm_answer_authserv(int, struct sshbuf *);
118int mm_answer_authpassword(int, struct sshbuf *);
119int mm_answer_bsdauthquery(int, struct sshbuf *);
120int mm_answer_bsdauthrespond(int, struct sshbuf *);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000121int mm_answer_keyallowed(int, struct sshbuf *);
122int mm_answer_keyverify(int, struct sshbuf *);
123int mm_answer_pty(int, struct sshbuf *);
124int mm_answer_pty_cleanup(int, struct sshbuf *);
125int mm_answer_term(int, struct sshbuf *);
126int mm_answer_rsa_keyallowed(int, struct sshbuf *);
127int mm_answer_rsa_challenge(int, struct sshbuf *);
128int mm_answer_rsa_response(int, struct sshbuf *);
129int mm_answer_sesskey(int, struct sshbuf *);
130int mm_answer_sessid(int, struct sshbuf *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000131
Damien Miller79418552002-04-23 20:28:48 +1000132#ifdef USE_PAM
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000133int mm_answer_pam_start(int, struct sshbuf *);
134int mm_answer_pam_account(int, struct sshbuf *);
135int mm_answer_pam_init_ctx(int, struct sshbuf *);
136int mm_answer_pam_query(int, struct sshbuf *);
137int mm_answer_pam_respond(int, struct sshbuf *);
138int mm_answer_pam_free_ctx(int, struct sshbuf *);
Damien Miller79418552002-04-23 20:28:48 +1000139#endif
140
Darren Tucker0efd1552003-08-26 11:49:55 +1000141#ifdef GSSAPI
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000142int mm_answer_gss_setup_ctx(int, struct sshbuf *);
143int mm_answer_gss_accept_ctx(int, struct sshbuf *);
144int mm_answer_gss_userok(int, struct sshbuf *);
145int mm_answer_gss_checkmic(int, struct sshbuf *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000146#endif
Damien Miller25162f22002-09-12 09:47:29 +1000147
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100148#ifdef SSH_AUDIT_EVENTS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000149int mm_answer_audit_event(int, struct sshbuf *);
150int mm_answer_audit_command(int, struct sshbuf *);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100151#endif
152
Damien Miller8f0bf232011-06-20 14:42:23 +1000153static int monitor_read_log(struct monitor *);
154
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000155static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000156
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000157/* local state for key verify */
158static u_char *key_blob = NULL;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000159static size_t key_bloblen = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000160static int key_blobtype = MM_NOKEY;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000161static struct sshauthopt *key_opts = NULL;
Damien Millera10f5612002-09-12 09:49:15 +1000162static char *hostbased_cuser = NULL;
163static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000164static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100165static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000166static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000167static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000168static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000169
170struct mon_table {
171 enum monitor_reqtype type;
172 int flags;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000173 int (*f)(int, struct sshbuf *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000174};
175
176#define MON_ISAUTH 0x0004 /* Required for Authentication */
177#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
178#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100179#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000180
181#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
182
183#define MON_PERMIT 0x1000 /* Request is permitted */
184
185struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000186#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000187 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000188#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000189 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
190 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
191 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000192 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000193 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000194#ifdef USE_PAM
195 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000196 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Millerb38b95f2016-08-29 11:47:07 +1000197 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
198 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
199 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000200 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000201#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100202#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100203 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100204#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000205#ifdef BSD_AUTH
206 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100207 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000208#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000209 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
210 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000211#ifdef GSSAPI
212 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
djm@openbsd.orgb33ad6d2016-09-05 13:57:31 +0000213 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
214 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
215 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000216#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000217 {0, 0, NULL}
218};
219
220struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000221#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000222 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000223#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000224 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
225 {MONITOR_REQ_PTY, 0, mm_answer_pty},
226 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
227 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100228#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100229 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
230 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
231#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000232 {0, 0, NULL}
233};
234
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000235struct mon_table *mon_dispatch;
236
237/* Specifies if a certain message is allowed at the moment */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000238static void
239monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
240{
241 while (ent->f != NULL) {
242 if (ent->type == type) {
243 ent->flags &= ~MON_PERMIT;
244 ent->flags |= permit ? MON_PERMIT : 0;
245 return;
246 }
247 ent++;
248 }
249}
250
251static void
252monitor_permit_authentications(int permit)
253{
254 struct mon_table *ent = mon_dispatch;
255
256 while (ent->f != NULL) {
257 if (ent->flags & MON_AUTH) {
258 ent->flags &= ~MON_PERMIT;
259 ent->flags |= permit ? MON_PERMIT : 0;
260 }
261 ent++;
262 }
263}
264
Darren Tucker3e33cec2003-10-02 16:12:36 +1000265void
266monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000267{
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000268 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000269 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100270 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000271
272 debug3("preauth child monitor started");
273
tb@openbsd.org34843802018-02-05 05:37:46 +0000274 if (pmonitor->m_recvfd >= 0)
275 close(pmonitor->m_recvfd);
276 if (pmonitor->m_log_sendfd >= 0)
277 close(pmonitor->m_log_sendfd);
Damien Miller8f0bf232011-06-20 14:42:23 +1000278 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
279
Darren Tucker3e33cec2003-10-02 16:12:36 +1000280 authctxt = _authctxt;
281 memset(authctxt, 0, sizeof(*authctxt));
djm@openbsd.org7c856852018-03-03 03:15:51 +0000282 ssh->authctxt = authctxt;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000283
Damien Miller120a1ec2018-07-10 19:39:52 +1000284 authctxt->loginmsg = loginmsg;
Darren Tuckerde0de392005-03-31 23:52:04 +1000285
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000286 mon_dispatch = mon_dispatch_proto20;
287 /* Permit requests for moduli and signatures */
288 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
289 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000290
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000291 /* The first few requests do not require asynchronous access */
292 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100293 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100294 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100295 auth_submethod = NULL;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000296 auth2_authctxt_reset_info(authctxt);
297
Darren Tuckerfbba7352006-11-07 23:16:08 +1100298 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100299
300 /* Special handling for multiple required authentications */
301 if (options.num_auth_methods != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100302 if (authenticated &&
303 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000304 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100305 debug3("%s: method %s: partial", __func__,
306 auth_method);
307 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100308 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100309 }
310 }
311
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000312 if (authenticated) {
313 if (!(ent->flags & MON_AUTHDECIDE))
314 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000315 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000316 if (authctxt->pw->pw_uid == 0 &&
djm@openbsd.org7c856852018-03-03 03:15:51 +0000317 !auth_root_allowed(ssh, auth_method))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000318 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000319#ifdef USE_PAM
320 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100321 if (options.use_pam && authenticated) {
Damien Miller120a1ec2018-07-10 19:39:52 +1000322 struct sshbuf *m;
Damien Miller1f499fd2003-08-25 13:08:49 +1000323
Damien Miller120a1ec2018-07-10 19:39:52 +1000324 if ((m = sshbuf_new()) == NULL)
325 fatal("%s: sshbuf_new failed",
326 __func__);
Damien Millera8e06ce2003-11-21 23:48:55 +1100327 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller120a1ec2018-07-10 19:39:52 +1000328 MONITOR_REQ_PAM_ACCOUNT, m);
329 authenticated = mm_answer_pam_account(
330 pmonitor->m_sendfd, m);
331 sshbuf_free(m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000332 }
333#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000334 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100335 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100336 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000337 auth_method, auth_submethod);
djm@openbsd.org94885382015-06-22 23:42:16 +0000338 if (!partial && !authenticated)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000339 authctxt->failures++;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000340 if (authenticated || partial) {
341 auth2_update_session_info(authctxt,
342 auth_method, auth_submethod);
343 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000344 }
345 }
346
347 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000348 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100349 if (strcmp(auth_method, "unknown") == 0)
350 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000351
352 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000353 __func__, authctxt->user);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000354 ssh->authctxt = NULL;
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000355 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000357 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000358
Damien Miller6a1937e2012-12-12 10:44:38 +1100359 /* Drain any buffered messages from the child */
360 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
361 ;
362
tb@openbsd.org34843802018-02-05 05:37:46 +0000363 if (pmonitor->m_recvfd >= 0)
364 close(pmonitor->m_recvfd);
365 if (pmonitor->m_log_sendfd >= 0)
366 close(pmonitor->m_log_sendfd);
Damien Miller8f0bf232011-06-20 14:42:23 +1000367 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000368}
369
Damien Millerbe64d432003-05-14 19:31:12 +1000370static void
371monitor_set_child_handler(pid_t pid)
372{
373 monitor_child_pid = pid;
374}
375
376static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000377monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000378{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000379 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000380}
381
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000382void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000383monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000384{
Damien Miller8f0bf232011-06-20 14:42:23 +1000385 close(pmonitor->m_recvfd);
386 pmonitor->m_recvfd = -1;
387
Damien Millerbe64d432003-05-14 19:31:12 +1000388 monitor_set_child_handler(pmonitor->m_pid);
389 signal(SIGHUP, &monitor_child_handler);
390 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000391 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000392#ifdef SIGXFSZ
393 signal(SIGXFSZ, SIG_IGN);
394#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000395
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000396 mon_dispatch = mon_dispatch_postauth20;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000397
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000398 /* Permit requests for moduli and signatures */
399 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
400 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
401 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
402
djm@openbsd.org7c856852018-03-03 03:15:51 +0000403 if (auth_opts->permit_pty_flag) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000404 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
405 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
406 }
407
408 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000409 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000410}
411
Damien Miller8f0bf232011-06-20 14:42:23 +1000412static int
413monitor_read_log(struct monitor *pmonitor)
414{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000415 struct sshbuf *logmsg;
Damien Miller8f0bf232011-06-20 14:42:23 +1000416 u_int len, level;
417 char *msg;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000418 u_char *p;
419 int r;
Damien Miller8f0bf232011-06-20 14:42:23 +1000420
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000421 if ((logmsg = sshbuf_new()) == NULL)
422 fatal("%s: sshbuf_new", __func__);
Damien Miller8f0bf232011-06-20 14:42:23 +1000423
424 /* Read length */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000425 if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
426 fatal("%s: reserve: %s", __func__, ssh_err(r));
427 if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
Damien Miller8f0bf232011-06-20 14:42:23 +1000428 if (errno == EPIPE) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000429 sshbuf_free(logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000430 debug("%s: child log fd closed", __func__);
431 close(pmonitor->m_log_recvfd);
432 pmonitor->m_log_recvfd = -1;
433 return -1;
434 }
435 fatal("%s: log fd read: %s", __func__, strerror(errno));
436 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000437 if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
438 fatal("%s: get len: %s", __func__, ssh_err(r));
Damien Miller8f0bf232011-06-20 14:42:23 +1000439 if (len <= 4 || len > 8192)
440 fatal("%s: invalid log message length %u", __func__, len);
441
442 /* Read severity, message */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000443 sshbuf_reset(logmsg);
444 if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
445 fatal("%s: reserve: %s", __func__, ssh_err(r));
446 if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
Damien Miller8f0bf232011-06-20 14:42:23 +1000447 fatal("%s: log fd read: %s", __func__, strerror(errno));
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000448 if ((r = sshbuf_get_u32(logmsg, &level)) != 0 ||
449 (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
450 fatal("%s: decode: %s", __func__, ssh_err(r));
Damien Miller8f0bf232011-06-20 14:42:23 +1000451
452 /* Log it */
Damien Miller8f0bf232011-06-20 14:42:23 +1000453 if (log_level_name(level) == NULL)
454 fatal("%s: invalid log level %u (corrupted message?)",
455 __func__, level);
456 do_log2(level, "%s [preauth]", msg);
457
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000458 sshbuf_free(logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000459 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000460
461 return 0;
462}
463
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000464int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000465monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000466 struct mon_table **pent)
467{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000468 struct sshbuf *m;
469 int r, ret;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000470 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000471 struct pollfd pfd[2];
472
473 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100474 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000475 pfd[0].fd = pmonitor->m_sendfd;
476 pfd[0].events = POLLIN;
477 pfd[1].fd = pmonitor->m_log_recvfd;
478 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000479 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
480 if (errno == EINTR || errno == EAGAIN)
481 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000482 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000483 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000484 if (pfd[1].revents) {
485 /*
486 * Drain all log messages before processing next
487 * monitor request.
488 */
489 monitor_read_log(pmonitor);
490 continue;
491 }
492 if (pfd[0].revents)
493 break; /* Continues below */
494 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000495
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000496 if ((m = sshbuf_new()) == NULL)
497 fatal("%s: sshbuf_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000498
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000499 mm_request_receive(pmonitor->m_sendfd, m);
500 if ((r = sshbuf_get_u8(m, &type)) != 0)
501 fatal("%s: decode: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000502
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000503 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000504
505 while (ent->f != NULL) {
506 if (ent->type == type)
507 break;
508 ent++;
509 }
510
511 if (ent->f != NULL) {
512 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000513 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000514 type);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000515 ret = (*ent->f)(pmonitor->m_sendfd, m);
516 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000517
518 /* The child may use this request only once, disable it */
519 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000520 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000521 type);
522 ent->flags &= ~MON_PERMIT;
523 }
524
525 if (pent != NULL)
526 *pent = ent;
527
528 return ret;
529 }
530
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000531 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000532
533 /* NOTREACHED */
534 return (-1);
535}
536
537/* allowed key state */
538static int
539monitor_allowed_key(u_char *blob, u_int bloblen)
540{
541 /* make sure key is allowed */
542 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000543 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000544 return (0);
545 return (1);
546}
547
548static void
549monitor_reset_key_state(void)
550{
551 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000552 free(key_blob);
553 free(hostbased_cuser);
554 free(hostbased_chost);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000555 sshauthopt_free(key_opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000556 key_blob = NULL;
557 key_bloblen = 0;
558 key_blobtype = MM_NOKEY;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000559 key_opts = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000560 hostbased_cuser = NULL;
561 hostbased_chost = NULL;
562}
563
Damien Miller1f0311c2014-05-15 14:24:09 +1000564#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000565int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000566mm_answer_moduli(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000567{
568 DH *dh;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000569 const BIGNUM *dh_p, *dh_g;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000570 int r;
571 u_int min, want, max;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000572
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000573 if ((r = sshbuf_get_u32(m, &min)) != 0 ||
574 (r = sshbuf_get_u32(m, &want)) != 0 ||
575 (r = sshbuf_get_u32(m, &max)) != 0)
576 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577
578 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000579 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000580 /* We need to check here, too, in case the child got corrupted */
581 if (max < min || want < min || max < want)
582 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000583 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000584
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000585 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586
587 dh = choose_dh(min, want, max);
588 if (dh == NULL) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000589 if ((r = sshbuf_put_u8(m, 0)) != 0)
590 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000591 return (0);
592 } else {
593 /* Send first bignum */
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000594 DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000595 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000596 (r = sshbuf_put_bignum2(m, dh_p)) != 0 ||
597 (r = sshbuf_put_bignum2(m, dh_g)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000598 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000599
600 DH_free(dh);
601 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000602 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000603 return (0);
604}
Damien Miller1f0311c2014-05-15 14:24:09 +1000605#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000606
607int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000608mm_answer_sign(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000610 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000611 extern int auth_sock; /* XXX move to state struct? */
612 struct sshkey *key;
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000613 struct sshbuf *sigbuf = NULL;
614 u_char *p = NULL, *signature = NULL;
615 char *alg = NULL;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000616 size_t datlen, siglen, alglen;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000617 int r, is_proof = 0;
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000618 u_int keyid, compat;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000619 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000620
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000621 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000622
djm@openbsd.org141efe42015-01-14 20:05:27 +0000623 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000624 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000625 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0 ||
626 (r = sshbuf_get_u32(m, &compat)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000627 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000628 if (keyid > INT_MAX)
629 fatal("%s: invalid key ID", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000630
Damien Millera63128d2006-03-15 12:08:28 +1100631 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000632 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
633 * SHA384 (48 bytes) and SHA512 (64 bytes).
djm@openbsd.org523463a2015-02-16 22:13:32 +0000634 *
635 * Otherwise, verify the signature request is for a hostkey
636 * proof.
637 *
638 * XXX perform similar check for KEX signature requests too?
639 * it's not trivial, since what is signed is the hash, rather
640 * than the full kex structure...
Damien Millera63128d2006-03-15 12:08:28 +1100641 */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000642 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
643 /*
644 * Construct expected hostkey proof and compare it to what
645 * the client sent us.
646 */
647 if (session_id2_len == 0) /* hostkeys is never first */
648 fatal("%s: bad data length: %zu", __func__, datlen);
649 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
650 fatal("%s: no hostkey for index %d", __func__, keyid);
651 if ((sigbuf = sshbuf_new()) == NULL)
652 fatal("%s: sshbuf_new", __func__);
djm@openbsd.org44732de2015-02-20 22:17:21 +0000653 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
654 (r = sshbuf_put_string(sigbuf, session_id2,
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000655 session_id2_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000656 (r = sshkey_puts(key, sigbuf)) != 0)
657 fatal("%s: couldn't prepare private key "
658 "proof buffer: %s", __func__, ssh_err(r));
659 if (datlen != sshbuf_len(sigbuf) ||
660 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
661 fatal("%s: bad data length: %zu, hostkey proof len %zu",
662 __func__, datlen, sshbuf_len(sigbuf));
663 sshbuf_free(sigbuf);
664 is_proof = 1;
665 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000666
Ben Lindstromf67e0772002-06-06 20:58:19 +0000667 /* save session id, it will be passed on the first call */
668 if (session_id2_len == 0) {
669 session_id2_len = datlen;
670 session_id2 = xmalloc(session_id2_len);
671 memcpy(session_id2, p, session_id2_len);
672 }
673
Damien Miller85b45e02013-07-20 13:21:52 +1000674 if ((key = get_hostkey_by_index(keyid)) != NULL) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000675 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000676 compat)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000677 fatal("%s: sshkey_sign failed: %s",
678 __func__, ssh_err(r));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000679 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000680 auth_sock > 0) {
681 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000682 p, datlen, alg, compat)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000683 fatal("%s: ssh_agent_sign failed: %s",
684 __func__, ssh_err(r));
685 }
Damien Miller85b45e02013-07-20 13:21:52 +1000686 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000687 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000688
djm@openbsd.org523463a2015-02-16 22:13:32 +0000689 debug3("%s: %s signature %p(%zu)", __func__,
690 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000691
djm@openbsd.org141efe42015-01-14 20:05:27 +0000692 sshbuf_reset(m);
693 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
694 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000695
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000696 free(alg);
Darren Tuckera627d422013-06-02 07:31:17 +1000697 free(p);
698 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000699
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000700 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000701
702 /* Turn on permissions for getpwnam */
703 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
704
705 return (0);
706}
707
708/* Retrieves the password entry and also checks if the user is permitted */
709
710int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000711mm_answer_pwnamallow(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000712{
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000713 struct ssh *ssh = active_state; /* XXX */
Darren Tuckerb09b6772004-06-22 15:06:46 +1000714 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000715 struct passwd *pwent;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000716 int r, allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000717 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000718
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000719 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000720
721 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000722 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000723
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000724 if ((r = sshbuf_get_cstring(m, &username, NULL)) != 0)
725 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000726
Darren Tuckerb09b6772004-06-22 15:06:46 +1000727 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728
Darren Tuckerb09b6772004-06-22 15:06:46 +1000729 authctxt->user = xstrdup(username);
730 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000731 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000732
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000733 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000734
735 if (pwent == NULL) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000736 if ((r = sshbuf_put_u8(m, 0)) != 0)
737 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf96d1832003-11-18 22:01:48 +1100738 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000739 goto out;
740 }
741
742 allowed = 1;
743 authctxt->pw = pwent;
744 authctxt->valid = 1;
745
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000746 /* XXX don't sent pwent to unpriv; send fake class/dir/shell too */
747 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
748 (r = sshbuf_put_string(m, pwent, sizeof(*pwent))) != 0 ||
749 (r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
750 (r = sshbuf_put_cstring(m, "*")) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000751#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000752 (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000753#endif
754#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000755 (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
Kevin Steves7e147602002-03-22 18:07:17 +0000756#endif
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000757 (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
758 (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
759 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100760
761 out:
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000762 ssh_packet_set_log_preamble(ssh, "%suser %s",
763 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000764 if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
765 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerf2e407e2011-05-20 19:04:14 +1000766
767#define M_CP_STROPT(x) do { \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000768 if (options.x != NULL) { \
769 if ((r = sshbuf_put_cstring(m, options.x)) != 0) \
770 fatal("%s: buffer error: %s", \
771 __func__, ssh_err(r)); \
772 } \
Damien Millerf2e407e2011-05-20 19:04:14 +1000773 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000774#define M_CP_STRARRAYOPT(x, nx) do { \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000775 for (i = 0; i < options.nx; i++) { \
776 if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
777 fatal("%s: buffer error: %s", \
778 __func__, ssh_err(r)); \
779 } \
Damien Millerd8478b62011-05-29 21:39:36 +1000780 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000781 /* See comment in servconf.h */
782 COPY_MATCH_STRING_OPTS();
783#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000784#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100785
786 /* Create valid auth method lists */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000787 if (auth2_setup_methods_lists(authctxt) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100788 /*
789 * The monitor will continue long enough to let the child
790 * run to it's packet_disconnect(), but it must not allow any
791 * authentication to succeed.
792 */
793 debug("%s: no valid authentication method lists", __func__);
794 }
795
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000796 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000797 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000798
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000799 /* Allow service/style information on the auth context */
800 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
801 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
802
Damien Millera33501b2002-05-08 12:24:42 +1000803#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000804 if (options.use_pam)
805 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000806#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000807
808 return (0);
809}
810
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000811int mm_answer_auth2_read_banner(int sock, struct sshbuf *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000812{
813 char *banner;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000814 int r;
Damien Miller5ad9fd92002-05-13 11:07:41 +1000815
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000816 sshbuf_reset(m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000817 banner = auth2_read_banner();
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000818 if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
819 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000820 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000821 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000822
823 return (0);
824}
825
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000826int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000827mm_answer_authserv(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000828{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000829 int r;
830
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000831 monitor_permit_authentications(1);
832
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000833 if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
834 (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
835 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000836 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000837 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000838
839 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000840 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000841 authctxt->style = NULL;
842 }
843
844 return (0);
845}
846
847int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000848mm_answer_authpassword(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000849{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000850 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000851 static int call_count;
852 char *passwd;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000853 int r, authenticated;
854 size_t plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000855
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000856 if (!options.password_authentication)
857 fatal("%s: password authentication not enabled", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000858 if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
859 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000860 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000861 authenticated = options.password_authentication &&
djm@openbsd.org7c856852018-03-03 03:15:51 +0000862 auth_password(ssh, passwd);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000863 explicit_bzero(passwd, plen);
Darren Tuckera627d422013-06-02 07:31:17 +1000864 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000865
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000866 sshbuf_reset(m);
867 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
868 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker01558b72016-07-18 09:33:25 +1000869#ifdef USE_PAM
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000870 if ((r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0)
871 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker01558b72016-07-18 09:33:25 +1000872#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000873
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000874 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000875 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000876
877 call_count++;
878 if (plen == 0 && call_count == 1)
879 auth_method = "none";
880 else
881 auth_method = "password";
882
883 /* Causes monitor loop to terminate if authenticated */
884 return (authenticated);
885}
886
887#ifdef BSD_AUTH
888int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000889mm_answer_bsdauthquery(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000890{
891 char *name, *infotxt;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000892 u_int numprompts, *echo_on, success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000893 char **prompts;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000894 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000895
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000896 if (!options.kbd_interactive_authentication)
897 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb7df3af2003-02-24 11:55:46 +1100898 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
899 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000900
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000901 sshbuf_reset(m);
902 if ((r = sshbuf_put_u32(m, success)) != 0)
903 fatal("%s: buffer error: %s", __func__, ssh_err(r));
904 if (success) {
905 if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
906 fatal("%s: buffer error: %s", __func__, ssh_err(r));
907 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000908
Damien Millerb7df3af2003-02-24 11:55:46 +1100909 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000910 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000911
Damien Millerb7df3af2003-02-24 11:55:46 +1100912 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000913 free(name);
914 free(infotxt);
915 free(prompts);
916 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000917 }
918
919 return (0);
920}
921
922int
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000923mm_answer_bsdauthrespond(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000924{
925 char *response;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000926 int r, authok;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000927
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000928 if (!options.kbd_interactive_authentication)
929 fatal("%s: kbd-int authentication not enabled", __func__);
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +0000930 if (authctxt->as == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000931 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000933 if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
934 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000935 authok = options.challenge_response_authentication &&
936 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000937 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000938 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000939 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000940
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000941 sshbuf_reset(m);
942 if ((r = sshbuf_put_u32(m, authok)) != 0)
943 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000944
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000945 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000946 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000947
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000948 auth_method = "keyboard-interactive";
949 auth_submethod = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000950
951 return (authok != 0);
952}
953#endif
954
Damien Miller79418552002-04-23 20:28:48 +1000955#ifdef USE_PAM
956int
Damien Miller120a1ec2018-07-10 19:39:52 +1000957mm_answer_pam_start(int sock, struct sshbuf *m)
Damien Miller79418552002-04-23 20:28:48 +1000958{
Damien Miller4e448a32003-05-14 15:11:48 +1000959 if (!options.use_pam)
960 fatal("UsePAM not set, but ended up in %s anyway", __func__);
961
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100962 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000963
Damien Miller1f499fd2003-08-25 13:08:49 +1000964 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
Damien Miller775f8a22016-08-31 10:48:07 +1000965 if (options.kbd_interactive_authentication)
966 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller1f499fd2003-08-25 13:08:49 +1000967
Damien Miller79418552002-04-23 20:28:48 +1000968 return (0);
969}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000970
Damien Miller1f499fd2003-08-25 13:08:49 +1000971int
Damien Miller120a1ec2018-07-10 19:39:52 +1000972mm_answer_pam_account(int sock, struct sshbuf *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000973{
974 u_int ret;
Damien Miller120a1ec2018-07-10 19:39:52 +1000975 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +1100976
Damien Miller1f499fd2003-08-25 13:08:49 +1000977 if (!options.use_pam)
Damien Miller775f8a22016-08-31 10:48:07 +1000978 fatal("%s: PAM not enabled", __func__);
Damien Miller1f499fd2003-08-25 13:08:49 +1000979
980 ret = do_pam_account();
981
Damien Miller120a1ec2018-07-10 19:39:52 +1000982 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
983 (r = sshbuf_put_stringb(m, loginmsg)) != 0)
984 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller1f499fd2003-08-25 13:08:49 +1000985
Darren Tucker7eda5922004-06-22 13:22:50 +1000986 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000987
988 return (ret);
989}
990
Damien Miller4f9f42a2003-05-10 19:28:02 +1000991static void *sshpam_ctxt, *sshpam_authok;
992extern KbdintDevice sshpam_device;
993
994int
Damien Miller120a1ec2018-07-10 19:39:52 +1000995mm_answer_pam_init_ctx(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000996{
Damien Miller120a1ec2018-07-10 19:39:52 +1000997 u_int ok = 0;
998 int r;
999
Damien Miller4f9f42a2003-05-10 19:28:02 +10001000 debug3("%s", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001001 if (!options.kbd_interactive_authentication)
1002 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001003 if (sshpam_ctxt != NULL)
1004 fatal("%s: already called", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001005 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1006 sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001007 sshbuf_reset(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001008 if (sshpam_ctxt != NULL) {
1009 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
Damien Millerb38b95f2016-08-29 11:47:07 +10001010 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
Damien Miller120a1ec2018-07-10 19:39:52 +10001011 ok = 1;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001012 }
Damien Miller120a1ec2018-07-10 19:39:52 +10001013 if ((r = sshbuf_put_u32(m, ok)) != 0)
1014 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker7eda5922004-06-22 13:22:50 +10001015 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001016 return (0);
1017}
1018
1019int
Damien Miller120a1ec2018-07-10 19:39:52 +10001020mm_answer_pam_query(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001021{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001022 char *name = NULL, *info = NULL, **prompts = NULL;
1023 u_int i, num = 0, *echo_on = 0;
Damien Miller120a1ec2018-07-10 19:39:52 +10001024 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001025
1026 debug3("%s", __func__);
1027 sshpam_authok = NULL;
Damien Millerb38b95f2016-08-29 11:47:07 +10001028 if (sshpam_ctxt == NULL)
1029 fatal("%s: no context", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001030 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1031 &num, &prompts, &echo_on);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001032 if (ret == 0 && num == 0)
1033 sshpam_authok = sshpam_ctxt;
1034 if (num > 1 || name == NULL || info == NULL)
Damien Millerb38b95f2016-08-29 11:47:07 +10001035 fatal("sshpam_device.query failed");
1036 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
Damien Miller120a1ec2018-07-10 19:39:52 +10001037 sshbuf_reset(m);
1038 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1039 (r = sshbuf_put_cstring(m, name)) != 0 ||
1040 (r = sshbuf_put_cstring(m, info)) != 0 ||
1041 (r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0 ||
1042 (r = sshbuf_put_u32(m, num)) != 0)
1043 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +10001044 free(name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001045 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001046 for (i = 0; i < num; ++i) {
Damien Miller120a1ec2018-07-10 19:39:52 +10001047 if ((r = sshbuf_put_cstring(m, prompts[i])) != 0 ||
1048 (r = sshbuf_put_u32(m, echo_on[i])) != 0)
1049 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckerf60845f2013-06-02 08:07:31 +10001050 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001051 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001052 free(prompts);
1053 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001054 auth_method = "keyboard-interactive";
1055 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001056 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001057 return (0);
1058}
1059
1060int
Damien Miller120a1ec2018-07-10 19:39:52 +10001061mm_answer_pam_respond(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001062{
1063 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001064 u_int i, num;
Damien Miller120a1ec2018-07-10 19:39:52 +10001065 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001066
1067 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001068 if (sshpam_ctxt == NULL)
1069 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001070 sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001071 if ((r = sshbuf_get_u32(m, &num)) != 0)
1072 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001073 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001074 resp = xcalloc(num, sizeof(char *));
Damien Miller120a1ec2018-07-10 19:39:52 +10001075 for (i = 0; i < num; ++i) {
1076 if ((r = sshbuf_get_cstring(m, &(resp[i]), NULL)) != 0)
1077 fatal("%s: buffer error: %s",
1078 __func__, ssh_err(r));
1079 }
Damien Miller4f9f42a2003-05-10 19:28:02 +10001080 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1081 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001082 free(resp[i]);
1083 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001084 } else {
1085 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1086 }
Damien Miller120a1ec2018-07-10 19:39:52 +10001087 sshbuf_reset(m);
1088 if ((r = sshbuf_put_u32(m, ret)) != 0)
1089 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker7eda5922004-06-22 13:22:50 +10001090 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001091 auth_method = "keyboard-interactive";
1092 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001093 if (ret == 0)
1094 sshpam_authok = sshpam_ctxt;
1095 return (0);
1096}
1097
1098int
Damien Miller120a1ec2018-07-10 19:39:52 +10001099mm_answer_pam_free_ctx(int sock, struct sshbuf *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001100{
Damien Miller5e75f512015-08-11 13:34:12 +10001101 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001102
1103 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001104 if (sshpam_ctxt == NULL)
1105 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001106 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001107 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +10001108 sshbuf_reset(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001109 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Millerb38b95f2016-08-29 11:47:07 +10001110 /* Allow another attempt */
1111 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller15b05cf2012-12-03 09:53:20 +11001112 auth_method = "keyboard-interactive";
1113 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001114 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001115}
Damien Miller79418552002-04-23 20:28:48 +10001116#endif
1117
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001118int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001119mm_answer_keyallowed(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001120{
djm@openbsd.org7c856852018-03-03 03:15:51 +00001121 struct ssh *ssh = active_state; /* XXX */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001122 struct sshkey *key = NULL;
Damien Millera10f5612002-09-12 09:49:15 +10001123 char *cuser, *chost;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001124 u_int pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001125 enum mm_keytype type = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001126 int r, allowed = 0;
1127 struct sshauthopt *opts = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001128
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001129 debug3("%s entering", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001130 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1131 (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
1132 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
1133 (r = sshkey_froms(m, &key)) != 0 ||
1134 (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
1135 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001136
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001137 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001138
Damien Miller3e3b5142003-11-17 21:13:40 +11001139 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001140 /* These should not make it past the privsep child */
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001141 if (sshkey_type_plain(key->type) == KEY_RSA &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001142 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1143 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1144
Darren Tucker47eede72005-03-14 23:08:12 +11001145 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001146 case MM_USERKEY:
Damien Miller7a8f5b32006-03-31 23:14:23 +11001147 auth_method = "publickey";
djm@openbsd.org7c856852018-03-03 03:15:51 +00001148 if (!options.pubkey_authentication)
1149 break;
1150 if (auth2_key_already_used(authctxt, key))
1151 break;
1152 if (match_pattern_list(sshkey_ssh_name(key),
1153 options.pubkey_key_types, 0) != 1)
1154 break;
1155 allowed = user_key_allowed(ssh, authctxt->pw, key,
1156 pubkey_auth_attempt, &opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001157 break;
1158 case MM_HOSTKEY:
djm@openbsd.org7c856852018-03-03 03:15:51 +00001159 auth_method = "hostbased";
1160 if (!options.hostbased_authentication)
1161 break;
1162 if (auth2_key_already_used(authctxt, key))
1163 break;
1164 if (match_pattern_list(sshkey_ssh_name(key),
1165 options.hostbased_key_types, 0) != 1)
1166 break;
1167 allowed = hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001168 cuser, chost, key);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001169 auth2_record_info(authctxt,
Damien Miller20bdcd72013-07-18 16:10:09 +10001170 "client user \"%.100s\", client host \"%.100s\"",
1171 cuser, chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001172 break;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001173 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001174 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001175 break;
1176 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001177 }
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001178
djm@openbsd.org7c856852018-03-03 03:15:51 +00001179 debug3("%s: %s authentication%s: %s key is %s", __func__,
1180 auth_method, pubkey_auth_attempt ? "" : " test",
1181 (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
1182 allowed ? "allowed" : "not allowed");
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001183
djm@openbsd.org8f574952017-06-24 06:34:38 +00001184 auth2_record_key(authctxt, 0, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001185
1186 /* clear temporarily storage (used by verify) */
1187 monitor_reset_key_state();
1188
1189 if (allowed) {
1190 /* Save temporarily for comparison in verify */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001191 if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
1192 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001193 key_blobtype = type;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001194 key_opts = opts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001195 hostbased_cuser = cuser;
1196 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001197 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001198 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001199 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001200 free(cuser);
1201 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001203 sshkey_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001204
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001205 sshbuf_reset(m);
1206 if ((r = sshbuf_put_u32(m, allowed)) != 0)
1207 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org7c856852018-03-03 03:15:51 +00001208 if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
1209 fatal("%s: sshauthopt_serialise: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001210 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001211
djm@openbsd.org7c856852018-03-03 03:15:51 +00001212 if (!allowed)
1213 sshauthopt_free(opts);
1214
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001215 return (0);
1216}
1217
1218static int
1219monitor_valid_userblob(u_char *data, u_int datalen)
1220{
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001221 struct sshbuf *b;
1222 const u_char *p;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001223 char *userstyle, *cp;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001224 size_t len;
1225 u_char type;
1226 int r, fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001227
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001228 if ((b = sshbuf_new()) == NULL)
1229 fatal("%s: sshbuf_new", __func__);
1230 if ((r = sshbuf_put(b, data, datalen)) != 0)
1231 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001232
1233 if (datafellows & SSH_OLD_SESSIONID) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001234 p = sshbuf_ptr(b);
1235 len = sshbuf_len(b);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001236 if ((session_id2 == NULL) ||
1237 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001238 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001239 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001240 if ((r = sshbuf_consume(b, session_id2_len)) != 0)
1241 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001242 } else {
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001243 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1244 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstromf67e0772002-06-06 20:58:19 +00001245 if ((session_id2 == NULL) ||
1246 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001247 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001248 fail++;
1249 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001250 if ((r = sshbuf_get_u8(b, &type)) != 0)
1251 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1252 if (type != SSH2_MSG_USERAUTH_REQUEST)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001253 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001254 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1255 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4ce189d2013-04-23 15:17:52 +10001256 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1257 authctxt->style ? ":" : "",
1258 authctxt->style ? authctxt->style : "");
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001259 if (strcmp(userstyle, cp) != 0) {
1260 logit("wrong user name passed to monitor: "
1261 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001262 fail++;
1263 }
Darren Tuckera627d422013-06-02 07:31:17 +10001264 free(userstyle);
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001265 free(cp);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001266 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1267 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1268 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001269 if (strcmp("publickey", cp) != 0)
1270 fail++;
1271 free(cp);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001272 if ((r = sshbuf_get_u8(b, &type)) != 0)
1273 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1274 if (type == 0)
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001275 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001276 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1277 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1278 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1279 if (sshbuf_len(b) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001280 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001281 sshbuf_free(b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001282 return (fail == 0);
1283}
1284
1285static int
Damien Millera10f5612002-09-12 09:49:15 +10001286monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1287 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001288{
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001289 struct sshbuf *b;
1290 const u_char *p;
1291 char *cp, *userstyle;
1292 size_t len;
1293 int r, fail = 0;
1294 u_char type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001296 if ((b = sshbuf_new()) == NULL)
1297 fatal("%s: sshbuf_new", __func__);
1298 if ((r = sshbuf_put(b, data, datalen)) != 0 ||
1299 (r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1300 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001301
Ben Lindstromf67e0772002-06-06 20:58:19 +00001302 if ((session_id2 == NULL) ||
1303 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001304 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001305 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001306
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001307 if ((r = sshbuf_get_u8(b, &type)) != 0)
1308 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1309 if (type != SSH2_MSG_USERAUTH_REQUEST)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001310 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001311 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1312 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4ce189d2013-04-23 15:17:52 +10001313 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1314 authctxt->style ? ":" : "",
1315 authctxt->style ? authctxt->style : "");
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001316 if (strcmp(userstyle, cp) != 0) {
1317 logit("wrong user name passed to monitor: "
1318 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001319 fail++;
1320 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001321 free(userstyle);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001322 free(cp);
1323 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1324 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1325 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1326 if (strcmp(cp, "hostbased") != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001327 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001328 free(cp);
1329 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1330 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1331 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001332
1333 /* verify client host, strip trailing dot if necessary */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001334 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1335 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1336 if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
1337 cp[len - 1] = '\0';
1338 if (strcmp(cp, chost) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001339 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001340 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001341
1342 /* verify client user */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001343 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1344 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1345 if (strcmp(cp, cuser) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001346 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001347 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001348
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001349 if (sshbuf_len(b) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001350 fail++;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001351 sshbuf_free(b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001352 return (fail == 0);
1353}
1354
1355int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001356mm_answer_keyverify(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001357{
djm@openbsd.org7c856852018-03-03 03:15:51 +00001358 struct ssh *ssh = active_state; /* XXX */
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001359 struct sshkey *key;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001360 u_char *signature, *data, *blob;
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001361 char *sigalg;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001362 size_t signaturelen, datalen, bloblen;
1363 int r, ret, valid_data = 0, encoded_ret;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001364
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001365 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1366 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001367 (r = sshbuf_get_string(m, &data, &datalen)) != 0 ||
1368 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001369 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001370
1371 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001372 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001373 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001374
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001375 /* Empty signature algorithm means NULL. */
1376 if (*sigalg == '\0') {
1377 free(sigalg);
1378 sigalg = NULL;
1379 }
1380
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001381 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1382 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1383 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001384
1385 switch (key_blobtype) {
1386 case MM_USERKEY:
1387 valid_data = monitor_valid_userblob(data, datalen);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001388 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001389 break;
1390 case MM_HOSTKEY:
1391 valid_data = monitor_valid_hostbasedblob(data, datalen,
1392 hostbased_cuser, hostbased_chost);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001393 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001394 break;
1395 default:
1396 valid_data = 0;
1397 break;
1398 }
1399 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001400 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001401
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001402 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +00001403 sigalg, active_state->compat);
djm@openbsd.org8f574952017-06-24 06:34:38 +00001404 debug3("%s: %s %p signature %s", __func__, auth_method, key,
1405 (ret == 0) ? "verified" : "unverified");
1406 auth2_record_key(authctxt, ret == 0, key);
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001407
Darren Tuckera627d422013-06-02 07:31:17 +10001408 free(blob);
1409 free(signature);
1410 free(data);
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001411 free(sigalg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001412
djm@openbsd.org7c856852018-03-03 03:15:51 +00001413 if (key_blobtype == MM_USERKEY)
1414 auth_activate_options(ssh, key_opts);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001415 monitor_reset_key_state();
1416
djm@openbsd.org8f574952017-06-24 06:34:38 +00001417 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001418 sshbuf_reset(m);
1419
1420 /* encode ret != 0 as positive integer, since we're sending u32 */
1421 encoded_ret = (ret != 0);
1422 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1423 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001424 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001425
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001426 return ret == 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001427}
1428
1429static void
1430mm_record_login(Session *s, struct passwd *pw)
1431{
djm@openbsd.org95767262016-03-07 19:02:43 +00001432 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001433 socklen_t fromlen;
1434 struct sockaddr_storage from;
1435
1436 /*
1437 * Get IP address of client. If the connection is not a socket, let
1438 * the address be 0.0.0.0.
1439 */
1440 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001441 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001442 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001443 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001444 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001445 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001446 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001447 }
1448 }
1449 /* Record that there was a login on that tty from the remote host. */
1450 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +00001451 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001452 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001453}
1454
1455static void
1456mm_session_close(Session *s)
1457{
Damien Miller04bd8b02003-05-25 14:38:33 +10001458 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001459 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001460 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001461 session_pty_cleanup2(s);
1462 }
Damien Miller7207f642008-05-19 15:34:50 +10001463 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001464}
1465
1466int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001467mm_answer_pty(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001468{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001469 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001470 Session *s;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001471 int r, res, fd0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001472
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001473 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001474
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001475 sshbuf_reset(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001476 s = session_new();
1477 if (s == NULL)
1478 goto error;
1479 s->authctxt = authctxt;
1480 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001481 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001482 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1483 if (res == 0)
1484 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001485 pty_setowner(authctxt->pw, s->tty);
1486
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001487 if ((r = sshbuf_put_u32(m, 1)) != 0 ||
1488 (r = sshbuf_put_cstring(m, s->tty)) != 0)
1489 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001490
1491 /* We need to trick ttyslot */
1492 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001493 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001494
1495 mm_record_login(s, authctxt->pw);
1496
1497 /* Now we can close the file descriptor again */
1498 close(0);
1499
Darren Tucker09991742004-07-17 17:05:14 +10001500 /* send messages generated by record_login */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001501 if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
1502 fatal("%s: put login message: %s", __func__, ssh_err(r));
1503 sshbuf_reset(loginmsg);
Darren Tucker09991742004-07-17 17:05:14 +10001504
1505 mm_request_send(sock, MONITOR_ANS_PTY, m);
1506
Damien Miller54fd7cf2007-09-17 12:04:08 +10001507 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1508 mm_send_fd(sock, s->ttyfd) == -1)
1509 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001510
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001511 /* make sure nothing uses fd 0 */
1512 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001513 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001514 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001515 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001516
1517 /* slave is not needed */
1518 close(s->ttyfd);
1519 s->ttyfd = s->ptyfd;
1520 /* no need to dup() because nobody closes ptyfd */
1521 s->ptymaster = s->ptyfd;
1522
Damien Miller9ab00b42006-08-05 12:40:11 +10001523 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001524
1525 return (0);
1526
1527 error:
1528 if (s != NULL)
1529 mm_session_close(s);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001530 if ((r = sshbuf_put_u32(m, 0)) != 0)
1531 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001532 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001533 return (0);
1534}
1535
1536int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001537mm_answer_pty_cleanup(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001538{
1539 Session *s;
1540 char *tty;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001541 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001542
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001543 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001544
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001545 if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
1546 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001547 if ((s = session_by_tty(tty)) != NULL)
1548 mm_session_close(s);
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001549 sshbuf_reset(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001550 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001551 return (0);
1552}
1553
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001554int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001555mm_answer_term(int sock, struct sshbuf *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001556{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001557 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001558 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001559 int res, status;
1560
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001561 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001562
1563 /* The child is terminating */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001564 session_destroy_all(ssh, &mm_session_close);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001565
Darren Tucker52358d62008-03-11 22:58:25 +11001566#ifdef USE_PAM
1567 if (options.use_pam)
1568 sshpam_cleanup();
1569#endif
1570
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001571 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001572 if (errno != EINTR)
1573 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001574
1575 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1576
1577 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001578 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001579}
1580
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001581#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001582/* Report that an audit event occurred */
1583int
Damien Miller120a1ec2018-07-10 19:39:52 +10001584mm_answer_audit_event(int socket, struct sshbuf *m)
Darren Tucker269a1ea2005-02-03 00:20:53 +11001585{
Damien Miller120a1ec2018-07-10 19:39:52 +10001586 u_int n;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001587 ssh_audit_event_t event;
Damien Miller120a1ec2018-07-10 19:39:52 +10001588 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001589
1590 debug3("%s entering", __func__);
1591
Damien Miller120a1ec2018-07-10 19:39:52 +10001592 if ((r = sshbuf_get_u32(m, &n)) != 0)
1593 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1594 event = (ssh_audit_event_t)n;
1595 switch (event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001596 case SSH_AUTH_FAIL_PUBKEY:
1597 case SSH_AUTH_FAIL_HOSTBASED:
1598 case SSH_AUTH_FAIL_GSSAPI:
1599 case SSH_LOGIN_EXCEED_MAXTRIES:
1600 case SSH_LOGIN_ROOT_DENIED:
1601 case SSH_CONNECTION_CLOSE:
1602 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001603 audit_event(event);
1604 break;
1605 default:
1606 fatal("Audit event type %d not permitted", event);
1607 }
1608
1609 return (0);
1610}
1611
1612int
Damien Miller120a1ec2018-07-10 19:39:52 +10001613mm_answer_audit_command(int socket, struct sshbuf *m)
Darren Tucker269a1ea2005-02-03 00:20:53 +11001614{
Darren Tucker269a1ea2005-02-03 00:20:53 +11001615 char *cmd;
Damien Miller120a1ec2018-07-10 19:39:52 +10001616 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +11001617
1618 debug3("%s entering", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +10001619 if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
1620 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker269a1ea2005-02-03 00:20:53 +11001621 /* sanity check command, if so how? */
1622 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001623 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001624 return (0);
1625}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001626#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001627
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001628void
markus@openbsd.org1e0cdf82017-05-31 08:09:45 +00001629monitor_clear_keystate(struct monitor *pmonitor)
1630{
1631 struct ssh *ssh = active_state; /* XXX */
1632
1633 ssh_clear_newkeys(ssh, MODE_IN);
1634 ssh_clear_newkeys(ssh, MODE_OUT);
1635 sshbuf_free(child_state);
1636 child_state = NULL;
1637}
1638
1639void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001640monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001641{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001642 struct ssh *ssh = active_state; /* XXX */
1643 struct kex *kex;
1644 int r;
1645
1646 debug3("%s: packet_set_state", __func__);
1647 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1648 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1649 sshbuf_free(child_state);
1650 child_state = NULL;
1651
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +00001652 if ((kex = ssh->kex) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001653 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001654#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001655 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1656 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +00001657 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1658 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1659 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001660 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1661 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001662# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001663 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001664# endif
Damien Miller773dda22015-01-30 23:10:17 +11001665#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001666 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1667 kex->load_host_public_key=&get_hostkey_public_by_type;
1668 kex->load_host_private_key=&get_hostkey_private_by_type;
1669 kex->host_key_index=&get_hostkey_index;
1670 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001671 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001672}
1673
1674/* This function requries careful sanity checking */
1675
1676void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001677mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001678{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001679 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001680
markus@openbsd.org091c3022015-01-19 19:52:16 +00001681 if ((child_state = sshbuf_new()) == NULL)
1682 fatal("%s: sshbuf_new failed", __func__);
1683 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1684 child_state);
1685 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001686}
1687
1688
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001689/* XXX */
1690
1691#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001692 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001693 fatal("fcntl(%d, F_SETFD)", x); \
1694} while (0)
1695
1696static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001697monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001698{
Damien Miller8f0bf232011-06-20 14:42:23 +10001699 int pair[2];
markus@openbsd.org84008602017-05-31 10:04:29 +00001700#ifdef SO_ZEROIZE
1701 int on = 1;
1702#endif
Damien Miller8f0bf232011-06-20 14:42:23 +10001703
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001704 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001705 fatal("%s: socketpair: %s", __func__, strerror(errno));
markus@openbsd.org84008602017-05-31 10:04:29 +00001706#ifdef SO_ZEROIZE
1707 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1708 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1709 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1710 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1711#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001712 FD_CLOSEONEXEC(pair[0]);
1713 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001714 mon->m_recvfd = pair[0];
1715 mon->m_sendfd = pair[1];
1716
1717 if (do_logfds) {
1718 if (pipe(pair) == -1)
1719 fatal("%s: pipe: %s", __func__, strerror(errno));
1720 FD_CLOSEONEXEC(pair[0]);
1721 FD_CLOSEONEXEC(pair[1]);
1722 mon->m_log_recvfd = pair[0];
1723 mon->m_log_sendfd = pair[1];
1724 } else
1725 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001726}
1727
1728#define MM_MEMSIZE 65536
1729
1730struct monitor *
1731monitor_init(void)
1732{
1733 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001734
Damien Miller07d86be2006-03-26 14:19:21 +11001735 mon = xcalloc(1, sizeof(*mon));
Damien Miller8f0bf232011-06-20 14:42:23 +10001736 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001737
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001738 return mon;
1739}
1740
1741void
1742monitor_reinit(struct monitor *mon)
1743{
Damien Miller8f0bf232011-06-20 14:42:23 +10001744 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001745}
Darren Tucker0efd1552003-08-26 11:49:55 +10001746
1747#ifdef GSSAPI
1748int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001749mm_answer_gss_setup_ctx(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001750{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001751 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001752 OM_uint32 major;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001753 size_t len;
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001754 u_char *p;
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001755 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +10001756
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001757 if (!options.gss_authentication)
1758 fatal("%s: GSSAPI authentication not enabled", __func__);
1759
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001760 if ((r = sshbuf_get_string(m, &p, &len)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001761 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001762 goid.elements = p;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001763 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001764
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001765 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001766
Darren Tuckera627d422013-06-02 07:31:17 +10001767 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001768
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001769 sshbuf_reset(m);
1770 if ((r = sshbuf_put_u32(m, major)) != 0)
1771 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001772
Damien Miller6fd6def2005-11-05 15:07:05 +11001773 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001774
1775 /* Now we have a context, enable the step */
1776 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1777
1778 return (0);
1779}
1780
1781int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001782mm_answer_gss_accept_ctx(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001783{
1784 gss_buffer_desc in;
1785 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001786 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001787 OM_uint32 flags = 0; /* GSI needs this */
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001788 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +10001789
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001790 if (!options.gss_authentication)
1791 fatal("%s: GSSAPI authentication not enabled", __func__);
1792
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001793 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001794 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001795 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001796 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001797
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001798 sshbuf_reset(m);
1799 if ((r = sshbuf_put_u32(m, major)) != 0 ||
1800 (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
1801 (r = sshbuf_put_u32(m, flags)) != 0)
1802 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001803 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001804
1805 gss_release_buffer(&minor, &out);
1806
Damien Miller6fd6def2005-11-05 15:07:05 +11001807 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001808 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1809 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001810 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001811 }
1812 return (0);
1813}
1814
1815int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001816mm_answer_gss_checkmic(int sock, struct sshbuf *m)
Damien Miller0425d402003-11-17 22:18:21 +11001817{
1818 gss_buffer_desc gssbuf, mic;
1819 OM_uint32 ret;
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001820 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +11001821
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001822 if (!options.gss_authentication)
1823 fatal("%s: GSSAPI authentication not enabled", __func__);
1824
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001825 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
1826 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001827 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller787b2ec2003-11-21 23:56:47 +11001828
Damien Miller0425d402003-11-17 22:18:21 +11001829 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001830
Darren Tuckera627d422013-06-02 07:31:17 +10001831 free(gssbuf.value);
1832 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001833
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001834 sshbuf_reset(m);
1835 if ((r = sshbuf_put_u32(m, ret)) != 0)
1836 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller787b2ec2003-11-21 23:56:47 +11001837
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001838 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001839
Damien Miller0425d402003-11-17 22:18:21 +11001840 if (!GSS_ERROR(ret))
1841 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001842
Damien Miller0425d402003-11-17 22:18:21 +11001843 return (0);
1844}
1845
1846int
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001847mm_answer_gss_userok(int sock, struct sshbuf *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001848{
djm@openbsd.org0f3958c2018-07-10 09:13:30 +00001849 int r, authenticated;
djm@openbsd.org8f574952017-06-24 06:34:38 +00001850 const char *displayname;
Darren Tucker0efd1552003-08-26 11:49:55 +10001851
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001852 if (!options.gss_authentication)
1853 fatal("%s: GSSAPI authentication not enabled", __func__);
1854
Darren Tucker0efd1552003-08-26 11:49:55 +10001855 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1856
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001857 sshbuf_reset(m);
1858 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1859 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001860
1861 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001862 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001863
Damien Miller6fd6def2005-11-05 15:07:05 +11001864 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001865
djm@openbsd.org8f574952017-06-24 06:34:38 +00001866 if ((displayname = ssh_gssapi_displayname()) != NULL)
1867 auth2_record_info(authctxt, "%s", displayname);
1868
Darren Tucker0efd1552003-08-26 11:49:55 +10001869 /* Monitor loop will terminate if authenticated */
1870 return (authenticated);
1871}
1872#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001873