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