xref: /titanic_52/usr/src/lib/libresolv2/common/resolv/res_update.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
17c478bd9Sstevel@tonic-gate #if !defined(lint) && !defined(SABER)
2*9525b14bSRao Shoaib static const char rcsid[] = "$Id: res_update.c,v 1.13 2005/04/27 04:56:43 sra Exp $";
37c478bd9Sstevel@tonic-gate #endif /* not lint */
47c478bd9Sstevel@tonic-gate 
57c478bd9Sstevel@tonic-gate /*
6*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1996-1999 by Internet Software Consortium.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
107c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
117c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
127c478bd9Sstevel@tonic-gate  *
13*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
16*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
22*9525b14bSRao Shoaib /*! \file
23*9525b14bSRao Shoaib  * \brief
247c478bd9Sstevel@tonic-gate  * Based on the Dynamic DNS reference implementation by Viraj Bais
25*9525b14bSRao Shoaib  * <viraj_bais@ccm.fm.intel.com>
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include "port_before.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/socket.h>
327c478bd9Sstevel@tonic-gate #include <sys/time.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <netinet/in.h>
357c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
367c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <limits.h>
407c478bd9Sstevel@tonic-gate #include <netdb.h>
417c478bd9Sstevel@tonic-gate #include <res_update.h>
427c478bd9Sstevel@tonic-gate #include <stdarg.h>
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <string.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <isc/list.h>
487c478bd9Sstevel@tonic-gate #include <resolv.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include "port_after.h"
517c478bd9Sstevel@tonic-gate #include "res_private.h"
527c478bd9Sstevel@tonic-gate 
53*9525b14bSRao Shoaib /*%
547c478bd9Sstevel@tonic-gate  * Separate a linked list of records into groups so that all records
557c478bd9Sstevel@tonic-gate  * in a group will belong to a single zone on the nameserver.
567c478bd9Sstevel@tonic-gate  * Create a dynamic update packet for each zone and send it to the
577c478bd9Sstevel@tonic-gate  * nameservers for that zone, and await answer.
587c478bd9Sstevel@tonic-gate  * Abort if error occurs in updating any zone.
597c478bd9Sstevel@tonic-gate  * Return the number of zones updated on success, < 0 on error.
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  * On error, caller must deal with the unsynchronized zones
627c478bd9Sstevel@tonic-gate  * eg. an A record might have been successfully added to the forward
637c478bd9Sstevel@tonic-gate  * zone but the corresponding PTR record would be missing if error
647c478bd9Sstevel@tonic-gate  * was encountered while updating the reverse zone.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate struct zonegrp {
687c478bd9Sstevel@tonic-gate 	char			z_origin[MAXDNAME];
697c478bd9Sstevel@tonic-gate 	ns_class		z_class;
707c478bd9Sstevel@tonic-gate 	union res_sockaddr_union z_nsaddrs[MAXNS];
717c478bd9Sstevel@tonic-gate 	int			z_nscount;
727c478bd9Sstevel@tonic-gate 	int			z_flags;
737c478bd9Sstevel@tonic-gate 	LIST(ns_updrec)		z_rrlist;
747c478bd9Sstevel@tonic-gate 	LINK(struct zonegrp)	z_link;
757c478bd9Sstevel@tonic-gate };
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #define ZG_F_ZONESECTADDED	0x0001
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /* Forward. */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static void	res_dprintf(const char *, ...) ISC_FORMAT_PRINTF(1, 2);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /* Macros. */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #define DPRINTF(x) do {\
867c478bd9Sstevel@tonic-gate 		int save_errno = errno; \
87*9525b14bSRao Shoaib 		if ((statp->options & RES_DEBUG) != 0U) res_dprintf x; \
887c478bd9Sstevel@tonic-gate 		errno = save_errno; \
897c478bd9Sstevel@tonic-gate 	} while (0)
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /* Public. */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate int
947c478bd9Sstevel@tonic-gate res_nupdate(res_state statp, ns_updrec *rrecp_in, ns_tsig_key *key) {
957c478bd9Sstevel@tonic-gate 	ns_updrec *rrecp;
967c478bd9Sstevel@tonic-gate 	u_char answer[PACKETSZ];
977c478bd9Sstevel@tonic-gate 	u_char *packet;
987c478bd9Sstevel@tonic-gate 	struct zonegrp *zptr, tgrp;
997c478bd9Sstevel@tonic-gate 	LIST(struct zonegrp) zgrps;
1007c478bd9Sstevel@tonic-gate 	int nzones = 0, nscount = 0, n;
1017c478bd9Sstevel@tonic-gate 	union res_sockaddr_union nsaddrs[MAXNS];
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	packet = malloc(NS_MAXMSG);
1047c478bd9Sstevel@tonic-gate 	if (packet == NULL) {
1057c478bd9Sstevel@tonic-gate 		DPRINTF(("malloc failed"));
1067c478bd9Sstevel@tonic-gate 		return (0);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 	/* Thread all of the updates onto a list of groups. */
1097c478bd9Sstevel@tonic-gate 	INIT_LIST(zgrps);
1107c478bd9Sstevel@tonic-gate 	memset(&tgrp, 0, sizeof (tgrp));
1117c478bd9Sstevel@tonic-gate 	for (rrecp = rrecp_in; rrecp;
1127c478bd9Sstevel@tonic-gate 	     rrecp = LINKED(rrecp, r_link) ? NEXT(rrecp, r_link) : NULL) {
1137c478bd9Sstevel@tonic-gate 		int nscnt;
1147c478bd9Sstevel@tonic-gate 		/* Find the origin for it if there is one. */
1157c478bd9Sstevel@tonic-gate 		tgrp.z_class = rrecp->r_class;
1167c478bd9Sstevel@tonic-gate 		nscnt = res_findzonecut2(statp, rrecp->r_dname, tgrp.z_class,
1177c478bd9Sstevel@tonic-gate 					 RES_EXHAUSTIVE, tgrp.z_origin,
1187c478bd9Sstevel@tonic-gate 					 sizeof tgrp.z_origin,
1197c478bd9Sstevel@tonic-gate 					 tgrp.z_nsaddrs, MAXNS);
1207c478bd9Sstevel@tonic-gate 		if (nscnt <= 0) {
1217c478bd9Sstevel@tonic-gate 			DPRINTF(("res_findzonecut failed (%d)", nscnt));
1227c478bd9Sstevel@tonic-gate 			goto done;
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 		tgrp.z_nscount = nscnt;
1257c478bd9Sstevel@tonic-gate 		/* Find the group for it if there is one. */
1267c478bd9Sstevel@tonic-gate 		for (zptr = HEAD(zgrps); zptr != NULL; zptr = NEXT(zptr, z_link))
1277c478bd9Sstevel@tonic-gate 			if (ns_samename(tgrp.z_origin, zptr->z_origin) == 1 &&
1287c478bd9Sstevel@tonic-gate 			    tgrp.z_class == zptr->z_class)
1297c478bd9Sstevel@tonic-gate 				break;
1307c478bd9Sstevel@tonic-gate 		/* Make a group for it if there isn't one. */
1317c478bd9Sstevel@tonic-gate 		if (zptr == NULL) {
1327c478bd9Sstevel@tonic-gate 			zptr = malloc(sizeof *zptr);
1337c478bd9Sstevel@tonic-gate 			if (zptr == NULL) {
1347c478bd9Sstevel@tonic-gate 				DPRINTF(("malloc failed"));
1357c478bd9Sstevel@tonic-gate 				goto done;
1367c478bd9Sstevel@tonic-gate 			}
1377c478bd9Sstevel@tonic-gate 			*zptr = tgrp;
1387c478bd9Sstevel@tonic-gate 			zptr->z_flags = 0;
1397c478bd9Sstevel@tonic-gate 			INIT_LINK(zptr, z_link);
1407c478bd9Sstevel@tonic-gate 			INIT_LIST(zptr->z_rrlist);
1417c478bd9Sstevel@tonic-gate 			APPEND(zgrps, zptr, z_link);
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 		/* Thread this rrecp onto the right group. */
1447c478bd9Sstevel@tonic-gate 		APPEND(zptr->z_rrlist, rrecp, r_glink);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	for (zptr = HEAD(zgrps); zptr != NULL; zptr = NEXT(zptr, z_link)) {
1487c478bd9Sstevel@tonic-gate 		/* Construct zone section and prepend it. */
1497c478bd9Sstevel@tonic-gate 		rrecp = res_mkupdrec(ns_s_zn, zptr->z_origin,
1507c478bd9Sstevel@tonic-gate 				     zptr->z_class, ns_t_soa, 0);
1517c478bd9Sstevel@tonic-gate 		if (rrecp == NULL) {
1527c478bd9Sstevel@tonic-gate 			DPRINTF(("res_mkupdrec failed"));
1537c478bd9Sstevel@tonic-gate 			goto done;
1547c478bd9Sstevel@tonic-gate 		}
1557c478bd9Sstevel@tonic-gate 		PREPEND(zptr->z_rrlist, rrecp, r_glink);
1567c478bd9Sstevel@tonic-gate 		zptr->z_flags |= ZG_F_ZONESECTADDED;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		/* Marshall the update message. */
1597c478bd9Sstevel@tonic-gate 		n = res_nmkupdate(statp, HEAD(zptr->z_rrlist),
1607c478bd9Sstevel@tonic-gate 				  packet, NS_MAXMSG);
1617c478bd9Sstevel@tonic-gate 		DPRINTF(("res_mkupdate -> %d", n));
1627c478bd9Sstevel@tonic-gate 		if (n < 0)
1637c478bd9Sstevel@tonic-gate 			goto done;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		/* Temporarily replace the resolver's nameserver set. */
1667c478bd9Sstevel@tonic-gate 		nscount = res_getservers(statp, nsaddrs, MAXNS);
1677c478bd9Sstevel@tonic-gate 		res_setservers(statp, zptr->z_nsaddrs, zptr->z_nscount);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		/* Send the update and remember the result. */
1707c478bd9Sstevel@tonic-gate 		if (key != NULL)
1717c478bd9Sstevel@tonic-gate 			n = res_nsendsigned(statp, packet, n, key,
1727c478bd9Sstevel@tonic-gate 					    answer, sizeof answer);
1737c478bd9Sstevel@tonic-gate 		else
1747c478bd9Sstevel@tonic-gate 			n = res_nsend(statp, packet, n, answer, sizeof answer);
1757c478bd9Sstevel@tonic-gate 		if (n < 0) {
1767c478bd9Sstevel@tonic-gate 			DPRINTF(("res_nsend: send error, n=%d (%s)\n",
1777c478bd9Sstevel@tonic-gate 				 n, strerror(errno)));
1787c478bd9Sstevel@tonic-gate 			goto done;
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 		if (((HEADER *)answer)->rcode == NOERROR)
1817c478bd9Sstevel@tonic-gate 			nzones++;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		/* Restore resolver's nameserver set. */
1847c478bd9Sstevel@tonic-gate 		res_setservers(statp, nsaddrs, nscount);
1857c478bd9Sstevel@tonic-gate 		nscount = 0;
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate  done:
1887c478bd9Sstevel@tonic-gate 	while (!EMPTY(zgrps)) {
1897c478bd9Sstevel@tonic-gate 		zptr = HEAD(zgrps);
1907c478bd9Sstevel@tonic-gate 		if ((zptr->z_flags & ZG_F_ZONESECTADDED) != 0)
1917c478bd9Sstevel@tonic-gate 			res_freeupdrec(HEAD(zptr->z_rrlist));
1927c478bd9Sstevel@tonic-gate 		UNLINK(zgrps, zptr, z_link);
1937c478bd9Sstevel@tonic-gate 		free(zptr);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	if (nscount != 0)
1967c478bd9Sstevel@tonic-gate 		res_setservers(statp, nsaddrs, nscount);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	free(packet);
1997c478bd9Sstevel@tonic-gate 	return (nzones);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /* Private. */
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate static void
2057c478bd9Sstevel@tonic-gate res_dprintf(const char *fmt, ...) {
2067c478bd9Sstevel@tonic-gate 	va_list ap;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
2097c478bd9Sstevel@tonic-gate 	fputs(";; res_nupdate: ", stderr);
2107c478bd9Sstevel@tonic-gate 	vfprintf(stderr, fmt, ap);
2117c478bd9Sstevel@tonic-gate 	fputc('\n', stderr);
2127c478bd9Sstevel@tonic-gate 	va_end(ap);
2137c478bd9Sstevel@tonic-gate }
214