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