blob: d26a7db05f3eeda2300e7f893dc5d8577bb1d060 [file] [log] [blame]
Damien Millerd5f62bf2010-09-24 22:11:14 +10001/* $OpenBSD: servconf.c,v 1.211 2010/09/22 05:01:29 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 Millere13cadf2010-09-10 11:15:33 +1000158 options->host_key_files[options->num_host_key_files++] =
159 _PATH_HOST_ECDSA_KEY_FILE;
Damien Miller7fc23732002-01-22 23:19:11 +1100160 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100161 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100162 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100163 if (options->num_ports == 0)
164 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
165 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000166 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000167 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000168 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100169 if (options->server_key_bits == -1)
Darren Tucker7499b0c2008-07-02 22:35:43 +1000170 options->server_key_bits = 1024;
Damien Miller95def091999-11-25 00:26:21 +1100171 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000172 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100173 if (options->key_regeneration_time == -1)
174 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000175 if (options->permit_root_login == PERMIT_NOT_SET)
176 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100177 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100178 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100179 if (options->ignore_user_known_hosts == -1)
180 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100181 if (options->print_motd == -1)
182 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000183 if (options->print_lastlog == -1)
184 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100185 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100186 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100187 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100188 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100189 if (options->x11_use_localhost == -1)
190 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000191 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000192 options->xauth_location = _PATH_XAUTH;
Damien Miller95def091999-11-25 00:26:21 +1100193 if (options->strict_modes == -1)
194 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100195 if (options->tcp_keep_alive == -1)
196 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100197 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100198 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100199 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000200 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100201 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100202 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000203 if (options->hostbased_authentication == -1)
204 options->hostbased_authentication = 0;
205 if (options->hostbased_uses_name_from_packet_only == -1)
206 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100207 if (options->rsa_authentication == -1)
208 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100209 if (options->pubkey_authentication == -1)
210 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100211 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000212 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100213 if (options->kerberos_or_local_passwd == -1)
214 options->kerberos_or_local_passwd = 1;
215 if (options->kerberos_ticket_cleanup == -1)
216 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100217 if (options->kerberos_get_afs_token == -1)
218 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000219 if (options->gss_authentication == -1)
220 options->gss_authentication = 0;
221 if (options->gss_cleanup_creds == -1)
222 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100223 if (options->password_authentication == -1)
224 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100225 if (options->kbd_interactive_authentication == -1)
226 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000227 if (options->challenge_response_authentication == -1)
228 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100229 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100230 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000231 if (options->permit_user_env == -1)
232 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100233 if (options->use_login == -1)
234 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000235 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000236 options->compression = COMP_DELAYED;
Damien Miller50a41ed2000-10-16 12:14:42 +1100237 if (options->allow_tcp_forwarding == -1)
238 options->allow_tcp_forwarding = 1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000239 if (options->allow_agent_forwarding == -1)
240 options->allow_agent_forwarding = 1;
Damien Millere247cc42000-05-07 12:03:14 +1000241 if (options->gateway_ports == -1)
242 options->gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000243 if (options->max_startups == -1)
244 options->max_startups = 10;
Damien Miller942da032000-08-18 13:59:06 +1000245 if (options->max_startups_rate == -1)
246 options->max_startups_rate = 100; /* 100% */
247 if (options->max_startups_begin == -1)
248 options->max_startups_begin = options->max_startups;
Darren Tucker89413db2004-05-24 10:36:23 +1000249 if (options->max_authtries == -1)
250 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000251 if (options->max_sessions == -1)
252 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000253 if (options->use_dns == -1)
254 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000255 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100256 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000257 if (options->client_alive_count_max == -1)
258 options->client_alive_count_max = 3;
Damien Miller75413ac2001-11-12 11:14:35 +1100259 if (options->authorized_keys_file2 == NULL) {
260 /* authorized_keys_file2 falls back to authorized_keys_file */
261 if (options->authorized_keys_file != NULL)
262 options->authorized_keys_file2 = options->authorized_keys_file;
263 else
264 options->authorized_keys_file2 = _PATH_SSH_USER_PERMITTED_KEYS2;
265 }
266 if (options->authorized_keys_file == NULL)
267 options->authorized_keys_file = _PATH_SSH_USER_PERMITTED_KEYS;
Damien Millerd27b9472005-12-13 19:29:02 +1100268 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100269 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller01ed2272008-11-05 16:20:46 +1100270 if (options->zero_knowledge_password_authentication == -1)
271 options->zero_knowledge_password_authentication = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000272
Ben Lindstromfb62a692002-06-06 19:47:11 +0000273 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000274 if (use_privsep == -1)
Ben Lindstromfb62a692002-06-06 19:47:11 +0000275 use_privsep = 1;
Damien Miller4903eb42002-06-21 16:20:44 +1000276
Tim Rice40017b02002-07-14 13:36:49 -0700277#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000278 if (use_privsep && options->compression == 1) {
279 error("This platform does not support both privilege "
280 "separation and compression");
281 error("Compression disabled");
282 options->compression = 0;
283 }
284#endif
285
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286}
287
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100289typedef enum {
290 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100291 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000292 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100293 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100294 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
295 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000296 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100297 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100298 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000299 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100300 sPasswordAuthentication, sKbdInteractiveAuthentication,
301 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000302 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100303 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller12c150e2003-12-17 16:31:10 +1100304 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000305 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Damien Miller50a41ed2000-10-16 12:14:42 +1100306 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000307 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000308 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
Damien Miller7207f642008-05-19 15:34:50 +1000309 sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000310 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100311 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000312 sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
Damien Millerd27b9472005-12-13 19:29:02 +1100313 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100314 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100315 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller0a80ca12010-02-27 07:55:05 +1100316 sZeroKnowledgePasswordAuthentication, sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000317 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Millerd5f62bf2010-09-24 22:11:14 +1000318 sKexAlgorithms,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000319 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320} ServerOpCodes;
321
Darren Tucker45150472006-07-12 22:34:17 +1000322#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
323#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
324#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
325
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100327static struct {
328 const char *name;
329 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000330 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100331} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100332 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000333#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000334 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000335#else
Darren Tucker45150472006-07-12 22:34:17 +1000336 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000337#endif
Darren Tucker45150472006-07-12 22:34:17 +1000338 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100339 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000340 { "port", sPort, SSHCFG_GLOBAL },
341 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
342 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
343 { "pidfile", sPidFile, SSHCFG_GLOBAL },
344 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
345 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
346 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100347 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000348 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
349 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
350 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100351 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
352 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000353 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100354 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
355 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000356 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000357#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100358 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000359 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
360 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100361#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000362 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000363#else
Darren Tucker45150472006-07-12 22:34:17 +1000364 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100365#endif
366#else
Darren Tucker1629c072007-02-19 22:25:37 +1100367 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000368 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
369 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
370 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000371#endif
Darren Tucker45150472006-07-12 22:34:17 +1000372 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
373 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000374#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100375 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000376 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000377#else
Darren Tucker1629c072007-02-19 22:25:37 +1100378 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000379 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000380#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100381 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
382 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100383 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000384 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
Damien Miller01ed2272008-11-05 16:20:46 +1100385#ifdef JPAKE
386 { "zeroknowledgepasswordauthentication", sZeroKnowledgePasswordAuthentication, SSHCFG_ALL },
387#else
388 { "zeroknowledgepasswordauthentication", sUnsupported, SSHCFG_ALL },
389#endif
Darren Tucker45150472006-07-12 22:34:17 +1000390 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
391 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
392 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
393 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
394 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
395 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
396 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000397 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
398 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
399 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000400 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
401 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100402 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000403 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
404 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
405 { "compression", sCompression, SSHCFG_GLOBAL },
406 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
407 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
408 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000409 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000410 { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
411 { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
412 { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
413 { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
414 { "ciphers", sCiphers, SSHCFG_GLOBAL },
415 { "macs", sMacs, SSHCFG_GLOBAL },
416 { "protocol", sProtocol, SSHCFG_GLOBAL },
417 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
418 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
419 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000420 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000421 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100422 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000423 { "usedns", sUseDNS, SSHCFG_GLOBAL },
424 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
425 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
426 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
427 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000428 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
429 { "authorizedkeysfile2", sAuthorizedKeysFile2, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000430 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Darren Tucker45150472006-07-12 22:34:17 +1000431 { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000432 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000433 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000434 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000435 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100436 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100437 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100438 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
439 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000440 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000441 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000442 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443};
444
Darren Tuckere7140f22008-06-10 23:01:51 +1000445static struct {
446 int val;
447 char *text;
448} tunmode_desc[] = {
449 { SSH_TUNMODE_NO, "no" },
450 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
451 { SSH_TUNMODE_ETHERNET, "ethernet" },
452 { SSH_TUNMODE_YES, "yes" },
453 { -1, NULL }
454};
455
Damien Miller5428f641999-11-25 11:54:57 +1100456/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000457 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100458 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459
Damien Miller4af51302000-04-16 11:18:38 +1000460static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100461parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000462 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000464 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465
Damien Miller95def091999-11-25 00:26:21 +1100466 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000467 if (strcasecmp(cp, keywords[i].name) == 0) {
468 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100469 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000470 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000472 error("%s: line %d: Bad configuration option: %s",
473 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100474 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475}
476
Darren Tucker88b6fb22010-01-13 22:44:29 +1100477char *
478derelativise_path(const char *path)
479{
Damien Miller44451d02010-03-26 10:40:04 +1100480 char *expanded, *ret, cwd[MAXPATHLEN];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100481
482 expanded = tilde_expand_filename(path, getuid());
483 if (*expanded == '/')
484 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100485 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100486 fatal("%s: getcwd: %s", __func__, strerror(errno));
487 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100488 xfree(expanded);
489 return ret;
490}
491
Ben Lindstrombba81212001-06-25 05:01:22 +0000492static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100493add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100494{
Damien Millereccb9de2005-06-17 12:59:34 +1000495 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100496
497 if (options->num_ports == 0)
498 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100499 if (options->address_family == -1)
500 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000501 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000502 for (i = 0; i < options->num_ports; i++)
503 add_one_listen_addr(options, addr, options->ports[i]);
504 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000505 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000506}
507
Ben Lindstrombba81212001-06-25 05:01:22 +0000508static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100509add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000510{
511 struct addrinfo hints, *ai, *aitop;
512 char strport[NI_MAXSERV];
513 int gaierr;
514
515 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100516 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000517 hints.ai_socktype = SOCK_STREAM;
518 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100519 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000520 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
521 fatal("bad addr or host: %s (%s)",
522 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100523 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000524 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
525 ;
526 ai->ai_next = options->listen_addrs;
527 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100528}
529
Darren Tucker45150472006-07-12 22:34:17 +1000530/*
531 * The strategy for the Match blocks is that the config file is parsed twice.
532 *
533 * The first time is at startup. activep is initialized to 1 and the
534 * directives in the global context are processed and acted on. Hitting a
535 * Match directive unsets activep and the directives inside the block are
536 * checked for syntax only.
537 *
538 * The second time is after a connection has been established but before
539 * authentication. activep is initialized to 2 and global config directives
540 * are ignored since they have already been processed. If the criteria in a
541 * Match block is met, activep is set and the subsequent directives
542 * processed and actioned until EOF or another Match block unsets it. Any
543 * options set are copied into the main server config.
544 *
545 * Potential additions/improvements:
546 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
547 *
548 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
549 * Match Address 192.168.0.*
550 * Tag trusted
551 * Match Group wheel
552 * Tag trusted
553 * Match Tag trusted
554 * AllowTcpForwarding yes
555 * GatewayPorts clientspecified
556 * [...]
557 *
558 * - Add a PermittedChannelRequests directive
559 * Match Group shell
560 * PermittedChannelRequests session,forwarded-tcpip
561 */
562
563static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000564match_cfg_line_group(const char *grps, int line, const char *user)
565{
566 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000567 struct passwd *pw;
568
Damien Miller565ca3f2006-08-19 00:23:15 +1000569 if (user == NULL)
570 goto out;
571
572 if ((pw = getpwnam(user)) == NULL) {
573 debug("Can't match group at line %d because user %.100s does "
574 "not exist", line, user);
575 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
576 debug("Can't Match group because user %.100s not in any group "
577 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000578 } else if (ga_match_pattern_list(grps) != 1) {
579 debug("user %.100s does not match group list %.100s at line %d",
580 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000581 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000582 debug("user %.100s matched group list %.100s at line %d", user,
583 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000584 result = 1;
585 }
586out:
587 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000588 return result;
589}
590
591static int
Darren Tucker45150472006-07-12 22:34:17 +1000592match_cfg_line(char **condition, int line, const char *user, const char *host,
593 const char *address)
594{
595 int result = 1;
596 char *arg, *attrib, *cp = *condition;
597 size_t len;
598
599 if (user == NULL)
600 debug3("checking syntax for 'Match %s'", cp);
601 else
602 debug3("checking match for '%s' user %s host %s addr %s", cp,
603 user ? user : "(null)", host ? host : "(null)",
604 address ? address : "(null)");
605
606 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
607 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
608 error("Missing Match criteria for %s", attrib);
609 return -1;
610 }
611 len = strlen(arg);
612 if (strcasecmp(attrib, "user") == 0) {
613 if (!user) {
614 result = 0;
615 continue;
616 }
617 if (match_pattern_list(user, arg, len, 0) != 1)
618 result = 0;
619 else
620 debug("user %.100s matched 'User %.100s' at "
621 "line %d", user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000622 } else if (strcasecmp(attrib, "group") == 0) {
623 switch (match_cfg_line_group(arg, line, user)) {
624 case -1:
625 return -1;
626 case 0:
627 result = 0;
628 }
Darren Tucker45150472006-07-12 22:34:17 +1000629 } else if (strcasecmp(attrib, "host") == 0) {
630 if (!host) {
631 result = 0;
632 continue;
633 }
634 if (match_hostname(host, arg, len) != 1)
635 result = 0;
636 else
637 debug("connection from %.100s matched 'Host "
638 "%.100s' at line %d", host, arg, line);
639 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000640 switch (addr_match_list(address, arg)) {
641 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000642 debug("connection from %.100s matched 'Address "
643 "%.100s' at line %d", address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000644 break;
645 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000646 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000647 result = 0;
648 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000649 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000650 return -1;
651 }
Darren Tucker45150472006-07-12 22:34:17 +1000652 } else {
653 error("Unsupported Match attribute %s", attrib);
654 return -1;
655 }
656 }
657 if (user != NULL)
658 debug3("match %sfound", result ? "" : "not ");
659 *condition = cp;
660 return result;
661}
662
Damien Millere2754432006-07-24 14:06:47 +1000663#define WHITESPACE " \t\r\n"
664
Ben Lindstromade03f62001-12-06 18:22:17 +0000665int
666process_server_config_line(ServerOptions *options, char *line,
Darren Tucker45150472006-07-12 22:34:17 +1000667 const char *filename, int linenum, int *activep, const char *user,
668 const char *host, const char *address)
Ben Lindstromade03f62001-12-06 18:22:17 +0000669{
670 char *cp, **charptr, *arg, *p;
Darren Tucker45150472006-07-12 22:34:17 +1000671 int cmdline = 0, *intptr, value, n;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100672 SyslogFacility *log_facility_ptr;
673 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000674 ServerOpCodes opcode;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100675 int port;
Darren Tucker45150472006-07-12 22:34:17 +1000676 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000677 size_t len;
Ben Lindstromade03f62001-12-06 18:22:17 +0000678
679 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100680 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100681 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000682 /* Ignore leading whitespace */
683 if (*arg == '\0')
684 arg = strdelim(&cp);
685 if (!arg || !*arg || *arg == '#')
686 return 0;
687 intptr = NULL;
688 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000689 opcode = parse_token(arg, filename, linenum, &flags);
690
691 if (activep == NULL) { /* We are processing a command line directive */
692 cmdline = 1;
693 activep = &cmdline;
694 }
695 if (*activep && opcode != sMatch)
696 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
697 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
698 if (user == NULL) {
699 fatal("%s line %d: Directive '%s' is not allowed "
700 "within a Match block", filename, linenum, arg);
701 } else { /* this is a directive we have already processed */
702 while (arg)
703 arg = strdelim(&cp);
704 return 0;
705 }
706 }
707
Ben Lindstromade03f62001-12-06 18:22:17 +0000708 switch (opcode) {
709 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000710 case sUsePAM:
711 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000712 goto parse_flag;
713
714 /* Standard Options */
715 case sBadOption:
716 return -1;
717 case sPort:
718 /* ignore ports from configfile if cmdline specifies ports */
719 if (options->ports_from_cmdline)
720 return 0;
721 if (options->listen_addrs != NULL)
722 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100723 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000724 if (options->num_ports >= MAX_PORTS)
725 fatal("%s line %d: too many ports.",
726 filename, linenum);
727 arg = strdelim(&cp);
728 if (!arg || *arg == '\0')
729 fatal("%s line %d: missing port number.",
730 filename, linenum);
731 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100732 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000733 fatal("%s line %d: Badly formatted port number.",
734 filename, linenum);
735 break;
736
737 case sServerKeyBits:
738 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +1000739 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +0000740 arg = strdelim(&cp);
741 if (!arg || *arg == '\0')
742 fatal("%s line %d: missing integer value.",
743 filename, linenum);
744 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000745 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000746 *intptr = value;
747 break;
748
749 case sLoginGraceTime:
750 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +1000751 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +0000752 arg = strdelim(&cp);
753 if (!arg || *arg == '\0')
754 fatal("%s line %d: missing time value.",
755 filename, linenum);
756 if ((value = convtime(arg)) == -1)
757 fatal("%s line %d: invalid time value.",
758 filename, linenum);
759 if (*intptr == -1)
760 *intptr = value;
761 break;
762
763 case sKeyRegenerationTime:
764 intptr = &options->key_regeneration_time;
765 goto parse_time;
766
767 case sListenAddress:
768 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100769 if (arg == NULL || *arg == '\0')
770 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000771 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000772 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
773 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
774 && strchr(p+1, ':') != NULL) {
775 add_listen_addr(options, arg, 0);
776 break;
777 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100778 p = hpdelim(&arg);
779 if (p == NULL)
780 fatal("%s line %d: bad address:port usage",
781 filename, linenum);
782 p = cleanhostname(p);
783 if (arg == NULL)
784 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100785 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100786 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000787
Damien Millerf91ee4c2005-03-01 21:24:33 +1100788 add_listen_addr(options, p, port);
789
Ben Lindstromade03f62001-12-06 18:22:17 +0000790 break;
791
Darren Tucker0f383232005-01-20 10:57:56 +1100792 case sAddressFamily:
793 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000794 if (!arg || *arg == '\0')
795 fatal("%s line %d: missing address family.",
796 filename, linenum);
Darren Tucker0f383232005-01-20 10:57:56 +1100797 intptr = &options->address_family;
798 if (options->listen_addrs != NULL)
799 fatal("%s line %d: address family must be specified before "
800 "ListenAddress.", filename, linenum);
801 if (strcasecmp(arg, "inet") == 0)
802 value = AF_INET;
803 else if (strcasecmp(arg, "inet6") == 0)
804 value = AF_INET6;
805 else if (strcasecmp(arg, "any") == 0)
806 value = AF_UNSPEC;
807 else
808 fatal("%s line %d: unsupported address family \"%s\".",
809 filename, linenum, arg);
810 if (*intptr == -1)
811 *intptr = value;
812 break;
813
Ben Lindstromade03f62001-12-06 18:22:17 +0000814 case sHostKeyFile:
815 intptr = &options->num_host_key_files;
816 if (*intptr >= MAX_HOSTKEYS)
817 fatal("%s line %d: too many host keys specified (max %d).",
818 filename, linenum, MAX_HOSTKEYS);
819 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +1000820 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +0000821 arg = strdelim(&cp);
822 if (!arg || *arg == '\0')
823 fatal("%s line %d: missing file name.",
824 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +1000825 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +1100826 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +0000827 /* increase optional counter */
828 if (intptr != NULL)
829 *intptr = *intptr + 1;
830 }
831 break;
832
Damien Miller0a80ca12010-02-27 07:55:05 +1100833 case sHostCertificate:
834 intptr = &options->num_host_cert_files;
835 if (*intptr >= MAX_HOSTKEYS)
836 fatal("%s line %d: too many host certificates "
837 "specified (max %d).", filename, linenum,
838 MAX_HOSTCERTS);
839 charptr = &options->host_cert_files[*intptr];
840 goto parse_filename;
841 break;
842
Ben Lindstromade03f62001-12-06 18:22:17 +0000843 case sPidFile:
844 charptr = &options->pid_file;
845 goto parse_filename;
846
847 case sPermitRootLogin:
848 intptr = &options->permit_root_login;
849 arg = strdelim(&cp);
850 if (!arg || *arg == '\0')
851 fatal("%s line %d: missing yes/"
852 "without-password/forced-commands-only/no "
853 "argument.", filename, linenum);
854 value = 0; /* silence compiler */
855 if (strcmp(arg, "without-password") == 0)
856 value = PERMIT_NO_PASSWD;
857 else if (strcmp(arg, "forced-commands-only") == 0)
858 value = PERMIT_FORCED_ONLY;
859 else if (strcmp(arg, "yes") == 0)
860 value = PERMIT_YES;
861 else if (strcmp(arg, "no") == 0)
862 value = PERMIT_NO;
863 else
864 fatal("%s line %d: Bad yes/"
865 "without-password/forced-commands-only/no "
866 "argument: %s", filename, linenum, arg);
Darren Tucker15f94272008-01-01 20:36:56 +1100867 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000868 *intptr = value;
869 break;
870
871 case sIgnoreRhosts:
872 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +1000873 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +0000874 arg = strdelim(&cp);
875 if (!arg || *arg == '\0')
876 fatal("%s line %d: missing yes/no argument.",
877 filename, linenum);
878 value = 0; /* silence compiler */
879 if (strcmp(arg, "yes") == 0)
880 value = 1;
881 else if (strcmp(arg, "no") == 0)
882 value = 0;
883 else
884 fatal("%s line %d: Bad yes/no argument: %s",
885 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +1000886 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000887 *intptr = value;
888 break;
889
890 case sIgnoreUserKnownHosts:
891 intptr = &options->ignore_user_known_hosts;
892 goto parse_flag;
893
Ben Lindstromade03f62001-12-06 18:22:17 +0000894 case sRhostsRSAAuthentication:
895 intptr = &options->rhosts_rsa_authentication;
896 goto parse_flag;
897
898 case sHostbasedAuthentication:
899 intptr = &options->hostbased_authentication;
900 goto parse_flag;
901
902 case sHostbasedUsesNameFromPacketOnly:
903 intptr = &options->hostbased_uses_name_from_packet_only;
904 goto parse_flag;
905
906 case sRSAAuthentication:
907 intptr = &options->rsa_authentication;
908 goto parse_flag;
909
910 case sPubkeyAuthentication:
911 intptr = &options->pubkey_authentication;
912 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000913
Ben Lindstromade03f62001-12-06 18:22:17 +0000914 case sKerberosAuthentication:
915 intptr = &options->kerberos_authentication;
916 goto parse_flag;
917
918 case sKerberosOrLocalPasswd:
919 intptr = &options->kerberos_or_local_passwd;
920 goto parse_flag;
921
922 case sKerberosTicketCleanup:
923 intptr = &options->kerberos_ticket_cleanup;
924 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +1000925
Darren Tucker22ef5082003-12-31 11:37:34 +1100926 case sKerberosGetAFSToken:
927 intptr = &options->kerberos_get_afs_token;
928 goto parse_flag;
929
Darren Tucker0efd1552003-08-26 11:49:55 +1000930 case sGssAuthentication:
931 intptr = &options->gss_authentication;
932 goto parse_flag;
933
934 case sGssCleanupCreds:
935 intptr = &options->gss_cleanup_creds;
936 goto parse_flag;
937
Ben Lindstromade03f62001-12-06 18:22:17 +0000938 case sPasswordAuthentication:
939 intptr = &options->password_authentication;
940 goto parse_flag;
941
Damien Miller01ed2272008-11-05 16:20:46 +1100942 case sZeroKnowledgePasswordAuthentication:
943 intptr = &options->zero_knowledge_password_authentication;
944 goto parse_flag;
945
Ben Lindstromade03f62001-12-06 18:22:17 +0000946 case sKbdInteractiveAuthentication:
947 intptr = &options->kbd_interactive_authentication;
948 goto parse_flag;
949
950 case sChallengeResponseAuthentication:
951 intptr = &options->challenge_response_authentication;
952 goto parse_flag;
953
954 case sPrintMotd:
955 intptr = &options->print_motd;
956 goto parse_flag;
957
958 case sPrintLastLog:
959 intptr = &options->print_lastlog;
960 goto parse_flag;
961
962 case sX11Forwarding:
963 intptr = &options->x11_forwarding;
964 goto parse_flag;
965
966 case sX11DisplayOffset:
967 intptr = &options->x11_display_offset;
968 goto parse_int;
969
Damien Miller95c249f2002-02-05 12:11:34 +1100970 case sX11UseLocalhost:
971 intptr = &options->x11_use_localhost;
972 goto parse_flag;
973
Ben Lindstromade03f62001-12-06 18:22:17 +0000974 case sXAuthLocation:
975 charptr = &options->xauth_location;
976 goto parse_filename;
977
978 case sStrictModes:
979 intptr = &options->strict_modes;
980 goto parse_flag;
981
Damien Miller12c150e2003-12-17 16:31:10 +1100982 case sTCPKeepAlive:
983 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +0000984 goto parse_flag;
985
986 case sEmptyPasswd:
987 intptr = &options->permit_empty_passwd;
988 goto parse_flag;
989
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000990 case sPermitUserEnvironment:
991 intptr = &options->permit_user_env;
992 goto parse_flag;
993
Ben Lindstromade03f62001-12-06 18:22:17 +0000994 case sUseLogin:
995 intptr = &options->use_login;
996 goto parse_flag;
997
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000998 case sCompression:
999 intptr = &options->compression;
Damien Miller9786e6e2005-07-26 21:54:56 +10001000 arg = strdelim(&cp);
1001 if (!arg || *arg == '\0')
1002 fatal("%s line %d: missing yes/no/delayed "
1003 "argument.", filename, linenum);
1004 value = 0; /* silence compiler */
1005 if (strcmp(arg, "delayed") == 0)
1006 value = COMP_DELAYED;
1007 else if (strcmp(arg, "yes") == 0)
1008 value = COMP_ZLIB;
1009 else if (strcmp(arg, "no") == 0)
1010 value = COMP_NONE;
1011 else
1012 fatal("%s line %d: Bad yes/no/delayed "
1013 "argument: %s", filename, linenum, arg);
1014 if (*intptr == -1)
1015 *intptr = value;
1016 break;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001017
Ben Lindstromade03f62001-12-06 18:22:17 +00001018 case sGatewayPorts:
1019 intptr = &options->gateway_ports;
Damien Millerf91ee4c2005-03-01 21:24:33 +11001020 arg = strdelim(&cp);
1021 if (!arg || *arg == '\0')
1022 fatal("%s line %d: missing yes/no/clientspecified "
1023 "argument.", filename, linenum);
1024 value = 0; /* silence compiler */
1025 if (strcmp(arg, "clientspecified") == 0)
1026 value = 2;
1027 else if (strcmp(arg, "yes") == 0)
1028 value = 1;
1029 else if (strcmp(arg, "no") == 0)
1030 value = 0;
1031 else
1032 fatal("%s line %d: Bad yes/no/clientspecified "
1033 "argument: %s", filename, linenum, arg);
Darren Tucker82347a82007-02-25 20:37:52 +11001034 if (*activep && *intptr == -1)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001035 *intptr = value;
1036 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001037
Damien Miller3a961dc2003-06-03 10:25:48 +10001038 case sUseDNS:
1039 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001040 goto parse_flag;
1041
1042 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001043 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001044 arg = strdelim(&cp);
1045 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001046 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001047 fatal("%.200s line %d: unsupported log facility '%s'",
1048 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001049 if (*log_facility_ptr == -1)
1050 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001051 break;
1052
1053 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001054 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001055 arg = strdelim(&cp);
1056 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001057 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001058 fatal("%.200s line %d: unsupported log level '%s'",
1059 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001060 if (*log_level_ptr == -1)
1061 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001062 break;
1063
1064 case sAllowTcpForwarding:
1065 intptr = &options->allow_tcp_forwarding;
1066 goto parse_flag;
1067
Damien Miller4f755cd2008-05-19 14:57:41 +10001068 case sAllowAgentForwarding:
1069 intptr = &options->allow_agent_forwarding;
1070 goto parse_flag;
1071
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001072 case sUsePrivilegeSeparation:
1073 intptr = &use_privsep;
1074 goto parse_flag;
1075
Ben Lindstromade03f62001-12-06 18:22:17 +00001076 case sAllowUsers:
1077 while ((arg = strdelim(&cp)) && *arg != '\0') {
1078 if (options->num_allow_users >= MAX_ALLOW_USERS)
1079 fatal("%s line %d: too many allow users.",
1080 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001081 options->allow_users[options->num_allow_users++] =
1082 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001083 }
1084 break;
1085
1086 case sDenyUsers:
1087 while ((arg = strdelim(&cp)) && *arg != '\0') {
1088 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001089 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001090 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001091 options->deny_users[options->num_deny_users++] =
1092 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001093 }
1094 break;
1095
1096 case sAllowGroups:
1097 while ((arg = strdelim(&cp)) && *arg != '\0') {
1098 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1099 fatal("%s line %d: too many allow groups.",
1100 filename, linenum);
Ben Lindstrome1353632002-06-23 21:29:23 +00001101 options->allow_groups[options->num_allow_groups++] =
1102 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001103 }
1104 break;
1105
1106 case sDenyGroups:
1107 while ((arg = strdelim(&cp)) && *arg != '\0') {
1108 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1109 fatal("%s line %d: too many deny groups.",
1110 filename, linenum);
1111 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
1112 }
1113 break;
1114
1115 case sCiphers:
1116 arg = strdelim(&cp);
1117 if (!arg || *arg == '\0')
1118 fatal("%s line %d: Missing argument.", filename, linenum);
1119 if (!ciphers_valid(arg))
1120 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1121 filename, linenum, arg ? arg : "<NONE>");
1122 if (options->ciphers == NULL)
1123 options->ciphers = xstrdup(arg);
1124 break;
1125
1126 case sMacs:
1127 arg = strdelim(&cp);
1128 if (!arg || *arg == '\0')
1129 fatal("%s line %d: Missing argument.", filename, linenum);
1130 if (!mac_valid(arg))
1131 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1132 filename, linenum, arg ? arg : "<NONE>");
1133 if (options->macs == NULL)
1134 options->macs = xstrdup(arg);
1135 break;
1136
Damien Millerd5f62bf2010-09-24 22:11:14 +10001137 case sKexAlgorithms:
1138 arg = strdelim(&cp);
1139 if (!arg || *arg == '\0')
1140 fatal("%s line %d: Missing argument.",
1141 filename, linenum);
1142 if (!kex_names_valid(arg))
1143 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1144 filename, linenum, arg ? arg : "<NONE>");
1145 if (options->kex_algorithms == NULL)
1146 options->kex_algorithms = xstrdup(arg);
1147 break;
1148
Ben Lindstromade03f62001-12-06 18:22:17 +00001149 case sProtocol:
1150 intptr = &options->protocol;
1151 arg = strdelim(&cp);
1152 if (!arg || *arg == '\0')
1153 fatal("%s line %d: Missing argument.", filename, linenum);
1154 value = proto_spec(arg);
1155 if (value == SSH_PROTO_UNKNOWN)
1156 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001157 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001158 if (*intptr == SSH_PROTO_UNKNOWN)
1159 *intptr = value;
1160 break;
1161
1162 case sSubsystem:
1163 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1164 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001165 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001166 }
1167 arg = strdelim(&cp);
1168 if (!arg || *arg == '\0')
1169 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001170 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001171 if (!*activep) {
1172 arg = strdelim(&cp);
1173 break;
1174 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001175 for (i = 0; i < options->num_subsystems; i++)
1176 if (strcmp(arg, options->subsystem_name[i]) == 0)
1177 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001178 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001179 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1180 arg = strdelim(&cp);
1181 if (!arg || *arg == '\0')
1182 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001183 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001184 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001185
1186 /* Collect arguments (separate to executable) */
1187 p = xstrdup(arg);
1188 len = strlen(p) + 1;
1189 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1190 len += 1 + strlen(arg);
1191 p = xrealloc(p, 1, len);
1192 strlcat(p, " ", len);
1193 strlcat(p, arg, len);
1194 }
1195 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001196 options->num_subsystems++;
1197 break;
1198
1199 case sMaxStartups:
1200 arg = strdelim(&cp);
1201 if (!arg || *arg == '\0')
1202 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001203 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001204 if ((n = sscanf(arg, "%d:%d:%d",
1205 &options->max_startups_begin,
1206 &options->max_startups_rate,
1207 &options->max_startups)) == 3) {
1208 if (options->max_startups_begin >
1209 options->max_startups ||
1210 options->max_startups_rate > 100 ||
1211 options->max_startups_rate < 1)
1212 fatal("%s line %d: Illegal MaxStartups spec.",
1213 filename, linenum);
1214 } else if (n != 1)
1215 fatal("%s line %d: Illegal MaxStartups spec.",
1216 filename, linenum);
1217 else
1218 options->max_startups = options->max_startups_begin;
1219 break;
1220
Darren Tucker89413db2004-05-24 10:36:23 +10001221 case sMaxAuthTries:
1222 intptr = &options->max_authtries;
1223 goto parse_int;
1224
Damien Miller7207f642008-05-19 15:34:50 +10001225 case sMaxSessions:
1226 intptr = &options->max_sessions;
1227 goto parse_int;
1228
Ben Lindstromade03f62001-12-06 18:22:17 +00001229 case sBanner:
1230 charptr = &options->banner;
1231 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001232
Ben Lindstromade03f62001-12-06 18:22:17 +00001233 /*
1234 * These options can contain %X options expanded at
1235 * connect time, so that you can specify paths like:
1236 *
1237 * AuthorizedKeysFile /etc/ssh_keys/%u
1238 */
1239 case sAuthorizedKeysFile:
Damien Miller30da3442010-05-10 11:58:03 +10001240 charptr = &options->authorized_keys_file;
1241 goto parse_tilde_filename;
Ben Lindstromade03f62001-12-06 18:22:17 +00001242 case sAuthorizedKeysFile2:
Damien Miller30da3442010-05-10 11:58:03 +10001243 charptr = &options->authorized_keys_file2;
1244 goto parse_tilde_filename;
1245 case sAuthorizedPrincipalsFile:
1246 charptr = &options->authorized_principals_file;
1247 parse_tilde_filename:
Damien Millerc4cb47b2010-03-22 05:52:26 +11001248 arg = strdelim(&cp);
1249 if (!arg || *arg == '\0')
1250 fatal("%s line %d: missing file name.",
1251 filename, linenum);
1252 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001253 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001254 /* increase optional counter */
1255 if (intptr != NULL)
1256 *intptr = *intptr + 1;
1257 }
1258 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001259
1260 case sClientAliveInterval:
1261 intptr = &options->client_alive_interval;
1262 goto parse_time;
1263
1264 case sClientAliveCountMax:
1265 intptr = &options->client_alive_count_max;
1266 goto parse_int;
1267
Darren Tucker46bc0752004-05-02 22:11:30 +10001268 case sAcceptEnv:
1269 while ((arg = strdelim(&cp)) && *arg != '\0') {
1270 if (strchr(arg, '=') != NULL)
1271 fatal("%s line %d: Invalid environment name.",
1272 filename, linenum);
1273 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1274 fatal("%s line %d: too many allow env.",
1275 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001276 if (!*activep)
1277 break;
Darren Tucker46bc0752004-05-02 22:11:30 +10001278 options->accept_env[options->num_accept_env++] =
1279 xstrdup(arg);
1280 }
1281 break;
1282
Damien Millerd27b9472005-12-13 19:29:02 +11001283 case sPermitTunnel:
1284 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001285 arg = strdelim(&cp);
1286 if (!arg || *arg == '\0')
1287 fatal("%s line %d: Missing yes/point-to-point/"
1288 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001289 value = -1;
1290 for (i = 0; tunmode_desc[i].val != -1; i++)
1291 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1292 value = tunmode_desc[i].val;
1293 break;
1294 }
1295 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001296 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1297 "no argument: %s", filename, linenum, arg);
1298 if (*intptr == -1)
1299 *intptr = value;
1300 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001301
Darren Tucker45150472006-07-12 22:34:17 +10001302 case sMatch:
1303 if (cmdline)
1304 fatal("Match directive not supported as a command-line "
1305 "option");
1306 value = match_cfg_line(&cp, linenum, user, host, address);
1307 if (value < 0)
1308 fatal("%s line %d: Bad Match condition", filename,
1309 linenum);
1310 *activep = value;
1311 break;
1312
Damien Miller9b439df2006-07-24 14:04:00 +10001313 case sPermitOpen:
1314 arg = strdelim(&cp);
1315 if (!arg || *arg == '\0')
1316 fatal("%s line %d: missing PermitOpen specification",
1317 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001318 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001319 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001320 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001321 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001322 options->num_permitted_opens = 0;
1323 }
Damien Miller9b439df2006-07-24 14:04:00 +10001324 break;
1325 }
Damien Millera29b95e2007-01-05 16:28:36 +11001326 if (*activep && n == -1)
1327 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001328 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1329 p = hpdelim(&arg);
1330 if (p == NULL)
1331 fatal("%s line %d: missing host in PermitOpen",
1332 filename, linenum);
1333 p = cleanhostname(p);
Damien Miller3dc71ad2009-01-28 16:31:22 +11001334 if (arg == NULL || (port = a2port(arg)) <= 0)
Damien Millera765cf42006-07-24 14:08:13 +10001335 fatal("%s line %d: bad port number in "
1336 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001337 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001338 options->num_permitted_opens =
1339 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001340 }
Damien Miller9b439df2006-07-24 14:04:00 +10001341 break;
1342
Damien Millere2754432006-07-24 14:06:47 +10001343 case sForceCommand:
1344 if (cp == NULL)
1345 fatal("%.200s line %d: Missing argument.", filename,
1346 linenum);
1347 len = strspn(cp, WHITESPACE);
1348 if (*activep && options->adm_forced_command == NULL)
1349 options->adm_forced_command = xstrdup(cp + len);
1350 return 0;
1351
Damien Millerd8cb1f12008-02-10 22:40:12 +11001352 case sChrootDirectory:
1353 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001354
1355 arg = strdelim(&cp);
1356 if (!arg || *arg == '\0')
1357 fatal("%s line %d: missing file name.",
1358 filename, linenum);
1359 if (*activep && *charptr == NULL)
1360 *charptr = xstrdup(arg);
1361 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001362
Damien Miller1aed65e2010-03-04 21:53:35 +11001363 case sTrustedUserCAKeys:
1364 charptr = &options->trusted_user_ca_keys;
1365 goto parse_filename;
1366
1367 case sRevokedKeys:
1368 charptr = &options->revoked_keys_file;
1369 goto parse_filename;
1370
Ben Lindstromade03f62001-12-06 18:22:17 +00001371 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001372 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001373 filename, linenum, arg);
1374 while (arg)
1375 arg = strdelim(&cp);
1376 break;
1377
Damien Millerf9b3feb2003-05-16 11:38:32 +10001378 case sUnsupported:
1379 logit("%s line %d: Unsupported option %s",
1380 filename, linenum, arg);
1381 while (arg)
1382 arg = strdelim(&cp);
1383 break;
1384
Ben Lindstromade03f62001-12-06 18:22:17 +00001385 default:
1386 fatal("%s line %d: Missing handler for opcode %s (%d)",
1387 filename, linenum, arg, opcode);
1388 }
1389 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1390 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1391 filename, linenum, arg);
1392 return 0;
1393}
1394
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001395/* Reads the server configuration file. */
1396
Damien Miller4af51302000-04-16 11:18:38 +10001397void
Darren Tucker645ab752004-06-25 13:33:20 +10001398load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001399{
Darren Tucker645ab752004-06-25 13:33:20 +10001400 char line[1024], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001401 FILE *f;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001402
Darren Tucker645ab752004-06-25 13:33:20 +10001403 debug2("%s: filename %s", __func__, filename);
1404 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001405 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001406 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001407 }
Darren Tucker645ab752004-06-25 13:33:20 +10001408 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001409 while (fgets(line, sizeof(line), f)) {
Darren Tucker645ab752004-06-25 13:33:20 +10001410 /*
1411 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001412 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001413 * line numbers later for error messages
1414 */
1415 if ((cp = strchr(line, '#')) != NULL)
1416 memcpy(cp, "\n", 2);
1417 cp = line + strspn(line, " \t\r");
1418
1419 buffer_append(conf, cp, strlen(cp));
1420 }
1421 buffer_append(conf, "\0", 1);
1422 fclose(f);
1423 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1424}
1425
1426void
Darren Tucker45150472006-07-12 22:34:17 +10001427parse_server_match_config(ServerOptions *options, const char *user,
1428 const char *host, const char *address)
Darren Tucker645ab752004-06-25 13:33:20 +10001429{
Darren Tucker45150472006-07-12 22:34:17 +10001430 ServerOptions mo;
1431
1432 initialize_server_options(&mo);
1433 parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
Darren Tucker1629c072007-02-19 22:25:37 +11001434 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001435}
1436
Darren Tucker1629c072007-02-19 22:25:37 +11001437/* Helper macros */
1438#define M_CP_INTOPT(n) do {\
1439 if (src->n != -1) \
1440 dst->n = src->n; \
1441} while (0)
1442#define M_CP_STROPT(n) do {\
1443 if (src->n != NULL) { \
1444 if (dst->n != NULL) \
1445 xfree(dst->n); \
1446 dst->n = src->n; \
1447 } \
1448} while(0)
1449
1450/*
1451 * Copy any supported values that are set.
1452 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001453 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001454 * array values that are not used pre-authentication, because any that we
1455 * do use must be explictly sent in mm_getpwnamallow().
1456 */
Darren Tucker45150472006-07-12 22:34:17 +10001457void
Darren Tucker1629c072007-02-19 22:25:37 +11001458copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001459{
Darren Tucker1629c072007-02-19 22:25:37 +11001460 M_CP_INTOPT(password_authentication);
1461 M_CP_INTOPT(gss_authentication);
1462 M_CP_INTOPT(rsa_authentication);
1463 M_CP_INTOPT(pubkey_authentication);
1464 M_CP_INTOPT(kerberos_authentication);
1465 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001466 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001467 M_CP_INTOPT(kbd_interactive_authentication);
Damien Miller01ed2272008-11-05 16:20:46 +11001468 M_CP_INTOPT(zero_knowledge_password_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001469 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001470 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001471
1472 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001473 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001474 M_CP_INTOPT(permit_tun);
Darren Tucker1629c072007-02-19 22:25:37 +11001475 M_CP_INTOPT(gateway_ports);
1476 M_CP_INTOPT(x11_display_offset);
1477 M_CP_INTOPT(x11_forwarding);
1478 M_CP_INTOPT(x11_use_localhost);
Damien Miller7207f642008-05-19 15:34:50 +10001479 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001480 M_CP_INTOPT(max_authtries);
Darren Tucker1629c072007-02-19 22:25:37 +11001481
1482 M_CP_STROPT(banner);
1483 if (preauth)
1484 return;
1485 M_CP_STROPT(adm_forced_command);
Damien Millerd8cb1f12008-02-10 22:40:12 +11001486 M_CP_STROPT(chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11001487 M_CP_STROPT(trusted_user_ca_keys);
1488 M_CP_STROPT(revoked_keys_file);
Damien Millerab6de352010-06-26 09:38:45 +10001489 M_CP_STROPT(authorized_keys_file);
1490 M_CP_STROPT(authorized_keys_file2);
1491 M_CP_STROPT(authorized_principals_file);
Darren Tucker45150472006-07-12 22:34:17 +10001492}
1493
Darren Tucker1629c072007-02-19 22:25:37 +11001494#undef M_CP_INTOPT
1495#undef M_CP_STROPT
1496
Darren Tucker45150472006-07-12 22:34:17 +10001497void
1498parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1499 const char *user, const char *host, const char *address)
1500{
1501 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001502 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001503
1504 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1505
Darren Tucker9fbac712004-08-12 22:41:44 +10001506 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tucker45150472006-07-12 22:34:17 +10001507 active = user ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001508 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001509 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001510 if (process_server_config_line(options, cp, filename,
Darren Tucker45150472006-07-12 22:34:17 +10001511 linenum++, &active, user, host, address) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001512 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001513 }
Darren Tucker9fbac712004-08-12 22:41:44 +10001514 xfree(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001515 if (bad_options > 0)
1516 fatal("%s: terminating, %d bad configuration options",
1517 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001518}
Darren Tuckere7140f22008-06-10 23:01:51 +10001519
1520static const char *
1521fmt_intarg(ServerOpCodes code, int val)
1522{
1523 if (code == sAddressFamily) {
1524 switch (val) {
1525 case AF_INET:
1526 return "inet";
1527 case AF_INET6:
1528 return "inet6";
1529 case AF_UNSPEC:
1530 return "any";
1531 default:
1532 return "UNKNOWN";
1533 }
1534 }
1535 if (code == sPermitRootLogin) {
1536 switch (val) {
1537 case PERMIT_NO_PASSWD:
Darren Tuckerff4350e2008-11-11 16:31:05 +11001538 return "without-password";
Darren Tuckere7140f22008-06-10 23:01:51 +10001539 case PERMIT_FORCED_ONLY:
1540 return "forced-commands-only";
1541 case PERMIT_YES:
1542 return "yes";
1543 }
1544 }
1545 if (code == sProtocol) {
1546 switch (val) {
1547 case SSH_PROTO_1:
1548 return "1";
1549 case SSH_PROTO_2:
1550 return "2";
1551 case (SSH_PROTO_1|SSH_PROTO_2):
1552 return "2,1";
1553 default:
1554 return "UNKNOWN";
1555 }
1556 }
1557 if (code == sGatewayPorts && val == 2)
1558 return "clientspecified";
1559 if (code == sCompression && val == COMP_DELAYED)
1560 return "delayed";
1561 switch (val) {
1562 case -1:
1563 return "unset";
1564 case 0:
1565 return "no";
1566 case 1:
1567 return "yes";
1568 }
1569 return "UNKNOWN";
1570}
1571
1572static const char *
1573lookup_opcode_name(ServerOpCodes code)
1574{
1575 u_int i;
1576
1577 for (i = 0; keywords[i].name != NULL; i++)
1578 if (keywords[i].opcode == code)
1579 return(keywords[i].name);
1580 return "UNKNOWN";
1581}
1582
1583static void
1584dump_cfg_int(ServerOpCodes code, int val)
1585{
1586 printf("%s %d\n", lookup_opcode_name(code), val);
1587}
1588
1589static void
1590dump_cfg_fmtint(ServerOpCodes code, int val)
1591{
1592 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
1593}
1594
1595static void
1596dump_cfg_string(ServerOpCodes code, const char *val)
1597{
1598 if (val == NULL)
1599 return;
1600 printf("%s %s\n", lookup_opcode_name(code), val);
1601}
1602
1603static void
1604dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
1605{
1606 u_int i;
1607
1608 for (i = 0; i < count; i++)
1609 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
1610}
1611
1612void
1613dump_config(ServerOptions *o)
1614{
1615 u_int i;
1616 int ret;
1617 struct addrinfo *ai;
1618 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
1619
1620 /* these are usually at the top of the config */
1621 for (i = 0; i < o->num_ports; i++)
1622 printf("port %d\n", o->ports[i]);
1623 dump_cfg_fmtint(sProtocol, o->protocol);
1624 dump_cfg_fmtint(sAddressFamily, o->address_family);
1625
1626 /* ListenAddress must be after Port */
1627 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
1628 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1629 sizeof(addr), port, sizeof(port),
1630 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1631 error("getnameinfo failed: %.100s",
1632 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
1633 strerror(errno));
1634 } else {
1635 if (ai->ai_family == AF_INET6)
1636 printf("listenaddress [%s]:%s\n", addr, port);
1637 else
1638 printf("listenaddress %s:%s\n", addr, port);
1639 }
1640 }
1641
1642 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10001643#ifdef USE_PAM
1644 dump_cfg_int(sUsePAM, o->use_pam);
1645#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001646 dump_cfg_int(sServerKeyBits, o->server_key_bits);
1647 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
1648 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
1649 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
1650 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11001651 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10001652 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
1653 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
1654
1655 /* formatted integer arguments */
1656 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
1657 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
1658 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
1659 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
1660 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
1661 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
1662 o->hostbased_uses_name_from_packet_only);
1663 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
1664 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10001665#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10001666 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
1667 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
1668 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10001669# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10001670 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10001671# endif
1672#endif
1673#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10001674 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
1675 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10001676#endif
Damien Miller01ed2272008-11-05 16:20:46 +11001677#ifdef JPAKE
1678 dump_cfg_fmtint(sZeroKnowledgePasswordAuthentication,
1679 o->zero_knowledge_password_authentication);
1680#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10001681 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
1682 dump_cfg_fmtint(sKbdInteractiveAuthentication,
1683 o->kbd_interactive_authentication);
1684 dump_cfg_fmtint(sChallengeResponseAuthentication,
1685 o->challenge_response_authentication);
1686 dump_cfg_fmtint(sPrintMotd, o->print_motd);
1687 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
1688 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
1689 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
1690 dump_cfg_fmtint(sStrictModes, o->strict_modes);
1691 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
1692 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
1693 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
1694 dump_cfg_fmtint(sUseLogin, o->use_login);
1695 dump_cfg_fmtint(sCompression, o->compression);
1696 dump_cfg_fmtint(sGatewayPorts, o->gateway_ports);
1697 dump_cfg_fmtint(sUseDNS, o->use_dns);
1698 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
1699 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
1700
1701 /* string arguments */
1702 dump_cfg_string(sPidFile, o->pid_file);
1703 dump_cfg_string(sXAuthLocation, o->xauth_location);
1704 dump_cfg_string(sCiphers, o->ciphers);
1705 dump_cfg_string(sMacs, o->macs);
1706 dump_cfg_string(sBanner, o->banner);
1707 dump_cfg_string(sAuthorizedKeysFile, o->authorized_keys_file);
1708 dump_cfg_string(sAuthorizedKeysFile2, o->authorized_keys_file2);
1709 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11001710 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11001711 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
1712 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10001713 dump_cfg_string(sAuthorizedPrincipalsFile,
1714 o->authorized_principals_file);
Darren Tuckere7140f22008-06-10 23:01:51 +10001715
1716 /* string arguments requiring a lookup */
1717 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
1718 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
1719
1720 /* string array arguments */
1721 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
1722 o->host_key_files);
Damien Miller0a80ca12010-02-27 07:55:05 +11001723 dump_cfg_strarray(sHostKeyFile, o->num_host_cert_files,
1724 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10001725 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
1726 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
1727 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
1728 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
1729 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
1730
1731 /* other arguments */
1732 for (i = 0; i < o->num_subsystems; i++)
1733 printf("subsystem %s %s\n", o->subsystem_name[i],
1734 o->subsystem_args[i]);
1735
1736 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
1737 o->max_startups_rate, o->max_startups);
1738
1739 for (i = 0; tunmode_desc[i].val != -1; i++)
1740 if (tunmode_desc[i].val == o->permit_tun) {
1741 s = tunmode_desc[i].text;
1742 break;
1743 }
1744 dump_cfg_string(sPermitTunnel, s);
1745
Darren Tuckere7140f22008-06-10 23:01:51 +10001746 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10001747}