blob: a8a40f97e91934f37882bf2886a8843ffb00ca14 [file] [log] [blame]
Damien Miller23528812012-04-22 11:24:43 +10001/* $OpenBSD: servconf.c,v 1.225 2012/04/12 02:42:32 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10005 *
Damien Millere4340be2000-09-16 13:29:08 +11006 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110011 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100012
13#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
Damien Millere3b60b52006-07-10 21:08:03 +100015#include <sys/types.h>
16#include <sys/socket.h>
17
Damien Miller0dac6fb2010-11-20 15:19:38 +110018#include <netinet/in.h>
19#include <netinet/in_systm.h>
20#include <netinet/ip.h>
21
Damien Millerb8fe89c2006-07-24 14:51:00 +100022#include <netdb.h>
Damien Miller565ca3f2006-08-19 00:23:15 +100023#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100024#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100025#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100026#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100027#include <signal.h>
Damien Millere6b3b612006-07-24 14:01:23 +100028#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100029#include <stdarg.h>
Darren Tuckere7140f22008-06-10 23:01:51 +100030#include <errno.h>
Damien Millerbe43ebf2006-07-24 13:51:51 +100031
Damien Millerb84886b2008-05-19 15:05:07 +100032#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100033#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100036#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037#include "servconf.h"
Damien Miller78928792000-04-12 20:17:38 +100038#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include "misc.h"
41#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100042#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000043#include "kex.h"
44#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100045#include "match.h"
Damien Miller9b439df2006-07-24 14:04:00 +100046#include "channels.h"
Damien Miller565ca3f2006-08-19 00:23:15 +100047#include "groupaccess.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048
Damien Miller3dc71ad2009-01-28 16:31:22 +110049static void add_listen_addr(ServerOptions *, char *, int);
50static void add_one_listen_addr(ServerOptions *, char *, int);
Damien Miller34132e52000-01-14 15:45:46 +110051
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000052/* Use of privilege separation or not */
53extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100054extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056/* Initializes the server options to their default values. */
57
Damien Miller4af51302000-04-16 11:18:38 +100058void
Damien Miller95def091999-11-25 00:26:21 +110059initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060{
Damien Miller95def091999-11-25 00:26:21 +110061 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110062
63 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100064 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110065
66 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110067 options->num_ports = 0;
68 options->ports_from_cmdline = 0;
69 options->listen_addrs = NULL;
Darren Tucker0f383232005-01-20 10:57:56 +110070 options->address_family = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110071 options->num_host_key_files = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110072 options->num_host_cert_files = 0;
Damien Miller6f83b8e2000-05-02 09:23:45 +100073 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110074 options->server_key_bits = -1;
75 options->login_grace_time = -1;
76 options->key_regeneration_time = -1;
Ben Lindstromd8a90212001-02-15 03:08:27 +000077 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110078 options->ignore_rhosts = -1;
79 options->ignore_user_known_hosts = -1;
80 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +000081 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +110082 options->x11_forwarding = -1;
83 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +110084 options->x11_use_localhost = -1;
Damien Millerd3a18572000-06-07 19:55:44 +100085 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +110086 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +110087 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +110088 options->log_facility = SYSLOG_FACILITY_NOT_SET;
89 options->log_level = SYSLOG_LEVEL_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110090 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +000091 options->hostbased_authentication = -1;
92 options->hostbased_uses_name_from_packet_only = -1;
Damien Miller95def091999-11-25 00:26:21 +110093 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110094 options->pubkey_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +110095 options->kerberos_authentication = -1;
96 options->kerberos_or_local_passwd = -1;
97 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +110098 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +100099 options->gss_authentication=-1;
100 options->gss_cleanup_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100101 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100102 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000103 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100104 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000105 options->permit_user_env = -1;
Damien Miller95def091999-11-25 00:26:21 +1100106 options->use_login = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000107 options->compression = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100108 options->allow_tcp_forwarding = -1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000109 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100110 options->num_allow_users = 0;
111 options->num_deny_users = 0;
112 options->num_allow_groups = 0;
113 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000114 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000115 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000116 options->kex_algorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000117 options->protocol = SSH_PROTO_UNKNOWN;
Damien Millere247cc42000-05-07 12:03:14 +1000118 options->gateway_ports = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000119 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000120 options->max_startups_begin = -1;
121 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000122 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000123 options->max_authtries = -1;
Damien Miller7207f642008-05-19 15:34:50 +1000124 options->max_sessions = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000125 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000126 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000127 options->client_alive_interval = -1;
128 options->client_alive_count_max = -1;
Damien Millerd8478b62011-05-29 21:39:36 +1000129 options->num_authkeys_files = 0;
Darren Tucker46bc0752004-05-02 22:11:30 +1000130 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100131 options->permit_tun = -1;
Damien Millera765cf42006-07-24 14:08:13 +1000132 options->num_permitted_opens = -1;
Damien Millere2754432006-07-24 14:06:47 +1000133 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100134 options->chroot_directory = NULL;
Damien Miller01ed2272008-11-05 16:20:46 +1100135 options->zero_knowledge_password_authentication = -1;
Damien Miller1aed65e2010-03-04 21:53:35 +1100136 options->revoked_keys_file = NULL;
137 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000138 options->authorized_principals_file = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100139 options->ip_qos_interactive = -1;
140 options->ip_qos_bulk = -1;
Damien Miller23528812012-04-22 11:24:43 +1000141 options->version_addendum = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142}
143
Damien Miller4af51302000-04-16 11:18:38 +1000144void
Damien Miller95def091999-11-25 00:26:21 +1100145fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146{
Damien Miller726273e2001-11-12 11:40:11 +1100147 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000148 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000149 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100150
151 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100152 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +1100153 options->protocol = SSH_PROTO_2;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100154 if (options->num_host_key_files == 0) {
155 /* fill default hostkeys for protocols */
156 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100157 options->host_key_files[options->num_host_key_files++] =
158 _PATH_HOST_KEY_FILE;
159 if (options->protocol & SSH_PROTO_2) {
160 options->host_key_files[options->num_host_key_files++] =
161 _PATH_HOST_RSA_KEY_FILE;
162 options->host_key_files[options->num_host_key_files++] =
163 _PATH_HOST_DSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100164#ifdef OPENSSL_HAS_ECC
Damien Millere13cadf2010-09-10 11:15:33 +1000165 options->host_key_files[options->num_host_key_files++] =
166 _PATH_HOST_ECDSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100167#endif
Damien Miller7fc23732002-01-22 23:19:11 +1100168 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100169 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100170 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100171 if (options->num_ports == 0)
172 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
173 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000174 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000175 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000176 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100177 if (options->server_key_bits == -1)
Darren Tucker7499b0c2008-07-02 22:35:43 +1000178 options->server_key_bits = 1024;
Damien Miller95def091999-11-25 00:26:21 +1100179 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000180 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100181 if (options->key_regeneration_time == -1)
182 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000183 if (options->permit_root_login == PERMIT_NOT_SET)
184 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100185 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100186 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100187 if (options->ignore_user_known_hosts == -1)
188 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100189 if (options->print_motd == -1)
190 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000191 if (options->print_lastlog == -1)
192 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100193 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100194 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100195 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100196 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100197 if (options->x11_use_localhost == -1)
198 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000199 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000200 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100201 if (options->strict_modes == -1)
202 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100203 if (options->tcp_keep_alive == -1)
204 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100205 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100206 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100207 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000208 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100209 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100210 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000211 if (options->hostbased_authentication == -1)
212 options->hostbased_authentication = 0;
213 if (options->hostbased_uses_name_from_packet_only == -1)
214 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100215 if (options->rsa_authentication == -1)
216 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100217 if (options->pubkey_authentication == -1)
218 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100219 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000220 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100221 if (options->kerberos_or_local_passwd == -1)
222 options->kerberos_or_local_passwd = 1;
223 if (options->kerberos_ticket_cleanup == -1)
224 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100225 if (options->kerberos_get_afs_token == -1)
226 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000227 if (options->gss_authentication == -1)
228 options->gss_authentication = 0;
229 if (options->gss_cleanup_creds == -1)
230 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100231 if (options->password_authentication == -1)
232 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100233 if (options->kbd_interactive_authentication == -1)
234 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000235 if (options->challenge_response_authentication == -1)
236 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100237 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100238 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000239 if (options->permit_user_env == -1)
240 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100241 if (options->use_login == -1)
242 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000243 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000244 options->compression = COMP_DELAYED;
Damien Miller50a41ed2000-10-16 12:14:42 +1100245 if (options->allow_tcp_forwarding == -1)
246 options->allow_tcp_forwarding = 1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000247 if (options->allow_agent_forwarding == -1)
248 options->allow_agent_forwarding = 1;
Damien Millere247cc42000-05-07 12:03:14 +1000249 if (options->gateway_ports == -1)
250 options->gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000251 if (options->max_startups == -1)
252 options->max_startups = 10;
Damien Miller942da032000-08-18 13:59:06 +1000253 if (options->max_startups_rate == -1)
254 options->max_startups_rate = 100; /* 100% */
255 if (options->max_startups_begin == -1)
256 options->max_startups_begin = options->max_startups;
Darren Tucker89413db2004-05-24 10:36:23 +1000257 if (options->max_authtries == -1)
258 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000259 if (options->max_sessions == -1)
260 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000261 if (options->use_dns == -1)
262 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000263 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100264 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000265 if (options->client_alive_count_max == -1)
266 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000267 if (options->num_authkeys_files == 0) {
268 options->authorized_keys_files[options->num_authkeys_files++] =
269 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS);
270 options->authorized_keys_files[options->num_authkeys_files++] =
271 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS2);
272 }
Damien Millerd27b9472005-12-13 19:29:02 +1100273 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100274 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller01ed2272008-11-05 16:20:46 +1100275 if (options->zero_knowledge_password_authentication == -1)
276 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100277 if (options->ip_qos_interactive == -1)
278 options->ip_qos_interactive = IPTOS_LOWDELAY;
279 if (options->ip_qos_bulk == -1)
280 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller23528812012-04-22 11:24:43 +1000281 if (options->version_addendum == NULL)
282 options->version_addendum = xstrdup("");
Ben Lindstromfb62a692002-06-06 19:47:11 +0000283 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000284 if (use_privsep == -1)
Damien Miller69ff1df2011-06-23 08:30:03 +1000285 use_privsep = PRIVSEP_ON;
Damien Miller4903eb42002-06-21 16:20:44 +1000286
Tim Rice40017b02002-07-14 13:36:49 -0700287#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000288 if (use_privsep && options->compression == 1) {
289 error("This platform does not support both privilege "
290 "separation and compression");
291 error("Compression disabled");
292 options->compression = 0;
293 }
294#endif
295
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296}
297
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100299typedef enum {
300 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100301 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000302 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100303 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100304 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
305 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000306 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100307 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100308 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000309 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100310 sPasswordAuthentication, sKbdInteractiveAuthentication,
311 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000312 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100313 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller12c150e2003-12-17 16:31:10 +1100314 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000315 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Damien Miller50a41ed2000-10-16 12:14:42 +1100316 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000317 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000318 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
Damien Miller7207f642008-05-19 15:34:50 +1000319 sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000320 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100321 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Damien Millerec2eaa32011-05-20 18:57:14 +1000322 sClientAliveCountMax, sAuthorizedKeysFile,
Damien Millerd27b9472005-12-13 19:29:02 +1100323 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100324 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100325 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller0a80ca12010-02-27 07:55:05 +1100326 sZeroKnowledgePasswordAuthentication, sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000327 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Miller23528812012-04-22 11:24:43 +1000328 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000329 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330} ServerOpCodes;
331
Darren Tucker45150472006-07-12 22:34:17 +1000332#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
333#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
334#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
335
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100337static struct {
338 const char *name;
339 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000340 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100341} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100342 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000343#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000344 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000345#else
Darren Tucker45150472006-07-12 22:34:17 +1000346 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000347#endif
Darren Tucker45150472006-07-12 22:34:17 +1000348 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100349 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000350 { "port", sPort, SSHCFG_GLOBAL },
351 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
352 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
353 { "pidfile", sPidFile, SSHCFG_GLOBAL },
354 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
355 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
356 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100357 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000358 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
359 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
360 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100361 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
362 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000363 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100364 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
365 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000366 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000367#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100368 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000369 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
370 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100371#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000372 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000373#else
Darren Tucker45150472006-07-12 22:34:17 +1000374 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100375#endif
376#else
Darren Tucker1629c072007-02-19 22:25:37 +1100377 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000378 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
379 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
380 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000381#endif
Darren Tucker45150472006-07-12 22:34:17 +1000382 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
383 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000384#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100385 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000386 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000387#else
Darren Tucker1629c072007-02-19 22:25:37 +1100388 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000389 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000390#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100391 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
392 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100393 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000394 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
Damien Miller01ed2272008-11-05 16:20:46 +1100395#ifdef JPAKE
396 { "zeroknowledgepasswordauthentication", sZeroKnowledgePasswordAuthentication, SSHCFG_ALL },
397#else
398 { "zeroknowledgepasswordauthentication", sUnsupported, SSHCFG_ALL },
399#endif
Darren Tucker45150472006-07-12 22:34:17 +1000400 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
401 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
402 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
403 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
404 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
405 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
406 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000407 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
408 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
409 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000410 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
411 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100412 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000413 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
414 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
415 { "compression", sCompression, SSHCFG_GLOBAL },
416 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
417 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
418 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000419 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000420 { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
421 { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
422 { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
423 { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
424 { "ciphers", sCiphers, SSHCFG_GLOBAL },
425 { "macs", sMacs, SSHCFG_GLOBAL },
426 { "protocol", sProtocol, SSHCFG_GLOBAL },
427 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
428 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
429 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000430 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000431 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100432 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000433 { "usedns", sUseDNS, SSHCFG_GLOBAL },
434 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
435 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
436 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
437 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000438 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000439 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000440 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Darren Tucker45150472006-07-12 22:34:17 +1000441 { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000442 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000443 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000444 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000445 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100446 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100447 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100448 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
449 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000450 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000451 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100452 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000453 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000454 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455};
456
Darren Tuckere7140f22008-06-10 23:01:51 +1000457static struct {
458 int val;
459 char *text;
460} tunmode_desc[] = {
461 { SSH_TUNMODE_NO, "no" },
462 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
463 { SSH_TUNMODE_ETHERNET, "ethernet" },
464 { SSH_TUNMODE_YES, "yes" },
465 { -1, NULL }
466};
467
Damien Miller5428f641999-11-25 11:54:57 +1100468/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000469 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100470 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471
Damien Miller4af51302000-04-16 11:18:38 +1000472static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100473parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000474 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000476 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477
Damien Miller95def091999-11-25 00:26:21 +1100478 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000479 if (strcasecmp(cp, keywords[i].name) == 0) {
480 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100481 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000482 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000484 error("%s: line %d: Bad configuration option: %s",
485 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100486 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487}
488
Darren Tucker88b6fb22010-01-13 22:44:29 +1100489char *
490derelativise_path(const char *path)
491{
Damien Miller44451d02010-03-26 10:40:04 +1100492 char *expanded, *ret, cwd[MAXPATHLEN];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100493
494 expanded = tilde_expand_filename(path, getuid());
495 if (*expanded == '/')
496 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100497 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100498 fatal("%s: getcwd: %s", __func__, strerror(errno));
499 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100500 xfree(expanded);
501 return ret;
502}
503
Ben Lindstrombba81212001-06-25 05:01:22 +0000504static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100505add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100506{
Damien Millereccb9de2005-06-17 12:59:34 +1000507 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100508
509 if (options->num_ports == 0)
510 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100511 if (options->address_family == -1)
512 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000513 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000514 for (i = 0; i < options->num_ports; i++)
515 add_one_listen_addr(options, addr, options->ports[i]);
516 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000517 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000518}
519
Ben Lindstrombba81212001-06-25 05:01:22 +0000520static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100521add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000522{
523 struct addrinfo hints, *ai, *aitop;
524 char strport[NI_MAXSERV];
525 int gaierr;
526
527 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100528 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000529 hints.ai_socktype = SOCK_STREAM;
530 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100531 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000532 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
533 fatal("bad addr or host: %s (%s)",
534 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100535 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000536 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
537 ;
538 ai->ai_next = options->listen_addrs;
539 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100540}
541
Darren Tucker45150472006-07-12 22:34:17 +1000542/*
543 * The strategy for the Match blocks is that the config file is parsed twice.
544 *
545 * The first time is at startup. activep is initialized to 1 and the
546 * directives in the global context are processed and acted on. Hitting a
547 * Match directive unsets activep and the directives inside the block are
548 * checked for syntax only.
549 *
550 * The second time is after a connection has been established but before
551 * authentication. activep is initialized to 2 and global config directives
552 * are ignored since they have already been processed. If the criteria in a
553 * Match block is met, activep is set and the subsequent directives
554 * processed and actioned until EOF or another Match block unsets it. Any
555 * options set are copied into the main server config.
556 *
557 * Potential additions/improvements:
558 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
559 *
560 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
561 * Match Address 192.168.0.*
562 * Tag trusted
563 * Match Group wheel
564 * Tag trusted
565 * Match Tag trusted
566 * AllowTcpForwarding yes
567 * GatewayPorts clientspecified
568 * [...]
569 *
570 * - Add a PermittedChannelRequests directive
571 * Match Group shell
572 * PermittedChannelRequests session,forwarded-tcpip
573 */
574
575static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000576match_cfg_line_group(const char *grps, int line, const char *user)
577{
578 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000579 struct passwd *pw;
580
Damien Miller565ca3f2006-08-19 00:23:15 +1000581 if (user == NULL)
582 goto out;
583
584 if ((pw = getpwnam(user)) == NULL) {
585 debug("Can't match group at line %d because user %.100s does "
586 "not exist", line, user);
587 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
588 debug("Can't Match group because user %.100s not in any group "
589 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000590 } else if (ga_match_pattern_list(grps) != 1) {
591 debug("user %.100s does not match group list %.100s at line %d",
592 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000593 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000594 debug("user %.100s matched group list %.100s at line %d", user,
595 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000596 result = 1;
597 }
598out:
599 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000600 return result;
601}
602
603static int
Darren Tucker45150472006-07-12 22:34:17 +1000604match_cfg_line(char **condition, int line, const char *user, const char *host,
605 const char *address)
606{
607 int result = 1;
608 char *arg, *attrib, *cp = *condition;
609 size_t len;
610
611 if (user == NULL)
612 debug3("checking syntax for 'Match %s'", cp);
613 else
614 debug3("checking match for '%s' user %s host %s addr %s", cp,
615 user ? user : "(null)", host ? host : "(null)",
616 address ? address : "(null)");
617
618 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
619 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
620 error("Missing Match criteria for %s", attrib);
621 return -1;
622 }
623 len = strlen(arg);
624 if (strcasecmp(attrib, "user") == 0) {
625 if (!user) {
626 result = 0;
627 continue;
628 }
629 if (match_pattern_list(user, arg, len, 0) != 1)
630 result = 0;
631 else
632 debug("user %.100s matched 'User %.100s' at "
633 "line %d", user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000634 } else if (strcasecmp(attrib, "group") == 0) {
635 switch (match_cfg_line_group(arg, line, user)) {
636 case -1:
637 return -1;
638 case 0:
639 result = 0;
640 }
Darren Tucker45150472006-07-12 22:34:17 +1000641 } else if (strcasecmp(attrib, "host") == 0) {
642 if (!host) {
643 result = 0;
644 continue;
645 }
646 if (match_hostname(host, arg, len) != 1)
647 result = 0;
648 else
649 debug("connection from %.100s matched 'Host "
650 "%.100s' at line %d", host, arg, line);
651 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000652 switch (addr_match_list(address, arg)) {
653 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000654 debug("connection from %.100s matched 'Address "
655 "%.100s' at line %d", address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000656 break;
657 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000658 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000659 result = 0;
660 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000661 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000662 return -1;
663 }
Darren Tucker45150472006-07-12 22:34:17 +1000664 } else {
665 error("Unsupported Match attribute %s", attrib);
666 return -1;
667 }
668 }
669 if (user != NULL)
670 debug3("match %sfound", result ? "" : "not ");
671 *condition = cp;
672 return result;
673}
674
Damien Millere2754432006-07-24 14:06:47 +1000675#define WHITESPACE " \t\r\n"
676
Damien Miller33322122011-06-20 14:43:11 +1000677/* Multistate option parsing */
678struct multistate {
679 char *key;
680 int value;
681};
682static const struct multistate multistate_addressfamily[] = {
683 { "inet", AF_INET },
684 { "inet6", AF_INET6 },
685 { "any", AF_UNSPEC },
686 { NULL, -1 }
687};
688static const struct multistate multistate_permitrootlogin[] = {
689 { "without-password", PERMIT_NO_PASSWD },
690 { "forced-commands-only", PERMIT_FORCED_ONLY },
691 { "yes", PERMIT_YES },
692 { "no", PERMIT_NO },
693 { NULL, -1 }
694};
695static const struct multistate multistate_compression[] = {
696 { "delayed", COMP_DELAYED },
697 { "yes", COMP_ZLIB },
698 { "no", COMP_NONE },
699 { NULL, -1 }
700};
701static const struct multistate multistate_gatewayports[] = {
702 { "clientspecified", 2 },
703 { "yes", 1 },
704 { "no", 0 },
705 { NULL, -1 }
706};
Damien Miller69ff1df2011-06-23 08:30:03 +1000707static const struct multistate multistate_privsep[] = {
708 { "sandbox", PRIVSEP_SANDBOX },
709 { "yes", PRIVSEP_ON },
710 { "no", PRIVSEP_OFF },
711 { NULL, -1 }
712};
Damien Miller33322122011-06-20 14:43:11 +1000713
Ben Lindstromade03f62001-12-06 18:22:17 +0000714int
715process_server_config_line(ServerOptions *options, char *line,
Darren Tucker45150472006-07-12 22:34:17 +1000716 const char *filename, int linenum, int *activep, const char *user,
717 const char *host, const char *address)
Ben Lindstromade03f62001-12-06 18:22:17 +0000718{
719 char *cp, **charptr, *arg, *p;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100720 int cmdline = 0, *intptr, value, value2, n;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100721 SyslogFacility *log_facility_ptr;
722 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000723 ServerOpCodes opcode;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100724 int port;
Darren Tucker45150472006-07-12 22:34:17 +1000725 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000726 size_t len;
Damien Miller33322122011-06-20 14:43:11 +1000727 const struct multistate *multistate_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000728
729 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100730 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100731 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000732 /* Ignore leading whitespace */
733 if (*arg == '\0')
734 arg = strdelim(&cp);
735 if (!arg || !*arg || *arg == '#')
736 return 0;
737 intptr = NULL;
738 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000739 opcode = parse_token(arg, filename, linenum, &flags);
740
741 if (activep == NULL) { /* We are processing a command line directive */
742 cmdline = 1;
743 activep = &cmdline;
744 }
745 if (*activep && opcode != sMatch)
746 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
747 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
748 if (user == NULL) {
749 fatal("%s line %d: Directive '%s' is not allowed "
750 "within a Match block", filename, linenum, arg);
751 } else { /* this is a directive we have already processed */
752 while (arg)
753 arg = strdelim(&cp);
754 return 0;
755 }
756 }
757
Ben Lindstromade03f62001-12-06 18:22:17 +0000758 switch (opcode) {
759 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000760 case sUsePAM:
761 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000762 goto parse_flag;
763
764 /* Standard Options */
765 case sBadOption:
766 return -1;
767 case sPort:
768 /* ignore ports from configfile if cmdline specifies ports */
769 if (options->ports_from_cmdline)
770 return 0;
771 if (options->listen_addrs != NULL)
772 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100773 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000774 if (options->num_ports >= MAX_PORTS)
775 fatal("%s line %d: too many ports.",
776 filename, linenum);
777 arg = strdelim(&cp);
778 if (!arg || *arg == '\0')
779 fatal("%s line %d: missing port number.",
780 filename, linenum);
781 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100782 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000783 fatal("%s line %d: Badly formatted port number.",
784 filename, linenum);
785 break;
786
787 case sServerKeyBits:
788 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +1000789 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +0000790 arg = strdelim(&cp);
791 if (!arg || *arg == '\0')
792 fatal("%s line %d: missing integer value.",
793 filename, linenum);
794 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000795 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000796 *intptr = value;
797 break;
798
799 case sLoginGraceTime:
800 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +1000801 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +0000802 arg = strdelim(&cp);
803 if (!arg || *arg == '\0')
804 fatal("%s line %d: missing time value.",
805 filename, linenum);
806 if ((value = convtime(arg)) == -1)
807 fatal("%s line %d: invalid time value.",
808 filename, linenum);
809 if (*intptr == -1)
810 *intptr = value;
811 break;
812
813 case sKeyRegenerationTime:
814 intptr = &options->key_regeneration_time;
815 goto parse_time;
816
817 case sListenAddress:
818 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100819 if (arg == NULL || *arg == '\0')
820 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000821 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000822 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
823 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
824 && strchr(p+1, ':') != NULL) {
825 add_listen_addr(options, arg, 0);
826 break;
827 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100828 p = hpdelim(&arg);
829 if (p == NULL)
830 fatal("%s line %d: bad address:port usage",
831 filename, linenum);
832 p = cleanhostname(p);
833 if (arg == NULL)
834 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100835 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100836 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000837
Damien Millerf91ee4c2005-03-01 21:24:33 +1100838 add_listen_addr(options, p, port);
839
Ben Lindstromade03f62001-12-06 18:22:17 +0000840 break;
841
Darren Tucker0f383232005-01-20 10:57:56 +1100842 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +1000843 intptr = &options->address_family;
844 multistate_ptr = multistate_addressfamily;
845 if (options->listen_addrs != NULL)
846 fatal("%s line %d: address family must be specified "
847 "before ListenAddress.", filename, linenum);
848 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +1100849 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000850 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +1000851 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +1000852 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +1000853 value = -1;
854 for (i = 0; multistate_ptr[i].key != NULL; i++) {
855 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
856 value = multistate_ptr[i].value;
857 break;
858 }
859 }
860 if (value == -1)
861 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +1100862 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +1000863 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +1100864 *intptr = value;
865 break;
866
Ben Lindstromade03f62001-12-06 18:22:17 +0000867 case sHostKeyFile:
868 intptr = &options->num_host_key_files;
869 if (*intptr >= MAX_HOSTKEYS)
870 fatal("%s line %d: too many host keys specified (max %d).",
871 filename, linenum, MAX_HOSTKEYS);
872 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +1000873 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +0000874 arg = strdelim(&cp);
875 if (!arg || *arg == '\0')
876 fatal("%s line %d: missing file name.",
877 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +1000878 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +1100879 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000880 /* increase optional counter */
881 if (intptr != NULL)
882 *intptr = *intptr + 1;
883 }
884 break;
885
Damien Miller0a80ca12010-02-27 07:55:05 +1100886 case sHostCertificate:
887 intptr = &options->num_host_cert_files;
888 if (*intptr >= MAX_HOSTKEYS)
889 fatal("%s line %d: too many host certificates "
890 "specified (max %d).", filename, linenum,
891 MAX_HOSTCERTS);
892 charptr = &options->host_cert_files[*intptr];
893 goto parse_filename;
894 break;
895
Ben Lindstromade03f62001-12-06 18:22:17 +0000896 case sPidFile:
897 charptr = &options->pid_file;
898 goto parse_filename;
899
900 case sPermitRootLogin:
901 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +1000902 multistate_ptr = multistate_permitrootlogin;
903 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +0000904
905 case sIgnoreRhosts:
906 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +1000907 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +0000908 arg = strdelim(&cp);
909 if (!arg || *arg == '\0')
910 fatal("%s line %d: missing yes/no argument.",
911 filename, linenum);
912 value = 0; /* silence compiler */
913 if (strcmp(arg, "yes") == 0)
914 value = 1;
915 else if (strcmp(arg, "no") == 0)
916 value = 0;
917 else
918 fatal("%s line %d: Bad yes/no argument: %s",
919 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +1000920 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000921 *intptr = value;
922 break;
923
924 case sIgnoreUserKnownHosts:
925 intptr = &options->ignore_user_known_hosts;
926 goto parse_flag;
927
Ben Lindstromade03f62001-12-06 18:22:17 +0000928 case sRhostsRSAAuthentication:
929 intptr = &options->rhosts_rsa_authentication;
930 goto parse_flag;
931
932 case sHostbasedAuthentication:
933 intptr = &options->hostbased_authentication;
934 goto parse_flag;
935
936 case sHostbasedUsesNameFromPacketOnly:
937 intptr = &options->hostbased_uses_name_from_packet_only;
938 goto parse_flag;
939
940 case sRSAAuthentication:
941 intptr = &options->rsa_authentication;
942 goto parse_flag;
943
944 case sPubkeyAuthentication:
945 intptr = &options->pubkey_authentication;
946 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000947
Ben Lindstromade03f62001-12-06 18:22:17 +0000948 case sKerberosAuthentication:
949 intptr = &options->kerberos_authentication;
950 goto parse_flag;
951
952 case sKerberosOrLocalPasswd:
953 intptr = &options->kerberos_or_local_passwd;
954 goto parse_flag;
955
956 case sKerberosTicketCleanup:
957 intptr = &options->kerberos_ticket_cleanup;
958 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000959
Darren Tucker22ef5082003-12-31 11:37:34 +1100960 case sKerberosGetAFSToken:
961 intptr = &options->kerberos_get_afs_token;
962 goto parse_flag;
963
Darren Tucker0efd1552003-08-26 11:49:55 +1000964 case sGssAuthentication:
965 intptr = &options->gss_authentication;
966 goto parse_flag;
967
968 case sGssCleanupCreds:
969 intptr = &options->gss_cleanup_creds;
970 goto parse_flag;
971
Ben Lindstromade03f62001-12-06 18:22:17 +0000972 case sPasswordAuthentication:
973 intptr = &options->password_authentication;
974 goto parse_flag;
975
Damien Miller01ed2272008-11-05 16:20:46 +1100976 case sZeroKnowledgePasswordAuthentication:
977 intptr = &options->zero_knowledge_password_authentication;
978 goto parse_flag;
979
Ben Lindstromade03f62001-12-06 18:22:17 +0000980 case sKbdInteractiveAuthentication:
981 intptr = &options->kbd_interactive_authentication;
982 goto parse_flag;
983
984 case sChallengeResponseAuthentication:
985 intptr = &options->challenge_response_authentication;
986 goto parse_flag;
987
988 case sPrintMotd:
989 intptr = &options->print_motd;
990 goto parse_flag;
991
992 case sPrintLastLog:
993 intptr = &options->print_lastlog;
994 goto parse_flag;
995
996 case sX11Forwarding:
997 intptr = &options->x11_forwarding;
998 goto parse_flag;
999
1000 case sX11DisplayOffset:
1001 intptr = &options->x11_display_offset;
1002 goto parse_int;
1003
Damien Miller95c249f2002-02-05 12:11:34 +11001004 case sX11UseLocalhost:
1005 intptr = &options->x11_use_localhost;
1006 goto parse_flag;
1007
Ben Lindstromade03f62001-12-06 18:22:17 +00001008 case sXAuthLocation:
1009 charptr = &options->xauth_location;
1010 goto parse_filename;
1011
1012 case sStrictModes:
1013 intptr = &options->strict_modes;
1014 goto parse_flag;
1015
Damien Miller12c150e2003-12-17 16:31:10 +11001016 case sTCPKeepAlive:
1017 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001018 goto parse_flag;
1019
1020 case sEmptyPasswd:
1021 intptr = &options->permit_empty_passwd;
1022 goto parse_flag;
1023
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001024 case sPermitUserEnvironment:
1025 intptr = &options->permit_user_env;
1026 goto parse_flag;
1027
Ben Lindstromade03f62001-12-06 18:22:17 +00001028 case sUseLogin:
1029 intptr = &options->use_login;
1030 goto parse_flag;
1031
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001032 case sCompression:
1033 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001034 multistate_ptr = multistate_compression;
1035 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001036
Ben Lindstromade03f62001-12-06 18:22:17 +00001037 case sGatewayPorts:
1038 intptr = &options->gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001039 multistate_ptr = multistate_gatewayports;
1040 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001041
Damien Miller3a961dc2003-06-03 10:25:48 +10001042 case sUseDNS:
1043 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001044 goto parse_flag;
1045
1046 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001047 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001048 arg = strdelim(&cp);
1049 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001050 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001051 fatal("%.200s line %d: unsupported log facility '%s'",
1052 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001053 if (*log_facility_ptr == -1)
1054 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001055 break;
1056
1057 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001058 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001059 arg = strdelim(&cp);
1060 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001061 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001062 fatal("%.200s line %d: unsupported log level '%s'",
1063 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001064 if (*log_level_ptr == -1)
1065 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001066 break;
1067
1068 case sAllowTcpForwarding:
1069 intptr = &options->allow_tcp_forwarding;
1070 goto parse_flag;
1071
Damien Miller4f755cd2008-05-19 14:57:41 +10001072 case sAllowAgentForwarding:
1073 intptr = &options->allow_agent_forwarding;
1074 goto parse_flag;
1075
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001076 case sUsePrivilegeSeparation:
1077 intptr = &use_privsep;
Damien Miller69ff1df2011-06-23 08:30:03 +10001078 multistate_ptr = multistate_privsep;
1079 goto parse_multistate;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001080
Ben Lindstromade03f62001-12-06 18:22:17 +00001081 case sAllowUsers:
1082 while ((arg = strdelim(&cp)) && *arg != '\0') {
1083 if (options->num_allow_users >= MAX_ALLOW_USERS)
1084 fatal("%s line %d: too many allow users.",
1085 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001086 options->allow_users[options->num_allow_users++] =
1087 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001088 }
1089 break;
1090
1091 case sDenyUsers:
1092 while ((arg = strdelim(&cp)) && *arg != '\0') {
1093 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001094 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001095 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001096 options->deny_users[options->num_deny_users++] =
1097 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001098 }
1099 break;
1100
1101 case sAllowGroups:
1102 while ((arg = strdelim(&cp)) && *arg != '\0') {
1103 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1104 fatal("%s line %d: too many allow groups.",
1105 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001106 options->allow_groups[options->num_allow_groups++] =
1107 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001108 }
1109 break;
1110
1111 case sDenyGroups:
1112 while ((arg = strdelim(&cp)) && *arg != '\0') {
1113 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1114 fatal("%s line %d: too many deny groups.",
1115 filename, linenum);
1116 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
1117 }
1118 break;
1119
1120 case sCiphers:
1121 arg = strdelim(&cp);
1122 if (!arg || *arg == '\0')
1123 fatal("%s line %d: Missing argument.", filename, linenum);
1124 if (!ciphers_valid(arg))
1125 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1126 filename, linenum, arg ? arg : "<NONE>");
1127 if (options->ciphers == NULL)
1128 options->ciphers = xstrdup(arg);
1129 break;
1130
1131 case sMacs:
1132 arg = strdelim(&cp);
1133 if (!arg || *arg == '\0')
1134 fatal("%s line %d: Missing argument.", filename, linenum);
1135 if (!mac_valid(arg))
1136 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1137 filename, linenum, arg ? arg : "<NONE>");
1138 if (options->macs == NULL)
1139 options->macs = xstrdup(arg);
1140 break;
1141
Damien Millerd5f62bf2010-09-24 22:11:14 +10001142 case sKexAlgorithms:
1143 arg = strdelim(&cp);
1144 if (!arg || *arg == '\0')
1145 fatal("%s line %d: Missing argument.",
1146 filename, linenum);
1147 if (!kex_names_valid(arg))
1148 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1149 filename, linenum, arg ? arg : "<NONE>");
1150 if (options->kex_algorithms == NULL)
1151 options->kex_algorithms = xstrdup(arg);
1152 break;
1153
Ben Lindstromade03f62001-12-06 18:22:17 +00001154 case sProtocol:
1155 intptr = &options->protocol;
1156 arg = strdelim(&cp);
1157 if (!arg || *arg == '\0')
1158 fatal("%s line %d: Missing argument.", filename, linenum);
1159 value = proto_spec(arg);
1160 if (value == SSH_PROTO_UNKNOWN)
1161 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001162 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001163 if (*intptr == SSH_PROTO_UNKNOWN)
1164 *intptr = value;
1165 break;
1166
1167 case sSubsystem:
1168 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1169 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001170 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001171 }
1172 arg = strdelim(&cp);
1173 if (!arg || *arg == '\0')
1174 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001175 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001176 if (!*activep) {
1177 arg = strdelim(&cp);
1178 break;
1179 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001180 for (i = 0; i < options->num_subsystems; i++)
1181 if (strcmp(arg, options->subsystem_name[i]) == 0)
1182 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001183 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001184 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1185 arg = strdelim(&cp);
1186 if (!arg || *arg == '\0')
1187 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001188 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001189 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001190
1191 /* Collect arguments (separate to executable) */
1192 p = xstrdup(arg);
1193 len = strlen(p) + 1;
1194 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1195 len += 1 + strlen(arg);
1196 p = xrealloc(p, 1, len);
1197 strlcat(p, " ", len);
1198 strlcat(p, arg, len);
1199 }
1200 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001201 options->num_subsystems++;
1202 break;
1203
1204 case sMaxStartups:
1205 arg = strdelim(&cp);
1206 if (!arg || *arg == '\0')
1207 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001208 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001209 if ((n = sscanf(arg, "%d:%d:%d",
1210 &options->max_startups_begin,
1211 &options->max_startups_rate,
1212 &options->max_startups)) == 3) {
1213 if (options->max_startups_begin >
1214 options->max_startups ||
1215 options->max_startups_rate > 100 ||
1216 options->max_startups_rate < 1)
1217 fatal("%s line %d: Illegal MaxStartups spec.",
1218 filename, linenum);
1219 } else if (n != 1)
1220 fatal("%s line %d: Illegal MaxStartups spec.",
1221 filename, linenum);
1222 else
1223 options->max_startups = options->max_startups_begin;
1224 break;
1225
Darren Tucker89413db2004-05-24 10:36:23 +10001226 case sMaxAuthTries:
1227 intptr = &options->max_authtries;
1228 goto parse_int;
1229
Damien Miller7207f642008-05-19 15:34:50 +10001230 case sMaxSessions:
1231 intptr = &options->max_sessions;
1232 goto parse_int;
1233
Ben Lindstromade03f62001-12-06 18:22:17 +00001234 case sBanner:
1235 charptr = &options->banner;
1236 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001237
Ben Lindstromade03f62001-12-06 18:22:17 +00001238 /*
1239 * These options can contain %X options expanded at
1240 * connect time, so that you can specify paths like:
1241 *
1242 * AuthorizedKeysFile /etc/ssh_keys/%u
1243 */
1244 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001245 if (*activep && options->num_authkeys_files == 0) {
1246 while ((arg = strdelim(&cp)) && *arg != '\0') {
1247 if (options->num_authkeys_files >=
1248 MAX_AUTHKEYS_FILES)
1249 fatal("%s line %d: "
1250 "too many authorized keys files.",
1251 filename, linenum);
1252 options->authorized_keys_files[
1253 options->num_authkeys_files++] =
1254 tilde_expand_filename(arg, getuid());
1255 }
1256 }
1257 return 0;
1258
Damien Miller30da3442010-05-10 11:58:03 +10001259 case sAuthorizedPrincipalsFile:
1260 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001261 arg = strdelim(&cp);
1262 if (!arg || *arg == '\0')
1263 fatal("%s line %d: missing file name.",
1264 filename, linenum);
1265 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001266 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001267 /* increase optional counter */
1268 if (intptr != NULL)
1269 *intptr = *intptr + 1;
1270 }
1271 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001272
1273 case sClientAliveInterval:
1274 intptr = &options->client_alive_interval;
1275 goto parse_time;
1276
1277 case sClientAliveCountMax:
1278 intptr = &options->client_alive_count_max;
1279 goto parse_int;
1280
Darren Tucker46bc0752004-05-02 22:11:30 +10001281 case sAcceptEnv:
1282 while ((arg = strdelim(&cp)) && *arg != '\0') {
1283 if (strchr(arg, '=') != NULL)
1284 fatal("%s line %d: Invalid environment name.",
1285 filename, linenum);
1286 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1287 fatal("%s line %d: too many allow env.",
1288 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001289 if (!*activep)
1290 break;
Darren Tucker46bc0752004-05-02 22:11:30 +10001291 options->accept_env[options->num_accept_env++] =
1292 xstrdup(arg);
1293 }
1294 break;
1295
Damien Millerd27b9472005-12-13 19:29:02 +11001296 case sPermitTunnel:
1297 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001298 arg = strdelim(&cp);
1299 if (!arg || *arg == '\0')
1300 fatal("%s line %d: Missing yes/point-to-point/"
1301 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001302 value = -1;
1303 for (i = 0; tunmode_desc[i].val != -1; i++)
1304 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1305 value = tunmode_desc[i].val;
1306 break;
1307 }
1308 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001309 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1310 "no argument: %s", filename, linenum, arg);
1311 if (*intptr == -1)
1312 *intptr = value;
1313 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001314
Darren Tucker45150472006-07-12 22:34:17 +10001315 case sMatch:
1316 if (cmdline)
1317 fatal("Match directive not supported as a command-line "
1318 "option");
1319 value = match_cfg_line(&cp, linenum, user, host, address);
1320 if (value < 0)
1321 fatal("%s line %d: Bad Match condition", filename,
1322 linenum);
1323 *activep = value;
1324 break;
1325
Damien Miller9b439df2006-07-24 14:04:00 +10001326 case sPermitOpen:
1327 arg = strdelim(&cp);
1328 if (!arg || *arg == '\0')
1329 fatal("%s line %d: missing PermitOpen specification",
1330 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001331 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001332 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001333 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001334 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001335 options->num_permitted_opens = 0;
1336 }
Damien Miller9b439df2006-07-24 14:04:00 +10001337 break;
1338 }
Damien Millerc6081482012-04-22 11:18:53 +10001339 if (strcmp(arg, "none") == 0) {
1340 if (*activep && n == -1) {
1341 channel_clear_adm_permitted_opens();
1342 options->num_permitted_opens = 1;
1343 channel_disable_adm_local_opens();
1344 }
1345 break;
1346 }
Damien Millera29b95e2007-01-05 16:28:36 +11001347 if (*activep && n == -1)
1348 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001349 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1350 p = hpdelim(&arg);
1351 if (p == NULL)
1352 fatal("%s line %d: missing host in PermitOpen",
1353 filename, linenum);
1354 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001355 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001356 fatal("%s line %d: bad port number in "
1357 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001358 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001359 options->num_permitted_opens =
1360 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001361 }
Damien Miller9b439df2006-07-24 14:04:00 +10001362 break;
1363
Damien Millere2754432006-07-24 14:06:47 +10001364 case sForceCommand:
1365 if (cp == NULL)
1366 fatal("%.200s line %d: Missing argument.", filename,
1367 linenum);
1368 len = strspn(cp, WHITESPACE);
1369 if (*activep && options->adm_forced_command == NULL)
1370 options->adm_forced_command = xstrdup(cp + len);
1371 return 0;
1372
Damien Millerd8cb1f12008-02-10 22:40:12 +11001373 case sChrootDirectory:
1374 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001375
1376 arg = strdelim(&cp);
1377 if (!arg || *arg == '\0')
1378 fatal("%s line %d: missing file name.",
1379 filename, linenum);
1380 if (*activep && *charptr == NULL)
1381 *charptr = xstrdup(arg);
1382 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001383
Damien Miller1aed65e2010-03-04 21:53:35 +11001384 case sTrustedUserCAKeys:
1385 charptr = &options->trusted_user_ca_keys;
1386 goto parse_filename;
1387
1388 case sRevokedKeys:
1389 charptr = &options->revoked_keys_file;
1390 goto parse_filename;
1391
Damien Miller0dac6fb2010-11-20 15:19:38 +11001392 case sIPQoS:
1393 arg = strdelim(&cp);
1394 if ((value = parse_ipqos(arg)) == -1)
1395 fatal("%s line %d: Bad IPQoS value: %s",
1396 filename, linenum, arg);
1397 arg = strdelim(&cp);
1398 if (arg == NULL)
1399 value2 = value;
1400 else if ((value2 = parse_ipqos(arg)) == -1)
1401 fatal("%s line %d: Bad IPQoS value: %s",
1402 filename, linenum, arg);
1403 if (*activep) {
1404 options->ip_qos_interactive = value;
1405 options->ip_qos_bulk = value2;
1406 }
1407 break;
1408
Damien Miller23528812012-04-22 11:24:43 +10001409 case sVersionAddendum:
1410 if (cp == NULL)
1411 fatal("%.200s line %d: Missing argument.", filename,
1412 linenum);
1413 len = strspn(cp, WHITESPACE);
1414 if (*activep && options->version_addendum == NULL) {
1415 if (strcasecmp(cp + len, "none") == 0)
1416 options->version_addendum = xstrdup("");
1417 else if (strchr(cp + len, '\r') != NULL)
1418 fatal("%.200s line %d: Invalid argument",
1419 filename, linenum);
1420 else
1421 options->version_addendum = xstrdup(cp + len);
1422 }
1423 return 0;
1424
Ben Lindstromade03f62001-12-06 18:22:17 +00001425 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001426 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001427 filename, linenum, arg);
1428 while (arg)
1429 arg = strdelim(&cp);
1430 break;
1431
Damien Millerf9b3feb2003-05-16 11:38:32 +10001432 case sUnsupported:
1433 logit("%s line %d: Unsupported option %s",
1434 filename, linenum, arg);
1435 while (arg)
1436 arg = strdelim(&cp);
1437 break;
1438
Ben Lindstromade03f62001-12-06 18:22:17 +00001439 default:
1440 fatal("%s line %d: Missing handler for opcode %s (%d)",
1441 filename, linenum, arg, opcode);
1442 }
1443 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1444 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1445 filename, linenum, arg);
1446 return 0;
1447}
1448
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449/* Reads the server configuration file. */
1450
Damien Miller4af51302000-04-16 11:18:38 +10001451void
Darren Tucker645ab752004-06-25 13:33:20 +10001452load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453{
Darren Tucker645ab752004-06-25 13:33:20 +10001454 char line[1024], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001455 FILE *f;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001456
Darren Tucker645ab752004-06-25 13:33:20 +10001457 debug2("%s: filename %s", __func__, filename);
1458 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001459 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001460 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001461 }
Darren Tucker645ab752004-06-25 13:33:20 +10001462 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001463 while (fgets(line, sizeof(line), f)) {
Darren Tucker645ab752004-06-25 13:33:20 +10001464 /*
1465 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001466 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001467 * line numbers later for error messages
1468 */
1469 if ((cp = strchr(line, '#')) != NULL)
1470 memcpy(cp, "\n", 2);
1471 cp = line + strspn(line, " \t\r");
1472
1473 buffer_append(conf, cp, strlen(cp));
1474 }
1475 buffer_append(conf, "\0", 1);
1476 fclose(f);
1477 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1478}
1479
1480void
Darren Tucker45150472006-07-12 22:34:17 +10001481parse_server_match_config(ServerOptions *options, const char *user,
1482 const char *host, const char *address)
Darren Tucker645ab752004-06-25 13:33:20 +10001483{
Darren Tucker45150472006-07-12 22:34:17 +10001484 ServerOptions mo;
1485
1486 initialize_server_options(&mo);
1487 parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
Darren Tucker1629c072007-02-19 22:25:37 +11001488 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001489}
1490
Darren Tucker1629c072007-02-19 22:25:37 +11001491/* Helper macros */
1492#define M_CP_INTOPT(n) do {\
1493 if (src->n != -1) \
1494 dst->n = src->n; \
1495} while (0)
1496#define M_CP_STROPT(n) do {\
1497 if (src->n != NULL) { \
1498 if (dst->n != NULL) \
1499 xfree(dst->n); \
1500 dst->n = src->n; \
1501 } \
1502} while(0)
Damien Millerd8478b62011-05-29 21:39:36 +10001503#define M_CP_STRARRAYOPT(n, num_n) do {\
1504 if (src->num_n != 0) { \
1505 for (dst->num_n = 0; dst->num_n < src->num_n; dst->num_n++) \
1506 dst->n[dst->num_n] = xstrdup(src->n[dst->num_n]); \
1507 } \
1508} while(0)
Darren Tucker1629c072007-02-19 22:25:37 +11001509
1510/*
1511 * Copy any supported values that are set.
1512 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001513 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001514 * array values that are not used pre-authentication, because any that we
1515 * do use must be explictly sent in mm_getpwnamallow().
1516 */
Darren Tucker45150472006-07-12 22:34:17 +10001517void
Darren Tucker1629c072007-02-19 22:25:37 +11001518copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001519{
Darren Tucker1629c072007-02-19 22:25:37 +11001520 M_CP_INTOPT(password_authentication);
1521 M_CP_INTOPT(gss_authentication);
1522 M_CP_INTOPT(rsa_authentication);
1523 M_CP_INTOPT(pubkey_authentication);
1524 M_CP_INTOPT(kerberos_authentication);
1525 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001526 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001527 M_CP_INTOPT(kbd_interactive_authentication);
Damien Miller01ed2272008-11-05 16:20:46 +11001528 M_CP_INTOPT(zero_knowledge_password_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001529 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001530 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001531
1532 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001533 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001534 M_CP_INTOPT(permit_tun);
Darren Tucker1629c072007-02-19 22:25:37 +11001535 M_CP_INTOPT(gateway_ports);
1536 M_CP_INTOPT(x11_display_offset);
1537 M_CP_INTOPT(x11_forwarding);
1538 M_CP_INTOPT(x11_use_localhost);
Damien Miller7207f642008-05-19 15:34:50 +10001539 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001540 M_CP_INTOPT(max_authtries);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001541 M_CP_INTOPT(ip_qos_interactive);
1542 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker1629c072007-02-19 22:25:37 +11001543
Damien Millerf2e407e2011-05-20 19:04:14 +10001544 /* See comment in servconf.h */
1545 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10001546
Damien Millerc2411902011-05-20 19:03:49 +10001547 /*
1548 * The only things that should be below this point are string options
1549 * which are only used after authentication.
1550 */
Damien Miller5d74e582011-05-20 19:03:31 +10001551 if (preauth)
1552 return;
Damien Millerd8478b62011-05-29 21:39:36 +10001553
Damien Miller5d74e582011-05-20 19:03:31 +10001554 M_CP_STROPT(adm_forced_command);
1555 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001556}
1557
Darren Tucker1629c072007-02-19 22:25:37 +11001558#undef M_CP_INTOPT
1559#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10001560#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11001561
Darren Tucker45150472006-07-12 22:34:17 +10001562void
1563parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1564 const char *user, const char *host, const char *address)
1565{
1566 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001567 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001568
1569 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1570
Darren Tucker9fbac712004-08-12 22:41:44 +10001571 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tucker45150472006-07-12 22:34:17 +10001572 active = user ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001573 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001574 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001575 if (process_server_config_line(options, cp, filename,
Darren Tucker45150472006-07-12 22:34:17 +10001576 linenum++, &active, user, host, address) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001577 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001578 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001579 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001580 if (bad_options > 0)
1581 fatal("%s: terminating, %d bad configuration options",
1582 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001583}
Darren Tuckere7140f22008-06-10 23:01:51 +10001584
1585static const char *
Damien Miller82c55872011-06-23 08:20:30 +10001586fmt_multistate_int(int val, const struct multistate *m)
1587{
1588 u_int i;
1589
1590 for (i = 0; m[i].key != NULL; i++) {
1591 if (m[i].value == val)
1592 return m[i].key;
1593 }
1594 return "UNKNOWN";
1595}
1596
1597static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10001598fmt_intarg(ServerOpCodes code, int val)
1599{
Damien Miller82c55872011-06-23 08:20:30 +10001600 if (val == -1)
1601 return "unset";
1602 switch (code) {
1603 case sAddressFamily:
1604 return fmt_multistate_int(val, multistate_addressfamily);
1605 case sPermitRootLogin:
1606 return fmt_multistate_int(val, multistate_permitrootlogin);
1607 case sGatewayPorts:
1608 return fmt_multistate_int(val, multistate_gatewayports);
1609 case sCompression:
1610 return fmt_multistate_int(val, multistate_compression);
Damien Miller69ff1df2011-06-23 08:30:03 +10001611 case sUsePrivilegeSeparation:
1612 return fmt_multistate_int(val, multistate_privsep);
Damien Miller82c55872011-06-23 08:20:30 +10001613 case sProtocol:
Darren Tuckere7140f22008-06-10 23:01:51 +10001614 switch (val) {
1615 case SSH_PROTO_1:
1616 return "1";
1617 case SSH_PROTO_2:
1618 return "2";
1619 case (SSH_PROTO_1|SSH_PROTO_2):
1620 return "2,1";
1621 default:
1622 return "UNKNOWN";
1623 }
Damien Miller82c55872011-06-23 08:20:30 +10001624 default:
1625 switch (val) {
1626 case 0:
1627 return "no";
1628 case 1:
1629 return "yes";
1630 default:
1631 return "UNKNOWN";
1632 }
Darren Tuckere7140f22008-06-10 23:01:51 +10001633 }
Darren Tuckere7140f22008-06-10 23:01:51 +10001634}
1635
1636static const char *
1637lookup_opcode_name(ServerOpCodes code)
1638{
1639 u_int i;
1640
1641 for (i = 0; keywords[i].name != NULL; i++)
1642 if (keywords[i].opcode == code)
1643 return(keywords[i].name);
1644 return "UNKNOWN";
1645}
1646
1647static void
1648dump_cfg_int(ServerOpCodes code, int val)
1649{
1650 printf("%s %d\n", lookup_opcode_name(code), val);
1651}
1652
1653static void
1654dump_cfg_fmtint(ServerOpCodes code, int val)
1655{
1656 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
1657}
1658
1659static void
1660dump_cfg_string(ServerOpCodes code, const char *val)
1661{
1662 if (val == NULL)
1663 return;
1664 printf("%s %s\n", lookup_opcode_name(code), val);
1665}
1666
1667static void
1668dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
1669{
1670 u_int i;
1671
1672 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10001673 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
1674}
1675
1676static void
1677dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
1678{
1679 u_int i;
1680
1681 printf("%s", lookup_opcode_name(code));
1682 for (i = 0; i < count; i++)
1683 printf(" %s", vals[i]);
1684 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10001685}
1686
1687void
1688dump_config(ServerOptions *o)
1689{
1690 u_int i;
1691 int ret;
1692 struct addrinfo *ai;
1693 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
1694
1695 /* these are usually at the top of the config */
1696 for (i = 0; i < o->num_ports; i++)
1697 printf("port %d\n", o->ports[i]);
1698 dump_cfg_fmtint(sProtocol, o->protocol);
1699 dump_cfg_fmtint(sAddressFamily, o->address_family);
1700
1701 /* ListenAddress must be after Port */
1702 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
1703 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1704 sizeof(addr), port, sizeof(port),
1705 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1706 error("getnameinfo failed: %.100s",
1707 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
1708 strerror(errno));
1709 } else {
1710 if (ai->ai_family == AF_INET6)
1711 printf("listenaddress [%s]:%s\n", addr, port);
1712 else
1713 printf("listenaddress %s:%s\n", addr, port);
1714 }
1715 }
1716
1717 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10001718#ifdef USE_PAM
1719 dump_cfg_int(sUsePAM, o->use_pam);
1720#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001721 dump_cfg_int(sServerKeyBits, o->server_key_bits);
1722 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
1723 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
1724 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
1725 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11001726 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10001727 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
1728 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
1729
1730 /* formatted integer arguments */
1731 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
1732 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
1733 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
1734 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
1735 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
1736 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
1737 o->hostbased_uses_name_from_packet_only);
1738 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
1739 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10001740#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10001741 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
1742 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
1743 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10001744# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10001745 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10001746# endif
1747#endif
1748#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10001749 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
1750 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10001751#endif
Damien Miller01ed2272008-11-05 16:20:46 +11001752#ifdef JPAKE
1753 dump_cfg_fmtint(sZeroKnowledgePasswordAuthentication,
1754 o->zero_knowledge_password_authentication);
1755#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001756 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
1757 dump_cfg_fmtint(sKbdInteractiveAuthentication,
1758 o->kbd_interactive_authentication);
1759 dump_cfg_fmtint(sChallengeResponseAuthentication,
1760 o->challenge_response_authentication);
1761 dump_cfg_fmtint(sPrintMotd, o->print_motd);
1762 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
1763 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
1764 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
1765 dump_cfg_fmtint(sStrictModes, o->strict_modes);
1766 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
1767 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
1768 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
1769 dump_cfg_fmtint(sUseLogin, o->use_login);
1770 dump_cfg_fmtint(sCompression, o->compression);
1771 dump_cfg_fmtint(sGatewayPorts, o->gateway_ports);
1772 dump_cfg_fmtint(sUseDNS, o->use_dns);
1773 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
1774 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
1775
1776 /* string arguments */
1777 dump_cfg_string(sPidFile, o->pid_file);
1778 dump_cfg_string(sXAuthLocation, o->xauth_location);
1779 dump_cfg_string(sCiphers, o->ciphers);
1780 dump_cfg_string(sMacs, o->macs);
1781 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10001782 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11001783 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11001784 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
1785 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10001786 dump_cfg_string(sAuthorizedPrincipalsFile,
1787 o->authorized_principals_file);
Damien Miller23528812012-04-22 11:24:43 +10001788 dump_cfg_string(sVersionAddendum, o->version_addendum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001789
1790 /* string arguments requiring a lookup */
1791 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
1792 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
1793
1794 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10001795 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
1796 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10001797 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
1798 o->host_key_files);
Damien Miller0a80ca12010-02-27 07:55:05 +11001799 dump_cfg_strarray(sHostKeyFile, o->num_host_cert_files,
1800 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10001801 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
1802 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
1803 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
1804 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
1805 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
1806
1807 /* other arguments */
1808 for (i = 0; i < o->num_subsystems; i++)
1809 printf("subsystem %s %s\n", o->subsystem_name[i],
1810 o->subsystem_args[i]);
1811
1812 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
1813 o->max_startups_rate, o->max_startups);
1814
1815 for (i = 0; tunmode_desc[i].val != -1; i++)
1816 if (tunmode_desc[i].val == o->permit_tun) {
1817 s = tunmode_desc[i].text;
1818 break;
1819 }
1820 dump_cfg_string(sPermitTunnel, s);
1821
Damien Miller91475862011-05-05 14:14:34 +10001822 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
1823 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11001824
Darren Tuckere7140f22008-06-10 23:01:51 +10001825 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10001826}