tags:

views:

65

answers:

1

I am working on a Perl script where it checks for root access, at the beginning of execution.

if ($< != 0) {
  print "You need to be root";
  die "  ";
}

How is the $< evaluated in Windows? I searched google and here, but could not find an answer.

The closest I got was in perlvar. It has some descriptions of special variable handling in Windows, but not for this case.

$<

The real uid of this process. (Mnemonic: it's the uid you came from, if you're running setuid.) You can change both the real uid and the effective uid at the same time by using POSIX::setuid(). Since changes to $< require a system call, check $! after a change attempt to detect any possible errors.

+10  A: 

On my Win7 box, $< and $> always return 0, regardless of whether I'm running as an Administrator, elevated Administrator, or regular user. I think Perl just punts on that because user IDs on Windows cannot be expressed simply as integers, so it doesn't even try.

Indeed, here's the source code (line 1073):

/* Ownership
 *
 * Just pretend that everyone is a superuser. NT will let us know if
 * we don\'t really have permission to do something.
 */

#define ROOT_UID    ((uid_t)0)
#define ROOT_GID    ((gid_t)0)

uid_t
getuid(void)
{
    return ROOT_UID;
}

uid_t
geteuid(void)
{
    return ROOT_UID;
}

gid_t
getgid(void)
{
    return ROOT_GID;
}

gid_t
getegid(void)
{
    return ROOT_GID;
}

int
setuid(uid_t auid)
{
    return (auid == ROOT_UID ? 0 : -1);
}

int
setgid(gid_t agid)
{
    return (agid == ROOT_GID ? 0 : -1);
}
Gabe
+1 for including the relevant section of source.
Jon Purdy
I have never seen a better case to be picked as correct. That is an irrefutable response to the OP
Joel