blob: 29457b8335637ad9ecbdc528830af1b6ead743e8 [file] [log] [blame]
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00002/* $OpenBSD: servconf.c,v 1.266 2015/04/29 03:48:56 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003/*
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
Damien Millere3b60b52006-07-10 21:08:03 +100016#include <sys/types.h>
17#include <sys/socket.h>
18
Damien Miller0dac6fb2010-11-20 15:19:38 +110019#include <netinet/in.h>
20#include <netinet/in_systm.h>
21#include <netinet/ip.h>
22
Darren Tucker5f96f3b2013-05-16 20:29:28 +100023#include <ctype.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100024#include <netdb.h>
Damien Miller565ca3f2006-08-19 00:23:15 +100025#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100026#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100027#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100028#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100029#include <signal.h>
Damien Millere6b3b612006-07-24 14:01:23 +100030#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000031#include <limits.h>
Damien Millerd7834352006-08-05 12:39:39 +100032#include <stdarg.h>
Darren Tuckere7140f22008-06-10 23:01:51 +100033#include <errno.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100034#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100035#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100036#endif
Damien Millerbe43ebf2006-07-24 13:51:51 +100037
Damien Millerb84886b2008-05-19 15:05:07 +100038#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100039#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100042#include "buffer.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100043#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044#include "servconf.h"
Damien Miller78928792000-04-12 20:17:38 +100045#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100048#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000049#include "kex.h"
50#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100051#include "match.h"
Damien Miller9b439df2006-07-24 14:04:00 +100052#include "channels.h"
Damien Miller565ca3f2006-08-19 00:23:15 +100053#include "groupaccess.h"
Darren Tuckerfbcf8272012-05-19 19:37:01 +100054#include "canohost.h"
55#include "packet.h"
Damien Millera6e3f012012-11-04 23:21:40 +110056#include "hostfile.h"
57#include "auth.h"
djm@openbsd.org57d378e2014-08-19 23:58:28 +000058#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000059#include "digest.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060
Damien Miller3dc71ad2009-01-28 16:31:22 +110061static void add_listen_addr(ServerOptions *, char *, int);
62static void add_one_listen_addr(ServerOptions *, char *, int);
Damien Miller34132e52000-01-14 15:45:46 +110063
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000064/* Use of privilege separation or not */
65extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100066extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000067
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068/* Initializes the server options to their default values. */
69
Damien Miller4af51302000-04-16 11:18:38 +100070void
Damien Miller95def091999-11-25 00:26:21 +110071initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072{
Damien Miller95def091999-11-25 00:26:21 +110073 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110074
75 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100076 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110077
78 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110079 options->num_ports = 0;
80 options->ports_from_cmdline = 0;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +000081 options->queued_listen_addrs = NULL;
82 options->num_queued_listens = 0;
Damien Miller34132e52000-01-14 15:45:46 +110083 options->listen_addrs = NULL;
Darren Tucker0f383232005-01-20 10:57:56 +110084 options->address_family = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110085 options->num_host_key_files = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110086 options->num_host_cert_files = 0;
Damien Miller85b45e02013-07-20 13:21:52 +100087 options->host_key_agent = NULL;
Damien Miller6f83b8e2000-05-02 09:23:45 +100088 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110089 options->server_key_bits = -1;
90 options->login_grace_time = -1;
91 options->key_regeneration_time = -1;
Ben Lindstromd8a90212001-02-15 03:08:27 +000092 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110093 options->ignore_rhosts = -1;
94 options->ignore_user_known_hosts = -1;
95 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +000096 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +110097 options->x11_forwarding = -1;
98 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +110099 options->x11_use_localhost = -1;
Damien Miller5ff30c62013-10-30 22:21:50 +1100100 options->permit_tty = -1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000101 options->permit_user_rc = -1;
Damien Millerd3a18572000-06-07 19:55:44 +1000102 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100103 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +1100104 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +1100105 options->log_facility = SYSLOG_FACILITY_NOT_SET;
106 options->log_level = SYSLOG_LEVEL_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +1100107 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000108 options->hostbased_authentication = -1;
109 options->hostbased_uses_name_from_packet_only = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000110 options->hostbased_key_types = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100111 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100112 options->pubkey_authentication = -1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000113 options->pubkey_key_types = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100114 options->kerberos_authentication = -1;
115 options->kerberos_or_local_passwd = -1;
116 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100117 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000118 options->gss_authentication=-1;
119 options->gss_cleanup_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100120 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100121 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000122 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100123 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000124 options->permit_user_env = -1;
Damien Miller95def091999-11-25 00:26:21 +1100125 options->use_login = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000126 options->compression = -1;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000127 options->rekey_limit = -1;
128 options->rekey_interval = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100129 options->allow_tcp_forwarding = -1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000130 options->allow_streamlocal_forwarding = -1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000131 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100132 options->num_allow_users = 0;
133 options->num_deny_users = 0;
134 options->num_allow_groups = 0;
135 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000136 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000137 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000138 options->kex_algorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000139 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller7acefbb2014-07-18 14:11:24 +1000140 options->fwd_opts.gateway_ports = -1;
141 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
142 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000143 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000144 options->max_startups_begin = -1;
145 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000146 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000147 options->max_authtries = -1;
Damien Miller7207f642008-05-19 15:34:50 +1000148 options->max_sessions = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000149 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000150 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000151 options->client_alive_interval = -1;
152 options->client_alive_count_max = -1;
Damien Millerd8478b62011-05-29 21:39:36 +1000153 options->num_authkeys_files = 0;
Darren Tucker46bc0752004-05-02 22:11:30 +1000154 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100155 options->permit_tun = -1;
Damien Millera765cf42006-07-24 14:08:13 +1000156 options->num_permitted_opens = -1;
Damien Millere2754432006-07-24 14:06:47 +1000157 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100158 options->chroot_directory = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +1100159 options->authorized_keys_command = NULL;
160 options->authorized_keys_command_user = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100161 options->revoked_keys_file = NULL;
162 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000163 options->authorized_principals_file = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100164 options->ip_qos_interactive = -1;
165 options->ip_qos_bulk = -1;
Damien Miller23528812012-04-22 11:24:43 +1000166 options->version_addendum = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000167 options->fingerprint_hash = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168}
169
djm@openbsd.org161cf412014-12-22 07:55:51 +0000170/* Returns 1 if a string option is unset or set to "none" or 0 otherwise. */
171static int
172option_clear_or_none(const char *o)
173{
174 return o == NULL || strcasecmp(o, "none") == 0;
175}
176
Damien Miller4af51302000-04-16 11:18:38 +1000177void
Damien Miller95def091999-11-25 00:26:21 +1100178fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179{
djm@openbsd.org161cf412014-12-22 07:55:51 +0000180 int i;
181
Damien Miller726273e2001-11-12 11:40:11 +1100182 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000183 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000184 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100185
186 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100187 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +1100188 options->protocol = SSH_PROTO_2;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100189 if (options->num_host_key_files == 0) {
190 /* fill default hostkeys for protocols */
191 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100192 options->host_key_files[options->num_host_key_files++] =
193 _PATH_HOST_KEY_FILE;
194 if (options->protocol & SSH_PROTO_2) {
195 options->host_key_files[options->num_host_key_files++] =
196 _PATH_HOST_RSA_KEY_FILE;
197 options->host_key_files[options->num_host_key_files++] =
198 _PATH_HOST_DSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100199#ifdef OPENSSL_HAS_ECC
Damien Millere13cadf2010-09-10 11:15:33 +1000200 options->host_key_files[options->num_host_key_files++] =
201 _PATH_HOST_ECDSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100202#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +1100203 options->host_key_files[options->num_host_key_files++] =
204 _PATH_HOST_ED25519_KEY_FILE;
Damien Miller7fc23732002-01-22 23:19:11 +1100205 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100206 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100207 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100208 if (options->num_ports == 0)
209 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000210 if (options->address_family == -1)
211 options->address_family = AF_UNSPEC;
Damien Miller34132e52000-01-14 15:45:46 +1100212 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000213 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000214 if (options->pid_file == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000215 options->pid_file = xstrdup(_PATH_SSH_DAEMON_PID_FILE);
Damien Miller95def091999-11-25 00:26:21 +1100216 if (options->server_key_bits == -1)
Darren Tucker7499b0c2008-07-02 22:35:43 +1000217 options->server_key_bits = 1024;
Damien Miller95def091999-11-25 00:26:21 +1100218 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000219 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100220 if (options->key_regeneration_time == -1)
221 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000222 if (options->permit_root_login == PERMIT_NOT_SET)
djm@openbsd.org88a7c592015-04-27 21:42:48 +0000223 options->permit_root_login = PERMIT_NO;
Damien Miller95def091999-11-25 00:26:21 +1100224 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100225 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100226 if (options->ignore_user_known_hosts == -1)
227 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100228 if (options->print_motd == -1)
229 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000230 if (options->print_lastlog == -1)
231 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100232 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100233 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100234 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100235 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100236 if (options->x11_use_localhost == -1)
237 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000238 if (options->xauth_location == NULL)
djm@openbsd.org161cf412014-12-22 07:55:51 +0000239 options->xauth_location = xstrdup(_PATH_XAUTH);
Damien Miller5ff30c62013-10-30 22:21:50 +1100240 if (options->permit_tty == -1)
241 options->permit_tty = 1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000242 if (options->permit_user_rc == -1)
243 options->permit_user_rc = 1;
Damien Miller95def091999-11-25 00:26:21 +1100244 if (options->strict_modes == -1)
245 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100246 if (options->tcp_keep_alive == -1)
247 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100248 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100249 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100250 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000251 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100252 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100253 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000254 if (options->hostbased_authentication == -1)
255 options->hostbased_authentication = 0;
256 if (options->hostbased_uses_name_from_packet_only == -1)
257 options->hostbased_uses_name_from_packet_only = 0;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000258 if (options->hostbased_key_types == NULL)
259 options->hostbased_key_types = xstrdup("*");
Damien Miller95def091999-11-25 00:26:21 +1100260 if (options->rsa_authentication == -1)
261 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100262 if (options->pubkey_authentication == -1)
263 options->pubkey_authentication = 1;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000264 if (options->pubkey_key_types == NULL)
265 options->pubkey_key_types = xstrdup("*");
Damien Miller95def091999-11-25 00:26:21 +1100266 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000267 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100268 if (options->kerberos_or_local_passwd == -1)
269 options->kerberos_or_local_passwd = 1;
270 if (options->kerberos_ticket_cleanup == -1)
271 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100272 if (options->kerberos_get_afs_token == -1)
273 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000274 if (options->gss_authentication == -1)
275 options->gss_authentication = 0;
276 if (options->gss_cleanup_creds == -1)
277 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100278 if (options->password_authentication == -1)
279 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100280 if (options->kbd_interactive_authentication == -1)
281 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000282 if (options->challenge_response_authentication == -1)
283 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100284 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100285 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000286 if (options->permit_user_env == -1)
287 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100288 if (options->use_login == -1)
289 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000290 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000291 options->compression = COMP_DELAYED;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000292 if (options->rekey_limit == -1)
293 options->rekey_limit = 0;
294 if (options->rekey_interval == -1)
295 options->rekey_interval = 0;
Damien Miller50a41ed2000-10-16 12:14:42 +1100296 if (options->allow_tcp_forwarding == -1)
Damien Milleraa5b3f82012-12-03 09:50:54 +1100297 options->allow_tcp_forwarding = FORWARD_ALLOW;
Damien Miller7acefbb2014-07-18 14:11:24 +1000298 if (options->allow_streamlocal_forwarding == -1)
299 options->allow_streamlocal_forwarding = FORWARD_ALLOW;
Damien Miller4f755cd2008-05-19 14:57:41 +1000300 if (options->allow_agent_forwarding == -1)
301 options->allow_agent_forwarding = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000302 if (options->fwd_opts.gateway_ports == -1)
303 options->fwd_opts.gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000304 if (options->max_startups == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100305 options->max_startups = 100;
Damien Miller942da032000-08-18 13:59:06 +1000306 if (options->max_startups_rate == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100307 options->max_startups_rate = 30; /* 30% */
Damien Miller942da032000-08-18 13:59:06 +1000308 if (options->max_startups_begin == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100309 options->max_startups_begin = 10;
Darren Tucker89413db2004-05-24 10:36:23 +1000310 if (options->max_authtries == -1)
311 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000312 if (options->max_sessions == -1)
313 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000314 if (options->use_dns == -1)
deraadt@openbsd.org3cd51032015-02-02 01:57:44 +0000315 options->use_dns = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000316 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100317 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000318 if (options->client_alive_count_max == -1)
319 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000320 if (options->num_authkeys_files == 0) {
321 options->authorized_keys_files[options->num_authkeys_files++] =
322 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS);
323 options->authorized_keys_files[options->num_authkeys_files++] =
324 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS2);
325 }
Damien Millerd27b9472005-12-13 19:29:02 +1100326 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100327 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100328 if (options->ip_qos_interactive == -1)
329 options->ip_qos_interactive = IPTOS_LOWDELAY;
330 if (options->ip_qos_bulk == -1)
331 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller23528812012-04-22 11:24:43 +1000332 if (options->version_addendum == NULL)
333 options->version_addendum = xstrdup("");
Damien Miller7acefbb2014-07-18 14:11:24 +1000334 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
335 options->fwd_opts.streamlocal_bind_mask = 0177;
336 if (options->fwd_opts.streamlocal_bind_unlink == -1)
337 options->fwd_opts.streamlocal_bind_unlink = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000338 if (options->fingerprint_hash == -1)
339 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
Ben Lindstromfb62a692002-06-06 19:47:11 +0000340 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000341 if (use_privsep == -1)
Damien Miller5a5c2b92012-07-31 12:21:34 +1000342 use_privsep = PRIVSEP_NOSANDBOX;
Damien Miller4903eb42002-06-21 16:20:44 +1000343
djm@openbsd.org161cf412014-12-22 07:55:51 +0000344#define CLEAR_ON_NONE(v) \
345 do { \
346 if (option_clear_or_none(v)) { \
347 free(v); \
348 v = NULL; \
349 } \
350 } while(0)
351 CLEAR_ON_NONE(options->pid_file);
352 CLEAR_ON_NONE(options->xauth_location);
353 CLEAR_ON_NONE(options->banner);
354 CLEAR_ON_NONE(options->trusted_user_ca_keys);
355 CLEAR_ON_NONE(options->revoked_keys_file);
356 for (i = 0; i < options->num_host_key_files; i++)
357 CLEAR_ON_NONE(options->host_key_files[i]);
358 for (i = 0; i < options->num_host_cert_files; i++)
359 CLEAR_ON_NONE(options->host_cert_files[i]);
360#undef CLEAR_ON_NONE
361
Tim Rice40017b02002-07-14 13:36:49 -0700362#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000363 if (use_privsep && options->compression == 1) {
364 error("This platform does not support both privilege "
365 "separation and compression");
366 error("Compression disabled");
367 options->compression = 0;
368 }
369#endif
370
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371}
372
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100374typedef enum {
375 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100376 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000377 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100378 /* Standard Options */
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000379 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime,
380 sKeyRegenerationTime, sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000381 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100382 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100383 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000384 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100385 sPasswordAuthentication, sKbdInteractiveAuthentication,
386 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000387 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100388 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller5ff30c62013-10-30 22:21:50 +1100389 sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000390 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000391 sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000392 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000393 sGatewayPorts, sPubkeyAuthentication, sPubkeyAcceptedKeyTypes,
394 sXAuthLocation, sSubsystem, sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000395 sBanner, sUseDNS, sHostbasedAuthentication,
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000396 sHostbasedUsesNameFromPacketOnly, sHostbasedAcceptedKeyTypes,
397 sClientAliveInterval, sClientAliveCountMax, sAuthorizedKeysFile,
Damien Millerd27b9472005-12-13 19:29:02 +1100398 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100399 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100400 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller7cc194f2014-02-04 11:12:56 +1100401 sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000402 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Miller23528812012-04-22 11:24:43 +1000403 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Miller09d3e122012-10-31 08:58:58 +1100404 sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
Damien Miller72e6b5c2014-07-04 09:00:04 +1000405 sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
Damien Miller7acefbb2014-07-18 14:11:24 +1000406 sStreamLocalBindMask, sStreamLocalBindUnlink,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000407 sAllowStreamLocalForwarding, sFingerprintHash,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000408 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409} ServerOpCodes;
410
Darren Tucker45150472006-07-12 22:34:17 +1000411#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
412#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
413#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
414
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100416static struct {
417 const char *name;
418 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000419 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100420} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100421 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000422#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000423 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000424#else
Darren Tucker45150472006-07-12 22:34:17 +1000425 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000426#endif
Darren Tucker45150472006-07-12 22:34:17 +1000427 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100428 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000429 { "port", sPort, SSHCFG_GLOBAL },
430 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
431 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
Damien Miller85b45e02013-07-20 13:21:52 +1000432 { "hostkeyagent", sHostKeyAgent, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000433 { "pidfile", sPidFile, SSHCFG_GLOBAL },
434 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
435 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
436 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100437 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000438 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
439 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
440 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100441 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
442 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000443 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000444 { "hostbasedacceptedkeytypes", sHostbasedAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100445 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
446 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000447 { "pubkeyacceptedkeytypes", sPubkeyAcceptedKeyTypes, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000448 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000449#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100450 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000451 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
452 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100453#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000454 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000455#else
Darren Tucker45150472006-07-12 22:34:17 +1000456 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100457#endif
458#else
Darren Tucker1629c072007-02-19 22:25:37 +1100459 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000460 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
461 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
462 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000463#endif
Darren Tucker45150472006-07-12 22:34:17 +1000464 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
465 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000466#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100467 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000468 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000469#else
Darren Tucker1629c072007-02-19 22:25:37 +1100470 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000471 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000472#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100473 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
474 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100475 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000476 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
477 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
478 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
479 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
480 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
481 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
482 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
483 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000484 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
485 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
486 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000487 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
488 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100489 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000490 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
491 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
492 { "compression", sCompression, SSHCFG_GLOBAL },
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000493 { "rekeylimit", sRekeyLimit, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000494 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
495 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
496 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000497 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Damien Millerc24da772012-06-20 21:53:58 +1000498 { "allowusers", sAllowUsers, SSHCFG_ALL },
499 { "denyusers", sDenyUsers, SSHCFG_ALL },
500 { "allowgroups", sAllowGroups, SSHCFG_ALL },
501 { "denygroups", sDenyGroups, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000502 { "ciphers", sCiphers, SSHCFG_GLOBAL },
503 { "macs", sMacs, SSHCFG_GLOBAL },
504 { "protocol", sProtocol, SSHCFG_GLOBAL },
505 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
506 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
507 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000508 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000509 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100510 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000511 { "usedns", sUseDNS, SSHCFG_GLOBAL },
512 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
513 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
514 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
515 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000516 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000517 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000518 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Damien Millerc24da772012-06-20 21:53:58 +1000519 { "acceptenv", sAcceptEnv, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000520 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Damien Miller5ff30c62013-10-30 22:21:50 +1100521 { "permittty", sPermitTTY, SSHCFG_ALL },
Damien Miller72e6b5c2014-07-04 09:00:04 +1000522 { "permituserrc", sPermitUserRC, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000523 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000524 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000525 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100526 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100527 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100528 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
529 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000530 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000531 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100532 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller09d3e122012-10-31 08:58:58 +1100533 { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
534 { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000535 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Damien Millera6e3f012012-11-04 23:21:40 +1100536 { "authenticationmethods", sAuthenticationMethods, SSHCFG_ALL },
Damien Miller7acefbb2014-07-18 14:11:24 +1000537 { "streamlocalbindmask", sStreamLocalBindMask, SSHCFG_ALL },
538 { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
539 { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000540 { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000541 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000542};
543
Darren Tuckere7140f22008-06-10 23:01:51 +1000544static struct {
545 int val;
546 char *text;
547} tunmode_desc[] = {
548 { SSH_TUNMODE_NO, "no" },
549 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
550 { SSH_TUNMODE_ETHERNET, "ethernet" },
551 { SSH_TUNMODE_YES, "yes" },
552 { -1, NULL }
553};
554
Damien Miller5428f641999-11-25 11:54:57 +1100555/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000556 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100557 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558
Damien Miller4af51302000-04-16 11:18:38 +1000559static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100560parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000561 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000563 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564
Damien Miller95def091999-11-25 00:26:21 +1100565 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000566 if (strcasecmp(cp, keywords[i].name) == 0) {
567 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100568 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000569 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000571 error("%s: line %d: Bad configuration option: %s",
572 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100573 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574}
575
Darren Tucker88b6fb22010-01-13 22:44:29 +1100576char *
577derelativise_path(const char *path)
578{
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000579 char *expanded, *ret, cwd[PATH_MAX];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100580
djm@openbsd.org161cf412014-12-22 07:55:51 +0000581 if (strcasecmp(path, "none") == 0)
582 return xstrdup("none");
Darren Tucker88b6fb22010-01-13 22:44:29 +1100583 expanded = tilde_expand_filename(path, getuid());
584 if (*expanded == '/')
585 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100586 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100587 fatal("%s: getcwd: %s", __func__, strerror(errno));
588 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tuckera627d422013-06-02 07:31:17 +1000589 free(expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100590 return ret;
591}
592
Ben Lindstrombba81212001-06-25 05:01:22 +0000593static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100594add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100595{
Damien Millereccb9de2005-06-17 12:59:34 +1000596 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100597
Ben Lindstrom19066a12001-04-12 23:39:26 +0000598 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000599 for (i = 0; i < options->num_ports; i++)
600 add_one_listen_addr(options, addr, options->ports[i]);
601 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000602 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000603}
604
Ben Lindstrombba81212001-06-25 05:01:22 +0000605static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100606add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000607{
608 struct addrinfo hints, *ai, *aitop;
609 char strport[NI_MAXSERV];
610 int gaierr;
611
612 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100613 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000614 hints.ai_socktype = SOCK_STREAM;
615 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100616 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000617 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
618 fatal("bad addr or host: %s (%s)",
619 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100620 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000621 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
622 ;
623 ai->ai_next = options->listen_addrs;
624 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100625}
626
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +0000627/*
628 * Queue a ListenAddress to be processed once we have all of the Ports
629 * and AddressFamily options.
630 */
631static void
632queue_listen_addr(ServerOptions *options, char *addr, int port)
633{
634 options->queued_listen_addrs = xreallocarray(
635 options->queued_listen_addrs, options->num_queued_listens + 1,
636 sizeof(addr));
637 options->queued_listen_ports = xreallocarray(
638 options->queued_listen_ports, options->num_queued_listens + 1,
639 sizeof(port));
640 options->queued_listen_addrs[options->num_queued_listens] =
641 xstrdup(addr);
642 options->queued_listen_ports[options->num_queued_listens] = port;
643 options->num_queued_listens++;
644}
645
646/*
647 * Process queued (text) ListenAddress entries.
648 */
649static void
650process_queued_listen_addrs(ServerOptions *options)
651{
652 u_int i;
653
654 if (options->num_ports == 0)
655 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
656 if (options->address_family == -1)
657 options->address_family = AF_UNSPEC;
658
659 for (i = 0; i < options->num_queued_listens; i++) {
660 add_listen_addr(options, options->queued_listen_addrs[i],
661 options->queued_listen_ports[i]);
662 free(options->queued_listen_addrs[i]);
663 options->queued_listen_addrs[i] = NULL;
664 }
665 free(options->queued_listen_addrs);
666 options->queued_listen_addrs = NULL;
667 free(options->queued_listen_ports);
668 options->queued_listen_ports = NULL;
669 options->num_queued_listens = 0;
670}
671
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000672struct connection_info *
673get_connection_info(int populate, int use_dns)
674{
675 static struct connection_info ci;
676
677 if (!populate)
678 return &ci;
679 ci.host = get_canonical_hostname(use_dns);
680 ci.address = get_remote_ipaddr();
681 ci.laddress = get_local_ipaddr(packet_get_connection_in());
682 ci.lport = get_local_port();
683 return &ci;
684}
685
Darren Tucker45150472006-07-12 22:34:17 +1000686/*
687 * The strategy for the Match blocks is that the config file is parsed twice.
688 *
689 * The first time is at startup. activep is initialized to 1 and the
690 * directives in the global context are processed and acted on. Hitting a
691 * Match directive unsets activep and the directives inside the block are
692 * checked for syntax only.
693 *
694 * The second time is after a connection has been established but before
695 * authentication. activep is initialized to 2 and global config directives
696 * are ignored since they have already been processed. If the criteria in a
697 * Match block is met, activep is set and the subsequent directives
698 * processed and actioned until EOF or another Match block unsets it. Any
699 * options set are copied into the main server config.
700 *
701 * Potential additions/improvements:
702 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
703 *
704 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
705 * Match Address 192.168.0.*
706 * Tag trusted
707 * Match Group wheel
708 * Tag trusted
709 * Match Tag trusted
710 * AllowTcpForwarding yes
711 * GatewayPorts clientspecified
712 * [...]
713 *
714 * - Add a PermittedChannelRequests directive
715 * Match Group shell
716 * PermittedChannelRequests session,forwarded-tcpip
717 */
718
719static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000720match_cfg_line_group(const char *grps, int line, const char *user)
721{
722 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000723 struct passwd *pw;
724
Damien Miller565ca3f2006-08-19 00:23:15 +1000725 if (user == NULL)
726 goto out;
727
728 if ((pw = getpwnam(user)) == NULL) {
729 debug("Can't match group at line %d because user %.100s does "
730 "not exist", line, user);
731 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
732 debug("Can't Match group because user %.100s not in any group "
733 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000734 } else if (ga_match_pattern_list(grps) != 1) {
735 debug("user %.100s does not match group list %.100s at line %d",
736 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000737 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000738 debug("user %.100s matched group list %.100s at line %d", user,
739 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000740 result = 1;
741 }
742out:
743 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000744 return result;
745}
746
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000747/*
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000748 * All of the attributes on a single Match line are ANDed together, so we need
Damien Miller03bf2e62013-10-24 21:01:26 +1100749 * to check every attribute and set the result to zero if any attribute does
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000750 * not match.
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000751 */
Damien Miller565ca3f2006-08-19 00:23:15 +1000752static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000753match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +1000754{
Damien Millercf31f382013-10-24 21:02:56 +1100755 int result = 1, attributes = 0, port;
Darren Tucker45150472006-07-12 22:34:17 +1000756 char *arg, *attrib, *cp = *condition;
757 size_t len;
758
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000759 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000760 debug3("checking syntax for 'Match %s'", cp);
761 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000762 debug3("checking match for '%s' user %s host %s addr %s "
763 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
764 ci->host ? ci->host : "(null)",
765 ci->address ? ci->address : "(null)",
766 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +1000767
768 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100769 attributes++;
770 if (strcasecmp(attrib, "all") == 0) {
771 if (attributes != 1 ||
772 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
773 error("'all' cannot be combined with other "
774 "Match attributes");
775 return -1;
776 }
777 *condition = cp;
778 return 1;
779 }
Darren Tucker45150472006-07-12 22:34:17 +1000780 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
781 error("Missing Match criteria for %s", attrib);
782 return -1;
783 }
784 len = strlen(arg);
785 if (strcasecmp(attrib, "user") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000786 if (ci == NULL || ci->user == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000787 result = 0;
788 continue;
789 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000790 if (match_pattern_list(ci->user, arg, len, 0) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000791 result = 0;
792 else
793 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000794 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000795 } else if (strcasecmp(attrib, "group") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000796 if (ci == NULL || ci->user == NULL) {
797 result = 0;
798 continue;
799 }
800 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +1000801 case -1:
802 return -1;
803 case 0:
804 result = 0;
805 }
Darren Tucker45150472006-07-12 22:34:17 +1000806 } else if (strcasecmp(attrib, "host") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000807 if (ci == NULL || ci->host == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000808 result = 0;
809 continue;
810 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000811 if (match_hostname(ci->host, arg, len) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000812 result = 0;
813 else
814 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000815 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +1000816 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000817 if (ci == NULL || ci->address == NULL) {
818 result = 0;
819 continue;
820 }
821 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000822 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000823 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000824 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000825 break;
826 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000827 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000828 result = 0;
829 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000830 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000831 return -1;
832 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000833 } else if (strcasecmp(attrib, "localaddress") == 0){
834 if (ci == NULL || ci->laddress == NULL) {
835 result = 0;
836 continue;
837 }
838 switch (addr_match_list(ci->laddress, arg)) {
839 case 1:
840 debug("connection from %.100s matched "
841 "'LocalAddress %.100s' at line %d",
842 ci->laddress, arg, line);
843 break;
844 case 0:
845 case -1:
846 result = 0;
847 break;
848 case -2:
849 return -1;
850 }
851 } else if (strcasecmp(attrib, "localport") == 0) {
852 if ((port = a2port(arg)) == -1) {
853 error("Invalid LocalPort '%s' on Match line",
854 arg);
855 return -1;
856 }
857 if (ci == NULL || ci->lport == 0) {
858 result = 0;
859 continue;
860 }
861 /* TODO support port lists */
862 if (port == ci->lport)
863 debug("connection from %.100s matched "
864 "'LocalPort %d' at line %d",
865 ci->laddress, port, line);
866 else
867 result = 0;
Darren Tucker45150472006-07-12 22:34:17 +1000868 } else {
869 error("Unsupported Match attribute %s", attrib);
870 return -1;
871 }
872 }
Damien Millercf31f382013-10-24 21:02:56 +1100873 if (attributes == 0) {
874 error("One or more attributes required for Match");
875 return -1;
876 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000877 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000878 debug3("match %sfound", result ? "" : "not ");
879 *condition = cp;
880 return result;
881}
882
Damien Millere2754432006-07-24 14:06:47 +1000883#define WHITESPACE " \t\r\n"
884
Damien Miller33322122011-06-20 14:43:11 +1000885/* Multistate option parsing */
886struct multistate {
887 char *key;
888 int value;
889};
890static const struct multistate multistate_addressfamily[] = {
891 { "inet", AF_INET },
892 { "inet6", AF_INET6 },
893 { "any", AF_UNSPEC },
894 { NULL, -1 }
895};
896static const struct multistate multistate_permitrootlogin[] = {
897 { "without-password", PERMIT_NO_PASSWD },
898 { "forced-commands-only", PERMIT_FORCED_ONLY },
899 { "yes", PERMIT_YES },
900 { "no", PERMIT_NO },
901 { NULL, -1 }
902};
903static const struct multistate multistate_compression[] = {
904 { "delayed", COMP_DELAYED },
905 { "yes", COMP_ZLIB },
906 { "no", COMP_NONE },
907 { NULL, -1 }
908};
909static const struct multistate multistate_gatewayports[] = {
910 { "clientspecified", 2 },
911 { "yes", 1 },
912 { "no", 0 },
913 { NULL, -1 }
914};
Damien Miller69ff1df2011-06-23 08:30:03 +1000915static const struct multistate multistate_privsep[] = {
Damien Miller5a5c2b92012-07-31 12:21:34 +1000916 { "yes", PRIVSEP_NOSANDBOX },
917 { "sandbox", PRIVSEP_ON },
918 { "nosandbox", PRIVSEP_NOSANDBOX },
Damien Miller69ff1df2011-06-23 08:30:03 +1000919 { "no", PRIVSEP_OFF },
920 { NULL, -1 }
921};
Damien Milleraa5b3f82012-12-03 09:50:54 +1100922static const struct multistate multistate_tcpfwd[] = {
923 { "yes", FORWARD_ALLOW },
924 { "all", FORWARD_ALLOW },
925 { "no", FORWARD_DENY },
926 { "remote", FORWARD_REMOTE },
927 { "local", FORWARD_LOCAL },
928 { NULL, -1 }
929};
Damien Miller33322122011-06-20 14:43:11 +1000930
Ben Lindstromade03f62001-12-06 18:22:17 +0000931int
932process_server_config_line(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000933 const char *filename, int linenum, int *activep,
934 struct connection_info *connectinfo)
Ben Lindstromade03f62001-12-06 18:22:17 +0000935{
Darren Tucker09c0f032013-05-16 20:48:57 +1000936 char *cp, **charptr, *arg, *p;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000937 int cmdline = 0, *intptr, value, value2, n, port;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100938 SyslogFacility *log_facility_ptr;
939 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000940 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000941 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000942 size_t len;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000943 long long val64;
Damien Miller33322122011-06-20 14:43:11 +1000944 const struct multistate *multistate_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000945
946 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100947 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100948 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000949 /* Ignore leading whitespace */
950 if (*arg == '\0')
951 arg = strdelim(&cp);
952 if (!arg || !*arg || *arg == '#')
953 return 0;
954 intptr = NULL;
955 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000956 opcode = parse_token(arg, filename, linenum, &flags);
957
958 if (activep == NULL) { /* We are processing a command line directive */
959 cmdline = 1;
960 activep = &cmdline;
961 }
962 if (*activep && opcode != sMatch)
963 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
964 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000965 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000966 fatal("%s line %d: Directive '%s' is not allowed "
967 "within a Match block", filename, linenum, arg);
968 } else { /* this is a directive we have already processed */
969 while (arg)
970 arg = strdelim(&cp);
971 return 0;
972 }
973 }
974
Ben Lindstromade03f62001-12-06 18:22:17 +0000975 switch (opcode) {
976 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000977 case sUsePAM:
978 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000979 goto parse_flag;
980
981 /* Standard Options */
982 case sBadOption:
983 return -1;
984 case sPort:
985 /* ignore ports from configfile if cmdline specifies ports */
986 if (options->ports_from_cmdline)
987 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000988 if (options->num_ports >= MAX_PORTS)
989 fatal("%s line %d: too many ports.",
990 filename, linenum);
991 arg = strdelim(&cp);
992 if (!arg || *arg == '\0')
993 fatal("%s line %d: missing port number.",
994 filename, linenum);
995 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100996 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000997 fatal("%s line %d: Badly formatted port number.",
998 filename, linenum);
999 break;
1000
1001 case sServerKeyBits:
1002 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +10001003 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +00001004 arg = strdelim(&cp);
1005 if (!arg || *arg == '\0')
1006 fatal("%s line %d: missing integer value.",
1007 filename, linenum);
1008 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +10001009 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001010 *intptr = value;
1011 break;
1012
1013 case sLoginGraceTime:
1014 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +10001015 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +00001016 arg = strdelim(&cp);
1017 if (!arg || *arg == '\0')
1018 fatal("%s line %d: missing time value.",
1019 filename, linenum);
1020 if ((value = convtime(arg)) == -1)
1021 fatal("%s line %d: invalid time value.",
1022 filename, linenum);
1023 if (*intptr == -1)
1024 *intptr = value;
1025 break;
1026
1027 case sKeyRegenerationTime:
1028 intptr = &options->key_regeneration_time;
1029 goto parse_time;
1030
1031 case sListenAddress:
1032 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001033 if (arg == NULL || *arg == '\0')
1034 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +00001035 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +10001036 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
1037 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
1038 && strchr(p+1, ':') != NULL) {
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00001039 queue_listen_addr(options, arg, 0);
Damien Miller203c7052005-08-12 22:11:37 +10001040 break;
1041 }
Damien Millerf91ee4c2005-03-01 21:24:33 +11001042 p = hpdelim(&arg);
1043 if (p == NULL)
1044 fatal("%s line %d: bad address:port usage",
1045 filename, linenum);
1046 p = cleanhostname(p);
1047 if (arg == NULL)
1048 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +11001049 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001050 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001051
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00001052 queue_listen_addr(options, p, port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11001053
Ben Lindstromade03f62001-12-06 18:22:17 +00001054 break;
1055
Darren Tucker0f383232005-01-20 10:57:56 +11001056 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +10001057 intptr = &options->address_family;
1058 multistate_ptr = multistate_addressfamily;
Damien Miller33322122011-06-20 14:43:11 +10001059 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +11001060 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +10001061 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +10001062 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +10001063 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +10001064 value = -1;
1065 for (i = 0; multistate_ptr[i].key != NULL; i++) {
1066 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
1067 value = multistate_ptr[i].value;
1068 break;
1069 }
1070 }
1071 if (value == -1)
1072 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +11001073 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +10001074 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +11001075 *intptr = value;
1076 break;
1077
Ben Lindstromade03f62001-12-06 18:22:17 +00001078 case sHostKeyFile:
1079 intptr = &options->num_host_key_files;
1080 if (*intptr >= MAX_HOSTKEYS)
1081 fatal("%s line %d: too many host keys specified (max %d).",
1082 filename, linenum, MAX_HOSTKEYS);
1083 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +10001084 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +00001085 arg = strdelim(&cp);
1086 if (!arg || *arg == '\0')
1087 fatal("%s line %d: missing file name.",
1088 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001089 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +11001090 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001091 /* increase optional counter */
1092 if (intptr != NULL)
1093 *intptr = *intptr + 1;
1094 }
1095 break;
1096
Damien Miller85b45e02013-07-20 13:21:52 +10001097 case sHostKeyAgent:
1098 charptr = &options->host_key_agent;
1099 arg = strdelim(&cp);
1100 if (!arg || *arg == '\0')
1101 fatal("%s line %d: missing socket name.",
1102 filename, linenum);
1103 if (*activep && *charptr == NULL)
1104 *charptr = !strcmp(arg, SSH_AUTHSOCKET_ENV_NAME) ?
1105 xstrdup(arg) : derelativise_path(arg);
1106 break;
1107
Damien Miller0a80ca12010-02-27 07:55:05 +11001108 case sHostCertificate:
1109 intptr = &options->num_host_cert_files;
1110 if (*intptr >= MAX_HOSTKEYS)
1111 fatal("%s line %d: too many host certificates "
1112 "specified (max %d).", filename, linenum,
1113 MAX_HOSTCERTS);
1114 charptr = &options->host_cert_files[*intptr];
1115 goto parse_filename;
1116 break;
1117
Ben Lindstromade03f62001-12-06 18:22:17 +00001118 case sPidFile:
1119 charptr = &options->pid_file;
1120 goto parse_filename;
1121
1122 case sPermitRootLogin:
1123 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +10001124 multistate_ptr = multistate_permitrootlogin;
1125 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001126
1127 case sIgnoreRhosts:
1128 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +10001129 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +00001130 arg = strdelim(&cp);
1131 if (!arg || *arg == '\0')
1132 fatal("%s line %d: missing yes/no argument.",
1133 filename, linenum);
1134 value = 0; /* silence compiler */
1135 if (strcmp(arg, "yes") == 0)
1136 value = 1;
1137 else if (strcmp(arg, "no") == 0)
1138 value = 0;
1139 else
1140 fatal("%s line %d: Bad yes/no argument: %s",
1141 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +10001142 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001143 *intptr = value;
1144 break;
1145
1146 case sIgnoreUserKnownHosts:
1147 intptr = &options->ignore_user_known_hosts;
1148 goto parse_flag;
1149
Ben Lindstromade03f62001-12-06 18:22:17 +00001150 case sRhostsRSAAuthentication:
1151 intptr = &options->rhosts_rsa_authentication;
1152 goto parse_flag;
1153
1154 case sHostbasedAuthentication:
1155 intptr = &options->hostbased_authentication;
1156 goto parse_flag;
1157
1158 case sHostbasedUsesNameFromPacketOnly:
1159 intptr = &options->hostbased_uses_name_from_packet_only;
1160 goto parse_flag;
1161
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001162 case sHostbasedAcceptedKeyTypes:
1163 charptr = &options->hostbased_key_types;
1164 parse_keytypes:
1165 arg = strdelim(&cp);
1166 if (!arg || *arg == '\0')
1167 fatal("%s line %d: Missing argument.",
1168 filename, linenum);
1169 if (!sshkey_names_valid2(arg, 1))
1170 fatal("%s line %d: Bad key types '%s'.",
1171 filename, linenum, arg ? arg : "<NONE>");
1172 if (*activep && *charptr == NULL)
1173 *charptr = xstrdup(arg);
1174 break;
1175
Ben Lindstromade03f62001-12-06 18:22:17 +00001176 case sRSAAuthentication:
1177 intptr = &options->rsa_authentication;
1178 goto parse_flag;
1179
1180 case sPubkeyAuthentication:
1181 intptr = &options->pubkey_authentication;
1182 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001183
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001184 case sPubkeyAcceptedKeyTypes:
1185 charptr = &options->pubkey_key_types;
1186 goto parse_keytypes;
1187
Ben Lindstromade03f62001-12-06 18:22:17 +00001188 case sKerberosAuthentication:
1189 intptr = &options->kerberos_authentication;
1190 goto parse_flag;
1191
1192 case sKerberosOrLocalPasswd:
1193 intptr = &options->kerberos_or_local_passwd;
1194 goto parse_flag;
1195
1196 case sKerberosTicketCleanup:
1197 intptr = &options->kerberos_ticket_cleanup;
1198 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001199
Darren Tucker22ef5082003-12-31 11:37:34 +11001200 case sKerberosGetAFSToken:
1201 intptr = &options->kerberos_get_afs_token;
1202 goto parse_flag;
1203
Darren Tucker0efd1552003-08-26 11:49:55 +10001204 case sGssAuthentication:
1205 intptr = &options->gss_authentication;
1206 goto parse_flag;
1207
1208 case sGssCleanupCreds:
1209 intptr = &options->gss_cleanup_creds;
1210 goto parse_flag;
1211
Ben Lindstromade03f62001-12-06 18:22:17 +00001212 case sPasswordAuthentication:
1213 intptr = &options->password_authentication;
1214 goto parse_flag;
1215
1216 case sKbdInteractiveAuthentication:
1217 intptr = &options->kbd_interactive_authentication;
1218 goto parse_flag;
1219
1220 case sChallengeResponseAuthentication:
1221 intptr = &options->challenge_response_authentication;
1222 goto parse_flag;
1223
1224 case sPrintMotd:
1225 intptr = &options->print_motd;
1226 goto parse_flag;
1227
1228 case sPrintLastLog:
1229 intptr = &options->print_lastlog;
1230 goto parse_flag;
1231
1232 case sX11Forwarding:
1233 intptr = &options->x11_forwarding;
1234 goto parse_flag;
1235
1236 case sX11DisplayOffset:
1237 intptr = &options->x11_display_offset;
1238 goto parse_int;
1239
Damien Miller95c249f2002-02-05 12:11:34 +11001240 case sX11UseLocalhost:
1241 intptr = &options->x11_use_localhost;
1242 goto parse_flag;
1243
Ben Lindstromade03f62001-12-06 18:22:17 +00001244 case sXAuthLocation:
1245 charptr = &options->xauth_location;
1246 goto parse_filename;
1247
Damien Miller5ff30c62013-10-30 22:21:50 +11001248 case sPermitTTY:
1249 intptr = &options->permit_tty;
1250 goto parse_flag;
1251
Damien Miller72e6b5c2014-07-04 09:00:04 +10001252 case sPermitUserRC:
1253 intptr = &options->permit_user_rc;
1254 goto parse_flag;
1255
Ben Lindstromade03f62001-12-06 18:22:17 +00001256 case sStrictModes:
1257 intptr = &options->strict_modes;
1258 goto parse_flag;
1259
Damien Miller12c150e2003-12-17 16:31:10 +11001260 case sTCPKeepAlive:
1261 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001262 goto parse_flag;
1263
1264 case sEmptyPasswd:
1265 intptr = &options->permit_empty_passwd;
1266 goto parse_flag;
1267
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001268 case sPermitUserEnvironment:
1269 intptr = &options->permit_user_env;
1270 goto parse_flag;
1271
Ben Lindstromade03f62001-12-06 18:22:17 +00001272 case sUseLogin:
1273 intptr = &options->use_login;
1274 goto parse_flag;
1275
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001276 case sCompression:
1277 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001278 multistate_ptr = multistate_compression;
1279 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001280
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001281 case sRekeyLimit:
1282 arg = strdelim(&cp);
1283 if (!arg || *arg == '\0')
1284 fatal("%.200s line %d: Missing argument.", filename,
1285 linenum);
1286 if (strcmp(arg, "default") == 0) {
1287 val64 = 0;
1288 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001289 if (scan_scaled(arg, &val64) == -1)
1290 fatal("%.200s line %d: Bad number '%s': %s",
1291 filename, linenum, arg, strerror(errno));
1292 /* check for too-large or too-small limits */
1293 if (val64 > UINT_MAX)
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001294 fatal("%.200s line %d: RekeyLimit too large",
1295 filename, linenum);
1296 if (val64 != 0 && val64 < 16)
1297 fatal("%.200s line %d: RekeyLimit too small",
1298 filename, linenum);
1299 }
1300 if (*activep && options->rekey_limit == -1)
1301 options->rekey_limit = (u_int32_t)val64;
1302 if (cp != NULL) { /* optional rekey interval present */
1303 if (strcmp(cp, "none") == 0) {
1304 (void)strdelim(&cp); /* discard */
1305 break;
1306 }
1307 intptr = &options->rekey_interval;
1308 goto parse_time;
1309 }
1310 break;
1311
Ben Lindstromade03f62001-12-06 18:22:17 +00001312 case sGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +10001313 intptr = &options->fwd_opts.gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001314 multistate_ptr = multistate_gatewayports;
1315 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001316
Damien Miller3a961dc2003-06-03 10:25:48 +10001317 case sUseDNS:
1318 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001319 goto parse_flag;
1320
1321 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001322 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001323 arg = strdelim(&cp);
1324 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001325 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001326 fatal("%.200s line %d: unsupported log facility '%s'",
1327 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001328 if (*log_facility_ptr == -1)
1329 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001330 break;
1331
1332 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001333 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001334 arg = strdelim(&cp);
1335 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001336 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001337 fatal("%.200s line %d: unsupported log level '%s'",
1338 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001339 if (*log_level_ptr == -1)
1340 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001341 break;
1342
1343 case sAllowTcpForwarding:
1344 intptr = &options->allow_tcp_forwarding;
Damien Milleraa5b3f82012-12-03 09:50:54 +11001345 multistate_ptr = multistate_tcpfwd;
1346 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001347
Damien Miller7acefbb2014-07-18 14:11:24 +10001348 case sAllowStreamLocalForwarding:
1349 intptr = &options->allow_streamlocal_forwarding;
1350 multistate_ptr = multistate_tcpfwd;
1351 goto parse_multistate;
1352
Damien Miller4f755cd2008-05-19 14:57:41 +10001353 case sAllowAgentForwarding:
1354 intptr = &options->allow_agent_forwarding;
1355 goto parse_flag;
1356
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001357 case sUsePrivilegeSeparation:
1358 intptr = &use_privsep;
Damien Miller69ff1df2011-06-23 08:30:03 +10001359 multistate_ptr = multistate_privsep;
1360 goto parse_multistate;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001361
Ben Lindstromade03f62001-12-06 18:22:17 +00001362 case sAllowUsers:
1363 while ((arg = strdelim(&cp)) && *arg != '\0') {
1364 if (options->num_allow_users >= MAX_ALLOW_USERS)
1365 fatal("%s line %d: too many allow users.",
1366 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001367 if (!*activep)
1368 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001369 options->allow_users[options->num_allow_users++] =
1370 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001371 }
1372 break;
1373
1374 case sDenyUsers:
1375 while ((arg = strdelim(&cp)) && *arg != '\0') {
1376 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001377 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001378 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001379 if (!*activep)
1380 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001381 options->deny_users[options->num_deny_users++] =
1382 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001383 }
1384 break;
1385
1386 case sAllowGroups:
1387 while ((arg = strdelim(&cp)) && *arg != '\0') {
1388 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1389 fatal("%s line %d: too many allow groups.",
1390 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001391 if (!*activep)
1392 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001393 options->allow_groups[options->num_allow_groups++] =
1394 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001395 }
1396 break;
1397
1398 case sDenyGroups:
1399 while ((arg = strdelim(&cp)) && *arg != '\0') {
1400 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1401 fatal("%s line %d: too many deny groups.",
1402 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001403 if (!*activep)
1404 continue;
1405 options->deny_groups[options->num_deny_groups++] =
1406 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001407 }
1408 break;
1409
1410 case sCiphers:
1411 arg = strdelim(&cp);
1412 if (!arg || *arg == '\0')
1413 fatal("%s line %d: Missing argument.", filename, linenum);
1414 if (!ciphers_valid(arg))
1415 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1416 filename, linenum, arg ? arg : "<NONE>");
1417 if (options->ciphers == NULL)
1418 options->ciphers = xstrdup(arg);
1419 break;
1420
1421 case sMacs:
1422 arg = strdelim(&cp);
1423 if (!arg || *arg == '\0')
1424 fatal("%s line %d: Missing argument.", filename, linenum);
1425 if (!mac_valid(arg))
1426 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1427 filename, linenum, arg ? arg : "<NONE>");
1428 if (options->macs == NULL)
1429 options->macs = xstrdup(arg);
1430 break;
1431
Damien Millerd5f62bf2010-09-24 22:11:14 +10001432 case sKexAlgorithms:
1433 arg = strdelim(&cp);
1434 if (!arg || *arg == '\0')
1435 fatal("%s line %d: Missing argument.",
1436 filename, linenum);
1437 if (!kex_names_valid(arg))
1438 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1439 filename, linenum, arg ? arg : "<NONE>");
1440 if (options->kex_algorithms == NULL)
1441 options->kex_algorithms = xstrdup(arg);
1442 break;
1443
Ben Lindstromade03f62001-12-06 18:22:17 +00001444 case sProtocol:
1445 intptr = &options->protocol;
1446 arg = strdelim(&cp);
1447 if (!arg || *arg == '\0')
1448 fatal("%s line %d: Missing argument.", filename, linenum);
1449 value = proto_spec(arg);
1450 if (value == SSH_PROTO_UNKNOWN)
1451 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001452 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001453 if (*intptr == SSH_PROTO_UNKNOWN)
1454 *intptr = value;
1455 break;
1456
1457 case sSubsystem:
1458 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1459 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001460 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001461 }
1462 arg = strdelim(&cp);
1463 if (!arg || *arg == '\0')
1464 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001465 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001466 if (!*activep) {
1467 arg = strdelim(&cp);
1468 break;
1469 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001470 for (i = 0; i < options->num_subsystems; i++)
1471 if (strcmp(arg, options->subsystem_name[i]) == 0)
1472 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001473 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001474 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1475 arg = strdelim(&cp);
1476 if (!arg || *arg == '\0')
1477 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001478 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001479 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001480
1481 /* Collect arguments (separate to executable) */
1482 p = xstrdup(arg);
1483 len = strlen(p) + 1;
1484 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1485 len += 1 + strlen(arg);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001486 p = xreallocarray(p, 1, len);
Damien Miller917f9b62006-07-10 20:36:47 +10001487 strlcat(p, " ", len);
1488 strlcat(p, arg, len);
1489 }
1490 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001491 options->num_subsystems++;
1492 break;
1493
1494 case sMaxStartups:
1495 arg = strdelim(&cp);
1496 if (!arg || *arg == '\0')
1497 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001498 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001499 if ((n = sscanf(arg, "%d:%d:%d",
1500 &options->max_startups_begin,
1501 &options->max_startups_rate,
1502 &options->max_startups)) == 3) {
1503 if (options->max_startups_begin >
1504 options->max_startups ||
1505 options->max_startups_rate > 100 ||
1506 options->max_startups_rate < 1)
1507 fatal("%s line %d: Illegal MaxStartups spec.",
1508 filename, linenum);
1509 } else if (n != 1)
1510 fatal("%s line %d: Illegal MaxStartups spec.",
1511 filename, linenum);
1512 else
1513 options->max_startups = options->max_startups_begin;
1514 break;
1515
Darren Tucker89413db2004-05-24 10:36:23 +10001516 case sMaxAuthTries:
1517 intptr = &options->max_authtries;
1518 goto parse_int;
1519
Damien Miller7207f642008-05-19 15:34:50 +10001520 case sMaxSessions:
1521 intptr = &options->max_sessions;
1522 goto parse_int;
1523
Ben Lindstromade03f62001-12-06 18:22:17 +00001524 case sBanner:
1525 charptr = &options->banner;
1526 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001527
Ben Lindstromade03f62001-12-06 18:22:17 +00001528 /*
1529 * These options can contain %X options expanded at
1530 * connect time, so that you can specify paths like:
1531 *
1532 * AuthorizedKeysFile /etc/ssh_keys/%u
1533 */
1534 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001535 if (*activep && options->num_authkeys_files == 0) {
1536 while ((arg = strdelim(&cp)) && *arg != '\0') {
1537 if (options->num_authkeys_files >=
1538 MAX_AUTHKEYS_FILES)
1539 fatal("%s line %d: "
1540 "too many authorized keys files.",
1541 filename, linenum);
1542 options->authorized_keys_files[
1543 options->num_authkeys_files++] =
1544 tilde_expand_filename(arg, getuid());
1545 }
1546 }
1547 return 0;
1548
Damien Miller30da3442010-05-10 11:58:03 +10001549 case sAuthorizedPrincipalsFile:
1550 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001551 arg = strdelim(&cp);
1552 if (!arg || *arg == '\0')
1553 fatal("%s line %d: missing file name.",
1554 filename, linenum);
1555 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001556 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001557 /* increase optional counter */
1558 if (intptr != NULL)
1559 *intptr = *intptr + 1;
1560 }
1561 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001562
1563 case sClientAliveInterval:
1564 intptr = &options->client_alive_interval;
1565 goto parse_time;
1566
1567 case sClientAliveCountMax:
1568 intptr = &options->client_alive_count_max;
1569 goto parse_int;
1570
Darren Tucker46bc0752004-05-02 22:11:30 +10001571 case sAcceptEnv:
1572 while ((arg = strdelim(&cp)) && *arg != '\0') {
1573 if (strchr(arg, '=') != NULL)
1574 fatal("%s line %d: Invalid environment name.",
1575 filename, linenum);
1576 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1577 fatal("%s line %d: too many allow env.",
1578 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001579 if (!*activep)
Damien Millerc24da772012-06-20 21:53:58 +10001580 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001581 options->accept_env[options->num_accept_env++] =
1582 xstrdup(arg);
1583 }
1584 break;
1585
Damien Millerd27b9472005-12-13 19:29:02 +11001586 case sPermitTunnel:
1587 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001588 arg = strdelim(&cp);
1589 if (!arg || *arg == '\0')
1590 fatal("%s line %d: Missing yes/point-to-point/"
1591 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001592 value = -1;
1593 for (i = 0; tunmode_desc[i].val != -1; i++)
1594 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1595 value = tunmode_desc[i].val;
1596 break;
1597 }
1598 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001599 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1600 "no argument: %s", filename, linenum, arg);
1601 if (*intptr == -1)
1602 *intptr = value;
1603 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001604
Darren Tucker45150472006-07-12 22:34:17 +10001605 case sMatch:
1606 if (cmdline)
1607 fatal("Match directive not supported as a command-line "
1608 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001609 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10001610 if (value < 0)
1611 fatal("%s line %d: Bad Match condition", filename,
1612 linenum);
1613 *activep = value;
1614 break;
1615
Damien Miller9b439df2006-07-24 14:04:00 +10001616 case sPermitOpen:
1617 arg = strdelim(&cp);
1618 if (!arg || *arg == '\0')
1619 fatal("%s line %d: missing PermitOpen specification",
1620 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001621 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001622 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001623 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001624 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001625 options->num_permitted_opens = 0;
1626 }
Damien Miller9b439df2006-07-24 14:04:00 +10001627 break;
1628 }
Damien Millerc6081482012-04-22 11:18:53 +10001629 if (strcmp(arg, "none") == 0) {
1630 if (*activep && n == -1) {
Damien Millerc6081482012-04-22 11:18:53 +10001631 options->num_permitted_opens = 1;
1632 channel_disable_adm_local_opens();
1633 }
1634 break;
1635 }
Damien Millera29b95e2007-01-05 16:28:36 +11001636 if (*activep && n == -1)
1637 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001638 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1639 p = hpdelim(&arg);
1640 if (p == NULL)
1641 fatal("%s line %d: missing host in PermitOpen",
1642 filename, linenum);
1643 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001644 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001645 fatal("%s line %d: bad port number in "
1646 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001647 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001648 options->num_permitted_opens =
1649 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001650 }
Damien Miller9b439df2006-07-24 14:04:00 +10001651 break;
1652
Damien Millere2754432006-07-24 14:06:47 +10001653 case sForceCommand:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001654 if (cp == NULL || *cp == '\0')
Damien Millere2754432006-07-24 14:06:47 +10001655 fatal("%.200s line %d: Missing argument.", filename,
1656 linenum);
1657 len = strspn(cp, WHITESPACE);
1658 if (*activep && options->adm_forced_command == NULL)
1659 options->adm_forced_command = xstrdup(cp + len);
1660 return 0;
1661
Damien Millerd8cb1f12008-02-10 22:40:12 +11001662 case sChrootDirectory:
1663 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001664
1665 arg = strdelim(&cp);
1666 if (!arg || *arg == '\0')
1667 fatal("%s line %d: missing file name.",
1668 filename, linenum);
1669 if (*activep && *charptr == NULL)
1670 *charptr = xstrdup(arg);
1671 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001672
Damien Miller1aed65e2010-03-04 21:53:35 +11001673 case sTrustedUserCAKeys:
1674 charptr = &options->trusted_user_ca_keys;
1675 goto parse_filename;
1676
1677 case sRevokedKeys:
1678 charptr = &options->revoked_keys_file;
1679 goto parse_filename;
1680
Damien Miller0dac6fb2010-11-20 15:19:38 +11001681 case sIPQoS:
1682 arg = strdelim(&cp);
1683 if ((value = parse_ipqos(arg)) == -1)
1684 fatal("%s line %d: Bad IPQoS value: %s",
1685 filename, linenum, arg);
1686 arg = strdelim(&cp);
1687 if (arg == NULL)
1688 value2 = value;
1689 else if ((value2 = parse_ipqos(arg)) == -1)
1690 fatal("%s line %d: Bad IPQoS value: %s",
1691 filename, linenum, arg);
1692 if (*activep) {
1693 options->ip_qos_interactive = value;
1694 options->ip_qos_bulk = value2;
1695 }
1696 break;
1697
Damien Miller23528812012-04-22 11:24:43 +10001698 case sVersionAddendum:
dtucker@openbsd.orgbd902b82015-04-23 04:53:53 +00001699 if (cp == NULL || *cp == '\0')
Damien Miller23528812012-04-22 11:24:43 +10001700 fatal("%.200s line %d: Missing argument.", filename,
1701 linenum);
1702 len = strspn(cp, WHITESPACE);
1703 if (*activep && options->version_addendum == NULL) {
1704 if (strcasecmp(cp + len, "none") == 0)
1705 options->version_addendum = xstrdup("");
1706 else if (strchr(cp + len, '\r') != NULL)
1707 fatal("%.200s line %d: Invalid argument",
1708 filename, linenum);
1709 else
1710 options->version_addendum = xstrdup(cp + len);
1711 }
1712 return 0;
1713
Damien Miller09d3e122012-10-31 08:58:58 +11001714 case sAuthorizedKeysCommand:
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001715 if (cp == NULL)
1716 fatal("%.200s line %d: Missing argument.", filename,
1717 linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001718 len = strspn(cp, WHITESPACE);
1719 if (*activep && options->authorized_keys_command == NULL) {
1720 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1721 fatal("%.200s line %d: AuthorizedKeysCommand "
1722 "must be an absolute path",
1723 filename, linenum);
1724 options->authorized_keys_command = xstrdup(cp + len);
1725 }
1726 return 0;
1727
1728 case sAuthorizedKeysCommandUser:
1729 charptr = &options->authorized_keys_command_user;
1730
1731 arg = strdelim(&cp);
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001732 if (!arg || *arg == '\0')
1733 fatal("%s line %d: missing AuthorizedKeysCommandUser "
1734 "argument.", filename, linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001735 if (*activep && *charptr == NULL)
1736 *charptr = xstrdup(arg);
1737 break;
1738
Damien Millera6e3f012012-11-04 23:21:40 +11001739 case sAuthenticationMethods:
1740 if (*activep && options->num_auth_methods == 0) {
1741 while ((arg = strdelim(&cp)) && *arg != '\0') {
1742 if (options->num_auth_methods >=
1743 MAX_AUTH_METHODS)
1744 fatal("%s line %d: "
1745 "too many authentication methods.",
1746 filename, linenum);
1747 if (auth2_methods_valid(arg, 0) != 0)
1748 fatal("%s line %d: invalid "
1749 "authentication method list.",
1750 filename, linenum);
1751 options->auth_methods[
1752 options->num_auth_methods++] = xstrdup(arg);
1753 }
1754 }
1755 return 0;
1756
Damien Miller7acefbb2014-07-18 14:11:24 +10001757 case sStreamLocalBindMask:
1758 arg = strdelim(&cp);
1759 if (!arg || *arg == '\0')
1760 fatal("%s line %d: missing StreamLocalBindMask argument.",
1761 filename, linenum);
1762 /* Parse mode in octal format */
1763 value = strtol(arg, &p, 8);
1764 if (arg == p || value < 0 || value > 0777)
1765 fatal("%s line %d: Bad mask.", filename, linenum);
1766 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1767 break;
1768
1769 case sStreamLocalBindUnlink:
1770 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1771 goto parse_flag;
1772
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001773 case sFingerprintHash:
1774 arg = strdelim(&cp);
1775 if (!arg || *arg == '\0')
1776 fatal("%.200s line %d: Missing argument.",
1777 filename, linenum);
1778 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1779 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1780 filename, linenum, arg);
1781 if (*activep)
1782 options->fingerprint_hash = value;
1783 break;
1784
Ben Lindstromade03f62001-12-06 18:22:17 +00001785 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001786 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001787 filename, linenum, arg);
1788 while (arg)
1789 arg = strdelim(&cp);
1790 break;
1791
Damien Millerf9b3feb2003-05-16 11:38:32 +10001792 case sUnsupported:
1793 logit("%s line %d: Unsupported option %s",
1794 filename, linenum, arg);
1795 while (arg)
1796 arg = strdelim(&cp);
1797 break;
1798
Ben Lindstromade03f62001-12-06 18:22:17 +00001799 default:
1800 fatal("%s line %d: Missing handler for opcode %s (%d)",
1801 filename, linenum, arg, opcode);
1802 }
1803 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1804 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1805 filename, linenum, arg);
1806 return 0;
1807}
1808
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001809/* Reads the server configuration file. */
1810
Damien Miller4af51302000-04-16 11:18:38 +10001811void
Darren Tucker645ab752004-06-25 13:33:20 +10001812load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001813{
Damien Miller46cb75a2012-07-31 12:22:37 +10001814 char line[4096], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001815 FILE *f;
Damien Miller46cb75a2012-07-31 12:22:37 +10001816 int lineno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001817
Darren Tucker645ab752004-06-25 13:33:20 +10001818 debug2("%s: filename %s", __func__, filename);
1819 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001820 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001821 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001822 }
Darren Tucker645ab752004-06-25 13:33:20 +10001823 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001824 while (fgets(line, sizeof(line), f)) {
Damien Miller46cb75a2012-07-31 12:22:37 +10001825 lineno++;
1826 if (strlen(line) == sizeof(line) - 1)
1827 fatal("%s line %d too long", filename, lineno);
Darren Tucker645ab752004-06-25 13:33:20 +10001828 /*
1829 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001830 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001831 * line numbers later for error messages
1832 */
1833 if ((cp = strchr(line, '#')) != NULL)
1834 memcpy(cp, "\n", 2);
1835 cp = line + strspn(line, " \t\r");
1836
1837 buffer_append(conf, cp, strlen(cp));
1838 }
1839 buffer_append(conf, "\0", 1);
1840 fclose(f);
1841 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1842}
1843
1844void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001845parse_server_match_config(ServerOptions *options,
1846 struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10001847{
Darren Tucker45150472006-07-12 22:34:17 +10001848 ServerOptions mo;
1849
1850 initialize_server_options(&mo);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001851 parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11001852 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001853}
1854
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001855int parse_server_match_testspec(struct connection_info *ci, char *spec)
1856{
1857 char *p;
1858
1859 while ((p = strsep(&spec, ",")) && *p != '\0') {
1860 if (strncmp(p, "addr=", 5) == 0) {
1861 ci->address = xstrdup(p + 5);
1862 } else if (strncmp(p, "host=", 5) == 0) {
1863 ci->host = xstrdup(p + 5);
1864 } else if (strncmp(p, "user=", 5) == 0) {
1865 ci->user = xstrdup(p + 5);
1866 } else if (strncmp(p, "laddr=", 6) == 0) {
1867 ci->laddress = xstrdup(p + 6);
1868 } else if (strncmp(p, "lport=", 6) == 0) {
1869 ci->lport = a2port(p + 6);
1870 if (ci->lport == -1) {
1871 fprintf(stderr, "Invalid port '%s' in test mode"
1872 " specification %s\n", p+6, p);
1873 return -1;
1874 }
1875 } else {
1876 fprintf(stderr, "Invalid test mode specification %s\n",
1877 p);
1878 return -1;
1879 }
1880 }
1881 return 0;
1882}
1883
1884/*
1885 * returns 1 for a complete spec, 0 for partial spec and -1 for an
1886 * empty spec.
1887 */
1888int server_match_spec_complete(struct connection_info *ci)
1889{
1890 if (ci->user && ci->host && ci->address)
1891 return 1; /* complete */
1892 if (!ci->user && !ci->host && !ci->address)
1893 return -1; /* empty */
1894 return 0; /* partial */
1895}
1896
Darren Tucker1629c072007-02-19 22:25:37 +11001897/*
1898 * Copy any supported values that are set.
1899 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001900 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001901 * array values that are not used pre-authentication, because any that we
1902 * do use must be explictly sent in mm_getpwnamallow().
1903 */
Darren Tucker45150472006-07-12 22:34:17 +10001904void
Darren Tucker1629c072007-02-19 22:25:37 +11001905copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001906{
Damien Miller534b2cc2013-12-05 14:07:27 +11001907#define M_CP_INTOPT(n) do {\
1908 if (src->n != -1) \
1909 dst->n = src->n; \
1910} while (0)
1911
Darren Tucker1629c072007-02-19 22:25:37 +11001912 M_CP_INTOPT(password_authentication);
1913 M_CP_INTOPT(gss_authentication);
1914 M_CP_INTOPT(rsa_authentication);
1915 M_CP_INTOPT(pubkey_authentication);
1916 M_CP_INTOPT(kerberos_authentication);
1917 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001918 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001919 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001920 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001921 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001922
1923 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10001924 M_CP_INTOPT(allow_streamlocal_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001925 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001926 M_CP_INTOPT(permit_tun);
Damien Miller7acefbb2014-07-18 14:11:24 +10001927 M_CP_INTOPT(fwd_opts.gateway_ports);
Darren Tucker1629c072007-02-19 22:25:37 +11001928 M_CP_INTOPT(x11_display_offset);
1929 M_CP_INTOPT(x11_forwarding);
1930 M_CP_INTOPT(x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11001931 M_CP_INTOPT(permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10001932 M_CP_INTOPT(permit_user_rc);
Damien Miller7207f642008-05-19 15:34:50 +10001933 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001934 M_CP_INTOPT(max_authtries);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001935 M_CP_INTOPT(ip_qos_interactive);
1936 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001937 M_CP_INTOPT(rekey_limit);
1938 M_CP_INTOPT(rekey_interval);
Darren Tucker1629c072007-02-19 22:25:37 +11001939
Damien Miller534b2cc2013-12-05 14:07:27 +11001940 /* M_CP_STROPT and M_CP_STRARRAYOPT should not appear before here */
1941#define M_CP_STROPT(n) do {\
1942 if (src->n != NULL && dst->n != src->n) { \
1943 free(dst->n); \
1944 dst->n = src->n; \
1945 } \
1946} while(0)
1947#define M_CP_STRARRAYOPT(n, num_n) do {\
1948 if (src->num_n != 0) { \
1949 for (dst->num_n = 0; dst->num_n < src->num_n; dst->num_n++) \
1950 dst->n[dst->num_n] = xstrdup(src->n[dst->num_n]); \
1951 } \
1952} while(0)
1953
Damien Millerf2e407e2011-05-20 19:04:14 +10001954 /* See comment in servconf.h */
1955 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10001956
Damien Millerc2411902011-05-20 19:03:49 +10001957 /*
1958 * The only things that should be below this point are string options
1959 * which are only used after authentication.
1960 */
Damien Miller5d74e582011-05-20 19:03:31 +10001961 if (preauth)
1962 return;
Damien Millerd8478b62011-05-29 21:39:36 +10001963
Damien Miller5d74e582011-05-20 19:03:31 +10001964 M_CP_STROPT(adm_forced_command);
1965 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001966}
1967
Darren Tucker1629c072007-02-19 22:25:37 +11001968#undef M_CP_INTOPT
1969#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10001970#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11001971
Darren Tucker45150472006-07-12 22:34:17 +10001972void
1973parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001974 struct connection_info *connectinfo)
Darren Tucker45150472006-07-12 22:34:17 +10001975{
1976 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001977 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001978
1979 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1980
Darren Tucker9fbac712004-08-12 22:41:44 +10001981 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001982 active = connectinfo ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001983 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001984 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001985 if (process_server_config_line(options, cp, filename,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001986 linenum++, &active, connectinfo) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001987 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001988 }
Darren Tuckera627d422013-06-02 07:31:17 +10001989 free(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001990 if (bad_options > 0)
1991 fatal("%s: terminating, %d bad configuration options",
1992 filename, bad_options);
dtucker@openbsd.org531a57a2015-04-29 03:48:56 +00001993 process_queued_listen_addrs(options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001994}
Darren Tuckere7140f22008-06-10 23:01:51 +10001995
1996static const char *
Damien Miller82c55872011-06-23 08:20:30 +10001997fmt_multistate_int(int val, const struct multistate *m)
1998{
1999 u_int i;
2000
2001 for (i = 0; m[i].key != NULL; i++) {
2002 if (m[i].value == val)
2003 return m[i].key;
2004 }
2005 return "UNKNOWN";
2006}
2007
2008static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10002009fmt_intarg(ServerOpCodes code, int val)
2010{
Damien Miller82c55872011-06-23 08:20:30 +10002011 if (val == -1)
2012 return "unset";
2013 switch (code) {
2014 case sAddressFamily:
2015 return fmt_multistate_int(val, multistate_addressfamily);
2016 case sPermitRootLogin:
2017 return fmt_multistate_int(val, multistate_permitrootlogin);
2018 case sGatewayPorts:
2019 return fmt_multistate_int(val, multistate_gatewayports);
2020 case sCompression:
2021 return fmt_multistate_int(val, multistate_compression);
Damien Miller69ff1df2011-06-23 08:30:03 +10002022 case sUsePrivilegeSeparation:
2023 return fmt_multistate_int(val, multistate_privsep);
Damien Milleraa5b3f82012-12-03 09:50:54 +11002024 case sAllowTcpForwarding:
2025 return fmt_multistate_int(val, multistate_tcpfwd);
Damien Miller7acefbb2014-07-18 14:11:24 +10002026 case sAllowStreamLocalForwarding:
2027 return fmt_multistate_int(val, multistate_tcpfwd);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002028 case sFingerprintHash:
2029 return ssh_digest_alg_name(val);
Damien Miller82c55872011-06-23 08:20:30 +10002030 case sProtocol:
Darren Tuckere7140f22008-06-10 23:01:51 +10002031 switch (val) {
2032 case SSH_PROTO_1:
2033 return "1";
2034 case SSH_PROTO_2:
2035 return "2";
2036 case (SSH_PROTO_1|SSH_PROTO_2):
2037 return "2,1";
2038 default:
2039 return "UNKNOWN";
2040 }
Damien Miller82c55872011-06-23 08:20:30 +10002041 default:
2042 switch (val) {
2043 case 0:
2044 return "no";
2045 case 1:
2046 return "yes";
2047 default:
2048 return "UNKNOWN";
2049 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002050 }
Darren Tuckere7140f22008-06-10 23:01:51 +10002051}
2052
2053static const char *
2054lookup_opcode_name(ServerOpCodes code)
2055{
2056 u_int i;
2057
2058 for (i = 0; keywords[i].name != NULL; i++)
2059 if (keywords[i].opcode == code)
2060 return(keywords[i].name);
2061 return "UNKNOWN";
2062}
2063
2064static void
2065dump_cfg_int(ServerOpCodes code, int val)
2066{
2067 printf("%s %d\n", lookup_opcode_name(code), val);
2068}
2069
2070static void
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002071dump_cfg_oct(ServerOpCodes code, int val)
2072{
2073 printf("%s 0%o\n", lookup_opcode_name(code), val);
2074}
2075
2076static void
Darren Tuckere7140f22008-06-10 23:01:51 +10002077dump_cfg_fmtint(ServerOpCodes code, int val)
2078{
2079 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
2080}
2081
2082static void
2083dump_cfg_string(ServerOpCodes code, const char *val)
2084{
2085 if (val == NULL)
2086 return;
djm@openbsd.org161cf412014-12-22 07:55:51 +00002087 printf("%s %s\n", lookup_opcode_name(code),
2088 val == NULL ? "none" : val);
Darren Tuckere7140f22008-06-10 23:01:51 +10002089}
2090
2091static void
2092dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
2093{
2094 u_int i;
2095
2096 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10002097 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
2098}
2099
2100static void
2101dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
2102{
2103 u_int i;
2104
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002105 if (count <= 0)
2106 return;
Damien Millerd8478b62011-05-29 21:39:36 +10002107 printf("%s", lookup_opcode_name(code));
2108 for (i = 0; i < count; i++)
2109 printf(" %s", vals[i]);
2110 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002111}
2112
2113void
2114dump_config(ServerOptions *o)
2115{
2116 u_int i;
2117 int ret;
2118 struct addrinfo *ai;
2119 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002120 char *laddr1 = xstrdup(""), *laddr2 = NULL;
Darren Tuckere7140f22008-06-10 23:01:51 +10002121
2122 /* these are usually at the top of the config */
2123 for (i = 0; i < o->num_ports; i++)
2124 printf("port %d\n", o->ports[i]);
2125 dump_cfg_fmtint(sProtocol, o->protocol);
2126 dump_cfg_fmtint(sAddressFamily, o->address_family);
2127
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002128 /*
2129 * ListenAddress must be after Port. add_one_listen_addr pushes
2130 * addresses onto a stack, so to maintain ordering we need to
2131 * print these in reverse order.
2132 */
Darren Tuckere7140f22008-06-10 23:01:51 +10002133 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
2134 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
2135 sizeof(addr), port, sizeof(port),
2136 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
2137 error("getnameinfo failed: %.100s",
2138 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
2139 strerror(errno));
2140 } else {
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002141 laddr2 = laddr1;
Darren Tuckere7140f22008-06-10 23:01:51 +10002142 if (ai->ai_family == AF_INET6)
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002143 xasprintf(&laddr1, "listenaddress [%s]:%s\n%s",
2144 addr, port, laddr2);
Darren Tuckere7140f22008-06-10 23:01:51 +10002145 else
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002146 xasprintf(&laddr1, "listenaddress %s:%s\n%s",
2147 addr, port, laddr2);
2148 free(laddr2);
Darren Tuckere7140f22008-06-10 23:01:51 +10002149 }
2150 }
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002151 printf("%s", laddr1);
2152 free(laddr1);
Darren Tuckere7140f22008-06-10 23:01:51 +10002153
2154 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10002155#ifdef USE_PAM
Darren Tucker70860b62015-04-17 10:56:13 +10002156 dump_cfg_fmtint(sUsePAM, o->use_pam);
Damien Miller212f0b02008-07-23 17:42:29 +10002157#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002158 dump_cfg_int(sServerKeyBits, o->server_key_bits);
2159 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
2160 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
2161 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
2162 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11002163 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10002164 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
2165 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
dtucker@openbsd.org1108ae22015-04-23 04:59:10 +00002166 dump_cfg_oct(sStreamLocalBindMask, o->fwd_opts.streamlocal_bind_mask);
Darren Tuckere7140f22008-06-10 23:01:51 +10002167
2168 /* formatted integer arguments */
2169 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
2170 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
2171 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
2172 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2173 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
2174 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
2175 o->hostbased_uses_name_from_packet_only);
2176 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
2177 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10002178#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10002179 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
2180 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
2181 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10002182# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10002183 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10002184# endif
2185#endif
2186#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10002187 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2188 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10002189#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002190 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2191 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2192 o->kbd_interactive_authentication);
2193 dump_cfg_fmtint(sChallengeResponseAuthentication,
2194 o->challenge_response_authentication);
2195 dump_cfg_fmtint(sPrintMotd, o->print_motd);
2196 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
2197 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
2198 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002199 dump_cfg_fmtint(sPermitTTY, o->permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002200 dump_cfg_fmtint(sPermitUserRC, o->permit_user_rc);
Darren Tuckere7140f22008-06-10 23:01:51 +10002201 dump_cfg_fmtint(sStrictModes, o->strict_modes);
2202 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
2203 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
2204 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
2205 dump_cfg_fmtint(sUseLogin, o->use_login);
2206 dump_cfg_fmtint(sCompression, o->compression);
Damien Miller7acefbb2014-07-18 14:11:24 +10002207 dump_cfg_fmtint(sGatewayPorts, o->fwd_opts.gateway_ports);
Darren Tuckere7140f22008-06-10 23:01:51 +10002208 dump_cfg_fmtint(sUseDNS, o->use_dns);
2209 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002210 dump_cfg_fmtint(sAllowAgentForwarding, o->allow_agent_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002211 dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
Darren Tuckere7140f22008-06-10 23:01:51 +10002212 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002213 dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
Darren Tuckere7140f22008-06-10 23:01:51 +10002214
2215 /* string arguments */
2216 dump_cfg_string(sPidFile, o->pid_file);
2217 dump_cfg_string(sXAuthLocation, o->xauth_location);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002218 dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
2219 dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
Darren Tuckere7140f22008-06-10 23:01:51 +10002220 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10002221 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11002222 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11002223 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
2224 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10002225 dump_cfg_string(sAuthorizedPrincipalsFile,
2226 o->authorized_principals_file);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002227 dump_cfg_string(sVersionAddendum, *o->version_addendum == '\0'
2228 ? "none" : o->version_addendum);
Damien Miller09d3e122012-10-31 08:58:58 +11002229 dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
2230 dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
Damien Miller85b45e02013-07-20 13:21:52 +10002231 dump_cfg_string(sHostKeyAgent, o->host_key_agent);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002232 dump_cfg_string(sKexAlgorithms,
djm@openbsd.org259a02e2014-10-13 00:38:35 +00002233 o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
djm@openbsd.org1f729f02015-01-13 07:39:19 +00002234 dump_cfg_string(sHostbasedAcceptedKeyTypes, o->hostbased_key_types ?
2235 o->hostbased_key_types : KEX_DEFAULT_PK_ALG);
2236 dump_cfg_string(sPubkeyAcceptedKeyTypes, o->pubkey_key_types ?
2237 o->pubkey_key_types : KEX_DEFAULT_PK_ALG);
Darren Tuckere7140f22008-06-10 23:01:51 +10002238
2239 /* string arguments requiring a lookup */
2240 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
2241 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
2242
2243 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10002244 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
2245 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002246 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
2247 o->host_key_files);
dtucker@openbsd.org40132ff2015-04-17 04:12:35 +00002248 dump_cfg_strarray(sHostCertificate, o->num_host_cert_files,
Damien Miller0a80ca12010-02-27 07:55:05 +11002249 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002250 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
2251 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
2252 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
2253 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
2254 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
Damien Millera6e3f012012-11-04 23:21:40 +11002255 dump_cfg_strarray_oneline(sAuthenticationMethods,
2256 o->num_auth_methods, o->auth_methods);
Darren Tuckere7140f22008-06-10 23:01:51 +10002257
2258 /* other arguments */
2259 for (i = 0; i < o->num_subsystems; i++)
2260 printf("subsystem %s %s\n", o->subsystem_name[i],
2261 o->subsystem_args[i]);
2262
2263 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
2264 o->max_startups_rate, o->max_startups);
2265
2266 for (i = 0; tunmode_desc[i].val != -1; i++)
2267 if (tunmode_desc[i].val == o->permit_tun) {
2268 s = tunmode_desc[i].text;
2269 break;
2270 }
2271 dump_cfg_string(sPermitTunnel, s);
2272
Damien Miller91475862011-05-05 14:14:34 +10002273 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2274 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11002275
Damien Millera6d6c1f2013-08-21 02:40:01 +10002276 printf("rekeylimit %lld %d\n", (long long)o->rekey_limit,
2277 o->rekey_interval);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002278
Darren Tuckere7140f22008-06-10 23:01:51 +10002279 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10002280}