Add an internal getenv wrapper function: zeekenv

It maps newer environment variable names starting with ZEEK to the
legacy names starting with BRO.
This commit is contained in:
Jon Siwek 2019-05-23 20:40:03 -07:00
parent 580822a32c
commit 7f0fb49612
14 changed files with 82 additions and 81 deletions

13
CHANGES
View file

@ -1,4 +1,17 @@
2.6-334 | 2019-05-23 20:40:03 -0700
* Add an internal getenv wrapper function: zeekenv (Jon Siwek, Corelight)
It maps newer environment variable names starting with ZEEK to the
legacy names starting with BRO.
* Rename all BRO-prefixed environment variables (Daniel Thayer)
For backward compatibility when reading values, we first check
the ZEEK-prefixed value, and if not set, then check the corresponding
BRO-prefixed value.
2.6-331 | 2019-05-23 18:03:42 -0700 2.6-331 | 2019-05-23 18:03:42 -0700
* Update broker unit test output. (Jon Siwek, Corelight) * Update broker unit test output. (Jon Siwek, Corelight)

View file

@ -1 +1 @@
2.6-331 2.6-334

View file

@ -16,7 +16,7 @@ export {
## Default address on which to listen. ## Default address on which to listen.
## ##
## .. zeek:see:: Broker::listen ## .. zeek:see:: Broker::listen
const default_listen_address = getenv("ZEEK_DEFAULT_LISTEN_ADDRESS") != "" ? getenv("ZEEK_DEFAULT_LISTEN_ADDRESS") : getenv("BRO_DEFAULT_LISTEN_ADDRESS") &redef; const default_listen_address = getenv("ZEEK_DEFAULT_LISTEN_ADDRESS") &redef;
## Default interval to retry connecting to a peer if it cannot be made to ## Default interval to retry connecting to a peer if it cannot be made to
## work initially, or if it ever becomes disconnected. Use of the ## work initially, or if it ever becomes disconnected. Use of the
@ -380,8 +380,6 @@ function listen(a: string, p: port, retry: interval): port
if ( bound == 0/tcp ) if ( bound == 0/tcp )
{ {
local e = getenv("ZEEK_DEFAULT_LISTEN_RETRY"); local e = getenv("ZEEK_DEFAULT_LISTEN_RETRY");
if ( e == "" )
e = getenv("BRO_DEFAULT_LISTEN_RETRY");
if ( e != "" ) if ( e != "" )
retry = double_to_interval(to_double(e)); retry = double_to_interval(to_double(e));

View file

@ -83,12 +83,8 @@ function default_rotation_postprocessor_func(info: Log::RotationInfo) : bool
local gz = info$fname[-3:] == ".gz" ? ".gz" : ""; local gz = info$fname[-3:] == ".gz" ? ".gz" : "";
local bls = getenv("ZEEK_LOG_SUFFIX"); local bls = getenv("ZEEK_LOG_SUFFIX");
if ( bls == "" )
{
bls = getenv("BRO_LOG_SUFFIX");
if ( bls == "" ) if ( bls == "" )
bls = "log"; bls = "log";
}
# Move file to name including both opening and closing time. # Move file to name including both opening and closing time.
local dst = fmt("%s.%s.%s%s", info$path, local dst = fmt("%s.%s.%s%s", info$path,

View file

@ -1807,12 +1807,9 @@ event net_done(t: time) { done_with_network = T; }
function log_file_name(tag: string): string function log_file_name(tag: string): string
{ {
local suffix = getenv("ZEEK_LOG_SUFFIX"); local suffix = getenv("ZEEK_LOG_SUFFIX");
if ( suffix == "" )
{
suffix = getenv("BRO_LOG_SUFFIX");
if ( suffix == "" ) if ( suffix == "" )
suffix = "log"; suffix = "log";
}
return fmt("%s.%s", tag, suffix); return fmt("%s.%s", tag, suffix);
} }

View file

@ -17,13 +17,10 @@ Brofiler::~Brofiler()
bool Brofiler::ReadStats() bool Brofiler::ReadStats()
{ {
char* bf = getenv("ZEEK_PROFILER_FILE"); char* bf = zeekenv("ZEEK_PROFILER_FILE");
if ( ! bf )
{
bf = getenv("BRO_PROFILER_FILE");
if ( ! bf ) if ( ! bf )
return false; return false;
}
FILE* f = fopen(bf, "r"); FILE* f = fopen(bf, "r");
if ( ! f ) if ( ! f )
@ -51,13 +48,10 @@ bool Brofiler::ReadStats()
bool Brofiler::WriteStats() bool Brofiler::WriteStats()
{ {
char* bf = getenv("ZEEK_PROFILER_FILE"); char* bf = zeekenv("ZEEK_PROFILER_FILE");
if ( ! bf )
{
bf = getenv("BRO_PROFILER_FILE");
if ( ! bf ) if ( ! bf )
return false; return false;
}
SafeDirname dirname{bf}; SafeDirname dirname{bf};

View file

@ -414,7 +414,7 @@ void DNS_Mgr::Init()
// script-layer option to configure the DNS resolver as it may not be // script-layer option to configure the DNS resolver as it may not be
// configured to the user's desired address at the time when we need to to // configured to the user's desired address at the time when we need to to
// the lookup. // the lookup.
auto dns_resolver = getenv("ZEEK_DNS_RESOLVER"); auto dns_resolver = zeekenv("ZEEK_DNS_RESOLVER");
auto dns_resolver_addr = dns_resolver ? IPAddr(dns_resolver) : IPAddr(); auto dns_resolver_addr = dns_resolver ? IPAddr(dns_resolver) : IPAddr();
char err[NB_DNS_ERRSIZE]; char err[NB_DNS_ERRSIZE];

View file

@ -339,7 +339,7 @@ function network_time%(%): time
## .. zeek:see:: setenv ## .. zeek:see:: setenv
function getenv%(var: string%): string function getenv%(var: string%): string
%{ %{
const char* env_val = getenv(var->CheckString()); const char* env_val = zeekenv(var->CheckString());
if ( ! env_val ) if ( ! env_val )
env_val = ""; // ### env_val = ""; // ###
return new StringVal(env_val); return new StringVal(env_val);

View file

@ -177,9 +177,7 @@ void Manager::InitPostScript()
BrokerConfig config{std::move(options)}; BrokerConfig config{std::move(options)};
auto max_threads_env = getenv("ZEEK_BROKER_MAX_THREADS"); auto max_threads_env = zeekenv("ZEEK_BROKER_MAX_THREADS");
if ( ! max_threads_env )
max_threads_env = getenv("BRO_BROKER_MAX_THREADS");
if ( max_threads_env ) if ( max_threads_env )
config.set("scheduler.max-threads", atoi(max_threads_env)); config.set("scheduler.max-threads", atoi(max_threads_env));
@ -305,9 +303,7 @@ void Manager::Peer(const string& addr, uint16_t port, double retry)
DBG_LOG(DBG_BROKER, "Starting to peer with %s:%" PRIu16, DBG_LOG(DBG_BROKER, "Starting to peer with %s:%" PRIu16,
addr.c_str(), port); addr.c_str(), port);
auto e = getenv("ZEEK_DEFAULT_CONNECT_RETRY"); auto e = zeekenv("ZEEK_DEFAULT_CONNECT_RETRY");
if ( ! e )
e = getenv("BRO_DEFAULT_CONNECT_RETRY");
if ( e ) if ( e )
retry = atoi(e); retry = atoi(e);

View file

@ -444,13 +444,10 @@ bool Ascii::DoHeartbeat(double network_time, double current_time)
string Ascii::LogExt() string Ascii::LogExt()
{ {
const char* ext = getenv("ZEEK_LOG_SUFFIX"); const char* ext = zeekenv("ZEEK_LOG_SUFFIX");
if ( ! ext )
{
ext = getenv("BRO_LOG_SUFFIX");
if ( ! ext ) if ( ! ext )
ext = "log"; ext = "log";
}
return ext; return ext;
} }

View file

@ -147,10 +147,7 @@ const char* bro_version()
bool bro_dns_fake() bool bro_dns_fake()
{ {
if ( getenv("ZEEK_DNS_FAKE") || getenv("BRO_DNS_FAKE") ) return zeekenv("ZEEK_DNS_FAKE");
return true;
else
return false;
} }
void usage(int code = 1) void usage(int code = 1)
@ -208,8 +205,8 @@ void usage(int code = 1)
fprintf(stderr, " $ZEEK_SEED_FILE | file to load seeds from (not set)\n"); fprintf(stderr, " $ZEEK_SEED_FILE | file to load seeds from (not set)\n");
fprintf(stderr, " $ZEEK_LOG_SUFFIX | ASCII log file extension (.%s)\n", logging::writer::Ascii::LogExt().c_str()); fprintf(stderr, " $ZEEK_LOG_SUFFIX | ASCII log file extension (.%s)\n", logging::writer::Ascii::LogExt().c_str());
fprintf(stderr, " $ZEEK_PROFILER_FILE | Output file for script execution statistics (not set)\n"); fprintf(stderr, " $ZEEK_PROFILER_FILE | Output file for script execution statistics (not set)\n");
fprintf(stderr, " $ZEEK_DISABLE_ZEEKYGEN | Disable Zeekygen documentation support (%s)\n", getenv("ZEEK_DISABLE_ZEEKYGEN") || getenv("BRO_DISABLE_BROXYGEN") ? "set" : "not set"); fprintf(stderr, " $ZEEK_DISABLE_ZEEKYGEN | Disable Zeekygen documentation support (%s)\n", zeekenv("ZEEK_DISABLE_ZEEKYGEN") ? "set" : "not set");
fprintf(stderr, " $ZEEK_DNS_RESOLVER | IPv4/IPv6 address of DNS resolver to use (%s)\n", getenv("ZEEK_DNS_RESOLVER") ? getenv("ZEEK_DNS_RESOLVER") : "not set, will use first IPv4 address from /etc/resolv.conf"); fprintf(stderr, " $ZEEK_DNS_RESOLVER | IPv4/IPv6 address of DNS resolver to use (%s)\n", zeekenv("ZEEK_DNS_RESOLVER") ? zeekenv("ZEEK_DNS_RESOLVER") : "not set, will use first IPv4 address from /etc/resolv.conf");
fprintf(stderr, "\n"); fprintf(stderr, "\n");
@ -428,10 +425,7 @@ int main(int argc, char** argv)
char* id_name = 0; char* id_name = 0;
char* events_file = 0; char* events_file = 0;
char* seed_load_file = getenv("ZEEK_SEED_FILE"); char* seed_load_file = zeekenv("ZEEK_SEED_FILE");
if ( ! seed_load_file )
seed_load_file = getenv("BRO_SEED_FILE");
char* seed_save_file = 0; char* seed_save_file = 0;
char* user_pcap_filter = 0; char* user_pcap_filter = 0;
char* debug_streams = 0; char* debug_streams = 0;
@ -500,9 +494,7 @@ int main(int argc, char** argv)
prefixes.append(strdup("")); // "" = "no prefix" prefixes.append(strdup("")); // "" = "no prefix"
char* p = getenv("ZEEK_PREFIXES"); char* p = zeekenv("ZEEK_PREFIXES");
if ( ! p )
p = getenv("BRO_PREFIXES");
if ( p ) if ( p )
add_to_name_list(p, ':', prefixes); add_to_name_list(p, ':', prefixes);
@ -1088,7 +1080,7 @@ int main(int argc, char** argv)
// Drain the event queue here to support the protocols framework configuring DPM // Drain the event queue here to support the protocols framework configuring DPM
mgr.Drain(); mgr.Drain();
if ( reporter->Errors() > 0 && ! getenv("ZEEK_ALLOW_INIT_ERRORS") ) if ( reporter->Errors() > 0 && ! zeekenv("ZEEK_ALLOW_INIT_ERRORS") )
reporter->FatalError("errors occurred while initializing"); reporter->FatalError("errors occurred while initializing");
broker_mgr->ZeekInitDone(); broker_mgr->ZeekInitDone();

View file

@ -958,15 +958,10 @@ const std::string& bro_path()
{ {
if ( bro_path_value.empty() ) if ( bro_path_value.empty() )
{ {
const char* path = getenv("ZEEKPATH"); const char* path = zeekenv("ZEEKPATH");
if ( ! path )
{
path = getenv("BROPATH");
if ( ! path ) if ( ! path )
path = DEFAULT_ZEEKPATH; path = DEFAULT_ZEEKPATH;
}
bro_path_value = path; bro_path_value = path;
} }
@ -984,30 +979,20 @@ extern void add_to_bro_path(const string& dir)
const char* bro_plugin_path() const char* bro_plugin_path()
{ {
const char* path = getenv("ZEEK_PLUGIN_PATH"); const char* path = zeekenv("ZEEK_PLUGIN_PATH");
if ( ! path )
{
path = getenv("BRO_PLUGIN_PATH");
if ( ! path ) if ( ! path )
path = BRO_PLUGIN_INSTALL_PATH; path = BRO_PLUGIN_INSTALL_PATH;
}
return path; return path;
} }
const char* bro_plugin_activate() const char* bro_plugin_activate()
{ {
const char* names = getenv("ZEEK_PLUGIN_ACTIVATE"); const char* names = zeekenv("ZEEK_PLUGIN_ACTIVATE");
if ( ! names )
{
names = getenv("BRO_PLUGIN_ACTIVATE");
if ( ! names ) if ( ! names )
names = ""; names = "";
}
return names; return names;
} }
@ -1403,11 +1388,7 @@ FILE* rotate_file(const char* name, RecordVal* rotate_info)
const char* log_file_name(const char* tag) const char* log_file_name(const char* tag)
{ {
const char* env = getenv("ZEEK_LOG_SUFFIX"); const char* env = zeekenv("ZEEK_LOG_SUFFIX");
if ( ! env )
env = getenv("BRO_LOG_SUFFIX");
return fmt("%s.%s", tag, (env ? env : "log")); return fmt("%s.%s", tag, (env ? env : "log"));
} }
@ -1862,3 +1843,34 @@ void bro_strerror_r(int bro_errno, char* buf, size_t buflen)
// GNU vs. XSI flavors make it harder to use strerror_r. // GNU vs. XSI flavors make it harder to use strerror_r.
strerror_r_helper(res, buf, buflen); strerror_r_helper(res, buf, buflen);
} }
char* zeekenv(const char* name)
{
static std::map<const char*, const char*, CompareString> legacy_vars = {
{ "ZEEKPATH", "BROPATH" },
{ "ZEEK_PLUGIN_PATH", "BRO_PLUGIN_PATH" },
{ "ZEEK_PLUGIN_ACTIVATE", "BRO_PLUGIN_ACTIVATE" },
{ "ZEEK_PREFIXES", "BRO_PREFIXES" },
{ "ZEEK_DNS_FAKE", "BRO_DNS_FAKE" },
{ "ZEEK_SEED_FILE", "BRO_SEED_FILE" },
{ "ZEEK_LOG_SUFFIX", "BRO_LOG_SUFFIX" },
{ "ZEEK_PROFILER_FILE", "BRO_PROFILER_FILE" },
{ "ZEEK_DISABLE_ZEEKYGEN", "BRO_DISABLE_BROXYGEN" },
{ "ZEEK_DEFAULT_CONNECT_RETRY", "BRO_DEFAULT_CONNECT_RETRY" },
{ "ZEEK_BROKER_MAX_THREADS", "BRO_BROKER_MAX_THREADS" },
{ "ZEEK_DEFAULT_LISTEN_ADDRESS", "BRO_DEFAULT_LISTEN_ADDRESS" },
{ "ZEEK_DEFAULT_LISTEN_RETRY", "BRO_DEFAULT_LISTEN_RETRY" },
};
auto rval = getenv(name);
if ( rval )
return rval;
auto it = legacy_vars.find(name);
if ( it == legacy_vars.end() )
return rval;
return getenv(it->second);
}

View file

@ -549,4 +549,10 @@ std::string canonify_name(const std::string& name);
*/ */
void bro_strerror_r(int bro_errno, char* buf, size_t buflen); void bro_strerror_r(int bro_errno, char* buf, size_t buflen);
/**
* A wrapper function for getenv(). Helps check for existence of
* legacy environment variable names that map to the latest \a name.
*/
char* zeekenv(const char* name);
#endif #endif

View file

@ -64,7 +64,7 @@ Manager::Manager(const string& arg_config, const string& bro_command)
identifiers(), all_info(), last_identifier_seen(), incomplete_type(), identifiers(), all_info(), last_identifier_seen(), incomplete_type(),
enum_mappings(), config(arg_config), bro_mtime() enum_mappings(), config(arg_config), bro_mtime()
{ {
if ( getenv("ZEEK_DISABLE_ZEEKYGEN") || getenv("BRO_DISABLE_BROXYGEN") ) if ( zeekenv("ZEEK_DISABLE_ZEEKYGEN") )
disabled = true; disabled = true;
// If running bro without the "-X" option, then we don't need bro_mtime. // If running bro without the "-X" option, then we don't need bro_mtime.