]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/resolv/res_update.c
This commit was generated by cvs2svn to compensate for changes in r162509,
[FreeBSD/FreeBSD.git] / lib / libc / resolv / res_update.c
1 #if !defined(lint) && !defined(SABER)
2 static const char rcsid[] = "$Id: res_update.c,v 1.6.2.4.4.2 2004/03/16 12:34:20 marka 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 /*
23  * Based on the Dynamic DNS reference implementation by Viraj Bais
24  * <viraj_bais@ccm.fm.intel.com>
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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 #ifdef _LIBC
174                         DPRINTF(("TSIG is not supported\n"));
175                         RES_SET_H_ERRNO(statp, NO_RECOVERY);
176                         goto done;
177 #else
178                         n = res_nsendsigned(statp, packet, n, key,
179                                             answer, sizeof answer);
180 #endif
181                 } else
182                         n = res_nsend(statp, packet, n, answer, sizeof answer);
183                 if (n < 0) {
184                         DPRINTF(("res_nsend: send error, n=%d (%s)\n",
185                                  n, strerror(errno)));
186                         goto done;
187                 }
188                 if (((HEADER *)answer)->rcode == NOERROR)
189                         nzones++;
190
191                 /* Restore resolver's nameserver set. */
192                 res_setservers(statp, nsaddrs, nscount);
193                 nscount = 0;
194         }
195  done:
196         while (!EMPTY(zgrps)) {
197                 zptr = HEAD(zgrps);
198                 if ((zptr->z_flags & ZG_F_ZONESECTADDED) != 0)
199                         res_freeupdrec(HEAD(zptr->z_rrlist));
200                 UNLINK(zgrps, zptr, z_link);
201                 free(zptr);
202         }
203         if (nscount != 0)
204                 res_setservers(statp, nsaddrs, nscount);
205
206         free(packet);
207         return (nzones);
208 }
209
210 /* Private. */
211
212 static void
213 res_dprintf(const char *fmt, ...) {
214         va_list ap;
215
216         va_start(ap, fmt);
217         fputs(";; res_nupdate: ", stderr);
218         vfprintf(stderr, fmt, ap);
219         fputc('\n', stderr);
220         va_end(ap);
221 }