blob: 44de35367119f3d79ab1ad6af209d5b59f21b29b [file] [log] [blame]
djm@openbsd.org868109b2015-07-01 02:39:06 +00001
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00002/* $OpenBSD: servconf.c,v 1.319 2017/11/03 03:18:53 dtucker 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
Damien Millerbe43ebf2006-07-24 13:51:51 +100043
Damien Millerb84886b2008-05-19 15:05:07 +100044#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100045#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100048#include "buffer.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100049#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050#include "servconf.h"
Damien Miller78928792000-04-12 20:17:38 +100051#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100054#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000055#include "kex.h"
56#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100057#include "match.h"
Damien Miller9b439df2006-07-24 14:04:00 +100058#include "channels.h"
Damien Miller565ca3f2006-08-19 00:23:15 +100059#include "groupaccess.h"
Darren Tuckerfbcf8272012-05-19 19:37:01 +100060#include "canohost.h"
61#include "packet.h"
Damien Millera6e3f012012-11-04 23:21:40 +110062#include "hostfile.h"
63#include "auth.h"
djm@openbsd.org57d378e2014-08-19 23:58:28 +000064#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000065#include "digest.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000067static void add_listen_addr(ServerOptions *, const char *,
68 const char *, int);
69static void add_one_listen_addr(ServerOptions *, const char *,
70 const char *, int);
Damien Miller34132e52000-01-14 15:45:46 +110071
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000072/* Use of privilege separation or not */
73extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100074extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000075
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076/* Initializes the server options to their default values. */
77
Damien Miller4af51302000-04-16 11:18:38 +100078void
Damien Miller95def091999-11-25 00:26:21 +110079initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080{
Damien Miller95def091999-11-25 00:26:21 +110081 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110082
83 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100084 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110085
86 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110087 options->num_ports = 0;
88 options->ports_from_cmdline = 0;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +000089 options->queued_listen_addrs = NULL;
90 options->num_queued_listens = 0;
Damien Miller34132e52000-01-14 15:45:46 +110091 options->listen_addrs = NULL;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000092 options->num_listen_addrs = 0;
Darren Tucker0f383232005-01-20 10:57:56 +110093 options->address_family = -1;
djm@openbsd.org35eb33f2017-10-25 00:17:08 +000094 options->routing_domain = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +110095 options->num_host_key_files = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110096 options->num_host_cert_files = 0;
Damien Miller85b45e02013-07-20 13:21:52 +100097 options->host_key_agent = NULL;
Damien Miller6f83b8e2000-05-02 09:23:45 +100098 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110099 options->login_grace_time = -1;
chris@openbsd.org3d5728a2015-07-31 15:38:09 +0000100 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +1100101 options->ignore_rhosts = -1;
102 options->ignore_user_known_hosts = -1;
103 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000104 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +1100105 options->x11_forwarding = -1;
106 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +1100107 options->x11_use_localhost = -1;
Damien Miller5ff30c62013-10-30 22:21:50 +1100108 options->permit_tty = -1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000109 options->permit_user_rc = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000110 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100111 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +1100112 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +1100113 options->log_facility = SYSLOG_FACILITY_NOT_SET;
114 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000115 options->hostbased_authentication = -1;
116 options->hostbased_uses_name_from_packet_only = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000117 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000118 options->hostkeyalgorithms = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100119 options->pubkey_authentication = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000120 options->pubkey_key_types = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100121 options->kerberos_authentication = -1;
122 options->kerberos_or_local_passwd = -1;
123 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100124 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000125 options->gss_authentication=-1;
126 options->gss_cleanup_creds = -1;
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000127 options->gss_strict_acceptor = -1;
Damien Miller95def091999-11-25 00:26:21 +1100128 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100129 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000130 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100131 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000132 options->permit_user_env = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000133 options->compression = -1;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000134 options->rekey_limit = -1;
135 options->rekey_interval = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100136 options->allow_tcp_forwarding = -1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000137 options->allow_streamlocal_forwarding = -1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000138 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100139 options->num_allow_users = 0;
140 options->num_deny_users = 0;
141 options->num_allow_groups = 0;
142 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000143 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000144 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000145 options->kex_algorithms = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000146 options->fwd_opts.gateway_ports = -1;
147 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
148 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000149 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000150 options->max_startups_begin = -1;
151 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000152 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000153 options->max_authtries = -1;
Damien Miller7207f642008-05-19 15:34:50 +1000154 options->max_sessions = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000155 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000156 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000157 options->client_alive_interval = -1;
158 options->client_alive_count_max = -1;
Damien Millerd8478b62011-05-29 21:39:36 +1000159 options->num_authkeys_files = 0;
Darren Tucker46bc0752004-05-02 22:11:30 +1000160 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100161 options->permit_tun = -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000162 options->permitted_opens = NULL;
Damien Millere2754432006-07-24 14:06:47 +1000163 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100164 options->chroot_directory = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +1100165 options->authorized_keys_command = NULL;
166 options->authorized_keys_command_user = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100167 options->revoked_keys_file = NULL;
168 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000169 options->authorized_principals_file = NULL;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000170 options->authorized_principals_command = NULL;
171 options->authorized_principals_command_user = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100172 options->ip_qos_interactive = -1;
173 options->ip_qos_bulk = -1;
Damien Miller23528812012-04-22 11:24:43 +1000174 options->version_addendum = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000175 options->fingerprint_hash = -1;
djm@openbsd.org7844f352016-11-30 03:00:05 +0000176 options->disable_forwarding = -1;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000177 options->expose_userauth_info = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178}
179
djm@openbsd.org161cf412014-12-22 07:55:51 +0000180/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
181static int
182option_clear_or_none(const char *o)
183{
184 return o == NULL || strcasecmp(o, "none") == 0;
185}
186
djm@openbsd.orged085102015-10-29 08:05:01 +0000187static void
188assemble_algorithms(ServerOptions *o)
189{
190 if (kex_assemble_names(KEX_SERVER_ENCRYPT, &o->ciphers) != 0 ||
191 kex_assemble_names(KEX_SERVER_MAC, &o->macs) != 0 ||
192 kex_assemble_names(KEX_SERVER_KEX, &o->kex_algorithms) != 0 ||
193 kex_assemble_names(KEX_DEFAULT_PK_ALG,
194 &o->hostkeyalgorithms) != 0 ||
195 kex_assemble_names(KEX_DEFAULT_PK_ALG,
196 &o->hostbased_key_types) != 0 ||
197 kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->pubkey_key_types) != 0)
198 fatal("kex_assemble_names failed");
199}
200
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000201static void
202array_append(const char *file, const int line, const char *directive,
203 char ***array, u_int *lp, const char *s)
204{
205
206 if (*lp >= INT_MAX)
207 fatal("%s line %d: Too many %s entries", file, line, directive);
208
209 *array = xrecallocarray(*array, *lp, *lp + 1, sizeof(**array));
210 (*array)[*lp] = xstrdup(s);
211 (*lp)++;
212}
213
214void
215servconf_add_hostkey(const char *file, const int line,
216 ServerOptions *options, const char *path)
217{
218 char *apath = derelativise_path(path);
219
220 array_append(file, line, "HostKey",
221 &options->host_key_files, &options->num_host_key_files, apath);
222 free(apath);
223}
224
225void
226servconf_add_hostcert(const char *file, const int line,
227 ServerOptions *options, const char *path)
228{
229 char *apath = derelativise_path(path);
230
231 array_append(file, line, "HostCertificate",
232 &options->host_cert_files, &options->num_host_cert_files, apath);
233 free(apath);
234}
235
Damien Miller4af51302000-04-16 11:18:38 +1000236void
Damien Miller95def091999-11-25 00:26:21 +1100237fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238{
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000239 u_int i;
djm@openbsd.org161cf412014-12-22 07:55:51 +0000240
Damien Miller726273e2001-11-12 11:40:11 +1100241 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000242 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000243 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100244
245 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100246 if (options->num_host_key_files == 0) {
247 /* fill default hostkeys for protocols */
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000248 servconf_add_hostkey("[default]", 0, options,
249 _PATH_HOST_RSA_KEY_FILE);
250 servconf_add_hostkey("[default]", 0, options,
251 _PATH_HOST_DSA_KEY_FILE);
Damien Millerdd190dd2010-11-11 14:17:02 +1100252#ifdef OPENSSL_HAS_ECC
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000253 servconf_add_hostkey("[default]", 0, options,
254 _PATH_HOST_ECDSA_KEY_FILE);
Damien Millerdd190dd2010-11-11 14:17:02 +1100255#endif
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000256 servconf_add_hostkey("[default]", 0, options,
257 _PATH_HOST_ED25519_KEY_FILE);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100258 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100259 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100260 if (options->num_ports == 0)
261 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000262 if (options->address_family == -1)
263 options->address_family = AF_UNSPEC;
Damien Miller34132e52000-01-14 15:45:46 +1100264 if (options->listen_addrs == NULL)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000265 add_listen_addr(options, NULL, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000266 if (options->pid_file == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000267 options->pid_file = xstrdup(_PATH_SSH_DAEMON_PID_FILE);
Damien Miller95def091999-11-25 00:26:21 +1100268 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000269 options->login_grace_time = 120;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000270 if (options->permit_root_login == PERMIT_NOT_SET)
chris@openbsd.org3d5728a2015-07-31 15:38:09 +0000271 options->permit_root_login = PERMIT_NO_PASSWD;
Damien Miller95def091999-11-25 00:26:21 +1100272 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100273 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100274 if (options->ignore_user_known_hosts == -1)
275 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100276 if (options->print_motd == -1)
277 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000278 if (options->print_lastlog == -1)
279 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100280 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100281 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100282 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100283 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100284 if (options->x11_use_localhost == -1)
285 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000286 if (options->xauth_location == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000287 options->xauth_location = xstrdup(_PATH_XAUTH);
Damien Miller5ff30c62013-10-30 22:21:50 +1100288 if (options->permit_tty == -1)
289 options->permit_tty = 1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000290 if (options->permit_user_rc == -1)
291 options->permit_user_rc = 1;
Damien Miller95def091999-11-25 00:26:21 +1100292 if (options->strict_modes == -1)
293 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100294 if (options->tcp_keep_alive == -1)
295 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100296 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100297 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100298 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000299 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000300 if (options->hostbased_authentication == -1)
301 options->hostbased_authentication = 0;
302 if (options->hostbased_uses_name_from_packet_only == -1)
303 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100304 if (options->pubkey_authentication == -1)
305 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100306 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000307 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100308 if (options->kerberos_or_local_passwd == -1)
309 options->kerberos_or_local_passwd = 1;
310 if (options->kerberos_ticket_cleanup == -1)
311 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100312 if (options->kerberos_get_afs_token == -1)
313 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000314 if (options->gss_authentication == -1)
315 options->gss_authentication = 0;
316 if (options->gss_cleanup_creds == -1)
317 options->gss_cleanup_creds = 1;
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000318 if (options->gss_strict_acceptor == -1)
djm@openbsd.org13bd2e22017-01-06 03:45:41 +0000319 options->gss_strict_acceptor = 1;
Damien Miller95def091999-11-25 00:26:21 +1100320 if (options->password_authentication == -1)
321 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100322 if (options->kbd_interactive_authentication == -1)
323 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000324 if (options->challenge_response_authentication == -1)
325 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100326 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100327 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000328 if (options->permit_user_env == -1)
329 options->permit_user_env = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000330 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000331 options->compression = COMP_DELAYED;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000332 if (options->rekey_limit == -1)
333 options->rekey_limit = 0;
334 if (options->rekey_interval == -1)
335 options->rekey_interval = 0;
Damien Miller50a41ed2000-10-16 12:14:42 +1100336 if (options->allow_tcp_forwarding == -1)
Damien Milleraa5b3f82012-12-03 09:50:54 +1100337 options->allow_tcp_forwarding = FORWARD_ALLOW;
Damien Miller7acefbb2014-07-18 14:11:24 +1000338 if (options->allow_streamlocal_forwarding == -1)
339 options->allow_streamlocal_forwarding = FORWARD_ALLOW;
Damien Miller4f755cd2008-05-19 14:57:41 +1000340 if (options->allow_agent_forwarding == -1)
341 options->allow_agent_forwarding = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000342 if (options->fwd_opts.gateway_ports == -1)
343 options->fwd_opts.gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000344 if (options->max_startups == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100345 options->max_startups = 100;
Damien Miller942da032000-08-18 13:59:06 +1000346 if (options->max_startups_rate == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100347 options->max_startups_rate = 30; /* 30% */
Damien Miller942da032000-08-18 13:59:06 +1000348 if (options->max_startups_begin == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100349 options->max_startups_begin = 10;
Darren Tucker89413db2004-05-24 10:36:23 +1000350 if (options->max_authtries == -1)
351 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000352 if (options->max_sessions == -1)
353 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000354 if (options->use_dns == -1)
deraadt@openbsd.org3cd51032015-02-02 01:57:44 +0000355 options->use_dns = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000356 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100357 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000358 if (options->client_alive_count_max == -1)
359 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000360 if (options->num_authkeys_files == 0) {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000361 array_append("[default]", 0, "AuthorizedKeysFiles",
362 &options->authorized_keys_files,
363 &options->num_authkeys_files,
364 _PATH_SSH_USER_PERMITTED_KEYS);
365 array_append("[default]", 0, "AuthorizedKeysFiles",
366 &options->authorized_keys_files,
367 &options->num_authkeys_files,
368 _PATH_SSH_USER_PERMITTED_KEYS2);
Damien Millerd8478b62011-05-29 21:39:36 +1000369 }
Damien Millerd27b9472005-12-13 19:29:02 +1100370 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100371 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100372 if (options->ip_qos_interactive == -1)
373 options->ip_qos_interactive = IPTOS_LOWDELAY;
374 if (options->ip_qos_bulk == -1)
375 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller23528812012-04-22 11:24:43 +1000376 if (options->version_addendum == NULL)
377 options->version_addendum = xstrdup("");
Damien Miller7acefbb2014-07-18 14:11:24 +1000378 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
379 options->fwd_opts.streamlocal_bind_mask = 0177;
380 if (options->fwd_opts.streamlocal_bind_unlink == -1)
381 options->fwd_opts.streamlocal_bind_unlink = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000382 if (options->fingerprint_hash == -1)
383 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org7844f352016-11-30 03:00:05 +0000384 if (options->disable_forwarding == -1)
385 options->disable_forwarding = 0;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000386 if (options->expose_userauth_info == -1)
387 options->expose_userauth_info = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000388
djm@openbsd.orged085102015-10-29 08:05:01 +0000389 assemble_algorithms(options);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000390
djm@openbsd.orgc5c3f322016-02-17 05:29:04 +0000391 /* Turn privilege separation and sandboxing on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000392 if (use_privsep == -1)
djm@openbsd.orgc5c3f322016-02-17 05:29:04 +0000393 use_privsep = PRIVSEP_ON;
Damien Miller4903eb42002-06-21 16:20:44 +1000394
djm@openbsd.org161cf412014-12-22 07:55:51 +0000395#define CLEAR_ON_NONE(v) \
396 do { \
397 if (option_clear_or_none(v)) { \
398 free(v); \
399 v = NULL; \
400 } \
401 } while(0)
402 CLEAR_ON_NONE(options->pid_file);
403 CLEAR_ON_NONE(options->xauth_location);
404 CLEAR_ON_NONE(options->banner);
405 CLEAR_ON_NONE(options->trusted_user_ca_keys);
406 CLEAR_ON_NONE(options->revoked_keys_file);
djm@openbsd.org7e8528c2015-05-01 04:17:51 +0000407 CLEAR_ON_NONE(options->authorized_principals_file);
djm@openbsd.org9fd04682015-11-13 04:38:06 +0000408 CLEAR_ON_NONE(options->adm_forced_command);
409 CLEAR_ON_NONE(options->chroot_directory);
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000410 CLEAR_ON_NONE(options->routing_domain);
djm@openbsd.org161cf412014-12-22 07:55:51 +0000411 for (i = 0; i < options->num_host_key_files; i++)
412 CLEAR_ON_NONE(options->host_key_files[i]);
413 for (i = 0; i < options->num_host_cert_files; i++)
414 CLEAR_ON_NONE(options->host_cert_files[i]);
415#undef CLEAR_ON_NONE
416
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +0000417 /* Similar handling for AuthenticationMethods=any */
418 if (options->num_auth_methods == 1 &&
419 strcmp(options->auth_methods[0], "any") == 0) {
420 free(options->auth_methods[0]);
421 options->auth_methods[0] = NULL;
422 options->num_auth_methods = 0;
423 }
424
Tim Rice40017b02002-07-14 13:36:49 -0700425#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000426 if (use_privsep && options->compression == 1) {
427 error("This platform does not support both privilege "
428 "separation and compression");
429 error("Compression disabled");
430 options->compression = 0;
431 }
432#endif
433
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434}
435
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100437typedef enum {
438 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100439 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000440 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100441 /* Standard Options */
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000442 sPort, sHostKeyFile, sLoginGraceTime,
443 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000444 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100445 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100446 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000447 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100448 sPasswordAuthentication, sKbdInteractiveAuthentication,
449 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000450 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100451 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller5ff30c62013-10-30 22:21:50 +1100452 sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
djm@openbsd.org83b58182016-08-19 03:18:06 +0000453 sPermitUserEnvironment, sAllowTcpForwarding, sCompression,
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000454 sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000455 sIgnoreUserKnownHosts, sCiphers, sMacs, sPidFile,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000456 sGatewayPorts, sPubkeyAuthentication, sPubkeyAcceptedKeyTypes,
457 sXAuthLocation, sSubsystem, sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000458 sBanner, sUseDNS, sHostbasedAuthentication,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000459 sHostbasedUsesNameFromPacketOnly, sHostbasedAcceptedKeyTypes,
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000460 sHostKeyAlgorithms,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000461 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000462 sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
463 sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100464 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100465 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller7cc194f2014-02-04 11:12:56 +1100466 sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000467 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000468 sAuthorizedPrincipalsCommand, sAuthorizedPrincipalsCommandUser,
Damien Miller23528812012-04-22 11:24:43 +1000469 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Miller09d3e122012-10-31 08:58:58 +1100470 sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
Damien Miller72e6b5c2014-07-04 09:00:04 +1000471 sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
Damien Miller7acefbb2014-07-18 14:11:24 +1000472 sStreamLocalBindMask, sStreamLocalBindUnlink,
djm@openbsd.org7844f352016-11-30 03:00:05 +0000473 sAllowStreamLocalForwarding, sFingerprintHash, sDisableForwarding,
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000474 sExposeAuthInfo, sRDomain,
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000475 sDeprecated, sIgnore, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476} ServerOpCodes;
477
Darren Tucker45150472006-07-12 22:34:17 +1000478#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
479#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
480#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
481
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100483static struct {
484 const char *name;
485 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000486 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100487} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100488 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000489#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000490 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000491#else
Darren Tucker45150472006-07-12 22:34:17 +1000492 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000493#endif
Darren Tucker45150472006-07-12 22:34:17 +1000494 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100495 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000496 { "port", sPort, SSHCFG_GLOBAL },
497 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
498 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
Damien Miller85b45e02013-07-20 13:21:52 +1000499 { "hostkeyagent", sHostKeyAgent, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000500 { "pidfile", sPidFile, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000501 { "serverkeybits", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000502 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000503 { "keyregenerationinterval", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100504 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000505 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
djm@openbsd.org54cd41a2017-05-17 01:24:17 +0000506 { "loglevel", sLogLevel, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000507 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000508 { "rhostsrsaauthentication", sDeprecated, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100509 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000510 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000511 { "hostbasedacceptedkeytypes", sHostbasedAcceptedKeyTypes, SSHCFG_ALL },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000512 { "hostkeyalgorithms", sHostKeyAlgorithms, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000513 { "rsaauthentication", sDeprecated, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100514 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000515 { "pubkeyacceptedkeytypes", sPubkeyAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000516 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000517#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100518 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000519 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
520 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100521#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000522 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000523#else
Darren Tucker45150472006-07-12 22:34:17 +1000524 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100525#endif
526#else
Darren Tucker1629c072007-02-19 22:25:37 +1100527 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000528 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
529 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
530 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000531#endif
Darren Tucker45150472006-07-12 22:34:17 +1000532 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
533 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000534#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100535 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000536 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000537 { "gssapistrictacceptorcheck", sGssStrictAcceptor, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000538#else
Darren Tucker1629c072007-02-19 22:25:37 +1100539 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000540 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000541 { "gssapistrictacceptorcheck", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000542#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100543 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
544 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100545 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000546 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
547 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
548 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
549 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
550 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
Damien Millerac908c12015-10-22 09:35:24 +1100551#ifdef DISABLE_LASTLOG
552 { "printlastlog", sUnsupported, SSHCFG_GLOBAL },
553#else
Darren Tucker45150472006-07-12 22:34:17 +1000554 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
Damien Millerac908c12015-10-22 09:35:24 +1100555#endif
Darren Tucker45150472006-07-12 22:34:17 +1000556 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
557 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000558 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
559 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
560 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000561 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
562 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100563 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000564 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
djm@openbsd.org83b58182016-08-19 03:18:06 +0000565 { "uselogin", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000566 { "compression", sCompression, SSHCFG_GLOBAL },
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000567 { "rekeylimit", sRekeyLimit, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000568 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
569 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
570 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000571 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Damien Millerc24da772012-06-20 21:53:58 +1000572 { "allowusers", sAllowUsers, SSHCFG_ALL },
573 { "denyusers", sDenyUsers, SSHCFG_ALL },
574 { "allowgroups", sAllowGroups, SSHCFG_ALL },
575 { "denygroups", sDenyGroups, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000576 { "ciphers", sCiphers, SSHCFG_GLOBAL },
577 { "macs", sMacs, SSHCFG_GLOBAL },
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000578 { "protocol", sIgnore, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000579 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
580 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
581 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000582 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000583 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100584 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000585 { "usedns", sUseDNS, SSHCFG_GLOBAL },
586 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
587 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
markus@openbsd.orgf0ddede2016-11-23 23:14:15 +0000588 { "clientaliveinterval", sClientAliveInterval, SSHCFG_ALL },
589 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000590 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000591 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
djm@openbsd.org66705942017-03-14 07:19:07 +0000592 { "useprivilegeseparation", sDeprecated, SSHCFG_GLOBAL},
Damien Millerc24da772012-06-20 21:53:58 +1000593 { "acceptenv", sAcceptEnv, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000594 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Damien Miller5ff30c62013-10-30 22:21:50 +1100595 { "permittty", sPermitTTY, SSHCFG_ALL },
Damien Miller72e6b5c2014-07-04 09:00:04 +1000596 { "permituserrc", sPermitUserRC, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000597 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000598 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000599 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100600 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100601 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100602 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
603 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000604 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000605 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100606 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller09d3e122012-10-31 08:58:58 +1100607 { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
608 { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000609 { "authorizedprincipalscommand", sAuthorizedPrincipalsCommand, SSHCFG_ALL },
610 { "authorizedprincipalscommanduser", sAuthorizedPrincipalsCommandUser, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000611 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Damien Millera6e3f012012-11-04 23:21:40 +1100612 { "authenticationmethods", sAuthenticationMethods, SSHCFG_ALL },
Damien Miller7acefbb2014-07-18 14:11:24 +1000613 { "streamlocalbindmask", sStreamLocalBindMask, SSHCFG_ALL },
614 { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
615 { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000616 { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
djm@openbsd.org7844f352016-11-30 03:00:05 +0000617 { "disableforwarding", sDisableForwarding, SSHCFG_ALL },
djm@openbsd.org8f574952017-06-24 06:34:38 +0000618 { "exposeauthinfo", sExposeAuthInfo, SSHCFG_ALL },
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000619 { "rdomain", sRDomain, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000620 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000621};
622
Darren Tuckere7140f22008-06-10 23:01:51 +1000623static struct {
624 int val;
625 char *text;
626} tunmode_desc[] = {
627 { SSH_TUNMODE_NO, "no" },
628 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
629 { SSH_TUNMODE_ETHERNET, "ethernet" },
630 { SSH_TUNMODE_YES, "yes" },
631 { -1, NULL }
632};
633
Damien Miller5428f641999-11-25 11:54:57 +1100634/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000635 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100636 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000637
Damien Miller4af51302000-04-16 11:18:38 +1000638static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100639parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000640 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000641{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000642 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000643
Damien Miller95def091999-11-25 00:26:21 +1100644 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000645 if (strcasecmp(cp, keywords[i].name) == 0) {
646 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100647 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000648 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000649
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000650 error("%s: line %d: Bad configuration option: %s",
651 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100652 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000653}
654
Darren Tucker88b6fb22010-01-13 22:44:29 +1100655char *
656derelativise_path(const char *path)
657{
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000658 char *expanded, *ret, cwd[PATH_MAX];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100659
djm@openbsd.org161cf412014-12-22 07:55:51 +0000660 if (strcasecmp(path, "none") == 0)
661 return xstrdup("none");
Darren Tucker88b6fb22010-01-13 22:44:29 +1100662 expanded = tilde_expand_filename(path, getuid());
663 if (*expanded == '/')
664 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100665 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100666 fatal("%s: getcwd: %s", __func__, strerror(errno));
667 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tuckera627d422013-06-02 07:31:17 +1000668 free(expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100669 return ret;
670}
671
Ben Lindstrombba81212001-06-25 05:01:22 +0000672static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000673add_listen_addr(ServerOptions *options, const char *addr,
674 const char *rdomain, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100675{
Damien Millereccb9de2005-06-17 12:59:34 +1000676 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100677
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000678 if (port > 0)
679 add_one_listen_addr(options, addr, rdomain, port);
680 else {
681 for (i = 0; i < options->num_ports; i++) {
682 add_one_listen_addr(options, addr, rdomain,
683 options->ports[i]);
684 }
685 }
Ben Lindstromc510af42001-04-07 17:25:48 +0000686}
687
Ben Lindstrombba81212001-06-25 05:01:22 +0000688static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000689add_one_listen_addr(ServerOptions *options, const char *addr,
690 const char *rdomain, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000691{
692 struct addrinfo hints, *ai, *aitop;
693 char strport[NI_MAXSERV];
694 int gaierr;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000695 u_int i;
696
697 /* Find listen_addrs entry for this rdomain */
698 for (i = 0; i < options->num_listen_addrs; i++) {
699 if (rdomain == NULL && options->listen_addrs[i].rdomain == NULL)
700 break;
701 if (rdomain == NULL || options->listen_addrs[i].rdomain == NULL)
702 continue;
703 if (strcmp(rdomain, options->listen_addrs[i].rdomain) == 0)
704 break;
705 }
706 if (i >= options->num_listen_addrs) {
707 /* No entry for this rdomain; allocate one */
708 if (i >= INT_MAX)
709 fatal("%s: too many listen addresses", __func__);
710 options->listen_addrs = xrecallocarray(options->listen_addrs,
711 options->num_listen_addrs, options->num_listen_addrs + 1,
712 sizeof(*options->listen_addrs));
713 i = options->num_listen_addrs++;
714 if (rdomain != NULL)
715 options->listen_addrs[i].rdomain = xstrdup(rdomain);
716 }
717 /* options->listen_addrs[i] points to the addresses for this rdomain */
Ben Lindstromc510af42001-04-07 17:25:48 +0000718
719 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100720 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000721 hints.ai_socktype = SOCK_STREAM;
722 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100723 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000724 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
725 fatal("bad addr or host: %s (%s)",
726 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100727 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000728 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
729 ;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000730 ai->ai_next = options->listen_addrs[i].addrs;
731 options->listen_addrs[i].addrs = aitop;
732}
733
734/* Returns nonzero if the routing domain name is valid */
735static int
736valid_rdomain(const char *name)
737{
Damien Miller43c29bb2017-10-25 13:10:59 +1100738#if defined(HAVE_SYS_VALID_RDOMAIN)
Damien Miller2de5c6b2017-10-27 08:42:33 +1100739 return sys_valid_rdomain(name);
Damien Miller43c29bb2017-10-25 13:10:59 +1100740#elif defined(__OpenBSD__)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000741 const char *errstr;
742 long long num;
743 struct rt_tableinfo info;
744 int mib[6];
745 size_t miblen = sizeof(mib);
746
747 if (name == NULL)
748 return 1;
749
750 num = strtonum(name, 0, 255, &errstr);
751 if (errstr != NULL)
752 return 0;
753
754 /* Check whether the table actually exists */
755 memset(mib, 0, sizeof(mib));
756 mib[0] = CTL_NET;
757 mib[1] = PF_ROUTE;
758 mib[4] = NET_RT_TABLE;
759 mib[5] = (int)num;
760 if (sysctl(mib, 6, &info, &miblen, NULL, 0) == -1)
761 return 0;
762
763 return 1;
Damien Miller43c29bb2017-10-25 13:10:59 +1100764#else /* defined(__OpenBSD__) */
765 error("Routing domains are not supported on this platform");
766 return 0;
767#endif
Damien Miller34132e52000-01-14 15:45:46 +1100768}
769
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000770/*
771 * Queue a ListenAddress to be processed once we have all of the Ports
772 * and AddressFamily options.
773 */
774static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000775queue_listen_addr(ServerOptions *options, const char *addr,
776 const char *rdomain, int port)
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000777{
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000778 struct queued_listenaddr *qla;
779
780 options->queued_listen_addrs = xrecallocarray(
781 options->queued_listen_addrs,
782 options->num_queued_listens, options->num_queued_listens + 1,
783 sizeof(*options->queued_listen_addrs));
784 qla = &options->queued_listen_addrs[options->num_queued_listens++];
785 qla->addr = xstrdup(addr);
786 qla->port = port;
787 qla->rdomain = rdomain == NULL ? NULL : xstrdup(rdomain);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000788}
789
790/*
791 * Process queued (text) ListenAddress entries.
792 */
793static void
794process_queued_listen_addrs(ServerOptions *options)
795{
796 u_int i;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000797 struct queued_listenaddr *qla;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000798
799 if (options->num_ports == 0)
800 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
801 if (options->address_family == -1)
802 options->address_family = AF_UNSPEC;
803
804 for (i = 0; i < options->num_queued_listens; i++) {
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000805 qla = &options->queued_listen_addrs[i];
806 add_listen_addr(options, qla->addr, qla->rdomain, qla->port);
807 free(qla->addr);
808 free(qla->rdomain);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000809 }
810 free(options->queued_listen_addrs);
811 options->queued_listen_addrs = NULL;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000812 options->num_queued_listens = 0;
813}
814
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000815/*
816 * Inform channels layer of permitopen options from configuration.
817 */
818void
819process_permitopen(struct ssh *ssh, ServerOptions *options)
820{
821 u_int i;
822 int port;
823 char *host, *arg, *oarg;
824
825 channel_clear_adm_permitted_opens(ssh);
826 if (options->num_permitted_opens == 0)
827 return; /* permit any */
828
829 /* handle keywords: "any" / "none" */
830 if (options->num_permitted_opens == 1 &&
831 strcmp(options->permitted_opens[0], "any") == 0)
832 return;
833 if (options->num_permitted_opens == 1 &&
834 strcmp(options->permitted_opens[0], "none") == 0) {
835 channel_disable_adm_local_opens(ssh);
836 return;
837 }
838 /* Otherwise treat it as a list of permitted host:port */
839 for (i = 0; i < options->num_permitted_opens; i++) {
840 oarg = arg = xstrdup(options->permitted_opens[i]);
841 host = hpdelim(&arg);
842 if (host == NULL)
843 fatal("%s: missing host in PermitOpen", __func__);
844 host = cleanhostname(host);
845 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
846 fatal("%s: bad port number in PermitOpen", __func__);
847 /* Send it to channels layer */
848 channel_add_adm_permitted_opens(ssh, host, port);
849 free(oarg);
850 }
851}
852
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000853struct connection_info *
854get_connection_info(int populate, int use_dns)
855{
djm@openbsd.org95767262016-03-07 19:02:43 +0000856 struct ssh *ssh = active_state; /* XXX */
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000857 static struct connection_info ci;
858
859 if (!populate)
860 return &ci;
djm@openbsd.org95767262016-03-07 19:02:43 +0000861 ci.host = auth_get_canonical_hostname(ssh, use_dns);
862 ci.address = ssh_remote_ipaddr(ssh);
863 ci.laddress = ssh_local_ipaddr(ssh);
864 ci.lport = ssh_local_port(ssh);
djm@openbsd.org68af80e2017-10-25 00:19:47 +0000865 ci.rdomain = ssh_packet_rdomain_in(ssh);
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000866 return &ci;
867}
868
Darren Tucker45150472006-07-12 22:34:17 +1000869/*
870 * The strategy for the Match blocks is that the config file is parsed twice.
871 *
872 * The first time is at startup. activep is initialized to 1 and the
873 * directives in the global context are processed and acted on. Hitting a
874 * Match directive unsets activep and the directives inside the block are
875 * checked for syntax only.
876 *
877 * The second time is after a connection has been established but before
878 * authentication. activep is initialized to 2 and global config directives
879 * are ignored since they have already been processed. If the criteria in a
880 * Match block is met, activep is set and the subsequent directives
881 * processed and actioned until EOF or another Match block unsets it. Any
882 * options set are copied into the main server config.
883 *
884 * Potential additions/improvements:
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000885 * - Add Match support for pre-kex directives, eg. Ciphers.
Darren Tucker45150472006-07-12 22:34:17 +1000886 *
887 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
888 * Match Address 192.168.0.*
889 * Tag trusted
890 * Match Group wheel
891 * Tag trusted
892 * Match Tag trusted
893 * AllowTcpForwarding yes
894 * GatewayPorts clientspecified
895 * [...]
896 *
897 * - Add a PermittedChannelRequests directive
898 * Match Group shell
899 * PermittedChannelRequests session,forwarded-tcpip
900 */
901
902static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000903match_cfg_line_group(const char *grps, int line, const char *user)
904{
905 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000906 struct passwd *pw;
907
Damien Miller565ca3f2006-08-19 00:23:15 +1000908 if (user == NULL)
909 goto out;
910
911 if ((pw = getpwnam(user)) == NULL) {
912 debug("Can't match group at line %d because user %.100s does "
913 "not exist", line, user);
914 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
915 debug("Can't Match group because user %.100s not in any group "
916 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000917 } else if (ga_match_pattern_list(grps) != 1) {
918 debug("user %.100s does not match group list %.100s at line %d",
919 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000920 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000921 debug("user %.100s matched group list %.100s at line %d", user,
922 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000923 result = 1;
924 }
925out:
926 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000927 return result;
928}
929
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000930static void
931match_test_missing_fatal(const char *criteria, const char *attrib)
932{
933 fatal("'Match %s' in configuration but '%s' not in connection "
934 "test specification.", criteria, attrib);
935}
936
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000937/*
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000938 * All of the attributes on a single Match line are ANDed together, so we need
Damien Miller03bf2e62013-10-24 21:01:26 +1100939 * to check every attribute and set the result to zero if any attribute does
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000940 * not match.
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000941 */
Damien Miller565ca3f2006-08-19 00:23:15 +1000942static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000943match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +1000944{
Damien Millercf31f382013-10-24 21:02:56 +1100945 int result = 1, attributes = 0, port;
Darren Tucker45150472006-07-12 22:34:17 +1000946 char *arg, *attrib, *cp = *condition;
Darren Tucker45150472006-07-12 22:34:17 +1000947
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000948 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000949 debug3("checking syntax for 'Match %s'", cp);
950 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000951 debug3("checking match for '%s' user %s host %s addr %s "
952 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
953 ci->host ? ci->host : "(null)",
954 ci->address ? ci->address : "(null)",
955 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +1000956
957 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100958 attributes++;
959 if (strcasecmp(attrib, "all") == 0) {
960 if (attributes != 1 ||
961 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
962 error("'all' cannot be combined with other "
963 "Match attributes");
964 return -1;
965 }
966 *condition = cp;
967 return 1;
968 }
Darren Tucker45150472006-07-12 22:34:17 +1000969 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
970 error("Missing Match criteria for %s", attrib);
971 return -1;
972 }
Darren Tucker45150472006-07-12 22:34:17 +1000973 if (strcasecmp(attrib, "user") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000974 if (ci == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000975 result = 0;
976 continue;
977 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000978 if (ci->user == NULL)
979 match_test_missing_fatal("User", "user");
djm@openbsd.orge661a862015-05-04 06:10:48 +0000980 if (match_pattern_list(ci->user, arg, 0) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000981 result = 0;
982 else
983 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000984 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000985 } else if (strcasecmp(attrib, "group") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000986 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000987 result = 0;
988 continue;
989 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000990 if (ci->user == NULL)
991 match_test_missing_fatal("Group", "user");
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000992 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +1000993 case -1:
994 return -1;
995 case 0:
996 result = 0;
997 }
Darren Tucker45150472006-07-12 22:34:17 +1000998 } else if (strcasecmp(attrib, "host") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000999 if (ci == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +10001000 result = 0;
1001 continue;
1002 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001003 if (ci->host == NULL)
1004 match_test_missing_fatal("Host", "host");
djm@openbsd.orge661a862015-05-04 06:10:48 +00001005 if (match_hostname(ci->host, arg) != 1)
Darren Tucker45150472006-07-12 22:34:17 +10001006 result = 0;
1007 else
1008 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001009 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +10001010 } else if (strcasecmp(attrib, "address") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001011 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001012 result = 0;
1013 continue;
1014 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001015 if (ci->address == NULL)
1016 match_test_missing_fatal("Address", "addr");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001017 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +10001018 case 1:
Darren Tucker45150472006-07-12 22:34:17 +10001019 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001020 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +10001021 break;
1022 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +10001023 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +10001024 result = 0;
1025 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +10001026 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +10001027 return -1;
1028 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001029 } else if (strcasecmp(attrib, "localaddress") == 0){
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001030 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001031 result = 0;
1032 continue;
1033 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001034 if (ci->laddress == NULL)
1035 match_test_missing_fatal("LocalAddress",
1036 "laddr");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001037 switch (addr_match_list(ci->laddress, arg)) {
1038 case 1:
1039 debug("connection from %.100s matched "
1040 "'LocalAddress %.100s' at line %d",
1041 ci->laddress, arg, line);
1042 break;
1043 case 0:
1044 case -1:
1045 result = 0;
1046 break;
1047 case -2:
1048 return -1;
1049 }
1050 } else if (strcasecmp(attrib, "localport") == 0) {
1051 if ((port = a2port(arg)) == -1) {
1052 error("Invalid LocalPort '%s' on Match line",
1053 arg);
1054 return -1;
1055 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001056 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001057 result = 0;
1058 continue;
1059 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001060 if (ci->lport == 0)
1061 match_test_missing_fatal("LocalPort", "lport");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001062 /* TODO support port lists */
1063 if (port == ci->lport)
1064 debug("connection from %.100s matched "
1065 "'LocalPort %d' at line %d",
1066 ci->laddress, port, line);
1067 else
1068 result = 0;
djm@openbsd.org68af80e2017-10-25 00:19:47 +00001069 } else if (strcasecmp(attrib, "rdomain") == 0) {
1070 if (ci == NULL || ci->rdomain == NULL) {
1071 result = 0;
1072 continue;
1073 }
1074 if (match_pattern_list(ci->rdomain, arg, 0) != 1)
1075 result = 0;
1076 else
1077 debug("user %.100s matched 'RDomain %.100s' at "
1078 "line %d", ci->rdomain, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +10001079 } else {
1080 error("Unsupported Match attribute %s", attrib);
1081 return -1;
1082 }
1083 }
Damien Millercf31f382013-10-24 21:02:56 +11001084 if (attributes == 0) {
1085 error("One or more attributes required for Match");
1086 return -1;
1087 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001088 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +10001089 debug3("match %sfound", result ? "" : "not ");
1090 *condition = cp;
1091 return result;
1092}
1093
Damien Millere2754432006-07-24 14:06:47 +10001094#define WHITESPACE " \t\r\n"
1095
Damien Miller33322122011-06-20 14:43:11 +10001096/* Multistate option parsing */
1097struct multistate {
1098 char *key;
1099 int value;
1100};
1101static const struct multistate multistate_addressfamily[] = {
1102 { "inet", AF_INET },
1103 { "inet6", AF_INET6 },
1104 { "any", AF_UNSPEC },
1105 { NULL, -1 }
1106};
1107static const struct multistate multistate_permitrootlogin[] = {
1108 { "without-password", PERMIT_NO_PASSWD },
deraadt@openbsd.org1dc8d932015-08-06 14:53:21 +00001109 { "prohibit-password", PERMIT_NO_PASSWD },
Damien Miller33322122011-06-20 14:43:11 +10001110 { "forced-commands-only", PERMIT_FORCED_ONLY },
1111 { "yes", PERMIT_YES },
1112 { "no", PERMIT_NO },
1113 { NULL, -1 }
1114};
1115static const struct multistate multistate_compression[] = {
djm@openbsd.org0082fba2016-09-28 16:33:06 +00001116 { "yes", COMP_DELAYED },
djm@openbsd.org4577ade2016-09-28 20:32:42 +00001117 { "delayed", COMP_DELAYED },
Damien Miller33322122011-06-20 14:43:11 +10001118 { "no", COMP_NONE },
1119 { NULL, -1 }
1120};
1121static const struct multistate multistate_gatewayports[] = {
1122 { "clientspecified", 2 },
1123 { "yes", 1 },
1124 { "no", 0 },
1125 { NULL, -1 }
1126};
Damien Milleraa5b3f82012-12-03 09:50:54 +11001127static const struct multistate multistate_tcpfwd[] = {
1128 { "yes", FORWARD_ALLOW },
1129 { "all", FORWARD_ALLOW },
1130 { "no", FORWARD_DENY },
1131 { "remote", FORWARD_REMOTE },
1132 { "local", FORWARD_LOCAL },
1133 { NULL, -1 }
1134};
Damien Miller33322122011-06-20 14:43:11 +10001135
Ben Lindstromade03f62001-12-06 18:22:17 +00001136int
1137process_server_config_line(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001138 const char *filename, int linenum, int *activep,
1139 struct connection_info *connectinfo)
Ben Lindstromade03f62001-12-06 18:22:17 +00001140{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001141 char *cp, **charptr, *arg, *arg2, *p;
Darren Tucker9113d0c2013-05-16 20:48:14 +10001142 int cmdline = 0, *intptr, value, value2, n, port;
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001143 SyslogFacility *log_facility_ptr;
1144 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +00001145 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +10001146 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +10001147 size_t len;
Darren Tucker9113d0c2013-05-16 20:48:14 +10001148 long long val64;
Damien Miller33322122011-06-20 14:43:11 +10001149 const struct multistate *multistate_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +00001150
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +00001151 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
1152 if ((len = strlen(line)) == 0)
1153 return 0;
1154 for (len--; len > 0; len--) {
1155 if (strchr(WHITESPACE "\f", line[len]) == NULL)
1156 break;
1157 line[len] = '\0';
1158 }
1159
Ben Lindstromade03f62001-12-06 18:22:17 +00001160 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +11001161 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +11001162 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +00001163 /* Ignore leading whitespace */
1164 if (*arg == '\0')
1165 arg = strdelim(&cp);
1166 if (!arg || !*arg || *arg == '#')
1167 return 0;
1168 intptr = NULL;
1169 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +10001170 opcode = parse_token(arg, filename, linenum, &flags);
1171
1172 if (activep == NULL) { /* We are processing a command line directive */
1173 cmdline = 1;
1174 activep = &cmdline;
1175 }
1176 if (*activep && opcode != sMatch)
1177 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
1178 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001179 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +10001180 fatal("%s line %d: Directive '%s' is not allowed "
1181 "within a Match block", filename, linenum, arg);
1182 } else { /* this is a directive we have already processed */
1183 while (arg)
1184 arg = strdelim(&cp);
1185 return 0;
1186 }
1187 }
1188
Ben Lindstromade03f62001-12-06 18:22:17 +00001189 switch (opcode) {
1190 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +10001191 case sUsePAM:
1192 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +00001193 goto parse_flag;
1194
1195 /* Standard Options */
1196 case sBadOption:
1197 return -1;
1198 case sPort:
1199 /* ignore ports from configfile if cmdline specifies ports */
1200 if (options->ports_from_cmdline)
1201 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +00001202 if (options->num_ports >= MAX_PORTS)
1203 fatal("%s line %d: too many ports.",
1204 filename, linenum);
1205 arg = strdelim(&cp);
1206 if (!arg || *arg == '\0')
1207 fatal("%s line %d: missing port number.",
1208 filename, linenum);
1209 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +11001210 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +00001211 fatal("%s line %d: Badly formatted port number.",
1212 filename, linenum);
1213 break;
1214
Ben Lindstromade03f62001-12-06 18:22:17 +00001215 case sLoginGraceTime:
1216 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +10001217 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +00001218 arg = strdelim(&cp);
1219 if (!arg || *arg == '\0')
1220 fatal("%s line %d: missing time value.",
1221 filename, linenum);
1222 if ((value = convtime(arg)) == -1)
1223 fatal("%s line %d: invalid time value.",
1224 filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001225 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001226 *intptr = value;
1227 break;
1228
Ben Lindstromade03f62001-12-06 18:22:17 +00001229 case sListenAddress:
1230 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001231 if (arg == NULL || *arg == '\0')
1232 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +00001233 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +10001234 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
1235 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
1236 && strchr(p+1, ':') != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001237 port = 0;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001238 p = arg;
1239 } else {
1240 p = hpdelim(&arg);
1241 if (p == NULL)
1242 fatal("%s line %d: bad address:port usage",
1243 filename, linenum);
1244 p = cleanhostname(p);
1245 if (arg == NULL)
1246 port = 0;
1247 else if ((port = a2port(arg)) <= 0)
1248 fatal("%s line %d: bad port number",
1249 filename, linenum);
1250 }
1251 /* Optional routing table */
1252 arg2 = NULL;
1253 if ((arg = strdelim(&cp)) != NULL) {
1254 if (strcmp(arg, "rdomain") != 0 ||
1255 (arg2 = strdelim(&cp)) == NULL)
1256 fatal("%s line %d: bad ListenAddress syntax",
1257 filename, linenum);
1258 if (!valid_rdomain(arg2))
1259 fatal("%s line %d: bad routing domain",
1260 filename, linenum);
1261 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001262
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001263 queue_listen_addr(options, p, arg2, port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001264
Ben Lindstromade03f62001-12-06 18:22:17 +00001265 break;
1266
Darren Tucker0f383232005-01-20 10:57:56 +11001267 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +10001268 intptr = &options->address_family;
1269 multistate_ptr = multistate_addressfamily;
Damien Miller33322122011-06-20 14:43:11 +10001270 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +11001271 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +10001272 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +10001273 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +10001274 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +10001275 value = -1;
1276 for (i = 0; multistate_ptr[i].key != NULL; i++) {
1277 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
1278 value = multistate_ptr[i].value;
1279 break;
1280 }
1281 }
1282 if (value == -1)
1283 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +11001284 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +10001285 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +11001286 *intptr = value;
1287 break;
1288
Ben Lindstromade03f62001-12-06 18:22:17 +00001289 case sHostKeyFile:
Ben Lindstromade03f62001-12-06 18:22:17 +00001290 arg = strdelim(&cp);
1291 if (!arg || *arg == '\0')
1292 fatal("%s line %d: missing file name.",
1293 filename, linenum);
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001294 if (*activep)
1295 servconf_add_hostkey(filename, linenum, options, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001296 break;
1297
Damien Miller85b45e02013-07-20 13:21:52 +10001298 case sHostKeyAgent:
1299 charptr = &options->host_key_agent;
1300 arg = strdelim(&cp);
1301 if (!arg || *arg == '\0')
1302 fatal("%s line %d: missing socket name.",
1303 filename, linenum);
1304 if (*activep && *charptr == NULL)
1305 *charptr = !strcmp(arg, SSH_AUTHSOCKET_ENV_NAME) ?
1306 xstrdup(arg) : derelativise_path(arg);
1307 break;
1308
Damien Miller0a80ca12010-02-27 07:55:05 +11001309 case sHostCertificate:
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001310 arg = strdelim(&cp);
1311 if (!arg || *arg == '\0')
1312 fatal("%s line %d: missing file name.",
1313 filename, linenum);
1314 if (*activep)
1315 servconf_add_hostcert(filename, linenum, options, arg);
1316 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001317
Ben Lindstromade03f62001-12-06 18:22:17 +00001318 case sPidFile:
1319 charptr = &options->pid_file;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001320 parse_filename:
1321 arg = strdelim(&cp);
1322 if (!arg || *arg == '\0')
1323 fatal("%s line %d: missing file name.",
1324 filename, linenum);
1325 if (*activep && *charptr == NULL) {
1326 *charptr = derelativise_path(arg);
1327 /* increase optional counter */
1328 if (intptr != NULL)
1329 *intptr = *intptr + 1;
1330 }
1331 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001332
1333 case sPermitRootLogin:
1334 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +10001335 multistate_ptr = multistate_permitrootlogin;
1336 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001337
1338 case sIgnoreRhosts:
1339 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +10001340 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +00001341 arg = strdelim(&cp);
1342 if (!arg || *arg == '\0')
1343 fatal("%s line %d: missing yes/no argument.",
1344 filename, linenum);
1345 value = 0; /* silence compiler */
1346 if (strcmp(arg, "yes") == 0)
1347 value = 1;
1348 else if (strcmp(arg, "no") == 0)
1349 value = 0;
1350 else
1351 fatal("%s line %d: Bad yes/no argument: %s",
1352 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +10001353 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001354 *intptr = value;
1355 break;
1356
1357 case sIgnoreUserKnownHosts:
1358 intptr = &options->ignore_user_known_hosts;
1359 goto parse_flag;
1360
Ben Lindstromade03f62001-12-06 18:22:17 +00001361 case sHostbasedAuthentication:
1362 intptr = &options->hostbased_authentication;
1363 goto parse_flag;
1364
1365 case sHostbasedUsesNameFromPacketOnly:
1366 intptr = &options->hostbased_uses_name_from_packet_only;
1367 goto parse_flag;
1368
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001369 case sHostbasedAcceptedKeyTypes:
1370 charptr = &options->hostbased_key_types;
1371 parse_keytypes:
1372 arg = strdelim(&cp);
1373 if (!arg || *arg == '\0')
1374 fatal("%s line %d: Missing argument.",
1375 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001376 if (*arg != '-' &&
1377 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001378 fatal("%s line %d: Bad key types '%s'.",
1379 filename, linenum, arg ? arg : "<NONE>");
1380 if (*activep && *charptr == NULL)
1381 *charptr = xstrdup(arg);
1382 break;
1383
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001384 case sHostKeyAlgorithms:
1385 charptr = &options->hostkeyalgorithms;
1386 goto parse_keytypes;
1387
Ben Lindstromade03f62001-12-06 18:22:17 +00001388 case sPubkeyAuthentication:
1389 intptr = &options->pubkey_authentication;
1390 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001391
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001392 case sPubkeyAcceptedKeyTypes:
1393 charptr = &options->pubkey_key_types;
1394 goto parse_keytypes;
1395
Ben Lindstromade03f62001-12-06 18:22:17 +00001396 case sKerberosAuthentication:
1397 intptr = &options->kerberos_authentication;
1398 goto parse_flag;
1399
1400 case sKerberosOrLocalPasswd:
1401 intptr = &options->kerberos_or_local_passwd;
1402 goto parse_flag;
1403
1404 case sKerberosTicketCleanup:
1405 intptr = &options->kerberos_ticket_cleanup;
1406 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001407
Darren Tucker22ef5082003-12-31 11:37:34 +11001408 case sKerberosGetAFSToken:
1409 intptr = &options->kerberos_get_afs_token;
1410 goto parse_flag;
1411
Darren Tucker0efd1552003-08-26 11:49:55 +10001412 case sGssAuthentication:
1413 intptr = &options->gss_authentication;
1414 goto parse_flag;
1415
1416 case sGssCleanupCreds:
1417 intptr = &options->gss_cleanup_creds;
1418 goto parse_flag;
1419
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +00001420 case sGssStrictAcceptor:
1421 intptr = &options->gss_strict_acceptor;
1422 goto parse_flag;
1423
Ben Lindstromade03f62001-12-06 18:22:17 +00001424 case sPasswordAuthentication:
1425 intptr = &options->password_authentication;
1426 goto parse_flag;
1427
1428 case sKbdInteractiveAuthentication:
1429 intptr = &options->kbd_interactive_authentication;
1430 goto parse_flag;
1431
1432 case sChallengeResponseAuthentication:
1433 intptr = &options->challenge_response_authentication;
1434 goto parse_flag;
1435
1436 case sPrintMotd:
1437 intptr = &options->print_motd;
1438 goto parse_flag;
1439
1440 case sPrintLastLog:
1441 intptr = &options->print_lastlog;
1442 goto parse_flag;
1443
1444 case sX11Forwarding:
1445 intptr = &options->x11_forwarding;
1446 goto parse_flag;
1447
1448 case sX11DisplayOffset:
1449 intptr = &options->x11_display_offset;
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +00001450 parse_int:
1451 arg = strdelim(&cp);
1452 if (!arg || *arg == '\0')
1453 fatal("%s line %d: missing integer value.",
1454 filename, linenum);
1455 value = atoi(arg);
1456 if (*activep && *intptr == -1)
1457 *intptr = value;
1458 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001459
Damien Miller95c249f2002-02-05 12:11:34 +11001460 case sX11UseLocalhost:
1461 intptr = &options->x11_use_localhost;
1462 goto parse_flag;
1463
Ben Lindstromade03f62001-12-06 18:22:17 +00001464 case sXAuthLocation:
1465 charptr = &options->xauth_location;
1466 goto parse_filename;
1467
Damien Miller5ff30c62013-10-30 22:21:50 +11001468 case sPermitTTY:
1469 intptr = &options->permit_tty;
1470 goto parse_flag;
1471
Damien Miller72e6b5c2014-07-04 09:00:04 +10001472 case sPermitUserRC:
1473 intptr = &options->permit_user_rc;
1474 goto parse_flag;
1475
Ben Lindstromade03f62001-12-06 18:22:17 +00001476 case sStrictModes:
1477 intptr = &options->strict_modes;
1478 goto parse_flag;
1479
Damien Miller12c150e2003-12-17 16:31:10 +11001480 case sTCPKeepAlive:
1481 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001482 goto parse_flag;
1483
1484 case sEmptyPasswd:
1485 intptr = &options->permit_empty_passwd;
1486 goto parse_flag;
1487
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001488 case sPermitUserEnvironment:
1489 intptr = &options->permit_user_env;
1490 goto parse_flag;
1491
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001492 case sCompression:
1493 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001494 multistate_ptr = multistate_compression;
1495 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001496
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001497 case sRekeyLimit:
1498 arg = strdelim(&cp);
1499 if (!arg || *arg == '\0')
1500 fatal("%.200s line %d: Missing argument.", filename,
1501 linenum);
1502 if (strcmp(arg, "default") == 0) {
1503 val64 = 0;
1504 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001505 if (scan_scaled(arg, &val64) == -1)
1506 fatal("%.200s line %d: Bad number '%s': %s",
1507 filename, linenum, arg, strerror(errno));
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001508 if (val64 != 0 && val64 < 16)
1509 fatal("%.200s line %d: RekeyLimit too small",
1510 filename, linenum);
1511 }
1512 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001513 options->rekey_limit = val64;
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001514 if (cp != NULL) { /* optional rekey interval present */
1515 if (strcmp(cp, "none") == 0) {
1516 (void)strdelim(&cp); /* discard */
1517 break;
1518 }
1519 intptr = &options->rekey_interval;
1520 goto parse_time;
1521 }
1522 break;
1523
Ben Lindstromade03f62001-12-06 18:22:17 +00001524 case sGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +10001525 intptr = &options->fwd_opts.gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001526 multistate_ptr = multistate_gatewayports;
1527 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001528
Damien Miller3a961dc2003-06-03 10:25:48 +10001529 case sUseDNS:
1530 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001531 goto parse_flag;
1532
1533 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001534 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001535 arg = strdelim(&cp);
1536 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001537 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001538 fatal("%.200s line %d: unsupported log facility '%s'",
1539 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001540 if (*log_facility_ptr == -1)
1541 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001542 break;
1543
1544 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001545 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001546 arg = strdelim(&cp);
1547 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001548 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001549 fatal("%.200s line %d: unsupported log level '%s'",
1550 filename, linenum, arg ? arg : "<NONE>");
djm@openbsd.org54cd41a2017-05-17 01:24:17 +00001551 if (*activep && *log_level_ptr == -1)
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001552 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001553 break;
1554
1555 case sAllowTcpForwarding:
1556 intptr = &options->allow_tcp_forwarding;
Damien Milleraa5b3f82012-12-03 09:50:54 +11001557 multistate_ptr = multistate_tcpfwd;
1558 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001559
Damien Miller7acefbb2014-07-18 14:11:24 +10001560 case sAllowStreamLocalForwarding:
1561 intptr = &options->allow_streamlocal_forwarding;
1562 multistate_ptr = multistate_tcpfwd;
1563 goto parse_multistate;
1564
Damien Miller4f755cd2008-05-19 14:57:41 +10001565 case sAllowAgentForwarding:
1566 intptr = &options->allow_agent_forwarding;
1567 goto parse_flag;
1568
djm@openbsd.org7844f352016-11-30 03:00:05 +00001569 case sDisableForwarding:
1570 intptr = &options->disable_forwarding;
1571 goto parse_flag;
1572
Ben Lindstromade03f62001-12-06 18:22:17 +00001573 case sAllowUsers:
1574 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.org010359b2016-11-06 05:46:37 +00001575 if (match_user(NULL, NULL, NULL, arg) == -1)
1576 fatal("%s line %d: invalid AllowUsers pattern: "
1577 "\"%.100s\"", filename, linenum, arg);
Damien Millerc24da772012-06-20 21:53:58 +10001578 if (!*activep)
1579 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001580 array_append(filename, linenum, "AllowUsers",
1581 &options->allow_users, &options->num_allow_users,
1582 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001583 }
1584 break;
1585
1586 case sDenyUsers:
1587 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.org010359b2016-11-06 05:46:37 +00001588 if (match_user(NULL, NULL, NULL, arg) == -1)
1589 fatal("%s line %d: invalid DenyUsers pattern: "
1590 "\"%.100s\"", filename, linenum, arg);
Damien Millerc24da772012-06-20 21:53:58 +10001591 if (!*activep)
1592 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001593 array_append(filename, linenum, "DenyUsers",
1594 &options->deny_users, &options->num_deny_users,
1595 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001596 }
1597 break;
1598
1599 case sAllowGroups:
1600 while ((arg = strdelim(&cp)) && *arg != '\0') {
Damien Millerc24da772012-06-20 21:53:58 +10001601 if (!*activep)
1602 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001603 array_append(filename, linenum, "AllowGroups",
1604 &options->allow_groups, &options->num_allow_groups,
1605 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001606 }
1607 break;
1608
1609 case sDenyGroups:
1610 while ((arg = strdelim(&cp)) && *arg != '\0') {
Damien Millerc24da772012-06-20 21:53:58 +10001611 if (!*activep)
1612 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001613 array_append(filename, linenum, "DenyGroups",
1614 &options->deny_groups, &options->num_deny_groups,
1615 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001616 }
1617 break;
1618
1619 case sCiphers:
1620 arg = strdelim(&cp);
1621 if (!arg || *arg == '\0')
1622 fatal("%s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001623 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstromade03f62001-12-06 18:22:17 +00001624 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1625 filename, linenum, arg ? arg : "<NONE>");
1626 if (options->ciphers == NULL)
1627 options->ciphers = xstrdup(arg);
1628 break;
1629
1630 case sMacs:
1631 arg = strdelim(&cp);
1632 if (!arg || *arg == '\0')
1633 fatal("%s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001634 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstromade03f62001-12-06 18:22:17 +00001635 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1636 filename, linenum, arg ? arg : "<NONE>");
1637 if (options->macs == NULL)
1638 options->macs = xstrdup(arg);
1639 break;
1640
Damien Millerd5f62bf2010-09-24 22:11:14 +10001641 case sKexAlgorithms:
1642 arg = strdelim(&cp);
1643 if (!arg || *arg == '\0')
1644 fatal("%s line %d: Missing argument.",
1645 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001646 if (*arg != '-' &&
1647 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001648 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1649 filename, linenum, arg ? arg : "<NONE>");
1650 if (options->kex_algorithms == NULL)
1651 options->kex_algorithms = xstrdup(arg);
1652 break;
1653
Ben Lindstromade03f62001-12-06 18:22:17 +00001654 case sSubsystem:
1655 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1656 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001657 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001658 }
1659 arg = strdelim(&cp);
1660 if (!arg || *arg == '\0')
1661 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001662 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001663 if (!*activep) {
1664 arg = strdelim(&cp);
1665 break;
1666 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001667 for (i = 0; i < options->num_subsystems; i++)
1668 if (strcmp(arg, options->subsystem_name[i]) == 0)
1669 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001670 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001671 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1672 arg = strdelim(&cp);
1673 if (!arg || *arg == '\0')
1674 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001675 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001676 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001677
1678 /* Collect arguments (separate to executable) */
1679 p = xstrdup(arg);
1680 len = strlen(p) + 1;
1681 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1682 len += 1 + strlen(arg);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001683 p = xreallocarray(p, 1, len);
Damien Miller917f9b62006-07-10 20:36:47 +10001684 strlcat(p, " ", len);
1685 strlcat(p, arg, len);
1686 }
1687 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001688 options->num_subsystems++;
1689 break;
1690
1691 case sMaxStartups:
1692 arg = strdelim(&cp);
1693 if (!arg || *arg == '\0')
1694 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001695 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001696 if ((n = sscanf(arg, "%d:%d:%d",
1697 &options->max_startups_begin,
1698 &options->max_startups_rate,
1699 &options->max_startups)) == 3) {
1700 if (options->max_startups_begin >
1701 options->max_startups ||
1702 options->max_startups_rate > 100 ||
1703 options->max_startups_rate < 1)
1704 fatal("%s line %d: Illegal MaxStartups spec.",
1705 filename, linenum);
1706 } else if (n != 1)
1707 fatal("%s line %d: Illegal MaxStartups spec.",
1708 filename, linenum);
1709 else
1710 options->max_startups = options->max_startups_begin;
1711 break;
1712
Darren Tucker89413db2004-05-24 10:36:23 +10001713 case sMaxAuthTries:
1714 intptr = &options->max_authtries;
1715 goto parse_int;
1716
Damien Miller7207f642008-05-19 15:34:50 +10001717 case sMaxSessions:
1718 intptr = &options->max_sessions;
1719 goto parse_int;
1720
Ben Lindstromade03f62001-12-06 18:22:17 +00001721 case sBanner:
1722 charptr = &options->banner;
1723 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001724
Ben Lindstromade03f62001-12-06 18:22:17 +00001725 /*
1726 * These options can contain %X options expanded at
1727 * connect time, so that you can specify paths like:
1728 *
1729 * AuthorizedKeysFile /etc/ssh_keys/%u
1730 */
1731 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001732 if (*activep && options->num_authkeys_files == 0) {
1733 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001734 arg = tilde_expand_filename(arg, getuid());
1735 array_append(filename, linenum,
1736 "AuthorizedKeysFile",
1737 &options->authorized_keys_files,
1738 &options->num_authkeys_files, arg);
1739 free(arg);
Damien Millerd8478b62011-05-29 21:39:36 +10001740 }
1741 }
1742 return 0;
1743
Damien Miller30da3442010-05-10 11:58:03 +10001744 case sAuthorizedPrincipalsFile:
1745 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001746 arg = strdelim(&cp);
1747 if (!arg || *arg == '\0')
1748 fatal("%s line %d: missing file name.",
1749 filename, linenum);
1750 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001751 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001752 /* increase optional counter */
1753 if (intptr != NULL)
1754 *intptr = *intptr + 1;
1755 }
1756 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001757
1758 case sClientAliveInterval:
1759 intptr = &options->client_alive_interval;
1760 goto parse_time;
1761
1762 case sClientAliveCountMax:
1763 intptr = &options->client_alive_count_max;
1764 goto parse_int;
1765
Darren Tucker46bc0752004-05-02 22:11:30 +10001766 case sAcceptEnv:
1767 while ((arg = strdelim(&cp)) && *arg != '\0') {
1768 if (strchr(arg, '=') != NULL)
1769 fatal("%s line %d: Invalid environment name.",
1770 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001771 if (!*activep)
Damien Millerc24da772012-06-20 21:53:58 +10001772 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001773 array_append(filename, linenum, "AcceptEnv",
1774 &options->accept_env, &options->num_accept_env,
1775 arg);
Darren Tucker46bc0752004-05-02 22:11:30 +10001776 }
1777 break;
1778
Damien Millerd27b9472005-12-13 19:29:02 +11001779 case sPermitTunnel:
1780 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001781 arg = strdelim(&cp);
1782 if (!arg || *arg == '\0')
1783 fatal("%s line %d: Missing yes/point-to-point/"
1784 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001785 value = -1;
1786 for (i = 0; tunmode_desc[i].val != -1; i++)
1787 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1788 value = tunmode_desc[i].val;
1789 break;
1790 }
1791 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001792 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1793 "no argument: %s", filename, linenum, arg);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001794 if (*activep && *intptr == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001795 *intptr = value;
1796 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001797
Darren Tucker45150472006-07-12 22:34:17 +10001798 case sMatch:
1799 if (cmdline)
1800 fatal("Match directive not supported as a command-line "
1801 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001802 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10001803 if (value < 0)
1804 fatal("%s line %d: Bad Match condition", filename,
1805 linenum);
1806 *activep = value;
1807 break;
1808
Damien Miller9b439df2006-07-24 14:04:00 +10001809 case sPermitOpen:
1810 arg = strdelim(&cp);
1811 if (!arg || *arg == '\0')
1812 fatal("%s line %d: missing PermitOpen specification",
1813 filename, linenum);
djm@openbsd.org69bda022017-10-04 18:49:30 +00001814 value = options->num_permitted_opens; /* modified later */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001815 if (strcmp(arg, "any") == 0 || strcmp(arg, "none") == 0) {
djm@openbsd.org69bda022017-10-04 18:49:30 +00001816 if (*activep && value == 0) {
Damien Millerc6081482012-04-22 11:18:53 +10001817 options->num_permitted_opens = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001818 options->permitted_opens = xcalloc(1,
1819 sizeof(*options->permitted_opens));
1820 options->permitted_opens[0] = xstrdup(arg);
dtucker@openbsd.org30484e52017-09-18 09:41:52 +00001821 }
Damien Millerc6081482012-04-22 11:18:53 +10001822 break;
1823 }
Damien Millera765cf42006-07-24 14:08:13 +10001824 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001825 arg2 = xstrdup(arg);
Damien Millera765cf42006-07-24 14:08:13 +10001826 p = hpdelim(&arg);
1827 if (p == NULL)
1828 fatal("%s line %d: missing host in PermitOpen",
1829 filename, linenum);
1830 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001831 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001832 fatal("%s line %d: bad port number in "
1833 "PermitOpen", filename, linenum);
djm@openbsd.org69bda022017-10-04 18:49:30 +00001834 if (*activep && value == 0) {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001835 array_append(filename, linenum,
1836 "PermitOpen",
1837 &options->permitted_opens,
1838 &options->num_permitted_opens, arg2);
1839 }
1840 free(arg2);
Damien Millera765cf42006-07-24 14:08:13 +10001841 }
Damien Miller9b439df2006-07-24 14:04:00 +10001842 break;
1843
Damien Millere2754432006-07-24 14:06:47 +10001844 case sForceCommand:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001845 if (cp == NULL || *cp == '\0')
Damien Millere2754432006-07-24 14:06:47 +10001846 fatal("%.200s line %d: Missing argument.", filename,
1847 linenum);
1848 len = strspn(cp, WHITESPACE);
1849 if (*activep && options->adm_forced_command == NULL)
1850 options->adm_forced_command = xstrdup(cp + len);
1851 return 0;
1852
Damien Millerd8cb1f12008-02-10 22:40:12 +11001853 case sChrootDirectory:
1854 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001855
1856 arg = strdelim(&cp);
1857 if (!arg || *arg == '\0')
1858 fatal("%s line %d: missing file name.",
1859 filename, linenum);
1860 if (*activep && *charptr == NULL)
1861 *charptr = xstrdup(arg);
1862 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001863
Damien Miller1aed65e2010-03-04 21:53:35 +11001864 case sTrustedUserCAKeys:
1865 charptr = &options->trusted_user_ca_keys;
1866 goto parse_filename;
1867
1868 case sRevokedKeys:
1869 charptr = &options->revoked_keys_file;
1870 goto parse_filename;
1871
Damien Miller0dac6fb2010-11-20 15:19:38 +11001872 case sIPQoS:
1873 arg = strdelim(&cp);
1874 if ((value = parse_ipqos(arg)) == -1)
1875 fatal("%s line %d: Bad IPQoS value: %s",
1876 filename, linenum, arg);
1877 arg = strdelim(&cp);
1878 if (arg == NULL)
1879 value2 = value;
1880 else if ((value2 = parse_ipqos(arg)) == -1)
1881 fatal("%s line %d: Bad IPQoS value: %s",
1882 filename, linenum, arg);
1883 if (*activep) {
1884 options->ip_qos_interactive = value;
1885 options->ip_qos_bulk = value2;
1886 }
1887 break;
1888
Damien Miller23528812012-04-22 11:24:43 +10001889 case sVersionAddendum:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001890 if (cp == NULL || *cp == '\0')
Damien Miller23528812012-04-22 11:24:43 +10001891 fatal("%.200s line %d: Missing argument.", filename,
1892 linenum);
1893 len = strspn(cp, WHITESPACE);
1894 if (*activep && options->version_addendum == NULL) {
1895 if (strcasecmp(cp + len, "none") == 0)
1896 options->version_addendum = xstrdup("");
1897 else if (strchr(cp + len, '\r') != NULL)
1898 fatal("%.200s line %d: Invalid argument",
1899 filename, linenum);
1900 else
1901 options->version_addendum = xstrdup(cp + len);
1902 }
1903 return 0;
1904
Damien Miller09d3e122012-10-31 08:58:58 +11001905 case sAuthorizedKeysCommand:
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001906 if (cp == NULL)
1907 fatal("%.200s line %d: Missing argument.", filename,
1908 linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001909 len = strspn(cp, WHITESPACE);
1910 if (*activep && options->authorized_keys_command == NULL) {
1911 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1912 fatal("%.200s line %d: AuthorizedKeysCommand "
1913 "must be an absolute path",
1914 filename, linenum);
1915 options->authorized_keys_command = xstrdup(cp + len);
1916 }
1917 return 0;
1918
1919 case sAuthorizedKeysCommandUser:
1920 charptr = &options->authorized_keys_command_user;
1921
1922 arg = strdelim(&cp);
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001923 if (!arg || *arg == '\0')
1924 fatal("%s line %d: missing AuthorizedKeysCommandUser "
1925 "argument.", filename, linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001926 if (*activep && *charptr == NULL)
1927 *charptr = xstrdup(arg);
1928 break;
1929
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +00001930 case sAuthorizedPrincipalsCommand:
1931 if (cp == NULL)
1932 fatal("%.200s line %d: Missing argument.", filename,
1933 linenum);
1934 len = strspn(cp, WHITESPACE);
1935 if (*activep &&
1936 options->authorized_principals_command == NULL) {
1937 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1938 fatal("%.200s line %d: "
1939 "AuthorizedPrincipalsCommand must be "
1940 "an absolute path", filename, linenum);
1941 options->authorized_principals_command =
1942 xstrdup(cp + len);
1943 }
1944 return 0;
1945
1946 case sAuthorizedPrincipalsCommandUser:
1947 charptr = &options->authorized_principals_command_user;
1948
1949 arg = strdelim(&cp);
1950 if (!arg || *arg == '\0')
1951 fatal("%s line %d: missing "
1952 "AuthorizedPrincipalsCommandUser argument.",
1953 filename, linenum);
1954 if (*activep && *charptr == NULL)
1955 *charptr = xstrdup(arg);
1956 break;
1957
Damien Millera6e3f012012-11-04 23:21:40 +11001958 case sAuthenticationMethods:
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001959 if (options->num_auth_methods == 0) {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001960 value = 0; /* seen "any" pseudo-method */
djm@openbsd.org46ecd192016-06-23 05:17:51 +00001961 value2 = 0; /* sucessfully parsed any method */
Damien Millera6e3f012012-11-04 23:21:40 +11001962 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001963 if (strcmp(arg, "any") == 0) {
1964 if (options->num_auth_methods > 0) {
1965 fatal("%s line %d: \"any\" "
1966 "must appear alone in "
1967 "AuthenticationMethods",
1968 filename, linenum);
1969 }
1970 value = 1;
1971 } else if (value) {
1972 fatal("%s line %d: \"any\" must appear "
1973 "alone in AuthenticationMethods",
1974 filename, linenum);
1975 } else if (auth2_methods_valid(arg, 0) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +11001976 fatal("%s line %d: invalid "
1977 "authentication method list.",
1978 filename, linenum);
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001979 }
djm@openbsd.org46ecd192016-06-23 05:17:51 +00001980 value2 = 1;
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001981 if (!*activep)
1982 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001983 array_append(filename, linenum,
1984 "AuthenticationMethods",
1985 &options->auth_methods,
1986 &options->num_auth_methods, arg);
Damien Millera6e3f012012-11-04 23:21:40 +11001987 }
djm@openbsd.org46ecd192016-06-23 05:17:51 +00001988 if (value2 == 0) {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001989 fatal("%s line %d: no AuthenticationMethods "
1990 "specified", filename, linenum);
1991 }
Damien Millera6e3f012012-11-04 23:21:40 +11001992 }
1993 return 0;
1994
Damien Miller7acefbb2014-07-18 14:11:24 +10001995 case sStreamLocalBindMask:
1996 arg = strdelim(&cp);
1997 if (!arg || *arg == '\0')
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001998 fatal("%s line %d: missing StreamLocalBindMask "
1999 "argument.", filename, linenum);
Damien Miller7acefbb2014-07-18 14:11:24 +10002000 /* Parse mode in octal format */
2001 value = strtol(arg, &p, 8);
2002 if (arg == p || value < 0 || value > 0777)
2003 fatal("%s line %d: Bad mask.", filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00002004 if (*activep)
2005 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
Damien Miller7acefbb2014-07-18 14:11:24 +10002006 break;
2007
2008 case sStreamLocalBindUnlink:
2009 intptr = &options->fwd_opts.streamlocal_bind_unlink;
2010 goto parse_flag;
2011
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002012 case sFingerprintHash:
2013 arg = strdelim(&cp);
2014 if (!arg || *arg == '\0')
2015 fatal("%.200s line %d: Missing argument.",
2016 filename, linenum);
2017 if ((value = ssh_digest_alg_by_name(arg)) == -1)
2018 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
2019 filename, linenum, arg);
2020 if (*activep)
2021 options->fingerprint_hash = value;
2022 break;
2023
djm@openbsd.org8f574952017-06-24 06:34:38 +00002024 case sExposeAuthInfo:
2025 intptr = &options->expose_userauth_info;
2026 goto parse_flag;
2027
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002028 case sRDomain:
2029 charptr = &options->routing_domain;
2030 arg = strdelim(&cp);
2031 if (!arg || *arg == '\0')
2032 fatal("%.200s line %d: Missing argument.",
2033 filename, linenum);
2034 if (strcasecmp(arg, "none") != 0 && strcmp(arg, "%D") != 0 &&
2035 !valid_rdomain(arg))
2036 fatal("%s line %d: bad routing domain",
2037 filename, linenum);
2038 if (*activep && *charptr == NULL)
2039 *charptr = xstrdup(arg);
2040
Ben Lindstromade03f62001-12-06 18:22:17 +00002041 case sDeprecated:
djm@openbsd.orgae363d72016-08-25 23:57:54 +00002042 case sIgnore:
Damien Millerf9b3feb2003-05-16 11:38:32 +10002043 case sUnsupported:
djm@openbsd.orgae363d72016-08-25 23:57:54 +00002044 do_log2(opcode == sIgnore ?
2045 SYSLOG_LEVEL_DEBUG2 : SYSLOG_LEVEL_INFO,
2046 "%s line %d: %s option %s", filename, linenum,
2047 opcode == sUnsupported ? "Unsupported" : "Deprecated", arg);
Damien Millerf9b3feb2003-05-16 11:38:32 +10002048 while (arg)
2049 arg = strdelim(&cp);
2050 break;
2051
Ben Lindstromade03f62001-12-06 18:22:17 +00002052 default:
2053 fatal("%s line %d: Missing handler for opcode %s (%d)",
2054 filename, linenum, arg, opcode);
2055 }
2056 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
2057 fatal("%s line %d: garbage at end of line; \"%.200s\".",
2058 filename, linenum, arg);
2059 return 0;
2060}
2061
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002062/* Reads the server configuration file. */
2063
Damien Miller4af51302000-04-16 11:18:38 +10002064void
Darren Tucker645ab752004-06-25 13:33:20 +10002065load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002066{
Damien Miller46cb75a2012-07-31 12:22:37 +10002067 char line[4096], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00002068 FILE *f;
Damien Miller46cb75a2012-07-31 12:22:37 +10002069 int lineno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002070
Darren Tucker645ab752004-06-25 13:33:20 +10002071 debug2("%s: filename %s", __func__, filename);
2072 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11002073 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002074 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11002075 }
Darren Tucker645ab752004-06-25 13:33:20 +10002076 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11002077 while (fgets(line, sizeof(line), f)) {
Damien Miller46cb75a2012-07-31 12:22:37 +10002078 lineno++;
2079 if (strlen(line) == sizeof(line) - 1)
2080 fatal("%s line %d too long", filename, lineno);
Darren Tucker645ab752004-06-25 13:33:20 +10002081 /*
2082 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10002083 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10002084 * line numbers later for error messages
2085 */
2086 if ((cp = strchr(line, '#')) != NULL)
2087 memcpy(cp, "\n", 2);
2088 cp = line + strspn(line, " \t\r");
2089
2090 buffer_append(conf, cp, strlen(cp));
2091 }
2092 buffer_append(conf, "\0", 1);
2093 fclose(f);
2094 debug2("%s: done config len = %d", __func__, buffer_len(conf));
2095}
2096
2097void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002098parse_server_match_config(ServerOptions *options,
2099 struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10002100{
Darren Tucker45150472006-07-12 22:34:17 +10002101 ServerOptions mo;
2102
2103 initialize_server_options(&mo);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002104 parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11002105 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10002106}
2107
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002108int parse_server_match_testspec(struct connection_info *ci, char *spec)
2109{
2110 char *p;
2111
2112 while ((p = strsep(&spec, ",")) && *p != '\0') {
2113 if (strncmp(p, "addr=", 5) == 0) {
2114 ci->address = xstrdup(p + 5);
2115 } else if (strncmp(p, "host=", 5) == 0) {
2116 ci->host = xstrdup(p + 5);
2117 } else if (strncmp(p, "user=", 5) == 0) {
2118 ci->user = xstrdup(p + 5);
2119 } else if (strncmp(p, "laddr=", 6) == 0) {
2120 ci->laddress = xstrdup(p + 6);
djm@openbsd.org68af80e2017-10-25 00:19:47 +00002121 } else if (strncmp(p, "rdomain=", 8) == 0) {
2122 ci->rdomain = xstrdup(p + 8);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002123 } else if (strncmp(p, "lport=", 6) == 0) {
2124 ci->lport = a2port(p + 6);
2125 if (ci->lport == -1) {
2126 fprintf(stderr, "Invalid port '%s' in test mode"
2127 " specification %s\n", p+6, p);
2128 return -1;
2129 }
2130 } else {
2131 fprintf(stderr, "Invalid test mode specification %s\n",
2132 p);
2133 return -1;
2134 }
2135 }
2136 return 0;
2137}
2138
2139/*
Darren Tucker1629c072007-02-19 22:25:37 +11002140 * Copy any supported values that are set.
2141 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10002142 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11002143 * array values that are not used pre-authentication, because any that we
2144 * do use must be explictly sent in mm_getpwnamallow().
2145 */
Darren Tucker45150472006-07-12 22:34:17 +10002146void
Darren Tucker1629c072007-02-19 22:25:37 +11002147copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10002148{
Damien Miller534b2cc2013-12-05 14:07:27 +11002149#define M_CP_INTOPT(n) do {\
2150 if (src->n != -1) \
2151 dst->n = src->n; \
2152} while (0)
2153
Darren Tucker1629c072007-02-19 22:25:37 +11002154 M_CP_INTOPT(password_authentication);
2155 M_CP_INTOPT(gss_authentication);
Darren Tucker1629c072007-02-19 22:25:37 +11002156 M_CP_INTOPT(pubkey_authentication);
2157 M_CP_INTOPT(kerberos_authentication);
2158 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10002159 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11002160 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11002161 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11002162 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11002163
2164 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002165 M_CP_INTOPT(allow_streamlocal_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10002166 M_CP_INTOPT(allow_agent_forwarding);
djm@openbsd.org7844f352016-11-30 03:00:05 +00002167 M_CP_INTOPT(disable_forwarding);
djm@openbsd.org8f574952017-06-24 06:34:38 +00002168 M_CP_INTOPT(expose_userauth_info);
Damien Millerab6de352010-06-26 09:38:45 +10002169 M_CP_INTOPT(permit_tun);
Damien Miller7acefbb2014-07-18 14:11:24 +10002170 M_CP_INTOPT(fwd_opts.gateway_ports);
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002171 M_CP_INTOPT(fwd_opts.streamlocal_bind_unlink);
Darren Tucker1629c072007-02-19 22:25:37 +11002172 M_CP_INTOPT(x11_display_offset);
2173 M_CP_INTOPT(x11_forwarding);
2174 M_CP_INTOPT(x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002175 M_CP_INTOPT(permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002176 M_CP_INTOPT(permit_user_rc);
Damien Miller7207f642008-05-19 15:34:50 +10002177 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10002178 M_CP_INTOPT(max_authtries);
markus@openbsd.orgf0ddede2016-11-23 23:14:15 +00002179 M_CP_INTOPT(client_alive_count_max);
2180 M_CP_INTOPT(client_alive_interval);
Damien Miller0dac6fb2010-11-20 15:19:38 +11002181 M_CP_INTOPT(ip_qos_interactive);
2182 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002183 M_CP_INTOPT(rekey_limit);
2184 M_CP_INTOPT(rekey_interval);
djm@openbsd.org54cd41a2017-05-17 01:24:17 +00002185 M_CP_INTOPT(log_level);
Darren Tucker1629c072007-02-19 22:25:37 +11002186
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002187 /*
2188 * The bind_mask is a mode_t that may be unsigned, so we can't use
2189 * M_CP_INTOPT - it does a signed comparison that causes compiler
2190 * warnings.
2191 */
dtucker@openbsd.org9faae502016-05-04 14:00:09 +00002192 if (src->fwd_opts.streamlocal_bind_mask != (mode_t)-1) {
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002193 dst->fwd_opts.streamlocal_bind_mask =
2194 src->fwd_opts.streamlocal_bind_mask;
2195 }
2196
Damien Miller534b2cc2013-12-05 14:07:27 +11002197 /* M_CP_STROPT and M_CP_STRARRAYOPT should not appear before here */
2198#define M_CP_STROPT(n) do {\
2199 if (src->n != NULL && dst->n != src->n) { \
2200 free(dst->n); \
2201 dst->n = src->n; \
2202 } \
2203} while(0)
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00002204#define M_CP_STRARRAYOPT(s, num_s) do {\
2205 u_int i; \
2206 if (src->num_s != 0) { \
2207 for (i = 0; i < dst->num_s; i++) \
2208 free(dst->s[i]); \
2209 free(dst->s); \
2210 dst->s = xcalloc(src->num_s, sizeof(*dst->s)); \
2211 for (i = 0; i < src->num_s; i++) \
2212 dst->s[i] = xstrdup(src->s[i]); \
2213 dst->num_s = src->num_s; \
djm@openbsd.org66bf74a2017-10-02 19:33:20 +00002214 } \
2215} while(0)
Damien Miller534b2cc2013-12-05 14:07:27 +11002216
Damien Millerf2e407e2011-05-20 19:04:14 +10002217 /* See comment in servconf.h */
2218 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10002219
djm@openbsd.orged085102015-10-29 08:05:01 +00002220 /* Arguments that accept '+...' need to be expanded */
2221 assemble_algorithms(dst);
2222
Damien Millerc2411902011-05-20 19:03:49 +10002223 /*
2224 * The only things that should be below this point are string options
2225 * which are only used after authentication.
2226 */
Damien Miller5d74e582011-05-20 19:03:31 +10002227 if (preauth)
2228 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002229
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002230 /* These options may be "none" to clear a global setting */
Damien Miller5d74e582011-05-20 19:03:31 +10002231 M_CP_STROPT(adm_forced_command);
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002232 if (option_clear_or_none(dst->adm_forced_command)) {
2233 free(dst->adm_forced_command);
2234 dst->adm_forced_command = NULL;
2235 }
Damien Miller5d74e582011-05-20 19:03:31 +10002236 M_CP_STROPT(chroot_directory);
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002237 if (option_clear_or_none(dst->chroot_directory)) {
2238 free(dst->chroot_directory);
2239 dst->chroot_directory = NULL;
2240 }
Darren Tucker45150472006-07-12 22:34:17 +10002241}
2242
Darren Tucker1629c072007-02-19 22:25:37 +11002243#undef M_CP_INTOPT
2244#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10002245#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11002246
Darren Tucker45150472006-07-12 22:34:17 +10002247void
2248parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002249 struct connection_info *connectinfo)
Darren Tucker45150472006-07-12 22:34:17 +10002250{
2251 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10002252 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10002253
2254 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
2255
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002256 if ((obuf = cbuf = sshbuf_dup_string(conf)) == NULL)
2257 fatal("%s: sshbuf_dup_string failed", __func__);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002258 active = connectinfo ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10002259 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11002260 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10002261 if (process_server_config_line(options, cp, filename,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002262 linenum++, &active, connectinfo) != 0)
Damien Miller95def091999-11-25 00:26:21 +11002263 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002264 }
Darren Tuckera627d422013-06-02 07:31:17 +10002265 free(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00002266 if (bad_options > 0)
2267 fatal("%s: terminating, %d bad configuration options",
2268 filename, bad_options);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00002269 process_queued_listen_addrs(options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002270}
Darren Tuckere7140f22008-06-10 23:01:51 +10002271
2272static const char *
Damien Miller82c55872011-06-23 08:20:30 +10002273fmt_multistate_int(int val, const struct multistate *m)
2274{
2275 u_int i;
2276
2277 for (i = 0; m[i].key != NULL; i++) {
2278 if (m[i].value == val)
2279 return m[i].key;
2280 }
2281 return "UNKNOWN";
2282}
2283
2284static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10002285fmt_intarg(ServerOpCodes code, int val)
2286{
Damien Miller82c55872011-06-23 08:20:30 +10002287 if (val == -1)
2288 return "unset";
2289 switch (code) {
2290 case sAddressFamily:
2291 return fmt_multistate_int(val, multistate_addressfamily);
2292 case sPermitRootLogin:
2293 return fmt_multistate_int(val, multistate_permitrootlogin);
2294 case sGatewayPorts:
2295 return fmt_multistate_int(val, multistate_gatewayports);
2296 case sCompression:
2297 return fmt_multistate_int(val, multistate_compression);
Damien Milleraa5b3f82012-12-03 09:50:54 +11002298 case sAllowTcpForwarding:
2299 return fmt_multistate_int(val, multistate_tcpfwd);
Damien Miller7acefbb2014-07-18 14:11:24 +10002300 case sAllowStreamLocalForwarding:
2301 return fmt_multistate_int(val, multistate_tcpfwd);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002302 case sFingerprintHash:
2303 return ssh_digest_alg_name(val);
Damien Miller82c55872011-06-23 08:20:30 +10002304 default:
2305 switch (val) {
2306 case 0:
2307 return "no";
2308 case 1:
2309 return "yes";
2310 default:
2311 return "UNKNOWN";
2312 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002313 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002314}
2315
2316static const char *
2317lookup_opcode_name(ServerOpCodes code)
2318{
2319 u_int i;
2320
2321 for (i = 0; keywords[i].name != NULL; i++)
2322 if (keywords[i].opcode == code)
2323 return(keywords[i].name);
2324 return "UNKNOWN";
2325}
2326
2327static void
2328dump_cfg_int(ServerOpCodes code, int val)
2329{
2330 printf("%s %d\n", lookup_opcode_name(code), val);
2331}
2332
2333static void
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002334dump_cfg_oct(ServerOpCodes code, int val)
2335{
2336 printf("%s 0%o\n", lookup_opcode_name(code), val);
2337}
2338
2339static void
Darren Tuckere7140f22008-06-10 23:01:51 +10002340dump_cfg_fmtint(ServerOpCodes code, int val)
2341{
2342 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2343}
2344
2345static void
2346dump_cfg_string(ServerOpCodes code, const char *val)
2347{
djm@openbsd.org161cf412014-12-22 07:55:51 +00002348 printf("%s %s\n", lookup_opcode_name(code),
2349 val == NULL ? "none" : val);
Darren Tuckere7140f22008-06-10 23:01:51 +10002350}
2351
2352static void
2353dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
2354{
2355 u_int i;
2356
2357 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10002358 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2359}
2360
2361static void
2362dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
2363{
2364 u_int i;
2365
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002366 if (count <= 0 && code != sAuthenticationMethods)
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002367 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002368 printf("%s", lookup_opcode_name(code));
2369 for (i = 0; i < count; i++)
2370 printf(" %s", vals[i]);
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002371 if (code == sAuthenticationMethods && count == 0)
2372 printf(" any");
Damien Millerd8478b62011-05-29 21:39:36 +10002373 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002374}
2375
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002376static char *
2377format_listen_addrs(struct listenaddr *la)
Darren Tuckere7140f22008-06-10 23:01:51 +10002378{
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002379 int r;
Darren Tuckere7140f22008-06-10 23:01:51 +10002380 struct addrinfo *ai;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002381 char addr[NI_MAXHOST], port[NI_MAXSERV];
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002382 char *laddr1 = xstrdup(""), *laddr2 = NULL;
Darren Tuckere7140f22008-06-10 23:01:51 +10002383
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002384 /*
2385 * ListenAddress must be after Port. add_one_listen_addr pushes
2386 * addresses onto a stack, so to maintain ordering we need to
2387 * print these in reverse order.
2388 */
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002389 for (ai = la->addrs; ai; ai = ai->ai_next) {
2390 if ((r = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
Darren Tuckere7140f22008-06-10 23:01:51 +10002391 sizeof(addr), port, sizeof(port),
2392 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002393 error("getnameinfo: %.100s", ssh_gai_strerror(r));
2394 continue;
Darren Tuckere7140f22008-06-10 23:01:51 +10002395 }
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002396 laddr2 = laddr1;
2397 if (ai->ai_family == AF_INET6) {
2398 xasprintf(&laddr1, "listenaddress [%s]:%s%s%s\n%s",
2399 addr, port,
2400 la->rdomain == NULL ? "" : " rdomain ",
2401 la->rdomain == NULL ? "" : la->rdomain,
2402 laddr2);
2403 } else {
2404 xasprintf(&laddr1, "listenaddress %s:%s%s%s\n%s",
2405 addr, port,
2406 la->rdomain == NULL ? "" : " rdomain ",
2407 la->rdomain == NULL ? "" : la->rdomain,
2408 laddr2);
2409 }
2410 free(laddr2);
Darren Tuckere7140f22008-06-10 23:01:51 +10002411 }
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002412 return laddr1;
2413}
2414
2415void
2416dump_config(ServerOptions *o)
2417{
2418 char *s;
2419 u_int i;
2420
2421 /* these are usually at the top of the config */
2422 for (i = 0; i < o->num_ports; i++)
2423 printf("port %d\n", o->ports[i]);
2424 dump_cfg_fmtint(sAddressFamily, o->address_family);
2425
2426 for (i = 0; i < o->num_listen_addrs; i++) {
2427 s = format_listen_addrs(&o->listen_addrs[i]);
2428 printf("%s", s);
2429 free(s);
2430 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002431
2432 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10002433#ifdef USE_PAM
Darren Tucker70860b62015-04-17 10:56:13 +10002434 dump_cfg_fmtint(sUsePAM, o->use_pam);
Damien Miller212f0b02008-07-23 17:42:29 +10002435#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002436 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
Darren Tuckere7140f22008-06-10 23:01:51 +10002437 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
2438 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11002439 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10002440 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
2441 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002442 dump_cfg_oct(sStreamLocalBindMask, o->fwd_opts.streamlocal_bind_mask);
Darren Tuckere7140f22008-06-10 23:01:51 +10002443
2444 /* formatted integer arguments */
2445 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
2446 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
2447 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
Darren Tuckere7140f22008-06-10 23:01:51 +10002448 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
2449 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
2450 o->hostbased_uses_name_from_packet_only);
Darren Tuckere7140f22008-06-10 23:01:51 +10002451 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10002452#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10002453 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
2454 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
2455 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10002456# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10002457 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10002458# endif
2459#endif
2460#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10002461 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2462 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10002463#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002464 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2465 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2466 o->kbd_interactive_authentication);
2467 dump_cfg_fmtint(sChallengeResponseAuthentication,
2468 o->challenge_response_authentication);
2469 dump_cfg_fmtint(sPrintMotd, o->print_motd);
Darren Tuckerfd4e4f22016-02-24 10:44:25 +11002470#ifndef DISABLE_LASTLOG
Darren Tuckere7140f22008-06-10 23:01:51 +10002471 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
Darren Tuckerfd4e4f22016-02-24 10:44:25 +11002472#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002473 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
2474 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002475 dump_cfg_fmtint(sPermitTTY, o->permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002476 dump_cfg_fmtint(sPermitUserRC, o->permit_user_rc);
Darren Tuckere7140f22008-06-10 23:01:51 +10002477 dump_cfg_fmtint(sStrictModes, o->strict_modes);
2478 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
2479 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
2480 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
Darren Tuckere7140f22008-06-10 23:01:51 +10002481 dump_cfg_fmtint(sCompression, o->compression);
Damien Miller7acefbb2014-07-18 14:11:24 +10002482 dump_cfg_fmtint(sGatewayPorts, o->fwd_opts.gateway_ports);
Darren Tuckere7140f22008-06-10 23:01:51 +10002483 dump_cfg_fmtint(sUseDNS, o->use_dns);
2484 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002485 dump_cfg_fmtint(sAllowAgentForwarding, o->allow_agent_forwarding);
djm@openbsd.org7844f352016-11-30 03:00:05 +00002486 dump_cfg_fmtint(sDisableForwarding, o->disable_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002487 dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
djm@openbsd.org771c2f52016-05-03 15:25:06 +00002488 dump_cfg_fmtint(sStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002489 dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
djm@openbsd.org8f574952017-06-24 06:34:38 +00002490 dump_cfg_fmtint(sExposeAuthInfo, o->expose_userauth_info);
Darren Tuckere7140f22008-06-10 23:01:51 +10002491
2492 /* string arguments */
2493 dump_cfg_string(sPidFile, o->pid_file);
2494 dump_cfg_string(sXAuthLocation, o->xauth_location);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002495 dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
2496 dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
Darren Tuckere7140f22008-06-10 23:01:51 +10002497 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10002498 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11002499 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11002500 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
2501 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10002502 dump_cfg_string(sAuthorizedPrincipalsFile,
2503 o->authorized_principals_file);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002504 dump_cfg_string(sVersionAddendum, *o->version_addendum == '\0'
2505 ? "none" : o->version_addendum);
Damien Miller09d3e122012-10-31 08:58:58 +11002506 dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
2507 dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +00002508 dump_cfg_string(sAuthorizedPrincipalsCommand, o->authorized_principals_command);
2509 dump_cfg_string(sAuthorizedPrincipalsCommandUser, o->authorized_principals_command_user);
Damien Miller85b45e02013-07-20 13:21:52 +10002510 dump_cfg_string(sHostKeyAgent, o->host_key_agent);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002511 dump_cfg_string(sKexAlgorithms,
djm@openbsd.org259a02e2014-10-13 00:38:35 +00002512 o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002513 dump_cfg_string(sHostbasedAcceptedKeyTypes, o->hostbased_key_types ?
2514 o->hostbased_key_types : KEX_DEFAULT_PK_ALG);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002515 dump_cfg_string(sHostKeyAlgorithms, o->hostkeyalgorithms ?
2516 o->hostkeyalgorithms : KEX_DEFAULT_PK_ALG);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002517 dump_cfg_string(sPubkeyAcceptedKeyTypes, o->pubkey_key_types ?
2518 o->pubkey_key_types : KEX_DEFAULT_PK_ALG);
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002519 dump_cfg_string(sRDomain, o->routing_domain);
Darren Tuckere7140f22008-06-10 23:01:51 +10002520
2521 /* string arguments requiring a lookup */
2522 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
2523 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
2524
2525 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10002526 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
2527 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002528 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
2529 o->host_key_files);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002530 dump_cfg_strarray(sHostCertificate, o->num_host_cert_files,
Damien Miller0a80ca12010-02-27 07:55:05 +11002531 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002532 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
2533 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
2534 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
2535 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
2536 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
Damien Millera6e3f012012-11-04 23:21:40 +11002537 dump_cfg_strarray_oneline(sAuthenticationMethods,
2538 o->num_auth_methods, o->auth_methods);
Darren Tuckere7140f22008-06-10 23:01:51 +10002539
2540 /* other arguments */
2541 for (i = 0; i < o->num_subsystems; i++)
2542 printf("subsystem %s %s\n", o->subsystem_name[i],
2543 o->subsystem_args[i]);
2544
2545 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
2546 o->max_startups_rate, o->max_startups);
2547
djm@openbsd.orgd685e5a2017-10-25 02:10:39 +00002548 s = NULL;
2549 for (i = 0; tunmode_desc[i].val != -1; i++) {
Darren Tuckere7140f22008-06-10 23:01:51 +10002550 if (tunmode_desc[i].val == o->permit_tun) {
2551 s = tunmode_desc[i].text;
2552 break;
2553 }
djm@openbsd.orgd685e5a2017-10-25 02:10:39 +00002554 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002555 dump_cfg_string(sPermitTunnel, s);
2556
Damien Miller91475862011-05-05 14:14:34 +10002557 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2558 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11002559
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002560 printf("rekeylimit %llu %d\n", (unsigned long long)o->rekey_limit,
Damien Millera6d6c1f2013-08-21 02:40:01 +10002561 o->rekey_interval);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002562
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002563 printf("permitopen");
2564 if (o->num_permitted_opens == 0)
2565 printf(" any");
2566 else {
2567 for (i = 0; i < o->num_permitted_opens; i++)
2568 printf(" %s", o->permitted_opens[i]);
2569 }
2570 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002571}