blob: 5606c3c7f24b461da507fdcc4cd1b91db9380737 [file] [log] [blame]
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001/* $OpenBSD: servconf.c,v 1.268 2015/05/01 07:08:08 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);
djm@openbsd.org7e8528c2015-05-01 04:17:51 +0000355 CLEAR_ON_NONE(options->authorized_principals_file);
djm@openbsd.org161cf412014-12-22 07:55:51 +0000356 for (i = 0; i < options->num_host_key_files; i++)
357 CLEAR_ON_NONE(options->host_key_files[i]);
358 for (i = 0; i < options->num_host_cert_files; i++)
359 CLEAR_ON_NONE(options->host_cert_files[i]);
360#undef CLEAR_ON_NONE
361
Tim Rice40017b02002-07-14 13:36:49 -0700362#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000363 if (use_privsep && options->compression == 1) {
364 error("This platform does not support both privilege "
365 "separation and compression");
366 error("Compression disabled");
367 options->compression = 0;
368 }
369#endif
370
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371}
372
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100374typedef enum {
375 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100376 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000377 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100378 /* Standard Options */
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000379 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime,
380 sKeyRegenerationTime, sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000381 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100382 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100383 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000384 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100385 sPasswordAuthentication, sKbdInteractiveAuthentication,
386 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000387 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100388 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller5ff30c62013-10-30 22:21:50 +1100389 sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000390 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000391 sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000392 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000393 sGatewayPorts, sPubkeyAuthentication, sPubkeyAcceptedKeyTypes,
394 sXAuthLocation, sSubsystem, sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000395 sBanner, sUseDNS, sHostbasedAuthentication,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000396 sHostbasedUsesNameFromPacketOnly, sHostbasedAcceptedKeyTypes,
397 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
Damien Millerd27b9472005-12-13 19:29:02 +1100398 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100399 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100400 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller7cc194f2014-02-04 11:12:56 +1100401 sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000402 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Miller23528812012-04-22 11:24:43 +1000403 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Miller09d3e122012-10-31 08:58:58 +1100404 sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
Damien Miller72e6b5c2014-07-04 09:00:04 +1000405 sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
Damien Miller7acefbb2014-07-18 14:11:24 +1000406 sStreamLocalBindMask, sStreamLocalBindUnlink,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000407 sAllowStreamLocalForwarding, sFingerprintHash,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000408 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409} ServerOpCodes;
410
Darren Tucker45150472006-07-12 22:34:17 +1000411#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
412#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
413#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
414
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100416static struct {
417 const char *name;
418 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000419 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100420} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100421 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000422#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000423 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000424#else
Darren Tucker45150472006-07-12 22:34:17 +1000425 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000426#endif
Darren Tucker45150472006-07-12 22:34:17 +1000427 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100428 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000429 { "port", sPort, SSHCFG_GLOBAL },
430 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
431 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
Damien Miller85b45e02013-07-20 13:21:52 +1000432 { "hostkeyagent", sHostKeyAgent, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000433 { "pidfile", sPidFile, SSHCFG_GLOBAL },
434 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
435 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
436 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100437 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000438 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
439 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
440 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100441 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
442 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000443 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000444 { "hostbasedacceptedkeytypes", sHostbasedAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100445 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
446 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000447 { "pubkeyacceptedkeytypes", sPubkeyAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000448 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000449#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100450 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000451 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
452 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100453#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000454 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000455#else
Darren Tucker45150472006-07-12 22:34:17 +1000456 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100457#endif
458#else
Darren Tucker1629c072007-02-19 22:25:37 +1100459 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000460 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
461 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
462 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000463#endif
Darren Tucker45150472006-07-12 22:34:17 +1000464 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
465 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000466#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100467 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000468 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000469#else
Darren Tucker1629c072007-02-19 22:25:37 +1100470 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000471 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000472#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100473 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
474 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100475 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000476 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
477 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
478 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
479 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
480 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
481 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
482 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
483 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000484 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
485 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
486 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000487 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
488 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100489 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000490 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
491 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
492 { "compression", sCompression, SSHCFG_GLOBAL },
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000493 { "rekeylimit", sRekeyLimit, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000494 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
495 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
496 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000497 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Damien Millerc24da772012-06-20 21:53:58 +1000498 { "allowusers", sAllowUsers, SSHCFG_ALL },
499 { "denyusers", sDenyUsers, SSHCFG_ALL },
500 { "allowgroups", sAllowGroups, SSHCFG_ALL },
501 { "denygroups", sDenyGroups, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000502 { "ciphers", sCiphers, SSHCFG_GLOBAL },
503 { "macs", sMacs, SSHCFG_GLOBAL },
504 { "protocol", sProtocol, SSHCFG_GLOBAL },
505 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
506 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
507 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000508 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000509 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100510 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000511 { "usedns", sUseDNS, SSHCFG_GLOBAL },
512 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
513 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
514 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
515 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000516 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000517 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000518 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Damien Millerc24da772012-06-20 21:53:58 +1000519 { "acceptenv", sAcceptEnv, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000520 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Damien Miller5ff30c62013-10-30 22:21:50 +1100521 { "permittty", sPermitTTY, SSHCFG_ALL },
Damien Miller72e6b5c2014-07-04 09:00:04 +1000522 { "permituserrc", sPermitUserRC, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000523 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000524 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000525 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100526 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100527 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100528 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
529 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000530 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000531 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100532 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller09d3e122012-10-31 08:58:58 +1100533 { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
534 { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000535 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Damien Millera6e3f012012-11-04 23:21:40 +1100536 { "authenticationmethods", sAuthenticationMethods, SSHCFG_ALL },
Damien Miller7acefbb2014-07-18 14:11:24 +1000537 { "streamlocalbindmask", sStreamLocalBindMask, SSHCFG_ALL },
538 { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
539 { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000540 { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000541 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000542};
543
Darren Tuckere7140f22008-06-10 23:01:51 +1000544static struct {
545 int val;
546 char *text;
547} tunmode_desc[] = {
548 { SSH_TUNMODE_NO, "no" },
549 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
550 { SSH_TUNMODE_ETHERNET, "ethernet" },
551 { SSH_TUNMODE_YES, "yes" },
552 { -1, NULL }
553};
554
Damien Miller5428f641999-11-25 11:54:57 +1100555/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000556 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100557 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558
Damien Miller4af51302000-04-16 11:18:38 +1000559static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100560parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000561 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000563 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564
Damien Miller95def091999-11-25 00:26:21 +1100565 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000566 if (strcasecmp(cp, keywords[i].name) == 0) {
567 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100568 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000569 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000571 error("%s: line %d: Bad configuration option: %s",
572 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100573 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574}
575
Darren Tucker88b6fb22010-01-13 22:44:29 +1100576char *
577derelativise_path(const char *path)
578{
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000579 char *expanded, *ret, cwd[PATH_MAX];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100580
djm@openbsd.org161cf412014-12-22 07:55:51 +0000581 if (strcasecmp(path, "none") == 0)
582 return xstrdup("none");
Darren Tucker88b6fb22010-01-13 22:44:29 +1100583 expanded = tilde_expand_filename(path, getuid());
584 if (*expanded == '/')
585 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100586 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100587 fatal("%s: getcwd: %s", __func__, strerror(errno));
588 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tuckera627d422013-06-02 07:31:17 +1000589 free(expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100590 return ret;
591}
592
Ben Lindstrombba81212001-06-25 05:01:22 +0000593static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100594add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100595{
Damien Millereccb9de2005-06-17 12:59:34 +1000596 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100597
Ben Lindstrom19066a12001-04-12 23:39:26 +0000598 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000599 for (i = 0; i < options->num_ports; i++)
600 add_one_listen_addr(options, addr, options->ports[i]);
601 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000602 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000603}
604
Ben Lindstrombba81212001-06-25 05:01:22 +0000605static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100606add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000607{
608 struct addrinfo hints, *ai, *aitop;
609 char strport[NI_MAXSERV];
610 int gaierr;
611
612 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100613 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000614 hints.ai_socktype = SOCK_STREAM;
615 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100616 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000617 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
618 fatal("bad addr or host: %s (%s)",
619 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100620 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000621 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
622 ;
623 ai->ai_next = options->listen_addrs;
624 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100625}
626
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000627/*
628 * Queue a ListenAddress to be processed once we have all of the Ports
629 * and AddressFamily options.
630 */
631static void
632queue_listen_addr(ServerOptions *options, char *addr, int port)
633{
634 options->queued_listen_addrs = xreallocarray(
635 options->queued_listen_addrs, options->num_queued_listens + 1,
636 sizeof(addr));
637 options->queued_listen_ports = xreallocarray(
638 options->queued_listen_ports, options->num_queued_listens + 1,
639 sizeof(port));
640 options->queued_listen_addrs[options->num_queued_listens] =
641 xstrdup(addr);
642 options->queued_listen_ports[options->num_queued_listens] = port;
643 options->num_queued_listens++;
644}
645
646/*
647 * Process queued (text) ListenAddress entries.
648 */
649static void
650process_queued_listen_addrs(ServerOptions *options)
651{
652 u_int i;
653
654 if (options->num_ports == 0)
655 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
656 if (options->address_family == -1)
657 options->address_family = AF_UNSPEC;
658
659 for (i = 0; i < options->num_queued_listens; i++) {
660 add_listen_addr(options, options->queued_listen_addrs[i],
661 options->queued_listen_ports[i]);
662 free(options->queued_listen_addrs[i]);
663 options->queued_listen_addrs[i] = NULL;
664 }
665 free(options->queued_listen_addrs);
666 options->queued_listen_addrs = NULL;
667 free(options->queued_listen_ports);
668 options->queued_listen_ports = NULL;
669 options->num_queued_listens = 0;
670}
671
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000672struct connection_info *
673get_connection_info(int populate, int use_dns)
674{
675 static struct connection_info ci;
676
677 if (!populate)
678 return &ci;
679 ci.host = get_canonical_hostname(use_dns);
680 ci.address = get_remote_ipaddr();
681 ci.laddress = get_local_ipaddr(packet_get_connection_in());
682 ci.lport = get_local_port();
683 return &ci;
684}
685
Darren Tucker45150472006-07-12 22:34:17 +1000686/*
687 * The strategy for the Match blocks is that the config file is parsed twice.
688 *
689 * The first time is at startup. activep is initialized to 1 and the
690 * directives in the global context are processed and acted on. Hitting a
691 * Match directive unsets activep and the directives inside the block are
692 * checked for syntax only.
693 *
694 * The second time is after a connection has been established but before
695 * authentication. activep is initialized to 2 and global config directives
696 * are ignored since they have already been processed. If the criteria in a
697 * Match block is met, activep is set and the subsequent directives
698 * processed and actioned until EOF or another Match block unsets it. Any
699 * options set are copied into the main server config.
700 *
701 * Potential additions/improvements:
702 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
703 *
704 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
705 * Match Address 192.168.0.*
706 * Tag trusted
707 * Match Group wheel
708 * Tag trusted
709 * Match Tag trusted
710 * AllowTcpForwarding yes
711 * GatewayPorts clientspecified
712 * [...]
713 *
714 * - Add a PermittedChannelRequests directive
715 * Match Group shell
716 * PermittedChannelRequests session,forwarded-tcpip
717 */
718
719static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000720match_cfg_line_group(const char *grps, int line, const char *user)
721{
722 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000723 struct passwd *pw;
724
Damien Miller565ca3f2006-08-19 00:23:15 +1000725 if (user == NULL)
726 goto out;
727
728 if ((pw = getpwnam(user)) == NULL) {
729 debug("Can't match group at line %d because user %.100s does "
730 "not exist", line, user);
731 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
732 debug("Can't Match group because user %.100s not in any group "
733 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000734 } else if (ga_match_pattern_list(grps) != 1) {
735 debug("user %.100s does not match group list %.100s at line %d",
736 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000737 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000738 debug("user %.100s matched group list %.100s at line %d", user,
739 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000740 result = 1;
741 }
742out:
743 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000744 return result;
745}
746
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000747/*
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000748 * All of the attributes on a single Match line are ANDed together, so we need
Damien Miller03bf2e62013-10-24 21:01:26 +1100749 * to check every attribute and set the result to zero if any attribute does
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000750 * not match.
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000751 */
Damien Miller565ca3f2006-08-19 00:23:15 +1000752static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000753match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +1000754{
Damien Millercf31f382013-10-24 21:02:56 +1100755 int result = 1, attributes = 0, port;
Darren Tucker45150472006-07-12 22:34:17 +1000756 char *arg, *attrib, *cp = *condition;
Darren Tucker45150472006-07-12 22:34:17 +1000757
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000758 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000759 debug3("checking syntax for 'Match %s'", cp);
760 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000761 debug3("checking match for '%s' user %s host %s addr %s "
762 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
763 ci->host ? ci->host : "(null)",
764 ci->address ? ci->address : "(null)",
765 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +1000766
767 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100768 attributes++;
769 if (strcasecmp(attrib, "all") == 0) {
770 if (attributes != 1 ||
771 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
772 error("'all' cannot be combined with other "
773 "Match attributes");
774 return -1;
775 }
776 *condition = cp;
777 return 1;
778 }
Darren Tucker45150472006-07-12 22:34:17 +1000779 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
780 error("Missing Match criteria for %s", attrib);
781 return -1;
782 }
Darren Tucker45150472006-07-12 22:34:17 +1000783 if (strcasecmp(attrib, "user") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000784 if (ci == NULL || ci->user == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000785 result = 0;
786 continue;
787 }
djm@openbsd.orge661a862015-05-04 06:10:48 +0000788 if (match_pattern_list(ci->user, arg, 0) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000789 result = 0;
790 else
791 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000792 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000793 } else if (strcasecmp(attrib, "group") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000794 if (ci == NULL || ci->user == NULL) {
795 result = 0;
796 continue;
797 }
798 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +1000799 case -1:
800 return -1;
801 case 0:
802 result = 0;
803 }
Darren Tucker45150472006-07-12 22:34:17 +1000804 } else if (strcasecmp(attrib, "host") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000805 if (ci == NULL || ci->host == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000806 result = 0;
807 continue;
808 }
djm@openbsd.orge661a862015-05-04 06:10:48 +0000809 if (match_hostname(ci->host, arg) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000810 result = 0;
811 else
812 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000813 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +1000814 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000815 if (ci == NULL || ci->address == NULL) {
816 result = 0;
817 continue;
818 }
819 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000820 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000821 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000822 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000823 break;
824 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000825 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000826 result = 0;
827 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000828 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000829 return -1;
830 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000831 } else if (strcasecmp(attrib, "localaddress") == 0){
832 if (ci == NULL || ci->laddress == NULL) {
833 result = 0;
834 continue;
835 }
836 switch (addr_match_list(ci->laddress, arg)) {
837 case 1:
838 debug("connection from %.100s matched "
839 "'LocalAddress %.100s' at line %d",
840 ci->laddress, arg, line);
841 break;
842 case 0:
843 case -1:
844 result = 0;
845 break;
846 case -2:
847 return -1;
848 }
849 } else if (strcasecmp(attrib, "localport") == 0) {
850 if ((port = a2port(arg)) == -1) {
851 error("Invalid LocalPort '%s' on Match line",
852 arg);
853 return -1;
854 }
855 if (ci == NULL || ci->lport == 0) {
856 result = 0;
857 continue;
858 }
859 /* TODO support port lists */
860 if (port == ci->lport)
861 debug("connection from %.100s matched "
862 "'LocalPort %d' at line %d",
863 ci->laddress, port, line);
864 else
865 result = 0;
Darren Tucker45150472006-07-12 22:34:17 +1000866 } else {
867 error("Unsupported Match attribute %s", attrib);
868 return -1;
869 }
870 }
Damien Millercf31f382013-10-24 21:02:56 +1100871 if (attributes == 0) {
872 error("One or more attributes required for Match");
873 return -1;
874 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000875 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000876 debug3("match %sfound", result ? "" : "not ");
877 *condition = cp;
878 return result;
879}
880
Damien Millere2754432006-07-24 14:06:47 +1000881#define WHITESPACE " \t\r\n"
882
Damien Miller33322122011-06-20 14:43:11 +1000883/* Multistate option parsing */
884struct multistate {
885 char *key;
886 int value;
887};
888static const struct multistate multistate_addressfamily[] = {
889 { "inet", AF_INET },
890 { "inet6", AF_INET6 },
891 { "any", AF_UNSPEC },
892 { NULL, -1 }
893};
894static const struct multistate multistate_permitrootlogin[] = {
895 { "without-password", PERMIT_NO_PASSWD },
896 { "forced-commands-only", PERMIT_FORCED_ONLY },
897 { "yes", PERMIT_YES },
898 { "no", PERMIT_NO },
899 { NULL, -1 }
900};
901static const struct multistate multistate_compression[] = {
902 { "delayed", COMP_DELAYED },
903 { "yes", COMP_ZLIB },
904 { "no", COMP_NONE },
905 { NULL, -1 }
906};
907static const struct multistate multistate_gatewayports[] = {
908 { "clientspecified", 2 },
909 { "yes", 1 },
910 { "no", 0 },
911 { NULL, -1 }
912};
Damien Miller69ff1df2011-06-23 08:30:03 +1000913static const struct multistate multistate_privsep[] = {
Damien Miller5a5c2b92012-07-31 12:21:34 +1000914 { "yes", PRIVSEP_NOSANDBOX },
915 { "sandbox", PRIVSEP_ON },
916 { "nosandbox", PRIVSEP_NOSANDBOX },
Damien Miller69ff1df2011-06-23 08:30:03 +1000917 { "no", PRIVSEP_OFF },
918 { NULL, -1 }
919};
Damien Milleraa5b3f82012-12-03 09:50:54 +1100920static const struct multistate multistate_tcpfwd[] = {
921 { "yes", FORWARD_ALLOW },
922 { "all", FORWARD_ALLOW },
923 { "no", FORWARD_DENY },
924 { "remote", FORWARD_REMOTE },
925 { "local", FORWARD_LOCAL },
926 { NULL, -1 }
927};
Damien Miller33322122011-06-20 14:43:11 +1000928
Ben Lindstromade03f62001-12-06 18:22:17 +0000929int
930process_server_config_line(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000931 const char *filename, int linenum, int *activep,
932 struct connection_info *connectinfo)
Ben Lindstromade03f62001-12-06 18:22:17 +0000933{
Darren Tucker09c0f032013-05-16 20:48:57 +1000934 char *cp, **charptr, *arg, *p;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000935 int cmdline = 0, *intptr, value, value2, n, port;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100936 SyslogFacility *log_facility_ptr;
937 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000938 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000939 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000940 size_t len;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000941 long long val64;
Damien Miller33322122011-06-20 14:43:11 +1000942 const struct multistate *multistate_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000943
944 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100945 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100946 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000947 /* Ignore leading whitespace */
948 if (*arg == '\0')
949 arg = strdelim(&cp);
950 if (!arg || !*arg || *arg == '#')
951 return 0;
952 intptr = NULL;
953 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000954 opcode = parse_token(arg, filename, linenum, &flags);
955
956 if (activep == NULL) { /* We are processing a command line directive */
957 cmdline = 1;
958 activep = &cmdline;
959 }
960 if (*activep && opcode != sMatch)
961 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
962 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000963 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000964 fatal("%s line %d: Directive '%s' is not allowed "
965 "within a Match block", filename, linenum, arg);
966 } else { /* this is a directive we have already processed */
967 while (arg)
968 arg = strdelim(&cp);
969 return 0;
970 }
971 }
972
Ben Lindstromade03f62001-12-06 18:22:17 +0000973 switch (opcode) {
974 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000975 case sUsePAM:
976 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000977 goto parse_flag;
978
979 /* Standard Options */
980 case sBadOption:
981 return -1;
982 case sPort:
983 /* ignore ports from configfile if cmdline specifies ports */
984 if (options->ports_from_cmdline)
985 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000986 if (options->num_ports >= MAX_PORTS)
987 fatal("%s line %d: too many ports.",
988 filename, linenum);
989 arg = strdelim(&cp);
990 if (!arg || *arg == '\0')
991 fatal("%s line %d: missing port number.",
992 filename, linenum);
993 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100994 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000995 fatal("%s line %d: Badly formatted port number.",
996 filename, linenum);
997 break;
998
999 case sServerKeyBits:
1000 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +10001001 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +00001002 arg = strdelim(&cp);
1003 if (!arg || *arg == '\0')
1004 fatal("%s line %d: missing integer value.",
1005 filename, linenum);
1006 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +10001007 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001008 *intptr = value;
1009 break;
1010
1011 case sLoginGraceTime:
1012 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +10001013 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +00001014 arg = strdelim(&cp);
1015 if (!arg || *arg == '\0')
1016 fatal("%s line %d: missing time value.",
1017 filename, linenum);
1018 if ((value = convtime(arg)) == -1)
1019 fatal("%s line %d: invalid time value.",
1020 filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001021 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001022 *intptr = value;
1023 break;
1024
1025 case sKeyRegenerationTime:
1026 intptr = &options->key_regeneration_time;
1027 goto parse_time;
1028
1029 case sListenAddress:
1030 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001031 if (arg == NULL || *arg == '\0')
1032 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +00001033 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +10001034 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
1035 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
1036 && strchr(p+1, ':') != NULL) {
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00001037 queue_listen_addr(options, arg, 0);
Damien Miller203c7052005-08-12 22:11:37 +10001038 break;
1039 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001040 p = hpdelim(&arg);
1041 if (p == NULL)
1042 fatal("%s line %d: bad address:port usage",
1043 filename, linenum);
1044 p = cleanhostname(p);
1045 if (arg == NULL)
1046 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001047 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001048 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001049
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00001050 queue_listen_addr(options, p, port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001051
Ben Lindstromade03f62001-12-06 18:22:17 +00001052 break;
1053
Darren Tucker0f383232005-01-20 10:57:56 +11001054 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +10001055 intptr = &options->address_family;
1056 multistate_ptr = multistate_addressfamily;
Damien Miller33322122011-06-20 14:43:11 +10001057 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +11001058 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +10001059 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +10001060 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +10001061 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +10001062 value = -1;
1063 for (i = 0; multistate_ptr[i].key != NULL; i++) {
1064 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
1065 value = multistate_ptr[i].value;
1066 break;
1067 }
1068 }
1069 if (value == -1)
1070 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +11001071 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +10001072 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +11001073 *intptr = value;
1074 break;
1075
Ben Lindstromade03f62001-12-06 18:22:17 +00001076 case sHostKeyFile:
1077 intptr = &options->num_host_key_files;
1078 if (*intptr >= MAX_HOSTKEYS)
1079 fatal("%s line %d: too many host keys specified (max %d).",
1080 filename, linenum, MAX_HOSTKEYS);
1081 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +10001082 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +00001083 arg = strdelim(&cp);
1084 if (!arg || *arg == '\0')
1085 fatal("%s line %d: missing file name.",
1086 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001087 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +11001088 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001089 /* increase optional counter */
1090 if (intptr != NULL)
1091 *intptr = *intptr + 1;
1092 }
1093 break;
1094
Damien Miller85b45e02013-07-20 13:21:52 +10001095 case sHostKeyAgent:
1096 charptr = &options->host_key_agent;
1097 arg = strdelim(&cp);
1098 if (!arg || *arg == '\0')
1099 fatal("%s line %d: missing socket name.",
1100 filename, linenum);
1101 if (*activep && *charptr == NULL)
1102 *charptr = !strcmp(arg, SSH_AUTHSOCKET_ENV_NAME) ?
1103 xstrdup(arg) : derelativise_path(arg);
1104 break;
1105
Damien Miller0a80ca12010-02-27 07:55:05 +11001106 case sHostCertificate:
1107 intptr = &options->num_host_cert_files;
1108 if (*intptr >= MAX_HOSTKEYS)
1109 fatal("%s line %d: too many host certificates "
1110 "specified (max %d).", filename, linenum,
1111 MAX_HOSTCERTS);
1112 charptr = &options->host_cert_files[*intptr];
1113 goto parse_filename;
1114 break;
1115
Ben Lindstromade03f62001-12-06 18:22:17 +00001116 case sPidFile:
1117 charptr = &options->pid_file;
1118 goto parse_filename;
1119
1120 case sPermitRootLogin:
1121 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +10001122 multistate_ptr = multistate_permitrootlogin;
1123 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001124
1125 case sIgnoreRhosts:
1126 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +10001127 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +00001128 arg = strdelim(&cp);
1129 if (!arg || *arg == '\0')
1130 fatal("%s line %d: missing yes/no argument.",
1131 filename, linenum);
1132 value = 0; /* silence compiler */
1133 if (strcmp(arg, "yes") == 0)
1134 value = 1;
1135 else if (strcmp(arg, "no") == 0)
1136 value = 0;
1137 else
1138 fatal("%s line %d: Bad yes/no argument: %s",
1139 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +10001140 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001141 *intptr = value;
1142 break;
1143
1144 case sIgnoreUserKnownHosts:
1145 intptr = &options->ignore_user_known_hosts;
1146 goto parse_flag;
1147
Ben Lindstromade03f62001-12-06 18:22:17 +00001148 case sRhostsRSAAuthentication:
1149 intptr = &options->rhosts_rsa_authentication;
1150 goto parse_flag;
1151
1152 case sHostbasedAuthentication:
1153 intptr = &options->hostbased_authentication;
1154 goto parse_flag;
1155
1156 case sHostbasedUsesNameFromPacketOnly:
1157 intptr = &options->hostbased_uses_name_from_packet_only;
1158 goto parse_flag;
1159
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001160 case sHostbasedAcceptedKeyTypes:
1161 charptr = &options->hostbased_key_types;
1162 parse_keytypes:
1163 arg = strdelim(&cp);
1164 if (!arg || *arg == '\0')
1165 fatal("%s line %d: Missing argument.",
1166 filename, linenum);
1167 if (!sshkey_names_valid2(arg, 1))
1168 fatal("%s line %d: Bad key types '%s'.",
1169 filename, linenum, arg ? arg : "<NONE>");
1170 if (*activep && *charptr == NULL)
1171 *charptr = xstrdup(arg);
1172 break;
1173
Ben Lindstromade03f62001-12-06 18:22:17 +00001174 case sRSAAuthentication:
1175 intptr = &options->rsa_authentication;
1176 goto parse_flag;
1177
1178 case sPubkeyAuthentication:
1179 intptr = &options->pubkey_authentication;
1180 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001181
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001182 case sPubkeyAcceptedKeyTypes:
1183 charptr = &options->pubkey_key_types;
1184 goto parse_keytypes;
1185
Ben Lindstromade03f62001-12-06 18:22:17 +00001186 case sKerberosAuthentication:
1187 intptr = &options->kerberos_authentication;
1188 goto parse_flag;
1189
1190 case sKerberosOrLocalPasswd:
1191 intptr = &options->kerberos_or_local_passwd;
1192 goto parse_flag;
1193
1194 case sKerberosTicketCleanup:
1195 intptr = &options->kerberos_ticket_cleanup;
1196 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001197
Darren Tucker22ef5082003-12-31 11:37:34 +11001198 case sKerberosGetAFSToken:
1199 intptr = &options->kerberos_get_afs_token;
1200 goto parse_flag;
1201
Darren Tucker0efd1552003-08-26 11:49:55 +10001202 case sGssAuthentication:
1203 intptr = &options->gss_authentication;
1204 goto parse_flag;
1205
1206 case sGssCleanupCreds:
1207 intptr = &options->gss_cleanup_creds;
1208 goto parse_flag;
1209
Ben Lindstromade03f62001-12-06 18:22:17 +00001210 case sPasswordAuthentication:
1211 intptr = &options->password_authentication;
1212 goto parse_flag;
1213
1214 case sKbdInteractiveAuthentication:
1215 intptr = &options->kbd_interactive_authentication;
1216 goto parse_flag;
1217
1218 case sChallengeResponseAuthentication:
1219 intptr = &options->challenge_response_authentication;
1220 goto parse_flag;
1221
1222 case sPrintMotd:
1223 intptr = &options->print_motd;
1224 goto parse_flag;
1225
1226 case sPrintLastLog:
1227 intptr = &options->print_lastlog;
1228 goto parse_flag;
1229
1230 case sX11Forwarding:
1231 intptr = &options->x11_forwarding;
1232 goto parse_flag;
1233
1234 case sX11DisplayOffset:
1235 intptr = &options->x11_display_offset;
1236 goto parse_int;
1237
Damien Miller95c249f2002-02-05 12:11:34 +11001238 case sX11UseLocalhost:
1239 intptr = &options->x11_use_localhost;
1240 goto parse_flag;
1241
Ben Lindstromade03f62001-12-06 18:22:17 +00001242 case sXAuthLocation:
1243 charptr = &options->xauth_location;
1244 goto parse_filename;
1245
Damien Miller5ff30c62013-10-30 22:21:50 +11001246 case sPermitTTY:
1247 intptr = &options->permit_tty;
1248 goto parse_flag;
1249
Damien Miller72e6b5c2014-07-04 09:00:04 +10001250 case sPermitUserRC:
1251 intptr = &options->permit_user_rc;
1252 goto parse_flag;
1253
Ben Lindstromade03f62001-12-06 18:22:17 +00001254 case sStrictModes:
1255 intptr = &options->strict_modes;
1256 goto parse_flag;
1257
Damien Miller12c150e2003-12-17 16:31:10 +11001258 case sTCPKeepAlive:
1259 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001260 goto parse_flag;
1261
1262 case sEmptyPasswd:
1263 intptr = &options->permit_empty_passwd;
1264 goto parse_flag;
1265
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001266 case sPermitUserEnvironment:
1267 intptr = &options->permit_user_env;
1268 goto parse_flag;
1269
Ben Lindstromade03f62001-12-06 18:22:17 +00001270 case sUseLogin:
1271 intptr = &options->use_login;
1272 goto parse_flag;
1273
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001274 case sCompression:
1275 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001276 multistate_ptr = multistate_compression;
1277 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001278
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001279 case sRekeyLimit:
1280 arg = strdelim(&cp);
1281 if (!arg || *arg == '\0')
1282 fatal("%.200s line %d: Missing argument.", filename,
1283 linenum);
1284 if (strcmp(arg, "default") == 0) {
1285 val64 = 0;
1286 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001287 if (scan_scaled(arg, &val64) == -1)
1288 fatal("%.200s line %d: Bad number '%s': %s",
1289 filename, linenum, arg, strerror(errno));
1290 /* check for too-large or too-small limits */
1291 if (val64 > UINT_MAX)
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001292 fatal("%.200s line %d: RekeyLimit too large",
1293 filename, linenum);
1294 if (val64 != 0 && val64 < 16)
1295 fatal("%.200s line %d: RekeyLimit too small",
1296 filename, linenum);
1297 }
1298 if (*activep && options->rekey_limit == -1)
1299 options->rekey_limit = (u_int32_t)val64;
1300 if (cp != NULL) { /* optional rekey interval present */
1301 if (strcmp(cp, "none") == 0) {
1302 (void)strdelim(&cp); /* discard */
1303 break;
1304 }
1305 intptr = &options->rekey_interval;
1306 goto parse_time;
1307 }
1308 break;
1309
Ben Lindstromade03f62001-12-06 18:22:17 +00001310 case sGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +10001311 intptr = &options->fwd_opts.gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001312 multistate_ptr = multistate_gatewayports;
1313 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001314
Damien Miller3a961dc2003-06-03 10:25:48 +10001315 case sUseDNS:
1316 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001317 goto parse_flag;
1318
1319 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001320 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001321 arg = strdelim(&cp);
1322 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001323 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001324 fatal("%.200s line %d: unsupported log facility '%s'",
1325 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001326 if (*log_facility_ptr == -1)
1327 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001328 break;
1329
1330 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001331 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001332 arg = strdelim(&cp);
1333 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001334 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001335 fatal("%.200s line %d: unsupported log level '%s'",
1336 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001337 if (*log_level_ptr == -1)
1338 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001339 break;
1340
1341 case sAllowTcpForwarding:
1342 intptr = &options->allow_tcp_forwarding;
Damien Milleraa5b3f82012-12-03 09:50:54 +11001343 multistate_ptr = multistate_tcpfwd;
1344 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001345
Damien Miller7acefbb2014-07-18 14:11:24 +10001346 case sAllowStreamLocalForwarding:
1347 intptr = &options->allow_streamlocal_forwarding;
1348 multistate_ptr = multistate_tcpfwd;
1349 goto parse_multistate;
1350
Damien Miller4f755cd2008-05-19 14:57:41 +10001351 case sAllowAgentForwarding:
1352 intptr = &options->allow_agent_forwarding;
1353 goto parse_flag;
1354
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001355 case sUsePrivilegeSeparation:
1356 intptr = &use_privsep;
Damien Miller69ff1df2011-06-23 08:30:03 +10001357 multistate_ptr = multistate_privsep;
1358 goto parse_multistate;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001359
Ben Lindstromade03f62001-12-06 18:22:17 +00001360 case sAllowUsers:
1361 while ((arg = strdelim(&cp)) && *arg != '\0') {
1362 if (options->num_allow_users >= MAX_ALLOW_USERS)
1363 fatal("%s line %d: too many allow users.",
1364 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001365 if (!*activep)
1366 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001367 options->allow_users[options->num_allow_users++] =
1368 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001369 }
1370 break;
1371
1372 case sDenyUsers:
1373 while ((arg = strdelim(&cp)) && *arg != '\0') {
1374 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001375 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001376 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001377 if (!*activep)
1378 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001379 options->deny_users[options->num_deny_users++] =
1380 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001381 }
1382 break;
1383
1384 case sAllowGroups:
1385 while ((arg = strdelim(&cp)) && *arg != '\0') {
1386 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1387 fatal("%s line %d: too many allow groups.",
1388 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001389 if (!*activep)
1390 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001391 options->allow_groups[options->num_allow_groups++] =
1392 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001393 }
1394 break;
1395
1396 case sDenyGroups:
1397 while ((arg = strdelim(&cp)) && *arg != '\0') {
1398 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1399 fatal("%s line %d: too many deny groups.",
1400 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001401 if (!*activep)
1402 continue;
1403 options->deny_groups[options->num_deny_groups++] =
1404 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001405 }
1406 break;
1407
1408 case sCiphers:
1409 arg = strdelim(&cp);
1410 if (!arg || *arg == '\0')
1411 fatal("%s line %d: Missing argument.", filename, linenum);
1412 if (!ciphers_valid(arg))
1413 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1414 filename, linenum, arg ? arg : "<NONE>");
1415 if (options->ciphers == NULL)
1416 options->ciphers = xstrdup(arg);
1417 break;
1418
1419 case sMacs:
1420 arg = strdelim(&cp);
1421 if (!arg || *arg == '\0')
1422 fatal("%s line %d: Missing argument.", filename, linenum);
1423 if (!mac_valid(arg))
1424 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1425 filename, linenum, arg ? arg : "<NONE>");
1426 if (options->macs == NULL)
1427 options->macs = xstrdup(arg);
1428 break;
1429
Damien Millerd5f62bf2010-09-24 22:11:14 +10001430 case sKexAlgorithms:
1431 arg = strdelim(&cp);
1432 if (!arg || *arg == '\0')
1433 fatal("%s line %d: Missing argument.",
1434 filename, linenum);
1435 if (!kex_names_valid(arg))
1436 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1437 filename, linenum, arg ? arg : "<NONE>");
1438 if (options->kex_algorithms == NULL)
1439 options->kex_algorithms = xstrdup(arg);
1440 break;
1441
Ben Lindstromade03f62001-12-06 18:22:17 +00001442 case sProtocol:
1443 intptr = &options->protocol;
1444 arg = strdelim(&cp);
1445 if (!arg || *arg == '\0')
1446 fatal("%s line %d: Missing argument.", filename, linenum);
1447 value = proto_spec(arg);
1448 if (value == SSH_PROTO_UNKNOWN)
1449 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001450 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001451 if (*intptr == SSH_PROTO_UNKNOWN)
1452 *intptr = value;
1453 break;
1454
1455 case sSubsystem:
1456 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1457 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001458 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001459 }
1460 arg = strdelim(&cp);
1461 if (!arg || *arg == '\0')
1462 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001463 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001464 if (!*activep) {
1465 arg = strdelim(&cp);
1466 break;
1467 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001468 for (i = 0; i < options->num_subsystems; i++)
1469 if (strcmp(arg, options->subsystem_name[i]) == 0)
1470 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001471 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001472 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1473 arg = strdelim(&cp);
1474 if (!arg || *arg == '\0')
1475 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001476 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001477 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001478
1479 /* Collect arguments (separate to executable) */
1480 p = xstrdup(arg);
1481 len = strlen(p) + 1;
1482 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1483 len += 1 + strlen(arg);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001484 p = xreallocarray(p, 1, len);
Damien Miller917f9b62006-07-10 20:36:47 +10001485 strlcat(p, " ", len);
1486 strlcat(p, arg, len);
1487 }
1488 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001489 options->num_subsystems++;
1490 break;
1491
1492 case sMaxStartups:
1493 arg = strdelim(&cp);
1494 if (!arg || *arg == '\0')
1495 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001496 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001497 if ((n = sscanf(arg, "%d:%d:%d",
1498 &options->max_startups_begin,
1499 &options->max_startups_rate,
1500 &options->max_startups)) == 3) {
1501 if (options->max_startups_begin >
1502 options->max_startups ||
1503 options->max_startups_rate > 100 ||
1504 options->max_startups_rate < 1)
1505 fatal("%s line %d: Illegal MaxStartups spec.",
1506 filename, linenum);
1507 } else if (n != 1)
1508 fatal("%s line %d: Illegal MaxStartups spec.",
1509 filename, linenum);
1510 else
1511 options->max_startups = options->max_startups_begin;
1512 break;
1513
Darren Tucker89413db2004-05-24 10:36:23 +10001514 case sMaxAuthTries:
1515 intptr = &options->max_authtries;
1516 goto parse_int;
1517
Damien Miller7207f642008-05-19 15:34:50 +10001518 case sMaxSessions:
1519 intptr = &options->max_sessions;
1520 goto parse_int;
1521
Ben Lindstromade03f62001-12-06 18:22:17 +00001522 case sBanner:
1523 charptr = &options->banner;
1524 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001525
Ben Lindstromade03f62001-12-06 18:22:17 +00001526 /*
1527 * These options can contain %X options expanded at
1528 * connect time, so that you can specify paths like:
1529 *
1530 * AuthorizedKeysFile /etc/ssh_keys/%u
1531 */
1532 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001533 if (*activep && options->num_authkeys_files == 0) {
1534 while ((arg = strdelim(&cp)) && *arg != '\0') {
1535 if (options->num_authkeys_files >=
1536 MAX_AUTHKEYS_FILES)
1537 fatal("%s line %d: "
1538 "too many authorized keys files.",
1539 filename, linenum);
1540 options->authorized_keys_files[
1541 options->num_authkeys_files++] =
1542 tilde_expand_filename(arg, getuid());
1543 }
1544 }
1545 return 0;
1546
Damien Miller30da3442010-05-10 11:58:03 +10001547 case sAuthorizedPrincipalsFile:
1548 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001549 arg = strdelim(&cp);
1550 if (!arg || *arg == '\0')
1551 fatal("%s line %d: missing file name.",
1552 filename, linenum);
1553 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001554 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001555 /* increase optional counter */
1556 if (intptr != NULL)
1557 *intptr = *intptr + 1;
1558 }
1559 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001560
1561 case sClientAliveInterval:
1562 intptr = &options->client_alive_interval;
1563 goto parse_time;
1564
1565 case sClientAliveCountMax:
1566 intptr = &options->client_alive_count_max;
1567 goto parse_int;
1568
Darren Tucker46bc0752004-05-02 22:11:30 +10001569 case sAcceptEnv:
1570 while ((arg = strdelim(&cp)) && *arg != '\0') {
1571 if (strchr(arg, '=') != NULL)
1572 fatal("%s line %d: Invalid environment name.",
1573 filename, linenum);
1574 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1575 fatal("%s line %d: too many allow env.",
1576 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001577 if (!*activep)
Damien Millerc24da772012-06-20 21:53:58 +10001578 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001579 options->accept_env[options->num_accept_env++] =
1580 xstrdup(arg);
1581 }
1582 break;
1583
Damien Millerd27b9472005-12-13 19:29:02 +11001584 case sPermitTunnel:
1585 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001586 arg = strdelim(&cp);
1587 if (!arg || *arg == '\0')
1588 fatal("%s line %d: Missing yes/point-to-point/"
1589 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001590 value = -1;
1591 for (i = 0; tunmode_desc[i].val != -1; i++)
1592 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1593 value = tunmode_desc[i].val;
1594 break;
1595 }
1596 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001597 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1598 "no argument: %s", filename, linenum, arg);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001599 if (*activep && *intptr == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001600 *intptr = value;
1601 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001602
Darren Tucker45150472006-07-12 22:34:17 +10001603 case sMatch:
1604 if (cmdline)
1605 fatal("Match directive not supported as a command-line "
1606 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001607 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10001608 if (value < 0)
1609 fatal("%s line %d: Bad Match condition", filename,
1610 linenum);
1611 *activep = value;
1612 break;
1613
Damien Miller9b439df2006-07-24 14:04:00 +10001614 case sPermitOpen:
1615 arg = strdelim(&cp);
1616 if (!arg || *arg == '\0')
1617 fatal("%s line %d: missing PermitOpen specification",
1618 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001619 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001620 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001621 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001622 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001623 options->num_permitted_opens = 0;
1624 }
Damien Miller9b439df2006-07-24 14:04:00 +10001625 break;
1626 }
Damien Millerc6081482012-04-22 11:18:53 +10001627 if (strcmp(arg, "none") == 0) {
1628 if (*activep && n == -1) {
Damien Millerc6081482012-04-22 11:18:53 +10001629 options->num_permitted_opens = 1;
1630 channel_disable_adm_local_opens();
1631 }
1632 break;
1633 }
Damien Millera29b95e2007-01-05 16:28:36 +11001634 if (*activep && n == -1)
1635 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001636 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1637 p = hpdelim(&arg);
1638 if (p == NULL)
1639 fatal("%s line %d: missing host in PermitOpen",
1640 filename, linenum);
1641 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001642 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001643 fatal("%s line %d: bad port number in "
1644 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001645 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001646 options->num_permitted_opens =
1647 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001648 }
Damien Miller9b439df2006-07-24 14:04:00 +10001649 break;
1650
Damien Millere2754432006-07-24 14:06:47 +10001651 case sForceCommand:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001652 if (cp == NULL || *cp == '\0')
Damien Millere2754432006-07-24 14:06:47 +10001653 fatal("%.200s line %d: Missing argument.", filename,
1654 linenum);
1655 len = strspn(cp, WHITESPACE);
1656 if (*activep && options->adm_forced_command == NULL)
1657 options->adm_forced_command = xstrdup(cp + len);
1658 return 0;
1659
Damien Millerd8cb1f12008-02-10 22:40:12 +11001660 case sChrootDirectory:
1661 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001662
1663 arg = strdelim(&cp);
1664 if (!arg || *arg == '\0')
1665 fatal("%s line %d: missing file name.",
1666 filename, linenum);
1667 if (*activep && *charptr == NULL)
1668 *charptr = xstrdup(arg);
1669 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001670
Damien Miller1aed65e2010-03-04 21:53:35 +11001671 case sTrustedUserCAKeys:
1672 charptr = &options->trusted_user_ca_keys;
1673 goto parse_filename;
1674
1675 case sRevokedKeys:
1676 charptr = &options->revoked_keys_file;
1677 goto parse_filename;
1678
Damien Miller0dac6fb2010-11-20 15:19:38 +11001679 case sIPQoS:
1680 arg = strdelim(&cp);
1681 if ((value = parse_ipqos(arg)) == -1)
1682 fatal("%s line %d: Bad IPQoS value: %s",
1683 filename, linenum, arg);
1684 arg = strdelim(&cp);
1685 if (arg == NULL)
1686 value2 = value;
1687 else if ((value2 = parse_ipqos(arg)) == -1)
1688 fatal("%s line %d: Bad IPQoS value: %s",
1689 filename, linenum, arg);
1690 if (*activep) {
1691 options->ip_qos_interactive = value;
1692 options->ip_qos_bulk = value2;
1693 }
1694 break;
1695
Damien Miller23528812012-04-22 11:24:43 +10001696 case sVersionAddendum:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001697 if (cp == NULL || *cp == '\0')
Damien Miller23528812012-04-22 11:24:43 +10001698 fatal("%.200s line %d: Missing argument.", filename,
1699 linenum);
1700 len = strspn(cp, WHITESPACE);
1701 if (*activep && options->version_addendum == NULL) {
1702 if (strcasecmp(cp + len, "none") == 0)
1703 options->version_addendum = xstrdup("");
1704 else if (strchr(cp + len, '\r') != NULL)
1705 fatal("%.200s line %d: Invalid argument",
1706 filename, linenum);
1707 else
1708 options->version_addendum = xstrdup(cp + len);
1709 }
1710 return 0;
1711
Damien Miller09d3e122012-10-31 08:58:58 +11001712 case sAuthorizedKeysCommand:
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001713 if (cp == NULL)
1714 fatal("%.200s line %d: Missing argument.", filename,
1715 linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001716 len = strspn(cp, WHITESPACE);
1717 if (*activep && options->authorized_keys_command == NULL) {
1718 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1719 fatal("%.200s line %d: AuthorizedKeysCommand "
1720 "must be an absolute path",
1721 filename, linenum);
1722 options->authorized_keys_command = xstrdup(cp + len);
1723 }
1724 return 0;
1725
1726 case sAuthorizedKeysCommandUser:
1727 charptr = &options->authorized_keys_command_user;
1728
1729 arg = strdelim(&cp);
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001730 if (!arg || *arg == '\0')
1731 fatal("%s line %d: missing AuthorizedKeysCommandUser "
1732 "argument.", filename, linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001733 if (*activep && *charptr == NULL)
1734 *charptr = xstrdup(arg);
1735 break;
1736
Damien Millera6e3f012012-11-04 23:21:40 +11001737 case sAuthenticationMethods:
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001738 if (options->num_auth_methods == 0) {
Damien Millera6e3f012012-11-04 23:21:40 +11001739 while ((arg = strdelim(&cp)) && *arg != '\0') {
1740 if (options->num_auth_methods >=
1741 MAX_AUTH_METHODS)
1742 fatal("%s line %d: "
1743 "too many authentication methods.",
1744 filename, linenum);
1745 if (auth2_methods_valid(arg, 0) != 0)
1746 fatal("%s line %d: invalid "
1747 "authentication method list.",
1748 filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001749 if (!*activep)
1750 continue;
Damien Millera6e3f012012-11-04 23:21:40 +11001751 options->auth_methods[
1752 options->num_auth_methods++] = xstrdup(arg);
1753 }
1754 }
1755 return 0;
1756
Damien Miller7acefbb2014-07-18 14:11:24 +10001757 case sStreamLocalBindMask:
1758 arg = strdelim(&cp);
1759 if (!arg || *arg == '\0')
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001760 fatal("%s line %d: missing StreamLocalBindMask "
1761 "argument.", filename, linenum);
Damien Miller7acefbb2014-07-18 14:11:24 +10001762 /* Parse mode in octal format */
1763 value = strtol(arg, &p, 8);
1764 if (arg == p || value < 0 || value > 0777)
1765 fatal("%s line %d: Bad mask.", filename, linenum);
djm@openbsd.org9559d7d2015-05-01 07:08:08 +00001766 if (*activep)
1767 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
Damien Miller7acefbb2014-07-18 14:11:24 +10001768 break;
1769
1770 case sStreamLocalBindUnlink:
1771 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1772 goto parse_flag;
1773
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001774 case sFingerprintHash:
1775 arg = strdelim(&cp);
1776 if (!arg || *arg == '\0')
1777 fatal("%.200s line %d: Missing argument.",
1778 filename, linenum);
1779 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1780 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1781 filename, linenum, arg);
1782 if (*activep)
1783 options->fingerprint_hash = value;
1784 break;
1785
Ben Lindstromade03f62001-12-06 18:22:17 +00001786 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001787 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001788 filename, linenum, arg);
1789 while (arg)
1790 arg = strdelim(&cp);
1791 break;
1792
Damien Millerf9b3feb2003-05-16 11:38:32 +10001793 case sUnsupported:
1794 logit("%s line %d: Unsupported option %s",
1795 filename, linenum, arg);
1796 while (arg)
1797 arg = strdelim(&cp);
1798 break;
1799
Ben Lindstromade03f62001-12-06 18:22:17 +00001800 default:
1801 fatal("%s line %d: Missing handler for opcode %s (%d)",
1802 filename, linenum, arg, opcode);
1803 }
1804 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1805 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1806 filename, linenum, arg);
1807 return 0;
1808}
1809
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001810/* Reads the server configuration file. */
1811
Damien Miller4af51302000-04-16 11:18:38 +10001812void
Darren Tucker645ab752004-06-25 13:33:20 +10001813load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001814{
Damien Miller46cb75a2012-07-31 12:22:37 +10001815 char line[4096], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001816 FILE *f;
Damien Miller46cb75a2012-07-31 12:22:37 +10001817 int lineno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001818
Darren Tucker645ab752004-06-25 13:33:20 +10001819 debug2("%s: filename %s", __func__, filename);
1820 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001821 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001822 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001823 }
Darren Tucker645ab752004-06-25 13:33:20 +10001824 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001825 while (fgets(line, sizeof(line), f)) {
Damien Miller46cb75a2012-07-31 12:22:37 +10001826 lineno++;
1827 if (strlen(line) == sizeof(line) - 1)
1828 fatal("%s line %d too long", filename, lineno);
Darren Tucker645ab752004-06-25 13:33:20 +10001829 /*
1830 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001831 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001832 * line numbers later for error messages
1833 */
1834 if ((cp = strchr(line, '#')) != NULL)
1835 memcpy(cp, "\n", 2);
1836 cp = line + strspn(line, " \t\r");
1837
1838 buffer_append(conf, cp, strlen(cp));
1839 }
1840 buffer_append(conf, "\0", 1);
1841 fclose(f);
1842 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1843}
1844
1845void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001846parse_server_match_config(ServerOptions *options,
1847 struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10001848{
Darren Tucker45150472006-07-12 22:34:17 +10001849 ServerOptions mo;
1850
1851 initialize_server_options(&mo);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001852 parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11001853 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001854}
1855
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001856int parse_server_match_testspec(struct connection_info *ci, char *spec)
1857{
1858 char *p;
1859
1860 while ((p = strsep(&spec, ",")) && *p != '\0') {
1861 if (strncmp(p, "addr=", 5) == 0) {
1862 ci->address = xstrdup(p + 5);
1863 } else if (strncmp(p, "host=", 5) == 0) {
1864 ci->host = xstrdup(p + 5);
1865 } else if (strncmp(p, "user=", 5) == 0) {
1866 ci->user = xstrdup(p + 5);
1867 } else if (strncmp(p, "laddr=", 6) == 0) {
1868 ci->laddress = xstrdup(p + 6);
1869 } else if (strncmp(p, "lport=", 6) == 0) {
1870 ci->lport = a2port(p + 6);
1871 if (ci->lport == -1) {
1872 fprintf(stderr, "Invalid port '%s' in test mode"
1873 " specification %s\n", p+6, p);
1874 return -1;
1875 }
1876 } else {
1877 fprintf(stderr, "Invalid test mode specification %s\n",
1878 p);
1879 return -1;
1880 }
1881 }
1882 return 0;
1883}
1884
1885/*
1886 * returns 1 for a complete spec, 0 for partial spec and -1 for an
1887 * empty spec.
1888 */
1889int server_match_spec_complete(struct connection_info *ci)
1890{
1891 if (ci->user && ci->host && ci->address)
1892 return 1; /* complete */
1893 if (!ci->user && !ci->host && !ci->address)
1894 return -1; /* empty */
1895 return 0; /* partial */
1896}
1897
Darren Tucker1629c072007-02-19 22:25:37 +11001898/*
1899 * Copy any supported values that are set.
1900 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001901 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001902 * array values that are not used pre-authentication, because any that we
1903 * do use must be explictly sent in mm_getpwnamallow().
1904 */
Darren Tucker45150472006-07-12 22:34:17 +10001905void
Darren Tucker1629c072007-02-19 22:25:37 +11001906copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001907{
Damien Miller534b2cc2013-12-05 14:07:27 +11001908#define M_CP_INTOPT(n) do {\
1909 if (src->n != -1) \
1910 dst->n = src->n; \
1911} while (0)
1912
Darren Tucker1629c072007-02-19 22:25:37 +11001913 M_CP_INTOPT(password_authentication);
1914 M_CP_INTOPT(gss_authentication);
1915 M_CP_INTOPT(rsa_authentication);
1916 M_CP_INTOPT(pubkey_authentication);
1917 M_CP_INTOPT(kerberos_authentication);
1918 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001919 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001920 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001921 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001922 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001923
1924 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10001925 M_CP_INTOPT(allow_streamlocal_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001926 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001927 M_CP_INTOPT(permit_tun);
Damien Miller7acefbb2014-07-18 14:11:24 +10001928 M_CP_INTOPT(fwd_opts.gateway_ports);
Darren Tucker1629c072007-02-19 22:25:37 +11001929 M_CP_INTOPT(x11_display_offset);
1930 M_CP_INTOPT(x11_forwarding);
1931 M_CP_INTOPT(x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11001932 M_CP_INTOPT(permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10001933 M_CP_INTOPT(permit_user_rc);
Damien Miller7207f642008-05-19 15:34:50 +10001934 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001935 M_CP_INTOPT(max_authtries);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001936 M_CP_INTOPT(ip_qos_interactive);
1937 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001938 M_CP_INTOPT(rekey_limit);
1939 M_CP_INTOPT(rekey_interval);
Darren Tucker1629c072007-02-19 22:25:37 +11001940
Damien Miller534b2cc2013-12-05 14:07:27 +11001941 /* M_CP_STROPT and M_CP_STRARRAYOPT should not appear before here */
1942#define M_CP_STROPT(n) do {\
1943 if (src->n != NULL && dst->n != src->n) { \
1944 free(dst->n); \
1945 dst->n = src->n; \
1946 } \
1947} while(0)
1948#define M_CP_STRARRAYOPT(n, num_n) do {\
1949 if (src->num_n != 0) { \
1950 for (dst->num_n = 0; dst->num_n < src->num_n; dst->num_n++) \
1951 dst->n[dst->num_n] = xstrdup(src->n[dst->num_n]); \
1952 } \
1953} while(0)
1954
Damien Millerf2e407e2011-05-20 19:04:14 +10001955 /* See comment in servconf.h */
1956 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10001957
Damien Millerc2411902011-05-20 19:03:49 +10001958 /*
1959 * The only things that should be below this point are string options
1960 * which are only used after authentication.
1961 */
Damien Miller5d74e582011-05-20 19:03:31 +10001962 if (preauth)
1963 return;
Damien Millerd8478b62011-05-29 21:39:36 +10001964
Damien Miller5d74e582011-05-20 19:03:31 +10001965 M_CP_STROPT(adm_forced_command);
1966 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001967}
1968
Darren Tucker1629c072007-02-19 22:25:37 +11001969#undef M_CP_INTOPT
1970#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10001971#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11001972
Darren Tucker45150472006-07-12 22:34:17 +10001973void
1974parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001975 struct connection_info *connectinfo)
Darren Tucker45150472006-07-12 22:34:17 +10001976{
1977 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001978 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001979
1980 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1981
Darren Tucker9fbac712004-08-12 22:41:44 +10001982 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001983 active = connectinfo ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001984 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001985 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001986 if (process_server_config_line(options, cp, filename,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001987 linenum++, &active, connectinfo) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001988 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001989 }
Darren Tuckera627d422013-06-02 07:31:17 +10001990 free(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001991 if (bad_options > 0)
1992 fatal("%s: terminating, %d bad configuration options",
1993 filename, bad_options);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00001994 process_queued_listen_addrs(options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001995}
Darren Tuckere7140f22008-06-10 23:01:51 +10001996
1997static const char *
Damien Miller82c55872011-06-23 08:20:30 +10001998fmt_multistate_int(int val, const struct multistate *m)
1999{
2000 u_int i;
2001
2002 for (i = 0; m[i].key != NULL; i++) {
2003 if (m[i].value == val)
2004 return m[i].key;
2005 }
2006 return "UNKNOWN";
2007}
2008
2009static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10002010fmt_intarg(ServerOpCodes code, int val)
2011{
Damien Miller82c55872011-06-23 08:20:30 +10002012 if (val == -1)
2013 return "unset";
2014 switch (code) {
2015 case sAddressFamily:
2016 return fmt_multistate_int(val, multistate_addressfamily);
2017 case sPermitRootLogin:
2018 return fmt_multistate_int(val, multistate_permitrootlogin);
2019 case sGatewayPorts:
2020 return fmt_multistate_int(val, multistate_gatewayports);
2021 case sCompression:
2022 return fmt_multistate_int(val, multistate_compression);
Damien Miller69ff1df2011-06-23 08:30:03 +10002023 case sUsePrivilegeSeparation:
2024 return fmt_multistate_int(val, multistate_privsep);
Damien Milleraa5b3f82012-12-03 09:50:54 +11002025 case sAllowTcpForwarding:
2026 return fmt_multistate_int(val, multistate_tcpfwd);
Damien Miller7acefbb2014-07-18 14:11:24 +10002027 case sAllowStreamLocalForwarding:
2028 return fmt_multistate_int(val, multistate_tcpfwd);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002029 case sFingerprintHash:
2030 return ssh_digest_alg_name(val);
Damien Miller82c55872011-06-23 08:20:30 +10002031 case sProtocol:
Darren Tuckere7140f22008-06-10 23:01:51 +10002032 switch (val) {
2033 case SSH_PROTO_1:
2034 return "1";
2035 case SSH_PROTO_2:
2036 return "2";
2037 case (SSH_PROTO_1|SSH_PROTO_2):
2038 return "2,1";
2039 default:
2040 return "UNKNOWN";
2041 }
Damien Miller82c55872011-06-23 08:20:30 +10002042 default:
2043 switch (val) {
2044 case 0:
2045 return "no";
2046 case 1:
2047 return "yes";
2048 default:
2049 return "UNKNOWN";
2050 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002051 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002052}
2053
2054static const char *
2055lookup_opcode_name(ServerOpCodes code)
2056{
2057 u_int i;
2058
2059 for (i = 0; keywords[i].name != NULL; i++)
2060 if (keywords[i].opcode == code)
2061 return(keywords[i].name);
2062 return "UNKNOWN";
2063}
2064
2065static void
2066dump_cfg_int(ServerOpCodes code, int val)
2067{
2068 printf("%s %d\n", lookup_opcode_name(code), val);
2069}
2070
2071static void
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002072dump_cfg_oct(ServerOpCodes code, int val)
2073{
2074 printf("%s 0%o\n", lookup_opcode_name(code), val);
2075}
2076
2077static void
Darren Tuckere7140f22008-06-10 23:01:51 +10002078dump_cfg_fmtint(ServerOpCodes code, int val)
2079{
2080 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2081}
2082
2083static void
2084dump_cfg_string(ServerOpCodes code, const char *val)
2085{
2086 if (val == NULL)
2087 return;
djm@openbsd.org161cf412014-12-22 07:55:51 +00002088 printf("%s %s\n", lookup_opcode_name(code),
2089 val == NULL ? "none" : val);
Darren Tuckere7140f22008-06-10 23:01:51 +10002090}
2091
2092static void
2093dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
2094{
2095 u_int i;
2096
2097 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10002098 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2099}
2100
2101static void
2102dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
2103{
2104 u_int i;
2105
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002106 if (count <= 0)
2107 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002108 printf("%s", lookup_opcode_name(code));
2109 for (i = 0; i < count; i++)
2110 printf(" %s", vals[i]);
2111 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002112}
2113
2114void
2115dump_config(ServerOptions *o)
2116{
2117 u_int i;
2118 int ret;
2119 struct addrinfo *ai;
2120 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002121 char *laddr1 = xstrdup(""), *laddr2 = NULL;
Darren Tuckere7140f22008-06-10 23:01:51 +10002122
2123 /* these are usually at the top of the config */
2124 for (i = 0; i < o->num_ports; i++)
2125 printf("port %d\n", o->ports[i]);
2126 dump_cfg_fmtint(sProtocol, o->protocol);
2127 dump_cfg_fmtint(sAddressFamily, o->address_family);
2128
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002129 /*
2130 * ListenAddress must be after Port. add_one_listen_addr pushes
2131 * addresses onto a stack, so to maintain ordering we need to
2132 * print these in reverse order.
2133 */
Darren Tuckere7140f22008-06-10 23:01:51 +10002134 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
2135 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
2136 sizeof(addr), port, sizeof(port),
2137 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
2138 error("getnameinfo failed: %.100s",
2139 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
2140 strerror(errno));
2141 } else {
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002142 laddr2 = laddr1;
Darren Tuckere7140f22008-06-10 23:01:51 +10002143 if (ai->ai_family == AF_INET6)
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002144 xasprintf(&laddr1, "listenaddress [%s]:%s\n%s",
2145 addr, port, laddr2);
Darren Tuckere7140f22008-06-10 23:01:51 +10002146 else
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002147 xasprintf(&laddr1, "listenaddress %s:%s\n%s",
2148 addr, port, laddr2);
2149 free(laddr2);
Darren Tuckere7140f22008-06-10 23:01:51 +10002150 }
2151 }
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002152 printf("%s", laddr1);
2153 free(laddr1);
Darren Tuckere7140f22008-06-10 23:01:51 +10002154
2155 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10002156#ifdef USE_PAM
Darren Tucker70860b62015-04-17 10:56:13 +10002157 dump_cfg_fmtint(sUsePAM, o->use_pam);
Damien Miller212f0b02008-07-23 17:42:29 +10002158#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002159 dump_cfg_int(sServerKeyBits, o->server_key_bits);
2160 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
2161 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
2162 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
2163 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11002164 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10002165 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
2166 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002167 dump_cfg_oct(sStreamLocalBindMask, o->fwd_opts.streamlocal_bind_mask);
Darren Tuckere7140f22008-06-10 23:01:51 +10002168
2169 /* formatted integer arguments */
2170 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
2171 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
2172 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
2173 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2174 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
2175 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
2176 o->hostbased_uses_name_from_packet_only);
2177 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
2178 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10002179#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10002180 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
2181 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
2182 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10002183# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10002184 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10002185# endif
2186#endif
2187#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10002188 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2189 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10002190#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002191 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2192 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2193 o->kbd_interactive_authentication);
2194 dump_cfg_fmtint(sChallengeResponseAuthentication,
2195 o->challenge_response_authentication);
2196 dump_cfg_fmtint(sPrintMotd, o->print_motd);
2197 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
2198 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
2199 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002200 dump_cfg_fmtint(sPermitTTY, o->permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002201 dump_cfg_fmtint(sPermitUserRC, o->permit_user_rc);
Darren Tuckere7140f22008-06-10 23:01:51 +10002202 dump_cfg_fmtint(sStrictModes, o->strict_modes);
2203 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
2204 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
2205 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
2206 dump_cfg_fmtint(sUseLogin, o->use_login);
2207 dump_cfg_fmtint(sCompression, o->compression);
Damien Miller7acefbb2014-07-18 14:11:24 +10002208 dump_cfg_fmtint(sGatewayPorts, o->fwd_opts.gateway_ports);
Darren Tuckere7140f22008-06-10 23:01:51 +10002209 dump_cfg_fmtint(sUseDNS, o->use_dns);
2210 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002211 dump_cfg_fmtint(sAllowAgentForwarding, o->allow_agent_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002212 dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
Darren Tuckere7140f22008-06-10 23:01:51 +10002213 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002214 dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
Darren Tuckere7140f22008-06-10 23:01:51 +10002215
2216 /* string arguments */
2217 dump_cfg_string(sPidFile, o->pid_file);
2218 dump_cfg_string(sXAuthLocation, o->xauth_location);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002219 dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
2220 dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
Darren Tuckere7140f22008-06-10 23:01:51 +10002221 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10002222 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11002223 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11002224 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
2225 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10002226 dump_cfg_string(sAuthorizedPrincipalsFile,
2227 o->authorized_principals_file);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002228 dump_cfg_string(sVersionAddendum, *o->version_addendum == '\0'
2229 ? "none" : o->version_addendum);
Damien Miller09d3e122012-10-31 08:58:58 +11002230 dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
2231 dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
Damien Miller85b45e02013-07-20 13:21:52 +10002232 dump_cfg_string(sHostKeyAgent, o->host_key_agent);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002233 dump_cfg_string(sKexAlgorithms,
djm@openbsd.org259a02e2014-10-13 00:38:35 +00002234 o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002235 dump_cfg_string(sHostbasedAcceptedKeyTypes, o->hostbased_key_types ?
2236 o->hostbased_key_types : KEX_DEFAULT_PK_ALG);
2237 dump_cfg_string(sPubkeyAcceptedKeyTypes, o->pubkey_key_types ?
2238 o->pubkey_key_types : KEX_DEFAULT_PK_ALG);
Darren Tuckere7140f22008-06-10 23:01:51 +10002239
2240 /* string arguments requiring a lookup */
2241 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
2242 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
2243
2244 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10002245 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
2246 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002247 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
2248 o->host_key_files);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002249 dump_cfg_strarray(sHostCertificate, o->num_host_cert_files,
Damien Miller0a80ca12010-02-27 07:55:05 +11002250 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002251 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
2252 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
2253 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
2254 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
2255 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
Damien Millera6e3f012012-11-04 23:21:40 +11002256 dump_cfg_strarray_oneline(sAuthenticationMethods,
2257 o->num_auth_methods, o->auth_methods);
Darren Tuckere7140f22008-06-10 23:01:51 +10002258
2259 /* other arguments */
2260 for (i = 0; i < o->num_subsystems; i++)
2261 printf("subsystem %s %s\n", o->subsystem_name[i],
2262 o->subsystem_args[i]);
2263
2264 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
2265 o->max_startups_rate, o->max_startups);
2266
2267 for (i = 0; tunmode_desc[i].val != -1; i++)
2268 if (tunmode_desc[i].val == o->permit_tun) {
2269 s = tunmode_desc[i].text;
2270 break;
2271 }
2272 dump_cfg_string(sPermitTunnel, s);
2273
Damien Miller91475862011-05-05 14:14:34 +10002274 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2275 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11002276
Damien Millera6d6c1f2013-08-21 02:40:01 +10002277 printf("rekeylimit %lld %d\n", (long long)o->rekey_limit,
2278 o->rekey_interval);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002279
Darren Tuckere7140f22008-06-10 23:01:51 +10002280 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10002281}