blob: 6eb3686617835c0f63c9efbe985d893133b6dada [file] [log] [blame]
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001
djm@openbsd.org161cf412014-12-22 07:55:51 +00002/* $OpenBSD: servconf.c,v 1.257 2014/12/22 07:55:51 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;
Damien Miller95def091999-11-25 00:26:21 +1100107 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100108 options->pubkey_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100109 options->kerberos_authentication = -1;
110 options->kerberos_or_local_passwd = -1;
111 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100112 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000113 options->gss_authentication=-1;
114 options->gss_cleanup_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100115 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100116 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000117 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100118 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000119 options->permit_user_env = -1;
Damien Miller95def091999-11-25 00:26:21 +1100120 options->use_login = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000121 options->compression = -1;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000122 options->rekey_limit = -1;
123 options->rekey_interval = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100124 options->allow_tcp_forwarding = -1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000125 options->allow_streamlocal_forwarding = -1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000126 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100127 options->num_allow_users = 0;
128 options->num_deny_users = 0;
129 options->num_allow_groups = 0;
130 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000131 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000132 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000133 options->kex_algorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000134 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller7acefbb2014-07-18 14:11:24 +1000135 options->fwd_opts.gateway_ports = -1;
136 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
137 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000138 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000139 options->max_startups_begin = -1;
140 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000141 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000142 options->max_authtries = -1;
Damien Miller7207f642008-05-19 15:34:50 +1000143 options->max_sessions = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000144 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000145 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000146 options->client_alive_interval = -1;
147 options->client_alive_count_max = -1;
Damien Millerd8478b62011-05-29 21:39:36 +1000148 options->num_authkeys_files = 0;
Darren Tucker46bc0752004-05-02 22:11:30 +1000149 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100150 options->permit_tun = -1;
Damien Millera765cf42006-07-24 14:08:13 +1000151 options->num_permitted_opens = -1;
Damien Millere2754432006-07-24 14:06:47 +1000152 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100153 options->chroot_directory = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +1100154 options->authorized_keys_command = NULL;
155 options->authorized_keys_command_user = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100156 options->revoked_keys_file = NULL;
157 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000158 options->authorized_principals_file = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100159 options->ip_qos_interactive = -1;
160 options->ip_qos_bulk = -1;
Damien Miller23528812012-04-22 11:24:43 +1000161 options->version_addendum = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000162 options->fingerprint_hash = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163}
164
djm@openbsd.org161cf412014-12-22 07:55:51 +0000165/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
166static int
167option_clear_or_none(const char *o)
168{
169 return o == NULL || strcasecmp(o, "none") == 0;
170}
171
Damien Miller4af51302000-04-16 11:18:38 +1000172void
Damien Miller95def091999-11-25 00:26:21 +1100173fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174{
djm@openbsd.org161cf412014-12-22 07:55:51 +0000175 int i;
176
Damien Miller726273e2001-11-12 11:40:11 +1100177 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000178 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000179 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100180
181 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100182 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +1100183 options->protocol = SSH_PROTO_2;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100184 if (options->num_host_key_files == 0) {
185 /* fill default hostkeys for protocols */
186 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100187 options->host_key_files[options->num_host_key_files++] =
188 _PATH_HOST_KEY_FILE;
189 if (options->protocol & SSH_PROTO_2) {
190 options->host_key_files[options->num_host_key_files++] =
191 _PATH_HOST_RSA_KEY_FILE;
192 options->host_key_files[options->num_host_key_files++] =
193 _PATH_HOST_DSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100194#ifdef OPENSSL_HAS_ECC
Damien Millere13cadf2010-09-10 11:15:33 +1000195 options->host_key_files[options->num_host_key_files++] =
196 _PATH_HOST_ECDSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100197#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +1100198 options->host_key_files[options->num_host_key_files++] =
199 _PATH_HOST_ED25519_KEY_FILE;
Damien Miller7fc23732002-01-22 23:19:11 +1100200 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100201 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100202 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100203 if (options->num_ports == 0)
204 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
205 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000206 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000207 if (options->pid_file == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000208 options->pid_file = xstrdup(_PATH_SSH_DAEMON_PID_FILE);
Damien Miller95def091999-11-25 00:26:21 +1100209 if (options->server_key_bits == -1)
Darren Tucker7499b0c2008-07-02 22:35:43 +1000210 options->server_key_bits = 1024;
Damien Miller95def091999-11-25 00:26:21 +1100211 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000212 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100213 if (options->key_regeneration_time == -1)
214 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000215 if (options->permit_root_login == PERMIT_NOT_SET)
216 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100217 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100218 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100219 if (options->ignore_user_known_hosts == -1)
220 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100221 if (options->print_motd == -1)
222 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000223 if (options->print_lastlog == -1)
224 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100225 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100226 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100227 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100228 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100229 if (options->x11_use_localhost == -1)
230 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000231 if (options->xauth_location == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000232 options->xauth_location = xstrdup(_PATH_XAUTH);
Damien Miller5ff30c62013-10-30 22:21:50 +1100233 if (options->permit_tty == -1)
234 options->permit_tty = 1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000235 if (options->permit_user_rc == -1)
236 options->permit_user_rc = 1;
Damien Miller95def091999-11-25 00:26:21 +1100237 if (options->strict_modes == -1)
238 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100239 if (options->tcp_keep_alive == -1)
240 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100241 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100242 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100243 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000244 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100245 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100246 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000247 if (options->hostbased_authentication == -1)
248 options->hostbased_authentication = 0;
249 if (options->hostbased_uses_name_from_packet_only == -1)
250 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100251 if (options->rsa_authentication == -1)
252 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100253 if (options->pubkey_authentication == -1)
254 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100255 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000256 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100257 if (options->kerberos_or_local_passwd == -1)
258 options->kerberos_or_local_passwd = 1;
259 if (options->kerberos_ticket_cleanup == -1)
260 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100261 if (options->kerberos_get_afs_token == -1)
262 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000263 if (options->gss_authentication == -1)
264 options->gss_authentication = 0;
265 if (options->gss_cleanup_creds == -1)
266 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100267 if (options->password_authentication == -1)
268 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100269 if (options->kbd_interactive_authentication == -1)
270 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000271 if (options->challenge_response_authentication == -1)
272 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100273 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100274 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000275 if (options->permit_user_env == -1)
276 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100277 if (options->use_login == -1)
278 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000279 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000280 options->compression = COMP_DELAYED;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000281 if (options->rekey_limit == -1)
282 options->rekey_limit = 0;
283 if (options->rekey_interval == -1)
284 options->rekey_interval = 0;
Damien Miller50a41ed2000-10-16 12:14:42 +1100285 if (options->allow_tcp_forwarding == -1)
Damien Milleraa5b3f82012-12-03 09:50:54 +1100286 options->allow_tcp_forwarding = FORWARD_ALLOW;
Damien Miller7acefbb2014-07-18 14:11:24 +1000287 if (options->allow_streamlocal_forwarding == -1)
288 options->allow_streamlocal_forwarding = FORWARD_ALLOW;
Damien Miller4f755cd2008-05-19 14:57:41 +1000289 if (options->allow_agent_forwarding == -1)
290 options->allow_agent_forwarding = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000291 if (options->fwd_opts.gateway_ports == -1)
292 options->fwd_opts.gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000293 if (options->max_startups == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100294 options->max_startups = 100;
Damien Miller942da032000-08-18 13:59:06 +1000295 if (options->max_startups_rate == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100296 options->max_startups_rate = 30; /* 30% */
Damien Miller942da032000-08-18 13:59:06 +1000297 if (options->max_startups_begin == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100298 options->max_startups_begin = 10;
Darren Tucker89413db2004-05-24 10:36:23 +1000299 if (options->max_authtries == -1)
300 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000301 if (options->max_sessions == -1)
302 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000303 if (options->use_dns == -1)
304 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000305 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100306 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000307 if (options->client_alive_count_max == -1)
308 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000309 if (options->num_authkeys_files == 0) {
310 options->authorized_keys_files[options->num_authkeys_files++] =
311 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS);
312 options->authorized_keys_files[options->num_authkeys_files++] =
313 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS2);
314 }
Damien Millerd27b9472005-12-13 19:29:02 +1100315 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100316 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100317 if (options->ip_qos_interactive == -1)
318 options->ip_qos_interactive = IPTOS_LOWDELAY;
319 if (options->ip_qos_bulk == -1)
320 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller23528812012-04-22 11:24:43 +1000321 if (options->version_addendum == NULL)
322 options->version_addendum = xstrdup("");
Damien Miller7acefbb2014-07-18 14:11:24 +1000323 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
324 options->fwd_opts.streamlocal_bind_mask = 0177;
325 if (options->fwd_opts.streamlocal_bind_unlink == -1)
326 options->fwd_opts.streamlocal_bind_unlink = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000327 if (options->fingerprint_hash == -1)
328 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
Ben Lindstromfb62a692002-06-06 19:47:11 +0000329 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000330 if (use_privsep == -1)
Damien Miller5a5c2b92012-07-31 12:21:34 +1000331 use_privsep = PRIVSEP_NOSANDBOX;
Damien Miller4903eb42002-06-21 16:20:44 +1000332
djm@openbsd.org161cf412014-12-22 07:55:51 +0000333#define CLEAR_ON_NONE(v) \
334 do { \
335 if (option_clear_or_none(v)) { \
336 free(v); \
337 v = NULL; \
338 } \
339 } while(0)
340 CLEAR_ON_NONE(options->pid_file);
341 CLEAR_ON_NONE(options->xauth_location);
342 CLEAR_ON_NONE(options->banner);
343 CLEAR_ON_NONE(options->trusted_user_ca_keys);
344 CLEAR_ON_NONE(options->revoked_keys_file);
345 for (i = 0; i < options->num_host_key_files; i++)
346 CLEAR_ON_NONE(options->host_key_files[i]);
347 for (i = 0; i < options->num_host_cert_files; i++)
348 CLEAR_ON_NONE(options->host_cert_files[i]);
349#undef CLEAR_ON_NONE
350
Tim Rice40017b02002-07-14 13:36:49 -0700351#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000352 if (use_privsep && options->compression == 1) {
353 error("This platform does not support both privilege "
354 "separation and compression");
355 error("Compression disabled");
356 options->compression = 0;
357 }
358#endif
359
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360}
361
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100363typedef enum {
364 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100365 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000366 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100367 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100368 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
369 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000370 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100371 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100372 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000373 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100374 sPasswordAuthentication, sKbdInteractiveAuthentication,
375 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000376 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100377 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller5ff30c62013-10-30 22:21:50 +1100378 sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000379 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000380 sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000381 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000382 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
Damien Miller7207f642008-05-19 15:34:50 +1000383 sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000384 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100385 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Damien Millerec2eaa32011-05-20 18:57:14 +1000386 sClientAliveCountMax, sAuthorizedKeysFile,
Damien Millerd27b9472005-12-13 19:29:02 +1100387 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100388 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100389 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller7cc194f2014-02-04 11:12:56 +1100390 sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000391 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Miller23528812012-04-22 11:24:43 +1000392 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Miller09d3e122012-10-31 08:58:58 +1100393 sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
Damien Miller72e6b5c2014-07-04 09:00:04 +1000394 sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
Damien Miller7acefbb2014-07-18 14:11:24 +1000395 sStreamLocalBindMask, sStreamLocalBindUnlink,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000396 sAllowStreamLocalForwarding, sFingerprintHash,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000397 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398} ServerOpCodes;
399
Darren Tucker45150472006-07-12 22:34:17 +1000400#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
401#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
402#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
403
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100405static struct {
406 const char *name;
407 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000408 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100409} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100410 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000411#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000412 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000413#else
Darren Tucker45150472006-07-12 22:34:17 +1000414 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000415#endif
Darren Tucker45150472006-07-12 22:34:17 +1000416 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100417 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000418 { "port", sPort, SSHCFG_GLOBAL },
419 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
420 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
Damien Miller85b45e02013-07-20 13:21:52 +1000421 { "hostkeyagent", sHostKeyAgent, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000422 { "pidfile", sPidFile, SSHCFG_GLOBAL },
423 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
424 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
425 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100426 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000427 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
428 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
429 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100430 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
431 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000432 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100433 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
434 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000435 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000436#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100437 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000438 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
439 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100440#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000441 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000442#else
Darren Tucker45150472006-07-12 22:34:17 +1000443 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100444#endif
445#else
Darren Tucker1629c072007-02-19 22:25:37 +1100446 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000447 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
448 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
449 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000450#endif
Darren Tucker45150472006-07-12 22:34:17 +1000451 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
452 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000453#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100454 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000455 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000456#else
Darren Tucker1629c072007-02-19 22:25:37 +1100457 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000458 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000459#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100460 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
461 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100462 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000463 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
464 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
465 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
466 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
467 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
468 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
469 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
470 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000471 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
472 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
473 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000474 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
475 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100476 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000477 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
478 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
479 { "compression", sCompression, SSHCFG_GLOBAL },
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000480 { "rekeylimit", sRekeyLimit, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000481 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
482 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
483 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000484 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Damien Millerc24da772012-06-20 21:53:58 +1000485 { "allowusers", sAllowUsers, SSHCFG_ALL },
486 { "denyusers", sDenyUsers, SSHCFG_ALL },
487 { "allowgroups", sAllowGroups, SSHCFG_ALL },
488 { "denygroups", sDenyGroups, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000489 { "ciphers", sCiphers, SSHCFG_GLOBAL },
490 { "macs", sMacs, SSHCFG_GLOBAL },
491 { "protocol", sProtocol, SSHCFG_GLOBAL },
492 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
493 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
494 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000495 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000496 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100497 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000498 { "usedns", sUseDNS, SSHCFG_GLOBAL },
499 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
500 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
501 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
502 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000503 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000504 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000505 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Damien Millerc24da772012-06-20 21:53:58 +1000506 { "acceptenv", sAcceptEnv, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000507 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Damien Miller5ff30c62013-10-30 22:21:50 +1100508 { "permittty", sPermitTTY, SSHCFG_ALL },
Damien Miller72e6b5c2014-07-04 09:00:04 +1000509 { "permituserrc", sPermitUserRC, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000510 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000511 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000512 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100513 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100514 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100515 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
516 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000517 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000518 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100519 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller09d3e122012-10-31 08:58:58 +1100520 { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
521 { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000522 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Damien Millera6e3f012012-11-04 23:21:40 +1100523 { "authenticationmethods", sAuthenticationMethods, SSHCFG_ALL },
Damien Miller7acefbb2014-07-18 14:11:24 +1000524 { "streamlocalbindmask", sStreamLocalBindMask, SSHCFG_ALL },
525 { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
526 { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000527 { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000528 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000529};
530
Darren Tuckere7140f22008-06-10 23:01:51 +1000531static struct {
532 int val;
533 char *text;
534} tunmode_desc[] = {
535 { SSH_TUNMODE_NO, "no" },
536 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
537 { SSH_TUNMODE_ETHERNET, "ethernet" },
538 { SSH_TUNMODE_YES, "yes" },
539 { -1, NULL }
540};
541
Damien Miller5428f641999-11-25 11:54:57 +1100542/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000543 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100544 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000545
Damien Miller4af51302000-04-16 11:18:38 +1000546static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100547parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000548 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000549{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000550 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551
Damien Miller95def091999-11-25 00:26:21 +1100552 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000553 if (strcasecmp(cp, keywords[i].name) == 0) {
554 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100555 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000556 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000558 error("%s: line %d: Bad configuration option: %s",
559 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100560 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561}
562
Darren Tucker88b6fb22010-01-13 22:44:29 +1100563char *
564derelativise_path(const char *path)
565{
Damien Miller44451d02010-03-26 10:40:04 +1100566 char *expanded, *ret, cwd[MAXPATHLEN];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100567
djm@openbsd.org161cf412014-12-22 07:55:51 +0000568 if (strcasecmp(path, "none") == 0)
569 return xstrdup("none");
Darren Tucker88b6fb22010-01-13 22:44:29 +1100570 expanded = tilde_expand_filename(path, getuid());
571 if (*expanded == '/')
572 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100573 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100574 fatal("%s: getcwd: %s", __func__, strerror(errno));
575 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tuckera627d422013-06-02 07:31:17 +1000576 free(expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100577 return ret;
578}
579
Ben Lindstrombba81212001-06-25 05:01:22 +0000580static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100581add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100582{
Damien Millereccb9de2005-06-17 12:59:34 +1000583 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100584
585 if (options->num_ports == 0)
586 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100587 if (options->address_family == -1)
588 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000589 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000590 for (i = 0; i < options->num_ports; i++)
591 add_one_listen_addr(options, addr, options->ports[i]);
592 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000593 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000594}
595
Ben Lindstrombba81212001-06-25 05:01:22 +0000596static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100597add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000598{
599 struct addrinfo hints, *ai, *aitop;
600 char strport[NI_MAXSERV];
601 int gaierr;
602
603 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100604 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000605 hints.ai_socktype = SOCK_STREAM;
606 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100607 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000608 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
609 fatal("bad addr or host: %s (%s)",
610 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100611 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000612 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
613 ;
614 ai->ai_next = options->listen_addrs;
615 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100616}
617
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000618struct connection_info *
619get_connection_info(int populate, int use_dns)
620{
621 static struct connection_info ci;
622
623 if (!populate)
624 return &ci;
625 ci.host = get_canonical_hostname(use_dns);
626 ci.address = get_remote_ipaddr();
627 ci.laddress = get_local_ipaddr(packet_get_connection_in());
628 ci.lport = get_local_port();
629 return &ci;
630}
631
Darren Tucker45150472006-07-12 22:34:17 +1000632/*
633 * The strategy for the Match blocks is that the config file is parsed twice.
634 *
635 * The first time is at startup. activep is initialized to 1 and the
636 * directives in the global context are processed and acted on. Hitting a
637 * Match directive unsets activep and the directives inside the block are
638 * checked for syntax only.
639 *
640 * The second time is after a connection has been established but before
641 * authentication. activep is initialized to 2 and global config directives
642 * are ignored since they have already been processed. If the criteria in a
643 * Match block is met, activep is set and the subsequent directives
644 * processed and actioned until EOF or another Match block unsets it. Any
645 * options set are copied into the main server config.
646 *
647 * Potential additions/improvements:
648 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
649 *
650 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
651 * Match Address 192.168.0.*
652 * Tag trusted
653 * Match Group wheel
654 * Tag trusted
655 * Match Tag trusted
656 * AllowTcpForwarding yes
657 * GatewayPorts clientspecified
658 * [...]
659 *
660 * - Add a PermittedChannelRequests directive
661 * Match Group shell
662 * PermittedChannelRequests session,forwarded-tcpip
663 */
664
665static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000666match_cfg_line_group(const char *grps, int line, const char *user)
667{
668 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000669 struct passwd *pw;
670
Damien Miller565ca3f2006-08-19 00:23:15 +1000671 if (user == NULL)
672 goto out;
673
674 if ((pw = getpwnam(user)) == NULL) {
675 debug("Can't match group at line %d because user %.100s does "
676 "not exist", line, user);
677 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
678 debug("Can't Match group because user %.100s not in any group "
679 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000680 } else if (ga_match_pattern_list(grps) != 1) {
681 debug("user %.100s does not match group list %.100s at line %d",
682 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000683 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000684 debug("user %.100s matched group list %.100s at line %d", user,
685 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000686 result = 1;
687 }
688out:
689 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000690 return result;
691}
692
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000693/*
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000694 * All of the attributes on a single Match line are ANDed together, so we need
Damien Miller03bf2e62013-10-24 21:01:26 +1100695 * to check every attribute and set the result to zero if any attribute does
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000696 * not match.
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000697 */
Damien Miller565ca3f2006-08-19 00:23:15 +1000698static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000699match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +1000700{
Damien Millercf31f382013-10-24 21:02:56 +1100701 int result = 1, attributes = 0, port;
Darren Tucker45150472006-07-12 22:34:17 +1000702 char *arg, *attrib, *cp = *condition;
703 size_t len;
704
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000705 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000706 debug3("checking syntax for 'Match %s'", cp);
707 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000708 debug3("checking match for '%s' user %s host %s addr %s "
709 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
710 ci->host ? ci->host : "(null)",
711 ci->address ? ci->address : "(null)",
712 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +1000713
714 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100715 attributes++;
716 if (strcasecmp(attrib, "all") == 0) {
717 if (attributes != 1 ||
718 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
719 error("'all' cannot be combined with other "
720 "Match attributes");
721 return -1;
722 }
723 *condition = cp;
724 return 1;
725 }
Darren Tucker45150472006-07-12 22:34:17 +1000726 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
727 error("Missing Match criteria for %s", attrib);
728 return -1;
729 }
730 len = strlen(arg);
731 if (strcasecmp(attrib, "user") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000732 if (ci == NULL || ci->user == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000733 result = 0;
734 continue;
735 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000736 if (match_pattern_list(ci->user, arg, len, 0) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000737 result = 0;
738 else
739 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000740 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000741 } else if (strcasecmp(attrib, "group") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000742 if (ci == NULL || ci->user == NULL) {
743 result = 0;
744 continue;
745 }
746 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +1000747 case -1:
748 return -1;
749 case 0:
750 result = 0;
751 }
Darren Tucker45150472006-07-12 22:34:17 +1000752 } else if (strcasecmp(attrib, "host") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000753 if (ci == NULL || ci->host == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000754 result = 0;
755 continue;
756 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000757 if (match_hostname(ci->host, arg, len) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000758 result = 0;
759 else
760 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000761 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +1000762 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000763 if (ci == NULL || ci->address == NULL) {
764 result = 0;
765 continue;
766 }
767 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000768 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000769 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000770 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000771 break;
772 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000773 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000774 result = 0;
775 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000776 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000777 return -1;
778 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000779 } else if (strcasecmp(attrib, "localaddress") == 0){
780 if (ci == NULL || ci->laddress == NULL) {
781 result = 0;
782 continue;
783 }
784 switch (addr_match_list(ci->laddress, arg)) {
785 case 1:
786 debug("connection from %.100s matched "
787 "'LocalAddress %.100s' at line %d",
788 ci->laddress, arg, line);
789 break;
790 case 0:
791 case -1:
792 result = 0;
793 break;
794 case -2:
795 return -1;
796 }
797 } else if (strcasecmp(attrib, "localport") == 0) {
798 if ((port = a2port(arg)) == -1) {
799 error("Invalid LocalPort '%s' on Match line",
800 arg);
801 return -1;
802 }
803 if (ci == NULL || ci->lport == 0) {
804 result = 0;
805 continue;
806 }
807 /* TODO support port lists */
808 if (port == ci->lport)
809 debug("connection from %.100s matched "
810 "'LocalPort %d' at line %d",
811 ci->laddress, port, line);
812 else
813 result = 0;
Darren Tucker45150472006-07-12 22:34:17 +1000814 } else {
815 error("Unsupported Match attribute %s", attrib);
816 return -1;
817 }
818 }
Damien Millercf31f382013-10-24 21:02:56 +1100819 if (attributes == 0) {
820 error("One or more attributes required for Match");
821 return -1;
822 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000823 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000824 debug3("match %sfound", result ? "" : "not ");
825 *condition = cp;
826 return result;
827}
828
Damien Millere2754432006-07-24 14:06:47 +1000829#define WHITESPACE " \t\r\n"
830
Damien Miller33322122011-06-20 14:43:11 +1000831/* Multistate option parsing */
832struct multistate {
833 char *key;
834 int value;
835};
836static const struct multistate multistate_addressfamily[] = {
837 { "inet", AF_INET },
838 { "inet6", AF_INET6 },
839 { "any", AF_UNSPEC },
840 { NULL, -1 }
841};
842static const struct multistate multistate_permitrootlogin[] = {
843 { "without-password", PERMIT_NO_PASSWD },
844 { "forced-commands-only", PERMIT_FORCED_ONLY },
845 { "yes", PERMIT_YES },
846 { "no", PERMIT_NO },
847 { NULL, -1 }
848};
849static const struct multistate multistate_compression[] = {
850 { "delayed", COMP_DELAYED },
851 { "yes", COMP_ZLIB },
852 { "no", COMP_NONE },
853 { NULL, -1 }
854};
855static const struct multistate multistate_gatewayports[] = {
856 { "clientspecified", 2 },
857 { "yes", 1 },
858 { "no", 0 },
859 { NULL, -1 }
860};
Damien Miller69ff1df2011-06-23 08:30:03 +1000861static const struct multistate multistate_privsep[] = {
Damien Miller5a5c2b92012-07-31 12:21:34 +1000862 { "yes", PRIVSEP_NOSANDBOX },
863 { "sandbox", PRIVSEP_ON },
864 { "nosandbox", PRIVSEP_NOSANDBOX },
Damien Miller69ff1df2011-06-23 08:30:03 +1000865 { "no", PRIVSEP_OFF },
866 { NULL, -1 }
867};
Damien Milleraa5b3f82012-12-03 09:50:54 +1100868static const struct multistate multistate_tcpfwd[] = {
869 { "yes", FORWARD_ALLOW },
870 { "all", FORWARD_ALLOW },
871 { "no", FORWARD_DENY },
872 { "remote", FORWARD_REMOTE },
873 { "local", FORWARD_LOCAL },
874 { NULL, -1 }
875};
Damien Miller33322122011-06-20 14:43:11 +1000876
Ben Lindstromade03f62001-12-06 18:22:17 +0000877int
878process_server_config_line(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000879 const char *filename, int linenum, int *activep,
880 struct connection_info *connectinfo)
Ben Lindstromade03f62001-12-06 18:22:17 +0000881{
Darren Tucker09c0f032013-05-16 20:48:57 +1000882 char *cp, **charptr, *arg, *p;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000883 int cmdline = 0, *intptr, value, value2, n, port;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100884 SyslogFacility *log_facility_ptr;
885 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000886 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000887 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000888 size_t len;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000889 long long val64;
Damien Miller33322122011-06-20 14:43:11 +1000890 const struct multistate *multistate_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000891
892 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100893 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100894 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000895 /* Ignore leading whitespace */
896 if (*arg == '\0')
897 arg = strdelim(&cp);
898 if (!arg || !*arg || *arg == '#')
899 return 0;
900 intptr = NULL;
901 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000902 opcode = parse_token(arg, filename, linenum, &flags);
903
904 if (activep == NULL) { /* We are processing a command line directive */
905 cmdline = 1;
906 activep = &cmdline;
907 }
908 if (*activep && opcode != sMatch)
909 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
910 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000911 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000912 fatal("%s line %d: Directive '%s' is not allowed "
913 "within a Match block", filename, linenum, arg);
914 } else { /* this is a directive we have already processed */
915 while (arg)
916 arg = strdelim(&cp);
917 return 0;
918 }
919 }
920
Ben Lindstromade03f62001-12-06 18:22:17 +0000921 switch (opcode) {
922 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000923 case sUsePAM:
924 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000925 goto parse_flag;
926
927 /* Standard Options */
928 case sBadOption:
929 return -1;
930 case sPort:
931 /* ignore ports from configfile if cmdline specifies ports */
932 if (options->ports_from_cmdline)
933 return 0;
934 if (options->listen_addrs != NULL)
935 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100936 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000937 if (options->num_ports >= MAX_PORTS)
938 fatal("%s line %d: too many ports.",
939 filename, linenum);
940 arg = strdelim(&cp);
941 if (!arg || *arg == '\0')
942 fatal("%s line %d: missing port number.",
943 filename, linenum);
944 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100945 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000946 fatal("%s line %d: Badly formatted port number.",
947 filename, linenum);
948 break;
949
950 case sServerKeyBits:
951 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +1000952 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +0000953 arg = strdelim(&cp);
954 if (!arg || *arg == '\0')
955 fatal("%s line %d: missing integer value.",
956 filename, linenum);
957 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000958 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000959 *intptr = value;
960 break;
961
962 case sLoginGraceTime:
963 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +1000964 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +0000965 arg = strdelim(&cp);
966 if (!arg || *arg == '\0')
967 fatal("%s line %d: missing time value.",
968 filename, linenum);
969 if ((value = convtime(arg)) == -1)
970 fatal("%s line %d: invalid time value.",
971 filename, linenum);
972 if (*intptr == -1)
973 *intptr = value;
974 break;
975
976 case sKeyRegenerationTime:
977 intptr = &options->key_regeneration_time;
978 goto parse_time;
979
980 case sListenAddress:
981 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100982 if (arg == NULL || *arg == '\0')
983 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000984 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000985 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
986 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
987 && strchr(p+1, ':') != NULL) {
988 add_listen_addr(options, arg, 0);
989 break;
990 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100991 p = hpdelim(&arg);
992 if (p == NULL)
993 fatal("%s line %d: bad address:port usage",
994 filename, linenum);
995 p = cleanhostname(p);
996 if (arg == NULL)
997 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100998 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100999 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001000
Damien Millerf91ee4c2005-03-01 21:24:33 +11001001 add_listen_addr(options, p, port);
1002
Ben Lindstromade03f62001-12-06 18:22:17 +00001003 break;
1004
Darren Tucker0f383232005-01-20 10:57:56 +11001005 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +10001006 intptr = &options->address_family;
1007 multistate_ptr = multistate_addressfamily;
1008 if (options->listen_addrs != NULL)
1009 fatal("%s line %d: address family must be specified "
1010 "before ListenAddress.", filename, linenum);
1011 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +11001012 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +10001013 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +10001014 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +10001015 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +10001016 value = -1;
1017 for (i = 0; multistate_ptr[i].key != NULL; i++) {
1018 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
1019 value = multistate_ptr[i].value;
1020 break;
1021 }
1022 }
1023 if (value == -1)
1024 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +11001025 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +10001026 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +11001027 *intptr = value;
1028 break;
1029
Ben Lindstromade03f62001-12-06 18:22:17 +00001030 case sHostKeyFile:
1031 intptr = &options->num_host_key_files;
1032 if (*intptr >= MAX_HOSTKEYS)
1033 fatal("%s line %d: too many host keys specified (max %d).",
1034 filename, linenum, MAX_HOSTKEYS);
1035 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +10001036 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +00001037 arg = strdelim(&cp);
1038 if (!arg || *arg == '\0')
1039 fatal("%s line %d: missing file name.",
1040 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001041 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +11001042 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001043 /* increase optional counter */
1044 if (intptr != NULL)
1045 *intptr = *intptr + 1;
1046 }
1047 break;
1048
Damien Miller85b45e02013-07-20 13:21:52 +10001049 case sHostKeyAgent:
1050 charptr = &options->host_key_agent;
1051 arg = strdelim(&cp);
1052 if (!arg || *arg == '\0')
1053 fatal("%s line %d: missing socket name.",
1054 filename, linenum);
1055 if (*activep && *charptr == NULL)
1056 *charptr = !strcmp(arg, SSH_AUTHSOCKET_ENV_NAME) ?
1057 xstrdup(arg) : derelativise_path(arg);
1058 break;
1059
Damien Miller0a80ca12010-02-27 07:55:05 +11001060 case sHostCertificate:
1061 intptr = &options->num_host_cert_files;
1062 if (*intptr >= MAX_HOSTKEYS)
1063 fatal("%s line %d: too many host certificates "
1064 "specified (max %d).", filename, linenum,
1065 MAX_HOSTCERTS);
1066 charptr = &options->host_cert_files[*intptr];
1067 goto parse_filename;
1068 break;
1069
Ben Lindstromade03f62001-12-06 18:22:17 +00001070 case sPidFile:
1071 charptr = &options->pid_file;
1072 goto parse_filename;
1073
1074 case sPermitRootLogin:
1075 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +10001076 multistate_ptr = multistate_permitrootlogin;
1077 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001078
1079 case sIgnoreRhosts:
1080 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +10001081 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +00001082 arg = strdelim(&cp);
1083 if (!arg || *arg == '\0')
1084 fatal("%s line %d: missing yes/no argument.",
1085 filename, linenum);
1086 value = 0; /* silence compiler */
1087 if (strcmp(arg, "yes") == 0)
1088 value = 1;
1089 else if (strcmp(arg, "no") == 0)
1090 value = 0;
1091 else
1092 fatal("%s line %d: Bad yes/no argument: %s",
1093 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +10001094 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001095 *intptr = value;
1096 break;
1097
1098 case sIgnoreUserKnownHosts:
1099 intptr = &options->ignore_user_known_hosts;
1100 goto parse_flag;
1101
Ben Lindstromade03f62001-12-06 18:22:17 +00001102 case sRhostsRSAAuthentication:
1103 intptr = &options->rhosts_rsa_authentication;
1104 goto parse_flag;
1105
1106 case sHostbasedAuthentication:
1107 intptr = &options->hostbased_authentication;
1108 goto parse_flag;
1109
1110 case sHostbasedUsesNameFromPacketOnly:
1111 intptr = &options->hostbased_uses_name_from_packet_only;
1112 goto parse_flag;
1113
1114 case sRSAAuthentication:
1115 intptr = &options->rsa_authentication;
1116 goto parse_flag;
1117
1118 case sPubkeyAuthentication:
1119 intptr = &options->pubkey_authentication;
1120 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001121
Ben Lindstromade03f62001-12-06 18:22:17 +00001122 case sKerberosAuthentication:
1123 intptr = &options->kerberos_authentication;
1124 goto parse_flag;
1125
1126 case sKerberosOrLocalPasswd:
1127 intptr = &options->kerberos_or_local_passwd;
1128 goto parse_flag;
1129
1130 case sKerberosTicketCleanup:
1131 intptr = &options->kerberos_ticket_cleanup;
1132 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001133
Darren Tucker22ef5082003-12-31 11:37:34 +11001134 case sKerberosGetAFSToken:
1135 intptr = &options->kerberos_get_afs_token;
1136 goto parse_flag;
1137
Darren Tucker0efd1552003-08-26 11:49:55 +10001138 case sGssAuthentication:
1139 intptr = &options->gss_authentication;
1140 goto parse_flag;
1141
1142 case sGssCleanupCreds:
1143 intptr = &options->gss_cleanup_creds;
1144 goto parse_flag;
1145
Ben Lindstromade03f62001-12-06 18:22:17 +00001146 case sPasswordAuthentication:
1147 intptr = &options->password_authentication;
1148 goto parse_flag;
1149
1150 case sKbdInteractiveAuthentication:
1151 intptr = &options->kbd_interactive_authentication;
1152 goto parse_flag;
1153
1154 case sChallengeResponseAuthentication:
1155 intptr = &options->challenge_response_authentication;
1156 goto parse_flag;
1157
1158 case sPrintMotd:
1159 intptr = &options->print_motd;
1160 goto parse_flag;
1161
1162 case sPrintLastLog:
1163 intptr = &options->print_lastlog;
1164 goto parse_flag;
1165
1166 case sX11Forwarding:
1167 intptr = &options->x11_forwarding;
1168 goto parse_flag;
1169
1170 case sX11DisplayOffset:
1171 intptr = &options->x11_display_offset;
1172 goto parse_int;
1173
Damien Miller95c249f2002-02-05 12:11:34 +11001174 case sX11UseLocalhost:
1175 intptr = &options->x11_use_localhost;
1176 goto parse_flag;
1177
Ben Lindstromade03f62001-12-06 18:22:17 +00001178 case sXAuthLocation:
1179 charptr = &options->xauth_location;
1180 goto parse_filename;
1181
Damien Miller5ff30c62013-10-30 22:21:50 +11001182 case sPermitTTY:
1183 intptr = &options->permit_tty;
1184 goto parse_flag;
1185
Damien Miller72e6b5c2014-07-04 09:00:04 +10001186 case sPermitUserRC:
1187 intptr = &options->permit_user_rc;
1188 goto parse_flag;
1189
Ben Lindstromade03f62001-12-06 18:22:17 +00001190 case sStrictModes:
1191 intptr = &options->strict_modes;
1192 goto parse_flag;
1193
Damien Miller12c150e2003-12-17 16:31:10 +11001194 case sTCPKeepAlive:
1195 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001196 goto parse_flag;
1197
1198 case sEmptyPasswd:
1199 intptr = &options->permit_empty_passwd;
1200 goto parse_flag;
1201
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001202 case sPermitUserEnvironment:
1203 intptr = &options->permit_user_env;
1204 goto parse_flag;
1205
Ben Lindstromade03f62001-12-06 18:22:17 +00001206 case sUseLogin:
1207 intptr = &options->use_login;
1208 goto parse_flag;
1209
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001210 case sCompression:
1211 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001212 multistate_ptr = multistate_compression;
1213 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001214
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001215 case sRekeyLimit:
1216 arg = strdelim(&cp);
1217 if (!arg || *arg == '\0')
1218 fatal("%.200s line %d: Missing argument.", filename,
1219 linenum);
1220 if (strcmp(arg, "default") == 0) {
1221 val64 = 0;
1222 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001223 if (scan_scaled(arg, &val64) == -1)
1224 fatal("%.200s line %d: Bad number '%s': %s",
1225 filename, linenum, arg, strerror(errno));
1226 /* check for too-large or too-small limits */
1227 if (val64 > UINT_MAX)
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001228 fatal("%.200s line %d: RekeyLimit too large",
1229 filename, linenum);
1230 if (val64 != 0 && val64 < 16)
1231 fatal("%.200s line %d: RekeyLimit too small",
1232 filename, linenum);
1233 }
1234 if (*activep && options->rekey_limit == -1)
1235 options->rekey_limit = (u_int32_t)val64;
1236 if (cp != NULL) { /* optional rekey interval present */
1237 if (strcmp(cp, "none") == 0) {
1238 (void)strdelim(&cp); /* discard */
1239 break;
1240 }
1241 intptr = &options->rekey_interval;
1242 goto parse_time;
1243 }
1244 break;
1245
Ben Lindstromade03f62001-12-06 18:22:17 +00001246 case sGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +10001247 intptr = &options->fwd_opts.gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001248 multistate_ptr = multistate_gatewayports;
1249 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001250
Damien Miller3a961dc2003-06-03 10:25:48 +10001251 case sUseDNS:
1252 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001253 goto parse_flag;
1254
1255 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001256 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001257 arg = strdelim(&cp);
1258 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001259 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001260 fatal("%.200s line %d: unsupported log facility '%s'",
1261 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001262 if (*log_facility_ptr == -1)
1263 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001264 break;
1265
1266 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001267 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001268 arg = strdelim(&cp);
1269 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001270 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001271 fatal("%.200s line %d: unsupported log level '%s'",
1272 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001273 if (*log_level_ptr == -1)
1274 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001275 break;
1276
1277 case sAllowTcpForwarding:
1278 intptr = &options->allow_tcp_forwarding;
Damien Milleraa5b3f82012-12-03 09:50:54 +11001279 multistate_ptr = multistate_tcpfwd;
1280 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001281
Damien Miller7acefbb2014-07-18 14:11:24 +10001282 case sAllowStreamLocalForwarding:
1283 intptr = &options->allow_streamlocal_forwarding;
1284 multistate_ptr = multistate_tcpfwd;
1285 goto parse_multistate;
1286
Damien Miller4f755cd2008-05-19 14:57:41 +10001287 case sAllowAgentForwarding:
1288 intptr = &options->allow_agent_forwarding;
1289 goto parse_flag;
1290
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001291 case sUsePrivilegeSeparation:
1292 intptr = &use_privsep;
Damien Miller69ff1df2011-06-23 08:30:03 +10001293 multistate_ptr = multistate_privsep;
1294 goto parse_multistate;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295
Ben Lindstromade03f62001-12-06 18:22:17 +00001296 case sAllowUsers:
1297 while ((arg = strdelim(&cp)) && *arg != '\0') {
1298 if (options->num_allow_users >= MAX_ALLOW_USERS)
1299 fatal("%s line %d: too many allow users.",
1300 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001301 if (!*activep)
1302 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001303 options->allow_users[options->num_allow_users++] =
1304 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001305 }
1306 break;
1307
1308 case sDenyUsers:
1309 while ((arg = strdelim(&cp)) && *arg != '\0') {
1310 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001311 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001312 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001313 if (!*activep)
1314 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001315 options->deny_users[options->num_deny_users++] =
1316 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001317 }
1318 break;
1319
1320 case sAllowGroups:
1321 while ((arg = strdelim(&cp)) && *arg != '\0') {
1322 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1323 fatal("%s line %d: too many allow groups.",
1324 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001325 if (!*activep)
1326 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001327 options->allow_groups[options->num_allow_groups++] =
1328 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001329 }
1330 break;
1331
1332 case sDenyGroups:
1333 while ((arg = strdelim(&cp)) && *arg != '\0') {
1334 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1335 fatal("%s line %d: too many deny groups.",
1336 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001337 if (!*activep)
1338 continue;
1339 options->deny_groups[options->num_deny_groups++] =
1340 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001341 }
1342 break;
1343
1344 case sCiphers:
1345 arg = strdelim(&cp);
1346 if (!arg || *arg == '\0')
1347 fatal("%s line %d: Missing argument.", filename, linenum);
1348 if (!ciphers_valid(arg))
1349 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1350 filename, linenum, arg ? arg : "<NONE>");
1351 if (options->ciphers == NULL)
1352 options->ciphers = xstrdup(arg);
1353 break;
1354
1355 case sMacs:
1356 arg = strdelim(&cp);
1357 if (!arg || *arg == '\0')
1358 fatal("%s line %d: Missing argument.", filename, linenum);
1359 if (!mac_valid(arg))
1360 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1361 filename, linenum, arg ? arg : "<NONE>");
1362 if (options->macs == NULL)
1363 options->macs = xstrdup(arg);
1364 break;
1365
Damien Millerd5f62bf2010-09-24 22:11:14 +10001366 case sKexAlgorithms:
1367 arg = strdelim(&cp);
1368 if (!arg || *arg == '\0')
1369 fatal("%s line %d: Missing argument.",
1370 filename, linenum);
1371 if (!kex_names_valid(arg))
1372 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1373 filename, linenum, arg ? arg : "<NONE>");
1374 if (options->kex_algorithms == NULL)
1375 options->kex_algorithms = xstrdup(arg);
1376 break;
1377
Ben Lindstromade03f62001-12-06 18:22:17 +00001378 case sProtocol:
1379 intptr = &options->protocol;
1380 arg = strdelim(&cp);
1381 if (!arg || *arg == '\0')
1382 fatal("%s line %d: Missing argument.", filename, linenum);
1383 value = proto_spec(arg);
1384 if (value == SSH_PROTO_UNKNOWN)
1385 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001386 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001387 if (*intptr == SSH_PROTO_UNKNOWN)
1388 *intptr = value;
1389 break;
1390
1391 case sSubsystem:
1392 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1393 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001394 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001395 }
1396 arg = strdelim(&cp);
1397 if (!arg || *arg == '\0')
1398 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001399 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001400 if (!*activep) {
1401 arg = strdelim(&cp);
1402 break;
1403 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001404 for (i = 0; i < options->num_subsystems; i++)
1405 if (strcmp(arg, options->subsystem_name[i]) == 0)
1406 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001407 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001408 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1409 arg = strdelim(&cp);
1410 if (!arg || *arg == '\0')
1411 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001412 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001413 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001414
1415 /* Collect arguments (separate to executable) */
1416 p = xstrdup(arg);
1417 len = strlen(p) + 1;
1418 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1419 len += 1 + strlen(arg);
1420 p = xrealloc(p, 1, len);
1421 strlcat(p, " ", len);
1422 strlcat(p, arg, len);
1423 }
1424 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001425 options->num_subsystems++;
1426 break;
1427
1428 case sMaxStartups:
1429 arg = strdelim(&cp);
1430 if (!arg || *arg == '\0')
1431 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001432 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001433 if ((n = sscanf(arg, "%d:%d:%d",
1434 &options->max_startups_begin,
1435 &options->max_startups_rate,
1436 &options->max_startups)) == 3) {
1437 if (options->max_startups_begin >
1438 options->max_startups ||
1439 options->max_startups_rate > 100 ||
1440 options->max_startups_rate < 1)
1441 fatal("%s line %d: Illegal MaxStartups spec.",
1442 filename, linenum);
1443 } else if (n != 1)
1444 fatal("%s line %d: Illegal MaxStartups spec.",
1445 filename, linenum);
1446 else
1447 options->max_startups = options->max_startups_begin;
1448 break;
1449
Darren Tucker89413db2004-05-24 10:36:23 +10001450 case sMaxAuthTries:
1451 intptr = &options->max_authtries;
1452 goto parse_int;
1453
Damien Miller7207f642008-05-19 15:34:50 +10001454 case sMaxSessions:
1455 intptr = &options->max_sessions;
1456 goto parse_int;
1457
Ben Lindstromade03f62001-12-06 18:22:17 +00001458 case sBanner:
1459 charptr = &options->banner;
1460 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001461
Ben Lindstromade03f62001-12-06 18:22:17 +00001462 /*
1463 * These options can contain %X options expanded at
1464 * connect time, so that you can specify paths like:
1465 *
1466 * AuthorizedKeysFile /etc/ssh_keys/%u
1467 */
1468 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001469 if (*activep && options->num_authkeys_files == 0) {
1470 while ((arg = strdelim(&cp)) && *arg != '\0') {
1471 if (options->num_authkeys_files >=
1472 MAX_AUTHKEYS_FILES)
1473 fatal("%s line %d: "
1474 "too many authorized keys files.",
1475 filename, linenum);
1476 options->authorized_keys_files[
1477 options->num_authkeys_files++] =
1478 tilde_expand_filename(arg, getuid());
1479 }
1480 }
1481 return 0;
1482
Damien Miller30da3442010-05-10 11:58:03 +10001483 case sAuthorizedPrincipalsFile:
1484 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001485 arg = strdelim(&cp);
1486 if (!arg || *arg == '\0')
1487 fatal("%s line %d: missing file name.",
1488 filename, linenum);
1489 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001490 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001491 /* increase optional counter */
1492 if (intptr != NULL)
1493 *intptr = *intptr + 1;
1494 }
1495 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001496
1497 case sClientAliveInterval:
1498 intptr = &options->client_alive_interval;
1499 goto parse_time;
1500
1501 case sClientAliveCountMax:
1502 intptr = &options->client_alive_count_max;
1503 goto parse_int;
1504
Darren Tucker46bc0752004-05-02 22:11:30 +10001505 case sAcceptEnv:
1506 while ((arg = strdelim(&cp)) && *arg != '\0') {
1507 if (strchr(arg, '=') != NULL)
1508 fatal("%s line %d: Invalid environment name.",
1509 filename, linenum);
1510 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1511 fatal("%s line %d: too many allow env.",
1512 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001513 if (!*activep)
Damien Millerc24da772012-06-20 21:53:58 +10001514 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001515 options->accept_env[options->num_accept_env++] =
1516 xstrdup(arg);
1517 }
1518 break;
1519
Damien Millerd27b9472005-12-13 19:29:02 +11001520 case sPermitTunnel:
1521 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001522 arg = strdelim(&cp);
1523 if (!arg || *arg == '\0')
1524 fatal("%s line %d: Missing yes/point-to-point/"
1525 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001526 value = -1;
1527 for (i = 0; tunmode_desc[i].val != -1; i++)
1528 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1529 value = tunmode_desc[i].val;
1530 break;
1531 }
1532 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001533 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1534 "no argument: %s", filename, linenum, arg);
1535 if (*intptr == -1)
1536 *intptr = value;
1537 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001538
Darren Tucker45150472006-07-12 22:34:17 +10001539 case sMatch:
1540 if (cmdline)
1541 fatal("Match directive not supported as a command-line "
1542 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001543 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10001544 if (value < 0)
1545 fatal("%s line %d: Bad Match condition", filename,
1546 linenum);
1547 *activep = value;
1548 break;
1549
Damien Miller9b439df2006-07-24 14:04:00 +10001550 case sPermitOpen:
1551 arg = strdelim(&cp);
1552 if (!arg || *arg == '\0')
1553 fatal("%s line %d: missing PermitOpen specification",
1554 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001555 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001556 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001557 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001558 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001559 options->num_permitted_opens = 0;
1560 }
Damien Miller9b439df2006-07-24 14:04:00 +10001561 break;
1562 }
Damien Millerc6081482012-04-22 11:18:53 +10001563 if (strcmp(arg, "none") == 0) {
1564 if (*activep && n == -1) {
Damien Millerc6081482012-04-22 11:18:53 +10001565 options->num_permitted_opens = 1;
1566 channel_disable_adm_local_opens();
1567 }
1568 break;
1569 }
Damien Millera29b95e2007-01-05 16:28:36 +11001570 if (*activep && n == -1)
1571 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001572 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1573 p = hpdelim(&arg);
1574 if (p == NULL)
1575 fatal("%s line %d: missing host in PermitOpen",
1576 filename, linenum);
1577 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001578 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001579 fatal("%s line %d: bad port number in "
1580 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001581 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001582 options->num_permitted_opens =
1583 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001584 }
Damien Miller9b439df2006-07-24 14:04:00 +10001585 break;
1586
Damien Millere2754432006-07-24 14:06:47 +10001587 case sForceCommand:
1588 if (cp == NULL)
1589 fatal("%.200s line %d: Missing argument.", filename,
1590 linenum);
1591 len = strspn(cp, WHITESPACE);
1592 if (*activep && options->adm_forced_command == NULL)
1593 options->adm_forced_command = xstrdup(cp + len);
1594 return 0;
1595
Damien Millerd8cb1f12008-02-10 22:40:12 +11001596 case sChrootDirectory:
1597 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001598
1599 arg = strdelim(&cp);
1600 if (!arg || *arg == '\0')
1601 fatal("%s line %d: missing file name.",
1602 filename, linenum);
1603 if (*activep && *charptr == NULL)
1604 *charptr = xstrdup(arg);
1605 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001606
Damien Miller1aed65e2010-03-04 21:53:35 +11001607 case sTrustedUserCAKeys:
1608 charptr = &options->trusted_user_ca_keys;
1609 goto parse_filename;
1610
1611 case sRevokedKeys:
1612 charptr = &options->revoked_keys_file;
1613 goto parse_filename;
1614
Damien Miller0dac6fb2010-11-20 15:19:38 +11001615 case sIPQoS:
1616 arg = strdelim(&cp);
1617 if ((value = parse_ipqos(arg)) == -1)
1618 fatal("%s line %d: Bad IPQoS value: %s",
1619 filename, linenum, arg);
1620 arg = strdelim(&cp);
1621 if (arg == NULL)
1622 value2 = value;
1623 else if ((value2 = parse_ipqos(arg)) == -1)
1624 fatal("%s line %d: Bad IPQoS value: %s",
1625 filename, linenum, arg);
1626 if (*activep) {
1627 options->ip_qos_interactive = value;
1628 options->ip_qos_bulk = value2;
1629 }
1630 break;
1631
Damien Miller23528812012-04-22 11:24:43 +10001632 case sVersionAddendum:
1633 if (cp == NULL)
1634 fatal("%.200s line %d: Missing argument.", filename,
1635 linenum);
1636 len = strspn(cp, WHITESPACE);
1637 if (*activep && options->version_addendum == NULL) {
1638 if (strcasecmp(cp + len, "none") == 0)
1639 options->version_addendum = xstrdup("");
1640 else if (strchr(cp + len, '\r') != NULL)
1641 fatal("%.200s line %d: Invalid argument",
1642 filename, linenum);
1643 else
1644 options->version_addendum = xstrdup(cp + len);
1645 }
1646 return 0;
1647
Damien Miller09d3e122012-10-31 08:58:58 +11001648 case sAuthorizedKeysCommand:
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001649 if (cp == NULL)
1650 fatal("%.200s line %d: Missing argument.", filename,
1651 linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001652 len = strspn(cp, WHITESPACE);
1653 if (*activep && options->authorized_keys_command == NULL) {
1654 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1655 fatal("%.200s line %d: AuthorizedKeysCommand "
1656 "must be an absolute path",
1657 filename, linenum);
1658 options->authorized_keys_command = xstrdup(cp + len);
1659 }
1660 return 0;
1661
1662 case sAuthorizedKeysCommandUser:
1663 charptr = &options->authorized_keys_command_user;
1664
1665 arg = strdelim(&cp);
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001666 if (!arg || *arg == '\0')
1667 fatal("%s line %d: missing AuthorizedKeysCommandUser "
1668 "argument.", filename, linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001669 if (*activep && *charptr == NULL)
1670 *charptr = xstrdup(arg);
1671 break;
1672
Damien Millera6e3f012012-11-04 23:21:40 +11001673 case sAuthenticationMethods:
1674 if (*activep && options->num_auth_methods == 0) {
1675 while ((arg = strdelim(&cp)) && *arg != '\0') {
1676 if (options->num_auth_methods >=
1677 MAX_AUTH_METHODS)
1678 fatal("%s line %d: "
1679 "too many authentication methods.",
1680 filename, linenum);
1681 if (auth2_methods_valid(arg, 0) != 0)
1682 fatal("%s line %d: invalid "
1683 "authentication method list.",
1684 filename, linenum);
1685 options->auth_methods[
1686 options->num_auth_methods++] = xstrdup(arg);
1687 }
1688 }
1689 return 0;
1690
Damien Miller7acefbb2014-07-18 14:11:24 +10001691 case sStreamLocalBindMask:
1692 arg = strdelim(&cp);
1693 if (!arg || *arg == '\0')
1694 fatal("%s line %d: missing StreamLocalBindMask argument.",
1695 filename, linenum);
1696 /* Parse mode in octal format */
1697 value = strtol(arg, &p, 8);
1698 if (arg == p || value < 0 || value > 0777)
1699 fatal("%s line %d: Bad mask.", filename, linenum);
1700 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1701 break;
1702
1703 case sStreamLocalBindUnlink:
1704 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1705 goto parse_flag;
1706
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001707 case sFingerprintHash:
1708 arg = strdelim(&cp);
1709 if (!arg || *arg == '\0')
1710 fatal("%.200s line %d: Missing argument.",
1711 filename, linenum);
1712 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1713 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1714 filename, linenum, arg);
1715 if (*activep)
1716 options->fingerprint_hash = value;
1717 break;
1718
Ben Lindstromade03f62001-12-06 18:22:17 +00001719 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001720 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001721 filename, linenum, arg);
1722 while (arg)
1723 arg = strdelim(&cp);
1724 break;
1725
Damien Millerf9b3feb2003-05-16 11:38:32 +10001726 case sUnsupported:
1727 logit("%s line %d: Unsupported option %s",
1728 filename, linenum, arg);
1729 while (arg)
1730 arg = strdelim(&cp);
1731 break;
1732
Ben Lindstromade03f62001-12-06 18:22:17 +00001733 default:
1734 fatal("%s line %d: Missing handler for opcode %s (%d)",
1735 filename, linenum, arg, opcode);
1736 }
1737 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1738 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1739 filename, linenum, arg);
1740 return 0;
1741}
1742
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001743/* Reads the server configuration file. */
1744
Damien Miller4af51302000-04-16 11:18:38 +10001745void
Darren Tucker645ab752004-06-25 13:33:20 +10001746load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001747{
Damien Miller46cb75a2012-07-31 12:22:37 +10001748 char line[4096], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001749 FILE *f;
Damien Miller46cb75a2012-07-31 12:22:37 +10001750 int lineno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001751
Darren Tucker645ab752004-06-25 13:33:20 +10001752 debug2("%s: filename %s", __func__, filename);
1753 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001754 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001755 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001756 }
Darren Tucker645ab752004-06-25 13:33:20 +10001757 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001758 while (fgets(line, sizeof(line), f)) {
Damien Miller46cb75a2012-07-31 12:22:37 +10001759 lineno++;
1760 if (strlen(line) == sizeof(line) - 1)
1761 fatal("%s line %d too long", filename, lineno);
Darren Tucker645ab752004-06-25 13:33:20 +10001762 /*
1763 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001764 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001765 * line numbers later for error messages
1766 */
1767 if ((cp = strchr(line, '#')) != NULL)
1768 memcpy(cp, "\n", 2);
1769 cp = line + strspn(line, " \t\r");
1770
1771 buffer_append(conf, cp, strlen(cp));
1772 }
1773 buffer_append(conf, "\0", 1);
1774 fclose(f);
1775 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1776}
1777
1778void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001779parse_server_match_config(ServerOptions *options,
1780 struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10001781{
Darren Tucker45150472006-07-12 22:34:17 +10001782 ServerOptions mo;
1783
1784 initialize_server_options(&mo);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001785 parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11001786 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001787}
1788
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001789int parse_server_match_testspec(struct connection_info *ci, char *spec)
1790{
1791 char *p;
1792
1793 while ((p = strsep(&spec, ",")) && *p != '\0') {
1794 if (strncmp(p, "addr=", 5) == 0) {
1795 ci->address = xstrdup(p + 5);
1796 } else if (strncmp(p, "host=", 5) == 0) {
1797 ci->host = xstrdup(p + 5);
1798 } else if (strncmp(p, "user=", 5) == 0) {
1799 ci->user = xstrdup(p + 5);
1800 } else if (strncmp(p, "laddr=", 6) == 0) {
1801 ci->laddress = xstrdup(p + 6);
1802 } else if (strncmp(p, "lport=", 6) == 0) {
1803 ci->lport = a2port(p + 6);
1804 if (ci->lport == -1) {
1805 fprintf(stderr, "Invalid port '%s' in test mode"
1806 " specification %s\n", p+6, p);
1807 return -1;
1808 }
1809 } else {
1810 fprintf(stderr, "Invalid test mode specification %s\n",
1811 p);
1812 return -1;
1813 }
1814 }
1815 return 0;
1816}
1817
1818/*
1819 * returns 1 for a complete spec, 0 for partial spec and -1 for an
1820 * empty spec.
1821 */
1822int server_match_spec_complete(struct connection_info *ci)
1823{
1824 if (ci->user && ci->host && ci->address)
1825 return 1; /* complete */
1826 if (!ci->user && !ci->host && !ci->address)
1827 return -1; /* empty */
1828 return 0; /* partial */
1829}
1830
Darren Tucker1629c072007-02-19 22:25:37 +11001831/*
1832 * Copy any supported values that are set.
1833 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001834 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001835 * array values that are not used pre-authentication, because any that we
1836 * do use must be explictly sent in mm_getpwnamallow().
1837 */
Darren Tucker45150472006-07-12 22:34:17 +10001838void
Darren Tucker1629c072007-02-19 22:25:37 +11001839copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001840{
Damien Miller534b2cc2013-12-05 14:07:27 +11001841#define M_CP_INTOPT(n) do {\
1842 if (src->n != -1) \
1843 dst->n = src->n; \
1844} while (0)
1845
Darren Tucker1629c072007-02-19 22:25:37 +11001846 M_CP_INTOPT(password_authentication);
1847 M_CP_INTOPT(gss_authentication);
1848 M_CP_INTOPT(rsa_authentication);
1849 M_CP_INTOPT(pubkey_authentication);
1850 M_CP_INTOPT(kerberos_authentication);
1851 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001852 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001853 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001854 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001855 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001856
1857 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10001858 M_CP_INTOPT(allow_streamlocal_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001859 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001860 M_CP_INTOPT(permit_tun);
Damien Miller7acefbb2014-07-18 14:11:24 +10001861 M_CP_INTOPT(fwd_opts.gateway_ports);
Darren Tucker1629c072007-02-19 22:25:37 +11001862 M_CP_INTOPT(x11_display_offset);
1863 M_CP_INTOPT(x11_forwarding);
1864 M_CP_INTOPT(x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11001865 M_CP_INTOPT(permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10001866 M_CP_INTOPT(permit_user_rc);
Damien Miller7207f642008-05-19 15:34:50 +10001867 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001868 M_CP_INTOPT(max_authtries);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001869 M_CP_INTOPT(ip_qos_interactive);
1870 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001871 M_CP_INTOPT(rekey_limit);
1872 M_CP_INTOPT(rekey_interval);
Darren Tucker1629c072007-02-19 22:25:37 +11001873
Damien Miller534b2cc2013-12-05 14:07:27 +11001874 /* M_CP_STROPT and M_CP_STRARRAYOPT should not appear before here */
1875#define M_CP_STROPT(n) do {\
1876 if (src->n != NULL && dst->n != src->n) { \
1877 free(dst->n); \
1878 dst->n = src->n; \
1879 } \
1880} while(0)
1881#define M_CP_STRARRAYOPT(n, num_n) do {\
1882 if (src->num_n != 0) { \
1883 for (dst->num_n = 0; dst->num_n < src->num_n; dst->num_n++) \
1884 dst->n[dst->num_n] = xstrdup(src->n[dst->num_n]); \
1885 } \
1886} while(0)
1887
Damien Millerf2e407e2011-05-20 19:04:14 +10001888 /* See comment in servconf.h */
1889 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10001890
Damien Millerc2411902011-05-20 19:03:49 +10001891 /*
1892 * The only things that should be below this point are string options
1893 * which are only used after authentication.
1894 */
Damien Miller5d74e582011-05-20 19:03:31 +10001895 if (preauth)
1896 return;
Damien Millerd8478b62011-05-29 21:39:36 +10001897
Damien Miller5d74e582011-05-20 19:03:31 +10001898 M_CP_STROPT(adm_forced_command);
1899 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001900}
1901
Darren Tucker1629c072007-02-19 22:25:37 +11001902#undef M_CP_INTOPT
1903#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10001904#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11001905
Darren Tucker45150472006-07-12 22:34:17 +10001906void
1907parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001908 struct connection_info *connectinfo)
Darren Tucker45150472006-07-12 22:34:17 +10001909{
1910 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001911 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001912
1913 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1914
Darren Tucker9fbac712004-08-12 22:41:44 +10001915 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001916 active = connectinfo ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001917 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001918 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001919 if (process_server_config_line(options, cp, filename,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001920 linenum++, &active, connectinfo) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001921 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001922 }
Darren Tuckera627d422013-06-02 07:31:17 +10001923 free(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001924 if (bad_options > 0)
1925 fatal("%s: terminating, %d bad configuration options",
1926 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001927}
Darren Tuckere7140f22008-06-10 23:01:51 +10001928
1929static const char *
Damien Miller82c55872011-06-23 08:20:30 +10001930fmt_multistate_int(int val, const struct multistate *m)
1931{
1932 u_int i;
1933
1934 for (i = 0; m[i].key != NULL; i++) {
1935 if (m[i].value == val)
1936 return m[i].key;
1937 }
1938 return "UNKNOWN";
1939}
1940
1941static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10001942fmt_intarg(ServerOpCodes code, int val)
1943{
Damien Miller82c55872011-06-23 08:20:30 +10001944 if (val == -1)
1945 return "unset";
1946 switch (code) {
1947 case sAddressFamily:
1948 return fmt_multistate_int(val, multistate_addressfamily);
1949 case sPermitRootLogin:
1950 return fmt_multistate_int(val, multistate_permitrootlogin);
1951 case sGatewayPorts:
1952 return fmt_multistate_int(val, multistate_gatewayports);
1953 case sCompression:
1954 return fmt_multistate_int(val, multistate_compression);
Damien Miller69ff1df2011-06-23 08:30:03 +10001955 case sUsePrivilegeSeparation:
1956 return fmt_multistate_int(val, multistate_privsep);
Damien Milleraa5b3f82012-12-03 09:50:54 +11001957 case sAllowTcpForwarding:
1958 return fmt_multistate_int(val, multistate_tcpfwd);
Damien Miller7acefbb2014-07-18 14:11:24 +10001959 case sAllowStreamLocalForwarding:
1960 return fmt_multistate_int(val, multistate_tcpfwd);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001961 case sFingerprintHash:
1962 return ssh_digest_alg_name(val);
Damien Miller82c55872011-06-23 08:20:30 +10001963 case sProtocol:
Darren Tuckere7140f22008-06-10 23:01:51 +10001964 switch (val) {
1965 case SSH_PROTO_1:
1966 return "1";
1967 case SSH_PROTO_2:
1968 return "2";
1969 case (SSH_PROTO_1|SSH_PROTO_2):
1970 return "2,1";
1971 default:
1972 return "UNKNOWN";
1973 }
Damien Miller82c55872011-06-23 08:20:30 +10001974 default:
1975 switch (val) {
1976 case 0:
1977 return "no";
1978 case 1:
1979 return "yes";
1980 default:
1981 return "UNKNOWN";
1982 }
Darren Tuckere7140f22008-06-10 23:01:51 +10001983 }
Darren Tuckere7140f22008-06-10 23:01:51 +10001984}
1985
1986static const char *
1987lookup_opcode_name(ServerOpCodes code)
1988{
1989 u_int i;
1990
1991 for (i = 0; keywords[i].name != NULL; i++)
1992 if (keywords[i].opcode == code)
1993 return(keywords[i].name);
1994 return "UNKNOWN";
1995}
1996
1997static void
1998dump_cfg_int(ServerOpCodes code, int val)
1999{
2000 printf("%s %d\n", lookup_opcode_name(code), val);
2001}
2002
2003static void
2004dump_cfg_fmtint(ServerOpCodes code, int val)
2005{
2006 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2007}
2008
2009static void
2010dump_cfg_string(ServerOpCodes code, const char *val)
2011{
2012 if (val == NULL)
2013 return;
djm@openbsd.org161cf412014-12-22 07:55:51 +00002014 printf("%s %s\n", lookup_opcode_name(code),
2015 val == NULL ? "none" : val);
Darren Tuckere7140f22008-06-10 23:01:51 +10002016}
2017
2018static void
2019dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
2020{
2021 u_int i;
2022
2023 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10002024 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2025}
2026
2027static void
2028dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
2029{
2030 u_int i;
2031
2032 printf("%s", lookup_opcode_name(code));
2033 for (i = 0; i < count; i++)
2034 printf(" %s", vals[i]);
2035 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002036}
2037
2038void
2039dump_config(ServerOptions *o)
2040{
2041 u_int i;
2042 int ret;
2043 struct addrinfo *ai;
2044 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
2045
2046 /* these are usually at the top of the config */
2047 for (i = 0; i < o->num_ports; i++)
2048 printf("port %d\n", o->ports[i]);
2049 dump_cfg_fmtint(sProtocol, o->protocol);
2050 dump_cfg_fmtint(sAddressFamily, o->address_family);
2051
2052 /* ListenAddress must be after Port */
2053 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
2054 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
2055 sizeof(addr), port, sizeof(port),
2056 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
2057 error("getnameinfo failed: %.100s",
2058 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
2059 strerror(errno));
2060 } else {
2061 if (ai->ai_family == AF_INET6)
2062 printf("listenaddress [%s]:%s\n", addr, port);
2063 else
2064 printf("listenaddress %s:%s\n", addr, port);
2065 }
2066 }
2067
2068 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10002069#ifdef USE_PAM
2070 dump_cfg_int(sUsePAM, o->use_pam);
2071#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002072 dump_cfg_int(sServerKeyBits, o->server_key_bits);
2073 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
2074 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
2075 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
2076 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11002077 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10002078 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
2079 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
2080
2081 /* formatted integer arguments */
2082 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
2083 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
2084 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
2085 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2086 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
2087 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
2088 o->hostbased_uses_name_from_packet_only);
2089 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
2090 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10002091#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10002092 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
2093 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
2094 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10002095# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10002096 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10002097# endif
2098#endif
2099#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10002100 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2101 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10002102#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002103 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2104 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2105 o->kbd_interactive_authentication);
2106 dump_cfg_fmtint(sChallengeResponseAuthentication,
2107 o->challenge_response_authentication);
2108 dump_cfg_fmtint(sPrintMotd, o->print_motd);
2109 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
2110 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
2111 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002112 dump_cfg_fmtint(sPermitTTY, o->permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002113 dump_cfg_fmtint(sPermitUserRC, o->permit_user_rc);
Darren Tuckere7140f22008-06-10 23:01:51 +10002114 dump_cfg_fmtint(sStrictModes, o->strict_modes);
2115 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
2116 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
2117 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
2118 dump_cfg_fmtint(sUseLogin, o->use_login);
2119 dump_cfg_fmtint(sCompression, o->compression);
Damien Miller7acefbb2014-07-18 14:11:24 +10002120 dump_cfg_fmtint(sGatewayPorts, o->fwd_opts.gateway_ports);
Darren Tuckere7140f22008-06-10 23:01:51 +10002121 dump_cfg_fmtint(sUseDNS, o->use_dns);
2122 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002123 dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
Darren Tuckere7140f22008-06-10 23:01:51 +10002124 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002125 dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
Darren Tuckere7140f22008-06-10 23:01:51 +10002126
2127 /* string arguments */
2128 dump_cfg_string(sPidFile, o->pid_file);
2129 dump_cfg_string(sXAuthLocation, o->xauth_location);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002130 dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
2131 dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
Darren Tuckere7140f22008-06-10 23:01:51 +10002132 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10002133 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11002134 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11002135 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
2136 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10002137 dump_cfg_string(sAuthorizedPrincipalsFile,
2138 o->authorized_principals_file);
Damien Miller23528812012-04-22 11:24:43 +10002139 dump_cfg_string(sVersionAddendum, o->version_addendum);
Damien Miller09d3e122012-10-31 08:58:58 +11002140 dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
2141 dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
Damien Miller85b45e02013-07-20 13:21:52 +10002142 dump_cfg_string(sHostKeyAgent, o->host_key_agent);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002143 dump_cfg_string(sKexAlgorithms,
djm@openbsd.org259a02e2014-10-13 00:38:35 +00002144 o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
Darren Tuckere7140f22008-06-10 23:01:51 +10002145
2146 /* string arguments requiring a lookup */
2147 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
2148 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
2149
2150 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10002151 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
2152 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002153 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
2154 o->host_key_files);
Damien Miller0a80ca12010-02-27 07:55:05 +11002155 dump_cfg_strarray(sHostKeyFile, o->num_host_cert_files,
2156 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002157 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
2158 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
2159 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
2160 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
2161 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
Damien Millera6e3f012012-11-04 23:21:40 +11002162 dump_cfg_strarray_oneline(sAuthenticationMethods,
2163 o->num_auth_methods, o->auth_methods);
Darren Tuckere7140f22008-06-10 23:01:51 +10002164
2165 /* other arguments */
2166 for (i = 0; i < o->num_subsystems; i++)
2167 printf("subsystem %s %s\n", o->subsystem_name[i],
2168 o->subsystem_args[i]);
2169
2170 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
2171 o->max_startups_rate, o->max_startups);
2172
2173 for (i = 0; tunmode_desc[i].val != -1; i++)
2174 if (tunmode_desc[i].val == o->permit_tun) {
2175 s = tunmode_desc[i].text;
2176 break;
2177 }
2178 dump_cfg_string(sPermitTunnel, s);
2179
Damien Miller91475862011-05-05 14:14:34 +10002180 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2181 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11002182
Damien Millera6d6c1f2013-08-21 02:40:01 +10002183 printf("rekeylimit %lld %d\n", (long long)o->rekey_limit,
2184 o->rekey_interval);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002185
Darren Tuckere7140f22008-06-10 23:01:51 +10002186 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10002187}