summaryrefslogtreecommitdiff
path: root/src/id.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2007-07-05 17:42:29 +0200
committerJim Meyering <jim@meyering.net>2007-07-06 07:44:39 +0200
commite0066f36c22dce02f9d6327cb881ee7eec6e7539 (patch)
tree39c5d9c47df892a1ab845e11fc89b93b8e9b6a1a /src/id.c
parentb8031ff7c74b153f1e929c059e42532a9771eb74 (diff)
downloadcoreutils-e0066f36c22dce02f9d6327cb881ee7eec6e7539.tar.gz
coreutils-e0066f36c22dce02f9d6327cb881ee7eec6e7539.tar.bz2
coreutils-e0066f36c22dce02f9d6327cb881ee7eec6e7539.zip
setuidgid: set all groups, not just the primary one; mgetgroups: new module
I wanted to use the xgetgroups function from id.c, so factored it out and made it into a non-exiting function (hence the "m" prefix rather than "x"). * src/setuidgid.c (main): Use mgetgroups. Include "mgetgroups.h". * src/id.c (xgetgroups): Remove function. Include "mgetgroups.h". (print_group_list): Use mgetgroups, not xgetgroups. * gl/modules/mgetgroups: New module. * gl/lib/mgetgroups.c: New file. mgetgroups is derived from id.c's xgetgroups function. * bootstrap.conf (gnulib_modules): Add mgetgroups. * gl/m4/mgetgroups.m4: New file. * gl/lib/mgetgroups.h: New file.
Diffstat (limited to 'src/id.c')
-rw-r--r--src/id.c65
1 files changed, 13 insertions, 52 deletions
diff --git a/src/id.c b/src/id.c
index 208775eb5..90c944587 100644
--- a/src/id.c
+++ b/src/id.c
@@ -30,6 +30,7 @@
#include "system.h"
#include "error.h"
#include "getugroups.h"
+#include "mgetgroups.h"
#include "quote.h"
/* The official name of this program (e.g., no `g' prefix). */
@@ -278,50 +279,6 @@ print_group (gid_t gid)
printf ("%s", grp->gr_name);
}
-#if HAVE_GETGROUPS
-
-/* FIXME: document */
-
-static bool
-xgetgroups (const char *username, gid_t gid, int *n_groups,
- GETGROUPS_T **groups)
-{
- int max_n_groups;
- int ng;
- GETGROUPS_T *g = NULL;
-
- if (!username)
- max_n_groups = getgroups (0, NULL);
- else
- max_n_groups = getugroups (0, NULL, username, gid);
-
- if (max_n_groups < 0)
- ng = -1;
- else
- {
- g = xnmalloc (max_n_groups, sizeof *g);
- if (!username)
- ng = getgroups (max_n_groups, g);
- else
- ng = getugroups (max_n_groups, g, username, gid);
- }
-
- if (ng < 0)
- {
- error (0, errno, _("cannot get supplemental group list"));
- free (g);
- return false;
- }
- else
- {
- *n_groups = ng;
- *groups = g;
- return true;
- }
-}
-
-#endif /* HAVE_GETGROUPS */
-
/* Print all of the distinct groups the user is in. */
static void
@@ -342,13 +299,15 @@ print_group_list (const char *username)
#if HAVE_GETGROUPS
{
- int n_groups;
GETGROUPS_T *groups;
- int i;
+ size_t i;
- if (! xgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
- &n_groups, &groups))
+ int n_groups = mgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
+ &groups);
+ if (n_groups < 0)
{
+ error (0, errno, _("failed to get groups for user %s"),
+ quote (username));
ok = false;
return;
}
@@ -400,13 +359,15 @@ print_full_info (const char *username)
#if HAVE_GETGROUPS
{
- int n_groups;
GETGROUPS_T *groups;
- int i;
+ size_t i;
- if (! xgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
- &n_groups, &groups))
+ int n_groups = mgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
+ &groups);
+ if (n_groups < 0)
{
+ error (0, errno, _("failed to get groups for user %s"),
+ quote (username));
ok = false;
return;
}