blob: abc3c72fb3e5068508c69924a917945571b850ca [file] [log] [blame]
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002/* $OpenBSD: servconf.c,v 1.256 2014/12/21 22:27:56 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003/*
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
Damien Millere3b60b52006-07-10 21:08:03 +100016#include <sys/types.h>
17#include <sys/socket.h>
18
Damien Miller0dac6fb2010-11-20 15:19:38 +110019#include <netinet/in.h>
20#include <netinet/in_systm.h>
21#include <netinet/ip.h>
22
Darren Tucker5f96f3b2013-05-16 20:29:28 +100023#include <ctype.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100024#include <netdb.h>
Damien Miller565ca3f2006-08-19 00:23:15 +100025#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100026#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100027#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100028#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100029#include <signal.h>
Damien Millere6b3b612006-07-24 14:01:23 +100030#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100031#include <stdarg.h>
Darren Tuckere7140f22008-06-10 23:01:51 +100032#include <errno.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100033#ifdef HAVE_UTIL_H
Darren Tuckerb7ee8522013-05-16 20:33:10 +100034#include <util.h>
Darren Tuckere194ba42013-05-16 20:47:31 +100035#endif
Damien Millerbe43ebf2006-07-24 13:51:51 +100036
Damien Millerb84886b2008-05-19 15:05:07 +100037#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include "log.h"
Damien Millerd7834352006-08-05 12:39:39 +100041#include "buffer.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100042#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043#include "servconf.h"
Damien Miller78928792000-04-12 20:17:38 +100044#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100047#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000048#include "kex.h"
49#include "mac.h"
Darren Tucker45150472006-07-12 22:34:17 +100050#include "match.h"
Damien Miller9b439df2006-07-24 14:04:00 +100051#include "channels.h"
Damien Miller565ca3f2006-08-19 00:23:15 +100052#include "groupaccess.h"
Darren Tuckerfbcf8272012-05-19 19:37:01 +100053#include "canohost.h"
54#include "packet.h"
Damien Millera6e3f012012-11-04 23:21:40 +110055#include "hostfile.h"
56#include "auth.h"
djm@openbsd.org57d378e2014-08-19 23:58:28 +000057#include "myproposal.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000058#include "digest.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000059
Damien Miller3dc71ad2009-01-28 16:31:22 +110060static void add_listen_addr(ServerOptions *, char *, int);
61static void add_one_listen_addr(ServerOptions *, char *, int);
Damien Miller34132e52000-01-14 15:45:46 +110062
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000063/* Use of privilege separation or not */
64extern int use_privsep;
Darren Tucker45150472006-07-12 22:34:17 +100065extern Buffer cfg;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067/* Initializes the server options to their default values. */
68
Damien Miller4af51302000-04-16 11:18:38 +100069void
Damien Miller95def091999-11-25 00:26:21 +110070initialize_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071{
Damien Miller95def091999-11-25 00:26:21 +110072 memset(options, 0, sizeof(*options));
Damien Miller726273e2001-11-12 11:40:11 +110073
74 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +100075 options->use_pam = -1;
Damien Miller726273e2001-11-12 11:40:11 +110076
77 /* Standard Options */
Damien Miller34132e52000-01-14 15:45:46 +110078 options->num_ports = 0;
79 options->ports_from_cmdline = 0;
80 options->listen_addrs = NULL;
Darren Tucker0f383232005-01-20 10:57:56 +110081 options->address_family = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +110082 options->num_host_key_files = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +110083 options->num_host_cert_files = 0;
Damien Miller85b45e02013-07-20 13:21:52 +100084 options->host_key_agent = NULL;
Damien Miller6f83b8e2000-05-02 09:23:45 +100085 options->pid_file = NULL;
Damien Miller95def091999-11-25 00:26:21 +110086 options->server_key_bits = -1;
87 options->login_grace_time = -1;
88 options->key_regeneration_time = -1;
Ben Lindstromd8a90212001-02-15 03:08:27 +000089 options->permit_root_login = PERMIT_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +110090 options->ignore_rhosts = -1;
91 options->ignore_user_known_hosts = -1;
92 options->print_motd = -1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +000093 options->print_lastlog = -1;
Damien Miller95def091999-11-25 00:26:21 +110094 options->x11_forwarding = -1;
95 options->x11_display_offset = -1;
Damien Miller95c249f2002-02-05 12:11:34 +110096 options->x11_use_localhost = -1;
Damien Miller5ff30c62013-10-30 22:21:50 +110097 options->permit_tty = -1;
Damien Miller72e6b5c2014-07-04 09:00:04 +100098 options->permit_user_rc = -1;
Damien Millerd3a18572000-06-07 19:55:44 +100099 options->xauth_location = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100100 options->strict_modes = -1;
Damien Miller12c150e2003-12-17 16:31:10 +1100101 options->tcp_keep_alive = -1;
Damien Millerfcd93202002-02-05 12:26:34 +1100102 options->log_facility = SYSLOG_FACILITY_NOT_SET;
103 options->log_level = SYSLOG_LEVEL_NOT_SET;
Damien Miller95def091999-11-25 00:26:21 +1100104 options->rhosts_rsa_authentication = -1;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000105 options->hostbased_authentication = -1;
106 options->hostbased_uses_name_from_packet_only = -1;
Damien Miller95def091999-11-25 00:26:21 +1100107 options->rsa_authentication = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100108 options->pubkey_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100109 options->kerberos_authentication = -1;
110 options->kerberos_or_local_passwd = -1;
111 options->kerberos_ticket_cleanup = -1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100112 options->kerberos_get_afs_token = -1;
Darren Tucker0efd1552003-08-26 11:49:55 +1000113 options->gss_authentication=-1;
114 options->gss_cleanup_creds = -1;
Damien Miller95def091999-11-25 00:26:21 +1100115 options->password_authentication = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100116 options->kbd_interactive_authentication = -1;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000117 options->challenge_response_authentication = -1;
Damien Miller95def091999-11-25 00:26:21 +1100118 options->permit_empty_passwd = -1;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000119 options->permit_user_env = -1;
Damien Miller95def091999-11-25 00:26:21 +1100120 options->use_login = -1;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000121 options->compression = -1;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000122 options->rekey_limit = -1;
123 options->rekey_interval = -1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100124 options->allow_tcp_forwarding = -1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000125 options->allow_streamlocal_forwarding = -1;
Damien Miller4f755cd2008-05-19 14:57:41 +1000126 options->allow_agent_forwarding = -1;
Damien Miller95def091999-11-25 00:26:21 +1100127 options->num_allow_users = 0;
128 options->num_deny_users = 0;
129 options->num_allow_groups = 0;
130 options->num_deny_groups = 0;
Damien Miller78928792000-04-12 20:17:38 +1000131 options->ciphers = NULL;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000132 options->macs = NULL;
Damien Millerd5f62bf2010-09-24 22:11:14 +1000133 options->kex_algorithms = NULL;
Damien Miller78928792000-04-12 20:17:38 +1000134 options->protocol = SSH_PROTO_UNKNOWN;
Damien Miller7acefbb2014-07-18 14:11:24 +1000135 options->fwd_opts.gateway_ports = -1;
136 options->fwd_opts.streamlocal_bind_mask = (mode_t)-1;
137 options->fwd_opts.streamlocal_bind_unlink = -1;
Damien Millerf6d9e222000-06-18 14:50:44 +1000138 options->num_subsystems = 0;
Damien Miller942da032000-08-18 13:59:06 +1000139 options->max_startups_begin = -1;
140 options->max_startups_rate = -1;
Damien Miller37023962000-07-11 17:31:38 +1000141 options->max_startups = -1;
Darren Tucker89413db2004-05-24 10:36:23 +1000142 options->max_authtries = -1;
Damien Miller7207f642008-05-19 15:34:50 +1000143 options->max_sessions = -1;
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000144 options->banner = NULL;
Damien Miller3a961dc2003-06-03 10:25:48 +1000145 options->use_dns = -1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000146 options->client_alive_interval = -1;
147 options->client_alive_count_max = -1;
Damien Millerd8478b62011-05-29 21:39:36 +1000148 options->num_authkeys_files = 0;
Darren Tucker46bc0752004-05-02 22:11:30 +1000149 options->num_accept_env = 0;
Damien Millerd27b9472005-12-13 19:29:02 +1100150 options->permit_tun = -1;
Damien Millera765cf42006-07-24 14:08:13 +1000151 options->num_permitted_opens = -1;
Damien Millere2754432006-07-24 14:06:47 +1000152 options->adm_forced_command = NULL;
Damien Millerd8cb1f12008-02-10 22:40:12 +1100153 options->chroot_directory = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +1100154 options->authorized_keys_command = NULL;
155 options->authorized_keys_command_user = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100156 options->revoked_keys_file = NULL;
157 options->trusted_user_ca_keys = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000158 options->authorized_principals_file = NULL;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100159 options->ip_qos_interactive = -1;
160 options->ip_qos_bulk = -1;
Damien Miller23528812012-04-22 11:24:43 +1000161 options->version_addendum = NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000162 options->fingerprint_hash = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163}
164
Damien Miller4af51302000-04-16 11:18:38 +1000165void
Damien Miller95def091999-11-25 00:26:21 +1100166fill_default_server_options(ServerOptions *options)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167{
Damien Miller726273e2001-11-12 11:40:11 +1100168 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000169 if (options->use_pam == -1)
Damien Miller5c3a5582003-09-23 22:12:38 +1000170 options->use_pam = 0;
Damien Miller726273e2001-11-12 11:40:11 +1100171
172 /* Standard Options */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100173 if (options->protocol == SSH_PROTO_UNKNOWN)
Darren Tuckerbad50762009-10-11 21:51:08 +1100174 options->protocol = SSH_PROTO_2;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100175 if (options->num_host_key_files == 0) {
176 /* fill default hostkeys for protocols */
177 if (options->protocol & SSH_PROTO_1)
Damien Miller7fc23732002-01-22 23:19:11 +1100178 options->host_key_files[options->num_host_key_files++] =
179 _PATH_HOST_KEY_FILE;
180 if (options->protocol & SSH_PROTO_2) {
181 options->host_key_files[options->num_host_key_files++] =
182 _PATH_HOST_RSA_KEY_FILE;
183 options->host_key_files[options->num_host_key_files++] =
184 _PATH_HOST_DSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100185#ifdef OPENSSL_HAS_ECC
Damien Millere13cadf2010-09-10 11:15:33 +1000186 options->host_key_files[options->num_host_key_files++] =
187 _PATH_HOST_ECDSA_KEY_FILE;
Damien Millerdd190dd2010-11-11 14:17:02 +1100188#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +1100189 options->host_key_files[options->num_host_key_files++] =
190 _PATH_HOST_ED25519_KEY_FILE;
Damien Miller7fc23732002-01-22 23:19:11 +1100191 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100192 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100193 /* No certificates by default */
Damien Miller34132e52000-01-14 15:45:46 +1100194 if (options->num_ports == 0)
195 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
196 if (options->listen_addrs == NULL)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000197 add_listen_addr(options, NULL, 0);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000198 if (options->pid_file == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000199 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
Damien Miller95def091999-11-25 00:26:21 +1100200 if (options->server_key_bits == -1)
Darren Tucker7499b0c2008-07-02 22:35:43 +1000201 options->server_key_bits = 1024;
Damien Miller95def091999-11-25 00:26:21 +1100202 if (options->login_grace_time == -1)
Damien Millerc1348632002-09-05 14:35:14 +1000203 options->login_grace_time = 120;
Damien Miller95def091999-11-25 00:26:21 +1100204 if (options->key_regeneration_time == -1)
205 options->key_regeneration_time = 3600;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000206 if (options->permit_root_login == PERMIT_NOT_SET)
207 options->permit_root_login = PERMIT_YES;
Damien Miller95def091999-11-25 00:26:21 +1100208 if (options->ignore_rhosts == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100209 options->ignore_rhosts = 1;
Damien Miller95def091999-11-25 00:26:21 +1100210 if (options->ignore_user_known_hosts == -1)
211 options->ignore_user_known_hosts = 0;
Damien Miller95def091999-11-25 00:26:21 +1100212 if (options->print_motd == -1)
213 options->print_motd = 1;
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000214 if (options->print_lastlog == -1)
215 options->print_lastlog = 1;
Damien Miller95def091999-11-25 00:26:21 +1100216 if (options->x11_forwarding == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100217 options->x11_forwarding = 0;
Damien Miller95def091999-11-25 00:26:21 +1100218 if (options->x11_display_offset == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100219 options->x11_display_offset = 10;
Damien Miller95c249f2002-02-05 12:11:34 +1100220 if (options->x11_use_localhost == -1)
221 options->x11_use_localhost = 1;
Damien Millerd3a18572000-06-07 19:55:44 +1000222 if (options->xauth_location == NULL)
Ben Lindstrom1bf11f62001-06-09 01:48:01 +0000223 options->xauth_location = _PATH_XAUTH;
Damien Miller5ff30c62013-10-30 22:21:50 +1100224 if (options->permit_tty == -1)
225 options->permit_tty = 1;
Damien Miller72e6b5c2014-07-04 09:00:04 +1000226 if (options->permit_user_rc == -1)
227 options->permit_user_rc = 1;
Damien Miller95def091999-11-25 00:26:21 +1100228 if (options->strict_modes == -1)
229 options->strict_modes = 1;
Damien Miller12c150e2003-12-17 16:31:10 +1100230 if (options->tcp_keep_alive == -1)
231 options->tcp_keep_alive = 1;
Damien Millerfcd93202002-02-05 12:26:34 +1100232 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
Damien Miller95def091999-11-25 00:26:21 +1100233 options->log_facility = SYSLOG_FACILITY_AUTH;
Damien Millerfcd93202002-02-05 12:26:34 +1100234 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000235 options->log_level = SYSLOG_LEVEL_INFO;
Damien Miller95def091999-11-25 00:26:21 +1100236 if (options->rhosts_rsa_authentication == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100237 options->rhosts_rsa_authentication = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000238 if (options->hostbased_authentication == -1)
239 options->hostbased_authentication = 0;
240 if (options->hostbased_uses_name_from_packet_only == -1)
241 options->hostbased_uses_name_from_packet_only = 0;
Damien Miller95def091999-11-25 00:26:21 +1100242 if (options->rsa_authentication == -1)
243 options->rsa_authentication = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100244 if (options->pubkey_authentication == -1)
245 options->pubkey_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100246 if (options->kerberos_authentication == -1)
Damien Millerd7de14b2002-04-23 21:04:51 +1000247 options->kerberos_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100248 if (options->kerberos_or_local_passwd == -1)
249 options->kerberos_or_local_passwd = 1;
250 if (options->kerberos_ticket_cleanup == -1)
251 options->kerberos_ticket_cleanup = 1;
Darren Tucker22ef5082003-12-31 11:37:34 +1100252 if (options->kerberos_get_afs_token == -1)
253 options->kerberos_get_afs_token = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000254 if (options->gss_authentication == -1)
255 options->gss_authentication = 0;
256 if (options->gss_cleanup_creds == -1)
257 options->gss_cleanup_creds = 1;
Damien Miller95def091999-11-25 00:26:21 +1100258 if (options->password_authentication == -1)
259 options->password_authentication = 1;
Damien Miller874d77b2000-10-14 16:23:11 +1100260 if (options->kbd_interactive_authentication == -1)
261 options->kbd_interactive_authentication = 0;
Ben Lindstrom551ea372001-06-05 18:56:16 +0000262 if (options->challenge_response_authentication == -1)
263 options->challenge_response_authentication = 1;
Damien Miller95def091999-11-25 00:26:21 +1100264 if (options->permit_empty_passwd == -1)
Damien Miller98c7ad62000-03-09 21:27:49 +1100265 options->permit_empty_passwd = 0;
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000266 if (options->permit_user_env == -1)
267 options->permit_user_env = 0;
Damien Miller95def091999-11-25 00:26:21 +1100268 if (options->use_login == -1)
269 options->use_login = 0;
Ben Lindstrom23e0f662002-06-21 01:09:47 +0000270 if (options->compression == -1)
Damien Miller9786e6e2005-07-26 21:54:56 +1000271 options->compression = COMP_DELAYED;
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000272 if (options->rekey_limit == -1)
273 options->rekey_limit = 0;
274 if (options->rekey_interval == -1)
275 options->rekey_interval = 0;
Damien Miller50a41ed2000-10-16 12:14:42 +1100276 if (options->allow_tcp_forwarding == -1)
Damien Milleraa5b3f82012-12-03 09:50:54 +1100277 options->allow_tcp_forwarding = FORWARD_ALLOW;
Damien Miller7acefbb2014-07-18 14:11:24 +1000278 if (options->allow_streamlocal_forwarding == -1)
279 options->allow_streamlocal_forwarding = FORWARD_ALLOW;
Damien Miller4f755cd2008-05-19 14:57:41 +1000280 if (options->allow_agent_forwarding == -1)
281 options->allow_agent_forwarding = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +1000282 if (options->fwd_opts.gateway_ports == -1)
283 options->fwd_opts.gateway_ports = 0;
Damien Miller37023962000-07-11 17:31:38 +1000284 if (options->max_startups == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100285 options->max_startups = 100;
Damien Miller942da032000-08-18 13:59:06 +1000286 if (options->max_startups_rate == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100287 options->max_startups_rate = 30; /* 30% */
Damien Miller942da032000-08-18 13:59:06 +1000288 if (options->max_startups_begin == -1)
Damien Miller1f583df2013-02-12 11:02:08 +1100289 options->max_startups_begin = 10;
Darren Tucker89413db2004-05-24 10:36:23 +1000290 if (options->max_authtries == -1)
291 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
Damien Miller7207f642008-05-19 15:34:50 +1000292 if (options->max_sessions == -1)
293 options->max_sessions = DEFAULT_SESSIONS_MAX;
Damien Miller3a961dc2003-06-03 10:25:48 +1000294 if (options->use_dns == -1)
295 options->use_dns = 1;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000296 if (options->client_alive_interval == -1)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100297 options->client_alive_interval = 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +0000298 if (options->client_alive_count_max == -1)
299 options->client_alive_count_max = 3;
Damien Millerd8478b62011-05-29 21:39:36 +1000300 if (options->num_authkeys_files == 0) {
301 options->authorized_keys_files[options->num_authkeys_files++] =
302 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS);
303 options->authorized_keys_files[options->num_authkeys_files++] =
304 xstrdup(_PATH_SSH_USER_PERMITTED_KEYS2);
305 }
Damien Millerd27b9472005-12-13 19:29:02 +1100306 if (options->permit_tun == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100307 options->permit_tun = SSH_TUNMODE_NO;
Damien Miller0dac6fb2010-11-20 15:19:38 +1100308 if (options->ip_qos_interactive == -1)
309 options->ip_qos_interactive = IPTOS_LOWDELAY;
310 if (options->ip_qos_bulk == -1)
311 options->ip_qos_bulk = IPTOS_THROUGHPUT;
Damien Miller23528812012-04-22 11:24:43 +1000312 if (options->version_addendum == NULL)
313 options->version_addendum = xstrdup("");
Damien Miller7acefbb2014-07-18 14:11:24 +1000314 if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
315 options->fwd_opts.streamlocal_bind_mask = 0177;
316 if (options->fwd_opts.streamlocal_bind_unlink == -1)
317 options->fwd_opts.streamlocal_bind_unlink = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000318 if (options->fingerprint_hash == -1)
319 options->fingerprint_hash = SSH_FP_HASH_DEFAULT;
Ben Lindstromfb62a692002-06-06 19:47:11 +0000320 /* Turn privilege separation on by default */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000321 if (use_privsep == -1)
Damien Miller5a5c2b92012-07-31 12:21:34 +1000322 use_privsep = PRIVSEP_NOSANDBOX;
Damien Miller4903eb42002-06-21 16:20:44 +1000323
Tim Rice40017b02002-07-14 13:36:49 -0700324#ifndef HAVE_MMAP
Damien Miller4903eb42002-06-21 16:20:44 +1000325 if (use_privsep && options->compression == 1) {
326 error("This platform does not support both privilege "
327 "separation and compression");
328 error("Compression disabled");
329 options->compression = 0;
330 }
331#endif
332
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333}
334
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335/* Keyword tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100336typedef enum {
337 sBadOption, /* == unknown option */
Damien Miller726273e2001-11-12 11:40:11 +1100338 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000339 sUsePAM,
Damien Miller726273e2001-11-12 11:40:11 +1100340 /* Standard Options */
Damien Miller95def091999-11-25 00:26:21 +1100341 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
342 sPermitRootLogin, sLogFacility, sLogLevel,
Darren Tuckerec960f22003-08-13 20:37:05 +1000343 sRhostsRSAAuthentication, sRSAAuthentication,
Damien Miller95def091999-11-25 00:26:21 +1100344 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
Darren Tucker22ef5082003-12-31 11:37:34 +1100345 sKerberosGetAFSToken,
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000346 sKerberosTgtPassing, sChallengeResponseAuthentication,
Darren Tucker0f383232005-01-20 10:57:56 +1100347 sPasswordAuthentication, sKbdInteractiveAuthentication,
348 sListenAddress, sAddressFamily,
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000349 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
Damien Miller95c249f2002-02-05 12:11:34 +1100350 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
Damien Miller5ff30c62013-10-30 22:21:50 +1100351 sPermitTTY, sStrictModes, sEmptyPasswd, sTCPKeepAlive,
Ben Lindstrom5d860f02002-08-01 01:28:38 +0000352 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000353 sRekeyLimit, sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000354 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
Darren Tucker89413db2004-05-24 10:36:23 +1000355 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
Damien Miller7207f642008-05-19 15:34:50 +1000356 sMaxStartups, sMaxAuthTries, sMaxSessions,
Damien Miller3a961dc2003-06-03 10:25:48 +1000357 sBanner, sUseDNS, sHostbasedAuthentication,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100358 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
Damien Millerec2eaa32011-05-20 18:57:14 +1000359 sClientAliveCountMax, sAuthorizedKeysFile,
Damien Millerd27b9472005-12-13 19:29:02 +1100360 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
Damien Millerd8cb1f12008-02-10 22:40:12 +1100361 sMatch, sPermitOpen, sForceCommand, sChrootDirectory,
Darren Tucker7bd98e72010-01-10 10:31:12 +1100362 sUsePrivilegeSeparation, sAllowAgentForwarding,
Damien Miller7cc194f2014-02-04 11:12:56 +1100363 sHostCertificate,
Damien Miller30da3442010-05-10 11:58:03 +1000364 sRevokedKeys, sTrustedUserCAKeys, sAuthorizedPrincipalsFile,
Damien Miller23528812012-04-22 11:24:43 +1000365 sKexAlgorithms, sIPQoS, sVersionAddendum,
Damien Miller09d3e122012-10-31 08:58:58 +1100366 sAuthorizedKeysCommand, sAuthorizedKeysCommandUser,
Damien Miller72e6b5c2014-07-04 09:00:04 +1000367 sAuthenticationMethods, sHostKeyAgent, sPermitUserRC,
Damien Miller7acefbb2014-07-18 14:11:24 +1000368 sStreamLocalBindMask, sStreamLocalBindUnlink,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000369 sAllowStreamLocalForwarding, sFingerprintHash,
Damien Millerf9b3feb2003-05-16 11:38:32 +1000370 sDeprecated, sUnsupported
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371} ServerOpCodes;
372
Darren Tucker45150472006-07-12 22:34:17 +1000373#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
374#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
375#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
376
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377/* Textual representation of the tokens. */
Damien Miller95def091999-11-25 00:26:21 +1100378static struct {
379 const char *name;
380 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000381 u_int flags;
Damien Miller95def091999-11-25 00:26:21 +1100382} keywords[] = {
Damien Miller726273e2001-11-12 11:40:11 +1100383 /* Portable-specific options */
Damien Miller6ac2c482003-05-16 11:42:35 +1000384#ifdef USE_PAM
Darren Tucker45150472006-07-12 22:34:17 +1000385 { "usepam", sUsePAM, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000386#else
Darren Tucker45150472006-07-12 22:34:17 +1000387 { "usepam", sUnsupported, SSHCFG_GLOBAL },
Damien Miller6ac2c482003-05-16 11:42:35 +1000388#endif
Darren Tucker45150472006-07-12 22:34:17 +1000389 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
Damien Miller726273e2001-11-12 11:40:11 +1100390 /* Standard Options */
Darren Tucker45150472006-07-12 22:34:17 +1000391 { "port", sPort, SSHCFG_GLOBAL },
392 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
393 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
Damien Miller85b45e02013-07-20 13:21:52 +1000394 { "hostkeyagent", sHostKeyAgent, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000395 { "pidfile", sPidFile, SSHCFG_GLOBAL },
396 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
397 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
398 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
Darren Tucker15f94272008-01-01 20:36:56 +1100399 { "permitrootlogin", sPermitRootLogin, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000400 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
401 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
402 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
Darren Tucker1629c072007-02-19 22:25:37 +1100403 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
404 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000405 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100406 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
407 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000408 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
Darren Tucker6aaa58c2003-08-02 22:24:49 +1000409#ifdef KRB5
Darren Tucker1629c072007-02-19 22:25:37 +1100410 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000411 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
412 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
Darren Tucker3c78c5e2004-01-23 22:03:10 +1100413#ifdef USE_AFS
Darren Tucker45150472006-07-12 22:34:17 +1000414 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000415#else
Darren Tucker45150472006-07-12 22:34:17 +1000416 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker409cb322004-01-05 22:36:51 +1100417#endif
418#else
Darren Tucker1629c072007-02-19 22:25:37 +1100419 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000420 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
421 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
422 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
Damien Millerf9b3feb2003-05-16 11:38:32 +1000423#endif
Darren Tucker45150472006-07-12 22:34:17 +1000424 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
425 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000426#ifdef GSSAPI
Darren Tucker1629c072007-02-19 22:25:37 +1100427 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000428 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000429#else
Darren Tucker1629c072007-02-19 22:25:37 +1100430 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000431 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
Darren Tucker0efd1552003-08-26 11:49:55 +1000432#endif
Darren Tucker1629c072007-02-19 22:25:37 +1100433 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
434 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
Darren Tucker1d75f222007-03-01 21:31:28 +1100435 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000436 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
437 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
438 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
439 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
440 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
441 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
442 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
443 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
Damien Millerd1de9952006-07-24 14:05:48 +1000444 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
445 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
446 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000447 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
448 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
Damien Miller51bde602008-11-03 19:23:10 +1100449 { "permitemptypasswords", sEmptyPasswd, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000450 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
451 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
452 { "compression", sCompression, SSHCFG_GLOBAL },
Darren Tucker5f96f3b2013-05-16 20:29:28 +1000453 { "rekeylimit", sRekeyLimit, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000454 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
455 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
456 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
Damien Miller4f755cd2008-05-19 14:57:41 +1000457 { "allowagentforwarding", sAllowAgentForwarding, SSHCFG_ALL },
Damien Millerc24da772012-06-20 21:53:58 +1000458 { "allowusers", sAllowUsers, SSHCFG_ALL },
459 { "denyusers", sDenyUsers, SSHCFG_ALL },
460 { "allowgroups", sAllowGroups, SSHCFG_ALL },
461 { "denygroups", sDenyGroups, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000462 { "ciphers", sCiphers, SSHCFG_GLOBAL },
463 { "macs", sMacs, SSHCFG_GLOBAL },
464 { "protocol", sProtocol, SSHCFG_GLOBAL },
465 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
466 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
467 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
Damien Miller307c1d12008-06-16 07:56:20 +1000468 { "maxauthtries", sMaxAuthTries, SSHCFG_ALL },
Damien Miller7207f642008-05-19 15:34:50 +1000469 { "maxsessions", sMaxSessions, SSHCFG_ALL },
Darren Tucker1629c072007-02-19 22:25:37 +1100470 { "banner", sBanner, SSHCFG_ALL },
Darren Tucker45150472006-07-12 22:34:17 +1000471 { "usedns", sUseDNS, SSHCFG_GLOBAL },
472 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
473 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
474 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
475 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
Damien Millerab6de352010-06-26 09:38:45 +1000476 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_ALL },
Damien Millerd8478b62011-05-29 21:39:36 +1000477 { "authorizedkeysfile2", sDeprecated, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000478 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL},
Damien Millerc24da772012-06-20 21:53:58 +1000479 { "acceptenv", sAcceptEnv, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000480 { "permittunnel", sPermitTunnel, SSHCFG_ALL },
Damien Miller5ff30c62013-10-30 22:21:50 +1100481 { "permittty", sPermitTTY, SSHCFG_ALL },
Damien Miller72e6b5c2014-07-04 09:00:04 +1000482 { "permituserrc", sPermitUserRC, SSHCFG_ALL },
Darren Tucker64cee362009-06-21 20:26:17 +1000483 { "match", sMatch, SSHCFG_ALL },
Damien Miller9b439df2006-07-24 14:04:00 +1000484 { "permitopen", sPermitOpen, SSHCFG_ALL },
Damien Millere2754432006-07-24 14:06:47 +1000485 { "forcecommand", sForceCommand, SSHCFG_ALL },
Damien Millerd8cb1f12008-02-10 22:40:12 +1100486 { "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
Damien Miller0a80ca12010-02-27 07:55:05 +1100487 { "hostcertificate", sHostCertificate, SSHCFG_GLOBAL },
Damien Miller1aed65e2010-03-04 21:53:35 +1100488 { "revokedkeys", sRevokedKeys, SSHCFG_ALL },
489 { "trustedusercakeys", sTrustedUserCAKeys, SSHCFG_ALL },
Damien Millerab6de352010-06-26 09:38:45 +1000490 { "authorizedprincipalsfile", sAuthorizedPrincipalsFile, SSHCFG_ALL },
Damien Millerd5f62bf2010-09-24 22:11:14 +1000491 { "kexalgorithms", sKexAlgorithms, SSHCFG_GLOBAL },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100492 { "ipqos", sIPQoS, SSHCFG_ALL },
Damien Miller09d3e122012-10-31 08:58:58 +1100493 { "authorizedkeyscommand", sAuthorizedKeysCommand, SSHCFG_ALL },
494 { "authorizedkeyscommanduser", sAuthorizedKeysCommandUser, SSHCFG_ALL },
Damien Miller23528812012-04-22 11:24:43 +1000495 { "versionaddendum", sVersionAddendum, SSHCFG_GLOBAL },
Damien Millera6e3f012012-11-04 23:21:40 +1100496 { "authenticationmethods", sAuthenticationMethods, SSHCFG_ALL },
Damien Miller7acefbb2014-07-18 14:11:24 +1000497 { "streamlocalbindmask", sStreamLocalBindMask, SSHCFG_ALL },
498 { "streamlocalbindunlink", sStreamLocalBindUnlink, SSHCFG_ALL },
499 { "allowstreamlocalforwarding", sAllowStreamLocalForwarding, SSHCFG_ALL },
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000500 { "fingerprinthash", sFingerprintHash, SSHCFG_GLOBAL },
Darren Tucker45150472006-07-12 22:34:17 +1000501 { NULL, sBadOption, 0 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502};
503
Darren Tuckere7140f22008-06-10 23:01:51 +1000504static struct {
505 int val;
506 char *text;
507} tunmode_desc[] = {
508 { SSH_TUNMODE_NO, "no" },
509 { SSH_TUNMODE_POINTOPOINT, "point-to-point" },
510 { SSH_TUNMODE_ETHERNET, "ethernet" },
511 { SSH_TUNMODE_YES, "yes" },
512 { -1, NULL }
513};
514
Damien Miller5428f641999-11-25 11:54:57 +1100515/*
Ben Lindstrom3704c262001-04-02 18:20:03 +0000516 * Returns the number of the token pointed to by cp or sBadOption.
Damien Miller5428f641999-11-25 11:54:57 +1100517 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518
Damien Miller4af51302000-04-16 11:18:38 +1000519static ServerOpCodes
Damien Miller95def091999-11-25 00:26:21 +1100520parse_token(const char *cp, const char *filename,
Darren Tucker45150472006-07-12 22:34:17 +1000521 int linenum, u_int *flags)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000523 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524
Damien Miller95def091999-11-25 00:26:21 +1100525 for (i = 0; keywords[i].name; i++)
Darren Tucker45150472006-07-12 22:34:17 +1000526 if (strcasecmp(cp, keywords[i].name) == 0) {
527 *flags = keywords[i].flags;
Damien Miller95def091999-11-25 00:26:21 +1100528 return keywords[i].opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000529 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530
Ben Lindstromb5cdc662001-04-16 02:13:26 +0000531 error("%s: line %d: Bad configuration option: %s",
532 filename, linenum, cp);
Damien Miller95def091999-11-25 00:26:21 +1100533 return sBadOption;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534}
535
Darren Tucker88b6fb22010-01-13 22:44:29 +1100536char *
537derelativise_path(const char *path)
538{
Damien Miller44451d02010-03-26 10:40:04 +1100539 char *expanded, *ret, cwd[MAXPATHLEN];
Darren Tucker88b6fb22010-01-13 22:44:29 +1100540
541 expanded = tilde_expand_filename(path, getuid());
542 if (*expanded == '/')
543 return expanded;
Damien Miller44451d02010-03-26 10:40:04 +1100544 if (getcwd(cwd, sizeof(cwd)) == NULL)
Darren Tucker88b6fb22010-01-13 22:44:29 +1100545 fatal("%s: getcwd: %s", __func__, strerror(errno));
546 xasprintf(&ret, "%s/%s", cwd, expanded);
Darren Tuckera627d422013-06-02 07:31:17 +1000547 free(expanded);
Darren Tucker88b6fb22010-01-13 22:44:29 +1100548 return ret;
549}
550
Ben Lindstrombba81212001-06-25 05:01:22 +0000551static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100552add_listen_addr(ServerOptions *options, char *addr, int port)
Damien Miller34132e52000-01-14 15:45:46 +1100553{
Damien Millereccb9de2005-06-17 12:59:34 +1000554 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100555
556 if (options->num_ports == 0)
557 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
Darren Tucker0f383232005-01-20 10:57:56 +1100558 if (options->address_family == -1)
559 options->address_family = AF_UNSPEC;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000560 if (port == 0)
Ben Lindstromc510af42001-04-07 17:25:48 +0000561 for (i = 0; i < options->num_ports; i++)
562 add_one_listen_addr(options, addr, options->ports[i]);
563 else
Ben Lindstrom19066a12001-04-12 23:39:26 +0000564 add_one_listen_addr(options, addr, port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000565}
566
Ben Lindstrombba81212001-06-25 05:01:22 +0000567static void
Damien Miller3dc71ad2009-01-28 16:31:22 +1100568add_one_listen_addr(ServerOptions *options, char *addr, int port)
Ben Lindstromc510af42001-04-07 17:25:48 +0000569{
570 struct addrinfo hints, *ai, *aitop;
571 char strport[NI_MAXSERV];
572 int gaierr;
573
574 memset(&hints, 0, sizeof(hints));
Darren Tucker0f383232005-01-20 10:57:56 +1100575 hints.ai_family = options->address_family;
Ben Lindstromc510af42001-04-07 17:25:48 +0000576 hints.ai_socktype = SOCK_STREAM;
577 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100578 snprintf(strport, sizeof strport, "%d", port);
Ben Lindstromc510af42001-04-07 17:25:48 +0000579 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
580 fatal("bad addr or host: %s (%s)",
581 addr ? addr : "<NULL>",
Darren Tucker4abde772007-12-29 02:43:51 +1100582 ssh_gai_strerror(gaierr));
Ben Lindstromc510af42001-04-07 17:25:48 +0000583 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
584 ;
585 ai->ai_next = options->listen_addrs;
586 options->listen_addrs = aitop;
Damien Miller34132e52000-01-14 15:45:46 +1100587}
588
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000589struct connection_info *
590get_connection_info(int populate, int use_dns)
591{
592 static struct connection_info ci;
593
594 if (!populate)
595 return &ci;
596 ci.host = get_canonical_hostname(use_dns);
597 ci.address = get_remote_ipaddr();
598 ci.laddress = get_local_ipaddr(packet_get_connection_in());
599 ci.lport = get_local_port();
600 return &ci;
601}
602
Darren Tucker45150472006-07-12 22:34:17 +1000603/*
604 * The strategy for the Match blocks is that the config file is parsed twice.
605 *
606 * The first time is at startup. activep is initialized to 1 and the
607 * directives in the global context are processed and acted on. Hitting a
608 * Match directive unsets activep and the directives inside the block are
609 * checked for syntax only.
610 *
611 * The second time is after a connection has been established but before
612 * authentication. activep is initialized to 2 and global config directives
613 * are ignored since they have already been processed. If the criteria in a
614 * Match block is met, activep is set and the subsequent directives
615 * processed and actioned until EOF or another Match block unsets it. Any
616 * options set are copied into the main server config.
617 *
618 * Potential additions/improvements:
619 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
620 *
621 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
622 * Match Address 192.168.0.*
623 * Tag trusted
624 * Match Group wheel
625 * Tag trusted
626 * Match Tag trusted
627 * AllowTcpForwarding yes
628 * GatewayPorts clientspecified
629 * [...]
630 *
631 * - Add a PermittedChannelRequests directive
632 * Match Group shell
633 * PermittedChannelRequests session,forwarded-tcpip
634 */
635
636static int
Damien Miller565ca3f2006-08-19 00:23:15 +1000637match_cfg_line_group(const char *grps, int line, const char *user)
638{
639 int result = 0;
Damien Miller565ca3f2006-08-19 00:23:15 +1000640 struct passwd *pw;
641
Damien Miller565ca3f2006-08-19 00:23:15 +1000642 if (user == NULL)
643 goto out;
644
645 if ((pw = getpwnam(user)) == NULL) {
646 debug("Can't match group at line %d because user %.100s does "
647 "not exist", line, user);
648 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
649 debug("Can't Match group because user %.100s not in any group "
650 "at line %d", user, line);
Darren Tuckerb03fd022008-07-04 13:51:12 +1000651 } else if (ga_match_pattern_list(grps) != 1) {
652 debug("user %.100s does not match group list %.100s at line %d",
653 user, grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000654 } else {
Darren Tuckerb03fd022008-07-04 13:51:12 +1000655 debug("user %.100s matched group list %.100s at line %d", user,
656 grps, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000657 result = 1;
658 }
659out:
660 ga_free();
Damien Miller565ca3f2006-08-19 00:23:15 +1000661 return result;
662}
663
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000664/*
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000665 * All of the attributes on a single Match line are ANDed together, so we need
Damien Miller03bf2e62013-10-24 21:01:26 +1100666 * to check every attribute and set the result to zero if any attribute does
Darren Tuckerbb6cc072012-09-17 13:25:06 +1000667 * not match.
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000668 */
Damien Miller565ca3f2006-08-19 00:23:15 +1000669static int
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000670match_cfg_line(char **condition, int line, struct connection_info *ci)
Darren Tucker45150472006-07-12 22:34:17 +1000671{
Damien Millercf31f382013-10-24 21:02:56 +1100672 int result = 1, attributes = 0, port;
Darren Tucker45150472006-07-12 22:34:17 +1000673 char *arg, *attrib, *cp = *condition;
674 size_t len;
675
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000676 if (ci == NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000677 debug3("checking syntax for 'Match %s'", cp);
678 else
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000679 debug3("checking match for '%s' user %s host %s addr %s "
680 "laddr %s lport %d", cp, ci->user ? ci->user : "(null)",
681 ci->host ? ci->host : "(null)",
682 ci->address ? ci->address : "(null)",
683 ci->laddress ? ci->laddress : "(null)", ci->lport);
Darren Tucker45150472006-07-12 22:34:17 +1000684
685 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
Damien Millercf31f382013-10-24 21:02:56 +1100686 attributes++;
687 if (strcasecmp(attrib, "all") == 0) {
688 if (attributes != 1 ||
689 ((arg = strdelim(&cp)) != NULL && *arg != '\0')) {
690 error("'all' cannot be combined with other "
691 "Match attributes");
692 return -1;
693 }
694 *condition = cp;
695 return 1;
696 }
Darren Tucker45150472006-07-12 22:34:17 +1000697 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
698 error("Missing Match criteria for %s", attrib);
699 return -1;
700 }
701 len = strlen(arg);
702 if (strcasecmp(attrib, "user") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000703 if (ci == NULL || ci->user == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000704 result = 0;
705 continue;
706 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000707 if (match_pattern_list(ci->user, arg, len, 0) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000708 result = 0;
709 else
710 debug("user %.100s matched 'User %.100s' at "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000711 "line %d", ci->user, arg, line);
Damien Miller565ca3f2006-08-19 00:23:15 +1000712 } else if (strcasecmp(attrib, "group") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000713 if (ci == NULL || ci->user == NULL) {
714 result = 0;
715 continue;
716 }
717 switch (match_cfg_line_group(arg, line, ci->user)) {
Damien Miller565ca3f2006-08-19 00:23:15 +1000718 case -1:
719 return -1;
720 case 0:
721 result = 0;
722 }
Darren Tucker45150472006-07-12 22:34:17 +1000723 } else if (strcasecmp(attrib, "host") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000724 if (ci == NULL || ci->host == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000725 result = 0;
726 continue;
727 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000728 if (match_hostname(ci->host, arg, len) != 1)
Darren Tucker45150472006-07-12 22:34:17 +1000729 result = 0;
730 else
731 debug("connection from %.100s matched 'Host "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000732 "%.100s' at line %d", ci->host, arg, line);
Darren Tucker45150472006-07-12 22:34:17 +1000733 } else if (strcasecmp(attrib, "address") == 0) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000734 if (ci == NULL || ci->address == NULL) {
735 result = 0;
736 continue;
737 }
738 switch (addr_match_list(ci->address, arg)) {
Darren Tucker7a3935d2008-06-10 22:59:10 +1000739 case 1:
Darren Tucker45150472006-07-12 22:34:17 +1000740 debug("connection from %.100s matched 'Address "
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000741 "%.100s' at line %d", ci->address, arg, line);
Darren Tucker7a3935d2008-06-10 22:59:10 +1000742 break;
743 case 0:
Darren Tucker896ad5a2008-06-11 09:34:46 +1000744 case -1:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000745 result = 0;
746 break;
Darren Tucker896ad5a2008-06-11 09:34:46 +1000747 case -2:
Darren Tucker7a3935d2008-06-10 22:59:10 +1000748 return -1;
749 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000750 } else if (strcasecmp(attrib, "localaddress") == 0){
751 if (ci == NULL || ci->laddress == NULL) {
752 result = 0;
753 continue;
754 }
755 switch (addr_match_list(ci->laddress, arg)) {
756 case 1:
757 debug("connection from %.100s matched "
758 "'LocalAddress %.100s' at line %d",
759 ci->laddress, arg, line);
760 break;
761 case 0:
762 case -1:
763 result = 0;
764 break;
765 case -2:
766 return -1;
767 }
768 } else if (strcasecmp(attrib, "localport") == 0) {
769 if ((port = a2port(arg)) == -1) {
770 error("Invalid LocalPort '%s' on Match line",
771 arg);
772 return -1;
773 }
774 if (ci == NULL || ci->lport == 0) {
775 result = 0;
776 continue;
777 }
778 /* TODO support port lists */
779 if (port == ci->lport)
780 debug("connection from %.100s matched "
781 "'LocalPort %d' at line %d",
782 ci->laddress, port, line);
783 else
784 result = 0;
Darren Tucker45150472006-07-12 22:34:17 +1000785 } else {
786 error("Unsupported Match attribute %s", attrib);
787 return -1;
788 }
789 }
Damien Millercf31f382013-10-24 21:02:56 +1100790 if (attributes == 0) {
791 error("One or more attributes required for Match");
792 return -1;
793 }
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000794 if (ci != NULL)
Darren Tucker45150472006-07-12 22:34:17 +1000795 debug3("match %sfound", result ? "" : "not ");
796 *condition = cp;
797 return result;
798}
799
Damien Millere2754432006-07-24 14:06:47 +1000800#define WHITESPACE " \t\r\n"
801
Damien Miller33322122011-06-20 14:43:11 +1000802/* Multistate option parsing */
803struct multistate {
804 char *key;
805 int value;
806};
807static const struct multistate multistate_addressfamily[] = {
808 { "inet", AF_INET },
809 { "inet6", AF_INET6 },
810 { "any", AF_UNSPEC },
811 { NULL, -1 }
812};
813static const struct multistate multistate_permitrootlogin[] = {
814 { "without-password", PERMIT_NO_PASSWD },
815 { "forced-commands-only", PERMIT_FORCED_ONLY },
816 { "yes", PERMIT_YES },
817 { "no", PERMIT_NO },
818 { NULL, -1 }
819};
820static const struct multistate multistate_compression[] = {
821 { "delayed", COMP_DELAYED },
822 { "yes", COMP_ZLIB },
823 { "no", COMP_NONE },
824 { NULL, -1 }
825};
826static const struct multistate multistate_gatewayports[] = {
827 { "clientspecified", 2 },
828 { "yes", 1 },
829 { "no", 0 },
830 { NULL, -1 }
831};
Damien Miller69ff1df2011-06-23 08:30:03 +1000832static const struct multistate multistate_privsep[] = {
Damien Miller5a5c2b92012-07-31 12:21:34 +1000833 { "yes", PRIVSEP_NOSANDBOX },
834 { "sandbox", PRIVSEP_ON },
835 { "nosandbox", PRIVSEP_NOSANDBOX },
Damien Miller69ff1df2011-06-23 08:30:03 +1000836 { "no", PRIVSEP_OFF },
837 { NULL, -1 }
838};
Damien Milleraa5b3f82012-12-03 09:50:54 +1100839static const struct multistate multistate_tcpfwd[] = {
840 { "yes", FORWARD_ALLOW },
841 { "all", FORWARD_ALLOW },
842 { "no", FORWARD_DENY },
843 { "remote", FORWARD_REMOTE },
844 { "local", FORWARD_LOCAL },
845 { NULL, -1 }
846};
Damien Miller33322122011-06-20 14:43:11 +1000847
Ben Lindstromade03f62001-12-06 18:22:17 +0000848int
849process_server_config_line(ServerOptions *options, char *line,
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000850 const char *filename, int linenum, int *activep,
851 struct connection_info *connectinfo)
Ben Lindstromade03f62001-12-06 18:22:17 +0000852{
Darren Tucker09c0f032013-05-16 20:48:57 +1000853 char *cp, **charptr, *arg, *p;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000854 int cmdline = 0, *intptr, value, value2, n, port;
Darren Tucker1e44c5d2008-01-01 20:32:26 +1100855 SyslogFacility *log_facility_ptr;
856 LogLevel *log_level_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000857 ServerOpCodes opcode;
Darren Tucker45150472006-07-12 22:34:17 +1000858 u_int i, flags = 0;
Damien Miller917f9b62006-07-10 20:36:47 +1000859 size_t len;
Darren Tucker9113d0c2013-05-16 20:48:14 +1000860 long long val64;
Damien Miller33322122011-06-20 14:43:11 +1000861 const struct multistate *multistate_ptr;
Ben Lindstromade03f62001-12-06 18:22:17 +0000862
863 cp = line;
Damien Miller78f16cb2006-03-26 13:54:37 +1100864 if ((arg = strdelim(&cp)) == NULL)
Damien Miller928b2362006-03-26 13:53:32 +1100865 return 0;
Ben Lindstromade03f62001-12-06 18:22:17 +0000866 /* Ignore leading whitespace */
867 if (*arg == '\0')
868 arg = strdelim(&cp);
869 if (!arg || !*arg || *arg == '#')
870 return 0;
871 intptr = NULL;
872 charptr = NULL;
Darren Tucker45150472006-07-12 22:34:17 +1000873 opcode = parse_token(arg, filename, linenum, &flags);
874
875 if (activep == NULL) { /* We are processing a command line directive */
876 cmdline = 1;
877 activep = &cmdline;
878 }
879 if (*activep && opcode != sMatch)
880 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
881 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
Darren Tuckerfbcf8272012-05-19 19:37:01 +1000882 if (connectinfo == NULL) {
Darren Tucker45150472006-07-12 22:34:17 +1000883 fatal("%s line %d: Directive '%s' is not allowed "
884 "within a Match block", filename, linenum, arg);
885 } else { /* this is a directive we have already processed */
886 while (arg)
887 arg = strdelim(&cp);
888 return 0;
889 }
890 }
891
Ben Lindstromade03f62001-12-06 18:22:17 +0000892 switch (opcode) {
893 /* Portable-specific options */
Damien Miller4e448a32003-05-14 15:11:48 +1000894 case sUsePAM:
895 intptr = &options->use_pam;
Ben Lindstromade03f62001-12-06 18:22:17 +0000896 goto parse_flag;
897
898 /* Standard Options */
899 case sBadOption:
900 return -1;
901 case sPort:
902 /* ignore ports from configfile if cmdline specifies ports */
903 if (options->ports_from_cmdline)
904 return 0;
905 if (options->listen_addrs != NULL)
906 fatal("%s line %d: ports must be specified before "
Damien Miller4fbf08a2002-01-22 23:35:09 +1100907 "ListenAddress.", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000908 if (options->num_ports >= MAX_PORTS)
909 fatal("%s line %d: too many ports.",
910 filename, linenum);
911 arg = strdelim(&cp);
912 if (!arg || *arg == '\0')
913 fatal("%s line %d: missing port number.",
914 filename, linenum);
915 options->ports[options->num_ports++] = a2port(arg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100916 if (options->ports[options->num_ports-1] <= 0)
Ben Lindstromade03f62001-12-06 18:22:17 +0000917 fatal("%s line %d: Badly formatted port number.",
918 filename, linenum);
919 break;
920
921 case sServerKeyBits:
922 intptr = &options->server_key_bits;
Damien Miller7207f642008-05-19 15:34:50 +1000923 parse_int:
Ben Lindstromade03f62001-12-06 18:22:17 +0000924 arg = strdelim(&cp);
925 if (!arg || *arg == '\0')
926 fatal("%s line %d: missing integer value.",
927 filename, linenum);
928 value = atoi(arg);
Darren Tucker45150472006-07-12 22:34:17 +1000929 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +0000930 *intptr = value;
931 break;
932
933 case sLoginGraceTime:
934 intptr = &options->login_grace_time;
Damien Miller7207f642008-05-19 15:34:50 +1000935 parse_time:
Ben Lindstromade03f62001-12-06 18:22:17 +0000936 arg = strdelim(&cp);
937 if (!arg || *arg == '\0')
938 fatal("%s line %d: missing time value.",
939 filename, linenum);
940 if ((value = convtime(arg)) == -1)
941 fatal("%s line %d: invalid time value.",
942 filename, linenum);
943 if (*intptr == -1)
944 *intptr = value;
945 break;
946
947 case sKeyRegenerationTime:
948 intptr = &options->key_regeneration_time;
949 goto parse_time;
950
951 case sListenAddress:
952 arg = strdelim(&cp);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100953 if (arg == NULL || *arg == '\0')
954 fatal("%s line %d: missing address",
Ben Lindstromade03f62001-12-06 18:22:17 +0000955 filename, linenum);
Damien Miller203c7052005-08-12 22:11:37 +1000956 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
957 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
958 && strchr(p+1, ':') != NULL) {
959 add_listen_addr(options, arg, 0);
960 break;
961 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100962 p = hpdelim(&arg);
963 if (p == NULL)
964 fatal("%s line %d: bad address:port usage",
965 filename, linenum);
966 p = cleanhostname(p);
967 if (arg == NULL)
968 port = 0;
Damien Miller3dc71ad2009-01-28 16:31:22 +1100969 else if ((port = a2port(arg)) <= 0)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100970 fatal("%s line %d: bad port number", filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +0000971
Damien Millerf91ee4c2005-03-01 21:24:33 +1100972 add_listen_addr(options, p, port);
973
Ben Lindstromade03f62001-12-06 18:22:17 +0000974 break;
975
Darren Tucker0f383232005-01-20 10:57:56 +1100976 case sAddressFamily:
Damien Miller33322122011-06-20 14:43:11 +1000977 intptr = &options->address_family;
978 multistate_ptr = multistate_addressfamily;
979 if (options->listen_addrs != NULL)
980 fatal("%s line %d: address family must be specified "
981 "before ListenAddress.", filename, linenum);
982 parse_multistate:
Darren Tucker0f383232005-01-20 10:57:56 +1100983 arg = strdelim(&cp);
Damien Miller17b23d82005-05-26 12:11:56 +1000984 if (!arg || *arg == '\0')
Damien Miller33322122011-06-20 14:43:11 +1000985 fatal("%s line %d: missing argument.",
Damien Miller17b23d82005-05-26 12:11:56 +1000986 filename, linenum);
Damien Miller33322122011-06-20 14:43:11 +1000987 value = -1;
988 for (i = 0; multistate_ptr[i].key != NULL; i++) {
989 if (strcasecmp(arg, multistate_ptr[i].key) == 0) {
990 value = multistate_ptr[i].value;
991 break;
992 }
993 }
994 if (value == -1)
995 fatal("%s line %d: unsupported option \"%s\".",
Darren Tucker0f383232005-01-20 10:57:56 +1100996 filename, linenum, arg);
Damien Miller33322122011-06-20 14:43:11 +1000997 if (*activep && *intptr == -1)
Darren Tucker0f383232005-01-20 10:57:56 +1100998 *intptr = value;
999 break;
1000
Ben Lindstromade03f62001-12-06 18:22:17 +00001001 case sHostKeyFile:
1002 intptr = &options->num_host_key_files;
1003 if (*intptr >= MAX_HOSTKEYS)
1004 fatal("%s line %d: too many host keys specified (max %d).",
1005 filename, linenum, MAX_HOSTKEYS);
1006 charptr = &options->host_key_files[*intptr];
Damien Miller7207f642008-05-19 15:34:50 +10001007 parse_filename:
Ben Lindstromade03f62001-12-06 18:22:17 +00001008 arg = strdelim(&cp);
1009 if (!arg || *arg == '\0')
1010 fatal("%s line %d: missing file name.",
1011 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001012 if (*activep && *charptr == NULL) {
Darren Tucker88b6fb22010-01-13 22:44:29 +11001013 *charptr = derelativise_path(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001014 /* increase optional counter */
1015 if (intptr != NULL)
1016 *intptr = *intptr + 1;
1017 }
1018 break;
1019
Damien Miller85b45e02013-07-20 13:21:52 +10001020 case sHostKeyAgent:
1021 charptr = &options->host_key_agent;
1022 arg = strdelim(&cp);
1023 if (!arg || *arg == '\0')
1024 fatal("%s line %d: missing socket name.",
1025 filename, linenum);
1026 if (*activep && *charptr == NULL)
1027 *charptr = !strcmp(arg, SSH_AUTHSOCKET_ENV_NAME) ?
1028 xstrdup(arg) : derelativise_path(arg);
1029 break;
1030
Damien Miller0a80ca12010-02-27 07:55:05 +11001031 case sHostCertificate:
1032 intptr = &options->num_host_cert_files;
1033 if (*intptr >= MAX_HOSTKEYS)
1034 fatal("%s line %d: too many host certificates "
1035 "specified (max %d).", filename, linenum,
1036 MAX_HOSTCERTS);
1037 charptr = &options->host_cert_files[*intptr];
1038 goto parse_filename;
1039 break;
1040
Ben Lindstromade03f62001-12-06 18:22:17 +00001041 case sPidFile:
1042 charptr = &options->pid_file;
1043 goto parse_filename;
1044
1045 case sPermitRootLogin:
1046 intptr = &options->permit_root_login;
Damien Miller33322122011-06-20 14:43:11 +10001047 multistate_ptr = multistate_permitrootlogin;
1048 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001049
1050 case sIgnoreRhosts:
1051 intptr = &options->ignore_rhosts;
Damien Miller7207f642008-05-19 15:34:50 +10001052 parse_flag:
Ben Lindstromade03f62001-12-06 18:22:17 +00001053 arg = strdelim(&cp);
1054 if (!arg || *arg == '\0')
1055 fatal("%s line %d: missing yes/no argument.",
1056 filename, linenum);
1057 value = 0; /* silence compiler */
1058 if (strcmp(arg, "yes") == 0)
1059 value = 1;
1060 else if (strcmp(arg, "no") == 0)
1061 value = 0;
1062 else
1063 fatal("%s line %d: Bad yes/no argument: %s",
1064 filename, linenum, arg);
Darren Tucker45150472006-07-12 22:34:17 +10001065 if (*activep && *intptr == -1)
Ben Lindstromade03f62001-12-06 18:22:17 +00001066 *intptr = value;
1067 break;
1068
1069 case sIgnoreUserKnownHosts:
1070 intptr = &options->ignore_user_known_hosts;
1071 goto parse_flag;
1072
Ben Lindstromade03f62001-12-06 18:22:17 +00001073 case sRhostsRSAAuthentication:
1074 intptr = &options->rhosts_rsa_authentication;
1075 goto parse_flag;
1076
1077 case sHostbasedAuthentication:
1078 intptr = &options->hostbased_authentication;
1079 goto parse_flag;
1080
1081 case sHostbasedUsesNameFromPacketOnly:
1082 intptr = &options->hostbased_uses_name_from_packet_only;
1083 goto parse_flag;
1084
1085 case sRSAAuthentication:
1086 intptr = &options->rsa_authentication;
1087 goto parse_flag;
1088
1089 case sPubkeyAuthentication:
1090 intptr = &options->pubkey_authentication;
1091 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001092
Ben Lindstromade03f62001-12-06 18:22:17 +00001093 case sKerberosAuthentication:
1094 intptr = &options->kerberos_authentication;
1095 goto parse_flag;
1096
1097 case sKerberosOrLocalPasswd:
1098 intptr = &options->kerberos_or_local_passwd;
1099 goto parse_flag;
1100
1101 case sKerberosTicketCleanup:
1102 intptr = &options->kerberos_ticket_cleanup;
1103 goto parse_flag;
Damien Miller2aa0ab42003-05-15 12:05:28 +10001104
Darren Tucker22ef5082003-12-31 11:37:34 +11001105 case sKerberosGetAFSToken:
1106 intptr = &options->kerberos_get_afs_token;
1107 goto parse_flag;
1108
Darren Tucker0efd1552003-08-26 11:49:55 +10001109 case sGssAuthentication:
1110 intptr = &options->gss_authentication;
1111 goto parse_flag;
1112
1113 case sGssCleanupCreds:
1114 intptr = &options->gss_cleanup_creds;
1115 goto parse_flag;
1116
Ben Lindstromade03f62001-12-06 18:22:17 +00001117 case sPasswordAuthentication:
1118 intptr = &options->password_authentication;
1119 goto parse_flag;
1120
1121 case sKbdInteractiveAuthentication:
1122 intptr = &options->kbd_interactive_authentication;
1123 goto parse_flag;
1124
1125 case sChallengeResponseAuthentication:
1126 intptr = &options->challenge_response_authentication;
1127 goto parse_flag;
1128
1129 case sPrintMotd:
1130 intptr = &options->print_motd;
1131 goto parse_flag;
1132
1133 case sPrintLastLog:
1134 intptr = &options->print_lastlog;
1135 goto parse_flag;
1136
1137 case sX11Forwarding:
1138 intptr = &options->x11_forwarding;
1139 goto parse_flag;
1140
1141 case sX11DisplayOffset:
1142 intptr = &options->x11_display_offset;
1143 goto parse_int;
1144
Damien Miller95c249f2002-02-05 12:11:34 +11001145 case sX11UseLocalhost:
1146 intptr = &options->x11_use_localhost;
1147 goto parse_flag;
1148
Ben Lindstromade03f62001-12-06 18:22:17 +00001149 case sXAuthLocation:
1150 charptr = &options->xauth_location;
1151 goto parse_filename;
1152
Damien Miller5ff30c62013-10-30 22:21:50 +11001153 case sPermitTTY:
1154 intptr = &options->permit_tty;
1155 goto parse_flag;
1156
Damien Miller72e6b5c2014-07-04 09:00:04 +10001157 case sPermitUserRC:
1158 intptr = &options->permit_user_rc;
1159 goto parse_flag;
1160
Ben Lindstromade03f62001-12-06 18:22:17 +00001161 case sStrictModes:
1162 intptr = &options->strict_modes;
1163 goto parse_flag;
1164
Damien Miller12c150e2003-12-17 16:31:10 +11001165 case sTCPKeepAlive:
1166 intptr = &options->tcp_keep_alive;
Ben Lindstromade03f62001-12-06 18:22:17 +00001167 goto parse_flag;
1168
1169 case sEmptyPasswd:
1170 intptr = &options->permit_empty_passwd;
1171 goto parse_flag;
1172
Ben Lindstrom5d860f02002-08-01 01:28:38 +00001173 case sPermitUserEnvironment:
1174 intptr = &options->permit_user_env;
1175 goto parse_flag;
1176
Ben Lindstromade03f62001-12-06 18:22:17 +00001177 case sUseLogin:
1178 intptr = &options->use_login;
1179 goto parse_flag;
1180
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001181 case sCompression:
1182 intptr = &options->compression;
Damien Miller33322122011-06-20 14:43:11 +10001183 multistate_ptr = multistate_compression;
1184 goto parse_multistate;
Ben Lindstrom23e0f662002-06-21 01:09:47 +00001185
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001186 case sRekeyLimit:
1187 arg = strdelim(&cp);
1188 if (!arg || *arg == '\0')
1189 fatal("%.200s line %d: Missing argument.", filename,
1190 linenum);
1191 if (strcmp(arg, "default") == 0) {
1192 val64 = 0;
1193 } else {
Darren Tuckerb7ee8522013-05-16 20:33:10 +10001194 if (scan_scaled(arg, &val64) == -1)
1195 fatal("%.200s line %d: Bad number '%s': %s",
1196 filename, linenum, arg, strerror(errno));
1197 /* check for too-large or too-small limits */
1198 if (val64 > UINT_MAX)
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001199 fatal("%.200s line %d: RekeyLimit too large",
1200 filename, linenum);
1201 if (val64 != 0 && val64 < 16)
1202 fatal("%.200s line %d: RekeyLimit too small",
1203 filename, linenum);
1204 }
1205 if (*activep && options->rekey_limit == -1)
1206 options->rekey_limit = (u_int32_t)val64;
1207 if (cp != NULL) { /* optional rekey interval present */
1208 if (strcmp(cp, "none") == 0) {
1209 (void)strdelim(&cp); /* discard */
1210 break;
1211 }
1212 intptr = &options->rekey_interval;
1213 goto parse_time;
1214 }
1215 break;
1216
Ben Lindstromade03f62001-12-06 18:22:17 +00001217 case sGatewayPorts:
Damien Miller7acefbb2014-07-18 14:11:24 +10001218 intptr = &options->fwd_opts.gateway_ports;
Damien Miller33322122011-06-20 14:43:11 +10001219 multistate_ptr = multistate_gatewayports;
1220 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001221
Damien Miller3a961dc2003-06-03 10:25:48 +10001222 case sUseDNS:
1223 intptr = &options->use_dns;
Ben Lindstromade03f62001-12-06 18:22:17 +00001224 goto parse_flag;
1225
1226 case sLogFacility:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001227 log_facility_ptr = &options->log_facility;
Ben Lindstromade03f62001-12-06 18:22:17 +00001228 arg = strdelim(&cp);
1229 value = log_facility_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001230 if (value == SYSLOG_FACILITY_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001231 fatal("%.200s line %d: unsupported log facility '%s'",
1232 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001233 if (*log_facility_ptr == -1)
1234 *log_facility_ptr = (SyslogFacility) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001235 break;
1236
1237 case sLogLevel:
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001238 log_level_ptr = &options->log_level;
Ben Lindstromade03f62001-12-06 18:22:17 +00001239 arg = strdelim(&cp);
1240 value = log_level_number(arg);
Damien Millerfcd93202002-02-05 12:26:34 +11001241 if (value == SYSLOG_LEVEL_NOT_SET)
Ben Lindstromade03f62001-12-06 18:22:17 +00001242 fatal("%.200s line %d: unsupported log level '%s'",
1243 filename, linenum, arg ? arg : "<NONE>");
Darren Tucker1e44c5d2008-01-01 20:32:26 +11001244 if (*log_level_ptr == -1)
1245 *log_level_ptr = (LogLevel) value;
Ben Lindstromade03f62001-12-06 18:22:17 +00001246 break;
1247
1248 case sAllowTcpForwarding:
1249 intptr = &options->allow_tcp_forwarding;
Damien Milleraa5b3f82012-12-03 09:50:54 +11001250 multistate_ptr = multistate_tcpfwd;
1251 goto parse_multistate;
Ben Lindstromade03f62001-12-06 18:22:17 +00001252
Damien Miller7acefbb2014-07-18 14:11:24 +10001253 case sAllowStreamLocalForwarding:
1254 intptr = &options->allow_streamlocal_forwarding;
1255 multistate_ptr = multistate_tcpfwd;
1256 goto parse_multistate;
1257
Damien Miller4f755cd2008-05-19 14:57:41 +10001258 case sAllowAgentForwarding:
1259 intptr = &options->allow_agent_forwarding;
1260 goto parse_flag;
1261
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001262 case sUsePrivilegeSeparation:
1263 intptr = &use_privsep;
Damien Miller69ff1df2011-06-23 08:30:03 +10001264 multistate_ptr = multistate_privsep;
1265 goto parse_multistate;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001266
Ben Lindstromade03f62001-12-06 18:22:17 +00001267 case sAllowUsers:
1268 while ((arg = strdelim(&cp)) && *arg != '\0') {
1269 if (options->num_allow_users >= MAX_ALLOW_USERS)
1270 fatal("%s line %d: too many allow users.",
1271 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001272 if (!*activep)
1273 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001274 options->allow_users[options->num_allow_users++] =
1275 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001276 }
1277 break;
1278
1279 case sDenyUsers:
1280 while ((arg = strdelim(&cp)) && *arg != '\0') {
1281 if (options->num_deny_users >= MAX_DENY_USERS)
Damien Miller4dec5d72006-08-05 11:38:40 +10001282 fatal("%s line %d: too many deny users.",
Ben Lindstromade03f62001-12-06 18:22:17 +00001283 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001284 if (!*activep)
1285 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001286 options->deny_users[options->num_deny_users++] =
1287 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001288 }
1289 break;
1290
1291 case sAllowGroups:
1292 while ((arg = strdelim(&cp)) && *arg != '\0') {
1293 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1294 fatal("%s line %d: too many allow groups.",
1295 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001296 if (!*activep)
1297 continue;
Ben Lindstrome1353632002-06-23 21:29:23 +00001298 options->allow_groups[options->num_allow_groups++] =
1299 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001300 }
1301 break;
1302
1303 case sDenyGroups:
1304 while ((arg = strdelim(&cp)) && *arg != '\0') {
1305 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1306 fatal("%s line %d: too many deny groups.",
1307 filename, linenum);
Damien Millerc24da772012-06-20 21:53:58 +10001308 if (!*activep)
1309 continue;
1310 options->deny_groups[options->num_deny_groups++] =
1311 xstrdup(arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001312 }
1313 break;
1314
1315 case sCiphers:
1316 arg = strdelim(&cp);
1317 if (!arg || *arg == '\0')
1318 fatal("%s line %d: Missing argument.", filename, linenum);
1319 if (!ciphers_valid(arg))
1320 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1321 filename, linenum, arg ? arg : "<NONE>");
1322 if (options->ciphers == NULL)
1323 options->ciphers = xstrdup(arg);
1324 break;
1325
1326 case sMacs:
1327 arg = strdelim(&cp);
1328 if (!arg || *arg == '\0')
1329 fatal("%s line %d: Missing argument.", filename, linenum);
1330 if (!mac_valid(arg))
1331 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1332 filename, linenum, arg ? arg : "<NONE>");
1333 if (options->macs == NULL)
1334 options->macs = xstrdup(arg);
1335 break;
1336
Damien Millerd5f62bf2010-09-24 22:11:14 +10001337 case sKexAlgorithms:
1338 arg = strdelim(&cp);
1339 if (!arg || *arg == '\0')
1340 fatal("%s line %d: Missing argument.",
1341 filename, linenum);
1342 if (!kex_names_valid(arg))
1343 fatal("%s line %d: Bad SSH2 KexAlgorithms '%s'.",
1344 filename, linenum, arg ? arg : "<NONE>");
1345 if (options->kex_algorithms == NULL)
1346 options->kex_algorithms = xstrdup(arg);
1347 break;
1348
Ben Lindstromade03f62001-12-06 18:22:17 +00001349 case sProtocol:
1350 intptr = &options->protocol;
1351 arg = strdelim(&cp);
1352 if (!arg || *arg == '\0')
1353 fatal("%s line %d: Missing argument.", filename, linenum);
1354 value = proto_spec(arg);
1355 if (value == SSH_PROTO_UNKNOWN)
1356 fatal("%s line %d: Bad protocol spec '%s'.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001357 filename, linenum, arg ? arg : "<NONE>");
Ben Lindstromade03f62001-12-06 18:22:17 +00001358 if (*intptr == SSH_PROTO_UNKNOWN)
1359 *intptr = value;
1360 break;
1361
1362 case sSubsystem:
1363 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1364 fatal("%s line %d: too many subsystems defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001365 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001366 }
1367 arg = strdelim(&cp);
1368 if (!arg || *arg == '\0')
1369 fatal("%s line %d: Missing subsystem name.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001370 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001371 if (!*activep) {
1372 arg = strdelim(&cp);
1373 break;
1374 }
Ben Lindstromade03f62001-12-06 18:22:17 +00001375 for (i = 0; i < options->num_subsystems; i++)
1376 if (strcmp(arg, options->subsystem_name[i]) == 0)
1377 fatal("%s line %d: Subsystem '%s' already defined.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001378 filename, linenum, arg);
Ben Lindstromade03f62001-12-06 18:22:17 +00001379 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1380 arg = strdelim(&cp);
1381 if (!arg || *arg == '\0')
1382 fatal("%s line %d: Missing subsystem command.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001383 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001384 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
Damien Miller917f9b62006-07-10 20:36:47 +10001385
1386 /* Collect arguments (separate to executable) */
1387 p = xstrdup(arg);
1388 len = strlen(p) + 1;
1389 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1390 len += 1 + strlen(arg);
1391 p = xrealloc(p, 1, len);
1392 strlcat(p, " ", len);
1393 strlcat(p, arg, len);
1394 }
1395 options->subsystem_args[options->num_subsystems] = p;
Ben Lindstromade03f62001-12-06 18:22:17 +00001396 options->num_subsystems++;
1397 break;
1398
1399 case sMaxStartups:
1400 arg = strdelim(&cp);
1401 if (!arg || *arg == '\0')
1402 fatal("%s line %d: Missing MaxStartups spec.",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001403 filename, linenum);
Ben Lindstromade03f62001-12-06 18:22:17 +00001404 if ((n = sscanf(arg, "%d:%d:%d",
1405 &options->max_startups_begin,
1406 &options->max_startups_rate,
1407 &options->max_startups)) == 3) {
1408 if (options->max_startups_begin >
1409 options->max_startups ||
1410 options->max_startups_rate > 100 ||
1411 options->max_startups_rate < 1)
1412 fatal("%s line %d: Illegal MaxStartups spec.",
1413 filename, linenum);
1414 } else if (n != 1)
1415 fatal("%s line %d: Illegal MaxStartups spec.",
1416 filename, linenum);
1417 else
1418 options->max_startups = options->max_startups_begin;
1419 break;
1420
Darren Tucker89413db2004-05-24 10:36:23 +10001421 case sMaxAuthTries:
1422 intptr = &options->max_authtries;
1423 goto parse_int;
1424
Damien Miller7207f642008-05-19 15:34:50 +10001425 case sMaxSessions:
1426 intptr = &options->max_sessions;
1427 goto parse_int;
1428
Ben Lindstromade03f62001-12-06 18:22:17 +00001429 case sBanner:
1430 charptr = &options->banner;
1431 goto parse_filename;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001432
Ben Lindstromade03f62001-12-06 18:22:17 +00001433 /*
1434 * These options can contain %X options expanded at
1435 * connect time, so that you can specify paths like:
1436 *
1437 * AuthorizedKeysFile /etc/ssh_keys/%u
1438 */
1439 case sAuthorizedKeysFile:
Damien Millerd8478b62011-05-29 21:39:36 +10001440 if (*activep && options->num_authkeys_files == 0) {
1441 while ((arg = strdelim(&cp)) && *arg != '\0') {
1442 if (options->num_authkeys_files >=
1443 MAX_AUTHKEYS_FILES)
1444 fatal("%s line %d: "
1445 "too many authorized keys files.",
1446 filename, linenum);
1447 options->authorized_keys_files[
1448 options->num_authkeys_files++] =
1449 tilde_expand_filename(arg, getuid());
1450 }
1451 }
1452 return 0;
1453
Damien Miller30da3442010-05-10 11:58:03 +10001454 case sAuthorizedPrincipalsFile:
1455 charptr = &options->authorized_principals_file;
Damien Millerc4cb47b2010-03-22 05:52:26 +11001456 arg = strdelim(&cp);
1457 if (!arg || *arg == '\0')
1458 fatal("%s line %d: missing file name.",
1459 filename, linenum);
1460 if (*activep && *charptr == NULL) {
Damien Miller4a5f0d32010-03-22 05:53:04 +11001461 *charptr = tilde_expand_filename(arg, getuid());
Damien Millerc4cb47b2010-03-22 05:52:26 +11001462 /* increase optional counter */
1463 if (intptr != NULL)
1464 *intptr = *intptr + 1;
1465 }
1466 break;
Ben Lindstromade03f62001-12-06 18:22:17 +00001467
1468 case sClientAliveInterval:
1469 intptr = &options->client_alive_interval;
1470 goto parse_time;
1471
1472 case sClientAliveCountMax:
1473 intptr = &options->client_alive_count_max;
1474 goto parse_int;
1475
Darren Tucker46bc0752004-05-02 22:11:30 +10001476 case sAcceptEnv:
1477 while ((arg = strdelim(&cp)) && *arg != '\0') {
1478 if (strchr(arg, '=') != NULL)
1479 fatal("%s line %d: Invalid environment name.",
1480 filename, linenum);
1481 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1482 fatal("%s line %d: too many allow env.",
1483 filename, linenum);
Darren Tucker45150472006-07-12 22:34:17 +10001484 if (!*activep)
Damien Millerc24da772012-06-20 21:53:58 +10001485 continue;
Darren Tucker46bc0752004-05-02 22:11:30 +10001486 options->accept_env[options->num_accept_env++] =
1487 xstrdup(arg);
1488 }
1489 break;
1490
Damien Millerd27b9472005-12-13 19:29:02 +11001491 case sPermitTunnel:
1492 intptr = &options->permit_tun;
Damien Miller7b58e802005-12-13 19:33:19 +11001493 arg = strdelim(&cp);
1494 if (!arg || *arg == '\0')
1495 fatal("%s line %d: Missing yes/point-to-point/"
1496 "ethernet/no argument.", filename, linenum);
Darren Tuckere7140f22008-06-10 23:01:51 +10001497 value = -1;
1498 for (i = 0; tunmode_desc[i].val != -1; i++)
1499 if (strcmp(tunmode_desc[i].text, arg) == 0) {
1500 value = tunmode_desc[i].val;
1501 break;
1502 }
1503 if (value == -1)
Damien Miller7b58e802005-12-13 19:33:19 +11001504 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1505 "no argument: %s", filename, linenum, arg);
1506 if (*intptr == -1)
1507 *intptr = value;
1508 break;
Damien Millerd27b9472005-12-13 19:29:02 +11001509
Darren Tucker45150472006-07-12 22:34:17 +10001510 case sMatch:
1511 if (cmdline)
1512 fatal("Match directive not supported as a command-line "
1513 "option");
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001514 value = match_cfg_line(&cp, linenum, connectinfo);
Darren Tucker45150472006-07-12 22:34:17 +10001515 if (value < 0)
1516 fatal("%s line %d: Bad Match condition", filename,
1517 linenum);
1518 *activep = value;
1519 break;
1520
Damien Miller9b439df2006-07-24 14:04:00 +10001521 case sPermitOpen:
1522 arg = strdelim(&cp);
1523 if (!arg || *arg == '\0')
1524 fatal("%s line %d: missing PermitOpen specification",
1525 filename, linenum);
Damien Miller9fc6a562007-01-05 16:29:02 +11001526 n = options->num_permitted_opens; /* modified later */
Damien Miller9b439df2006-07-24 14:04:00 +10001527 if (strcmp(arg, "any") == 0) {
Damien Miller9fc6a562007-01-05 16:29:02 +11001528 if (*activep && n == -1) {
Damien Miller9b439df2006-07-24 14:04:00 +10001529 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001530 options->num_permitted_opens = 0;
1531 }
Damien Miller9b439df2006-07-24 14:04:00 +10001532 break;
1533 }
Damien Millerc6081482012-04-22 11:18:53 +10001534 if (strcmp(arg, "none") == 0) {
1535 if (*activep && n == -1) {
Damien Millerc6081482012-04-22 11:18:53 +10001536 options->num_permitted_opens = 1;
1537 channel_disable_adm_local_opens();
1538 }
1539 break;
1540 }
Damien Millera29b95e2007-01-05 16:28:36 +11001541 if (*activep && n == -1)
1542 channel_clear_adm_permitted_opens();
Damien Millera765cf42006-07-24 14:08:13 +10001543 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1544 p = hpdelim(&arg);
1545 if (p == NULL)
1546 fatal("%s line %d: missing host in PermitOpen",
1547 filename, linenum);
1548 p = cleanhostname(p);
Darren Tucker1338b9e2011-10-02 18:57:35 +11001549 if (arg == NULL || ((port = permitopen_port(arg)) < 0))
Damien Millera765cf42006-07-24 14:08:13 +10001550 fatal("%s line %d: bad port number in "
1551 "PermitOpen", filename, linenum);
Damien Millera29b95e2007-01-05 16:28:36 +11001552 if (*activep && n == -1)
Damien Millera765cf42006-07-24 14:08:13 +10001553 options->num_permitted_opens =
1554 channel_add_adm_permitted_opens(p, port);
Damien Millera765cf42006-07-24 14:08:13 +10001555 }
Damien Miller9b439df2006-07-24 14:04:00 +10001556 break;
1557
Damien Millere2754432006-07-24 14:06:47 +10001558 case sForceCommand:
1559 if (cp == NULL)
1560 fatal("%.200s line %d: Missing argument.", filename,
1561 linenum);
1562 len = strspn(cp, WHITESPACE);
1563 if (*activep && options->adm_forced_command == NULL)
1564 options->adm_forced_command = xstrdup(cp + len);
1565 return 0;
1566
Damien Millerd8cb1f12008-02-10 22:40:12 +11001567 case sChrootDirectory:
1568 charptr = &options->chroot_directory;
Damien Miller54e37732008-02-10 22:48:55 +11001569
1570 arg = strdelim(&cp);
1571 if (!arg || *arg == '\0')
1572 fatal("%s line %d: missing file name.",
1573 filename, linenum);
1574 if (*activep && *charptr == NULL)
1575 *charptr = xstrdup(arg);
1576 break;
Damien Millerd8cb1f12008-02-10 22:40:12 +11001577
Damien Miller1aed65e2010-03-04 21:53:35 +11001578 case sTrustedUserCAKeys:
1579 charptr = &options->trusted_user_ca_keys;
1580 goto parse_filename;
1581
1582 case sRevokedKeys:
1583 charptr = &options->revoked_keys_file;
1584 goto parse_filename;
1585
Damien Miller0dac6fb2010-11-20 15:19:38 +11001586 case sIPQoS:
1587 arg = strdelim(&cp);
1588 if ((value = parse_ipqos(arg)) == -1)
1589 fatal("%s line %d: Bad IPQoS value: %s",
1590 filename, linenum, arg);
1591 arg = strdelim(&cp);
1592 if (arg == NULL)
1593 value2 = value;
1594 else if ((value2 = parse_ipqos(arg)) == -1)
1595 fatal("%s line %d: Bad IPQoS value: %s",
1596 filename, linenum, arg);
1597 if (*activep) {
1598 options->ip_qos_interactive = value;
1599 options->ip_qos_bulk = value2;
1600 }
1601 break;
1602
Damien Miller23528812012-04-22 11:24:43 +10001603 case sVersionAddendum:
1604 if (cp == NULL)
1605 fatal("%.200s line %d: Missing argument.", filename,
1606 linenum);
1607 len = strspn(cp, WHITESPACE);
1608 if (*activep && options->version_addendum == NULL) {
1609 if (strcasecmp(cp + len, "none") == 0)
1610 options->version_addendum = xstrdup("");
1611 else if (strchr(cp + len, '\r') != NULL)
1612 fatal("%.200s line %d: Invalid argument",
1613 filename, linenum);
1614 else
1615 options->version_addendum = xstrdup(cp + len);
1616 }
1617 return 0;
1618
Damien Miller09d3e122012-10-31 08:58:58 +11001619 case sAuthorizedKeysCommand:
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001620 if (cp == NULL)
1621 fatal("%.200s line %d: Missing argument.", filename,
1622 linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001623 len = strspn(cp, WHITESPACE);
1624 if (*activep && options->authorized_keys_command == NULL) {
1625 if (cp[len] != '/' && strcasecmp(cp + len, "none") != 0)
1626 fatal("%.200s line %d: AuthorizedKeysCommand "
1627 "must be an absolute path",
1628 filename, linenum);
1629 options->authorized_keys_command = xstrdup(cp + len);
1630 }
1631 return 0;
1632
1633 case sAuthorizedKeysCommandUser:
1634 charptr = &options->authorized_keys_command_user;
1635
1636 arg = strdelim(&cp);
jsg@openbsd.org72bba3d2014-11-24 03:39:22 +00001637 if (!arg || *arg == '\0')
1638 fatal("%s line %d: missing AuthorizedKeysCommandUser "
1639 "argument.", filename, linenum);
Damien Miller09d3e122012-10-31 08:58:58 +11001640 if (*activep && *charptr == NULL)
1641 *charptr = xstrdup(arg);
1642 break;
1643
Damien Millera6e3f012012-11-04 23:21:40 +11001644 case sAuthenticationMethods:
1645 if (*activep && options->num_auth_methods == 0) {
1646 while ((arg = strdelim(&cp)) && *arg != '\0') {
1647 if (options->num_auth_methods >=
1648 MAX_AUTH_METHODS)
1649 fatal("%s line %d: "
1650 "too many authentication methods.",
1651 filename, linenum);
1652 if (auth2_methods_valid(arg, 0) != 0)
1653 fatal("%s line %d: invalid "
1654 "authentication method list.",
1655 filename, linenum);
1656 options->auth_methods[
1657 options->num_auth_methods++] = xstrdup(arg);
1658 }
1659 }
1660 return 0;
1661
Damien Miller7acefbb2014-07-18 14:11:24 +10001662 case sStreamLocalBindMask:
1663 arg = strdelim(&cp);
1664 if (!arg || *arg == '\0')
1665 fatal("%s line %d: missing StreamLocalBindMask argument.",
1666 filename, linenum);
1667 /* Parse mode in octal format */
1668 value = strtol(arg, &p, 8);
1669 if (arg == p || value < 0 || value > 0777)
1670 fatal("%s line %d: Bad mask.", filename, linenum);
1671 options->fwd_opts.streamlocal_bind_mask = (mode_t)value;
1672 break;
1673
1674 case sStreamLocalBindUnlink:
1675 intptr = &options->fwd_opts.streamlocal_bind_unlink;
1676 goto parse_flag;
1677
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001678 case sFingerprintHash:
1679 arg = strdelim(&cp);
1680 if (!arg || *arg == '\0')
1681 fatal("%.200s line %d: Missing argument.",
1682 filename, linenum);
1683 if ((value = ssh_digest_alg_by_name(arg)) == -1)
1684 fatal("%.200s line %d: Invalid hash algorithm \"%s\".",
1685 filename, linenum, arg);
1686 if (*activep)
1687 options->fingerprint_hash = value;
1688 break;
1689
Ben Lindstromade03f62001-12-06 18:22:17 +00001690 case sDeprecated:
Damien Miller996acd22003-04-09 20:59:48 +10001691 logit("%s line %d: Deprecated option %s",
Ben Lindstromade03f62001-12-06 18:22:17 +00001692 filename, linenum, arg);
1693 while (arg)
1694 arg = strdelim(&cp);
1695 break;
1696
Damien Millerf9b3feb2003-05-16 11:38:32 +10001697 case sUnsupported:
1698 logit("%s line %d: Unsupported option %s",
1699 filename, linenum, arg);
1700 while (arg)
1701 arg = strdelim(&cp);
1702 break;
1703
Ben Lindstromade03f62001-12-06 18:22:17 +00001704 default:
1705 fatal("%s line %d: Missing handler for opcode %s (%d)",
1706 filename, linenum, arg, opcode);
1707 }
1708 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1709 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1710 filename, linenum, arg);
1711 return 0;
1712}
1713
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001714/* Reads the server configuration file. */
1715
Damien Miller4af51302000-04-16 11:18:38 +10001716void
Darren Tucker645ab752004-06-25 13:33:20 +10001717load_server_config(const char *filename, Buffer *conf)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001718{
Damien Miller46cb75a2012-07-31 12:22:37 +10001719 char line[4096], *cp;
Ben Lindstrome1353632002-06-23 21:29:23 +00001720 FILE *f;
Damien Miller46cb75a2012-07-31 12:22:37 +10001721 int lineno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001722
Darren Tucker645ab752004-06-25 13:33:20 +10001723 debug2("%s: filename %s", __func__, filename);
1724 if ((f = fopen(filename, "r")) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001725 perror(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001726 exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001727 }
Darren Tucker645ab752004-06-25 13:33:20 +10001728 buffer_clear(conf);
Damien Miller95def091999-11-25 00:26:21 +11001729 while (fgets(line, sizeof(line), f)) {
Damien Miller46cb75a2012-07-31 12:22:37 +10001730 lineno++;
1731 if (strlen(line) == sizeof(line) - 1)
1732 fatal("%s line %d too long", filename, lineno);
Darren Tucker645ab752004-06-25 13:33:20 +10001733 /*
1734 * Trim out comments and strip whitespace
Darren Tuckerfc959702004-07-17 16:12:08 +10001735 * NB - preserve newlines, they are needed to reproduce
Darren Tucker645ab752004-06-25 13:33:20 +10001736 * line numbers later for error messages
1737 */
1738 if ((cp = strchr(line, '#')) != NULL)
1739 memcpy(cp, "\n", 2);
1740 cp = line + strspn(line, " \t\r");
1741
1742 buffer_append(conf, cp, strlen(cp));
1743 }
1744 buffer_append(conf, "\0", 1);
1745 fclose(f);
1746 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1747}
1748
1749void
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001750parse_server_match_config(ServerOptions *options,
1751 struct connection_info *connectinfo)
Darren Tucker645ab752004-06-25 13:33:20 +10001752{
Darren Tucker45150472006-07-12 22:34:17 +10001753 ServerOptions mo;
1754
1755 initialize_server_options(&mo);
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001756 parse_server_config(&mo, "reprocess config", &cfg, connectinfo);
Darren Tucker1629c072007-02-19 22:25:37 +11001757 copy_set_server_options(options, &mo, 0);
Darren Tucker45150472006-07-12 22:34:17 +10001758}
1759
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001760int parse_server_match_testspec(struct connection_info *ci, char *spec)
1761{
1762 char *p;
1763
1764 while ((p = strsep(&spec, ",")) && *p != '\0') {
1765 if (strncmp(p, "addr=", 5) == 0) {
1766 ci->address = xstrdup(p + 5);
1767 } else if (strncmp(p, "host=", 5) == 0) {
1768 ci->host = xstrdup(p + 5);
1769 } else if (strncmp(p, "user=", 5) == 0) {
1770 ci->user = xstrdup(p + 5);
1771 } else if (strncmp(p, "laddr=", 6) == 0) {
1772 ci->laddress = xstrdup(p + 6);
1773 } else if (strncmp(p, "lport=", 6) == 0) {
1774 ci->lport = a2port(p + 6);
1775 if (ci->lport == -1) {
1776 fprintf(stderr, "Invalid port '%s' in test mode"
1777 " specification %s\n", p+6, p);
1778 return -1;
1779 }
1780 } else {
1781 fprintf(stderr, "Invalid test mode specification %s\n",
1782 p);
1783 return -1;
1784 }
1785 }
1786 return 0;
1787}
1788
1789/*
1790 * returns 1 for a complete spec, 0 for partial spec and -1 for an
1791 * empty spec.
1792 */
1793int server_match_spec_complete(struct connection_info *ci)
1794{
1795 if (ci->user && ci->host && ci->address)
1796 return 1; /* complete */
1797 if (!ci->user && !ci->host && !ci->address)
1798 return -1; /* empty */
1799 return 0; /* partial */
1800}
1801
Darren Tucker1629c072007-02-19 22:25:37 +11001802/*
1803 * Copy any supported values that are set.
1804 *
Darren Tucker3b59dfa2009-06-21 17:54:47 +10001805 * If the preauth flag is set, we do not bother copying the string or
Darren Tucker1629c072007-02-19 22:25:37 +11001806 * array values that are not used pre-authentication, because any that we
1807 * do use must be explictly sent in mm_getpwnamallow().
1808 */
Darren Tucker45150472006-07-12 22:34:17 +10001809void
Darren Tucker1629c072007-02-19 22:25:37 +11001810copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
Darren Tucker45150472006-07-12 22:34:17 +10001811{
Damien Miller534b2cc2013-12-05 14:07:27 +11001812#define M_CP_INTOPT(n) do {\
1813 if (src->n != -1) \
1814 dst->n = src->n; \
1815} while (0)
1816
Darren Tucker1629c072007-02-19 22:25:37 +11001817 M_CP_INTOPT(password_authentication);
1818 M_CP_INTOPT(gss_authentication);
1819 M_CP_INTOPT(rsa_authentication);
1820 M_CP_INTOPT(pubkey_authentication);
1821 M_CP_INTOPT(kerberos_authentication);
1822 M_CP_INTOPT(hostbased_authentication);
Damien Millerab6de352010-06-26 09:38:45 +10001823 M_CP_INTOPT(hostbased_uses_name_from_packet_only);
Darren Tucker1629c072007-02-19 22:25:37 +11001824 M_CP_INTOPT(kbd_interactive_authentication);
Darren Tucker15f94272008-01-01 20:36:56 +11001825 M_CP_INTOPT(permit_root_login);
Damien Miller51bde602008-11-03 19:23:10 +11001826 M_CP_INTOPT(permit_empty_passwd);
Darren Tucker1629c072007-02-19 22:25:37 +11001827
1828 M_CP_INTOPT(allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10001829 M_CP_INTOPT(allow_streamlocal_forwarding);
Damien Miller4f755cd2008-05-19 14:57:41 +10001830 M_CP_INTOPT(allow_agent_forwarding);
Damien Millerab6de352010-06-26 09:38:45 +10001831 M_CP_INTOPT(permit_tun);
Damien Miller7acefbb2014-07-18 14:11:24 +10001832 M_CP_INTOPT(fwd_opts.gateway_ports);
Darren Tucker1629c072007-02-19 22:25:37 +11001833 M_CP_INTOPT(x11_display_offset);
1834 M_CP_INTOPT(x11_forwarding);
1835 M_CP_INTOPT(x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11001836 M_CP_INTOPT(permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10001837 M_CP_INTOPT(permit_user_rc);
Damien Miller7207f642008-05-19 15:34:50 +10001838 M_CP_INTOPT(max_sessions);
Damien Miller307c1d12008-06-16 07:56:20 +10001839 M_CP_INTOPT(max_authtries);
Damien Miller0dac6fb2010-11-20 15:19:38 +11001840 M_CP_INTOPT(ip_qos_interactive);
1841 M_CP_INTOPT(ip_qos_bulk);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001842 M_CP_INTOPT(rekey_limit);
1843 M_CP_INTOPT(rekey_interval);
Darren Tucker1629c072007-02-19 22:25:37 +11001844
Damien Miller534b2cc2013-12-05 14:07:27 +11001845 /* M_CP_STROPT and M_CP_STRARRAYOPT should not appear before here */
1846#define M_CP_STROPT(n) do {\
1847 if (src->n != NULL && dst->n != src->n) { \
1848 free(dst->n); \
1849 dst->n = src->n; \
1850 } \
1851} while(0)
1852#define M_CP_STRARRAYOPT(n, num_n) do {\
1853 if (src->num_n != 0) { \
1854 for (dst->num_n = 0; dst->num_n < src->num_n; dst->num_n++) \
1855 dst->n[dst->num_n] = xstrdup(src->n[dst->num_n]); \
1856 } \
1857} while(0)
1858
Damien Millerf2e407e2011-05-20 19:04:14 +10001859 /* See comment in servconf.h */
1860 COPY_MATCH_STRING_OPTS();
Damien Miller5d74e582011-05-20 19:03:31 +10001861
Damien Millerc2411902011-05-20 19:03:49 +10001862 /*
1863 * The only things that should be below this point are string options
1864 * which are only used after authentication.
1865 */
Damien Miller5d74e582011-05-20 19:03:31 +10001866 if (preauth)
1867 return;
Damien Millerd8478b62011-05-29 21:39:36 +10001868
Damien Miller5d74e582011-05-20 19:03:31 +10001869 M_CP_STROPT(adm_forced_command);
1870 M_CP_STROPT(chroot_directory);
Darren Tucker45150472006-07-12 22:34:17 +10001871}
1872
Darren Tucker1629c072007-02-19 22:25:37 +11001873#undef M_CP_INTOPT
1874#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +10001875#undef M_CP_STRARRAYOPT
Darren Tucker1629c072007-02-19 22:25:37 +11001876
Darren Tucker45150472006-07-12 22:34:17 +10001877void
1878parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001879 struct connection_info *connectinfo)
Darren Tucker45150472006-07-12 22:34:17 +10001880{
1881 int active, linenum, bad_options = 0;
Darren Tucker9fbac712004-08-12 22:41:44 +10001882 char *cp, *obuf, *cbuf;
Darren Tucker645ab752004-06-25 13:33:20 +10001883
1884 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1885
Darren Tucker9fbac712004-08-12 22:41:44 +10001886 obuf = cbuf = xstrdup(buffer_ptr(conf));
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001887 active = connectinfo ? 0 : 1;
Darren Tucker137e9c92004-08-13 21:30:24 +10001888 linenum = 1;
Darren Tucker47eede72005-03-14 23:08:12 +11001889 while ((cp = strsep(&cbuf, "\n")) != NULL) {
Darren Tucker645ab752004-06-25 13:33:20 +10001890 if (process_server_config_line(options, cp, filename,
Darren Tuckerfbcf8272012-05-19 19:37:01 +10001891 linenum++, &active, connectinfo) != 0)
Damien Miller95def091999-11-25 00:26:21 +11001892 bad_options++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001893 }
Darren Tuckera627d422013-06-02 07:31:17 +10001894 free(obuf);
Ben Lindstromb5cdc662001-04-16 02:13:26 +00001895 if (bad_options > 0)
1896 fatal("%s: terminating, %d bad configuration options",
1897 filename, bad_options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001898}
Darren Tuckere7140f22008-06-10 23:01:51 +10001899
1900static const char *
Damien Miller82c55872011-06-23 08:20:30 +10001901fmt_multistate_int(int val, const struct multistate *m)
1902{
1903 u_int i;
1904
1905 for (i = 0; m[i].key != NULL; i++) {
1906 if (m[i].value == val)
1907 return m[i].key;
1908 }
1909 return "UNKNOWN";
1910}
1911
1912static const char *
Darren Tuckere7140f22008-06-10 23:01:51 +10001913fmt_intarg(ServerOpCodes code, int val)
1914{
Damien Miller82c55872011-06-23 08:20:30 +10001915 if (val == -1)
1916 return "unset";
1917 switch (code) {
1918 case sAddressFamily:
1919 return fmt_multistate_int(val, multistate_addressfamily);
1920 case sPermitRootLogin:
1921 return fmt_multistate_int(val, multistate_permitrootlogin);
1922 case sGatewayPorts:
1923 return fmt_multistate_int(val, multistate_gatewayports);
1924 case sCompression:
1925 return fmt_multistate_int(val, multistate_compression);
Damien Miller69ff1df2011-06-23 08:30:03 +10001926 case sUsePrivilegeSeparation:
1927 return fmt_multistate_int(val, multistate_privsep);
Damien Milleraa5b3f82012-12-03 09:50:54 +11001928 case sAllowTcpForwarding:
1929 return fmt_multistate_int(val, multistate_tcpfwd);
Damien Miller7acefbb2014-07-18 14:11:24 +10001930 case sAllowStreamLocalForwarding:
1931 return fmt_multistate_int(val, multistate_tcpfwd);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001932 case sFingerprintHash:
1933 return ssh_digest_alg_name(val);
Damien Miller82c55872011-06-23 08:20:30 +10001934 case sProtocol:
Darren Tuckere7140f22008-06-10 23:01:51 +10001935 switch (val) {
1936 case SSH_PROTO_1:
1937 return "1";
1938 case SSH_PROTO_2:
1939 return "2";
1940 case (SSH_PROTO_1|SSH_PROTO_2):
1941 return "2,1";
1942 default:
1943 return "UNKNOWN";
1944 }
Damien Miller82c55872011-06-23 08:20:30 +10001945 default:
1946 switch (val) {
1947 case 0:
1948 return "no";
1949 case 1:
1950 return "yes";
1951 default:
1952 return "UNKNOWN";
1953 }
Darren Tuckere7140f22008-06-10 23:01:51 +10001954 }
Darren Tuckere7140f22008-06-10 23:01:51 +10001955}
1956
1957static const char *
1958lookup_opcode_name(ServerOpCodes code)
1959{
1960 u_int i;
1961
1962 for (i = 0; keywords[i].name != NULL; i++)
1963 if (keywords[i].opcode == code)
1964 return(keywords[i].name);
1965 return "UNKNOWN";
1966}
1967
1968static void
1969dump_cfg_int(ServerOpCodes code, int val)
1970{
1971 printf("%s %d\n", lookup_opcode_name(code), val);
1972}
1973
1974static void
1975dump_cfg_fmtint(ServerOpCodes code, int val)
1976{
1977 printf("%s %s\n", lookup_opcode_name(code), fmt_intarg(code, val));
1978}
1979
1980static void
1981dump_cfg_string(ServerOpCodes code, const char *val)
1982{
1983 if (val == NULL)
1984 return;
1985 printf("%s %s\n", lookup_opcode_name(code), val);
1986}
1987
1988static void
1989dump_cfg_strarray(ServerOpCodes code, u_int count, char **vals)
1990{
1991 u_int i;
1992
1993 for (i = 0; i < count; i++)
Damien Millerd8478b62011-05-29 21:39:36 +10001994 printf("%s %s\n", lookup_opcode_name(code), vals[i]);
1995}
1996
1997static void
1998dump_cfg_strarray_oneline(ServerOpCodes code, u_int count, char **vals)
1999{
2000 u_int i;
2001
2002 printf("%s", lookup_opcode_name(code));
2003 for (i = 0; i < count; i++)
2004 printf(" %s", vals[i]);
2005 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10002006}
2007
2008void
2009dump_config(ServerOptions *o)
2010{
2011 u_int i;
2012 int ret;
2013 struct addrinfo *ai;
2014 char addr[NI_MAXHOST], port[NI_MAXSERV], *s = NULL;
2015
2016 /* these are usually at the top of the config */
2017 for (i = 0; i < o->num_ports; i++)
2018 printf("port %d\n", o->ports[i]);
2019 dump_cfg_fmtint(sProtocol, o->protocol);
2020 dump_cfg_fmtint(sAddressFamily, o->address_family);
2021
2022 /* ListenAddress must be after Port */
2023 for (ai = o->listen_addrs; ai; ai = ai->ai_next) {
2024 if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
2025 sizeof(addr), port, sizeof(port),
2026 NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
2027 error("getnameinfo failed: %.100s",
2028 (ret != EAI_SYSTEM) ? gai_strerror(ret) :
2029 strerror(errno));
2030 } else {
2031 if (ai->ai_family == AF_INET6)
2032 printf("listenaddress [%s]:%s\n", addr, port);
2033 else
2034 printf("listenaddress %s:%s\n", addr, port);
2035 }
2036 }
2037
2038 /* integer arguments */
Damien Miller212f0b02008-07-23 17:42:29 +10002039#ifdef USE_PAM
2040 dump_cfg_int(sUsePAM, o->use_pam);
2041#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002042 dump_cfg_int(sServerKeyBits, o->server_key_bits);
2043 dump_cfg_int(sLoginGraceTime, o->login_grace_time);
2044 dump_cfg_int(sKeyRegenerationTime, o->key_regeneration_time);
2045 dump_cfg_int(sX11DisplayOffset, o->x11_display_offset);
2046 dump_cfg_int(sMaxAuthTries, o->max_authtries);
Damien Miller7fc5c0f2008-11-05 16:12:11 +11002047 dump_cfg_int(sMaxSessions, o->max_sessions);
Darren Tuckere7140f22008-06-10 23:01:51 +10002048 dump_cfg_int(sClientAliveInterval, o->client_alive_interval);
2049 dump_cfg_int(sClientAliveCountMax, o->client_alive_count_max);
2050
2051 /* formatted integer arguments */
2052 dump_cfg_fmtint(sPermitRootLogin, o->permit_root_login);
2053 dump_cfg_fmtint(sIgnoreRhosts, o->ignore_rhosts);
2054 dump_cfg_fmtint(sIgnoreUserKnownHosts, o->ignore_user_known_hosts);
2055 dump_cfg_fmtint(sRhostsRSAAuthentication, o->rhosts_rsa_authentication);
2056 dump_cfg_fmtint(sHostbasedAuthentication, o->hostbased_authentication);
2057 dump_cfg_fmtint(sHostbasedUsesNameFromPacketOnly,
2058 o->hostbased_uses_name_from_packet_only);
2059 dump_cfg_fmtint(sRSAAuthentication, o->rsa_authentication);
2060 dump_cfg_fmtint(sPubkeyAuthentication, o->pubkey_authentication);
Damien Miller6ef430d2008-07-23 17:40:04 +10002061#ifdef KRB5
Darren Tuckere7140f22008-06-10 23:01:51 +10002062 dump_cfg_fmtint(sKerberosAuthentication, o->kerberos_authentication);
2063 dump_cfg_fmtint(sKerberosOrLocalPasswd, o->kerberos_or_local_passwd);
2064 dump_cfg_fmtint(sKerberosTicketCleanup, o->kerberos_ticket_cleanup);
Damien Miller6ef430d2008-07-23 17:40:04 +10002065# ifdef USE_AFS
Darren Tuckere7140f22008-06-10 23:01:51 +10002066 dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
Damien Miller6ef430d2008-07-23 17:40:04 +10002067# endif
2068#endif
2069#ifdef GSSAPI
Darren Tuckere7140f22008-06-10 23:01:51 +10002070 dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
2071 dump_cfg_fmtint(sGssCleanupCreds, o->gss_cleanup_creds);
Damien Miller6ef430d2008-07-23 17:40:04 +10002072#endif
Darren Tuckere7140f22008-06-10 23:01:51 +10002073 dump_cfg_fmtint(sPasswordAuthentication, o->password_authentication);
2074 dump_cfg_fmtint(sKbdInteractiveAuthentication,
2075 o->kbd_interactive_authentication);
2076 dump_cfg_fmtint(sChallengeResponseAuthentication,
2077 o->challenge_response_authentication);
2078 dump_cfg_fmtint(sPrintMotd, o->print_motd);
2079 dump_cfg_fmtint(sPrintLastLog, o->print_lastlog);
2080 dump_cfg_fmtint(sX11Forwarding, o->x11_forwarding);
2081 dump_cfg_fmtint(sX11UseLocalhost, o->x11_use_localhost);
Damien Miller5ff30c62013-10-30 22:21:50 +11002082 dump_cfg_fmtint(sPermitTTY, o->permit_tty);
Damien Miller72e6b5c2014-07-04 09:00:04 +10002083 dump_cfg_fmtint(sPermitUserRC, o->permit_user_rc);
Darren Tuckere7140f22008-06-10 23:01:51 +10002084 dump_cfg_fmtint(sStrictModes, o->strict_modes);
2085 dump_cfg_fmtint(sTCPKeepAlive, o->tcp_keep_alive);
2086 dump_cfg_fmtint(sEmptyPasswd, o->permit_empty_passwd);
2087 dump_cfg_fmtint(sPermitUserEnvironment, o->permit_user_env);
2088 dump_cfg_fmtint(sUseLogin, o->use_login);
2089 dump_cfg_fmtint(sCompression, o->compression);
Damien Miller7acefbb2014-07-18 14:11:24 +10002090 dump_cfg_fmtint(sGatewayPorts, o->fwd_opts.gateway_ports);
Darren Tuckere7140f22008-06-10 23:01:51 +10002091 dump_cfg_fmtint(sUseDNS, o->use_dns);
2092 dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
Damien Miller7acefbb2014-07-18 14:11:24 +10002093 dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
Darren Tuckere7140f22008-06-10 23:01:51 +10002094 dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
djm@openbsd.org56d1c832014-12-21 22:27:55 +00002095 dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
Darren Tuckere7140f22008-06-10 23:01:51 +10002096
2097 /* string arguments */
2098 dump_cfg_string(sPidFile, o->pid_file);
2099 dump_cfg_string(sXAuthLocation, o->xauth_location);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002100 dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
2101 dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
Darren Tuckere7140f22008-06-10 23:01:51 +10002102 dump_cfg_string(sBanner, o->banner);
Darren Tuckere7140f22008-06-10 23:01:51 +10002103 dump_cfg_string(sForceCommand, o->adm_forced_command);
Darren Tuckerd3300452010-01-10 19:26:43 +11002104 dump_cfg_string(sChrootDirectory, o->chroot_directory);
Damien Miller1aed65e2010-03-04 21:53:35 +11002105 dump_cfg_string(sTrustedUserCAKeys, o->trusted_user_ca_keys);
2106 dump_cfg_string(sRevokedKeys, o->revoked_keys_file);
Damien Miller30da3442010-05-10 11:58:03 +10002107 dump_cfg_string(sAuthorizedPrincipalsFile,
2108 o->authorized_principals_file);
Damien Miller23528812012-04-22 11:24:43 +10002109 dump_cfg_string(sVersionAddendum, o->version_addendum);
Damien Miller09d3e122012-10-31 08:58:58 +11002110 dump_cfg_string(sAuthorizedKeysCommand, o->authorized_keys_command);
2111 dump_cfg_string(sAuthorizedKeysCommandUser, o->authorized_keys_command_user);
Damien Miller85b45e02013-07-20 13:21:52 +10002112 dump_cfg_string(sHostKeyAgent, o->host_key_agent);
djm@openbsd.org57d378e2014-08-19 23:58:28 +00002113 dump_cfg_string(sKexAlgorithms,
djm@openbsd.org259a02e2014-10-13 00:38:35 +00002114 o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
Darren Tuckere7140f22008-06-10 23:01:51 +10002115
2116 /* string arguments requiring a lookup */
2117 dump_cfg_string(sLogLevel, log_level_name(o->log_level));
2118 dump_cfg_string(sLogFacility, log_facility_name(o->log_facility));
2119
2120 /* string array arguments */
Damien Millerd8478b62011-05-29 21:39:36 +10002121 dump_cfg_strarray_oneline(sAuthorizedKeysFile, o->num_authkeys_files,
2122 o->authorized_keys_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002123 dump_cfg_strarray(sHostKeyFile, o->num_host_key_files,
2124 o->host_key_files);
Damien Miller0a80ca12010-02-27 07:55:05 +11002125 dump_cfg_strarray(sHostKeyFile, o->num_host_cert_files,
2126 o->host_cert_files);
Darren Tuckere7140f22008-06-10 23:01:51 +10002127 dump_cfg_strarray(sAllowUsers, o->num_allow_users, o->allow_users);
2128 dump_cfg_strarray(sDenyUsers, o->num_deny_users, o->deny_users);
2129 dump_cfg_strarray(sAllowGroups, o->num_allow_groups, o->allow_groups);
2130 dump_cfg_strarray(sDenyGroups, o->num_deny_groups, o->deny_groups);
2131 dump_cfg_strarray(sAcceptEnv, o->num_accept_env, o->accept_env);
Damien Millera6e3f012012-11-04 23:21:40 +11002132 dump_cfg_strarray_oneline(sAuthenticationMethods,
2133 o->num_auth_methods, o->auth_methods);
Darren Tuckere7140f22008-06-10 23:01:51 +10002134
2135 /* other arguments */
2136 for (i = 0; i < o->num_subsystems; i++)
2137 printf("subsystem %s %s\n", o->subsystem_name[i],
2138 o->subsystem_args[i]);
2139
2140 printf("maxstartups %d:%d:%d\n", o->max_startups_begin,
2141 o->max_startups_rate, o->max_startups);
2142
2143 for (i = 0; tunmode_desc[i].val != -1; i++)
2144 if (tunmode_desc[i].val == o->permit_tun) {
2145 s = tunmode_desc[i].text;
2146 break;
2147 }
2148 dump_cfg_string(sPermitTunnel, s);
2149
Damien Miller91475862011-05-05 14:14:34 +10002150 printf("ipqos %s ", iptos2str(o->ip_qos_interactive));
2151 printf("%s\n", iptos2str(o->ip_qos_bulk));
Damien Miller0dac6fb2010-11-20 15:19:38 +11002152
Damien Millera6d6c1f2013-08-21 02:40:01 +10002153 printf("rekeylimit %lld %d\n", (long long)o->rekey_limit,
2154 o->rekey_interval);
Darren Tucker5f96f3b2013-05-16 20:29:28 +10002155
Darren Tuckere7140f22008-06-10 23:01:51 +10002156 channel_print_adm_permitted_opens();
Darren Tuckere7140f22008-06-10 23:01:51 +10002157}