]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/networks.cgi
Corrected credits
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / networks.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22
23 use strict;
24
25 require '/var/ipfire/general-functions.pl';
26 require "${General::swroot}/lang.pl";
27 require "${General::swroot}/header.pl";
28
29 my %netsettings=();
30 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
31
32 my @networks=();
33 my $filename = "${General::swroot}/firewall/customnetworks";
34 &setup_default_networks();
35
36 &Header::getcgihash(\%cgiparams);
37
38 if ($cgiparams{'ACTION'} eq $Lang::tr{'add'}){
39
40 &validateparams();
41 unless($errormessage){
42 $key++; # Add one to last sequence number
43 open(FILE,">>$filename") or die 'Unable to open custom networks file.';
44 flock FILE, 2;
45 print FILE "$key,$cgiparams{'NAME'},$cgiparams{'IPADDRESS'},$cgiparams{'NETMASK'}\n";
46 close(FILE);
47 &General::log("$Lang::tr{'network added'}: $cgiparams{'NAME'}");
48 undef %cgiparams;
49 }
50 }
51
52 if ($cgiparams{'ACTION'} eq $Lang::tr{'update'})
53 {
54 &validateparams();
55 # Darren Critchley - If there is an error don't waste any more processing time
56 if ($errormessage) { $cgiparams{'ACTION'} = $Lang::tr{'edit'}; goto UPD_ERROR; }
57
58 unless($errormessage){
59 open(FILE, $filename) or die 'Unable to open custom networks file.';
60 my @current = <FILE>;
61 close(FILE);
62 open(FILE, ">$filename") or die 'Unable to open config file.';
63 flock FILE, 2;
64 foreach my $line (@current) {
65 chomp($line);
66 my @temp = split(/\,/,$line);
67 if ($cgiparams{'KEY'} eq $temp[0]) {
68 print FILE "$cgiparams{'KEY'},$cgiparams{'NAME'},$cgiparams{'IPADDRESS'},$cgiparams{'NETMASK'}\n";
69 } else {
70 print FILE "$line\n";
71 }
72 }
73 close(FILE);
74 &General::log("$Lang::tr{'network updated'}: $cgiparams{'NAME'}");
75 undef %cgiparams;
76 }
77 UPD_ERROR:
78 }
79
80 if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'})
81 {
82 open(FILE, "$filename") or die 'Unable to open custom networks file.';
83 my @current = <FILE>;
84 close(FILE);
85
86 unless ($errormessage)
87 {
88 foreach my $line (@current)
89 {
90 chomp($line);
91 my @temp = split(/\,/,$line);
92 if ($cgiparams{'KEY'} eq $temp[0]) {
93 $cgiparams{'NAME'} = $temp[1];
94 $cgiparams{'IPADDRESS'} = $temp[2];
95 $cgiparams{'NETMASK'} = $temp[3];
96 }
97
98 }
99 }
100 }
101
102 if ($cgiparams{'ACTION'} eq $Lang::tr{'remove'})
103 {
104 open(FILE, $filename) or die 'Unable to open custom networks file.';
105 my @current = <FILE>;
106 close(FILE);
107
108 open(FILE, ">$filename") or die 'Unable to open custom networks file.';
109 flock FILE, 2;
110 foreach my $line (@current)
111 {
112 chomp($line);
113 if ($line ne '') {
114 my @temp = split(/\,/,$line);
115 if ($cgiparams{'KEY'} eq $temp[0]) {
116 &General::log("$Lang::tr{'network removed'}: $temp[1]");
117 } else {
118 print FILE "$temp[0],$temp[1],$temp[2],$temp[3]\n";
119 }
120 }
121 }
122 close(FILE);
123 undef %cgiparams;
124 }
125
126 if ($cgiparams{'ACTION'} eq $Lang::tr{'reset'})
127 {
128 undef %cgiparams;
129 }
130
131 if ($cgiparams{'ACTION'} eq '')
132 {
133 $cgiparams{'KEY'} = '';
134 $cgiparams{'IPADDRESS'} = '';
135 $cgiparams{'NETMASK'} = '';
136 $cgiparams{'NAME'} = '';
137 }
138
139 &Header::showhttpheaders();
140
141 &Header::openpage($Lang::tr{'networks settings'}, 1, '');
142
143 &Header::openbigbox('100%', 'LEFT', '', $errormessage);
144
145 # DEBUG DEBUG
146 #&Header::openbox('100%', 'LEFT', 'DEBUG');
147 #foreach $line (keys %cgiparams) {
148 # print "<CLASS NAME='base'>$line = $cgiparams{$line}<BR>";
149 #}
150 #print "$ENV{'QUERY_STRING'}\n";
151 #print "&nbsp;</CLASS>\n";
152 #&Header::closebox();
153
154 if ($errormessage) {
155 &Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
156 print "<CLASS NAME='base'><FONT COLOR='${Header::colourred}'>$errormessage\n</FONT>";
157 print "&nbsp;</CLASS>\n";
158 &Header::closebox();
159 }
160
161 if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}){
162 &Header::openbox('100%', 'LEFT', "$Lang::tr{'edit network'}:");
163 } else {
164 &Header::openbox('100%', 'LEFT', "$Lang::tr{'add network'}:");
165 }
166 print <<END
167 <FORM METHOD='POST'>
168 <DIV ALIGN='CENTER'>
169 <TABLE WIDTH='100%'>
170 <TR align="center">
171 <TD><strong>$Lang::tr{'name'}</strong></TD>
172 <TD><strong>$Lang::tr{'ip address'}</strong></TD>
173 <TD><strong>$Lang::tr{'netmask'}</strong></TD>
174 <TD>&nbsp;</TD>
175 <TD>&nbsp;</TD>
176 <TD>&nbsp;</TD>
177 </TR>
178 <TR align="center">
179 <TD>
180 <INPUT TYPE='TEXT' NAME='NAME' VALUE='$cgiparams{'NAME'}' SIZE='20' MAXLENGTH='20'>
181 </TD>
182 <TD>
183 <INPUT TYPE='TEXT' NAME='IPADDRESS' VALUE='$cgiparams{'IPADDRESS'}' SIZE='15' MAXLENGTH='15'>
184 </TD>
185 <TD>
186 <INPUT TYPE='TEXT' NAME='NETMASK' VALUE='$cgiparams{'NETMASK'}' SIZE='15' MAXLENGTH='15'>
187 </TD>
188 END
189 ;
190 if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}){
191 # Darren Critchley - put in next release - author has authorized GPL inclusion
192 # print "<TD ALIGN='CENTER'><a href='ipcalc.cgi' target='_blank'>IP Calculator</a></TD>\n";
193 print "<TD ALIGN='CENTER'><INPUT TYPE='SUBMIT' NAME='ACTION' VALUE='$Lang::tr{'update'}'></TD>\n";
194 print "<INPUT TYPE='HIDDEN' NAME='KEY' VALUE='$cgiparams{'KEY'}'>\n";
195 print "<TD ALIGN='CENTER'><INPUT TYPE='SUBMIT' NAME='ACTION' VALUE='$Lang::tr{'reset'}'></TD>\n";
196 } else {
197 # Darren Critchley - put in next release - author has authorized GPL inclusion
198 # print "<TD ALIGN='CENTER'><a href='ipcalc.cgi' target='_blank'>IP Calculator</a></TD>\n";
199 print "<TD ALIGN='CENTER'><INPUT TYPE='SUBMIT' NAME='ACTION' VALUE='$Lang::tr{'add'}'></TD>\n";
200 print "<TD ALIGN='CENTER'><INPUT TYPE='SUBMIT' NAME='ACTION' VALUE='$Lang::tr{'reset'}'></TD>\n";
201 }
202 print <<END
203 </TR>
204 </TABLE>
205 </DIV>
206 </FORM>
207 END
208 ;
209 &Header::closebox();
210
211 &Header::openbox('100%', 'LEFT', "$Lang::tr{'custom networks'}:");
212 print <<END
213 <DIV ALIGN='CENTER'>
214 <TABLE WIDTH='100%' ALIGN='CENTER'>
215 <TR align="center">
216 <TD><strong>$Lang::tr{'name'}</strong></TD>
217 <TD><strong>$Lang::tr{'ip address'}</strong></TD>
218 <TD><strong>$Lang::tr{'netmask'}</strong></TD>
219 </TR>
220 END
221 ;
222 &display_custom_networks();
223 print <<END
224 </TABLE>
225 </DIV>
226 END
227 ;
228 &Header::closebox();
229
230 &Header::openbox('100%', 'LEFT', "$Lang::tr{'default networks'}:");
231 print <<END
232 <DIV ALIGN='CENTER'>
233 <TABLE WIDTH='100%' ALIGN='CENTER'>
234 <TR align="center">
235 <TD><strong>$Lang::tr{'name'}</strong></TD>
236 <TD><strong>$Lang::tr{'ip address'}</strong></TD>
237 <TD><strong>$Lang::tr{'netmask'}</strong></TD>
238 </TR>
239 END
240 ;
241 &display_default_networks();
242 print <<END
243 </TABLE>
244 </DIV>
245 END
246 ;
247 &Header::closebox();
248
249 print "$Lang::tr{'this feature has been sponsored by'} : ";
250 print "<A HREF='http://www.kdi.ca/' TARGET='_blank'>Kobelt Development Inc.</A>.\n";
251
252 &Header::closebigbox();
253
254 &Header::closepage();
255
256 sub display_custom_networks
257 {
258 open(FILE, "$filename") or die 'Unable to open networks file.';
259 my @current = <FILE>;
260 close(FILE);
261
262 my $id = 0;
263 foreach $line (@current)
264 {
265 chomp($line);
266 if ($line ne ''){
267 my @temp = split(/\,/,$line);
268 # Darren Critchley highlight the row we are editing
269 if ( $cgiparams{'ACTION'} eq $Lang::tr{'edit'} && $cgiparams{'KEY'} eq $temp[0] ) {
270 print "<TR BGCOLOR='${Header::colouryellow}'>\n";
271 } else {
272 if ($id % 2) {
273 print "<TR BGCOLOR='${Header::table1colour}'>\n";
274 } else {
275 print "<TR BGCOLOR='${Header::table2colour}'>\n";
276 }
277 }
278 print "<TD>$temp[1]</TD>\n";
279 print "<TD ALIGN='CENTER'>$temp[2]</TD>\n";
280 print "<TD ALIGN='CENTER'>$temp[3]</TD>\n";
281 print <<END
282 <FORM METHOD='POST' NAME='frm$temp[0]'>
283 <TD ALIGN='CENTER'>
284 <INPUT TYPE='hidden' NAME='ACTION' VALUE='$Lang::tr{'edit'}'>
285 <INPUT TYPE='image' NAME='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' width='20' height='20' border='0'>
286 <INPUT TYPE='hidden' NAME='KEY' VALUE='$temp[0]'>
287 </TD>
288 </FORM>
289 <FORM METHOD='POST' NAME='frm$temp[0]b'>
290 <TD ALIGN='CENTER'>
291 <INPUT TYPE='hidden' NAME='ACTION' VALUE='$Lang::tr{'remove'}'>
292 <INPUT TYPE='image' NAME='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' width='20' height='20' border='0'>
293 <INPUT TYPE='hidden' NAME='KEY' VALUE='$temp[0]'>
294 </TD>
295 </FORM>
296 END
297 ;
298 print "</TR>\n";
299 $id++;
300 }
301 }
302 }
303
304 sub display_default_networks
305 {
306 foreach $line (sort @networks)
307 {
308 my @temp = split(/\,/,$line);
309 if ($id % 2) {
310 print "<TR BGCOLOR='${Header::table1colour}'>\n";
311 } else {
312 print "<TR BGCOLOR='${Header::table2colour}'>\n";
313 }
314 print "<TD>$temp[0]</TD>\n";
315 print "<TD ALIGN='CENTER'>$temp[1]</TD>\n";
316 print "<TD ALIGN='CENTER'>$temp[2]</TD>\n";
317 print "</TR>\n";
318 $id++;
319 }
320 }
321
322 sub setup_default_networks
323 {
324 # Get current defined networks (Red, Green, Blue, Orange)
325 my $line = "Any,0.0.0.0,0.0.0.0";
326 push (@networks, $line);
327 $line = "localhost,127.0.0.1,255.255.255.255";
328 push (@networks, $line);
329 $line = "localnet,127.0.0.0,255.0.0.0";
330 push (@networks, $line);
331 $line = "Private Network 10.0.0.0,10.0.0.0,255.0.0.0";
332 push (@networks, $line);
333 $line = "Private Network 172.16.0.0,172.16.0.0,255.240.0.0";
334 push (@networks, $line);
335 $line = "Private Network 192.168.0.0,192.168.0.0,255.255.0.0";
336 push (@networks, $line);
337
338 my $red_address=`cat ${General::swroot}/red/local-ipaddress`;
339 $line = "Red Address,$red_address,";
340 push (@networks, $line);
341
342 $line = "Green Address,$netsettings{'GREEN_ADDRESS'},255.255.255.255";
343 push (@networks, $line);
344 $line = "Green Network,$netsettings{'GREEN_NETADDRESS'},$netsettings{'GREEN_NETMASK'}";
345 push (@networks, $line);
346
347 if ($netsettings{'ORANGE_DEV'}ne ''){
348 $line = "Orange Address,$netsettings{'ORANGE_ADDRESS'},255.255.255.255";
349 push (@networks, $line);
350 $line = "Orange Network,$netsettings{'ORANGE_NETADDRESS'},$netsettings{'ORANGE_NETMASK'}";
351 push (@networks, $line);
352 }
353
354 if ($netsettings{'BLUE_DEV'}ne ''){
355 $line = "Blue Address,$netsettings{'BLUE_ADDRESS'},255.255.255.255";
356 push (@networks, $line);
357 $line = "Blue Network,$netsettings{'BLUE_NETADDRESS'},$netsettings{'BLUE_NETMASK'}";
358 push (@networks, $line);
359 }
360 open(FILE, "${General::swroot}/ethernet/aliases") or die 'Unable to open aliases file.';
361 my @current = <FILE>;
362 close(FILE);
363 my $ctr = 0;
364 foreach my $lne (@current)
365 {
366 if ($lne ne ''){
367 chomp($lne);
368 my @temp = split(/\,/,$lne);
369 if ($temp[2] eq '') {
370 $temp[2] = "Alias $ctr : $temp[0]";
371 }
372 $line = "$temp[2],$temp[0],";
373 push (@networks, $line);
374 $ctr++;
375 }
376 }
377 }
378
379 # Validate Field Entries
380 sub validateparams
381 {
382 if ($cgiparams{'NAME'} eq '') {
383 $errormessage = $Lang::tr{'nonetworkname'};
384 return;
385 }
386 $cgiparams{'NAME'}=&Header::cleanhtml($cgiparams{'NAME'});
387 unless(&General::validip($cgiparams{'IPADDRESS'})){$errormessage = $Lang::tr{'invalid ip'}; }
388 unless($errormessage){
389 my @tmp = split(/\./,$cgiparams{'IPADDRESS'});
390 if ($cgiparams{'NETMASK'} eq '' && $tmp[3] ne '255' && $tmp[3] ne '0'){
391 $cgiparams{'NETMASK'} = "255.255.255.255";
392 }
393 }
394 unless(&General::validmask($cgiparams{'NETMASK'})){$errormessage = $Lang::tr{'subnet is invalid'}; }
395
396 open(FILE, $filename) or die 'Unable to open custom network file.';
397 my @current = <FILE>;
398 close(FILE);
399 foreach my $line (@current)
400 {
401 chomp($line);
402 if ($line ne '') {
403 my @temp = split(/\,/,$line);
404 if ($cgiparams{'NAME'} eq $temp[1] && $cgiparams{'KEY'} ne $temp[0]) {
405 $errormessage=$Lang::tr{'duplicate name'};
406 return;
407 }
408 $key=$temp[0];
409 }
410 }
411 foreach $line (@networks)
412 {
413 my @temp = split(/\,/,$line);
414 if ($cgiparams{'NAME'} eq $temp[0]) {
415 $errormessage=$Lang::tr{'duplicate name'};
416 return;
417 }
418 }
419 }