blob: c87a9a2b6d6894f098fac5f4c1d6bf51cea5532f [file] [log] [blame]
djm@openbsd.org868109b2015-07-01 02:39:06 +00001
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00002/* $OpenBSD: servconf.c,v 1.325 2018/02/23 15:58:37 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003/*
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
Damien Millere3b60b52006-07-10 21:08:03 +100016#include <sys/types.h>
17#include <sys/socket.h>
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000018#ifdef HAVE_SYS_SYSCTL_H
19#include <sys/sysctl.h>
20#endif
Damien Millere3b60b52006-07-10 21:08:03 +100021
Damien Miller0dac6fb2010-11-20 15:19:38 +110022#include <netinet/in.h>
23#include <netinet/in_systm.h>
24#include <netinet/ip.h>
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000025#ifdef HAVE_NET_ROUTE_H
26#include <net/route.h>
27#endif
Damien Miller0dac6fb2010-11-20 15:19:38 +110028
Darren Tucker5f96f3b2013-05-16 20:29:28 +100029#include <ctype.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100030#include <netdb.h>
Damien Miller565ca3f2006-08-19 00:23:15 +100031#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100032#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100033#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100034#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100035#include <signal.h>
Damien Millere6b3b612006-07-24 14:01:23 +100036#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000037#include <limits.h>
Damien Millerd7834352006-08-05 12:39:39 +100038#include <stdarg.h>
Darren Tuckere7140f22008-06-10 23:01:51 +100039#include <errno.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100040#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100041#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100042#endif
Damien Millerbe43ebf2006-07-24 13:51:51 +100043
Damien Millerb84886b2008-05-19 15:05:07 +100044#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100045#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100048#include "buffer.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100049#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050#include "servconf.h"
Damien Miller78928792000-04-12 20:17:38 +100051#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100054#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000055#include "kex.h"
56#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100057#include "match.h"
Damien Miller9b439df2006-07-24 14:04:00 +100058#include "channels.h"
Damien Miller565ca3f2006-08-19 00:23:15 +100059#include "groupaccess.h"
Darren Tuckerfbcf8272012-05-19 19:37:01 +100060#include "canohost.h"
61#include "packet.h"
Damien Millera6e3f012012-11-04 23:21:40 +110062#include "hostfile.h"
63#include "auth.h"
djm@openbsd.org57d378e2014-08-19 23:58:28 +000064#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000065#include "digest.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000067static void add_listen_addr(ServerOptions *, const char *,
68 const char *, int);
69static void add_one_listen_addr(ServerOptions *, const char *,
70 const char *, int);
Damien Miller34132e52000-01-14 15:45:46 +110071
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000072/* Use of privilege separation or not */
73extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100074extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000075
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076/* Initializes the server options to their default values. */
77
Damien Miller4af51302000-04-16 11:18:38 +100078void
Damien Miller95def091999-11-25 00:26:21 +110079initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080{
Damien Miller95def091999-11-25 00:26:21 +110081 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110082
83 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100084 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110085
86 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110087 options->num_ports = 0;
88 options->ports_from_cmdline = 0;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +000089 options->queued_listen_addrs = NULL;
90 options->num_queued_listens = 0;
Damien Miller34132e52000-01-14 15:45:46 +110091 options->listen_addrs = NULL;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000092 options->num_listen_addrs = 0;
Darren Tucker0f383232005-01-20 10:57:56 +110093 options->address_family = -1;
djm@openbsd.org35eb33f2017-10-25 00:17:08 +000094 options->routing_domain = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +110095 options->num_host_key_files = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110096 options->num_host_cert_files = 0;
Damien Miller85b45e02013-07-20 13:21:52 +100097 options->host_key_agent = NULL;
Damien Miller6f83b8e2000-05-02 09:23:45 +100098 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110099 options->login_grace_time = -1;
chris@openbsd.org3d5728a2015-07-31 15:38:09 +0000100 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +1100101 options->ignore_rhosts = -1;
102 options->ignore_user_known_hosts = -1;
103 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000104 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +1100105 options->x11_forwarding = -1;
106 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +1100107 options->x11_use_localhost = -1;
Damien Miller5ff30c62013-10-30 22:21:50 +1100108 options->permit_tty = -1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000109 options->permit_user_rc = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000110 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100111 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +1100112 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +1100113 options->log_facility = SYSLOG_FACILITY_NOT_SET;
114 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000115 options->hostbased_authentication = -1;
116 options->hostbased_uses_name_from_packet_only = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000117 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000118 options->hostkeyalgorithms = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100119 options->pubkey_authentication = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000120 options->pubkey_key_types = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100121 options->kerberos_authentication = -1;
122 options->kerberos_or_local_passwd = -1;
123 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100124 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000125 options->gss_authentication=-1;
126 options->gss_cleanup_creds = -1;
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000127 options->gss_strict_acceptor = -1;
Damien Miller95def091999-11-25 00:26:21 +1100128 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100129 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000130 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100131 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000132 options->permit_user_env = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000133 options->compression = -1;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000134 options->rekey_limit = -1;
135 options->rekey_interval = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100136 options->allow_tcp_forwarding = -1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000137 options->allow_streamlocal_forwarding = -1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000138 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100139 options->num_allow_users = 0;
140 options->num_deny_users = 0;
141 options->num_allow_groups = 0;
142 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000143 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000144 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000145 options->kex_algorithms = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000146 options->fwd_opts.gateway_ports = -1;
147 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
148 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000149 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000150 options->max_startups_begin = -1;
151 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000152 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000153 options->max_authtries = -1;
Damien Miller7207f642008-05-19 15:34:50 +1000154 options->max_sessions = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000155 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000156 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000157 options->client_alive_interval = -1;
158 options->client_alive_count_max = -1;
Damien Millerd8478b62011-05-29 21:39:36 +1000159 options->num_authkeys_files = 0;
Darren Tucker46bc0752004-05-02 22:11:30 +1000160 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100161 options->permit_tun = -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000162 options->permitted_opens = NULL;
Damien Millere2754432006-07-24 14:06:47 +1000163 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100164 options->chroot_directory = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +1100165 options->authorized_keys_command = NULL;
166 options->authorized_keys_command_user = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100167 options->revoked_keys_file = NULL;
168 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000169 options->authorized_principals_file = NULL;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000170 options->authorized_principals_command = NULL;
171 options->authorized_principals_command_user = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100172 options->ip_qos_interactive = -1;
173 options->ip_qos_bulk = -1;
Damien Miller23528812012-04-22 11:24:43 +1000174 options->version_addendum = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000175 options->fingerprint_hash = -1;
djm@openbsd.org7844f352016-11-30 03:00:05 +0000176 options->disable_forwarding = -1;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000177 options->expose_userauth_info = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178}
179
djm@openbsd.org161cf412014-12-22 07:55:51 +0000180/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
181static int
182option_clear_or_none(const char *o)
183{
184 return o == NULL || strcasecmp(o, "none") == 0;
185}
186
djm@openbsd.orged085102015-10-29 08:05:01 +0000187static void
188assemble_algorithms(ServerOptions *o)
189{
190 if (kex_assemble_names(KEX_SERVER_ENCRYPT, &o->ciphers) != 0 ||
191 kex_assemble_names(KEX_SERVER_MAC, &o->macs) != 0 ||
192 kex_assemble_names(KEX_SERVER_KEX, &o->kex_algorithms) != 0 ||
193 kex_assemble_names(KEX_DEFAULT_PK_ALG,
194 &o->hostkeyalgorithms) != 0 ||
195 kex_assemble_names(KEX_DEFAULT_PK_ALG,
196 &o->hostbased_key_types) != 0 ||
197 kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->pubkey_key_types) != 0)
198 fatal("kex_assemble_names failed");
199}
200
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000201static void
202array_append(const char *file, const int line, const char *directive,
203 char ***array, u_int *lp, const char *s)
204{
205
206 if (*lp >= INT_MAX)
207 fatal("%s line %d: Too many %s entries", file, line, directive);
208
209 *array = xrecallocarray(*array, *lp, *lp + 1, sizeof(**array));
210 (*array)[*lp] = xstrdup(s);
211 (*lp)++;
212}
213
214void
215servconf_add_hostkey(const char *file, const int line,
216 ServerOptions *options, const char *path)
217{
218 char *apath = derelativise_path(path);
219
220 array_append(file, line, "HostKey",
221 &options->host_key_files, &options->num_host_key_files, apath);
222 free(apath);
223}
224
225void
226servconf_add_hostcert(const char *file, const int line,
227 ServerOptions *options, const char *path)
228{
229 char *apath = derelativise_path(path);
230
231 array_append(file, line, "HostCertificate",
232 &options->host_cert_files, &options->num_host_cert_files, apath);
233 free(apath);
234}
235
Damien Miller4af51302000-04-16 11:18:38 +1000236void
Damien Miller95def091999-11-25 00:26:21 +1100237fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238{
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000239 u_int i;
djm@openbsd.org161cf412014-12-22 07:55:51 +0000240
Damien Miller726273e2001-11-12 11:40:11 +1100241 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000242 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000243 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100244
245 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100246 if (options->num_host_key_files == 0) {
247 /* fill default hostkeys for protocols */
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000248 servconf_add_hostkey("[default]", 0, options,
249 _PATH_HOST_RSA_KEY_FILE);
Damien Millerdd190dd2010-11-11 14:17:02 +1100250#ifdef OPENSSL_HAS_ECC
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000251 servconf_add_hostkey("[default]", 0, options,
252 _PATH_HOST_ECDSA_KEY_FILE);
Damien Millerdd190dd2010-11-11 14:17:02 +1100253#endif
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000254 servconf_add_hostkey("[default]", 0, options,
255 _PATH_HOST_ED25519_KEY_FILE);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000256 servconf_add_hostkey("[default]", 0, options,
257 _PATH_HOST_XMSS_KEY_FILE);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100258 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100259 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100260 if (options->num_ports == 0)
261 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000262 if (options->address_family == -1)
263 options->address_family = AF_UNSPEC;
Damien Miller34132e52000-01-14 15:45:46 +1100264 if (options->listen_addrs == NULL)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000265 add_listen_addr(options, NULL, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000266 if (options->pid_file == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000267 options->pid_file = xstrdup(_PATH_SSH_DAEMON_PID_FILE);
Damien Miller95def091999-11-25 00:26:21 +1100268 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000269 options->login_grace_time = 120;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000270 if (options->permit_root_login == PERMIT_NOT_SET)
chris@openbsd.org3d5728a2015-07-31 15:38:09 +0000271 options->permit_root_login = PERMIT_NO_PASSWD;
Damien Miller95def091999-11-25 00:26:21 +1100272 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100273 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100274 if (options->ignore_user_known_hosts == -1)
275 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100276 if (options->print_motd == -1)
277 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000278 if (options->print_lastlog == -1)
279 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100280 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100281 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100282 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100283 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100284 if (options->x11_use_localhost == -1)
285 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000286 if (options->xauth_location == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000287 options->xauth_location = xstrdup(_PATH_XAUTH);
Damien Miller5ff30c62013-10-30 22:21:50 +1100288 if (options->permit_tty == -1)
289 options->permit_tty = 1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000290 if (options->permit_user_rc == -1)
291 options->permit_user_rc = 1;
Damien Miller95def091999-11-25 00:26:21 +1100292 if (options->strict_modes == -1)
293 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100294 if (options->tcp_keep_alive == -1)
295 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100296 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100297 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100298 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000299 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000300 if (options->hostbased_authentication == -1)
301 options->hostbased_authentication = 0;
302 if (options->hostbased_uses_name_from_packet_only == -1)
303 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100304 if (options->pubkey_authentication == -1)
305 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100306 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000307 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100308 if (options->kerberos_or_local_passwd == -1)
309 options->kerberos_or_local_passwd = 1;
310 if (options->kerberos_ticket_cleanup == -1)
311 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100312 if (options->kerberos_get_afs_token == -1)
313 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000314 if (options->gss_authentication == -1)
315 options->gss_authentication = 0;
316 if (options->gss_cleanup_creds == -1)
317 options->gss_cleanup_creds = 1;
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000318 if (options->gss_strict_acceptor == -1)
djm@openbsd.org13bd2e22017-01-06 03:45:41 +0000319 options->gss_strict_acceptor = 1;
Damien Miller95def091999-11-25 00:26:21 +1100320 if (options->password_authentication == -1)
321 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100322 if (options->kbd_interactive_authentication == -1)
323 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000324 if (options->challenge_response_authentication == -1)
325 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100326 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100327 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000328 if (options->permit_user_env == -1)
329 options->permit_user_env = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000330 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000331 options->compression = COMP_DELAYED;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000332 if (options->rekey_limit == -1)
333 options->rekey_limit = 0;
334 if (options->rekey_interval == -1)
335 options->rekey_interval = 0;
Damien Miller50a41ed2000-10-16 12:14:42 +1100336 if (options->allow_tcp_forwarding == -1)
Damien Milleraa5b3f82012-12-03 09:50:54 +1100337 options->allow_tcp_forwarding = FORWARD_ALLOW;
Damien Miller7acefbb2014-07-18 14:11:24 +1000338 if (options->allow_streamlocal_forwarding == -1)
339 options->allow_streamlocal_forwarding = FORWARD_ALLOW;
Damien Miller4f755cd2008-05-19 14:57:41 +1000340 if (options->allow_agent_forwarding == -1)
341 options->allow_agent_forwarding = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000342 if (options->fwd_opts.gateway_ports == -1)
343 options->fwd_opts.gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000344 if (options->max_startups == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100345 options->max_startups = 100;
Damien Miller942da032000-08-18 13:59:06 +1000346 if (options->max_startups_rate == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100347 options->max_startups_rate = 30; /* 30% */
Damien Miller942da032000-08-18 13:59:06 +1000348 if (options->max_startups_begin == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100349 options->max_startups_begin = 10;
Darren Tucker89413db2004-05-24 10:36:23 +1000350 if (options->max_authtries == -1)
351 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000352 if (options->max_sessions == -1)
353 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000354 if (options->use_dns == -1)
deraadt@openbsd.org3cd51032015-02-02 01:57:44 +0000355 options->use_dns = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000356 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100357 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000358 if (options->client_alive_count_max == -1)
359 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000360 if (options->num_authkeys_files == 0) {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000361 array_append("[default]", 0, "AuthorizedKeysFiles",
362 &options->authorized_keys_files,
363 &options->num_authkeys_files,
364 _PATH_SSH_USER_PERMITTED_KEYS);
365 array_append("[default]", 0, "AuthorizedKeysFiles",
366 &options->authorized_keys_files,
367 &options->num_authkeys_files,
368 _PATH_SSH_USER_PERMITTED_KEYS2);
Damien Millerd8478b62011-05-29 21:39:36 +1000369 }
Damien Millerd27b9472005-12-13 19:29:02 +1100370 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100371 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100372 if (options->ip_qos_interactive == -1)
373 options->ip_qos_interactive = IPTOS_LOWDELAY;
374 if (options->ip_qos_bulk == -1)
375 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller23528812012-04-22 11:24:43 +1000376 if (options->version_addendum == NULL)
377 options->version_addendum = xstrdup("");
Damien Miller7acefbb2014-07-18 14:11:24 +1000378 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
379 options->fwd_opts.streamlocal_bind_mask = 0177;
380 if (options->fwd_opts.streamlocal_bind_unlink == -1)
381 options->fwd_opts.streamlocal_bind_unlink = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000382 if (options->fingerprint_hash == -1)
383 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org7844f352016-11-30 03:00:05 +0000384 if (options->disable_forwarding == -1)
385 options->disable_forwarding = 0;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000386 if (options->expose_userauth_info == -1)
387 options->expose_userauth_info = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000388
djm@openbsd.orged085102015-10-29 08:05:01 +0000389 assemble_algorithms(options);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000390
djm@openbsd.orgc5c3f322016-02-17 05:29:04 +0000391 /* Turn privilege separation and sandboxing on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000392 if (use_privsep == -1)
djm@openbsd.orgc5c3f322016-02-17 05:29:04 +0000393 use_privsep = PRIVSEP_ON;
Damien Miller4903eb42002-06-21 16:20:44 +1000394
djm@openbsd.org161cf412014-12-22 07:55:51 +0000395#define CLEAR_ON_NONE(v) \
396 do { \
397 if (option_clear_or_none(v)) { \
398 free(v); \
399 v = NULL; \
400 } \
401 } while(0)
402 CLEAR_ON_NONE(options->pid_file);
403 CLEAR_ON_NONE(options->xauth_location);
404 CLEAR_ON_NONE(options->banner);
405 CLEAR_ON_NONE(options->trusted_user_ca_keys);
406 CLEAR_ON_NONE(options->revoked_keys_file);
djm@openbsd.org7e8528c2015-05-01 04:17:51 +0000407 CLEAR_ON_NONE(options->authorized_principals_file);
djm@openbsd.org9fd04682015-11-13 04:38:06 +0000408 CLEAR_ON_NONE(options->adm_forced_command);
409 CLEAR_ON_NONE(options->chroot_directory);
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000410 CLEAR_ON_NONE(options->routing_domain);
djm@openbsd.org161cf412014-12-22 07:55:51 +0000411 for (i = 0; i < options->num_host_key_files; i++)
412 CLEAR_ON_NONE(options->host_key_files[i]);
413 for (i = 0; i < options->num_host_cert_files; i++)
414 CLEAR_ON_NONE(options->host_cert_files[i]);
415#undef CLEAR_ON_NONE
416
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +0000417 /* Similar handling for AuthenticationMethods=any */
418 if (options->num_auth_methods == 1 &&
419 strcmp(options->auth_methods[0], "any") == 0) {
420 free(options->auth_methods[0]);
421 options->auth_methods[0] = NULL;
422 options->num_auth_methods = 0;
423 }
424
Tim Rice40017b02002-07-14 13:36:49 -0700425#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000426 if (use_privsep && options->compression == 1) {
427 error("This platform does not support both privilege "
428 "separation and compression");
429 error("Compression disabled");
430 options->compression = 0;
431 }
432#endif
433
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434}
435
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100437typedef enum {
438 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100439 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000440 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100441 /* Standard Options */
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000442 sPort, sHostKeyFile, sLoginGraceTime,
443 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000444 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100445 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
dtucker@openbsd.org745771f2018-02-09 02:37:36 +0000446 sKerberosGetAFSToken, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100447 sPasswordAuthentication, sKbdInteractiveAuthentication,
448 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000449 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100450 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller5ff30c62013-10-30 22:21:50 +1100451 sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
djm@openbsd.org83b58182016-08-19 03:18:06 +0000452 sPermitUserEnvironment, sAllowTcpForwarding, sCompression,
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000453 sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000454 sIgnoreUserKnownHosts, sCiphers, sMacs, sPidFile,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000455 sGatewayPorts, sPubkeyAuthentication, sPubkeyAcceptedKeyTypes,
456 sXAuthLocation, sSubsystem, sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000457 sBanner, sUseDNS, sHostbasedAuthentication,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000458 sHostbasedUsesNameFromPacketOnly, sHostbasedAcceptedKeyTypes,
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000459 sHostKeyAlgorithms,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000460 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000461 sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
462 sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100463 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100464 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller7cc194f2014-02-04 11:12:56 +1100465 sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000466 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000467 sAuthorizedPrincipalsCommand, sAuthorizedPrincipalsCommandUser,
Damien Miller23528812012-04-22 11:24:43 +1000468 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Miller09d3e122012-10-31 08:58:58 +1100469 sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
Damien Miller72e6b5c2014-07-04 09:00:04 +1000470 sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
Damien Miller7acefbb2014-07-18 14:11:24 +1000471 sStreamLocalBindMask, sStreamLocalBindUnlink,
djm@openbsd.org7844f352016-11-30 03:00:05 +0000472 sAllowStreamLocalForwarding, sFingerprintHash, sDisableForwarding,
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000473 sExposeAuthInfo, sRDomain,
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000474 sDeprecated, sIgnore, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475} ServerOpCodes;
476
Darren Tucker45150472006-07-12 22:34:17 +1000477#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
478#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
479#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
480
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100482static struct {
483 const char *name;
484 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000485 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100486} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100487 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000488#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000489 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000490#else
Darren Tucker45150472006-07-12 22:34:17 +1000491 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000492#endif
Darren Tucker45150472006-07-12 22:34:17 +1000493 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100494 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000495 { "port", sPort, SSHCFG_GLOBAL },
496 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
497 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
Damien Miller85b45e02013-07-20 13:21:52 +1000498 { "hostkeyagent", sHostKeyAgent, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000499 { "pidfile", sPidFile, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000500 { "serverkeybits", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000501 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000502 { "keyregenerationinterval", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100503 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000504 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
djm@openbsd.org54cd41a2017-05-17 01:24:17 +0000505 { "loglevel", sLogLevel, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000506 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000507 { "rhostsrsaauthentication", sDeprecated, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100508 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000509 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000510 { "hostbasedacceptedkeytypes", sHostbasedAcceptedKeyTypes, SSHCFG_ALL },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000511 { "hostkeyalgorithms", sHostKeyAlgorithms, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000512 { "rsaauthentication", sDeprecated, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100513 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000514 { "pubkeyacceptedkeytypes", sPubkeyAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000515 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000516#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100517 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000518 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
519 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100520#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000521 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000522#else
Darren Tucker45150472006-07-12 22:34:17 +1000523 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100524#endif
525#else
Darren Tucker1629c072007-02-19 22:25:37 +1100526 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000527 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
528 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
529 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000530#endif
Darren Tucker45150472006-07-12 22:34:17 +1000531 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
532 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000533#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100534 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000535 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000536 { "gssapistrictacceptorcheck", sGssStrictAcceptor, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000537#else
Darren Tucker1629c072007-02-19 22:25:37 +1100538 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000539 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000540 { "gssapistrictacceptorcheck", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000541#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100542 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
543 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100544 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000545 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
546 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
547 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
548 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
549 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
Damien Millerac908c12015-10-22 09:35:24 +1100550#ifdef DISABLE_LASTLOG
551 { "printlastlog", sUnsupported, SSHCFG_GLOBAL },
552#else
Darren Tucker45150472006-07-12 22:34:17 +1000553 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
Damien Millerac908c12015-10-22 09:35:24 +1100554#endif
Darren Tucker45150472006-07-12 22:34:17 +1000555 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
556 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000557 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
558 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
559 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000560 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
561 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100562 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000563 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
djm@openbsd.org83b58182016-08-19 03:18:06 +0000564 { "uselogin", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000565 { "compression", sCompression, SSHCFG_GLOBAL },
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000566 { "rekeylimit", sRekeyLimit, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000567 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
568 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
569 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000570 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Damien Millerc24da772012-06-20 21:53:58 +1000571 { "allowusers", sAllowUsers, SSHCFG_ALL },
572 { "denyusers", sDenyUsers, SSHCFG_ALL },
573 { "allowgroups", sAllowGroups, SSHCFG_ALL },
574 { "denygroups", sDenyGroups, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000575 { "ciphers", sCiphers, SSHCFG_GLOBAL },
576 { "macs", sMacs, SSHCFG_GLOBAL },
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000577 { "protocol", sIgnore, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000578 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
579 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
580 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000581 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000582 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100583 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000584 { "usedns", sUseDNS, SSHCFG_GLOBAL },
585 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
586 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
markus@openbsd.orgf0ddede2016-11-23 23:14:15 +0000587 { "clientaliveinterval", sClientAliveInterval, SSHCFG_ALL },
588 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000589 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000590 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
djm@openbsd.org66705942017-03-14 07:19:07 +0000591 { "useprivilegeseparation", sDeprecated, SSHCFG_GLOBAL},
Damien Millerc24da772012-06-20 21:53:58 +1000592 { "acceptenv", sAcceptEnv, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000593 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Damien Miller5ff30c62013-10-30 22:21:50 +1100594 { "permittty", sPermitTTY, SSHCFG_ALL },
Damien Miller72e6b5c2014-07-04 09:00:04 +1000595 { "permituserrc", sPermitUserRC, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000596 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000597 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000598 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100599 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100600 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100601 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
602 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000603 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000604 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100605 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller09d3e122012-10-31 08:58:58 +1100606 { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
607 { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000608 { "authorizedprincipalscommand", sAuthorizedPrincipalsCommand, SSHCFG_ALL },
609 { "authorizedprincipalscommanduser", sAuthorizedPrincipalsCommandUser, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000610 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Damien Millera6e3f012012-11-04 23:21:40 +1100611 { "authenticationmethods", sAuthenticationMethods, SSHCFG_ALL },
Damien Miller7acefbb2014-07-18 14:11:24 +1000612 { "streamlocalbindmask", sStreamLocalBindMask, SSHCFG_ALL },
613 { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
614 { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000615 { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
djm@openbsd.org7844f352016-11-30 03:00:05 +0000616 { "disableforwarding", sDisableForwarding, SSHCFG_ALL },
djm@openbsd.org8f574952017-06-24 06:34:38 +0000617 { "exposeauthinfo", sExposeAuthInfo, SSHCFG_ALL },
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000618 { "rdomain", sRDomain, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000619 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620};
621
Darren Tuckere7140f22008-06-10 23:01:51 +1000622static struct {
623 int val;
624 char *text;
625} tunmode_desc[] = {
626 { SSH_TUNMODE_NO, "no" },
627 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
628 { SSH_TUNMODE_ETHERNET, "ethernet" },
629 { SSH_TUNMODE_YES, "yes" },
630 { -1, NULL }
631};
632
Damien Miller5428f641999-11-25 11:54:57 +1100633/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000634 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100635 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000636
Damien Miller4af51302000-04-16 11:18:38 +1000637static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100638parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000639 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000640{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000641 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642
Damien Miller95def091999-11-25 00:26:21 +1100643 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000644 if (strcasecmp(cp, keywords[i].name) == 0) {
645 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100646 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000647 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000648
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000649 error("%s: line %d: Bad configuration option: %s",
650 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100651 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000652}
653
Darren Tucker88b6fb22010-01-13 22:44:29 +1100654char *
655derelativise_path(const char *path)
656{
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000657 char *expanded, *ret, cwd[PATH_MAX];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100658
djm@openbsd.org161cf412014-12-22 07:55:51 +0000659 if (strcasecmp(path, "none") == 0)
660 return xstrdup("none");
Darren Tucker88b6fb22010-01-13 22:44:29 +1100661 expanded = tilde_expand_filename(path, getuid());
662 if (*expanded == '/')
663 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100664 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100665 fatal("%s: getcwd: %s", __func__, strerror(errno));
666 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tuckera627d422013-06-02 07:31:17 +1000667 free(expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100668 return ret;
669}
670
Ben Lindstrombba81212001-06-25 05:01:22 +0000671static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000672add_listen_addr(ServerOptions *options, const char *addr,
673 const char *rdomain, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100674{
Damien Millereccb9de2005-06-17 12:59:34 +1000675 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100676
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000677 if (port > 0)
678 add_one_listen_addr(options, addr, rdomain, port);
679 else {
680 for (i = 0; i < options->num_ports; i++) {
681 add_one_listen_addr(options, addr, rdomain,
682 options->ports[i]);
683 }
684 }
Ben Lindstromc510af42001-04-07 17:25:48 +0000685}
686
Ben Lindstrombba81212001-06-25 05:01:22 +0000687static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000688add_one_listen_addr(ServerOptions *options, const char *addr,
689 const char *rdomain, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000690{
691 struct addrinfo hints, *ai, *aitop;
692 char strport[NI_MAXSERV];
693 int gaierr;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000694 u_int i;
695
696 /* Find listen_addrs entry for this rdomain */
697 for (i = 0; i < options->num_listen_addrs; i++) {
698 if (rdomain == NULL && options->listen_addrs[i].rdomain == NULL)
699 break;
700 if (rdomain == NULL || options->listen_addrs[i].rdomain == NULL)
701 continue;
702 if (strcmp(rdomain, options->listen_addrs[i].rdomain) == 0)
703 break;
704 }
705 if (i >= options->num_listen_addrs) {
706 /* No entry for this rdomain; allocate one */
707 if (i >= INT_MAX)
708 fatal("%s: too many listen addresses", __func__);
709 options->listen_addrs = xrecallocarray(options->listen_addrs,
710 options->num_listen_addrs, options->num_listen_addrs + 1,
711 sizeof(*options->listen_addrs));
712 i = options->num_listen_addrs++;
713 if (rdomain != NULL)
714 options->listen_addrs[i].rdomain = xstrdup(rdomain);
715 }
716 /* options->listen_addrs[i] points to the addresses for this rdomain */
Ben Lindstromc510af42001-04-07 17:25:48 +0000717
718 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100719 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000720 hints.ai_socktype = SOCK_STREAM;
721 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100722 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000723 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
724 fatal("bad addr or host: %s (%s)",
725 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100726 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000727 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
728 ;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000729 ai->ai_next = options->listen_addrs[i].addrs;
730 options->listen_addrs[i].addrs = aitop;
731}
732
733/* Returns nonzero if the routing domain name is valid */
734static int
735valid_rdomain(const char *name)
736{
Damien Miller43c29bb2017-10-25 13:10:59 +1100737#if defined(HAVE_SYS_VALID_RDOMAIN)
Damien Miller2de5c6b2017-10-27 08:42:33 +1100738 return sys_valid_rdomain(name);
Damien Miller43c29bb2017-10-25 13:10:59 +1100739#elif defined(__OpenBSD__)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000740 const char *errstr;
741 long long num;
742 struct rt_tableinfo info;
743 int mib[6];
744 size_t miblen = sizeof(mib);
745
746 if (name == NULL)
747 return 1;
748
749 num = strtonum(name, 0, 255, &errstr);
750 if (errstr != NULL)
751 return 0;
752
753 /* Check whether the table actually exists */
754 memset(mib, 0, sizeof(mib));
755 mib[0] = CTL_NET;
756 mib[1] = PF_ROUTE;
757 mib[4] = NET_RT_TABLE;
758 mib[5] = (int)num;
759 if (sysctl(mib, 6, &info, &miblen, NULL, 0) == -1)
760 return 0;
761
762 return 1;
Damien Miller43c29bb2017-10-25 13:10:59 +1100763#else /* defined(__OpenBSD__) */
764 error("Routing domains are not supported on this platform");
765 return 0;
766#endif
Damien Miller34132e52000-01-14 15:45:46 +1100767}
768
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000769/*
770 * Queue a ListenAddress to be processed once we have all of the Ports
771 * and AddressFamily options.
772 */
773static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000774queue_listen_addr(ServerOptions *options, const char *addr,
775 const char *rdomain, int port)
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000776{
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000777 struct queued_listenaddr *qla;
778
779 options->queued_listen_addrs = xrecallocarray(
780 options->queued_listen_addrs,
781 options->num_queued_listens, options->num_queued_listens + 1,
782 sizeof(*options->queued_listen_addrs));
783 qla = &options->queued_listen_addrs[options->num_queued_listens++];
784 qla->addr = xstrdup(addr);
785 qla->port = port;
786 qla->rdomain = rdomain == NULL ? NULL : xstrdup(rdomain);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000787}
788
789/*
790 * Process queued (text) ListenAddress entries.
791 */
792static void
793process_queued_listen_addrs(ServerOptions *options)
794{
795 u_int i;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000796 struct queued_listenaddr *qla;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000797
798 if (options->num_ports == 0)
799 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
800 if (options->address_family == -1)
801 options->address_family = AF_UNSPEC;
802
803 for (i = 0; i < options->num_queued_listens; i++) {
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000804 qla = &options->queued_listen_addrs[i];
805 add_listen_addr(options, qla->addr, qla->rdomain, qla->port);
806 free(qla->addr);
807 free(qla->rdomain);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000808 }
809 free(options->queued_listen_addrs);
810 options->queued_listen_addrs = NULL;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000811 options->num_queued_listens = 0;
812}
813
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000814/*
815 * Inform channels layer of permitopen options from configuration.
816 */
817void
818process_permitopen(struct ssh *ssh, ServerOptions *options)
819{
820 u_int i;
821 int port;
822 char *host, *arg, *oarg;
823
824 channel_clear_adm_permitted_opens(ssh);
825 if (options->num_permitted_opens == 0)
826 return; /* permit any */
827
828 /* handle keywords: "any" / "none" */
829 if (options->num_permitted_opens == 1 &&
830 strcmp(options->permitted_opens[0], "any") == 0)
831 return;
832 if (options->num_permitted_opens == 1 &&
833 strcmp(options->permitted_opens[0], "none") == 0) {
834 channel_disable_adm_local_opens(ssh);
835 return;
836 }
837 /* Otherwise treat it as a list of permitted host:port */
838 for (i = 0; i < options->num_permitted_opens; i++) {
839 oarg = arg = xstrdup(options->permitted_opens[i]);
840 host = hpdelim(&arg);
841 if (host == NULL)
842 fatal("%s: missing host in PermitOpen", __func__);
843 host = cleanhostname(host);
844 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
845 fatal("%s: bad port number in PermitOpen", __func__);
846 /* Send it to channels layer */
847 channel_add_adm_permitted_opens(ssh, host, port);
848 free(oarg);
849 }
850}
851
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000852struct connection_info *
853get_connection_info(int populate, int use_dns)
854{
djm@openbsd.org95767262016-03-07 19:02:43 +0000855 struct ssh *ssh = active_state; /* XXX */
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000856 static struct connection_info ci;
857
858 if (!populate)
859 return &ci;
djm@openbsd.org95767262016-03-07 19:02:43 +0000860 ci.host = auth_get_canonical_hostname(ssh, use_dns);
861 ci.address = ssh_remote_ipaddr(ssh);
862 ci.laddress = ssh_local_ipaddr(ssh);
863 ci.lport = ssh_local_port(ssh);
djm@openbsd.org68af80e2017-10-25 00:19:47 +0000864 ci.rdomain = ssh_packet_rdomain_in(ssh);
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000865 return &ci;
866}
867
Darren Tucker45150472006-07-12 22:34:17 +1000868/*
869 * The strategy for the Match blocks is that the config file is parsed twice.
870 *
871 * The first time is at startup. activep is initialized to 1 and the
872 * directives in the global context are processed and acted on. Hitting a
873 * Match directive unsets activep and the directives inside the block are
874 * checked for syntax only.
875 *
876 * The second time is after a connection has been established but before
877 * authentication. activep is initialized to 2 and global config directives
878 * are ignored since they have already been processed. If the criteria in a
879 * Match block is met, activep is set and the subsequent directives
880 * processed and actioned until EOF or another Match block unsets it. Any
881 * options set are copied into the main server config.
882 *
883 * Potential additions/improvements:
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000884 * - Add Match support for pre-kex directives, eg. Ciphers.
Darren Tucker45150472006-07-12 22:34:17 +1000885 *
886 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
887 * Match Address 192.168.0.*
888 * Tag trusted
889 * Match Group wheel
890 * Tag trusted
891 * Match Tag trusted
892 * AllowTcpForwarding yes
893 * GatewayPorts clientspecified
894 * [...]
895 *
896 * - Add a PermittedChannelRequests directive
897 * Match Group shell
898 * PermittedChannelRequests session,forwarded-tcpip
899 */
900
901static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000902match_cfg_line_group(const char *grps, int line, const char *user)
903{
904 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000905 struct passwd *pw;
906
Damien Miller565ca3f2006-08-19 00:23:15 +1000907 if (user == NULL)
908 goto out;
909
910 if ((pw = getpwnam(user)) == NULL) {
911 debug("Can't match group at line %d because user %.100s does "
912 "not exist", line, user);
913 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
914 debug("Can't Match group because user %.100s not in any group "
915 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000916 } else if (ga_match_pattern_list(grps) != 1) {
917 debug("user %.100s does not match group list %.100s at line %d",
918 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000919 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000920 debug("user %.100s matched group list %.100s at line %d", user,
921 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000922 result = 1;
923 }
924out:
925 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000926 return result;
927}
928
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000929static void
930match_test_missing_fatal(const char *criteria, const char *attrib)
931{
932 fatal("'Match %s' in configuration but '%s' not in connection "
933 "test specification.", criteria, attrib);
934}
935
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000936/*
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000937 * All of the attributes on a single Match line are ANDed together, so we need
Damien Miller03bf2e62013-10-24 21:01:26 +1100938 * to check every attribute and set the result to zero if any attribute does
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000939 * not match.
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000940 */
Damien Miller565ca3f2006-08-19 00:23:15 +1000941static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000942match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +1000943{
Damien Millercf31f382013-10-24 21:02:56 +1100944 int result = 1, attributes = 0, port;
Darren Tucker45150472006-07-12 22:34:17 +1000945 char *arg, *attrib, *cp = *condition;
Darren Tucker45150472006-07-12 22:34:17 +1000946
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000947 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000948 debug3("checking syntax for 'Match %s'", cp);
949 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000950 debug3("checking match for '%s' user %s host %s addr %s "
951 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
952 ci->host ? ci->host : "(null)",
953 ci->address ? ci->address : "(null)",
954 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +1000955
956 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100957 attributes++;
958 if (strcasecmp(attrib, "all") == 0) {
959 if (attributes != 1 ||
960 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
961 error("'all' cannot be combined with other "
962 "Match attributes");
963 return -1;
964 }
965 *condition = cp;
966 return 1;
967 }
Darren Tucker45150472006-07-12 22:34:17 +1000968 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
969 error("Missing Match criteria for %s", attrib);
970 return -1;
971 }
Darren Tucker45150472006-07-12 22:34:17 +1000972 if (strcasecmp(attrib, "user") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000973 if (ci == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000974 result = 0;
975 continue;
976 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000977 if (ci->user == NULL)
978 match_test_missing_fatal("User", "user");
djm@openbsd.orge661a862015-05-04 06:10:48 +0000979 if (match_pattern_list(ci->user, arg, 0) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000980 result = 0;
981 else
982 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000983 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000984 } else if (strcasecmp(attrib, "group") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000985 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000986 result = 0;
987 continue;
988 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000989 if (ci->user == NULL)
990 match_test_missing_fatal("Group", "user");
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000991 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +1000992 case -1:
993 return -1;
994 case 0:
995 result = 0;
996 }
Darren Tucker45150472006-07-12 22:34:17 +1000997 } else if (strcasecmp(attrib, "host") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000998 if (ci == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000999 result = 0;
1000 continue;
1001 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001002 if (ci->host == NULL)
1003 match_test_missing_fatal("Host", "host");
djm@openbsd.orge661a862015-05-04 06:10:48 +00001004 if (match_hostname(ci->host, arg) != 1)
Darren Tucker45150472006-07-12 22:34:17 +10001005 result = 0;
1006 else
1007 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001008 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +10001009 } else if (strcasecmp(attrib, "address") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001010 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001011 result = 0;
1012 continue;
1013 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001014 if (ci->address == NULL)
1015 match_test_missing_fatal("Address", "addr");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001016 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +10001017 case 1:
Darren Tucker45150472006-07-12 22:34:17 +10001018 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001019 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +10001020 break;
1021 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +10001022 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +10001023 result = 0;
1024 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +10001025 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +10001026 return -1;
1027 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001028 } else if (strcasecmp(attrib, "localaddress") == 0){
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001029 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001030 result = 0;
1031 continue;
1032 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001033 if (ci->laddress == NULL)
1034 match_test_missing_fatal("LocalAddress",
1035 "laddr");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001036 switch (addr_match_list(ci->laddress, arg)) {
1037 case 1:
1038 debug("connection from %.100s matched "
1039 "'LocalAddress %.100s' at line %d",
1040 ci->laddress, arg, line);
1041 break;
1042 case 0:
1043 case -1:
1044 result = 0;
1045 break;
1046 case -2:
1047 return -1;
1048 }
1049 } else if (strcasecmp(attrib, "localport") == 0) {
1050 if ((port = a2port(arg)) == -1) {
1051 error("Invalid LocalPort '%s' on Match line",
1052 arg);
1053 return -1;
1054 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001055 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001056 result = 0;
1057 continue;
1058 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001059 if (ci->lport == 0)
1060 match_test_missing_fatal("LocalPort", "lport");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001061 /* TODO support port lists */
1062 if (port == ci->lport)
1063 debug("connection from %.100s matched "
1064 "'LocalPort %d' at line %d",
1065 ci->laddress, port, line);
1066 else
1067 result = 0;
djm@openbsd.org68af80e2017-10-25 00:19:47 +00001068 } else if (strcasecmp(attrib, "rdomain") == 0) {
1069 if (ci == NULL || ci->rdomain == NULL) {
1070 result = 0;
1071 continue;
1072 }
1073 if (match_pattern_list(ci->rdomain, arg, 0) != 1)
1074 result = 0;
1075 else
1076 debug("user %.100s matched 'RDomain %.100s' at "
1077 "line %d", ci->rdomain, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +10001078 } else {
1079 error("Unsupported Match attribute %s", attrib);
1080 return -1;
1081 }
1082 }
Damien Millercf31f382013-10-24 21:02:56 +11001083 if (attributes == 0) {
1084 error("One or more attributes required for Match");
1085 return -1;
1086 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001087 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +10001088 debug3("match %sfound", result ? "" : "not ");
1089 *condition = cp;
1090 return result;
1091}
1092
Damien Millere2754432006-07-24 14:06:47 +10001093#define WHITESPACE " \t\r\n"
1094
Damien Miller33322122011-06-20 14:43:11 +10001095/* Multistate option parsing */
1096struct multistate {
1097 char *key;
1098 int value;
1099};
djm@openbsd.org@openbsd.org33edb6e2017-11-03 05:18:44 +00001100static const struct multistate multistate_flag[] = {
1101 { "yes", 1 },
1102 { "no", 0 },
1103 { NULL, -1 }
1104};
Damien Miller33322122011-06-20 14:43:11 +10001105static const struct multistate multistate_addressfamily[] = {
1106 { "inet", AF_INET },
1107 { "inet6", AF_INET6 },
1108 { "any", AF_UNSPEC },
1109 { NULL, -1 }
1110};
1111static const struct multistate multistate_permitrootlogin[] = {
1112 { "without-password", PERMIT_NO_PASSWD },
deraadt@openbsd.org1dc8d932015-08-06 14:53:21 +00001113 { "prohibit-password", PERMIT_NO_PASSWD },
Damien Miller33322122011-06-20 14:43:11 +10001114 { "forced-commands-only", PERMIT_FORCED_ONLY },
1115 { "yes", PERMIT_YES },
1116 { "no", PERMIT_NO },
1117 { NULL, -1 }
1118};
1119static const struct multistate multistate_compression[] = {
djm@openbsd.org0082fba2016-09-28 16:33:06 +00001120 { "yes", COMP_DELAYED },
djm@openbsd.org4577ade2016-09-28 20:32:42 +00001121 { "delayed", COMP_DELAYED },
Damien Miller33322122011-06-20 14:43:11 +10001122 { "no", COMP_NONE },
1123 { NULL, -1 }
1124};
1125static const struct multistate multistate_gatewayports[] = {
1126 { "clientspecified", 2 },
1127 { "yes", 1 },
1128 { "no", 0 },
1129 { NULL, -1 }
1130};
Damien Milleraa5b3f82012-12-03 09:50:54 +11001131static const struct multistate multistate_tcpfwd[] = {
1132 { "yes", FORWARD_ALLOW },
1133 { "all", FORWARD_ALLOW },
1134 { "no", FORWARD_DENY },
1135 { "remote", FORWARD_REMOTE },
1136 { "local", FORWARD_LOCAL },
1137 { NULL, -1 }
1138};
Damien Miller33322122011-06-20 14:43:11 +10001139
Ben Lindstromade03f62001-12-06 18:22:17 +00001140int
1141process_server_config_line(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001142 const char *filename, int linenum, int *activep,
1143 struct connection_info *connectinfo)
Ben Lindstromade03f62001-12-06 18:22:17 +00001144{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001145 char *cp, **charptr, *arg, *arg2, *p;
Darren Tucker9113d0c2013-05-16 20:48:14 +10001146 int cmdline = 0, *intptr, value, value2, n, port;
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001147 SyslogFacility *log_facility_ptr;
1148 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +00001149 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +10001150 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +10001151 size_t len;
Darren Tucker9113d0c2013-05-16 20:48:14 +10001152 long long val64;
Damien Miller33322122011-06-20 14:43:11 +10001153 const struct multistate *multistate_ptr;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001154 const char *errstr;
Ben Lindstromade03f62001-12-06 18:22:17 +00001155
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +00001156 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
1157 if ((len = strlen(line)) == 0)
1158 return 0;
1159 for (len--; len > 0; len--) {
1160 if (strchr(WHITESPACE "\f", line[len]) == NULL)
1161 break;
1162 line[len] = '\0';
1163 }
1164
Ben Lindstromade03f62001-12-06 18:22:17 +00001165 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +11001166 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +11001167 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +00001168 /* Ignore leading whitespace */
1169 if (*arg == '\0')
1170 arg = strdelim(&cp);
1171 if (!arg || !*arg || *arg == '#')
1172 return 0;
1173 intptr = NULL;
1174 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +10001175 opcode = parse_token(arg, filename, linenum, &flags);
1176
1177 if (activep == NULL) { /* We are processing a command line directive */
1178 cmdline = 1;
1179 activep = &cmdline;
1180 }
1181 if (*activep && opcode != sMatch)
1182 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
1183 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001184 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +10001185 fatal("%s line %d: Directive '%s' is not allowed "
1186 "within a Match block", filename, linenum, arg);
1187 } else { /* this is a directive we have already processed */
1188 while (arg)
1189 arg = strdelim(&cp);
1190 return 0;
1191 }
1192 }
1193
Ben Lindstromade03f62001-12-06 18:22:17 +00001194 switch (opcode) {
1195 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +10001196 case sUsePAM:
1197 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +00001198 goto parse_flag;
1199
1200 /* Standard Options */
1201 case sBadOption:
1202 return -1;
1203 case sPort:
1204 /* ignore ports from configfile if cmdline specifies ports */
1205 if (options->ports_from_cmdline)
1206 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +00001207 if (options->num_ports >= MAX_PORTS)
1208 fatal("%s line %d: too many ports.",
1209 filename, linenum);
1210 arg = strdelim(&cp);
1211 if (!arg || *arg == '\0')
1212 fatal("%s line %d: missing port number.",
1213 filename, linenum);
1214 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +11001215 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +00001216 fatal("%s line %d: Badly formatted port number.",
1217 filename, linenum);
1218 break;
1219
Ben Lindstromade03f62001-12-06 18:22:17 +00001220 case sLoginGraceTime:
1221 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +10001222 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +00001223 arg = strdelim(&cp);
1224 if (!arg || *arg == '\0')
1225 fatal("%s line %d: missing time value.",
1226 filename, linenum);
1227 if ((value = convtime(arg)) == -1)
1228 fatal("%s line %d: invalid time value.",
1229 filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001230 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001231 *intptr = value;
1232 break;
1233
Ben Lindstromade03f62001-12-06 18:22:17 +00001234 case sListenAddress:
1235 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001236 if (arg == NULL || *arg == '\0')
1237 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +00001238 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +10001239 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
1240 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
1241 && strchr(p+1, ':') != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001242 port = 0;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001243 p = arg;
1244 } else {
1245 p = hpdelim(&arg);
1246 if (p == NULL)
1247 fatal("%s line %d: bad address:port usage",
1248 filename, linenum);
1249 p = cleanhostname(p);
1250 if (arg == NULL)
1251 port = 0;
1252 else if ((port = a2port(arg)) <= 0)
1253 fatal("%s line %d: bad port number",
1254 filename, linenum);
1255 }
1256 /* Optional routing table */
1257 arg2 = NULL;
1258 if ((arg = strdelim(&cp)) != NULL) {
1259 if (strcmp(arg, "rdomain") != 0 ||
1260 (arg2 = strdelim(&cp)) == NULL)
1261 fatal("%s line %d: bad ListenAddress syntax",
1262 filename, linenum);
1263 if (!valid_rdomain(arg2))
1264 fatal("%s line %d: bad routing domain",
1265 filename, linenum);
1266 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001267
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001268 queue_listen_addr(options, p, arg2, port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001269
Ben Lindstromade03f62001-12-06 18:22:17 +00001270 break;
1271
Darren Tucker0f383232005-01-20 10:57:56 +11001272 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +10001273 intptr = &options->address_family;
1274 multistate_ptr = multistate_addressfamily;
Damien Miller33322122011-06-20 14:43:11 +10001275 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +11001276 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +10001277 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +10001278 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +10001279 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +10001280 value = -1;
1281 for (i = 0; multistate_ptr[i].key != NULL; i++) {
1282 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
1283 value = multistate_ptr[i].value;
1284 break;
1285 }
1286 }
1287 if (value == -1)
1288 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +11001289 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +10001290 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +11001291 *intptr = value;
1292 break;
1293
Ben Lindstromade03f62001-12-06 18:22:17 +00001294 case sHostKeyFile:
Ben Lindstromade03f62001-12-06 18:22:17 +00001295 arg = strdelim(&cp);
1296 if (!arg || *arg == '\0')
1297 fatal("%s line %d: missing file name.",
1298 filename, linenum);
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001299 if (*activep)
1300 servconf_add_hostkey(filename, linenum, options, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001301 break;
1302
Damien Miller85b45e02013-07-20 13:21:52 +10001303 case sHostKeyAgent:
1304 charptr = &options->host_key_agent;
1305 arg = strdelim(&cp);
1306 if (!arg || *arg == '\0')
1307 fatal("%s line %d: missing socket name.",
1308 filename, linenum);
1309 if (*activep && *charptr == NULL)
1310 *charptr = !strcmp(arg, SSH_AUTHSOCKET_ENV_NAME) ?
1311 xstrdup(arg) : derelativise_path(arg);
1312 break;
1313
Damien Miller0a80ca12010-02-27 07:55:05 +11001314 case sHostCertificate:
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001315 arg = strdelim(&cp);
1316 if (!arg || *arg == '\0')
1317 fatal("%s line %d: missing file name.",
1318 filename, linenum);
1319 if (*activep)
1320 servconf_add_hostcert(filename, linenum, options, arg);
1321 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001322
Ben Lindstromade03f62001-12-06 18:22:17 +00001323 case sPidFile:
1324 charptr = &options->pid_file;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001325 parse_filename:
1326 arg = strdelim(&cp);
1327 if (!arg || *arg == '\0')
1328 fatal("%s line %d: missing file name.",
1329 filename, linenum);
1330 if (*activep && *charptr == NULL) {
1331 *charptr = derelativise_path(arg);
1332 /* increase optional counter */
1333 if (intptr != NULL)
1334 *intptr = *intptr + 1;
1335 }
1336 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001337
1338 case sPermitRootLogin:
1339 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +10001340 multistate_ptr = multistate_permitrootlogin;
1341 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001342
1343 case sIgnoreRhosts:
1344 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +10001345 parse_flag:
djm@openbsd.org@openbsd.org33edb6e2017-11-03 05:18:44 +00001346 multistate_ptr = multistate_flag;
1347 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001348
1349 case sIgnoreUserKnownHosts:
1350 intptr = &options->ignore_user_known_hosts;
1351 goto parse_flag;
1352
Ben Lindstromade03f62001-12-06 18:22:17 +00001353 case sHostbasedAuthentication:
1354 intptr = &options->hostbased_authentication;
1355 goto parse_flag;
1356
1357 case sHostbasedUsesNameFromPacketOnly:
1358 intptr = &options->hostbased_uses_name_from_packet_only;
1359 goto parse_flag;
1360
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001361 case sHostbasedAcceptedKeyTypes:
1362 charptr = &options->hostbased_key_types;
1363 parse_keytypes:
1364 arg = strdelim(&cp);
1365 if (!arg || *arg == '\0')
1366 fatal("%s line %d: Missing argument.",
1367 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001368 if (*arg != '-' &&
1369 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001370 fatal("%s line %d: Bad key types '%s'.",
1371 filename, linenum, arg ? arg : "<NONE>");
1372 if (*activep && *charptr == NULL)
1373 *charptr = xstrdup(arg);
1374 break;
1375
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001376 case sHostKeyAlgorithms:
1377 charptr = &options->hostkeyalgorithms;
1378 goto parse_keytypes;
1379
Ben Lindstromade03f62001-12-06 18:22:17 +00001380 case sPubkeyAuthentication:
1381 intptr = &options->pubkey_authentication;
1382 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001383
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001384 case sPubkeyAcceptedKeyTypes:
1385 charptr = &options->pubkey_key_types;
1386 goto parse_keytypes;
1387
Ben Lindstromade03f62001-12-06 18:22:17 +00001388 case sKerberosAuthentication:
1389 intptr = &options->kerberos_authentication;
1390 goto parse_flag;
1391
1392 case sKerberosOrLocalPasswd:
1393 intptr = &options->kerberos_or_local_passwd;
1394 goto parse_flag;
1395
1396 case sKerberosTicketCleanup:
1397 intptr = &options->kerberos_ticket_cleanup;
1398 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001399
Darren Tucker22ef5082003-12-31 11:37:34 +11001400 case sKerberosGetAFSToken:
1401 intptr = &options->kerberos_get_afs_token;
1402 goto parse_flag;
1403
Darren Tucker0efd1552003-08-26 11:49:55 +10001404 case sGssAuthentication:
1405 intptr = &options->gss_authentication;
1406 goto parse_flag;
1407
1408 case sGssCleanupCreds:
1409 intptr = &options->gss_cleanup_creds;
1410 goto parse_flag;
1411
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +00001412 case sGssStrictAcceptor:
1413 intptr = &options->gss_strict_acceptor;
1414 goto parse_flag;
1415
Ben Lindstromade03f62001-12-06 18:22:17 +00001416 case sPasswordAuthentication:
1417 intptr = &options->password_authentication;
1418 goto parse_flag;
1419
1420 case sKbdInteractiveAuthentication:
1421 intptr = &options->kbd_interactive_authentication;
1422 goto parse_flag;
1423
1424 case sChallengeResponseAuthentication:
1425 intptr = &options->challenge_response_authentication;
1426 goto parse_flag;
1427
1428 case sPrintMotd:
1429 intptr = &options->print_motd;
1430 goto parse_flag;
1431
1432 case sPrintLastLog:
1433 intptr = &options->print_lastlog;
1434 goto parse_flag;
1435
1436 case sX11Forwarding:
1437 intptr = &options->x11_forwarding;
1438 goto parse_flag;
1439
1440 case sX11DisplayOffset:
1441 intptr = &options->x11_display_offset;
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +00001442 parse_int:
1443 arg = strdelim(&cp);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001444 if ((errstr = atoi_err(arg, &value)) != NULL)
1445 fatal("%s line %d: integer value %s.",
1446 filename, linenum, errstr);
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +00001447 if (*activep && *intptr == -1)
1448 *intptr = value;
1449 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001450
Damien Miller95c249f2002-02-05 12:11:34 +11001451 case sX11UseLocalhost:
1452 intptr = &options->x11_use_localhost;
1453 goto parse_flag;
1454
Ben Lindstromade03f62001-12-06 18:22:17 +00001455 case sXAuthLocation:
1456 charptr = &options->xauth_location;
1457 goto parse_filename;
1458
Damien Miller5ff30c62013-10-30 22:21:50 +11001459 case sPermitTTY:
1460 intptr = &options->permit_tty;
1461 goto parse_flag;
1462
Damien Miller72e6b5c2014-07-04 09:00:04 +10001463 case sPermitUserRC:
1464 intptr = &options->permit_user_rc;
1465 goto parse_flag;
1466
Ben Lindstromade03f62001-12-06 18:22:17 +00001467 case sStrictModes:
1468 intptr = &options->strict_modes;
1469 goto parse_flag;
1470
Damien Miller12c150e2003-12-17 16:31:10 +11001471 case sTCPKeepAlive:
1472 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001473 goto parse_flag;
1474
1475 case sEmptyPasswd:
1476 intptr = &options->permit_empty_passwd;
1477 goto parse_flag;
1478
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001479 case sPermitUserEnvironment:
1480 intptr = &options->permit_user_env;
1481 goto parse_flag;
1482
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001483 case sCompression:
1484 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001485 multistate_ptr = multistate_compression;
1486 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001487
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001488 case sRekeyLimit:
1489 arg = strdelim(&cp);
1490 if (!arg || *arg == '\0')
1491 fatal("%.200s line %d: Missing argument.", filename,
1492 linenum);
1493 if (strcmp(arg, "default") == 0) {
1494 val64 = 0;
1495 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001496 if (scan_scaled(arg, &val64) == -1)
1497 fatal("%.200s line %d: Bad number '%s': %s",
1498 filename, linenum, arg, strerror(errno));
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001499 if (val64 != 0 && val64 < 16)
1500 fatal("%.200s line %d: RekeyLimit too small",
1501 filename, linenum);
1502 }
1503 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001504 options->rekey_limit = val64;
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001505 if (cp != NULL) { /* optional rekey interval present */
1506 if (strcmp(cp, "none") == 0) {
1507 (void)strdelim(&cp); /* discard */
1508 break;
1509 }
1510 intptr = &options->rekey_interval;
1511 goto parse_time;
1512 }
1513 break;
1514
Ben Lindstromade03f62001-12-06 18:22:17 +00001515 case sGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +10001516 intptr = &options->fwd_opts.gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001517 multistate_ptr = multistate_gatewayports;
1518 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001519
Damien Miller3a961dc2003-06-03 10:25:48 +10001520 case sUseDNS:
1521 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001522 goto parse_flag;
1523
1524 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001525 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001526 arg = strdelim(&cp);
1527 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001528 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001529 fatal("%.200s line %d: unsupported log facility '%s'",
1530 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001531 if (*log_facility_ptr == -1)
1532 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001533 break;
1534
1535 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001536 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001537 arg = strdelim(&cp);
1538 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001539 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001540 fatal("%.200s line %d: unsupported log level '%s'",
1541 filename, linenum, arg ? arg : "<NONE>");
djm@openbsd.org54cd41a2017-05-17 01:24:17 +00001542 if (*activep && *log_level_ptr == -1)
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001543 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001544 break;
1545
1546 case sAllowTcpForwarding:
1547 intptr = &options->allow_tcp_forwarding;
Damien Milleraa5b3f82012-12-03 09:50:54 +11001548 multistate_ptr = multistate_tcpfwd;
1549 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001550
Damien Miller7acefbb2014-07-18 14:11:24 +10001551 case sAllowStreamLocalForwarding:
1552 intptr = &options->allow_streamlocal_forwarding;
1553 multistate_ptr = multistate_tcpfwd;
1554 goto parse_multistate;
1555
Damien Miller4f755cd2008-05-19 14:57:41 +10001556 case sAllowAgentForwarding:
1557 intptr = &options->allow_agent_forwarding;
1558 goto parse_flag;
1559
djm@openbsd.org7844f352016-11-30 03:00:05 +00001560 case sDisableForwarding:
1561 intptr = &options->disable_forwarding;
1562 goto parse_flag;
1563
Ben Lindstromade03f62001-12-06 18:22:17 +00001564 case sAllowUsers:
1565 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.org010359b2016-11-06 05:46:37 +00001566 if (match_user(NULL, NULL, NULL, arg) == -1)
1567 fatal("%s line %d: invalid AllowUsers pattern: "
1568 "\"%.100s\"", filename, linenum, arg);
Damien Millerc24da772012-06-20 21:53:58 +10001569 if (!*activep)
1570 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001571 array_append(filename, linenum, "AllowUsers",
1572 &options->allow_users, &options->num_allow_users,
1573 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001574 }
1575 break;
1576
1577 case sDenyUsers:
1578 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.org010359b2016-11-06 05:46:37 +00001579 if (match_user(NULL, NULL, NULL, arg) == -1)
1580 fatal("%s line %d: invalid DenyUsers pattern: "
1581 "\"%.100s\"", filename, linenum, arg);
Damien Millerc24da772012-06-20 21:53:58 +10001582 if (!*activep)
1583 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001584 array_append(filename, linenum, "DenyUsers",
1585 &options->deny_users, &options->num_deny_users,
1586 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001587 }
1588 break;
1589
1590 case sAllowGroups:
1591 while ((arg = strdelim(&cp)) && *arg != '\0') {
Damien Millerc24da772012-06-20 21:53:58 +10001592 if (!*activep)
1593 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001594 array_append(filename, linenum, "AllowGroups",
1595 &options->allow_groups, &options->num_allow_groups,
1596 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001597 }
1598 break;
1599
1600 case sDenyGroups:
1601 while ((arg = strdelim(&cp)) && *arg != '\0') {
Damien Millerc24da772012-06-20 21:53:58 +10001602 if (!*activep)
1603 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001604 array_append(filename, linenum, "DenyGroups",
1605 &options->deny_groups, &options->num_deny_groups,
1606 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001607 }
1608 break;
1609
1610 case sCiphers:
1611 arg = strdelim(&cp);
1612 if (!arg || *arg == '\0')
1613 fatal("%s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001614 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstromade03f62001-12-06 18:22:17 +00001615 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1616 filename, linenum, arg ? arg : "<NONE>");
1617 if (options->ciphers == NULL)
1618 options->ciphers = xstrdup(arg);
1619 break;
1620
1621 case sMacs:
1622 arg = strdelim(&cp);
1623 if (!arg || *arg == '\0')
1624 fatal("%s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001625 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstromade03f62001-12-06 18:22:17 +00001626 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1627 filename, linenum, arg ? arg : "<NONE>");
1628 if (options->macs == NULL)
1629 options->macs = xstrdup(arg);
1630 break;
1631
Damien Millerd5f62bf2010-09-24 22:11:14 +10001632 case sKexAlgorithms:
1633 arg = strdelim(&cp);
1634 if (!arg || *arg == '\0')
1635 fatal("%s line %d: Missing argument.",
1636 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001637 if (*arg != '-' &&
1638 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001639 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1640 filename, linenum, arg ? arg : "<NONE>");
1641 if (options->kex_algorithms == NULL)
1642 options->kex_algorithms = xstrdup(arg);
1643 break;
1644
Ben Lindstromade03f62001-12-06 18:22:17 +00001645 case sSubsystem:
1646 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1647 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001648 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001649 }
1650 arg = strdelim(&cp);
1651 if (!arg || *arg == '\0')
1652 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001653 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001654 if (!*activep) {
1655 arg = strdelim(&cp);
1656 break;
1657 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001658 for (i = 0; i < options->num_subsystems; i++)
1659 if (strcmp(arg, options->subsystem_name[i]) == 0)
1660 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001661 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001662 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1663 arg = strdelim(&cp);
1664 if (!arg || *arg == '\0')
1665 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001666 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001667 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001668
1669 /* Collect arguments (separate to executable) */
1670 p = xstrdup(arg);
1671 len = strlen(p) + 1;
1672 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1673 len += 1 + strlen(arg);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001674 p = xreallocarray(p, 1, len);
Damien Miller917f9b62006-07-10 20:36:47 +10001675 strlcat(p, " ", len);
1676 strlcat(p, arg, len);
1677 }
1678 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001679 options->num_subsystems++;
1680 break;
1681
1682 case sMaxStartups:
1683 arg = strdelim(&cp);
1684 if (!arg || *arg == '\0')
1685 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001686 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001687 if ((n = sscanf(arg, "%d:%d:%d",
1688 &options->max_startups_begin,
1689 &options->max_startups_rate,
1690 &options->max_startups)) == 3) {
1691 if (options->max_startups_begin >
1692 options->max_startups ||
1693 options->max_startups_rate > 100 ||
1694 options->max_startups_rate < 1)
1695 fatal("%s line %d: Illegal MaxStartups spec.",
1696 filename, linenum);
1697 } else if (n != 1)
1698 fatal("%s line %d: Illegal MaxStartups spec.",
1699 filename, linenum);
1700 else
1701 options->max_startups = options->max_startups_begin;
1702 break;
1703
Darren Tucker89413db2004-05-24 10:36:23 +10001704 case sMaxAuthTries:
1705 intptr = &options->max_authtries;
1706 goto parse_int;
1707
Damien Miller7207f642008-05-19 15:34:50 +10001708 case sMaxSessions:
1709 intptr = &options->max_sessions;
1710 goto parse_int;
1711
Ben Lindstromade03f62001-12-06 18:22:17 +00001712 case sBanner:
1713 charptr = &options->banner;
1714 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001715
Ben Lindstromade03f62001-12-06 18:22:17 +00001716 /*
1717 * These options can contain %X options expanded at
1718 * connect time, so that you can specify paths like:
1719 *
1720 * AuthorizedKeysFile /etc/ssh_keys/%u
1721 */
1722 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001723 if (*activep && options->num_authkeys_files == 0) {
1724 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001725 arg = tilde_expand_filename(arg, getuid());
1726 array_append(filename, linenum,
1727 "AuthorizedKeysFile",
1728 &options->authorized_keys_files,
1729 &options->num_authkeys_files, arg);
1730 free(arg);
Damien Millerd8478b62011-05-29 21:39:36 +10001731 }
1732 }
1733 return 0;
1734
Damien Miller30da3442010-05-10 11:58:03 +10001735 case sAuthorizedPrincipalsFile:
1736 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001737 arg = strdelim(&cp);
1738 if (!arg || *arg == '\0')
1739 fatal("%s line %d: missing file name.",
1740 filename, linenum);
1741 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001742 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001743 /* increase optional counter */
1744 if (intptr != NULL)
1745 *intptr = *intptr + 1;
1746 }
1747 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001748
1749 case sClientAliveInterval:
1750 intptr = &options->client_alive_interval;
1751 goto parse_time;
1752
1753 case sClientAliveCountMax:
1754 intptr = &options->client_alive_count_max;
1755 goto parse_int;
1756
Darren Tucker46bc0752004-05-02 22:11:30 +10001757 case sAcceptEnv:
1758 while ((arg = strdelim(&cp)) && *arg != '\0') {
1759 if (strchr(arg, '=') != NULL)
1760 fatal("%s line %d: Invalid environment name.",
1761 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001762 if (!*activep)
Damien Millerc24da772012-06-20 21:53:58 +10001763 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001764 array_append(filename, linenum, "AcceptEnv",
1765 &options->accept_env, &options->num_accept_env,
1766 arg);
Darren Tucker46bc0752004-05-02 22:11:30 +10001767 }
1768 break;
1769
Damien Millerd27b9472005-12-13 19:29:02 +11001770 case sPermitTunnel:
1771 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001772 arg = strdelim(&cp);
1773 if (!arg || *arg == '\0')
1774 fatal("%s line %d: Missing yes/point-to-point/"
1775 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001776 value = -1;
1777 for (i = 0; tunmode_desc[i].val != -1; i++)
1778 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1779 value = tunmode_desc[i].val;
1780 break;
1781 }
1782 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001783 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1784 "no argument: %s", filename, linenum, arg);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001785 if (*activep && *intptr == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001786 *intptr = value;
1787 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001788
Darren Tucker45150472006-07-12 22:34:17 +10001789 case sMatch:
1790 if (cmdline)
1791 fatal("Match directive not supported as a command-line "
1792 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001793 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10001794 if (value < 0)
1795 fatal("%s line %d: Bad Match condition", filename,
1796 linenum);
1797 *activep = value;
1798 break;
1799
Damien Miller9b439df2006-07-24 14:04:00 +10001800 case sPermitOpen:
1801 arg = strdelim(&cp);
1802 if (!arg || *arg == '\0')
1803 fatal("%s line %d: missing PermitOpen specification",
1804 filename, linenum);
djm@openbsd.org69bda022017-10-04 18:49:30 +00001805 value = options->num_permitted_opens; /* modified later */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001806 if (strcmp(arg, "any") == 0 || strcmp(arg, "none") == 0) {
djm@openbsd.org69bda022017-10-04 18:49:30 +00001807 if (*activep && value == 0) {
Damien Millerc6081482012-04-22 11:18:53 +10001808 options->num_permitted_opens = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001809 options->permitted_opens = xcalloc(1,
1810 sizeof(*options->permitted_opens));
1811 options->permitted_opens[0] = xstrdup(arg);
dtucker@openbsd.org30484e52017-09-18 09:41:52 +00001812 }
Damien Millerc6081482012-04-22 11:18:53 +10001813 break;
1814 }
Damien Millera765cf42006-07-24 14:08:13 +10001815 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001816 arg2 = xstrdup(arg);
Damien Millera765cf42006-07-24 14:08:13 +10001817 p = hpdelim(&arg);
1818 if (p == NULL)
1819 fatal("%s line %d: missing host in PermitOpen",
1820 filename, linenum);
1821 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001822 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001823 fatal("%s line %d: bad port number in "
1824 "PermitOpen", filename, linenum);
djm@openbsd.org69bda022017-10-04 18:49:30 +00001825 if (*activep && value == 0) {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001826 array_append(filename, linenum,
1827 "PermitOpen",
1828 &options->permitted_opens,
1829 &options->num_permitted_opens, arg2);
1830 }
1831 free(arg2);
Damien Millera765cf42006-07-24 14:08:13 +10001832 }
Damien Miller9b439df2006-07-24 14:04:00 +10001833 break;
1834
Damien Millere2754432006-07-24 14:06:47 +10001835 case sForceCommand:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001836 if (cp == NULL || *cp == '\0')
Damien Millere2754432006-07-24 14:06:47 +10001837 fatal("%.200s line %d: Missing argument.", filename,
1838 linenum);
1839 len = strspn(cp, WHITESPACE);
1840 if (*activep && options->adm_forced_command == NULL)
1841 options->adm_forced_command = xstrdup(cp + len);
1842 return 0;
1843
Damien Millerd8cb1f12008-02-10 22:40:12 +11001844 case sChrootDirectory:
1845 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001846
1847 arg = strdelim(&cp);
1848 if (!arg || *arg == '\0')
1849 fatal("%s line %d: missing file name.",
1850 filename, linenum);
1851 if (*activep && *charptr == NULL)
1852 *charptr = xstrdup(arg);
1853 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001854
Damien Miller1aed65e2010-03-04 21:53:35 +11001855 case sTrustedUserCAKeys:
1856 charptr = &options->trusted_user_ca_keys;
1857 goto parse_filename;
1858
1859 case sRevokedKeys:
1860 charptr = &options->revoked_keys_file;
1861 goto parse_filename;
1862
Damien Miller0dac6fb2010-11-20 15:19:38 +11001863 case sIPQoS:
1864 arg = strdelim(&cp);
1865 if ((value = parse_ipqos(arg)) == -1)
1866 fatal("%s line %d: Bad IPQoS value: %s",
1867 filename, linenum, arg);
1868 arg = strdelim(&cp);
1869 if (arg == NULL)
1870 value2 = value;
1871 else if ((value2 = parse_ipqos(arg)) == -1)
1872 fatal("%s line %d: Bad IPQoS value: %s",
1873 filename, linenum, arg);
1874 if (*activep) {
1875 options->ip_qos_interactive = value;
1876 options->ip_qos_bulk = value2;
1877 }
1878 break;
1879
Damien Miller23528812012-04-22 11:24:43 +10001880 case sVersionAddendum:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001881 if (cp == NULL || *cp == '\0')
Damien Miller23528812012-04-22 11:24:43 +10001882 fatal("%.200s line %d: Missing argument.", filename,
1883 linenum);
1884 len = strspn(cp, WHITESPACE);
1885 if (*activep && options->version_addendum == NULL) {
1886 if (strcasecmp(cp + len, "none") == 0)
1887 options->version_addendum = xstrdup("");
1888 else if (strchr(cp + len, '\r') != NULL)
1889 fatal("%.200s line %d: Invalid argument",
1890 filename, linenum);
1891 else
1892 options->version_addendum = xstrdup(cp + len);
1893 }
1894 return 0;
1895
Damien Miller09d3e122012-10-31 08:58:58 +11001896 case sAuthorizedKeysCommand:
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001897 if (cp == NULL)
1898 fatal("%.200s line %d: Missing argument.", filename,
1899 linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001900 len = strspn(cp, WHITESPACE);
1901 if (*activep && options->authorized_keys_command == NULL) {
1902 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1903 fatal("%.200s line %d: AuthorizedKeysCommand "
1904 "must be an absolute path",
1905 filename, linenum);
1906 options->authorized_keys_command = xstrdup(cp + len);
1907 }
1908 return 0;
1909
1910 case sAuthorizedKeysCommandUser:
1911 charptr = &options->authorized_keys_command_user;
1912
1913 arg = strdelim(&cp);
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001914 if (!arg || *arg == '\0')
1915 fatal("%s line %d: missing AuthorizedKeysCommandUser "
1916 "argument.", filename, linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001917 if (*activep && *charptr == NULL)
1918 *charptr = xstrdup(arg);
1919 break;
1920
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +00001921 case sAuthorizedPrincipalsCommand:
1922 if (cp == NULL)
1923 fatal("%.200s line %d: Missing argument.", filename,
1924 linenum);
1925 len = strspn(cp, WHITESPACE);
1926 if (*activep &&
1927 options->authorized_principals_command == NULL) {
1928 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1929 fatal("%.200s line %d: "
1930 "AuthorizedPrincipalsCommand must be "
1931 "an absolute path", filename, linenum);
1932 options->authorized_principals_command =
1933 xstrdup(cp + len);
1934 }
1935 return 0;
1936
1937 case sAuthorizedPrincipalsCommandUser:
1938 charptr = &options->authorized_principals_command_user;
1939
1940 arg = strdelim(&cp);
1941 if (!arg || *arg == '\0')
1942 fatal("%s line %d: missing "
1943 "AuthorizedPrincipalsCommandUser argument.",
1944 filename, linenum);
1945 if (*activep && *charptr == NULL)
1946 *charptr = xstrdup(arg);
1947 break;
1948
Damien Millera6e3f012012-11-04 23:21:40 +11001949 case sAuthenticationMethods:
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001950 if (options->num_auth_methods == 0) {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001951 value = 0; /* seen "any" pseudo-method */
djm@openbsd.org46ecd192016-06-23 05:17:51 +00001952 value2 = 0; /* sucessfully parsed any method */
Damien Millera6e3f012012-11-04 23:21:40 +11001953 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001954 if (strcmp(arg, "any") == 0) {
1955 if (options->num_auth_methods > 0) {
1956 fatal("%s line %d: \"any\" "
1957 "must appear alone in "
1958 "AuthenticationMethods",
1959 filename, linenum);
1960 }
1961 value = 1;
1962 } else if (value) {
1963 fatal("%s line %d: \"any\" must appear "
1964 "alone in AuthenticationMethods",
1965 filename, linenum);
1966 } else if (auth2_methods_valid(arg, 0) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +11001967 fatal("%s line %d: invalid "
1968 "authentication method list.",
1969 filename, linenum);
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001970 }
djm@openbsd.org46ecd192016-06-23 05:17:51 +00001971 value2 = 1;
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001972 if (!*activep)
1973 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001974 array_append(filename, linenum,
1975 "AuthenticationMethods",
1976 &options->auth_methods,
1977 &options->num_auth_methods, arg);
Damien Millera6e3f012012-11-04 23:21:40 +11001978 }
djm@openbsd.org46ecd192016-06-23 05:17:51 +00001979 if (value2 == 0) {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001980 fatal("%s line %d: no AuthenticationMethods "
1981 "specified", filename, linenum);
1982 }
Damien Millera6e3f012012-11-04 23:21:40 +11001983 }
1984 return 0;
1985
Damien Miller7acefbb2014-07-18 14:11:24 +10001986 case sStreamLocalBindMask:
1987 arg = strdelim(&cp);
1988 if (!arg || *arg == '\0')
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001989 fatal("%s line %d: missing StreamLocalBindMask "
1990 "argument.", filename, linenum);
Damien Miller7acefbb2014-07-18 14:11:24 +10001991 /* Parse mode in octal format */
1992 value = strtol(arg, &p, 8);
1993 if (arg == p || value < 0 || value > 0777)
1994 fatal("%s line %d: Bad mask.", filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001995 if (*activep)
1996 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
Damien Miller7acefbb2014-07-18 14:11:24 +10001997 break;
1998
1999 case sStreamLocalBindUnlink:
2000 intptr = &options->fwd_opts.streamlocal_bind_unlink;
2001 goto parse_flag;
2002
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002003 case sFingerprintHash:
2004 arg = strdelim(&cp);
2005 if (!arg || *arg == '\0')
2006 fatal("%.200s line %d: Missing argument.",
2007 filename, linenum);
2008 if ((value = ssh_digest_alg_by_name(arg)) == -1)
2009 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
2010 filename, linenum, arg);
2011 if (*activep)
2012 options->fingerprint_hash = value;
2013 break;
2014
djm@openbsd.org8f574952017-06-24 06:34:38 +00002015 case sExposeAuthInfo:
2016 intptr = &options->expose_userauth_info;
2017 goto parse_flag;
2018
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002019 case sRDomain:
2020 charptr = &options->routing_domain;
2021 arg = strdelim(&cp);
2022 if (!arg || *arg == '\0')
2023 fatal("%.200s line %d: Missing argument.",
2024 filename, linenum);
2025 if (strcasecmp(arg, "none") != 0 && strcmp(arg, "%D") != 0 &&
2026 !valid_rdomain(arg))
2027 fatal("%s line %d: bad routing domain",
2028 filename, linenum);
2029 if (*activep && *charptr == NULL)
2030 *charptr = xstrdup(arg);
dtucker@openbsd.org168ecec2017-12-05 23:56:07 +00002031 break;
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002032
Ben Lindstromade03f62001-12-06 18:22:17 +00002033 case sDeprecated:
djm@openbsd.orgae363d72016-08-25 23:57:54 +00002034 case sIgnore:
Damien Millerf9b3feb2003-05-16 11:38:32 +10002035 case sUnsupported:
djm@openbsd.orgae363d72016-08-25 23:57:54 +00002036 do_log2(opcode == sIgnore ?
2037 SYSLOG_LEVEL_DEBUG2 : SYSLOG_LEVEL_INFO,
2038 "%s line %d: %s option %s", filename, linenum,
2039 opcode == sUnsupported ? "Unsupported" : "Deprecated", arg);
Damien Millerf9b3feb2003-05-16 11:38:32 +10002040 while (arg)
2041 arg = strdelim(&cp);
2042 break;
2043
Ben Lindstromade03f62001-12-06 18:22:17 +00002044 default:
2045 fatal("%s line %d: Missing handler for opcode %s (%d)",
2046 filename, linenum, arg, opcode);
2047 }
2048 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
2049 fatal("%s line %d: garbage at end of line; \"%.200s\".",
2050 filename, linenum, arg);
2051 return 0;
2052}
2053
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002054/* Reads the server configuration file. */
2055
Damien Miller4af51302000-04-16 11:18:38 +10002056void
Darren Tucker645ab752004-06-25 13:33:20 +10002057load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002058{
Damien Miller46cb75a2012-07-31 12:22:37 +10002059 char line[4096], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00002060 FILE *f;
Damien Miller46cb75a2012-07-31 12:22:37 +10002061 int lineno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002062
Darren Tucker645ab752004-06-25 13:33:20 +10002063 debug2("%s: filename %s", __func__, filename);
2064 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11002065 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002066 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11002067 }
Darren Tucker645ab752004-06-25 13:33:20 +10002068 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11002069 while (fgets(line, sizeof(line), f)) {
Damien Miller46cb75a2012-07-31 12:22:37 +10002070 lineno++;
2071 if (strlen(line) == sizeof(line) - 1)
2072 fatal("%s line %d too long", filename, lineno);
Darren Tucker645ab752004-06-25 13:33:20 +10002073 /*
2074 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10002075 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10002076 * line numbers later for error messages
2077 */
2078 if ((cp = strchr(line, '#')) != NULL)
2079 memcpy(cp, "\n", 2);
2080 cp = line + strspn(line, " \t\r");
2081
2082 buffer_append(conf, cp, strlen(cp));
2083 }
2084 buffer_append(conf, "\0", 1);
2085 fclose(f);
2086 debug2("%s: done config len = %d", __func__, buffer_len(conf));
2087}
2088
2089void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002090parse_server_match_config(ServerOptions *options,
2091 struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10002092{
Darren Tucker45150472006-07-12 22:34:17 +10002093 ServerOptions mo;
2094
2095 initialize_server_options(&mo);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002096 parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11002097 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10002098}
2099
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002100int parse_server_match_testspec(struct connection_info *ci, char *spec)
2101{
2102 char *p;
2103
2104 while ((p = strsep(&spec, ",")) && *p != '\0') {
2105 if (strncmp(p, "addr=", 5) == 0) {
2106 ci->address = xstrdup(p + 5);
2107 } else if (strncmp(p, "host=", 5) == 0) {
2108 ci->host = xstrdup(p + 5);
2109 } else if (strncmp(p, "user=", 5) == 0) {
2110 ci->user = xstrdup(p + 5);
2111 } else if (strncmp(p, "laddr=", 6) == 0) {
2112 ci->laddress = xstrdup(p + 6);
djm@openbsd.org68af80e2017-10-25 00:19:47 +00002113 } else if (strncmp(p, "rdomain=", 8) == 0) {
2114 ci->rdomain = xstrdup(p + 8);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002115 } else if (strncmp(p, "lport=", 6) == 0) {
2116 ci->lport = a2port(p + 6);
2117 if (ci->lport == -1) {
2118 fprintf(stderr, "Invalid port '%s' in test mode"
2119 " specification %s\n", p+6, p);
2120 return -1;
2121 }
2122 } else {
2123 fprintf(stderr, "Invalid test mode specification %s\n",
2124 p);
2125 return -1;
2126 }
2127 }
2128 return 0;
2129}
2130
2131/*
Darren Tucker1629c072007-02-19 22:25:37 +11002132 * Copy any supported values that are set.
2133 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10002134 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11002135 * array values that are not used pre-authentication, because any that we
2136 * do use must be explictly sent in mm_getpwnamallow().
2137 */
Darren Tucker45150472006-07-12 22:34:17 +10002138void
Darren Tucker1629c072007-02-19 22:25:37 +11002139copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10002140{
Damien Miller534b2cc2013-12-05 14:07:27 +11002141#define M_CP_INTOPT(n) do {\
2142 if (src->n != -1) \
2143 dst->n = src->n; \
2144} while (0)
2145
Darren Tucker1629c072007-02-19 22:25:37 +11002146 M_CP_INTOPT(password_authentication);
2147 M_CP_INTOPT(gss_authentication);
Darren Tucker1629c072007-02-19 22:25:37 +11002148 M_CP_INTOPT(pubkey_authentication);
2149 M_CP_INTOPT(kerberos_authentication);
2150 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10002151 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11002152 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11002153 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11002154 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11002155
2156 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002157 M_CP_INTOPT(allow_streamlocal_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10002158 M_CP_INTOPT(allow_agent_forwarding);
djm@openbsd.org7844f352016-11-30 03:00:05 +00002159 M_CP_INTOPT(disable_forwarding);
djm@openbsd.org8f574952017-06-24 06:34:38 +00002160 M_CP_INTOPT(expose_userauth_info);
Damien Millerab6de352010-06-26 09:38:45 +10002161 M_CP_INTOPT(permit_tun);
Damien Miller7acefbb2014-07-18 14:11:24 +10002162 M_CP_INTOPT(fwd_opts.gateway_ports);
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002163 M_CP_INTOPT(fwd_opts.streamlocal_bind_unlink);
Darren Tucker1629c072007-02-19 22:25:37 +11002164 M_CP_INTOPT(x11_display_offset);
2165 M_CP_INTOPT(x11_forwarding);
2166 M_CP_INTOPT(x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002167 M_CP_INTOPT(permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002168 M_CP_INTOPT(permit_user_rc);
Damien Miller7207f642008-05-19 15:34:50 +10002169 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10002170 M_CP_INTOPT(max_authtries);
markus@openbsd.orgf0ddede2016-11-23 23:14:15 +00002171 M_CP_INTOPT(client_alive_count_max);
2172 M_CP_INTOPT(client_alive_interval);
Damien Miller0dac6fb2010-11-20 15:19:38 +11002173 M_CP_INTOPT(ip_qos_interactive);
2174 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002175 M_CP_INTOPT(rekey_limit);
2176 M_CP_INTOPT(rekey_interval);
djm@openbsd.org54cd41a2017-05-17 01:24:17 +00002177 M_CP_INTOPT(log_level);
Darren Tucker1629c072007-02-19 22:25:37 +11002178
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002179 /*
2180 * The bind_mask is a mode_t that may be unsigned, so we can't use
2181 * M_CP_INTOPT - it does a signed comparison that causes compiler
2182 * warnings.
2183 */
dtucker@openbsd.org9faae502016-05-04 14:00:09 +00002184 if (src->fwd_opts.streamlocal_bind_mask != (mode_t)-1) {
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002185 dst->fwd_opts.streamlocal_bind_mask =
2186 src->fwd_opts.streamlocal_bind_mask;
2187 }
2188
Damien Miller534b2cc2013-12-05 14:07:27 +11002189 /* M_CP_STROPT and M_CP_STRARRAYOPT should not appear before here */
2190#define M_CP_STROPT(n) do {\
2191 if (src->n != NULL && dst->n != src->n) { \
2192 free(dst->n); \
2193 dst->n = src->n; \
2194 } \
2195} while(0)
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00002196#define M_CP_STRARRAYOPT(s, num_s) do {\
2197 u_int i; \
2198 if (src->num_s != 0) { \
2199 for (i = 0; i < dst->num_s; i++) \
2200 free(dst->s[i]); \
2201 free(dst->s); \
2202 dst->s = xcalloc(src->num_s, sizeof(*dst->s)); \
2203 for (i = 0; i < src->num_s; i++) \
2204 dst->s[i] = xstrdup(src->s[i]); \
2205 dst->num_s = src->num_s; \
djm@openbsd.org66bf74a2017-10-02 19:33:20 +00002206 } \
2207} while(0)
Damien Miller534b2cc2013-12-05 14:07:27 +11002208
Damien Millerf2e407e2011-05-20 19:04:14 +10002209 /* See comment in servconf.h */
2210 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10002211
djm@openbsd.orged085102015-10-29 08:05:01 +00002212 /* Arguments that accept '+...' need to be expanded */
2213 assemble_algorithms(dst);
2214
Damien Millerc2411902011-05-20 19:03:49 +10002215 /*
2216 * The only things that should be below this point are string options
2217 * which are only used after authentication.
2218 */
Damien Miller5d74e582011-05-20 19:03:31 +10002219 if (preauth)
2220 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002221
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002222 /* These options may be "none" to clear a global setting */
Damien Miller5d74e582011-05-20 19:03:31 +10002223 M_CP_STROPT(adm_forced_command);
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002224 if (option_clear_or_none(dst->adm_forced_command)) {
2225 free(dst->adm_forced_command);
2226 dst->adm_forced_command = NULL;
2227 }
Damien Miller5d74e582011-05-20 19:03:31 +10002228 M_CP_STROPT(chroot_directory);
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002229 if (option_clear_or_none(dst->chroot_directory)) {
2230 free(dst->chroot_directory);
2231 dst->chroot_directory = NULL;
2232 }
Darren Tucker45150472006-07-12 22:34:17 +10002233}
2234
Darren Tucker1629c072007-02-19 22:25:37 +11002235#undef M_CP_INTOPT
2236#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10002237#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11002238
Darren Tucker45150472006-07-12 22:34:17 +10002239void
2240parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002241 struct connection_info *connectinfo)
Darren Tucker45150472006-07-12 22:34:17 +10002242{
2243 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10002244 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10002245
2246 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
2247
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002248 if ((obuf = cbuf = sshbuf_dup_string(conf)) == NULL)
2249 fatal("%s: sshbuf_dup_string failed", __func__);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002250 active = connectinfo ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10002251 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11002252 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10002253 if (process_server_config_line(options, cp, filename,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002254 linenum++, &active, connectinfo) != 0)
Damien Miller95def091999-11-25 00:26:21 +11002255 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002256 }
Darren Tuckera627d422013-06-02 07:31:17 +10002257 free(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00002258 if (bad_options > 0)
2259 fatal("%s: terminating, %d bad configuration options",
2260 filename, bad_options);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00002261 process_queued_listen_addrs(options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002262}
Darren Tuckere7140f22008-06-10 23:01:51 +10002263
2264static const char *
Damien Miller82c55872011-06-23 08:20:30 +10002265fmt_multistate_int(int val, const struct multistate *m)
2266{
2267 u_int i;
2268
2269 for (i = 0; m[i].key != NULL; i++) {
2270 if (m[i].value == val)
2271 return m[i].key;
2272 }
2273 return "UNKNOWN";
2274}
2275
2276static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10002277fmt_intarg(ServerOpCodes code, int val)
2278{
Damien Miller82c55872011-06-23 08:20:30 +10002279 if (val == -1)
2280 return "unset";
2281 switch (code) {
2282 case sAddressFamily:
2283 return fmt_multistate_int(val, multistate_addressfamily);
2284 case sPermitRootLogin:
2285 return fmt_multistate_int(val, multistate_permitrootlogin);
2286 case sGatewayPorts:
2287 return fmt_multistate_int(val, multistate_gatewayports);
2288 case sCompression:
2289 return fmt_multistate_int(val, multistate_compression);
Damien Milleraa5b3f82012-12-03 09:50:54 +11002290 case sAllowTcpForwarding:
2291 return fmt_multistate_int(val, multistate_tcpfwd);
Damien Miller7acefbb2014-07-18 14:11:24 +10002292 case sAllowStreamLocalForwarding:
2293 return fmt_multistate_int(val, multistate_tcpfwd);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002294 case sFingerprintHash:
2295 return ssh_digest_alg_name(val);
Damien Miller82c55872011-06-23 08:20:30 +10002296 default:
2297 switch (val) {
2298 case 0:
2299 return "no";
2300 case 1:
2301 return "yes";
2302 default:
2303 return "UNKNOWN";
2304 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002305 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002306}
2307
2308static const char *
2309lookup_opcode_name(ServerOpCodes code)
2310{
2311 u_int i;
2312
2313 for (i = 0; keywords[i].name != NULL; i++)
2314 if (keywords[i].opcode == code)
2315 return(keywords[i].name);
2316 return "UNKNOWN";
2317}
2318
2319static void
2320dump_cfg_int(ServerOpCodes code, int val)
2321{
2322 printf("%s %d\n", lookup_opcode_name(code), val);
2323}
2324
2325static void
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002326dump_cfg_oct(ServerOpCodes code, int val)
2327{
2328 printf("%s 0%o\n", lookup_opcode_name(code), val);
2329}
2330
2331static void
Darren Tuckere7140f22008-06-10 23:01:51 +10002332dump_cfg_fmtint(ServerOpCodes code, int val)
2333{
2334 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2335}
2336
2337static void
2338dump_cfg_string(ServerOpCodes code, const char *val)
2339{
djm@openbsd.org161cf412014-12-22 07:55:51 +00002340 printf("%s %s\n", lookup_opcode_name(code),
2341 val == NULL ? "none" : val);
Darren Tuckere7140f22008-06-10 23:01:51 +10002342}
2343
2344static void
2345dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
2346{
2347 u_int i;
2348
2349 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10002350 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2351}
2352
2353static void
2354dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
2355{
2356 u_int i;
2357
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002358 if (count <= 0 && code != sAuthenticationMethods)
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002359 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002360 printf("%s", lookup_opcode_name(code));
2361 for (i = 0; i < count; i++)
2362 printf(" %s", vals[i]);
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002363 if (code == sAuthenticationMethods && count == 0)
2364 printf(" any");
Damien Millerd8478b62011-05-29 21:39:36 +10002365 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002366}
2367
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002368static char *
2369format_listen_addrs(struct listenaddr *la)
Darren Tuckere7140f22008-06-10 23:01:51 +10002370{
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002371 int r;
Darren Tuckere7140f22008-06-10 23:01:51 +10002372 struct addrinfo *ai;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002373 char addr[NI_MAXHOST], port[NI_MAXSERV];
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002374 char *laddr1 = xstrdup(""), *laddr2 = NULL;
Darren Tuckere7140f22008-06-10 23:01:51 +10002375
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002376 /*
2377 * ListenAddress must be after Port. add_one_listen_addr pushes
2378 * addresses onto a stack, so to maintain ordering we need to
2379 * print these in reverse order.
2380 */
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002381 for (ai = la->addrs; ai; ai = ai->ai_next) {
2382 if ((r = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
Darren Tuckere7140f22008-06-10 23:01:51 +10002383 sizeof(addr), port, sizeof(port),
2384 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002385 error("getnameinfo: %.100s", ssh_gai_strerror(r));
2386 continue;
Darren Tuckere7140f22008-06-10 23:01:51 +10002387 }
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002388 laddr2 = laddr1;
2389 if (ai->ai_family == AF_INET6) {
2390 xasprintf(&laddr1, "listenaddress [%s]:%s%s%s\n%s",
2391 addr, port,
2392 la->rdomain == NULL ? "" : " rdomain ",
2393 la->rdomain == NULL ? "" : la->rdomain,
2394 laddr2);
2395 } else {
2396 xasprintf(&laddr1, "listenaddress %s:%s%s%s\n%s",
2397 addr, port,
2398 la->rdomain == NULL ? "" : " rdomain ",
2399 la->rdomain == NULL ? "" : la->rdomain,
2400 laddr2);
2401 }
2402 free(laddr2);
Darren Tuckere7140f22008-06-10 23:01:51 +10002403 }
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002404 return laddr1;
2405}
2406
2407void
2408dump_config(ServerOptions *o)
2409{
2410 char *s;
2411 u_int i;
2412
2413 /* these are usually at the top of the config */
2414 for (i = 0; i < o->num_ports; i++)
2415 printf("port %d\n", o->ports[i]);
2416 dump_cfg_fmtint(sAddressFamily, o->address_family);
2417
2418 for (i = 0; i < o->num_listen_addrs; i++) {
2419 s = format_listen_addrs(&o->listen_addrs[i]);
2420 printf("%s", s);
2421 free(s);
2422 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002423
2424 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10002425#ifdef USE_PAM
Darren Tucker70860b62015-04-17 10:56:13 +10002426 dump_cfg_fmtint(sUsePAM, o->use_pam);
Damien Miller212f0b02008-07-23 17:42:29 +10002427#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002428 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
Darren Tuckere7140f22008-06-10 23:01:51 +10002429 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
2430 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11002431 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10002432 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
2433 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002434 dump_cfg_oct(sStreamLocalBindMask, o->fwd_opts.streamlocal_bind_mask);
Darren Tuckere7140f22008-06-10 23:01:51 +10002435
2436 /* formatted integer arguments */
2437 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
2438 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
2439 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
Darren Tuckere7140f22008-06-10 23:01:51 +10002440 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
2441 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
2442 o->hostbased_uses_name_from_packet_only);
Darren Tuckere7140f22008-06-10 23:01:51 +10002443 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10002444#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10002445 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
2446 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
2447 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10002448# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10002449 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10002450# endif
2451#endif
2452#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10002453 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2454 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10002455#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002456 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2457 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2458 o->kbd_interactive_authentication);
2459 dump_cfg_fmtint(sChallengeResponseAuthentication,
2460 o->challenge_response_authentication);
2461 dump_cfg_fmtint(sPrintMotd, o->print_motd);
Darren Tuckerfd4e4f22016-02-24 10:44:25 +11002462#ifndef DISABLE_LASTLOG
Darren Tuckere7140f22008-06-10 23:01:51 +10002463 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
Darren Tuckerfd4e4f22016-02-24 10:44:25 +11002464#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002465 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
2466 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002467 dump_cfg_fmtint(sPermitTTY, o->permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002468 dump_cfg_fmtint(sPermitUserRC, o->permit_user_rc);
Darren Tuckere7140f22008-06-10 23:01:51 +10002469 dump_cfg_fmtint(sStrictModes, o->strict_modes);
2470 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
2471 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
2472 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
Darren Tuckere7140f22008-06-10 23:01:51 +10002473 dump_cfg_fmtint(sCompression, o->compression);
Damien Miller7acefbb2014-07-18 14:11:24 +10002474 dump_cfg_fmtint(sGatewayPorts, o->fwd_opts.gateway_ports);
Darren Tuckere7140f22008-06-10 23:01:51 +10002475 dump_cfg_fmtint(sUseDNS, o->use_dns);
2476 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002477 dump_cfg_fmtint(sAllowAgentForwarding, o->allow_agent_forwarding);
djm@openbsd.org7844f352016-11-30 03:00:05 +00002478 dump_cfg_fmtint(sDisableForwarding, o->disable_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002479 dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
djm@openbsd.org771c2f52016-05-03 15:25:06 +00002480 dump_cfg_fmtint(sStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002481 dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
djm@openbsd.org8f574952017-06-24 06:34:38 +00002482 dump_cfg_fmtint(sExposeAuthInfo, o->expose_userauth_info);
Darren Tuckere7140f22008-06-10 23:01:51 +10002483
2484 /* string arguments */
2485 dump_cfg_string(sPidFile, o->pid_file);
2486 dump_cfg_string(sXAuthLocation, o->xauth_location);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002487 dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
2488 dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
Darren Tuckere7140f22008-06-10 23:01:51 +10002489 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10002490 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11002491 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11002492 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
2493 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10002494 dump_cfg_string(sAuthorizedPrincipalsFile,
2495 o->authorized_principals_file);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002496 dump_cfg_string(sVersionAddendum, *o->version_addendum == '\0'
2497 ? "none" : o->version_addendum);
Damien Miller09d3e122012-10-31 08:58:58 +11002498 dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
2499 dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +00002500 dump_cfg_string(sAuthorizedPrincipalsCommand, o->authorized_principals_command);
2501 dump_cfg_string(sAuthorizedPrincipalsCommandUser, o->authorized_principals_command_user);
Damien Miller85b45e02013-07-20 13:21:52 +10002502 dump_cfg_string(sHostKeyAgent, o->host_key_agent);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002503 dump_cfg_string(sKexAlgorithms,
djm@openbsd.org259a02e2014-10-13 00:38:35 +00002504 o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002505 dump_cfg_string(sHostbasedAcceptedKeyTypes, o->hostbased_key_types ?
2506 o->hostbased_key_types : KEX_DEFAULT_PK_ALG);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002507 dump_cfg_string(sHostKeyAlgorithms, o->hostkeyalgorithms ?
2508 o->hostkeyalgorithms : KEX_DEFAULT_PK_ALG);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002509 dump_cfg_string(sPubkeyAcceptedKeyTypes, o->pubkey_key_types ?
2510 o->pubkey_key_types : KEX_DEFAULT_PK_ALG);
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002511 dump_cfg_string(sRDomain, o->routing_domain);
Darren Tuckere7140f22008-06-10 23:01:51 +10002512
2513 /* string arguments requiring a lookup */
2514 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
2515 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
2516
2517 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10002518 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
2519 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002520 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
2521 o->host_key_files);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002522 dump_cfg_strarray(sHostCertificate, o->num_host_cert_files,
Damien Miller0a80ca12010-02-27 07:55:05 +11002523 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002524 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
2525 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
2526 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
2527 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
2528 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
Damien Millera6e3f012012-11-04 23:21:40 +11002529 dump_cfg_strarray_oneline(sAuthenticationMethods,
2530 o->num_auth_methods, o->auth_methods);
Darren Tuckere7140f22008-06-10 23:01:51 +10002531
2532 /* other arguments */
2533 for (i = 0; i < o->num_subsystems; i++)
2534 printf("subsystem %s %s\n", o->subsystem_name[i],
2535 o->subsystem_args[i]);
2536
2537 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
2538 o->max_startups_rate, o->max_startups);
2539
djm@openbsd.orgd685e5a2017-10-25 02:10:39 +00002540 s = NULL;
2541 for (i = 0; tunmode_desc[i].val != -1; i++) {
Darren Tuckere7140f22008-06-10 23:01:51 +10002542 if (tunmode_desc[i].val == o->permit_tun) {
2543 s = tunmode_desc[i].text;
2544 break;
2545 }
djm@openbsd.orgd685e5a2017-10-25 02:10:39 +00002546 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002547 dump_cfg_string(sPermitTunnel, s);
2548
Damien Miller91475862011-05-05 14:14:34 +10002549 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2550 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11002551
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002552 printf("rekeylimit %llu %d\n", (unsigned long long)o->rekey_limit,
Damien Millera6d6c1f2013-08-21 02:40:01 +10002553 o->rekey_interval);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002554
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002555 printf("permitopen");
2556 if (o->num_permitted_opens == 0)
2557 printf(" any");
2558 else {
2559 for (i = 0; i < o->num_permitted_opens; i++)
2560 printf(" %s", o->permitted_opens[i]);
2561 }
2562 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002563}