| 1 |
/* getugroups.c -- return a list of the groups a user is in |
| 2 |
|
| 3 |
Copyright (C) 1990-1991, 1998-2000, 2003-2026 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is free software: you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU Lesser General Public License as |
| 7 |
published by the Free Software Foundation; either version 2.1 of the |
| 8 |
License, or (at your option) any later version. |
| 9 |
|
| 10 |
This file is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
/* Written by David MacKenzie. */ |
| 19 |
|
| 20 |
#include <config.h> |
| 21 |
|
| 22 |
#include "getugroups.h" |
| 23 |
|
| 24 |
#include <errno.h> |
| 25 |
#include <limits.h> |
| 26 |
#include <stdio.h> /* grp.h on alpha OSF1 V2.0 uses "FILE *". */ |
| 27 |
#include <string.h> |
| 28 |
#include <unistd.h> |
| 29 |
|
| 30 |
#if !HAVE_GRP_H || defined __ANDROID__ |
| 31 |
|
| 32 |
/* Mingw lacks all things related to group management. The best we |
| 33 |
can do is fail with ENOSYS. |
| 34 |
|
| 35 |
Bionic declares e.g. getgrent() in <grp.h> but it isn't actually |
| 36 |
defined in the library. */ |
| 37 |
|
| 38 |
int |
| 39 |
getugroups (_GL_UNUSED int maxcount, |
| 40 |
_GL_UNUSED gid_t *grouplist, |
| 41 |
_GL_UNUSED char const *username, |
| 42 |
_GL_UNUSED gid_t gid) |
| 43 |
{ |
| 44 |
errno = ENOSYS; |
| 45 |
return -1; |
| 46 |
} |
| 47 |
|
| 48 |
#else /* HAVE_GRP_H */ |
| 49 |
# include <grp.h> |
| 50 |
|
| 51 |
/* Like 'getgroups', but for user USERNAME instead of for the current |
| 52 |
process. Store at most MAXCOUNT group IDs in the GROUPLIST array. |
| 53 |
If GID is not -1, store it first (if possible). GID should be the |
| 54 |
group ID (pw_gid) obtained from getpwuid, in case USERNAME is not |
| 55 |
listed in /etc/groups. Upon failure, set errno and return -1. |
| 56 |
Otherwise, return the number of IDs we've written into GROUPLIST. */ |
| 57 |
|
| 58 |
int |
| 59 |
getugroups (int maxcount, gid_t *grouplist, char const *username, |
| 60 |
gid_t gid) |
| 61 |
{ |
| 62 |
int count = 0; |
| 63 |
|
| 64 |
if (gid != (gid_t) -1) |
| 65 |
{ |
| 66 |
if (maxcount != 0) |
| 67 |
grouplist[count] = gid; |
| 68 |
++count; |
| 69 |
} |
| 70 |
|
| 71 |
setgrent (); |
| 72 |
while (1) |
| 73 |
{ |
| 74 |
errno = 0; |
| 75 |
struct group *grp = getgrent (); |
| 76 |
if (grp == NULL) |
| 77 |
break; |
| 78 |
|
| 79 |
for (char **cp = grp->gr_mem; *cp; ++cp) |
| 80 |
{ |
| 81 |
if (streq (username, *cp)) |
| 82 |
{ |
| 83 |
/* See if this group number is already on the list. */ |
| 84 |
int n; |
| 85 |
for (n = 0; n < count; ++n) |
| 86 |
if (grouplist && grouplist[n] == grp->gr_gid) |
| 87 |
break; |
| 88 |
|
| 89 |
/* If it's a new group number, then try to add it to the list. */ |
| 90 |
if (n == count) |
| 91 |
{ |
| 92 |
if (maxcount != 0) |
| 93 |
{ |
| 94 |
if (count >= maxcount) |
| 95 |
goto done; |
| 96 |
grouplist[count] = grp->gr_gid; |
| 97 |
} |
| 98 |
if (count == INT_MAX) |
| 99 |
{ |
| 100 |
errno = EOVERFLOW; |
| 101 |
goto done; |
| 102 |
} |
| 103 |
count++; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
if (errno != 0) |
| 110 |
count = -1; |
| 111 |
|
| 112 |
done: |
| 113 |
{ |
| 114 |
int saved_errno = errno; |
| 115 |
endgrent (); |
| 116 |
errno = saved_errno; |
| 117 |
} |
| 118 |
|
| 119 |
return count; |
| 120 |
} |
| 121 |
|
| 122 |
#endif /* HAVE_GRP_H */ |