]>
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
=> "Input policy.",
40 enum
=> ['ACCEPT', 'REJECT', 'DROP'],
43 description
=> "Output policy.",
46 enum
=> ['ACCEPT', 'REJECT', 'DROP'],
48 log_level_in
=> get_standard_option
('pve-fw-loglevel', {
49 description
=> "Log level for incoming traffic." }),
50 log_level_out
=> get_standard_option
('pve-fw-loglevel', {
51 description
=> "Log level for outgoing traffic." }),
55 my $add_option_properties = sub {
56 my ($properties) = @_;
58 foreach my $k (keys %$option_properties) {
59 $properties->{$k} = $option_properties->{$k};
65 sub register_handlers
{
66 my ($class, $rule_env) = @_;
68 $class->register_method({
72 permissions
=> { user
=> 'all' },
73 description
=> "Directory index.",
75 additionalProperties
=> 0,
77 node
=> get_standard_option
('pve-node'),
78 vmid
=> get_standard_option
('pve-vmid'),
87 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
94 { name
=> 'aliases' },
97 { name
=> 'options' },
104 $class->register_method({
105 name
=> 'get_options',
108 description
=> "Get VM firewall options.",
111 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
114 additionalProperties
=> 0,
116 node
=> get_standard_option
('pve-node'),
117 vmid
=> get_standard_option
('pve-vmid'),
122 #additionalProperties => 1,
123 properties
=> $option_properties,
128 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
129 my $vmfw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
131 return PVE
::Firewall
::copy_opject_with_digest
($vmfw_conf->{options
});
134 $class->register_method({
135 name
=> 'set_options',
138 description
=> "Set Firewall options.",
142 check
=> ['perm', '/vms/{vmid}', [ 'VM.Config.Network' ]],
145 additionalProperties
=> 0,
146 properties
=> &$add_option_properties({
147 node
=> get_standard_option
('pve-node'),
148 vmid
=> get_standard_option
('pve-vmid'),
150 type
=> 'string', format
=> 'pve-configid-list',
151 description
=> "A list of settings you want to delete.",
154 digest
=> get_standard_option
('pve-config-digest'),
157 returns
=> { type
=> "null" },
162 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
163 my $vmfw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
165 my (undef, $digest) = PVE
::Firewall
::copy_opject_with_digest
($vmfw_conf->{options
});
166 PVE
::Tools
::assert_if_modified
($digest, $param->{digest
});
168 if ($param->{delete}) {
169 foreach my $opt (PVE
::Tools
::split_list
($param->{delete})) {
170 raise_param_exc
({ delete => "no such option '$opt'" })
171 if !$option_properties->{$opt};
172 delete $vmfw_conf->{options
}->{$opt};
176 if (defined($param->{enable
})) {
177 $param->{enable
} = $param->{enable
} ?
1 : 0;
180 foreach my $k (keys %$option_properties) {
181 next if !defined($param->{$k});
182 $vmfw_conf->{options
}->{$k} = $param->{$k};
185 PVE
::Firewall
::save_vmfw_conf
($param->{vmid
}, $vmfw_conf);
190 $class->register_method({
194 description
=> "Read firewall log",
197 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
201 additionalProperties
=> 0,
203 node
=> get_standard_option
('pve-node'),
204 vmid
=> get_standard_option
('pve-vmid'),
223 description
=> "Line number",
227 description
=> "Line text",
236 my $rpcenv = PVE
::RPCEnvironment
::get
();
237 my $user = $rpcenv->get_user();
238 my $vmid = $param->{vmid
};
240 my ($count, $lines) = PVE
::Tools
::dump_logfile
("/var/log/pve-firewall.log",
241 $param->{start
}, $param->{limit
},
244 $rpcenv->set_result_attrib('total', $count);
250 $class->register_method({
254 description
=> "Lists possible IPSet/Alias reference which are allowed in source/dest properties.",
256 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
259 additionalProperties
=> 0,
261 node
=> get_standard_option
('pve-node'),
262 vmid
=> get_standard_option
('pve-vmid'),
264 description
=> "Only list references of specified type.",
266 enum
=> ['alias', 'ipset'],
278 enum
=> ['alias', 'ipset'],
293 my $cluster_conf = PVE
::Firewall
::load_clusterfw_conf
();
294 my $fw_conf = PVE
::Firewall
::load_vmfw_conf
($cluster_conf, $rule_env, $param->{vmid
});
299 foreach my $conf (($cluster_conf, $fw_conf)) {
301 if (!$param->{type
} || $param->{type
} eq 'ipset') {
302 foreach my $name (keys %{$conf->{ipset
}}) {
308 if (my $comment = $conf->{ipset_comments
}->{$name}) {
309 $data->{comment
} = $comment;
311 $ipsets->{$name} = $data;
315 if (!$param->{type
} || $param->{type
} eq 'alias') {
316 foreach my $name (keys %{$conf->{aliases
}}) {
317 my $e = $conf->{aliases
}->{$name};
323 $data->{comment
} = $e->{comment
} if $e->{comment
};
324 $aliases->{$name} = $data;
330 foreach my $e (values %$ipsets) { push @$res, $e; };
331 foreach my $e (values %$aliases) { push @$res, $e; };
337 package PVE
::API2
::Firewall
::VM
;
342 use base
qw(PVE::API2::Firewall::VMBase);
344 __PACKAGE__-
>register_method ({
345 subclass
=> "PVE::API2::Firewall::VMRules",
349 __PACKAGE__-
>register_method ({
350 subclass
=> "PVE::API2::Firewall::VMAliases",
354 __PACKAGE__-
>register_method ({
355 subclass
=> "PVE::API2::Firewall::VMIPSetList",
359 __PACKAGE__-
>register_handlers('vm');
361 package PVE
::API2
::Firewall
::CT
;
366 use base
qw(PVE::API2::Firewall::VMBase);
368 __PACKAGE__-
>register_method ({
369 subclass
=> "PVE::API2::Firewall::CTRules",
373 __PACKAGE__-
>register_method ({
374 subclass
=> "PVE::API2::Firewall::CTAliases",
378 __PACKAGE__-
>register_method ({
379 subclass
=> "PVE::API2::Firewall::CTIPSetList",
383 __PACKAGE__-
>register_handlers('vm');