blob: 68db047f2b7a5ef45e02db4eda2a40a08d82dddd [file] [log] [blame]
djm@openbsd.org868109b2015-07-01 02:39:06 +00001
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002/* $OpenBSD: servconf.c,v 1.315 2017/10/25 00:15:35 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003/*
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
Damien Millere3b60b52006-07-10 21:08:03 +100016#include <sys/types.h>
17#include <sys/socket.h>
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000018#ifdef HAVE_SYS_SYSCTL_H
19#include <sys/sysctl.h>
20#endif
Damien Millere3b60b52006-07-10 21:08:03 +100021
Damien Miller0dac6fb2010-11-20 15:19:38 +110022#include <netinet/in.h>
23#include <netinet/in_systm.h>
24#include <netinet/ip.h>
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000025#ifdef HAVE_NET_ROUTE_H
26#include <net/route.h>
27#endif
Damien Miller0dac6fb2010-11-20 15:19:38 +110028
Darren Tucker5f96f3b2013-05-16 20:29:28 +100029#include <ctype.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100030#include <netdb.h>
Damien Miller565ca3f2006-08-19 00:23:15 +100031#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100032#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100033#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100034#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100035#include <signal.h>
Damien Millere6b3b612006-07-24 14:01:23 +100036#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000037#include <limits.h>
Damien Millerd7834352006-08-05 12:39:39 +100038#include <stdarg.h>
Darren Tuckere7140f22008-06-10 23:01:51 +100039#include <errno.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100040#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100041#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100042#endif
Damien Millerbe43ebf2006-07-24 13:51:51 +100043
Damien Millerb84886b2008-05-19 15:05:07 +100044#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100045#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100048#include "buffer.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100049#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050#include "servconf.h"
Damien Miller78928792000-04-12 20:17:38 +100051#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100054#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000055#include "kex.h"
56#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100057#include "match.h"
Damien Miller9b439df2006-07-24 14:04:00 +100058#include "channels.h"
Damien Miller565ca3f2006-08-19 00:23:15 +100059#include "groupaccess.h"
Darren Tuckerfbcf8272012-05-19 19:37:01 +100060#include "canohost.h"
61#include "packet.h"
Damien Millera6e3f012012-11-04 23:21:40 +110062#include "hostfile.h"
63#include "auth.h"
djm@openbsd.org57d378e2014-08-19 23:58:28 +000064#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000065#include "digest.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000067static void add_listen_addr(ServerOptions *, const char *,
68 const char *, int);
69static void add_one_listen_addr(ServerOptions *, const char *,
70 const char *, int);
Damien Miller34132e52000-01-14 15:45:46 +110071
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000072/* Use of privilege separation or not */
73extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100074extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000075
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076/* Initializes the server options to their default values. */
77
Damien Miller4af51302000-04-16 11:18:38 +100078void
Damien Miller95def091999-11-25 00:26:21 +110079initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080{
Damien Miller95def091999-11-25 00:26:21 +110081 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110082
83 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100084 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110085
86 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110087 options->num_ports = 0;
88 options->ports_from_cmdline = 0;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +000089 options->queued_listen_addrs = NULL;
90 options->num_queued_listens = 0;
Damien Miller34132e52000-01-14 15:45:46 +110091 options->listen_addrs = NULL;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +000092 options->num_listen_addrs = 0;
Darren Tucker0f383232005-01-20 10:57:56 +110093 options->address_family = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110094 options->num_host_key_files = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110095 options->num_host_cert_files = 0;
Damien Miller85b45e02013-07-20 13:21:52 +100096 options->host_key_agent = NULL;
Damien Miller6f83b8e2000-05-02 09:23:45 +100097 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110098 options->login_grace_time = -1;
chris@openbsd.org3d5728a2015-07-31 15:38:09 +000099 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +1100100 options->ignore_rhosts = -1;
101 options->ignore_user_known_hosts = -1;
102 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000103 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +1100104 options->x11_forwarding = -1;
105 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +1100106 options->x11_use_localhost = -1;
Damien Miller5ff30c62013-10-30 22:21:50 +1100107 options->permit_tty = -1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000108 options->permit_user_rc = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000109 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100110 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +1100111 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +1100112 options->log_facility = SYSLOG_FACILITY_NOT_SET;
113 options->log_level = SYSLOG_LEVEL_NOT_SET;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000114 options->hostbased_authentication = -1;
115 options->hostbased_uses_name_from_packet_only = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000116 options->hostbased_key_types = NULL;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000117 options->hostkeyalgorithms = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100118 options->pubkey_authentication = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000119 options->pubkey_key_types = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100120 options->kerberos_authentication = -1;
121 options->kerberos_or_local_passwd = -1;
122 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100123 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000124 options->gss_authentication=-1;
125 options->gss_cleanup_creds = -1;
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000126 options->gss_strict_acceptor = -1;
Damien Miller95def091999-11-25 00:26:21 +1100127 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100128 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000129 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100130 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000131 options->permit_user_env = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000132 options->compression = -1;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000133 options->rekey_limit = -1;
134 options->rekey_interval = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100135 options->allow_tcp_forwarding = -1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000136 options->allow_streamlocal_forwarding = -1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000137 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100138 options->num_allow_users = 0;
139 options->num_deny_users = 0;
140 options->num_allow_groups = 0;
141 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000142 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000143 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000144 options->kex_algorithms = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +1000145 options->fwd_opts.gateway_ports = -1;
146 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
147 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000148 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000149 options->max_startups_begin = -1;
150 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000151 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000152 options->max_authtries = -1;
Damien Miller7207f642008-05-19 15:34:50 +1000153 options->max_sessions = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000154 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000155 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000156 options->client_alive_interval = -1;
157 options->client_alive_count_max = -1;
Damien Millerd8478b62011-05-29 21:39:36 +1000158 options->num_authkeys_files = 0;
Darren Tucker46bc0752004-05-02 22:11:30 +1000159 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100160 options->permit_tun = -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000161 options->permitted_opens = NULL;
Damien Millere2754432006-07-24 14:06:47 +1000162 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100163 options->chroot_directory = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +1100164 options->authorized_keys_command = NULL;
165 options->authorized_keys_command_user = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100166 options->revoked_keys_file = NULL;
167 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000168 options->authorized_principals_file = NULL;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000169 options->authorized_principals_command = NULL;
170 options->authorized_principals_command_user = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100171 options->ip_qos_interactive = -1;
172 options->ip_qos_bulk = -1;
Damien Miller23528812012-04-22 11:24:43 +1000173 options->version_addendum = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000174 options->fingerprint_hash = -1;
djm@openbsd.org7844f352016-11-30 03:00:05 +0000175 options->disable_forwarding = -1;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000176 options->expose_userauth_info = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177}
178
djm@openbsd.org161cf412014-12-22 07:55:51 +0000179/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
180static int
181option_clear_or_none(const char *o)
182{
183 return o == NULL || strcasecmp(o, "none") == 0;
184}
185
djm@openbsd.orged085102015-10-29 08:05:01 +0000186static void
187assemble_algorithms(ServerOptions *o)
188{
189 if (kex_assemble_names(KEX_SERVER_ENCRYPT, &o->ciphers) != 0 ||
190 kex_assemble_names(KEX_SERVER_MAC, &o->macs) != 0 ||
191 kex_assemble_names(KEX_SERVER_KEX, &o->kex_algorithms) != 0 ||
192 kex_assemble_names(KEX_DEFAULT_PK_ALG,
193 &o->hostkeyalgorithms) != 0 ||
194 kex_assemble_names(KEX_DEFAULT_PK_ALG,
195 &o->hostbased_key_types) != 0 ||
196 kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->pubkey_key_types) != 0)
197 fatal("kex_assemble_names failed");
198}
199
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000200static void
201array_append(const char *file, const int line, const char *directive,
202 char ***array, u_int *lp, const char *s)
203{
204
205 if (*lp >= INT_MAX)
206 fatal("%s line %d: Too many %s entries", file, line, directive);
207
208 *array = xrecallocarray(*array, *lp, *lp + 1, sizeof(**array));
209 (*array)[*lp] = xstrdup(s);
210 (*lp)++;
211}
212
213void
214servconf_add_hostkey(const char *file, const int line,
215 ServerOptions *options, const char *path)
216{
217 char *apath = derelativise_path(path);
218
219 array_append(file, line, "HostKey",
220 &options->host_key_files, &options->num_host_key_files, apath);
221 free(apath);
222}
223
224void
225servconf_add_hostcert(const char *file, const int line,
226 ServerOptions *options, const char *path)
227{
228 char *apath = derelativise_path(path);
229
230 array_append(file, line, "HostCertificate",
231 &options->host_cert_files, &options->num_host_cert_files, apath);
232 free(apath);
233}
234
Damien Miller4af51302000-04-16 11:18:38 +1000235void
Damien Miller95def091999-11-25 00:26:21 +1100236fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237{
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000238 u_int i;
djm@openbsd.org161cf412014-12-22 07:55:51 +0000239
Damien Miller726273e2001-11-12 11:40:11 +1100240 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000241 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000242 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100243
244 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100245 if (options->num_host_key_files == 0) {
246 /* fill default hostkeys for protocols */
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000247 servconf_add_hostkey("[default]", 0, options,
248 _PATH_HOST_RSA_KEY_FILE);
249 servconf_add_hostkey("[default]", 0, options,
250 _PATH_HOST_DSA_KEY_FILE);
Damien Millerdd190dd2010-11-11 14:17:02 +1100251#ifdef OPENSSL_HAS_ECC
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000252 servconf_add_hostkey("[default]", 0, options,
253 _PATH_HOST_ECDSA_KEY_FILE);
Damien Millerdd190dd2010-11-11 14:17:02 +1100254#endif
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000255 servconf_add_hostkey("[default]", 0, options,
256 _PATH_HOST_ED25519_KEY_FILE);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100257 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100258 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100259 if (options->num_ports == 0)
260 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000261 if (options->address_family == -1)
262 options->address_family = AF_UNSPEC;
Damien Miller34132e52000-01-14 15:45:46 +1100263 if (options->listen_addrs == NULL)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000264 add_listen_addr(options, NULL, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000265 if (options->pid_file == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000266 options->pid_file = xstrdup(_PATH_SSH_DAEMON_PID_FILE);
Damien Miller95def091999-11-25 00:26:21 +1100267 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000268 options->login_grace_time = 120;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000269 if (options->permit_root_login == PERMIT_NOT_SET)
chris@openbsd.org3d5728a2015-07-31 15:38:09 +0000270 options->permit_root_login = PERMIT_NO_PASSWD;
Damien Miller95def091999-11-25 00:26:21 +1100271 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100272 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100273 if (options->ignore_user_known_hosts == -1)
274 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100275 if (options->print_motd == -1)
276 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000277 if (options->print_lastlog == -1)
278 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100279 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100280 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100281 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100282 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100283 if (options->x11_use_localhost == -1)
284 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000285 if (options->xauth_location == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000286 options->xauth_location = xstrdup(_PATH_XAUTH);
Damien Miller5ff30c62013-10-30 22:21:50 +1100287 if (options->permit_tty == -1)
288 options->permit_tty = 1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000289 if (options->permit_user_rc == -1)
290 options->permit_user_rc = 1;
Damien Miller95def091999-11-25 00:26:21 +1100291 if (options->strict_modes == -1)
292 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100293 if (options->tcp_keep_alive == -1)
294 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100295 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100296 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100297 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000298 options->log_level = SYSLOG_LEVEL_INFO;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000299 if (options->hostbased_authentication == -1)
300 options->hostbased_authentication = 0;
301 if (options->hostbased_uses_name_from_packet_only == -1)
302 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100303 if (options->pubkey_authentication == -1)
304 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100305 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000306 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100307 if (options->kerberos_or_local_passwd == -1)
308 options->kerberos_or_local_passwd = 1;
309 if (options->kerberos_ticket_cleanup == -1)
310 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100311 if (options->kerberos_get_afs_token == -1)
312 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000313 if (options->gss_authentication == -1)
314 options->gss_authentication = 0;
315 if (options->gss_cleanup_creds == -1)
316 options->gss_cleanup_creds = 1;
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000317 if (options->gss_strict_acceptor == -1)
djm@openbsd.org13bd2e22017-01-06 03:45:41 +0000318 options->gss_strict_acceptor = 1;
Damien Miller95def091999-11-25 00:26:21 +1100319 if (options->password_authentication == -1)
320 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100321 if (options->kbd_interactive_authentication == -1)
322 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000323 if (options->challenge_response_authentication == -1)
324 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100325 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100326 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000327 if (options->permit_user_env == -1)
328 options->permit_user_env = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000329 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000330 options->compression = COMP_DELAYED;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000331 if (options->rekey_limit == -1)
332 options->rekey_limit = 0;
333 if (options->rekey_interval == -1)
334 options->rekey_interval = 0;
Damien Miller50a41ed2000-10-16 12:14:42 +1100335 if (options->allow_tcp_forwarding == -1)
Damien Milleraa5b3f82012-12-03 09:50:54 +1100336 options->allow_tcp_forwarding = FORWARD_ALLOW;
Damien Miller7acefbb2014-07-18 14:11:24 +1000337 if (options->allow_streamlocal_forwarding == -1)
338 options->allow_streamlocal_forwarding = FORWARD_ALLOW;
Damien Miller4f755cd2008-05-19 14:57:41 +1000339 if (options->allow_agent_forwarding == -1)
340 options->allow_agent_forwarding = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000341 if (options->fwd_opts.gateway_ports == -1)
342 options->fwd_opts.gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000343 if (options->max_startups == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100344 options->max_startups = 100;
Damien Miller942da032000-08-18 13:59:06 +1000345 if (options->max_startups_rate == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100346 options->max_startups_rate = 30; /* 30% */
Damien Miller942da032000-08-18 13:59:06 +1000347 if (options->max_startups_begin == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100348 options->max_startups_begin = 10;
Darren Tucker89413db2004-05-24 10:36:23 +1000349 if (options->max_authtries == -1)
350 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000351 if (options->max_sessions == -1)
352 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000353 if (options->use_dns == -1)
deraadt@openbsd.org3cd51032015-02-02 01:57:44 +0000354 options->use_dns = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000355 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100356 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000357 if (options->client_alive_count_max == -1)
358 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000359 if (options->num_authkeys_files == 0) {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +0000360 array_append("[default]", 0, "AuthorizedKeysFiles",
361 &options->authorized_keys_files,
362 &options->num_authkeys_files,
363 _PATH_SSH_USER_PERMITTED_KEYS);
364 array_append("[default]", 0, "AuthorizedKeysFiles",
365 &options->authorized_keys_files,
366 &options->num_authkeys_files,
367 _PATH_SSH_USER_PERMITTED_KEYS2);
Damien Millerd8478b62011-05-29 21:39:36 +1000368 }
Damien Millerd27b9472005-12-13 19:29:02 +1100369 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100370 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100371 if (options->ip_qos_interactive == -1)
372 options->ip_qos_interactive = IPTOS_LOWDELAY;
373 if (options->ip_qos_bulk == -1)
374 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller23528812012-04-22 11:24:43 +1000375 if (options->version_addendum == NULL)
376 options->version_addendum = xstrdup("");
Damien Miller7acefbb2014-07-18 14:11:24 +1000377 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
378 options->fwd_opts.streamlocal_bind_mask = 0177;
379 if (options->fwd_opts.streamlocal_bind_unlink == -1)
380 options->fwd_opts.streamlocal_bind_unlink = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000381 if (options->fingerprint_hash == -1)
382 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
djm@openbsd.org7844f352016-11-30 03:00:05 +0000383 if (options->disable_forwarding == -1)
384 options->disable_forwarding = 0;
djm@openbsd.org8f574952017-06-24 06:34:38 +0000385 if (options->expose_userauth_info == -1)
386 options->expose_userauth_info = 0;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000387
djm@openbsd.orged085102015-10-29 08:05:01 +0000388 assemble_algorithms(options);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000389
djm@openbsd.orgc5c3f322016-02-17 05:29:04 +0000390 /* Turn privilege separation and sandboxing on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000391 if (use_privsep == -1)
djm@openbsd.orgc5c3f322016-02-17 05:29:04 +0000392 use_privsep = PRIVSEP_ON;
Damien Miller4903eb42002-06-21 16:20:44 +1000393
djm@openbsd.org161cf412014-12-22 07:55:51 +0000394#define CLEAR_ON_NONE(v) \
395 do { \
396 if (option_clear_or_none(v)) { \
397 free(v); \
398 v = NULL; \
399 } \
400 } while(0)
401 CLEAR_ON_NONE(options->pid_file);
402 CLEAR_ON_NONE(options->xauth_location);
403 CLEAR_ON_NONE(options->banner);
404 CLEAR_ON_NONE(options->trusted_user_ca_keys);
405 CLEAR_ON_NONE(options->revoked_keys_file);
djm@openbsd.org7e8528c2015-05-01 04:17:51 +0000406 CLEAR_ON_NONE(options->authorized_principals_file);
djm@openbsd.org9fd04682015-11-13 04:38:06 +0000407 CLEAR_ON_NONE(options->adm_forced_command);
408 CLEAR_ON_NONE(options->chroot_directory);
djm@openbsd.org161cf412014-12-22 07:55:51 +0000409 for (i = 0; i < options->num_host_key_files; i++)
410 CLEAR_ON_NONE(options->host_key_files[i]);
411 for (i = 0; i < options->num_host_cert_files; i++)
412 CLEAR_ON_NONE(options->host_cert_files[i]);
413#undef CLEAR_ON_NONE
414
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +0000415 /* Similar handling for AuthenticationMethods=any */
416 if (options->num_auth_methods == 1 &&
417 strcmp(options->auth_methods[0], "any") == 0) {
418 free(options->auth_methods[0]);
419 options->auth_methods[0] = NULL;
420 options->num_auth_methods = 0;
421 }
422
Tim Rice40017b02002-07-14 13:36:49 -0700423#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000424 if (use_privsep && options->compression == 1) {
425 error("This platform does not support both privilege "
426 "separation and compression");
427 error("Compression disabled");
428 options->compression = 0;
429 }
430#endif
431
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432}
433
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100435typedef enum {
436 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100437 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000438 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100439 /* Standard Options */
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000440 sPort, sHostKeyFile, sLoginGraceTime,
441 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000442 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100443 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100444 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000445 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100446 sPasswordAuthentication, sKbdInteractiveAuthentication,
447 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000448 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100449 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller5ff30c62013-10-30 22:21:50 +1100450 sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
djm@openbsd.org83b58182016-08-19 03:18:06 +0000451 sPermitUserEnvironment, sAllowTcpForwarding, sCompression,
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000452 sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000453 sIgnoreUserKnownHosts, sCiphers, sMacs, sPidFile,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000454 sGatewayPorts, sPubkeyAuthentication, sPubkeyAcceptedKeyTypes,
455 sXAuthLocation, sSubsystem, sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000456 sBanner, sUseDNS, sHostbasedAuthentication,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000457 sHostbasedUsesNameFromPacketOnly, sHostbasedAcceptedKeyTypes,
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000458 sHostKeyAlgorithms,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000459 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000460 sGssAuthentication, sGssCleanupCreds, sGssStrictAcceptor,
461 sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100462 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100463 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller7cc194f2014-02-04 11:12:56 +1100464 sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000465 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000466 sAuthorizedPrincipalsCommand, sAuthorizedPrincipalsCommandUser,
Damien Miller23528812012-04-22 11:24:43 +1000467 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Miller09d3e122012-10-31 08:58:58 +1100468 sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
Damien Miller72e6b5c2014-07-04 09:00:04 +1000469 sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
Damien Miller7acefbb2014-07-18 14:11:24 +1000470 sStreamLocalBindMask, sStreamLocalBindUnlink,
djm@openbsd.org7844f352016-11-30 03:00:05 +0000471 sAllowStreamLocalForwarding, sFingerprintHash, sDisableForwarding,
djm@openbsd.org8f574952017-06-24 06:34:38 +0000472 sExposeAuthInfo,
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000473 sDeprecated, sIgnore, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474} ServerOpCodes;
475
Darren Tucker45150472006-07-12 22:34:17 +1000476#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
477#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
478#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
479
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100481static struct {
482 const char *name;
483 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000484 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100485} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100486 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000487#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000488 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000489#else
Darren Tucker45150472006-07-12 22:34:17 +1000490 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000491#endif
Darren Tucker45150472006-07-12 22:34:17 +1000492 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100493 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000494 { "port", sPort, SSHCFG_GLOBAL },
495 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
496 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
Damien Miller85b45e02013-07-20 13:21:52 +1000497 { "hostkeyagent", sHostKeyAgent, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000498 { "pidfile", sPidFile, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000499 { "serverkeybits", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000500 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000501 { "keyregenerationinterval", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100502 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000503 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
djm@openbsd.org54cd41a2017-05-17 01:24:17 +0000504 { "loglevel", sLogLevel, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000505 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000506 { "rhostsrsaauthentication", sDeprecated, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100507 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000508 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000509 { "hostbasedacceptedkeytypes", sHostbasedAcceptedKeyTypes, SSHCFG_ALL },
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000510 { "hostkeyalgorithms", sHostKeyAlgorithms, SSHCFG_GLOBAL },
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +0000511 { "rsaauthentication", sDeprecated, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100512 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000513 { "pubkeyacceptedkeytypes", sPubkeyAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000514 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000515#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100516 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000517 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
518 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100519#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000520 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000521#else
Darren Tucker45150472006-07-12 22:34:17 +1000522 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100523#endif
524#else
Darren Tucker1629c072007-02-19 22:25:37 +1100525 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000526 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
527 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
528 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000529#endif
Darren Tucker45150472006-07-12 22:34:17 +1000530 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
531 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000532#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100533 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000534 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000535 { "gssapistrictacceptorcheck", sGssStrictAcceptor, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000536#else
Darren Tucker1629c072007-02-19 22:25:37 +1100537 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000538 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +0000539 { "gssapistrictacceptorcheck", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000540#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100541 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
542 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100543 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000544 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
545 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
546 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
547 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
548 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
Damien Millerac908c12015-10-22 09:35:24 +1100549#ifdef DISABLE_LASTLOG
550 { "printlastlog", sUnsupported, SSHCFG_GLOBAL },
551#else
Darren Tucker45150472006-07-12 22:34:17 +1000552 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
Damien Millerac908c12015-10-22 09:35:24 +1100553#endif
Darren Tucker45150472006-07-12 22:34:17 +1000554 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
555 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000556 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
557 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
558 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000559 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
560 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100561 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000562 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
djm@openbsd.org83b58182016-08-19 03:18:06 +0000563 { "uselogin", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000564 { "compression", sCompression, SSHCFG_GLOBAL },
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000565 { "rekeylimit", sRekeyLimit, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000566 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
567 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
568 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000569 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Damien Millerc24da772012-06-20 21:53:58 +1000570 { "allowusers", sAllowUsers, SSHCFG_ALL },
571 { "denyusers", sDenyUsers, SSHCFG_ALL },
572 { "allowgroups", sAllowGroups, SSHCFG_ALL },
573 { "denygroups", sDenyGroups, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000574 { "ciphers", sCiphers, SSHCFG_GLOBAL },
575 { "macs", sMacs, SSHCFG_GLOBAL },
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000576 { "protocol", sIgnore, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000577 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
578 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
579 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000580 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000581 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100582 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000583 { "usedns", sUseDNS, SSHCFG_GLOBAL },
584 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
585 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
markus@openbsd.orgf0ddede2016-11-23 23:14:15 +0000586 { "clientaliveinterval", sClientAliveInterval, SSHCFG_ALL },
587 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000588 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000589 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
djm@openbsd.org66705942017-03-14 07:19:07 +0000590 { "useprivilegeseparation", sDeprecated, SSHCFG_GLOBAL},
Damien Millerc24da772012-06-20 21:53:58 +1000591 { "acceptenv", sAcceptEnv, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000592 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Damien Miller5ff30c62013-10-30 22:21:50 +1100593 { "permittty", sPermitTTY, SSHCFG_ALL },
Damien Miller72e6b5c2014-07-04 09:00:04 +1000594 { "permituserrc", sPermitUserRC, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000595 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000596 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000597 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100598 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100599 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100600 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
601 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000602 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000603 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100604 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller09d3e122012-10-31 08:58:58 +1100605 { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
606 { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000607 { "authorizedprincipalscommand", sAuthorizedPrincipalsCommand, SSHCFG_ALL },
608 { "authorizedprincipalscommanduser", sAuthorizedPrincipalsCommandUser, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000609 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Damien Millera6e3f012012-11-04 23:21:40 +1100610 { "authenticationmethods", sAuthenticationMethods, SSHCFG_ALL },
Damien Miller7acefbb2014-07-18 14:11:24 +1000611 { "streamlocalbindmask", sStreamLocalBindMask, SSHCFG_ALL },
612 { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
613 { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000614 { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
djm@openbsd.org7844f352016-11-30 03:00:05 +0000615 { "disableforwarding", sDisableForwarding, SSHCFG_ALL },
djm@openbsd.org8f574952017-06-24 06:34:38 +0000616 { "exposeauthinfo", sExposeAuthInfo, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000617 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618};
619
Darren Tuckere7140f22008-06-10 23:01:51 +1000620static struct {
621 int val;
622 char *text;
623} tunmode_desc[] = {
624 { SSH_TUNMODE_NO, "no" },
625 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
626 { SSH_TUNMODE_ETHERNET, "ethernet" },
627 { SSH_TUNMODE_YES, "yes" },
628 { -1, NULL }
629};
630
Damien Miller5428f641999-11-25 11:54:57 +1100631/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000632 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100633 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634
Damien Miller4af51302000-04-16 11:18:38 +1000635static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100636parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000637 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000638{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000639 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000640
Damien Miller95def091999-11-25 00:26:21 +1100641 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000642 if (strcasecmp(cp, keywords[i].name) == 0) {
643 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100644 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000645 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000646
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000647 error("%s: line %d: Bad configuration option: %s",
648 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100649 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000650}
651
Darren Tucker88b6fb22010-01-13 22:44:29 +1100652char *
653derelativise_path(const char *path)
654{
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000655 char *expanded, *ret, cwd[PATH_MAX];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100656
djm@openbsd.org161cf412014-12-22 07:55:51 +0000657 if (strcasecmp(path, "none") == 0)
658 return xstrdup("none");
Darren Tucker88b6fb22010-01-13 22:44:29 +1100659 expanded = tilde_expand_filename(path, getuid());
660 if (*expanded == '/')
661 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100662 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100663 fatal("%s: getcwd: %s", __func__, strerror(errno));
664 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tuckera627d422013-06-02 07:31:17 +1000665 free(expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100666 return ret;
667}
668
Ben Lindstrombba81212001-06-25 05:01:22 +0000669static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000670add_listen_addr(ServerOptions *options, const char *addr,
671 const char *rdomain, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100672{
Damien Millereccb9de2005-06-17 12:59:34 +1000673 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100674
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000675 if (port > 0)
676 add_one_listen_addr(options, addr, rdomain, port);
677 else {
678 for (i = 0; i < options->num_ports; i++) {
679 add_one_listen_addr(options, addr, rdomain,
680 options->ports[i]);
681 }
682 }
Ben Lindstromc510af42001-04-07 17:25:48 +0000683}
684
Ben Lindstrombba81212001-06-25 05:01:22 +0000685static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000686add_one_listen_addr(ServerOptions *options, const char *addr,
687 const char *rdomain, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000688{
689 struct addrinfo hints, *ai, *aitop;
690 char strport[NI_MAXSERV];
691 int gaierr;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000692 u_int i;
693
694 /* Find listen_addrs entry for this rdomain */
695 for (i = 0; i < options->num_listen_addrs; i++) {
696 if (rdomain == NULL && options->listen_addrs[i].rdomain == NULL)
697 break;
698 if (rdomain == NULL || options->listen_addrs[i].rdomain == NULL)
699 continue;
700 if (strcmp(rdomain, options->listen_addrs[i].rdomain) == 0)
701 break;
702 }
703 if (i >= options->num_listen_addrs) {
704 /* No entry for this rdomain; allocate one */
705 if (i >= INT_MAX)
706 fatal("%s: too many listen addresses", __func__);
707 options->listen_addrs = xrecallocarray(options->listen_addrs,
708 options->num_listen_addrs, options->num_listen_addrs + 1,
709 sizeof(*options->listen_addrs));
710 i = options->num_listen_addrs++;
711 if (rdomain != NULL)
712 options->listen_addrs[i].rdomain = xstrdup(rdomain);
713 }
714 /* options->listen_addrs[i] points to the addresses for this rdomain */
Ben Lindstromc510af42001-04-07 17:25:48 +0000715
716 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100717 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000718 hints.ai_socktype = SOCK_STREAM;
719 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100720 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000721 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
722 fatal("bad addr or host: %s (%s)",
723 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100724 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000725 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
726 ;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000727 ai->ai_next = options->listen_addrs[i].addrs;
728 options->listen_addrs[i].addrs = aitop;
729}
730
731/* Returns nonzero if the routing domain name is valid */
732static int
733valid_rdomain(const char *name)
734{
735 const char *errstr;
736 long long num;
737 struct rt_tableinfo info;
738 int mib[6];
739 size_t miblen = sizeof(mib);
740
741 if (name == NULL)
742 return 1;
743
744 num = strtonum(name, 0, 255, &errstr);
745 if (errstr != NULL)
746 return 0;
747
748 /* Check whether the table actually exists */
749 memset(mib, 0, sizeof(mib));
750 mib[0] = CTL_NET;
751 mib[1] = PF_ROUTE;
752 mib[4] = NET_RT_TABLE;
753 mib[5] = (int)num;
754 if (sysctl(mib, 6, &info, &miblen, NULL, 0) == -1)
755 return 0;
756
757 return 1;
Damien Miller34132e52000-01-14 15:45:46 +1100758}
759
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000760/*
761 * Queue a ListenAddress to be processed once we have all of the Ports
762 * and AddressFamily options.
763 */
764static void
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000765queue_listen_addr(ServerOptions *options, const char *addr,
766 const char *rdomain, int port)
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000767{
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000768 struct queued_listenaddr *qla;
769
770 options->queued_listen_addrs = xrecallocarray(
771 options->queued_listen_addrs,
772 options->num_queued_listens, options->num_queued_listens + 1,
773 sizeof(*options->queued_listen_addrs));
774 qla = &options->queued_listen_addrs[options->num_queued_listens++];
775 qla->addr = xstrdup(addr);
776 qla->port = port;
777 qla->rdomain = rdomain == NULL ? NULL : xstrdup(rdomain);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000778}
779
780/*
781 * Process queued (text) ListenAddress entries.
782 */
783static void
784process_queued_listen_addrs(ServerOptions *options)
785{
786 u_int i;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000787 struct queued_listenaddr *qla;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000788
789 if (options->num_ports == 0)
790 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
791 if (options->address_family == -1)
792 options->address_family = AF_UNSPEC;
793
794 for (i = 0; i < options->num_queued_listens; i++) {
djm@openbsd.orgacf559e2017-10-25 00:15:35 +0000795 qla = &options->queued_listen_addrs[i];
796 add_listen_addr(options, qla->addr, qla->rdomain, qla->port);
797 free(qla->addr);
798 free(qla->rdomain);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000799 }
800 free(options->queued_listen_addrs);
801 options->queued_listen_addrs = NULL;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000802 options->num_queued_listens = 0;
803}
804
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000805/*
806 * Inform channels layer of permitopen options from configuration.
807 */
808void
809process_permitopen(struct ssh *ssh, ServerOptions *options)
810{
811 u_int i;
812 int port;
813 char *host, *arg, *oarg;
814
815 channel_clear_adm_permitted_opens(ssh);
816 if (options->num_permitted_opens == 0)
817 return; /* permit any */
818
819 /* handle keywords: "any" / "none" */
820 if (options->num_permitted_opens == 1 &&
821 strcmp(options->permitted_opens[0], "any") == 0)
822 return;
823 if (options->num_permitted_opens == 1 &&
824 strcmp(options->permitted_opens[0], "none") == 0) {
825 channel_disable_adm_local_opens(ssh);
826 return;
827 }
828 /* Otherwise treat it as a list of permitted host:port */
829 for (i = 0; i < options->num_permitted_opens; i++) {
830 oarg = arg = xstrdup(options->permitted_opens[i]);
831 host = hpdelim(&arg);
832 if (host == NULL)
833 fatal("%s: missing host in PermitOpen", __func__);
834 host = cleanhostname(host);
835 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
836 fatal("%s: bad port number in PermitOpen", __func__);
837 /* Send it to channels layer */
838 channel_add_adm_permitted_opens(ssh, host, port);
839 free(oarg);
840 }
841}
842
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000843struct connection_info *
844get_connection_info(int populate, int use_dns)
845{
djm@openbsd.org95767262016-03-07 19:02:43 +0000846 struct ssh *ssh = active_state; /* XXX */
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000847 static struct connection_info ci;
848
849 if (!populate)
850 return &ci;
djm@openbsd.org95767262016-03-07 19:02:43 +0000851 ci.host = auth_get_canonical_hostname(ssh, use_dns);
852 ci.address = ssh_remote_ipaddr(ssh);
853 ci.laddress = ssh_local_ipaddr(ssh);
854 ci.lport = ssh_local_port(ssh);
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000855 return &ci;
856}
857
Darren Tucker45150472006-07-12 22:34:17 +1000858/*
859 * The strategy for the Match blocks is that the config file is parsed twice.
860 *
861 * The first time is at startup. activep is initialized to 1 and the
862 * directives in the global context are processed and acted on. Hitting a
863 * Match directive unsets activep and the directives inside the block are
864 * checked for syntax only.
865 *
866 * The second time is after a connection has been established but before
867 * authentication. activep is initialized to 2 and global config directives
868 * are ignored since they have already been processed. If the criteria in a
869 * Match block is met, activep is set and the subsequent directives
870 * processed and actioned until EOF or another Match block unsets it. Any
871 * options set are copied into the main server config.
872 *
873 * Potential additions/improvements:
djm@openbsd.orgae363d72016-08-25 23:57:54 +0000874 * - Add Match support for pre-kex directives, eg. Ciphers.
Darren Tucker45150472006-07-12 22:34:17 +1000875 *
876 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
877 * Match Address 192.168.0.*
878 * Tag trusted
879 * Match Group wheel
880 * Tag trusted
881 * Match Tag trusted
882 * AllowTcpForwarding yes
883 * GatewayPorts clientspecified
884 * [...]
885 *
886 * - Add a PermittedChannelRequests directive
887 * Match Group shell
888 * PermittedChannelRequests session,forwarded-tcpip
889 */
890
891static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000892match_cfg_line_group(const char *grps, int line, const char *user)
893{
894 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000895 struct passwd *pw;
896
Damien Miller565ca3f2006-08-19 00:23:15 +1000897 if (user == NULL)
898 goto out;
899
900 if ((pw = getpwnam(user)) == NULL) {
901 debug("Can't match group at line %d because user %.100s does "
902 "not exist", line, user);
903 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
904 debug("Can't Match group because user %.100s not in any group "
905 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000906 } else if (ga_match_pattern_list(grps) != 1) {
907 debug("user %.100s does not match group list %.100s at line %d",
908 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000909 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000910 debug("user %.100s matched group list %.100s at line %d", user,
911 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000912 result = 1;
913 }
914out:
915 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000916 return result;
917}
918
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000919/*
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000920 * All of the attributes on a single Match line are ANDed together, so we need
Damien Miller03bf2e62013-10-24 21:01:26 +1100921 * to check every attribute and set the result to zero if any attribute does
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000922 * not match.
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000923 */
Damien Miller565ca3f2006-08-19 00:23:15 +1000924static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000925match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +1000926{
Damien Millercf31f382013-10-24 21:02:56 +1100927 int result = 1, attributes = 0, port;
Darren Tucker45150472006-07-12 22:34:17 +1000928 char *arg, *attrib, *cp = *condition;
Darren Tucker45150472006-07-12 22:34:17 +1000929
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000930 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000931 debug3("checking syntax for 'Match %s'", cp);
932 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000933 debug3("checking match for '%s' user %s host %s addr %s "
934 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
935 ci->host ? ci->host : "(null)",
936 ci->address ? ci->address : "(null)",
937 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +1000938
939 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100940 attributes++;
941 if (strcasecmp(attrib, "all") == 0) {
942 if (attributes != 1 ||
943 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
944 error("'all' cannot be combined with other "
945 "Match attributes");
946 return -1;
947 }
948 *condition = cp;
949 return 1;
950 }
Darren Tucker45150472006-07-12 22:34:17 +1000951 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
952 error("Missing Match criteria for %s", attrib);
953 return -1;
954 }
Darren Tucker45150472006-07-12 22:34:17 +1000955 if (strcasecmp(attrib, "user") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000956 if (ci == NULL || ci->user == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000957 result = 0;
958 continue;
959 }
djm@openbsd.orge661a862015-05-04 06:10:48 +0000960 if (match_pattern_list(ci->user, arg, 0) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000961 result = 0;
962 else
963 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000964 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000965 } else if (strcasecmp(attrib, "group") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000966 if (ci == NULL || ci->user == NULL) {
967 result = 0;
968 continue;
969 }
970 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +1000971 case -1:
972 return -1;
973 case 0:
974 result = 0;
975 }
Darren Tucker45150472006-07-12 22:34:17 +1000976 } else if (strcasecmp(attrib, "host") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000977 if (ci == NULL || ci->host == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000978 result = 0;
979 continue;
980 }
djm@openbsd.orge661a862015-05-04 06:10:48 +0000981 if (match_hostname(ci->host, arg) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000982 result = 0;
983 else
984 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000985 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +1000986 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000987 if (ci == NULL || ci->address == NULL) {
988 result = 0;
989 continue;
990 }
991 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000992 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000993 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000994 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000995 break;
996 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000997 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000998 result = 0;
999 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +10001000 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +10001001 return -1;
1002 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001003 } else if (strcasecmp(attrib, "localaddress") == 0){
1004 if (ci == NULL || ci->laddress == NULL) {
1005 result = 0;
1006 continue;
1007 }
1008 switch (addr_match_list(ci->laddress, arg)) {
1009 case 1:
1010 debug("connection from %.100s matched "
1011 "'LocalAddress %.100s' at line %d",
1012 ci->laddress, arg, line);
1013 break;
1014 case 0:
1015 case -1:
1016 result = 0;
1017 break;
1018 case -2:
1019 return -1;
1020 }
1021 } else if (strcasecmp(attrib, "localport") == 0) {
1022 if ((port = a2port(arg)) == -1) {
1023 error("Invalid LocalPort '%s' on Match line",
1024 arg);
1025 return -1;
1026 }
1027 if (ci == NULL || ci->lport == 0) {
1028 result = 0;
1029 continue;
1030 }
1031 /* TODO support port lists */
1032 if (port == ci->lport)
1033 debug("connection from %.100s matched "
1034 "'LocalPort %d' at line %d",
1035 ci->laddress, port, line);
1036 else
1037 result = 0;
Darren Tucker45150472006-07-12 22:34:17 +10001038 } else {
1039 error("Unsupported Match attribute %s", attrib);
1040 return -1;
1041 }
1042 }
Damien Millercf31f382013-10-24 21:02:56 +11001043 if (attributes == 0) {
1044 error("One or more attributes required for Match");
1045 return -1;
1046 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001047 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +10001048 debug3("match %sfound", result ? "" : "not ");
1049 *condition = cp;
1050 return result;
1051}
1052
Damien Millere2754432006-07-24 14:06:47 +10001053#define WHITESPACE " \t\r\n"
1054
Damien Miller33322122011-06-20 14:43:11 +10001055/* Multistate option parsing */
1056struct multistate {
1057 char *key;
1058 int value;
1059};
1060static const struct multistate multistate_addressfamily[] = {
1061 { "inet", AF_INET },
1062 { "inet6", AF_INET6 },
1063 { "any", AF_UNSPEC },
1064 { NULL, -1 }
1065};
1066static const struct multistate multistate_permitrootlogin[] = {
1067 { "without-password", PERMIT_NO_PASSWD },
deraadt@openbsd.org1dc8d932015-08-06 14:53:21 +00001068 { "prohibit-password", PERMIT_NO_PASSWD },
Damien Miller33322122011-06-20 14:43:11 +10001069 { "forced-commands-only", PERMIT_FORCED_ONLY },
1070 { "yes", PERMIT_YES },
1071 { "no", PERMIT_NO },
1072 { NULL, -1 }
1073};
1074static const struct multistate multistate_compression[] = {
djm@openbsd.org0082fba2016-09-28 16:33:06 +00001075 { "yes", COMP_DELAYED },
djm@openbsd.org4577ade2016-09-28 20:32:42 +00001076 { "delayed", COMP_DELAYED },
Damien Miller33322122011-06-20 14:43:11 +10001077 { "no", COMP_NONE },
1078 { NULL, -1 }
1079};
1080static const struct multistate multistate_gatewayports[] = {
1081 { "clientspecified", 2 },
1082 { "yes", 1 },
1083 { "no", 0 },
1084 { NULL, -1 }
1085};
Damien Milleraa5b3f82012-12-03 09:50:54 +11001086static const struct multistate multistate_tcpfwd[] = {
1087 { "yes", FORWARD_ALLOW },
1088 { "all", FORWARD_ALLOW },
1089 { "no", FORWARD_DENY },
1090 { "remote", FORWARD_REMOTE },
1091 { "local", FORWARD_LOCAL },
1092 { NULL, -1 }
1093};
Damien Miller33322122011-06-20 14:43:11 +10001094
Ben Lindstromade03f62001-12-06 18:22:17 +00001095int
1096process_server_config_line(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001097 const char *filename, int linenum, int *activep,
1098 struct connection_info *connectinfo)
Ben Lindstromade03f62001-12-06 18:22:17 +00001099{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001100 char *cp, **charptr, *arg, *arg2, *p;
Darren Tucker9113d0c2013-05-16 20:48:14 +10001101 int cmdline = 0, *intptr, value, value2, n, port;
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001102 SyslogFacility *log_facility_ptr;
1103 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +00001104 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +10001105 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +10001106 size_t len;
Darren Tucker9113d0c2013-05-16 20:48:14 +10001107 long long val64;
Damien Miller33322122011-06-20 14:43:11 +10001108 const struct multistate *multistate_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +00001109
djm@openbsd.orgc924b2e2017-02-03 05:05:56 +00001110 /* Strip trailing whitespace. Allow \f (form feed) at EOL only */
1111 if ((len = strlen(line)) == 0)
1112 return 0;
1113 for (len--; len > 0; len--) {
1114 if (strchr(WHITESPACE "\f", line[len]) == NULL)
1115 break;
1116 line[len] = '\0';
1117 }
1118
Ben Lindstromade03f62001-12-06 18:22:17 +00001119 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +11001120 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +11001121 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +00001122 /* Ignore leading whitespace */
1123 if (*arg == '\0')
1124 arg = strdelim(&cp);
1125 if (!arg || !*arg || *arg == '#')
1126 return 0;
1127 intptr = NULL;
1128 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +10001129 opcode = parse_token(arg, filename, linenum, &flags);
1130
1131 if (activep == NULL) { /* We are processing a command line directive */
1132 cmdline = 1;
1133 activep = &cmdline;
1134 }
1135 if (*activep && opcode != sMatch)
1136 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
1137 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001138 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +10001139 fatal("%s line %d: Directive '%s' is not allowed "
1140 "within a Match block", filename, linenum, arg);
1141 } else { /* this is a directive we have already processed */
1142 while (arg)
1143 arg = strdelim(&cp);
1144 return 0;
1145 }
1146 }
1147
Ben Lindstromade03f62001-12-06 18:22:17 +00001148 switch (opcode) {
1149 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +10001150 case sUsePAM:
1151 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +00001152 goto parse_flag;
1153
1154 /* Standard Options */
1155 case sBadOption:
1156 return -1;
1157 case sPort:
1158 /* ignore ports from configfile if cmdline specifies ports */
1159 if (options->ports_from_cmdline)
1160 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +00001161 if (options->num_ports >= MAX_PORTS)
1162 fatal("%s line %d: too many ports.",
1163 filename, linenum);
1164 arg = strdelim(&cp);
1165 if (!arg || *arg == '\0')
1166 fatal("%s line %d: missing port number.",
1167 filename, linenum);
1168 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +11001169 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +00001170 fatal("%s line %d: Badly formatted port number.",
1171 filename, linenum);
1172 break;
1173
Ben Lindstromade03f62001-12-06 18:22:17 +00001174 case sLoginGraceTime:
1175 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +10001176 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +00001177 arg = strdelim(&cp);
1178 if (!arg || *arg == '\0')
1179 fatal("%s line %d: missing time value.",
1180 filename, linenum);
1181 if ((value = convtime(arg)) == -1)
1182 fatal("%s line %d: invalid time value.",
1183 filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001184 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001185 *intptr = value;
1186 break;
1187
Ben Lindstromade03f62001-12-06 18:22:17 +00001188 case sListenAddress:
1189 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001190 if (arg == NULL || *arg == '\0')
1191 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +00001192 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +10001193 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
1194 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
1195 && strchr(p+1, ':') != NULL) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001196 port = 0;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001197 p = arg;
1198 } else {
1199 p = hpdelim(&arg);
1200 if (p == NULL)
1201 fatal("%s line %d: bad address:port usage",
1202 filename, linenum);
1203 p = cleanhostname(p);
1204 if (arg == NULL)
1205 port = 0;
1206 else if ((port = a2port(arg)) <= 0)
1207 fatal("%s line %d: bad port number",
1208 filename, linenum);
1209 }
1210 /* Optional routing table */
1211 arg2 = NULL;
1212 if ((arg = strdelim(&cp)) != NULL) {
1213 if (strcmp(arg, "rdomain") != 0 ||
1214 (arg2 = strdelim(&cp)) == NULL)
1215 fatal("%s line %d: bad ListenAddress syntax",
1216 filename, linenum);
1217 if (!valid_rdomain(arg2))
1218 fatal("%s line %d: bad routing domain",
1219 filename, linenum);
1220 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001221
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00001222 queue_listen_addr(options, p, arg2, port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001223
Ben Lindstromade03f62001-12-06 18:22:17 +00001224 break;
1225
Darren Tucker0f383232005-01-20 10:57:56 +11001226 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +10001227 intptr = &options->address_family;
1228 multistate_ptr = multistate_addressfamily;
Damien Miller33322122011-06-20 14:43:11 +10001229 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +11001230 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +10001231 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +10001232 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +10001233 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +10001234 value = -1;
1235 for (i = 0; multistate_ptr[i].key != NULL; i++) {
1236 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
1237 value = multistate_ptr[i].value;
1238 break;
1239 }
1240 }
1241 if (value == -1)
1242 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +11001243 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +10001244 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +11001245 *intptr = value;
1246 break;
1247
Ben Lindstromade03f62001-12-06 18:22:17 +00001248 case sHostKeyFile:
Ben Lindstromade03f62001-12-06 18:22:17 +00001249 arg = strdelim(&cp);
1250 if (!arg || *arg == '\0')
1251 fatal("%s line %d: missing file name.",
1252 filename, linenum);
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001253 if (*activep)
1254 servconf_add_hostkey(filename, linenum, options, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001255 break;
1256
Damien Miller85b45e02013-07-20 13:21:52 +10001257 case sHostKeyAgent:
1258 charptr = &options->host_key_agent;
1259 arg = strdelim(&cp);
1260 if (!arg || *arg == '\0')
1261 fatal("%s line %d: missing socket name.",
1262 filename, linenum);
1263 if (*activep && *charptr == NULL)
1264 *charptr = !strcmp(arg, SSH_AUTHSOCKET_ENV_NAME) ?
1265 xstrdup(arg) : derelativise_path(arg);
1266 break;
1267
Damien Miller0a80ca12010-02-27 07:55:05 +11001268 case sHostCertificate:
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001269 arg = strdelim(&cp);
1270 if (!arg || *arg == '\0')
1271 fatal("%s line %d: missing file name.",
1272 filename, linenum);
1273 if (*activep)
1274 servconf_add_hostcert(filename, linenum, options, arg);
1275 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001276
Ben Lindstromade03f62001-12-06 18:22:17 +00001277 case sPidFile:
1278 charptr = &options->pid_file;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001279 parse_filename:
1280 arg = strdelim(&cp);
1281 if (!arg || *arg == '\0')
1282 fatal("%s line %d: missing file name.",
1283 filename, linenum);
1284 if (*activep && *charptr == NULL) {
1285 *charptr = derelativise_path(arg);
1286 /* increase optional counter */
1287 if (intptr != NULL)
1288 *intptr = *intptr + 1;
1289 }
1290 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001291
1292 case sPermitRootLogin:
1293 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +10001294 multistate_ptr = multistate_permitrootlogin;
1295 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001296
1297 case sIgnoreRhosts:
1298 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +10001299 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +00001300 arg = strdelim(&cp);
1301 if (!arg || *arg == '\0')
1302 fatal("%s line %d: missing yes/no argument.",
1303 filename, linenum);
1304 value = 0; /* silence compiler */
1305 if (strcmp(arg, "yes") == 0)
1306 value = 1;
1307 else if (strcmp(arg, "no") == 0)
1308 value = 0;
1309 else
1310 fatal("%s line %d: Bad yes/no argument: %s",
1311 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +10001312 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001313 *intptr = value;
1314 break;
1315
1316 case sIgnoreUserKnownHosts:
1317 intptr = &options->ignore_user_known_hosts;
1318 goto parse_flag;
1319
Ben Lindstromade03f62001-12-06 18:22:17 +00001320 case sHostbasedAuthentication:
1321 intptr = &options->hostbased_authentication;
1322 goto parse_flag;
1323
1324 case sHostbasedUsesNameFromPacketOnly:
1325 intptr = &options->hostbased_uses_name_from_packet_only;
1326 goto parse_flag;
1327
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001328 case sHostbasedAcceptedKeyTypes:
1329 charptr = &options->hostbased_key_types;
1330 parse_keytypes:
1331 arg = strdelim(&cp);
1332 if (!arg || *arg == '\0')
1333 fatal("%s line %d: Missing argument.",
1334 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001335 if (*arg != '-' &&
1336 !sshkey_names_valid2(*arg == '+' ? arg + 1 : arg, 1))
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001337 fatal("%s line %d: Bad key types '%s'.",
1338 filename, linenum, arg ? arg : "<NONE>");
1339 if (*activep && *charptr == NULL)
1340 *charptr = xstrdup(arg);
1341 break;
1342
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001343 case sHostKeyAlgorithms:
1344 charptr = &options->hostkeyalgorithms;
1345 goto parse_keytypes;
1346
Ben Lindstromade03f62001-12-06 18:22:17 +00001347 case sPubkeyAuthentication:
1348 intptr = &options->pubkey_authentication;
1349 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001350
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001351 case sPubkeyAcceptedKeyTypes:
1352 charptr = &options->pubkey_key_types;
1353 goto parse_keytypes;
1354
Ben Lindstromade03f62001-12-06 18:22:17 +00001355 case sKerberosAuthentication:
1356 intptr = &options->kerberos_authentication;
1357 goto parse_flag;
1358
1359 case sKerberosOrLocalPasswd:
1360 intptr = &options->kerberos_or_local_passwd;
1361 goto parse_flag;
1362
1363 case sKerberosTicketCleanup:
1364 intptr = &options->kerberos_ticket_cleanup;
1365 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001366
Darren Tucker22ef5082003-12-31 11:37:34 +11001367 case sKerberosGetAFSToken:
1368 intptr = &options->kerberos_get_afs_token;
1369 goto parse_flag;
1370
Darren Tucker0efd1552003-08-26 11:49:55 +10001371 case sGssAuthentication:
1372 intptr = &options->gss_authentication;
1373 goto parse_flag;
1374
1375 case sGssCleanupCreds:
1376 intptr = &options->gss_cleanup_creds;
1377 goto parse_flag;
1378
djm@openbsd.orgd7c31da2015-05-22 03:50:02 +00001379 case sGssStrictAcceptor:
1380 intptr = &options->gss_strict_acceptor;
1381 goto parse_flag;
1382
Ben Lindstromade03f62001-12-06 18:22:17 +00001383 case sPasswordAuthentication:
1384 intptr = &options->password_authentication;
1385 goto parse_flag;
1386
1387 case sKbdInteractiveAuthentication:
1388 intptr = &options->kbd_interactive_authentication;
1389 goto parse_flag;
1390
1391 case sChallengeResponseAuthentication:
1392 intptr = &options->challenge_response_authentication;
1393 goto parse_flag;
1394
1395 case sPrintMotd:
1396 intptr = &options->print_motd;
1397 goto parse_flag;
1398
1399 case sPrintLastLog:
1400 intptr = &options->print_lastlog;
1401 goto parse_flag;
1402
1403 case sX11Forwarding:
1404 intptr = &options->x11_forwarding;
1405 goto parse_flag;
1406
1407 case sX11DisplayOffset:
1408 intptr = &options->x11_display_offset;
naddy@openbsd.orgc38ea632016-08-15 12:27:56 +00001409 parse_int:
1410 arg = strdelim(&cp);
1411 if (!arg || *arg == '\0')
1412 fatal("%s line %d: missing integer value.",
1413 filename, linenum);
1414 value = atoi(arg);
1415 if (*activep && *intptr == -1)
1416 *intptr = value;
1417 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001418
Damien Miller95c249f2002-02-05 12:11:34 +11001419 case sX11UseLocalhost:
1420 intptr = &options->x11_use_localhost;
1421 goto parse_flag;
1422
Ben Lindstromade03f62001-12-06 18:22:17 +00001423 case sXAuthLocation:
1424 charptr = &options->xauth_location;
1425 goto parse_filename;
1426
Damien Miller5ff30c62013-10-30 22:21:50 +11001427 case sPermitTTY:
1428 intptr = &options->permit_tty;
1429 goto parse_flag;
1430
Damien Miller72e6b5c2014-07-04 09:00:04 +10001431 case sPermitUserRC:
1432 intptr = &options->permit_user_rc;
1433 goto parse_flag;
1434
Ben Lindstromade03f62001-12-06 18:22:17 +00001435 case sStrictModes:
1436 intptr = &options->strict_modes;
1437 goto parse_flag;
1438
Damien Miller12c150e2003-12-17 16:31:10 +11001439 case sTCPKeepAlive:
1440 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001441 goto parse_flag;
1442
1443 case sEmptyPasswd:
1444 intptr = &options->permit_empty_passwd;
1445 goto parse_flag;
1446
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001447 case sPermitUserEnvironment:
1448 intptr = &options->permit_user_env;
1449 goto parse_flag;
1450
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001451 case sCompression:
1452 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001453 multistate_ptr = multistate_compression;
1454 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001455
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001456 case sRekeyLimit:
1457 arg = strdelim(&cp);
1458 if (!arg || *arg == '\0')
1459 fatal("%.200s line %d: Missing argument.", filename,
1460 linenum);
1461 if (strcmp(arg, "default") == 0) {
1462 val64 = 0;
1463 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001464 if (scan_scaled(arg, &val64) == -1)
1465 fatal("%.200s line %d: Bad number '%s': %s",
1466 filename, linenum, arg, strerror(errno));
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001467 if (val64 != 0 && val64 < 16)
1468 fatal("%.200s line %d: RekeyLimit too small",
1469 filename, linenum);
1470 }
1471 if (*activep && options->rekey_limit == -1)
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00001472 options->rekey_limit = val64;
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001473 if (cp != NULL) { /* optional rekey interval present */
1474 if (strcmp(cp, "none") == 0) {
1475 (void)strdelim(&cp); /* discard */
1476 break;
1477 }
1478 intptr = &options->rekey_interval;
1479 goto parse_time;
1480 }
1481 break;
1482
Ben Lindstromade03f62001-12-06 18:22:17 +00001483 case sGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +10001484 intptr = &options->fwd_opts.gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001485 multistate_ptr = multistate_gatewayports;
1486 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001487
Damien Miller3a961dc2003-06-03 10:25:48 +10001488 case sUseDNS:
1489 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001490 goto parse_flag;
1491
1492 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001493 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001494 arg = strdelim(&cp);
1495 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001496 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001497 fatal("%.200s line %d: unsupported log facility '%s'",
1498 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001499 if (*log_facility_ptr == -1)
1500 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001501 break;
1502
1503 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001504 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001505 arg = strdelim(&cp);
1506 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001507 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001508 fatal("%.200s line %d: unsupported log level '%s'",
1509 filename, linenum, arg ? arg : "<NONE>");
djm@openbsd.org54cd41a2017-05-17 01:24:17 +00001510 if (*activep && *log_level_ptr == -1)
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001511 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001512 break;
1513
1514 case sAllowTcpForwarding:
1515 intptr = &options->allow_tcp_forwarding;
Damien Milleraa5b3f82012-12-03 09:50:54 +11001516 multistate_ptr = multistate_tcpfwd;
1517 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001518
Damien Miller7acefbb2014-07-18 14:11:24 +10001519 case sAllowStreamLocalForwarding:
1520 intptr = &options->allow_streamlocal_forwarding;
1521 multistate_ptr = multistate_tcpfwd;
1522 goto parse_multistate;
1523
Damien Miller4f755cd2008-05-19 14:57:41 +10001524 case sAllowAgentForwarding:
1525 intptr = &options->allow_agent_forwarding;
1526 goto parse_flag;
1527
djm@openbsd.org7844f352016-11-30 03:00:05 +00001528 case sDisableForwarding:
1529 intptr = &options->disable_forwarding;
1530 goto parse_flag;
1531
Ben Lindstromade03f62001-12-06 18:22:17 +00001532 case sAllowUsers:
1533 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.org010359b2016-11-06 05:46:37 +00001534 if (match_user(NULL, NULL, NULL, arg) == -1)
1535 fatal("%s line %d: invalid AllowUsers pattern: "
1536 "\"%.100s\"", filename, linenum, arg);
Damien Millerc24da772012-06-20 21:53:58 +10001537 if (!*activep)
1538 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001539 array_append(filename, linenum, "AllowUsers",
1540 &options->allow_users, &options->num_allow_users,
1541 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001542 }
1543 break;
1544
1545 case sDenyUsers:
1546 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.org010359b2016-11-06 05:46:37 +00001547 if (match_user(NULL, NULL, NULL, arg) == -1)
1548 fatal("%s line %d: invalid DenyUsers pattern: "
1549 "\"%.100s\"", filename, linenum, arg);
Damien Millerc24da772012-06-20 21:53:58 +10001550 if (!*activep)
1551 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001552 array_append(filename, linenum, "DenyUsers",
1553 &options->deny_users, &options->num_deny_users,
1554 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001555 }
1556 break;
1557
1558 case sAllowGroups:
1559 while ((arg = strdelim(&cp)) && *arg != '\0') {
Damien Millerc24da772012-06-20 21:53:58 +10001560 if (!*activep)
1561 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001562 array_append(filename, linenum, "AllowGroups",
1563 &options->allow_groups, &options->num_allow_groups,
1564 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001565 }
1566 break;
1567
1568 case sDenyGroups:
1569 while ((arg = strdelim(&cp)) && *arg != '\0') {
Damien Millerc24da772012-06-20 21:53:58 +10001570 if (!*activep)
1571 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001572 array_append(filename, linenum, "DenyGroups",
1573 &options->deny_groups, &options->num_deny_groups,
1574 arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001575 }
1576 break;
1577
1578 case sCiphers:
1579 arg = strdelim(&cp);
1580 if (!arg || *arg == '\0')
1581 fatal("%s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001582 if (*arg != '-' && !ciphers_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstromade03f62001-12-06 18:22:17 +00001583 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1584 filename, linenum, arg ? arg : "<NONE>");
1585 if (options->ciphers == NULL)
1586 options->ciphers = xstrdup(arg);
1587 break;
1588
1589 case sMacs:
1590 arg = strdelim(&cp);
1591 if (!arg || *arg == '\0')
1592 fatal("%s line %d: Missing argument.", filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001593 if (*arg != '-' && !mac_valid(*arg == '+' ? arg + 1 : arg))
Ben Lindstromade03f62001-12-06 18:22:17 +00001594 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1595 filename, linenum, arg ? arg : "<NONE>");
1596 if (options->macs == NULL)
1597 options->macs = xstrdup(arg);
1598 break;
1599
Damien Millerd5f62bf2010-09-24 22:11:14 +10001600 case sKexAlgorithms:
1601 arg = strdelim(&cp);
1602 if (!arg || *arg == '\0')
1603 fatal("%s line %d: Missing argument.",
1604 filename, linenum);
djm@openbsd.org68bc8cf2017-02-03 23:01:19 +00001605 if (*arg != '-' &&
1606 !kex_names_valid(*arg == '+' ? arg + 1 : arg))
Damien Millerd5f62bf2010-09-24 22:11:14 +10001607 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1608 filename, linenum, arg ? arg : "<NONE>");
1609 if (options->kex_algorithms == NULL)
1610 options->kex_algorithms = xstrdup(arg);
1611 break;
1612
Ben Lindstromade03f62001-12-06 18:22:17 +00001613 case sSubsystem:
1614 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1615 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001616 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001617 }
1618 arg = strdelim(&cp);
1619 if (!arg || *arg == '\0')
1620 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001621 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001622 if (!*activep) {
1623 arg = strdelim(&cp);
1624 break;
1625 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001626 for (i = 0; i < options->num_subsystems; i++)
1627 if (strcmp(arg, options->subsystem_name[i]) == 0)
1628 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001629 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001630 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1631 arg = strdelim(&cp);
1632 if (!arg || *arg == '\0')
1633 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001634 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001635 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001636
1637 /* Collect arguments (separate to executable) */
1638 p = xstrdup(arg);
1639 len = strlen(p) + 1;
1640 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1641 len += 1 + strlen(arg);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001642 p = xreallocarray(p, 1, len);
Damien Miller917f9b62006-07-10 20:36:47 +10001643 strlcat(p, " ", len);
1644 strlcat(p, arg, len);
1645 }
1646 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001647 options->num_subsystems++;
1648 break;
1649
1650 case sMaxStartups:
1651 arg = strdelim(&cp);
1652 if (!arg || *arg == '\0')
1653 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001654 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001655 if ((n = sscanf(arg, "%d:%d:%d",
1656 &options->max_startups_begin,
1657 &options->max_startups_rate,
1658 &options->max_startups)) == 3) {
1659 if (options->max_startups_begin >
1660 options->max_startups ||
1661 options->max_startups_rate > 100 ||
1662 options->max_startups_rate < 1)
1663 fatal("%s line %d: Illegal MaxStartups spec.",
1664 filename, linenum);
1665 } else if (n != 1)
1666 fatal("%s line %d: Illegal MaxStartups spec.",
1667 filename, linenum);
1668 else
1669 options->max_startups = options->max_startups_begin;
1670 break;
1671
Darren Tucker89413db2004-05-24 10:36:23 +10001672 case sMaxAuthTries:
1673 intptr = &options->max_authtries;
1674 goto parse_int;
1675
Damien Miller7207f642008-05-19 15:34:50 +10001676 case sMaxSessions:
1677 intptr = &options->max_sessions;
1678 goto parse_int;
1679
Ben Lindstromade03f62001-12-06 18:22:17 +00001680 case sBanner:
1681 charptr = &options->banner;
1682 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001683
Ben Lindstromade03f62001-12-06 18:22:17 +00001684 /*
1685 * These options can contain %X options expanded at
1686 * connect time, so that you can specify paths like:
1687 *
1688 * AuthorizedKeysFile /etc/ssh_keys/%u
1689 */
1690 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001691 if (*activep && options->num_authkeys_files == 0) {
1692 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001693 arg = tilde_expand_filename(arg, getuid());
1694 array_append(filename, linenum,
1695 "AuthorizedKeysFile",
1696 &options->authorized_keys_files,
1697 &options->num_authkeys_files, arg);
1698 free(arg);
Damien Millerd8478b62011-05-29 21:39:36 +10001699 }
1700 }
1701 return 0;
1702
Damien Miller30da3442010-05-10 11:58:03 +10001703 case sAuthorizedPrincipalsFile:
1704 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001705 arg = strdelim(&cp);
1706 if (!arg || *arg == '\0')
1707 fatal("%s line %d: missing file name.",
1708 filename, linenum);
1709 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001710 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001711 /* increase optional counter */
1712 if (intptr != NULL)
1713 *intptr = *intptr + 1;
1714 }
1715 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001716
1717 case sClientAliveInterval:
1718 intptr = &options->client_alive_interval;
1719 goto parse_time;
1720
1721 case sClientAliveCountMax:
1722 intptr = &options->client_alive_count_max;
1723 goto parse_int;
1724
Darren Tucker46bc0752004-05-02 22:11:30 +10001725 case sAcceptEnv:
1726 while ((arg = strdelim(&cp)) && *arg != '\0') {
1727 if (strchr(arg, '=') != NULL)
1728 fatal("%s line %d: Invalid environment name.",
1729 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001730 if (!*activep)
Damien Millerc24da772012-06-20 21:53:58 +10001731 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001732 array_append(filename, linenum, "AcceptEnv",
1733 &options->accept_env, &options->num_accept_env,
1734 arg);
Darren Tucker46bc0752004-05-02 22:11:30 +10001735 }
1736 break;
1737
Damien Millerd27b9472005-12-13 19:29:02 +11001738 case sPermitTunnel:
1739 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001740 arg = strdelim(&cp);
1741 if (!arg || *arg == '\0')
1742 fatal("%s line %d: Missing yes/point-to-point/"
1743 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001744 value = -1;
1745 for (i = 0; tunmode_desc[i].val != -1; i++)
1746 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1747 value = tunmode_desc[i].val;
1748 break;
1749 }
1750 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001751 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1752 "no argument: %s", filename, linenum, arg);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001753 if (*activep && *intptr == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001754 *intptr = value;
1755 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001756
Darren Tucker45150472006-07-12 22:34:17 +10001757 case sMatch:
1758 if (cmdline)
1759 fatal("Match directive not supported as a command-line "
1760 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001761 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10001762 if (value < 0)
1763 fatal("%s line %d: Bad Match condition", filename,
1764 linenum);
1765 *activep = value;
1766 break;
1767
Damien Miller9b439df2006-07-24 14:04:00 +10001768 case sPermitOpen:
1769 arg = strdelim(&cp);
1770 if (!arg || *arg == '\0')
1771 fatal("%s line %d: missing PermitOpen specification",
1772 filename, linenum);
djm@openbsd.org69bda022017-10-04 18:49:30 +00001773 value = options->num_permitted_opens; /* modified later */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001774 if (strcmp(arg, "any") == 0 || strcmp(arg, "none") == 0) {
djm@openbsd.org69bda022017-10-04 18:49:30 +00001775 if (*activep && value == 0) {
Damien Millerc6081482012-04-22 11:18:53 +10001776 options->num_permitted_opens = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001777 options->permitted_opens = xcalloc(1,
1778 sizeof(*options->permitted_opens));
1779 options->permitted_opens[0] = xstrdup(arg);
dtucker@openbsd.org30484e52017-09-18 09:41:52 +00001780 }
Damien Millerc6081482012-04-22 11:18:53 +10001781 break;
1782 }
Damien Millera765cf42006-07-24 14:08:13 +10001783 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001784 arg2 = xstrdup(arg);
Damien Millera765cf42006-07-24 14:08:13 +10001785 p = hpdelim(&arg);
1786 if (p == NULL)
1787 fatal("%s line %d: missing host in PermitOpen",
1788 filename, linenum);
1789 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001790 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001791 fatal("%s line %d: bad port number in "
1792 "PermitOpen", filename, linenum);
djm@openbsd.org69bda022017-10-04 18:49:30 +00001793 if (*activep && value == 0) {
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001794 array_append(filename, linenum,
1795 "PermitOpen",
1796 &options->permitted_opens,
1797 &options->num_permitted_opens, arg2);
1798 }
1799 free(arg2);
Damien Millera765cf42006-07-24 14:08:13 +10001800 }
Damien Miller9b439df2006-07-24 14:04:00 +10001801 break;
1802
Damien Millere2754432006-07-24 14:06:47 +10001803 case sForceCommand:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001804 if (cp == NULL || *cp == '\0')
Damien Millere2754432006-07-24 14:06:47 +10001805 fatal("%.200s line %d: Missing argument.", filename,
1806 linenum);
1807 len = strspn(cp, WHITESPACE);
1808 if (*activep && options->adm_forced_command == NULL)
1809 options->adm_forced_command = xstrdup(cp + len);
1810 return 0;
1811
Damien Millerd8cb1f12008-02-10 22:40:12 +11001812 case sChrootDirectory:
1813 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001814
1815 arg = strdelim(&cp);
1816 if (!arg || *arg == '\0')
1817 fatal("%s line %d: missing file name.",
1818 filename, linenum);
1819 if (*activep && *charptr == NULL)
1820 *charptr = xstrdup(arg);
1821 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001822
Damien Miller1aed65e2010-03-04 21:53:35 +11001823 case sTrustedUserCAKeys:
1824 charptr = &options->trusted_user_ca_keys;
1825 goto parse_filename;
1826
1827 case sRevokedKeys:
1828 charptr = &options->revoked_keys_file;
1829 goto parse_filename;
1830
Damien Miller0dac6fb2010-11-20 15:19:38 +11001831 case sIPQoS:
1832 arg = strdelim(&cp);
1833 if ((value = parse_ipqos(arg)) == -1)
1834 fatal("%s line %d: Bad IPQoS value: %s",
1835 filename, linenum, arg);
1836 arg = strdelim(&cp);
1837 if (arg == NULL)
1838 value2 = value;
1839 else if ((value2 = parse_ipqos(arg)) == -1)
1840 fatal("%s line %d: Bad IPQoS value: %s",
1841 filename, linenum, arg);
1842 if (*activep) {
1843 options->ip_qos_interactive = value;
1844 options->ip_qos_bulk = value2;
1845 }
1846 break;
1847
Damien Miller23528812012-04-22 11:24:43 +10001848 case sVersionAddendum:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001849 if (cp == NULL || *cp == '\0')
Damien Miller23528812012-04-22 11:24:43 +10001850 fatal("%.200s line %d: Missing argument.", filename,
1851 linenum);
1852 len = strspn(cp, WHITESPACE);
1853 if (*activep && options->version_addendum == NULL) {
1854 if (strcasecmp(cp + len, "none") == 0)
1855 options->version_addendum = xstrdup("");
1856 else if (strchr(cp + len, '\r') != NULL)
1857 fatal("%.200s line %d: Invalid argument",
1858 filename, linenum);
1859 else
1860 options->version_addendum = xstrdup(cp + len);
1861 }
1862 return 0;
1863
Damien Miller09d3e122012-10-31 08:58:58 +11001864 case sAuthorizedKeysCommand:
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001865 if (cp == NULL)
1866 fatal("%.200s line %d: Missing argument.", filename,
1867 linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001868 len = strspn(cp, WHITESPACE);
1869 if (*activep && options->authorized_keys_command == NULL) {
1870 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1871 fatal("%.200s line %d: AuthorizedKeysCommand "
1872 "must be an absolute path",
1873 filename, linenum);
1874 options->authorized_keys_command = xstrdup(cp + len);
1875 }
1876 return 0;
1877
1878 case sAuthorizedKeysCommandUser:
1879 charptr = &options->authorized_keys_command_user;
1880
1881 arg = strdelim(&cp);
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001882 if (!arg || *arg == '\0')
1883 fatal("%s line %d: missing AuthorizedKeysCommandUser "
1884 "argument.", filename, linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001885 if (*activep && *charptr == NULL)
1886 *charptr = xstrdup(arg);
1887 break;
1888
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +00001889 case sAuthorizedPrincipalsCommand:
1890 if (cp == NULL)
1891 fatal("%.200s line %d: Missing argument.", filename,
1892 linenum);
1893 len = strspn(cp, WHITESPACE);
1894 if (*activep &&
1895 options->authorized_principals_command == NULL) {
1896 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1897 fatal("%.200s line %d: "
1898 "AuthorizedPrincipalsCommand must be "
1899 "an absolute path", filename, linenum);
1900 options->authorized_principals_command =
1901 xstrdup(cp + len);
1902 }
1903 return 0;
1904
1905 case sAuthorizedPrincipalsCommandUser:
1906 charptr = &options->authorized_principals_command_user;
1907
1908 arg = strdelim(&cp);
1909 if (!arg || *arg == '\0')
1910 fatal("%s line %d: missing "
1911 "AuthorizedPrincipalsCommandUser argument.",
1912 filename, linenum);
1913 if (*activep && *charptr == NULL)
1914 *charptr = xstrdup(arg);
1915 break;
1916
Damien Millera6e3f012012-11-04 23:21:40 +11001917 case sAuthenticationMethods:
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001918 if (options->num_auth_methods == 0) {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001919 value = 0; /* seen "any" pseudo-method */
djm@openbsd.org46ecd192016-06-23 05:17:51 +00001920 value2 = 0; /* sucessfully parsed any method */
Damien Millera6e3f012012-11-04 23:21:40 +11001921 while ((arg = strdelim(&cp)) && *arg != '\0') {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001922 if (strcmp(arg, "any") == 0) {
1923 if (options->num_auth_methods > 0) {
1924 fatal("%s line %d: \"any\" "
1925 "must appear alone in "
1926 "AuthenticationMethods",
1927 filename, linenum);
1928 }
1929 value = 1;
1930 } else if (value) {
1931 fatal("%s line %d: \"any\" must appear "
1932 "alone in AuthenticationMethods",
1933 filename, linenum);
1934 } else if (auth2_methods_valid(arg, 0) != 0) {
Damien Millera6e3f012012-11-04 23:21:40 +11001935 fatal("%s line %d: invalid "
1936 "authentication method list.",
1937 filename, linenum);
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001938 }
djm@openbsd.org46ecd192016-06-23 05:17:51 +00001939 value2 = 1;
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001940 if (!*activep)
1941 continue;
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00001942 array_append(filename, linenum,
1943 "AuthenticationMethods",
1944 &options->auth_methods,
1945 &options->num_auth_methods, arg);
Damien Millera6e3f012012-11-04 23:21:40 +11001946 }
djm@openbsd.org46ecd192016-06-23 05:17:51 +00001947 if (value2 == 0) {
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00001948 fatal("%s line %d: no AuthenticationMethods "
1949 "specified", filename, linenum);
1950 }
Damien Millera6e3f012012-11-04 23:21:40 +11001951 }
1952 return 0;
1953
Damien Miller7acefbb2014-07-18 14:11:24 +10001954 case sStreamLocalBindMask:
1955 arg = strdelim(&cp);
1956 if (!arg || *arg == '\0')
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001957 fatal("%s line %d: missing StreamLocalBindMask "
1958 "argument.", filename, linenum);
Damien Miller7acefbb2014-07-18 14:11:24 +10001959 /* Parse mode in octal format */
1960 value = strtol(arg, &p, 8);
1961 if (arg == p || value < 0 || value > 0777)
1962 fatal("%s line %d: Bad mask.", filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001963 if (*activep)
1964 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
Damien Miller7acefbb2014-07-18 14:11:24 +10001965 break;
1966
1967 case sStreamLocalBindUnlink:
1968 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1969 goto parse_flag;
1970
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001971 case sFingerprintHash:
1972 arg = strdelim(&cp);
1973 if (!arg || *arg == '\0')
1974 fatal("%.200s line %d: Missing argument.",
1975 filename, linenum);
1976 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1977 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1978 filename, linenum, arg);
1979 if (*activep)
1980 options->fingerprint_hash = value;
1981 break;
1982
djm@openbsd.org8f574952017-06-24 06:34:38 +00001983 case sExposeAuthInfo:
1984 intptr = &options->expose_userauth_info;
1985 goto parse_flag;
1986
Ben Lindstromade03f62001-12-06 18:22:17 +00001987 case sDeprecated:
djm@openbsd.orgae363d72016-08-25 23:57:54 +00001988 case sIgnore:
Damien Millerf9b3feb2003-05-16 11:38:32 +10001989 case sUnsupported:
djm@openbsd.orgae363d72016-08-25 23:57:54 +00001990 do_log2(opcode == sIgnore ?
1991 SYSLOG_LEVEL_DEBUG2 : SYSLOG_LEVEL_INFO,
1992 "%s line %d: %s option %s", filename, linenum,
1993 opcode == sUnsupported ? "Unsupported" : "Deprecated", arg);
Damien Millerf9b3feb2003-05-16 11:38:32 +10001994 while (arg)
1995 arg = strdelim(&cp);
1996 break;
1997
Ben Lindstromade03f62001-12-06 18:22:17 +00001998 default:
1999 fatal("%s line %d: Missing handler for opcode %s (%d)",
2000 filename, linenum, arg, opcode);
2001 }
2002 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
2003 fatal("%s line %d: garbage at end of line; \"%.200s\".",
2004 filename, linenum, arg);
2005 return 0;
2006}
2007
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002008/* Reads the server configuration file. */
2009
Damien Miller4af51302000-04-16 11:18:38 +10002010void
Darren Tucker645ab752004-06-25 13:33:20 +10002011load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002012{
Damien Miller46cb75a2012-07-31 12:22:37 +10002013 char line[4096], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00002014 FILE *f;
Damien Miller46cb75a2012-07-31 12:22:37 +10002015 int lineno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002016
Darren Tucker645ab752004-06-25 13:33:20 +10002017 debug2("%s: filename %s", __func__, filename);
2018 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11002019 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002020 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11002021 }
Darren Tucker645ab752004-06-25 13:33:20 +10002022 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11002023 while (fgets(line, sizeof(line), f)) {
Damien Miller46cb75a2012-07-31 12:22:37 +10002024 lineno++;
2025 if (strlen(line) == sizeof(line) - 1)
2026 fatal("%s line %d too long", filename, lineno);
Darren Tucker645ab752004-06-25 13:33:20 +10002027 /*
2028 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10002029 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10002030 * line numbers later for error messages
2031 */
2032 if ((cp = strchr(line, '#')) != NULL)
2033 memcpy(cp, "\n", 2);
2034 cp = line + strspn(line, " \t\r");
2035
2036 buffer_append(conf, cp, strlen(cp));
2037 }
2038 buffer_append(conf, "\0", 1);
2039 fclose(f);
2040 debug2("%s: done config len = %d", __func__, buffer_len(conf));
2041}
2042
2043void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002044parse_server_match_config(ServerOptions *options,
2045 struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10002046{
Darren Tucker45150472006-07-12 22:34:17 +10002047 ServerOptions mo;
2048
2049 initialize_server_options(&mo);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002050 parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11002051 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10002052}
2053
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002054int parse_server_match_testspec(struct connection_info *ci, char *spec)
2055{
2056 char *p;
2057
2058 while ((p = strsep(&spec, ",")) && *p != '\0') {
2059 if (strncmp(p, "addr=", 5) == 0) {
2060 ci->address = xstrdup(p + 5);
2061 } else if (strncmp(p, "host=", 5) == 0) {
2062 ci->host = xstrdup(p + 5);
2063 } else if (strncmp(p, "user=", 5) == 0) {
2064 ci->user = xstrdup(p + 5);
2065 } else if (strncmp(p, "laddr=", 6) == 0) {
2066 ci->laddress = xstrdup(p + 6);
2067 } else if (strncmp(p, "lport=", 6) == 0) {
2068 ci->lport = a2port(p + 6);
2069 if (ci->lport == -1) {
2070 fprintf(stderr, "Invalid port '%s' in test mode"
2071 " specification %s\n", p+6, p);
2072 return -1;
2073 }
2074 } else {
2075 fprintf(stderr, "Invalid test mode specification %s\n",
2076 p);
2077 return -1;
2078 }
2079 }
2080 return 0;
2081}
2082
2083/*
2084 * returns 1 for a complete spec, 0 for partial spec and -1 for an
2085 * empty spec.
2086 */
2087int server_match_spec_complete(struct connection_info *ci)
2088{
2089 if (ci->user && ci->host && ci->address)
2090 return 1; /* complete */
2091 if (!ci->user && !ci->host && !ci->address)
2092 return -1; /* empty */
2093 return 0; /* partial */
2094}
2095
Darren Tucker1629c072007-02-19 22:25:37 +11002096/*
2097 * Copy any supported values that are set.
2098 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10002099 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11002100 * array values that are not used pre-authentication, because any that we
2101 * do use must be explictly sent in mm_getpwnamallow().
2102 */
Darren Tucker45150472006-07-12 22:34:17 +10002103void
Darren Tucker1629c072007-02-19 22:25:37 +11002104copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10002105{
Damien Miller534b2cc2013-12-05 14:07:27 +11002106#define M_CP_INTOPT(n) do {\
2107 if (src->n != -1) \
2108 dst->n = src->n; \
2109} while (0)
2110
Darren Tucker1629c072007-02-19 22:25:37 +11002111 M_CP_INTOPT(password_authentication);
2112 M_CP_INTOPT(gss_authentication);
Darren Tucker1629c072007-02-19 22:25:37 +11002113 M_CP_INTOPT(pubkey_authentication);
2114 M_CP_INTOPT(kerberos_authentication);
2115 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10002116 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11002117 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11002118 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11002119 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11002120
2121 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002122 M_CP_INTOPT(allow_streamlocal_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10002123 M_CP_INTOPT(allow_agent_forwarding);
djm@openbsd.org7844f352016-11-30 03:00:05 +00002124 M_CP_INTOPT(disable_forwarding);
djm@openbsd.org8f574952017-06-24 06:34:38 +00002125 M_CP_INTOPT(expose_userauth_info);
Damien Millerab6de352010-06-26 09:38:45 +10002126 M_CP_INTOPT(permit_tun);
Damien Miller7acefbb2014-07-18 14:11:24 +10002127 M_CP_INTOPT(fwd_opts.gateway_ports);
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002128 M_CP_INTOPT(fwd_opts.streamlocal_bind_unlink);
Darren Tucker1629c072007-02-19 22:25:37 +11002129 M_CP_INTOPT(x11_display_offset);
2130 M_CP_INTOPT(x11_forwarding);
2131 M_CP_INTOPT(x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002132 M_CP_INTOPT(permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002133 M_CP_INTOPT(permit_user_rc);
Damien Miller7207f642008-05-19 15:34:50 +10002134 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10002135 M_CP_INTOPT(max_authtries);
markus@openbsd.orgf0ddede2016-11-23 23:14:15 +00002136 M_CP_INTOPT(client_alive_count_max);
2137 M_CP_INTOPT(client_alive_interval);
Damien Miller0dac6fb2010-11-20 15:19:38 +11002138 M_CP_INTOPT(ip_qos_interactive);
2139 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002140 M_CP_INTOPT(rekey_limit);
2141 M_CP_INTOPT(rekey_interval);
djm@openbsd.org54cd41a2017-05-17 01:24:17 +00002142 M_CP_INTOPT(log_level);
Darren Tucker1629c072007-02-19 22:25:37 +11002143
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002144 /*
2145 * The bind_mask is a mode_t that may be unsigned, so we can't use
2146 * M_CP_INTOPT - it does a signed comparison that causes compiler
2147 * warnings.
2148 */
dtucker@openbsd.org9faae502016-05-04 14:00:09 +00002149 if (src->fwd_opts.streamlocal_bind_mask != (mode_t)-1) {
djm@openbsd.orgcfefbce2016-05-03 15:57:39 +00002150 dst->fwd_opts.streamlocal_bind_mask =
2151 src->fwd_opts.streamlocal_bind_mask;
2152 }
2153
Damien Miller534b2cc2013-12-05 14:07:27 +11002154 /* M_CP_STROPT and M_CP_STRARRAYOPT should not appear before here */
2155#define M_CP_STROPT(n) do {\
2156 if (src->n != NULL && dst->n != src->n) { \
2157 free(dst->n); \
2158 dst->n = src->n; \
2159 } \
2160} while(0)
djm@openbsd.orgdceabc72017-10-05 15:52:03 +00002161#define M_CP_STRARRAYOPT(s, num_s) do {\
2162 u_int i; \
2163 if (src->num_s != 0) { \
2164 for (i = 0; i < dst->num_s; i++) \
2165 free(dst->s[i]); \
2166 free(dst->s); \
2167 dst->s = xcalloc(src->num_s, sizeof(*dst->s)); \
2168 for (i = 0; i < src->num_s; i++) \
2169 dst->s[i] = xstrdup(src->s[i]); \
2170 dst->num_s = src->num_s; \
djm@openbsd.org66bf74a2017-10-02 19:33:20 +00002171 } \
2172} while(0)
Damien Miller534b2cc2013-12-05 14:07:27 +11002173
Damien Millerf2e407e2011-05-20 19:04:14 +10002174 /* See comment in servconf.h */
2175 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10002176
djm@openbsd.orged085102015-10-29 08:05:01 +00002177 /* Arguments that accept '+...' need to be expanded */
2178 assemble_algorithms(dst);
2179
Damien Millerc2411902011-05-20 19:03:49 +10002180 /*
2181 * The only things that should be below this point are string options
2182 * which are only used after authentication.
2183 */
Damien Miller5d74e582011-05-20 19:03:31 +10002184 if (preauth)
2185 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002186
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002187 /* These options may be "none" to clear a global setting */
Damien Miller5d74e582011-05-20 19:03:31 +10002188 M_CP_STROPT(adm_forced_command);
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002189 if (option_clear_or_none(dst->adm_forced_command)) {
2190 free(dst->adm_forced_command);
2191 dst->adm_forced_command = NULL;
2192 }
Damien Miller5d74e582011-05-20 19:03:31 +10002193 M_CP_STROPT(chroot_directory);
djm@openbsd.org9fd04682015-11-13 04:38:06 +00002194 if (option_clear_or_none(dst->chroot_directory)) {
2195 free(dst->chroot_directory);
2196 dst->chroot_directory = NULL;
2197 }
Darren Tucker45150472006-07-12 22:34:17 +10002198}
2199
Darren Tucker1629c072007-02-19 22:25:37 +11002200#undef M_CP_INTOPT
2201#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10002202#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11002203
Darren Tucker45150472006-07-12 22:34:17 +10002204void
2205parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002206 struct connection_info *connectinfo)
Darren Tucker45150472006-07-12 22:34:17 +10002207{
2208 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10002209 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10002210
2211 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
2212
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002213 if ((obuf = cbuf = sshbuf_dup_string(conf)) == NULL)
2214 fatal("%s: sshbuf_dup_string failed", __func__);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002215 active = connectinfo ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10002216 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11002217 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10002218 if (process_server_config_line(options, cp, filename,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10002219 linenum++, &active, connectinfo) != 0)
Damien Miller95def091999-11-25 00:26:21 +11002220 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002221 }
Darren Tuckera627d422013-06-02 07:31:17 +10002222 free(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00002223 if (bad_options > 0)
2224 fatal("%s: terminating, %d bad configuration options",
2225 filename, bad_options);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00002226 process_queued_listen_addrs(options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002227}
Darren Tuckere7140f22008-06-10 23:01:51 +10002228
2229static const char *
Damien Miller82c55872011-06-23 08:20:30 +10002230fmt_multistate_int(int val, const struct multistate *m)
2231{
2232 u_int i;
2233
2234 for (i = 0; m[i].key != NULL; i++) {
2235 if (m[i].value == val)
2236 return m[i].key;
2237 }
2238 return "UNKNOWN";
2239}
2240
2241static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10002242fmt_intarg(ServerOpCodes code, int val)
2243{
Damien Miller82c55872011-06-23 08:20:30 +10002244 if (val == -1)
2245 return "unset";
2246 switch (code) {
2247 case sAddressFamily:
2248 return fmt_multistate_int(val, multistate_addressfamily);
2249 case sPermitRootLogin:
2250 return fmt_multistate_int(val, multistate_permitrootlogin);
2251 case sGatewayPorts:
2252 return fmt_multistate_int(val, multistate_gatewayports);
2253 case sCompression:
2254 return fmt_multistate_int(val, multistate_compression);
Damien Milleraa5b3f82012-12-03 09:50:54 +11002255 case sAllowTcpForwarding:
2256 return fmt_multistate_int(val, multistate_tcpfwd);
Damien Miller7acefbb2014-07-18 14:11:24 +10002257 case sAllowStreamLocalForwarding:
2258 return fmt_multistate_int(val, multistate_tcpfwd);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002259 case sFingerprintHash:
2260 return ssh_digest_alg_name(val);
Damien Miller82c55872011-06-23 08:20:30 +10002261 default:
2262 switch (val) {
2263 case 0:
2264 return "no";
2265 case 1:
2266 return "yes";
2267 default:
2268 return "UNKNOWN";
2269 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002270 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002271}
2272
2273static const char *
2274lookup_opcode_name(ServerOpCodes code)
2275{
2276 u_int i;
2277
2278 for (i = 0; keywords[i].name != NULL; i++)
2279 if (keywords[i].opcode == code)
2280 return(keywords[i].name);
2281 return "UNKNOWN";
2282}
2283
2284static void
2285dump_cfg_int(ServerOpCodes code, int val)
2286{
2287 printf("%s %d\n", lookup_opcode_name(code), val);
2288}
2289
2290static void
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002291dump_cfg_oct(ServerOpCodes code, int val)
2292{
2293 printf("%s 0%o\n", lookup_opcode_name(code), val);
2294}
2295
2296static void
Darren Tuckere7140f22008-06-10 23:01:51 +10002297dump_cfg_fmtint(ServerOpCodes code, int val)
2298{
2299 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2300}
2301
2302static void
2303dump_cfg_string(ServerOpCodes code, const char *val)
2304{
djm@openbsd.org161cf412014-12-22 07:55:51 +00002305 printf("%s %s\n", lookup_opcode_name(code),
2306 val == NULL ? "none" : val);
Darren Tuckere7140f22008-06-10 23:01:51 +10002307}
2308
2309static void
2310dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
2311{
2312 u_int i;
2313
2314 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10002315 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2316}
2317
2318static void
2319dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
2320{
2321 u_int i;
2322
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002323 if (count <= 0 && code != sAuthenticationMethods)
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002324 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002325 printf("%s", lookup_opcode_name(code));
2326 for (i = 0; i < count; i++)
2327 printf(" %s", vals[i]);
djm@openbsd.orgb64faeb2016-06-17 05:03:40 +00002328 if (code == sAuthenticationMethods && count == 0)
2329 printf(" any");
Damien Millerd8478b62011-05-29 21:39:36 +10002330 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002331}
2332
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002333static char *
2334format_listen_addrs(struct listenaddr *la)
Darren Tuckere7140f22008-06-10 23:01:51 +10002335{
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002336 int r;
Darren Tuckere7140f22008-06-10 23:01:51 +10002337 struct addrinfo *ai;
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002338 char addr[NI_MAXHOST], port[NI_MAXSERV];
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002339 char *laddr1 = xstrdup(""), *laddr2 = NULL;
Darren Tuckere7140f22008-06-10 23:01:51 +10002340
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002341 /*
2342 * ListenAddress must be after Port. add_one_listen_addr pushes
2343 * addresses onto a stack, so to maintain ordering we need to
2344 * print these in reverse order.
2345 */
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002346 for (ai = la->addrs; ai; ai = ai->ai_next) {
2347 if ((r = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
Darren Tuckere7140f22008-06-10 23:01:51 +10002348 sizeof(addr), port, sizeof(port),
2349 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002350 error("getnameinfo: %.100s", ssh_gai_strerror(r));
2351 continue;
Darren Tuckere7140f22008-06-10 23:01:51 +10002352 }
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002353 laddr2 = laddr1;
2354 if (ai->ai_family == AF_INET6) {
2355 xasprintf(&laddr1, "listenaddress [%s]:%s%s%s\n%s",
2356 addr, port,
2357 la->rdomain == NULL ? "" : " rdomain ",
2358 la->rdomain == NULL ? "" : la->rdomain,
2359 laddr2);
2360 } else {
2361 xasprintf(&laddr1, "listenaddress %s:%s%s%s\n%s",
2362 addr, port,
2363 la->rdomain == NULL ? "" : " rdomain ",
2364 la->rdomain == NULL ? "" : la->rdomain,
2365 laddr2);
2366 }
2367 free(laddr2);
Darren Tuckere7140f22008-06-10 23:01:51 +10002368 }
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00002369 return laddr1;
2370}
2371
2372void
2373dump_config(ServerOptions *o)
2374{
2375 char *s;
2376 u_int i;
2377
2378 /* these are usually at the top of the config */
2379 for (i = 0; i < o->num_ports; i++)
2380 printf("port %d\n", o->ports[i]);
2381 dump_cfg_fmtint(sAddressFamily, o->address_family);
2382
2383 for (i = 0; i < o->num_listen_addrs; i++) {
2384 s = format_listen_addrs(&o->listen_addrs[i]);
2385 printf("%s", s);
2386 free(s);
2387 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002388
2389 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10002390#ifdef USE_PAM
Darren Tucker70860b62015-04-17 10:56:13 +10002391 dump_cfg_fmtint(sUsePAM, o->use_pam);
Damien Miller212f0b02008-07-23 17:42:29 +10002392#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002393 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
Darren Tuckere7140f22008-06-10 23:01:51 +10002394 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
2395 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11002396 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10002397 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
2398 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002399 dump_cfg_oct(sStreamLocalBindMask, o->fwd_opts.streamlocal_bind_mask);
Darren Tuckere7140f22008-06-10 23:01:51 +10002400
2401 /* formatted integer arguments */
2402 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
2403 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
2404 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
Darren Tuckere7140f22008-06-10 23:01:51 +10002405 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
2406 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
2407 o->hostbased_uses_name_from_packet_only);
Darren Tuckere7140f22008-06-10 23:01:51 +10002408 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10002409#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10002410 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
2411 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
2412 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10002413# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10002414 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10002415# endif
2416#endif
2417#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10002418 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2419 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10002420#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002421 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2422 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2423 o->kbd_interactive_authentication);
2424 dump_cfg_fmtint(sChallengeResponseAuthentication,
2425 o->challenge_response_authentication);
2426 dump_cfg_fmtint(sPrintMotd, o->print_motd);
Darren Tuckerfd4e4f22016-02-24 10:44:25 +11002427#ifndef DISABLE_LASTLOG
Darren Tuckere7140f22008-06-10 23:01:51 +10002428 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
Darren Tuckerfd4e4f22016-02-24 10:44:25 +11002429#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002430 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
2431 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002432 dump_cfg_fmtint(sPermitTTY, o->permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002433 dump_cfg_fmtint(sPermitUserRC, o->permit_user_rc);
Darren Tuckere7140f22008-06-10 23:01:51 +10002434 dump_cfg_fmtint(sStrictModes, o->strict_modes);
2435 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
2436 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
2437 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
Darren Tuckere7140f22008-06-10 23:01:51 +10002438 dump_cfg_fmtint(sCompression, o->compression);
Damien Miller7acefbb2014-07-18 14:11:24 +10002439 dump_cfg_fmtint(sGatewayPorts, o->fwd_opts.gateway_ports);
Darren Tuckere7140f22008-06-10 23:01:51 +10002440 dump_cfg_fmtint(sUseDNS, o->use_dns);
2441 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002442 dump_cfg_fmtint(sAllowAgentForwarding, o->allow_agent_forwarding);
djm@openbsd.org7844f352016-11-30 03:00:05 +00002443 dump_cfg_fmtint(sDisableForwarding, o->disable_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002444 dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
djm@openbsd.org771c2f52016-05-03 15:25:06 +00002445 dump_cfg_fmtint(sStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002446 dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
djm@openbsd.org8f574952017-06-24 06:34:38 +00002447 dump_cfg_fmtint(sExposeAuthInfo, o->expose_userauth_info);
Darren Tuckere7140f22008-06-10 23:01:51 +10002448
2449 /* string arguments */
2450 dump_cfg_string(sPidFile, o->pid_file);
2451 dump_cfg_string(sXAuthLocation, o->xauth_location);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002452 dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
2453 dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
Darren Tuckere7140f22008-06-10 23:01:51 +10002454 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10002455 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11002456 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11002457 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
2458 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10002459 dump_cfg_string(sAuthorizedPrincipalsFile,
2460 o->authorized_principals_file);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002461 dump_cfg_string(sVersionAddendum, *o->version_addendum == '\0'
2462 ? "none" : o->version_addendum);
Damien Miller09d3e122012-10-31 08:58:58 +11002463 dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
2464 dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +00002465 dump_cfg_string(sAuthorizedPrincipalsCommand, o->authorized_principals_command);
2466 dump_cfg_string(sAuthorizedPrincipalsCommandUser, o->authorized_principals_command_user);
Damien Miller85b45e02013-07-20 13:21:52 +10002467 dump_cfg_string(sHostKeyAgent, o->host_key_agent);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002468 dump_cfg_string(sKexAlgorithms,
djm@openbsd.org259a02e2014-10-13 00:38:35 +00002469 o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002470 dump_cfg_string(sHostbasedAcceptedKeyTypes, o->hostbased_key_types ?
2471 o->hostbased_key_types : KEX_DEFAULT_PK_ALG);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00002472 dump_cfg_string(sHostKeyAlgorithms, o->hostkeyalgorithms ?
2473 o->hostkeyalgorithms : KEX_DEFAULT_PK_ALG);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002474 dump_cfg_string(sPubkeyAcceptedKeyTypes, o->pubkey_key_types ?
2475 o->pubkey_key_types : KEX_DEFAULT_PK_ALG);
Darren Tuckere7140f22008-06-10 23:01:51 +10002476
2477 /* string arguments requiring a lookup */
2478 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
2479 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
2480
2481 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10002482 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
2483 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002484 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
2485 o->host_key_files);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002486 dump_cfg_strarray(sHostCertificate, o->num_host_cert_files,
Damien Miller0a80ca12010-02-27 07:55:05 +11002487 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002488 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
2489 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
2490 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
2491 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
2492 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
Damien Millera6e3f012012-11-04 23:21:40 +11002493 dump_cfg_strarray_oneline(sAuthenticationMethods,
2494 o->num_auth_methods, o->auth_methods);
Darren Tuckere7140f22008-06-10 23:01:51 +10002495
2496 /* other arguments */
2497 for (i = 0; i < o->num_subsystems; i++)
2498 printf("subsystem %s %s\n", o->subsystem_name[i],
2499 o->subsystem_args[i]);
2500
2501 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
2502 o->max_startups_rate, o->max_startups);
2503
2504 for (i = 0; tunmode_desc[i].val != -1; i++)
2505 if (tunmode_desc[i].val == o->permit_tun) {
2506 s = tunmode_desc[i].text;
2507 break;
2508 }
2509 dump_cfg_string(sPermitTunnel, s);
2510
Damien Miller91475862011-05-05 14:14:34 +10002511 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2512 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11002513
dtucker@openbsd.org921ff002016-01-29 02:54:45 +00002514 printf("rekeylimit %llu %d\n", (unsigned long long)o->rekey_limit,
Damien Millera6d6c1f2013-08-21 02:40:01 +10002515 o->rekey_interval);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002516
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002517 printf("permitopen");
2518 if (o->num_permitted_opens == 0)
2519 printf(" any");
2520 else {
2521 for (i = 0; i < o->num_permitted_opens; i++)
2522 printf(" %s", o->permitted_opens[i]);
2523 }
2524 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002525}