]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - html/cgi-bin/wakeonlan.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
24 # enable only the following on debugging purpose
26 #use CGI::Carp 'fatalsToBrowser';
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
34 # remove comment from next line to get wakeup info in seperate page
36 # remove comment from next line to get wakeup info as inline box
40 #workaround to suppress a warning when a variable is used only once
41 my @dummy = ( ${Header
::colouryellow
} );
48 our $datafile = "/var/ipfire/wakeonlan/clients.conf";
52 my %mainsettings = ();
53 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
54 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
57 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
60 $cgiparams{'ACTION'} = ''; # add/edit/update/remove/wakeup
61 $cgiparams{'ID'} = ''; # points to record for ACTION (edit/update/remove)
62 $cgiparams{'CLIENT_MAC'} = '';
63 $cgiparams{'CLIENT_IFACE'} = '';
64 $cgiparams{'CLIENT_COMMENT'} = '';
65 &Header
::getcgihash
(\
%cgiparams);
68 $selected{'CLIENT_IFACE'}{'green'} = '';
69 $selected{'CLIENT_IFACE'}{'blue'} = '';
70 $selected{'CLIENT_IFACE'}{'orange'} = '';
71 $selected{'CLIENT_IFACE'}{'red'} = '';
73 &Header
::showhttpheaders
();
75 my $errormessage = "";
77 if ( $cgiparams{'ACTION'} eq 'add' )
79 # add a device, check for valid and non-duplicate MAC
80 if ( $cgiparams{'CLIENT_MAC'} eq '' )
85 $cgiparams{'CLIENT_MAC'} =~ tr/-/:/;
87 unless( &General
::validmac
($cgiparams{'CLIENT_MAC'}) )
89 $errormessage = $Lang::tr
{'invalid mac address'};
93 for $i ( 0 .. $#wol_devices )
95 if ( lc($cgiparams{'CLIENT_MAC'}) eq lc($wol_devices[$i]{'MAC'}) )
97 $errormessage = $Lang::tr
{'duplicate mac'};
102 unless ( $errormessage )
104 push @wol_devices, { MAC
=> uc($cgiparams{'CLIENT_MAC'}), IFace
=> $cgiparams{'CLIENT_IFACE'}, Comment
=> $cgiparams{'CLIENT_COMMENT'} };
110 # jump here to keep cgiparams!
113 if ( $cgiparams{'ACTION'} eq 'update' )
115 # update a device, check for valid and non-duplicate MAC
116 if ( $cgiparams{'CLIENT_MAC'} eq '' )
121 $cgiparams{'CLIENT_MAC'} =~ tr/-/:/;
123 unless( &General
::validmac
($cgiparams{'CLIENT_MAC'}) )
125 $errormessage = $Lang::tr
{'invalid mac address'};
129 for $i ( 0 .. $#wol_devices )
131 if ( $i == $cgiparams{'ID'} ) { next; }
132 if ( lc($cgiparams{'CLIENT_MAC'}) eq lc($wol_devices[$i]{'MAC'}) )
134 $errormessage = $Lang::tr
{'duplicate mac'};
139 unless ( $errormessage )
141 $wol_devices[$cgiparams{'ID'}]{'MAC'} = $cgiparams{'CLIENT_MAC'};
142 $wol_devices[$cgiparams{'ID'}]{'IFace'} = $cgiparams{'CLIENT_IFACE'};
143 $wol_devices[$cgiparams{'ID'}]{'Comment'} = $cgiparams{'CLIENT_COMMENT'};
149 # jump here to keep cgiparams!
152 if ( $cgiparams{'ACTION'} eq 'remove' )
154 # simply set MAC to empty, WriteConfig will handle the gory details
155 $wol_devices[$cgiparams{'ID'}]{'MAC'} = '';
159 if ( ($cgiparams{'ACTION'} ne 'wakeup') || ($refresh ne 'yes') )
161 &Header
::openpage
($Lang::tr
{'WakeOnLan'}, 1, '');
162 &Header
::openbigbox
('100%', 'left', '', $errormessage);
165 if ( $cgiparams{'ACTION'} eq 'wakeup' )
168 my $mac = $wol_devices[$cgiparams{'ID'}]{'MAC'};
169 my $iface = uc($wol_devices[$cgiparams{'ID'}]{'IFace'}).'_DEV';
170 $iface = $netsettings{"$iface"};
174 system("/usr/local/bin/launch-ether-wake $mac $iface");
176 # make a box with info, 'refresh' to normal screen after 5 seconds
177 if ( $refresh eq 'yes' )
179 &Header
::openpage
($Lang::tr
{'WakeOnLan'}, 1, "<meta http-equiv='refresh' content='3;url=/cgi-bin/wakeonlan.cgi'");
180 &Header
::openbigbox
('100%', 'left');
182 &Header
::openbox
('100%', 'left', $Lang::tr
{'WakeOnLan'});
183 print "<p>$Lang::tr{'magic packet send to:'} $mac ($iface)</p>";
186 if ( $refresh eq 'yes' )
188 &Header
::closebigbox
();
189 &Header
::closepage
();
195 #print "Action: $cgiparams{'ACTION'}<br />";
196 #print "ID: $cgiparams{'ID'}<br />";
197 #print "MAC: $cgiparams{'CLIENT_MAC'}<br />";
198 #print "IFace: $cgiparams{'CLIENT_IFACE'}<br />";
199 #print "Rem: $cgiparams{'CLIENT_COMMENT'}<br />";
203 # some error from add / update
204 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
205 print "<class name='base'>$errormessage\n";
206 print " </class>\n";
210 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
212 $selected{'CLIENT_IFACE'}{$cgiparams{'CLIENT_IFACE'}} = "selected='selected'";
213 my $buttontext = $Lang::tr
{'add'};
214 if ( $cgiparams{'ACTION'} eq 'edit' )
216 &Header
::openbox
('100%', 'left', "$Lang::tr{'edit device'}");
217 $buttontext = $Lang::tr
{'update'};
218 $cgiparams{'CLIENT_MAC'} = $wol_devices[$cgiparams{'ID'}]{'MAC'};
219 $selected{'CLIENT_IFACE'}{$wol_devices[$cgiparams{'ID'}]{'IFace'}} = "selected='selected'";
220 $cgiparams{'CLIENT_COMMENT'} = $wol_devices[$cgiparams{'ID'}]{'Comment'};
222 elsif ( $cgiparams{'ACTION'} eq 'update' )
224 &Header
::openbox
('100%', 'left', "$Lang::tr{'edit device'}");
225 $buttontext = $Lang::tr
{'update'};
229 &Header
::openbox
('100%', 'left', "$Lang::tr{'add device'}");
235 <td width='15%' class='base'>$Lang::tr{'mac address'}: </td>
236 <td width='40%'><input type='text' name='CLIENT_MAC' value='$cgiparams{'CLIENT_MAC'}' size='25' /></td>
237 <td width='10%' class='base'>$Lang::tr{'interface'}: </td>
239 <select name='CLIENT_IFACE'>
243 print "<option value='green' $selected{'CLIENT_IFACE'}{'green'}>$Lang::tr{'green'}</option>";
246 print "<option value='blue' $selected{'CLIENT_IFACE'}{'blue'}>$Lang::tr{'blue'}</option>";
248 if (&haveOrangeNet
())
250 print "<option value='orange' $selected{'CLIENT_IFACE'}{'orange'}>$Lang::tr{'orange'}</option>";
252 # red for some testing purposes only
253 # print "<option value='red' $selected{'CLIENT_IFACE'}{'red'}>$Lang::tr{'red'}</option>";
259 <td width='15%' class='base'>$Lang::tr{'remark'}: <img src='/blob.gif' alt='*' /></td>
260 <td colspan='4' align='left'><input type='text' name='CLIENT_COMMENT' value='$cgiparams{'CLIENT_COMMENT'}' size='40' /></td>
266 <td class='base' valign='top'><img src='/blob.gif' alt='*' /></td>
267 <td width='55%' class='base'>$Lang::tr{'this field may be blank'}</td>
268 <td width='40%' align='center'>
272 if ( ($cgiparams{'ACTION'} eq 'edit') || ($cgiparams{'ACTION'} eq 'update') )
274 print "<input type='hidden' name='ID' value='$cgiparams{'ID'}' />\n";
275 print "<input type='hidden' name='ACTION' value='update' />";
279 print "<input type='hidden' name='ACTION' value='add' />";
281 print "<input type='submit' name='SUBMIT' value='$buttontext' /></td></tr></table>";
287 #######################################
289 # now list already configured devivces
291 #######################################
292 &Header
::openbox
('100%', 'left', "$Lang::tr{'current devices'}");
295 <table width='100%' class='tbl'>
297 <th align='center' width='20%'><b>$Lang::tr{'mac address'}</b></th>
298 <th align='center' width='10%'><b>$Lang::tr{'interface'}</b></th>
299 <th align='center' width='60%'><b>$Lang::tr{'remark'}</b></th>
300 <th align='center' colspan='2'><b>$Lang::tr{'action'}</b></th>
306 for $i ( 0 .. $#wol_devices )
308 my $wol_mac = $wol_devices[$i]{'MAC'};
309 my $wol_iface = $wol_devices[$i]{'IFace'};
310 my $wol_txt = &Header
::cleanhtml
($wol_devices[$i]{'Comment'});
312 if ( (($cgiparams{'ACTION'} eq 'edit') || ($cgiparams{'ACTION'} eq 'update')) && ($i == $cgiparams{'ID'}) )
315 $col="bgcolor='${Header::colouryellow}'";
320 $col="bgcolor='$color{'color20'}'";
325 $col="bgcolor='$color{'color22'}'";
329 <td align='center' $col>$wol_mac</td>
330 <td align='center' $col>$Lang::tr{"$wol_iface"}</td>
331 <td align='left' $col>$wol_txt</td>
332 <td align='center' $col>
335 if ( (($wol_iface eq 'blue') && ! &haveBlueNet
())
336 || (($wol_iface eq 'orange') && ! &haveOrangeNet
()) )
338 # configured IFace (momentarily) not available -> now wakeup button/image
344 <form method='post' name='frma$i' action='$ENV{'SCRIPT_NAME'}'>
345 <input type='hidden' name='ACTION' value='wakeup' />
346 <input type='image' name='wakeup' src='/images/wakeup.gif' alt='$Lang::tr{'wol wakeup'}' title='$Lang::tr{'wol wakeup'}' />
347 <input type='hidden' name='ID' value='$i' />
354 <td align='center' $col>
355 <form method='post' name='frmb$i' action='$ENV{'SCRIPT_NAME'}'>
356 <input type='hidden' name='ACTION' value='edit' />
357 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
358 <input type='hidden' name='ID' value='$i' />
361 <td align='center' $col>
362 <form method='post' name='frmc$i' action='$ENV{'SCRIPT_NAME'}'>
363 <input type='hidden' name='ACTION' value='remove' />
364 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
365 <input type='hidden' name='ID' value='$i' />
377 &Header
::closebigbox
();
378 &Header
::closepage
();
381 # load the configuration file
386 # ID,MAC,IFACE,,Comment
389 if ( open(FILE
, "$datafile") )
397 # populate devices list
398 foreach $line ( @tmpfile )
400 chomp($line); # remove newline
401 my @temp = split(/\,/,$line,5);
402 if ( $temp[1] eq '' ) { next; }
403 unless(&General
::validmac
($temp[1])) { next; }
405 push @wol_devices, { ID
=> $temp[0], MAC
=> $temp[1], IFace
=> $temp[2], Comment
=> $temp[4] };
410 # write the configuration file
419 for $i ( 0 .. $#wol_devices )
421 unless(&General
::validmac
($wol_devices[$i]{'MAC'})) { next; }
422 unshift (@tmp_clients, uc($wol_devices[$i]{'MAC'}).",$wol_devices[$i]{'IFace'},,$wol_devices[$i]{'Comment'}");
425 # sort tmp_clients on MAC
426 @tmp_clients = sort ( @tmp_clients );
428 open(FILE
, ">$datafile") or die 'hosts datafile error';
431 foreach $line (@tmp_clients)
433 print FILE
"$count,$line\n";
443 # copied these from dmzholes.cgi (thnx dotzball)
444 # seems to be the way to do this :-S
448 if ($netsettings{'CONFIG_TYPE'} == 2) {return 1;}
449 if ($netsettings{'CONFIG_TYPE'} == 4) {return 1;}
455 if ($netsettings{'CONFIG_TYPE'} == 3) {return 1;}
456 if ($netsettings{'CONFIG_TYPE'} == 4) {return 1;}