diff --git a/CHANGES b/CHANGES index d9146fbb9b..1779902ddd 100644 --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,11 @@ +2.6-227 | 2019-04-16 17:44:31 -0700 + + * GH-237: add `@load foo.bro` -> foo.zeek fallback (Jon Siwek, Corelight) + + When failing to locate a script with explicit .bro suffix, check for + whether one with a .zeek suffix exists and use it instead. + 2.6-225 | 2019-04-16 16:07:49 -0700 * Use .zeek file suffix in unit tests (Jon Siwek, Corelight) diff --git a/NEWS b/NEWS index 36b9556b3e..a1e04773ff 100644 --- a/NEWS +++ b/NEWS @@ -81,10 +81,12 @@ Changed Functionality been renamed to ``.zeek``. - The search logic for the ``@load`` script directive now prefers files - ending in ``.zeek``, but will fallback to loading a ``.bro`` file if it - exists. E.g. ``@load foo`` will check for ``foo.zeek`` and then ``foo.bro``. - Note that ``@load foo.bro`` will not automatically check for - ``@load foo.zeek``. + ending in ``.zeek``, but will fallback to loading a ``.bro`` file if + it exists. E.g. ``@load foo`` will first check for a ``foo.zeek`` + file to load and then otherwise ``foo.bro``. Note that + ``@load foo.bro`` (with the explicit ``.bro`` file suffix) prefers + in the opposite order: it first checks for ``foo.bro`` and then + falls back to a ``foo.zeek``, if it exists. - The for-loop index variable for vectors has been changed from 'int' to 'count' type. It's unlikely this would alter/break any diff --git a/VERSION b/VERSION index 23ad9f21a7..168f57bc28 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.6-225 +2.6-227 diff --git a/src/util.cc b/src/util.cc index 8b4bd0a88b..0367700ffb 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1298,6 +1298,14 @@ string find_file(const string& filename, const string& path_set, return string(); } +static bool ends_with(const std::string& s, const std::string& ending) + { + if ( ending.size() > s.size() ) + return false; + + return std::equal(ending.rbegin(), ending.rend(), s.rbegin()); + } + string find_script_file(const string& filename, const string& path_set) { vector paths; @@ -1313,6 +1321,14 @@ string find_script_file(const string& filename, const string& path_set) return f; } + if ( ends_with(filename, ".bro") ) + { + // We were looking for a file explicitly ending in .bro and didn't + // find it, so fall back to one ending in .zeek, if it exists. + auto fallback = string(filename.data(), filename.size() - 4) + ".zeek"; + return find_script_file(fallback, path_set); + } + return string(); } diff --git a/testing/btest/Baseline/core.load-explicit-bro-suffix-fallback/out b/testing/btest/Baseline/core.load-explicit-bro-suffix-fallback/out new file mode 100644 index 0000000000..c67eefbfc1 --- /dev/null +++ b/testing/btest/Baseline/core.load-explicit-bro-suffix-fallback/out @@ -0,0 +1 @@ +loaded foo.zeek diff --git a/testing/btest/core/load-explicit-bro-suffix-fallback.zeek b/testing/btest/core/load-explicit-bro-suffix-fallback.zeek new file mode 100644 index 0000000000..28f770ca48 --- /dev/null +++ b/testing/btest/core/load-explicit-bro-suffix-fallback.zeek @@ -0,0 +1,12 @@ +# @TEST-EXEC: bro -b %INPUT >out +# @TEST-EXEC: btest-diff out + +# We don't have a foo.bro, but we'll accept foo.zeek. +@load foo.bro + +@TEST-START-FILE foo.zeek +event bro_init() + { + print "loaded foo.zeek"; + } +@TEST-END-FILE