blob: 12f43c91eaa227a550961a8c20edf06e3d57c20a [file] [log] [blame]
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001
2/* $OpenBSD: servconf.c,v 1.226 2012/05/13 01:42:32 dtucker 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 Miller01ed2272008-11-05 16:20:46 +1100138 options->zero_knowledge_password_authentication = -1;
Damien Miller1aed65e2010-03-04 21:53:35 +1100139 options->revoked_keys_file = NULL;
140 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000141 options->authorized_principals_file = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100142 options->ip_qos_interactive = -1;
143 options->ip_qos_bulk = -1;
Damien Miller23528812012-04-22 11:24:43 +1000144 options->version_addendum = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145}
146
Damien Miller4af51302000-04-16 11:18:38 +1000147void
Damien Miller95def091999-11-25 00:26:21 +1100148fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149{
Damien Miller726273e2001-11-12 11:40:11 +1100150 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000151 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000152 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100153
154 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100155 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +1100156 options->protocol = SSH_PROTO_2;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100157 if (options->num_host_key_files == 0) {
158 /* fill default hostkeys for protocols */
159 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100160 options->host_key_files[options->num_host_key_files++] =
161 _PATH_HOST_KEY_FILE;
162 if (options->protocol & SSH_PROTO_2) {
163 options->host_key_files[options->num_host_key_files++] =
164 _PATH_HOST_RSA_KEY_FILE;
165 options->host_key_files[options->num_host_key_files++] =
166 _PATH_HOST_DSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100167#ifdef OPENSSL_HAS_ECC
Damien Millere13cadf2010-09-10 11:15:33 +1000168 options->host_key_files[options->num_host_key_files++] =
169 _PATH_HOST_ECDSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100170#endif
Damien Miller7fc23732002-01-22 23:19:11 +1100171 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100172 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100173 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100174 if (options->num_ports == 0)
175 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
176 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000177 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000178 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000179 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100180 if (options->server_key_bits == -1)
Darren Tucker7499b0c2008-07-02 22:35:43 +1000181 options->server_key_bits = 1024;
Damien Miller95def091999-11-25 00:26:21 +1100182 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000183 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100184 if (options->key_regeneration_time == -1)
185 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000186 if (options->permit_root_login == PERMIT_NOT_SET)
187 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100188 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100189 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100190 if (options->ignore_user_known_hosts == -1)
191 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100192 if (options->print_motd == -1)
193 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000194 if (options->print_lastlog == -1)
195 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100196 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100197 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100198 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100199 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100200 if (options->x11_use_localhost == -1)
201 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000202 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000203 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100204 if (options->strict_modes == -1)
205 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100206 if (options->tcp_keep_alive == -1)
207 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100208 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100209 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100210 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000211 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100212 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100213 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000214 if (options->hostbased_authentication == -1)
215 options->hostbased_authentication = 0;
216 if (options->hostbased_uses_name_from_packet_only == -1)
217 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100218 if (options->rsa_authentication == -1)
219 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100220 if (options->pubkey_authentication == -1)
221 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100222 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000223 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100224 if (options->kerberos_or_local_passwd == -1)
225 options->kerberos_or_local_passwd = 1;
226 if (options->kerberos_ticket_cleanup == -1)
227 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100228 if (options->kerberos_get_afs_token == -1)
229 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000230 if (options->gss_authentication == -1)
231 options->gss_authentication = 0;
232 if (options->gss_cleanup_creds == -1)
233 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100234 if (options->password_authentication == -1)
235 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100236 if (options->kbd_interactive_authentication == -1)
237 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000238 if (options->challenge_response_authentication == -1)
239 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100240 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100241 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000242 if (options->permit_user_env == -1)
243 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100244 if (options->use_login == -1)
245 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000246 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000247 options->compression = COMP_DELAYED;
Damien Miller50a41ed2000-10-16 12:14:42 +1100248 if (options->allow_tcp_forwarding == -1)
249 options->allow_tcp_forwarding = 1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000250 if (options->allow_agent_forwarding == -1)
251 options->allow_agent_forwarding = 1;
Damien Millere247cc42000-05-07 12:03:14 +1000252 if (options->gateway_ports == -1)
253 options->gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000254 if (options->max_startups == -1)
255 options->max_startups = 10;
Damien Miller942da032000-08-18 13:59:06 +1000256 if (options->max_startups_rate == -1)
257 options->max_startups_rate = 100; /* 100% */
258 if (options->max_startups_begin == -1)
259 options->max_startups_begin = options->max_startups;
Darren Tucker89413db2004-05-24 10:36:23 +1000260 if (options->max_authtries == -1)
261 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000262 if (options->max_sessions == -1)
263 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000264 if (options->use_dns == -1)
265 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000266 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100267 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000268 if (options->client_alive_count_max == -1)
269 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000270 if (options->num_authkeys_files == 0) {
271 options->authorized_keys_files[options->num_authkeys_files++] =
272 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS);
273 options->authorized_keys_files[options->num_authkeys_files++] =
274 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS2);
275 }
Damien Millerd27b9472005-12-13 19:29:02 +1100276 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100277 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller01ed2272008-11-05 16:20:46 +1100278 if (options->zero_knowledge_password_authentication == -1)
279 options->zero_knowledge_password_authentication = 0;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100280 if (options->ip_qos_interactive == -1)
281 options->ip_qos_interactive = IPTOS_LOWDELAY;
282 if (options->ip_qos_bulk == -1)
283 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller23528812012-04-22 11:24:43 +1000284 if (options->version_addendum == NULL)
285 options->version_addendum = xstrdup("");
Ben Lindstromfb62a692002-06-06 19:47:11 +0000286 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000287 if (use_privsep == -1)
Damien Miller69ff1df2011-06-23 08:30:03 +1000288 use_privsep = PRIVSEP_ON;
Damien Miller4903eb42002-06-21 16:20:44 +1000289
Tim Rice40017b02002-07-14 13:36:49 -0700290#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000291 if (use_privsep && options->compression == 1) {
292 error("This platform does not support both privilege "
293 "separation and compression");
294 error("Compression disabled");
295 options->compression = 0;
296 }
297#endif
298
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299}
300
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100302typedef enum {
303 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100304 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000305 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100306 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100307 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
308 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000309 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100310 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100311 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000312 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100313 sPasswordAuthentication, sKbdInteractiveAuthentication,
314 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000315 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100316 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller12c150e2003-12-17 16:31:10 +1100317 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000318 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Damien Miller50a41ed2000-10-16 12:14:42 +1100319 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000320 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000321 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
Damien Miller7207f642008-05-19 15:34:50 +1000322 sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000323 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100324 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Damien Millerec2eaa32011-05-20 18:57:14 +1000325 sClientAliveCountMax, sAuthorizedKeysFile,
Damien Millerd27b9472005-12-13 19:29:02 +1100326 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100327 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100328 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller0a80ca12010-02-27 07:55:05 +1100329 sZeroKnowledgePasswordAuthentication, sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000330 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Miller23528812012-04-22 11:24:43 +1000331 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000332 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333} ServerOpCodes;
334
Darren Tucker45150472006-07-12 22:34:17 +1000335#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
336#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
337#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
338
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100340static struct {
341 const char *name;
342 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000343 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100344} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100345 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000346#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000347 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000348#else
Darren Tucker45150472006-07-12 22:34:17 +1000349 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000350#endif
Darren Tucker45150472006-07-12 22:34:17 +1000351 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100352 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000353 { "port", sPort, SSHCFG_GLOBAL },
354 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
355 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
356 { "pidfile", sPidFile, SSHCFG_GLOBAL },
357 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
358 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
359 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100360 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000361 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
362 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
363 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100364 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
365 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000366 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100367 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
368 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000369 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000370#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100371 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000372 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
373 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100374#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000375 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000376#else
Darren Tucker45150472006-07-12 22:34:17 +1000377 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100378#endif
379#else
Darren Tucker1629c072007-02-19 22:25:37 +1100380 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000381 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
382 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
383 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000384#endif
Darren Tucker45150472006-07-12 22:34:17 +1000385 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
386 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000387#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100388 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000389 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000390#else
Darren Tucker1629c072007-02-19 22:25:37 +1100391 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000392 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000393#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100394 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
395 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100396 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000397 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
Damien Miller01ed2272008-11-05 16:20:46 +1100398#ifdef JPAKE
399 { "zeroknowledgepasswordauthentication", sZeroKnowledgePasswordAuthentication, SSHCFG_ALL },
400#else
401 { "zeroknowledgepasswordauthentication", sUnsupported, SSHCFG_ALL },
402#endif
Darren Tucker45150472006-07-12 22:34:17 +1000403 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
404 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
405 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
406 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
407 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
408 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
409 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000410 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
411 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
412 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000413 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
414 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100415 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000416 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
417 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
418 { "compression", sCompression, SSHCFG_GLOBAL },
419 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
420 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
421 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000422 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000423 { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
424 { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
425 { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
426 { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
427 { "ciphers", sCiphers, SSHCFG_GLOBAL },
428 { "macs", sMacs, SSHCFG_GLOBAL },
429 { "protocol", sProtocol, SSHCFG_GLOBAL },
430 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
431 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
432 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000433 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000434 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100435 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000436 { "usedns", sUseDNS, SSHCFG_GLOBAL },
437 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
438 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
439 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
440 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000441 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000442 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000443 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Darren Tucker45150472006-07-12 22:34:17 +1000444 { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000445 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000446 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000447 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000448 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100449 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100450 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100451 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
452 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000453 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000454 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100455 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000456 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000457 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458};
459
Darren Tuckere7140f22008-06-10 23:01:51 +1000460static struct {
461 int val;
462 char *text;
463} tunmode_desc[] = {
464 { SSH_TUNMODE_NO, "no" },
465 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
466 { SSH_TUNMODE_ETHERNET, "ethernet" },
467 { SSH_TUNMODE_YES, "yes" },
468 { -1, NULL }
469};
470
Damien Miller5428f641999-11-25 11:54:57 +1100471/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000472 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100473 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474
Damien Miller4af51302000-04-16 11:18:38 +1000475static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100476parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000477 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000479 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480
Damien Miller95def091999-11-25 00:26:21 +1100481 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000482 if (strcasecmp(cp, keywords[i].name) == 0) {
483 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100484 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000485 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000487 error("%s: line %d: Bad configuration option: %s",
488 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100489 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490}
491
Darren Tucker88b6fb22010-01-13 22:44:29 +1100492char *
493derelativise_path(const char *path)
494{
Damien Miller44451d02010-03-26 10:40:04 +1100495 char *expanded, *ret, cwd[MAXPATHLEN];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100496
497 expanded = tilde_expand_filename(path, getuid());
498 if (*expanded == '/')
499 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100500 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100501 fatal("%s: getcwd: %s", __func__, strerror(errno));
502 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100503 xfree(expanded);
504 return ret;
505}
506
Ben Lindstrombba81212001-06-25 05:01:22 +0000507static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100508add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100509{
Damien Millereccb9de2005-06-17 12:59:34 +1000510 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100511
512 if (options->num_ports == 0)
513 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100514 if (options->address_family == -1)
515 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000516 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000517 for (i = 0; i < options->num_ports; i++)
518 add_one_listen_addr(options, addr, options->ports[i]);
519 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000520 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000521}
522
Ben Lindstrombba81212001-06-25 05:01:22 +0000523static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100524add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000525{
526 struct addrinfo hints, *ai, *aitop;
527 char strport[NI_MAXSERV];
528 int gaierr;
529
530 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100531 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000532 hints.ai_socktype = SOCK_STREAM;
533 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100534 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000535 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
536 fatal("bad addr or host: %s (%s)",
537 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100538 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000539 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
540 ;
541 ai->ai_next = options->listen_addrs;
542 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100543}
544
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000545struct connection_info *
546get_connection_info(int populate, int use_dns)
547{
548 static struct connection_info ci;
549
550 if (!populate)
551 return &ci;
552 ci.host = get_canonical_hostname(use_dns);
553 ci.address = get_remote_ipaddr();
554 ci.laddress = get_local_ipaddr(packet_get_connection_in());
555 ci.lport = get_local_port();
556 return &ci;
557}
558
Darren Tucker45150472006-07-12 22:34:17 +1000559/*
560 * The strategy for the Match blocks is that the config file is parsed twice.
561 *
562 * The first time is at startup. activep is initialized to 1 and the
563 * directives in the global context are processed and acted on. Hitting a
564 * Match directive unsets activep and the directives inside the block are
565 * checked for syntax only.
566 *
567 * The second time is after a connection has been established but before
568 * authentication. activep is initialized to 2 and global config directives
569 * are ignored since they have already been processed. If the criteria in a
570 * Match block is met, activep is set and the subsequent directives
571 * processed and actioned until EOF or another Match block unsets it. Any
572 * options set are copied into the main server config.
573 *
574 * Potential additions/improvements:
575 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
576 *
577 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
578 * Match Address 192.168.0.*
579 * Tag trusted
580 * Match Group wheel
581 * Tag trusted
582 * Match Tag trusted
583 * AllowTcpForwarding yes
584 * GatewayPorts clientspecified
585 * [...]
586 *
587 * - Add a PermittedChannelRequests directive
588 * Match Group shell
589 * PermittedChannelRequests session,forwarded-tcpip
590 */
591
592static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000593match_cfg_line_group(const char *grps, int line, const char *user)
594{
595 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000596 struct passwd *pw;
597
Damien Miller565ca3f2006-08-19 00:23:15 +1000598 if (user == NULL)
599 goto out;
600
601 if ((pw = getpwnam(user)) == NULL) {
602 debug("Can't match group at line %d because user %.100s does "
603 "not exist", line, user);
604 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
605 debug("Can't Match group because user %.100s not in any group "
606 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000607 } else if (ga_match_pattern_list(grps) != 1) {
608 debug("user %.100s does not match group list %.100s at line %d",
609 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000610 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000611 debug("user %.100s matched group list %.100s at line %d", user,
612 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000613 result = 1;
614 }
615out:
616 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000617 return result;
618}
619
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000620/*
621 * All of the attributes on a single Match line are ANDed together, so we need to check every
622 * attribute and set the result to zero if any attribute does not match.
623 */
Damien Miller565ca3f2006-08-19 00:23:15 +1000624static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000625match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +1000626{
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000627 int result = 1, port;
Darren Tucker45150472006-07-12 22:34:17 +1000628 char *arg, *attrib, *cp = *condition;
629 size_t len;
630
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000631 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000632 debug3("checking syntax for 'Match %s'", cp);
633 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000634 debug3("checking match for '%s' user %s host %s addr %s "
635 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
636 ci->host ? ci->host : "(null)",
637 ci->address ? ci->address : "(null)",
638 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +1000639
640 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
641 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
642 error("Missing Match criteria for %s", attrib);
643 return -1;
644 }
645 len = strlen(arg);
646 if (strcasecmp(attrib, "user") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000647 if (ci == NULL || ci->user == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000648 result = 0;
649 continue;
650 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000651 if (match_pattern_list(ci->user, arg, len, 0) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000652 result = 0;
653 else
654 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000655 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000656 } else if (strcasecmp(attrib, "group") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000657 if (ci == NULL || ci->user == NULL) {
658 result = 0;
659 continue;
660 }
661 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +1000662 case -1:
663 return -1;
664 case 0:
665 result = 0;
666 }
Darren Tucker45150472006-07-12 22:34:17 +1000667 } else if (strcasecmp(attrib, "host") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000668 if (ci == NULL || ci->host == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000669 result = 0;
670 continue;
671 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000672 if (match_hostname(ci->host, arg, len) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000673 result = 0;
674 else
675 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000676 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +1000677 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000678 if (ci == NULL || ci->address == NULL) {
679 result = 0;
680 continue;
681 }
682 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000683 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000684 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000685 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000686 break;
687 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000688 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000689 result = 0;
690 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000691 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000692 return -1;
693 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000694 } else if (strcasecmp(attrib, "localaddress") == 0){
695 if (ci == NULL || ci->laddress == NULL) {
696 result = 0;
697 continue;
698 }
699 switch (addr_match_list(ci->laddress, arg)) {
700 case 1:
701 debug("connection from %.100s matched "
702 "'LocalAddress %.100s' at line %d",
703 ci->laddress, arg, line);
704 break;
705 case 0:
706 case -1:
707 result = 0;
708 break;
709 case -2:
710 return -1;
711 }
712 } else if (strcasecmp(attrib, "localport") == 0) {
713 if ((port = a2port(arg)) == -1) {
714 error("Invalid LocalPort '%s' on Match line",
715 arg);
716 return -1;
717 }
718 if (ci == NULL || ci->lport == 0) {
719 result = 0;
720 continue;
721 }
722 /* TODO support port lists */
723 if (port == ci->lport)
724 debug("connection from %.100s matched "
725 "'LocalPort %d' at line %d",
726 ci->laddress, port, line);
727 else
728 result = 0;
Darren Tucker45150472006-07-12 22:34:17 +1000729 } else {
730 error("Unsupported Match attribute %s", attrib);
731 return -1;
732 }
733 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000734 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000735 debug3("match %sfound", result ? "" : "not ");
736 *condition = cp;
737 return result;
738}
739
Damien Millere2754432006-07-24 14:06:47 +1000740#define WHITESPACE " \t\r\n"
741
Damien Miller33322122011-06-20 14:43:11 +1000742/* Multistate option parsing */
743struct multistate {
744 char *key;
745 int value;
746};
747static const struct multistate multistate_addressfamily[] = {
748 { "inet", AF_INET },
749 { "inet6", AF_INET6 },
750 { "any", AF_UNSPEC },
751 { NULL, -1 }
752};
753static const struct multistate multistate_permitrootlogin[] = {
754 { "without-password", PERMIT_NO_PASSWD },
755 { "forced-commands-only", PERMIT_FORCED_ONLY },
756 { "yes", PERMIT_YES },
757 { "no", PERMIT_NO },
758 { NULL, -1 }
759};
760static const struct multistate multistate_compression[] = {
761 { "delayed", COMP_DELAYED },
762 { "yes", COMP_ZLIB },
763 { "no", COMP_NONE },
764 { NULL, -1 }
765};
766static const struct multistate multistate_gatewayports[] = {
767 { "clientspecified", 2 },
768 { "yes", 1 },
769 { "no", 0 },
770 { NULL, -1 }
771};
Damien Miller69ff1df2011-06-23 08:30:03 +1000772static const struct multistate multistate_privsep[] = {
773 { "sandbox", PRIVSEP_SANDBOX },
774 { "yes", PRIVSEP_ON },
775 { "no", PRIVSEP_OFF },
776 { NULL, -1 }
777};
Damien Miller33322122011-06-20 14:43:11 +1000778
Ben Lindstromade03f62001-12-06 18:22:17 +0000779int
780process_server_config_line(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000781 const char *filename, int linenum, int *activep,
782 struct connection_info *connectinfo)
Ben Lindstromade03f62001-12-06 18:22:17 +0000783{
784 char *cp, **charptr, *arg, *p;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100785 int cmdline = 0, *intptr, value, value2, n;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100786 SyslogFacility *log_facility_ptr;
787 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000788 ServerOpCodes opcode;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100789 int port;
Darren Tucker45150472006-07-12 22:34:17 +1000790 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000791 size_t len;
Damien Miller33322122011-06-20 14:43:11 +1000792 const struct multistate *multistate_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000793
794 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100795 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100796 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000797 /* Ignore leading whitespace */
798 if (*arg == '\0')
799 arg = strdelim(&cp);
800 if (!arg || !*arg || *arg == '#')
801 return 0;
802 intptr = NULL;
803 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000804 opcode = parse_token(arg, filename, linenum, &flags);
805
806 if (activep == NULL) { /* We are processing a command line directive */
807 cmdline = 1;
808 activep = &cmdline;
809 }
810 if (*activep && opcode != sMatch)
811 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
812 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000813 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000814 fatal("%s line %d: Directive '%s' is not allowed "
815 "within a Match block", filename, linenum, arg);
816 } else { /* this is a directive we have already processed */
817 while (arg)
818 arg = strdelim(&cp);
819 return 0;
820 }
821 }
822
Ben Lindstromade03f62001-12-06 18:22:17 +0000823 switch (opcode) {
824 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000825 case sUsePAM:
826 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000827 goto parse_flag;
828
829 /* Standard Options */
830 case sBadOption:
831 return -1;
832 case sPort:
833 /* ignore ports from configfile if cmdline specifies ports */
834 if (options->ports_from_cmdline)
835 return 0;
836 if (options->listen_addrs != NULL)
837 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100838 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000839 if (options->num_ports >= MAX_PORTS)
840 fatal("%s line %d: too many ports.",
841 filename, linenum);
842 arg = strdelim(&cp);
843 if (!arg || *arg == '\0')
844 fatal("%s line %d: missing port number.",
845 filename, linenum);
846 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100847 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000848 fatal("%s line %d: Badly formatted port number.",
849 filename, linenum);
850 break;
851
852 case sServerKeyBits:
853 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +1000854 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +0000855 arg = strdelim(&cp);
856 if (!arg || *arg == '\0')
857 fatal("%s line %d: missing integer value.",
858 filename, linenum);
859 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000860 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000861 *intptr = value;
862 break;
863
864 case sLoginGraceTime:
865 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +1000866 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +0000867 arg = strdelim(&cp);
868 if (!arg || *arg == '\0')
869 fatal("%s line %d: missing time value.",
870 filename, linenum);
871 if ((value = convtime(arg)) == -1)
872 fatal("%s line %d: invalid time value.",
873 filename, linenum);
874 if (*intptr == -1)
875 *intptr = value;
876 break;
877
878 case sKeyRegenerationTime:
879 intptr = &options->key_regeneration_time;
880 goto parse_time;
881
882 case sListenAddress:
883 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100884 if (arg == NULL || *arg == '\0')
885 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000886 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000887 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
888 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
889 && strchr(p+1, ':') != NULL) {
890 add_listen_addr(options, arg, 0);
891 break;
892 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100893 p = hpdelim(&arg);
894 if (p == NULL)
895 fatal("%s line %d: bad address:port usage",
896 filename, linenum);
897 p = cleanhostname(p);
898 if (arg == NULL)
899 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100900 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100901 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000902
Damien Millerf91ee4c2005-03-01 21:24:33 +1100903 add_listen_addr(options, p, port);
904
Ben Lindstromade03f62001-12-06 18:22:17 +0000905 break;
906
Darren Tucker0f383232005-01-20 10:57:56 +1100907 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +1000908 intptr = &options->address_family;
909 multistate_ptr = multistate_addressfamily;
910 if (options->listen_addrs != NULL)
911 fatal("%s line %d: address family must be specified "
912 "before ListenAddress.", filename, linenum);
913 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +1100914 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000915 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +1000916 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +1000917 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +1000918 value = -1;
919 for (i = 0; multistate_ptr[i].key != NULL; i++) {
920 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
921 value = multistate_ptr[i].value;
922 break;
923 }
924 }
925 if (value == -1)
926 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +1100927 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +1000928 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +1100929 *intptr = value;
930 break;
931
Ben Lindstromade03f62001-12-06 18:22:17 +0000932 case sHostKeyFile:
933 intptr = &options->num_host_key_files;
934 if (*intptr >= MAX_HOSTKEYS)
935 fatal("%s line %d: too many host keys specified (max %d).",
936 filename, linenum, MAX_HOSTKEYS);
937 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +1000938 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +0000939 arg = strdelim(&cp);
940 if (!arg || *arg == '\0')
941 fatal("%s line %d: missing file name.",
942 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +1000943 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +1100944 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000945 /* increase optional counter */
946 if (intptr != NULL)
947 *intptr = *intptr + 1;
948 }
949 break;
950
Damien Miller0a80ca12010-02-27 07:55:05 +1100951 case sHostCertificate:
952 intptr = &options->num_host_cert_files;
953 if (*intptr >= MAX_HOSTKEYS)
954 fatal("%s line %d: too many host certificates "
955 "specified (max %d).", filename, linenum,
956 MAX_HOSTCERTS);
957 charptr = &options->host_cert_files[*intptr];
958 goto parse_filename;
959 break;
960
Ben Lindstromade03f62001-12-06 18:22:17 +0000961 case sPidFile:
962 charptr = &options->pid_file;
963 goto parse_filename;
964
965 case sPermitRootLogin:
966 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +1000967 multistate_ptr = multistate_permitrootlogin;
968 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +0000969
970 case sIgnoreRhosts:
971 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +1000972 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +0000973 arg = strdelim(&cp);
974 if (!arg || *arg == '\0')
975 fatal("%s line %d: missing yes/no argument.",
976 filename, linenum);
977 value = 0; /* silence compiler */
978 if (strcmp(arg, "yes") == 0)
979 value = 1;
980 else if (strcmp(arg, "no") == 0)
981 value = 0;
982 else
983 fatal("%s line %d: Bad yes/no argument: %s",
984 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +1000985 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000986 *intptr = value;
987 break;
988
989 case sIgnoreUserKnownHosts:
990 intptr = &options->ignore_user_known_hosts;
991 goto parse_flag;
992
Ben Lindstromade03f62001-12-06 18:22:17 +0000993 case sRhostsRSAAuthentication:
994 intptr = &options->rhosts_rsa_authentication;
995 goto parse_flag;
996
997 case sHostbasedAuthentication:
998 intptr = &options->hostbased_authentication;
999 goto parse_flag;
1000
1001 case sHostbasedUsesNameFromPacketOnly:
1002 intptr = &options->hostbased_uses_name_from_packet_only;
1003 goto parse_flag;
1004
1005 case sRSAAuthentication:
1006 intptr = &options->rsa_authentication;
1007 goto parse_flag;
1008
1009 case sPubkeyAuthentication:
1010 intptr = &options->pubkey_authentication;
1011 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001012
Ben Lindstromade03f62001-12-06 18:22:17 +00001013 case sKerberosAuthentication:
1014 intptr = &options->kerberos_authentication;
1015 goto parse_flag;
1016
1017 case sKerberosOrLocalPasswd:
1018 intptr = &options->kerberos_or_local_passwd;
1019 goto parse_flag;
1020
1021 case sKerberosTicketCleanup:
1022 intptr = &options->kerberos_ticket_cleanup;
1023 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001024
Darren Tucker22ef5082003-12-31 11:37:34 +11001025 case sKerberosGetAFSToken:
1026 intptr = &options->kerberos_get_afs_token;
1027 goto parse_flag;
1028
Darren Tucker0efd1552003-08-26 11:49:55 +10001029 case sGssAuthentication:
1030 intptr = &options->gss_authentication;
1031 goto parse_flag;
1032
1033 case sGssCleanupCreds:
1034 intptr = &options->gss_cleanup_creds;
1035 goto parse_flag;
1036
Ben Lindstromade03f62001-12-06 18:22:17 +00001037 case sPasswordAuthentication:
1038 intptr = &options->password_authentication;
1039 goto parse_flag;
1040
Damien Miller01ed2272008-11-05 16:20:46 +11001041 case sZeroKnowledgePasswordAuthentication:
1042 intptr = &options->zero_knowledge_password_authentication;
1043 goto parse_flag;
1044
Ben Lindstromade03f62001-12-06 18:22:17 +00001045 case sKbdInteractiveAuthentication:
1046 intptr = &options->kbd_interactive_authentication;
1047 goto parse_flag;
1048
1049 case sChallengeResponseAuthentication:
1050 intptr = &options->challenge_response_authentication;
1051 goto parse_flag;
1052
1053 case sPrintMotd:
1054 intptr = &options->print_motd;
1055 goto parse_flag;
1056
1057 case sPrintLastLog:
1058 intptr = &options->print_lastlog;
1059 goto parse_flag;
1060
1061 case sX11Forwarding:
1062 intptr = &options->x11_forwarding;
1063 goto parse_flag;
1064
1065 case sX11DisplayOffset:
1066 intptr = &options->x11_display_offset;
1067 goto parse_int;
1068
Damien Miller95c249f2002-02-05 12:11:34 +11001069 case sX11UseLocalhost:
1070 intptr = &options->x11_use_localhost;
1071 goto parse_flag;
1072
Ben Lindstromade03f62001-12-06 18:22:17 +00001073 case sXAuthLocation:
1074 charptr = &options->xauth_location;
1075 goto parse_filename;
1076
1077 case sStrictModes:
1078 intptr = &options->strict_modes;
1079 goto parse_flag;
1080
Damien Miller12c150e2003-12-17 16:31:10 +11001081 case sTCPKeepAlive:
1082 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001083 goto parse_flag;
1084
1085 case sEmptyPasswd:
1086 intptr = &options->permit_empty_passwd;
1087 goto parse_flag;
1088
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001089 case sPermitUserEnvironment:
1090 intptr = &options->permit_user_env;
1091 goto parse_flag;
1092
Ben Lindstromade03f62001-12-06 18:22:17 +00001093 case sUseLogin:
1094 intptr = &options->use_login;
1095 goto parse_flag;
1096
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001097 case sCompression:
1098 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001099 multistate_ptr = multistate_compression;
1100 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001101
Ben Lindstromade03f62001-12-06 18:22:17 +00001102 case sGatewayPorts:
1103 intptr = &options->gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001104 multistate_ptr = multistate_gatewayports;
1105 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001106
Damien Miller3a961dc2003-06-03 10:25:48 +10001107 case sUseDNS:
1108 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001109 goto parse_flag;
1110
1111 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001112 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001113 arg = strdelim(&cp);
1114 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001115 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001116 fatal("%.200s line %d: unsupported log facility '%s'",
1117 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001118 if (*log_facility_ptr == -1)
1119 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001120 break;
1121
1122 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001123 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001124 arg = strdelim(&cp);
1125 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001126 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001127 fatal("%.200s line %d: unsupported log level '%s'",
1128 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001129 if (*log_level_ptr == -1)
1130 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001131 break;
1132
1133 case sAllowTcpForwarding:
1134 intptr = &options->allow_tcp_forwarding;
1135 goto parse_flag;
1136
Damien Miller4f755cd2008-05-19 14:57:41 +10001137 case sAllowAgentForwarding:
1138 intptr = &options->allow_agent_forwarding;
1139 goto parse_flag;
1140
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001141 case sUsePrivilegeSeparation:
1142 intptr = &use_privsep;
Damien Miller69ff1df2011-06-23 08:30:03 +10001143 multistate_ptr = multistate_privsep;
1144 goto parse_multistate;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001145
Ben Lindstromade03f62001-12-06 18:22:17 +00001146 case sAllowUsers:
1147 while ((arg = strdelim(&cp)) && *arg != '\0') {
1148 if (options->num_allow_users >= MAX_ALLOW_USERS)
1149 fatal("%s line %d: too many allow users.",
1150 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001151 options->allow_users[options->num_allow_users++] =
1152 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001153 }
1154 break;
1155
1156 case sDenyUsers:
1157 while ((arg = strdelim(&cp)) && *arg != '\0') {
1158 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001159 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001160 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001161 options->deny_users[options->num_deny_users++] =
1162 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001163 }
1164 break;
1165
1166 case sAllowGroups:
1167 while ((arg = strdelim(&cp)) && *arg != '\0') {
1168 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1169 fatal("%s line %d: too many allow groups.",
1170 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001171 options->allow_groups[options->num_allow_groups++] =
1172 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001173 }
1174 break;
1175
1176 case sDenyGroups:
1177 while ((arg = strdelim(&cp)) && *arg != '\0') {
1178 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1179 fatal("%s line %d: too many deny groups.",
1180 filename, linenum);
1181 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
1182 }
1183 break;
1184
1185 case sCiphers:
1186 arg = strdelim(&cp);
1187 if (!arg || *arg == '\0')
1188 fatal("%s line %d: Missing argument.", filename, linenum);
1189 if (!ciphers_valid(arg))
1190 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1191 filename, linenum, arg ? arg : "<NONE>");
1192 if (options->ciphers == NULL)
1193 options->ciphers = xstrdup(arg);
1194 break;
1195
1196 case sMacs:
1197 arg = strdelim(&cp);
1198 if (!arg || *arg == '\0')
1199 fatal("%s line %d: Missing argument.", filename, linenum);
1200 if (!mac_valid(arg))
1201 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1202 filename, linenum, arg ? arg : "<NONE>");
1203 if (options->macs == NULL)
1204 options->macs = xstrdup(arg);
1205 break;
1206
Damien Millerd5f62bf2010-09-24 22:11:14 +10001207 case sKexAlgorithms:
1208 arg = strdelim(&cp);
1209 if (!arg || *arg == '\0')
1210 fatal("%s line %d: Missing argument.",
1211 filename, linenum);
1212 if (!kex_names_valid(arg))
1213 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1214 filename, linenum, arg ? arg : "<NONE>");
1215 if (options->kex_algorithms == NULL)
1216 options->kex_algorithms = xstrdup(arg);
1217 break;
1218
Ben Lindstromade03f62001-12-06 18:22:17 +00001219 case sProtocol:
1220 intptr = &options->protocol;
1221 arg = strdelim(&cp);
1222 if (!arg || *arg == '\0')
1223 fatal("%s line %d: Missing argument.", filename, linenum);
1224 value = proto_spec(arg);
1225 if (value == SSH_PROTO_UNKNOWN)
1226 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001227 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001228 if (*intptr == SSH_PROTO_UNKNOWN)
1229 *intptr = value;
1230 break;
1231
1232 case sSubsystem:
1233 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1234 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001235 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001236 }
1237 arg = strdelim(&cp);
1238 if (!arg || *arg == '\0')
1239 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001240 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001241 if (!*activep) {
1242 arg = strdelim(&cp);
1243 break;
1244 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001245 for (i = 0; i < options->num_subsystems; i++)
1246 if (strcmp(arg, options->subsystem_name[i]) == 0)
1247 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001248 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001249 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1250 arg = strdelim(&cp);
1251 if (!arg || *arg == '\0')
1252 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001253 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001254 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001255
1256 /* Collect arguments (separate to executable) */
1257 p = xstrdup(arg);
1258 len = strlen(p) + 1;
1259 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1260 len += 1 + strlen(arg);
1261 p = xrealloc(p, 1, len);
1262 strlcat(p, " ", len);
1263 strlcat(p, arg, len);
1264 }
1265 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001266 options->num_subsystems++;
1267 break;
1268
1269 case sMaxStartups:
1270 arg = strdelim(&cp);
1271 if (!arg || *arg == '\0')
1272 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001273 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001274 if ((n = sscanf(arg, "%d:%d:%d",
1275 &options->max_startups_begin,
1276 &options->max_startups_rate,
1277 &options->max_startups)) == 3) {
1278 if (options->max_startups_begin >
1279 options->max_startups ||
1280 options->max_startups_rate > 100 ||
1281 options->max_startups_rate < 1)
1282 fatal("%s line %d: Illegal MaxStartups spec.",
1283 filename, linenum);
1284 } else if (n != 1)
1285 fatal("%s line %d: Illegal MaxStartups spec.",
1286 filename, linenum);
1287 else
1288 options->max_startups = options->max_startups_begin;
1289 break;
1290
Darren Tucker89413db2004-05-24 10:36:23 +10001291 case sMaxAuthTries:
1292 intptr = &options->max_authtries;
1293 goto parse_int;
1294
Damien Miller7207f642008-05-19 15:34:50 +10001295 case sMaxSessions:
1296 intptr = &options->max_sessions;
1297 goto parse_int;
1298
Ben Lindstromade03f62001-12-06 18:22:17 +00001299 case sBanner:
1300 charptr = &options->banner;
1301 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001302
Ben Lindstromade03f62001-12-06 18:22:17 +00001303 /*
1304 * These options can contain %X options expanded at
1305 * connect time, so that you can specify paths like:
1306 *
1307 * AuthorizedKeysFile /etc/ssh_keys/%u
1308 */
1309 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001310 if (*activep && options->num_authkeys_files == 0) {
1311 while ((arg = strdelim(&cp)) && *arg != '\0') {
1312 if (options->num_authkeys_files >=
1313 MAX_AUTHKEYS_FILES)
1314 fatal("%s line %d: "
1315 "too many authorized keys files.",
1316 filename, linenum);
1317 options->authorized_keys_files[
1318 options->num_authkeys_files++] =
1319 tilde_expand_filename(arg, getuid());
1320 }
1321 }
1322 return 0;
1323
Damien Miller30da3442010-05-10 11:58:03 +10001324 case sAuthorizedPrincipalsFile:
1325 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001326 arg = strdelim(&cp);
1327 if (!arg || *arg == '\0')
1328 fatal("%s line %d: missing file name.",
1329 filename, linenum);
1330 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001331 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001332 /* increase optional counter */
1333 if (intptr != NULL)
1334 *intptr = *intptr + 1;
1335 }
1336 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001337
1338 case sClientAliveInterval:
1339 intptr = &options->client_alive_interval;
1340 goto parse_time;
1341
1342 case sClientAliveCountMax:
1343 intptr = &options->client_alive_count_max;
1344 goto parse_int;
1345
Darren Tucker46bc0752004-05-02 22:11:30 +10001346 case sAcceptEnv:
1347 while ((arg = strdelim(&cp)) && *arg != '\0') {
1348 if (strchr(arg, '=') != NULL)
1349 fatal("%s line %d: Invalid environment name.",
1350 filename, linenum);
1351 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1352 fatal("%s line %d: too many allow env.",
1353 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001354 if (!*activep)
1355 break;
Darren Tucker46bc0752004-05-02 22:11:30 +10001356 options->accept_env[options->num_accept_env++] =
1357 xstrdup(arg);
1358 }
1359 break;
1360
Damien Millerd27b9472005-12-13 19:29:02 +11001361 case sPermitTunnel:
1362 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001363 arg = strdelim(&cp);
1364 if (!arg || *arg == '\0')
1365 fatal("%s line %d: Missing yes/point-to-point/"
1366 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001367 value = -1;
1368 for (i = 0; tunmode_desc[i].val != -1; i++)
1369 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1370 value = tunmode_desc[i].val;
1371 break;
1372 }
1373 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001374 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1375 "no argument: %s", filename, linenum, arg);
1376 if (*intptr == -1)
1377 *intptr = value;
1378 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001379
Darren Tucker45150472006-07-12 22:34:17 +10001380 case sMatch:
1381 if (cmdline)
1382 fatal("Match directive not supported as a command-line "
1383 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001384 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10001385 if (value < 0)
1386 fatal("%s line %d: Bad Match condition", filename,
1387 linenum);
1388 *activep = value;
1389 break;
1390
Damien Miller9b439df2006-07-24 14:04:00 +10001391 case sPermitOpen:
1392 arg = strdelim(&cp);
1393 if (!arg || *arg == '\0')
1394 fatal("%s line %d: missing PermitOpen specification",
1395 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001396 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001397 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001398 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001399 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001400 options->num_permitted_opens = 0;
1401 }
Damien Miller9b439df2006-07-24 14:04:00 +10001402 break;
1403 }
Damien Millerc6081482012-04-22 11:18:53 +10001404 if (strcmp(arg, "none") == 0) {
1405 if (*activep && n == -1) {
1406 channel_clear_adm_permitted_opens();
1407 options->num_permitted_opens = 1;
1408 channel_disable_adm_local_opens();
1409 }
1410 break;
1411 }
Damien Millera29b95e2007-01-05 16:28:36 +11001412 if (*activep && n == -1)
1413 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001414 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1415 p = hpdelim(&arg);
1416 if (p == NULL)
1417 fatal("%s line %d: missing host in PermitOpen",
1418 filename, linenum);
1419 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001420 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001421 fatal("%s line %d: bad port number in "
1422 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001423 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001424 options->num_permitted_opens =
1425 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001426 }
Damien Miller9b439df2006-07-24 14:04:00 +10001427 break;
1428
Damien Millere2754432006-07-24 14:06:47 +10001429 case sForceCommand:
1430 if (cp == NULL)
1431 fatal("%.200s line %d: Missing argument.", filename,
1432 linenum);
1433 len = strspn(cp, WHITESPACE);
1434 if (*activep && options->adm_forced_command == NULL)
1435 options->adm_forced_command = xstrdup(cp + len);
1436 return 0;
1437
Damien Millerd8cb1f12008-02-10 22:40:12 +11001438 case sChrootDirectory:
1439 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001440
1441 arg = strdelim(&cp);
1442 if (!arg || *arg == '\0')
1443 fatal("%s line %d: missing file name.",
1444 filename, linenum);
1445 if (*activep && *charptr == NULL)
1446 *charptr = xstrdup(arg);
1447 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001448
Damien Miller1aed65e2010-03-04 21:53:35 +11001449 case sTrustedUserCAKeys:
1450 charptr = &options->trusted_user_ca_keys;
1451 goto parse_filename;
1452
1453 case sRevokedKeys:
1454 charptr = &options->revoked_keys_file;
1455 goto parse_filename;
1456
Damien Miller0dac6fb2010-11-20 15:19:38 +11001457 case sIPQoS:
1458 arg = strdelim(&cp);
1459 if ((value = parse_ipqos(arg)) == -1)
1460 fatal("%s line %d: Bad IPQoS value: %s",
1461 filename, linenum, arg);
1462 arg = strdelim(&cp);
1463 if (arg == NULL)
1464 value2 = value;
1465 else if ((value2 = parse_ipqos(arg)) == -1)
1466 fatal("%s line %d: Bad IPQoS value: %s",
1467 filename, linenum, arg);
1468 if (*activep) {
1469 options->ip_qos_interactive = value;
1470 options->ip_qos_bulk = value2;
1471 }
1472 break;
1473
Damien Miller23528812012-04-22 11:24:43 +10001474 case sVersionAddendum:
1475 if (cp == NULL)
1476 fatal("%.200s line %d: Missing argument.", filename,
1477 linenum);
1478 len = strspn(cp, WHITESPACE);
1479 if (*activep && options->version_addendum == NULL) {
1480 if (strcasecmp(cp + len, "none") == 0)
1481 options->version_addendum = xstrdup("");
1482 else if (strchr(cp + len, '\r') != NULL)
1483 fatal("%.200s line %d: Invalid argument",
1484 filename, linenum);
1485 else
1486 options->version_addendum = xstrdup(cp + len);
1487 }
1488 return 0;
1489
Ben Lindstromade03f62001-12-06 18:22:17 +00001490 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001491 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001492 filename, linenum, arg);
1493 while (arg)
1494 arg = strdelim(&cp);
1495 break;
1496
Damien Millerf9b3feb2003-05-16 11:38:32 +10001497 case sUnsupported:
1498 logit("%s line %d: Unsupported option %s",
1499 filename, linenum, arg);
1500 while (arg)
1501 arg = strdelim(&cp);
1502 break;
1503
Ben Lindstromade03f62001-12-06 18:22:17 +00001504 default:
1505 fatal("%s line %d: Missing handler for opcode %s (%d)",
1506 filename, linenum, arg, opcode);
1507 }
1508 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1509 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1510 filename, linenum, arg);
1511 return 0;
1512}
1513
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001514/* Reads the server configuration file. */
1515
Damien Miller4af51302000-04-16 11:18:38 +10001516void
Darren Tucker645ab752004-06-25 13:33:20 +10001517load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001518{
Darren Tucker645ab752004-06-25 13:33:20 +10001519 char line[1024], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001520 FILE *f;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001521
Darren Tucker645ab752004-06-25 13:33:20 +10001522 debug2("%s: filename %s", __func__, filename);
1523 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001524 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001525 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001526 }
Darren Tucker645ab752004-06-25 13:33:20 +10001527 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001528 while (fgets(line, sizeof(line), f)) {
Darren Tucker645ab752004-06-25 13:33:20 +10001529 /*
1530 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001531 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001532 * line numbers later for error messages
1533 */
1534 if ((cp = strchr(line, '#')) != NULL)
1535 memcpy(cp, "\n", 2);
1536 cp = line + strspn(line, " \t\r");
1537
1538 buffer_append(conf, cp, strlen(cp));
1539 }
1540 buffer_append(conf, "\0", 1);
1541 fclose(f);
1542 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1543}
1544
1545void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001546parse_server_match_config(ServerOptions *options,
1547 struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10001548{
Darren Tucker45150472006-07-12 22:34:17 +10001549 ServerOptions mo;
1550
1551 initialize_server_options(&mo);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001552 parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11001553 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001554}
1555
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001556int parse_server_match_testspec(struct connection_info *ci, char *spec)
1557{
1558 char *p;
1559
1560 while ((p = strsep(&spec, ",")) && *p != '\0') {
1561 if (strncmp(p, "addr=", 5) == 0) {
1562 ci->address = xstrdup(p + 5);
1563 } else if (strncmp(p, "host=", 5) == 0) {
1564 ci->host = xstrdup(p + 5);
1565 } else if (strncmp(p, "user=", 5) == 0) {
1566 ci->user = xstrdup(p + 5);
1567 } else if (strncmp(p, "laddr=", 6) == 0) {
1568 ci->laddress = xstrdup(p + 6);
1569 } else if (strncmp(p, "lport=", 6) == 0) {
1570 ci->lport = a2port(p + 6);
1571 if (ci->lport == -1) {
1572 fprintf(stderr, "Invalid port '%s' in test mode"
1573 " specification %s\n", p+6, p);
1574 return -1;
1575 }
1576 } else {
1577 fprintf(stderr, "Invalid test mode specification %s\n",
1578 p);
1579 return -1;
1580 }
1581 }
1582 return 0;
1583}
1584
1585/*
1586 * returns 1 for a complete spec, 0 for partial spec and -1 for an
1587 * empty spec.
1588 */
1589int server_match_spec_complete(struct connection_info *ci)
1590{
1591 if (ci->user && ci->host && ci->address)
1592 return 1; /* complete */
1593 if (!ci->user && !ci->host && !ci->address)
1594 return -1; /* empty */
1595 return 0; /* partial */
1596}
1597
Darren Tucker1629c072007-02-19 22:25:37 +11001598/* Helper macros */
1599#define M_CP_INTOPT(n) do {\
1600 if (src->n != -1) \
1601 dst->n = src->n; \
1602} while (0)
1603#define M_CP_STROPT(n) do {\
1604 if (src->n != NULL) { \
1605 if (dst->n != NULL) \
1606 xfree(dst->n); \
1607 dst->n = src->n; \
1608 } \
1609} while(0)
Damien Millerd8478b62011-05-29 21:39:36 +10001610#define M_CP_STRARRAYOPT(n, num_n) do {\
1611 if (src->num_n != 0) { \
1612 for (dst->num_n = 0; dst->num_n < src->num_n; dst->num_n++) \
1613 dst->n[dst->num_n] = xstrdup(src->n[dst->num_n]); \
1614 } \
1615} while(0)
Darren Tucker1629c072007-02-19 22:25:37 +11001616
1617/*
1618 * Copy any supported values that are set.
1619 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001620 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001621 * array values that are not used pre-authentication, because any that we
1622 * do use must be explictly sent in mm_getpwnamallow().
1623 */
Darren Tucker45150472006-07-12 22:34:17 +10001624void
Darren Tucker1629c072007-02-19 22:25:37 +11001625copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001626{
Darren Tucker1629c072007-02-19 22:25:37 +11001627 M_CP_INTOPT(password_authentication);
1628 M_CP_INTOPT(gss_authentication);
1629 M_CP_INTOPT(rsa_authentication);
1630 M_CP_INTOPT(pubkey_authentication);
1631 M_CP_INTOPT(kerberos_authentication);
1632 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001633 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001634 M_CP_INTOPT(kbd_interactive_authentication);
Damien Miller01ed2272008-11-05 16:20:46 +11001635 M_CP_INTOPT(zero_knowledge_password_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001636 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001637 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001638
1639 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001640 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001641 M_CP_INTOPT(permit_tun);
Darren Tucker1629c072007-02-19 22:25:37 +11001642 M_CP_INTOPT(gateway_ports);
1643 M_CP_INTOPT(x11_display_offset);
1644 M_CP_INTOPT(x11_forwarding);
1645 M_CP_INTOPT(x11_use_localhost);
Damien Miller7207f642008-05-19 15:34:50 +10001646 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001647 M_CP_INTOPT(max_authtries);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001648 M_CP_INTOPT(ip_qos_interactive);
1649 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker1629c072007-02-19 22:25:37 +11001650
Damien Millerf2e407e2011-05-20 19:04:14 +10001651 /* See comment in servconf.h */
1652 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10001653
Damien Millerc2411902011-05-20 19:03:49 +10001654 /*
1655 * The only things that should be below this point are string options
1656 * which are only used after authentication.
1657 */
Damien Miller5d74e582011-05-20 19:03:31 +10001658 if (preauth)
1659 return;
Damien Millerd8478b62011-05-29 21:39:36 +10001660
Damien Miller5d74e582011-05-20 19:03:31 +10001661 M_CP_STROPT(adm_forced_command);
1662 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001663}
1664
Darren Tucker1629c072007-02-19 22:25:37 +11001665#undef M_CP_INTOPT
1666#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10001667#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11001668
Darren Tucker45150472006-07-12 22:34:17 +10001669void
1670parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001671 struct connection_info *connectinfo)
Darren Tucker45150472006-07-12 22:34:17 +10001672{
1673 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001674 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001675
1676 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1677
Darren Tucker9fbac712004-08-12 22:41:44 +10001678 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001679 active = connectinfo ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001680 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001681 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001682 if (process_server_config_line(options, cp, filename,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001683 linenum++, &active, connectinfo) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001684 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001685 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001686 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001687 if (bad_options > 0)
1688 fatal("%s: terminating, %d bad configuration options",
1689 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001690}
Darren Tuckere7140f22008-06-10 23:01:51 +10001691
1692static const char *
Damien Miller82c55872011-06-23 08:20:30 +10001693fmt_multistate_int(int val, const struct multistate *m)
1694{
1695 u_int i;
1696
1697 for (i = 0; m[i].key != NULL; i++) {
1698 if (m[i].value == val)
1699 return m[i].key;
1700 }
1701 return "UNKNOWN";
1702}
1703
1704static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10001705fmt_intarg(ServerOpCodes code, int val)
1706{
Damien Miller82c55872011-06-23 08:20:30 +10001707 if (val == -1)
1708 return "unset";
1709 switch (code) {
1710 case sAddressFamily:
1711 return fmt_multistate_int(val, multistate_addressfamily);
1712 case sPermitRootLogin:
1713 return fmt_multistate_int(val, multistate_permitrootlogin);
1714 case sGatewayPorts:
1715 return fmt_multistate_int(val, multistate_gatewayports);
1716 case sCompression:
1717 return fmt_multistate_int(val, multistate_compression);
Damien Miller69ff1df2011-06-23 08:30:03 +10001718 case sUsePrivilegeSeparation:
1719 return fmt_multistate_int(val, multistate_privsep);
Damien Miller82c55872011-06-23 08:20:30 +10001720 case sProtocol:
Darren Tuckere7140f22008-06-10 23:01:51 +10001721 switch (val) {
1722 case SSH_PROTO_1:
1723 return "1";
1724 case SSH_PROTO_2:
1725 return "2";
1726 case (SSH_PROTO_1|SSH_PROTO_2):
1727 return "2,1";
1728 default:
1729 return "UNKNOWN";
1730 }
Damien Miller82c55872011-06-23 08:20:30 +10001731 default:
1732 switch (val) {
1733 case 0:
1734 return "no";
1735 case 1:
1736 return "yes";
1737 default:
1738 return "UNKNOWN";
1739 }
Darren Tuckere7140f22008-06-10 23:01:51 +10001740 }
Darren Tuckere7140f22008-06-10 23:01:51 +10001741}
1742
1743static const char *
1744lookup_opcode_name(ServerOpCodes code)
1745{
1746 u_int i;
1747
1748 for (i = 0; keywords[i].name != NULL; i++)
1749 if (keywords[i].opcode == code)
1750 return(keywords[i].name);
1751 return "UNKNOWN";
1752}
1753
1754static void
1755dump_cfg_int(ServerOpCodes code, int val)
1756{
1757 printf("%s %d\n", lookup_opcode_name(code), val);
1758}
1759
1760static void
1761dump_cfg_fmtint(ServerOpCodes code, int val)
1762{
1763 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
1764}
1765
1766static void
1767dump_cfg_string(ServerOpCodes code, const char *val)
1768{
1769 if (val == NULL)
1770 return;
1771 printf("%s %s\n", lookup_opcode_name(code), val);
1772}
1773
1774static void
1775dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
1776{
1777 u_int i;
1778
1779 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10001780 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
1781}
1782
1783static void
1784dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
1785{
1786 u_int i;
1787
1788 printf("%s", lookup_opcode_name(code));
1789 for (i = 0; i < count; i++)
1790 printf(" %s", vals[i]);
1791 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10001792}
1793
1794void
1795dump_config(ServerOptions *o)
1796{
1797 u_int i;
1798 int ret;
1799 struct addrinfo *ai;
1800 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
1801
1802 /* these are usually at the top of the config */
1803 for (i = 0; i < o->num_ports; i++)
1804 printf("port %d\n", o->ports[i]);
1805 dump_cfg_fmtint(sProtocol, o->protocol);
1806 dump_cfg_fmtint(sAddressFamily, o->address_family);
1807
1808 /* ListenAddress must be after Port */
1809 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
1810 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1811 sizeof(addr), port, sizeof(port),
1812 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1813 error("getnameinfo failed: %.100s",
1814 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
1815 strerror(errno));
1816 } else {
1817 if (ai->ai_family == AF_INET6)
1818 printf("listenaddress [%s]:%s\n", addr, port);
1819 else
1820 printf("listenaddress %s:%s\n", addr, port);
1821 }
1822 }
1823
1824 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10001825#ifdef USE_PAM
1826 dump_cfg_int(sUsePAM, o->use_pam);
1827#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001828 dump_cfg_int(sServerKeyBits, o->server_key_bits);
1829 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
1830 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
1831 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
1832 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11001833 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10001834 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
1835 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
1836
1837 /* formatted integer arguments */
1838 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
1839 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
1840 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
1841 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
1842 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
1843 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
1844 o->hostbased_uses_name_from_packet_only);
1845 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
1846 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10001847#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10001848 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
1849 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
1850 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10001851# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10001852 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10001853# endif
1854#endif
1855#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10001856 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
1857 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10001858#endif
Damien Miller01ed2272008-11-05 16:20:46 +11001859#ifdef JPAKE
1860 dump_cfg_fmtint(sZeroKnowledgePasswordAuthentication,
1861 o->zero_knowledge_password_authentication);
1862#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001863 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
1864 dump_cfg_fmtint(sKbdInteractiveAuthentication,
1865 o->kbd_interactive_authentication);
1866 dump_cfg_fmtint(sChallengeResponseAuthentication,
1867 o->challenge_response_authentication);
1868 dump_cfg_fmtint(sPrintMotd, o->print_motd);
1869 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
1870 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
1871 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
1872 dump_cfg_fmtint(sStrictModes, o->strict_modes);
1873 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
1874 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
1875 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
1876 dump_cfg_fmtint(sUseLogin, o->use_login);
1877 dump_cfg_fmtint(sCompression, o->compression);
1878 dump_cfg_fmtint(sGatewayPorts, o->gateway_ports);
1879 dump_cfg_fmtint(sUseDNS, o->use_dns);
1880 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
1881 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
1882
1883 /* string arguments */
1884 dump_cfg_string(sPidFile, o->pid_file);
1885 dump_cfg_string(sXAuthLocation, o->xauth_location);
1886 dump_cfg_string(sCiphers, o->ciphers);
1887 dump_cfg_string(sMacs, o->macs);
1888 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10001889 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11001890 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11001891 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
1892 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10001893 dump_cfg_string(sAuthorizedPrincipalsFile,
1894 o->authorized_principals_file);
Damien Miller23528812012-04-22 11:24:43 +10001895 dump_cfg_string(sVersionAddendum, o->version_addendum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001896
1897 /* string arguments requiring a lookup */
1898 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
1899 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
1900
1901 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10001902 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
1903 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10001904 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
1905 o->host_key_files);
Damien Miller0a80ca12010-02-27 07:55:05 +11001906 dump_cfg_strarray(sHostKeyFile, o->num_host_cert_files,
1907 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10001908 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
1909 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
1910 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
1911 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
1912 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
1913
1914 /* other arguments */
1915 for (i = 0; i < o->num_subsystems; i++)
1916 printf("subsystem %s %s\n", o->subsystem_name[i],
1917 o->subsystem_args[i]);
1918
1919 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
1920 o->max_startups_rate, o->max_startups);
1921
1922 for (i = 0; tunmode_desc[i].val != -1; i++)
1923 if (tunmode_desc[i].val == o->permit_tun) {
1924 s = tunmode_desc[i].text;
1925 break;
1926 }
1927 dump_cfg_string(sPermitTunnel, s);
1928
Damien Miller91475862011-05-05 14:14:34 +10001929 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
1930 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11001931
Darren Tuckere7140f22008-06-10 23:01:51 +10001932 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10001933}