line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package User::AccountChecker; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
92991
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
42
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
76
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
1234
|
use FindBin qw($Bin $Script); |
|
1
|
|
|
|
|
1769
|
|
|
1
|
|
|
|
|
3222
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
User::AccountChecker - I |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 VERSION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Version 0.02 |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=cut |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 SYNOPSIS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
S |
24
|
|
|
|
|
|
|
I> |
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
|
|
|
|
|
|
|
Cnew()> |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
I |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=over 4 |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=item |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
return new instance of User::AccountChecker |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=back |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=cut |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub new { |
74
|
0
|
|
|
0
|
1
|
|
my ($this) = shift; |
75
|
0
|
|
|
|
|
|
return bless({}, $this); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head2 isuser |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
C<$object-Eisuser($name)> |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
I>.> |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=over 4 |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=item |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
S> the name of the user account to check> |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=item |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
return true if the current user is B>, false otherwise |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=back |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=cut |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub isuser { |
99
|
0
|
|
|
0
|
1
|
|
my ($this, $name) = @_; |
100
|
0
|
0
|
|
|
|
|
return ($name eq getpwuid($<)) ? 1 : 0; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 isroot |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
C<$object-Eisroot()> |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
I |
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
|
|
|
|
|
|
|
sub isroot { |
120
|
0
|
|
|
0
|
1
|
|
my ($this) = shift; |
121
|
0
|
|
|
|
|
|
return $this->isuser("root"); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 musttoberoot |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
C<$object-Emusttoberoot()> |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
S |
129
|
|
|
|
|
|
|
I> |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=cut |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub musttoberoot { |
134
|
0
|
|
|
0
|
1
|
|
my ($this) = shift; |
135
|
0
|
0
|
|
|
|
|
unless ($this->isroot) { |
136
|
0
|
|
|
|
|
|
die("You must to be logged as root."); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 shellrootcmd |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
C<$object-Eshellrootcmd($command)> |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
S> if it is founded.> |
145
|
|
|
|
|
|
|
I> |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=over 4 |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=item |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
S> the command to wich add sudo> |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
return B> with sudo prefix if the current user isn't root, else return B> |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=back |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=cut |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub shellrootcmd { |
162
|
0
|
|
|
0
|
1
|
|
my ($this, $cmd) = @_; |
163
|
|
|
|
|
|
|
# current user is root |
164
|
0
|
0
|
|
|
|
|
return $cmd if ($this->isroot); |
165
|
|
|
|
|
|
|
# current user is not root |
166
|
|
|
|
|
|
|
# ask pass |
167
|
0
|
|
|
|
|
|
my $askpass = `which ssh-askpass`; |
168
|
0
|
|
|
|
|
|
chomp($askpass); |
169
|
0
|
0
|
|
|
|
|
if (-e $askpass) { |
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-Erunasroot($commandargs)> |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
I |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=over 4 |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=item |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
S> the command arguments (eg. @ARGV)> |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=back |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=cut |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub runasroot { |
193
|
0
|
|
|
0
|
1
|
|
my ($this, @args) = @_; |
194
|
0
|
0
|
|
|
|
|
if (!$this->isroot()) { |
195
|
0
|
0
|
|
|
|
|
my $sargs = (@args > 0) ? ' '.join(' ', @args) : ''; |
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<< >> |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 BUGS |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
210
|
|
|
|
|
|
|
the web interface at L. 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 |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
L |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=item * CPAN Ratings |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
L |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=item * Search CPAN |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
L |
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
|
|
|
|
|
|
|
1; # End of User::AccountChecker |