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