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