1 #if !defined(lint) && !defined(SABER)
2 static char rcsid[] = "$FreeBSD: src/lib/libc/net/res_update.c,v 1.2.2.2 2002/09/19 13:45:23 nectar Exp $";
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>
41 * Separate a linked list of records into groups so that all records
42 * in a group will belong to a single zone on the nameserver.
43 * Create a dynamic update packet for each zone and send it to the
44 * nameservers for that zone, and await answer.
45 * Abort if error occurs in updating any zone.
46 * Return the number of zones updated on success, < 0 on error.
48 * On error, caller must deal with the unsynchronized zones
49 * eg. an A record might have been successfully added to the forward
50 * zone but the corresponding PTR record would be missing if error
51 * was encountered while updating the reverse zone.
57 char nsname[MAXDNAME];
58 struct in_addr nsaddr1;
62 char z_origin[MAXDNAME];
64 char z_soardata[MAXDNAME + 5 * INT32SZ];
65 struct ns1 z_ns[NSMAX];
68 struct zonegrp *z_next;
73 res_update(ns_updrec *rrecp_in) {
74 ns_updrec *rrecp, *tmprrecp;
75 u_char buf[PACKETSZ], answer[PACKETSZ], packet[2*PACKETSZ];
76 char name[MAXDNAME], zname[MAXDNAME], primary[MAXDNAME],
78 u_char soardata[2*MAXCDNAME+5*INT32SZ];
79 char *dname, *svdname, *cp1, *target;
81 HEADER *hp = (HEADER *) answer;
82 struct zonegrp *zptr = NULL, *tmpzptr, *prevzptr, *zgrp_start = NULL;
83 int i, j, k = 0, n, ancount, nscount, arcount, rcode, rdatasize,
84 newgroup, done, myzone, seen_before, numzones = 0;
85 u_int16_t dlen, class, qclass, type, qtype;
88 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
89 h_errno = NETDB_INTERNAL;
93 for (rrecp = rrecp_in; rrecp; rrecp = rrecp->r_next) {
94 dname = rrecp->r_dname;
96 if (dname[n-1] == '.')
99 qclass = rrecp->r_class;
103 while (!done && dname) {
104 if (qtype == T_SOA) {
105 for (tmpzptr = zgrp_start;
106 tmpzptr && !seen_before;
107 tmpzptr = tmpzptr->z_next) {
108 if (strcasecmp(dname,
109 tmpzptr->z_origin) == 0 &&
110 tmpzptr->z_class == qclass)
112 for (tmprrecp = tmpzptr->z_rr;
113 tmprrecp && !seen_before;
114 tmprrecp = tmprrecp->r_grpnext)
115 if (strcasecmp(dname, tmprrecp->r_dname) == 0
116 && tmprrecp->r_class == qclass) {
122 * Append to the end of
125 for (tmprrecp = tmpzptr->z_rr;
127 tmprrecp = tmprrecp->r_grpnext)
129 tmprrecp->r_grpnext = rrecp;
130 rrecp->r_grpnext = NULL;
135 } else if (qtype == T_A) {
136 for (tmpzptr = zgrp_start;
138 tmpzptr = tmpzptr->z_next)
139 for (i = 0; i < tmpzptr->z_nscount; i++)
140 if (tmpzptr->z_class == qclass &&
141 strcasecmp(tmpzptr->z_ns[i].nsname,
143 tmpzptr->z_ns[i].nsaddr1.s_addr != 0) {
144 zptr->z_ns[k].nsaddr1.s_addr =
145 tmpzptr->z_ns[i].nsaddr1.s_addr;
152 n = res_mkquery(QUERY, dname, qclass, qtype, NULL,
153 0, NULL, buf, sizeof buf);
155 fprintf(stderr, "res_update: mkquery failed\n");
158 n = res_send(buf, n, answer, sizeof answer);
160 fprintf(stderr, "res_update: send error for %s\n",
163 } else if (n > sizeof(answer)) {
164 fprintf(stderr, "res_update: buffer too small\n");
169 ancount = ntohs(hp->ancount);
170 nscount = ntohs(hp->nscount);
171 arcount = ntohs(hp->arcount);
173 cp = answer + HFIXEDSZ;
175 /* skip the question section */
176 n = dn_skipname(cp, eom);
177 if (n < 0 || cp + n + 2 * INT16SZ > eom)
179 cp += n + 2 * INT16SZ;
181 if (qtype == T_SOA) {
182 if (ancount == 0 && nscount == 0 && arcount == 0) {
184 * if (rcode == NOERROR) then the dname exists but
185 * has no soa record associated with it.
186 * if (rcode == NXDOMAIN) then the dname does not
187 * exist and the server is replying out of NCACHE.
188 * in either case, proceed with the next try
190 dname = strchr(dname, '.');
194 } else if ((rcode == NOERROR || rcode == NXDOMAIN) &&
196 nscount == 1 && arcount == 0) {
198 * name/data does not exist, soa record supplied in the
201 /* authority section must contain the soa record */
202 if ((n = dn_expand(answer, eom, cp, zname,
206 if (cp + 2 * INT16SZ > eom)
210 if (type != T_SOA || class != qclass) {
211 fprintf(stderr, "unknown answer\n");
217 if (strcasecmp(dname, zname) == 0) {
220 } else if ((dname = strchr(dname, '.')) != NULL)
223 dname = strchr(svdname, '.');
230 } else if (rcode == NOERROR && ancount == 1) {
232 * found the zone name
233 * new servers will supply NS records for the zone
234 * in authority section and A records for those
235 * nameservers in the additional section
236 * older servers have to be explicitly queried for
237 * NS records for the zone
239 /* answer section must contain the soa record */
240 if ((n = dn_expand(answer, eom, cp, zname,
245 if (cp + 2 * INT16SZ > eom)
249 if (type == T_CNAME) {
250 dname = strchr(dname, '.');
255 if (strcasecmp(dname, zname) != 0 ||
257 class != rrecp->r_class) {
258 fprintf(stderr, "unknown answer\n");
264 "unknown response: ans=%d, auth=%d, add=%d, rcode=%d\n",
265 ancount, nscount, arcount, hp->rcode);
268 if (cp + INT32SZ + INT16SZ > eom)
270 /* continue processing the soa record */
279 if (strcasecmp(zname, zptr->z_origin) == 0 &&
280 type == T_SOA && class == qclass) {
288 for (tmprrecp = zptr->z_rr;
290 tmprrecp = tmprrecp->r_grpnext)
292 tmprrecp->r_grpnext = rrecp;
293 rrecp->r_grpnext = NULL;
298 if ((n = dn_expand(answer, eom, cp, primary,
299 sizeof primary)) < 0)
303 * We don't have to bounds check here because the
304 * next use of 'cp' is in dn_expand().
306 cp1 = (char *)soardata;
307 strcpy(cp1, primary);
308 cp1 += strlen(cp1) + 1;
309 if ((n = dn_expand(answer, eom, cp, mailaddr,
310 sizeof mailaddr)) < 0)
313 strcpy(cp1, mailaddr);
314 cp1 += strlen(cp1) + 1;
315 if (cp + 5*INT32SZ > eom)
317 memcpy(cp1, cp, 5*INT32SZ);
320 rdatasize = (u_char *)cp1 - soardata;
321 zptr = calloc(1, sizeof(struct zonegrp));
324 if (zgrp_start == NULL)
327 prevzptr->z_next = zptr;
329 rrecp->r_grpnext = NULL;
330 strcpy(zptr->z_origin, zname);
331 zptr->z_class = class;
332 memcpy(zptr->z_soardata, soardata, rdatasize);
333 /* fallthrough to process NS and A records */
335 } else if (qtype == T_NS) {
336 if (rcode == NOERROR && ancount > 0) {
337 strcpy(zname, dname);
338 for (zptr = zgrp_start; zptr; zptr = zptr->z_next) {
339 if (strcasecmp(zname, zptr->z_origin) == 0)
343 /* should not happen */
347 * answer and authority sections contain
348 * the same information, skip answer section
350 for (j = 0; j < ancount; j++) {
351 n = dn_skipname(cp, eom);
354 n += 2*INT16SZ + INT32SZ;
355 if (cp + n + INT16SZ > eom)
363 /* fallthrough to process NS and A records */
365 fprintf(stderr, "cannot determine nameservers for %s:\
366 ans=%d, auth=%d, add=%d, rcode=%d\n",
367 dname, ancount, nscount, arcount, hp->rcode);
370 } else if (qtype == T_A) {
371 if (rcode == NOERROR && ancount > 0) {
373 ancount = nscount = 0;
374 /* fallthrough to process A records */
376 fprintf(stderr, "cannot determine address for %s:\
377 ans=%d, auth=%d, add=%d, rcode=%d\n",
378 dname, ancount, nscount, arcount, hp->rcode);
382 /* process NS records for the zone */
384 for (i = 0; i < nscount; i++) {
385 if ((n = dn_expand(answer, eom, cp, name,
389 if (cp + 3 * INT16SZ + INT32SZ > eom)
397 if (strcasecmp(name, zname) == 0 &&
398 type == T_NS && class == qclass) {
399 if ((n = dn_expand(answer, eom, cp,
400 name, sizeof name)) < 0)
402 target = zptr->z_ns[j++].nsname;
403 strcpy(target, name);
407 if (zptr->z_nscount == 0)
409 /* get addresses for the nameservers */
410 for (i = 0; i < arcount; i++) {
411 if ((n = dn_expand(answer, eom, cp, name,
415 if (cp + 3 * INT16SZ + INT32SZ > eom)
423 if (type == T_A && dlen == INT32SZ && class == qclass) {
424 for (j = 0; j < zptr->z_nscount; j++)
425 if (strcasecmp(name, zptr->z_ns[j].nsname) == 0) {
426 memcpy(&zptr->z_ns[j].nsaddr1.s_addr, cp,
433 if (zptr->z_nscount == 0) {
439 for (k = 0; k < zptr->z_nscount; k++)
440 if (zptr->z_ns[k].nsaddr1.s_addr == 0) {
442 dname = zptr->z_ns[k].nsname;
449 _res.options |= RES_DEBUG;
450 for (zptr = zgrp_start; zptr; zptr = zptr->z_next) {
452 /* append zone section */
453 rrecp = res_mkupdrec(ns_s_zn, zptr->z_origin,
454 zptr->z_class, ns_t_soa, 0);
456 fprintf(stderr, "saverrec error\n");
460 rrecp->r_grpnext = zptr->z_rr;
463 n = res_mkupdate(zptr->z_rr, packet, sizeof packet);
465 fprintf(stderr, "res_mkupdate error\n");
469 fprintf(stdout, "res_mkupdate: packet size = %d\n", n);
472 * Override the list of NS records from res_init() with
473 * the authoritative nameservers for the zone being updated.
474 * Sort primary to be the first in the list of nameservers.
476 for (i = 0; i < zptr->z_nscount; i++) {
477 if (strcasecmp(zptr->z_ns[i].nsname,
478 zptr->z_soardata) == 0) {
479 struct in_addr tmpaddr;
482 strcpy(zptr->z_ns[i].nsname,
483 zptr->z_ns[0].nsname);
484 strcpy(zptr->z_ns[0].nsname,
486 tmpaddr = zptr->z_ns[i].nsaddr1;
487 zptr->z_ns[i].nsaddr1 =
488 zptr->z_ns[0].nsaddr1;
489 zptr->z_ns[0].nsaddr1 = tmpaddr;
494 for (i = 0; i < MAXNS; i++) {
495 _res.nsaddr_list[i].sin_addr = zptr->z_ns[i].nsaddr1;
496 _res.nsaddr_list[i].sin_family = AF_INET;
497 _res.nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
499 _res.nscount = (zptr->z_nscount < MAXNS) ?
500 zptr->z_nscount : MAXNS;
501 n = res_send(packet, n, answer, sizeof(answer));
503 fprintf(stderr, "res_send: send error, n=%d\n", n);
505 } else if (n > sizeof(answer)) {
506 fprintf(stderr, "res_send: buffer too small\n");
512 /* free malloc'ed memory */
515 zgrp_start = zgrp_start->z_next;
516 res_freeupdrec(zptr->z_rr); /* Zone section we allocated. */