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