]>
git.proxmox.com Git - pve-firewall.git/blob - src/PVE/API2/Firewall/VM.pm
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
=> "Enable default IP filters. " .
43 "This is equivalent to adding an empty ipfilter-net<id> ipset " .
44 "for every interface. Such ipsets implicitly contain sane default " .
45 "restrictions such as restricting IPv6 link local addresses to " .
46 "the one derived from the interface's MAC address.",
51 description
=> "Input policy.",
54 enum
=> ['ACCEPT', 'REJECT', 'DROP'],
57 description
=> "Output policy.",
60 enum
=> ['ACCEPT', 'REJECT', 'DROP'],
62 log_level_in
=> get_standard_option
('pve-fw-loglevel', {
63 description
=> "Log level for incoming traffic." }),
64 log_level_out
=> get_standard_option
('pve-fw-loglevel', {
65 description
=> "Log level for outgoing traffic." }),
69 my $add_option_properties = sub {
70 my ($properties) = @_;
72 foreach my $k (keys %$option_properties) {
73 $properties->{$k} = $option_properties->{$k};
79 sub register_handlers
{
80 my ($class, $rule_env) = @_;
82 $class->register_method({
86 permissions
=> { user
=> 'all' },
87 description
=> "Directory index.",
89 additionalProperties
=> 0,
91 node
=> get_standard_option
('pve-node'),
92 vmid
=> get_standard_option
('pve-vmid'),
101 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
108 { name
=> 'aliases' },
111 { name
=> 'options' },
118 $class->register_method({
119 name
=> 'get_options',
122 description
=> "Get VM firewall options.",
125 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
128 additionalProperties
=> 0,
130 node
=> get_standard_option
('pve-node'),
131 vmid
=> get_standard_option
('pve-vmid'),
136 #additionalProperties => 1,
137 properties
=> $option_properties,
142 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
143 my $vmfw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
145 return PVE
::Firewall
::copy_opject_with_digest
($vmfw_conf->{options
});
148 $class->register_method({
149 name
=> 'set_options',
152 description
=> "Set Firewall options.",
156 check
=> ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]],
159 additionalProperties
=> 0,
160 properties
=> &$add_option_properties({
161 node
=> get_standard_option
('pve-node'),
162 vmid
=> get_standard_option
('pve-vmid'),
164 type
=> 'string', format
=> 'pve-configid-list',
165 description
=> "A list of settings you want to delete.",
168 digest
=> get_standard_option
('pve-config-digest'),
171 returns
=> { type
=> "null" },
176 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
177 my $vmfw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
179 my (undef, $digest) = PVE
::Firewall
::copy_opject_with_digest
($vmfw_conf->{options
});
180 PVE
::Tools
::assert_if_modified
($digest, $param->{digest
});
182 if ($param->{delete}) {
183 foreach my $opt (PVE
::Tools
::split_list
($param->{delete})) {
184 raise_param_exc
({ delete => "no such option '$opt'" })
185 if !$option_properties->{$opt};
186 delete $vmfw_conf->{options
}->{$opt};
190 if (defined($param->{enable
})) {
191 $param->{enable
} = $param->{enable
} ?
1 : 0;
194 foreach my $k (keys %$option_properties) {
195 next if !defined($param->{$k});
196 $vmfw_conf->{options
}->{$k} = $param->{$k};
199 PVE
::Firewall
::save_vmfw_conf
($param->{vmid
}, $vmfw_conf);
204 $class->register_method({
208 description
=> "Read firewall log",
211 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
215 additionalProperties
=> 0,
217 node
=> get_standard_option
('pve-node'),
218 vmid
=> get_standard_option
('pve-vmid'),
237 description
=> "Line number",
241 description
=> "Line text",
250 my $rpcenv = PVE
::RPCEnvironment
::get
();
251 my $user = $rpcenv->get_user();
252 my $vmid = $param->{vmid
};
254 my ($count, $lines) = PVE
::Tools
::dump_logfile
("/var/log/pve-firewall.log",
255 $param->{start
}, $param->{limit
},
258 $rpcenv->set_result_attrib('total', $count);
264 $class->register_method({
268 description
=> "Lists possible IPSet/Alias reference which are allowed in source/dest properties.",
270 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
273 additionalProperties
=> 0,
275 node
=> get_standard_option
('pve-node'),
276 vmid
=> get_standard_option
('pve-vmid'),
278 description
=> "Only list references of specified type.",
280 enum
=> ['alias', 'ipset'],
292 enum
=> ['alias', 'ipset'],
307 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
308 my $fw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
313 foreach my $conf (($cluster_conf, $fw_conf)) {
315 if (!$param->{type
} || $param->{type
} eq 'ipset') {
316 foreach my $name (keys %{$conf->{ipset
}}) {
322 if (my $comment = $conf->{ipset_comments
}->{$name}) {
323 $data->{comment
} = $comment;
325 $ipsets->{$name} = $data;
329 if (!$param->{type
} || $param->{type
} eq 'alias') {
330 foreach my $name (keys %{$conf->{aliases
}}) {
331 my $e = $conf->{aliases
}->{$name};
337 $data->{comment
} = $e->{comment
} if $e->{comment
};
338 $aliases->{$name} = $data;
344 foreach my $e (values %$ipsets) { push @$res, $e; };
345 foreach my $e (values %$aliases) { push @$res, $e; };
351 package PVE
::API2
::Firewall
::VM
;
356 use base
qw(PVE::API2::Firewall::VMBase);
358 __PACKAGE__-
>register_method ({
359 subclass
=> "PVE::API2::Firewall::VMRules",
363 __PACKAGE__-
>register_method ({
364 subclass
=> "PVE::API2::Firewall::VMAliases",
368 __PACKAGE__-
>register_method ({
369 subclass
=> "PVE::API2::Firewall::VMIPSetList",
373 __PACKAGE__-
>register_handlers('vm');
375 package PVE
::API2
::Firewall
::CT
;
380 use base
qw(PVE::API2::Firewall::VMBase);
382 __PACKAGE__-
>register_method ({
383 subclass
=> "PVE::API2::Firewall::CTRules",
387 __PACKAGE__-
>register_method ({
388 subclass
=> "PVE::API2::Firewall::CTAliases",
392 __PACKAGE__-
>register_method ({
393 subclass
=> "PVE::API2::Firewall::CTIPSetList",
397 __PACKAGE__-
>register_handlers('vm');