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