Kea: load_unload.cc Source File

| Kea 3.2.0-git

user_chk/load_unload.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2025 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_log.h>
13#include <user_registry.h>
14#include <user_file.h>
15
16#include <iostream>
17#include <fstream>
18#include <errno.h>
19
20using namespace isc::hooks;
21using namespace user_chk;
22
24 UserRegistryPtr user_registry;
25
27std::fstream user_chk_output;
28
31 const char* registry_fname = "/usr/local/share/kea/user_chk_registry.txt";
32
35 const char* user_chk_output_fname = "/usr/local/share/kea/user_chk_outcome.txt";
36
38 const char* query_user_id_label = "query_user_id";
39
41 const char* registered_user_label = "registered_user";
42
47 const char* default_user4_id_str = "00000000";
48
53 const char *default_user6_id_str = "00000000";
54
55// Functions accessed by the hooks framework use C linkage to avoid the name
56// mangling that accompanies use of the C++ compiler as well as to avoid
57// issues related to namespaces.
58extern "C" {
59
66 int load(LibraryHandle&) {
67 // non-zero indicates an error.
68 int ret_val = 0;
69
70 try {
71 // If the hook library is dedicated to a specific server(s)
72 // please check here process name (Daemon::getProcName() from
73 // the process library).
74
75 // Instantiate the registry.
76 user_registry.reset(new UserRegistry());
77
78 // Create the data source.
79 UserDataSourcePtr user_file(new UserFile(registry_fname));
80
81 // Set the registry's data source
82 user_registry->setSource(user_file);
83
84 // Do an initial load of the registry.
85 user_registry->refresh();
86
87 // zero out the errno to be safe
88 errno = 0;
89
90 // Open up the output file for user_chk results.
91 user_chk_output.open(user_chk_output_fname,
92 std::fstream::out | std::fstream::app);
93
94 if (!user_chk_output) {
95 // Grab the system error message.
96 const char* errmsg = strerror(errno);
97 isc_throw(isc::Unexpected, "Cannot open output file: "
98 << user_chk_output_fname
99 << " reason: " << errmsg);
100 }
101 }
102 catch (const std::exception& ex) {
103 // Log the error and return failure.
104 LOG_ERROR(user_chk_logger, USER_CHK_HOOK_LOAD_ERROR)
105 .arg(ex.what());
106 ret_val = 1;
107 }
108
109 return (ret_val);
110}
111
117 int unload() {
118 try {
119 user_registry.reset();
120 if (user_chk_output.is_open()) {
121 user_chk_output.close();
122 }
123 } catch (const std::exception& ex) {
124 // On the off chance something goes awry, catch it and log it.
125 // @todo Not sure if we should return a non-zero result or not.
126 LOG_ERROR(user_chk_logger, USER_CHK_HOOK_UNLOAD_ERROR)
127 .arg(ex.what());
128 }
129
130 return (0);
131}
132
133}
isc::Unexpected
A generic exception that is thrown when an unexpected error condition occurs.
Definition exceptions/exceptions.h:153
isc::hooks::LibraryHandle
Library handle.
Definition library_handle.h:60
user_chk::UserFile
Provides a UserDataSource implementation for JSON text files.
Definition user_file.h:58
user_chk::UserRegistry
Embodies an update-able, searchable list of unique users This class provides the means to create and ...
Definition user_registry.h:38
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
hooks.h
LOG_ERROR
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
isc::hooks
Definition callout_handle.cc:21
user_chk
Defines the logger used by the user check hooks library.
Definition user.cc:19
user_chk::UserRegistryPtr
boost::shared_ptr< UserRegistry > UserRegistryPtr
Define a smart pointer to a UserRegistry.
Definition user_registry.h:118
user_chk::UserDataSourcePtr
boost::shared_ptr< UserDataSource > UserDataSourcePtr
Defines a smart pointer to a UserDataSource.
Definition user_data_source.h:67
user_chk::user_chk_logger
isc::log::Logger user_chk_logger("user_chk")
User Check Logger.
Definition user_chk_log.h:21
registry_fname
const char * registry_fname
User registry input file name.
Definition user_chk/load_unload.cc:31
load
int load(LibraryHandle &)
Called by the Hooks library manager when the library is loaded.
Definition user_chk/load_unload.cc:66
query_user_id_label
const char * query_user_id_label
Text label of user id in the inbound query in callout context.
Definition user_chk/load_unload.cc:38
user_chk_output_fname
const char * user_chk_output_fname
User check outcome file name.
Definition user_chk/load_unload.cc:35
user_chk_output
std::fstream user_chk_output
Output filestream for recording user check outcomes.
Definition user_chk/load_unload.cc:27
user_registry
UserRegistryPtr user_registry
Pointer to the registry instance.
Definition user_chk/load_unload.cc:24
default_user4_id_str
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...
Definition user_chk/load_unload.cc:47
registered_user_label
const char * registered_user_label
Text label of registered user pointer in callout context.
Definition user_chk/load_unload.cc:41
unload
int unload()
Called by the Hooks library manager when the library is unloaded.
Definition user_chk/load_unload.cc:117
default_user6_id_str
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...
Definition user_chk/load_unload.cc:53
user_chk_log.h
USER_CHK_HOOK_LOAD_ERROR
const isc::log::MessageID USER_CHK_HOOK_LOAD_ERROR
USER_CHK_HOOK_UNLOAD_ERROR
const isc::log::MessageID USER_CHK_HOOK_UNLOAD_ERROR
user_file.h
Defines the class, UserFile, which implements the UserDataSource interface for text files.
user_registry.h
Defines the class, UserRegistry.