Kea: load_unload.cc Source File

| Kea 1.5.0

load_unload.cc
Go to the documentation of this file.
1 // Copyright (C) 2013-2015 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
24 UserRegistryPtr user_registry;
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
66 int load(LibraryHandle&) {
67 // non-zero indicates an error.
68 int ret_val = 0;
69
70 try {
71 // Instantiate the registry.
72 user_registry.reset(new UserRegistry());
73
74 // Create the data source.
75 UserDataSourcePtr user_file(new UserFile(registry_fname));
76
77 // Set the registry's data source
78 user_registry->setSource(user_file);
79
80 // Do an initial load of the registry.
81 user_registry->refresh();
82
83 // zero out the errno to be safe
84 errno = 0;
85
86 // Open up the output file for user_chk results.
87 user_chk_output.open(user_chk_output_fname,
88 std::fstream::out | std::fstream::app);
89
90 if (!user_chk_output) {
91 // Grab the system error message.
92 const char* errmsg = strerror(errno);
93 isc_throw(isc::Unexpected, "Cannot open output file: "
94 << user_chk_output_fname
95 << " reason: " << errmsg);
96 }
97 }
98 catch (const std::exception& ex) {
99 // Log the error and return failure.
100 LOG_ERROR(user_chk_logger, USER_CHK_HOOK_LOAD_ERROR)
101 .arg(ex.what());
102 ret_val = 1;
103 }
104
105 return (ret_val);
106 }
107
113 int unload() {
114 try {
115 user_registry.reset();
116 if (user_chk_output.is_open()) {
117 user_chk_output.close();
118 }
119 } catch (const std::exception& ex) {
120 // On the off chance something goes awry, catch it and log it.
121 // @todo Not sure if we should return a non-zero result or not.
122 LOG_ERROR(user_chk_logger, USER_CHK_HOOK_UNLOAD_ERROR)
123 .arg(ex.what());
124 }
125
126 return (0);
127 }
128
129 }
user_registry
UserRegistryPtr user_registry
Pointer to the registry instance.
Definition: load_unload.cc:24
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: load_unload.cc:53
isc::Unexpected
A generic exception that is thrown when an unexpected error condition occurs.
Definition: exceptions/exceptions.h:153
LOG_ERROR
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
hooks.h
user_registry.h
query_user_id_label
const char * query_user_id_label
Text label of user id in the inbound query in callout context.
Definition: load_unload.cc:38
user_chk_output
std::fstream user_chk_output
Output filestream for recording user check outcomes.
Definition: load_unload.cc:27
registered_user_label
const char * registered_user_label
Text label of registered user pointer in callout context.
Definition: load_unload.cc:41
unload
int unload()
Called by the Hooks library manager when the library is unloaded.
Definition: load_unload.cc:113
user_chk::UserFile
Provides a UserDataSource implementation for JSON text files.
Definition: user_file.h:58
user_chk::UserDataSourcePtr
boost::shared_ptr< UserDataSource > UserDataSourcePtr
Defines a smart pointer to a UserDataSource.
Definition: user_data_source.h:67
isc::hooks::LibraryHandle
Library handle.
Definition: library_handle.h:60
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
user_file.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
user_chk::user_chk_logger
isc::log::Logger user_chk_logger("user_chk")
User Check Logger.
Definition: user_chk_log.h:21
load
int load(LibraryHandle &)
Called by the Hooks library manager when the library is loaded.
Definition: load_unload.cc:66
user_chk
Defines the logger used by the user check hooks library.
Definition: user.cc:19
user_chk_log.h
user_chk::UserRegistryPtr
boost::shared_ptr< UserRegistry > UserRegistryPtr
Define a smart pointer to a UserRegistry.
Definition: user_registry.h:118
isc::hooks
Definition: callout_handle.cc:21
user_chk_output_fname
const char * user_chk_output_fname
User check outcome file name.
Definition: load_unload.cc:35
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: load_unload.cc:47
registry_fname
const char * registry_fname
User registry input file name.
Definition: load_unload.cc:31