blob: 475076bf2a3e2e808f7d833c309f18522351b06d [file] [log] [blame]
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00002/* $OpenBSD: servconf.c,v 1.259 2015/01/16 06:40:12 deraadt Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003/*
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
Damien Millere3b60b52006-07-10 21:08:03 +100016#include <sys/types.h>
17#include <sys/socket.h>
18
Damien Miller0dac6fb2010-11-20 15:19:38 +110019#include <netinet/in.h>
20#include <netinet/in_systm.h>
21#include <netinet/ip.h>
22
Darren Tucker5f96f3b2013-05-16 20:29:28 +100023#include <ctype.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100024#include <netdb.h>
Damien Miller565ca3f2006-08-19 00:23:15 +100025#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100026#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100027#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100028#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100029#include <signal.h>
Damien Millere6b3b612006-07-24 14:01:23 +100030#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerd7834352006-08-05 12:39:39 +100032#include <stdarg.h>
Darren Tuckere7140f22008-06-10 23:01:51 +100033#include <errno.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100034#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100035#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100036#endif
Damien Millerbe43ebf2006-07-24 13:51:51 +100037
Damien Millerb84886b2008-05-19 15:05:07 +100038#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100039#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100042#include "buffer.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100043#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044#include "servconf.h"
Damien Miller78928792000-04-12 20:17:38 +100045#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100048#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000049#include "kex.h"
50#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100051#include "match.h"
Damien Miller9b439df2006-07-24 14:04:00 +100052#include "channels.h"
Damien Miller565ca3f2006-08-19 00:23:15 +100053#include "groupaccess.h"
Darren Tuckerfbcf8272012-05-19 19:37:01 +100054#include "canohost.h"
55#include "packet.h"
Damien Millera6e3f012012-11-04 23:21:40 +110056#include "hostfile.h"
57#include "auth.h"
djm@openbsd.org57d378e2014-08-19 23:58:28 +000058#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000059#include "digest.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060
Damien Miller3dc71ad2009-01-28 16:31:22 +110061static void add_listen_addr(ServerOptions *, char *, int);
62static void add_one_listen_addr(ServerOptions *, char *, int);
Damien Miller34132e52000-01-14 15:45:46 +110063
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000064/* Use of privilege separation or not */
65extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100066extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000067
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068/* Initializes the server options to their default values. */
69
Damien Miller4af51302000-04-16 11:18:38 +100070void
Damien Miller95def091999-11-25 00:26:21 +110071initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072{
Damien Miller95def091999-11-25 00:26:21 +110073 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110074
75 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100076 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110077
78 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110079 options->num_ports = 0;
80 options->ports_from_cmdline = 0;
81 options->listen_addrs = NULL;
Darren Tucker0f383232005-01-20 10:57:56 +110082 options->address_family = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110083 options->num_host_key_files = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110084 options->num_host_cert_files = 0;
Damien Miller85b45e02013-07-20 13:21:52 +100085 options->host_key_agent = NULL;
Damien Miller6f83b8e2000-05-02 09:23:45 +100086 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110087 options->server_key_bits = -1;
88 options->login_grace_time = -1;
89 options->key_regeneration_time = -1;
Ben Lindstromd8a90212001-02-15 03:08:27 +000090 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110091 options->ignore_rhosts = -1;
92 options->ignore_user_known_hosts = -1;
93 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +000094 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +110095 options->x11_forwarding = -1;
96 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +110097 options->x11_use_localhost = -1;
Damien Miller5ff30c62013-10-30 22:21:50 +110098 options->permit_tty = -1;
Damien Miller72e6b5c2014-07-04 09:00:04 +100099 options->permit_user_rc = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000100 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100101 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +1100102 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +1100103 options->log_facility = SYSLOG_FACILITY_NOT_SET;
104 options->log_level = SYSLOG_LEVEL_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +1100105 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000106 options->hostbased_authentication = -1;
107 options->hostbased_uses_name_from_packet_only = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000108 options->hostbased_key_types = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100109 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100110 options->pubkey_authentication = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000111 options->pubkey_key_types = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100112 options->kerberos_authentication = -1;
113 options->kerberos_or_local_passwd = -1;
114 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100115 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000116 options->gss_authentication=-1;
117 options->gss_cleanup_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100118 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100119 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000120 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100121 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000122 options->permit_user_env = -1;
Damien Miller95def091999-11-25 00:26:21 +1100123 options->use_login = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000124 options->compression = -1;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000125 options->rekey_limit = -1;
126 options->rekey_interval = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100127 options->allow_tcp_forwarding = -1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000128 options->allow_streamlocal_forwarding = -1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000129 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100130 options->num_allow_users = 0;
131 options->num_deny_users = 0;
132 options->num_allow_groups = 0;
133 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000134 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000135 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000136 options->kex_algorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000137 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller7acefbb2014-07-18 14:11:24 +1000138 options->fwd_opts.gateway_ports = -1;
139 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
140 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000141 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000142 options->max_startups_begin = -1;
143 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000144 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000145 options->max_authtries = -1;
Damien Miller7207f642008-05-19 15:34:50 +1000146 options->max_sessions = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000147 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000148 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000149 options->client_alive_interval = -1;
150 options->client_alive_count_max = -1;
Damien Millerd8478b62011-05-29 21:39:36 +1000151 options->num_authkeys_files = 0;
Darren Tucker46bc0752004-05-02 22:11:30 +1000152 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100153 options->permit_tun = -1;
Damien Millera765cf42006-07-24 14:08:13 +1000154 options->num_permitted_opens = -1;
Damien Millere2754432006-07-24 14:06:47 +1000155 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100156 options->chroot_directory = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +1100157 options->authorized_keys_command = NULL;
158 options->authorized_keys_command_user = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100159 options->revoked_keys_file = NULL;
160 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000161 options->authorized_principals_file = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100162 options->ip_qos_interactive = -1;
163 options->ip_qos_bulk = -1;
Damien Miller23528812012-04-22 11:24:43 +1000164 options->version_addendum = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000165 options->fingerprint_hash = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166}
167
djm@openbsd.org161cf412014-12-22 07:55:51 +0000168/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
169static int
170option_clear_or_none(const char *o)
171{
172 return o == NULL || strcasecmp(o, "none") == 0;
173}
174
Damien Miller4af51302000-04-16 11:18:38 +1000175void
Damien Miller95def091999-11-25 00:26:21 +1100176fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177{
djm@openbsd.org161cf412014-12-22 07:55:51 +0000178 int i;
179
Damien Miller726273e2001-11-12 11:40:11 +1100180 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000181 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000182 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100183
184 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100185 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +1100186 options->protocol = SSH_PROTO_2;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100187 if (options->num_host_key_files == 0) {
188 /* fill default hostkeys for protocols */
189 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100190 options->host_key_files[options->num_host_key_files++] =
191 _PATH_HOST_KEY_FILE;
192 if (options->protocol & SSH_PROTO_2) {
193 options->host_key_files[options->num_host_key_files++] =
194 _PATH_HOST_RSA_KEY_FILE;
195 options->host_key_files[options->num_host_key_files++] =
196 _PATH_HOST_DSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100197#ifdef OPENSSL_HAS_ECC
Damien Millere13cadf2010-09-10 11:15:33 +1000198 options->host_key_files[options->num_host_key_files++] =
199 _PATH_HOST_ECDSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100200#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +1100201 options->host_key_files[options->num_host_key_files++] =
202 _PATH_HOST_ED25519_KEY_FILE;
Damien Miller7fc23732002-01-22 23:19:11 +1100203 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100204 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100205 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100206 if (options->num_ports == 0)
207 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
208 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000209 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000210 if (options->pid_file == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000211 options->pid_file = xstrdup(_PATH_SSH_DAEMON_PID_FILE);
Damien Miller95def091999-11-25 00:26:21 +1100212 if (options->server_key_bits == -1)
Darren Tucker7499b0c2008-07-02 22:35:43 +1000213 options->server_key_bits = 1024;
Damien Miller95def091999-11-25 00:26:21 +1100214 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000215 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100216 if (options->key_regeneration_time == -1)
217 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000218 if (options->permit_root_login == PERMIT_NOT_SET)
219 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100220 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100221 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100222 if (options->ignore_user_known_hosts == -1)
223 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100224 if (options->print_motd == -1)
225 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000226 if (options->print_lastlog == -1)
227 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100228 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100229 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100230 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100231 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100232 if (options->x11_use_localhost == -1)
233 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000234 if (options->xauth_location == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000235 options->xauth_location = xstrdup(_PATH_XAUTH);
Damien Miller5ff30c62013-10-30 22:21:50 +1100236 if (options->permit_tty == -1)
237 options->permit_tty = 1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000238 if (options->permit_user_rc == -1)
239 options->permit_user_rc = 1;
Damien Miller95def091999-11-25 00:26:21 +1100240 if (options->strict_modes == -1)
241 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100242 if (options->tcp_keep_alive == -1)
243 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100244 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100245 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100246 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000247 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100248 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100249 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000250 if (options->hostbased_authentication == -1)
251 options->hostbased_authentication = 0;
252 if (options->hostbased_uses_name_from_packet_only == -1)
253 options->hostbased_uses_name_from_packet_only = 0;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000254 if (options->hostbased_key_types == NULL)
255 options->hostbased_key_types = xstrdup("*");
Damien Miller95def091999-11-25 00:26:21 +1100256 if (options->rsa_authentication == -1)
257 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100258 if (options->pubkey_authentication == -1)
259 options->pubkey_authentication = 1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000260 if (options->pubkey_key_types == NULL)
261 options->pubkey_key_types = xstrdup("*");
Damien Miller95def091999-11-25 00:26:21 +1100262 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000263 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100264 if (options->kerberos_or_local_passwd == -1)
265 options->kerberos_or_local_passwd = 1;
266 if (options->kerberos_ticket_cleanup == -1)
267 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100268 if (options->kerberos_get_afs_token == -1)
269 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000270 if (options->gss_authentication == -1)
271 options->gss_authentication = 0;
272 if (options->gss_cleanup_creds == -1)
273 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100274 if (options->password_authentication == -1)
275 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100276 if (options->kbd_interactive_authentication == -1)
277 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000278 if (options->challenge_response_authentication == -1)
279 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100280 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100281 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000282 if (options->permit_user_env == -1)
283 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100284 if (options->use_login == -1)
285 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000286 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000287 options->compression = COMP_DELAYED;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000288 if (options->rekey_limit == -1)
289 options->rekey_limit = 0;
290 if (options->rekey_interval == -1)
291 options->rekey_interval = 0;
Damien Miller50a41ed2000-10-16 12:14:42 +1100292 if (options->allow_tcp_forwarding == -1)
Damien Milleraa5b3f82012-12-03 09:50:54 +1100293 options->allow_tcp_forwarding = FORWARD_ALLOW;
Damien Miller7acefbb2014-07-18 14:11:24 +1000294 if (options->allow_streamlocal_forwarding == -1)
295 options->allow_streamlocal_forwarding = FORWARD_ALLOW;
Damien Miller4f755cd2008-05-19 14:57:41 +1000296 if (options->allow_agent_forwarding == -1)
297 options->allow_agent_forwarding = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000298 if (options->fwd_opts.gateway_ports == -1)
299 options->fwd_opts.gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000300 if (options->max_startups == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100301 options->max_startups = 100;
Damien Miller942da032000-08-18 13:59:06 +1000302 if (options->max_startups_rate == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100303 options->max_startups_rate = 30; /* 30% */
Damien Miller942da032000-08-18 13:59:06 +1000304 if (options->max_startups_begin == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100305 options->max_startups_begin = 10;
Darren Tucker89413db2004-05-24 10:36:23 +1000306 if (options->max_authtries == -1)
307 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000308 if (options->max_sessions == -1)
309 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000310 if (options->use_dns == -1)
311 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000312 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100313 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000314 if (options->client_alive_count_max == -1)
315 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000316 if (options->num_authkeys_files == 0) {
317 options->authorized_keys_files[options->num_authkeys_files++] =
318 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS);
319 options->authorized_keys_files[options->num_authkeys_files++] =
320 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS2);
321 }
Damien Millerd27b9472005-12-13 19:29:02 +1100322 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100323 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100324 if (options->ip_qos_interactive == -1)
325 options->ip_qos_interactive = IPTOS_LOWDELAY;
326 if (options->ip_qos_bulk == -1)
327 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller23528812012-04-22 11:24:43 +1000328 if (options->version_addendum == NULL)
329 options->version_addendum = xstrdup("");
Damien Miller7acefbb2014-07-18 14:11:24 +1000330 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
331 options->fwd_opts.streamlocal_bind_mask = 0177;
332 if (options->fwd_opts.streamlocal_bind_unlink == -1)
333 options->fwd_opts.streamlocal_bind_unlink = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000334 if (options->fingerprint_hash == -1)
335 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
Ben Lindstromfb62a692002-06-06 19:47:11 +0000336 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000337 if (use_privsep == -1)
Damien Miller5a5c2b92012-07-31 12:21:34 +1000338 use_privsep = PRIVSEP_NOSANDBOX;
Damien Miller4903eb42002-06-21 16:20:44 +1000339
djm@openbsd.org161cf412014-12-22 07:55:51 +0000340#define CLEAR_ON_NONE(v) \
341 do { \
342 if (option_clear_or_none(v)) { \
343 free(v); \
344 v = NULL; \
345 } \
346 } while(0)
347 CLEAR_ON_NONE(options->pid_file);
348 CLEAR_ON_NONE(options->xauth_location);
349 CLEAR_ON_NONE(options->banner);
350 CLEAR_ON_NONE(options->trusted_user_ca_keys);
351 CLEAR_ON_NONE(options->revoked_keys_file);
352 for (i = 0; i < options->num_host_key_files; i++)
353 CLEAR_ON_NONE(options->host_key_files[i]);
354 for (i = 0; i < options->num_host_cert_files; i++)
355 CLEAR_ON_NONE(options->host_cert_files[i]);
356#undef CLEAR_ON_NONE
357
Tim Rice40017b02002-07-14 13:36:49 -0700358#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000359 if (use_privsep && options->compression == 1) {
360 error("This platform does not support both privilege "
361 "separation and compression");
362 error("Compression disabled");
363 options->compression = 0;
364 }
365#endif
366
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367}
368
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000369/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100370typedef enum {
371 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100372 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000373 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100374 /* Standard Options */
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000375 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime,
376 sKeyRegenerationTime, sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000377 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100378 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100379 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000380 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100381 sPasswordAuthentication, sKbdInteractiveAuthentication,
382 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000383 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100384 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller5ff30c62013-10-30 22:21:50 +1100385 sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000386 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000387 sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000388 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000389 sGatewayPorts, sPubkeyAuthentication, sPubkeyAcceptedKeyTypes,
390 sXAuthLocation, sSubsystem, sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000391 sBanner, sUseDNS, sHostbasedAuthentication,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000392 sHostbasedUsesNameFromPacketOnly, sHostbasedAcceptedKeyTypes,
393 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
Damien Millerd27b9472005-12-13 19:29:02 +1100394 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100395 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100396 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller7cc194f2014-02-04 11:12:56 +1100397 sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000398 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Miller23528812012-04-22 11:24:43 +1000399 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Miller09d3e122012-10-31 08:58:58 +1100400 sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
Damien Miller72e6b5c2014-07-04 09:00:04 +1000401 sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
Damien Miller7acefbb2014-07-18 14:11:24 +1000402 sStreamLocalBindMask, sStreamLocalBindUnlink,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000403 sAllowStreamLocalForwarding, sFingerprintHash,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000404 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405} ServerOpCodes;
406
Darren Tucker45150472006-07-12 22:34:17 +1000407#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
408#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
409#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
410
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100412static struct {
413 const char *name;
414 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000415 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100416} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100417 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000418#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000419 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000420#else
Darren Tucker45150472006-07-12 22:34:17 +1000421 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000422#endif
Darren Tucker45150472006-07-12 22:34:17 +1000423 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100424 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000425 { "port", sPort, SSHCFG_GLOBAL },
426 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
427 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
Damien Miller85b45e02013-07-20 13:21:52 +1000428 { "hostkeyagent", sHostKeyAgent, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000429 { "pidfile", sPidFile, SSHCFG_GLOBAL },
430 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
431 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
432 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100433 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000434 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
435 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
436 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100437 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
438 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000439 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000440 { "hostbasedacceptedkeytypes", sHostbasedAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100441 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
442 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000443 { "pubkeyacceptedkeytypes", sPubkeyAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000444 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000445#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100446 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000447 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
448 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100449#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000450 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000451#else
Darren Tucker45150472006-07-12 22:34:17 +1000452 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100453#endif
454#else
Darren Tucker1629c072007-02-19 22:25:37 +1100455 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000456 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
457 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
458 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000459#endif
Darren Tucker45150472006-07-12 22:34:17 +1000460 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
461 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000462#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100463 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000464 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000465#else
Darren Tucker1629c072007-02-19 22:25:37 +1100466 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000467 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000468#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100469 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
470 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100471 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000472 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
473 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
474 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
475 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
476 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
477 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
478 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
479 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000480 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
481 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
482 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000483 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
484 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100485 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000486 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
487 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
488 { "compression", sCompression, SSHCFG_GLOBAL },
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000489 { "rekeylimit", sRekeyLimit, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000490 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
491 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
492 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000493 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Damien Millerc24da772012-06-20 21:53:58 +1000494 { "allowusers", sAllowUsers, SSHCFG_ALL },
495 { "denyusers", sDenyUsers, SSHCFG_ALL },
496 { "allowgroups", sAllowGroups, SSHCFG_ALL },
497 { "denygroups", sDenyGroups, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000498 { "ciphers", sCiphers, SSHCFG_GLOBAL },
499 { "macs", sMacs, SSHCFG_GLOBAL },
500 { "protocol", sProtocol, SSHCFG_GLOBAL },
501 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
502 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
503 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000504 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000505 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100506 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000507 { "usedns", sUseDNS, SSHCFG_GLOBAL },
508 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
509 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
510 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
511 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000512 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000513 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000514 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Damien Millerc24da772012-06-20 21:53:58 +1000515 { "acceptenv", sAcceptEnv, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000516 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Damien Miller5ff30c62013-10-30 22:21:50 +1100517 { "permittty", sPermitTTY, SSHCFG_ALL },
Damien Miller72e6b5c2014-07-04 09:00:04 +1000518 { "permituserrc", sPermitUserRC, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000519 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000520 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000521 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100522 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100523 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100524 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
525 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000526 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000527 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100528 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller09d3e122012-10-31 08:58:58 +1100529 { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
530 { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000531 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Damien Millera6e3f012012-11-04 23:21:40 +1100532 { "authenticationmethods", sAuthenticationMethods, SSHCFG_ALL },
Damien Miller7acefbb2014-07-18 14:11:24 +1000533 { "streamlocalbindmask", sStreamLocalBindMask, SSHCFG_ALL },
534 { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
535 { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000536 { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000537 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538};
539
Darren Tuckere7140f22008-06-10 23:01:51 +1000540static struct {
541 int val;
542 char *text;
543} tunmode_desc[] = {
544 { SSH_TUNMODE_NO, "no" },
545 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
546 { SSH_TUNMODE_ETHERNET, "ethernet" },
547 { SSH_TUNMODE_YES, "yes" },
548 { -1, NULL }
549};
550
Damien Miller5428f641999-11-25 11:54:57 +1100551/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000552 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100553 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000554
Damien Miller4af51302000-04-16 11:18:38 +1000555static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100556parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000557 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000559 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560
Damien Miller95def091999-11-25 00:26:21 +1100561 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000562 if (strcasecmp(cp, keywords[i].name) == 0) {
563 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100564 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000565 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000567 error("%s: line %d: Bad configuration option: %s",
568 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100569 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570}
571
Darren Tucker88b6fb22010-01-13 22:44:29 +1100572char *
573derelativise_path(const char *path)
574{
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000575 char *expanded, *ret, cwd[PATH_MAX];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100576
djm@openbsd.org161cf412014-12-22 07:55:51 +0000577 if (strcasecmp(path, "none") == 0)
578 return xstrdup("none");
Darren Tucker88b6fb22010-01-13 22:44:29 +1100579 expanded = tilde_expand_filename(path, getuid());
580 if (*expanded == '/')
581 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100582 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100583 fatal("%s: getcwd: %s", __func__, strerror(errno));
584 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tuckera627d422013-06-02 07:31:17 +1000585 free(expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100586 return ret;
587}
588
Ben Lindstrombba81212001-06-25 05:01:22 +0000589static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100590add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100591{
Damien Millereccb9de2005-06-17 12:59:34 +1000592 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100593
594 if (options->num_ports == 0)
595 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100596 if (options->address_family == -1)
597 options->address_family = AF_UNSPEC;
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
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000627struct connection_info *
628get_connection_info(int populate, int use_dns)
629{
630 static struct connection_info ci;
631
632 if (!populate)
633 return &ci;
634 ci.host = get_canonical_hostname(use_dns);
635 ci.address = get_remote_ipaddr();
636 ci.laddress = get_local_ipaddr(packet_get_connection_in());
637 ci.lport = get_local_port();
638 return &ci;
639}
640
Darren Tucker45150472006-07-12 22:34:17 +1000641/*
642 * The strategy for the Match blocks is that the config file is parsed twice.
643 *
644 * The first time is at startup. activep is initialized to 1 and the
645 * directives in the global context are processed and acted on. Hitting a
646 * Match directive unsets activep and the directives inside the block are
647 * checked for syntax only.
648 *
649 * The second time is after a connection has been established but before
650 * authentication. activep is initialized to 2 and global config directives
651 * are ignored since they have already been processed. If the criteria in a
652 * Match block is met, activep is set and the subsequent directives
653 * processed and actioned until EOF or another Match block unsets it. Any
654 * options set are copied into the main server config.
655 *
656 * Potential additions/improvements:
657 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
658 *
659 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
660 * Match Address 192.168.0.*
661 * Tag trusted
662 * Match Group wheel
663 * Tag trusted
664 * Match Tag trusted
665 * AllowTcpForwarding yes
666 * GatewayPorts clientspecified
667 * [...]
668 *
669 * - Add a PermittedChannelRequests directive
670 * Match Group shell
671 * PermittedChannelRequests session,forwarded-tcpip
672 */
673
674static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000675match_cfg_line_group(const char *grps, int line, const char *user)
676{
677 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000678 struct passwd *pw;
679
Damien Miller565ca3f2006-08-19 00:23:15 +1000680 if (user == NULL)
681 goto out;
682
683 if ((pw = getpwnam(user)) == NULL) {
684 debug("Can't match group at line %d because user %.100s does "
685 "not exist", line, user);
686 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
687 debug("Can't Match group because user %.100s not in any group "
688 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000689 } else if (ga_match_pattern_list(grps) != 1) {
690 debug("user %.100s does not match group list %.100s at line %d",
691 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000692 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000693 debug("user %.100s matched group list %.100s at line %d", user,
694 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000695 result = 1;
696 }
697out:
698 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000699 return result;
700}
701
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000702/*
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000703 * All of the attributes on a single Match line are ANDed together, so we need
Damien Miller03bf2e62013-10-24 21:01:26 +1100704 * to check every attribute and set the result to zero if any attribute does
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000705 * not match.
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000706 */
Damien Miller565ca3f2006-08-19 00:23:15 +1000707static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000708match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +1000709{
Damien Millercf31f382013-10-24 21:02:56 +1100710 int result = 1, attributes = 0, port;
Darren Tucker45150472006-07-12 22:34:17 +1000711 char *arg, *attrib, *cp = *condition;
712 size_t len;
713
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000714 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000715 debug3("checking syntax for 'Match %s'", cp);
716 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000717 debug3("checking match for '%s' user %s host %s addr %s "
718 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
719 ci->host ? ci->host : "(null)",
720 ci->address ? ci->address : "(null)",
721 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +1000722
723 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100724 attributes++;
725 if (strcasecmp(attrib, "all") == 0) {
726 if (attributes != 1 ||
727 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
728 error("'all' cannot be combined with other "
729 "Match attributes");
730 return -1;
731 }
732 *condition = cp;
733 return 1;
734 }
Darren Tucker45150472006-07-12 22:34:17 +1000735 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
736 error("Missing Match criteria for %s", attrib);
737 return -1;
738 }
739 len = strlen(arg);
740 if (strcasecmp(attrib, "user") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000741 if (ci == NULL || ci->user == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000742 result = 0;
743 continue;
744 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000745 if (match_pattern_list(ci->user, arg, len, 0) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000746 result = 0;
747 else
748 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000749 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000750 } else if (strcasecmp(attrib, "group") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000751 if (ci == NULL || ci->user == NULL) {
752 result = 0;
753 continue;
754 }
755 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +1000756 case -1:
757 return -1;
758 case 0:
759 result = 0;
760 }
Darren Tucker45150472006-07-12 22:34:17 +1000761 } else if (strcasecmp(attrib, "host") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000762 if (ci == NULL || ci->host == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000763 result = 0;
764 continue;
765 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000766 if (match_hostname(ci->host, arg, len) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000767 result = 0;
768 else
769 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000770 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +1000771 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000772 if (ci == NULL || ci->address == NULL) {
773 result = 0;
774 continue;
775 }
776 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000777 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000778 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000779 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000780 break;
781 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000782 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000783 result = 0;
784 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000785 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000786 return -1;
787 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000788 } else if (strcasecmp(attrib, "localaddress") == 0){
789 if (ci == NULL || ci->laddress == NULL) {
790 result = 0;
791 continue;
792 }
793 switch (addr_match_list(ci->laddress, arg)) {
794 case 1:
795 debug("connection from %.100s matched "
796 "'LocalAddress %.100s' at line %d",
797 ci->laddress, arg, line);
798 break;
799 case 0:
800 case -1:
801 result = 0;
802 break;
803 case -2:
804 return -1;
805 }
806 } else if (strcasecmp(attrib, "localport") == 0) {
807 if ((port = a2port(arg)) == -1) {
808 error("Invalid LocalPort '%s' on Match line",
809 arg);
810 return -1;
811 }
812 if (ci == NULL || ci->lport == 0) {
813 result = 0;
814 continue;
815 }
816 /* TODO support port lists */
817 if (port == ci->lport)
818 debug("connection from %.100s matched "
819 "'LocalPort %d' at line %d",
820 ci->laddress, port, line);
821 else
822 result = 0;
Darren Tucker45150472006-07-12 22:34:17 +1000823 } else {
824 error("Unsupported Match attribute %s", attrib);
825 return -1;
826 }
827 }
Damien Millercf31f382013-10-24 21:02:56 +1100828 if (attributes == 0) {
829 error("One or more attributes required for Match");
830 return -1;
831 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000832 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000833 debug3("match %sfound", result ? "" : "not ");
834 *condition = cp;
835 return result;
836}
837
Damien Millere2754432006-07-24 14:06:47 +1000838#define WHITESPACE " \t\r\n"
839
Damien Miller33322122011-06-20 14:43:11 +1000840/* Multistate option parsing */
841struct multistate {
842 char *key;
843 int value;
844};
845static const struct multistate multistate_addressfamily[] = {
846 { "inet", AF_INET },
847 { "inet6", AF_INET6 },
848 { "any", AF_UNSPEC },
849 { NULL, -1 }
850};
851static const struct multistate multistate_permitrootlogin[] = {
852 { "without-password", PERMIT_NO_PASSWD },
853 { "forced-commands-only", PERMIT_FORCED_ONLY },
854 { "yes", PERMIT_YES },
855 { "no", PERMIT_NO },
856 { NULL, -1 }
857};
858static const struct multistate multistate_compression[] = {
859 { "delayed", COMP_DELAYED },
860 { "yes", COMP_ZLIB },
861 { "no", COMP_NONE },
862 { NULL, -1 }
863};
864static const struct multistate multistate_gatewayports[] = {
865 { "clientspecified", 2 },
866 { "yes", 1 },
867 { "no", 0 },
868 { NULL, -1 }
869};
Damien Miller69ff1df2011-06-23 08:30:03 +1000870static const struct multistate multistate_privsep[] = {
Damien Miller5a5c2b92012-07-31 12:21:34 +1000871 { "yes", PRIVSEP_NOSANDBOX },
872 { "sandbox", PRIVSEP_ON },
873 { "nosandbox", PRIVSEP_NOSANDBOX },
Damien Miller69ff1df2011-06-23 08:30:03 +1000874 { "no", PRIVSEP_OFF },
875 { NULL, -1 }
876};
Damien Milleraa5b3f82012-12-03 09:50:54 +1100877static const struct multistate multistate_tcpfwd[] = {
878 { "yes", FORWARD_ALLOW },
879 { "all", FORWARD_ALLOW },
880 { "no", FORWARD_DENY },
881 { "remote", FORWARD_REMOTE },
882 { "local", FORWARD_LOCAL },
883 { NULL, -1 }
884};
Damien Miller33322122011-06-20 14:43:11 +1000885
Ben Lindstromade03f62001-12-06 18:22:17 +0000886int
887process_server_config_line(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000888 const char *filename, int linenum, int *activep,
889 struct connection_info *connectinfo)
Ben Lindstromade03f62001-12-06 18:22:17 +0000890{
Darren Tucker09c0f032013-05-16 20:48:57 +1000891 char *cp, **charptr, *arg, *p;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000892 int cmdline = 0, *intptr, value, value2, n, port;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100893 SyslogFacility *log_facility_ptr;
894 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000895 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000896 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000897 size_t len;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000898 long long val64;
Damien Miller33322122011-06-20 14:43:11 +1000899 const struct multistate *multistate_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000900
901 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100902 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100903 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000904 /* Ignore leading whitespace */
905 if (*arg == '\0')
906 arg = strdelim(&cp);
907 if (!arg || !*arg || *arg == '#')
908 return 0;
909 intptr = NULL;
910 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000911 opcode = parse_token(arg, filename, linenum, &flags);
912
913 if (activep == NULL) { /* We are processing a command line directive */
914 cmdline = 1;
915 activep = &cmdline;
916 }
917 if (*activep && opcode != sMatch)
918 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
919 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000920 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000921 fatal("%s line %d: Directive '%s' is not allowed "
922 "within a Match block", filename, linenum, arg);
923 } else { /* this is a directive we have already processed */
924 while (arg)
925 arg = strdelim(&cp);
926 return 0;
927 }
928 }
929
Ben Lindstromade03f62001-12-06 18:22:17 +0000930 switch (opcode) {
931 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000932 case sUsePAM:
933 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000934 goto parse_flag;
935
936 /* Standard Options */
937 case sBadOption:
938 return -1;
939 case sPort:
940 /* ignore ports from configfile if cmdline specifies ports */
941 if (options->ports_from_cmdline)
942 return 0;
943 if (options->listen_addrs != NULL)
944 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100945 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000946 if (options->num_ports >= MAX_PORTS)
947 fatal("%s line %d: too many ports.",
948 filename, linenum);
949 arg = strdelim(&cp);
950 if (!arg || *arg == '\0')
951 fatal("%s line %d: missing port number.",
952 filename, linenum);
953 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100954 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000955 fatal("%s line %d: Badly formatted port number.",
956 filename, linenum);
957 break;
958
959 case sServerKeyBits:
960 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +1000961 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +0000962 arg = strdelim(&cp);
963 if (!arg || *arg == '\0')
964 fatal("%s line %d: missing integer value.",
965 filename, linenum);
966 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000967 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000968 *intptr = value;
969 break;
970
971 case sLoginGraceTime:
972 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +1000973 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +0000974 arg = strdelim(&cp);
975 if (!arg || *arg == '\0')
976 fatal("%s line %d: missing time value.",
977 filename, linenum);
978 if ((value = convtime(arg)) == -1)
979 fatal("%s line %d: invalid time value.",
980 filename, linenum);
981 if (*intptr == -1)
982 *intptr = value;
983 break;
984
985 case sKeyRegenerationTime:
986 intptr = &options->key_regeneration_time;
987 goto parse_time;
988
989 case sListenAddress:
990 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100991 if (arg == NULL || *arg == '\0')
992 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000993 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000994 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
995 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
996 && strchr(p+1, ':') != NULL) {
997 add_listen_addr(options, arg, 0);
998 break;
999 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001000 p = hpdelim(&arg);
1001 if (p == NULL)
1002 fatal("%s line %d: bad address:port usage",
1003 filename, linenum);
1004 p = cleanhostname(p);
1005 if (arg == NULL)
1006 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001007 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001008 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001009
Damien Millerf91ee4c2005-03-01 21:24:33 +11001010 add_listen_addr(options, p, port);
1011
Ben Lindstromade03f62001-12-06 18:22:17 +00001012 break;
1013
Darren Tucker0f383232005-01-20 10:57:56 +11001014 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +10001015 intptr = &options->address_family;
1016 multistate_ptr = multistate_addressfamily;
1017 if (options->listen_addrs != NULL)
1018 fatal("%s line %d: address family must be specified "
1019 "before ListenAddress.", filename, linenum);
1020 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +11001021 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +10001022 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +10001023 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +10001024 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +10001025 value = -1;
1026 for (i = 0; multistate_ptr[i].key != NULL; i++) {
1027 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
1028 value = multistate_ptr[i].value;
1029 break;
1030 }
1031 }
1032 if (value == -1)
1033 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +11001034 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +10001035 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +11001036 *intptr = value;
1037 break;
1038
Ben Lindstromade03f62001-12-06 18:22:17 +00001039 case sHostKeyFile:
1040 intptr = &options->num_host_key_files;
1041 if (*intptr >= MAX_HOSTKEYS)
1042 fatal("%s line %d: too many host keys specified (max %d).",
1043 filename, linenum, MAX_HOSTKEYS);
1044 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +10001045 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +00001046 arg = strdelim(&cp);
1047 if (!arg || *arg == '\0')
1048 fatal("%s line %d: missing file name.",
1049 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001050 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +11001051 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001052 /* increase optional counter */
1053 if (intptr != NULL)
1054 *intptr = *intptr + 1;
1055 }
1056 break;
1057
Damien Miller85b45e02013-07-20 13:21:52 +10001058 case sHostKeyAgent:
1059 charptr = &options->host_key_agent;
1060 arg = strdelim(&cp);
1061 if (!arg || *arg == '\0')
1062 fatal("%s line %d: missing socket name.",
1063 filename, linenum);
1064 if (*activep && *charptr == NULL)
1065 *charptr = !strcmp(arg, SSH_AUTHSOCKET_ENV_NAME) ?
1066 xstrdup(arg) : derelativise_path(arg);
1067 break;
1068
Damien Miller0a80ca12010-02-27 07:55:05 +11001069 case sHostCertificate:
1070 intptr = &options->num_host_cert_files;
1071 if (*intptr >= MAX_HOSTKEYS)
1072 fatal("%s line %d: too many host certificates "
1073 "specified (max %d).", filename, linenum,
1074 MAX_HOSTCERTS);
1075 charptr = &options->host_cert_files[*intptr];
1076 goto parse_filename;
1077 break;
1078
Ben Lindstromade03f62001-12-06 18:22:17 +00001079 case sPidFile:
1080 charptr = &options->pid_file;
1081 goto parse_filename;
1082
1083 case sPermitRootLogin:
1084 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +10001085 multistate_ptr = multistate_permitrootlogin;
1086 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001087
1088 case sIgnoreRhosts:
1089 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +10001090 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +00001091 arg = strdelim(&cp);
1092 if (!arg || *arg == '\0')
1093 fatal("%s line %d: missing yes/no argument.",
1094 filename, linenum);
1095 value = 0; /* silence compiler */
1096 if (strcmp(arg, "yes") == 0)
1097 value = 1;
1098 else if (strcmp(arg, "no") == 0)
1099 value = 0;
1100 else
1101 fatal("%s line %d: Bad yes/no argument: %s",
1102 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +10001103 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001104 *intptr = value;
1105 break;
1106
1107 case sIgnoreUserKnownHosts:
1108 intptr = &options->ignore_user_known_hosts;
1109 goto parse_flag;
1110
Ben Lindstromade03f62001-12-06 18:22:17 +00001111 case sRhostsRSAAuthentication:
1112 intptr = &options->rhosts_rsa_authentication;
1113 goto parse_flag;
1114
1115 case sHostbasedAuthentication:
1116 intptr = &options->hostbased_authentication;
1117 goto parse_flag;
1118
1119 case sHostbasedUsesNameFromPacketOnly:
1120 intptr = &options->hostbased_uses_name_from_packet_only;
1121 goto parse_flag;
1122
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001123 case sHostbasedAcceptedKeyTypes:
1124 charptr = &options->hostbased_key_types;
1125 parse_keytypes:
1126 arg = strdelim(&cp);
1127 if (!arg || *arg == '\0')
1128 fatal("%s line %d: Missing argument.",
1129 filename, linenum);
1130 if (!sshkey_names_valid2(arg, 1))
1131 fatal("%s line %d: Bad key types '%s'.",
1132 filename, linenum, arg ? arg : "<NONE>");
1133 if (*activep && *charptr == NULL)
1134 *charptr = xstrdup(arg);
1135 break;
1136
Ben Lindstromade03f62001-12-06 18:22:17 +00001137 case sRSAAuthentication:
1138 intptr = &options->rsa_authentication;
1139 goto parse_flag;
1140
1141 case sPubkeyAuthentication:
1142 intptr = &options->pubkey_authentication;
1143 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001144
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001145 case sPubkeyAcceptedKeyTypes:
1146 charptr = &options->pubkey_key_types;
1147 goto parse_keytypes;
1148
Ben Lindstromade03f62001-12-06 18:22:17 +00001149 case sKerberosAuthentication:
1150 intptr = &options->kerberos_authentication;
1151 goto parse_flag;
1152
1153 case sKerberosOrLocalPasswd:
1154 intptr = &options->kerberos_or_local_passwd;
1155 goto parse_flag;
1156
1157 case sKerberosTicketCleanup:
1158 intptr = &options->kerberos_ticket_cleanup;
1159 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001160
Darren Tucker22ef5082003-12-31 11:37:34 +11001161 case sKerberosGetAFSToken:
1162 intptr = &options->kerberos_get_afs_token;
1163 goto parse_flag;
1164
Darren Tucker0efd1552003-08-26 11:49:55 +10001165 case sGssAuthentication:
1166 intptr = &options->gss_authentication;
1167 goto parse_flag;
1168
1169 case sGssCleanupCreds:
1170 intptr = &options->gss_cleanup_creds;
1171 goto parse_flag;
1172
Ben Lindstromade03f62001-12-06 18:22:17 +00001173 case sPasswordAuthentication:
1174 intptr = &options->password_authentication;
1175 goto parse_flag;
1176
1177 case sKbdInteractiveAuthentication:
1178 intptr = &options->kbd_interactive_authentication;
1179 goto parse_flag;
1180
1181 case sChallengeResponseAuthentication:
1182 intptr = &options->challenge_response_authentication;
1183 goto parse_flag;
1184
1185 case sPrintMotd:
1186 intptr = &options->print_motd;
1187 goto parse_flag;
1188
1189 case sPrintLastLog:
1190 intptr = &options->print_lastlog;
1191 goto parse_flag;
1192
1193 case sX11Forwarding:
1194 intptr = &options->x11_forwarding;
1195 goto parse_flag;
1196
1197 case sX11DisplayOffset:
1198 intptr = &options->x11_display_offset;
1199 goto parse_int;
1200
Damien Miller95c249f2002-02-05 12:11:34 +11001201 case sX11UseLocalhost:
1202 intptr = &options->x11_use_localhost;
1203 goto parse_flag;
1204
Ben Lindstromade03f62001-12-06 18:22:17 +00001205 case sXAuthLocation:
1206 charptr = &options->xauth_location;
1207 goto parse_filename;
1208
Damien Miller5ff30c62013-10-30 22:21:50 +11001209 case sPermitTTY:
1210 intptr = &options->permit_tty;
1211 goto parse_flag;
1212
Damien Miller72e6b5c2014-07-04 09:00:04 +10001213 case sPermitUserRC:
1214 intptr = &options->permit_user_rc;
1215 goto parse_flag;
1216
Ben Lindstromade03f62001-12-06 18:22:17 +00001217 case sStrictModes:
1218 intptr = &options->strict_modes;
1219 goto parse_flag;
1220
Damien Miller12c150e2003-12-17 16:31:10 +11001221 case sTCPKeepAlive:
1222 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001223 goto parse_flag;
1224
1225 case sEmptyPasswd:
1226 intptr = &options->permit_empty_passwd;
1227 goto parse_flag;
1228
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001229 case sPermitUserEnvironment:
1230 intptr = &options->permit_user_env;
1231 goto parse_flag;
1232
Ben Lindstromade03f62001-12-06 18:22:17 +00001233 case sUseLogin:
1234 intptr = &options->use_login;
1235 goto parse_flag;
1236
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001237 case sCompression:
1238 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001239 multistate_ptr = multistate_compression;
1240 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001241
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001242 case sRekeyLimit:
1243 arg = strdelim(&cp);
1244 if (!arg || *arg == '\0')
1245 fatal("%.200s line %d: Missing argument.", filename,
1246 linenum);
1247 if (strcmp(arg, "default") == 0) {
1248 val64 = 0;
1249 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001250 if (scan_scaled(arg, &val64) == -1)
1251 fatal("%.200s line %d: Bad number '%s': %s",
1252 filename, linenum, arg, strerror(errno));
1253 /* check for too-large or too-small limits */
1254 if (val64 > UINT_MAX)
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001255 fatal("%.200s line %d: RekeyLimit too large",
1256 filename, linenum);
1257 if (val64 != 0 && val64 < 16)
1258 fatal("%.200s line %d: RekeyLimit too small",
1259 filename, linenum);
1260 }
1261 if (*activep && options->rekey_limit == -1)
1262 options->rekey_limit = (u_int32_t)val64;
1263 if (cp != NULL) { /* optional rekey interval present */
1264 if (strcmp(cp, "none") == 0) {
1265 (void)strdelim(&cp); /* discard */
1266 break;
1267 }
1268 intptr = &options->rekey_interval;
1269 goto parse_time;
1270 }
1271 break;
1272
Ben Lindstromade03f62001-12-06 18:22:17 +00001273 case sGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +10001274 intptr = &options->fwd_opts.gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001275 multistate_ptr = multistate_gatewayports;
1276 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001277
Damien Miller3a961dc2003-06-03 10:25:48 +10001278 case sUseDNS:
1279 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001280 goto parse_flag;
1281
1282 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001283 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001284 arg = strdelim(&cp);
1285 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001286 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001287 fatal("%.200s line %d: unsupported log facility '%s'",
1288 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001289 if (*log_facility_ptr == -1)
1290 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001291 break;
1292
1293 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001294 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001295 arg = strdelim(&cp);
1296 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001297 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001298 fatal("%.200s line %d: unsupported log level '%s'",
1299 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001300 if (*log_level_ptr == -1)
1301 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001302 break;
1303
1304 case sAllowTcpForwarding:
1305 intptr = &options->allow_tcp_forwarding;
Damien Milleraa5b3f82012-12-03 09:50:54 +11001306 multistate_ptr = multistate_tcpfwd;
1307 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001308
Damien Miller7acefbb2014-07-18 14:11:24 +10001309 case sAllowStreamLocalForwarding:
1310 intptr = &options->allow_streamlocal_forwarding;
1311 multistate_ptr = multistate_tcpfwd;
1312 goto parse_multistate;
1313
Damien Miller4f755cd2008-05-19 14:57:41 +10001314 case sAllowAgentForwarding:
1315 intptr = &options->allow_agent_forwarding;
1316 goto parse_flag;
1317
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001318 case sUsePrivilegeSeparation:
1319 intptr = &use_privsep;
Damien Miller69ff1df2011-06-23 08:30:03 +10001320 multistate_ptr = multistate_privsep;
1321 goto parse_multistate;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001322
Ben Lindstromade03f62001-12-06 18:22:17 +00001323 case sAllowUsers:
1324 while ((arg = strdelim(&cp)) && *arg != '\0') {
1325 if (options->num_allow_users >= MAX_ALLOW_USERS)
1326 fatal("%s line %d: too many allow users.",
1327 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001328 if (!*activep)
1329 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001330 options->allow_users[options->num_allow_users++] =
1331 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001332 }
1333 break;
1334
1335 case sDenyUsers:
1336 while ((arg = strdelim(&cp)) && *arg != '\0') {
1337 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001338 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001339 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001340 if (!*activep)
1341 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001342 options->deny_users[options->num_deny_users++] =
1343 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001344 }
1345 break;
1346
1347 case sAllowGroups:
1348 while ((arg = strdelim(&cp)) && *arg != '\0') {
1349 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1350 fatal("%s line %d: too many allow groups.",
1351 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001352 if (!*activep)
1353 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001354 options->allow_groups[options->num_allow_groups++] =
1355 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001356 }
1357 break;
1358
1359 case sDenyGroups:
1360 while ((arg = strdelim(&cp)) && *arg != '\0') {
1361 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1362 fatal("%s line %d: too many deny groups.",
1363 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001364 if (!*activep)
1365 continue;
1366 options->deny_groups[options->num_deny_groups++] =
1367 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001368 }
1369 break;
1370
1371 case sCiphers:
1372 arg = strdelim(&cp);
1373 if (!arg || *arg == '\0')
1374 fatal("%s line %d: Missing argument.", filename, linenum);
1375 if (!ciphers_valid(arg))
1376 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1377 filename, linenum, arg ? arg : "<NONE>");
1378 if (options->ciphers == NULL)
1379 options->ciphers = xstrdup(arg);
1380 break;
1381
1382 case sMacs:
1383 arg = strdelim(&cp);
1384 if (!arg || *arg == '\0')
1385 fatal("%s line %d: Missing argument.", filename, linenum);
1386 if (!mac_valid(arg))
1387 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1388 filename, linenum, arg ? arg : "<NONE>");
1389 if (options->macs == NULL)
1390 options->macs = xstrdup(arg);
1391 break;
1392
Damien Millerd5f62bf2010-09-24 22:11:14 +10001393 case sKexAlgorithms:
1394 arg = strdelim(&cp);
1395 if (!arg || *arg == '\0')
1396 fatal("%s line %d: Missing argument.",
1397 filename, linenum);
1398 if (!kex_names_valid(arg))
1399 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1400 filename, linenum, arg ? arg : "<NONE>");
1401 if (options->kex_algorithms == NULL)
1402 options->kex_algorithms = xstrdup(arg);
1403 break;
1404
Ben Lindstromade03f62001-12-06 18:22:17 +00001405 case sProtocol:
1406 intptr = &options->protocol;
1407 arg = strdelim(&cp);
1408 if (!arg || *arg == '\0')
1409 fatal("%s line %d: Missing argument.", filename, linenum);
1410 value = proto_spec(arg);
1411 if (value == SSH_PROTO_UNKNOWN)
1412 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001413 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001414 if (*intptr == SSH_PROTO_UNKNOWN)
1415 *intptr = value;
1416 break;
1417
1418 case sSubsystem:
1419 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1420 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001421 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001422 }
1423 arg = strdelim(&cp);
1424 if (!arg || *arg == '\0')
1425 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001426 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001427 if (!*activep) {
1428 arg = strdelim(&cp);
1429 break;
1430 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001431 for (i = 0; i < options->num_subsystems; i++)
1432 if (strcmp(arg, options->subsystem_name[i]) == 0)
1433 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001434 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001435 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1436 arg = strdelim(&cp);
1437 if (!arg || *arg == '\0')
1438 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001439 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001440 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001441
1442 /* Collect arguments (separate to executable) */
1443 p = xstrdup(arg);
1444 len = strlen(p) + 1;
1445 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1446 len += 1 + strlen(arg);
1447 p = xrealloc(p, 1, len);
1448 strlcat(p, " ", len);
1449 strlcat(p, arg, len);
1450 }
1451 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001452 options->num_subsystems++;
1453 break;
1454
1455 case sMaxStartups:
1456 arg = strdelim(&cp);
1457 if (!arg || *arg == '\0')
1458 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001459 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001460 if ((n = sscanf(arg, "%d:%d:%d",
1461 &options->max_startups_begin,
1462 &options->max_startups_rate,
1463 &options->max_startups)) == 3) {
1464 if (options->max_startups_begin >
1465 options->max_startups ||
1466 options->max_startups_rate > 100 ||
1467 options->max_startups_rate < 1)
1468 fatal("%s line %d: Illegal MaxStartups spec.",
1469 filename, linenum);
1470 } else if (n != 1)
1471 fatal("%s line %d: Illegal MaxStartups spec.",
1472 filename, linenum);
1473 else
1474 options->max_startups = options->max_startups_begin;
1475 break;
1476
Darren Tucker89413db2004-05-24 10:36:23 +10001477 case sMaxAuthTries:
1478 intptr = &options->max_authtries;
1479 goto parse_int;
1480
Damien Miller7207f642008-05-19 15:34:50 +10001481 case sMaxSessions:
1482 intptr = &options->max_sessions;
1483 goto parse_int;
1484
Ben Lindstromade03f62001-12-06 18:22:17 +00001485 case sBanner:
1486 charptr = &options->banner;
1487 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001488
Ben Lindstromade03f62001-12-06 18:22:17 +00001489 /*
1490 * These options can contain %X options expanded at
1491 * connect time, so that you can specify paths like:
1492 *
1493 * AuthorizedKeysFile /etc/ssh_keys/%u
1494 */
1495 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001496 if (*activep && options->num_authkeys_files == 0) {
1497 while ((arg = strdelim(&cp)) && *arg != '\0') {
1498 if (options->num_authkeys_files >=
1499 MAX_AUTHKEYS_FILES)
1500 fatal("%s line %d: "
1501 "too many authorized keys files.",
1502 filename, linenum);
1503 options->authorized_keys_files[
1504 options->num_authkeys_files++] =
1505 tilde_expand_filename(arg, getuid());
1506 }
1507 }
1508 return 0;
1509
Damien Miller30da3442010-05-10 11:58:03 +10001510 case sAuthorizedPrincipalsFile:
1511 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001512 arg = strdelim(&cp);
1513 if (!arg || *arg == '\0')
1514 fatal("%s line %d: missing file name.",
1515 filename, linenum);
1516 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001517 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001518 /* increase optional counter */
1519 if (intptr != NULL)
1520 *intptr = *intptr + 1;
1521 }
1522 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001523
1524 case sClientAliveInterval:
1525 intptr = &options->client_alive_interval;
1526 goto parse_time;
1527
1528 case sClientAliveCountMax:
1529 intptr = &options->client_alive_count_max;
1530 goto parse_int;
1531
Darren Tucker46bc0752004-05-02 22:11:30 +10001532 case sAcceptEnv:
1533 while ((arg = strdelim(&cp)) && *arg != '\0') {
1534 if (strchr(arg, '=') != NULL)
1535 fatal("%s line %d: Invalid environment name.",
1536 filename, linenum);
1537 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1538 fatal("%s line %d: too many allow env.",
1539 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001540 if (!*activep)
Damien Millerc24da772012-06-20 21:53:58 +10001541 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001542 options->accept_env[options->num_accept_env++] =
1543 xstrdup(arg);
1544 }
1545 break;
1546
Damien Millerd27b9472005-12-13 19:29:02 +11001547 case sPermitTunnel:
1548 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001549 arg = strdelim(&cp);
1550 if (!arg || *arg == '\0')
1551 fatal("%s line %d: Missing yes/point-to-point/"
1552 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001553 value = -1;
1554 for (i = 0; tunmode_desc[i].val != -1; i++)
1555 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1556 value = tunmode_desc[i].val;
1557 break;
1558 }
1559 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001560 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1561 "no argument: %s", filename, linenum, arg);
1562 if (*intptr == -1)
1563 *intptr = value;
1564 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001565
Darren Tucker45150472006-07-12 22:34:17 +10001566 case sMatch:
1567 if (cmdline)
1568 fatal("Match directive not supported as a command-line "
1569 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001570 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10001571 if (value < 0)
1572 fatal("%s line %d: Bad Match condition", filename,
1573 linenum);
1574 *activep = value;
1575 break;
1576
Damien Miller9b439df2006-07-24 14:04:00 +10001577 case sPermitOpen:
1578 arg = strdelim(&cp);
1579 if (!arg || *arg == '\0')
1580 fatal("%s line %d: missing PermitOpen specification",
1581 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001582 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001583 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001584 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001585 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001586 options->num_permitted_opens = 0;
1587 }
Damien Miller9b439df2006-07-24 14:04:00 +10001588 break;
1589 }
Damien Millerc6081482012-04-22 11:18:53 +10001590 if (strcmp(arg, "none") == 0) {
1591 if (*activep && n == -1) {
Damien Millerc6081482012-04-22 11:18:53 +10001592 options->num_permitted_opens = 1;
1593 channel_disable_adm_local_opens();
1594 }
1595 break;
1596 }
Damien Millera29b95e2007-01-05 16:28:36 +11001597 if (*activep && n == -1)
1598 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001599 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1600 p = hpdelim(&arg);
1601 if (p == NULL)
1602 fatal("%s line %d: missing host in PermitOpen",
1603 filename, linenum);
1604 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001605 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001606 fatal("%s line %d: bad port number in "
1607 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001608 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001609 options->num_permitted_opens =
1610 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001611 }
Damien Miller9b439df2006-07-24 14:04:00 +10001612 break;
1613
Damien Millere2754432006-07-24 14:06:47 +10001614 case sForceCommand:
1615 if (cp == NULL)
1616 fatal("%.200s line %d: Missing argument.", filename,
1617 linenum);
1618 len = strspn(cp, WHITESPACE);
1619 if (*activep && options->adm_forced_command == NULL)
1620 options->adm_forced_command = xstrdup(cp + len);
1621 return 0;
1622
Damien Millerd8cb1f12008-02-10 22:40:12 +11001623 case sChrootDirectory:
1624 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001625
1626 arg = strdelim(&cp);
1627 if (!arg || *arg == '\0')
1628 fatal("%s line %d: missing file name.",
1629 filename, linenum);
1630 if (*activep && *charptr == NULL)
1631 *charptr = xstrdup(arg);
1632 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001633
Damien Miller1aed65e2010-03-04 21:53:35 +11001634 case sTrustedUserCAKeys:
1635 charptr = &options->trusted_user_ca_keys;
1636 goto parse_filename;
1637
1638 case sRevokedKeys:
1639 charptr = &options->revoked_keys_file;
1640 goto parse_filename;
1641
Damien Miller0dac6fb2010-11-20 15:19:38 +11001642 case sIPQoS:
1643 arg = strdelim(&cp);
1644 if ((value = parse_ipqos(arg)) == -1)
1645 fatal("%s line %d: Bad IPQoS value: %s",
1646 filename, linenum, arg);
1647 arg = strdelim(&cp);
1648 if (arg == NULL)
1649 value2 = value;
1650 else if ((value2 = parse_ipqos(arg)) == -1)
1651 fatal("%s line %d: Bad IPQoS value: %s",
1652 filename, linenum, arg);
1653 if (*activep) {
1654 options->ip_qos_interactive = value;
1655 options->ip_qos_bulk = value2;
1656 }
1657 break;
1658
Damien Miller23528812012-04-22 11:24:43 +10001659 case sVersionAddendum:
1660 if (cp == NULL)
1661 fatal("%.200s line %d: Missing argument.", filename,
1662 linenum);
1663 len = strspn(cp, WHITESPACE);
1664 if (*activep && options->version_addendum == NULL) {
1665 if (strcasecmp(cp + len, "none") == 0)
1666 options->version_addendum = xstrdup("");
1667 else if (strchr(cp + len, '\r') != NULL)
1668 fatal("%.200s line %d: Invalid argument",
1669 filename, linenum);
1670 else
1671 options->version_addendum = xstrdup(cp + len);
1672 }
1673 return 0;
1674
Damien Miller09d3e122012-10-31 08:58:58 +11001675 case sAuthorizedKeysCommand:
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001676 if (cp == NULL)
1677 fatal("%.200s line %d: Missing argument.", filename,
1678 linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001679 len = strspn(cp, WHITESPACE);
1680 if (*activep && options->authorized_keys_command == NULL) {
1681 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1682 fatal("%.200s line %d: AuthorizedKeysCommand "
1683 "must be an absolute path",
1684 filename, linenum);
1685 options->authorized_keys_command = xstrdup(cp + len);
1686 }
1687 return 0;
1688
1689 case sAuthorizedKeysCommandUser:
1690 charptr = &options->authorized_keys_command_user;
1691
1692 arg = strdelim(&cp);
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001693 if (!arg || *arg == '\0')
1694 fatal("%s line %d: missing AuthorizedKeysCommandUser "
1695 "argument.", filename, linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001696 if (*activep && *charptr == NULL)
1697 *charptr = xstrdup(arg);
1698 break;
1699
Damien Millera6e3f012012-11-04 23:21:40 +11001700 case sAuthenticationMethods:
1701 if (*activep && options->num_auth_methods == 0) {
1702 while ((arg = strdelim(&cp)) && *arg != '\0') {
1703 if (options->num_auth_methods >=
1704 MAX_AUTH_METHODS)
1705 fatal("%s line %d: "
1706 "too many authentication methods.",
1707 filename, linenum);
1708 if (auth2_methods_valid(arg, 0) != 0)
1709 fatal("%s line %d: invalid "
1710 "authentication method list.",
1711 filename, linenum);
1712 options->auth_methods[
1713 options->num_auth_methods++] = xstrdup(arg);
1714 }
1715 }
1716 return 0;
1717
Damien Miller7acefbb2014-07-18 14:11:24 +10001718 case sStreamLocalBindMask:
1719 arg = strdelim(&cp);
1720 if (!arg || *arg == '\0')
1721 fatal("%s line %d: missing StreamLocalBindMask argument.",
1722 filename, linenum);
1723 /* Parse mode in octal format */
1724 value = strtol(arg, &p, 8);
1725 if (arg == p || value < 0 || value > 0777)
1726 fatal("%s line %d: Bad mask.", filename, linenum);
1727 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1728 break;
1729
1730 case sStreamLocalBindUnlink:
1731 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1732 goto parse_flag;
1733
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001734 case sFingerprintHash:
1735 arg = strdelim(&cp);
1736 if (!arg || *arg == '\0')
1737 fatal("%.200s line %d: Missing argument.",
1738 filename, linenum);
1739 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1740 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1741 filename, linenum, arg);
1742 if (*activep)
1743 options->fingerprint_hash = value;
1744 break;
1745
Ben Lindstromade03f62001-12-06 18:22:17 +00001746 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001747 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001748 filename, linenum, arg);
1749 while (arg)
1750 arg = strdelim(&cp);
1751 break;
1752
Damien Millerf9b3feb2003-05-16 11:38:32 +10001753 case sUnsupported:
1754 logit("%s line %d: Unsupported option %s",
1755 filename, linenum, arg);
1756 while (arg)
1757 arg = strdelim(&cp);
1758 break;
1759
Ben Lindstromade03f62001-12-06 18:22:17 +00001760 default:
1761 fatal("%s line %d: Missing handler for opcode %s (%d)",
1762 filename, linenum, arg, opcode);
1763 }
1764 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1765 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1766 filename, linenum, arg);
1767 return 0;
1768}
1769
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001770/* Reads the server configuration file. */
1771
Damien Miller4af51302000-04-16 11:18:38 +10001772void
Darren Tucker645ab752004-06-25 13:33:20 +10001773load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001774{
Damien Miller46cb75a2012-07-31 12:22:37 +10001775 char line[4096], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001776 FILE *f;
Damien Miller46cb75a2012-07-31 12:22:37 +10001777 int lineno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001778
Darren Tucker645ab752004-06-25 13:33:20 +10001779 debug2("%s: filename %s", __func__, filename);
1780 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001781 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001782 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001783 }
Darren Tucker645ab752004-06-25 13:33:20 +10001784 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001785 while (fgets(line, sizeof(line), f)) {
Damien Miller46cb75a2012-07-31 12:22:37 +10001786 lineno++;
1787 if (strlen(line) == sizeof(line) - 1)
1788 fatal("%s line %d too long", filename, lineno);
Darren Tucker645ab752004-06-25 13:33:20 +10001789 /*
1790 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001791 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001792 * line numbers later for error messages
1793 */
1794 if ((cp = strchr(line, '#')) != NULL)
1795 memcpy(cp, "\n", 2);
1796 cp = line + strspn(line, " \t\r");
1797
1798 buffer_append(conf, cp, strlen(cp));
1799 }
1800 buffer_append(conf, "\0", 1);
1801 fclose(f);
1802 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1803}
1804
1805void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001806parse_server_match_config(ServerOptions *options,
1807 struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10001808{
Darren Tucker45150472006-07-12 22:34:17 +10001809 ServerOptions mo;
1810
1811 initialize_server_options(&mo);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001812 parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11001813 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001814}
1815
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001816int parse_server_match_testspec(struct connection_info *ci, char *spec)
1817{
1818 char *p;
1819
1820 while ((p = strsep(&spec, ",")) && *p != '\0') {
1821 if (strncmp(p, "addr=", 5) == 0) {
1822 ci->address = xstrdup(p + 5);
1823 } else if (strncmp(p, "host=", 5) == 0) {
1824 ci->host = xstrdup(p + 5);
1825 } else if (strncmp(p, "user=", 5) == 0) {
1826 ci->user = xstrdup(p + 5);
1827 } else if (strncmp(p, "laddr=", 6) == 0) {
1828 ci->laddress = xstrdup(p + 6);
1829 } else if (strncmp(p, "lport=", 6) == 0) {
1830 ci->lport = a2port(p + 6);
1831 if (ci->lport == -1) {
1832 fprintf(stderr, "Invalid port '%s' in test mode"
1833 " specification %s\n", p+6, p);
1834 return -1;
1835 }
1836 } else {
1837 fprintf(stderr, "Invalid test mode specification %s\n",
1838 p);
1839 return -1;
1840 }
1841 }
1842 return 0;
1843}
1844
1845/*
1846 * returns 1 for a complete spec, 0 for partial spec and -1 for an
1847 * empty spec.
1848 */
1849int server_match_spec_complete(struct connection_info *ci)
1850{
1851 if (ci->user && ci->host && ci->address)
1852 return 1; /* complete */
1853 if (!ci->user && !ci->host && !ci->address)
1854 return -1; /* empty */
1855 return 0; /* partial */
1856}
1857
Darren Tucker1629c072007-02-19 22:25:37 +11001858/*
1859 * Copy any supported values that are set.
1860 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001861 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001862 * array values that are not used pre-authentication, because any that we
1863 * do use must be explictly sent in mm_getpwnamallow().
1864 */
Darren Tucker45150472006-07-12 22:34:17 +10001865void
Darren Tucker1629c072007-02-19 22:25:37 +11001866copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001867{
Damien Miller534b2cc2013-12-05 14:07:27 +11001868#define M_CP_INTOPT(n) do {\
1869 if (src->n != -1) \
1870 dst->n = src->n; \
1871} while (0)
1872
Darren Tucker1629c072007-02-19 22:25:37 +11001873 M_CP_INTOPT(password_authentication);
1874 M_CP_INTOPT(gss_authentication);
1875 M_CP_INTOPT(rsa_authentication);
1876 M_CP_INTOPT(pubkey_authentication);
1877 M_CP_INTOPT(kerberos_authentication);
1878 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001879 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001880 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001881 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001882 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001883
1884 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10001885 M_CP_INTOPT(allow_streamlocal_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001886 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001887 M_CP_INTOPT(permit_tun);
Damien Miller7acefbb2014-07-18 14:11:24 +10001888 M_CP_INTOPT(fwd_opts.gateway_ports);
Darren Tucker1629c072007-02-19 22:25:37 +11001889 M_CP_INTOPT(x11_display_offset);
1890 M_CP_INTOPT(x11_forwarding);
1891 M_CP_INTOPT(x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11001892 M_CP_INTOPT(permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10001893 M_CP_INTOPT(permit_user_rc);
Damien Miller7207f642008-05-19 15:34:50 +10001894 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001895 M_CP_INTOPT(max_authtries);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001896 M_CP_INTOPT(ip_qos_interactive);
1897 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001898 M_CP_INTOPT(rekey_limit);
1899 M_CP_INTOPT(rekey_interval);
Darren Tucker1629c072007-02-19 22:25:37 +11001900
Damien Miller534b2cc2013-12-05 14:07:27 +11001901 /* M_CP_STROPT and M_CP_STRARRAYOPT should not appear before here */
1902#define M_CP_STROPT(n) do {\
1903 if (src->n != NULL && dst->n != src->n) { \
1904 free(dst->n); \
1905 dst->n = src->n; \
1906 } \
1907} while(0)
1908#define M_CP_STRARRAYOPT(n, num_n) do {\
1909 if (src->num_n != 0) { \
1910 for (dst->num_n = 0; dst->num_n < src->num_n; dst->num_n++) \
1911 dst->n[dst->num_n] = xstrdup(src->n[dst->num_n]); \
1912 } \
1913} while(0)
1914
Damien Millerf2e407e2011-05-20 19:04:14 +10001915 /* See comment in servconf.h */
1916 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10001917
Damien Millerc2411902011-05-20 19:03:49 +10001918 /*
1919 * The only things that should be below this point are string options
1920 * which are only used after authentication.
1921 */
Damien Miller5d74e582011-05-20 19:03:31 +10001922 if (preauth)
1923 return;
Damien Millerd8478b62011-05-29 21:39:36 +10001924
Damien Miller5d74e582011-05-20 19:03:31 +10001925 M_CP_STROPT(adm_forced_command);
1926 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001927}
1928
Darren Tucker1629c072007-02-19 22:25:37 +11001929#undef M_CP_INTOPT
1930#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10001931#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11001932
Darren Tucker45150472006-07-12 22:34:17 +10001933void
1934parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001935 struct connection_info *connectinfo)
Darren Tucker45150472006-07-12 22:34:17 +10001936{
1937 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001938 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001939
1940 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1941
Darren Tucker9fbac712004-08-12 22:41:44 +10001942 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001943 active = connectinfo ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001944 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001945 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001946 if (process_server_config_line(options, cp, filename,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001947 linenum++, &active, connectinfo) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001948 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001949 }
Darren Tuckera627d422013-06-02 07:31:17 +10001950 free(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001951 if (bad_options > 0)
1952 fatal("%s: terminating, %d bad configuration options",
1953 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001954}
Darren Tuckere7140f22008-06-10 23:01:51 +10001955
1956static const char *
Damien Miller82c55872011-06-23 08:20:30 +10001957fmt_multistate_int(int val, const struct multistate *m)
1958{
1959 u_int i;
1960
1961 for (i = 0; m[i].key != NULL; i++) {
1962 if (m[i].value == val)
1963 return m[i].key;
1964 }
1965 return "UNKNOWN";
1966}
1967
1968static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10001969fmt_intarg(ServerOpCodes code, int val)
1970{
Damien Miller82c55872011-06-23 08:20:30 +10001971 if (val == -1)
1972 return "unset";
1973 switch (code) {
1974 case sAddressFamily:
1975 return fmt_multistate_int(val, multistate_addressfamily);
1976 case sPermitRootLogin:
1977 return fmt_multistate_int(val, multistate_permitrootlogin);
1978 case sGatewayPorts:
1979 return fmt_multistate_int(val, multistate_gatewayports);
1980 case sCompression:
1981 return fmt_multistate_int(val, multistate_compression);
Damien Miller69ff1df2011-06-23 08:30:03 +10001982 case sUsePrivilegeSeparation:
1983 return fmt_multistate_int(val, multistate_privsep);
Damien Milleraa5b3f82012-12-03 09:50:54 +11001984 case sAllowTcpForwarding:
1985 return fmt_multistate_int(val, multistate_tcpfwd);
Damien Miller7acefbb2014-07-18 14:11:24 +10001986 case sAllowStreamLocalForwarding:
1987 return fmt_multistate_int(val, multistate_tcpfwd);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001988 case sFingerprintHash:
1989 return ssh_digest_alg_name(val);
Damien Miller82c55872011-06-23 08:20:30 +10001990 case sProtocol:
Darren Tuckere7140f22008-06-10 23:01:51 +10001991 switch (val) {
1992 case SSH_PROTO_1:
1993 return "1";
1994 case SSH_PROTO_2:
1995 return "2";
1996 case (SSH_PROTO_1|SSH_PROTO_2):
1997 return "2,1";
1998 default:
1999 return "UNKNOWN";
2000 }
Damien Miller82c55872011-06-23 08:20:30 +10002001 default:
2002 switch (val) {
2003 case 0:
2004 return "no";
2005 case 1:
2006 return "yes";
2007 default:
2008 return "UNKNOWN";
2009 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002010 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002011}
2012
2013static const char *
2014lookup_opcode_name(ServerOpCodes code)
2015{
2016 u_int i;
2017
2018 for (i = 0; keywords[i].name != NULL; i++)
2019 if (keywords[i].opcode == code)
2020 return(keywords[i].name);
2021 return "UNKNOWN";
2022}
2023
2024static void
2025dump_cfg_int(ServerOpCodes code, int val)
2026{
2027 printf("%s %d\n", lookup_opcode_name(code), val);
2028}
2029
2030static void
2031dump_cfg_fmtint(ServerOpCodes code, int val)
2032{
2033 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2034}
2035
2036static void
2037dump_cfg_string(ServerOpCodes code, const char *val)
2038{
2039 if (val == NULL)
2040 return;
djm@openbsd.org161cf412014-12-22 07:55:51 +00002041 printf("%s %s\n", lookup_opcode_name(code),
2042 val == NULL ? "none" : val);
Darren Tuckere7140f22008-06-10 23:01:51 +10002043}
2044
2045static void
2046dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
2047{
2048 u_int i;
2049
2050 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10002051 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2052}
2053
2054static void
2055dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
2056{
2057 u_int i;
2058
2059 printf("%s", lookup_opcode_name(code));
2060 for (i = 0; i < count; i++)
2061 printf(" %s", vals[i]);
2062 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002063}
2064
2065void
2066dump_config(ServerOptions *o)
2067{
2068 u_int i;
2069 int ret;
2070 struct addrinfo *ai;
2071 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
2072
2073 /* these are usually at the top of the config */
2074 for (i = 0; i < o->num_ports; i++)
2075 printf("port %d\n", o->ports[i]);
2076 dump_cfg_fmtint(sProtocol, o->protocol);
2077 dump_cfg_fmtint(sAddressFamily, o->address_family);
2078
2079 /* ListenAddress must be after Port */
2080 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
2081 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
2082 sizeof(addr), port, sizeof(port),
2083 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
2084 error("getnameinfo failed: %.100s",
2085 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
2086 strerror(errno));
2087 } else {
2088 if (ai->ai_family == AF_INET6)
2089 printf("listenaddress [%s]:%s\n", addr, port);
2090 else
2091 printf("listenaddress %s:%s\n", addr, port);
2092 }
2093 }
2094
2095 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10002096#ifdef USE_PAM
2097 dump_cfg_int(sUsePAM, o->use_pam);
2098#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002099 dump_cfg_int(sServerKeyBits, o->server_key_bits);
2100 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
2101 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
2102 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
2103 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11002104 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10002105 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
2106 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
2107
2108 /* formatted integer arguments */
2109 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
2110 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
2111 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
2112 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2113 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
2114 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
2115 o->hostbased_uses_name_from_packet_only);
2116 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
2117 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10002118#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10002119 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
2120 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
2121 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10002122# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10002123 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10002124# endif
2125#endif
2126#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10002127 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2128 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10002129#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002130 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2131 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2132 o->kbd_interactive_authentication);
2133 dump_cfg_fmtint(sChallengeResponseAuthentication,
2134 o->challenge_response_authentication);
2135 dump_cfg_fmtint(sPrintMotd, o->print_motd);
2136 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
2137 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
2138 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002139 dump_cfg_fmtint(sPermitTTY, o->permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002140 dump_cfg_fmtint(sPermitUserRC, o->permit_user_rc);
Darren Tuckere7140f22008-06-10 23:01:51 +10002141 dump_cfg_fmtint(sStrictModes, o->strict_modes);
2142 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
2143 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
2144 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
2145 dump_cfg_fmtint(sUseLogin, o->use_login);
2146 dump_cfg_fmtint(sCompression, o->compression);
Damien Miller7acefbb2014-07-18 14:11:24 +10002147 dump_cfg_fmtint(sGatewayPorts, o->fwd_opts.gateway_ports);
Darren Tuckere7140f22008-06-10 23:01:51 +10002148 dump_cfg_fmtint(sUseDNS, o->use_dns);
2149 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002150 dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
Darren Tuckere7140f22008-06-10 23:01:51 +10002151 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002152 dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
Darren Tuckere7140f22008-06-10 23:01:51 +10002153
2154 /* string arguments */
2155 dump_cfg_string(sPidFile, o->pid_file);
2156 dump_cfg_string(sXAuthLocation, o->xauth_location);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002157 dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
2158 dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
Darren Tuckere7140f22008-06-10 23:01:51 +10002159 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10002160 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11002161 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11002162 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
2163 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10002164 dump_cfg_string(sAuthorizedPrincipalsFile,
2165 o->authorized_principals_file);
Damien Miller23528812012-04-22 11:24:43 +10002166 dump_cfg_string(sVersionAddendum, o->version_addendum);
Damien Miller09d3e122012-10-31 08:58:58 +11002167 dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
2168 dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
Damien Miller85b45e02013-07-20 13:21:52 +10002169 dump_cfg_string(sHostKeyAgent, o->host_key_agent);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002170 dump_cfg_string(sKexAlgorithms,
djm@openbsd.org259a02e2014-10-13 00:38:35 +00002171 o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002172 dump_cfg_string(sHostbasedAcceptedKeyTypes, o->hostbased_key_types ?
2173 o->hostbased_key_types : KEX_DEFAULT_PK_ALG);
2174 dump_cfg_string(sPubkeyAcceptedKeyTypes, o->pubkey_key_types ?
2175 o->pubkey_key_types : KEX_DEFAULT_PK_ALG);
Darren Tuckere7140f22008-06-10 23:01:51 +10002176
2177 /* string arguments requiring a lookup */
2178 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
2179 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
2180
2181 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10002182 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
2183 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002184 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
2185 o->host_key_files);
Damien Miller0a80ca12010-02-27 07:55:05 +11002186 dump_cfg_strarray(sHostKeyFile, o->num_host_cert_files,
2187 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002188 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
2189 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
2190 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
2191 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
2192 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
Damien Millera6e3f012012-11-04 23:21:40 +11002193 dump_cfg_strarray_oneline(sAuthenticationMethods,
2194 o->num_auth_methods, o->auth_methods);
Darren Tuckere7140f22008-06-10 23:01:51 +10002195
2196 /* other arguments */
2197 for (i = 0; i < o->num_subsystems; i++)
2198 printf("subsystem %s %s\n", o->subsystem_name[i],
2199 o->subsystem_args[i]);
2200
2201 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
2202 o->max_startups_rate, o->max_startups);
2203
2204 for (i = 0; tunmode_desc[i].val != -1; i++)
2205 if (tunmode_desc[i].val == o->permit_tun) {
2206 s = tunmode_desc[i].text;
2207 break;
2208 }
2209 dump_cfg_string(sPermitTunnel, s);
2210
Damien Miller91475862011-05-05 14:14:34 +10002211 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2212 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11002213
Damien Millera6d6c1f2013-08-21 02:40:01 +10002214 printf("rekeylimit %lld %d\n", (long long)o->rekey_limit,
2215 o->rekey_interval);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002216
Darren Tuckere7140f22008-06-10 23:01:51 +10002217 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10002218}