getgrent.c revision 1.19.2.1 1 /* $NetBSD: getgrent.c,v 1.19.2.1 1997/05/24 04:50:54 lukem 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 * Portions Copyright (c) 1997, Luke Mewburn. All Rights Reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #if defined(LIBC_SCCS) && !defined(lint)
39 #if 0
40 static char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 3/21/94";
41 #else
42 static char rcsid[] = "$NetBSD: getgrent.c,v 1.19.2.1 1997/05/24 04:50:54 lukem Exp $";
43 #endif
44 #endif /* LIBC_SCCS and not lint */
45
46 #include <sys/types.h>
47 #include <grp.h>
48 #include <limits.h>
49 #include <nsswitch.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <syslog.h>
54 #ifdef HESIOD
55 #include <hesiod.h>
56 #endif
57 #ifdef YP
58 #include <rpc/rpc.h>
59 #include <rpcsvc/yp_prot.h>
60 #include <rpcsvc/ypclnt.h>
61 #endif
62
63 static FILE *_gr_fp;
64 static struct group _gr_group;
65 static int _gr_stayopen;
66
67 static int grscan __P((int, int, const char *));
68 static int matchline __P((int, int, const char *));
69 static int start_gr __P((void));
70
71 #define MAXGRP 200
72 #define MAXLINELENGTH 1024
73
74 static char *members[MAXGRP];
75 static char line[MAXLINELENGTH];
76
77 #ifdef YP
78 enum _grmode { GRMODE_NONE, GRMODE_FULL, GRMODE_NAME };
79 static enum _grmode __grmode;
80 static char *__ypcurrent, *__ypdomain;
81 static int __ypcurrentlen;
82 #endif
83
84 #ifdef HESIOD
85 static int __hesindex;
86 #endif
87
88 struct group *
89 getgrent()
90 {
91 if ((!_gr_fp && !start_gr()) || !grscan(0, 0, NULL))
92 return(NULL);
93 return(&_gr_group);
94 }
95
96 struct group *
97 getgrnam(name)
98 const char *name;
99 {
100 int rval;
101
102 if (!start_gr())
103 return(NULL);
104 rval = grscan(1, 0, name);
105 if (!_gr_stayopen)
106 endgrent();
107 return(rval ? &_gr_group : NULL);
108 }
109
110 struct group *
111 getgrgid(gid)
112 gid_t gid;
113 {
114 int rval;
115
116 if (!start_gr())
117 return(NULL);
118 rval = grscan(1, gid, NULL);
119 if (!_gr_stayopen)
120 endgrent();
121 return(rval ? &_gr_group : NULL);
122 }
123
124 static int
125 start_gr()
126 {
127 #ifdef YP
128 __grmode = GRMODE_NONE;
129 if (__ypcurrent)
130 free(__ypcurrent);
131 __ypcurrent = NULL;
132 #endif
133 #ifdef HESIOD
134 __hesindex = 0;
135 #endif
136 if (_gr_fp) {
137 rewind(_gr_fp);
138 return(1);
139 }
140 return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
141 }
142
143 void
144 setgrent()
145 {
146 (void) setgroupent(0);
147 }
148
149 int
150 setgroupent(stayopen)
151 int stayopen;
152 {
153 if (!start_gr())
154 return(0);
155 _gr_stayopen = stayopen;
156 return(1);
157 }
158
159 void
160 endgrent()
161 {
162 #ifdef YP
163 __grmode = GRMODE_NONE;
164 if (__ypcurrent)
165 free(__ypcurrent);
166 __ypcurrent = NULL;
167 #endif
168 #ifdef HESIOD
169 __hesindex = 0;
170 #endif
171 if (_gr_fp) {
172 (void)fclose(_gr_fp);
173 _gr_fp = NULL;
174 }
175 }
176
177 static int
178 _local_grscan(rv, cb_data, ap)
179 void *rv;
180 void *cb_data;
181 va_list ap;
182 {
183 int search = va_arg(ap, int);
184 int gid = va_arg(ap, int);
185 const char *name = va_arg(ap, const char *);
186
187 for (;;) {
188 if (!fgets(line, sizeof(line), _gr_fp))
189 return(NS_NOTFOUND);
190 /* skip lines that are too big */
191 if (!strchr(line, '\n')) {
192 int ch;
193
194 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
195 ;
196 continue;
197 }
198 if (matchline(search, gid, name))
199 return(NS_SUCCESS);
200 }
201 /* NOTREACHED */
202 }
203
204 #ifdef HESIOD
205 static int
206 _dns_grscan(rv, cb_data, ap)
207 void *rv;
208 void *cb_data;
209 va_list ap;
210 {
211 int search = va_arg(ap, int);
212 int gid = va_arg(ap, int);
213 const char *name = va_arg(ap, const char *);
214
215 char **hp;
216
217 for (;;) {
218 if (search) {
219 if (name)
220 strncpy(line, name, sizeof(line));
221 else
222 snprintf(line, sizeof(line), "%u", gid);
223 } else {
224 snprintf(line, sizeof(line), "group-%u", __hesindex);
225 __hesindex++;
226 }
227
228 line[sizeof(line) - 1] = '\0';
229 hp = hes_resolve(line, "group");
230 if (hp == NULL) {
231 switch (hes_error()) {
232 case HES_ER_NOTFOUND:
233 if (! search)
234 __hesindex = 0;
235 return(NS_NOTFOUND);
236 case HES_ER_OK:
237 abort();
238 default:
239 return(NS_UNAVAIL);
240 }
241 }
242
243 /* only check first elem */
244 strncpy(line, hp[0], sizeof(line));
245 line[sizeof(line) - 1] = '\0';
246 hes_free(hp);
247 if (matchline(search, gid, name))
248 return(NS_SUCCESS);
249 else if (search)
250 return(NS_NOTFOUND);
251 }
252 }
253 #endif
254
255 #ifdef YP
256 static int
257 _nis_grscan(rv, cb_data, ap)
258 void *rv;
259 void *cb_data;
260 va_list ap;
261 {
262 int search = va_arg(ap, int);
263 int gid = va_arg(ap, int);
264 const char *name = va_arg(ap, const char *);
265
266 char *key, *data;
267 int keylen, datalen;
268 int r;
269
270 if(__ypdomain == NULL) {
271 switch (yp_get_default_domain(&__ypdomain)) {
272 case 0:
273 break;
274 case YPERR_RESRC:
275 return(NS_TRYAGAIN);
276 default:
277 return(NS_UNAVAIL);
278 }
279 }
280
281 if (search) { /* specific group or gid */
282 if (name)
283 strncpy(line, name, sizeof(line));
284 else
285 snprintf(line, sizeof(line), "%u", gid);
286 line[sizeof(line) - 1] = '\0';
287 data = NULL;
288 r = yp_match(__ypdomain,
289 (name) ? "group.byname" : "group.bygid",
290 line, strlen(line), &data, &datalen);
291 switch (r) {
292 case 0:
293 break;
294 case YPERR_KEY:
295 if (data)
296 free(data);
297 return(NS_NOTFOUND);
298 default:
299 if (data)
300 free(data);
301 return(NS_UNAVAIL);
302 }
303 data[datalen] = '\0'; /* clear trailing \n */
304 strncpy(line, data, sizeof(line));
305 line[sizeof(line) - 1] = '\0';
306 free(data);
307 if (matchline(search, gid, name))
308 return(NS_SUCCESS);
309 else
310 return(NS_NOTFOUND);
311 }
312
313 for (;;) {
314 data = NULL;
315 if(__ypcurrent) {
316 key = NULL;
317 r = yp_next(__ypdomain, "group.byname",
318 __ypcurrent, __ypcurrentlen,
319 &key, &keylen, &data, &datalen);
320 free(__ypcurrent);
321 switch (r) {
322 case 0:
323 break;
324 case YPERR_NOMORE:
325 __ypcurrent = NULL;
326 if (key)
327 free(key);
328 if (data)
329 free(data);
330 return(NS_NOTFOUND);
331 default:
332 if (key)
333 free(key);
334 if (data)
335 free(data);
336 return(NS_UNAVAIL);
337 }
338 __ypcurrent = key;
339 __ypcurrentlen = keylen;
340 } else {
341 if (yp_first(__ypdomain, "group.byname",
342 &__ypcurrent, &__ypcurrentlen,
343 &data, &datalen)) {
344 if (data);
345 free(data);
346 return(NS_UNAVAIL);
347 }
348 }
349 data[datalen] = '\0'; /* clear trailing \n */
350 strncpy(line, data, sizeof(line));
351 line[sizeof(line) - 1] = '\0';
352 free(data);
353 if (matchline(search, gid, name))
354 return(NS_SUCCESS);
355 }
356 /* NOTREACHED */
357 }
358 #endif
359
360 #if defined(YP) || defined(HESIOD)
361 /*
362 * log an error if "files" or "compat" is specified in group_compat database
363 */
364 static int
365 _bad_grscan(rv, cb_data, ap)
366 void *rv;
367 void *cb_data;
368 va_list ap;
369 {
370 static int warned;
371 if (!warned) {
372 syslog(LOG_ERR,
373 "nsswitch.conf group_compat database can't use '%s'",
374 (char *)cb_data);
375 }
376 warned = 1;
377 return NS_UNAVAIL;
378 }
379
380 /*
381 * when a name lookup in compat mode is required, look it up in group_compat
382 * nsswitch database. only Hesiod and NIS is supported - it doesn't make
383 * sense to lookup compat names from 'files' or 'compat'
384 */
385 static int
386 __grscancompat(search, gid, name)
387 int search, gid;
388 const char *name;
389 {
390 static ns_dtab dtab;
391
392 NS_FILES_CB(dtab, _bad_grscan, "files");
393 NS_DNS_CB(dtab, _dns_grscan, NULL);
394 NS_NIS_CB(dtab, _nis_grscan, NULL);
395 NS_COMPAT_CB(dtab, _bad_grscan, "compat");
396
397 return nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, search, gid, name);
398 }
399
400
401 static int
402 _compat_grscan(rv, cb_data, ap)
403 void *rv;
404 void *cb_data;
405 va_list ap;
406 {
407 int search = va_arg(ap, int);
408 int gid = va_arg(ap, int);
409 const char *name = va_arg(ap, const char *);
410
411 static char *grname = NULL;
412
413 for (;;) {
414 if(__grmode != GRMODE_NONE) {
415 int r;
416
417 switch(__grmode) {
418 case GRMODE_FULL:
419 r = __grscancompat(search, gid, name);
420 if (r == NS_SUCCESS)
421 return(r);
422 __grmode = GRMODE_NONE;
423 break;
424 case GRMODE_NAME:
425 if(grname == (char *)NULL) {
426 __grmode = GRMODE_NONE;
427 break;
428 }
429 r = __grscancompat(1, 0, grname);
430 free(grname);
431 grname = (char *)NULL;
432 if (r != NS_SUCCESS)
433 break;
434 if (!search)
435 return NS_SUCCESS;
436 if (name) {
437 if (! strcmp(_gr_group.gr_name, name))
438 return NS_SUCCESS;
439 } else {
440 if (_gr_group.gr_gid == gid)
441 return NS_SUCCESS;
442 }
443 break;
444 case GRMODE_NONE:
445 abort();
446 }
447 continue;
448 }
449
450 if (!fgets(line, sizeof(line), _gr_fp))
451 return(NS_NOTFOUND);
452 /* skip lines that are too big */
453 if (!strchr(line, '\n')) {
454 int ch;
455
456 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
457 ;
458 continue;
459 }
460
461 if (line[0] == '+') {
462 char *tptr, *bp;
463
464 switch(line[1]) {
465 case ':':
466 case '\0':
467 case '\n':
468 __grmode = GRMODE_FULL;
469 break;
470 default:
471 __grmode = GRMODE_NAME;
472 bp = line;
473 tptr = strsep(&bp, ":\n");
474 grname = strdup(tptr + 1);
475 break;
476 }
477 continue;
478 }
479 if (matchline(search, gid, name))
480 return(NS_SUCCESS);
481 }
482 /* NOTREACHED */
483 }
484 #endif /* YP || HESIOD */
485
486 static int
487 grscan(search, gid, name)
488 int search, gid;
489 const char *name;
490 {
491 int r;
492 static ns_dtab dtab;
493
494 NS_FILES_CB(dtab, _local_grscan, NULL);
495 NS_DNS_CB(dtab, _dns_grscan, NULL);
496 NS_NIS_CB(dtab, _nis_grscan, NULL);
497 NS_COMPAT_CB(dtab, _compat_grscan, NULL);
498
499 r = nsdispatch(NULL, dtab, NSDB_GROUP, search, gid, name);
500 return (r == NS_SUCCESS) ? 1 : 0;
501 }
502
503 static int
504 matchline(search, gid, name)
505 int search, gid;
506 const char *name;
507 {
508 unsigned long id;
509 char *cp, **m;
510 char *bp, *ep;
511
512 if (line[0] == '+')
513 return(0); /* sanity check to prevent recursion */
514 bp = line;
515 _gr_group.gr_name = strsep(&bp, ":\n");
516 if (search && name && strcmp(_gr_group.gr_name, name))
517 return(0);
518 _gr_group.gr_passwd = strsep(&bp, ":\n");
519 if (!(cp = strsep(&bp, ":\n")))
520 return(0);
521 id = strtoul(cp, &ep, 10);
522 if (id > GID_MAX || *ep != '\0')
523 return(0);
524 _gr_group.gr_gid = (gid_t)id;
525 if (search && name == NULL && _gr_group.gr_gid != gid)
526 return(0);
527 cp = NULL;
528 if (bp == NULL)
529 return(0);
530 for (m = _gr_group.gr_mem = members;; bp++) {
531 if (m == &members[MAXGRP - 1])
532 break;
533 if (*bp == ',') {
534 if (cp) {
535 *bp = '\0';
536 *m++ = cp;
537 cp = NULL;
538 }
539 } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
540 if (cp) {
541 *bp = '\0';
542 *m++ = cp;
543 }
544 break;
545 } else if (cp == NULL)
546 cp = bp;
547 }
548 *m = NULL;
549 return(1);
550 }
551