]> andersk Git - gssapi-openssh.git/blame - openssh/servconf.c
This is a derived file. It should not be in CVS.
[gssapi-openssh.git] / openssh / servconf.c
CommitLineData
d4487008 1/* $OpenBSD: servconf.c,v 1.172 2007/04/23 10:15:39 dtucker Exp $ */
3c0ef626 2/*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 *
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 */
12
13#include "includes.h"
3c0ef626 14
9108f8d9 15#include <sys/types.h>
16#include <sys/socket.h>
17
18#include <netdb.h>
19#include <pwd.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <signal.h>
24#include <unistd.h>
25#include <stdarg.h>
26
27#include "xmalloc.h"
3c0ef626 28#include "ssh.h"
29#include "log.h"
9108f8d9 30#include "buffer.h"
3c0ef626 31#include "servconf.h"
3c0ef626 32#include "compat.h"
33#include "pathnames.h"
3c0ef626 34#include "misc.h"
35#include "cipher.h"
9108f8d9 36#include "key.h"
3c0ef626 37#include "kex.h"
38#include "mac.h"
9108f8d9 39#include "match.h"
40#include "channels.h"
41#include "groupaccess.h"
3c0ef626 42
43static void add_listen_addr(ServerOptions *, char *, u_short);
44static void add_one_listen_addr(ServerOptions *, char *, u_short);
45
700318f3 46/* Use of privilege separation or not */
47extern int use_privsep;
9108f8d9 48extern Buffer cfg;
3c0ef626 49
50/* Initializes the server options to their default values. */
51
52void
53initialize_server_options(ServerOptions *options)
54{
55 memset(options, 0, sizeof(*options));
56
57 /* Portable-specific options */
0fff78ff 58 options->use_pam = -1;
3c0ef626 59
60 /* Standard Options */
61 options->num_ports = 0;
62 options->ports_from_cmdline = 0;
63 options->listen_addrs = NULL;
996d5e62 64 options->address_family = -1;
3c0ef626 65 options->num_host_key_files = 0;
66 options->pid_file = NULL;
67 options->server_key_bits = -1;
68 options->login_grace_time = -1;
69 options->key_regeneration_time = -1;
70 options->permit_root_login = PERMIT_NOT_SET;
71 options->ignore_rhosts = -1;
72 options->ignore_user_known_hosts = -1;
73 options->print_motd = -1;
74 options->print_lastlog = -1;
75 options->x11_forwarding = -1;
76 options->x11_display_offset = -1;
e9a17296 77 options->x11_use_localhost = -1;
3c0ef626 78 options->xauth_location = NULL;
79 options->strict_modes = -1;
cdd66111 80 options->tcp_keep_alive = -1;
e9a17296 81 options->log_facility = SYSLOG_FACILITY_NOT_SET;
82 options->log_level = SYSLOG_LEVEL_NOT_SET;
3c0ef626 83 options->rhosts_rsa_authentication = -1;
84 options->hostbased_authentication = -1;
85 options->hostbased_uses_name_from_packet_only = -1;
86 options->rsa_authentication = -1;
87 options->pubkey_authentication = -1;
3c0ef626 88 options->kerberos_authentication = -1;
89 options->kerberos_or_local_passwd = -1;
90 options->kerberos_ticket_cleanup = -1;
cdd66111 91 options->kerberos_get_afs_token = -1;
0fff78ff 92 options->gss_authentication=-1;
93 options->gss_cleanup_creds = -1;
3c0ef626 94 options->password_authentication = -1;
95 options->kbd_interactive_authentication = -1;
96 options->challenge_response_authentication = -1;
97 options->permit_empty_passwd = -1;
41b2f314 98 options->permit_user_env = -1;
3c0ef626 99 options->use_login = -1;
f5799ae1 100 options->compression = -1;
3c0ef626 101 options->allow_tcp_forwarding = -1;
102 options->num_allow_users = 0;
103 options->num_deny_users = 0;
104 options->num_allow_groups = 0;
105 options->num_deny_groups = 0;
106 options->ciphers = NULL;
107 options->macs = NULL;
108 options->protocol = SSH_PROTO_UNKNOWN;
109 options->gateway_ports = -1;
110 options->num_subsystems = 0;
111 options->max_startups_begin = -1;
112 options->max_startups_rate = -1;
113 options->max_startups = -1;
c9f39d2c 114 options->max_authtries = -1;
3c0ef626 115 options->banner = NULL;
0fff78ff 116 options->use_dns = -1;
3c0ef626 117 options->client_alive_interval = -1;
118 options->client_alive_count_max = -1;
119 options->authorized_keys_file = NULL;
120 options->authorized_keys_file2 = NULL;
c9f39d2c 121 options->num_accept_env = 0;
2c06c99b 122 options->permit_tun = -1;
9108f8d9 123 options->num_permitted_opens = -1;
124 options->adm_forced_command = NULL;
3c0ef626 125}
126
127void
128fill_default_server_options(ServerOptions *options)
129{
130 /* Portable-specific options */
0fff78ff 131 if (options->use_pam == -1)
acc3d05e 132 options->use_pam = 0;
3c0ef626 133
134 /* Standard Options */
135 if (options->protocol == SSH_PROTO_UNKNOWN)
136 options->protocol = SSH_PROTO_1|SSH_PROTO_2;
137 if (options->num_host_key_files == 0) {
138 /* fill default hostkeys for protocols */
139 if (options->protocol & SSH_PROTO_1)
e9a17296 140 options->host_key_files[options->num_host_key_files++] =
141 _PATH_HOST_KEY_FILE;
142 if (options->protocol & SSH_PROTO_2) {
143 options->host_key_files[options->num_host_key_files++] =
144 _PATH_HOST_RSA_KEY_FILE;
145 options->host_key_files[options->num_host_key_files++] =
146 _PATH_HOST_DSA_KEY_FILE;
147 }
3c0ef626 148 }
149 if (options->num_ports == 0)
150 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
151 if (options->listen_addrs == NULL)
152 add_listen_addr(options, NULL, 0);
153 if (options->pid_file == NULL)
154 options->pid_file = _PATH_SSH_DAEMON_PID_FILE;
155 if (options->server_key_bits == -1)
156 options->server_key_bits = 768;
157 if (options->login_grace_time == -1)
41b2f314 158 options->login_grace_time = 120;
3c0ef626 159 if (options->key_regeneration_time == -1)
160 options->key_regeneration_time = 3600;
161 if (options->permit_root_login == PERMIT_NOT_SET)
162 options->permit_root_login = PERMIT_YES;
163 if (options->ignore_rhosts == -1)
164 options->ignore_rhosts = 1;
165 if (options->ignore_user_known_hosts == -1)
166 options->ignore_user_known_hosts = 0;
167 if (options->print_motd == -1)
168 options->print_motd = 1;
169 if (options->print_lastlog == -1)
170 options->print_lastlog = 1;
171 if (options->x11_forwarding == -1)
172 options->x11_forwarding = 0;
173 if (options->x11_display_offset == -1)
174 options->x11_display_offset = 10;
e9a17296 175 if (options->x11_use_localhost == -1)
176 options->x11_use_localhost = 1;
3c0ef626 177 if (options->xauth_location == NULL)
178 options->xauth_location = _PATH_XAUTH;
3c0ef626 179 if (options->strict_modes == -1)
180 options->strict_modes = 1;
cdd66111 181 if (options->tcp_keep_alive == -1)
182 options->tcp_keep_alive = 1;
e9a17296 183 if (options->log_facility == SYSLOG_FACILITY_NOT_SET)
3c0ef626 184 options->log_facility = SYSLOG_FACILITY_AUTH;
e9a17296 185 if (options->log_level == SYSLOG_LEVEL_NOT_SET)
3c0ef626 186 options->log_level = SYSLOG_LEVEL_INFO;
3c0ef626 187 if (options->rhosts_rsa_authentication == -1)
188 options->rhosts_rsa_authentication = 0;
189 if (options->hostbased_authentication == -1)
190 options->hostbased_authentication = 0;
191 if (options->hostbased_uses_name_from_packet_only == -1)
192 options->hostbased_uses_name_from_packet_only = 0;
193 if (options->rsa_authentication == -1)
194 options->rsa_authentication = 1;
195 if (options->pubkey_authentication == -1)
196 options->pubkey_authentication = 1;
3c0ef626 197 if (options->kerberos_authentication == -1)
700318f3 198 options->kerberos_authentication = 0;
3c0ef626 199 if (options->kerberos_or_local_passwd == -1)
200 options->kerberos_or_local_passwd = 1;
201 if (options->kerberos_ticket_cleanup == -1)
202 options->kerberos_ticket_cleanup = 1;
cdd66111 203 if (options->kerberos_get_afs_token == -1)
204 options->kerberos_get_afs_token = 0;
0fff78ff 205 if (options->gss_authentication == -1)
206 options->gss_authentication = 0;
207 if (options->gss_cleanup_creds == -1)
208 options->gss_cleanup_creds = 1;
3c0ef626 209 if (options->password_authentication == -1)
210 options->password_authentication = 1;
211 if (options->kbd_interactive_authentication == -1)
212 options->kbd_interactive_authentication = 0;
213 if (options->challenge_response_authentication == -1)
214 options->challenge_response_authentication = 1;
215 if (options->permit_empty_passwd == -1)
216 options->permit_empty_passwd = 0;
41b2f314 217 if (options->permit_user_env == -1)
218 options->permit_user_env = 0;
3c0ef626 219 if (options->use_login == -1)
220 options->use_login = 0;
f5799ae1 221 if (options->compression == -1)
665a873d 222 options->compression = COMP_DELAYED;
3c0ef626 223 if (options->allow_tcp_forwarding == -1)
224 options->allow_tcp_forwarding = 1;
225 if (options->gateway_ports == -1)
226 options->gateway_ports = 0;
227 if (options->max_startups == -1)
228 options->max_startups = 10;
229 if (options->max_startups_rate == -1)
230 options->max_startups_rate = 100; /* 100% */
231 if (options->max_startups_begin == -1)
232 options->max_startups_begin = options->max_startups;
c9f39d2c 233 if (options->max_authtries == -1)
234 options->max_authtries = DEFAULT_AUTH_FAIL_MAX;
0fff78ff 235 if (options->use_dns == -1)
236 options->use_dns = 1;
3c0ef626 237 if (options->client_alive_interval == -1)
e9a17296 238 options->client_alive_interval = 0;
3c0ef626 239 if (options->client_alive_count_max == -1)
240 options->client_alive_count_max = 3;
241 if (options->authorized_keys_file2 == NULL) {
242 /* authorized_keys_file2 falls back to authorized_keys_file */
243 if (options->authorized_keys_file != NULL)
244 options->authorized_keys_file2 = options->authorized_keys_file;
245 else
246 options->authorized_keys_file2 = _PATH_SSH_USER_PERMITTED_KEYS2;
247 }
248 if (options->authorized_keys_file == NULL)
249 options->authorized_keys_file = _PATH_SSH_USER_PERMITTED_KEYS;
2c06c99b 250 if (options->permit_tun == -1)
251 options->permit_tun = SSH_TUNMODE_NO;
700318f3 252
f5799ae1 253 /* Turn privilege separation on by default */
700318f3 254 if (use_privsep == -1)
f5799ae1 255 use_privsep = 1;
256
41b2f314 257#ifndef HAVE_MMAP
f5799ae1 258 if (use_privsep && options->compression == 1) {
259 error("This platform does not support both privilege "
260 "separation and compression");
261 error("Compression disabled");
262 options->compression = 0;
263 }
264#endif
265
3c0ef626 266}
267
268/* Keyword tokens. */
269typedef enum {
270 sBadOption, /* == unknown option */
271 /* Portable-specific options */
0fff78ff 272 sUsePAM,
3c0ef626 273 /* Standard Options */
274 sPort, sHostKeyFile, sServerKeyBits, sLoginGraceTime, sKeyRegenerationTime,
275 sPermitRootLogin, sLogFacility, sLogLevel,
0fff78ff 276 sRhostsRSAAuthentication, sRSAAuthentication,
3c0ef626 277 sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
cdd66111 278 sKerberosGetAFSToken,
0fff78ff 279 sKerberosTgtPassing, sChallengeResponseAuthentication,
996d5e62 280 sPasswordAuthentication, sKbdInteractiveAuthentication,
281 sListenAddress, sAddressFamily,
3c0ef626 282 sPrintMotd, sPrintLastLog, sIgnoreRhosts,
e9a17296 283 sX11Forwarding, sX11DisplayOffset, sX11UseLocalhost,
cdd66111 284 sStrictModes, sEmptyPasswd, sTCPKeepAlive,
41b2f314 285 sPermitUserEnvironment, sUseLogin, sAllowTcpForwarding, sCompression,
3c0ef626 286 sAllowUsers, sDenyUsers, sAllowGroups, sDenyGroups,
287 sIgnoreUserKnownHosts, sCiphers, sMacs, sProtocol, sPidFile,
c9f39d2c 288 sGatewayPorts, sPubkeyAuthentication, sXAuthLocation, sSubsystem,
289 sMaxStartups, sMaxAuthTries,
0fff78ff 290 sBanner, sUseDNS, sHostbasedAuthentication,
e9a17296 291 sHostbasedUsesNameFromPacketOnly, sClientAliveInterval,
3c0ef626 292 sClientAliveCountMax, sAuthorizedKeysFile, sAuthorizedKeysFile2,
2c06c99b 293 sGssAuthentication, sGssCleanupCreds, sAcceptEnv, sPermitTunnel,
9108f8d9 294 sMatch, sPermitOpen, sForceCommand,
700318f3 295 sUsePrivilegeSeparation,
0fff78ff 296 sDeprecated, sUnsupported
3c0ef626 297} ServerOpCodes;
298
9108f8d9 299#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
300#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
301#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
302
3c0ef626 303/* Textual representation of the tokens. */
304static struct {
305 const char *name;
306 ServerOpCodes opcode;
9108f8d9 307 u_int flags;
3c0ef626 308} keywords[] = {
309 /* Portable-specific options */
0fff78ff 310#ifdef USE_PAM
9108f8d9 311 { "usepam", sUsePAM, SSHCFG_GLOBAL },
0fff78ff 312#else
9108f8d9 313 { "usepam", sUnsupported, SSHCFG_GLOBAL },
0fff78ff 314#endif
9108f8d9 315 { "pamauthenticationviakbdint", sDeprecated, SSHCFG_GLOBAL },
3c0ef626 316 /* Standard Options */
9108f8d9 317 { "port", sPort, SSHCFG_GLOBAL },
318 { "hostkey", sHostKeyFile, SSHCFG_GLOBAL },
319 { "hostdsakey", sHostKeyFile, SSHCFG_GLOBAL }, /* alias */
320 { "pidfile", sPidFile, SSHCFG_GLOBAL },
321 { "serverkeybits", sServerKeyBits, SSHCFG_GLOBAL },
322 { "logingracetime", sLoginGraceTime, SSHCFG_GLOBAL },
323 { "keyregenerationinterval", sKeyRegenerationTime, SSHCFG_GLOBAL },
324 { "permitrootlogin", sPermitRootLogin, SSHCFG_GLOBAL },
325 { "syslogfacility", sLogFacility, SSHCFG_GLOBAL },
326 { "loglevel", sLogLevel, SSHCFG_GLOBAL },
327 { "rhostsauthentication", sDeprecated, SSHCFG_GLOBAL },
799ae497 328 { "rhostsrsaauthentication", sRhostsRSAAuthentication, SSHCFG_ALL },
329 { "hostbasedauthentication", sHostbasedAuthentication, SSHCFG_ALL },
9108f8d9 330 { "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly, SSHCFG_GLOBAL },
799ae497 331 { "rsaauthentication", sRSAAuthentication, SSHCFG_ALL },
332 { "pubkeyauthentication", sPubkeyAuthentication, SSHCFG_ALL },
9108f8d9 333 { "dsaauthentication", sPubkeyAuthentication, SSHCFG_GLOBAL }, /* alias */
0fff78ff 334#ifdef KRB5
799ae497 335 { "kerberosauthentication", sKerberosAuthentication, SSHCFG_ALL },
9108f8d9 336 { "kerberosorlocalpasswd", sKerberosOrLocalPasswd, SSHCFG_GLOBAL },
337 { "kerberosticketcleanup", sKerberosTicketCleanup, SSHCFG_GLOBAL },
cdd66111 338#ifdef USE_AFS
9108f8d9 339 { "kerberosgetafstoken", sKerberosGetAFSToken, SSHCFG_GLOBAL },
cdd66111 340#else
9108f8d9 341 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
cdd66111 342#endif
0fff78ff 343#else
799ae497 344 { "kerberosauthentication", sUnsupported, SSHCFG_ALL },
9108f8d9 345 { "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
346 { "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
347 { "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
3c0ef626 348#endif
9108f8d9 349 { "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
350 { "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
0fff78ff 351#ifdef GSSAPI
799ae497 352 { "gssapiauthentication", sGssAuthentication, SSHCFG_ALL },
9108f8d9 353 { "gssapicleanupcredentials", sGssCleanupCreds, SSHCFG_GLOBAL },
0fff78ff 354#else
799ae497 355 { "gssapiauthentication", sUnsupported, SSHCFG_ALL },
9108f8d9 356 { "gssapicleanupcredentials", sUnsupported, SSHCFG_GLOBAL },
3c0ef626 357#endif
799ae497 358 { "passwordauthentication", sPasswordAuthentication, SSHCFG_ALL },
359 { "kbdinteractiveauthentication", sKbdInteractiveAuthentication, SSHCFG_ALL },
9108f8d9 360 { "challengeresponseauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL },
361 { "skeyauthentication", sChallengeResponseAuthentication, SSHCFG_GLOBAL }, /* alias */
362 { "checkmail", sDeprecated, SSHCFG_GLOBAL },
363 { "listenaddress", sListenAddress, SSHCFG_GLOBAL },
364 { "addressfamily", sAddressFamily, SSHCFG_GLOBAL },
365 { "printmotd", sPrintMotd, SSHCFG_GLOBAL },
366 { "printlastlog", sPrintLastLog, SSHCFG_GLOBAL },
367 { "ignorerhosts", sIgnoreRhosts, SSHCFG_GLOBAL },
368 { "ignoreuserknownhosts", sIgnoreUserKnownHosts, SSHCFG_GLOBAL },
369 { "x11forwarding", sX11Forwarding, SSHCFG_ALL },
370 { "x11displayoffset", sX11DisplayOffset, SSHCFG_ALL },
371 { "x11uselocalhost", sX11UseLocalhost, SSHCFG_ALL },
372 { "xauthlocation", sXAuthLocation, SSHCFG_GLOBAL },
373 { "strictmodes", sStrictModes, SSHCFG_GLOBAL },
374 { "permitemptypasswords", sEmptyPasswd, SSHCFG_GLOBAL },
375 { "permituserenvironment", sPermitUserEnvironment, SSHCFG_GLOBAL },
376 { "uselogin", sUseLogin, SSHCFG_GLOBAL },
377 { "compression", sCompression, SSHCFG_GLOBAL },
378 { "tcpkeepalive", sTCPKeepAlive, SSHCFG_GLOBAL },
379 { "keepalive", sTCPKeepAlive, SSHCFG_GLOBAL }, /* obsolete alias */
380 { "allowtcpforwarding", sAllowTcpForwarding, SSHCFG_ALL },
381 { "allowusers", sAllowUsers, SSHCFG_GLOBAL },
382 { "denyusers", sDenyUsers, SSHCFG_GLOBAL },
383 { "allowgroups", sAllowGroups, SSHCFG_GLOBAL },
384 { "denygroups", sDenyGroups, SSHCFG_GLOBAL },
385 { "ciphers", sCiphers, SSHCFG_GLOBAL },
386 { "macs", sMacs, SSHCFG_GLOBAL },
387 { "protocol", sProtocol, SSHCFG_GLOBAL },
388 { "gatewayports", sGatewayPorts, SSHCFG_ALL },
389 { "subsystem", sSubsystem, SSHCFG_GLOBAL },
390 { "maxstartups", sMaxStartups, SSHCFG_GLOBAL },
391 { "maxauthtries", sMaxAuthTries, SSHCFG_GLOBAL },
799ae497 392 { "banner", sBanner, SSHCFG_ALL },
9108f8d9 393 { "usedns", sUseDNS, SSHCFG_GLOBAL },
394 { "verifyreversemapping", sDeprecated, SSHCFG_GLOBAL },
395 { "reversemappingcheck", sDeprecated, SSHCFG_GLOBAL },
396 { "clientaliveinterval", sClientAliveInterval, SSHCFG_GLOBAL },
397 { "clientalivecountmax", sClientAliveCountMax, SSHCFG_GLOBAL },
398 { "authorizedkeysfile", sAuthorizedKeysFile, SSHCFG_GLOBAL },
399 { "authorizedkeysfile2", sAuthorizedKeysFile2, SSHCFG_GLOBAL },
400 { "useprivilegeseparation", sUsePrivilegeSeparation, SSHCFG_GLOBAL },
401 { "acceptenv", sAcceptEnv, SSHCFG_GLOBAL },
402 { "permittunnel", sPermitTunnel, SSHCFG_GLOBAL },
403 { "match", sMatch, SSHCFG_ALL },
404 { "permitopen", sPermitOpen, SSHCFG_ALL },
405 { "forcecommand", sForceCommand, SSHCFG_ALL },
406 { NULL, sBadOption, 0 }
3c0ef626 407};
408
409/*
410 * Returns the number of the token pointed to by cp or sBadOption.
411 */
412
413static ServerOpCodes
414parse_token(const char *cp, const char *filename,
9108f8d9 415 int linenum, u_int *flags)
3c0ef626 416{
417 u_int i;
418
419 for (i = 0; keywords[i].name; i++)
9108f8d9 420 if (strcasecmp(cp, keywords[i].name) == 0) {
421 *flags = keywords[i].flags;
3c0ef626 422 return keywords[i].opcode;
9108f8d9 423 }
3c0ef626 424
425 error("%s: line %d: Bad configuration option: %s",
426 filename, linenum, cp);
427 return sBadOption;
428}
429
430static void
431add_listen_addr(ServerOptions *options, char *addr, u_short port)
432{
665a873d 433 u_int i;
3c0ef626 434
435 if (options->num_ports == 0)
436 options->ports[options->num_ports++] = SSH_DEFAULT_PORT;
996d5e62 437 if (options->address_family == -1)
438 options->address_family = AF_UNSPEC;
3c0ef626 439 if (port == 0)
440 for (i = 0; i < options->num_ports; i++)
441 add_one_listen_addr(options, addr, options->ports[i]);
442 else
443 add_one_listen_addr(options, addr, port);
444}
445
446static void
447add_one_listen_addr(ServerOptions *options, char *addr, u_short port)
448{
449 struct addrinfo hints, *ai, *aitop;
450 char strport[NI_MAXSERV];
451 int gaierr;
452
453 memset(&hints, 0, sizeof(hints));
996d5e62 454 hints.ai_family = options->address_family;
3c0ef626 455 hints.ai_socktype = SOCK_STREAM;
456 hints.ai_flags = (addr == NULL) ? AI_PASSIVE : 0;
680cee3b 457 snprintf(strport, sizeof strport, "%u", port);
3c0ef626 458 if ((gaierr = getaddrinfo(addr, strport, &hints, &aitop)) != 0)
459 fatal("bad addr or host: %s (%s)",
460 addr ? addr : "<NULL>",
461 gai_strerror(gaierr));
462 for (ai = aitop; ai->ai_next; ai = ai->ai_next)
463 ;
464 ai->ai_next = options->listen_addrs;
465 options->listen_addrs = aitop;
466}
467
9108f8d9 468/*
469 * The strategy for the Match blocks is that the config file is parsed twice.
470 *
471 * The first time is at startup. activep is initialized to 1 and the
472 * directives in the global context are processed and acted on. Hitting a
473 * Match directive unsets activep and the directives inside the block are
474 * checked for syntax only.
475 *
476 * The second time is after a connection has been established but before
477 * authentication. activep is initialized to 2 and global config directives
478 * are ignored since they have already been processed. If the criteria in a
479 * Match block is met, activep is set and the subsequent directives
480 * processed and actioned until EOF or another Match block unsets it. Any
481 * options set are copied into the main server config.
482 *
483 * Potential additions/improvements:
484 * - Add Match support for pre-kex directives, eg Protocol, Ciphers.
485 *
486 * - Add a Tag directive (idea from David Leonard) ala pf, eg:
487 * Match Address 192.168.0.*
488 * Tag trusted
489 * Match Group wheel
490 * Tag trusted
491 * Match Tag trusted
492 * AllowTcpForwarding yes
493 * GatewayPorts clientspecified
494 * [...]
495 *
496 * - Add a PermittedChannelRequests directive
497 * Match Group shell
498 * PermittedChannelRequests session,forwarded-tcpip
499 */
500
501static int
502match_cfg_line_group(const char *grps, int line, const char *user)
503{
504 int result = 0;
505 u_int ngrps = 0;
506 char *arg, *p, *cp, *grplist[MAX_MATCH_GROUPS];
507 struct passwd *pw;
508
509 /*
510 * Even if we do not have a user yet, we still need to check for
511 * valid syntax.
512 */
513 arg = cp = xstrdup(grps);
514 while ((p = strsep(&cp, ",")) != NULL && *p != '\0') {
515 if (ngrps >= MAX_MATCH_GROUPS) {
516 error("line %d: too many groups in Match Group", line);
517 result = -1;
518 goto out;
519 }
520 grplist[ngrps++] = p;
521 }
522
523 if (user == NULL)
524 goto out;
525
526 if ((pw = getpwnam(user)) == NULL) {
527 debug("Can't match group at line %d because user %.100s does "
528 "not exist", line, user);
529 } else if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
530 debug("Can't Match group because user %.100s not in any group "
531 "at line %d", user, line);
532 } else if (ga_match(grplist, ngrps) != 1) {
533 debug("user %.100s does not match group %.100s at line %d",
534 user, arg, line);
535 } else {
536 debug("user %.100s matched group %.100s at line %d", user,
537 arg, line);
538 result = 1;
539 }
540out:
541 ga_free();
542 xfree(arg);
543 return result;
544}
545
546static int
547match_cfg_line(char **condition, int line, const char *user, const char *host,
548 const char *address)
549{
550 int result = 1;
551 char *arg, *attrib, *cp = *condition;
552 size_t len;
553
554 if (user == NULL)
555 debug3("checking syntax for 'Match %s'", cp);
556 else
557 debug3("checking match for '%s' user %s host %s addr %s", cp,
558 user ? user : "(null)", host ? host : "(null)",
559 address ? address : "(null)");
560
561 while ((attrib = strdelim(&cp)) && *attrib != '\0') {
562 if ((arg = strdelim(&cp)) == NULL || *arg == '\0') {
563 error("Missing Match criteria for %s", attrib);
564 return -1;
565 }
566 len = strlen(arg);
567 if (strcasecmp(attrib, "user") == 0) {
568 if (!user) {
569 result = 0;
570 continue;
571 }
572 if (match_pattern_list(user, arg, len, 0) != 1)
573 result = 0;
574 else
575 debug("user %.100s matched 'User %.100s' at "
576 "line %d", user, arg, line);
577 } else if (strcasecmp(attrib, "group") == 0) {
578 switch (match_cfg_line_group(arg, line, user)) {
579 case -1:
580 return -1;
581 case 0:
582 result = 0;
583 }
584 } else if (strcasecmp(attrib, "host") == 0) {
585 if (!host) {
586 result = 0;
587 continue;
588 }
589 if (match_hostname(host, arg, len) != 1)
590 result = 0;
591 else
592 debug("connection from %.100s matched 'Host "
593 "%.100s' at line %d", host, arg, line);
594 } else if (strcasecmp(attrib, "address") == 0) {
9108f8d9 595 if (!address) {
596 result = 0;
597 continue;
598 }
599 if (match_hostname(address, arg, len) != 1)
600 result = 0;
601 else
602 debug("connection from %.100s matched 'Address "
603 "%.100s' at line %d", address, arg, line);
604 } else {
605 error("Unsupported Match attribute %s", attrib);
606 return -1;
607 }
608 }
609 if (user != NULL)
610 debug3("match %sfound", result ? "" : "not ");
611 *condition = cp;
612 return result;
613}
614
615#define WHITESPACE " \t\r\n"
616
e9a17296 617int
618process_server_config_line(ServerOptions *options, char *line,
9108f8d9 619 const char *filename, int linenum, int *activep, const char *user,
620 const char *host, const char *address)
3c0ef626 621{
3c0ef626 622 char *cp, **charptr, *arg, *p;
9108f8d9 623 int cmdline = 0, *intptr, value, n;
3c0ef626 624 ServerOpCodes opcode;
996d5e62 625 u_short port;
9108f8d9 626 u_int i, flags = 0;
627 size_t len;
3c0ef626 628
e9a17296 629 cp = line;
9108f8d9 630 if ((arg = strdelim(&cp)) == NULL)
631 return 0;
e9a17296 632 /* Ignore leading whitespace */
633 if (*arg == '\0')
3c0ef626 634 arg = strdelim(&cp);
e9a17296 635 if (!arg || !*arg || *arg == '#')
636 return 0;
637 intptr = NULL;
638 charptr = NULL;
9108f8d9 639 opcode = parse_token(arg, filename, linenum, &flags);
640
641 if (activep == NULL) { /* We are processing a command line directive */
642 cmdline = 1;
643 activep = &cmdline;
644 }
645 if (*activep && opcode != sMatch)
646 debug3("%s:%d setting %s %s", filename, linenum, arg, cp);
647 if (*activep == 0 && !(flags & SSHCFG_MATCH)) {
648 if (user == NULL) {
649 fatal("%s line %d: Directive '%s' is not allowed "
650 "within a Match block", filename, linenum, arg);
651 } else { /* this is a directive we have already processed */
652 while (arg)
653 arg = strdelim(&cp);
654 return 0;
655 }
656 }
657
e9a17296 658 switch (opcode) {
659 /* Portable-specific options */
0fff78ff 660 case sUsePAM:
661 intptr = &options->use_pam;
e9a17296 662 goto parse_flag;
3c0ef626 663
e9a17296 664 /* Standard Options */
665 case sBadOption:
666 return -1;
667 case sPort:
668 /* ignore ports from configfile if cmdline specifies ports */
669 if (options->ports_from_cmdline)
670 return 0;
671 if (options->listen_addrs != NULL)
672 fatal("%s line %d: ports must be specified before "
673 "ListenAddress.", filename, linenum);
674 if (options->num_ports >= MAX_PORTS)
675 fatal("%s line %d: too many ports.",
676 filename, linenum);
677 arg = strdelim(&cp);
678 if (!arg || *arg == '\0')
679 fatal("%s line %d: missing port number.",
680 filename, linenum);
681 options->ports[options->num_ports++] = a2port(arg);
682 if (options->ports[options->num_ports-1] == 0)
683 fatal("%s line %d: Badly formatted port number.",
684 filename, linenum);
685 break;
686
687 case sServerKeyBits:
688 intptr = &options->server_key_bits;
3c0ef626 689parse_int:
e9a17296 690 arg = strdelim(&cp);
691 if (!arg || *arg == '\0')
692 fatal("%s line %d: missing integer value.",
693 filename, linenum);
694 value = atoi(arg);
9108f8d9 695 if (*activep && *intptr == -1)
e9a17296 696 *intptr = value;
697 break;
698
699 case sLoginGraceTime:
700 intptr = &options->login_grace_time;
3c0ef626 701parse_time:
e9a17296 702 arg = strdelim(&cp);
703 if (!arg || *arg == '\0')
704 fatal("%s line %d: missing time value.",
705 filename, linenum);
706 if ((value = convtime(arg)) == -1)
707 fatal("%s line %d: invalid time value.",
708 filename, linenum);
709 if (*intptr == -1)
710 *intptr = value;
711 break;
712
713 case sKeyRegenerationTime:
714 intptr = &options->key_regeneration_time;
715 goto parse_time;
716
717 case sListenAddress:
718 arg = strdelim(&cp);
996d5e62 719 if (arg == NULL || *arg == '\0')
720 fatal("%s line %d: missing address",
e9a17296 721 filename, linenum);
665a873d 722 /* check for bare IPv6 address: no "[]" and 2 or more ":" */
723 if (strchr(arg, '[') == NULL && (p = strchr(arg, ':')) != NULL
724 && strchr(p+1, ':') != NULL) {
725 add_listen_addr(options, arg, 0);
726 break;
727 }
996d5e62 728 p = hpdelim(&arg);
729 if (p == NULL)
730 fatal("%s line %d: bad address:port usage",
e9a17296 731 filename, linenum);
996d5e62 732 p = cleanhostname(p);
733 if (arg == NULL)
734 port = 0;
735 else if ((port = a2port(arg)) == 0)
736 fatal("%s line %d: bad port number", filename, linenum);
737
738 add_listen_addr(options, p, port);
739
740 break;
741
742 case sAddressFamily:
743 arg = strdelim(&cp);
665a873d 744 if (!arg || *arg == '\0')
745 fatal("%s line %d: missing address family.",
746 filename, linenum);
996d5e62 747 intptr = &options->address_family;
748 if (options->listen_addrs != NULL)
749 fatal("%s line %d: address family must be specified before "
750 "ListenAddress.", filename, linenum);
751 if (strcasecmp(arg, "inet") == 0)
752 value = AF_INET;
753 else if (strcasecmp(arg, "inet6") == 0)
754 value = AF_INET6;
755 else if (strcasecmp(arg, "any") == 0)
756 value = AF_UNSPEC;
757 else
758 fatal("%s line %d: unsupported address family \"%s\".",
759 filename, linenum, arg);
760 if (*intptr == -1)
761 *intptr = value;
e9a17296 762 break;
763
764 case sHostKeyFile:
765 intptr = &options->num_host_key_files;
766 if (*intptr >= MAX_HOSTKEYS)
767 fatal("%s line %d: too many host keys specified (max %d).",
768 filename, linenum, MAX_HOSTKEYS);
769 charptr = &options->host_key_files[*intptr];
3c0ef626 770parse_filename:
e9a17296 771 arg = strdelim(&cp);
772 if (!arg || *arg == '\0')
773 fatal("%s line %d: missing file name.",
774 filename, linenum);
9108f8d9 775 if (*activep && *charptr == NULL) {
e9a17296 776 *charptr = tilde_expand_filename(arg, getuid());
777 /* increase optional counter */
778 if (intptr != NULL)
779 *intptr = *intptr + 1;
780 }
781 break;
3c0ef626 782
e9a17296 783 case sPidFile:
784 charptr = &options->pid_file;
785 goto parse_filename;
3c0ef626 786
e9a17296 787 case sPermitRootLogin:
788 intptr = &options->permit_root_login;
789 arg = strdelim(&cp);
790 if (!arg || *arg == '\0')
791 fatal("%s line %d: missing yes/"
792 "without-password/forced-commands-only/no "
793 "argument.", filename, linenum);
794 value = 0; /* silence compiler */
795 if (strcmp(arg, "without-password") == 0)
796 value = PERMIT_NO_PASSWD;
797 else if (strcmp(arg, "forced-commands-only") == 0)
798 value = PERMIT_FORCED_ONLY;
799 else if (strcmp(arg, "yes") == 0)
800 value = PERMIT_YES;
801 else if (strcmp(arg, "no") == 0)
802 value = PERMIT_NO;
803 else
804 fatal("%s line %d: Bad yes/"
805 "without-password/forced-commands-only/no "
806 "argument: %s", filename, linenum, arg);
807 if (*intptr == -1)
808 *intptr = value;
809 break;
810
811 case sIgnoreRhosts:
812 intptr = &options->ignore_rhosts;
3c0ef626 813parse_flag:
e9a17296 814 arg = strdelim(&cp);
815 if (!arg || *arg == '\0')
816 fatal("%s line %d: missing yes/no argument.",
817 filename, linenum);
818 value = 0; /* silence compiler */
819 if (strcmp(arg, "yes") == 0)
820 value = 1;
821 else if (strcmp(arg, "no") == 0)
822 value = 0;
823 else
824 fatal("%s line %d: Bad yes/no argument: %s",
825 filename, linenum, arg);
9108f8d9 826 if (*activep && *intptr == -1)
e9a17296 827 *intptr = value;
828 break;
829
830 case sIgnoreUserKnownHosts:
831 intptr = &options->ignore_user_known_hosts;
832 goto parse_flag;
833
e9a17296 834 case sRhostsRSAAuthentication:
835 intptr = &options->rhosts_rsa_authentication;
836 goto parse_flag;
837
838 case sHostbasedAuthentication:
839 intptr = &options->hostbased_authentication;
840 goto parse_flag;
841
842 case sHostbasedUsesNameFromPacketOnly:
843 intptr = &options->hostbased_uses_name_from_packet_only;
844 goto parse_flag;
845
846 case sRSAAuthentication:
847 intptr = &options->rsa_authentication;
848 goto parse_flag;
849
850 case sPubkeyAuthentication:
851 intptr = &options->pubkey_authentication;
852 goto parse_flag;
0fff78ff 853
e9a17296 854 case sKerberosAuthentication:
855 intptr = &options->kerberos_authentication;
856 goto parse_flag;
3c0ef626 857
e9a17296 858 case sKerberosOrLocalPasswd:
859 intptr = &options->kerberos_or_local_passwd;
860 goto parse_flag;
3c0ef626 861
e9a17296 862 case sKerberosTicketCleanup:
863 intptr = &options->kerberos_ticket_cleanup;
864 goto parse_flag;
0fff78ff 865
cdd66111 866 case sKerberosGetAFSToken:
867 intptr = &options->kerberos_get_afs_token;
868 goto parse_flag;
869
0fff78ff 870 case sGssAuthentication:
871 intptr = &options->gss_authentication;
e9a17296 872 goto parse_flag;
0fff78ff 873
874 case sGssCleanupCreds:
875 intptr = &options->gss_cleanup_creds;
e9a17296 876 goto parse_flag;
3c0ef626 877
e9a17296 878 case sPasswordAuthentication:
879 intptr = &options->password_authentication;
880 goto parse_flag;
3c0ef626 881
e9a17296 882 case sKbdInteractiveAuthentication:
883 intptr = &options->kbd_interactive_authentication;
884 goto parse_flag;
3c0ef626 885
e9a17296 886 case sChallengeResponseAuthentication:
887 intptr = &options->challenge_response_authentication;
888 goto parse_flag;
3c0ef626 889
e9a17296 890 case sPrintMotd:
891 intptr = &options->print_motd;
892 goto parse_flag;
3c0ef626 893
e9a17296 894 case sPrintLastLog:
895 intptr = &options->print_lastlog;
896 goto parse_flag;
3c0ef626 897
e9a17296 898 case sX11Forwarding:
899 intptr = &options->x11_forwarding;
900 goto parse_flag;
3c0ef626 901
e9a17296 902 case sX11DisplayOffset:
903 intptr = &options->x11_display_offset;
904 goto parse_int;
3c0ef626 905
e9a17296 906 case sX11UseLocalhost:
907 intptr = &options->x11_use_localhost;
908 goto parse_flag;
3c0ef626 909
e9a17296 910 case sXAuthLocation:
911 charptr = &options->xauth_location;
912 goto parse_filename;
3c0ef626 913
e9a17296 914 case sStrictModes:
915 intptr = &options->strict_modes;
916 goto parse_flag;
3c0ef626 917
cdd66111 918 case sTCPKeepAlive:
919 intptr = &options->tcp_keep_alive;
e9a17296 920 goto parse_flag;
3c0ef626 921
e9a17296 922 case sEmptyPasswd:
923 intptr = &options->permit_empty_passwd;
924 goto parse_flag;
3c0ef626 925
41b2f314 926 case sPermitUserEnvironment:
927 intptr = &options->permit_user_env;
928 goto parse_flag;
929
e9a17296 930 case sUseLogin:
931 intptr = &options->use_login;
932 goto parse_flag;
3c0ef626 933
f5799ae1 934 case sCompression:
935 intptr = &options->compression;
665a873d 936 arg = strdelim(&cp);
937 if (!arg || *arg == '\0')
938 fatal("%s line %d: missing yes/no/delayed "
939 "argument.", filename, linenum);
940 value = 0; /* silence compiler */
941 if (strcmp(arg, "delayed") == 0)
942 value = COMP_DELAYED;
943 else if (strcmp(arg, "yes") == 0)
944 value = COMP_ZLIB;
945 else if (strcmp(arg, "no") == 0)
946 value = COMP_NONE;
947 else
948 fatal("%s line %d: Bad yes/no/delayed "
949 "argument: %s", filename, linenum, arg);
950 if (*intptr == -1)
951 *intptr = value;
952 break;
f5799ae1 953
e9a17296 954 case sGatewayPorts:
955 intptr = &options->gateway_ports;
996d5e62 956 arg = strdelim(&cp);
957 if (!arg || *arg == '\0')
958 fatal("%s line %d: missing yes/no/clientspecified "
959 "argument.", filename, linenum);
960 value = 0; /* silence compiler */
961 if (strcmp(arg, "clientspecified") == 0)
962 value = 2;
963 else if (strcmp(arg, "yes") == 0)
964 value = 1;
965 else if (strcmp(arg, "no") == 0)
966 value = 0;
967 else
968 fatal("%s line %d: Bad yes/no/clientspecified "
969 "argument: %s", filename, linenum, arg);
799ae497 970 if (*activep && *intptr == -1)
996d5e62 971 *intptr = value;
972 break;
3c0ef626 973
0fff78ff 974 case sUseDNS:
975 intptr = &options->use_dns;
e9a17296 976 goto parse_flag;
3c0ef626 977
e9a17296 978 case sLogFacility:
979 intptr = (int *) &options->log_facility;
980 arg = strdelim(&cp);
981 value = log_facility_number(arg);
982 if (value == SYSLOG_FACILITY_NOT_SET)
983 fatal("%.200s line %d: unsupported log facility '%s'",
984 filename, linenum, arg ? arg : "<NONE>");
985 if (*intptr == -1)
986 *intptr = (SyslogFacility) value;
987 break;
988
989 case sLogLevel:
990 intptr = (int *) &options->log_level;
991 arg = strdelim(&cp);
992 value = log_level_number(arg);
993 if (value == SYSLOG_LEVEL_NOT_SET)
994 fatal("%.200s line %d: unsupported log level '%s'",
995 filename, linenum, arg ? arg : "<NONE>");
996 if (*intptr == -1)
997 *intptr = (LogLevel) value;
998 break;
999
1000 case sAllowTcpForwarding:
1001 intptr = &options->allow_tcp_forwarding;
1002 goto parse_flag;
1003
700318f3 1004 case sUsePrivilegeSeparation:
1005 intptr = &use_privsep;
1006 goto parse_flag;
1007
e9a17296 1008 case sAllowUsers:
1009 while ((arg = strdelim(&cp)) && *arg != '\0') {
1010 if (options->num_allow_users >= MAX_ALLOW_USERS)
1011 fatal("%s line %d: too many allow users.",
1012 filename, linenum);
680cee3b 1013 options->allow_users[options->num_allow_users++] =
1014 xstrdup(arg);
e9a17296 1015 }
1016 break;
3c0ef626 1017
e9a17296 1018 case sDenyUsers:
1019 while ((arg = strdelim(&cp)) && *arg != '\0') {
1020 if (options->num_deny_users >= MAX_DENY_USERS)
9108f8d9 1021 fatal("%s line %d: too many deny users.",
e9a17296 1022 filename, linenum);
680cee3b 1023 options->deny_users[options->num_deny_users++] =
1024 xstrdup(arg);
e9a17296 1025 }
1026 break;
3c0ef626 1027
e9a17296 1028 case sAllowGroups:
1029 while ((arg = strdelim(&cp)) && *arg != '\0') {
1030 if (options->num_allow_groups >= MAX_ALLOW_GROUPS)
1031 fatal("%s line %d: too many allow groups.",
1032 filename, linenum);
680cee3b 1033 options->allow_groups[options->num_allow_groups++] =
1034 xstrdup(arg);
e9a17296 1035 }
1036 break;
3c0ef626 1037
e9a17296 1038 case sDenyGroups:
1039 while ((arg = strdelim(&cp)) && *arg != '\0') {
1040 if (options->num_deny_groups >= MAX_DENY_GROUPS)
1041 fatal("%s line %d: too many deny groups.",
1042 filename, linenum);
1043 options->deny_groups[options->num_deny_groups++] = xstrdup(arg);
1044 }
1045 break;
1046
1047 case sCiphers:
1048 arg = strdelim(&cp);
1049 if (!arg || *arg == '\0')
1050 fatal("%s line %d: Missing argument.", filename, linenum);
1051 if (!ciphers_valid(arg))
1052 fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
1053 filename, linenum, arg ? arg : "<NONE>");
1054 if (options->ciphers == NULL)
1055 options->ciphers = xstrdup(arg);
1056 break;
1057
1058 case sMacs:
1059 arg = strdelim(&cp);
1060 if (!arg || *arg == '\0')
1061 fatal("%s line %d: Missing argument.", filename, linenum);
1062 if (!mac_valid(arg))
1063 fatal("%s line %d: Bad SSH2 mac spec '%s'.",
1064 filename, linenum, arg ? arg : "<NONE>");
1065 if (options->macs == NULL)
1066 options->macs = xstrdup(arg);
1067 break;
1068
1069 case sProtocol:
1070 intptr = &options->protocol;
1071 arg = strdelim(&cp);
1072 if (!arg || *arg == '\0')
1073 fatal("%s line %d: Missing argument.", filename, linenum);
1074 value = proto_spec(arg);
1075 if (value == SSH_PROTO_UNKNOWN)
1076 fatal("%s line %d: Bad protocol spec '%s'.",
1077 filename, linenum, arg ? arg : "<NONE>");
1078 if (*intptr == SSH_PROTO_UNKNOWN)
1079 *intptr = value;
1080 break;
1081
1082 case sSubsystem:
1083 if (options->num_subsystems >= MAX_SUBSYSTEMS) {
1084 fatal("%s line %d: too many subsystems defined.",
1085 filename, linenum);
1086 }
1087 arg = strdelim(&cp);
1088 if (!arg || *arg == '\0')
1089 fatal("%s line %d: Missing subsystem name.",
1090 filename, linenum);
9108f8d9 1091 if (!*activep) {
1092 arg = strdelim(&cp);
1093 break;
1094 }
e9a17296 1095 for (i = 0; i < options->num_subsystems; i++)
1096 if (strcmp(arg, options->subsystem_name[i]) == 0)
1097 fatal("%s line %d: Subsystem '%s' already defined.",
1098 filename, linenum, arg);
1099 options->subsystem_name[options->num_subsystems] = xstrdup(arg);
1100 arg = strdelim(&cp);
1101 if (!arg || *arg == '\0')
1102 fatal("%s line %d: Missing subsystem command.",
1103 filename, linenum);
1104 options->subsystem_command[options->num_subsystems] = xstrdup(arg);
9108f8d9 1105
1106 /* Collect arguments (separate to executable) */
1107 p = xstrdup(arg);
1108 len = strlen(p) + 1;
1109 while ((arg = strdelim(&cp)) != NULL && *arg != '\0') {
1110 len += 1 + strlen(arg);
1111 p = xrealloc(p, 1, len);
1112 strlcat(p, " ", len);
1113 strlcat(p, arg, len);
1114 }
1115 options->subsystem_args[options->num_subsystems] = p;
e9a17296 1116 options->num_subsystems++;
1117 break;
1118
1119 case sMaxStartups:
1120 arg = strdelim(&cp);
1121 if (!arg || *arg == '\0')
1122 fatal("%s line %d: Missing MaxStartups spec.",
1123 filename, linenum);
1124 if ((n = sscanf(arg, "%d:%d:%d",
1125 &options->max_startups_begin,
1126 &options->max_startups_rate,
1127 &options->max_startups)) == 3) {
1128 if (options->max_startups_begin >
1129 options->max_startups ||
1130 options->max_startups_rate > 100 ||
1131 options->max_startups_rate < 1)
3c0ef626 1132 fatal("%s line %d: Illegal MaxStartups spec.",
1133 filename, linenum);
e9a17296 1134 } else if (n != 1)
1135 fatal("%s line %d: Illegal MaxStartups spec.",
1136 filename, linenum);
1137 else
1138 options->max_startups = options->max_startups_begin;
1139 break;
1140
c9f39d2c 1141 case sMaxAuthTries:
1142 intptr = &options->max_authtries;
1143 goto parse_int;
1144
e9a17296 1145 case sBanner:
1146 charptr = &options->banner;
1147 goto parse_filename;
1148 /*
1149 * These options can contain %X options expanded at
1150 * connect time, so that you can specify paths like:
1151 *
1152 * AuthorizedKeysFile /etc/ssh_keys/%u
1153 */
1154 case sAuthorizedKeysFile:
1155 case sAuthorizedKeysFile2:
9108f8d9 1156 charptr = (opcode == sAuthorizedKeysFile) ?
e9a17296 1157 &options->authorized_keys_file :
1158 &options->authorized_keys_file2;
1159 goto parse_filename;
1160
1161 case sClientAliveInterval:
1162 intptr = &options->client_alive_interval;
1163 goto parse_time;
1164
1165 case sClientAliveCountMax:
1166 intptr = &options->client_alive_count_max;
1167 goto parse_int;
1168
c9f39d2c 1169 case sAcceptEnv:
1170 while ((arg = strdelim(&cp)) && *arg != '\0') {
1171 if (strchr(arg, '=') != NULL)
1172 fatal("%s line %d: Invalid environment name.",
1173 filename, linenum);
1174 if (options->num_accept_env >= MAX_ACCEPT_ENV)
1175 fatal("%s line %d: too many allow env.",
1176 filename, linenum);
9108f8d9 1177 if (!*activep)
1178 break;
c9f39d2c 1179 options->accept_env[options->num_accept_env++] =
1180 xstrdup(arg);
1181 }
1182 break;
1183
2c06c99b 1184 case sPermitTunnel:
1185 intptr = &options->permit_tun;
1186 arg = strdelim(&cp);
1187 if (!arg || *arg == '\0')
1188 fatal("%s line %d: Missing yes/point-to-point/"
1189 "ethernet/no argument.", filename, linenum);
1190 value = 0; /* silence compiler */
1191 if (strcasecmp(arg, "ethernet") == 0)
1192 value = SSH_TUNMODE_ETHERNET;
1193 else if (strcasecmp(arg, "point-to-point") == 0)
1194 value = SSH_TUNMODE_POINTOPOINT;
1195 else if (strcasecmp(arg, "yes") == 0)
1196 value = SSH_TUNMODE_YES;
1197 else if (strcasecmp(arg, "no") == 0)
1198 value = SSH_TUNMODE_NO;
1199 else
1200 fatal("%s line %d: Bad yes/point-to-point/ethernet/"
1201 "no argument: %s", filename, linenum, arg);
1202 if (*intptr == -1)
1203 *intptr = value;
1204 break;
1205
9108f8d9 1206 case sMatch:
1207 if (cmdline)
1208 fatal("Match directive not supported as a command-line "
1209 "option");
1210 value = match_cfg_line(&cp, linenum, user, host, address);
1211 if (value < 0)
1212 fatal("%s line %d: Bad Match condition", filename,
1213 linenum);
1214 *activep = value;
1215 break;
1216
1217 case sPermitOpen:
1218 arg = strdelim(&cp);
1219 if (!arg || *arg == '\0')
1220 fatal("%s line %d: missing PermitOpen specification",
1221 filename, linenum);
799ae497 1222 n = options->num_permitted_opens; /* modified later */
9108f8d9 1223 if (strcmp(arg, "any") == 0) {
799ae497 1224 if (*activep && n == -1) {
9108f8d9 1225 channel_clear_adm_permitted_opens();
1226 options->num_permitted_opens = 0;
1227 }
1228 break;
1229 }
799ae497 1230 if (*activep && n == -1)
1231 channel_clear_adm_permitted_opens();
9108f8d9 1232 for (; arg != NULL && *arg != '\0'; arg = strdelim(&cp)) {
1233 p = hpdelim(&arg);
1234 if (p == NULL)
1235 fatal("%s line %d: missing host in PermitOpen",
1236 filename, linenum);
1237 p = cleanhostname(p);
1238 if (arg == NULL || (port = a2port(arg)) == 0)
1239 fatal("%s line %d: bad port number in "
1240 "PermitOpen", filename, linenum);
799ae497 1241 if (*activep && n == -1)
9108f8d9 1242 options->num_permitted_opens =
1243 channel_add_adm_permitted_opens(p, port);
9108f8d9 1244 }
1245 break;
1246
1247 case sForceCommand:
1248 if (cp == NULL)
1249 fatal("%.200s line %d: Missing argument.", filename,
1250 linenum);
1251 len = strspn(cp, WHITESPACE);
1252 if (*activep && options->adm_forced_command == NULL)
1253 options->adm_forced_command = xstrdup(cp + len);
1254 return 0;
1255
e9a17296 1256 case sDeprecated:
0fff78ff 1257 logit("%s line %d: Deprecated option %s",
1258 filename, linenum, arg);
1259 while (arg)
1260 arg = strdelim(&cp);
1261 break;
1262
1263 case sUnsupported:
1264 logit("%s line %d: Unsupported option %s",
e9a17296 1265 filename, linenum, arg);
1266 while (arg)
1267 arg = strdelim(&cp);
1268 break;
1269
1270 default:
1271 fatal("%s line %d: Missing handler for opcode %s (%d)",
1272 filename, linenum, arg, opcode);
1273 }
1274 if ((arg = strdelim(&cp)) != NULL && *arg != '\0')
1275 fatal("%s line %d: garbage at end of line; \"%.200s\".",
1276 filename, linenum, arg);
1277 return 0;
1278}
3c0ef626 1279
e9a17296 1280/* Reads the server configuration file. */
3c0ef626 1281
e9a17296 1282void
c9f39d2c 1283load_server_config(const char *filename, Buffer *conf)
e9a17296 1284{
c9f39d2c 1285 char line[1024], *cp;
680cee3b 1286 FILE *f;
e9a17296 1287
c9f39d2c 1288 debug2("%s: filename %s", __func__, filename);
1289 if ((f = fopen(filename, "r")) == NULL) {
e9a17296 1290 perror(filename);
1291 exit(1);
1292 }
c9f39d2c 1293 buffer_clear(conf);
e9a17296 1294 while (fgets(line, sizeof(line), f)) {
c9f39d2c 1295 /*
1296 * Trim out comments and strip whitespace
1297 * NB - preserve newlines, they are needed to reproduce
1298 * line numbers later for error messages
1299 */
1300 if ((cp = strchr(line, '#')) != NULL)
1301 memcpy(cp, "\n", 2);
1302 cp = line + strspn(line, " \t\r");
1303
1304 buffer_append(conf, cp, strlen(cp));
3c0ef626 1305 }
c9f39d2c 1306 buffer_append(conf, "\0", 1);
3c0ef626 1307 fclose(f);
c9f39d2c 1308 debug2("%s: done config len = %d", __func__, buffer_len(conf));
1309}
1310
1311void
9108f8d9 1312parse_server_match_config(ServerOptions *options, const char *user,
1313 const char *host, const char *address)
1314{
1315 ServerOptions mo;
1316
1317 initialize_server_options(&mo);
1318 parse_server_config(&mo, "reprocess config", &cfg, user, host, address);
799ae497 1319 copy_set_server_options(options, &mo, 0);
9108f8d9 1320}
1321
799ae497 1322/* Helper macros */
1323#define M_CP_INTOPT(n) do {\
1324 if (src->n != -1) \
1325 dst->n = src->n; \
1326} while (0)
1327#define M_CP_STROPT(n) do {\
1328 if (src->n != NULL) { \
1329 if (dst->n != NULL) \
1330 xfree(dst->n); \
1331 dst->n = src->n; \
1332 } \
1333} while(0)
1334
1335/*
1336 * Copy any supported values that are set.
1337 *
1338 * If the preauth flag is set, we do not bother copying the the string or
1339 * array values that are not used pre-authentication, because any that we
1340 * do use must be explictly sent in mm_getpwnamallow().
1341 */
9108f8d9 1342void
799ae497 1343copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
9108f8d9 1344{
799ae497 1345 M_CP_INTOPT(password_authentication);
1346 M_CP_INTOPT(gss_authentication);
1347 M_CP_INTOPT(rsa_authentication);
1348 M_CP_INTOPT(pubkey_authentication);
1349 M_CP_INTOPT(kerberos_authentication);
1350 M_CP_INTOPT(hostbased_authentication);
1351 M_CP_INTOPT(kbd_interactive_authentication);
1352
1353 M_CP_INTOPT(allow_tcp_forwarding);
1354 M_CP_INTOPT(gateway_ports);
1355 M_CP_INTOPT(x11_display_offset);
1356 M_CP_INTOPT(x11_forwarding);
1357 M_CP_INTOPT(x11_use_localhost);
1358
1359 M_CP_STROPT(banner);
1360 if (preauth)
1361 return;
1362 M_CP_STROPT(adm_forced_command);
9108f8d9 1363}
1364
799ae497 1365#undef M_CP_INTOPT
1366#undef M_CP_STROPT
1367
9108f8d9 1368void
1369parse_server_config(ServerOptions *options, const char *filename, Buffer *conf,
1370 const char *user, const char *host, const char *address)
c9f39d2c 1371{
9108f8d9 1372 int active, linenum, bad_options = 0;
c9f39d2c 1373 char *cp, *obuf, *cbuf;
1374
1375 debug2("%s: config %s len %d", __func__, filename, buffer_len(conf));
1376
1377 obuf = cbuf = xstrdup(buffer_ptr(conf));
9108f8d9 1378 active = user ? 0 : 1;
c9f39d2c 1379 linenum = 1;
dec6d9fe 1380 while ((cp = strsep(&cbuf, "\n")) != NULL) {
c9f39d2c 1381 if (process_server_config_line(options, cp, filename,
9108f8d9 1382 linenum++, &active, user, host, address) != 0)
c9f39d2c 1383 bad_options++;
1384 }
1385 xfree(obuf);
3c0ef626 1386 if (bad_options > 0)
1387 fatal("%s: terminating, %d bad configuration options",
1388 filename, bad_options);
1389}
This page took 0.296064 seconds and 5 git commands to generate.