[Pkg-shadow-devel] [Git][debian/adduser][zg/debian-bug-248500] 3 commits: adapt REUSE_IDS patch to use --ingroup's gid as uid if free
Marc Haber (@zugschlus)
gitlab at salsa.debian.org
Tue Nov 17 10:22:26 GMT 2026
Marc Haber pushed to branch zg/debian-bug-248500 at Debian / adduser
Commits:
fdb8738f by Marc Haber at 2026-11-17T10:48:17+01:00
adapt REUSE_IDS patch to use --ingroup's gid as uid if free
- - - - -
efc33179 by Marc Haber at 2026-11-17T10:49:37+01:00
add ingroup_uid.t to test --ingroup gid => uid mechnanism
- - - - -
e84e7a80 by Marc Haber at 2026-11-17T10:50:05+01:00
adapt firstlastuidgid.t to REUSE_UIDs patch
- - - - -
4 changed files:
- AdduserCommon.pm
- adduser
- debian/tests/f/firstlastuidgid.t
- + debian/tests/f/ingroup_uid.t
Changes:
=====================================
AdduserCommon.pm
=====================================
@@ -330,12 +330,18 @@ sub record_id_used {
# reserved_uid_pool: hashref of ids reserved by other pending
# UID_POOL entries, keyed by id. May be undef or omitted, which
# is treated the same as an empty hashref.
+# floor: optional lower bound for the scan of the range, as raised
+# by id_floor() when REUSE_IDS=no. It does NOT apply to the
+# preferred id: the GID of an existing group is already in use
+# as a GID, so using it as UID too does not hand out an id that
+# the counter has not yet passed. If omitted, min is used.
# return values:
# -1 if no free uid is available
# otherwise the choosen uid
sub first_avail_uid {
- my ($min, $max, $pool_id, $preferred, $reserved_uid_pool) = @_;
+ my ($min, $max, $pool_id, $preferred, $reserved_uid_pool, $floor) = @_;
$reserved_uid_pool //= {};
+ $floor = $min if (!defined($floor) || $floor < $min);
if (defined ($pool_id)) {
return $pool_id if (!defined(getpwuid($pool_id)));
return -1;
@@ -345,9 +351,9 @@ sub first_avail_uid {
log_trace( "first_avail_uid: preferred id %d is free, using it", $preferred );
return $preferred;
}
- log_info( mtx("Selecting UID from range %d to %d ...\n"),$min,$max);
+ log_info( mtx("Selecting UID from range %d to %d ...\n"),$floor,$max);
- my $t = $min;
+ my $t = $floor;
while ($t <= $max) {
return $t if (!exists($reserved_uid_pool->{$t}) and !defined(getpwuid($t)));
$t++;
=====================================
adduser
=====================================
@@ -683,7 +683,7 @@ if ($action eq "addsysuser") {
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);
+ my $floor_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
@@ -700,10 +700,11 @@ if ($action eq "addsysuser") {
$last_sys_uid,
$uid_pool{$new_name}{'id'},
$preferred_uid,
- \%reserved_uid_pool);
+ \%reserved_uid_pool,
+ $floor_sys_uid);
if ($new_uid == -1) {
log_warn( mtx("No UID is available in the range %d-%d (FIRST_SYS_UID - LAST_SYS_UID)."),
- $first_sys_uid,
+ $floor_sys_uid,
$last_sys_uid );
log_fatal( mtx("The user `%s' was not created."), $new_name);
exit( RET_NO_ID_IN_RANGE );
@@ -925,13 +926,14 @@ if ($action eq "adduser") {
# 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 $floor_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,
$uid_pool{$new_name}{'id'},
$ingroup_gid,
- \%reserved_uid_pool);
+ \%reserved_uid_pool,
+ $floor_uid);
} else {
my ($first_uidgid, $last_uidgid) = &range_intersection(
$first_uid, $last_uid, $first_gid, $last_gid);
=====================================
debian/tests/f/firstlastuidgid.t
=====================================
@@ -27,6 +27,10 @@ my $prefix;
sub cleanup {
my $prefix=$_[0] || '';
+ # the id counter (REUSE_IDS=no) must not leak from one test group
+ # into the next one, each of which expects to start from its own
+ # configured first id.
+ reset_state_file();
foreach $userbase( @unames ) {
$user=$prefix.$userbase;
system("/usr/sbin/deluser @quiet --remove-home $user 2>/dev/null");
@@ -233,6 +237,15 @@ sub assert_usergroup_loop {
$uidcount++;
}
} else {
+ if ($user_index == 1 && !$via_cli) {
+ # With the range taken from the config file, the id
+ # counter (REUSE_IDS=no) decides where the scan for the
+ # second user starts. What is checked here is the range,
+ # the counter itself is tested in id_counter.t.
+ $uidcount = (getpwnam($user))[2];
+ cmp_ok($uidcount, '>=', $firstuid,
+ "uid of $user is inside the configured range");
+ }
assert_user_has_uid($user, $uidcount);
$uidcount++;
while (defined(getpwuid($uidcount))) {
=====================================
debian/tests/f/ingroup_uid.t
=====================================
@@ -0,0 +1,56 @@
+#! /usr/bin/perl -Idebian/tests/lib
+
+# adduser --ingroup: if the GID of the primary group is also free as a
+# UID, the user gets that UID, even if the id counter (REUSE_IDS=no)
+# has already advanced beyond it because of groups created later.
+
+use diagnostics;
+use strict;
+use warnings;
+
+use AdduserTestsCommon;
+use Test::More;
+
+my $adduser = '/usr/sbin/adduser';
+my $addgroup = '/usr/sbin/addgroup';
+my $delgroup = '/usr/sbin/delgroup';
+
+my @groups = qw(aingrpuid1 aingrpuid2 aingrpuid3 aingrpuid4);
+my @users = qw(aingrpuser1);
+
+END {
+ cleanup_user($_) foreach (@users);
+ foreach my $group (@groups) {
+ system($delgroup, $group) if (defined(getgrnam($group)));
+ }
+ done_testing();
+}
+
+foreach my $reuse ('no', 'yes') {
+ apply_config('REUSE_IDS', $reuse);
+ reset_state_file();
+
+ foreach my $group (@groups) {
+ assert_command_success($addgroup,
+ '--stdoutmsglevel=error', '--stderrmsglevel=error', $group);
+ }
+
+ my $gid = scalar(getgrnam('aingrpuid2'));
+ ok(defined($gid), "group aingrpuid2 has gid $gid (REUSE_IDS=$reuse)");
+ ok(!defined(getpwuid($gid)), "uid $gid is free");
+
+ assert_command_success($adduser,
+ '--stdoutmsglevel=error', '--stderrmsglevel=error',
+ '--comment', '\"\"', '--disabled-password',
+ '--ingroup', 'aingrpuid2', $users[0]);
+ assert_user_exists($users[0]);
+ is(scalar(getpwnam($users[0])), $gid,
+ "uid equals gid of --ingroup group (REUSE_IDS=$reuse)");
+
+ cleanup_user($users[0]);
+ foreach my $group (@groups) {
+ system($delgroup, $group);
+ }
+}
+
+# vim: tabstop=4 shiftwidth=4 expandtab
View it on GitLab: https://salsa.debian.org/debian/adduser/-/compare/77a746b66c9c772f4e794377d11b6df64f38f92d...e84e7a802c0f166a7a422bd1465f5fa15a5cf6d4
--
View it on GitLab: https://salsa.debian.org/debian/adduser/-/compare/77a746b66c9c772f4e794377d11b6df64f38f92d...e84e7a802c0f166a7a422bd1465f5fa15a5cf6d4
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/20261117/b69d535e/attachment-0001.htm>
More information about the Pkg-shadow-devel
mailing list