1 package PVE
::API2
::Firewall
::VMBase
;
5 use PVE
::JSONSchema
qw(get_standard_option);
8 use PVE
::API2
::Firewall
::Rules
;
9 use PVE
::API2
::Firewall
::Aliases
;
11 use Data
::Dumper
; # fixme: remove
13 use base
qw(PVE::RESTHandler);
15 my $option_properties = {
17 description
=> "Enable host firewall rules.",
22 description
=> "Enable/disable MAC address filter.",
27 description
=> "Enable DHCP.",
32 description
=> "Enable NDP.",
37 description
=> "Allow sending Router Advertisement.",
42 description
=> "Input policy.",
45 enum
=> ['ACCEPT', 'REJECT', 'DROP'],
48 description
=> "Output policy.",
51 enum
=> ['ACCEPT', 'REJECT', 'DROP'],
53 log_level_in
=> get_standard_option
('pve-fw-loglevel', {
54 description
=> "Log level for incoming traffic." }),
55 log_level_out
=> get_standard_option
('pve-fw-loglevel', {
56 description
=> "Log level for outgoing traffic." }),
60 my $add_option_properties = sub {
61 my ($properties) = @_;
63 foreach my $k (keys %$option_properties) {
64 $properties->{$k} = $option_properties->{$k};
70 sub register_handlers
{
71 my ($class, $rule_env) = @_;
73 $class->register_method({
77 permissions
=> { user
=> 'all' },
78 description
=> "Directory index.",
80 additionalProperties
=> 0,
82 node
=> get_standard_option
('pve-node'),
83 vmid
=> get_standard_option
('pve-vmid'),
92 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
99 { name
=> 'aliases' },
102 { name
=> 'options' },
109 $class->register_method({
110 name
=> 'get_options',
113 description
=> "Get VM firewall options.",
116 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
119 additionalProperties
=> 0,
121 node
=> get_standard_option
('pve-node'),
122 vmid
=> get_standard_option
('pve-vmid'),
127 #additionalProperties => 1,
128 properties
=> $option_properties,
133 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
134 my $vmfw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
136 return PVE
::Firewall
::copy_opject_with_digest
($vmfw_conf->{options
});
139 $class->register_method({
140 name
=> 'set_options',
143 description
=> "Set Firewall options.",
147 check
=> ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]],
150 additionalProperties
=> 0,
151 properties
=> &$add_option_properties({
152 node
=> get_standard_option
('pve-node'),
153 vmid
=> get_standard_option
('pve-vmid'),
155 type
=> 'string', format
=> 'pve-configid-list',
156 description
=> "A list of settings you want to delete.",
159 digest
=> get_standard_option
('pve-config-digest'),
162 returns
=> { type
=> "null" },
167 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
168 my $vmfw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
170 my (undef, $digest) = PVE
::Firewall
::copy_opject_with_digest
($vmfw_conf->{options
});
171 PVE
::Tools
::assert_if_modified
($digest, $param->{digest
});
173 if ($param->{delete}) {
174 foreach my $opt (PVE
::Tools
::split_list
($param->{delete})) {
175 raise_param_exc
({ delete => "no such option '$opt'" })
176 if !$option_properties->{$opt};
177 delete $vmfw_conf->{options
}->{$opt};
181 if (defined($param->{enable
})) {
182 $param->{enable
} = $param->{enable
} ?
1 : 0;
185 foreach my $k (keys %$option_properties) {
186 next if !defined($param->{$k});
187 $vmfw_conf->{options
}->{$k} = $param->{$k};
190 PVE
::Firewall
::save_vmfw_conf
($param->{vmid
}, $vmfw_conf);
195 $class->register_method({
199 description
=> "Read firewall log",
202 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
206 additionalProperties
=> 0,
208 node
=> get_standard_option
('pve-node'),
209 vmid
=> get_standard_option
('pve-vmid'),
228 description
=> "Line number",
232 description
=> "Line text",
241 my $rpcenv = PVE
::RPCEnvironment
::get
();
242 my $user = $rpcenv->get_user();
243 my $vmid = $param->{vmid
};
245 my ($count, $lines) = PVE
::Tools
::dump_logfile
("/var/log/pve-firewall.log",
246 $param->{start
}, $param->{limit
},
249 $rpcenv->set_result_attrib('total', $count);
255 $class->register_method({
259 description
=> "Lists possible IPSet/Alias reference which are allowed in source/dest properties.",
261 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
264 additionalProperties
=> 0,
266 node
=> get_standard_option
('pve-node'),
267 vmid
=> get_standard_option
('pve-vmid'),
269 description
=> "Only list references of specified type.",
271 enum
=> ['alias', 'ipset'],
283 enum
=> ['alias', 'ipset'],
298 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
299 my $fw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
304 foreach my $conf (($cluster_conf, $fw_conf)) {
306 if (!$param->{type
} || $param->{type
} eq 'ipset') {
307 foreach my $name (keys %{$conf->{ipset
}}) {
313 if (my $comment = $conf->{ipset_comments
}->{$name}) {
314 $data->{comment
} = $comment;
316 $ipsets->{$name} = $data;
320 if (!$param->{type
} || $param->{type
} eq 'alias') {
321 foreach my $name (keys %{$conf->{aliases
}}) {
322 my $e = $conf->{aliases
}->{$name};
328 $data->{comment
} = $e->{comment
} if $e->{comment
};
329 $aliases->{$name} = $data;
335 foreach my $e (values %$ipsets) { push @$res, $e; };
336 foreach my $e (values %$aliases) { push @$res, $e; };
342 package PVE
::API2
::Firewall
::VM
;
347 use base
qw(PVE::API2::Firewall::VMBase);
349 __PACKAGE__-
>register_method ({
350 subclass
=> "PVE::API2::Firewall::VMRules",
354 __PACKAGE__-
>register_method ({
355 subclass
=> "PVE::API2::Firewall::VMAliases",
359 __PACKAGE__-
>register_method ({
360 subclass
=> "PVE::API2::Firewall::VMIPSetList",
364 __PACKAGE__-
>register_handlers('vm');
366 package PVE
::API2
::Firewall
::CT
;
371 use base
qw(PVE::API2::Firewall::VMBase);
373 __PACKAGE__-
>register_method ({
374 subclass
=> "PVE::API2::Firewall::CTRules",
378 __PACKAGE__-
>register_method ({
379 subclass
=> "PVE::API2::Firewall::CTAliases",
383 __PACKAGE__-
>register_method ({
384 subclass
=> "PVE::API2::Firewall::CTIPSetList",
388 __PACKAGE__-
>register_handlers('vm');