]> git.proxmox.com Git - pve-firewall.git/blame - src/PVE/API2/Firewall/Host.pm
bump version to 5.0.4
[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
6198a78f 121 PVE::Firewall::lock_hostfw_conf(undef, 10, sub {
a38849e6
FG
122 my $cluster_conf = PVE::Firewall::load_clusterfw_conf();
123 my $hostfw_conf = PVE::Firewall::load_hostfw_conf($cluster_conf);
124
125 my (undef, $digest) = PVE::Firewall::copy_opject_with_digest($hostfw_conf->{options});
126 PVE::Tools::assert_if_modified($digest, $param->{digest});
127
128 if ($param->{delete}) {
129 foreach my $opt (PVE::Tools::split_list($param->{delete})) {
130 raise_param_exc({ delete => "no such option '$opt'" })
131 if !$option_properties->{$opt};
132 delete $hostfw_conf->{options}->{$opt};
133 }
6302c41f 134 }
6302c41f 135
a38849e6
FG
136 if (defined($param->{enable})) {
137 $param->{enable} = $param->{enable} ? 1 : 0;
138 }
6302c41f 139
a38849e6
FG
140 foreach my $k (keys %$option_properties) {
141 next if !defined($param->{$k});
142 $hostfw_conf->{options}->{$k} = $param->{$k};
143 }
6302c41f 144
a38849e6
FG
145 PVE::Firewall::save_hostfw_conf($hostfw_conf);
146 });
6302c41f
DM
147
148 return undef;
149 }});
150
a959126d 151__PACKAGE__->register_method({
75a12a9d
TL
152 name => 'log',
153 path => 'log',
a959126d
DM
154 method => 'GET',
155 description => "Read firewall log",
156 proxyto => 'node',
157 permissions => {
158 check => ['perm', '/nodes/{node}', [ 'Sys.Syslog' ]],
159 },
160 protected => 1,
161 parameters => {
162 additionalProperties => 0,
163 properties => {
164 node => get_standard_option('pve-node'),
165 start => {
166 type => 'integer',
167 minimum => 0,
168 optional => 1,
169 },
170 limit => {
171 type => 'integer',
172 minimum => 0,
173 optional => 1,
174 },
8bd9b3e4
CE
175 since => {
176 type => 'integer',
177 minimum => 0,
178 description => "Display log since this UNIX epoch.",
179 optional => 1,
180 },
181 until => {
182 type => 'integer',
183 minimum => 0,
184 description => "Display log until this UNIX epoch.",
185 optional => 1,
186 },
a959126d
DM
187 },
188 },
189 returns => {
190 type => 'array',
75a12a9d 191 items => {
a959126d
DM
192 type => "object",
193 properties => {
194 n => {
195 description=> "Line number",
196 type=> 'integer',
197 },
198 t => {
199 description=> "Line text",
200 type => 'string',
201 }
202 }
203 }
204 },
205 code => sub {
206 my ($param) = @_;
207
208 my $rpcenv = PVE::RPCEnvironment::get();
209 my $user = $rpcenv->get_user();
210 my $node = $param->{node};
8bd9b3e4 211 my $filename = "/var/log/pve-firewall.log";
a959126d 212
8bd9b3e4
CE
213 my ($count, $lines) = PVE::Firewall::Helpers::dump_fw_logfile(
214 $filename, $param, undef);
a959126d
DM
215
216 $rpcenv->set_result_attrib('total', $count);
75a12a9d
TL
217
218 return $lines;
a959126d
DM
219 }});
220
8b27beb9 2211;