]> git.ipfire.org Git - thirdparty/dhcp.git/blob - omapip/isclib.c
[master]
[thirdparty/dhcp.git] / omapip / isclib.c
1 /*
2 * Copyright(c) 2009-2010,2013 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
33 dhcp_context_t dhcp_gbl_ctx;
34
35 void
36 isclib_cleanup(void)
37 {
38 #if defined (NSUPDATE)
39 if (dhcp_gbl_ctx.dnsclient != NULL)
40 dns_client_destroy((dns_client_t **)&dhcp_gbl_ctx.dnsclient);
41 #endif
42
43 if (dhcp_gbl_ctx.task != NULL) {
44 isc_task_shutdown(dhcp_gbl_ctx.task);
45 isc_task_detach(&dhcp_gbl_ctx.task);
46 }
47
48 if (dhcp_gbl_ctx.timermgr != NULL)
49 isc_timermgr_destroy(&dhcp_gbl_ctx.timermgr);
50
51 if (dhcp_gbl_ctx.socketmgr != NULL)
52 isc_socketmgr_destroy(&dhcp_gbl_ctx.socketmgr);
53
54 if (dhcp_gbl_ctx.taskmgr != NULL)
55 isc_taskmgr_destroy(&dhcp_gbl_ctx.taskmgr);
56
57 if (dhcp_gbl_ctx.actx_started != ISC_FALSE) {
58 isc_app_ctxfinish(dhcp_gbl_ctx.actx);
59 dhcp_gbl_ctx.actx_started = ISC_FALSE;
60 }
61
62 if (dhcp_gbl_ctx.actx != NULL)
63 isc_appctx_destroy(&dhcp_gbl_ctx.actx);
64
65 if (dhcp_gbl_ctx.mctx != NULL)
66 isc_mem_detach(&dhcp_gbl_ctx.mctx);
67
68 return;
69 }
70
71 isc_result_t
72 dhcp_context_create(void) {
73 isc_result_t result;
74
75 /*
76 * Set up the error messages, this isn't the right place
77 * for this call but it is convienent for now.
78 */
79 result = dhcp_result_register();
80 if (result != ISC_R_SUCCESS) {
81 log_fatal("register_table() %s: %u", "failed", result);
82 }
83
84 memset(&dhcp_gbl_ctx, 0, sizeof (dhcp_gbl_ctx));
85
86 isc_lib_register();
87
88 /* get the current time for use as the random seed */
89 gettimeofday(&cur_tv, (struct timezone *)0);
90 isc_random_seed(cur_tv.tv_sec);
91
92 #if defined (NSUPDATE)
93 result = dns_lib_init();
94 if (result != ISC_R_SUCCESS)
95 goto cleanup;
96 #endif
97
98 result = isc_mem_create(0, 0, &dhcp_gbl_ctx.mctx);
99 if (result != ISC_R_SUCCESS)
100 goto cleanup;
101
102 result = isc_appctx_create(dhcp_gbl_ctx.mctx, &dhcp_gbl_ctx.actx);
103 if (result != ISC_R_SUCCESS)
104 goto cleanup;
105
106 result = isc_app_ctxstart(dhcp_gbl_ctx.actx);
107 if (result != ISC_R_SUCCESS)
108 return (result);
109 dhcp_gbl_ctx.actx_started = ISC_TRUE;
110
111 result = isc_taskmgr_createinctx(dhcp_gbl_ctx.mctx,
112 dhcp_gbl_ctx.actx,
113 1, 0,
114 &dhcp_gbl_ctx.taskmgr);
115 if (result != ISC_R_SUCCESS)
116 goto cleanup;
117
118 result = isc_socketmgr_createinctx(dhcp_gbl_ctx.mctx,
119 dhcp_gbl_ctx.actx,
120 &dhcp_gbl_ctx.socketmgr);
121 if (result != ISC_R_SUCCESS)
122 goto cleanup;
123
124 result = isc_timermgr_createinctx(dhcp_gbl_ctx.mctx,
125 dhcp_gbl_ctx.actx,
126 &dhcp_gbl_ctx.timermgr);
127 if (result != ISC_R_SUCCESS)
128 goto cleanup;
129
130 result = isc_task_create(dhcp_gbl_ctx.taskmgr, 0, &dhcp_gbl_ctx.task);
131 if (result != ISC_R_SUCCESS)
132 goto cleanup;
133
134 #if defined (NSUPDATE)
135 result = dns_client_createx(dhcp_gbl_ctx.mctx,
136 dhcp_gbl_ctx.actx,
137 dhcp_gbl_ctx.taskmgr,
138 dhcp_gbl_ctx.socketmgr,
139 dhcp_gbl_ctx.timermgr,
140 0,
141 &dhcp_gbl_ctx.dnsclient);
142 if (result != ISC_R_SUCCESS)
143 goto cleanup;
144 #else
145 /* The dst library is inited as part of dns_lib_init, we don't
146 * need it if NSUPDATE is enabled */
147 result = dst_lib_init(dhcp_gbl_ctx.mctx, NULL, 0);
148 if (result != ISC_R_SUCCESS)
149 goto cleanup;
150
151 #endif
152 return(ISC_R_SUCCESS);
153
154 cleanup:
155 /*
156 * Currently we don't try and cleanup, just return an error
157 * expecting that our caller will log the error and exit.
158 */
159
160 return(result);
161 }
162
163 /*
164 * Convert a string name into the proper structure for the isc routines
165 *
166 * Previously we allowed names without a trailing '.' however the current
167 * dns and dst code requires the names to end in a period. If the
168 * name doesn't have a trailing period add one as part of creating
169 * the dns name.
170 */
171
172 isc_result_t
173 dhcp_isc_name(unsigned char *namestr,
174 dns_fixedname_t *namefix,
175 dns_name_t **name)
176 {
177 size_t namelen;
178 isc_buffer_t b;
179 isc_result_t result;
180
181 namelen = strlen((char *)namestr);
182 isc_buffer_init(&b, namestr, namelen);
183 isc_buffer_add(&b, namelen);
184 dns_fixedname_init(namefix);
185 *name = dns_fixedname_name(namefix);
186 result = dns_name_fromtext(*name, &b, dns_rootname, 0, NULL);
187 isc_buffer_invalidate(&b);
188 return(result);
189 }
190
191 isc_result_t
192 isclib_make_dst_key(char *inname,
193 char *algorithm,
194 unsigned char *secret,
195 int length,
196 dst_key_t **dstkey)
197 {
198 isc_result_t result;
199 dns_name_t *name;
200 dns_fixedname_t name0;
201 isc_buffer_t b;
202
203 isc_buffer_init(&b, secret, length);
204 isc_buffer_add(&b, length);
205
206 /* We only support HMAC_MD5 currently */
207 if (strcasecmp(algorithm, DHCP_HMAC_MD5_NAME) != 0) {
208 return(DHCP_R_INVALIDARG);
209 }
210
211 result = dhcp_isc_name((unsigned char *)inname, &name0, &name);
212 if (result != ISC_R_SUCCESS) {
213 return(result);
214 }
215
216 return(dst_key_frombuffer(name, DST_ALG_HMACMD5, DNS_KEYOWNER_ENTITY,
217 DNS_KEYPROTO_DNSSEC, dns_rdataclass_in,
218 &b, dhcp_gbl_ctx.mctx, dstkey));
219 }
220
221 /**
222 * signal handler that initiates server shutdown
223 *
224 * @param signal signal code that we received
225 */
226 void dhcp_signal_handler(int signal) {
227 isc_appctx_t *ctx = dhcp_gbl_ctx.actx;
228 if (ctx && ctx->methods && ctx->methods->ctxshutdown) {
229 /*
230 * Let's not use standard log facilities here. They may not be
231 * signal safe, e.g. we could get the signal in the middle of
232 * another log call
233 */
234 printf("Received signal %d, initiating shutdown.\n", signal);
235 ctx->methods->ctxshutdown(ctx);
236 }
237 }