Make it possible to pass command line options through to scripts.

The feature is documented with the zeek_script_args variable in
init-bare.zeek.
This commit is contained in:
Seth Hall 2020-10-08 15:46:28 -04:00
parent 6902b645ba
commit 2bdc56dfcd
5 changed files with 69 additions and 2 deletions

View file

@ -454,6 +454,18 @@ type connection: record {
inner_vlan: int &optional;
};
## Arguments given to Zeek from the command line. In order to use this, Zeek
## must use the "--" command line argument, then give the script name immediately
## after the double hyphens and the provide the arguments after that. For example:
##
## zeek --bare-mode -- myscript.zeek -a -b -c
##
## To use Zeek as an executable interpreter, include a line at the top of a script
## like the following and make the script executable:
##
## #!/usr/local/zeek/bin/zeek --
const zeek_script_args: vector of string = vector();
## Default amount of time a file can be inactive before the file analysis
## gives up and discards any internal state related to the file.
option default_file_timeout_interval: interval = 2 mins;

View file

@ -30,6 +30,8 @@ zeek::VectorType* index_vec;
zeek::VectorType* mime_matches;
zeek::RecordType* mime_match;
zeek::VectorVal* zeek_script_args;
zeek::RecordType* socks_address;
zeek::TableVal* tcp_reassembler_ports_orig;

View file

@ -5,6 +5,7 @@
#include "Options.h"
#include <algorithm>
#include <sstream>
#include <unistd.h>
@ -186,10 +187,50 @@ Options parse_cmdline(int argc, char** argv)
}
else
{
for ( auto i = 0; i < argc; ++i )
if ( argc > 1 )
{
auto endsWith = [](const std::string& str, const std::string& suffix)
{
return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
};
auto i = 0;
for ( ; i < argc && ! endsWith(argv[i], "--"); ++i )
{
zeek_args.emplace_back(argv[i]);
}
// If a script is invoked with Zeek as the interpreter, the arguments provided
// directly in the interpreter line of the script won't be broken apart in the
// argv on Linux so we split it up here.
if ( endsWith(argv[i], "--") && zeek_args.size() == 1 )
{
std::istringstream iss(argv[i]);
for ( std::string s; iss >> s; )
{
if ( ! endsWith(s, "--") )
{
zeek_args.emplace_back(s);
}
}
}
if ( i < argc )
{
// There is an additional increment here to skip over the "--" if it was found.
if ( endsWith(argv[i], "--") )
++i;
// The first argument after the double hyphens in implicitly a script name.
rval.scripts_to_load.emplace_back(argv[i++]);
// If there are more argument, grab them for script arguments
for ( ; i < argc; ++i )
rval.script_args.emplace_back(argv[i]);
}
}
}
constexpr struct option long_opts[] = {
{"parse-only", no_argument, nullptr, 'a'},
{"bare-mode", no_argument, nullptr, 'b'},

View file

@ -74,6 +74,8 @@ struct Options {
std::set<std::string> plugins_to_load;
std::vector<std::string> scripts_to_load;
std::vector<std::string> script_options_to_set;
std::vector<std::string> script_args;
};
/**

View file

@ -660,6 +660,16 @@ SetupResult setup(int argc, char** argv, Options* zopts)
init_net_var();
run_bif_initializers();
// Assign the script_args for command line processing in Zeek scripts.
if ( ! options.script_args.empty() )
{
auto script_args_val = zeek::id::find_val("zeek_script_args")->AsVectorVal();
for ( const string& script_arg: options.script_args )
{
script_args_val->Assign(script_args_val->Size(), zeek::make_intrusive<zeek::StringVal>(script_arg));
}
}
// Must come after plugin activation (and also after hash
// initialization).
binpac::FlowBuffer::Policy flowbuffer_policy;