blib/lib/Unix/Sudo.pm

filter: Statementstmt 44.2% 23 / 52 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 38.9% 30 / 77 SCAR 36.5
CC
10
Cov
34%
sudo
110.0
BEGIN
1.0
BEGIN
1.0
CRAP 38.3
↑ Summary
1 package Unix::Sudo;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = '1';
7  
8 5 require Exporter;
9 5 our @ISA = qw(Exporter);
10 5 our @EXPORT_OK = qw(sudo);
11  
12 5 ▶use B::Deparse;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use PadWalker qw(peek_my);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Probe::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 Unix::Sudo - run a block as root
20  
21 =head1 SYNOPSIS
22  
23 As a normal user who can C<sudo> ...
24  
25 use Unix::Sudo qw(sudo);
26  
27 print `whoami`; # shows your normal username
28 sudo {
29 eval "no tainting";
30 print `whoami`; # root
31 };
32 print `whoami`; # back to normal
33  
34 =head1 EXPORTS / FUNCTIONS
35  
36 There is one function, which can be exported if you wish but is not exported by
37 default:
38  
39 =head2 sudo
40  
41 Takes a code-ref as its only argument. This can be in a variable, or as an
42 anonymous code-ref, or a block:
43  
44 my $code = sub { 1 + 2 };
45 sudo($code);
46  
47 sudo(sub { 1 + 2 });
48  
49 sudo { 1 + 2 };
50  
51 That code-ref will be executed as root, with no arguments, and with as much as
52 possible of the calling code's environment. It may return an integer value from
53 0 to 254. Anything else will be numified.
54  
55 If you want to return something more complicated then I recommend that you
56 return it on STDOUT and use L<Capture::Tiny> to retrieve it.
57  
58 =head1 ERRORS
59  
60 A return value of 255 is special and is used to indicate that the code couldn't
61 be compiled for some reason. When this happens the child process will spit an
62 error message to STDERR, and the parent will die and attempt to tell you where
63 you passed dodgy code to Unix::Sudo.
64  
65 See L<CAVEATS> for some hints on circumstances when this might happen.
66  
67 =head1 HOW IT WORKS
68  
69 Internally, your code will be de-parsed into its text form and then executed thus:
70  
71 system(
72 'sudo', '-p', '...', 'perl', '-T',
73 '-I...', ...
74 '-e',
75 "exit(do{ $your_code_here })"
76 ) >> 8;
77  
78 C<sudo> might have to prompt for a password. If it does, then the prompt will
79 make it clear that this is Unix::Sudo asking for it.
80  
81 It's not just your code that is passed to the child process. There are also a
82 bunch of C<-I> arguments, so that it knows about any directories in the parent
83 process's C<@INC>, and it will also get copies of all the lexical variables
84 that are in scope in the calling code.
85  
86 Under the bonnet it uses L<B::Deparse> to turn your code-ref into text,
87 L<PadWalker>'s C<peek_my()> to get variables, and L<Data::Dumper> (and
88 C<$Data::Dumper::Deparse>) to turn those variables into text, all of which is
89 pre-pended to your code.
90  
91 =head1 CAVEATS
92  
93 Your code will always have C<strict> and C<warnings> turned on, and be run with
94 taint-checking enabled. If you need to you can turn tainting off as shown in
95 the synopsis. Note that you can't just say 'no tainting', the C<eval> is
96 required, otherwise C<no>, just like C<use>, will be run at compile-time I<in
97 the calling code> and not in the child process where you need it.
98  
99 If your code needs to C<use> any modules, or any subroutines that are imported,
100 you will need to say so inside the code-ref you pass. And again, remember that
101 you'll have to C<eval> any C<use> statements.
102  
103 The variables that are passed through to your code are read-write, but any
104 changes you make are local to the child process so will not be communicated
105 back to the parent process.
106  
107 Any blessed references, tied variables, or objects that use C<overload> in
108 those variables may not behave as you expect. For example, a record that has
109 been read from a database won't have an active database connection; something
110 tied to a filehandle won't have an open filehandle; an object that uses
111 C<overload> to make reading its value have side-effects will not have those
112 side-effects respected in the parent process. In general, you should use this
113 to "promote" as little of your code as possible to run as root, and I<only>
114 your code so that you can be as aware as possible of the preceding.
115  
116 =head1 DEBUGGING
117  
118 If your code isn't behaving as you expect or is dieing then I recommend that
119 you set UNIX_SUDO_SPILLGUTS=1 in your environment. This will cause Unix::Sudo
120 to C<warn()> you about what it is about to execute before it does so.
121  
122 =head1 SECURITY CONCERNS
123  
124 This code will run potentially user-supplied code as root. I have done what I
125 can to avoid security hilarity, but if you allow someone to pass C<rm -rf /*>
126 that's your problem.
127  
128 I have mitigated potential problems by:
129  
130 =over
131  
132 =item using the LIST form of C<system>
133  
134 It shouldn't be possible to craft input that makes my code run your code as
135 root and then make my code run something else as root.
136  
137 It is of course still possible to make my code run your code as root and for
138 your code to then run other stuff as root.
139  
140 =item tainting is turned on
141  
142 That means that any input to your code from the outside world is internally
143 marked as being untrusted, and you are restricted in what you can do with it.
144 You can of course circumvent this by untainting, either in the usual regexy
145 ways or as noted above via C<no tainting>.
146  
147 =back
148  
149 I strongly recommend that you read and understand the source code and also read
150 L<perlsec> before using this code.
151  
152 =head1 STABILITY
153  
154 I make no promises about the stability of the interface, and it is subject to
155 change without notice. This is because I want to strongly encourage you to
156 read the documentation and the source code before installing new versions of
157 this code.
158  
159 I also therefore urge you that if you use this module in anything important
160 you should "pin" it to a particular version number in whatever you use for
161 managing your dependencies.
162  
163 =head1 BUGS/FEEDBACK
164  
165 Please report bugs at
166 L<https://github.com/DrHyde/perl-modules-Unix-Sudo/issues>, including, if
167 possible, a test case.
168  
169 =head1 SOURCE CODE REPOSITORY
170  
171 L<git://github.com/DrHyde/perl-modules-Unix-Sudo.git>
172  
173 =head1 AUTHOR, COPYRIGHT and LICENCE
174  
175 Copyright 2019 David Cantrell E<lt>F<david@cantrell.org.uk>E<gt>
176  
177 This software is free-as-in-speech software, and may be used, distributed, and
178 modified under the terms of either the GNU General Public Licence version 2 or
179 the Artistic Licence. It's up to you which one you use. The full text of the
180 licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively.
181  
182 =head1 CONSPIRACY
183  
184 This module is also free-as-in-mason software.
185  
186 =cut
187  
188 sub sudo(&) {
189 0 ▶ my $context = peek_my(1);
Subroutine sudo: not called CC=10 SCAR=47.0
Pod documented
190 0 my $code = '';
191  
192 0 my $deparse = B::Deparse->new();
193  
194 # declare all the variables first, in case one of them is a code-ref
195 # that refers to one of the others
196 0 foreach my $variable (keys %{$context}) {
197 0 $code .= "my $variable;\n";
198 }
199 # now give them their values
200 0 foreach my $variable (keys %{$context}) {
201 0 local $Data::Dumper::Deparse = 1;
202 0 local $Data::Dumper::Indent = 0;
203  
204 0 my $value = $context->{$variable};
205  
206 0 ▶ if(substr($variable, 0, 1) eq '%') {
Branch: 3 branches
BranchTrueFalseTotal
if (substr($variable, 0, 1) eq '%') { } 0 0 0
elsif (substr($variable, 0, 1) eq '@') { } 0 0 0
elsif (substr($variable, 0, 1) eq '$') { } 0 0 0
207 0 $code .= "$variable = %{ (), do { my ".Dumper($value)."}};\n"
208 } elsif(substr($variable, 0, 1) eq '@') {
209 0 $code .= "$variable = \@{ (), do { my ".Dumper($value)."}};\n"
210 } elsif(substr($variable, 0, 1) eq '$') {
211 0 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'REF' and ref ${$value;} eq 'CODE') { } 0 0 0
Condition0%0/3
ref $value eq "REF" and ref ${$value;} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value eq "REF" and ref ${$value;} eq "CODE"
ref $value eq "REF" ref ${$value;} eq "CODE"
Truth table0%0/3
A and B
A= ref $value eq "REF"B= ref ${$value;} eq "CODE"
ABresult
0X0
100
111
212 ref($value) eq 'REF' &&
213 0 ref(${$value}) eq 'CODE'
214 ) {
215 $code .= "$variable = sub ".
216 0 $deparse->coderef2text(${$value}).
217 ";\n";
218 } else {
219 0 $code .= "$variable = \${ scalar do { my ".Dumper($value)."}};\n"
220 }
221 } else {
222 0 die("Sorry, Unix::Sudo can't cope with sigil '".
223 substr($variable, 0, 1).
224 "'\n");
225 }
226 }
227  
228 0 $code .= $deparse->coderef2text(shift);
229  
230 0 ▶ if($ENV{UNIX_SUDO_SPILLGUTS}) { warn $code }
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"UNIX_SUDO_SPILLGUTS"}) 0 0 0
231
232 my $rv = system(
233 "sudo", "-p", "Unix::Sudo needs your password: ",
234 Probe::Perl->find_perl_interpreter(),
235 0 (map { "-I$_" } grep { -d } @INC),
236 "-T", "-e", "exit do { $code }"
237 ) >> 8;
238  
239 0 ▶ if($rv == 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == 255) { } 0 0 0
240 0 die(sprintf(
241 "Your code didn't compile when passed to Unix::Sudo::sudo at %s line %s\n",
242 (caller(1))[1, 2]
243 ));
244 } else {
245 0 return $rv
246 }
247 }
248  
249 5 1;
↑ Summary