]>
git.proxmox.com Git - pve-firewall.git/blob - src/PVE/API2/Firewall/VM.pm
3e49e8887c93aa6838ee6ec296d41ca31114fac2
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
=> "Input policy.",
35 enum
=> ['ACCEPT', 'REJECT', 'DROP'],
38 description
=> "Output policy.",
41 enum
=> ['ACCEPT', 'REJECT', 'DROP'],
43 log_level_in
=> get_standard_option
('pve-fw-loglevel', {
44 description
=> "Log level for incoming traffic." }),
45 log_level_out
=> get_standard_option
('pve-fw-loglevel', {
46 description
=> "Log level for outgoing traffic." }),
50 my $add_option_properties = sub {
51 my ($properties) = @_;
53 foreach my $k (keys %$option_properties) {
54 $properties->{$k} = $option_properties->{$k};
60 sub register_handlers
{
61 my ($class, $rule_env) = @_;
63 $class->register_method({
67 permissions
=> { user
=> 'all' },
68 description
=> "Directory index.",
70 additionalProperties
=> 0,
72 node
=> get_standard_option
('pve-node'),
73 vmid
=> get_standard_option
('pve-vmid'),
82 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
89 { name
=> 'aliases' },
92 { name
=> 'options' },
99 $class->register_method({
100 name
=> 'get_options',
103 description
=> "Get VM firewall options.",
106 additionalProperties
=> 0,
108 node
=> get_standard_option
('pve-node'),
109 vmid
=> get_standard_option
('pve-vmid'),
114 #additionalProperties => 1,
115 properties
=> $option_properties,
120 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
121 my $vmfw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
123 return PVE
::Firewall
::copy_opject_with_digest
($vmfw_conf->{options
});
126 $class->register_method({
127 name
=> 'set_options',
130 description
=> "Set Firewall options.",
134 additionalProperties
=> 0,
135 properties
=> &$add_option_properties({
136 node
=> get_standard_option
('pve-node'),
137 vmid
=> get_standard_option
('pve-vmid'),
139 type
=> 'string', format
=> 'pve-configid-list',
140 description
=> "A list of settings you want to delete.",
143 digest
=> get_standard_option
('pve-config-digest'),
146 returns
=> { type
=> "null" },
151 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
152 my $vmfw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
154 my (undef, $digest) = PVE
::Firewall
::copy_opject_with_digest
($vmfw_conf->{options
});
155 PVE
::Tools
::assert_if_modified
($digest, $param->{digest
});
157 if ($param->{delete}) {
158 foreach my $opt (PVE
::Tools
::split_list
($param->{delete})) {
159 raise_param_exc
({ delete => "no such option '$opt'" })
160 if !$option_properties->{$opt};
161 delete $vmfw_conf->{options
}->{$opt};
165 if (defined($param->{enable
})) {
166 $param->{enable
} = $param->{enable
} ?
1 : 0;
169 foreach my $k (keys %$option_properties) {
170 next if !defined($param->{$k});
171 $vmfw_conf->{options
}->{$k} = $param->{$k};
174 PVE
::Firewall
::save_vmfw_conf
($param->{vmid
}, $vmfw_conf);
179 $class->register_method({
183 description
=> "Read firewall log",
186 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
190 additionalProperties
=> 0,
192 node
=> get_standard_option
('pve-node'),
193 vmid
=> get_standard_option
('pve-vmid'),
212 description
=> "Line number",
216 description
=> "Line text",
225 my $rpcenv = PVE
::RPCEnvironment
::get
();
226 my $user = $rpcenv->get_user();
227 my $vmid = $param->{vmid
};
229 my ($count, $lines) = PVE
::Tools
::dump_logfile
("/var/log/pve-firewall.log",
230 $param->{start
}, $param->{limit
},
233 $rpcenv->set_result_attrib('total', $count);
239 $class->register_method({
243 description
=> "Lists possible IPSet/Alias reference which are allowed in source/dest properties.",
245 additionalProperties
=> 0,
247 node
=> get_standard_option
('pve-node'),
248 vmid
=> get_standard_option
('pve-vmid'),
258 enum
=> ['alias', 'ipset'],
273 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
274 my $fw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
279 foreach my $conf (($cluster_conf, $fw_conf)) {
281 foreach my $name (keys %{$conf->{ipset
}}) {
287 if (my $comment = $conf->{ipset_comments
}->{$name}) {
288 $data->{comment
} = $comment;
290 $ipsets->{$name} = $data;
293 foreach my $name (keys %{$conf->{aliases
}}) {
294 my $e = $conf->{aliases
}->{$name};
300 $data->{comment
} = $e->{comment
} if $e->{comment
};
301 $aliases->{$name} = $data;
306 foreach my $e (values %$ipsets) { push @$res, $e; };
307 foreach my $e (values %$aliases) { push @$res, $e; };
313 package PVE
::API2
::Firewall
::VM
;
318 use base
qw(PVE::API2::Firewall::VMBase);
320 __PACKAGE__-
>register_method ({
321 subclass
=> "PVE::API2::Firewall::VMRules",
325 __PACKAGE__-
>register_method ({
326 subclass
=> "PVE::API2::Firewall::VMAliases",
330 __PACKAGE__-
>register_method ({
331 subclass
=> "PVE::API2::Firewall::VMIPSetList",
335 __PACKAGE__-
>register_handlers('vm');
337 package PVE
::API2
::Firewall
::CT
;
342 use base
qw(PVE::API2::Firewall::VMBase);
344 __PACKAGE__-
>register_method ({
345 subclass
=> "PVE::API2::Firewall::CTRules",
349 __PACKAGE__-
>register_method ({
350 subclass
=> "PVE::API2::Firewall::CTAliases",
354 __PACKAGE__-
>register_method ({
355 subclass
=> "PVE::API2::Firewall::CTIPSetList",
359 __PACKAGE__-
>register_handlers('vm');