blob: e6d49099b05a7d9b934ac67588c5880080df2ca1 [file] [log] [blame]
Damien Miller4f755cd2008-05-19 14:57:41 +10001/* $OpenBSD: servconf.c,v 1.178 2008/05/07 05:49:37 pyr 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 Miller4f755cd2008-05-19 14:57:41 +1000102 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100103 options->num_allow_users = 0;
104 options->num_deny_users = 0;
105 options->num_allow_groups = 0;
106 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000107 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000108 options->macs = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000109 options->protocol = SSH_PROTO_UNKNOWN;
Damien Millere247cc42000-05-07 12:03:14 +1000110 options->gateway_ports = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000111 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000112 options->max_startups_begin = -1;
113 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000114 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000115 options->max_authtries = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000116 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000117 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000118 options->client_alive_interval = -1;
119 options->client_alive_count_max = -1;
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000120 options->authorized_keys_file = NULL;
121 options->authorized_keys_file2 = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +1000122 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100123 options->permit_tun = -1;
Damien Millera765cf42006-07-24 14:08:13 +1000124 options->num_permitted_opens = -1;
Damien Millere2754432006-07-24 14:06:47 +1000125 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100126 options->chroot_directory = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127}
128
Damien Miller4af51302000-04-16 11:18:38 +1000129void
Damien Miller95def091999-11-25 00:26:21 +1100130fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131{
Damien Miller726273e2001-11-12 11:40:11 +1100132 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000133 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000134 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100135
136 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100137 if (options->protocol == SSH_PROTO_UNKNOWN)
138 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
139 if (options->num_host_key_files == 0) {
140 /* fill default hostkeys for protocols */
141 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100142 options->host_key_files[options->num_host_key_files++] =
143 _PATH_HOST_KEY_FILE;
144 if (options->protocol & SSH_PROTO_2) {
145 options->host_key_files[options->num_host_key_files++] =
146 _PATH_HOST_RSA_KEY_FILE;
147 options->host_key_files[options->num_host_key_files++] =
148 _PATH_HOST_DSA_KEY_FILE;
149 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100150 }
Damien Miller34132e52000-01-14 15:45:46 +1100151 if (options->num_ports == 0)
152 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
153 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000154 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000155 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000156 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100157 if (options->server_key_bits == -1)
158 options->server_key_bits = 768;
159 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000160 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100161 if (options->key_regeneration_time == -1)
162 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000163 if (options->permit_root_login == PERMIT_NOT_SET)
164 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100165 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100166 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100167 if (options->ignore_user_known_hosts == -1)
168 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100169 if (options->print_motd == -1)
170 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000171 if (options->print_lastlog == -1)
172 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100173 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100174 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100175 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100176 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100177 if (options->x11_use_localhost == -1)
178 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000179 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000180 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100181 if (options->strict_modes == -1)
182 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100183 if (options->tcp_keep_alive == -1)
184 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100185 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100186 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100187 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000188 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100189 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100190 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000191 if (options->hostbased_authentication == -1)
192 options->hostbased_authentication = 0;
193 if (options->hostbased_uses_name_from_packet_only == -1)
194 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100195 if (options->rsa_authentication == -1)
196 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100197 if (options->pubkey_authentication == -1)
198 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100199 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000200 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100201 if (options->kerberos_or_local_passwd == -1)
202 options->kerberos_or_local_passwd = 1;
203 if (options->kerberos_ticket_cleanup == -1)
204 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100205 if (options->kerberos_get_afs_token == -1)
206 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000207 if (options->gss_authentication == -1)
208 options->gss_authentication = 0;
209 if (options->gss_cleanup_creds == -1)
210 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100211 if (options->password_authentication == -1)
212 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100213 if (options->kbd_interactive_authentication == -1)
214 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000215 if (options->challenge_response_authentication == -1)
216 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100217 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100218 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000219 if (options->permit_user_env == -1)
220 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100221 if (options->use_login == -1)
222 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000223 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000224 options->compression = COMP_DELAYED;
Damien Miller50a41ed2000-10-16 12:14:42 +1100225 if (options->allow_tcp_forwarding == -1)
226 options->allow_tcp_forwarding = 1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000227 if (options->allow_agent_forwarding == -1)
228 options->allow_agent_forwarding = 1;
Damien Millere247cc42000-05-07 12:03:14 +1000229 if (options->gateway_ports == -1)
230 options->gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000231 if (options->max_startups == -1)
232 options->max_startups = 10;
Damien Miller942da032000-08-18 13:59:06 +1000233 if (options->max_startups_rate == -1)
234 options->max_startups_rate = 100; /* 100% */
235 if (options->max_startups_begin == -1)
236 options->max_startups_begin = options->max_startups;
Darren Tucker89413db2004-05-24 10:36:23 +1000237 if (options->max_authtries == -1)
238 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000239 if (options->use_dns == -1)
240 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000241 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100242 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000243 if (options->client_alive_count_max == -1)
244 options->client_alive_count_max = 3;
Damien Miller75413ac2001-11-12 11:14:35 +1100245 if (options->authorized_keys_file2 == NULL) {
246 /* authorized_keys_file2 falls back to authorized_keys_file */
247 if (options->authorized_keys_file != NULL)
248 options->authorized_keys_file2 = options->authorized_keys_file;
249 else
250 options->authorized_keys_file2 = _PATH_SSH_USER_PERMITTED_KEYS2;
251 }
252 if (options->authorized_keys_file == NULL)
253 options->authorized_keys_file = _PATH_SSH_USER_PERMITTED_KEYS;
Damien Millerd27b9472005-12-13 19:29:02 +1100254 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100255 options->permit_tun = SSH_TUNMODE_NO;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000256
Ben Lindstromfb62a692002-06-06 19:47:11 +0000257 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000258 if (use_privsep == -1)
Ben Lindstromfb62a692002-06-06 19:47:11 +0000259 use_privsep = 1;
Damien Miller4903eb42002-06-21 16:20:44 +1000260
Tim Rice40017b02002-07-14 13:36:49 -0700261#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000262 if (use_privsep && options->compression == 1) {
263 error("This platform does not support both privilege "
264 "separation and compression");
265 error("Compression disabled");
266 options->compression = 0;
267 }
268#endif
269
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270}
271
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100273typedef enum {
274 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100275 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000276 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100277 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100278 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
279 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000280 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100281 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100282 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000283 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100284 sPasswordAuthentication, sKbdInteractiveAuthentication,
285 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000286 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100287 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller12c150e2003-12-17 16:31:10 +1100288 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000289 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Damien Miller50a41ed2000-10-16 12:14:42 +1100290 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000291 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000292 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
293 sMaxStartups, sMaxAuthTries,
Damien Miller3a961dc2003-06-03 10:25:48 +1000294 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100295 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000296 sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
Damien Millerd27b9472005-12-13 19:29:02 +1100297 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100298 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Damien Miller4f755cd2008-05-19 14:57:41 +1000299 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000300 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301} ServerOpCodes;
302
Darren Tucker45150472006-07-12 22:34:17 +1000303#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
304#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
305#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
306
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100308static struct {
309 const char *name;
310 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000311 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100312} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100313 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000314#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000315 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000316#else
Darren Tucker45150472006-07-12 22:34:17 +1000317 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000318#endif
Darren Tucker45150472006-07-12 22:34:17 +1000319 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100320 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000321 { "port", sPort, SSHCFG_GLOBAL },
322 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
323 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
324 { "pidfile", sPidFile, SSHCFG_GLOBAL },
325 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
326 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
327 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100328 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000329 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
330 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
331 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100332 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
333 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000334 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100335 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
336 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000337 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000338#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100339 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000340 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
341 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100342#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000343 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000344#else
Darren Tucker45150472006-07-12 22:34:17 +1000345 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100346#endif
347#else
Darren Tucker1629c072007-02-19 22:25:37 +1100348 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000349 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
350 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
351 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000352#endif
Darren Tucker45150472006-07-12 22:34:17 +1000353 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
354 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000355#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100356 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000357 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000358#else
Darren Tucker1629c072007-02-19 22:25:37 +1100359 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000360 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000361#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100362 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
363 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100364 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000365 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
366 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
367 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
368 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
369 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
370 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
371 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
372 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000373 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
374 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
375 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000376 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
377 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
378 { "permitemptypasswords", sEmptyPasswd, SSHCFG_GLOBAL },
379 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
380 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
381 { "compression", sCompression, SSHCFG_GLOBAL },
382 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
383 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
384 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000385 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000386 { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
387 { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
388 { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
389 { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
390 { "ciphers", sCiphers, SSHCFG_GLOBAL },
391 { "macs", sMacs, SSHCFG_GLOBAL },
392 { "protocol", sProtocol, SSHCFG_GLOBAL },
393 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
394 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
395 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
396 { "maxauthtries", sMaxAuthTries, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100397 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000398 { "usedns", sUseDNS, SSHCFG_GLOBAL },
399 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
400 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
401 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
402 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
403 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_GLOBAL },
404 { "authorizedkeysfile2", sAuthorizedKeysFile2, SSHCFG_GLOBAL },
405 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL },
406 { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
407 { "permittunnel", sPermitTunnel, SSHCFG_GLOBAL },
Damien Miller9b439df2006-07-24 14:04:00 +1000408 { "match", sMatch, SSHCFG_ALL },
409 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000410 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100411 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000412 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413};
414
Damien Miller5428f641999-11-25 11:54:57 +1100415/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000416 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100417 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418
Damien Miller4af51302000-04-16 11:18:38 +1000419static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100420parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000421 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000423 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424
Damien Miller95def091999-11-25 00:26:21 +1100425 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000426 if (strcasecmp(cp, keywords[i].name) == 0) {
427 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100428 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000429 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000431 error("%s: line %d: Bad configuration option: %s",
432 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100433 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434}
435
Ben Lindstrombba81212001-06-25 05:01:22 +0000436static void
Ben Lindstrom19066a12001-04-12 23:39:26 +0000437add_listen_addr(ServerOptions *options, char *addr, u_short port)
Damien Miller34132e52000-01-14 15:45:46 +1100438{
Damien Millereccb9de2005-06-17 12:59:34 +1000439 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100440
441 if (options->num_ports == 0)
442 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100443 if (options->address_family == -1)
444 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000445 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000446 for (i = 0; i < options->num_ports; i++)
447 add_one_listen_addr(options, addr, options->ports[i]);
448 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000449 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000450}
451
Ben Lindstrombba81212001-06-25 05:01:22 +0000452static void
Ben Lindstromc510af42001-04-07 17:25:48 +0000453add_one_listen_addr(ServerOptions *options, char *addr, u_short port)
454{
455 struct addrinfo hints, *ai, *aitop;
456 char strport[NI_MAXSERV];
457 int gaierr;
458
459 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100460 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000461 hints.ai_socktype = SOCK_STREAM;
462 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Ben Lindstrome1353632002-06-23 21:29:23 +0000463 snprintf(strport, sizeof strport, "%u", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000464 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
465 fatal("bad addr or host: %s (%s)",
466 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100467 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000468 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
469 ;
470 ai->ai_next = options->listen_addrs;
471 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100472}
473
Darren Tucker45150472006-07-12 22:34:17 +1000474/*
475 * The strategy for the Match blocks is that the config file is parsed twice.
476 *
477 * The first time is at startup. activep is initialized to 1 and the
478 * directives in the global context are processed and acted on. Hitting a
479 * Match directive unsets activep and the directives inside the block are
480 * checked for syntax only.
481 *
482 * The second time is after a connection has been established but before
483 * authentication. activep is initialized to 2 and global config directives
484 * are ignored since they have already been processed. If the criteria in a
485 * Match block is met, activep is set and the subsequent directives
486 * processed and actioned until EOF or another Match block unsets it. Any
487 * options set are copied into the main server config.
488 *
489 * Potential additions/improvements:
490 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
491 *
492 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
493 * Match Address 192.168.0.*
494 * Tag trusted
495 * Match Group wheel
496 * Tag trusted
497 * Match Tag trusted
498 * AllowTcpForwarding yes
499 * GatewayPorts clientspecified
500 * [...]
501 *
502 * - Add a PermittedChannelRequests directive
503 * Match Group shell
504 * PermittedChannelRequests session,forwarded-tcpip
505 */
506
507static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000508match_cfg_line_group(const char *grps, int line, const char *user)
509{
510 int result = 0;
511 u_int ngrps = 0;
512 char *arg, *p, *cp, *grplist[MAX_MATCH_GROUPS];
513 struct passwd *pw;
514
515 /*
516 * Even if we do not have a user yet, we still need to check for
517 * valid syntax.
518 */
519 arg = cp = xstrdup(grps);
520 while ((p = strsep(&cp, ",")) != NULL && *p != '\0') {
521 if (ngrps >= MAX_MATCH_GROUPS) {
522 error("line %d: too many groups in Match Group", line);
523 result = -1;
524 goto out;
525 }
526 grplist[ngrps++] = p;
527 }
528
529 if (user == NULL)
530 goto out;
531
532 if ((pw = getpwnam(user)) == NULL) {
533 debug("Can't match group at line %d because user %.100s does "
534 "not exist", line, user);
535 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
536 debug("Can't Match group because user %.100s not in any group "
537 "at line %d", user, line);
538 } else if (ga_match(grplist, ngrps) != 1) {
539 debug("user %.100s does not match group %.100s at line %d",
540 user, arg, line);
541 } else {
542 debug("user %.100s matched group %.100s at line %d", user,
543 arg, line);
544 result = 1;
545 }
546out:
547 ga_free();
548 xfree(arg);
549 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 Tucker45150472006-07-12 22:34:17 +1000601 if (!address) {
602 result = 0;
603 continue;
604 }
605 if (match_hostname(address, arg, len) != 1)
606 result = 0;
607 else
608 debug("connection from %.100s matched 'Address "
609 "%.100s' at line %d", address, arg, line);
610 } else {
611 error("Unsupported Match attribute %s", attrib);
612 return -1;
613 }
614 }
615 if (user != NULL)
616 debug3("match %sfound", result ? "" : "not ");
617 *condition = cp;
618 return result;
619}
620
Damien Millere2754432006-07-24 14:06:47 +1000621#define WHITESPACE " \t\r\n"
622
Ben Lindstromade03f62001-12-06 18:22:17 +0000623int
624process_server_config_line(ServerOptions *options, char *line,
Darren Tucker45150472006-07-12 22:34:17 +1000625 const char *filename, int linenum, int *activep, const char *user,
626 const char *host, const char *address)
Ben Lindstromade03f62001-12-06 18:22:17 +0000627{
628 char *cp, **charptr, *arg, *p;
Darren Tucker45150472006-07-12 22:34:17 +1000629 int cmdline = 0, *intptr, value, n;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100630 SyslogFacility *log_facility_ptr;
631 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000632 ServerOpCodes opcode;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100633 u_short port;
Darren Tucker45150472006-07-12 22:34:17 +1000634 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000635 size_t len;
Ben Lindstromade03f62001-12-06 18:22:17 +0000636
637 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100638 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100639 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000640 /* Ignore leading whitespace */
641 if (*arg == '\0')
642 arg = strdelim(&cp);
643 if (!arg || !*arg || *arg == '#')
644 return 0;
645 intptr = NULL;
646 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000647 opcode = parse_token(arg, filename, linenum, &flags);
648
649 if (activep == NULL) { /* We are processing a command line directive */
650 cmdline = 1;
651 activep = &cmdline;
652 }
653 if (*activep && opcode != sMatch)
654 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
655 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
656 if (user == NULL) {
657 fatal("%s line %d: Directive '%s' is not allowed "
658 "within a Match block", filename, linenum, arg);
659 } else { /* this is a directive we have already processed */
660 while (arg)
661 arg = strdelim(&cp);
662 return 0;
663 }
664 }
665
Ben Lindstromade03f62001-12-06 18:22:17 +0000666 switch (opcode) {
667 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000668 case sUsePAM:
669 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000670 goto parse_flag;
671
672 /* Standard Options */
673 case sBadOption:
674 return -1;
675 case sPort:
676 /* ignore ports from configfile if cmdline specifies ports */
677 if (options->ports_from_cmdline)
678 return 0;
679 if (options->listen_addrs != NULL)
680 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100681 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000682 if (options->num_ports >= MAX_PORTS)
683 fatal("%s line %d: too many ports.",
684 filename, linenum);
685 arg = strdelim(&cp);
686 if (!arg || *arg == '\0')
687 fatal("%s line %d: missing port number.",
688 filename, linenum);
689 options->ports[options->num_ports++] = a2port(arg);
690 if (options->ports[options->num_ports-1] == 0)
691 fatal("%s line %d: Badly formatted port number.",
692 filename, linenum);
693 break;
694
695 case sServerKeyBits:
696 intptr = &options->server_key_bits;
697parse_int:
698 arg = strdelim(&cp);
699 if (!arg || *arg == '\0')
700 fatal("%s line %d: missing integer value.",
701 filename, linenum);
702 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000703 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000704 *intptr = value;
705 break;
706
707 case sLoginGraceTime:
708 intptr = &options->login_grace_time;
709parse_time:
710 arg = strdelim(&cp);
711 if (!arg || *arg == '\0')
712 fatal("%s line %d: missing time value.",
713 filename, linenum);
714 if ((value = convtime(arg)) == -1)
715 fatal("%s line %d: invalid time value.",
716 filename, linenum);
717 if (*intptr == -1)
718 *intptr = value;
719 break;
720
721 case sKeyRegenerationTime:
722 intptr = &options->key_regeneration_time;
723 goto parse_time;
724
725 case sListenAddress:
726 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100727 if (arg == NULL || *arg == '\0')
728 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000729 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000730 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
731 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
732 && strchr(p+1, ':') != NULL) {
733 add_listen_addr(options, arg, 0);
734 break;
735 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100736 p = hpdelim(&arg);
737 if (p == NULL)
738 fatal("%s line %d: bad address:port usage",
739 filename, linenum);
740 p = cleanhostname(p);
741 if (arg == NULL)
742 port = 0;
743 else if ((port = a2port(arg)) == 0)
744 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000745
Damien Millerf91ee4c2005-03-01 21:24:33 +1100746 add_listen_addr(options, p, port);
747
Ben Lindstromade03f62001-12-06 18:22:17 +0000748 break;
749
Darren Tucker0f383232005-01-20 10:57:56 +1100750 case sAddressFamily:
751 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000752 if (!arg || *arg == '\0')
753 fatal("%s line %d: missing address family.",
754 filename, linenum);
Darren Tucker0f383232005-01-20 10:57:56 +1100755 intptr = &options->address_family;
756 if (options->listen_addrs != NULL)
757 fatal("%s line %d: address family must be specified before "
758 "ListenAddress.", filename, linenum);
759 if (strcasecmp(arg, "inet") == 0)
760 value = AF_INET;
761 else if (strcasecmp(arg, "inet6") == 0)
762 value = AF_INET6;
763 else if (strcasecmp(arg, "any") == 0)
764 value = AF_UNSPEC;
765 else
766 fatal("%s line %d: unsupported address family \"%s\".",
767 filename, linenum, arg);
768 if (*intptr == -1)
769 *intptr = value;
770 break;
771
Ben Lindstromade03f62001-12-06 18:22:17 +0000772 case sHostKeyFile:
773 intptr = &options->num_host_key_files;
774 if (*intptr >= MAX_HOSTKEYS)
775 fatal("%s line %d: too many host keys specified (max %d).",
776 filename, linenum, MAX_HOSTKEYS);
777 charptr = &options->host_key_files[*intptr];
778parse_filename:
779 arg = strdelim(&cp);
780 if (!arg || *arg == '\0')
781 fatal("%s line %d: missing file name.",
782 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +1000783 if (*activep && *charptr == NULL) {
Ben Lindstromade03f62001-12-06 18:22:17 +0000784 *charptr = tilde_expand_filename(arg, getuid());
785 /* increase optional counter */
786 if (intptr != NULL)
787 *intptr = *intptr + 1;
788 }
789 break;
790
791 case sPidFile:
792 charptr = &options->pid_file;
793 goto parse_filename;
794
795 case sPermitRootLogin:
796 intptr = &options->permit_root_login;
797 arg = strdelim(&cp);
798 if (!arg || *arg == '\0')
799 fatal("%s line %d: missing yes/"
800 "without-password/forced-commands-only/no "
801 "argument.", filename, linenum);
802 value = 0; /* silence compiler */
803 if (strcmp(arg, "without-password") == 0)
804 value = PERMIT_NO_PASSWD;
805 else if (strcmp(arg, "forced-commands-only") == 0)
806 value = PERMIT_FORCED_ONLY;
807 else if (strcmp(arg, "yes") == 0)
808 value = PERMIT_YES;
809 else if (strcmp(arg, "no") == 0)
810 value = PERMIT_NO;
811 else
812 fatal("%s line %d: Bad yes/"
813 "without-password/forced-commands-only/no "
814 "argument: %s", filename, linenum, arg);
Darren Tucker15f94272008-01-01 20:36:56 +1100815 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000816 *intptr = value;
817 break;
818
819 case sIgnoreRhosts:
820 intptr = &options->ignore_rhosts;
821parse_flag:
822 arg = strdelim(&cp);
823 if (!arg || *arg == '\0')
824 fatal("%s line %d: missing yes/no argument.",
825 filename, linenum);
826 value = 0; /* silence compiler */
827 if (strcmp(arg, "yes") == 0)
828 value = 1;
829 else if (strcmp(arg, "no") == 0)
830 value = 0;
831 else
832 fatal("%s line %d: Bad yes/no argument: %s",
833 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +1000834 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000835 *intptr = value;
836 break;
837
838 case sIgnoreUserKnownHosts:
839 intptr = &options->ignore_user_known_hosts;
840 goto parse_flag;
841
Ben Lindstromade03f62001-12-06 18:22:17 +0000842 case sRhostsRSAAuthentication:
843 intptr = &options->rhosts_rsa_authentication;
844 goto parse_flag;
845
846 case sHostbasedAuthentication:
847 intptr = &options->hostbased_authentication;
848 goto parse_flag;
849
850 case sHostbasedUsesNameFromPacketOnly:
851 intptr = &options->hostbased_uses_name_from_packet_only;
852 goto parse_flag;
853
854 case sRSAAuthentication:
855 intptr = &options->rsa_authentication;
856 goto parse_flag;
857
858 case sPubkeyAuthentication:
859 intptr = &options->pubkey_authentication;
860 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000861
Ben Lindstromade03f62001-12-06 18:22:17 +0000862 case sKerberosAuthentication:
863 intptr = &options->kerberos_authentication;
864 goto parse_flag;
865
866 case sKerberosOrLocalPasswd:
867 intptr = &options->kerberos_or_local_passwd;
868 goto parse_flag;
869
870 case sKerberosTicketCleanup:
871 intptr = &options->kerberos_ticket_cleanup;
872 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000873
Darren Tucker22ef5082003-12-31 11:37:34 +1100874 case sKerberosGetAFSToken:
875 intptr = &options->kerberos_get_afs_token;
876 goto parse_flag;
877
Darren Tucker0efd1552003-08-26 11:49:55 +1000878 case sGssAuthentication:
879 intptr = &options->gss_authentication;
880 goto parse_flag;
881
882 case sGssCleanupCreds:
883 intptr = &options->gss_cleanup_creds;
884 goto parse_flag;
885
Ben Lindstromade03f62001-12-06 18:22:17 +0000886 case sPasswordAuthentication:
887 intptr = &options->password_authentication;
888 goto parse_flag;
889
890 case sKbdInteractiveAuthentication:
891 intptr = &options->kbd_interactive_authentication;
892 goto parse_flag;
893
894 case sChallengeResponseAuthentication:
895 intptr = &options->challenge_response_authentication;
896 goto parse_flag;
897
898 case sPrintMotd:
899 intptr = &options->print_motd;
900 goto parse_flag;
901
902 case sPrintLastLog:
903 intptr = &options->print_lastlog;
904 goto parse_flag;
905
906 case sX11Forwarding:
907 intptr = &options->x11_forwarding;
908 goto parse_flag;
909
910 case sX11DisplayOffset:
911 intptr = &options->x11_display_offset;
912 goto parse_int;
913
Damien Miller95c249f2002-02-05 12:11:34 +1100914 case sX11UseLocalhost:
915 intptr = &options->x11_use_localhost;
916 goto parse_flag;
917
Ben Lindstromade03f62001-12-06 18:22:17 +0000918 case sXAuthLocation:
919 charptr = &options->xauth_location;
920 goto parse_filename;
921
922 case sStrictModes:
923 intptr = &options->strict_modes;
924 goto parse_flag;
925
Damien Miller12c150e2003-12-17 16:31:10 +1100926 case sTCPKeepAlive:
927 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +0000928 goto parse_flag;
929
930 case sEmptyPasswd:
931 intptr = &options->permit_empty_passwd;
932 goto parse_flag;
933
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000934 case sPermitUserEnvironment:
935 intptr = &options->permit_user_env;
936 goto parse_flag;
937
Ben Lindstromade03f62001-12-06 18:22:17 +0000938 case sUseLogin:
939 intptr = &options->use_login;
940 goto parse_flag;
941
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000942 case sCompression:
943 intptr = &options->compression;
Damien Miller9786e6e2005-07-26 21:54:56 +1000944 arg = strdelim(&cp);
945 if (!arg || *arg == '\0')
946 fatal("%s line %d: missing yes/no/delayed "
947 "argument.", filename, linenum);
948 value = 0; /* silence compiler */
949 if (strcmp(arg, "delayed") == 0)
950 value = COMP_DELAYED;
951 else if (strcmp(arg, "yes") == 0)
952 value = COMP_ZLIB;
953 else if (strcmp(arg, "no") == 0)
954 value = COMP_NONE;
955 else
956 fatal("%s line %d: Bad yes/no/delayed "
957 "argument: %s", filename, linenum, arg);
958 if (*intptr == -1)
959 *intptr = value;
960 break;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000961
Ben Lindstromade03f62001-12-06 18:22:17 +0000962 case sGatewayPorts:
963 intptr = &options->gateway_ports;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100964 arg = strdelim(&cp);
965 if (!arg || *arg == '\0')
966 fatal("%s line %d: missing yes/no/clientspecified "
967 "argument.", filename, linenum);
968 value = 0; /* silence compiler */
969 if (strcmp(arg, "clientspecified") == 0)
970 value = 2;
971 else if (strcmp(arg, "yes") == 0)
972 value = 1;
973 else if (strcmp(arg, "no") == 0)
974 value = 0;
975 else
976 fatal("%s line %d: Bad yes/no/clientspecified "
977 "argument: %s", filename, linenum, arg);
Darren Tucker82347a82007-02-25 20:37:52 +1100978 if (*activep && *intptr == -1)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100979 *intptr = value;
980 break;
Ben Lindstromade03f62001-12-06 18:22:17 +0000981
Damien Miller3a961dc2003-06-03 10:25:48 +1000982 case sUseDNS:
983 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +0000984 goto parse_flag;
985
986 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100987 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +0000988 arg = strdelim(&cp);
989 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +1100990 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +0000991 fatal("%.200s line %d: unsupported log facility '%s'",
992 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100993 if (*log_facility_ptr == -1)
994 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +0000995 break;
996
997 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100998 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +0000999 arg = strdelim(&cp);
1000 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001001 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001002 fatal("%.200s line %d: unsupported log level '%s'",
1003 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001004 if (*log_level_ptr == -1)
1005 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001006 break;
1007
1008 case sAllowTcpForwarding:
1009 intptr = &options->allow_tcp_forwarding;
1010 goto parse_flag;
1011
Damien Miller4f755cd2008-05-19 14:57:41 +10001012 case sAllowAgentForwarding:
1013 intptr = &options->allow_agent_forwarding;
1014 goto parse_flag;
1015
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001016 case sUsePrivilegeSeparation:
1017 intptr = &use_privsep;
1018 goto parse_flag;
1019
Ben Lindstromade03f62001-12-06 18:22:17 +00001020 case sAllowUsers:
1021 while ((arg = strdelim(&cp)) && *arg != '\0') {
1022 if (options->num_allow_users >= MAX_ALLOW_USERS)
1023 fatal("%s line %d: too many allow users.",
1024 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001025 options->allow_users[options->num_allow_users++] =
1026 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001027 }
1028 break;
1029
1030 case sDenyUsers:
1031 while ((arg = strdelim(&cp)) && *arg != '\0') {
1032 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001033 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001034 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001035 options->deny_users[options->num_deny_users++] =
1036 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001037 }
1038 break;
1039
1040 case sAllowGroups:
1041 while ((arg = strdelim(&cp)) && *arg != '\0') {
1042 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1043 fatal("%s line %d: too many allow groups.",
1044 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001045 options->allow_groups[options->num_allow_groups++] =
1046 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001047 }
1048 break;
1049
1050 case sDenyGroups:
1051 while ((arg = strdelim(&cp)) && *arg != '\0') {
1052 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1053 fatal("%s line %d: too many deny groups.",
1054 filename, linenum);
1055 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
1056 }
1057 break;
1058
1059 case sCiphers:
1060 arg = strdelim(&cp);
1061 if (!arg || *arg == '\0')
1062 fatal("%s line %d: Missing argument.", filename, linenum);
1063 if (!ciphers_valid(arg))
1064 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1065 filename, linenum, arg ? arg : "<NONE>");
1066 if (options->ciphers == NULL)
1067 options->ciphers = xstrdup(arg);
1068 break;
1069
1070 case sMacs:
1071 arg = strdelim(&cp);
1072 if (!arg || *arg == '\0')
1073 fatal("%s line %d: Missing argument.", filename, linenum);
1074 if (!mac_valid(arg))
1075 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1076 filename, linenum, arg ? arg : "<NONE>");
1077 if (options->macs == NULL)
1078 options->macs = xstrdup(arg);
1079 break;
1080
1081 case sProtocol:
1082 intptr = &options->protocol;
1083 arg = strdelim(&cp);
1084 if (!arg || *arg == '\0')
1085 fatal("%s line %d: Missing argument.", filename, linenum);
1086 value = proto_spec(arg);
1087 if (value == SSH_PROTO_UNKNOWN)
1088 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001089 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001090 if (*intptr == SSH_PROTO_UNKNOWN)
1091 *intptr = value;
1092 break;
1093
1094 case sSubsystem:
1095 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1096 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001097 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001098 }
1099 arg = strdelim(&cp);
1100 if (!arg || *arg == '\0')
1101 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001102 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001103 if (!*activep) {
1104 arg = strdelim(&cp);
1105 break;
1106 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001107 for (i = 0; i < options->num_subsystems; i++)
1108 if (strcmp(arg, options->subsystem_name[i]) == 0)
1109 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001110 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001111 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1112 arg = strdelim(&cp);
1113 if (!arg || *arg == '\0')
1114 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001115 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001116 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001117
1118 /* Collect arguments (separate to executable) */
1119 p = xstrdup(arg);
1120 len = strlen(p) + 1;
1121 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1122 len += 1 + strlen(arg);
1123 p = xrealloc(p, 1, len);
1124 strlcat(p, " ", len);
1125 strlcat(p, arg, len);
1126 }
1127 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001128 options->num_subsystems++;
1129 break;
1130
1131 case sMaxStartups:
1132 arg = strdelim(&cp);
1133 if (!arg || *arg == '\0')
1134 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001135 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001136 if ((n = sscanf(arg, "%d:%d:%d",
1137 &options->max_startups_begin,
1138 &options->max_startups_rate,
1139 &options->max_startups)) == 3) {
1140 if (options->max_startups_begin >
1141 options->max_startups ||
1142 options->max_startups_rate > 100 ||
1143 options->max_startups_rate < 1)
1144 fatal("%s line %d: Illegal MaxStartups spec.",
1145 filename, linenum);
1146 } else if (n != 1)
1147 fatal("%s line %d: Illegal MaxStartups spec.",
1148 filename, linenum);
1149 else
1150 options->max_startups = options->max_startups_begin;
1151 break;
1152
Darren Tucker89413db2004-05-24 10:36:23 +10001153 case sMaxAuthTries:
1154 intptr = &options->max_authtries;
1155 goto parse_int;
1156
Ben Lindstromade03f62001-12-06 18:22:17 +00001157 case sBanner:
1158 charptr = &options->banner;
1159 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001160
Ben Lindstromade03f62001-12-06 18:22:17 +00001161 /*
1162 * These options can contain %X options expanded at
1163 * connect time, so that you can specify paths like:
1164 *
1165 * AuthorizedKeysFile /etc/ssh_keys/%u
1166 */
1167 case sAuthorizedKeysFile:
1168 case sAuthorizedKeysFile2:
Damien Miller4dec5d72006-08-05 11:38:40 +10001169 charptr = (opcode == sAuthorizedKeysFile) ?
Ben Lindstromade03f62001-12-06 18:22:17 +00001170 &options->authorized_keys_file :
1171 &options->authorized_keys_file2;
1172 goto parse_filename;
1173
1174 case sClientAliveInterval:
1175 intptr = &options->client_alive_interval;
1176 goto parse_time;
1177
1178 case sClientAliveCountMax:
1179 intptr = &options->client_alive_count_max;
1180 goto parse_int;
1181
Darren Tucker46bc0752004-05-02 22:11:30 +10001182 case sAcceptEnv:
1183 while ((arg = strdelim(&cp)) && *arg != '\0') {
1184 if (strchr(arg, '=') != NULL)
1185 fatal("%s line %d: Invalid environment name.",
1186 filename, linenum);
1187 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1188 fatal("%s line %d: too many allow env.",
1189 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001190 if (!*activep)
1191 break;
Darren Tucker46bc0752004-05-02 22:11:30 +10001192 options->accept_env[options->num_accept_env++] =
1193 xstrdup(arg);
1194 }
1195 break;
1196
Damien Millerd27b9472005-12-13 19:29:02 +11001197 case sPermitTunnel:
1198 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001199 arg = strdelim(&cp);
1200 if (!arg || *arg == '\0')
1201 fatal("%s line %d: Missing yes/point-to-point/"
1202 "ethernet/no argument.", filename, linenum);
1203 value = 0; /* silence compiler */
1204 if (strcasecmp(arg, "ethernet") == 0)
1205 value = SSH_TUNMODE_ETHERNET;
1206 else if (strcasecmp(arg, "point-to-point") == 0)
1207 value = SSH_TUNMODE_POINTOPOINT;
1208 else if (strcasecmp(arg, "yes") == 0)
1209 value = SSH_TUNMODE_YES;
1210 else if (strcasecmp(arg, "no") == 0)
1211 value = SSH_TUNMODE_NO;
1212 else
1213 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1214 "no argument: %s", filename, linenum, arg);
1215 if (*intptr == -1)
1216 *intptr = value;
1217 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001218
Darren Tucker45150472006-07-12 22:34:17 +10001219 case sMatch:
1220 if (cmdline)
1221 fatal("Match directive not supported as a command-line "
1222 "option");
1223 value = match_cfg_line(&cp, linenum, user, host, address);
1224 if (value < 0)
1225 fatal("%s line %d: Bad Match condition", filename,
1226 linenum);
1227 *activep = value;
1228 break;
1229
Damien Miller9b439df2006-07-24 14:04:00 +10001230 case sPermitOpen:
1231 arg = strdelim(&cp);
1232 if (!arg || *arg == '\0')
1233 fatal("%s line %d: missing PermitOpen specification",
1234 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001235 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001236 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001237 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001238 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001239 options->num_permitted_opens = 0;
1240 }
Damien Miller9b439df2006-07-24 14:04:00 +10001241 break;
1242 }
Damien Millera29b95e2007-01-05 16:28:36 +11001243 if (*activep && n == -1)
1244 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001245 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1246 p = hpdelim(&arg);
1247 if (p == NULL)
1248 fatal("%s line %d: missing host in PermitOpen",
1249 filename, linenum);
1250 p = cleanhostname(p);
1251 if (arg == NULL || (port = a2port(arg)) == 0)
1252 fatal("%s line %d: bad port number in "
1253 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001254 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001255 options->num_permitted_opens =
1256 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001257 }
Damien Miller9b439df2006-07-24 14:04:00 +10001258 break;
1259
Damien Millere2754432006-07-24 14:06:47 +10001260 case sForceCommand:
1261 if (cp == NULL)
1262 fatal("%.200s line %d: Missing argument.", filename,
1263 linenum);
1264 len = strspn(cp, WHITESPACE);
1265 if (*activep && options->adm_forced_command == NULL)
1266 options->adm_forced_command = xstrdup(cp + len);
1267 return 0;
1268
Damien Millerd8cb1f12008-02-10 22:40:12 +11001269 case sChrootDirectory:
1270 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001271
1272 arg = strdelim(&cp);
1273 if (!arg || *arg == '\0')
1274 fatal("%s line %d: missing file name.",
1275 filename, linenum);
1276 if (*activep && *charptr == NULL)
1277 *charptr = xstrdup(arg);
1278 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001279
Ben Lindstromade03f62001-12-06 18:22:17 +00001280 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001281 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001282 filename, linenum, arg);
1283 while (arg)
1284 arg = strdelim(&cp);
1285 break;
1286
Damien Millerf9b3feb2003-05-16 11:38:32 +10001287 case sUnsupported:
1288 logit("%s line %d: Unsupported option %s",
1289 filename, linenum, arg);
1290 while (arg)
1291 arg = strdelim(&cp);
1292 break;
1293
Ben Lindstromade03f62001-12-06 18:22:17 +00001294 default:
1295 fatal("%s line %d: Missing handler for opcode %s (%d)",
1296 filename, linenum, arg, opcode);
1297 }
1298 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1299 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1300 filename, linenum, arg);
1301 return 0;
1302}
1303
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001304/* Reads the server configuration file. */
1305
Damien Miller4af51302000-04-16 11:18:38 +10001306void
Darren Tucker645ab752004-06-25 13:33:20 +10001307load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001308{
Darren Tucker645ab752004-06-25 13:33:20 +10001309 char line[1024], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001310 FILE *f;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001311
Darren Tucker645ab752004-06-25 13:33:20 +10001312 debug2("%s: filename %s", __func__, filename);
1313 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001314 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001315 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001316 }
Darren Tucker645ab752004-06-25 13:33:20 +10001317 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001318 while (fgets(line, sizeof(line), f)) {
Darren Tucker645ab752004-06-25 13:33:20 +10001319 /*
1320 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001321 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001322 * line numbers later for error messages
1323 */
1324 if ((cp = strchr(line, '#')) != NULL)
1325 memcpy(cp, "\n", 2);
1326 cp = line + strspn(line, " \t\r");
1327
1328 buffer_append(conf, cp, strlen(cp));
1329 }
1330 buffer_append(conf, "\0", 1);
1331 fclose(f);
1332 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1333}
1334
1335void
Darren Tucker45150472006-07-12 22:34:17 +10001336parse_server_match_config(ServerOptions *options, const char *user,
1337 const char *host, const char *address)
Darren Tucker645ab752004-06-25 13:33:20 +10001338{
Darren Tucker45150472006-07-12 22:34:17 +10001339 ServerOptions mo;
1340
1341 initialize_server_options(&mo);
1342 parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
Darren Tucker1629c072007-02-19 22:25:37 +11001343 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001344}
1345
Darren Tucker1629c072007-02-19 22:25:37 +11001346/* Helper macros */
1347#define M_CP_INTOPT(n) do {\
1348 if (src->n != -1) \
1349 dst->n = src->n; \
1350} while (0)
1351#define M_CP_STROPT(n) do {\
1352 if (src->n != NULL) { \
1353 if (dst->n != NULL) \
1354 xfree(dst->n); \
1355 dst->n = src->n; \
1356 } \
1357} while(0)
1358
1359/*
1360 * Copy any supported values that are set.
1361 *
1362 * If the preauth flag is set, we do not bother copying the the string or
1363 * array values that are not used pre-authentication, because any that we
1364 * do use must be explictly sent in mm_getpwnamallow().
1365 */
Darren Tucker45150472006-07-12 22:34:17 +10001366void
Darren Tucker1629c072007-02-19 22:25:37 +11001367copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001368{
Darren Tucker1629c072007-02-19 22:25:37 +11001369 M_CP_INTOPT(password_authentication);
1370 M_CP_INTOPT(gss_authentication);
1371 M_CP_INTOPT(rsa_authentication);
1372 M_CP_INTOPT(pubkey_authentication);
1373 M_CP_INTOPT(kerberos_authentication);
1374 M_CP_INTOPT(hostbased_authentication);
1375 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001376 M_CP_INTOPT(permit_root_login);
Darren Tucker1629c072007-02-19 22:25:37 +11001377
1378 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001379 M_CP_INTOPT(allow_agent_forwarding);
Darren Tucker1629c072007-02-19 22:25:37 +11001380 M_CP_INTOPT(gateway_ports);
1381 M_CP_INTOPT(x11_display_offset);
1382 M_CP_INTOPT(x11_forwarding);
1383 M_CP_INTOPT(x11_use_localhost);
1384
1385 M_CP_STROPT(banner);
1386 if (preauth)
1387 return;
1388 M_CP_STROPT(adm_forced_command);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001389 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001390}
1391
Darren Tucker1629c072007-02-19 22:25:37 +11001392#undef M_CP_INTOPT
1393#undef M_CP_STROPT
1394
Darren Tucker45150472006-07-12 22:34:17 +10001395void
1396parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1397 const char *user, const char *host, const char *address)
1398{
1399 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001400 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001401
1402 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1403
Darren Tucker9fbac712004-08-12 22:41:44 +10001404 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tucker45150472006-07-12 22:34:17 +10001405 active = user ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001406 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001407 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001408 if (process_server_config_line(options, cp, filename,
Darren Tucker45150472006-07-12 22:34:17 +10001409 linenum++, &active, user, host, address) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001410 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001411 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001412 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001413 if (bad_options > 0)
1414 fatal("%s: terminating, %d bad configuration options",
1415 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001416}