Coverage Report

Created: 2026-02-14 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/lib/addns/dnsrecord.c
Line
Count
Source
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>
5
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
9
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.
14
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.
19
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/>.
22
*/
23
24
#include "dns.h"
25
#include "lib/util/genrand.h"
26
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 )
30
0
{
31
0
  struct dns_request *req = NULL;
32
0
  struct dns_question *q = NULL;
33
0
  DNS_ERROR err;
34
35
0
  if (!(req = talloc_zero(mem_ctx, struct dns_request)) ||
36
0
      !(req->questions = talloc_array(req, struct dns_question *, 1)) ||
37
0
      !(req->questions[0] = talloc(req->questions,
38
0
           struct dns_question))) {
39
0
    TALLOC_FREE(req);
40
0
    return ERROR_DNS_NO_MEMORY;
41
0
  }
42
43
0
  generate_random_buffer((uint8_t *)&req->id, sizeof(req->id));
44
45
0
  req->num_questions = 1;
46
0
  q = req->questions[0];
47
48
0
  err = dns_domain_name_from_string(q, name, &q->name);
49
0
  if (!ERR_DNS_IS_OK(err)) {
50
0
    TALLOC_FREE(req);
51
0
    return err;
52
0
  }
53
54
0
  q->q_type = q_type;
55
0
  q->q_class = q_class;
56
57
0
  *preq = req;
58
0
  return ERROR_DNS_SUCCESS;
59
0
}
60
61
DNS_ERROR dns_create_update( TALLOC_CTX *mem_ctx, const char *name,
62
           struct dns_update_request **preq )
63
0
{
64
0
  struct dns_update_request *req = NULL;
65
0
  struct dns_zone *z = NULL;
66
0
  DNS_ERROR err;
67
68
0
  if (!(req = talloc_zero(mem_ctx, struct dns_update_request)) ||
69
0
      !(req->zones = talloc_array(req, struct dns_zone *, 1)) ||
70
0
      !(req->zones[0] = talloc(req->zones, struct dns_zone))) {
71
0
    TALLOC_FREE(req);
72
0
    return ERROR_DNS_NO_MEMORY;
73
0
  }
74
75
0
  req->id = random();
76
0
  req->flags = 0x2800;  /* Dynamic update */
77
78
0
  req->num_zones = 1;
79
0
  z = req->zones[0];
80
81
0
  err = dns_domain_name_from_string(z, name, &z->name);
82
0
  if (!ERR_DNS_IS_OK(err)) {
83
0
    TALLOC_FREE(req);
84
0
    return err;
85
0
  }
86
87
0
  z->z_type = QTYPE_SOA;
88
0
  z->z_class = DNS_CLASS_IN;
89
90
0
  *preq = req;
91
0
  return ERROR_DNS_SUCCESS;
92
0
}
93
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)
98
0
{
99
0
  struct dns_rrec *rec = NULL;
100
0
  DNS_ERROR err;
101
102
0
  if (!(rec = talloc(mem_ctx, struct dns_rrec))) {
103
0
    return ERROR_DNS_NO_MEMORY;
104
0
  }
105
106
0
  err = dns_domain_name_from_string(rec, name, &rec->name);
107
0
  if (!(ERR_DNS_IS_OK(err))) {
108
0
    TALLOC_FREE(rec);
109
0
    return err;
110
0
  }
111
112
0
  rec->type = type;
113
0
  rec->r_class = r_class;
114
0
  rec->ttl = ttl;
115
0
  rec->data_length = data_length;
116
0
  rec->data = talloc_move(rec, &data);
117
118
0
  *prec = rec;
119
0
  return ERROR_DNS_SUCCESS;
120
0
}
121
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)
125
0
{
126
0
  uint8_t *data;
127
0
  DNS_ERROR err;
128
0
  struct in_addr ip;
129
130
0
  if (pss->ss_family != AF_INET) {
131
0
    return ERROR_DNS_INVALID_PARAMETER;
132
0
  }
133
134
0
  ip = ((const struct sockaddr_in *)pss)->sin_addr;
135
0
  if (!(data = (uint8_t *)talloc_memdup(mem_ctx, (const void *)&ip.s_addr,
136
0
              sizeof(ip.s_addr)))) {
137
0
    return ERROR_DNS_NO_MEMORY;
138
0
  }
139
140
0
  err = dns_create_rrec(mem_ctx, host, QTYPE_A, DNS_CLASS_IN, ttl,
141
0
            sizeof(ip.s_addr), data, prec);
142
143
0
  if (!ERR_DNS_IS_OK(err)) {
144
0
    TALLOC_FREE(data);
145
0
  }
146
147
0
  return err;
148
0
}
149
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)
153
0
{
154
0
#ifdef HAVE_IPV6
155
0
  uint8_t *data;
156
0
  DNS_ERROR err;
157
0
  struct in6_addr ip6;
158
159
0
  if (pss->ss_family != AF_INET6) {
160
0
    return ERROR_DNS_INVALID_PARAMETER;
161
0
  }
162
163
0
  ip6 = ((const struct sockaddr_in6 *)pss)->sin6_addr;
164
0
  if (!(data = (uint8_t *)talloc_memdup(mem_ctx, (const void *)&ip6.s6_addr,
165
0
              sizeof(ip6.s6_addr)))) {
166
0
    return ERROR_DNS_NO_MEMORY;
167
0
  }
168
169
0
  err = dns_create_rrec(mem_ctx, host, QTYPE_AAAA, DNS_CLASS_IN, ttl,
170
0
            sizeof(ip6.s6_addr), data, prec);
171
172
0
  if (!ERR_DNS_IS_OK(err)) {
173
0
    TALLOC_FREE(data);
174
0
  }
175
176
0
  return err;
177
#else
178
  return ERROR_DNS_INVALID_PARAMETER;
179
#endif
180
0
}
181
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)
187
0
{
188
0
  if (ss != NULL) {
189
0
    switch (ss->ss_family) {
190
0
    case AF_INET:
191
0
      return dns_create_a_record(mem_ctx, name, 0, ss, prec);
192
0
#ifdef HAVE_IPV6
193
0
    case AF_INET6:
194
0
      return dns_create_aaaa_record(mem_ctx, name, 0, ss, prec);
195
0
#endif
196
0
    default:
197
0
      return ERROR_DNS_INVALID_PARAMETER;
198
0
    }
199
0
  }
200
201
0
  return dns_create_rrec(mem_ctx, name, QTYPE_ANY, DNS_CLASS_IN, 0, 0,
202
0
             NULL, prec);
203
0
}
204
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)
209
0
{
210
0
  return dns_create_rrec(mem_ctx, name, type, DNS_CLASS_NONE, 0,
211
0
             0, NULL, prec);
212
0
}
213
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)
219
0
{
220
0
  return dns_create_rrec(mem_ctx, name, type, r_class, 0, 0, NULL, prec);
221
0
}
222
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)
228
0
{
229
0
  struct dns_buffer *buf = NULL;
230
0
  struct dns_domain_name *algorithm = NULL;
231
0
  DNS_ERROR err;
232
233
0
  if (!(buf = dns_create_buffer(mem_ctx))) {
234
0
    return ERROR_DNS_NO_MEMORY;
235
0
  }
236
237
0
  err = dns_domain_name_from_string(buf, algorithm_name, &algorithm);
238
0
  if (!ERR_DNS_IS_OK(err)) goto error;
239
240
0
  dns_marshall_domain_name(buf, algorithm);
241
0
  dns_marshall_uint32(buf, inception);
242
0
  dns_marshall_uint32(buf, expiration);
243
0
  dns_marshall_uint16(buf, mode);
244
0
  dns_marshall_uint16(buf, error);
245
0
  dns_marshall_uint16(buf, key_length);
246
0
  dns_marshall_buffer(buf, key, key_length);
247
0
  dns_marshall_uint16(buf, 0); /* Other Size */
248
249
0
  if (!ERR_DNS_IS_OK(buf->error)) {
250
0
    err = buf->error;
251
0
    goto error;
252
0
  }
253
254
0
  err = dns_create_rrec(mem_ctx, keyname, QTYPE_TKEY, DNS_CLASS_ANY, 0,
255
0
            buf->offset, buf->data, prec);
256
257
0
 error:
258
0
  TALLOC_FREE(buf);
259
0
  return err;
260
0
}
261
262
DNS_ERROR dns_unmarshall_tkey_record(TALLOC_CTX *mem_ctx, struct dns_rrec *rec,
263
             struct dns_tkey_record **ptkey)
264
0
{
265
0
  struct dns_tkey_record *tkey;
266
0
  struct dns_buffer buf;
267
0
  uint32_t tmp_inception, tmp_expiration;
268
  
269
0
  if (!(tkey = talloc(mem_ctx, struct dns_tkey_record))) {
270
0
    return ERROR_DNS_NO_MEMORY;
271
0
  }
272
273
0
  buf.data = rec->data;
274
0
  buf.size = rec->data_length;
275
0
  buf.offset = 0;
276
0
  buf.error = ERROR_DNS_SUCCESS;
277
278
0
  dns_unmarshall_domain_name(tkey, &buf, &tkey->algorithm);
279
0
  dns_unmarshall_uint32(&buf, &tmp_inception);
280
0
  dns_unmarshall_uint32(&buf, &tmp_expiration);
281
0
  dns_unmarshall_uint16(&buf, &tkey->mode);
282
0
  dns_unmarshall_uint16(&buf, &tkey->error);
283
0
  dns_unmarshall_uint16(&buf, &tkey->key_length);
284
285
0
  if (!ERR_DNS_IS_OK(buf.error)) goto error;
286
287
0
  if (tkey->key_length) {
288
0
    if (!(tkey->key = talloc_array(tkey, uint8_t, tkey->key_length))) {
289
0
      buf.error = ERROR_DNS_NO_MEMORY;
290
0
      goto error;
291
0
    }
292
0
  } else {
293
0
    tkey->key = NULL;
294
0
  }
295
296
0
  dns_unmarshall_buffer(&buf, tkey->key, tkey->key_length);
297
0
  if (!ERR_DNS_IS_OK(buf.error)) goto error;
298
299
0
  tkey->inception = (time_t)tmp_inception;
300
0
  tkey->expiration = (time_t)tmp_expiration;
301
302
0
  *ptkey = tkey;
303
0
  return ERROR_DNS_SUCCESS;
304
305
0
 error:
306
0
  TALLOC_FREE(tkey);
307
0
  return buf.error;
308
0
}
309
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)
316
0
{
317
0
  struct dns_buffer *buf = NULL;
318
0
  struct dns_domain_name *algorithm = NULL;
319
0
  DNS_ERROR err;
320
321
0
  if (!(buf = dns_create_buffer(mem_ctx))) {
322
0
    return ERROR_DNS_NO_MEMORY;
323
0
  }
324
325
0
  err = dns_domain_name_from_string(buf, algorithm_name, &algorithm);
326
0
  if (!ERR_DNS_IS_OK(err)) goto error;
327
328
0
  dns_marshall_domain_name(buf, algorithm);
329
0
  dns_marshall_uint16(buf, 0); /* time prefix */
330
0
  dns_marshall_uint32(buf, time_signed);
331
0
  dns_marshall_uint16(buf, fudge);
332
0
  dns_marshall_uint16(buf, mac_length);
333
0
  dns_marshall_buffer(buf, mac, mac_length);
334
0
  dns_marshall_uint16(buf, original_id);
335
0
  dns_marshall_uint16(buf, error);
336
0
  dns_marshall_uint16(buf, 0); /* Other Size */
337
338
0
  if (!ERR_DNS_IS_OK(buf->error)) {
339
0
    err = buf->error;
340
0
    goto error;
341
0
  }
342
343
0
  err = dns_create_rrec(mem_ctx, keyname, QTYPE_TSIG, DNS_CLASS_ANY, 0,
344
0
            buf->offset, buf->data, prec);
345
346
0
 error:
347
0
  TALLOC_FREE(buf);
348
0
  return err;
349
0
}
350
351
DNS_ERROR dns_add_rrec(TALLOC_CTX *mem_ctx, struct dns_rrec *rec,
352
           uint16_t *num_records, struct dns_rrec ***records)
353
0
{
354
0
  struct dns_rrec **new_records;
355
356
0
  if (!(new_records = talloc_realloc(mem_ctx, *records,
357
0
             struct dns_rrec *,
358
0
             (*num_records)+1))) {
359
0
    return ERROR_DNS_NO_MEMORY;
360
0
  }
361
362
0
  new_records[*num_records] = talloc_move(new_records, &rec);
363
364
0
  *num_records += 1;
365
0
  *records = new_records;
366
0
  return ERROR_DNS_SUCCESS;
367
0
}
368
369
/*
370
 * Create a request that probes a server whether the list of IP addresses
371
 * provides meets our expectations
372
 */
373
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)
378
0
{
379
0
  struct dns_update_request *req = NULL;
380
0
  struct dns_rrec *rec = NULL;
381
0
  DNS_ERROR err;
382
0
  uint16_t i;
383
384
0
  err = dns_create_update(mem_ctx, zone, &req);
385
0
  if (!ERR_DNS_IS_OK(err)) return err;
386
387
0
  err = dns_create_name_not_in_use_record(req, host, QTYPE_CNAME, &rec);
388
0
  if (!ERR_DNS_IS_OK(err)) goto error;
389
390
0
  err = dns_add_rrec(req, rec, &req->num_preqs, &req->preqs);
391
0
  if (!ERR_DNS_IS_OK(err)) goto error;
392
393
0
  for (i=0; i<num_ips; i++) {
394
0
    err = dns_create_name_in_use_record(req, host,
395
0
                &sslist[i], &rec);
396
0
    if (!ERR_DNS_IS_OK(err)) goto error;
397
398
0
    err = dns_add_rrec(req, rec, &req->num_preqs, &req->preqs);
399
0
    if (!ERR_DNS_IS_OK(err)) goto error;
400
0
  }
401
402
0
  *preq = req;
403
0
  return ERROR_DNS_SUCCESS;
404
405
0
 error:
406
0
  TALLOC_FREE(req);
407
0
  return err;
408
0
}
409
         
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)
417
0
{
418
0
  struct dns_update_request *req = NULL;
419
0
  struct dns_rrec *rec = NULL;
420
0
  DNS_ERROR err;
421
0
  size_t i;
422
423
0
  err = dns_create_update(mem_ctx, domainname, &req);
424
0
  if (!ERR_DNS_IS_OK(err)) return err;
425
426
  /*
427
   * Use the same prereq as WinXP -- No CNAME records for this host.
428
   */
429
430
0
  err = dns_create_rrec(req, hostname, QTYPE_CNAME, DNS_CLASS_NONE,
431
0
            0, 0, NULL, &rec);
432
0
  if (!ERR_DNS_IS_OK(err)) goto error;
433
434
0
  err = dns_add_rrec(req, rec, &req->num_preqs, &req->preqs);
435
0
  if (!ERR_DNS_IS_OK(err)) goto error;
436
437
  /*
438
   * Delete all existing RRsets from our name
439
   */
440
441
0
  err = dns_create_delete_record(req, hostname, QTYPE_ANY, DNS_CLASS_ANY,
442
0
               &rec);
443
0
  if (!ERR_DNS_IS_OK(err)) goto error;
444
445
0
  err = dns_add_rrec(req, rec, &req->num_updates, &req->updates);
446
0
  if (!ERR_DNS_IS_OK(err)) goto error;
447
448
  /*
449
   * .. and add our IPs
450
   */
451
452
0
  for ( i=0; i<num_addrs; i++ ) {
453
454
0
    switch(ss_addrs[i].ss_family) {
455
0
    case AF_INET:
456
0
      err = dns_create_a_record(req,
457
0
              hostname,
458
0
              ttl,
459
0
              &ss_addrs[i],
460
0
              &rec);
461
0
      break;
462
0
#ifdef HAVE_IPV6
463
0
    case AF_INET6:
464
0
      err = dns_create_aaaa_record(req,
465
0
                 hostname,
466
0
                 ttl,
467
0
                 &ss_addrs[i],
468
0
                 &rec);
469
0
      break;
470
0
#endif
471
0
    default:
472
0
      continue;
473
0
    }
474
0
    if (!ERR_DNS_IS_OK(err))
475
0
      goto error;
476
477
0
    err = dns_add_rrec(req, rec, &req->num_updates, &req->updates);
478
0
    if (!ERR_DNS_IS_OK(err))
479
0
      goto error;
480
0
  }
481
482
0
  *preq = req;
483
0
  return ERROR_DNS_SUCCESS;
484
485
0
 error:
486
  TALLOC_FREE(req);
487
0
  return err;
488
0
}