]>
git.proxmox.com Git - pve-firewall.git/blob - src/PVE/API2/Firewall/VM.pm
422210399bf43454fa6aa21d22b13135afb6e4ba
1 package PVE
::API2
::Firewall
::VMBase
;
6 use PVE
::Exception
qw(raise_param_exc);
7 use PVE
::JSONSchema
qw(get_standard_option);
10 use PVE
::API2
::Firewall
::Rules
;
11 use PVE
::API2
::Firewall
::Aliases
;
14 use base
qw(PVE::RESTHandler);
16 my $option_properties = $PVE::Firewall
::vm_option_properties
;
18 my $add_option_properties = sub {
19 my ($properties) = @_;
21 foreach my $k (keys %$option_properties) {
22 $properties->{$k} = $option_properties->{$k};
28 sub register_handlers
{
29 my ($class, $rule_env) = @_;
31 $class->register_method({
35 permissions
=> { user
=> 'all' },
36 description
=> "Directory index.",
38 additionalProperties
=> 0,
40 node
=> get_standard_option
('pve-node'),
41 vmid
=> get_standard_option
('pve-vmid'),
50 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
57 { name
=> 'aliases' },
60 { name
=> 'options' },
67 $class->register_method({
68 name
=> 'get_options',
71 description
=> "Get VM firewall options.",
74 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
77 additionalProperties
=> 0,
79 node
=> get_standard_option
('pve-node'),
80 vmid
=> get_standard_option
('pve-vmid'),
85 #additionalProperties => 1,
86 properties
=> $option_properties,
91 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
92 my $vmfw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
94 return PVE
::Firewall
::copy_opject_with_digest
($vmfw_conf->{options
});
97 $class->register_method({
98 name
=> 'set_options',
101 description
=> "Set Firewall options.",
105 check
=> ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]],
108 additionalProperties
=> 0,
109 properties
=> &$add_option_properties({
110 node
=> get_standard_option
('pve-node'),
111 vmid
=> get_standard_option
('pve-vmid'),
113 type
=> 'string', format
=> 'pve-configid-list',
114 description
=> "A list of settings you want to delete.",
117 digest
=> get_standard_option
('pve-config-digest'),
120 returns
=> { type
=> "null" },
124 PVE
::Firewall
::lock_vmfw_conf
($param->{vmid
}, 10, sub {
125 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
126 my $vmfw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
128 my (undef, $digest) = PVE
::Firewall
::copy_opject_with_digest
($vmfw_conf->{options
});
129 PVE
::Tools
::assert_if_modified
($digest, $param->{digest
});
131 if ($param->{delete}) {
132 foreach my $opt (PVE
::Tools
::split_list
($param->{delete})) {
133 raise_param_exc
({ delete => "no such option '$opt'" })
134 if !$option_properties->{$opt};
135 delete $vmfw_conf->{options
}->{$opt};
139 if (defined($param->{enable
})) {
140 $param->{enable
} = $param->{enable
} ?
1 : 0;
143 foreach my $k (keys %$option_properties) {
144 next if !defined($param->{$k});
145 $vmfw_conf->{options
}->{$k} = $param->{$k};
148 PVE
::Firewall
::save_vmfw_conf
($param->{vmid
}, $vmfw_conf);
154 $class->register_method({
158 description
=> "Read firewall log",
161 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
165 additionalProperties
=> 0,
167 node
=> get_standard_option
('pve-node'),
168 vmid
=> get_standard_option
('pve-vmid'),
182 description
=> "Display log since this UNIX epoch.",
188 description
=> "Display log until this UNIX epoch.",
199 description
=> "Line number",
203 description
=> "Line text",
212 my $rpcenv = PVE
::RPCEnvironment
::get
();
213 my $user = $rpcenv->get_user();
214 my $filename = "/var/log/pve-firewall.log";
215 my $vmid = $param->{'vmid'};
220 return $line =~ m/$reg/;
223 my ($count, $lines) = PVE
::Firewall
::Helpers
::dump_fw_logfile
(
224 $filename, $param, $callback);
226 $rpcenv->set_result_attrib('total', $count);
232 $class->register_method({
236 description
=> "Lists possible IPSet/Alias reference which are allowed in source/dest properties.",
238 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
241 additionalProperties
=> 0,
243 node
=> get_standard_option
('pve-node'),
244 vmid
=> get_standard_option
('pve-vmid'),
246 description
=> "Only list references of specified type.",
248 enum
=> ['alias', 'ipset'],
260 enum
=> ['alias', 'ipset'],
281 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
282 my $fw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
284 my $dc_refs = PVE
::Firewall
::Helpers
::collect_refs
($cluster_conf, $param->{type
}, 'dc');
285 my $vm_refs = PVE
::Firewall
::Helpers
::collect_refs
($fw_conf, $param->{type
}, 'guest');
287 return [@$dc_refs, @$vm_refs];
291 package PVE
::API2
::Firewall
::VM
;
296 use base
qw(PVE::API2::Firewall::VMBase);
298 __PACKAGE__-
>register_method ({
299 subclass
=> "PVE::API2::Firewall::VMRules",
303 __PACKAGE__-
>register_method ({
304 subclass
=> "PVE::API2::Firewall::VMAliases",
308 __PACKAGE__-
>register_method ({
309 subclass
=> "PVE::API2::Firewall::VMIPSetList",
313 __PACKAGE__-
>register_handlers('vm');
315 package PVE
::API2
::Firewall
::CT
;
320 use base
qw(PVE::API2::Firewall::VMBase);
322 __PACKAGE__-
>register_method ({
323 subclass
=> "PVE::API2::Firewall::CTRules",
327 __PACKAGE__-
>register_method ({
328 subclass
=> "PVE::API2::Firewall::CTAliases",
332 __PACKAGE__-
>register_method ({
333 subclass
=> "PVE::API2::Firewall::CTIPSetList",
337 __PACKAGE__-
>register_handlers('vm');