1 static const char rcsid
[] = "$Id: res_update.c,v 1.13 2005/04/27 04:56:43 sra Exp $";
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996-1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Based on the Dynamic DNS reference implementation by Viraj Bais
23 * <viraj_bais@ccm.fm.intel.com>
26 #include "port_before.h"
28 #include <sys/param.h>
29 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <arpa/nameser.h>
39 #include <res_update.h>
48 #include "port_after.h"
49 #include "res_private.h"
52 * Separate a linked list of records into groups so that all records
53 * in a group will belong to a single zone on the nameserver.
54 * Create a dynamic update packet for each zone and send it to the
55 * nameservers for that zone, and await answer.
56 * Abort if error occurs in updating any zone.
57 * Return the number of zones updated on success, < 0 on error.
59 * On error, caller must deal with the unsynchronized zones
60 * eg. an A record might have been successfully added to the forward
61 * zone but the corresponding PTR record would be missing if error
62 * was encountered while updating the reverse zone.
66 char z_origin
[MAXDNAME
];
68 union res_sockaddr_union z_nsaddrs
[MAXNS
];
71 LIST(ns_updrec
) z_rrlist
;
72 LINK(struct zonegrp
) z_link
;
75 #define ZG_F_ZONESECTADDED 0x0001
79 static void res_dprintf(const char *, ...) ISC_FORMAT_PRINTF(1, 2);
83 #define DPRINTF(x) do {\
84 int save_errno = errno; \
85 if ((statp->options & RES_DEBUG) != 0U) res_dprintf x; \
92 res_nupdate(res_state statp
, ns_updrec
*rrecp_in
, ns_tsig_key
*key
) {
94 u_char answer
[PACKETSZ
];
96 struct zonegrp
*zptr
, tgrp
;
97 LIST(struct zonegrp
) zgrps
;
98 int nzones
= 0, nscount
= 0, n
;
99 union res_sockaddr_union nsaddrs
[MAXNS
];
101 packet
= malloc(NS_MAXMSG
);
102 if (packet
== NULL
) {
103 DPRINTF(("malloc failed"));
106 /* Thread all of the updates onto a list of groups. */
108 memset(&tgrp
, 0, sizeof (tgrp
));
109 for (rrecp
= rrecp_in
; rrecp
;
110 rrecp
= LINKED(rrecp
, r_link
) ? NEXT(rrecp
, r_link
) : NULL
) {
112 /* Find the origin for it if there is one. */
113 tgrp
.z_class
= rrecp
->r_class
;
114 nscnt
= res_findzonecut2(statp
, rrecp
->r_dname
, tgrp
.z_class
,
115 RES_EXHAUSTIVE
, tgrp
.z_origin
,
116 sizeof tgrp
.z_origin
,
117 tgrp
.z_nsaddrs
, MAXNS
);
119 DPRINTF(("res_findzonecut failed (%d)", nscnt
));
122 tgrp
.z_nscount
= nscnt
;
123 /* Find the group for it if there is one. */
124 for (zptr
= HEAD(zgrps
); zptr
!= NULL
; zptr
= NEXT(zptr
, z_link
))
125 if (ns_samename(tgrp
.z_origin
, zptr
->z_origin
) == 1 &&
126 tgrp
.z_class
== zptr
->z_class
)
128 /* Make a group for it if there isn't one. */
130 zptr
= malloc(sizeof *zptr
);
132 DPRINTF(("malloc failed"));
137 INIT_LINK(zptr
, z_link
);
138 INIT_LIST(zptr
->z_rrlist
);
139 APPEND(zgrps
, zptr
, z_link
);
141 /* Thread this rrecp onto the right group. */
142 APPEND(zptr
->z_rrlist
, rrecp
, r_glink
);
145 for (zptr
= HEAD(zgrps
); zptr
!= NULL
; zptr
= NEXT(zptr
, z_link
)) {
146 /* Construct zone section and prepend it. */
147 rrecp
= res_mkupdrec(ns_s_zn
, zptr
->z_origin
,
148 zptr
->z_class
, ns_t_soa
, 0);
150 DPRINTF(("res_mkupdrec failed"));
153 PREPEND(zptr
->z_rrlist
, rrecp
, r_glink
);
154 zptr
->z_flags
|= ZG_F_ZONESECTADDED
;
156 /* Marshall the update message. */
157 n
= res_nmkupdate(statp
, HEAD(zptr
->z_rrlist
),
159 DPRINTF(("res_mkupdate -> %d", n
));
163 /* Temporarily replace the resolver's nameserver set. */
164 nscount
= res_getservers(statp
, nsaddrs
, MAXNS
);
165 res_setservers(statp
, zptr
->z_nsaddrs
, zptr
->z_nscount
);
167 /* Send the update and remember the result. */
169 n
= res_nsendsigned(statp
, packet
, n
, key
,
170 answer
, sizeof answer
);
172 n
= res_nsend(statp
, packet
, n
, answer
, sizeof answer
);
174 DPRINTF(("res_nsend: send error, n=%d (%s)\n",
175 n
, strerror(errno
)));
178 if (((HEADER
*)answer
)->rcode
== NOERROR
)
181 /* Restore resolver's nameserver set. */
182 res_setservers(statp
, nsaddrs
, nscount
);
186 while (!EMPTY(zgrps
)) {
188 if ((zptr
->z_flags
& ZG_F_ZONESECTADDED
) != 0)
189 res_freeupdrec(HEAD(zptr
->z_rrlist
));
190 UNLINK(zgrps
, zptr
, z_link
);
194 res_setservers(statp
, nsaddrs
, nscount
);
203 res_dprintf(const char *fmt
, ...) {
207 fputs(";; res_nupdate: ", stderr
);
208 vfprintf(stderr
, fmt
, ap
);