getgrent.c revision 1.2 1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)getgrent.c 5.9 (Berkeley) 4/1/91";
36 #endif /* LIBC_SCCS and not lint */
37
38 #include <sys/types.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <grp.h>
43 #ifdef YP
44 #include <rpc/rpc.h>
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.h>
47 #endif
48
49 static FILE *_gr_fp;
50 static struct group _gr_group;
51 static int _gr_stayopen;
52 static int grscan(), start_gr();
53
54 #define MAXGRP 200
55 static char *members[MAXGRP];
56 #define MAXLINELENGTH 1024
57 static char line[MAXLINELENGTH];
58
59 #ifdef YP
60 static char *__ypcurrent, *__ypdomain;
61 static int __ypcurrentlen, __ypmode=0;
62 #endif
63
64 struct group *
65 getgrent()
66 {
67 if (!_gr_fp && !start_gr() || !grscan(0, 0, NULL))
68 return(NULL);
69 return(&_gr_group);
70 }
71
72 struct group *
73 getgrnam(name)
74 const char *name;
75 {
76 int rval;
77
78 if (!start_gr())
79 return(NULL);
80 rval = grscan(1, 0, name);
81 if (!_gr_stayopen)
82 endgrent();
83 return(rval ? &_gr_group : NULL);
84 }
85
86 struct group *
87 #ifdef __STDC__
88 getgrgid(gid_t gid)
89 #else
90 getgrgid(gid)
91 gid_t gid;
92 #endif
93 {
94 int rval;
95
96 if (!start_gr())
97 return(NULL);
98 rval = grscan(1, gid, NULL);
99 if (!_gr_stayopen)
100 endgrent();
101 return(rval ? &_gr_group : NULL);
102 }
103
104 static int
105 start_gr()
106 {
107 if (_gr_fp) {
108 rewind(_gr_fp);
109 return(1);
110 }
111 return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
112 }
113
114 int
115 setgrent()
116 {
117 return(setgroupent(0));
118 }
119
120 int
121 setgroupent(stayopen)
122 int stayopen;
123 {
124 if (!start_gr())
125 return(0);
126 _gr_stayopen = stayopen;
127 return(1);
128 }
129
130 void
131 endgrent()
132 {
133 if (_gr_fp) {
134 (void)fclose(_gr_fp);
135 _gr_fp = NULL;
136 }
137 }
138
139 static int
140 grscan(search, gid, name)
141 register int search, gid;
142 register char *name;
143 {
144 register char *cp, **m;
145 char *bp;
146 char *fgets(), *strsep(), *index();
147
148 for (;;) {
149 #ifdef YP
150 if(__ypmode) {
151 char *key, *data;
152 int keylen, datalen;
153 int r;
154
155 if(!__ypdomain) {
156 if(yp_get_default_domain(&__ypdomain)) {
157 __ypmode = 0;
158 continue;
159 }
160 }
161 if(__ypcurrent) {
162 r = yp_next(__ypdomain, "group.byname",
163 __ypcurrent, __ypcurrentlen,
164 &key, &keylen, &data, &datalen);
165 free(__ypcurrent);
166 /*printf("yp_next %d\n", r);*/
167 switch(r) {
168 case 0:
169 break;
170 default:
171 __ypcurrent = NULL;
172 __ypmode = 0;
173 free(data);
174 continue;
175 }
176 __ypcurrent = key;
177 __ypcurrentlen = keylen;
178 bcopy(data, line, datalen);
179 free(data);
180 } else {
181 r = yp_first(__ypdomain, "group.byname",
182 &__ypcurrent, &__ypcurrentlen,
183 &data, &datalen);
184 /*printf("yp_first %d\n", r);*/
185 switch(r) {
186 case 0:
187 break;
188 default:
189 __ypmode = 0;
190 free(data);
191 continue;
192 }
193 bcopy(data, line, datalen);
194 free(data);
195 }
196 line[datalen] = '\0';
197 /*printf("line = %s\n", line);*/
198 bp = line;
199 goto parse;
200 }
201 #endif
202 if (!fgets(line, sizeof(line), _gr_fp))
203 return(0);
204 bp = line;
205 /* skip lines that are too big */
206 if (!index(line, '\n')) {
207 int ch;
208
209 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
210 ;
211 continue;
212 }
213 #ifdef YP
214 if ((strcmp("+\n", line) == 0) || (strncmp("+:*:0:", line, 5) == 0)) {
215 if(_yp_check(NULL)) {
216 __ypmode = 1;
217 continue;
218 }
219 }
220 parse:
221 #endif
222 _gr_group.gr_name = strsep(&bp, ":\n");
223 if (search && name && strcmp(_gr_group.gr_name, name))
224 continue;
225 _gr_group.gr_passwd = strsep(&bp, ":\n");
226 if (!(cp = strsep(&bp, ":\n")))
227 continue;
228 _gr_group.gr_gid = atoi(cp);
229 if (search && name == NULL && _gr_group.gr_gid != gid)
230 continue;
231 for (m = _gr_group.gr_mem = members;; ++m) {
232 if (m == &members[MAXGRP - 1]) {
233 *m = NULL;
234 break;
235 }
236 if ((*m = strsep(&bp, ", \n")) == NULL)
237 break;
238 }
239 return(1);
240 }
241 /* NOTREACHED */
242 }
243