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