[028aaaf] | 1 | #include "port_before.h" |
---|
| 2 | |
---|
| 3 | #if !defined(lint) && !defined(SABER) |
---|
| 4 | static const char rcsid[] = "$Id: res_update.c,v 1.12.18.1 2005/04/27 05:01:12 sra Exp $"; |
---|
| 5 | #endif /* not lint */ |
---|
| 6 | |
---|
| 7 | /* |
---|
| 8 | * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") |
---|
| 9 | * Copyright (c) 1996-1999 by Internet Software Consortium. |
---|
| 10 | * |
---|
| 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. |
---|
| 14 | * |
---|
| 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. |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | /*! \file |
---|
| 25 | * \brief |
---|
| 26 | * Based on the Dynamic DNS reference implementation by Viraj Bais |
---|
| 27 | * <viraj_bais@ccm.fm.intel.com> |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #include <sys/cdefs.h> |
---|
| 31 | __FBSDID("$FreeBSD$"); |
---|
| 32 | |
---|
| 33 | #include "port_before.h" |
---|
| 34 | |
---|
| 35 | #include <sys/param.h> |
---|
| 36 | #include <sys/socket.h> |
---|
| 37 | #include <sys/time.h> |
---|
| 38 | |
---|
| 39 | #include <netinet/in.h> |
---|
| 40 | #include <arpa/inet.h> |
---|
| 41 | #include <arpa/nameser.h> |
---|
| 42 | |
---|
| 43 | #include <errno.h> |
---|
| 44 | #include <limits.h> |
---|
| 45 | #include <netdb.h> |
---|
| 46 | #include <res_update.h> |
---|
| 47 | #include <stdarg.h> |
---|
| 48 | #include <stdio.h> |
---|
| 49 | #include <stdlib.h> |
---|
| 50 | #include <string.h> |
---|
| 51 | |
---|
[236e427] | 52 | #ifdef __rtems__ |
---|
| 53 | #include <isc/print.h> |
---|
| 54 | #endif |
---|
[028aaaf] | 55 | #include <isc/list.h> |
---|
| 56 | #include <resolv.h> |
---|
| 57 | |
---|
| 58 | #include "port_after.h" |
---|
| 59 | #include "res_private.h" |
---|
| 60 | |
---|
| 61 | /*% |
---|
| 62 | * Separate a linked list of records into groups so that all records |
---|
| 63 | * in a group will belong to a single zone on the nameserver. |
---|
| 64 | * Create a dynamic update packet for each zone and send it to the |
---|
| 65 | * nameservers for that zone, and await answer. |
---|
| 66 | * Abort if error occurs in updating any zone. |
---|
| 67 | * Return the number of zones updated on success, < 0 on error. |
---|
| 68 | * |
---|
| 69 | * On error, caller must deal with the unsynchronized zones |
---|
| 70 | * eg. an A record might have been successfully added to the forward |
---|
| 71 | * zone but the corresponding PTR record would be missing if error |
---|
| 72 | * was encountered while updating the reverse zone. |
---|
| 73 | */ |
---|
| 74 | |
---|
| 75 | struct zonegrp { |
---|
| 76 | char z_origin[MAXDNAME]; |
---|
| 77 | ns_class z_class; |
---|
| 78 | union res_sockaddr_union z_nsaddrs[MAXNS]; |
---|
| 79 | int z_nscount; |
---|
| 80 | int z_flags; |
---|
| 81 | LIST(ns_updrec) z_rrlist; |
---|
| 82 | LINK(struct zonegrp) z_link; |
---|
| 83 | }; |
---|
| 84 | |
---|
| 85 | #define ZG_F_ZONESECTADDED 0x0001 |
---|
| 86 | |
---|
| 87 | /* Forward. */ |
---|
| 88 | |
---|
| 89 | static void res_dprintf(const char *, ...) ISC_FORMAT_PRINTF(1, 2); |
---|
| 90 | |
---|
| 91 | /* Macros. */ |
---|
| 92 | |
---|
| 93 | #define DPRINTF(x) do {\ |
---|
| 94 | int save_errno = errno; \ |
---|
| 95 | if ((statp->options & RES_DEBUG) != 0U) res_dprintf x; \ |
---|
| 96 | errno = save_errno; \ |
---|
| 97 | } while (0) |
---|
| 98 | |
---|
| 99 | /* Public. */ |
---|
| 100 | |
---|
| 101 | int |
---|
| 102 | res_nupdate(res_state statp, ns_updrec *rrecp_in, ns_tsig_key *key) { |
---|
| 103 | ns_updrec *rrecp; |
---|
| 104 | u_char answer[PACKETSZ]; |
---|
| 105 | u_char *packet; |
---|
| 106 | struct zonegrp *zptr, tgrp; |
---|
| 107 | LIST(struct zonegrp) zgrps; |
---|
| 108 | int nzones = 0, nscount = 0, n; |
---|
| 109 | union res_sockaddr_union nsaddrs[MAXNS]; |
---|
| 110 | |
---|
| 111 | packet = malloc(NS_MAXMSG); |
---|
| 112 | if (packet == NULL) { |
---|
| 113 | DPRINTF(("malloc failed")); |
---|
| 114 | return (0); |
---|
| 115 | } |
---|
| 116 | /* Thread all of the updates onto a list of groups. */ |
---|
| 117 | INIT_LIST(zgrps); |
---|
| 118 | memset(&tgrp, 0, sizeof (tgrp)); |
---|
| 119 | for (rrecp = rrecp_in; rrecp; |
---|
| 120 | rrecp = LINKED(rrecp, r_link) ? NEXT(rrecp, r_link) : NULL) { |
---|
| 121 | int nscnt; |
---|
| 122 | /* Find the origin for it if there is one. */ |
---|
| 123 | tgrp.z_class = rrecp->r_class; |
---|
| 124 | nscnt = res_findzonecut2(statp, rrecp->r_dname, tgrp.z_class, |
---|
| 125 | RES_EXHAUSTIVE, tgrp.z_origin, |
---|
| 126 | sizeof tgrp.z_origin, |
---|
| 127 | tgrp.z_nsaddrs, MAXNS); |
---|
| 128 | if (nscnt <= 0) { |
---|
| 129 | DPRINTF(("res_findzonecut failed (%d)", nscnt)); |
---|
| 130 | goto done; |
---|
| 131 | } |
---|
| 132 | tgrp.z_nscount = nscnt; |
---|
| 133 | /* Find the group for it if there is one. */ |
---|
| 134 | for (zptr = HEAD(zgrps); zptr != NULL; zptr = NEXT(zptr, z_link)) |
---|
| 135 | if (ns_samename(tgrp.z_origin, zptr->z_origin) == 1 && |
---|
| 136 | tgrp.z_class == zptr->z_class) |
---|
| 137 | break; |
---|
| 138 | /* Make a group for it if there isn't one. */ |
---|
| 139 | if (zptr == NULL) { |
---|
| 140 | zptr = malloc(sizeof *zptr); |
---|
| 141 | if (zptr == NULL) { |
---|
| 142 | DPRINTF(("malloc failed")); |
---|
| 143 | goto done; |
---|
| 144 | } |
---|
| 145 | *zptr = tgrp; |
---|
| 146 | zptr->z_flags = 0; |
---|
| 147 | INIT_LINK(zptr, z_link); |
---|
| 148 | INIT_LIST(zptr->z_rrlist); |
---|
| 149 | APPEND(zgrps, zptr, z_link); |
---|
| 150 | } |
---|
| 151 | /* Thread this rrecp onto the right group. */ |
---|
| 152 | APPEND(zptr->z_rrlist, rrecp, r_glink); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | for (zptr = HEAD(zgrps); zptr != NULL; zptr = NEXT(zptr, z_link)) { |
---|
| 156 | /* Construct zone section and prepend it. */ |
---|
| 157 | rrecp = res_mkupdrec(ns_s_zn, zptr->z_origin, |
---|
| 158 | zptr->z_class, ns_t_soa, 0); |
---|
| 159 | if (rrecp == NULL) { |
---|
| 160 | DPRINTF(("res_mkupdrec failed")); |
---|
| 161 | goto done; |
---|
| 162 | } |
---|
| 163 | PREPEND(zptr->z_rrlist, rrecp, r_glink); |
---|
| 164 | zptr->z_flags |= ZG_F_ZONESECTADDED; |
---|
| 165 | |
---|
| 166 | /* Marshall the update message. */ |
---|
| 167 | n = res_nmkupdate(statp, HEAD(zptr->z_rrlist), |
---|
| 168 | packet, NS_MAXMSG); |
---|
| 169 | DPRINTF(("res_mkupdate -> %d", n)); |
---|
| 170 | if (n < 0) |
---|
| 171 | goto done; |
---|
| 172 | |
---|
| 173 | /* Temporarily replace the resolver's nameserver set. */ |
---|
| 174 | nscount = res_getservers(statp, nsaddrs, MAXNS); |
---|
| 175 | res_setservers(statp, zptr->z_nsaddrs, zptr->z_nscount); |
---|
| 176 | |
---|
| 177 | /* Send the update and remember the result. */ |
---|
| 178 | if (key != NULL) { |
---|
| 179 | #ifdef _LIBC |
---|
| 180 | DPRINTF(("TSIG is not supported\n")); |
---|
| 181 | RES_SET_H_ERRNO(statp, NO_RECOVERY); |
---|
| 182 | goto done; |
---|
| 183 | #else |
---|
| 184 | n = res_nsendsigned(statp, packet, n, key, |
---|
| 185 | answer, sizeof answer); |
---|
| 186 | #endif |
---|
| 187 | } else |
---|
| 188 | n = res_nsend(statp, packet, n, answer, sizeof answer); |
---|
| 189 | if (n < 0) { |
---|
| 190 | DPRINTF(("res_nsend: send error, n=%d (%s)\n", |
---|
| 191 | n, strerror(errno))); |
---|
| 192 | goto done; |
---|
| 193 | } |
---|
| 194 | if (((HEADER *)answer)->rcode == NOERROR) |
---|
| 195 | nzones++; |
---|
| 196 | |
---|
| 197 | /* Restore resolver's nameserver set. */ |
---|
| 198 | res_setservers(statp, nsaddrs, nscount); |
---|
| 199 | nscount = 0; |
---|
| 200 | } |
---|
| 201 | done: |
---|
| 202 | while (!EMPTY(zgrps)) { |
---|
| 203 | zptr = HEAD(zgrps); |
---|
| 204 | if ((zptr->z_flags & ZG_F_ZONESECTADDED) != 0) |
---|
| 205 | res_freeupdrec(HEAD(zptr->z_rrlist)); |
---|
| 206 | UNLINK(zgrps, zptr, z_link); |
---|
| 207 | free(zptr); |
---|
| 208 | } |
---|
| 209 | if (nscount != 0) |
---|
| 210 | res_setservers(statp, nsaddrs, nscount); |
---|
| 211 | |
---|
| 212 | free(packet); |
---|
| 213 | return (nzones); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | /* Private. */ |
---|
| 217 | |
---|
| 218 | static void |
---|
| 219 | res_dprintf(const char *fmt, ...) { |
---|
| 220 | va_list ap; |
---|
| 221 | |
---|
| 222 | va_start(ap, fmt); |
---|
| 223 | fputs(";; res_nupdate: ", stderr); |
---|
| 224 | vfprintf(stderr, fmt, ap); |
---|
| 225 | fputc('\n', stderr); |
---|
| 226 | va_end(ap); |
---|
| 227 | } |
---|