getgrent.c revision 1.1.1.1.14.1 1 1.1.1.1.14.1 yamt /* $NetBSD: getgrent.c,v 1.1.1.1.14.1 2012/10/30 18:55:27 yamt Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos * Copyright (c) 1996-1999 by Internet Software Consortium.
6 1.1 christos *
7 1.1 christos * Permission to use, copy, modify, and distribute this software for any
8 1.1 christos * purpose with or without fee is hereby granted, provided that the above
9 1.1 christos * copyright notice and this permission notice appear in all copies.
10 1.1 christos *
11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 christos */
19 1.1 christos
20 1.1 christos #if !defined(LINT) && !defined(CODECENTER)
21 1.1.1.1.14.1 yamt static const char rcsid[] = "Id: getgrent.c,v 1.5 2005/04/27 04:56:24 sra Exp ";
22 1.1 christos #endif
23 1.1 christos
24 1.1 christos /* Imports */
25 1.1 christos
26 1.1 christos #include "port_before.h"
27 1.1 christos
28 1.1 christos #if !defined(WANT_IRS_GR) || defined(__BIND_NOSTATIC)
29 1.1 christos static int __bind_irs_gr_unneeded;
30 1.1 christos #else
31 1.1 christos
32 1.1 christos #include <sys/types.h>
33 1.1 christos
34 1.1 christos #include <netinet/in.h>
35 1.1 christos #include <arpa/nameser.h>
36 1.1 christos
37 1.1 christos #include <errno.h>
38 1.1 christos #include <grp.h>
39 1.1 christos #include <resolv.h>
40 1.1 christos #include <stdio.h>
41 1.1 christos #include <string.h>
42 1.1 christos #include <unistd.h>
43 1.1 christos
44 1.1 christos #include <irs.h>
45 1.1 christos
46 1.1 christos #include "port_after.h"
47 1.1 christos
48 1.1 christos #include "irs_data.h"
49 1.1 christos
50 1.1 christos /* Forward */
51 1.1 christos
52 1.1 christos static struct net_data *init(void);
53 1.1 christos void endgrent(void);
54 1.1 christos
55 1.1 christos /* Public */
56 1.1 christos
57 1.1 christos struct group *
58 1.1 christos getgrent() {
59 1.1 christos struct net_data *net_data = init();
60 1.1 christos
61 1.1 christos return (getgrent_p(net_data));
62 1.1 christos }
63 1.1 christos
64 1.1 christos struct group *
65 1.1 christos getgrnam(const char *name) {
66 1.1 christos struct net_data *net_data = init();
67 1.1 christos
68 1.1 christos return (getgrnam_p(name, net_data));
69 1.1 christos }
70 1.1 christos
71 1.1 christos struct group *
72 1.1 christos getgrgid(gid_t gid) {
73 1.1 christos struct net_data *net_data = init();
74 1.1 christos
75 1.1 christos return (getgrgid_p(gid, net_data));
76 1.1 christos }
77 1.1 christos
78 1.1 christos int
79 1.1 christos setgroupent(int stayopen) {
80 1.1 christos struct net_data *net_data = init();
81 1.1 christos
82 1.1 christos return (setgroupent_p(stayopen, net_data));
83 1.1 christos }
84 1.1 christos
85 1.1 christos #ifdef SETGRENT_VOID
86 1.1 christos void
87 1.1 christos setgrent(void) {
88 1.1 christos struct net_data *net_data = init();
89 1.1 christos
90 1.1 christos setgrent_p(net_data);
91 1.1 christos }
92 1.1 christos #else
93 1.1 christos int
94 1.1 christos setgrent(void) {
95 1.1 christos struct net_data *net_data = init();
96 1.1 christos
97 1.1 christos return (setgrent_p(net_data));
98 1.1 christos }
99 1.1 christos #endif /* SETGRENT_VOID */
100 1.1 christos
101 1.1 christos void
102 1.1 christos endgrent() {
103 1.1 christos struct net_data *net_data = init();
104 1.1 christos
105 1.1 christos endgrent_p(net_data);
106 1.1 christos }
107 1.1 christos
108 1.1 christos int
109 1.1 christos getgrouplist(GETGROUPLIST_ARGS) {
110 1.1 christos struct net_data *net_data = init();
111 1.1 christos
112 1.1 christos return (getgrouplist_p(name, basegid, groups, ngroups, net_data));
113 1.1 christos }
114 1.1 christos
115 1.1 christos /* Shared private. */
116 1.1 christos
117 1.1 christos struct group *
118 1.1 christos getgrent_p(struct net_data *net_data) {
119 1.1 christos struct irs_gr *gr;
120 1.1 christos
121 1.1 christos if (!net_data || !(gr = net_data->gr))
122 1.1 christos return (NULL);
123 1.1 christos net_data->gr_last = (*gr->next)(gr);
124 1.1 christos return (net_data->gr_last);
125 1.1 christos }
126 1.1 christos
127 1.1 christos struct group *
128 1.1 christos getgrnam_p(const char *name, struct net_data *net_data) {
129 1.1 christos struct irs_gr *gr;
130 1.1 christos
131 1.1 christos if (!net_data || !(gr = net_data->gr))
132 1.1 christos return (NULL);
133 1.1 christos if (net_data->gr_stayopen && net_data->gr_last &&
134 1.1 christos !strcmp(net_data->gr_last->gr_name, name))
135 1.1 christos return (net_data->gr_last);
136 1.1 christos net_data->gr_last = (*gr->byname)(gr, name);
137 1.1 christos if (!net_data->gr_stayopen)
138 1.1 christos endgrent();
139 1.1 christos return (net_data->gr_last);
140 1.1 christos }
141 1.1 christos
142 1.1 christos struct group *
143 1.1 christos getgrgid_p(gid_t gid, struct net_data *net_data) {
144 1.1 christos struct irs_gr *gr;
145 1.1 christos
146 1.1 christos if (!net_data || !(gr = net_data->gr))
147 1.1 christos return (NULL);
148 1.1 christos if (net_data->gr_stayopen && net_data->gr_last &&
149 1.1 christos (gid_t)net_data->gr_last->gr_gid == gid)
150 1.1 christos return (net_data->gr_last);
151 1.1 christos net_data->gr_last = (*gr->bygid)(gr, gid);
152 1.1 christos if (!net_data->gr_stayopen)
153 1.1 christos endgrent();
154 1.1 christos return (net_data->gr_last);
155 1.1 christos }
156 1.1 christos
157 1.1 christos int
158 1.1 christos setgroupent_p(int stayopen, struct net_data *net_data) {
159 1.1 christos struct irs_gr *gr;
160 1.1 christos
161 1.1 christos if (!net_data || !(gr = net_data->gr))
162 1.1 christos return (0);
163 1.1 christos (*gr->rewind)(gr);
164 1.1 christos net_data->gr_stayopen = (stayopen != 0);
165 1.1 christos if (stayopen == 0)
166 1.1 christos net_data_minimize(net_data);
167 1.1 christos return (1);
168 1.1 christos }
169 1.1 christos
170 1.1 christos #ifdef SETGRENT_VOID
171 1.1 christos void
172 1.1 christos setgrent_p(struct net_data *net_data) {
173 1.1 christos (void)setgroupent_p(0, net_data);
174 1.1 christos }
175 1.1 christos #else
176 1.1 christos int
177 1.1 christos setgrent_p(struct net_data *net_data) {
178 1.1 christos return (setgroupent_p(0, net_data));
179 1.1 christos }
180 1.1 christos #endif /* SETGRENT_VOID */
181 1.1 christos
182 1.1 christos void
183 1.1 christos endgrent_p(struct net_data *net_data) {
184 1.1 christos struct irs_gr *gr;
185 1.1 christos
186 1.1 christos if ((net_data != NULL) && ((gr = net_data->gr) != NULL))
187 1.1 christos (*gr->minimize)(gr);
188 1.1 christos }
189 1.1 christos
190 1.1 christos int
191 1.1 christos getgrouplist_p(const char *name, gid_t basegid, gid_t *groups, int *ngroups,
192 1.1 christos struct net_data *net_data) {
193 1.1 christos struct irs_gr *gr;
194 1.1 christos
195 1.1 christos if (!net_data || !(gr = net_data->gr)) {
196 1.1 christos *ngroups = 0;
197 1.1 christos return (-1);
198 1.1 christos }
199 1.1 christos return ((*gr->list)(gr, name, basegid, groups, ngroups));
200 1.1 christos }
201 1.1 christos
202 1.1 christos /* Private */
203 1.1 christos
204 1.1 christos static struct net_data *
205 1.1 christos init() {
206 1.1 christos struct net_data *net_data;
207 1.1 christos
208 1.1 christos if (!(net_data = net_data_init(NULL)))
209 1.1 christos goto error;
210 1.1 christos if (!net_data->gr) {
211 1.1 christos net_data->gr = (*net_data->irs->gr_map)(net_data->irs);
212 1.1 christos
213 1.1 christos if (!net_data->gr || !net_data->res) {
214 1.1 christos error:
215 1.1 christos errno = EIO;
216 1.1 christos return (NULL);
217 1.1 christos }
218 1.1 christos (*net_data->gr->res_set)(net_data->gr, net_data->res,
219 1.1 christos NULL);
220 1.1 christos }
221 1.1 christos
222 1.1 christos return (net_data);
223 1.1 christos }
224 1.1 christos
225 1.1 christos #endif /* WANT_IRS_GR */
226 1.1 christos /*! \file */
227