]> git.ipfire.org Git - thirdparty/dhcp.git/blob - omapip/isclib.c
Cleanup for ddns changes ticket rt20527
[thirdparty/dhcp.git] / omapip / isclib.c
1 /*
2 * Copyright(c) 2009 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
32 dhcp_context_t dhcp_gbl_ctx;
33
34 void
35 isclib_cleanup(void)
36 {
37 #if defined (NSUPDATE)
38 if (dhcp_gbl_ctx.dnsclient != NULL)
39 dns_client_destroy((dns_client_t **)&dhcp_gbl_ctx.dnsclient);
40 #endif
41
42 if (dhcp_gbl_ctx.task != NULL) {
43 // isc_task_destroy(&dhcp_gbl_ctx.task);
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 isclib_cleanup();
156
157 return(result);
158 }
159
160 /* Convert a string name into the proper structure for the isc routines */
161 isc_result_t
162 dhcp_isc_name(unsigned char *namestr,
163 dns_fixedname_t *namefix,
164 dns_name_t **name)
165 {
166 size_t namelen;
167 isc_buffer_t b;
168 isc_result_t result;
169
170 namelen = strlen((char *)namestr);
171 isc_buffer_init(&b, namestr, namelen);
172 isc_buffer_add(&b, namelen);
173 dns_fixedname_init(namefix);
174 *name = dns_fixedname_name(namefix);
175 result = dns_name_fromtext(*name, &b, NULL, 0, NULL);
176 isc_buffer_invalidate(&b);
177 return(result);
178 }
179
180 isc_result_t
181 isclib_make_dst_key(char *inname,
182 char *algorithm,
183 unsigned char *secret,
184 int length,
185 dst_key_t **dstkey)
186 {
187 isc_result_t result;
188 dns_name_t *name;
189 dns_fixedname_t name0;
190 isc_buffer_t b;
191 int namelen;
192
193 isc_buffer_init(&b, secret, length);
194 isc_buffer_add(&b, length);
195
196 /* We only support HMAC_MD5 currently */
197 if (strcasecmp(algorithm, DHCP_HMAC_MD5_NAME) != 0) {
198 return(DHCP_R_INVALIDARG);
199 }
200
201 /*
202 * Previously we allowed key names without a trailing '.'
203 * however the current dst code requires the names to end
204 * in a period. If the name doesn't have a trailing period
205 * add one before sending it to the dst code.
206 */
207 namelen = strlen(inname);
208 if (inname[namelen-1] != '.') {
209 char *newname = NULL;
210 newname = (char *)dmalloc(namelen + 2, MDL);
211 if (newname == NULL) {
212 log_error("unable to allocate memory for key name");
213 return(ISC_R_NOMEMORY);
214 }
215 strcpy(newname, inname);
216 newname[namelen] = '.';
217 newname[namelen+1] = 0;
218 result = dhcp_isc_name((unsigned char *)newname, &name0, &name);
219 dfree(newname, MDL);
220 } else {
221 result = dhcp_isc_name((unsigned char *)inname, &name0, &name);
222 }
223
224 if (result != ISC_R_SUCCESS) {
225 return(result);
226 }
227
228 return(dst_key_frombuffer(name, DST_ALG_HMACMD5, DNS_KEYOWNER_ENTITY,
229 DNS_KEYPROTO_DNSSEC, dns_rdataclass_in,
230 &b, dhcp_gbl_ctx.mctx, dstkey));
231 }
232