blob: 0f0d090686b165652e0b81ce45581cdc6ac9d6f7 [file] [log] [blame]
djm@openbsd.org868109b2015-07-01 02:39:06 +00001
markus@openbsd.org5886b922018-03-01 20:32:16 +00002/* $OpenBSD: servconf.c,v 1.326 2018/03/01 20:32:16 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.org5886b922018-03-01 20:32:16 +0000256#ifdef WITH_XMSS
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000257 servconf_add_hostkey("[default]", 0, options,
258 _PATH_HOST_XMSS_KEY_FILE);
markus@openbsd.org5886b922018-03-01 20:32:16 +0000259#endif /* WITH_XMSS */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100260 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100261 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100262 if (options->num_ports == 0)
263 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000264 if (options->address_family == -1)
265 options->address_family = AF_UNSPEC;
Damien Miller34132e52000-01-14 15:45:46 +1100266 if (options->listen_addrs == NULL)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000267 add_listen_addr(options, NULL, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000268 if (options->pid_file == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000269 options->pid_file = xstrdup(_PATH_SSH_DAEMON_PID_FILE);
Damien Miller95def091999-11-25 00:26:21 +1100270 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000271 options->login_grace_time = 120;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000272 if (options->permit_root_login == PERMIT_NOT_SET)
chris@openbsd.org3d5728a2015-07-31 15:38:09 +0000273 options->permit_root_login = PERMIT_NO_PASSWD;
Damien Miller95def091999-11-25 00:26:21 +1100274 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100275 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100276 if (options->ignore_user_known_hosts == -1)
277 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100278 if (options->print_motd == -1)
279 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000280 if (options->print_lastlog == -1)
281 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100282 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100283 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100284 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100285 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100286 if (options->x11_use_localhost == -1)
287 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000288 if (options->xauth_location == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000289 options->xauth_location = xstrdup(_PATH_XAUTH);
Damien Miller5ff30c62013-10-30 22:21:50 +1100290 if (options->permit_tty == -1)
291 options->permit_tty = 1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000292 if (options->permit_user_rc == -1)
293 options->permit_user_rc = 1;
Damien Miller95def091999-11-25 00:26:21 +1100294 if (options->strict_modes == -1)
295 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100296 if (options->tcp_keep_alive == -1)
297 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100298 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100299 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100300 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000301 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000302 if (options->hostbased_authentication == -1)
303 options->hostbased_authentication = 0;
304 if (options->hostbased_uses_name_from_packet_only == -1)
305 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100306 if (options->pubkey_authentication == -1)
307 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100308 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000309 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100310 if (options->kerberos_or_local_passwd == -1)
311 options->kerberos_or_local_passwd = 1;
312 if (options->kerberos_ticket_cleanup == -1)
313 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100314 if (options->kerberos_get_afs_token == -1)
315 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000316 if (options->gss_authentication == -1)
317 options->gss_authentication = 0;
318 if (options->gss_cleanup_creds == -1)
319 options->gss_cleanup_creds = 1;
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000320 if (options->gss_strict_acceptor == -1)
djm@openbsd.org13bd2e22017-01-06 03:45:41 +0000321 options->gss_strict_acceptor = 1;
Damien Miller95def091999-11-25 00:26:21 +1100322 if (options->password_authentication == -1)
323 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100324 if (options->kbd_interactive_authentication == -1)
325 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000326 if (options->challenge_response_authentication == -1)
327 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100328 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100329 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000330 if (options->permit_user_env == -1)
331 options->permit_user_env = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000332 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000333 options->compression = COMP_DELAYED;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000334 if (options->rekey_limit == -1)
335 options->rekey_limit = 0;
336 if (options->rekey_interval == -1)
337 options->rekey_interval = 0;
Damien Miller50a41ed2000-10-16 12:14:42 +1100338 if (options->allow_tcp_forwarding == -1)
Damien Milleraa5b3f82012-12-03 09:50:54 +1100339 options->allow_tcp_forwarding = FORWARD_ALLOW;
Damien Miller7acefbb2014-07-18 14:11:24 +1000340 if (options->allow_streamlocal_forwarding == -1)
341 options->allow_streamlocal_forwarding = FORWARD_ALLOW;
Damien Miller4f755cd2008-05-19 14:57:41 +1000342 if (options->allow_agent_forwarding == -1)
343 options->allow_agent_forwarding = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000344 if (options->fwd_opts.gateway_ports == -1)
345 options->fwd_opts.gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000346 if (options->max_startups == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100347 options->max_startups = 100;
Damien Miller942da032000-08-18 13:59:06 +1000348 if (options->max_startups_rate == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100349 options->max_startups_rate = 30; /* 30% */
Damien Miller942da032000-08-18 13:59:06 +1000350 if (options->max_startups_begin == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100351 options->max_startups_begin = 10;
Darren Tucker89413db2004-05-24 10:36:23 +1000352 if (options->max_authtries == -1)
353 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000354 if (options->max_sessions == -1)
355 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000356 if (options->use_dns == -1)
deraadt@openbsd.org3cd51032015-02-02 01:57:44 +0000357 options->use_dns = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000358 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100359 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000360 if (options->client_alive_count_max == -1)
361 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000362 if (options->num_authkeys_files == 0) {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000363 array_append("[default]", 0, "AuthorizedKeysFiles",
364 &options->authorized_keys_files,
365 &options->num_authkeys_files,
366 _PATH_SSH_USER_PERMITTED_KEYS);
367 array_append("[default]", 0, "AuthorizedKeysFiles",
368 &options->authorized_keys_files,
369 &options->num_authkeys_files,
370 _PATH_SSH_USER_PERMITTED_KEYS2);
Damien Millerd8478b62011-05-29 21:39:36 +1000371 }
Damien Millerd27b9472005-12-13 19:29:02 +1100372 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100373 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100374 if (options->ip_qos_interactive == -1)
375 options->ip_qos_interactive = IPTOS_LOWDELAY;
376 if (options->ip_qos_bulk == -1)
377 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller23528812012-04-22 11:24:43 +1000378 if (options->version_addendum == NULL)
379 options->version_addendum = xstrdup("");
Damien Miller7acefbb2014-07-18 14:11:24 +1000380 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
381 options->fwd_opts.streamlocal_bind_mask = 0177;
382 if (options->fwd_opts.streamlocal_bind_unlink == -1)
383 options->fwd_opts.streamlocal_bind_unlink = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000384 if (options->fingerprint_hash == -1)
385 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org7844f352016-11-30 03:00:05 +0000386 if (options->disable_forwarding == -1)
387 options->disable_forwarding = 0;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000388 if (options->expose_userauth_info == -1)
389 options->expose_userauth_info = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000390
djm@openbsd.orged085102015-10-29 08:05:01 +0000391 assemble_algorithms(options);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000392
djm@openbsd.orgc5c3f322016-02-17 05:29:04 +0000393 /* Turn privilege separation and sandboxing on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000394 if (use_privsep == -1)
djm@openbsd.orgc5c3f322016-02-17 05:29:04 +0000395 use_privsep = PRIVSEP_ON;
Damien Miller4903eb42002-06-21 16:20:44 +1000396
djm@openbsd.org161cf412014-12-22 07:55:51 +0000397#define CLEAR_ON_NONE(v) \
398 do { \
399 if (option_clear_or_none(v)) { \
400 free(v); \
401 v = NULL; \
402 } \
403 } while(0)
404 CLEAR_ON_NONE(options->pid_file);
405 CLEAR_ON_NONE(options->xauth_location);
406 CLEAR_ON_NONE(options->banner);
407 CLEAR_ON_NONE(options->trusted_user_ca_keys);
408 CLEAR_ON_NONE(options->revoked_keys_file);
djm@openbsd.org7e8528c2015-05-01 04:17:51 +0000409 CLEAR_ON_NONE(options->authorized_principals_file);
djm@openbsd.org9fd04682015-11-13 04:38:06 +0000410 CLEAR_ON_NONE(options->adm_forced_command);
411 CLEAR_ON_NONE(options->chroot_directory);
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000412 CLEAR_ON_NONE(options->routing_domain);
djm@openbsd.org161cf412014-12-22 07:55:51 +0000413 for (i = 0; i < options->num_host_key_files; i++)
414 CLEAR_ON_NONE(options->host_key_files[i]);
415 for (i = 0; i < options->num_host_cert_files; i++)
416 CLEAR_ON_NONE(options->host_cert_files[i]);
417#undef CLEAR_ON_NONE
418
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +0000419 /* Similar handling for AuthenticationMethods=any */
420 if (options->num_auth_methods == 1 &&
421 strcmp(options->auth_methods[0], "any") == 0) {
422 free(options->auth_methods[0]);
423 options->auth_methods[0] = NULL;
424 options->num_auth_methods = 0;
425 }
426
Tim Rice40017b02002-07-14 13:36:49 -0700427#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000428 if (use_privsep && options->compression == 1) {
429 error("This platform does not support both privilege "
430 "separation and compression");
431 error("Compression disabled");
432 options->compression = 0;
433 }
434#endif
435
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436}
437
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100439typedef enum {
440 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100441 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000442 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100443 /* Standard Options */
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000444 sPort, sHostKeyFile, sLoginGraceTime,
445 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000446 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100447 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
dtucker@openbsd.org745771f2018-02-09 02:37:36 +0000448 sKerberosGetAFSToken, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100449 sPasswordAuthentication, sKbdInteractiveAuthentication,
450 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000451 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100452 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller5ff30c62013-10-30 22:21:50 +1100453 sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
djm@openbsd.org83b58182016-08-19 03:18:06 +0000454 sPermitUserEnvironment, sAllowTcpForwarding, sCompression,
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000455 sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000456 sIgnoreUserKnownHosts, sCiphers, sMacs, sPidFile,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000457 sGatewayPorts, sPubkeyAuthentication, sPubkeyAcceptedKeyTypes,
458 sXAuthLocation, sSubsystem, sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000459 sBanner, sUseDNS, sHostbasedAuthentication,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000460 sHostbasedUsesNameFromPacketOnly, sHostbasedAcceptedKeyTypes,
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000461 sHostKeyAlgorithms,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000462 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000463 sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
464 sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100465 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100466 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller7cc194f2014-02-04 11:12:56 +1100467 sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000468 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000469 sAuthorizedPrincipalsCommand, sAuthorizedPrincipalsCommandUser,
Damien Miller23528812012-04-22 11:24:43 +1000470 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Miller09d3e122012-10-31 08:58:58 +1100471 sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
Damien Miller72e6b5c2014-07-04 09:00:04 +1000472 sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
Damien Miller7acefbb2014-07-18 14:11:24 +1000473 sStreamLocalBindMask, sStreamLocalBindUnlink,
djm@openbsd.org7844f352016-11-30 03:00:05 +0000474 sAllowStreamLocalForwarding, sFingerprintHash, sDisableForwarding,
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000475 sExposeAuthInfo, sRDomain,
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000476 sDeprecated, sIgnore, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477} ServerOpCodes;
478
Darren Tucker45150472006-07-12 22:34:17 +1000479#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
480#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
481#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
482
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100484static struct {
485 const char *name;
486 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000487 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100488} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100489 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000490#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000491 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000492#else
Darren Tucker45150472006-07-12 22:34:17 +1000493 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000494#endif
Darren Tucker45150472006-07-12 22:34:17 +1000495 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100496 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000497 { "port", sPort, SSHCFG_GLOBAL },
498 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
499 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
Damien Miller85b45e02013-07-20 13:21:52 +1000500 { "hostkeyagent", sHostKeyAgent, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000501 { "pidfile", sPidFile, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000502 { "serverkeybits", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000503 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000504 { "keyregenerationinterval", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100505 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000506 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
djm@openbsd.org54cd41a2017-05-17 01:24:17 +0000507 { "loglevel", sLogLevel, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000508 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000509 { "rhostsrsaauthentication", sDeprecated, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100510 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000511 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000512 { "hostbasedacceptedkeytypes", sHostbasedAcceptedKeyTypes, SSHCFG_ALL },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000513 { "hostkeyalgorithms", sHostKeyAlgorithms, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000514 { "rsaauthentication", sDeprecated, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100515 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000516 { "pubkeyacceptedkeytypes", sPubkeyAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000517 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000518#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100519 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000520 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
521 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100522#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000523 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000524#else
Darren Tucker45150472006-07-12 22:34:17 +1000525 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100526#endif
527#else
Darren Tucker1629c072007-02-19 22:25:37 +1100528 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000529 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
530 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
531 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000532#endif
Darren Tucker45150472006-07-12 22:34:17 +1000533 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
534 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000535#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100536 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000537 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000538 { "gssapistrictacceptorcheck", sGssStrictAcceptor, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000539#else
Darren Tucker1629c072007-02-19 22:25:37 +1100540 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000541 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000542 { "gssapistrictacceptorcheck", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000543#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100544 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
545 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100546 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000547 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
548 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
549 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
550 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
551 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
Damien Millerac908c12015-10-22 09:35:24 +1100552#ifdef DISABLE_LASTLOG
553 { "printlastlog", sUnsupported, SSHCFG_GLOBAL },
554#else
Darren Tucker45150472006-07-12 22:34:17 +1000555 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
Damien Millerac908c12015-10-22 09:35:24 +1100556#endif
Darren Tucker45150472006-07-12 22:34:17 +1000557 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
558 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000559 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
560 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
561 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000562 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
563 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100564 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000565 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
djm@openbsd.org83b58182016-08-19 03:18:06 +0000566 { "uselogin", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000567 { "compression", sCompression, SSHCFG_GLOBAL },
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000568 { "rekeylimit", sRekeyLimit, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000569 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
570 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
571 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000572 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Damien Millerc24da772012-06-20 21:53:58 +1000573 { "allowusers", sAllowUsers, SSHCFG_ALL },
574 { "denyusers", sDenyUsers, SSHCFG_ALL },
575 { "allowgroups", sAllowGroups, SSHCFG_ALL },
576 { "denygroups", sDenyGroups, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000577 { "ciphers", sCiphers, SSHCFG_GLOBAL },
578 { "macs", sMacs, SSHCFG_GLOBAL },
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000579 { "protocol", sIgnore, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000580 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
581 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
582 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000583 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000584 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100585 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000586 { "usedns", sUseDNS, SSHCFG_GLOBAL },
587 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
588 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
markus@openbsd.orgf0ddede2016-11-23 23:14:15 +0000589 { "clientaliveinterval", sClientAliveInterval, SSHCFG_ALL },
590 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000591 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000592 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
djm@openbsd.org66705942017-03-14 07:19:07 +0000593 { "useprivilegeseparation", sDeprecated, SSHCFG_GLOBAL},
Damien Millerc24da772012-06-20 21:53:58 +1000594 { "acceptenv", sAcceptEnv, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000595 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Damien Miller5ff30c62013-10-30 22:21:50 +1100596 { "permittty", sPermitTTY, SSHCFG_ALL },
Damien Miller72e6b5c2014-07-04 09:00:04 +1000597 { "permituserrc", sPermitUserRC, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000598 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000599 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000600 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100601 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100602 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100603 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
604 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000605 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000606 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100607 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller09d3e122012-10-31 08:58:58 +1100608 { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
609 { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000610 { "authorizedprincipalscommand", sAuthorizedPrincipalsCommand, SSHCFG_ALL },
611 { "authorizedprincipalscommanduser", sAuthorizedPrincipalsCommandUser, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000612 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Damien Millera6e3f012012-11-04 23:21:40 +1100613 { "authenticationmethods", sAuthenticationMethods, SSHCFG_ALL },
Damien Miller7acefbb2014-07-18 14:11:24 +1000614 { "streamlocalbindmask", sStreamLocalBindMask, SSHCFG_ALL },
615 { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
616 { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000617 { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
djm@openbsd.org7844f352016-11-30 03:00:05 +0000618 { "disableforwarding", sDisableForwarding, SSHCFG_ALL },
djm@openbsd.org8f574952017-06-24 06:34:38 +0000619 { "exposeauthinfo", sExposeAuthInfo, SSHCFG_ALL },
djm@openbsd.org35eb33f2017-10-25 00:17:08 +0000620 { "rdomain", sRDomain, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000621 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000622};
623
Darren Tuckere7140f22008-06-10 23:01:51 +1000624static struct {
625 int val;
626 char *text;
627} tunmode_desc[] = {
628 { SSH_TUNMODE_NO, "no" },
629 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
630 { SSH_TUNMODE_ETHERNET, "ethernet" },
631 { SSH_TUNMODE_YES, "yes" },
632 { -1, NULL }
633};
634
Damien Miller5428f641999-11-25 11:54:57 +1100635/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000636 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100637 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000638
Damien Miller4af51302000-04-16 11:18:38 +1000639static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100640parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000641 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000643 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644
Damien Miller95def091999-11-25 00:26:21 +1100645 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000646 if (strcasecmp(cp, keywords[i].name) == 0) {
647 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100648 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000649 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000650
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000651 error("%s: line %d: Bad configuration option: %s",
652 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100653 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000654}
655
Darren Tucker88b6fb22010-01-13 22:44:29 +1100656char *
657derelativise_path(const char *path)
658{
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000659 char *expanded, *ret, cwd[PATH_MAX];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100660
djm@openbsd.org161cf412014-12-22 07:55:51 +0000661 if (strcasecmp(path, "none") == 0)
662 return xstrdup("none");
Darren Tucker88b6fb22010-01-13 22:44:29 +1100663 expanded = tilde_expand_filename(path, getuid());
664 if (*expanded == '/')
665 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100666 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100667 fatal("%s: getcwd: %s", __func__, strerror(errno));
668 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tuckera627d422013-06-02 07:31:17 +1000669 free(expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100670 return ret;
671}
672
Ben Lindstrombba81212001-06-25 05:01:22 +0000673static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000674add_listen_addr(ServerOptions *options, const char *addr,
675 const char *rdomain, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100676{
Damien Millereccb9de2005-06-17 12:59:34 +1000677 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100678
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000679 if (port > 0)
680 add_one_listen_addr(options, addr, rdomain, port);
681 else {
682 for (i = 0; i < options->num_ports; i++) {
683 add_one_listen_addr(options, addr, rdomain,
684 options->ports[i]);
685 }
686 }
Ben Lindstromc510af42001-04-07 17:25:48 +0000687}
688
Ben Lindstrombba81212001-06-25 05:01:22 +0000689static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000690add_one_listen_addr(ServerOptions *options, const char *addr,
691 const char *rdomain, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000692{
693 struct addrinfo hints, *ai, *aitop;
694 char strport[NI_MAXSERV];
695 int gaierr;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000696 u_int i;
697
698 /* Find listen_addrs entry for this rdomain */
699 for (i = 0; i < options->num_listen_addrs; i++) {
700 if (rdomain == NULL && options->listen_addrs[i].rdomain == NULL)
701 break;
702 if (rdomain == NULL || options->listen_addrs[i].rdomain == NULL)
703 continue;
704 if (strcmp(rdomain, options->listen_addrs[i].rdomain) == 0)
705 break;
706 }
707 if (i >= options->num_listen_addrs) {
708 /* No entry for this rdomain; allocate one */
709 if (i >= INT_MAX)
710 fatal("%s: too many listen addresses", __func__);
711 options->listen_addrs = xrecallocarray(options->listen_addrs,
712 options->num_listen_addrs, options->num_listen_addrs + 1,
713 sizeof(*options->listen_addrs));
714 i = options->num_listen_addrs++;
715 if (rdomain != NULL)
716 options->listen_addrs[i].rdomain = xstrdup(rdomain);
717 }
718 /* options->listen_addrs[i] points to the addresses for this rdomain */
Ben Lindstromc510af42001-04-07 17:25:48 +0000719
720 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100721 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000722 hints.ai_socktype = SOCK_STREAM;
723 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100724 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000725 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
726 fatal("bad addr or host: %s (%s)",
727 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100728 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000729 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
730 ;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000731 ai->ai_next = options->listen_addrs[i].addrs;
732 options->listen_addrs[i].addrs = aitop;
733}
734
735/* Returns nonzero if the routing domain name is valid */
736static int
737valid_rdomain(const char *name)
738{
Damien Miller43c29bb2017-10-25 13:10:59 +1100739#if defined(HAVE_SYS_VALID_RDOMAIN)
Damien Miller2de5c6b2017-10-27 08:42:33 +1100740 return sys_valid_rdomain(name);
Damien Miller43c29bb2017-10-25 13:10:59 +1100741#elif defined(__OpenBSD__)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000742 const char *errstr;
743 long long num;
744 struct rt_tableinfo info;
745 int mib[6];
746 size_t miblen = sizeof(mib);
747
748 if (name == NULL)
749 return 1;
750
751 num = strtonum(name, 0, 255, &errstr);
752 if (errstr != NULL)
753 return 0;
754
755 /* Check whether the table actually exists */
756 memset(mib, 0, sizeof(mib));
757 mib[0] = CTL_NET;
758 mib[1] = PF_ROUTE;
759 mib[4] = NET_RT_TABLE;
760 mib[5] = (int)num;
761 if (sysctl(mib, 6, &info, &miblen, NULL, 0) == -1)
762 return 0;
763
764 return 1;
Damien Miller43c29bb2017-10-25 13:10:59 +1100765#else /* defined(__OpenBSD__) */
766 error("Routing domains are not supported on this platform");
767 return 0;
768#endif
Damien Miller34132e52000-01-14 15:45:46 +1100769}
770
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000771/*
772 * Queue a ListenAddress to be processed once we have all of the Ports
773 * and AddressFamily options.
774 */
775static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000776queue_listen_addr(ServerOptions *options, const char *addr,
777 const char *rdomain, int port)
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000778{
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000779 struct queued_listenaddr *qla;
780
781 options->queued_listen_addrs = xrecallocarray(
782 options->queued_listen_addrs,
783 options->num_queued_listens, options->num_queued_listens + 1,
784 sizeof(*options->queued_listen_addrs));
785 qla = &options->queued_listen_addrs[options->num_queued_listens++];
786 qla->addr = xstrdup(addr);
787 qla->port = port;
788 qla->rdomain = rdomain == NULL ? NULL : xstrdup(rdomain);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000789}
790
791/*
792 * Process queued (text) ListenAddress entries.
793 */
794static void
795process_queued_listen_addrs(ServerOptions *options)
796{
797 u_int i;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000798 struct queued_listenaddr *qla;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000799
800 if (options->num_ports == 0)
801 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
802 if (options->address_family == -1)
803 options->address_family = AF_UNSPEC;
804
805 for (i = 0; i < options->num_queued_listens; i++) {
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000806 qla = &options->queued_listen_addrs[i];
807 add_listen_addr(options, qla->addr, qla->rdomain, qla->port);
808 free(qla->addr);
809 free(qla->rdomain);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000810 }
811 free(options->queued_listen_addrs);
812 options->queued_listen_addrs = NULL;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000813 options->num_queued_listens = 0;
814}
815
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000816/*
817 * Inform channels layer of permitopen options from configuration.
818 */
819void
820process_permitopen(struct ssh *ssh, ServerOptions *options)
821{
822 u_int i;
823 int port;
824 char *host, *arg, *oarg;
825
826 channel_clear_adm_permitted_opens(ssh);
827 if (options->num_permitted_opens == 0)
828 return; /* permit any */
829
830 /* handle keywords: "any" / "none" */
831 if (options->num_permitted_opens == 1 &&
832 strcmp(options->permitted_opens[0], "any") == 0)
833 return;
834 if (options->num_permitted_opens == 1 &&
835 strcmp(options->permitted_opens[0], "none") == 0) {
836 channel_disable_adm_local_opens(ssh);
837 return;
838 }
839 /* Otherwise treat it as a list of permitted host:port */
840 for (i = 0; i < options->num_permitted_opens; i++) {
841 oarg = arg = xstrdup(options->permitted_opens[i]);
842 host = hpdelim(&arg);
843 if (host == NULL)
844 fatal("%s: missing host in PermitOpen", __func__);
845 host = cleanhostname(host);
846 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
847 fatal("%s: bad port number in PermitOpen", __func__);
848 /* Send it to channels layer */
849 channel_add_adm_permitted_opens(ssh, host, port);
850 free(oarg);
851 }
852}
853
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000854struct connection_info *
855get_connection_info(int populate, int use_dns)
856{
djm@openbsd.org95767262016-03-07 19:02:43 +0000857 struct ssh *ssh = active_state; /* XXX */
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000858 static struct connection_info ci;
859
860 if (!populate)
861 return &ci;
djm@openbsd.org95767262016-03-07 19:02:43 +0000862 ci.host = auth_get_canonical_hostname(ssh, use_dns);
863 ci.address = ssh_remote_ipaddr(ssh);
864 ci.laddress = ssh_local_ipaddr(ssh);
865 ci.lport = ssh_local_port(ssh);
djm@openbsd.org68af80e2017-10-25 00:19:47 +0000866 ci.rdomain = ssh_packet_rdomain_in(ssh);
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000867 return &ci;
868}
869
Darren Tucker45150472006-07-12 22:34:17 +1000870/*
871 * The strategy for the Match blocks is that the config file is parsed twice.
872 *
873 * The first time is at startup. activep is initialized to 1 and the
874 * directives in the global context are processed and acted on. Hitting a
875 * Match directive unsets activep and the directives inside the block are
876 * checked for syntax only.
877 *
878 * The second time is after a connection has been established but before
879 * authentication. activep is initialized to 2 and global config directives
880 * are ignored since they have already been processed. If the criteria in a
881 * Match block is met, activep is set and the subsequent directives
882 * processed and actioned until EOF or another Match block unsets it. Any
883 * options set are copied into the main server config.
884 *
885 * Potential additions/improvements:
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000886 * - Add Match support for pre-kex directives, eg. Ciphers.
Darren Tucker45150472006-07-12 22:34:17 +1000887 *
888 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
889 * Match Address 192.168.0.*
890 * Tag trusted
891 * Match Group wheel
892 * Tag trusted
893 * Match Tag trusted
894 * AllowTcpForwarding yes
895 * GatewayPorts clientspecified
896 * [...]
897 *
898 * - Add a PermittedChannelRequests directive
899 * Match Group shell
900 * PermittedChannelRequests session,forwarded-tcpip
901 */
902
903static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000904match_cfg_line_group(const char *grps, int line, const char *user)
905{
906 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000907 struct passwd *pw;
908
Damien Miller565ca3f2006-08-19 00:23:15 +1000909 if (user == NULL)
910 goto out;
911
912 if ((pw = getpwnam(user)) == NULL) {
913 debug("Can't match group at line %d because user %.100s does "
914 "not exist", line, user);
915 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
916 debug("Can't Match group because user %.100s not in any group "
917 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000918 } else if (ga_match_pattern_list(grps) != 1) {
919 debug("user %.100s does not match group list %.100s at line %d",
920 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000921 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000922 debug("user %.100s matched group list %.100s at line %d", user,
923 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000924 result = 1;
925 }
926out:
927 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000928 return result;
929}
930
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000931static void
932match_test_missing_fatal(const char *criteria, const char *attrib)
933{
934 fatal("'Match %s' in configuration but '%s' not in connection "
935 "test specification.", criteria, attrib);
936}
937
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000938/*
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000939 * All of the attributes on a single Match line are ANDed together, so we need
Damien Miller03bf2e62013-10-24 21:01:26 +1100940 * to check every attribute and set the result to zero if any attribute does
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000941 * not match.
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000942 */
Damien Miller565ca3f2006-08-19 00:23:15 +1000943static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000944match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +1000945{
Damien Millercf31f382013-10-24 21:02:56 +1100946 int result = 1, attributes = 0, port;
Darren Tucker45150472006-07-12 22:34:17 +1000947 char *arg, *attrib, *cp = *condition;
Darren Tucker45150472006-07-12 22:34:17 +1000948
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000949 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000950 debug3("checking syntax for 'Match %s'", cp);
951 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000952 debug3("checking match for '%s' user %s host %s addr %s "
953 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
954 ci->host ? ci->host : "(null)",
955 ci->address ? ci->address : "(null)",
956 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +1000957
958 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100959 attributes++;
960 if (strcasecmp(attrib, "all") == 0) {
961 if (attributes != 1 ||
962 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
963 error("'all' cannot be combined with other "
964 "Match attributes");
965 return -1;
966 }
967 *condition = cp;
968 return 1;
969 }
Darren Tucker45150472006-07-12 22:34:17 +1000970 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
971 error("Missing Match criteria for %s", attrib);
972 return -1;
973 }
Darren Tucker45150472006-07-12 22:34:17 +1000974 if (strcasecmp(attrib, "user") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000975 if (ci == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000976 result = 0;
977 continue;
978 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000979 if (ci->user == NULL)
980 match_test_missing_fatal("User", "user");
djm@openbsd.orge661a862015-05-04 06:10:48 +0000981 if (match_pattern_list(ci->user, arg, 0) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000982 result = 0;
983 else
984 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000985 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000986 } else if (strcasecmp(attrib, "group") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000987 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000988 result = 0;
989 continue;
990 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +0000991 if (ci->user == NULL)
992 match_test_missing_fatal("Group", "user");
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000993 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +1000994 case -1:
995 return -1;
996 case 0:
997 result = 0;
998 }
Darren Tucker45150472006-07-12 22:34:17 +1000999 } else if (strcasecmp(attrib, "host") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001000 if (ci == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +10001001 result = 0;
1002 continue;
1003 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001004 if (ci->host == NULL)
1005 match_test_missing_fatal("Host", "host");
djm@openbsd.orge661a862015-05-04 06:10:48 +00001006 if (match_hostname(ci->host, arg) != 1)
Darren Tucker45150472006-07-12 22:34:17 +10001007 result = 0;
1008 else
1009 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001010 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +10001011 } else if (strcasecmp(attrib, "address") == 0) {
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001012 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001013 result = 0;
1014 continue;
1015 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001016 if (ci->address == NULL)
1017 match_test_missing_fatal("Address", "addr");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001018 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +10001019 case 1:
Darren Tucker45150472006-07-12 22:34:17 +10001020 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001021 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +10001022 break;
1023 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +10001024 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +10001025 result = 0;
1026 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +10001027 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +10001028 return -1;
1029 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001030 } else if (strcasecmp(attrib, "localaddress") == 0){
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001031 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001032 result = 0;
1033 continue;
1034 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001035 if (ci->laddress == NULL)
1036 match_test_missing_fatal("LocalAddress",
1037 "laddr");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001038 switch (addr_match_list(ci->laddress, arg)) {
1039 case 1:
1040 debug("connection from %.100s matched "
1041 "'LocalAddress %.100s' at line %d",
1042 ci->laddress, arg, line);
1043 break;
1044 case 0:
1045 case -1:
1046 result = 0;
1047 break;
1048 case -2:
1049 return -1;
1050 }
1051 } else if (strcasecmp(attrib, "localport") == 0) {
1052 if ((port = a2port(arg)) == -1) {
1053 error("Invalid LocalPort '%s' on Match line",
1054 arg);
1055 return -1;
1056 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001057 if (ci == NULL) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001058 result = 0;
1059 continue;
1060 }
dtucker@openbsd.org@openbsd.org0208a482017-11-03 03:18:53 +00001061 if (ci->lport == 0)
1062 match_test_missing_fatal("LocalPort", "lport");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001063 /* TODO support port lists */
1064 if (port == ci->lport)
1065 debug("connection from %.100s matched "
1066 "'LocalPort %d' at line %d",
1067 ci->laddress, port, line);
1068 else
1069 result = 0;
djm@openbsd.org68af80e2017-10-25 00:19:47 +00001070 } else if (strcasecmp(attrib, "rdomain") == 0) {
1071 if (ci == NULL || ci->rdomain == NULL) {
1072 result = 0;
1073 continue;
1074 }
1075 if (match_pattern_list(ci->rdomain, arg, 0) != 1)
1076 result = 0;
1077 else
1078 debug("user %.100s matched 'RDomain %.100s' at "
1079 "line %d", ci->rdomain, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +10001080 } else {
1081 error("Unsupported Match attribute %s", attrib);
1082 return -1;
1083 }
1084 }
Damien Millercf31f382013-10-24 21:02:56 +11001085 if (attributes == 0) {
1086 error("One or more attributes required for Match");
1087 return -1;
1088 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001089 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +10001090 debug3("match %sfound", result ? "" : "not ");
1091 *condition = cp;
1092 return result;
1093}
1094
Damien Millere2754432006-07-24 14:06:47 +10001095#define WHITESPACE " \t\r\n"
1096
Damien Miller33322122011-06-20 14:43:11 +10001097/* Multistate option parsing */
1098struct multistate {
1099 char *key;
1100 int value;
1101};
djm@openbsd.org@openbsd.org33edb6e2017-11-03 05:18:44 +00001102static const struct multistate multistate_flag[] = {
1103 { "yes", 1 },
1104 { "no", 0 },
1105 { NULL, -1 }
1106};
Damien Miller33322122011-06-20 14:43:11 +10001107static const struct multistate multistate_addressfamily[] = {
1108 { "inet", AF_INET },
1109 { "inet6", AF_INET6 },
1110 { "any", AF_UNSPEC },
1111 { NULL, -1 }
1112};
1113static const struct multistate multistate_permitrootlogin[] = {
1114 { "without-password", PERMIT_NO_PASSWD },
deraadt@openbsd.org1dc8d932015-08-06 14:53:21 +00001115 { "prohibit-password", PERMIT_NO_PASSWD },
Damien Miller33322122011-06-20 14:43:11 +10001116 { "forced-commands-only", PERMIT_FORCED_ONLY },
1117 { "yes", PERMIT_YES },
1118 { "no", PERMIT_NO },
1119 { NULL, -1 }
1120};
1121static const struct multistate multistate_compression[] = {
djm@openbsd.org0082fba2016-09-28 16:33:06 +00001122 { "yes", COMP_DELAYED },
djm@openbsd.org4577ade2016-09-28 20:32:42 +00001123 { "delayed", COMP_DELAYED },
Damien Miller33322122011-06-20 14:43:11 +10001124 { "no", COMP_NONE },
1125 { NULL, -1 }
1126};
1127static const struct multistate multistate_gatewayports[] = {
1128 { "clientspecified", 2 },
1129 { "yes", 1 },
1130 { "no", 0 },
1131 { NULL, -1 }
1132};
Damien Milleraa5b3f82012-12-03 09:50:54 +11001133static const struct multistate multistate_tcpfwd[] = {
1134 { "yes", FORWARD_ALLOW },
1135 { "all", FORWARD_ALLOW },
1136 { "no", FORWARD_DENY },
1137 { "remote", FORWARD_REMOTE },
1138 { "local", FORWARD_LOCAL },
1139 { NULL, -1 }
1140};
Damien Miller33322122011-06-20 14:43:11 +10001141
Ben Lindstromade03f62001-12-06 18:22:17 +00001142int
1143process_server_config_line(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001144 const char *filename, int linenum, int *activep,
1145 struct connection_info *connectinfo)
Ben Lindstromade03f62001-12-06 18:22:17 +00001146{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001147 char *cp, **charptr, *arg, *arg2, *p;
Darren Tucker9113d0c2013-05-16 20:48:14 +10001148 int cmdline = 0, *intptr, value, value2, n, port;
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001149 SyslogFacility *log_facility_ptr;
1150 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +00001151 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +10001152 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +10001153 size_t len;
Darren Tucker9113d0c2013-05-16 20:48:14 +10001154 long long val64;
Damien Miller33322122011-06-20 14:43:11 +10001155 const struct multistate *multistate_ptr;
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001156 const char *errstr;
Ben Lindstromade03f62001-12-06 18:22:17 +00001157
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +00001158 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
1159 if ((len = strlen(line)) == 0)
1160 return 0;
1161 for (len--; len > 0; len--) {
1162 if (strchr(WHITESPACE "\f", line[len]) == NULL)
1163 break;
1164 line[len] = '\0';
1165 }
1166
Ben Lindstromade03f62001-12-06 18:22:17 +00001167 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +11001168 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +11001169 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +00001170 /* Ignore leading whitespace */
1171 if (*arg == '\0')
1172 arg = strdelim(&cp);
1173 if (!arg || !*arg || *arg == '#')
1174 return 0;
1175 intptr = NULL;
1176 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +10001177 opcode = parse_token(arg, filename, linenum, &flags);
1178
1179 if (activep == NULL) { /* We are processing a command line directive */
1180 cmdline = 1;
1181 activep = &cmdline;
1182 }
1183 if (*activep && opcode != sMatch)
1184 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
1185 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001186 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +10001187 fatal("%s line %d: Directive '%s' is not allowed "
1188 "within a Match block", filename, linenum, arg);
1189 } else { /* this is a directive we have already processed */
1190 while (arg)
1191 arg = strdelim(&cp);
1192 return 0;
1193 }
1194 }
1195
Ben Lindstromade03f62001-12-06 18:22:17 +00001196 switch (opcode) {
1197 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +10001198 case sUsePAM:
1199 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +00001200 goto parse_flag;
1201
1202 /* Standard Options */
1203 case sBadOption:
1204 return -1;
1205 case sPort:
1206 /* ignore ports from configfile if cmdline specifies ports */
1207 if (options->ports_from_cmdline)
1208 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +00001209 if (options->num_ports >= MAX_PORTS)
1210 fatal("%s line %d: too many ports.",
1211 filename, linenum);
1212 arg = strdelim(&cp);
1213 if (!arg || *arg == '\0')
1214 fatal("%s line %d: missing port number.",
1215 filename, linenum);
1216 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +11001217 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +00001218 fatal("%s line %d: Badly formatted port number.",
1219 filename, linenum);
1220 break;
1221
Ben Lindstromade03f62001-12-06 18:22:17 +00001222 case sLoginGraceTime:
1223 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +10001224 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +00001225 arg = strdelim(&cp);
1226 if (!arg || *arg == '\0')
1227 fatal("%s line %d: missing time value.",
1228 filename, linenum);
1229 if ((value = convtime(arg)) == -1)
1230 fatal("%s line %d: invalid time value.",
1231 filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001232 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001233 *intptr = value;
1234 break;
1235
Ben Lindstromade03f62001-12-06 18:22:17 +00001236 case sListenAddress:
1237 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001238 if (arg == NULL || *arg == '\0')
1239 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +00001240 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +10001241 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
1242 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
1243 && strchr(p+1, ':') != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001244 port = 0;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001245 p = arg;
1246 } else {
1247 p = hpdelim(&arg);
1248 if (p == NULL)
1249 fatal("%s line %d: bad address:port usage",
1250 filename, linenum);
1251 p = cleanhostname(p);
1252 if (arg == NULL)
1253 port = 0;
1254 else if ((port = a2port(arg)) <= 0)
1255 fatal("%s line %d: bad port number",
1256 filename, linenum);
1257 }
1258 /* Optional routing table */
1259 arg2 = NULL;
1260 if ((arg = strdelim(&cp)) != NULL) {
1261 if (strcmp(arg, "rdomain") != 0 ||
1262 (arg2 = strdelim(&cp)) == NULL)
1263 fatal("%s line %d: bad ListenAddress syntax",
1264 filename, linenum);
1265 if (!valid_rdomain(arg2))
1266 fatal("%s line %d: bad routing domain",
1267 filename, linenum);
1268 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001269
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001270 queue_listen_addr(options, p, arg2, port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001271
Ben Lindstromade03f62001-12-06 18:22:17 +00001272 break;
1273
Darren Tucker0f383232005-01-20 10:57:56 +11001274 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +10001275 intptr = &options->address_family;
1276 multistate_ptr = multistate_addressfamily;
Damien Miller33322122011-06-20 14:43:11 +10001277 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +11001278 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +10001279 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +10001280 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +10001281 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +10001282 value = -1;
1283 for (i = 0; multistate_ptr[i].key != NULL; i++) {
1284 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
1285 value = multistate_ptr[i].value;
1286 break;
1287 }
1288 }
1289 if (value == -1)
1290 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +11001291 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +10001292 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +11001293 *intptr = value;
1294 break;
1295
Ben Lindstromade03f62001-12-06 18:22:17 +00001296 case sHostKeyFile:
Ben Lindstromade03f62001-12-06 18:22:17 +00001297 arg = strdelim(&cp);
1298 if (!arg || *arg == '\0')
1299 fatal("%s line %d: missing file name.",
1300 filename, linenum);
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001301 if (*activep)
1302 servconf_add_hostkey(filename, linenum, options, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001303 break;
1304
Damien Miller85b45e02013-07-20 13:21:52 +10001305 case sHostKeyAgent:
1306 charptr = &options->host_key_agent;
1307 arg = strdelim(&cp);
1308 if (!arg || *arg == '\0')
1309 fatal("%s line %d: missing socket name.",
1310 filename, linenum);
1311 if (*activep && *charptr == NULL)
1312 *charptr = !strcmp(arg, SSH_AUTHSOCKET_ENV_NAME) ?
1313 xstrdup(arg) : derelativise_path(arg);
1314 break;
1315
Damien Miller0a80ca12010-02-27 07:55:05 +11001316 case sHostCertificate:
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001317 arg = strdelim(&cp);
1318 if (!arg || *arg == '\0')
1319 fatal("%s line %d: missing file name.",
1320 filename, linenum);
1321 if (*activep)
1322 servconf_add_hostcert(filename, linenum, options, arg);
1323 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001324
Ben Lindstromade03f62001-12-06 18:22:17 +00001325 case sPidFile:
1326 charptr = &options->pid_file;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001327 parse_filename:
1328 arg = strdelim(&cp);
1329 if (!arg || *arg == '\0')
1330 fatal("%s line %d: missing file name.",
1331 filename, linenum);
1332 if (*activep && *charptr == NULL) {
1333 *charptr = derelativise_path(arg);
1334 /* increase optional counter */
1335 if (intptr != NULL)
1336 *intptr = *intptr + 1;
1337 }
1338 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001339
1340 case sPermitRootLogin:
1341 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +10001342 multistate_ptr = multistate_permitrootlogin;
1343 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001344
1345 case sIgnoreRhosts:
1346 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +10001347 parse_flag:
djm@openbsd.org@openbsd.org33edb6e2017-11-03 05:18:44 +00001348 multistate_ptr = multistate_flag;
1349 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001350
1351 case sIgnoreUserKnownHosts:
1352 intptr = &options->ignore_user_known_hosts;
1353 goto parse_flag;
1354
Ben Lindstromade03f62001-12-06 18:22:17 +00001355 case sHostbasedAuthentication:
1356 intptr = &options->hostbased_authentication;
1357 goto parse_flag;
1358
1359 case sHostbasedUsesNameFromPacketOnly:
1360 intptr = &options->hostbased_uses_name_from_packet_only;
1361 goto parse_flag;
1362
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001363 case sHostbasedAcceptedKeyTypes:
1364 charptr = &options->hostbased_key_types;
1365 parse_keytypes:
1366 arg = strdelim(&cp);
1367 if (!arg || *arg == '\0')
1368 fatal("%s line %d: Missing argument.",
1369 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001370 if (*arg != '-' &&
1371 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001372 fatal("%s line %d: Bad key types '%s'.",
1373 filename, linenum, arg ? arg : "<NONE>");
1374 if (*activep && *charptr == NULL)
1375 *charptr = xstrdup(arg);
1376 break;
1377
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001378 case sHostKeyAlgorithms:
1379 charptr = &options->hostkeyalgorithms;
1380 goto parse_keytypes;
1381
Ben Lindstromade03f62001-12-06 18:22:17 +00001382 case sPubkeyAuthentication:
1383 intptr = &options->pubkey_authentication;
1384 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001385
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001386 case sPubkeyAcceptedKeyTypes:
1387 charptr = &options->pubkey_key_types;
1388 goto parse_keytypes;
1389
Ben Lindstromade03f62001-12-06 18:22:17 +00001390 case sKerberosAuthentication:
1391 intptr = &options->kerberos_authentication;
1392 goto parse_flag;
1393
1394 case sKerberosOrLocalPasswd:
1395 intptr = &options->kerberos_or_local_passwd;
1396 goto parse_flag;
1397
1398 case sKerberosTicketCleanup:
1399 intptr = &options->kerberos_ticket_cleanup;
1400 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001401
Darren Tucker22ef5082003-12-31 11:37:34 +11001402 case sKerberosGetAFSToken:
1403 intptr = &options->kerberos_get_afs_token;
1404 goto parse_flag;
1405
Darren Tucker0efd1552003-08-26 11:49:55 +10001406 case sGssAuthentication:
1407 intptr = &options->gss_authentication;
1408 goto parse_flag;
1409
1410 case sGssCleanupCreds:
1411 intptr = &options->gss_cleanup_creds;
1412 goto parse_flag;
1413
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +00001414 case sGssStrictAcceptor:
1415 intptr = &options->gss_strict_acceptor;
1416 goto parse_flag;
1417
Ben Lindstromade03f62001-12-06 18:22:17 +00001418 case sPasswordAuthentication:
1419 intptr = &options->password_authentication;
1420 goto parse_flag;
1421
1422 case sKbdInteractiveAuthentication:
1423 intptr = &options->kbd_interactive_authentication;
1424 goto parse_flag;
1425
1426 case sChallengeResponseAuthentication:
1427 intptr = &options->challenge_response_authentication;
1428 goto parse_flag;
1429
1430 case sPrintMotd:
1431 intptr = &options->print_motd;
1432 goto parse_flag;
1433
1434 case sPrintLastLog:
1435 intptr = &options->print_lastlog;
1436 goto parse_flag;
1437
1438 case sX11Forwarding:
1439 intptr = &options->x11_forwarding;
1440 goto parse_flag;
1441
1442 case sX11DisplayOffset:
1443 intptr = &options->x11_display_offset;
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +00001444 parse_int:
1445 arg = strdelim(&cp);
dtucker@openbsd.org609d96b2017-12-05 23:59:47 +00001446 if ((errstr = atoi_err(arg, &value)) != NULL)
1447 fatal("%s line %d: integer value %s.",
1448 filename, linenum, errstr);
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +00001449 if (*activep && *intptr == -1)
1450 *intptr = value;
1451 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001452
Damien Miller95c249f2002-02-05 12:11:34 +11001453 case sX11UseLocalhost:
1454 intptr = &options->x11_use_localhost;
1455 goto parse_flag;
1456
Ben Lindstromade03f62001-12-06 18:22:17 +00001457 case sXAuthLocation:
1458 charptr = &options->xauth_location;
1459 goto parse_filename;
1460
Damien Miller5ff30c62013-10-30 22:21:50 +11001461 case sPermitTTY:
1462 intptr = &options->permit_tty;
1463 goto parse_flag;
1464
Damien Miller72e6b5c2014-07-04 09:00:04 +10001465 case sPermitUserRC:
1466 intptr = &options->permit_user_rc;
1467 goto parse_flag;
1468
Ben Lindstromade03f62001-12-06 18:22:17 +00001469 case sStrictModes:
1470 intptr = &options->strict_modes;
1471 goto parse_flag;
1472
Damien Miller12c150e2003-12-17 16:31:10 +11001473 case sTCPKeepAlive:
1474 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001475 goto parse_flag;
1476
1477 case sEmptyPasswd:
1478 intptr = &options->permit_empty_passwd;
1479 goto parse_flag;
1480
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001481 case sPermitUserEnvironment:
1482 intptr = &options->permit_user_env;
1483 goto parse_flag;
1484
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001485 case sCompression:
1486 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001487 multistate_ptr = multistate_compression;
1488 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001489
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001490 case sRekeyLimit:
1491 arg = strdelim(&cp);
1492 if (!arg || *arg == '\0')
1493 fatal("%.200s line %d: Missing argument.", filename,
1494 linenum);
1495 if (strcmp(arg, "default") == 0) {
1496 val64 = 0;
1497 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001498 if (scan_scaled(arg, &val64) == -1)
1499 fatal("%.200s line %d: Bad number '%s': %s",
1500 filename, linenum, arg, strerror(errno));
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001501 if (val64 != 0 && val64 < 16)
1502 fatal("%.200s line %d: RekeyLimit too small",
1503 filename, linenum);
1504 }
1505 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001506 options->rekey_limit = val64;
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001507 if (cp != NULL) { /* optional rekey interval present */
1508 if (strcmp(cp, "none") == 0) {
1509 (void)strdelim(&cp); /* discard */
1510 break;
1511 }
1512 intptr = &options->rekey_interval;
1513 goto parse_time;
1514 }
1515 break;
1516
Ben Lindstromade03f62001-12-06 18:22:17 +00001517 case sGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +10001518 intptr = &options->fwd_opts.gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001519 multistate_ptr = multistate_gatewayports;
1520 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001521
Damien Miller3a961dc2003-06-03 10:25:48 +10001522 case sUseDNS:
1523 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001524 goto parse_flag;
1525
1526 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001527 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001528 arg = strdelim(&cp);
1529 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001530 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001531 fatal("%.200s line %d: unsupported log facility '%s'",
1532 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001533 if (*log_facility_ptr == -1)
1534 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001535 break;
1536
1537 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001538 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001539 arg = strdelim(&cp);
1540 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001541 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001542 fatal("%.200s line %d: unsupported log level '%s'",
1543 filename, linenum, arg ? arg : "<NONE>");
djm@openbsd.org54cd41a2017-05-17 01:24:17 +00001544 if (*activep && *log_level_ptr == -1)
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001545 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001546 break;
1547
1548 case sAllowTcpForwarding:
1549 intptr = &options->allow_tcp_forwarding;
Damien Milleraa5b3f82012-12-03 09:50:54 +11001550 multistate_ptr = multistate_tcpfwd;
1551 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001552
Damien Miller7acefbb2014-07-18 14:11:24 +10001553 case sAllowStreamLocalForwarding:
1554 intptr = &options->allow_streamlocal_forwarding;
1555 multistate_ptr = multistate_tcpfwd;
1556 goto parse_multistate;
1557
Damien Miller4f755cd2008-05-19 14:57:41 +10001558 case sAllowAgentForwarding:
1559 intptr = &options->allow_agent_forwarding;
1560 goto parse_flag;
1561
djm@openbsd.org7844f352016-11-30 03:00:05 +00001562 case sDisableForwarding:
1563 intptr = &options->disable_forwarding;
1564 goto parse_flag;
1565
Ben Lindstromade03f62001-12-06 18:22:17 +00001566 case sAllowUsers:
1567 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.org010359b2016-11-06 05:46:37 +00001568 if (match_user(NULL, NULL, NULL, arg) == -1)
1569 fatal("%s line %d: invalid AllowUsers pattern: "
1570 "\"%.100s\"", filename, linenum, arg);
Damien Millerc24da772012-06-20 21:53:58 +10001571 if (!*activep)
1572 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001573 array_append(filename, linenum, "AllowUsers",
1574 &options->allow_users, &options->num_allow_users,
1575 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001576 }
1577 break;
1578
1579 case sDenyUsers:
1580 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.org010359b2016-11-06 05:46:37 +00001581 if (match_user(NULL, NULL, NULL, arg) == -1)
1582 fatal("%s line %d: invalid DenyUsers pattern: "
1583 "\"%.100s\"", filename, linenum, arg);
Damien Millerc24da772012-06-20 21:53:58 +10001584 if (!*activep)
1585 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001586 array_append(filename, linenum, "DenyUsers",
1587 &options->deny_users, &options->num_deny_users,
1588 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001589 }
1590 break;
1591
1592 case sAllowGroups:
1593 while ((arg = strdelim(&cp)) && *arg != '\0') {
Damien Millerc24da772012-06-20 21:53:58 +10001594 if (!*activep)
1595 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001596 array_append(filename, linenum, "AllowGroups",
1597 &options->allow_groups, &options->num_allow_groups,
1598 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001599 }
1600 break;
1601
1602 case sDenyGroups:
1603 while ((arg = strdelim(&cp)) && *arg != '\0') {
Damien Millerc24da772012-06-20 21:53:58 +10001604 if (!*activep)
1605 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001606 array_append(filename, linenum, "DenyGroups",
1607 &options->deny_groups, &options->num_deny_groups,
1608 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001609 }
1610 break;
1611
1612 case sCiphers:
1613 arg = strdelim(&cp);
1614 if (!arg || *arg == '\0')
1615 fatal("%s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001616 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstromade03f62001-12-06 18:22:17 +00001617 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1618 filename, linenum, arg ? arg : "<NONE>");
1619 if (options->ciphers == NULL)
1620 options->ciphers = xstrdup(arg);
1621 break;
1622
1623 case sMacs:
1624 arg = strdelim(&cp);
1625 if (!arg || *arg == '\0')
1626 fatal("%s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001627 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstromade03f62001-12-06 18:22:17 +00001628 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1629 filename, linenum, arg ? arg : "<NONE>");
1630 if (options->macs == NULL)
1631 options->macs = xstrdup(arg);
1632 break;
1633
Damien Millerd5f62bf2010-09-24 22:11:14 +10001634 case sKexAlgorithms:
1635 arg = strdelim(&cp);
1636 if (!arg || *arg == '\0')
1637 fatal("%s line %d: Missing argument.",
1638 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001639 if (*arg != '-' &&
1640 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001641 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1642 filename, linenum, arg ? arg : "<NONE>");
1643 if (options->kex_algorithms == NULL)
1644 options->kex_algorithms = xstrdup(arg);
1645 break;
1646
Ben Lindstromade03f62001-12-06 18:22:17 +00001647 case sSubsystem:
1648 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1649 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001650 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001651 }
1652 arg = strdelim(&cp);
1653 if (!arg || *arg == '\0')
1654 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001655 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001656 if (!*activep) {
1657 arg = strdelim(&cp);
1658 break;
1659 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001660 for (i = 0; i < options->num_subsystems; i++)
1661 if (strcmp(arg, options->subsystem_name[i]) == 0)
1662 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001663 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001664 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1665 arg = strdelim(&cp);
1666 if (!arg || *arg == '\0')
1667 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001668 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001669 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001670
1671 /* Collect arguments (separate to executable) */
1672 p = xstrdup(arg);
1673 len = strlen(p) + 1;
1674 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1675 len += 1 + strlen(arg);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001676 p = xreallocarray(p, 1, len);
Damien Miller917f9b62006-07-10 20:36:47 +10001677 strlcat(p, " ", len);
1678 strlcat(p, arg, len);
1679 }
1680 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001681 options->num_subsystems++;
1682 break;
1683
1684 case sMaxStartups:
1685 arg = strdelim(&cp);
1686 if (!arg || *arg == '\0')
1687 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001688 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001689 if ((n = sscanf(arg, "%d:%d:%d",
1690 &options->max_startups_begin,
1691 &options->max_startups_rate,
1692 &options->max_startups)) == 3) {
1693 if (options->max_startups_begin >
1694 options->max_startups ||
1695 options->max_startups_rate > 100 ||
1696 options->max_startups_rate < 1)
1697 fatal("%s line %d: Illegal MaxStartups spec.",
1698 filename, linenum);
1699 } else if (n != 1)
1700 fatal("%s line %d: Illegal MaxStartups spec.",
1701 filename, linenum);
1702 else
1703 options->max_startups = options->max_startups_begin;
1704 break;
1705
Darren Tucker89413db2004-05-24 10:36:23 +10001706 case sMaxAuthTries:
1707 intptr = &options->max_authtries;
1708 goto parse_int;
1709
Damien Miller7207f642008-05-19 15:34:50 +10001710 case sMaxSessions:
1711 intptr = &options->max_sessions;
1712 goto parse_int;
1713
Ben Lindstromade03f62001-12-06 18:22:17 +00001714 case sBanner:
1715 charptr = &options->banner;
1716 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001717
Ben Lindstromade03f62001-12-06 18:22:17 +00001718 /*
1719 * These options can contain %X options expanded at
1720 * connect time, so that you can specify paths like:
1721 *
1722 * AuthorizedKeysFile /etc/ssh_keys/%u
1723 */
1724 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001725 if (*activep && options->num_authkeys_files == 0) {
1726 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001727 arg = tilde_expand_filename(arg, getuid());
1728 array_append(filename, linenum,
1729 "AuthorizedKeysFile",
1730 &options->authorized_keys_files,
1731 &options->num_authkeys_files, arg);
1732 free(arg);
Damien Millerd8478b62011-05-29 21:39:36 +10001733 }
1734 }
1735 return 0;
1736
Damien Miller30da3442010-05-10 11:58:03 +10001737 case sAuthorizedPrincipalsFile:
1738 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001739 arg = strdelim(&cp);
1740 if (!arg || *arg == '\0')
1741 fatal("%s line %d: missing file name.",
1742 filename, linenum);
1743 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001744 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001745 /* increase optional counter */
1746 if (intptr != NULL)
1747 *intptr = *intptr + 1;
1748 }
1749 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001750
1751 case sClientAliveInterval:
1752 intptr = &options->client_alive_interval;
1753 goto parse_time;
1754
1755 case sClientAliveCountMax:
1756 intptr = &options->client_alive_count_max;
1757 goto parse_int;
1758
Darren Tucker46bc0752004-05-02 22:11:30 +10001759 case sAcceptEnv:
1760 while ((arg = strdelim(&cp)) && *arg != '\0') {
1761 if (strchr(arg, '=') != NULL)
1762 fatal("%s line %d: Invalid environment name.",
1763 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001764 if (!*activep)
Damien Millerc24da772012-06-20 21:53:58 +10001765 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001766 array_append(filename, linenum, "AcceptEnv",
1767 &options->accept_env, &options->num_accept_env,
1768 arg);
Darren Tucker46bc0752004-05-02 22:11:30 +10001769 }
1770 break;
1771
Damien Millerd27b9472005-12-13 19:29:02 +11001772 case sPermitTunnel:
1773 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001774 arg = strdelim(&cp);
1775 if (!arg || *arg == '\0')
1776 fatal("%s line %d: Missing yes/point-to-point/"
1777 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001778 value = -1;
1779 for (i = 0; tunmode_desc[i].val != -1; i++)
1780 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1781 value = tunmode_desc[i].val;
1782 break;
1783 }
1784 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001785 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1786 "no argument: %s", filename, linenum, arg);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001787 if (*activep && *intptr == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001788 *intptr = value;
1789 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001790
Darren Tucker45150472006-07-12 22:34:17 +10001791 case sMatch:
1792 if (cmdline)
1793 fatal("Match directive not supported as a command-line "
1794 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001795 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10001796 if (value < 0)
1797 fatal("%s line %d: Bad Match condition", filename,
1798 linenum);
1799 *activep = value;
1800 break;
1801
Damien Miller9b439df2006-07-24 14:04:00 +10001802 case sPermitOpen:
1803 arg = strdelim(&cp);
1804 if (!arg || *arg == '\0')
1805 fatal("%s line %d: missing PermitOpen specification",
1806 filename, linenum);
djm@openbsd.org69bda022017-10-04 18:49:30 +00001807 value = options->num_permitted_opens; /* modified later */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001808 if (strcmp(arg, "any") == 0 || strcmp(arg, "none") == 0) {
djm@openbsd.org69bda022017-10-04 18:49:30 +00001809 if (*activep && value == 0) {
Damien Millerc6081482012-04-22 11:18:53 +10001810 options->num_permitted_opens = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001811 options->permitted_opens = xcalloc(1,
1812 sizeof(*options->permitted_opens));
1813 options->permitted_opens[0] = xstrdup(arg);
dtucker@openbsd.org30484e52017-09-18 09:41:52 +00001814 }
Damien Millerc6081482012-04-22 11:18:53 +10001815 break;
1816 }
Damien Millera765cf42006-07-24 14:08:13 +10001817 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001818 arg2 = xstrdup(arg);
Damien Millera765cf42006-07-24 14:08:13 +10001819 p = hpdelim(&arg);
1820 if (p == NULL)
1821 fatal("%s line %d: missing host in PermitOpen",
1822 filename, linenum);
1823 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001824 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001825 fatal("%s line %d: bad port number in "
1826 "PermitOpen", filename, linenum);
djm@openbsd.org69bda022017-10-04 18:49:30 +00001827 if (*activep && value == 0) {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001828 array_append(filename, linenum,
1829 "PermitOpen",
1830 &options->permitted_opens,
1831 &options->num_permitted_opens, arg2);
1832 }
1833 free(arg2);
Damien Millera765cf42006-07-24 14:08:13 +10001834 }
Damien Miller9b439df2006-07-24 14:04:00 +10001835 break;
1836
Damien Millere2754432006-07-24 14:06:47 +10001837 case sForceCommand:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001838 if (cp == NULL || *cp == '\0')
Damien Millere2754432006-07-24 14:06:47 +10001839 fatal("%.200s line %d: Missing argument.", filename,
1840 linenum);
1841 len = strspn(cp, WHITESPACE);
1842 if (*activep && options->adm_forced_command == NULL)
1843 options->adm_forced_command = xstrdup(cp + len);
1844 return 0;
1845
Damien Millerd8cb1f12008-02-10 22:40:12 +11001846 case sChrootDirectory:
1847 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001848
1849 arg = strdelim(&cp);
1850 if (!arg || *arg == '\0')
1851 fatal("%s line %d: missing file name.",
1852 filename, linenum);
1853 if (*activep && *charptr == NULL)
1854 *charptr = xstrdup(arg);
1855 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001856
Damien Miller1aed65e2010-03-04 21:53:35 +11001857 case sTrustedUserCAKeys:
1858 charptr = &options->trusted_user_ca_keys;
1859 goto parse_filename;
1860
1861 case sRevokedKeys:
1862 charptr = &options->revoked_keys_file;
1863 goto parse_filename;
1864
Damien Miller0dac6fb2010-11-20 15:19:38 +11001865 case sIPQoS:
1866 arg = strdelim(&cp);
1867 if ((value = parse_ipqos(arg)) == -1)
1868 fatal("%s line %d: Bad IPQoS value: %s",
1869 filename, linenum, arg);
1870 arg = strdelim(&cp);
1871 if (arg == NULL)
1872 value2 = value;
1873 else if ((value2 = parse_ipqos(arg)) == -1)
1874 fatal("%s line %d: Bad IPQoS value: %s",
1875 filename, linenum, arg);
1876 if (*activep) {
1877 options->ip_qos_interactive = value;
1878 options->ip_qos_bulk = value2;
1879 }
1880 break;
1881
Damien Miller23528812012-04-22 11:24:43 +10001882 case sVersionAddendum:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001883 if (cp == NULL || *cp == '\0')
Damien Miller23528812012-04-22 11:24:43 +10001884 fatal("%.200s line %d: Missing argument.", filename,
1885 linenum);
1886 len = strspn(cp, WHITESPACE);
1887 if (*activep && options->version_addendum == NULL) {
1888 if (strcasecmp(cp + len, "none") == 0)
1889 options->version_addendum = xstrdup("");
1890 else if (strchr(cp + len, '\r') != NULL)
1891 fatal("%.200s line %d: Invalid argument",
1892 filename, linenum);
1893 else
1894 options->version_addendum = xstrdup(cp + len);
1895 }
1896 return 0;
1897
Damien Miller09d3e122012-10-31 08:58:58 +11001898 case sAuthorizedKeysCommand:
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001899 if (cp == NULL)
1900 fatal("%.200s line %d: Missing argument.", filename,
1901 linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001902 len = strspn(cp, WHITESPACE);
1903 if (*activep && options->authorized_keys_command == NULL) {
1904 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1905 fatal("%.200s line %d: AuthorizedKeysCommand "
1906 "must be an absolute path",
1907 filename, linenum);
1908 options->authorized_keys_command = xstrdup(cp + len);
1909 }
1910 return 0;
1911
1912 case sAuthorizedKeysCommandUser:
1913 charptr = &options->authorized_keys_command_user;
1914
1915 arg = strdelim(&cp);
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001916 if (!arg || *arg == '\0')
1917 fatal("%s line %d: missing AuthorizedKeysCommandUser "
1918 "argument.", filename, linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001919 if (*activep && *charptr == NULL)
1920 *charptr = xstrdup(arg);
1921 break;
1922
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +00001923 case sAuthorizedPrincipalsCommand:
1924 if (cp == NULL)
1925 fatal("%.200s line %d: Missing argument.", filename,
1926 linenum);
1927 len = strspn(cp, WHITESPACE);
1928 if (*activep &&
1929 options->authorized_principals_command == NULL) {
1930 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1931 fatal("%.200s line %d: "
1932 "AuthorizedPrincipalsCommand must be "
1933 "an absolute path", filename, linenum);
1934 options->authorized_principals_command =
1935 xstrdup(cp + len);
1936 }
1937 return 0;
1938
1939 case sAuthorizedPrincipalsCommandUser:
1940 charptr = &options->authorized_principals_command_user;
1941
1942 arg = strdelim(&cp);
1943 if (!arg || *arg == '\0')
1944 fatal("%s line %d: missing "
1945 "AuthorizedPrincipalsCommandUser argument.",
1946 filename, linenum);
1947 if (*activep && *charptr == NULL)
1948 *charptr = xstrdup(arg);
1949 break;
1950
Damien Millera6e3f012012-11-04 23:21:40 +11001951 case sAuthenticationMethods:
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001952 if (options->num_auth_methods == 0) {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001953 value = 0; /* seen "any" pseudo-method */
djm@openbsd.org46ecd192016-06-23 05:17:51 +00001954 value2 = 0; /* sucessfully parsed any method */
Damien Millera6e3f012012-11-04 23:21:40 +11001955 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001956 if (strcmp(arg, "any") == 0) {
1957 if (options->num_auth_methods > 0) {
1958 fatal("%s line %d: \"any\" "
1959 "must appear alone in "
1960 "AuthenticationMethods",
1961 filename, linenum);
1962 }
1963 value = 1;
1964 } else if (value) {
1965 fatal("%s line %d: \"any\" must appear "
1966 "alone in AuthenticationMethods",
1967 filename, linenum);
1968 } else if (auth2_methods_valid(arg, 0) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +11001969 fatal("%s line %d: invalid "
1970 "authentication method list.",
1971 filename, linenum);
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001972 }
djm@openbsd.org46ecd192016-06-23 05:17:51 +00001973 value2 = 1;
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001974 if (!*activep)
1975 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001976 array_append(filename, linenum,
1977 "AuthenticationMethods",
1978 &options->auth_methods,
1979 &options->num_auth_methods, arg);
Damien Millera6e3f012012-11-04 23:21:40 +11001980 }
djm@openbsd.org46ecd192016-06-23 05:17:51 +00001981 if (value2 == 0) {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001982 fatal("%s line %d: no AuthenticationMethods "
1983 "specified", filename, linenum);
1984 }
Damien Millera6e3f012012-11-04 23:21:40 +11001985 }
1986 return 0;
1987
Damien Miller7acefbb2014-07-18 14:11:24 +10001988 case sStreamLocalBindMask:
1989 arg = strdelim(&cp);
1990 if (!arg || *arg == '\0')
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001991 fatal("%s line %d: missing StreamLocalBindMask "
1992 "argument.", filename, linenum);
Damien Miller7acefbb2014-07-18 14:11:24 +10001993 /* Parse mode in octal format */
1994 value = strtol(arg, &p, 8);
1995 if (arg == p || value < 0 || value > 0777)
1996 fatal("%s line %d: Bad mask.", filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001997 if (*activep)
1998 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
Damien Miller7acefbb2014-07-18 14:11:24 +10001999 break;
2000
2001 case sStreamLocalBindUnlink:
2002 intptr = &options->fwd_opts.streamlocal_bind_unlink;
2003 goto parse_flag;
2004
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002005 case sFingerprintHash:
2006 arg = strdelim(&cp);
2007 if (!arg || *arg == '\0')
2008 fatal("%.200s line %d: Missing argument.",
2009 filename, linenum);
2010 if ((value = ssh_digest_alg_by_name(arg)) == -1)
2011 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
2012 filename, linenum, arg);
2013 if (*activep)
2014 options->fingerprint_hash = value;
2015 break;
2016
djm@openbsd.org8f574952017-06-24 06:34:38 +00002017 case sExposeAuthInfo:
2018 intptr = &options->expose_userauth_info;
2019 goto parse_flag;
2020
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002021 case sRDomain:
2022 charptr = &options->routing_domain;
2023 arg = strdelim(&cp);
2024 if (!arg || *arg == '\0')
2025 fatal("%.200s line %d: Missing argument.",
2026 filename, linenum);
2027 if (strcasecmp(arg, "none") != 0 && strcmp(arg, "%D") != 0 &&
2028 !valid_rdomain(arg))
2029 fatal("%s line %d: bad routing domain",
2030 filename, linenum);
2031 if (*activep && *charptr == NULL)
2032 *charptr = xstrdup(arg);
dtucker@openbsd.org168ecec2017-12-05 23:56:07 +00002033 break;
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002034
Ben Lindstromade03f62001-12-06 18:22:17 +00002035 case sDeprecated:
djm@openbsd.orgae363d72016-08-25 23:57:54 +00002036 case sIgnore:
Damien Millerf9b3feb2003-05-16 11:38:32 +10002037 case sUnsupported:
djm@openbsd.orgae363d72016-08-25 23:57:54 +00002038 do_log2(opcode == sIgnore ?
2039 SYSLOG_LEVEL_DEBUG2 : SYSLOG_LEVEL_INFO,
2040 "%s line %d: %s option %s", filename, linenum,
2041 opcode == sUnsupported ? "Unsupported" : "Deprecated", arg);
Damien Millerf9b3feb2003-05-16 11:38:32 +10002042 while (arg)
2043 arg = strdelim(&cp);
2044 break;
2045
Ben Lindstromade03f62001-12-06 18:22:17 +00002046 default:
2047 fatal("%s line %d: Missing handler for opcode %s (%d)",
2048 filename, linenum, arg, opcode);
2049 }
2050 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
2051 fatal("%s line %d: garbage at end of line; \"%.200s\".",
2052 filename, linenum, arg);
2053 return 0;
2054}
2055
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002056/* Reads the server configuration file. */
2057
Damien Miller4af51302000-04-16 11:18:38 +10002058void
Darren Tucker645ab752004-06-25 13:33:20 +10002059load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002060{
Damien Miller46cb75a2012-07-31 12:22:37 +10002061 char line[4096], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00002062 FILE *f;
Damien Miller46cb75a2012-07-31 12:22:37 +10002063 int lineno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002064
Darren Tucker645ab752004-06-25 13:33:20 +10002065 debug2("%s: filename %s", __func__, filename);
2066 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11002067 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002068 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11002069 }
Darren Tucker645ab752004-06-25 13:33:20 +10002070 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11002071 while (fgets(line, sizeof(line), f)) {
Damien Miller46cb75a2012-07-31 12:22:37 +10002072 lineno++;
2073 if (strlen(line) == sizeof(line) - 1)
2074 fatal("%s line %d too long", filename, lineno);
Darren Tucker645ab752004-06-25 13:33:20 +10002075 /*
2076 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10002077 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10002078 * line numbers later for error messages
2079 */
2080 if ((cp = strchr(line, '#')) != NULL)
2081 memcpy(cp, "\n", 2);
2082 cp = line + strspn(line, " \t\r");
2083
2084 buffer_append(conf, cp, strlen(cp));
2085 }
2086 buffer_append(conf, "\0", 1);
2087 fclose(f);
2088 debug2("%s: done config len = %d", __func__, buffer_len(conf));
2089}
2090
2091void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002092parse_server_match_config(ServerOptions *options,
2093 struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10002094{
Darren Tucker45150472006-07-12 22:34:17 +10002095 ServerOptions mo;
2096
2097 initialize_server_options(&mo);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002098 parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11002099 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10002100}
2101
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002102int parse_server_match_testspec(struct connection_info *ci, char *spec)
2103{
2104 char *p;
2105
2106 while ((p = strsep(&spec, ",")) && *p != '\0') {
2107 if (strncmp(p, "addr=", 5) == 0) {
2108 ci->address = xstrdup(p + 5);
2109 } else if (strncmp(p, "host=", 5) == 0) {
2110 ci->host = xstrdup(p + 5);
2111 } else if (strncmp(p, "user=", 5) == 0) {
2112 ci->user = xstrdup(p + 5);
2113 } else if (strncmp(p, "laddr=", 6) == 0) {
2114 ci->laddress = xstrdup(p + 6);
djm@openbsd.org68af80e2017-10-25 00:19:47 +00002115 } else if (strncmp(p, "rdomain=", 8) == 0) {
2116 ci->rdomain = xstrdup(p + 8);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002117 } else if (strncmp(p, "lport=", 6) == 0) {
2118 ci->lport = a2port(p + 6);
2119 if (ci->lport == -1) {
2120 fprintf(stderr, "Invalid port '%s' in test mode"
2121 " specification %s\n", p+6, p);
2122 return -1;
2123 }
2124 } else {
2125 fprintf(stderr, "Invalid test mode specification %s\n",
2126 p);
2127 return -1;
2128 }
2129 }
2130 return 0;
2131}
2132
2133/*
Darren Tucker1629c072007-02-19 22:25:37 +11002134 * Copy any supported values that are set.
2135 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10002136 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11002137 * array values that are not used pre-authentication, because any that we
2138 * do use must be explictly sent in mm_getpwnamallow().
2139 */
Darren Tucker45150472006-07-12 22:34:17 +10002140void
Darren Tucker1629c072007-02-19 22:25:37 +11002141copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10002142{
Damien Miller534b2cc2013-12-05 14:07:27 +11002143#define M_CP_INTOPT(n) do {\
2144 if (src->n != -1) \
2145 dst->n = src->n; \
2146} while (0)
2147
Darren Tucker1629c072007-02-19 22:25:37 +11002148 M_CP_INTOPT(password_authentication);
2149 M_CP_INTOPT(gss_authentication);
Darren Tucker1629c072007-02-19 22:25:37 +11002150 M_CP_INTOPT(pubkey_authentication);
2151 M_CP_INTOPT(kerberos_authentication);
2152 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10002153 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11002154 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11002155 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11002156 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11002157
2158 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002159 M_CP_INTOPT(allow_streamlocal_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10002160 M_CP_INTOPT(allow_agent_forwarding);
djm@openbsd.org7844f352016-11-30 03:00:05 +00002161 M_CP_INTOPT(disable_forwarding);
djm@openbsd.org8f574952017-06-24 06:34:38 +00002162 M_CP_INTOPT(expose_userauth_info);
Damien Millerab6de352010-06-26 09:38:45 +10002163 M_CP_INTOPT(permit_tun);
Damien Miller7acefbb2014-07-18 14:11:24 +10002164 M_CP_INTOPT(fwd_opts.gateway_ports);
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002165 M_CP_INTOPT(fwd_opts.streamlocal_bind_unlink);
Darren Tucker1629c072007-02-19 22:25:37 +11002166 M_CP_INTOPT(x11_display_offset);
2167 M_CP_INTOPT(x11_forwarding);
2168 M_CP_INTOPT(x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002169 M_CP_INTOPT(permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002170 M_CP_INTOPT(permit_user_rc);
Damien Miller7207f642008-05-19 15:34:50 +10002171 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10002172 M_CP_INTOPT(max_authtries);
markus@openbsd.orgf0ddede2016-11-23 23:14:15 +00002173 M_CP_INTOPT(client_alive_count_max);
2174 M_CP_INTOPT(client_alive_interval);
Damien Miller0dac6fb2010-11-20 15:19:38 +11002175 M_CP_INTOPT(ip_qos_interactive);
2176 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002177 M_CP_INTOPT(rekey_limit);
2178 M_CP_INTOPT(rekey_interval);
djm@openbsd.org54cd41a2017-05-17 01:24:17 +00002179 M_CP_INTOPT(log_level);
Darren Tucker1629c072007-02-19 22:25:37 +11002180
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002181 /*
2182 * The bind_mask is a mode_t that may be unsigned, so we can't use
2183 * M_CP_INTOPT - it does a signed comparison that causes compiler
2184 * warnings.
2185 */
dtucker@openbsd.org9faae502016-05-04 14:00:09 +00002186 if (src->fwd_opts.streamlocal_bind_mask != (mode_t)-1) {
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002187 dst->fwd_opts.streamlocal_bind_mask =
2188 src->fwd_opts.streamlocal_bind_mask;
2189 }
2190
Damien Miller534b2cc2013-12-05 14:07:27 +11002191 /* M_CP_STROPT and M_CP_STRARRAYOPT should not appear before here */
2192#define M_CP_STROPT(n) do {\
2193 if (src->n != NULL && dst->n != src->n) { \
2194 free(dst->n); \
2195 dst->n = src->n; \
2196 } \
2197} while(0)
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00002198#define M_CP_STRARRAYOPT(s, num_s) do {\
2199 u_int i; \
2200 if (src->num_s != 0) { \
2201 for (i = 0; i < dst->num_s; i++) \
2202 free(dst->s[i]); \
2203 free(dst->s); \
2204 dst->s = xcalloc(src->num_s, sizeof(*dst->s)); \
2205 for (i = 0; i < src->num_s; i++) \
2206 dst->s[i] = xstrdup(src->s[i]); \
2207 dst->num_s = src->num_s; \
djm@openbsd.org66bf74a2017-10-02 19:33:20 +00002208 } \
2209} while(0)
Damien Miller534b2cc2013-12-05 14:07:27 +11002210
Damien Millerf2e407e2011-05-20 19:04:14 +10002211 /* See comment in servconf.h */
2212 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10002213
djm@openbsd.orged085102015-10-29 08:05:01 +00002214 /* Arguments that accept '+...' need to be expanded */
2215 assemble_algorithms(dst);
2216
Damien Millerc2411902011-05-20 19:03:49 +10002217 /*
2218 * The only things that should be below this point are string options
2219 * which are only used after authentication.
2220 */
Damien Miller5d74e582011-05-20 19:03:31 +10002221 if (preauth)
2222 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002223
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002224 /* These options may be "none" to clear a global setting */
Damien Miller5d74e582011-05-20 19:03:31 +10002225 M_CP_STROPT(adm_forced_command);
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002226 if (option_clear_or_none(dst->adm_forced_command)) {
2227 free(dst->adm_forced_command);
2228 dst->adm_forced_command = NULL;
2229 }
Damien Miller5d74e582011-05-20 19:03:31 +10002230 M_CP_STROPT(chroot_directory);
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002231 if (option_clear_or_none(dst->chroot_directory)) {
2232 free(dst->chroot_directory);
2233 dst->chroot_directory = NULL;
2234 }
Darren Tucker45150472006-07-12 22:34:17 +10002235}
2236
Darren Tucker1629c072007-02-19 22:25:37 +11002237#undef M_CP_INTOPT
2238#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10002239#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11002240
Darren Tucker45150472006-07-12 22:34:17 +10002241void
2242parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002243 struct connection_info *connectinfo)
Darren Tucker45150472006-07-12 22:34:17 +10002244{
2245 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10002246 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10002247
2248 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
2249
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002250 if ((obuf = cbuf = sshbuf_dup_string(conf)) == NULL)
2251 fatal("%s: sshbuf_dup_string failed", __func__);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002252 active = connectinfo ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10002253 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11002254 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10002255 if (process_server_config_line(options, cp, filename,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002256 linenum++, &active, connectinfo) != 0)
Damien Miller95def091999-11-25 00:26:21 +11002257 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002258 }
Darren Tuckera627d422013-06-02 07:31:17 +10002259 free(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00002260 if (bad_options > 0)
2261 fatal("%s: terminating, %d bad configuration options",
2262 filename, bad_options);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00002263 process_queued_listen_addrs(options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002264}
Darren Tuckere7140f22008-06-10 23:01:51 +10002265
2266static const char *
Damien Miller82c55872011-06-23 08:20:30 +10002267fmt_multistate_int(int val, const struct multistate *m)
2268{
2269 u_int i;
2270
2271 for (i = 0; m[i].key != NULL; i++) {
2272 if (m[i].value == val)
2273 return m[i].key;
2274 }
2275 return "UNKNOWN";
2276}
2277
2278static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10002279fmt_intarg(ServerOpCodes code, int val)
2280{
Damien Miller82c55872011-06-23 08:20:30 +10002281 if (val == -1)
2282 return "unset";
2283 switch (code) {
2284 case sAddressFamily:
2285 return fmt_multistate_int(val, multistate_addressfamily);
2286 case sPermitRootLogin:
2287 return fmt_multistate_int(val, multistate_permitrootlogin);
2288 case sGatewayPorts:
2289 return fmt_multistate_int(val, multistate_gatewayports);
2290 case sCompression:
2291 return fmt_multistate_int(val, multistate_compression);
Damien Milleraa5b3f82012-12-03 09:50:54 +11002292 case sAllowTcpForwarding:
2293 return fmt_multistate_int(val, multistate_tcpfwd);
Damien Miller7acefbb2014-07-18 14:11:24 +10002294 case sAllowStreamLocalForwarding:
2295 return fmt_multistate_int(val, multistate_tcpfwd);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002296 case sFingerprintHash:
2297 return ssh_digest_alg_name(val);
Damien Miller82c55872011-06-23 08:20:30 +10002298 default:
2299 switch (val) {
2300 case 0:
2301 return "no";
2302 case 1:
2303 return "yes";
2304 default:
2305 return "UNKNOWN";
2306 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002307 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002308}
2309
2310static const char *
2311lookup_opcode_name(ServerOpCodes code)
2312{
2313 u_int i;
2314
2315 for (i = 0; keywords[i].name != NULL; i++)
2316 if (keywords[i].opcode == code)
2317 return(keywords[i].name);
2318 return "UNKNOWN";
2319}
2320
2321static void
2322dump_cfg_int(ServerOpCodes code, int val)
2323{
2324 printf("%s %d\n", lookup_opcode_name(code), val);
2325}
2326
2327static void
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002328dump_cfg_oct(ServerOpCodes code, int val)
2329{
2330 printf("%s 0%o\n", lookup_opcode_name(code), val);
2331}
2332
2333static void
Darren Tuckere7140f22008-06-10 23:01:51 +10002334dump_cfg_fmtint(ServerOpCodes code, int val)
2335{
2336 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2337}
2338
2339static void
2340dump_cfg_string(ServerOpCodes code, const char *val)
2341{
djm@openbsd.org161cf412014-12-22 07:55:51 +00002342 printf("%s %s\n", lookup_opcode_name(code),
2343 val == NULL ? "none" : val);
Darren Tuckere7140f22008-06-10 23:01:51 +10002344}
2345
2346static void
2347dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
2348{
2349 u_int i;
2350
2351 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10002352 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2353}
2354
2355static void
2356dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
2357{
2358 u_int i;
2359
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002360 if (count <= 0 && code != sAuthenticationMethods)
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002361 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002362 printf("%s", lookup_opcode_name(code));
2363 for (i = 0; i < count; i++)
2364 printf(" %s", vals[i]);
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002365 if (code == sAuthenticationMethods && count == 0)
2366 printf(" any");
Damien Millerd8478b62011-05-29 21:39:36 +10002367 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002368}
2369
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002370static char *
2371format_listen_addrs(struct listenaddr *la)
Darren Tuckere7140f22008-06-10 23:01:51 +10002372{
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002373 int r;
Darren Tuckere7140f22008-06-10 23:01:51 +10002374 struct addrinfo *ai;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002375 char addr[NI_MAXHOST], port[NI_MAXSERV];
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002376 char *laddr1 = xstrdup(""), *laddr2 = NULL;
Darren Tuckere7140f22008-06-10 23:01:51 +10002377
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002378 /*
2379 * ListenAddress must be after Port. add_one_listen_addr pushes
2380 * addresses onto a stack, so to maintain ordering we need to
2381 * print these in reverse order.
2382 */
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002383 for (ai = la->addrs; ai; ai = ai->ai_next) {
2384 if ((r = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
Darren Tuckere7140f22008-06-10 23:01:51 +10002385 sizeof(addr), port, sizeof(port),
2386 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002387 error("getnameinfo: %.100s", ssh_gai_strerror(r));
2388 continue;
Darren Tuckere7140f22008-06-10 23:01:51 +10002389 }
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002390 laddr2 = laddr1;
2391 if (ai->ai_family == AF_INET6) {
2392 xasprintf(&laddr1, "listenaddress [%s]:%s%s%s\n%s",
2393 addr, port,
2394 la->rdomain == NULL ? "" : " rdomain ",
2395 la->rdomain == NULL ? "" : la->rdomain,
2396 laddr2);
2397 } else {
2398 xasprintf(&laddr1, "listenaddress %s:%s%s%s\n%s",
2399 addr, port,
2400 la->rdomain == NULL ? "" : " rdomain ",
2401 la->rdomain == NULL ? "" : la->rdomain,
2402 laddr2);
2403 }
2404 free(laddr2);
Darren Tuckere7140f22008-06-10 23:01:51 +10002405 }
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002406 return laddr1;
2407}
2408
2409void
2410dump_config(ServerOptions *o)
2411{
2412 char *s;
2413 u_int i;
2414
2415 /* these are usually at the top of the config */
2416 for (i = 0; i < o->num_ports; i++)
2417 printf("port %d\n", o->ports[i]);
2418 dump_cfg_fmtint(sAddressFamily, o->address_family);
2419
2420 for (i = 0; i < o->num_listen_addrs; i++) {
2421 s = format_listen_addrs(&o->listen_addrs[i]);
2422 printf("%s", s);
2423 free(s);
2424 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002425
2426 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10002427#ifdef USE_PAM
Darren Tucker70860b62015-04-17 10:56:13 +10002428 dump_cfg_fmtint(sUsePAM, o->use_pam);
Damien Miller212f0b02008-07-23 17:42:29 +10002429#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002430 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
Darren Tuckere7140f22008-06-10 23:01:51 +10002431 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
2432 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11002433 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10002434 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
2435 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002436 dump_cfg_oct(sStreamLocalBindMask, o->fwd_opts.streamlocal_bind_mask);
Darren Tuckere7140f22008-06-10 23:01:51 +10002437
2438 /* formatted integer arguments */
2439 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
2440 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
2441 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
Darren Tuckere7140f22008-06-10 23:01:51 +10002442 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
2443 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
2444 o->hostbased_uses_name_from_packet_only);
Darren Tuckere7140f22008-06-10 23:01:51 +10002445 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10002446#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10002447 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
2448 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
2449 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10002450# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10002451 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10002452# endif
2453#endif
2454#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10002455 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2456 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10002457#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002458 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2459 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2460 o->kbd_interactive_authentication);
2461 dump_cfg_fmtint(sChallengeResponseAuthentication,
2462 o->challenge_response_authentication);
2463 dump_cfg_fmtint(sPrintMotd, o->print_motd);
Darren Tuckerfd4e4f22016-02-24 10:44:25 +11002464#ifndef DISABLE_LASTLOG
Darren Tuckere7140f22008-06-10 23:01:51 +10002465 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
Darren Tuckerfd4e4f22016-02-24 10:44:25 +11002466#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002467 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
2468 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002469 dump_cfg_fmtint(sPermitTTY, o->permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002470 dump_cfg_fmtint(sPermitUserRC, o->permit_user_rc);
Darren Tuckere7140f22008-06-10 23:01:51 +10002471 dump_cfg_fmtint(sStrictModes, o->strict_modes);
2472 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
2473 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
2474 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
Darren Tuckere7140f22008-06-10 23:01:51 +10002475 dump_cfg_fmtint(sCompression, o->compression);
Damien Miller7acefbb2014-07-18 14:11:24 +10002476 dump_cfg_fmtint(sGatewayPorts, o->fwd_opts.gateway_ports);
Darren Tuckere7140f22008-06-10 23:01:51 +10002477 dump_cfg_fmtint(sUseDNS, o->use_dns);
2478 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002479 dump_cfg_fmtint(sAllowAgentForwarding, o->allow_agent_forwarding);
djm@openbsd.org7844f352016-11-30 03:00:05 +00002480 dump_cfg_fmtint(sDisableForwarding, o->disable_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002481 dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
djm@openbsd.org771c2f52016-05-03 15:25:06 +00002482 dump_cfg_fmtint(sStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002483 dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
djm@openbsd.org8f574952017-06-24 06:34:38 +00002484 dump_cfg_fmtint(sExposeAuthInfo, o->expose_userauth_info);
Darren Tuckere7140f22008-06-10 23:01:51 +10002485
2486 /* string arguments */
2487 dump_cfg_string(sPidFile, o->pid_file);
2488 dump_cfg_string(sXAuthLocation, o->xauth_location);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002489 dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
2490 dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
Darren Tuckere7140f22008-06-10 23:01:51 +10002491 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10002492 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11002493 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11002494 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
2495 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10002496 dump_cfg_string(sAuthorizedPrincipalsFile,
2497 o->authorized_principals_file);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002498 dump_cfg_string(sVersionAddendum, *o->version_addendum == '\0'
2499 ? "none" : o->version_addendum);
Damien Miller09d3e122012-10-31 08:58:58 +11002500 dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
2501 dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +00002502 dump_cfg_string(sAuthorizedPrincipalsCommand, o->authorized_principals_command);
2503 dump_cfg_string(sAuthorizedPrincipalsCommandUser, o->authorized_principals_command_user);
Damien Miller85b45e02013-07-20 13:21:52 +10002504 dump_cfg_string(sHostKeyAgent, o->host_key_agent);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002505 dump_cfg_string(sKexAlgorithms,
djm@openbsd.org259a02e2014-10-13 00:38:35 +00002506 o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002507 dump_cfg_string(sHostbasedAcceptedKeyTypes, o->hostbased_key_types ?
2508 o->hostbased_key_types : KEX_DEFAULT_PK_ALG);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002509 dump_cfg_string(sHostKeyAlgorithms, o->hostkeyalgorithms ?
2510 o->hostkeyalgorithms : KEX_DEFAULT_PK_ALG);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002511 dump_cfg_string(sPubkeyAcceptedKeyTypes, o->pubkey_key_types ?
2512 o->pubkey_key_types : KEX_DEFAULT_PK_ALG);
djm@openbsd.org35eb33f2017-10-25 00:17:08 +00002513 dump_cfg_string(sRDomain, o->routing_domain);
Darren Tuckere7140f22008-06-10 23:01:51 +10002514
2515 /* string arguments requiring a lookup */
2516 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
2517 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
2518
2519 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10002520 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
2521 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002522 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
2523 o->host_key_files);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002524 dump_cfg_strarray(sHostCertificate, o->num_host_cert_files,
Damien Miller0a80ca12010-02-27 07:55:05 +11002525 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002526 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
2527 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
2528 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
2529 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
2530 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
Damien Millera6e3f012012-11-04 23:21:40 +11002531 dump_cfg_strarray_oneline(sAuthenticationMethods,
2532 o->num_auth_methods, o->auth_methods);
Darren Tuckere7140f22008-06-10 23:01:51 +10002533
2534 /* other arguments */
2535 for (i = 0; i < o->num_subsystems; i++)
2536 printf("subsystem %s %s\n", o->subsystem_name[i],
2537 o->subsystem_args[i]);
2538
2539 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
2540 o->max_startups_rate, o->max_startups);
2541
djm@openbsd.orgd685e5a2017-10-25 02:10:39 +00002542 s = NULL;
2543 for (i = 0; tunmode_desc[i].val != -1; i++) {
Darren Tuckere7140f22008-06-10 23:01:51 +10002544 if (tunmode_desc[i].val == o->permit_tun) {
2545 s = tunmode_desc[i].text;
2546 break;
2547 }
djm@openbsd.orgd685e5a2017-10-25 02:10:39 +00002548 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002549 dump_cfg_string(sPermitTunnel, s);
2550
Damien Miller91475862011-05-05 14:14:34 +10002551 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2552 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11002553
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002554 printf("rekeylimit %llu %d\n", (unsigned long long)o->rekey_limit,
Damien Millera6d6c1f2013-08-21 02:40:01 +10002555 o->rekey_interval);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002556
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002557 printf("permitopen");
2558 if (o->num_permitted_opens == 0)
2559 printf(" any");
2560 else {
2561 for (i = 0; i < o->num_permitted_opens; i++)
2562 printf(" %s", o->permitted_opens[i]);
2563 }
2564 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002565}