]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/wakeonlan.cgi
Merge remote-tracking branch 'origin/master' into next
[ipfire-2.x.git] / html / cgi-bin / wakeonlan.cgi
CommitLineData
4e565351 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###############################################################################
4e565351
MT
21
22use strict;
23
24# enable only the following on debugging purpose
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
27
28
29require '/var/ipfire/general-functions.pl';
30require "${General::swroot}/lang.pl";
31require "${General::swroot}/header.pl";
32
33
34# remove comment from next line to get wakeup info in seperate page
35my $refresh = 'yes';
36# remove comment from next line to get wakeup info as inline box
37#my $refresh = '';
38
39
40#workaround to suppress a warning when a variable is used only once
41my @dummy = ( ${Header::colouryellow} );
42undef (@dummy);
43my $line;
44my $i;
45
46my @wol_devices = ();
47#configfile
48our $datafile = "/var/ipfire/wakeonlan/clients.conf";
49&ReadConfig;
50
f2fdd0c1
CS
51my %color = ();
52my %mainsettings = ();
53&General::readhash("${General::swroot}/main/settings", \%mainsettings);
54&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
55
4e565351
MT
56my %netsettings = ();
57&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
58my %cgiparams = ();
59
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);
66
67my %selected = ();
68$selected{'CLIENT_IFACE'}{'green'} = '';
69$selected{'CLIENT_IFACE'}{'blue'} = '';
70$selected{'CLIENT_IFACE'}{'orange'} = '';
71$selected{'CLIENT_IFACE'}{'red'} = '';
72
73&Header::showhttpheaders();
74
75my $errormessage = "";
76
77if ( $cgiparams{'ACTION'} eq 'add' )
78{
79 # add a device, check for valid and non-duplicate MAC
80 if ( $cgiparams{'CLIENT_MAC'} eq '' )
81 {
82 goto ADDEXIT;
83 }
84
85 $cgiparams{'CLIENT_MAC'} =~ tr/-/:/;
86
87 unless( &General::validmac($cgiparams{'CLIENT_MAC'}) )
88 {
89 $errormessage = $Lang::tr{'invalid mac address'};
90 goto ADDEXIT;
91 }
92
93 for $i ( 0 .. $#wol_devices )
94 {
95 if ( lc($cgiparams{'CLIENT_MAC'}) eq lc($wol_devices[$i]{'MAC'}) )
96 {
97 $errormessage = $Lang::tr{'duplicate mac'};
98 goto ADDEXIT;
99 }
100 }
101
102 unless ( $errormessage )
103 {
104 push @wol_devices, { MAC => uc($cgiparams{'CLIENT_MAC'}), IFace => $cgiparams{'CLIENT_IFACE'}, Comment => $cgiparams{'CLIENT_COMMENT'} };
105 &WriteConfig;
106 undef %cgiparams;
107 }
108
109ADDEXIT:
110# jump here to keep cgiparams!
111}
112
113if ( $cgiparams{'ACTION'} eq 'update' )
114{
115 # update a device, check for valid and non-duplicate MAC
116 if ( $cgiparams{'CLIENT_MAC'} eq '' )
117 {
118 goto UPDATEEXIT;
119 }
120
121 $cgiparams{'CLIENT_MAC'} =~ tr/-/:/;
122
123 unless( &General::validmac($cgiparams{'CLIENT_MAC'}) )
124 {
125 $errormessage = $Lang::tr{'invalid mac address'};
126 goto UPDATEEXIT;
127 }
128
129 for $i ( 0 .. $#wol_devices )
130 {
131 if ( $i == $cgiparams{'ID'} ) { next; }
132 if ( lc($cgiparams{'CLIENT_MAC'}) eq lc($wol_devices[$i]{'MAC'}) )
133 {
134 $errormessage = $Lang::tr{'duplicate mac'};
135 goto UPDATEEXIT;
136 }
137 }
138
139 unless ( $errormessage )
140 {
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'};
144 &WriteConfig;
145 undef %cgiparams;
146 }
147
148UPDATEEXIT:
149# jump here to keep cgiparams!
150}
151
152if ( $cgiparams{'ACTION'} eq 'remove' )
153{
154 # simply set MAC to empty, WriteConfig will handle the gory details
155 $wol_devices[$cgiparams{'ID'}]{'MAC'} = '';
156 &WriteConfig;
157}
158
159if ( ($cgiparams{'ACTION'} ne 'wakeup') || ($refresh ne 'yes') )
160{
161 &Header::openpage($Lang::tr{'WakeOnLan'}, 1, '');
162 &Header::openbigbox('100%', 'left', '', $errormessage);
163}
164
165if ( $cgiparams{'ACTION'} eq 'wakeup' )
166{
167 # wakey wakey
168 my $mac = $wol_devices[$cgiparams{'ID'}]{'MAC'};
169 my $iface = uc($wol_devices[$cgiparams{'ID'}]{'IFace'}).'_DEV';
170 $iface = $netsettings{"$iface"};
171
172 undef %cgiparams;
173
e455cafe 174 system("/usr/local/bin/launch-ether-wake $mac $iface");
4e565351
MT
175
176 # make a box with info, 'refresh' to normal screen after 5 seconds
177 if ( $refresh eq 'yes' )
178 {
1fde937c 179 &Header::openpage($Lang::tr{'WakeOnLan'}, 1, "<meta http-equiv='refresh' content='3;url=/cgi-bin/wakeonlan.cgi'");
4e565351
MT
180 &Header::openbigbox('100%', 'left');
181 }
182 &Header::openbox('100%', 'left', $Lang::tr{'WakeOnLan'});
183 print "<p>$Lang::tr{'magic packet send to:'} $mac ($iface)</p>";
184 &Header::closebox();
185
186 if ( $refresh eq 'yes' )
187 {
188 &Header::closebigbox();
189 &Header::closepage();
190 # that's all folks
191 exit;
192 }
193}
194
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 />";
200
201if ( $errormessage )
202{
203 # some error from add / update
204 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
205 print "<class name='base'>$errormessage\n";
206 print "&nbsp;</class>\n";
207 &Header::closebox();
208}
209
210print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
211
212$selected{'CLIENT_IFACE'}{$cgiparams{'CLIENT_IFACE'}} = "selected='selected'";
213my $buttontext = $Lang::tr{'add'};
214if ( $cgiparams{'ACTION'} eq 'edit' )
215{
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'};
221}
222elsif ( $cgiparams{'ACTION'} eq 'update' )
223{
224 &Header::openbox('100%', 'left', "$Lang::tr{'edit device'}");
225 $buttontext = $Lang::tr{'update'};
226}
227else
228{
229 &Header::openbox('100%', 'left', "$Lang::tr{'add device'}");
230}
231
232print <<END
233<table width='100%'>
234<tr>
235 <td width='15%' class='base'>$Lang::tr{'mac address'}:&nbsp;</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'}:&nbsp;</td>
238 <td align='left'>
239 <select name='CLIENT_IFACE'>
240END
241;
242
243print "<option value='green' $selected{'CLIENT_IFACE'}{'green'}>$Lang::tr{'green'}</option>";
244if (&haveBlueNet())
245{
246 print "<option value='blue' $selected{'CLIENT_IFACE'}{'blue'}>$Lang::tr{'blue'}</option>";
247}
248if (&haveOrangeNet())
249{
250 print "<option value='orange' $selected{'CLIENT_IFACE'}{'orange'}>$Lang::tr{'orange'}</option>";
251}
252# red for some testing purposes only
253# print "<option value='red' $selected{'CLIENT_IFACE'}{'red'}>$Lang::tr{'red'}</option>";
254print <<END
255 </select>
256 </td>
257</tr>
258<tr>
259 <td width='15%' class='base'>$Lang::tr{'remark'}:&nbsp;<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>
261</tr>
262</table>
86048afb 263<br>
4e565351
MT
264<hr />
265<table width='100%'>
266<tr>
86048afb
AM
267 <td class='base' valign='top'><img src='/blob.gif' alt='*' />$Lang::tr{'this field may be blank'}</td>
268 <td width='40%' align='right'>
4e565351
MT
269END
270;
271
272if ( ($cgiparams{'ACTION'} eq 'edit') || ($cgiparams{'ACTION'} eq 'update') )
273{
274 print "<input type='hidden' name='ID' value='$cgiparams{'ID'}' />\n";
275 print "<input type='hidden' name='ACTION' value='update' />";
276}
277else
278{
279 print "<input type='hidden' name='ACTION' value='add' />";
280}
281print "<input type='submit' name='SUBMIT' value='$buttontext' /></td></tr></table>";
282
283&Header::closebox();
284
285print "</form>\n";
286
287#######################################
288#
289# now list already configured devivces
290#
291#######################################
292&Header::openbox('100%', 'left', "$Lang::tr{'current devices'}");
293
294print <<END
c380ac83 295<table width='100%' class='tbl'>
4e565351 296<tr>
c380ac83
AM
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>
301<th></th>
4e565351
MT
302</tr>
303END
304;
c380ac83 305my $col="";
4e565351
MT
306for $i ( 0 .. $#wol_devices )
307{
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'});
311
312 if ( (($cgiparams{'ACTION'} eq 'edit') || ($cgiparams{'ACTION'} eq 'update')) && ($i == $cgiparams{'ID'}) )
313 {
c380ac83
AM
314 print "<tr>";
315 $col="bgcolor='${Header::colouryellow}'";
4e565351
MT
316 }
317 elsif ( $i % 2)
318 {
c380ac83
AM
319 print "<tr>";
320 $col="bgcolor='$color{'color20'}'";
4e565351 321 }
c380ac83 322 else
4e565351 323 {
c380ac83
AM
324 print "<tr>";
325 $col="bgcolor='$color{'color22'}'";
4e565351
MT
326 }
327
328 print <<END
c380ac83
AM
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>
4e565351
MT
333END
334;
335 if ( (($wol_iface eq 'blue') && ! &haveBlueNet())
336 || (($wol_iface eq 'orange') && ! &haveOrangeNet()) )
337 {
338 # configured IFace (momentarily) not available -> now wakeup button/image
339 print "&nbsp;";
340 }
341 else
342 {
343 print <<END
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' />
348</form>
349END
350;
351 }
352 print <<END
353</td>
c380ac83 354<td align='center' $col>
4e565351
MT
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' />
359 </form>
360</td>
c380ac83 361<td align='center' $col>
4e565351
MT
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' />
366 </form>
367</td>
368END
369;
370 print "</tr>\n";
371}
372
373print "</table>";
374
375&Header::closebox();
376
377&Header::closebigbox();
378&Header::closepage();
379
380#
381# load the configuration file
382#
383sub ReadConfig
384{
385 # datafileformat:
386 # ID,MAC,IFACE,,Comment
387 #
388 my @tmpfile = ();
389 if ( open(FILE, "$datafile") )
390 {
391 @tmpfile = <FILE>;
392 close (FILE);
393 }
394
395 @wol_devices = ();
396
397 # populate devices list
398 foreach $line ( @tmpfile )
399 {
400 chomp($line); # remove newline
401 my @temp = split(/\,/,$line,5);
402 if ( $temp[1] eq '' ) { next; }
403 unless(&General::validmac($temp[1])) { next; }
404
405 push @wol_devices, { ID => $temp[0], MAC => $temp[1], IFace => $temp[2], Comment => $temp[4] };
406 }
407}
408
409#
410# write the configuration file
411#
412sub WriteConfig
413{
414 my $line;
415 my @temp;
416
417 my @tmp_clients;
418
419 for $i ( 0 .. $#wol_devices )
420 {
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'}");
423 }
424
425 # sort tmp_clients on MAC
426 @tmp_clients = sort ( @tmp_clients );
427
428 open(FILE, ">$datafile") or die 'hosts datafile error';
429
430 my $count = 0;
431 foreach $line (@tmp_clients)
432 {
433 print FILE "$count,$line\n";
434 $count++;
435 }
436 close FILE;
437
438 &ReadConfig;
439}
440
441
442#
443# copied these from dmzholes.cgi (thnx dotzball)
444# seems to be the way to do this :-S
445#
446sub haveOrangeNet
447{
40d0c2eb
CS
448 if ($netsettings{'CONFIG_TYPE'} == 2) {return 1;}
449 if ($netsettings{'CONFIG_TYPE'} == 4) {return 1;}
4e565351
MT
450 return 0;
451}
452
453sub haveBlueNet
454{
40d0c2eb 455 if ($netsettings{'CONFIG_TYPE'} == 3) {return 1;}
4e565351 456 if ($netsettings{'CONFIG_TYPE'} == 4) {return 1;}
4e565351
MT
457 return 0;
458}