2008-04-27 06:10:09 +05:30
|
|
|
/*
|
|
|
|
* Copyright (c) 1989 - 1994, Julianne Frances Haugh
|
|
|
|
* Copyright (c) 1996 - 1998, Marek Michałkiewicz
|
|
|
|
* Copyright (c) 2001 - 2006, Tomasz Kłoczko
|
2009-03-14 18:43:47 +05:30
|
|
|
* Copyright (c) 2007 - 2009, Nicolas François
|
2008-04-27 06:10:09 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the copyright holders or contributors may not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_SETGROUPS
|
|
|
|
|
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#define SEP ",:"
|
|
|
|
/*
|
|
|
|
* Add groups with names from LIST (separated by commas or colons)
|
|
|
|
* to the supplementary group set. Silently ignore groups which are
|
|
|
|
* already there. Warning: uses strtok().
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
int add_groups (const char *list)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
GETGROUPS_T *grouplist, *tmp;
|
2009-03-16 02:40:35 +05:30
|
|
|
size_t i;
|
2009-03-14 18:43:47 +05:30
|
|
|
int ngroups;
|
2008-05-26 03:33:09 +05:30
|
|
|
bool added;
|
2007-10-07 17:14:02 +05:30
|
|
|
char *token;
|
|
|
|
char buf[1024];
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
if (strlen (list) >= sizeof (buf)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
strcpy (buf, list);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
i = 16;
|
|
|
|
for (;;) {
|
2007-10-07 17:17:22 +05:30
|
|
|
grouplist = (gid_t *) malloc (i * sizeof (GETGROUPS_T));
|
2008-06-13 23:49:34 +05:30
|
|
|
if (NULL == grouplist) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
2008-06-13 23:49:34 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
ngroups = getgroups (i, grouplist);
|
2009-03-16 02:40:35 +05:30
|
|
|
if ((-1 == ngroups) || (i > (size_t)ngroups)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
2008-06-13 23:49:34 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
/* not enough room, so try allocating a larger buffer */
|
2007-10-07 17:15:23 +05:30
|
|
|
free (grouplist);
|
2007-10-07 17:14:02 +05:30
|
|
|
i *= 2;
|
|
|
|
}
|
|
|
|
if (ngroups < 0) {
|
2007-10-07 17:15:23 +05:30
|
|
|
free (grouplist);
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-05-26 03:33:09 +05:30
|
|
|
added = false;
|
|
|
|
for (token = strtok (buf, SEP); NULL != token; token = strtok (NULL, SEP)) {
|
* lib/prototypes.h, configure.in, libmisc/Makefile.am,
libmisc/xgetXXbyYY.c, libmisc/xgetpwnam.c, libmisc/xgetpwuid.c,
libmisc/xgetgrnam.c, libmisc/xgetgrgid.c, libmisc/xgetspnam.c:
Added functions xgetpwnam(), xgetpwuid(), xgetgrnam(),
xgetgrgid(), and xgetspnam(). They allocate memory for the
returned structure and are more robust to successive calls. They
are implemented with the libc's getxxyyy_r() functions if
available.
* libmisc/limits.c, libmisc/entry.c, libmisc/chowntty.c,
libmisc/addgrps.c, libmisc/myname.c, libmisc/rlogin.c,
libmisc/pwdcheck.c, src/newgrp.c, src/login_nopam.c,
src/userdel.c, src/lastlog.c, src/grpck.c, src/gpasswd.c,
src/newusers.c, src/chpasswd.c, src/chfn.c, src/groupmems.c,
src/usermod.c, src/expiry.c, src/groupdel.c, src/chgpasswd.c,
src/su.c, src/useradd.c, src/groupmod.c, src/passwd.c, src/pwck.c,
src/groupadd.c, src/chage.c, src/login.c, src/suauth.c,
src/faillog.c, src/groups.c, src/chsh.c, src/id.c: Review all the
usage of one of the getpwnam(), getpwuid(), getgrnam(),
getgrgid(), and getspnam() functions. It was noticed on
http://bugs.debian.org/341230 that chfn and chsh use a passwd
structure after calling a pam function, which result in using
information from the passwd structure requested by pam, not the
original one. It is much easier to use the new xget... functions
to avoid these issues. I've checked which call to the original
get... functions could be left (reducing the scope of the
structure if possible), and I've left comments to ease future
reviews (e.g. /* local, no need for xgetpwnam */).
Note: the getpwent/getgrent calls should probably be checked also.
* src/groupdel.c, src/expiry.c: Fix typos in comments.
* src/groupmod.c: Re-indent.
* libmisc/Makefile.am, lib/groupmem.c, lib/groupio.c, lib/pwmem.c,
lib/pwio.c, lib/shadowmem.c, lib/shadowio.c: Move the __<xx>_dup
functions (used by the xget... functions) from the <xx>io.c files
to the new <xx>mem.c files. This avoid linking some utils against
the SELinux library.
2007-11-19 04:45:26 +05:30
|
|
|
struct group *grp;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
* lib/prototypes.h, configure.in, libmisc/Makefile.am,
libmisc/xgetXXbyYY.c, libmisc/xgetpwnam.c, libmisc/xgetpwuid.c,
libmisc/xgetgrnam.c, libmisc/xgetgrgid.c, libmisc/xgetspnam.c:
Added functions xgetpwnam(), xgetpwuid(), xgetgrnam(),
xgetgrgid(), and xgetspnam(). They allocate memory for the
returned structure and are more robust to successive calls. They
are implemented with the libc's getxxyyy_r() functions if
available.
* libmisc/limits.c, libmisc/entry.c, libmisc/chowntty.c,
libmisc/addgrps.c, libmisc/myname.c, libmisc/rlogin.c,
libmisc/pwdcheck.c, src/newgrp.c, src/login_nopam.c,
src/userdel.c, src/lastlog.c, src/grpck.c, src/gpasswd.c,
src/newusers.c, src/chpasswd.c, src/chfn.c, src/groupmems.c,
src/usermod.c, src/expiry.c, src/groupdel.c, src/chgpasswd.c,
src/su.c, src/useradd.c, src/groupmod.c, src/passwd.c, src/pwck.c,
src/groupadd.c, src/chage.c, src/login.c, src/suauth.c,
src/faillog.c, src/groups.c, src/chsh.c, src/id.c: Review all the
usage of one of the getpwnam(), getpwuid(), getgrnam(),
getgrgid(), and getspnam() functions. It was noticed on
http://bugs.debian.org/341230 that chfn and chsh use a passwd
structure after calling a pam function, which result in using
information from the passwd structure requested by pam, not the
original one. It is much easier to use the new xget... functions
to avoid these issues. I've checked which call to the original
get... functions could be left (reducing the scope of the
structure if possible), and I've left comments to ease future
reviews (e.g. /* local, no need for xgetpwnam */).
Note: the getpwent/getgrent calls should probably be checked also.
* src/groupdel.c, src/expiry.c: Fix typos in comments.
* src/groupmod.c: Re-indent.
* libmisc/Makefile.am, lib/groupmem.c, lib/groupio.c, lib/pwmem.c,
lib/pwio.c, lib/shadowmem.c, lib/shadowio.c: Move the __<xx>_dup
functions (used by the xget... functions) from the <xx>io.c files
to the new <xx>mem.c files. This avoid linking some utils against
the SELinux library.
2007-11-19 04:45:26 +05:30
|
|
|
grp = getgrnam (token); /* local, no need for xgetgrnam */
|
2008-05-26 03:33:09 +05:30
|
|
|
if (NULL == grp) {
|
2007-10-07 17:15:23 +05:30
|
|
|
fprintf (stderr, _("Warning: unknown group %s\n"),
|
|
|
|
token);
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-03-16 02:40:35 +05:30
|
|
|
for (i = 0; i < (size_t)ngroups && grouplist[i] != grp->gr_gid; i++);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2009-03-16 02:40:35 +05:30
|
|
|
if (i < (size_t)ngroups) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2008-06-13 23:49:34 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
if (ngroups >= sysconf (_SC_NGROUPS_MAX)) {
|
2008-01-25 02:37:14 +05:30
|
|
|
fputs (_("Warning: too many groups\n"), stderr);
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
}
|
2009-03-16 02:40:35 +05:30
|
|
|
tmp = (gid_t *) realloc (grouplist, (size_t)(ngroups + 1) * sizeof (GETGROUPS_T));
|
2008-05-26 03:33:09 +05:30
|
|
|
if (NULL == tmp) {
|
2007-10-07 17:15:23 +05:30
|
|
|
free (grouplist);
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2009-03-16 02:40:35 +05:30
|
|
|
tmp[ngroups] = grp->gr_gid;
|
|
|
|
ngroups++;
|
2007-10-07 17:14:02 +05:30
|
|
|
grouplist = tmp;
|
2008-05-26 03:33:09 +05:30
|
|
|
added = true;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2008-06-13 23:49:34 +05:30
|
|
|
if (added) {
|
2009-03-14 18:43:47 +05:30
|
|
|
return setgroups ((size_t)ngroups, grouplist);
|
2008-06-13 23:49:34 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2008-04-27 06:10:09 +05:30
|
|
|
|