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