blob: 6de77164e6de3b668010c55306dc8b7e1e9d05e3 [file] [log] [blame]
Damien Millerc6081482012-04-22 11:18:53 +10001/* $OpenBSD: servconf.c,v 1.224 2012/03/29 23:54:36 dtucker 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 Millerd4a8b7e1999-10-27 13:42:43 +1000141}
142
Damien Miller4af51302000-04-16 11:18:38 +1000143void
Damien Miller95def091999-11-25 00:26:21 +1100144fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145{
Damien Miller726273e2001-11-12 11:40:11 +1100146 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000147 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000148 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100149
150 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100151 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +1100152 options->protocol = SSH_PROTO_2;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100153 if (options->num_host_key_files == 0) {
154 /* fill default hostkeys for protocols */
155 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100156 options->host_key_files[options->num_host_key_files++] =
157 _PATH_HOST_KEY_FILE;
158 if (options->protocol & SSH_PROTO_2) {
159 options->host_key_files[options->num_host_key_files++] =
160 _PATH_HOST_RSA_KEY_FILE;
161 options->host_key_files[options->num_host_key_files++] =
162 _PATH_HOST_DSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100163#ifdef OPENSSL_HAS_ECC
Damien Millere13cadf2010-09-10 11:15:33 +1000164 options->host_key_files[options->num_host_key_files++] =
165 _PATH_HOST_ECDSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100166#endif
Damien Miller7fc23732002-01-22 23:19:11 +1100167 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100168 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100169 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100170 if (options->num_ports == 0)
171 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
172 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000173 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000174 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000175 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100176 if (options->server_key_bits == -1)
Darren Tucker7499b0c2008-07-02 22:35:43 +1000177 options->server_key_bits = 1024;
Damien Miller95def091999-11-25 00:26:21 +1100178 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000179 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100180 if (options->key_regeneration_time == -1)
181 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000182 if (options->permit_root_login == PERMIT_NOT_SET)
183 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100184 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100185 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100186 if (options->ignore_user_known_hosts == -1)
187 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100188 if (options->print_motd == -1)
189 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000190 if (options->print_lastlog == -1)
191 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100192 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100193 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100194 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100195 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100196 if (options->x11_use_localhost == -1)
197 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000198 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000199 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100200 if (options->strict_modes == -1)
201 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100202 if (options->tcp_keep_alive == -1)
203 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100204 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100205 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100206 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000207 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100208 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100209 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000210 if (options->hostbased_authentication == -1)
211 options->hostbased_authentication = 0;
212 if (options->hostbased_uses_name_from_packet_only == -1)
213 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100214 if (options->rsa_authentication == -1)
215 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100216 if (options->pubkey_authentication == -1)
217 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100218 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000219 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100220 if (options->kerberos_or_local_passwd == -1)
221 options->kerberos_or_local_passwd = 1;
222 if (options->kerberos_ticket_cleanup == -1)
223 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100224 if (options->kerberos_get_afs_token == -1)
225 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000226 if (options->gss_authentication == -1)
227 options->gss_authentication = 0;
228 if (options->gss_cleanup_creds == -1)
229 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100230 if (options->password_authentication == -1)
231 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100232 if (options->kbd_interactive_authentication == -1)
233 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000234 if (options->challenge_response_authentication == -1)
235 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100236 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100237 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000238 if (options->permit_user_env == -1)
239 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100240 if (options->use_login == -1)
241 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000242 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000243 options->compression = COMP_DELAYED;
Damien Miller50a41ed2000-10-16 12:14:42 +1100244 if (options->allow_tcp_forwarding == -1)
245 options->allow_tcp_forwarding = 1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000246 if (options->allow_agent_forwarding == -1)
247 options->allow_agent_forwarding = 1;
Damien Millere247cc42000-05-07 12:03:14 +1000248 if (options->gateway_ports == -1)
249 options->gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000250 if (options->max_startups == -1)
251 options->max_startups = 10;
Damien Miller942da032000-08-18 13:59:06 +1000252 if (options->max_startups_rate == -1)
253 options->max_startups_rate = 100; /* 100% */
254 if (options->max_startups_begin == -1)
255 options->max_startups_begin = options->max_startups;
Darren Tucker89413db2004-05-24 10:36:23 +1000256 if (options->max_authtries == -1)
257 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000258 if (options->max_sessions == -1)
259 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000260 if (options->use_dns == -1)
261 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000262 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100263 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000264 if (options->client_alive_count_max == -1)
265 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000266 if (options->num_authkeys_files == 0) {
267 options->authorized_keys_files[options->num_authkeys_files++] =
268 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS);
269 options->authorized_keys_files[options->num_authkeys_files++] =
270 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS2);
271 }
Damien Millerd27b9472005-12-13 19:29:02 +1100272 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100273 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller01ed2272008-11-05 16:20:46 +1100274 if (options->zero_knowledge_password_authentication == -1)
275 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100276 if (options->ip_qos_interactive == -1)
277 options->ip_qos_interactive = IPTOS_LOWDELAY;
278 if (options->ip_qos_bulk == -1)
279 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000280
Ben Lindstromfb62a692002-06-06 19:47:11 +0000281 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000282 if (use_privsep == -1)
Damien Miller69ff1df2011-06-23 08:30:03 +1000283 use_privsep = PRIVSEP_ON;
Damien Miller4903eb42002-06-21 16:20:44 +1000284
Tim Rice40017b02002-07-14 13:36:49 -0700285#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000286 if (use_privsep && options->compression == 1) {
287 error("This platform does not support both privilege "
288 "separation and compression");
289 error("Compression disabled");
290 options->compression = 0;
291 }
292#endif
293
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294}
295
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100297typedef enum {
298 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100299 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000300 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100301 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100302 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
303 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000304 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100305 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100306 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000307 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100308 sPasswordAuthentication, sKbdInteractiveAuthentication,
309 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000310 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100311 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller12c150e2003-12-17 16:31:10 +1100312 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000313 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Damien Miller50a41ed2000-10-16 12:14:42 +1100314 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000315 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000316 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
Damien Miller7207f642008-05-19 15:34:50 +1000317 sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000318 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100319 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Damien Millerec2eaa32011-05-20 18:57:14 +1000320 sClientAliveCountMax, sAuthorizedKeysFile,
Damien Millerd27b9472005-12-13 19:29:02 +1100321 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100322 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100323 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller0a80ca12010-02-27 07:55:05 +1100324 sZeroKnowledgePasswordAuthentication, sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000325 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Miller0dac6fb2010-11-20 15:19:38 +1100326 sKexAlgorithms, sIPQoS,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000327 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328} ServerOpCodes;
329
Darren Tucker45150472006-07-12 22:34:17 +1000330#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
331#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
332#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
333
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100335static struct {
336 const char *name;
337 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000338 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100339} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100340 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000341#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000342 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000343#else
Darren Tucker45150472006-07-12 22:34:17 +1000344 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000345#endif
Darren Tucker45150472006-07-12 22:34:17 +1000346 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100347 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000348 { "port", sPort, SSHCFG_GLOBAL },
349 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
350 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
351 { "pidfile", sPidFile, SSHCFG_GLOBAL },
352 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
353 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
354 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100355 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000356 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
357 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
358 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100359 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
360 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000361 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100362 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
363 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000364 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000365#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100366 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000367 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
368 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100369#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000370 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000371#else
Darren Tucker45150472006-07-12 22:34:17 +1000372 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100373#endif
374#else
Darren Tucker1629c072007-02-19 22:25:37 +1100375 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000376 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
377 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
378 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000379#endif
Darren Tucker45150472006-07-12 22:34:17 +1000380 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
381 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000382#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100383 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000384 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000385#else
Darren Tucker1629c072007-02-19 22:25:37 +1100386 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000387 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000388#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100389 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
390 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100391 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000392 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
Damien Miller01ed2272008-11-05 16:20:46 +1100393#ifdef JPAKE
394 { "zeroknowledgepasswordauthentication", sZeroKnowledgePasswordAuthentication, SSHCFG_ALL },
395#else
396 { "zeroknowledgepasswordauthentication", sUnsupported, SSHCFG_ALL },
397#endif
Darren Tucker45150472006-07-12 22:34:17 +1000398 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
399 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
400 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
401 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
402 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
403 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
404 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000405 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
406 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
407 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000408 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
409 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100410 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000411 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
412 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
413 { "compression", sCompression, SSHCFG_GLOBAL },
414 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
415 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
416 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000417 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000418 { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
419 { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
420 { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
421 { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
422 { "ciphers", sCiphers, SSHCFG_GLOBAL },
423 { "macs", sMacs, SSHCFG_GLOBAL },
424 { "protocol", sProtocol, SSHCFG_GLOBAL },
425 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
426 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
427 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000428 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000429 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100430 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000431 { "usedns", sUseDNS, SSHCFG_GLOBAL },
432 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
433 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
434 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
435 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000436 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000437 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000438 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Darren Tucker45150472006-07-12 22:34:17 +1000439 { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000440 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000441 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000442 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000443 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100444 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100445 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100446 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
447 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000448 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000449 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100450 { "ipqos", sIPQoS, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000451 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452};
453
Darren Tuckere7140f22008-06-10 23:01:51 +1000454static struct {
455 int val;
456 char *text;
457} tunmode_desc[] = {
458 { SSH_TUNMODE_NO, "no" },
459 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
460 { SSH_TUNMODE_ETHERNET, "ethernet" },
461 { SSH_TUNMODE_YES, "yes" },
462 { -1, NULL }
463};
464
Damien Miller5428f641999-11-25 11:54:57 +1100465/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000466 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100467 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468
Damien Miller4af51302000-04-16 11:18:38 +1000469static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100470parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000471 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000473 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474
Damien Miller95def091999-11-25 00:26:21 +1100475 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000476 if (strcasecmp(cp, keywords[i].name) == 0) {
477 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100478 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000479 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000481 error("%s: line %d: Bad configuration option: %s",
482 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100483 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484}
485
Darren Tucker88b6fb22010-01-13 22:44:29 +1100486char *
487derelativise_path(const char *path)
488{
Damien Miller44451d02010-03-26 10:40:04 +1100489 char *expanded, *ret, cwd[MAXPATHLEN];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100490
491 expanded = tilde_expand_filename(path, getuid());
492 if (*expanded == '/')
493 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100494 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100495 fatal("%s: getcwd: %s", __func__, strerror(errno));
496 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100497 xfree(expanded);
498 return ret;
499}
500
Ben Lindstrombba81212001-06-25 05:01:22 +0000501static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100502add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100503{
Damien Millereccb9de2005-06-17 12:59:34 +1000504 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100505
506 if (options->num_ports == 0)
507 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100508 if (options->address_family == -1)
509 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000510 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000511 for (i = 0; i < options->num_ports; i++)
512 add_one_listen_addr(options, addr, options->ports[i]);
513 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000514 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000515}
516
Ben Lindstrombba81212001-06-25 05:01:22 +0000517static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100518add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000519{
520 struct addrinfo hints, *ai, *aitop;
521 char strport[NI_MAXSERV];
522 int gaierr;
523
524 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100525 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000526 hints.ai_socktype = SOCK_STREAM;
527 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100528 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000529 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
530 fatal("bad addr or host: %s (%s)",
531 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100532 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000533 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
534 ;
535 ai->ai_next = options->listen_addrs;
536 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100537}
538
Darren Tucker45150472006-07-12 22:34:17 +1000539/*
540 * The strategy for the Match blocks is that the config file is parsed twice.
541 *
542 * The first time is at startup. activep is initialized to 1 and the
543 * directives in the global context are processed and acted on. Hitting a
544 * Match directive unsets activep and the directives inside the block are
545 * checked for syntax only.
546 *
547 * The second time is after a connection has been established but before
548 * authentication. activep is initialized to 2 and global config directives
549 * are ignored since they have already been processed. If the criteria in a
550 * Match block is met, activep is set and the subsequent directives
551 * processed and actioned until EOF or another Match block unsets it. Any
552 * options set are copied into the main server config.
553 *
554 * Potential additions/improvements:
555 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
556 *
557 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
558 * Match Address 192.168.0.*
559 * Tag trusted
560 * Match Group wheel
561 * Tag trusted
562 * Match Tag trusted
563 * AllowTcpForwarding yes
564 * GatewayPorts clientspecified
565 * [...]
566 *
567 * - Add a PermittedChannelRequests directive
568 * Match Group shell
569 * PermittedChannelRequests session,forwarded-tcpip
570 */
571
572static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000573match_cfg_line_group(const char *grps, int line, const char *user)
574{
575 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000576 struct passwd *pw;
577
Damien Miller565ca3f2006-08-19 00:23:15 +1000578 if (user == NULL)
579 goto out;
580
581 if ((pw = getpwnam(user)) == NULL) {
582 debug("Can't match group at line %d because user %.100s does "
583 "not exist", line, user);
584 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
585 debug("Can't Match group because user %.100s not in any group "
586 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000587 } else if (ga_match_pattern_list(grps) != 1) {
588 debug("user %.100s does not match group list %.100s at line %d",
589 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000590 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000591 debug("user %.100s matched group list %.100s at line %d", user,
592 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000593 result = 1;
594 }
595out:
596 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000597 return result;
598}
599
600static int
Darren Tucker45150472006-07-12 22:34:17 +1000601match_cfg_line(char **condition, int line, const char *user, const char *host,
602 const char *address)
603{
604 int result = 1;
605 char *arg, *attrib, *cp = *condition;
606 size_t len;
607
608 if (user == NULL)
609 debug3("checking syntax for 'Match %s'", cp);
610 else
611 debug3("checking match for '%s' user %s host %s addr %s", cp,
612 user ? user : "(null)", host ? host : "(null)",
613 address ? address : "(null)");
614
615 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
616 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
617 error("Missing Match criteria for %s", attrib);
618 return -1;
619 }
620 len = strlen(arg);
621 if (strcasecmp(attrib, "user") == 0) {
622 if (!user) {
623 result = 0;
624 continue;
625 }
626 if (match_pattern_list(user, arg, len, 0) != 1)
627 result = 0;
628 else
629 debug("user %.100s matched 'User %.100s' at "
630 "line %d", user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000631 } else if (strcasecmp(attrib, "group") == 0) {
632 switch (match_cfg_line_group(arg, line, user)) {
633 case -1:
634 return -1;
635 case 0:
636 result = 0;
637 }
Darren Tucker45150472006-07-12 22:34:17 +1000638 } else if (strcasecmp(attrib, "host") == 0) {
639 if (!host) {
640 result = 0;
641 continue;
642 }
643 if (match_hostname(host, arg, len) != 1)
644 result = 0;
645 else
646 debug("connection from %.100s matched 'Host "
647 "%.100s' at line %d", host, arg, line);
648 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000649 switch (addr_match_list(address, arg)) {
650 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000651 debug("connection from %.100s matched 'Address "
652 "%.100s' at line %d", address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000653 break;
654 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000655 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000656 result = 0;
657 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000658 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000659 return -1;
660 }
Darren Tucker45150472006-07-12 22:34:17 +1000661 } else {
662 error("Unsupported Match attribute %s", attrib);
663 return -1;
664 }
665 }
666 if (user != NULL)
667 debug3("match %sfound", result ? "" : "not ");
668 *condition = cp;
669 return result;
670}
671
Damien Millere2754432006-07-24 14:06:47 +1000672#define WHITESPACE " \t\r\n"
673
Damien Miller33322122011-06-20 14:43:11 +1000674/* Multistate option parsing */
675struct multistate {
676 char *key;
677 int value;
678};
679static const struct multistate multistate_addressfamily[] = {
680 { "inet", AF_INET },
681 { "inet6", AF_INET6 },
682 { "any", AF_UNSPEC },
683 { NULL, -1 }
684};
685static const struct multistate multistate_permitrootlogin[] = {
686 { "without-password", PERMIT_NO_PASSWD },
687 { "forced-commands-only", PERMIT_FORCED_ONLY },
688 { "yes", PERMIT_YES },
689 { "no", PERMIT_NO },
690 { NULL, -1 }
691};
692static const struct multistate multistate_compression[] = {
693 { "delayed", COMP_DELAYED },
694 { "yes", COMP_ZLIB },
695 { "no", COMP_NONE },
696 { NULL, -1 }
697};
698static const struct multistate multistate_gatewayports[] = {
699 { "clientspecified", 2 },
700 { "yes", 1 },
701 { "no", 0 },
702 { NULL, -1 }
703};
Damien Miller69ff1df2011-06-23 08:30:03 +1000704static const struct multistate multistate_privsep[] = {
705 { "sandbox", PRIVSEP_SANDBOX },
706 { "yes", PRIVSEP_ON },
707 { "no", PRIVSEP_OFF },
708 { NULL, -1 }
709};
Damien Miller33322122011-06-20 14:43:11 +1000710
Ben Lindstromade03f62001-12-06 18:22:17 +0000711int
712process_server_config_line(ServerOptions *options, char *line,
Darren Tucker45150472006-07-12 22:34:17 +1000713 const char *filename, int linenum, int *activep, const char *user,
714 const char *host, const char *address)
Ben Lindstromade03f62001-12-06 18:22:17 +0000715{
716 char *cp, **charptr, *arg, *p;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100717 int cmdline = 0, *intptr, value, value2, n;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100718 SyslogFacility *log_facility_ptr;
719 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000720 ServerOpCodes opcode;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100721 int port;
Darren Tucker45150472006-07-12 22:34:17 +1000722 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000723 size_t len;
Damien Miller33322122011-06-20 14:43:11 +1000724 const struct multistate *multistate_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000725
726 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100727 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100728 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000729 /* Ignore leading whitespace */
730 if (*arg == '\0')
731 arg = strdelim(&cp);
732 if (!arg || !*arg || *arg == '#')
733 return 0;
734 intptr = NULL;
735 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000736 opcode = parse_token(arg, filename, linenum, &flags);
737
738 if (activep == NULL) { /* We are processing a command line directive */
739 cmdline = 1;
740 activep = &cmdline;
741 }
742 if (*activep && opcode != sMatch)
743 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
744 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
745 if (user == NULL) {
746 fatal("%s line %d: Directive '%s' is not allowed "
747 "within a Match block", filename, linenum, arg);
748 } else { /* this is a directive we have already processed */
749 while (arg)
750 arg = strdelim(&cp);
751 return 0;
752 }
753 }
754
Ben Lindstromade03f62001-12-06 18:22:17 +0000755 switch (opcode) {
756 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000757 case sUsePAM:
758 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000759 goto parse_flag;
760
761 /* Standard Options */
762 case sBadOption:
763 return -1;
764 case sPort:
765 /* ignore ports from configfile if cmdline specifies ports */
766 if (options->ports_from_cmdline)
767 return 0;
768 if (options->listen_addrs != NULL)
769 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100770 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000771 if (options->num_ports >= MAX_PORTS)
772 fatal("%s line %d: too many ports.",
773 filename, linenum);
774 arg = strdelim(&cp);
775 if (!arg || *arg == '\0')
776 fatal("%s line %d: missing port number.",
777 filename, linenum);
778 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100779 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000780 fatal("%s line %d: Badly formatted port number.",
781 filename, linenum);
782 break;
783
784 case sServerKeyBits:
785 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +1000786 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +0000787 arg = strdelim(&cp);
788 if (!arg || *arg == '\0')
789 fatal("%s line %d: missing integer value.",
790 filename, linenum);
791 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000792 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000793 *intptr = value;
794 break;
795
796 case sLoginGraceTime:
797 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +1000798 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +0000799 arg = strdelim(&cp);
800 if (!arg || *arg == '\0')
801 fatal("%s line %d: missing time value.",
802 filename, linenum);
803 if ((value = convtime(arg)) == -1)
804 fatal("%s line %d: invalid time value.",
805 filename, linenum);
806 if (*intptr == -1)
807 *intptr = value;
808 break;
809
810 case sKeyRegenerationTime:
811 intptr = &options->key_regeneration_time;
812 goto parse_time;
813
814 case sListenAddress:
815 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100816 if (arg == NULL || *arg == '\0')
817 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000818 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000819 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
820 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
821 && strchr(p+1, ':') != NULL) {
822 add_listen_addr(options, arg, 0);
823 break;
824 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100825 p = hpdelim(&arg);
826 if (p == NULL)
827 fatal("%s line %d: bad address:port usage",
828 filename, linenum);
829 p = cleanhostname(p);
830 if (arg == NULL)
831 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100832 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100833 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000834
Damien Millerf91ee4c2005-03-01 21:24:33 +1100835 add_listen_addr(options, p, port);
836
Ben Lindstromade03f62001-12-06 18:22:17 +0000837 break;
838
Darren Tucker0f383232005-01-20 10:57:56 +1100839 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +1000840 intptr = &options->address_family;
841 multistate_ptr = multistate_addressfamily;
842 if (options->listen_addrs != NULL)
843 fatal("%s line %d: address family must be specified "
844 "before ListenAddress.", filename, linenum);
845 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +1100846 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000847 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +1000848 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +1000849 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +1000850 value = -1;
851 for (i = 0; multistate_ptr[i].key != NULL; i++) {
852 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
853 value = multistate_ptr[i].value;
854 break;
855 }
856 }
857 if (value == -1)
858 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +1100859 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +1000860 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +1100861 *intptr = value;
862 break;
863
Ben Lindstromade03f62001-12-06 18:22:17 +0000864 case sHostKeyFile:
865 intptr = &options->num_host_key_files;
866 if (*intptr >= MAX_HOSTKEYS)
867 fatal("%s line %d: too many host keys specified (max %d).",
868 filename, linenum, MAX_HOSTKEYS);
869 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +1000870 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +0000871 arg = strdelim(&cp);
872 if (!arg || *arg == '\0')
873 fatal("%s line %d: missing file name.",
874 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +1000875 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +1100876 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000877 /* increase optional counter */
878 if (intptr != NULL)
879 *intptr = *intptr + 1;
880 }
881 break;
882
Damien Miller0a80ca12010-02-27 07:55:05 +1100883 case sHostCertificate:
884 intptr = &options->num_host_cert_files;
885 if (*intptr >= MAX_HOSTKEYS)
886 fatal("%s line %d: too many host certificates "
887 "specified (max %d).", filename, linenum,
888 MAX_HOSTCERTS);
889 charptr = &options->host_cert_files[*intptr];
890 goto parse_filename;
891 break;
892
Ben Lindstromade03f62001-12-06 18:22:17 +0000893 case sPidFile:
894 charptr = &options->pid_file;
895 goto parse_filename;
896
897 case sPermitRootLogin:
898 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +1000899 multistate_ptr = multistate_permitrootlogin;
900 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +0000901
902 case sIgnoreRhosts:
903 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +1000904 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +0000905 arg = strdelim(&cp);
906 if (!arg || *arg == '\0')
907 fatal("%s line %d: missing yes/no argument.",
908 filename, linenum);
909 value = 0; /* silence compiler */
910 if (strcmp(arg, "yes") == 0)
911 value = 1;
912 else if (strcmp(arg, "no") == 0)
913 value = 0;
914 else
915 fatal("%s line %d: Bad yes/no argument: %s",
916 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +1000917 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000918 *intptr = value;
919 break;
920
921 case sIgnoreUserKnownHosts:
922 intptr = &options->ignore_user_known_hosts;
923 goto parse_flag;
924
Ben Lindstromade03f62001-12-06 18:22:17 +0000925 case sRhostsRSAAuthentication:
926 intptr = &options->rhosts_rsa_authentication;
927 goto parse_flag;
928
929 case sHostbasedAuthentication:
930 intptr = &options->hostbased_authentication;
931 goto parse_flag;
932
933 case sHostbasedUsesNameFromPacketOnly:
934 intptr = &options->hostbased_uses_name_from_packet_only;
935 goto parse_flag;
936
937 case sRSAAuthentication:
938 intptr = &options->rsa_authentication;
939 goto parse_flag;
940
941 case sPubkeyAuthentication:
942 intptr = &options->pubkey_authentication;
943 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000944
Ben Lindstromade03f62001-12-06 18:22:17 +0000945 case sKerberosAuthentication:
946 intptr = &options->kerberos_authentication;
947 goto parse_flag;
948
949 case sKerberosOrLocalPasswd:
950 intptr = &options->kerberos_or_local_passwd;
951 goto parse_flag;
952
953 case sKerberosTicketCleanup:
954 intptr = &options->kerberos_ticket_cleanup;
955 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000956
Darren Tucker22ef5082003-12-31 11:37:34 +1100957 case sKerberosGetAFSToken:
958 intptr = &options->kerberos_get_afs_token;
959 goto parse_flag;
960
Darren Tucker0efd1552003-08-26 11:49:55 +1000961 case sGssAuthentication:
962 intptr = &options->gss_authentication;
963 goto parse_flag;
964
965 case sGssCleanupCreds:
966 intptr = &options->gss_cleanup_creds;
967 goto parse_flag;
968
Ben Lindstromade03f62001-12-06 18:22:17 +0000969 case sPasswordAuthentication:
970 intptr = &options->password_authentication;
971 goto parse_flag;
972
Damien Miller01ed2272008-11-05 16:20:46 +1100973 case sZeroKnowledgePasswordAuthentication:
974 intptr = &options->zero_knowledge_password_authentication;
975 goto parse_flag;
976
Ben Lindstromade03f62001-12-06 18:22:17 +0000977 case sKbdInteractiveAuthentication:
978 intptr = &options->kbd_interactive_authentication;
979 goto parse_flag;
980
981 case sChallengeResponseAuthentication:
982 intptr = &options->challenge_response_authentication;
983 goto parse_flag;
984
985 case sPrintMotd:
986 intptr = &options->print_motd;
987 goto parse_flag;
988
989 case sPrintLastLog:
990 intptr = &options->print_lastlog;
991 goto parse_flag;
992
993 case sX11Forwarding:
994 intptr = &options->x11_forwarding;
995 goto parse_flag;
996
997 case sX11DisplayOffset:
998 intptr = &options->x11_display_offset;
999 goto parse_int;
1000
Damien Miller95c249f2002-02-05 12:11:34 +11001001 case sX11UseLocalhost:
1002 intptr = &options->x11_use_localhost;
1003 goto parse_flag;
1004
Ben Lindstromade03f62001-12-06 18:22:17 +00001005 case sXAuthLocation:
1006 charptr = &options->xauth_location;
1007 goto parse_filename;
1008
1009 case sStrictModes:
1010 intptr = &options->strict_modes;
1011 goto parse_flag;
1012
Damien Miller12c150e2003-12-17 16:31:10 +11001013 case sTCPKeepAlive:
1014 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001015 goto parse_flag;
1016
1017 case sEmptyPasswd:
1018 intptr = &options->permit_empty_passwd;
1019 goto parse_flag;
1020
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001021 case sPermitUserEnvironment:
1022 intptr = &options->permit_user_env;
1023 goto parse_flag;
1024
Ben Lindstromade03f62001-12-06 18:22:17 +00001025 case sUseLogin:
1026 intptr = &options->use_login;
1027 goto parse_flag;
1028
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001029 case sCompression:
1030 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001031 multistate_ptr = multistate_compression;
1032 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001033
Ben Lindstromade03f62001-12-06 18:22:17 +00001034 case sGatewayPorts:
1035 intptr = &options->gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001036 multistate_ptr = multistate_gatewayports;
1037 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001038
Damien Miller3a961dc2003-06-03 10:25:48 +10001039 case sUseDNS:
1040 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001041 goto parse_flag;
1042
1043 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001044 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001045 arg = strdelim(&cp);
1046 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001047 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001048 fatal("%.200s line %d: unsupported log facility '%s'",
1049 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001050 if (*log_facility_ptr == -1)
1051 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001052 break;
1053
1054 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001055 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001056 arg = strdelim(&cp);
1057 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001058 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001059 fatal("%.200s line %d: unsupported log level '%s'",
1060 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001061 if (*log_level_ptr == -1)
1062 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001063 break;
1064
1065 case sAllowTcpForwarding:
1066 intptr = &options->allow_tcp_forwarding;
1067 goto parse_flag;
1068
Damien Miller4f755cd2008-05-19 14:57:41 +10001069 case sAllowAgentForwarding:
1070 intptr = &options->allow_agent_forwarding;
1071 goto parse_flag;
1072
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001073 case sUsePrivilegeSeparation:
1074 intptr = &use_privsep;
Damien Miller69ff1df2011-06-23 08:30:03 +10001075 multistate_ptr = multistate_privsep;
1076 goto parse_multistate;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001077
Ben Lindstromade03f62001-12-06 18:22:17 +00001078 case sAllowUsers:
1079 while ((arg = strdelim(&cp)) && *arg != '\0') {
1080 if (options->num_allow_users >= MAX_ALLOW_USERS)
1081 fatal("%s line %d: too many allow users.",
1082 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001083 options->allow_users[options->num_allow_users++] =
1084 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001085 }
1086 break;
1087
1088 case sDenyUsers:
1089 while ((arg = strdelim(&cp)) && *arg != '\0') {
1090 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001091 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001092 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001093 options->deny_users[options->num_deny_users++] =
1094 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001095 }
1096 break;
1097
1098 case sAllowGroups:
1099 while ((arg = strdelim(&cp)) && *arg != '\0') {
1100 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1101 fatal("%s line %d: too many allow groups.",
1102 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001103 options->allow_groups[options->num_allow_groups++] =
1104 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001105 }
1106 break;
1107
1108 case sDenyGroups:
1109 while ((arg = strdelim(&cp)) && *arg != '\0') {
1110 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1111 fatal("%s line %d: too many deny groups.",
1112 filename, linenum);
1113 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
1114 }
1115 break;
1116
1117 case sCiphers:
1118 arg = strdelim(&cp);
1119 if (!arg || *arg == '\0')
1120 fatal("%s line %d: Missing argument.", filename, linenum);
1121 if (!ciphers_valid(arg))
1122 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1123 filename, linenum, arg ? arg : "<NONE>");
1124 if (options->ciphers == NULL)
1125 options->ciphers = xstrdup(arg);
1126 break;
1127
1128 case sMacs:
1129 arg = strdelim(&cp);
1130 if (!arg || *arg == '\0')
1131 fatal("%s line %d: Missing argument.", filename, linenum);
1132 if (!mac_valid(arg))
1133 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1134 filename, linenum, arg ? arg : "<NONE>");
1135 if (options->macs == NULL)
1136 options->macs = xstrdup(arg);
1137 break;
1138
Damien Millerd5f62bf2010-09-24 22:11:14 +10001139 case sKexAlgorithms:
1140 arg = strdelim(&cp);
1141 if (!arg || *arg == '\0')
1142 fatal("%s line %d: Missing argument.",
1143 filename, linenum);
1144 if (!kex_names_valid(arg))
1145 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1146 filename, linenum, arg ? arg : "<NONE>");
1147 if (options->kex_algorithms == NULL)
1148 options->kex_algorithms = xstrdup(arg);
1149 break;
1150
Ben Lindstromade03f62001-12-06 18:22:17 +00001151 case sProtocol:
1152 intptr = &options->protocol;
1153 arg = strdelim(&cp);
1154 if (!arg || *arg == '\0')
1155 fatal("%s line %d: Missing argument.", filename, linenum);
1156 value = proto_spec(arg);
1157 if (value == SSH_PROTO_UNKNOWN)
1158 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001159 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001160 if (*intptr == SSH_PROTO_UNKNOWN)
1161 *intptr = value;
1162 break;
1163
1164 case sSubsystem:
1165 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1166 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001167 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001168 }
1169 arg = strdelim(&cp);
1170 if (!arg || *arg == '\0')
1171 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001172 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001173 if (!*activep) {
1174 arg = strdelim(&cp);
1175 break;
1176 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001177 for (i = 0; i < options->num_subsystems; i++)
1178 if (strcmp(arg, options->subsystem_name[i]) == 0)
1179 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001180 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001181 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1182 arg = strdelim(&cp);
1183 if (!arg || *arg == '\0')
1184 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001185 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001186 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001187
1188 /* Collect arguments (separate to executable) */
1189 p = xstrdup(arg);
1190 len = strlen(p) + 1;
1191 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1192 len += 1 + strlen(arg);
1193 p = xrealloc(p, 1, len);
1194 strlcat(p, " ", len);
1195 strlcat(p, arg, len);
1196 }
1197 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001198 options->num_subsystems++;
1199 break;
1200
1201 case sMaxStartups:
1202 arg = strdelim(&cp);
1203 if (!arg || *arg == '\0')
1204 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001205 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001206 if ((n = sscanf(arg, "%d:%d:%d",
1207 &options->max_startups_begin,
1208 &options->max_startups_rate,
1209 &options->max_startups)) == 3) {
1210 if (options->max_startups_begin >
1211 options->max_startups ||
1212 options->max_startups_rate > 100 ||
1213 options->max_startups_rate < 1)
1214 fatal("%s line %d: Illegal MaxStartups spec.",
1215 filename, linenum);
1216 } else if (n != 1)
1217 fatal("%s line %d: Illegal MaxStartups spec.",
1218 filename, linenum);
1219 else
1220 options->max_startups = options->max_startups_begin;
1221 break;
1222
Darren Tucker89413db2004-05-24 10:36:23 +10001223 case sMaxAuthTries:
1224 intptr = &options->max_authtries;
1225 goto parse_int;
1226
Damien Miller7207f642008-05-19 15:34:50 +10001227 case sMaxSessions:
1228 intptr = &options->max_sessions;
1229 goto parse_int;
1230
Ben Lindstromade03f62001-12-06 18:22:17 +00001231 case sBanner:
1232 charptr = &options->banner;
1233 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001234
Ben Lindstromade03f62001-12-06 18:22:17 +00001235 /*
1236 * These options can contain %X options expanded at
1237 * connect time, so that you can specify paths like:
1238 *
1239 * AuthorizedKeysFile /etc/ssh_keys/%u
1240 */
1241 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001242 if (*activep && options->num_authkeys_files == 0) {
1243 while ((arg = strdelim(&cp)) && *arg != '\0') {
1244 if (options->num_authkeys_files >=
1245 MAX_AUTHKEYS_FILES)
1246 fatal("%s line %d: "
1247 "too many authorized keys files.",
1248 filename, linenum);
1249 options->authorized_keys_files[
1250 options->num_authkeys_files++] =
1251 tilde_expand_filename(arg, getuid());
1252 }
1253 }
1254 return 0;
1255
Damien Miller30da3442010-05-10 11:58:03 +10001256 case sAuthorizedPrincipalsFile:
1257 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001258 arg = strdelim(&cp);
1259 if (!arg || *arg == '\0')
1260 fatal("%s line %d: missing file name.",
1261 filename, linenum);
1262 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001263 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001264 /* increase optional counter */
1265 if (intptr != NULL)
1266 *intptr = *intptr + 1;
1267 }
1268 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001269
1270 case sClientAliveInterval:
1271 intptr = &options->client_alive_interval;
1272 goto parse_time;
1273
1274 case sClientAliveCountMax:
1275 intptr = &options->client_alive_count_max;
1276 goto parse_int;
1277
Darren Tucker46bc0752004-05-02 22:11:30 +10001278 case sAcceptEnv:
1279 while ((arg = strdelim(&cp)) && *arg != '\0') {
1280 if (strchr(arg, '=') != NULL)
1281 fatal("%s line %d: Invalid environment name.",
1282 filename, linenum);
1283 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1284 fatal("%s line %d: too many allow env.",
1285 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001286 if (!*activep)
1287 break;
Darren Tucker46bc0752004-05-02 22:11:30 +10001288 options->accept_env[options->num_accept_env++] =
1289 xstrdup(arg);
1290 }
1291 break;
1292
Damien Millerd27b9472005-12-13 19:29:02 +11001293 case sPermitTunnel:
1294 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001295 arg = strdelim(&cp);
1296 if (!arg || *arg == '\0')
1297 fatal("%s line %d: Missing yes/point-to-point/"
1298 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001299 value = -1;
1300 for (i = 0; tunmode_desc[i].val != -1; i++)
1301 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1302 value = tunmode_desc[i].val;
1303 break;
1304 }
1305 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001306 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1307 "no argument: %s", filename, linenum, arg);
1308 if (*intptr == -1)
1309 *intptr = value;
1310 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001311
Darren Tucker45150472006-07-12 22:34:17 +10001312 case sMatch:
1313 if (cmdline)
1314 fatal("Match directive not supported as a command-line "
1315 "option");
1316 value = match_cfg_line(&cp, linenum, user, host, address);
1317 if (value < 0)
1318 fatal("%s line %d: Bad Match condition", filename,
1319 linenum);
1320 *activep = value;
1321 break;
1322
Damien Miller9b439df2006-07-24 14:04:00 +10001323 case sPermitOpen:
1324 arg = strdelim(&cp);
1325 if (!arg || *arg == '\0')
1326 fatal("%s line %d: missing PermitOpen specification",
1327 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001328 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001329 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001330 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001331 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001332 options->num_permitted_opens = 0;
1333 }
Damien Miller9b439df2006-07-24 14:04:00 +10001334 break;
1335 }
Damien Millerc6081482012-04-22 11:18:53 +10001336 if (strcmp(arg, "none") == 0) {
1337 if (*activep && n == -1) {
1338 channel_clear_adm_permitted_opens();
1339 options->num_permitted_opens = 1;
1340 channel_disable_adm_local_opens();
1341 }
1342 break;
1343 }
Damien Millera29b95e2007-01-05 16:28:36 +11001344 if (*activep && n == -1)
1345 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001346 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1347 p = hpdelim(&arg);
1348 if (p == NULL)
1349 fatal("%s line %d: missing host in PermitOpen",
1350 filename, linenum);
1351 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001352 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001353 fatal("%s line %d: bad port number in "
1354 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001355 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001356 options->num_permitted_opens =
1357 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001358 }
Damien Miller9b439df2006-07-24 14:04:00 +10001359 break;
1360
Damien Millere2754432006-07-24 14:06:47 +10001361 case sForceCommand:
1362 if (cp == NULL)
1363 fatal("%.200s line %d: Missing argument.", filename,
1364 linenum);
1365 len = strspn(cp, WHITESPACE);
1366 if (*activep && options->adm_forced_command == NULL)
1367 options->adm_forced_command = xstrdup(cp + len);
1368 return 0;
1369
Damien Millerd8cb1f12008-02-10 22:40:12 +11001370 case sChrootDirectory:
1371 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001372
1373 arg = strdelim(&cp);
1374 if (!arg || *arg == '\0')
1375 fatal("%s line %d: missing file name.",
1376 filename, linenum);
1377 if (*activep && *charptr == NULL)
1378 *charptr = xstrdup(arg);
1379 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001380
Damien Miller1aed65e2010-03-04 21:53:35 +11001381 case sTrustedUserCAKeys:
1382 charptr = &options->trusted_user_ca_keys;
1383 goto parse_filename;
1384
1385 case sRevokedKeys:
1386 charptr = &options->revoked_keys_file;
1387 goto parse_filename;
1388
Damien Miller0dac6fb2010-11-20 15:19:38 +11001389 case sIPQoS:
1390 arg = strdelim(&cp);
1391 if ((value = parse_ipqos(arg)) == -1)
1392 fatal("%s line %d: Bad IPQoS value: %s",
1393 filename, linenum, arg);
1394 arg = strdelim(&cp);
1395 if (arg == NULL)
1396 value2 = value;
1397 else if ((value2 = parse_ipqos(arg)) == -1)
1398 fatal("%s line %d: Bad IPQoS value: %s",
1399 filename, linenum, arg);
1400 if (*activep) {
1401 options->ip_qos_interactive = value;
1402 options->ip_qos_bulk = value2;
1403 }
1404 break;
1405
Ben Lindstromade03f62001-12-06 18:22:17 +00001406 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001407 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001408 filename, linenum, arg);
1409 while (arg)
1410 arg = strdelim(&cp);
1411 break;
1412
Damien Millerf9b3feb2003-05-16 11:38:32 +10001413 case sUnsupported:
1414 logit("%s line %d: Unsupported option %s",
1415 filename, linenum, arg);
1416 while (arg)
1417 arg = strdelim(&cp);
1418 break;
1419
Ben Lindstromade03f62001-12-06 18:22:17 +00001420 default:
1421 fatal("%s line %d: Missing handler for opcode %s (%d)",
1422 filename, linenum, arg, opcode);
1423 }
1424 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1425 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1426 filename, linenum, arg);
1427 return 0;
1428}
1429
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001430/* Reads the server configuration file. */
1431
Damien Miller4af51302000-04-16 11:18:38 +10001432void
Darren Tucker645ab752004-06-25 13:33:20 +10001433load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001434{
Darren Tucker645ab752004-06-25 13:33:20 +10001435 char line[1024], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001436 FILE *f;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001437
Darren Tucker645ab752004-06-25 13:33:20 +10001438 debug2("%s: filename %s", __func__, filename);
1439 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001440 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001441 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001442 }
Darren Tucker645ab752004-06-25 13:33:20 +10001443 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001444 while (fgets(line, sizeof(line), f)) {
Darren Tucker645ab752004-06-25 13:33:20 +10001445 /*
1446 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001447 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001448 * line numbers later for error messages
1449 */
1450 if ((cp = strchr(line, '#')) != NULL)
1451 memcpy(cp, "\n", 2);
1452 cp = line + strspn(line, " \t\r");
1453
1454 buffer_append(conf, cp, strlen(cp));
1455 }
1456 buffer_append(conf, "\0", 1);
1457 fclose(f);
1458 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1459}
1460
1461void
Darren Tucker45150472006-07-12 22:34:17 +10001462parse_server_match_config(ServerOptions *options, const char *user,
1463 const char *host, const char *address)
Darren Tucker645ab752004-06-25 13:33:20 +10001464{
Darren Tucker45150472006-07-12 22:34:17 +10001465 ServerOptions mo;
1466
1467 initialize_server_options(&mo);
1468 parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
Darren Tucker1629c072007-02-19 22:25:37 +11001469 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001470}
1471
Darren Tucker1629c072007-02-19 22:25:37 +11001472/* Helper macros */
1473#define M_CP_INTOPT(n) do {\
1474 if (src->n != -1) \
1475 dst->n = src->n; \
1476} while (0)
1477#define M_CP_STROPT(n) do {\
1478 if (src->n != NULL) { \
1479 if (dst->n != NULL) \
1480 xfree(dst->n); \
1481 dst->n = src->n; \
1482 } \
1483} while(0)
Damien Millerd8478b62011-05-29 21:39:36 +10001484#define M_CP_STRARRAYOPT(n, num_n) do {\
1485 if (src->num_n != 0) { \
1486 for (dst->num_n = 0; dst->num_n < src->num_n; dst->num_n++) \
1487 dst->n[dst->num_n] = xstrdup(src->n[dst->num_n]); \
1488 } \
1489} while(0)
Darren Tucker1629c072007-02-19 22:25:37 +11001490
1491/*
1492 * Copy any supported values that are set.
1493 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001494 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001495 * array values that are not used pre-authentication, because any that we
1496 * do use must be explictly sent in mm_getpwnamallow().
1497 */
Darren Tucker45150472006-07-12 22:34:17 +10001498void
Darren Tucker1629c072007-02-19 22:25:37 +11001499copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001500{
Darren Tucker1629c072007-02-19 22:25:37 +11001501 M_CP_INTOPT(password_authentication);
1502 M_CP_INTOPT(gss_authentication);
1503 M_CP_INTOPT(rsa_authentication);
1504 M_CP_INTOPT(pubkey_authentication);
1505 M_CP_INTOPT(kerberos_authentication);
1506 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001507 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001508 M_CP_INTOPT(kbd_interactive_authentication);
Damien Miller01ed2272008-11-05 16:20:46 +11001509 M_CP_INTOPT(zero_knowledge_password_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001510 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001511 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001512
1513 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001514 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001515 M_CP_INTOPT(permit_tun);
Darren Tucker1629c072007-02-19 22:25:37 +11001516 M_CP_INTOPT(gateway_ports);
1517 M_CP_INTOPT(x11_display_offset);
1518 M_CP_INTOPT(x11_forwarding);
1519 M_CP_INTOPT(x11_use_localhost);
Damien Miller7207f642008-05-19 15:34:50 +10001520 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001521 M_CP_INTOPT(max_authtries);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001522 M_CP_INTOPT(ip_qos_interactive);
1523 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker1629c072007-02-19 22:25:37 +11001524
Damien Millerf2e407e2011-05-20 19:04:14 +10001525 /* See comment in servconf.h */
1526 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10001527
Damien Millerc2411902011-05-20 19:03:49 +10001528 /*
1529 * The only things that should be below this point are string options
1530 * which are only used after authentication.
1531 */
Damien Miller5d74e582011-05-20 19:03:31 +10001532 if (preauth)
1533 return;
Damien Millerd8478b62011-05-29 21:39:36 +10001534
Damien Miller5d74e582011-05-20 19:03:31 +10001535 M_CP_STROPT(adm_forced_command);
1536 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001537}
1538
Darren Tucker1629c072007-02-19 22:25:37 +11001539#undef M_CP_INTOPT
1540#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10001541#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11001542
Darren Tucker45150472006-07-12 22:34:17 +10001543void
1544parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1545 const char *user, const char *host, const char *address)
1546{
1547 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001548 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001549
1550 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1551
Darren Tucker9fbac712004-08-12 22:41:44 +10001552 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tucker45150472006-07-12 22:34:17 +10001553 active = user ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001554 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001555 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001556 if (process_server_config_line(options, cp, filename,
Darren Tucker45150472006-07-12 22:34:17 +10001557 linenum++, &active, user, host, address) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001558 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001559 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001560 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001561 if (bad_options > 0)
1562 fatal("%s: terminating, %d bad configuration options",
1563 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001564}
Darren Tuckere7140f22008-06-10 23:01:51 +10001565
1566static const char *
Damien Miller82c55872011-06-23 08:20:30 +10001567fmt_multistate_int(int val, const struct multistate *m)
1568{
1569 u_int i;
1570
1571 for (i = 0; m[i].key != NULL; i++) {
1572 if (m[i].value == val)
1573 return m[i].key;
1574 }
1575 return "UNKNOWN";
1576}
1577
1578static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10001579fmt_intarg(ServerOpCodes code, int val)
1580{
Damien Miller82c55872011-06-23 08:20:30 +10001581 if (val == -1)
1582 return "unset";
1583 switch (code) {
1584 case sAddressFamily:
1585 return fmt_multistate_int(val, multistate_addressfamily);
1586 case sPermitRootLogin:
1587 return fmt_multistate_int(val, multistate_permitrootlogin);
1588 case sGatewayPorts:
1589 return fmt_multistate_int(val, multistate_gatewayports);
1590 case sCompression:
1591 return fmt_multistate_int(val, multistate_compression);
Damien Miller69ff1df2011-06-23 08:30:03 +10001592 case sUsePrivilegeSeparation:
1593 return fmt_multistate_int(val, multistate_privsep);
Damien Miller82c55872011-06-23 08:20:30 +10001594 case sProtocol:
Darren Tuckere7140f22008-06-10 23:01:51 +10001595 switch (val) {
1596 case SSH_PROTO_1:
1597 return "1";
1598 case SSH_PROTO_2:
1599 return "2";
1600 case (SSH_PROTO_1|SSH_PROTO_2):
1601 return "2,1";
1602 default:
1603 return "UNKNOWN";
1604 }
Damien Miller82c55872011-06-23 08:20:30 +10001605 default:
1606 switch (val) {
1607 case 0:
1608 return "no";
1609 case 1:
1610 return "yes";
1611 default:
1612 return "UNKNOWN";
1613 }
Darren Tuckere7140f22008-06-10 23:01:51 +10001614 }
Darren Tuckere7140f22008-06-10 23:01:51 +10001615}
1616
1617static const char *
1618lookup_opcode_name(ServerOpCodes code)
1619{
1620 u_int i;
1621
1622 for (i = 0; keywords[i].name != NULL; i++)
1623 if (keywords[i].opcode == code)
1624 return(keywords[i].name);
1625 return "UNKNOWN";
1626}
1627
1628static void
1629dump_cfg_int(ServerOpCodes code, int val)
1630{
1631 printf("%s %d\n", lookup_opcode_name(code), val);
1632}
1633
1634static void
1635dump_cfg_fmtint(ServerOpCodes code, int val)
1636{
1637 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
1638}
1639
1640static void
1641dump_cfg_string(ServerOpCodes code, const char *val)
1642{
1643 if (val == NULL)
1644 return;
1645 printf("%s %s\n", lookup_opcode_name(code), val);
1646}
1647
1648static void
1649dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
1650{
1651 u_int i;
1652
1653 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10001654 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
1655}
1656
1657static void
1658dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
1659{
1660 u_int i;
1661
1662 printf("%s", lookup_opcode_name(code));
1663 for (i = 0; i < count; i++)
1664 printf(" %s", vals[i]);
1665 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10001666}
1667
1668void
1669dump_config(ServerOptions *o)
1670{
1671 u_int i;
1672 int ret;
1673 struct addrinfo *ai;
1674 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
1675
1676 /* these are usually at the top of the config */
1677 for (i = 0; i < o->num_ports; i++)
1678 printf("port %d\n", o->ports[i]);
1679 dump_cfg_fmtint(sProtocol, o->protocol);
1680 dump_cfg_fmtint(sAddressFamily, o->address_family);
1681
1682 /* ListenAddress must be after Port */
1683 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
1684 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1685 sizeof(addr), port, sizeof(port),
1686 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1687 error("getnameinfo failed: %.100s",
1688 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
1689 strerror(errno));
1690 } else {
1691 if (ai->ai_family == AF_INET6)
1692 printf("listenaddress [%s]:%s\n", addr, port);
1693 else
1694 printf("listenaddress %s:%s\n", addr, port);
1695 }
1696 }
1697
1698 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10001699#ifdef USE_PAM
1700 dump_cfg_int(sUsePAM, o->use_pam);
1701#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001702 dump_cfg_int(sServerKeyBits, o->server_key_bits);
1703 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
1704 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
1705 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
1706 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11001707 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10001708 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
1709 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
1710
1711 /* formatted integer arguments */
1712 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
1713 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
1714 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
1715 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
1716 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
1717 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
1718 o->hostbased_uses_name_from_packet_only);
1719 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
1720 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10001721#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10001722 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
1723 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
1724 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10001725# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10001726 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10001727# endif
1728#endif
1729#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10001730 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
1731 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10001732#endif
Damien Miller01ed2272008-11-05 16:20:46 +11001733#ifdef JPAKE
1734 dump_cfg_fmtint(sZeroKnowledgePasswordAuthentication,
1735 o->zero_knowledge_password_authentication);
1736#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001737 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
1738 dump_cfg_fmtint(sKbdInteractiveAuthentication,
1739 o->kbd_interactive_authentication);
1740 dump_cfg_fmtint(sChallengeResponseAuthentication,
1741 o->challenge_response_authentication);
1742 dump_cfg_fmtint(sPrintMotd, o->print_motd);
1743 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
1744 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
1745 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
1746 dump_cfg_fmtint(sStrictModes, o->strict_modes);
1747 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
1748 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
1749 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
1750 dump_cfg_fmtint(sUseLogin, o->use_login);
1751 dump_cfg_fmtint(sCompression, o->compression);
1752 dump_cfg_fmtint(sGatewayPorts, o->gateway_ports);
1753 dump_cfg_fmtint(sUseDNS, o->use_dns);
1754 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
1755 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
1756
1757 /* string arguments */
1758 dump_cfg_string(sPidFile, o->pid_file);
1759 dump_cfg_string(sXAuthLocation, o->xauth_location);
1760 dump_cfg_string(sCiphers, o->ciphers);
1761 dump_cfg_string(sMacs, o->macs);
1762 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10001763 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11001764 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11001765 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
1766 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10001767 dump_cfg_string(sAuthorizedPrincipalsFile,
1768 o->authorized_principals_file);
Darren Tuckere7140f22008-06-10 23:01:51 +10001769
1770 /* string arguments requiring a lookup */
1771 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
1772 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
1773
1774 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10001775 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
1776 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10001777 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
1778 o->host_key_files);
Damien Miller0a80ca12010-02-27 07:55:05 +11001779 dump_cfg_strarray(sHostKeyFile, o->num_host_cert_files,
1780 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10001781 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
1782 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
1783 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
1784 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
1785 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
1786
1787 /* other arguments */
1788 for (i = 0; i < o->num_subsystems; i++)
1789 printf("subsystem %s %s\n", o->subsystem_name[i],
1790 o->subsystem_args[i]);
1791
1792 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
1793 o->max_startups_rate, o->max_startups);
1794
1795 for (i = 0; tunmode_desc[i].val != -1; i++)
1796 if (tunmode_desc[i].val == o->permit_tun) {
1797 s = tunmode_desc[i].text;
1798 break;
1799 }
1800 dump_cfg_string(sPermitTunnel, s);
1801
Damien Miller91475862011-05-05 14:14:34 +10001802 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
1803 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11001804
Darren Tuckere7140f22008-06-10 23:01:51 +10001805 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10001806}