shadow/libmisc
Tomas Mraz ee9e506af2 Make the group and user allocation more effective.
Previously, the allocation was optimized for an outdated
deployment style (that of /etc/group alongside nss_db). The issue
here is that this results in extremely poor performance when using
SSSD, Winbind or nss_ldap.

There were actually two serious bugs here that have been addressed:

1) Running getgrent() loops won't work in most SSSD or Winbind
environments, as full group enumeration is disabled by default.
This could easily result in auto-allocating a group that was
already in use. (This might result in a security issue as well, if
the shared GID is a privileged group).

2) For system groups, the loop was always iterating through the
complete SYS_GID_MIN->SYS_GID_MAX range. On SSSD and Winbind, this
means hundreds of round-trips to LDAP (unless the GIDs were
specifically configured to be ignored by the SSSD or winbindd).
To a user with a slow connection to their LDAP server, this would
appear as if groupadd -r was hung. (Though it would eventually
complete).

This patch changes the algorithm to be more favorable for LDAP
environments, at the expense of some performance when using nss_db.
Given that the DB is a local service, this should have a negligible
effect from a user's perspective.

With the new algorithm, we simply first iterate through all entries
in the local database with gr_next(), recording the IDs that are in
use. We then start from the highest presumed-available entry and
call getgrgid() to see if it is available. We continue this until
we come to the first unused GID. We then select that and return it.

If we make it through all the remaining IDs without finding a free
one, we start over from the beginning of the range and try to find
room in one of the gaps in the range.

The patch was originally written by Stephen Gallagher and applied
identically also to the user allocation by Tomáš Mráz.

Signed-off-by: Serge Hallyn <serge@hallyn.com>
2017-01-29 14:37:21 -06:00
..
.indent.pro Commit the last version from the PLD CVS repository. 2007-10-07 14:36:51 +00:00
addgrps.c * NEWS, libmisc/addgrps.c: Fix allocator loop. Continue to 2011-06-02 15:36:29 +00:00
age.c * libmisc/env.c, libmisc/age.c: Added splint annotations. 2009-04-23 17:33:21 +00:00
audit_help.c * libmisc/audit_help.c (audit_logger):pgname is not used. We let 2010-08-21 15:22:39 +00:00
basename.c Miscellaneous: 2011-09-18 21:02:43 +00:00
chkname.c * libmisc/chkname.c: Do not include <utmp.h> and <utmpx.h>. There 2009-04-28 19:14:05 +00:00
chkname.h Updated copyright dates. 2008-05-25 23:31:10 +00:00
chowndir.c * libmisc/chowndir.c: Add splint annotations. 2011-08-14 14:00:14 +00:00
chowntty.c * libmisc/limits.c: Avoid implicit conversion of integer to 2010-08-22 19:13:53 +00:00
cleanup_group.c * libmisc/cleanup_group.c: Fix compilation when compiled without 2008-12-23 00:39:54 +00:00
cleanup_user.c * libmisc/audit_help.c: Added audit_logger_message() to log 2008-12-22 21:52:43 +00:00
cleanup.c * lib/prototypes.h, libmisc/cleanup.c, lib/spawn.c, src/chage.c: 2011-10-18 20:23:33 +00:00
console.c * NEWS, libmisc/console.c: Fix CONSOLE parser. This caused login 2010-11-19 21:54:41 +00:00
copydir.c 2012-02-13 Mike Frysinger <vapier@gentoo.org> 2012-02-13 19:16:29 +00:00
entry.c Make sure every source files are distributed with a copyright and license. 2008-04-27 00:40:09 +00:00
env.c * libmisc/isexpired.c: Added parenthesis. 2011-06-16 21:25:36 +00:00
failure.c * libmisc/limits.c: Avoid implicit conversion of integer to 2010-08-22 19:13:53 +00:00
failure.h * libmisc/failure.h: Replace HAVE_UTMPX_H by USE_UTMPX. 2009-04-28 19:19:33 +00:00
find_new_gid.c Make the group and user allocation more effective. 2017-01-29 14:37:21 -06:00
find_new_sub_gids.c Tweak uid/gid map default configuration 2016-02-15 18:11:10 -05:00
find_new_sub_uids.c Tweak uid/gid map default configuration 2016-02-15 18:11:10 -05:00
find_new_uid.c Make the group and user allocation more effective. 2017-01-29 14:37:21 -06:00
getdate.h * libmisc/limits.c: Avoid implicit conversion of integer to 2010-08-22 19:13:53 +00:00
getdate.y * libmisc/getdate.y: abbrev is a bool. 2008-05-26 08:59:54 +00:00
getgr_nam_gid.c * lib/prototypes.h, libmisc/getgr_nam_gid.c: getgr_nam_gid() 2011-08-14 13:16:26 +00:00
getrange.c * libmisc/get_gid.c, libmisc/get_uid.c, libmisc/Makefile.am, 2009-03-08 20:26:56 +00:00
hushed.c * libmisc/limits.c: Avoid implicit conversion of integer to 2010-08-22 19:13:53 +00:00
idmapping.c idmapping: add more checks for overflow 2016-08-14 21:48:50 -05:00
idmapping.h Document the semantic of ranges. 2013-08-13 19:28:07 +02:00
isexpired.c * libmisc/isexpired.c: Added parenthesis. 2011-06-16 21:25:36 +00:00
limits.c * man/limits.5.xml, libmisc/limits.c: Sort limit identifiers. 2011-11-06 18:39:47 +00:00
list.c * libmisc/console.c, libmisc/motd.c, libmisc/setupenv.c, 2010-08-21 15:32:53 +00:00
log.c * lib/prototypes.h: Replace HAVE_UTMPX_H by USE_UTMPX. 2009-04-27 20:15:09 +00:00
loginprompt.c * libmisc/salt.c (SHA_salt_rounds): It is statically ensured that 2011-09-18 20:41:38 +00:00
mail.c * libmisc/mail.c, libmisc/copydir.c: Added missing include of 2009-04-27 20:09:18 +00:00
Makefile.am */Makefile.am: Replace INCLUDES with AM_CPPFLAGS 2016-04-29 17:30:18 -07:00
motd.c * libmisc/console.c, libmisc/motd.c, libmisc/setupenv.c, 2010-08-21 15:32:53 +00:00
myname.c * libmisc/myname.c: Updated splint annotations. 2009-04-26 17:10:49 +00:00
obscure.c * libmisc/limits.c: Avoid implicit conversion of integer to 2010-08-22 19:13:53 +00:00
pam_pass_non_interractive.c Updated copyrights. 2010-08-22 13:04:54 +00:00
pam_pass.c * libmisc/pam_pass.c: Removed comment regarding pam_misc. This is 2009-05-09 13:15:17 +00:00
pwd2spwd.c * lib/prototypes.h, libmisc/addgrps.c: restrict add_groups() to 2009-04-05 22:29:42 +00:00
pwd_init.c Make sure every source files are distributed with a copyright and license. 2008-04-27 00:40:09 +00:00
pwdcheck.c * libmisc/pwdcheck.c (passwd_check): The progname is not used. 2009-04-23 20:17:02 +00:00
remove_tree.c Integrate review comments from Julien Cristau 2010-09-05 15:34:42 +00:00
rlogin.c * lib/exitcodes.h: Define E_SUCCESS as EXIT_SUCCESS. Added FIXMEs. 2009-04-30 21:08:49 +00:00
root_flag.c use chdir() before calling chroot() 2013-07-29 11:05:16 +02:00
salt.c (shadow_random): Use long instead of size_t. 2013-08-13 19:16:24 +02:00
setugid.c Updated copyrights. 2010-08-22 13:04:54 +00:00
setupenv.c Fix memory leak. 2013-08-02 20:28:24 +02:00
shell.c * libmisc/limits.c: Avoid implicit conversion of integer to 2010-08-22 19:13:53 +00:00
strtoday.c * libmisc/strtoday.c: Empty dates indicate a disabled date, this is 2010-03-20 10:19:50 +00:00
sub.c Fix typo in comment. 2013-08-04 15:56:32 +02:00
sulog.c Updated copyrights. 2010-08-22 13:04:54 +00:00
ttytype.c * libmisc/limits.c: Avoid implicit conversion of integer to 2010-08-22 19:13:53 +00:00
tz.c Updated copyrights. 2010-08-22 13:04:54 +00:00
ulimit.c * libmisc/limits.c: Add brackets and parenthesis. 2008-06-15 21:59:41 +00:00
user_busy.c Fix user busy errors at userdel 2015-11-17 10:53:55 -06:00
utmp.c Fix for Alioth#314271 (musl libc) 2013-08-07 02:18:43 +02:00
valid.c crypt() in glibc/eglibc 2.17 now fails if passed 2013-07-28 18:41:11 +02:00
xgetgrgid.c * libmisc/xgetXXbyYY.c, libmisc/xgetpwnam.c, libmisc/xgetgrnam.c, 2009-06-11 21:33:00 +00:00
xgetgrnam.c * libmisc/xgetXXbyYY.c, libmisc/xgetpwnam.c, libmisc/xgetgrnam.c, 2009-06-11 21:33:00 +00:00
xgetpwnam.c * libmisc/xgetXXbyYY.c, libmisc/xgetpwnam.c, libmisc/xgetgrnam.c, 2009-06-11 21:33:00 +00:00
xgetpwuid.c * libmisc/xgetXXbyYY.c, libmisc/xgetpwnam.c, libmisc/xgetgrnam.c, 2009-06-11 21:33:00 +00:00
xgetspnam.c * libmisc/xgetXXbyYY.c, libmisc/xgetpwnam.c, libmisc/xgetgrnam.c, 2009-06-11 21:33:00 +00:00
xgetXXbyYY.c Re-indent. 2011-11-06 18:40:06 +00:00
xmalloc.c * libmisc/xmalloc.c: Harmonize message. 2011-06-02 18:41:05 +00:00
yesno.c * libmisc/yesno.c: Ignore the return value of puts. 2009-04-23 11:14:56 +00:00