blob: 03a503d0783ceec5892aecebe8b0b09c3d702cf8 [file] [log] [blame]
Damien Miller7c1b2c42011-05-15 08:51:05 +10001/* $OpenBSD: servconf.c,v 1.215 2011/05/11 04:47:06 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,
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000316 sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
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 },
433 { "authorizedkeysfile2", sAuthorizedKeysFile2, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000434 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Darren Tucker45150472006-07-12 22:34:17 +1000435 { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000436 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000437 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000438 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000439 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100440 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100441 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100442 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
443 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000444 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000445 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100446 { "ipqos", sIPQoS, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000447 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448};
449
Darren Tuckere7140f22008-06-10 23:01:51 +1000450static struct {
451 int val;
452 char *text;
453} tunmode_desc[] = {
454 { SSH_TUNMODE_NO, "no" },
455 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
456 { SSH_TUNMODE_ETHERNET, "ethernet" },
457 { SSH_TUNMODE_YES, "yes" },
458 { -1, NULL }
459};
460
Damien Miller5428f641999-11-25 11:54:57 +1100461/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000462 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100463 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464
Damien Miller4af51302000-04-16 11:18:38 +1000465static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100466parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000467 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000469 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470
Damien Miller95def091999-11-25 00:26:21 +1100471 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000472 if (strcasecmp(cp, keywords[i].name) == 0) {
473 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100474 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000475 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000477 error("%s: line %d: Bad configuration option: %s",
478 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100479 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480}
481
Darren Tucker88b6fb22010-01-13 22:44:29 +1100482char *
483derelativise_path(const char *path)
484{
Damien Miller44451d02010-03-26 10:40:04 +1100485 char *expanded, *ret, cwd[MAXPATHLEN];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100486
487 expanded = tilde_expand_filename(path, getuid());
488 if (*expanded == '/')
489 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100490 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100491 fatal("%s: getcwd: %s", __func__, strerror(errno));
492 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100493 xfree(expanded);
494 return ret;
495}
496
Ben Lindstrombba81212001-06-25 05:01:22 +0000497static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100498add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100499{
Damien Millereccb9de2005-06-17 12:59:34 +1000500 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100501
502 if (options->num_ports == 0)
503 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100504 if (options->address_family == -1)
505 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000506 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000507 for (i = 0; i < options->num_ports; i++)
508 add_one_listen_addr(options, addr, options->ports[i]);
509 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000510 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000511}
512
Ben Lindstrombba81212001-06-25 05:01:22 +0000513static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100514add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000515{
516 struct addrinfo hints, *ai, *aitop;
517 char strport[NI_MAXSERV];
518 int gaierr;
519
520 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100521 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000522 hints.ai_socktype = SOCK_STREAM;
523 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100524 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000525 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
526 fatal("bad addr or host: %s (%s)",
527 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100528 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000529 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
530 ;
531 ai->ai_next = options->listen_addrs;
532 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100533}
534
Darren Tucker45150472006-07-12 22:34:17 +1000535/*
536 * The strategy for the Match blocks is that the config file is parsed twice.
537 *
538 * The first time is at startup. activep is initialized to 1 and the
539 * directives in the global context are processed and acted on. Hitting a
540 * Match directive unsets activep and the directives inside the block are
541 * checked for syntax only.
542 *
543 * The second time is after a connection has been established but before
544 * authentication. activep is initialized to 2 and global config directives
545 * are ignored since they have already been processed. If the criteria in a
546 * Match block is met, activep is set and the subsequent directives
547 * processed and actioned until EOF or another Match block unsets it. Any
548 * options set are copied into the main server config.
549 *
550 * Potential additions/improvements:
551 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
552 *
553 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
554 * Match Address 192.168.0.*
555 * Tag trusted
556 * Match Group wheel
557 * Tag trusted
558 * Match Tag trusted
559 * AllowTcpForwarding yes
560 * GatewayPorts clientspecified
561 * [...]
562 *
563 * - Add a PermittedChannelRequests directive
564 * Match Group shell
565 * PermittedChannelRequests session,forwarded-tcpip
566 */
567
568static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000569match_cfg_line_group(const char *grps, int line, const char *user)
570{
571 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000572 struct passwd *pw;
573
Damien Miller565ca3f2006-08-19 00:23:15 +1000574 if (user == NULL)
575 goto out;
576
577 if ((pw = getpwnam(user)) == NULL) {
578 debug("Can't match group at line %d because user %.100s does "
579 "not exist", line, user);
580 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
581 debug("Can't Match group because user %.100s not in any group "
582 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000583 } else if (ga_match_pattern_list(grps) != 1) {
584 debug("user %.100s does not match group list %.100s at line %d",
585 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000586 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000587 debug("user %.100s matched group list %.100s at line %d", user,
588 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000589 result = 1;
590 }
591out:
592 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000593 return result;
594}
595
596static int
Darren Tucker45150472006-07-12 22:34:17 +1000597match_cfg_line(char **condition, int line, const char *user, const char *host,
598 const char *address)
599{
600 int result = 1;
601 char *arg, *attrib, *cp = *condition;
602 size_t len;
603
604 if (user == NULL)
605 debug3("checking syntax for 'Match %s'", cp);
606 else
607 debug3("checking match for '%s' user %s host %s addr %s", cp,
608 user ? user : "(null)", host ? host : "(null)",
609 address ? address : "(null)");
610
611 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
612 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
613 error("Missing Match criteria for %s", attrib);
614 return -1;
615 }
616 len = strlen(arg);
617 if (strcasecmp(attrib, "user") == 0) {
618 if (!user) {
619 result = 0;
620 continue;
621 }
622 if (match_pattern_list(user, arg, len, 0) != 1)
623 result = 0;
624 else
625 debug("user %.100s matched 'User %.100s' at "
626 "line %d", user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000627 } else if (strcasecmp(attrib, "group") == 0) {
628 switch (match_cfg_line_group(arg, line, user)) {
629 case -1:
630 return -1;
631 case 0:
632 result = 0;
633 }
Darren Tucker45150472006-07-12 22:34:17 +1000634 } else if (strcasecmp(attrib, "host") == 0) {
635 if (!host) {
636 result = 0;
637 continue;
638 }
639 if (match_hostname(host, arg, len) != 1)
640 result = 0;
641 else
642 debug("connection from %.100s matched 'Host "
643 "%.100s' at line %d", host, arg, line);
644 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000645 switch (addr_match_list(address, arg)) {
646 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000647 debug("connection from %.100s matched 'Address "
648 "%.100s' at line %d", address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000649 break;
650 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000651 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000652 result = 0;
653 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000654 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000655 return -1;
656 }
Darren Tucker45150472006-07-12 22:34:17 +1000657 } else {
658 error("Unsupported Match attribute %s", attrib);
659 return -1;
660 }
661 }
662 if (user != NULL)
663 debug3("match %sfound", result ? "" : "not ");
664 *condition = cp;
665 return result;
666}
667
Damien Millere2754432006-07-24 14:06:47 +1000668#define WHITESPACE " \t\r\n"
669
Ben Lindstromade03f62001-12-06 18:22:17 +0000670int
671process_server_config_line(ServerOptions *options, char *line,
Darren Tucker45150472006-07-12 22:34:17 +1000672 const char *filename, int linenum, int *activep, const char *user,
673 const char *host, const char *address)
Ben Lindstromade03f62001-12-06 18:22:17 +0000674{
675 char *cp, **charptr, *arg, *p;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100676 int cmdline = 0, *intptr, value, value2, n;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100677 SyslogFacility *log_facility_ptr;
678 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000679 ServerOpCodes opcode;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100680 int port;
Darren Tucker45150472006-07-12 22:34:17 +1000681 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000682 size_t len;
Ben Lindstromade03f62001-12-06 18:22:17 +0000683
684 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100685 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100686 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000687 /* Ignore leading whitespace */
688 if (*arg == '\0')
689 arg = strdelim(&cp);
690 if (!arg || !*arg || *arg == '#')
691 return 0;
692 intptr = NULL;
693 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000694 opcode = parse_token(arg, filename, linenum, &flags);
695
696 if (activep == NULL) { /* We are processing a command line directive */
697 cmdline = 1;
698 activep = &cmdline;
699 }
700 if (*activep && opcode != sMatch)
701 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
702 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
703 if (user == NULL) {
704 fatal("%s line %d: Directive '%s' is not allowed "
705 "within a Match block", filename, linenum, arg);
706 } else { /* this is a directive we have already processed */
707 while (arg)
708 arg = strdelim(&cp);
709 return 0;
710 }
711 }
712
Ben Lindstromade03f62001-12-06 18:22:17 +0000713 switch (opcode) {
714 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000715 case sUsePAM:
716 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000717 goto parse_flag;
718
719 /* Standard Options */
720 case sBadOption:
721 return -1;
722 case sPort:
723 /* ignore ports from configfile if cmdline specifies ports */
724 if (options->ports_from_cmdline)
725 return 0;
726 if (options->listen_addrs != NULL)
727 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100728 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000729 if (options->num_ports >= MAX_PORTS)
730 fatal("%s line %d: too many ports.",
731 filename, linenum);
732 arg = strdelim(&cp);
733 if (!arg || *arg == '\0')
734 fatal("%s line %d: missing port number.",
735 filename, linenum);
736 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100737 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000738 fatal("%s line %d: Badly formatted port number.",
739 filename, linenum);
740 break;
741
742 case sServerKeyBits:
743 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +1000744 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +0000745 arg = strdelim(&cp);
746 if (!arg || *arg == '\0')
747 fatal("%s line %d: missing integer value.",
748 filename, linenum);
749 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000750 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000751 *intptr = value;
752 break;
753
754 case sLoginGraceTime:
755 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +1000756 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +0000757 arg = strdelim(&cp);
758 if (!arg || *arg == '\0')
759 fatal("%s line %d: missing time value.",
760 filename, linenum);
761 if ((value = convtime(arg)) == -1)
762 fatal("%s line %d: invalid time value.",
763 filename, linenum);
764 if (*intptr == -1)
765 *intptr = value;
766 break;
767
768 case sKeyRegenerationTime:
769 intptr = &options->key_regeneration_time;
770 goto parse_time;
771
772 case sListenAddress:
773 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100774 if (arg == NULL || *arg == '\0')
775 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000776 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000777 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
778 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
779 && strchr(p+1, ':') != NULL) {
780 add_listen_addr(options, arg, 0);
781 break;
782 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100783 p = hpdelim(&arg);
784 if (p == NULL)
785 fatal("%s line %d: bad address:port usage",
786 filename, linenum);
787 p = cleanhostname(p);
788 if (arg == NULL)
789 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100790 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100791 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000792
Damien Millerf91ee4c2005-03-01 21:24:33 +1100793 add_listen_addr(options, p, port);
794
Ben Lindstromade03f62001-12-06 18:22:17 +0000795 break;
796
Darren Tucker0f383232005-01-20 10:57:56 +1100797 case sAddressFamily:
798 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000799 if (!arg || *arg == '\0')
800 fatal("%s line %d: missing address family.",
801 filename, linenum);
Darren Tucker0f383232005-01-20 10:57:56 +1100802 intptr = &options->address_family;
803 if (options->listen_addrs != NULL)
804 fatal("%s line %d: address family must be specified before "
805 "ListenAddress.", filename, linenum);
806 if (strcasecmp(arg, "inet") == 0)
807 value = AF_INET;
808 else if (strcasecmp(arg, "inet6") == 0)
809 value = AF_INET6;
810 else if (strcasecmp(arg, "any") == 0)
811 value = AF_UNSPEC;
812 else
813 fatal("%s line %d: unsupported address family \"%s\".",
814 filename, linenum, arg);
815 if (*intptr == -1)
816 *intptr = value;
817 break;
818
Ben Lindstromade03f62001-12-06 18:22:17 +0000819 case sHostKeyFile:
820 intptr = &options->num_host_key_files;
821 if (*intptr >= MAX_HOSTKEYS)
822 fatal("%s line %d: too many host keys specified (max %d).",
823 filename, linenum, MAX_HOSTKEYS);
824 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +1000825 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +0000826 arg = strdelim(&cp);
827 if (!arg || *arg == '\0')
828 fatal("%s line %d: missing file name.",
829 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +1000830 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +1100831 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000832 /* increase optional counter */
833 if (intptr != NULL)
834 *intptr = *intptr + 1;
835 }
836 break;
837
Damien Miller0a80ca12010-02-27 07:55:05 +1100838 case sHostCertificate:
839 intptr = &options->num_host_cert_files;
840 if (*intptr >= MAX_HOSTKEYS)
841 fatal("%s line %d: too many host certificates "
842 "specified (max %d).", filename, linenum,
843 MAX_HOSTCERTS);
844 charptr = &options->host_cert_files[*intptr];
845 goto parse_filename;
846 break;
847
Ben Lindstromade03f62001-12-06 18:22:17 +0000848 case sPidFile:
849 charptr = &options->pid_file;
850 goto parse_filename;
851
852 case sPermitRootLogin:
853 intptr = &options->permit_root_login;
854 arg = strdelim(&cp);
855 if (!arg || *arg == '\0')
856 fatal("%s line %d: missing yes/"
857 "without-password/forced-commands-only/no "
858 "argument.", filename, linenum);
859 value = 0; /* silence compiler */
860 if (strcmp(arg, "without-password") == 0)
861 value = PERMIT_NO_PASSWD;
862 else if (strcmp(arg, "forced-commands-only") == 0)
863 value = PERMIT_FORCED_ONLY;
864 else if (strcmp(arg, "yes") == 0)
865 value = PERMIT_YES;
866 else if (strcmp(arg, "no") == 0)
867 value = PERMIT_NO;
868 else
869 fatal("%s line %d: Bad yes/"
870 "without-password/forced-commands-only/no "
871 "argument: %s", filename, linenum, arg);
Darren Tucker15f94272008-01-01 20:36:56 +1100872 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000873 *intptr = value;
874 break;
875
876 case sIgnoreRhosts:
877 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +1000878 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +0000879 arg = strdelim(&cp);
880 if (!arg || *arg == '\0')
881 fatal("%s line %d: missing yes/no argument.",
882 filename, linenum);
883 value = 0; /* silence compiler */
884 if (strcmp(arg, "yes") == 0)
885 value = 1;
886 else if (strcmp(arg, "no") == 0)
887 value = 0;
888 else
889 fatal("%s line %d: Bad yes/no argument: %s",
890 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +1000891 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000892 *intptr = value;
893 break;
894
895 case sIgnoreUserKnownHosts:
896 intptr = &options->ignore_user_known_hosts;
897 goto parse_flag;
898
Ben Lindstromade03f62001-12-06 18:22:17 +0000899 case sRhostsRSAAuthentication:
900 intptr = &options->rhosts_rsa_authentication;
901 goto parse_flag;
902
903 case sHostbasedAuthentication:
904 intptr = &options->hostbased_authentication;
905 goto parse_flag;
906
907 case sHostbasedUsesNameFromPacketOnly:
908 intptr = &options->hostbased_uses_name_from_packet_only;
909 goto parse_flag;
910
911 case sRSAAuthentication:
912 intptr = &options->rsa_authentication;
913 goto parse_flag;
914
915 case sPubkeyAuthentication:
916 intptr = &options->pubkey_authentication;
917 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000918
Ben Lindstromade03f62001-12-06 18:22:17 +0000919 case sKerberosAuthentication:
920 intptr = &options->kerberos_authentication;
921 goto parse_flag;
922
923 case sKerberosOrLocalPasswd:
924 intptr = &options->kerberos_or_local_passwd;
925 goto parse_flag;
926
927 case sKerberosTicketCleanup:
928 intptr = &options->kerberos_ticket_cleanup;
929 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000930
Darren Tucker22ef5082003-12-31 11:37:34 +1100931 case sKerberosGetAFSToken:
932 intptr = &options->kerberos_get_afs_token;
933 goto parse_flag;
934
Darren Tucker0efd1552003-08-26 11:49:55 +1000935 case sGssAuthentication:
936 intptr = &options->gss_authentication;
937 goto parse_flag;
938
939 case sGssCleanupCreds:
940 intptr = &options->gss_cleanup_creds;
941 goto parse_flag;
942
Ben Lindstromade03f62001-12-06 18:22:17 +0000943 case sPasswordAuthentication:
944 intptr = &options->password_authentication;
945 goto parse_flag;
946
Damien Miller01ed2272008-11-05 16:20:46 +1100947 case sZeroKnowledgePasswordAuthentication:
948 intptr = &options->zero_knowledge_password_authentication;
949 goto parse_flag;
950
Ben Lindstromade03f62001-12-06 18:22:17 +0000951 case sKbdInteractiveAuthentication:
952 intptr = &options->kbd_interactive_authentication;
953 goto parse_flag;
954
955 case sChallengeResponseAuthentication:
956 intptr = &options->challenge_response_authentication;
957 goto parse_flag;
958
959 case sPrintMotd:
960 intptr = &options->print_motd;
961 goto parse_flag;
962
963 case sPrintLastLog:
964 intptr = &options->print_lastlog;
965 goto parse_flag;
966
967 case sX11Forwarding:
968 intptr = &options->x11_forwarding;
969 goto parse_flag;
970
971 case sX11DisplayOffset:
972 intptr = &options->x11_display_offset;
973 goto parse_int;
974
Damien Miller95c249f2002-02-05 12:11:34 +1100975 case sX11UseLocalhost:
976 intptr = &options->x11_use_localhost;
977 goto parse_flag;
978
Ben Lindstromade03f62001-12-06 18:22:17 +0000979 case sXAuthLocation:
980 charptr = &options->xauth_location;
981 goto parse_filename;
982
983 case sStrictModes:
984 intptr = &options->strict_modes;
985 goto parse_flag;
986
Damien Miller12c150e2003-12-17 16:31:10 +1100987 case sTCPKeepAlive:
988 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +0000989 goto parse_flag;
990
991 case sEmptyPasswd:
992 intptr = &options->permit_empty_passwd;
993 goto parse_flag;
994
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000995 case sPermitUserEnvironment:
996 intptr = &options->permit_user_env;
997 goto parse_flag;
998
Ben Lindstromade03f62001-12-06 18:22:17 +0000999 case sUseLogin:
1000 intptr = &options->use_login;
1001 goto parse_flag;
1002
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001003 case sCompression:
1004 intptr = &options->compression;
Damien Miller9786e6e2005-07-26 21:54:56 +10001005 arg = strdelim(&cp);
1006 if (!arg || *arg == '\0')
1007 fatal("%s line %d: missing yes/no/delayed "
1008 "argument.", filename, linenum);
1009 value = 0; /* silence compiler */
1010 if (strcmp(arg, "delayed") == 0)
1011 value = COMP_DELAYED;
1012 else if (strcmp(arg, "yes") == 0)
1013 value = COMP_ZLIB;
1014 else if (strcmp(arg, "no") == 0)
1015 value = COMP_NONE;
1016 else
1017 fatal("%s line %d: Bad yes/no/delayed "
1018 "argument: %s", filename, linenum, arg);
1019 if (*intptr == -1)
1020 *intptr = value;
1021 break;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001022
Ben Lindstromade03f62001-12-06 18:22:17 +00001023 case sGatewayPorts:
1024 intptr = &options->gateway_ports;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001025 arg = strdelim(&cp);
1026 if (!arg || *arg == '\0')
1027 fatal("%s line %d: missing yes/no/clientspecified "
1028 "argument.", filename, linenum);
1029 value = 0; /* silence compiler */
1030 if (strcmp(arg, "clientspecified") == 0)
1031 value = 2;
1032 else if (strcmp(arg, "yes") == 0)
1033 value = 1;
1034 else if (strcmp(arg, "no") == 0)
1035 value = 0;
1036 else
1037 fatal("%s line %d: Bad yes/no/clientspecified "
1038 "argument: %s", filename, linenum, arg);
Darren Tucker82347a82007-02-25 20:37:52 +11001039 if (*activep && *intptr == -1)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001040 *intptr = value;
1041 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001042
Damien Miller3a961dc2003-06-03 10:25:48 +10001043 case sUseDNS:
1044 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001045 goto parse_flag;
1046
1047 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001048 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001049 arg = strdelim(&cp);
1050 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001051 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001052 fatal("%.200s line %d: unsupported log facility '%s'",
1053 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001054 if (*log_facility_ptr == -1)
1055 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001056 break;
1057
1058 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001059 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001060 arg = strdelim(&cp);
1061 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001062 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001063 fatal("%.200s line %d: unsupported log level '%s'",
1064 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001065 if (*log_level_ptr == -1)
1066 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001067 break;
1068
1069 case sAllowTcpForwarding:
1070 intptr = &options->allow_tcp_forwarding;
1071 goto parse_flag;
1072
Damien Miller4f755cd2008-05-19 14:57:41 +10001073 case sAllowAgentForwarding:
1074 intptr = &options->allow_agent_forwarding;
1075 goto parse_flag;
1076
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001077 case sUsePrivilegeSeparation:
1078 intptr = &use_privsep;
1079 goto parse_flag;
1080
Ben Lindstromade03f62001-12-06 18:22:17 +00001081 case sAllowUsers:
1082 while ((arg = strdelim(&cp)) && *arg != '\0') {
1083 if (options->num_allow_users >= MAX_ALLOW_USERS)
1084 fatal("%s line %d: too many allow users.",
1085 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001086 options->allow_users[options->num_allow_users++] =
1087 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001088 }
1089 break;
1090
1091 case sDenyUsers:
1092 while ((arg = strdelim(&cp)) && *arg != '\0') {
1093 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001094 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001095 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001096 options->deny_users[options->num_deny_users++] =
1097 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001098 }
1099 break;
1100
1101 case sAllowGroups:
1102 while ((arg = strdelim(&cp)) && *arg != '\0') {
1103 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1104 fatal("%s line %d: too many allow groups.",
1105 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001106 options->allow_groups[options->num_allow_groups++] =
1107 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001108 }
1109 break;
1110
1111 case sDenyGroups:
1112 while ((arg = strdelim(&cp)) && *arg != '\0') {
1113 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1114 fatal("%s line %d: too many deny groups.",
1115 filename, linenum);
1116 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
1117 }
1118 break;
1119
1120 case sCiphers:
1121 arg = strdelim(&cp);
1122 if (!arg || *arg == '\0')
1123 fatal("%s line %d: Missing argument.", filename, linenum);
1124 if (!ciphers_valid(arg))
1125 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1126 filename, linenum, arg ? arg : "<NONE>");
1127 if (options->ciphers == NULL)
1128 options->ciphers = xstrdup(arg);
1129 break;
1130
1131 case sMacs:
1132 arg = strdelim(&cp);
1133 if (!arg || *arg == '\0')
1134 fatal("%s line %d: Missing argument.", filename, linenum);
1135 if (!mac_valid(arg))
1136 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1137 filename, linenum, arg ? arg : "<NONE>");
1138 if (options->macs == NULL)
1139 options->macs = xstrdup(arg);
1140 break;
1141
Damien Millerd5f62bf2010-09-24 22:11:14 +10001142 case sKexAlgorithms:
1143 arg = strdelim(&cp);
1144 if (!arg || *arg == '\0')
1145 fatal("%s line %d: Missing argument.",
1146 filename, linenum);
1147 if (!kex_names_valid(arg))
1148 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1149 filename, linenum, arg ? arg : "<NONE>");
1150 if (options->kex_algorithms == NULL)
1151 options->kex_algorithms = xstrdup(arg);
1152 break;
1153
Ben Lindstromade03f62001-12-06 18:22:17 +00001154 case sProtocol:
1155 intptr = &options->protocol;
1156 arg = strdelim(&cp);
1157 if (!arg || *arg == '\0')
1158 fatal("%s line %d: Missing argument.", filename, linenum);
1159 value = proto_spec(arg);
1160 if (value == SSH_PROTO_UNKNOWN)
1161 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001162 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001163 if (*intptr == SSH_PROTO_UNKNOWN)
1164 *intptr = value;
1165 break;
1166
1167 case sSubsystem:
1168 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1169 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001170 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001171 }
1172 arg = strdelim(&cp);
1173 if (!arg || *arg == '\0')
1174 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001175 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001176 if (!*activep) {
1177 arg = strdelim(&cp);
1178 break;
1179 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001180 for (i = 0; i < options->num_subsystems; i++)
1181 if (strcmp(arg, options->subsystem_name[i]) == 0)
1182 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001183 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001184 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1185 arg = strdelim(&cp);
1186 if (!arg || *arg == '\0')
1187 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001188 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001189 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001190
1191 /* Collect arguments (separate to executable) */
1192 p = xstrdup(arg);
1193 len = strlen(p) + 1;
1194 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1195 len += 1 + strlen(arg);
1196 p = xrealloc(p, 1, len);
1197 strlcat(p, " ", len);
1198 strlcat(p, arg, len);
1199 }
1200 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001201 options->num_subsystems++;
1202 break;
1203
1204 case sMaxStartups:
1205 arg = strdelim(&cp);
1206 if (!arg || *arg == '\0')
1207 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001208 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001209 if ((n = sscanf(arg, "%d:%d:%d",
1210 &options->max_startups_begin,
1211 &options->max_startups_rate,
1212 &options->max_startups)) == 3) {
1213 if (options->max_startups_begin >
1214 options->max_startups ||
1215 options->max_startups_rate > 100 ||
1216 options->max_startups_rate < 1)
1217 fatal("%s line %d: Illegal MaxStartups spec.",
1218 filename, linenum);
1219 } else if (n != 1)
1220 fatal("%s line %d: Illegal MaxStartups spec.",
1221 filename, linenum);
1222 else
1223 options->max_startups = options->max_startups_begin;
1224 break;
1225
Darren Tucker89413db2004-05-24 10:36:23 +10001226 case sMaxAuthTries:
1227 intptr = &options->max_authtries;
1228 goto parse_int;
1229
Damien Miller7207f642008-05-19 15:34:50 +10001230 case sMaxSessions:
1231 intptr = &options->max_sessions;
1232 goto parse_int;
1233
Ben Lindstromade03f62001-12-06 18:22:17 +00001234 case sBanner:
1235 charptr = &options->banner;
1236 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001237
Ben Lindstromade03f62001-12-06 18:22:17 +00001238 /*
1239 * These options can contain %X options expanded at
1240 * connect time, so that you can specify paths like:
1241 *
1242 * AuthorizedKeysFile /etc/ssh_keys/%u
1243 */
1244 case sAuthorizedKeysFile:
Damien Miller30da3442010-05-10 11:58:03 +10001245 charptr = &options->authorized_keys_file;
1246 goto parse_tilde_filename;
Damien Miller30da3442010-05-10 11:58:03 +10001247 case sAuthorizedPrincipalsFile:
1248 charptr = &options->authorized_principals_file;
1249 parse_tilde_filename:
Damien Millerc4cb47b2010-03-22 05:52:26 +11001250 arg = strdelim(&cp);
1251 if (!arg || *arg == '\0')
1252 fatal("%s line %d: missing file name.",
1253 filename, linenum);
1254 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001255 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001256 /* increase optional counter */
1257 if (intptr != NULL)
1258 *intptr = *intptr + 1;
1259 }
1260 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001261
1262 case sClientAliveInterval:
1263 intptr = &options->client_alive_interval;
1264 goto parse_time;
1265
1266 case sClientAliveCountMax:
1267 intptr = &options->client_alive_count_max;
1268 goto parse_int;
1269
Darren Tucker46bc0752004-05-02 22:11:30 +10001270 case sAcceptEnv:
1271 while ((arg = strdelim(&cp)) && *arg != '\0') {
1272 if (strchr(arg, '=') != NULL)
1273 fatal("%s line %d: Invalid environment name.",
1274 filename, linenum);
1275 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1276 fatal("%s line %d: too many allow env.",
1277 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001278 if (!*activep)
1279 break;
Darren Tucker46bc0752004-05-02 22:11:30 +10001280 options->accept_env[options->num_accept_env++] =
1281 xstrdup(arg);
1282 }
1283 break;
1284
Damien Millerd27b9472005-12-13 19:29:02 +11001285 case sPermitTunnel:
1286 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001287 arg = strdelim(&cp);
1288 if (!arg || *arg == '\0')
1289 fatal("%s line %d: Missing yes/point-to-point/"
1290 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001291 value = -1;
1292 for (i = 0; tunmode_desc[i].val != -1; i++)
1293 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1294 value = tunmode_desc[i].val;
1295 break;
1296 }
1297 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001298 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1299 "no argument: %s", filename, linenum, arg);
1300 if (*intptr == -1)
1301 *intptr = value;
1302 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001303
Darren Tucker45150472006-07-12 22:34:17 +10001304 case sMatch:
1305 if (cmdline)
1306 fatal("Match directive not supported as a command-line "
1307 "option");
1308 value = match_cfg_line(&cp, linenum, user, host, address);
1309 if (value < 0)
1310 fatal("%s line %d: Bad Match condition", filename,
1311 linenum);
1312 *activep = value;
1313 break;
1314
Damien Miller9b439df2006-07-24 14:04:00 +10001315 case sPermitOpen:
1316 arg = strdelim(&cp);
1317 if (!arg || *arg == '\0')
1318 fatal("%s line %d: missing PermitOpen specification",
1319 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001320 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001321 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001322 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001323 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001324 options->num_permitted_opens = 0;
1325 }
Damien Miller9b439df2006-07-24 14:04:00 +10001326 break;
1327 }
Damien Millera29b95e2007-01-05 16:28:36 +11001328 if (*activep && n == -1)
1329 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001330 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1331 p = hpdelim(&arg);
1332 if (p == NULL)
1333 fatal("%s line %d: missing host in PermitOpen",
1334 filename, linenum);
1335 p = cleanhostname(p);
Damien Miller3dc71ad2009-01-28 16:31:22 +11001336 if (arg == NULL || (port = a2port(arg)) <= 0)
Damien Millera765cf42006-07-24 14:08:13 +10001337 fatal("%s line %d: bad port number in "
1338 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001339 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001340 options->num_permitted_opens =
1341 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001342 }
Damien Miller9b439df2006-07-24 14:04:00 +10001343 break;
1344
Damien Millere2754432006-07-24 14:06:47 +10001345 case sForceCommand:
1346 if (cp == NULL)
1347 fatal("%.200s line %d: Missing argument.", filename,
1348 linenum);
1349 len = strspn(cp, WHITESPACE);
1350 if (*activep && options->adm_forced_command == NULL)
1351 options->adm_forced_command = xstrdup(cp + len);
1352 return 0;
1353
Damien Millerd8cb1f12008-02-10 22:40:12 +11001354 case sChrootDirectory:
1355 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001356
1357 arg = strdelim(&cp);
1358 if (!arg || *arg == '\0')
1359 fatal("%s line %d: missing file name.",
1360 filename, linenum);
1361 if (*activep && *charptr == NULL)
1362 *charptr = xstrdup(arg);
1363 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001364
Damien Miller1aed65e2010-03-04 21:53:35 +11001365 case sTrustedUserCAKeys:
1366 charptr = &options->trusted_user_ca_keys;
1367 goto parse_filename;
1368
1369 case sRevokedKeys:
1370 charptr = &options->revoked_keys_file;
1371 goto parse_filename;
1372
Damien Miller0dac6fb2010-11-20 15:19:38 +11001373 case sIPQoS:
1374 arg = strdelim(&cp);
1375 if ((value = parse_ipqos(arg)) == -1)
1376 fatal("%s line %d: Bad IPQoS value: %s",
1377 filename, linenum, arg);
1378 arg = strdelim(&cp);
1379 if (arg == NULL)
1380 value2 = value;
1381 else if ((value2 = parse_ipqos(arg)) == -1)
1382 fatal("%s line %d: Bad IPQoS value: %s",
1383 filename, linenum, arg);
1384 if (*activep) {
1385 options->ip_qos_interactive = value;
1386 options->ip_qos_bulk = value2;
1387 }
1388 break;
1389
Ben Lindstromade03f62001-12-06 18:22:17 +00001390 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001391 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001392 filename, linenum, arg);
1393 while (arg)
1394 arg = strdelim(&cp);
1395 break;
1396
Damien Millerf9b3feb2003-05-16 11:38:32 +10001397 case sUnsupported:
1398 logit("%s line %d: Unsupported option %s",
1399 filename, linenum, arg);
1400 while (arg)
1401 arg = strdelim(&cp);
1402 break;
1403
Ben Lindstromade03f62001-12-06 18:22:17 +00001404 default:
1405 fatal("%s line %d: Missing handler for opcode %s (%d)",
1406 filename, linenum, arg, opcode);
1407 }
1408 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1409 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1410 filename, linenum, arg);
1411 return 0;
1412}
1413
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001414/* Reads the server configuration file. */
1415
Damien Miller4af51302000-04-16 11:18:38 +10001416void
Darren Tucker645ab752004-06-25 13:33:20 +10001417load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001418{
Darren Tucker645ab752004-06-25 13:33:20 +10001419 char line[1024], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001420 FILE *f;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001421
Darren Tucker645ab752004-06-25 13:33:20 +10001422 debug2("%s: filename %s", __func__, filename);
1423 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001424 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001425 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001426 }
Darren Tucker645ab752004-06-25 13:33:20 +10001427 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001428 while (fgets(line, sizeof(line), f)) {
Darren Tucker645ab752004-06-25 13:33:20 +10001429 /*
1430 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001431 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001432 * line numbers later for error messages
1433 */
1434 if ((cp = strchr(line, '#')) != NULL)
1435 memcpy(cp, "\n", 2);
1436 cp = line + strspn(line, " \t\r");
1437
1438 buffer_append(conf, cp, strlen(cp));
1439 }
1440 buffer_append(conf, "\0", 1);
1441 fclose(f);
1442 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1443}
1444
1445void
Darren Tucker45150472006-07-12 22:34:17 +10001446parse_server_match_config(ServerOptions *options, const char *user,
1447 const char *host, const char *address)
Darren Tucker645ab752004-06-25 13:33:20 +10001448{
Darren Tucker45150472006-07-12 22:34:17 +10001449 ServerOptions mo;
1450
1451 initialize_server_options(&mo);
1452 parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
Darren Tucker1629c072007-02-19 22:25:37 +11001453 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001454}
1455
Darren Tucker1629c072007-02-19 22:25:37 +11001456/* Helper macros */
1457#define M_CP_INTOPT(n) do {\
1458 if (src->n != -1) \
1459 dst->n = src->n; \
1460} while (0)
1461#define M_CP_STROPT(n) do {\
1462 if (src->n != NULL) { \
1463 if (dst->n != NULL) \
1464 xfree(dst->n); \
1465 dst->n = src->n; \
1466 } \
1467} while(0)
1468
1469/*
1470 * Copy any supported values that are set.
1471 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001472 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001473 * array values that are not used pre-authentication, because any that we
1474 * do use must be explictly sent in mm_getpwnamallow().
1475 */
Darren Tucker45150472006-07-12 22:34:17 +10001476void
Darren Tucker1629c072007-02-19 22:25:37 +11001477copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001478{
Darren Tucker1629c072007-02-19 22:25:37 +11001479 M_CP_INTOPT(password_authentication);
1480 M_CP_INTOPT(gss_authentication);
1481 M_CP_INTOPT(rsa_authentication);
1482 M_CP_INTOPT(pubkey_authentication);
1483 M_CP_INTOPT(kerberos_authentication);
1484 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001485 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001486 M_CP_INTOPT(kbd_interactive_authentication);
Damien Miller01ed2272008-11-05 16:20:46 +11001487 M_CP_INTOPT(zero_knowledge_password_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001488 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001489 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001490
1491 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001492 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001493 M_CP_INTOPT(permit_tun);
Darren Tucker1629c072007-02-19 22:25:37 +11001494 M_CP_INTOPT(gateway_ports);
1495 M_CP_INTOPT(x11_display_offset);
1496 M_CP_INTOPT(x11_forwarding);
1497 M_CP_INTOPT(x11_use_localhost);
Damien Miller7207f642008-05-19 15:34:50 +10001498 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001499 M_CP_INTOPT(max_authtries);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001500 M_CP_INTOPT(ip_qos_interactive);
1501 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker1629c072007-02-19 22:25:37 +11001502
1503 M_CP_STROPT(banner);
1504 if (preauth)
1505 return;
1506 M_CP_STROPT(adm_forced_command);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001507 M_CP_STROPT(chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11001508 M_CP_STROPT(trusted_user_ca_keys);
1509 M_CP_STROPT(revoked_keys_file);
Damien Millerab6de352010-06-26 09:38:45 +10001510 M_CP_STROPT(authorized_keys_file);
Damien Millerab6de352010-06-26 09:38:45 +10001511 M_CP_STROPT(authorized_principals_file);
Darren Tucker45150472006-07-12 22:34:17 +10001512}
1513
Darren Tucker1629c072007-02-19 22:25:37 +11001514#undef M_CP_INTOPT
1515#undef M_CP_STROPT
1516
Darren Tucker45150472006-07-12 22:34:17 +10001517void
1518parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1519 const char *user, const char *host, const char *address)
1520{
1521 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001522 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001523
1524 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1525
Darren Tucker9fbac712004-08-12 22:41:44 +10001526 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tucker45150472006-07-12 22:34:17 +10001527 active = user ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001528 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001529 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001530 if (process_server_config_line(options, cp, filename,
Darren Tucker45150472006-07-12 22:34:17 +10001531 linenum++, &active, user, host, address) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001532 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001533 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001534 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001535 if (bad_options > 0)
1536 fatal("%s: terminating, %d bad configuration options",
1537 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001538}
Darren Tuckere7140f22008-06-10 23:01:51 +10001539
1540static const char *
1541fmt_intarg(ServerOpCodes code, int val)
1542{
1543 if (code == sAddressFamily) {
1544 switch (val) {
1545 case AF_INET:
1546 return "inet";
1547 case AF_INET6:
1548 return "inet6";
1549 case AF_UNSPEC:
1550 return "any";
1551 default:
1552 return "UNKNOWN";
1553 }
1554 }
1555 if (code == sPermitRootLogin) {
1556 switch (val) {
1557 case PERMIT_NO_PASSWD:
Darren Tuckerff4350e2008-11-11 16:31:05 +11001558 return "without-password";
Darren Tuckere7140f22008-06-10 23:01:51 +10001559 case PERMIT_FORCED_ONLY:
1560 return "forced-commands-only";
1561 case PERMIT_YES:
1562 return "yes";
1563 }
1564 }
1565 if (code == sProtocol) {
1566 switch (val) {
1567 case SSH_PROTO_1:
1568 return "1";
1569 case SSH_PROTO_2:
1570 return "2";
1571 case (SSH_PROTO_1|SSH_PROTO_2):
1572 return "2,1";
1573 default:
1574 return "UNKNOWN";
1575 }
1576 }
1577 if (code == sGatewayPorts && val == 2)
1578 return "clientspecified";
1579 if (code == sCompression && val == COMP_DELAYED)
1580 return "delayed";
1581 switch (val) {
1582 case -1:
1583 return "unset";
1584 case 0:
1585 return "no";
1586 case 1:
1587 return "yes";
1588 }
1589 return "UNKNOWN";
1590}
1591
1592static const char *
1593lookup_opcode_name(ServerOpCodes code)
1594{
1595 u_int i;
1596
1597 for (i = 0; keywords[i].name != NULL; i++)
1598 if (keywords[i].opcode == code)
1599 return(keywords[i].name);
1600 return "UNKNOWN";
1601}
1602
1603static void
1604dump_cfg_int(ServerOpCodes code, int val)
1605{
1606 printf("%s %d\n", lookup_opcode_name(code), val);
1607}
1608
1609static void
1610dump_cfg_fmtint(ServerOpCodes code, int val)
1611{
1612 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
1613}
1614
1615static void
1616dump_cfg_string(ServerOpCodes code, const char *val)
1617{
1618 if (val == NULL)
1619 return;
1620 printf("%s %s\n", lookup_opcode_name(code), val);
1621}
1622
1623static void
1624dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
1625{
1626 u_int i;
1627
1628 for (i = 0; i < count; i++)
1629 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
1630}
1631
1632void
1633dump_config(ServerOptions *o)
1634{
1635 u_int i;
1636 int ret;
1637 struct addrinfo *ai;
1638 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
1639
1640 /* these are usually at the top of the config */
1641 for (i = 0; i < o->num_ports; i++)
1642 printf("port %d\n", o->ports[i]);
1643 dump_cfg_fmtint(sProtocol, o->protocol);
1644 dump_cfg_fmtint(sAddressFamily, o->address_family);
1645
1646 /* ListenAddress must be after Port */
1647 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
1648 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1649 sizeof(addr), port, sizeof(port),
1650 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1651 error("getnameinfo failed: %.100s",
1652 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
1653 strerror(errno));
1654 } else {
1655 if (ai->ai_family == AF_INET6)
1656 printf("listenaddress [%s]:%s\n", addr, port);
1657 else
1658 printf("listenaddress %s:%s\n", addr, port);
1659 }
1660 }
1661
1662 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10001663#ifdef USE_PAM
1664 dump_cfg_int(sUsePAM, o->use_pam);
1665#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001666 dump_cfg_int(sServerKeyBits, o->server_key_bits);
1667 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
1668 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
1669 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
1670 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11001671 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10001672 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
1673 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
1674
1675 /* formatted integer arguments */
1676 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
1677 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
1678 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
1679 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
1680 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
1681 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
1682 o->hostbased_uses_name_from_packet_only);
1683 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
1684 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10001685#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10001686 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
1687 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
1688 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10001689# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10001690 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10001691# endif
1692#endif
1693#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10001694 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
1695 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10001696#endif
Damien Miller01ed2272008-11-05 16:20:46 +11001697#ifdef JPAKE
1698 dump_cfg_fmtint(sZeroKnowledgePasswordAuthentication,
1699 o->zero_knowledge_password_authentication);
1700#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001701 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
1702 dump_cfg_fmtint(sKbdInteractiveAuthentication,
1703 o->kbd_interactive_authentication);
1704 dump_cfg_fmtint(sChallengeResponseAuthentication,
1705 o->challenge_response_authentication);
1706 dump_cfg_fmtint(sPrintMotd, o->print_motd);
1707 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
1708 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
1709 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
1710 dump_cfg_fmtint(sStrictModes, o->strict_modes);
1711 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
1712 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
1713 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
1714 dump_cfg_fmtint(sUseLogin, o->use_login);
1715 dump_cfg_fmtint(sCompression, o->compression);
1716 dump_cfg_fmtint(sGatewayPorts, o->gateway_ports);
1717 dump_cfg_fmtint(sUseDNS, o->use_dns);
1718 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
1719 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
1720
1721 /* string arguments */
1722 dump_cfg_string(sPidFile, o->pid_file);
1723 dump_cfg_string(sXAuthLocation, o->xauth_location);
1724 dump_cfg_string(sCiphers, o->ciphers);
1725 dump_cfg_string(sMacs, o->macs);
1726 dump_cfg_string(sBanner, o->banner);
1727 dump_cfg_string(sAuthorizedKeysFile, o->authorized_keys_file);
Darren Tuckere7140f22008-06-10 23:01:51 +10001728 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11001729 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11001730 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
1731 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10001732 dump_cfg_string(sAuthorizedPrincipalsFile,
1733 o->authorized_principals_file);
Darren Tuckere7140f22008-06-10 23:01:51 +10001734
1735 /* string arguments requiring a lookup */
1736 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
1737 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
1738
1739 /* string array arguments */
1740 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
1741 o->host_key_files);
Damien Miller0a80ca12010-02-27 07:55:05 +11001742 dump_cfg_strarray(sHostKeyFile, o->num_host_cert_files,
1743 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10001744 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
1745 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
1746 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
1747 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
1748 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
1749
1750 /* other arguments */
1751 for (i = 0; i < o->num_subsystems; i++)
1752 printf("subsystem %s %s\n", o->subsystem_name[i],
1753 o->subsystem_args[i]);
1754
1755 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
1756 o->max_startups_rate, o->max_startups);
1757
1758 for (i = 0; tunmode_desc[i].val != -1; i++)
1759 if (tunmode_desc[i].val == o->permit_tun) {
1760 s = tunmode_desc[i].text;
1761 break;
1762 }
1763 dump_cfg_string(sPermitTunnel, s);
1764
Damien Miller91475862011-05-05 14:14:34 +10001765 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
1766 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11001767
Darren Tuckere7140f22008-06-10 23:01:51 +10001768 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10001769}