17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1996-1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
18*9525b14bSRao Shoaib /*! \file
19*9525b14bSRao Shoaib  * \brief
207c478bd9Sstevel@tonic-gate  * dns.c --- this is the top-level accessor function for the dns
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #include "port_before.h"
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <stdlib.h>
267c478bd9Sstevel@tonic-gate #include <string.h>
277c478bd9Sstevel@tonic-gate #include <errno.h>
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <netinet/in.h>
317c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
327c478bd9Sstevel@tonic-gate #include <resolv.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <resolv.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
377c478bd9Sstevel@tonic-gate #include <irs.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "port_after.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "irs_p.h"
427c478bd9Sstevel@tonic-gate #include "hesiod.h"
437c478bd9Sstevel@tonic-gate #include "dns_p.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /* forward */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static void		dns_close(struct irs_acc *);
487c478bd9Sstevel@tonic-gate static struct __res_state *	dns_res_get(struct irs_acc *);
497c478bd9Sstevel@tonic-gate static void		dns_res_set(struct irs_acc *, struct __res_state *,
507c478bd9Sstevel@tonic-gate 				void (*)(void *));
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* public */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate struct irs_acc *
irs_dns_acc(const char * options)557c478bd9Sstevel@tonic-gate irs_dns_acc(const char *options) {
567c478bd9Sstevel@tonic-gate 	struct irs_acc *acc;
577c478bd9Sstevel@tonic-gate 	struct dns_p *dns;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	UNUSED(options);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	if (!(acc = memget(sizeof *acc))) {
627c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
637c478bd9Sstevel@tonic-gate 		return (NULL);
647c478bd9Sstevel@tonic-gate 	}
657c478bd9Sstevel@tonic-gate 	memset(acc, 0x5e, sizeof *acc);
667c478bd9Sstevel@tonic-gate 	if (!(dns = memget(sizeof *dns))) {
677c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
687c478bd9Sstevel@tonic-gate 		memput(acc, sizeof *acc);
697c478bd9Sstevel@tonic-gate 		return (NULL);
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 	memset(dns, 0x5e, sizeof *dns);
727c478bd9Sstevel@tonic-gate 	dns->res = NULL;
737c478bd9Sstevel@tonic-gate 	dns->free_res = NULL;
747c478bd9Sstevel@tonic-gate 	if (hesiod_init(&dns->hes_ctx) < 0) {
757c478bd9Sstevel@tonic-gate 		/*
767c478bd9Sstevel@tonic-gate 		 * We allow the dns accessor class to initialize
777c478bd9Sstevel@tonic-gate 		 * despite hesiod failing to initialize correctly,
787c478bd9Sstevel@tonic-gate 		 * since dns host queries don't depend on hesiod.
797c478bd9Sstevel@tonic-gate 		 */
807c478bd9Sstevel@tonic-gate 		dns->hes_ctx = NULL;
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 	acc->private = dns;
837c478bd9Sstevel@tonic-gate #ifdef WANT_IRS_GR
847c478bd9Sstevel@tonic-gate 	acc->gr_map = irs_dns_gr;
857c478bd9Sstevel@tonic-gate #else
867c478bd9Sstevel@tonic-gate 	acc->gr_map = NULL;
877c478bd9Sstevel@tonic-gate #endif
887c478bd9Sstevel@tonic-gate #ifdef WANT_IRS_PW
897c478bd9Sstevel@tonic-gate 	acc->pw_map = irs_dns_pw;
907c478bd9Sstevel@tonic-gate #else
917c478bd9Sstevel@tonic-gate 	acc->pw_map = NULL;
927c478bd9Sstevel@tonic-gate #endif
937c478bd9Sstevel@tonic-gate 	acc->sv_map = irs_dns_sv;
947c478bd9Sstevel@tonic-gate 	acc->pr_map = irs_dns_pr;
957c478bd9Sstevel@tonic-gate 	acc->ho_map = irs_dns_ho;
967c478bd9Sstevel@tonic-gate 	acc->nw_map = irs_dns_nw;
977c478bd9Sstevel@tonic-gate 	acc->ng_map = irs_nul_ng;
987c478bd9Sstevel@tonic-gate 	acc->res_get = dns_res_get;
997c478bd9Sstevel@tonic-gate 	acc->res_set = dns_res_set;
1007c478bd9Sstevel@tonic-gate 	acc->close = dns_close;
1017c478bd9Sstevel@tonic-gate 	return (acc);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /* methods */
1057c478bd9Sstevel@tonic-gate static struct __res_state *
dns_res_get(struct irs_acc * this)1067c478bd9Sstevel@tonic-gate dns_res_get(struct irs_acc *this) {
1077c478bd9Sstevel@tonic-gate 	struct dns_p *dns = (struct dns_p *)this->private;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if (dns->res == NULL) {
1107c478bd9Sstevel@tonic-gate 		struct __res_state *res;
1117c478bd9Sstevel@tonic-gate 		res = (struct __res_state *)malloc(sizeof *res);
1127c478bd9Sstevel@tonic-gate 		if (res == NULL)
1137c478bd9Sstevel@tonic-gate 			return (NULL);
114*9525b14bSRao Shoaib 		memset(res, 0, sizeof *res);
1157c478bd9Sstevel@tonic-gate 		dns_res_set(this, res, free);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
118*9525b14bSRao Shoaib 	if ((dns->res->options & RES_INIT) == 0U &&
1197c478bd9Sstevel@tonic-gate 	    res_ninit(dns->res) < 0)
1207c478bd9Sstevel@tonic-gate 		return (NULL);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	return (dns->res);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static void
dns_res_set(struct irs_acc * this,struct __res_state * res,void (* free_res)(void *))1267c478bd9Sstevel@tonic-gate dns_res_set(struct irs_acc *this, struct __res_state *res,
1277c478bd9Sstevel@tonic-gate 	    void (*free_res)(void *)) {
1287c478bd9Sstevel@tonic-gate 	struct dns_p *dns = (struct dns_p *)this->private;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (dns->res && dns->free_res) {
1317c478bd9Sstevel@tonic-gate 		res_nclose(dns->res);
1327c478bd9Sstevel@tonic-gate 		(*dns->free_res)(dns->res);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 	dns->res = res;
1357c478bd9Sstevel@tonic-gate 	dns->free_res = free_res;
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static void
dns_close(struct irs_acc * this)1397c478bd9Sstevel@tonic-gate dns_close(struct irs_acc *this) {
1407c478bd9Sstevel@tonic-gate 	struct dns_p *dns;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	dns = (struct dns_p *)this->private;
1437c478bd9Sstevel@tonic-gate 	if (dns->res && dns->free_res)
1447c478bd9Sstevel@tonic-gate 		(*dns->free_res)(dns->res);
1457c478bd9Sstevel@tonic-gate 	if (dns->hes_ctx)
1467c478bd9Sstevel@tonic-gate 		hesiod_end(dns->hes_ctx);
1477c478bd9Sstevel@tonic-gate 	memput(dns, sizeof *dns);
1487c478bd9Sstevel@tonic-gate 	memput(this, sizeof *this);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
151