]> git.proxmox.com Git - pve-firewall.git/blame - src/PVE/API2/Firewall/Host.pm
api: add locking helpers
[pve-firewall.git] / src / PVE / API2 / Firewall / Host.pm
CommitLineData
8b27beb9
DM
1package PVE::API2::Firewall::Host;
2
3use strict;
4use warnings;
f6163c2e
TL
5
6use PVE::Exception qw(raise_param_exc);
8b27beb9 7use PVE::JSONSchema qw(get_standard_option);
a959126d 8use PVE::RPCEnvironment;
8b27beb9
DM
9
10use PVE::Firewall;
63c91681 11use PVE::API2::Firewall::Rules;
8b27beb9 12
8b27beb9
DM
13
14use base qw(PVE::RESTHandler);
15
63c91681 16__PACKAGE__->register_method ({
75a12a9d 17 subclass => "PVE::API2::Firewall::HostRules",
63c91681
DM
18 path => 'rules',
19});
20
8b27beb9
DM
21__PACKAGE__->register_method({
22 name => 'index',
23 path => '',
24 method => 'GET',
25 permissions => { user => 'all' },
26 description => "Directory index.",
27 parameters => {
28 additionalProperties => 0,
29 properties => {
30 node => get_standard_option('pve-node'),
31 },
32 },
33 returns => {
34 type => 'array',
35 items => {
36 type => "object",
37 properties => {},
38 },
39 links => [ { rel => 'child', href => "{name}" } ],
40 },
41 code => sub {
42 my ($param) = @_;
43
44 my $result = [
45 { name => 'rules' },
46 { name => 'options' },
a959126d 47 { name => 'log' },
8b27beb9
DM
48 ];
49
50 return $result;
51 }});
52
e313afe0 53my $option_properties = $PVE::Firewall::host_option_properties;
6302c41f
DM
54
55my $add_option_properties = sub {
56 my ($properties) = @_;
57
58 foreach my $k (keys %$option_properties) {
59 $properties->{$k} = $option_properties->{$k};
60 }
75a12a9d 61
6302c41f
DM
62 return $properties;
63};
64
65
8b27beb9
DM
66__PACKAGE__->register_method({
67 name => 'get_options',
68 path => 'options',
69 method => 'GET',
70 description => "Get host firewall options.",
71 proxyto => 'node',
60c103df
DM
72 permissions => {
73 check => ['perm', '/nodes/{node}', [ 'Sys.Audit' ]],
74 },
8b27beb9
DM
75 parameters => {
76 additionalProperties => 0,
77 properties => {
78 node => get_standard_option('pve-node'),
79 },
80 },
81 returns => {
82 type => "object",
6302c41f
DM
83 #additionalProperties => 1,
84 properties => $option_properties,
8b27beb9
DM
85 },
86 code => sub {
87 my ($param) = @_;
88
f78c7ca0
TL
89 my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
90 my $hostfw_conf = PVE::Firewall::load_hostfw_conf($cluster_conf);
8b27beb9 91
5d38d64f 92 return PVE::Firewall::copy_opject_with_digest($hostfw_conf->{options});
8b27beb9
DM
93 }});
94
6302c41f
DM
95__PACKAGE__->register_method({
96 name => 'set_options',
97 path => 'options',
98 method => 'PUT',
99 description => "Set Firewall options.",
100 protected => 1,
101 proxyto => 'node',
60c103df
DM
102 permissions => {
103 check => ['perm', '/nodes/{node}', [ 'Sys.Modify' ]],
104 },
6302c41f
DM
105 parameters => {
106 additionalProperties => 0,
107 properties => &$add_option_properties({
108 node => get_standard_option('pve-node'),
109 delete => {
110 type => 'string', format => 'pve-configid-list',
111 description => "A list of settings you want to delete.",
112 optional => 1,
113 },
114 digest => get_standard_option('pve-config-digest'),
115 }),
116 },
117 returns => { type => "null" },
118 code => sub {
119 my ($param) = @_;
120
f78c7ca0
TL
121 my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
122 my $hostfw_conf = PVE::Firewall::load_hostfw_conf($cluster_conf);
6302c41f
DM
123
124 my (undef, $digest) = PVE::Firewall::copy_opject_with_digest($hostfw_conf->{options});
125 PVE::Tools::assert_if_modified($digest, $param->{digest});
126
127 if ($param->{delete}) {
128 foreach my $opt (PVE::Tools::split_list($param->{delete})) {
75a12a9d 129 raise_param_exc({ delete => "no such option '$opt'" })
6302c41f
DM
130 if !$option_properties->{$opt};
131 delete $hostfw_conf->{options}->{$opt};
132 }
133 }
134
135 if (defined($param->{enable})) {
136 $param->{enable} = $param->{enable} ? 1 : 0;
137 }
138
139 foreach my $k (keys %$option_properties) {
140 next if !defined($param->{$k});
75a12a9d 141 $hostfw_conf->{options}->{$k} = $param->{$k};
6302c41f
DM
142 }
143
144 PVE::Firewall::save_hostfw_conf($hostfw_conf);
145
146 return undef;
147 }});
148
a959126d 149__PACKAGE__->register_method({
75a12a9d
TL
150 name => 'log',
151 path => 'log',
a959126d
DM
152 method => 'GET',
153 description => "Read firewall log",
154 proxyto => 'node',
155 permissions => {
156 check => ['perm', '/nodes/{node}', [ 'Sys.Syslog' ]],
157 },
158 protected => 1,
159 parameters => {
160 additionalProperties => 0,
161 properties => {
162 node => get_standard_option('pve-node'),
163 start => {
164 type => 'integer',
165 minimum => 0,
166 optional => 1,
167 },
168 limit => {
169 type => 'integer',
170 minimum => 0,
171 optional => 1,
172 },
173 },
174 },
175 returns => {
176 type => 'array',
75a12a9d 177 items => {
a959126d
DM
178 type => "object",
179 properties => {
180 n => {
181 description=> "Line number",
182 type=> 'integer',
183 },
184 t => {
185 description=> "Line text",
186 type => 'string',
187 }
188 }
189 }
190 },
191 code => sub {
192 my ($param) = @_;
193
194 my $rpcenv = PVE::RPCEnvironment::get();
195 my $user = $rpcenv->get_user();
196 my $node = $param->{node};
197
198 my ($count, $lines) = PVE::Tools::dump_logfile("/var/log/pve-firewall.log", $param->{start}, $param->{limit});
199
200 $rpcenv->set_result_attrib('total', $count);
75a12a9d
TL
201
202 return $lines;
a959126d
DM
203 }});
204
8b27beb9 2051;