blob: 1b6bdb4af276da81089696e0c2e5b44eb45b88b2 [file] [log] [blame]
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002/* $OpenBSD: servconf.c,v 1.258 2015/01/13 07:39:19 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003/*
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
Damien Millere3b60b52006-07-10 21:08:03 +100016#include <sys/types.h>
17#include <sys/socket.h>
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>
Damien Millerd7834352006-08-05 12:39:39 +100031#include <stdarg.h>
Darren Tuckere7140f22008-06-10 23:01:51 +100032#include <errno.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100033#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100034#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100035#endif
Damien Millerbe43ebf2006-07-24 13:51:51 +100036
Damien Millerb84886b2008-05-19 15:05:07 +100037#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100041#include "buffer.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100042#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043#include "servconf.h"
Damien Miller78928792000-04-12 20:17:38 +100044#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100047#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000048#include "kex.h"
49#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100050#include "match.h"
Damien Miller9b439df2006-07-24 14:04:00 +100051#include "channels.h"
Damien Miller565ca3f2006-08-19 00:23:15 +100052#include "groupaccess.h"
Darren Tuckerfbcf8272012-05-19 19:37:01 +100053#include "canohost.h"
54#include "packet.h"
Damien Millera6e3f012012-11-04 23:21:40 +110055#include "hostfile.h"
56#include "auth.h"
djm@openbsd.org57d378e2014-08-19 23:58:28 +000057#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000058#include "digest.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000059
Damien Miller3dc71ad2009-01-28 16:31:22 +110060static void add_listen_addr(ServerOptions *, char *, int);
61static void add_one_listen_addr(ServerOptions *, char *, int);
Damien Miller34132e52000-01-14 15:45:46 +110062
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000063/* Use of privilege separation or not */
64extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100065extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067/* Initializes the server options to their default values. */
68
Damien Miller4af51302000-04-16 11:18:38 +100069void
Damien Miller95def091999-11-25 00:26:21 +110070initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071{
Damien Miller95def091999-11-25 00:26:21 +110072 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110073
74 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100075 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110076
77 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110078 options->num_ports = 0;
79 options->ports_from_cmdline = 0;
80 options->listen_addrs = NULL;
Darren Tucker0f383232005-01-20 10:57:56 +110081 options->address_family = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110082 options->num_host_key_files = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110083 options->num_host_cert_files = 0;
Damien Miller85b45e02013-07-20 13:21:52 +100084 options->host_key_agent = NULL;
Damien Miller6f83b8e2000-05-02 09:23:45 +100085 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110086 options->server_key_bits = -1;
87 options->login_grace_time = -1;
88 options->key_regeneration_time = -1;
Ben Lindstromd8a90212001-02-15 03:08:27 +000089 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110090 options->ignore_rhosts = -1;
91 options->ignore_user_known_hosts = -1;
92 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +000093 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +110094 options->x11_forwarding = -1;
95 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +110096 options->x11_use_localhost = -1;
Damien Miller5ff30c62013-10-30 22:21:50 +110097 options->permit_tty = -1;
Damien Miller72e6b5c2014-07-04 09:00:04 +100098 options->permit_user_rc = -1;
Damien Millerd3a18572000-06-07 19:55:44 +100099 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100100 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +1100101 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +1100102 options->log_facility = SYSLOG_FACILITY_NOT_SET;
103 options->log_level = SYSLOG_LEVEL_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +1100104 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000105 options->hostbased_authentication = -1;
106 options->hostbased_uses_name_from_packet_only = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000107 options->hostbased_key_types = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100108 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100109 options->pubkey_authentication = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000110 options->pubkey_key_types = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100111 options->kerberos_authentication = -1;
112 options->kerberos_or_local_passwd = -1;
113 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100114 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000115 options->gss_authentication=-1;
116 options->gss_cleanup_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100117 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100118 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000119 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100120 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000121 options->permit_user_env = -1;
Damien Miller95def091999-11-25 00:26:21 +1100122 options->use_login = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000123 options->compression = -1;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000124 options->rekey_limit = -1;
125 options->rekey_interval = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100126 options->allow_tcp_forwarding = -1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000127 options->allow_streamlocal_forwarding = -1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000128 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100129 options->num_allow_users = 0;
130 options->num_deny_users = 0;
131 options->num_allow_groups = 0;
132 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000133 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000134 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000135 options->kex_algorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000136 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller7acefbb2014-07-18 14:11:24 +1000137 options->fwd_opts.gateway_ports = -1;
138 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
139 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000140 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000141 options->max_startups_begin = -1;
142 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000143 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000144 options->max_authtries = -1;
Damien Miller7207f642008-05-19 15:34:50 +1000145 options->max_sessions = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000146 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000147 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000148 options->client_alive_interval = -1;
149 options->client_alive_count_max = -1;
Damien Millerd8478b62011-05-29 21:39:36 +1000150 options->num_authkeys_files = 0;
Darren Tucker46bc0752004-05-02 22:11:30 +1000151 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100152 options->permit_tun = -1;
Damien Millera765cf42006-07-24 14:08:13 +1000153 options->num_permitted_opens = -1;
Damien Millere2754432006-07-24 14:06:47 +1000154 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100155 options->chroot_directory = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +1100156 options->authorized_keys_command = NULL;
157 options->authorized_keys_command_user = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100158 options->revoked_keys_file = NULL;
159 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000160 options->authorized_principals_file = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100161 options->ip_qos_interactive = -1;
162 options->ip_qos_bulk = -1;
Damien Miller23528812012-04-22 11:24:43 +1000163 options->version_addendum = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000164 options->fingerprint_hash = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165}
166
djm@openbsd.org161cf412014-12-22 07:55:51 +0000167/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
168static int
169option_clear_or_none(const char *o)
170{
171 return o == NULL || strcasecmp(o, "none") == 0;
172}
173
Damien Miller4af51302000-04-16 11:18:38 +1000174void
Damien Miller95def091999-11-25 00:26:21 +1100175fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176{
djm@openbsd.org161cf412014-12-22 07:55:51 +0000177 int i;
178
Damien Miller726273e2001-11-12 11:40:11 +1100179 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000180 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000181 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100182
183 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100184 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +1100185 options->protocol = SSH_PROTO_2;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100186 if (options->num_host_key_files == 0) {
187 /* fill default hostkeys for protocols */
188 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100189 options->host_key_files[options->num_host_key_files++] =
190 _PATH_HOST_KEY_FILE;
191 if (options->protocol & SSH_PROTO_2) {
192 options->host_key_files[options->num_host_key_files++] =
193 _PATH_HOST_RSA_KEY_FILE;
194 options->host_key_files[options->num_host_key_files++] =
195 _PATH_HOST_DSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100196#ifdef OPENSSL_HAS_ECC
Damien Millere13cadf2010-09-10 11:15:33 +1000197 options->host_key_files[options->num_host_key_files++] =
198 _PATH_HOST_ECDSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100199#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +1100200 options->host_key_files[options->num_host_key_files++] =
201 _PATH_HOST_ED25519_KEY_FILE;
Damien Miller7fc23732002-01-22 23:19:11 +1100202 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100203 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100204 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100205 if (options->num_ports == 0)
206 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
207 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000208 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000209 if (options->pid_file == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000210 options->pid_file = xstrdup(_PATH_SSH_DAEMON_PID_FILE);
Damien Miller95def091999-11-25 00:26:21 +1100211 if (options->server_key_bits == -1)
Darren Tucker7499b0c2008-07-02 22:35:43 +1000212 options->server_key_bits = 1024;
Damien Miller95def091999-11-25 00:26:21 +1100213 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000214 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100215 if (options->key_regeneration_time == -1)
216 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000217 if (options->permit_root_login == PERMIT_NOT_SET)
218 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100219 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100220 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100221 if (options->ignore_user_known_hosts == -1)
222 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100223 if (options->print_motd == -1)
224 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000225 if (options->print_lastlog == -1)
226 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100227 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100228 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100229 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100230 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100231 if (options->x11_use_localhost == -1)
232 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000233 if (options->xauth_location == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000234 options->xauth_location = xstrdup(_PATH_XAUTH);
Damien Miller5ff30c62013-10-30 22:21:50 +1100235 if (options->permit_tty == -1)
236 options->permit_tty = 1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000237 if (options->permit_user_rc == -1)
238 options->permit_user_rc = 1;
Damien Miller95def091999-11-25 00:26:21 +1100239 if (options->strict_modes == -1)
240 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100241 if (options->tcp_keep_alive == -1)
242 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100243 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100244 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100245 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000246 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100247 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100248 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000249 if (options->hostbased_authentication == -1)
250 options->hostbased_authentication = 0;
251 if (options->hostbased_uses_name_from_packet_only == -1)
252 options->hostbased_uses_name_from_packet_only = 0;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000253 if (options->hostbased_key_types == NULL)
254 options->hostbased_key_types = xstrdup("*");
Damien Miller95def091999-11-25 00:26:21 +1100255 if (options->rsa_authentication == -1)
256 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100257 if (options->pubkey_authentication == -1)
258 options->pubkey_authentication = 1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000259 if (options->pubkey_key_types == NULL)
260 options->pubkey_key_types = xstrdup("*");
Damien Miller95def091999-11-25 00:26:21 +1100261 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000262 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100263 if (options->kerberos_or_local_passwd == -1)
264 options->kerberos_or_local_passwd = 1;
265 if (options->kerberos_ticket_cleanup == -1)
266 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100267 if (options->kerberos_get_afs_token == -1)
268 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000269 if (options->gss_authentication == -1)
270 options->gss_authentication = 0;
271 if (options->gss_cleanup_creds == -1)
272 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100273 if (options->password_authentication == -1)
274 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100275 if (options->kbd_interactive_authentication == -1)
276 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000277 if (options->challenge_response_authentication == -1)
278 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100279 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100280 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000281 if (options->permit_user_env == -1)
282 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100283 if (options->use_login == -1)
284 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000285 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000286 options->compression = COMP_DELAYED;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000287 if (options->rekey_limit == -1)
288 options->rekey_limit = 0;
289 if (options->rekey_interval == -1)
290 options->rekey_interval = 0;
Damien Miller50a41ed2000-10-16 12:14:42 +1100291 if (options->allow_tcp_forwarding == -1)
Damien Milleraa5b3f82012-12-03 09:50:54 +1100292 options->allow_tcp_forwarding = FORWARD_ALLOW;
Damien Miller7acefbb2014-07-18 14:11:24 +1000293 if (options->allow_streamlocal_forwarding == -1)
294 options->allow_streamlocal_forwarding = FORWARD_ALLOW;
Damien Miller4f755cd2008-05-19 14:57:41 +1000295 if (options->allow_agent_forwarding == -1)
296 options->allow_agent_forwarding = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000297 if (options->fwd_opts.gateway_ports == -1)
298 options->fwd_opts.gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000299 if (options->max_startups == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100300 options->max_startups = 100;
Damien Miller942da032000-08-18 13:59:06 +1000301 if (options->max_startups_rate == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100302 options->max_startups_rate = 30; /* 30% */
Damien Miller942da032000-08-18 13:59:06 +1000303 if (options->max_startups_begin == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100304 options->max_startups_begin = 10;
Darren Tucker89413db2004-05-24 10:36:23 +1000305 if (options->max_authtries == -1)
306 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000307 if (options->max_sessions == -1)
308 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000309 if (options->use_dns == -1)
310 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000311 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100312 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000313 if (options->client_alive_count_max == -1)
314 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000315 if (options->num_authkeys_files == 0) {
316 options->authorized_keys_files[options->num_authkeys_files++] =
317 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS);
318 options->authorized_keys_files[options->num_authkeys_files++] =
319 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS2);
320 }
Damien Millerd27b9472005-12-13 19:29:02 +1100321 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100322 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100323 if (options->ip_qos_interactive == -1)
324 options->ip_qos_interactive = IPTOS_LOWDELAY;
325 if (options->ip_qos_bulk == -1)
326 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller23528812012-04-22 11:24:43 +1000327 if (options->version_addendum == NULL)
328 options->version_addendum = xstrdup("");
Damien Miller7acefbb2014-07-18 14:11:24 +1000329 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
330 options->fwd_opts.streamlocal_bind_mask = 0177;
331 if (options->fwd_opts.streamlocal_bind_unlink == -1)
332 options->fwd_opts.streamlocal_bind_unlink = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000333 if (options->fingerprint_hash == -1)
334 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
Ben Lindstromfb62a692002-06-06 19:47:11 +0000335 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000336 if (use_privsep == -1)
Damien Miller5a5c2b92012-07-31 12:21:34 +1000337 use_privsep = PRIVSEP_NOSANDBOX;
Damien Miller4903eb42002-06-21 16:20:44 +1000338
djm@openbsd.org161cf412014-12-22 07:55:51 +0000339#define CLEAR_ON_NONE(v) \
340 do { \
341 if (option_clear_or_none(v)) { \
342 free(v); \
343 v = NULL; \
344 } \
345 } while(0)
346 CLEAR_ON_NONE(options->pid_file);
347 CLEAR_ON_NONE(options->xauth_location);
348 CLEAR_ON_NONE(options->banner);
349 CLEAR_ON_NONE(options->trusted_user_ca_keys);
350 CLEAR_ON_NONE(options->revoked_keys_file);
351 for (i = 0; i < options->num_host_key_files; i++)
352 CLEAR_ON_NONE(options->host_key_files[i]);
353 for (i = 0; i < options->num_host_cert_files; i++)
354 CLEAR_ON_NONE(options->host_cert_files[i]);
355#undef CLEAR_ON_NONE
356
Tim Rice40017b02002-07-14 13:36:49 -0700357#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000358 if (use_privsep && options->compression == 1) {
359 error("This platform does not support both privilege "
360 "separation and compression");
361 error("Compression disabled");
362 options->compression = 0;
363 }
364#endif
365
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366}
367
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000368/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100369typedef enum {
370 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100371 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000372 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100373 /* Standard Options */
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000374 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime,
375 sKeyRegenerationTime, sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000376 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100377 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100378 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000379 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100380 sPasswordAuthentication, sKbdInteractiveAuthentication,
381 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000382 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100383 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller5ff30c62013-10-30 22:21:50 +1100384 sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000385 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000386 sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000387 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000388 sGatewayPorts, sPubkeyAuthentication, sPubkeyAcceptedKeyTypes,
389 sXAuthLocation, sSubsystem, sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000390 sBanner, sUseDNS, sHostbasedAuthentication,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000391 sHostbasedUsesNameFromPacketOnly, sHostbasedAcceptedKeyTypes,
392 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
Damien Millerd27b9472005-12-13 19:29:02 +1100393 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100394 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100395 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller7cc194f2014-02-04 11:12:56 +1100396 sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000397 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Miller23528812012-04-22 11:24:43 +1000398 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Miller09d3e122012-10-31 08:58:58 +1100399 sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
Damien Miller72e6b5c2014-07-04 09:00:04 +1000400 sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
Damien Miller7acefbb2014-07-18 14:11:24 +1000401 sStreamLocalBindMask, sStreamLocalBindUnlink,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000402 sAllowStreamLocalForwarding, sFingerprintHash,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000403 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404} ServerOpCodes;
405
Darren Tucker45150472006-07-12 22:34:17 +1000406#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
407#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
408#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
409
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100411static struct {
412 const char *name;
413 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000414 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100415} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100416 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000417#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000418 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000419#else
Darren Tucker45150472006-07-12 22:34:17 +1000420 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000421#endif
Darren Tucker45150472006-07-12 22:34:17 +1000422 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100423 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000424 { "port", sPort, SSHCFG_GLOBAL },
425 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
426 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
Damien Miller85b45e02013-07-20 13:21:52 +1000427 { "hostkeyagent", sHostKeyAgent, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000428 { "pidfile", sPidFile, SSHCFG_GLOBAL },
429 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
430 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
431 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100432 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000433 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
434 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
435 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100436 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
437 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000438 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000439 { "hostbasedacceptedkeytypes", sHostbasedAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100440 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
441 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000442 { "pubkeyacceptedkeytypes", sPubkeyAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000443 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000444#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100445 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000446 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
447 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100448#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000449 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000450#else
Darren Tucker45150472006-07-12 22:34:17 +1000451 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100452#endif
453#else
Darren Tucker1629c072007-02-19 22:25:37 +1100454 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000455 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
456 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
457 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000458#endif
Darren Tucker45150472006-07-12 22:34:17 +1000459 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
460 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000461#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100462 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000463 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000464#else
Darren Tucker1629c072007-02-19 22:25:37 +1100465 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000466 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000467#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100468 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
469 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100470 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000471 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
472 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
473 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
474 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
475 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
476 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
477 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
478 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000479 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
480 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
481 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000482 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
483 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100484 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000485 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
486 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
487 { "compression", sCompression, SSHCFG_GLOBAL },
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000488 { "rekeylimit", sRekeyLimit, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000489 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
490 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
491 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000492 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Damien Millerc24da772012-06-20 21:53:58 +1000493 { "allowusers", sAllowUsers, SSHCFG_ALL },
494 { "denyusers", sDenyUsers, SSHCFG_ALL },
495 { "allowgroups", sAllowGroups, SSHCFG_ALL },
496 { "denygroups", sDenyGroups, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000497 { "ciphers", sCiphers, SSHCFG_GLOBAL },
498 { "macs", sMacs, SSHCFG_GLOBAL },
499 { "protocol", sProtocol, SSHCFG_GLOBAL },
500 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
501 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
502 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000503 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000504 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100505 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000506 { "usedns", sUseDNS, SSHCFG_GLOBAL },
507 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
508 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
509 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
510 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000511 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000512 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000513 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Damien Millerc24da772012-06-20 21:53:58 +1000514 { "acceptenv", sAcceptEnv, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000515 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Damien Miller5ff30c62013-10-30 22:21:50 +1100516 { "permittty", sPermitTTY, SSHCFG_ALL },
Damien Miller72e6b5c2014-07-04 09:00:04 +1000517 { "permituserrc", sPermitUserRC, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000518 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000519 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000520 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100521 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100522 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100523 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
524 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000525 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000526 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100527 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller09d3e122012-10-31 08:58:58 +1100528 { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
529 { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000530 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Damien Millera6e3f012012-11-04 23:21:40 +1100531 { "authenticationmethods", sAuthenticationMethods, SSHCFG_ALL },
Damien Miller7acefbb2014-07-18 14:11:24 +1000532 { "streamlocalbindmask", sStreamLocalBindMask, SSHCFG_ALL },
533 { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
534 { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000535 { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000536 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537};
538
Darren Tuckere7140f22008-06-10 23:01:51 +1000539static struct {
540 int val;
541 char *text;
542} tunmode_desc[] = {
543 { SSH_TUNMODE_NO, "no" },
544 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
545 { SSH_TUNMODE_ETHERNET, "ethernet" },
546 { SSH_TUNMODE_YES, "yes" },
547 { -1, NULL }
548};
549
Damien Miller5428f641999-11-25 11:54:57 +1100550/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000551 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100552 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553
Damien Miller4af51302000-04-16 11:18:38 +1000554static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100555parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000556 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000558 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559
Damien Miller95def091999-11-25 00:26:21 +1100560 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000561 if (strcasecmp(cp, keywords[i].name) == 0) {
562 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100563 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000564 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000566 error("%s: line %d: Bad configuration option: %s",
567 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100568 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569}
570
Darren Tucker88b6fb22010-01-13 22:44:29 +1100571char *
572derelativise_path(const char *path)
573{
Damien Miller44451d02010-03-26 10:40:04 +1100574 char *expanded, *ret, cwd[MAXPATHLEN];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100575
djm@openbsd.org161cf412014-12-22 07:55:51 +0000576 if (strcasecmp(path, "none") == 0)
577 return xstrdup("none");
Darren Tucker88b6fb22010-01-13 22:44:29 +1100578 expanded = tilde_expand_filename(path, getuid());
579 if (*expanded == '/')
580 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100581 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100582 fatal("%s: getcwd: %s", __func__, strerror(errno));
583 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tuckera627d422013-06-02 07:31:17 +1000584 free(expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100585 return ret;
586}
587
Ben Lindstrombba81212001-06-25 05:01:22 +0000588static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100589add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100590{
Damien Millereccb9de2005-06-17 12:59:34 +1000591 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100592
593 if (options->num_ports == 0)
594 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100595 if (options->address_family == -1)
596 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000597 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000598 for (i = 0; i < options->num_ports; i++)
599 add_one_listen_addr(options, addr, options->ports[i]);
600 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000601 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000602}
603
Ben Lindstrombba81212001-06-25 05:01:22 +0000604static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100605add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000606{
607 struct addrinfo hints, *ai, *aitop;
608 char strport[NI_MAXSERV];
609 int gaierr;
610
611 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100612 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000613 hints.ai_socktype = SOCK_STREAM;
614 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100615 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000616 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
617 fatal("bad addr or host: %s (%s)",
618 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100619 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000620 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
621 ;
622 ai->ai_next = options->listen_addrs;
623 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100624}
625
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000626struct connection_info *
627get_connection_info(int populate, int use_dns)
628{
629 static struct connection_info ci;
630
631 if (!populate)
632 return &ci;
633 ci.host = get_canonical_hostname(use_dns);
634 ci.address = get_remote_ipaddr();
635 ci.laddress = get_local_ipaddr(packet_get_connection_in());
636 ci.lport = get_local_port();
637 return &ci;
638}
639
Darren Tucker45150472006-07-12 22:34:17 +1000640/*
641 * The strategy for the Match blocks is that the config file is parsed twice.
642 *
643 * The first time is at startup. activep is initialized to 1 and the
644 * directives in the global context are processed and acted on. Hitting a
645 * Match directive unsets activep and the directives inside the block are
646 * checked for syntax only.
647 *
648 * The second time is after a connection has been established but before
649 * authentication. activep is initialized to 2 and global config directives
650 * are ignored since they have already been processed. If the criteria in a
651 * Match block is met, activep is set and the subsequent directives
652 * processed and actioned until EOF or another Match block unsets it. Any
653 * options set are copied into the main server config.
654 *
655 * Potential additions/improvements:
656 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
657 *
658 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
659 * Match Address 192.168.0.*
660 * Tag trusted
661 * Match Group wheel
662 * Tag trusted
663 * Match Tag trusted
664 * AllowTcpForwarding yes
665 * GatewayPorts clientspecified
666 * [...]
667 *
668 * - Add a PermittedChannelRequests directive
669 * Match Group shell
670 * PermittedChannelRequests session,forwarded-tcpip
671 */
672
673static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000674match_cfg_line_group(const char *grps, int line, const char *user)
675{
676 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000677 struct passwd *pw;
678
Damien Miller565ca3f2006-08-19 00:23:15 +1000679 if (user == NULL)
680 goto out;
681
682 if ((pw = getpwnam(user)) == NULL) {
683 debug("Can't match group at line %d because user %.100s does "
684 "not exist", line, user);
685 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
686 debug("Can't Match group because user %.100s not in any group "
687 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000688 } else if (ga_match_pattern_list(grps) != 1) {
689 debug("user %.100s does not match group list %.100s at line %d",
690 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000691 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000692 debug("user %.100s matched group list %.100s at line %d", user,
693 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000694 result = 1;
695 }
696out:
697 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000698 return result;
699}
700
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000701/*
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000702 * All of the attributes on a single Match line are ANDed together, so we need
Damien Miller03bf2e62013-10-24 21:01:26 +1100703 * to check every attribute and set the result to zero if any attribute does
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000704 * not match.
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000705 */
Damien Miller565ca3f2006-08-19 00:23:15 +1000706static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000707match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +1000708{
Damien Millercf31f382013-10-24 21:02:56 +1100709 int result = 1, attributes = 0, port;
Darren Tucker45150472006-07-12 22:34:17 +1000710 char *arg, *attrib, *cp = *condition;
711 size_t len;
712
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000713 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000714 debug3("checking syntax for 'Match %s'", cp);
715 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000716 debug3("checking match for '%s' user %s host %s addr %s "
717 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
718 ci->host ? ci->host : "(null)",
719 ci->address ? ci->address : "(null)",
720 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +1000721
722 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100723 attributes++;
724 if (strcasecmp(attrib, "all") == 0) {
725 if (attributes != 1 ||
726 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
727 error("'all' cannot be combined with other "
728 "Match attributes");
729 return -1;
730 }
731 *condition = cp;
732 return 1;
733 }
Darren Tucker45150472006-07-12 22:34:17 +1000734 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
735 error("Missing Match criteria for %s", attrib);
736 return -1;
737 }
738 len = strlen(arg);
739 if (strcasecmp(attrib, "user") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000740 if (ci == NULL || ci->user == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000741 result = 0;
742 continue;
743 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000744 if (match_pattern_list(ci->user, arg, len, 0) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000745 result = 0;
746 else
747 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000748 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000749 } else if (strcasecmp(attrib, "group") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000750 if (ci == NULL || ci->user == NULL) {
751 result = 0;
752 continue;
753 }
754 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +1000755 case -1:
756 return -1;
757 case 0:
758 result = 0;
759 }
Darren Tucker45150472006-07-12 22:34:17 +1000760 } else if (strcasecmp(attrib, "host") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000761 if (ci == NULL || ci->host == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000762 result = 0;
763 continue;
764 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000765 if (match_hostname(ci->host, arg, len) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000766 result = 0;
767 else
768 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000769 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +1000770 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000771 if (ci == NULL || ci->address == NULL) {
772 result = 0;
773 continue;
774 }
775 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000776 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000777 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000778 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000779 break;
780 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000781 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000782 result = 0;
783 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000784 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000785 return -1;
786 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000787 } else if (strcasecmp(attrib, "localaddress") == 0){
788 if (ci == NULL || ci->laddress == NULL) {
789 result = 0;
790 continue;
791 }
792 switch (addr_match_list(ci->laddress, arg)) {
793 case 1:
794 debug("connection from %.100s matched "
795 "'LocalAddress %.100s' at line %d",
796 ci->laddress, arg, line);
797 break;
798 case 0:
799 case -1:
800 result = 0;
801 break;
802 case -2:
803 return -1;
804 }
805 } else if (strcasecmp(attrib, "localport") == 0) {
806 if ((port = a2port(arg)) == -1) {
807 error("Invalid LocalPort '%s' on Match line",
808 arg);
809 return -1;
810 }
811 if (ci == NULL || ci->lport == 0) {
812 result = 0;
813 continue;
814 }
815 /* TODO support port lists */
816 if (port == ci->lport)
817 debug("connection from %.100s matched "
818 "'LocalPort %d' at line %d",
819 ci->laddress, port, line);
820 else
821 result = 0;
Darren Tucker45150472006-07-12 22:34:17 +1000822 } else {
823 error("Unsupported Match attribute %s", attrib);
824 return -1;
825 }
826 }
Damien Millercf31f382013-10-24 21:02:56 +1100827 if (attributes == 0) {
828 error("One or more attributes required for Match");
829 return -1;
830 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000831 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000832 debug3("match %sfound", result ? "" : "not ");
833 *condition = cp;
834 return result;
835}
836
Damien Millere2754432006-07-24 14:06:47 +1000837#define WHITESPACE " \t\r\n"
838
Damien Miller33322122011-06-20 14:43:11 +1000839/* Multistate option parsing */
840struct multistate {
841 char *key;
842 int value;
843};
844static const struct multistate multistate_addressfamily[] = {
845 { "inet", AF_INET },
846 { "inet6", AF_INET6 },
847 { "any", AF_UNSPEC },
848 { NULL, -1 }
849};
850static const struct multistate multistate_permitrootlogin[] = {
851 { "without-password", PERMIT_NO_PASSWD },
852 { "forced-commands-only", PERMIT_FORCED_ONLY },
853 { "yes", PERMIT_YES },
854 { "no", PERMIT_NO },
855 { NULL, -1 }
856};
857static const struct multistate multistate_compression[] = {
858 { "delayed", COMP_DELAYED },
859 { "yes", COMP_ZLIB },
860 { "no", COMP_NONE },
861 { NULL, -1 }
862};
863static const struct multistate multistate_gatewayports[] = {
864 { "clientspecified", 2 },
865 { "yes", 1 },
866 { "no", 0 },
867 { NULL, -1 }
868};
Damien Miller69ff1df2011-06-23 08:30:03 +1000869static const struct multistate multistate_privsep[] = {
Damien Miller5a5c2b92012-07-31 12:21:34 +1000870 { "yes", PRIVSEP_NOSANDBOX },
871 { "sandbox", PRIVSEP_ON },
872 { "nosandbox", PRIVSEP_NOSANDBOX },
Damien Miller69ff1df2011-06-23 08:30:03 +1000873 { "no", PRIVSEP_OFF },
874 { NULL, -1 }
875};
Damien Milleraa5b3f82012-12-03 09:50:54 +1100876static const struct multistate multistate_tcpfwd[] = {
877 { "yes", FORWARD_ALLOW },
878 { "all", FORWARD_ALLOW },
879 { "no", FORWARD_DENY },
880 { "remote", FORWARD_REMOTE },
881 { "local", FORWARD_LOCAL },
882 { NULL, -1 }
883};
Damien Miller33322122011-06-20 14:43:11 +1000884
Ben Lindstromade03f62001-12-06 18:22:17 +0000885int
886process_server_config_line(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000887 const char *filename, int linenum, int *activep,
888 struct connection_info *connectinfo)
Ben Lindstromade03f62001-12-06 18:22:17 +0000889{
Darren Tucker09c0f032013-05-16 20:48:57 +1000890 char *cp, **charptr, *arg, *p;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000891 int cmdline = 0, *intptr, value, value2, n, port;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100892 SyslogFacility *log_facility_ptr;
893 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000894 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000895 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000896 size_t len;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000897 long long val64;
Damien Miller33322122011-06-20 14:43:11 +1000898 const struct multistate *multistate_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000899
900 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100901 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100902 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000903 /* Ignore leading whitespace */
904 if (*arg == '\0')
905 arg = strdelim(&cp);
906 if (!arg || !*arg || *arg == '#')
907 return 0;
908 intptr = NULL;
909 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000910 opcode = parse_token(arg, filename, linenum, &flags);
911
912 if (activep == NULL) { /* We are processing a command line directive */
913 cmdline = 1;
914 activep = &cmdline;
915 }
916 if (*activep && opcode != sMatch)
917 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
918 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000919 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000920 fatal("%s line %d: Directive '%s' is not allowed "
921 "within a Match block", filename, linenum, arg);
922 } else { /* this is a directive we have already processed */
923 while (arg)
924 arg = strdelim(&cp);
925 return 0;
926 }
927 }
928
Ben Lindstromade03f62001-12-06 18:22:17 +0000929 switch (opcode) {
930 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000931 case sUsePAM:
932 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000933 goto parse_flag;
934
935 /* Standard Options */
936 case sBadOption:
937 return -1;
938 case sPort:
939 /* ignore ports from configfile if cmdline specifies ports */
940 if (options->ports_from_cmdline)
941 return 0;
942 if (options->listen_addrs != NULL)
943 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100944 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000945 if (options->num_ports >= MAX_PORTS)
946 fatal("%s line %d: too many ports.",
947 filename, linenum);
948 arg = strdelim(&cp);
949 if (!arg || *arg == '\0')
950 fatal("%s line %d: missing port number.",
951 filename, linenum);
952 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100953 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000954 fatal("%s line %d: Badly formatted port number.",
955 filename, linenum);
956 break;
957
958 case sServerKeyBits:
959 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +1000960 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +0000961 arg = strdelim(&cp);
962 if (!arg || *arg == '\0')
963 fatal("%s line %d: missing integer value.",
964 filename, linenum);
965 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000966 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000967 *intptr = value;
968 break;
969
970 case sLoginGraceTime:
971 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +1000972 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +0000973 arg = strdelim(&cp);
974 if (!arg || *arg == '\0')
975 fatal("%s line %d: missing time value.",
976 filename, linenum);
977 if ((value = convtime(arg)) == -1)
978 fatal("%s line %d: invalid time value.",
979 filename, linenum);
980 if (*intptr == -1)
981 *intptr = value;
982 break;
983
984 case sKeyRegenerationTime:
985 intptr = &options->key_regeneration_time;
986 goto parse_time;
987
988 case sListenAddress:
989 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100990 if (arg == NULL || *arg == '\0')
991 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000992 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000993 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
994 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
995 && strchr(p+1, ':') != NULL) {
996 add_listen_addr(options, arg, 0);
997 break;
998 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100999 p = hpdelim(&arg);
1000 if (p == NULL)
1001 fatal("%s line %d: bad address:port usage",
1002 filename, linenum);
1003 p = cleanhostname(p);
1004 if (arg == NULL)
1005 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001006 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001007 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001008
Damien Millerf91ee4c2005-03-01 21:24:33 +11001009 add_listen_addr(options, p, port);
1010
Ben Lindstromade03f62001-12-06 18:22:17 +00001011 break;
1012
Darren Tucker0f383232005-01-20 10:57:56 +11001013 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +10001014 intptr = &options->address_family;
1015 multistate_ptr = multistate_addressfamily;
1016 if (options->listen_addrs != NULL)
1017 fatal("%s line %d: address family must be specified "
1018 "before ListenAddress.", filename, linenum);
1019 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +11001020 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +10001021 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +10001022 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +10001023 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +10001024 value = -1;
1025 for (i = 0; multistate_ptr[i].key != NULL; i++) {
1026 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
1027 value = multistate_ptr[i].value;
1028 break;
1029 }
1030 }
1031 if (value == -1)
1032 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +11001033 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +10001034 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +11001035 *intptr = value;
1036 break;
1037
Ben Lindstromade03f62001-12-06 18:22:17 +00001038 case sHostKeyFile:
1039 intptr = &options->num_host_key_files;
1040 if (*intptr >= MAX_HOSTKEYS)
1041 fatal("%s line %d: too many host keys specified (max %d).",
1042 filename, linenum, MAX_HOSTKEYS);
1043 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +10001044 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +00001045 arg = strdelim(&cp);
1046 if (!arg || *arg == '\0')
1047 fatal("%s line %d: missing file name.",
1048 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001049 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +11001050 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001051 /* increase optional counter */
1052 if (intptr != NULL)
1053 *intptr = *intptr + 1;
1054 }
1055 break;
1056
Damien Miller85b45e02013-07-20 13:21:52 +10001057 case sHostKeyAgent:
1058 charptr = &options->host_key_agent;
1059 arg = strdelim(&cp);
1060 if (!arg || *arg == '\0')
1061 fatal("%s line %d: missing socket name.",
1062 filename, linenum);
1063 if (*activep && *charptr == NULL)
1064 *charptr = !strcmp(arg, SSH_AUTHSOCKET_ENV_NAME) ?
1065 xstrdup(arg) : derelativise_path(arg);
1066 break;
1067
Damien Miller0a80ca12010-02-27 07:55:05 +11001068 case sHostCertificate:
1069 intptr = &options->num_host_cert_files;
1070 if (*intptr >= MAX_HOSTKEYS)
1071 fatal("%s line %d: too many host certificates "
1072 "specified (max %d).", filename, linenum,
1073 MAX_HOSTCERTS);
1074 charptr = &options->host_cert_files[*intptr];
1075 goto parse_filename;
1076 break;
1077
Ben Lindstromade03f62001-12-06 18:22:17 +00001078 case sPidFile:
1079 charptr = &options->pid_file;
1080 goto parse_filename;
1081
1082 case sPermitRootLogin:
1083 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +10001084 multistate_ptr = multistate_permitrootlogin;
1085 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001086
1087 case sIgnoreRhosts:
1088 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +10001089 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +00001090 arg = strdelim(&cp);
1091 if (!arg || *arg == '\0')
1092 fatal("%s line %d: missing yes/no argument.",
1093 filename, linenum);
1094 value = 0; /* silence compiler */
1095 if (strcmp(arg, "yes") == 0)
1096 value = 1;
1097 else if (strcmp(arg, "no") == 0)
1098 value = 0;
1099 else
1100 fatal("%s line %d: Bad yes/no argument: %s",
1101 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +10001102 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001103 *intptr = value;
1104 break;
1105
1106 case sIgnoreUserKnownHosts:
1107 intptr = &options->ignore_user_known_hosts;
1108 goto parse_flag;
1109
Ben Lindstromade03f62001-12-06 18:22:17 +00001110 case sRhostsRSAAuthentication:
1111 intptr = &options->rhosts_rsa_authentication;
1112 goto parse_flag;
1113
1114 case sHostbasedAuthentication:
1115 intptr = &options->hostbased_authentication;
1116 goto parse_flag;
1117
1118 case sHostbasedUsesNameFromPacketOnly:
1119 intptr = &options->hostbased_uses_name_from_packet_only;
1120 goto parse_flag;
1121
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001122 case sHostbasedAcceptedKeyTypes:
1123 charptr = &options->hostbased_key_types;
1124 parse_keytypes:
1125 arg = strdelim(&cp);
1126 if (!arg || *arg == '\0')
1127 fatal("%s line %d: Missing argument.",
1128 filename, linenum);
1129 if (!sshkey_names_valid2(arg, 1))
1130 fatal("%s line %d: Bad key types '%s'.",
1131 filename, linenum, arg ? arg : "<NONE>");
1132 if (*activep && *charptr == NULL)
1133 *charptr = xstrdup(arg);
1134 break;
1135
Ben Lindstromade03f62001-12-06 18:22:17 +00001136 case sRSAAuthentication:
1137 intptr = &options->rsa_authentication;
1138 goto parse_flag;
1139
1140 case sPubkeyAuthentication:
1141 intptr = &options->pubkey_authentication;
1142 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001143
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001144 case sPubkeyAcceptedKeyTypes:
1145 charptr = &options->pubkey_key_types;
1146 goto parse_keytypes;
1147
Ben Lindstromade03f62001-12-06 18:22:17 +00001148 case sKerberosAuthentication:
1149 intptr = &options->kerberos_authentication;
1150 goto parse_flag;
1151
1152 case sKerberosOrLocalPasswd:
1153 intptr = &options->kerberos_or_local_passwd;
1154 goto parse_flag;
1155
1156 case sKerberosTicketCleanup:
1157 intptr = &options->kerberos_ticket_cleanup;
1158 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001159
Darren Tucker22ef5082003-12-31 11:37:34 +11001160 case sKerberosGetAFSToken:
1161 intptr = &options->kerberos_get_afs_token;
1162 goto parse_flag;
1163
Darren Tucker0efd1552003-08-26 11:49:55 +10001164 case sGssAuthentication:
1165 intptr = &options->gss_authentication;
1166 goto parse_flag;
1167
1168 case sGssCleanupCreds:
1169 intptr = &options->gss_cleanup_creds;
1170 goto parse_flag;
1171
Ben Lindstromade03f62001-12-06 18:22:17 +00001172 case sPasswordAuthentication:
1173 intptr = &options->password_authentication;
1174 goto parse_flag;
1175
1176 case sKbdInteractiveAuthentication:
1177 intptr = &options->kbd_interactive_authentication;
1178 goto parse_flag;
1179
1180 case sChallengeResponseAuthentication:
1181 intptr = &options->challenge_response_authentication;
1182 goto parse_flag;
1183
1184 case sPrintMotd:
1185 intptr = &options->print_motd;
1186 goto parse_flag;
1187
1188 case sPrintLastLog:
1189 intptr = &options->print_lastlog;
1190 goto parse_flag;
1191
1192 case sX11Forwarding:
1193 intptr = &options->x11_forwarding;
1194 goto parse_flag;
1195
1196 case sX11DisplayOffset:
1197 intptr = &options->x11_display_offset;
1198 goto parse_int;
1199
Damien Miller95c249f2002-02-05 12:11:34 +11001200 case sX11UseLocalhost:
1201 intptr = &options->x11_use_localhost;
1202 goto parse_flag;
1203
Ben Lindstromade03f62001-12-06 18:22:17 +00001204 case sXAuthLocation:
1205 charptr = &options->xauth_location;
1206 goto parse_filename;
1207
Damien Miller5ff30c62013-10-30 22:21:50 +11001208 case sPermitTTY:
1209 intptr = &options->permit_tty;
1210 goto parse_flag;
1211
Damien Miller72e6b5c2014-07-04 09:00:04 +10001212 case sPermitUserRC:
1213 intptr = &options->permit_user_rc;
1214 goto parse_flag;
1215
Ben Lindstromade03f62001-12-06 18:22:17 +00001216 case sStrictModes:
1217 intptr = &options->strict_modes;
1218 goto parse_flag;
1219
Damien Miller12c150e2003-12-17 16:31:10 +11001220 case sTCPKeepAlive:
1221 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001222 goto parse_flag;
1223
1224 case sEmptyPasswd:
1225 intptr = &options->permit_empty_passwd;
1226 goto parse_flag;
1227
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001228 case sPermitUserEnvironment:
1229 intptr = &options->permit_user_env;
1230 goto parse_flag;
1231
Ben Lindstromade03f62001-12-06 18:22:17 +00001232 case sUseLogin:
1233 intptr = &options->use_login;
1234 goto parse_flag;
1235
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001236 case sCompression:
1237 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001238 multistate_ptr = multistate_compression;
1239 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001240
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001241 case sRekeyLimit:
1242 arg = strdelim(&cp);
1243 if (!arg || *arg == '\0')
1244 fatal("%.200s line %d: Missing argument.", filename,
1245 linenum);
1246 if (strcmp(arg, "default") == 0) {
1247 val64 = 0;
1248 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001249 if (scan_scaled(arg, &val64) == -1)
1250 fatal("%.200s line %d: Bad number '%s': %s",
1251 filename, linenum, arg, strerror(errno));
1252 /* check for too-large or too-small limits */
1253 if (val64 > UINT_MAX)
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001254 fatal("%.200s line %d: RekeyLimit too large",
1255 filename, linenum);
1256 if (val64 != 0 && val64 < 16)
1257 fatal("%.200s line %d: RekeyLimit too small",
1258 filename, linenum);
1259 }
1260 if (*activep && options->rekey_limit == -1)
1261 options->rekey_limit = (u_int32_t)val64;
1262 if (cp != NULL) { /* optional rekey interval present */
1263 if (strcmp(cp, "none") == 0) {
1264 (void)strdelim(&cp); /* discard */
1265 break;
1266 }
1267 intptr = &options->rekey_interval;
1268 goto parse_time;
1269 }
1270 break;
1271
Ben Lindstromade03f62001-12-06 18:22:17 +00001272 case sGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +10001273 intptr = &options->fwd_opts.gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001274 multistate_ptr = multistate_gatewayports;
1275 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001276
Damien Miller3a961dc2003-06-03 10:25:48 +10001277 case sUseDNS:
1278 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001279 goto parse_flag;
1280
1281 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001282 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001283 arg = strdelim(&cp);
1284 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001285 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001286 fatal("%.200s line %d: unsupported log facility '%s'",
1287 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001288 if (*log_facility_ptr == -1)
1289 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001290 break;
1291
1292 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001293 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001294 arg = strdelim(&cp);
1295 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001296 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001297 fatal("%.200s line %d: unsupported log level '%s'",
1298 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001299 if (*log_level_ptr == -1)
1300 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001301 break;
1302
1303 case sAllowTcpForwarding:
1304 intptr = &options->allow_tcp_forwarding;
Damien Milleraa5b3f82012-12-03 09:50:54 +11001305 multistate_ptr = multistate_tcpfwd;
1306 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001307
Damien Miller7acefbb2014-07-18 14:11:24 +10001308 case sAllowStreamLocalForwarding:
1309 intptr = &options->allow_streamlocal_forwarding;
1310 multistate_ptr = multistate_tcpfwd;
1311 goto parse_multistate;
1312
Damien Miller4f755cd2008-05-19 14:57:41 +10001313 case sAllowAgentForwarding:
1314 intptr = &options->allow_agent_forwarding;
1315 goto parse_flag;
1316
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001317 case sUsePrivilegeSeparation:
1318 intptr = &use_privsep;
Damien Miller69ff1df2011-06-23 08:30:03 +10001319 multistate_ptr = multistate_privsep;
1320 goto parse_multistate;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001321
Ben Lindstromade03f62001-12-06 18:22:17 +00001322 case sAllowUsers:
1323 while ((arg = strdelim(&cp)) && *arg != '\0') {
1324 if (options->num_allow_users >= MAX_ALLOW_USERS)
1325 fatal("%s line %d: too many allow users.",
1326 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001327 if (!*activep)
1328 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001329 options->allow_users[options->num_allow_users++] =
1330 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001331 }
1332 break;
1333
1334 case sDenyUsers:
1335 while ((arg = strdelim(&cp)) && *arg != '\0') {
1336 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001337 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001338 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001339 if (!*activep)
1340 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001341 options->deny_users[options->num_deny_users++] =
1342 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001343 }
1344 break;
1345
1346 case sAllowGroups:
1347 while ((arg = strdelim(&cp)) && *arg != '\0') {
1348 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1349 fatal("%s line %d: too many allow groups.",
1350 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001351 if (!*activep)
1352 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001353 options->allow_groups[options->num_allow_groups++] =
1354 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001355 }
1356 break;
1357
1358 case sDenyGroups:
1359 while ((arg = strdelim(&cp)) && *arg != '\0') {
1360 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1361 fatal("%s line %d: too many deny groups.",
1362 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001363 if (!*activep)
1364 continue;
1365 options->deny_groups[options->num_deny_groups++] =
1366 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001367 }
1368 break;
1369
1370 case sCiphers:
1371 arg = strdelim(&cp);
1372 if (!arg || *arg == '\0')
1373 fatal("%s line %d: Missing argument.", filename, linenum);
1374 if (!ciphers_valid(arg))
1375 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1376 filename, linenum, arg ? arg : "<NONE>");
1377 if (options->ciphers == NULL)
1378 options->ciphers = xstrdup(arg);
1379 break;
1380
1381 case sMacs:
1382 arg = strdelim(&cp);
1383 if (!arg || *arg == '\0')
1384 fatal("%s line %d: Missing argument.", filename, linenum);
1385 if (!mac_valid(arg))
1386 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1387 filename, linenum, arg ? arg : "<NONE>");
1388 if (options->macs == NULL)
1389 options->macs = xstrdup(arg);
1390 break;
1391
Damien Millerd5f62bf2010-09-24 22:11:14 +10001392 case sKexAlgorithms:
1393 arg = strdelim(&cp);
1394 if (!arg || *arg == '\0')
1395 fatal("%s line %d: Missing argument.",
1396 filename, linenum);
1397 if (!kex_names_valid(arg))
1398 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1399 filename, linenum, arg ? arg : "<NONE>");
1400 if (options->kex_algorithms == NULL)
1401 options->kex_algorithms = xstrdup(arg);
1402 break;
1403
Ben Lindstromade03f62001-12-06 18:22:17 +00001404 case sProtocol:
1405 intptr = &options->protocol;
1406 arg = strdelim(&cp);
1407 if (!arg || *arg == '\0')
1408 fatal("%s line %d: Missing argument.", filename, linenum);
1409 value = proto_spec(arg);
1410 if (value == SSH_PROTO_UNKNOWN)
1411 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001412 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001413 if (*intptr == SSH_PROTO_UNKNOWN)
1414 *intptr = value;
1415 break;
1416
1417 case sSubsystem:
1418 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1419 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001420 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001421 }
1422 arg = strdelim(&cp);
1423 if (!arg || *arg == '\0')
1424 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001425 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001426 if (!*activep) {
1427 arg = strdelim(&cp);
1428 break;
1429 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001430 for (i = 0; i < options->num_subsystems; i++)
1431 if (strcmp(arg, options->subsystem_name[i]) == 0)
1432 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001433 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001434 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1435 arg = strdelim(&cp);
1436 if (!arg || *arg == '\0')
1437 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001438 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001439 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001440
1441 /* Collect arguments (separate to executable) */
1442 p = xstrdup(arg);
1443 len = strlen(p) + 1;
1444 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1445 len += 1 + strlen(arg);
1446 p = xrealloc(p, 1, len);
1447 strlcat(p, " ", len);
1448 strlcat(p, arg, len);
1449 }
1450 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001451 options->num_subsystems++;
1452 break;
1453
1454 case sMaxStartups:
1455 arg = strdelim(&cp);
1456 if (!arg || *arg == '\0')
1457 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001458 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001459 if ((n = sscanf(arg, "%d:%d:%d",
1460 &options->max_startups_begin,
1461 &options->max_startups_rate,
1462 &options->max_startups)) == 3) {
1463 if (options->max_startups_begin >
1464 options->max_startups ||
1465 options->max_startups_rate > 100 ||
1466 options->max_startups_rate < 1)
1467 fatal("%s line %d: Illegal MaxStartups spec.",
1468 filename, linenum);
1469 } else if (n != 1)
1470 fatal("%s line %d: Illegal MaxStartups spec.",
1471 filename, linenum);
1472 else
1473 options->max_startups = options->max_startups_begin;
1474 break;
1475
Darren Tucker89413db2004-05-24 10:36:23 +10001476 case sMaxAuthTries:
1477 intptr = &options->max_authtries;
1478 goto parse_int;
1479
Damien Miller7207f642008-05-19 15:34:50 +10001480 case sMaxSessions:
1481 intptr = &options->max_sessions;
1482 goto parse_int;
1483
Ben Lindstromade03f62001-12-06 18:22:17 +00001484 case sBanner:
1485 charptr = &options->banner;
1486 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001487
Ben Lindstromade03f62001-12-06 18:22:17 +00001488 /*
1489 * These options can contain %X options expanded at
1490 * connect time, so that you can specify paths like:
1491 *
1492 * AuthorizedKeysFile /etc/ssh_keys/%u
1493 */
1494 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001495 if (*activep && options->num_authkeys_files == 0) {
1496 while ((arg = strdelim(&cp)) && *arg != '\0') {
1497 if (options->num_authkeys_files >=
1498 MAX_AUTHKEYS_FILES)
1499 fatal("%s line %d: "
1500 "too many authorized keys files.",
1501 filename, linenum);
1502 options->authorized_keys_files[
1503 options->num_authkeys_files++] =
1504 tilde_expand_filename(arg, getuid());
1505 }
1506 }
1507 return 0;
1508
Damien Miller30da3442010-05-10 11:58:03 +10001509 case sAuthorizedPrincipalsFile:
1510 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001511 arg = strdelim(&cp);
1512 if (!arg || *arg == '\0')
1513 fatal("%s line %d: missing file name.",
1514 filename, linenum);
1515 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001516 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001517 /* increase optional counter */
1518 if (intptr != NULL)
1519 *intptr = *intptr + 1;
1520 }
1521 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001522
1523 case sClientAliveInterval:
1524 intptr = &options->client_alive_interval;
1525 goto parse_time;
1526
1527 case sClientAliveCountMax:
1528 intptr = &options->client_alive_count_max;
1529 goto parse_int;
1530
Darren Tucker46bc0752004-05-02 22:11:30 +10001531 case sAcceptEnv:
1532 while ((arg = strdelim(&cp)) && *arg != '\0') {
1533 if (strchr(arg, '=') != NULL)
1534 fatal("%s line %d: Invalid environment name.",
1535 filename, linenum);
1536 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1537 fatal("%s line %d: too many allow env.",
1538 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001539 if (!*activep)
Damien Millerc24da772012-06-20 21:53:58 +10001540 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001541 options->accept_env[options->num_accept_env++] =
1542 xstrdup(arg);
1543 }
1544 break;
1545
Damien Millerd27b9472005-12-13 19:29:02 +11001546 case sPermitTunnel:
1547 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001548 arg = strdelim(&cp);
1549 if (!arg || *arg == '\0')
1550 fatal("%s line %d: Missing yes/point-to-point/"
1551 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001552 value = -1;
1553 for (i = 0; tunmode_desc[i].val != -1; i++)
1554 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1555 value = tunmode_desc[i].val;
1556 break;
1557 }
1558 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001559 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1560 "no argument: %s", filename, linenum, arg);
1561 if (*intptr == -1)
1562 *intptr = value;
1563 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001564
Darren Tucker45150472006-07-12 22:34:17 +10001565 case sMatch:
1566 if (cmdline)
1567 fatal("Match directive not supported as a command-line "
1568 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001569 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10001570 if (value < 0)
1571 fatal("%s line %d: Bad Match condition", filename,
1572 linenum);
1573 *activep = value;
1574 break;
1575
Damien Miller9b439df2006-07-24 14:04:00 +10001576 case sPermitOpen:
1577 arg = strdelim(&cp);
1578 if (!arg || *arg == '\0')
1579 fatal("%s line %d: missing PermitOpen specification",
1580 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001581 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001582 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001583 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001584 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001585 options->num_permitted_opens = 0;
1586 }
Damien Miller9b439df2006-07-24 14:04:00 +10001587 break;
1588 }
Damien Millerc6081482012-04-22 11:18:53 +10001589 if (strcmp(arg, "none") == 0) {
1590 if (*activep && n == -1) {
Damien Millerc6081482012-04-22 11:18:53 +10001591 options->num_permitted_opens = 1;
1592 channel_disable_adm_local_opens();
1593 }
1594 break;
1595 }
Damien Millera29b95e2007-01-05 16:28:36 +11001596 if (*activep && n == -1)
1597 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001598 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1599 p = hpdelim(&arg);
1600 if (p == NULL)
1601 fatal("%s line %d: missing host in PermitOpen",
1602 filename, linenum);
1603 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001604 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001605 fatal("%s line %d: bad port number in "
1606 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001607 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001608 options->num_permitted_opens =
1609 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001610 }
Damien Miller9b439df2006-07-24 14:04:00 +10001611 break;
1612
Damien Millere2754432006-07-24 14:06:47 +10001613 case sForceCommand:
1614 if (cp == NULL)
1615 fatal("%.200s line %d: Missing argument.", filename,
1616 linenum);
1617 len = strspn(cp, WHITESPACE);
1618 if (*activep && options->adm_forced_command == NULL)
1619 options->adm_forced_command = xstrdup(cp + len);
1620 return 0;
1621
Damien Millerd8cb1f12008-02-10 22:40:12 +11001622 case sChrootDirectory:
1623 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001624
1625 arg = strdelim(&cp);
1626 if (!arg || *arg == '\0')
1627 fatal("%s line %d: missing file name.",
1628 filename, linenum);
1629 if (*activep && *charptr == NULL)
1630 *charptr = xstrdup(arg);
1631 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001632
Damien Miller1aed65e2010-03-04 21:53:35 +11001633 case sTrustedUserCAKeys:
1634 charptr = &options->trusted_user_ca_keys;
1635 goto parse_filename;
1636
1637 case sRevokedKeys:
1638 charptr = &options->revoked_keys_file;
1639 goto parse_filename;
1640
Damien Miller0dac6fb2010-11-20 15:19:38 +11001641 case sIPQoS:
1642 arg = strdelim(&cp);
1643 if ((value = parse_ipqos(arg)) == -1)
1644 fatal("%s line %d: Bad IPQoS value: %s",
1645 filename, linenum, arg);
1646 arg = strdelim(&cp);
1647 if (arg == NULL)
1648 value2 = value;
1649 else if ((value2 = parse_ipqos(arg)) == -1)
1650 fatal("%s line %d: Bad IPQoS value: %s",
1651 filename, linenum, arg);
1652 if (*activep) {
1653 options->ip_qos_interactive = value;
1654 options->ip_qos_bulk = value2;
1655 }
1656 break;
1657
Damien Miller23528812012-04-22 11:24:43 +10001658 case sVersionAddendum:
1659 if (cp == NULL)
1660 fatal("%.200s line %d: Missing argument.", filename,
1661 linenum);
1662 len = strspn(cp, WHITESPACE);
1663 if (*activep && options->version_addendum == NULL) {
1664 if (strcasecmp(cp + len, "none") == 0)
1665 options->version_addendum = xstrdup("");
1666 else if (strchr(cp + len, '\r') != NULL)
1667 fatal("%.200s line %d: Invalid argument",
1668 filename, linenum);
1669 else
1670 options->version_addendum = xstrdup(cp + len);
1671 }
1672 return 0;
1673
Damien Miller09d3e122012-10-31 08:58:58 +11001674 case sAuthorizedKeysCommand:
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001675 if (cp == NULL)
1676 fatal("%.200s line %d: Missing argument.", filename,
1677 linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001678 len = strspn(cp, WHITESPACE);
1679 if (*activep && options->authorized_keys_command == NULL) {
1680 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1681 fatal("%.200s line %d: AuthorizedKeysCommand "
1682 "must be an absolute path",
1683 filename, linenum);
1684 options->authorized_keys_command = xstrdup(cp + len);
1685 }
1686 return 0;
1687
1688 case sAuthorizedKeysCommandUser:
1689 charptr = &options->authorized_keys_command_user;
1690
1691 arg = strdelim(&cp);
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001692 if (!arg || *arg == '\0')
1693 fatal("%s line %d: missing AuthorizedKeysCommandUser "
1694 "argument.", filename, linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001695 if (*activep && *charptr == NULL)
1696 *charptr = xstrdup(arg);
1697 break;
1698
Damien Millera6e3f012012-11-04 23:21:40 +11001699 case sAuthenticationMethods:
1700 if (*activep && options->num_auth_methods == 0) {
1701 while ((arg = strdelim(&cp)) && *arg != '\0') {
1702 if (options->num_auth_methods >=
1703 MAX_AUTH_METHODS)
1704 fatal("%s line %d: "
1705 "too many authentication methods.",
1706 filename, linenum);
1707 if (auth2_methods_valid(arg, 0) != 0)
1708 fatal("%s line %d: invalid "
1709 "authentication method list.",
1710 filename, linenum);
1711 options->auth_methods[
1712 options->num_auth_methods++] = xstrdup(arg);
1713 }
1714 }
1715 return 0;
1716
Damien Miller7acefbb2014-07-18 14:11:24 +10001717 case sStreamLocalBindMask:
1718 arg = strdelim(&cp);
1719 if (!arg || *arg == '\0')
1720 fatal("%s line %d: missing StreamLocalBindMask argument.",
1721 filename, linenum);
1722 /* Parse mode in octal format */
1723 value = strtol(arg, &p, 8);
1724 if (arg == p || value < 0 || value > 0777)
1725 fatal("%s line %d: Bad mask.", filename, linenum);
1726 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1727 break;
1728
1729 case sStreamLocalBindUnlink:
1730 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1731 goto parse_flag;
1732
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001733 case sFingerprintHash:
1734 arg = strdelim(&cp);
1735 if (!arg || *arg == '\0')
1736 fatal("%.200s line %d: Missing argument.",
1737 filename, linenum);
1738 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1739 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1740 filename, linenum, arg);
1741 if (*activep)
1742 options->fingerprint_hash = value;
1743 break;
1744
Ben Lindstromade03f62001-12-06 18:22:17 +00001745 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001746 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001747 filename, linenum, arg);
1748 while (arg)
1749 arg = strdelim(&cp);
1750 break;
1751
Damien Millerf9b3feb2003-05-16 11:38:32 +10001752 case sUnsupported:
1753 logit("%s line %d: Unsupported option %s",
1754 filename, linenum, arg);
1755 while (arg)
1756 arg = strdelim(&cp);
1757 break;
1758
Ben Lindstromade03f62001-12-06 18:22:17 +00001759 default:
1760 fatal("%s line %d: Missing handler for opcode %s (%d)",
1761 filename, linenum, arg, opcode);
1762 }
1763 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1764 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1765 filename, linenum, arg);
1766 return 0;
1767}
1768
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001769/* Reads the server configuration file. */
1770
Damien Miller4af51302000-04-16 11:18:38 +10001771void
Darren Tucker645ab752004-06-25 13:33:20 +10001772load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001773{
Damien Miller46cb75a2012-07-31 12:22:37 +10001774 char line[4096], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001775 FILE *f;
Damien Miller46cb75a2012-07-31 12:22:37 +10001776 int lineno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001777
Darren Tucker645ab752004-06-25 13:33:20 +10001778 debug2("%s: filename %s", __func__, filename);
1779 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001780 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001781 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001782 }
Darren Tucker645ab752004-06-25 13:33:20 +10001783 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001784 while (fgets(line, sizeof(line), f)) {
Damien Miller46cb75a2012-07-31 12:22:37 +10001785 lineno++;
1786 if (strlen(line) == sizeof(line) - 1)
1787 fatal("%s line %d too long", filename, lineno);
Darren Tucker645ab752004-06-25 13:33:20 +10001788 /*
1789 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001790 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001791 * line numbers later for error messages
1792 */
1793 if ((cp = strchr(line, '#')) != NULL)
1794 memcpy(cp, "\n", 2);
1795 cp = line + strspn(line, " \t\r");
1796
1797 buffer_append(conf, cp, strlen(cp));
1798 }
1799 buffer_append(conf, "\0", 1);
1800 fclose(f);
1801 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1802}
1803
1804void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001805parse_server_match_config(ServerOptions *options,
1806 struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10001807{
Darren Tucker45150472006-07-12 22:34:17 +10001808 ServerOptions mo;
1809
1810 initialize_server_options(&mo);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001811 parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11001812 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001813}
1814
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001815int parse_server_match_testspec(struct connection_info *ci, char *spec)
1816{
1817 char *p;
1818
1819 while ((p = strsep(&spec, ",")) && *p != '\0') {
1820 if (strncmp(p, "addr=", 5) == 0) {
1821 ci->address = xstrdup(p + 5);
1822 } else if (strncmp(p, "host=", 5) == 0) {
1823 ci->host = xstrdup(p + 5);
1824 } else if (strncmp(p, "user=", 5) == 0) {
1825 ci->user = xstrdup(p + 5);
1826 } else if (strncmp(p, "laddr=", 6) == 0) {
1827 ci->laddress = xstrdup(p + 6);
1828 } else if (strncmp(p, "lport=", 6) == 0) {
1829 ci->lport = a2port(p + 6);
1830 if (ci->lport == -1) {
1831 fprintf(stderr, "Invalid port '%s' in test mode"
1832 " specification %s\n", p+6, p);
1833 return -1;
1834 }
1835 } else {
1836 fprintf(stderr, "Invalid test mode specification %s\n",
1837 p);
1838 return -1;
1839 }
1840 }
1841 return 0;
1842}
1843
1844/*
1845 * returns 1 for a complete spec, 0 for partial spec and -1 for an
1846 * empty spec.
1847 */
1848int server_match_spec_complete(struct connection_info *ci)
1849{
1850 if (ci->user && ci->host && ci->address)
1851 return 1; /* complete */
1852 if (!ci->user && !ci->host && !ci->address)
1853 return -1; /* empty */
1854 return 0; /* partial */
1855}
1856
Darren Tucker1629c072007-02-19 22:25:37 +11001857/*
1858 * Copy any supported values that are set.
1859 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001860 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001861 * array values that are not used pre-authentication, because any that we
1862 * do use must be explictly sent in mm_getpwnamallow().
1863 */
Darren Tucker45150472006-07-12 22:34:17 +10001864void
Darren Tucker1629c072007-02-19 22:25:37 +11001865copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001866{
Damien Miller534b2cc2013-12-05 14:07:27 +11001867#define M_CP_INTOPT(n) do {\
1868 if (src->n != -1) \
1869 dst->n = src->n; \
1870} while (0)
1871
Darren Tucker1629c072007-02-19 22:25:37 +11001872 M_CP_INTOPT(password_authentication);
1873 M_CP_INTOPT(gss_authentication);
1874 M_CP_INTOPT(rsa_authentication);
1875 M_CP_INTOPT(pubkey_authentication);
1876 M_CP_INTOPT(kerberos_authentication);
1877 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001878 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001879 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001880 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001881 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001882
1883 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10001884 M_CP_INTOPT(allow_streamlocal_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001885 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001886 M_CP_INTOPT(permit_tun);
Damien Miller7acefbb2014-07-18 14:11:24 +10001887 M_CP_INTOPT(fwd_opts.gateway_ports);
Darren Tucker1629c072007-02-19 22:25:37 +11001888 M_CP_INTOPT(x11_display_offset);
1889 M_CP_INTOPT(x11_forwarding);
1890 M_CP_INTOPT(x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11001891 M_CP_INTOPT(permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10001892 M_CP_INTOPT(permit_user_rc);
Damien Miller7207f642008-05-19 15:34:50 +10001893 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001894 M_CP_INTOPT(max_authtries);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001895 M_CP_INTOPT(ip_qos_interactive);
1896 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001897 M_CP_INTOPT(rekey_limit);
1898 M_CP_INTOPT(rekey_interval);
Darren Tucker1629c072007-02-19 22:25:37 +11001899
Damien Miller534b2cc2013-12-05 14:07:27 +11001900 /* M_CP_STROPT and M_CP_STRARRAYOPT should not appear before here */
1901#define M_CP_STROPT(n) do {\
1902 if (src->n != NULL && dst->n != src->n) { \
1903 free(dst->n); \
1904 dst->n = src->n; \
1905 } \
1906} while(0)
1907#define M_CP_STRARRAYOPT(n, num_n) do {\
1908 if (src->num_n != 0) { \
1909 for (dst->num_n = 0; dst->num_n < src->num_n; dst->num_n++) \
1910 dst->n[dst->num_n] = xstrdup(src->n[dst->num_n]); \
1911 } \
1912} while(0)
1913
Damien Millerf2e407e2011-05-20 19:04:14 +10001914 /* See comment in servconf.h */
1915 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10001916
Damien Millerc2411902011-05-20 19:03:49 +10001917 /*
1918 * The only things that should be below this point are string options
1919 * which are only used after authentication.
1920 */
Damien Miller5d74e582011-05-20 19:03:31 +10001921 if (preauth)
1922 return;
Damien Millerd8478b62011-05-29 21:39:36 +10001923
Damien Miller5d74e582011-05-20 19:03:31 +10001924 M_CP_STROPT(adm_forced_command);
1925 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001926}
1927
Darren Tucker1629c072007-02-19 22:25:37 +11001928#undef M_CP_INTOPT
1929#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10001930#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11001931
Darren Tucker45150472006-07-12 22:34:17 +10001932void
1933parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001934 struct connection_info *connectinfo)
Darren Tucker45150472006-07-12 22:34:17 +10001935{
1936 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001937 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001938
1939 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1940
Darren Tucker9fbac712004-08-12 22:41:44 +10001941 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001942 active = connectinfo ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001943 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001944 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001945 if (process_server_config_line(options, cp, filename,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001946 linenum++, &active, connectinfo) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001947 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001948 }
Darren Tuckera627d422013-06-02 07:31:17 +10001949 free(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001950 if (bad_options > 0)
1951 fatal("%s: terminating, %d bad configuration options",
1952 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001953}
Darren Tuckere7140f22008-06-10 23:01:51 +10001954
1955static const char *
Damien Miller82c55872011-06-23 08:20:30 +10001956fmt_multistate_int(int val, const struct multistate *m)
1957{
1958 u_int i;
1959
1960 for (i = 0; m[i].key != NULL; i++) {
1961 if (m[i].value == val)
1962 return m[i].key;
1963 }
1964 return "UNKNOWN";
1965}
1966
1967static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10001968fmt_intarg(ServerOpCodes code, int val)
1969{
Damien Miller82c55872011-06-23 08:20:30 +10001970 if (val == -1)
1971 return "unset";
1972 switch (code) {
1973 case sAddressFamily:
1974 return fmt_multistate_int(val, multistate_addressfamily);
1975 case sPermitRootLogin:
1976 return fmt_multistate_int(val, multistate_permitrootlogin);
1977 case sGatewayPorts:
1978 return fmt_multistate_int(val, multistate_gatewayports);
1979 case sCompression:
1980 return fmt_multistate_int(val, multistate_compression);
Damien Miller69ff1df2011-06-23 08:30:03 +10001981 case sUsePrivilegeSeparation:
1982 return fmt_multistate_int(val, multistate_privsep);
Damien Milleraa5b3f82012-12-03 09:50:54 +11001983 case sAllowTcpForwarding:
1984 return fmt_multistate_int(val, multistate_tcpfwd);
Damien Miller7acefbb2014-07-18 14:11:24 +10001985 case sAllowStreamLocalForwarding:
1986 return fmt_multistate_int(val, multistate_tcpfwd);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001987 case sFingerprintHash:
1988 return ssh_digest_alg_name(val);
Damien Miller82c55872011-06-23 08:20:30 +10001989 case sProtocol:
Darren Tuckere7140f22008-06-10 23:01:51 +10001990 switch (val) {
1991 case SSH_PROTO_1:
1992 return "1";
1993 case SSH_PROTO_2:
1994 return "2";
1995 case (SSH_PROTO_1|SSH_PROTO_2):
1996 return "2,1";
1997 default:
1998 return "UNKNOWN";
1999 }
Damien Miller82c55872011-06-23 08:20:30 +10002000 default:
2001 switch (val) {
2002 case 0:
2003 return "no";
2004 case 1:
2005 return "yes";
2006 default:
2007 return "UNKNOWN";
2008 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002009 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002010}
2011
2012static const char *
2013lookup_opcode_name(ServerOpCodes code)
2014{
2015 u_int i;
2016
2017 for (i = 0; keywords[i].name != NULL; i++)
2018 if (keywords[i].opcode == code)
2019 return(keywords[i].name);
2020 return "UNKNOWN";
2021}
2022
2023static void
2024dump_cfg_int(ServerOpCodes code, int val)
2025{
2026 printf("%s %d\n", lookup_opcode_name(code), val);
2027}
2028
2029static void
2030dump_cfg_fmtint(ServerOpCodes code, int val)
2031{
2032 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2033}
2034
2035static void
2036dump_cfg_string(ServerOpCodes code, const char *val)
2037{
2038 if (val == NULL)
2039 return;
djm@openbsd.org161cf412014-12-22 07:55:51 +00002040 printf("%s %s\n", lookup_opcode_name(code),
2041 val == NULL ? "none" : val);
Darren Tuckere7140f22008-06-10 23:01:51 +10002042}
2043
2044static void
2045dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
2046{
2047 u_int i;
2048
2049 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10002050 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2051}
2052
2053static void
2054dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
2055{
2056 u_int i;
2057
2058 printf("%s", lookup_opcode_name(code));
2059 for (i = 0; i < count; i++)
2060 printf(" %s", vals[i]);
2061 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002062}
2063
2064void
2065dump_config(ServerOptions *o)
2066{
2067 u_int i;
2068 int ret;
2069 struct addrinfo *ai;
2070 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
2071
2072 /* these are usually at the top of the config */
2073 for (i = 0; i < o->num_ports; i++)
2074 printf("port %d\n", o->ports[i]);
2075 dump_cfg_fmtint(sProtocol, o->protocol);
2076 dump_cfg_fmtint(sAddressFamily, o->address_family);
2077
2078 /* ListenAddress must be after Port */
2079 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
2080 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
2081 sizeof(addr), port, sizeof(port),
2082 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
2083 error("getnameinfo failed: %.100s",
2084 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
2085 strerror(errno));
2086 } else {
2087 if (ai->ai_family == AF_INET6)
2088 printf("listenaddress [%s]:%s\n", addr, port);
2089 else
2090 printf("listenaddress %s:%s\n", addr, port);
2091 }
2092 }
2093
2094 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10002095#ifdef USE_PAM
2096 dump_cfg_int(sUsePAM, o->use_pam);
2097#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002098 dump_cfg_int(sServerKeyBits, o->server_key_bits);
2099 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
2100 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
2101 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
2102 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11002103 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10002104 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
2105 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
2106
2107 /* formatted integer arguments */
2108 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
2109 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
2110 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
2111 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2112 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
2113 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
2114 o->hostbased_uses_name_from_packet_only);
2115 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
2116 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10002117#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10002118 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
2119 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
2120 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10002121# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10002122 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10002123# endif
2124#endif
2125#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10002126 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2127 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10002128#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002129 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2130 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2131 o->kbd_interactive_authentication);
2132 dump_cfg_fmtint(sChallengeResponseAuthentication,
2133 o->challenge_response_authentication);
2134 dump_cfg_fmtint(sPrintMotd, o->print_motd);
2135 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
2136 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
2137 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002138 dump_cfg_fmtint(sPermitTTY, o->permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002139 dump_cfg_fmtint(sPermitUserRC, o->permit_user_rc);
Darren Tuckere7140f22008-06-10 23:01:51 +10002140 dump_cfg_fmtint(sStrictModes, o->strict_modes);
2141 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
2142 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
2143 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
2144 dump_cfg_fmtint(sUseLogin, o->use_login);
2145 dump_cfg_fmtint(sCompression, o->compression);
Damien Miller7acefbb2014-07-18 14:11:24 +10002146 dump_cfg_fmtint(sGatewayPorts, o->fwd_opts.gateway_ports);
Darren Tuckere7140f22008-06-10 23:01:51 +10002147 dump_cfg_fmtint(sUseDNS, o->use_dns);
2148 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002149 dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
Darren Tuckere7140f22008-06-10 23:01:51 +10002150 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002151 dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
Darren Tuckere7140f22008-06-10 23:01:51 +10002152
2153 /* string arguments */
2154 dump_cfg_string(sPidFile, o->pid_file);
2155 dump_cfg_string(sXAuthLocation, o->xauth_location);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002156 dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
2157 dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
Darren Tuckere7140f22008-06-10 23:01:51 +10002158 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10002159 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11002160 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11002161 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
2162 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10002163 dump_cfg_string(sAuthorizedPrincipalsFile,
2164 o->authorized_principals_file);
Damien Miller23528812012-04-22 11:24:43 +10002165 dump_cfg_string(sVersionAddendum, o->version_addendum);
Damien Miller09d3e122012-10-31 08:58:58 +11002166 dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
2167 dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
Damien Miller85b45e02013-07-20 13:21:52 +10002168 dump_cfg_string(sHostKeyAgent, o->host_key_agent);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002169 dump_cfg_string(sKexAlgorithms,
djm@openbsd.org259a02e2014-10-13 00:38:35 +00002170 o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002171 dump_cfg_string(sHostbasedAcceptedKeyTypes, o->hostbased_key_types ?
2172 o->hostbased_key_types : KEX_DEFAULT_PK_ALG);
2173 dump_cfg_string(sPubkeyAcceptedKeyTypes, o->pubkey_key_types ?
2174 o->pubkey_key_types : KEX_DEFAULT_PK_ALG);
Darren Tuckere7140f22008-06-10 23:01:51 +10002175
2176 /* string arguments requiring a lookup */
2177 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
2178 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
2179
2180 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10002181 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
2182 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002183 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
2184 o->host_key_files);
Damien Miller0a80ca12010-02-27 07:55:05 +11002185 dump_cfg_strarray(sHostKeyFile, o->num_host_cert_files,
2186 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002187 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
2188 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
2189 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
2190 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
2191 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
Damien Millera6e3f012012-11-04 23:21:40 +11002192 dump_cfg_strarray_oneline(sAuthenticationMethods,
2193 o->num_auth_methods, o->auth_methods);
Darren Tuckere7140f22008-06-10 23:01:51 +10002194
2195 /* other arguments */
2196 for (i = 0; i < o->num_subsystems; i++)
2197 printf("subsystem %s %s\n", o->subsystem_name[i],
2198 o->subsystem_args[i]);
2199
2200 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
2201 o->max_startups_rate, o->max_startups);
2202
2203 for (i = 0; tunmode_desc[i].val != -1; i++)
2204 if (tunmode_desc[i].val == o->permit_tun) {
2205 s = tunmode_desc[i].text;
2206 break;
2207 }
2208 dump_cfg_string(sPermitTunnel, s);
2209
Damien Miller91475862011-05-05 14:14:34 +10002210 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2211 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11002212
Damien Millera6d6c1f2013-08-21 02:40:01 +10002213 printf("rekeylimit %lld %d\n", (long long)o->rekey_limit,
2214 o->rekey_interval);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002215
Darren Tuckere7140f22008-06-10 23:01:51 +10002216 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10002217}