getgrent.c revision 1.17 1 1.17 lukem /* $NetBSD: getgrent.c,v 1.17 1997/05/21 01:51:39 lukem 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.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
38 1.11 cgd #if 0
39 1.11 cgd static char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 3/21/94";
40 1.11 cgd #else
41 1.17 lukem static char rcsid[] = "$NetBSD: getgrent.c,v 1.17 1997/05/21 01:51:39 lukem Exp $";
42 1.11 cgd #endif
43 1.1 cgd #endif /* LIBC_SCCS and not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/types.h>
46 1.1 cgd #include <stdio.h>
47 1.1 cgd #include <stdlib.h>
48 1.2 deraadt #include <string.h>
49 1.1 cgd #include <grp.h>
50 1.2 deraadt #ifdef YP
51 1.2 deraadt #include <rpc/rpc.h>
52 1.2 deraadt #include <rpcsvc/yp_prot.h>
53 1.2 deraadt #include <rpcsvc/ypclnt.h>
54 1.2 deraadt #endif
55 1.1 cgd
56 1.1 cgd static FILE *_gr_fp;
57 1.1 cgd static struct group _gr_group;
58 1.1 cgd static int _gr_stayopen;
59 1.1 cgd static int grscan(), start_gr();
60 1.1 cgd
61 1.1 cgd #define MAXGRP 200
62 1.1 cgd static char *members[MAXGRP];
63 1.1 cgd #define MAXLINELENGTH 1024
64 1.1 cgd static char line[MAXLINELENGTH];
65 1.1 cgd
66 1.2 deraadt #ifdef YP
67 1.13 phil enum _ypmode { YPMODE_NONE, YPMODE_FULL, YPMODE_NAME };
68 1.13 phil static enum _ypmode __ypmode;
69 1.2 deraadt static char *__ypcurrent, *__ypdomain;
70 1.13 phil static int __ypcurrentlen;
71 1.2 deraadt #endif
72 1.2 deraadt
73 1.1 cgd struct group *
74 1.1 cgd getgrent()
75 1.1 cgd {
76 1.1 cgd if (!_gr_fp && !start_gr() || !grscan(0, 0, NULL))
77 1.1 cgd return(NULL);
78 1.1 cgd return(&_gr_group);
79 1.1 cgd }
80 1.1 cgd
81 1.1 cgd struct group *
82 1.1 cgd getgrnam(name)
83 1.1 cgd const char *name;
84 1.1 cgd {
85 1.1 cgd int rval;
86 1.1 cgd
87 1.1 cgd if (!start_gr())
88 1.1 cgd return(NULL);
89 1.1 cgd rval = grscan(1, 0, name);
90 1.1 cgd if (!_gr_stayopen)
91 1.1 cgd endgrent();
92 1.1 cgd return(rval ? &_gr_group : NULL);
93 1.1 cgd }
94 1.1 cgd
95 1.1 cgd struct group *
96 1.1 cgd #ifdef __STDC__
97 1.1 cgd getgrgid(gid_t gid)
98 1.1 cgd #else
99 1.1 cgd getgrgid(gid)
100 1.1 cgd gid_t gid;
101 1.1 cgd #endif
102 1.1 cgd {
103 1.1 cgd int rval;
104 1.1 cgd
105 1.1 cgd if (!start_gr())
106 1.1 cgd return(NULL);
107 1.1 cgd rval = grscan(1, gid, NULL);
108 1.1 cgd if (!_gr_stayopen)
109 1.1 cgd endgrent();
110 1.1 cgd return(rval ? &_gr_group : NULL);
111 1.1 cgd }
112 1.1 cgd
113 1.2 deraadt static int
114 1.1 cgd start_gr()
115 1.1 cgd {
116 1.1 cgd if (_gr_fp) {
117 1.1 cgd rewind(_gr_fp);
118 1.7 deraadt #ifdef YP
119 1.13 phil __ypmode = YPMODE_NONE;
120 1.16 lukem if (__ypcurrent)
121 1.7 deraadt free(__ypcurrent);
122 1.7 deraadt __ypcurrent = NULL;
123 1.7 deraadt #endif
124 1.1 cgd return(1);
125 1.1 cgd }
126 1.1 cgd return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
127 1.1 cgd }
128 1.1 cgd
129 1.5 jtc void
130 1.1 cgd setgrent()
131 1.1 cgd {
132 1.5 jtc (void) setgroupent(0);
133 1.1 cgd }
134 1.1 cgd
135 1.1 cgd int
136 1.1 cgd setgroupent(stayopen)
137 1.1 cgd int stayopen;
138 1.1 cgd {
139 1.1 cgd if (!start_gr())
140 1.1 cgd return(0);
141 1.1 cgd _gr_stayopen = stayopen;
142 1.1 cgd return(1);
143 1.1 cgd }
144 1.1 cgd
145 1.1 cgd void
146 1.1 cgd endgrent()
147 1.1 cgd {
148 1.1 cgd if (_gr_fp) {
149 1.1 cgd (void)fclose(_gr_fp);
150 1.1 cgd _gr_fp = NULL;
151 1.7 deraadt #ifdef YP
152 1.13 phil __ypmode = YPMODE_NONE;
153 1.16 lukem if (__ypcurrent)
154 1.7 deraadt free(__ypcurrent);
155 1.7 deraadt __ypcurrent = NULL;
156 1.7 deraadt #endif
157 1.1 cgd }
158 1.1 cgd }
159 1.1 cgd
160 1.2 deraadt static int
161 1.1 cgd grscan(search, gid, name)
162 1.16 lukem int search, gid;
163 1.16 lukem const char *name;
164 1.1 cgd {
165 1.16 lukem char *cp, **m;
166 1.1 cgd char *bp;
167 1.13 phil #ifdef YP
168 1.15 thorpej char *key, *data;
169 1.15 thorpej int keylen, datalen;
170 1.15 thorpej int r;
171 1.13 phil char *grname = (char *)NULL;
172 1.13 phil #endif
173 1.1 cgd
174 1.1 cgd for (;;) {
175 1.2 deraadt #ifdef YP
176 1.16 lukem if (__ypmode != YPMODE_NONE) {
177 1.2 deraadt
178 1.16 lukem if (!__ypdomain) {
179 1.16 lukem if (yp_get_default_domain(&__ypdomain)) {
180 1.13 phil __ypmode = YPMODE_NONE;
181 1.16 lukem if (grname != (char *)NULL) {
182 1.13 phil free(grname);
183 1.13 phil grname = (char *)NULL;
184 1.13 phil }
185 1.2 deraadt continue;
186 1.2 deraadt }
187 1.2 deraadt }
188 1.13 phil switch(__ypmode) {
189 1.13 phil case YPMODE_FULL:
190 1.16 lukem data = NULL;
191 1.16 lukem if (__ypcurrent) {
192 1.17 lukem key = NULL;
193 1.13 phil r = yp_next(__ypdomain, "group.byname",
194 1.13 phil __ypcurrent, __ypcurrentlen,
195 1.13 phil &key, &keylen, &data, &datalen);
196 1.13 phil free(__ypcurrent);
197 1.17 lukem if (r != 0) {
198 1.13 phil __ypcurrent = NULL;
199 1.17 lukem if (key)
200 1.17 lukem free(key);
201 1.17 lukem }
202 1.16 lukem else {
203 1.16 lukem __ypcurrent = key;
204 1.16 lukem __ypcurrentlen = keylen;
205 1.13 phil }
206 1.13 phil } else {
207 1.13 phil r = yp_first(__ypdomain, "group.byname",
208 1.13 phil &__ypcurrent, &__ypcurrentlen,
209 1.13 phil &data, &datalen);
210 1.16 lukem }
211 1.16 lukem if (r != 0) {
212 1.16 lukem __ypmode = YPMODE_NONE;
213 1.16 lukem if (data)
214 1.13 phil free(data);
215 1.16 lukem continue;
216 1.2 deraadt }
217 1.16 lukem bcopy(data, line, datalen);
218 1.16 lukem free(data);
219 1.13 phil break;
220 1.13 phil case YPMODE_NAME:
221 1.16 lukem if (grname != (char *)NULL) {
222 1.16 lukem data = NULL;
223 1.13 phil r = yp_match(__ypdomain, "group.byname",
224 1.13 phil grname, strlen(grname),
225 1.13 phil &data, &datalen);
226 1.13 phil __ypmode = YPMODE_NONE;
227 1.13 phil free(grname);
228 1.13 phil grname = (char *)NULL;
229 1.16 lukem if (r != 0) {
230 1.16 lukem if (data)
231 1.16 lukem free(data);
232 1.13 phil continue;
233 1.13 phil }
234 1.13 phil bcopy(data, line, datalen);
235 1.2 deraadt free(data);
236 1.13 phil } else {
237 1.16 lukem /* YP not available? */
238 1.16 lukem __ypmode = YPMODE_NONE;
239 1.2 deraadt continue;
240 1.2 deraadt }
241 1.13 phil break;
242 1.2 deraadt }
243 1.2 deraadt line[datalen] = '\0';
244 1.2 deraadt bp = line;
245 1.2 deraadt goto parse;
246 1.2 deraadt }
247 1.16 lukem #endif /* YP */
248 1.1 cgd if (!fgets(line, sizeof(line), _gr_fp))
249 1.1 cgd return(0);
250 1.1 cgd bp = line;
251 1.1 cgd /* skip lines that are too big */
252 1.6 jtc if (!strchr(line, '\n')) {
253 1.1 cgd int ch;
254 1.1 cgd
255 1.1 cgd while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
256 1.1 cgd ;
257 1.1 cgd continue;
258 1.1 cgd }
259 1.2 deraadt #ifdef YP
260 1.13 phil if (line[0] == '+') {
261 1.13 phil switch(line[1]) {
262 1.13 phil case ':':
263 1.13 phil case '\0':
264 1.13 phil case '\n':
265 1.16 lukem if (_yp_check(NULL)) {
266 1.15 thorpej if (!search) {
267 1.15 thorpej __ypmode = YPMODE_FULL;
268 1.15 thorpej continue;
269 1.15 thorpej }
270 1.16 lukem if (!__ypdomain &&
271 1.15 thorpej yp_get_default_domain(&__ypdomain))
272 1.15 thorpej continue;
273 1.16 lukem data = NULL;
274 1.15 thorpej if (name) {
275 1.15 thorpej r = yp_match(__ypdomain,
276 1.15 thorpej "group.byname",
277 1.15 thorpej name, strlen(name),
278 1.15 thorpej &data, &datalen);
279 1.15 thorpej } else {
280 1.15 thorpej char buf[20];
281 1.15 thorpej sprintf(buf, "%d", gid);
282 1.15 thorpej r = yp_match(__ypdomain,
283 1.15 thorpej "group.bygid",
284 1.15 thorpej buf, strlen(buf),
285 1.15 thorpej &data, &datalen);
286 1.15 thorpej }
287 1.16 lukem if (r != 0) {
288 1.16 lukem if (data)
289 1.16 lukem free(data);
290 1.15 thorpej continue;
291 1.16 lukem }
292 1.15 thorpej bcopy(data, line, datalen);
293 1.15 thorpej free(data);
294 1.15 thorpej line[datalen] = '\0';
295 1.15 thorpej bp = line;
296 1.15 thorpej _gr_group.gr_name = strsep(&bp, ":\n");
297 1.15 thorpej _gr_group.gr_passwd =
298 1.16 lukem strsep(&bp, ":\n");
299 1.15 thorpej if (!(cp = strsep(&bp, ":\n")))
300 1.15 thorpej continue;
301 1.15 thorpej _gr_group.gr_gid =
302 1.16 lukem name ? atoi(cp) : gid;
303 1.15 thorpej goto found_it;
304 1.12 pk }
305 1.13 phil break;
306 1.13 phil default:
307 1.16 lukem if (_yp_check(NULL)) {
308 1.16 lukem char *tptr;
309 1.13 phil
310 1.15 thorpej tptr = strsep(&bp, ":\n");
311 1.16 lukem if (search && name &&
312 1.16 lukem strcmp(tptr, name))
313 1.15 thorpej continue;
314 1.13 phil __ypmode = YPMODE_NAME;
315 1.13 phil grname = strdup(tptr + 1);
316 1.12 pk continue;
317 1.12 pk }
318 1.13 phil break;
319 1.2 deraadt }
320 1.2 deraadt }
321 1.2 deraadt parse:
322 1.16 lukem #endif /* YP */
323 1.1 cgd _gr_group.gr_name = strsep(&bp, ":\n");
324 1.1 cgd if (search && name && strcmp(_gr_group.gr_name, name))
325 1.1 cgd continue;
326 1.1 cgd _gr_group.gr_passwd = strsep(&bp, ":\n");
327 1.1 cgd if (!(cp = strsep(&bp, ":\n")))
328 1.1 cgd continue;
329 1.1 cgd _gr_group.gr_gid = atoi(cp);
330 1.1 cgd if (search && name == NULL && _gr_group.gr_gid != gid)
331 1.1 cgd continue;
332 1.15 thorpej found_it:
333 1.8 deraadt cp = NULL;
334 1.9 deraadt if (bp == NULL)
335 1.9 deraadt continue;
336 1.8 deraadt for (m = _gr_group.gr_mem = members;; bp++) {
337 1.8 deraadt if (m == &members[MAXGRP - 1])
338 1.1 cgd break;
339 1.8 deraadt if (*bp == ',') {
340 1.8 deraadt if (cp) {
341 1.8 deraadt *bp = '\0';
342 1.8 deraadt *m++ = cp;
343 1.8 deraadt cp = NULL;
344 1.8 deraadt }
345 1.8 deraadt } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
346 1.8 deraadt if (cp) {
347 1.8 deraadt *bp = '\0';
348 1.8 deraadt *m++ = cp;
349 1.8 deraadt }
350 1.1 cgd break;
351 1.8 deraadt } else if (cp == NULL)
352 1.8 deraadt cp = bp;
353 1.1 cgd }
354 1.8 deraadt *m = NULL;
355 1.1 cgd return(1);
356 1.1 cgd }
357 1.1 cgd /* NOTREACHED */
358 1.1 cgd }
359