Fix MMDB code to re-open explicitly opened DBs correctly

The filename from which a DB first gets opened (either via an explicitly
specified filename, or via the path sequence now configurable at the script
layer) is now "sticky", meaning re-opening won't switch to a different file.

This was easiest by moving most state into the MMDB class itself. The previous
approach of tracking the two DB instances via a smart pointer and blowing the
pointed-to objects away as needed is now instead one of two objects fixed over
the lifetime of Zeek, able to open/close/reopen their underlying Maxmind DBs.

The MMDB class now only has one Lookup() method since there was no need to break
them apart -- it saves the return of a MMDB_lookup_result_s over the stack and
there's no need for throwing an exception.
This commit is contained in:
Christian Kreibich 2024-01-10 12:22:24 -08:00
parent 2e3270d7ec
commit e8f0f727cd
2 changed files with 183 additions and 174 deletions

View file

@ -10,25 +10,76 @@ namespace zeek {
#include <maxminddb.h>
// The MMDB class encapsulates a low-level libmaxmind MMDB_s structure. It
// tracks whether that DB is currently loaded, and can open it from a file in
// two ways: (1) via explicit specification of a filename, (2) by determining
// the configuration from configuration settings in the script layer (mmdb_dir
// etc). This configuration depends on whether this is a geolocation DB or an
// ASN one, so details are left to derived classes below that specialize.
//
// The class tracks the inode and modification time of a DB file to detect
// "stale" DBs, which get reloaded (from the same location in the file system)
// upon the first lookup that detects staleness.
class MMDB {
public:
MMDB(const char* filename, struct stat info);
MMDB();
virtual ~MMDB();
~MMDB();
// Implements the logic to determine a file system path for the DB from
// script-layer configuration settings, and opens the DB from there. Returns
// true if successful, false otherwise.
virtual bool OpenFromScriptConfig() = 0;
// Helper string to identify the type of DB, useful in error messages.
virtual const char* Description() = 0;
// Opens the DB at the given location, closing and cleaning up any currently
// opened DB if there is one. Returns true if successful, false otherwise.
bool OpenFile(const char* filename);
// Closes a currently opened DB, releasing its state. Safe to call on a
// closed DB.
void Close();
// Predicate; returns true if the DB is currently opened.
bool IsOpen() const { return mmdb.filename != nullptr; }
// Ensures that upon return the underlying DB file is loaded. When no
// filename is configured for the DB (i.e. OpenFile() has never been called
// on it), this triggers the script-level configuration lookup via
// OpenFromScriptConfig(). When a filename is available but it's not
// currently loaded, it does so. Finally, if there's a loaded DB but it's
// found to be stale, it gets reloaded. When the load operation succeeds, or
// the DB was already loaded and not stale, this returns true, and false if
// anything went wrong.
bool EnsureLoaded();
// Looks up a given IP address in the DB, storing the result in the provided
// result structure.
bool Lookup(const zeek::IPAddr& addr, MMDB_lookup_result_s& result);
bool StaleDB();
const char* Filename();
private:
MMDB_lookup_result_s Lookup(const struct sockaddr* const sa);
bool IsStaleDB();
std::string filename;
MMDB_s mmdb;
struct stat file_info;
bool lookup_error;
bool reported_error; // to ensure we emit builtin errors during opening only once.
double last_check;
};
class LocDB : public MMDB {
public:
bool OpenFromScriptConfig();
const char* Description() { return "GeoIP location database"; }
};
class AsnDB : public MMDB {
public:
bool OpenFromScriptConfig();
const char* Description() { return "GeoIP ASN database"; }
};
#endif // USE_GEOIP
ValPtr mmdb_open_location_db(zeek::StringVal* filename);