Kea  2.5.2
load_unload.cc
Go to the documentation of this file.
1 // Copyright (C) 2013-2022 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 
20 using namespace isc::hooks;
21 using namespace user_chk;
22 
25 
27 std::fstream user_chk_output;
28 
31 const char* registry_fname = "/tmp/user_chk_registry.txt";
32 
35 const char* user_chk_output_fname = "/tmp/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.
58 extern "C" {
59 
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.
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.
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: "
99  << " reason: " << errmsg);
100  }
101  }
102  catch (const std::exception& ex) {
103  // Log the error and return failure.
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.
127  .arg(ex.what());
128  }
129 
130  return (0);
131 }
132 
133 }
A generic exception that is thrown when an unexpected error condition occurs.
Provides a UserDataSource implementation for JSON text files.
Definition: user_file.h:58
Embodies an update-able, searchable list of unique users This class provides the means to create and ...
Definition: user_registry.h:38
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
const char * registry_fname
User registry input file name.
Definition: load_unload.cc:31
int load(LibraryHandle &)
Called by the Hooks library manager when the library is loaded.
Definition: load_unload.cc:66
const char * query_user_id_label
Text label of user id in the inbound query in callout context.
Definition: load_unload.cc:38
const char * user_chk_output_fname
User check outcome file name.
Definition: load_unload.cc:35
std::fstream user_chk_output
Output filestream for recording user check outcomes.
Definition: load_unload.cc:27
UserRegistryPtr user_registry
Pointer to the registry instance.
Definition: load_unload.cc:24
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: load_unload.cc:47
const char * registered_user_label
Text label of registered user pointer in callout context.
Definition: load_unload.cc:41
int unload()
Called by the Hooks library manager when the library is unloaded.
Definition: load_unload.cc:117
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: load_unload.cc:53
#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:19
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.
Definition: user_chk_log.h:21
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.