2008-04-27 06:10:09 +05:30
|
|
|
/*
|
|
|
|
* Copyright (c) 1991 - 1994, Julianne Frances Haugh
|
|
|
|
* Copyright (c) 2008 , Nicolas François
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2008-02-03 22:23:07 +05:30
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "prototypes.h"
|
|
|
|
#include "groupio.h"
|
|
|
|
#include "getdef.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find_new_gid - Find a new unused GID.
|
|
|
|
*
|
|
|
|
* If successful, find_new_gid provides an unused group ID in the
|
|
|
|
* [GID_MIN:GID_MAX] range.
|
|
|
|
* This ID should be higher than all the used GID, but if not possible,
|
|
|
|
* the lowest unused ID in the range will be returned.
|
|
|
|
*
|
|
|
|
* Return 0 on success, -1 if no unused GIDs are available.
|
|
|
|
*/
|
2008-06-09 23:43:09 +05:30
|
|
|
int find_new_gid (bool sys_group, gid_t *gid, gid_t const *preferred_gid)
|
2008-02-03 22:23:07 +05:30
|
|
|
{
|
|
|
|
const struct group *grp;
|
|
|
|
gid_t gid_min, gid_max, group_id;
|
2008-09-13 17:24:49 +05:30
|
|
|
char *used_gids;
|
2008-02-03 22:23:07 +05:30
|
|
|
|
|
|
|
assert (gid != NULL);
|
|
|
|
|
2008-06-09 23:43:09 +05:30
|
|
|
if (!sys_group) {
|
2008-06-14 03:19:57 +05:30
|
|
|
gid_min = getdef_ulong ("GID_MIN", 1000L);
|
|
|
|
gid_max = getdef_ulong ("GID_MAX", 60000L);
|
2008-02-20 02:31:38 +05:30
|
|
|
} else {
|
2008-06-14 03:19:57 +05:30
|
|
|
gid_min = getdef_ulong ("SYS_GID_MIN", 1L);
|
|
|
|
gid_max = getdef_ulong ("GID_MIN", 1000L) - 1;
|
|
|
|
gid_max = getdef_ulong ("SYS_GID_MAX", (unsigned long) gid_max);
|
2008-02-20 02:31:38 +05:30
|
|
|
}
|
2008-09-13 17:24:49 +05:30
|
|
|
used_gids = alloca (sizeof (char) * gid_max +1);
|
|
|
|
memset (used_gids, 0, sizeof (char) * gid_max + 1);
|
2008-02-03 22:23:07 +05:30
|
|
|
|
|
|
|
if ( (NULL != preferred_gid)
|
|
|
|
&& (*preferred_gid >= gid_min)
|
|
|
|
&& (*preferred_gid <= gid_max)
|
|
|
|
/* Check if the user exists according to NSS */
|
|
|
|
&& (getgrgid (*preferred_gid) == NULL)
|
|
|
|
/* Check also the local database in case of uncommitted
|
|
|
|
* changes */
|
|
|
|
&& (gr_locate_gid (*preferred_gid) == NULL)) {
|
|
|
|
*gid = *preferred_gid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
group_id = gid_min;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search the entire group file,
|
|
|
|
* looking for the largest unused value.
|
|
|
|
*
|
2008-09-13 17:24:49 +05:30
|
|
|
* We check the list of groups according to NSS (setgrent/getgrent),
|
|
|
|
* but we also check the local database (gr_rewind/gr_next) in case
|
2008-02-03 22:23:07 +05:30
|
|
|
* some groups were created but the changes were not committed yet.
|
|
|
|
*/
|
|
|
|
setgrent ();
|
2009-02-23 04:53:15 +05:30
|
|
|
while ((grp = getgrent ()) != NULL) {
|
2008-02-03 22:23:07 +05:30
|
|
|
if ((grp->gr_gid >= group_id) && (grp->gr_gid <= gid_max)) {
|
|
|
|
group_id = grp->gr_gid + 1;
|
|
|
|
}
|
2008-09-13 17:24:49 +05:30
|
|
|
/* create index of used GIDs */
|
|
|
|
if (grp->gr_gid <= gid_max) {
|
|
|
|
used_gids[grp->gr_gid] = 1;
|
|
|
|
}
|
2008-02-03 22:23:07 +05:30
|
|
|
}
|
Ensure that getpwent() is used in setpwent(), getpwent(),
endpwend() sequences (ditto for getgrent(), getspent(), and
getsgent()). The only real (minor) issue was in login, which kept
the passwd file open.
* libmisc/entry.c: Remove unneeded setspent() and endspent() (only
getspnam is called in the middle).
* libmisc/find_new_ids.c: Make sure to close the password and
group files with endpwent() and endgrent().
* libmisc/pwdcheck.c: Remove unneeded endspent() (only getspnam()
is called before).
* src/lastlog.c, src/passwd.c, src/groupmod.c, src/faillog.c,
src/groups.c: Make sure to close
the password file with endpwent().
* src/login.c: Remove unneeded setpwent() (only xgetpwnam is
called before).
* src/login.c, src/newgrp.c: Fix typos in comments.
2008-04-17 03:22:46 +05:30
|
|
|
endgrent ();
|
2009-02-23 04:53:15 +05:30
|
|
|
gr_rewind ();
|
2009-03-16 02:42:57 +05:30
|
|
|
while ((grp = gr_next ()) != NULL) {
|
2009-02-23 04:53:15 +05:30
|
|
|
if ((grp->gr_gid >= group_id) && (grp->gr_gid <= gid_max)) {
|
|
|
|
group_id = grp->gr_gid + 1;
|
|
|
|
}
|
|
|
|
/* create index of used GIDs */
|
|
|
|
if (grp->gr_gid <= gid_max) {
|
|
|
|
used_gids[grp->gr_gid] = 1;
|
|
|
|
}
|
|
|
|
}
|
2008-02-03 22:23:07 +05:30
|
|
|
|
2009-04-11 21:30:45 +05:30
|
|
|
/* find free system account in reverse order */
|
|
|
|
if (sys_group) {
|
|
|
|
for (group_id = gid_max; group_id >= gid_min; group_id--) {
|
|
|
|
if (0 == used_gids[group_id]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( group_id < gid_min ) {
|
|
|
|
fprintf (stderr,
|
|
|
|
_("%s: Can't get unique GID (no more available GIDs)\n"),
|
|
|
|
Prog);
|
|
|
|
SYSLOG ((LOG_WARN,
|
|
|
|
"no more available GID on the system"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-03 22:23:07 +05:30
|
|
|
/*
|
|
|
|
* If a group with GID equal to GID_MAX exists, the above algorithm
|
|
|
|
* will give us GID_MAX+1 even if not unique. Search for the first
|
2008-09-13 17:24:49 +05:30
|
|
|
* free GID starting with GID_MIN.
|
2008-02-03 22:23:07 +05:30
|
|
|
*/
|
|
|
|
if (group_id == gid_max + 1) {
|
|
|
|
for (group_id = gid_min; group_id < gid_max; group_id++) {
|
2008-09-14 19:12:10 +05:30
|
|
|
if (0 == used_gids[group_id]) {
|
2008-02-03 22:23:07 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (group_id == gid_max) {
|
* src/newgrp.c, src/userdel.c, src/grpck.c, src/gpasswd.c,
src/newusers.c, src/pwconv.c, src/chpasswd.c, src/logoutd.c,
src/chfn.c, src/groupmems.c, src/usermod.c, src/pwunconv.c,
src/expiry.c, src/groupdel.c, src/chgpasswd.c, src/useradd.c,
src/su.c, src/groupmod.c, src/passwd.c, src/pwck.c,
src/groupadd.c, src/chage.c, src/login.c, src/grpconv.c,
src/groups.c, src/grpunconv.c, src/chsh.c: Prog is now global (not
static to the file) so that it can be used by the helper functions
of libmisc.
* lib/prototypes.h: Added extern char *Prog.
* libmisc/find_new_gid.c, libmisc/find_new_uid.c: Indicate the
program name with the warning.
2008-09-06 18:21:53 +05:30
|
|
|
fprintf (stderr, _("%s: Can't get unique GID (no more available GIDs)\n"), Prog);
|
|
|
|
SYSLOG ((LOG_WARN, "no more available GID on the system"));
|
2008-02-03 22:23:07 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*gid = group_id;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|