From 240d7625a25925743021fbf08abf444a68f2f588 Mon Sep 17 00:00:00 2001 From: Olivier Blin Date: Mon, 1 Nov 2010 22:11:52 +0000 Subject: add --tar and --untar support to iurt_root_command --- iurt_root_command | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'iurt_root_command') diff --git a/iurt_root_command b/iurt_root_command index eca6e60..6c79126 100755 --- a/iurt_root_command +++ b/iurt_root_command @@ -112,6 +112,28 @@ $run{todo} = []; [ "", "modprobe", 1, "]", "modprobe try to modprobe the given module if authorized.", \&modprobe, "Modprobing" ], + [ "", "tar", [ + ["", "tar", 2, " ", "tar directory into file", + sub { + my ($tmp, @arg) = @_; + $tmp->[0] ||= {}; + push @$tmp, @arg; + 1; + }, "Setting tar command arguments"], + ], " ", + "Create tarball", + \&tar, "Create tarball" ], + [ "", "untar", [ + ["", "untar", -1, " [files]", "untar file into directory (optionally selecting files only)", + sub { + my ($tmp, @arg) = @_; + $tmp->[0] ||= {}; + push @$tmp, @arg; + 1; + }, "Setting untar command arguments"], + ], " [files]", + "Uncompress tarball", + \&untar, "Uncompress tarball" ], ); open(my $LOG, ">&STDERR"); @@ -283,3 +305,42 @@ sub ln { link $file1, $file2; } +sub check_tar_authorized { + my ($file, $dir) = @_; + if (!$ENV{SUDO_USER}) { + plog('FAIL', "must be run from sudo"); + return; + } + my $authorized = (getpwnam($ENV{SUDO_USER}))[7]; + if (!$authorized) { + plog('FAIL', "can't find home for $ENV{SUDO_USER}"); + return; + } + if ($file !~ /^\Q$authorized\E/ || $dir !~ /^\Q$authorized\E/) { + plog('FAIL', "(un)tar: $file or $dir forbidden"); + return; + } + + 1; +} + +sub tar { + my ($_run, $_opt, $file, $dir) = @_; + check_tar_authorized($file, $dir) or return; + return !system('tar', 'caf', $file, '-C', $dir, '.'); +} + +sub untar { + my ($_run, $_opt, $file, $dir, @o_files) = @_; + if (!$file || !$dir) { + plog('FAIL', "untar: missing arguments"); + return; + } + check_tar_authorized($file, $dir) or return; + if (any { /^-/ } @o_files) { + plog('FAIL', "untar: options forbidden"); + return; + } + mkdir_p($dir); + return !system('tar', 'xf', $file, '-C', $dir, @o_files); +} -- cgit v1.2.1