blob: d38d0bfb17540af7dc3e1b16e55af77ca8590439 [file] [log] [blame]
Damien Millerd8cb1f12008-02-10 22:40:12 +11001/* $OpenBSD: servconf.c,v 1.176 2008/02/08 23:24:08 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>
Damien Millerbe43ebf2006-07-24 13:51:51 +100026
Damien Millerd7834352006-08-05 12:39:39 +100027#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000029#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100030#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031#include "servconf.h"
Damien Miller78928792000-04-12 20:17:38 +100032#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000033#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000034#include "misc.h"
35#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100036#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000037#include "kex.h"
38#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100039#include "match.h"
Damien Miller9b439df2006-07-24 14:04:00 +100040#include "channels.h"
Damien Miller565ca3f2006-08-19 00:23:15 +100041#include "groupaccess.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042
Ben Lindstrombba81212001-06-25 05:01:22 +000043static void add_listen_addr(ServerOptions *, char *, u_short);
44static void add_one_listen_addr(ServerOptions *, char *, u_short);
Damien Miller34132e52000-01-14 15:45:46 +110045
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000046/* Use of privilege separation or not */
47extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100048extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000049
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050/* Initializes the server options to their default values. */
51
Damien Miller4af51302000-04-16 11:18:38 +100052void
Damien Miller95def091999-11-25 00:26:21 +110053initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054{
Damien Miller95def091999-11-25 00:26:21 +110055 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110056
57 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100058 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110059
60 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110061 options->num_ports = 0;
62 options->ports_from_cmdline = 0;
63 options->listen_addrs = NULL;
Darren Tucker0f383232005-01-20 10:57:56 +110064 options->address_family = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110065 options->num_host_key_files = 0;
Damien Miller6f83b8e2000-05-02 09:23:45 +100066 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110067 options->server_key_bits = -1;
68 options->login_grace_time = -1;
69 options->key_regeneration_time = -1;
Ben Lindstromd8a90212001-02-15 03:08:27 +000070 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110071 options->ignore_rhosts = -1;
72 options->ignore_user_known_hosts = -1;
73 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +000074 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +110075 options->x11_forwarding = -1;
76 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +110077 options->x11_use_localhost = -1;
Damien Millerd3a18572000-06-07 19:55:44 +100078 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +110079 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +110080 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +110081 options->log_facility = SYSLOG_FACILITY_NOT_SET;
82 options->log_level = SYSLOG_LEVEL_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110083 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +000084 options->hostbased_authentication = -1;
85 options->hostbased_uses_name_from_packet_only = -1;
Damien Miller95def091999-11-25 00:26:21 +110086 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110087 options->pubkey_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +110088 options->kerberos_authentication = -1;
89 options->kerberos_or_local_passwd = -1;
90 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +110091 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +100092 options->gss_authentication=-1;
93 options->gss_cleanup_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +110094 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +110095 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +000096 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +110097 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +000098 options->permit_user_env = -1;
Damien Miller95def091999-11-25 00:26:21 +110099 options->use_login = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000100 options->compression = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100101 options->allow_tcp_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100102 options->num_allow_users = 0;
103 options->num_deny_users = 0;
104 options->num_allow_groups = 0;
105 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000106 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000107 options->macs = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000108 options->protocol = SSH_PROTO_UNKNOWN;
Damien Millere247cc42000-05-07 12:03:14 +1000109 options->gateway_ports = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000110 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000111 options->max_startups_begin = -1;
112 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000113 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000114 options->max_authtries = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000115 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000116 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000117 options->client_alive_interval = -1;
118 options->client_alive_count_max = -1;
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000119 options->authorized_keys_file = NULL;
120 options->authorized_keys_file2 = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +1000121 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100122 options->permit_tun = -1;
Damien Millera765cf42006-07-24 14:08:13 +1000123 options->num_permitted_opens = -1;
Damien Millere2754432006-07-24 14:06:47 +1000124 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100125 options->chroot_directory = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126}
127
Damien Miller4af51302000-04-16 11:18:38 +1000128void
Damien Miller95def091999-11-25 00:26:21 +1100129fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130{
Damien Miller726273e2001-11-12 11:40:11 +1100131 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000132 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000133 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100134
135 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100136 if (options->protocol == SSH_PROTO_UNKNOWN)
137 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
138 if (options->num_host_key_files == 0) {
139 /* fill default hostkeys for protocols */
140 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100141 options->host_key_files[options->num_host_key_files++] =
142 _PATH_HOST_KEY_FILE;
143 if (options->protocol & SSH_PROTO_2) {
144 options->host_key_files[options->num_host_key_files++] =
145 _PATH_HOST_RSA_KEY_FILE;
146 options->host_key_files[options->num_host_key_files++] =
147 _PATH_HOST_DSA_KEY_FILE;
148 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100149 }
Damien Miller34132e52000-01-14 15:45:46 +1100150 if (options->num_ports == 0)
151 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
152 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000153 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000154 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000155 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100156 if (options->server_key_bits == -1)
157 options->server_key_bits = 768;
158 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000159 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100160 if (options->key_regeneration_time == -1)
161 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000162 if (options->permit_root_login == PERMIT_NOT_SET)
163 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100164 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100165 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100166 if (options->ignore_user_known_hosts == -1)
167 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100168 if (options->print_motd == -1)
169 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000170 if (options->print_lastlog == -1)
171 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100172 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100173 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100174 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100175 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100176 if (options->x11_use_localhost == -1)
177 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000178 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000179 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100180 if (options->strict_modes == -1)
181 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100182 if (options->tcp_keep_alive == -1)
183 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100184 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100185 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100186 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000187 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100188 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100189 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000190 if (options->hostbased_authentication == -1)
191 options->hostbased_authentication = 0;
192 if (options->hostbased_uses_name_from_packet_only == -1)
193 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100194 if (options->rsa_authentication == -1)
195 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100196 if (options->pubkey_authentication == -1)
197 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100198 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000199 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100200 if (options->kerberos_or_local_passwd == -1)
201 options->kerberos_or_local_passwd = 1;
202 if (options->kerberos_ticket_cleanup == -1)
203 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100204 if (options->kerberos_get_afs_token == -1)
205 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000206 if (options->gss_authentication == -1)
207 options->gss_authentication = 0;
208 if (options->gss_cleanup_creds == -1)
209 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100210 if (options->password_authentication == -1)
211 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100212 if (options->kbd_interactive_authentication == -1)
213 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000214 if (options->challenge_response_authentication == -1)
215 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100216 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100217 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000218 if (options->permit_user_env == -1)
219 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100220 if (options->use_login == -1)
221 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000222 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000223 options->compression = COMP_DELAYED;
Damien Miller50a41ed2000-10-16 12:14:42 +1100224 if (options->allow_tcp_forwarding == -1)
225 options->allow_tcp_forwarding = 1;
Damien Millere247cc42000-05-07 12:03:14 +1000226 if (options->gateway_ports == -1)
227 options->gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000228 if (options->max_startups == -1)
229 options->max_startups = 10;
Damien Miller942da032000-08-18 13:59:06 +1000230 if (options->max_startups_rate == -1)
231 options->max_startups_rate = 100; /* 100% */
232 if (options->max_startups_begin == -1)
233 options->max_startups_begin = options->max_startups;
Darren Tucker89413db2004-05-24 10:36:23 +1000234 if (options->max_authtries == -1)
235 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000236 if (options->use_dns == -1)
237 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000238 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100239 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000240 if (options->client_alive_count_max == -1)
241 options->client_alive_count_max = 3;
Damien Miller75413ac2001-11-12 11:14:35 +1100242 if (options->authorized_keys_file2 == NULL) {
243 /* authorized_keys_file2 falls back to authorized_keys_file */
244 if (options->authorized_keys_file != NULL)
245 options->authorized_keys_file2 = options->authorized_keys_file;
246 else
247 options->authorized_keys_file2 = _PATH_SSH_USER_PERMITTED_KEYS2;
248 }
249 if (options->authorized_keys_file == NULL)
250 options->authorized_keys_file = _PATH_SSH_USER_PERMITTED_KEYS;
Damien Millerd27b9472005-12-13 19:29:02 +1100251 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100252 options->permit_tun = SSH_TUNMODE_NO;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000253
Ben Lindstromfb62a692002-06-06 19:47:11 +0000254 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000255 if (use_privsep == -1)
Ben Lindstromfb62a692002-06-06 19:47:11 +0000256 use_privsep = 1;
Damien Miller4903eb42002-06-21 16:20:44 +1000257
Tim Rice40017b02002-07-14 13:36:49 -0700258#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000259 if (use_privsep && options->compression == 1) {
260 error("This platform does not support both privilege "
261 "separation and compression");
262 error("Compression disabled");
263 options->compression = 0;
264 }
265#endif
266
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267}
268
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100270typedef enum {
271 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100272 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000273 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100274 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100275 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
276 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000277 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100278 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100279 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000280 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100281 sPasswordAuthentication, sKbdInteractiveAuthentication,
282 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000283 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100284 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller12c150e2003-12-17 16:31:10 +1100285 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000286 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Damien Miller50a41ed2000-10-16 12:14:42 +1100287 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000288 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000289 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
290 sMaxStartups, sMaxAuthTries,
Damien Miller3a961dc2003-06-03 10:25:48 +1000291 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100292 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000293 sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
Damien Millerd27b9472005-12-13 19:29:02 +1100294 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100295 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Ben Lindstromc7431342002-03-22 03:11:49 +0000296 sUsePrivilegeSeparation,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000297 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298} ServerOpCodes;
299
Darren Tucker45150472006-07-12 22:34:17 +1000300#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
301#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
302#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
303
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100305static struct {
306 const char *name;
307 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000308 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100309} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100310 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000311#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000312 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000313#else
Darren Tucker45150472006-07-12 22:34:17 +1000314 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000315#endif
Darren Tucker45150472006-07-12 22:34:17 +1000316 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100317 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000318 { "port", sPort, SSHCFG_GLOBAL },
319 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
320 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
321 { "pidfile", sPidFile, SSHCFG_GLOBAL },
322 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
323 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
324 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100325 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000326 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
327 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
328 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100329 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
330 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000331 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100332 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
333 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000334 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000335#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100336 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000337 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
338 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100339#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000340 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000341#else
Darren Tucker45150472006-07-12 22:34:17 +1000342 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100343#endif
344#else
Darren Tucker1629c072007-02-19 22:25:37 +1100345 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000346 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
347 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
348 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000349#endif
Darren Tucker45150472006-07-12 22:34:17 +1000350 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
351 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000352#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100353 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000354 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000355#else
Darren Tucker1629c072007-02-19 22:25:37 +1100356 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000357 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000358#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100359 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
360 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100361 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000362 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
363 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
364 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
365 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
366 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
367 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
368 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
369 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000370 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
371 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
372 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000373 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
374 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
375 { "permitemptypasswords", sEmptyPasswd, SSHCFG_GLOBAL },
376 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
377 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
378 { "compression", sCompression, SSHCFG_GLOBAL },
379 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
380 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
381 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
382 { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
383 { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
384 { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
385 { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
386 { "ciphers", sCiphers, SSHCFG_GLOBAL },
387 { "macs", sMacs, SSHCFG_GLOBAL },
388 { "protocol", sProtocol, SSHCFG_GLOBAL },
389 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
390 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
391 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
392 { "maxauthtries", sMaxAuthTries, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100393 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000394 { "usedns", sUseDNS, SSHCFG_GLOBAL },
395 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
396 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
397 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
398 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
399 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_GLOBAL },
400 { "authorizedkeysfile2", sAuthorizedKeysFile2, SSHCFG_GLOBAL },
401 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL },
402 { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
403 { "permittunnel", sPermitTunnel, SSHCFG_GLOBAL },
Damien Miller9b439df2006-07-24 14:04:00 +1000404 { "match", sMatch, SSHCFG_ALL },
405 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000406 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100407 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000408 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409};
410
Damien Miller5428f641999-11-25 11:54:57 +1100411/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000412 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100413 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414
Damien Miller4af51302000-04-16 11:18:38 +1000415static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100416parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000417 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000419 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420
Damien Miller95def091999-11-25 00:26:21 +1100421 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000422 if (strcasecmp(cp, keywords[i].name) == 0) {
423 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100424 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000425 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000427 error("%s: line %d: Bad configuration option: %s",
428 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100429 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430}
431
Ben Lindstrombba81212001-06-25 05:01:22 +0000432static void
Ben Lindstrom19066a12001-04-12 23:39:26 +0000433add_listen_addr(ServerOptions *options, char *addr, u_short port)
Damien Miller34132e52000-01-14 15:45:46 +1100434{
Damien Millereccb9de2005-06-17 12:59:34 +1000435 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100436
437 if (options->num_ports == 0)
438 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100439 if (options->address_family == -1)
440 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000441 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000442 for (i = 0; i < options->num_ports; i++)
443 add_one_listen_addr(options, addr, options->ports[i]);
444 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000445 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000446}
447
Ben Lindstrombba81212001-06-25 05:01:22 +0000448static void
Ben Lindstromc510af42001-04-07 17:25:48 +0000449add_one_listen_addr(ServerOptions *options, char *addr, u_short port)
450{
451 struct addrinfo hints, *ai, *aitop;
452 char strport[NI_MAXSERV];
453 int gaierr;
454
455 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100456 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000457 hints.ai_socktype = SOCK_STREAM;
458 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Ben Lindstrome1353632002-06-23 21:29:23 +0000459 snprintf(strport, sizeof strport, "%u", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000460 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
461 fatal("bad addr or host: %s (%s)",
462 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100463 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000464 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
465 ;
466 ai->ai_next = options->listen_addrs;
467 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100468}
469
Darren Tucker45150472006-07-12 22:34:17 +1000470/*
471 * The strategy for the Match blocks is that the config file is parsed twice.
472 *
473 * The first time is at startup. activep is initialized to 1 and the
474 * directives in the global context are processed and acted on. Hitting a
475 * Match directive unsets activep and the directives inside the block are
476 * checked for syntax only.
477 *
478 * The second time is after a connection has been established but before
479 * authentication. activep is initialized to 2 and global config directives
480 * are ignored since they have already been processed. If the criteria in a
481 * Match block is met, activep is set and the subsequent directives
482 * processed and actioned until EOF or another Match block unsets it. Any
483 * options set are copied into the main server config.
484 *
485 * Potential additions/improvements:
486 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
487 *
488 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
489 * Match Address 192.168.0.*
490 * Tag trusted
491 * Match Group wheel
492 * Tag trusted
493 * Match Tag trusted
494 * AllowTcpForwarding yes
495 * GatewayPorts clientspecified
496 * [...]
497 *
498 * - Add a PermittedChannelRequests directive
499 * Match Group shell
500 * PermittedChannelRequests session,forwarded-tcpip
501 */
502
503static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000504match_cfg_line_group(const char *grps, int line, const char *user)
505{
506 int result = 0;
507 u_int ngrps = 0;
508 char *arg, *p, *cp, *grplist[MAX_MATCH_GROUPS];
509 struct passwd *pw;
510
511 /*
512 * Even if we do not have a user yet, we still need to check for
513 * valid syntax.
514 */
515 arg = cp = xstrdup(grps);
516 while ((p = strsep(&cp, ",")) != NULL && *p != '\0') {
517 if (ngrps >= MAX_MATCH_GROUPS) {
518 error("line %d: too many groups in Match Group", line);
519 result = -1;
520 goto out;
521 }
522 grplist[ngrps++] = p;
523 }
524
525 if (user == NULL)
526 goto out;
527
528 if ((pw = getpwnam(user)) == NULL) {
529 debug("Can't match group at line %d because user %.100s does "
530 "not exist", line, user);
531 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
532 debug("Can't Match group because user %.100s not in any group "
533 "at line %d", user, line);
534 } else if (ga_match(grplist, ngrps) != 1) {
535 debug("user %.100s does not match group %.100s at line %d",
536 user, arg, line);
537 } else {
538 debug("user %.100s matched group %.100s at line %d", user,
539 arg, line);
540 result = 1;
541 }
542out:
543 ga_free();
544 xfree(arg);
545 return result;
546}
547
548static int
Darren Tucker45150472006-07-12 22:34:17 +1000549match_cfg_line(char **condition, int line, const char *user, const char *host,
550 const char *address)
551{
552 int result = 1;
553 char *arg, *attrib, *cp = *condition;
554 size_t len;
555
556 if (user == NULL)
557 debug3("checking syntax for 'Match %s'", cp);
558 else
559 debug3("checking match for '%s' user %s host %s addr %s", cp,
560 user ? user : "(null)", host ? host : "(null)",
561 address ? address : "(null)");
562
563 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
564 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
565 error("Missing Match criteria for %s", attrib);
566 return -1;
567 }
568 len = strlen(arg);
569 if (strcasecmp(attrib, "user") == 0) {
570 if (!user) {
571 result = 0;
572 continue;
573 }
574 if (match_pattern_list(user, arg, len, 0) != 1)
575 result = 0;
576 else
577 debug("user %.100s matched 'User %.100s' at "
578 "line %d", user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000579 } else if (strcasecmp(attrib, "group") == 0) {
580 switch (match_cfg_line_group(arg, line, user)) {
581 case -1:
582 return -1;
583 case 0:
584 result = 0;
585 }
Darren Tucker45150472006-07-12 22:34:17 +1000586 } else if (strcasecmp(attrib, "host") == 0) {
587 if (!host) {
588 result = 0;
589 continue;
590 }
591 if (match_hostname(host, arg, len) != 1)
592 result = 0;
593 else
594 debug("connection from %.100s matched 'Host "
595 "%.100s' at line %d", host, arg, line);
596 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tucker45150472006-07-12 22:34:17 +1000597 if (!address) {
598 result = 0;
599 continue;
600 }
601 if (match_hostname(address, arg, len) != 1)
602 result = 0;
603 else
604 debug("connection from %.100s matched 'Address "
605 "%.100s' at line %d", address, arg, line);
606 } else {
607 error("Unsupported Match attribute %s", attrib);
608 return -1;
609 }
610 }
611 if (user != NULL)
612 debug3("match %sfound", result ? "" : "not ");
613 *condition = cp;
614 return result;
615}
616
Damien Millere2754432006-07-24 14:06:47 +1000617#define WHITESPACE " \t\r\n"
618
Ben Lindstromade03f62001-12-06 18:22:17 +0000619int
620process_server_config_line(ServerOptions *options, char *line,
Darren Tucker45150472006-07-12 22:34:17 +1000621 const char *filename, int linenum, int *activep, const char *user,
622 const char *host, const char *address)
Ben Lindstromade03f62001-12-06 18:22:17 +0000623{
624 char *cp, **charptr, *arg, *p;
Darren Tucker45150472006-07-12 22:34:17 +1000625 int cmdline = 0, *intptr, value, n;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100626 SyslogFacility *log_facility_ptr;
627 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000628 ServerOpCodes opcode;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100629 u_short port;
Darren Tucker45150472006-07-12 22:34:17 +1000630 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000631 size_t len;
Ben Lindstromade03f62001-12-06 18:22:17 +0000632
633 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100634 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100635 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000636 /* Ignore leading whitespace */
637 if (*arg == '\0')
638 arg = strdelim(&cp);
639 if (!arg || !*arg || *arg == '#')
640 return 0;
641 intptr = NULL;
642 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000643 opcode = parse_token(arg, filename, linenum, &flags);
644
645 if (activep == NULL) { /* We are processing a command line directive */
646 cmdline = 1;
647 activep = &cmdline;
648 }
649 if (*activep && opcode != sMatch)
650 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
651 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
652 if (user == NULL) {
653 fatal("%s line %d: Directive '%s' is not allowed "
654 "within a Match block", filename, linenum, arg);
655 } else { /* this is a directive we have already processed */
656 while (arg)
657 arg = strdelim(&cp);
658 return 0;
659 }
660 }
661
Ben Lindstromade03f62001-12-06 18:22:17 +0000662 switch (opcode) {
663 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000664 case sUsePAM:
665 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000666 goto parse_flag;
667
668 /* Standard Options */
669 case sBadOption:
670 return -1;
671 case sPort:
672 /* ignore ports from configfile if cmdline specifies ports */
673 if (options->ports_from_cmdline)
674 return 0;
675 if (options->listen_addrs != NULL)
676 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100677 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000678 if (options->num_ports >= MAX_PORTS)
679 fatal("%s line %d: too many ports.",
680 filename, linenum);
681 arg = strdelim(&cp);
682 if (!arg || *arg == '\0')
683 fatal("%s line %d: missing port number.",
684 filename, linenum);
685 options->ports[options->num_ports++] = a2port(arg);
686 if (options->ports[options->num_ports-1] == 0)
687 fatal("%s line %d: Badly formatted port number.",
688 filename, linenum);
689 break;
690
691 case sServerKeyBits:
692 intptr = &options->server_key_bits;
693parse_int:
694 arg = strdelim(&cp);
695 if (!arg || *arg == '\0')
696 fatal("%s line %d: missing integer value.",
697 filename, linenum);
698 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000699 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000700 *intptr = value;
701 break;
702
703 case sLoginGraceTime:
704 intptr = &options->login_grace_time;
705parse_time:
706 arg = strdelim(&cp);
707 if (!arg || *arg == '\0')
708 fatal("%s line %d: missing time value.",
709 filename, linenum);
710 if ((value = convtime(arg)) == -1)
711 fatal("%s line %d: invalid time value.",
712 filename, linenum);
713 if (*intptr == -1)
714 *intptr = value;
715 break;
716
717 case sKeyRegenerationTime:
718 intptr = &options->key_regeneration_time;
719 goto parse_time;
720
721 case sListenAddress:
722 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100723 if (arg == NULL || *arg == '\0')
724 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000725 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000726 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
727 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
728 && strchr(p+1, ':') != NULL) {
729 add_listen_addr(options, arg, 0);
730 break;
731 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100732 p = hpdelim(&arg);
733 if (p == NULL)
734 fatal("%s line %d: bad address:port usage",
735 filename, linenum);
736 p = cleanhostname(p);
737 if (arg == NULL)
738 port = 0;
739 else if ((port = a2port(arg)) == 0)
740 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000741
Damien Millerf91ee4c2005-03-01 21:24:33 +1100742 add_listen_addr(options, p, port);
743
Ben Lindstromade03f62001-12-06 18:22:17 +0000744 break;
745
Darren Tucker0f383232005-01-20 10:57:56 +1100746 case sAddressFamily:
747 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000748 if (!arg || *arg == '\0')
749 fatal("%s line %d: missing address family.",
750 filename, linenum);
Darren Tucker0f383232005-01-20 10:57:56 +1100751 intptr = &options->address_family;
752 if (options->listen_addrs != NULL)
753 fatal("%s line %d: address family must be specified before "
754 "ListenAddress.", filename, linenum);
755 if (strcasecmp(arg, "inet") == 0)
756 value = AF_INET;
757 else if (strcasecmp(arg, "inet6") == 0)
758 value = AF_INET6;
759 else if (strcasecmp(arg, "any") == 0)
760 value = AF_UNSPEC;
761 else
762 fatal("%s line %d: unsupported address family \"%s\".",
763 filename, linenum, arg);
764 if (*intptr == -1)
765 *intptr = value;
766 break;
767
Ben Lindstromade03f62001-12-06 18:22:17 +0000768 case sHostKeyFile:
769 intptr = &options->num_host_key_files;
770 if (*intptr >= MAX_HOSTKEYS)
771 fatal("%s line %d: too many host keys specified (max %d).",
772 filename, linenum, MAX_HOSTKEYS);
773 charptr = &options->host_key_files[*intptr];
774parse_filename:
775 arg = strdelim(&cp);
776 if (!arg || *arg == '\0')
777 fatal("%s line %d: missing file name.",
778 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +1000779 if (*activep && *charptr == NULL) {
Ben Lindstromade03f62001-12-06 18:22:17 +0000780 *charptr = tilde_expand_filename(arg, getuid());
781 /* increase optional counter */
782 if (intptr != NULL)
783 *intptr = *intptr + 1;
784 }
785 break;
786
787 case sPidFile:
788 charptr = &options->pid_file;
789 goto parse_filename;
790
791 case sPermitRootLogin:
792 intptr = &options->permit_root_login;
793 arg = strdelim(&cp);
794 if (!arg || *arg == '\0')
795 fatal("%s line %d: missing yes/"
796 "without-password/forced-commands-only/no "
797 "argument.", filename, linenum);
798 value = 0; /* silence compiler */
799 if (strcmp(arg, "without-password") == 0)
800 value = PERMIT_NO_PASSWD;
801 else if (strcmp(arg, "forced-commands-only") == 0)
802 value = PERMIT_FORCED_ONLY;
803 else if (strcmp(arg, "yes") == 0)
804 value = PERMIT_YES;
805 else if (strcmp(arg, "no") == 0)
806 value = PERMIT_NO;
807 else
808 fatal("%s line %d: Bad yes/"
809 "without-password/forced-commands-only/no "
810 "argument: %s", filename, linenum, arg);
Darren Tucker15f94272008-01-01 20:36:56 +1100811 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000812 *intptr = value;
813 break;
814
815 case sIgnoreRhosts:
816 intptr = &options->ignore_rhosts;
817parse_flag:
818 arg = strdelim(&cp);
819 if (!arg || *arg == '\0')
820 fatal("%s line %d: missing yes/no argument.",
821 filename, linenum);
822 value = 0; /* silence compiler */
823 if (strcmp(arg, "yes") == 0)
824 value = 1;
825 else if (strcmp(arg, "no") == 0)
826 value = 0;
827 else
828 fatal("%s line %d: Bad yes/no argument: %s",
829 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +1000830 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000831 *intptr = value;
832 break;
833
834 case sIgnoreUserKnownHosts:
835 intptr = &options->ignore_user_known_hosts;
836 goto parse_flag;
837
Ben Lindstromade03f62001-12-06 18:22:17 +0000838 case sRhostsRSAAuthentication:
839 intptr = &options->rhosts_rsa_authentication;
840 goto parse_flag;
841
842 case sHostbasedAuthentication:
843 intptr = &options->hostbased_authentication;
844 goto parse_flag;
845
846 case sHostbasedUsesNameFromPacketOnly:
847 intptr = &options->hostbased_uses_name_from_packet_only;
848 goto parse_flag;
849
850 case sRSAAuthentication:
851 intptr = &options->rsa_authentication;
852 goto parse_flag;
853
854 case sPubkeyAuthentication:
855 intptr = &options->pubkey_authentication;
856 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000857
Ben Lindstromade03f62001-12-06 18:22:17 +0000858 case sKerberosAuthentication:
859 intptr = &options->kerberos_authentication;
860 goto parse_flag;
861
862 case sKerberosOrLocalPasswd:
863 intptr = &options->kerberos_or_local_passwd;
864 goto parse_flag;
865
866 case sKerberosTicketCleanup:
867 intptr = &options->kerberos_ticket_cleanup;
868 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000869
Darren Tucker22ef5082003-12-31 11:37:34 +1100870 case sKerberosGetAFSToken:
871 intptr = &options->kerberos_get_afs_token;
872 goto parse_flag;
873
Darren Tucker0efd1552003-08-26 11:49:55 +1000874 case sGssAuthentication:
875 intptr = &options->gss_authentication;
876 goto parse_flag;
877
878 case sGssCleanupCreds:
879 intptr = &options->gss_cleanup_creds;
880 goto parse_flag;
881
Ben Lindstromade03f62001-12-06 18:22:17 +0000882 case sPasswordAuthentication:
883 intptr = &options->password_authentication;
884 goto parse_flag;
885
886 case sKbdInteractiveAuthentication:
887 intptr = &options->kbd_interactive_authentication;
888 goto parse_flag;
889
890 case sChallengeResponseAuthentication:
891 intptr = &options->challenge_response_authentication;
892 goto parse_flag;
893
894 case sPrintMotd:
895 intptr = &options->print_motd;
896 goto parse_flag;
897
898 case sPrintLastLog:
899 intptr = &options->print_lastlog;
900 goto parse_flag;
901
902 case sX11Forwarding:
903 intptr = &options->x11_forwarding;
904 goto parse_flag;
905
906 case sX11DisplayOffset:
907 intptr = &options->x11_display_offset;
908 goto parse_int;
909
Damien Miller95c249f2002-02-05 12:11:34 +1100910 case sX11UseLocalhost:
911 intptr = &options->x11_use_localhost;
912 goto parse_flag;
913
Ben Lindstromade03f62001-12-06 18:22:17 +0000914 case sXAuthLocation:
915 charptr = &options->xauth_location;
916 goto parse_filename;
917
918 case sStrictModes:
919 intptr = &options->strict_modes;
920 goto parse_flag;
921
Damien Miller12c150e2003-12-17 16:31:10 +1100922 case sTCPKeepAlive:
923 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +0000924 goto parse_flag;
925
926 case sEmptyPasswd:
927 intptr = &options->permit_empty_passwd;
928 goto parse_flag;
929
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000930 case sPermitUserEnvironment:
931 intptr = &options->permit_user_env;
932 goto parse_flag;
933
Ben Lindstromade03f62001-12-06 18:22:17 +0000934 case sUseLogin:
935 intptr = &options->use_login;
936 goto parse_flag;
937
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000938 case sCompression:
939 intptr = &options->compression;
Damien Miller9786e6e2005-07-26 21:54:56 +1000940 arg = strdelim(&cp);
941 if (!arg || *arg == '\0')
942 fatal("%s line %d: missing yes/no/delayed "
943 "argument.", filename, linenum);
944 value = 0; /* silence compiler */
945 if (strcmp(arg, "delayed") == 0)
946 value = COMP_DELAYED;
947 else if (strcmp(arg, "yes") == 0)
948 value = COMP_ZLIB;
949 else if (strcmp(arg, "no") == 0)
950 value = COMP_NONE;
951 else
952 fatal("%s line %d: Bad yes/no/delayed "
953 "argument: %s", filename, linenum, arg);
954 if (*intptr == -1)
955 *intptr = value;
956 break;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000957
Ben Lindstromade03f62001-12-06 18:22:17 +0000958 case sGatewayPorts:
959 intptr = &options->gateway_ports;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100960 arg = strdelim(&cp);
961 if (!arg || *arg == '\0')
962 fatal("%s line %d: missing yes/no/clientspecified "
963 "argument.", filename, linenum);
964 value = 0; /* silence compiler */
965 if (strcmp(arg, "clientspecified") == 0)
966 value = 2;
967 else if (strcmp(arg, "yes") == 0)
968 value = 1;
969 else if (strcmp(arg, "no") == 0)
970 value = 0;
971 else
972 fatal("%s line %d: Bad yes/no/clientspecified "
973 "argument: %s", filename, linenum, arg);
Darren Tucker82347a82007-02-25 20:37:52 +1100974 if (*activep && *intptr == -1)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100975 *intptr = value;
976 break;
Ben Lindstromade03f62001-12-06 18:22:17 +0000977
Damien Miller3a961dc2003-06-03 10:25:48 +1000978 case sUseDNS:
979 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +0000980 goto parse_flag;
981
982 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100983 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +0000984 arg = strdelim(&cp);
985 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100986 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +0000987 fatal("%.200s line %d: unsupported log facility '%s'",
988 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100989 if (*log_facility_ptr == -1)
990 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +0000991 break;
992
993 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100994 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +0000995 arg = strdelim(&cp);
996 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100997 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +0000998 fatal("%.200s line %d: unsupported log level '%s'",
999 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001000 if (*log_level_ptr == -1)
1001 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001002 break;
1003
1004 case sAllowTcpForwarding:
1005 intptr = &options->allow_tcp_forwarding;
1006 goto parse_flag;
1007
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001008 case sUsePrivilegeSeparation:
1009 intptr = &use_privsep;
1010 goto parse_flag;
1011
Ben Lindstromade03f62001-12-06 18:22:17 +00001012 case sAllowUsers:
1013 while ((arg = strdelim(&cp)) && *arg != '\0') {
1014 if (options->num_allow_users >= MAX_ALLOW_USERS)
1015 fatal("%s line %d: too many allow users.",
1016 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001017 options->allow_users[options->num_allow_users++] =
1018 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001019 }
1020 break;
1021
1022 case sDenyUsers:
1023 while ((arg = strdelim(&cp)) && *arg != '\0') {
1024 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001025 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001026 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001027 options->deny_users[options->num_deny_users++] =
1028 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001029 }
1030 break;
1031
1032 case sAllowGroups:
1033 while ((arg = strdelim(&cp)) && *arg != '\0') {
1034 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1035 fatal("%s line %d: too many allow groups.",
1036 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001037 options->allow_groups[options->num_allow_groups++] =
1038 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001039 }
1040 break;
1041
1042 case sDenyGroups:
1043 while ((arg = strdelim(&cp)) && *arg != '\0') {
1044 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1045 fatal("%s line %d: too many deny groups.",
1046 filename, linenum);
1047 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
1048 }
1049 break;
1050
1051 case sCiphers:
1052 arg = strdelim(&cp);
1053 if (!arg || *arg == '\0')
1054 fatal("%s line %d: Missing argument.", filename, linenum);
1055 if (!ciphers_valid(arg))
1056 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1057 filename, linenum, arg ? arg : "<NONE>");
1058 if (options->ciphers == NULL)
1059 options->ciphers = xstrdup(arg);
1060 break;
1061
1062 case sMacs:
1063 arg = strdelim(&cp);
1064 if (!arg || *arg == '\0')
1065 fatal("%s line %d: Missing argument.", filename, linenum);
1066 if (!mac_valid(arg))
1067 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1068 filename, linenum, arg ? arg : "<NONE>");
1069 if (options->macs == NULL)
1070 options->macs = xstrdup(arg);
1071 break;
1072
1073 case sProtocol:
1074 intptr = &options->protocol;
1075 arg = strdelim(&cp);
1076 if (!arg || *arg == '\0')
1077 fatal("%s line %d: Missing argument.", filename, linenum);
1078 value = proto_spec(arg);
1079 if (value == SSH_PROTO_UNKNOWN)
1080 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001081 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001082 if (*intptr == SSH_PROTO_UNKNOWN)
1083 *intptr = value;
1084 break;
1085
1086 case sSubsystem:
1087 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1088 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001089 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001090 }
1091 arg = strdelim(&cp);
1092 if (!arg || *arg == '\0')
1093 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001094 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001095 if (!*activep) {
1096 arg = strdelim(&cp);
1097 break;
1098 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001099 for (i = 0; i < options->num_subsystems; i++)
1100 if (strcmp(arg, options->subsystem_name[i]) == 0)
1101 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001102 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001103 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1104 arg = strdelim(&cp);
1105 if (!arg || *arg == '\0')
1106 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001107 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001108 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001109
1110 /* Collect arguments (separate to executable) */
1111 p = xstrdup(arg);
1112 len = strlen(p) + 1;
1113 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1114 len += 1 + strlen(arg);
1115 p = xrealloc(p, 1, len);
1116 strlcat(p, " ", len);
1117 strlcat(p, arg, len);
1118 }
1119 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001120 options->num_subsystems++;
1121 break;
1122
1123 case sMaxStartups:
1124 arg = strdelim(&cp);
1125 if (!arg || *arg == '\0')
1126 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001127 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001128 if ((n = sscanf(arg, "%d:%d:%d",
1129 &options->max_startups_begin,
1130 &options->max_startups_rate,
1131 &options->max_startups)) == 3) {
1132 if (options->max_startups_begin >
1133 options->max_startups ||
1134 options->max_startups_rate > 100 ||
1135 options->max_startups_rate < 1)
1136 fatal("%s line %d: Illegal MaxStartups spec.",
1137 filename, linenum);
1138 } else if (n != 1)
1139 fatal("%s line %d: Illegal MaxStartups spec.",
1140 filename, linenum);
1141 else
1142 options->max_startups = options->max_startups_begin;
1143 break;
1144
Darren Tucker89413db2004-05-24 10:36:23 +10001145 case sMaxAuthTries:
1146 intptr = &options->max_authtries;
1147 goto parse_int;
1148
Ben Lindstromade03f62001-12-06 18:22:17 +00001149 case sBanner:
1150 charptr = &options->banner;
1151 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001152
Ben Lindstromade03f62001-12-06 18:22:17 +00001153 /*
1154 * These options can contain %X options expanded at
1155 * connect time, so that you can specify paths like:
1156 *
1157 * AuthorizedKeysFile /etc/ssh_keys/%u
1158 */
1159 case sAuthorizedKeysFile:
1160 case sAuthorizedKeysFile2:
Damien Miller4dec5d72006-08-05 11:38:40 +10001161 charptr = (opcode == sAuthorizedKeysFile) ?
Ben Lindstromade03f62001-12-06 18:22:17 +00001162 &options->authorized_keys_file :
1163 &options->authorized_keys_file2;
1164 goto parse_filename;
1165
1166 case sClientAliveInterval:
1167 intptr = &options->client_alive_interval;
1168 goto parse_time;
1169
1170 case sClientAliveCountMax:
1171 intptr = &options->client_alive_count_max;
1172 goto parse_int;
1173
Darren Tucker46bc0752004-05-02 22:11:30 +10001174 case sAcceptEnv:
1175 while ((arg = strdelim(&cp)) && *arg != '\0') {
1176 if (strchr(arg, '=') != NULL)
1177 fatal("%s line %d: Invalid environment name.",
1178 filename, linenum);
1179 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1180 fatal("%s line %d: too many allow env.",
1181 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001182 if (!*activep)
1183 break;
Darren Tucker46bc0752004-05-02 22:11:30 +10001184 options->accept_env[options->num_accept_env++] =
1185 xstrdup(arg);
1186 }
1187 break;
1188
Damien Millerd27b9472005-12-13 19:29:02 +11001189 case sPermitTunnel:
1190 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001191 arg = strdelim(&cp);
1192 if (!arg || *arg == '\0')
1193 fatal("%s line %d: Missing yes/point-to-point/"
1194 "ethernet/no argument.", filename, linenum);
1195 value = 0; /* silence compiler */
1196 if (strcasecmp(arg, "ethernet") == 0)
1197 value = SSH_TUNMODE_ETHERNET;
1198 else if (strcasecmp(arg, "point-to-point") == 0)
1199 value = SSH_TUNMODE_POINTOPOINT;
1200 else if (strcasecmp(arg, "yes") == 0)
1201 value = SSH_TUNMODE_YES;
1202 else if (strcasecmp(arg, "no") == 0)
1203 value = SSH_TUNMODE_NO;
1204 else
1205 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1206 "no argument: %s", filename, linenum, arg);
1207 if (*intptr == -1)
1208 *intptr = value;
1209 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001210
Darren Tucker45150472006-07-12 22:34:17 +10001211 case sMatch:
1212 if (cmdline)
1213 fatal("Match directive not supported as a command-line "
1214 "option");
1215 value = match_cfg_line(&cp, linenum, user, host, address);
1216 if (value < 0)
1217 fatal("%s line %d: Bad Match condition", filename,
1218 linenum);
1219 *activep = value;
1220 break;
1221
Damien Miller9b439df2006-07-24 14:04:00 +10001222 case sPermitOpen:
1223 arg = strdelim(&cp);
1224 if (!arg || *arg == '\0')
1225 fatal("%s line %d: missing PermitOpen specification",
1226 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001227 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001228 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001229 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001230 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001231 options->num_permitted_opens = 0;
1232 }
Damien Miller9b439df2006-07-24 14:04:00 +10001233 break;
1234 }
Damien Millera29b95e2007-01-05 16:28:36 +11001235 if (*activep && n == -1)
1236 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001237 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1238 p = hpdelim(&arg);
1239 if (p == NULL)
1240 fatal("%s line %d: missing host in PermitOpen",
1241 filename, linenum);
1242 p = cleanhostname(p);
1243 if (arg == NULL || (port = a2port(arg)) == 0)
1244 fatal("%s line %d: bad port number in "
1245 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001246 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001247 options->num_permitted_opens =
1248 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001249 }
Damien Miller9b439df2006-07-24 14:04:00 +10001250 break;
1251
Damien Millere2754432006-07-24 14:06:47 +10001252 case sForceCommand:
1253 if (cp == NULL)
1254 fatal("%.200s line %d: Missing argument.", filename,
1255 linenum);
1256 len = strspn(cp, WHITESPACE);
1257 if (*activep && options->adm_forced_command == NULL)
1258 options->adm_forced_command = xstrdup(cp + len);
1259 return 0;
1260
Damien Millerd8cb1f12008-02-10 22:40:12 +11001261 case sChrootDirectory:
1262 charptr = &options->chroot_directory;
1263 goto parse_filename;
1264
Ben Lindstromade03f62001-12-06 18:22:17 +00001265 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001266 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001267 filename, linenum, arg);
1268 while (arg)
1269 arg = strdelim(&cp);
1270 break;
1271
Damien Millerf9b3feb2003-05-16 11:38:32 +10001272 case sUnsupported:
1273 logit("%s line %d: Unsupported option %s",
1274 filename, linenum, arg);
1275 while (arg)
1276 arg = strdelim(&cp);
1277 break;
1278
Ben Lindstromade03f62001-12-06 18:22:17 +00001279 default:
1280 fatal("%s line %d: Missing handler for opcode %s (%d)",
1281 filename, linenum, arg, opcode);
1282 }
1283 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1284 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1285 filename, linenum, arg);
1286 return 0;
1287}
1288
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001289/* Reads the server configuration file. */
1290
Damien Miller4af51302000-04-16 11:18:38 +10001291void
Darren Tucker645ab752004-06-25 13:33:20 +10001292load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001293{
Darren Tucker645ab752004-06-25 13:33:20 +10001294 char line[1024], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001295 FILE *f;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001296
Darren Tucker645ab752004-06-25 13:33:20 +10001297 debug2("%s: filename %s", __func__, filename);
1298 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001299 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001300 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001301 }
Darren Tucker645ab752004-06-25 13:33:20 +10001302 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001303 while (fgets(line, sizeof(line), f)) {
Darren Tucker645ab752004-06-25 13:33:20 +10001304 /*
1305 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001306 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001307 * line numbers later for error messages
1308 */
1309 if ((cp = strchr(line, '#')) != NULL)
1310 memcpy(cp, "\n", 2);
1311 cp = line + strspn(line, " \t\r");
1312
1313 buffer_append(conf, cp, strlen(cp));
1314 }
1315 buffer_append(conf, "\0", 1);
1316 fclose(f);
1317 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1318}
1319
1320void
Darren Tucker45150472006-07-12 22:34:17 +10001321parse_server_match_config(ServerOptions *options, const char *user,
1322 const char *host, const char *address)
Darren Tucker645ab752004-06-25 13:33:20 +10001323{
Darren Tucker45150472006-07-12 22:34:17 +10001324 ServerOptions mo;
1325
1326 initialize_server_options(&mo);
1327 parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
Darren Tucker1629c072007-02-19 22:25:37 +11001328 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001329}
1330
Darren Tucker1629c072007-02-19 22:25:37 +11001331/* Helper macros */
1332#define M_CP_INTOPT(n) do {\
1333 if (src->n != -1) \
1334 dst->n = src->n; \
1335} while (0)
1336#define M_CP_STROPT(n) do {\
1337 if (src->n != NULL) { \
1338 if (dst->n != NULL) \
1339 xfree(dst->n); \
1340 dst->n = src->n; \
1341 } \
1342} while(0)
1343
1344/*
1345 * Copy any supported values that are set.
1346 *
1347 * If the preauth flag is set, we do not bother copying the the string or
1348 * array values that are not used pre-authentication, because any that we
1349 * do use must be explictly sent in mm_getpwnamallow().
1350 */
Darren Tucker45150472006-07-12 22:34:17 +10001351void
Darren Tucker1629c072007-02-19 22:25:37 +11001352copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001353{
Darren Tucker1629c072007-02-19 22:25:37 +11001354 M_CP_INTOPT(password_authentication);
1355 M_CP_INTOPT(gss_authentication);
1356 M_CP_INTOPT(rsa_authentication);
1357 M_CP_INTOPT(pubkey_authentication);
1358 M_CP_INTOPT(kerberos_authentication);
1359 M_CP_INTOPT(hostbased_authentication);
1360 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001361 M_CP_INTOPT(permit_root_login);
Darren Tucker1629c072007-02-19 22:25:37 +11001362
1363 M_CP_INTOPT(allow_tcp_forwarding);
1364 M_CP_INTOPT(gateway_ports);
1365 M_CP_INTOPT(x11_display_offset);
1366 M_CP_INTOPT(x11_forwarding);
1367 M_CP_INTOPT(x11_use_localhost);
1368
1369 M_CP_STROPT(banner);
1370 if (preauth)
1371 return;
1372 M_CP_STROPT(adm_forced_command);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001373 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001374}
1375
Darren Tucker1629c072007-02-19 22:25:37 +11001376#undef M_CP_INTOPT
1377#undef M_CP_STROPT
1378
Darren Tucker45150472006-07-12 22:34:17 +10001379void
1380parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1381 const char *user, const char *host, const char *address)
1382{
1383 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001384 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001385
1386 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1387
Darren Tucker9fbac712004-08-12 22:41:44 +10001388 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tucker45150472006-07-12 22:34:17 +10001389 active = user ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001390 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001391 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001392 if (process_server_config_line(options, cp, filename,
Darren Tucker45150472006-07-12 22:34:17 +10001393 linenum++, &active, user, host, address) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001394 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001395 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001396 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001397 if (bad_options > 0)
1398 fatal("%s: terminating, %d bad configuration options",
1399 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400}