libc: Move unused rcsid[] etc. constants into the
[dragonfly.git] / lib / libc / resolv / res_update.c
blob0e76477f1564c28c4df9cb9ad39dc14d6a11524d
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * $Id: res_update.c,v 1.13 2005/04/27 04:56:43 sra Exp $
20 /*! \file
21 * \brief
22 * Based on the Dynamic DNS reference implementation by Viraj Bais
23 * <viraj_bais@ccm.fm.intel.com>
26 #include "port_before.h"
28 #include <sys/param.h>
29 #include <sys/socket.h>
30 #include <sys/time.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <arpa/nameser.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <netdb.h>
39 #include <res_update.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
45 #include "isc/list.h"
46 #include <resolv.h>
48 #include "port_after.h"
49 #include "res_private.h"
51 /*%
52 * Separate a linked list of records into groups so that all records
53 * in a group will belong to a single zone on the nameserver.
54 * Create a dynamic update packet for each zone and send it to the
55 * nameservers for that zone, and await answer.
56 * Abort if error occurs in updating any zone.
57 * Return the number of zones updated on success, < 0 on error.
59 * On error, caller must deal with the unsynchronized zones
60 * eg. an A record might have been successfully added to the forward
61 * zone but the corresponding PTR record would be missing if error
62 * was encountered while updating the reverse zone.
65 struct zonegrp {
66 char z_origin[MAXDNAME];
67 ns_class z_class;
68 union res_sockaddr_union z_nsaddrs[MAXNS];
69 int z_nscount;
70 int z_flags;
71 LIST(ns_updrec) z_rrlist;
72 LINK(struct zonegrp) z_link;
75 #define ZG_F_ZONESECTADDED 0x0001
77 /* Forward. */
79 static void res_dprintf(const char *, ...) ISC_FORMAT_PRINTF(1, 2);
81 /* Macros. */
83 #define DPRINTF(x) do {\
84 int save_errno = errno; \
85 if ((statp->options & RES_DEBUG) != 0U) res_dprintf x; \
86 errno = save_errno; \
87 } while (0)
89 /* Public. */
91 int
92 res_nupdate(res_state statp, ns_updrec *rrecp_in, ns_tsig_key *key) {
93 ns_updrec *rrecp;
94 u_char answer[PACKETSZ];
95 u_char *packet;
96 struct zonegrp *zptr, tgrp;
97 LIST(struct zonegrp) zgrps;
98 int nzones = 0, nscount = 0, n;
99 union res_sockaddr_union nsaddrs[MAXNS];
101 packet = malloc(NS_MAXMSG);
102 if (packet == NULL) {
103 DPRINTF(("malloc failed"));
104 return (0);
106 /* Thread all of the updates onto a list of groups. */
107 INIT_LIST(zgrps);
108 memset(&tgrp, 0, sizeof (tgrp));
109 for (rrecp = rrecp_in; rrecp;
110 rrecp = LINKED(rrecp, r_link) ? NEXT(rrecp, r_link) : NULL) {
111 int nscnt;
112 /* Find the origin for it if there is one. */
113 tgrp.z_class = rrecp->r_class;
114 nscnt = res_findzonecut2(statp, rrecp->r_dname, tgrp.z_class,
115 RES_EXHAUSTIVE, tgrp.z_origin,
116 sizeof tgrp.z_origin,
117 tgrp.z_nsaddrs, MAXNS);
118 if (nscnt <= 0) {
119 DPRINTF(("res_findzonecut failed (%d)", nscnt));
120 goto done;
122 tgrp.z_nscount = nscnt;
123 /* Find the group for it if there is one. */
124 for (zptr = HEAD(zgrps); zptr != NULL; zptr = NEXT(zptr, z_link))
125 if (ns_samename(tgrp.z_origin, zptr->z_origin) == 1 &&
126 tgrp.z_class == zptr->z_class)
127 break;
128 /* Make a group for it if there isn't one. */
129 if (zptr == NULL) {
130 zptr = malloc(sizeof *zptr);
131 if (zptr == NULL) {
132 DPRINTF(("malloc failed"));
133 goto done;
135 *zptr = tgrp;
136 zptr->z_flags = 0;
137 INIT_LINK(zptr, z_link);
138 INIT_LIST(zptr->z_rrlist);
139 APPEND(zgrps, zptr, z_link);
141 /* Thread this rrecp onto the right group. */
142 APPEND(zptr->z_rrlist, rrecp, r_glink);
145 for (zptr = HEAD(zgrps); zptr != NULL; zptr = NEXT(zptr, z_link)) {
146 /* Construct zone section and prepend it. */
147 rrecp = res_mkupdrec(ns_s_zn, zptr->z_origin,
148 zptr->z_class, ns_t_soa, 0);
149 if (rrecp == NULL) {
150 DPRINTF(("res_mkupdrec failed"));
151 goto done;
153 PREPEND(zptr->z_rrlist, rrecp, r_glink);
154 zptr->z_flags |= ZG_F_ZONESECTADDED;
156 /* Marshall the update message. */
157 n = res_nmkupdate(statp, HEAD(zptr->z_rrlist),
158 packet, NS_MAXMSG);
159 DPRINTF(("res_mkupdate -> %d", n));
160 if (n < 0)
161 goto done;
163 /* Temporarily replace the resolver's nameserver set. */
164 nscount = res_getservers(statp, nsaddrs, MAXNS);
165 res_setservers(statp, zptr->z_nsaddrs, zptr->z_nscount);
167 /* Send the update and remember the result. */
168 if (key != NULL)
169 #ifdef _LIBC
171 DPRINTF(("TSIG is not supported\n"));
172 RES_SET_H_ERRNO(statp, NO_RECOVERY);
173 goto done;
175 #else
176 n = res_nsendsigned(statp, packet, n, key,
177 answer, sizeof answer);
178 #endif
179 else
180 n = res_nsend(statp, packet, n, answer, sizeof answer);
181 if (n < 0) {
182 DPRINTF(("res_nsend: send error, n=%d (%s)\n",
183 n, strerror(errno)));
184 goto done;
186 if (((HEADER *)answer)->rcode == NOERROR)
187 nzones++;
189 /* Restore resolver's nameserver set. */
190 res_setservers(statp, nsaddrs, nscount);
191 nscount = 0;
193 done:
194 while (!EMPTY(zgrps)) {
195 zptr = HEAD(zgrps);
196 if ((zptr->z_flags & ZG_F_ZONESECTADDED) != 0)
197 res_freeupdrec(HEAD(zptr->z_rrlist));
198 UNLINK(zgrps, zptr, z_link);
199 free(zptr);
201 if (nscount != 0)
202 res_setservers(statp, nsaddrs, nscount);
204 free(packet);
205 return (nzones);
208 /* Private. */
210 static void
211 res_dprintf(const char *fmt, ...) {
212 va_list ap;
214 va_start(ap, fmt);
215 fputs(";; res_nupdate: ", stderr);
216 vfprintf(stderr, fmt, ap);
217 fputc('\n', stderr);
218 va_end(ap);