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>
|
|
|
|
|
|
|
|
#include "rcsid.h"
|
2007-10-07 17:16:07 +05:30
|
|
|
RCSID ("$Id: addgrps.c,v 1.7 2005/03/31 05:14:50 kloczek Exp $")
|
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;
|
|
|
|
int i, ngroups, added;
|
|
|
|
struct group *grp;
|
|
|
|
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:15:23 +05:30
|
|
|
grouplist = malloc (i * sizeof (GETGROUPS_T));
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!grouplist)
|
|
|
|
return -1;
|
2007-10-07 17:15:23 +05:30
|
|
|
ngroups = getgroups (i, grouplist);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (i > ngroups)
|
|
|
|
break;
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
added = 0;
|
2007-10-07 17:15:23 +05:30
|
|
|
for (token = strtok (buf, SEP); token; token = strtok (NULL, SEP)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
grp = getgrnam (token);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!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;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
for (i = 0; i < ngroups && grouplist[i] != grp->gr_gid; i++);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
if (i < ngroups)
|
|
|
|
continue;
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
if (ngroups >= sysconf (_SC_NGROUPS_MAX)) {
|
|
|
|
fprintf (stderr, _("Warning: too many groups\n"));
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
}
|
2007-10-07 17:16:07 +05:30
|
|
|
tmp = realloc (grouplist, (ngroups + 1) * sizeof (GETGROUPS_T));
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!tmp) {
|
2007-10-07 17:15:23 +05:30
|
|
|
free (grouplist);
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmp[ngroups++] = grp->gr_gid;
|
|
|
|
grouplist = tmp;
|
|
|
|
added++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (added)
|
2007-10-07 17:15:23 +05:30
|
|
|
return setgroups (ngroups, grouplist);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|