2 Linux DNS client library implementation
3 Copyright (C) 2006 Krishna Ganugapati <krishnag@centeris.com>
4 Copyright (C) 2006 Gerald Carter <jerry@samba.org>
6 ** NOTE! The following LGPL license applies to the libaddns
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "lib/util/genrand.h"
27 DNS_ERROR
dns_create_query( TALLOC_CTX
*mem_ctx
, const char *name
,
28 uint16_t q_type
, uint16_t q_class
,
29 struct dns_request
**preq
)
31 struct dns_request
*req
= NULL
;
32 struct dns_question
*q
= NULL
;
35 if (!(req
= talloc_zero(mem_ctx
, struct dns_request
)) ||
36 !(req
->questions
= talloc_array(req
, struct dns_question
*, 1)) ||
37 !(req
->questions
[0] = talloc(req
->questions
,
38 struct dns_question
))) {
40 return ERROR_DNS_NO_MEMORY
;
43 generate_random_buffer((uint8_t *)&req
->id
, sizeof(req
->id
));
45 req
->num_questions
= 1;
46 q
= req
->questions
[0];
48 err
= dns_domain_name_from_string(q
, name
, &q
->name
);
49 if (!ERR_DNS_IS_OK(err
)) {
58 return ERROR_DNS_SUCCESS
;
61 DNS_ERROR
dns_create_update( TALLOC_CTX
*mem_ctx
, const char *name
,
62 struct dns_update_request
**preq
)
64 struct dns_update_request
*req
= NULL
;
65 struct dns_zone
*z
= NULL
;
68 if (!(req
= talloc_zero(mem_ctx
, struct dns_update_request
)) ||
69 !(req
->zones
= talloc_array(req
, struct dns_zone
*, 1)) ||
70 !(req
->zones
[0] = talloc(req
->zones
, struct dns_zone
))) {
72 return ERROR_DNS_NO_MEMORY
;
76 req
->flags
= 0x2800; /* Dynamic update */
81 err
= dns_domain_name_from_string(z
, name
, &z
->name
);
82 if (!ERR_DNS_IS_OK(err
)) {
87 z
->z_type
= QTYPE_SOA
;
88 z
->z_class
= DNS_CLASS_IN
;
91 return ERROR_DNS_SUCCESS
;
94 DNS_ERROR
dns_create_rrec(TALLOC_CTX
*mem_ctx
, const char *name
,
95 uint16_t type
, uint16_t r_class
, uint32_t ttl
,
96 uint16_t data_length
, uint8_t *data
,
97 struct dns_rrec
**prec
)
99 struct dns_rrec
*rec
= NULL
;
102 if (!(rec
= talloc(mem_ctx
, struct dns_rrec
))) {
103 return ERROR_DNS_NO_MEMORY
;
106 err
= dns_domain_name_from_string(rec
, name
, &rec
->name
);
107 if (!(ERR_DNS_IS_OK(err
))) {
113 rec
->r_class
= r_class
;
115 rec
->data_length
= data_length
;
116 rec
->data
= talloc_move(rec
, &data
);
119 return ERROR_DNS_SUCCESS
;
122 DNS_ERROR
dns_create_a_record(TALLOC_CTX
*mem_ctx
, const char *host
,
123 uint32_t ttl
, const struct sockaddr_storage
*pss
,
124 struct dns_rrec
**prec
)
130 if (pss
->ss_family
!= AF_INET
) {
131 return ERROR_DNS_INVALID_PARAMETER
;
134 ip
= ((const struct sockaddr_in
*)pss
)->sin_addr
;
135 if (!(data
= (uint8_t *)talloc_memdup(mem_ctx
, (const void *)&ip
.s_addr
,
136 sizeof(ip
.s_addr
)))) {
137 return ERROR_DNS_NO_MEMORY
;
140 err
= dns_create_rrec(mem_ctx
, host
, QTYPE_A
, DNS_CLASS_IN
, ttl
,
141 sizeof(ip
.s_addr
), data
, prec
);
143 if (!ERR_DNS_IS_OK(err
)) {
150 DNS_ERROR
dns_create_aaaa_record(TALLOC_CTX
*mem_ctx
, const char *host
,
151 uint32_t ttl
, const struct sockaddr_storage
*pss
,
152 struct dns_rrec
**prec
)
159 if (pss
->ss_family
!= AF_INET6
) {
160 return ERROR_DNS_INVALID_PARAMETER
;
163 ip6
= ((const struct sockaddr_in6
*)pss
)->sin6_addr
;
164 if (!(data
= (uint8_t *)talloc_memdup(mem_ctx
, (const void *)&ip6
.s6_addr
,
165 sizeof(ip6
.s6_addr
)))) {
166 return ERROR_DNS_NO_MEMORY
;
169 err
= dns_create_rrec(mem_ctx
, host
, QTYPE_AAAA
, DNS_CLASS_IN
, ttl
,
170 sizeof(ip6
.s6_addr
), data
, prec
);
172 if (!ERR_DNS_IS_OK(err
)) {
178 return ERROR_DNS_INVALID_PARAMETER
;
182 DNS_ERROR
dns_create_name_in_use_record(TALLOC_CTX
*mem_ctx
,
184 const struct sockaddr_storage
*ss
,
185 struct dns_rrec
**prec
)
188 switch (ss
->ss_family
) {
190 return dns_create_a_record(mem_ctx
, name
, 0, ss
, prec
);
193 return dns_create_aaaa_record(mem_ctx
, name
, 0, ss
, prec
);
196 return ERROR_DNS_INVALID_PARAMETER
;
200 return dns_create_rrec(mem_ctx
, name
, QTYPE_ANY
, DNS_CLASS_IN
, 0, 0,
204 DNS_ERROR
dns_create_name_not_in_use_record(TALLOC_CTX
*mem_ctx
,
205 const char *name
, uint32_t type
,
206 struct dns_rrec
**prec
)
208 return dns_create_rrec(mem_ctx
, name
, type
, DNS_CLASS_NONE
, 0,
212 DNS_ERROR
dns_create_delete_record(TALLOC_CTX
*mem_ctx
, const char *name
,
213 uint16_t type
, uint16_t r_class
,
214 struct dns_rrec
**prec
)
216 return dns_create_rrec(mem_ctx
, name
, type
, r_class
, 0, 0, NULL
, prec
);
219 DNS_ERROR
dns_create_tkey_record(TALLOC_CTX
*mem_ctx
, const char *keyname
,
220 const char *algorithm_name
, time_t inception
,
221 time_t expiration
, uint16_t mode
, uint16_t error
,
222 uint16_t key_length
, const uint8_t *key
,
223 struct dns_rrec
**prec
)
225 struct dns_buffer
*buf
= NULL
;
226 struct dns_domain_name
*algorithm
= NULL
;
229 if (!(buf
= dns_create_buffer(mem_ctx
))) {
230 return ERROR_DNS_NO_MEMORY
;
233 err
= dns_domain_name_from_string(buf
, algorithm_name
, &algorithm
);
234 if (!ERR_DNS_IS_OK(err
)) goto error
;
236 dns_marshall_domain_name(buf
, algorithm
);
237 dns_marshall_uint32(buf
, inception
);
238 dns_marshall_uint32(buf
, expiration
);
239 dns_marshall_uint16(buf
, mode
);
240 dns_marshall_uint16(buf
, error
);
241 dns_marshall_uint16(buf
, key_length
);
242 dns_marshall_buffer(buf
, key
, key_length
);
243 dns_marshall_uint16(buf
, 0); /* Other Size */
245 if (!ERR_DNS_IS_OK(buf
->error
)) {
250 err
= dns_create_rrec(mem_ctx
, keyname
, QTYPE_TKEY
, DNS_CLASS_ANY
, 0,
251 buf
->offset
, buf
->data
, prec
);
258 DNS_ERROR
dns_unmarshall_tkey_record(TALLOC_CTX
*mem_ctx
, struct dns_rrec
*rec
,
259 struct dns_tkey_record
**ptkey
)
261 struct dns_tkey_record
*tkey
;
262 struct dns_buffer buf
;
263 uint32_t tmp_inception
, tmp_expiration
;
265 if (!(tkey
= talloc(mem_ctx
, struct dns_tkey_record
))) {
266 return ERROR_DNS_NO_MEMORY
;
269 buf
.data
= rec
->data
;
270 buf
.size
= rec
->data_length
;
272 buf
.error
= ERROR_DNS_SUCCESS
;
274 dns_unmarshall_domain_name(tkey
, &buf
, &tkey
->algorithm
);
275 dns_unmarshall_uint32(&buf
, &tmp_inception
);
276 dns_unmarshall_uint32(&buf
, &tmp_expiration
);
277 dns_unmarshall_uint16(&buf
, &tkey
->mode
);
278 dns_unmarshall_uint16(&buf
, &tkey
->error
);
279 dns_unmarshall_uint16(&buf
, &tkey
->key_length
);
281 if (!ERR_DNS_IS_OK(buf
.error
)) goto error
;
283 if (tkey
->key_length
) {
284 if (!(tkey
->key
= talloc_array(tkey
, uint8_t, tkey
->key_length
))) {
285 buf
.error
= ERROR_DNS_NO_MEMORY
;
292 dns_unmarshall_buffer(&buf
, tkey
->key
, tkey
->key_length
);
293 if (!ERR_DNS_IS_OK(buf
.error
)) goto error
;
295 tkey
->inception
= (time_t)tmp_inception
;
296 tkey
->expiration
= (time_t)tmp_expiration
;
299 return ERROR_DNS_SUCCESS
;
306 DNS_ERROR
dns_create_tsig_record(TALLOC_CTX
*mem_ctx
, const char *keyname
,
307 const char *algorithm_name
,
308 time_t time_signed
, uint16_t fudge
,
309 uint16_t mac_length
, const uint8_t *mac
,
310 uint16_t original_id
, uint16_t error
,
311 struct dns_rrec
**prec
)
313 struct dns_buffer
*buf
= NULL
;
314 struct dns_domain_name
*algorithm
= NULL
;
317 if (!(buf
= dns_create_buffer(mem_ctx
))) {
318 return ERROR_DNS_NO_MEMORY
;
321 err
= dns_domain_name_from_string(buf
, algorithm_name
, &algorithm
);
322 if (!ERR_DNS_IS_OK(err
)) goto error
;
324 dns_marshall_domain_name(buf
, algorithm
);
325 dns_marshall_uint16(buf
, 0); /* time prefix */
326 dns_marshall_uint32(buf
, time_signed
);
327 dns_marshall_uint16(buf
, fudge
);
328 dns_marshall_uint16(buf
, mac_length
);
329 dns_marshall_buffer(buf
, mac
, mac_length
);
330 dns_marshall_uint16(buf
, original_id
);
331 dns_marshall_uint16(buf
, error
);
332 dns_marshall_uint16(buf
, 0); /* Other Size */
334 if (!ERR_DNS_IS_OK(buf
->error
)) {
339 err
= dns_create_rrec(mem_ctx
, keyname
, QTYPE_TSIG
, DNS_CLASS_ANY
, 0,
340 buf
->offset
, buf
->data
, prec
);
347 DNS_ERROR
dns_add_rrec(TALLOC_CTX
*mem_ctx
, struct dns_rrec
*rec
,
348 uint16_t *num_records
, struct dns_rrec
***records
)
350 struct dns_rrec
**new_records
;
352 if (!(new_records
= talloc_realloc(mem_ctx
, *records
,
354 (*num_records
)+1))) {
355 return ERROR_DNS_NO_MEMORY
;
358 new_records
[*num_records
] = talloc_move(new_records
, &rec
);
361 *records
= new_records
;
362 return ERROR_DNS_SUCCESS
;
366 * Create a request that probes a server whether the list of IP addresses
367 * provides meets our expectations
370 DNS_ERROR
dns_create_probe(TALLOC_CTX
*mem_ctx
, const char *zone
,
371 const char *host
, int num_ips
,
372 const struct sockaddr_storage
*sslist
,
373 struct dns_update_request
**preq
)
375 struct dns_update_request
*req
= NULL
;
376 struct dns_rrec
*rec
= NULL
;
380 err
= dns_create_update(mem_ctx
, zone
, &req
);
381 if (!ERR_DNS_IS_OK(err
)) return err
;
383 err
= dns_create_name_not_in_use_record(req
, host
, QTYPE_CNAME
, &rec
);
384 if (!ERR_DNS_IS_OK(err
)) goto error
;
386 err
= dns_add_rrec(req
, rec
, &req
->num_preqs
, &req
->preqs
);
387 if (!ERR_DNS_IS_OK(err
)) goto error
;
389 for (i
=0; i
<num_ips
; i
++) {
390 err
= dns_create_name_in_use_record(req
, host
,
392 if (!ERR_DNS_IS_OK(err
)) goto error
;
394 err
= dns_add_rrec(req
, rec
, &req
->num_preqs
, &req
->preqs
);
395 if (!ERR_DNS_IS_OK(err
)) goto error
;
399 return ERROR_DNS_SUCCESS
;
406 DNS_ERROR
dns_create_update_request(TALLOC_CTX
*mem_ctx
,
407 const char *domainname
,
408 const char *hostname
,
409 const struct sockaddr_storage
*ss_addrs
,
412 struct dns_update_request
**preq
)
414 struct dns_update_request
*req
= NULL
;
415 struct dns_rrec
*rec
= NULL
;
419 err
= dns_create_update(mem_ctx
, domainname
, &req
);
420 if (!ERR_DNS_IS_OK(err
)) return err
;
423 * Use the same prereq as WinXP -- No CNAME records for this host.
426 err
= dns_create_rrec(req
, hostname
, QTYPE_CNAME
, DNS_CLASS_NONE
,
428 if (!ERR_DNS_IS_OK(err
)) goto error
;
430 err
= dns_add_rrec(req
, rec
, &req
->num_preqs
, &req
->preqs
);
431 if (!ERR_DNS_IS_OK(err
)) goto error
;
434 * Delete all existing RRsets from our name
437 err
= dns_create_delete_record(req
, hostname
, QTYPE_ANY
, DNS_CLASS_ANY
,
439 if (!ERR_DNS_IS_OK(err
)) goto error
;
441 err
= dns_add_rrec(req
, rec
, &req
->num_updates
, &req
->updates
);
442 if (!ERR_DNS_IS_OK(err
)) goto error
;
448 for ( i
=0; i
<num_addrs
; i
++ ) {
450 switch(ss_addrs
[i
].ss_family
) {
452 err
= dns_create_a_record(req
,
460 err
= dns_create_aaaa_record(req
,
470 if (!ERR_DNS_IS_OK(err
))
473 err
= dns_add_rrec(req
, rec
, &req
->num_updates
, &req
->updates
);
474 if (!ERR_DNS_IS_OK(err
))
479 return ERROR_DNS_SUCCESS
;