User-AccountChecker

 view release on metacpan or  search on metacpan

lib/User/AccountChecker.pm  view on Meta::CPAN

    
    # need root permissions for a shell command
    my $shellcommand = $uuac->shellrootcmd("cat /etc/shadow");
    # if current user is root, $shellcommand == "cat /etc/shadow" 
    # otherwise, $shellcommand == "sudo cat /etc/shadow", and
    # $ENV{'SUDO_ASKPASS'} if ssh-askpass is installed
    
    # force a script to be runned as root
    $uuac->runasroot(@ARGV);
    # $uuac->isroot() should be true
    
    # need root permissions to continue
    $uuac->musttoberoot();
    print("if you see this message then you get root permissions.\n");

=head1 SUBROUTINES/METHODS

=head2 new

C<User::AccountChecker-E<gt>new()>

I<Constructor>

=over 4

=item

return new instance of User::AccountChecker

=back

=cut

sub new {
    my ($this) = shift;
    return bless({}, $this);
}

=head2 isuser

C<$object-E<gt>isuser($name)>

I<Checks if the current user is B<C<$name>>.>

=over 4

=item

S<param        string        B<C<$name>>        the name of the user account to check>

=item

return true if the current user is B<C<$name>>, false otherwise

=back

=cut

sub isuser {
    my ($this, $name) = @_;
    return ($name eq getpwuid($<)) ? 1 : 0;
}

=head2 isroot

C<$object-E<gt>isroot()>

I<Checks if the current user is root.>

=over 4

=item

return true if the current user is root, false otherwise

=back

=cut

sub isroot {
    my ($this) = shift;
    return $this->isuser("root");
}

=head2 musttoberoot

C<$object-E<gt>musttoberoot()>

S<I<Requires the current user to be logged as root.>
I<Dies if the current user is not root.>>

=cut

sub musttoberoot {
    my ($this) = shift;
    unless ($this->isroot) {
        die("You must to be logged as root.");
    }
}

=head2 shellrootcmd

C<$object-E<gt>shellrootcmd($command)>

S<I<Checks for ssh-askpass linux command and initialize B<C<$ENV{'SUDO_ASKPASS'}>> if it is founded.>
I<Adds sudo at the beginning of a command, or at the beginning of each piped commands.>>

=over 4

=item

S<param        string        B<C<$command>>        the command to wich add sudo>

=item

return B<C<$command>> with sudo prefix if the current user isn't root, else return B<C<$command>>

=back  

=cut



( run in 2.980 seconds using v1.01-cache-2.11-cpan-a49fcb8fa48 )