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