blob: 43f484709b8b73763c491e592f65cc2d81123e19 [file] [log] [blame]
djm@openbsd.org0082fba2016-09-28 16:33:06 +00001/* $OpenBSD: monitor.c,v 1.166 2016/09/28 16:33:06 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
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{
286 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100287 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000288
289 debug3("preauth child monitor started");
290
Damien Miller8f0bf232011-06-20 14:42:23 +1000291 close(pmonitor->m_recvfd);
292 close(pmonitor->m_log_sendfd);
293 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
294
Darren Tucker3e33cec2003-10-02 16:12:36 +1000295 authctxt = _authctxt;
296 memset(authctxt, 0, sizeof(*authctxt));
297
Darren Tuckerde0de392005-03-31 23:52:04 +1000298 authctxt->loginmsg = &loginmsg;
299
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000300 mon_dispatch = mon_dispatch_proto20;
301 /* Permit requests for moduli and signatures */
302 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
303 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000304
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000305 /* The first few requests do not require asynchronous access */
306 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100307 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100308 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100309 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100310 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100311
312 /* Special handling for multiple required authentications */
313 if (options.num_auth_methods != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100314 if (authenticated &&
315 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000316 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100317 debug3("%s: method %s: partial", __func__,
318 auth_method);
319 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100320 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100321 }
322 }
323
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000324 if (authenticated) {
325 if (!(ent->flags & MON_AUTHDECIDE))
326 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000327 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000328 if (authctxt->pw->pw_uid == 0 &&
329 !auth_root_allowed(auth_method))
330 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000331#ifdef USE_PAM
332 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100333 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000334 Buffer m;
335
336 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100337 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000338 MONITOR_REQ_PAM_ACCOUNT, &m);
339 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
340 buffer_free(&m);
341 }
342#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000343 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100344 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100345 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000346 auth_method, auth_submethod);
djm@openbsd.org94885382015-06-22 23:42:16 +0000347 if (!partial && !authenticated)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348 authctxt->failures++;
349 }
350 }
351
352 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000353 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100354 if (strcmp(auth_method, "unknown") == 0)
355 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356
357 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000358 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000359
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000360 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000361
Damien Miller6a1937e2012-12-12 10:44:38 +1100362 /* Drain any buffered messages from the child */
363 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
364 ;
365
Damien Miller8f0bf232011-06-20 14:42:23 +1000366 close(pmonitor->m_sendfd);
367 close(pmonitor->m_log_recvfd);
368 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000369}
370
Damien Millerbe64d432003-05-14 19:31:12 +1000371static void
372monitor_set_child_handler(pid_t pid)
373{
374 monitor_child_pid = pid;
375}
376
377static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000378monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000379{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000380 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000381}
382
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000383void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000384monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000385{
Damien Miller8f0bf232011-06-20 14:42:23 +1000386 close(pmonitor->m_recvfd);
387 pmonitor->m_recvfd = -1;
388
Damien Millerbe64d432003-05-14 19:31:12 +1000389 monitor_set_child_handler(pmonitor->m_pid);
390 signal(SIGHUP, &monitor_child_handler);
391 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000392 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000393#ifdef SIGXFSZ
394 signal(SIGXFSZ, SIG_IGN);
395#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000396
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000397 mon_dispatch = mon_dispatch_postauth20;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000398
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000399 /* Permit requests for moduli and signatures */
400 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
401 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
402 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
403
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000404 if (!no_pty_flag) {
405 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
406 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
407 }
408
409 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000410 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000411}
412
Damien Miller8f0bf232011-06-20 14:42:23 +1000413static int
414monitor_read_log(struct monitor *pmonitor)
415{
416 Buffer logmsg;
417 u_int len, level;
418 char *msg;
419
420 buffer_init(&logmsg);
421
422 /* Read length */
423 buffer_append_space(&logmsg, 4);
424 if (atomicio(read, pmonitor->m_log_recvfd,
425 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
426 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100427 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000428 debug("%s: child log fd closed", __func__);
429 close(pmonitor->m_log_recvfd);
430 pmonitor->m_log_recvfd = -1;
431 return -1;
432 }
433 fatal("%s: log fd read: %s", __func__, strerror(errno));
434 }
435 len = buffer_get_int(&logmsg);
436 if (len <= 4 || len > 8192)
437 fatal("%s: invalid log message length %u", __func__, len);
438
439 /* Read severity, message */
440 buffer_clear(&logmsg);
441 buffer_append_space(&logmsg, len);
442 if (atomicio(read, pmonitor->m_log_recvfd,
443 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
444 fatal("%s: log fd read: %s", __func__, strerror(errno));
445
446 /* Log it */
447 level = buffer_get_int(&logmsg);
448 msg = buffer_get_string(&logmsg, NULL);
449 if (log_level_name(level) == NULL)
450 fatal("%s: invalid log level %u (corrupted message?)",
451 __func__, level);
452 do_log2(level, "%s [preauth]", msg);
453
454 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000455 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000456
457 return 0;
458}
459
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000460int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000461monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000462 struct mon_table **pent)
463{
464 Buffer m;
465 int ret;
466 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000467 struct pollfd pfd[2];
468
469 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100470 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000471 pfd[0].fd = pmonitor->m_sendfd;
472 pfd[0].events = POLLIN;
473 pfd[1].fd = pmonitor->m_log_recvfd;
474 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000475 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
476 if (errno == EINTR || errno == EAGAIN)
477 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000478 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000479 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000480 if (pfd[1].revents) {
481 /*
482 * Drain all log messages before processing next
483 * monitor request.
484 */
485 monitor_read_log(pmonitor);
486 continue;
487 }
488 if (pfd[0].revents)
489 break; /* Continues below */
490 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000491
492 buffer_init(&m);
493
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000494 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000495 type = buffer_get_char(&m);
496
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000497 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000498
499 while (ent->f != NULL) {
500 if (ent->type == type)
501 break;
502 ent++;
503 }
504
505 if (ent->f != NULL) {
506 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000507 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000508 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000509 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000510 buffer_free(&m);
511
512 /* The child may use this request only once, disable it */
513 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000514 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000515 type);
516 ent->flags &= ~MON_PERMIT;
517 }
518
519 if (pent != NULL)
520 *pent = ent;
521
522 return ret;
523 }
524
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000525 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000526
527 /* NOTREACHED */
528 return (-1);
529}
530
531/* allowed key state */
532static int
533monitor_allowed_key(u_char *blob, u_int bloblen)
534{
535 /* make sure key is allowed */
536 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000537 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000538 return (0);
539 return (1);
540}
541
542static void
543monitor_reset_key_state(void)
544{
545 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000546 free(key_blob);
547 free(hostbased_cuser);
548 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000549 key_blob = NULL;
550 key_bloblen = 0;
551 key_blobtype = MM_NOKEY;
552 hostbased_cuser = NULL;
553 hostbased_chost = NULL;
554}
555
Damien Miller1f0311c2014-05-15 14:24:09 +1000556#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000557int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000558mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000559{
560 DH *dh;
561 int min, want, max;
562
563 min = buffer_get_int(m);
564 want = buffer_get_int(m);
565 max = buffer_get_int(m);
566
567 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000568 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000569 /* We need to check here, too, in case the child got corrupted */
570 if (max < min || want < min || max < want)
571 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000572 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000573
574 buffer_clear(m);
575
576 dh = choose_dh(min, want, max);
577 if (dh == NULL) {
578 buffer_put_char(m, 0);
579 return (0);
580 } else {
581 /* Send first bignum */
582 buffer_put_char(m, 1);
583 buffer_put_bignum2(m, dh->p);
584 buffer_put_bignum2(m, dh->g);
585
586 DH_free(dh);
587 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000588 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000589 return (0);
590}
Damien Miller1f0311c2014-05-15 14:24:09 +1000591#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000592
593int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000594mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000595{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000596 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000597 extern int auth_sock; /* XXX move to state struct? */
598 struct sshkey *key;
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000599 struct sshbuf *sigbuf = NULL;
600 u_char *p = NULL, *signature = NULL;
601 char *alg = NULL;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000602 size_t datlen, siglen, alglen;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000603 int r, is_proof = 0;
604 u_int keyid;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000605 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000606
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000607 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000608
djm@openbsd.org141efe42015-01-14 20:05:27 +0000609 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000610 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
611 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000612 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000613 if (keyid > INT_MAX)
614 fatal("%s: invalid key ID", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000615
Damien Millera63128d2006-03-15 12:08:28 +1100616 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000617 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
618 * SHA384 (48 bytes) and SHA512 (64 bytes).
djm@openbsd.org523463a2015-02-16 22:13:32 +0000619 *
620 * Otherwise, verify the signature request is for a hostkey
621 * proof.
622 *
623 * XXX perform similar check for KEX signature requests too?
624 * it's not trivial, since what is signed is the hash, rather
625 * than the full kex structure...
Damien Millera63128d2006-03-15 12:08:28 +1100626 */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000627 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
628 /*
629 * Construct expected hostkey proof and compare it to what
630 * the client sent us.
631 */
632 if (session_id2_len == 0) /* hostkeys is never first */
633 fatal("%s: bad data length: %zu", __func__, datlen);
634 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
635 fatal("%s: no hostkey for index %d", __func__, keyid);
636 if ((sigbuf = sshbuf_new()) == NULL)
637 fatal("%s: sshbuf_new", __func__);
djm@openbsd.org44732de2015-02-20 22:17:21 +0000638 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
639 (r = sshbuf_put_string(sigbuf, session_id2,
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000640 session_id2_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000641 (r = sshkey_puts(key, sigbuf)) != 0)
642 fatal("%s: couldn't prepare private key "
643 "proof buffer: %s", __func__, ssh_err(r));
644 if (datlen != sshbuf_len(sigbuf) ||
645 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
646 fatal("%s: bad data length: %zu, hostkey proof len %zu",
647 __func__, datlen, sshbuf_len(sigbuf));
648 sshbuf_free(sigbuf);
649 is_proof = 1;
650 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000651
Ben Lindstromf67e0772002-06-06 20:58:19 +0000652 /* save session id, it will be passed on the first call */
653 if (session_id2_len == 0) {
654 session_id2_len = datlen;
655 session_id2 = xmalloc(session_id2_len);
656 memcpy(session_id2, p, session_id2_len);
657 }
658
Damien Miller85b45e02013-07-20 13:21:52 +1000659 if ((key = get_hostkey_by_index(keyid)) != NULL) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000660 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000661 datafellows)) != 0)
662 fatal("%s: sshkey_sign failed: %s",
663 __func__, ssh_err(r));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000664 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000665 auth_sock > 0) {
666 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000667 p, datlen, alg, datafellows)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000668 fatal("%s: ssh_agent_sign failed: %s",
669 __func__, ssh_err(r));
670 }
Damien Miller85b45e02013-07-20 13:21:52 +1000671 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000672 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000673
djm@openbsd.org523463a2015-02-16 22:13:32 +0000674 debug3("%s: %s signature %p(%zu)", __func__,
675 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000676
djm@openbsd.org141efe42015-01-14 20:05:27 +0000677 sshbuf_reset(m);
678 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
679 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000680
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000681 free(alg);
Darren Tuckera627d422013-06-02 07:31:17 +1000682 free(p);
683 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000684
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000685 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000686
687 /* Turn on permissions for getpwnam */
688 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
689
690 return (0);
691}
692
693/* Retrieves the password entry and also checks if the user is permitted */
694
695int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000696mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000697{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000698 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000699 struct passwd *pwent;
700 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000701 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000702
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000703 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000704
705 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000706 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000707
Darren Tuckerb09b6772004-06-22 15:06:46 +1000708 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000709
Darren Tuckerb09b6772004-06-22 15:06:46 +1000710 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000711
Darren Tuckerb09b6772004-06-22 15:06:46 +1000712 authctxt->user = xstrdup(username);
713 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000714 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000715
716 buffer_clear(m);
717
718 if (pwent == NULL) {
719 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100720 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000721 goto out;
722 }
723
724 allowed = 1;
725 authctxt->pw = pwent;
726 authctxt->valid = 1;
727
728 buffer_put_char(m, 1);
729 buffer_put_string(m, pwent, sizeof(struct passwd));
730 buffer_put_cstring(m, pwent->pw_name);
731 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000732#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000733 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000734#endif
735#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000736 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000737#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000738 buffer_put_cstring(m, pwent->pw_dir);
739 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100740
741 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100742 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000743
744#define M_CP_STROPT(x) do { \
745 if (options.x != NULL) \
746 buffer_put_cstring(m, options.x); \
747 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000748#define M_CP_STRARRAYOPT(x, nx) do { \
749 for (i = 0; i < options.nx; i++) \
750 buffer_put_cstring(m, options.x[i]); \
751 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000752 /* See comment in servconf.h */
753 COPY_MATCH_STRING_OPTS();
754#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000755#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100756
757 /* Create valid auth method lists */
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000758 if (auth2_setup_methods_lists(authctxt) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +1100759 /*
760 * The monitor will continue long enough to let the child
761 * run to it's packet_disconnect(), but it must not allow any
762 * authentication to succeed.
763 */
764 debug("%s: no valid authentication method lists", __func__);
765 }
766
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000767 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000768 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000769
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000770 /* Allow service/style information on the auth context */
771 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
772 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
773
Damien Millera33501b2002-05-08 12:24:42 +1000774#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000775 if (options.use_pam)
776 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000777#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000778
779 return (0);
780}
781
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000782int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000783{
784 char *banner;
785
786 buffer_clear(m);
787 banner = auth2_read_banner();
788 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000789 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000790 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000791
792 return (0);
793}
794
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000795int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000796mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000797{
798 monitor_permit_authentications(1);
799
800 authctxt->service = buffer_get_string(m, NULL);
801 authctxt->style = buffer_get_string(m, NULL);
802 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000803 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000804
805 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000806 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000807 authctxt->style = NULL;
808 }
809
810 return (0);
811}
812
813int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000814mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000815{
816 static int call_count;
817 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000818 int authenticated;
819 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000820
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000821 if (!options.password_authentication)
822 fatal("%s: password authentication not enabled", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000823 passwd = buffer_get_string(m, &plen);
824 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000825 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000826 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100827 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000828 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000829
830 buffer_clear(m);
831 buffer_put_int(m, authenticated);
Darren Tucker01558b72016-07-18 09:33:25 +1000832#ifdef USE_PAM
833 buffer_put_int(m, sshpam_get_maxtries_reached());
834#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000835
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000836 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000837 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000838
839 call_count++;
840 if (plen == 0 && call_count == 1)
841 auth_method = "none";
842 else
843 auth_method = "password";
844
845 /* Causes monitor loop to terminate if authenticated */
846 return (authenticated);
847}
848
849#ifdef BSD_AUTH
850int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000851mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000852{
853 char *name, *infotxt;
854 u_int numprompts;
855 u_int *echo_on;
856 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100857 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000858
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000859 if (!options.kbd_interactive_authentication)
860 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb7df3af2003-02-24 11:55:46 +1100861 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
862 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000863
864 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100865 buffer_put_int(m, success);
866 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000867 buffer_put_cstring(m, prompts[0]);
868
Damien Millerb7df3af2003-02-24 11:55:46 +1100869 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000870 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000871
Damien Millerb7df3af2003-02-24 11:55:46 +1100872 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000873 free(name);
874 free(infotxt);
875 free(prompts);
876 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000877 }
878
879 return (0);
880}
881
882int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000883mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000884{
885 char *response;
886 int authok;
887
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +0000888 if (!options.kbd_interactive_authentication)
889 fatal("%s: kbd-int authentication not enabled", __func__);
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +0000890 if (authctxt->as == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000891 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000892
893 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000894 authok = options.challenge_response_authentication &&
895 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000896 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000897 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000898 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000899
900 buffer_clear(m);
901 buffer_put_int(m, authok);
902
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000903 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000904 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000905
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000906 auth_method = "keyboard-interactive";
907 auth_submethod = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000908
909 return (authok != 0);
910}
911#endif
912
913#ifdef SKEY
914int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000915mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000916{
917 struct skey skey;
918 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100919 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000920
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000921 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
922 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000923
924 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100925 buffer_put_int(m, success);
926 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000927 buffer_put_cstring(m, challenge);
928
Damien Millerb7df3af2003-02-24 11:55:46 +1100929 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000930 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000931
932 return (0);
933}
934
935int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000936mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000937{
938 char *response;
939 int authok;
940
941 response = buffer_get_string(m, NULL);
942
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000943 authok = (options.challenge_response_authentication &&
944 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000945 skey_haskey(authctxt->pw->pw_name) == 0 &&
946 skey_passcheck(authctxt->pw->pw_name, response) != -1);
947
Darren Tuckerf60845f2013-06-02 08:07:31 +1000948 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000949
950 buffer_clear(m);
951 buffer_put_int(m, authok);
952
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000953 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000954 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000955
Damien Millerac947352015-11-20 12:35:41 +1100956 auth_method = "keyboard-interactive";
957 auth_submethod = "skey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000958
959 return (authok != 0);
960}
961#endif
962
Damien Miller79418552002-04-23 20:28:48 +1000963#ifdef USE_PAM
964int
Darren Tucker7eda5922004-06-22 13:22:50 +1000965mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000966{
Damien Miller4e448a32003-05-14 15:11:48 +1000967 if (!options.use_pam)
968 fatal("UsePAM not set, but ended up in %s anyway", __func__);
969
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100970 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000971
Damien Miller1f499fd2003-08-25 13:08:49 +1000972 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
Damien Miller775f8a22016-08-31 10:48:07 +1000973 if (options.kbd_interactive_authentication)
974 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller1f499fd2003-08-25 13:08:49 +1000975
Damien Miller79418552002-04-23 20:28:48 +1000976 return (0);
977}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000978
Damien Miller1f499fd2003-08-25 13:08:49 +1000979int
Darren Tucker7eda5922004-06-22 13:22:50 +1000980mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000981{
982 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100983
Damien Miller1f499fd2003-08-25 13:08:49 +1000984 if (!options.use_pam)
Damien Miller775f8a22016-08-31 10:48:07 +1000985 fatal("%s: PAM not enabled", __func__);
Damien Miller1f499fd2003-08-25 13:08:49 +1000986
987 ret = do_pam_account();
988
989 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000990 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000991
Darren Tucker7eda5922004-06-22 13:22:50 +1000992 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000993
994 return (ret);
995}
996
Damien Miller4f9f42a2003-05-10 19:28:02 +1000997static void *sshpam_ctxt, *sshpam_authok;
998extern KbdintDevice sshpam_device;
999
1000int
Darren Tucker7eda5922004-06-22 13:22:50 +10001001mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001002{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001003 debug3("%s", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001004 if (!options.kbd_interactive_authentication)
1005 fatal("%s: kbd-int authentication not enabled", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001006 if (sshpam_ctxt != NULL)
1007 fatal("%s: already called", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001008 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1009 sshpam_authok = NULL;
1010 buffer_clear(m);
1011 if (sshpam_ctxt != NULL) {
1012 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
Damien Millerb38b95f2016-08-29 11:47:07 +10001013 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001014 buffer_put_int(m, 1);
1015 } else {
1016 buffer_put_int(m, 0);
1017 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001018 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001019 return (0);
1020}
1021
1022int
Darren Tucker7eda5922004-06-22 13:22:50 +10001023mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001024{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001025 char *name = NULL, *info = NULL, **prompts = NULL;
1026 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001027 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001028
1029 debug3("%s", __func__);
1030 sshpam_authok = NULL;
Damien Millerb38b95f2016-08-29 11:47:07 +10001031 if (sshpam_ctxt == NULL)
1032 fatal("%s: no context", __func__);
Damien Miller775f8a22016-08-31 10:48:07 +10001033 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1034 &num, &prompts, &echo_on);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001035 if (ret == 0 && num == 0)
1036 sshpam_authok = sshpam_ctxt;
1037 if (num > 1 || name == NULL || info == NULL)
Damien Millerb38b95f2016-08-29 11:47:07 +10001038 fatal("sshpam_device.query failed");
1039 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001040 buffer_clear(m);
1041 buffer_put_int(m, ret);
1042 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001043 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001044 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001045 free(info);
Darren Tucker01558b72016-07-18 09:33:25 +10001046 buffer_put_int(m, sshpam_get_maxtries_reached());
Damien Miller4f9f42a2003-05-10 19:28:02 +10001047 buffer_put_int(m, num);
1048 for (i = 0; i < num; ++i) {
1049 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001050 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001051 buffer_put_int(m, echo_on[i]);
1052 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001053 free(prompts);
1054 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001055 auth_method = "keyboard-interactive";
1056 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001057 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001058 return (0);
1059}
1060
1061int
Darren Tucker7eda5922004-06-22 13:22:50 +10001062mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001063{
1064 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001065 u_int i, num;
1066 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001067
1068 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001069 if (sshpam_ctxt == NULL)
1070 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001071 sshpam_authok = NULL;
1072 num = buffer_get_int(m);
1073 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001074 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001075 for (i = 0; i < num; ++i)
1076 resp[i] = buffer_get_string(m, NULL);
1077 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1078 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001079 free(resp[i]);
1080 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001081 } else {
1082 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1083 }
1084 buffer_clear(m);
1085 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001086 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001087 auth_method = "keyboard-interactive";
1088 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001089 if (ret == 0)
1090 sshpam_authok = sshpam_ctxt;
1091 return (0);
1092}
1093
1094int
Darren Tucker7eda5922004-06-22 13:22:50 +10001095mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001096{
Damien Miller5e75f512015-08-11 13:34:12 +10001097 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001098
1099 debug3("%s", __func__);
Damien Millerb38b95f2016-08-29 11:47:07 +10001100 if (sshpam_ctxt == NULL)
1101 fatal("%s: no context", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001102 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001103 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001104 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001105 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Millerb38b95f2016-08-29 11:47:07 +10001106 /* Allow another attempt */
1107 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
Damien Miller15b05cf2012-12-03 09:53:20 +11001108 auth_method = "keyboard-interactive";
1109 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001110 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001111}
Damien Miller79418552002-04-23 20:28:48 +10001112#endif
1113
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001114int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001115mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001116{
1117 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001118 char *cuser, *chost;
1119 u_char *blob;
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001120 u_int bloblen, pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001121 enum mm_keytype type = 0;
1122 int allowed = 0;
1123
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001124 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001125
1126 type = buffer_get_int(m);
1127 cuser = buffer_get_string(m, NULL);
1128 chost = buffer_get_string(m, NULL);
1129 blob = buffer_get_string(m, &bloblen);
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001130 pubkey_auth_attempt = buffer_get_int(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001131
1132 key = key_from_blob(blob, bloblen);
1133
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001134 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001135
Damien Miller3e3b5142003-11-17 21:13:40 +11001136 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001137 /* These should not make it past the privsep child */
1138 if (key_type_plain(key->type) == KEY_RSA &&
1139 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1140 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1141
Darren Tucker47eede72005-03-14 23:08:12 +11001142 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001143 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001144 allowed = options.pubkey_authentication &&
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001145 !auth2_userkey_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001146 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001147 options.pubkey_key_types, 0) == 1 &&
1148 user_key_allowed(authctxt->pw, key,
1149 pubkey_auth_attempt);
Damien Miller20bdcd72013-07-18 16:10:09 +10001150 pubkey_auth_info(authctxt, key, NULL);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001151 auth_method = "publickey";
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001152 if (options.pubkey_authentication &&
1153 (!pubkey_auth_attempt || allowed != 1))
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001154 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001155 break;
1156 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001157 allowed = options.hostbased_authentication &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001158 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001159 options.hostbased_key_types, 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001160 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001161 cuser, chost, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001162 pubkey_auth_info(authctxt, key,
1163 "client user \"%.100s\", client host \"%.100s\"",
1164 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001165 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001166 break;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001167 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001168 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001169 break;
1170 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001171 }
djm@openbsd.orgc36d91b2016-07-22 03:39:13 +00001172
1173 debug3("%s: key %p is %s",
1174 __func__, key, allowed ? "allowed" : "not allowed");
1175
Damien Miller00111382003-03-10 11:21:17 +11001176 if (key != NULL)
1177 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001178
1179 /* clear temporarily storage (used by verify) */
1180 monitor_reset_key_state();
1181
1182 if (allowed) {
1183 /* Save temporarily for comparison in verify */
1184 key_blob = blob;
1185 key_bloblen = bloblen;
1186 key_blobtype = type;
1187 hostbased_cuser = cuser;
1188 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001189 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001190 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001191 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001192 free(blob);
1193 free(cuser);
1194 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001195 }
1196
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001197 buffer_clear(m);
1198 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001199 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001200
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001201 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001203 return (0);
1204}
1205
1206static int
1207monitor_valid_userblob(u_char *data, u_int datalen)
1208{
1209 Buffer b;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001210 u_char *p;
1211 char *userstyle, *cp;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001212 u_int len;
1213 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001214
1215 buffer_init(&b);
1216 buffer_append(&b, data, datalen);
1217
1218 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001219 p = buffer_ptr(&b);
1220 len = buffer_len(&b);
1221 if ((session_id2 == NULL) ||
1222 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001223 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001224 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001225 buffer_consume(&b, session_id2_len);
1226 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001227 p = buffer_get_string(&b, &len);
1228 if ((session_id2 == NULL) ||
1229 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001230 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001231 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001232 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001233 }
1234 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1235 fail++;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001236 cp = buffer_get_cstring(&b, NULL);
Damien Miller4ce189d2013-04-23 15:17:52 +10001237 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1238 authctxt->style ? ":" : "",
1239 authctxt->style ? authctxt->style : "");
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001240 if (strcmp(userstyle, cp) != 0) {
1241 logit("wrong user name passed to monitor: "
1242 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001243 fail++;
1244 }
Darren Tuckera627d422013-06-02 07:31:17 +10001245 free(userstyle);
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001246 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001247 buffer_skip_string(&b);
1248 if (datafellows & SSH_BUG_PKAUTH) {
1249 if (!buffer_get_char(&b))
1250 fail++;
1251 } else {
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001252 cp = buffer_get_cstring(&b, NULL);
1253 if (strcmp("publickey", cp) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001254 fail++;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001255 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001256 if (!buffer_get_char(&b))
1257 fail++;
1258 buffer_skip_string(&b);
1259 }
1260 buffer_skip_string(&b);
1261 if (buffer_len(&b) != 0)
1262 fail++;
1263 buffer_free(&b);
1264 return (fail == 0);
1265}
1266
1267static int
Damien Millera10f5612002-09-12 09:49:15 +10001268monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1269 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001270{
1271 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001272 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001273 u_int len;
1274 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001275
1276 buffer_init(&b);
1277 buffer_append(&b, data, datalen);
1278
Ben Lindstromf67e0772002-06-06 20:58:19 +00001279 p = buffer_get_string(&b, &len);
1280 if ((session_id2 == NULL) ||
1281 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001282 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001283 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001284 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001285
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001286 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1287 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001288 p = buffer_get_cstring(&b, NULL);
1289 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1290 authctxt->style ? ":" : "",
1291 authctxt->style ? authctxt->style : "");
1292 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001293 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001294 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295 fail++;
1296 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001297 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001298 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001299 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001300 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001301 if (strcmp(p, "hostbased") != 0)
1302 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001303 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001304 buffer_skip_string(&b); /* pkalg */
1305 buffer_skip_string(&b); /* pkblob */
1306
1307 /* verify client host, strip trailing dot if necessary */
1308 p = buffer_get_string(&b, NULL);
1309 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1310 p[len - 1] = '\0';
1311 if (strcmp(p, chost) != 0)
1312 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001313 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001314
1315 /* verify client user */
1316 p = buffer_get_string(&b, NULL);
1317 if (strcmp(p, cuser) != 0)
1318 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001319 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001320
1321 if (buffer_len(&b) != 0)
1322 fail++;
1323 buffer_free(&b);
1324 return (fail == 0);
1325}
1326
1327int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001328mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001329{
1330 Key *key;
1331 u_char *signature, *data, *blob;
1332 u_int signaturelen, datalen, bloblen;
1333 int verified = 0;
1334 int valid_data = 0;
1335
1336 blob = buffer_get_string(m, &bloblen);
1337 signature = buffer_get_string(m, &signaturelen);
1338 data = buffer_get_string(m, &datalen);
1339
1340 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001341 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001342 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001343
1344 key = key_from_blob(blob, bloblen);
1345 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001346 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001347
1348 switch (key_blobtype) {
1349 case MM_USERKEY:
1350 valid_data = monitor_valid_userblob(data, datalen);
1351 break;
1352 case MM_HOSTKEY:
1353 valid_data = monitor_valid_hostbasedblob(data, datalen,
1354 hostbased_cuser, hostbased_chost);
1355 break;
1356 default:
1357 valid_data = 0;
1358 break;
1359 }
1360 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001361 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001362
1363 verified = key_verify(key, signature, signaturelen, data, datalen);
1364 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001365 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001366
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001367 /* If auth was successful then record key to ensure it isn't reused */
djm@openbsd.orgf54d8ac2015-09-04 04:44:08 +00001368 if (verified == 1 && key_blobtype == MM_USERKEY)
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001369 auth2_record_userkey(authctxt, key);
1370 else
1371 key_free(key);
1372
Darren Tuckera627d422013-06-02 07:31:17 +10001373 free(blob);
1374 free(signature);
1375 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001376
Ben Lindstrome1c09122002-06-23 00:38:24 +00001377 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1378
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001379 monitor_reset_key_state();
1380
1381 buffer_clear(m);
1382 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001383 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001384
Darren Tuckerfbba7352006-11-07 23:16:08 +11001385 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001386}
1387
1388static void
1389mm_record_login(Session *s, struct passwd *pw)
1390{
djm@openbsd.org95767262016-03-07 19:02:43 +00001391 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001392 socklen_t fromlen;
1393 struct sockaddr_storage from;
1394
1395 /*
1396 * Get IP address of client. If the connection is not a socket, let
1397 * the address be 0.0.0.0.
1398 */
1399 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001400 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001401 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001402 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001403 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001404 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001405 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001406 }
1407 }
1408 /* Record that there was a login on that tty from the remote host. */
1409 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +00001410 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001411 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001412}
1413
1414static void
1415mm_session_close(Session *s)
1416{
Damien Miller04bd8b02003-05-25 14:38:33 +10001417 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001418 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001419 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001420 session_pty_cleanup2(s);
1421 }
Damien Miller7207f642008-05-19 15:34:50 +10001422 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001423}
1424
1425int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001426mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001427{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001428 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001429 Session *s;
1430 int res, fd0;
1431
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001432 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001433
1434 buffer_clear(m);
1435 s = session_new();
1436 if (s == NULL)
1437 goto error;
1438 s->authctxt = authctxt;
1439 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001440 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001441 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1442 if (res == 0)
1443 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001444 pty_setowner(authctxt->pw, s->tty);
1445
1446 buffer_put_int(m, 1);
1447 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001448
1449 /* We need to trick ttyslot */
1450 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001451 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001452
1453 mm_record_login(s, authctxt->pw);
1454
1455 /* Now we can close the file descriptor again */
1456 close(0);
1457
Darren Tucker09991742004-07-17 17:05:14 +10001458 /* send messages generated by record_login */
1459 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1460 buffer_clear(&loginmsg);
1461
1462 mm_request_send(sock, MONITOR_ANS_PTY, m);
1463
Damien Miller54fd7cf2007-09-17 12:04:08 +10001464 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1465 mm_send_fd(sock, s->ttyfd) == -1)
1466 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001467
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001468 /* make sure nothing uses fd 0 */
1469 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001470 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001471 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001472 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001473
1474 /* slave is not needed */
1475 close(s->ttyfd);
1476 s->ttyfd = s->ptyfd;
1477 /* no need to dup() because nobody closes ptyfd */
1478 s->ptymaster = s->ptyfd;
1479
Damien Miller9ab00b42006-08-05 12:40:11 +10001480 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001481
1482 return (0);
1483
1484 error:
1485 if (s != NULL)
1486 mm_session_close(s);
1487 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001488 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001489 return (0);
1490}
1491
1492int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001493mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001494{
1495 Session *s;
1496 char *tty;
1497
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001498 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001499
1500 tty = buffer_get_string(m, NULL);
1501 if ((s = session_by_tty(tty)) != NULL)
1502 mm_session_close(s);
1503 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001504 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001505 return (0);
1506}
1507
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001508int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001509mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001510{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001511 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001512 int res, status;
1513
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001514 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001515
1516 /* The child is terminating */
1517 session_destroy_all(&mm_session_close);
1518
Darren Tucker52358d62008-03-11 22:58:25 +11001519#ifdef USE_PAM
1520 if (options.use_pam)
1521 sshpam_cleanup();
1522#endif
1523
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001524 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001525 if (errno != EINTR)
1526 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001527
1528 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1529
1530 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001531 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532}
1533
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001534#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001535/* Report that an audit event occurred */
1536int
1537mm_answer_audit_event(int socket, Buffer *m)
1538{
1539 ssh_audit_event_t event;
1540
1541 debug3("%s entering", __func__);
1542
1543 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001544 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001545 case SSH_AUTH_FAIL_PUBKEY:
1546 case SSH_AUTH_FAIL_HOSTBASED:
1547 case SSH_AUTH_FAIL_GSSAPI:
1548 case SSH_LOGIN_EXCEED_MAXTRIES:
1549 case SSH_LOGIN_ROOT_DENIED:
1550 case SSH_CONNECTION_CLOSE:
1551 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001552 audit_event(event);
1553 break;
1554 default:
1555 fatal("Audit event type %d not permitted", event);
1556 }
1557
1558 return (0);
1559}
1560
1561int
1562mm_answer_audit_command(int socket, Buffer *m)
1563{
1564 u_int len;
1565 char *cmd;
1566
1567 debug3("%s entering", __func__);
1568 cmd = buffer_get_string(m, &len);
1569 /* sanity check command, if so how? */
1570 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001571 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001572 return (0);
1573}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001574#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001575
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001576void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001577monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001578{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001579 struct ssh *ssh = active_state; /* XXX */
1580 struct kex *kex;
1581 int r;
1582
1583 debug3("%s: packet_set_state", __func__);
1584 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1585 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1586 sshbuf_free(child_state);
1587 child_state = NULL;
1588
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +00001589 if ((kex = ssh->kex) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001590 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001591#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001592 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1593 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +00001594 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1595 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1596 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001597 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1598 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001599# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001600 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001601# endif
Damien Miller773dda22015-01-30 23:10:17 +11001602#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001603 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1604 kex->load_host_public_key=&get_hostkey_public_by_type;
1605 kex->load_host_private_key=&get_hostkey_private_by_type;
1606 kex->host_key_index=&get_hostkey_index;
1607 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001608 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001609}
1610
1611/* This function requries careful sanity checking */
1612
1613void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001614mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001615{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001616 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001617
markus@openbsd.org091c3022015-01-19 19:52:16 +00001618 if ((child_state = sshbuf_new()) == NULL)
1619 fatal("%s: sshbuf_new failed", __func__);
1620 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1621 child_state);
1622 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001623}
1624
1625
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001626/* XXX */
1627
1628#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001629 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001630 fatal("fcntl(%d, F_SETFD)", x); \
1631} while (0)
1632
1633static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001634monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001635{
Damien Miller8f0bf232011-06-20 14:42:23 +10001636 int pair[2];
1637
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001638 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001639 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001640 FD_CLOSEONEXEC(pair[0]);
1641 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001642 mon->m_recvfd = pair[0];
1643 mon->m_sendfd = pair[1];
1644
1645 if (do_logfds) {
1646 if (pipe(pair) == -1)
1647 fatal("%s: pipe: %s", __func__, strerror(errno));
1648 FD_CLOSEONEXEC(pair[0]);
1649 FD_CLOSEONEXEC(pair[1]);
1650 mon->m_log_recvfd = pair[0];
1651 mon->m_log_sendfd = pair[1];
1652 } else
1653 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001654}
1655
1656#define MM_MEMSIZE 65536
1657
1658struct monitor *
1659monitor_init(void)
1660{
1661 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001662
Damien Miller07d86be2006-03-26 14:19:21 +11001663 mon = xcalloc(1, sizeof(*mon));
Damien Miller8f0bf232011-06-20 14:42:23 +10001664 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001665
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001666 return mon;
1667}
1668
1669void
1670monitor_reinit(struct monitor *mon)
1671{
Damien Miller8f0bf232011-06-20 14:42:23 +10001672 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001673}
Darren Tucker0efd1552003-08-26 11:49:55 +10001674
1675#ifdef GSSAPI
1676int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001677mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001678{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001679 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001680 OM_uint32 major;
1681 u_int len;
1682
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001683 if (!options.gss_authentication)
1684 fatal("%s: GSSAPI authentication not enabled", __func__);
1685
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001686 goid.elements = buffer_get_string(m, &len);
1687 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001688
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001689 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001690
Darren Tuckera627d422013-06-02 07:31:17 +10001691 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001692
1693 buffer_clear(m);
1694 buffer_put_int(m, major);
1695
Damien Miller6fd6def2005-11-05 15:07:05 +11001696 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001697
1698 /* Now we have a context, enable the step */
1699 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1700
1701 return (0);
1702}
1703
1704int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001705mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001706{
1707 gss_buffer_desc in;
1708 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001709 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001710 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001711 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001712
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001713 if (!options.gss_authentication)
1714 fatal("%s: GSSAPI authentication not enabled", __func__);
1715
Darren Tucker600ad8d2003-08-26 12:10:48 +10001716 in.value = buffer_get_string(m, &len);
1717 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001718 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001719 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001720
1721 buffer_clear(m);
1722 buffer_put_int(m, major);
1723 buffer_put_string(m, out.value, out.length);
1724 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001725 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001726
1727 gss_release_buffer(&minor, &out);
1728
Damien Miller6fd6def2005-11-05 15:07:05 +11001729 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001730 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1731 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001732 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001733 }
1734 return (0);
1735}
1736
1737int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001738mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001739{
1740 gss_buffer_desc gssbuf, mic;
1741 OM_uint32 ret;
1742 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001743
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001744 if (!options.gss_authentication)
1745 fatal("%s: GSSAPI authentication not enabled", __func__);
1746
Damien Miller0425d402003-11-17 22:18:21 +11001747 gssbuf.value = buffer_get_string(m, &len);
1748 gssbuf.length = len;
1749 mic.value = buffer_get_string(m, &len);
1750 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001751
Damien Miller0425d402003-11-17 22:18:21 +11001752 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001753
Darren Tuckera627d422013-06-02 07:31:17 +10001754 free(gssbuf.value);
1755 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001756
Damien Miller0425d402003-11-17 22:18:21 +11001757 buffer_clear(m);
1758 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001759
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001760 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001761
Damien Miller0425d402003-11-17 22:18:21 +11001762 if (!GSS_ERROR(ret))
1763 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001764
Damien Miller0425d402003-11-17 22:18:21 +11001765 return (0);
1766}
1767
1768int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001769mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001770{
1771 int authenticated;
1772
djm@openbsd.org7fd0ea82016-08-30 07:50:21 +00001773 if (!options.gss_authentication)
1774 fatal("%s: GSSAPI authentication not enabled", __func__);
1775
Darren Tucker0efd1552003-08-26 11:49:55 +10001776 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1777
1778 buffer_clear(m);
1779 buffer_put_int(m, authenticated);
1780
1781 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001782 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001783
Damien Miller6fd6def2005-11-05 15:07:05 +11001784 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001785
1786 /* Monitor loop will terminate if authenticated */
1787 return (authenticated);
1788}
1789#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001790