blob: 70f5f73f051921f1bc80573c514144e7e8995583 [file] [log] [blame]
djm@openbsd.org868109b2015-07-01 02:39:06 +00001
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00002/* $OpenBSD: servconf.c,v 1.360 2020/01/31 22:42:45 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003/*
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
Damien Millere3b60b52006-07-10 21:08:03 +100016#include <sys/types.h>
17#include <sys/socket.h>
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000018#ifdef HAVE_SYS_SYSCTL_H
19#include <sys/sysctl.h>
20#endif
Damien Millere3b60b52006-07-10 21:08:03 +100021
Damien Miller0dac6fb2010-11-20 15:19:38 +110022#include <netinet/in.h>
23#include <netinet/in_systm.h>
24#include <netinet/ip.h>
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000025#ifdef HAVE_NET_ROUTE_H
26#include <net/route.h>
27#endif
Damien Miller0dac6fb2010-11-20 15:19:38 +110028
Darren Tucker5f96f3b2013-05-16 20:29:28 +100029#include <ctype.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100030#include <netdb.h>
Damien Miller565ca3f2006-08-19 00:23:15 +100031#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100032#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100033#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100034#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100035#include <signal.h>
Damien Millere6b3b612006-07-24 14:01:23 +100036#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000037#include <limits.h>
Damien Millerd7834352006-08-05 12:39:39 +100038#include <stdarg.h>
Darren Tuckere7140f22008-06-10 23:01:51 +100039#include <errno.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100040#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100041#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100042#endif
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +000043#ifdef USE_SYSTEM_GLOB
44# include <glob.h>
45#else
46# include "openbsd-compat/glob.h"
47#endif
Damien Millerbe43ebf2006-07-24 13:51:51 +100048
Damien Millerb84886b2008-05-19 15:05:07 +100049#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100050#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "log.h"
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +000053#include "sshbuf.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100054#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055#include "servconf.h"
Damien Miller78928792000-04-12 20:17:38 +100056#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "cipher.h"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +000059#include "sshkey.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000060#include "kex.h"
61#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100062#include "match.h"
Damien Miller9b439df2006-07-24 14:04:00 +100063#include "channels.h"
Damien Miller565ca3f2006-08-19 00:23:15 +100064#include "groupaccess.h"
Darren Tuckerfbcf8272012-05-19 19:37:01 +100065#include "canohost.h"
66#include "packet.h"
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +000067#include "ssherr.h"
Damien Millera6e3f012012-11-04 23:21:40 +110068#include "hostfile.h"
69#include "auth.h"
djm@openbsd.org57d378e2014-08-19 23:58:28 +000070#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000071#include "digest.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000073static void add_listen_addr(ServerOptions *, const char *,
74 const char *, int);
75static void add_one_listen_addr(ServerOptions *, const char *,
76 const char *, int);
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +000077void parse_server_config_depth(ServerOptions *options, const char *filename,
78 struct sshbuf *conf, struct include_list *includes,
79 struct connection_info *connectinfo, int flags, int *activep, int depth);
Damien Miller34132e52000-01-14 15:45:46 +110080
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000081/* Use of privilege separation or not */
82extern int use_privsep;
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +000083extern struct sshbuf *cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000084
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085/* Initializes the server options to their default values. */
86
Damien Miller4af51302000-04-16 11:18:38 +100087void
Damien Miller95def091999-11-25 00:26:21 +110088initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089{
Damien Miller95def091999-11-25 00:26:21 +110090 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110091
92 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100093 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110094
95 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110096 options->num_ports = 0;
97 options->ports_from_cmdline = 0;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +000098 options->queued_listen_addrs = NULL;
99 options->num_queued_listens = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100100 options->listen_addrs = NULL;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000101 options->num_listen_addrs = 0;
Darren Tucker0f383232005-01-20 10:57:56 +1100102 options->address_family = -1;
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000103 options->routing_domain = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100104 options->num_host_key_files = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100105 options->num_host_cert_files = 0;
Damien Miller85b45e02013-07-20 13:21:52 +1000106 options->host_key_agent = NULL;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000107 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100108 options->login_grace_time = -1;
chris@openbsd.org3d5728a2015-07-31 15:38:09 +0000109 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +1100110 options->ignore_rhosts = -1;
111 options->ignore_user_known_hosts = -1;
112 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000113 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +1100114 options->x11_forwarding = -1;
115 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +1100116 options->x11_use_localhost = -1;
Damien Miller5ff30c62013-10-30 22:21:50 +1100117 options->permit_tty = -1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000118 options->permit_user_rc = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000119 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100120 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +1100121 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +1100122 options->log_facility = SYSLOG_FACILITY_NOT_SET;
123 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000124 options->hostbased_authentication = -1;
125 options->hostbased_uses_name_from_packet_only = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000126 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000127 options->hostkeyalgorithms = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100128 options->pubkey_authentication = -1;
djm@openbsd.org0fddf292019-11-25 00:52:46 +0000129 options->pubkey_auth_options = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000130 options->pubkey_key_types = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100131 options->kerberos_authentication = -1;
132 options->kerberos_or_local_passwd = -1;
133 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100134 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000135 options->gss_authentication=-1;
136 options->gss_cleanup_creds = -1;
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000137 options->gss_strict_acceptor = -1;
Damien Miller95def091999-11-25 00:26:21 +1100138 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100139 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000140 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100141 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000142 options->permit_user_env = -1;
djm@openbsd.org95344c22018-07-03 10:59:35 +0000143 options->permit_user_env_whitelist = NULL;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000144 options->compression = -1;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000145 options->rekey_limit = -1;
146 options->rekey_interval = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100147 options->allow_tcp_forwarding = -1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000148 options->allow_streamlocal_forwarding = -1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000149 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100150 options->num_allow_users = 0;
151 options->num_deny_users = 0;
152 options->num_allow_groups = 0;
153 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000154 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000155 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000156 options->kex_algorithms = NULL;
djm@openbsd.org86e57372018-09-20 03:28:06 +0000157 options->ca_sign_algorithms = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000158 options->fwd_opts.gateway_ports = -1;
159 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
160 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000161 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000162 options->max_startups_begin = -1;
163 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000164 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000165 options->max_authtries = -1;
Damien Miller7207f642008-05-19 15:34:50 +1000166 options->max_sessions = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000167 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000168 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000169 options->client_alive_interval = -1;
170 options->client_alive_count_max = -1;
Damien Millerd8478b62011-05-29 21:39:36 +1000171 options->num_authkeys_files = 0;
Darren Tucker46bc0752004-05-02 22:11:30 +1000172 options->num_accept_env = 0;
djm@openbsd.org28013752018-06-09 03:03:10 +0000173 options->num_setenv = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100174 options->permit_tun = -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000175 options->permitted_opens = NULL;
djm@openbsd.org93c06ab2018-06-06 18:23:32 +0000176 options->permitted_listens = NULL;
Damien Millere2754432006-07-24 14:06:47 +1000177 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100178 options->chroot_directory = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +1100179 options->authorized_keys_command = NULL;
180 options->authorized_keys_command_user = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100181 options->revoked_keys_file = NULL;
djm@openbsd.org56584cc2019-12-15 18:57:30 +0000182 options->sk_provider = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100183 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000184 options->authorized_principals_file = NULL;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000185 options->authorized_principals_command = NULL;
186 options->authorized_principals_command_user = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100187 options->ip_qos_interactive = -1;
188 options->ip_qos_bulk = -1;
Damien Miller23528812012-04-22 11:24:43 +1000189 options->version_addendum = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000190 options->fingerprint_hash = -1;
djm@openbsd.org7844f352016-11-30 03:00:05 +0000191 options->disable_forwarding = -1;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000192 options->expose_userauth_info = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193}
194
djm@openbsd.org161cf412014-12-22 07:55:51 +0000195/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
196static int
197option_clear_or_none(const char *o)
198{
199 return o == NULL || strcasecmp(o, "none") == 0;
200}
201
djm@openbsd.orged085102015-10-29 08:05:01 +0000202static void
203assemble_algorithms(ServerOptions *o)
204{
djm@openbsd.org86e57372018-09-20 03:28:06 +0000205 char *all_cipher, *all_mac, *all_kex, *all_key, *all_sig;
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +0000206 char *def_cipher, *def_mac, *def_kex, *def_key, *def_sig;
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +0000207 int r;
djm@openbsd.org312d2f22018-07-04 13:49:31 +0000208
209 all_cipher = cipher_alg_list(',', 0);
210 all_mac = mac_alg_list(',');
211 all_kex = kex_alg_list(',');
212 all_key = sshkey_alg_list(0, 0, 1, ',');
djm@openbsd.org86e57372018-09-20 03:28:06 +0000213 all_sig = sshkey_alg_list(0, 1, 1, ',');
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +0000214 /* remove unsupported algos from default lists */
215 def_cipher = match_filter_whitelist(KEX_SERVER_ENCRYPT, all_cipher);
216 def_mac = match_filter_whitelist(KEX_SERVER_MAC, all_mac);
217 def_kex = match_filter_whitelist(KEX_SERVER_KEX, all_kex);
218 def_key = match_filter_whitelist(KEX_DEFAULT_PK_ALG, all_key);
219 def_sig = match_filter_whitelist(SSH_ALLOWED_CA_SIGALGS, all_sig);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +0000220#define ASSEMBLE(what, defaults, all) \
221 do { \
222 if ((r = kex_assemble_names(&o->what, defaults, all)) != 0) \
223 fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
224 } while (0)
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +0000225 ASSEMBLE(ciphers, def_cipher, all_cipher);
226 ASSEMBLE(macs, def_mac, all_mac);
227 ASSEMBLE(kex_algorithms, def_kex, all_kex);
228 ASSEMBLE(hostkeyalgorithms, def_key, all_key);
229 ASSEMBLE(hostbased_key_types, def_key, all_key);
230 ASSEMBLE(pubkey_key_types, def_key, all_key);
231 ASSEMBLE(ca_sign_algorithms, def_sig, all_sig);
djm@openbsd.org1b9dd4a2018-08-12 20:19:13 +0000232#undef ASSEMBLE
djm@openbsd.org312d2f22018-07-04 13:49:31 +0000233 free(all_cipher);
234 free(all_mac);
235 free(all_kex);
236 free(all_key);
djm@openbsd.org86e57372018-09-20 03:28:06 +0000237 free(all_sig);
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +0000238 free(def_cipher);
239 free(def_mac);
240 free(def_kex);
241 free(def_key);
242 free(def_sig);
djm@openbsd.orged085102015-10-29 08:05:01 +0000243}
244
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000245static void
djm@openbsd.org928f1232018-11-19 04:12:32 +0000246array_append2(const char *file, const int line, const char *directive,
247 char ***array, int **iarray, u_int *lp, const char *s, int i)
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000248{
249
250 if (*lp >= INT_MAX)
251 fatal("%s line %d: Too many %s entries", file, line, directive);
252
djm@openbsd.org928f1232018-11-19 04:12:32 +0000253 if (iarray != NULL) {
254 *iarray = xrecallocarray(*iarray, *lp, *lp + 1,
255 sizeof(**iarray));
256 (*iarray)[*lp] = i;
257 }
258
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000259 *array = xrecallocarray(*array, *lp, *lp + 1, sizeof(**array));
260 (*array)[*lp] = xstrdup(s);
261 (*lp)++;
262}
263
djm@openbsd.org928f1232018-11-19 04:12:32 +0000264static void
265array_append(const char *file, const int line, const char *directive,
266 char ***array, u_int *lp, const char *s)
267{
268 array_append2(file, line, directive, array, NULL, lp, s, 0);
269}
270
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000271void
272servconf_add_hostkey(const char *file, const int line,
djm@openbsd.org928f1232018-11-19 04:12:32 +0000273 ServerOptions *options, const char *path, int userprovided)
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000274{
275 char *apath = derelativise_path(path);
276
djm@openbsd.org928f1232018-11-19 04:12:32 +0000277 array_append2(file, line, "HostKey",
278 &options->host_key_files, &options->host_key_file_userprovided,
279 &options->num_host_key_files, apath, userprovided);
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000280 free(apath);
281}
282
283void
284servconf_add_hostcert(const char *file, const int line,
285 ServerOptions *options, const char *path)
286{
287 char *apath = derelativise_path(path);
288
289 array_append(file, line, "HostCertificate",
290 &options->host_cert_files, &options->num_host_cert_files, apath);
291 free(apath);
292}
293
Damien Miller4af51302000-04-16 11:18:38 +1000294void
Damien Miller95def091999-11-25 00:26:21 +1100295fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296{
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000297 u_int i;
djm@openbsd.org161cf412014-12-22 07:55:51 +0000298
Damien Miller726273e2001-11-12 11:40:11 +1100299 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000300 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000301 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100302
303 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100304 if (options->num_host_key_files == 0) {
305 /* fill default hostkeys for protocols */
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000306 servconf_add_hostkey("[default]", 0, options,
djm@openbsd.org928f1232018-11-19 04:12:32 +0000307 _PATH_HOST_RSA_KEY_FILE, 0);
Damien Millerdd190dd2010-11-11 14:17:02 +1100308#ifdef OPENSSL_HAS_ECC
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000309 servconf_add_hostkey("[default]", 0, options,
djm@openbsd.org928f1232018-11-19 04:12:32 +0000310 _PATH_HOST_ECDSA_KEY_FILE, 0);
Damien Millerdd190dd2010-11-11 14:17:02 +1100311#endif
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000312 servconf_add_hostkey("[default]", 0, options,
djm@openbsd.org928f1232018-11-19 04:12:32 +0000313 _PATH_HOST_ED25519_KEY_FILE, 0);
markus@openbsd.org5886b922018-03-01 20:32:16 +0000314#ifdef WITH_XMSS
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000315 servconf_add_hostkey("[default]", 0, options,
djm@openbsd.org928f1232018-11-19 04:12:32 +0000316 _PATH_HOST_XMSS_KEY_FILE, 0);
markus@openbsd.org5886b922018-03-01 20:32:16 +0000317#endif /* WITH_XMSS */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100318 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100319 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100320 if (options->num_ports == 0)
321 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000322 if (options->address_family == -1)
323 options->address_family = AF_UNSPEC;
Damien Miller34132e52000-01-14 15:45:46 +1100324 if (options->listen_addrs == NULL)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000325 add_listen_addr(options, NULL, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000326 if (options->pid_file == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000327 options->pid_file = xstrdup(_PATH_SSH_DAEMON_PID_FILE);
Damien Miller95def091999-11-25 00:26:21 +1100328 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000329 options->login_grace_time = 120;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000330 if (options->permit_root_login == PERMIT_NOT_SET)
chris@openbsd.org3d5728a2015-07-31 15:38:09 +0000331 options->permit_root_login = PERMIT_NO_PASSWD;
Damien Miller95def091999-11-25 00:26:21 +1100332 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100333 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100334 if (options->ignore_user_known_hosts == -1)
335 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100336 if (options->print_motd == -1)
337 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000338 if (options->print_lastlog == -1)
339 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100340 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100341 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100342 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100343 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100344 if (options->x11_use_localhost == -1)
345 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000346 if (options->xauth_location == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000347 options->xauth_location = xstrdup(_PATH_XAUTH);
Damien Miller5ff30c62013-10-30 22:21:50 +1100348 if (options->permit_tty == -1)
349 options->permit_tty = 1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000350 if (options->permit_user_rc == -1)
351 options->permit_user_rc = 1;
Damien Miller95def091999-11-25 00:26:21 +1100352 if (options->strict_modes == -1)
353 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100354 if (options->tcp_keep_alive == -1)
355 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100356 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100357 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100358 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000359 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000360 if (options->hostbased_authentication == -1)
361 options->hostbased_authentication = 0;
362 if (options->hostbased_uses_name_from_packet_only == -1)
363 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100364 if (options->pubkey_authentication == -1)
365 options->pubkey_authentication = 1;
djm@openbsd.org0fddf292019-11-25 00:52:46 +0000366 if (options->pubkey_auth_options == -1)
367 options->pubkey_auth_options = 0;
Damien Miller95def091999-11-25 00:26:21 +1100368 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000369 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100370 if (options->kerberos_or_local_passwd == -1)
371 options->kerberos_or_local_passwd = 1;
372 if (options->kerberos_ticket_cleanup == -1)
373 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100374 if (options->kerberos_get_afs_token == -1)
375 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000376 if (options->gss_authentication == -1)
377 options->gss_authentication = 0;
378 if (options->gss_cleanup_creds == -1)
379 options->gss_cleanup_creds = 1;
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000380 if (options->gss_strict_acceptor == -1)
djm@openbsd.org13bd2e22017-01-06 03:45:41 +0000381 options->gss_strict_acceptor = 1;
Damien Miller95def091999-11-25 00:26:21 +1100382 if (options->password_authentication == -1)
383 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100384 if (options->kbd_interactive_authentication == -1)
385 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000386 if (options->challenge_response_authentication == -1)
387 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100388 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100389 options->permit_empty_passwd = 0;
djm@openbsd.org95344c22018-07-03 10:59:35 +0000390 if (options->permit_user_env == -1) {
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000391 options->permit_user_env = 0;
djm@openbsd.org95344c22018-07-03 10:59:35 +0000392 options->permit_user_env_whitelist = NULL;
393 }
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000394 if (options->compression == -1)
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +0000395#ifdef WITH_ZLIB
sf@openbsd.org168b46f2018-07-09 13:37:10 +0000396 options->compression = COMP_DELAYED;
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +0000397#else
398 options->compression = COMP_NONE;
399#endif
400
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000401 if (options->rekey_limit == -1)
402 options->rekey_limit = 0;
403 if (options->rekey_interval == -1)
404 options->rekey_interval = 0;
Damien Miller50a41ed2000-10-16 12:14:42 +1100405 if (options->allow_tcp_forwarding == -1)
Damien Milleraa5b3f82012-12-03 09:50:54 +1100406 options->allow_tcp_forwarding = FORWARD_ALLOW;
Damien Miller7acefbb2014-07-18 14:11:24 +1000407 if (options->allow_streamlocal_forwarding == -1)
408 options->allow_streamlocal_forwarding = FORWARD_ALLOW;
Damien Miller4f755cd2008-05-19 14:57:41 +1000409 if (options->allow_agent_forwarding == -1)
410 options->allow_agent_forwarding = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000411 if (options->fwd_opts.gateway_ports == -1)
412 options->fwd_opts.gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000413 if (options->max_startups == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100414 options->max_startups = 100;
Damien Miller942da032000-08-18 13:59:06 +1000415 if (options->max_startups_rate == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100416 options->max_startups_rate = 30; /* 30% */
Damien Miller942da032000-08-18 13:59:06 +1000417 if (options->max_startups_begin == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100418 options->max_startups_begin = 10;
Darren Tucker89413db2004-05-24 10:36:23 +1000419 if (options->max_authtries == -1)
420 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000421 if (options->max_sessions == -1)
422 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000423 if (options->use_dns == -1)
deraadt@openbsd.org3cd51032015-02-02 01:57:44 +0000424 options->use_dns = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000425 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100426 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000427 if (options->client_alive_count_max == -1)
428 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000429 if (options->num_authkeys_files == 0) {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000430 array_append("[default]", 0, "AuthorizedKeysFiles",
431 &options->authorized_keys_files,
432 &options->num_authkeys_files,
433 _PATH_SSH_USER_PERMITTED_KEYS);
434 array_append("[default]", 0, "AuthorizedKeysFiles",
435 &options->authorized_keys_files,
436 &options->num_authkeys_files,
437 _PATH_SSH_USER_PERMITTED_KEYS2);
Damien Millerd8478b62011-05-29 21:39:36 +1000438 }
Damien Millerd27b9472005-12-13 19:29:02 +1100439 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100440 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100441 if (options->ip_qos_interactive == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +0000442 options->ip_qos_interactive = IPTOS_DSCP_AF21;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100443 if (options->ip_qos_bulk == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +0000444 options->ip_qos_bulk = IPTOS_DSCP_CS1;
Damien Miller23528812012-04-22 11:24:43 +1000445 if (options->version_addendum == NULL)
446 options->version_addendum = xstrdup("");
Damien Miller7acefbb2014-07-18 14:11:24 +1000447 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
448 options->fwd_opts.streamlocal_bind_mask = 0177;
449 if (options->fwd_opts.streamlocal_bind_unlink == -1)
450 options->fwd_opts.streamlocal_bind_unlink = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000451 if (options->fingerprint_hash == -1)
452 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org7844f352016-11-30 03:00:05 +0000453 if (options->disable_forwarding == -1)
454 options->disable_forwarding = 0;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000455 if (options->expose_userauth_info == -1)
456 options->expose_userauth_info = 0;
djm@openbsd.org56584cc2019-12-15 18:57:30 +0000457 if (options->sk_provider == NULL)
458 options->sk_provider = xstrdup("internal");
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000459
djm@openbsd.orged085102015-10-29 08:05:01 +0000460 assemble_algorithms(options);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000461
djm@openbsd.orgc5c3f322016-02-17 05:29:04 +0000462 /* Turn privilege separation and sandboxing on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000463 if (use_privsep == -1)
djm@openbsd.orgc5c3f322016-02-17 05:29:04 +0000464 use_privsep = PRIVSEP_ON;
Damien Miller4903eb42002-06-21 16:20:44 +1000465
djm@openbsd.org161cf412014-12-22 07:55:51 +0000466#define CLEAR_ON_NONE(v) \
467 do { \
468 if (option_clear_or_none(v)) { \
469 free(v); \
470 v = NULL; \
471 } \
472 } while(0)
473 CLEAR_ON_NONE(options->pid_file);
474 CLEAR_ON_NONE(options->xauth_location);
475 CLEAR_ON_NONE(options->banner);
476 CLEAR_ON_NONE(options->trusted_user_ca_keys);
477 CLEAR_ON_NONE(options->revoked_keys_file);
djm@openbsd.org56584cc2019-12-15 18:57:30 +0000478 CLEAR_ON_NONE(options->sk_provider);
djm@openbsd.org7e8528c2015-05-01 04:17:51 +0000479 CLEAR_ON_NONE(options->authorized_principals_file);
djm@openbsd.org9fd04682015-11-13 04:38:06 +0000480 CLEAR_ON_NONE(options->adm_forced_command);
481 CLEAR_ON_NONE(options->chroot_directory);
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000482 CLEAR_ON_NONE(options->routing_domain);
djm@openbsd.org3145d382019-12-15 20:59:23 +0000483 CLEAR_ON_NONE(options->host_key_agent);
djm@openbsd.org161cf412014-12-22 07:55:51 +0000484 for (i = 0; i < options->num_host_key_files; i++)
485 CLEAR_ON_NONE(options->host_key_files[i]);
486 for (i = 0; i < options->num_host_cert_files; i++)
487 CLEAR_ON_NONE(options->host_cert_files[i]);
488#undef CLEAR_ON_NONE
489
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +0000490 /* Similar handling for AuthenticationMethods=any */
491 if (options->num_auth_methods == 1 &&
492 strcmp(options->auth_methods[0], "any") == 0) {
493 free(options->auth_methods[0]);
494 options->auth_methods[0] = NULL;
495 options->num_auth_methods = 0;
496 }
497
Tim Rice40017b02002-07-14 13:36:49 -0700498#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000499 if (use_privsep && options->compression == 1) {
500 error("This platform does not support both privilege "
501 "separation and compression");
502 error("Compression disabled");
503 options->compression = 0;
504 }
505#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506}
507
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100509typedef enum {
510 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100511 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000512 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100513 /* Standard Options */
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000514 sPort, sHostKeyFile, sLoginGraceTime,
515 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000516 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100517 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
dtucker@openbsd.org745771f2018-02-09 02:37:36 +0000518 sKerberosGetAFSToken, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100519 sPasswordAuthentication, sKbdInteractiveAuthentication,
520 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000521 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100522 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller5ff30c62013-10-30 22:21:50 +1100523 sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
djm@openbsd.org83b58182016-08-19 03:18:06 +0000524 sPermitUserEnvironment, sAllowTcpForwarding, sCompression,
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000525 sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000526 sIgnoreUserKnownHosts, sCiphers, sMacs, sPidFile,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000527 sGatewayPorts, sPubkeyAuthentication, sPubkeyAcceptedKeyTypes,
528 sXAuthLocation, sSubsystem, sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000529 sBanner, sUseDNS, sHostbasedAuthentication,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000530 sHostbasedUsesNameFromPacketOnly, sHostbasedAcceptedKeyTypes,
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000531 sHostKeyAlgorithms,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000532 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000533 sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
djm@openbsd.org28013752018-06-09 03:03:10 +0000534 sAcceptEnv, sSetEnv, sPermitTunnel,
djm@openbsd.org93c06ab2018-06-06 18:23:32 +0000535 sMatch, sPermitOpen, sPermitListen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100536 sUsePrivilegeSeparation, sAllowAgentForwarding,
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +0000537 sHostCertificate, sInclude,
Damien Miller30da3442010-05-10 11:58:03 +1000538 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000539 sAuthorizedPrincipalsCommand, sAuthorizedPrincipalsCommandUser,
djm@openbsd.org86e57372018-09-20 03:28:06 +0000540 sKexAlgorithms, sCASignatureAlgorithms, sIPQoS, sVersionAddendum,
Damien Miller09d3e122012-10-31 08:58:58 +1100541 sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
Damien Miller72e6b5c2014-07-04 09:00:04 +1000542 sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
Damien Miller7acefbb2014-07-18 14:11:24 +1000543 sStreamLocalBindMask, sStreamLocalBindUnlink,
djm@openbsd.org7844f352016-11-30 03:00:05 +0000544 sAllowStreamLocalForwarding, sFingerprintHash, sDisableForwarding,
djm@openbsd.org56584cc2019-12-15 18:57:30 +0000545 sExposeAuthInfo, sRDomain, sPubkeyAuthOptions, sSecurityKeyProvider,
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000546 sDeprecated, sIgnore, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547} ServerOpCodes;
548
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +0000549#define SSHCFG_GLOBAL 0x01 /* allowed in main section of config */
550#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
551#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
552#define SSHCFG_NEVERMATCH 0x04 /* Match never matches; internal only */
Darren Tucker45150472006-07-12 22:34:17 +1000553
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000554/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100555static struct {
556 const char *name;
557 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000558 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100559} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100560 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000561#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000562 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000563#else
Darren Tucker45150472006-07-12 22:34:17 +1000564 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000565#endif
Darren Tucker45150472006-07-12 22:34:17 +1000566 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100567 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000568 { "port", sPort, SSHCFG_GLOBAL },
569 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
570 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
Damien Miller85b45e02013-07-20 13:21:52 +1000571 { "hostkeyagent", sHostKeyAgent, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000572 { "pidfile", sPidFile, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000573 { "serverkeybits", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000574 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000575 { "keyregenerationinterval", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100576 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000577 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
djm@openbsd.org54cd41a2017-05-17 01:24:17 +0000578 { "loglevel", sLogLevel, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000579 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000580 { "rhostsrsaauthentication", sDeprecated, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100581 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000582 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000583 { "hostbasedacceptedkeytypes", sHostbasedAcceptedKeyTypes, SSHCFG_ALL },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000584 { "hostkeyalgorithms", sHostKeyAlgorithms, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000585 { "rsaauthentication", sDeprecated, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100586 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000587 { "pubkeyacceptedkeytypes", sPubkeyAcceptedKeyTypes, SSHCFG_ALL },
djm@openbsd.org0fddf292019-11-25 00:52:46 +0000588 { "pubkeyauthoptions", sPubkeyAuthOptions, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000589 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000590#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100591 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000592 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
593 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100594#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000595 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000596#else
Darren Tucker45150472006-07-12 22:34:17 +1000597 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100598#endif
599#else
Darren Tucker1629c072007-02-19 22:25:37 +1100600 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000601 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
602 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
603 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000604#endif
Darren Tucker45150472006-07-12 22:34:17 +1000605 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
606 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000607#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100608 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000609 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000610 { "gssapistrictacceptorcheck", sGssStrictAcceptor, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000611#else
Darren Tucker1629c072007-02-19 22:25:37 +1100612 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000613 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000614 { "gssapistrictacceptorcheck", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000615#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100616 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
617 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100618 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Damien Miller87f08be2018-07-20 13:18:28 +1000619 { "skeyauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000620 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
621 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
622 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
623 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
Damien Millerac908c12015-10-22 09:35:24 +1100624#ifdef DISABLE_LASTLOG
625 { "printlastlog", sUnsupported, SSHCFG_GLOBAL },
626#else
Darren Tucker45150472006-07-12 22:34:17 +1000627 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
Damien Millerac908c12015-10-22 09:35:24 +1100628#endif
Darren Tucker45150472006-07-12 22:34:17 +1000629 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
630 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000631 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
632 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
633 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000634 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
635 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100636 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000637 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
djm@openbsd.org83b58182016-08-19 03:18:06 +0000638 { "uselogin", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000639 { "compression", sCompression, SSHCFG_GLOBAL },
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000640 { "rekeylimit", sRekeyLimit, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000641 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
642 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
643 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000644 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Damien Millerc24da772012-06-20 21:53:58 +1000645 { "allowusers", sAllowUsers, SSHCFG_ALL },
646 { "denyusers", sDenyUsers, SSHCFG_ALL },
647 { "allowgroups", sAllowGroups, SSHCFG_ALL },
648 { "denygroups", sDenyGroups, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000649 { "ciphers", sCiphers, SSHCFG_GLOBAL },
650 { "macs", sMacs, SSHCFG_GLOBAL },
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000651 { "protocol", sIgnore, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000652 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
653 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
654 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000655 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000656 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100657 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000658 { "usedns", sUseDNS, SSHCFG_GLOBAL },
659 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
660 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
markus@openbsd.orgf0ddede2016-11-23 23:14:15 +0000661 { "clientaliveinterval", sClientAliveInterval, SSHCFG_ALL },
662 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000663 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000664 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
djm@openbsd.org66705942017-03-14 07:19:07 +0000665 { "useprivilegeseparation", sDeprecated, SSHCFG_GLOBAL},
Damien Millerc24da772012-06-20 21:53:58 +1000666 { "acceptenv", sAcceptEnv, SSHCFG_ALL },
djm@openbsd.org28013752018-06-09 03:03:10 +0000667 { "setenv", sSetEnv, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000668 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Damien Miller5ff30c62013-10-30 22:21:50 +1100669 { "permittty", sPermitTTY, SSHCFG_ALL },
Damien Miller72e6b5c2014-07-04 09:00:04 +1000670 { "permituserrc", sPermitUserRC, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000671 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000672 { "permitopen", sPermitOpen, SSHCFG_ALL },
djm@openbsd.org93c06ab2018-06-06 18:23:32 +0000673 { "permitlisten", sPermitListen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000674 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100675 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100676 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100677 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
678 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000679 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000680 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +0000681 { "include", sInclude, SSHCFG_ALL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100682 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller09d3e122012-10-31 08:58:58 +1100683 { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
684 { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000685 { "authorizedprincipalscommand", sAuthorizedPrincipalsCommand, SSHCFG_ALL },
686 { "authorizedprincipalscommanduser", sAuthorizedPrincipalsCommandUser, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000687 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Damien Millera6e3f012012-11-04 23:21:40 +1100688 { "authenticationmethods", sAuthenticationMethods, SSHCFG_ALL },
Damien Miller7acefbb2014-07-18 14:11:24 +1000689 { "streamlocalbindmask", sStreamLocalBindMask, SSHCFG_ALL },
690 { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
691 { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000692 { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
djm@openbsd.org7844f352016-11-30 03:00:05 +0000693 { "disableforwarding", sDisableForwarding, SSHCFG_ALL },
djm@openbsd.org8f574952017-06-24 06:34:38 +0000694 { "exposeauthinfo", sExposeAuthInfo, SSHCFG_ALL },
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000695 { "rdomain", sRDomain, SSHCFG_ALL },
djm@openbsd.org0cbed242018-09-20 23:40:16 +0000696 { "casignaturealgorithms", sCASignatureAlgorithms, SSHCFG_ALL },
djm@openbsd.org56584cc2019-12-15 18:57:30 +0000697 { "securitykeyprovider", sSecurityKeyProvider, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000698 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000699};
700
Darren Tuckere7140f22008-06-10 23:01:51 +1000701static struct {
702 int val;
703 char *text;
704} tunmode_desc[] = {
705 { SSH_TUNMODE_NO, "no" },
706 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
707 { SSH_TUNMODE_ETHERNET, "ethernet" },
708 { SSH_TUNMODE_YES, "yes" },
709 { -1, NULL }
710};
711
djm@openbsd.org115063a2018-06-06 18:22:41 +0000712/* Returns an opcode name from its number */
713
714static const char *
715lookup_opcode_name(ServerOpCodes code)
716{
717 u_int i;
718
719 for (i = 0; keywords[i].name != NULL; i++)
720 if (keywords[i].opcode == code)
721 return(keywords[i].name);
722 return "UNKNOWN";
723}
724
725
Damien Miller5428f641999-11-25 11:54:57 +1100726/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000727 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100728 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729
Damien Miller4af51302000-04-16 11:18:38 +1000730static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100731parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000732 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000734 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000735
Damien Miller95def091999-11-25 00:26:21 +1100736 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000737 if (strcasecmp(cp, keywords[i].name) == 0) {
738 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100739 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000740 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000742 error("%s: line %d: Bad configuration option: %s",
743 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100744 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000745}
746
Darren Tucker88b6fb22010-01-13 22:44:29 +1100747char *
748derelativise_path(const char *path)
749{
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000750 char *expanded, *ret, cwd[PATH_MAX];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100751
djm@openbsd.org161cf412014-12-22 07:55:51 +0000752 if (strcasecmp(path, "none") == 0)
753 return xstrdup("none");
Darren Tucker88b6fb22010-01-13 22:44:29 +1100754 expanded = tilde_expand_filename(path, getuid());
djm@openbsd.org2a358622018-11-16 03:26:01 +0000755 if (path_absolute(expanded))
Darren Tucker88b6fb22010-01-13 22:44:29 +1100756 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100757 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100758 fatal("%s: getcwd: %s", __func__, strerror(errno));
759 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tuckera627d422013-06-02 07:31:17 +1000760 free(expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100761 return ret;
762}
763
Ben Lindstrombba81212001-06-25 05:01:22 +0000764static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000765add_listen_addr(ServerOptions *options, const char *addr,
766 const char *rdomain, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100767{
Damien Millereccb9de2005-06-17 12:59:34 +1000768 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100769
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000770 if (port > 0)
771 add_one_listen_addr(options, addr, rdomain, port);
772 else {
773 for (i = 0; i < options->num_ports; i++) {
774 add_one_listen_addr(options, addr, rdomain,
775 options->ports[i]);
776 }
777 }
Ben Lindstromc510af42001-04-07 17:25:48 +0000778}
779
Ben Lindstrombba81212001-06-25 05:01:22 +0000780static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000781add_one_listen_addr(ServerOptions *options, const char *addr,
782 const char *rdomain, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000783{
784 struct addrinfo hints, *ai, *aitop;
785 char strport[NI_MAXSERV];
786 int gaierr;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000787 u_int i;
788
789 /* Find listen_addrs entry for this rdomain */
790 for (i = 0; i < options->num_listen_addrs; i++) {
791 if (rdomain == NULL && options->listen_addrs[i].rdomain == NULL)
792 break;
793 if (rdomain == NULL || options->listen_addrs[i].rdomain == NULL)
794 continue;
795 if (strcmp(rdomain, options->listen_addrs[i].rdomain) == 0)
796 break;
797 }
798 if (i >= options->num_listen_addrs) {
799 /* No entry for this rdomain; allocate one */
800 if (i >= INT_MAX)
801 fatal("%s: too many listen addresses", __func__);
802 options->listen_addrs = xrecallocarray(options->listen_addrs,
803 options->num_listen_addrs, options->num_listen_addrs + 1,
804 sizeof(*options->listen_addrs));
805 i = options->num_listen_addrs++;
806 if (rdomain != NULL)
807 options->listen_addrs[i].rdomain = xstrdup(rdomain);
808 }
809 /* options->listen_addrs[i] points to the addresses for this rdomain */
Ben Lindstromc510af42001-04-07 17:25:48 +0000810
811 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100812 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000813 hints.ai_socktype = SOCK_STREAM;
814 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100815 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000816 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
817 fatal("bad addr or host: %s (%s)",
818 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100819 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000820 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
821 ;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000822 ai->ai_next = options->listen_addrs[i].addrs;
823 options->listen_addrs[i].addrs = aitop;
824}
825
826/* Returns nonzero if the routing domain name is valid */
827static int
828valid_rdomain(const char *name)
829{
Damien Miller43c29bb2017-10-25 13:10:59 +1100830#if defined(HAVE_SYS_VALID_RDOMAIN)
Damien Miller2de5c6b2017-10-27 08:42:33 +1100831 return sys_valid_rdomain(name);
Damien Miller43c29bb2017-10-25 13:10:59 +1100832#elif defined(__OpenBSD__)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000833 const char *errstr;
834 long long num;
835 struct rt_tableinfo info;
836 int mib[6];
837 size_t miblen = sizeof(mib);
838
839 if (name == NULL)
840 return 1;
841
842 num = strtonum(name, 0, 255, &errstr);
843 if (errstr != NULL)
844 return 0;
845
846 /* Check whether the table actually exists */
847 memset(mib, 0, sizeof(mib));
848 mib[0] = CTL_NET;
849 mib[1] = PF_ROUTE;
850 mib[4] = NET_RT_TABLE;
851 mib[5] = (int)num;
852 if (sysctl(mib, 6, &info, &miblen, NULL, 0) == -1)
853 return 0;
854
855 return 1;
Damien Miller43c29bb2017-10-25 13:10:59 +1100856#else /* defined(__OpenBSD__) */
857 error("Routing domains are not supported on this platform");
858 return 0;
859#endif
Damien Miller34132e52000-01-14 15:45:46 +1100860}
861
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000862/*
863 * Queue a ListenAddress to be processed once we have all of the Ports
864 * and AddressFamily options.
865 */
866static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000867queue_listen_addr(ServerOptions *options, const char *addr,
868 const char *rdomain, int port)
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000869{
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000870 struct queued_listenaddr *qla;
871
872 options->queued_listen_addrs = xrecallocarray(
873 options->queued_listen_addrs,
874 options->num_queued_listens, options->num_queued_listens + 1,
875 sizeof(*options->queued_listen_addrs));
876 qla = &options->queued_listen_addrs[options->num_queued_listens++];
877 qla->addr = xstrdup(addr);
878 qla->port = port;
879 qla->rdomain = rdomain == NULL ? NULL : xstrdup(rdomain);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000880}
881
882/*
883 * Process queued (text) ListenAddress entries.
884 */
885static void
886process_queued_listen_addrs(ServerOptions *options)
887{
888 u_int i;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000889 struct queued_listenaddr *qla;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000890
891 if (options->num_ports == 0)
892 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
893 if (options->address_family == -1)
894 options->address_family = AF_UNSPEC;
895
896 for (i = 0; i < options->num_queued_listens; i++) {
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000897 qla = &options->queued_listen_addrs[i];
898 add_listen_addr(options, qla->addr, qla->rdomain, qla->port);
899 free(qla->addr);
900 free(qla->rdomain);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000901 }
902 free(options->queued_listen_addrs);
903 options->queued_listen_addrs = NULL;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000904 options->num_queued_listens = 0;
905}
906
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000907/*
djm@openbsd.org115063a2018-06-06 18:22:41 +0000908 * Inform channels layer of permitopen options for a single forwarding
909 * direction (local/remote).
910 */
911static void
912process_permitopen_list(struct ssh *ssh, ServerOpCodes opcode,
913 char **opens, u_int num_opens)
914{
915 u_int i;
916 int port;
dtucker@openbsd.orgd05ea252019-01-23 21:50:56 +0000917 char *host, *arg, *oarg, ch;
djm@openbsd.org115063a2018-06-06 18:22:41 +0000918 int where = opcode == sPermitOpen ? FORWARD_LOCAL : FORWARD_REMOTE;
919 const char *what = lookup_opcode_name(opcode);
920
921 channel_clear_permission(ssh, FORWARD_ADM, where);
922 if (num_opens == 0)
923 return; /* permit any */
924
925 /* handle keywords: "any" / "none" */
926 if (num_opens == 1 && strcmp(opens[0], "any") == 0)
927 return;
928 if (num_opens == 1 && strcmp(opens[0], "none") == 0) {
929 channel_disable_admin(ssh, where);
930 return;
931 }
932 /* Otherwise treat it as a list of permitted host:port */
933 for (i = 0; i < num_opens; i++) {
934 oarg = arg = xstrdup(opens[i]);
dtucker@openbsd.org281ce042019-01-24 02:34:52 +0000935 ch = '\0';
dtucker@openbsd.orgd05ea252019-01-23 21:50:56 +0000936 host = hpdelim2(&arg, &ch);
937 if (host == NULL || ch == '/')
djm@openbsd.org115063a2018-06-06 18:22:41 +0000938 fatal("%s: missing host in %s", __func__, what);
939 host = cleanhostname(host);
940 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
941 fatal("%s: bad port number in %s", __func__, what);
942 /* Send it to channels layer */
943 channel_add_permission(ssh, FORWARD_ADM,
944 where, host, port);
945 free(oarg);
946 }
947}
948
949/*
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000950 * Inform channels layer of permitopen options from configuration.
951 */
952void
953process_permitopen(struct ssh *ssh, ServerOptions *options)
954{
djm@openbsd.org115063a2018-06-06 18:22:41 +0000955 process_permitopen_list(ssh, sPermitOpen,
956 options->permitted_opens, options->num_permitted_opens);
djm@openbsd.org93c06ab2018-06-06 18:23:32 +0000957 process_permitopen_list(ssh, sPermitListen,
958 options->permitted_listens,
959 options->num_permitted_listens);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000960}
961
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000962struct connection_info *
djm@openbsd.org172a5922019-01-19 21:37:48 +0000963get_connection_info(struct ssh *ssh, int populate, int use_dns)
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000964{
965 static struct connection_info ci;
966
djm@openbsd.org172a5922019-01-19 21:37:48 +0000967 if (ssh == NULL || !populate)
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000968 return &ci;
djm@openbsd.org95767262016-03-07 19:02:43 +0000969 ci.host = auth_get_canonical_hostname(ssh, use_dns);
970 ci.address = ssh_remote_ipaddr(ssh);
971 ci.laddress = ssh_local_ipaddr(ssh);
972 ci.lport = ssh_local_port(ssh);
djm@openbsd.org68af80e2017-10-25 00:19:47 +0000973 ci.rdomain = ssh_packet_rdomain_in(ssh);
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000974 return &ci;
975}
976
Darren Tucker45150472006-07-12 22:34:17 +1000977/*
978 * The strategy for the Match blocks is that the config file is parsed twice.
979 *
980 * The first time is at startup. activep is initialized to 1 and the
981 * directives in the global context are processed and acted on. Hitting a
982 * Match directive unsets activep and the directives inside the block are
983 * checked for syntax only.
984 *
985 * The second time is after a connection has been established but before
986 * authentication. activep is initialized to 2 and global config directives
987 * are ignored since they have already been processed. If the criteria in a
988 * Match block is met, activep is set and the subsequent directives
989 * processed and actioned until EOF or another Match block unsets it. Any
990 * options set are copied into the main server config.
991 *
992 * Potential additions/improvements:
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000993 * - Add Match support for pre-kex directives, eg. Ciphers.
Darren Tucker45150472006-07-12 22:34:17 +1000994 *
995 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
996 * Match Address 192.168.0.*
997 * Tag trusted
998 * Match Group wheel
999 * Tag trusted
1000 * Match Tag trusted
1001 * AllowTcpForwarding yes
1002 * GatewayPorts clientspecified
1003 * [...]
1004 *
1005 * - Add a PermittedChannelRequests directive
1006 * Match Group shell
1007 * PermittedChannelRequests session,forwarded-tcpip
1008 */
1009
1010static int
Damien Miller565ca3f2006-08-19 00:23:15 +10001011match_cfg_line_group(const char *grps, int line, const char *user)
1012{
1013 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +10001014 struct passwd *pw;
1015
Damien Miller565ca3f2006-08-19 00:23:15 +10001016 if (user == NULL)
1017 goto out;
1018
1019 if ((pw = getpwnam(user)) == NULL) {
1020 debug("Can't match group at line %d because user %.100s does "
1021 "not exist", line, user);
1022 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
1023 debug("Can't Match group because user %.100s not in any group "
1024 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +10001025 } else if (ga_match_pattern_list(grps) != 1) {
1026 debug("user %.100s does not match group list %.100s at line %d",
1027 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +10001028 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +10001029 debug("user %.100s matched group list %.100s at line %d", user,
1030 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +10001031 result = 1;
1032 }
1033out:
1034 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +10001035 return result;
1036}
1037
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001038static void
1039match_test_missing_fatal(const char *criteria, const char *attrib)
1040{
1041 fatal("'Match %s' in configuration but '%s' not in connection "
1042 "test specification.", criteria, attrib);
1043}
1044
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001045/*
Darren Tuckerbb6cc072012-09-17 13:25:06 +10001046 * All of the attributes on a single Match line are ANDed together, so we need
Damien Miller03bf2e62013-10-24 21:01:26 +11001047 * to check every attribute and set the result to zero if any attribute does
Darren Tuckerbb6cc072012-09-17 13:25:06 +10001048 * not match.
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001049 */
Damien Miller565ca3f2006-08-19 00:23:15 +10001050static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001051match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +10001052{
Damien Millercf31f382013-10-24 21:02:56 +11001053 int result = 1, attributes = 0, port;
Darren Tucker45150472006-07-12 22:34:17 +10001054 char *arg, *attrib, *cp = *condition;
Darren Tucker45150472006-07-12 22:34:17 +10001055
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001056 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +10001057 debug3("checking syntax for 'Match %s'", cp);
1058 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001059 debug3("checking match for '%s' user %s host %s addr %s "
1060 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
1061 ci->host ? ci->host : "(null)",
1062 ci->address ? ci->address : "(null)",
1063 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +10001064
1065 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +11001066 attributes++;
1067 if (strcasecmp(attrib, "all") == 0) {
1068 if (attributes != 1 ||
1069 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
1070 error("'all' cannot be combined with other "
1071 "Match attributes");
1072 return -1;
1073 }
1074 *condition = cp;
1075 return 1;
1076 }
Darren Tucker45150472006-07-12 22:34:17 +10001077 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
1078 error("Missing Match criteria for %s", attrib);
1079 return -1;
1080 }
Darren Tucker45150472006-07-12 22:34:17 +10001081 if (strcasecmp(attrib, "user") == 0) {
dtucker@openbsd.orge826bbc2019-04-18 18:56:16 +00001082 if (ci == NULL || (ci->test && ci->user == NULL)) {
Darren Tucker45150472006-07-12 22:34:17 +10001083 result = 0;
1084 continue;
1085 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001086 if (ci->user == NULL)
1087 match_test_missing_fatal("User", "user");
dtucker@openbsd.orgfd10cf02019-03-06 22:14:23 +00001088 if (match_usergroup_pattern_list(ci->user, arg) != 1)
Darren Tucker45150472006-07-12 22:34:17 +10001089 result = 0;
1090 else
1091 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001092 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +10001093 } else if (strcasecmp(attrib, "group") == 0) {
dtucker@openbsd.orge826bbc2019-04-18 18:56:16 +00001094 if (ci == NULL || (ci->test && ci->user == NULL)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001095 result = 0;
1096 continue;
1097 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001098 if (ci->user == NULL)
1099 match_test_missing_fatal("Group", "user");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001100 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +10001101 case -1:
1102 return -1;
1103 case 0:
1104 result = 0;
1105 }
Darren Tucker45150472006-07-12 22:34:17 +10001106 } else if (strcasecmp(attrib, "host") == 0) {
dtucker@openbsd.orge826bbc2019-04-18 18:56:16 +00001107 if (ci == NULL || (ci->test && ci->host == NULL)) {
Darren Tucker45150472006-07-12 22:34:17 +10001108 result = 0;
1109 continue;
1110 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001111 if (ci->host == NULL)
1112 match_test_missing_fatal("Host", "host");
djm@openbsd.orge661a862015-05-04 06:10:48 +00001113 if (match_hostname(ci->host, arg) != 1)
Darren Tucker45150472006-07-12 22:34:17 +10001114 result = 0;
1115 else
1116 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001117 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +10001118 } else if (strcasecmp(attrib, "address") == 0) {
dtucker@openbsd.orge826bbc2019-04-18 18:56:16 +00001119 if (ci == NULL || (ci->test && ci->address == NULL)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001120 result = 0;
1121 continue;
1122 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001123 if (ci->address == NULL)
1124 match_test_missing_fatal("Address", "addr");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001125 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +10001126 case 1:
Darren Tucker45150472006-07-12 22:34:17 +10001127 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001128 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +10001129 break;
1130 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +10001131 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +10001132 result = 0;
1133 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +10001134 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +10001135 return -1;
1136 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001137 } else if (strcasecmp(attrib, "localaddress") == 0){
dtucker@openbsd.orge826bbc2019-04-18 18:56:16 +00001138 if (ci == NULL || (ci->test && ci->laddress == NULL)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001139 result = 0;
1140 continue;
1141 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001142 if (ci->laddress == NULL)
1143 match_test_missing_fatal("LocalAddress",
1144 "laddr");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001145 switch (addr_match_list(ci->laddress, arg)) {
1146 case 1:
1147 debug("connection from %.100s matched "
1148 "'LocalAddress %.100s' at line %d",
1149 ci->laddress, arg, line);
1150 break;
1151 case 0:
1152 case -1:
1153 result = 0;
1154 break;
1155 case -2:
1156 return -1;
1157 }
1158 } else if (strcasecmp(attrib, "localport") == 0) {
1159 if ((port = a2port(arg)) == -1) {
1160 error("Invalid LocalPort '%s' on Match line",
1161 arg);
1162 return -1;
1163 }
dtucker@openbsd.orge826bbc2019-04-18 18:56:16 +00001164 if (ci == NULL || (ci->test && ci->lport == -1)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001165 result = 0;
1166 continue;
1167 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001168 if (ci->lport == 0)
1169 match_test_missing_fatal("LocalPort", "lport");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001170 /* TODO support port lists */
1171 if (port == ci->lport)
1172 debug("connection from %.100s matched "
1173 "'LocalPort %d' at line %d",
1174 ci->laddress, port, line);
1175 else
1176 result = 0;
djm@openbsd.org68af80e2017-10-25 00:19:47 +00001177 } else if (strcasecmp(attrib, "rdomain") == 0) {
dtucker@openbsd.orge826bbc2019-04-18 18:56:16 +00001178 if (ci == NULL || (ci->test && ci->rdomain == NULL)) {
djm@openbsd.org68af80e2017-10-25 00:19:47 +00001179 result = 0;
1180 continue;
1181 }
dtucker@openbsd.orge826bbc2019-04-18 18:56:16 +00001182 if (ci->rdomain == NULL)
1183 match_test_missing_fatal("RDomain", "rdomain");
djm@openbsd.org68af80e2017-10-25 00:19:47 +00001184 if (match_pattern_list(ci->rdomain, arg, 0) != 1)
1185 result = 0;
1186 else
1187 debug("user %.100s matched 'RDomain %.100s' at "
1188 "line %d", ci->rdomain, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +10001189 } else {
1190 error("Unsupported Match attribute %s", attrib);
1191 return -1;
1192 }
1193 }
Damien Millercf31f382013-10-24 21:02:56 +11001194 if (attributes == 0) {
1195 error("One or more attributes required for Match");
1196 return -1;
1197 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001198 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +10001199 debug3("match %sfound", result ? "" : "not ");
1200 *condition = cp;
1201 return result;
1202}
1203
Damien Millere2754432006-07-24 14:06:47 +10001204#define WHITESPACE " \t\r\n"
1205
Damien Miller33322122011-06-20 14:43:11 +10001206/* Multistate option parsing */
1207struct multistate {
1208 char *key;
1209 int value;
1210};
djm@openbsd.org@openbsd.org33edb6e2017-11-03 05:18:44 +00001211static const struct multistate multistate_flag[] = {
1212 { "yes", 1 },
1213 { "no", 0 },
1214 { NULL, -1 }
1215};
Damien Miller33322122011-06-20 14:43:11 +10001216static const struct multistate multistate_addressfamily[] = {
1217 { "inet", AF_INET },
1218 { "inet6", AF_INET6 },
1219 { "any", AF_UNSPEC },
1220 { NULL, -1 }
1221};
1222static const struct multistate multistate_permitrootlogin[] = {
1223 { "without-password", PERMIT_NO_PASSWD },
deraadt@openbsd.org1dc8d932015-08-06 14:53:21 +00001224 { "prohibit-password", PERMIT_NO_PASSWD },
Damien Miller33322122011-06-20 14:43:11 +10001225 { "forced-commands-only", PERMIT_FORCED_ONLY },
1226 { "yes", PERMIT_YES },
1227 { "no", PERMIT_NO },
1228 { NULL, -1 }
1229};
1230static const struct multistate multistate_compression[] = {
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +00001231#ifdef WITH_ZLIB
sf@openbsd.org168b46f2018-07-09 13:37:10 +00001232 { "yes", COMP_DELAYED },
1233 { "delayed", COMP_DELAYED },
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +00001234#endif
Damien Miller33322122011-06-20 14:43:11 +10001235 { "no", COMP_NONE },
1236 { NULL, -1 }
1237};
1238static const struct multistate multistate_gatewayports[] = {
1239 { "clientspecified", 2 },
1240 { "yes", 1 },
1241 { "no", 0 },
1242 { NULL, -1 }
1243};
Damien Milleraa5b3f82012-12-03 09:50:54 +11001244static const struct multistate multistate_tcpfwd[] = {
1245 { "yes", FORWARD_ALLOW },
1246 { "all", FORWARD_ALLOW },
1247 { "no", FORWARD_DENY },
1248 { "remote", FORWARD_REMOTE },
1249 { "local", FORWARD_LOCAL },
1250 { NULL, -1 }
1251};
Damien Miller33322122011-06-20 14:43:11 +10001252
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00001253static int
1254process_server_config_line_depth(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001255 const char *filename, int linenum, int *activep,
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00001256 struct connection_info *connectinfo, int inc_flags, int depth,
1257 struct include_list *includes)
Ben Lindstromade03f62001-12-06 18:22:17 +00001258{
dtucker@openbsd.org281ce042019-01-24 02:34:52 +00001259 char ch, *cp, ***chararrayptr, **charptr, *arg, *arg2, *p;
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00001260 int cmdline = 0, *intptr, value, value2, n, port, oactive, r, found;
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001261 SyslogFacility *log_facility_ptr;
1262 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +00001263 ServerOpCodes opcode;
djm@openbsd.org115063a2018-06-06 18:22:41 +00001264 u_int i, *uintptr, uvalue, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +10001265 size_t len;
Darren Tucker9113d0c2013-05-16 20:48:14 +10001266 long long val64;
Damien Miller33322122011-06-20 14:43:11 +10001267 const struct multistate *multistate_ptr;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001268 const char *errstr;
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00001269 struct include_item *item;
1270 glob_t gbuf;
Ben Lindstromade03f62001-12-06 18:22:17 +00001271
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +00001272 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
1273 if ((len = strlen(line)) == 0)
1274 return 0;
1275 for (len--; len > 0; len--) {
1276 if (strchr(WHITESPACE "\f", line[len]) == NULL)
1277 break;
1278 line[len] = '\0';
1279 }
1280
Ben Lindstromade03f62001-12-06 18:22:17 +00001281 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +11001282 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +11001283 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +00001284 /* Ignore leading whitespace */
1285 if (*arg == '\0')
1286 arg = strdelim(&cp);
1287 if (!arg || !*arg || *arg == '#')
1288 return 0;
1289 intptr = NULL;
1290 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +10001291 opcode = parse_token(arg, filename, linenum, &flags);
1292
1293 if (activep == NULL) { /* We are processing a command line directive */
1294 cmdline = 1;
1295 activep = &cmdline;
1296 }
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00001297 if (*activep && opcode != sMatch && opcode != sInclude)
Darren Tucker45150472006-07-12 22:34:17 +10001298 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
1299 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001300 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +10001301 fatal("%s line %d: Directive '%s' is not allowed "
1302 "within a Match block", filename, linenum, arg);
1303 } else { /* this is a directive we have already processed */
1304 while (arg)
1305 arg = strdelim(&cp);
1306 return 0;
1307 }
1308 }
1309
Ben Lindstromade03f62001-12-06 18:22:17 +00001310 switch (opcode) {
1311 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +10001312 case sUsePAM:
1313 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +00001314 goto parse_flag;
1315
1316 /* Standard Options */
1317 case sBadOption:
1318 return -1;
1319 case sPort:
1320 /* ignore ports from configfile if cmdline specifies ports */
1321 if (options->ports_from_cmdline)
1322 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +00001323 if (options->num_ports >= MAX_PORTS)
1324 fatal("%s line %d: too many ports.",
1325 filename, linenum);
1326 arg = strdelim(&cp);
1327 if (!arg || *arg == '\0')
1328 fatal("%s line %d: missing port number.",
1329 filename, linenum);
1330 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +11001331 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +00001332 fatal("%s line %d: Badly formatted port number.",
1333 filename, linenum);
1334 break;
1335
Ben Lindstromade03f62001-12-06 18:22:17 +00001336 case sLoginGraceTime:
1337 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +10001338 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +00001339 arg = strdelim(&cp);
1340 if (!arg || *arg == '\0')
1341 fatal("%s line %d: missing time value.",
1342 filename, linenum);
1343 if ((value = convtime(arg)) == -1)
1344 fatal("%s line %d: invalid time value.",
1345 filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001346 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001347 *intptr = value;
1348 break;
1349
Ben Lindstromade03f62001-12-06 18:22:17 +00001350 case sListenAddress:
1351 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001352 if (arg == NULL || *arg == '\0')
1353 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +00001354 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +10001355 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
1356 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
1357 && strchr(p+1, ':') != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001358 port = 0;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001359 p = arg;
1360 } else {
dtucker@openbsd.orgd05ea252019-01-23 21:50:56 +00001361 arg2 = NULL;
dtucker@openbsd.org281ce042019-01-24 02:34:52 +00001362 ch = '\0';
dtucker@openbsd.orgd05ea252019-01-23 21:50:56 +00001363 p = hpdelim2(&arg, &ch);
1364 if (p == NULL || ch == '/')
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001365 fatal("%s line %d: bad address:port usage",
1366 filename, linenum);
1367 p = cleanhostname(p);
1368 if (arg == NULL)
1369 port = 0;
1370 else if ((port = a2port(arg)) <= 0)
1371 fatal("%s line %d: bad port number",
1372 filename, linenum);
1373 }
1374 /* Optional routing table */
1375 arg2 = NULL;
1376 if ((arg = strdelim(&cp)) != NULL) {
1377 if (strcmp(arg, "rdomain") != 0 ||
1378 (arg2 = strdelim(&cp)) == NULL)
1379 fatal("%s line %d: bad ListenAddress syntax",
1380 filename, linenum);
1381 if (!valid_rdomain(arg2))
1382 fatal("%s line %d: bad routing domain",
1383 filename, linenum);
1384 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001385
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001386 queue_listen_addr(options, p, arg2, port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001387
Ben Lindstromade03f62001-12-06 18:22:17 +00001388 break;
1389
Darren Tucker0f383232005-01-20 10:57:56 +11001390 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +10001391 intptr = &options->address_family;
1392 multistate_ptr = multistate_addressfamily;
Damien Miller33322122011-06-20 14:43:11 +10001393 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +11001394 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +10001395 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +10001396 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +10001397 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +10001398 value = -1;
1399 for (i = 0; multistate_ptr[i].key != NULL; i++) {
1400 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
1401 value = multistate_ptr[i].value;
1402 break;
1403 }
1404 }
1405 if (value == -1)
1406 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +11001407 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +10001408 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +11001409 *intptr = value;
1410 break;
1411
Ben Lindstromade03f62001-12-06 18:22:17 +00001412 case sHostKeyFile:
Ben Lindstromade03f62001-12-06 18:22:17 +00001413 arg = strdelim(&cp);
1414 if (!arg || *arg == '\0')
1415 fatal("%s line %d: missing file name.",
1416 filename, linenum);
djm@openbsd.org928f1232018-11-19 04:12:32 +00001417 if (*activep) {
1418 servconf_add_hostkey(filename, linenum,
1419 options, arg, 1);
1420 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001421 break;
1422
Damien Miller85b45e02013-07-20 13:21:52 +10001423 case sHostKeyAgent:
1424 charptr = &options->host_key_agent;
1425 arg = strdelim(&cp);
1426 if (!arg || *arg == '\0')
1427 fatal("%s line %d: missing socket name.",
1428 filename, linenum);
1429 if (*activep && *charptr == NULL)
1430 *charptr = !strcmp(arg, SSH_AUTHSOCKET_ENV_NAME) ?
1431 xstrdup(arg) : derelativise_path(arg);
1432 break;
1433
Damien Miller0a80ca12010-02-27 07:55:05 +11001434 case sHostCertificate:
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001435 arg = strdelim(&cp);
1436 if (!arg || *arg == '\0')
1437 fatal("%s line %d: missing file name.",
1438 filename, linenum);
1439 if (*activep)
1440 servconf_add_hostcert(filename, linenum, options, arg);
1441 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001442
Ben Lindstromade03f62001-12-06 18:22:17 +00001443 case sPidFile:
1444 charptr = &options->pid_file;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001445 parse_filename:
1446 arg = strdelim(&cp);
1447 if (!arg || *arg == '\0')
1448 fatal("%s line %d: missing file name.",
1449 filename, linenum);
1450 if (*activep && *charptr == NULL) {
1451 *charptr = derelativise_path(arg);
1452 /* increase optional counter */
1453 if (intptr != NULL)
1454 *intptr = *intptr + 1;
1455 }
1456 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001457
1458 case sPermitRootLogin:
1459 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +10001460 multistate_ptr = multistate_permitrootlogin;
1461 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001462
1463 case sIgnoreRhosts:
1464 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +10001465 parse_flag:
djm@openbsd.org@openbsd.org33edb6e2017-11-03 05:18:44 +00001466 multistate_ptr = multistate_flag;
1467 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001468
1469 case sIgnoreUserKnownHosts:
1470 intptr = &options->ignore_user_known_hosts;
1471 goto parse_flag;
1472
Ben Lindstromade03f62001-12-06 18:22:17 +00001473 case sHostbasedAuthentication:
1474 intptr = &options->hostbased_authentication;
1475 goto parse_flag;
1476
1477 case sHostbasedUsesNameFromPacketOnly:
1478 intptr = &options->hostbased_uses_name_from_packet_only;
1479 goto parse_flag;
1480
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001481 case sHostbasedAcceptedKeyTypes:
1482 charptr = &options->hostbased_key_types;
1483 parse_keytypes:
1484 arg = strdelim(&cp);
1485 if (!arg || *arg == '\0')
1486 fatal("%s line %d: Missing argument.",
1487 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001488 if (*arg != '-' &&
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001489 !sshkey_names_valid2(*arg == '+' || *arg == '^' ?
1490 arg + 1 : arg, 1))
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001491 fatal("%s line %d: Bad key types '%s'.",
1492 filename, linenum, arg ? arg : "<NONE>");
1493 if (*activep && *charptr == NULL)
1494 *charptr = xstrdup(arg);
1495 break;
1496
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001497 case sHostKeyAlgorithms:
1498 charptr = &options->hostkeyalgorithms;
1499 goto parse_keytypes;
1500
djm@openbsd.org86e57372018-09-20 03:28:06 +00001501 case sCASignatureAlgorithms:
1502 charptr = &options->ca_sign_algorithms;
1503 goto parse_keytypes;
1504
Ben Lindstromade03f62001-12-06 18:22:17 +00001505 case sPubkeyAuthentication:
1506 intptr = &options->pubkey_authentication;
1507 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001508
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001509 case sPubkeyAcceptedKeyTypes:
1510 charptr = &options->pubkey_key_types;
1511 goto parse_keytypes;
1512
djm@openbsd.org0fddf292019-11-25 00:52:46 +00001513 case sPubkeyAuthOptions:
1514 intptr = &options->pubkey_auth_options;
1515 value = 0;
1516 while ((arg = strdelim(&cp)) && *arg != '\0') {
1517 if (strcasecmp(arg, "none") == 0)
1518 continue;
1519 if (strcasecmp(arg, "touch-required") == 0)
1520 value |= PUBKEYAUTH_TOUCH_REQUIRED;
1521 else {
1522 fatal("%s line %d: unsupported "
1523 "PubkeyAuthOptions option %s",
1524 filename, linenum, arg);
1525 }
1526 }
1527 if (*activep && *intptr == -1)
1528 *intptr = value;
1529 break;
1530
Ben Lindstromade03f62001-12-06 18:22:17 +00001531 case sKerberosAuthentication:
1532 intptr = &options->kerberos_authentication;
1533 goto parse_flag;
1534
1535 case sKerberosOrLocalPasswd:
1536 intptr = &options->kerberos_or_local_passwd;
1537 goto parse_flag;
1538
1539 case sKerberosTicketCleanup:
1540 intptr = &options->kerberos_ticket_cleanup;
1541 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001542
Darren Tucker22ef5082003-12-31 11:37:34 +11001543 case sKerberosGetAFSToken:
1544 intptr = &options->kerberos_get_afs_token;
1545 goto parse_flag;
1546
Darren Tucker0efd1552003-08-26 11:49:55 +10001547 case sGssAuthentication:
1548 intptr = &options->gss_authentication;
1549 goto parse_flag;
1550
1551 case sGssCleanupCreds:
1552 intptr = &options->gss_cleanup_creds;
1553 goto parse_flag;
1554
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +00001555 case sGssStrictAcceptor:
1556 intptr = &options->gss_strict_acceptor;
1557 goto parse_flag;
1558
Ben Lindstromade03f62001-12-06 18:22:17 +00001559 case sPasswordAuthentication:
1560 intptr = &options->password_authentication;
1561 goto parse_flag;
1562
1563 case sKbdInteractiveAuthentication:
1564 intptr = &options->kbd_interactive_authentication;
1565 goto parse_flag;
1566
1567 case sChallengeResponseAuthentication:
1568 intptr = &options->challenge_response_authentication;
1569 goto parse_flag;
1570
1571 case sPrintMotd:
1572 intptr = &options->print_motd;
1573 goto parse_flag;
1574
1575 case sPrintLastLog:
1576 intptr = &options->print_lastlog;
1577 goto parse_flag;
1578
1579 case sX11Forwarding:
1580 intptr = &options->x11_forwarding;
1581 goto parse_flag;
1582
1583 case sX11DisplayOffset:
1584 intptr = &options->x11_display_offset;
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +00001585 parse_int:
1586 arg = strdelim(&cp);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001587 if ((errstr = atoi_err(arg, &value)) != NULL)
1588 fatal("%s line %d: integer value %s.",
1589 filename, linenum, errstr);
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +00001590 if (*activep && *intptr == -1)
1591 *intptr = value;
1592 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001593
Damien Miller95c249f2002-02-05 12:11:34 +11001594 case sX11UseLocalhost:
1595 intptr = &options->x11_use_localhost;
1596 goto parse_flag;
1597
Ben Lindstromade03f62001-12-06 18:22:17 +00001598 case sXAuthLocation:
1599 charptr = &options->xauth_location;
1600 goto parse_filename;
1601
Damien Miller5ff30c62013-10-30 22:21:50 +11001602 case sPermitTTY:
1603 intptr = &options->permit_tty;
1604 goto parse_flag;
1605
Damien Miller72e6b5c2014-07-04 09:00:04 +10001606 case sPermitUserRC:
1607 intptr = &options->permit_user_rc;
1608 goto parse_flag;
1609
Ben Lindstromade03f62001-12-06 18:22:17 +00001610 case sStrictModes:
1611 intptr = &options->strict_modes;
1612 goto parse_flag;
1613
Damien Miller12c150e2003-12-17 16:31:10 +11001614 case sTCPKeepAlive:
1615 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001616 goto parse_flag;
1617
1618 case sEmptyPasswd:
1619 intptr = &options->permit_empty_passwd;
1620 goto parse_flag;
1621
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001622 case sPermitUserEnvironment:
1623 intptr = &options->permit_user_env;
djm@openbsd.org95344c22018-07-03 10:59:35 +00001624 charptr = &options->permit_user_env_whitelist;
1625 arg = strdelim(&cp);
1626 if (!arg || *arg == '\0')
1627 fatal("%s line %d: missing argument.",
1628 filename, linenum);
1629 value = 0;
1630 p = NULL;
1631 if (strcmp(arg, "yes") == 0)
1632 value = 1;
1633 else if (strcmp(arg, "no") == 0)
1634 value = 0;
1635 else {
1636 /* Pattern-list specified */
1637 value = 1;
1638 p = xstrdup(arg);
1639 }
1640 if (*activep && *intptr == -1) {
1641 *intptr = value;
1642 *charptr = p;
1643 p = NULL;
1644 }
1645 free(p);
1646 break;
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001647
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001648 case sCompression:
1649 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001650 multistate_ptr = multistate_compression;
1651 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001652
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001653 case sRekeyLimit:
1654 arg = strdelim(&cp);
1655 if (!arg || *arg == '\0')
1656 fatal("%.200s line %d: Missing argument.", filename,
1657 linenum);
1658 if (strcmp(arg, "default") == 0) {
1659 val64 = 0;
1660 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001661 if (scan_scaled(arg, &val64) == -1)
1662 fatal("%.200s line %d: Bad number '%s': %s",
1663 filename, linenum, arg, strerror(errno));
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001664 if (val64 != 0 && val64 < 16)
1665 fatal("%.200s line %d: RekeyLimit too small",
1666 filename, linenum);
1667 }
1668 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001669 options->rekey_limit = val64;
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001670 if (cp != NULL) { /* optional rekey interval present */
1671 if (strcmp(cp, "none") == 0) {
1672 (void)strdelim(&cp); /* discard */
1673 break;
1674 }
1675 intptr = &options->rekey_interval;
1676 goto parse_time;
1677 }
1678 break;
1679
Ben Lindstromade03f62001-12-06 18:22:17 +00001680 case sGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +10001681 intptr = &options->fwd_opts.gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001682 multistate_ptr = multistate_gatewayports;
1683 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001684
Damien Miller3a961dc2003-06-03 10:25:48 +10001685 case sUseDNS:
1686 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001687 goto parse_flag;
1688
1689 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001690 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001691 arg = strdelim(&cp);
1692 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001693 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001694 fatal("%.200s line %d: unsupported log facility '%s'",
1695 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001696 if (*log_facility_ptr == -1)
1697 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001698 break;
1699
1700 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001701 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001702 arg = strdelim(&cp);
1703 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001704 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001705 fatal("%.200s line %d: unsupported log level '%s'",
1706 filename, linenum, arg ? arg : "<NONE>");
djm@openbsd.org54cd41a2017-05-17 01:24:17 +00001707 if (*activep && *log_level_ptr == -1)
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001708 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001709 break;
1710
1711 case sAllowTcpForwarding:
1712 intptr = &options->allow_tcp_forwarding;
Damien Milleraa5b3f82012-12-03 09:50:54 +11001713 multistate_ptr = multistate_tcpfwd;
1714 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001715
Damien Miller7acefbb2014-07-18 14:11:24 +10001716 case sAllowStreamLocalForwarding:
1717 intptr = &options->allow_streamlocal_forwarding;
1718 multistate_ptr = multistate_tcpfwd;
1719 goto parse_multistate;
1720
Damien Miller4f755cd2008-05-19 14:57:41 +10001721 case sAllowAgentForwarding:
1722 intptr = &options->allow_agent_forwarding;
1723 goto parse_flag;
1724
djm@openbsd.org7844f352016-11-30 03:00:05 +00001725 case sDisableForwarding:
1726 intptr = &options->disable_forwarding;
1727 goto parse_flag;
1728
Ben Lindstromade03f62001-12-06 18:22:17 +00001729 case sAllowUsers:
1730 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.org010359b2016-11-06 05:46:37 +00001731 if (match_user(NULL, NULL, NULL, arg) == -1)
1732 fatal("%s line %d: invalid AllowUsers pattern: "
1733 "\"%.100s\"", filename, linenum, arg);
Damien Millerc24da772012-06-20 21:53:58 +10001734 if (!*activep)
1735 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001736 array_append(filename, linenum, "AllowUsers",
1737 &options->allow_users, &options->num_allow_users,
1738 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001739 }
1740 break;
1741
1742 case sDenyUsers:
1743 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.org010359b2016-11-06 05:46:37 +00001744 if (match_user(NULL, NULL, NULL, arg) == -1)
1745 fatal("%s line %d: invalid DenyUsers pattern: "
1746 "\"%.100s\"", filename, linenum, arg);
Damien Millerc24da772012-06-20 21:53:58 +10001747 if (!*activep)
1748 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001749 array_append(filename, linenum, "DenyUsers",
1750 &options->deny_users, &options->num_deny_users,
1751 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001752 }
1753 break;
1754
1755 case sAllowGroups:
1756 while ((arg = strdelim(&cp)) && *arg != '\0') {
Damien Millerc24da772012-06-20 21:53:58 +10001757 if (!*activep)
1758 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001759 array_append(filename, linenum, "AllowGroups",
1760 &options->allow_groups, &options->num_allow_groups,
1761 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001762 }
1763 break;
1764
1765 case sDenyGroups:
1766 while ((arg = strdelim(&cp)) && *arg != '\0') {
Damien Millerc24da772012-06-20 21:53:58 +10001767 if (!*activep)
1768 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001769 array_append(filename, linenum, "DenyGroups",
1770 &options->deny_groups, &options->num_deny_groups,
1771 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001772 }
1773 break;
1774
1775 case sCiphers:
1776 arg = strdelim(&cp);
1777 if (!arg || *arg == '\0')
1778 fatal("%s line %d: Missing argument.", filename, linenum);
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001779 if (*arg != '-' &&
1780 !ciphers_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg))
Ben Lindstromade03f62001-12-06 18:22:17 +00001781 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1782 filename, linenum, arg ? arg : "<NONE>");
1783 if (options->ciphers == NULL)
1784 options->ciphers = xstrdup(arg);
1785 break;
1786
1787 case sMacs:
1788 arg = strdelim(&cp);
1789 if (!arg || *arg == '\0')
1790 fatal("%s line %d: Missing argument.", filename, linenum);
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001791 if (*arg != '-' &&
1792 !mac_valid(*arg == '+' || *arg == '^' ? arg + 1 : arg))
Ben Lindstromade03f62001-12-06 18:22:17 +00001793 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1794 filename, linenum, arg ? arg : "<NONE>");
1795 if (options->macs == NULL)
1796 options->macs = xstrdup(arg);
1797 break;
1798
Damien Millerd5f62bf2010-09-24 22:11:14 +10001799 case sKexAlgorithms:
1800 arg = strdelim(&cp);
1801 if (!arg || *arg == '\0')
1802 fatal("%s line %d: Missing argument.",
1803 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001804 if (*arg != '-' &&
naddy@openbsd.org91a21352019-09-06 14:45:34 +00001805 !kex_names_valid(*arg == '+' || *arg == '^' ?
1806 arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001807 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1808 filename, linenum, arg ? arg : "<NONE>");
1809 if (options->kex_algorithms == NULL)
1810 options->kex_algorithms = xstrdup(arg);
1811 break;
1812
Ben Lindstromade03f62001-12-06 18:22:17 +00001813 case sSubsystem:
1814 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1815 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001816 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001817 }
1818 arg = strdelim(&cp);
1819 if (!arg || *arg == '\0')
1820 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001821 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001822 if (!*activep) {
1823 arg = strdelim(&cp);
1824 break;
1825 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001826 for (i = 0; i < options->num_subsystems; i++)
1827 if (strcmp(arg, options->subsystem_name[i]) == 0)
1828 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001829 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001830 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1831 arg = strdelim(&cp);
1832 if (!arg || *arg == '\0')
1833 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001834 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001835 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001836
1837 /* Collect arguments (separate to executable) */
1838 p = xstrdup(arg);
1839 len = strlen(p) + 1;
1840 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1841 len += 1 + strlen(arg);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001842 p = xreallocarray(p, 1, len);
Damien Miller917f9b62006-07-10 20:36:47 +10001843 strlcat(p, " ", len);
1844 strlcat(p, arg, len);
1845 }
1846 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001847 options->num_subsystems++;
1848 break;
1849
1850 case sMaxStartups:
1851 arg = strdelim(&cp);
1852 if (!arg || *arg == '\0')
1853 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001854 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001855 if ((n = sscanf(arg, "%d:%d:%d",
1856 &options->max_startups_begin,
1857 &options->max_startups_rate,
1858 &options->max_startups)) == 3) {
1859 if (options->max_startups_begin >
1860 options->max_startups ||
1861 options->max_startups_rate > 100 ||
1862 options->max_startups_rate < 1)
1863 fatal("%s line %d: Illegal MaxStartups spec.",
1864 filename, linenum);
1865 } else if (n != 1)
1866 fatal("%s line %d: Illegal MaxStartups spec.",
1867 filename, linenum);
1868 else
1869 options->max_startups = options->max_startups_begin;
1870 break;
1871
Darren Tucker89413db2004-05-24 10:36:23 +10001872 case sMaxAuthTries:
1873 intptr = &options->max_authtries;
1874 goto parse_int;
1875
Damien Miller7207f642008-05-19 15:34:50 +10001876 case sMaxSessions:
1877 intptr = &options->max_sessions;
1878 goto parse_int;
1879
Ben Lindstromade03f62001-12-06 18:22:17 +00001880 case sBanner:
1881 charptr = &options->banner;
1882 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001883
Ben Lindstromade03f62001-12-06 18:22:17 +00001884 /*
1885 * These options can contain %X options expanded at
1886 * connect time, so that you can specify paths like:
1887 *
1888 * AuthorizedKeysFile /etc/ssh_keys/%u
1889 */
1890 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001891 if (*activep && options->num_authkeys_files == 0) {
1892 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001893 arg = tilde_expand_filename(arg, getuid());
1894 array_append(filename, linenum,
1895 "AuthorizedKeysFile",
1896 &options->authorized_keys_files,
1897 &options->num_authkeys_files, arg);
1898 free(arg);
Damien Millerd8478b62011-05-29 21:39:36 +10001899 }
1900 }
1901 return 0;
1902
Damien Miller30da3442010-05-10 11:58:03 +10001903 case sAuthorizedPrincipalsFile:
1904 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001905 arg = strdelim(&cp);
1906 if (!arg || *arg == '\0')
1907 fatal("%s line %d: missing file name.",
1908 filename, linenum);
1909 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001910 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001911 /* increase optional counter */
1912 if (intptr != NULL)
1913 *intptr = *intptr + 1;
1914 }
1915 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001916
1917 case sClientAliveInterval:
1918 intptr = &options->client_alive_interval;
1919 goto parse_time;
1920
1921 case sClientAliveCountMax:
1922 intptr = &options->client_alive_count_max;
1923 goto parse_int;
1924
Darren Tucker46bc0752004-05-02 22:11:30 +10001925 case sAcceptEnv:
1926 while ((arg = strdelim(&cp)) && *arg != '\0') {
1927 if (strchr(arg, '=') != NULL)
1928 fatal("%s line %d: Invalid environment name.",
1929 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001930 if (!*activep)
Damien Millerc24da772012-06-20 21:53:58 +10001931 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001932 array_append(filename, linenum, "AcceptEnv",
1933 &options->accept_env, &options->num_accept_env,
1934 arg);
Darren Tucker46bc0752004-05-02 22:11:30 +10001935 }
1936 break;
1937
djm@openbsd.org28013752018-06-09 03:03:10 +00001938 case sSetEnv:
1939 uvalue = options->num_setenv;
1940 while ((arg = strdelimw(&cp)) && *arg != '\0') {
1941 if (strchr(arg, '=') == NULL)
1942 fatal("%s line %d: Invalid environment.",
1943 filename, linenum);
1944 if (!*activep || uvalue != 0)
1945 continue;
1946 array_append(filename, linenum, "SetEnv",
1947 &options->setenv, &options->num_setenv, arg);
1948 }
1949 break;
1950
Damien Millerd27b9472005-12-13 19:29:02 +11001951 case sPermitTunnel:
1952 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001953 arg = strdelim(&cp);
1954 if (!arg || *arg == '\0')
1955 fatal("%s line %d: Missing yes/point-to-point/"
1956 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001957 value = -1;
1958 for (i = 0; tunmode_desc[i].val != -1; i++)
1959 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1960 value = tunmode_desc[i].val;
1961 break;
1962 }
1963 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001964 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1965 "no argument: %s", filename, linenum, arg);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001966 if (*activep && *intptr == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001967 *intptr = value;
1968 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001969
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00001970 case sInclude:
1971 if (cmdline) {
1972 fatal("Include directive not supported as a "
1973 "command-line option");
1974 }
1975 value = 0;
1976 while ((arg2 = strdelim(&cp)) != NULL && *arg2 != '\0') {
1977 value++;
1978 found = 0;
1979 if (*arg2 != '/' && *arg2 != '~') {
1980 xasprintf(&arg, "%s/%s", SSHDIR, arg);
1981 } else
1982 arg = xstrdup(arg2);
1983
1984 /*
1985 * Don't let included files clobber the containing
1986 * file's Match state.
1987 */
1988 oactive = *activep;
1989
1990 /* consult cache of include files */
1991 TAILQ_FOREACH(item, includes, entry) {
1992 if (strcmp(item->selector, arg) != 0)
1993 continue;
1994 if (item->filename != NULL) {
1995 parse_server_config_depth(options,
1996 item->filename, item->contents,
1997 includes, connectinfo,
1998 (oactive ? 0 : SSHCFG_NEVERMATCH),
1999 activep, depth + 1);
2000 }
2001 found = 1;
2002 *activep = oactive;
2003 }
2004 if (found != 0) {
2005 free(arg);
2006 continue;
2007 }
2008
2009 /* requested glob was not in cache */
2010 debug2("%s line %d: new include %s",
2011 filename, linenum, arg);
2012 if ((r = glob(arg, 0, NULL, &gbuf)) != 0) {
2013 if (r != GLOB_NOMATCH) {
2014 fatal("%s line %d: include \"%s\" "
2015 "glob failed", filename,
2016 linenum, arg);
2017 }
2018 /*
2019 * If no entry matched then record a
2020 * placeholder to skip later glob calls.
2021 */
2022 debug2("%s line %d: no match for %s",
2023 filename, linenum, arg);
2024 item = xcalloc(1, sizeof(*item));
2025 item->selector = strdup(arg);
2026 TAILQ_INSERT_TAIL(includes,
2027 item, entry);
2028 }
2029 if (gbuf.gl_pathc > INT_MAX)
2030 fatal("%s: too many glob results", __func__);
2031 for (n = 0; n < (int)gbuf.gl_pathc; n++) {
2032 debug2("%s line %d: including %s",
2033 filename, linenum, gbuf.gl_pathv[n]);
2034 item = xcalloc(1, sizeof(*item));
2035 item->selector = strdup(arg);
2036 item->filename = strdup(gbuf.gl_pathv[n]);
2037 if ((item->contents = sshbuf_new()) == NULL) {
2038 fatal("%s: sshbuf_new failed",
2039 __func__);
2040 }
2041 load_server_config(item->filename,
2042 item->contents);
2043 parse_server_config_depth(options,
2044 item->filename, item->contents,
2045 includes, connectinfo,
2046 (oactive ? 0 : SSHCFG_NEVERMATCH),
2047 activep, depth + 1);
2048 *activep = oactive;
2049 TAILQ_INSERT_TAIL(includes, item, entry);
2050 }
2051 globfree(&gbuf);
2052 free(arg);
2053 }
2054 if (value == 0) {
2055 fatal("%s line %d: Include missing filename argument",
2056 filename, linenum);
2057 }
2058 break;
2059
Darren Tucker45150472006-07-12 22:34:17 +10002060 case sMatch:
2061 if (cmdline)
2062 fatal("Match directive not supported as a command-line "
2063 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002064 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10002065 if (value < 0)
2066 fatal("%s line %d: Bad Match condition", filename,
2067 linenum);
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00002068 *activep = (inc_flags & SSHCFG_NEVERMATCH) ? 0 : value;
Darren Tucker45150472006-07-12 22:34:17 +10002069 break;
2070
djm@openbsd.org93c06ab2018-06-06 18:23:32 +00002071 case sPermitListen:
Damien Miller9b439df2006-07-24 14:04:00 +10002072 case sPermitOpen:
djm@openbsd.org93c06ab2018-06-06 18:23:32 +00002073 if (opcode == sPermitListen) {
2074 uintptr = &options->num_permitted_listens;
2075 chararrayptr = &options->permitted_listens;
djm@openbsd.org115063a2018-06-06 18:22:41 +00002076 } else {
2077 uintptr = &options->num_permitted_opens;
2078 chararrayptr = &options->permitted_opens;
2079 }
Damien Miller9b439df2006-07-24 14:04:00 +10002080 arg = strdelim(&cp);
2081 if (!arg || *arg == '\0')
djm@openbsd.org115063a2018-06-06 18:22:41 +00002082 fatal("%s line %d: missing %s specification",
2083 filename, linenum, lookup_opcode_name(opcode));
2084 uvalue = *uintptr; /* modified later */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002085 if (strcmp(arg, "any") == 0 || strcmp(arg, "none") == 0) {
djm@openbsd.org115063a2018-06-06 18:22:41 +00002086 if (*activep && uvalue == 0) {
2087 *uintptr = 1;
2088 *chararrayptr = xcalloc(1,
2089 sizeof(**chararrayptr));
2090 (*chararrayptr)[0] = xstrdup(arg);
dtucker@openbsd.org30484e52017-09-18 09:41:52 +00002091 }
Damien Millerc6081482012-04-22 11:18:53 +10002092 break;
2093 }
Damien Millera765cf42006-07-24 14:08:13 +10002094 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
djm@openbsd.org87ddd672018-06-19 02:59:41 +00002095 if (opcode == sPermitListen &&
2096 strchr(arg, ':') == NULL) {
2097 /*
2098 * Allow bare port number for PermitListen
2099 * to indicate a wildcard listen host.
2100 */
2101 xasprintf(&arg2, "*:%s", arg);
2102 } else {
2103 arg2 = xstrdup(arg);
dtucker@openbsd.org281ce042019-01-24 02:34:52 +00002104 ch = '\0';
dtucker@openbsd.orgd05ea252019-01-23 21:50:56 +00002105 p = hpdelim2(&arg, &ch);
2106 if (p == NULL || ch == '/') {
djm@openbsd.org87ddd672018-06-19 02:59:41 +00002107 fatal("%s line %d: missing host in %s",
2108 filename, linenum,
2109 lookup_opcode_name(opcode));
2110 }
2111 p = cleanhostname(p);
djm@openbsd.org115063a2018-06-06 18:22:41 +00002112 }
djm@openbsd.org115063a2018-06-06 18:22:41 +00002113 if (arg == NULL ||
2114 ((port = permitopen_port(arg)) < 0)) {
2115 fatal("%s line %d: bad port number in %s",
2116 filename, linenum,
2117 lookup_opcode_name(opcode));
2118 }
2119 if (*activep && uvalue == 0) {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00002120 array_append(filename, linenum,
djm@openbsd.org115063a2018-06-06 18:22:41 +00002121 lookup_opcode_name(opcode),
2122 chararrayptr, uintptr, arg2);
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00002123 }
2124 free(arg2);
Damien Millera765cf42006-07-24 14:08:13 +10002125 }
Damien Miller9b439df2006-07-24 14:04:00 +10002126 break;
2127
Damien Millere2754432006-07-24 14:06:47 +10002128 case sForceCommand:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00002129 if (cp == NULL || *cp == '\0')
Damien Millere2754432006-07-24 14:06:47 +10002130 fatal("%.200s line %d: Missing argument.", filename,
2131 linenum);
2132 len = strspn(cp, WHITESPACE);
2133 if (*activep && options->adm_forced_command == NULL)
2134 options->adm_forced_command = xstrdup(cp + len);
2135 return 0;
2136
Damien Millerd8cb1f12008-02-10 22:40:12 +11002137 case sChrootDirectory:
2138 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11002139
2140 arg = strdelim(&cp);
2141 if (!arg || *arg == '\0')
2142 fatal("%s line %d: missing file name.",
2143 filename, linenum);
2144 if (*activep && *charptr == NULL)
2145 *charptr = xstrdup(arg);
2146 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11002147
Damien Miller1aed65e2010-03-04 21:53:35 +11002148 case sTrustedUserCAKeys:
2149 charptr = &options->trusted_user_ca_keys;
2150 goto parse_filename;
2151
2152 case sRevokedKeys:
2153 charptr = &options->revoked_keys_file;
2154 goto parse_filename;
2155
djm@openbsd.org56584cc2019-12-15 18:57:30 +00002156 case sSecurityKeyProvider:
2157 charptr = &options->sk_provider;
djm@openbsd.org747e2512019-12-15 20:57:15 +00002158 arg = strdelim(&cp);
2159 if (!arg || *arg == '\0')
2160 fatal("%s line %d: missing file name.",
2161 filename, linenum);
2162 if (*activep && *charptr == NULL) {
2163 *charptr = strcasecmp(arg, "internal") == 0 ?
2164 xstrdup(arg) : derelativise_path(arg);
2165 /* increase optional counter */
2166 if (intptr != NULL)
2167 *intptr = *intptr + 1;
2168 }
2169 break;
djm@openbsd.org56584cc2019-12-15 18:57:30 +00002170
Damien Miller0dac6fb2010-11-20 15:19:38 +11002171 case sIPQoS:
2172 arg = strdelim(&cp);
2173 if ((value = parse_ipqos(arg)) == -1)
2174 fatal("%s line %d: Bad IPQoS value: %s",
2175 filename, linenum, arg);
2176 arg = strdelim(&cp);
2177 if (arg == NULL)
2178 value2 = value;
2179 else if ((value2 = parse_ipqos(arg)) == -1)
2180 fatal("%s line %d: Bad IPQoS value: %s",
2181 filename, linenum, arg);
2182 if (*activep) {
2183 options->ip_qos_interactive = value;
2184 options->ip_qos_bulk = value2;
2185 }
2186 break;
2187
Damien Miller23528812012-04-22 11:24:43 +10002188 case sVersionAddendum:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00002189 if (cp == NULL || *cp == '\0')
Damien Miller23528812012-04-22 11:24:43 +10002190 fatal("%.200s line %d: Missing argument.", filename,
2191 linenum);
2192 len = strspn(cp, WHITESPACE);
2193 if (*activep && options->version_addendum == NULL) {
2194 if (strcasecmp(cp + len, "none") == 0)
2195 options->version_addendum = xstrdup("");
2196 else if (strchr(cp + len, '\r') != NULL)
2197 fatal("%.200s line %d: Invalid argument",
2198 filename, linenum);
2199 else
2200 options->version_addendum = xstrdup(cp + len);
2201 }
2202 return 0;
2203
Damien Miller09d3e122012-10-31 08:58:58 +11002204 case sAuthorizedKeysCommand:
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00002205 if (cp == NULL)
2206 fatal("%.200s line %d: Missing argument.", filename,
2207 linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11002208 len = strspn(cp, WHITESPACE);
2209 if (*activep && options->authorized_keys_command == NULL) {
2210 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
2211 fatal("%.200s line %d: AuthorizedKeysCommand "
2212 "must be an absolute path",
2213 filename, linenum);
2214 options->authorized_keys_command = xstrdup(cp + len);
2215 }
2216 return 0;
2217
2218 case sAuthorizedKeysCommandUser:
2219 charptr = &options->authorized_keys_command_user;
2220
2221 arg = strdelim(&cp);
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00002222 if (!arg || *arg == '\0')
2223 fatal("%s line %d: missing AuthorizedKeysCommandUser "
2224 "argument.", filename, linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11002225 if (*activep && *charptr == NULL)
2226 *charptr = xstrdup(arg);
2227 break;
2228
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +00002229 case sAuthorizedPrincipalsCommand:
2230 if (cp == NULL)
2231 fatal("%.200s line %d: Missing argument.", filename,
2232 linenum);
2233 len = strspn(cp, WHITESPACE);
2234 if (*activep &&
2235 options->authorized_principals_command == NULL) {
2236 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
2237 fatal("%.200s line %d: "
2238 "AuthorizedPrincipalsCommand must be "
2239 "an absolute path", filename, linenum);
2240 options->authorized_principals_command =
2241 xstrdup(cp + len);
2242 }
2243 return 0;
2244
2245 case sAuthorizedPrincipalsCommandUser:
2246 charptr = &options->authorized_principals_command_user;
2247
2248 arg = strdelim(&cp);
2249 if (!arg || *arg == '\0')
2250 fatal("%s line %d: missing "
2251 "AuthorizedPrincipalsCommandUser argument.",
2252 filename, linenum);
2253 if (*activep && *charptr == NULL)
2254 *charptr = xstrdup(arg);
2255 break;
2256
Damien Millera6e3f012012-11-04 23:21:40 +11002257 case sAuthenticationMethods:
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00002258 if (options->num_auth_methods == 0) {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002259 value = 0; /* seen "any" pseudo-method */
djm@openbsd.org001aa552018-04-10 00:10:49 +00002260 value2 = 0; /* successfully parsed any method */
Damien Millera6e3f012012-11-04 23:21:40 +11002261 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002262 if (strcmp(arg, "any") == 0) {
2263 if (options->num_auth_methods > 0) {
2264 fatal("%s line %d: \"any\" "
2265 "must appear alone in "
2266 "AuthenticationMethods",
2267 filename, linenum);
2268 }
2269 value = 1;
2270 } else if (value) {
2271 fatal("%s line %d: \"any\" must appear "
2272 "alone in AuthenticationMethods",
2273 filename, linenum);
2274 } else if (auth2_methods_valid(arg, 0) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +11002275 fatal("%s line %d: invalid "
2276 "authentication method list.",
2277 filename, linenum);
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002278 }
djm@openbsd.org46ecd192016-06-23 05:17:51 +00002279 value2 = 1;
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00002280 if (!*activep)
2281 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00002282 array_append(filename, linenum,
2283 "AuthenticationMethods",
2284 &options->auth_methods,
2285 &options->num_auth_methods, arg);
Damien Millera6e3f012012-11-04 23:21:40 +11002286 }
djm@openbsd.org46ecd192016-06-23 05:17:51 +00002287 if (value2 == 0) {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002288 fatal("%s line %d: no AuthenticationMethods "
2289 "specified", filename, linenum);
2290 }
Damien Millera6e3f012012-11-04 23:21:40 +11002291 }
2292 return 0;
2293
Damien Miller7acefbb2014-07-18 14:11:24 +10002294 case sStreamLocalBindMask:
2295 arg = strdelim(&cp);
2296 if (!arg || *arg == '\0')
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00002297 fatal("%s line %d: missing StreamLocalBindMask "
2298 "argument.", filename, linenum);
Damien Miller7acefbb2014-07-18 14:11:24 +10002299 /* Parse mode in octal format */
2300 value = strtol(arg, &p, 8);
2301 if (arg == p || value < 0 || value > 0777)
2302 fatal("%s line %d: Bad mask.", filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00002303 if (*activep)
2304 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
Damien Miller7acefbb2014-07-18 14:11:24 +10002305 break;
2306
2307 case sStreamLocalBindUnlink:
2308 intptr = &options->fwd_opts.streamlocal_bind_unlink;
2309 goto parse_flag;
2310
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002311 case sFingerprintHash:
2312 arg = strdelim(&cp);
2313 if (!arg || *arg == '\0')
2314 fatal("%.200s line %d: Missing argument.",
2315 filename, linenum);
2316 if ((value = ssh_digest_alg_by_name(arg)) == -1)
2317 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
2318 filename, linenum, arg);
2319 if (*activep)
2320 options->fingerprint_hash = value;
2321 break;
2322
djm@openbsd.org8f574952017-06-24 06:34:38 +00002323 case sExposeAuthInfo:
2324 intptr = &options->expose_userauth_info;
2325 goto parse_flag;
2326
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002327 case sRDomain:
2328 charptr = &options->routing_domain;
2329 arg = strdelim(&cp);
2330 if (!arg || *arg == '\0')
2331 fatal("%.200s line %d: Missing argument.",
2332 filename, linenum);
2333 if (strcasecmp(arg, "none") != 0 && strcmp(arg, "%D") != 0 &&
2334 !valid_rdomain(arg))
2335 fatal("%s line %d: bad routing domain",
2336 filename, linenum);
2337 if (*activep && *charptr == NULL)
2338 *charptr = xstrdup(arg);
dtucker@openbsd.org168ecec2017-12-05 23:56:07 +00002339 break;
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002340
Ben Lindstromade03f62001-12-06 18:22:17 +00002341 case sDeprecated:
djm@openbsd.orgae363d72016-08-25 23:57:54 +00002342 case sIgnore:
Damien Millerf9b3feb2003-05-16 11:38:32 +10002343 case sUnsupported:
djm@openbsd.orgae363d72016-08-25 23:57:54 +00002344 do_log2(opcode == sIgnore ?
2345 SYSLOG_LEVEL_DEBUG2 : SYSLOG_LEVEL_INFO,
2346 "%s line %d: %s option %s", filename, linenum,
2347 opcode == sUnsupported ? "Unsupported" : "Deprecated", arg);
Damien Millerf9b3feb2003-05-16 11:38:32 +10002348 while (arg)
2349 arg = strdelim(&cp);
2350 break;
2351
Ben Lindstromade03f62001-12-06 18:22:17 +00002352 default:
2353 fatal("%s line %d: Missing handler for opcode %s (%d)",
2354 filename, linenum, arg, opcode);
2355 }
2356 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
2357 fatal("%s line %d: garbage at end of line; \"%.200s\".",
2358 filename, linenum, arg);
2359 return 0;
2360}
2361
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00002362int
2363process_server_config_line(ServerOptions *options, char *line,
2364 const char *filename, int linenum, int *activep,
2365 struct connection_info *connectinfo, struct include_list *includes)
2366{
2367 return process_server_config_line_depth(options, line, filename,
2368 linenum, activep, connectinfo, 0, 0, includes);
2369}
2370
2371
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002372/* Reads the server configuration file. */
2373
Damien Miller4af51302000-04-16 11:18:38 +10002374void
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00002375load_server_config(const char *filename, struct sshbuf *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002376{
markus@openbsd.org7f906352018-06-06 18:29:18 +00002377 char *line = NULL, *cp;
2378 size_t linesize = 0;
Ben Lindstrome1353632002-06-23 21:29:23 +00002379 FILE *f;
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00002380 int r, lineno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002381
Darren Tucker645ab752004-06-25 13:33:20 +10002382 debug2("%s: filename %s", __func__, filename);
2383 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11002384 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002385 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11002386 }
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00002387 sshbuf_reset(conf);
markus@openbsd.org7f906352018-06-06 18:29:18 +00002388 while (getline(&line, &linesize, f) != -1) {
Damien Miller46cb75a2012-07-31 12:22:37 +10002389 lineno++;
Darren Tucker645ab752004-06-25 13:33:20 +10002390 /*
2391 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10002392 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10002393 * line numbers later for error messages
2394 */
2395 if ((cp = strchr(line, '#')) != NULL)
2396 memcpy(cp, "\n", 2);
2397 cp = line + strspn(line, " \t\r");
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00002398 if ((r = sshbuf_put(conf, cp, strlen(cp))) != 0)
2399 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker645ab752004-06-25 13:33:20 +10002400 }
markus@openbsd.org7f906352018-06-06 18:29:18 +00002401 free(line);
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00002402 if ((r = sshbuf_put_u8(conf, 0)) != 0)
2403 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker645ab752004-06-25 13:33:20 +10002404 fclose(f);
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00002405 debug2("%s: done config len = %zu", __func__, sshbuf_len(conf));
Darren Tucker645ab752004-06-25 13:33:20 +10002406}
2407
2408void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002409parse_server_match_config(ServerOptions *options,
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00002410 struct include_list *includes, struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10002411{
Darren Tucker45150472006-07-12 22:34:17 +10002412 ServerOptions mo;
2413
2414 initialize_server_options(&mo);
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00002415 parse_server_config(&mo, "reprocess config", cfg, includes,
2416 connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11002417 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10002418}
2419
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002420int parse_server_match_testspec(struct connection_info *ci, char *spec)
2421{
2422 char *p;
2423
2424 while ((p = strsep(&spec, ",")) && *p != '\0') {
2425 if (strncmp(p, "addr=", 5) == 0) {
2426 ci->address = xstrdup(p + 5);
2427 } else if (strncmp(p, "host=", 5) == 0) {
2428 ci->host = xstrdup(p + 5);
2429 } else if (strncmp(p, "user=", 5) == 0) {
2430 ci->user = xstrdup(p + 5);
2431 } else if (strncmp(p, "laddr=", 6) == 0) {
2432 ci->laddress = xstrdup(p + 6);
djm@openbsd.org68af80e2017-10-25 00:19:47 +00002433 } else if (strncmp(p, "rdomain=", 8) == 0) {
2434 ci->rdomain = xstrdup(p + 8);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002435 } else if (strncmp(p, "lport=", 6) == 0) {
2436 ci->lport = a2port(p + 6);
2437 if (ci->lport == -1) {
2438 fprintf(stderr, "Invalid port '%s' in test mode"
2439 " specification %s\n", p+6, p);
2440 return -1;
2441 }
2442 } else {
2443 fprintf(stderr, "Invalid test mode specification %s\n",
2444 p);
2445 return -1;
2446 }
2447 }
2448 return 0;
2449}
2450
2451/*
Darren Tucker1629c072007-02-19 22:25:37 +11002452 * Copy any supported values that are set.
2453 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10002454 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11002455 * array values that are not used pre-authentication, because any that we
djm@openbsd.org001aa552018-04-10 00:10:49 +00002456 * do use must be explicitly sent in mm_getpwnamallow().
Darren Tucker1629c072007-02-19 22:25:37 +11002457 */
Darren Tucker45150472006-07-12 22:34:17 +10002458void
Darren Tucker1629c072007-02-19 22:25:37 +11002459copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10002460{
Damien Miller534b2cc2013-12-05 14:07:27 +11002461#define M_CP_INTOPT(n) do {\
2462 if (src->n != -1) \
2463 dst->n = src->n; \
2464} while (0)
2465
Darren Tucker1629c072007-02-19 22:25:37 +11002466 M_CP_INTOPT(password_authentication);
2467 M_CP_INTOPT(gss_authentication);
Darren Tucker1629c072007-02-19 22:25:37 +11002468 M_CP_INTOPT(pubkey_authentication);
djm@openbsd.org0fddf292019-11-25 00:52:46 +00002469 M_CP_INTOPT(pubkey_auth_options);
Darren Tucker1629c072007-02-19 22:25:37 +11002470 M_CP_INTOPT(kerberos_authentication);
2471 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10002472 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11002473 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11002474 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11002475 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11002476
2477 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002478 M_CP_INTOPT(allow_streamlocal_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10002479 M_CP_INTOPT(allow_agent_forwarding);
djm@openbsd.org7844f352016-11-30 03:00:05 +00002480 M_CP_INTOPT(disable_forwarding);
djm@openbsd.org8f574952017-06-24 06:34:38 +00002481 M_CP_INTOPT(expose_userauth_info);
Damien Millerab6de352010-06-26 09:38:45 +10002482 M_CP_INTOPT(permit_tun);
Damien Miller7acefbb2014-07-18 14:11:24 +10002483 M_CP_INTOPT(fwd_opts.gateway_ports);
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002484 M_CP_INTOPT(fwd_opts.streamlocal_bind_unlink);
Darren Tucker1629c072007-02-19 22:25:37 +11002485 M_CP_INTOPT(x11_display_offset);
2486 M_CP_INTOPT(x11_forwarding);
2487 M_CP_INTOPT(x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002488 M_CP_INTOPT(permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002489 M_CP_INTOPT(permit_user_rc);
Damien Miller7207f642008-05-19 15:34:50 +10002490 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10002491 M_CP_INTOPT(max_authtries);
markus@openbsd.orgf0ddede2016-11-23 23:14:15 +00002492 M_CP_INTOPT(client_alive_count_max);
2493 M_CP_INTOPT(client_alive_interval);
Damien Miller0dac6fb2010-11-20 15:19:38 +11002494 M_CP_INTOPT(ip_qos_interactive);
2495 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002496 M_CP_INTOPT(rekey_limit);
2497 M_CP_INTOPT(rekey_interval);
djm@openbsd.org54cd41a2017-05-17 01:24:17 +00002498 M_CP_INTOPT(log_level);
Darren Tucker1629c072007-02-19 22:25:37 +11002499
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002500 /*
2501 * The bind_mask is a mode_t that may be unsigned, so we can't use
2502 * M_CP_INTOPT - it does a signed comparison that causes compiler
2503 * warnings.
2504 */
dtucker@openbsd.org9faae502016-05-04 14:00:09 +00002505 if (src->fwd_opts.streamlocal_bind_mask != (mode_t)-1) {
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002506 dst->fwd_opts.streamlocal_bind_mask =
2507 src->fwd_opts.streamlocal_bind_mask;
2508 }
2509
Damien Miller534b2cc2013-12-05 14:07:27 +11002510 /* M_CP_STROPT and M_CP_STRARRAYOPT should not appear before here */
2511#define M_CP_STROPT(n) do {\
2512 if (src->n != NULL && dst->n != src->n) { \
2513 free(dst->n); \
2514 dst->n = src->n; \
2515 } \
2516} while(0)
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00002517#define M_CP_STRARRAYOPT(s, num_s) do {\
2518 u_int i; \
2519 if (src->num_s != 0) { \
2520 for (i = 0; i < dst->num_s; i++) \
2521 free(dst->s[i]); \
2522 free(dst->s); \
2523 dst->s = xcalloc(src->num_s, sizeof(*dst->s)); \
2524 for (i = 0; i < src->num_s; i++) \
2525 dst->s[i] = xstrdup(src->s[i]); \
2526 dst->num_s = src->num_s; \
djm@openbsd.org66bf74a2017-10-02 19:33:20 +00002527 } \
2528} while(0)
Damien Miller534b2cc2013-12-05 14:07:27 +11002529
Damien Millerf2e407e2011-05-20 19:04:14 +10002530 /* See comment in servconf.h */
2531 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10002532
djm@openbsd.orged085102015-10-29 08:05:01 +00002533 /* Arguments that accept '+...' need to be expanded */
2534 assemble_algorithms(dst);
2535
Damien Millerc2411902011-05-20 19:03:49 +10002536 /*
2537 * The only things that should be below this point are string options
2538 * which are only used after authentication.
2539 */
Damien Miller5d74e582011-05-20 19:03:31 +10002540 if (preauth)
2541 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002542
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002543 /* These options may be "none" to clear a global setting */
Damien Miller5d74e582011-05-20 19:03:31 +10002544 M_CP_STROPT(adm_forced_command);
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002545 if (option_clear_or_none(dst->adm_forced_command)) {
2546 free(dst->adm_forced_command);
2547 dst->adm_forced_command = NULL;
2548 }
Damien Miller5d74e582011-05-20 19:03:31 +10002549 M_CP_STROPT(chroot_directory);
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002550 if (option_clear_or_none(dst->chroot_directory)) {
2551 free(dst->chroot_directory);
2552 dst->chroot_directory = NULL;
2553 }
Darren Tucker45150472006-07-12 22:34:17 +10002554}
2555
Darren Tucker1629c072007-02-19 22:25:37 +11002556#undef M_CP_INTOPT
2557#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10002558#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11002559
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00002560#define SERVCONF_MAX_DEPTH 16
Darren Tucker45150472006-07-12 22:34:17 +10002561void
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00002562parse_server_config_depth(ServerOptions *options, const char *filename,
2563 struct sshbuf *conf, struct include_list *includes,
2564 struct connection_info *connectinfo, int flags, int *activep, int depth)
Darren Tucker45150472006-07-12 22:34:17 +10002565{
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00002566 int linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10002567 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10002568
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00002569 if (depth < 0 || depth > SERVCONF_MAX_DEPTH)
2570 fatal("Too many recursive configuration includes");
2571
markus@openbsd.orgc3cb7792018-07-09 21:29:36 +00002572 debug2("%s: config %s len %zu", __func__, filename, sshbuf_len(conf));
Darren Tucker645ab752004-06-25 13:33:20 +10002573
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002574 if ((obuf = cbuf = sshbuf_dup_string(conf)) == NULL)
2575 fatal("%s: sshbuf_dup_string failed", __func__);
Darren Tucker137e9c92004-08-13 21:30:24 +10002576 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11002577 while ((cp = strsep(&cbuf, "\n")) != NULL) {
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00002578 if (process_server_config_line_depth(options, cp,
2579 filename, linenum++, activep, connectinfo, flags,
2580 depth, includes) != 0)
Damien Miller95def091999-11-25 00:26:21 +11002581 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002582 }
Darren Tuckera627d422013-06-02 07:31:17 +10002583 free(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00002584 if (bad_options > 0)
2585 fatal("%s: terminating, %d bad configuration options",
2586 filename, bad_options);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00002587 process_queued_listen_addrs(options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002588}
Darren Tuckere7140f22008-06-10 23:01:51 +10002589
djm@openbsd.orgc2bd7f72020-01-31 22:42:45 +00002590void
2591parse_server_config(ServerOptions *options, const char *filename,
2592 struct sshbuf *conf, struct include_list *includes,
2593 struct connection_info *connectinfo)
2594{
2595 int active = connectinfo ? 0 : 1;
2596 parse_server_config_depth(options, filename, conf, includes,
2597 connectinfo, 0, &active, 0);
2598}
2599
Darren Tuckere7140f22008-06-10 23:01:51 +10002600static const char *
Damien Miller82c55872011-06-23 08:20:30 +10002601fmt_multistate_int(int val, const struct multistate *m)
2602{
2603 u_int i;
2604
2605 for (i = 0; m[i].key != NULL; i++) {
2606 if (m[i].value == val)
2607 return m[i].key;
2608 }
2609 return "UNKNOWN";
2610}
2611
2612static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10002613fmt_intarg(ServerOpCodes code, int val)
2614{
Damien Miller82c55872011-06-23 08:20:30 +10002615 if (val == -1)
2616 return "unset";
2617 switch (code) {
2618 case sAddressFamily:
2619 return fmt_multistate_int(val, multistate_addressfamily);
2620 case sPermitRootLogin:
2621 return fmt_multistate_int(val, multistate_permitrootlogin);
2622 case sGatewayPorts:
2623 return fmt_multistate_int(val, multistate_gatewayports);
2624 case sCompression:
2625 return fmt_multistate_int(val, multistate_compression);
Damien Milleraa5b3f82012-12-03 09:50:54 +11002626 case sAllowTcpForwarding:
2627 return fmt_multistate_int(val, multistate_tcpfwd);
Damien Miller7acefbb2014-07-18 14:11:24 +10002628 case sAllowStreamLocalForwarding:
2629 return fmt_multistate_int(val, multistate_tcpfwd);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002630 case sFingerprintHash:
2631 return ssh_digest_alg_name(val);
Damien Miller82c55872011-06-23 08:20:30 +10002632 default:
2633 switch (val) {
2634 case 0:
2635 return "no";
2636 case 1:
2637 return "yes";
2638 default:
2639 return "UNKNOWN";
2640 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002641 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002642}
2643
Darren Tuckere7140f22008-06-10 23:01:51 +10002644static void
2645dump_cfg_int(ServerOpCodes code, int val)
2646{
2647 printf("%s %d\n", lookup_opcode_name(code), val);
2648}
2649
2650static void
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002651dump_cfg_oct(ServerOpCodes code, int val)
2652{
2653 printf("%s 0%o\n", lookup_opcode_name(code), val);
2654}
2655
2656static void
Darren Tuckere7140f22008-06-10 23:01:51 +10002657dump_cfg_fmtint(ServerOpCodes code, int val)
2658{
2659 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2660}
2661
2662static void
2663dump_cfg_string(ServerOpCodes code, const char *val)
2664{
djm@openbsd.org161cf412014-12-22 07:55:51 +00002665 printf("%s %s\n", lookup_opcode_name(code),
2666 val == NULL ? "none" : val);
Darren Tuckere7140f22008-06-10 23:01:51 +10002667}
2668
2669static void
2670dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
2671{
2672 u_int i;
2673
2674 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10002675 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2676}
2677
2678static void
2679dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
2680{
2681 u_int i;
2682
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002683 if (count <= 0 && code != sAuthenticationMethods)
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002684 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002685 printf("%s", lookup_opcode_name(code));
2686 for (i = 0; i < count; i++)
2687 printf(" %s", vals[i]);
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002688 if (code == sAuthenticationMethods && count == 0)
2689 printf(" any");
Damien Millerd8478b62011-05-29 21:39:36 +10002690 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002691}
2692
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002693static char *
2694format_listen_addrs(struct listenaddr *la)
Darren Tuckere7140f22008-06-10 23:01:51 +10002695{
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002696 int r;
Darren Tuckere7140f22008-06-10 23:01:51 +10002697 struct addrinfo *ai;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002698 char addr[NI_MAXHOST], port[NI_MAXSERV];
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002699 char *laddr1 = xstrdup(""), *laddr2 = NULL;
Darren Tuckere7140f22008-06-10 23:01:51 +10002700
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002701 /*
2702 * ListenAddress must be after Port. add_one_listen_addr pushes
2703 * addresses onto a stack, so to maintain ordering we need to
2704 * print these in reverse order.
2705 */
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002706 for (ai = la->addrs; ai; ai = ai->ai_next) {
2707 if ((r = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
Darren Tuckere7140f22008-06-10 23:01:51 +10002708 sizeof(addr), port, sizeof(port),
2709 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002710 error("getnameinfo: %.100s", ssh_gai_strerror(r));
2711 continue;
Darren Tuckere7140f22008-06-10 23:01:51 +10002712 }
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002713 laddr2 = laddr1;
2714 if (ai->ai_family == AF_INET6) {
2715 xasprintf(&laddr1, "listenaddress [%s]:%s%s%s\n%s",
2716 addr, port,
2717 la->rdomain == NULL ? "" : " rdomain ",
2718 la->rdomain == NULL ? "" : la->rdomain,
2719 laddr2);
2720 } else {
2721 xasprintf(&laddr1, "listenaddress %s:%s%s%s\n%s",
2722 addr, port,
2723 la->rdomain == NULL ? "" : " rdomain ",
2724 la->rdomain == NULL ? "" : la->rdomain,
2725 laddr2);
2726 }
2727 free(laddr2);
Darren Tuckere7140f22008-06-10 23:01:51 +10002728 }
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002729 return laddr1;
2730}
2731
2732void
2733dump_config(ServerOptions *o)
2734{
2735 char *s;
2736 u_int i;
2737
2738 /* these are usually at the top of the config */
2739 for (i = 0; i < o->num_ports; i++)
2740 printf("port %d\n", o->ports[i]);
2741 dump_cfg_fmtint(sAddressFamily, o->address_family);
2742
2743 for (i = 0; i < o->num_listen_addrs; i++) {
2744 s = format_listen_addrs(&o->listen_addrs[i]);
2745 printf("%s", s);
2746 free(s);
2747 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002748
2749 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10002750#ifdef USE_PAM
Darren Tucker70860b62015-04-17 10:56:13 +10002751 dump_cfg_fmtint(sUsePAM, o->use_pam);
Damien Miller212f0b02008-07-23 17:42:29 +10002752#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002753 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
Darren Tuckere7140f22008-06-10 23:01:51 +10002754 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
2755 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11002756 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10002757 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
2758 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002759 dump_cfg_oct(sStreamLocalBindMask, o->fwd_opts.streamlocal_bind_mask);
Darren Tuckere7140f22008-06-10 23:01:51 +10002760
2761 /* formatted integer arguments */
2762 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
2763 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
2764 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
Darren Tuckere7140f22008-06-10 23:01:51 +10002765 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
2766 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
2767 o->hostbased_uses_name_from_packet_only);
Darren Tuckere7140f22008-06-10 23:01:51 +10002768 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10002769#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10002770 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
2771 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
2772 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10002773# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10002774 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10002775# endif
2776#endif
2777#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10002778 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2779 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10002780#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002781 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2782 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2783 o->kbd_interactive_authentication);
2784 dump_cfg_fmtint(sChallengeResponseAuthentication,
2785 o->challenge_response_authentication);
2786 dump_cfg_fmtint(sPrintMotd, o->print_motd);
Darren Tuckerfd4e4f22016-02-24 10:44:25 +11002787#ifndef DISABLE_LASTLOG
Darren Tuckere7140f22008-06-10 23:01:51 +10002788 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
Darren Tuckerfd4e4f22016-02-24 10:44:25 +11002789#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002790 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
2791 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002792 dump_cfg_fmtint(sPermitTTY, o->permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002793 dump_cfg_fmtint(sPermitUserRC, o->permit_user_rc);
Darren Tuckere7140f22008-06-10 23:01:51 +10002794 dump_cfg_fmtint(sStrictModes, o->strict_modes);
2795 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
2796 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
Darren Tuckere7140f22008-06-10 23:01:51 +10002797 dump_cfg_fmtint(sCompression, o->compression);
Damien Miller7acefbb2014-07-18 14:11:24 +10002798 dump_cfg_fmtint(sGatewayPorts, o->fwd_opts.gateway_ports);
Darren Tuckere7140f22008-06-10 23:01:51 +10002799 dump_cfg_fmtint(sUseDNS, o->use_dns);
2800 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002801 dump_cfg_fmtint(sAllowAgentForwarding, o->allow_agent_forwarding);
djm@openbsd.org7844f352016-11-30 03:00:05 +00002802 dump_cfg_fmtint(sDisableForwarding, o->disable_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002803 dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
djm@openbsd.org771c2f52016-05-03 15:25:06 +00002804 dump_cfg_fmtint(sStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002805 dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
djm@openbsd.org8f574952017-06-24 06:34:38 +00002806 dump_cfg_fmtint(sExposeAuthInfo, o->expose_userauth_info);
Darren Tuckere7140f22008-06-10 23:01:51 +10002807
2808 /* string arguments */
2809 dump_cfg_string(sPidFile, o->pid_file);
2810 dump_cfg_string(sXAuthLocation, o->xauth_location);
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002811 dump_cfg_string(sCiphers, o->ciphers);
2812 dump_cfg_string(sMacs, o->macs);
Darren Tuckere7140f22008-06-10 23:01:51 +10002813 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10002814 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11002815 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11002816 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
2817 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
djm@openbsd.org56584cc2019-12-15 18:57:30 +00002818 dump_cfg_string(sSecurityKeyProvider, o->sk_provider);
Damien Miller30da3442010-05-10 11:58:03 +10002819 dump_cfg_string(sAuthorizedPrincipalsFile,
2820 o->authorized_principals_file);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002821 dump_cfg_string(sVersionAddendum, *o->version_addendum == '\0'
2822 ? "none" : o->version_addendum);
Damien Miller09d3e122012-10-31 08:58:58 +11002823 dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
2824 dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +00002825 dump_cfg_string(sAuthorizedPrincipalsCommand, o->authorized_principals_command);
2826 dump_cfg_string(sAuthorizedPrincipalsCommandUser, o->authorized_principals_command_user);
Damien Miller85b45e02013-07-20 13:21:52 +10002827 dump_cfg_string(sHostKeyAgent, o->host_key_agent);
dtucker@openbsd.orgc4b3a122020-01-23 02:46:49 +00002828 dump_cfg_string(sKexAlgorithms, o->kex_algorithms);
2829 dump_cfg_string(sCASignatureAlgorithms, o->ca_sign_algorithms);
2830 dump_cfg_string(sHostbasedAcceptedKeyTypes, o->hostbased_key_types);
2831 dump_cfg_string(sHostKeyAlgorithms, o->hostkeyalgorithms);
2832 dump_cfg_string(sPubkeyAcceptedKeyTypes, o->pubkey_key_types);
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002833 dump_cfg_string(sRDomain, o->routing_domain);
Darren Tuckere7140f22008-06-10 23:01:51 +10002834
2835 /* string arguments requiring a lookup */
2836 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
2837 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
2838
2839 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10002840 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
2841 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002842 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
2843 o->host_key_files);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002844 dump_cfg_strarray(sHostCertificate, o->num_host_cert_files,
Damien Miller0a80ca12010-02-27 07:55:05 +11002845 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002846 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
2847 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
2848 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
2849 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
2850 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
djm@openbsd.org28013752018-06-09 03:03:10 +00002851 dump_cfg_strarray(sSetEnv, o->num_setenv, o->setenv);
Damien Millera6e3f012012-11-04 23:21:40 +11002852 dump_cfg_strarray_oneline(sAuthenticationMethods,
2853 o->num_auth_methods, o->auth_methods);
Darren Tuckere7140f22008-06-10 23:01:51 +10002854
2855 /* other arguments */
2856 for (i = 0; i < o->num_subsystems; i++)
2857 printf("subsystem %s %s\n", o->subsystem_name[i],
2858 o->subsystem_args[i]);
2859
2860 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
2861 o->max_startups_rate, o->max_startups);
2862
djm@openbsd.orgd685e5a2017-10-25 02:10:39 +00002863 s = NULL;
2864 for (i = 0; tunmode_desc[i].val != -1; i++) {
Darren Tuckere7140f22008-06-10 23:01:51 +10002865 if (tunmode_desc[i].val == o->permit_tun) {
2866 s = tunmode_desc[i].text;
2867 break;
2868 }
djm@openbsd.orgd685e5a2017-10-25 02:10:39 +00002869 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002870 dump_cfg_string(sPermitTunnel, s);
2871
Damien Miller91475862011-05-05 14:14:34 +10002872 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2873 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11002874
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002875 printf("rekeylimit %llu %d\n", (unsigned long long)o->rekey_limit,
Damien Millera6d6c1f2013-08-21 02:40:01 +10002876 o->rekey_interval);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002877
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002878 printf("permitopen");
2879 if (o->num_permitted_opens == 0)
2880 printf(" any");
2881 else {
2882 for (i = 0; i < o->num_permitted_opens; i++)
2883 printf(" %s", o->permitted_opens[i]);
2884 }
2885 printf("\n");
djm@openbsd.org93c06ab2018-06-06 18:23:32 +00002886 printf("permitlisten");
2887 if (o->num_permitted_listens == 0)
djm@openbsd.org115063a2018-06-06 18:22:41 +00002888 printf(" any");
2889 else {
djm@openbsd.org93c06ab2018-06-06 18:23:32 +00002890 for (i = 0; i < o->num_permitted_listens; i++)
2891 printf(" %s", o->permitted_listens[i]);
djm@openbsd.org115063a2018-06-06 18:22:41 +00002892 }
2893 printf("\n");
djm@openbsd.org95344c22018-07-03 10:59:35 +00002894
2895 if (o->permit_user_env_whitelist == NULL) {
2896 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
2897 } else {
2898 printf("permituserenvironment %s\n",
2899 o->permit_user_env_whitelist);
2900 }
2901
djm@openbsd.org0fddf292019-11-25 00:52:46 +00002902 printf("pubkeyauthoptions");
2903 if (o->pubkey_auth_options == 0)
2904 printf(" none");
2905 if (o->pubkey_auth_options & PUBKEYAUTH_TOUCH_REQUIRED)
2906 printf(" touch-required");
2907 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002908}