| 1 | package User::AccountChecker; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use FindBin qw($Bin $Script); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | User::AccountChecker - I<Tools for user account checking in an Unix environment> | ||||||||||
| 11 | |||||||||||
| 12 | =head1 VERSION | ||||||||||
| 13 | |||||||||||
| 14 | Version 0.02 | ||||||||||
| 15 | |||||||||||
| 16 | =cut | ||||||||||
| 17 | |||||||||||
| 18 | 2 | our $VERSION = '0.02'; | |||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||
| 22 | |||||||||||
| 23 | S<I<Provides an interface to check current user account.> | ||||||||||
| 24 | I<Useful for shell scripting.>> | ||||||||||
| 25 | |||||||||||
| 26 | A little code snippet : | ||||||||||
| 27 | |||||||||||
| 28 | use User::AccountChecker; | ||||||||||
| 29 | |||||||||||
| 30 | my $uuac = User::AccountChecker->new(); | ||||||||||
| 31 | # need to identify current user | ||||||||||
| 32 | die("You are not allowed to continue.\n") unless($uuac->isuser('root')); | ||||||||||
| 33 | |||||||||||
| 34 | # check if root user | ||||||||||
| 35 | if ($uuac->isroot) { | ||||||||||
| 36 | print("You are root.\n"); | ||||||||||
| 37 | } else { | ||||||||||
| 38 | print("You are not root.\n"); | ||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | # need root permissions for a shell command | ||||||||||
| 42 | my $shellcommand = $uuac->shellrootcmd("cat /etc/shadow"); | ||||||||||
| 43 | # if current user is root, $shellcommand == "cat /etc/shadow" | ||||||||||
| 44 | # otherwise, $shellcommand == "sudo cat /etc/shadow", and | ||||||||||
| 45 | # $ENV{'SUDO_ASKPASS'} if ssh-askpass is installed | ||||||||||
| 46 | |||||||||||
| 47 | # force a script to be runned as root | ||||||||||
| 48 | $uuac->runasroot(@ARGV); | ||||||||||
| 49 | # $uuac->isroot() should be true | ||||||||||
| 50 | |||||||||||
| 51 | # need root permissions to continue | ||||||||||
| 52 | $uuac->musttoberoot(); | ||||||||||
| 53 | print("if you see this message then you get root permissions.\n"); | ||||||||||
| 54 | |||||||||||
| 55 | =head1 SUBROUTINES/METHODS | ||||||||||
| 56 | |||||||||||
| 57 | =head2 new | ||||||||||
| 58 | |||||||||||
| 59 | C<User::AccountChecker-E<gt>new()> | ||||||||||
| 60 | |||||||||||
| 61 | I<Constructor> | ||||||||||
| 62 | |||||||||||
| 63 | =over 4 | ||||||||||
| 64 | |||||||||||
| 65 | =item | ||||||||||
| 66 | |||||||||||
| 67 | return new instance of User::AccountChecker | ||||||||||
| 68 | |||||||||||
| 69 | =back | ||||||||||
| 70 | |||||||||||
| 71 | =cut | ||||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 74 | 0 | my ($this) = shift; | |||||||||
| 75 | 0 | return bless({}, $this); | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | =head2 isuser | ||||||||||
| 79 | |||||||||||
| 80 | C<$object-E<gt>isuser($name)> | ||||||||||
| 81 | |||||||||||
| 82 | I<Checks if the current user is B<C<$name>>.> | ||||||||||
| 83 | |||||||||||
| 84 | =over 4 | ||||||||||
| 85 | |||||||||||
| 86 | =item | ||||||||||
| 87 | |||||||||||
| 88 | S<param string B<C<$name>> the name of the user account to check> | ||||||||||
| 89 | |||||||||||
| 90 | =item | ||||||||||
| 91 | |||||||||||
| 92 | return true if the current user is B<C<$name>>, false otherwise | ||||||||||
| 93 | |||||||||||
| 94 | =back | ||||||||||
| 95 | |||||||||||
| 96 | =cut | ||||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | sub isuser { | ||||||||
|
Subroutine
isuser: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 99 | 0 | my ($this, $name) = @_; | |||||||||
| 100 | 0 | ▶ | return ($name eq getpwuid($<)) ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | =head2 isroot | ||||||||||
| 104 | |||||||||||
| 105 | C<$object-E<gt>isroot()> | ||||||||||
| 106 | |||||||||||
| 107 | I<Checks if the current user is root.> | ||||||||||
| 108 | |||||||||||
| 109 | =over 4 | ||||||||||
| 110 | |||||||||||
| 111 | =item | ||||||||||
| 112 | |||||||||||
| 113 | return true if the current user is root, false otherwise | ||||||||||
| 114 | |||||||||||
| 115 | =back | ||||||||||
| 116 | |||||||||||
| 117 | =cut | ||||||||||
| 118 | |||||||||||
| 119 | 0 | ▶ | sub isroot { | ||||||||
|
Subroutine
isroot: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 120 | 0 | my ($this) = shift; | |||||||||
| 121 | 0 | return $this->isuser("root"); | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | =head2 musttoberoot | ||||||||||
| 125 | |||||||||||
| 126 | C<$object-E<gt>musttoberoot()> | ||||||||||
| 127 | |||||||||||
| 128 | S<I<Requires the current user to be logged as root.> | ||||||||||
| 129 | I<Dies if the current user is not root.>> | ||||||||||
| 130 | |||||||||||
| 131 | =cut | ||||||||||
| 132 | |||||||||||
| 133 | 0 | ▶ | sub musttoberoot { | ||||||||
|
Subroutine
musttoberoot: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 134 | 0 | my ($this) = shift; | |||||||||
| 135 | 0 | ▶ | unless ($this->isroot) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 0 | die("You must to be logged as root."); | |||||||||
| 137 | } | ||||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | =head2 shellrootcmd | ||||||||||
| 141 | |||||||||||
| 142 | C<$object-E<gt>shellrootcmd($command)> | ||||||||||
| 143 | |||||||||||
| 144 | S<I<Checks for ssh-askpass linux command and initialize B<C<$ENV{'SUDO_ASKPASS'}>> if it is founded.> | ||||||||||
| 145 | I<Adds sudo at the beginning of a command, or at the beginning of each piped commands.>> | ||||||||||
| 146 | |||||||||||
| 147 | =over 4 | ||||||||||
| 148 | |||||||||||
| 149 | =item | ||||||||||
| 150 | |||||||||||
| 151 | S<param string B<C<$command>> the command to wich add sudo> | ||||||||||
| 152 | |||||||||||
| 153 | =item | ||||||||||
| 154 | |||||||||||
| 155 | return B<C<$command>> with sudo prefix if the current user isn't root, else return B<C<$command>> | ||||||||||
| 156 | |||||||||||
| 157 | =back | ||||||||||
| 158 | |||||||||||
| 159 | =cut | ||||||||||
| 160 | |||||||||||
| 161 | 0 | ▶ | sub shellrootcmd { | ||||||||
|
Subroutine
shellrootcmd: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 162 | 0 | my ($this, $cmd) = @_; | |||||||||
| 163 | # current user is root | ||||||||||
| 164 | 0 | ▶ | return $cmd if ($this->isroot); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 165 | # current user is not root | ||||||||||
| 166 | # ask pass | ||||||||||
| 167 | 0 | my $askpass = `which ssh-askpass`; | |||||||||
| 168 | 0 | chomp($askpass); | |||||||||
| 169 | 0 | ▶ | if (-e $askpass) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | 0 | $ENV{'SUDO_ASKPASS'} = $askpass; | |||||||||
| 171 | } | ||||||||||
| 172 | 0 | $cmd =~ s/(&&|\|)/$1 sudo/ig; | |||||||||
| 173 | 0 | return 'sudo '.$cmd; | |||||||||
| 174 | } | ||||||||||
| 175 | |||||||||||
| 176 | =head2 runasroot | ||||||||||
| 177 | |||||||||||
| 178 | C<$object-E<gt>runasroot($commandargs)> | ||||||||||
| 179 | |||||||||||
| 180 | I<Forces a script to be runned as root> | ||||||||||
| 181 | |||||||||||
| 182 | =over 4 | ||||||||||
| 183 | |||||||||||
| 184 | =item | ||||||||||
| 185 | |||||||||||
| 186 | S<param array B<C<$commandargs>> the command arguments (eg. @ARGV)> | ||||||||||
| 187 | |||||||||||
| 188 | =back | ||||||||||
| 189 | |||||||||||
| 190 | =cut | ||||||||||
| 191 | |||||||||||
| 192 | 0 | ▶ | sub runasroot { | ||||||||
|
Subroutine
runasroot: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 193 | 0 | my ($this, @args) = @_; | |||||||||
| 194 | 0 | ▶ | if (!$this->isroot()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 195 | 0 | ▶ | my $sargs = (@args > 0) ? ' '.join(' ', @args) : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 196 | 0 | my $cmd = $this->shellrootcmd('su -c "'.$Bin.'/'.$Script.$sargs.'"'); | |||||||||
| 197 | 0 | chomp($cmd); | |||||||||
| 198 | 0 | system($cmd); | |||||||||
| 199 | 0 | exit 0; | |||||||||
| 200 | } | ||||||||||
| 201 | } | ||||||||||
| 202 | |||||||||||
| 203 | =head1 AUTHOR | ||||||||||
| 204 | |||||||||||
| 205 | Eric Villard, C<< <evi at cpan.org> >> | ||||||||||
| 206 | |||||||||||
| 207 | =head1 BUGS | ||||||||||
| 208 | |||||||||||
| 209 | Please report any bugs or feature requests to C<bug-User at rt.cpan.org>, or through | ||||||||||
| 210 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=User>. I will be notified, and then you'll | ||||||||||
| 211 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 212 | |||||||||||
| 213 | |||||||||||
| 214 | |||||||||||
| 215 | |||||||||||
| 216 | =head1 SUPPORT | ||||||||||
| 217 | |||||||||||
| 218 | You can find documentation for this module with the perldoc command. | ||||||||||
| 219 | |||||||||||
| 220 | perldoc User::AccountChecker | ||||||||||
| 221 | |||||||||||
| 222 | |||||||||||
| 223 | You can also look for information at: | ||||||||||
| 224 | |||||||||||
| 225 | =over 4 | ||||||||||
| 226 | |||||||||||
| 227 | =item * RT: CPAN's request tracker | ||||||||||
| 228 | |||||||||||
| 229 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=User> | ||||||||||
| 230 | |||||||||||
| 231 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 232 | |||||||||||
| 233 | L<http://annocpan.org/dist/User> | ||||||||||
| 234 | |||||||||||
| 235 | =item * CPAN Ratings | ||||||||||
| 236 | |||||||||||
| 237 | L<http://cpanratings.perl.org/d/User> | ||||||||||
| 238 | |||||||||||
| 239 | =item * Search CPAN | ||||||||||
| 240 | |||||||||||
| 241 | L<http://search.cpan.org/dist/User/> | ||||||||||
| 242 | |||||||||||
| 243 | =back | ||||||||||
| 244 | |||||||||||
| 245 | |||||||||||
| 246 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 247 | |||||||||||
| 248 | |||||||||||
| 249 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 250 | |||||||||||
| 251 | Copyright 2010 Eric Villard. | ||||||||||
| 252 | |||||||||||
| 253 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 254 | under the terms of either: the GNU General Public License as published | ||||||||||
| 255 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 256 | |||||||||||
| 257 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 258 | |||||||||||
| 259 | |||||||||||
| 260 | =cut | ||||||||||
| 261 | |||||||||||
| 262 | 2 | 1; # End of User::AccountChecker | |||||||||