1 /* $NetBSD: getgrent.c,v 1.11 2005/09/14 15:31:18 he Exp $ */
4 * Copyright (c) 1989, 1991, 1993
5 * The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Portions Copyright (c) 1994, Jason Downs. All Rights Reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
45 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
46 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
48 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * Copied from: lib/libc/gen/getgrent.c
59 * NetBSD: getgrent.c,v 1.46 2003/02/17 00:11:54 simonb Exp
60 * and then gutted, leaving only /etc/group support.
63 #include <sys/cdefs.h>
66 #define endgrent _endgrent
67 #define getgrent _getgrent
68 #define getgrgid _getgrgid
69 #define getgrnam _getgrnam
70 #define getgrnam_r _getgrnam_r
71 #define setgrent _setgrent
72 #define setgroupent _setgroupent
73 #define getgrouplist _getgrouplist
75 __weak_alias(endgrent
,_endgrent
)
76 __weak_alias(getgrent
,_getgrent
)
77 __weak_alias(getgrgid
,_getgrgid
)
78 __weak_alias(getgrnam
,_getgrnam
)
79 __weak_alias(getgrnam_r
,_getgrnam_r
)
80 __weak_alias(setgrent
,_setgrent
)
81 __weak_alias(setgroupent
,_setgroupent
)
82 __weak_alias(getgrouplist
,_getgrouplist
)
85 #include <sys/param.h>
96 static struct group _gr_group
;
97 static int _gr_stayopen
;
98 static int _gr_filesdone
;
100 static int grscan(int, gid_t
, const char *, const char *);
101 static int grstart(void);
102 static int grmatchline(int, gid_t
, const char *, const char *);
105 #define MAXLINELENGTH 1024
107 static __aconst
char *members
[MAXGRP
];
108 static char grline
[MAXLINELENGTH
];
114 if ((!_gr_fp
&& !grstart()) || !grscan(0, 0, NULL
, NULL
))
120 getgrnam_r(const char *name
, struct group
*grp
, char *buffer
,
121 size_t buflen
, struct group
**result
)
123 struct group
*gp
, *bgp
;
126 * We blatantly cheat (don't provide reentrancy)
127 * and hope to get away with it
131 bgp
= (struct group
*)buffer
;
132 if (buflen
< sizeof(struct group
))
141 return (gp
) ? ENOENT
: 0;
145 getgrnam(const char *name
)
151 rval
= grscan(1, 0, name
, NULL
);
154 return (rval
) ? &_gr_group
: NULL
;
164 rval
= grscan(1, gid
, NULL
, NULL
);
167 return (rval
) ? &_gr_group
: NULL
;
179 return (_gr_fp
= fopen(_PATH_GROUP
, "r")) ? 1 : 0;
186 (void) setgroupent(0);
190 setgroupent(int stayopen
)
195 _gr_stayopen
= stayopen
;
205 (void)fclose(_gr_fp
);
211 getgrouplist(const char *uname
, gid_t agroup
,
212 gid_t
*groups
, int *grpcnt
)
215 int maxgroups
, i
, ngroups
, ret
;
222 * install primary group
224 if (ngroups
< maxgroups
)
225 groups
[ngroups
] = agroup
;
231 * Scan the group file to find additional groups.
235 while ((grp
= getgrent()) != NULL
) {
236 if (grp
->gr_gid
== agroup
)
238 for (i
= 0; grp
->gr_mem
[i
]; i
++) {
239 if (strcmp(grp
->gr_mem
[i
], uname
) != 0)
241 for (i
= 0; i
< MIN(ngroups
, maxgroups
); i
++) {
242 if (grp
->gr_gid
== groups
[i
])
245 if (ngroups
< maxgroups
)
246 groups
[ngroups
] = grp
->gr_gid
;
259 grscan(int search
, gid_t gid
, const char *name
, const char *user
)
265 if (!fgets(grline
, sizeof(grline
), _gr_fp
)) {
270 /* skip lines that are too big */
271 if (!strchr(grline
, '\n')) {
274 while ((ch
= getc(_gr_fp
)) != '\n' && ch
!= EOF
)
278 if (grmatchline(search
, gid
, name
, user
))
285 grmatchline(int search
, gid_t gid
, const char *name
, const char *user
)
291 /* name may be NULL if search is nonzero */
294 _gr_group
.gr_name
= strsep(&bp
, ":\n");
295 if (search
&& name
&& strcmp(_gr_group
.gr_name
, name
))
297 _gr_group
.gr_passwd
= strsep(&bp
, ":\n");
298 if (!(cp
= strsep(&bp
, ":\n")))
300 id
= strtoul(cp
, &ep
, 10);
301 if (id
> GID_MAX
|| *ep
!= '\0')
303 _gr_group
.gr_gid
= (gid_t
)id
;
304 if (search
&& name
== NULL
&& _gr_group
.gr_gid
!= gid
)
309 for (_gr_group
.gr_mem
= m
= members
;; bp
++) {
310 if (m
== &members
[MAXGRP
- 1])
318 } else if (*bp
== '\0' || *bp
== '\n' || *bp
== ' ') {
324 } else if (cp
== NULL
)
329 for (m
= members
; *m
; m
++)
330 if (!strcmp(user
, *m
))