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