blob: 4e5fd2f048cd90dca1ee5025cc272726c665ed72 [file] [log] [blame]
Damien Millerc54b02c2010-10-07 21:40:17 +11001/* $OpenBSD: servconf.c,v 1.212 2010/09/30 11:04:51 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10005 *
Damien Millere4340be2000-09-16 13:29:08 +11006 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110011 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100012
13#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
Damien Millere3b60b52006-07-10 21:08:03 +100015#include <sys/types.h>
16#include <sys/socket.h>
17
Damien Millerb8fe89c2006-07-24 14:51:00 +100018#include <netdb.h>
Damien Miller565ca3f2006-08-19 00:23:15 +100019#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100020#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100021#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100022#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100023#include <signal.h>
Damien Millere6b3b612006-07-24 14:01:23 +100024#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100025#include <stdarg.h>
Darren Tuckere7140f22008-06-10 23:01:51 +100026#include <errno.h>
Damien Millerbe43ebf2006-07-24 13:51:51 +100027
Damien Millerb84886b2008-05-19 15:05:07 +100028#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100029#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000031#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100032#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033#include "servconf.h"
Damien Miller78928792000-04-12 20:17:38 +100034#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036#include "misc.h"
37#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000039#include "kex.h"
40#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100041#include "match.h"
Damien Miller9b439df2006-07-24 14:04:00 +100042#include "channels.h"
Damien Miller565ca3f2006-08-19 00:23:15 +100043#include "groupaccess.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044
Damien Miller3dc71ad2009-01-28 16:31:22 +110045static void add_listen_addr(ServerOptions *, char *, int);
46static void add_one_listen_addr(ServerOptions *, char *, int);
Damien Miller34132e52000-01-14 15:45:46 +110047
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000048/* Use of privilege separation or not */
49extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100050extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052/* Initializes the server options to their default values. */
53
Damien Miller4af51302000-04-16 11:18:38 +100054void
Damien Miller95def091999-11-25 00:26:21 +110055initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056{
Damien Miller95def091999-11-25 00:26:21 +110057 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110058
59 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100060 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110061
62 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110063 options->num_ports = 0;
64 options->ports_from_cmdline = 0;
65 options->listen_addrs = NULL;
Darren Tucker0f383232005-01-20 10:57:56 +110066 options->address_family = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110067 options->num_host_key_files = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110068 options->num_host_cert_files = 0;
Damien Miller6f83b8e2000-05-02 09:23:45 +100069 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110070 options->server_key_bits = -1;
71 options->login_grace_time = -1;
72 options->key_regeneration_time = -1;
Ben Lindstromd8a90212001-02-15 03:08:27 +000073 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110074 options->ignore_rhosts = -1;
75 options->ignore_user_known_hosts = -1;
76 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +000077 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +110078 options->x11_forwarding = -1;
79 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +110080 options->x11_use_localhost = -1;
Damien Millerd3a18572000-06-07 19:55:44 +100081 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +110082 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +110083 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +110084 options->log_facility = SYSLOG_FACILITY_NOT_SET;
85 options->log_level = SYSLOG_LEVEL_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110086 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +000087 options->hostbased_authentication = -1;
88 options->hostbased_uses_name_from_packet_only = -1;
Damien Miller95def091999-11-25 00:26:21 +110089 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110090 options->pubkey_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +110091 options->kerberos_authentication = -1;
92 options->kerberos_or_local_passwd = -1;
93 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +110094 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +100095 options->gss_authentication=-1;
96 options->gss_cleanup_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +110097 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +110098 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +000099 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100100 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000101 options->permit_user_env = -1;
Damien Miller95def091999-11-25 00:26:21 +1100102 options->use_login = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000103 options->compression = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100104 options->allow_tcp_forwarding = -1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000105 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100106 options->num_allow_users = 0;
107 options->num_deny_users = 0;
108 options->num_allow_groups = 0;
109 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000110 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000111 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000112 options->kex_algorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000113 options->protocol = SSH_PROTO_UNKNOWN;
Damien Millere247cc42000-05-07 12:03:14 +1000114 options->gateway_ports = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000115 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000116 options->max_startups_begin = -1;
117 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000118 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000119 options->max_authtries = -1;
Damien Miller7207f642008-05-19 15:34:50 +1000120 options->max_sessions = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000121 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000122 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000123 options->client_alive_interval = -1;
124 options->client_alive_count_max = -1;
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000125 options->authorized_keys_file = NULL;
126 options->authorized_keys_file2 = NULL;
Darren Tucker46bc0752004-05-02 22:11:30 +1000127 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100128 options->permit_tun = -1;
Damien Millera765cf42006-07-24 14:08:13 +1000129 options->num_permitted_opens = -1;
Damien Millere2754432006-07-24 14:06:47 +1000130 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100131 options->chroot_directory = NULL;
Damien Miller01ed2272008-11-05 16:20:46 +1100132 options->zero_knowledge_password_authentication = -1;
Damien Miller1aed65e2010-03-04 21:53:35 +1100133 options->revoked_keys_file = NULL;
134 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000135 options->authorized_principals_file = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136}
137
Damien Miller4af51302000-04-16 11:18:38 +1000138void
Damien Miller95def091999-11-25 00:26:21 +1100139fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140{
Damien Miller726273e2001-11-12 11:40:11 +1100141 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000142 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000143 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100144
145 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100146 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +1100147 options->protocol = SSH_PROTO_2;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100148 if (options->num_host_key_files == 0) {
149 /* fill default hostkeys for protocols */
150 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100151 options->host_key_files[options->num_host_key_files++] =
152 _PATH_HOST_KEY_FILE;
153 if (options->protocol & SSH_PROTO_2) {
154 options->host_key_files[options->num_host_key_files++] =
155 _PATH_HOST_RSA_KEY_FILE;
156 options->host_key_files[options->num_host_key_files++] =
157 _PATH_HOST_DSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100158#ifdef OPENSSL_HAS_ECC
Damien Millere13cadf2010-09-10 11:15:33 +1000159 options->host_key_files[options->num_host_key_files++] =
160 _PATH_HOST_ECDSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100161#endif
Damien Miller7fc23732002-01-22 23:19:11 +1100162 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100163 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100164 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100165 if (options->num_ports == 0)
166 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
167 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000168 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000169 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000170 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100171 if (options->server_key_bits == -1)
Darren Tucker7499b0c2008-07-02 22:35:43 +1000172 options->server_key_bits = 1024;
Damien Miller95def091999-11-25 00:26:21 +1100173 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000174 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100175 if (options->key_regeneration_time == -1)
176 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000177 if (options->permit_root_login == PERMIT_NOT_SET)
178 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100179 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100180 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100181 if (options->ignore_user_known_hosts == -1)
182 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100183 if (options->print_motd == -1)
184 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000185 if (options->print_lastlog == -1)
186 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100187 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100188 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100189 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100190 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100191 if (options->x11_use_localhost == -1)
192 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000193 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000194 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100195 if (options->strict_modes == -1)
196 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100197 if (options->tcp_keep_alive == -1)
198 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100199 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100200 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100201 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000202 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100203 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100204 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000205 if (options->hostbased_authentication == -1)
206 options->hostbased_authentication = 0;
207 if (options->hostbased_uses_name_from_packet_only == -1)
208 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100209 if (options->rsa_authentication == -1)
210 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100211 if (options->pubkey_authentication == -1)
212 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100213 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000214 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100215 if (options->kerberos_or_local_passwd == -1)
216 options->kerberos_or_local_passwd = 1;
217 if (options->kerberos_ticket_cleanup == -1)
218 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100219 if (options->kerberos_get_afs_token == -1)
220 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000221 if (options->gss_authentication == -1)
222 options->gss_authentication = 0;
223 if (options->gss_cleanup_creds == -1)
224 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100225 if (options->password_authentication == -1)
226 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100227 if (options->kbd_interactive_authentication == -1)
228 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000229 if (options->challenge_response_authentication == -1)
230 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100231 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100232 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000233 if (options->permit_user_env == -1)
234 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100235 if (options->use_login == -1)
236 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000237 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000238 options->compression = COMP_DELAYED;
Damien Miller50a41ed2000-10-16 12:14:42 +1100239 if (options->allow_tcp_forwarding == -1)
240 options->allow_tcp_forwarding = 1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000241 if (options->allow_agent_forwarding == -1)
242 options->allow_agent_forwarding = 1;
Damien Millere247cc42000-05-07 12:03:14 +1000243 if (options->gateway_ports == -1)
244 options->gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000245 if (options->max_startups == -1)
246 options->max_startups = 10;
Damien Miller942da032000-08-18 13:59:06 +1000247 if (options->max_startups_rate == -1)
248 options->max_startups_rate = 100; /* 100% */
249 if (options->max_startups_begin == -1)
250 options->max_startups_begin = options->max_startups;
Darren Tucker89413db2004-05-24 10:36:23 +1000251 if (options->max_authtries == -1)
252 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000253 if (options->max_sessions == -1)
254 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000255 if (options->use_dns == -1)
256 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000257 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100258 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000259 if (options->client_alive_count_max == -1)
260 options->client_alive_count_max = 3;
Damien Miller75413ac2001-11-12 11:14:35 +1100261 if (options->authorized_keys_file2 == NULL) {
262 /* authorized_keys_file2 falls back to authorized_keys_file */
263 if (options->authorized_keys_file != NULL)
Damien Millerc54b02c2010-10-07 21:40:17 +1100264 options->authorized_keys_file2 = xstrdup(options->authorized_keys_file);
Damien Miller75413ac2001-11-12 11:14:35 +1100265 else
Damien Millerc54b02c2010-10-07 21:40:17 +1100266 options->authorized_keys_file2 = xstrdup(_PATH_SSH_USER_PERMITTED_KEYS2);
Damien Miller75413ac2001-11-12 11:14:35 +1100267 }
268 if (options->authorized_keys_file == NULL)
Damien Millerc54b02c2010-10-07 21:40:17 +1100269 options->authorized_keys_file = xstrdup(_PATH_SSH_USER_PERMITTED_KEYS);
Damien Millerd27b9472005-12-13 19:29:02 +1100270 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100271 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller01ed2272008-11-05 16:20:46 +1100272 if (options->zero_knowledge_password_authentication == -1)
273 options->zero_knowledge_password_authentication = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000274
Ben Lindstromfb62a692002-06-06 19:47:11 +0000275 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000276 if (use_privsep == -1)
Ben Lindstromfb62a692002-06-06 19:47:11 +0000277 use_privsep = 1;
Damien Miller4903eb42002-06-21 16:20:44 +1000278
Tim Rice40017b02002-07-14 13:36:49 -0700279#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000280 if (use_privsep && options->compression == 1) {
281 error("This platform does not support both privilege "
282 "separation and compression");
283 error("Compression disabled");
284 options->compression = 0;
285 }
286#endif
287
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288}
289
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100291typedef enum {
292 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100293 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000294 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100295 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100296 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
297 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000298 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100299 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100300 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000301 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100302 sPasswordAuthentication, sKbdInteractiveAuthentication,
303 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000304 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100305 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller12c150e2003-12-17 16:31:10 +1100306 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000307 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Damien Miller50a41ed2000-10-16 12:14:42 +1100308 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000309 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000310 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
Damien Miller7207f642008-05-19 15:34:50 +1000311 sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000312 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100313 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000314 sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
Damien Millerd27b9472005-12-13 19:29:02 +1100315 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100316 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100317 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller0a80ca12010-02-27 07:55:05 +1100318 sZeroKnowledgePasswordAuthentication, sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000319 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Millerd5f62bf2010-09-24 22:11:14 +1000320 sKexAlgorithms,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000321 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322} ServerOpCodes;
323
Darren Tucker45150472006-07-12 22:34:17 +1000324#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
325#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
326#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
327
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100329static struct {
330 const char *name;
331 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000332 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100333} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100334 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000335#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000336 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000337#else
Darren Tucker45150472006-07-12 22:34:17 +1000338 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000339#endif
Darren Tucker45150472006-07-12 22:34:17 +1000340 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100341 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000342 { "port", sPort, SSHCFG_GLOBAL },
343 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
344 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
345 { "pidfile", sPidFile, SSHCFG_GLOBAL },
346 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
347 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
348 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100349 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000350 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
351 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
352 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100353 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
354 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000355 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100356 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
357 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000358 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000359#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100360 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000361 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
362 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100363#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000364 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000365#else
Darren Tucker45150472006-07-12 22:34:17 +1000366 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100367#endif
368#else
Darren Tucker1629c072007-02-19 22:25:37 +1100369 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000370 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
371 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
372 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000373#endif
Darren Tucker45150472006-07-12 22:34:17 +1000374 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
375 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000376#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100377 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000378 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000379#else
Darren Tucker1629c072007-02-19 22:25:37 +1100380 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000381 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000382#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100383 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
384 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100385 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000386 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
Damien Miller01ed2272008-11-05 16:20:46 +1100387#ifdef JPAKE
388 { "zeroknowledgepasswordauthentication", sZeroKnowledgePasswordAuthentication, SSHCFG_ALL },
389#else
390 { "zeroknowledgepasswordauthentication", sUnsupported, SSHCFG_ALL },
391#endif
Darren Tucker45150472006-07-12 22:34:17 +1000392 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
393 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
394 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
395 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
396 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
397 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
398 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000399 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
400 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
401 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000402 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
403 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100404 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000405 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
406 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
407 { "compression", sCompression, SSHCFG_GLOBAL },
408 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
409 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
410 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000411 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000412 { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
413 { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
414 { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
415 { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
416 { "ciphers", sCiphers, SSHCFG_GLOBAL },
417 { "macs", sMacs, SSHCFG_GLOBAL },
418 { "protocol", sProtocol, SSHCFG_GLOBAL },
419 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
420 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
421 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000422 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000423 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100424 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000425 { "usedns", sUseDNS, SSHCFG_GLOBAL },
426 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
427 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
428 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
429 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000430 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
431 { "authorizedkeysfile2", sAuthorizedKeysFile2, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000432 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Darren Tucker45150472006-07-12 22:34:17 +1000433 { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000434 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000435 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000436 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000437 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100438 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100439 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100440 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
441 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000442 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000443 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000444 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445};
446
Darren Tuckere7140f22008-06-10 23:01:51 +1000447static struct {
448 int val;
449 char *text;
450} tunmode_desc[] = {
451 { SSH_TUNMODE_NO, "no" },
452 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
453 { SSH_TUNMODE_ETHERNET, "ethernet" },
454 { SSH_TUNMODE_YES, "yes" },
455 { -1, NULL }
456};
457
Damien Miller5428f641999-11-25 11:54:57 +1100458/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000459 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100460 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461
Damien Miller4af51302000-04-16 11:18:38 +1000462static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100463parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000464 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000466 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467
Damien Miller95def091999-11-25 00:26:21 +1100468 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000469 if (strcasecmp(cp, keywords[i].name) == 0) {
470 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100471 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000472 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000474 error("%s: line %d: Bad configuration option: %s",
475 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100476 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477}
478
Darren Tucker88b6fb22010-01-13 22:44:29 +1100479char *
480derelativise_path(const char *path)
481{
Damien Miller44451d02010-03-26 10:40:04 +1100482 char *expanded, *ret, cwd[MAXPATHLEN];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100483
484 expanded = tilde_expand_filename(path, getuid());
485 if (*expanded == '/')
486 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100487 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100488 fatal("%s: getcwd: %s", __func__, strerror(errno));
489 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100490 xfree(expanded);
491 return ret;
492}
493
Ben Lindstrombba81212001-06-25 05:01:22 +0000494static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100495add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100496{
Damien Millereccb9de2005-06-17 12:59:34 +1000497 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100498
499 if (options->num_ports == 0)
500 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100501 if (options->address_family == -1)
502 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000503 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000504 for (i = 0; i < options->num_ports; i++)
505 add_one_listen_addr(options, addr, options->ports[i]);
506 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000507 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000508}
509
Ben Lindstrombba81212001-06-25 05:01:22 +0000510static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100511add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000512{
513 struct addrinfo hints, *ai, *aitop;
514 char strport[NI_MAXSERV];
515 int gaierr;
516
517 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100518 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000519 hints.ai_socktype = SOCK_STREAM;
520 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100521 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000522 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
523 fatal("bad addr or host: %s (%s)",
524 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100525 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000526 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
527 ;
528 ai->ai_next = options->listen_addrs;
529 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100530}
531
Darren Tucker45150472006-07-12 22:34:17 +1000532/*
533 * The strategy for the Match blocks is that the config file is parsed twice.
534 *
535 * The first time is at startup. activep is initialized to 1 and the
536 * directives in the global context are processed and acted on. Hitting a
537 * Match directive unsets activep and the directives inside the block are
538 * checked for syntax only.
539 *
540 * The second time is after a connection has been established but before
541 * authentication. activep is initialized to 2 and global config directives
542 * are ignored since they have already been processed. If the criteria in a
543 * Match block is met, activep is set and the subsequent directives
544 * processed and actioned until EOF or another Match block unsets it. Any
545 * options set are copied into the main server config.
546 *
547 * Potential additions/improvements:
548 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
549 *
550 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
551 * Match Address 192.168.0.*
552 * Tag trusted
553 * Match Group wheel
554 * Tag trusted
555 * Match Tag trusted
556 * AllowTcpForwarding yes
557 * GatewayPorts clientspecified
558 * [...]
559 *
560 * - Add a PermittedChannelRequests directive
561 * Match Group shell
562 * PermittedChannelRequests session,forwarded-tcpip
563 */
564
565static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000566match_cfg_line_group(const char *grps, int line, const char *user)
567{
568 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000569 struct passwd *pw;
570
Damien Miller565ca3f2006-08-19 00:23:15 +1000571 if (user == NULL)
572 goto out;
573
574 if ((pw = getpwnam(user)) == NULL) {
575 debug("Can't match group at line %d because user %.100s does "
576 "not exist", line, user);
577 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
578 debug("Can't Match group because user %.100s not in any group "
579 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000580 } else if (ga_match_pattern_list(grps) != 1) {
581 debug("user %.100s does not match group list %.100s at line %d",
582 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000583 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000584 debug("user %.100s matched group list %.100s at line %d", user,
585 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000586 result = 1;
587 }
588out:
589 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000590 return result;
591}
592
593static int
Darren Tucker45150472006-07-12 22:34:17 +1000594match_cfg_line(char **condition, int line, const char *user, const char *host,
595 const char *address)
596{
597 int result = 1;
598 char *arg, *attrib, *cp = *condition;
599 size_t len;
600
601 if (user == NULL)
602 debug3("checking syntax for 'Match %s'", cp);
603 else
604 debug3("checking match for '%s' user %s host %s addr %s", cp,
605 user ? user : "(null)", host ? host : "(null)",
606 address ? address : "(null)");
607
608 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
609 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
610 error("Missing Match criteria for %s", attrib);
611 return -1;
612 }
613 len = strlen(arg);
614 if (strcasecmp(attrib, "user") == 0) {
615 if (!user) {
616 result = 0;
617 continue;
618 }
619 if (match_pattern_list(user, arg, len, 0) != 1)
620 result = 0;
621 else
622 debug("user %.100s matched 'User %.100s' at "
623 "line %d", user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000624 } else if (strcasecmp(attrib, "group") == 0) {
625 switch (match_cfg_line_group(arg, line, user)) {
626 case -1:
627 return -1;
628 case 0:
629 result = 0;
630 }
Darren Tucker45150472006-07-12 22:34:17 +1000631 } else if (strcasecmp(attrib, "host") == 0) {
632 if (!host) {
633 result = 0;
634 continue;
635 }
636 if (match_hostname(host, arg, len) != 1)
637 result = 0;
638 else
639 debug("connection from %.100s matched 'Host "
640 "%.100s' at line %d", host, arg, line);
641 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000642 switch (addr_match_list(address, arg)) {
643 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000644 debug("connection from %.100s matched 'Address "
645 "%.100s' at line %d", address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000646 break;
647 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000648 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000649 result = 0;
650 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000651 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000652 return -1;
653 }
Darren Tucker45150472006-07-12 22:34:17 +1000654 } else {
655 error("Unsupported Match attribute %s", attrib);
656 return -1;
657 }
658 }
659 if (user != NULL)
660 debug3("match %sfound", result ? "" : "not ");
661 *condition = cp;
662 return result;
663}
664
Damien Millere2754432006-07-24 14:06:47 +1000665#define WHITESPACE " \t\r\n"
666
Ben Lindstromade03f62001-12-06 18:22:17 +0000667int
668process_server_config_line(ServerOptions *options, char *line,
Darren Tucker45150472006-07-12 22:34:17 +1000669 const char *filename, int linenum, int *activep, const char *user,
670 const char *host, const char *address)
Ben Lindstromade03f62001-12-06 18:22:17 +0000671{
672 char *cp, **charptr, *arg, *p;
Darren Tucker45150472006-07-12 22:34:17 +1000673 int cmdline = 0, *intptr, value, n;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100674 SyslogFacility *log_facility_ptr;
675 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000676 ServerOpCodes opcode;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100677 int port;
Darren Tucker45150472006-07-12 22:34:17 +1000678 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000679 size_t len;
Ben Lindstromade03f62001-12-06 18:22:17 +0000680
681 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100682 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100683 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000684 /* Ignore leading whitespace */
685 if (*arg == '\0')
686 arg = strdelim(&cp);
687 if (!arg || !*arg || *arg == '#')
688 return 0;
689 intptr = NULL;
690 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000691 opcode = parse_token(arg, filename, linenum, &flags);
692
693 if (activep == NULL) { /* We are processing a command line directive */
694 cmdline = 1;
695 activep = &cmdline;
696 }
697 if (*activep && opcode != sMatch)
698 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
699 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
700 if (user == NULL) {
701 fatal("%s line %d: Directive '%s' is not allowed "
702 "within a Match block", filename, linenum, arg);
703 } else { /* this is a directive we have already processed */
704 while (arg)
705 arg = strdelim(&cp);
706 return 0;
707 }
708 }
709
Ben Lindstromade03f62001-12-06 18:22:17 +0000710 switch (opcode) {
711 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000712 case sUsePAM:
713 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000714 goto parse_flag;
715
716 /* Standard Options */
717 case sBadOption:
718 return -1;
719 case sPort:
720 /* ignore ports from configfile if cmdline specifies ports */
721 if (options->ports_from_cmdline)
722 return 0;
723 if (options->listen_addrs != NULL)
724 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100725 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000726 if (options->num_ports >= MAX_PORTS)
727 fatal("%s line %d: too many ports.",
728 filename, linenum);
729 arg = strdelim(&cp);
730 if (!arg || *arg == '\0')
731 fatal("%s line %d: missing port number.",
732 filename, linenum);
733 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100734 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000735 fatal("%s line %d: Badly formatted port number.",
736 filename, linenum);
737 break;
738
739 case sServerKeyBits:
740 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +1000741 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +0000742 arg = strdelim(&cp);
743 if (!arg || *arg == '\0')
744 fatal("%s line %d: missing integer value.",
745 filename, linenum);
746 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000747 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000748 *intptr = value;
749 break;
750
751 case sLoginGraceTime:
752 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +1000753 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +0000754 arg = strdelim(&cp);
755 if (!arg || *arg == '\0')
756 fatal("%s line %d: missing time value.",
757 filename, linenum);
758 if ((value = convtime(arg)) == -1)
759 fatal("%s line %d: invalid time value.",
760 filename, linenum);
761 if (*intptr == -1)
762 *intptr = value;
763 break;
764
765 case sKeyRegenerationTime:
766 intptr = &options->key_regeneration_time;
767 goto parse_time;
768
769 case sListenAddress:
770 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100771 if (arg == NULL || *arg == '\0')
772 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000773 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000774 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
775 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
776 && strchr(p+1, ':') != NULL) {
777 add_listen_addr(options, arg, 0);
778 break;
779 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100780 p = hpdelim(&arg);
781 if (p == NULL)
782 fatal("%s line %d: bad address:port usage",
783 filename, linenum);
784 p = cleanhostname(p);
785 if (arg == NULL)
786 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100787 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100788 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000789
Damien Millerf91ee4c2005-03-01 21:24:33 +1100790 add_listen_addr(options, p, port);
791
Ben Lindstromade03f62001-12-06 18:22:17 +0000792 break;
793
Darren Tucker0f383232005-01-20 10:57:56 +1100794 case sAddressFamily:
795 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000796 if (!arg || *arg == '\0')
797 fatal("%s line %d: missing address family.",
798 filename, linenum);
Darren Tucker0f383232005-01-20 10:57:56 +1100799 intptr = &options->address_family;
800 if (options->listen_addrs != NULL)
801 fatal("%s line %d: address family must be specified before "
802 "ListenAddress.", filename, linenum);
803 if (strcasecmp(arg, "inet") == 0)
804 value = AF_INET;
805 else if (strcasecmp(arg, "inet6") == 0)
806 value = AF_INET6;
807 else if (strcasecmp(arg, "any") == 0)
808 value = AF_UNSPEC;
809 else
810 fatal("%s line %d: unsupported address family \"%s\".",
811 filename, linenum, arg);
812 if (*intptr == -1)
813 *intptr = value;
814 break;
815
Ben Lindstromade03f62001-12-06 18:22:17 +0000816 case sHostKeyFile:
817 intptr = &options->num_host_key_files;
818 if (*intptr >= MAX_HOSTKEYS)
819 fatal("%s line %d: too many host keys specified (max %d).",
820 filename, linenum, MAX_HOSTKEYS);
821 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +1000822 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +0000823 arg = strdelim(&cp);
824 if (!arg || *arg == '\0')
825 fatal("%s line %d: missing file name.",
826 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +1000827 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +1100828 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000829 /* increase optional counter */
830 if (intptr != NULL)
831 *intptr = *intptr + 1;
832 }
833 break;
834
Damien Miller0a80ca12010-02-27 07:55:05 +1100835 case sHostCertificate:
836 intptr = &options->num_host_cert_files;
837 if (*intptr >= MAX_HOSTKEYS)
838 fatal("%s line %d: too many host certificates "
839 "specified (max %d).", filename, linenum,
840 MAX_HOSTCERTS);
841 charptr = &options->host_cert_files[*intptr];
842 goto parse_filename;
843 break;
844
Ben Lindstromade03f62001-12-06 18:22:17 +0000845 case sPidFile:
846 charptr = &options->pid_file;
847 goto parse_filename;
848
849 case sPermitRootLogin:
850 intptr = &options->permit_root_login;
851 arg = strdelim(&cp);
852 if (!arg || *arg == '\0')
853 fatal("%s line %d: missing yes/"
854 "without-password/forced-commands-only/no "
855 "argument.", filename, linenum);
856 value = 0; /* silence compiler */
857 if (strcmp(arg, "without-password") == 0)
858 value = PERMIT_NO_PASSWD;
859 else if (strcmp(arg, "forced-commands-only") == 0)
860 value = PERMIT_FORCED_ONLY;
861 else if (strcmp(arg, "yes") == 0)
862 value = PERMIT_YES;
863 else if (strcmp(arg, "no") == 0)
864 value = PERMIT_NO;
865 else
866 fatal("%s line %d: Bad yes/"
867 "without-password/forced-commands-only/no "
868 "argument: %s", filename, linenum, arg);
Darren Tucker15f94272008-01-01 20:36:56 +1100869 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000870 *intptr = value;
871 break;
872
873 case sIgnoreRhosts:
874 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +1000875 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +0000876 arg = strdelim(&cp);
877 if (!arg || *arg == '\0')
878 fatal("%s line %d: missing yes/no argument.",
879 filename, linenum);
880 value = 0; /* silence compiler */
881 if (strcmp(arg, "yes") == 0)
882 value = 1;
883 else if (strcmp(arg, "no") == 0)
884 value = 0;
885 else
886 fatal("%s line %d: Bad yes/no argument: %s",
887 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +1000888 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000889 *intptr = value;
890 break;
891
892 case sIgnoreUserKnownHosts:
893 intptr = &options->ignore_user_known_hosts;
894 goto parse_flag;
895
Ben Lindstromade03f62001-12-06 18:22:17 +0000896 case sRhostsRSAAuthentication:
897 intptr = &options->rhosts_rsa_authentication;
898 goto parse_flag;
899
900 case sHostbasedAuthentication:
901 intptr = &options->hostbased_authentication;
902 goto parse_flag;
903
904 case sHostbasedUsesNameFromPacketOnly:
905 intptr = &options->hostbased_uses_name_from_packet_only;
906 goto parse_flag;
907
908 case sRSAAuthentication:
909 intptr = &options->rsa_authentication;
910 goto parse_flag;
911
912 case sPubkeyAuthentication:
913 intptr = &options->pubkey_authentication;
914 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000915
Ben Lindstromade03f62001-12-06 18:22:17 +0000916 case sKerberosAuthentication:
917 intptr = &options->kerberos_authentication;
918 goto parse_flag;
919
920 case sKerberosOrLocalPasswd:
921 intptr = &options->kerberos_or_local_passwd;
922 goto parse_flag;
923
924 case sKerberosTicketCleanup:
925 intptr = &options->kerberos_ticket_cleanup;
926 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000927
Darren Tucker22ef5082003-12-31 11:37:34 +1100928 case sKerberosGetAFSToken:
929 intptr = &options->kerberos_get_afs_token;
930 goto parse_flag;
931
Darren Tucker0efd1552003-08-26 11:49:55 +1000932 case sGssAuthentication:
933 intptr = &options->gss_authentication;
934 goto parse_flag;
935
936 case sGssCleanupCreds:
937 intptr = &options->gss_cleanup_creds;
938 goto parse_flag;
939
Ben Lindstromade03f62001-12-06 18:22:17 +0000940 case sPasswordAuthentication:
941 intptr = &options->password_authentication;
942 goto parse_flag;
943
Damien Miller01ed2272008-11-05 16:20:46 +1100944 case sZeroKnowledgePasswordAuthentication:
945 intptr = &options->zero_knowledge_password_authentication;
946 goto parse_flag;
947
Ben Lindstromade03f62001-12-06 18:22:17 +0000948 case sKbdInteractiveAuthentication:
949 intptr = &options->kbd_interactive_authentication;
950 goto parse_flag;
951
952 case sChallengeResponseAuthentication:
953 intptr = &options->challenge_response_authentication;
954 goto parse_flag;
955
956 case sPrintMotd:
957 intptr = &options->print_motd;
958 goto parse_flag;
959
960 case sPrintLastLog:
961 intptr = &options->print_lastlog;
962 goto parse_flag;
963
964 case sX11Forwarding:
965 intptr = &options->x11_forwarding;
966 goto parse_flag;
967
968 case sX11DisplayOffset:
969 intptr = &options->x11_display_offset;
970 goto parse_int;
971
Damien Miller95c249f2002-02-05 12:11:34 +1100972 case sX11UseLocalhost:
973 intptr = &options->x11_use_localhost;
974 goto parse_flag;
975
Ben Lindstromade03f62001-12-06 18:22:17 +0000976 case sXAuthLocation:
977 charptr = &options->xauth_location;
978 goto parse_filename;
979
980 case sStrictModes:
981 intptr = &options->strict_modes;
982 goto parse_flag;
983
Damien Miller12c150e2003-12-17 16:31:10 +1100984 case sTCPKeepAlive:
985 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +0000986 goto parse_flag;
987
988 case sEmptyPasswd:
989 intptr = &options->permit_empty_passwd;
990 goto parse_flag;
991
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000992 case sPermitUserEnvironment:
993 intptr = &options->permit_user_env;
994 goto parse_flag;
995
Ben Lindstromade03f62001-12-06 18:22:17 +0000996 case sUseLogin:
997 intptr = &options->use_login;
998 goto parse_flag;
999
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001000 case sCompression:
1001 intptr = &options->compression;
Damien Miller9786e6e2005-07-26 21:54:56 +10001002 arg = strdelim(&cp);
1003 if (!arg || *arg == '\0')
1004 fatal("%s line %d: missing yes/no/delayed "
1005 "argument.", filename, linenum);
1006 value = 0; /* silence compiler */
1007 if (strcmp(arg, "delayed") == 0)
1008 value = COMP_DELAYED;
1009 else if (strcmp(arg, "yes") == 0)
1010 value = COMP_ZLIB;
1011 else if (strcmp(arg, "no") == 0)
1012 value = COMP_NONE;
1013 else
1014 fatal("%s line %d: Bad yes/no/delayed "
1015 "argument: %s", filename, linenum, arg);
1016 if (*intptr == -1)
1017 *intptr = value;
1018 break;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001019
Ben Lindstromade03f62001-12-06 18:22:17 +00001020 case sGatewayPorts:
1021 intptr = &options->gateway_ports;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001022 arg = strdelim(&cp);
1023 if (!arg || *arg == '\0')
1024 fatal("%s line %d: missing yes/no/clientspecified "
1025 "argument.", filename, linenum);
1026 value = 0; /* silence compiler */
1027 if (strcmp(arg, "clientspecified") == 0)
1028 value = 2;
1029 else if (strcmp(arg, "yes") == 0)
1030 value = 1;
1031 else if (strcmp(arg, "no") == 0)
1032 value = 0;
1033 else
1034 fatal("%s line %d: Bad yes/no/clientspecified "
1035 "argument: %s", filename, linenum, arg);
Darren Tucker82347a82007-02-25 20:37:52 +11001036 if (*activep && *intptr == -1)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001037 *intptr = value;
1038 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001039
Damien Miller3a961dc2003-06-03 10:25:48 +10001040 case sUseDNS:
1041 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001042 goto parse_flag;
1043
1044 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001045 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001046 arg = strdelim(&cp);
1047 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001048 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001049 fatal("%.200s line %d: unsupported log facility '%s'",
1050 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001051 if (*log_facility_ptr == -1)
1052 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001053 break;
1054
1055 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001056 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001057 arg = strdelim(&cp);
1058 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001059 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001060 fatal("%.200s line %d: unsupported log level '%s'",
1061 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001062 if (*log_level_ptr == -1)
1063 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001064 break;
1065
1066 case sAllowTcpForwarding:
1067 intptr = &options->allow_tcp_forwarding;
1068 goto parse_flag;
1069
Damien Miller4f755cd2008-05-19 14:57:41 +10001070 case sAllowAgentForwarding:
1071 intptr = &options->allow_agent_forwarding;
1072 goto parse_flag;
1073
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001074 case sUsePrivilegeSeparation:
1075 intptr = &use_privsep;
1076 goto parse_flag;
1077
Ben Lindstromade03f62001-12-06 18:22:17 +00001078 case sAllowUsers:
1079 while ((arg = strdelim(&cp)) && *arg != '\0') {
1080 if (options->num_allow_users >= MAX_ALLOW_USERS)
1081 fatal("%s line %d: too many allow users.",
1082 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001083 options->allow_users[options->num_allow_users++] =
1084 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001085 }
1086 break;
1087
1088 case sDenyUsers:
1089 while ((arg = strdelim(&cp)) && *arg != '\0') {
1090 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001091 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001092 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001093 options->deny_users[options->num_deny_users++] =
1094 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001095 }
1096 break;
1097
1098 case sAllowGroups:
1099 while ((arg = strdelim(&cp)) && *arg != '\0') {
1100 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1101 fatal("%s line %d: too many allow groups.",
1102 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001103 options->allow_groups[options->num_allow_groups++] =
1104 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001105 }
1106 break;
1107
1108 case sDenyGroups:
1109 while ((arg = strdelim(&cp)) && *arg != '\0') {
1110 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1111 fatal("%s line %d: too many deny groups.",
1112 filename, linenum);
1113 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
1114 }
1115 break;
1116
1117 case sCiphers:
1118 arg = strdelim(&cp);
1119 if (!arg || *arg == '\0')
1120 fatal("%s line %d: Missing argument.", filename, linenum);
1121 if (!ciphers_valid(arg))
1122 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1123 filename, linenum, arg ? arg : "<NONE>");
1124 if (options->ciphers == NULL)
1125 options->ciphers = xstrdup(arg);
1126 break;
1127
1128 case sMacs:
1129 arg = strdelim(&cp);
1130 if (!arg || *arg == '\0')
1131 fatal("%s line %d: Missing argument.", filename, linenum);
1132 if (!mac_valid(arg))
1133 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1134 filename, linenum, arg ? arg : "<NONE>");
1135 if (options->macs == NULL)
1136 options->macs = xstrdup(arg);
1137 break;
1138
Damien Millerd5f62bf2010-09-24 22:11:14 +10001139 case sKexAlgorithms:
1140 arg = strdelim(&cp);
1141 if (!arg || *arg == '\0')
1142 fatal("%s line %d: Missing argument.",
1143 filename, linenum);
1144 if (!kex_names_valid(arg))
1145 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1146 filename, linenum, arg ? arg : "<NONE>");
1147 if (options->kex_algorithms == NULL)
1148 options->kex_algorithms = xstrdup(arg);
1149 break;
1150
Ben Lindstromade03f62001-12-06 18:22:17 +00001151 case sProtocol:
1152 intptr = &options->protocol;
1153 arg = strdelim(&cp);
1154 if (!arg || *arg == '\0')
1155 fatal("%s line %d: Missing argument.", filename, linenum);
1156 value = proto_spec(arg);
1157 if (value == SSH_PROTO_UNKNOWN)
1158 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001159 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001160 if (*intptr == SSH_PROTO_UNKNOWN)
1161 *intptr = value;
1162 break;
1163
1164 case sSubsystem:
1165 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1166 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001167 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001168 }
1169 arg = strdelim(&cp);
1170 if (!arg || *arg == '\0')
1171 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001172 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001173 if (!*activep) {
1174 arg = strdelim(&cp);
1175 break;
1176 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001177 for (i = 0; i < options->num_subsystems; i++)
1178 if (strcmp(arg, options->subsystem_name[i]) == 0)
1179 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001180 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001181 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1182 arg = strdelim(&cp);
1183 if (!arg || *arg == '\0')
1184 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001185 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001186 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001187
1188 /* Collect arguments (separate to executable) */
1189 p = xstrdup(arg);
1190 len = strlen(p) + 1;
1191 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1192 len += 1 + strlen(arg);
1193 p = xrealloc(p, 1, len);
1194 strlcat(p, " ", len);
1195 strlcat(p, arg, len);
1196 }
1197 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001198 options->num_subsystems++;
1199 break;
1200
1201 case sMaxStartups:
1202 arg = strdelim(&cp);
1203 if (!arg || *arg == '\0')
1204 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001205 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001206 if ((n = sscanf(arg, "%d:%d:%d",
1207 &options->max_startups_begin,
1208 &options->max_startups_rate,
1209 &options->max_startups)) == 3) {
1210 if (options->max_startups_begin >
1211 options->max_startups ||
1212 options->max_startups_rate > 100 ||
1213 options->max_startups_rate < 1)
1214 fatal("%s line %d: Illegal MaxStartups spec.",
1215 filename, linenum);
1216 } else if (n != 1)
1217 fatal("%s line %d: Illegal MaxStartups spec.",
1218 filename, linenum);
1219 else
1220 options->max_startups = options->max_startups_begin;
1221 break;
1222
Darren Tucker89413db2004-05-24 10:36:23 +10001223 case sMaxAuthTries:
1224 intptr = &options->max_authtries;
1225 goto parse_int;
1226
Damien Miller7207f642008-05-19 15:34:50 +10001227 case sMaxSessions:
1228 intptr = &options->max_sessions;
1229 goto parse_int;
1230
Ben Lindstromade03f62001-12-06 18:22:17 +00001231 case sBanner:
1232 charptr = &options->banner;
1233 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001234
Ben Lindstromade03f62001-12-06 18:22:17 +00001235 /*
1236 * These options can contain %X options expanded at
1237 * connect time, so that you can specify paths like:
1238 *
1239 * AuthorizedKeysFile /etc/ssh_keys/%u
1240 */
1241 case sAuthorizedKeysFile:
Damien Miller30da3442010-05-10 11:58:03 +10001242 charptr = &options->authorized_keys_file;
1243 goto parse_tilde_filename;
Ben Lindstromade03f62001-12-06 18:22:17 +00001244 case sAuthorizedKeysFile2:
Damien Miller30da3442010-05-10 11:58:03 +10001245 charptr = &options->authorized_keys_file2;
1246 goto parse_tilde_filename;
1247 case sAuthorizedPrincipalsFile:
1248 charptr = &options->authorized_principals_file;
1249 parse_tilde_filename:
Damien Millerc4cb47b2010-03-22 05:52:26 +11001250 arg = strdelim(&cp);
1251 if (!arg || *arg == '\0')
1252 fatal("%s line %d: missing file name.",
1253 filename, linenum);
1254 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001255 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001256 /* increase optional counter */
1257 if (intptr != NULL)
1258 *intptr = *intptr + 1;
1259 }
1260 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001261
1262 case sClientAliveInterval:
1263 intptr = &options->client_alive_interval;
1264 goto parse_time;
1265
1266 case sClientAliveCountMax:
1267 intptr = &options->client_alive_count_max;
1268 goto parse_int;
1269
Darren Tucker46bc0752004-05-02 22:11:30 +10001270 case sAcceptEnv:
1271 while ((arg = strdelim(&cp)) && *arg != '\0') {
1272 if (strchr(arg, '=') != NULL)
1273 fatal("%s line %d: Invalid environment name.",
1274 filename, linenum);
1275 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1276 fatal("%s line %d: too many allow env.",
1277 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001278 if (!*activep)
1279 break;
Darren Tucker46bc0752004-05-02 22:11:30 +10001280 options->accept_env[options->num_accept_env++] =
1281 xstrdup(arg);
1282 }
1283 break;
1284
Damien Millerd27b9472005-12-13 19:29:02 +11001285 case sPermitTunnel:
1286 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001287 arg = strdelim(&cp);
1288 if (!arg || *arg == '\0')
1289 fatal("%s line %d: Missing yes/point-to-point/"
1290 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001291 value = -1;
1292 for (i = 0; tunmode_desc[i].val != -1; i++)
1293 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1294 value = tunmode_desc[i].val;
1295 break;
1296 }
1297 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001298 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1299 "no argument: %s", filename, linenum, arg);
1300 if (*intptr == -1)
1301 *intptr = value;
1302 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001303
Darren Tucker45150472006-07-12 22:34:17 +10001304 case sMatch:
1305 if (cmdline)
1306 fatal("Match directive not supported as a command-line "
1307 "option");
1308 value = match_cfg_line(&cp, linenum, user, host, address);
1309 if (value < 0)
1310 fatal("%s line %d: Bad Match condition", filename,
1311 linenum);
1312 *activep = value;
1313 break;
1314
Damien Miller9b439df2006-07-24 14:04:00 +10001315 case sPermitOpen:
1316 arg = strdelim(&cp);
1317 if (!arg || *arg == '\0')
1318 fatal("%s line %d: missing PermitOpen specification",
1319 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001320 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001321 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001322 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001323 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001324 options->num_permitted_opens = 0;
1325 }
Damien Miller9b439df2006-07-24 14:04:00 +10001326 break;
1327 }
Damien Millera29b95e2007-01-05 16:28:36 +11001328 if (*activep && n == -1)
1329 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001330 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1331 p = hpdelim(&arg);
1332 if (p == NULL)
1333 fatal("%s line %d: missing host in PermitOpen",
1334 filename, linenum);
1335 p = cleanhostname(p);
Damien Miller3dc71ad2009-01-28 16:31:22 +11001336 if (arg == NULL || (port = a2port(arg)) <= 0)
Damien Millera765cf42006-07-24 14:08:13 +10001337 fatal("%s line %d: bad port number in "
1338 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001339 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001340 options->num_permitted_opens =
1341 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001342 }
Damien Miller9b439df2006-07-24 14:04:00 +10001343 break;
1344
Damien Millere2754432006-07-24 14:06:47 +10001345 case sForceCommand:
1346 if (cp == NULL)
1347 fatal("%.200s line %d: Missing argument.", filename,
1348 linenum);
1349 len = strspn(cp, WHITESPACE);
1350 if (*activep && options->adm_forced_command == NULL)
1351 options->adm_forced_command = xstrdup(cp + len);
1352 return 0;
1353
Damien Millerd8cb1f12008-02-10 22:40:12 +11001354 case sChrootDirectory:
1355 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001356
1357 arg = strdelim(&cp);
1358 if (!arg || *arg == '\0')
1359 fatal("%s line %d: missing file name.",
1360 filename, linenum);
1361 if (*activep && *charptr == NULL)
1362 *charptr = xstrdup(arg);
1363 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001364
Damien Miller1aed65e2010-03-04 21:53:35 +11001365 case sTrustedUserCAKeys:
1366 charptr = &options->trusted_user_ca_keys;
1367 goto parse_filename;
1368
1369 case sRevokedKeys:
1370 charptr = &options->revoked_keys_file;
1371 goto parse_filename;
1372
Ben Lindstromade03f62001-12-06 18:22:17 +00001373 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001374 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001375 filename, linenum, arg);
1376 while (arg)
1377 arg = strdelim(&cp);
1378 break;
1379
Damien Millerf9b3feb2003-05-16 11:38:32 +10001380 case sUnsupported:
1381 logit("%s line %d: Unsupported option %s",
1382 filename, linenum, arg);
1383 while (arg)
1384 arg = strdelim(&cp);
1385 break;
1386
Ben Lindstromade03f62001-12-06 18:22:17 +00001387 default:
1388 fatal("%s line %d: Missing handler for opcode %s (%d)",
1389 filename, linenum, arg, opcode);
1390 }
1391 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1392 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1393 filename, linenum, arg);
1394 return 0;
1395}
1396
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001397/* Reads the server configuration file. */
1398
Damien Miller4af51302000-04-16 11:18:38 +10001399void
Darren Tucker645ab752004-06-25 13:33:20 +10001400load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001401{
Darren Tucker645ab752004-06-25 13:33:20 +10001402 char line[1024], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001403 FILE *f;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001404
Darren Tucker645ab752004-06-25 13:33:20 +10001405 debug2("%s: filename %s", __func__, filename);
1406 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001407 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001408 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001409 }
Darren Tucker645ab752004-06-25 13:33:20 +10001410 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001411 while (fgets(line, sizeof(line), f)) {
Darren Tucker645ab752004-06-25 13:33:20 +10001412 /*
1413 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001414 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001415 * line numbers later for error messages
1416 */
1417 if ((cp = strchr(line, '#')) != NULL)
1418 memcpy(cp, "\n", 2);
1419 cp = line + strspn(line, " \t\r");
1420
1421 buffer_append(conf, cp, strlen(cp));
1422 }
1423 buffer_append(conf, "\0", 1);
1424 fclose(f);
1425 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1426}
1427
1428void
Darren Tucker45150472006-07-12 22:34:17 +10001429parse_server_match_config(ServerOptions *options, const char *user,
1430 const char *host, const char *address)
Darren Tucker645ab752004-06-25 13:33:20 +10001431{
Darren Tucker45150472006-07-12 22:34:17 +10001432 ServerOptions mo;
1433
1434 initialize_server_options(&mo);
1435 parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
Darren Tucker1629c072007-02-19 22:25:37 +11001436 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001437}
1438
Darren Tucker1629c072007-02-19 22:25:37 +11001439/* Helper macros */
1440#define M_CP_INTOPT(n) do {\
1441 if (src->n != -1) \
1442 dst->n = src->n; \
1443} while (0)
1444#define M_CP_STROPT(n) do {\
1445 if (src->n != NULL) { \
1446 if (dst->n != NULL) \
1447 xfree(dst->n); \
1448 dst->n = src->n; \
1449 } \
1450} while(0)
1451
1452/*
1453 * Copy any supported values that are set.
1454 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001455 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001456 * array values that are not used pre-authentication, because any that we
1457 * do use must be explictly sent in mm_getpwnamallow().
1458 */
Darren Tucker45150472006-07-12 22:34:17 +10001459void
Darren Tucker1629c072007-02-19 22:25:37 +11001460copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001461{
Darren Tucker1629c072007-02-19 22:25:37 +11001462 M_CP_INTOPT(password_authentication);
1463 M_CP_INTOPT(gss_authentication);
1464 M_CP_INTOPT(rsa_authentication);
1465 M_CP_INTOPT(pubkey_authentication);
1466 M_CP_INTOPT(kerberos_authentication);
1467 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001468 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001469 M_CP_INTOPT(kbd_interactive_authentication);
Damien Miller01ed2272008-11-05 16:20:46 +11001470 M_CP_INTOPT(zero_knowledge_password_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001471 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001472 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001473
1474 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001475 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001476 M_CP_INTOPT(permit_tun);
Darren Tucker1629c072007-02-19 22:25:37 +11001477 M_CP_INTOPT(gateway_ports);
1478 M_CP_INTOPT(x11_display_offset);
1479 M_CP_INTOPT(x11_forwarding);
1480 M_CP_INTOPT(x11_use_localhost);
Damien Miller7207f642008-05-19 15:34:50 +10001481 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001482 M_CP_INTOPT(max_authtries);
Darren Tucker1629c072007-02-19 22:25:37 +11001483
1484 M_CP_STROPT(banner);
1485 if (preauth)
1486 return;
1487 M_CP_STROPT(adm_forced_command);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001488 M_CP_STROPT(chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11001489 M_CP_STROPT(trusted_user_ca_keys);
1490 M_CP_STROPT(revoked_keys_file);
Damien Millerab6de352010-06-26 09:38:45 +10001491 M_CP_STROPT(authorized_keys_file);
1492 M_CP_STROPT(authorized_keys_file2);
1493 M_CP_STROPT(authorized_principals_file);
Darren Tucker45150472006-07-12 22:34:17 +10001494}
1495
Darren Tucker1629c072007-02-19 22:25:37 +11001496#undef M_CP_INTOPT
1497#undef M_CP_STROPT
1498
Darren Tucker45150472006-07-12 22:34:17 +10001499void
1500parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1501 const char *user, const char *host, const char *address)
1502{
1503 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001504 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001505
1506 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1507
Darren Tucker9fbac712004-08-12 22:41:44 +10001508 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tucker45150472006-07-12 22:34:17 +10001509 active = user ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001510 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001511 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001512 if (process_server_config_line(options, cp, filename,
Darren Tucker45150472006-07-12 22:34:17 +10001513 linenum++, &active, user, host, address) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001514 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001515 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001516 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001517 if (bad_options > 0)
1518 fatal("%s: terminating, %d bad configuration options",
1519 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001520}
Darren Tuckere7140f22008-06-10 23:01:51 +10001521
1522static const char *
1523fmt_intarg(ServerOpCodes code, int val)
1524{
1525 if (code == sAddressFamily) {
1526 switch (val) {
1527 case AF_INET:
1528 return "inet";
1529 case AF_INET6:
1530 return "inet6";
1531 case AF_UNSPEC:
1532 return "any";
1533 default:
1534 return "UNKNOWN";
1535 }
1536 }
1537 if (code == sPermitRootLogin) {
1538 switch (val) {
1539 case PERMIT_NO_PASSWD:
Darren Tuckerff4350e2008-11-11 16:31:05 +11001540 return "without-password";
Darren Tuckere7140f22008-06-10 23:01:51 +10001541 case PERMIT_FORCED_ONLY:
1542 return "forced-commands-only";
1543 case PERMIT_YES:
1544 return "yes";
1545 }
1546 }
1547 if (code == sProtocol) {
1548 switch (val) {
1549 case SSH_PROTO_1:
1550 return "1";
1551 case SSH_PROTO_2:
1552 return "2";
1553 case (SSH_PROTO_1|SSH_PROTO_2):
1554 return "2,1";
1555 default:
1556 return "UNKNOWN";
1557 }
1558 }
1559 if (code == sGatewayPorts && val == 2)
1560 return "clientspecified";
1561 if (code == sCompression && val == COMP_DELAYED)
1562 return "delayed";
1563 switch (val) {
1564 case -1:
1565 return "unset";
1566 case 0:
1567 return "no";
1568 case 1:
1569 return "yes";
1570 }
1571 return "UNKNOWN";
1572}
1573
1574static const char *
1575lookup_opcode_name(ServerOpCodes code)
1576{
1577 u_int i;
1578
1579 for (i = 0; keywords[i].name != NULL; i++)
1580 if (keywords[i].opcode == code)
1581 return(keywords[i].name);
1582 return "UNKNOWN";
1583}
1584
1585static void
1586dump_cfg_int(ServerOpCodes code, int val)
1587{
1588 printf("%s %d\n", lookup_opcode_name(code), val);
1589}
1590
1591static void
1592dump_cfg_fmtint(ServerOpCodes code, int val)
1593{
1594 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
1595}
1596
1597static void
1598dump_cfg_string(ServerOpCodes code, const char *val)
1599{
1600 if (val == NULL)
1601 return;
1602 printf("%s %s\n", lookup_opcode_name(code), val);
1603}
1604
1605static void
1606dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
1607{
1608 u_int i;
1609
1610 for (i = 0; i < count; i++)
1611 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
1612}
1613
1614void
1615dump_config(ServerOptions *o)
1616{
1617 u_int i;
1618 int ret;
1619 struct addrinfo *ai;
1620 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
1621
1622 /* these are usually at the top of the config */
1623 for (i = 0; i < o->num_ports; i++)
1624 printf("port %d\n", o->ports[i]);
1625 dump_cfg_fmtint(sProtocol, o->protocol);
1626 dump_cfg_fmtint(sAddressFamily, o->address_family);
1627
1628 /* ListenAddress must be after Port */
1629 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
1630 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1631 sizeof(addr), port, sizeof(port),
1632 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1633 error("getnameinfo failed: %.100s",
1634 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
1635 strerror(errno));
1636 } else {
1637 if (ai->ai_family == AF_INET6)
1638 printf("listenaddress [%s]:%s\n", addr, port);
1639 else
1640 printf("listenaddress %s:%s\n", addr, port);
1641 }
1642 }
1643
1644 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10001645#ifdef USE_PAM
1646 dump_cfg_int(sUsePAM, o->use_pam);
1647#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001648 dump_cfg_int(sServerKeyBits, o->server_key_bits);
1649 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
1650 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
1651 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
1652 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11001653 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10001654 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
1655 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
1656
1657 /* formatted integer arguments */
1658 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
1659 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
1660 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
1661 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
1662 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
1663 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
1664 o->hostbased_uses_name_from_packet_only);
1665 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
1666 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10001667#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10001668 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
1669 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
1670 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10001671# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10001672 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10001673# endif
1674#endif
1675#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10001676 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
1677 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10001678#endif
Damien Miller01ed2272008-11-05 16:20:46 +11001679#ifdef JPAKE
1680 dump_cfg_fmtint(sZeroKnowledgePasswordAuthentication,
1681 o->zero_knowledge_password_authentication);
1682#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001683 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
1684 dump_cfg_fmtint(sKbdInteractiveAuthentication,
1685 o->kbd_interactive_authentication);
1686 dump_cfg_fmtint(sChallengeResponseAuthentication,
1687 o->challenge_response_authentication);
1688 dump_cfg_fmtint(sPrintMotd, o->print_motd);
1689 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
1690 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
1691 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
1692 dump_cfg_fmtint(sStrictModes, o->strict_modes);
1693 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
1694 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
1695 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
1696 dump_cfg_fmtint(sUseLogin, o->use_login);
1697 dump_cfg_fmtint(sCompression, o->compression);
1698 dump_cfg_fmtint(sGatewayPorts, o->gateway_ports);
1699 dump_cfg_fmtint(sUseDNS, o->use_dns);
1700 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
1701 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
1702
1703 /* string arguments */
1704 dump_cfg_string(sPidFile, o->pid_file);
1705 dump_cfg_string(sXAuthLocation, o->xauth_location);
1706 dump_cfg_string(sCiphers, o->ciphers);
1707 dump_cfg_string(sMacs, o->macs);
1708 dump_cfg_string(sBanner, o->banner);
1709 dump_cfg_string(sAuthorizedKeysFile, o->authorized_keys_file);
1710 dump_cfg_string(sAuthorizedKeysFile2, o->authorized_keys_file2);
1711 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11001712 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11001713 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
1714 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10001715 dump_cfg_string(sAuthorizedPrincipalsFile,
1716 o->authorized_principals_file);
Darren Tuckere7140f22008-06-10 23:01:51 +10001717
1718 /* string arguments requiring a lookup */
1719 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
1720 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
1721
1722 /* string array arguments */
1723 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
1724 o->host_key_files);
Damien Miller0a80ca12010-02-27 07:55:05 +11001725 dump_cfg_strarray(sHostKeyFile, o->num_host_cert_files,
1726 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10001727 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
1728 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
1729 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
1730 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
1731 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
1732
1733 /* other arguments */
1734 for (i = 0; i < o->num_subsystems; i++)
1735 printf("subsystem %s %s\n", o->subsystem_name[i],
1736 o->subsystem_args[i]);
1737
1738 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
1739 o->max_startups_rate, o->max_startups);
1740
1741 for (i = 0; tunmode_desc[i].val != -1; i++)
1742 if (tunmode_desc[i].val == o->permit_tun) {
1743 s = tunmode_desc[i].text;
1744 break;
1745 }
1746 dump_cfg_string(sPermitTunnel, s);
1747
Darren Tuckere7140f22008-06-10 23:01:51 +10001748 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10001749}