ISC DHCP  4.3.6b1
A reference DHCPv4 and DHCPv6 implementation
isclib.c
Go to the documentation of this file.
1 /*
2  * Copyright(c) 2009-2017 by Internet Systems Consortium, Inc.("ISC")
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  *
16  * Internet Systems Consortium, Inc.
17  * 950 Charter Street
18  * Redwood City, CA 94063
19  * <info@isc.org>
20  * http://www.isc.org/
21  *
22  */
23 
24 /*Trying to figure out what we need to define to get things to work.
25  It looks like we want/need the export library but need the fdwatchcommand
26  which may be a problem */
27 
28 #include "dhcpd.h"
29 
30 #include <sys/time.h>
31 #include <signal.h>
32 
35 
36 #if defined (NSUPDATE)
37 
38 /* This routine will open up the /etc/resolv.conf file and
39  * send any nameservers it finds to the DNS client code.
40  * It may be moved to be part of the dns client code instead
41  * of being in the DHCP code
42  */
43 isc_result_t
44 dhcp_dns_client_setservers(void)
45 {
46  isc_result_t result;
47  irs_resconf_t *resconf = NULL;
48  isc_sockaddrlist_t *nameservers;
49  isc_sockaddr_t *sa;
50 
51  result = irs_resconf_load(dhcp_gbl_ctx.mctx, _PATH_RESOLV_CONF,
52  &resconf);
53  if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
54  log_error("irs_resconf_load failed: %d.", result);
55  return (result);
56  }
57 
58  nameservers = irs_resconf_getnameservers(resconf);
59 
60  /* Initialize port numbers */
61  for (sa = ISC_LIST_HEAD(*nameservers);
62  sa != NULL;
63  sa = ISC_LIST_NEXT(sa, link)) {
64  switch (sa->type.sa.sa_family) {
65  case AF_INET:
66  sa->type.sin.sin_port = htons(NS_DEFAULTPORT);
67  break;
68  case AF_INET6:
69  sa->type.sin6.sin6_port = htons(NS_DEFAULTPORT);
70  break;
71  default:
72  break;
73  }
74  }
75 
76  result = dns_client_setservers(dhcp_gbl_ctx.dnsclient,
77  dns_rdataclass_in,
78  NULL, nameservers);
79  if (result != ISC_R_SUCCESS) {
80  log_error("dns_client_setservers failed: %d.",
81  result);
82  }
83  return (result);
84 }
85 #endif
86 
87 void
89 {
90 #if defined (NSUPDATE)
91  if (dhcp_gbl_ctx.dnsclient != NULL)
92  dns_client_destroy((dns_client_t **)&dhcp_gbl_ctx.dnsclient);
93 #endif
94 
95  if (dhcp_gbl_ctx.task != NULL) {
96  isc_task_shutdown(dhcp_gbl_ctx.task);
97  isc_task_detach(&dhcp_gbl_ctx.task);
98  }
99 
100  if (dhcp_gbl_ctx.timermgr != NULL)
101  isc_timermgr_destroy(&dhcp_gbl_ctx.timermgr);
102 
103  if (dhcp_gbl_ctx.socketmgr != NULL)
104  isc_socketmgr_destroy(&dhcp_gbl_ctx.socketmgr);
105 
106  if (dhcp_gbl_ctx.taskmgr != NULL)
107  isc_taskmgr_destroy(&dhcp_gbl_ctx.taskmgr);
108 
109  if (dhcp_gbl_ctx.actx_started != ISC_FALSE) {
110  isc_app_ctxfinish(dhcp_gbl_ctx.actx);
111  dhcp_gbl_ctx.actx_started = ISC_FALSE;
112  }
113 
114  if (dhcp_gbl_ctx.actx != NULL)
115  isc_appctx_destroy(&dhcp_gbl_ctx.actx);
116 
117  if (dhcp_gbl_ctx.mctx != NULL)
118  isc_mem_detach(&dhcp_gbl_ctx.mctx);
119 
120  return;
121 }
122 
123 /* Installs a handler for a signal using sigaction */
124 static void
125 handle_signal(int sig, void (*handler)(int)) {
126  struct sigaction sa;
127 
128  memset(&sa, 0, sizeof(sa));
129  sa.sa_handler = handler;
130  sigfillset(&sa.sa_mask);
131  if (sigaction(sig, &sa, NULL) != 0) {
132  log_debug("handle_signal() failed for signal %d error: %s",
133  sig, strerror(errno));
134  }
135 }
136 
137 isc_result_t
139  struct in_addr *local4,
140  struct in6_addr *local6) {
141  isc_result_t result;
142 
143  if ((flags & DHCP_CONTEXT_PRE_DB) != 0) {
144  /*
145  * Set up the error messages, this isn't the right place
146  * for this call but it is convienent for now.
147  */
148  result = dhcp_result_register();
149  if (result != ISC_R_SUCCESS) {
150  log_fatal("register_table() %s: %u", "failed", result);
151  }
152 
153  memset(&dhcp_gbl_ctx, 0, sizeof (dhcp_gbl_ctx));
154 
155  isc_lib_register();
156 
157  /* get the current time for use as the random seed */
158  gettimeofday(&cur_tv, (struct timezone *)0);
159  isc_random_seed(cur_tv.tv_sec);
160 
161  /* we need to create the memory context before
162  * the lib inits in case we aren't doing NSUPDATE
163  * in which case dst needs a memory context
164  */
165  result = isc_mem_create(0, 0, &dhcp_gbl_ctx.mctx);
166  if (result != ISC_R_SUCCESS)
167  goto cleanup;
168 
169 
170 #if defined (NSUPDATE)
171  result = dns_lib_init();
172  if (result != ISC_R_SUCCESS)
173  goto cleanup;
174 #else
175  /* The dst library is inited as part of dns_lib_init, we don't
176  * need it if NSUPDATE is enabled */
177  result = dst_lib_init(dhcp_gbl_ctx.mctx, NULL, 0);
178  if (result != ISC_R_SUCCESS)
179  goto cleanup;
180 
181 #endif
182 
183  result = isc_appctx_create(dhcp_gbl_ctx.mctx,
184  &dhcp_gbl_ctx.actx);
185  if (result != ISC_R_SUCCESS)
186  goto cleanup;
187 
188  result = isc_app_ctxstart(dhcp_gbl_ctx.actx);
189  if (result != ISC_R_SUCCESS)
190  return (result);
191  dhcp_gbl_ctx.actx_started = ISC_TRUE;
192 
193  /* Not all OSs support suppressing SIGPIPE through socket
194  * options, so set the sigal action to be ignore. This allows
195  * broken connections to fail gracefully with EPIPE on writes */
196  handle_signal(SIGPIPE, SIG_IGN);
197 
198  result = isc_taskmgr_createinctx(dhcp_gbl_ctx.mctx,
199  dhcp_gbl_ctx.actx,
200  1, 0,
201  &dhcp_gbl_ctx.taskmgr);
202  if (result != ISC_R_SUCCESS)
203  goto cleanup;
204 
205  result = isc_socketmgr_createinctx(dhcp_gbl_ctx.mctx,
206  dhcp_gbl_ctx.actx,
207  &dhcp_gbl_ctx.socketmgr);
208  if (result != ISC_R_SUCCESS)
209  goto cleanup;
210 
211  result = isc_timermgr_createinctx(dhcp_gbl_ctx.mctx,
212  dhcp_gbl_ctx.actx,
213  &dhcp_gbl_ctx.timermgr);
214  if (result != ISC_R_SUCCESS)
215  goto cleanup;
216 
217  result = isc_task_create(dhcp_gbl_ctx.taskmgr, 0, &dhcp_gbl_ctx.task);
218  if (result != ISC_R_SUCCESS)
219  goto cleanup;
220  }
221 
222 #if defined (NSUPDATE)
223  if ((flags & DHCP_CONTEXT_POST_DB) != 0) {
224  /* Setting addresses only.
225  * All real work will be done later on if needed to avoid
226  * listening on ddns port if client/server was compiled with
227  * ddns support but not using it. */
228  if (local4 != NULL) {
229  dhcp_gbl_ctx.use_local4 = 1;
230  isc_sockaddr_fromin(&dhcp_gbl_ctx.local4_sockaddr,
231  local4, 0);
232  }
233 
234  if (local6 != NULL) {
235  dhcp_gbl_ctx.use_local6 = 1;
236  isc_sockaddr_fromin6(&dhcp_gbl_ctx.local6_sockaddr,
237  local6, 0);
238  }
239 
240  if (!(flags & DHCP_DNS_CLIENT_LAZY_INIT)) {
241  result = dns_client_init();
242  }
243  }
244 #endif
245 
246  return(ISC_R_SUCCESS);
247 
248  cleanup:
249  /*
250  * Currently we don't try and cleanup, just return an error
251  * expecting that our caller will log the error and exit.
252  */
253 
254  return(result);
255 }
256 
257 /*
258  * Convert a string name into the proper structure for the isc routines
259  *
260  * Previously we allowed names without a trailing '.' however the current
261  * dns and dst code requires the names to end in a period. If the
262  * name doesn't have a trailing period add one as part of creating
263  * the dns name.
264  */
265 
266 isc_result_t
267 dhcp_isc_name(unsigned char *namestr,
268  dns_fixedname_t *namefix,
269  dns_name_t **name)
270 {
271  size_t namelen;
272  isc_buffer_t b;
273  isc_result_t result;
274 
275  namelen = strlen((char *)namestr);
276  isc_buffer_init(&b, namestr, namelen);
277  isc_buffer_add(&b, namelen);
278  dns_fixedname_init(namefix);
279  *name = dns_fixedname_name(namefix);
280  result = dns_name_fromtext(*name, &b, dns_rootname, 0, NULL);
281  isc_buffer_invalidate(&b);
282  return(result);
283 }
284 
285 isc_result_t
286 isclib_make_dst_key(char *inname,
287  char *algorithm,
288  unsigned char *secret,
289  int length,
290  dst_key_t **dstkey)
291 {
292  isc_result_t result;
293  dns_name_t *name;
294  dns_fixedname_t name0;
295  isc_buffer_t b;
296  unsigned int algorithm_code;
297 
298  isc_buffer_init(&b, secret, length);
299  isc_buffer_add(&b, length);
300 
301  if (strcasecmp(algorithm, DHCP_HMAC_MD5_NAME) == 0) {
302  algorithm_code = DST_ALG_HMACMD5;
303  } else if (strcasecmp(algorithm, DHCP_HMAC_SHA1_NAME) == 0) {
304  algorithm_code = DST_ALG_HMACSHA1;
305  } else if (strcasecmp(algorithm, DHCP_HMAC_SHA224_NAME) == 0) {
306  algorithm_code = DST_ALG_HMACSHA224;
307  } else if (strcasecmp(algorithm, DHCP_HMAC_SHA256_NAME) == 0) {
308  algorithm_code = DST_ALG_HMACSHA256;
309  } else if (strcasecmp(algorithm, DHCP_HMAC_SHA384_NAME) == 0) {
310  algorithm_code = DST_ALG_HMACSHA384;
311  } else if (strcasecmp(algorithm, DHCP_HMAC_SHA512_NAME) == 0) {
312  algorithm_code = DST_ALG_HMACSHA512;
313  } else {
314  return(DHCP_R_INVALIDARG);
315  }
316 
317  result = dhcp_isc_name((unsigned char *)inname, &name0, &name);
318  if (result != ISC_R_SUCCESS) {
319  return(result);
320  }
321 
322  return(dst_key_frombuffer(name, algorithm_code, DNS_KEYOWNER_ENTITY,
323  DNS_KEYPROTO_DNSSEC, dns_rdataclass_in,
324  &b, dhcp_gbl_ctx.mctx, dstkey));
325 }
326 
332 void dhcp_signal_handler(int signal) {
333  isc_appctx_t *ctx = dhcp_gbl_ctx.actx;
334  int prev = shutdown_signal;
335 
336  if (prev != 0) {
337  /* Already in shutdown. */
338  return;
339  }
340  /* Possible race but does it matter? */
341  shutdown_signal = signal;
342 
343  /* Use reload (aka suspend) for easier dispatch() reenter. */
344  if (ctx && ctx->methods && ctx->methods->ctxsuspend) {
345  (void) isc_app_ctxsuspend(ctx);
346  }
347 }
348 
349 isc_result_t dns_client_init() {
350  isc_result_t result;
351  if (dhcp_gbl_ctx.dnsclient == NULL) {
352  result = dns_client_createx2(dhcp_gbl_ctx.mctx,
353  dhcp_gbl_ctx.actx,
354  dhcp_gbl_ctx.taskmgr,
355  dhcp_gbl_ctx.socketmgr,
356  dhcp_gbl_ctx.timermgr,
357  0,
358  &dhcp_gbl_ctx.dnsclient,
359  (dhcp_gbl_ctx.use_local4 ?
360  &dhcp_gbl_ctx.local4_sockaddr
361  : NULL),
362  (dhcp_gbl_ctx.use_local6 ?
363  &dhcp_gbl_ctx.local6_sockaddr
364  : NULL));
365 
366  if (result != ISC_R_SUCCESS) {
367  log_error("Unable to create DNS client context:"
368  " result: %d", result);
369  return result;
370  }
371 
372  /* If we can't set up the servers we may not be able to
373  * do DDNS but we should continue to try and perform
374  * our basic functions and let the user sort it out. */
375  result = dhcp_dns_client_setservers();
376  if (result != ISC_R_SUCCESS) {
377  log_error("Unable to set resolver from resolv.conf; "
378  "startup continuing but DDNS support "
379  "may be affected: result %d", result);
380  }
381  }
382 
383  return ISC_R_SUCCESS;
384 }
#define DHCP_HMAC_SHA512_NAME
Definition: isclib.h:117
isc_result_t dhcp_isc_name(unsigned char *namestr, dns_fixedname_t *namefix, dns_name_t **name)
Definition: isclib.c:267
isc_result_t dhcp_context_create(int flags, struct in_addr *local4, struct in6_addr *local6)
Definition: isclib.c:138
isc_result_t dhcp_result_register(void)
Definition: result.c:79
#define DHCP_R_INVALIDARG
Definition: result.h:48
isc_result_t isclib_make_dst_key(char *inname, char *algorithm, unsigned char *secret, int length, dst_key_t **dstkey)
Definition: isclib.c:286
int int int log_debug(const char *,...) __attribute__((__format__(__printf__
#define DHCP_CONTEXT_PRE_DB
Definition: isclib.h:130
isc_timermgr_t * timermgr
Definition: isclib.h:98
isc_taskmgr_t * taskmgr
Definition: isclib.h:95
int log_error(const char *,...) __attribute__((__format__(__printf__
void dhcp_signal_handler(int signal)
Definition: isclib.c:332
isc_socketmgr_t * socketmgr
Definition: isclib.h:97
void log_fatal(const char *,...) __attribute__((__format__(__printf__
#define DHCP_CONTEXT_POST_DB
Definition: isclib.h:131
#define DHCP_HMAC_SHA1_NAME
Definition: isclib.h:113
isc_mem_t * mctx
Definition: isclib.h:92
isc_result_t dns_client_init()
Definition: isclib.c:349
#define DHCP_DNS_CLIENT_LAZY_INIT
Definition: isclib.h:132
#define NS_DEFAULTPORT
Definition: nameser.h:86
int actx_started
Definition: isclib.h:94
#define DHCP_HMAC_SHA256_NAME
Definition: isclib.h:115
isc_appctx_t * actx
Definition: isclib.h:93
int shutdown_signal
Definition: isclib.c:34
void cleanup(void)
void isclib_cleanup(void)
Definition: isclib.c:88
dhcp_context_t dhcp_gbl_ctx
Definition: isclib.c:33
struct timeval cur_tv
Definition: dispatch.c:35
#define DHCP_HMAC_MD5_NAME
Definition: isclib.h:112
#define DHCP_HMAC_SHA384_NAME
Definition: isclib.h:116
isc_task_t * task
Definition: isclib.h:96
#define DHCP_HMAC_SHA224_NAME
Definition: isclib.h:114
#define _PATH_RESOLV_CONF
Definition: dhcpd.h:1581