getgrent.c revision 1.20 1 /* $NetBSD: getgrent.c,v 1.20 1997/07/13 19:01:22 christos Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Portions Copyright (c) 1994, Jason Downs. All Rights Reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 #if 0
40 static char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 3/21/94";
41 #else
42 __RCSID("$NetBSD: getgrent.c,v 1.20 1997/07/13 19:01:22 christos Exp $");
43 #endif
44 #endif /* LIBC_SCCS and not lint */
45
46 #include <sys/types.h>
47 #include <limits.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <grp.h>
52 #ifdef YP
53 #include <rpc/rpc.h>
54 #include <rpcsvc/yp_prot.h>
55 #include <rpcsvc/ypclnt.h>
56 #endif
57
58 static FILE *_gr_fp;
59 static struct group _gr_group;
60 static int _gr_stayopen;
61 static int grscan __P((int, gid_t, const char *));
62 static int start_gr __P((void));
63
64 #define MAXGRP 200
65 static char *members[MAXGRP];
66 #define MAXLINELENGTH 1024
67 static char line[MAXLINELENGTH];
68
69 #ifdef YP
70 enum _ypmode { YPMODE_NONE, YPMODE_FULL, YPMODE_NAME };
71 static enum _ypmode __ypmode;
72 static char *__ypcurrent, *__ypdomain;
73 static int __ypcurrentlen;
74 #endif
75
76 struct group *
77 getgrent()
78 {
79 if ((!_gr_fp && !start_gr()) || !grscan(0, 0, NULL))
80 return(NULL);
81 return(&_gr_group);
82 }
83
84 struct group *
85 getgrnam(name)
86 const char *name;
87 {
88 int rval;
89
90 if (!start_gr())
91 return(NULL);
92 rval = grscan(1, 0, name);
93 if (!_gr_stayopen)
94 endgrent();
95 return(rval ? &_gr_group : NULL);
96 }
97
98 struct group *
99 #ifdef __STDC__
100 getgrgid(gid_t gid)
101 #else
102 getgrgid(gid)
103 gid_t gid;
104 #endif
105 {
106 int rval;
107
108 if (!start_gr())
109 return(NULL);
110 rval = grscan(1, gid, NULL);
111 if (!_gr_stayopen)
112 endgrent();
113 return(rval ? &_gr_group : NULL);
114 }
115
116 static int
117 start_gr()
118 {
119 if (_gr_fp) {
120 rewind(_gr_fp);
121 #ifdef YP
122 __ypmode = YPMODE_NONE;
123 if (__ypcurrent)
124 free(__ypcurrent);
125 __ypcurrent = NULL;
126 #endif
127 return(1);
128 }
129 return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
130 }
131
132 void
133 setgrent()
134 {
135 (void) setgroupent(0);
136 }
137
138 int
139 setgroupent(stayopen)
140 int stayopen;
141 {
142 if (!start_gr())
143 return(0);
144 _gr_stayopen = stayopen;
145 return(1);
146 }
147
148 void
149 endgrent()
150 {
151 if (_gr_fp) {
152 (void)fclose(_gr_fp);
153 _gr_fp = NULL;
154 #ifdef YP
155 __ypmode = YPMODE_NONE;
156 if (__ypcurrent)
157 free(__ypcurrent);
158 __ypcurrent = NULL;
159 #endif
160 }
161 }
162
163 static int
164 grscan(search, gid, name)
165 int search;
166 gid_t gid;
167 const char *name;
168 {
169 char *cp, **m;
170 char *bp, *ep;
171 unsigned long id;
172 #ifdef YP
173 char *key, *data;
174 int keylen, datalen;
175 int r;
176 char *grname = (char *)NULL;
177 #endif
178
179 for (;;) {
180 #ifdef YP
181 if (__ypmode != YPMODE_NONE) {
182
183 if (!__ypdomain) {
184 if (yp_get_default_domain(&__ypdomain)) {
185 __ypmode = YPMODE_NONE;
186 if (grname != (char *)NULL) {
187 free(grname);
188 grname = (char *)NULL;
189 }
190 continue;
191 }
192 }
193 switch(__ypmode) {
194 case YPMODE_FULL:
195 data = NULL;
196 if (__ypcurrent) {
197 key = NULL;
198 r = yp_next(__ypdomain, "group.byname",
199 __ypcurrent, __ypcurrentlen,
200 &key, &keylen, &data, &datalen);
201 free(__ypcurrent);
202 if (r != 0) {
203 __ypcurrent = NULL;
204 if (key)
205 free(key);
206 }
207 else {
208 __ypcurrent = key;
209 __ypcurrentlen = keylen;
210 }
211 } else {
212 r = yp_first(__ypdomain, "group.byname",
213 &__ypcurrent, &__ypcurrentlen,
214 &data, &datalen);
215 }
216 if (r != 0) {
217 __ypmode = YPMODE_NONE;
218 if (data)
219 free(data);
220 continue;
221 }
222 bcopy(data, line, datalen);
223 free(data);
224 break;
225 case YPMODE_NAME:
226 if (grname != (char *)NULL) {
227 data = NULL;
228 r = yp_match(__ypdomain, "group.byname",
229 grname, strlen(grname),
230 &data, &datalen);
231 __ypmode = YPMODE_NONE;
232 free(grname);
233 grname = (char *)NULL;
234 if (r != 0) {
235 if (data)
236 free(data);
237 continue;
238 }
239 bcopy(data, line, datalen);
240 free(data);
241 } else {
242 /* YP not available? */
243 __ypmode = YPMODE_NONE;
244 continue;
245 }
246 break;
247 case YPMODE_NONE:
248 abort(); /* Cannot happen */
249 break;
250 }
251 line[datalen] = '\0';
252 bp = line;
253 goto parse;
254 }
255 #endif /* YP */
256 if (!fgets(line, sizeof(line), _gr_fp))
257 return(0);
258 bp = line;
259 /* skip lines that are too big */
260 if (!strchr(line, '\n')) {
261 int ch;
262
263 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
264 ;
265 continue;
266 }
267 #ifdef YP
268 if (line[0] == '+') {
269 switch(line[1]) {
270 case ':':
271 case '\0':
272 case '\n':
273 if (_yp_check(NULL)) {
274 if (!search) {
275 __ypmode = YPMODE_FULL;
276 continue;
277 }
278 if (!__ypdomain &&
279 yp_get_default_domain(&__ypdomain))
280 continue;
281 data = NULL;
282 if (name) {
283 r = yp_match(__ypdomain,
284 "group.byname",
285 name, strlen(name),
286 &data, &datalen);
287 } else {
288 char buf[20];
289 snprintf(buf, sizeof(buf),
290 "%u", gid);
291 r = yp_match(__ypdomain,
292 "group.bygid",
293 buf, strlen(buf),
294 &data, &datalen);
295 }
296 if (r != 0) {
297 if (data)
298 free(data);
299 continue;
300 }
301 bcopy(data, line, datalen);
302 free(data);
303 line[datalen] = '\0';
304 bp = line;
305 _gr_group.gr_name = strsep(&bp, ":\n");
306 _gr_group.gr_passwd =
307 strsep(&bp, ":\n");
308 if (!(cp = strsep(&bp, ":\n")))
309 continue;
310 if (name) {
311 id = strtoul(cp, &ep, 10);
312 if (id > GID_MAX || *ep != '\0')
313 continue;
314 _gr_group.gr_gid = (gid_t)id;
315 } else
316 _gr_group.gr_gid = gid;
317 goto found_it;
318 }
319 break;
320 default:
321 if (_yp_check(NULL)) {
322 char *tptr;
323
324 tptr = strsep(&bp, ":\n");
325 if (search && name &&
326 strcmp(tptr, name))
327 continue;
328 __ypmode = YPMODE_NAME;
329 grname = strdup(tptr + 1);
330 continue;
331 }
332 break;
333 }
334 }
335 parse:
336 #endif /* YP */
337 _gr_group.gr_name = strsep(&bp, ":\n");
338 if (search && name && strcmp(_gr_group.gr_name, name))
339 continue;
340 _gr_group.gr_passwd = strsep(&bp, ":\n");
341 if (!(cp = strsep(&bp, ":\n")))
342 continue;
343 id = strtoul(cp, &ep, 10);
344 if (id > GID_MAX || *ep != '\0')
345 continue;
346 _gr_group.gr_gid = (gid_t)id;
347 if (search && name == NULL && _gr_group.gr_gid != gid)
348 continue;
349 found_it:
350 cp = NULL;
351 if (bp == NULL)
352 continue;
353 for (m = _gr_group.gr_mem = members;; bp++) {
354 if (m == &members[MAXGRP - 1])
355 break;
356 if (*bp == ',') {
357 if (cp) {
358 *bp = '\0';
359 *m++ = cp;
360 cp = NULL;
361 }
362 } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
363 if (cp) {
364 *bp = '\0';
365 *m++ = cp;
366 }
367 break;
368 } else if (cp == NULL)
369 cp = bp;
370 }
371 *m = NULL;
372 return(1);
373 }
374 /* NOTREACHED */
375 }
376