]> git.proxmox.com Git - pve-firewall.git/blame - pvefw
compile: use verbose output when started from CLI
[pve-firewall.git] / pvefw
CommitLineData
b6360c3f
DM
1#!/usr/bin/perl -w
2
3use strict;
4use lib qw(.);
5use PVE::Firewall;
dddd9413 6
80bfe1ff
DM
7use PVE::SafeSyslog;
8use PVE::Cluster;
9use PVE::INotify;
10use PVE::RPCEnvironment;
b6360c3f 11
80bfe1ff
DM
12use PVE::JSONSchema qw(get_standard_option);
13
14use PVE::CLIHandler;
15
16use base qw(PVE::CLIHandler);
17
18$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
19
20initlog ('pvefw');
21
22die "please run as root\n" if $> != 0;
23
24PVE::INotify::inotify_init();
25
26my $rpcenv = PVE::RPCEnvironment->init('cli');
27
28$rpcenv->init_request();
29$rpcenv->set_language($ENV{LANG});
30$rpcenv->set_user('root@pam');
b6360c3f 31
80bfe1ff
DM
32__PACKAGE__->register_method ({
33 name => 'compile',
34 path => 'compile',
35 method => 'POST',
3fa83edf 36 description => "Compile amd print firewall rules. This is only for testing.",
80bfe1ff
DM
37 parameters => {
38 additionalProperties => 0,
3fa83edf
DM
39 properties => {
40 verbose => {
41 description => "Verbose output.",
42 type => "boolean",
43 optional => 1,
3fa83edf
DM
44 },
45 },
80bfe1ff
DM
46 },
47 returns => { type => 'null' },
48
49 code => sub {
50 my ($param) = @_;
51
e0809a95
DM
52 my $rpcenv = PVE::RPCEnvironment::get();
53
54 $param->{verbose} = 1
55 if !defined($param->{verbose}) && ($rpcenv->{type} eq 'cli');
56
06320eb0 57 my $code = sub {
3fa83edf
DM
58 my $ruleset = PVE::Firewall::compile();
59 PVE::Firewall::get_ruleset_status($ruleset, 1) if $param->{verbose};
06320eb0
DM
60 };
61
62 PVE::Firewall::run_locked($code);
f789653a 63
5e1267a5
DM
64 return undef;
65 }});
80bfe1ff 66
5e1267a5
DM
67__PACKAGE__->register_method ({
68 name => 'start',
69 path => 'start',
70 method => 'POST',
a332200b 71 description => "Start (or restart if already active) firewall.",
5e1267a5
DM
72 parameters => {
73 additionalProperties => 0,
3fa83edf
DM
74 properties => {
75 verbose => {
76 description => "Verbose output.",
77 type => "boolean",
78 optional => 1,
79 default => 0,
80 },
81 },
5e1267a5
DM
82 },
83 returns => { type => 'null' },
80bfe1ff 84
5e1267a5
DM
85 code => sub {
86 my ($param) = @_;
80bfe1ff 87
06320eb0 88 my $code = sub {
a84f4d96
DM
89 my $ruleset = PVE::Firewall::compile();
90 PVE::Firewall::apply_ruleset($ruleset, $param->{verbose});
06320eb0
DM
91 };
92
93 PVE::Firewall::run_locked($code);
80bfe1ff
DM
94
95 return undef;
80bfe1ff
DM
96 }});
97
80bfe1ff
DM
98__PACKAGE__->register_method ({
99 name => 'stop',
100 path => 'stop',
101 method => 'POST',
a332200b 102 description => "Stop firewall. This will remove all rules installed by this script. The host is then unprotected.",
80bfe1ff
DM
103 parameters => {
104 additionalProperties => 0,
105 properties => {},
106 },
107 returns => { type => 'null' },
108
109 code => sub {
110 my ($param) = @_;
111
06320eb0 112 my $code = sub {
b16e818e
DM
113 my $chash = PVE::Firewall::iptables_get_chains();
114 my $cmdlist = "*filter\n";
dec84fcd 115 my $rule = "INPUT -j PVEFW-INPUT";
3fa83edf
DM
116 if (PVE::Firewall::iptables_rule_exist($rule)) {
117 $cmdlist .= "-D $rule\n";
118 }
dec84fcd 119 $rule = "OUTPUT -j PVEFW-OUTPUT";
3fa83edf
DM
120 if (PVE::Firewall::iptables_rule_exist($rule)) {
121 $cmdlist .= "-D $rule\n";
122 }
123
dec84fcd 124 $rule = "FORWARD -j PVEFW-FORWARD";
3fa83edf
DM
125 if (PVE::Firewall::iptables_rule_exist($rule)) {
126 $cmdlist .= "-D $rule\n";
127 }
128
b16e818e
DM
129 foreach my $chain (keys %$chash) {
130 $cmdlist .= "-F $chain\n";
131 }
132 foreach my $chain (keys %$chash) {
133 $cmdlist .= "-X $chain\n";
134 }
135 $cmdlist .= "COMMIT\n";
136
137 PVE::Firewall::iptables_restore_cmdlist($cmdlist);
06320eb0
DM
138 };
139
140 PVE::Firewall::run_locked($code);
80bfe1ff
DM
141
142 return undef;
143 }});
144
145my $nodename = PVE::INotify::nodename();
146
147my $cmddef = {
148 compile => [ __PACKAGE__, 'compile', []],
149 start => [ __PACKAGE__, 'start', []],
150 stop => [ __PACKAGE__, 'stop', []],
80bfe1ff
DM
151};
152
153my $cmd = shift;
154
155PVE::CLIHandler::handle_cmd($cmddef, "pvefw", $cmd, \@ARGV, undef, $0);
b6360c3f
DM
156
157exit(0);
80bfe1ff 158