xref: /onnv-gate/usr/src/lib/libresolv2/common/irs/nis.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate  * Copyright (c) 1996-1999 by Internet Software Consortium.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: nis.c,v 1.3 2005/04/27 04:56:32 sra Exp $";
200Sstevel@tonic-gate #endif
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /* Imports */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include "port_before.h"
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #ifdef WANT_IRS_NIS
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <rpc/rpc.h>
290Sstevel@tonic-gate #include <rpc/xdr.h>
300Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
310Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <netinet/in.h>
39*11038SRao.Shoaib@Sun.COM #ifdef T_NULL
40*11038SRao.Shoaib@Sun.COM #undef T_NULL			/* Silence re-definition warning of T_NULL. */
41*11038SRao.Shoaib@Sun.COM #endif
420Sstevel@tonic-gate #include <arpa/nameser.h>
430Sstevel@tonic-gate #include <resolv.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <isc/memcluster.h>
460Sstevel@tonic-gate #include <irs.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include "port_after.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include "irs_p.h"
510Sstevel@tonic-gate #include "hesiod.h"
520Sstevel@tonic-gate #include "nis_p.h"
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /* Forward */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static void		nis_close(struct irs_acc *);
570Sstevel@tonic-gate static struct __res_state * nis_res_get(struct irs_acc *);
580Sstevel@tonic-gate static void		nis_res_set(struct irs_acc *, struct __res_state *,
590Sstevel@tonic-gate 				void (*)(void *));
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /* Public */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate struct irs_acc *
irs_nis_acc(const char * options)640Sstevel@tonic-gate irs_nis_acc(const char *options) {
650Sstevel@tonic-gate 	struct nis_p *nis;
660Sstevel@tonic-gate 	struct irs_acc *acc;
670Sstevel@tonic-gate 	char *domain;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	UNUSED(options);
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	if (yp_get_default_domain(&domain) != 0)
720Sstevel@tonic-gate 		return (NULL);
730Sstevel@tonic-gate 	if (!(nis = memget(sizeof *nis))) {
740Sstevel@tonic-gate 		errno = ENOMEM;
750Sstevel@tonic-gate 		return (NULL);
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate 	memset(nis, 0, sizeof *nis);
780Sstevel@tonic-gate 	if (!(acc = memget(sizeof *acc))) {
790Sstevel@tonic-gate 		memput(nis, sizeof *nis);
800Sstevel@tonic-gate 		errno = ENOMEM;
810Sstevel@tonic-gate 		return (NULL);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 	memset(acc, 0x5e, sizeof *acc);
840Sstevel@tonic-gate 	acc->private = nis;
850Sstevel@tonic-gate 	nis->domain = strdup(domain);
860Sstevel@tonic-gate #ifdef WANT_IRS_GR
870Sstevel@tonic-gate 	acc->gr_map = irs_nis_gr;
880Sstevel@tonic-gate #else
890Sstevel@tonic-gate 	acc->gr_map = NULL;
900Sstevel@tonic-gate #endif
910Sstevel@tonic-gate #ifdef WANT_IRS_PW
920Sstevel@tonic-gate 	acc->pw_map = irs_nis_pw;
930Sstevel@tonic-gate #else
940Sstevel@tonic-gate 	acc->pw_map = NULL;
950Sstevel@tonic-gate #endif
960Sstevel@tonic-gate 	acc->sv_map = irs_nis_sv;
970Sstevel@tonic-gate 	acc->pr_map = irs_nis_pr;
980Sstevel@tonic-gate 	acc->ho_map = irs_nis_ho;
990Sstevel@tonic-gate 	acc->nw_map = irs_nis_nw;
1000Sstevel@tonic-gate 	acc->ng_map = irs_nis_ng;
1010Sstevel@tonic-gate 	acc->res_get = nis_res_get;
1020Sstevel@tonic-gate 	acc->res_set = nis_res_set;
1030Sstevel@tonic-gate 	acc->close = nis_close;
1040Sstevel@tonic-gate 	return (acc);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /* Methods */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate static struct __res_state *
nis_res_get(struct irs_acc * this)1100Sstevel@tonic-gate nis_res_get(struct irs_acc *this) {
1110Sstevel@tonic-gate 	struct nis_p *nis = (struct nis_p *)this->private;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	if (nis->res == NULL) {
1140Sstevel@tonic-gate 		struct __res_state *res;
1150Sstevel@tonic-gate 		res = (struct __res_state *)malloc(sizeof *res);
1160Sstevel@tonic-gate 		if (res == NULL)
1170Sstevel@tonic-gate 			return (NULL);
1180Sstevel@tonic-gate 		memset(res, 0, sizeof *res);
1190Sstevel@tonic-gate 		nis_res_set(this, res, free);
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	if ((nis->res->options & RES_INIT) == 0 &&
1230Sstevel@tonic-gate 	    res_ninit(nis->res) < 0)
1240Sstevel@tonic-gate 		return (NULL);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	return (nis->res);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static void
nis_res_set(struct irs_acc * this,struct __res_state * res,void (* free_res)(void *))1300Sstevel@tonic-gate nis_res_set(struct irs_acc *this, struct __res_state *res,
1310Sstevel@tonic-gate 		void (*free_res)(void *)) {
1320Sstevel@tonic-gate 	struct nis_p *nis = (struct nis_p *)this->private;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	if (nis->res && nis->free_res) {
1350Sstevel@tonic-gate 		res_nclose(nis->res);
1360Sstevel@tonic-gate 		(*nis->free_res)(nis->res);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	nis->res = res;
1400Sstevel@tonic-gate 	nis->free_res = free_res;
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static void
nis_close(struct irs_acc * this)1440Sstevel@tonic-gate nis_close(struct irs_acc *this) {
1450Sstevel@tonic-gate 	struct nis_p *nis = (struct nis_p *)this->private;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (nis->res && nis->free_res)
1480Sstevel@tonic-gate 		(*nis->free_res)(nis->res);
1490Sstevel@tonic-gate 	free(nis->domain);
1500Sstevel@tonic-gate 	memput(nis, sizeof *nis);
1510Sstevel@tonic-gate 	memput(this, sizeof *this);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate #endif /*WANT_IRS_NIS*/
155*11038SRao.Shoaib@Sun.COM 
156*11038SRao.Shoaib@Sun.COM /*! \file */
157