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