Skip to content

Commit

Permalink
lib/, src/: Use agetgroups() instead of its pattern
Browse files Browse the repository at this point in the history
Reviewed-by: Serge Hallyn <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar authored and hallyn committed Jan 24, 2025
1 parent 05322ed commit 1d7dfa0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
16 changes: 3 additions & 13 deletions lib/addgrps.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#include <string.h>
#include <sys/types.h>

#include "alloc/malloc.h"
#include "alloc/reallocf.h"
#include "search/l/lsearch.h"
#include "shadow/grp/agetgroups.h"
#include "shadowlog.h"
#include "string/strchr/strchrscnt.h"

Expand All @@ -38,29 +38,19 @@ add_groups(const char *list)
FILE *shadow_logfd = log_get_logfd();
gid_t *gids;
size_t n;
ssize_t n0;

n0 = getgroups(0, NULL);
if (n0 == -1)
return -1;

gids = MALLOC(n0, gid_t);
gids = agetgroups(&n);
if (gids == NULL)
return -1;

n0 = getgroups(n0, gids);
if (n0 == -1)
goto free_gids;

gids = REALLOCF(gids, n0 + strchrscnt(list, ",:") + 1, gid_t);
gids = REALLOCF(gids, n + strchrscnt(list, ",:") + 1, gid_t);
if (gids == NULL)
return -1;

p = dup = strdup(list);
if (dup == NULL)
goto free_gids;

n = n0;
while (NULL != (g = strsep(&p, ",:"))) {
struct group *grp;

Expand Down
15 changes: 4 additions & 11 deletions src/newgrp.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "prototypes.h"
#include "search/l/lfind.h"
#include "search/l/lsearch.h"
#include "shadow/grp/agetgroups.h"
#include "shadowlog.h"
#include "string/sprintf/snprintf.h"
#include "string/strcmp/streq.h"
Expand Down Expand Up @@ -557,17 +558,9 @@ int main (int argc, char **argv)
* nasty message but at least your real and effective group ids are
* set.
*/

ngroups = getgroups(0, NULL);
if (ngroups == -1)
goto fail_gg;

gids = XMALLOC(ngroups, gid_t);

ngroups = getgroups(ngroups, gids);
if (ngroups == -1) {
fail_gg:
perror("getgroups");
gids = agetgroups(&ngroups);
if (gids == NULL) {
perror("agetgroups");
#ifdef WITH_AUDIT
if (group) {
SNPRINTF(audit_buf, "changing new-group=%s", group);
Expand Down

0 comments on commit 1d7dfa0

Please sign in to comment.