2 * Copyright (c) 1996 by Internet Software Consortium.
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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
17 * $FreeBSD: src/lib/libc/net/res_update.c,v 1.2.2.2 2002/09/19 13:45:23 nectar Exp $
18 * $DragonFly: src/lib/libc/net/res_update.c,v 1.3 2005/11/13 02:04:47 swildner Exp $
22 * Based on the Dynamic DNS reference implementation by Viraj Bais
23 * <viraj_bais@ccm.fm.intel.com>
26 #include <sys/param.h>
27 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <arpa/nameser.h>
40 * Separate a linked list of records into groups so that all records
41 * in a group will belong to a single zone on the nameserver.
42 * Create a dynamic update packet for each zone and send it to the
43 * nameservers for that zone, and await answer.
44 * Abort if error occurs in updating any zone.
45 * Return the number of zones updated on success, < 0 on error.
47 * On error, caller must deal with the unsynchronized zones
48 * eg. an A record might have been successfully added to the forward
49 * zone but the corresponding PTR record would be missing if error
50 * was encountered while updating the reverse zone.
56 char nsname[MAXDNAME];
57 struct in_addr nsaddr1;
61 char z_origin[MAXDNAME];
63 char z_soardata[MAXDNAME + 5 * INT32SZ];
64 struct ns1 z_ns[NSMAX];
67 struct zonegrp *z_next;
72 res_update(ns_updrec *rrecp_in) {
73 ns_updrec *rrecp, *tmprrecp;
74 u_char buf[PACKETSZ], answer[PACKETSZ], packet[2*PACKETSZ];
75 char name[MAXDNAME], zname[MAXDNAME], primary[MAXDNAME],
77 u_char soardata[2*MAXCDNAME+5*INT32SZ];
78 char *dname, *svdname, *cp1, *target;
80 HEADER *hp = (HEADER *) answer;
81 struct zonegrp *zptr = NULL, *tmpzptr, *prevzptr, *zgrp_start = NULL;
82 int i, j, k = 0, n, ancount, nscount, arcount, rcode, rdatasize,
83 newgroup, done, myzone, seen_before, numzones = 0;
84 u_int16_t dlen, class, qclass, type, qtype;
87 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
88 h_errno = NETDB_INTERNAL;
92 for (rrecp = rrecp_in; rrecp; rrecp = rrecp->r_next) {
93 dname = rrecp->r_dname;
95 if (dname[n-1] == '.')
98 qclass = rrecp->r_class;
102 while (!done && dname) {
103 if (qtype == T_SOA) {
104 for (tmpzptr = zgrp_start;
105 tmpzptr && !seen_before;
106 tmpzptr = tmpzptr->z_next) {
107 if (strcasecmp(dname,
108 tmpzptr->z_origin) == 0 &&
109 tmpzptr->z_class == qclass)
111 for (tmprrecp = tmpzptr->z_rr;
112 tmprrecp && !seen_before;
113 tmprrecp = tmprrecp->r_grpnext)
114 if (strcasecmp(dname, tmprrecp->r_dname) == 0
115 && tmprrecp->r_class == qclass) {
121 * Append to the end of
124 for (tmprrecp = tmpzptr->z_rr;
126 tmprrecp = tmprrecp->r_grpnext)
128 tmprrecp->r_grpnext = rrecp;
129 rrecp->r_grpnext = NULL;
134 } else if (qtype == T_A) {
135 for (tmpzptr = zgrp_start;
137 tmpzptr = tmpzptr->z_next)
138 for (i = 0; i < tmpzptr->z_nscount; i++)
139 if (tmpzptr->z_class == qclass &&
140 strcasecmp(tmpzptr->z_ns[i].nsname,
142 tmpzptr->z_ns[i].nsaddr1.s_addr != 0) {
143 zptr->z_ns[k].nsaddr1.s_addr =
144 tmpzptr->z_ns[i].nsaddr1.s_addr;
151 n = res_mkquery(QUERY, dname, qclass, qtype, NULL,
152 0, NULL, buf, sizeof buf);
154 fprintf(stderr, "res_update: mkquery failed\n");
157 n = res_send(buf, n, answer, sizeof answer);
159 fprintf(stderr, "res_update: send error for %s\n",
162 } else if (n > sizeof(answer)) {
163 fprintf(stderr, "res_update: buffer too small\n");
168 ancount = ntohs(hp->ancount);
169 nscount = ntohs(hp->nscount);
170 arcount = ntohs(hp->arcount);
172 cp = answer + HFIXEDSZ;
174 /* skip the question section */
175 n = dn_skipname(cp, eom);
176 if (n < 0 || cp + n + 2 * INT16SZ > eom)
178 cp += n + 2 * INT16SZ;
180 if (qtype == T_SOA) {
181 if (ancount == 0 && nscount == 0 && arcount == 0) {
183 * if (rcode == NOERROR) then the dname exists but
184 * has no soa record associated with it.
185 * if (rcode == NXDOMAIN) then the dname does not
186 * exist and the server is replying out of NCACHE.
187 * in either case, proceed with the next try
189 dname = strchr(dname, '.');
193 } else if ((rcode == NOERROR || rcode == NXDOMAIN) &&
195 nscount == 1 && arcount == 0) {
197 * name/data does not exist, soa record supplied in the
200 /* authority section must contain the soa record */
201 if ((n = dn_expand(answer, eom, cp, zname,
205 if (cp + 2 * INT16SZ > eom)
209 if (type != T_SOA || class != qclass) {
210 fprintf(stderr, "unknown answer\n");
216 if (strcasecmp(dname, zname) == 0) {
219 } else if ((dname = strchr(dname, '.')) != NULL)
222 dname = strchr(svdname, '.');
229 } else if (rcode == NOERROR && ancount == 1) {
231 * found the zone name
232 * new servers will supply NS records for the zone
233 * in authority section and A records for those
234 * nameservers in the additional section
235 * older servers have to be explicitly queried for
236 * NS records for the zone
238 /* answer section must contain the soa record */
239 if ((n = dn_expand(answer, eom, cp, zname,
244 if (cp + 2 * INT16SZ > eom)
248 if (type == T_CNAME) {
249 dname = strchr(dname, '.');
254 if (strcasecmp(dname, zname) != 0 ||
256 class != rrecp->r_class) {
257 fprintf(stderr, "unknown answer\n");
263 "unknown response: ans=%d, auth=%d, add=%d, rcode=%d\n",
264 ancount, nscount, arcount, hp->rcode);
267 if (cp + INT32SZ + INT16SZ > eom)
269 /* continue processing the soa record */
278 if (strcasecmp(zname, zptr->z_origin) == 0 &&
279 type == T_SOA && class == qclass) {
287 for (tmprrecp = zptr->z_rr;
289 tmprrecp = tmprrecp->r_grpnext)
291 tmprrecp->r_grpnext = rrecp;
292 rrecp->r_grpnext = NULL;
297 if ((n = dn_expand(answer, eom, cp, primary,
298 sizeof primary)) < 0)
302 * We don't have to bounds check here because the
303 * next use of 'cp' is in dn_expand().
305 cp1 = (char *)soardata;
306 strcpy(cp1, primary);
307 cp1 += strlen(cp1) + 1;
308 if ((n = dn_expand(answer, eom, cp, mailaddr,
309 sizeof mailaddr)) < 0)
312 strcpy(cp1, mailaddr);
313 cp1 += strlen(cp1) + 1;
314 if (cp + 5*INT32SZ > eom)
316 memcpy(cp1, cp, 5*INT32SZ);
319 rdatasize = (u_char *)cp1 - soardata;
320 zptr = calloc(1, sizeof(struct zonegrp));
323 if (zgrp_start == NULL)
326 prevzptr->z_next = zptr;
328 rrecp->r_grpnext = NULL;
329 strcpy(zptr->z_origin, zname);
330 zptr->z_class = class;
331 memcpy(zptr->z_soardata, soardata, rdatasize);
332 /* fallthrough to process NS and A records */
334 } else if (qtype == T_NS) {
335 if (rcode == NOERROR && ancount > 0) {
336 strcpy(zname, dname);
337 for (zptr = zgrp_start; zptr; zptr = zptr->z_next) {
338 if (strcasecmp(zname, zptr->z_origin) == 0)
342 /* should not happen */
346 * answer and authority sections contain
347 * the same information, skip answer section
349 for (j = 0; j < ancount; j++) {
350 n = dn_skipname(cp, eom);
353 n += 2*INT16SZ + INT32SZ;
354 if (cp + n + INT16SZ > eom)
362 /* fallthrough to process NS and A records */
364 fprintf(stderr, "cannot determine nameservers for %s:\
365 ans=%d, auth=%d, add=%d, rcode=%d\n",
366 dname, ancount, nscount, arcount, hp->rcode);
369 } else if (qtype == T_A) {
370 if (rcode == NOERROR && ancount > 0) {
372 ancount = nscount = 0;
373 /* fallthrough to process A records */
375 fprintf(stderr, "cannot determine address for %s:\
376 ans=%d, auth=%d, add=%d, rcode=%d\n",
377 dname, ancount, nscount, arcount, hp->rcode);
381 /* process NS records for the zone */
383 for (i = 0; i < nscount; i++) {
384 if ((n = dn_expand(answer, eom, cp, name,
388 if (cp + 3 * INT16SZ + INT32SZ > eom)
396 if (strcasecmp(name, zname) == 0 &&
397 type == T_NS && class == qclass) {
398 if ((n = dn_expand(answer, eom, cp,
399 name, sizeof name)) < 0)
401 target = zptr->z_ns[j++].nsname;
402 strcpy(target, name);
406 if (zptr->z_nscount == 0)
408 /* get addresses for the nameservers */
409 for (i = 0; i < arcount; i++) {
410 if ((n = dn_expand(answer, eom, cp, name,
414 if (cp + 3 * INT16SZ + INT32SZ > eom)
422 if (type == T_A && dlen == INT32SZ && class == qclass) {
423 for (j = 0; j < zptr->z_nscount; j++)
424 if (strcasecmp(name, zptr->z_ns[j].nsname) == 0) {
425 memcpy(&zptr->z_ns[j].nsaddr1.s_addr, cp,
432 if (zptr->z_nscount == 0) {
438 for (k = 0; k < zptr->z_nscount; k++)
439 if (zptr->z_ns[k].nsaddr1.s_addr == 0) {
441 dname = zptr->z_ns[k].nsname;
448 _res.options |= RES_DEBUG;
449 for (zptr = zgrp_start; zptr; zptr = zptr->z_next) {
451 /* append zone section */
452 rrecp = res_mkupdrec(ns_s_zn, zptr->z_origin,
453 zptr->z_class, ns_t_soa, 0);
455 fprintf(stderr, "saverrec error\n");
459 rrecp->r_grpnext = zptr->z_rr;
462 n = res_mkupdate(zptr->z_rr, packet, sizeof packet);
464 fprintf(stderr, "res_mkupdate error\n");
468 fprintf(stdout, "res_mkupdate: packet size = %d\n", n);
471 * Override the list of NS records from res_init() with
472 * the authoritative nameservers for the zone being updated.
473 * Sort primary to be the first in the list of nameservers.
475 for (i = 0; i < zptr->z_nscount; i++) {
476 if (strcasecmp(zptr->z_ns[i].nsname,
477 zptr->z_soardata) == 0) {
478 struct in_addr tmpaddr;
481 strcpy(zptr->z_ns[i].nsname,
482 zptr->z_ns[0].nsname);
483 strcpy(zptr->z_ns[0].nsname,
485 tmpaddr = zptr->z_ns[i].nsaddr1;
486 zptr->z_ns[i].nsaddr1 =
487 zptr->z_ns[0].nsaddr1;
488 zptr->z_ns[0].nsaddr1 = tmpaddr;
493 for (i = 0; i < MAXNS; i++) {
494 _res.nsaddr_list[i].sin_addr = zptr->z_ns[i].nsaddr1;
495 _res.nsaddr_list[i].sin_family = AF_INET;
496 _res.nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
498 _res.nscount = (zptr->z_nscount < MAXNS) ?
499 zptr->z_nscount : MAXNS;
500 n = res_send(packet, n, answer, sizeof(answer));
502 fprintf(stderr, "res_send: send error, n=%d\n", n);
504 } else if (n > sizeof(answer)) {
505 fprintf(stderr, "res_send: buffer too small\n");
511 /* free malloc'ed memory */
514 zgrp_start = zgrp_start->z_next;
515 res_freeupdrec(zptr->z_rr); /* Zone section we allocated. */