1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*
7*0Sstevel@tonic-gate  * Portions Copyright (c) 1996,1999 by Internet Software Consortium.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
10*0Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
11*0Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14*0Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16*0Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17*0Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18*0Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19*0Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20*0Sstevel@tonic-gate  * SOFTWARE.
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
26*0Sstevel@tonic-gate static const char rcsid[] = "$Id: getnetgrent.c,v 1.17 2003/04/29 05:51:14 marka Exp $";
27*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /* Imports */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include "port_before.h"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #if !defined(__BIND_NOSTATIC)
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <netinet/in.h>
38*0Sstevel@tonic-gate #include <arpa/nameser.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <errno.h>
41*0Sstevel@tonic-gate #include <resolv.h>
42*0Sstevel@tonic-gate #include <stdio.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include <irs.h>
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #include "port_after.h"
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #include "irs_data.h"
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /* Forward */
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate static struct net_data *init(void);
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate /* Public */
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate #ifndef SETNETGRENT_ARGS
58*0Sstevel@tonic-gate #define SETNETGRENT_ARGS const char *netgroup
59*0Sstevel@tonic-gate #endif
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
62*0Sstevel@tonic-gate void
63*0Sstevel@tonic-gate #else
64*0Sstevel@tonic-gate int
65*0Sstevel@tonic-gate #endif
66*0Sstevel@tonic-gate setnetgrent(SETNETGRENT_ARGS) {
67*0Sstevel@tonic-gate 	struct net_data *net_data = init();
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	setnetgrent_p(netgroup, net_data);
70*0Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
71*0Sstevel@tonic-gate #else
72*0Sstevel@tonic-gate 	return (0);
73*0Sstevel@tonic-gate #endif
74*0Sstevel@tonic-gate }
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
77*0Sstevel@tonic-gate void
78*0Sstevel@tonic-gate #else
79*0Sstevel@tonic-gate int
80*0Sstevel@tonic-gate #endif
81*0Sstevel@tonic-gate endnetgrent(void) {
82*0Sstevel@tonic-gate 	struct net_data *net_data = init();
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	endnetgrent_p(net_data);
85*0Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
86*0Sstevel@tonic-gate #else
87*0Sstevel@tonic-gate 	return (0);
88*0Sstevel@tonic-gate #endif
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate #ifndef INNETGR_ARGS
92*0Sstevel@tonic-gate #define INNETGR_ARGS const char *netgroup, const char *host, \
93*0Sstevel@tonic-gate 		     const char *user, const char *domain
94*0Sstevel@tonic-gate #endif
95*0Sstevel@tonic-gate int
96*0Sstevel@tonic-gate innetgr(INNETGR_ARGS) {
97*0Sstevel@tonic-gate 	struct net_data *net_data = init();
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	return (innetgr_p(netgroup, host, user, domain, net_data));
100*0Sstevel@tonic-gate }
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
103*0Sstevel@tonic-gate int
104*0Sstevel@tonic-gate getnetgrent(const char **host, const char **user, const char **domain) {
105*0Sstevel@tonic-gate 	struct net_data *net_data = init();
106*0Sstevel@tonic-gate 	const char *ch, *cu, *cd;
107*0Sstevel@tonic-gate 	int ret;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	ret = getnetgrent_p(&ch, &cu, &cd, net_data);
110*0Sstevel@tonic-gate 	if (ret != 1)
111*0Sstevel@tonic-gate 		return (ret);
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	DE_CONST(ch, *host);
114*0Sstevel@tonic-gate 	DE_CONST(cu, *user);
115*0Sstevel@tonic-gate 	DE_CONST(cd, *domain);
116*0Sstevel@tonic-gate 	return (ret);
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate #else
119*0Sstevel@tonic-gate int
120*0Sstevel@tonic-gate getnetgrent(char **host, char **user, char **domain) {
121*0Sstevel@tonic-gate 	struct net_data *net_data = init();
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	return (getnetgrent_p((const char **)host, (const char **)user,
124*0Sstevel@tonic-gate 		(const char **)domain, net_data));
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate }
127*0Sstevel@tonic-gate #endif /* ORIGINAL_ISC_CODE */
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate /* Shared private. */
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate void
132*0Sstevel@tonic-gate setnetgrent_p(const char *netgroup, struct net_data *net_data) {
133*0Sstevel@tonic-gate 	struct irs_ng *ng;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	if ((net_data != NULL) && ((ng = net_data->ng) != NULL))
136*0Sstevel@tonic-gate 		(*ng->rewind)(ng, netgroup);
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate void
140*0Sstevel@tonic-gate endnetgrent_p(struct net_data *net_data) {
141*0Sstevel@tonic-gate 	struct irs_ng *ng;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	if (!net_data)
144*0Sstevel@tonic-gate 		return;
145*0Sstevel@tonic-gate 	if ((ng = net_data->ng) != NULL)
146*0Sstevel@tonic-gate 		(*ng->close)(ng);
147*0Sstevel@tonic-gate 	net_data->ng = NULL;
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate int
151*0Sstevel@tonic-gate innetgr_p(const char *netgroup, const char *host,
152*0Sstevel@tonic-gate 	  const char *user, const char *domain,
153*0Sstevel@tonic-gate 	  struct net_data *net_data) {
154*0Sstevel@tonic-gate 	struct irs_ng *ng;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	if (!net_data || !(ng = net_data->ng))
157*0Sstevel@tonic-gate 		return (0);
158*0Sstevel@tonic-gate 	return ((*ng->test)(ng, netgroup, host, user, domain));
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate int
162*0Sstevel@tonic-gate getnetgrent_p(const char **host, const char **user, const char **domain,
163*0Sstevel@tonic-gate 	      struct net_data *net_data ) {
164*0Sstevel@tonic-gate 	struct irs_ng *ng;
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	if (!net_data || !(ng = net_data->ng))
167*0Sstevel@tonic-gate 		return (0);
168*0Sstevel@tonic-gate 	return ((*ng->next)(ng, host, user, domain));
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate /* Private */
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate static struct net_data *
174*0Sstevel@tonic-gate init(void) {
175*0Sstevel@tonic-gate 	struct net_data *net_data;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	if (!(net_data = net_data_init(NULL)))
178*0Sstevel@tonic-gate 		goto error;
179*0Sstevel@tonic-gate 	if (!net_data->ng) {
180*0Sstevel@tonic-gate 		net_data->ng = (*net_data->irs->ng_map)(net_data->irs);
181*0Sstevel@tonic-gate 		if (!net_data->ng) {
182*0Sstevel@tonic-gate   error:
183*0Sstevel@tonic-gate 			errno = EIO;
184*0Sstevel@tonic-gate 			return (NULL);
185*0Sstevel@tonic-gate 		}
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	return (net_data);
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate #endif /*__BIND_NOSTATIC*/
192