xref: /illumos-gate/usr/src/lib/libresolv2/common/irs/getnetgrent.c (revision 458f44a49dc56cd17a39815122214e7a1b4793e3)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
3*9525b14bSRao Shoaib  * Copyright (C) 1996-1999, 2001, 2003  Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
5*9525b14bSRao Shoaib  * Permission to use, copy, modify, and/or 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 WITH
10*9525b14bSRao Shoaib  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*9525b14bSRao Shoaib  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*9525b14bSRao Shoaib  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*9525b14bSRao Shoaib  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*9525b14bSRao Shoaib  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*9525b14bSRao Shoaib  * PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate /* Imports */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include "port_before.h"
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #if !defined(__BIND_NOSTATIC)
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #include <sys/types.h>
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <netinet/in.h>
277c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <resolv.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <irs.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "port_after.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "irs_data.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /* Forward */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static struct net_data *init(void);
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /* Public */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #ifndef SETNETGRENT_ARGS
477c478bd9Sstevel@tonic-gate #define SETNETGRENT_ARGS const char *netgroup
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate void
setnetgrent(SETNETGRENT_ARGS)507c478bd9Sstevel@tonic-gate setnetgrent(SETNETGRENT_ARGS) {
517c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	setnetgrent_p(netgroup, net_data);
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate void
endnetgrent(void)577c478bd9Sstevel@tonic-gate endnetgrent(void) {
587c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	endnetgrent_p(net_data);
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #ifndef INNETGR_ARGS
647c478bd9Sstevel@tonic-gate #define INNETGR_ARGS const char *netgroup, const char *host, \
657c478bd9Sstevel@tonic-gate 		     const char *user, const char *domain
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate int
innetgr(INNETGR_ARGS)687c478bd9Sstevel@tonic-gate innetgr(INNETGR_ARGS) {
697c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	return (innetgr_p(netgroup, host, user, domain, net_data));
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate int
getnetgrent(NGR_R_CONST char ** host,NGR_R_CONST char ** user,NGR_R_CONST char ** domain)75*9525b14bSRao Shoaib getnetgrent(NGR_R_CONST char **host, NGR_R_CONST char **user,
76*9525b14bSRao Shoaib 	    NGR_R_CONST char **domain)
77*9525b14bSRao Shoaib {
787c478bd9Sstevel@tonic-gate 	struct net_data *net_data = init();
797c478bd9Sstevel@tonic-gate 	const char *ch, *cu, *cd;
807c478bd9Sstevel@tonic-gate 	int ret;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	ret = getnetgrent_p(&ch, &cu, &cd, net_data);
837c478bd9Sstevel@tonic-gate 	if (ret != 1)
847c478bd9Sstevel@tonic-gate 		return (ret);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	DE_CONST(ch, *host);
877c478bd9Sstevel@tonic-gate 	DE_CONST(cu, *user);
887c478bd9Sstevel@tonic-gate 	DE_CONST(cd, *domain);
897c478bd9Sstevel@tonic-gate 	return (ret);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /* Shared private. */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate void
setnetgrent_p(const char * netgroup,struct net_data * net_data)957c478bd9Sstevel@tonic-gate setnetgrent_p(const char *netgroup, struct net_data *net_data) {
967c478bd9Sstevel@tonic-gate 	struct irs_ng *ng;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if ((net_data != NULL) && ((ng = net_data->ng) != NULL))
997c478bd9Sstevel@tonic-gate 		(*ng->rewind)(ng, netgroup);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate void
endnetgrent_p(struct net_data * net_data)1037c478bd9Sstevel@tonic-gate endnetgrent_p(struct net_data *net_data) {
1047c478bd9Sstevel@tonic-gate 	struct irs_ng *ng;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (!net_data)
1077c478bd9Sstevel@tonic-gate 		return;
1087c478bd9Sstevel@tonic-gate 	if ((ng = net_data->ng) != NULL)
1097c478bd9Sstevel@tonic-gate 		(*ng->close)(ng);
1107c478bd9Sstevel@tonic-gate 	net_data->ng = NULL;
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate int
innetgr_p(const char * netgroup,const char * host,const char * user,const char * domain,struct net_data * net_data)1147c478bd9Sstevel@tonic-gate innetgr_p(const char *netgroup, const char *host,
1157c478bd9Sstevel@tonic-gate 	  const char *user, const char *domain,
1167c478bd9Sstevel@tonic-gate 	  struct net_data *net_data) {
1177c478bd9Sstevel@tonic-gate 	struct irs_ng *ng;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (!net_data || !(ng = net_data->ng))
1207c478bd9Sstevel@tonic-gate 		return (0);
1217c478bd9Sstevel@tonic-gate 	return ((*ng->test)(ng, netgroup, host, user, domain));
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate int
getnetgrent_p(const char ** host,const char ** user,const char ** domain,struct net_data * net_data)1257c478bd9Sstevel@tonic-gate getnetgrent_p(const char **host, const char **user, const char **domain,
1267c478bd9Sstevel@tonic-gate 	      struct net_data *net_data ) {
1277c478bd9Sstevel@tonic-gate 	struct irs_ng *ng;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (!net_data || !(ng = net_data->ng))
1307c478bd9Sstevel@tonic-gate 		return (0);
1317c478bd9Sstevel@tonic-gate 	return ((*ng->next)(ng, host, user, domain));
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /* Private */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static struct net_data *
init(void)1377c478bd9Sstevel@tonic-gate init(void) {
1387c478bd9Sstevel@tonic-gate 	struct net_data *net_data;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (!(net_data = net_data_init(NULL)))
1417c478bd9Sstevel@tonic-gate 		goto error;
1427c478bd9Sstevel@tonic-gate 	if (!net_data->ng) {
1437c478bd9Sstevel@tonic-gate 		net_data->ng = (*net_data->irs->ng_map)(net_data->irs);
1447c478bd9Sstevel@tonic-gate 		if (!net_data->ng) {
1457c478bd9Sstevel@tonic-gate   error:
1467c478bd9Sstevel@tonic-gate 			errno = EIO;
1477c478bd9Sstevel@tonic-gate 			return (NULL);
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	return (net_data);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate #endif /*__BIND_NOSTATIC*/
155*9525b14bSRao Shoaib 
156*9525b14bSRao Shoaib /*! \file */
157