Kea 3.1.8
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|X] [-d] [-{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 << " -c file: specify configuration file" << endl;
64 cerr << " -t file: check the configuration file syntax and exit" << endl;
65 cerr << " -T file: check the configuration file doing hooks load and extra "
66 << "checks and exit" << endl;
67 cerr << " -p number: specify non-standard server port number 1-65535 "
68 << "(useful for testing only)" << endl;
69 cerr << " -P number: specify non-standard client port number 1-65535 "
70 << "(useful for testing only)" << endl;
71 cerr << " -X: disables security restrictions" << endl;
72 exit(EXIT_FAILURE);
73}
74} // namespace
75
76int
77main(int argc, char* argv[]) {
79
80 int ch;
81 // The default. Any other values are useful for testing only.
82 int server_port_number = DHCP6_SERVER_PORT;
83 // Not zero values are useful for testing only.
84 int client_port_number = 0;
85 bool verbose_mode = false; // Should server be verbose?
86 bool check_mode = false; // Check syntax
87 bool load_hooks = false; // Check hooks config
88
89 // The standard config file
90 std::string config_file("");
91
92 // This is the DHCPv6 server
93 CfgMgr::instance().setFamily(AF_INET6);
94
95 while ((ch = getopt(argc, argv, "dvVWc:p:P:t:T:X")) != -1) {
96 switch (ch) {
97 case 'd':
98 verbose_mode = true;
99 break;
100
101 case 'v':
102 cout << Dhcpv6Srv::getVersion(false) << endl;
103 return (EXIT_SUCCESS);
104
105 case 'V':
106 cout << Dhcpv6Srv::getVersion(true) << endl;
107 return (EXIT_SUCCESS);
108
109 case 'W':
110 cout << isc::detail::getConfigReport() << endl;
111 return (EXIT_SUCCESS);
112
113 case 'T':
114 load_hooks = true;
115 check_mode = true;
116 config_file = optarg;
117 break;
118
119 case 't':
120 check_mode = true;
121 config_file = optarg;
122 break;
123
124 case 'c': // config file
125 config_file = optarg;
126 break;
127
128 case 'p': // server port number
129 try {
130 server_port_number = boost::lexical_cast<int>(optarg);
131 } catch (const boost::bad_lexical_cast &) {
132 cerr << "Failed to parse server port number: [" << optarg
133 << "], 1-65535 allowed." << endl;
134 usage();
135 }
136 if (server_port_number <= 0 || server_port_number > 65535) {
137 cerr << "Failed to parse server port number: [" << optarg
138 << "], 1-65535 allowed." << endl;
139 usage();
140 }
141 break;
142
143 case 'P': // client port number
144 try {
145 client_port_number = boost::lexical_cast<int>(optarg);
146 } catch (const boost::bad_lexical_cast &) {
147 cerr << "Failed to parse client port number: [" << optarg
148 << "], 1-65535 allowed." << endl;
149 usage();
150 }
151 if (client_port_number <= 0 || client_port_number > 65535) {
152 cerr << "Failed to parse client port number: [" << optarg
153 << "], 1-65535 allowed." << endl;
154 usage();
155 }
156 break;
157
158 case 'X': // relax security checks
160 break;
161
162 default:
163 usage();
164 }
165 }
166
167 // Check for extraneous parameters.
168 if (argc > optind) {
169 usage();
170 }
171
172 // Configuration file is required.
173 if (config_file.empty()) {
174 cerr << "Configuration file not specified." << endl;
175 usage();
176 }
177
178 if (check_mode) {
179 try {
180 // We need to initialize logging, in case any error messages are to be printed.
181 // This is just a test, so we don't care about lockfile.
182 setenv("KEA_LOCKFILE_DIR", "none", 0);
185
186 // Check the syntax first.
187 Parser6Context parser;
188 ConstElementPtr json;
189 json = parser.parseFile(config_file, Parser6Context::PARSER_DHCP6);
190 if (!json) {
191 cerr << "No configuration found" << endl;
192 return (EXIT_FAILURE);
193 }
194 if (verbose_mode) {
195 cerr << "Syntax check OK" << endl;
196 }
197
198 // Check the logic next.
199 ConstElementPtr dhcp6 = json->get("Dhcp6");
200 if (!dhcp6) {
201 cerr << "Missing mandatory Dhcp6 element" << endl;
202 return (EXIT_FAILURE);
203 }
204 ControlledDhcpv6Srv server(0);
205 ConstElementPtr answer;
206
207 server.setProcName(DHCP6_NAME);
208
209 // Now we pass the Dhcp6 configuration to the server, but
210 // tell it to check the configuration only (check_only = true)
211 answer = configureDhcp6Server(server, dhcp6, true, load_hooks);
212
213 int status_code = 0;
214 answer = isc::config::parseAnswer(status_code, answer);
215 if (status_code == 0) {
216 return (EXIT_SUCCESS);
217 } else {
218 cerr << "Error encountered: " << answer->stringValue() << endl;
219 return (EXIT_FAILURE);
220 }
221 } catch (const std::exception& ex) {
222 cerr << "Syntax check failed with: " << ex.what() << endl;
223 }
224 return (EXIT_FAILURE);
225 }
226
227 int ret = EXIT_SUCCESS;
228 try {
229 // It is important that we set a default logger name because this name
230 // will be used when the user doesn't provide the logging configuration
231 // in the Kea configuration file.
233
234 // Initialize logging. If verbose, we'll use maximum verbosity.
237 .arg(getpid())
238 .arg(server_port_number)
239 .arg(client_port_number)
240 .arg(verbose_mode ? "yes" : "no");
241
243 .arg(VERSION)
244 .arg(PACKAGE_VERSION_TYPE);
245
246 if (string(PACKAGE_VERSION_TYPE) == "development") {
248 }
249
250 if (amRunningAsRoot()) {
252 }
253
256 }
257
258 // Create the server instance.
259 ControlledDhcpv6Srv server(server_port_number, client_port_number);
260
261 // Remember verbose-mode
262 server.setVerbose(verbose_mode);
263
264 // Create our PID file.
265 server.setProcName(DHCP6_NAME);
266 server.setConfigFile(config_file);
267 server.createPIDFile();
268
269 try {
270 // Initialize the server.
271 server.init(config_file);
272 } catch (const std::exception& ex) {
273
274 // Let's log out what went wrong.
275 try {
276 // Log with the current logger, but only if it's not
277 // configured with console output so as to not log twice.
279 LOG_ERROR(dhcp6_logger, DHCP6_INIT_FAIL).arg(ex.what());
280 }
281
282 // Log on the console as well.
283 isc::log::LoggerManager log_manager;
284 log_manager.process();
285 LOG_ERROR(dhcp6_logger, DHCP6_INIT_FAIL).arg(ex.what());
286 } catch (...) {
287 // The exception thrown during the initialization could
288 // originate from logger subsystem. Therefore LOG_ERROR()
289 // may fail as well.
290 cerr << "Failed to initialize server: " << ex.what() << endl;
291 }
292
293 return (EXIT_FAILURE);
294 }
295
296 // Tell the admin we are ready to process packets
297 LOG_INFO(dhcp6_logger, DHCP6_STARTED).arg(VERSION);
298
299 // And run the main loop of the server.
300 ret = server.run();
301
303
304 } catch (const isc::process::DaemonPIDExists& ex) {
305 // First, we print the error on stderr (that should always work)
306 cerr << DHCP6_NAME << " already running? " << ex.what()
307 << endl;
308
309 // Let's also try to log it using logging system, but we're not
310 // sure if it's usable (the exception may have been thrown from
311 // the logger subsystem)
312 try {
314 .arg(DHCP6_NAME).arg(ex.what());
315 } catch (...) {
316 // Already logged so ignore
317 }
318 ret = EXIT_FAILURE;
319 } catch (const std::exception& ex) {
320 // First, we print the error on stderr (that should always work)
321 cerr << DHCP6_NAME << ": Fatal error during start up: " << ex.what()
322 << endl;
323
324 // Let's also try to log it using logging system, but we're not
325 // sure if it's usable (the exception may have been thrown from
326 // the logger subsystem)
327 try {
329 } catch (...) {
330 // Already logged so ignore
331 }
332 ret = EXIT_FAILURE;
333 } catch (...) {
334 cerr << DHCP6_NAME << ": Fatal error during start up"
335 << endl;
336 ret = EXIT_FAILURE;
337 }
338
339 return (ret);
340}
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:667
static std::string getVersion(bool extended)
returns Kea version on stdout and exit.
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 setVerbose(const bool verbose)
Sets or clears verbose mode.
Definition daemon.cc:82
static void loggerInit(const char *log_name, bool verbose)
Initializes logger.
Definition daemon.cc:91
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:159
void createPIDFile(int pid=0)
Creates the PID file.
Definition daemon.cc:236
void setConfigFile(const std::string &config_file)
Sets the configuration file name.
Definition daemon.cc:112
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:77
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
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