]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/: Use agetgroups() instead of its pattern
authorAlejandro Colomar <alx@kernel.org>
Tue, 19 Nov 2024 00:20:56 +0000 (01:20 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 24 Jan 2025 13:58:13 +0000 (07:58 -0600)
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/addgrps.c
src/newgrp.c

index bfdaf2edcf654279ef0c571c104583613e2a7cbe..83a5b20a86622995a0c95b25ce4d475acd04c2b2 100644 (file)
@@ -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"
 
@@ -38,21 +38,12 @@ 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;
 
@@ -60,7 +51,6 @@ add_groups(const char *list)
        if (dup == NULL)
                goto free_gids;
 
-       n = n0;
        while (NULL != (g = strsep(&p, ",:"))) {
                struct group  *grp;
 
index ee31b56b2c2009f3199f8a34483a6b94d7cf10d7..6e05277f64ed636f86bf95be1552c09aa9e1df7b 100644 (file)
@@ -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"
@@ -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);