Kea 3.3.1
dhcp6/main.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2026 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
7#include <config.h>
8#include <kea_version.h>
9
11#include <dhcp6/dhcp6_log.h>
14#include <dhcpsrv/cfgmgr.h>
16#include <log/logger_support.h>
17#include <log/logger_manager.h>
18#include <log/output_option.h>
20#include <process/daemon.h>
21#include <util/filesystem.h>
22
23#include <boost/lexical_cast.hpp>
24
25#include <cstdlib>
26#include <iostream>
27
28using namespace isc::data;
29using namespace isc::dhcp;
30using namespace isc::process;
31using namespace isc::util::file;
32using namespace std;
33
42
43namespace {
44
45const char* const DHCP6_NAME = "kea-dhcp6";
46
50void
51usage() {
52 cerr << "Kea DHCPv6 server, "
53 << "version " << VERSION
54 << " (" << PACKAGE_VERSION_TYPE << ")"
55 << endl;
56 cerr << endl;
57 cerr << "Usage: " << DHCP6_NAME
58 << " -[v|V|W] [-d|F|R|X] [-{c|t|T} cfgfile] [-p number] [-P number]" << endl;
59 cerr << " -v: print version number and exit" << endl;
60 cerr << " -V: print extended version and exit" << endl;
61 cerr << " -W: display the configuration report and exit" << endl;
62 cerr << " -d: debug mode with extra verbosity (former -v)" << endl;
63 cerr << " -F: exit on critical error" << endl;
64 cerr << " -R: config back end repair mode" << endl;
65 cerr << " -X: disables security restrictions" << endl;
66 cerr << " -c file: specify configuration file" << endl;
67 cerr << " -t file: check the configuration file syntax and exit" << endl;
68 cerr << " -T file: check the configuration file doing hooks load and extra "
69 << "checks and exit" << endl;
70 cerr << " -p number: specify non-standard server port number 1-65535 "
71 << "(useful for testing only)" << endl;
72 cerr << " -P number: specify non-standard client port number 1-65535 "
73 << "(useful for testing only)" << endl;
74 exit(EXIT_FAILURE);
75}
76} // namespace
77
78int
79main(int argc, char* argv[]) {
81
82 int ch;
83 // The default. Any other values are useful for testing only.
84 int server_port_number = DHCP6_SERVER_PORT;
85 // Not zero values are useful for testing only.
86 int client_port_number = 0;
87 bool verbose_mode = false; // Should server be verbose?
88 bool check_mode = false; // Check syntax
89 bool load_hooks = false; // Check hooks config
90
91 // The standard config file
92 std::string config_file("");
93
94 try {
95 // This is the DHCPv6 server
96 IfaceMgr::instance().setFamily(AF_INET6);
97 CfgMgr::instance().setFamily(AF_INET6);
98 } catch (...) {
99 // suppress all errors.
100 }
101
102 optarg = 0;
103 opterr = 0;
104 optind = 1;
105
106 while ((ch = getopt(argc, argv, "vVWdFRXc:t:T:p:P:")) != -1) {
107 switch (ch) {
108 case 'v':
109 cout << Dhcpv6Srv::getVersion(false) << endl;
110 return (EXIT_SUCCESS);
111
112 case 'V':
113 cout << Dhcpv6Srv::getVersion(true) << endl;
114 return (EXIT_SUCCESS);
115
116 case 'W':
117 cout << isc::detail::getConfigReport() << endl;
118 return (EXIT_SUCCESS);
119
120 case 'd':
121 verbose_mode = true;
122 break;
123
124 case 'F': // exit on fatal error
126 break;
127
128 case 'R': // CB repair mode
130 break;
131
132 case 'X': // relax security checks
134 break;
135
136 case 'c': // config file
137 config_file = optarg;
138 break;
139
140 case 't':
141 check_mode = true;
142 config_file = optarg;
143 break;
144
145 case 'T':
146 load_hooks = true;
147 check_mode = true;
148 config_file = optarg;
149 break;
150
151 case 'p': // server port number
152 try {
153 server_port_number = boost::lexical_cast<int>(optarg);
154 } catch (const boost::bad_lexical_cast &) {
155 cerr << "Failed to parse server port number: [" << optarg
156 << "], 1-65535 allowed." << endl;
157 usage();
158 }
159 if (server_port_number <= 0 || server_port_number > 65535) {
160 cerr << "Failed to parse server port number: [" << optarg
161 << "], 1-65535 allowed." << endl;
162 usage();
163 }
164 break;
165
166 case 'P': // client port number
167 try {
168 client_port_number = boost::lexical_cast<int>(optarg);
169 } catch (const boost::bad_lexical_cast &) {
170 cerr << "Failed to parse client port number: [" << optarg
171 << "], 1-65535 allowed." << endl;
172 usage();
173 }
174 if (client_port_number <= 0 || client_port_number > 65535) {
175 cerr << "Failed to parse client port number: [" << optarg
176 << "], 1-65535 allowed." << endl;
177 usage();
178 }
179 break;
180
181 default:
182 usage();
183 }
184 }
185
186 // Check for extraneous parameters.
187 if (argc > optind) {
188 usage();
189 }
190
191 // Configuration file is required.
192 if (config_file.empty()) {
193 cerr << "Configuration file not specified." << endl;
194 usage();
195 }
196
197 if (check_mode) {
198 try {
199 // We need to initialize logging, in case any error messages are to be printed.
200 // This is just a test, so we don't care about lockfile.
201 setenv("KEA_LOCKFILE_DIR", "none", 0);
204
205 // Check the syntax first.
206 Parser6Context parser;
207 ConstElementPtr json;
208 json = parser.parseFile(config_file, Parser6Context::PARSER_DHCP6);
209 if (!json) {
210 cerr << "No configuration found" << endl;
211 return (EXIT_FAILURE);
212 }
213 if (verbose_mode) {
214 cerr << "Syntax check OK" << endl;
215 }
216
217 // Check the logic next.
218 ConstElementPtr dhcp6 = json->get("Dhcp6");
219 if (!dhcp6) {
220 cerr << "Missing mandatory Dhcp6 element" << endl;
221 return (EXIT_FAILURE);
222 }
223 ControlledDhcpv6Srv server(0);
224 ConstElementPtr answer;
225
226 server.setProcName(DHCP6_NAME);
227
228 // Now we pass the Dhcp6 configuration to the server, but
229 // tell it to check the configuration only (check_only = true)
230 answer = configureDhcp6Server(server, dhcp6, true, load_hooks);
231
232 int status_code = 0;
233 answer = isc::config::parseAnswer(status_code, answer);
234 if (status_code == 0) {
235 return (EXIT_SUCCESS);
236 } else {
237 cerr << "Error encountered: " << answer->stringValue() << endl;
238 return (EXIT_FAILURE);
239 }
240 } catch (const std::exception& ex) {
241 cerr << "Syntax check failed with: " << ex.what() << endl;
242 }
243 return (EXIT_FAILURE);
244 }
245
246 int ret = EXIT_SUCCESS;
247 try {
248 // It is important that we set a default logger name because this name
249 // will be used when the user doesn't provide the logging configuration
250 // in the Kea configuration file.
252
253 // Initialize logging. If verbose, we'll use maximum verbosity.
256 .arg(getpid())
257 .arg(server_port_number)
258 .arg(client_port_number)
259 .arg(verbose_mode ? "yes" : "no");
260
262 .arg(VERSION)
263 .arg(PACKAGE_VERSION_TYPE);
264
265 if (string(PACKAGE_VERSION_TYPE) == "development") {
267 }
268
269 if (amRunningAsRoot()) {
271 }
272
275 }
276
279 }
280
281 // Create the server instance.
282 ControlledDhcpv6Srv server(server_port_number, client_port_number);
283
284 // Remember verbose-mode
285 server.setVerbose(verbose_mode);
286
287 // Create our PID file.
288 server.setProcName(DHCP6_NAME);
289 server.setConfigFile(config_file);
290 server.createPIDFile();
291
292 try {
293 // Initialize the server.
294 server.init(config_file);
295 } catch (const std::exception& ex) {
296
297 // Let's log out what went wrong.
298 try {
299 // Log with the current logger, but only if it's not
300 // configured with console output so as to not log twice.
302 LOG_ERROR(dhcp6_logger, DHCP6_INIT_FAIL).arg(ex.what());
303 }
304
305 // Log on the console as well.
306 isc::log::LoggerManager log_manager;
307 log_manager.process();
308 LOG_ERROR(dhcp6_logger, DHCP6_INIT_FAIL).arg(ex.what());
309 } catch (...) {
310 // The exception thrown during the initialization could
311 // originate from logger subsystem. Therefore LOG_ERROR()
312 // may fail as well.
313 cerr << "Failed to initialize server: " << ex.what() << endl;
314 }
315
316 return (EXIT_FAILURE);
317 }
318
319 // Tell the admin we are ready to process packets
320 LOG_INFO(dhcp6_logger, DHCP6_STARTED).arg(VERSION);
321
322 // And run the main loop of the server.
323 ret = server.run();
324
326
327 } catch (const isc::process::DaemonPIDExists& ex) {
328 // First, we print the error on stderr (that should always work)
329 cerr << DHCP6_NAME << " already running? " << ex.what()
330 << endl;
331
332 // Let's also try to log it using logging system, but we're not
333 // sure if it's usable (the exception may have been thrown from
334 // the logger subsystem)
335 try {
337 .arg(DHCP6_NAME).arg(ex.what());
338 } catch (...) {
339 // Already logged so ignore
340 }
341 ret = EXIT_FAILURE;
342 } catch (const std::exception& ex) {
343 // First, we print the error on stderr (that should always work)
344 cerr << DHCP6_NAME << ": Fatal error during start up: " << ex.what()
345 << endl;
346
347 // Let's also try to log it using logging system, but we're not
348 // sure if it's usable (the exception may have been thrown from
349 // the logger subsystem)
350 try {
352 } catch (...) {
353 // Already logged so ignore
354 }
355 ret = EXIT_FAILURE;
356 } catch (...) {
357 cerr << DHCP6_NAME << ": Fatal error during start up"
358 << endl;
359 ret = EXIT_FAILURE;
360 }
361
362 return (ret);
363}
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
void setFamily(uint16_t family)
Sets address family (AF_INET or AF_INET6).
Definition cfgmgr.h:241
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:29
Controlled version of the DHCPv6 server.
void init(const std::string &config_file)
Initializes the server.
int run()
Main server processing loop.
Definition dhcp6_srv.cc:659
static std::string getVersion(bool extended)
returns Kea version on stdout and exit.
void setFamily(uint16_t family)
Sets address family (AF_INET or AF_INET6).
Definition iface_mgr.h:1517
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition iface_mgr.cc:52
Evaluation context, an interface to the expression evaluation.
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
@ PARSER_DHCP6
This parser will parse the content as Dhcp6 config wrapped in a map (that's the regular config file).
void process(T start, T finish)
Process Specifications.
Exception thrown when the PID file points to a live PID.
Definition daemon.h:25
static void setShutdownOnFailure(bool shutdown)
Set the shutdown on critical failure flag.
Definition daemon.h:275
static bool getCBRepairMode()
Get the CB repair mode flag.
Definition daemon.h:282
static void setVerbose(const bool verbose)
Sets or clears verbose mode.
Definition daemon.cc:84
static void setCBRepairMode(bool cb_repair_mode)
Set the CB repair mode flag.
Definition daemon.h:289
static void loggerInit(const char *log_name, bool verbose)
Initializes logger.
Definition daemon.cc:93
static void setDefaultLoggerName(const std::string &logger)
Sets the default logger name.
Definition daemon.h:230
static void setProcName(const std::string &proc_name)
Sets the process name.
Definition daemon.cc:161
void createPIDFile(int pid=0)
Creates the PID file.
Definition daemon.cc:238
void setConfigFile(const std::string &config_file)
Sets the configuration file name.
Definition daemon.cc:114
static bool shouldEnforceSecurity()
Indicates security checks should be enforced.
static void enableEnforcement(bool enable)
Enables or disables security enforcement checks.
int main(int argc, char *argv[])
Definition dhcp6/main.cc:79
void usage()
Print Usage.
Logging initialization functions.
#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_FATAL(LOGGER, MESSAGE)
Macro to conveniently test fatal output and log it.
Definition macros.h:38
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
ConstElementPtr parseAnswer(int &rcode, const ConstElementPtr &msg)
Parses a standard config/command level answer and returns arguments or text status code.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
std::string getConfigReport()
Definition cfgrpt.cc:20
const isc::log::MessageID DHCP6_CB_REPAIR_MODE_ENABLED
isc::data::ConstElementPtr configureDhcp6Server(Dhcpv6Srv &server, isc::data::ConstElementPtr config_set, bool check_only, bool extra_checks)
Configure DHCPv6 server (Dhcpv6Srv) with a set of configuration values.
const isc::log::MessageID DHCP6_INIT_FAIL
const int DBG_DHCP6_START
Debug level used to log information during server startup.
Definition dhcp6_log.h:22
const isc::log::MessageID DHCP6_SERVER_FAILED
const isc::log::MessageID DHCP6_DEVELOPMENT_VERSION
const isc::log::MessageID DHCP6_SHUTDOWN
const isc::log::MessageID DHCP6_STARTED
const isc::log::MessageID DHCP6_STARTING
const isc::log::MessageID DHCP6_START_INFO
const isc::log::MessageID DHCP6_SECURITY_CHECKS_DISABLED
const isc::log::MessageID DHCP6_ALREADY_RUNNING
const isc::log::MessageID DHCP6_ROOT_USER_SECURITY_WARNING
const char * DHCP6_ROOT_LOGGER_NAME
Defines the name of the root level (default) logger.
Definition dhcp6_log.cc:26
isc::log::Logger dhcp6_logger(DHCP6_APP_LOGGER_NAME)
Base logger for DHCPv6 server.
Definition dhcp6_log.h:88
bool amRunningAsRoot()
Indicates if current user is root.
void setUmask()
Set umask (at least 0027 i.e. no group write and no other access).
Definition filesystem.cc:98