Kea 3.3.3
user_chk/load_unload.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
8
9#include <config.h>
10
11#include <hooks/hooks.h>
12#include <user_chk/user_file.h>
14
15#include <fstream>
16#include <iostream>
17
18#include <errno.h>
19#include <user_chk_log.h>
20
21using namespace isc::hooks;
22using namespace user_chk;
23
26
28std::fstream user_chk_output;
29
32const char* registry_fname = "/usr/local/share/kea/user_chk_registry.txt";
33
36const char* user_chk_output_fname = "/usr/local/share/kea/user_chk_outcome.txt";
37
39const char* query_user_id_label = "query_user_id";
40
42const char* registered_user_label = "registered_user";
43
48const char* default_user4_id_str = "00000000";
49
54const char *default_user6_id_str = "00000000";
55
56// Functions accessed by the hooks framework use C linkage to avoid the name
57// mangling that accompanies use of the C++ compiler as well as to avoid
58// issues related to namespaces.
59extern "C" {
60
68 // non-zero indicates an error.
69 int ret_val = 0;
70
71 try {
72 // If the hook library is dedicated to a specific server(s)
73 // please check here process name (Daemon::getProcName() from
74 // the process library).
75
76 // Instantiate the registry.
77 user_registry.reset(new UserRegistry());
78
79 // Create the data source.
81
82 // Set the registry's data source
83 user_registry->setSource(user_file);
84
85 // Do an initial load of the registry.
86 user_registry->refresh();
87
88 // zero out the errno to be safe
89 errno = 0;
90
91 // Open up the output file for user_chk results.
93 std::fstream::out | std::fstream::app);
94
95 if (!user_chk_output) {
96 // Grab the system error message.
97 const char* errmsg = strerror(errno);
98 isc_throw(isc::Unexpected, "Cannot open output file: "
100 << " reason: " << errmsg);
101 }
102 }
103 catch (const std::exception& ex) {
104 // Log the error and return failure.
106 .arg(ex.what());
107 ret_val = 1;
108 }
109
110 return (ret_val);
111}
112
118int unload() {
119 try {
120 user_registry.reset();
121 if (user_chk_output.is_open()) {
122 user_chk_output.close();
123 }
124 } catch (const std::exception& ex) {
125 // On the off chance something goes awry, catch it and log it.
126 // @todo Not sure if we should return a non-zero result or not.
128 .arg(ex.what());
129 }
130
131 return (0);
132}
133
134}
A generic exception that is thrown when an unexpected error condition occurs.
Provides a UserDataSource implementation for JSON text files.
Definition user_file.h:60
Embodies an update-able, searchable list of unique users This class provides the means to create and ...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
Defines the logger used by the user check hooks library.
Definition user.cc:18
boost::shared_ptr< UserRegistry > UserRegistryPtr
Define a smart pointer to a UserRegistry.
boost::shared_ptr< UserDataSource > UserDataSourcePtr
Defines a smart pointer to a UserDataSource.
isc::log::Logger user_chk_logger("user_chk")
User Check Logger.
const char * registry_fname
User registry input file name.
int load(LibraryHandle &)
Called by the Hooks library manager when the library is loaded.
const char * query_user_id_label
Text label of user id in the inbound query in callout context.
const char * user_chk_output_fname
User check outcome file name.
std::fstream user_chk_output
Output filestream for recording user check outcomes.
UserRegistryPtr user_registry
Pointer to the registry instance.
const char * default_user4_id_str
Text id used to identify the default IPv4 user in the registry The format is a string containing an e...
const char * registered_user_label
Text label of registered user pointer in callout context.
int unload()
Called by the Hooks library manager when the library is unloaded.
const char * default_user6_id_str
Text id used to identify the default IPv6 user in the registry The format is a string containing an e...
const isc::log::MessageID USER_CHK_HOOK_LOAD_ERROR
const isc::log::MessageID USER_CHK_HOOK_UNLOAD_ERROR
Defines the class, UserFile, which implements the UserDataSource interface for text files.
Defines the class, UserRegistry.