2 * Copyright(c) 2009 by Internet Systems Consortium, Inc.("ISC")
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.
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.
16 * Internet Systems Consortium, Inc.
18 * Redwood City, CA 94063
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 */
30 dhcp_context_t dhcp_gbl_ctx
;
35 #if defined (NSUPDATE)
36 if (dhcp_gbl_ctx
.dnsclient
!= NULL
)
37 dns_client_destroy((dns_client_t
**)&dhcp_gbl_ctx
.dnsclient
);
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
);
46 if (dhcp_gbl_ctx
.timermgr
!= NULL
)
47 isc_timermgr_destroy(&dhcp_gbl_ctx
.timermgr
);
49 if (dhcp_gbl_ctx
.socketmgr
!= NULL
)
50 isc_socketmgr_destroy(&dhcp_gbl_ctx
.socketmgr
);
52 if (dhcp_gbl_ctx
.taskmgr
!= NULL
)
53 isc_taskmgr_destroy(&dhcp_gbl_ctx
.taskmgr
);
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
;
60 if (dhcp_gbl_ctx
.actx
!= NULL
)
61 isc_appctx_destroy(&dhcp_gbl_ctx
.actx
);
63 if (dhcp_gbl_ctx
.mctx
!= NULL
)
64 isc_mem_detach(&dhcp_gbl_ctx
.mctx
);
70 dhcp_context_create(void) {
74 * Set up the error messages, this isn't the right place
75 * for this call but it is convienent for now.
77 result
= dhcp_result_register();
78 if (result
!= ISC_R_SUCCESS
) {
79 log_fatal("register_table() %s: %u", "failed", result
);
82 memset(&dhcp_gbl_ctx
, 0, sizeof (dhcp_gbl_ctx
));
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
);
90 #if defined (NSUPDATE)
91 result
= dns_lib_init();
92 if (result
!= ISC_R_SUCCESS
)
96 result
= isc_mem_create(0, 0, &dhcp_gbl_ctx
.mctx
);
97 if (result
!= ISC_R_SUCCESS
)
100 result
= isc_appctx_create(dhcp_gbl_ctx
.mctx
, &dhcp_gbl_ctx
.actx
);
101 if (result
!= ISC_R_SUCCESS
)
104 result
= isc_app_ctxstart(dhcp_gbl_ctx
.actx
);
105 if (result
!= ISC_R_SUCCESS
)
107 dhcp_gbl_ctx
.actx_started
= ISC_TRUE
;
109 result
= isc_taskmgr_createinctx(dhcp_gbl_ctx
.mctx
,
112 &dhcp_gbl_ctx
.taskmgr
);
113 if (result
!= ISC_R_SUCCESS
)
116 result
= isc_socketmgr_createinctx(dhcp_gbl_ctx
.mctx
,
118 &dhcp_gbl_ctx
.socketmgr
);
119 if (result
!= ISC_R_SUCCESS
)
122 result
= isc_timermgr_createinctx(dhcp_gbl_ctx
.mctx
,
124 &dhcp_gbl_ctx
.timermgr
);
125 if (result
!= ISC_R_SUCCESS
)
128 result
= isc_task_create(dhcp_gbl_ctx
.taskmgr
, 0, &dhcp_gbl_ctx
.task
);
129 if (result
!= ISC_R_SUCCESS
)
132 #if defined (NSUPDATE)
133 result
= dns_client_createx(dhcp_gbl_ctx
.mctx
,
135 dhcp_gbl_ctx
.taskmgr
,
136 dhcp_gbl_ctx
.socketmgr
,
137 dhcp_gbl_ctx
.timermgr
,
139 &dhcp_gbl_ctx
.dnsclient
);
140 if (result
!= ISC_R_SUCCESS
)
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
)
150 return(ISC_R_SUCCESS
);
158 /* Convert a string name into the proper structure for the isc routines */
160 dhcp_isc_name(unsigned char *namestr
,
161 dns_fixedname_t
*namefix
,
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
);
179 isclib_make_dst_key(char *inname
,
181 unsigned char *secret
,
187 dns_fixedname_t name0
;
191 isc_buffer_init(&b
, secret
, length
);
192 isc_buffer_add(&b
, length
);
194 /* We only support HMAC_MD5 currently */
195 if (strcasecmp(algorithm
, DHCP_HMAC_MD5_NAME
) != 0) {
196 return(DHCP_R_INVALIDARG
);
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.
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
);
213 strcpy(newname
, inname
);
214 newname
[namelen
] = '.';
215 newname
[namelen
+1] = 0;
216 result
= dhcp_isc_name((unsigned char *)newname
, &name0
, &name
);
219 result
= dhcp_isc_name((unsigned char *)inname
, &name0
, &name
);
222 if (result
!= ISC_R_SUCCESS
) {
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
));