aboutsummaryrefslogtreecommitdiffstats
path: root/iurt_root_command
blob: c88a932c0500a99b8c185c76fd53cbf6ef68db98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/perl
#
#!/usr/bin/perl
#
# Copyright (C) 2006 Mandriva
# 
# Author: Florent Villard <warly@mandriva.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# run commands which needs root privilege
#

use strict;
my $program_name = 'iurt_root_command';
use Mkcd::Commandline qw(parseCommandLine usage);
use MDK::Common;
use File::NCopy qw(copy);

my $arg = @ARGV;
my (@params, %run);
$run{program_name} = $program_name;

my %authorized_modules = ( 'unionfs' => 1 );
my $sudo = '/usr/bin/sudo';

$run{todo} = [ ];
@params = ( 
    #    [ "one letter option", "long name option", "number of args (-X means ´at least X´)", "help text", "function to call", "log info"]
    #
    # no_rsync, config_help and copy_srpm kept for compatibility reasons
    #
    [ "", "$program_name", 0, "[--verbose <level>] 
		    [--modprobe <module>] 
		    [--mkdir [--parents] <dir1> <dir2> ... <dirn>]", 
    "$program_name is a perl script to execute commands which need root privilege, it helps probram which needs occasional root privileges for some commands.", 
    sub { $arg or usage($program_name, \@params) }, "Running $program_name" ],
    [ "", "cp", [
   ["", "cp", -1, "[-r] <file1> <file2> ... <filen> <dest>]", "copy the files to dest",
        sub {    
            my ($tmp, @arg) = @_; 
            $tmp->[0] ||= {}; 
            push @$tmp, @arg; 
            1 
        }, "Setting cp command arguments"], 
        ["r", "recursive", 0, "",  
        "Also copy directories and subdirectories",  
        sub { my ($tmp) = @_; $tmp->[0]{recursive} = 1; 1 }, "Set the recursive flag"], 
    ], "[-r] <file1> <file2> ... <filen> <dest>", 
    "Copy files", 
    \&cp, "Copying files" ],
   [ "", "ln", [
   ["", "ln", 2, "<file1> <file2>", "link file1 to file2",
        sub {    
            my ($tmp, @arg) = @_; 
            $tmp->[0] ||= {}; 
            push @$tmp, @arg; 
            1 
        }, "Setting ln command arguments"], 
#        ["r", "recursive", 0, "",  
#        "Also create needed parents directories",  
#        sub { my ($tmp) = @_; $tmp->[0]{recursive} = 1; 1 }, "Set the recursive flag"], 
    ], "<file1> <file2>", 
    "Link files", 
    \&ln, "Linking files" ],
    [ "", "mkdir", [
    ["", "mkdir", -1, "[--parents] <dir1> <dir2> ... <dirn>]", "mkdir create the given path",
        sub {    
            my ($tmp, @arg) = @_; 
            $tmp->[0] ||= {}; 
            push @$tmp, @arg; 
            1 
        }, "Setting auto mode arguments"], 
        ["p", "parents", 0, "",  
        "Also create needed parents directories",  
        sub { my ($tmp) = @_; $tmp->[0]{parents} = 1; 1 }, "Set the parents flag"], 
    ], "[--parents] <dir1> <dir2> ... <dirn>]", 
    "mkdir create the given path", 
    \&mkdir, "Creating the path" ],
    [ "", "rm", [
   ["", "rm", -1, "[-r] <file1> <file2> ... <filen>", "remove the provided files",
        sub {    
            my ($tmp, @arg) = @_; 
            $tmp->[0] ||= {}; 
            push @$tmp, @arg; 
            1 
        }, "Setting rm command arguments"], 
        ["r", "recursive", 0, "",  
        "Also create needed parents directories",  
        sub { my ($tmp) = @_; $tmp->[0]{recursive} = 1; 1 }, "Set the recursive flag"], 
    ], "[-r] <file1> <file2> ... <filen>", 
    "Remove files", 
    \&rm, "Removing files" ],
    [ "", "initdb", 1 , "<chroot>]", 
    "perform a rpm --initdb in the chroot.", 
    \&initdb, "Initializing the rpm database" ],
    [ "v", "verbose", 1, "<verbose level>", 
    "modprobe try to modprobe the given module if authorized.", 
    sub { $run{verbose} = @_->[0]; 1 }, "Setting verbose level" ],
    [ "", "modprobe", 1, "<module>]", 
    "modprobe try to modprobe the given module if authorized.", 
    \&modprobe, "Modprobing" ],
);

open(my $LOG, ">&STDERR");
$run{LOG} = $LOG;

#print {$run{LOG}} "$program_name: @ARGV\n";
my $todo = parseCommandLine($program_name, \@ARGV, \@params);
@ARGV and usage($program_name, \@params, "@ARGV, too many arguments");
my $ok = 1;
foreach my $t (@$todo)  {
    print {$run{LOG}} "$program_name: $t->[2]\n" if $run{verbose} > 5;
    my $ok2 = &{$t->[0]}(\%run, @{$t->[1]});
    $ok2 or print {$run{LOG}} "ERROR: $t->[2]\n";
    $ok &&= $ok2;
}
print "$program_name: Success!\n" if $ok;
exit !$ok;

sub modprobe {
    my ($run, $module) = @_;
    if (!$authorized_modules{$module}) {
	print {$run->{LOG}} "ERROR $program_name: unauthorized module $module\n";
	return 0
    }
    open my $modules, '/proc/modules';
    my $ok;
    while (my $m = <$modules>) {
	if ($m =~ /unionfs/) {
	    return 1
	}
    }
    system($sudo, "/sbin/depmod", "-a");
    !system($sudo, "/sbin/modprobe", "-f", $module)
}

sub mkdir {
    my ($run, $opt, @dir) = @_;
    foreach my $path (@dir) {
	-d $path and next;
	if ($path =~ m,/dev|/proc|/root|/var, && $path !~ /chroot|unionfs/) {
	    print {$run->{LOG}} "ERROR $program_name: $path creation forbidden\n";
	}
	if ($opt->{parents}) {
	    mkdir_p $path
	} else {
	    mkdir $path
	}
    }
    1
}

sub initdb {
    my ($run, $chroot) = @_;
    if (-d $chroot && $chroot !~ /chroot|unionfs/) {
	print {$run{LOG}} "ERROR $program_name: rpm --initdb not authorized in $chroot\n";
	return 0
    }
    !system("rpm", "--initdb", "--root", "$chroot")
}

sub rm {
    my ($run, $opt, @files) = @_;
    my $ok = 1;
    my $done;
    my $unauthorized = "^(/etc|/root|/dev|/var|/lib|/usr)";
    foreach my $f (@files) {
	if (-d $f) {
	    if (!$opt->{recursive}) {
		print {$run->{LOG}} "$program_name: could not remove directories without the -r option\n";
		$ok = 0
	    } else {
		if ($f =~ m,$unauthorized,) {
		    print {$run->{LOG}} "$program_name: removal of $f forbidden\n";
		    $ok = 0
		} else {
		    system($sudo, 'rm', '-rf', $f);
		    print {$run->{LOG}} "$program_name: removing $f\n" if $run->{verbose};
		    $done = 1
		}
	    }
	} else {
	    if ($f =~ m,/$unauthorized,) {
		print {$run->{LOG}} "$program_name: removal of $f forbidden\n";
		$ok = 0
	    } else {
		if ($f =~ /\*\?/) {
		    foreach my $file (glob $f) {
			if ($f =~ m,$unauthorized,) {
			    print {$run->{LOG}} "$program_name: removal of $f forbidden\n";
			    $ok = 0
			} else {
			    unlink $file;
			    $done = 1;
			    print {$run->{LOG}} "$program_name: removing $file\n" if $run->{verbose}
			}
		    }
		} else {
		    unlink $f;
		    $done = 1;
		    print {$run->{LOG}} "$program_name: removing $f\n" if $run->{verbose}
		}
	    }
	}
    }
    if (!$done) { print {$run->{LOG}} "$program_name: nothing deleted\n" }
    $ok
}

sub cp {
    my ($run, $opt, @files) = @_;
    my $ok = 1;
    my $done;
    my $dest = pop @files;
    my $unauthorized = "^(/etc|/root|/dev|/var|/lib|/usr)";
    if ($dest =~ /$unauthorized/ || $dest eq '/') {
	print {$run->{LOG}} "$program_name: copying to $dest forbidden\n";
	return
    }	
    foreach my $f (@files) {
	if (-d $f) {
	    if (!$opt->{recursive}) {
		print {$run->{LOG}} "$program_name: could not copy directories without the -r option\n";
		$ok = 0
	    } else {
		system($sudo, 'cp', '-raf', $f);
		print {$run->{LOG}} "$program_name: copying $f -> $dest\n" if $run->{verbose};
		$done = 1
	    }
	} else {
	    if ($f =~ /\*?/) {
		foreach my $file (glob $f) {
		    if (copy $file, $dest) {
			$done = 1;
			print {$run->{LOG}} "$program_name: copying $file -> $dest\n" if $run->{verbose}
		    } else {
			$ok = 0;
			print {$run->{LOG}} "$program_name: copying $file to $dest failed ($!)\n" if $run->{verbose}
		    }
		}
	    } else {
		if (copy $f, $dest) {
		    $done = 1;
		    print {$run->{LOG}} "$program_name: copying $f -> $dest\n" if $run->{verbose}
		} else {
		    $ok = 0;
		    print {$run->{LOG}} "$program_name: copying $f to $dest failed ($!)\n" if $run->{verbose}
		}
	    }
	}
    }
    if (!$done) { print {$run->{LOG}} "$program_name: nothing copied\n" }
    $ok
}

sub ln {
    my ($run, $opt, $file1, $file2) = @_;
    my $unauthorized = "^(/etc|/root|/dev|/var|/lib|/usr)";
    if ($file2 =~ /$unauthorized/ || $file2 eq '/') {
	print {$run->{LOG}} "$program_name: linking to $file2 forbidden\n";
	return
    }	
    link $file1, $file2;
}