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