Kea 3.1.1
flex_id/load_unload.cc
Go to the documentation of this file.
1// Copyright (C) 2017-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 <cc/data.h>
12#include <dhcp/option.h>
13#include <dhcpsrv/cfgmgr.h>
14#include <eval/token.h>
15#include <eval/eval_context.h>
16#include <hooks/hooks.h>
17#include <process/daemon.h>
18#include <flex_id.h>
19#include <flex_id_log.h>
20
21using namespace isc;
22using namespace hooks;
23using namespace flex_id;
24using namespace isc::data;
25using namespace isc::dhcp;
26using namespace isc::process;
27using namespace isc::log;
28
29namespace { // anonymous namespace.
30
35bool checkExpression(bool v6, const std::string& expr) {
36 try {
37 EvalContext eval_ctx(v6 ? Option::V6 : Option::V4);
38 eval_ctx.parseString(expr, EvalContext::PARSER_STRING);
39 return (true);
40 } catch (const std::exception& ex) {
42 .arg(expr)
43 .arg(ex.what());
44 return (false);
45 }
46}
47
48} // end of anonymous namespace.
49
50// Functions accessed by the hooks framework use C linkage to avoid the name
51// mangling that accompanies use of the C++ compiler as well as to avoid
52// issues related to namespaces.
53extern "C" {
54
61int load(LibraryHandle& handle) {
62 // non-zero indicates an error.
63 int ret_val = 0;
64
65 try {
66 // Make the hook library not loadable by d2 or ca.
67 bool v6 = (CfgMgr::instance().getFamily() == AF_INET6);
68 const std::string& proc_name = Daemon::getProcName();
69 const std::string& expected_proc_name =
70 (v6 ? "kea-dhcp6" : "kea-dhcp4");
71 if (proc_name != expected_proc_name) {
72 isc_throw(Unexpected, "Bad process name: " << proc_name
73 << ", expected " << expected_proc_name);
74 }
75
76 // identifier-expression is optional.
77 data::ConstElementPtr param = handle.getParameter("identifier-expression");
78 std::string expr;
79 if (param) {
80 // It must be a string...
81 if (param->getType() != Element::string) {
83 .arg(Element::typeToName(param->getType()));
84 return (1);
85 }
86
87 std::string expr = param->stringValue();
88 if (!expr.empty() && !checkExpression(v6, expr)) {
89 // The error was logged.
90 return (1);
91 }
92 }
93
94 if (expr.empty()) {
95 // Ok, we can continue without the expression. This is likely the
96 // case when users are only interested in ignore-iaid.
99 }
100
101 // replace-client-id indicates if flexible identifier should be used to
102 // replace original client identifier (or DUID) within the client query.
103 bool replace_client_id = false;
104 data::ConstElementPtr param_replace = handle.getParameter("replace-client-id");
105 if (param_replace) {
106 // It must be a boolean value.
107 if (param_replace->getType() != Element::boolean) {
109 .arg(Element::typeToName(param_replace->getType()));
110 return (1);
111 }
112
113 replace_client_id = param_replace->boolValue();
114 }
115
116 // ignore-iaid indicates if iaid should be ignored.
117 bool ignore_iaid = false;
118 if (v6) {
119 data::ConstElementPtr param_ignore = handle.getParameter("ignore-iaid");
120 if (param_ignore) {
121 // It must be a boolean value.
122 if (param_ignore->getType() != Element::boolean) {
124 .arg(Element::typeToName(param_ignore->getType()));
125 return (1);
126 }
127
128 ignore_iaid = param_ignore->boolValue();
129 if (ignore_iaid) {
131 }
132 }
133 }
134
135 // Remove any old expressions that may have been stored.
137
138 // Store specified expression.
139 storeConfiguration(v6, expr, replace_client_id, ignore_iaid);
140 } catch (const std::exception& ex) {
141 // Log the error and return failure.
143 .arg(ex.what());
144 ret_val = 1;
145 }
146
147 return (ret_val);
148}
149
153int unload() {
155 return (0);
156}
157
162 return (1);
163}
164
165}
static std::string typeToName(Element::types type)
Returns the name of the given type as a string.
Definition data.cc:651
@ boolean
Definition data.h:142
@ string
Definition data.h:144
A generic exception that is thrown when an unexpected error condition occurs.
uint16_t getFamily() const
Returns address family.
Definition cfgmgr.h:246
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:29
Evaluation context, an interface to the expression evaluation.
@ PARSER_STRING
expression is expected to evaluate to string
isc::data::ConstElementPtr getParameter(const std::string &name)
Returns configuration parameter for the library.
static std::string getProcName()
returns the process name This value is used as when forming the default PID file name
Definition daemon.cc:151
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
int multi_threading_compatible()
This function is called to retrieve the multi-threading compatibility.
int unload()
Called by the Hooks library manager when the library is unloaded.
int load(LibraryHandle &handle)
Called by the Hooks library manager when the library is loaded.
const isc::log::MessageID FLEX_ID_IGNORE_IAID_JSON_TYPE
const isc::log::MessageID FLEX_ID_UNLOAD
const isc::log::MessageID FLEX_ID_EXPRESSION_PARSE_FAILED
const isc::log::MessageID FLEX_ID_IGNORE_IAID_ENABLED
const isc::log::MessageID FLEX_ID_LOAD_ERROR
const isc::log::MessageID FLEX_ID_NO_IDENTIFIER_EXPRESSION
const isc::log::MessageID FLEX_ID_REPLACE_CLIENT_ID_JSON_TYPE
const isc::log::MessageID FLEX_ID_EXPRESSION_INVALID_JSON_TYPE
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::log::Logger flex_id_logger("flex-id-hooks")
Flexible Identifier Logger.
Definition flex_id_log.h:22
void clearConfiguration()
Clears stored configuration.
Definition callouts.cc:74
void storeConfiguration(bool v6, const std::string &expr, const bool apply_to_leases, const bool ignore_iaid)
Stores expression.
Definition callouts.cc:64
const int DBGLVL_TRACE_BASIC
Trace basic operations.
Defines the logger used by the top-level component of kea-lfc.