2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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.
20 * Based on the Dynamic DNS reference implementation by Viraj Bais
21 * <viraj_bais@ccm.fm.intel.com>
24 #include "port_before.h"
26 #include <sys/param.h>
27 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <arpa/nameser.h>
37 #include <res_update.h>
46 #include "port_after.h"
47 #include "res_private.h"
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.
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.
64 char z_origin
[MAXDNAME
];
66 union res_sockaddr_union z_nsaddrs
[MAXNS
];
69 LIST(ns_updrec
) z_rrlist
;
70 LINK(struct zonegrp
) z_link
;
73 #define ZG_F_ZONESECTADDED 0x0001
77 static void res_dprintf(const char *, ...) ISC_FORMAT_PRINTF(1, 2);
81 #define DPRINTF(x) do {\
82 int save_errno = errno; \
83 if ((statp->options & RES_DEBUG) != 0U) res_dprintf x; \
90 res_nupdate(res_state statp
, ns_updrec
*rrecp_in
, ns_tsig_key
*key
) {
92 u_char answer
[PACKETSZ
];
94 struct zonegrp
*zptr
, tgrp
;
95 LIST(struct zonegrp
) zgrps
;
96 int nzones
= 0, nscount
= 0, n
;
97 union res_sockaddr_union nsaddrs
[MAXNS
];
99 packet
= malloc(NS_MAXMSG
);
100 if (packet
== NULL
) {
101 DPRINTF(("malloc failed"));
104 /* Thread all of the updates onto a list of groups. */
106 memset(&tgrp
, 0, sizeof (tgrp
));
107 for (rrecp
= rrecp_in
; rrecp
;
108 rrecp
= LINKED(rrecp
, r_link
) ? NEXT(rrecp
, r_link
) : NULL
) {
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
);
117 DPRINTF(("res_findzonecut failed (%d)", nscnt
));
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
)
126 /* Make a group for it if there isn't one. */
128 zptr
= malloc(sizeof *zptr
);
130 DPRINTF(("malloc failed"));
135 INIT_LINK(zptr
, z_link
);
136 INIT_LIST(zptr
->z_rrlist
);
137 APPEND(zgrps
, zptr
, z_link
);
139 /* Thread this rrecp onto the right group. */
140 APPEND(zptr
->z_rrlist
, rrecp
, r_glink
);
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);
148 DPRINTF(("res_mkupdrec failed"));
151 PREPEND(zptr
->z_rrlist
, rrecp
, r_glink
);
152 zptr
->z_flags
|= ZG_F_ZONESECTADDED
;
154 /* Marshall the update message. */
155 n
= res_nmkupdate(statp
, HEAD(zptr
->z_rrlist
),
157 DPRINTF(("res_mkupdate -> %d", n
));
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
);
165 /* Send the update and remember the result. */
167 n
= res_nsendsigned(statp
, packet
, n
, key
,
168 answer
, sizeof answer
);
170 n
= res_nsend(statp
, packet
, n
, answer
, sizeof answer
);
172 DPRINTF(("res_nsend: send error, n=%d (%s)\n",
173 n
, strerror(errno
)));
176 if (((HEADER
*)answer
)->rcode
== NOERROR
)
179 /* Restore resolver's nameserver set. */
180 res_setservers(statp
, nsaddrs
, nscount
);
184 while (!EMPTY(zgrps
)) {
186 if ((zptr
->z_flags
& ZG_F_ZONESECTADDED
) != 0)
187 res_freeupdrec(HEAD(zptr
->z_rrlist
));
188 UNLINK(zgrps
, zptr
, z_link
);
192 res_setservers(statp
, nsaddrs
, nscount
);
201 res_dprintf(const char *fmt
, ...) {
205 fputs(";; res_nupdate: ", stderr
);
206 vfprintf(stderr
, fmt
, ap
);