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