For some ranges I get a valid answer:
perl -e 'use lib "lib"; use Passwd::Unix; my $p=Passwd::Unix->new;
print "nobody: ".$p->uid('nobody')." "; my$u=$p->unused_uid(1,1200);
if (defined $u) { print "OK ($u)\n"; } else { print "BUG\n" } '
nobody: 65534 OK (11)
While for other ranges not:
perl -e 'use lib "lib"; use Passwd::Unix; my $p=Passwd::Unix->new; print
"nobody: ".$p->uid('nobody')." "; my$u=$p->unused_uid(65534,70000); if
(defined $u) { print "OK ($u)\n"; } else { print "BUG\n" } '
nobody: 65534 BUG
See attached patch for tests of this bug
HTH
Christian
Subject: | passwd-unix-0.621-unused-uid-gid.patch |
diff -ruN Passwd-Unix-0.621/t/17-unused-uid-debian.t Passwd-Unix-0.621-unused-uid-gid-patch/t/17-unused-uid-debian.t
--- Passwd-Unix-0.621/t/17-unused-uid-debian.t 1970-01-01 01:00:00.000000000 +0100
+++ Passwd-Unix-0.621-unused-uid-gid-patch/t/17-unused-uid-debian.t 2012-03-22 23:44:52.084191189 +0100
@@ -0,0 +1,46 @@
+use Test::More tests => 5;
+use File::Copy;
+use File::Path;
+
+# prepare to leave provided files by tar untouched
+mkdir 't/tmp';
+foreach my $f (qw(passwd-debian group-debian shadow-debian shadow-debian)) {
+ copy( "t/$f", "t/tmp/$f" ) or die "Copy $f failed: $!";
+}
+
+# unused_uid( [MINUID] [,MAXUID] )
+#
+# This method returns the first unused UID in a given range. The default MINUID
+# is 0. The default MAXUID is maximal integer value (computed from $Config{
+# intsize } ).
+
+use_ok('Passwd::Unix');
+my $pu = Passwd::Unix->new(
+ passwd => 't/tmp/passwd-debian',
+ shadow => 't/tmp/shadow-debian',
+ group => 't/tmp/group-debian',
+ gshadow => 't/tmp/gshadow-debian',
+);
+
+my $unused_uid1 = $pu->unused_uid( 0, 100 );
+ok( $unused_uid1 == 11, 'unused_uid: next free uid between [0..100] is 11' );
+
+my $unused_uid2 = $pu->unused_uid( 100, 1000 );
+ok( $unused_uid2 == 122,
+ 'unused_uid: next free uid between [100..1000] is 122' );
+
+my $unused_uid3 = $pu->unused_uid( 1000, 65534 );
+ok( $unused_uid3 == 1002,
+ 'unused_uid: next free uid between [1000..65534] is 1002' );
+
+SKIP: {
+ my $unused_uid4 = $pu->unused_uid( 65534, 70000 );
+ skip 'because of a bug', 1 unless defined $unused_uid4;
+
+ ok( $unused_uid4 == 65535,
+ 'unused_uid: next free uid between [65534..70000] is 65535' );
+}
+
+# cleanup
+rmtree 't/tmp';
+
diff -ruN Passwd-Unix-0.621/t/18-unused-gid-debian.t Passwd-Unix-0.621-unused-uid-gid-patch/t/18-unused-gid-debian.t
--- Passwd-Unix-0.621/t/18-unused-gid-debian.t 1970-01-01 01:00:00.000000000 +0100
+++ Passwd-Unix-0.621-unused-uid-gid-patch/t/18-unused-gid-debian.t 2012-03-22 23:45:04.327690826 +0100
@@ -0,0 +1,51 @@
+use Test::More tests => 6;
+use File::Copy;
+use File::Path;
+
+# prepare to leave provided files by tar untouched
+mkdir 't/tmp';
+foreach my $f (qw(passwd-debian group-debian shadow-debian shadow-debian)) {
+ copy( "t/$f", "t/tmp/$f" ) or die "Copy $f failed: $!";
+}
+
+# unused_gid( [MINGID] [,MAXGID] )
+#
+# This method returns the first unused GID in a given range. The default
+# MINGID is 0. The default MAXGID is maximal integer value (computed from
+# $Config{ intsize } ).
+
+use_ok('Passwd::Unix');
+my $pu = Passwd::Unix->new(
+ passwd => 't/tmp/passwd-debian',
+ shadow => 't/tmp/shadow-debian',
+ group => 't/tmp/group-debian',
+ gshadow => 't/tmp/gshadow-debian',
+);
+
+my $unused_gid1 = $pu->unused_gid( 0, 100 );
+ok( $unused_gid1 == 11, 'unused_gid: next free gid between [0..100] is 11' );
+
+my $unused_gid2 = $pu->unused_gid( 100, 1000 );
+ok( $unused_gid2 == 132,
+ 'unused_gid: next free gid between [100..1000] is 132' );
+
+my $unused_gid3 = $pu->unused_gid( 1000, 65534 );
+ok( $unused_gid3 == 1002,
+ 'unused_gid: next free gid between [1000..65534] is 1002' );
+
+SKIP: {
+my $unused_gid4 = $pu->unused_gid( 65534, 65536 );
+ skip 'because of a bug', 1 unless defined $unused_gid4;
+ok( $unused_gid4 == 65535,
+ 'unused_gid: next free gid between [65534,..65536] is 65535' );
+};
+
+SKIP: {
+my $unused_gid5 = $pu->unused_gid( 65534, 70000 );
+ skip 'because of a bug', 1 unless defined $unused_gid5;
+ok( $unused_gid5 == 65535,
+ 'unused_gid: next free gid between [65534..70000] is 65535' );
+};
+# cleanup
+rmtree 't/tmp';
+
diff -ruN Passwd-Unix-0.621/t/group-debian Passwd-Unix-0.621-unused-uid-gid-patch/t/group-debian
--- Passwd-Unix-0.621/t/group-debian 1970-01-01 01:00:00.000000000 +0100
+++ Passwd-Unix-0.621-unused-uid-gid-patch/t/group-debian 2012-03-22 23:46:32.116191707 +0100
@@ -0,0 +1,72 @@
+root:x:0:
+daemon:x:1:
+bin:x:2:
+sys:x:3:
+adm:x:4:frodo
+tty:x:5:
+disk:x:6:
+lp:x:7:
+mail:x:8:
+news:x:9:
+uucp:x:10:
+man:x:12:
+proxy:x:13:
+kmem:x:15:
+dialout:x:20:bilbo,gsmsms,frodo
+fax:x:21:frodo
+voice:x:22:
+cdrom:x:24:bilbo,frodo,freevo
+floppy:x:25:bilbo,frodo
+tape:x:26:frodo
+sudo:x:27:
+audio:x:29:bilbo,pulse,frodo,freevo
+dip:x:30:frodo
+www-data:x:33:
+backup:x:34:
+operator:x:37:
+list:x:38:
+irc:x:39:
+src:x:40:
+gnats:x:41:
+shadow:x:42:
+utmp:x:43:
+video:x:44:bilbo,frodo
+sasl:x:45:
+plugdev:x:46:bilbo,frodo,freevo
+staff:x:50:
+games:x:60:
+users:x:100:
+nogroup:x:65534:
+libuuid:x:101:
+crontab:x:102:
+scanner:x:103:saned,frodo
+ssl-cert:x:104:
+Debian-exim:x:105:
+mlocate:x:106:
+ssh:x:107:
+messagebus:x:108:
+avahi-autoipd:x:109:
+avahi:x:110:
+netdev:x:111:bilbo
+lpadmin:x:112:
+gdm:x:113:
+stb-admin:x:114:
+haldaemon:x:115:
+powerdev:x:116:bilbo
+bilbo:x:1000:
+nvram:x:117:
+frodo:x:1001:
+fuse:x:119:bilbo,frodo
+gsmsms:x:120:
+vde2-net:x:121:
+bluetooth:x:122:
+kvm:x:123:
+libvirt:x:124:bilbo
+polkituser:x:118:
+saned:x:125:
+utempter:x:126:
+cl-builder:x:127:
+pulse:x:128:
+pulse-access:x:129:
+science:x:130:
+freevo:x:131:
diff -ruN Passwd-Unix-0.621/t/gshadow-debian Passwd-Unix-0.621-unused-uid-gid-patch/t/gshadow-debian
--- Passwd-Unix-0.621/t/gshadow-debian 1970-01-01 01:00:00.000000000 +0100
+++ Passwd-Unix-0.621-unused-uid-gid-patch/t/gshadow-debian 2012-03-22 23:46:32.116191707 +0100
@@ -0,0 +1,72 @@
+root:*::
+daemon:*::
+bin:*::
+sys:*::
+adm:*::frodo
+tty:*::
+disk:*::
+lp:*::
+mail:*::
+news:*::
+uucp:*::
+man:*::
+proxy:*::
+kmem:*::
+dialout:*::bilbo,gsmsms,frodo
+fax:*::frodo
+voice:*::
+cdrom:*::bilbo,frodo,freevo
+floppy:*::bilbo,frodo
+tape:*::frodo
+sudo:*::
+audio:*::bilbo,pulse,frodo,freevo
+dip:*::frodo
+www-data:*::
+backup:*::
+operator:*::
+list:*::
+irc:*::
+src:*::
+gnats:*::
+shadow:*::
+utmp:*::
+video:*::bilbo,frodo
+sasl:*::
+plugdev:*::bilbo,frodo,freevo
+staff:*::
+games:*::
+users:*::
+nogroup:*::
+libuuid:!::
+crontab:!::
+scanner:!::saned,frodo
+ssl-cert:!::
+Debian-exim:!::
+mlocate:!::
+ssh:!::
+messagebus:!::
+avahi-autoipd:!::
+avahi:!::
+netdev:!::bilbo
+lpadmin:!::
+gdm:!::
+stb-admin:!::
+haldaemon:!::
+powerdev:!::bilbo
+bilbo:!::
+nvram:!::
+frodo:!::
+fuse:!::frodo
+gsmsms:!::
+vde2-net:!::
+bluetooth:!::
+kvm:!::
+libvirt:!::bilbo
+polkituser:!::
+saned:!::
+utempter:!::
+cl-builder:!::
+pulse:!::
+pulse-access:!::
+science:!::
+freevo:!::
diff -ruN Passwd-Unix-0.621/t/passwd-debian Passwd-Unix-0.621-unused-uid-gid-patch/t/passwd-debian
--- Passwd-Unix-0.621/t/passwd-debian 1970-01-01 01:00:00.000000000 +0100
+++ Passwd-Unix-0.621-unused-uid-gid-patch/t/passwd-debian 2012-03-22 23:46:32.116191707 +0100
@@ -0,0 +1,42 @@
+root:x:0:0:root:/root:/bin/zsh
+daemon:x:1:1:daemon:/usr/sbin:/bin/sh
+bin:x:2:2:bin:/bin:/bin/sh
+sys:x:3:3:sys:/dev:/bin/sh
+sync:x:4:65534:sync:/bin:/bin/sync
+games:x:5:60:games:/usr/games:/bin/sh
+man:x:6:12:man:/var/cache/man:/bin/sh
+lp:x:7:7:lp:/var/spool/lpd:/bin/sh
+mail:x:8:8:mail:/var/mail:/bin/sh
+news:x:9:9:news:/var/spool/news:/bin/sh
+uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
+proxy:x:13:13:proxy:/bin:/bin/sh
+www-data:x:33:33:www-data:/var/www:/bin/sh
+backup:x:34:34:backup:/var/backups:/bin/sh
+list:x:38:38:Mailing List Manager:/var/list:/bin/sh
+irc:x:39:39:ircd:/var/run/ircd:/bin/sh
+gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
+nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
+libuuid:x:100:101::/var/lib/libuuid:/bin/sh
+Debian-exim:x:101:105::/var/spool/exim4:/bin/false
+statd:x:102:65534::/var/lib/nfs:/bin/false
+messagebus:x:103:108::/var/run/dbus:/bin/false
+avahi-autoipd:x:104:109:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false
+avahi:x:105:110:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
+gdm:x:106:113:Gnome Display Manager:/var/lib/gdm:/bin/false
+haldaemon:x:107:115:Hardware abstraction layer,,,:/var/run/hald:/bin/false
+hplip:x:108:7:HPLIP system user,,,:/var/run/hplip:/bin/false
+bilbo:x:1000:1000:Bilbo Baggins,,,:/home/bilbo:/bin/zsh
+festival:x:109:29::/home/festival:/bin/false
+sshd:x:110:65534::/var/run/sshd:/usr/sbin/nologin
+frodo:x:1001:1001::/home/frodo:/bin/rbash
+gsmsms:x:111:120::/var/spool/sms:/bin/false
+speech-dispatcher:x:112:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh
+vde2-net:x:113:121::/var/run/vde2:/bin/false
+dnsmasq:x:114:65534:dnsmasq,,,:/var/lib/misc:/bin/false
+polkituser:x:115:118:PolicyKit,,,:/var/run/PolicyKit:/bin/false
+saned:x:116:125::/home/saned:/bin/false
+usbmux:x:117:46:usbmux daemon,,,:/home/usbmux:/bin/false
+cl-builder:x:118:127::/usr/share/common-lisp/:/bin/false
+pulse:x:119:128:PulseAudio daemon,,,:/var/run/pulse:/bin/false
+libvirt-qemu:x:120:123:Libvirt Qemu,,,:/var/lib/libvirt:/bin/false
+freevo:x:121:131::/var/lib/freevo:/bin/false
diff -ruN Passwd-Unix-0.621/t/shadow-debian Passwd-Unix-0.621-unused-uid-gid-patch/t/shadow-debian
--- Passwd-Unix-0.621/t/shadow-debian 1970-01-01 01:00:00.000000000 +0100
+++ Passwd-Unix-0.621-unused-uid-gid-patch/t/shadow-debian 2012-03-22 23:46:32.116191707 +0100
@@ -0,0 +1,42 @@
+root:*:14491:0:99999:7:::
+daemon:*:14491:0:99999:7:::
+bin:*:14491:0:99999:7:::
+sys:*:14491:0:99999:7:::
+sync:*:14491:0:99999:7:::
+games:*:14491:0:99999:7:::
+man:*:14491:0:99999:7:::
+lp:*:14491:0:99999:7:::
+mail:*:14491:0:99999:7:::
+news:*:14491:0:99999:7:::
+uucp:*:14491:0:99999:7:::
+proxy:*:14491:0:99999:7:::
+www-data:*:14491:0:99999:7:::
+backup:*:14491:0:99999:7:::
+list:*:14491:0:99999:7:::
+irc:*:14491:0:99999:7:::
+gnats:*:14491:0:99999:7:::
+nobody:*:14491:0:99999:7:::
+libuuid:!:14491:0:99999:7:::
+Debian-exim:!:14491:0:99999:7:::
+statd:*:14491:0:99999:7:::
+messagebus:*:14491:0:99999:7:::
+avahi-autoipd:*:14491:0:99999:7:::
+avahi:*:14491:0:99999:7:::
+gdm:*:14491:0:99999:7:::
+haldaemon:*:14491:0:99999:7:::
+hplip:*:14491:0:99999:7:::
+bilbo:*:15101:0:99999:7:::
+festival:*:14492:0:99999:7:::
+sshd:*:14493:0:99999:7:::
+frodo:!:14503:0:99999:7:::
+gsmsms:*:14681:0:99999:7:::
+speech-dispatcher:!:14688:0:99999:7:::
+vde2-net:*:14810:0:99999:7:::
+dnsmasq:*:14921:0:99999:7:::
+polkituser:*:14933:0:99999:7:::
+saned:*:14933:0:99999:7:::
+usbmux:*:15007:0:99999:7:::
+cl-builder:!:15007:0:99999:7:::
+pulse:*:15007:0:99999:7:::
+libvirt-qemu:!:15007:0:99999:7:::
+freevo:!:15098:0:99999:7:::