getgrent.c revision 1.19.2.4 1 /* $NetBSD: getgrent.c,v 1.19.2.4 1998/11/02 03:33:13 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 #include <sys/cdefs.h>
39 #if defined(LIBC_SCCS) && !defined(lint)
40 #if 0
41 static char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 3/21/94";
42 #else
43 __RCSID("$NetBSD: getgrent.c,v 1.19.2.4 1998/11/02 03:33:13 lukem Exp $");
44 #endif
45 #endif /* LIBC_SCCS and not lint */
46
47 #include "namespace.h"
48 #include <sys/types.h>
49 #include <grp.h>
50 #include <limits.h>
51 #include <nsswitch.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <syslog.h>
56 #ifdef HESIOD
57 #include <hesiod.h>
58 #endif
59 #ifdef YP
60 #include <rpc/rpc.h>
61 #include <rpcsvc/yp_prot.h>
62 #include <rpcsvc/ypclnt.h>
63 #endif
64
65 #ifdef __weak_alias
66 __weak_alias(endgrent,_endgrent);
67 __weak_alias(getgrent,_getgrent);
68 __weak_alias(getgrgid,_getgrgid);
69 __weak_alias(getgrnam,_getgrnam);
70 __weak_alias(setgrent,_setgrent);
71 __weak_alias(setgroupent,_setgroupent);
72 #endif
73
74 static FILE *_gr_fp;
75 static struct group _gr_group;
76 static int _gr_stayopen;
77 static int _gr_nomore;
78
79 static int grscan __P((int, gid_t, const char *));
80 static int matchline __P((int, gid_t, const char *));
81 static int start_gr __P((void));
82
83 #define MAXGRP 200
84 #define MAXLINELENGTH 1024
85
86 static __aconst char *members[MAXGRP];
87 static char line[MAXLINELENGTH];
88
89 #ifdef YP
90 enum _grmode { GRMODE_NONE, GRMODE_FULL, GRMODE_NAME };
91 static enum _grmode __grmode;
92 static char *__ypcurrent, *__ypdomain;
93 static int __ypcurrentlen;
94 #endif
95
96 #ifdef HESIOD
97 static int __gr_hesnum;
98 #endif
99
100 struct group *
101 getgrent()
102 {
103 _gr_nomore = 0;
104 if ((!_gr_fp && !start_gr()) || !grscan(0, 0, NULL) || _gr_nomore)
105 return(NULL);
106 return &_gr_group;
107 }
108
109 struct group *
110 getgrnam(name)
111 const char *name;
112 {
113 int rval;
114
115 if (!start_gr())
116 return NULL;
117 rval = grscan(1, 0, name);
118 if (!_gr_stayopen)
119 endgrent();
120 return (rval) ? &_gr_group : NULL;
121 }
122
123 struct group *
124 getgrgid(gid)
125 gid_t gid;
126 {
127 int rval;
128
129 if (!start_gr())
130 return NULL;
131 rval = grscan(1, gid, NULL);
132 if (!_gr_stayopen)
133 endgrent();
134 return (rval) ? &_gr_group : NULL;
135 }
136
137 static int
138 start_gr()
139 {
140 #ifdef YP
141 __grmode = GRMODE_NONE;
142 if (__ypcurrent)
143 free(__ypcurrent);
144 __ypcurrent = NULL;
145 #endif
146 #ifdef HESIOD
147 __gr_hesnum = 0;
148 #endif
149 if (_gr_fp) {
150 rewind(_gr_fp);
151 return 1;
152 }
153 return (_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0;
154 }
155
156 void
157 setgrent()
158 {
159 (void) setgroupent(0);
160 }
161
162 int
163 setgroupent(stayopen)
164 int stayopen;
165 {
166 if (!start_gr())
167 return 0;
168 _gr_stayopen = stayopen;
169 return 1;
170 }
171
172 void
173 endgrent()
174 {
175 #ifdef YP
176 __grmode = GRMODE_NONE;
177 if (__ypcurrent)
178 free(__ypcurrent);
179 __ypcurrent = NULL;
180 #endif
181 #ifdef HESIOD
182 __gr_hesnum = 0;
183 #endif
184 if (_gr_fp) {
185 (void)fclose(_gr_fp);
186 _gr_fp = NULL;
187 }
188 }
189
190
191 static int _local_grscan __P((void *, void *, va_list));
192
193 static int
194 _local_grscan(rv, cb_data, ap)
195 void *rv;
196 void *cb_data;
197 va_list ap;
198 {
199 int search = va_arg(ap, int);
200 gid_t gid = va_arg(ap, gid_t);
201 const char *name = va_arg(ap, const char *);
202
203 for (;;) {
204 if (!fgets(line, sizeof(line), _gr_fp)) {
205 if (!search) {
206 _gr_nomore = 1;
207 return NS_SUCCESS;
208 }
209 return NS_NOTFOUND;
210 }
211 /* skip lines that are too big */
212 if (!strchr(line, '\n')) {
213 int ch;
214
215 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
216 ;
217 continue;
218 }
219 if (matchline(search, gid, name))
220 return NS_SUCCESS;
221 }
222 /* NOTREACHED */
223 }
224
225 #ifdef HESIOD
226 static int _dns_grscan __P((void *, void *, va_list));
227
228 static int
229 _dns_grscan(rv, cb_data, ap)
230 void *rv;
231 void *cb_data;
232 va_list ap;
233 {
234 int search = va_arg(ap, int);
235 gid_t gid = va_arg(ap, gid_t);
236 const char *name = va_arg(ap, const char *);
237
238 char **hp;
239
240 for (;;) {
241 if (search) {
242 if (name)
243 strncpy(line, name, sizeof(line));
244 else
245 snprintf(line, sizeof(line), "%u", gid);
246 } else {
247 snprintf(line, sizeof(line), "group-%u", __gr_hesnum);
248 __gr_hesnum++;
249 }
250
251 line[sizeof(line) - 1] = '\0';
252 hp = hes_resolve(line, "group");
253 if (hp == NULL) {
254 switch (hes_error()) {
255 case HES_ER_NOTFOUND:
256 if (!search) {
257 __gr_hesnum = 0;
258 _gr_nomore = 1;
259 return NS_SUCCESS;
260 }
261 return NS_NOTFOUND;
262 case HES_ER_OK:
263 abort();
264 default:
265 return NS_UNAVAIL;
266 }
267 }
268
269 /* only check first elem */
270 strncpy(line, hp[0], sizeof(line));
271 line[sizeof(line) - 1] = '\0';
272 hes_free(hp);
273 if (matchline(search, gid, name))
274 return NS_SUCCESS;
275 else if (search)
276 return NS_NOTFOUND;
277 }
278 }
279 #endif
280
281 #ifdef YP
282 static int _nis_grscan __P((void *, void *, va_list));
283
284 static int
285 _nis_grscan(rv, cb_data, ap)
286 void *rv;
287 void *cb_data;
288 va_list ap;
289 {
290 int search = va_arg(ap, int);
291 gid_t gid = va_arg(ap, gid_t);
292 const char *name = va_arg(ap, const char *);
293
294 char *key, *data;
295 int keylen, datalen;
296 int r;
297
298 if(__ypdomain == NULL) {
299 switch (yp_get_default_domain(&__ypdomain)) {
300 case 0:
301 break;
302 case YPERR_RESRC:
303 return NS_TRYAGAIN;
304 default:
305 return NS_UNAVAIL;
306 }
307 }
308
309 if (search) { /* specific group or gid */
310 if (name)
311 strncpy(line, name, sizeof(line));
312 else
313 snprintf(line, sizeof(line), "%u", gid);
314 line[sizeof(line) - 1] = '\0';
315 data = NULL;
316 r = yp_match(__ypdomain,
317 (name) ? "group.byname" : "group.bygid",
318 line, (int)strlen(line), &data, &datalen);
319 switch (r) {
320 case 0:
321 break;
322 case YPERR_KEY:
323 if (data)
324 free(data);
325 return NS_NOTFOUND;
326 default:
327 if (data)
328 free(data);
329 return NS_UNAVAIL;
330 }
331 data[datalen] = '\0'; /* clear trailing \n */
332 strncpy(line, data, sizeof(line));
333 line[sizeof(line) - 1] = '\0';
334 free(data);
335 if (matchline(search, gid, name))
336 return NS_SUCCESS;
337 else
338 return NS_NOTFOUND;
339 }
340
341 for (;;) { /* ! search */
342 data = NULL;
343 if(__ypcurrent) {
344 key = NULL;
345 r = yp_next(__ypdomain, "group.byname",
346 __ypcurrent, __ypcurrentlen,
347 &key, &keylen, &data, &datalen);
348 free(__ypcurrent);
349 switch (r) {
350 case 0:
351 break;
352 case YPERR_NOMORE:
353 __ypcurrent = NULL;
354 if (key)
355 free(key);
356 if (data)
357 free(data);
358 _gr_nomore = 1;
359 return NS_SUCCESS;
360 default:
361 if (key)
362 free(key);
363 if (data)
364 free(data);
365 return NS_UNAVAIL;
366 }
367 __ypcurrent = key;
368 __ypcurrentlen = keylen;
369 } else {
370 if (yp_first(__ypdomain, "group.byname",
371 &__ypcurrent, &__ypcurrentlen,
372 &data, &datalen)) {
373 if (data);
374 free(data);
375 return NS_UNAVAIL;
376 }
377 }
378 data[datalen] = '\0'; /* clear trailing \n */
379 strncpy(line, data, sizeof(line));
380 line[sizeof(line) - 1] = '\0';
381 free(data);
382 if (matchline(search, gid, name))
383 return NS_SUCCESS;
384 }
385 /* NOTREACHED */
386 }
387 #endif
388
389 #if defined(YP) || defined(HESIOD)
390 /*
391 * log an error if "files" or "compat" is specified in group_compat database
392 */
393 static int _bad_grscan __P((void *, void *, va_list));
394
395 static int
396 _bad_grscan(rv, cb_data, ap)
397 void *rv;
398 void *cb_data;
399 va_list ap;
400 {
401 static int warned;
402
403 if (!warned) {
404 syslog(LOG_ERR,
405 "nsswitch.conf group_compat database can't use '%s'",
406 (char *)cb_data);
407 }
408 warned = 1;
409 return NS_UNAVAIL;
410 }
411
412 /*
413 * when a name lookup in compat mode is required, look it up in group_compat
414 * nsswitch database. only Hesiod and NIS is supported - it doesn't make
415 * sense to lookup compat names from 'files' or 'compat'
416 */
417
418 static int __grscancompat __P((int, gid_t, const char *));
419
420 static int
421 __grscancompat(search, gid, name)
422 int search;
423 gid_t gid;
424 const char *name;
425 {
426 static ns_dtab dtab;
427
428 if (dtab[NS_FILES].cb == NULL) {
429 NS_FILES_CB(dtab, _bad_grscan, "files");
430 NS_DNS_CB(dtab, _dns_grscan, NULL);
431 NS_NIS_CB(dtab, _nis_grscan, NULL);
432 NS_COMPAT_CB(dtab, _bad_grscan, "compat");
433 }
434
435 return nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, search, gid, name);
436 }
437
438
439 static int _compat_grscan __P((void *, void *, va_list));
440
441 static int
442 _compat_grscan(rv, cb_data, ap)
443 void *rv;
444 void *cb_data;
445 va_list ap;
446 {
447 int search = va_arg(ap, int);
448 gid_t gid = va_arg(ap, gid_t);
449 const char *name = va_arg(ap, const char *);
450
451 static char *grname = NULL;
452
453 for (;;) {
454 if(__grmode != GRMODE_NONE) {
455 int r;
456
457 switch(__grmode) {
458 case GRMODE_FULL:
459 r = __grscancompat(search, gid, name);
460 if (r == NS_SUCCESS)
461 return r;
462 __grmode = GRMODE_NONE;
463 break;
464 case GRMODE_NAME:
465 if(grname == (char *)NULL) {
466 __grmode = GRMODE_NONE;
467 break;
468 }
469 r = __grscancompat(1, 0, grname);
470 free(grname);
471 grname = (char *)NULL;
472 if (r != NS_SUCCESS)
473 break;
474 if (!search)
475 return NS_SUCCESS;
476 if (name) {
477 if (! strcmp(_gr_group.gr_name, name))
478 return NS_SUCCESS;
479 } else {
480 if (_gr_group.gr_gid == gid)
481 return NS_SUCCESS;
482 }
483 break;
484 case GRMODE_NONE:
485 abort();
486 }
487 continue;
488 }
489
490 if (!fgets(line, sizeof(line), _gr_fp))
491 return NS_NOTFOUND;
492 /* skip lines that are too big */
493 if (!strchr(line, '\n')) {
494 int ch;
495
496 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
497 ;
498 continue;
499 }
500
501 if (line[0] == '+') {
502 char *tptr, *bp;
503
504 switch(line[1]) {
505 case ':':
506 case '\0':
507 case '\n':
508 __grmode = GRMODE_FULL;
509 break;
510 default:
511 __grmode = GRMODE_NAME;
512 bp = line;
513 tptr = strsep(&bp, ":\n");
514 grname = strdup(tptr + 1);
515 break;
516 }
517 continue;
518 }
519 if (matchline(search, gid, name))
520 return NS_SUCCESS;
521 }
522 /* NOTREACHED */
523 }
524 #endif /* YP || HESIOD */
525
526 static int
527 grscan(search, gid, name)
528 int search;
529 gid_t gid;
530 const char *name;
531 {
532 int r;
533 static ns_dtab dtab;
534
535 if (dtab[NS_FILES].cb == NULL) {
536 NS_FILES_CB(dtab, _local_grscan, NULL);
537 NS_DNS_CB(dtab, _dns_grscan, NULL);
538 NS_NIS_CB(dtab, _nis_grscan, NULL);
539 NS_COMPAT_CB(dtab, _compat_grscan, NULL);
540 }
541
542 r = nsdispatch(NULL, dtab, NSDB_GROUP, search, gid, name);
543 return (r == NS_SUCCESS) ? 1 : 0;
544 }
545
546 static int
547 matchline(search, gid, name)
548 int search;
549 gid_t gid;
550 const char *name;
551 {
552 unsigned long id;
553 __aconst char **m;
554 char *cp, *bp, *ep;
555
556 if (line[0] == '+')
557 return 0; /* sanity check to prevent recursion */
558 bp = line;
559 _gr_group.gr_name = strsep(&bp, ":\n");
560 if (search && name && strcmp(_gr_group.gr_name, name))
561 return 0;
562 _gr_group.gr_passwd = strsep(&bp, ":\n");
563 if (!(cp = strsep(&bp, ":\n")))
564 return 0;
565 id = strtoul(cp, &ep, 10);
566 if (id > GID_MAX || *ep != '\0')
567 return 0;
568 _gr_group.gr_gid = (gid_t)id;
569 if (search && name == NULL && _gr_group.gr_gid != gid)
570 return 0;
571 cp = NULL;
572 if (bp == NULL)
573 return 0;
574 for (_gr_group.gr_mem = m = members;; bp++) {
575 if (m == &members[MAXGRP - 1])
576 break;
577 if (*bp == ',') {
578 if (cp) {
579 *bp = '\0';
580 *m++ = cp;
581 cp = NULL;
582 }
583 } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
584 if (cp) {
585 *bp = '\0';
586 *m++ = cp;
587 }
588 break;
589 } else if (cp == NULL)
590 cp = bp;
591 }
592 *m = NULL;
593 return 1;
594 }
595