blob: 48cb0d5b1984bb522f082ae9b75e1de572aec74a [file] [log] [blame]
Damien Miller91475862011-05-05 14:14:34 +10001/* $OpenBSD: servconf.c,v 1.214 2011/03/29 18:54:17 stevesk 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;
130 options->authorized_keys_file2 = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +1000131 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100132 options->permit_tun = -1;
Damien Millera765cf42006-07-24 14:08:13 +1000133 options->num_permitted_opens = -1;
Damien Millere2754432006-07-24 14:06:47 +1000134 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100135 options->chroot_directory = NULL;
Damien Miller01ed2272008-11-05 16:20:46 +1100136 options->zero_knowledge_password_authentication = -1;
Damien Miller1aed65e2010-03-04 21:53:35 +1100137 options->revoked_keys_file = NULL;
138 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000139 options->authorized_principals_file = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100140 options->ip_qos_interactive = -1;
141 options->ip_qos_bulk = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142}
143
Damien Miller4af51302000-04-16 11:18:38 +1000144void
Damien Miller95def091999-11-25 00:26:21 +1100145fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146{
Damien Miller726273e2001-11-12 11:40:11 +1100147 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000148 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000149 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100150
151 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100152 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +1100153 options->protocol = SSH_PROTO_2;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100154 if (options->num_host_key_files == 0) {
155 /* fill default hostkeys for protocols */
156 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100157 options->host_key_files[options->num_host_key_files++] =
158 _PATH_HOST_KEY_FILE;
159 if (options->protocol & SSH_PROTO_2) {
160 options->host_key_files[options->num_host_key_files++] =
161 _PATH_HOST_RSA_KEY_FILE;
162 options->host_key_files[options->num_host_key_files++] =
163 _PATH_HOST_DSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100164#ifdef OPENSSL_HAS_ECC
Damien Millere13cadf2010-09-10 11:15:33 +1000165 options->host_key_files[options->num_host_key_files++] =
166 _PATH_HOST_ECDSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100167#endif
Damien Miller7fc23732002-01-22 23:19:11 +1100168 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100169 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100170 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100171 if (options->num_ports == 0)
172 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
173 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000174 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000175 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000176 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100177 if (options->server_key_bits == -1)
Darren Tucker7499b0c2008-07-02 22:35:43 +1000178 options->server_key_bits = 1024;
Damien Miller95def091999-11-25 00:26:21 +1100179 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000180 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100181 if (options->key_regeneration_time == -1)
182 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000183 if (options->permit_root_login == PERMIT_NOT_SET)
184 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100185 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100186 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100187 if (options->ignore_user_known_hosts == -1)
188 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100189 if (options->print_motd == -1)
190 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000191 if (options->print_lastlog == -1)
192 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100193 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100194 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100195 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100196 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100197 if (options->x11_use_localhost == -1)
198 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000199 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000200 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100201 if (options->strict_modes == -1)
202 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100203 if (options->tcp_keep_alive == -1)
204 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100205 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100206 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100207 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000208 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100209 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100210 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000211 if (options->hostbased_authentication == -1)
212 options->hostbased_authentication = 0;
213 if (options->hostbased_uses_name_from_packet_only == -1)
214 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100215 if (options->rsa_authentication == -1)
216 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100217 if (options->pubkey_authentication == -1)
218 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100219 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000220 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100221 if (options->kerberos_or_local_passwd == -1)
222 options->kerberos_or_local_passwd = 1;
223 if (options->kerberos_ticket_cleanup == -1)
224 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100225 if (options->kerberos_get_afs_token == -1)
226 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000227 if (options->gss_authentication == -1)
228 options->gss_authentication = 0;
229 if (options->gss_cleanup_creds == -1)
230 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100231 if (options->password_authentication == -1)
232 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100233 if (options->kbd_interactive_authentication == -1)
234 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000235 if (options->challenge_response_authentication == -1)
236 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100237 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100238 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000239 if (options->permit_user_env == -1)
240 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100241 if (options->use_login == -1)
242 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000243 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000244 options->compression = COMP_DELAYED;
Damien Miller50a41ed2000-10-16 12:14:42 +1100245 if (options->allow_tcp_forwarding == -1)
246 options->allow_tcp_forwarding = 1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000247 if (options->allow_agent_forwarding == -1)
248 options->allow_agent_forwarding = 1;
Damien Millere247cc42000-05-07 12:03:14 +1000249 if (options->gateway_ports == -1)
250 options->gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000251 if (options->max_startups == -1)
252 options->max_startups = 10;
Damien Miller942da032000-08-18 13:59:06 +1000253 if (options->max_startups_rate == -1)
254 options->max_startups_rate = 100; /* 100% */
255 if (options->max_startups_begin == -1)
256 options->max_startups_begin = options->max_startups;
Darren Tucker89413db2004-05-24 10:36:23 +1000257 if (options->max_authtries == -1)
258 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000259 if (options->max_sessions == -1)
260 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000261 if (options->use_dns == -1)
262 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000263 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100264 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000265 if (options->client_alive_count_max == -1)
266 options->client_alive_count_max = 3;
Damien Miller75413ac2001-11-12 11:14:35 +1100267 if (options->authorized_keys_file2 == NULL) {
268 /* authorized_keys_file2 falls back to authorized_keys_file */
269 if (options->authorized_keys_file != NULL)
Damien Millerc54b02c2010-10-07 21:40:17 +1100270 options->authorized_keys_file2 = xstrdup(options->authorized_keys_file);
Damien Miller75413ac2001-11-12 11:14:35 +1100271 else
Damien Millerc54b02c2010-10-07 21:40:17 +1100272 options->authorized_keys_file2 = xstrdup(_PATH_SSH_USER_PERMITTED_KEYS2);
Damien Miller75413ac2001-11-12 11:14:35 +1100273 }
274 if (options->authorized_keys_file == NULL)
Damien Millerc54b02c2010-10-07 21:40:17 +1100275 options->authorized_keys_file = xstrdup(_PATH_SSH_USER_PERMITTED_KEYS);
Damien Millerd27b9472005-12-13 19:29:02 +1100276 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100277 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller01ed2272008-11-05 16:20:46 +1100278 if (options->zero_knowledge_password_authentication == -1)
279 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100280 if (options->ip_qos_interactive == -1)
281 options->ip_qos_interactive = IPTOS_LOWDELAY;
282 if (options->ip_qos_bulk == -1)
283 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000284
Ben Lindstromfb62a692002-06-06 19:47:11 +0000285 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000286 if (use_privsep == -1)
Ben Lindstromfb62a692002-06-06 19:47:11 +0000287 use_privsep = 1;
Damien Miller4903eb42002-06-21 16:20:44 +1000288
Tim Rice40017b02002-07-14 13:36:49 -0700289#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000290 if (use_privsep && options->compression == 1) {
291 error("This platform does not support both privilege "
292 "separation and compression");
293 error("Compression disabled");
294 options->compression = 0;
295 }
296#endif
297
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298}
299
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100301typedef enum {
302 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100303 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000304 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100305 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100306 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
307 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000308 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100309 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100310 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000311 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100312 sPasswordAuthentication, sKbdInteractiveAuthentication,
313 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000314 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100315 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller12c150e2003-12-17 16:31:10 +1100316 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000317 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Damien Miller50a41ed2000-10-16 12:14:42 +1100318 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000319 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000320 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
Damien Miller7207f642008-05-19 15:34:50 +1000321 sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000322 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100323 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000324 sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
Damien Millerd27b9472005-12-13 19:29:02 +1100325 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100326 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100327 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller0a80ca12010-02-27 07:55:05 +1100328 sZeroKnowledgePasswordAuthentication, sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000329 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Miller0dac6fb2010-11-20 15:19:38 +1100330 sKexAlgorithms, sIPQoS,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000331 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332} ServerOpCodes;
333
Darren Tucker45150472006-07-12 22:34:17 +1000334#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
335#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
336#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
337
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100339static struct {
340 const char *name;
341 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000342 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100343} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100344 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000345#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000346 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000347#else
Darren Tucker45150472006-07-12 22:34:17 +1000348 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000349#endif
Darren Tucker45150472006-07-12 22:34:17 +1000350 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100351 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000352 { "port", sPort, SSHCFG_GLOBAL },
353 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
354 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
355 { "pidfile", sPidFile, SSHCFG_GLOBAL },
356 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
357 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
358 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100359 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000360 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
361 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
362 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100363 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
364 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000365 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100366 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
367 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000368 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000369#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100370 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000371 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
372 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100373#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000374 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000375#else
Darren Tucker45150472006-07-12 22:34:17 +1000376 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100377#endif
378#else
Darren Tucker1629c072007-02-19 22:25:37 +1100379 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000380 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
381 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
382 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000383#endif
Darren Tucker45150472006-07-12 22:34:17 +1000384 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
385 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000386#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100387 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000388 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000389#else
Darren Tucker1629c072007-02-19 22:25:37 +1100390 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000391 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000392#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100393 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
394 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100395 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000396 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
Damien Miller01ed2272008-11-05 16:20:46 +1100397#ifdef JPAKE
398 { "zeroknowledgepasswordauthentication", sZeroKnowledgePasswordAuthentication, SSHCFG_ALL },
399#else
400 { "zeroknowledgepasswordauthentication", sUnsupported, SSHCFG_ALL },
401#endif
Darren Tucker45150472006-07-12 22:34:17 +1000402 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
403 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
404 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
405 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
406 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
407 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
408 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000409 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
410 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
411 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000412 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
413 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100414 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000415 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
416 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
417 { "compression", sCompression, SSHCFG_GLOBAL },
418 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
419 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
420 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000421 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000422 { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
423 { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
424 { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
425 { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
426 { "ciphers", sCiphers, SSHCFG_GLOBAL },
427 { "macs", sMacs, SSHCFG_GLOBAL },
428 { "protocol", sProtocol, SSHCFG_GLOBAL },
429 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
430 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
431 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000432 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000433 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100434 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000435 { "usedns", sUseDNS, SSHCFG_GLOBAL },
436 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
437 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
438 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
439 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000440 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
441 { "authorizedkeysfile2", sAuthorizedKeysFile2, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000442 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Darren Tucker45150472006-07-12 22:34:17 +1000443 { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000444 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000445 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000446 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000447 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100448 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100449 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100450 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
451 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000452 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000453 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100454 { "ipqos", sIPQoS, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000455 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456};
457
Darren Tuckere7140f22008-06-10 23:01:51 +1000458static struct {
459 int val;
460 char *text;
461} tunmode_desc[] = {
462 { SSH_TUNMODE_NO, "no" },
463 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
464 { SSH_TUNMODE_ETHERNET, "ethernet" },
465 { SSH_TUNMODE_YES, "yes" },
466 { -1, NULL }
467};
468
Damien Miller5428f641999-11-25 11:54:57 +1100469/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000470 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100471 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472
Damien Miller4af51302000-04-16 11:18:38 +1000473static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100474parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000475 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000477 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478
Damien Miller95def091999-11-25 00:26:21 +1100479 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000480 if (strcasecmp(cp, keywords[i].name) == 0) {
481 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100482 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000483 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000485 error("%s: line %d: Bad configuration option: %s",
486 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100487 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488}
489
Darren Tucker88b6fb22010-01-13 22:44:29 +1100490char *
491derelativise_path(const char *path)
492{
Damien Miller44451d02010-03-26 10:40:04 +1100493 char *expanded, *ret, cwd[MAXPATHLEN];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100494
495 expanded = tilde_expand_filename(path, getuid());
496 if (*expanded == '/')
497 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100498 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100499 fatal("%s: getcwd: %s", __func__, strerror(errno));
500 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100501 xfree(expanded);
502 return ret;
503}
504
Ben Lindstrombba81212001-06-25 05:01:22 +0000505static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100506add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100507{
Damien Millereccb9de2005-06-17 12:59:34 +1000508 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100509
510 if (options->num_ports == 0)
511 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100512 if (options->address_family == -1)
513 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000514 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000515 for (i = 0; i < options->num_ports; i++)
516 add_one_listen_addr(options, addr, options->ports[i]);
517 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000518 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000519}
520
Ben Lindstrombba81212001-06-25 05:01:22 +0000521static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100522add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000523{
524 struct addrinfo hints, *ai, *aitop;
525 char strport[NI_MAXSERV];
526 int gaierr;
527
528 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100529 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000530 hints.ai_socktype = SOCK_STREAM;
531 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100532 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000533 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
534 fatal("bad addr or host: %s (%s)",
535 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100536 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000537 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
538 ;
539 ai->ai_next = options->listen_addrs;
540 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100541}
542
Darren Tucker45150472006-07-12 22:34:17 +1000543/*
544 * The strategy for the Match blocks is that the config file is parsed twice.
545 *
546 * The first time is at startup. activep is initialized to 1 and the
547 * directives in the global context are processed and acted on. Hitting a
548 * Match directive unsets activep and the directives inside the block are
549 * checked for syntax only.
550 *
551 * The second time is after a connection has been established but before
552 * authentication. activep is initialized to 2 and global config directives
553 * are ignored since they have already been processed. If the criteria in a
554 * Match block is met, activep is set and the subsequent directives
555 * processed and actioned until EOF or another Match block unsets it. Any
556 * options set are copied into the main server config.
557 *
558 * Potential additions/improvements:
559 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
560 *
561 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
562 * Match Address 192.168.0.*
563 * Tag trusted
564 * Match Group wheel
565 * Tag trusted
566 * Match Tag trusted
567 * AllowTcpForwarding yes
568 * GatewayPorts clientspecified
569 * [...]
570 *
571 * - Add a PermittedChannelRequests directive
572 * Match Group shell
573 * PermittedChannelRequests session,forwarded-tcpip
574 */
575
576static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000577match_cfg_line_group(const char *grps, int line, const char *user)
578{
579 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000580 struct passwd *pw;
581
Damien Miller565ca3f2006-08-19 00:23:15 +1000582 if (user == NULL)
583 goto out;
584
585 if ((pw = getpwnam(user)) == NULL) {
586 debug("Can't match group at line %d because user %.100s does "
587 "not exist", line, user);
588 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
589 debug("Can't Match group because user %.100s not in any group "
590 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000591 } else if (ga_match_pattern_list(grps) != 1) {
592 debug("user %.100s does not match group list %.100s at line %d",
593 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000594 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000595 debug("user %.100s matched group list %.100s at line %d", user,
596 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000597 result = 1;
598 }
599out:
600 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000601 return result;
602}
603
604static int
Darren Tucker45150472006-07-12 22:34:17 +1000605match_cfg_line(char **condition, int line, const char *user, const char *host,
606 const char *address)
607{
608 int result = 1;
609 char *arg, *attrib, *cp = *condition;
610 size_t len;
611
612 if (user == NULL)
613 debug3("checking syntax for 'Match %s'", cp);
614 else
615 debug3("checking match for '%s' user %s host %s addr %s", cp,
616 user ? user : "(null)", host ? host : "(null)",
617 address ? address : "(null)");
618
619 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
620 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
621 error("Missing Match criteria for %s", attrib);
622 return -1;
623 }
624 len = strlen(arg);
625 if (strcasecmp(attrib, "user") == 0) {
626 if (!user) {
627 result = 0;
628 continue;
629 }
630 if (match_pattern_list(user, arg, len, 0) != 1)
631 result = 0;
632 else
633 debug("user %.100s matched 'User %.100s' at "
634 "line %d", user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000635 } else if (strcasecmp(attrib, "group") == 0) {
636 switch (match_cfg_line_group(arg, line, user)) {
637 case -1:
638 return -1;
639 case 0:
640 result = 0;
641 }
Darren Tucker45150472006-07-12 22:34:17 +1000642 } else if (strcasecmp(attrib, "host") == 0) {
643 if (!host) {
644 result = 0;
645 continue;
646 }
647 if (match_hostname(host, arg, len) != 1)
648 result = 0;
649 else
650 debug("connection from %.100s matched 'Host "
651 "%.100s' at line %d", host, arg, line);
652 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000653 switch (addr_match_list(address, arg)) {
654 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000655 debug("connection from %.100s matched 'Address "
656 "%.100s' at line %d", address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000657 break;
658 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000659 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000660 result = 0;
661 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000662 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000663 return -1;
664 }
Darren Tucker45150472006-07-12 22:34:17 +1000665 } else {
666 error("Unsupported Match attribute %s", attrib);
667 return -1;
668 }
669 }
670 if (user != NULL)
671 debug3("match %sfound", result ? "" : "not ");
672 *condition = cp;
673 return result;
674}
675
Damien Millere2754432006-07-24 14:06:47 +1000676#define WHITESPACE " \t\r\n"
677
Ben Lindstromade03f62001-12-06 18:22:17 +0000678int
679process_server_config_line(ServerOptions *options, char *line,
Darren Tucker45150472006-07-12 22:34:17 +1000680 const char *filename, int linenum, int *activep, const char *user,
681 const char *host, const char *address)
Ben Lindstromade03f62001-12-06 18:22:17 +0000682{
683 char *cp, **charptr, *arg, *p;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100684 int cmdline = 0, *intptr, value, value2, n;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100685 SyslogFacility *log_facility_ptr;
686 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000687 ServerOpCodes opcode;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100688 int port;
Darren Tucker45150472006-07-12 22:34:17 +1000689 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000690 size_t len;
Ben Lindstromade03f62001-12-06 18:22:17 +0000691
692 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100693 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100694 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000695 /* Ignore leading whitespace */
696 if (*arg == '\0')
697 arg = strdelim(&cp);
698 if (!arg || !*arg || *arg == '#')
699 return 0;
700 intptr = NULL;
701 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000702 opcode = parse_token(arg, filename, linenum, &flags);
703
704 if (activep == NULL) { /* We are processing a command line directive */
705 cmdline = 1;
706 activep = &cmdline;
707 }
708 if (*activep && opcode != sMatch)
709 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
710 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
711 if (user == NULL) {
712 fatal("%s line %d: Directive '%s' is not allowed "
713 "within a Match block", filename, linenum, arg);
714 } else { /* this is a directive we have already processed */
715 while (arg)
716 arg = strdelim(&cp);
717 return 0;
718 }
719 }
720
Ben Lindstromade03f62001-12-06 18:22:17 +0000721 switch (opcode) {
722 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000723 case sUsePAM:
724 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000725 goto parse_flag;
726
727 /* Standard Options */
728 case sBadOption:
729 return -1;
730 case sPort:
731 /* ignore ports from configfile if cmdline specifies ports */
732 if (options->ports_from_cmdline)
733 return 0;
734 if (options->listen_addrs != NULL)
735 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100736 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000737 if (options->num_ports >= MAX_PORTS)
738 fatal("%s line %d: too many ports.",
739 filename, linenum);
740 arg = strdelim(&cp);
741 if (!arg || *arg == '\0')
742 fatal("%s line %d: missing port number.",
743 filename, linenum);
744 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100745 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000746 fatal("%s line %d: Badly formatted port number.",
747 filename, linenum);
748 break;
749
750 case sServerKeyBits:
751 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +1000752 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +0000753 arg = strdelim(&cp);
754 if (!arg || *arg == '\0')
755 fatal("%s line %d: missing integer value.",
756 filename, linenum);
757 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000758 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000759 *intptr = value;
760 break;
761
762 case sLoginGraceTime:
763 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +1000764 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +0000765 arg = strdelim(&cp);
766 if (!arg || *arg == '\0')
767 fatal("%s line %d: missing time value.",
768 filename, linenum);
769 if ((value = convtime(arg)) == -1)
770 fatal("%s line %d: invalid time value.",
771 filename, linenum);
772 if (*intptr == -1)
773 *intptr = value;
774 break;
775
776 case sKeyRegenerationTime:
777 intptr = &options->key_regeneration_time;
778 goto parse_time;
779
780 case sListenAddress:
781 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100782 if (arg == NULL || *arg == '\0')
783 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000784 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000785 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
786 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
787 && strchr(p+1, ':') != NULL) {
788 add_listen_addr(options, arg, 0);
789 break;
790 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100791 p = hpdelim(&arg);
792 if (p == NULL)
793 fatal("%s line %d: bad address:port usage",
794 filename, linenum);
795 p = cleanhostname(p);
796 if (arg == NULL)
797 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100798 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100799 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000800
Damien Millerf91ee4c2005-03-01 21:24:33 +1100801 add_listen_addr(options, p, port);
802
Ben Lindstromade03f62001-12-06 18:22:17 +0000803 break;
804
Darren Tucker0f383232005-01-20 10:57:56 +1100805 case sAddressFamily:
806 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000807 if (!arg || *arg == '\0')
808 fatal("%s line %d: missing address family.",
809 filename, linenum);
Darren Tucker0f383232005-01-20 10:57:56 +1100810 intptr = &options->address_family;
811 if (options->listen_addrs != NULL)
812 fatal("%s line %d: address family must be specified before "
813 "ListenAddress.", filename, linenum);
814 if (strcasecmp(arg, "inet") == 0)
815 value = AF_INET;
816 else if (strcasecmp(arg, "inet6") == 0)
817 value = AF_INET6;
818 else if (strcasecmp(arg, "any") == 0)
819 value = AF_UNSPEC;
820 else
821 fatal("%s line %d: unsupported address family \"%s\".",
822 filename, linenum, arg);
823 if (*intptr == -1)
824 *intptr = value;
825 break;
826
Ben Lindstromade03f62001-12-06 18:22:17 +0000827 case sHostKeyFile:
828 intptr = &options->num_host_key_files;
829 if (*intptr >= MAX_HOSTKEYS)
830 fatal("%s line %d: too many host keys specified (max %d).",
831 filename, linenum, MAX_HOSTKEYS);
832 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +1000833 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +0000834 arg = strdelim(&cp);
835 if (!arg || *arg == '\0')
836 fatal("%s line %d: missing file name.",
837 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +1000838 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +1100839 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000840 /* increase optional counter */
841 if (intptr != NULL)
842 *intptr = *intptr + 1;
843 }
844 break;
845
Damien Miller0a80ca12010-02-27 07:55:05 +1100846 case sHostCertificate:
847 intptr = &options->num_host_cert_files;
848 if (*intptr >= MAX_HOSTKEYS)
849 fatal("%s line %d: too many host certificates "
850 "specified (max %d).", filename, linenum,
851 MAX_HOSTCERTS);
852 charptr = &options->host_cert_files[*intptr];
853 goto parse_filename;
854 break;
855
Ben Lindstromade03f62001-12-06 18:22:17 +0000856 case sPidFile:
857 charptr = &options->pid_file;
858 goto parse_filename;
859
860 case sPermitRootLogin:
861 intptr = &options->permit_root_login;
862 arg = strdelim(&cp);
863 if (!arg || *arg == '\0')
864 fatal("%s line %d: missing yes/"
865 "without-password/forced-commands-only/no "
866 "argument.", filename, linenum);
867 value = 0; /* silence compiler */
868 if (strcmp(arg, "without-password") == 0)
869 value = PERMIT_NO_PASSWD;
870 else if (strcmp(arg, "forced-commands-only") == 0)
871 value = PERMIT_FORCED_ONLY;
872 else if (strcmp(arg, "yes") == 0)
873 value = PERMIT_YES;
874 else if (strcmp(arg, "no") == 0)
875 value = PERMIT_NO;
876 else
877 fatal("%s line %d: Bad yes/"
878 "without-password/forced-commands-only/no "
879 "argument: %s", filename, linenum, arg);
Darren Tucker15f94272008-01-01 20:36:56 +1100880 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000881 *intptr = value;
882 break;
883
884 case sIgnoreRhosts:
885 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +1000886 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +0000887 arg = strdelim(&cp);
888 if (!arg || *arg == '\0')
889 fatal("%s line %d: missing yes/no argument.",
890 filename, linenum);
891 value = 0; /* silence compiler */
892 if (strcmp(arg, "yes") == 0)
893 value = 1;
894 else if (strcmp(arg, "no") == 0)
895 value = 0;
896 else
897 fatal("%s line %d: Bad yes/no argument: %s",
898 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +1000899 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000900 *intptr = value;
901 break;
902
903 case sIgnoreUserKnownHosts:
904 intptr = &options->ignore_user_known_hosts;
905 goto parse_flag;
906
Ben Lindstromade03f62001-12-06 18:22:17 +0000907 case sRhostsRSAAuthentication:
908 intptr = &options->rhosts_rsa_authentication;
909 goto parse_flag;
910
911 case sHostbasedAuthentication:
912 intptr = &options->hostbased_authentication;
913 goto parse_flag;
914
915 case sHostbasedUsesNameFromPacketOnly:
916 intptr = &options->hostbased_uses_name_from_packet_only;
917 goto parse_flag;
918
919 case sRSAAuthentication:
920 intptr = &options->rsa_authentication;
921 goto parse_flag;
922
923 case sPubkeyAuthentication:
924 intptr = &options->pubkey_authentication;
925 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000926
Ben Lindstromade03f62001-12-06 18:22:17 +0000927 case sKerberosAuthentication:
928 intptr = &options->kerberos_authentication;
929 goto parse_flag;
930
931 case sKerberosOrLocalPasswd:
932 intptr = &options->kerberos_or_local_passwd;
933 goto parse_flag;
934
935 case sKerberosTicketCleanup:
936 intptr = &options->kerberos_ticket_cleanup;
937 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000938
Darren Tucker22ef5082003-12-31 11:37:34 +1100939 case sKerberosGetAFSToken:
940 intptr = &options->kerberos_get_afs_token;
941 goto parse_flag;
942
Darren Tucker0efd1552003-08-26 11:49:55 +1000943 case sGssAuthentication:
944 intptr = &options->gss_authentication;
945 goto parse_flag;
946
947 case sGssCleanupCreds:
948 intptr = &options->gss_cleanup_creds;
949 goto parse_flag;
950
Ben Lindstromade03f62001-12-06 18:22:17 +0000951 case sPasswordAuthentication:
952 intptr = &options->password_authentication;
953 goto parse_flag;
954
Damien Miller01ed2272008-11-05 16:20:46 +1100955 case sZeroKnowledgePasswordAuthentication:
956 intptr = &options->zero_knowledge_password_authentication;
957 goto parse_flag;
958
Ben Lindstromade03f62001-12-06 18:22:17 +0000959 case sKbdInteractiveAuthentication:
960 intptr = &options->kbd_interactive_authentication;
961 goto parse_flag;
962
963 case sChallengeResponseAuthentication:
964 intptr = &options->challenge_response_authentication;
965 goto parse_flag;
966
967 case sPrintMotd:
968 intptr = &options->print_motd;
969 goto parse_flag;
970
971 case sPrintLastLog:
972 intptr = &options->print_lastlog;
973 goto parse_flag;
974
975 case sX11Forwarding:
976 intptr = &options->x11_forwarding;
977 goto parse_flag;
978
979 case sX11DisplayOffset:
980 intptr = &options->x11_display_offset;
981 goto parse_int;
982
Damien Miller95c249f2002-02-05 12:11:34 +1100983 case sX11UseLocalhost:
984 intptr = &options->x11_use_localhost;
985 goto parse_flag;
986
Ben Lindstromade03f62001-12-06 18:22:17 +0000987 case sXAuthLocation:
988 charptr = &options->xauth_location;
989 goto parse_filename;
990
991 case sStrictModes:
992 intptr = &options->strict_modes;
993 goto parse_flag;
994
Damien Miller12c150e2003-12-17 16:31:10 +1100995 case sTCPKeepAlive:
996 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +0000997 goto parse_flag;
998
999 case sEmptyPasswd:
1000 intptr = &options->permit_empty_passwd;
1001 goto parse_flag;
1002
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001003 case sPermitUserEnvironment:
1004 intptr = &options->permit_user_env;
1005 goto parse_flag;
1006
Ben Lindstromade03f62001-12-06 18:22:17 +00001007 case sUseLogin:
1008 intptr = &options->use_login;
1009 goto parse_flag;
1010
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001011 case sCompression:
1012 intptr = &options->compression;
Damien Miller9786e6e2005-07-26 21:54:56 +10001013 arg = strdelim(&cp);
1014 if (!arg || *arg == '\0')
1015 fatal("%s line %d: missing yes/no/delayed "
1016 "argument.", filename, linenum);
1017 value = 0; /* silence compiler */
1018 if (strcmp(arg, "delayed") == 0)
1019 value = COMP_DELAYED;
1020 else if (strcmp(arg, "yes") == 0)
1021 value = COMP_ZLIB;
1022 else if (strcmp(arg, "no") == 0)
1023 value = COMP_NONE;
1024 else
1025 fatal("%s line %d: Bad yes/no/delayed "
1026 "argument: %s", filename, linenum, arg);
1027 if (*intptr == -1)
1028 *intptr = value;
1029 break;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001030
Ben Lindstromade03f62001-12-06 18:22:17 +00001031 case sGatewayPorts:
1032 intptr = &options->gateway_ports;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001033 arg = strdelim(&cp);
1034 if (!arg || *arg == '\0')
1035 fatal("%s line %d: missing yes/no/clientspecified "
1036 "argument.", filename, linenum);
1037 value = 0; /* silence compiler */
1038 if (strcmp(arg, "clientspecified") == 0)
1039 value = 2;
1040 else if (strcmp(arg, "yes") == 0)
1041 value = 1;
1042 else if (strcmp(arg, "no") == 0)
1043 value = 0;
1044 else
1045 fatal("%s line %d: Bad yes/no/clientspecified "
1046 "argument: %s", filename, linenum, arg);
Darren Tucker82347a82007-02-25 20:37:52 +11001047 if (*activep && *intptr == -1)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001048 *intptr = value;
1049 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001050
Damien Miller3a961dc2003-06-03 10:25:48 +10001051 case sUseDNS:
1052 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001053 goto parse_flag;
1054
1055 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001056 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001057 arg = strdelim(&cp);
1058 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001059 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001060 fatal("%.200s line %d: unsupported log facility '%s'",
1061 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001062 if (*log_facility_ptr == -1)
1063 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001064 break;
1065
1066 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001067 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001068 arg = strdelim(&cp);
1069 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001070 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001071 fatal("%.200s line %d: unsupported log level '%s'",
1072 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001073 if (*log_level_ptr == -1)
1074 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001075 break;
1076
1077 case sAllowTcpForwarding:
1078 intptr = &options->allow_tcp_forwarding;
1079 goto parse_flag;
1080
Damien Miller4f755cd2008-05-19 14:57:41 +10001081 case sAllowAgentForwarding:
1082 intptr = &options->allow_agent_forwarding;
1083 goto parse_flag;
1084
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001085 case sUsePrivilegeSeparation:
1086 intptr = &use_privsep;
1087 goto parse_flag;
1088
Ben Lindstromade03f62001-12-06 18:22:17 +00001089 case sAllowUsers:
1090 while ((arg = strdelim(&cp)) && *arg != '\0') {
1091 if (options->num_allow_users >= MAX_ALLOW_USERS)
1092 fatal("%s line %d: too many allow users.",
1093 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001094 options->allow_users[options->num_allow_users++] =
1095 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001096 }
1097 break;
1098
1099 case sDenyUsers:
1100 while ((arg = strdelim(&cp)) && *arg != '\0') {
1101 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001102 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001103 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001104 options->deny_users[options->num_deny_users++] =
1105 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001106 }
1107 break;
1108
1109 case sAllowGroups:
1110 while ((arg = strdelim(&cp)) && *arg != '\0') {
1111 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1112 fatal("%s line %d: too many allow groups.",
1113 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001114 options->allow_groups[options->num_allow_groups++] =
1115 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001116 }
1117 break;
1118
1119 case sDenyGroups:
1120 while ((arg = strdelim(&cp)) && *arg != '\0') {
1121 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1122 fatal("%s line %d: too many deny groups.",
1123 filename, linenum);
1124 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
1125 }
1126 break;
1127
1128 case sCiphers:
1129 arg = strdelim(&cp);
1130 if (!arg || *arg == '\0')
1131 fatal("%s line %d: Missing argument.", filename, linenum);
1132 if (!ciphers_valid(arg))
1133 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1134 filename, linenum, arg ? arg : "<NONE>");
1135 if (options->ciphers == NULL)
1136 options->ciphers = xstrdup(arg);
1137 break;
1138
1139 case sMacs:
1140 arg = strdelim(&cp);
1141 if (!arg || *arg == '\0')
1142 fatal("%s line %d: Missing argument.", filename, linenum);
1143 if (!mac_valid(arg))
1144 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1145 filename, linenum, arg ? arg : "<NONE>");
1146 if (options->macs == NULL)
1147 options->macs = xstrdup(arg);
1148 break;
1149
Damien Millerd5f62bf2010-09-24 22:11:14 +10001150 case sKexAlgorithms:
1151 arg = strdelim(&cp);
1152 if (!arg || *arg == '\0')
1153 fatal("%s line %d: Missing argument.",
1154 filename, linenum);
1155 if (!kex_names_valid(arg))
1156 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1157 filename, linenum, arg ? arg : "<NONE>");
1158 if (options->kex_algorithms == NULL)
1159 options->kex_algorithms = xstrdup(arg);
1160 break;
1161
Ben Lindstromade03f62001-12-06 18:22:17 +00001162 case sProtocol:
1163 intptr = &options->protocol;
1164 arg = strdelim(&cp);
1165 if (!arg || *arg == '\0')
1166 fatal("%s line %d: Missing argument.", filename, linenum);
1167 value = proto_spec(arg);
1168 if (value == SSH_PROTO_UNKNOWN)
1169 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001170 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001171 if (*intptr == SSH_PROTO_UNKNOWN)
1172 *intptr = value;
1173 break;
1174
1175 case sSubsystem:
1176 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1177 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001178 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001179 }
1180 arg = strdelim(&cp);
1181 if (!arg || *arg == '\0')
1182 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001183 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001184 if (!*activep) {
1185 arg = strdelim(&cp);
1186 break;
1187 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001188 for (i = 0; i < options->num_subsystems; i++)
1189 if (strcmp(arg, options->subsystem_name[i]) == 0)
1190 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001191 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001192 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1193 arg = strdelim(&cp);
1194 if (!arg || *arg == '\0')
1195 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001196 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001197 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001198
1199 /* Collect arguments (separate to executable) */
1200 p = xstrdup(arg);
1201 len = strlen(p) + 1;
1202 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1203 len += 1 + strlen(arg);
1204 p = xrealloc(p, 1, len);
1205 strlcat(p, " ", len);
1206 strlcat(p, arg, len);
1207 }
1208 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001209 options->num_subsystems++;
1210 break;
1211
1212 case sMaxStartups:
1213 arg = strdelim(&cp);
1214 if (!arg || *arg == '\0')
1215 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001216 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001217 if ((n = sscanf(arg, "%d:%d:%d",
1218 &options->max_startups_begin,
1219 &options->max_startups_rate,
1220 &options->max_startups)) == 3) {
1221 if (options->max_startups_begin >
1222 options->max_startups ||
1223 options->max_startups_rate > 100 ||
1224 options->max_startups_rate < 1)
1225 fatal("%s line %d: Illegal MaxStartups spec.",
1226 filename, linenum);
1227 } else if (n != 1)
1228 fatal("%s line %d: Illegal MaxStartups spec.",
1229 filename, linenum);
1230 else
1231 options->max_startups = options->max_startups_begin;
1232 break;
1233
Darren Tucker89413db2004-05-24 10:36:23 +10001234 case sMaxAuthTries:
1235 intptr = &options->max_authtries;
1236 goto parse_int;
1237
Damien Miller7207f642008-05-19 15:34:50 +10001238 case sMaxSessions:
1239 intptr = &options->max_sessions;
1240 goto parse_int;
1241
Ben Lindstromade03f62001-12-06 18:22:17 +00001242 case sBanner:
1243 charptr = &options->banner;
1244 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001245
Ben Lindstromade03f62001-12-06 18:22:17 +00001246 /*
1247 * These options can contain %X options expanded at
1248 * connect time, so that you can specify paths like:
1249 *
1250 * AuthorizedKeysFile /etc/ssh_keys/%u
1251 */
1252 case sAuthorizedKeysFile:
Damien Miller30da3442010-05-10 11:58:03 +10001253 charptr = &options->authorized_keys_file;
1254 goto parse_tilde_filename;
Ben Lindstromade03f62001-12-06 18:22:17 +00001255 case sAuthorizedKeysFile2:
Damien Miller30da3442010-05-10 11:58:03 +10001256 charptr = &options->authorized_keys_file2;
1257 goto parse_tilde_filename;
1258 case sAuthorizedPrincipalsFile:
1259 charptr = &options->authorized_principals_file;
1260 parse_tilde_filename:
Damien Millerc4cb47b2010-03-22 05:52:26 +11001261 arg = strdelim(&cp);
1262 if (!arg || *arg == '\0')
1263 fatal("%s line %d: missing file name.",
1264 filename, linenum);
1265 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001266 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001267 /* increase optional counter */
1268 if (intptr != NULL)
1269 *intptr = *intptr + 1;
1270 }
1271 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001272
1273 case sClientAliveInterval:
1274 intptr = &options->client_alive_interval;
1275 goto parse_time;
1276
1277 case sClientAliveCountMax:
1278 intptr = &options->client_alive_count_max;
1279 goto parse_int;
1280
Darren Tucker46bc0752004-05-02 22:11:30 +10001281 case sAcceptEnv:
1282 while ((arg = strdelim(&cp)) && *arg != '\0') {
1283 if (strchr(arg, '=') != NULL)
1284 fatal("%s line %d: Invalid environment name.",
1285 filename, linenum);
1286 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1287 fatal("%s line %d: too many allow env.",
1288 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001289 if (!*activep)
1290 break;
Darren Tucker46bc0752004-05-02 22:11:30 +10001291 options->accept_env[options->num_accept_env++] =
1292 xstrdup(arg);
1293 }
1294 break;
1295
Damien Millerd27b9472005-12-13 19:29:02 +11001296 case sPermitTunnel:
1297 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001298 arg = strdelim(&cp);
1299 if (!arg || *arg == '\0')
1300 fatal("%s line %d: Missing yes/point-to-point/"
1301 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001302 value = -1;
1303 for (i = 0; tunmode_desc[i].val != -1; i++)
1304 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1305 value = tunmode_desc[i].val;
1306 break;
1307 }
1308 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001309 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1310 "no argument: %s", filename, linenum, arg);
1311 if (*intptr == -1)
1312 *intptr = value;
1313 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001314
Darren Tucker45150472006-07-12 22:34:17 +10001315 case sMatch:
1316 if (cmdline)
1317 fatal("Match directive not supported as a command-line "
1318 "option");
1319 value = match_cfg_line(&cp, linenum, user, host, address);
1320 if (value < 0)
1321 fatal("%s line %d: Bad Match condition", filename,
1322 linenum);
1323 *activep = value;
1324 break;
1325
Damien Miller9b439df2006-07-24 14:04:00 +10001326 case sPermitOpen:
1327 arg = strdelim(&cp);
1328 if (!arg || *arg == '\0')
1329 fatal("%s line %d: missing PermitOpen specification",
1330 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001331 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001332 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001333 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001334 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001335 options->num_permitted_opens = 0;
1336 }
Damien Miller9b439df2006-07-24 14:04:00 +10001337 break;
1338 }
Damien Millera29b95e2007-01-05 16:28:36 +11001339 if (*activep && n == -1)
1340 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001341 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1342 p = hpdelim(&arg);
1343 if (p == NULL)
1344 fatal("%s line %d: missing host in PermitOpen",
1345 filename, linenum);
1346 p = cleanhostname(p);
Damien Miller3dc71ad2009-01-28 16:31:22 +11001347 if (arg == NULL || (port = a2port(arg)) <= 0)
Damien Millera765cf42006-07-24 14:08:13 +10001348 fatal("%s line %d: bad port number in "
1349 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001350 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001351 options->num_permitted_opens =
1352 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001353 }
Damien Miller9b439df2006-07-24 14:04:00 +10001354 break;
1355
Damien Millere2754432006-07-24 14:06:47 +10001356 case sForceCommand:
1357 if (cp == NULL)
1358 fatal("%.200s line %d: Missing argument.", filename,
1359 linenum);
1360 len = strspn(cp, WHITESPACE);
1361 if (*activep && options->adm_forced_command == NULL)
1362 options->adm_forced_command = xstrdup(cp + len);
1363 return 0;
1364
Damien Millerd8cb1f12008-02-10 22:40:12 +11001365 case sChrootDirectory:
1366 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001367
1368 arg = strdelim(&cp);
1369 if (!arg || *arg == '\0')
1370 fatal("%s line %d: missing file name.",
1371 filename, linenum);
1372 if (*activep && *charptr == NULL)
1373 *charptr = xstrdup(arg);
1374 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001375
Damien Miller1aed65e2010-03-04 21:53:35 +11001376 case sTrustedUserCAKeys:
1377 charptr = &options->trusted_user_ca_keys;
1378 goto parse_filename;
1379
1380 case sRevokedKeys:
1381 charptr = &options->revoked_keys_file;
1382 goto parse_filename;
1383
Damien Miller0dac6fb2010-11-20 15:19:38 +11001384 case sIPQoS:
1385 arg = strdelim(&cp);
1386 if ((value = parse_ipqos(arg)) == -1)
1387 fatal("%s line %d: Bad IPQoS value: %s",
1388 filename, linenum, arg);
1389 arg = strdelim(&cp);
1390 if (arg == NULL)
1391 value2 = value;
1392 else if ((value2 = parse_ipqos(arg)) == -1)
1393 fatal("%s line %d: Bad IPQoS value: %s",
1394 filename, linenum, arg);
1395 if (*activep) {
1396 options->ip_qos_interactive = value;
1397 options->ip_qos_bulk = value2;
1398 }
1399 break;
1400
Ben Lindstromade03f62001-12-06 18:22:17 +00001401 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001402 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001403 filename, linenum, arg);
1404 while (arg)
1405 arg = strdelim(&cp);
1406 break;
1407
Damien Millerf9b3feb2003-05-16 11:38:32 +10001408 case sUnsupported:
1409 logit("%s line %d: Unsupported option %s",
1410 filename, linenum, arg);
1411 while (arg)
1412 arg = strdelim(&cp);
1413 break;
1414
Ben Lindstromade03f62001-12-06 18:22:17 +00001415 default:
1416 fatal("%s line %d: Missing handler for opcode %s (%d)",
1417 filename, linenum, arg, opcode);
1418 }
1419 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1420 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1421 filename, linenum, arg);
1422 return 0;
1423}
1424
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001425/* Reads the server configuration file. */
1426
Damien Miller4af51302000-04-16 11:18:38 +10001427void
Darren Tucker645ab752004-06-25 13:33:20 +10001428load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001429{
Darren Tucker645ab752004-06-25 13:33:20 +10001430 char line[1024], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001431 FILE *f;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001432
Darren Tucker645ab752004-06-25 13:33:20 +10001433 debug2("%s: filename %s", __func__, filename);
1434 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001435 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001436 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001437 }
Darren Tucker645ab752004-06-25 13:33:20 +10001438 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001439 while (fgets(line, sizeof(line), f)) {
Darren Tucker645ab752004-06-25 13:33:20 +10001440 /*
1441 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001442 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001443 * line numbers later for error messages
1444 */
1445 if ((cp = strchr(line, '#')) != NULL)
1446 memcpy(cp, "\n", 2);
1447 cp = line + strspn(line, " \t\r");
1448
1449 buffer_append(conf, cp, strlen(cp));
1450 }
1451 buffer_append(conf, "\0", 1);
1452 fclose(f);
1453 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1454}
1455
1456void
Darren Tucker45150472006-07-12 22:34:17 +10001457parse_server_match_config(ServerOptions *options, const char *user,
1458 const char *host, const char *address)
Darren Tucker645ab752004-06-25 13:33:20 +10001459{
Darren Tucker45150472006-07-12 22:34:17 +10001460 ServerOptions mo;
1461
1462 initialize_server_options(&mo);
1463 parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
Darren Tucker1629c072007-02-19 22:25:37 +11001464 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001465}
1466
Darren Tucker1629c072007-02-19 22:25:37 +11001467/* Helper macros */
1468#define M_CP_INTOPT(n) do {\
1469 if (src->n != -1) \
1470 dst->n = src->n; \
1471} while (0)
1472#define M_CP_STROPT(n) do {\
1473 if (src->n != NULL) { \
1474 if (dst->n != NULL) \
1475 xfree(dst->n); \
1476 dst->n = src->n; \
1477 } \
1478} while(0)
1479
1480/*
1481 * Copy any supported values that are set.
1482 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001483 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001484 * array values that are not used pre-authentication, because any that we
1485 * do use must be explictly sent in mm_getpwnamallow().
1486 */
Darren Tucker45150472006-07-12 22:34:17 +10001487void
Darren Tucker1629c072007-02-19 22:25:37 +11001488copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001489{
Darren Tucker1629c072007-02-19 22:25:37 +11001490 M_CP_INTOPT(password_authentication);
1491 M_CP_INTOPT(gss_authentication);
1492 M_CP_INTOPT(rsa_authentication);
1493 M_CP_INTOPT(pubkey_authentication);
1494 M_CP_INTOPT(kerberos_authentication);
1495 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001496 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001497 M_CP_INTOPT(kbd_interactive_authentication);
Damien Miller01ed2272008-11-05 16:20:46 +11001498 M_CP_INTOPT(zero_knowledge_password_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001499 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001500 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001501
1502 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001503 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001504 M_CP_INTOPT(permit_tun);
Darren Tucker1629c072007-02-19 22:25:37 +11001505 M_CP_INTOPT(gateway_ports);
1506 M_CP_INTOPT(x11_display_offset);
1507 M_CP_INTOPT(x11_forwarding);
1508 M_CP_INTOPT(x11_use_localhost);
Damien Miller7207f642008-05-19 15:34:50 +10001509 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001510 M_CP_INTOPT(max_authtries);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001511 M_CP_INTOPT(ip_qos_interactive);
1512 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker1629c072007-02-19 22:25:37 +11001513
1514 M_CP_STROPT(banner);
1515 if (preauth)
1516 return;
1517 M_CP_STROPT(adm_forced_command);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001518 M_CP_STROPT(chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11001519 M_CP_STROPT(trusted_user_ca_keys);
1520 M_CP_STROPT(revoked_keys_file);
Damien Millerab6de352010-06-26 09:38:45 +10001521 M_CP_STROPT(authorized_keys_file);
1522 M_CP_STROPT(authorized_keys_file2);
1523 M_CP_STROPT(authorized_principals_file);
Darren Tucker45150472006-07-12 22:34:17 +10001524}
1525
Darren Tucker1629c072007-02-19 22:25:37 +11001526#undef M_CP_INTOPT
1527#undef M_CP_STROPT
1528
Darren Tucker45150472006-07-12 22:34:17 +10001529void
1530parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1531 const char *user, const char *host, const char *address)
1532{
1533 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001534 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001535
1536 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1537
Darren Tucker9fbac712004-08-12 22:41:44 +10001538 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tucker45150472006-07-12 22:34:17 +10001539 active = user ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001540 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001541 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001542 if (process_server_config_line(options, cp, filename,
Darren Tucker45150472006-07-12 22:34:17 +10001543 linenum++, &active, user, host, address) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001544 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001545 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001546 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001547 if (bad_options > 0)
1548 fatal("%s: terminating, %d bad configuration options",
1549 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001550}
Darren Tuckere7140f22008-06-10 23:01:51 +10001551
1552static const char *
1553fmt_intarg(ServerOpCodes code, int val)
1554{
1555 if (code == sAddressFamily) {
1556 switch (val) {
1557 case AF_INET:
1558 return "inet";
1559 case AF_INET6:
1560 return "inet6";
1561 case AF_UNSPEC:
1562 return "any";
1563 default:
1564 return "UNKNOWN";
1565 }
1566 }
1567 if (code == sPermitRootLogin) {
1568 switch (val) {
1569 case PERMIT_NO_PASSWD:
Darren Tuckerff4350e2008-11-11 16:31:05 +11001570 return "without-password";
Darren Tuckere7140f22008-06-10 23:01:51 +10001571 case PERMIT_FORCED_ONLY:
1572 return "forced-commands-only";
1573 case PERMIT_YES:
1574 return "yes";
1575 }
1576 }
1577 if (code == sProtocol) {
1578 switch (val) {
1579 case SSH_PROTO_1:
1580 return "1";
1581 case SSH_PROTO_2:
1582 return "2";
1583 case (SSH_PROTO_1|SSH_PROTO_2):
1584 return "2,1";
1585 default:
1586 return "UNKNOWN";
1587 }
1588 }
1589 if (code == sGatewayPorts && val == 2)
1590 return "clientspecified";
1591 if (code == sCompression && val == COMP_DELAYED)
1592 return "delayed";
1593 switch (val) {
1594 case -1:
1595 return "unset";
1596 case 0:
1597 return "no";
1598 case 1:
1599 return "yes";
1600 }
1601 return "UNKNOWN";
1602}
1603
1604static const char *
1605lookup_opcode_name(ServerOpCodes code)
1606{
1607 u_int i;
1608
1609 for (i = 0; keywords[i].name != NULL; i++)
1610 if (keywords[i].opcode == code)
1611 return(keywords[i].name);
1612 return "UNKNOWN";
1613}
1614
1615static void
1616dump_cfg_int(ServerOpCodes code, int val)
1617{
1618 printf("%s %d\n", lookup_opcode_name(code), val);
1619}
1620
1621static void
1622dump_cfg_fmtint(ServerOpCodes code, int val)
1623{
1624 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
1625}
1626
1627static void
1628dump_cfg_string(ServerOpCodes code, const char *val)
1629{
1630 if (val == NULL)
1631 return;
1632 printf("%s %s\n", lookup_opcode_name(code), val);
1633}
1634
1635static void
1636dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
1637{
1638 u_int i;
1639
1640 for (i = 0; i < count; i++)
1641 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
1642}
1643
1644void
1645dump_config(ServerOptions *o)
1646{
1647 u_int i;
1648 int ret;
1649 struct addrinfo *ai;
1650 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
1651
1652 /* these are usually at the top of the config */
1653 for (i = 0; i < o->num_ports; i++)
1654 printf("port %d\n", o->ports[i]);
1655 dump_cfg_fmtint(sProtocol, o->protocol);
1656 dump_cfg_fmtint(sAddressFamily, o->address_family);
1657
1658 /* ListenAddress must be after Port */
1659 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
1660 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1661 sizeof(addr), port, sizeof(port),
1662 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1663 error("getnameinfo failed: %.100s",
1664 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
1665 strerror(errno));
1666 } else {
1667 if (ai->ai_family == AF_INET6)
1668 printf("listenaddress [%s]:%s\n", addr, port);
1669 else
1670 printf("listenaddress %s:%s\n", addr, port);
1671 }
1672 }
1673
1674 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10001675#ifdef USE_PAM
1676 dump_cfg_int(sUsePAM, o->use_pam);
1677#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001678 dump_cfg_int(sServerKeyBits, o->server_key_bits);
1679 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
1680 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
1681 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
1682 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11001683 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10001684 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
1685 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
1686
1687 /* formatted integer arguments */
1688 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
1689 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
1690 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
1691 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
1692 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
1693 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
1694 o->hostbased_uses_name_from_packet_only);
1695 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
1696 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10001697#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10001698 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
1699 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
1700 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10001701# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10001702 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10001703# endif
1704#endif
1705#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10001706 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
1707 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10001708#endif
Damien Miller01ed2272008-11-05 16:20:46 +11001709#ifdef JPAKE
1710 dump_cfg_fmtint(sZeroKnowledgePasswordAuthentication,
1711 o->zero_knowledge_password_authentication);
1712#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001713 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
1714 dump_cfg_fmtint(sKbdInteractiveAuthentication,
1715 o->kbd_interactive_authentication);
1716 dump_cfg_fmtint(sChallengeResponseAuthentication,
1717 o->challenge_response_authentication);
1718 dump_cfg_fmtint(sPrintMotd, o->print_motd);
1719 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
1720 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
1721 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
1722 dump_cfg_fmtint(sStrictModes, o->strict_modes);
1723 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
1724 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
1725 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
1726 dump_cfg_fmtint(sUseLogin, o->use_login);
1727 dump_cfg_fmtint(sCompression, o->compression);
1728 dump_cfg_fmtint(sGatewayPorts, o->gateway_ports);
1729 dump_cfg_fmtint(sUseDNS, o->use_dns);
1730 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
1731 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
1732
1733 /* string arguments */
1734 dump_cfg_string(sPidFile, o->pid_file);
1735 dump_cfg_string(sXAuthLocation, o->xauth_location);
1736 dump_cfg_string(sCiphers, o->ciphers);
1737 dump_cfg_string(sMacs, o->macs);
1738 dump_cfg_string(sBanner, o->banner);
1739 dump_cfg_string(sAuthorizedKeysFile, o->authorized_keys_file);
1740 dump_cfg_string(sAuthorizedKeysFile2, o->authorized_keys_file2);
1741 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11001742 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11001743 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
1744 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10001745 dump_cfg_string(sAuthorizedPrincipalsFile,
1746 o->authorized_principals_file);
Darren Tuckere7140f22008-06-10 23:01:51 +10001747
1748 /* string arguments requiring a lookup */
1749 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
1750 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
1751
1752 /* string array arguments */
1753 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
1754 o->host_key_files);
Damien Miller0a80ca12010-02-27 07:55:05 +11001755 dump_cfg_strarray(sHostKeyFile, o->num_host_cert_files,
1756 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10001757 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
1758 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
1759 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
1760 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
1761 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
1762
1763 /* other arguments */
1764 for (i = 0; i < o->num_subsystems; i++)
1765 printf("subsystem %s %s\n", o->subsystem_name[i],
1766 o->subsystem_args[i]);
1767
1768 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
1769 o->max_startups_rate, o->max_startups);
1770
1771 for (i = 0; tunmode_desc[i].val != -1; i++)
1772 if (tunmode_desc[i].val == o->permit_tun) {
1773 s = tunmode_desc[i].text;
1774 break;
1775 }
1776 dump_cfg_string(sPermitTunnel, s);
1777
Damien Miller91475862011-05-05 14:14:34 +10001778 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
1779 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11001780
Darren Tuckere7140f22008-06-10 23:01:51 +10001781 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10001782}