drsuapi.idl: fix source_dsa spelling
[samba4-gss.git] / lib / addns / dnsrecord.c
blob3c5a81c9c4c3e141d6c33c8cee9d0847af9bc22f
1 /*
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
8 ** under the LGPL
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/>.
24 #include "dns.h"
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;
33 DNS_ERROR err;
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))) {
39 TALLOC_FREE(req);
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)) {
50 TALLOC_FREE(req);
51 return err;
54 q->q_type = q_type;
55 q->q_class = q_class;
57 *preq = req;
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;
66 DNS_ERROR err;
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))) {
71 TALLOC_FREE(req);
72 return ERROR_DNS_NO_MEMORY;
75 req->id = random();
76 req->flags = 0x2800; /* Dynamic update */
78 req->num_zones = 1;
79 z = req->zones[0];
81 err = dns_domain_name_from_string(z, name, &z->name);
82 if (!ERR_DNS_IS_OK(err)) {
83 TALLOC_FREE(req);
84 return err;
87 z->z_type = QTYPE_SOA;
88 z->z_class = DNS_CLASS_IN;
90 *preq = req;
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;
100 DNS_ERROR err;
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))) {
108 TALLOC_FREE(rec);
109 return err;
112 rec->type = type;
113 rec->r_class = r_class;
114 rec->ttl = ttl;
115 rec->data_length = data_length;
116 rec->data = talloc_move(rec, &data);
118 *prec = rec;
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)
126 uint8_t *data;
127 DNS_ERROR err;
128 struct in_addr ip;
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)) {
144 TALLOC_FREE(data);
147 return 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)
154 #ifdef HAVE_IPV6
155 uint8_t *data;
156 DNS_ERROR err;
157 struct in6_addr ip6;
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)) {
173 TALLOC_FREE(data);
176 return err;
177 #else
178 return ERROR_DNS_INVALID_PARAMETER;
179 #endif
182 static DNS_ERROR dns_create_name_in_use_record(
183 TALLOC_CTX *mem_ctx,
184 const char *name,
185 const struct sockaddr_storage *ss,
186 struct dns_rrec **prec)
188 if (ss != NULL) {
189 switch (ss->ss_family) {
190 case AF_INET:
191 return dns_create_a_record(mem_ctx, name, 0, ss, prec);
192 #ifdef HAVE_IPV6
193 case AF_INET6:
194 return dns_create_aaaa_record(mem_ctx, name, 0, ss, prec);
195 #endif
196 default:
197 return ERROR_DNS_INVALID_PARAMETER;
201 return dns_create_rrec(mem_ctx, name, QTYPE_ANY, DNS_CLASS_IN, 0, 0,
202 NULL, prec);
205 static DNS_ERROR dns_create_name_not_in_use_record(TALLOC_CTX *mem_ctx,
206 const char *name,
207 uint32_t type,
208 struct dns_rrec **prec)
210 return dns_create_rrec(mem_ctx, name, type, DNS_CLASS_NONE, 0,
211 0, NULL, prec);
214 static DNS_ERROR dns_create_delete_record(TALLOC_CTX *mem_ctx,
215 const char *name,
216 uint16_t type,
217 uint16_t r_class,
218 struct dns_rrec **prec)
220 return dns_create_rrec(mem_ctx, name, type, r_class, 0, 0, NULL, prec);
223 DNS_ERROR dns_create_tkey_record(TALLOC_CTX *mem_ctx, const char *keyname,
224 const char *algorithm_name, time_t inception,
225 time_t expiration, uint16_t mode, uint16_t error,
226 uint16_t key_length, const uint8_t *key,
227 struct dns_rrec **prec)
229 struct dns_buffer *buf = NULL;
230 struct dns_domain_name *algorithm = NULL;
231 DNS_ERROR err;
233 if (!(buf = dns_create_buffer(mem_ctx))) {
234 return ERROR_DNS_NO_MEMORY;
237 err = dns_domain_name_from_string(buf, algorithm_name, &algorithm);
238 if (!ERR_DNS_IS_OK(err)) goto error;
240 dns_marshall_domain_name(buf, algorithm);
241 dns_marshall_uint32(buf, inception);
242 dns_marshall_uint32(buf, expiration);
243 dns_marshall_uint16(buf, mode);
244 dns_marshall_uint16(buf, error);
245 dns_marshall_uint16(buf, key_length);
246 dns_marshall_buffer(buf, key, key_length);
247 dns_marshall_uint16(buf, 0); /* Other Size */
249 if (!ERR_DNS_IS_OK(buf->error)) {
250 err = buf->error;
251 goto error;
254 err = dns_create_rrec(mem_ctx, keyname, QTYPE_TKEY, DNS_CLASS_ANY, 0,
255 buf->offset, buf->data, prec);
257 error:
258 TALLOC_FREE(buf);
259 return err;
262 DNS_ERROR dns_unmarshall_tkey_record(TALLOC_CTX *mem_ctx, struct dns_rrec *rec,
263 struct dns_tkey_record **ptkey)
265 struct dns_tkey_record *tkey;
266 struct dns_buffer buf;
267 uint32_t tmp_inception, tmp_expiration;
269 if (!(tkey = talloc(mem_ctx, struct dns_tkey_record))) {
270 return ERROR_DNS_NO_MEMORY;
273 buf.data = rec->data;
274 buf.size = rec->data_length;
275 buf.offset = 0;
276 buf.error = ERROR_DNS_SUCCESS;
278 dns_unmarshall_domain_name(tkey, &buf, &tkey->algorithm);
279 dns_unmarshall_uint32(&buf, &tmp_inception);
280 dns_unmarshall_uint32(&buf, &tmp_expiration);
281 dns_unmarshall_uint16(&buf, &tkey->mode);
282 dns_unmarshall_uint16(&buf, &tkey->error);
283 dns_unmarshall_uint16(&buf, &tkey->key_length);
285 if (!ERR_DNS_IS_OK(buf.error)) goto error;
287 if (tkey->key_length) {
288 if (!(tkey->key = talloc_array(tkey, uint8_t, tkey->key_length))) {
289 buf.error = ERROR_DNS_NO_MEMORY;
290 goto error;
292 } else {
293 tkey->key = NULL;
296 dns_unmarshall_buffer(&buf, tkey->key, tkey->key_length);
297 if (!ERR_DNS_IS_OK(buf.error)) goto error;
299 tkey->inception = (time_t)tmp_inception;
300 tkey->expiration = (time_t)tmp_expiration;
302 *ptkey = tkey;
303 return ERROR_DNS_SUCCESS;
305 error:
306 TALLOC_FREE(tkey);
307 return buf.error;
310 DNS_ERROR dns_create_tsig_record(TALLOC_CTX *mem_ctx, const char *keyname,
311 const char *algorithm_name,
312 time_t time_signed, uint16_t fudge,
313 uint16_t mac_length, const uint8_t *mac,
314 uint16_t original_id, uint16_t error,
315 struct dns_rrec **prec)
317 struct dns_buffer *buf = NULL;
318 struct dns_domain_name *algorithm = NULL;
319 DNS_ERROR err;
321 if (!(buf = dns_create_buffer(mem_ctx))) {
322 return ERROR_DNS_NO_MEMORY;
325 err = dns_domain_name_from_string(buf, algorithm_name, &algorithm);
326 if (!ERR_DNS_IS_OK(err)) goto error;
328 dns_marshall_domain_name(buf, algorithm);
329 dns_marshall_uint16(buf, 0); /* time prefix */
330 dns_marshall_uint32(buf, time_signed);
331 dns_marshall_uint16(buf, fudge);
332 dns_marshall_uint16(buf, mac_length);
333 dns_marshall_buffer(buf, mac, mac_length);
334 dns_marshall_uint16(buf, original_id);
335 dns_marshall_uint16(buf, error);
336 dns_marshall_uint16(buf, 0); /* Other Size */
338 if (!ERR_DNS_IS_OK(buf->error)) {
339 err = buf->error;
340 goto error;
343 err = dns_create_rrec(mem_ctx, keyname, QTYPE_TSIG, DNS_CLASS_ANY, 0,
344 buf->offset, buf->data, prec);
346 error:
347 TALLOC_FREE(buf);
348 return err;
351 DNS_ERROR dns_add_rrec(TALLOC_CTX *mem_ctx, struct dns_rrec *rec,
352 uint16_t *num_records, struct dns_rrec ***records)
354 struct dns_rrec **new_records;
356 if (!(new_records = talloc_realloc(mem_ctx, *records,
357 struct dns_rrec *,
358 (*num_records)+1))) {
359 return ERROR_DNS_NO_MEMORY;
362 new_records[*num_records] = talloc_move(new_records, &rec);
364 *num_records += 1;
365 *records = new_records;
366 return ERROR_DNS_SUCCESS;
370 * Create a request that probes a server whether the list of IP addresses
371 * provides meets our expectations
374 DNS_ERROR dns_create_probe(TALLOC_CTX *mem_ctx, const char *zone,
375 const char *host, int num_ips,
376 const struct sockaddr_storage *sslist,
377 struct dns_update_request **preq)
379 struct dns_update_request *req = NULL;
380 struct dns_rrec *rec = NULL;
381 DNS_ERROR err;
382 uint16_t i;
384 err = dns_create_update(mem_ctx, zone, &req);
385 if (!ERR_DNS_IS_OK(err)) return err;
387 err = dns_create_name_not_in_use_record(req, host, QTYPE_CNAME, &rec);
388 if (!ERR_DNS_IS_OK(err)) goto error;
390 err = dns_add_rrec(req, rec, &req->num_preqs, &req->preqs);
391 if (!ERR_DNS_IS_OK(err)) goto error;
393 for (i=0; i<num_ips; i++) {
394 err = dns_create_name_in_use_record(req, host,
395 &sslist[i], &rec);
396 if (!ERR_DNS_IS_OK(err)) goto error;
398 err = dns_add_rrec(req, rec, &req->num_preqs, &req->preqs);
399 if (!ERR_DNS_IS_OK(err)) goto error;
402 *preq = req;
403 return ERROR_DNS_SUCCESS;
405 error:
406 TALLOC_FREE(req);
407 return err;
410 DNS_ERROR dns_create_update_request(TALLOC_CTX *mem_ctx,
411 const char *domainname,
412 const char *hostname,
413 const struct sockaddr_storage *ss_addrs,
414 size_t num_addrs,
415 uint32_t ttl,
416 struct dns_update_request **preq)
418 struct dns_update_request *req = NULL;
419 struct dns_rrec *rec = NULL;
420 DNS_ERROR err;
421 size_t i;
423 err = dns_create_update(mem_ctx, domainname, &req);
424 if (!ERR_DNS_IS_OK(err)) return err;
427 * Use the same prereq as WinXP -- No CNAME records for this host.
430 err = dns_create_rrec(req, hostname, QTYPE_CNAME, DNS_CLASS_NONE,
431 0, 0, NULL, &rec);
432 if (!ERR_DNS_IS_OK(err)) goto error;
434 err = dns_add_rrec(req, rec, &req->num_preqs, &req->preqs);
435 if (!ERR_DNS_IS_OK(err)) goto error;
438 * Delete all existing RRsets from our name
441 err = dns_create_delete_record(req, hostname, QTYPE_ANY, DNS_CLASS_ANY,
442 &rec);
443 if (!ERR_DNS_IS_OK(err)) goto error;
445 err = dns_add_rrec(req, rec, &req->num_updates, &req->updates);
446 if (!ERR_DNS_IS_OK(err)) goto error;
449 * .. and add our IPs
452 for ( i=0; i<num_addrs; i++ ) {
454 switch(ss_addrs[i].ss_family) {
455 case AF_INET:
456 err = dns_create_a_record(req,
457 hostname,
458 ttl,
459 &ss_addrs[i],
460 &rec);
461 break;
462 #ifdef HAVE_IPV6
463 case AF_INET6:
464 err = dns_create_aaaa_record(req,
465 hostname,
466 ttl,
467 &ss_addrs[i],
468 &rec);
469 break;
470 #endif
471 default:
472 continue;
474 if (!ERR_DNS_IS_OK(err))
475 goto error;
477 err = dns_add_rrec(req, rec, &req->num_updates, &req->updates);
478 if (!ERR_DNS_IS_OK(err))
479 goto error;
482 *preq = req;
483 return ERROR_DNS_SUCCESS;
485 error:
486 TALLOC_FREE(req);
487 return err;