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