]> git.proxmox.com Git - pve-firewall.git/blob - src/PVE/API2/Firewall/Host.pm
complete options API for host.fw
[pve-firewall.git] / src / PVE / API2 / Firewall / Host.pm
1 package PVE::API2::Firewall::Host;
2
3 use strict;
4 use warnings;
5 use PVE::JSONSchema qw(get_standard_option);
6 use PVE::RPCEnvironment;
7
8 use PVE::Firewall;
9 use PVE::API2::Firewall::Rules;
10
11 use Data::Dumper; # fixme: remove
12
13 use base qw(PVE::RESTHandler);
14
15 __PACKAGE__->register_method ({
16 subclass => "PVE::API2::Firewall::HostRules",
17 path => 'rules',
18 });
19
20 __PACKAGE__->register_method({
21 name => 'index',
22 path => '',
23 method => 'GET',
24 permissions => { user => 'all' },
25 description => "Directory index.",
26 parameters => {
27 additionalProperties => 0,
28 properties => {
29 node => get_standard_option('pve-node'),
30 },
31 },
32 returns => {
33 type => 'array',
34 items => {
35 type => "object",
36 properties => {},
37 },
38 links => [ { rel => 'child', href => "{name}" } ],
39 },
40 code => sub {
41 my ($param) = @_;
42
43 my $result = [
44 { name => 'rules' },
45 { name => 'options' },
46 { name => 'log' },
47 ];
48
49 return $result;
50 }});
51
52 my $option_properties = {
53 enable => {
54 description => "Enable host firewall rules.",
55 type => 'boolean',
56 optional => 1,
57 },
58 log_level_in => get_standard_option('pve-fw-loglevel', {
59 description => "Log level for incoming traffic." }),
60 log_level_out => get_standard_option('pve-fw-loglevel', {
61 description => "Log level for outgoing traffic." }),
62 tcp_flags_log_level => get_standard_option('pve-fw-loglevel', {
63 description => "Log level for illegal tcp flags filter." }),
64 smurf_log_level => get_standard_option('pve-fw-loglevel', {
65 description => "Log level for SMURFS filter." }),
66 nosmurfs => {
67 description => "Enable SMURFS filter.",
68 type => 'boolean',
69 optional => 1,
70 },
71 tcpflags => {
72 description => "Filter illegal combinations of TCP flags.",
73 type => 'boolean',
74 optional => 1,
75 },
76 allow_bridge_route => {
77 description => "Enable firewall when bridges contains IP address. The firewall is not fully functional in that case, so you need to enable that explicitly",
78 type => 'boolean',
79 optional => 1,
80 },
81 optimize => {
82 description => "Allow rules processing speed optimizations.",
83 type => 'boolean',
84 optional => 1,
85 },
86 nf_conntrack_max => {
87 description => "Maximum number of tracked connections.",
88 type => 'integer',
89 optional => 1,
90 minimum => 32768,
91 },
92 nf_conntrack_tcp_timeout_established => {
93 description => "Conntrack established timeout.",
94 type => 'integer',
95 optional => 1,
96 minimum => 7875,
97 }
98 };
99
100 my $add_option_properties = sub {
101 my ($properties) = @_;
102
103 foreach my $k (keys %$option_properties) {
104 $properties->{$k} = $option_properties->{$k};
105 }
106
107 return $properties;
108 };
109
110
111 __PACKAGE__->register_method({
112 name => 'get_options',
113 path => 'options',
114 method => 'GET',
115 description => "Get host firewall options.",
116 proxyto => 'node',
117 parameters => {
118 additionalProperties => 0,
119 properties => {
120 node => get_standard_option('pve-node'),
121 },
122 },
123 returns => {
124 type => "object",
125 #additionalProperties => 1,
126 properties => $option_properties,
127 },
128 code => sub {
129 my ($param) = @_;
130
131 my $hostfw_conf = PVE::Firewall::load_hostfw_conf();
132
133 return PVE::Firewall::copy_opject_with_digest($hostfw_conf->{options});
134 }});
135
136 __PACKAGE__->register_method({
137 name => 'set_options',
138 path => 'options',
139 method => 'PUT',
140 description => "Set Firewall options.",
141 protected => 1,
142 proxyto => 'node',
143 parameters => {
144 additionalProperties => 0,
145 properties => &$add_option_properties({
146 node => get_standard_option('pve-node'),
147 delete => {
148 type => 'string', format => 'pve-configid-list',
149 description => "A list of settings you want to delete.",
150 optional => 1,
151 },
152 digest => get_standard_option('pve-config-digest'),
153 }),
154 },
155 returns => { type => "null" },
156 code => sub {
157 my ($param) = @_;
158
159 my $hostfw_conf = PVE::Firewall::load_hostfw_conf();
160
161 my (undef, $digest) = PVE::Firewall::copy_opject_with_digest($hostfw_conf->{options});
162 PVE::Tools::assert_if_modified($digest, $param->{digest});
163
164 if ($param->{delete}) {
165 foreach my $opt (PVE::Tools::split_list($param->{delete})) {
166 raise_param_exc({ delete => "no such option '$opt'" })
167 if !$option_properties->{$opt};
168 delete $hostfw_conf->{options}->{$opt};
169 }
170 }
171
172 if (defined($param->{enable})) {
173 $param->{enable} = $param->{enable} ? 1 : 0;
174 }
175
176 foreach my $k (keys %$option_properties) {
177 next if !defined($param->{$k});
178 $hostfw_conf->{options}->{$k} = $param->{$k};
179 }
180
181 PVE::Firewall::save_hostfw_conf($hostfw_conf);
182
183 return undef;
184 }});
185
186 __PACKAGE__->register_method({
187 name => 'log',
188 path => 'log',
189 method => 'GET',
190 description => "Read firewall log",
191 proxyto => 'node',
192 permissions => {
193 check => ['perm', '/nodes/{node}', [ 'Sys.Syslog' ]],
194 },
195 protected => 1,
196 parameters => {
197 additionalProperties => 0,
198 properties => {
199 node => get_standard_option('pve-node'),
200 start => {
201 type => 'integer',
202 minimum => 0,
203 optional => 1,
204 },
205 limit => {
206 type => 'integer',
207 minimum => 0,
208 optional => 1,
209 },
210 },
211 },
212 returns => {
213 type => 'array',
214 items => {
215 type => "object",
216 properties => {
217 n => {
218 description=> "Line number",
219 type=> 'integer',
220 },
221 t => {
222 description=> "Line text",
223 type => 'string',
224 }
225 }
226 }
227 },
228 code => sub {
229 my ($param) = @_;
230
231 my $rpcenv = PVE::RPCEnvironment::get();
232 my $user = $rpcenv->get_user();
233 my $node = $param->{node};
234
235 my ($count, $lines) = PVE::Tools::dump_logfile("/var/log/pve-firewall.log", $param->{start}, $param->{limit});
236
237 $rpcenv->set_result_attrib('total', $count);
238
239 return $lines;
240 }});
241
242 1;