Kea  1.9.9-git
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 
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  // Instantiate the registry.
72  user_registry.reset(new UserRegistry());
73 
74  // Create the data source.
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.
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: "
95  << " reason: " << errmsg);
96  }
97  }
98  catch (const std::exception& ex) {
99  // Log the error and return failure.
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.
123  .arg(ex.what());
124  }
125 
126  return (0);
127 }
128 
129 }
const char * user_chk_output_fname
User check outcome file name.
Definition: load_unload.cc:35
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 * 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
int unload()
Called by the Hooks library manager when the library is unloaded.
Definition: load_unload.cc:113
Provides a UserDataSource implementation for JSON text files.
Definition: user_file.h:58
const char * registered_user_label
Text label of registered user pointer in callout context.
Definition: load_unload.cc:41
Defines the class, UserFile, which implements the UserDataSource interface for text files...
Defines the logger used by the user check hooks library.
Definition: user.cc:19
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::fstream user_chk_output
Output filestream for recording user check outcomes.
Definition: load_unload.cc:27
int load(LibraryHandle &)
Called by the Hooks library manager when the library is loaded.
Definition: load_unload.cc:66
A generic exception that is thrown when an unexpected error condition occurs.
isc::log::Logger user_chk_logger("user_chk")
User Check Logger.
Definition: user_chk_log.h:21
boost::shared_ptr< UserDataSource > UserDataSourcePtr
Defines a smart pointer to a UserDataSource.
Embodies an update-able, searchable list of unique users This class provides the means to create and ...
Definition: user_registry.h:38
const isc::log::MessageID USER_CHK_HOOK_UNLOAD_ERROR
UserRegistryPtr user_registry
Pointer to the registry instance.
Definition: load_unload.cc:24
boost::shared_ptr< UserRegistry > UserRegistryPtr
Define a smart pointer to a UserRegistry.
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 isc::log::MessageID USER_CHK_HOOK_LOAD_ERROR
Defines the class, UserRegistry.
const char * registry_fname
User registry input file name.
Definition: load_unload.cc:31