blob: f2d414334448efb8fd0befb091d141f7e4d50b72 [file] [log] [blame]
Damien Miller7fc5c0f2008-11-05 16:12:11 +11001/* $OpenBSD: servconf.c,v 1.189 2008/11/03 08:59:41 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 Millerb8fe89c2006-07-24 14:51:00 +100018#include <netdb.h>
Damien Miller565ca3f2006-08-19 00:23:15 +100019#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100020#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100021#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100022#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100023#include <signal.h>
Damien Millere6b3b612006-07-24 14:01:23 +100024#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100025#include <stdarg.h>
Darren Tuckere7140f22008-06-10 23:01:51 +100026#include <errno.h>
Damien Millerbe43ebf2006-07-24 13:51:51 +100027
Damien Millerb84886b2008-05-19 15:05:07 +100028#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100029#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000031#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100032#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033#include "servconf.h"
Damien Miller78928792000-04-12 20:17:38 +100034#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036#include "misc.h"
37#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000039#include "kex.h"
40#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100041#include "match.h"
Damien Miller9b439df2006-07-24 14:04:00 +100042#include "channels.h"
Damien Miller565ca3f2006-08-19 00:23:15 +100043#include "groupaccess.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044
Ben Lindstrombba81212001-06-25 05:01:22 +000045static void add_listen_addr(ServerOptions *, char *, u_short);
46static void add_one_listen_addr(ServerOptions *, char *, u_short);
Damien Miller34132e52000-01-14 15:45:46 +110047
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000048/* Use of privilege separation or not */
49extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100050extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052/* Initializes the server options to their default values. */
53
Damien Miller4af51302000-04-16 11:18:38 +100054void
Damien Miller95def091999-11-25 00:26:21 +110055initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056{
Damien Miller95def091999-11-25 00:26:21 +110057 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110058
59 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100060 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110061
62 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110063 options->num_ports = 0;
64 options->ports_from_cmdline = 0;
65 options->listen_addrs = NULL;
Darren Tucker0f383232005-01-20 10:57:56 +110066 options->address_family = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110067 options->num_host_key_files = 0;
Damien Miller6f83b8e2000-05-02 09:23:45 +100068 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110069 options->server_key_bits = -1;
70 options->login_grace_time = -1;
71 options->key_regeneration_time = -1;
Ben Lindstromd8a90212001-02-15 03:08:27 +000072 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110073 options->ignore_rhosts = -1;
74 options->ignore_user_known_hosts = -1;
75 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +000076 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +110077 options->x11_forwarding = -1;
78 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +110079 options->x11_use_localhost = -1;
Damien Millerd3a18572000-06-07 19:55:44 +100080 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +110081 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +110082 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +110083 options->log_facility = SYSLOG_FACILITY_NOT_SET;
84 options->log_level = SYSLOG_LEVEL_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110085 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +000086 options->hostbased_authentication = -1;
87 options->hostbased_uses_name_from_packet_only = -1;
Damien Miller95def091999-11-25 00:26:21 +110088 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110089 options->pubkey_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +110090 options->kerberos_authentication = -1;
91 options->kerberos_or_local_passwd = -1;
92 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +110093 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +100094 options->gss_authentication=-1;
95 options->gss_cleanup_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +110096 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +110097 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +000098 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +110099 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000100 options->permit_user_env = -1;
Damien Miller95def091999-11-25 00:26:21 +1100101 options->use_login = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000102 options->compression = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100103 options->allow_tcp_forwarding = -1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000104 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100105 options->num_allow_users = 0;
106 options->num_deny_users = 0;
107 options->num_allow_groups = 0;
108 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000109 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000110 options->macs = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000111 options->protocol = SSH_PROTO_UNKNOWN;
Damien Millere247cc42000-05-07 12:03:14 +1000112 options->gateway_ports = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000113 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000114 options->max_startups_begin = -1;
115 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000116 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000117 options->max_authtries = -1;
Damien Miller7207f642008-05-19 15:34:50 +1000118 options->max_sessions = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000119 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000120 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000121 options->client_alive_interval = -1;
122 options->client_alive_count_max = -1;
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000123 options->authorized_keys_file = NULL;
124 options->authorized_keys_file2 = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +1000125 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100126 options->permit_tun = -1;
Damien Millera765cf42006-07-24 14:08:13 +1000127 options->num_permitted_opens = -1;
Damien Millere2754432006-07-24 14:06:47 +1000128 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100129 options->chroot_directory = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130}
131
Damien Miller4af51302000-04-16 11:18:38 +1000132void
Damien Miller95def091999-11-25 00:26:21 +1100133fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134{
Damien Miller726273e2001-11-12 11:40:11 +1100135 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000136 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000137 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100138
139 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100140 if (options->protocol == SSH_PROTO_UNKNOWN)
141 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
142 if (options->num_host_key_files == 0) {
143 /* fill default hostkeys for protocols */
144 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100145 options->host_key_files[options->num_host_key_files++] =
146 _PATH_HOST_KEY_FILE;
147 if (options->protocol & SSH_PROTO_2) {
148 options->host_key_files[options->num_host_key_files++] =
149 _PATH_HOST_RSA_KEY_FILE;
150 options->host_key_files[options->num_host_key_files++] =
151 _PATH_HOST_DSA_KEY_FILE;
152 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100153 }
Damien Miller34132e52000-01-14 15:45:46 +1100154 if (options->num_ports == 0)
155 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
156 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000157 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000158 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000159 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100160 if (options->server_key_bits == -1)
Darren Tucker7499b0c2008-07-02 22:35:43 +1000161 options->server_key_bits = 1024;
Damien Miller95def091999-11-25 00:26:21 +1100162 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000163 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100164 if (options->key_regeneration_time == -1)
165 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000166 if (options->permit_root_login == PERMIT_NOT_SET)
167 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100168 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100169 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100170 if (options->ignore_user_known_hosts == -1)
171 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100172 if (options->print_motd == -1)
173 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000174 if (options->print_lastlog == -1)
175 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100176 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100177 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100178 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100179 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100180 if (options->x11_use_localhost == -1)
181 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000182 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000183 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100184 if (options->strict_modes == -1)
185 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100186 if (options->tcp_keep_alive == -1)
187 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100188 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100189 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100190 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000191 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100192 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100193 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000194 if (options->hostbased_authentication == -1)
195 options->hostbased_authentication = 0;
196 if (options->hostbased_uses_name_from_packet_only == -1)
197 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100198 if (options->rsa_authentication == -1)
199 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100200 if (options->pubkey_authentication == -1)
201 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100202 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000203 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100204 if (options->kerberos_or_local_passwd == -1)
205 options->kerberos_or_local_passwd = 1;
206 if (options->kerberos_ticket_cleanup == -1)
207 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100208 if (options->kerberos_get_afs_token == -1)
209 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000210 if (options->gss_authentication == -1)
211 options->gss_authentication = 0;
212 if (options->gss_cleanup_creds == -1)
213 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100214 if (options->password_authentication == -1)
215 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100216 if (options->kbd_interactive_authentication == -1)
217 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000218 if (options->challenge_response_authentication == -1)
219 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100220 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100221 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000222 if (options->permit_user_env == -1)
223 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100224 if (options->use_login == -1)
225 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000226 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000227 options->compression = COMP_DELAYED;
Damien Miller50a41ed2000-10-16 12:14:42 +1100228 if (options->allow_tcp_forwarding == -1)
229 options->allow_tcp_forwarding = 1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000230 if (options->allow_agent_forwarding == -1)
231 options->allow_agent_forwarding = 1;
Damien Millere247cc42000-05-07 12:03:14 +1000232 if (options->gateway_ports == -1)
233 options->gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000234 if (options->max_startups == -1)
235 options->max_startups = 10;
Damien Miller942da032000-08-18 13:59:06 +1000236 if (options->max_startups_rate == -1)
237 options->max_startups_rate = 100; /* 100% */
238 if (options->max_startups_begin == -1)
239 options->max_startups_begin = options->max_startups;
Darren Tucker89413db2004-05-24 10:36:23 +1000240 if (options->max_authtries == -1)
241 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000242 if (options->max_sessions == -1)
243 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000244 if (options->use_dns == -1)
245 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000246 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100247 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000248 if (options->client_alive_count_max == -1)
249 options->client_alive_count_max = 3;
Damien Miller75413ac2001-11-12 11:14:35 +1100250 if (options->authorized_keys_file2 == NULL) {
251 /* authorized_keys_file2 falls back to authorized_keys_file */
252 if (options->authorized_keys_file != NULL)
253 options->authorized_keys_file2 = options->authorized_keys_file;
254 else
255 options->authorized_keys_file2 = _PATH_SSH_USER_PERMITTED_KEYS2;
256 }
257 if (options->authorized_keys_file == NULL)
258 options->authorized_keys_file = _PATH_SSH_USER_PERMITTED_KEYS;
Damien Millerd27b9472005-12-13 19:29:02 +1100259 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100260 options->permit_tun = SSH_TUNMODE_NO;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000261
Ben Lindstromfb62a692002-06-06 19:47:11 +0000262 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000263 if (use_privsep == -1)
Ben Lindstromfb62a692002-06-06 19:47:11 +0000264 use_privsep = 1;
Damien Miller4903eb42002-06-21 16:20:44 +1000265
Tim Rice40017b02002-07-14 13:36:49 -0700266#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000267 if (use_privsep && options->compression == 1) {
268 error("This platform does not support both privilege "
269 "separation and compression");
270 error("Compression disabled");
271 options->compression = 0;
272 }
273#endif
274
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275}
276
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100278typedef enum {
279 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100280 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000281 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100282 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100283 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
284 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000285 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100286 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100287 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000288 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100289 sPasswordAuthentication, sKbdInteractiveAuthentication,
290 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000291 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100292 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller12c150e2003-12-17 16:31:10 +1100293 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000294 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Damien Miller50a41ed2000-10-16 12:14:42 +1100295 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000296 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000297 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
Damien Miller7207f642008-05-19 15:34:50 +1000298 sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000299 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100300 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000301 sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
Damien Millerd27b9472005-12-13 19:29:02 +1100302 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100303 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Damien Miller4f755cd2008-05-19 14:57:41 +1000304 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000305 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306} ServerOpCodes;
307
Darren Tucker45150472006-07-12 22:34:17 +1000308#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
309#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
310#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
311
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100313static struct {
314 const char *name;
315 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000316 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100317} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100318 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000319#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000320 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000321#else
Darren Tucker45150472006-07-12 22:34:17 +1000322 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000323#endif
Darren Tucker45150472006-07-12 22:34:17 +1000324 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100325 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000326 { "port", sPort, SSHCFG_GLOBAL },
327 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
328 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
329 { "pidfile", sPidFile, SSHCFG_GLOBAL },
330 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
331 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
332 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100333 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000334 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
335 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
336 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100337 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
338 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000339 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100340 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
341 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000342 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000343#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100344 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000345 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
346 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100347#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000348 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000349#else
Darren Tucker45150472006-07-12 22:34:17 +1000350 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100351#endif
352#else
Darren Tucker1629c072007-02-19 22:25:37 +1100353 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000354 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
355 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
356 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000357#endif
Darren Tucker45150472006-07-12 22:34:17 +1000358 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
359 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000360#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100361 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000362 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000363#else
Darren Tucker1629c072007-02-19 22:25:37 +1100364 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000365 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000366#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100367 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
368 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100369 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000370 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
371 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
372 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
373 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
374 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
375 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
376 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
377 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000378 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
379 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
380 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000381 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
382 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100383 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000384 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
385 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
386 { "compression", sCompression, SSHCFG_GLOBAL },
387 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
388 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
389 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000390 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000391 { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
392 { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
393 { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
394 { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
395 { "ciphers", sCiphers, SSHCFG_GLOBAL },
396 { "macs", sMacs, SSHCFG_GLOBAL },
397 { "protocol", sProtocol, SSHCFG_GLOBAL },
398 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
399 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
400 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000401 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000402 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100403 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000404 { "usedns", sUseDNS, SSHCFG_GLOBAL },
405 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
406 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
407 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
408 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
409 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_GLOBAL },
410 { "authorizedkeysfile2", sAuthorizedKeysFile2, SSHCFG_GLOBAL },
411 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL },
412 { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
413 { "permittunnel", sPermitTunnel, SSHCFG_GLOBAL },
Damien Miller9b439df2006-07-24 14:04:00 +1000414 { "match", sMatch, SSHCFG_ALL },
415 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000416 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100417 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000418 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419};
420
Darren Tuckere7140f22008-06-10 23:01:51 +1000421static struct {
422 int val;
423 char *text;
424} tunmode_desc[] = {
425 { SSH_TUNMODE_NO, "no" },
426 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
427 { SSH_TUNMODE_ETHERNET, "ethernet" },
428 { SSH_TUNMODE_YES, "yes" },
429 { -1, NULL }
430};
431
Damien Miller5428f641999-11-25 11:54:57 +1100432/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000433 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100434 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435
Damien Miller4af51302000-04-16 11:18:38 +1000436static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100437parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000438 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000440 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441
Damien Miller95def091999-11-25 00:26:21 +1100442 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000443 if (strcasecmp(cp, keywords[i].name) == 0) {
444 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100445 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000446 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000448 error("%s: line %d: Bad configuration option: %s",
449 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100450 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451}
452
Ben Lindstrombba81212001-06-25 05:01:22 +0000453static void
Ben Lindstrom19066a12001-04-12 23:39:26 +0000454add_listen_addr(ServerOptions *options, char *addr, u_short port)
Damien Miller34132e52000-01-14 15:45:46 +1100455{
Damien Millereccb9de2005-06-17 12:59:34 +1000456 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100457
458 if (options->num_ports == 0)
459 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100460 if (options->address_family == -1)
461 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000462 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000463 for (i = 0; i < options->num_ports; i++)
464 add_one_listen_addr(options, addr, options->ports[i]);
465 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000466 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000467}
468
Ben Lindstrombba81212001-06-25 05:01:22 +0000469static void
Ben Lindstromc510af42001-04-07 17:25:48 +0000470add_one_listen_addr(ServerOptions *options, char *addr, u_short port)
471{
472 struct addrinfo hints, *ai, *aitop;
473 char strport[NI_MAXSERV];
474 int gaierr;
475
476 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100477 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000478 hints.ai_socktype = SOCK_STREAM;
479 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Ben Lindstrome1353632002-06-23 21:29:23 +0000480 snprintf(strport, sizeof strport, "%u", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000481 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
482 fatal("bad addr or host: %s (%s)",
483 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100484 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000485 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
486 ;
487 ai->ai_next = options->listen_addrs;
488 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100489}
490
Darren Tucker45150472006-07-12 22:34:17 +1000491/*
492 * The strategy for the Match blocks is that the config file is parsed twice.
493 *
494 * The first time is at startup. activep is initialized to 1 and the
495 * directives in the global context are processed and acted on. Hitting a
496 * Match directive unsets activep and the directives inside the block are
497 * checked for syntax only.
498 *
499 * The second time is after a connection has been established but before
500 * authentication. activep is initialized to 2 and global config directives
501 * are ignored since they have already been processed. If the criteria in a
502 * Match block is met, activep is set and the subsequent directives
503 * processed and actioned until EOF or another Match block unsets it. Any
504 * options set are copied into the main server config.
505 *
506 * Potential additions/improvements:
507 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
508 *
509 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
510 * Match Address 192.168.0.*
511 * Tag trusted
512 * Match Group wheel
513 * Tag trusted
514 * Match Tag trusted
515 * AllowTcpForwarding yes
516 * GatewayPorts clientspecified
517 * [...]
518 *
519 * - Add a PermittedChannelRequests directive
520 * Match Group shell
521 * PermittedChannelRequests session,forwarded-tcpip
522 */
523
524static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000525match_cfg_line_group(const char *grps, int line, const char *user)
526{
527 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000528 struct passwd *pw;
529
Damien Miller565ca3f2006-08-19 00:23:15 +1000530 if (user == NULL)
531 goto out;
532
533 if ((pw = getpwnam(user)) == NULL) {
534 debug("Can't match group at line %d because user %.100s does "
535 "not exist", line, user);
536 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
537 debug("Can't Match group because user %.100s not in any group "
538 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000539 } else if (ga_match_pattern_list(grps) != 1) {
540 debug("user %.100s does not match group list %.100s at line %d",
541 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000542 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000543 debug("user %.100s matched group list %.100s at line %d", user,
544 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000545 result = 1;
546 }
547out:
548 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000549 return result;
550}
551
552static int
Darren Tucker45150472006-07-12 22:34:17 +1000553match_cfg_line(char **condition, int line, const char *user, const char *host,
554 const char *address)
555{
556 int result = 1;
557 char *arg, *attrib, *cp = *condition;
558 size_t len;
559
560 if (user == NULL)
561 debug3("checking syntax for 'Match %s'", cp);
562 else
563 debug3("checking match for '%s' user %s host %s addr %s", cp,
564 user ? user : "(null)", host ? host : "(null)",
565 address ? address : "(null)");
566
567 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
568 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
569 error("Missing Match criteria for %s", attrib);
570 return -1;
571 }
572 len = strlen(arg);
573 if (strcasecmp(attrib, "user") == 0) {
574 if (!user) {
575 result = 0;
576 continue;
577 }
578 if (match_pattern_list(user, arg, len, 0) != 1)
579 result = 0;
580 else
581 debug("user %.100s matched 'User %.100s' at "
582 "line %d", user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000583 } else if (strcasecmp(attrib, "group") == 0) {
584 switch (match_cfg_line_group(arg, line, user)) {
585 case -1:
586 return -1;
587 case 0:
588 result = 0;
589 }
Darren Tucker45150472006-07-12 22:34:17 +1000590 } else if (strcasecmp(attrib, "host") == 0) {
591 if (!host) {
592 result = 0;
593 continue;
594 }
595 if (match_hostname(host, arg, len) != 1)
596 result = 0;
597 else
598 debug("connection from %.100s matched 'Host "
599 "%.100s' at line %d", host, arg, line);
600 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000601 switch (addr_match_list(address, arg)) {
602 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000603 debug("connection from %.100s matched 'Address "
604 "%.100s' at line %d", address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000605 break;
606 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000607 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000608 result = 0;
609 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000610 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000611 return -1;
612 }
Darren Tucker45150472006-07-12 22:34:17 +1000613 } else {
614 error("Unsupported Match attribute %s", attrib);
615 return -1;
616 }
617 }
618 if (user != NULL)
619 debug3("match %sfound", result ? "" : "not ");
620 *condition = cp;
621 return result;
622}
623
Damien Millere2754432006-07-24 14:06:47 +1000624#define WHITESPACE " \t\r\n"
625
Ben Lindstromade03f62001-12-06 18:22:17 +0000626int
627process_server_config_line(ServerOptions *options, char *line,
Darren Tucker45150472006-07-12 22:34:17 +1000628 const char *filename, int linenum, int *activep, const char *user,
629 const char *host, const char *address)
Ben Lindstromade03f62001-12-06 18:22:17 +0000630{
631 char *cp, **charptr, *arg, *p;
Darren Tucker45150472006-07-12 22:34:17 +1000632 int cmdline = 0, *intptr, value, n;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100633 SyslogFacility *log_facility_ptr;
634 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000635 ServerOpCodes opcode;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100636 u_short port;
Darren Tucker45150472006-07-12 22:34:17 +1000637 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000638 size_t len;
Ben Lindstromade03f62001-12-06 18:22:17 +0000639
640 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100641 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100642 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000643 /* Ignore leading whitespace */
644 if (*arg == '\0')
645 arg = strdelim(&cp);
646 if (!arg || !*arg || *arg == '#')
647 return 0;
648 intptr = NULL;
649 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000650 opcode = parse_token(arg, filename, linenum, &flags);
651
652 if (activep == NULL) { /* We are processing a command line directive */
653 cmdline = 1;
654 activep = &cmdline;
655 }
656 if (*activep && opcode != sMatch)
657 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
658 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
659 if (user == NULL) {
660 fatal("%s line %d: Directive '%s' is not allowed "
661 "within a Match block", filename, linenum, arg);
662 } else { /* this is a directive we have already processed */
663 while (arg)
664 arg = strdelim(&cp);
665 return 0;
666 }
667 }
668
Ben Lindstromade03f62001-12-06 18:22:17 +0000669 switch (opcode) {
670 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000671 case sUsePAM:
672 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000673 goto parse_flag;
674
675 /* Standard Options */
676 case sBadOption:
677 return -1;
678 case sPort:
679 /* ignore ports from configfile if cmdline specifies ports */
680 if (options->ports_from_cmdline)
681 return 0;
682 if (options->listen_addrs != NULL)
683 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100684 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000685 if (options->num_ports >= MAX_PORTS)
686 fatal("%s line %d: too many ports.",
687 filename, linenum);
688 arg = strdelim(&cp);
689 if (!arg || *arg == '\0')
690 fatal("%s line %d: missing port number.",
691 filename, linenum);
692 options->ports[options->num_ports++] = a2port(arg);
693 if (options->ports[options->num_ports-1] == 0)
694 fatal("%s line %d: Badly formatted port number.",
695 filename, linenum);
696 break;
697
698 case sServerKeyBits:
699 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +1000700 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +0000701 arg = strdelim(&cp);
702 if (!arg || *arg == '\0')
703 fatal("%s line %d: missing integer value.",
704 filename, linenum);
705 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000706 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000707 *intptr = value;
708 break;
709
710 case sLoginGraceTime:
711 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +1000712 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +0000713 arg = strdelim(&cp);
714 if (!arg || *arg == '\0')
715 fatal("%s line %d: missing time value.",
716 filename, linenum);
717 if ((value = convtime(arg)) == -1)
718 fatal("%s line %d: invalid time value.",
719 filename, linenum);
720 if (*intptr == -1)
721 *intptr = value;
722 break;
723
724 case sKeyRegenerationTime:
725 intptr = &options->key_regeneration_time;
726 goto parse_time;
727
728 case sListenAddress:
729 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100730 if (arg == NULL || *arg == '\0')
731 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000732 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000733 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
734 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
735 && strchr(p+1, ':') != NULL) {
736 add_listen_addr(options, arg, 0);
737 break;
738 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100739 p = hpdelim(&arg);
740 if (p == NULL)
741 fatal("%s line %d: bad address:port usage",
742 filename, linenum);
743 p = cleanhostname(p);
744 if (arg == NULL)
745 port = 0;
746 else if ((port = a2port(arg)) == 0)
747 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000748
Damien Millerf91ee4c2005-03-01 21:24:33 +1100749 add_listen_addr(options, p, port);
750
Ben Lindstromade03f62001-12-06 18:22:17 +0000751 break;
752
Darren Tucker0f383232005-01-20 10:57:56 +1100753 case sAddressFamily:
754 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000755 if (!arg || *arg == '\0')
756 fatal("%s line %d: missing address family.",
757 filename, linenum);
Darren Tucker0f383232005-01-20 10:57:56 +1100758 intptr = &options->address_family;
759 if (options->listen_addrs != NULL)
760 fatal("%s line %d: address family must be specified before "
761 "ListenAddress.", filename, linenum);
762 if (strcasecmp(arg, "inet") == 0)
763 value = AF_INET;
764 else if (strcasecmp(arg, "inet6") == 0)
765 value = AF_INET6;
766 else if (strcasecmp(arg, "any") == 0)
767 value = AF_UNSPEC;
768 else
769 fatal("%s line %d: unsupported address family \"%s\".",
770 filename, linenum, arg);
771 if (*intptr == -1)
772 *intptr = value;
773 break;
774
Ben Lindstromade03f62001-12-06 18:22:17 +0000775 case sHostKeyFile:
776 intptr = &options->num_host_key_files;
777 if (*intptr >= MAX_HOSTKEYS)
778 fatal("%s line %d: too many host keys specified (max %d).",
779 filename, linenum, MAX_HOSTKEYS);
780 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +1000781 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +0000782 arg = strdelim(&cp);
783 if (!arg || *arg == '\0')
784 fatal("%s line %d: missing file name.",
785 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +1000786 if (*activep && *charptr == NULL) {
Ben Lindstromade03f62001-12-06 18:22:17 +0000787 *charptr = tilde_expand_filename(arg, getuid());
788 /* increase optional counter */
789 if (intptr != NULL)
790 *intptr = *intptr + 1;
791 }
792 break;
793
794 case sPidFile:
795 charptr = &options->pid_file;
796 goto parse_filename;
797
798 case sPermitRootLogin:
799 intptr = &options->permit_root_login;
800 arg = strdelim(&cp);
801 if (!arg || *arg == '\0')
802 fatal("%s line %d: missing yes/"
803 "without-password/forced-commands-only/no "
804 "argument.", filename, linenum);
805 value = 0; /* silence compiler */
806 if (strcmp(arg, "without-password") == 0)
807 value = PERMIT_NO_PASSWD;
808 else if (strcmp(arg, "forced-commands-only") == 0)
809 value = PERMIT_FORCED_ONLY;
810 else if (strcmp(arg, "yes") == 0)
811 value = PERMIT_YES;
812 else if (strcmp(arg, "no") == 0)
813 value = PERMIT_NO;
814 else
815 fatal("%s line %d: Bad yes/"
816 "without-password/forced-commands-only/no "
817 "argument: %s", filename, linenum, arg);
Darren Tucker15f94272008-01-01 20:36:56 +1100818 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000819 *intptr = value;
820 break;
821
822 case sIgnoreRhosts:
823 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +1000824 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +0000825 arg = strdelim(&cp);
826 if (!arg || *arg == '\0')
827 fatal("%s line %d: missing yes/no argument.",
828 filename, linenum);
829 value = 0; /* silence compiler */
830 if (strcmp(arg, "yes") == 0)
831 value = 1;
832 else if (strcmp(arg, "no") == 0)
833 value = 0;
834 else
835 fatal("%s line %d: Bad yes/no argument: %s",
836 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +1000837 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000838 *intptr = value;
839 break;
840
841 case sIgnoreUserKnownHosts:
842 intptr = &options->ignore_user_known_hosts;
843 goto parse_flag;
844
Ben Lindstromade03f62001-12-06 18:22:17 +0000845 case sRhostsRSAAuthentication:
846 intptr = &options->rhosts_rsa_authentication;
847 goto parse_flag;
848
849 case sHostbasedAuthentication:
850 intptr = &options->hostbased_authentication;
851 goto parse_flag;
852
853 case sHostbasedUsesNameFromPacketOnly:
854 intptr = &options->hostbased_uses_name_from_packet_only;
855 goto parse_flag;
856
857 case sRSAAuthentication:
858 intptr = &options->rsa_authentication;
859 goto parse_flag;
860
861 case sPubkeyAuthentication:
862 intptr = &options->pubkey_authentication;
863 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000864
Ben Lindstromade03f62001-12-06 18:22:17 +0000865 case sKerberosAuthentication:
866 intptr = &options->kerberos_authentication;
867 goto parse_flag;
868
869 case sKerberosOrLocalPasswd:
870 intptr = &options->kerberos_or_local_passwd;
871 goto parse_flag;
872
873 case sKerberosTicketCleanup:
874 intptr = &options->kerberos_ticket_cleanup;
875 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000876
Darren Tucker22ef5082003-12-31 11:37:34 +1100877 case sKerberosGetAFSToken:
878 intptr = &options->kerberos_get_afs_token;
879 goto parse_flag;
880
Darren Tucker0efd1552003-08-26 11:49:55 +1000881 case sGssAuthentication:
882 intptr = &options->gss_authentication;
883 goto parse_flag;
884
885 case sGssCleanupCreds:
886 intptr = &options->gss_cleanup_creds;
887 goto parse_flag;
888
Ben Lindstromade03f62001-12-06 18:22:17 +0000889 case sPasswordAuthentication:
890 intptr = &options->password_authentication;
891 goto parse_flag;
892
893 case sKbdInteractiveAuthentication:
894 intptr = &options->kbd_interactive_authentication;
895 goto parse_flag;
896
897 case sChallengeResponseAuthentication:
898 intptr = &options->challenge_response_authentication;
899 goto parse_flag;
900
901 case sPrintMotd:
902 intptr = &options->print_motd;
903 goto parse_flag;
904
905 case sPrintLastLog:
906 intptr = &options->print_lastlog;
907 goto parse_flag;
908
909 case sX11Forwarding:
910 intptr = &options->x11_forwarding;
911 goto parse_flag;
912
913 case sX11DisplayOffset:
914 intptr = &options->x11_display_offset;
915 goto parse_int;
916
Damien Miller95c249f2002-02-05 12:11:34 +1100917 case sX11UseLocalhost:
918 intptr = &options->x11_use_localhost;
919 goto parse_flag;
920
Ben Lindstromade03f62001-12-06 18:22:17 +0000921 case sXAuthLocation:
922 charptr = &options->xauth_location;
923 goto parse_filename;
924
925 case sStrictModes:
926 intptr = &options->strict_modes;
927 goto parse_flag;
928
Damien Miller12c150e2003-12-17 16:31:10 +1100929 case sTCPKeepAlive:
930 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +0000931 goto parse_flag;
932
933 case sEmptyPasswd:
934 intptr = &options->permit_empty_passwd;
935 goto parse_flag;
936
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000937 case sPermitUserEnvironment:
938 intptr = &options->permit_user_env;
939 goto parse_flag;
940
Ben Lindstromade03f62001-12-06 18:22:17 +0000941 case sUseLogin:
942 intptr = &options->use_login;
943 goto parse_flag;
944
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000945 case sCompression:
946 intptr = &options->compression;
Damien Miller9786e6e2005-07-26 21:54:56 +1000947 arg = strdelim(&cp);
948 if (!arg || *arg == '\0')
949 fatal("%s line %d: missing yes/no/delayed "
950 "argument.", filename, linenum);
951 value = 0; /* silence compiler */
952 if (strcmp(arg, "delayed") == 0)
953 value = COMP_DELAYED;
954 else if (strcmp(arg, "yes") == 0)
955 value = COMP_ZLIB;
956 else if (strcmp(arg, "no") == 0)
957 value = COMP_NONE;
958 else
959 fatal("%s line %d: Bad yes/no/delayed "
960 "argument: %s", filename, linenum, arg);
961 if (*intptr == -1)
962 *intptr = value;
963 break;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000964
Ben Lindstromade03f62001-12-06 18:22:17 +0000965 case sGatewayPorts:
966 intptr = &options->gateway_ports;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100967 arg = strdelim(&cp);
968 if (!arg || *arg == '\0')
969 fatal("%s line %d: missing yes/no/clientspecified "
970 "argument.", filename, linenum);
971 value = 0; /* silence compiler */
972 if (strcmp(arg, "clientspecified") == 0)
973 value = 2;
974 else if (strcmp(arg, "yes") == 0)
975 value = 1;
976 else if (strcmp(arg, "no") == 0)
977 value = 0;
978 else
979 fatal("%s line %d: Bad yes/no/clientspecified "
980 "argument: %s", filename, linenum, arg);
Darren Tucker82347a82007-02-25 20:37:52 +1100981 if (*activep && *intptr == -1)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100982 *intptr = value;
983 break;
Ben Lindstromade03f62001-12-06 18:22:17 +0000984
Damien Miller3a961dc2003-06-03 10:25:48 +1000985 case sUseDNS:
986 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +0000987 goto parse_flag;
988
989 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100990 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +0000991 arg = strdelim(&cp);
992 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100993 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +0000994 fatal("%.200s line %d: unsupported log facility '%s'",
995 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100996 if (*log_facility_ptr == -1)
997 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +0000998 break;
999
1000 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001001 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001002 arg = strdelim(&cp);
1003 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001004 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001005 fatal("%.200s line %d: unsupported log level '%s'",
1006 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001007 if (*log_level_ptr == -1)
1008 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001009 break;
1010
1011 case sAllowTcpForwarding:
1012 intptr = &options->allow_tcp_forwarding;
1013 goto parse_flag;
1014
Damien Miller4f755cd2008-05-19 14:57:41 +10001015 case sAllowAgentForwarding:
1016 intptr = &options->allow_agent_forwarding;
1017 goto parse_flag;
1018
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001019 case sUsePrivilegeSeparation:
1020 intptr = &use_privsep;
1021 goto parse_flag;
1022
Ben Lindstromade03f62001-12-06 18:22:17 +00001023 case sAllowUsers:
1024 while ((arg = strdelim(&cp)) && *arg != '\0') {
1025 if (options->num_allow_users >= MAX_ALLOW_USERS)
1026 fatal("%s line %d: too many allow users.",
1027 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001028 options->allow_users[options->num_allow_users++] =
1029 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001030 }
1031 break;
1032
1033 case sDenyUsers:
1034 while ((arg = strdelim(&cp)) && *arg != '\0') {
1035 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001036 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001037 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001038 options->deny_users[options->num_deny_users++] =
1039 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001040 }
1041 break;
1042
1043 case sAllowGroups:
1044 while ((arg = strdelim(&cp)) && *arg != '\0') {
1045 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1046 fatal("%s line %d: too many allow groups.",
1047 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001048 options->allow_groups[options->num_allow_groups++] =
1049 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001050 }
1051 break;
1052
1053 case sDenyGroups:
1054 while ((arg = strdelim(&cp)) && *arg != '\0') {
1055 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1056 fatal("%s line %d: too many deny groups.",
1057 filename, linenum);
1058 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
1059 }
1060 break;
1061
1062 case sCiphers:
1063 arg = strdelim(&cp);
1064 if (!arg || *arg == '\0')
1065 fatal("%s line %d: Missing argument.", filename, linenum);
1066 if (!ciphers_valid(arg))
1067 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1068 filename, linenum, arg ? arg : "<NONE>");
1069 if (options->ciphers == NULL)
1070 options->ciphers = xstrdup(arg);
1071 break;
1072
1073 case sMacs:
1074 arg = strdelim(&cp);
1075 if (!arg || *arg == '\0')
1076 fatal("%s line %d: Missing argument.", filename, linenum);
1077 if (!mac_valid(arg))
1078 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1079 filename, linenum, arg ? arg : "<NONE>");
1080 if (options->macs == NULL)
1081 options->macs = xstrdup(arg);
1082 break;
1083
1084 case sProtocol:
1085 intptr = &options->protocol;
1086 arg = strdelim(&cp);
1087 if (!arg || *arg == '\0')
1088 fatal("%s line %d: Missing argument.", filename, linenum);
1089 value = proto_spec(arg);
1090 if (value == SSH_PROTO_UNKNOWN)
1091 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001092 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001093 if (*intptr == SSH_PROTO_UNKNOWN)
1094 *intptr = value;
1095 break;
1096
1097 case sSubsystem:
1098 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1099 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001100 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001101 }
1102 arg = strdelim(&cp);
1103 if (!arg || *arg == '\0')
1104 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001105 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001106 if (!*activep) {
1107 arg = strdelim(&cp);
1108 break;
1109 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001110 for (i = 0; i < options->num_subsystems; i++)
1111 if (strcmp(arg, options->subsystem_name[i]) == 0)
1112 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001113 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001114 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1115 arg = strdelim(&cp);
1116 if (!arg || *arg == '\0')
1117 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001118 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001119 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001120
1121 /* Collect arguments (separate to executable) */
1122 p = xstrdup(arg);
1123 len = strlen(p) + 1;
1124 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1125 len += 1 + strlen(arg);
1126 p = xrealloc(p, 1, len);
1127 strlcat(p, " ", len);
1128 strlcat(p, arg, len);
1129 }
1130 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001131 options->num_subsystems++;
1132 break;
1133
1134 case sMaxStartups:
1135 arg = strdelim(&cp);
1136 if (!arg || *arg == '\0')
1137 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001138 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001139 if ((n = sscanf(arg, "%d:%d:%d",
1140 &options->max_startups_begin,
1141 &options->max_startups_rate,
1142 &options->max_startups)) == 3) {
1143 if (options->max_startups_begin >
1144 options->max_startups ||
1145 options->max_startups_rate > 100 ||
1146 options->max_startups_rate < 1)
1147 fatal("%s line %d: Illegal MaxStartups spec.",
1148 filename, linenum);
1149 } else if (n != 1)
1150 fatal("%s line %d: Illegal MaxStartups spec.",
1151 filename, linenum);
1152 else
1153 options->max_startups = options->max_startups_begin;
1154 break;
1155
Darren Tucker89413db2004-05-24 10:36:23 +10001156 case sMaxAuthTries:
1157 intptr = &options->max_authtries;
1158 goto parse_int;
1159
Damien Miller7207f642008-05-19 15:34:50 +10001160 case sMaxSessions:
1161 intptr = &options->max_sessions;
1162 goto parse_int;
1163
Ben Lindstromade03f62001-12-06 18:22:17 +00001164 case sBanner:
1165 charptr = &options->banner;
1166 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001167
Ben Lindstromade03f62001-12-06 18:22:17 +00001168 /*
1169 * These options can contain %X options expanded at
1170 * connect time, so that you can specify paths like:
1171 *
1172 * AuthorizedKeysFile /etc/ssh_keys/%u
1173 */
1174 case sAuthorizedKeysFile:
1175 case sAuthorizedKeysFile2:
Damien Miller4dec5d72006-08-05 11:38:40 +10001176 charptr = (opcode == sAuthorizedKeysFile) ?
Ben Lindstromade03f62001-12-06 18:22:17 +00001177 &options->authorized_keys_file :
1178 &options->authorized_keys_file2;
1179 goto parse_filename;
1180
1181 case sClientAliveInterval:
1182 intptr = &options->client_alive_interval;
1183 goto parse_time;
1184
1185 case sClientAliveCountMax:
1186 intptr = &options->client_alive_count_max;
1187 goto parse_int;
1188
Darren Tucker46bc0752004-05-02 22:11:30 +10001189 case sAcceptEnv:
1190 while ((arg = strdelim(&cp)) && *arg != '\0') {
1191 if (strchr(arg, '=') != NULL)
1192 fatal("%s line %d: Invalid environment name.",
1193 filename, linenum);
1194 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1195 fatal("%s line %d: too many allow env.",
1196 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001197 if (!*activep)
1198 break;
Darren Tucker46bc0752004-05-02 22:11:30 +10001199 options->accept_env[options->num_accept_env++] =
1200 xstrdup(arg);
1201 }
1202 break;
1203
Damien Millerd27b9472005-12-13 19:29:02 +11001204 case sPermitTunnel:
1205 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001206 arg = strdelim(&cp);
1207 if (!arg || *arg == '\0')
1208 fatal("%s line %d: Missing yes/point-to-point/"
1209 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001210 value = -1;
1211 for (i = 0; tunmode_desc[i].val != -1; i++)
1212 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1213 value = tunmode_desc[i].val;
1214 break;
1215 }
1216 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001217 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1218 "no argument: %s", filename, linenum, arg);
1219 if (*intptr == -1)
1220 *intptr = value;
1221 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001222
Darren Tucker45150472006-07-12 22:34:17 +10001223 case sMatch:
1224 if (cmdline)
1225 fatal("Match directive not supported as a command-line "
1226 "option");
1227 value = match_cfg_line(&cp, linenum, user, host, address);
1228 if (value < 0)
1229 fatal("%s line %d: Bad Match condition", filename,
1230 linenum);
1231 *activep = value;
1232 break;
1233
Damien Miller9b439df2006-07-24 14:04:00 +10001234 case sPermitOpen:
1235 arg = strdelim(&cp);
1236 if (!arg || *arg == '\0')
1237 fatal("%s line %d: missing PermitOpen specification",
1238 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001239 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001240 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001241 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001242 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001243 options->num_permitted_opens = 0;
1244 }
Damien Miller9b439df2006-07-24 14:04:00 +10001245 break;
1246 }
Damien Millera29b95e2007-01-05 16:28:36 +11001247 if (*activep && n == -1)
1248 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001249 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1250 p = hpdelim(&arg);
1251 if (p == NULL)
1252 fatal("%s line %d: missing host in PermitOpen",
1253 filename, linenum);
1254 p = cleanhostname(p);
1255 if (arg == NULL || (port = a2port(arg)) == 0)
1256 fatal("%s line %d: bad port number in "
1257 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001258 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001259 options->num_permitted_opens =
1260 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001261 }
Damien Miller9b439df2006-07-24 14:04:00 +10001262 break;
1263
Damien Millere2754432006-07-24 14:06:47 +10001264 case sForceCommand:
1265 if (cp == NULL)
1266 fatal("%.200s line %d: Missing argument.", filename,
1267 linenum);
1268 len = strspn(cp, WHITESPACE);
1269 if (*activep && options->adm_forced_command == NULL)
1270 options->adm_forced_command = xstrdup(cp + len);
1271 return 0;
1272
Damien Millerd8cb1f12008-02-10 22:40:12 +11001273 case sChrootDirectory:
1274 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001275
1276 arg = strdelim(&cp);
1277 if (!arg || *arg == '\0')
1278 fatal("%s line %d: missing file name.",
1279 filename, linenum);
1280 if (*activep && *charptr == NULL)
1281 *charptr = xstrdup(arg);
1282 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001283
Ben Lindstromade03f62001-12-06 18:22:17 +00001284 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001285 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001286 filename, linenum, arg);
1287 while (arg)
1288 arg = strdelim(&cp);
1289 break;
1290
Damien Millerf9b3feb2003-05-16 11:38:32 +10001291 case sUnsupported:
1292 logit("%s line %d: Unsupported option %s",
1293 filename, linenum, arg);
1294 while (arg)
1295 arg = strdelim(&cp);
1296 break;
1297
Ben Lindstromade03f62001-12-06 18:22:17 +00001298 default:
1299 fatal("%s line %d: Missing handler for opcode %s (%d)",
1300 filename, linenum, arg, opcode);
1301 }
1302 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1303 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1304 filename, linenum, arg);
1305 return 0;
1306}
1307
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001308/* Reads the server configuration file. */
1309
Damien Miller4af51302000-04-16 11:18:38 +10001310void
Darren Tucker645ab752004-06-25 13:33:20 +10001311load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001312{
Darren Tucker645ab752004-06-25 13:33:20 +10001313 char line[1024], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001314 FILE *f;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001315
Darren Tucker645ab752004-06-25 13:33:20 +10001316 debug2("%s: filename %s", __func__, filename);
1317 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001318 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001319 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001320 }
Darren Tucker645ab752004-06-25 13:33:20 +10001321 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001322 while (fgets(line, sizeof(line), f)) {
Darren Tucker645ab752004-06-25 13:33:20 +10001323 /*
1324 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001325 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001326 * line numbers later for error messages
1327 */
1328 if ((cp = strchr(line, '#')) != NULL)
1329 memcpy(cp, "\n", 2);
1330 cp = line + strspn(line, " \t\r");
1331
1332 buffer_append(conf, cp, strlen(cp));
1333 }
1334 buffer_append(conf, "\0", 1);
1335 fclose(f);
1336 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1337}
1338
1339void
Darren Tucker45150472006-07-12 22:34:17 +10001340parse_server_match_config(ServerOptions *options, const char *user,
1341 const char *host, const char *address)
Darren Tucker645ab752004-06-25 13:33:20 +10001342{
Darren Tucker45150472006-07-12 22:34:17 +10001343 ServerOptions mo;
1344
1345 initialize_server_options(&mo);
1346 parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
Darren Tucker1629c072007-02-19 22:25:37 +11001347 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001348}
1349
Darren Tucker1629c072007-02-19 22:25:37 +11001350/* Helper macros */
1351#define M_CP_INTOPT(n) do {\
1352 if (src->n != -1) \
1353 dst->n = src->n; \
1354} while (0)
1355#define M_CP_STROPT(n) do {\
1356 if (src->n != NULL) { \
1357 if (dst->n != NULL) \
1358 xfree(dst->n); \
1359 dst->n = src->n; \
1360 } \
1361} while(0)
1362
1363/*
1364 * Copy any supported values that are set.
1365 *
1366 * If the preauth flag is set, we do not bother copying the the string or
1367 * array values that are not used pre-authentication, because any that we
1368 * do use must be explictly sent in mm_getpwnamallow().
1369 */
Darren Tucker45150472006-07-12 22:34:17 +10001370void
Darren Tucker1629c072007-02-19 22:25:37 +11001371copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001372{
Darren Tucker1629c072007-02-19 22:25:37 +11001373 M_CP_INTOPT(password_authentication);
1374 M_CP_INTOPT(gss_authentication);
1375 M_CP_INTOPT(rsa_authentication);
1376 M_CP_INTOPT(pubkey_authentication);
1377 M_CP_INTOPT(kerberos_authentication);
1378 M_CP_INTOPT(hostbased_authentication);
1379 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001380 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001381 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001382
1383 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001384 M_CP_INTOPT(allow_agent_forwarding);
Darren Tucker1629c072007-02-19 22:25:37 +11001385 M_CP_INTOPT(gateway_ports);
1386 M_CP_INTOPT(x11_display_offset);
1387 M_CP_INTOPT(x11_forwarding);
1388 M_CP_INTOPT(x11_use_localhost);
Damien Miller7207f642008-05-19 15:34:50 +10001389 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001390 M_CP_INTOPT(max_authtries);
Darren Tucker1629c072007-02-19 22:25:37 +11001391
1392 M_CP_STROPT(banner);
1393 if (preauth)
1394 return;
1395 M_CP_STROPT(adm_forced_command);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001396 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001397}
1398
Darren Tucker1629c072007-02-19 22:25:37 +11001399#undef M_CP_INTOPT
1400#undef M_CP_STROPT
1401
Darren Tucker45150472006-07-12 22:34:17 +10001402void
1403parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1404 const char *user, const char *host, const char *address)
1405{
1406 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001407 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001408
1409 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1410
Darren Tucker9fbac712004-08-12 22:41:44 +10001411 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tucker45150472006-07-12 22:34:17 +10001412 active = user ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001413 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001414 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001415 if (process_server_config_line(options, cp, filename,
Darren Tucker45150472006-07-12 22:34:17 +10001416 linenum++, &active, user, host, address) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001417 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001418 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001419 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001420 if (bad_options > 0)
1421 fatal("%s: terminating, %d bad configuration options",
1422 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423}
Darren Tuckere7140f22008-06-10 23:01:51 +10001424
1425static const char *
1426fmt_intarg(ServerOpCodes code, int val)
1427{
1428 if (code == sAddressFamily) {
1429 switch (val) {
1430 case AF_INET:
1431 return "inet";
1432 case AF_INET6:
1433 return "inet6";
1434 case AF_UNSPEC:
1435 return "any";
1436 default:
1437 return "UNKNOWN";
1438 }
1439 }
1440 if (code == sPermitRootLogin) {
1441 switch (val) {
1442 case PERMIT_NO_PASSWD:
1443 return "without-passord";
1444 case PERMIT_FORCED_ONLY:
1445 return "forced-commands-only";
1446 case PERMIT_YES:
1447 return "yes";
1448 }
1449 }
1450 if (code == sProtocol) {
1451 switch (val) {
1452 case SSH_PROTO_1:
1453 return "1";
1454 case SSH_PROTO_2:
1455 return "2";
1456 case (SSH_PROTO_1|SSH_PROTO_2):
1457 return "2,1";
1458 default:
1459 return "UNKNOWN";
1460 }
1461 }
1462 if (code == sGatewayPorts && val == 2)
1463 return "clientspecified";
1464 if (code == sCompression && val == COMP_DELAYED)
1465 return "delayed";
1466 switch (val) {
1467 case -1:
1468 return "unset";
1469 case 0:
1470 return "no";
1471 case 1:
1472 return "yes";
1473 }
1474 return "UNKNOWN";
1475}
1476
1477static const char *
1478lookup_opcode_name(ServerOpCodes code)
1479{
1480 u_int i;
1481
1482 for (i = 0; keywords[i].name != NULL; i++)
1483 if (keywords[i].opcode == code)
1484 return(keywords[i].name);
1485 return "UNKNOWN";
1486}
1487
1488static void
1489dump_cfg_int(ServerOpCodes code, int val)
1490{
1491 printf("%s %d\n", lookup_opcode_name(code), val);
1492}
1493
1494static void
1495dump_cfg_fmtint(ServerOpCodes code, int val)
1496{
1497 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
1498}
1499
1500static void
1501dump_cfg_string(ServerOpCodes code, const char *val)
1502{
1503 if (val == NULL)
1504 return;
1505 printf("%s %s\n", lookup_opcode_name(code), val);
1506}
1507
1508static void
1509dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
1510{
1511 u_int i;
1512
1513 for (i = 0; i < count; i++)
1514 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
1515}
1516
1517void
1518dump_config(ServerOptions *o)
1519{
1520 u_int i;
1521 int ret;
1522 struct addrinfo *ai;
1523 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
1524
1525 /* these are usually at the top of the config */
1526 for (i = 0; i < o->num_ports; i++)
1527 printf("port %d\n", o->ports[i]);
1528 dump_cfg_fmtint(sProtocol, o->protocol);
1529 dump_cfg_fmtint(sAddressFamily, o->address_family);
1530
1531 /* ListenAddress must be after Port */
1532 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
1533 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1534 sizeof(addr), port, sizeof(port),
1535 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1536 error("getnameinfo failed: %.100s",
1537 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
1538 strerror(errno));
1539 } else {
1540 if (ai->ai_family == AF_INET6)
1541 printf("listenaddress [%s]:%s\n", addr, port);
1542 else
1543 printf("listenaddress %s:%s\n", addr, port);
1544 }
1545 }
1546
1547 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10001548#ifdef USE_PAM
1549 dump_cfg_int(sUsePAM, o->use_pam);
1550#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001551 dump_cfg_int(sServerKeyBits, o->server_key_bits);
1552 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
1553 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
1554 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
1555 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11001556 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10001557 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
1558 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
1559
1560 /* formatted integer arguments */
1561 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
1562 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
1563 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
1564 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
1565 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
1566 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
1567 o->hostbased_uses_name_from_packet_only);
1568 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
1569 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10001570#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10001571 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
1572 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
1573 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10001574# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10001575 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10001576# endif
1577#endif
1578#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10001579 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
1580 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10001581#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001582 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
1583 dump_cfg_fmtint(sKbdInteractiveAuthentication,
1584 o->kbd_interactive_authentication);
1585 dump_cfg_fmtint(sChallengeResponseAuthentication,
1586 o->challenge_response_authentication);
1587 dump_cfg_fmtint(sPrintMotd, o->print_motd);
1588 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
1589 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
1590 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
1591 dump_cfg_fmtint(sStrictModes, o->strict_modes);
1592 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
1593 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
1594 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
1595 dump_cfg_fmtint(sUseLogin, o->use_login);
1596 dump_cfg_fmtint(sCompression, o->compression);
1597 dump_cfg_fmtint(sGatewayPorts, o->gateway_ports);
1598 dump_cfg_fmtint(sUseDNS, o->use_dns);
1599 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
1600 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
1601
1602 /* string arguments */
1603 dump_cfg_string(sPidFile, o->pid_file);
1604 dump_cfg_string(sXAuthLocation, o->xauth_location);
1605 dump_cfg_string(sCiphers, o->ciphers);
1606 dump_cfg_string(sMacs, o->macs);
1607 dump_cfg_string(sBanner, o->banner);
1608 dump_cfg_string(sAuthorizedKeysFile, o->authorized_keys_file);
1609 dump_cfg_string(sAuthorizedKeysFile2, o->authorized_keys_file2);
1610 dump_cfg_string(sForceCommand, o->adm_forced_command);
1611
1612 /* string arguments requiring a lookup */
1613 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
1614 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
1615
1616 /* string array arguments */
1617 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
1618 o->host_key_files);
1619 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
1620 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
1621 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
1622 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
1623 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
1624
1625 /* other arguments */
1626 for (i = 0; i < o->num_subsystems; i++)
1627 printf("subsystem %s %s\n", o->subsystem_name[i],
1628 o->subsystem_args[i]);
1629
1630 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
1631 o->max_startups_rate, o->max_startups);
1632
1633 for (i = 0; tunmode_desc[i].val != -1; i++)
1634 if (tunmode_desc[i].val == o->permit_tun) {
1635 s = tunmode_desc[i].text;
1636 break;
1637 }
1638 dump_cfg_string(sPermitTunnel, s);
1639
1640 printf("permitopen");
1641 channel_print_adm_permitted_opens();
1642 printf("\n");
1643}