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