[Pkg-shadow-devel] [Git][debian/adduser][debian/latest] 5 commits: AdduserStatefile: add global values not tied to a user
Marc Haber (@zugschlus)
gitlab at salsa.debian.org
Thu Nov 26 13:14:42 GMT 2026
Marc Haber pushed to branch debian/latest at Debian / adduser
Commits:
6090785b by Juanes at 2026-10-28T11:59:10+01:00
AdduserStatefile: add global values not tied to a user
- - - - -
1bc91cd2 by Juanes at 2026-10-28T11:59:10+01:00
adduser: do not hand out an id twice (Closes: #248500)
- - - - -
24c18481 by Juanes at 2026-10-28T11:59:10+01:00
document REUSE_IDS and ID_COUNTER_INIT
- - - - -
3d247c5b by Juanes at 2026-10-28T11:59:10+01:00
tests: cover the id counter
- - - - -
ee36dd24 by Juanes at 2026-10-28T11:59:10+01:00
tests: drop firstlastuidgid_orig.t
- - - - -
8 changed files:
- AdduserCommon.pm
- AdduserStatefile.pm
- adduser
- adduser.conf
- − debian/tests/f/firstlastuidgid_orig.t
- + debian/tests/f/id_counter.t
- debian/tests/lib/AdduserTestsCommon.pm
- doc/adduser.conf.5
Changes:
=====================================
AdduserCommon.pm
=====================================
@@ -154,6 +154,9 @@ use constant {
'existing_user_status',
'existing_group_status',
'range_intersection',
+ 'highest_used_id',
+ 'id_floor',
+ 'record_id_used',
'first_avail_uid',
'first_avail_gid',
'first_avail_uid_gid',
@@ -218,6 +221,102 @@ sub range_intersection {
return ($floor, $ceiling);
}
+# highest_used_id: return the highest id in use within a range, looking
+# at both users and groups
+# parameters:
+# min, max: the range
+# return values:
+# the highest id in use, or undef if no id of the range is in use
+sub highest_used_id {
+ my ($min, $max) = @_;
+ my $highest;
+
+ setpwent();
+ while (my @pw = getpwent()) {
+ my $id = $pw[2];
+ next if ($id < $min || $id > $max);
+ $highest = $id if (!defined($highest) || $id > $highest);
+ }
+ endpwent();
+
+ setgrent();
+ while (my @gr = getgrent()) {
+ my $id = $gr[2];
+ next if ($id < $min || $id > $max);
+ $highest = $id if (!defined($highest) || $id > $highest);
+ }
+ endgrent();
+
+ return $highest;
+}
+
+my %id_floor_warned;
+
+# id_floor: raise the lower bound of an id range so that ids which have
+# already been handed out by adduser are not handed out again
+# parameters:
+# min, max: the range from the configuration
+# key: the state file key holding the counter for this range
+# config: reference to the configuration hash
+# return values:
+# the lower bound to actually use
+sub id_floor {
+ my ($min, $max, $key, $config) = @_;
+
+ return $min if (!defined($key));
+ return $min if (($config->{"reuse_ids"} // "no") =~ /yes/i);
+
+ my $last = get_global_value($key);
+
+ if (!defined($last)) {
+ # no counter yet: this is the first time on this system
+ if (($config->{"id_counter_init"} // "first_free") eq "highest") {
+ my $highest = highest_used_id($min, $max);
+ $last = $highest if (defined($highest));
+ }
+ return $min if (!defined($last));
+ }
+
+ my $floor = ($last + 1 > $min) ? $last + 1 : $min;
+ log_trace("id_floor: counter %s is %s, range %s-%s starts at %s",
+ $key, $last, $min, $max, $floor);
+
+ if (!$id_floor_warned{$key}) {
+ # uid and gid floors are raised separately, warn only once
+ $id_floor_warned{$key} = 1;
+ if ($floor > $max) {
+ log_warn(mtx("All ids up to %d have already been assigned and REUSE_IDS is set to no."),
+ $last);
+ } elsif (($max - $floor + 1) <= (($max - $min + 1) / 10)) {
+ log_warn(mtx("Only %d ids are left in the range %d-%d with REUSE_IDS set to no."),
+ $max - $floor + 1, $min, $max);
+ }
+ }
+
+ return $floor;
+}
+
+# record_id_used: remember the highest id handed out by adduser in a range
+# parameters:
+# id: the id that has just been assigned
+# key: the state file key holding the counter for this range
+# config: reference to the configuration hash
+# return values:
+# none
+sub record_id_used {
+ my ($id, $key, $config) = @_;
+
+ return if (!defined($key));
+
+ return if (($config->{"reuse_ids"} // "no") =~ /yes/i);
+
+ my $last = get_global_value($key) // -1;
+ return if ($id <= $last);
+
+ log_trace("record_id_used: setting counter %s to %s", $key, $id);
+ set_global_value($key, $id);
+}
+
# first_avail_uid: return the first available uid in given range
# parameters:
# min, max: the range
@@ -647,6 +746,8 @@ sub read_config {
uid_pool => "",
gid_pool => "",
reserve_uid_pool => "yes",
+ reuse_ids => "no",
+ id_counter_init => "first_free",
reserve_gid_pool => "yes",
loggerparms => "",
stdoutmsglevel => "warn",
=====================================
AdduserStatefile.pm
=====================================
@@ -22,6 +22,8 @@ use vars qw(@EXPORT);
'get_state_value',
'get_state_user_data',
'delete_state_user',
+ 'set_global_value',
+ 'get_global_value',
);
use strict;
@@ -30,6 +32,7 @@ use warnings;
our $STATE_DIR = '/var/lib/adduser';
our $STATE_FILE = "$STATE_DIR/state";
our $LOCK_FILE = "$STATE_DIR/state.lock";
+our $GLOBAL_RECORD = '@global';
# init_state_file()
#
@@ -67,21 +70,23 @@ sub init_state_file {
sub set_state_value {
my ($username, $key, $value) = @_;
+ die "Invalid user name" if ($username eq $GLOBAL_RECORD);
+
my $lock_fh = _acquire_lock();
- my %data = _read_state();
+ my ($data, $globals) = _read_state();
- $data{$username} ||= {};
+ $data->{$username} ||= {};
if (!defined $value || $value eq '') {
- delete $data{$username}{$key};
- if (keys %{$data{$username}} == 0) {
- delete $data{$username};
+ delete $data->{$username}{$key};
+ if (keys %{$data->{$username}} == 0) {
+ delete $data->{$username};
}
} else {
die "Invalid value" if $value =~ /[:\n]/;
- $data{$username}{$key} = $value;
+ $data->{$username}{$key} = $value;
}
- _write_state(\%data);
+ _write_state($data, $globals);
_release_lock($lock_fh);
}
@@ -105,15 +110,14 @@ sub get_state_value {
# Return undef/empty hash if state file doesn't exist yet
return undef unless -f $STATE_FILE;
- my %data = _read_state();
-
+ my ($data, $globals) = _read_state();
# Return undef/empty hash if user doesn't exist
- if (!exists $data{$username}) {
+ if (!exists $data->{$username}) {
return defined $key ? undef : {};
}
# Return specific key or all data
- return defined $key ? $data{$username}{$key} : $data{$username};
+ return defined $key ? $data->{$username}{$key} : $data->{$username};
}
# get_user_data($username)
@@ -149,12 +153,57 @@ sub delete_state_user {
# Acquire lock, read, modify, write, release lock
my $lock_fh = _acquire_lock();
- my %data = _read_state();
- delete $data{$username};
- _write_state(\%data);
+ my ($data, $globals) = _read_state();
+ delete $data->{$username};
+ _write_state($data, $globals);
+ _release_lock($lock_fh);
+}
+
+# set_global_value($key, $value)
+#
+# Set a key-value pair that does not belong to any user account.
+#
+# Parameters:
+# $key - The key name (cannot contain '=', ':', or newlines)
+# $value - The value to store (cannot contain ':' or newlines)
+# If empty or undefined, the key is deleted
+sub set_global_value {
+ my ($key, $value) = @_;
+
+ die "Invalid key" if (!defined($key) || $key =~ /[=:\n]/);
+
+ my $lock_fh = _acquire_lock();
+ my ($data, $globals) = _read_state();
+
+ if (!defined $value || $value eq '') {
+ delete $globals->{$key};
+ } else {
+ die "Invalid value" if $value =~ /[:\n]/;
+ $globals->{$key} = $value;
+ }
+
+ _write_state($data, $globals);
_release_lock($lock_fh);
}
+# get_global_value($key)
+#
+# Retrieve a value that does not belong to any user account.
+#
+# Parameters:
+# $key - The key name to retrieve
+#
+# Returns:
+# The value if found, undef otherwise
+sub get_global_value {
+ my ($key) = @_;
+
+ return undef unless -f $STATE_FILE;
+
+ my ($data, $globals) = _read_state();
+ return $globals->{$key};
+}
+
# _acquire_lock()
#
# Internal function to acquire an exclusive lock on the state file.
@@ -205,15 +254,15 @@ sub _release_lock {
# Empty lines and lines starting with '#' are ignored.
#
# Returns:
-# A hash where keys are usernames and values are hash references
-# containing the key-value pairs for that user
+# Two hash references: the first maps user names to their key-value
+# pairs, the second holds the values that belong to no user
#
# Dies if the file cannot be opened.
sub _read_state {
- my %data;
+ my (%data, %globals);
# Return empty hash if state file doesn't exist yet
- return %data unless -f $STATE_FILE;
+ return (\%data, \%globals) unless -f $STATE_FILE;
open(my $fh, '<', $STATE_FILE) or die "Cannot read $STATE_FILE: $!";
@@ -231,50 +280,62 @@ sub _read_state {
my @fields = split(/:/, $line);
next unless @fields >= 1;
- my $username = shift @fields;
- $data{$username} = {};
-
- # Parse each key=value pair
+ my $record = shift @fields;
+ my %pairs;
foreach my $pair (@fields) {
my ($key, $value) = split(/=/, $pair, 2);
- $data{$username}{$key} = $value if defined $key && defined $value;
+ $pairs{$key} = $value if defined $key && defined $value;
+ }
+
+ if ($record eq $GLOBAL_RECORD) {
+ %globals = (%globals, %pairs);
+ } else {
+ $data{$record} = \%pairs;
}
}
close($fh);
- return %data;
+ return (\%data, \%globals);
}
-# _write_state(\%data)
+# _write_state($data_ref, $globals_ref)
#
# Internal function to write the entire state file atomically.
# MUST be called while holding the lock via _acquire_lock().
# Uses a temporary file and rename() to ensure atomicity.
#
# Parameters:
-# $data_ref - A hash reference in the format returned by _read_state()
+# $data_ref - A hash reference mapping user names to their key-value pairs
+# $globals_ref - A hash reference with the values that belong to no user
#
# The output is sorted by username and by key within each username
# for consistent, diff-friendly output.
#
# Dies if the file cannot be written or renamed.
sub _write_state {
- my ($data_ref) = @_;
+ my ($data_ref, $globals_ref) = @_;
# Use a process-specific temporary file name
my $temp_file = "$STATE_FILE.tmp.$$";
open(my $fh, '>', $temp_file) or die "Cannot write to $temp_file: $!";
+ if (%$globals_ref) {
+ my @pairs;
+ foreach my $key (sort keys %$globals_ref) {
+ push @pairs, "$key=$globals_ref->{$key}";
+ }
+ print $fh "$GLOBAL_RECORD:", join(':', @pairs), "\n";
+ }
+
# Write each user's data, sorted for consistency
foreach my $username (sort keys %$data_ref) {
my @pairs;
# Build key=value pairs, sorted by key
foreach my $key (sort keys %{$data_ref->{$username}}) {
- my $value = $data_ref->{$username}{$key};
- push @pairs, "$key=$value";
+ push @pairs, "$key=$data_ref->{$username}{$key}";
}
# Write the line: username:key1=value1:key2=value2:...
=====================================
adduser
=====================================
@@ -350,6 +350,11 @@ if (@authorized_keys && $action ne "adduser") {
@authorized_keys = ();
}
+# an explicitly requested range overrides everything: the account gets the
+# first free id in that range and the counter is neither read nor updated
+my $range_forced = defined($new_firstuid) || defined($new_lastuid)
+ || defined($new_firstgid) || defined($new_lastgid);
+
# $new_firstuid = $new_firstuid || $config{"first_uid"} || 1000;
# $new_lastuid = $new_lastuid || $config{"last_uid"} || 59999;
# $new_firstgid = $new_firstgid || $config{"first_gid"} || 1000;
@@ -467,6 +472,10 @@ $SIG{'INT'} = $SIG{'QUIT'} = $SIG{'HUP'} = 'handler';
if ($action eq "addsysgroup") {
acquire_lock();
+ my $counter_key;
+
+ # an id given by the admin does not take part in the reuse counter
+ my $gid_forced = defined($gid_option);
log_trace( "addsysuser %s, uid %s", $new_name, $new_uid );
# Check if requested group already exists and we can exit safely
@@ -496,6 +505,8 @@ if ($action eq "addsysgroup") {
if (!defined($gid_option)) {
$first_gid = $new_firstgid || $config{"first_system_gid"};
$last_gid = $new_lastgid || $config{"last_system_gid"};
+ $counter_key = 'last_sys_id' unless ($range_forced);
+ $first_gid = id_floor($first_gid, $last_gid, $counter_key, \%config);
$gid_option = &first_avail_gid($first_gid,
$last_gid,
$gid_pool{$new_name}{'id'},
@@ -515,6 +526,8 @@ if ($action eq "addsysgroup") {
if( $ga_ret == RET_INVALID_NAME_FROM_USERADD ) {
$returnvalue = RET_INVALID_NAME_FROM_USERADD;
}
+ record_id_used($gid_option, $counter_key, \%config)
+ unless ($gid_forced || $returnvalue != RET_OK);
release_lock(0);
exit( $returnvalue );
}
@@ -525,6 +538,9 @@ if ($action eq "addsysgroup") {
##############
if ($action eq "addgroup") {
acquire_lock();
+ my $counter_key;
+
+ my $gid_forced = defined($gid_option);
if (defined egetgrnam(encode($charset, $new_name))) {
log_fatal( mtx("The group `%s' already exists."), $new_name);
exit( RET_OBJECT_EXISTS );
@@ -536,6 +552,8 @@ if ($action eq "addgroup") {
if (!defined($gid_option)) {
$first_gid = $new_firstgid || $config{"first_gid"};
$last_gid = $new_lastgid || $config{"last_gid"};
+ $counter_key = 'last_id' unless ($range_forced);
+ $first_gid = id_floor($first_gid, $last_gid, $counter_key, \%config);
log_debug( "Searching for gid with first_gid=%s, last_gid=%s, new_name=%s, gid_pool=%s",
$first_gid,
$last_gid,
@@ -561,6 +579,8 @@ if ($action eq "addgroup") {
if( $ga_ret == RET_INVALID_NAME_FROM_USERADD ) {
$returnvalue = RET_INVALID_NAME_FROM_USERADD;
}
+ record_id_used($gid_option, $counter_key, \%config)
+ unless ($gid_forced || $returnvalue != RET_OK);
release_lock(0);
exit( $returnvalue );
}
@@ -599,6 +619,9 @@ if ($action eq 'addusertogroup') {
################
if ($action eq "addsysuser") {
acquire_lock();
+ my $counter_key;
+
+ my $uid_forced = defined($new_uid);
log_trace( "addsysuser %s, uid %s", $new_name, $new_uid );
my $ret = existing_user_status(\%config, $new_name, $new_uid);
@@ -637,6 +660,8 @@ if ($action eq "addsysuser") {
my $last_sys_gid = $new_lastgid || $config{"last_system_gid"};
my ($first_uidgid, $last_uidgid) = &range_intersection(
$first_sys_uid, $last_sys_uid, $first_sys_gid, $last_sys_gid);
+ $counter_key = 'last_sys_id' unless ($range_forced);
+ $first_uidgid = id_floor($first_uidgid, $last_uidgid, $counter_key, \%config);
# TODO: Check what happens when those ranges do not overlap
$new_uid = &first_avail_uid_gid( $first_uidgid,
$last_uidgid,
@@ -657,6 +682,8 @@ if ($action eq "addsysuser") {
elsif (!defined($new_uid) && !$make_group_also) {
my $first_sys_uid = $new_firstuid || $config{"first_system_uid"};
my $last_sys_uid = $new_lastuid || $config{"last_system_uid"};
+ $counter_key = 'last_sys_id' unless ($range_forced);
+ $first_sys_uid = id_floor($first_sys_uid, $last_sys_uid, $counter_key, \%config);
# if the primary group is already known by GID (--gid) or by
# name (--ingroup), and that GID happens to also be free as a
# UID, prefer it, so the user ends up with matching UID/GID
@@ -747,6 +774,9 @@ if ($action eq "addsysuser") {
$returnvalue = RET_INVALID_NAME_FROM_USERADD;
}
+ record_id_used($new_uid, $counter_key, \%config)
+ unless ($uid_forced || $returnvalue != RET_OK);
+
release_lock(0);
if (defined($comment_tainted)) {
@@ -779,6 +809,10 @@ if ($action eq "addsysuser") {
#############
if ($action eq "adduser") {
acquire_lock();
+ my $counter_key;
+
+ # an id given by the admin does not take part in the reuse counter
+ my $uid_forced = defined($new_uid);
$primary_gid=-1;
my @supplemental_groups=();
@@ -879,6 +913,7 @@ if ($action eq "adduser") {
$first_gid = $new_firstgid || $config{"first_gid"};
$last_gid = $new_lastgid || $config{"last_gid"};
#}
+
log_trace( "first_uid %s, last_uid %s, first_gid %s, last_gid %s", $first_uid, $last_uid, $first_gid, $last_gid );
log_info (gtx("Adding user `%s' ..."),$new_name);
@@ -889,6 +924,8 @@ if ($action eq "adduser") {
# even though it was not created as a usergroup. Falls back
# to the lowest free UID in range if the GID is not free as
# a UID (or is outside the UID range).
+ $counter_key = 'last_id' unless ($range_forced);
+ $first_uid = id_floor($first_uid, $last_uid, $counter_key, \%config);
my $ingroup_gid = egetgrnam($ingroup_name);
$new_uid = &first_avail_uid( $first_uid,
$last_uid,
@@ -898,6 +935,8 @@ if ($action eq "adduser") {
} else {
my ($first_uidgid, $last_uidgid) = &range_intersection(
$first_uid, $last_uid, $first_gid, $last_gid);
+ $counter_key = 'last_id' unless ($range_forced);
+ $first_uidgid = id_floor($first_uidgid, $last_uidgid, $counter_key, \%config);
# TODO: Check what happens when those ranges do not overlap
$new_uid = &first_avail_uid_gid( $first_uidgid,
$last_uidgid,
@@ -1093,6 +1132,8 @@ if ($action eq "adduser") {
systemcall('/usr/local/sbin/adduser.local', $new_name, $new_uid,
$primary_gid, $home_dir) if (-x "/usr/local/sbin/adduser.local");
+ record_id_used($new_uid, $counter_key, \%config)
+ unless ($uid_forced || $returnvalue != RET_OK);
release_lock(0);
exit( $returnvalue );
=====================================
adduser.conf
=====================================
@@ -47,6 +47,26 @@
#FIRST_GID=1000
#LAST_GID=59999
+# Whether an id that has already been handed out may be handed out again
+# once the account is deleted. With REUSE_IDS=no, adduser keeps a counter
+# in /var/lib/adduser/state and never allocates below it, so a uid or gid
+# is used at most once on this system.
+# An explicit range on the command line (--firstuid, --lastuid, --firstgid,
+# --lastgid) overrides the counter: the first free id of that range is used
+# and the counter is neither read nor updated.
+# Default: REUSE_IDS=no
+#REUSE_IDS=no
+
+# Where the counter starts on a system that does not have one yet.
+# With first_free, the counter only starts once adduser hands out an id,
+# so ids below the highest one already in use remain available.
+# With highest, the counter starts above the highest id currently in use
+# in the range. That also protects the ids of accounts deleted before the
+# counter existed, at the price of never filling the gaps below it.
+# This setting has no effect once the counter exists.
+# Default: ID_COUNTER_INIT=first_free
+#ID_COUNTER_INIT=first_free
+
# Specify a file or a directory containing UID and GID pool.
#UID_POOL=/etc/adduser-pool.conf
#UID_POOL=/etc/adduser-pool.d
=====================================
debian/tests/f/firstlastuidgid_orig.t deleted
=====================================
@@ -1,1020 +0,0 @@
-#! /usr/bin/perl -Idebian/tests/lib
-
-# check first/last uid/gid functionality
-
-use diagnostics;
-use strict;
-use warnings;
-
-use AdduserTestsCommon;
-use Test::More;
-
-my @quiet=('--stdoutmsglevel=error', '--stderrmsglevel=error');
-my $gidcount;
-my $uidcount;
-
-my @unames = ( 'oflugu01', 'oflugu02', 'oflugu03', 'oflugu04' );
-my $unamex = 'oflugu05';
-my @gnames = ( 'oflugg01', 'oflugg02', 'oflugg03', 'oflugg04' );
-my $gnamex = 'oflugg05';
-my $funame = 'ofixedu01';
-my $fgname = 'ofixedg01';
-my $user;
-my $userbase;
-my $fuid;
-my $group;
-my $groupbase;
-my $fgid;
-my $prefix;
-
-my $firstuid1;
-my $lastuid1;
-my $firstgid1;
-my $lastgid1;
-
-sub cleanup {
- my $prefix=$_[0] || '';
- foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- system("/usr/sbin/deluser @quiet --remove-home $user 2>/dev/null");
- assert_user_does_not_exist($user);
- }
- system("/usr/sbin/deluser @quiet --remove-home $prefix$unamex 2>/dev/null");
- assert_user_does_not_exist($prefix.$unamex);
- system("/usr/sbin/deluser @quiet --remove-home $prefix$funame 2>/dev/null");
- assert_user_does_not_exist($prefix.$funame);
- $uidcount=0;
- foreach $groupbase( @gnames ) {
- $group=$prefix.$groupbase;
- system("/usr/sbin/delgroup @quiet $group 2>/dev/null");
- assert_group_does_not_exist($group);
- }
- system("/usr/sbin/delgroup @quiet $prefix$gnamex 2>/dev/null");
- assert_group_does_not_exist($prefix.$gnamex);
- system("/usr/sbin/delgroup @quiet $prefix$fgname 2>/dev/null");
- assert_user_does_not_exist($prefix.$fgname);
- $gidcount=0;
-}
-cleanup();
-
-# test group U1A: default config, no command line
-
-$prefix = 'u1a';
-$fuid = 3750;
-$fgid = 3761;
-my %confhash=();
-apply_config_hash(\%confhash);
-
-foreach $groupbase( @gnames ) {
- $group = $prefix.$groupbase;
- assert_command_success('/usr/sbin/addgroup', @quiet, $group);
- assert_group_exists($group);
- if ($gidcount==0) {
- $gidcount=((getgrnam($group))[2]);
- }
- assert_group_has_gid($group, $gidcount);
- $gidcount++;
-}
-assert_command_success('/usr/sbin/addgroup', @quiet,
- '--gid', $fgid,
- $prefix.$fgname);
-assert_group_exists($prefix.$fgname);
-assert_group_has_gid($prefix.$fgname, $fgid);
-
-foreach $userbase( @unames ) {
- $user = $prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- $user);
- assert_user_exists($user);
- if ($uidcount==0) {
- $uidcount=((getpwnam($user))[2]);
- }
- assert_primary_group_membership_exists($user, $prefix.$gnames[0]);
- assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-assert_command_success('/usr/sbin/adduser', @quiet,
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--uid', $fuid,
- $prefix.$funame);
-assert_user_exists($prefix.$funame);
-assert_user_has_uid($prefix.$funame, $fuid);
-cleanup($prefix);
-
-$prefix='u1a2';
-foreach $userbase( @unames ) {
- $user = $prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--comment', '""', '--disabled-password', '--no-create-home',
- $user);
- assert_user_exists($user);
- assert_group_exists($user);
- if ($uidcount==0) {
- $uidcount=((getpwnam($user))[2]);
- }
- if ($gidcount==0) {
- $gidcount=((getgrnam($user))[2]);
- }
- assert_primary_group_membership_exists($user, $user);
- assert_user_has_uid($user, $uidcount);
- assert_group_has_gid($user, $gidcount);
- $uidcount++;
- $gidcount++;
-}
-cleanup($prefix);
-
-# test group U2A: default config, uid/gid range requested on command line
-
-$prefix='u2a';
-$firstuid1=2000;
-$firstgid1=2100;
-$fuid = 3750;
-$fgid = 3761;
-$gidcount=$firstgid1;
-$uidcount=$firstuid1;
-foreach $groupbase( @gnames ) {
- $group=$prefix.$groupbase;
- assert_command_success('/usr/sbin/addgroup', @quiet,
- '--firstgid', $firstgid1,
- $group);
- assert_group_exists($group);
- assert_group_has_gid($group, $gidcount);
- $gidcount++;
-}
-assert_group_does_not_exist($prefix.$fgname);
-assert_command_success('/usr/sbin/addgroup', @quiet,
- '--firstgid', $firstgid1,
- '--gid', $fgid,
- $prefix.$fgname);
-assert_group_exists($prefix.$fgname);
-assert_group_has_gid($prefix.$fgname, $fgid);
-
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1,
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, $prefix.$gnames[0]);
- # commented. this behavior has changed - the first user created
- # gets the same uid as the group, the following users are one off
- # the expectation. this test is correctly in the new test file,
- # hence commented here.
- # assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-assert_command_success('/usr/sbin/adduser', @quiet,
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1,
- '--uid', $fuid,
- $prefix.$funame);
-assert_user_exists($prefix.$funame);
-assert_user_has_uid($prefix.$funame, $fuid);
-cleanup($prefix);
-
-$prefix='u2a2';
-$firstuid1=2000;
-$fuid = 3750;
-$uidcount=$firstuid1;
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1,
- $user);
- assert_user_exists($user);
- assert_group_exists($user);
- assert_primary_group_membership_exists($user, $user);
- assert_user_has_uid($user, $uidcount);
- assert_group_has_gid($user, $uidcount);
- $uidcount++;
-}
-cleanup($prefix);
-
-# test group U3A: uid range requested by config, no command line
-
-$prefix='u3a';
-$firstuid1=2000;
-$firstgid1=2100;
-$fuid = 3750;
-$fgid = 3761;
-%confhash=();
-$confhash{'FIRST_UID'}="$firstuid1";
-$confhash{'FIRST_GID'}="$firstgid1";
-apply_config_hash(\%confhash);
-
-$gidcount=$firstgid1;
-$uidcount=$firstuid1;
-foreach $groupbase( @gnames ) {
- $group=$prefix.$groupbase;
- assert_command_success('/usr/sbin/addgroup', @quiet, $group);
- assert_group_exists($group);
- assert_group_has_gid($group, $gidcount);
- $gidcount++;
-}
-assert_command_success('/usr/sbin/addgroup', @quiet,
- '--gid', $fgid,
- $prefix.$fgname);
-assert_group_exists($prefix.$fgname);
-assert_group_has_gid($prefix.$fgname, $fgid);
-
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, $prefix.$gnames[0]);
- # commented. this behavior has changed - the first user created
- # gets the same uid as the group, the following users are one off
- # the expectation. this test is correctly in the new test file,
- # hence commented here.
- #assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-assert_command_success('/usr/sbin/adduser', @quiet,
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--uid', $fuid,
- $prefix.$funame);
-assert_user_exists($prefix.$funame);
-assert_user_has_uid($prefix.$funame, $fuid);
-cleanup($prefix);
-
-$prefix='u3a2';
-$firstuid1=2000;
-$fuid = 3750;
-$uidcount=$firstuid1;
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--comment', '""', '--disabled-password', '--no-create-home',
- $user);
- assert_user_exists($user);
- assert_group_exists($user);
- assert_primary_group_membership_exists($user, $user);
- # commented. this behavior has changed. new adduser tries to
- # honor both the uid and the gid range in this case.
- # this test is correctly in the new test file,
- # hence commented here.
- #assert_user_has_uid($user, $uidcount);
- #assert_group_has_gid($user, $uidcount);
- $uidcount++;
-}
-cleanup($prefix);
-
-# test group U4A: ranges requested by config, overriden by command line
-
-$prefix='u4a';
-$firstuid1=2200;
-$firstgid1=2300;
-$fuid = 3750;
-$fgid = 3761;
-$gidcount=$firstgid1;
-$uidcount=$firstuid1;
-foreach $groupbase( @gnames ) {
- $group=$prefix.$groupbase;
- assert_command_success('/usr/sbin/addgroup', @quiet,
- '--firstgid', $firstgid1,
- $group);
- assert_group_exists($group);
- assert_group_has_gid($group, $gidcount);
- $gidcount++;
-}
-assert_command_success('/usr/sbin/addgroup', @quiet,
- '--firstgid', $firstgid1,
- '--gid', $fgid,
- $prefix.$fgname);
-assert_group_exists($prefix.$fgname);
-assert_group_has_gid($prefix.$fgname, $fgid);
-
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1,
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, $prefix.$gnames[0]);
- # commented. this behavior has changed - the first user created
- # gets the same uid as the group, the following users are one off
- # the expectation. this test is correctly in the new test file,
- # hence commented here.
- #assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-assert_command_success('/usr/sbin/adduser', @quiet,
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1,
- '--uid', $fuid,
- $prefix.$funame);
-assert_user_exists($prefix.$funame);
-assert_user_has_uid($prefix.$funame, $fuid);
-cleanup($prefix);
-
-$prefix='u4a2';
-$firstuid1=2000;
-$fuid = 3750;
-$uidcount=$firstuid1;
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1,
- $user);
- assert_user_exists($user);
- assert_group_exists($user);
- assert_primary_group_membership_exists($user, $user);
- # commented. this behavior has changed. new adduser tries to
- # honor both the uid and the gid range in this case.
- # this test is correctly in the new test file,
- # hence commented here.
- #assert_user_has_uid($user, $uidcount);
- #assert_group_has_gid($user, $uidcount);
- $uidcount++;
-}
-cleanup($prefix);
-
-%confhash=();
-apply_config_hash(\%confhash);
-
-# test group S1A: default config, no command line
-
-$prefix = 's1a';
-$fuid = 200;
-$fgid = 210;
-%confhash=();
-apply_config_hash(\%confhash);
-
-foreach $groupbase( @gnames ) {
- $group = $prefix.$groupbase;
- assert_command_success('/usr/sbin/addgroup', @quiet, '--system', $group);
- assert_group_exists($group);
- if ($gidcount==0) {
- $gidcount=((getgrnam($group))[2]);
- }
- assert_group_has_gid($group, $gidcount);
- while (defined(getgrgid($gidcount))) {
- $gidcount++;
- }
-}
-assert_command_success('/usr/sbin/addgroup', @quiet,
- '--system',
- '--gid', $fgid,
- $prefix.$fgname);
-assert_group_exists($prefix.$fgname);
-assert_group_has_gid($prefix.$fgname, $fgid);
-
-foreach $userbase( @unames ) {
- $user = $prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- $user);
- assert_user_exists($user);
- if ($uidcount==0) {
- $uidcount=((getpwnam($user))[2]);
- }
- assert_primary_group_membership_exists($user, $prefix.$gnames[0]);
- # commented. this behavior has changed - the first user created
- # gets the same uid as the group, the following users are one off
- # the expectation. this test is correctly in the new test file,
- # hence commented here.
- #assert_user_has_uid($user, $uidcount);
- while (defined(getpwuid($uidcount))) {
- $uidcount++;
- }
-}
-assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--uid', $fuid,
- $prefix.$funame);
-assert_user_exists($prefix.$funame);
-assert_user_has_uid($prefix.$funame, $fuid);
-cleanup($prefix);
-
-$prefix='s1a2';
-foreach $userbase( @unames ) {
- $user = $prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--comment', '""', '--disabled-password', '--no-create-home',
- $user);
- assert_user_exists($user);
- if ($uidcount==0) {
- $uidcount=((getpwnam($user))[2]);
- }
- assert_primary_group_membership_exists($user, 'nogroup');
- assert_user_has_uid($user, $uidcount);
- while (defined(getpwuid($uidcount))) {
- $uidcount++;
- }
-}
-cleanup($prefix);
-
-# test group S2A: default config, uid/gid range requested on command line
-
-$prefix='s2a';
-$firstuid1=220;
-$firstgid1=230;
-$fuid = 850;
-$fgid = 860;
-$gidcount=$firstgid1;
-$uidcount=$firstuid1;
-foreach $groupbase( @gnames ) {
- $group=$prefix.$groupbase;
- assert_command_success('/usr/sbin/addgroup', @quiet,
- '--system',
- '--firstgid', $firstgid1,
- $group);
- assert_group_exists($group);
- assert_group_has_gid($group, $gidcount);
- $gidcount++;
-}
-assert_group_does_not_exist($prefix.$fgname);
-assert_command_success('/usr/sbin/addgroup', @quiet,
- '--system',
- '--firstgid', $firstgid1,
- '--gid', $fgid,
- $prefix.$fgname);
-assert_group_exists($prefix.$fgname);
-assert_group_has_gid($prefix.$fgname, $fgid);
-
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1,
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, $prefix.$gnames[0]);
- # commented. this behavior has changed - the first user created
- # gets the same uid as the group, the following users are one off
- # the expectation. this test is correctly in the new test file,
- # hence commented here.
- #assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1,
- '--uid', $fuid,
- $prefix.$funame);
-assert_user_exists($prefix.$funame);
-assert_user_has_uid($prefix.$funame, $fuid);
-cleanup($prefix);
-
-$prefix='s2a2';
-$firstuid1=240;
-$fuid = 250;
-$uidcount=$firstuid1;
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1,
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, 'nogroup');
- assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-cleanup($prefix);
-
-# test group S3A: uid range requested by config, no command line
-
-$prefix='s3a';
-$firstuid1=260;
-$firstgid1=270;
-$fuid = 870;
-$fgid = 880;
-%confhash=();
-$confhash{'FIRST_SYSTEM_UID'}="$firstuid1";
-$confhash{'FIRST_SYSTEM_GID'}="$firstgid1";
-apply_config_hash(\%confhash);
-
-$gidcount=$firstgid1;
-$uidcount=$firstuid1;
-foreach $groupbase( @gnames ) {
- $group=$prefix.$groupbase;
- assert_command_success('/usr/sbin/addgroup', '--system', @quiet, $group);
- assert_group_exists($group);
- assert_group_has_gid($group, $gidcount);
- $gidcount++;
-}
-assert_command_success('/usr/sbin/addgroup', @quiet,
- '--system',
- '--gid', $fgid,
- $prefix.$fgname);
-assert_group_exists($prefix.$fgname);
-assert_group_has_gid($prefix.$fgname, $fgid);
-
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, $prefix.$gnames[0]);
- # commented. this behavior has changed - the first user created
- # gets the same uid as the group, the following users are one off
- # the expectation. this test is correctly in the new test file,
- # hence commented here.
- #assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--uid', $fuid,
- $prefix.$funame);
-assert_user_exists($prefix.$funame);
-assert_user_has_uid($prefix.$funame, $fuid);
-cleanup($prefix);
-
-$prefix='s3a2';
-$firstuid1=260;
-$fuid = 870;
-$uidcount=$firstuid1;
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--comment', '""', '--disabled-password', '--no-create-home',
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, 'nogroup');
- assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-cleanup($prefix);
-
-# test group S4A: ranges requested by config, overriden by command line
-
-$prefix='s4a';
-$firstuid1=290;
-$firstgid1=300;
-$fuid = 810;
-$fgid = 820;
-$gidcount=$firstgid1;
-$uidcount=$firstuid1;
-foreach $groupbase( @gnames ) {
- $group=$prefix.$groupbase;
- assert_command_success('/usr/sbin/addgroup', @quiet,
- '--system',
- '--firstgid', $firstgid1,
- $group);
- assert_group_exists($group);
- assert_group_has_gid($group, $gidcount);
- $gidcount++;
-}
-assert_command_success('/usr/sbin/addgroup', @quiet,
- '--system',
- '--firstgid', $firstgid1,
- '--gid', $fgid,
- $prefix.$fgname);
-assert_group_exists($prefix.$fgname);
-assert_group_has_gid($prefix.$fgname, $fgid);
-
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1,
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, $prefix.$gnames[0]);
- # commented. this behavior has changed - the first user created
- # gets the same uid as the group, the following users are one off
- # the expectation. this test is correctly in the new test file,
- # hence commented here.
- #assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1,
- '--uid', $fuid,
- $prefix.$funame);
-assert_user_exists($prefix.$funame);
-assert_user_has_uid($prefix.$funame, $fuid);
-cleanup($prefix);
-
-$prefix='s4a2';
-$firstuid1=310;
-$fuid = 830;
-$uidcount=$firstuid1;
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1,
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, 'nogroup');
- assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-cleanup($prefix);
-
-%confhash=();
-apply_config_hash(\%confhash);
-
-
-# tests with last_ options: we set a range and fill it, last creation must fail
-
-# test group U1L: not applicable
-# test group U2L: default config, uid/gid range requested on command line
-
-$prefix='u2l';
-$firstuid1=2090;
-$lastuid1=2093;
-$firstgid1=2190;
-$lastgid1=2193;
-$fuid = 3750;
-$fgid = 3761;
-$gidcount=$firstgid1;
-$uidcount=$firstuid1;
-foreach $groupbase( @gnames ) {
- $group=$prefix.$groupbase;
- assert_command_success('/usr/sbin/addgroup', @quiet,
- '--firstgid', $firstgid1, '--lastgid', $lastgid1,
- $group);
- assert_group_exists($group);
- assert_group_has_gid($group, $gidcount);
- $gidcount++;
-}
-assert_command_failure_silent('/usr/sbin/addgroup', @quiet,
- '--firstgid', $firstgid1, '--lastgid', $lastgid1,
- $prefix.$gnamex);
-assert_group_does_not_exist($prefix.$gnamex);
-assert_command_success('/usr/sbin/addgroup', @quiet,
- '--firstgid', $firstgid1, '--lastgid', $lastgid1,
- '--gid', $fgid,
- $prefix.$fgname);
-assert_group_exists($prefix.$fgname);
-assert_group_has_gid($prefix.$fgname, $fgid);
-
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1, '--lastuid', $lastuid1,
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, $prefix.$gnames[0]);
- assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-assert_command_failure_silent('/usr/sbin/adduser', @quiet,
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1, '--lastuid', $lastuid1,
- $prefix.$unamex);
-assert_user_does_not_exist($prefix.$unamex);
-assert_command_success('/usr/sbin/adduser', @quiet,
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1, '--lastuid', $lastuid1,
- '--uid', $fuid,
- $prefix.$funame);
-assert_user_exists($prefix.$funame);
-assert_user_has_uid($prefix.$funame, $fuid);
-cleanup($prefix);
-
-# test group U3L: uid range requested by config, no command line
-
-$prefix='u3l';
-$firstuid1=2090;
-$lastuid1=2093;
-$firstgid1=2190;
-$lastgid1=2193;
-$fuid = 3750;
-$fgid = 3761;
-%confhash=();
-$confhash{'FIRST_UID'}="$firstuid1";
-$confhash{'FIRST_GID'}="$firstgid1";
-$confhash{'LAST_UID'}="$lastuid1";
-$confhash{'LAST_GID'}="$lastgid1";
-apply_config_hash(\%confhash);
-$gidcount=$firstgid1;
-$uidcount=$firstuid1;
-foreach $groupbase( @gnames ) {
- $group=$prefix.$groupbase;
- assert_command_success('/usr/sbin/addgroup', @quiet,
- $group);
- assert_group_exists($group);
- assert_group_has_gid($group, $gidcount);
- $gidcount++;
-}
-assert_command_failure_silent('/usr/sbin/addgroup', @quiet,
- $prefix.$gnamex);
-assert_group_does_not_exist($prefix.$gnamex);
-assert_command_success('/usr/sbin/addgroup', @quiet,
- '--gid', $fgid,
- $prefix.$fgname);
-assert_group_exists($prefix.$fgname);
-assert_group_has_gid($prefix.$fgname, $fgid);
-
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, $prefix.$gnames[0]);
- assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-assert_command_failure_silent('/usr/sbin/adduser', @quiet,
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- $prefix.$unamex);
-assert_user_does_not_exist($unamex);
-assert_command_success('/usr/sbin/adduser', @quiet,
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--uid', $fuid,
- $prefix.$funame);
-assert_user_exists($prefix.$funame);
-assert_user_has_uid($prefix.$funame, $fuid);
-cleanup($prefix);
-
-# test group U4L: ranges requested by config, overriden by command line
-
-$prefix='u4l';
-$firstuid1=2290;
-$lastuid1=2293;
-$firstgid1=2390;
-$lastgid1=2393;
-$fuid = 3750;
-$fgid = 3761;
-$gidcount=$firstgid1;
-$uidcount=$firstuid1;
-foreach $groupbase( @gnames ) {
- $group=$prefix.$groupbase;
- assert_command_success('/usr/sbin/addgroup', @quiet,
- '--firstgid', $firstgid1, '--lastgid', $lastgid1,
- $group);
- assert_group_exists($group);
- assert_group_has_gid($group, $gidcount);
- $gidcount++;
-}
-assert_command_failure_silent('/usr/sbin/addgroup', @quiet,
- '--firstgid', $firstgid1, '--lastgid', $lastgid1,
- $prefix.$gnamex);
-assert_group_does_not_exist($prefix.$gnamex);
-assert_command_success('/usr/sbin/addgroup', @quiet,
- '--firstgid', $firstgid1, '--lastgid', $lastgid1,
- '--gid', $fgid,
- $prefix.$fgname);
-assert_group_exists($prefix.$fgname);
-assert_group_has_gid($prefix.$fgname, $fgid);
-
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1, '--lastuid', $lastuid1,
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, $prefix.$gnames[0]);
- assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-assert_command_failure_silent('/usr/sbin/adduser', @quiet,
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1, '--lastuid', $lastuid1,
- $prefix.$unamex);
-assert_user_does_not_exist($prefix.$unamex);
-assert_command_success('/usr/sbin/adduser', @quiet,
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1, '--lastuid', $lastuid1,
- '--uid', $fuid,
- $prefix.$funame);
-assert_user_exists($prefix.$funame);
-assert_user_has_uid($prefix.$funame, $fuid);
-cleanup($prefix);
-
-%confhash=();
-apply_config_hash(\%confhash);
-
-# test group S1L: not applicable
-# test group S2L: default config, uid/gid range requested on command line
-
-$prefix='s2l';
-$firstuid1=400;
-$lastuid1=403;
-$firstgid1=500;
-$lastgid1=503;
-$fuid = 700;
-$fgid = 750;
-$gidcount=$firstgid1;
-$uidcount=$firstuid1;
-foreach $groupbase( @gnames ) {
- $group=$prefix.$groupbase;
- assert_command_success('/usr/sbin/addgroup', @quiet,
- '--system',
- '--firstgid', $firstgid1, '--lastgid', $lastgid1,
- $group);
- assert_group_exists($group);
- assert_group_has_gid($group, $gidcount);
- $gidcount++;
-}
-assert_command_failure_silent('/usr/sbin/addgroup', @quiet,
- '--system',
- '--firstgid', $firstgid1, '--lastgid', $lastgid1,
- $prefix.$gnamex);
-assert_group_does_not_exist($prefix.$gnamex);
-assert_command_success('/usr/sbin/addgroup', @quiet,
- '--system',
- '--firstgid', $firstgid1, '--lastgid', $lastgid1,
- '--gid', $fgid,
- $prefix.$fgname);
-assert_group_exists($prefix.$fgname);
-assert_group_has_gid($prefix.$fgname, $fgid);
-
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1, '--lastuid', $lastuid1,
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, $prefix.$gnames[0]);
- assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-assert_command_failure_silent('/usr/sbin/adduser', @quiet,
- '--system',
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1, '--lastuid', $lastuid1,
- $prefix.$unamex);
-assert_user_does_not_exist($prefix.$unamex);
-assert_command_success_silent('/usr/sbin/adduser', @quiet,
- '--system',
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1, '--lastuid', $lastuid1,
- '--uid', $fuid,
- $prefix.$funame);
-assert_user_exists($prefix.$funame);
-assert_user_has_uid($prefix.$funame, $fuid);
-cleanup($prefix);
-
-# test group S3L: uid range requested by config, no command line
-
-$prefix='s3l';
-$firstuid1=410;
-$lastuid1=413;
-$firstgid1=510;
-$lastgid1=513;
-$fuid = 710;
-$fgid = 760;
-%confhash=();
-$confhash{'FIRST_SYSTEM_UID'}="$firstuid1";
-$confhash{'FIRST_SYSTEM_GID'}="$firstgid1";
-$confhash{'LAST_SYSTEM_UID'}="$lastuid1";
-$confhash{'LAST_SYSTEM_GID'}="$lastgid1";
-apply_config_hash(\%confhash);
-$gidcount=$firstgid1;
-$uidcount=$firstuid1;
-foreach $groupbase( @gnames ) {
- $group=$prefix.$groupbase;
- assert_command_success('/usr/sbin/addgroup', @quiet,
- '--system',
- $group);
- assert_group_exists($group);
- assert_group_has_gid($group, $gidcount);
- $gidcount++;
-}
-assert_command_failure_silent('/usr/sbin/addgroup', @quiet,
- '--system',
- $prefix.$gnamex);
-assert_group_does_not_exist($prefix.$gnamex);
-assert_command_success('/usr/sbin/addgroup', @quiet,
- '--system',
- '--gid', $fgid,
- $prefix.$fgname);
-assert_group_exists($prefix.$fgname);
-assert_group_has_gid($prefix.$fgname, $fgid);
-
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, $prefix.$gnames[0]);
- assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-assert_command_failure_silent('/usr/sbin/adduser', @quiet,
- '--system',
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- $prefix.$unamex);
-assert_user_does_not_exist($unamex);
-assert_command_success_silent('/usr/sbin/adduser', @quiet,
- '--system',
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--uid', $fuid,
- $prefix.$funame);
-assert_user_exists($prefix.$funame);
-assert_user_has_uid($prefix.$funame, $fuid);
-cleanup($prefix);
-
-# test group S4L: ranges requested by config, overriden by command line
-
-$prefix='s4l';
-$firstuid1=320;
-$lastuid1=323;
-$firstgid1=520;
-$lastgid1=523;
-$fuid = 720;
-$fgid = 770;
-$gidcount=$firstgid1;
-$uidcount=$firstuid1;
-foreach $groupbase( @gnames ) {
- $group=$prefix.$groupbase;
- assert_command_success('/usr/sbin/addgroup', @quiet,
- '--system',
- '--firstgid', $firstgid1, '--lastgid', $lastgid1,
- $group);
- assert_group_exists($group);
- assert_group_has_gid($group, $gidcount);
- $gidcount++;
-}
-assert_command_failure_silent('/usr/sbin/addgroup', @quiet,
- '--system',
- '--firstgid', $firstgid1, '--lastgid', $lastgid1,
- $prefix.$gnamex);
-assert_group_does_not_exist($prefix.$gnamex);
-assert_command_success('/usr/sbin/addgroup', @quiet,
- '--system',
- '--firstgid', $firstgid1, '--lastgid', $lastgid1,
- '--gid', $fgid,
- $prefix.$fgname);
-assert_group_exists($prefix.$fgname);
-assert_group_has_gid($prefix.$fgname, $fgid);
-
-foreach $userbase( @unames ) {
- $user=$prefix.$userbase;
- assert_command_success('/usr/sbin/adduser', @quiet,
- '--system',
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1, '--lastuid', $lastuid1,
- $user);
- assert_user_exists($user);
- assert_primary_group_membership_exists($user, $prefix.$gnames[0]);
- assert_user_has_uid($user, $uidcount);
- $uidcount++;
-}
-assert_command_failure_silent('/usr/sbin/adduser', @quiet,
- '--system',
- '--ingroup', $prefix.$gnames[0],
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1, '--lastuid', $lastuid1,
- $prefix.$unamex);
-assert_user_does_not_exist($prefix.$unamex);
-assert_command_success_silent('/usr/sbin/adduser', @quiet,
- '--system',
- '--comment', '""', '--disabled-password', '--no-create-home',
- '--firstuid', $firstuid1, '--lastuid', $lastuid1,
- '--uid', $fuid,
- $prefix.$funame);
-assert_user_exists($prefix.$funame);
-assert_user_has_uid($prefix.$funame, $fuid);
-cleanup($prefix);
-
-%confhash=();
-apply_config_hash(\%confhash);
-cleanup();
-
-done_testing();
-
-# vim: tabstop=4 shiftwidth=4 expandtab
=====================================
debian/tests/f/id_counter.t
=====================================
@@ -0,0 +1,265 @@
+#! /usr/bin/perl -Idebian/tests/lib
+
+# tests for the id counter that keeps adduser from re-using ids
+# (REUSE_IDS and ID_COUNTER_INIT)
+
+use diagnostics;
+use strict;
+use warnings;
+
+use AdduserTestsCommon;
+use Test::More;
+
+use lib '/usr/share/perl5';
+use Debian::AdduserStatefile;
+
+my $adduser = '/usr/sbin/adduser';
+my $deluser = '/usr/sbin/deluser';
+my $addgroup = '/usr/sbin/addgroup';
+my $delgroup = '/usr/sbin/delgroup';
+
+my @users = qw(
+ ausridinit1 ausridbug1 ausridbug2 ausridreuse1 ausridreuse2
+ ausridsys1 ausridsys2 ausridsysafter ausridforced ausridfull
+ ausridhigh ausridhighnew ausridlost1 ausridlost2
+ ausridconc1 ausridconc2 ausridconc3 ausridconc4 ausridconc5
+ ausridconc1 ausridconc2 ausridconc3 ausridconc4 ausridconc5
+ ausridrange1 ausridrange2
+);
+my @groups = qw(ausridgroup1);
+
+END {
+ cleanup_user($_) foreach (@users);
+ foreach my $group (@groups) {
+ system($delgroup, $group) if (defined(getgrnam($group)));
+ }
+ done_testing();
+}
+
+# create a user without any interactive prompt, and return its uid
+sub add_user {
+ my ($name, @args) = @_;
+ assert_command_success($adduser,
+ '--stdoutmsglevel=error', '--stderrmsglevel=error',
+ '--comment', '""', '--disabled-password',
+ @args, $name);
+ assert_user_exists($name);
+ return scalar(getpwnam($name));
+}
+
+sub remove_user {
+ my ($name) = @_;
+ system($deluser, '--remove-home', $name);
+ assert_user_does_not_exist($name);
+}
+
+##############################################################
+# 1. on a system without a state file, the counter is created
+# with the id that has just been handed out
+##############################################################
+apply_config('REUSE_IDS', 'no');
+reset_state_file();
+assert_no_state_file();
+
+my $uid = add_user('ausridinit1');
+assert_state_global('last_id', $uid);
+
+##############################################################
+# 2. an id that has been used once is not handed out again,
+# even after the account has been deleted
+# (the case Joerg Hoh described in 2006)
+##############################################################
+apply_config('REUSE_IDS', 'no');
+reset_state_file();
+
+my $uid_first = add_user('ausridbug1');
+remove_user('ausridbug1');
+
+my $uid_second = add_user('ausridbug2');
+isnt($uid_second, $uid_first,
+ "the uid of a deleted account is not handed out again");
+cmp_ok($uid_second, '>', $uid_first,
+ "the new uid is above the counter");
+assert_state_global('last_id', $uid_second);
+
+##############################################################
+# 3. with REUSE_IDS set to yes, the previous behaviour is kept
+# and no counter is written at all
+##############################################################
+apply_config('REUSE_IDS', 'yes');
+reset_state_file();
+
+my $uid_reuse = add_user('ausridreuse1');
+remove_user('ausridreuse1');
+
+my $uid_again = add_user('ausridreuse2');
+is($uid_again, $uid_reuse,
+ "with REUSE_IDS=yes the freed uid is used again");
+assert_no_state_file();
+
+##############################################################
+# 4. system users have a counter of their own, and using it
+# does not affect the counter of the regular range
+##############################################################
+apply_config('REUSE_IDS', 'no');
+reset_state_file();
+
+my $sys_first = add_user('ausridsys1', '--system');
+assert_state_global('last_sys_id', $sys_first);
+remove_user('ausridsys1');
+
+my $sys_second = add_user('ausridsys2', '--system');
+cmp_ok($sys_second, '>', $sys_first,
+ "the uid of a deleted system account is not handed out again");
+assert_state_global('last_sys_id', $sys_second);
+
+# the regular range is untouched by all of the above
+my $after_sys = add_user('ausridsysafter');
+cmp_ok($after_sys, '>=', 1000,
+ "the system counter does not push regular uids");
+assert_state_global('last_id', $after_sys);
+
+##############################################################
+# 5. an id given by the admin is none of adduser's business:
+# the counter is neither consulted nor updated
+##############################################################
+apply_config('REUSE_IDS', 'no');
+reset_state_file();
+
+my $forced = add_user('ausridforced', '--uid', '4242');
+is($forced, 4242, "the requested uid has been used");
+assert_no_state_file();
+
+##############################################################
+# 6. addgroup consumes an id of the range as well, so it moves
+# the same counter: this is what keeps uids and gids aligned
+##############################################################
+apply_config('REUSE_IDS', 'no');
+reset_state_file();
+
+assert_command_success($addgroup,
+ '--stdoutmsglevel=error', '--stderrmsglevel=error', 'ausridgroup1');
+my $gid = scalar(getgrnam('ausridgroup1'));
+assert_state_global('last_id', $gid);
+
+##############################################################
+# 7. when the counter has passed the end of the range, adduser
+# explains why and creates nothing
+##############################################################
+apply_config('REUSE_IDS', 'no');
+reset_state_file();
+set_global_value('last_id', 59999);
+
+assert_command_result_silent(22, $adduser,
+ '--comment', '""', '--disabled-password', 'ausridfull');
+assert_user_does_not_exist('ausridfull');
+
+##############################################################
+# 8. with ID_COUNTER_INIT set to highest, the counter starts
+# above the highest id in use instead of at the first free one
+##############################################################
+apply_config('REUSE_IDS', 'no', 'ID_COUNTER_INIT', 'highest');
+reset_state_file();
+
+my $high_uid = 5000;
+$high_uid++ while (defined(getpwuid($high_uid)) || defined(getgrgid($high_uid)));
+
+my $high = add_user('ausridhigh', '--uid', $high_uid);
+is($high, $high_uid, "the requested uid has been used");
+assert_no_state_file();
+
+my $after_high = add_user('ausridhighnew');
+is($after_high, $high_uid + 1,
+ "the counter has been initialised above the highest id in use");
+assert_state_global('last_id', $high_uid + 1);
+
+##############################################################
+# 9. the guarantee only holds while the state file survives:
+# losing it degrades to the behaviour of a fresh system
+##############################################################
+apply_config('REUSE_IDS', 'no');
+reset_state_file();
+
+my $before_loss = add_user('ausridlost1');
+assert_state_global('last_id', $before_loss);
+
+reset_state_file();
+assert_no_state_file();
+
+my $after_loss = add_user('ausridlost2');
+assert_state_global('last_id', $after_loss);
+isnt($after_loss, $before_loss,
+ "the id of the still existing account is not handed out again");
+
+##############################################################
+# 10. concurrent invocations must not hand out the same id.
+# Five processes at once, no timing assumptions: what is
+# checked is that all five ids differ and that the counter
+# ends up at the highest of them.
+##############################################################
+apply_config('REUSE_IDS', 'no');
+reset_state_file();
+
+my @concurrent = qw(ausridconc1 ausridconc2 ausridconc3 ausridconc4 ausridconc5);
+my @pids;
+
+foreach my $name (@concurrent) {
+ my $pid = fork();
+ if (!defined($pid)) {
+ fail("fork failed: $!");
+ last;
+ }
+ if ($pid == 0) {
+ # the child replaces itself, so it never runs the test harness
+ open(STDOUT, '>', '/dev/null');
+ open(STDERR, '>', '/dev/null');
+ exec($adduser, '--comment', '""', '--disabled-password', $name);
+ exit(127);
+ }
+ push(@pids, $pid);
+}
+waitpid($_, 0) foreach (@pids);
+
+my %seen;
+my $highest = 0;
+foreach my $name (@concurrent) {
+ my $id = scalar(getpwnam($name));
+ if (!defined($id)) {
+ fail("concurrent user $name has been created");
+ next;
+ }
+ $seen{$id} = $name;
+ $highest = $id if ($id > $highest);
+}
+
+is(scalar(keys %seen), scalar(@concurrent),
+ "concurrent invocations handed out distinct uids");
+assert_state_global('last_id', $highest);
+
+##############################################################
+# 11. an explicitly requested range overrides the counter:
+# the account gets the first free id in that range, the
+# counter is neither read nor updated, and an id freed
+# inside that range is handed out again.
+##############################################################
+apply_config('REUSE_IDS', 'no');
+reset_state_file();
+
+my $ranged = add_user('ausridrange1', '--firstuid', '31000', '--lastuid', '31010');
+is($ranged, 31000, "an explicit range starts at its first free id");
+assert_no_state_file();
+
+cleanup_user('ausridrange1');
+my $ranged_again = add_user('ausridrange1', '--firstuid', '31000', '--lastuid', '31010');
+is($ranged_again, 31000, "an explicit range hands out a freed id again");
+assert_no_state_file();
+
+# the counter was never written, so the next regular user starts it on its
+# own id instead of continuing from the ranged one
+my $regular = add_user('ausridrange2');
+isnt($regular, 31000, "a regular user is not affected by the previous range");
+assert_state_global('last_id', $regular);
+
+# done_testing(); done in END
+
+# vim: tabstop=4 shiftwidth=4 expandtab
\ No newline at end of file
=====================================
debian/tests/lib/AdduserTestsCommon.pm
=====================================
@@ -7,10 +7,14 @@ use strict;
use warnings;
use Encode;
use I18N::Langinfo qw(langinfo CODESET);
-use File::Path qw(remove_tree);
+use File::Path qw(make_path remove_tree);
use Test::More;
our @EXPORT = qw(
+ reset_state_file
+ assert_no_state_file
+ assert_state_global
+ assert_state_user
egetgrnam
egetpwnam
in_range
@@ -104,10 +108,20 @@ BEGIN {
'etc/login.defs',
'etc/passwd',
'etc/shadow',
+ 'var/lib/adduser/state',
);
- mkdir('/var/cache/adduser/tests');
- system('/bin/tar', 'cf', '/var/cache/adduser/tests/state.tar', '--directory=/', @conffiles);
+ make_path('/var/lib/adduser');
+ if (! -e '/var/lib/adduser/state') {
+ open(my $fh, '>', '/var/lib/adduser/state')
+ or die "cannot create /var/lib/adduser/state: $!";
+ close($fh);
+ }
+
+ make_path('/var/cache/adduser/tests');
+ system('/bin/tar', 'cf', '/var/cache/adduser/tests/state.tar',
+ '--directory=/', @conffiles) == 0
+ or die "cannot create the test state snapshot";
}
}
@@ -142,6 +156,66 @@ our @skelfiles = (
"Masaüstü/ŞĞÇaŞ-tst",
);
+our $STATE_FILE = '/var/lib/adduser/state';
+our $STATE_GLOBAL = '@global';
+
+sub _read_state_file {
+ my %state;
+
+ open(my $fh, '<', $STATE_FILE) or return %state;
+ while (my $line = <$fh>) {
+ chomp $line;
+ next if ($line =~ /^\s*$/ || $line =~ /^\s*#/);
+ my ($record, @pairs) = split(/:/, $line);
+ next unless defined $record;
+ foreach my $pair (@pairs) {
+ my ($key, $value) = split(/=/, $pair, 2);
+ $state{$record}{$key} = $value if (defined $key && defined $value);
+ }
+ }
+ close($fh);
+
+ return %state;
+}
+
+sub _assert_state_record {
+ my ($record, $key, $expected, $desc) = @_;
+ my $name = "state file: $desc has $key=$expected";
+
+ if (! -e $STATE_FILE) {
+ fail($name);
+ diag("state file $STATE_FILE does not exist");
+ return;
+ }
+
+ my %state = _read_state_file();
+ if (!exists $state{$record}) {
+ fail($name);
+ diag("no record `$record' in $STATE_FILE");
+ return;
+ }
+
+ is($state{$record}{$key}, $expected, $name);
+}
+
+sub reset_state_file {
+ unlink($STATE_FILE);
+}
+
+sub assert_no_state_file {
+ ok(! -e $STATE_FILE, "state file does not exist");
+}
+
+sub assert_state_global {
+ my ($key, $expected) = @_;
+ _assert_state_record($STATE_GLOBAL, $key, $expected, "global record");
+}
+
+sub assert_state_user {
+ my ($user, $key, $expected) = @_;
+ _assert_state_record($user, $key, $expected, "user $user");
+}
+
sub egetgrnam {
my ($name) = @_;
$name = encode($charset, $name);
=====================================
doc/adduser.conf.5
=====================================
@@ -91,6 +91,20 @@ Defaults to \fI1000\fP - \fI59999\fP.
.B GID_POOL
See \fBUID_POOL\fP.
.TP
+.B ID_COUNTER_INIT
+Where the id counter starts on a system that does not have one yet.
+With \fIfirst_free\fP,
+the counter only comes into existence when \fBadduser\fR(8) hands out an id,
+so ids below the highest one already in use remain available.
+With \fIhighest\fP,
+the counter starts above the highest id currently in use in the range,
+which also protects the ids of accounts that were deleted
+before the counter existed,
+at the price of never filling the gaps below it.
+This option has no effect once the counter exists,
+or if \fBREUSE_IDS\fP is set to \fIyes\fP.
+Defaults to \fIfirst_free\fP.
+.TP
.B LAST_SYSTEM_GID
.TQ
.B LAST_GID
@@ -117,6 +131,22 @@ files are truly reserved.
See UID AND GID POOLS in the NOTES section.
Defaults to \fIyes\fR.
.TP
+.B REUSE_IDS
+Controls whether an id that has already been handed out by \fBadduser\fR(8)
+may be handed out again after the account has been deleted.
+With \fIno\fP,
+a counter is kept in \fI/var/lib/adduser/state\fP
+and no id below it is allocated again,
+so a uid or gid is used at most once on this system.
+An explicit range given on the command line
+(\fB\-\-firstuid\fP, \fB\-\-lastuid\fP, \fB\-\-firstgid\fP, \fB\-\-lastgid\fP)
+overrides the counter:
+the first free id of that range is used
+and the counter is neither read nor updated.
+Ids requested with \fB\-\-uid\fP or \fB\-\-gid\fP never take part in the counter.
+See \fBID_COUNTER_INIT\fP.
+Defaults to \fIno\fR.
+.TP
.B SKEL
The directory from which
skeletal user configuration files will be copied.
@@ -226,6 +256,8 @@ by accounts that are not in the pool.
.SH FILES
.I /etc/adduser.conf
+.br
+.I /var/lib/adduser/state
.SH SEE ALSO
.BR deluser.conf (5),
.BR addgroup (8),
View it on GitLab: https://salsa.debian.org/debian/adduser/-/compare/7f02f31460228373d98b259971a93e44620ca475...ee36dd24d093d0fa82cd898e58cb7d0e2f2a9e53
--
View it on GitLab: https://salsa.debian.org/debian/adduser/-/compare/7f02f31460228373d98b259971a93e44620ca475...ee36dd24d093d0fa82cd898e58cb7d0e2f2a9e53
You're receiving this email because of your account on salsa.debian.org. Manage all notifications: https://salsa.debian.org/-/profile/notifications | Help: https://salsa.debian.org/help
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-shadow-devel/attachments/20261126/f43b3d1b/attachment-0001.htm>
More information about the Pkg-shadow-devel
mailing list