From b09d4fd1f2ce3e506974df62ea0b83efd7be10e4 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 19 Feb 2017 16:59:30 +0100 Subject: borg-restore.pl: Move untaint to Helper package Signed-off-by: Florian Pritz --- borg-restore.pl | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/borg-restore.pl b/borg-restore.pl index 5ac2682..5a0a139 100755 --- a/borg-restore.pl +++ b/borg-restore.pl @@ -106,14 +106,6 @@ sub debug { say STDERR @_ if $opts{debug}; } -sub untaint { - my $data = shift; - my $regex = shift; - - $data =~ m/^($regex)$/ or die "Failed to untaint: $data"; - return $1; -} - sub borg_list { my @archives; @@ -254,9 +246,9 @@ sub restore { my $archive = shift; my $destination = shift; - $destination = untaint($destination, qr(.*)); - $path = untaint($path, qr(.*)); - my $archive_name = untaint($archive->{archive}, qr([a-zA-Z0-9-]+)); + $destination = Helper::untaint($destination, qr(.*)); + $path = Helper::untaint($path, qr(.*)); + my $archive_name = Helper::untaint($archive->{archive}, qr([a-zA-Z0-9-]+)); printf "Restoring %s to %s from archive %s\n", $path, $destination, $archive->{archive}; @@ -269,7 +261,7 @@ sub restore { chdir($destination) or die "Failed to chdir: $!"; my $final_destination = abs_path($basename); - $final_destination = untaint($final_destination, qr(.*)); + $final_destination = Helper::untaint($final_destination, qr(.*)); debug("Removing ".$final_destination); File::Path::remove_tree($final_destination); system(qw(borg extract -v --strip-components), $components_to_strip, "::".$archive_name, $path); @@ -496,7 +488,7 @@ sub update_cache { sub main { # untaint PATH because we only expect this to run as root - $ENV{PATH} = untaint($ENV{PATH}, qr(.*)); + $ENV{PATH} = Helper::untaint($ENV{PATH}, qr(.*)); Getopt::Long::Configure ("bundling"); GetOptions(\%opts, "help|h", "debug", "update-cache|u", "destination|d=s", "time|t=s") or pod2usage(2); @@ -607,14 +599,6 @@ sub initialize_db { $self->{dbh}->do('create table `archives` (`archive_name` text unique);'); } -sub untaint { - my $data = shift; - my $regex = shift; - - $data =~ m/^($regex)$/ or die "Failed to untaint: $data"; - return $1; -} - sub get_archive_names { my $self = shift; @@ -641,7 +625,7 @@ sub add_archive_name { my $self = shift; my $archive = shift; - $archive = untaint($archive, qr([a-zA-Z0-9-]+)); + $archive = Helper::untaint($archive, qr([a-zA-Z0-9-]+)); my $st = $self->{dbh}->prepare('insert into `archives` (`archive_name`) values (?);'); $st->execute($archive); @@ -662,7 +646,7 @@ sub remove_archive { my $self = shift; my $archive = shift; - $archive = untaint($archive, qr([a-zA-Z0-9-]+)); + $archive = Helper::untaint($archive, qr([a-zA-Z0-9-]+)); my $archive_id = $self->get_archive_id($archive); @@ -688,7 +672,7 @@ sub remove_archive { sub _prefix_archive_id { my $archive = shift; - $archive = untaint($archive, qr([a-zA-Z0-9-]+)); + $archive = Helper::untaint($archive, qr([a-zA-Z0-9-]+)); return 'timestamp-'.$archive; } @@ -705,7 +689,7 @@ sub get_archives_for_path { my $path = shift; my $st = $self->{dbh}->prepare('select * from `files` where `path` = ?;'); - $st->execute(untaint($path, qr(.*))); + $st->execute(Helper::untaint($path, qr(.*))); my @ret; @@ -757,3 +741,13 @@ sub vacuum { $self->{dbh}->do("vacuum"); } + +package Helper; + +sub untaint { + my $data = shift; + my $regex = shift; + + $data =~ m/^($regex)$/ or die "Failed to untaint: $data"; + return $1; +} -- cgit v1.2.3-24-g4f1b