summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/id.c65
-rw-r--r--src/setuidgid.c20
2 files changed, 30 insertions, 55 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;
}
diff --git a/src/setuidgid.c b/src/setuidgid.c
index 27c4c7991..ccc8403fc 100644
--- a/src/setuidgid.c
+++ b/src/setuidgid.c
@@ -1,5 +1,5 @@
/* setuidgid - run a command with the UID and GID of a specified user
- Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+ Copyright (C) 2003-2007 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -28,6 +28,7 @@
#include "error.h"
#include "long-options.h"
+#include "mgetgroups.h"
#include "quote.h"
#define PROGRAM_NAME "setuidgid"
@@ -105,8 +106,21 @@ main (int argc, char **argv)
_("unknown user-ID: %s"), quote (user_id));
#if HAVE_SETGROUPS
- if (setgroups (1, &pwd->pw_gid))
- error (SETUIDGID_FAILURE, errno, _("cannot set supplemental group"));
+ {
+ GETGROUPS_T *groups;
+ int n_groups = mgetgroups (user_id, pwd->pw_gid, &groups);
+ if (n_groups < 0)
+ {
+ n_groups = 1;
+ groups = xmalloc (sizeof *groups);
+ *groups = pwd->pw_gid;
+ }
+
+ if (0 < n_groups && setgroups (n_groups, groups))
+ error (SETUIDGID_FAILURE, errno, _("cannot set supplemental group(s)"));
+
+ free (groups);
+ }
#endif
if (setgid (pwd->pw_gid))