1 /*	$NetBSD: res_update.c,v 1.1.1.1 2009/04/12 15:33:55 christos Exp $	*/
2 
3 #if !defined(lint) && !defined(SABER)
4 static const char rcsid[] = "Id: res_update.c,v 1.13 2005/04/27 04:56:43 sra Exp";
5 #endif /* not lint */
6 
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 "port_before.h"
31 
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <sys/time.h>
35 
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <arpa/nameser.h>
39 
40 #include <errno.h>
41 #include <limits.h>
42 #include <netdb.h>
43 #include <res_update.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 
49 #include <isc/list.h>
50 #include <resolv.h>
51 
52 #include "port_after.h"
53 #include "res_private.h"
54 
55 /*%
56  * Separate a linked list of records into groups so that all records
57  * in a group will belong to a single zone on the nameserver.
58  * Create a dynamic update packet for each zone and send it to the
59  * nameservers for that zone, and await answer.
60  * Abort if error occurs in updating any zone.
61  * Return the number of zones updated on success, < 0 on error.
62  *
63  * On error, caller must deal with the unsynchronized zones
64  * eg. an A record might have been successfully added to the forward
65  * zone but the corresponding PTR record would be missing if error
66  * was encountered while updating the reverse zone.
67  */
68 
69 struct zonegrp {
70 	char			z_origin[MAXDNAME];
71 	ns_class		z_class;
72 	union res_sockaddr_union z_nsaddrs[MAXNS];
73 	int			z_nscount;
74 	int			z_flags;
75 	LIST(ns_updrec)		z_rrlist;
76 	LINK(struct zonegrp)	z_link;
77 };
78 
79 #define ZG_F_ZONESECTADDED	0x0001
80 
81 /* Forward. */
82 
83 static void	res_dprintf(const char *, ...) ISC_FORMAT_PRINTF(1, 2);
84 
85 /* Macros. */
86 
87 #define DPRINTF(x) do {\
88 		int save_errno = errno; \
89 		if ((statp->options & RES_DEBUG) != 0U) res_dprintf x; \
90 		errno = save_errno; \
91 	} while (0)
92 
93 /* Public. */
94 
95 int
96 res_nupdate(res_state statp, ns_updrec *rrecp_in, ns_tsig_key *key) {
97 	ns_updrec *rrecp;
98 	u_char answer[PACKETSZ];
99 	u_char *packet;
100 	struct zonegrp *zptr, tgrp;
101 	LIST(struct zonegrp) zgrps;
102 	int nzones = 0, nscount = 0, n;
103 	union res_sockaddr_union nsaddrs[MAXNS];
104 
105 	packet = malloc(NS_MAXMSG);
106 	if (packet == NULL) {
107 		DPRINTF(("malloc failed"));
108 		return (0);
109 	}
110 	/* Thread all of the updates onto a list of groups. */
111 	INIT_LIST(zgrps);
112 	memset(&tgrp, 0, sizeof (tgrp));
113 	for (rrecp = rrecp_in; rrecp;
114 	     rrecp = LINKED(rrecp, r_link) ? NEXT(rrecp, r_link) : NULL) {
115 		int nscnt;
116 		/* Find the origin for it if there is one. */
117 		tgrp.z_class = rrecp->r_class;
118 		nscnt = res_findzonecut2(statp, rrecp->r_dname, tgrp.z_class,
119 					 RES_EXHAUSTIVE, tgrp.z_origin,
120 					 sizeof tgrp.z_origin,
121 					 tgrp.z_nsaddrs, MAXNS);
122 		if (nscnt <= 0) {
123 			DPRINTF(("res_findzonecut failed (%d)", nscnt));
124 			goto done;
125 		}
126 		tgrp.z_nscount = nscnt;
127 		/* Find the group for it if there is one. */
128 		for (zptr = HEAD(zgrps); zptr != NULL; zptr = NEXT(zptr, z_link))
129 			if (ns_samename(tgrp.z_origin, zptr->z_origin) == 1 &&
130 			    tgrp.z_class == zptr->z_class)
131 				break;
132 		/* Make a group for it if there isn't one. */
133 		if (zptr == NULL) {
134 			zptr = malloc(sizeof *zptr);
135 			if (zptr == NULL) {
136 				DPRINTF(("malloc failed"));
137 				goto done;
138 			}
139 			*zptr = tgrp;
140 			zptr->z_flags = 0;
141 			INIT_LINK(zptr, z_link);
142 			INIT_LIST(zptr->z_rrlist);
143 			APPEND(zgrps, zptr, z_link);
144 		}
145 		/* Thread this rrecp onto the right group. */
146 		APPEND(zptr->z_rrlist, rrecp, r_glink);
147 	}
148 
149 	for (zptr = HEAD(zgrps); zptr != NULL; zptr = NEXT(zptr, z_link)) {
150 		/* Construct zone section and prepend it. */
151 		rrecp = res_mkupdrec(ns_s_zn, zptr->z_origin,
152 				     zptr->z_class, ns_t_soa, 0);
153 		if (rrecp == NULL) {
154 			DPRINTF(("res_mkupdrec failed"));
155 			goto done;
156 		}
157 		PREPEND(zptr->z_rrlist, rrecp, r_glink);
158 		zptr->z_flags |= ZG_F_ZONESECTADDED;
159 
160 		/* Marshall the update message. */
161 		n = res_nmkupdate(statp, HEAD(zptr->z_rrlist),
162 				  packet, NS_MAXMSG);
163 		DPRINTF(("res_mkupdate -> %d", n));
164 		if (n < 0)
165 			goto done;
166 
167 		/* Temporarily replace the resolver's nameserver set. */
168 		nscount = res_getservers(statp, nsaddrs, MAXNS);
169 		res_setservers(statp, zptr->z_nsaddrs, zptr->z_nscount);
170 
171 		/* Send the update and remember the result. */
172 		if (key != NULL)
173 			n = res_nsendsigned(statp, packet, n, key,
174 					    answer, sizeof answer);
175 		else
176 			n = res_nsend(statp, packet, n, answer, sizeof answer);
177 		if (n < 0) {
178 			DPRINTF(("res_nsend: send error, n=%d (%s)\n",
179 				 n, strerror(errno)));
180 			goto done;
181 		}
182 		if (((HEADER *)answer)->rcode == NOERROR)
183 			nzones++;
184 
185 		/* Restore resolver's nameserver set. */
186 		res_setservers(statp, nsaddrs, nscount);
187 		nscount = 0;
188 	}
189  done:
190 	while (!EMPTY(zgrps)) {
191 		zptr = HEAD(zgrps);
192 		if ((zptr->z_flags & ZG_F_ZONESECTADDED) != 0)
193 			res_freeupdrec(HEAD(zptr->z_rrlist));
194 		UNLINK(zgrps, zptr, z_link);
195 		free(zptr);
196 	}
197 	if (nscount != 0)
198 		res_setservers(statp, nsaddrs, nscount);
199 
200 	free(packet);
201 	return (nzones);
202 }
203 
204 /* Private. */
205 
206 static void
207 res_dprintf(const char *fmt, ...) {
208 	va_list ap;
209 
210 	va_start(ap, fmt);
211 	fputs(";; res_nupdate: ", stderr);
212 	vfprintf(stderr, fmt, ap);
213 	fputc('\n', stderr);
214 	va_end(ap);
215 }
216