1 /* $NetBSD: getgrent.c,v 1.34.2.1 1999/04/27 14:10:58 perry Exp $ */
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
5 * Portions Copyright (c) 1994, Jason Downs. All Rights Reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)getgrent.c 8.2 (Berkeley) 3/21/94
36 * $FreeBSD: src/lib/libc/gen/getgrent.c,v 1.17.6.1 2001/03/05 08:56:02 obrien Exp $
37 * $DragonFly: src/lib/libc/gen/getgrent.c,v 1.5 2005/11/19 22:32:53 swildner Exp $
42 #include <sys/types.h>
50 static struct group _gr_group
;
51 static int _gr_stayopen
;
53 static int grscan(int, gid_t
, const char *);
54 static int start_gr(void);
58 #include <rpcsvc/yp_prot.h>
59 #include <rpcsvc/ypclnt.h>
60 static int _gr_stepping_yp
;
61 static int _gr_yp_enabled
;
62 static int _getypgroup(struct group
*, const char *, const char *);
63 static int _nextypgroup(struct group
*);
66 /* initial size for malloc and increase steps for realloc */
68 #define MAXLINELENGTH 256
70 static char **members
; /* list of group members */
71 static int maxgrp
; /* current length of **mebers */
72 static char *line
; /* temp buffer for group line */
73 static int maxlinelength
; /* current length of *line */
76 * Lines longer than MAXLINELENGTHLIMIT will be counted as an error.
77 * <= 0 disable check for maximum line length
78 * 256K is enough for 64,000 uids
80 #define MAXLINELENGTHLIMIT (256 * 1024)
81 #define GROUP_IGNORE_COMMENTS 1 /* allow comments in /etc/group */
86 if (!_gr_fp
&& !start_gr()) {
91 if (_gr_stepping_yp
) {
92 if (_nextypgroup(&_gr_group
))
98 if (!grscan(0, 0, NULL
))
101 if(_gr_group
.gr_name
[0] == '+' && _gr_group
.gr_name
[1]) {
102 _getypgroup(&_gr_group
, &_gr_group
.gr_name
[1],
104 } else if(_gr_group
.gr_name
[0] == '+') {
105 if (!_nextypgroup(&_gr_group
))
115 getgrnam(const char *name
)
124 rval
= grscan(1, 0, name
);
126 if(rval
== -1 && (_gr_yp_enabled
< 0 || (_gr_yp_enabled
&&
127 _gr_group
.gr_name
[0] == '+'))) {
128 if (!(rval
= _getypgroup(&_gr_group
, name
, "group.byname")))
134 return (rval
) ? &_gr_group
: NULL
;
147 rval
= grscan(1, gid
, NULL
);
149 if(rval
== -1 && _gr_yp_enabled
) {
151 snprintf(buf
, sizeof buf
, "%d", (unsigned)gid
);
152 if (!(rval
= _getypgroup(&_gr_group
, buf
, "group.bygid")))
158 return (rval
) ? &_gr_group
: NULL
;
168 _gr_fp
= fopen(_PATH_GROUP
, "r");
169 if(!_gr_fp
) return 0;
172 * This is a disgusting hack, used to determine when YP is enabled.
173 * This would be easier if we had a group database to go along with
174 * the password database.
180 while((my_line
= fgetln(_gr_fp
, &linelen
)) != NULL
) {
181 if(my_line
[0] == '+') {
182 if(my_line
[1] && my_line
[1] != ':' && !_gr_yp_enabled
) {
194 if (maxlinelength
== 0) {
195 if ((line
= (char *)malloc(MAXLINELENGTH
)) == NULL
)
197 maxlinelength
+= MAXLINELENGTH
;
201 if ((members
= (char **) malloc(sizeof(char **) *
213 return setgroupent(0);
217 setgroupent(int stayopen
)
221 _gr_stayopen
= stayopen
;
235 (void)fclose(_gr_fp
);
241 grscan(int search
, gid_t gid
, const char *name
)
254 if (fgets(line
, maxlinelength
, _gr_fp
) == NULL
)
257 if (!index(line
, '\n')) {
262 /* don't allocate infinite memory */
263 if (MAXLINELENGTHLIMIT
> 0 &&
264 maxlinelength
>= MAXLINELENGTHLIMIT
)
267 if ((line
= reallocf(line
,
268 (maxlinelength
+ MAXLINELENGTH
))) == NULL
)
271 if (fgets(line
+ maxlinelength
- 1,
272 MAXLINELENGTH
+ 1, _gr_fp
) == NULL
)
275 maxlinelength
+= MAXLINELENGTH
;
276 } while (!index(line
+ maxlinelength
-
277 MAXLINELENGTH
- 1, '\n'));
280 #ifdef GROUP_IGNORE_COMMENTS
282 * Ignore comments: ^[ \t]*#
284 for (cp
= line
; *cp
!= '\0'; cp
++)
285 if (*cp
!= ' ' && *cp
!= '\t')
287 if (*cp
== '#' || *cp
== '\0')
293 if ((_gr_group
.gr_name
= strsep(&bp
, ":\n")) == NULL
)
297 * XXX We need to be careful to avoid proceeding
298 * past this point under certain circumstances or
299 * we risk dereferencing null pointers down below.
301 if (_gr_group
.gr_name
[0] == '+') {
302 if (strlen(_gr_group
.gr_name
) == 1) {
312 cp
= &_gr_group
.gr_name
[1];
313 if (search
&& name
!= NULL
)
314 if (strcmp(cp
, name
))
316 if (!_getypgroup(&_gr_group
, cp
,
319 if (search
&& name
== NULL
)
320 if (gid
!= _gr_group
.gr_gid
)
322 /* We're going to override -- tell the world. */
327 if (_gr_group
.gr_name
[0] == '+')
330 if (search
&& name
) {
331 if(strcmp(_gr_group
.gr_name
, name
)) {
336 if ((cp
= strsep(&bp
, ":\n")) == NULL
) {
342 if (strlen(cp
) || !_ypfound
)
343 _gr_group
.gr_passwd
= cp
;
345 if ((_gr_group
.gr_passwd
= strsep(&bp
, ":\n")) == NULL
)
348 if (!(cp
= strsep(&bp
, ":\n"))) {
358 * Hurm. Should we be doing this? We allow UIDs to
359 * be overridden -- what about GIDs?
363 _gr_group
.gr_gid
= atoi(cp
);
364 if (search
&& name
== NULL
&& _gr_group
.gr_gid
!= gid
)
367 if (bp
== NULL
) /* !!! Must check for this! */
370 if ((cp
= strsep(&bp
, ":\n")) == NULL
)
373 if (!strlen(cp
) && _ypfound
)
380 for (m
= members
; ; bp
++) {
381 if (m
== (members
+ maxgrp
- 1)) {
382 if ((members
= (char **)
385 (maxgrp
+ MAXGRP
))) == NULL
)
387 m
= members
+ maxgrp
- 1;
396 } else if (*bp
== '\0' || *bp
== '\n' || *bp
== ' ') {
402 } else if (cp
== NULL
)
406 _gr_group
.gr_mem
= members
;
417 _gr_breakout_yp(struct group
*gr
, char *result
)
423 * XXX If 's' ends up being a NULL pointer, punt on this group.
424 * It means the NIS group entry is badly formatted and should
427 if ((s
= strsep(&result
, ":")) == NULL
) return 0; /* name */
430 if ((s
= strsep(&result
, ":")) == NULL
) return 0; /* password */
433 if ((s
= strsep(&result
, ":")) == NULL
) return 0; /* gid */
434 gr
->gr_gid
= atoi(s
);
436 if ((s
= result
) == NULL
) return 0;
439 for (m
= members
; ; s
++) {
440 if (m
== members
+ maxgrp
- 1) {
441 if ((members
= (char **)reallocf(members
,
442 sizeof(char **) * (maxgrp
+ MAXGRP
))) == NULL
)
444 m
= members
+ maxgrp
- 1;
453 } else if (*s
== '\0' || *s
== '\n' || *s
== ' ') {
459 } else if (cp
== NULL
) {
463 _gr_group
.gr_mem
= members
;
469 static char *_gr_yp_domain
;
472 _getypgroup(struct group
*gr
, const char *name
, const char *map
)
475 static char resultbuf
[YPMAXRECORD
+ 2];
479 if(yp_get_default_domain(&_gr_yp_domain
))
483 if(yp_match(_gr_yp_domain
, map
, name
, strlen(name
),
484 &result
, &resultlen
))
487 s
= strchr(result
, '\n');
490 if (strlcpy(resultbuf
, result
, sizeof(resultbuf
)) >= sizeof(resultbuf
))
493 return(_gr_breakout_yp(gr
, resultbuf
));
499 _nextypgroup(struct group
*gr
)
502 static size_t keylen
;
503 char *lastkey
, *result
;
504 static char resultbuf
[YPMAXRECORD
+ 2];
509 if(yp_get_default_domain(&_gr_yp_domain
))
513 if(!_gr_stepping_yp
) {
515 rv
= yp_first(_gr_yp_domain
, "group.byname",
516 &key
, &keylen
, &result
, &resultlen
);
525 rv
= yp_next(_gr_yp_domain
, "group.byname", key
, keylen
,
526 &key
, &keylen
, &result
, &resultlen
);
534 if(resultlen
> sizeof(resultbuf
)) {
539 strncpy(resultbuf
, result
, resultlen
);
540 resultbuf
[resultlen
] = '\0';
542 if((result
= strchr(resultbuf
, '\n')) != NULL
)
544 if (_gr_breakout_yp(gr
, resultbuf
))