2 * Copyright (c) 2010, 2011, 2013 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 */
39 #if defined(HAVE_GETGRSET)
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 #elif defined(HAVE__GETGROUPSBYMEMBER)
85 * BSD-compatible getgrouplist(3) using _getgroupsbymember(3)
88 getgrouplist(const char *name, gid_t basegid, gid_t *groups, int *ngroupsp)
90 int ngroups, grpsize = *ngroupsp;
94 /* We support BSD semantics where the first element is the base gid */
97 /* The last arg is 1 because we already filled in the base gid. */
98 ngroups = _getgroupsbymember(name, groups, grpsize, 1);
107 #else /* !HAVE_GETGRSET && !HAVE__GETGROUPSBYMEMBER */
110 * BSD-compatible getgrouplist(3) using getgrent(3)
113 getgrouplist(const char *name, gid_t basegid, gid_t *groups, int *ngroupsp)
116 int grpsize = *ngroupsp;
120 /* We support BSD semantics where the first element is the base gid */
126 while ((grp = getgrent()) != NULL) {
127 if (grp->gr_gid == basegid)
130 for (i = 0; grp->gr_mem[i] != NULL; i++) {
131 if (strcmp(name, grp->gr_mem[i]) == 0)
134 if (grp->gr_mem[i] == NULL)
135 continue; /* user not found */
137 /* Only add if it is not the same as an existing gid */
138 for (i = 0; i < ngroups; i++) {
139 if (grp->gr_gid == groups[i])
143 if (ngroups == grpsize)
145 groups[ngroups++] = grp->gr_gid;
156 #endif /* !HAVE_GETGRSET && !HAVE__GETGROUPSBYMEMBER */