1 package PVE
::Firewall
::Helpers
;
6 use Date
::Parse
qw(str2time);
8 use File
::Basename
qw(fileparse);
11 use PVE
::Tools
qw(file_get_contents file_set_contents);
21 my $pvefw_conf_dir = "/etc/pve/firewall";
24 my ($vmid, $timeout, $code, @param) = @_;
26 die "can't lock VM firewall config for undefined VMID\n"
29 my $res = PVE
::Cluster
::cfs_lock_firewall
("vm-$vmid", $timeout, $code, @param);
35 sub remove_vmfw_conf
{
38 my $vmfw_conffile = "$pvefw_conf_dir/$vmid.fw";
40 unlink $vmfw_conffile;
44 my ($vmid, $newid) = @_;
46 my $sourcevm_conffile = "$pvefw_conf_dir/$vmid.fw";
47 my $clonevm_conffile = "$pvefw_conf_dir/$newid.fw";
49 lock_vmfw_conf
($newid, 10, sub {
50 if (-f
$clonevm_conffile) {
51 unlink $clonevm_conffile;
53 if (-f
$sourcevm_conffile) {
54 my $data = file_get_contents
($sourcevm_conffile);
55 file_set_contents
($clonevm_conffile, $data);
61 my ($filename, $param, $callback) = @_;
62 my ($start, $limit, $since, $until) = $param->@{qw(start limit since until)};
67 if (defined($callback)) {
68 return undef if !$callback->($line);
71 if ($since || $until) {
72 my @words = split / /, $line;
73 my $timestamp = str2time
($words[3], $words[4]);
74 return undef if $since && $timestamp < $since;
75 return undef if $until && $timestamp > $until;
81 if (!defined($since) && !defined($until)) {
82 return PVE
::Tools
::dump_logfile
($filename, $start, $limit, $filter);
92 # Take into consideration also rotated logs
93 my ($basename, $logdir, $type) = fileparse
($filename);
94 my $regex = qr/^\Q$basename\E(\.[\d]+(\.gz)?)?$/;
97 PVE
::Tools
::dir_glob_foreach
($logdir, $regex, sub {
102 @files = reverse sort @files;
105 for my $filename (@files) {
106 $state{'final'} = $filecount == $#files;
110 if ($filename =~ /\.gz$/) {
111 $fh = IO
::Zlib-
>new($logdir.$filename, "r");
113 $fh = IO
::File-
>new($logdir.$filename, "r");
117 # If file vanished since reading dir entries, ignore
120 my $lines = $state{'lines'};
121 my $count = ++$state{'count'};
122 push @$lines, ($count, { n
=> $count, t
=> "unable to open file - $!"});
126 PVE
::Tools
::dump_logfile_by_filehandle
($fh, $filter, \
%state);
131 return ($state{'count'}, $state{'lines'});
135 my ($conf, $type, $scope) = @_;
140 if (!$type || $type eq 'ipset') {
141 foreach my $name (keys %{$conf->{ipset
}}) {
148 if (my $comment = $conf->{ipset_comments
}->{$name}) {
149 $data->{comment
} = $comment;
155 if (!$type || $type eq 'alias') {
156 foreach my $name (keys %{$conf->{aliases
}}) {
157 my $e = $conf->{aliases
}->{$name};
164 $data->{comment
} = $e->{comment
} if $e->{comment
};