3 #if !defined(lint) && !defined(SABER)
4 static const char rcsid
[] = "Id: res_update.c,v 1.13 2005/04/27 04:56:43 sra Exp";
8 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
9 * Copyright (c) 1996-1999 by Internet Software Consortium.
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Based on the Dynamic DNS reference implementation by Viraj Bais
27 * <viraj_bais@ccm.fm.intel.com>
30 #include "port_before.h"
32 #include <sys/param.h>
33 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <arpa/nameser.h>
43 #include <res_update.h>
52 #include "port_after.h"
53 #include "res_private.h"
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.
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.
70 char z_origin
[MAXDNAME
];
72 union res_sockaddr_union z_nsaddrs
[MAXNS
];
75 LIST(ns_updrec
) z_rrlist
;
76 LINK(struct zonegrp
) z_link
;
79 #define ZG_F_ZONESECTADDED 0x0001
83 static void res_dprintf(const char *, ...) ISC_FORMAT_PRINTF(1, 2);
87 #define DPRINTF(x) do {\
88 int save_errno = errno; \
89 if ((statp->options & RES_DEBUG) != 0U) res_dprintf x; \
96 res_nupdate(res_state statp
, ns_updrec
*rrecp_in
, ns_tsig_key
*key
) {
98 u_char answer
[PACKETSZ
];
100 struct zonegrp
*zptr
, tgrp
;
101 LIST(struct zonegrp
) zgrps
;
102 int nzones
= 0, nscount
= 0, n
;
103 union res_sockaddr_union nsaddrs
[MAXNS
];
105 packet
= malloc(NS_MAXMSG
);
106 if (packet
== NULL
) {
107 DPRINTF(("malloc failed"));
110 /* Thread all of the updates onto a list of groups. */
112 memset(&tgrp
, 0, sizeof (tgrp
));
113 for (rrecp
= rrecp_in
; rrecp
;
114 rrecp
= LINKED(rrecp
, r_link
) ? NEXT(rrecp
, r_link
) : NULL
) {
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
);
123 DPRINTF(("res_findzonecut failed (%d)", nscnt
));
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
)
132 /* Make a group for it if there isn't one. */
134 zptr
= malloc(sizeof *zptr
);
136 DPRINTF(("malloc failed"));
141 INIT_LINK(zptr
, z_link
);
142 INIT_LIST(zptr
->z_rrlist
);
143 APPEND(zgrps
, zptr
, z_link
);
145 /* Thread this rrecp onto the right group. */
146 APPEND(zptr
->z_rrlist
, rrecp
, r_glink
);
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);
154 DPRINTF(("res_mkupdrec failed"));
157 PREPEND(zptr
->z_rrlist
, rrecp
, r_glink
);
158 zptr
->z_flags
|= ZG_F_ZONESECTADDED
;
160 /* Marshall the update message. */
161 n
= res_nmkupdate(statp
, HEAD(zptr
->z_rrlist
),
163 DPRINTF(("res_mkupdate -> %d", n
));
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
);
171 /* Send the update and remember the result. */
173 n
= res_nsendsigned(statp
, packet
, n
, key
,
174 answer
, sizeof answer
);
176 n
= res_nsend(statp
, packet
, n
, answer
, sizeof answer
);
178 DPRINTF(("res_nsend: send error, n=%d (%s)\n",
179 n
, strerror(errno
)));
182 if (((HEADER
*)answer
)->rcode
== NOERROR
)
185 /* Restore resolver's nameserver set. */
186 res_setservers(statp
, nsaddrs
, nscount
);
190 while (!EMPTY(zgrps
)) {
192 if ((zptr
->z_flags
& ZG_F_ZONESECTADDED
) != 0)
193 res_freeupdrec(HEAD(zptr
->z_rrlist
));
194 UNLINK(zgrps
, zptr
, z_link
);
198 res_setservers(statp
, nsaddrs
, nscount
);
207 res_dprintf(const char *fmt
, ...) {
211 fputs(";; res_nupdate: ", stderr
);
212 vfprintf(stderr
, fmt
, ap
);