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