Fjernet gruppebegrensning (allokerer GID-listen dynamisk).

This commit is contained in:
David Kaasen 2017-09-29 13:45:25 +00:00
parent 33c3ba82e2
commit 63d34a6e15
1 changed files with 13 additions and 3 deletions

View File

@ -204,20 +204,28 @@ member(char *gr) {
char **get_group_names(int *numgroups) char **get_group_names(int *numgroups)
{ {
char **grouplist; char **grouplist;
gid_t gids[33]; gid_t *gids = NULL;
int real_nr_groups, nr_groups, i; int real_nr_groups, nr_groups, i;
struct group *g; struct group *g;
nr_groups = 0; nr_groups = 0;
nr_groups = getgroups(32, &gids[0]); /* Allow a max of 32 groups */ /* Find number and allocate */
nr_groups = getgroups(0, NULL);
if (nr_groups == -1) {
dberror(NULL, "Error while trying to find group count");
return NULL;
}
gids = malloc(nr_groups*sizeof(gid_t));
/* Fetch group IDs */
nr_groups = getgroups(nr_groups, gids);
if (nr_groups == -1) { if (nr_groups == -1) {
dberror(NULL, "Error while trying to fetch group info"); dberror(NULL, "Error while trying to fetch group info");
return NULL; return NULL;
} }
grouplist = malloc(33 * sizeof(char *)); grouplist = malloc((nr_groups + 1)*sizeof(char *));
real_nr_groups = 0; real_nr_groups = 0;
for (i = 0; i < nr_groups; i++) { for (i = 0; i < nr_groups; i++) {
@ -232,6 +240,8 @@ char **get_group_names(int *numgroups)
} }
grouplist[real_nr_groups] = NULL; grouplist[real_nr_groups] = NULL;
free(gids);
*numgroups = real_nr_groups; *numgroups = real_nr_groups;
return grouplist; return grouplist;