1#ifndef __APPLE__
2#if !defined(lint) && !defined(SABER)
3static const char rcsid[] = "$Id: res_update.c,v 1.1 2006/03/01 19:01:39 majka Exp $";
4#endif /* not lint */
5#endif
6
7/*
8 * Copyright (c) 1996-1999 by Internet Software Consortium.
9 *
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
15 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
17 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
18 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
19 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
20 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
21 * SOFTWARE.
22 */
23
24/*
25 * Based on the Dynamic DNS reference implementation by Viraj Bais
26 * <viraj_bais@ccm.fm.intel.com>
27 */
28
29#ifndef __APPLE__
30#include "port_before.h"
31#endif
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 <resolv.h>
51
52#ifndef __APPLE__
53#include <isc/list.h>
54#include "port_after.h"
55#endif
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
72struct zonegrp {
73	char			z_origin[NS_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
86static void	res_dprintf(const char *, ...);
87
88/* Macros. */
89
90#define DPRINTF(x) do {\
91		int save_errno = errno; \
92		if ((statp->options & RES_DEBUG) != 0) res_dprintf x; \
93		errno = save_errno; \
94	} while (0)
95
96/* Public. */
97
98int
99res_nupdate(res_state statp, ns_updrec *rrecp_in, ns_tsig_key *key) {
100	ns_updrec *rrecp;
101	u_char answer[NS_PACKETSZ], packet[2*NS_PACKETSZ];
102	struct zonegrp *zptr, tgrp;
103	LIST(struct zonegrp) zgrps;
104	int nzones = 0, nscount = 0, n;
105	union res_sockaddr_union nsaddrs[MAXNS];
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, sizeof packet);
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			n = res_nsendsigned(statp, packet, n, key,
171					    answer, sizeof answer);
172		else
173			n = res_nsend(statp, packet, n, answer, sizeof answer);
174		if (n < 0) {
175			DPRINTF(("res_nsend: send error, n=%d (%s)\n",
176				 n, strerror(errno)));
177			goto done;
178		}
179		if (((HEADER *)answer)->rcode == ns_r_noerror)
180			nzones++;
181
182		/* Restore resolver's nameserver set. */
183		res_setservers(statp, nsaddrs, nscount);
184		nscount = 0;
185	}
186 done:
187	while (!EMPTY(zgrps)) {
188		zptr = HEAD(zgrps);
189		if ((zptr->z_flags & ZG_F_ZONESECTADDED) != 0)
190			res_freeupdrec(HEAD(zptr->z_rrlist));
191		UNLINK(zgrps, zptr, z_link);
192		free(zptr);
193	}
194	if (nscount != 0)
195		res_setservers(statp, nsaddrs, nscount);
196
197	return (nzones);
198}
199
200/* Private. */
201
202static void
203res_dprintf(const char *fmt, ...) {
204	va_list ap;
205
206	va_start(ap, fmt);
207	fputs(";; res_nupdate: ", stderr);
208	vfprintf(stderr, fmt, ap);
209	fputc('\n', stderr);
210	va_end(ap);
211}
212