blob: 8e69ea5ce68447883a0b5c83630d673cdebceb2e [file] [log] [blame]
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001
Damien Miller09d3e122012-10-31 08:58:58 +11002/* $OpenBSD: servconf.c,v 1.231 2012/10/30 21:29:54 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003/*
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
Damien Millere3b60b52006-07-10 21:08:03 +100016#include <sys/types.h>
17#include <sys/socket.h>
18
Damien Miller0dac6fb2010-11-20 15:19:38 +110019#include <netinet/in.h>
20#include <netinet/in_systm.h>
21#include <netinet/ip.h>
22
Damien Millerb8fe89c2006-07-24 14:51:00 +100023#include <netdb.h>
Damien Miller565ca3f2006-08-19 00:23:15 +100024#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100025#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100026#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100027#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100028#include <signal.h>
Damien Millere6b3b612006-07-24 14:01:23 +100029#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100030#include <stdarg.h>
Darren Tuckere7140f22008-06-10 23:01:51 +100031#include <errno.h>
Damien Millerbe43ebf2006-07-24 13:51:51 +100032
Damien Millerb84886b2008-05-19 15:05:07 +100033#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100034#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100037#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038#include "servconf.h"
Damien Miller78928792000-04-12 20:17:38 +100039#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "misc.h"
42#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100043#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000044#include "kex.h"
45#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100046#include "match.h"
Damien Miller9b439df2006-07-24 14:04:00 +100047#include "channels.h"
Damien Miller565ca3f2006-08-19 00:23:15 +100048#include "groupaccess.h"
Darren Tuckerfbcf8272012-05-19 19:37:01 +100049#include "canohost.h"
50#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051
Damien Miller3dc71ad2009-01-28 16:31:22 +110052static void add_listen_addr(ServerOptions *, char *, int);
53static void add_one_listen_addr(ServerOptions *, char *, int);
Damien Miller34132e52000-01-14 15:45:46 +110054
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000055/* Use of privilege separation or not */
56extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100057extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059/* Initializes the server options to their default values. */
60
Damien Miller4af51302000-04-16 11:18:38 +100061void
Damien Miller95def091999-11-25 00:26:21 +110062initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063{
Damien Miller95def091999-11-25 00:26:21 +110064 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110065
66 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100067 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110068
69 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110070 options->num_ports = 0;
71 options->ports_from_cmdline = 0;
72 options->listen_addrs = NULL;
Darren Tucker0f383232005-01-20 10:57:56 +110073 options->address_family = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110074 options->num_host_key_files = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110075 options->num_host_cert_files = 0;
Damien Miller6f83b8e2000-05-02 09:23:45 +100076 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110077 options->server_key_bits = -1;
78 options->login_grace_time = -1;
79 options->key_regeneration_time = -1;
Ben Lindstromd8a90212001-02-15 03:08:27 +000080 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110081 options->ignore_rhosts = -1;
82 options->ignore_user_known_hosts = -1;
83 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +000084 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +110085 options->x11_forwarding = -1;
86 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +110087 options->x11_use_localhost = -1;
Damien Millerd3a18572000-06-07 19:55:44 +100088 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +110089 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +110090 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +110091 options->log_facility = SYSLOG_FACILITY_NOT_SET;
92 options->log_level = SYSLOG_LEVEL_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110093 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +000094 options->hostbased_authentication = -1;
95 options->hostbased_uses_name_from_packet_only = -1;
Damien Miller95def091999-11-25 00:26:21 +110096 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110097 options->pubkey_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +110098 options->kerberos_authentication = -1;
99 options->kerberos_or_local_passwd = -1;
100 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100101 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000102 options->gss_authentication=-1;
103 options->gss_cleanup_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100104 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100105 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000106 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100107 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000108 options->permit_user_env = -1;
Damien Miller95def091999-11-25 00:26:21 +1100109 options->use_login = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000110 options->compression = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100111 options->allow_tcp_forwarding = -1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000112 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100113 options->num_allow_users = 0;
114 options->num_deny_users = 0;
115 options->num_allow_groups = 0;
116 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000117 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000118 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000119 options->kex_algorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000120 options->protocol = SSH_PROTO_UNKNOWN;
Damien Millere247cc42000-05-07 12:03:14 +1000121 options->gateway_ports = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000122 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000123 options->max_startups_begin = -1;
124 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000125 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000126 options->max_authtries = -1;
Damien Miller7207f642008-05-19 15:34:50 +1000127 options->max_sessions = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000128 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000129 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000130 options->client_alive_interval = -1;
131 options->client_alive_count_max = -1;
Damien Millerd8478b62011-05-29 21:39:36 +1000132 options->num_authkeys_files = 0;
Darren Tucker46bc0752004-05-02 22:11:30 +1000133 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100134 options->permit_tun = -1;
Damien Millera765cf42006-07-24 14:08:13 +1000135 options->num_permitted_opens = -1;
Damien Millere2754432006-07-24 14:06:47 +1000136 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100137 options->chroot_directory = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +1100138 options->authorized_keys_command = NULL;
139 options->authorized_keys_command_user = NULL;
Damien Miller01ed2272008-11-05 16:20:46 +1100140 options->zero_knowledge_password_authentication = -1;
Damien Miller1aed65e2010-03-04 21:53:35 +1100141 options->revoked_keys_file = NULL;
142 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000143 options->authorized_principals_file = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100144 options->ip_qos_interactive = -1;
145 options->ip_qos_bulk = -1;
Damien Miller23528812012-04-22 11:24:43 +1000146 options->version_addendum = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147}
148
Damien Miller4af51302000-04-16 11:18:38 +1000149void
Damien Miller95def091999-11-25 00:26:21 +1100150fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151{
Damien Miller726273e2001-11-12 11:40:11 +1100152 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000153 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000154 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100155
156 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100157 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +1100158 options->protocol = SSH_PROTO_2;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100159 if (options->num_host_key_files == 0) {
160 /* fill default hostkeys for protocols */
161 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100162 options->host_key_files[options->num_host_key_files++] =
163 _PATH_HOST_KEY_FILE;
164 if (options->protocol & SSH_PROTO_2) {
165 options->host_key_files[options->num_host_key_files++] =
166 _PATH_HOST_RSA_KEY_FILE;
167 options->host_key_files[options->num_host_key_files++] =
168 _PATH_HOST_DSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100169#ifdef OPENSSL_HAS_ECC
Damien Millere13cadf2010-09-10 11:15:33 +1000170 options->host_key_files[options->num_host_key_files++] =
171 _PATH_HOST_ECDSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100172#endif
Damien Miller7fc23732002-01-22 23:19:11 +1100173 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100174 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100175 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100176 if (options->num_ports == 0)
177 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
178 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000179 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000180 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000181 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100182 if (options->server_key_bits == -1)
Darren Tucker7499b0c2008-07-02 22:35:43 +1000183 options->server_key_bits = 1024;
Damien Miller95def091999-11-25 00:26:21 +1100184 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000185 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100186 if (options->key_regeneration_time == -1)
187 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000188 if (options->permit_root_login == PERMIT_NOT_SET)
189 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100190 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100191 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100192 if (options->ignore_user_known_hosts == -1)
193 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100194 if (options->print_motd == -1)
195 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000196 if (options->print_lastlog == -1)
197 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100198 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100199 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100200 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100201 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100202 if (options->x11_use_localhost == -1)
203 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000204 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000205 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100206 if (options->strict_modes == -1)
207 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100208 if (options->tcp_keep_alive == -1)
209 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100210 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100211 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100212 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000213 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100214 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100215 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000216 if (options->hostbased_authentication == -1)
217 options->hostbased_authentication = 0;
218 if (options->hostbased_uses_name_from_packet_only == -1)
219 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100220 if (options->rsa_authentication == -1)
221 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100222 if (options->pubkey_authentication == -1)
223 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100224 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000225 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100226 if (options->kerberos_or_local_passwd == -1)
227 options->kerberos_or_local_passwd = 1;
228 if (options->kerberos_ticket_cleanup == -1)
229 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100230 if (options->kerberos_get_afs_token == -1)
231 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000232 if (options->gss_authentication == -1)
233 options->gss_authentication = 0;
234 if (options->gss_cleanup_creds == -1)
235 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100236 if (options->password_authentication == -1)
237 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100238 if (options->kbd_interactive_authentication == -1)
239 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000240 if (options->challenge_response_authentication == -1)
241 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100242 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100243 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000244 if (options->permit_user_env == -1)
245 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100246 if (options->use_login == -1)
247 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000248 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000249 options->compression = COMP_DELAYED;
Damien Miller50a41ed2000-10-16 12:14:42 +1100250 if (options->allow_tcp_forwarding == -1)
251 options->allow_tcp_forwarding = 1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000252 if (options->allow_agent_forwarding == -1)
253 options->allow_agent_forwarding = 1;
Damien Millere247cc42000-05-07 12:03:14 +1000254 if (options->gateway_ports == -1)
255 options->gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000256 if (options->max_startups == -1)
257 options->max_startups = 10;
Damien Miller942da032000-08-18 13:59:06 +1000258 if (options->max_startups_rate == -1)
259 options->max_startups_rate = 100; /* 100% */
260 if (options->max_startups_begin == -1)
261 options->max_startups_begin = options->max_startups;
Darren Tucker89413db2004-05-24 10:36:23 +1000262 if (options->max_authtries == -1)
263 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000264 if (options->max_sessions == -1)
265 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000266 if (options->use_dns == -1)
267 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000268 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100269 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000270 if (options->client_alive_count_max == -1)
271 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000272 if (options->num_authkeys_files == 0) {
273 options->authorized_keys_files[options->num_authkeys_files++] =
274 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS);
275 options->authorized_keys_files[options->num_authkeys_files++] =
276 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS2);
277 }
Damien Millerd27b9472005-12-13 19:29:02 +1100278 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100279 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller01ed2272008-11-05 16:20:46 +1100280 if (options->zero_knowledge_password_authentication == -1)
281 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100282 if (options->ip_qos_interactive == -1)
283 options->ip_qos_interactive = IPTOS_LOWDELAY;
284 if (options->ip_qos_bulk == -1)
285 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller23528812012-04-22 11:24:43 +1000286 if (options->version_addendum == NULL)
287 options->version_addendum = xstrdup("");
Ben Lindstromfb62a692002-06-06 19:47:11 +0000288 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000289 if (use_privsep == -1)
Damien Miller5a5c2b92012-07-31 12:21:34 +1000290 use_privsep = PRIVSEP_NOSANDBOX;
Damien Miller4903eb42002-06-21 16:20:44 +1000291
Tim Rice40017b02002-07-14 13:36:49 -0700292#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000293 if (use_privsep && options->compression == 1) {
294 error("This platform does not support both privilege "
295 "separation and compression");
296 error("Compression disabled");
297 options->compression = 0;
298 }
299#endif
300
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301}
302
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100304typedef enum {
305 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100306 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000307 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100308 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100309 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
310 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000311 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100312 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100313 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000314 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100315 sPasswordAuthentication, sKbdInteractiveAuthentication,
316 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000317 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100318 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller12c150e2003-12-17 16:31:10 +1100319 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000320 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Damien Miller50a41ed2000-10-16 12:14:42 +1100321 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000322 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000323 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
Damien Miller7207f642008-05-19 15:34:50 +1000324 sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000325 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100326 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Damien Millerec2eaa32011-05-20 18:57:14 +1000327 sClientAliveCountMax, sAuthorizedKeysFile,
Damien Millerd27b9472005-12-13 19:29:02 +1100328 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100329 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100330 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller0a80ca12010-02-27 07:55:05 +1100331 sZeroKnowledgePasswordAuthentication, sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000332 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Miller23528812012-04-22 11:24:43 +1000333 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Miller09d3e122012-10-31 08:58:58 +1100334 sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000335 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336} ServerOpCodes;
337
Darren Tucker45150472006-07-12 22:34:17 +1000338#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
339#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
340#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
341
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100343static struct {
344 const char *name;
345 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000346 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100347} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100348 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000349#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000350 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000351#else
Darren Tucker45150472006-07-12 22:34:17 +1000352 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000353#endif
Darren Tucker45150472006-07-12 22:34:17 +1000354 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100355 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000356 { "port", sPort, SSHCFG_GLOBAL },
357 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
358 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
359 { "pidfile", sPidFile, SSHCFG_GLOBAL },
360 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
361 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
362 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100363 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000364 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
365 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
366 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100367 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
368 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000369 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100370 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
371 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000372 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000373#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100374 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000375 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
376 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100377#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000378 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000379#else
Darren Tucker45150472006-07-12 22:34:17 +1000380 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100381#endif
382#else
Darren Tucker1629c072007-02-19 22:25:37 +1100383 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000384 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
385 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
386 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000387#endif
Darren Tucker45150472006-07-12 22:34:17 +1000388 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
389 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000390#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100391 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000392 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000393#else
Darren Tucker1629c072007-02-19 22:25:37 +1100394 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000395 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000396#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100397 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
398 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100399 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000400 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
Damien Miller01ed2272008-11-05 16:20:46 +1100401#ifdef JPAKE
402 { "zeroknowledgepasswordauthentication", sZeroKnowledgePasswordAuthentication, SSHCFG_ALL },
403#else
404 { "zeroknowledgepasswordauthentication", sUnsupported, SSHCFG_ALL },
405#endif
Darren Tucker45150472006-07-12 22:34:17 +1000406 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
407 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
408 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
409 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
410 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
411 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
412 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000413 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
414 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
415 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000416 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
417 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100418 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000419 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
420 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
421 { "compression", sCompression, SSHCFG_GLOBAL },
422 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
423 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
424 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000425 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Damien Millerc24da772012-06-20 21:53:58 +1000426 { "allowusers", sAllowUsers, SSHCFG_ALL },
427 { "denyusers", sDenyUsers, SSHCFG_ALL },
428 { "allowgroups", sAllowGroups, SSHCFG_ALL },
429 { "denygroups", sDenyGroups, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000430 { "ciphers", sCiphers, SSHCFG_GLOBAL },
431 { "macs", sMacs, SSHCFG_GLOBAL },
432 { "protocol", sProtocol, SSHCFG_GLOBAL },
433 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
434 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
435 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000436 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000437 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100438 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000439 { "usedns", sUseDNS, SSHCFG_GLOBAL },
440 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
441 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
442 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
443 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000444 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000445 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000446 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Damien Millerc24da772012-06-20 21:53:58 +1000447 { "acceptenv", sAcceptEnv, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000448 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000449 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000450 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000451 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100452 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100453 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100454 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
455 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000456 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000457 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100458 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller09d3e122012-10-31 08:58:58 +1100459 { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
460 { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000461 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000462 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463};
464
Darren Tuckere7140f22008-06-10 23:01:51 +1000465static struct {
466 int val;
467 char *text;
468} tunmode_desc[] = {
469 { SSH_TUNMODE_NO, "no" },
470 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
471 { SSH_TUNMODE_ETHERNET, "ethernet" },
472 { SSH_TUNMODE_YES, "yes" },
473 { -1, NULL }
474};
475
Damien Miller5428f641999-11-25 11:54:57 +1100476/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000477 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100478 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479
Damien Miller4af51302000-04-16 11:18:38 +1000480static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100481parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000482 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000484 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485
Damien Miller95def091999-11-25 00:26:21 +1100486 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000487 if (strcasecmp(cp, keywords[i].name) == 0) {
488 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100489 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000490 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000492 error("%s: line %d: Bad configuration option: %s",
493 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100494 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495}
496
Darren Tucker88b6fb22010-01-13 22:44:29 +1100497char *
498derelativise_path(const char *path)
499{
Damien Miller44451d02010-03-26 10:40:04 +1100500 char *expanded, *ret, cwd[MAXPATHLEN];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100501
502 expanded = tilde_expand_filename(path, getuid());
503 if (*expanded == '/')
504 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100505 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100506 fatal("%s: getcwd: %s", __func__, strerror(errno));
507 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100508 xfree(expanded);
509 return ret;
510}
511
Ben Lindstrombba81212001-06-25 05:01:22 +0000512static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100513add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100514{
Damien Millereccb9de2005-06-17 12:59:34 +1000515 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100516
517 if (options->num_ports == 0)
518 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100519 if (options->address_family == -1)
520 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000521 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000522 for (i = 0; i < options->num_ports; i++)
523 add_one_listen_addr(options, addr, options->ports[i]);
524 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000525 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000526}
527
Ben Lindstrombba81212001-06-25 05:01:22 +0000528static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100529add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000530{
531 struct addrinfo hints, *ai, *aitop;
532 char strport[NI_MAXSERV];
533 int gaierr;
534
535 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100536 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000537 hints.ai_socktype = SOCK_STREAM;
538 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100539 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000540 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
541 fatal("bad addr or host: %s (%s)",
542 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100543 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000544 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
545 ;
546 ai->ai_next = options->listen_addrs;
547 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100548}
549
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000550struct connection_info *
551get_connection_info(int populate, int use_dns)
552{
553 static struct connection_info ci;
554
555 if (!populate)
556 return &ci;
557 ci.host = get_canonical_hostname(use_dns);
558 ci.address = get_remote_ipaddr();
559 ci.laddress = get_local_ipaddr(packet_get_connection_in());
560 ci.lport = get_local_port();
561 return &ci;
562}
563
Darren Tucker45150472006-07-12 22:34:17 +1000564/*
565 * The strategy for the Match blocks is that the config file is parsed twice.
566 *
567 * The first time is at startup. activep is initialized to 1 and the
568 * directives in the global context are processed and acted on. Hitting a
569 * Match directive unsets activep and the directives inside the block are
570 * checked for syntax only.
571 *
572 * The second time is after a connection has been established but before
573 * authentication. activep is initialized to 2 and global config directives
574 * are ignored since they have already been processed. If the criteria in a
575 * Match block is met, activep is set and the subsequent directives
576 * processed and actioned until EOF or another Match block unsets it. Any
577 * options set are copied into the main server config.
578 *
579 * Potential additions/improvements:
580 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
581 *
582 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
583 * Match Address 192.168.0.*
584 * Tag trusted
585 * Match Group wheel
586 * Tag trusted
587 * Match Tag trusted
588 * AllowTcpForwarding yes
589 * GatewayPorts clientspecified
590 * [...]
591 *
592 * - Add a PermittedChannelRequests directive
593 * Match Group shell
594 * PermittedChannelRequests session,forwarded-tcpip
595 */
596
597static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000598match_cfg_line_group(const char *grps, int line, const char *user)
599{
600 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000601 struct passwd *pw;
602
Damien Miller565ca3f2006-08-19 00:23:15 +1000603 if (user == NULL)
604 goto out;
605
606 if ((pw = getpwnam(user)) == NULL) {
607 debug("Can't match group at line %d because user %.100s does "
608 "not exist", line, user);
609 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
610 debug("Can't Match group because user %.100s not in any group "
611 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000612 } else if (ga_match_pattern_list(grps) != 1) {
613 debug("user %.100s does not match group list %.100s at line %d",
614 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000615 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000616 debug("user %.100s matched group list %.100s at line %d", user,
617 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000618 result = 1;
619 }
620out:
621 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000622 return result;
623}
624
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000625/*
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000626 * All of the attributes on a single Match line are ANDed together, so we need
627 * to check every * attribute and set the result to zero if any attribute does
628 * not match.
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000629 */
Damien Miller565ca3f2006-08-19 00:23:15 +1000630static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000631match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +1000632{
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000633 int result = 1, port;
Darren Tucker45150472006-07-12 22:34:17 +1000634 char *arg, *attrib, *cp = *condition;
635 size_t len;
636
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000637 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000638 debug3("checking syntax for 'Match %s'", cp);
639 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000640 debug3("checking match for '%s' user %s host %s addr %s "
641 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
642 ci->host ? ci->host : "(null)",
643 ci->address ? ci->address : "(null)",
644 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +1000645
646 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
647 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
648 error("Missing Match criteria for %s", attrib);
649 return -1;
650 }
651 len = strlen(arg);
652 if (strcasecmp(attrib, "user") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000653 if (ci == NULL || ci->user == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000654 result = 0;
655 continue;
656 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000657 if (match_pattern_list(ci->user, arg, len, 0) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000658 result = 0;
659 else
660 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000661 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000662 } else if (strcasecmp(attrib, "group") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000663 if (ci == NULL || ci->user == NULL) {
664 result = 0;
665 continue;
666 }
667 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +1000668 case -1:
669 return -1;
670 case 0:
671 result = 0;
672 }
Darren Tucker45150472006-07-12 22:34:17 +1000673 } else if (strcasecmp(attrib, "host") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000674 if (ci == NULL || ci->host == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000675 result = 0;
676 continue;
677 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000678 if (match_hostname(ci->host, arg, len) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000679 result = 0;
680 else
681 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000682 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +1000683 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000684 if (ci == NULL || ci->address == NULL) {
685 result = 0;
686 continue;
687 }
688 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000689 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000690 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000691 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000692 break;
693 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000694 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000695 result = 0;
696 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000697 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000698 return -1;
699 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000700 } else if (strcasecmp(attrib, "localaddress") == 0){
701 if (ci == NULL || ci->laddress == NULL) {
702 result = 0;
703 continue;
704 }
705 switch (addr_match_list(ci->laddress, arg)) {
706 case 1:
707 debug("connection from %.100s matched "
708 "'LocalAddress %.100s' at line %d",
709 ci->laddress, arg, line);
710 break;
711 case 0:
712 case -1:
713 result = 0;
714 break;
715 case -2:
716 return -1;
717 }
718 } else if (strcasecmp(attrib, "localport") == 0) {
719 if ((port = a2port(arg)) == -1) {
720 error("Invalid LocalPort '%s' on Match line",
721 arg);
722 return -1;
723 }
724 if (ci == NULL || ci->lport == 0) {
725 result = 0;
726 continue;
727 }
728 /* TODO support port lists */
729 if (port == ci->lport)
730 debug("connection from %.100s matched "
731 "'LocalPort %d' at line %d",
732 ci->laddress, port, line);
733 else
734 result = 0;
Darren Tucker45150472006-07-12 22:34:17 +1000735 } else {
736 error("Unsupported Match attribute %s", attrib);
737 return -1;
738 }
739 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000740 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000741 debug3("match %sfound", result ? "" : "not ");
742 *condition = cp;
743 return result;
744}
745
Damien Millere2754432006-07-24 14:06:47 +1000746#define WHITESPACE " \t\r\n"
747
Damien Miller33322122011-06-20 14:43:11 +1000748/* Multistate option parsing */
749struct multistate {
750 char *key;
751 int value;
752};
753static const struct multistate multistate_addressfamily[] = {
754 { "inet", AF_INET },
755 { "inet6", AF_INET6 },
756 { "any", AF_UNSPEC },
757 { NULL, -1 }
758};
759static const struct multistate multistate_permitrootlogin[] = {
760 { "without-password", PERMIT_NO_PASSWD },
761 { "forced-commands-only", PERMIT_FORCED_ONLY },
762 { "yes", PERMIT_YES },
763 { "no", PERMIT_NO },
764 { NULL, -1 }
765};
766static const struct multistate multistate_compression[] = {
767 { "delayed", COMP_DELAYED },
768 { "yes", COMP_ZLIB },
769 { "no", COMP_NONE },
770 { NULL, -1 }
771};
772static const struct multistate multistate_gatewayports[] = {
773 { "clientspecified", 2 },
774 { "yes", 1 },
775 { "no", 0 },
776 { NULL, -1 }
777};
Damien Miller69ff1df2011-06-23 08:30:03 +1000778static const struct multistate multistate_privsep[] = {
Damien Miller5a5c2b92012-07-31 12:21:34 +1000779 { "yes", PRIVSEP_NOSANDBOX },
780 { "sandbox", PRIVSEP_ON },
781 { "nosandbox", PRIVSEP_NOSANDBOX },
Damien Miller69ff1df2011-06-23 08:30:03 +1000782 { "no", PRIVSEP_OFF },
783 { NULL, -1 }
784};
Damien Miller33322122011-06-20 14:43:11 +1000785
Ben Lindstromade03f62001-12-06 18:22:17 +0000786int
787process_server_config_line(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000788 const char *filename, int linenum, int *activep,
789 struct connection_info *connectinfo)
Ben Lindstromade03f62001-12-06 18:22:17 +0000790{
791 char *cp, **charptr, *arg, *p;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100792 int cmdline = 0, *intptr, value, value2, n;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100793 SyslogFacility *log_facility_ptr;
794 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000795 ServerOpCodes opcode;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100796 int port;
Darren Tucker45150472006-07-12 22:34:17 +1000797 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000798 size_t len;
Damien Miller33322122011-06-20 14:43:11 +1000799 const struct multistate *multistate_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000800
801 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100802 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100803 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000804 /* Ignore leading whitespace */
805 if (*arg == '\0')
806 arg = strdelim(&cp);
807 if (!arg || !*arg || *arg == '#')
808 return 0;
809 intptr = NULL;
810 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000811 opcode = parse_token(arg, filename, linenum, &flags);
812
813 if (activep == NULL) { /* We are processing a command line directive */
814 cmdline = 1;
815 activep = &cmdline;
816 }
817 if (*activep && opcode != sMatch)
818 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
819 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000820 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000821 fatal("%s line %d: Directive '%s' is not allowed "
822 "within a Match block", filename, linenum, arg);
823 } else { /* this is a directive we have already processed */
824 while (arg)
825 arg = strdelim(&cp);
826 return 0;
827 }
828 }
829
Ben Lindstromade03f62001-12-06 18:22:17 +0000830 switch (opcode) {
831 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000832 case sUsePAM:
833 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000834 goto parse_flag;
835
836 /* Standard Options */
837 case sBadOption:
838 return -1;
839 case sPort:
840 /* ignore ports from configfile if cmdline specifies ports */
841 if (options->ports_from_cmdline)
842 return 0;
843 if (options->listen_addrs != NULL)
844 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100845 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000846 if (options->num_ports >= MAX_PORTS)
847 fatal("%s line %d: too many ports.",
848 filename, linenum);
849 arg = strdelim(&cp);
850 if (!arg || *arg == '\0')
851 fatal("%s line %d: missing port number.",
852 filename, linenum);
853 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100854 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000855 fatal("%s line %d: Badly formatted port number.",
856 filename, linenum);
857 break;
858
859 case sServerKeyBits:
860 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +1000861 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +0000862 arg = strdelim(&cp);
863 if (!arg || *arg == '\0')
864 fatal("%s line %d: missing integer value.",
865 filename, linenum);
866 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000867 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000868 *intptr = value;
869 break;
870
871 case sLoginGraceTime:
872 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +1000873 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +0000874 arg = strdelim(&cp);
875 if (!arg || *arg == '\0')
876 fatal("%s line %d: missing time value.",
877 filename, linenum);
878 if ((value = convtime(arg)) == -1)
879 fatal("%s line %d: invalid time value.",
880 filename, linenum);
881 if (*intptr == -1)
882 *intptr = value;
883 break;
884
885 case sKeyRegenerationTime:
886 intptr = &options->key_regeneration_time;
887 goto parse_time;
888
889 case sListenAddress:
890 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100891 if (arg == NULL || *arg == '\0')
892 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000893 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000894 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
895 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
896 && strchr(p+1, ':') != NULL) {
897 add_listen_addr(options, arg, 0);
898 break;
899 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100900 p = hpdelim(&arg);
901 if (p == NULL)
902 fatal("%s line %d: bad address:port usage",
903 filename, linenum);
904 p = cleanhostname(p);
905 if (arg == NULL)
906 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100907 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100908 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000909
Damien Millerf91ee4c2005-03-01 21:24:33 +1100910 add_listen_addr(options, p, port);
911
Ben Lindstromade03f62001-12-06 18:22:17 +0000912 break;
913
Darren Tucker0f383232005-01-20 10:57:56 +1100914 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +1000915 intptr = &options->address_family;
916 multistate_ptr = multistate_addressfamily;
917 if (options->listen_addrs != NULL)
918 fatal("%s line %d: address family must be specified "
919 "before ListenAddress.", filename, linenum);
920 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +1100921 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000922 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +1000923 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +1000924 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +1000925 value = -1;
926 for (i = 0; multistate_ptr[i].key != NULL; i++) {
927 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
928 value = multistate_ptr[i].value;
929 break;
930 }
931 }
932 if (value == -1)
933 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +1100934 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +1000935 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +1100936 *intptr = value;
937 break;
938
Ben Lindstromade03f62001-12-06 18:22:17 +0000939 case sHostKeyFile:
940 intptr = &options->num_host_key_files;
941 if (*intptr >= MAX_HOSTKEYS)
942 fatal("%s line %d: too many host keys specified (max %d).",
943 filename, linenum, MAX_HOSTKEYS);
944 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +1000945 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +0000946 arg = strdelim(&cp);
947 if (!arg || *arg == '\0')
948 fatal("%s line %d: missing file name.",
949 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +1000950 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +1100951 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000952 /* increase optional counter */
953 if (intptr != NULL)
954 *intptr = *intptr + 1;
955 }
956 break;
957
Damien Miller0a80ca12010-02-27 07:55:05 +1100958 case sHostCertificate:
959 intptr = &options->num_host_cert_files;
960 if (*intptr >= MAX_HOSTKEYS)
961 fatal("%s line %d: too many host certificates "
962 "specified (max %d).", filename, linenum,
963 MAX_HOSTCERTS);
964 charptr = &options->host_cert_files[*intptr];
965 goto parse_filename;
966 break;
967
Ben Lindstromade03f62001-12-06 18:22:17 +0000968 case sPidFile:
969 charptr = &options->pid_file;
970 goto parse_filename;
971
972 case sPermitRootLogin:
973 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +1000974 multistate_ptr = multistate_permitrootlogin;
975 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +0000976
977 case sIgnoreRhosts:
978 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +1000979 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +0000980 arg = strdelim(&cp);
981 if (!arg || *arg == '\0')
982 fatal("%s line %d: missing yes/no argument.",
983 filename, linenum);
984 value = 0; /* silence compiler */
985 if (strcmp(arg, "yes") == 0)
986 value = 1;
987 else if (strcmp(arg, "no") == 0)
988 value = 0;
989 else
990 fatal("%s line %d: Bad yes/no argument: %s",
991 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +1000992 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000993 *intptr = value;
994 break;
995
996 case sIgnoreUserKnownHosts:
997 intptr = &options->ignore_user_known_hosts;
998 goto parse_flag;
999
Ben Lindstromade03f62001-12-06 18:22:17 +00001000 case sRhostsRSAAuthentication:
1001 intptr = &options->rhosts_rsa_authentication;
1002 goto parse_flag;
1003
1004 case sHostbasedAuthentication:
1005 intptr = &options->hostbased_authentication;
1006 goto parse_flag;
1007
1008 case sHostbasedUsesNameFromPacketOnly:
1009 intptr = &options->hostbased_uses_name_from_packet_only;
1010 goto parse_flag;
1011
1012 case sRSAAuthentication:
1013 intptr = &options->rsa_authentication;
1014 goto parse_flag;
1015
1016 case sPubkeyAuthentication:
1017 intptr = &options->pubkey_authentication;
1018 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001019
Ben Lindstromade03f62001-12-06 18:22:17 +00001020 case sKerberosAuthentication:
1021 intptr = &options->kerberos_authentication;
1022 goto parse_flag;
1023
1024 case sKerberosOrLocalPasswd:
1025 intptr = &options->kerberos_or_local_passwd;
1026 goto parse_flag;
1027
1028 case sKerberosTicketCleanup:
1029 intptr = &options->kerberos_ticket_cleanup;
1030 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001031
Darren Tucker22ef5082003-12-31 11:37:34 +11001032 case sKerberosGetAFSToken:
1033 intptr = &options->kerberos_get_afs_token;
1034 goto parse_flag;
1035
Darren Tucker0efd1552003-08-26 11:49:55 +10001036 case sGssAuthentication:
1037 intptr = &options->gss_authentication;
1038 goto parse_flag;
1039
1040 case sGssCleanupCreds:
1041 intptr = &options->gss_cleanup_creds;
1042 goto parse_flag;
1043
Ben Lindstromade03f62001-12-06 18:22:17 +00001044 case sPasswordAuthentication:
1045 intptr = &options->password_authentication;
1046 goto parse_flag;
1047
Damien Miller01ed2272008-11-05 16:20:46 +11001048 case sZeroKnowledgePasswordAuthentication:
1049 intptr = &options->zero_knowledge_password_authentication;
1050 goto parse_flag;
1051
Ben Lindstromade03f62001-12-06 18:22:17 +00001052 case sKbdInteractiveAuthentication:
1053 intptr = &options->kbd_interactive_authentication;
1054 goto parse_flag;
1055
1056 case sChallengeResponseAuthentication:
1057 intptr = &options->challenge_response_authentication;
1058 goto parse_flag;
1059
1060 case sPrintMotd:
1061 intptr = &options->print_motd;
1062 goto parse_flag;
1063
1064 case sPrintLastLog:
1065 intptr = &options->print_lastlog;
1066 goto parse_flag;
1067
1068 case sX11Forwarding:
1069 intptr = &options->x11_forwarding;
1070 goto parse_flag;
1071
1072 case sX11DisplayOffset:
1073 intptr = &options->x11_display_offset;
1074 goto parse_int;
1075
Damien Miller95c249f2002-02-05 12:11:34 +11001076 case sX11UseLocalhost:
1077 intptr = &options->x11_use_localhost;
1078 goto parse_flag;
1079
Ben Lindstromade03f62001-12-06 18:22:17 +00001080 case sXAuthLocation:
1081 charptr = &options->xauth_location;
1082 goto parse_filename;
1083
1084 case sStrictModes:
1085 intptr = &options->strict_modes;
1086 goto parse_flag;
1087
Damien Miller12c150e2003-12-17 16:31:10 +11001088 case sTCPKeepAlive:
1089 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001090 goto parse_flag;
1091
1092 case sEmptyPasswd:
1093 intptr = &options->permit_empty_passwd;
1094 goto parse_flag;
1095
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001096 case sPermitUserEnvironment:
1097 intptr = &options->permit_user_env;
1098 goto parse_flag;
1099
Ben Lindstromade03f62001-12-06 18:22:17 +00001100 case sUseLogin:
1101 intptr = &options->use_login;
1102 goto parse_flag;
1103
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001104 case sCompression:
1105 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001106 multistate_ptr = multistate_compression;
1107 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001108
Ben Lindstromade03f62001-12-06 18:22:17 +00001109 case sGatewayPorts:
1110 intptr = &options->gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001111 multistate_ptr = multistate_gatewayports;
1112 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001113
Damien Miller3a961dc2003-06-03 10:25:48 +10001114 case sUseDNS:
1115 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001116 goto parse_flag;
1117
1118 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001119 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001120 arg = strdelim(&cp);
1121 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001122 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001123 fatal("%.200s line %d: unsupported log facility '%s'",
1124 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001125 if (*log_facility_ptr == -1)
1126 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001127 break;
1128
1129 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001130 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001131 arg = strdelim(&cp);
1132 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001133 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001134 fatal("%.200s line %d: unsupported log level '%s'",
1135 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001136 if (*log_level_ptr == -1)
1137 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001138 break;
1139
1140 case sAllowTcpForwarding:
1141 intptr = &options->allow_tcp_forwarding;
1142 goto parse_flag;
1143
Damien Miller4f755cd2008-05-19 14:57:41 +10001144 case sAllowAgentForwarding:
1145 intptr = &options->allow_agent_forwarding;
1146 goto parse_flag;
1147
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001148 case sUsePrivilegeSeparation:
1149 intptr = &use_privsep;
Damien Miller69ff1df2011-06-23 08:30:03 +10001150 multistate_ptr = multistate_privsep;
1151 goto parse_multistate;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001152
Ben Lindstromade03f62001-12-06 18:22:17 +00001153 case sAllowUsers:
1154 while ((arg = strdelim(&cp)) && *arg != '\0') {
1155 if (options->num_allow_users >= MAX_ALLOW_USERS)
1156 fatal("%s line %d: too many allow users.",
1157 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001158 if (!*activep)
1159 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001160 options->allow_users[options->num_allow_users++] =
1161 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001162 }
1163 break;
1164
1165 case sDenyUsers:
1166 while ((arg = strdelim(&cp)) && *arg != '\0') {
1167 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001168 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001169 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001170 if (!*activep)
1171 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001172 options->deny_users[options->num_deny_users++] =
1173 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001174 }
1175 break;
1176
1177 case sAllowGroups:
1178 while ((arg = strdelim(&cp)) && *arg != '\0') {
1179 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1180 fatal("%s line %d: too many allow groups.",
1181 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001182 if (!*activep)
1183 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001184 options->allow_groups[options->num_allow_groups++] =
1185 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001186 }
1187 break;
1188
1189 case sDenyGroups:
1190 while ((arg = strdelim(&cp)) && *arg != '\0') {
1191 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1192 fatal("%s line %d: too many deny groups.",
1193 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001194 if (!*activep)
1195 continue;
1196 options->deny_groups[options->num_deny_groups++] =
1197 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001198 }
1199 break;
1200
1201 case sCiphers:
1202 arg = strdelim(&cp);
1203 if (!arg || *arg == '\0')
1204 fatal("%s line %d: Missing argument.", filename, linenum);
1205 if (!ciphers_valid(arg))
1206 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1207 filename, linenum, arg ? arg : "<NONE>");
1208 if (options->ciphers == NULL)
1209 options->ciphers = xstrdup(arg);
1210 break;
1211
1212 case sMacs:
1213 arg = strdelim(&cp);
1214 if (!arg || *arg == '\0')
1215 fatal("%s line %d: Missing argument.", filename, linenum);
1216 if (!mac_valid(arg))
1217 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1218 filename, linenum, arg ? arg : "<NONE>");
1219 if (options->macs == NULL)
1220 options->macs = xstrdup(arg);
1221 break;
1222
Damien Millerd5f62bf2010-09-24 22:11:14 +10001223 case sKexAlgorithms:
1224 arg = strdelim(&cp);
1225 if (!arg || *arg == '\0')
1226 fatal("%s line %d: Missing argument.",
1227 filename, linenum);
1228 if (!kex_names_valid(arg))
1229 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1230 filename, linenum, arg ? arg : "<NONE>");
1231 if (options->kex_algorithms == NULL)
1232 options->kex_algorithms = xstrdup(arg);
1233 break;
1234
Ben Lindstromade03f62001-12-06 18:22:17 +00001235 case sProtocol:
1236 intptr = &options->protocol;
1237 arg = strdelim(&cp);
1238 if (!arg || *arg == '\0')
1239 fatal("%s line %d: Missing argument.", filename, linenum);
1240 value = proto_spec(arg);
1241 if (value == SSH_PROTO_UNKNOWN)
1242 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001243 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001244 if (*intptr == SSH_PROTO_UNKNOWN)
1245 *intptr = value;
1246 break;
1247
1248 case sSubsystem:
1249 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1250 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001251 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001252 }
1253 arg = strdelim(&cp);
1254 if (!arg || *arg == '\0')
1255 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001256 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001257 if (!*activep) {
1258 arg = strdelim(&cp);
1259 break;
1260 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001261 for (i = 0; i < options->num_subsystems; i++)
1262 if (strcmp(arg, options->subsystem_name[i]) == 0)
1263 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001264 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001265 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1266 arg = strdelim(&cp);
1267 if (!arg || *arg == '\0')
1268 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001269 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001270 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001271
1272 /* Collect arguments (separate to executable) */
1273 p = xstrdup(arg);
1274 len = strlen(p) + 1;
1275 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1276 len += 1 + strlen(arg);
1277 p = xrealloc(p, 1, len);
1278 strlcat(p, " ", len);
1279 strlcat(p, arg, len);
1280 }
1281 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001282 options->num_subsystems++;
1283 break;
1284
1285 case sMaxStartups:
1286 arg = strdelim(&cp);
1287 if (!arg || *arg == '\0')
1288 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001289 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001290 if ((n = sscanf(arg, "%d:%d:%d",
1291 &options->max_startups_begin,
1292 &options->max_startups_rate,
1293 &options->max_startups)) == 3) {
1294 if (options->max_startups_begin >
1295 options->max_startups ||
1296 options->max_startups_rate > 100 ||
1297 options->max_startups_rate < 1)
1298 fatal("%s line %d: Illegal MaxStartups spec.",
1299 filename, linenum);
1300 } else if (n != 1)
1301 fatal("%s line %d: Illegal MaxStartups spec.",
1302 filename, linenum);
1303 else
1304 options->max_startups = options->max_startups_begin;
1305 break;
1306
Darren Tucker89413db2004-05-24 10:36:23 +10001307 case sMaxAuthTries:
1308 intptr = &options->max_authtries;
1309 goto parse_int;
1310
Damien Miller7207f642008-05-19 15:34:50 +10001311 case sMaxSessions:
1312 intptr = &options->max_sessions;
1313 goto parse_int;
1314
Ben Lindstromade03f62001-12-06 18:22:17 +00001315 case sBanner:
1316 charptr = &options->banner;
1317 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001318
Ben Lindstromade03f62001-12-06 18:22:17 +00001319 /*
1320 * These options can contain %X options expanded at
1321 * connect time, so that you can specify paths like:
1322 *
1323 * AuthorizedKeysFile /etc/ssh_keys/%u
1324 */
1325 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001326 if (*activep && options->num_authkeys_files == 0) {
1327 while ((arg = strdelim(&cp)) && *arg != '\0') {
1328 if (options->num_authkeys_files >=
1329 MAX_AUTHKEYS_FILES)
1330 fatal("%s line %d: "
1331 "too many authorized keys files.",
1332 filename, linenum);
1333 options->authorized_keys_files[
1334 options->num_authkeys_files++] =
1335 tilde_expand_filename(arg, getuid());
1336 }
1337 }
1338 return 0;
1339
Damien Miller30da3442010-05-10 11:58:03 +10001340 case sAuthorizedPrincipalsFile:
1341 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001342 arg = strdelim(&cp);
1343 if (!arg || *arg == '\0')
1344 fatal("%s line %d: missing file name.",
1345 filename, linenum);
1346 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001347 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001348 /* increase optional counter */
1349 if (intptr != NULL)
1350 *intptr = *intptr + 1;
1351 }
1352 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001353
1354 case sClientAliveInterval:
1355 intptr = &options->client_alive_interval;
1356 goto parse_time;
1357
1358 case sClientAliveCountMax:
1359 intptr = &options->client_alive_count_max;
1360 goto parse_int;
1361
Darren Tucker46bc0752004-05-02 22:11:30 +10001362 case sAcceptEnv:
1363 while ((arg = strdelim(&cp)) && *arg != '\0') {
1364 if (strchr(arg, '=') != NULL)
1365 fatal("%s line %d: Invalid environment name.",
1366 filename, linenum);
1367 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1368 fatal("%s line %d: too many allow env.",
1369 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001370 if (!*activep)
Damien Millerc24da772012-06-20 21:53:58 +10001371 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001372 options->accept_env[options->num_accept_env++] =
1373 xstrdup(arg);
1374 }
1375 break;
1376
Damien Millerd27b9472005-12-13 19:29:02 +11001377 case sPermitTunnel:
1378 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001379 arg = strdelim(&cp);
1380 if (!arg || *arg == '\0')
1381 fatal("%s line %d: Missing yes/point-to-point/"
1382 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001383 value = -1;
1384 for (i = 0; tunmode_desc[i].val != -1; i++)
1385 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1386 value = tunmode_desc[i].val;
1387 break;
1388 }
1389 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001390 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1391 "no argument: %s", filename, linenum, arg);
1392 if (*intptr == -1)
1393 *intptr = value;
1394 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001395
Darren Tucker45150472006-07-12 22:34:17 +10001396 case sMatch:
1397 if (cmdline)
1398 fatal("Match directive not supported as a command-line "
1399 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001400 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10001401 if (value < 0)
1402 fatal("%s line %d: Bad Match condition", filename,
1403 linenum);
1404 *activep = value;
1405 break;
1406
Damien Miller9b439df2006-07-24 14:04:00 +10001407 case sPermitOpen:
1408 arg = strdelim(&cp);
1409 if (!arg || *arg == '\0')
1410 fatal("%s line %d: missing PermitOpen specification",
1411 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001412 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001413 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001414 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001415 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001416 options->num_permitted_opens = 0;
1417 }
Damien Miller9b439df2006-07-24 14:04:00 +10001418 break;
1419 }
Damien Millerc6081482012-04-22 11:18:53 +10001420 if (strcmp(arg, "none") == 0) {
1421 if (*activep && n == -1) {
1422 channel_clear_adm_permitted_opens();
1423 options->num_permitted_opens = 1;
1424 channel_disable_adm_local_opens();
1425 }
1426 break;
1427 }
Damien Millera29b95e2007-01-05 16:28:36 +11001428 if (*activep && n == -1)
1429 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001430 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1431 p = hpdelim(&arg);
1432 if (p == NULL)
1433 fatal("%s line %d: missing host in PermitOpen",
1434 filename, linenum);
1435 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001436 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001437 fatal("%s line %d: bad port number in "
1438 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001439 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001440 options->num_permitted_opens =
1441 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001442 }
Damien Miller9b439df2006-07-24 14:04:00 +10001443 break;
1444
Damien Millere2754432006-07-24 14:06:47 +10001445 case sForceCommand:
1446 if (cp == NULL)
1447 fatal("%.200s line %d: Missing argument.", filename,
1448 linenum);
1449 len = strspn(cp, WHITESPACE);
1450 if (*activep && options->adm_forced_command == NULL)
1451 options->adm_forced_command = xstrdup(cp + len);
1452 return 0;
1453
Damien Millerd8cb1f12008-02-10 22:40:12 +11001454 case sChrootDirectory:
1455 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001456
1457 arg = strdelim(&cp);
1458 if (!arg || *arg == '\0')
1459 fatal("%s line %d: missing file name.",
1460 filename, linenum);
1461 if (*activep && *charptr == NULL)
1462 *charptr = xstrdup(arg);
1463 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001464
Damien Miller1aed65e2010-03-04 21:53:35 +11001465 case sTrustedUserCAKeys:
1466 charptr = &options->trusted_user_ca_keys;
1467 goto parse_filename;
1468
1469 case sRevokedKeys:
1470 charptr = &options->revoked_keys_file;
1471 goto parse_filename;
1472
Damien Miller0dac6fb2010-11-20 15:19:38 +11001473 case sIPQoS:
1474 arg = strdelim(&cp);
1475 if ((value = parse_ipqos(arg)) == -1)
1476 fatal("%s line %d: Bad IPQoS value: %s",
1477 filename, linenum, arg);
1478 arg = strdelim(&cp);
1479 if (arg == NULL)
1480 value2 = value;
1481 else if ((value2 = parse_ipqos(arg)) == -1)
1482 fatal("%s line %d: Bad IPQoS value: %s",
1483 filename, linenum, arg);
1484 if (*activep) {
1485 options->ip_qos_interactive = value;
1486 options->ip_qos_bulk = value2;
1487 }
1488 break;
1489
Damien Miller23528812012-04-22 11:24:43 +10001490 case sVersionAddendum:
1491 if (cp == NULL)
1492 fatal("%.200s line %d: Missing argument.", filename,
1493 linenum);
1494 len = strspn(cp, WHITESPACE);
1495 if (*activep && options->version_addendum == NULL) {
1496 if (strcasecmp(cp + len, "none") == 0)
1497 options->version_addendum = xstrdup("");
1498 else if (strchr(cp + len, '\r') != NULL)
1499 fatal("%.200s line %d: Invalid argument",
1500 filename, linenum);
1501 else
1502 options->version_addendum = xstrdup(cp + len);
1503 }
1504 return 0;
1505
Damien Miller09d3e122012-10-31 08:58:58 +11001506 case sAuthorizedKeysCommand:
1507 len = strspn(cp, WHITESPACE);
1508 if (*activep && options->authorized_keys_command == NULL) {
1509 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1510 fatal("%.200s line %d: AuthorizedKeysCommand "
1511 "must be an absolute path",
1512 filename, linenum);
1513 options->authorized_keys_command = xstrdup(cp + len);
1514 }
1515 return 0;
1516
1517 case sAuthorizedKeysCommandUser:
1518 charptr = &options->authorized_keys_command_user;
1519
1520 arg = strdelim(&cp);
1521 if (*activep && *charptr == NULL)
1522 *charptr = xstrdup(arg);
1523 break;
1524
Ben Lindstromade03f62001-12-06 18:22:17 +00001525 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001526 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001527 filename, linenum, arg);
1528 while (arg)
1529 arg = strdelim(&cp);
1530 break;
1531
Damien Millerf9b3feb2003-05-16 11:38:32 +10001532 case sUnsupported:
1533 logit("%s line %d: Unsupported option %s",
1534 filename, linenum, arg);
1535 while (arg)
1536 arg = strdelim(&cp);
1537 break;
1538
Ben Lindstromade03f62001-12-06 18:22:17 +00001539 default:
1540 fatal("%s line %d: Missing handler for opcode %s (%d)",
1541 filename, linenum, arg, opcode);
1542 }
1543 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1544 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1545 filename, linenum, arg);
1546 return 0;
1547}
1548
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001549/* Reads the server configuration file. */
1550
Damien Miller4af51302000-04-16 11:18:38 +10001551void
Darren Tucker645ab752004-06-25 13:33:20 +10001552load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001553{
Damien Miller46cb75a2012-07-31 12:22:37 +10001554 char line[4096], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001555 FILE *f;
Damien Miller46cb75a2012-07-31 12:22:37 +10001556 int lineno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001557
Darren Tucker645ab752004-06-25 13:33:20 +10001558 debug2("%s: filename %s", __func__, filename);
1559 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001560 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001561 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001562 }
Darren Tucker645ab752004-06-25 13:33:20 +10001563 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001564 while (fgets(line, sizeof(line), f)) {
Damien Miller46cb75a2012-07-31 12:22:37 +10001565 lineno++;
1566 if (strlen(line) == sizeof(line) - 1)
1567 fatal("%s line %d too long", filename, lineno);
Darren Tucker645ab752004-06-25 13:33:20 +10001568 /*
1569 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001570 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001571 * line numbers later for error messages
1572 */
1573 if ((cp = strchr(line, '#')) != NULL)
1574 memcpy(cp, "\n", 2);
1575 cp = line + strspn(line, " \t\r");
1576
1577 buffer_append(conf, cp, strlen(cp));
1578 }
1579 buffer_append(conf, "\0", 1);
1580 fclose(f);
1581 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1582}
1583
1584void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001585parse_server_match_config(ServerOptions *options,
1586 struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10001587{
Darren Tucker45150472006-07-12 22:34:17 +10001588 ServerOptions mo;
1589
1590 initialize_server_options(&mo);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001591 parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11001592 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001593}
1594
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001595int parse_server_match_testspec(struct connection_info *ci, char *spec)
1596{
1597 char *p;
1598
1599 while ((p = strsep(&spec, ",")) && *p != '\0') {
1600 if (strncmp(p, "addr=", 5) == 0) {
1601 ci->address = xstrdup(p + 5);
1602 } else if (strncmp(p, "host=", 5) == 0) {
1603 ci->host = xstrdup(p + 5);
1604 } else if (strncmp(p, "user=", 5) == 0) {
1605 ci->user = xstrdup(p + 5);
1606 } else if (strncmp(p, "laddr=", 6) == 0) {
1607 ci->laddress = xstrdup(p + 6);
1608 } else if (strncmp(p, "lport=", 6) == 0) {
1609 ci->lport = a2port(p + 6);
1610 if (ci->lport == -1) {
1611 fprintf(stderr, "Invalid port '%s' in test mode"
1612 " specification %s\n", p+6, p);
1613 return -1;
1614 }
1615 } else {
1616 fprintf(stderr, "Invalid test mode specification %s\n",
1617 p);
1618 return -1;
1619 }
1620 }
1621 return 0;
1622}
1623
1624/*
1625 * returns 1 for a complete spec, 0 for partial spec and -1 for an
1626 * empty spec.
1627 */
1628int server_match_spec_complete(struct connection_info *ci)
1629{
1630 if (ci->user && ci->host && ci->address)
1631 return 1; /* complete */
1632 if (!ci->user && !ci->host && !ci->address)
1633 return -1; /* empty */
1634 return 0; /* partial */
1635}
1636
Darren Tucker1629c072007-02-19 22:25:37 +11001637/* Helper macros */
1638#define M_CP_INTOPT(n) do {\
1639 if (src->n != -1) \
1640 dst->n = src->n; \
1641} while (0)
1642#define M_CP_STROPT(n) do {\
1643 if (src->n != NULL) { \
1644 if (dst->n != NULL) \
1645 xfree(dst->n); \
1646 dst->n = src->n; \
1647 } \
1648} while(0)
Damien Millerd8478b62011-05-29 21:39:36 +10001649#define M_CP_STRARRAYOPT(n, num_n) do {\
1650 if (src->num_n != 0) { \
1651 for (dst->num_n = 0; dst->num_n < src->num_n; dst->num_n++) \
1652 dst->n[dst->num_n] = xstrdup(src->n[dst->num_n]); \
1653 } \
1654} while(0)
Darren Tucker1629c072007-02-19 22:25:37 +11001655
1656/*
1657 * Copy any supported values that are set.
1658 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001659 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001660 * array values that are not used pre-authentication, because any that we
1661 * do use must be explictly sent in mm_getpwnamallow().
1662 */
Darren Tucker45150472006-07-12 22:34:17 +10001663void
Darren Tucker1629c072007-02-19 22:25:37 +11001664copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001665{
Darren Tucker1629c072007-02-19 22:25:37 +11001666 M_CP_INTOPT(password_authentication);
1667 M_CP_INTOPT(gss_authentication);
1668 M_CP_INTOPT(rsa_authentication);
1669 M_CP_INTOPT(pubkey_authentication);
1670 M_CP_INTOPT(kerberos_authentication);
1671 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001672 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001673 M_CP_INTOPT(kbd_interactive_authentication);
Damien Miller01ed2272008-11-05 16:20:46 +11001674 M_CP_INTOPT(zero_knowledge_password_authentication);
Damien Miller09d3e122012-10-31 08:58:58 +11001675 M_CP_STROPT(authorized_keys_command);
1676 M_CP_STROPT(authorized_keys_command_user);
Darren Tucker15f94272008-01-01 20:36:56 +11001677 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001678 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001679
1680 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001681 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001682 M_CP_INTOPT(permit_tun);
Darren Tucker1629c072007-02-19 22:25:37 +11001683 M_CP_INTOPT(gateway_ports);
1684 M_CP_INTOPT(x11_display_offset);
1685 M_CP_INTOPT(x11_forwarding);
1686 M_CP_INTOPT(x11_use_localhost);
Damien Miller7207f642008-05-19 15:34:50 +10001687 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001688 M_CP_INTOPT(max_authtries);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001689 M_CP_INTOPT(ip_qos_interactive);
1690 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker1629c072007-02-19 22:25:37 +11001691
Damien Millerf2e407e2011-05-20 19:04:14 +10001692 /* See comment in servconf.h */
1693 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10001694
Damien Millerc2411902011-05-20 19:03:49 +10001695 /*
1696 * The only things that should be below this point are string options
1697 * which are only used after authentication.
1698 */
Damien Miller5d74e582011-05-20 19:03:31 +10001699 if (preauth)
1700 return;
Damien Millerd8478b62011-05-29 21:39:36 +10001701
Damien Miller5d74e582011-05-20 19:03:31 +10001702 M_CP_STROPT(adm_forced_command);
1703 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001704}
1705
Darren Tucker1629c072007-02-19 22:25:37 +11001706#undef M_CP_INTOPT
1707#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10001708#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11001709
Darren Tucker45150472006-07-12 22:34:17 +10001710void
1711parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001712 struct connection_info *connectinfo)
Darren Tucker45150472006-07-12 22:34:17 +10001713{
1714 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001715 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001716
1717 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1718
Darren Tucker9fbac712004-08-12 22:41:44 +10001719 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001720 active = connectinfo ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001721 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001722 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001723 if (process_server_config_line(options, cp, filename,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001724 linenum++, &active, connectinfo) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001725 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001726 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001727 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001728 if (bad_options > 0)
1729 fatal("%s: terminating, %d bad configuration options",
1730 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001731}
Darren Tuckere7140f22008-06-10 23:01:51 +10001732
1733static const char *
Damien Miller82c55872011-06-23 08:20:30 +10001734fmt_multistate_int(int val, const struct multistate *m)
1735{
1736 u_int i;
1737
1738 for (i = 0; m[i].key != NULL; i++) {
1739 if (m[i].value == val)
1740 return m[i].key;
1741 }
1742 return "UNKNOWN";
1743}
1744
1745static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10001746fmt_intarg(ServerOpCodes code, int val)
1747{
Damien Miller82c55872011-06-23 08:20:30 +10001748 if (val == -1)
1749 return "unset";
1750 switch (code) {
1751 case sAddressFamily:
1752 return fmt_multistate_int(val, multistate_addressfamily);
1753 case sPermitRootLogin:
1754 return fmt_multistate_int(val, multistate_permitrootlogin);
1755 case sGatewayPorts:
1756 return fmt_multistate_int(val, multistate_gatewayports);
1757 case sCompression:
1758 return fmt_multistate_int(val, multistate_compression);
Damien Miller69ff1df2011-06-23 08:30:03 +10001759 case sUsePrivilegeSeparation:
1760 return fmt_multistate_int(val, multistate_privsep);
Damien Miller82c55872011-06-23 08:20:30 +10001761 case sProtocol:
Darren Tuckere7140f22008-06-10 23:01:51 +10001762 switch (val) {
1763 case SSH_PROTO_1:
1764 return "1";
1765 case SSH_PROTO_2:
1766 return "2";
1767 case (SSH_PROTO_1|SSH_PROTO_2):
1768 return "2,1";
1769 default:
1770 return "UNKNOWN";
1771 }
Damien Miller82c55872011-06-23 08:20:30 +10001772 default:
1773 switch (val) {
1774 case 0:
1775 return "no";
1776 case 1:
1777 return "yes";
1778 default:
1779 return "UNKNOWN";
1780 }
Darren Tuckere7140f22008-06-10 23:01:51 +10001781 }
Darren Tuckere7140f22008-06-10 23:01:51 +10001782}
1783
1784static const char *
1785lookup_opcode_name(ServerOpCodes code)
1786{
1787 u_int i;
1788
1789 for (i = 0; keywords[i].name != NULL; i++)
1790 if (keywords[i].opcode == code)
1791 return(keywords[i].name);
1792 return "UNKNOWN";
1793}
1794
1795static void
1796dump_cfg_int(ServerOpCodes code, int val)
1797{
1798 printf("%s %d\n", lookup_opcode_name(code), val);
1799}
1800
1801static void
1802dump_cfg_fmtint(ServerOpCodes code, int val)
1803{
1804 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
1805}
1806
1807static void
1808dump_cfg_string(ServerOpCodes code, const char *val)
1809{
1810 if (val == NULL)
1811 return;
1812 printf("%s %s\n", lookup_opcode_name(code), val);
1813}
1814
1815static void
1816dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
1817{
1818 u_int i;
1819
1820 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10001821 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
1822}
1823
1824static void
1825dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
1826{
1827 u_int i;
1828
1829 printf("%s", lookup_opcode_name(code));
1830 for (i = 0; i < count; i++)
1831 printf(" %s", vals[i]);
1832 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10001833}
1834
1835void
1836dump_config(ServerOptions *o)
1837{
1838 u_int i;
1839 int ret;
1840 struct addrinfo *ai;
1841 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
1842
1843 /* these are usually at the top of the config */
1844 for (i = 0; i < o->num_ports; i++)
1845 printf("port %d\n", o->ports[i]);
1846 dump_cfg_fmtint(sProtocol, o->protocol);
1847 dump_cfg_fmtint(sAddressFamily, o->address_family);
1848
1849 /* ListenAddress must be after Port */
1850 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
1851 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1852 sizeof(addr), port, sizeof(port),
1853 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1854 error("getnameinfo failed: %.100s",
1855 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
1856 strerror(errno));
1857 } else {
1858 if (ai->ai_family == AF_INET6)
1859 printf("listenaddress [%s]:%s\n", addr, port);
1860 else
1861 printf("listenaddress %s:%s\n", addr, port);
1862 }
1863 }
1864
1865 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10001866#ifdef USE_PAM
1867 dump_cfg_int(sUsePAM, o->use_pam);
1868#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001869 dump_cfg_int(sServerKeyBits, o->server_key_bits);
1870 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
1871 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
1872 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
1873 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11001874 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10001875 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
1876 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
1877
1878 /* formatted integer arguments */
1879 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
1880 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
1881 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
1882 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
1883 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
1884 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
1885 o->hostbased_uses_name_from_packet_only);
1886 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
1887 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10001888#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10001889 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
1890 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
1891 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10001892# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10001893 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10001894# endif
1895#endif
1896#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10001897 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
1898 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10001899#endif
Damien Miller01ed2272008-11-05 16:20:46 +11001900#ifdef JPAKE
1901 dump_cfg_fmtint(sZeroKnowledgePasswordAuthentication,
1902 o->zero_knowledge_password_authentication);
1903#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001904 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
1905 dump_cfg_fmtint(sKbdInteractiveAuthentication,
1906 o->kbd_interactive_authentication);
1907 dump_cfg_fmtint(sChallengeResponseAuthentication,
1908 o->challenge_response_authentication);
1909 dump_cfg_fmtint(sPrintMotd, o->print_motd);
1910 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
1911 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
1912 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
1913 dump_cfg_fmtint(sStrictModes, o->strict_modes);
1914 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
1915 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
1916 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
1917 dump_cfg_fmtint(sUseLogin, o->use_login);
1918 dump_cfg_fmtint(sCompression, o->compression);
1919 dump_cfg_fmtint(sGatewayPorts, o->gateway_ports);
1920 dump_cfg_fmtint(sUseDNS, o->use_dns);
1921 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
1922 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
1923
1924 /* string arguments */
1925 dump_cfg_string(sPidFile, o->pid_file);
1926 dump_cfg_string(sXAuthLocation, o->xauth_location);
1927 dump_cfg_string(sCiphers, o->ciphers);
1928 dump_cfg_string(sMacs, o->macs);
1929 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10001930 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11001931 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11001932 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
1933 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10001934 dump_cfg_string(sAuthorizedPrincipalsFile,
1935 o->authorized_principals_file);
Damien Miller23528812012-04-22 11:24:43 +10001936 dump_cfg_string(sVersionAddendum, o->version_addendum);
Damien Miller09d3e122012-10-31 08:58:58 +11001937 dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
1938 dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
Darren Tuckere7140f22008-06-10 23:01:51 +10001939
1940 /* string arguments requiring a lookup */
1941 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
1942 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
1943
1944 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10001945 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
1946 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10001947 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
1948 o->host_key_files);
Damien Miller0a80ca12010-02-27 07:55:05 +11001949 dump_cfg_strarray(sHostKeyFile, o->num_host_cert_files,
1950 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10001951 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
1952 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
1953 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
1954 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
1955 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
1956
1957 /* other arguments */
1958 for (i = 0; i < o->num_subsystems; i++)
1959 printf("subsystem %s %s\n", o->subsystem_name[i],
1960 o->subsystem_args[i]);
1961
1962 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
1963 o->max_startups_rate, o->max_startups);
1964
1965 for (i = 0; tunmode_desc[i].val != -1; i++)
1966 if (tunmode_desc[i].val == o->permit_tun) {
1967 s = tunmode_desc[i].text;
1968 break;
1969 }
1970 dump_cfg_string(sPermitTunnel, s);
1971
Damien Miller91475862011-05-05 14:14:34 +10001972 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
1973 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11001974
Darren Tuckere7140f22008-06-10 23:01:51 +10001975 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10001976}