Skip to content

Commit

Permalink
id: uid 0 is valid
Browse files Browse the repository at this point in the history
* Problem1: id reports that my root user doesn't exist because the uid is zero
* This is because $uid is undef if getpwnam() fails, but this was treated the same as 0

$ id root
uid=0(root) gid=0(root) groups=0(root),117(lpadmin)
$ perl id root
id: root: No such user

* Problem2: id reports that 'root77' user exists when it does not
* This is because a non-numeric parameter was passed to getpwuid()

$ perl id root77
uid=0(root) gid=0(root) groups=117(lpadmin),0(root)
  • Loading branch information
mknos authored Oct 8, 2023
1 parent 97b398d commit 92678da
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions bin/id
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ my($user,$pw,$uid,$gid,$tp);

if ( @ARGV ) { # user specified
($user,$pw,$uid,$gid) = getpwnam $ARGV[0];
($user,$pw,$uid,$gid) = getpwuid $ARGV[0] unless ( $uid );
die "id: $ARGV[0]: No such user\n" unless ( $uid );
if (!defined($uid) && $ARGV[0] =~ m/\A[0-9]+\Z/) {
($user,$pw,$uid,$gid) = getpwuid $ARGV[0];
}
die "id: $ARGV[0]: No such user\n" unless (defined $uid);
}

if ( $opt_u ) { # print uid
$tp = ($uid)?$uid:($opt_r)?$<:$>;
$tp = defined $uid ? $uid : $opt_r ? $< : $>;
$tp = scalar getpwuid $tp || $tp if ( $opt_n );
}
elsif ( $opt_g ) { # print gid
Expand Down

0 comments on commit 92678da

Please sign in to comment.