blob: 2dee9721de380a42dd5cc574b07989e6878d85ac [file] [log] [blame]
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001/* $OpenBSD: monitor.c,v 1.169 2017/05/30 14:10:53 markus Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100031#include <sys/socket.h>
Damien Millerd7834352006-08-05 12:39:39 +100032#include "openbsd-compat/sys-tree.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110033#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000034
Darren Tucker39972492006-07-12 22:22:46 +100035#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100036#include <fcntl.h>
djm@openbsd.org1a31d022016-05-02 08:49:03 +000037#include <limits.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110038#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110039#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110040#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100041#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110042#include <signal.h>
Damien Miller9af21972015-02-24 09:04:32 +110043#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000044#include <stdint.h>
Damien Miller9af21972015-02-24 09:04:32 +110045#endif
Damien Millere7a1e5c2006-08-05 11:34:19 +100046#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100047#include <string.h>
Damien Miller86687062014-07-02 15:28:02 +100048#include <stdarg.h>
49#include <stdio.h>
Damien Miller607aede2006-09-01 15:48:19 +100050#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100051#ifdef HAVE_POLL_H
52#include <poll.h>
53#else
54# ifdef HAVE_SYS_POLL_H
55# include <sys/poll.h>
56# endif
57#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000058
59#ifdef SKEY
60#include <skey.h>
61#endif
62
Damien Miller1f0311c2014-05-15 14:24:09 +100063#ifdef WITH_OPENSSL
Damien Miller03e20032006-03-15 11:16:59 +110064#include <openssl/dh.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100065#endif
Damien Miller03e20032006-03-15 11:16:59 +110066
Damien Millerb84886b2008-05-19 15:05:07 +100067#include "openbsd-compat/sys-queue.h"
Damien Miller8f0bf232011-06-20 14:42:23 +100068#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100069#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#include "key.h"
72#include "buffer.h"
73#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000074#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100075#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000076#include "kex.h"
77#include "dh.h"
Darren Tucker01558b72016-07-18 09:33:25 +100078#include "auth-pam.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000079#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
80#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000081#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000082#define TARGET_OS_MAC 1
83#else
84#include "zlib.h"
85#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000086#include "packet.h"
87#include "auth-options.h"
88#include "sshpty.h"
89#include "channels.h"
90#include "session.h"
91#include "sshlogin.h"
92#include "canohost.h"
93#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100094#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095#include "servconf.h"
96#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +100097#ifdef GSSAPI
98#include "ssh-gss.h"
99#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000100#include "monitor_wrap.h"
101#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000102#include "compat.h"
103#include "ssh2.h"
Damien Miller85b45e02013-07-20 13:21:52 +1000104#include "authfd.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000105#include "match.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000106#include "ssherr.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107
Darren Tucker0efd1552003-08-26 11:49:55 +1000108#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000109static Gssctxt *gsscontext = NULL;
110#endif
111
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000112/* Imports */
113extern ServerOptions options;
114extern u_int utmp_len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000115extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000116extern Buffer auth_debug;
117extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000118extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000119
120/* State exported from the child */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000121static struct sshbuf *child_state;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000122
Damien Miller469954d2003-06-18 20:25:33 +1000123/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000124
125int mm_answer_moduli(int, Buffer *);
126int mm_answer_sign(int, Buffer *);
127int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000128int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000129int mm_answer_authserv(int, Buffer *);
130int mm_answer_authpassword(int, Buffer *);
131int mm_answer_bsdauthquery(int, Buffer *);
132int mm_answer_bsdauthrespond(int, Buffer *);
133int mm_answer_skeyquery(int, Buffer *);
134int mm_answer_skeyrespond(int, Buffer *);
135int mm_answer_keyallowed(int, Buffer *);
136int mm_answer_keyverify(int, Buffer *);
137int mm_answer_pty(int, Buffer *);
138int mm_answer_pty_cleanup(int, Buffer *);
139int mm_answer_term(int, Buffer *);
140int mm_answer_rsa_keyallowed(int, Buffer *);
141int mm_answer_rsa_challenge(int, Buffer *);
142int mm_answer_rsa_response(int, Buffer *);
143int mm_answer_sesskey(int, Buffer *);
144int mm_answer_sessid(int, Buffer *);
145
Damien Miller79418552002-04-23 20:28:48 +1000146#ifdef USE_PAM
147int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000148int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000149int mm_answer_pam_init_ctx(int, Buffer *);
150int mm_answer_pam_query(int, Buffer *);
151int mm_answer_pam_respond(int, Buffer *);
152int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000153#endif
154
Darren Tucker0efd1552003-08-26 11:49:55 +1000155#ifdef GSSAPI
156int mm_answer_gss_setup_ctx(int, Buffer *);
157int mm_answer_gss_accept_ctx(int, Buffer *);
158int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100159int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000160#endif
Damien Miller25162f22002-09-12 09:47:29 +1000161
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100162#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100163int mm_answer_audit_event(int, Buffer *);
164int mm_answer_audit_command(int, Buffer *);
165#endif
166
Damien Miller8f0bf232011-06-20 14:42:23 +1000167static int monitor_read_log(struct monitor *);
168
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000169static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000170
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171/* local state for key verify */
172static u_char *key_blob = NULL;
173static u_int key_bloblen = 0;
174static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000175static char *hostbased_cuser = NULL;
176static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000177static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100178static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000179static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000180static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000181static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000182
183struct mon_table {
184 enum monitor_reqtype type;
185 int flags;
186 int (*f)(int, Buffer *);
187};
188
189#define MON_ISAUTH 0x0004 /* Required for Authentication */
190#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
191#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100192#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000193
194#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
195
196#define MON_PERMIT 0x1000 /* Request is permitted */
197
198struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000199#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000200 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000201#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
203 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
204 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000205 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000207#ifdef USE_PAM
208 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000209 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Millerb38b95f2016-08-29 11:47:07 +1000210 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
211 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
212 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000213 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000214#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100215#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100216 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100217#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000218#ifdef BSD_AUTH
219 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100220 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000221#endif
222#ifdef SKEY
223 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
224 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
225#endif
226 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
227 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000228#ifdef GSSAPI
229 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
djm@openbsd.orgb33ad6d2016-09-05 13:57:31 +0000230 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
231 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
232 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000233#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000234 {0, 0, NULL}
235};
236
237struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000238#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000239 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000240#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000241 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
242 {MONITOR_REQ_PTY, 0, mm_answer_pty},
243 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
244 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100245#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100246 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
247 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
248#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000249 {0, 0, NULL}
250};
251
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000252struct mon_table *mon_dispatch;
253
254/* Specifies if a certain message is allowed at the moment */
255
256static void
257monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
258{
259 while (ent->f != NULL) {
260 if (ent->type == type) {
261 ent->flags &= ~MON_PERMIT;
262 ent->flags |= permit ? MON_PERMIT : 0;
263 return;
264 }
265 ent++;
266 }
267}
268
269static void
270monitor_permit_authentications(int permit)
271{
272 struct mon_table *ent = mon_dispatch;
273
274 while (ent->f != NULL) {
275 if (ent->flags & MON_AUTH) {
276 ent->flags &= ~MON_PERMIT;
277 ent->flags |= permit ? MON_PERMIT : 0;
278 }
279 ent++;
280 }
281}
282
Darren Tucker3e33cec2003-10-02 16:12:36 +1000283void
284monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000285{
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000286 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000287 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100288 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000289
290 debug3("preauth child monitor started");
291
Damien Miller8f0bf232011-06-20 14:42:23 +1000292 close(pmonitor->m_recvfd);
293 close(pmonitor->m_log_sendfd);
294 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
295
Darren Tucker3e33cec2003-10-02 16:12:36 +1000296 authctxt = _authctxt;
297 memset(authctxt, 0, sizeof(*authctxt));
298
Darren Tuckerde0de392005-03-31 23:52:04 +1000299 authctxt->loginmsg = &loginmsg;
300
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000301 mon_dispatch = mon_dispatch_proto20;
302 /* Permit requests for moduli and signatures */
303 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
304 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000305
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000306 /* The first few requests do not require asynchronous access */
307 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100308 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100309 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100310 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100311 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100312
313 /* Special handling for multiple required authentications */
314 if (options.num_auth_methods != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100315 if (authenticated &&
316 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000317 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100318 debug3("%s: method %s: partial", __func__,
319 auth_method);
320 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100321 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100322 }
323 }
324
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000325 if (authenticated) {
326 if (!(ent->flags & MON_AUTHDECIDE))
327 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000328 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000329 if (authctxt->pw->pw_uid == 0 &&
330 !auth_root_allowed(auth_method))
331 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000332#ifdef USE_PAM
333 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100334 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000335 Buffer m;
336
337 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100338 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000339 MONITOR_REQ_PAM_ACCOUNT, &m);
340 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
341 buffer_free(&m);
342 }
343#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000344 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100345 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100346 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000347 auth_method, auth_submethod);
djm@openbsd.org94885382015-06-22 23:42:16 +0000348 if (!partial && !authenticated)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000349 authctxt->failures++;
350 }
351 }
352
353 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000354 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100355 if (strcmp(auth_method, "unknown") == 0)
356 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357
358 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000359 __func__, authctxt->user);
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000360 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000361
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000362 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000363
Damien Miller6a1937e2012-12-12 10:44:38 +1100364 /* Drain any buffered messages from the child */
365 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
366 ;
367
Damien Miller8f0bf232011-06-20 14:42:23 +1000368 close(pmonitor->m_sendfd);
369 close(pmonitor->m_log_recvfd);
370 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000371}
372
Damien Millerbe64d432003-05-14 19:31:12 +1000373static void
374monitor_set_child_handler(pid_t pid)
375{
376 monitor_child_pid = pid;
377}
378
379static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000380monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000381{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000382 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000383}
384
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000385void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000386monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000387{
Damien Miller8f0bf232011-06-20 14:42:23 +1000388 close(pmonitor->m_recvfd);
389 pmonitor->m_recvfd = -1;
390
Damien Millerbe64d432003-05-14 19:31:12 +1000391 monitor_set_child_handler(pmonitor->m_pid);
392 signal(SIGHUP, &monitor_child_handler);
393 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000394 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000395#ifdef SIGXFSZ
396 signal(SIGXFSZ, SIG_IGN);
397#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000398
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000399 mon_dispatch = mon_dispatch_postauth20;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000400
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000401 /* Permit requests for moduli and signatures */
402 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
403 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
404 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
405
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000406 if (!no_pty_flag) {
407 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
408 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
409 }
410
411 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000412 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000413}
414
Damien Miller8f0bf232011-06-20 14:42:23 +1000415static int
416monitor_read_log(struct monitor *pmonitor)
417{
418 Buffer logmsg;
419 u_int len, level;
420 char *msg;
421
422 buffer_init(&logmsg);
423
424 /* Read length */
425 buffer_append_space(&logmsg, 4);
426 if (atomicio(read, pmonitor->m_log_recvfd,
427 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
428 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100429 buffer_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 }
437 len = buffer_get_int(&logmsg);
438 if (len <= 4 || len > 8192)
439 fatal("%s: invalid log message length %u", __func__, len);
440
441 /* Read severity, message */
442 buffer_clear(&logmsg);
443 buffer_append_space(&logmsg, len);
444 if (atomicio(read, pmonitor->m_log_recvfd,
445 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
446 fatal("%s: log fd read: %s", __func__, strerror(errno));
447
448 /* Log it */
449 level = buffer_get_int(&logmsg);
450 msg = buffer_get_string(&logmsg, NULL);
451 if (log_level_name(level) == NULL)
452 fatal("%s: invalid log level %u (corrupted message?)",
453 __func__, level);
454 do_log2(level, "%s [preauth]", msg);
455
456 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000457 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000458
459 return 0;
460}
461
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000462int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000463monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000464 struct mon_table **pent)
465{
466 Buffer m;
467 int ret;
468 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000469 struct pollfd pfd[2];
470
471 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100472 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000473 pfd[0].fd = pmonitor->m_sendfd;
474 pfd[0].events = POLLIN;
475 pfd[1].fd = pmonitor->m_log_recvfd;
476 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000477 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
478 if (errno == EINTR || errno == EAGAIN)
479 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000480 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000481 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000482 if (pfd[1].revents) {
483 /*
484 * Drain all log messages before processing next
485 * monitor request.
486 */
487 monitor_read_log(pmonitor);
488 continue;
489 }
490 if (pfd[0].revents)
491 break; /* Continues below */
492 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000493
494 buffer_init(&m);
495
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000496 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000497 type = buffer_get_char(&m);
498
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000499 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000500
501 while (ent->f != NULL) {
502 if (ent->type == type)
503 break;
504 ent++;
505 }
506
507 if (ent->f != NULL) {
508 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000509 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000510 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000511 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000512 buffer_free(&m);
513
514 /* The child may use this request only once, disable it */
515 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000516 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000517 type);
518 ent->flags &= ~MON_PERMIT;
519 }
520
521 if (pent != NULL)
522 *pent = ent;
523
524 return ret;
525 }
526
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000527 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000528
529 /* NOTREACHED */
530 return (-1);
531}
532
533/* allowed key state */
534static int
535monitor_allowed_key(u_char *blob, u_int bloblen)
536{
537 /* make sure key is allowed */
538 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000539 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000540 return (0);
541 return (1);
542}
543
544static void
545monitor_reset_key_state(void)
546{
547 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000548 free(key_blob);
549 free(hostbased_cuser);
550 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000551 key_blob = NULL;
552 key_bloblen = 0;
553 key_blobtype = MM_NOKEY;
554 hostbased_cuser = NULL;
555 hostbased_chost = NULL;
556}
557
Damien Miller1f0311c2014-05-15 14:24:09 +1000558#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000559int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000560mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561{
562 DH *dh;
563 int min, want, max;
564
565 min = buffer_get_int(m);
566 want = buffer_get_int(m);
567 max = buffer_get_int(m);
568
569 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000570 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000571 /* We need to check here, too, in case the child got corrupted */
572 if (max < min || want < min || max < want)
573 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000574 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000575
576 buffer_clear(m);
577
578 dh = choose_dh(min, want, max);
579 if (dh == NULL) {
580 buffer_put_char(m, 0);
581 return (0);
582 } else {
583 /* Send first bignum */
584 buffer_put_char(m, 1);
585 buffer_put_bignum2(m, dh->p);
586 buffer_put_bignum2(m, dh->g);
587
588 DH_free(dh);
589 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000590 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000591 return (0);
592}
Damien Miller1f0311c2014-05-15 14:24:09 +1000593#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594
595int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000596mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000598 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000599 extern int auth_sock; /* XXX move to state struct? */
600 struct sshkey *key;
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000601 struct sshbuf *sigbuf = NULL;
602 u_char *p = NULL, *signature = NULL;
603 char *alg = NULL;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000604 size_t datlen, siglen, alglen;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000605 int r, is_proof = 0;
606 u_int keyid;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000607 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000608
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000609 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000610
djm@openbsd.org141efe42015-01-14 20:05:27 +0000611 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000612 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
613 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000614 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000615 if (keyid > INT_MAX)
616 fatal("%s: invalid key ID", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000617
Damien Millera63128d2006-03-15 12:08:28 +1100618 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000619 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
620 * SHA384 (48 bytes) and SHA512 (64 bytes).
djm@openbsd.org523463a2015-02-16 22:13:32 +0000621 *
622 * Otherwise, verify the signature request is for a hostkey
623 * proof.
624 *
625 * XXX perform similar check for KEX signature requests too?
626 * it's not trivial, since what is signed is the hash, rather
627 * than the full kex structure...
Damien Millera63128d2006-03-15 12:08:28 +1100628 */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000629 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
630 /*
631 * Construct expected hostkey proof and compare it to what
632 * the client sent us.
633 */
634 if (session_id2_len == 0) /* hostkeys is never first */
635 fatal("%s: bad data length: %zu", __func__, datlen);
636 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
637 fatal("%s: no hostkey for index %d", __func__, keyid);
638 if ((sigbuf = sshbuf_new()) == NULL)
639 fatal("%s: sshbuf_new", __func__);
djm@openbsd.org44732de2015-02-20 22:17:21 +0000640 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
641 (r = sshbuf_put_string(sigbuf, session_id2,
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000642 session_id2_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000643 (r = sshkey_puts(key, sigbuf)) != 0)
644 fatal("%s: couldn't prepare private key "
645 "proof buffer: %s", __func__, ssh_err(r));
646 if (datlen != sshbuf_len(sigbuf) ||
647 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
648 fatal("%s: bad data length: %zu, hostkey proof len %zu",
649 __func__, datlen, sshbuf_len(sigbuf));
650 sshbuf_free(sigbuf);
651 is_proof = 1;
652 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000653
Ben Lindstromf67e0772002-06-06 20:58:19 +0000654 /* save session id, it will be passed on the first call */
655 if (session_id2_len == 0) {
656 session_id2_len = datlen;
657 session_id2 = xmalloc(session_id2_len);
658 memcpy(session_id2, p, session_id2_len);
659 }
660
Damien Miller85b45e02013-07-20 13:21:52 +1000661 if ((key = get_hostkey_by_index(keyid)) != NULL) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000662 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000663 datafellows)) != 0)
664 fatal("%s: sshkey_sign failed: %s",
665 __func__, ssh_err(r));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000666 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000667 auth_sock > 0) {
668 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000669 p, datlen, alg, datafellows)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000670 fatal("%s: ssh_agent_sign failed: %s",
671 __func__, ssh_err(r));
672 }
Damien Miller85b45e02013-07-20 13:21:52 +1000673 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000674 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000675
djm@openbsd.org523463a2015-02-16 22:13:32 +0000676 debug3("%s: %s signature %p(%zu)", __func__,
677 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000678
djm@openbsd.org141efe42015-01-14 20:05:27 +0000679 sshbuf_reset(m);
680 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
681 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000682
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000683 free(alg);
Darren Tuckera627d422013-06-02 07:31:17 +1000684 free(p);
685 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000686
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000687 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000688
689 /* Turn on permissions for getpwnam */
690 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
691
692 return (0);
693}
694
695/* Retrieves the password entry and also checks if the user is permitted */
696
697int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000698mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000699{
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000700 struct ssh *ssh = active_state; /* XXX */
Darren Tuckerb09b6772004-06-22 15:06:46 +1000701 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000702 struct passwd *pwent;
703 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000704 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000705
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000706 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000707
708 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000709 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000710
Darren Tuckerb09b6772004-06-22 15:06:46 +1000711 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000712
Darren Tuckerb09b6772004-06-22 15:06:46 +1000713 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000714
Darren Tuckerb09b6772004-06-22 15:06:46 +1000715 authctxt->user = xstrdup(username);
716 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000717 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000718
719 buffer_clear(m);
720
721 if (pwent == NULL) {
722 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100723 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000724 goto out;
725 }
726
727 allowed = 1;
728 authctxt->pw = pwent;
729 authctxt->valid = 1;
730
731 buffer_put_char(m, 1);
732 buffer_put_string(m, pwent, sizeof(struct passwd));
733 buffer_put_cstring(m, pwent->pw_name);
734 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000735#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000736 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000737#endif
738#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000739 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000740#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000741 buffer_put_cstring(m, pwent->pw_dir);
742 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100743
744 out:
djm@openbsd.orgf1a19342017-02-03 23:05:57 +0000745 ssh_packet_set_log_preamble(ssh, "%suser %s",
746 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
Darren Tucker1629c072007-02-19 22:25:37 +1100747 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000748
749#define M_CP_STROPT(x) do { \
750 if (options.x != NULL) \
751 buffer_put_cstring(m, options.x); \
752 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000753#define M_CP_STRARRAYOPT(x, nx) do { \
754 for (i = 0; i < options.nx; i++) \
755 buffer_put_cstring(m, options.x[i]); \
756 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000757 /* See comment in servconf.h */
758 COPY_MATCH_STRING_OPTS();
759#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000760#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100761
762 /* Create valid auth method lists */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000763 if (auth2_setup_methods_lists(authctxt) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100764 /*
765 * The monitor will continue long enough to let the child
766 * run to it's packet_disconnect(), but it must not allow any
767 * authentication to succeed.
768 */
769 debug("%s: no valid authentication method lists", __func__);
770 }
771
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000772 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000773 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000774
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000775 /* Allow service/style information on the auth context */
776 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
777 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
778
Damien Millera33501b2002-05-08 12:24:42 +1000779#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000780 if (options.use_pam)
781 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000782#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000783
784 return (0);
785}
786
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000787int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000788{
789 char *banner;
790
791 buffer_clear(m);
792 banner = auth2_read_banner();
793 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000794 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000795 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000796
797 return (0);
798}
799
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000800int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000801mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000802{
803 monitor_permit_authentications(1);
804
805 authctxt->service = buffer_get_string(m, NULL);
806 authctxt->style = buffer_get_string(m, NULL);
807 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000808 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000809
810 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000811 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000812 authctxt->style = NULL;
813 }
814
815 return (0);
816}
817
818int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000819mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000820{
821 static int call_count;
822 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000823 int authenticated;
824 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000825
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000826 if (!options.password_authentication)
827 fatal("%s: password authentication not enabled", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000828 passwd = buffer_get_string(m, &plen);
829 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000830 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000831 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100832 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000833 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000834
835 buffer_clear(m);
836 buffer_put_int(m, authenticated);
Darren Tucker01558b72016-07-18 09:33:25 +1000837#ifdef USE_PAM
838 buffer_put_int(m, sshpam_get_maxtries_reached());
839#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000840
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000841 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000842 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000843
844 call_count++;
845 if (plen == 0 && call_count == 1)
846 auth_method = "none";
847 else
848 auth_method = "password";
849
850 /* Causes monitor loop to terminate if authenticated */
851 return (authenticated);
852}
853
854#ifdef BSD_AUTH
855int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000856mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000857{
858 char *name, *infotxt;
859 u_int numprompts;
860 u_int *echo_on;
861 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100862 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000863
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000864 if (!options.kbd_interactive_authentication)
865 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb7df3af2003-02-24 11:55:46 +1100866 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
867 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000868
869 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100870 buffer_put_int(m, success);
871 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000872 buffer_put_cstring(m, prompts[0]);
873
Damien Millerb7df3af2003-02-24 11:55:46 +1100874 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000875 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000876
Damien Millerb7df3af2003-02-24 11:55:46 +1100877 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000878 free(name);
879 free(infotxt);
880 free(prompts);
881 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000882 }
883
884 return (0);
885}
886
887int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000888mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000889{
890 char *response;
891 int authok;
892
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000893 if (!options.kbd_interactive_authentication)
894 fatal("%s: kbd-int authentication not enabled", __func__);
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +0000895 if (authctxt->as == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000896 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000897
898 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000899 authok = options.challenge_response_authentication &&
900 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000901 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000902 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000903 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000904
905 buffer_clear(m);
906 buffer_put_int(m, authok);
907
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000908 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000909 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000910
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000911 auth_method = "keyboard-interactive";
912 auth_submethod = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000913
914 return (authok != 0);
915}
916#endif
917
918#ifdef SKEY
919int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000920mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000921{
922 struct skey skey;
923 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100924 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000925
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000926 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
927 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000928
929 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100930 buffer_put_int(m, success);
931 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932 buffer_put_cstring(m, challenge);
933
Damien Millerb7df3af2003-02-24 11:55:46 +1100934 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000935 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000936
937 return (0);
938}
939
940int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000941mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000942{
943 char *response;
944 int authok;
945
946 response = buffer_get_string(m, NULL);
947
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000948 authok = (options.challenge_response_authentication &&
949 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000950 skey_haskey(authctxt->pw->pw_name) == 0 &&
951 skey_passcheck(authctxt->pw->pw_name, response) != -1);
952
Darren Tuckerf60845f2013-06-02 08:07:31 +1000953 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000954
955 buffer_clear(m);
956 buffer_put_int(m, authok);
957
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000958 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000959 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000960
Damien Millerac947352015-11-20 12:35:41 +1100961 auth_method = "keyboard-interactive";
962 auth_submethod = "skey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000963
964 return (authok != 0);
965}
966#endif
967
Damien Miller79418552002-04-23 20:28:48 +1000968#ifdef USE_PAM
969int
Darren Tucker7eda5922004-06-22 13:22:50 +1000970mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000971{
Damien Miller4e448a32003-05-14 15:11:48 +1000972 if (!options.use_pam)
973 fatal("UsePAM not set, but ended up in %s anyway", __func__);
974
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100975 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000976
Damien Miller1f499fd2003-08-25 13:08:49 +1000977 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
Damien Miller775f8a22016-08-31 10:48:07 +1000978 if (options.kbd_interactive_authentication)
979 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller1f499fd2003-08-25 13:08:49 +1000980
Damien Miller79418552002-04-23 20:28:48 +1000981 return (0);
982}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000983
Damien Miller1f499fd2003-08-25 13:08:49 +1000984int
Darren Tucker7eda5922004-06-22 13:22:50 +1000985mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000986{
987 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100988
Damien Miller1f499fd2003-08-25 13:08:49 +1000989 if (!options.use_pam)
Damien Miller775f8a22016-08-31 10:48:07 +1000990 fatal("%s: PAM not enabled", __func__);
Damien Miller1f499fd2003-08-25 13:08:49 +1000991
992 ret = do_pam_account();
993
994 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000995 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000996
Darren Tucker7eda5922004-06-22 13:22:50 +1000997 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000998
999 return (ret);
1000}
1001
Damien Miller4f9f42a2003-05-10 19:28:02 +10001002static void *sshpam_ctxt, *sshpam_authok;
1003extern KbdintDevice sshpam_device;
1004
1005int
Darren Tucker7eda5922004-06-22 13:22:50 +10001006mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001007{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001008 debug3("%s", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001009 if (!options.kbd_interactive_authentication)
1010 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001011 if (sshpam_ctxt != NULL)
1012 fatal("%s: already called", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001013 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1014 sshpam_authok = NULL;
1015 buffer_clear(m);
1016 if (sshpam_ctxt != NULL) {
1017 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
Damien Millerb38b95f2016-08-29 11:47:07 +10001018 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001019 buffer_put_int(m, 1);
1020 } else {
1021 buffer_put_int(m, 0);
1022 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001023 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001024 return (0);
1025}
1026
1027int
Darren Tucker7eda5922004-06-22 13:22:50 +10001028mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001029{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001030 char *name = NULL, *info = NULL, **prompts = NULL;
1031 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001032 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001033
1034 debug3("%s", __func__);
1035 sshpam_authok = NULL;
Damien Millerb38b95f2016-08-29 11:47:07 +10001036 if (sshpam_ctxt == NULL)
1037 fatal("%s: no context", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001038 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1039 &num, &prompts, &echo_on);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001040 if (ret == 0 && num == 0)
1041 sshpam_authok = sshpam_ctxt;
1042 if (num > 1 || name == NULL || info == NULL)
Damien Millerb38b95f2016-08-29 11:47:07 +10001043 fatal("sshpam_device.query failed");
1044 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001045 buffer_clear(m);
1046 buffer_put_int(m, ret);
1047 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001048 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001049 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001050 free(info);
Darren Tucker01558b72016-07-18 09:33:25 +10001051 buffer_put_int(m, sshpam_get_maxtries_reached());
Damien Miller4f9f42a2003-05-10 19:28:02 +10001052 buffer_put_int(m, num);
1053 for (i = 0; i < num; ++i) {
1054 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001055 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001056 buffer_put_int(m, echo_on[i]);
1057 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001058 free(prompts);
1059 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001060 auth_method = "keyboard-interactive";
1061 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001062 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001063 return (0);
1064}
1065
1066int
Darren Tucker7eda5922004-06-22 13:22:50 +10001067mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001068{
1069 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001070 u_int i, num;
1071 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001072
1073 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001074 if (sshpam_ctxt == NULL)
1075 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001076 sshpam_authok = NULL;
1077 num = buffer_get_int(m);
1078 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001079 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001080 for (i = 0; i < num; ++i)
1081 resp[i] = buffer_get_string(m, NULL);
1082 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1083 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001084 free(resp[i]);
1085 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001086 } else {
1087 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1088 }
1089 buffer_clear(m);
1090 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001091 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001092 auth_method = "keyboard-interactive";
1093 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001094 if (ret == 0)
1095 sshpam_authok = sshpam_ctxt;
1096 return (0);
1097}
1098
1099int
Darren Tucker7eda5922004-06-22 13:22:50 +10001100mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001101{
Damien Miller5e75f512015-08-11 13:34:12 +10001102 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001103
1104 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001105 if (sshpam_ctxt == NULL)
1106 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001107 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001108 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001109 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001110 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Millerb38b95f2016-08-29 11:47:07 +10001111 /* Allow another attempt */
1112 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller15b05cf2012-12-03 09:53:20 +11001113 auth_method = "keyboard-interactive";
1114 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001115 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001116}
Damien Miller79418552002-04-23 20:28:48 +10001117#endif
1118
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001119int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001120mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001121{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001122 struct sshkey *key;
Damien Millera10f5612002-09-12 09:49:15 +10001123 char *cuser, *chost;
1124 u_char *blob;
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001125 u_int bloblen, pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001126 enum mm_keytype type = 0;
1127 int allowed = 0;
1128
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001129 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001130
1131 type = buffer_get_int(m);
1132 cuser = buffer_get_string(m, NULL);
1133 chost = buffer_get_string(m, NULL);
1134 blob = buffer_get_string(m, &bloblen);
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001135 pubkey_auth_attempt = buffer_get_int(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001136
1137 key = key_from_blob(blob, bloblen);
1138
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001139 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001140
Damien Miller3e3b5142003-11-17 21:13:40 +11001141 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001142 /* These should not make it past the privsep child */
1143 if (key_type_plain(key->type) == KEY_RSA &&
1144 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1145 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1146
Darren Tucker47eede72005-03-14 23:08:12 +11001147 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001148 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001149 allowed = options.pubkey_authentication &&
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001150 !auth2_userkey_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001151 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001152 options.pubkey_key_types, 0) == 1 &&
1153 user_key_allowed(authctxt->pw, key,
1154 pubkey_auth_attempt);
Damien Miller20bdcd72013-07-18 16:10:09 +10001155 pubkey_auth_info(authctxt, key, NULL);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001156 auth_method = "publickey";
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001157 if (options.pubkey_authentication &&
1158 (!pubkey_auth_attempt || allowed != 1))
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001159 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001160 break;
1161 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001162 allowed = options.hostbased_authentication &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001163 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001164 options.hostbased_key_types, 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001165 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001166 cuser, chost, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001167 pubkey_auth_info(authctxt, key,
1168 "client user \"%.100s\", client host \"%.100s\"",
1169 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001170 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001171 break;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001172 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001173 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001174 break;
1175 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001176 }
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001177
1178 debug3("%s: key %p is %s",
1179 __func__, key, allowed ? "allowed" : "not allowed");
1180
Damien Miller00111382003-03-10 11:21:17 +11001181 if (key != NULL)
1182 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001183
1184 /* clear temporarily storage (used by verify) */
1185 monitor_reset_key_state();
1186
1187 if (allowed) {
1188 /* Save temporarily for comparison in verify */
1189 key_blob = blob;
1190 key_bloblen = bloblen;
1191 key_blobtype = type;
1192 hostbased_cuser = cuser;
1193 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001194 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001195 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001196 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001197 free(blob);
1198 free(cuser);
1199 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001200 }
1201
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202 buffer_clear(m);
1203 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001204 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001205
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001206 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001207
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001208 return (0);
1209}
1210
1211static int
1212monitor_valid_userblob(u_char *data, u_int datalen)
1213{
1214 Buffer b;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001215 u_char *p;
1216 char *userstyle, *cp;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001217 u_int len;
1218 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001219
1220 buffer_init(&b);
1221 buffer_append(&b, data, datalen);
1222
1223 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001224 p = buffer_ptr(&b);
1225 len = buffer_len(&b);
1226 if ((session_id2 == NULL) ||
1227 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001228 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001229 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001230 buffer_consume(&b, session_id2_len);
1231 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001232 p = buffer_get_string(&b, &len);
1233 if ((session_id2 == NULL) ||
1234 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001235 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001236 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001237 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001238 }
1239 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1240 fail++;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001241 cp = buffer_get_cstring(&b, NULL);
Damien Miller4ce189d2013-04-23 15:17:52 +10001242 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1243 authctxt->style ? ":" : "",
1244 authctxt->style ? authctxt->style : "");
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001245 if (strcmp(userstyle, cp) != 0) {
1246 logit("wrong user name passed to monitor: "
1247 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001248 fail++;
1249 }
Darren Tuckera627d422013-06-02 07:31:17 +10001250 free(userstyle);
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001251 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001252 buffer_skip_string(&b);
1253 if (datafellows & SSH_BUG_PKAUTH) {
1254 if (!buffer_get_char(&b))
1255 fail++;
1256 } else {
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001257 cp = buffer_get_cstring(&b, NULL);
1258 if (strcmp("publickey", cp) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001259 fail++;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001260 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001261 if (!buffer_get_char(&b))
1262 fail++;
1263 buffer_skip_string(&b);
1264 }
1265 buffer_skip_string(&b);
1266 if (buffer_len(&b) != 0)
1267 fail++;
1268 buffer_free(&b);
1269 return (fail == 0);
1270}
1271
1272static int
Damien Millera10f5612002-09-12 09:49:15 +10001273monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1274 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001275{
1276 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001277 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001278 u_int len;
1279 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001280
1281 buffer_init(&b);
1282 buffer_append(&b, data, datalen);
1283
Ben Lindstromf67e0772002-06-06 20:58:19 +00001284 p = buffer_get_string(&b, &len);
1285 if ((session_id2 == NULL) ||
1286 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001287 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001288 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001289 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001290
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001291 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1292 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001293 p = buffer_get_cstring(&b, NULL);
1294 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1295 authctxt->style ? ":" : "",
1296 authctxt->style ? authctxt->style : "");
1297 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001298 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001299 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001300 fail++;
1301 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001302 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001303 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001304 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001305 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001306 if (strcmp(p, "hostbased") != 0)
1307 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001308 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001309 buffer_skip_string(&b); /* pkalg */
1310 buffer_skip_string(&b); /* pkblob */
1311
1312 /* verify client host, strip trailing dot if necessary */
1313 p = buffer_get_string(&b, NULL);
1314 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1315 p[len - 1] = '\0';
1316 if (strcmp(p, chost) != 0)
1317 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001318 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001319
1320 /* verify client user */
1321 p = buffer_get_string(&b, NULL);
1322 if (strcmp(p, cuser) != 0)
1323 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001324 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001325
1326 if (buffer_len(&b) != 0)
1327 fail++;
1328 buffer_free(&b);
1329 return (fail == 0);
1330}
1331
1332int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001333mm_answer_keyverify(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001334{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001335 struct sshkey *key;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001336 u_char *signature, *data, *blob;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001337 size_t signaturelen, datalen, bloblen;
1338 int r, ret, valid_data = 0, encoded_ret;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001339
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001340 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1341 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
1342 (r = sshbuf_get_string(m, &data, &datalen)) != 0)
1343 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001344
1345 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001346 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001347 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001348
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001349 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1350 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1351 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001352
1353 switch (key_blobtype) {
1354 case MM_USERKEY:
1355 valid_data = monitor_valid_userblob(data, datalen);
1356 break;
1357 case MM_HOSTKEY:
1358 valid_data = monitor_valid_hostbasedblob(data, datalen,
1359 hostbased_cuser, hostbased_chost);
1360 break;
1361 default:
1362 valid_data = 0;
1363 break;
1364 }
1365 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001366 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001367
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001368 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
1369 active_state->compat);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001370 debug3("%s: key %p signature %s",
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001371 __func__, key, (ret == 0) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001372
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001373 /* If auth was successful then record key to ensure it isn't reused */
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001374 if (ret == 0 && key_blobtype == MM_USERKEY)
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001375 auth2_record_userkey(authctxt, key);
1376 else
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001377 sshkey_free(key);
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001378
Darren Tuckera627d422013-06-02 07:31:17 +10001379 free(blob);
1380 free(signature);
1381 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001382
Ben Lindstrome1c09122002-06-23 00:38:24 +00001383 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1384
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001385 monitor_reset_key_state();
1386
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001387 sshbuf_reset(m);
1388
1389 /* encode ret != 0 as positive integer, since we're sending u32 */
1390 encoded_ret = (ret != 0);
1391 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1392 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001393 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001394
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001395 return ret == 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001396}
1397
1398static void
1399mm_record_login(Session *s, struct passwd *pw)
1400{
djm@openbsd.org95767262016-03-07 19:02:43 +00001401 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001402 socklen_t fromlen;
1403 struct sockaddr_storage from;
1404
1405 /*
1406 * Get IP address of client. If the connection is not a socket, let
1407 * the address be 0.0.0.0.
1408 */
1409 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001410 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001411 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001412 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001413 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001414 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001415 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001416 }
1417 }
1418 /* Record that there was a login on that tty from the remote host. */
1419 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +00001420 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001421 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001422}
1423
1424static void
1425mm_session_close(Session *s)
1426{
Damien Miller04bd8b02003-05-25 14:38:33 +10001427 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001429 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001430 session_pty_cleanup2(s);
1431 }
Damien Miller7207f642008-05-19 15:34:50 +10001432 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001433}
1434
1435int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001436mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001437{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001438 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001439 Session *s;
1440 int res, fd0;
1441
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001442 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001443
1444 buffer_clear(m);
1445 s = session_new();
1446 if (s == NULL)
1447 goto error;
1448 s->authctxt = authctxt;
1449 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001450 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001451 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1452 if (res == 0)
1453 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001454 pty_setowner(authctxt->pw, s->tty);
1455
1456 buffer_put_int(m, 1);
1457 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001458
1459 /* We need to trick ttyslot */
1460 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001461 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001462
1463 mm_record_login(s, authctxt->pw);
1464
1465 /* Now we can close the file descriptor again */
1466 close(0);
1467
Darren Tucker09991742004-07-17 17:05:14 +10001468 /* send messages generated by record_login */
1469 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1470 buffer_clear(&loginmsg);
1471
1472 mm_request_send(sock, MONITOR_ANS_PTY, m);
1473
Damien Miller54fd7cf2007-09-17 12:04:08 +10001474 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1475 mm_send_fd(sock, s->ttyfd) == -1)
1476 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001477
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001478 /* make sure nothing uses fd 0 */
1479 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001480 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001481 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001482 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001483
1484 /* slave is not needed */
1485 close(s->ttyfd);
1486 s->ttyfd = s->ptyfd;
1487 /* no need to dup() because nobody closes ptyfd */
1488 s->ptymaster = s->ptyfd;
1489
Damien Miller9ab00b42006-08-05 12:40:11 +10001490 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001491
1492 return (0);
1493
1494 error:
1495 if (s != NULL)
1496 mm_session_close(s);
1497 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001498 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001499 return (0);
1500}
1501
1502int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001503mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001504{
1505 Session *s;
1506 char *tty;
1507
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001508 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001509
1510 tty = buffer_get_string(m, NULL);
1511 if ((s = session_by_tty(tty)) != NULL)
1512 mm_session_close(s);
1513 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001514 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001515 return (0);
1516}
1517
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001518int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001519mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001520{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001521 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001522 int res, status;
1523
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001524 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001525
1526 /* The child is terminating */
1527 session_destroy_all(&mm_session_close);
1528
Darren Tucker52358d62008-03-11 22:58:25 +11001529#ifdef USE_PAM
1530 if (options.use_pam)
1531 sshpam_cleanup();
1532#endif
1533
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001534 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001535 if (errno != EINTR)
1536 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001537
1538 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1539
1540 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001541 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001542}
1543
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001544#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001545/* Report that an audit event occurred */
1546int
1547mm_answer_audit_event(int socket, Buffer *m)
1548{
1549 ssh_audit_event_t event;
1550
1551 debug3("%s entering", __func__);
1552
1553 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001554 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001555 case SSH_AUTH_FAIL_PUBKEY:
1556 case SSH_AUTH_FAIL_HOSTBASED:
1557 case SSH_AUTH_FAIL_GSSAPI:
1558 case SSH_LOGIN_EXCEED_MAXTRIES:
1559 case SSH_LOGIN_ROOT_DENIED:
1560 case SSH_CONNECTION_CLOSE:
1561 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001562 audit_event(event);
1563 break;
1564 default:
1565 fatal("Audit event type %d not permitted", event);
1566 }
1567
1568 return (0);
1569}
1570
1571int
1572mm_answer_audit_command(int socket, Buffer *m)
1573{
1574 u_int len;
1575 char *cmd;
1576
1577 debug3("%s entering", __func__);
1578 cmd = buffer_get_string(m, &len);
1579 /* sanity check command, if so how? */
1580 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001581 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001582 return (0);
1583}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001584#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001585
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001586void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001587monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001588{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001589 struct ssh *ssh = active_state; /* XXX */
1590 struct kex *kex;
1591 int r;
1592
1593 debug3("%s: packet_set_state", __func__);
1594 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1595 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1596 sshbuf_free(child_state);
1597 child_state = NULL;
1598
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +00001599 if ((kex = ssh->kex) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001600 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001601#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001602 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1603 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +00001604 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1605 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1606 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001607 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1608 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001609# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001610 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001611# endif
Damien Miller773dda22015-01-30 23:10:17 +11001612#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001613 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1614 kex->load_host_public_key=&get_hostkey_public_by_type;
1615 kex->load_host_private_key=&get_hostkey_private_by_type;
1616 kex->host_key_index=&get_hostkey_index;
1617 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001618 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001619}
1620
1621/* This function requries careful sanity checking */
1622
1623void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001624mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001625{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001626 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001627
markus@openbsd.org091c3022015-01-19 19:52:16 +00001628 if ((child_state = sshbuf_new()) == NULL)
1629 fatal("%s: sshbuf_new failed", __func__);
1630 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1631 child_state);
1632 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001633}
1634
1635
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001636/* XXX */
1637
1638#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001639 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001640 fatal("fcntl(%d, F_SETFD)", x); \
1641} while (0)
1642
1643static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001644monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001645{
Damien Miller8f0bf232011-06-20 14:42:23 +10001646 int pair[2];
1647
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001648 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001649 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001650 FD_CLOSEONEXEC(pair[0]);
1651 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001652 mon->m_recvfd = pair[0];
1653 mon->m_sendfd = pair[1];
1654
1655 if (do_logfds) {
1656 if (pipe(pair) == -1)
1657 fatal("%s: pipe: %s", __func__, strerror(errno));
1658 FD_CLOSEONEXEC(pair[0]);
1659 FD_CLOSEONEXEC(pair[1]);
1660 mon->m_log_recvfd = pair[0];
1661 mon->m_log_sendfd = pair[1];
1662 } else
1663 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001664}
1665
1666#define MM_MEMSIZE 65536
1667
1668struct monitor *
1669monitor_init(void)
1670{
1671 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001672
Damien Miller07d86be2006-03-26 14:19:21 +11001673 mon = xcalloc(1, sizeof(*mon));
Damien Miller8f0bf232011-06-20 14:42:23 +10001674 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001675
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001676 return mon;
1677}
1678
1679void
1680monitor_reinit(struct monitor *mon)
1681{
Damien Miller8f0bf232011-06-20 14:42:23 +10001682 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001683}
Darren Tucker0efd1552003-08-26 11:49:55 +10001684
1685#ifdef GSSAPI
1686int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001687mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001688{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001689 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001690 OM_uint32 major;
1691 u_int len;
1692
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001693 if (!options.gss_authentication)
1694 fatal("%s: GSSAPI authentication not enabled", __func__);
1695
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001696 goid.elements = buffer_get_string(m, &len);
1697 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001698
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001699 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001700
Darren Tuckera627d422013-06-02 07:31:17 +10001701 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001702
1703 buffer_clear(m);
1704 buffer_put_int(m, major);
1705
Damien Miller6fd6def2005-11-05 15:07:05 +11001706 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001707
1708 /* Now we have a context, enable the step */
1709 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1710
1711 return (0);
1712}
1713
1714int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001715mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001716{
1717 gss_buffer_desc in;
1718 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001719 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001720 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001721 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001722
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001723 if (!options.gss_authentication)
1724 fatal("%s: GSSAPI authentication not enabled", __func__);
1725
Darren Tucker600ad8d2003-08-26 12:10:48 +10001726 in.value = buffer_get_string(m, &len);
1727 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001728 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001729 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001730
1731 buffer_clear(m);
1732 buffer_put_int(m, major);
1733 buffer_put_string(m, out.value, out.length);
1734 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001735 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001736
1737 gss_release_buffer(&minor, &out);
1738
Damien Miller6fd6def2005-11-05 15:07:05 +11001739 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001740 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1741 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001742 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001743 }
1744 return (0);
1745}
1746
1747int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001748mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001749{
1750 gss_buffer_desc gssbuf, mic;
1751 OM_uint32 ret;
1752 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001753
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001754 if (!options.gss_authentication)
1755 fatal("%s: GSSAPI authentication not enabled", __func__);
1756
Damien Miller0425d402003-11-17 22:18:21 +11001757 gssbuf.value = buffer_get_string(m, &len);
1758 gssbuf.length = len;
1759 mic.value = buffer_get_string(m, &len);
1760 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001761
Damien Miller0425d402003-11-17 22:18:21 +11001762 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001763
Darren Tuckera627d422013-06-02 07:31:17 +10001764 free(gssbuf.value);
1765 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001766
Damien Miller0425d402003-11-17 22:18:21 +11001767 buffer_clear(m);
1768 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001769
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001770 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001771
Damien Miller0425d402003-11-17 22:18:21 +11001772 if (!GSS_ERROR(ret))
1773 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001774
Damien Miller0425d402003-11-17 22:18:21 +11001775 return (0);
1776}
1777
1778int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001779mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001780{
1781 int authenticated;
1782
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001783 if (!options.gss_authentication)
1784 fatal("%s: GSSAPI authentication not enabled", __func__);
1785
Darren Tucker0efd1552003-08-26 11:49:55 +10001786 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1787
1788 buffer_clear(m);
1789 buffer_put_int(m, authenticated);
1790
1791 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001792 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001793
Damien Miller6fd6def2005-11-05 15:07:05 +11001794 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001795
1796 /* Monitor loop will terminate if authenticated */
1797 return (authenticated);
1798}
1799#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001800