1 #if !defined(lint) && !defined(SABER)
2 static char rcsid[] = "$FreeBSD$";
6 * Copyright (c) 1996 by Internet Software Consortium.
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
13 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
14 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
18 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 * Based on the Dynamic DNS reference implementation by Viraj Bais
24 * <viraj_bais@ccm.fm.intel.com>
27 #include <sys/param.h>
28 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <arpa/nameser.h>
42 * Separate a linked list of records into groups so that all records
43 * in a group will belong to a single zone on the nameserver.
44 * Create a dynamic update packet for each zone and send it to the
45 * nameservers for that zone, and await answer.
46 * Abort if error occurs in updating any zone.
47 * Return the number of zones updated on success, < 0 on error.
49 * On error, caller must deal with the unsynchronized zones
50 * eg. an A record might have been successfully added to the forward
51 * zone but the corresponding PTR record would be missing if error
52 * was encountered while updating the reverse zone.
58 char nsname[MAXDNAME];
59 struct in_addr nsaddr1;
63 char z_origin[MAXDNAME];
65 char z_soardata[MAXDNAME + 5 * INT32SZ];
66 struct ns1 z_ns[NSMAX];
69 struct zonegrp *z_next;
74 res_update(ns_updrec *rrecp_in) {
75 ns_updrec *rrecp, *tmprrecp;
76 u_char buf[PACKETSZ], answer[PACKETSZ], packet[2*PACKETSZ];
77 char name[MAXDNAME], zname[MAXDNAME], primary[MAXDNAME],
79 u_char soardata[2*MAXCDNAME+5*INT32SZ];
80 char *dname, *svdname, *cp1, *target;
82 HEADER *hp = (HEADER *) answer;
83 struct zonegrp *zptr = NULL, *tmpzptr, *prevzptr, *zgrp_start = NULL;
84 int i, j, k = 0, n, ancount, nscount, arcount, rcode, rdatasize,
85 newgroup, done, myzone, seen_before, numzones = 0;
86 u_int16_t dlen, class, qclass, type, qtype;
89 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
90 h_errno = NETDB_INTERNAL;
94 for (rrecp = rrecp_in; rrecp; rrecp = rrecp->r_next) {
95 dname = rrecp->r_dname;
97 if (dname[n-1] == '.')
100 qclass = rrecp->r_class;
104 while (!done && dname) {
105 if (qtype == T_SOA) {
106 for (tmpzptr = zgrp_start;
107 tmpzptr && !seen_before;
108 tmpzptr = tmpzptr->z_next) {
109 if (strcasecmp(dname,
110 tmpzptr->z_origin) == 0 &&
111 tmpzptr->z_class == qclass)
113 for (tmprrecp = tmpzptr->z_rr;
114 tmprrecp && !seen_before;
115 tmprrecp = tmprrecp->r_grpnext)
116 if (strcasecmp(dname, tmprrecp->r_dname) == 0
117 && tmprrecp->r_class == qclass) {
123 * Append to the end of
126 for (tmprrecp = tmpzptr->z_rr;
128 tmprrecp = tmprrecp->r_grpnext)
130 tmprrecp->r_grpnext = rrecp;
131 rrecp->r_grpnext = NULL;
136 } else if (qtype == T_A) {
137 for (tmpzptr = zgrp_start;
139 tmpzptr = tmpzptr->z_next)
140 for (i = 0; i < tmpzptr->z_nscount; i++)
141 if (tmpzptr->z_class == qclass &&
142 strcasecmp(tmpzptr->z_ns[i].nsname,
144 tmpzptr->z_ns[i].nsaddr1.s_addr != 0) {
145 zptr->z_ns[k].nsaddr1.s_addr =
146 tmpzptr->z_ns[i].nsaddr1.s_addr;
153 n = res_mkquery(QUERY, dname, qclass, qtype, NULL,
154 0, NULL, buf, sizeof buf);
156 fprintf(stderr, "res_update: mkquery failed\n");
159 n = res_send(buf, n, answer, sizeof answer);
161 fprintf(stderr, "res_update: send error for %s\n",
167 ancount = ntohs(hp->ancount);
168 nscount = ntohs(hp->nscount);
169 arcount = ntohs(hp->arcount);
171 cp = answer + HFIXEDSZ;
173 /* skip the question section */
174 n = dn_skipname(cp, eom);
175 if (n < 0 || cp + n + 2 * INT16SZ > eom)
177 cp += n + 2 * INT16SZ;
179 if (qtype == T_SOA) {
180 if (ancount == 0 && nscount == 0 && arcount == 0) {
182 * if (rcode == NOERROR) then the dname exists but
183 * has no soa record associated with it.
184 * if (rcode == NXDOMAIN) then the dname does not
185 * exist and the server is replying out of NCACHE.
186 * in either case, proceed with the next try
188 dname = strchr(dname, '.');
192 } else if ((rcode == NOERROR || rcode == NXDOMAIN) &&
194 nscount == 1 && arcount == 0) {
196 * name/data does not exist, soa record supplied in the
199 /* authority section must contain the soa record */
200 if ((n = dn_expand(answer, eom, cp, zname,
204 if (cp + 2 * INT16SZ > eom)
208 if (type != T_SOA || class != qclass) {
209 fprintf(stderr, "unknown answer\n");
215 if (strcasecmp(dname, zname) == 0) {
218 } else if ((dname = strchr(dname, '.')) != NULL)
221 dname = strchr(svdname, '.');
228 } else if (rcode == NOERROR && ancount == 1) {
230 * found the zone name
231 * new servers will supply NS records for the zone
232 * in authority section and A records for those
233 * nameservers in the additional section
234 * older servers have to be explicitly queried for
235 * NS records for the zone
237 /* answer section must contain the soa record */
238 if ((n = dn_expand(answer, eom, cp, zname,
243 if (cp + 2 * INT16SZ > eom)
247 if (type == T_CNAME) {
248 dname = strchr(dname, '.');
253 if (strcasecmp(dname, zname) != 0 ||
255 class != rrecp->r_class) {
256 fprintf(stderr, "unknown answer\n");
262 "unknown response: ans=%d, auth=%d, add=%d, rcode=%d\n",
263 ancount, nscount, arcount, hp->rcode);
266 if (cp + INT32SZ + INT16SZ > eom)
268 /* continue processing the soa record */
277 if (strcasecmp(zname, zptr->z_origin) == 0 &&
278 type == T_SOA && class == qclass) {
286 for (tmprrecp = zptr->z_rr;
288 tmprrecp = tmprrecp->r_grpnext)
290 tmprrecp->r_grpnext = rrecp;
291 rrecp->r_grpnext = NULL;
296 if ((n = dn_expand(answer, eom, cp, primary,
297 sizeof primary)) < 0)
301 * We don't have to bounds check here because the
302 * next use of 'cp' is in dn_expand().
304 cp1 = (char *)soardata;
305 strcpy(cp1, primary);
306 cp1 += strlen(cp1) + 1;
307 if ((n = dn_expand(answer, eom, cp, mailaddr,
308 sizeof mailaddr)) < 0)
311 strcpy(cp1, mailaddr);
312 cp1 += strlen(cp1) + 1;
313 if (cp + 5*INT32SZ > eom)
315 memcpy(cp1, cp, 5*INT32SZ);
318 rdatasize = (u_char *)cp1 - soardata;
319 zptr = calloc(1, sizeof(struct zonegrp));
322 if (zgrp_start == NULL)
325 prevzptr->z_next = zptr;
327 rrecp->r_grpnext = NULL;
328 strcpy(zptr->z_origin, zname);
329 zptr->z_class = class;
330 memcpy(zptr->z_soardata, soardata, rdatasize);
331 /* fallthrough to process NS and A records */
333 } else if (qtype == T_NS) {
334 if (rcode == NOERROR && ancount > 0) {
335 strcpy(zname, dname);
336 for (zptr = zgrp_start; zptr; zptr = zptr->z_next) {
337 if (strcasecmp(zname, zptr->z_origin) == 0)
341 /* should not happen */
345 * answer and authority sections contain
346 * the same information, skip answer section
348 for (j = 0; j < ancount; j++) {
349 n = dn_skipname(cp, eom);
352 n += 2*INT16SZ + INT32SZ;
353 if (cp + n + INT16SZ > eom)
361 /* fallthrough to process NS and A records */
363 fprintf(stderr, "cannot determine nameservers for %s:\
364 ans=%d, auth=%d, add=%d, rcode=%d\n",
365 dname, ancount, nscount, arcount, hp->rcode);
368 } else if (qtype == T_A) {
369 if (rcode == NOERROR && ancount > 0) {
371 ancount = nscount = 0;
372 /* fallthrough to process A records */
374 fprintf(stderr, "cannot determine address for %s:\
375 ans=%d, auth=%d, add=%d, rcode=%d\n",
376 dname, ancount, nscount, arcount, hp->rcode);
380 /* process NS records for the zone */
382 for (i = 0; i < nscount; i++) {
383 if ((n = dn_expand(answer, eom, cp, name,
387 if (cp + 3 * INT16SZ + INT32SZ > eom)
395 if (strcasecmp(name, zname) == 0 &&
396 type == T_NS && class == qclass) {
397 if ((n = dn_expand(answer, eom, cp,
398 name, sizeof name)) < 0)
400 target = zptr->z_ns[j++].nsname;
401 strcpy(target, name);
405 if (zptr->z_nscount == 0)
407 /* get addresses for the nameservers */
408 for (i = 0; i < arcount; i++) {
409 if ((n = dn_expand(answer, eom, cp, name,
413 if (cp + 3 * INT16SZ + INT32SZ > eom)
421 if (type == T_A && dlen == INT32SZ && class == qclass) {
422 for (j = 0; j < zptr->z_nscount; j++)
423 if (strcasecmp(name, zptr->z_ns[j].nsname) == 0) {
424 memcpy(&zptr->z_ns[j].nsaddr1.s_addr, cp,
431 if (zptr->z_nscount == 0) {
437 for (k = 0; k < zptr->z_nscount; k++)
438 if (zptr->z_ns[k].nsaddr1.s_addr == 0) {
440 dname = zptr->z_ns[k].nsname;
447 _res.options |= RES_DEBUG;
448 for (zptr = zgrp_start; zptr; zptr = zptr->z_next) {
450 /* append zone section */
451 rrecp = res_mkupdrec(ns_s_zn, zptr->z_origin,
452 zptr->z_class, ns_t_soa, 0);
454 fprintf(stderr, "saverrec error\n");
458 rrecp->r_grpnext = zptr->z_rr;
461 n = res_mkupdate(zptr->z_rr, packet, sizeof packet);
463 fprintf(stderr, "res_mkupdate error\n");
467 fprintf(stdout, "res_mkupdate: packet size = %d\n", n);
470 * Override the list of NS records from res_init() with
471 * the authoritative nameservers for the zone being updated.
472 * Sort primary to be the first in the list of nameservers.
474 for (i = 0; i < zptr->z_nscount; i++) {
475 if (strcasecmp(zptr->z_ns[i].nsname,
476 zptr->z_soardata) == 0) {
477 struct in_addr tmpaddr;
480 strcpy(zptr->z_ns[i].nsname,
481 zptr->z_ns[0].nsname);
482 strcpy(zptr->z_ns[0].nsname,
484 tmpaddr = zptr->z_ns[i].nsaddr1;
485 zptr->z_ns[i].nsaddr1 =
486 zptr->z_ns[0].nsaddr1;
487 zptr->z_ns[0].nsaddr1 = tmpaddr;
492 for (i = 0; i < MAXNS; i++) {
493 _res.nsaddr_list[i].sin_addr = zptr->z_ns[i].nsaddr1;
494 _res.nsaddr_list[i].sin_family = AF_INET;
495 _res.nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
497 _res.nscount = (zptr->z_nscount < MAXNS) ?
498 zptr->z_nscount : MAXNS;
499 n = res_send(packet, n, answer, sizeof(answer));
501 fprintf(stderr, "res_send: send error, n=%d\n", n);
507 /* free malloc'ed memory */
510 zgrp_start = zgrp_start->z_next;
511 res_freeupdrec(zptr->z_rr); /* Zone section we allocated. */