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