17c478bd9Sstevel@tonic-gate /*
29525b14bSRao 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  *
99525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
109525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
119525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
129525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
139525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
149525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
159525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include "port_before.h"
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #ifdef WANT_IRS_NIS
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
237c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
247c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
257c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
32*55fea89dSDan Cross #include <netinet/in.h>
339525b14bSRao Shoaib #ifdef T_NULL
349525b14bSRao Shoaib #undef T_NULL			/* Silence re-definition warning of T_NULL. */
359525b14bSRao Shoaib #endif
367c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
377c478bd9Sstevel@tonic-gate #include <resolv.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
407c478bd9Sstevel@tonic-gate #include <irs.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "port_after.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include "irs_p.h"
457c478bd9Sstevel@tonic-gate #include "hesiod.h"
467c478bd9Sstevel@tonic-gate #include "nis_p.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /* Forward */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static void		nis_close(struct irs_acc *);
517c478bd9Sstevel@tonic-gate static struct __res_state * nis_res_get(struct irs_acc *);
527c478bd9Sstevel@tonic-gate static void		nis_res_set(struct irs_acc *, struct __res_state *,
537c478bd9Sstevel@tonic-gate 				void (*)(void *));
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* Public */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate struct irs_acc *
irs_nis_acc(const char * options)587c478bd9Sstevel@tonic-gate irs_nis_acc(const char *options) {
597c478bd9Sstevel@tonic-gate 	struct nis_p *nis;
607c478bd9Sstevel@tonic-gate 	struct irs_acc *acc;
617c478bd9Sstevel@tonic-gate 	char *domain;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	UNUSED(options);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if (yp_get_default_domain(&domain) != 0)
667c478bd9Sstevel@tonic-gate 		return (NULL);
677c478bd9Sstevel@tonic-gate 	if (!(nis = memget(sizeof *nis))) {
687c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
697c478bd9Sstevel@tonic-gate 		return (NULL);
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 	memset(nis, 0, sizeof *nis);
727c478bd9Sstevel@tonic-gate 	if (!(acc = memget(sizeof *acc))) {
737c478bd9Sstevel@tonic-gate 		memput(nis, sizeof *nis);
747c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
757c478bd9Sstevel@tonic-gate 		return (NULL);
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 	memset(acc, 0x5e, sizeof *acc);
787c478bd9Sstevel@tonic-gate 	acc->private = nis;
797c478bd9Sstevel@tonic-gate 	nis->domain = strdup(domain);
807c478bd9Sstevel@tonic-gate #ifdef WANT_IRS_GR
817c478bd9Sstevel@tonic-gate 	acc->gr_map = irs_nis_gr;
827c478bd9Sstevel@tonic-gate #else
837c478bd9Sstevel@tonic-gate 	acc->gr_map = NULL;
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate #ifdef WANT_IRS_PW
867c478bd9Sstevel@tonic-gate 	acc->pw_map = irs_nis_pw;
877c478bd9Sstevel@tonic-gate #else
887c478bd9Sstevel@tonic-gate 	acc->pw_map = NULL;
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate 	acc->sv_map = irs_nis_sv;
917c478bd9Sstevel@tonic-gate 	acc->pr_map = irs_nis_pr;
927c478bd9Sstevel@tonic-gate 	acc->ho_map = irs_nis_ho;
937c478bd9Sstevel@tonic-gate 	acc->nw_map = irs_nis_nw;
947c478bd9Sstevel@tonic-gate 	acc->ng_map = irs_nis_ng;
957c478bd9Sstevel@tonic-gate 	acc->res_get = nis_res_get;
967c478bd9Sstevel@tonic-gate 	acc->res_set = nis_res_set;
977c478bd9Sstevel@tonic-gate 	acc->close = nis_close;
987c478bd9Sstevel@tonic-gate 	return (acc);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /* Methods */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static struct __res_state *
nis_res_get(struct irs_acc * this)1047c478bd9Sstevel@tonic-gate nis_res_get(struct irs_acc *this) {
1057c478bd9Sstevel@tonic-gate 	struct nis_p *nis = (struct nis_p *)this->private;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (nis->res == NULL) {
1087c478bd9Sstevel@tonic-gate 		struct __res_state *res;
1097c478bd9Sstevel@tonic-gate 		res = (struct __res_state *)malloc(sizeof *res);
1107c478bd9Sstevel@tonic-gate 		if (res == NULL)
1117c478bd9Sstevel@tonic-gate 			return (NULL);
1127c478bd9Sstevel@tonic-gate 		memset(res, 0, sizeof *res);
1137c478bd9Sstevel@tonic-gate 		nis_res_set(this, res, free);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if ((nis->res->options & RES_INIT) == 0 &&
1177c478bd9Sstevel@tonic-gate 	    res_ninit(nis->res) < 0)
1187c478bd9Sstevel@tonic-gate 		return (NULL);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	return (nis->res);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static void
nis_res_set(struct irs_acc * this,struct __res_state * res,void (* free_res)(void *))1247c478bd9Sstevel@tonic-gate nis_res_set(struct irs_acc *this, struct __res_state *res,
1257c478bd9Sstevel@tonic-gate 		void (*free_res)(void *)) {
1267c478bd9Sstevel@tonic-gate 	struct nis_p *nis = (struct nis_p *)this->private;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (nis->res && nis->free_res) {
1297c478bd9Sstevel@tonic-gate 		res_nclose(nis->res);
1307c478bd9Sstevel@tonic-gate 		(*nis->free_res)(nis->res);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	nis->res = res;
1347c478bd9Sstevel@tonic-gate 	nis->free_res = free_res;
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static void
nis_close(struct irs_acc * this)1387c478bd9Sstevel@tonic-gate nis_close(struct irs_acc *this) {
1397c478bd9Sstevel@tonic-gate 	struct nis_p *nis = (struct nis_p *)this->private;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (nis->res && nis->free_res)
1427c478bd9Sstevel@tonic-gate 		(*nis->free_res)(nis->res);
1437c478bd9Sstevel@tonic-gate 	free(nis->domain);
1447c478bd9Sstevel@tonic-gate 	memput(nis, sizeof *nis);
1457c478bd9Sstevel@tonic-gate 	memput(this, sizeof *this);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate #endif /*WANT_IRS_NIS*/
1499525b14bSRao Shoaib 
1509525b14bSRao Shoaib /*! \file */
151