NYCPHP Meetup

NYPHP.org

[nycphp-talk] Determining if cmd line script is running as root?

Michael B Allen ioplex at gmail.com
Mon Jul 30 02:49:34 EDT 2007


On 7/26/07, Jon Baer <jonbaer at jonbaer.com> wrote:
> One possible way ...
>
> <?php $user = `whoami`; echo $user; ?>
>
> $php whoami.php
> jonbaer
> $sudo php whoami.php
> root

Hi Guys,

Actually what I settled on was:

function is_root() {
    if (is_writable('/'))
        return TRUE;
    if (extension_loaded('posix') && posix_getuid() == 0)
        return TRUE;
    if (getenv('USER') == 'root')
        return TRUE;
    return FALSE;
}

But looking at this now I think perhaps
function_exists('posix_getuid') would be better than extension_loaded.
I'll have to change that.

Also, I guess is_writeable('/') isn't fool-proof.

Mike



More information about the talk mailing list