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