getgrent.c revision 1.15 1 1.15 thorpej /* $NetBSD: getgrent.c,v 1.15 1997/01/22 01:21:06 thorpej 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.15 thorpej static char rcsid[] = "$NetBSD: getgrent.c,v 1.15 1997/01/22 01:21:06 thorpej 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.7 deraadt 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.7 deraadt 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.1 cgd register int search, gid;
163 1.14 cgd register const char *name;
164 1.1 cgd {
165 1.1 cgd register 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.13 phil if(__ypmode != YPMODE_NONE) {
177 1.2 deraadt
178 1.2 deraadt if(!__ypdomain) {
179 1.2 deraadt if(yp_get_default_domain(&__ypdomain)) {
180 1.13 phil __ypmode = YPMODE_NONE;
181 1.13 phil 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.13 phil if(__ypcurrent) {
191 1.13 phil r = yp_next(__ypdomain, "group.byname",
192 1.13 phil __ypcurrent, __ypcurrentlen,
193 1.13 phil &key, &keylen, &data, &datalen);
194 1.13 phil free(__ypcurrent);
195 1.13 phil if(r != 0) {
196 1.13 phil __ypcurrent = NULL;
197 1.13 phil __ypmode = YPMODE_NONE;
198 1.13 phil free(data);
199 1.13 phil continue;
200 1.13 phil }
201 1.13 phil __ypcurrent = key;
202 1.13 phil __ypcurrentlen = keylen;
203 1.13 phil bcopy(data, line, datalen);
204 1.13 phil free(data);
205 1.13 phil } else {
206 1.13 phil r = yp_first(__ypdomain, "group.byname",
207 1.13 phil &__ypcurrent, &__ypcurrentlen,
208 1.13 phil &data, &datalen);
209 1.13 phil if(r != 0) {
210 1.13 phil __ypmode = YPMODE_NONE;
211 1.13 phil free(data);
212 1.13 phil continue;
213 1.13 phil }
214 1.13 phil bcopy(data, line, datalen);
215 1.2 deraadt free(data);
216 1.2 deraadt }
217 1.13 phil break;
218 1.13 phil case YPMODE_NAME:
219 1.13 phil if(grname != (char *)NULL) {
220 1.13 phil r = yp_match(__ypdomain, "group.byname",
221 1.13 phil grname, strlen(grname),
222 1.13 phil &data, &datalen);
223 1.13 phil __ypmode = YPMODE_NONE;
224 1.13 phil free(grname);
225 1.13 phil grname = (char *)NULL;
226 1.13 phil if(r != 0) {
227 1.13 phil free(data);
228 1.13 phil continue;
229 1.13 phil }
230 1.13 phil bcopy(data, line, datalen);
231 1.2 deraadt free(data);
232 1.13 phil } else {
233 1.13 phil __ypmode = YPMODE_NONE; /* ??? */
234 1.2 deraadt continue;
235 1.2 deraadt }
236 1.13 phil break;
237 1.2 deraadt }
238 1.2 deraadt line[datalen] = '\0';
239 1.2 deraadt bp = line;
240 1.2 deraadt goto parse;
241 1.2 deraadt }
242 1.2 deraadt #endif
243 1.1 cgd if (!fgets(line, sizeof(line), _gr_fp))
244 1.1 cgd return(0);
245 1.1 cgd bp = line;
246 1.1 cgd /* skip lines that are too big */
247 1.6 jtc if (!strchr(line, '\n')) {
248 1.1 cgd int ch;
249 1.1 cgd
250 1.1 cgd while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
251 1.1 cgd ;
252 1.1 cgd continue;
253 1.1 cgd }
254 1.2 deraadt #ifdef YP
255 1.13 phil if (line[0] == '+') {
256 1.13 phil switch(line[1]) {
257 1.13 phil case ':':
258 1.13 phil case '\0':
259 1.13 phil case '\n':
260 1.13 phil if(_yp_check(NULL)) {
261 1.15 thorpej if (!search) {
262 1.15 thorpej __ypmode = YPMODE_FULL;
263 1.15 thorpej continue;
264 1.15 thorpej }
265 1.15 thorpej if(!__ypdomain &&
266 1.15 thorpej yp_get_default_domain(&__ypdomain))
267 1.15 thorpej continue;
268 1.15 thorpej if (name) {
269 1.15 thorpej r = yp_match(__ypdomain,
270 1.15 thorpej "group.byname",
271 1.15 thorpej name, strlen(name),
272 1.15 thorpej &data, &datalen);
273 1.15 thorpej } else {
274 1.15 thorpej char buf[20];
275 1.15 thorpej sprintf(buf, "%d", gid);
276 1.15 thorpej r = yp_match(__ypdomain,
277 1.15 thorpej "group.bygid",
278 1.15 thorpej buf, strlen(buf),
279 1.15 thorpej &data, &datalen);
280 1.15 thorpej }
281 1.15 thorpej if (r != 0)
282 1.15 thorpej continue;
283 1.15 thorpej bcopy(data, line, datalen);
284 1.15 thorpej free(data);
285 1.15 thorpej line[datalen] = '\0';
286 1.15 thorpej bp = line;
287 1.15 thorpej _gr_group.gr_name = strsep(&bp, ":\n");
288 1.15 thorpej _gr_group.gr_passwd =
289 1.15 thorpej strsep(&bp, ":\n");
290 1.15 thorpej if (!(cp = strsep(&bp, ":\n")))
291 1.15 thorpej continue;
292 1.15 thorpej _gr_group.gr_gid =
293 1.15 thorpej name ? atoi(cp) : gid;
294 1.15 thorpej goto found_it;
295 1.12 pk }
296 1.13 phil break;
297 1.13 phil default:
298 1.13 phil if(_yp_check(NULL)) {
299 1.13 phil register char *tptr;
300 1.13 phil
301 1.15 thorpej tptr = strsep(&bp, ":\n");
302 1.15 thorpej if (search && name && strcmp(tptr, name))
303 1.15 thorpej continue;
304 1.13 phil __ypmode = YPMODE_NAME;
305 1.13 phil grname = strdup(tptr + 1);
306 1.12 pk continue;
307 1.12 pk }
308 1.13 phil break;
309 1.2 deraadt }
310 1.2 deraadt }
311 1.2 deraadt parse:
312 1.2 deraadt #endif
313 1.1 cgd _gr_group.gr_name = strsep(&bp, ":\n");
314 1.1 cgd if (search && name && strcmp(_gr_group.gr_name, name))
315 1.1 cgd continue;
316 1.1 cgd _gr_group.gr_passwd = strsep(&bp, ":\n");
317 1.1 cgd if (!(cp = strsep(&bp, ":\n")))
318 1.1 cgd continue;
319 1.1 cgd _gr_group.gr_gid = atoi(cp);
320 1.1 cgd if (search && name == NULL && _gr_group.gr_gid != gid)
321 1.1 cgd continue;
322 1.15 thorpej found_it:
323 1.8 deraadt cp = NULL;
324 1.9 deraadt if (bp == NULL)
325 1.9 deraadt continue;
326 1.8 deraadt for (m = _gr_group.gr_mem = members;; bp++) {
327 1.8 deraadt if (m == &members[MAXGRP - 1])
328 1.1 cgd break;
329 1.8 deraadt if (*bp == ',') {
330 1.8 deraadt if (cp) {
331 1.8 deraadt *bp = '\0';
332 1.8 deraadt *m++ = cp;
333 1.8 deraadt cp = NULL;
334 1.8 deraadt }
335 1.8 deraadt } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
336 1.8 deraadt if (cp) {
337 1.8 deraadt *bp = '\0';
338 1.8 deraadt *m++ = cp;
339 1.8 deraadt }
340 1.1 cgd break;
341 1.8 deraadt } else if (cp == NULL)
342 1.8 deraadt cp = bp;
343 1.1 cgd }
344 1.8 deraadt *m = NULL;
345 1.1 cgd return(1);
346 1.1 cgd }
347 1.1 cgd /* NOTREACHED */
348 1.1 cgd }
349