write real files
[pve-firewall.git] / PVE / Firewall.pm
1 package PVE::Firewall;
2
3 use warnings;
4 use strict;
5 use Data::Dumper;
6 use PVE::Tools;
7 use PVE::QemuServer;
8
9 # we need complete VM configuration of all VMs (openvz/qemu)
10 # in vmdata
11
12 sub compile {
13 my ($targetdir, $vmdata) = @_;
14
15 my $netinfo;
16
17 my $zoneinfo = {
18 fw => { type => 'firewall' },
19 };
20
21 my $register_bridge = sub {
22 my ($bridge) = @_;
23
24 my $zone = 'z' . $bridge;
25
26 return $zone if $zoneinfo->{$zone};
27
28 $zoneinfo->{$zone} = {
29 type => 'bridge',
30 bridge => $bridge,
31 };
32
33 return $zone;
34 };
35
36 foreach my $vmid (keys %{$vmdata->{qemu}}) {
37 $netinfo->{$vmid} = {};
38 my $conf = $vmdata->{qemu}->{$vmid};
39 foreach my $opt (keys %$conf) {
40 next if $opt !~ m/^net(\d+)$/;
41 my $netid = $1;
42 my $net = PVE::QemuServer::parse_net($conf->{$opt});
43 next if !$net;
44 die "implement me" if !$net->{bridge};
45 my $bridge = $net->{bridge};
46 my $bridge_zone = &$register_bridge($bridge);
47 if (defined($net->{tag})) {
48 $bridge = $bridge .= "v$net->{tag}";
49 $bridge_zone = &$register_bridge($bridge);
50 }
51
52 my $vmzone = $conf->{zone} || "vm$vmid";
53 my $zone = "$bridge_zone$vmzone";
54 $net->{zone} = $zone;
55 $zoneinfo->{$zone}->{type} = 'bport';
56 $zoneinfo->{$zone}->{bridge_zone} = $bridge_zone;
57 $zoneinfo->{$zone}->{ifaces}->{"tap${vmid}i${netid}"} = 1;
58 $netinfo->{$vmid}->{$netid} = $net;
59 }
60 }
61
62 #print Dumper($netinfo);
63
64 # NOTE: zone names have length limit, so we need to
65 # translate them into shorter names
66
67 my $zoneid = 0;
68 my $zonemap = { fw => 'fw' };
69
70 my $lookup_zonename = sub {
71 my ($zone) = @_;
72
73 return $zonemap->{$zone} if defined($zonemap->{$zone});
74 $zonemap->{$zone} = 'z' . $zoneid++;
75
76 return $zonemap->{$zone};
77 };
78
79 # dump zone file
80
81 my $out;
82
83 my $format = "%-15s %-10s %s\n";
84 $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS');
85
86 foreach my $z (sort keys %$zoneinfo) {
87 my $zid = &$lookup_zonename($z);
88 if ($zoneinfo->{$z}->{type} eq 'firewall') {
89 $out .= sprintf($format, $zid, $zoneinfo->{$z}->{type}, '');
90 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
91 $out .= sprintf($format, &$lookup_zonename($z), 'ipv4', '');
92 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
93 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
94 my $bzid = &$lookup_zonename($bridge_zone);
95 $out .= sprintf($format, "$zid:$bzid", 'bport', '');
96 } else {
97 die "internal error";
98 }
99 }
100
101 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
102
103 PVE::Tools::file_set_contents("$targetdir/zones", $out);
104
105 # dump interfaces
106
107 $format = "%-15s %-20s %-10s %s\n";
108 $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS');
109
110 foreach my $z (sort keys %$zoneinfo) {
111 my $zid = &$lookup_zonename($z);
112 if ($zoneinfo->{$z}->{type} eq 'firewall') {
113 # do nothing;
114 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
115 my $bridge = $zoneinfo->{$z}->{bridge} || die "internal error";
116 $out .= sprintf($format, $zid, $bridge, 'detect', 'bridge');
117
118 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
119 my $ifaces = $zoneinfo->{$z}->{ifaces};
120 foreach my $iface (sort keys %$ifaces) {
121 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
122 my $bridge = $zoneinfo->{$bridge_zone}->{bridge} || die "internal error";
123 my $iftxt = "$bridge:$iface";
124 $out .= sprintf($format, $zid, $iftxt, '', '');
125 }
126 } else {
127 die "internal error";
128 }
129 }
130
131 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
132
133 PVE::Tools::file_set_contents("$targetdir/interfaces", $out);
134
135 # dump policy
136
137 $format = "%-15s %-15s %-15s %s\n";
138 $out = sprintf($format, '#SOURCE', 'DEST', 'POLICY', 'LOG');
139 $out .= sprintf($format, 'all', 'all', 'REJECT', 'info');
140
141 PVE::Tools::file_set_contents("$targetdir/policy", $out);
142
143
144 }
145
146
147 sub activate {
148
149 }
150
151
152 1;