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 */
32 dhcp_context_t dhcp_gbl_ctx
;
37 #if defined (NSUPDATE)
38 if (dhcp_gbl_ctx
.dnsclient
!= NULL
)
39 dns_client_destroy((dns_client_t
**)&dhcp_gbl_ctx
.dnsclient
);
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
);
48 if (dhcp_gbl_ctx
.timermgr
!= NULL
)
49 isc_timermgr_destroy(&dhcp_gbl_ctx
.timermgr
);
51 if (dhcp_gbl_ctx
.socketmgr
!= NULL
)
52 isc_socketmgr_destroy(&dhcp_gbl_ctx
.socketmgr
);
54 if (dhcp_gbl_ctx
.taskmgr
!= NULL
)
55 isc_taskmgr_destroy(&dhcp_gbl_ctx
.taskmgr
);
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
;
62 if (dhcp_gbl_ctx
.actx
!= NULL
)
63 isc_appctx_destroy(&dhcp_gbl_ctx
.actx
);
65 if (dhcp_gbl_ctx
.mctx
!= NULL
)
66 isc_mem_detach(&dhcp_gbl_ctx
.mctx
);
72 dhcp_context_create(void) {
76 * Set up the error messages, this isn't the right place
77 * for this call but it is convienent for now.
79 result
= dhcp_result_register();
80 if (result
!= ISC_R_SUCCESS
) {
81 log_fatal("register_table() %s: %u", "failed", result
);
84 memset(&dhcp_gbl_ctx
, 0, sizeof (dhcp_gbl_ctx
));
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
);
92 #if defined (NSUPDATE)
93 result
= dns_lib_init();
94 if (result
!= ISC_R_SUCCESS
)
98 result
= isc_mem_create(0, 0, &dhcp_gbl_ctx
.mctx
);
99 if (result
!= ISC_R_SUCCESS
)
102 result
= isc_appctx_create(dhcp_gbl_ctx
.mctx
, &dhcp_gbl_ctx
.actx
);
103 if (result
!= ISC_R_SUCCESS
)
106 result
= isc_app_ctxstart(dhcp_gbl_ctx
.actx
);
107 if (result
!= ISC_R_SUCCESS
)
109 dhcp_gbl_ctx
.actx_started
= ISC_TRUE
;
111 result
= isc_taskmgr_createinctx(dhcp_gbl_ctx
.mctx
,
114 &dhcp_gbl_ctx
.taskmgr
);
115 if (result
!= ISC_R_SUCCESS
)
118 result
= isc_socketmgr_createinctx(dhcp_gbl_ctx
.mctx
,
120 &dhcp_gbl_ctx
.socketmgr
);
121 if (result
!= ISC_R_SUCCESS
)
124 result
= isc_timermgr_createinctx(dhcp_gbl_ctx
.mctx
,
126 &dhcp_gbl_ctx
.timermgr
);
127 if (result
!= ISC_R_SUCCESS
)
130 result
= isc_task_create(dhcp_gbl_ctx
.taskmgr
, 0, &dhcp_gbl_ctx
.task
);
131 if (result
!= ISC_R_SUCCESS
)
134 #if defined (NSUPDATE)
135 result
= dns_client_createx(dhcp_gbl_ctx
.mctx
,
137 dhcp_gbl_ctx
.taskmgr
,
138 dhcp_gbl_ctx
.socketmgr
,
139 dhcp_gbl_ctx
.timermgr
,
141 &dhcp_gbl_ctx
.dnsclient
);
142 if (result
!= ISC_R_SUCCESS
)
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
)
152 return(ISC_R_SUCCESS
);
160 /* Convert a string name into the proper structure for the isc routines */
162 dhcp_isc_name(unsigned char *namestr
,
163 dns_fixedname_t
*namefix
,
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
);
181 isclib_make_dst_key(char *inname
,
183 unsigned char *secret
,
189 dns_fixedname_t name0
;
193 isc_buffer_init(&b
, secret
, length
);
194 isc_buffer_add(&b
, length
);
196 /* We only support HMAC_MD5 currently */
197 if (strcasecmp(algorithm
, DHCP_HMAC_MD5_NAME
) != 0) {
198 return(DHCP_R_INVALIDARG
);
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.
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
);
215 strcpy(newname
, inname
);
216 newname
[namelen
] = '.';
217 newname
[namelen
+1] = 0;
218 result
= dhcp_isc_name((unsigned char *)newname
, &name0
, &name
);
221 result
= dhcp_isc_name((unsigned char *)inname
, &name0
, &name
);
224 if (result
!= ISC_R_SUCCESS
) {
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
));