]>
Commit | Line | Data |
---|---|---|
ac1cfefa | 1 | #!/usr/bin/perl |
70df8302 MT |
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 | ############################################################################### | |
ac1cfefa MT |
21 | |
22 | use strict; | |
23 | ||
24 | # enable only the following on debugging purpose | |
25 | #use warnings; | |
26 | #use CGI::Carp 'fatalsToBrowser'; | |
27 | ||
986e08d9 | 28 | require '/var/ipfire/general-functions.pl'; |
ac1cfefa MT |
29 | require "${General::swroot}/lang.pl"; |
30 | require "${General::swroot}/header.pl"; | |
31 | ||
32 | #workaround to suppress a warning when a variable is used only once | |
33 | my @dummy = ( ${Header::colouryellow} ); | |
34 | undef (@dummy); | |
35 | ||
36 | # Files used | |
37 | my $setting = "${General::swroot}/main/settings"; | |
38 | our $datafile = "${General::swroot}/main/hosts"; #(our: used in subroutine) | |
39 | ||
f2fdd0c1 CS |
40 | my %color = (); |
41 | my %mainsettings = (); | |
42 | &General::readhash("${General::swroot}/main/settings", \%mainsettings); | |
43 | &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color); | |
44 | ||
ac1cfefa MT |
45 | our %settings = (); |
46 | #Settings1 | |
47 | # removed | |
48 | ||
49 | #Settings2 for editing the multi-line list | |
50 | #Must not be saved ! | |
51 | $settings{'EN'} = ''; # reuse for dummy field in position zero | |
52 | $settings{'IP'} = ''; | |
53 | $settings{'HOST'} = ''; | |
54 | $settings{'DOM'} = ''; | |
55 | my @nosaved=('EN','IP','HOST','DOM'); # List here ALL setting2 fields. Mandatory | |
56 | ||
57 | $settings{'ACTION'} = ''; # add/edit/remove | |
58 | $settings{'KEY1'} = ''; # point record for ACTION | |
59 | ||
60 | #Define each field that can be used to sort columns | |
61 | my $sortstring='^IP|^HOST|^DOM'; | |
62 | $settings{'SORT_HOSTSLIST'} = 'HOST'; | |
63 | my $errormessage = ''; | |
64 | my $warnmessage = ''; | |
65 | ||
66 | &Header::showhttpheaders(); | |
67 | ||
68 | #Get GUI values | |
69 | &Header::getcgihash(\%settings); | |
70 | ||
71 | # Load multiline data | |
72 | our @current = (); | |
73 | if (open(FILE, "$datafile")) { | |
74 | @current = <FILE>; | |
75 | close (FILE); | |
76 | } | |
77 | ||
78 | ## Settings1 Box not used... | |
79 | &General::readhash("${General::swroot}/main/settings", \%settings); | |
80 | ||
81 | ||
82 | ## Now manipulate the multi-line list with Settings2 | |
83 | # Basic actions are: | |
84 | # toggle the check box | |
85 | # add/update a new line | |
86 | # begin editing a line | |
87 | # remove a line | |
88 | ||
89 | ||
90 | # Toggle enable/disable field. Field is in second position | |
91 | if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) { | |
92 | #move out new line | |
93 | chomp(@current[$settings{'KEY1'}]); | |
94 | my @temp = split(/\,/,@current[$settings{'KEY1'}]); | |
95 | ||
96 | $temp[0] = $temp[0] ne '' ? '' : 'on'; # Toggle the field | |
97 | @current[$settings{'KEY1'}] = join (',',@temp)."\n"; | |
98 | $settings{'KEY1'} = ''; # End edit mode | |
99 | ||
100 | &General::log($Lang::tr{'hosts config changed'}); | |
101 | ||
102 | #Save current | |
103 | open(FILE, ">$datafile") or die 'hosts datafile error'; | |
104 | print FILE @current; | |
105 | close(FILE); | |
106 | ||
107 | # Rebuild configuration file | |
108 | &BuildConfiguration; | |
109 | } | |
110 | ||
111 | if ($settings{'ACTION'} eq $Lang::tr{'add'}) { | |
112 | # Validate inputs | |
113 | unless(&General::validip($settings{'IP'})) { | |
114 | $errormessage = $Lang::tr{'invalid fixed ip address'}; | |
115 | } | |
116 | ||
117 | unless(&General::validhostname($settings{'HOST'})) { | |
118 | $errormessage = $Lang::tr{'invalid hostname'}; | |
119 | } | |
120 | ||
121 | if ($settings{'DOM'} && ! &General::validdomainname($settings{'DOM'})) { | |
122 | $errormessage = $Lang::tr{'invalid domain name'}; | |
123 | } | |
124 | ||
125 | ||
126 | unless ($errormessage) { | |
127 | if ($settings{'KEY1'} eq '') { #add or edit ? | |
128 | unshift (@current, "$settings{'EN'},$settings{'IP'},$settings{'HOST'},$settings{'DOM'}\n"); | |
129 | &General::log($Lang::tr{'hosts config added'}); | |
130 | } else { | |
131 | @current[$settings{'KEY1'}] = "$settings{'EN'},$settings{'IP'},$settings{'HOST'},$settings{'DOM'}\n"; | |
132 | $settings{'KEY1'} = ''; # End edit mode | |
133 | &General::log($Lang::tr{'hosts config changed'}); | |
134 | } | |
135 | ||
136 | # Write changes to config file. | |
137 | &SortDataFile; # sort newly added/modified entry | |
138 | &BuildConfiguration; # then re-build new host | |
139 | ||
140 | #map ($settings{$_}='' ,@nosaved); # Clear fields | |
141 | } | |
142 | } | |
143 | ||
144 | if ($settings{'ACTION'} eq $Lang::tr{'edit'}) { | |
145 | #move out new line | |
146 | my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current | |
147 | chomp($line); | |
148 | my @temp = split(/\,/, $line); | |
149 | $settings{'EN'}=$temp[0]; # Prepare the screen for editing | |
150 | $settings{'IP'}=$temp[1]; | |
151 | $settings{'HOST'}=$temp[2]; | |
152 | $settings{'DOM'}=$temp[3]; | |
153 | } | |
154 | ||
155 | if ($settings{'ACTION'} eq $Lang::tr{'remove'}) { | |
156 | splice (@current,$settings{'KEY1'},1); # Delete line | |
157 | open(FILE, ">$datafile") or die 'hosts datafile error'; | |
158 | print FILE @current; | |
159 | close(FILE); | |
160 | $settings{'KEY1'} = ''; # End remove mode | |
161 | &General::log($Lang::tr{'hosts config changed'}); | |
162 | ||
163 | &BuildConfiguration; # then re-build conf which use new data | |
164 | } | |
165 | ||
166 | ||
167 | ||
168 | ## Check if sorting is asked | |
169 | # If same column clicked, reverse the sort. | |
170 | if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) { | |
171 | my $newsort=$ENV{'QUERY_STRING'}; | |
172 | my $actual=$settings{'SORT_HOSTSLIST'}; | |
173 | #Reverse actual sort ? | |
174 | if ($actual =~ $newsort) { | |
175 | my $Rev=''; | |
176 | if ($actual !~ 'Rev') { | |
177 | $Rev='Rev'; | |
178 | } | |
179 | $newsort.=$Rev; | |
180 | } | |
181 | $settings{'SORT_HOSTSLIST'}=$newsort; | |
182 | map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved | |
183 | &General::writehash($setting, \%settings); | |
184 | &SortDataFile; | |
185 | $settings{'ACTION'} = 'SORT'; # Create an 'ACTION' | |
186 | map ($settings{$_} = '' ,@nosaved,'KEY1'); # and reinit vars to empty | |
187 | } | |
188 | ||
189 | if ($settings{'ACTION'} eq '' ) { # First launch from GUI | |
190 | # Place here default value when nothing is initialized | |
191 | $settings{'EN'} = 'on'; | |
192 | $settings{'DOM'} = $settings{'DOMAINNAME'}; | |
193 | } | |
194 | ||
195 | &Header::openpage($Lang::tr{'hostname'}, 1, ''); | |
196 | &Header::openbigbox('100%', 'left', '', $errormessage); | |
197 | my %checked=(); # Checkbox manipulations | |
198 | ||
199 | if ($errormessage) { | |
200 | &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); | |
201 | print "<font class='base'>$errormessage </font>"; | |
202 | &Header::closebox(); | |
203 | } | |
204 | ||
205 | # | |
206 | # Remove if no Setting1 needed | |
207 | # | |
208 | #if ($warnmessage) { | |
209 | # $warnmessage = "<font color=${Header::colourred}><b>$Lang::tr{'capswarning'}</b></font>: $warnmessage"; | |
210 | #} | |
211 | #&Header::openbox('100%', 'left', $Lang::tr{'settings'}); | |
212 | #print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>"; | |
213 | #print <<END | |
214 | #<table width='100%'> | |
215 | #<tr> | |
216 | # <td class='base'>$Lang::tr{'domain name'} : $settings{'DOMAINNAME'}</td> | |
217 | #</table> | |
218 | # | |
219 | #END | |
220 | #; | |
221 | # | |
222 | #print <<END | |
223 | #<table width='100%'> | |
224 | #<hr /> | |
225 | #<tr> | |
226 | # <td class='base' width='25%'><!--<img src='/blob.gif' align='top' alt='*' /> $Lang::tr{'this field may be blank'}</td>--> | |
227 | # <td class='base' width='25%'>$warnmessage</td> | |
228 | # <td width='50%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' disabled='disabled' /></td> | |
229 | #</tr> | |
230 | #</table> | |
231 | #</form> | |
232 | #END | |
233 | #; | |
234 | #&Header::closebox(); # end of Settings1 | |
235 | ||
236 | ||
237 | # | |
238 | # Second check box is for editing the list | |
239 | # | |
240 | $checked{'EN'}{'on'} = ($settings{'EN'} eq '' ) ? '' : "checked='checked'"; | |
241 | ||
242 | my $buttontext = $Lang::tr{'add'}; | |
243 | if ($settings{'KEY1'} ne '') { | |
244 | $buttontext = $Lang::tr{'update'}; | |
245 | &Header::openbox('100%', 'left', $Lang::tr{'edit an existing host'}); | |
246 | } else { | |
247 | &Header::openbox('100%', 'left', $Lang::tr{'add a host'}); | |
248 | } | |
249 | ||
250 | #Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order' | |
251 | print <<END | |
252 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
253 | <input type='hidden' name='KEY1' value='$settings{'KEY1'}' /> | |
254 | <table width='100%'> | |
255 | <tr> | |
256 | <td class='base'>$Lang::tr{'host ip'}: </td> | |
257 | <td><input type='text' name='IP' value='$settings{'IP'}' /></td> | |
258 | <td class='base'>$Lang::tr{'hostname'}:</td> | |
259 | <td><input type='text' name='HOST' value='$settings{'HOST'}' /></td> | |
260 | </tr><tr> | |
261 | <td class='base'>$Lang::tr{'domain name'}: <img src='/blob.gif' alt='*' /></td> | |
262 | <td><input type='text' name='DOM' value='$settings{'DOM'}' /></td> | |
263 | <td class='base'>$Lang::tr{'enabled'}</td> | |
264 | <td><input type='checkbox' name='EN' $checked{'EN'}{'on'} /></td> | |
265 | </tr> | |
266 | </table> | |
267 | <hr /> | |
268 | <table width='100%'> | |
269 | <tr> | |
270 | <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' /> $Lang::tr{'this field may be blank'}</td> | |
271 | <td width='50%' align='center'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td> | |
272 | </tr> | |
273 | </table> | |
274 | </form> | |
275 | END | |
276 | ; | |
277 | &Header::closebox(); | |
278 | ||
279 | # | |
280 | # Third box shows the list, in columns | |
281 | # | |
282 | # Columns headers may content a link. In this case it must be named in $sortstring | |
283 | # | |
284 | &Header::openbox('100%', 'left', $Lang::tr{'current hosts'}); | |
285 | print <<END | |
286 | <table width='100%'> | |
287 | <tr> | |
288 | <td width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'host ip'}</b></a></td> | |
289 | <td width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?HOST'><b>$Lang::tr{'hostname'}</b></a></td> | |
290 | <td width='50%' align='center'><a href='$ENV{'SCRIPT_NAME'}?DOM'><b>$Lang::tr{'domain name'}</b></a></td> | |
291 | <td width='10%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></td> | |
292 | </tr> | |
293 | END | |
294 | ; | |
295 | ||
296 | # | |
297 | # Print each line of @current list | |
298 | # | |
299 | ||
300 | my $key = 0; | |
301 | foreach my $line (@current) { | |
302 | chomp($line); # remove newline | |
303 | my @temp=split(/\,/,$line); | |
304 | $temp[3] ='' unless defined $temp[3]; # not always populated | |
305 | ||
306 | #Choose icon for checkbox | |
307 | my $gif = ''; | |
308 | my $gdesc = ''; | |
309 | if ($temp[0] ne '' ) { | |
310 | $gif = 'on.gif'; | |
311 | $gdesc = $Lang::tr{'click to disable'}; | |
312 | } else { | |
313 | $gif = 'off.gif'; | |
314 | $gdesc = $Lang::tr{'click to enable'}; | |
315 | } | |
316 | ||
317 | #Colorize each line | |
318 | if ($settings{'KEY1'} eq $key) { | |
319 | print "<tr bgcolor='${Header::colouryellow}'>"; | |
320 | } elsif ($key % 2) { | |
f2fdd0c1 | 321 | print "<tr bgcolor='$color{'color22'}'>"; |
ac1cfefa | 322 | } else { |
f2fdd0c1 | 323 | print "<tr bgcolor='$color{'color20'}'>"; |
ac1cfefa MT |
324 | } |
325 | print <<END | |
326 | <td align='center'>$temp[1]</td> | |
327 | <td align='center'>$temp[2]</td> | |
328 | <td align='center'>$temp[3]</td> | |
329 | <td align='center'> | |
330 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
331 | <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' /> | |
332 | <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' /> | |
333 | <input type='hidden' name='KEY1' value='$key' /> | |
334 | </form> | |
335 | </td> | |
336 | ||
337 | <td align='center'> | |
338 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
339 | <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' /> | |
340 | <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' /> | |
341 | <input type='hidden' name='KEY1' value='$key' /> | |
342 | </form> | |
343 | </td> | |
344 | ||
345 | <td align='center'> | |
346 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
347 | <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' /> | |
348 | <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' /> | |
349 | <input type='hidden' name='KEY1' value='$key' /> | |
350 | </form> | |
351 | </td> | |
352 | </tr> | |
353 | END | |
354 | ; | |
355 | $key++; | |
356 | } | |
357 | print "</table>"; | |
358 | ||
359 | # If table contains entries, print 'Key to action icons' | |
360 | if ($key) { | |
361 | print <<END | |
362 | <table> | |
363 | <tr> | |
364 | <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td> | |
365 | <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td> | |
366 | <td class='base'>$Lang::tr{'click to disable'}</td> | |
367 | <td> </td> | |
368 | <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td> | |
369 | <td class='base'>$Lang::tr{'click to enable'}</td> | |
370 | <td> </td> | |
371 | <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td> | |
372 | <td class='base'>$Lang::tr{'edit'}</td> | |
373 | <td> </td> | |
374 | <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td> | |
375 | <td class='base'>$Lang::tr{'remove'}</td> | |
376 | </tr> | |
377 | </table> | |
378 | END | |
379 | ; | |
380 | } | |
381 | ||
382 | &Header::closebox(); | |
383 | &Header::closebigbox(); | |
384 | &Header::closepage(); | |
385 | ||
386 | ## Ouf it's the end ! | |
387 | ||
388 | # Sort the "current" array according to choices | |
389 | sub SortDataFile | |
390 | { | |
391 | our %entries = (); | |
392 | ||
393 | # Sort pair of record received in $a $b special vars. | |
394 | # When IP is specified use numeric sort else alpha. | |
395 | # If sortname ends with 'Rev', do reverse sort. | |
396 | # | |
397 | sub fixedleasesort { | |
398 | my $qs=''; # The sort field specified minus 'Rev' | |
399 | if (rindex ($settings{'SORT_HOSTSLIST'},'Rev') != -1) { | |
400 | $qs=substr ($settings{'SORT_HOSTSLIST'},0,length($settings{'SORT_HOSTSLIST'})-3); | |
401 | if ($qs eq 'IP') { | |
402 | my @a = split(/\./,$entries{$a}->{$qs}); | |
403 | my @b = split(/\./,$entries{$b}->{$qs}); | |
404 | ($b[0]<=>$a[0]) || | |
405 | ($b[1]<=>$a[1]) || | |
406 | ($b[2]<=>$a[2]) || | |
407 | ($b[3]<=>$a[3]); | |
408 | } else { | |
409 | $entries{$b}->{$qs} cmp $entries{$a}->{$qs}; | |
410 | } | |
411 | } else { #not reverse | |
412 | $qs=$settings{'SORT_HOSTSLIST'}; | |
413 | if ($qs eq 'IP') { | |
414 | my @a = split(/\./,$entries{$a}->{$qs}); | |
415 | my @b = split(/\./,$entries{$b}->{$qs}); | |
416 | ($a[0]<=>$b[0]) || | |
417 | ($a[1]<=>$b[1]) || | |
418 | ($a[2]<=>$b[2]) || | |
419 | ($a[3]<=>$b[3]); | |
420 | } else { | |
421 | $entries{$a}->{$qs} cmp $entries{$b}->{$qs}; | |
422 | } | |
423 | } | |
424 | } | |
425 | ||
426 | #Use an associative array (%entries) | |
427 | my $key = 0; | |
428 | foreach my $line (@current) { | |
429 | chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK) | |
430 | my @temp = ( '','','', ''); | |
431 | @temp = split (',',$line); | |
432 | ||
433 | # Build a pair 'Field Name',value for each of the data dataline. | |
434 | # Each SORTABLE field must have is pair. | |
435 | # Other data fields (non sortable) can be grouped in one | |
436 | ||
437 | my @record = ('KEY',$key++,'EN',$temp[0],'IP',$temp[1],'HOST',$temp[2],'DOM',$temp[3]); | |
438 | my $record = {}; # create a reference to empty hash | |
439 | %{$record} = @record; # populate that hash with @record | |
440 | $entries{$record->{KEY}} = $record; # add this to a hash of hashes | |
441 | } | |
442 | ||
443 | open(FILE, ">$datafile") or die 'hosts datafile error'; | |
444 | ||
445 | # Each field value is printed , with the newline ! Don't forget separator and order of them. | |
446 | foreach my $entry (sort fixedleasesort keys %entries) { | |
447 | print FILE "$entries{$entry}->{EN},$entries{$entry}->{IP},$entries{$entry}->{HOST},$entries{$entry}->{DOM}\n"; | |
448 | } | |
449 | ||
450 | close(FILE); | |
451 | # Reload sorted @current | |
452 | open (FILE, "$datafile"); | |
453 | @current = <FILE>; | |
454 | close (FILE); | |
455 | } | |
456 | ||
457 | # | |
458 | # Build the configuration file | |
459 | # | |
460 | sub BuildConfiguration { | |
461 | system '/usr/local/bin/rebuildhosts'; | |
462 | } |