blob: c0291947bcfc05fcaa1d8de79e39013c5c1a6fb6 [file] [log] [blame]
djm@openbsd.orge661a862015-05-04 06:10:48 +00001/* $OpenBSD: servconf.c,v 1.269 2015/05/04 06:10:48 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10005 *
Damien Millere4340be2000-09-16 13:29:08 +11006 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110011 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100012
13#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
Damien Millere3b60b52006-07-10 21:08:03 +100015#include <sys/types.h>
16#include <sys/socket.h>
17
Damien Miller0dac6fb2010-11-20 15:19:38 +110018#include <netinet/in.h>
19#include <netinet/in_systm.h>
20#include <netinet/ip.h>
21
Darren Tucker5f96f3b2013-05-16 20:29:28 +100022#include <ctype.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100023#include <netdb.h>
Damien Miller565ca3f2006-08-19 00:23:15 +100024#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100025#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100026#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100027#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100028#include <signal.h>
Damien Millere6b3b612006-07-24 14:01:23 +100029#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000030#include <limits.h>
Damien Millerd7834352006-08-05 12:39:39 +100031#include <stdarg.h>
Darren Tuckere7140f22008-06-10 23:01:51 +100032#include <errno.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100033#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100034#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100035#endif
Damien Millerbe43ebf2006-07-24 13:51:51 +100036
Damien Millerb84886b2008-05-19 15:05:07 +100037#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100041#include "buffer.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100042#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043#include "servconf.h"
Damien Miller78928792000-04-12 20:17:38 +100044#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100047#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000048#include "kex.h"
49#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100050#include "match.h"
Damien Miller9b439df2006-07-24 14:04:00 +100051#include "channels.h"
Damien Miller565ca3f2006-08-19 00:23:15 +100052#include "groupaccess.h"
Darren Tuckerfbcf8272012-05-19 19:37:01 +100053#include "canohost.h"
54#include "packet.h"
Damien Millera6e3f012012-11-04 23:21:40 +110055#include "hostfile.h"
56#include "auth.h"
djm@openbsd.org57d378e2014-08-19 23:58:28 +000057#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000058#include "digest.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000059
Damien Miller3dc71ad2009-01-28 16:31:22 +110060static void add_listen_addr(ServerOptions *, char *, int);
61static void add_one_listen_addr(ServerOptions *, char *, int);
Damien Miller34132e52000-01-14 15:45:46 +110062
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000063/* Use of privilege separation or not */
64extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100065extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067/* Initializes the server options to their default values. */
68
Damien Miller4af51302000-04-16 11:18:38 +100069void
Damien Miller95def091999-11-25 00:26:21 +110070initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071{
Damien Miller95def091999-11-25 00:26:21 +110072 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110073
74 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100075 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110076
77 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110078 options->num_ports = 0;
79 options->ports_from_cmdline = 0;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +000080 options->queued_listen_addrs = NULL;
81 options->num_queued_listens = 0;
Damien Miller34132e52000-01-14 15:45:46 +110082 options->listen_addrs = NULL;
Darren Tucker0f383232005-01-20 10:57:56 +110083 options->address_family = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110084 options->num_host_key_files = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110085 options->num_host_cert_files = 0;
Damien Miller85b45e02013-07-20 13:21:52 +100086 options->host_key_agent = NULL;
Damien Miller6f83b8e2000-05-02 09:23:45 +100087 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110088 options->server_key_bits = -1;
89 options->login_grace_time = -1;
90 options->key_regeneration_time = -1;
Ben Lindstromd8a90212001-02-15 03:08:27 +000091 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110092 options->ignore_rhosts = -1;
93 options->ignore_user_known_hosts = -1;
94 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +000095 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +110096 options->x11_forwarding = -1;
97 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +110098 options->x11_use_localhost = -1;
Damien Miller5ff30c62013-10-30 22:21:50 +110099 options->permit_tty = -1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000100 options->permit_user_rc = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000101 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100102 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +1100103 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +1100104 options->log_facility = SYSLOG_FACILITY_NOT_SET;
105 options->log_level = SYSLOG_LEVEL_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +1100106 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000107 options->hostbased_authentication = -1;
108 options->hostbased_uses_name_from_packet_only = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000109 options->hostbased_key_types = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100110 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100111 options->pubkey_authentication = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000112 options->pubkey_key_types = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100113 options->kerberos_authentication = -1;
114 options->kerberos_or_local_passwd = -1;
115 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100116 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000117 options->gss_authentication=-1;
118 options->gss_cleanup_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100119 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100120 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000121 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100122 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000123 options->permit_user_env = -1;
Damien Miller95def091999-11-25 00:26:21 +1100124 options->use_login = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000125 options->compression = -1;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000126 options->rekey_limit = -1;
127 options->rekey_interval = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100128 options->allow_tcp_forwarding = -1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000129 options->allow_streamlocal_forwarding = -1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000130 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100131 options->num_allow_users = 0;
132 options->num_deny_users = 0;
133 options->num_allow_groups = 0;
134 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000135 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000136 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000137 options->kex_algorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000138 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller7acefbb2014-07-18 14:11:24 +1000139 options->fwd_opts.gateway_ports = -1;
140 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
141 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000142 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000143 options->max_startups_begin = -1;
144 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000145 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000146 options->max_authtries = -1;
Damien Miller7207f642008-05-19 15:34:50 +1000147 options->max_sessions = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000148 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000149 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000150 options->client_alive_interval = -1;
151 options->client_alive_count_max = -1;
Damien Millerd8478b62011-05-29 21:39:36 +1000152 options->num_authkeys_files = 0;
Darren Tucker46bc0752004-05-02 22:11:30 +1000153 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100154 options->permit_tun = -1;
Damien Millera765cf42006-07-24 14:08:13 +1000155 options->num_permitted_opens = -1;
Damien Millere2754432006-07-24 14:06:47 +1000156 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100157 options->chroot_directory = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +1100158 options->authorized_keys_command = NULL;
159 options->authorized_keys_command_user = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100160 options->revoked_keys_file = NULL;
161 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000162 options->authorized_principals_file = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100163 options->ip_qos_interactive = -1;
164 options->ip_qos_bulk = -1;
Damien Miller23528812012-04-22 11:24:43 +1000165 options->version_addendum = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000166 options->fingerprint_hash = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167}
168
djm@openbsd.org161cf412014-12-22 07:55:51 +0000169/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
170static int
171option_clear_or_none(const char *o)
172{
173 return o == NULL || strcasecmp(o, "none") == 0;
174}
175
Damien Miller4af51302000-04-16 11:18:38 +1000176void
Damien Miller95def091999-11-25 00:26:21 +1100177fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178{
djm@openbsd.org161cf412014-12-22 07:55:51 +0000179 int i;
180
Damien Miller726273e2001-11-12 11:40:11 +1100181 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000182 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000183 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100184
185 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100186 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +1100187 options->protocol = SSH_PROTO_2;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100188 if (options->num_host_key_files == 0) {
189 /* fill default hostkeys for protocols */
190 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100191 options->host_key_files[options->num_host_key_files++] =
192 _PATH_HOST_KEY_FILE;
193 if (options->protocol & SSH_PROTO_2) {
194 options->host_key_files[options->num_host_key_files++] =
195 _PATH_HOST_RSA_KEY_FILE;
196 options->host_key_files[options->num_host_key_files++] =
197 _PATH_HOST_DSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100198#ifdef OPENSSL_HAS_ECC
Damien Millere13cadf2010-09-10 11:15:33 +1000199 options->host_key_files[options->num_host_key_files++] =
200 _PATH_HOST_ECDSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100201#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +1100202 options->host_key_files[options->num_host_key_files++] =
203 _PATH_HOST_ED25519_KEY_FILE;
Damien Miller7fc23732002-01-22 23:19:11 +1100204 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100205 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100206 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100207 if (options->num_ports == 0)
208 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000209 if (options->address_family == -1)
210 options->address_family = AF_UNSPEC;
Damien Miller34132e52000-01-14 15:45:46 +1100211 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000212 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000213 if (options->pid_file == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000214 options->pid_file = xstrdup(_PATH_SSH_DAEMON_PID_FILE);
Damien Miller95def091999-11-25 00:26:21 +1100215 if (options->server_key_bits == -1)
Darren Tucker7499b0c2008-07-02 22:35:43 +1000216 options->server_key_bits = 1024;
Damien Miller95def091999-11-25 00:26:21 +1100217 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000218 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100219 if (options->key_regeneration_time == -1)
220 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000221 if (options->permit_root_login == PERMIT_NOT_SET)
djm@openbsd.org88a7c592015-04-27 21:42:48 +0000222 options->permit_root_login = PERMIT_NO;
Damien Miller95def091999-11-25 00:26:21 +1100223 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100224 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100225 if (options->ignore_user_known_hosts == -1)
226 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100227 if (options->print_motd == -1)
228 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000229 if (options->print_lastlog == -1)
230 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100231 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100232 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100233 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100234 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100235 if (options->x11_use_localhost == -1)
236 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000237 if (options->xauth_location == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000238 options->xauth_location = xstrdup(_PATH_XAUTH);
Damien Miller5ff30c62013-10-30 22:21:50 +1100239 if (options->permit_tty == -1)
240 options->permit_tty = 1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000241 if (options->permit_user_rc == -1)
242 options->permit_user_rc = 1;
Damien Miller95def091999-11-25 00:26:21 +1100243 if (options->strict_modes == -1)
244 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100245 if (options->tcp_keep_alive == -1)
246 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100247 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100248 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100249 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000250 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100251 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100252 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000253 if (options->hostbased_authentication == -1)
254 options->hostbased_authentication = 0;
255 if (options->hostbased_uses_name_from_packet_only == -1)
256 options->hostbased_uses_name_from_packet_only = 0;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000257 if (options->hostbased_key_types == NULL)
258 options->hostbased_key_types = xstrdup("*");
Damien Miller95def091999-11-25 00:26:21 +1100259 if (options->rsa_authentication == -1)
260 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100261 if (options->pubkey_authentication == -1)
262 options->pubkey_authentication = 1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000263 if (options->pubkey_key_types == NULL)
264 options->pubkey_key_types = xstrdup("*");
Damien Miller95def091999-11-25 00:26:21 +1100265 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000266 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100267 if (options->kerberos_or_local_passwd == -1)
268 options->kerberos_or_local_passwd = 1;
269 if (options->kerberos_ticket_cleanup == -1)
270 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100271 if (options->kerberos_get_afs_token == -1)
272 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000273 if (options->gss_authentication == -1)
274 options->gss_authentication = 0;
275 if (options->gss_cleanup_creds == -1)
276 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100277 if (options->password_authentication == -1)
278 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100279 if (options->kbd_interactive_authentication == -1)
280 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000281 if (options->challenge_response_authentication == -1)
282 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100283 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100284 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000285 if (options->permit_user_env == -1)
286 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100287 if (options->use_login == -1)
288 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000289 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000290 options->compression = COMP_DELAYED;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000291 if (options->rekey_limit == -1)
292 options->rekey_limit = 0;
293 if (options->rekey_interval == -1)
294 options->rekey_interval = 0;
Damien Miller50a41ed2000-10-16 12:14:42 +1100295 if (options->allow_tcp_forwarding == -1)
Damien Milleraa5b3f82012-12-03 09:50:54 +1100296 options->allow_tcp_forwarding = FORWARD_ALLOW;
Damien Miller7acefbb2014-07-18 14:11:24 +1000297 if (options->allow_streamlocal_forwarding == -1)
298 options->allow_streamlocal_forwarding = FORWARD_ALLOW;
Damien Miller4f755cd2008-05-19 14:57:41 +1000299 if (options->allow_agent_forwarding == -1)
300 options->allow_agent_forwarding = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000301 if (options->fwd_opts.gateway_ports == -1)
302 options->fwd_opts.gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000303 if (options->max_startups == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100304 options->max_startups = 100;
Damien Miller942da032000-08-18 13:59:06 +1000305 if (options->max_startups_rate == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100306 options->max_startups_rate = 30; /* 30% */
Damien Miller942da032000-08-18 13:59:06 +1000307 if (options->max_startups_begin == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100308 options->max_startups_begin = 10;
Darren Tucker89413db2004-05-24 10:36:23 +1000309 if (options->max_authtries == -1)
310 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000311 if (options->max_sessions == -1)
312 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000313 if (options->use_dns == -1)
deraadt@openbsd.org3cd51032015-02-02 01:57:44 +0000314 options->use_dns = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000315 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100316 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000317 if (options->client_alive_count_max == -1)
318 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000319 if (options->num_authkeys_files == 0) {
320 options->authorized_keys_files[options->num_authkeys_files++] =
321 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS);
322 options->authorized_keys_files[options->num_authkeys_files++] =
323 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS2);
324 }
Damien Millerd27b9472005-12-13 19:29:02 +1100325 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100326 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100327 if (options->ip_qos_interactive == -1)
328 options->ip_qos_interactive = IPTOS_LOWDELAY;
329 if (options->ip_qos_bulk == -1)
330 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller23528812012-04-22 11:24:43 +1000331 if (options->version_addendum == NULL)
332 options->version_addendum = xstrdup("");
Damien Miller7acefbb2014-07-18 14:11:24 +1000333 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
334 options->fwd_opts.streamlocal_bind_mask = 0177;
335 if (options->fwd_opts.streamlocal_bind_unlink == -1)
336 options->fwd_opts.streamlocal_bind_unlink = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000337 if (options->fingerprint_hash == -1)
338 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
Ben Lindstromfb62a692002-06-06 19:47:11 +0000339 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000340 if (use_privsep == -1)
Damien Miller5a5c2b92012-07-31 12:21:34 +1000341 use_privsep = PRIVSEP_NOSANDBOX;
Damien Miller4903eb42002-06-21 16:20:44 +1000342
djm@openbsd.org161cf412014-12-22 07:55:51 +0000343#define CLEAR_ON_NONE(v) \
344 do { \
345 if (option_clear_or_none(v)) { \
346 free(v); \
347 v = NULL; \
348 } \
349 } while(0)
350 CLEAR_ON_NONE(options->pid_file);
351 CLEAR_ON_NONE(options->xauth_location);
352 CLEAR_ON_NONE(options->banner);
353 CLEAR_ON_NONE(options->trusted_user_ca_keys);
354 CLEAR_ON_NONE(options->revoked_keys_file);
355 for (i = 0; i < options->num_host_key_files; i++)
356 CLEAR_ON_NONE(options->host_key_files[i]);
357 for (i = 0; i < options->num_host_cert_files; i++)
358 CLEAR_ON_NONE(options->host_cert_files[i]);
359#undef CLEAR_ON_NONE
360
Tim Rice40017b02002-07-14 13:36:49 -0700361#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000362 if (use_privsep && options->compression == 1) {
363 error("This platform does not support both privilege "
364 "separation and compression");
365 error("Compression disabled");
366 options->compression = 0;
367 }
368#endif
369
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370}
371
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100373typedef enum {
374 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100375 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000376 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100377 /* Standard Options */
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000378 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime,
379 sKeyRegenerationTime, sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000380 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100381 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100382 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000383 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100384 sPasswordAuthentication, sKbdInteractiveAuthentication,
385 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000386 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100387 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller5ff30c62013-10-30 22:21:50 +1100388 sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000389 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000390 sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000391 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000392 sGatewayPorts, sPubkeyAuthentication, sPubkeyAcceptedKeyTypes,
393 sXAuthLocation, sSubsystem, sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000394 sBanner, sUseDNS, sHostbasedAuthentication,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000395 sHostbasedUsesNameFromPacketOnly, sHostbasedAcceptedKeyTypes,
396 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
Damien Millerd27b9472005-12-13 19:29:02 +1100397 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100398 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100399 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller7cc194f2014-02-04 11:12:56 +1100400 sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000401 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Miller23528812012-04-22 11:24:43 +1000402 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Miller09d3e122012-10-31 08:58:58 +1100403 sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
Damien Miller72e6b5c2014-07-04 09:00:04 +1000404 sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
Damien Miller7acefbb2014-07-18 14:11:24 +1000405 sStreamLocalBindMask, sStreamLocalBindUnlink,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000406 sAllowStreamLocalForwarding, sFingerprintHash,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000407 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408} ServerOpCodes;
409
Darren Tucker45150472006-07-12 22:34:17 +1000410#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
411#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
412#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
413
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100415static struct {
416 const char *name;
417 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000418 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100419} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100420 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000421#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000422 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000423#else
Darren Tucker45150472006-07-12 22:34:17 +1000424 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000425#endif
Darren Tucker45150472006-07-12 22:34:17 +1000426 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100427 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000428 { "port", sPort, SSHCFG_GLOBAL },
429 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
430 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
Damien Miller85b45e02013-07-20 13:21:52 +1000431 { "hostkeyagent", sHostKeyAgent, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000432 { "pidfile", sPidFile, SSHCFG_GLOBAL },
433 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
434 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
435 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100436 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000437 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
438 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
439 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100440 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
441 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000442 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000443 { "hostbasedacceptedkeytypes", sHostbasedAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100444 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
445 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000446 { "pubkeyacceptedkeytypes", sPubkeyAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000447 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000448#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100449 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000450 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
451 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100452#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000453 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000454#else
Darren Tucker45150472006-07-12 22:34:17 +1000455 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100456#endif
457#else
Darren Tucker1629c072007-02-19 22:25:37 +1100458 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000459 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
460 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
461 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000462#endif
Darren Tucker45150472006-07-12 22:34:17 +1000463 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
464 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000465#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100466 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000467 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000468#else
Darren Tucker1629c072007-02-19 22:25:37 +1100469 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000470 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000471#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100472 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
473 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100474 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000475 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
476 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
477 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
478 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
479 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
480 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
481 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
482 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000483 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
484 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
485 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000486 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
487 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100488 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000489 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
490 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
491 { "compression", sCompression, SSHCFG_GLOBAL },
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000492 { "rekeylimit", sRekeyLimit, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000493 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
494 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
495 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000496 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Damien Millerc24da772012-06-20 21:53:58 +1000497 { "allowusers", sAllowUsers, SSHCFG_ALL },
498 { "denyusers", sDenyUsers, SSHCFG_ALL },
499 { "allowgroups", sAllowGroups, SSHCFG_ALL },
500 { "denygroups", sDenyGroups, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000501 { "ciphers", sCiphers, SSHCFG_GLOBAL },
502 { "macs", sMacs, SSHCFG_GLOBAL },
503 { "protocol", sProtocol, SSHCFG_GLOBAL },
504 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
505 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
506 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000507 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000508 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100509 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000510 { "usedns", sUseDNS, SSHCFG_GLOBAL },
511 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
512 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
513 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
514 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000515 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000516 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000517 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Damien Millerc24da772012-06-20 21:53:58 +1000518 { "acceptenv", sAcceptEnv, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000519 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Damien Miller5ff30c62013-10-30 22:21:50 +1100520 { "permittty", sPermitTTY, SSHCFG_ALL },
Damien Miller72e6b5c2014-07-04 09:00:04 +1000521 { "permituserrc", sPermitUserRC, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000522 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000523 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000524 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100525 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100526 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100527 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
528 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000529 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000530 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100531 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller09d3e122012-10-31 08:58:58 +1100532 { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
533 { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000534 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Damien Millera6e3f012012-11-04 23:21:40 +1100535 { "authenticationmethods", sAuthenticationMethods, SSHCFG_ALL },
Damien Miller7acefbb2014-07-18 14:11:24 +1000536 { "streamlocalbindmask", sStreamLocalBindMask, SSHCFG_ALL },
537 { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
538 { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000539 { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000540 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000541};
542
Darren Tuckere7140f22008-06-10 23:01:51 +1000543static struct {
544 int val;
545 char *text;
546} tunmode_desc[] = {
547 { SSH_TUNMODE_NO, "no" },
548 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
549 { SSH_TUNMODE_ETHERNET, "ethernet" },
550 { SSH_TUNMODE_YES, "yes" },
551 { -1, NULL }
552};
553
Damien Miller5428f641999-11-25 11:54:57 +1100554/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000555 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100556 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557
Damien Miller4af51302000-04-16 11:18:38 +1000558static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100559parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000560 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000562 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563
Damien Miller95def091999-11-25 00:26:21 +1100564 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000565 if (strcasecmp(cp, keywords[i].name) == 0) {
566 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100567 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000568 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000570 error("%s: line %d: Bad configuration option: %s",
571 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100572 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573}
574
Darren Tucker88b6fb22010-01-13 22:44:29 +1100575char *
576derelativise_path(const char *path)
577{
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000578 char *expanded, *ret, cwd[PATH_MAX];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100579
djm@openbsd.org161cf412014-12-22 07:55:51 +0000580 if (strcasecmp(path, "none") == 0)
581 return xstrdup("none");
Darren Tucker88b6fb22010-01-13 22:44:29 +1100582 expanded = tilde_expand_filename(path, getuid());
583 if (*expanded == '/')
584 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100585 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100586 fatal("%s: getcwd: %s", __func__, strerror(errno));
587 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tuckera627d422013-06-02 07:31:17 +1000588 free(expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100589 return ret;
590}
591
Ben Lindstrombba81212001-06-25 05:01:22 +0000592static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100593add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100594{
Damien Millereccb9de2005-06-17 12:59:34 +1000595 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100596
Ben Lindstrom19066a12001-04-12 23:39:26 +0000597 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000598 for (i = 0; i < options->num_ports; i++)
599 add_one_listen_addr(options, addr, options->ports[i]);
600 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000601 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000602}
603
Ben Lindstrombba81212001-06-25 05:01:22 +0000604static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100605add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000606{
607 struct addrinfo hints, *ai, *aitop;
608 char strport[NI_MAXSERV];
609 int gaierr;
610
611 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100612 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000613 hints.ai_socktype = SOCK_STREAM;
614 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100615 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000616 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
617 fatal("bad addr or host: %s (%s)",
618 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100619 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000620 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
621 ;
622 ai->ai_next = options->listen_addrs;
623 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100624}
625
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000626/*
627 * Queue a ListenAddress to be processed once we have all of the Ports
628 * and AddressFamily options.
629 */
630static void
631queue_listen_addr(ServerOptions *options, char *addr, int port)
632{
633 options->queued_listen_addrs = xreallocarray(
634 options->queued_listen_addrs, options->num_queued_listens + 1,
635 sizeof(addr));
636 options->queued_listen_ports = xreallocarray(
637 options->queued_listen_ports, options->num_queued_listens + 1,
638 sizeof(port));
639 options->queued_listen_addrs[options->num_queued_listens] =
640 xstrdup(addr);
641 options->queued_listen_ports[options->num_queued_listens] = port;
642 options->num_queued_listens++;
643}
644
645/*
646 * Process queued (text) ListenAddress entries.
647 */
648static void
649process_queued_listen_addrs(ServerOptions *options)
650{
651 u_int i;
652
653 if (options->num_ports == 0)
654 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
655 if (options->address_family == -1)
656 options->address_family = AF_UNSPEC;
657
658 for (i = 0; i < options->num_queued_listens; i++) {
659 add_listen_addr(options, options->queued_listen_addrs[i],
660 options->queued_listen_ports[i]);
661 free(options->queued_listen_addrs[i]);
662 options->queued_listen_addrs[i] = NULL;
663 }
664 free(options->queued_listen_addrs);
665 options->queued_listen_addrs = NULL;
666 free(options->queued_listen_ports);
667 options->queued_listen_ports = NULL;
668 options->num_queued_listens = 0;
669}
670
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000671struct connection_info *
672get_connection_info(int populate, int use_dns)
673{
674 static struct connection_info ci;
675
676 if (!populate)
677 return &ci;
678 ci.host = get_canonical_hostname(use_dns);
679 ci.address = get_remote_ipaddr();
680 ci.laddress = get_local_ipaddr(packet_get_connection_in());
681 ci.lport = get_local_port();
682 return &ci;
683}
684
Darren Tucker45150472006-07-12 22:34:17 +1000685/*
686 * The strategy for the Match blocks is that the config file is parsed twice.
687 *
688 * The first time is at startup. activep is initialized to 1 and the
689 * directives in the global context are processed and acted on. Hitting a
690 * Match directive unsets activep and the directives inside the block are
691 * checked for syntax only.
692 *
693 * The second time is after a connection has been established but before
694 * authentication. activep is initialized to 2 and global config directives
695 * are ignored since they have already been processed. If the criteria in a
696 * Match block is met, activep is set and the subsequent directives
697 * processed and actioned until EOF or another Match block unsets it. Any
698 * options set are copied into the main server config.
699 *
700 * Potential additions/improvements:
701 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
702 *
703 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
704 * Match Address 192.168.0.*
705 * Tag trusted
706 * Match Group wheel
707 * Tag trusted
708 * Match Tag trusted
709 * AllowTcpForwarding yes
710 * GatewayPorts clientspecified
711 * [...]
712 *
713 * - Add a PermittedChannelRequests directive
714 * Match Group shell
715 * PermittedChannelRequests session,forwarded-tcpip
716 */
717
718static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000719match_cfg_line_group(const char *grps, int line, const char *user)
720{
721 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000722 struct passwd *pw;
723
Damien Miller565ca3f2006-08-19 00:23:15 +1000724 if (user == NULL)
725 goto out;
726
727 if ((pw = getpwnam(user)) == NULL) {
728 debug("Can't match group at line %d because user %.100s does "
729 "not exist", line, user);
730 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
731 debug("Can't Match group because user %.100s not in any group "
732 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000733 } else if (ga_match_pattern_list(grps) != 1) {
734 debug("user %.100s does not match group list %.100s at line %d",
735 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000736 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000737 debug("user %.100s matched group list %.100s at line %d", user,
738 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000739 result = 1;
740 }
741out:
742 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000743 return result;
744}
745
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000746/*
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000747 * All of the attributes on a single Match line are ANDed together, so we need
Damien Miller03bf2e62013-10-24 21:01:26 +1100748 * to check every attribute and set the result to zero if any attribute does
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000749 * not match.
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000750 */
Damien Miller565ca3f2006-08-19 00:23:15 +1000751static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000752match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +1000753{
Damien Millercf31f382013-10-24 21:02:56 +1100754 int result = 1, attributes = 0, port;
Darren Tucker45150472006-07-12 22:34:17 +1000755 char *arg, *attrib, *cp = *condition;
Darren Tucker45150472006-07-12 22:34:17 +1000756
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000757 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000758 debug3("checking syntax for 'Match %s'", cp);
759 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000760 debug3("checking match for '%s' user %s host %s addr %s "
761 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
762 ci->host ? ci->host : "(null)",
763 ci->address ? ci->address : "(null)",
764 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +1000765
766 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100767 attributes++;
768 if (strcasecmp(attrib, "all") == 0) {
769 if (attributes != 1 ||
770 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
771 error("'all' cannot be combined with other "
772 "Match attributes");
773 return -1;
774 }
775 *condition = cp;
776 return 1;
777 }
Darren Tucker45150472006-07-12 22:34:17 +1000778 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
779 error("Missing Match criteria for %s", attrib);
780 return -1;
781 }
Darren Tucker45150472006-07-12 22:34:17 +1000782 if (strcasecmp(attrib, "user") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000783 if (ci == NULL || ci->user == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000784 result = 0;
785 continue;
786 }
djm@openbsd.orge661a862015-05-04 06:10:48 +0000787 if (match_pattern_list(ci->user, arg, 0) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000788 result = 0;
789 else
790 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000791 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000792 } else if (strcasecmp(attrib, "group") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000793 if (ci == NULL || ci->user == NULL) {
794 result = 0;
795 continue;
796 }
797 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +1000798 case -1:
799 return -1;
800 case 0:
801 result = 0;
802 }
Darren Tucker45150472006-07-12 22:34:17 +1000803 } else if (strcasecmp(attrib, "host") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000804 if (ci == NULL || ci->host == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000805 result = 0;
806 continue;
807 }
djm@openbsd.orge661a862015-05-04 06:10:48 +0000808 if (match_hostname(ci->host, arg) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000809 result = 0;
810 else
811 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000812 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +1000813 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000814 if (ci == NULL || ci->address == NULL) {
815 result = 0;
816 continue;
817 }
818 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000819 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000820 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000821 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000822 break;
823 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000824 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000825 result = 0;
826 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000827 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000828 return -1;
829 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000830 } else if (strcasecmp(attrib, "localaddress") == 0){
831 if (ci == NULL || ci->laddress == NULL) {
832 result = 0;
833 continue;
834 }
835 switch (addr_match_list(ci->laddress, arg)) {
836 case 1:
837 debug("connection from %.100s matched "
838 "'LocalAddress %.100s' at line %d",
839 ci->laddress, arg, line);
840 break;
841 case 0:
842 case -1:
843 result = 0;
844 break;
845 case -2:
846 return -1;
847 }
848 } else if (strcasecmp(attrib, "localport") == 0) {
849 if ((port = a2port(arg)) == -1) {
850 error("Invalid LocalPort '%s' on Match line",
851 arg);
852 return -1;
853 }
854 if (ci == NULL || ci->lport == 0) {
855 result = 0;
856 continue;
857 }
858 /* TODO support port lists */
859 if (port == ci->lport)
860 debug("connection from %.100s matched "
861 "'LocalPort %d' at line %d",
862 ci->laddress, port, line);
863 else
864 result = 0;
Darren Tucker45150472006-07-12 22:34:17 +1000865 } else {
866 error("Unsupported Match attribute %s", attrib);
867 return -1;
868 }
869 }
Damien Millercf31f382013-10-24 21:02:56 +1100870 if (attributes == 0) {
871 error("One or more attributes required for Match");
872 return -1;
873 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000874 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000875 debug3("match %sfound", result ? "" : "not ");
876 *condition = cp;
877 return result;
878}
879
Damien Millere2754432006-07-24 14:06:47 +1000880#define WHITESPACE " \t\r\n"
881
Damien Miller33322122011-06-20 14:43:11 +1000882/* Multistate option parsing */
883struct multistate {
884 char *key;
885 int value;
886};
887static const struct multistate multistate_addressfamily[] = {
888 { "inet", AF_INET },
889 { "inet6", AF_INET6 },
890 { "any", AF_UNSPEC },
891 { NULL, -1 }
892};
893static const struct multistate multistate_permitrootlogin[] = {
894 { "without-password", PERMIT_NO_PASSWD },
895 { "forced-commands-only", PERMIT_FORCED_ONLY },
896 { "yes", PERMIT_YES },
897 { "no", PERMIT_NO },
898 { NULL, -1 }
899};
900static const struct multistate multistate_compression[] = {
901 { "delayed", COMP_DELAYED },
902 { "yes", COMP_ZLIB },
903 { "no", COMP_NONE },
904 { NULL, -1 }
905};
906static const struct multistate multistate_gatewayports[] = {
907 { "clientspecified", 2 },
908 { "yes", 1 },
909 { "no", 0 },
910 { NULL, -1 }
911};
Damien Miller69ff1df2011-06-23 08:30:03 +1000912static const struct multistate multistate_privsep[] = {
Damien Miller5a5c2b92012-07-31 12:21:34 +1000913 { "yes", PRIVSEP_NOSANDBOX },
914 { "sandbox", PRIVSEP_ON },
915 { "nosandbox", PRIVSEP_NOSANDBOX },
Damien Miller69ff1df2011-06-23 08:30:03 +1000916 { "no", PRIVSEP_OFF },
917 { NULL, -1 }
918};
Damien Milleraa5b3f82012-12-03 09:50:54 +1100919static const struct multistate multistate_tcpfwd[] = {
920 { "yes", FORWARD_ALLOW },
921 { "all", FORWARD_ALLOW },
922 { "no", FORWARD_DENY },
923 { "remote", FORWARD_REMOTE },
924 { "local", FORWARD_LOCAL },
925 { NULL, -1 }
926};
Damien Miller33322122011-06-20 14:43:11 +1000927
Ben Lindstromade03f62001-12-06 18:22:17 +0000928int
929process_server_config_line(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000930 const char *filename, int linenum, int *activep,
931 struct connection_info *connectinfo)
Ben Lindstromade03f62001-12-06 18:22:17 +0000932{
Darren Tucker09c0f032013-05-16 20:48:57 +1000933 char *cp, **charptr, *arg, *p;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000934 int cmdline = 0, *intptr, value, value2, n, port;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100935 SyslogFacility *log_facility_ptr;
936 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000937 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000938 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000939 size_t len;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000940 long long val64;
Damien Miller33322122011-06-20 14:43:11 +1000941 const struct multistate *multistate_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000942
943 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100944 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100945 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000946 /* Ignore leading whitespace */
947 if (*arg == '\0')
948 arg = strdelim(&cp);
949 if (!arg || !*arg || *arg == '#')
950 return 0;
951 intptr = NULL;
952 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000953 opcode = parse_token(arg, filename, linenum, &flags);
954
955 if (activep == NULL) { /* We are processing a command line directive */
956 cmdline = 1;
957 activep = &cmdline;
958 }
959 if (*activep && opcode != sMatch)
960 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
961 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000962 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000963 fatal("%s line %d: Directive '%s' is not allowed "
964 "within a Match block", filename, linenum, arg);
965 } else { /* this is a directive we have already processed */
966 while (arg)
967 arg = strdelim(&cp);
968 return 0;
969 }
970 }
971
Ben Lindstromade03f62001-12-06 18:22:17 +0000972 switch (opcode) {
973 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000974 case sUsePAM:
975 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000976 goto parse_flag;
977
978 /* Standard Options */
979 case sBadOption:
980 return -1;
981 case sPort:
982 /* ignore ports from configfile if cmdline specifies ports */
983 if (options->ports_from_cmdline)
984 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000985 if (options->num_ports >= MAX_PORTS)
986 fatal("%s line %d: too many ports.",
987 filename, linenum);
988 arg = strdelim(&cp);
989 if (!arg || *arg == '\0')
990 fatal("%s line %d: missing port number.",
991 filename, linenum);
992 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100993 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000994 fatal("%s line %d: Badly formatted port number.",
995 filename, linenum);
996 break;
997
998 case sServerKeyBits:
999 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +10001000 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +00001001 arg = strdelim(&cp);
1002 if (!arg || *arg == '\0')
1003 fatal("%s line %d: missing integer value.",
1004 filename, linenum);
1005 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +10001006 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001007 *intptr = value;
1008 break;
1009
1010 case sLoginGraceTime:
1011 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +10001012 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +00001013 arg = strdelim(&cp);
1014 if (!arg || *arg == '\0')
1015 fatal("%s line %d: missing time value.",
1016 filename, linenum);
1017 if ((value = convtime(arg)) == -1)
1018 fatal("%s line %d: invalid time value.",
1019 filename, linenum);
1020 if (*intptr == -1)
1021 *intptr = value;
1022 break;
1023
1024 case sKeyRegenerationTime:
1025 intptr = &options->key_regeneration_time;
1026 goto parse_time;
1027
1028 case sListenAddress:
1029 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001030 if (arg == NULL || *arg == '\0')
1031 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +00001032 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +10001033 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
1034 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
1035 && strchr(p+1, ':') != NULL) {
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00001036 queue_listen_addr(options, arg, 0);
Damien Miller203c7052005-08-12 22:11:37 +10001037 break;
1038 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001039 p = hpdelim(&arg);
1040 if (p == NULL)
1041 fatal("%s line %d: bad address:port usage",
1042 filename, linenum);
1043 p = cleanhostname(p);
1044 if (arg == NULL)
1045 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001046 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001047 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001048
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00001049 queue_listen_addr(options, p, port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001050
Ben Lindstromade03f62001-12-06 18:22:17 +00001051 break;
1052
Darren Tucker0f383232005-01-20 10:57:56 +11001053 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +10001054 intptr = &options->address_family;
1055 multistate_ptr = multistate_addressfamily;
Damien Miller33322122011-06-20 14:43:11 +10001056 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +11001057 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +10001058 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +10001059 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +10001060 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +10001061 value = -1;
1062 for (i = 0; multistate_ptr[i].key != NULL; i++) {
1063 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
1064 value = multistate_ptr[i].value;
1065 break;
1066 }
1067 }
1068 if (value == -1)
1069 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +11001070 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +10001071 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +11001072 *intptr = value;
1073 break;
1074
Ben Lindstromade03f62001-12-06 18:22:17 +00001075 case sHostKeyFile:
1076 intptr = &options->num_host_key_files;
1077 if (*intptr >= MAX_HOSTKEYS)
1078 fatal("%s line %d: too many host keys specified (max %d).",
1079 filename, linenum, MAX_HOSTKEYS);
1080 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +10001081 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +00001082 arg = strdelim(&cp);
1083 if (!arg || *arg == '\0')
1084 fatal("%s line %d: missing file name.",
1085 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001086 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +11001087 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001088 /* increase optional counter */
1089 if (intptr != NULL)
1090 *intptr = *intptr + 1;
1091 }
1092 break;
1093
Damien Miller85b45e02013-07-20 13:21:52 +10001094 case sHostKeyAgent:
1095 charptr = &options->host_key_agent;
1096 arg = strdelim(&cp);
1097 if (!arg || *arg == '\0')
1098 fatal("%s line %d: missing socket name.",
1099 filename, linenum);
1100 if (*activep && *charptr == NULL)
1101 *charptr = !strcmp(arg, SSH_AUTHSOCKET_ENV_NAME) ?
1102 xstrdup(arg) : derelativise_path(arg);
1103 break;
1104
Damien Miller0a80ca12010-02-27 07:55:05 +11001105 case sHostCertificate:
1106 intptr = &options->num_host_cert_files;
1107 if (*intptr >= MAX_HOSTKEYS)
1108 fatal("%s line %d: too many host certificates "
1109 "specified (max %d).", filename, linenum,
1110 MAX_HOSTCERTS);
1111 charptr = &options->host_cert_files[*intptr];
1112 goto parse_filename;
1113 break;
1114
Ben Lindstromade03f62001-12-06 18:22:17 +00001115 case sPidFile:
1116 charptr = &options->pid_file;
1117 goto parse_filename;
1118
1119 case sPermitRootLogin:
1120 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +10001121 multistate_ptr = multistate_permitrootlogin;
1122 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001123
1124 case sIgnoreRhosts:
1125 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +10001126 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +00001127 arg = strdelim(&cp);
1128 if (!arg || *arg == '\0')
1129 fatal("%s line %d: missing yes/no argument.",
1130 filename, linenum);
1131 value = 0; /* silence compiler */
1132 if (strcmp(arg, "yes") == 0)
1133 value = 1;
1134 else if (strcmp(arg, "no") == 0)
1135 value = 0;
1136 else
1137 fatal("%s line %d: Bad yes/no argument: %s",
1138 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +10001139 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001140 *intptr = value;
1141 break;
1142
1143 case sIgnoreUserKnownHosts:
1144 intptr = &options->ignore_user_known_hosts;
1145 goto parse_flag;
1146
Ben Lindstromade03f62001-12-06 18:22:17 +00001147 case sRhostsRSAAuthentication:
1148 intptr = &options->rhosts_rsa_authentication;
1149 goto parse_flag;
1150
1151 case sHostbasedAuthentication:
1152 intptr = &options->hostbased_authentication;
1153 goto parse_flag;
1154
1155 case sHostbasedUsesNameFromPacketOnly:
1156 intptr = &options->hostbased_uses_name_from_packet_only;
1157 goto parse_flag;
1158
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001159 case sHostbasedAcceptedKeyTypes:
1160 charptr = &options->hostbased_key_types;
1161 parse_keytypes:
1162 arg = strdelim(&cp);
1163 if (!arg || *arg == '\0')
1164 fatal("%s line %d: Missing argument.",
1165 filename, linenum);
1166 if (!sshkey_names_valid2(arg, 1))
1167 fatal("%s line %d: Bad key types '%s'.",
1168 filename, linenum, arg ? arg : "<NONE>");
1169 if (*activep && *charptr == NULL)
1170 *charptr = xstrdup(arg);
1171 break;
1172
Ben Lindstromade03f62001-12-06 18:22:17 +00001173 case sRSAAuthentication:
1174 intptr = &options->rsa_authentication;
1175 goto parse_flag;
1176
1177 case sPubkeyAuthentication:
1178 intptr = &options->pubkey_authentication;
1179 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001180
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001181 case sPubkeyAcceptedKeyTypes:
1182 charptr = &options->pubkey_key_types;
1183 goto parse_keytypes;
1184
Ben Lindstromade03f62001-12-06 18:22:17 +00001185 case sKerberosAuthentication:
1186 intptr = &options->kerberos_authentication;
1187 goto parse_flag;
1188
1189 case sKerberosOrLocalPasswd:
1190 intptr = &options->kerberos_or_local_passwd;
1191 goto parse_flag;
1192
1193 case sKerberosTicketCleanup:
1194 intptr = &options->kerberos_ticket_cleanup;
1195 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001196
Darren Tucker22ef5082003-12-31 11:37:34 +11001197 case sKerberosGetAFSToken:
1198 intptr = &options->kerberos_get_afs_token;
1199 goto parse_flag;
1200
Darren Tucker0efd1552003-08-26 11:49:55 +10001201 case sGssAuthentication:
1202 intptr = &options->gss_authentication;
1203 goto parse_flag;
1204
1205 case sGssCleanupCreds:
1206 intptr = &options->gss_cleanup_creds;
1207 goto parse_flag;
1208
Ben Lindstromade03f62001-12-06 18:22:17 +00001209 case sPasswordAuthentication:
1210 intptr = &options->password_authentication;
1211 goto parse_flag;
1212
1213 case sKbdInteractiveAuthentication:
1214 intptr = &options->kbd_interactive_authentication;
1215 goto parse_flag;
1216
1217 case sChallengeResponseAuthentication:
1218 intptr = &options->challenge_response_authentication;
1219 goto parse_flag;
1220
1221 case sPrintMotd:
1222 intptr = &options->print_motd;
1223 goto parse_flag;
1224
1225 case sPrintLastLog:
1226 intptr = &options->print_lastlog;
1227 goto parse_flag;
1228
1229 case sX11Forwarding:
1230 intptr = &options->x11_forwarding;
1231 goto parse_flag;
1232
1233 case sX11DisplayOffset:
1234 intptr = &options->x11_display_offset;
1235 goto parse_int;
1236
Damien Miller95c249f2002-02-05 12:11:34 +11001237 case sX11UseLocalhost:
1238 intptr = &options->x11_use_localhost;
1239 goto parse_flag;
1240
Ben Lindstromade03f62001-12-06 18:22:17 +00001241 case sXAuthLocation:
1242 charptr = &options->xauth_location;
1243 goto parse_filename;
1244
Damien Miller5ff30c62013-10-30 22:21:50 +11001245 case sPermitTTY:
1246 intptr = &options->permit_tty;
1247 goto parse_flag;
1248
Damien Miller72e6b5c2014-07-04 09:00:04 +10001249 case sPermitUserRC:
1250 intptr = &options->permit_user_rc;
1251 goto parse_flag;
1252
Ben Lindstromade03f62001-12-06 18:22:17 +00001253 case sStrictModes:
1254 intptr = &options->strict_modes;
1255 goto parse_flag;
1256
Damien Miller12c150e2003-12-17 16:31:10 +11001257 case sTCPKeepAlive:
1258 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001259 goto parse_flag;
1260
1261 case sEmptyPasswd:
1262 intptr = &options->permit_empty_passwd;
1263 goto parse_flag;
1264
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001265 case sPermitUserEnvironment:
1266 intptr = &options->permit_user_env;
1267 goto parse_flag;
1268
Ben Lindstromade03f62001-12-06 18:22:17 +00001269 case sUseLogin:
1270 intptr = &options->use_login;
1271 goto parse_flag;
1272
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001273 case sCompression:
1274 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001275 multistate_ptr = multistate_compression;
1276 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001277
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001278 case sRekeyLimit:
1279 arg = strdelim(&cp);
1280 if (!arg || *arg == '\0')
1281 fatal("%.200s line %d: Missing argument.", filename,
1282 linenum);
1283 if (strcmp(arg, "default") == 0) {
1284 val64 = 0;
1285 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001286 if (scan_scaled(arg, &val64) == -1)
1287 fatal("%.200s line %d: Bad number '%s': %s",
1288 filename, linenum, arg, strerror(errno));
1289 /* check for too-large or too-small limits */
1290 if (val64 > UINT_MAX)
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001291 fatal("%.200s line %d: RekeyLimit too large",
1292 filename, linenum);
1293 if (val64 != 0 && val64 < 16)
1294 fatal("%.200s line %d: RekeyLimit too small",
1295 filename, linenum);
1296 }
1297 if (*activep && options->rekey_limit == -1)
1298 options->rekey_limit = (u_int32_t)val64;
1299 if (cp != NULL) { /* optional rekey interval present */
1300 if (strcmp(cp, "none") == 0) {
1301 (void)strdelim(&cp); /* discard */
1302 break;
1303 }
1304 intptr = &options->rekey_interval;
1305 goto parse_time;
1306 }
1307 break;
1308
Ben Lindstromade03f62001-12-06 18:22:17 +00001309 case sGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +10001310 intptr = &options->fwd_opts.gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001311 multistate_ptr = multistate_gatewayports;
1312 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001313
Damien Miller3a961dc2003-06-03 10:25:48 +10001314 case sUseDNS:
1315 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001316 goto parse_flag;
1317
1318 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001319 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001320 arg = strdelim(&cp);
1321 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001322 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001323 fatal("%.200s line %d: unsupported log facility '%s'",
1324 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001325 if (*log_facility_ptr == -1)
1326 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001327 break;
1328
1329 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001330 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001331 arg = strdelim(&cp);
1332 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001333 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001334 fatal("%.200s line %d: unsupported log level '%s'",
1335 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001336 if (*log_level_ptr == -1)
1337 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001338 break;
1339
1340 case sAllowTcpForwarding:
1341 intptr = &options->allow_tcp_forwarding;
Damien Milleraa5b3f82012-12-03 09:50:54 +11001342 multistate_ptr = multistate_tcpfwd;
1343 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001344
Damien Miller7acefbb2014-07-18 14:11:24 +10001345 case sAllowStreamLocalForwarding:
1346 intptr = &options->allow_streamlocal_forwarding;
1347 multistate_ptr = multistate_tcpfwd;
1348 goto parse_multistate;
1349
Damien Miller4f755cd2008-05-19 14:57:41 +10001350 case sAllowAgentForwarding:
1351 intptr = &options->allow_agent_forwarding;
1352 goto parse_flag;
1353
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001354 case sUsePrivilegeSeparation:
1355 intptr = &use_privsep;
Damien Miller69ff1df2011-06-23 08:30:03 +10001356 multistate_ptr = multistate_privsep;
1357 goto parse_multistate;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001358
Ben Lindstromade03f62001-12-06 18:22:17 +00001359 case sAllowUsers:
1360 while ((arg = strdelim(&cp)) && *arg != '\0') {
1361 if (options->num_allow_users >= MAX_ALLOW_USERS)
1362 fatal("%s line %d: too many allow users.",
1363 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001364 if (!*activep)
1365 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001366 options->allow_users[options->num_allow_users++] =
1367 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001368 }
1369 break;
1370
1371 case sDenyUsers:
1372 while ((arg = strdelim(&cp)) && *arg != '\0') {
1373 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001374 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001375 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001376 if (!*activep)
1377 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001378 options->deny_users[options->num_deny_users++] =
1379 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001380 }
1381 break;
1382
1383 case sAllowGroups:
1384 while ((arg = strdelim(&cp)) && *arg != '\0') {
1385 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1386 fatal("%s line %d: too many allow groups.",
1387 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001388 if (!*activep)
1389 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001390 options->allow_groups[options->num_allow_groups++] =
1391 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001392 }
1393 break;
1394
1395 case sDenyGroups:
1396 while ((arg = strdelim(&cp)) && *arg != '\0') {
1397 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1398 fatal("%s line %d: too many deny groups.",
1399 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001400 if (!*activep)
1401 continue;
1402 options->deny_groups[options->num_deny_groups++] =
1403 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001404 }
1405 break;
1406
1407 case sCiphers:
1408 arg = strdelim(&cp);
1409 if (!arg || *arg == '\0')
1410 fatal("%s line %d: Missing argument.", filename, linenum);
1411 if (!ciphers_valid(arg))
1412 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1413 filename, linenum, arg ? arg : "<NONE>");
1414 if (options->ciphers == NULL)
1415 options->ciphers = xstrdup(arg);
1416 break;
1417
1418 case sMacs:
1419 arg = strdelim(&cp);
1420 if (!arg || *arg == '\0')
1421 fatal("%s line %d: Missing argument.", filename, linenum);
1422 if (!mac_valid(arg))
1423 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1424 filename, linenum, arg ? arg : "<NONE>");
1425 if (options->macs == NULL)
1426 options->macs = xstrdup(arg);
1427 break;
1428
Damien Millerd5f62bf2010-09-24 22:11:14 +10001429 case sKexAlgorithms:
1430 arg = strdelim(&cp);
1431 if (!arg || *arg == '\0')
1432 fatal("%s line %d: Missing argument.",
1433 filename, linenum);
1434 if (!kex_names_valid(arg))
1435 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1436 filename, linenum, arg ? arg : "<NONE>");
1437 if (options->kex_algorithms == NULL)
1438 options->kex_algorithms = xstrdup(arg);
1439 break;
1440
Ben Lindstromade03f62001-12-06 18:22:17 +00001441 case sProtocol:
1442 intptr = &options->protocol;
1443 arg = strdelim(&cp);
1444 if (!arg || *arg == '\0')
1445 fatal("%s line %d: Missing argument.", filename, linenum);
1446 value = proto_spec(arg);
1447 if (value == SSH_PROTO_UNKNOWN)
1448 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001449 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001450 if (*intptr == SSH_PROTO_UNKNOWN)
1451 *intptr = value;
1452 break;
1453
1454 case sSubsystem:
1455 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1456 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001457 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001458 }
1459 arg = strdelim(&cp);
1460 if (!arg || *arg == '\0')
1461 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001462 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001463 if (!*activep) {
1464 arg = strdelim(&cp);
1465 break;
1466 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001467 for (i = 0; i < options->num_subsystems; i++)
1468 if (strcmp(arg, options->subsystem_name[i]) == 0)
1469 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001470 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001471 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1472 arg = strdelim(&cp);
1473 if (!arg || *arg == '\0')
1474 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001475 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001476 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001477
1478 /* Collect arguments (separate to executable) */
1479 p = xstrdup(arg);
1480 len = strlen(p) + 1;
1481 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1482 len += 1 + strlen(arg);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001483 p = xreallocarray(p, 1, len);
Damien Miller917f9b62006-07-10 20:36:47 +10001484 strlcat(p, " ", len);
1485 strlcat(p, arg, len);
1486 }
1487 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001488 options->num_subsystems++;
1489 break;
1490
1491 case sMaxStartups:
1492 arg = strdelim(&cp);
1493 if (!arg || *arg == '\0')
1494 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001495 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001496 if ((n = sscanf(arg, "%d:%d:%d",
1497 &options->max_startups_begin,
1498 &options->max_startups_rate,
1499 &options->max_startups)) == 3) {
1500 if (options->max_startups_begin >
1501 options->max_startups ||
1502 options->max_startups_rate > 100 ||
1503 options->max_startups_rate < 1)
1504 fatal("%s line %d: Illegal MaxStartups spec.",
1505 filename, linenum);
1506 } else if (n != 1)
1507 fatal("%s line %d: Illegal MaxStartups spec.",
1508 filename, linenum);
1509 else
1510 options->max_startups = options->max_startups_begin;
1511 break;
1512
Darren Tucker89413db2004-05-24 10:36:23 +10001513 case sMaxAuthTries:
1514 intptr = &options->max_authtries;
1515 goto parse_int;
1516
Damien Miller7207f642008-05-19 15:34:50 +10001517 case sMaxSessions:
1518 intptr = &options->max_sessions;
1519 goto parse_int;
1520
Ben Lindstromade03f62001-12-06 18:22:17 +00001521 case sBanner:
1522 charptr = &options->banner;
1523 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001524
Ben Lindstromade03f62001-12-06 18:22:17 +00001525 /*
1526 * These options can contain %X options expanded at
1527 * connect time, so that you can specify paths like:
1528 *
1529 * AuthorizedKeysFile /etc/ssh_keys/%u
1530 */
1531 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001532 if (*activep && options->num_authkeys_files == 0) {
1533 while ((arg = strdelim(&cp)) && *arg != '\0') {
1534 if (options->num_authkeys_files >=
1535 MAX_AUTHKEYS_FILES)
1536 fatal("%s line %d: "
1537 "too many authorized keys files.",
1538 filename, linenum);
1539 options->authorized_keys_files[
1540 options->num_authkeys_files++] =
1541 tilde_expand_filename(arg, getuid());
1542 }
1543 }
1544 return 0;
1545
Damien Miller30da3442010-05-10 11:58:03 +10001546 case sAuthorizedPrincipalsFile:
1547 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001548 arg = strdelim(&cp);
1549 if (!arg || *arg == '\0')
1550 fatal("%s line %d: missing file name.",
1551 filename, linenum);
1552 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001553 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001554 /* increase optional counter */
1555 if (intptr != NULL)
1556 *intptr = *intptr + 1;
1557 }
1558 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001559
1560 case sClientAliveInterval:
1561 intptr = &options->client_alive_interval;
1562 goto parse_time;
1563
1564 case sClientAliveCountMax:
1565 intptr = &options->client_alive_count_max;
1566 goto parse_int;
1567
Darren Tucker46bc0752004-05-02 22:11:30 +10001568 case sAcceptEnv:
1569 while ((arg = strdelim(&cp)) && *arg != '\0') {
1570 if (strchr(arg, '=') != NULL)
1571 fatal("%s line %d: Invalid environment name.",
1572 filename, linenum);
1573 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1574 fatal("%s line %d: too many allow env.",
1575 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001576 if (!*activep)
Damien Millerc24da772012-06-20 21:53:58 +10001577 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001578 options->accept_env[options->num_accept_env++] =
1579 xstrdup(arg);
1580 }
1581 break;
1582
Damien Millerd27b9472005-12-13 19:29:02 +11001583 case sPermitTunnel:
1584 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001585 arg = strdelim(&cp);
1586 if (!arg || *arg == '\0')
1587 fatal("%s line %d: Missing yes/point-to-point/"
1588 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001589 value = -1;
1590 for (i = 0; tunmode_desc[i].val != -1; i++)
1591 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1592 value = tunmode_desc[i].val;
1593 break;
1594 }
1595 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001596 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1597 "no argument: %s", filename, linenum, arg);
1598 if (*intptr == -1)
1599 *intptr = value;
1600 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001601
Darren Tucker45150472006-07-12 22:34:17 +10001602 case sMatch:
1603 if (cmdline)
1604 fatal("Match directive not supported as a command-line "
1605 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001606 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10001607 if (value < 0)
1608 fatal("%s line %d: Bad Match condition", filename,
1609 linenum);
1610 *activep = value;
1611 break;
1612
Damien Miller9b439df2006-07-24 14:04:00 +10001613 case sPermitOpen:
1614 arg = strdelim(&cp);
1615 if (!arg || *arg == '\0')
1616 fatal("%s line %d: missing PermitOpen specification",
1617 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001618 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001619 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001620 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001621 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001622 options->num_permitted_opens = 0;
1623 }
Damien Miller9b439df2006-07-24 14:04:00 +10001624 break;
1625 }
Damien Millerc6081482012-04-22 11:18:53 +10001626 if (strcmp(arg, "none") == 0) {
1627 if (*activep && n == -1) {
Damien Millerc6081482012-04-22 11:18:53 +10001628 options->num_permitted_opens = 1;
1629 channel_disable_adm_local_opens();
1630 }
1631 break;
1632 }
Damien Millera29b95e2007-01-05 16:28:36 +11001633 if (*activep && n == -1)
1634 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001635 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1636 p = hpdelim(&arg);
1637 if (p == NULL)
1638 fatal("%s line %d: missing host in PermitOpen",
1639 filename, linenum);
1640 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001641 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001642 fatal("%s line %d: bad port number in "
1643 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001644 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001645 options->num_permitted_opens =
1646 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001647 }
Damien Miller9b439df2006-07-24 14:04:00 +10001648 break;
1649
Damien Millere2754432006-07-24 14:06:47 +10001650 case sForceCommand:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001651 if (cp == NULL || *cp == '\0')
Damien Millere2754432006-07-24 14:06:47 +10001652 fatal("%.200s line %d: Missing argument.", filename,
1653 linenum);
1654 len = strspn(cp, WHITESPACE);
1655 if (*activep && options->adm_forced_command == NULL)
1656 options->adm_forced_command = xstrdup(cp + len);
1657 return 0;
1658
Damien Millerd8cb1f12008-02-10 22:40:12 +11001659 case sChrootDirectory:
1660 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001661
1662 arg = strdelim(&cp);
1663 if (!arg || *arg == '\0')
1664 fatal("%s line %d: missing file name.",
1665 filename, linenum);
1666 if (*activep && *charptr == NULL)
1667 *charptr = xstrdup(arg);
1668 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001669
Damien Miller1aed65e2010-03-04 21:53:35 +11001670 case sTrustedUserCAKeys:
1671 charptr = &options->trusted_user_ca_keys;
1672 goto parse_filename;
1673
1674 case sRevokedKeys:
1675 charptr = &options->revoked_keys_file;
1676 goto parse_filename;
1677
Damien Miller0dac6fb2010-11-20 15:19:38 +11001678 case sIPQoS:
1679 arg = strdelim(&cp);
1680 if ((value = parse_ipqos(arg)) == -1)
1681 fatal("%s line %d: Bad IPQoS value: %s",
1682 filename, linenum, arg);
1683 arg = strdelim(&cp);
1684 if (arg == NULL)
1685 value2 = value;
1686 else if ((value2 = parse_ipqos(arg)) == -1)
1687 fatal("%s line %d: Bad IPQoS value: %s",
1688 filename, linenum, arg);
1689 if (*activep) {
1690 options->ip_qos_interactive = value;
1691 options->ip_qos_bulk = value2;
1692 }
1693 break;
1694
Damien Miller23528812012-04-22 11:24:43 +10001695 case sVersionAddendum:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001696 if (cp == NULL || *cp == '\0')
Damien Miller23528812012-04-22 11:24:43 +10001697 fatal("%.200s line %d: Missing argument.", filename,
1698 linenum);
1699 len = strspn(cp, WHITESPACE);
1700 if (*activep && options->version_addendum == NULL) {
1701 if (strcasecmp(cp + len, "none") == 0)
1702 options->version_addendum = xstrdup("");
1703 else if (strchr(cp + len, '\r') != NULL)
1704 fatal("%.200s line %d: Invalid argument",
1705 filename, linenum);
1706 else
1707 options->version_addendum = xstrdup(cp + len);
1708 }
1709 return 0;
1710
Damien Miller09d3e122012-10-31 08:58:58 +11001711 case sAuthorizedKeysCommand:
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001712 if (cp == NULL)
1713 fatal("%.200s line %d: Missing argument.", filename,
1714 linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001715 len = strspn(cp, WHITESPACE);
1716 if (*activep && options->authorized_keys_command == NULL) {
1717 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1718 fatal("%.200s line %d: AuthorizedKeysCommand "
1719 "must be an absolute path",
1720 filename, linenum);
1721 options->authorized_keys_command = xstrdup(cp + len);
1722 }
1723 return 0;
1724
1725 case sAuthorizedKeysCommandUser:
1726 charptr = &options->authorized_keys_command_user;
1727
1728 arg = strdelim(&cp);
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001729 if (!arg || *arg == '\0')
1730 fatal("%s line %d: missing AuthorizedKeysCommandUser "
1731 "argument.", filename, linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001732 if (*activep && *charptr == NULL)
1733 *charptr = xstrdup(arg);
1734 break;
1735
Damien Millera6e3f012012-11-04 23:21:40 +11001736 case sAuthenticationMethods:
1737 if (*activep && options->num_auth_methods == 0) {
1738 while ((arg = strdelim(&cp)) && *arg != '\0') {
1739 if (options->num_auth_methods >=
1740 MAX_AUTH_METHODS)
1741 fatal("%s line %d: "
1742 "too many authentication methods.",
1743 filename, linenum);
1744 if (auth2_methods_valid(arg, 0) != 0)
1745 fatal("%s line %d: invalid "
1746 "authentication method list.",
1747 filename, linenum);
1748 options->auth_methods[
1749 options->num_auth_methods++] = xstrdup(arg);
1750 }
1751 }
1752 return 0;
1753
Damien Miller7acefbb2014-07-18 14:11:24 +10001754 case sStreamLocalBindMask:
1755 arg = strdelim(&cp);
1756 if (!arg || *arg == '\0')
1757 fatal("%s line %d: missing StreamLocalBindMask argument.",
1758 filename, linenum);
1759 /* Parse mode in octal format */
1760 value = strtol(arg, &p, 8);
1761 if (arg == p || value < 0 || value > 0777)
1762 fatal("%s line %d: Bad mask.", filename, linenum);
1763 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1764 break;
1765
1766 case sStreamLocalBindUnlink:
1767 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1768 goto parse_flag;
1769
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001770 case sFingerprintHash:
1771 arg = strdelim(&cp);
1772 if (!arg || *arg == '\0')
1773 fatal("%.200s line %d: Missing argument.",
1774 filename, linenum);
1775 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1776 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1777 filename, linenum, arg);
1778 if (*activep)
1779 options->fingerprint_hash = value;
1780 break;
1781
Ben Lindstromade03f62001-12-06 18:22:17 +00001782 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001783 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001784 filename, linenum, arg);
1785 while (arg)
1786 arg = strdelim(&cp);
1787 break;
1788
Damien Millerf9b3feb2003-05-16 11:38:32 +10001789 case sUnsupported:
1790 logit("%s line %d: Unsupported option %s",
1791 filename, linenum, arg);
1792 while (arg)
1793 arg = strdelim(&cp);
1794 break;
1795
Ben Lindstromade03f62001-12-06 18:22:17 +00001796 default:
1797 fatal("%s line %d: Missing handler for opcode %s (%d)",
1798 filename, linenum, arg, opcode);
1799 }
1800 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1801 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1802 filename, linenum, arg);
1803 return 0;
1804}
1805
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001806/* Reads the server configuration file. */
1807
Damien Miller4af51302000-04-16 11:18:38 +10001808void
Darren Tucker645ab752004-06-25 13:33:20 +10001809load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001810{
Damien Miller46cb75a2012-07-31 12:22:37 +10001811 char line[4096], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001812 FILE *f;
Damien Miller46cb75a2012-07-31 12:22:37 +10001813 int lineno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001814
Darren Tucker645ab752004-06-25 13:33:20 +10001815 debug2("%s: filename %s", __func__, filename);
1816 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001817 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001818 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001819 }
Darren Tucker645ab752004-06-25 13:33:20 +10001820 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001821 while (fgets(line, sizeof(line), f)) {
Damien Miller46cb75a2012-07-31 12:22:37 +10001822 lineno++;
1823 if (strlen(line) == sizeof(line) - 1)
1824 fatal("%s line %d too long", filename, lineno);
Darren Tucker645ab752004-06-25 13:33:20 +10001825 /*
1826 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001827 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001828 * line numbers later for error messages
1829 */
1830 if ((cp = strchr(line, '#')) != NULL)
1831 memcpy(cp, "\n", 2);
1832 cp = line + strspn(line, " \t\r");
1833
1834 buffer_append(conf, cp, strlen(cp));
1835 }
1836 buffer_append(conf, "\0", 1);
1837 fclose(f);
1838 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1839}
1840
1841void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001842parse_server_match_config(ServerOptions *options,
1843 struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10001844{
Darren Tucker45150472006-07-12 22:34:17 +10001845 ServerOptions mo;
1846
1847 initialize_server_options(&mo);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001848 parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11001849 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001850}
1851
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001852int parse_server_match_testspec(struct connection_info *ci, char *spec)
1853{
1854 char *p;
1855
1856 while ((p = strsep(&spec, ",")) && *p != '\0') {
1857 if (strncmp(p, "addr=", 5) == 0) {
1858 ci->address = xstrdup(p + 5);
1859 } else if (strncmp(p, "host=", 5) == 0) {
1860 ci->host = xstrdup(p + 5);
1861 } else if (strncmp(p, "user=", 5) == 0) {
1862 ci->user = xstrdup(p + 5);
1863 } else if (strncmp(p, "laddr=", 6) == 0) {
1864 ci->laddress = xstrdup(p + 6);
1865 } else if (strncmp(p, "lport=", 6) == 0) {
1866 ci->lport = a2port(p + 6);
1867 if (ci->lport == -1) {
1868 fprintf(stderr, "Invalid port '%s' in test mode"
1869 " specification %s\n", p+6, p);
1870 return -1;
1871 }
1872 } else {
1873 fprintf(stderr, "Invalid test mode specification %s\n",
1874 p);
1875 return -1;
1876 }
1877 }
1878 return 0;
1879}
1880
1881/*
1882 * returns 1 for a complete spec, 0 for partial spec and -1 for an
1883 * empty spec.
1884 */
1885int server_match_spec_complete(struct connection_info *ci)
1886{
1887 if (ci->user && ci->host && ci->address)
1888 return 1; /* complete */
1889 if (!ci->user && !ci->host && !ci->address)
1890 return -1; /* empty */
1891 return 0; /* partial */
1892}
1893
Darren Tucker1629c072007-02-19 22:25:37 +11001894/*
1895 * Copy any supported values that are set.
1896 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001897 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001898 * array values that are not used pre-authentication, because any that we
1899 * do use must be explictly sent in mm_getpwnamallow().
1900 */
Darren Tucker45150472006-07-12 22:34:17 +10001901void
Darren Tucker1629c072007-02-19 22:25:37 +11001902copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001903{
Damien Miller534b2cc2013-12-05 14:07:27 +11001904#define M_CP_INTOPT(n) do {\
1905 if (src->n != -1) \
1906 dst->n = src->n; \
1907} while (0)
1908
Darren Tucker1629c072007-02-19 22:25:37 +11001909 M_CP_INTOPT(password_authentication);
1910 M_CP_INTOPT(gss_authentication);
1911 M_CP_INTOPT(rsa_authentication);
1912 M_CP_INTOPT(pubkey_authentication);
1913 M_CP_INTOPT(kerberos_authentication);
1914 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001915 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001916 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001917 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001918 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001919
1920 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10001921 M_CP_INTOPT(allow_streamlocal_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001922 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001923 M_CP_INTOPT(permit_tun);
Damien Miller7acefbb2014-07-18 14:11:24 +10001924 M_CP_INTOPT(fwd_opts.gateway_ports);
Darren Tucker1629c072007-02-19 22:25:37 +11001925 M_CP_INTOPT(x11_display_offset);
1926 M_CP_INTOPT(x11_forwarding);
1927 M_CP_INTOPT(x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11001928 M_CP_INTOPT(permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10001929 M_CP_INTOPT(permit_user_rc);
Damien Miller7207f642008-05-19 15:34:50 +10001930 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001931 M_CP_INTOPT(max_authtries);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001932 M_CP_INTOPT(ip_qos_interactive);
1933 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001934 M_CP_INTOPT(rekey_limit);
1935 M_CP_INTOPT(rekey_interval);
Darren Tucker1629c072007-02-19 22:25:37 +11001936
Damien Miller534b2cc2013-12-05 14:07:27 +11001937 /* M_CP_STROPT and M_CP_STRARRAYOPT should not appear before here */
1938#define M_CP_STROPT(n) do {\
1939 if (src->n != NULL && dst->n != src->n) { \
1940 free(dst->n); \
1941 dst->n = src->n; \
1942 } \
1943} while(0)
1944#define M_CP_STRARRAYOPT(n, num_n) do {\
1945 if (src->num_n != 0) { \
1946 for (dst->num_n = 0; dst->num_n < src->num_n; dst->num_n++) \
1947 dst->n[dst->num_n] = xstrdup(src->n[dst->num_n]); \
1948 } \
1949} while(0)
1950
Damien Millerf2e407e2011-05-20 19:04:14 +10001951 /* See comment in servconf.h */
1952 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10001953
Damien Millerc2411902011-05-20 19:03:49 +10001954 /*
1955 * The only things that should be below this point are string options
1956 * which are only used after authentication.
1957 */
Damien Miller5d74e582011-05-20 19:03:31 +10001958 if (preauth)
1959 return;
Damien Millerd8478b62011-05-29 21:39:36 +10001960
Damien Miller5d74e582011-05-20 19:03:31 +10001961 M_CP_STROPT(adm_forced_command);
1962 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001963}
1964
Darren Tucker1629c072007-02-19 22:25:37 +11001965#undef M_CP_INTOPT
1966#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10001967#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11001968
Darren Tucker45150472006-07-12 22:34:17 +10001969void
1970parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001971 struct connection_info *connectinfo)
Darren Tucker45150472006-07-12 22:34:17 +10001972{
1973 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001974 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001975
1976 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1977
Darren Tucker9fbac712004-08-12 22:41:44 +10001978 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001979 active = connectinfo ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001980 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001981 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001982 if (process_server_config_line(options, cp, filename,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001983 linenum++, &active, connectinfo) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001984 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001985 }
Darren Tuckera627d422013-06-02 07:31:17 +10001986 free(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001987 if (bad_options > 0)
1988 fatal("%s: terminating, %d bad configuration options",
1989 filename, bad_options);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00001990 process_queued_listen_addrs(options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001991}
Darren Tuckere7140f22008-06-10 23:01:51 +10001992
1993static const char *
Damien Miller82c55872011-06-23 08:20:30 +10001994fmt_multistate_int(int val, const struct multistate *m)
1995{
1996 u_int i;
1997
1998 for (i = 0; m[i].key != NULL; i++) {
1999 if (m[i].value == val)
2000 return m[i].key;
2001 }
2002 return "UNKNOWN";
2003}
2004
2005static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10002006fmt_intarg(ServerOpCodes code, int val)
2007{
Damien Miller82c55872011-06-23 08:20:30 +10002008 if (val == -1)
2009 return "unset";
2010 switch (code) {
2011 case sAddressFamily:
2012 return fmt_multistate_int(val, multistate_addressfamily);
2013 case sPermitRootLogin:
2014 return fmt_multistate_int(val, multistate_permitrootlogin);
2015 case sGatewayPorts:
2016 return fmt_multistate_int(val, multistate_gatewayports);
2017 case sCompression:
2018 return fmt_multistate_int(val, multistate_compression);
Damien Miller69ff1df2011-06-23 08:30:03 +10002019 case sUsePrivilegeSeparation:
2020 return fmt_multistate_int(val, multistate_privsep);
Damien Milleraa5b3f82012-12-03 09:50:54 +11002021 case sAllowTcpForwarding:
2022 return fmt_multistate_int(val, multistate_tcpfwd);
Damien Miller7acefbb2014-07-18 14:11:24 +10002023 case sAllowStreamLocalForwarding:
2024 return fmt_multistate_int(val, multistate_tcpfwd);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002025 case sFingerprintHash:
2026 return ssh_digest_alg_name(val);
Damien Miller82c55872011-06-23 08:20:30 +10002027 case sProtocol:
Darren Tuckere7140f22008-06-10 23:01:51 +10002028 switch (val) {
2029 case SSH_PROTO_1:
2030 return "1";
2031 case SSH_PROTO_2:
2032 return "2";
2033 case (SSH_PROTO_1|SSH_PROTO_2):
2034 return "2,1";
2035 default:
2036 return "UNKNOWN";
2037 }
Damien Miller82c55872011-06-23 08:20:30 +10002038 default:
2039 switch (val) {
2040 case 0:
2041 return "no";
2042 case 1:
2043 return "yes";
2044 default:
2045 return "UNKNOWN";
2046 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002047 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002048}
2049
2050static const char *
2051lookup_opcode_name(ServerOpCodes code)
2052{
2053 u_int i;
2054
2055 for (i = 0; keywords[i].name != NULL; i++)
2056 if (keywords[i].opcode == code)
2057 return(keywords[i].name);
2058 return "UNKNOWN";
2059}
2060
2061static void
2062dump_cfg_int(ServerOpCodes code, int val)
2063{
2064 printf("%s %d\n", lookup_opcode_name(code), val);
2065}
2066
2067static void
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002068dump_cfg_oct(ServerOpCodes code, int val)
2069{
2070 printf("%s 0%o\n", lookup_opcode_name(code), val);
2071}
2072
2073static void
Darren Tuckere7140f22008-06-10 23:01:51 +10002074dump_cfg_fmtint(ServerOpCodes code, int val)
2075{
2076 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2077}
2078
2079static void
2080dump_cfg_string(ServerOpCodes code, const char *val)
2081{
2082 if (val == NULL)
2083 return;
djm@openbsd.org161cf412014-12-22 07:55:51 +00002084 printf("%s %s\n", lookup_opcode_name(code),
2085 val == NULL ? "none" : val);
Darren Tuckere7140f22008-06-10 23:01:51 +10002086}
2087
2088static void
2089dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
2090{
2091 u_int i;
2092
2093 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10002094 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2095}
2096
2097static void
2098dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
2099{
2100 u_int i;
2101
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002102 if (count <= 0)
2103 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002104 printf("%s", lookup_opcode_name(code));
2105 for (i = 0; i < count; i++)
2106 printf(" %s", vals[i]);
2107 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002108}
2109
2110void
2111dump_config(ServerOptions *o)
2112{
2113 u_int i;
2114 int ret;
2115 struct addrinfo *ai;
2116 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002117 char *laddr1 = xstrdup(""), *laddr2 = NULL;
Darren Tuckere7140f22008-06-10 23:01:51 +10002118
2119 /* these are usually at the top of the config */
2120 for (i = 0; i < o->num_ports; i++)
2121 printf("port %d\n", o->ports[i]);
2122 dump_cfg_fmtint(sProtocol, o->protocol);
2123 dump_cfg_fmtint(sAddressFamily, o->address_family);
2124
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002125 /*
2126 * ListenAddress must be after Port. add_one_listen_addr pushes
2127 * addresses onto a stack, so to maintain ordering we need to
2128 * print these in reverse order.
2129 */
Darren Tuckere7140f22008-06-10 23:01:51 +10002130 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
2131 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
2132 sizeof(addr), port, sizeof(port),
2133 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
2134 error("getnameinfo failed: %.100s",
2135 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
2136 strerror(errno));
2137 } else {
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002138 laddr2 = laddr1;
Darren Tuckere7140f22008-06-10 23:01:51 +10002139 if (ai->ai_family == AF_INET6)
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002140 xasprintf(&laddr1, "listenaddress [%s]:%s\n%s",
2141 addr, port, laddr2);
Darren Tuckere7140f22008-06-10 23:01:51 +10002142 else
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002143 xasprintf(&laddr1, "listenaddress %s:%s\n%s",
2144 addr, port, laddr2);
2145 free(laddr2);
Darren Tuckere7140f22008-06-10 23:01:51 +10002146 }
2147 }
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002148 printf("%s", laddr1);
2149 free(laddr1);
Darren Tuckere7140f22008-06-10 23:01:51 +10002150
2151 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10002152#ifdef USE_PAM
Darren Tucker70860b62015-04-17 10:56:13 +10002153 dump_cfg_fmtint(sUsePAM, o->use_pam);
Damien Miller212f0b02008-07-23 17:42:29 +10002154#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002155 dump_cfg_int(sServerKeyBits, o->server_key_bits);
2156 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
2157 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
2158 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
2159 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11002160 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10002161 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
2162 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002163 dump_cfg_oct(sStreamLocalBindMask, o->fwd_opts.streamlocal_bind_mask);
Darren Tuckere7140f22008-06-10 23:01:51 +10002164
2165 /* formatted integer arguments */
2166 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
2167 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
2168 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
2169 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2170 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
2171 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
2172 o->hostbased_uses_name_from_packet_only);
2173 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
2174 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10002175#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10002176 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
2177 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
2178 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10002179# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10002180 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10002181# endif
2182#endif
2183#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10002184 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2185 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10002186#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002187 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2188 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2189 o->kbd_interactive_authentication);
2190 dump_cfg_fmtint(sChallengeResponseAuthentication,
2191 o->challenge_response_authentication);
2192 dump_cfg_fmtint(sPrintMotd, o->print_motd);
2193 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
2194 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
2195 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002196 dump_cfg_fmtint(sPermitTTY, o->permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002197 dump_cfg_fmtint(sPermitUserRC, o->permit_user_rc);
Darren Tuckere7140f22008-06-10 23:01:51 +10002198 dump_cfg_fmtint(sStrictModes, o->strict_modes);
2199 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
2200 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
2201 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
2202 dump_cfg_fmtint(sUseLogin, o->use_login);
2203 dump_cfg_fmtint(sCompression, o->compression);
Damien Miller7acefbb2014-07-18 14:11:24 +10002204 dump_cfg_fmtint(sGatewayPorts, o->fwd_opts.gateway_ports);
Darren Tuckere7140f22008-06-10 23:01:51 +10002205 dump_cfg_fmtint(sUseDNS, o->use_dns);
2206 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002207 dump_cfg_fmtint(sAllowAgentForwarding, o->allow_agent_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002208 dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
Darren Tuckere7140f22008-06-10 23:01:51 +10002209 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002210 dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
Darren Tuckere7140f22008-06-10 23:01:51 +10002211
2212 /* string arguments */
2213 dump_cfg_string(sPidFile, o->pid_file);
2214 dump_cfg_string(sXAuthLocation, o->xauth_location);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002215 dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
2216 dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
Darren Tuckere7140f22008-06-10 23:01:51 +10002217 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10002218 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11002219 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11002220 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
2221 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10002222 dump_cfg_string(sAuthorizedPrincipalsFile,
2223 o->authorized_principals_file);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002224 dump_cfg_string(sVersionAddendum, *o->version_addendum == '\0'
2225 ? "none" : o->version_addendum);
Damien Miller09d3e122012-10-31 08:58:58 +11002226 dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
2227 dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
Damien Miller85b45e02013-07-20 13:21:52 +10002228 dump_cfg_string(sHostKeyAgent, o->host_key_agent);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002229 dump_cfg_string(sKexAlgorithms,
djm@openbsd.org259a02e2014-10-13 00:38:35 +00002230 o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002231 dump_cfg_string(sHostbasedAcceptedKeyTypes, o->hostbased_key_types ?
2232 o->hostbased_key_types : KEX_DEFAULT_PK_ALG);
2233 dump_cfg_string(sPubkeyAcceptedKeyTypes, o->pubkey_key_types ?
2234 o->pubkey_key_types : KEX_DEFAULT_PK_ALG);
Darren Tuckere7140f22008-06-10 23:01:51 +10002235
2236 /* string arguments requiring a lookup */
2237 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
2238 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
2239
2240 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10002241 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
2242 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002243 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
2244 o->host_key_files);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002245 dump_cfg_strarray(sHostCertificate, o->num_host_cert_files,
Damien Miller0a80ca12010-02-27 07:55:05 +11002246 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002247 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
2248 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
2249 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
2250 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
2251 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
Damien Millera6e3f012012-11-04 23:21:40 +11002252 dump_cfg_strarray_oneline(sAuthenticationMethods,
2253 o->num_auth_methods, o->auth_methods);
Darren Tuckere7140f22008-06-10 23:01:51 +10002254
2255 /* other arguments */
2256 for (i = 0; i < o->num_subsystems; i++)
2257 printf("subsystem %s %s\n", o->subsystem_name[i],
2258 o->subsystem_args[i]);
2259
2260 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
2261 o->max_startups_rate, o->max_startups);
2262
2263 for (i = 0; tunmode_desc[i].val != -1; i++)
2264 if (tunmode_desc[i].val == o->permit_tun) {
2265 s = tunmode_desc[i].text;
2266 break;
2267 }
2268 dump_cfg_string(sPermitTunnel, s);
2269
Damien Miller91475862011-05-05 14:14:34 +10002270 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2271 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11002272
Damien Millera6d6c1f2013-08-21 02:40:01 +10002273 printf("rekeylimit %lld %d\n", (long long)o->rekey_limit,
2274 o->rekey_interval);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002275
Darren Tuckere7140f22008-06-10 23:01:51 +10002276 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10002277}