update-users-groups.pl: Keep track of deallocated UIDs/GIDs

When a user or group is revived, this allows it to be allocated the
UID/GID it had before.

A consequence is that UIDs and GIDs are no longer reused.

Fixes #24010.

+53 -17
+53 -17
nixos/modules/config/update-users-groups.pl
··· 6 6 make_path("/var/lib/nixos", { mode => 0755 }); 7 7 8 8 9 + # Keep track of deleted uids and gids. 10 + my $uidMapFile = "/var/lib/nixos/uid-map"; 11 + my $uidMap = -e $uidMapFile ? decode_json(read_file($uidMapFile)) : {}; 12 + 13 + my $gidMapFile = "/var/lib/nixos/gid-map"; 14 + my $gidMap = -e $gidMapFile ? decode_json(read_file($gidMapFile)) : {}; 15 + 16 + 17 + sub updateFile { 18 + my ($path, $contents, $perms) = @_; 19 + write_file("$path.tmp", { binmode => ':utf8', perms => $perms // 0644 }, $contents); 20 + rename("$path.tmp", $path) or die; 21 + } 22 + 23 + 9 24 sub hashPassword { 10 25 my ($password) = @_; 11 26 my $salt = ""; ··· 18 33 # Functions for allocating free GIDs/UIDs. FIXME: respect ID ranges in 19 34 # /etc/login.defs. 20 35 sub allocId { 21 - my ($used, $idMin, $idMax, $up, $getid) = @_; 36 + my ($used, $prevUsed, $idMin, $idMax, $up, $getid) = @_; 22 37 my $id = $up ? $idMin : $idMax; 23 38 while ($id >= $idMin && $id <= $idMax) { 24 - if (!$used->{$id} && !defined &$getid($id)) { 39 + if (!$used->{$id} && !$prevUsed->{$id} && !defined &$getid($id)) { 25 40 $used->{$id} = 1; 26 41 return $id; 27 42 } ··· 31 46 die "$0: out of free UIDs or GIDs\n"; 32 47 } 33 48 34 - my (%gidsUsed, %uidsUsed); 49 + my (%gidsUsed, %uidsUsed, %gidsPrevUsed, %uidsPrevUsed); 35 50 36 51 sub allocGid { 37 - return allocId(\%gidsUsed, 400, 499, 0, sub { my ($gid) = @_; getgrgid($gid) }); 52 + my ($name) = @_; 53 + my $prevGid = $gidMap->{$name}; 54 + if (defined $prevGid && !defined $gidsUsed{$prevGid}) { 55 + print STDERR "reviving group '$name' with GID $prevGid\n"; 56 + $gidsUsed{$prevGid} = 1; 57 + return $prevGid; 58 + } 59 + return allocId(\%gidsUsed, \%gidsPrevUsed, 400, 499, 0, sub { my ($gid) = @_; getgrgid($gid) }); 38 60 } 39 61 40 62 sub allocUid { 41 - my ($isSystemUser) = @_; 63 + my ($name, $isSystemUser) = @_; 42 64 my ($min, $max, $up) = $isSystemUser ? (400, 499, 0) : (1000, 29999, 1); 43 - return allocId(\%uidsUsed, $min, $max, $up, sub { my ($uid) = @_; getpwuid($uid) }); 65 + my $prevUid = $uidMap->{$name}; 66 + if (defined $prevUid && $prevUid >= $min && $prevUid <= $max && !defined $uidsUsed{$prevUid}) { 67 + print STDERR "reviving user '$name' with UID $prevUid\n"; 68 + $uidsUsed{$prevUid} = 1; 69 + return $prevUid; 70 + } 71 + return allocId(\%uidsUsed, \%uidsPrevUsed, $min, $max, $up, sub { my ($uid) = @_; getpwuid($uid) }); 44 72 } 45 73 46 74 47 75 # Read the declared users/groups. 48 76 my $spec = decode_json(read_file($ARGV[0])); 49 77 50 - # Don't allocate UIDs/GIDs that are already in use. 78 + # Don't allocate UIDs/GIDs that are manually assigned. 51 79 foreach my $g (@{$spec->{groups}}) { 52 80 $gidsUsed{$g->{gid}} = 1 if defined $g->{gid}; 53 81 } ··· 55 83 foreach my $u (@{$spec->{users}}) { 56 84 $uidsUsed{$u->{uid}} = 1 if defined $u->{uid}; 57 85 } 86 + 87 + # Likewise for previously used but deleted UIDs/GIDs. 88 + $uidsPrevUsed{$_} = 1 foreach values %{$uidMap}; 89 + $gidsPrevUsed{$_} = 1 foreach values %{$gidMap}; 90 + 58 91 59 92 # Read the current /etc/group. 60 93 sub parseGroup { ··· 114 147 } 115 148 } 116 149 } else { 117 - $g->{gid} = allocGid if !defined $g->{gid}; 150 + $g->{gid} = allocGid($name) if !defined $g->{gid}; 118 151 $g->{password} = "x"; 119 152 } 120 153 121 154 $g->{members} = join ",", sort(keys(%members)); 122 155 $groupsOut{$name} = $g; 156 + 157 + $gidMap->{$name} = $g->{gid}; 123 158 } 124 159 125 160 # Update the persistent list of declarative groups. 126 - write_file($declGroupsFile, { binmode => ':utf8' }, join(" ", sort(keys %groupsOut))); 161 + updateFile($declGroupsFile, join(" ", sort(keys %groupsOut))); 127 162 128 163 # Merge in the existing /etc/group. 129 164 foreach my $name (keys %groupsCur) { ··· 140 175 # Rewrite /etc/group. FIXME: acquire lock. 141 176 my @lines = map { join(":", $_->{name}, $_->{password}, $_->{gid}, $_->{members}) . "\n" } 142 177 (sort { $a->{gid} <=> $b->{gid} } values(%groupsOut)); 143 - write_file("/etc/group.tmp", { binmode => ':utf8' }, @lines); 144 - rename("/etc/group.tmp", "/etc/group") or die; 178 + updateFile($gidMapFile, encode_json($gidMap)); 179 + updateFile("/etc/group", \@lines); 145 180 system("nscd --invalidate group"); 146 181 147 182 # Generate a new /etc/passwd containing the declared users. ··· 167 202 $u->{uid} = $existing->{uid}; 168 203 } 169 204 } else { 170 - $u->{uid} = allocUid($u->{isSystemUser}) if !defined $u->{uid}; 205 + $u->{uid} = allocUid($name, $u->{isSystemUser}) if !defined $u->{uid}; 171 206 172 207 if (defined $u->{initialPassword}) { 173 208 $u->{hashedPassword} = hashPassword($u->{initialPassword}); ··· 195 230 196 231 $u->{fakePassword} = $existing->{fakePassword} // "x"; 197 232 $usersOut{$name} = $u; 233 + 234 + $uidMap->{$name} = $u->{uid}; 198 235 } 199 236 200 237 # Update the persistent list of declarative users. 201 - write_file($declUsersFile, { binmode => ':utf8' }, join(" ", sort(keys %usersOut))); 238 + updateFile($declUsersFile, join(" ", sort(keys %usersOut))); 202 239 203 240 # Merge in the existing /etc/passwd. 204 241 foreach my $name (keys %usersCur) { ··· 214 251 # Rewrite /etc/passwd. FIXME: acquire lock. 215 252 @lines = map { join(":", $_->{name}, $_->{fakePassword}, $_->{uid}, $_->{gid}, $_->{description}, $_->{home}, $_->{shell}) . "\n" } 216 253 (sort { $a->{uid} <=> $b->{uid} } (values %usersOut)); 217 - write_file("/etc/passwd.tmp", { binmode => ':utf8' }, @lines); 218 - rename("/etc/passwd.tmp", "/etc/passwd") or die; 254 + updateFile($uidMapFile, encode_json($uidMap)); 255 + updateFile("/etc/passwd", \@lines); 219 256 system("nscd --invalidate passwd"); 220 257 221 258 ··· 242 279 push @shadowNew, join(":", $u->{name}, $hashedPassword, "1::::::") . "\n"; 243 280 } 244 281 245 - write_file("/etc/shadow.tmp", { binmode => ':utf8', perms => 0600 }, @shadowNew); 246 - rename("/etc/shadow.tmp", "/etc/shadow") or die; 282 + updateFile("/etc/shadow", \@shadowNew, 0600);