servconf.c revision 4a2e944d74dafc80c85d74c8b11430bbecd98824
/*
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
* All rights reserved
*
* As far as I am concerned, the code I have written for this software
* can be used freely for any purpose. Any derived versions of this
* software must be clearly marked as such, and if the derived work is
* incompatible with the protocol description in the RFC file, it must be
* called by a name other than "ssh" or "Secure Shell".
*/
/*
*/
#include "includes.h"
RCSID("$OpenBSD: servconf.c,v 1.115 2002/09/04 18:52:42 stevesk Exp $");
#ifdef HAVE_DEFOPEN
#include <deflt.h>
#endif /* HAVE_DEFOPEN */
#if defined(KRB4)
#include <krb.h>
#endif
#if defined(KRB5)
#ifdef HEIMDAL
#include <krb.h>
#else
/* Bodge - but then, so is using the kerberos IV KEYFILE to get a Kerberos V
* keytab */
#define KEYFILE "/etc/krb5.keytab"
#endif
#endif
#ifdef AFS
#include <kafs.h>
#endif
#include "ssh.h"
#include "log.h"
#include "buffer.h"
#include "servconf.h"
#include "xmalloc.h"
#include "compat.h"
#include "pathnames.h"
#include "tildexpand.h"
#include "misc.h"
#include "cipher.h"
#include "kex.h"
#include "mac.h"
#include "auth.h"
#include "match.h"
#include "groupaccess.h"
/* AF_UNSPEC or AF_INET or AF_INET6 */
extern int IPv4or6;
/*
* Initializes the server options to their initial (unset) values. Some of those
* that stay unset after the command line options and configuration files are
* read are set to their default values in fill_default_server_options().
*/
void
{
/* Standard Options */
options->ports_from_cmdline = 0;
options->num_host_key_files = 0;
#ifdef GSSAPI
#endif
#endif
#endif
#ifdef AFS
#endif
options->num_allow_users = 0;
options->num_deny_users = 0;
options->num_allow_groups = 0;
options->num_deny_groups = 0;
options->num_subsystems = 0;
}
#ifdef HAVE_DEFOPEN
/*
*
* PermitRootLogin
* PermitEmptyPasswords
* LoginGraceTime
*
* CONSOLE=* -> PermitRootLogin=without-password
* #CONSOLE=* -> PermitRootLogin=yes
*
* PASSREQ=YES -> PermitEmptyPasswords=no
* PASSREQ=NO -> PermitEmptyPasswords=yes
* #PASSREQ=* -> PermitEmptyPasswords=no
*
* TIMEOUT=<secs> -> LoginGraceTime=<secs>
* #TIMEOUT=<secs> -> LoginGraceTime=300
*/
static
void
{
int flags;
char *ptr;
if (defopen(_PATH_DEFAULT_LOGIN))
return;
/* Ignore case */
options->permit_empty_passwd = 0;
}
}
}
else
}
}
#endif /* HAVE_DEFOPEN */
void
{
#ifdef HAVE_DEFOPEN
#endif /* HAVE_DEFOPEN */
/* Standard Options */
if (options->num_host_key_files == 0) {
/* fill default hostkeys for protocols */
#ifndef GSSAPI
/* With GSS keyex we can run v2 w/ no host keys */
}
#endif /* GSSAPI */
}
options->rhosts_authentication = 0;
#ifdef GSSAPI
#endif
#endif
options->kerberos_tgt_passing = 0;
#endif
#ifdef AFS
options->afs_token_passing = 0;
#endif
/*
* options->pam_authentication_via_kbd_int has intentionally no default
* value since we do not need it.
*/
options->permit_empty_passwd = 0;
options->permit_user_env = 0;
options->gateway_ports = 0;
options->client_alive_interval = 0;
/* authorized_keys_file2 falls back to authorized_keys_file */
else
}
}
/* Keyword tokens. */
typedef enum {
sBadOption, /* == unknown option */
/* Portable-specific options */
/* Standard Options */
#ifdef GSSAPI
#endif /* GSSAPI */
#endif
#endif
#ifdef AFS
#endif
/* Textual representation of the tokens. */
static struct {
const char *name;
} keywords[] = {
/* Portable-specific options */
/* Standard Options */
{ "hostbasedusesnamefrompacketonly", sHostbasedUsesNameFromPacketOnly },
#ifdef GSSAPI
#ifndef SUNW_GSSAPI
#endif /* SUNW_GSSAPI */
#endif
#endif
#endif
#ifdef AFS
#endif
{ NULL, sBadOption, 0 }
};
/*
* Returns the number of the token pointed to by cp or sBadOption.
*/
static ServerOpCodes
{
u_int i;
}
error("%s: line %d: Bad configuration option: %s",
return sBadOption;
}
static void
{
int i;
if (port == 0)
else
}
static void
{
char strport[NI_MAXSERV];
int gaierr;
fatal("bad addr or host: %s (%s)",
;
}
/*
* The strategy for the Match blocks is that the config file is parsed twice.
*
* The first time is at startup. activep is initialized to 1 and the
* directives in the global context are processed and acted on. Hitting a
* Match directive unsets activep and the directives inside the block are
* checked for syntax only.
*
* The second time is after a connection has been established but before
* authentication. activep is initialized to 2 and global config directives
* are ignored since they have already been processed. If the criteria in a
* Match block is met, activep is set and the subsequent directives
* processed and actioned until EOF or another Match block unsets it. Any
* options set are copied into the main server config.
*
* Potential additions/improvements:
* - Add Match support for pre-kex directives, eg Protocol, Ciphers.
*
* - Add a Tag directive (idea from David Leonard) ala pf, eg:
* Match Address 192.168.0.*
* Tag trusted
* Match Group wheel
* Tag trusted
* Match Tag trusted
* AllowTcpForwarding yes
* GatewayPorts clientspecified
* [...]
*
* - Add a PermittedChannelRequests directive
* Match Group shell
* PermittedChannelRequests session,forwarded-tcpip
*/
static int
{
int result = 0;
goto out;
debug("Can't match group at line %d because user %.100s does "
debug("Can't Match group because user %.100s not in any group "
debug("user %.100s does not match group list %.100s at line %d",
} else {
result = 1;
}
out:
ga_free();
return result;
}
static int
const char *address)
{
int result = 1;
else
return -1;
}
if (!user) {
result = 0;
continue;
}
result = 0;
else
debug("user %.100s matched 'User %.100s' at "
case -1:
return -1;
case 0:
result = 0;
}
if (!host) {
result = 0;
continue;
}
result = 0;
else
debug("connection from %.100s matched 'Host "
case 1:
debug("connection from %.100s matched 'Address "
break;
case 0:
case -1:
result = 0;
break;
case -2:
return -1;
}
} else {
return -1;
}
}
return result;
}
#define WHITESPACE " \t\r\n"
int
{
/* Ignore leading whitespace */
if (*arg == '\0')
return 0;
cmdline = 1;
}
fatal("%s line %d: Directive '%s' is not allowed "
} else { /* this is a directive we have already processed */
while (arg)
return 0;
}
}
switch (opcode) {
/* Portable-specific options */
log("%s line %d: PAMAuthenticationViaKbdInt has been "
"deprecated. You should use KbdInteractiveAuthentication "
goto parse_flag;
/* Standard Options */
case sBadOption:
return -1;
case sPort:
/* ignore ports from configfile if cmdline specifies ports */
if (options->ports_from_cmdline)
return 0;
fatal("%s line %d: ports must be specified before "
fatal("%s line %d: too many ports.",
fatal("%s line %d: missing port number.",
fatal("%s line %d: Badly formatted port number.",
break;
case sServerKeyBits:
fatal("%s line %d: missing integer value.",
break;
case sLoginGraceTime:
fatal("%s line %d: missing time value.",
fatal("%s line %d: invalid time value.",
if (*intptr == -1)
break;
case sKeyRegenerationTime:
goto parse_time;
case sListenAddress:
fatal("%s line %d: missing inet addr.",
if (*arg == '[') {
fatal("%s line %d: bad ipv6 inet addr usage.",
arg++;
break;
}
if (*p == ':') {
p++;
if (*p == '\0')
fatal("%s line %d: bad inet addr:port usage.",
else {
*(p-1) = '\0';
fatal("%s line %d: bad port number.",
}
} else if (*p == '\0')
else
fatal("%s line %d: bad inet addr usage.",
break;
case sHostKeyFile:
if (*intptr >= MAX_HOSTKEYS)
fatal("%s line %d: too many host keys specified (max %d).",
fatal("%s line %d: missing file name.",
/* increase optional counter */
}
break;
case sPidFile:
goto parse_filename;
case sPermitRootLogin:
fatal("%s line %d: missing yes/"
value = 0; /* silence compiler */
value = PERMIT_YES;
else
fatal("%s line %d: Bad yes/"
break;
case sIgnoreRhosts:
value = 0; /* silence compiler */
value = 1;
value = 0;
else
break;
case sIgnoreUserKnownHosts:
goto parse_flag;
case sRhostsAuthentication:
goto parse_flag;
case sRhostsRSAAuthentication:
goto parse_flag;
case sHostbasedAuthentication:
goto parse_flag;
goto parse_flag;
case sRSAAuthentication:
goto parse_flag;
case sPubkeyAuthentication:
goto parse_flag;
#ifdef GSSAPI
case sGssAuthentication:
goto parse_flag;
case sGssKeyEx:
goto parse_flag;
case sGssStoreDelegCreds:
goto parse_flag;
#ifndef SUNW_GSSAPI
case sGssUseSessionCredCache:
goto parse_flag;
case sGssCleanupCreds:
goto parse_flag;
#endif /* SUNW_GSSAPI */
#endif /* GSSAPI */
case sKerberosAuthentication:
goto parse_flag;
case sKerberosOrLocalPasswd:
goto parse_flag;
case sKerberosTicketCleanup:
goto parse_flag;
#endif
case sKerberosTgtPassing:
goto parse_flag;
#endif
#ifdef AFS
case sAFSTokenPassing:
goto parse_flag;
#endif
case sPasswordAuthentication:
goto parse_flag;
goto parse_flag;
goto parse_flag;
case sPrintMotd:
goto parse_flag;
case sPrintLastLog:
goto parse_flag;
case sX11Forwarding:
goto parse_flag;
case sX11DisplayOffset:
goto parse_int;
case sX11UseLocalhost:
goto parse_flag;
case sXAuthLocation:
goto parse_filename;
case sStrictModes:
goto parse_flag;
case sKeepAlives:
goto parse_flag;
case sEmptyPasswd:
goto parse_flag;
case sPermitUserEnvironment:
goto parse_flag;
case sUseLogin:
log("%s line %d: ignoring UseLogin option value."
while (arg)
break;
case sCompression:
goto parse_flag;
case sGatewayPorts:
fatal("%s line %d: missing yes/no/clientspecified "
value = 0; /* silence compiler */
value = 2;
value = 1;
value = 0;
else
fatal("%s line %d: Bad yes/no/clientspecified "
break;
case sVerifyReverseMapping:
goto parse_flag;
case sLogFacility:
if (value == SYSLOG_FACILITY_NOT_SET)
fatal("%.200s line %d: unsupported log facility '%s'",
if (*intptr == -1)
break;
case sLogLevel:
if (value == SYSLOG_LEVEL_NOT_SET)
fatal("%.200s line %d: unsupported log level '%s'",
if (*intptr == -1)
break;
case sAllowTcpForwarding:
goto parse_flag;
case sUsePrivilegeSeparation:
log("%s line %d: ignoring UsePrivilegeSeparation option value."
while (arg)
break;
case sAllowUsers:
fatal("%s line %d: too many allow users.",
}
break;
case sDenyUsers:
fatal( "%s line %d: too many deny users.",
}
break;
case sAllowGroups:
fatal("%s line %d: too many allow groups.",
}
break;
case sDenyGroups:
fatal("%s line %d: too many deny groups.",
}
break;
case sCiphers:
if (!ciphers_valid(arg))
fatal("%s line %d: Bad SSH2 cipher spec '%s'.",
break;
case sMacs:
fatal("%s line %d: Bad SSH2 mac spec '%s'.",
break;
case sProtocol:
if (value == SSH_PROTO_UNKNOWN)
fatal("%s line %d: Bad protocol spec '%s'.",
if (*intptr == SSH_PROTO_UNKNOWN)
break;
case sSubsystem:
fatal("%s line %d: too many subsystems defined.",
}
fatal("%s line %d: Missing subsystem name.",
if (!*activep) {
break;
}
for (i = 0; i < options->num_subsystems; i++)
fatal("%s line %d: Subsystem '%s' already defined.",
fatal("%s line %d: Missing subsystem command.",
/*
* Collect arguments (separate to executable), including the
* name of the executable, in a way that is easier to parse
* later.
*/
}
break;
case sMaxStartups:
fatal("%s line %d: Missing MaxStartups spec.",
if (options->max_startups_begin >
options->max_startups ||
fatal("%s line %d: Illegal MaxStartups spec.",
} else if (n != 1)
fatal("%s line %d: Illegal MaxStartups spec.",
else
break;
case sBanner:
goto parse_filename;
/*
* These options can contain %X options expanded at
* connect time, so that you can specify paths like:
*
*/
case sAuthorizedKeysFile:
case sAuthorizedKeysFile2:
goto parse_filename;
case sClientAliveInterval:
goto parse_time;
case sClientAliveCountMax:
goto parse_int;
case sMaxAuthTries:
goto parse_int;
case sMaxAuthTriesLog:
goto parse_int;
case sLookupClientHostnames:
goto parse_flag;
case sUseOpenSSLEngine:
goto parse_flag;
case sChrootDirectory:
fatal("%s line %d: missing directory name for "
break;
case sPreUserauthHook:
goto parse_filename;
case sMatch:
if (cmdline)
fatal("Match directive not supported as a command-line "
"option");
if (value < 0)
linenum);
break;
case sDeprecated:
log("%s line %d: Deprecated option %s",
while (arg)
break;
case sPAMServicePrefix:
fatal("%s line %d: Missing argument.",
fatal("%s line %d: PAMServiceName and PAMServicePrefix "
break;
case sPAMServiceName:
fatal("%s line %d: Missing argument.",
fatal("%s line %d: PAMServiceName and PAMServicePrefix "
break;
default:
fatal("%s line %d: Missing handler for opcode %s (%d)",
}
fatal("%s line %d: garbage at end of line; \"%.200s\".",
return 0;
}
/* Reads the server configuration file. */
void
{
FILE *f;
exit(1);
}
/*
* Trim out comments and strip whitespace
* NB - preserve newlines, they are needed to reproduce
* line numbers later for error messages
*/
}
fclose(f);
}
void
{
}
/* Helper macros */
#define M_CP_INTOPT(n) do {\
if (src->n != -1) \
} while (0)
#define M_CP_STROPT(n) do {\
} \
} while(0)
/*
* Copy any supported values that are set.
*
* If the preauth flag is set, we do not bother copying the the string or
* array values that are not used pre-authentication, because any that we
* do use must be explictly sent in mm_getpwnamallow().
*/
void
{
if (preauth)
return;
}
void
{
linenum = 1;
bad_options++;
}
if (bad_options > 0)
fatal("%s: terminating, %d bad configuration options",
}
/*
* Note that "none" is a special path having the same affect on sshd
* configuration as not specifying ChrootDirectory at all.
*/
int
chroot_requested(char *chroot_directory)
{
return (chroot_directory != NULL &&
}