From e0066f36c22dce02f9d6327cb881ee7eec6e7539 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Thu, 5 Jul 2007 17:42:29 +0200 Subject: 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. --- gl/lib/mgetgroups.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ gl/lib/mgetgroups.h | 21 ++++++++++++++ gl/m4/mgetgroups.m4 | 10 +++++++ gl/modules/mgetgroups | 25 ++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 gl/lib/mgetgroups.c create mode 100644 gl/lib/mgetgroups.h create mode 100644 gl/m4/mgetgroups.m4 create mode 100644 gl/modules/mgetgroups (limited to 'gl') diff --git a/gl/lib/mgetgroups.c b/gl/lib/mgetgroups.c new file mode 100644 index 000000000..8552f2639 --- /dev/null +++ b/gl/lib/mgetgroups.c @@ -0,0 +1,80 @@ +/* getugroups.c -- return a list of the groups a user is in + + Copyright (C) 2007 Free Software Foundation. + + 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 + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Extracted from coreutils' src/id.c. */ + +#include + +#include "mgetgroups.h" + +#include +#include +#include + +#include "getugroups.h" +#include "xalloc.h" + +/* Like getugroups, but store the result in malloc'd storage. + Set *GROUPS to the malloc'd list of all group IDs of which USERNAME + is a member. If GID is not -1, store it first. GID should be the + group ID (pw_gid) obtained from getpwuid, in case USERNAME is not + listed in the groups database (e.g., /etc/groups). Upon failure, + don't modify *GROUPS, set errno, and return -1. Otherwise, return + the number of groups. */ + +int +mgetgroups (const char *username, gid_t gid, GETGROUPS_T **groups) +{ + int max_n_groups; + int ng; + GETGROUPS_T *g; + + max_n_groups = (username + ? getugroups (0, NULL, username, gid) + : getgroups (0, NULL)); + + /* If we failed to count groups with NULL for a buffer, + try again with a non-NULL one, just in case. */ + if (max_n_groups < 0) + max_n_groups = 5; + + if (xalloc_oversized (max_n_groups, sizeof *g)) + { + errno = ENOMEM; + return -1; + } + + g = malloc (max_n_groups * sizeof *g); + if (g == NULL) + return -1; + + ng = (username + ? getugroups (max_n_groups, g, username, gid) + : getgroups (max_n_groups, g)); + + if (ng < 0) + { + int saved_errno = errno; + free (g); + errno = saved_errno; + return -1; + } + + *groups = g; + return ng; +} diff --git a/gl/lib/mgetgroups.h b/gl/lib/mgetgroups.h new file mode 100644 index 000000000..23f04e189 --- /dev/null +++ b/gl/lib/mgetgroups.h @@ -0,0 +1,21 @@ +/* Get a list of all group IDs associated with a specified user ID. + Copyright (C) 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 + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. + If not, write to the Free Software Foundation, + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include + +int mgetgroups (const char *username, gid_t gid, GETGROUPS_T **groups); diff --git a/gl/m4/mgetgroups.m4 b/gl/m4/mgetgroups.m4 new file mode 100644 index 000000000..81835415f --- /dev/null +++ b/gl/m4/mgetgroups.m4 @@ -0,0 +1,10 @@ +#serial 1 +dnl Copyright (C) 2007 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_MGETGROUPS], +[ + AC_LIBOBJ([mgetgroups]) +]) diff --git a/gl/modules/mgetgroups b/gl/modules/mgetgroups new file mode 100644 index 000000000..3e2ead100 --- /dev/null +++ b/gl/modules/mgetgroups @@ -0,0 +1,25 @@ +Description: +Return the group IDs of a user in malloc'd storage. + +Files: +lib/mgetgroups.c +lib/mgetgroups.h +m4/mgetgroups.m4 + +Depends-on: +free +getugroups +xalloc + +configure.ac: +gl_MGETGROUPS + +Makefile.am: + +Include: + +License: +LGPL + +Maintainer: +Jim Meyering -- cgit v1.2.3-54-g00ecf