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