blob: 9d193c9eb5ff8304fca86e4efbd416d1c80df146 [file] [log] [blame]
dtucker@openbsd.orgfc173ae2019-11-13 11:25:11 +00001/* $OpenBSD: sshd.c,v 1.540 2019/11/13 11:25:11 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11006 * This program is the ssh daemon. It listens for connections from clients,
7 * and performs authentication, executes use commands or shell, and forwards
Damien Miller95def091999-11-25 00:26:21 +11008 * information to/from the application to the user client over an encrypted
Damien Millere4340be2000-09-16 13:29:08 +11009 * connection. This can also handle forwarding of X11, TCP/IP, and
10 * authentication agent connections.
Damien Millerefb4afe2000-04-12 18:45:05 +100011 *
Damien Millere4340be2000-09-16 13:29:08 +110012 * As far as I am concerned, the code I have written for this software
13 * can be used freely for any purpose. Any derived versions of this
14 * software must be clearly marked as such, and if the derived work is
15 * incompatible with the protocol description in the RFC file, it must be
16 * called by a name other than "ssh" or "Secure Shell".
17 *
18 * SSH2 implementation:
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000019 * Privilege Separation:
Damien Millere4340be2000-09-16 13:29:08 +110020 *
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000021 * Copyright (c) 2000, 2001, 2002 Markus Friedl. All rights reserved.
22 * Copyright (c) 2002 Niels Provos. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110023 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
34 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
36 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
37 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
42 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110043 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110046
Damien Miller9cf6d072006-03-15 11:29:24 +110047#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100048#include <sys/ioctl.h>
49#include <sys/socket.h>
Damien Millerf17883e2006-03-15 11:45:54 +110050#ifdef HAVE_SYS_STAT_H
51# include <sys/stat.h>
52#endif
Damien Miller9aec9192006-08-05 10:57:45 +100053#ifdef HAVE_SYS_TIME_H
54# include <sys/time.h>
55#endif
Damien Millerd7834352006-08-05 12:39:39 +100056#include "openbsd-compat/sys-tree.h"
Damien Millerb84886b2008-05-19 15:05:07 +100057#include "openbsd-compat/sys-queue.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110058#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110059
Darren Tucker39972492006-07-12 22:22:46 +100060#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100061#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100062#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110063#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110064#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110065#endif
Damien Millera1738e42006-07-10 21:33:04 +100066#include <grp.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100067#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110068#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100069#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100070#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100071#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100072#include <string.h>
Damien Miller75bb6642006-08-05 14:07:20 +100073#include <unistd.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000074#include <limits.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Damien Miller1f0311c2014-05-15 14:24:09 +100076#ifdef WITH_OPENSSL
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include <openssl/dh.h>
78#include <openssl/bn.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000079#include <openssl/rand.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110080#include "openbsd-compat/openssl-compat.h"
Damien Miller1f0311c2014-05-15 14:24:09 +100081#endif
Darren Tuckerbfaaf962008-02-28 19:13:52 +110082
Kevin Steves0ea1d9d2002-04-25 18:17:04 +000083#ifdef HAVE_SECUREWARE
84#include <sys/security.h>
85#include <prot.h>
86#endif
Ben Lindstrom226cfa02001-01-22 05:34:40 +000087
Damien Millerd7834352006-08-05 12:39:39 +100088#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000089#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000090#include "ssh2.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000091#include "sshpty.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000093#include "log.h"
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +000094#include "sshbuf.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100095#include "misc.h"
markus@openbsd.org3a1638d2015-07-10 06:21:53 +000096#include "match.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097#include "servconf.h"
98#include "uidswap.h"
99#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000100#include "cipher.h"
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100101#include "digest.h"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000102#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +1000103#include "kex.h"
Damien Millerefb4afe2000-04-12 18:45:05 +1000104#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +1000105#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000106#include "pathnames.h"
107#include "atomicio.h"
108#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +1000109#include "hostfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000110#include "auth.h"
Damien Miller85b45e02013-07-20 13:21:52 +1000111#include "authfd.h"
Darren Tucker645ab752004-06-25 13:33:20 +1000112#include "msg.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000113#include "dispatch.h"
Ben Lindstrom1bae4042001-10-03 17:46:39 +0000114#include "channels.h"
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +0000115#include "session.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000116#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +1000117#ifdef GSSAPI
118#include "ssh-gss.h"
119#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000120#include "monitor_wrap.h"
Damien Millerdcbd41e2011-06-23 19:45:51 +1000121#include "ssh-sandbox.h"
djm@openbsd.org7c856852018-03-03 03:15:51 +0000122#include "auth-options.h"
Damien Millerb7576772006-07-10 20:23:39 +1000123#include "version.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000124#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
Damien Miller035a5b42004-06-26 08:16:31 +1000126/* Re-exec fds */
127#define REEXEC_DEVCRYPTO_RESERVED_FD (STDERR_FILENO + 1)
128#define REEXEC_STARTUP_PIPE_FD (STDERR_FILENO + 2)
129#define REEXEC_CONFIG_PASS_FD (STDERR_FILENO + 3)
130#define REEXEC_MIN_FREE_FD (STDERR_FILENO + 4)
131
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000132extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000133
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134/* Server configuration options. */
135ServerOptions options;
136
137/* Name of the server configuration file. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000138char *config_file_name = _PATH_SERVER_CONFIG_FILE;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Miller4af51302000-04-16 11:18:38 +1000140/*
Damien Miller95def091999-11-25 00:26:21 +1100141 * Debug mode flag. This can be set on the command line. If debug
142 * mode is enabled, extra debugging output will be sent to the system
143 * log, the daemon will not go to background, and will exit after processing
144 * the first connection.
145 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146int debug_flag = 0;
147
djm@openbsd.org@openbsd.org548d3a62017-11-14 00:45:29 +0000148/*
149 * Indicating that the daemon should only test the configuration and keys.
150 * If test_flag > 1 ("-T" flag), then sshd will also dump the effective
151 * configuration, optionally using connection information provided by the
152 * "-C" flag.
153 */
djm@openbsd.orgdbb4dec2019-01-17 01:50:24 +0000154static int test_flag = 0;
Ben Lindstrom794325a2001-08-06 21:09:07 +0000155
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156/* Flag indicating that the daemon is being started from inetd. */
djm@openbsd.orgdbb4dec2019-01-17 01:50:24 +0000157static int inetd_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158
Ben Lindstromc72745a2000-12-02 19:03:54 +0000159/* Flag indicating that sshd should not detach and become a daemon. */
djm@openbsd.orgdbb4dec2019-01-17 01:50:24 +0000160static int no_daemon_flag = 0;
Ben Lindstromc72745a2000-12-02 19:03:54 +0000161
Damien Miller5ce662a1999-11-11 17:57:39 +1100162/* debug goes to stderr unless inetd_flag is set */
djm@openbsd.orgdbb4dec2019-01-17 01:50:24 +0000163static int log_stderr = 0;
Damien Miller5ce662a1999-11-11 17:57:39 +1100164
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165/* Saved arguments to main(). */
djm@openbsd.orgdbb4dec2019-01-17 01:50:24 +0000166static char **saved_argv;
167static int saved_argc;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168
Darren Tucker645ab752004-06-25 13:33:20 +1000169/* re-exec */
djm@openbsd.orgdbb4dec2019-01-17 01:50:24 +0000170static int rexeced_flag = 0;
171static int rexec_flag = 1;
172static int rexec_argc = 0;
173static char **rexec_argv;
Darren Tucker645ab752004-06-25 13:33:20 +1000174
Damien Miller5428f641999-11-25 11:54:57 +1100175/*
Damien Miller34132e52000-01-14 15:45:46 +1100176 * The sockets that the server is listening; this is used in the SIGHUP
177 * signal handler.
Damien Miller5428f641999-11-25 11:54:57 +1100178 */
Damien Miller34132e52000-01-14 15:45:46 +1100179#define MAX_LISTEN_SOCKS 16
djm@openbsd.orgdbb4dec2019-01-17 01:50:24 +0000180static int listen_socks[MAX_LISTEN_SOCKS];
181static int num_listen_socks = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182
Damien Miller85b45e02013-07-20 13:21:52 +1000183/* Daemon's agent connection */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000184int auth_sock = -1;
djm@openbsd.orgdbb4dec2019-01-17 01:50:24 +0000185static int have_agent = 0;
Damien Miller85b45e02013-07-20 13:21:52 +1000186
Damien Miller5428f641999-11-25 11:54:57 +1100187/*
188 * Any really sensitive data in the application is contained in this
189 * structure. The idea is that this structure could be locked into memory so
190 * that the pages do not get written into swap. However, there are some
191 * problems. The private key contains BIGNUMs, and we do not (in principle)
192 * have access to the internals of them, and locking just the structure is
193 * not very useful. Currently, memory locking is not implemented.
194 */
Damien Miller95def091999-11-25 00:26:21 +1100195struct {
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000196 struct sshkey **host_keys; /* all private host keys */
197 struct sshkey **host_pubkeys; /* all public host keys */
198 struct sshkey **host_certificates; /* all public host certificates */
199 int have_ssh2_key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200} sensitive_data;
201
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000202/* This is set to true when a signal is received. */
Ben Lindstrom5e71c542001-12-06 16:48:14 +0000203static volatile sig_atomic_t received_sighup = 0;
204static volatile sig_atomic_t received_sigterm = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205
Damien Millerb38eff82000-04-01 11:09:21 +1000206/* session identifier, used by RSA-auth */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000207u_char session_id[16];
Damien Millerb38eff82000-04-01 11:09:21 +1000208
Damien Millereba71ba2000-04-29 23:57:08 +1000209/* same for ssh2 */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000210u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000211u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000212
Damien Miller942da032000-08-18 13:59:06 +1000213/* record remote hostname or ip */
deraadt@openbsd.org087266e2015-01-20 23:14:00 +0000214u_int utmp_len = HOST_NAME_MAX+1;
Damien Miller942da032000-08-18 13:59:06 +1000215
djm@openbsd.org76a24b32019-03-01 02:32:39 +0000216/*
217 * startup_pipes/flags are used for tracking children of the listening sshd
218 * process early in their lifespans. This tracking is needed for three things:
219 *
220 * 1) Implementing the MaxStartups limit of concurrent unauthenticated
221 * connections.
222 * 2) Avoiding a race condition for SIGHUP processing, where child processes
223 * may have listen_socks open that could collide with main listener process
224 * after it restarts.
225 * 3) Ensuring that rexec'd sshd processes have received their initial state
226 * from the parent listen process before handling SIGHUP.
227 *
228 * Child processes signal that they have completed closure of the listen_socks
229 * and (if applicable) received their rexec state by sending a char over their
230 * sock. Child processes signal that authentication has completed by closing
231 * the sock (or by exiting).
232 */
djm@openbsd.orgdbb4dec2019-01-17 01:50:24 +0000233static int *startup_pipes = NULL;
djm@openbsd.org76a24b32019-03-01 02:32:39 +0000234static int *startup_flags = NULL; /* Indicates child closed listener */
235static int startup_pipe = -1; /* in child */
Ben Lindstromd84df982001-12-06 16:35:40 +0000236
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000237/* variables used for privilege separation */
Darren Tucker45150472006-07-12 22:34:17 +1000238int use_privsep = -1;
Darren Tuckera8be9e22004-02-06 16:40:27 +1100239struct monitor *pmonitor = NULL;
Damien Miller9ee2c602011-09-22 21:38:30 +1000240int privsep_is_preauth = 1;
Darren Tuckerd13281f2017-03-29 12:39:39 +1100241static int privsep_chroot = 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000242
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000243/* global connection state and authentication contexts */
Darren Tucker3e33cec2003-10-02 16:12:36 +1000244Authctxt *the_authctxt = NULL;
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000245struct ssh *the_active_state;
Darren Tucker3e33cec2003-10-02 16:12:36 +1000246
djm@openbsd.org7c856852018-03-03 03:15:51 +0000247/* global key/cert auth options. XXX move to permanent ssh->authctxt? */
248struct sshauthopt *auth_opts = NULL;
249
Darren Tucker45150472006-07-12 22:34:17 +1000250/* sshd_config buffer */
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +0000251struct sshbuf *cfg;
Darren Tucker45150472006-07-12 22:34:17 +1000252
Darren Tucker09991742004-07-17 17:05:14 +1000253/* message to be displayed after login */
markus@openbsd.org2808d182018-07-09 21:26:02 +0000254struct sshbuf *loginmsg;
Darren Tucker09991742004-07-17 17:05:14 +1000255
Damien Miller6433df02006-09-07 10:36:43 +1000256/* Unprivileged user */
257struct passwd *privsep_pw = NULL;
258
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259/* Prototypes for various functions defined later in this file. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000260void destroy_sensitive_data(void);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000261void demote_sensitive_data(void);
djm@openbsd.org6350e032019-01-19 21:42:30 +0000262static void do_ssh2_kex(struct ssh *);
Damien Miller874d77b2000-10-14 16:23:11 +1100263
Damien Miller98c7ad62000-03-09 21:27:49 +1100264/*
Damien Miller34132e52000-01-14 15:45:46 +1100265 * Close all listening sockets
266 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000267static void
Damien Miller34132e52000-01-14 15:45:46 +1100268close_listen_socks(void)
269{
270 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000271
Damien Miller34132e52000-01-14 15:45:46 +1100272 for (i = 0; i < num_listen_socks; i++)
273 close(listen_socks[i]);
274 num_listen_socks = -1;
275}
276
Ben Lindstromd84df982001-12-06 16:35:40 +0000277static void
278close_startup_pipes(void)
279{
280 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000281
Ben Lindstromd84df982001-12-06 16:35:40 +0000282 if (startup_pipes)
283 for (i = 0; i < options.max_startups; i++)
284 if (startup_pipes[i] != -1)
285 close(startup_pipes[i]);
286}
287
Damien Miller34132e52000-01-14 15:45:46 +1100288/*
Damien Miller95def091999-11-25 00:26:21 +1100289 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
290 * the effect is to reread the configuration file (and to regenerate
291 * the server key).
292 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100293
294/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000295static void
Damien Miller95def091999-11-25 00:26:21 +1100296sighup_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297{
Damien Miller95def091999-11-25 00:26:21 +1100298 received_sighup = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299}
300
Damien Miller95def091999-11-25 00:26:21 +1100301/*
302 * Called from the main program after receiving SIGHUP.
303 * Restarts the server.
304 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000305static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000306sighup_restart(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307{
Damien Miller996acd22003-04-09 20:59:48 +1000308 logit("Received SIGHUP; restarting.");
dtucker@openbsd.orgf2398eb2016-12-04 22:27:25 +0000309 if (options.pid_file != NULL)
310 unlink(options.pid_file);
Darren Tuckerf2bf36c2013-09-22 19:02:40 +1000311 platform_pre_restart();
Damien Miller34132e52000-01-14 15:45:46 +1100312 close_listen_socks();
Ben Lindstromd84df982001-12-06 16:35:40 +0000313 close_startup_pipes();
Darren Tuckered623962007-02-25 20:37:21 +1100314 alarm(0); /* alarm timer persists across exec */
Darren Tucker2c671bf2010-01-09 22:28:43 +1100315 signal(SIGHUP, SIG_IGN); /* will be restored after exec */
Damien Miller95def091999-11-25 00:26:21 +1100316 execv(saved_argv[0], saved_argv);
Damien Miller996acd22003-04-09 20:59:48 +1000317 logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
Ben Lindstrom822b6342002-06-23 21:38:49 +0000318 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100319 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320}
321
Damien Miller95def091999-11-25 00:26:21 +1100322/*
323 * Generic signal handler for terminating signals in the master daemon.
Damien Miller95def091999-11-25 00:26:21 +1100324 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100325/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000326static void
Damien Miller95def091999-11-25 00:26:21 +1100327sigterm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328{
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000329 received_sigterm = sig;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330}
331
Damien Miller95def091999-11-25 00:26:21 +1100332/*
333 * SIGCHLD handler. This is called whenever a child dies. This will then
Ben Lindstromec46e0b2001-06-09 01:27:31 +0000334 * reap any zombies left by exited children.
Damien Miller95def091999-11-25 00:26:21 +1100335 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100336/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000337static void
Damien Miller95def091999-11-25 00:26:21 +1100338main_sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339{
Damien Miller95def091999-11-25 00:26:21 +1100340 int save_errno = errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000341 pid_t pid;
Damien Miller95def091999-11-25 00:26:21 +1100342 int status;
Damien Miller431f66b1999-11-21 18:31:57 +1100343
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000344 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000345 (pid == -1 && errno == EINTR))
Damien Miller95def091999-11-25 00:26:21 +1100346 ;
Damien Miller95def091999-11-25 00:26:21 +1100347 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348}
349
Damien Miller95def091999-11-25 00:26:21 +1100350/*
351 * Signal handler for the alarm after the login grace period has expired.
352 */
Damien Millerf0b15df2006-03-26 13:59:20 +1100353/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000354static void
Damien Miller95def091999-11-25 00:26:21 +1100355grace_alarm_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356{
Darren Tuckera8be9e22004-02-06 16:40:27 +1100357 if (use_privsep && pmonitor != NULL && pmonitor->m_pid > 0)
358 kill(pmonitor->m_pid, SIGALRM);
359
Damien Miller09d3e122012-10-31 08:58:58 +1100360 /*
361 * Try to kill any processes that we have spawned, E.g. authorized
362 * keys command helpers.
363 */
364 if (getpgid(0) == getpid()) {
365 signal(SIGTERM, SIG_IGN);
Damien Millerab16ef42014-01-28 15:08:12 +1100366 kill(0, SIGTERM);
Damien Miller09d3e122012-10-31 08:58:58 +1100367 }
368
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000369 /* XXX pre-format ipaddr/port so we don't need to access active_state */
Damien Miller95def091999-11-25 00:26:21 +1100370 /* Log error and exit. */
djm@openbsd.org95767262016-03-07 19:02:43 +0000371 sigdie("Timeout before authentication for %s port %d",
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000372 ssh_remote_ipaddr(the_active_state),
373 ssh_remote_port(the_active_state));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374}
375
Damien Miller0bc1bd82000-11-13 22:57:25 +1100376/* Destroy the host and server keys. They will no longer be needed. */
Damien Millereba71ba2000-04-29 23:57:08 +1000377void
378destroy_sensitive_data(void)
379{
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000380 u_int i;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100381
Damien Miller9f0f5c62001-12-21 14:45:46 +1100382 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100383 if (sensitive_data.host_keys[i]) {
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000384 sshkey_free(sensitive_data.host_keys[i]);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100385 sensitive_data.host_keys[i] = NULL;
386 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100387 if (sensitive_data.host_certificates[i]) {
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000388 sshkey_free(sensitive_data.host_certificates[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +1100389 sensitive_data.host_certificates[i] = NULL;
390 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100391 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100392}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100393
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000394/* Demote private to public keys for network child */
395void
396demote_sensitive_data(void)
397{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000398 struct sshkey *tmp;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000399 u_int i;
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000400 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000401
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000402 for (i = 0; i < options.num_host_key_files; i++) {
403 if (sensitive_data.host_keys[i]) {
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000404 if ((r = sshkey_from_private(
405 sensitive_data.host_keys[i], &tmp)) != 0)
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000406 fatal("could not demote host %s key: %s",
407 sshkey_type(sensitive_data.host_keys[i]),
408 ssh_err(r));
409 sshkey_free(sensitive_data.host_keys[i]);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000410 sensitive_data.host_keys[i] = tmp;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000411 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100412 /* Certs do not need demotion */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000413 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000414}
415
Ben Lindstrom08105192002-03-22 02:50:06 +0000416static void
Damien Millerc9f880c2016-11-30 13:51:49 +1100417reseed_prngs(void)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000418{
Darren Tucker64cee362009-06-21 20:26:17 +1000419 u_int32_t rnd[256];
Damien Millerc9f880c2016-11-30 13:51:49 +1100420
421#ifdef WITH_OPENSSL
422 RAND_poll();
423#endif
424 arc4random_stir(); /* noop on recent arc4random() implementations */
425 arc4random_buf(rnd, sizeof(rnd)); /* let arc4random notice PID change */
426
427#ifdef WITH_OPENSSL
428 RAND_seed(rnd, sizeof(rnd));
429 /* give libcrypto a chance to notice the PID change */
430 if ((RAND_bytes((u_char *)rnd, 1)) != 1)
431 fatal("%s: RAND_bytes failed", __func__);
432#endif
433
434 explicit_bzero(rnd, sizeof(rnd));
435}
436
437static void
438privsep_preauth_child(void)
439{
Ben Lindstrom810af962002-07-04 00:11:40 +0000440 gid_t gidset[1];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000441
442 /* Enable challenge-response authentication for privilege separation */
443 privsep_challenge_enable();
444
Damien Millerfb3423b2014-02-27 10:20:07 +1100445#ifdef GSSAPI
Damien Millere6a74ae2014-02-27 10:17:49 +1100446 /* Cache supported mechanism OIDs for later use */
djm@openbsd.orgcb24d9f2018-09-21 12:23:17 +0000447 ssh_gssapi_prepare_supported_oids();
Damien Millerfb3423b2014-02-27 10:20:07 +1100448#endif
Damien Millere6a74ae2014-02-27 10:17:49 +1100449
Damien Millerc9f880c2016-11-30 13:51:49 +1100450 reseed_prngs();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000451
452 /* Demote the private keys to public keys. */
453 demote_sensitive_data();
454
djm@openbsd.org5b4010d2015-11-16 22:51:05 +0000455 /* Demote the child */
Darren Tuckerd13281f2017-03-29 12:39:39 +1100456 if (privsep_chroot) {
djm@openbsd.org5b4010d2015-11-16 22:51:05 +0000457 /* Change our root directory */
458 if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
459 fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
460 strerror(errno));
461 if (chdir("/") == -1)
462 fatal("chdir(\"/\"): %s", strerror(errno));
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000463
djm@openbsd.org5b4010d2015-11-16 22:51:05 +0000464 /* Drop our privileges */
465 debug3("privsep user:group %u:%u", (u_int)privsep_pw->pw_uid,
466 (u_int)privsep_pw->pw_gid);
467 gidset[0] = privsep_pw->pw_gid;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000468 if (setgroups(1, gidset) == -1)
djm@openbsd.org5b4010d2015-11-16 22:51:05 +0000469 fatal("setgroups: %.100s", strerror(errno));
470 permanently_set_uid(privsep_pw);
471 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000472}
473
Darren Tucker3e33cec2003-10-02 16:12:36 +1000474static int
djm@openbsd.org6350e032019-01-19 21:42:30 +0000475privsep_preauth(struct ssh *ssh)
Ben Lindstrom943481c2002-03-22 03:43:46 +0000476{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000477 int status, r;
Ben Lindstrom943481c2002-03-22 03:43:46 +0000478 pid_t pid;
Damien Miller69ff1df2011-06-23 08:30:03 +1000479 struct ssh_sandbox *box = NULL;
Ben Lindstrom943481c2002-03-22 03:43:46 +0000480
481 /* Set up unprivileged child process to deal with network data */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000482 pmonitor = monitor_init();
Ben Lindstrom943481c2002-03-22 03:43:46 +0000483 /* Store a pointer to the kex for later rekeying */
djm@openbsd.org6350e032019-01-19 21:42:30 +0000484 pmonitor->m_pkex = &ssh->kex;
Ben Lindstrom943481c2002-03-22 03:43:46 +0000485
Damien Miller5a5c2b92012-07-31 12:21:34 +1000486 if (use_privsep == PRIVSEP_ON)
Damien Miller868ea1e2014-01-17 16:47:04 +1100487 box = ssh_sandbox_init(pmonitor);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000488 pid = fork();
489 if (pid == -1) {
490 fatal("fork of unprivileged child failed");
491 } else if (pid != 0) {
Ben Lindstromce0f6342002-06-11 16:42:49 +0000492 debug2("Network child is on pid %ld", (long)pid);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000493
Darren Tucker3b4b2d32012-07-02 18:54:31 +1000494 pmonitor->m_pid = pid;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000495 if (have_agent) {
496 r = ssh_get_authentication_socket(&auth_sock);
497 if (r != 0) {
498 error("Could not get agent socket: %s",
499 ssh_err(r));
500 have_agent = 0;
501 }
502 }
Damien Miller69ff1df2011-06-23 08:30:03 +1000503 if (box != NULL)
504 ssh_sandbox_parent_preauth(box, pid);
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000505 monitor_child_preauth(ssh, pmonitor);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000506
Ben Lindstrom943481c2002-03-22 03:43:46 +0000507 /* Wait for the child's exit status */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000508 while (waitpid(pid, &status, 0) == -1) {
Damien Miller9ee2c602011-09-22 21:38:30 +1000509 if (errno == EINTR)
510 continue;
511 pmonitor->m_pid = -1;
512 fatal("%s: waitpid: %s", __func__, strerror(errno));
Damien Miller69ff1df2011-06-23 08:30:03 +1000513 }
Damien Miller9ee2c602011-09-22 21:38:30 +1000514 privsep_is_preauth = 0;
515 pmonitor->m_pid = -1;
Damien Miller69ff1df2011-06-23 08:30:03 +1000516 if (WIFEXITED(status)) {
517 if (WEXITSTATUS(status) != 0)
518 fatal("%s: preauth child exited with status %d",
519 __func__, WEXITSTATUS(status));
520 } else if (WIFSIGNALED(status))
521 fatal("%s: preauth child terminated by signal %d",
522 __func__, WTERMSIG(status));
523 if (box != NULL)
524 ssh_sandbox_parent_finish(box);
525 return 1;
Ben Lindstrom943481c2002-03-22 03:43:46 +0000526 } else {
527 /* child */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000528 close(pmonitor->m_sendfd);
Damien Miller8f0bf232011-06-20 14:42:23 +1000529 close(pmonitor->m_log_recvfd);
530
531 /* Arrange for logging to be sent to the monitor */
532 set_log_handler(mm_log_handler, pmonitor);
Ben Lindstrom943481c2002-03-22 03:43:46 +0000533
djm@openbsd.org5b4010d2015-11-16 22:51:05 +0000534 privsep_preauth_child();
Ben Lindstromf90f58d2002-03-26 01:53:03 +0000535 setproctitle("%s", "[net]");
Damien Miller69ff1df2011-06-23 08:30:03 +1000536 if (box != NULL)
537 ssh_sandbox_child(box);
538
539 return 0;
Ben Lindstrom943481c2002-03-22 03:43:46 +0000540 }
Ben Lindstrom943481c2002-03-22 03:43:46 +0000541}
542
Ben Lindstrom08105192002-03-22 02:50:06 +0000543static void
djm@openbsd.org6350e032019-01-19 21:42:30 +0000544privsep_postauth(struct ssh *ssh, Authctxt *authctxt)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000545{
Tim Rice9dd30812002-07-07 13:43:36 -0700546#ifdef DISABLE_FD_PASSING
Tim Rice8eff3192002-06-25 15:35:15 -0700547 if (1) {
548#else
djm@openbsd.org83b58182016-08-19 03:18:06 +0000549 if (authctxt->pw->pw_uid == 0) {
Tim Rice8eff3192002-06-25 15:35:15 -0700550#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000551 /* File descriptor passing is broken or root login */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000552 use_privsep = 0;
Darren Tucker45b01422005-10-03 18:20:00 +1000553 goto skip;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000554 }
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000555
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000556 /* New socket pair */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000557 monitor_reinit(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000558
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000559 pmonitor->m_pid = fork();
560 if (pmonitor->m_pid == -1)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561 fatal("fork of unprivileged child failed");
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000562 else if (pmonitor->m_pid != 0) {
Damien Millerb61f3fc2008-07-11 17:36:48 +1000563 verbose("User child is on pid %ld", (long)pmonitor->m_pid);
markus@openbsd.org2808d182018-07-09 21:26:02 +0000564 sshbuf_reset(loginmsg);
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000565 monitor_clear_keystate(ssh, pmonitor);
566 monitor_child_postauth(ssh, pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000567
568 /* NEVERREACHED */
569 exit(0);
570 }
571
Damien Miller8f0bf232011-06-20 14:42:23 +1000572 /* child */
573
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000574 close(pmonitor->m_sendfd);
Damien Miller8f0bf232011-06-20 14:42:23 +1000575 pmonitor->m_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000576
577 /* Demote the private keys to public keys. */
578 demote_sensitive_data();
579
Damien Millerc9f880c2016-11-30 13:51:49 +1100580 reseed_prngs();
Damien Miller76e95da2008-03-07 18:31:24 +1100581
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000582 /* Drop privileges */
583 do_setusercontext(authctxt->pw);
584
Darren Tucker45b01422005-10-03 18:20:00 +1000585 skip:
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586 /* It is safe now to apply the key state */
djm@openbsd.orgec00f912019-01-19 21:43:07 +0000587 monitor_apply_keystate(ssh, pmonitor);
Damien Miller9786e6e2005-07-26 21:54:56 +1000588
589 /*
590 * Tell the packet layer that authentication was successful, since
591 * this information is not part of the key state.
592 */
djm@openbsd.org6350e032019-01-19 21:42:30 +0000593 ssh_packet_set_authenticated(ssh);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594}
595
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000596static void
597append_hostkey_type(struct sshbuf *b, const char *s)
598{
599 int r;
600
601 if (match_pattern_list(s, options.hostkeyalgorithms, 0) != 1) {
602 debug3("%s: %s key not permitted by HostkeyAlgorithms",
603 __func__, s);
604 return;
605 }
606 if ((r = sshbuf_putf(b, "%s%s", sshbuf_len(b) > 0 ? "," : "", s)) != 0)
607 fatal("%s: sshbuf_putf: %s", __func__, ssh_err(r));
608}
609
Ben Lindstrombba81212001-06-25 05:01:22 +0000610static char *
Damien Miller0bc1bd82000-11-13 22:57:25 +1100611list_hostkey_types(void)
612{
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000613 struct sshbuf *b;
614 struct sshkey *key;
Damien Millerf58b58c2003-11-17 21:18:23 +1100615 char *ret;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000616 u_int i;
Damien Miller0e3b8722002-01-22 23:26:38 +1100617
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000618 if ((b = sshbuf_new()) == NULL)
619 fatal("%s: sshbuf_new failed", __func__);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100620 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100621 key = sensitive_data.host_keys[i];
djm@openbsd.orgce63c4b2015-02-16 22:30:03 +0000622 if (key == NULL)
Damien Miller85b45e02013-07-20 13:21:52 +1000623 key = sensitive_data.host_pubkeys[i];
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000624 if (key == NULL)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100625 continue;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000626 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100627 case KEY_RSA:
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000628 /* for RSA we also support SHA2 signatures */
629 append_hostkey_type(b, "rsa-sha2-512");
630 append_hostkey_type(b, "rsa-sha2-256");
631 /* FALLTHROUGH */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100632 case KEY_DSA:
Damien Millereb8b60e2010-08-31 22:41:14 +1000633 case KEY_ECDSA:
Damien Miller5be9d9e2013-12-07 11:24:01 +1100634 case KEY_ED25519:
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000635 case KEY_XMSS:
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000636 append_hostkey_type(b, sshkey_ssh_name(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100637 break;
638 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100639 /* If the private key has a cert peer, then list that too */
640 key = sensitive_data.host_certificates[i];
641 if (key == NULL)
642 continue;
643 switch (key->type) {
644 case KEY_RSA_CERT:
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000645 /* for RSA we also support SHA2 signatures */
646 append_hostkey_type(b,
647 "rsa-sha2-512-cert-v01@openssh.com");
648 append_hostkey_type(b,
649 "rsa-sha2-256-cert-v01@openssh.com");
650 /* FALLTHROUGH */
Damien Miller0a80ca12010-02-27 07:55:05 +1100651 case KEY_DSA_CERT:
Damien Millereb8b60e2010-08-31 22:41:14 +1000652 case KEY_ECDSA_CERT:
Damien Miller5be9d9e2013-12-07 11:24:01 +1100653 case KEY_ED25519_CERT:
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000654 case KEY_XMSS_CERT:
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000655 append_hostkey_type(b, sshkey_ssh_name(key));
Damien Miller0a80ca12010-02-27 07:55:05 +1100656 break;
657 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100658 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000659 if ((ret = sshbuf_dup_string(b)) == NULL)
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000660 fatal("%s: sshbuf_dup_string failed", __func__);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000661 sshbuf_free(b);
662 debug("%s: %s", __func__, ret);
Damien Millerf58b58c2003-11-17 21:18:23 +1100663 return ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100664}
665
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000666static struct sshkey *
djm@openbsd.org5104db72015-01-26 06:10:03 +0000667get_hostkey_by_type(int type, int nid, int need_private, struct ssh *ssh)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100668{
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000669 u_int i;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000670 struct sshkey *key;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000671
Damien Miller9f0f5c62001-12-21 14:45:46 +1100672 for (i = 0; i < options.num_host_key_files; i++) {
Damien Miller4e270b02010-04-16 15:56:21 +1000673 switch (type) {
Damien Miller4e270b02010-04-16 15:56:21 +1000674 case KEY_RSA_CERT:
675 case KEY_DSA_CERT:
Damien Millereb8b60e2010-08-31 22:41:14 +1000676 case KEY_ECDSA_CERT:
Damien Miller5be9d9e2013-12-07 11:24:01 +1100677 case KEY_ED25519_CERT:
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000678 case KEY_XMSS_CERT:
Damien Miller0a80ca12010-02-27 07:55:05 +1100679 key = sensitive_data.host_certificates[i];
Damien Miller4e270b02010-04-16 15:56:21 +1000680 break;
681 default:
Damien Miller0a80ca12010-02-27 07:55:05 +1100682 key = sensitive_data.host_keys[i];
Damien Miller85b45e02013-07-20 13:21:52 +1000683 if (key == NULL && !need_private)
684 key = sensitive_data.host_pubkeys[i];
Damien Miller4e270b02010-04-16 15:56:21 +1000685 break;
686 }
djm@openbsd.org5104db72015-01-26 06:10:03 +0000687 if (key != NULL && key->type == type &&
688 (key->type != KEY_ECDSA || key->ecdsa_nid == nid))
Damien Miller0a80ca12010-02-27 07:55:05 +1100689 return need_private ?
690 sensitive_data.host_keys[i] : key;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100691 }
692 return NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000693}
694
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000695struct sshkey *
djm@openbsd.org5104db72015-01-26 06:10:03 +0000696get_hostkey_public_by_type(int type, int nid, struct ssh *ssh)
Damien Miller0a80ca12010-02-27 07:55:05 +1100697{
djm@openbsd.org5104db72015-01-26 06:10:03 +0000698 return get_hostkey_by_type(type, nid, 0, ssh);
Damien Miller0a80ca12010-02-27 07:55:05 +1100699}
700
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000701struct sshkey *
djm@openbsd.org5104db72015-01-26 06:10:03 +0000702get_hostkey_private_by_type(int type, int nid, struct ssh *ssh)
Damien Miller0a80ca12010-02-27 07:55:05 +1100703{
djm@openbsd.org5104db72015-01-26 06:10:03 +0000704 return get_hostkey_by_type(type, nid, 1, ssh);
Damien Miller0a80ca12010-02-27 07:55:05 +1100705}
706
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000707struct sshkey *
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000708get_hostkey_by_index(int ind)
709{
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000710 if (ind < 0 || (u_int)ind >= options.num_host_key_files)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000711 return (NULL);
712 return (sensitive_data.host_keys[ind]);
713}
714
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000715struct sshkey *
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000716get_hostkey_public_by_index(int ind, struct ssh *ssh)
Damien Miller85b45e02013-07-20 13:21:52 +1000717{
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000718 if (ind < 0 || (u_int)ind >= options.num_host_key_files)
Damien Miller85b45e02013-07-20 13:21:52 +1000719 return (NULL);
720 return (sensitive_data.host_pubkeys[ind]);
721}
722
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000723int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000724get_hostkey_index(struct sshkey *key, int compare, struct ssh *ssh)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725{
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000726 u_int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000727
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728 for (i = 0; i < options.num_host_key_files; i++) {
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000729 if (sshkey_is_cert(key)) {
djm@openbsd.org523463a2015-02-16 22:13:32 +0000730 if (key == sensitive_data.host_certificates[i] ||
731 (compare && sensitive_data.host_certificates[i] &&
732 sshkey_equal(key,
733 sensitive_data.host_certificates[i])))
Damien Miller0a80ca12010-02-27 07:55:05 +1100734 return (i);
735 } else {
djm@openbsd.org523463a2015-02-16 22:13:32 +0000736 if (key == sensitive_data.host_keys[i] ||
737 (compare && sensitive_data.host_keys[i] &&
738 sshkey_equal(key, sensitive_data.host_keys[i])))
Damien Miller0a80ca12010-02-27 07:55:05 +1100739 return (i);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000740 if (key == sensitive_data.host_pubkeys[i] ||
741 (compare && sensitive_data.host_pubkeys[i] &&
742 sshkey_equal(key, sensitive_data.host_pubkeys[i])))
Damien Miller85b45e02013-07-20 13:21:52 +1000743 return (i);
Damien Miller0a80ca12010-02-27 07:55:05 +1100744 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000745 }
746 return (-1);
747}
748
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000749/* Inform the client of all hostkeys */
750static void
751notify_hostkeys(struct ssh *ssh)
752{
753 struct sshbuf *buf;
754 struct sshkey *key;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000755 u_int i, nkeys;
756 int r;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000757 char *fp;
758
dtucker@openbsd.orgd8f391c2015-04-10 05:16:50 +0000759 /* Some clients cannot cope with the hostkeys message, skip those. */
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000760 if (ssh->compat & SSH_BUG_HOSTKEYS)
dtucker@openbsd.orgd8f391c2015-04-10 05:16:50 +0000761 return;
762
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000763 if ((buf = sshbuf_new()) == NULL)
764 fatal("%s: sshbuf_new", __func__);
765 for (i = nkeys = 0; i < options.num_host_key_files; i++) {
766 key = get_hostkey_public_by_index(i, ssh);
767 if (key == NULL || key->type == KEY_UNSPEC ||
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +0000768 sshkey_is_cert(key))
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000769 continue;
770 fp = sshkey_fingerprint(key, options.fingerprint_hash,
771 SSH_FP_DEFAULT);
772 debug3("%s: key %d: %s %s", __func__, i,
773 sshkey_ssh_name(key), fp);
774 free(fp);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000775 if (nkeys == 0) {
djm@openbsd.org6350e032019-01-19 21:42:30 +0000776 /*
777 * Start building the request when we find the
778 * first usable key.
779 */
780 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
781 (r = sshpkt_put_cstring(ssh, "hostkeys-00@openssh.com")) != 0 ||
782 (r = sshpkt_put_u8(ssh, 0)) != 0) /* want reply */
783 sshpkt_fatal(ssh, r, "%s: start request", __func__);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000784 }
djm@openbsd.org6350e032019-01-19 21:42:30 +0000785 /* Append the key to the request */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000786 sshbuf_reset(buf);
787 if ((r = sshkey_putb(key, buf)) != 0)
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000788 fatal("%s: couldn't put hostkey %d: %s",
789 __func__, i, ssh_err(r));
djm@openbsd.org6350e032019-01-19 21:42:30 +0000790 if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
791 sshpkt_fatal(ssh, r, "%s: append key", __func__);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000792 nkeys++;
793 }
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000794 debug3("%s: sent %u hostkeys", __func__, nkeys);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000795 if (nkeys == 0)
796 fatal("%s: no hostkeys", __func__);
djm@openbsd.org6350e032019-01-19 21:42:30 +0000797 if ((r = sshpkt_send(ssh)) != 0)
798 sshpkt_fatal(ssh, r, "%s: send", __func__);
djm@openbsd.org523463a2015-02-16 22:13:32 +0000799 sshbuf_free(buf);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000800}
801
Damien Miller942da032000-08-18 13:59:06 +1000802/*
803 * returns 1 if connection should be dropped, 0 otherwise.
804 * dropping starts at connection #max_startups_begin with a probability
805 * of (max_startups_rate/100). the probability increases linearly until
806 * all connections are dropped for startups > max_startups
807 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000808static int
Damien Miller942da032000-08-18 13:59:06 +1000809drop_connection(int startups)
810{
Darren Tucker178fa662004-11-05 20:09:09 +1100811 int p, r;
Damien Miller942da032000-08-18 13:59:06 +1000812
813 if (startups < options.max_startups_begin)
814 return 0;
815 if (startups >= options.max_startups)
816 return 1;
817 if (options.max_startups_rate == 100)
818 return 1;
819
820 p = 100 - options.max_startups_rate;
821 p *= startups - options.max_startups_begin;
Darren Tucker178fa662004-11-05 20:09:09 +1100822 p /= options.max_startups - options.max_startups_begin;
Damien Miller942da032000-08-18 13:59:06 +1000823 p += options.max_startups_rate;
Damien Miller354c48c2008-05-19 14:50:00 +1000824 r = arc4random_uniform(100);
Damien Miller942da032000-08-18 13:59:06 +1000825
Darren Tucker3269b132004-11-05 20:20:59 +1100826 debug("drop_connection: p %d, r %d", p, r);
Damien Miller942da032000-08-18 13:59:06 +1000827 return (r < p) ? 1 : 0;
828}
829
Ben Lindstromade03f62001-12-06 18:22:17 +0000830static void
831usage(void)
832{
Damien Miller0c889cd2004-03-22 09:36:00 +1100833 fprintf(stderr, "%s, %s\n",
Damien Miller1f0311c2014-05-15 14:24:09 +1000834 SSH_RELEASE,
835#ifdef WITH_OPENSSL
djm@openbsd.orga65784c2018-10-23 05:56:35 +0000836 OpenSSL_version(OPENSSL_VERSION)
Damien Miller1f0311c2014-05-15 14:24:09 +1000837#else
838 "without OpenSSL"
839#endif
840 );
Damien Millerb4087862004-03-22 09:35:21 +1100841 fprintf(stderr,
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000842"usage: sshd [-46DdeiqTt] [-C connection_spec] [-c host_cert_file]\n"
Damien Miller03d4d7e2013-04-23 15:21:06 +1000843" [-E log_file] [-f config_file] [-g login_grace_time]\n"
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000844" [-h host_key_file] [-o option] [-p port] [-u len]\n"
Damien Millerb4087862004-03-22 09:35:21 +1100845 );
Ben Lindstromade03f62001-12-06 18:22:17 +0000846 exit(1);
847}
848
Darren Tucker645ab752004-06-25 13:33:20 +1000849static void
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000850send_rexec_state(int fd, struct sshbuf *conf)
Darren Tucker645ab752004-06-25 13:33:20 +1000851{
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000852 struct sshbuf *m;
853 int r;
Darren Tucker645ab752004-06-25 13:33:20 +1000854
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000855 debug3("%s: entering fd = %d config len %zu", __func__, fd,
856 sshbuf_len(conf));
Darren Tucker645ab752004-06-25 13:33:20 +1000857
858 /*
859 * Protocol from reexec master to child:
860 * string configuration
Darren Tuckerc6f82192005-09-27 22:46:32 +1000861 * string rngseed (only if OpenSSL is not self-seeded)
Darren Tucker645ab752004-06-25 13:33:20 +1000862 */
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000863 if ((m = sshbuf_new()) == NULL)
864 fatal("%s: sshbuf_new failed", __func__);
865 if ((r = sshbuf_put_stringb(m, conf)) != 0)
866 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker645ab752004-06-25 13:33:20 +1000867
Damien Miller72ef7c12015-01-15 02:21:31 +1100868#if defined(WITH_OPENSSL) && !defined(OPENSSL_PRNG_ONLY)
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000869 rexec_send_rng_seed(m);
Darren Tuckerc6f82192005-09-27 22:46:32 +1000870#endif
871
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000872 if (ssh_msg_send(fd, 0, m) == -1)
Darren Tucker645ab752004-06-25 13:33:20 +1000873 fatal("%s: ssh_msg_send failed", __func__);
874
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000875 sshbuf_free(m);
Darren Tucker645ab752004-06-25 13:33:20 +1000876
877 debug3("%s: done", __func__);
878}
879
880static void
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +0000881recv_rexec_state(int fd, struct sshbuf *conf)
Darren Tucker645ab752004-06-25 13:33:20 +1000882{
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +0000883 struct sshbuf *m;
884 u_char *cp, ver;
885 size_t len;
886 int r;
Darren Tucker645ab752004-06-25 13:33:20 +1000887
888 debug3("%s: entering fd = %d", __func__, fd);
889
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +0000890 if ((m = sshbuf_new()) == NULL)
891 fatal("%s: sshbuf_new failed", __func__);
892 if (ssh_msg_recv(fd, m) == -1)
Darren Tucker645ab752004-06-25 13:33:20 +1000893 fatal("%s: ssh_msg_recv failed", __func__);
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +0000894 if ((r = sshbuf_get_u8(m, &ver)) != 0)
895 fatal("%s: buffer error: %s", __func__, ssh_err(r));
896 if (ver != 0)
Darren Tucker645ab752004-06-25 13:33:20 +1000897 fatal("%s: rexec version mismatch", __func__);
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +0000898 if ((r = sshbuf_get_string(m, &cp, &len)) != 0)
899 fatal("%s: buffer error: %s", __func__, ssh_err(r));
900 if (conf != NULL && (r = sshbuf_put(conf, cp, len)))
901 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller72ef7c12015-01-15 02:21:31 +1100902#if defined(WITH_OPENSSL) && !defined(OPENSSL_PRNG_ONLY)
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +0000903 rexec_recv_rng_seed(m);
Darren Tuckerc6f82192005-09-27 22:46:32 +1000904#endif
905
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +0000906 free(cp);
907 sshbuf_free(m);
Darren Tucker645ab752004-06-25 13:33:20 +1000908
909 debug3("%s: done", __func__);
910}
911
Damien Millera1f68402006-08-19 00:31:39 +1000912/* Accept a connection from inetd */
913static void
914server_accept_inetd(int *sock_in, int *sock_out)
915{
916 int fd;
917
Damien Millera1f68402006-08-19 00:31:39 +1000918 if (rexeced_flag) {
919 close(REEXEC_CONFIG_PASS_FD);
920 *sock_in = *sock_out = dup(STDIN_FILENO);
Damien Millera1f68402006-08-19 00:31:39 +1000921 } else {
922 *sock_in = dup(STDIN_FILENO);
923 *sock_out = dup(STDOUT_FILENO);
924 }
925 /*
926 * We intentionally do not close the descriptors 0, 1, and 2
927 * as our code for setting the descriptors won't work if
928 * ttyfd happens to be one of those.
929 */
930 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
931 dup2(fd, STDIN_FILENO);
932 dup2(fd, STDOUT_FILENO);
Darren Tucker0cca17f2013-06-06 08:21:14 +1000933 if (!log_stderr)
934 dup2(fd, STDERR_FILENO);
935 if (fd > (log_stderr ? STDERR_FILENO : STDOUT_FILENO))
Damien Millera1f68402006-08-19 00:31:39 +1000936 close(fd);
937 }
938 debug("inetd sockets after dupping: %d, %d", *sock_in, *sock_out);
939}
940
941/*
942 * Listen for TCP connections
943 */
944static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000945listen_on_addrs(struct listenaddr *la)
Damien Millera1f68402006-08-19 00:31:39 +1000946{
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000947 int ret, listen_sock;
Damien Millera1f68402006-08-19 00:31:39 +1000948 struct addrinfo *ai;
949 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
950
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000951 for (ai = la->addrs; ai; ai = ai->ai_next) {
Damien Millera1f68402006-08-19 00:31:39 +1000952 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
953 continue;
954 if (num_listen_socks >= MAX_LISTEN_SOCKS)
955 fatal("Too many listen sockets. "
956 "Enlarge MAX_LISTEN_SOCKS");
957 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
958 ntop, sizeof(ntop), strport, sizeof(strport),
959 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
960 error("getnameinfo failed: %.100s",
Darren Tucker4abde772007-12-29 02:43:51 +1100961 ssh_gai_strerror(ret));
Damien Millera1f68402006-08-19 00:31:39 +1000962 continue;
963 }
964 /* Create socket for listening. */
Darren Tucker7bd98e72010-01-10 10:31:12 +1100965 listen_sock = socket(ai->ai_family, ai->ai_socktype,
966 ai->ai_protocol);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000967 if (listen_sock == -1) {
Damien Millera1f68402006-08-19 00:31:39 +1000968 /* kernel may not support ipv6 */
969 verbose("socket: %.100s", strerror(errno));
970 continue;
971 }
972 if (set_nonblock(listen_sock) == -1) {
973 close(listen_sock);
974 continue;
975 }
djm@openbsd.org8071a692017-02-24 03:16:34 +0000976 if (fcntl(listen_sock, F_SETFD, FD_CLOEXEC) == -1) {
977 verbose("socket: CLOEXEC: %s", strerror(errno));
978 close(listen_sock);
979 continue;
980 }
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000981 /* Socket options */
982 set_reuseaddr(listen_sock);
983 if (la->rdomain != NULL &&
984 set_rdomain(listen_sock, la->rdomain) == -1) {
985 close(listen_sock);
986 continue;
987 }
Damien Millera1f68402006-08-19 00:31:39 +1000988
Damien Miller49d2a282008-01-20 08:56:00 +1100989 /* Only communicate in IPv6 over AF_INET6 sockets. */
Damien Miller04ee0f82009-11-18 17:48:30 +1100990 if (ai->ai_family == AF_INET6)
991 sock_set_v6only(listen_sock);
Damien Miller49d2a282008-01-20 08:56:00 +1100992
Damien Millera1f68402006-08-19 00:31:39 +1000993 debug("Bind to port %s on %s.", strport, ntop);
994
995 /* Bind the socket to the desired port. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000996 if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) == -1) {
Damien Millera1f68402006-08-19 00:31:39 +1000997 error("Bind to port %s on %s failed: %.200s.",
998 strport, ntop, strerror(errno));
999 close(listen_sock);
1000 continue;
1001 }
1002 listen_socks[num_listen_socks] = listen_sock;
1003 num_listen_socks++;
1004
1005 /* Start listening on the port. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001006 if (listen(listen_sock, SSH_LISTEN_BACKLOG) == -1)
Damien Millera1f68402006-08-19 00:31:39 +10001007 fatal("listen on [%s]:%s: %.100s",
1008 ntop, strport, strerror(errno));
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001009 logit("Server listening on %s port %s%s%s.",
1010 ntop, strport,
1011 la->rdomain == NULL ? "" : " rdomain ",
1012 la->rdomain == NULL ? "" : la->rdomain);
Damien Millera1f68402006-08-19 00:31:39 +10001013 }
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001014}
1015
1016static void
1017server_listen(void)
1018{
1019 u_int i;
1020
1021 for (i = 0; i < options.num_listen_addrs; i++) {
1022 listen_on_addrs(&options.listen_addrs[i]);
1023 freeaddrinfo(options.listen_addrs[i].addrs);
1024 free(options.listen_addrs[i].rdomain);
1025 memset(&options.listen_addrs[i], 0,
1026 sizeof(options.listen_addrs[i]));
1027 }
1028 free(options.listen_addrs);
1029 options.listen_addrs = NULL;
1030 options.num_listen_addrs = 0;
Damien Millera1f68402006-08-19 00:31:39 +10001031
1032 if (!num_listen_socks)
1033 fatal("Cannot bind any address.");
1034}
1035
1036/*
1037 * The main TCP accept loop. Note that, for the non-debug case, returns
1038 * from this function are in a forked subprocess.
1039 */
1040static void
1041server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
1042{
1043 fd_set *fdset;
1044 int i, j, ret, maxfd;
djm@openbsd.org76a24b32019-03-01 02:32:39 +00001045 int startups = 0, listening = 0, lameduck = 0;
Damien Millera1f68402006-08-19 00:31:39 +10001046 int startup_p[2] = { -1 , -1 };
djm@openbsd.org76a24b32019-03-01 02:32:39 +00001047 char c = 0;
Damien Millera1f68402006-08-19 00:31:39 +10001048 struct sockaddr_storage from;
1049 socklen_t fromlen;
1050 pid_t pid;
Damien Miller045bda52013-09-14 09:44:37 +10001051 u_char rnd[256];
Damien Millera1f68402006-08-19 00:31:39 +10001052
1053 /* setup fd set for accept */
1054 fdset = NULL;
1055 maxfd = 0;
1056 for (i = 0; i < num_listen_socks; i++)
1057 if (listen_socks[i] > maxfd)
1058 maxfd = listen_socks[i];
1059 /* pipes connected to unauthenticated childs */
1060 startup_pipes = xcalloc(options.max_startups, sizeof(int));
djm@openbsd.org76a24b32019-03-01 02:32:39 +00001061 startup_flags = xcalloc(options.max_startups, sizeof(int));
Damien Millera1f68402006-08-19 00:31:39 +10001062 for (i = 0; i < options.max_startups; i++)
1063 startup_pipes[i] = -1;
1064
1065 /*
1066 * Stay listening for connections until the system crashes or
1067 * the daemon is killed with a signal.
1068 */
1069 for (;;) {
djm@openbsd.org76a24b32019-03-01 02:32:39 +00001070 if (received_sighup) {
1071 if (!lameduck) {
1072 debug("Received SIGHUP; waiting for children");
1073 close_listen_socks();
1074 lameduck = 1;
1075 }
1076 if (listening <= 0)
1077 sighup_restart();
1078 }
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001079 free(fdset);
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +00001080 fdset = xcalloc(howmany(maxfd + 1, NFDBITS),
Damien Millera1f68402006-08-19 00:31:39 +10001081 sizeof(fd_mask));
1082
1083 for (i = 0; i < num_listen_socks; i++)
1084 FD_SET(listen_socks[i], fdset);
1085 for (i = 0; i < options.max_startups; i++)
1086 if (startup_pipes[i] != -1)
1087 FD_SET(startup_pipes[i], fdset);
1088
1089 /* Wait in select until there is a connection. */
1090 ret = select(maxfd+1, fdset, NULL, NULL, NULL);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001091 if (ret == -1 && errno != EINTR)
Damien Millera1f68402006-08-19 00:31:39 +10001092 error("select: %.100s", strerror(errno));
1093 if (received_sigterm) {
1094 logit("Received signal %d; terminating.",
1095 (int) received_sigterm);
1096 close_listen_socks();
djm@openbsd.org161cf412014-12-22 07:55:51 +00001097 if (options.pid_file != NULL)
1098 unlink(options.pid_file);
Damien Miller26b57ce2011-05-05 14:15:09 +10001099 exit(received_sigterm == SIGTERM ? 0 : 255);
Damien Millera1f68402006-08-19 00:31:39 +10001100 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001101 if (ret == -1)
Damien Millera1f68402006-08-19 00:31:39 +10001102 continue;
1103
djm@openbsd.org76a24b32019-03-01 02:32:39 +00001104 for (i = 0; i < options.max_startups; i++) {
1105 if (startup_pipes[i] == -1 ||
1106 !FD_ISSET(startup_pipes[i], fdset))
1107 continue;
1108 switch (read(startup_pipes[i], &c, sizeof(c))) {
1109 case -1:
1110 if (errno == EINTR || errno == EAGAIN)
1111 continue;
1112 if (errno != EPIPE) {
1113 error("%s: startup pipe %d (fd=%d): "
1114 "read %s", __func__, i,
1115 startup_pipes[i], strerror(errno));
1116 }
1117 /* FALLTHROUGH */
1118 case 0:
1119 /* child exited or completed auth */
Damien Millera1f68402006-08-19 00:31:39 +10001120 close(startup_pipes[i]);
1121 startup_pipes[i] = -1;
1122 startups--;
djm@openbsd.org76a24b32019-03-01 02:32:39 +00001123 if (startup_flags[i])
1124 listening--;
1125 break;
1126 case 1:
1127 /* child has finished preliminaries */
1128 if (startup_flags[i]) {
1129 listening--;
1130 startup_flags[i] = 0;
1131 }
1132 break;
Damien Millera1f68402006-08-19 00:31:39 +10001133 }
djm@openbsd.org76a24b32019-03-01 02:32:39 +00001134 }
Damien Millera1f68402006-08-19 00:31:39 +10001135 for (i = 0; i < num_listen_socks; i++) {
1136 if (!FD_ISSET(listen_socks[i], fdset))
1137 continue;
1138 fromlen = sizeof(from);
1139 *newsock = accept(listen_socks[i],
1140 (struct sockaddr *)&from, &fromlen);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001141 if (*newsock == -1) {
Damien Miller37f1c082013-04-23 15:20:43 +10001142 if (errno != EINTR && errno != EWOULDBLOCK &&
1143 errno != ECONNABORTED && errno != EAGAIN)
Damien Millera116d132012-04-22 11:23:46 +10001144 error("accept: %.100s",
1145 strerror(errno));
1146 if (errno == EMFILE || errno == ENFILE)
1147 usleep(100 * 1000);
Damien Millera1f68402006-08-19 00:31:39 +10001148 continue;
1149 }
1150 if (unset_nonblock(*newsock) == -1) {
1151 close(*newsock);
1152 continue;
1153 }
1154 if (drop_connection(startups) == 1) {
djm@openbsd.org08a1e702016-12-09 03:04:29 +00001155 char *laddr = get_local_ipaddr(*newsock);
1156 char *raddr = get_peer_ipaddr(*newsock);
dtucker@openbsd.orgfc173ae2019-11-13 11:25:11 +00001157 char msg[] = "Exceeded MaxStartups\r\n";
djm@openbsd.org08a1e702016-12-09 03:04:29 +00001158
1159 verbose("drop connection #%d from [%s]:%d "
1160 "on [%s]:%d past MaxStartups", startups,
1161 raddr, get_peer_port(*newsock),
1162 laddr, get_local_port(*newsock));
1163 free(laddr);
1164 free(raddr);
dtucker@openbsd.orgfc173ae2019-11-13 11:25:11 +00001165 /* best-effort notification to client */
1166 (void)write(*newsock, msg, strlen(msg));
Damien Millera1f68402006-08-19 00:31:39 +10001167 close(*newsock);
1168 continue;
1169 }
1170 if (pipe(startup_p) == -1) {
1171 close(*newsock);
1172 continue;
1173 }
1174
1175 if (rexec_flag && socketpair(AF_UNIX,
1176 SOCK_STREAM, 0, config_s) == -1) {
1177 error("reexec socketpair: %s",
1178 strerror(errno));
1179 close(*newsock);
1180 close(startup_p[0]);
1181 close(startup_p[1]);
1182 continue;
1183 }
1184
1185 for (j = 0; j < options.max_startups; j++)
1186 if (startup_pipes[j] == -1) {
1187 startup_pipes[j] = startup_p[0];
1188 if (maxfd < startup_p[0])
1189 maxfd = startup_p[0];
1190 startups++;
djm@openbsd.org76a24b32019-03-01 02:32:39 +00001191 startup_flags[j] = 1;
Damien Millera1f68402006-08-19 00:31:39 +10001192 break;
1193 }
1194
1195 /*
1196 * Got connection. Fork a child to handle it, unless
1197 * we are in debugging mode.
1198 */
1199 if (debug_flag) {
1200 /*
1201 * In debugging mode. Close the listening
1202 * socket, and start processing the
1203 * connection without forking.
1204 */
1205 debug("Server will not fork when running in debugging mode.");
1206 close_listen_socks();
1207 *sock_in = *newsock;
1208 *sock_out = *newsock;
1209 close(startup_p[0]);
1210 close(startup_p[1]);
1211 startup_pipe = -1;
1212 pid = getpid();
1213 if (rexec_flag) {
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00001214 send_rexec_state(config_s[0], cfg);
Damien Millera1f68402006-08-19 00:31:39 +10001215 close(config_s[0]);
1216 }
djm@openbsd.org76a24b32019-03-01 02:32:39 +00001217 return;
Damien Millera1f68402006-08-19 00:31:39 +10001218 }
1219
1220 /*
1221 * Normal production daemon. Fork, and have
1222 * the child process the connection. The
1223 * parent continues listening.
1224 */
Damien Miller1b06dc32006-08-31 03:24:41 +10001225 platform_pre_fork();
djm@openbsd.org76a24b32019-03-01 02:32:39 +00001226 listening++;
Damien Millera1f68402006-08-19 00:31:39 +10001227 if ((pid = fork()) == 0) {
1228 /*
1229 * Child. Close the listening and
1230 * max_startup sockets. Start using
1231 * the accepted socket. Reinitialize
1232 * logging (since our pid has changed).
djm@openbsd.org76a24b32019-03-01 02:32:39 +00001233 * We return from this function to handle
Damien Millera1f68402006-08-19 00:31:39 +10001234 * the connection.
1235 */
Damien Miller1b06dc32006-08-31 03:24:41 +10001236 platform_post_fork_child();
Damien Millera1f68402006-08-19 00:31:39 +10001237 startup_pipe = startup_p[1];
1238 close_startup_pipes();
1239 close_listen_socks();
1240 *sock_in = *newsock;
1241 *sock_out = *newsock;
1242 log_init(__progname,
1243 options.log_level,
1244 options.log_facility,
1245 log_stderr);
1246 if (rexec_flag)
1247 close(config_s[0]);
djm@openbsd.org76a24b32019-03-01 02:32:39 +00001248 else {
1249 /*
1250 * Signal parent that the preliminaries
1251 * for this child are complete. For the
1252 * re-exec case, this happens after the
1253 * child has received the rexec state
1254 * from the server.
1255 */
1256 (void)atomicio(vwrite, startup_pipe,
1257 "\0", 1);
1258 }
1259 return;
Damien Millera1f68402006-08-19 00:31:39 +10001260 }
1261
1262 /* Parent. Stay in the loop. */
Damien Miller1b06dc32006-08-31 03:24:41 +10001263 platform_post_fork_parent(pid);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001264 if (pid == -1)
Damien Millera1f68402006-08-19 00:31:39 +10001265 error("fork: %.100s", strerror(errno));
1266 else
1267 debug("Forked child %ld.", (long)pid);
1268
1269 close(startup_p[1]);
1270
1271 if (rexec_flag) {
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00001272 send_rexec_state(config_s[0], cfg);
Damien Millera1f68402006-08-19 00:31:39 +10001273 close(config_s[0]);
1274 close(config_s[1]);
1275 }
Damien Millera1f68402006-08-19 00:31:39 +10001276 close(*newsock);
1277
1278 /*
1279 * Ensure that our random state differs
1280 * from that of the child
1281 */
1282 arc4random_stir();
Damien Miller045bda52013-09-14 09:44:37 +10001283 arc4random_buf(rnd, sizeof(rnd));
Damien Miller72ef7c12015-01-15 02:21:31 +11001284#ifdef WITH_OPENSSL
Damien Miller045bda52013-09-14 09:44:37 +10001285 RAND_seed(rnd, sizeof(rnd));
Damien Miller07889c72015-11-14 18:44:49 +11001286 if ((RAND_bytes((u_char *)rnd, 1)) != 1)
1287 fatal("%s: RAND_bytes failed", __func__);
Damien Miller72ef7c12015-01-15 02:21:31 +11001288#endif
Damien Miller1d2c4562014-02-04 11:18:20 +11001289 explicit_bzero(rnd, sizeof(rnd));
Damien Millera1f68402006-08-19 00:31:39 +10001290 }
Damien Millera1f68402006-08-19 00:31:39 +10001291 }
1292}
1293
djm@openbsd.org95767262016-03-07 19:02:43 +00001294/*
1295 * If IP options are supported, make sure there are none (log and
1296 * return an error if any are found). Basically we are worried about
1297 * source routing; it can be used to pretend you are somebody
1298 * (ip-address) you are not. That itself may be "almost acceptable"
djm@openbsd.org001aa552018-04-10 00:10:49 +00001299 * under certain circumstances, but rhosts authentication is useless
djm@openbsd.org95767262016-03-07 19:02:43 +00001300 * if source routing is accepted. Notice also that if we just dropped
1301 * source routing here, the other side could use IP spoofing to do
1302 * rest of the interaction and could still bypass security. So we
1303 * exit here if we detect any IP options.
1304 */
1305static void
1306check_ip_options(struct ssh *ssh)
1307{
1308#ifdef IP_OPTIONS
1309 int sock_in = ssh_packet_get_connection_in(ssh);
1310 struct sockaddr_storage from;
djm@openbsd.org95767262016-03-07 19:02:43 +00001311 u_char opts[200];
djm@openbsd.orgdc664d12016-08-28 22:28:12 +00001312 socklen_t i, option_size = sizeof(opts), fromlen = sizeof(from);
djm@openbsd.org95767262016-03-07 19:02:43 +00001313 char text[sizeof(opts) * 3 + 1];
1314
1315 memset(&from, 0, sizeof(from));
1316 if (getpeername(sock_in, (struct sockaddr *)&from,
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001317 &fromlen) == -1)
djm@openbsd.org95767262016-03-07 19:02:43 +00001318 return;
1319 if (from.ss_family != AF_INET)
1320 return;
1321 /* XXX IPv6 options? */
1322
1323 if (getsockopt(sock_in, IPPROTO_IP, IP_OPTIONS, opts,
1324 &option_size) >= 0 && option_size != 0) {
1325 text[0] = '\0';
1326 for (i = 0; i < option_size; i++)
1327 snprintf(text + i*3, sizeof(text) - i*3,
1328 " %2.2x", opts[i]);
1329 fatal("Connection from %.100s port %d with IP opts: %.800s",
1330 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), text);
1331 }
1332 return;
1333#endif /* IP_OPTIONS */
1334}
Damien Millera1f68402006-08-19 00:31:39 +10001335
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00001336/* Set the routing domain for this process */
1337static void
1338set_process_rdomain(struct ssh *ssh, const char *name)
1339{
Damien Miller43c29bb2017-10-25 13:10:59 +11001340#if defined(HAVE_SYS_SET_PROCESS_RDOMAIN)
1341 if (name == NULL)
1342 return; /* default */
1343
1344 if (strcmp(name, "%D") == 0) {
1345 /* "expands" to routing domain of connection */
1346 if ((name = ssh_packet_rdomain_in(ssh)) == NULL)
1347 return;
1348 }
1349 /* NB. We don't pass 'ssh' to sys_set_process_rdomain() */
1350 return sys_set_process_rdomain(name);
1351#elif defined(__OpenBSD__)
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00001352 int rtable, ortable = getrtable();
1353 const char *errstr;
1354
1355 if (name == NULL)
1356 return; /* default */
1357
1358 if (strcmp(name, "%D") == 0) {
1359 /* "expands" to routing domain of connection */
1360 if ((name = ssh_packet_rdomain_in(ssh)) == NULL)
1361 return;
1362 }
1363
1364 rtable = (int)strtonum(name, 0, 255, &errstr);
1365 if (errstr != NULL) /* Shouldn't happen */
1366 fatal("Invalid routing domain \"%s\": %s", name, errstr);
1367 if (rtable != ortable && setrtable(rtable) != 0)
1368 fatal("Unable to set routing domain %d: %s",
1369 rtable, strerror(errno));
1370 debug("%s: set routing domain %d (was %d)", __func__, rtable, ortable);
Damien Miller43c29bb2017-10-25 13:10:59 +11001371#else /* defined(__OpenBSD__) */
1372 fatal("Unable to set routing domain: not supported in this platform");
1373#endif
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00001374}
1375
dtucker@openbsd.orge9d910b2018-04-13 03:57:26 +00001376static void
1377accumulate_host_timing_secret(struct sshbuf *server_cfg,
djm@openbsd.org4f7a56d2019-06-21 04:21:04 +00001378 struct sshkey *key)
dtucker@openbsd.orge9d910b2018-04-13 03:57:26 +00001379{
1380 static struct ssh_digest_ctx *ctx;
1381 u_char *hash;
1382 size_t len;
1383 struct sshbuf *buf;
1384 int r;
1385
1386 if (ctx == NULL && (ctx = ssh_digest_start(SSH_DIGEST_SHA512)) == NULL)
1387 fatal("%s: ssh_digest_start", __func__);
1388 if (key == NULL) { /* finalize */
1389 /* add server config in case we are using agent for host keys */
1390 if (ssh_digest_update(ctx, sshbuf_ptr(server_cfg),
1391 sshbuf_len(server_cfg)) != 0)
1392 fatal("%s: ssh_digest_update", __func__);
1393 len = ssh_digest_bytes(SSH_DIGEST_SHA512);
1394 hash = xmalloc(len);
1395 if (ssh_digest_final(ctx, hash, len) != 0)
1396 fatal("%s: ssh_digest_final", __func__);
1397 options.timing_secret = PEEK_U64(hash);
1398 freezero(hash, len);
1399 ssh_digest_free(ctx);
1400 ctx = NULL;
1401 return;
1402 }
1403 if ((buf = sshbuf_new()) == NULL)
1404 fatal("%s could not allocate buffer", __func__);
1405 if ((r = sshkey_private_serialize(key, buf)) != 0)
1406 fatal("sshkey_private_serialize: %s", ssh_err(r));
1407 if (ssh_digest_update(ctx, sshbuf_ptr(buf), sshbuf_len(buf)) != 0)
1408 fatal("%s: ssh_digest_update", __func__);
1409 sshbuf_reset(buf);
1410 sshbuf_free(buf);
1411}
1412
Damien Miller95def091999-11-25 00:26:21 +11001413/*
1414 * Main program for the daemon.
1415 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001416int
1417main(int ac, char **av)
1418{
djm@openbsd.org95767262016-03-07 19:02:43 +00001419 struct ssh *ssh = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001420 extern char *optarg;
1421 extern int optind;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001422 int r, opt, on = 1, already_daemon, remote_port;
Damien Miller386c6a22004-06-30 22:40:20 +10001423 int sock_in = -1, sock_out = -1, newsock = -1;
djm@openbsd.org68af80e2017-10-25 00:19:47 +00001424 const char *remote_ip, *rdomain;
dtucker@openbsd.org15fdfc92015-04-15 23:23:25 +00001425 char *fp, *line, *laddr, *logfile = NULL;
Damien Millera1f68402006-08-19 00:31:39 +10001426 int config_s[2] = { -1 , -1 };
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001427 u_int i, j;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001428 u_int64_t ibytes, obytes;
Damien Miller6ca16c62008-06-16 07:50:58 +10001429 mode_t new_umask;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001430 struct sshkey *key;
1431 struct sshkey *pubkey;
Damien Miller85b45e02013-07-20 13:21:52 +10001432 int keytype;
Darren Tucker3e33cec2003-10-02 16:12:36 +10001433 Authctxt *authctxt;
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001434 struct connection_info *connection_info = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001435
Kevin Steves0ea1d9d2002-04-25 18:17:04 +00001436#ifdef HAVE_SECUREWARE
1437 (void)set_auth_parameters(ac, av);
1438#endif
Damien Miller59d3d5b2003-08-22 09:34:41 +10001439 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001440
Damien Millera8ed44b2003-01-10 09:53:12 +11001441 /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
Damien Millerb8c656e2000-06-28 15:22:41 +10001442 saved_argc = ac;
Darren Tucker17c5d032004-06-25 14:22:23 +10001443 rexec_argc = ac;
Darren Tuckerd8093e42006-05-04 16:24:34 +10001444 saved_argv = xcalloc(ac + 1, sizeof(*saved_argv));
Damien Millere3fa20e2017-10-23 16:25:24 +11001445 for (i = 0; (int)i < ac; i++)
Damien Millera8ed44b2003-01-10 09:53:12 +11001446 saved_argv[i] = xstrdup(av[i]);
Damien Miller04cb5362003-05-15 21:29:10 +10001447 saved_argv[i] = NULL;
Damien Millera8ed44b2003-01-10 09:53:12 +11001448
1449#ifndef HAVE_SETPROCTITLE
1450 /* Prepare for later setproctitle emulation */
1451 compat_init_setproctitle(ac, av);
Damien Millerf2e3e9d2003-06-02 12:15:54 +10001452 av = saved_argv;
Damien Millera8ed44b2003-01-10 09:53:12 +11001453#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001454
Damien Millerbfba3542004-03-22 09:29:57 +11001455 if (geteuid() == 0 && setgroups(0, NULL) == -1)
1456 debug("setgroups(): %.200s", strerror(errno));
1457
Darren Tuckerce321d82005-10-03 18:11:24 +10001458 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1459 sanitise_stdfd();
1460
Damien Miller42c5ec42018-11-23 10:40:06 +11001461 seed_rng();
1462
Damien Miller95def091999-11-25 00:26:21 +11001463 /* Initialize configuration options to their default values. */
1464 initialize_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001465
Damien Miller95def091999-11-25 00:26:21 +11001466 /* Parse command-line arguments. */
djm@openbsd.org3e91b4e2015-05-24 23:39:16 +00001467 while ((opt = getopt(ac, av,
1468 "C:E:b:c:f:g:h:k:o:p:u:46DQRTdeiqrt")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001469 switch (opt) {
Damien Miller34132e52000-01-14 15:45:46 +11001470 case '4':
Darren Tucker0f383232005-01-20 10:57:56 +11001471 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +11001472 break;
1473 case '6':
Darren Tucker0f383232005-01-20 10:57:56 +11001474 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +11001475 break;
Damien Miller95def091999-11-25 00:26:21 +11001476 case 'f':
1477 config_file_name = optarg;
1478 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001479 case 'c':
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001480 servconf_add_hostcert("[command-line]", 0,
1481 &options, optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11001482 break;
Damien Miller95def091999-11-25 00:26:21 +11001483 case 'd':
Darren Tuckere98dfa32003-07-19 19:54:31 +10001484 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +11001485 debug_flag = 1;
1486 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +10001487 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
Damien Millere4340be2000-09-16 13:29:08 +11001488 options.log_level++;
Damien Miller95def091999-11-25 00:26:21 +11001489 break;
Ben Lindstromc72745a2000-12-02 19:03:54 +00001490 case 'D':
1491 no_daemon_flag = 1;
1492 break;
Damien Miller03d4d7e2013-04-23 15:21:06 +10001493 case 'E':
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +00001494 logfile = optarg;
Damien Miller03d4d7e2013-04-23 15:21:06 +10001495 /* FALLTHROUGH */
Ben Lindstrom9fce9f02001-04-11 23:10:09 +00001496 case 'e':
1497 log_stderr = 1;
1498 break;
Damien Miller95def091999-11-25 00:26:21 +11001499 case 'i':
1500 inetd_flag = 1;
1501 break;
Darren Tucker645ab752004-06-25 13:33:20 +10001502 case 'r':
1503 rexec_flag = 0;
1504 break;
1505 case 'R':
1506 rexeced_flag = 1;
1507 inetd_flag = 1;
1508 break;
Damien Miller95def091999-11-25 00:26:21 +11001509 case 'Q':
Ben Lindstromd5390202001-01-29 08:07:43 +00001510 /* ignored */
Damien Miller95def091999-11-25 00:26:21 +11001511 break;
1512 case 'q':
1513 options.log_level = SYSLOG_LEVEL_QUIET;
1514 break;
1515 case 'b':
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +00001516 /* protocol 1, ignored */
Damien Miller95def091999-11-25 00:26:21 +11001517 break;
1518 case 'p':
Damien Miller34132e52000-01-14 15:45:46 +11001519 options.ports_from_cmdline = 1;
Damien Millere4340be2000-09-16 13:29:08 +11001520 if (options.num_ports >= MAX_PORTS) {
1521 fprintf(stderr, "too many ports.\n");
1522 exit(1);
1523 }
Ben Lindstrom19066a12001-04-12 23:39:26 +00001524 options.ports[options.num_ports++] = a2port(optarg);
Damien Miller3dc71ad2009-01-28 16:31:22 +11001525 if (options.ports[options.num_ports-1] <= 0) {
Ben Lindstrom19066a12001-04-12 23:39:26 +00001526 fprintf(stderr, "Bad port number.\n");
1527 exit(1);
1528 }
Damien Miller95def091999-11-25 00:26:21 +11001529 break;
1530 case 'g':
Ben Lindstrom1bda4c82001-06-05 19:59:08 +00001531 if ((options.login_grace_time = convtime(optarg)) == -1) {
1532 fprintf(stderr, "Invalid login grace time.\n");
1533 exit(1);
1534 }
Damien Miller95def091999-11-25 00:26:21 +11001535 break;
1536 case 'k':
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +00001537 /* protocol 1, ignored */
Damien Miller95def091999-11-25 00:26:21 +11001538 break;
1539 case 'h':
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001540 servconf_add_hostkey("[command-line]", 0,
djm@openbsd.org928f1232018-11-19 04:12:32 +00001541 &options, optarg, 1);
Damien Miller95def091999-11-25 00:26:21 +11001542 break;
Ben Lindstrom794325a2001-08-06 21:09:07 +00001543 case 't':
1544 test_flag = 1;
1545 break;
Darren Tuckere7140f22008-06-10 23:01:51 +10001546 case 'T':
1547 test_flag = 2;
1548 break;
1549 case 'C':
djm@openbsd.org172a5922019-01-19 21:37:48 +00001550 connection_info = get_connection_info(ssh, 0, 0);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001551 if (parse_server_match_testspec(connection_info,
1552 optarg) == -1)
1553 exit(1);
Darren Tuckere7140f22008-06-10 23:01:51 +10001554 break;
Damien Miller942da032000-08-18 13:59:06 +10001555 case 'u':
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001556 utmp_len = (u_int)strtonum(optarg, 0, HOST_NAME_MAX+1+1, NULL);
1557 if (utmp_len > HOST_NAME_MAX+1) {
Ben Lindstrom41daec72002-07-23 21:15:13 +00001558 fprintf(stderr, "Invalid utmp length.\n");
1559 exit(1);
1560 }
Damien Miller942da032000-08-18 13:59:06 +10001561 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001562 case 'o':
Damien Millerb9997192003-12-17 16:29:22 +11001563 line = xstrdup(optarg);
1564 if (process_server_config_line(&options, line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001565 "command-line", 0, NULL, NULL) != 0)
Damien Miller9f0f5c62001-12-21 14:45:46 +11001566 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +10001567 free(line);
Ben Lindstromade03f62001-12-06 18:22:17 +00001568 break;
Damien Miller95def091999-11-25 00:26:21 +11001569 case '?':
1570 default:
Ben Lindstromade03f62001-12-06 18:22:17 +00001571 usage();
1572 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001573 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001574 }
Darren Tucker645ab752004-06-25 13:33:20 +10001575 if (rexeced_flag || inetd_flag)
1576 rexec_flag = 0;
djm@openbsd.org2a358622018-11-16 03:26:01 +00001577 if (!test_flag && rexec_flag && !path_absolute(av[0]))
Darren Tucker645ab752004-06-25 13:33:20 +10001578 fatal("sshd re-exec requires execution with an absolute path");
1579 if (rexeced_flag)
Damien Miller035a5b42004-06-26 08:16:31 +10001580 closefrom(REEXEC_MIN_FREE_FD);
1581 else
1582 closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001583
Damien Miller03d4d7e2013-04-23 15:21:06 +10001584 /* If requested, redirect the logs to the specified logfile. */
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +00001585 if (logfile != NULL)
Damien Miller03d4d7e2013-04-23 15:21:06 +10001586 log_redirect_stderr_to(logfile);
Damien Miller34132e52000-01-14 15:45:46 +11001587 /*
1588 * Force logging to stderr until we have loaded the private host
1589 * key (unless started from inetd)
1590 */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001591 log_init(__progname,
Damien Miller5aa5d782002-02-08 22:01:54 +11001592 options.log_level == SYSLOG_LEVEL_NOT_SET ?
1593 SYSLOG_LEVEL_INFO : options.log_level,
1594 options.log_facility == SYSLOG_FACILITY_NOT_SET ?
1595 SYSLOG_FACILITY_AUTH : options.log_facility,
Ben Lindstromc2faa4a2002-11-09 15:50:03 +00001596 log_stderr || !inetd_flag);
Damien Miller34132e52000-01-14 15:45:46 +11001597
Darren Tucker86c093d2004-03-08 22:59:03 +11001598 /*
1599 * Unset KRB5CCNAME, otherwise the user's session may inherit it from
1600 * root's environment
Damien Miller94cf4c82005-07-17 17:04:47 +10001601 */
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001602 if (getenv("KRB5CCNAME") != NULL)
Tim Ricee3609c92012-02-14 10:03:30 -08001603 (void) unsetenv("KRB5CCNAME");
Darren Tucker9dc6c7d2005-02-02 18:30:33 +11001604
Darren Tucker645ab752004-06-25 13:33:20 +10001605 sensitive_data.have_ssh2_key = 0;
1606
Darren Tuckere7140f22008-06-10 23:01:51 +10001607 /*
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001608 * If we're not doing an extended test do not silently ignore connection
1609 * test params.
Darren Tuckere7140f22008-06-10 23:01:51 +10001610 */
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001611 if (test_flag < 2 && connection_info != NULL)
Darren Tuckere7140f22008-06-10 23:01:51 +10001612 fatal("Config test connection parameter (-C) provided without "
1613 "test mode (-T)");
1614
Darren Tucker645ab752004-06-25 13:33:20 +10001615 /* Fetch our configuration */
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00001616 if ((cfg = sshbuf_new()) == NULL)
1617 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.org76a24b32019-03-01 02:32:39 +00001618 if (rexeced_flag) {
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00001619 recv_rexec_state(REEXEC_CONFIG_PASS_FD, cfg);
djm@openbsd.org76a24b32019-03-01 02:32:39 +00001620 if (!debug_flag) {
1621 startup_pipe = dup(REEXEC_STARTUP_PIPE_FD);
1622 close(REEXEC_STARTUP_PIPE_FD);
1623 /*
1624 * Signal parent that this child is at a point where
1625 * they can go away if they have a SIGHUP pending.
1626 */
1627 (void)atomicio(vwrite, startup_pipe, "\0", 1);
1628 }
1629 }
djm@openbsd.orgdbcc6522015-04-27 00:21:21 +00001630 else if (strcasecmp(config_file_name, "none") != 0)
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00001631 load_server_config(config_file_name, cfg);
Darren Tucker645ab752004-06-25 13:33:20 +10001632
Darren Tucker45150472006-07-12 22:34:17 +10001633 parse_server_config(&options, rexeced_flag ? "rexec" : config_file_name,
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00001634 cfg, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001635
Damien Miller95def091999-11-25 00:26:21 +11001636 /* Fill in default values for those options not explicitly set. */
1637 fill_default_server_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001638
Darren Tucker97b1bb52007-03-21 20:38:53 +11001639 /* challenge-response is implemented via keyboard interactive */
1640 if (options.challenge_response_authentication)
1641 options.kbd_interactive_authentication = 1;
1642
Damien Millerd0d10992012-11-04 22:23:14 +11001643 /* Check that options are sensible */
1644 if (options.authorized_keys_command_user == NULL &&
1645 (options.authorized_keys_command != NULL &&
1646 strcasecmp(options.authorized_keys_command, "none") != 0))
1647 fatal("AuthorizedKeysCommand set without "
1648 "AuthorizedKeysCommandUser");
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +00001649 if (options.authorized_principals_command_user == NULL &&
1650 (options.authorized_principals_command != NULL &&
1651 strcasecmp(options.authorized_principals_command, "none") != 0))
1652 fatal("AuthorizedPrincipalsCommand set without "
1653 "AuthorizedPrincipalsCommandUser");
Damien Millerd0d10992012-11-04 22:23:14 +11001654
Damien Millera6e3f012012-11-04 23:21:40 +11001655 /*
1656 * Check whether there is any path through configured auth methods.
1657 * Unfortunately it is not possible to verify this generally before
1658 * daemonisation in the presence of Match block, but this catches
1659 * and warns for trivial misconfigurations that could break login.
1660 */
1661 if (options.num_auth_methods != 0) {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001662 for (i = 0; i < options.num_auth_methods; i++) {
1663 if (auth2_methods_valid(options.auth_methods[i],
Damien Millera6e3f012012-11-04 23:21:40 +11001664 1) == 0)
1665 break;
1666 }
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001667 if (i >= options.num_auth_methods)
Damien Millera6e3f012012-11-04 23:21:40 +11001668 fatal("AuthenticationMethods cannot be satisfied by "
1669 "enabled authentication methods");
1670 }
1671
Damien Miller95def091999-11-25 00:26:21 +11001672 /* Check that there are no remaining arguments. */
1673 if (optind < ac) {
1674 fprintf(stderr, "Extra argument %s.\n", av[optind]);
1675 exit(1);
1676 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001677
Damien Miller22e8a1e2013-02-12 11:04:48 +11001678 debug("sshd version %s, %s", SSH_VERSION,
Damien Miller1f0311c2014-05-15 14:24:09 +10001679#ifdef WITH_OPENSSL
djm@openbsd.orga65784c2018-10-23 05:56:35 +00001680 OpenSSL_version(OPENSSL_VERSION)
Damien Miller1f0311c2014-05-15 14:24:09 +10001681#else
1682 "without OpenSSL"
1683#endif
1684 );
Damien Miller2ccf6611999-11-15 15:25:10 +11001685
Darren Tuckerdf0e4382006-11-07 11:28:40 +11001686 /* Store privilege separation user for later use if required. */
Darren Tuckerd13281f2017-03-29 12:39:39 +11001687 privsep_chroot = use_privsep && (getuid() == 0 || geteuid() == 0);
Darren Tuckerdf0e4382006-11-07 11:28:40 +11001688 if ((privsep_pw = getpwnam(SSH_PRIVSEP_USER)) == NULL) {
Darren Tuckerd13281f2017-03-29 12:39:39 +11001689 if (privsep_chroot || options.kerberos_authentication)
Darren Tuckerdf0e4382006-11-07 11:28:40 +11001690 fatal("Privilege separation user %s does not exist",
1691 SSH_PRIVSEP_USER);
1692 } else {
Darren Tuckerdf0e4382006-11-07 11:28:40 +11001693 privsep_pw = pwcopy(privsep_pw);
djm@openbsd.orgd6364f62018-01-23 05:01:15 +00001694 freezero(privsep_pw->pw_passwd, strlen(privsep_pw->pw_passwd));
Darren Tuckerdf0e4382006-11-07 11:28:40 +11001695 privsep_pw->pw_passwd = xstrdup("*");
1696 }
Damien Miller6433df02006-09-07 10:36:43 +10001697 endpwent();
1698
Damien Miller85b45e02013-07-20 13:21:52 +10001699 /* load host keys */
Damien Miller07d86be2006-03-26 14:19:21 +11001700 sensitive_data.host_keys = xcalloc(options.num_host_key_files,
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001701 sizeof(struct sshkey *));
Damien Miller85b45e02013-07-20 13:21:52 +10001702 sensitive_data.host_pubkeys = xcalloc(options.num_host_key_files,
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001703 sizeof(struct sshkey *));
Damien Miller85b45e02013-07-20 13:21:52 +10001704
1705 if (options.host_key_agent) {
1706 if (strcmp(options.host_key_agent, SSH_AUTHSOCKET_ENV_NAME))
1707 setenv(SSH_AUTHSOCKET_ENV_NAME,
1708 options.host_key_agent, 1);
djm@openbsd.org83f8ffa2015-01-17 18:53:34 +00001709 if ((r = ssh_get_authentication_socket(NULL)) == 0)
1710 have_agent = 1;
1711 else
1712 error("Could not connect to agent \"%s\": %s",
1713 options.host_key_agent, ssh_err(r));
Damien Miller85b45e02013-07-20 13:21:52 +10001714 }
Damien Millereba71ba2000-04-29 23:57:08 +10001715
Damien Miller9f0f5c62001-12-21 14:45:46 +11001716 for (i = 0; i < options.num_host_key_files; i++) {
djm@openbsd.org928f1232018-11-19 04:12:32 +00001717 int ll = options.host_key_file_userprovided[i] ?
1718 SYSLOG_LEVEL_ERROR : SYSLOG_LEVEL_DEBUG1;
1719
djm@openbsd.org161cf412014-12-22 07:55:51 +00001720 if (options.host_key_files[i] == NULL)
1721 continue;
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001722 if ((r = sshkey_load_private(options.host_key_files[i], "",
1723 &key, NULL)) != 0 && r != SSH_ERR_SYSTEM_ERROR)
djm@openbsd.org928f1232018-11-19 04:12:32 +00001724 do_log2(ll, "Unable to load host key \"%s\": %s",
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001725 options.host_key_files[i], ssh_err(r));
djm@openbsd.org4f7a56d2019-06-21 04:21:04 +00001726 if (r == 0 && (r = sshkey_shield_private(key)) != 0) {
1727 do_log2(ll, "Unable to shield host key \"%s\": %s",
1728 options.host_key_files[i], ssh_err(r));
1729 sshkey_free(key);
1730 key = NULL;
1731 }
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001732 if ((r = sshkey_load_public(options.host_key_files[i],
1733 &pubkey, NULL)) != 0 && r != SSH_ERR_SYSTEM_ERROR)
djm@openbsd.org928f1232018-11-19 04:12:32 +00001734 do_log2(ll, "Unable to load host key \"%s\": %s",
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001735 options.host_key_files[i], ssh_err(r));
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001736 if (pubkey == NULL && key != NULL)
djm@openbsd.org482d23b2018-09-13 02:08:33 +00001737 if ((r = sshkey_from_private(key, &pubkey)) != 0)
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001738 fatal("Could not demote key: \"%s\": %s",
1739 options.host_key_files[i], ssh_err(r));
Ben Lindstromd0fca422001-03-26 13:44:06 +00001740 sensitive_data.host_keys[i] = key;
Damien Miller85b45e02013-07-20 13:21:52 +10001741 sensitive_data.host_pubkeys[i] = pubkey;
1742
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00001743 if (key == NULL && pubkey != NULL && have_agent) {
djm@openbsd.orgce63c4b2015-02-16 22:30:03 +00001744 debug("will rely on agent for hostkey %s",
1745 options.host_key_files[i]);
Damien Miller85b45e02013-07-20 13:21:52 +10001746 keytype = pubkey->type;
1747 } else if (key != NULL) {
1748 keytype = key->type;
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00001749 accumulate_host_timing_secret(cfg, key);
Damien Miller85b45e02013-07-20 13:21:52 +10001750 } else {
djm@openbsd.org928f1232018-11-19 04:12:32 +00001751 do_log2(ll, "Unable to load host key: %s",
Ben Lindstrom15f33862001-04-16 02:00:02 +00001752 options.host_key_files[i]);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001753 sensitive_data.host_keys[i] = NULL;
Damien Miller85b45e02013-07-20 13:21:52 +10001754 sensitive_data.host_pubkeys[i] = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001755 continue;
1756 }
Damien Miller85b45e02013-07-20 13:21:52 +10001757
1758 switch (keytype) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001759 case KEY_RSA:
1760 case KEY_DSA:
Damien Millereb8b60e2010-08-31 22:41:14 +10001761 case KEY_ECDSA:
Damien Miller5be9d9e2013-12-07 11:24:01 +11001762 case KEY_ED25519:
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00001763 case KEY_XMSS:
djm@openbsd.org6049a542015-01-31 20:30:05 +00001764 if (have_agent || key != NULL)
1765 sensitive_data.have_ssh2_key = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001766 break;
1767 }
djm@openbsd.org6049a542015-01-31 20:30:05 +00001768 if ((fp = sshkey_fingerprint(pubkey, options.fingerprint_hash,
1769 SSH_FP_DEFAULT)) == NULL)
1770 fatal("sshkey_fingerprint failed");
1771 debug("%s host key #%d: %s %s",
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00001772 key ? "private" : "agent", i, sshkey_ssh_name(pubkey), fp);
djm@openbsd.org6049a542015-01-31 20:30:05 +00001773 free(fp);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001774 }
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00001775 accumulate_host_timing_secret(cfg, NULL);
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00001776 if (!sensitive_data.have_ssh2_key) {
Damien Miller996acd22003-04-09 20:59:48 +10001777 logit("sshd: no hostkeys available -- exiting.");
Damien Miller95def091999-11-25 00:26:21 +11001778 exit(1);
1779 }
Damien Miller95def091999-11-25 00:26:21 +11001780
Damien Miller0a80ca12010-02-27 07:55:05 +11001781 /*
1782 * Load certificates. They are stored in an array at identical
1783 * indices to the public keys that they relate to.
1784 */
1785 sensitive_data.host_certificates = xcalloc(options.num_host_key_files,
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001786 sizeof(struct sshkey *));
Damien Miller0a80ca12010-02-27 07:55:05 +11001787 for (i = 0; i < options.num_host_key_files; i++)
1788 sensitive_data.host_certificates[i] = NULL;
1789
1790 for (i = 0; i < options.num_host_cert_files; i++) {
djm@openbsd.org161cf412014-12-22 07:55:51 +00001791 if (options.host_cert_files[i] == NULL)
1792 continue;
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001793 if ((r = sshkey_load_public(options.host_cert_files[i],
1794 &key, NULL)) != 0) {
1795 error("Could not load host certificate \"%s\": %s",
1796 options.host_cert_files[i], ssh_err(r));
Damien Miller0a80ca12010-02-27 07:55:05 +11001797 continue;
1798 }
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001799 if (!sshkey_is_cert(key)) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001800 error("Certificate file is not a certificate: %s",
1801 options.host_cert_files[i]);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001802 sshkey_free(key);
Damien Miller0a80ca12010-02-27 07:55:05 +11001803 continue;
1804 }
1805 /* Find matching private key */
1806 for (j = 0; j < options.num_host_key_files; j++) {
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001807 if (sshkey_equal_public(key,
Damien Miller0a80ca12010-02-27 07:55:05 +11001808 sensitive_data.host_keys[j])) {
1809 sensitive_data.host_certificates[j] = key;
1810 break;
1811 }
1812 }
1813 if (j >= options.num_host_key_files) {
1814 error("No matching private key for certificate: %s",
1815 options.host_cert_files[i]);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001816 sshkey_free(key);
Damien Miller0a80ca12010-02-27 07:55:05 +11001817 continue;
1818 }
1819 sensitive_data.host_certificates[j] = key;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001820 debug("host certificate: #%u type %d %s", j, key->type,
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001821 sshkey_type(key));
Damien Miller0a80ca12010-02-27 07:55:05 +11001822 }
Damien Miller1f0311c2014-05-15 14:24:09 +10001823
Darren Tuckerd13281f2017-03-29 12:39:39 +11001824 if (privsep_chroot) {
Ben Lindstroma26ea632002-06-06 20:46:25 +00001825 struct stat st;
1826
Ben Lindstroma26ea632002-06-06 20:46:25 +00001827 if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
1828 (S_ISDIR(st.st_mode) == 0))
1829 fatal("Missing privilege separation directory: %s",
1830 _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstrom59627352002-06-27 18:02:21 +00001831
1832#ifdef HAVE_CYGWIN
1833 if (check_ntsec(_PATH_PRIVSEP_CHROOT_DIR) &&
1834 (st.st_uid != getuid () ||
1835 (st.st_mode & (S_IWGRP|S_IWOTH)) != 0))
1836#else
Ben Lindstrom2dfacb32002-06-23 00:33:47 +00001837 if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
Ben Lindstrom59627352002-06-27 18:02:21 +00001838#endif
Damien Miller180fc5b2003-02-24 11:50:18 +11001839 fatal("%s must be owned by root and not group or "
1840 "world-writable.", _PATH_PRIVSEP_CHROOT_DIR);
Ben Lindstroma26ea632002-06-06 20:46:25 +00001841 }
1842
Darren Tuckere7140f22008-06-10 23:01:51 +10001843 if (test_flag > 1) {
djm@openbsd.org@openbsd.org548d3a62017-11-14 00:45:29 +00001844 /*
1845 * If no connection info was provided by -C then use
1846 * use a blank one that will cause no predicate to match.
1847 */
1848 if (connection_info == NULL)
djm@openbsd.org172a5922019-01-19 21:37:48 +00001849 connection_info = get_connection_info(ssh, 0, 0);
dtucker@openbsd.orge826bbc2019-04-18 18:56:16 +00001850 connection_info->test = 1;
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001851 parse_server_match_config(&options, connection_info);
Darren Tuckere7140f22008-06-10 23:01:51 +10001852 dump_config(&options);
1853 }
1854
Ben Lindstrom794325a2001-08-06 21:09:07 +00001855 /* Configuration looks good, so exit if in test mode. */
1856 if (test_flag)
1857 exit(0);
1858
Damien Miller87aea252002-05-10 12:20:24 +10001859 /*
1860 * Clear out any supplemental groups we may have inherited. This
1861 * prevents inadvertent creation of files with bad modes (in the
Damien Millera8e06ce2003-11-21 23:48:55 +11001862 * portable version at least, it's certainly possible for PAM
1863 * to create a file, and we can't control the code in every
Damien Miller87aea252002-05-10 12:20:24 +10001864 * module which might be used).
1865 */
1866 if (setgroups(0, NULL) < 0)
1867 debug("setgroups() failed: %.200s", strerror(errno));
1868
Darren Tucker645ab752004-06-25 13:33:20 +10001869 if (rexec_flag) {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001870 if (rexec_argc < 0)
1871 fatal("rexec_argc %d < 0", rexec_argc);
Damien Miller07d86be2006-03-26 14:19:21 +11001872 rexec_argv = xcalloc(rexec_argc + 2, sizeof(char *));
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001873 for (i = 0; i < (u_int)rexec_argc; i++) {
Darren Tucker645ab752004-06-25 13:33:20 +10001874 debug("rexec_argv[%d]='%s'", i, saved_argv[i]);
1875 rexec_argv[i] = saved_argv[i];
1876 }
1877 rexec_argv[rexec_argc] = "-R";
1878 rexec_argv[rexec_argc + 1] = NULL;
1879 }
1880
Damien Miller6ca16c62008-06-16 07:50:58 +10001881 /* Ensure that umask disallows at least group and world write */
1882 new_umask = umask(0077) | 0022;
1883 (void) umask(new_umask);
1884
Damien Millereba71ba2000-04-29 23:57:08 +10001885 /* Initialize the log (it is reinitialized below in case we forked). */
Darren Tuckerea7c8122005-01-20 11:03:08 +11001886 if (debug_flag && (!inetd_flag || rexeced_flag))
Damien Miller95def091999-11-25 00:26:21 +11001887 log_stderr = 1;
Kevin Stevesec84dc12000-12-13 17:45:15 +00001888 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001889
Damien Millereba71ba2000-04-29 23:57:08 +10001890 /*
dtucker@openbsd.org7fc47662016-11-30 00:28:31 +00001891 * If not in debugging mode, not started from inetd and not already
1892 * daemonized (eg re-exec via SIGHUP), disconnect from the controlling
1893 * terminal, and fork. The original process exits.
Damien Millereba71ba2000-04-29 23:57:08 +10001894 */
dtucker@openbsd.org7fc47662016-11-30 00:28:31 +00001895 already_daemon = daemonized();
1896 if (!(debug_flag || inetd_flag || no_daemon_flag || already_daemon)) {
dtucker@openbsd.orgb0899ee2016-11-29 03:54:50 +00001897
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001898 if (daemon(0, 0) == -1)
Damien Miller95def091999-11-25 00:26:21 +11001899 fatal("daemon() failed: %.200s", strerror(errno));
1900
dtucker@openbsd.orgb0899ee2016-11-29 03:54:50 +00001901 disconnect_controlling_tty();
Damien Miller95def091999-11-25 00:26:21 +11001902 }
1903 /* Reinitialize the log (because of the fork above). */
Kevin Stevesec84dc12000-12-13 17:45:15 +00001904 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Miller95def091999-11-25 00:26:21 +11001905
Damien Miller95def091999-11-25 00:26:21 +11001906 /* Chdir to the root directory so that the current disk can be
1907 unmounted if desired. */
Darren Tuckerdbee3082013-05-16 20:32:29 +10001908 if (chdir("/") == -1)
1909 error("chdir(\"/\"): %s", strerror(errno));
Damien Miller9f0f5c62001-12-21 14:45:46 +11001910
Ben Lindstromde71cda2001-03-24 00:43:26 +00001911 /* ignore SIGPIPE */
1912 signal(SIGPIPE, SIG_IGN);
Damien Miller95def091999-11-25 00:26:21 +11001913
Damien Millera1f68402006-08-19 00:31:39 +10001914 /* Get a connection, either from inetd or a listening TCP socket */
Damien Miller95def091999-11-25 00:26:21 +11001915 if (inetd_flag) {
Damien Millera1f68402006-08-19 00:31:39 +10001916 server_accept_inetd(&sock_in, &sock_out);
Damien Miller95def091999-11-25 00:26:21 +11001917 } else {
Darren Tuckerc8802aa2009-12-08 13:39:48 +11001918 platform_pre_listen();
Damien Millera1f68402006-08-19 00:31:39 +10001919 server_listen();
Damien Miller34132e52000-01-14 15:45:46 +11001920
Ben Lindstrom98097862001-06-25 05:10:20 +00001921 signal(SIGHUP, sighup_handler);
Damien Millera1f68402006-08-19 00:31:39 +10001922 signal(SIGCHLD, main_sigchld_handler);
Ben Lindstrom98097862001-06-25 05:10:20 +00001923 signal(SIGTERM, sigterm_handler);
1924 signal(SIGQUIT, sigterm_handler);
1925
Damien Millera1f68402006-08-19 00:31:39 +10001926 /*
1927 * Write out the pid file after the sigterm handler
1928 * is setup and the listen sockets are bound
1929 */
dtucker@openbsd.orgf2398eb2016-12-04 22:27:25 +00001930 if (options.pid_file != NULL && !debug_flag) {
Damien Millera1f68402006-08-19 00:31:39 +10001931 FILE *f = fopen(options.pid_file, "w");
1932
Darren Tuckere5327042003-07-03 13:40:44 +10001933 if (f == NULL) {
1934 error("Couldn't create pid file \"%s\": %s",
1935 options.pid_file, strerror(errno));
1936 } else {
Ben Lindstromce0f6342002-06-11 16:42:49 +00001937 fprintf(f, "%ld\n", (long) getpid());
Damien Miller95def091999-11-25 00:26:21 +11001938 fclose(f);
1939 }
1940 }
Damien Miller95def091999-11-25 00:26:21 +11001941
Damien Millera1f68402006-08-19 00:31:39 +10001942 /* Accept a connection and return in a forked child */
1943 server_accept_loop(&sock_in, &sock_out,
1944 &newsock, config_s);
Damien Miller95def091999-11-25 00:26:21 +11001945 }
1946
1947 /* This is the child processing a new connection. */
Damien Miller57aae982004-03-08 23:11:25 +11001948 setproctitle("%s", "[accepted]");
Damien Miller95def091999-11-25 00:26:21 +11001949
Darren Tucker6832b832004-08-12 22:36:51 +10001950 /*
1951 * Create a new session and process group since the 4.4BSD
1952 * setlogin() affects the entire process group. We don't
1953 * want the child to be able to affect the parent.
1954 */
1955#if !defined(SSHD_ACQUIRES_CTTY)
1956 /*
1957 * If setsid is called, on some platforms sshd will later acquire a
1958 * controlling terminal which will result in "could not set
1959 * controlling tty" errors.
1960 */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001961 if (!debug_flag && !inetd_flag && setsid() == -1)
Darren Tucker6832b832004-08-12 22:36:51 +10001962 error("setsid: %.100s", strerror(errno));
1963#endif
1964
Darren Tucker645ab752004-06-25 13:33:20 +10001965 if (rexec_flag) {
1966 int fd;
1967
Damien Miller035a5b42004-06-26 08:16:31 +10001968 debug("rexec start in %d out %d newsock %d pipe %d sock %d",
1969 sock_in, sock_out, newsock, startup_pipe, config_s[0]);
Darren Tucker645ab752004-06-25 13:33:20 +10001970 dup2(newsock, STDIN_FILENO);
1971 dup2(STDIN_FILENO, STDOUT_FILENO);
1972 if (startup_pipe == -1)
Damien Miller035a5b42004-06-26 08:16:31 +10001973 close(REEXEC_STARTUP_PIPE_FD);
Damien Miller61ee4d62013-10-15 11:56:47 +11001974 else if (startup_pipe != REEXEC_STARTUP_PIPE_FD) {
Damien Miller035a5b42004-06-26 08:16:31 +10001975 dup2(startup_pipe, REEXEC_STARTUP_PIPE_FD);
Damien Miller61ee4d62013-10-15 11:56:47 +11001976 close(startup_pipe);
1977 startup_pipe = REEXEC_STARTUP_PIPE_FD;
1978 }
Darren Tucker645ab752004-06-25 13:33:20 +10001979
Damien Miller035a5b42004-06-26 08:16:31 +10001980 dup2(config_s[1], REEXEC_CONFIG_PASS_FD);
Darren Tucker645ab752004-06-25 13:33:20 +10001981 close(config_s[1]);
Damien Miller035a5b42004-06-26 08:16:31 +10001982
Darren Tucker645ab752004-06-25 13:33:20 +10001983 execv(rexec_argv[0], rexec_argv);
1984
1985 /* Reexec has failed, fall back and continue */
1986 error("rexec of %s failed: %s", rexec_argv[0], strerror(errno));
Damien Miller035a5b42004-06-26 08:16:31 +10001987 recv_rexec_state(REEXEC_CONFIG_PASS_FD, NULL);
Darren Tucker645ab752004-06-25 13:33:20 +10001988 log_init(__progname, options.log_level,
1989 options.log_facility, log_stderr);
1990
1991 /* Clean up fds */
Damien Miller035a5b42004-06-26 08:16:31 +10001992 close(REEXEC_CONFIG_PASS_FD);
1993 newsock = sock_out = sock_in = dup(STDIN_FILENO);
Darren Tucker645ab752004-06-25 13:33:20 +10001994 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1995 dup2(fd, STDIN_FILENO);
1996 dup2(fd, STDOUT_FILENO);
1997 if (fd > STDERR_FILENO)
1998 close(fd);
1999 }
Damien Miller035a5b42004-06-26 08:16:31 +10002000 debug("rexec cleanup in %d out %d newsock %d pipe %d sock %d",
2001 sock_in, sock_out, newsock, startup_pipe, config_s[0]);
Darren Tucker645ab752004-06-25 13:33:20 +10002002 }
2003
Damien Miller133d9d32010-01-30 17:30:04 +11002004 /* Executed child processes don't need these. */
2005 fcntl(sock_out, F_SETFD, FD_CLOEXEC);
2006 fcntl(sock_in, F_SETFD, FD_CLOEXEC);
2007
Damien Miller5428f641999-11-25 11:54:57 +11002008 /*
2009 * Disable the key regeneration alarm. We will not regenerate the
2010 * key since we are no longer in a position to give it to anyone. We
2011 * will not restart on SIGHUP since it no longer makes sense.
2012 */
Damien Miller95def091999-11-25 00:26:21 +11002013 alarm(0);
2014 signal(SIGALRM, SIG_DFL);
2015 signal(SIGHUP, SIG_DFL);
2016 signal(SIGTERM, SIG_DFL);
2017 signal(SIGQUIT, SIG_DFL);
2018 signal(SIGCHLD, SIG_DFL);
Damien Miller4e0f5e12000-08-29 11:05:50 +11002019 signal(SIGINT, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +11002020
Damien Miller5428f641999-11-25 11:54:57 +11002021 /*
2022 * Register our connection. This turns encryption off because we do
2023 * not have a key.
2024 */
djm@openbsd.org6350e032019-01-19 21:42:30 +00002025 if ((ssh = ssh_packet_set_connection(NULL, sock_in, sock_out)) == NULL)
2026 fatal("Unable to create connection");
djm@openbsd.org04c091f2019-01-19 21:43:56 +00002027 the_active_state = ssh;
djm@openbsd.org6350e032019-01-19 21:42:30 +00002028 ssh_packet_set_server(ssh);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002029
djm@openbsd.org95767262016-03-07 19:02:43 +00002030 check_ip_options(ssh);
Damien Miller95def091999-11-25 00:26:21 +11002031
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002032 /* Prepare the channels layer */
2033 channel_init_channels(ssh);
2034 channel_set_af(ssh, options.address_family);
2035 process_permitopen(ssh, &options);
2036
Damien Miller4f1d6b22005-05-26 11:59:32 +10002037 /* Set SO_KEEPALIVE if requested. */
djm@openbsd.org6350e032019-01-19 21:42:30 +00002038 if (options.tcp_keep_alive && ssh_packet_connection_is_on_socket(ssh) &&
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002039 setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) == -1)
Damien Miller4f1d6b22005-05-26 11:59:32 +10002040 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
2041
djm@openbsd.org95767262016-03-07 19:02:43 +00002042 if ((remote_port = ssh_remote_port(ssh)) < 0) {
2043 debug("ssh_remote_port failed");
Damien Miller677257f2005-06-17 12:55:03 +10002044 cleanup_exit(255);
2045 }
Damien Miller4d3fd542005-11-05 15:13:24 +11002046
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002047 if (options.routing_domain != NULL)
2048 set_process_rdomain(ssh, options.routing_domain);
2049
Damien Miller4d3fd542005-11-05 15:13:24 +11002050 /*
Damien Millereb13e552006-06-13 13:03:53 +10002051 * The rest of the code depends on the fact that
djm@openbsd.org95767262016-03-07 19:02:43 +00002052 * ssh_remote_ipaddr() caches the remote ip, even if
Damien Millereb13e552006-06-13 13:03:53 +10002053 * the socket goes away.
2054 */
djm@openbsd.org95767262016-03-07 19:02:43 +00002055 remote_ip = ssh_remote_ipaddr(ssh);
Damien Miller95def091999-11-25 00:26:21 +11002056
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11002057#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11002058 audit_connection_from(remote_ip, remote_port);
2059#endif
Damien Miller6a4a4b92001-11-12 11:07:11 +11002060
djm@openbsd.org68af80e2017-10-25 00:19:47 +00002061 rdomain = ssh_packet_rdomain_in(ssh);
2062
Damien Miller95def091999-11-25 00:26:21 +11002063 /* Log the connection. */
dtucker@openbsd.org15fdfc92015-04-15 23:23:25 +00002064 laddr = get_local_ipaddr(sock_in);
djm@openbsd.org@openbsd.orgb77c29a2017-10-27 00:18:41 +00002065 verbose("Connection from %s port %d on %s port %d%s%s%s",
djm@openbsd.org68af80e2017-10-25 00:19:47 +00002066 remote_ip, remote_port, laddr, ssh_local_port(ssh),
djm@openbsd.org@openbsd.orgb77c29a2017-10-27 00:18:41 +00002067 rdomain == NULL ? "" : " rdomain \"",
2068 rdomain == NULL ? "" : rdomain,
2069 rdomain == NULL ? "" : "\"");
dtucker@openbsd.org15fdfc92015-04-15 23:23:25 +00002070 free(laddr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002071
Damien Miller5428f641999-11-25 11:54:57 +11002072 /*
Damien Miller788f2122005-11-05 15:14:59 +11002073 * We don't want to listen forever unless the other side
Damien Miller5428f641999-11-25 11:54:57 +11002074 * successfully authenticates itself. So we set up an alarm which is
2075 * cleared after successful authentication. A limit of zero
Damien Miller788f2122005-11-05 15:14:59 +11002076 * indicates no limit. Note that we don't set the alarm in debugging
Damien Miller5428f641999-11-25 11:54:57 +11002077 * mode; it is just annoying to have the server exit just when you
2078 * are about to discover the bug.
2079 */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +00002080 signal(SIGALRM, grace_alarm_handler);
Damien Miller95def091999-11-25 00:26:21 +11002081 if (!debug_flag)
2082 alarm(options.login_grace_time);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002083
djm@openbsd.org0a843d92018-12-27 03:25:24 +00002084 if (kex_exchange_identification(ssh, -1, options.version_addendum) != 0)
2085 cleanup_exit(255); /* error already logged */
2086
djm@openbsd.org6350e032019-01-19 21:42:30 +00002087 ssh_packet_set_nonblocking(ssh);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002088
Darren Tucker3e33cec2003-10-02 16:12:36 +10002089 /* allocate authentication context */
Damien Miller07d86be2006-03-26 14:19:21 +11002090 authctxt = xcalloc(1, sizeof(*authctxt));
djm@openbsd.org6350e032019-01-19 21:42:30 +00002091 ssh->authctxt = authctxt;
Darren Tucker3e33cec2003-10-02 16:12:36 +10002092
Damien Miller120a1ec2018-07-10 19:39:52 +10002093 authctxt->loginmsg = loginmsg;
Darren Tuckerf3bb4342005-03-31 21:39:25 +10002094
Darren Tucker3e33cec2003-10-02 16:12:36 +10002095 /* XXX global for cleanup, access from other modules */
2096 the_authctxt = authctxt;
2097
djm@openbsd.org7c856852018-03-03 03:15:51 +00002098 /* Set default key authentication options */
2099 if ((auth_opts = sshauthopt_new_with_keys_defaults()) == NULL)
2100 fatal("allocation failed");
2101
Darren Tucker5c14c732005-01-24 21:55:49 +11002102 /* prepare buffer to collect messages to display to user after login */
markus@openbsd.org2808d182018-07-09 21:26:02 +00002103 if ((loginmsg = sshbuf_new()) == NULL)
2104 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckercd70e1b2010-03-07 23:05:17 +11002105 auth_debug_reset();
Darren Tucker5c14c732005-01-24 21:55:49 +11002106
Damien Miller85b45e02013-07-20 13:21:52 +10002107 if (use_privsep) {
djm@openbsd.org6350e032019-01-19 21:42:30 +00002108 if (privsep_preauth(ssh) == 1)
Ben Lindstrom943481c2002-03-22 03:43:46 +00002109 goto authenticated;
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00002110 } else if (have_agent) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00002111 if ((r = ssh_get_authentication_socket(&auth_sock)) != 0) {
2112 error("Unable to get agent socket: %s", ssh_err(r));
djm@openbsd.org83f8ffa2015-01-17 18:53:34 +00002113 have_agent = 0;
djm@openbsd.org141efe42015-01-14 20:05:27 +00002114 }
2115 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002116
Damien Miller396691a2000-01-20 22:44:08 +11002117 /* perform the key exchange */
Damien Miller396691a2000-01-20 22:44:08 +11002118 /* authenticate user and start session */
djm@openbsd.org6350e032019-01-19 21:42:30 +00002119 do_ssh2_kex(ssh);
Damien Miller5ebce132019-01-20 09:44:53 +11002120 do_authentication2(ssh);
markus@openbsd.org6cb6dcf2016-08-13 17:47:40 +00002121
Ben Lindstrom943481c2002-03-22 03:43:46 +00002122 /*
2123 * If we use privilege separation, the unprivileged child transfers
2124 * the current keystate and exits
2125 */
2126 if (use_privsep) {
djm@openbsd.org04c091f2019-01-19 21:43:56 +00002127 mm_send_keystate(ssh, pmonitor);
djm@openbsd.org6350e032019-01-19 21:42:30 +00002128 ssh_packet_clear_keys(ssh);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002129 exit(0);
Ben Lindstrom943481c2002-03-22 03:43:46 +00002130 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002131
2132 authenticated:
Damien Miller7bff1a92005-12-24 14:59:12 +11002133 /*
2134 * Cancel the alarm we set to limit the time taken for
2135 * authentication.
2136 */
2137 alarm(0);
2138 signal(SIGALRM, SIG_DFL);
Damien Miller3f8123c2006-08-19 00:32:46 +10002139 authctxt->authenticated = 1;
Damien Miller7bff1a92005-12-24 14:59:12 +11002140 if (startup_pipe != -1) {
2141 close(startup_pipe);
2142 startup_pipe = -1;
2143 }
2144
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11002145#ifdef SSH_AUDIT_EVENTS
Damien Miller9b655dc2019-01-20 14:55:27 +11002146 audit_event(ssh, SSH_AUTH_SUCCESS);
Darren Tucker269a1ea2005-02-03 00:20:53 +11002147#endif
2148
Darren Tucker52358d62008-03-11 22:58:25 +11002149#ifdef GSSAPI
2150 if (options.gss_authentication) {
2151 temporarily_use_uid(authctxt->pw);
2152 ssh_gssapi_storecreds();
2153 restore_uid();
2154 }
2155#endif
2156#ifdef USE_PAM
2157 if (options.use_pam) {
2158 do_pam_setcred(1);
djm@openbsd.org7c856852018-03-03 03:15:51 +00002159 do_pam_session(ssh);
Darren Tucker52358d62008-03-11 22:58:25 +11002160 }
2161#endif
2162
Ben Lindstrom6328ab32002-03-22 02:54:23 +00002163 /*
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002164 * In privilege separation, we fork another child and prepare
2165 * file descriptor passing.
2166 */
2167 if (use_privsep) {
djm@openbsd.org6350e032019-01-19 21:42:30 +00002168 privsep_postauth(ssh, authctxt);
Ben Lindstrom943481c2002-03-22 03:43:46 +00002169 /* the monitor process [priv] will not return */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002170 }
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00002171
djm@openbsd.org6350e032019-01-19 21:42:30 +00002172 ssh_packet_set_timeout(ssh, options.client_alive_interval,
Darren Tucker3fc464e2008-06-13 06:42:45 +10002173 options.client_alive_count_max);
2174
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002175 /* Try to send all our hostkeys to the client */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002176 notify_hostkeys(ssh);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00002177
Darren Tucker3e33cec2003-10-02 16:12:36 +10002178 /* Start session. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002179 do_authenticated(ssh, authctxt);
Ben Lindstrom73ab9ba2002-03-22 01:27:35 +00002180
Damien Miller3a5b0232002-03-13 13:19:42 +11002181 /* The connection has been terminated. */
djm@openbsd.org6350e032019-01-19 21:42:30 +00002182 ssh_packet_get_bytes(ssh, &ibytes, &obytes);
Damien Miller821de0a2011-01-11 17:20:29 +11002183 verbose("Transferred: sent %llu, received %llu bytes",
2184 (unsigned long long)obytes, (unsigned long long)ibytes);
Damien Millerb61f3fc2008-07-11 17:36:48 +10002185
2186 verbose("Closing connection to %.500s port %d", remote_ip, remote_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002187
Damien Millerbeb4ba51999-12-28 15:09:35 +11002188#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +10002189 if (options.use_pam)
2190 finish_pam();
Damien Millerbeb4ba51999-12-28 15:09:35 +11002191#endif /* USE_PAM */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002192
Darren Tucker2b59a6d2005-03-06 22:38:51 +11002193#ifdef SSH_AUDIT_EVENTS
Damien Miller9b655dc2019-01-20 14:55:27 +11002194 PRIVSEP(audit_event(ssh, SSH_CONNECTION_CLOSE));
Darren Tucker2b59a6d2005-03-06 22:38:51 +11002195#endif
2196
djm@openbsd.org6350e032019-01-19 21:42:30 +00002197 ssh_packet_close(ssh);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002198
2199 if (use_privsep)
2200 mm_terminate();
2201
Damien Miller95def091999-11-25 00:26:21 +11002202 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002203}
2204
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00002205int
djm@openbsd.org04c091f2019-01-19 21:43:56 +00002206sshd_hostkey_sign(struct ssh *ssh, struct sshkey *privkey,
2207 struct sshkey *pubkey, u_char **signature, size_t *slenp,
2208 const u_char *data, size_t dlen, const char *alg)
Damien Miller85b45e02013-07-20 13:21:52 +10002209{
djm@openbsd.org141efe42015-01-14 20:05:27 +00002210 int r;
2211
djm@openbsd.org04c091f2019-01-19 21:43:56 +00002212 if (use_privsep) {
2213 if (privkey) {
2214 if (mm_sshkey_sign(ssh, privkey, signature, slenp,
djm@openbsd.org9a14c642019-10-31 21:23:19 +00002215 data, dlen, alg, NULL, ssh->compat) < 0)
djm@openbsd.org04c091f2019-01-19 21:43:56 +00002216 fatal("%s: privkey sign failed", __func__);
2217 } else {
2218 if (mm_sshkey_sign(ssh, pubkey, signature, slenp,
djm@openbsd.org9a14c642019-10-31 21:23:19 +00002219 data, dlen, alg, NULL, ssh->compat) < 0)
djm@openbsd.org04c091f2019-01-19 21:43:56 +00002220 fatal("%s: pubkey sign failed", __func__);
2221 }
Damien Miller85b45e02013-07-20 13:21:52 +10002222 } else {
djm@openbsd.org04c091f2019-01-19 21:43:56 +00002223 if (privkey) {
2224 if (sshkey_sign(privkey, signature, slenp, data, dlen,
djm@openbsd.org9a14c642019-10-31 21:23:19 +00002225 alg, NULL, ssh->compat) < 0)
djm@openbsd.org04c091f2019-01-19 21:43:56 +00002226 fatal("%s: privkey sign failed", __func__);
2227 } else {
2228 if ((r = ssh_agent_sign(auth_sock, pubkey,
2229 signature, slenp, data, dlen, alg,
2230 ssh->compat)) != 0) {
2231 fatal("%s: agent sign failed: %s",
2232 __func__, ssh_err(r));
2233 }
2234 }
Damien Miller85b45e02013-07-20 13:21:52 +10002235 }
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00002236 return 0;
Damien Miller85b45e02013-07-20 13:21:52 +10002237}
2238
djm@openbsd.orgbdfd29f2015-07-03 03:47:00 +00002239/* SSH2 key exchange */
Ben Lindstrombba81212001-06-25 05:01:22 +00002240static void
djm@openbsd.org6350e032019-01-19 21:42:30 +00002241do_ssh2_kex(struct ssh *ssh)
Damien Millerefb4afe2000-04-12 18:45:05 +10002242{
Damien Miller9235a032014-04-20 13:17:20 +10002243 char *myproposal[PROPOSAL_MAX] = { KEX_SERVER };
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00002244 struct kex *kex;
markus@openbsd.org57e783c2015-01-20 20:16:21 +00002245 int r;
Damien Millerefb4afe2000-04-12 18:45:05 +10002246
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00002247 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
djm@openbsd.orgc3903c32018-08-13 02:41:05 +00002248 options.kex_algorithms);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00002249 myproposal[PROPOSAL_ENC_ALGS_CTOS] = compat_cipher_proposal(
djm@openbsd.orgc3903c32018-08-13 02:41:05 +00002250 options.ciphers);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00002251 myproposal[PROPOSAL_ENC_ALGS_STOC] = compat_cipher_proposal(
djm@openbsd.orgc3903c32018-08-13 02:41:05 +00002252 options.ciphers);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00002253 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
2254 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
Damien Millera0ff4662001-03-30 10:49:35 +10002255
Damien Miller9786e6e2005-07-26 21:54:56 +10002256 if (options.compression == COMP_NONE) {
Ben Lindstrom23e0f662002-06-21 01:09:47 +00002257 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
dtucker@openbsd.org8c02e362016-05-24 04:43:45 +00002258 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
Ben Lindstrom23e0f662002-06-21 01:09:47 +00002259 }
Damien Miller9395b282014-04-20 13:25:30 +10002260
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002261 if (options.rekey_limit || options.rekey_interval)
djm@openbsd.org6350e032019-01-19 21:42:30 +00002262 ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
dtucker@openbsd.orgc998bf02017-02-03 02:56:00 +00002263 options.rekey_interval);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002264
Damien Miller324541e2013-12-31 12:25:40 +11002265 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal(
djm@openbsd.orgc3903c32018-08-13 02:41:05 +00002266 list_hostkey_types());
Damien Miller0bc1bd82000-11-13 22:57:25 +11002267
Ben Lindstrom8ac91062001-04-04 17:57:54 +00002268 /* start key exchange */
djm@openbsd.org6350e032019-01-19 21:42:30 +00002269 if ((r = kex_setup(ssh, myproposal)) != 0)
markus@openbsd.org57e783c2015-01-20 20:16:21 +00002270 fatal("kex_setup: %s", ssh_err(r));
djm@openbsd.org6350e032019-01-19 21:42:30 +00002271 kex = ssh->kex;
Damien Miller1f0311c2014-05-15 14:24:09 +10002272#ifdef WITH_OPENSSL
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +00002273 kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
2274 kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
2275 kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
2276 kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
2277 kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
Damien Miller8e7fb332003-02-24 12:03:03 +11002278 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11002279 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11002280# ifdef OPENSSL_HAS_ECC
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +00002281 kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11002282# endif
Damien Miller1f0311c2014-05-15 14:24:09 +10002283#endif
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +00002284 kex->kex[KEX_C25519_SHA256] = kex_gen_server;
2285 kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_server;
Damien Miller0a80ca12010-02-27 07:55:05 +11002286 kex->load_host_public_key=&get_hostkey_public_by_type;
2287 kex->load_host_private_key=&get_hostkey_private_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002288 kex->host_key_index=&get_hostkey_index;
Damien Miller85b45e02013-07-20 13:21:52 +10002289 kex->sign = sshd_hostkey_sign;
Damien Millerefb4afe2000-04-12 18:45:05 +10002290
djm@openbsd.org6350e032019-01-19 21:42:30 +00002291 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &kex->done);
Damien Miller874d77b2000-10-14 16:23:11 +11002292
Ben Lindstrom2d90e002001-04-04 02:00:54 +00002293 session_id2 = kex->session_id;
2294 session_id2_len = kex->session_id_len;
2295
Damien Miller874d77b2000-10-14 16:23:11 +11002296#ifdef DEBUG_KEXDH
2297 /* send 1st encrypted/maced/compressed message */
2298 packet_start(SSH2_MSG_IGNORE);
2299 packet_put_cstring("markus");
2300 packet_send();
2301 packet_write_wait();
2302#endif
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +00002303 debug("KEX done");
Damien Millerefb4afe2000-04-12 18:45:05 +10002304}
Darren Tucker3e33cec2003-10-02 16:12:36 +10002305
2306/* server specific fatal cleanup */
2307void
2308cleanup_exit(int i)
2309{
djm@openbsd.org04c091f2019-01-19 21:43:56 +00002310 if (the_active_state != NULL && the_authctxt != NULL) {
2311 do_cleanup(the_active_state, the_authctxt);
Damien Miller75c62722014-04-20 13:24:31 +10002312 if (use_privsep && privsep_is_preauth &&
2313 pmonitor != NULL && pmonitor->m_pid > 1) {
Damien Miller9ee2c602011-09-22 21:38:30 +10002314 debug("Killing privsep child %d", pmonitor->m_pid);
2315 if (kill(pmonitor->m_pid, SIGKILL) != 0 &&
Darren Tucker2e135602011-10-02 19:10:13 +11002316 errno != ESRCH)
Damien Miller9ee2c602011-09-22 21:38:30 +10002317 error("%s: kill(%d): %s", __func__,
2318 pmonitor->m_pid, strerror(errno));
2319 }
2320 }
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11002321#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11002322 /* done after do_cleanup so it can cancel the PAM auth 'thread' */
Damien Miller9b655dc2019-01-20 14:55:27 +11002323 if (the_active_state != NULL && (!use_privsep || mm_is_monitor()))
2324 audit_event(the_active_state, SSH_CONNECTION_ABANDON);
Darren Tucker269a1ea2005-02-03 00:20:53 +11002325#endif
Darren Tucker3e33cec2003-10-02 16:12:36 +10002326 _exit(i);
2327}