xref: /onnv-gate/usr/src/lib/libresolv2/common/resolv/res_update.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate #if !defined(lint) && !defined(SABER)
2*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: res_update.c,v 1.13 2005/04/27 04:56:43 sra Exp $";
30Sstevel@tonic-gate #endif /* not lint */
40Sstevel@tonic-gate 
50Sstevel@tonic-gate /*
6*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
70Sstevel@tonic-gate  * Copyright (c) 1996-1999 by Internet Software Consortium.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
100Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
110Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
120Sstevel@tonic-gate  *
13*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
16*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
22*11038SRao.Shoaib@Sun.COM /*! \file
23*11038SRao.Shoaib@Sun.COM  * \brief
240Sstevel@tonic-gate  * Based on the Dynamic DNS reference implementation by Viraj Bais
25*11038SRao.Shoaib@Sun.COM  * <viraj_bais@ccm.fm.intel.com>
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include "port_before.h"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/socket.h>
320Sstevel@tonic-gate #include <sys/time.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <netinet/in.h>
350Sstevel@tonic-gate #include <arpa/inet.h>
360Sstevel@tonic-gate #include <arpa/nameser.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <limits.h>
400Sstevel@tonic-gate #include <netdb.h>
410Sstevel@tonic-gate #include <res_update.h>
420Sstevel@tonic-gate #include <stdarg.h>
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <stdlib.h>
450Sstevel@tonic-gate #include <string.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <isc/list.h>
480Sstevel@tonic-gate #include <resolv.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include "port_after.h"
510Sstevel@tonic-gate #include "res_private.h"
520Sstevel@tonic-gate 
53*11038SRao.Shoaib@Sun.COM /*%
540Sstevel@tonic-gate  * Separate a linked list of records into groups so that all records
550Sstevel@tonic-gate  * in a group will belong to a single zone on the nameserver.
560Sstevel@tonic-gate  * Create a dynamic update packet for each zone and send it to the
570Sstevel@tonic-gate  * nameservers for that zone, and await answer.
580Sstevel@tonic-gate  * Abort if error occurs in updating any zone.
590Sstevel@tonic-gate  * Return the number of zones updated on success, < 0 on error.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * On error, caller must deal with the unsynchronized zones
620Sstevel@tonic-gate  * eg. an A record might have been successfully added to the forward
630Sstevel@tonic-gate  * zone but the corresponding PTR record would be missing if error
640Sstevel@tonic-gate  * was encountered while updating the reverse zone.
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate struct zonegrp {
680Sstevel@tonic-gate 	char			z_origin[MAXDNAME];
690Sstevel@tonic-gate 	ns_class		z_class;
700Sstevel@tonic-gate 	union res_sockaddr_union z_nsaddrs[MAXNS];
710Sstevel@tonic-gate 	int			z_nscount;
720Sstevel@tonic-gate 	int			z_flags;
730Sstevel@tonic-gate 	LIST(ns_updrec)		z_rrlist;
740Sstevel@tonic-gate 	LINK(struct zonegrp)	z_link;
750Sstevel@tonic-gate };
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #define ZG_F_ZONESECTADDED	0x0001
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /* Forward. */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static void	res_dprintf(const char *, ...) ISC_FORMAT_PRINTF(1, 2);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /* Macros. */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define DPRINTF(x) do {\
860Sstevel@tonic-gate 		int save_errno = errno; \
87*11038SRao.Shoaib@Sun.COM 		if ((statp->options & RES_DEBUG) != 0U) res_dprintf x; \
880Sstevel@tonic-gate 		errno = save_errno; \
890Sstevel@tonic-gate 	} while (0)
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /* Public. */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate int
res_nupdate(res_state statp,ns_updrec * rrecp_in,ns_tsig_key * key)940Sstevel@tonic-gate res_nupdate(res_state statp, ns_updrec *rrecp_in, ns_tsig_key *key) {
950Sstevel@tonic-gate 	ns_updrec *rrecp;
960Sstevel@tonic-gate 	u_char answer[PACKETSZ];
970Sstevel@tonic-gate 	u_char *packet;
980Sstevel@tonic-gate 	struct zonegrp *zptr, tgrp;
990Sstevel@tonic-gate 	LIST(struct zonegrp) zgrps;
1000Sstevel@tonic-gate 	int nzones = 0, nscount = 0, n;
1010Sstevel@tonic-gate 	union res_sockaddr_union nsaddrs[MAXNS];
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	packet = malloc(NS_MAXMSG);
1040Sstevel@tonic-gate 	if (packet == NULL) {
1050Sstevel@tonic-gate 		DPRINTF(("malloc failed"));
1060Sstevel@tonic-gate 		return (0);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 	/* Thread all of the updates onto a list of groups. */
1090Sstevel@tonic-gate 	INIT_LIST(zgrps);
1100Sstevel@tonic-gate 	memset(&tgrp, 0, sizeof (tgrp));
1110Sstevel@tonic-gate 	for (rrecp = rrecp_in; rrecp;
1120Sstevel@tonic-gate 	     rrecp = LINKED(rrecp, r_link) ? NEXT(rrecp, r_link) : NULL) {
1130Sstevel@tonic-gate 		int nscnt;
1140Sstevel@tonic-gate 		/* Find the origin for it if there is one. */
1150Sstevel@tonic-gate 		tgrp.z_class = rrecp->r_class;
1160Sstevel@tonic-gate 		nscnt = res_findzonecut2(statp, rrecp->r_dname, tgrp.z_class,
1170Sstevel@tonic-gate 					 RES_EXHAUSTIVE, tgrp.z_origin,
1180Sstevel@tonic-gate 					 sizeof tgrp.z_origin,
1190Sstevel@tonic-gate 					 tgrp.z_nsaddrs, MAXNS);
1200Sstevel@tonic-gate 		if (nscnt <= 0) {
1210Sstevel@tonic-gate 			DPRINTF(("res_findzonecut failed (%d)", nscnt));
1220Sstevel@tonic-gate 			goto done;
1230Sstevel@tonic-gate 		}
1240Sstevel@tonic-gate 		tgrp.z_nscount = nscnt;
1250Sstevel@tonic-gate 		/* Find the group for it if there is one. */
1260Sstevel@tonic-gate 		for (zptr = HEAD(zgrps); zptr != NULL; zptr = NEXT(zptr, z_link))
1270Sstevel@tonic-gate 			if (ns_samename(tgrp.z_origin, zptr->z_origin) == 1 &&
1280Sstevel@tonic-gate 			    tgrp.z_class == zptr->z_class)
1290Sstevel@tonic-gate 				break;
1300Sstevel@tonic-gate 		/* Make a group for it if there isn't one. */
1310Sstevel@tonic-gate 		if (zptr == NULL) {
1320Sstevel@tonic-gate 			zptr = malloc(sizeof *zptr);
1330Sstevel@tonic-gate 			if (zptr == NULL) {
1340Sstevel@tonic-gate 				DPRINTF(("malloc failed"));
1350Sstevel@tonic-gate 				goto done;
1360Sstevel@tonic-gate 			}
1370Sstevel@tonic-gate 			*zptr = tgrp;
1380Sstevel@tonic-gate 			zptr->z_flags = 0;
1390Sstevel@tonic-gate 			INIT_LINK(zptr, z_link);
1400Sstevel@tonic-gate 			INIT_LIST(zptr->z_rrlist);
1410Sstevel@tonic-gate 			APPEND(zgrps, zptr, z_link);
1420Sstevel@tonic-gate 		}
1430Sstevel@tonic-gate 		/* Thread this rrecp onto the right group. */
1440Sstevel@tonic-gate 		APPEND(zptr->z_rrlist, rrecp, r_glink);
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	for (zptr = HEAD(zgrps); zptr != NULL; zptr = NEXT(zptr, z_link)) {
1480Sstevel@tonic-gate 		/* Construct zone section and prepend it. */
1490Sstevel@tonic-gate 		rrecp = res_mkupdrec(ns_s_zn, zptr->z_origin,
1500Sstevel@tonic-gate 				     zptr->z_class, ns_t_soa, 0);
1510Sstevel@tonic-gate 		if (rrecp == NULL) {
1520Sstevel@tonic-gate 			DPRINTF(("res_mkupdrec failed"));
1530Sstevel@tonic-gate 			goto done;
1540Sstevel@tonic-gate 		}
1550Sstevel@tonic-gate 		PREPEND(zptr->z_rrlist, rrecp, r_glink);
1560Sstevel@tonic-gate 		zptr->z_flags |= ZG_F_ZONESECTADDED;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 		/* Marshall the update message. */
1590Sstevel@tonic-gate 		n = res_nmkupdate(statp, HEAD(zptr->z_rrlist),
1600Sstevel@tonic-gate 				  packet, NS_MAXMSG);
1610Sstevel@tonic-gate 		DPRINTF(("res_mkupdate -> %d", n));
1620Sstevel@tonic-gate 		if (n < 0)
1630Sstevel@tonic-gate 			goto done;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 		/* Temporarily replace the resolver's nameserver set. */
1660Sstevel@tonic-gate 		nscount = res_getservers(statp, nsaddrs, MAXNS);
1670Sstevel@tonic-gate 		res_setservers(statp, zptr->z_nsaddrs, zptr->z_nscount);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 		/* Send the update and remember the result. */
1700Sstevel@tonic-gate 		if (key != NULL)
1710Sstevel@tonic-gate 			n = res_nsendsigned(statp, packet, n, key,
1720Sstevel@tonic-gate 					    answer, sizeof answer);
1730Sstevel@tonic-gate 		else
1740Sstevel@tonic-gate 			n = res_nsend(statp, packet, n, answer, sizeof answer);
1750Sstevel@tonic-gate 		if (n < 0) {
1760Sstevel@tonic-gate 			DPRINTF(("res_nsend: send error, n=%d (%s)\n",
1770Sstevel@tonic-gate 				 n, strerror(errno)));
1780Sstevel@tonic-gate 			goto done;
1790Sstevel@tonic-gate 		}
1800Sstevel@tonic-gate 		if (((HEADER *)answer)->rcode == NOERROR)
1810Sstevel@tonic-gate 			nzones++;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 		/* Restore resolver's nameserver set. */
1840Sstevel@tonic-gate 		res_setservers(statp, nsaddrs, nscount);
1850Sstevel@tonic-gate 		nscount = 0;
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate  done:
1880Sstevel@tonic-gate 	while (!EMPTY(zgrps)) {
1890Sstevel@tonic-gate 		zptr = HEAD(zgrps);
1900Sstevel@tonic-gate 		if ((zptr->z_flags & ZG_F_ZONESECTADDED) != 0)
1910Sstevel@tonic-gate 			res_freeupdrec(HEAD(zptr->z_rrlist));
1920Sstevel@tonic-gate 		UNLINK(zgrps, zptr, z_link);
1930Sstevel@tonic-gate 		free(zptr);
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 	if (nscount != 0)
1960Sstevel@tonic-gate 		res_setservers(statp, nsaddrs, nscount);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	free(packet);
1990Sstevel@tonic-gate 	return (nzones);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate /* Private. */
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate static void
res_dprintf(const char * fmt,...)2050Sstevel@tonic-gate res_dprintf(const char *fmt, ...) {
2060Sstevel@tonic-gate 	va_list ap;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	va_start(ap, fmt);
2090Sstevel@tonic-gate 	fputs(";; res_nupdate: ", stderr);
2100Sstevel@tonic-gate 	vfprintf(stderr, fmt, ap);
2110Sstevel@tonic-gate 	fputc('\n', stderr);
2120Sstevel@tonic-gate 	va_end(ap);
2130Sstevel@tonic-gate }
214