blob: daed26a66e6a7837bea14001e53ae473cc6b8b48 [file] [log] [blame]
Damien Millerf2e407e2011-05-20 19:04:14 +10001/* $OpenBSD: servconf.c,v 1.218 2011/05/20 03:25:45 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;
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000129 options->authorized_keys_file = NULL;
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 Miller75413ac2001-11-12 11:14:35 +1100266 if (options->authorized_keys_file == NULL)
Damien Millerc54b02c2010-10-07 21:40:17 +1100267 options->authorized_keys_file = xstrdup(_PATH_SSH_USER_PERMITTED_KEYS);
Damien Millerd27b9472005-12-13 19:29:02 +1100268 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100269 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller01ed2272008-11-05 16:20:46 +1100270 if (options->zero_knowledge_password_authentication == -1)
271 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100272 if (options->ip_qos_interactive == -1)
273 options->ip_qos_interactive = IPTOS_LOWDELAY;
274 if (options->ip_qos_bulk == -1)
275 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000276
Ben Lindstromfb62a692002-06-06 19:47:11 +0000277 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000278 if (use_privsep == -1)
Ben Lindstromfb62a692002-06-06 19:47:11 +0000279 use_privsep = 1;
Damien Miller4903eb42002-06-21 16:20:44 +1000280
Tim Rice40017b02002-07-14 13:36:49 -0700281#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000282 if (use_privsep && options->compression == 1) {
283 error("This platform does not support both privilege "
284 "separation and compression");
285 error("Compression disabled");
286 options->compression = 0;
287 }
288#endif
289
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290}
291
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100293typedef enum {
294 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100295 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000296 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100297 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100298 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
299 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000300 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100301 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100302 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000303 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100304 sPasswordAuthentication, sKbdInteractiveAuthentication,
305 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000306 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100307 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller12c150e2003-12-17 16:31:10 +1100308 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000309 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Damien Miller50a41ed2000-10-16 12:14:42 +1100310 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000311 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000312 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
Damien Miller7207f642008-05-19 15:34:50 +1000313 sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000314 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100315 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Damien Millerec2eaa32011-05-20 18:57:14 +1000316 sClientAliveCountMax, sAuthorizedKeysFile,
Damien Millerd27b9472005-12-13 19:29:02 +1100317 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100318 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100319 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller0a80ca12010-02-27 07:55:05 +1100320 sZeroKnowledgePasswordAuthentication, sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000321 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Miller0dac6fb2010-11-20 15:19:38 +1100322 sKexAlgorithms, sIPQoS,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000323 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324} ServerOpCodes;
325
Darren Tucker45150472006-07-12 22:34:17 +1000326#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
327#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
328#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
329
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100331static struct {
332 const char *name;
333 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000334 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100335} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100336 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000337#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000338 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000339#else
Darren Tucker45150472006-07-12 22:34:17 +1000340 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000341#endif
Darren Tucker45150472006-07-12 22:34:17 +1000342 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100343 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000344 { "port", sPort, SSHCFG_GLOBAL },
345 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
346 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
347 { "pidfile", sPidFile, SSHCFG_GLOBAL },
348 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
349 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
350 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100351 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000352 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
353 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
354 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100355 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
356 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000357 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100358 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
359 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000360 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000361#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100362 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000363 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
364 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100365#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000366 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000367#else
Darren Tucker45150472006-07-12 22:34:17 +1000368 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100369#endif
370#else
Darren Tucker1629c072007-02-19 22:25:37 +1100371 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000372 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
373 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
374 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000375#endif
Darren Tucker45150472006-07-12 22:34:17 +1000376 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
377 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000378#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100379 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000380 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000381#else
Darren Tucker1629c072007-02-19 22:25:37 +1100382 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000383 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000384#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100385 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
386 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100387 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000388 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
Damien Miller01ed2272008-11-05 16:20:46 +1100389#ifdef JPAKE
390 { "zeroknowledgepasswordauthentication", sZeroKnowledgePasswordAuthentication, SSHCFG_ALL },
391#else
392 { "zeroknowledgepasswordauthentication", sUnsupported, SSHCFG_ALL },
393#endif
Darren Tucker45150472006-07-12 22:34:17 +1000394 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
395 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
396 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
397 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
398 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
399 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
400 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000401 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
402 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
403 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000404 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
405 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100406 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000407 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
408 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
409 { "compression", sCompression, SSHCFG_GLOBAL },
410 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
411 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
412 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000413 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000414 { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
415 { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
416 { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
417 { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
418 { "ciphers", sCiphers, SSHCFG_GLOBAL },
419 { "macs", sMacs, SSHCFG_GLOBAL },
420 { "protocol", sProtocol, SSHCFG_GLOBAL },
421 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
422 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
423 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000424 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000425 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100426 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000427 { "usedns", sUseDNS, SSHCFG_GLOBAL },
428 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
429 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
430 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
431 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000432 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000433 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Darren Tucker45150472006-07-12 22:34:17 +1000434 { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000435 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000436 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000437 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000438 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100439 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100440 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100441 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
442 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000443 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000444 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100445 { "ipqos", sIPQoS, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000446 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447};
448
Darren Tuckere7140f22008-06-10 23:01:51 +1000449static struct {
450 int val;
451 char *text;
452} tunmode_desc[] = {
453 { SSH_TUNMODE_NO, "no" },
454 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
455 { SSH_TUNMODE_ETHERNET, "ethernet" },
456 { SSH_TUNMODE_YES, "yes" },
457 { -1, NULL }
458};
459
Damien Miller5428f641999-11-25 11:54:57 +1100460/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000461 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100462 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463
Damien Miller4af51302000-04-16 11:18:38 +1000464static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100465parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000466 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000468 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469
Damien Miller95def091999-11-25 00:26:21 +1100470 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000471 if (strcasecmp(cp, keywords[i].name) == 0) {
472 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100473 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000474 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000476 error("%s: line %d: Bad configuration option: %s",
477 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100478 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479}
480
Darren Tucker88b6fb22010-01-13 22:44:29 +1100481char *
482derelativise_path(const char *path)
483{
Damien Miller44451d02010-03-26 10:40:04 +1100484 char *expanded, *ret, cwd[MAXPATHLEN];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100485
486 expanded = tilde_expand_filename(path, getuid());
487 if (*expanded == '/')
488 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100489 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100490 fatal("%s: getcwd: %s", __func__, strerror(errno));
491 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100492 xfree(expanded);
493 return ret;
494}
495
Ben Lindstrombba81212001-06-25 05:01:22 +0000496static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100497add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100498{
Damien Millereccb9de2005-06-17 12:59:34 +1000499 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100500
501 if (options->num_ports == 0)
502 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100503 if (options->address_family == -1)
504 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000505 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000506 for (i = 0; i < options->num_ports; i++)
507 add_one_listen_addr(options, addr, options->ports[i]);
508 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000509 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000510}
511
Ben Lindstrombba81212001-06-25 05:01:22 +0000512static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100513add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000514{
515 struct addrinfo hints, *ai, *aitop;
516 char strport[NI_MAXSERV];
517 int gaierr;
518
519 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100520 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000521 hints.ai_socktype = SOCK_STREAM;
522 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100523 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000524 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
525 fatal("bad addr or host: %s (%s)",
526 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100527 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000528 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
529 ;
530 ai->ai_next = options->listen_addrs;
531 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100532}
533
Darren Tucker45150472006-07-12 22:34:17 +1000534/*
535 * The strategy for the Match blocks is that the config file is parsed twice.
536 *
537 * The first time is at startup. activep is initialized to 1 and the
538 * directives in the global context are processed and acted on. Hitting a
539 * Match directive unsets activep and the directives inside the block are
540 * checked for syntax only.
541 *
542 * The second time is after a connection has been established but before
543 * authentication. activep is initialized to 2 and global config directives
544 * are ignored since they have already been processed. If the criteria in a
545 * Match block is met, activep is set and the subsequent directives
546 * processed and actioned until EOF or another Match block unsets it. Any
547 * options set are copied into the main server config.
548 *
549 * Potential additions/improvements:
550 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
551 *
552 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
553 * Match Address 192.168.0.*
554 * Tag trusted
555 * Match Group wheel
556 * Tag trusted
557 * Match Tag trusted
558 * AllowTcpForwarding yes
559 * GatewayPorts clientspecified
560 * [...]
561 *
562 * - Add a PermittedChannelRequests directive
563 * Match Group shell
564 * PermittedChannelRequests session,forwarded-tcpip
565 */
566
567static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000568match_cfg_line_group(const char *grps, int line, const char *user)
569{
570 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000571 struct passwd *pw;
572
Damien Miller565ca3f2006-08-19 00:23:15 +1000573 if (user == NULL)
574 goto out;
575
576 if ((pw = getpwnam(user)) == NULL) {
577 debug("Can't match group at line %d because user %.100s does "
578 "not exist", line, user);
579 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
580 debug("Can't Match group because user %.100s not in any group "
581 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000582 } else if (ga_match_pattern_list(grps) != 1) {
583 debug("user %.100s does not match group list %.100s at line %d",
584 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000585 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000586 debug("user %.100s matched group list %.100s at line %d", user,
587 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000588 result = 1;
589 }
590out:
591 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000592 return result;
593}
594
595static int
Darren Tucker45150472006-07-12 22:34:17 +1000596match_cfg_line(char **condition, int line, const char *user, const char *host,
597 const char *address)
598{
599 int result = 1;
600 char *arg, *attrib, *cp = *condition;
601 size_t len;
602
603 if (user == NULL)
604 debug3("checking syntax for 'Match %s'", cp);
605 else
606 debug3("checking match for '%s' user %s host %s addr %s", cp,
607 user ? user : "(null)", host ? host : "(null)",
608 address ? address : "(null)");
609
610 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
611 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
612 error("Missing Match criteria for %s", attrib);
613 return -1;
614 }
615 len = strlen(arg);
616 if (strcasecmp(attrib, "user") == 0) {
617 if (!user) {
618 result = 0;
619 continue;
620 }
621 if (match_pattern_list(user, arg, len, 0) != 1)
622 result = 0;
623 else
624 debug("user %.100s matched 'User %.100s' at "
625 "line %d", user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000626 } else if (strcasecmp(attrib, "group") == 0) {
627 switch (match_cfg_line_group(arg, line, user)) {
628 case -1:
629 return -1;
630 case 0:
631 result = 0;
632 }
Darren Tucker45150472006-07-12 22:34:17 +1000633 } else if (strcasecmp(attrib, "host") == 0) {
634 if (!host) {
635 result = 0;
636 continue;
637 }
638 if (match_hostname(host, arg, len) != 1)
639 result = 0;
640 else
641 debug("connection from %.100s matched 'Host "
642 "%.100s' at line %d", host, arg, line);
643 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000644 switch (addr_match_list(address, arg)) {
645 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000646 debug("connection from %.100s matched 'Address "
647 "%.100s' at line %d", address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000648 break;
649 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000650 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000651 result = 0;
652 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000653 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000654 return -1;
655 }
Darren Tucker45150472006-07-12 22:34:17 +1000656 } else {
657 error("Unsupported Match attribute %s", attrib);
658 return -1;
659 }
660 }
661 if (user != NULL)
662 debug3("match %sfound", result ? "" : "not ");
663 *condition = cp;
664 return result;
665}
666
Damien Millere2754432006-07-24 14:06:47 +1000667#define WHITESPACE " \t\r\n"
668
Ben Lindstromade03f62001-12-06 18:22:17 +0000669int
670process_server_config_line(ServerOptions *options, char *line,
Darren Tucker45150472006-07-12 22:34:17 +1000671 const char *filename, int linenum, int *activep, const char *user,
672 const char *host, const char *address)
Ben Lindstromade03f62001-12-06 18:22:17 +0000673{
674 char *cp, **charptr, *arg, *p;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100675 int cmdline = 0, *intptr, value, value2, n;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100676 SyslogFacility *log_facility_ptr;
677 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000678 ServerOpCodes opcode;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100679 int port;
Darren Tucker45150472006-07-12 22:34:17 +1000680 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000681 size_t len;
Ben Lindstromade03f62001-12-06 18:22:17 +0000682
683 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100684 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100685 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000686 /* Ignore leading whitespace */
687 if (*arg == '\0')
688 arg = strdelim(&cp);
689 if (!arg || !*arg || *arg == '#')
690 return 0;
691 intptr = NULL;
692 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000693 opcode = parse_token(arg, filename, linenum, &flags);
694
695 if (activep == NULL) { /* We are processing a command line directive */
696 cmdline = 1;
697 activep = &cmdline;
698 }
699 if (*activep && opcode != sMatch)
700 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
701 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
702 if (user == NULL) {
703 fatal("%s line %d: Directive '%s' is not allowed "
704 "within a Match block", filename, linenum, arg);
705 } else { /* this is a directive we have already processed */
706 while (arg)
707 arg = strdelim(&cp);
708 return 0;
709 }
710 }
711
Ben Lindstromade03f62001-12-06 18:22:17 +0000712 switch (opcode) {
713 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000714 case sUsePAM:
715 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000716 goto parse_flag;
717
718 /* Standard Options */
719 case sBadOption:
720 return -1;
721 case sPort:
722 /* ignore ports from configfile if cmdline specifies ports */
723 if (options->ports_from_cmdline)
724 return 0;
725 if (options->listen_addrs != NULL)
726 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100727 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000728 if (options->num_ports >= MAX_PORTS)
729 fatal("%s line %d: too many ports.",
730 filename, linenum);
731 arg = strdelim(&cp);
732 if (!arg || *arg == '\0')
733 fatal("%s line %d: missing port number.",
734 filename, linenum);
735 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100736 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000737 fatal("%s line %d: Badly formatted port number.",
738 filename, linenum);
739 break;
740
741 case sServerKeyBits:
742 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +1000743 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +0000744 arg = strdelim(&cp);
745 if (!arg || *arg == '\0')
746 fatal("%s line %d: missing integer value.",
747 filename, linenum);
748 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000749 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000750 *intptr = value;
751 break;
752
753 case sLoginGraceTime:
754 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +1000755 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +0000756 arg = strdelim(&cp);
757 if (!arg || *arg == '\0')
758 fatal("%s line %d: missing time value.",
759 filename, linenum);
760 if ((value = convtime(arg)) == -1)
761 fatal("%s line %d: invalid time value.",
762 filename, linenum);
763 if (*intptr == -1)
764 *intptr = value;
765 break;
766
767 case sKeyRegenerationTime:
768 intptr = &options->key_regeneration_time;
769 goto parse_time;
770
771 case sListenAddress:
772 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100773 if (arg == NULL || *arg == '\0')
774 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000775 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000776 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
777 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
778 && strchr(p+1, ':') != NULL) {
779 add_listen_addr(options, arg, 0);
780 break;
781 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100782 p = hpdelim(&arg);
783 if (p == NULL)
784 fatal("%s line %d: bad address:port usage",
785 filename, linenum);
786 p = cleanhostname(p);
787 if (arg == NULL)
788 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100789 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100790 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000791
Damien Millerf91ee4c2005-03-01 21:24:33 +1100792 add_listen_addr(options, p, port);
793
Ben Lindstromade03f62001-12-06 18:22:17 +0000794 break;
795
Darren Tucker0f383232005-01-20 10:57:56 +1100796 case sAddressFamily:
797 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000798 if (!arg || *arg == '\0')
799 fatal("%s line %d: missing address family.",
800 filename, linenum);
Darren Tucker0f383232005-01-20 10:57:56 +1100801 intptr = &options->address_family;
802 if (options->listen_addrs != NULL)
803 fatal("%s line %d: address family must be specified before "
804 "ListenAddress.", filename, linenum);
805 if (strcasecmp(arg, "inet") == 0)
806 value = AF_INET;
807 else if (strcasecmp(arg, "inet6") == 0)
808 value = AF_INET6;
809 else if (strcasecmp(arg, "any") == 0)
810 value = AF_UNSPEC;
811 else
812 fatal("%s line %d: unsupported address family \"%s\".",
813 filename, linenum, arg);
814 if (*intptr == -1)
815 *intptr = value;
816 break;
817
Ben Lindstromade03f62001-12-06 18:22:17 +0000818 case sHostKeyFile:
819 intptr = &options->num_host_key_files;
820 if (*intptr >= MAX_HOSTKEYS)
821 fatal("%s line %d: too many host keys specified (max %d).",
822 filename, linenum, MAX_HOSTKEYS);
823 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +1000824 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +0000825 arg = strdelim(&cp);
826 if (!arg || *arg == '\0')
827 fatal("%s line %d: missing file name.",
828 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +1000829 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +1100830 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000831 /* increase optional counter */
832 if (intptr != NULL)
833 *intptr = *intptr + 1;
834 }
835 break;
836
Damien Miller0a80ca12010-02-27 07:55:05 +1100837 case sHostCertificate:
838 intptr = &options->num_host_cert_files;
839 if (*intptr >= MAX_HOSTKEYS)
840 fatal("%s line %d: too many host certificates "
841 "specified (max %d).", filename, linenum,
842 MAX_HOSTCERTS);
843 charptr = &options->host_cert_files[*intptr];
844 goto parse_filename;
845 break;
846
Ben Lindstromade03f62001-12-06 18:22:17 +0000847 case sPidFile:
848 charptr = &options->pid_file;
849 goto parse_filename;
850
851 case sPermitRootLogin:
852 intptr = &options->permit_root_login;
853 arg = strdelim(&cp);
854 if (!arg || *arg == '\0')
855 fatal("%s line %d: missing yes/"
856 "without-password/forced-commands-only/no "
857 "argument.", filename, linenum);
858 value = 0; /* silence compiler */
859 if (strcmp(arg, "without-password") == 0)
860 value = PERMIT_NO_PASSWD;
861 else if (strcmp(arg, "forced-commands-only") == 0)
862 value = PERMIT_FORCED_ONLY;
863 else if (strcmp(arg, "yes") == 0)
864 value = PERMIT_YES;
865 else if (strcmp(arg, "no") == 0)
866 value = PERMIT_NO;
867 else
868 fatal("%s line %d: Bad yes/"
869 "without-password/forced-commands-only/no "
870 "argument: %s", filename, linenum, arg);
Darren Tucker15f94272008-01-01 20:36:56 +1100871 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000872 *intptr = value;
873 break;
874
875 case sIgnoreRhosts:
876 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +1000877 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +0000878 arg = strdelim(&cp);
879 if (!arg || *arg == '\0')
880 fatal("%s line %d: missing yes/no argument.",
881 filename, linenum);
882 value = 0; /* silence compiler */
883 if (strcmp(arg, "yes") == 0)
884 value = 1;
885 else if (strcmp(arg, "no") == 0)
886 value = 0;
887 else
888 fatal("%s line %d: Bad yes/no argument: %s",
889 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +1000890 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000891 *intptr = value;
892 break;
893
894 case sIgnoreUserKnownHosts:
895 intptr = &options->ignore_user_known_hosts;
896 goto parse_flag;
897
Ben Lindstromade03f62001-12-06 18:22:17 +0000898 case sRhostsRSAAuthentication:
899 intptr = &options->rhosts_rsa_authentication;
900 goto parse_flag;
901
902 case sHostbasedAuthentication:
903 intptr = &options->hostbased_authentication;
904 goto parse_flag;
905
906 case sHostbasedUsesNameFromPacketOnly:
907 intptr = &options->hostbased_uses_name_from_packet_only;
908 goto parse_flag;
909
910 case sRSAAuthentication:
911 intptr = &options->rsa_authentication;
912 goto parse_flag;
913
914 case sPubkeyAuthentication:
915 intptr = &options->pubkey_authentication;
916 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000917
Ben Lindstromade03f62001-12-06 18:22:17 +0000918 case sKerberosAuthentication:
919 intptr = &options->kerberos_authentication;
920 goto parse_flag;
921
922 case sKerberosOrLocalPasswd:
923 intptr = &options->kerberos_or_local_passwd;
924 goto parse_flag;
925
926 case sKerberosTicketCleanup:
927 intptr = &options->kerberos_ticket_cleanup;
928 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000929
Darren Tucker22ef5082003-12-31 11:37:34 +1100930 case sKerberosGetAFSToken:
931 intptr = &options->kerberos_get_afs_token;
932 goto parse_flag;
933
Darren Tucker0efd1552003-08-26 11:49:55 +1000934 case sGssAuthentication:
935 intptr = &options->gss_authentication;
936 goto parse_flag;
937
938 case sGssCleanupCreds:
939 intptr = &options->gss_cleanup_creds;
940 goto parse_flag;
941
Ben Lindstromade03f62001-12-06 18:22:17 +0000942 case sPasswordAuthentication:
943 intptr = &options->password_authentication;
944 goto parse_flag;
945
Damien Miller01ed2272008-11-05 16:20:46 +1100946 case sZeroKnowledgePasswordAuthentication:
947 intptr = &options->zero_knowledge_password_authentication;
948 goto parse_flag;
949
Ben Lindstromade03f62001-12-06 18:22:17 +0000950 case sKbdInteractiveAuthentication:
951 intptr = &options->kbd_interactive_authentication;
952 goto parse_flag;
953
954 case sChallengeResponseAuthentication:
955 intptr = &options->challenge_response_authentication;
956 goto parse_flag;
957
958 case sPrintMotd:
959 intptr = &options->print_motd;
960 goto parse_flag;
961
962 case sPrintLastLog:
963 intptr = &options->print_lastlog;
964 goto parse_flag;
965
966 case sX11Forwarding:
967 intptr = &options->x11_forwarding;
968 goto parse_flag;
969
970 case sX11DisplayOffset:
971 intptr = &options->x11_display_offset;
972 goto parse_int;
973
Damien Miller95c249f2002-02-05 12:11:34 +1100974 case sX11UseLocalhost:
975 intptr = &options->x11_use_localhost;
976 goto parse_flag;
977
Ben Lindstromade03f62001-12-06 18:22:17 +0000978 case sXAuthLocation:
979 charptr = &options->xauth_location;
980 goto parse_filename;
981
982 case sStrictModes:
983 intptr = &options->strict_modes;
984 goto parse_flag;
985
Damien Miller12c150e2003-12-17 16:31:10 +1100986 case sTCPKeepAlive:
987 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +0000988 goto parse_flag;
989
990 case sEmptyPasswd:
991 intptr = &options->permit_empty_passwd;
992 goto parse_flag;
993
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000994 case sPermitUserEnvironment:
995 intptr = &options->permit_user_env;
996 goto parse_flag;
997
Ben Lindstromade03f62001-12-06 18:22:17 +0000998 case sUseLogin:
999 intptr = &options->use_login;
1000 goto parse_flag;
1001
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001002 case sCompression:
1003 intptr = &options->compression;
Damien Miller9786e6e2005-07-26 21:54:56 +10001004 arg = strdelim(&cp);
1005 if (!arg || *arg == '\0')
1006 fatal("%s line %d: missing yes/no/delayed "
1007 "argument.", filename, linenum);
1008 value = 0; /* silence compiler */
1009 if (strcmp(arg, "delayed") == 0)
1010 value = COMP_DELAYED;
1011 else if (strcmp(arg, "yes") == 0)
1012 value = COMP_ZLIB;
1013 else if (strcmp(arg, "no") == 0)
1014 value = COMP_NONE;
1015 else
1016 fatal("%s line %d: Bad yes/no/delayed "
1017 "argument: %s", filename, linenum, arg);
1018 if (*intptr == -1)
1019 *intptr = value;
1020 break;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001021
Ben Lindstromade03f62001-12-06 18:22:17 +00001022 case sGatewayPorts:
1023 intptr = &options->gateway_ports;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001024 arg = strdelim(&cp);
1025 if (!arg || *arg == '\0')
1026 fatal("%s line %d: missing yes/no/clientspecified "
1027 "argument.", filename, linenum);
1028 value = 0; /* silence compiler */
1029 if (strcmp(arg, "clientspecified") == 0)
1030 value = 2;
1031 else if (strcmp(arg, "yes") == 0)
1032 value = 1;
1033 else if (strcmp(arg, "no") == 0)
1034 value = 0;
1035 else
1036 fatal("%s line %d: Bad yes/no/clientspecified "
1037 "argument: %s", filename, linenum, arg);
Darren Tucker82347a82007-02-25 20:37:52 +11001038 if (*activep && *intptr == -1)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001039 *intptr = value;
1040 break;
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;
1078 goto parse_flag;
1079
Ben Lindstromade03f62001-12-06 18:22:17 +00001080 case sAllowUsers:
1081 while ((arg = strdelim(&cp)) && *arg != '\0') {
1082 if (options->num_allow_users >= MAX_ALLOW_USERS)
1083 fatal("%s line %d: too many allow users.",
1084 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001085 options->allow_users[options->num_allow_users++] =
1086 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001087 }
1088 break;
1089
1090 case sDenyUsers:
1091 while ((arg = strdelim(&cp)) && *arg != '\0') {
1092 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001093 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001094 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001095 options->deny_users[options->num_deny_users++] =
1096 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001097 }
1098 break;
1099
1100 case sAllowGroups:
1101 while ((arg = strdelim(&cp)) && *arg != '\0') {
1102 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1103 fatal("%s line %d: too many allow groups.",
1104 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001105 options->allow_groups[options->num_allow_groups++] =
1106 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001107 }
1108 break;
1109
1110 case sDenyGroups:
1111 while ((arg = strdelim(&cp)) && *arg != '\0') {
1112 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1113 fatal("%s line %d: too many deny groups.",
1114 filename, linenum);
1115 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
1116 }
1117 break;
1118
1119 case sCiphers:
1120 arg = strdelim(&cp);
1121 if (!arg || *arg == '\0')
1122 fatal("%s line %d: Missing argument.", filename, linenum);
1123 if (!ciphers_valid(arg))
1124 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1125 filename, linenum, arg ? arg : "<NONE>");
1126 if (options->ciphers == NULL)
1127 options->ciphers = xstrdup(arg);
1128 break;
1129
1130 case sMacs:
1131 arg = strdelim(&cp);
1132 if (!arg || *arg == '\0')
1133 fatal("%s line %d: Missing argument.", filename, linenum);
1134 if (!mac_valid(arg))
1135 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1136 filename, linenum, arg ? arg : "<NONE>");
1137 if (options->macs == NULL)
1138 options->macs = xstrdup(arg);
1139 break;
1140
Damien Millerd5f62bf2010-09-24 22:11:14 +10001141 case sKexAlgorithms:
1142 arg = strdelim(&cp);
1143 if (!arg || *arg == '\0')
1144 fatal("%s line %d: Missing argument.",
1145 filename, linenum);
1146 if (!kex_names_valid(arg))
1147 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1148 filename, linenum, arg ? arg : "<NONE>");
1149 if (options->kex_algorithms == NULL)
1150 options->kex_algorithms = xstrdup(arg);
1151 break;
1152
Ben Lindstromade03f62001-12-06 18:22:17 +00001153 case sProtocol:
1154 intptr = &options->protocol;
1155 arg = strdelim(&cp);
1156 if (!arg || *arg == '\0')
1157 fatal("%s line %d: Missing argument.", filename, linenum);
1158 value = proto_spec(arg);
1159 if (value == SSH_PROTO_UNKNOWN)
1160 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001161 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001162 if (*intptr == SSH_PROTO_UNKNOWN)
1163 *intptr = value;
1164 break;
1165
1166 case sSubsystem:
1167 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1168 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001169 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001170 }
1171 arg = strdelim(&cp);
1172 if (!arg || *arg == '\0')
1173 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001174 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001175 if (!*activep) {
1176 arg = strdelim(&cp);
1177 break;
1178 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001179 for (i = 0; i < options->num_subsystems; i++)
1180 if (strcmp(arg, options->subsystem_name[i]) == 0)
1181 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001182 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001183 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1184 arg = strdelim(&cp);
1185 if (!arg || *arg == '\0')
1186 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001187 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001188 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001189
1190 /* Collect arguments (separate to executable) */
1191 p = xstrdup(arg);
1192 len = strlen(p) + 1;
1193 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1194 len += 1 + strlen(arg);
1195 p = xrealloc(p, 1, len);
1196 strlcat(p, " ", len);
1197 strlcat(p, arg, len);
1198 }
1199 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001200 options->num_subsystems++;
1201 break;
1202
1203 case sMaxStartups:
1204 arg = strdelim(&cp);
1205 if (!arg || *arg == '\0')
1206 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001207 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001208 if ((n = sscanf(arg, "%d:%d:%d",
1209 &options->max_startups_begin,
1210 &options->max_startups_rate,
1211 &options->max_startups)) == 3) {
1212 if (options->max_startups_begin >
1213 options->max_startups ||
1214 options->max_startups_rate > 100 ||
1215 options->max_startups_rate < 1)
1216 fatal("%s line %d: Illegal MaxStartups spec.",
1217 filename, linenum);
1218 } else if (n != 1)
1219 fatal("%s line %d: Illegal MaxStartups spec.",
1220 filename, linenum);
1221 else
1222 options->max_startups = options->max_startups_begin;
1223 break;
1224
Darren Tucker89413db2004-05-24 10:36:23 +10001225 case sMaxAuthTries:
1226 intptr = &options->max_authtries;
1227 goto parse_int;
1228
Damien Miller7207f642008-05-19 15:34:50 +10001229 case sMaxSessions:
1230 intptr = &options->max_sessions;
1231 goto parse_int;
1232
Ben Lindstromade03f62001-12-06 18:22:17 +00001233 case sBanner:
1234 charptr = &options->banner;
1235 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001236
Ben Lindstromade03f62001-12-06 18:22:17 +00001237 /*
1238 * These options can contain %X options expanded at
1239 * connect time, so that you can specify paths like:
1240 *
1241 * AuthorizedKeysFile /etc/ssh_keys/%u
1242 */
1243 case sAuthorizedKeysFile:
Damien Miller30da3442010-05-10 11:58:03 +10001244 charptr = &options->authorized_keys_file;
1245 goto parse_tilde_filename;
Damien Miller30da3442010-05-10 11:58:03 +10001246 case sAuthorizedPrincipalsFile:
1247 charptr = &options->authorized_principals_file;
1248 parse_tilde_filename:
Damien Millerc4cb47b2010-03-22 05:52:26 +11001249 arg = strdelim(&cp);
1250 if (!arg || *arg == '\0')
1251 fatal("%s line %d: missing file name.",
1252 filename, linenum);
1253 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001254 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001255 /* increase optional counter */
1256 if (intptr != NULL)
1257 *intptr = *intptr + 1;
1258 }
1259 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001260
1261 case sClientAliveInterval:
1262 intptr = &options->client_alive_interval;
1263 goto parse_time;
1264
1265 case sClientAliveCountMax:
1266 intptr = &options->client_alive_count_max;
1267 goto parse_int;
1268
Darren Tucker46bc0752004-05-02 22:11:30 +10001269 case sAcceptEnv:
1270 while ((arg = strdelim(&cp)) && *arg != '\0') {
1271 if (strchr(arg, '=') != NULL)
1272 fatal("%s line %d: Invalid environment name.",
1273 filename, linenum);
1274 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1275 fatal("%s line %d: too many allow env.",
1276 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001277 if (!*activep)
1278 break;
Darren Tucker46bc0752004-05-02 22:11:30 +10001279 options->accept_env[options->num_accept_env++] =
1280 xstrdup(arg);
1281 }
1282 break;
1283
Damien Millerd27b9472005-12-13 19:29:02 +11001284 case sPermitTunnel:
1285 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001286 arg = strdelim(&cp);
1287 if (!arg || *arg == '\0')
1288 fatal("%s line %d: Missing yes/point-to-point/"
1289 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001290 value = -1;
1291 for (i = 0; tunmode_desc[i].val != -1; i++)
1292 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1293 value = tunmode_desc[i].val;
1294 break;
1295 }
1296 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001297 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1298 "no argument: %s", filename, linenum, arg);
1299 if (*intptr == -1)
1300 *intptr = value;
1301 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001302
Darren Tucker45150472006-07-12 22:34:17 +10001303 case sMatch:
1304 if (cmdline)
1305 fatal("Match directive not supported as a command-line "
1306 "option");
1307 value = match_cfg_line(&cp, linenum, user, host, address);
1308 if (value < 0)
1309 fatal("%s line %d: Bad Match condition", filename,
1310 linenum);
1311 *activep = value;
1312 break;
1313
Damien Miller9b439df2006-07-24 14:04:00 +10001314 case sPermitOpen:
1315 arg = strdelim(&cp);
1316 if (!arg || *arg == '\0')
1317 fatal("%s line %d: missing PermitOpen specification",
1318 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001319 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001320 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001321 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001322 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001323 options->num_permitted_opens = 0;
1324 }
Damien Miller9b439df2006-07-24 14:04:00 +10001325 break;
1326 }
Damien Millera29b95e2007-01-05 16:28:36 +11001327 if (*activep && n == -1)
1328 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001329 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1330 p = hpdelim(&arg);
1331 if (p == NULL)
1332 fatal("%s line %d: missing host in PermitOpen",
1333 filename, linenum);
1334 p = cleanhostname(p);
Damien Miller3dc71ad2009-01-28 16:31:22 +11001335 if (arg == NULL || (port = a2port(arg)) <= 0)
Damien Millera765cf42006-07-24 14:08:13 +10001336 fatal("%s line %d: bad port number in "
1337 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001338 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001339 options->num_permitted_opens =
1340 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001341 }
Damien Miller9b439df2006-07-24 14:04:00 +10001342 break;
1343
Damien Millere2754432006-07-24 14:06:47 +10001344 case sForceCommand:
1345 if (cp == NULL)
1346 fatal("%.200s line %d: Missing argument.", filename,
1347 linenum);
1348 len = strspn(cp, WHITESPACE);
1349 if (*activep && options->adm_forced_command == NULL)
1350 options->adm_forced_command = xstrdup(cp + len);
1351 return 0;
1352
Damien Millerd8cb1f12008-02-10 22:40:12 +11001353 case sChrootDirectory:
1354 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001355
1356 arg = strdelim(&cp);
1357 if (!arg || *arg == '\0')
1358 fatal("%s line %d: missing file name.",
1359 filename, linenum);
1360 if (*activep && *charptr == NULL)
1361 *charptr = xstrdup(arg);
1362 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001363
Damien Miller1aed65e2010-03-04 21:53:35 +11001364 case sTrustedUserCAKeys:
1365 charptr = &options->trusted_user_ca_keys;
1366 goto parse_filename;
1367
1368 case sRevokedKeys:
1369 charptr = &options->revoked_keys_file;
1370 goto parse_filename;
1371
Damien Miller0dac6fb2010-11-20 15:19:38 +11001372 case sIPQoS:
1373 arg = strdelim(&cp);
1374 if ((value = parse_ipqos(arg)) == -1)
1375 fatal("%s line %d: Bad IPQoS value: %s",
1376 filename, linenum, arg);
1377 arg = strdelim(&cp);
1378 if (arg == NULL)
1379 value2 = value;
1380 else if ((value2 = parse_ipqos(arg)) == -1)
1381 fatal("%s line %d: Bad IPQoS value: %s",
1382 filename, linenum, arg);
1383 if (*activep) {
1384 options->ip_qos_interactive = value;
1385 options->ip_qos_bulk = value2;
1386 }
1387 break;
1388
Ben Lindstromade03f62001-12-06 18:22:17 +00001389 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001390 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001391 filename, linenum, arg);
1392 while (arg)
1393 arg = strdelim(&cp);
1394 break;
1395
Damien Millerf9b3feb2003-05-16 11:38:32 +10001396 case sUnsupported:
1397 logit("%s line %d: Unsupported option %s",
1398 filename, linenum, arg);
1399 while (arg)
1400 arg = strdelim(&cp);
1401 break;
1402
Ben Lindstromade03f62001-12-06 18:22:17 +00001403 default:
1404 fatal("%s line %d: Missing handler for opcode %s (%d)",
1405 filename, linenum, arg, opcode);
1406 }
1407 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1408 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1409 filename, linenum, arg);
1410 return 0;
1411}
1412
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001413/* Reads the server configuration file. */
1414
Damien Miller4af51302000-04-16 11:18:38 +10001415void
Darren Tucker645ab752004-06-25 13:33:20 +10001416load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001417{
Darren Tucker645ab752004-06-25 13:33:20 +10001418 char line[1024], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001419 FILE *f;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001420
Darren Tucker645ab752004-06-25 13:33:20 +10001421 debug2("%s: filename %s", __func__, filename);
1422 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001423 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001424 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001425 }
Darren Tucker645ab752004-06-25 13:33:20 +10001426 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001427 while (fgets(line, sizeof(line), f)) {
Darren Tucker645ab752004-06-25 13:33:20 +10001428 /*
1429 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001430 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001431 * line numbers later for error messages
1432 */
1433 if ((cp = strchr(line, '#')) != NULL)
1434 memcpy(cp, "\n", 2);
1435 cp = line + strspn(line, " \t\r");
1436
1437 buffer_append(conf, cp, strlen(cp));
1438 }
1439 buffer_append(conf, "\0", 1);
1440 fclose(f);
1441 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1442}
1443
1444void
Darren Tucker45150472006-07-12 22:34:17 +10001445parse_server_match_config(ServerOptions *options, const char *user,
1446 const char *host, const char *address)
Darren Tucker645ab752004-06-25 13:33:20 +10001447{
Darren Tucker45150472006-07-12 22:34:17 +10001448 ServerOptions mo;
1449
1450 initialize_server_options(&mo);
1451 parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
Darren Tucker1629c072007-02-19 22:25:37 +11001452 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001453}
1454
Darren Tucker1629c072007-02-19 22:25:37 +11001455/* Helper macros */
1456#define M_CP_INTOPT(n) do {\
1457 if (src->n != -1) \
1458 dst->n = src->n; \
1459} while (0)
1460#define M_CP_STROPT(n) do {\
1461 if (src->n != NULL) { \
1462 if (dst->n != NULL) \
1463 xfree(dst->n); \
1464 dst->n = src->n; \
1465 } \
1466} while(0)
1467
1468/*
1469 * Copy any supported values that are set.
1470 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001471 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001472 * array values that are not used pre-authentication, because any that we
1473 * do use must be explictly sent in mm_getpwnamallow().
1474 */
Darren Tucker45150472006-07-12 22:34:17 +10001475void
Darren Tucker1629c072007-02-19 22:25:37 +11001476copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001477{
Darren Tucker1629c072007-02-19 22:25:37 +11001478 M_CP_INTOPT(password_authentication);
1479 M_CP_INTOPT(gss_authentication);
1480 M_CP_INTOPT(rsa_authentication);
1481 M_CP_INTOPT(pubkey_authentication);
1482 M_CP_INTOPT(kerberos_authentication);
1483 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001484 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001485 M_CP_INTOPT(kbd_interactive_authentication);
Damien Miller01ed2272008-11-05 16:20:46 +11001486 M_CP_INTOPT(zero_knowledge_password_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001487 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001488 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001489
1490 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001491 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001492 M_CP_INTOPT(permit_tun);
Darren Tucker1629c072007-02-19 22:25:37 +11001493 M_CP_INTOPT(gateway_ports);
1494 M_CP_INTOPT(x11_display_offset);
1495 M_CP_INTOPT(x11_forwarding);
1496 M_CP_INTOPT(x11_use_localhost);
Damien Miller7207f642008-05-19 15:34:50 +10001497 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001498 M_CP_INTOPT(max_authtries);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001499 M_CP_INTOPT(ip_qos_interactive);
1500 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker1629c072007-02-19 22:25:37 +11001501
Damien Millerf2e407e2011-05-20 19:04:14 +10001502 /* See comment in servconf.h */
1503 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10001504
Damien Millerc2411902011-05-20 19:03:49 +10001505 /*
1506 * The only things that should be below this point are string options
1507 * which are only used after authentication.
1508 */
Damien Miller5d74e582011-05-20 19:03:31 +10001509 if (preauth)
1510 return;
1511 M_CP_STROPT(adm_forced_command);
1512 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001513}
1514
Darren Tucker1629c072007-02-19 22:25:37 +11001515#undef M_CP_INTOPT
1516#undef M_CP_STROPT
1517
Darren Tucker45150472006-07-12 22:34:17 +10001518void
1519parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1520 const char *user, const char *host, const char *address)
1521{
1522 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001523 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001524
1525 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1526
Darren Tucker9fbac712004-08-12 22:41:44 +10001527 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tucker45150472006-07-12 22:34:17 +10001528 active = user ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001529 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001530 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001531 if (process_server_config_line(options, cp, filename,
Darren Tucker45150472006-07-12 22:34:17 +10001532 linenum++, &active, user, host, address) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001533 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001534 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001535 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001536 if (bad_options > 0)
1537 fatal("%s: terminating, %d bad configuration options",
1538 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001539}
Darren Tuckere7140f22008-06-10 23:01:51 +10001540
1541static const char *
1542fmt_intarg(ServerOpCodes code, int val)
1543{
1544 if (code == sAddressFamily) {
1545 switch (val) {
1546 case AF_INET:
1547 return "inet";
1548 case AF_INET6:
1549 return "inet6";
1550 case AF_UNSPEC:
1551 return "any";
1552 default:
1553 return "UNKNOWN";
1554 }
1555 }
1556 if (code == sPermitRootLogin) {
1557 switch (val) {
1558 case PERMIT_NO_PASSWD:
Darren Tuckerff4350e2008-11-11 16:31:05 +11001559 return "without-password";
Darren Tuckere7140f22008-06-10 23:01:51 +10001560 case PERMIT_FORCED_ONLY:
1561 return "forced-commands-only";
1562 case PERMIT_YES:
1563 return "yes";
1564 }
1565 }
1566 if (code == sProtocol) {
1567 switch (val) {
1568 case SSH_PROTO_1:
1569 return "1";
1570 case SSH_PROTO_2:
1571 return "2";
1572 case (SSH_PROTO_1|SSH_PROTO_2):
1573 return "2,1";
1574 default:
1575 return "UNKNOWN";
1576 }
1577 }
1578 if (code == sGatewayPorts && val == 2)
1579 return "clientspecified";
1580 if (code == sCompression && val == COMP_DELAYED)
1581 return "delayed";
1582 switch (val) {
1583 case -1:
1584 return "unset";
1585 case 0:
1586 return "no";
1587 case 1:
1588 return "yes";
1589 }
1590 return "UNKNOWN";
1591}
1592
1593static const char *
1594lookup_opcode_name(ServerOpCodes code)
1595{
1596 u_int i;
1597
1598 for (i = 0; keywords[i].name != NULL; i++)
1599 if (keywords[i].opcode == code)
1600 return(keywords[i].name);
1601 return "UNKNOWN";
1602}
1603
1604static void
1605dump_cfg_int(ServerOpCodes code, int val)
1606{
1607 printf("%s %d\n", lookup_opcode_name(code), val);
1608}
1609
1610static void
1611dump_cfg_fmtint(ServerOpCodes code, int val)
1612{
1613 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
1614}
1615
1616static void
1617dump_cfg_string(ServerOpCodes code, const char *val)
1618{
1619 if (val == NULL)
1620 return;
1621 printf("%s %s\n", lookup_opcode_name(code), val);
1622}
1623
1624static void
1625dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
1626{
1627 u_int i;
1628
1629 for (i = 0; i < count; i++)
1630 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
1631}
1632
1633void
1634dump_config(ServerOptions *o)
1635{
1636 u_int i;
1637 int ret;
1638 struct addrinfo *ai;
1639 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
1640
1641 /* these are usually at the top of the config */
1642 for (i = 0; i < o->num_ports; i++)
1643 printf("port %d\n", o->ports[i]);
1644 dump_cfg_fmtint(sProtocol, o->protocol);
1645 dump_cfg_fmtint(sAddressFamily, o->address_family);
1646
1647 /* ListenAddress must be after Port */
1648 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
1649 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1650 sizeof(addr), port, sizeof(port),
1651 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1652 error("getnameinfo failed: %.100s",
1653 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
1654 strerror(errno));
1655 } else {
1656 if (ai->ai_family == AF_INET6)
1657 printf("listenaddress [%s]:%s\n", addr, port);
1658 else
1659 printf("listenaddress %s:%s\n", addr, port);
1660 }
1661 }
1662
1663 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10001664#ifdef USE_PAM
1665 dump_cfg_int(sUsePAM, o->use_pam);
1666#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001667 dump_cfg_int(sServerKeyBits, o->server_key_bits);
1668 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
1669 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
1670 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
1671 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11001672 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10001673 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
1674 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
1675
1676 /* formatted integer arguments */
1677 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
1678 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
1679 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
1680 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
1681 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
1682 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
1683 o->hostbased_uses_name_from_packet_only);
1684 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
1685 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10001686#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10001687 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
1688 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
1689 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10001690# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10001691 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10001692# endif
1693#endif
1694#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10001695 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
1696 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10001697#endif
Damien Miller01ed2272008-11-05 16:20:46 +11001698#ifdef JPAKE
1699 dump_cfg_fmtint(sZeroKnowledgePasswordAuthentication,
1700 o->zero_knowledge_password_authentication);
1701#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001702 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
1703 dump_cfg_fmtint(sKbdInteractiveAuthentication,
1704 o->kbd_interactive_authentication);
1705 dump_cfg_fmtint(sChallengeResponseAuthentication,
1706 o->challenge_response_authentication);
1707 dump_cfg_fmtint(sPrintMotd, o->print_motd);
1708 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
1709 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
1710 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
1711 dump_cfg_fmtint(sStrictModes, o->strict_modes);
1712 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
1713 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
1714 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
1715 dump_cfg_fmtint(sUseLogin, o->use_login);
1716 dump_cfg_fmtint(sCompression, o->compression);
1717 dump_cfg_fmtint(sGatewayPorts, o->gateway_ports);
1718 dump_cfg_fmtint(sUseDNS, o->use_dns);
1719 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
1720 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
1721
1722 /* string arguments */
1723 dump_cfg_string(sPidFile, o->pid_file);
1724 dump_cfg_string(sXAuthLocation, o->xauth_location);
1725 dump_cfg_string(sCiphers, o->ciphers);
1726 dump_cfg_string(sMacs, o->macs);
1727 dump_cfg_string(sBanner, o->banner);
1728 dump_cfg_string(sAuthorizedKeysFile, o->authorized_keys_file);
Darren Tuckere7140f22008-06-10 23:01:51 +10001729 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11001730 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11001731 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
1732 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10001733 dump_cfg_string(sAuthorizedPrincipalsFile,
1734 o->authorized_principals_file);
Darren Tuckere7140f22008-06-10 23:01:51 +10001735
1736 /* string arguments requiring a lookup */
1737 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
1738 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
1739
1740 /* string array arguments */
1741 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
1742 o->host_key_files);
Damien Miller0a80ca12010-02-27 07:55:05 +11001743 dump_cfg_strarray(sHostKeyFile, o->num_host_cert_files,
1744 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10001745 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
1746 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
1747 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
1748 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
1749 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
1750
1751 /* other arguments */
1752 for (i = 0; i < o->num_subsystems; i++)
1753 printf("subsystem %s %s\n", o->subsystem_name[i],
1754 o->subsystem_args[i]);
1755
1756 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
1757 o->max_startups_rate, o->max_startups);
1758
1759 for (i = 0; tunmode_desc[i].val != -1; i++)
1760 if (tunmode_desc[i].val == o->permit_tun) {
1761 s = tunmode_desc[i].text;
1762 break;
1763 }
1764 dump_cfg_string(sPermitTunnel, s);
1765
Damien Miller91475862011-05-05 14:14:34 +10001766 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
1767 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11001768
Darren Tuckere7140f22008-06-10 23:01:51 +10001769 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10001770}