blob: b75faf3f8bbfe7064d7774ad2cb8be3d0f51dce0 [file] [log] [blame]
djm@openbsd.org868109b2015-07-01 02:39:06 +00001
djm@openbsd.org115063a2018-06-06 18:22:41 +00002/* $OpenBSD: servconf.c,v 1.329 2018/06/06 18:22:41 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003/*
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
Damien Millere3b60b52006-07-10 21:08:03 +100016#include <sys/types.h>
17#include <sys/socket.h>
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000018#ifdef HAVE_SYS_SYSCTL_H
19#include <sys/sysctl.h>
20#endif
Damien Millere3b60b52006-07-10 21:08:03 +100021
Damien Miller0dac6fb2010-11-20 15:19:38 +110022#include <netinet/in.h>
23#include <netinet/in_systm.h>
24#include <netinet/ip.h>
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000025#ifdef HAVE_NET_ROUTE_H
26#include <net/route.h>
27#endif
Damien Miller0dac6fb2010-11-20 15:19:38 +110028
Darren Tucker5f96f3b2013-05-16 20:29:28 +100029#include <ctype.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100030#include <netdb.h>
Damien Miller565ca3f2006-08-19 00:23:15 +100031#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100032#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100033#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100034#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100035#include <signal.h>
Damien Millere6b3b612006-07-24 14:01:23 +100036#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000037#include <limits.h>
Damien Millerd7834352006-08-05 12:39:39 +100038#include <stdarg.h>
Darren Tuckere7140f22008-06-10 23:01:51 +100039#include <errno.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100040#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100041#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100042#endif
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;
djm@openbsd.org115063a2018-06-06 18:22:41 +0000163 options->permitted_remote_opens = NULL;
Damien Millere2754432006-07-24 14:06:47 +1000164 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100165 options->chroot_directory = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +1100166 options->authorized_keys_command = NULL;
167 options->authorized_keys_command_user = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100168 options->revoked_keys_file = NULL;
169 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000170 options->authorized_principals_file = NULL;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000171 options->authorized_principals_command = NULL;
172 options->authorized_principals_command_user = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100173 options->ip_qos_interactive = -1;
174 options->ip_qos_bulk = -1;
Damien Miller23528812012-04-22 11:24:43 +1000175 options->version_addendum = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000176 options->fingerprint_hash = -1;
djm@openbsd.org7844f352016-11-30 03:00:05 +0000177 options->disable_forwarding = -1;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000178 options->expose_userauth_info = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179}
180
djm@openbsd.org161cf412014-12-22 07:55:51 +0000181/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
182static int
183option_clear_or_none(const char *o)
184{
185 return o == NULL || strcasecmp(o, "none") == 0;
186}
187
djm@openbsd.orged085102015-10-29 08:05:01 +0000188static void
189assemble_algorithms(ServerOptions *o)
190{
191 if (kex_assemble_names(KEX_SERVER_ENCRYPT, &o->ciphers) != 0 ||
192 kex_assemble_names(KEX_SERVER_MAC, &o->macs) != 0 ||
193 kex_assemble_names(KEX_SERVER_KEX, &o->kex_algorithms) != 0 ||
194 kex_assemble_names(KEX_DEFAULT_PK_ALG,
195 &o->hostkeyalgorithms) != 0 ||
196 kex_assemble_names(KEX_DEFAULT_PK_ALG,
197 &o->hostbased_key_types) != 0 ||
198 kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->pubkey_key_types) != 0)
199 fatal("kex_assemble_names failed");
200}
201
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000202static void
203array_append(const char *file, const int line, const char *directive,
204 char ***array, u_int *lp, const char *s)
205{
206
207 if (*lp >= INT_MAX)
208 fatal("%s line %d: Too many %s entries", file, line, directive);
209
210 *array = xrecallocarray(*array, *lp, *lp + 1, sizeof(**array));
211 (*array)[*lp] = xstrdup(s);
212 (*lp)++;
213}
214
215void
216servconf_add_hostkey(const char *file, const int line,
217 ServerOptions *options, const char *path)
218{
219 char *apath = derelativise_path(path);
220
221 array_append(file, line, "HostKey",
222 &options->host_key_files, &options->num_host_key_files, apath);
223 free(apath);
224}
225
226void
227servconf_add_hostcert(const char *file, const int line,
228 ServerOptions *options, const char *path)
229{
230 char *apath = derelativise_path(path);
231
232 array_append(file, line, "HostCertificate",
233 &options->host_cert_files, &options->num_host_cert_files, apath);
234 free(apath);
235}
236
Damien Miller4af51302000-04-16 11:18:38 +1000237void
Damien Miller95def091999-11-25 00:26:21 +1100238fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239{
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000240 u_int i;
djm@openbsd.org161cf412014-12-22 07:55:51 +0000241
Damien Miller726273e2001-11-12 11:40:11 +1100242 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000243 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000244 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100245
246 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100247 if (options->num_host_key_files == 0) {
248 /* fill default hostkeys for protocols */
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000249 servconf_add_hostkey("[default]", 0, options,
250 _PATH_HOST_RSA_KEY_FILE);
Damien Millerdd190dd2010-11-11 14:17:02 +1100251#ifdef OPENSSL_HAS_ECC
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000252 servconf_add_hostkey("[default]", 0, options,
253 _PATH_HOST_ECDSA_KEY_FILE);
Damien Millerdd190dd2010-11-11 14:17:02 +1100254#endif
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000255 servconf_add_hostkey("[default]", 0, options,
256 _PATH_HOST_ED25519_KEY_FILE);
markus@openbsd.org5886b922018-03-01 20:32:16 +0000257#ifdef WITH_XMSS
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000258 servconf_add_hostkey("[default]", 0, options,
259 _PATH_HOST_XMSS_KEY_FILE);
markus@openbsd.org5886b922018-03-01 20:32:16 +0000260#endif /* WITH_XMSS */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100261 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100262 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100263 if (options->num_ports == 0)
264 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000265 if (options->address_family == -1)
266 options->address_family = AF_UNSPEC;
Damien Miller34132e52000-01-14 15:45:46 +1100267 if (options->listen_addrs == NULL)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000268 add_listen_addr(options, NULL, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000269 if (options->pid_file == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000270 options->pid_file = xstrdup(_PATH_SSH_DAEMON_PID_FILE);
Damien Miller95def091999-11-25 00:26:21 +1100271 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000272 options->login_grace_time = 120;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000273 if (options->permit_root_login == PERMIT_NOT_SET)
chris@openbsd.org3d5728a2015-07-31 15:38:09 +0000274 options->permit_root_login = PERMIT_NO_PASSWD;
Damien Miller95def091999-11-25 00:26:21 +1100275 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100276 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100277 if (options->ignore_user_known_hosts == -1)
278 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100279 if (options->print_motd == -1)
280 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000281 if (options->print_lastlog == -1)
282 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100283 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100284 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100285 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100286 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100287 if (options->x11_use_localhost == -1)
288 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000289 if (options->xauth_location == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000290 options->xauth_location = xstrdup(_PATH_XAUTH);
Damien Miller5ff30c62013-10-30 22:21:50 +1100291 if (options->permit_tty == -1)
292 options->permit_tty = 1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000293 if (options->permit_user_rc == -1)
294 options->permit_user_rc = 1;
Damien Miller95def091999-11-25 00:26:21 +1100295 if (options->strict_modes == -1)
296 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100297 if (options->tcp_keep_alive == -1)
298 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100299 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100300 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100301 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000302 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000303 if (options->hostbased_authentication == -1)
304 options->hostbased_authentication = 0;
305 if (options->hostbased_uses_name_from_packet_only == -1)
306 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100307 if (options->pubkey_authentication == -1)
308 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100309 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000310 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100311 if (options->kerberos_or_local_passwd == -1)
312 options->kerberos_or_local_passwd = 1;
313 if (options->kerberos_ticket_cleanup == -1)
314 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100315 if (options->kerberos_get_afs_token == -1)
316 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000317 if (options->gss_authentication == -1)
318 options->gss_authentication = 0;
319 if (options->gss_cleanup_creds == -1)
320 options->gss_cleanup_creds = 1;
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000321 if (options->gss_strict_acceptor == -1)
djm@openbsd.org13bd2e22017-01-06 03:45:41 +0000322 options->gss_strict_acceptor = 1;
Damien Miller95def091999-11-25 00:26:21 +1100323 if (options->password_authentication == -1)
324 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100325 if (options->kbd_interactive_authentication == -1)
326 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000327 if (options->challenge_response_authentication == -1)
328 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100329 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100330 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000331 if (options->permit_user_env == -1)
332 options->permit_user_env = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000333 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000334 options->compression = COMP_DELAYED;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000335 if (options->rekey_limit == -1)
336 options->rekey_limit = 0;
337 if (options->rekey_interval == -1)
338 options->rekey_interval = 0;
Damien Miller50a41ed2000-10-16 12:14:42 +1100339 if (options->allow_tcp_forwarding == -1)
Damien Milleraa5b3f82012-12-03 09:50:54 +1100340 options->allow_tcp_forwarding = FORWARD_ALLOW;
Damien Miller7acefbb2014-07-18 14:11:24 +1000341 if (options->allow_streamlocal_forwarding == -1)
342 options->allow_streamlocal_forwarding = FORWARD_ALLOW;
Damien Miller4f755cd2008-05-19 14:57:41 +1000343 if (options->allow_agent_forwarding == -1)
344 options->allow_agent_forwarding = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000345 if (options->fwd_opts.gateway_ports == -1)
346 options->fwd_opts.gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000347 if (options->max_startups == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100348 options->max_startups = 100;
Damien Miller942da032000-08-18 13:59:06 +1000349 if (options->max_startups_rate == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100350 options->max_startups_rate = 30; /* 30% */
Damien Miller942da032000-08-18 13:59:06 +1000351 if (options->max_startups_begin == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100352 options->max_startups_begin = 10;
Darren Tucker89413db2004-05-24 10:36:23 +1000353 if (options->max_authtries == -1)
354 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000355 if (options->max_sessions == -1)
356 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000357 if (options->use_dns == -1)
deraadt@openbsd.org3cd51032015-02-02 01:57:44 +0000358 options->use_dns = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000359 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100360 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000361 if (options->client_alive_count_max == -1)
362 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000363 if (options->num_authkeys_files == 0) {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000364 array_append("[default]", 0, "AuthorizedKeysFiles",
365 &options->authorized_keys_files,
366 &options->num_authkeys_files,
367 _PATH_SSH_USER_PERMITTED_KEYS);
368 array_append("[default]", 0, "AuthorizedKeysFiles",
369 &options->authorized_keys_files,
370 &options->num_authkeys_files,
371 _PATH_SSH_USER_PERMITTED_KEYS2);
Damien Millerd8478b62011-05-29 21:39:36 +1000372 }
Damien Millerd27b9472005-12-13 19:29:02 +1100373 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100374 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100375 if (options->ip_qos_interactive == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +0000376 options->ip_qos_interactive = IPTOS_DSCP_AF21;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100377 if (options->ip_qos_bulk == -1)
job@openbsd.org5ee84482018-04-04 15:12:17 +0000378 options->ip_qos_bulk = IPTOS_DSCP_CS1;
Damien Miller23528812012-04-22 11:24:43 +1000379 if (options->version_addendum == NULL)
380 options->version_addendum = xstrdup("");
Damien Miller7acefbb2014-07-18 14:11:24 +1000381 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
382 options->fwd_opts.streamlocal_bind_mask = 0177;
383 if (options->fwd_opts.streamlocal_bind_unlink == -1)
384 options->fwd_opts.streamlocal_bind_unlink = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000385 if (options->fingerprint_hash == -1)
386 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org7844f352016-11-30 03:00:05 +0000387 if (options->disable_forwarding == -1)
388 options->disable_forwarding = 0;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000389 if (options->expose_userauth_info == -1)
390 options->expose_userauth_info = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000391
djm@openbsd.orged085102015-10-29 08:05:01 +0000392 assemble_algorithms(options);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000393
djm@openbsd.orgc5c3f322016-02-17 05:29:04 +0000394 /* Turn privilege separation and sandboxing on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000395 if (use_privsep == -1)
djm@openbsd.orgc5c3f322016-02-17 05:29:04 +0000396 use_privsep = PRIVSEP_ON;
Damien Miller4903eb42002-06-21 16:20:44 +1000397
djm@openbsd.org161cf412014-12-22 07:55:51 +0000398#define CLEAR_ON_NONE(v) \
399 do { \
400 if (option_clear_or_none(v)) { \
401 free(v); \
402 v = NULL; \
403 } \
404 } while(0)
405 CLEAR_ON_NONE(options->pid_file);
406 CLEAR_ON_NONE(options->xauth_location);
407 CLEAR_ON_NONE(options->banner);
408 CLEAR_ON_NONE(options->trusted_user_ca_keys);
409 CLEAR_ON_NONE(options->revoked_keys_file);
djm@openbsd.org7e8528c2015-05-01 04:17:51 +0000410 CLEAR_ON_NONE(options->authorized_principals_file);
djm@openbsd.org9fd04682015-11-13 04:38:06 +0000411 CLEAR_ON_NONE(options->adm_forced_command);
412 CLEAR_ON_NONE(options->chroot_directory);
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000413 CLEAR_ON_NONE(options->routing_domain);
djm@openbsd.org161cf412014-12-22 07:55:51 +0000414 for (i = 0; i < options->num_host_key_files; i++)
415 CLEAR_ON_NONE(options->host_key_files[i]);
416 for (i = 0; i < options->num_host_cert_files; i++)
417 CLEAR_ON_NONE(options->host_cert_files[i]);
418#undef CLEAR_ON_NONE
419
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +0000420 /* Similar handling for AuthenticationMethods=any */
421 if (options->num_auth_methods == 1 &&
422 strcmp(options->auth_methods[0], "any") == 0) {
423 free(options->auth_methods[0]);
424 options->auth_methods[0] = NULL;
425 options->num_auth_methods = 0;
426 }
427
Tim Rice40017b02002-07-14 13:36:49 -0700428#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000429 if (use_privsep && options->compression == 1) {
430 error("This platform does not support both privilege "
431 "separation and compression");
432 error("Compression disabled");
433 options->compression = 0;
434 }
435#endif
436
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437}
438
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100440typedef enum {
441 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100442 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000443 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100444 /* Standard Options */
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000445 sPort, sHostKeyFile, sLoginGraceTime,
446 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000447 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100448 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
dtucker@openbsd.org745771f2018-02-09 02:37:36 +0000449 sKerberosGetAFSToken, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100450 sPasswordAuthentication, sKbdInteractiveAuthentication,
451 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000452 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100453 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller5ff30c62013-10-30 22:21:50 +1100454 sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
djm@openbsd.org83b58182016-08-19 03:18:06 +0000455 sPermitUserEnvironment, sAllowTcpForwarding, sCompression,
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000456 sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000457 sIgnoreUserKnownHosts, sCiphers, sMacs, sPidFile,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000458 sGatewayPorts, sPubkeyAuthentication, sPubkeyAcceptedKeyTypes,
459 sXAuthLocation, sSubsystem, sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000460 sBanner, sUseDNS, sHostbasedAuthentication,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000461 sHostbasedUsesNameFromPacketOnly, sHostbasedAcceptedKeyTypes,
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000462 sHostKeyAlgorithms,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000463 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000464 sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
465 sAcceptEnv, sPermitTunnel,
djm@openbsd.org115063a2018-06-06 18:22:41 +0000466 sMatch, sPermitOpen, sPermitRemoteOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100467 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller7cc194f2014-02-04 11:12:56 +1100468 sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000469 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000470 sAuthorizedPrincipalsCommand, sAuthorizedPrincipalsCommandUser,
Damien Miller23528812012-04-22 11:24:43 +1000471 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Miller09d3e122012-10-31 08:58:58 +1100472 sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
Damien Miller72e6b5c2014-07-04 09:00:04 +1000473 sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
Damien Miller7acefbb2014-07-18 14:11:24 +1000474 sStreamLocalBindMask, sStreamLocalBindUnlink,
djm@openbsd.org7844f352016-11-30 03:00:05 +0000475 sAllowStreamLocalForwarding, sFingerprintHash, sDisableForwarding,
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000476 sExposeAuthInfo, sRDomain,
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000477 sDeprecated, sIgnore, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478} ServerOpCodes;
479
Darren Tucker45150472006-07-12 22:34:17 +1000480#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
481#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
482#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
483
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100485static struct {
486 const char *name;
487 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000488 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100489} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100490 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000491#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000492 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000493#else
Darren Tucker45150472006-07-12 22:34:17 +1000494 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000495#endif
Darren Tucker45150472006-07-12 22:34:17 +1000496 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100497 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000498 { "port", sPort, SSHCFG_GLOBAL },
499 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
500 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
Damien Miller85b45e02013-07-20 13:21:52 +1000501 { "hostkeyagent", sHostKeyAgent, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000502 { "pidfile", sPidFile, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000503 { "serverkeybits", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000504 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000505 { "keyregenerationinterval", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100506 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000507 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
djm@openbsd.org54cd41a2017-05-17 01:24:17 +0000508 { "loglevel", sLogLevel, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000509 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000510 { "rhostsrsaauthentication", sDeprecated, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100511 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000512 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000513 { "hostbasedacceptedkeytypes", sHostbasedAcceptedKeyTypes, SSHCFG_ALL },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000514 { "hostkeyalgorithms", sHostKeyAlgorithms, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000515 { "rsaauthentication", sDeprecated, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100516 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000517 { "pubkeyacceptedkeytypes", sPubkeyAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000518 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000519#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100520 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000521 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
522 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100523#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000524 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000525#else
Darren Tucker45150472006-07-12 22:34:17 +1000526 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100527#endif
528#else
Darren Tucker1629c072007-02-19 22:25:37 +1100529 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000530 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
531 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
532 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000533#endif
Darren Tucker45150472006-07-12 22:34:17 +1000534 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
535 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000536#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100537 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000538 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000539 { "gssapistrictacceptorcheck", sGssStrictAcceptor, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000540#else
Darren Tucker1629c072007-02-19 22:25:37 +1100541 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000542 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000543 { "gssapistrictacceptorcheck", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000544#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100545 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
546 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100547 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000548 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
549 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
550 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
551 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
552 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
Damien Millerac908c12015-10-22 09:35:24 +1100553#ifdef DISABLE_LASTLOG
554 { "printlastlog", sUnsupported, SSHCFG_GLOBAL },
555#else
Darren Tucker45150472006-07-12 22:34:17 +1000556 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
Damien Millerac908c12015-10-22 09:35:24 +1100557#endif
Darren Tucker45150472006-07-12 22:34:17 +1000558 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
559 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000560 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
561 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
562 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000563 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
564 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100565 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000566 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
djm@openbsd.org83b58182016-08-19 03:18:06 +0000567 { "uselogin", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000568 { "compression", sCompression, SSHCFG_GLOBAL },
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000569 { "rekeylimit", sRekeyLimit, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000570 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
571 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
572 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000573 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Damien Millerc24da772012-06-20 21:53:58 +1000574 { "allowusers", sAllowUsers, SSHCFG_ALL },
575 { "denyusers", sDenyUsers, SSHCFG_ALL },
576 { "allowgroups", sAllowGroups, SSHCFG_ALL },
577 { "denygroups", sDenyGroups, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000578 { "ciphers", sCiphers, SSHCFG_GLOBAL },
579 { "macs", sMacs, SSHCFG_GLOBAL },
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000580 { "protocol", sIgnore, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000581 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
582 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
583 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000584 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000585 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100586 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000587 { "usedns", sUseDNS, SSHCFG_GLOBAL },
588 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
589 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
markus@openbsd.orgf0ddede2016-11-23 23:14:15 +0000590 { "clientaliveinterval", sClientAliveInterval, SSHCFG_ALL },
591 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000592 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000593 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
djm@openbsd.org66705942017-03-14 07:19:07 +0000594 { "useprivilegeseparation", sDeprecated, SSHCFG_GLOBAL},
Damien Millerc24da772012-06-20 21:53:58 +1000595 { "acceptenv", sAcceptEnv, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000596 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Damien Miller5ff30c62013-10-30 22:21:50 +1100597 { "permittty", sPermitTTY, SSHCFG_ALL },
Damien Miller72e6b5c2014-07-04 09:00:04 +1000598 { "permituserrc", sPermitUserRC, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000599 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000600 { "permitopen", sPermitOpen, SSHCFG_ALL },
djm@openbsd.org115063a2018-06-06 18:22:41 +0000601 { "permitremoteopen", sPermitRemoteOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000602 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100603 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100604 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100605 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
606 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000607 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000608 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100609 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller09d3e122012-10-31 08:58:58 +1100610 { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
611 { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000612 { "authorizedprincipalscommand", sAuthorizedPrincipalsCommand, SSHCFG_ALL },
613 { "authorizedprincipalscommanduser", sAuthorizedPrincipalsCommandUser, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000614 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Damien Millera6e3f012012-11-04 23:21:40 +1100615 { "authenticationmethods", sAuthenticationMethods, SSHCFG_ALL },
Damien Miller7acefbb2014-07-18 14:11:24 +1000616 { "streamlocalbindmask", sStreamLocalBindMask, SSHCFG_ALL },
617 { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
618 { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000619 { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
djm@openbsd.org7844f352016-11-30 03:00:05 +0000620 { "disableforwarding", sDisableForwarding, SSHCFG_ALL },
djm@openbsd.org8f574952017-06-24 06:34:38 +0000621 { "exposeauthinfo", sExposeAuthInfo, SSHCFG_ALL },
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000622 { "rdomain", sRDomain, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000623 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624};
625
Darren Tuckere7140f22008-06-10 23:01:51 +1000626static struct {
627 int val;
628 char *text;
629} tunmode_desc[] = {
630 { SSH_TUNMODE_NO, "no" },
631 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
632 { SSH_TUNMODE_ETHERNET, "ethernet" },
633 { SSH_TUNMODE_YES, "yes" },
634 { -1, NULL }
635};
636
djm@openbsd.org115063a2018-06-06 18:22:41 +0000637/* Returns an opcode name from its number */
638
639static const char *
640lookup_opcode_name(ServerOpCodes code)
641{
642 u_int i;
643
644 for (i = 0; keywords[i].name != NULL; i++)
645 if (keywords[i].opcode == code)
646 return(keywords[i].name);
647 return "UNKNOWN";
648}
649
650
Damien Miller5428f641999-11-25 11:54:57 +1100651/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000652 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100653 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000654
Damien Miller4af51302000-04-16 11:18:38 +1000655static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100656parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000657 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000658{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000659 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000660
Damien Miller95def091999-11-25 00:26:21 +1100661 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000662 if (strcasecmp(cp, keywords[i].name) == 0) {
663 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100664 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000665 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000666
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000667 error("%s: line %d: Bad configuration option: %s",
668 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100669 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000670}
671
Darren Tucker88b6fb22010-01-13 22:44:29 +1100672char *
673derelativise_path(const char *path)
674{
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000675 char *expanded, *ret, cwd[PATH_MAX];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100676
djm@openbsd.org161cf412014-12-22 07:55:51 +0000677 if (strcasecmp(path, "none") == 0)
678 return xstrdup("none");
Darren Tucker88b6fb22010-01-13 22:44:29 +1100679 expanded = tilde_expand_filename(path, getuid());
680 if (*expanded == '/')
681 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100682 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100683 fatal("%s: getcwd: %s", __func__, strerror(errno));
684 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tuckera627d422013-06-02 07:31:17 +1000685 free(expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100686 return ret;
687}
688
Ben Lindstrombba81212001-06-25 05:01:22 +0000689static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000690add_listen_addr(ServerOptions *options, const char *addr,
691 const char *rdomain, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100692{
Damien Millereccb9de2005-06-17 12:59:34 +1000693 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100694
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000695 if (port > 0)
696 add_one_listen_addr(options, addr, rdomain, port);
697 else {
698 for (i = 0; i < options->num_ports; i++) {
699 add_one_listen_addr(options, addr, rdomain,
700 options->ports[i]);
701 }
702 }
Ben Lindstromc510af42001-04-07 17:25:48 +0000703}
704
Ben Lindstrombba81212001-06-25 05:01:22 +0000705static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000706add_one_listen_addr(ServerOptions *options, const char *addr,
707 const char *rdomain, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000708{
709 struct addrinfo hints, *ai, *aitop;
710 char strport[NI_MAXSERV];
711 int gaierr;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000712 u_int i;
713
714 /* Find listen_addrs entry for this rdomain */
715 for (i = 0; i < options->num_listen_addrs; i++) {
716 if (rdomain == NULL && options->listen_addrs[i].rdomain == NULL)
717 break;
718 if (rdomain == NULL || options->listen_addrs[i].rdomain == NULL)
719 continue;
720 if (strcmp(rdomain, options->listen_addrs[i].rdomain) == 0)
721 break;
722 }
723 if (i >= options->num_listen_addrs) {
724 /* No entry for this rdomain; allocate one */
725 if (i >= INT_MAX)
726 fatal("%s: too many listen addresses", __func__);
727 options->listen_addrs = xrecallocarray(options->listen_addrs,
728 options->num_listen_addrs, options->num_listen_addrs + 1,
729 sizeof(*options->listen_addrs));
730 i = options->num_listen_addrs++;
731 if (rdomain != NULL)
732 options->listen_addrs[i].rdomain = xstrdup(rdomain);
733 }
734 /* options->listen_addrs[i] points to the addresses for this rdomain */
Ben Lindstromc510af42001-04-07 17:25:48 +0000735
736 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100737 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000738 hints.ai_socktype = SOCK_STREAM;
739 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100740 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000741 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
742 fatal("bad addr or host: %s (%s)",
743 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100744 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000745 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
746 ;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000747 ai->ai_next = options->listen_addrs[i].addrs;
748 options->listen_addrs[i].addrs = aitop;
749}
750
751/* Returns nonzero if the routing domain name is valid */
752static int
753valid_rdomain(const char *name)
754{
Damien Miller43c29bb2017-10-25 13:10:59 +1100755#if defined(HAVE_SYS_VALID_RDOMAIN)
Damien Miller2de5c6b2017-10-27 08:42:33 +1100756 return sys_valid_rdomain(name);
Damien Miller43c29bb2017-10-25 13:10:59 +1100757#elif defined(__OpenBSD__)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000758 const char *errstr;
759 long long num;
760 struct rt_tableinfo info;
761 int mib[6];
762 size_t miblen = sizeof(mib);
763
764 if (name == NULL)
765 return 1;
766
767 num = strtonum(name, 0, 255, &errstr);
768 if (errstr != NULL)
769 return 0;
770
771 /* Check whether the table actually exists */
772 memset(mib, 0, sizeof(mib));
773 mib[0] = CTL_NET;
774 mib[1] = PF_ROUTE;
775 mib[4] = NET_RT_TABLE;
776 mib[5] = (int)num;
777 if (sysctl(mib, 6, &info, &miblen, NULL, 0) == -1)
778 return 0;
779
780 return 1;
Damien Miller43c29bb2017-10-25 13:10:59 +1100781#else /* defined(__OpenBSD__) */
782 error("Routing domains are not supported on this platform");
783 return 0;
784#endif
Damien Miller34132e52000-01-14 15:45:46 +1100785}
786
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000787/*
788 * Queue a ListenAddress to be processed once we have all of the Ports
789 * and AddressFamily options.
790 */
791static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000792queue_listen_addr(ServerOptions *options, const char *addr,
793 const char *rdomain, int port)
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000794{
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000795 struct queued_listenaddr *qla;
796
797 options->queued_listen_addrs = xrecallocarray(
798 options->queued_listen_addrs,
799 options->num_queued_listens, options->num_queued_listens + 1,
800 sizeof(*options->queued_listen_addrs));
801 qla = &options->queued_listen_addrs[options->num_queued_listens++];
802 qla->addr = xstrdup(addr);
803 qla->port = port;
804 qla->rdomain = rdomain == NULL ? NULL : xstrdup(rdomain);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000805}
806
807/*
808 * Process queued (text) ListenAddress entries.
809 */
810static void
811process_queued_listen_addrs(ServerOptions *options)
812{
813 u_int i;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000814 struct queued_listenaddr *qla;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000815
816 if (options->num_ports == 0)
817 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
818 if (options->address_family == -1)
819 options->address_family = AF_UNSPEC;
820
821 for (i = 0; i < options->num_queued_listens; i++) {
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000822 qla = &options->queued_listen_addrs[i];
823 add_listen_addr(options, qla->addr, qla->rdomain, qla->port);
824 free(qla->addr);
825 free(qla->rdomain);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000826 }
827 free(options->queued_listen_addrs);
828 options->queued_listen_addrs = NULL;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000829 options->num_queued_listens = 0;
830}
831
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000832/*
djm@openbsd.org115063a2018-06-06 18:22:41 +0000833 * Inform channels layer of permitopen options for a single forwarding
834 * direction (local/remote).
835 */
836static void
837process_permitopen_list(struct ssh *ssh, ServerOpCodes opcode,
838 char **opens, u_int num_opens)
839{
840 u_int i;
841 int port;
842 char *host, *arg, *oarg;
843 int where = opcode == sPermitOpen ? FORWARD_LOCAL : FORWARD_REMOTE;
844 const char *what = lookup_opcode_name(opcode);
845
846 channel_clear_permission(ssh, FORWARD_ADM, where);
847 if (num_opens == 0)
848 return; /* permit any */
849
850 /* handle keywords: "any" / "none" */
851 if (num_opens == 1 && strcmp(opens[0], "any") == 0)
852 return;
853 if (num_opens == 1 && strcmp(opens[0], "none") == 0) {
854 channel_disable_admin(ssh, where);
855 return;
856 }
857 /* Otherwise treat it as a list of permitted host:port */
858 for (i = 0; i < num_opens; i++) {
859 oarg = arg = xstrdup(opens[i]);
860 host = hpdelim(&arg);
861 if (host == NULL)
862 fatal("%s: missing host in %s", __func__, what);
863 host = cleanhostname(host);
864 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
865 fatal("%s: bad port number in %s", __func__, what);
866 /* Send it to channels layer */
867 channel_add_permission(ssh, FORWARD_ADM,
868 where, host, port);
869 free(oarg);
870 }
871}
872
873/*
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000874 * Inform channels layer of permitopen options from configuration.
875 */
876void
877process_permitopen(struct ssh *ssh, ServerOptions *options)
878{
djm@openbsd.org115063a2018-06-06 18:22:41 +0000879 process_permitopen_list(ssh, sPermitOpen,
880 options->permitted_opens, options->num_permitted_opens);
881 process_permitopen_list(ssh, sPermitRemoteOpen,
882 options->permitted_remote_opens,
883 options->num_permitted_remote_opens);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000884}
885
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000886struct connection_info *
887get_connection_info(int populate, int use_dns)
888{
djm@openbsd.org95767262016-03-07 19:02:43 +0000889 struct ssh *ssh = active_state; /* XXX */
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000890 static struct connection_info ci;
891
892 if (!populate)
893 return &ci;
djm@openbsd.org95767262016-03-07 19:02:43 +0000894 ci.host = auth_get_canonical_hostname(ssh, use_dns);
895 ci.address = ssh_remote_ipaddr(ssh);
896 ci.laddress = ssh_local_ipaddr(ssh);
897 ci.lport = ssh_local_port(ssh);
djm@openbsd.org68af80e2017-10-25 00:19:47 +0000898 ci.rdomain = ssh_packet_rdomain_in(ssh);
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000899 return &ci;
900}
901
Darren Tucker45150472006-07-12 22:34:17 +1000902/*
903 * The strategy for the Match blocks is that the config file is parsed twice.
904 *
905 * The first time is at startup. activep is initialized to 1 and the
906 * directives in the global context are processed and acted on. Hitting a
907 * Match directive unsets activep and the directives inside the block are
908 * checked for syntax only.
909 *
910 * The second time is after a connection has been established but before
911 * authentication. activep is initialized to 2 and global config directives
912 * are ignored since they have already been processed. If the criteria in a
913 * Match block is met, activep is set and the subsequent directives
914 * processed and actioned until EOF or another Match block unsets it. Any
915 * options set are copied into the main server config.
916 *
917 * Potential additions/improvements:
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000918 * - Add Match support for pre-kex directives, eg. Ciphers.
Darren Tucker45150472006-07-12 22:34:17 +1000919 *
920 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
921 * Match Address 192.168.0.*
922 * Tag trusted
923 * Match Group wheel
924 * Tag trusted
925 * Match Tag trusted
926 * AllowTcpForwarding yes
927 * GatewayPorts clientspecified
928 * [...]
929 *
930 * - Add a PermittedChannelRequests directive
931 * Match Group shell
932 * PermittedChannelRequests session,forwarded-tcpip
933 */
934
935static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000936match_cfg_line_group(const char *grps, int line, const char *user)
937{
938 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000939 struct passwd *pw;
940
Damien Miller565ca3f2006-08-19 00:23:15 +1000941 if (user == NULL)
942 goto out;
943
944 if ((pw = getpwnam(user)) == NULL) {
945 debug("Can't match group at line %d because user %.100s does "
946 "not exist", line, user);
947 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
948 debug("Can't Match group because user %.100s not in any group "
949 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000950 } else if (ga_match_pattern_list(grps) != 1) {
951 debug("user %.100s does not match group list %.100s at line %d",
952 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000953 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000954 debug("user %.100s matched group list %.100s at line %d", user,
955 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000956 result = 1;
957 }
958out:
959 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000960 return result;
961}
962
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000963static void
964match_test_missing_fatal(const char *criteria, const char *attrib)
965{
966 fatal("'Match %s' in configuration but '%s' not in connection "
967 "test specification.", criteria, attrib);
968}
969
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000970/*
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000971 * All of the attributes on a single Match line are ANDed together, so we need
Damien Miller03bf2e62013-10-24 21:01:26 +1100972 * to check every attribute and set the result to zero if any attribute does
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000973 * not match.
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000974 */
Damien Miller565ca3f2006-08-19 00:23:15 +1000975static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000976match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +1000977{
Damien Millercf31f382013-10-24 21:02:56 +1100978 int result = 1, attributes = 0, port;
Darren Tucker45150472006-07-12 22:34:17 +1000979 char *arg, *attrib, *cp = *condition;
Darren Tucker45150472006-07-12 22:34:17 +1000980
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000981 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000982 debug3("checking syntax for 'Match %s'", cp);
983 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000984 debug3("checking match for '%s' user %s host %s addr %s "
985 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
986 ci->host ? ci->host : "(null)",
987 ci->address ? ci->address : "(null)",
988 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +1000989
990 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100991 attributes++;
992 if (strcasecmp(attrib, "all") == 0) {
993 if (attributes != 1 ||
994 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
995 error("'all' cannot be combined with other "
996 "Match attributes");
997 return -1;
998 }
999 *condition = cp;
1000 return 1;
1001 }
Darren Tucker45150472006-07-12 22:34:17 +10001002 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
1003 error("Missing Match criteria for %s", attrib);
1004 return -1;
1005 }
Darren Tucker45150472006-07-12 22:34:17 +10001006 if (strcasecmp(attrib, "user") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001007 if (ci == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +10001008 result = 0;
1009 continue;
1010 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001011 if (ci->user == NULL)
1012 match_test_missing_fatal("User", "user");
djm@openbsd.orge661a862015-05-04 06:10:48 +00001013 if (match_pattern_list(ci->user, arg, 0) != 1)
Darren Tucker45150472006-07-12 22:34:17 +10001014 result = 0;
1015 else
1016 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001017 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +10001018 } else if (strcasecmp(attrib, "group") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001019 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001020 result = 0;
1021 continue;
1022 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001023 if (ci->user == NULL)
1024 match_test_missing_fatal("Group", "user");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001025 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +10001026 case -1:
1027 return -1;
1028 case 0:
1029 result = 0;
1030 }
Darren Tucker45150472006-07-12 22:34:17 +10001031 } else if (strcasecmp(attrib, "host") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001032 if (ci == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +10001033 result = 0;
1034 continue;
1035 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001036 if (ci->host == NULL)
1037 match_test_missing_fatal("Host", "host");
djm@openbsd.orge661a862015-05-04 06:10:48 +00001038 if (match_hostname(ci->host, arg) != 1)
Darren Tucker45150472006-07-12 22:34:17 +10001039 result = 0;
1040 else
1041 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001042 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +10001043 } else if (strcasecmp(attrib, "address") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001044 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001045 result = 0;
1046 continue;
1047 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001048 if (ci->address == NULL)
1049 match_test_missing_fatal("Address", "addr");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001050 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +10001051 case 1:
Darren Tucker45150472006-07-12 22:34:17 +10001052 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001053 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +10001054 break;
1055 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +10001056 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +10001057 result = 0;
1058 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +10001059 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +10001060 return -1;
1061 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001062 } else if (strcasecmp(attrib, "localaddress") == 0){
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001063 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001064 result = 0;
1065 continue;
1066 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001067 if (ci->laddress == NULL)
1068 match_test_missing_fatal("LocalAddress",
1069 "laddr");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001070 switch (addr_match_list(ci->laddress, arg)) {
1071 case 1:
1072 debug("connection from %.100s matched "
1073 "'LocalAddress %.100s' at line %d",
1074 ci->laddress, arg, line);
1075 break;
1076 case 0:
1077 case -1:
1078 result = 0;
1079 break;
1080 case -2:
1081 return -1;
1082 }
1083 } else if (strcasecmp(attrib, "localport") == 0) {
1084 if ((port = a2port(arg)) == -1) {
1085 error("Invalid LocalPort '%s' on Match line",
1086 arg);
1087 return -1;
1088 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001089 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001090 result = 0;
1091 continue;
1092 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001093 if (ci->lport == 0)
1094 match_test_missing_fatal("LocalPort", "lport");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001095 /* TODO support port lists */
1096 if (port == ci->lport)
1097 debug("connection from %.100s matched "
1098 "'LocalPort %d' at line %d",
1099 ci->laddress, port, line);
1100 else
1101 result = 0;
djm@openbsd.org68af80e2017-10-25 00:19:47 +00001102 } else if (strcasecmp(attrib, "rdomain") == 0) {
1103 if (ci == NULL || ci->rdomain == NULL) {
1104 result = 0;
1105 continue;
1106 }
1107 if (match_pattern_list(ci->rdomain, arg, 0) != 1)
1108 result = 0;
1109 else
1110 debug("user %.100s matched 'RDomain %.100s' at "
1111 "line %d", ci->rdomain, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +10001112 } else {
1113 error("Unsupported Match attribute %s", attrib);
1114 return -1;
1115 }
1116 }
Damien Millercf31f382013-10-24 21:02:56 +11001117 if (attributes == 0) {
1118 error("One or more attributes required for Match");
1119 return -1;
1120 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001121 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +10001122 debug3("match %sfound", result ? "" : "not ");
1123 *condition = cp;
1124 return result;
1125}
1126
Damien Millere2754432006-07-24 14:06:47 +10001127#define WHITESPACE " \t\r\n"
1128
Damien Miller33322122011-06-20 14:43:11 +10001129/* Multistate option parsing */
1130struct multistate {
1131 char *key;
1132 int value;
1133};
djm@openbsd.org@openbsd.org33edb6e2017-11-03 05:18:44 +00001134static const struct multistate multistate_flag[] = {
1135 { "yes", 1 },
1136 { "no", 0 },
1137 { NULL, -1 }
1138};
Damien Miller33322122011-06-20 14:43:11 +10001139static const struct multistate multistate_addressfamily[] = {
1140 { "inet", AF_INET },
1141 { "inet6", AF_INET6 },
1142 { "any", AF_UNSPEC },
1143 { NULL, -1 }
1144};
1145static const struct multistate multistate_permitrootlogin[] = {
1146 { "without-password", PERMIT_NO_PASSWD },
deraadt@openbsd.org1dc8d932015-08-06 14:53:21 +00001147 { "prohibit-password", PERMIT_NO_PASSWD },
Damien Miller33322122011-06-20 14:43:11 +10001148 { "forced-commands-only", PERMIT_FORCED_ONLY },
1149 { "yes", PERMIT_YES },
1150 { "no", PERMIT_NO },
1151 { NULL, -1 }
1152};
1153static const struct multistate multistate_compression[] = {
djm@openbsd.org0082fba2016-09-28 16:33:06 +00001154 { "yes", COMP_DELAYED },
djm@openbsd.org4577ade2016-09-28 20:32:42 +00001155 { "delayed", COMP_DELAYED },
Damien Miller33322122011-06-20 14:43:11 +10001156 { "no", COMP_NONE },
1157 { NULL, -1 }
1158};
1159static const struct multistate multistate_gatewayports[] = {
1160 { "clientspecified", 2 },
1161 { "yes", 1 },
1162 { "no", 0 },
1163 { NULL, -1 }
1164};
Damien Milleraa5b3f82012-12-03 09:50:54 +11001165static const struct multistate multistate_tcpfwd[] = {
1166 { "yes", FORWARD_ALLOW },
1167 { "all", FORWARD_ALLOW },
1168 { "no", FORWARD_DENY },
1169 { "remote", FORWARD_REMOTE },
1170 { "local", FORWARD_LOCAL },
1171 { NULL, -1 }
1172};
Damien Miller33322122011-06-20 14:43:11 +10001173
Ben Lindstromade03f62001-12-06 18:22:17 +00001174int
1175process_server_config_line(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001176 const char *filename, int linenum, int *activep,
1177 struct connection_info *connectinfo)
Ben Lindstromade03f62001-12-06 18:22:17 +00001178{
djm@openbsd.org115063a2018-06-06 18:22:41 +00001179 char *cp, ***chararrayptr, **charptr, *arg, *arg2, *p;
Darren Tucker9113d0c2013-05-16 20:48:14 +10001180 int cmdline = 0, *intptr, value, value2, n, port;
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001181 SyslogFacility *log_facility_ptr;
1182 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +00001183 ServerOpCodes opcode;
djm@openbsd.org115063a2018-06-06 18:22:41 +00001184 u_int i, *uintptr, uvalue, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +10001185 size_t len;
Darren Tucker9113d0c2013-05-16 20:48:14 +10001186 long long val64;
Damien Miller33322122011-06-20 14:43:11 +10001187 const struct multistate *multistate_ptr;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001188 const char *errstr;
Ben Lindstromade03f62001-12-06 18:22:17 +00001189
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +00001190 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
1191 if ((len = strlen(line)) == 0)
1192 return 0;
1193 for (len--; len > 0; len--) {
1194 if (strchr(WHITESPACE "\f", line[len]) == NULL)
1195 break;
1196 line[len] = '\0';
1197 }
1198
Ben Lindstromade03f62001-12-06 18:22:17 +00001199 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +11001200 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +11001201 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +00001202 /* Ignore leading whitespace */
1203 if (*arg == '\0')
1204 arg = strdelim(&cp);
1205 if (!arg || !*arg || *arg == '#')
1206 return 0;
1207 intptr = NULL;
1208 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +10001209 opcode = parse_token(arg, filename, linenum, &flags);
1210
1211 if (activep == NULL) { /* We are processing a command line directive */
1212 cmdline = 1;
1213 activep = &cmdline;
1214 }
1215 if (*activep && opcode != sMatch)
1216 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
1217 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001218 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +10001219 fatal("%s line %d: Directive '%s' is not allowed "
1220 "within a Match block", filename, linenum, arg);
1221 } else { /* this is a directive we have already processed */
1222 while (arg)
1223 arg = strdelim(&cp);
1224 return 0;
1225 }
1226 }
1227
Ben Lindstromade03f62001-12-06 18:22:17 +00001228 switch (opcode) {
1229 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +10001230 case sUsePAM:
1231 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +00001232 goto parse_flag;
1233
1234 /* Standard Options */
1235 case sBadOption:
1236 return -1;
1237 case sPort:
1238 /* ignore ports from configfile if cmdline specifies ports */
1239 if (options->ports_from_cmdline)
1240 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +00001241 if (options->num_ports >= MAX_PORTS)
1242 fatal("%s line %d: too many ports.",
1243 filename, linenum);
1244 arg = strdelim(&cp);
1245 if (!arg || *arg == '\0')
1246 fatal("%s line %d: missing port number.",
1247 filename, linenum);
1248 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +11001249 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +00001250 fatal("%s line %d: Badly formatted port number.",
1251 filename, linenum);
1252 break;
1253
Ben Lindstromade03f62001-12-06 18:22:17 +00001254 case sLoginGraceTime:
1255 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +10001256 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +00001257 arg = strdelim(&cp);
1258 if (!arg || *arg == '\0')
1259 fatal("%s line %d: missing time value.",
1260 filename, linenum);
1261 if ((value = convtime(arg)) == -1)
1262 fatal("%s line %d: invalid time value.",
1263 filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001264 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001265 *intptr = value;
1266 break;
1267
Ben Lindstromade03f62001-12-06 18:22:17 +00001268 case sListenAddress:
1269 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001270 if (arg == NULL || *arg == '\0')
1271 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +00001272 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +10001273 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
1274 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
1275 && strchr(p+1, ':') != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001276 port = 0;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001277 p = arg;
1278 } else {
1279 p = hpdelim(&arg);
1280 if (p == NULL)
1281 fatal("%s line %d: bad address:port usage",
1282 filename, linenum);
1283 p = cleanhostname(p);
1284 if (arg == NULL)
1285 port = 0;
1286 else if ((port = a2port(arg)) <= 0)
1287 fatal("%s line %d: bad port number",
1288 filename, linenum);
1289 }
1290 /* Optional routing table */
1291 arg2 = NULL;
1292 if ((arg = strdelim(&cp)) != NULL) {
1293 if (strcmp(arg, "rdomain") != 0 ||
1294 (arg2 = strdelim(&cp)) == NULL)
1295 fatal("%s line %d: bad ListenAddress syntax",
1296 filename, linenum);
1297 if (!valid_rdomain(arg2))
1298 fatal("%s line %d: bad routing domain",
1299 filename, linenum);
1300 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001301
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001302 queue_listen_addr(options, p, arg2, port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001303
Ben Lindstromade03f62001-12-06 18:22:17 +00001304 break;
1305
Darren Tucker0f383232005-01-20 10:57:56 +11001306 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +10001307 intptr = &options->address_family;
1308 multistate_ptr = multistate_addressfamily;
Damien Miller33322122011-06-20 14:43:11 +10001309 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +11001310 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +10001311 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +10001312 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +10001313 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +10001314 value = -1;
1315 for (i = 0; multistate_ptr[i].key != NULL; i++) {
1316 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
1317 value = multistate_ptr[i].value;
1318 break;
1319 }
1320 }
1321 if (value == -1)
1322 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +11001323 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +10001324 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +11001325 *intptr = value;
1326 break;
1327
Ben Lindstromade03f62001-12-06 18:22:17 +00001328 case sHostKeyFile:
Ben Lindstromade03f62001-12-06 18:22:17 +00001329 arg = strdelim(&cp);
1330 if (!arg || *arg == '\0')
1331 fatal("%s line %d: missing file name.",
1332 filename, linenum);
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001333 if (*activep)
1334 servconf_add_hostkey(filename, linenum, options, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001335 break;
1336
Damien Miller85b45e02013-07-20 13:21:52 +10001337 case sHostKeyAgent:
1338 charptr = &options->host_key_agent;
1339 arg = strdelim(&cp);
1340 if (!arg || *arg == '\0')
1341 fatal("%s line %d: missing socket name.",
1342 filename, linenum);
1343 if (*activep && *charptr == NULL)
1344 *charptr = !strcmp(arg, SSH_AUTHSOCKET_ENV_NAME) ?
1345 xstrdup(arg) : derelativise_path(arg);
1346 break;
1347
Damien Miller0a80ca12010-02-27 07:55:05 +11001348 case sHostCertificate:
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001349 arg = strdelim(&cp);
1350 if (!arg || *arg == '\0')
1351 fatal("%s line %d: missing file name.",
1352 filename, linenum);
1353 if (*activep)
1354 servconf_add_hostcert(filename, linenum, options, arg);
1355 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001356
Ben Lindstromade03f62001-12-06 18:22:17 +00001357 case sPidFile:
1358 charptr = &options->pid_file;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001359 parse_filename:
1360 arg = strdelim(&cp);
1361 if (!arg || *arg == '\0')
1362 fatal("%s line %d: missing file name.",
1363 filename, linenum);
1364 if (*activep && *charptr == NULL) {
1365 *charptr = derelativise_path(arg);
1366 /* increase optional counter */
1367 if (intptr != NULL)
1368 *intptr = *intptr + 1;
1369 }
1370 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001371
1372 case sPermitRootLogin:
1373 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +10001374 multistate_ptr = multistate_permitrootlogin;
1375 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001376
1377 case sIgnoreRhosts:
1378 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +10001379 parse_flag:
djm@openbsd.org@openbsd.org33edb6e2017-11-03 05:18:44 +00001380 multistate_ptr = multistate_flag;
1381 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001382
1383 case sIgnoreUserKnownHosts:
1384 intptr = &options->ignore_user_known_hosts;
1385 goto parse_flag;
1386
Ben Lindstromade03f62001-12-06 18:22:17 +00001387 case sHostbasedAuthentication:
1388 intptr = &options->hostbased_authentication;
1389 goto parse_flag;
1390
1391 case sHostbasedUsesNameFromPacketOnly:
1392 intptr = &options->hostbased_uses_name_from_packet_only;
1393 goto parse_flag;
1394
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001395 case sHostbasedAcceptedKeyTypes:
1396 charptr = &options->hostbased_key_types;
1397 parse_keytypes:
1398 arg = strdelim(&cp);
1399 if (!arg || *arg == '\0')
1400 fatal("%s line %d: Missing argument.",
1401 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001402 if (*arg != '-' &&
1403 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001404 fatal("%s line %d: Bad key types '%s'.",
1405 filename, linenum, arg ? arg : "<NONE>");
1406 if (*activep && *charptr == NULL)
1407 *charptr = xstrdup(arg);
1408 break;
1409
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001410 case sHostKeyAlgorithms:
1411 charptr = &options->hostkeyalgorithms;
1412 goto parse_keytypes;
1413
Ben Lindstromade03f62001-12-06 18:22:17 +00001414 case sPubkeyAuthentication:
1415 intptr = &options->pubkey_authentication;
1416 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001417
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001418 case sPubkeyAcceptedKeyTypes:
1419 charptr = &options->pubkey_key_types;
1420 goto parse_keytypes;
1421
Ben Lindstromade03f62001-12-06 18:22:17 +00001422 case sKerberosAuthentication:
1423 intptr = &options->kerberos_authentication;
1424 goto parse_flag;
1425
1426 case sKerberosOrLocalPasswd:
1427 intptr = &options->kerberos_or_local_passwd;
1428 goto parse_flag;
1429
1430 case sKerberosTicketCleanup:
1431 intptr = &options->kerberos_ticket_cleanup;
1432 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001433
Darren Tucker22ef5082003-12-31 11:37:34 +11001434 case sKerberosGetAFSToken:
1435 intptr = &options->kerberos_get_afs_token;
1436 goto parse_flag;
1437
Darren Tucker0efd1552003-08-26 11:49:55 +10001438 case sGssAuthentication:
1439 intptr = &options->gss_authentication;
1440 goto parse_flag;
1441
1442 case sGssCleanupCreds:
1443 intptr = &options->gss_cleanup_creds;
1444 goto parse_flag;
1445
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +00001446 case sGssStrictAcceptor:
1447 intptr = &options->gss_strict_acceptor;
1448 goto parse_flag;
1449
Ben Lindstromade03f62001-12-06 18:22:17 +00001450 case sPasswordAuthentication:
1451 intptr = &options->password_authentication;
1452 goto parse_flag;
1453
1454 case sKbdInteractiveAuthentication:
1455 intptr = &options->kbd_interactive_authentication;
1456 goto parse_flag;
1457
1458 case sChallengeResponseAuthentication:
1459 intptr = &options->challenge_response_authentication;
1460 goto parse_flag;
1461
1462 case sPrintMotd:
1463 intptr = &options->print_motd;
1464 goto parse_flag;
1465
1466 case sPrintLastLog:
1467 intptr = &options->print_lastlog;
1468 goto parse_flag;
1469
1470 case sX11Forwarding:
1471 intptr = &options->x11_forwarding;
1472 goto parse_flag;
1473
1474 case sX11DisplayOffset:
1475 intptr = &options->x11_display_offset;
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +00001476 parse_int:
1477 arg = strdelim(&cp);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001478 if ((errstr = atoi_err(arg, &value)) != NULL)
1479 fatal("%s line %d: integer value %s.",
1480 filename, linenum, errstr);
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +00001481 if (*activep && *intptr == -1)
1482 *intptr = value;
1483 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001484
Damien Miller95c249f2002-02-05 12:11:34 +11001485 case sX11UseLocalhost:
1486 intptr = &options->x11_use_localhost;
1487 goto parse_flag;
1488
Ben Lindstromade03f62001-12-06 18:22:17 +00001489 case sXAuthLocation:
1490 charptr = &options->xauth_location;
1491 goto parse_filename;
1492
Damien Miller5ff30c62013-10-30 22:21:50 +11001493 case sPermitTTY:
1494 intptr = &options->permit_tty;
1495 goto parse_flag;
1496
Damien Miller72e6b5c2014-07-04 09:00:04 +10001497 case sPermitUserRC:
1498 intptr = &options->permit_user_rc;
1499 goto parse_flag;
1500
Ben Lindstromade03f62001-12-06 18:22:17 +00001501 case sStrictModes:
1502 intptr = &options->strict_modes;
1503 goto parse_flag;
1504
Damien Miller12c150e2003-12-17 16:31:10 +11001505 case sTCPKeepAlive:
1506 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001507 goto parse_flag;
1508
1509 case sEmptyPasswd:
1510 intptr = &options->permit_empty_passwd;
1511 goto parse_flag;
1512
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001513 case sPermitUserEnvironment:
1514 intptr = &options->permit_user_env;
1515 goto parse_flag;
1516
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001517 case sCompression:
1518 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001519 multistate_ptr = multistate_compression;
1520 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001521
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001522 case sRekeyLimit:
1523 arg = strdelim(&cp);
1524 if (!arg || *arg == '\0')
1525 fatal("%.200s line %d: Missing argument.", filename,
1526 linenum);
1527 if (strcmp(arg, "default") == 0) {
1528 val64 = 0;
1529 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001530 if (scan_scaled(arg, &val64) == -1)
1531 fatal("%.200s line %d: Bad number '%s': %s",
1532 filename, linenum, arg, strerror(errno));
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001533 if (val64 != 0 && val64 < 16)
1534 fatal("%.200s line %d: RekeyLimit too small",
1535 filename, linenum);
1536 }
1537 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001538 options->rekey_limit = val64;
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001539 if (cp != NULL) { /* optional rekey interval present */
1540 if (strcmp(cp, "none") == 0) {
1541 (void)strdelim(&cp); /* discard */
1542 break;
1543 }
1544 intptr = &options->rekey_interval;
1545 goto parse_time;
1546 }
1547 break;
1548
Ben Lindstromade03f62001-12-06 18:22:17 +00001549 case sGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +10001550 intptr = &options->fwd_opts.gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001551 multistate_ptr = multistate_gatewayports;
1552 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001553
Damien Miller3a961dc2003-06-03 10:25:48 +10001554 case sUseDNS:
1555 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001556 goto parse_flag;
1557
1558 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001559 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001560 arg = strdelim(&cp);
1561 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001562 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001563 fatal("%.200s line %d: unsupported log facility '%s'",
1564 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001565 if (*log_facility_ptr == -1)
1566 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001567 break;
1568
1569 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001570 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001571 arg = strdelim(&cp);
1572 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001573 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001574 fatal("%.200s line %d: unsupported log level '%s'",
1575 filename, linenum, arg ? arg : "<NONE>");
djm@openbsd.org54cd41a2017-05-17 01:24:17 +00001576 if (*activep && *log_level_ptr == -1)
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001577 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001578 break;
1579
1580 case sAllowTcpForwarding:
1581 intptr = &options->allow_tcp_forwarding;
Damien Milleraa5b3f82012-12-03 09:50:54 +11001582 multistate_ptr = multistate_tcpfwd;
1583 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001584
Damien Miller7acefbb2014-07-18 14:11:24 +10001585 case sAllowStreamLocalForwarding:
1586 intptr = &options->allow_streamlocal_forwarding;
1587 multistate_ptr = multistate_tcpfwd;
1588 goto parse_multistate;
1589
Damien Miller4f755cd2008-05-19 14:57:41 +10001590 case sAllowAgentForwarding:
1591 intptr = &options->allow_agent_forwarding;
1592 goto parse_flag;
1593
djm@openbsd.org7844f352016-11-30 03:00:05 +00001594 case sDisableForwarding:
1595 intptr = &options->disable_forwarding;
1596 goto parse_flag;
1597
Ben Lindstromade03f62001-12-06 18:22:17 +00001598 case sAllowUsers:
1599 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.org010359b2016-11-06 05:46:37 +00001600 if (match_user(NULL, NULL, NULL, arg) == -1)
1601 fatal("%s line %d: invalid AllowUsers pattern: "
1602 "\"%.100s\"", filename, linenum, arg);
Damien Millerc24da772012-06-20 21:53:58 +10001603 if (!*activep)
1604 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001605 array_append(filename, linenum, "AllowUsers",
1606 &options->allow_users, &options->num_allow_users,
1607 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001608 }
1609 break;
1610
1611 case sDenyUsers:
1612 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.org010359b2016-11-06 05:46:37 +00001613 if (match_user(NULL, NULL, NULL, arg) == -1)
1614 fatal("%s line %d: invalid DenyUsers pattern: "
1615 "\"%.100s\"", filename, linenum, arg);
Damien Millerc24da772012-06-20 21:53:58 +10001616 if (!*activep)
1617 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001618 array_append(filename, linenum, "DenyUsers",
1619 &options->deny_users, &options->num_deny_users,
1620 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001621 }
1622 break;
1623
1624 case sAllowGroups:
1625 while ((arg = strdelim(&cp)) && *arg != '\0') {
Damien Millerc24da772012-06-20 21:53:58 +10001626 if (!*activep)
1627 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001628 array_append(filename, linenum, "AllowGroups",
1629 &options->allow_groups, &options->num_allow_groups,
1630 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001631 }
1632 break;
1633
1634 case sDenyGroups:
1635 while ((arg = strdelim(&cp)) && *arg != '\0') {
Damien Millerc24da772012-06-20 21:53:58 +10001636 if (!*activep)
1637 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001638 array_append(filename, linenum, "DenyGroups",
1639 &options->deny_groups, &options->num_deny_groups,
1640 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001641 }
1642 break;
1643
1644 case sCiphers:
1645 arg = strdelim(&cp);
1646 if (!arg || *arg == '\0')
1647 fatal("%s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001648 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstromade03f62001-12-06 18:22:17 +00001649 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1650 filename, linenum, arg ? arg : "<NONE>");
1651 if (options->ciphers == NULL)
1652 options->ciphers = xstrdup(arg);
1653 break;
1654
1655 case sMacs:
1656 arg = strdelim(&cp);
1657 if (!arg || *arg == '\0')
1658 fatal("%s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001659 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstromade03f62001-12-06 18:22:17 +00001660 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1661 filename, linenum, arg ? arg : "<NONE>");
1662 if (options->macs == NULL)
1663 options->macs = xstrdup(arg);
1664 break;
1665
Damien Millerd5f62bf2010-09-24 22:11:14 +10001666 case sKexAlgorithms:
1667 arg = strdelim(&cp);
1668 if (!arg || *arg == '\0')
1669 fatal("%s line %d: Missing argument.",
1670 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001671 if (*arg != '-' &&
1672 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001673 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1674 filename, linenum, arg ? arg : "<NONE>");
1675 if (options->kex_algorithms == NULL)
1676 options->kex_algorithms = xstrdup(arg);
1677 break;
1678
Ben Lindstromade03f62001-12-06 18:22:17 +00001679 case sSubsystem:
1680 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1681 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001682 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001683 }
1684 arg = strdelim(&cp);
1685 if (!arg || *arg == '\0')
1686 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001687 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001688 if (!*activep) {
1689 arg = strdelim(&cp);
1690 break;
1691 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001692 for (i = 0; i < options->num_subsystems; i++)
1693 if (strcmp(arg, options->subsystem_name[i]) == 0)
1694 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001695 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001696 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1697 arg = strdelim(&cp);
1698 if (!arg || *arg == '\0')
1699 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001700 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001701 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001702
1703 /* Collect arguments (separate to executable) */
1704 p = xstrdup(arg);
1705 len = strlen(p) + 1;
1706 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1707 len += 1 + strlen(arg);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001708 p = xreallocarray(p, 1, len);
Damien Miller917f9b62006-07-10 20:36:47 +10001709 strlcat(p, " ", len);
1710 strlcat(p, arg, len);
1711 }
1712 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001713 options->num_subsystems++;
1714 break;
1715
1716 case sMaxStartups:
1717 arg = strdelim(&cp);
1718 if (!arg || *arg == '\0')
1719 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001720 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001721 if ((n = sscanf(arg, "%d:%d:%d",
1722 &options->max_startups_begin,
1723 &options->max_startups_rate,
1724 &options->max_startups)) == 3) {
1725 if (options->max_startups_begin >
1726 options->max_startups ||
1727 options->max_startups_rate > 100 ||
1728 options->max_startups_rate < 1)
1729 fatal("%s line %d: Illegal MaxStartups spec.",
1730 filename, linenum);
1731 } else if (n != 1)
1732 fatal("%s line %d: Illegal MaxStartups spec.",
1733 filename, linenum);
1734 else
1735 options->max_startups = options->max_startups_begin;
1736 break;
1737
Darren Tucker89413db2004-05-24 10:36:23 +10001738 case sMaxAuthTries:
1739 intptr = &options->max_authtries;
1740 goto parse_int;
1741
Damien Miller7207f642008-05-19 15:34:50 +10001742 case sMaxSessions:
1743 intptr = &options->max_sessions;
1744 goto parse_int;
1745
Ben Lindstromade03f62001-12-06 18:22:17 +00001746 case sBanner:
1747 charptr = &options->banner;
1748 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001749
Ben Lindstromade03f62001-12-06 18:22:17 +00001750 /*
1751 * These options can contain %X options expanded at
1752 * connect time, so that you can specify paths like:
1753 *
1754 * AuthorizedKeysFile /etc/ssh_keys/%u
1755 */
1756 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001757 if (*activep && options->num_authkeys_files == 0) {
1758 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001759 arg = tilde_expand_filename(arg, getuid());
1760 array_append(filename, linenum,
1761 "AuthorizedKeysFile",
1762 &options->authorized_keys_files,
1763 &options->num_authkeys_files, arg);
1764 free(arg);
Damien Millerd8478b62011-05-29 21:39:36 +10001765 }
1766 }
1767 return 0;
1768
Damien Miller30da3442010-05-10 11:58:03 +10001769 case sAuthorizedPrincipalsFile:
1770 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001771 arg = strdelim(&cp);
1772 if (!arg || *arg == '\0')
1773 fatal("%s line %d: missing file name.",
1774 filename, linenum);
1775 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001776 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001777 /* increase optional counter */
1778 if (intptr != NULL)
1779 *intptr = *intptr + 1;
1780 }
1781 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001782
1783 case sClientAliveInterval:
1784 intptr = &options->client_alive_interval;
1785 goto parse_time;
1786
1787 case sClientAliveCountMax:
1788 intptr = &options->client_alive_count_max;
1789 goto parse_int;
1790
Darren Tucker46bc0752004-05-02 22:11:30 +10001791 case sAcceptEnv:
1792 while ((arg = strdelim(&cp)) && *arg != '\0') {
1793 if (strchr(arg, '=') != NULL)
1794 fatal("%s line %d: Invalid environment name.",
1795 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001796 if (!*activep)
Damien Millerc24da772012-06-20 21:53:58 +10001797 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001798 array_append(filename, linenum, "AcceptEnv",
1799 &options->accept_env, &options->num_accept_env,
1800 arg);
Darren Tucker46bc0752004-05-02 22:11:30 +10001801 }
1802 break;
1803
Damien Millerd27b9472005-12-13 19:29:02 +11001804 case sPermitTunnel:
1805 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001806 arg = strdelim(&cp);
1807 if (!arg || *arg == '\0')
1808 fatal("%s line %d: Missing yes/point-to-point/"
1809 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001810 value = -1;
1811 for (i = 0; tunmode_desc[i].val != -1; i++)
1812 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1813 value = tunmode_desc[i].val;
1814 break;
1815 }
1816 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001817 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1818 "no argument: %s", filename, linenum, arg);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001819 if (*activep && *intptr == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001820 *intptr = value;
1821 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001822
Darren Tucker45150472006-07-12 22:34:17 +10001823 case sMatch:
1824 if (cmdline)
1825 fatal("Match directive not supported as a command-line "
1826 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001827 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10001828 if (value < 0)
1829 fatal("%s line %d: Bad Match condition", filename,
1830 linenum);
1831 *activep = value;
1832 break;
1833
djm@openbsd.org115063a2018-06-06 18:22:41 +00001834 case sPermitRemoteOpen:
Damien Miller9b439df2006-07-24 14:04:00 +10001835 case sPermitOpen:
djm@openbsd.org115063a2018-06-06 18:22:41 +00001836 if (opcode == sPermitRemoteOpen) {
1837 uintptr = &options->num_permitted_remote_opens;
1838 chararrayptr = &options->permitted_remote_opens;
1839 } else {
1840 uintptr = &options->num_permitted_opens;
1841 chararrayptr = &options->permitted_opens;
1842 }
Damien Miller9b439df2006-07-24 14:04:00 +10001843 arg = strdelim(&cp);
1844 if (!arg || *arg == '\0')
djm@openbsd.org115063a2018-06-06 18:22:41 +00001845 fatal("%s line %d: missing %s specification",
1846 filename, linenum, lookup_opcode_name(opcode));
1847 uvalue = *uintptr; /* modified later */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001848 if (strcmp(arg, "any") == 0 || strcmp(arg, "none") == 0) {
djm@openbsd.org115063a2018-06-06 18:22:41 +00001849 if (*activep && uvalue == 0) {
1850 *uintptr = 1;
1851 *chararrayptr = xcalloc(1,
1852 sizeof(**chararrayptr));
1853 (*chararrayptr)[0] = xstrdup(arg);
dtucker@openbsd.org30484e52017-09-18 09:41:52 +00001854 }
Damien Millerc6081482012-04-22 11:18:53 +10001855 break;
1856 }
Damien Millera765cf42006-07-24 14:08:13 +10001857 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001858 arg2 = xstrdup(arg);
Damien Millera765cf42006-07-24 14:08:13 +10001859 p = hpdelim(&arg);
djm@openbsd.org115063a2018-06-06 18:22:41 +00001860 /* XXX support bare port number for PermitRemoteOpen */
1861 if (p == NULL) {
1862 fatal("%s line %d: missing host in %s",
1863 filename, linenum,
1864 lookup_opcode_name(opcode));
1865 }
Damien Millera765cf42006-07-24 14:08:13 +10001866 p = cleanhostname(p);
djm@openbsd.org115063a2018-06-06 18:22:41 +00001867 if (arg == NULL ||
1868 ((port = permitopen_port(arg)) < 0)) {
1869 fatal("%s line %d: bad port number in %s",
1870 filename, linenum,
1871 lookup_opcode_name(opcode));
1872 }
1873 if (*activep && uvalue == 0) {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001874 array_append(filename, linenum,
djm@openbsd.org115063a2018-06-06 18:22:41 +00001875 lookup_opcode_name(opcode),
1876 chararrayptr, uintptr, arg2);
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001877 }
1878 free(arg2);
Damien Millera765cf42006-07-24 14:08:13 +10001879 }
Damien Miller9b439df2006-07-24 14:04:00 +10001880 break;
1881
Damien Millere2754432006-07-24 14:06:47 +10001882 case sForceCommand:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001883 if (cp == NULL || *cp == '\0')
Damien Millere2754432006-07-24 14:06:47 +10001884 fatal("%.200s line %d: Missing argument.", filename,
1885 linenum);
1886 len = strspn(cp, WHITESPACE);
1887 if (*activep && options->adm_forced_command == NULL)
1888 options->adm_forced_command = xstrdup(cp + len);
1889 return 0;
1890
Damien Millerd8cb1f12008-02-10 22:40:12 +11001891 case sChrootDirectory:
1892 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001893
1894 arg = strdelim(&cp);
1895 if (!arg || *arg == '\0')
1896 fatal("%s line %d: missing file name.",
1897 filename, linenum);
1898 if (*activep && *charptr == NULL)
1899 *charptr = xstrdup(arg);
1900 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001901
Damien Miller1aed65e2010-03-04 21:53:35 +11001902 case sTrustedUserCAKeys:
1903 charptr = &options->trusted_user_ca_keys;
1904 goto parse_filename;
1905
1906 case sRevokedKeys:
1907 charptr = &options->revoked_keys_file;
1908 goto parse_filename;
1909
Damien Miller0dac6fb2010-11-20 15:19:38 +11001910 case sIPQoS:
1911 arg = strdelim(&cp);
1912 if ((value = parse_ipqos(arg)) == -1)
1913 fatal("%s line %d: Bad IPQoS value: %s",
1914 filename, linenum, arg);
1915 arg = strdelim(&cp);
1916 if (arg == NULL)
1917 value2 = value;
1918 else if ((value2 = parse_ipqos(arg)) == -1)
1919 fatal("%s line %d: Bad IPQoS value: %s",
1920 filename, linenum, arg);
1921 if (*activep) {
1922 options->ip_qos_interactive = value;
1923 options->ip_qos_bulk = value2;
1924 }
1925 break;
1926
Damien Miller23528812012-04-22 11:24:43 +10001927 case sVersionAddendum:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001928 if (cp == NULL || *cp == '\0')
Damien Miller23528812012-04-22 11:24:43 +10001929 fatal("%.200s line %d: Missing argument.", filename,
1930 linenum);
1931 len = strspn(cp, WHITESPACE);
1932 if (*activep && options->version_addendum == NULL) {
1933 if (strcasecmp(cp + len, "none") == 0)
1934 options->version_addendum = xstrdup("");
1935 else if (strchr(cp + len, '\r') != NULL)
1936 fatal("%.200s line %d: Invalid argument",
1937 filename, linenum);
1938 else
1939 options->version_addendum = xstrdup(cp + len);
1940 }
1941 return 0;
1942
Damien Miller09d3e122012-10-31 08:58:58 +11001943 case sAuthorizedKeysCommand:
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001944 if (cp == NULL)
1945 fatal("%.200s line %d: Missing argument.", filename,
1946 linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001947 len = strspn(cp, WHITESPACE);
1948 if (*activep && options->authorized_keys_command == NULL) {
1949 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1950 fatal("%.200s line %d: AuthorizedKeysCommand "
1951 "must be an absolute path",
1952 filename, linenum);
1953 options->authorized_keys_command = xstrdup(cp + len);
1954 }
1955 return 0;
1956
1957 case sAuthorizedKeysCommandUser:
1958 charptr = &options->authorized_keys_command_user;
1959
1960 arg = strdelim(&cp);
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001961 if (!arg || *arg == '\0')
1962 fatal("%s line %d: missing AuthorizedKeysCommandUser "
1963 "argument.", filename, linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001964 if (*activep && *charptr == NULL)
1965 *charptr = xstrdup(arg);
1966 break;
1967
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +00001968 case sAuthorizedPrincipalsCommand:
1969 if (cp == NULL)
1970 fatal("%.200s line %d: Missing argument.", filename,
1971 linenum);
1972 len = strspn(cp, WHITESPACE);
1973 if (*activep &&
1974 options->authorized_principals_command == NULL) {
1975 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1976 fatal("%.200s line %d: "
1977 "AuthorizedPrincipalsCommand must be "
1978 "an absolute path", filename, linenum);
1979 options->authorized_principals_command =
1980 xstrdup(cp + len);
1981 }
1982 return 0;
1983
1984 case sAuthorizedPrincipalsCommandUser:
1985 charptr = &options->authorized_principals_command_user;
1986
1987 arg = strdelim(&cp);
1988 if (!arg || *arg == '\0')
1989 fatal("%s line %d: missing "
1990 "AuthorizedPrincipalsCommandUser argument.",
1991 filename, linenum);
1992 if (*activep && *charptr == NULL)
1993 *charptr = xstrdup(arg);
1994 break;
1995
Damien Millera6e3f012012-11-04 23:21:40 +11001996 case sAuthenticationMethods:
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001997 if (options->num_auth_methods == 0) {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001998 value = 0; /* seen "any" pseudo-method */
djm@openbsd.org001aa552018-04-10 00:10:49 +00001999 value2 = 0; /* successfully parsed any method */
Damien Millera6e3f012012-11-04 23:21:40 +11002000 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002001 if (strcmp(arg, "any") == 0) {
2002 if (options->num_auth_methods > 0) {
2003 fatal("%s line %d: \"any\" "
2004 "must appear alone in "
2005 "AuthenticationMethods",
2006 filename, linenum);
2007 }
2008 value = 1;
2009 } else if (value) {
2010 fatal("%s line %d: \"any\" must appear "
2011 "alone in AuthenticationMethods",
2012 filename, linenum);
2013 } else if (auth2_methods_valid(arg, 0) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +11002014 fatal("%s line %d: invalid "
2015 "authentication method list.",
2016 filename, linenum);
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002017 }
djm@openbsd.org46ecd192016-06-23 05:17:51 +00002018 value2 = 1;
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00002019 if (!*activep)
2020 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00002021 array_append(filename, linenum,
2022 "AuthenticationMethods",
2023 &options->auth_methods,
2024 &options->num_auth_methods, arg);
Damien Millera6e3f012012-11-04 23:21:40 +11002025 }
djm@openbsd.org46ecd192016-06-23 05:17:51 +00002026 if (value2 == 0) {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002027 fatal("%s line %d: no AuthenticationMethods "
2028 "specified", filename, linenum);
2029 }
Damien Millera6e3f012012-11-04 23:21:40 +11002030 }
2031 return 0;
2032
Damien Miller7acefbb2014-07-18 14:11:24 +10002033 case sStreamLocalBindMask:
2034 arg = strdelim(&cp);
2035 if (!arg || *arg == '\0')
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00002036 fatal("%s line %d: missing StreamLocalBindMask "
2037 "argument.", filename, linenum);
Damien Miller7acefbb2014-07-18 14:11:24 +10002038 /* Parse mode in octal format */
2039 value = strtol(arg, &p, 8);
2040 if (arg == p || value < 0 || value > 0777)
2041 fatal("%s line %d: Bad mask.", filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00002042 if (*activep)
2043 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
Damien Miller7acefbb2014-07-18 14:11:24 +10002044 break;
2045
2046 case sStreamLocalBindUnlink:
2047 intptr = &options->fwd_opts.streamlocal_bind_unlink;
2048 goto parse_flag;
2049
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002050 case sFingerprintHash:
2051 arg = strdelim(&cp);
2052 if (!arg || *arg == '\0')
2053 fatal("%.200s line %d: Missing argument.",
2054 filename, linenum);
2055 if ((value = ssh_digest_alg_by_name(arg)) == -1)
2056 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
2057 filename, linenum, arg);
2058 if (*activep)
2059 options->fingerprint_hash = value;
2060 break;
2061
djm@openbsd.org8f574952017-06-24 06:34:38 +00002062 case sExposeAuthInfo:
2063 intptr = &options->expose_userauth_info;
2064 goto parse_flag;
2065
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002066 case sRDomain:
2067 charptr = &options->routing_domain;
2068 arg = strdelim(&cp);
2069 if (!arg || *arg == '\0')
2070 fatal("%.200s line %d: Missing argument.",
2071 filename, linenum);
2072 if (strcasecmp(arg, "none") != 0 && strcmp(arg, "%D") != 0 &&
2073 !valid_rdomain(arg))
2074 fatal("%s line %d: bad routing domain",
2075 filename, linenum);
2076 if (*activep && *charptr == NULL)
2077 *charptr = xstrdup(arg);
dtucker@openbsd.org168ecec2017-12-05 23:56:07 +00002078 break;
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002079
Ben Lindstromade03f62001-12-06 18:22:17 +00002080 case sDeprecated:
djm@openbsd.orgae363d72016-08-25 23:57:54 +00002081 case sIgnore:
Damien Millerf9b3feb2003-05-16 11:38:32 +10002082 case sUnsupported:
djm@openbsd.orgae363d72016-08-25 23:57:54 +00002083 do_log2(opcode == sIgnore ?
2084 SYSLOG_LEVEL_DEBUG2 : SYSLOG_LEVEL_INFO,
2085 "%s line %d: %s option %s", filename, linenum,
2086 opcode == sUnsupported ? "Unsupported" : "Deprecated", arg);
Damien Millerf9b3feb2003-05-16 11:38:32 +10002087 while (arg)
2088 arg = strdelim(&cp);
2089 break;
2090
Ben Lindstromade03f62001-12-06 18:22:17 +00002091 default:
2092 fatal("%s line %d: Missing handler for opcode %s (%d)",
2093 filename, linenum, arg, opcode);
2094 }
2095 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
2096 fatal("%s line %d: garbage at end of line; \"%.200s\".",
2097 filename, linenum, arg);
2098 return 0;
2099}
2100
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002101/* Reads the server configuration file. */
2102
Damien Miller4af51302000-04-16 11:18:38 +10002103void
Darren Tucker645ab752004-06-25 13:33:20 +10002104load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002105{
Damien Miller46cb75a2012-07-31 12:22:37 +10002106 char line[4096], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00002107 FILE *f;
Damien Miller46cb75a2012-07-31 12:22:37 +10002108 int lineno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002109
Darren Tucker645ab752004-06-25 13:33:20 +10002110 debug2("%s: filename %s", __func__, filename);
2111 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11002112 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002113 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11002114 }
Darren Tucker645ab752004-06-25 13:33:20 +10002115 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11002116 while (fgets(line, sizeof(line), f)) {
Damien Miller46cb75a2012-07-31 12:22:37 +10002117 lineno++;
2118 if (strlen(line) == sizeof(line) - 1)
2119 fatal("%s line %d too long", filename, lineno);
Darren Tucker645ab752004-06-25 13:33:20 +10002120 /*
2121 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10002122 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10002123 * line numbers later for error messages
2124 */
2125 if ((cp = strchr(line, '#')) != NULL)
2126 memcpy(cp, "\n", 2);
2127 cp = line + strspn(line, " \t\r");
2128
2129 buffer_append(conf, cp, strlen(cp));
2130 }
2131 buffer_append(conf, "\0", 1);
2132 fclose(f);
2133 debug2("%s: done config len = %d", __func__, buffer_len(conf));
2134}
2135
2136void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002137parse_server_match_config(ServerOptions *options,
2138 struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10002139{
Darren Tucker45150472006-07-12 22:34:17 +10002140 ServerOptions mo;
2141
2142 initialize_server_options(&mo);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002143 parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11002144 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10002145}
2146
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002147int parse_server_match_testspec(struct connection_info *ci, char *spec)
2148{
2149 char *p;
2150
2151 while ((p = strsep(&spec, ",")) && *p != '\0') {
2152 if (strncmp(p, "addr=", 5) == 0) {
2153 ci->address = xstrdup(p + 5);
2154 } else if (strncmp(p, "host=", 5) == 0) {
2155 ci->host = xstrdup(p + 5);
2156 } else if (strncmp(p, "user=", 5) == 0) {
2157 ci->user = xstrdup(p + 5);
2158 } else if (strncmp(p, "laddr=", 6) == 0) {
2159 ci->laddress = xstrdup(p + 6);
djm@openbsd.org68af80e2017-10-25 00:19:47 +00002160 } else if (strncmp(p, "rdomain=", 8) == 0) {
2161 ci->rdomain = xstrdup(p + 8);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002162 } else if (strncmp(p, "lport=", 6) == 0) {
2163 ci->lport = a2port(p + 6);
2164 if (ci->lport == -1) {
2165 fprintf(stderr, "Invalid port '%s' in test mode"
2166 " specification %s\n", p+6, p);
2167 return -1;
2168 }
2169 } else {
2170 fprintf(stderr, "Invalid test mode specification %s\n",
2171 p);
2172 return -1;
2173 }
2174 }
2175 return 0;
2176}
2177
2178/*
Darren Tucker1629c072007-02-19 22:25:37 +11002179 * Copy any supported values that are set.
2180 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10002181 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11002182 * array values that are not used pre-authentication, because any that we
djm@openbsd.org001aa552018-04-10 00:10:49 +00002183 * do use must be explicitly sent in mm_getpwnamallow().
Darren Tucker1629c072007-02-19 22:25:37 +11002184 */
Darren Tucker45150472006-07-12 22:34:17 +10002185void
Darren Tucker1629c072007-02-19 22:25:37 +11002186copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10002187{
Damien Miller534b2cc2013-12-05 14:07:27 +11002188#define M_CP_INTOPT(n) do {\
2189 if (src->n != -1) \
2190 dst->n = src->n; \
2191} while (0)
2192
Darren Tucker1629c072007-02-19 22:25:37 +11002193 M_CP_INTOPT(password_authentication);
2194 M_CP_INTOPT(gss_authentication);
Darren Tucker1629c072007-02-19 22:25:37 +11002195 M_CP_INTOPT(pubkey_authentication);
2196 M_CP_INTOPT(kerberos_authentication);
2197 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10002198 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11002199 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11002200 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11002201 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11002202
2203 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002204 M_CP_INTOPT(allow_streamlocal_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10002205 M_CP_INTOPT(allow_agent_forwarding);
djm@openbsd.org7844f352016-11-30 03:00:05 +00002206 M_CP_INTOPT(disable_forwarding);
djm@openbsd.org8f574952017-06-24 06:34:38 +00002207 M_CP_INTOPT(expose_userauth_info);
Damien Millerab6de352010-06-26 09:38:45 +10002208 M_CP_INTOPT(permit_tun);
Damien Miller7acefbb2014-07-18 14:11:24 +10002209 M_CP_INTOPT(fwd_opts.gateway_ports);
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002210 M_CP_INTOPT(fwd_opts.streamlocal_bind_unlink);
Darren Tucker1629c072007-02-19 22:25:37 +11002211 M_CP_INTOPT(x11_display_offset);
2212 M_CP_INTOPT(x11_forwarding);
2213 M_CP_INTOPT(x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002214 M_CP_INTOPT(permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002215 M_CP_INTOPT(permit_user_rc);
Damien Miller7207f642008-05-19 15:34:50 +10002216 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10002217 M_CP_INTOPT(max_authtries);
markus@openbsd.orgf0ddede2016-11-23 23:14:15 +00002218 M_CP_INTOPT(client_alive_count_max);
2219 M_CP_INTOPT(client_alive_interval);
Damien Miller0dac6fb2010-11-20 15:19:38 +11002220 M_CP_INTOPT(ip_qos_interactive);
2221 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002222 M_CP_INTOPT(rekey_limit);
2223 M_CP_INTOPT(rekey_interval);
djm@openbsd.org54cd41a2017-05-17 01:24:17 +00002224 M_CP_INTOPT(log_level);
Darren Tucker1629c072007-02-19 22:25:37 +11002225
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002226 /*
2227 * The bind_mask is a mode_t that may be unsigned, so we can't use
2228 * M_CP_INTOPT - it does a signed comparison that causes compiler
2229 * warnings.
2230 */
dtucker@openbsd.org9faae502016-05-04 14:00:09 +00002231 if (src->fwd_opts.streamlocal_bind_mask != (mode_t)-1) {
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002232 dst->fwd_opts.streamlocal_bind_mask =
2233 src->fwd_opts.streamlocal_bind_mask;
2234 }
2235
Damien Miller534b2cc2013-12-05 14:07:27 +11002236 /* M_CP_STROPT and M_CP_STRARRAYOPT should not appear before here */
2237#define M_CP_STROPT(n) do {\
2238 if (src->n != NULL && dst->n != src->n) { \
2239 free(dst->n); \
2240 dst->n = src->n; \
2241 } \
2242} while(0)
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00002243#define M_CP_STRARRAYOPT(s, num_s) do {\
2244 u_int i; \
2245 if (src->num_s != 0) { \
2246 for (i = 0; i < dst->num_s; i++) \
2247 free(dst->s[i]); \
2248 free(dst->s); \
2249 dst->s = xcalloc(src->num_s, sizeof(*dst->s)); \
2250 for (i = 0; i < src->num_s; i++) \
2251 dst->s[i] = xstrdup(src->s[i]); \
2252 dst->num_s = src->num_s; \
djm@openbsd.org66bf74a2017-10-02 19:33:20 +00002253 } \
2254} while(0)
Damien Miller534b2cc2013-12-05 14:07:27 +11002255
Damien Millerf2e407e2011-05-20 19:04:14 +10002256 /* See comment in servconf.h */
2257 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10002258
djm@openbsd.orged085102015-10-29 08:05:01 +00002259 /* Arguments that accept '+...' need to be expanded */
2260 assemble_algorithms(dst);
2261
Damien Millerc2411902011-05-20 19:03:49 +10002262 /*
2263 * The only things that should be below this point are string options
2264 * which are only used after authentication.
2265 */
Damien Miller5d74e582011-05-20 19:03:31 +10002266 if (preauth)
2267 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002268
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002269 /* These options may be "none" to clear a global setting */
Damien Miller5d74e582011-05-20 19:03:31 +10002270 M_CP_STROPT(adm_forced_command);
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002271 if (option_clear_or_none(dst->adm_forced_command)) {
2272 free(dst->adm_forced_command);
2273 dst->adm_forced_command = NULL;
2274 }
Damien Miller5d74e582011-05-20 19:03:31 +10002275 M_CP_STROPT(chroot_directory);
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002276 if (option_clear_or_none(dst->chroot_directory)) {
2277 free(dst->chroot_directory);
2278 dst->chroot_directory = NULL;
2279 }
Darren Tucker45150472006-07-12 22:34:17 +10002280}
2281
Darren Tucker1629c072007-02-19 22:25:37 +11002282#undef M_CP_INTOPT
2283#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10002284#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11002285
Darren Tucker45150472006-07-12 22:34:17 +10002286void
2287parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002288 struct connection_info *connectinfo)
Darren Tucker45150472006-07-12 22:34:17 +10002289{
2290 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10002291 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10002292
2293 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
2294
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002295 if ((obuf = cbuf = sshbuf_dup_string(conf)) == NULL)
2296 fatal("%s: sshbuf_dup_string failed", __func__);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002297 active = connectinfo ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10002298 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11002299 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10002300 if (process_server_config_line(options, cp, filename,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002301 linenum++, &active, connectinfo) != 0)
Damien Miller95def091999-11-25 00:26:21 +11002302 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002303 }
Darren Tuckera627d422013-06-02 07:31:17 +10002304 free(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00002305 if (bad_options > 0)
2306 fatal("%s: terminating, %d bad configuration options",
2307 filename, bad_options);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00002308 process_queued_listen_addrs(options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002309}
Darren Tuckere7140f22008-06-10 23:01:51 +10002310
2311static const char *
Damien Miller82c55872011-06-23 08:20:30 +10002312fmt_multistate_int(int val, const struct multistate *m)
2313{
2314 u_int i;
2315
2316 for (i = 0; m[i].key != NULL; i++) {
2317 if (m[i].value == val)
2318 return m[i].key;
2319 }
2320 return "UNKNOWN";
2321}
2322
2323static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10002324fmt_intarg(ServerOpCodes code, int val)
2325{
Damien Miller82c55872011-06-23 08:20:30 +10002326 if (val == -1)
2327 return "unset";
2328 switch (code) {
2329 case sAddressFamily:
2330 return fmt_multistate_int(val, multistate_addressfamily);
2331 case sPermitRootLogin:
2332 return fmt_multistate_int(val, multistate_permitrootlogin);
2333 case sGatewayPorts:
2334 return fmt_multistate_int(val, multistate_gatewayports);
2335 case sCompression:
2336 return fmt_multistate_int(val, multistate_compression);
Damien Milleraa5b3f82012-12-03 09:50:54 +11002337 case sAllowTcpForwarding:
2338 return fmt_multistate_int(val, multistate_tcpfwd);
Damien Miller7acefbb2014-07-18 14:11:24 +10002339 case sAllowStreamLocalForwarding:
2340 return fmt_multistate_int(val, multistate_tcpfwd);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002341 case sFingerprintHash:
2342 return ssh_digest_alg_name(val);
Damien Miller82c55872011-06-23 08:20:30 +10002343 default:
2344 switch (val) {
2345 case 0:
2346 return "no";
2347 case 1:
2348 return "yes";
2349 default:
2350 return "UNKNOWN";
2351 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002352 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002353}
2354
Darren Tuckere7140f22008-06-10 23:01:51 +10002355static void
2356dump_cfg_int(ServerOpCodes code, int val)
2357{
2358 printf("%s %d\n", lookup_opcode_name(code), val);
2359}
2360
2361static void
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002362dump_cfg_oct(ServerOpCodes code, int val)
2363{
2364 printf("%s 0%o\n", lookup_opcode_name(code), val);
2365}
2366
2367static void
Darren Tuckere7140f22008-06-10 23:01:51 +10002368dump_cfg_fmtint(ServerOpCodes code, int val)
2369{
2370 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2371}
2372
2373static void
2374dump_cfg_string(ServerOpCodes code, const char *val)
2375{
djm@openbsd.org161cf412014-12-22 07:55:51 +00002376 printf("%s %s\n", lookup_opcode_name(code),
2377 val == NULL ? "none" : val);
Darren Tuckere7140f22008-06-10 23:01:51 +10002378}
2379
2380static void
2381dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
2382{
2383 u_int i;
2384
2385 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10002386 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2387}
2388
2389static void
2390dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
2391{
2392 u_int i;
2393
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002394 if (count <= 0 && code != sAuthenticationMethods)
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002395 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002396 printf("%s", lookup_opcode_name(code));
2397 for (i = 0; i < count; i++)
2398 printf(" %s", vals[i]);
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002399 if (code == sAuthenticationMethods && count == 0)
2400 printf(" any");
Damien Millerd8478b62011-05-29 21:39:36 +10002401 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002402}
2403
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002404static char *
2405format_listen_addrs(struct listenaddr *la)
Darren Tuckere7140f22008-06-10 23:01:51 +10002406{
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002407 int r;
Darren Tuckere7140f22008-06-10 23:01:51 +10002408 struct addrinfo *ai;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002409 char addr[NI_MAXHOST], port[NI_MAXSERV];
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002410 char *laddr1 = xstrdup(""), *laddr2 = NULL;
Darren Tuckere7140f22008-06-10 23:01:51 +10002411
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002412 /*
2413 * ListenAddress must be after Port. add_one_listen_addr pushes
2414 * addresses onto a stack, so to maintain ordering we need to
2415 * print these in reverse order.
2416 */
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002417 for (ai = la->addrs; ai; ai = ai->ai_next) {
2418 if ((r = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
Darren Tuckere7140f22008-06-10 23:01:51 +10002419 sizeof(addr), port, sizeof(port),
2420 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002421 error("getnameinfo: %.100s", ssh_gai_strerror(r));
2422 continue;
Darren Tuckere7140f22008-06-10 23:01:51 +10002423 }
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002424 laddr2 = laddr1;
2425 if (ai->ai_family == AF_INET6) {
2426 xasprintf(&laddr1, "listenaddress [%s]:%s%s%s\n%s",
2427 addr, port,
2428 la->rdomain == NULL ? "" : " rdomain ",
2429 la->rdomain == NULL ? "" : la->rdomain,
2430 laddr2);
2431 } else {
2432 xasprintf(&laddr1, "listenaddress %s:%s%s%s\n%s",
2433 addr, port,
2434 la->rdomain == NULL ? "" : " rdomain ",
2435 la->rdomain == NULL ? "" : la->rdomain,
2436 laddr2);
2437 }
2438 free(laddr2);
Darren Tuckere7140f22008-06-10 23:01:51 +10002439 }
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002440 return laddr1;
2441}
2442
2443void
2444dump_config(ServerOptions *o)
2445{
2446 char *s;
2447 u_int i;
2448
2449 /* these are usually at the top of the config */
2450 for (i = 0; i < o->num_ports; i++)
2451 printf("port %d\n", o->ports[i]);
2452 dump_cfg_fmtint(sAddressFamily, o->address_family);
2453
2454 for (i = 0; i < o->num_listen_addrs; i++) {
2455 s = format_listen_addrs(&o->listen_addrs[i]);
2456 printf("%s", s);
2457 free(s);
2458 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002459
2460 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10002461#ifdef USE_PAM
Darren Tucker70860b62015-04-17 10:56:13 +10002462 dump_cfg_fmtint(sUsePAM, o->use_pam);
Damien Miller212f0b02008-07-23 17:42:29 +10002463#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002464 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
Darren Tuckere7140f22008-06-10 23:01:51 +10002465 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
2466 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11002467 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10002468 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
2469 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002470 dump_cfg_oct(sStreamLocalBindMask, o->fwd_opts.streamlocal_bind_mask);
Darren Tuckere7140f22008-06-10 23:01:51 +10002471
2472 /* formatted integer arguments */
2473 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
2474 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
2475 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
Darren Tuckere7140f22008-06-10 23:01:51 +10002476 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
2477 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
2478 o->hostbased_uses_name_from_packet_only);
Darren Tuckere7140f22008-06-10 23:01:51 +10002479 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10002480#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10002481 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
2482 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
2483 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10002484# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10002485 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10002486# endif
2487#endif
2488#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10002489 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2490 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10002491#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002492 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2493 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2494 o->kbd_interactive_authentication);
2495 dump_cfg_fmtint(sChallengeResponseAuthentication,
2496 o->challenge_response_authentication);
2497 dump_cfg_fmtint(sPrintMotd, o->print_motd);
Darren Tuckerfd4e4f22016-02-24 10:44:25 +11002498#ifndef DISABLE_LASTLOG
Darren Tuckere7140f22008-06-10 23:01:51 +10002499 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
Darren Tuckerfd4e4f22016-02-24 10:44:25 +11002500#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002501 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
2502 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002503 dump_cfg_fmtint(sPermitTTY, o->permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002504 dump_cfg_fmtint(sPermitUserRC, o->permit_user_rc);
Darren Tuckere7140f22008-06-10 23:01:51 +10002505 dump_cfg_fmtint(sStrictModes, o->strict_modes);
2506 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
2507 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
2508 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
Darren Tuckere7140f22008-06-10 23:01:51 +10002509 dump_cfg_fmtint(sCompression, o->compression);
Damien Miller7acefbb2014-07-18 14:11:24 +10002510 dump_cfg_fmtint(sGatewayPorts, o->fwd_opts.gateway_ports);
Darren Tuckere7140f22008-06-10 23:01:51 +10002511 dump_cfg_fmtint(sUseDNS, o->use_dns);
2512 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002513 dump_cfg_fmtint(sAllowAgentForwarding, o->allow_agent_forwarding);
djm@openbsd.org7844f352016-11-30 03:00:05 +00002514 dump_cfg_fmtint(sDisableForwarding, o->disable_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002515 dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
djm@openbsd.org771c2f52016-05-03 15:25:06 +00002516 dump_cfg_fmtint(sStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002517 dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
djm@openbsd.org8f574952017-06-24 06:34:38 +00002518 dump_cfg_fmtint(sExposeAuthInfo, o->expose_userauth_info);
Darren Tuckere7140f22008-06-10 23:01:51 +10002519
2520 /* string arguments */
2521 dump_cfg_string(sPidFile, o->pid_file);
2522 dump_cfg_string(sXAuthLocation, o->xauth_location);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002523 dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
2524 dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
Darren Tuckere7140f22008-06-10 23:01:51 +10002525 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10002526 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11002527 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11002528 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
2529 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10002530 dump_cfg_string(sAuthorizedPrincipalsFile,
2531 o->authorized_principals_file);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002532 dump_cfg_string(sVersionAddendum, *o->version_addendum == '\0'
2533 ? "none" : o->version_addendum);
Damien Miller09d3e122012-10-31 08:58:58 +11002534 dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
2535 dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +00002536 dump_cfg_string(sAuthorizedPrincipalsCommand, o->authorized_principals_command);
2537 dump_cfg_string(sAuthorizedPrincipalsCommandUser, o->authorized_principals_command_user);
Damien Miller85b45e02013-07-20 13:21:52 +10002538 dump_cfg_string(sHostKeyAgent, o->host_key_agent);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002539 dump_cfg_string(sKexAlgorithms,
djm@openbsd.org259a02e2014-10-13 00:38:35 +00002540 o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002541 dump_cfg_string(sHostbasedAcceptedKeyTypes, o->hostbased_key_types ?
2542 o->hostbased_key_types : KEX_DEFAULT_PK_ALG);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002543 dump_cfg_string(sHostKeyAlgorithms, o->hostkeyalgorithms ?
2544 o->hostkeyalgorithms : KEX_DEFAULT_PK_ALG);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002545 dump_cfg_string(sPubkeyAcceptedKeyTypes, o->pubkey_key_types ?
2546 o->pubkey_key_types : KEX_DEFAULT_PK_ALG);
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002547 dump_cfg_string(sRDomain, o->routing_domain);
Darren Tuckere7140f22008-06-10 23:01:51 +10002548
2549 /* string arguments requiring a lookup */
2550 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
2551 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
2552
2553 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10002554 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
2555 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002556 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
2557 o->host_key_files);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002558 dump_cfg_strarray(sHostCertificate, o->num_host_cert_files,
Damien Miller0a80ca12010-02-27 07:55:05 +11002559 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002560 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
2561 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
2562 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
2563 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
2564 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
Damien Millera6e3f012012-11-04 23:21:40 +11002565 dump_cfg_strarray_oneline(sAuthenticationMethods,
2566 o->num_auth_methods, o->auth_methods);
Darren Tuckere7140f22008-06-10 23:01:51 +10002567
2568 /* other arguments */
2569 for (i = 0; i < o->num_subsystems; i++)
2570 printf("subsystem %s %s\n", o->subsystem_name[i],
2571 o->subsystem_args[i]);
2572
2573 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
2574 o->max_startups_rate, o->max_startups);
2575
djm@openbsd.orgd685e5a2017-10-25 02:10:39 +00002576 s = NULL;
2577 for (i = 0; tunmode_desc[i].val != -1; i++) {
Darren Tuckere7140f22008-06-10 23:01:51 +10002578 if (tunmode_desc[i].val == o->permit_tun) {
2579 s = tunmode_desc[i].text;
2580 break;
2581 }
djm@openbsd.orgd685e5a2017-10-25 02:10:39 +00002582 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002583 dump_cfg_string(sPermitTunnel, s);
2584
Damien Miller91475862011-05-05 14:14:34 +10002585 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2586 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11002587
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002588 printf("rekeylimit %llu %d\n", (unsigned long long)o->rekey_limit,
Damien Millera6d6c1f2013-08-21 02:40:01 +10002589 o->rekey_interval);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002590
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002591 printf("permitopen");
2592 if (o->num_permitted_opens == 0)
2593 printf(" any");
2594 else {
2595 for (i = 0; i < o->num_permitted_opens; i++)
2596 printf(" %s", o->permitted_opens[i]);
2597 }
2598 printf("\n");
djm@openbsd.org115063a2018-06-06 18:22:41 +00002599 printf("permitremoteopen");
2600 if (o->num_permitted_remote_opens == 0)
2601 printf(" any");
2602 else {
2603 for (i = 0; i < o->num_permitted_remote_opens; i++)
2604 printf(" %s", o->permitted_remote_opens[i]);
2605 }
2606 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002607}