2 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
28 #endif /* STDC_HEADERS */
31 #endif /* HAVE_STRING_H */
34 #endif /* HAVE_STRINGS_H */
41 * BSD-compatible getgrouplist(3) using getgrset(3)
44 getgrouplist(const char *name, gid_t basegid, gid_t *groups, int *ngroupsp)
46 char *cp, *grset = NULL;
48 int grpsize = *ngroupsp;
52 /* We support BSD semantics where the first element is the base gid */
58 aix_setauthdb((char *) name);
60 if ((grset = getgrset(name)) != NULL) {
61 for (cp = strtok(grset, ","); cp != NULL; cp = strtok(NULL, ",")) {
64 if (ngroups == grpsize)
66 groups[ngroups++] = gid;
82 #else /* HAVE_GETGRSET */
85 * BSD-compatible getgrouplist(3) using getgrent(3)
88 getgrouplist(const char *name, gid_t basegid, gid_t *groups, int *ngroupsp)
91 int grpsize = *ngroupsp;
95 /* We support BSD semantics where the first element is the base gid */
101 while ((grp = getgrent()) != NULL) {
102 if (grp->gr_gid == basegid)
105 for (i = 0; grp->gr_mem[i] != NULL; i++) {
106 if (strcmp(name, grp->gr_mem[i]) == 0)
109 if (grp->gr_mem[i] == NULL)
110 continue; /* user not found */
112 /* Only add if it is not the same as an existing gid */
113 for (i = 0; i < ngroups; i++) {
114 if (grp->gr_gid == groups[i])
118 if (ngroups == grpsize)
120 groups[ngroups++] = grp->gr_gid;
131 #endif /* HAVE_GETGRSET */