]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/wakeonlan.cgi
3 # IPFire WakeOnLan-AddOn CGI
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
8 # This code is distributed under the terms of the GPL
13 # enable only the following on debugging purpose
15 #use CGI::Carp 'fatalsToBrowser';
18 require '/var/ipfire/general-functions.pl';
19 require "${General::swroot}/lang.pl";
20 require "${General::swroot}/header.pl";
23 # remove comment from next line to get wakeup info in seperate page
25 # remove comment from next line to get wakeup info as inline box
29 #workaround to suppress a warning when a variable is used only once
30 my @dummy = ( ${Header
::colouryellow
} );
37 our $datafile = "/var/ipfire/wakeonlan/clients.conf";
41 my %mainsettings = ();
42 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
43 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
46 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
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);
57 $selected{'CLIENT_IFACE'}{'green'} = '';
58 $selected{'CLIENT_IFACE'}{'blue'} = '';
59 $selected{'CLIENT_IFACE'}{'orange'} = '';
60 $selected{'CLIENT_IFACE'}{'red'} = '';
62 &Header
::showhttpheaders
();
64 my $errormessage = "";
66 if ( $cgiparams{'ACTION'} eq 'add' )
68 # add a device, check for valid and non-duplicate MAC
69 if ( $cgiparams{'CLIENT_MAC'} eq '' )
74 $cgiparams{'CLIENT_MAC'} =~ tr/-/:/;
76 unless( &General
::validmac
($cgiparams{'CLIENT_MAC'}) )
78 $errormessage = $Lang::tr
{'invalid mac address'};
82 for $i ( 0 .. $#wol_devices )
84 if ( lc($cgiparams{'CLIENT_MAC'}) eq lc($wol_devices[$i]{'MAC'}) )
86 $errormessage = $Lang::tr
{'duplicate mac'};
91 unless ( $errormessage )
93 push @wol_devices, { MAC
=> uc($cgiparams{'CLIENT_MAC'}), IFace
=> $cgiparams{'CLIENT_IFACE'}, Comment
=> $cgiparams{'CLIENT_COMMENT'} };
99 # jump here to keep cgiparams!
102 if ( $cgiparams{'ACTION'} eq 'update' )
104 # update a device, check for valid and non-duplicate MAC
105 if ( $cgiparams{'CLIENT_MAC'} eq '' )
110 $cgiparams{'CLIENT_MAC'} =~ tr/-/:/;
112 unless( &General
::validmac
($cgiparams{'CLIENT_MAC'}) )
114 $errormessage = $Lang::tr
{'invalid mac address'};
118 for $i ( 0 .. $#wol_devices )
120 if ( $i == $cgiparams{'ID'} ) { next; }
121 if ( lc($cgiparams{'CLIENT_MAC'}) eq lc($wol_devices[$i]{'MAC'}) )
123 $errormessage = $Lang::tr
{'duplicate mac'};
128 unless ( $errormessage )
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'};
138 # jump here to keep cgiparams!
141 if ( $cgiparams{'ACTION'} eq 'remove' )
143 # simply set MAC to empty, WriteConfig will handle the gory details
144 $wol_devices[$cgiparams{'ID'}]{'MAC'} = '';
148 if ( ($cgiparams{'ACTION'} ne 'wakeup') || ($refresh ne 'yes') )
150 &Header
::openpage
($Lang::tr
{'WakeOnLan'}, 1, '');
151 &Header
::openbigbox
('100%', 'left', '', $errormessage);
154 if ( $cgiparams{'ACTION'} eq 'wakeup' )
157 my $mac = $wol_devices[$cgiparams{'ID'}]{'MAC'};
158 my $iface = uc($wol_devices[$cgiparams{'ID'}]{'IFace'}).'_DEV';
159 $iface = $netsettings{"$iface"};
163 system("/usr/local/bin/launch-ether-wake $mac $iface");
165 # make a box with info, 'refresh' to normal screen after 5 seconds
166 if ( $refresh eq 'yes' )
168 &Header
::openpage
($Lang::tr
{'WakeOnLan'}, 1, "<meta http-equiv='refresh' content='3;url=/cgi-bin/wakeonlan.cgi'");
169 &Header
::openbigbox
('100%', 'left');
171 &Header
::openbox
('100%', 'left', $Lang::tr
{'WakeOnLan'});
172 print "<p>$Lang::tr{'magic packet send to:'} $mac ($iface)</p>";
175 if ( $refresh eq 'yes' )
177 &Header
::closebigbox
();
178 &Header
::closepage
();
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 />";
192 # some error from add / update
193 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
194 print "<class name='base'>$errormessage\n";
195 print " </class>\n";
199 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
201 $selected{'CLIENT_IFACE'}{$cgiparams{'CLIENT_IFACE'}} = "selected='selected'";
202 my $buttontext = $Lang::tr
{'add'};
203 if ( $cgiparams{'ACTION'} eq 'edit' )
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'};
211 elsif ( $cgiparams{'ACTION'} eq 'update' )
213 &Header
::openbox
('100%', 'left', "$Lang::tr{'edit device'}");
214 $buttontext = $Lang::tr
{'update'};
218 &Header
::openbox
('100%', 'left', "$Lang::tr{'add device'}");
224 <td width='15%' class='base'>$Lang::tr{'mac address'}: </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'}: </td>
228 <select name='CLIENT_IFACE'>
232 print "<option value='green' $selected{'CLIENT_IFACE'}{'green'}>$Lang::tr{'green'}</option>";
235 print "<option value='blue' $selected{'CLIENT_IFACE'}{'blue'}>$Lang::tr{'blue'}</option>";
237 if (&haveOrangeNet
())
239 print "<option value='orange' $selected{'CLIENT_IFACE'}{'orange'}>$Lang::tr{'orange'}</option>";
241 # red for some testing purposes only
242 # print "<option value='red' $selected{'CLIENT_IFACE'}{'red'}>$Lang::tr{'red'}</option>";
248 <td width='15%' class='base'>$Lang::tr{'remark'}: <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>
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'>
261 if ( ($cgiparams{'ACTION'} eq 'edit') || ($cgiparams{'ACTION'} eq 'update') )
263 print "<input type='hidden' name='ID' value='$cgiparams{'ID'}' />\n";
264 print "<input type='hidden' name='ACTION' value='update' />";
268 print "<input type='hidden' name='ACTION' value='add' />";
270 print "<input type='submit' name='SUBMIT' value='$buttontext' /></td></tr></table>";
276 #######################################
278 # now list already configured devivces
280 #######################################
281 &Header
::openbox
('100%', 'left', "$Lang::tr{'current devices'}");
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>
294 for $i ( 0 .. $#wol_devices )
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'});
300 if ( (($cgiparams{'ACTION'} eq 'edit') || ($cgiparams{'ACTION'} eq 'update')) && ($i == $cgiparams{'ID'}) )
302 print "<tr bgcolor='${Header::colouryellow}'>";
306 print "<tr bgcolor='$color{'color22'}'>";
310 print "<tr bgcolor='$color{'color20'}'>";
314 <td align='center'>$wol_mac</td>
315 <td align='center'>$Lang::tr{"$wol_iface"}</td>
316 <td align='left'>$wol_txt</td>
320 if ( (($wol_iface eq 'blue') && ! &haveBlueNet
())
321 || (($wol_iface eq 'orange') && ! &haveOrangeNet
()) )
323 # configured IFace (momentarily) not available -> now wakeup button/image
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' />
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' />
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' />
362 &Header
::closebigbox
();
363 &Header
::closepage
();
366 # load the configuration file
371 # ID,MAC,IFACE,,Comment
374 if ( open(FILE
, "$datafile") )
382 # populate devices list
383 foreach $line ( @tmpfile )
385 chomp($line); # remove newline
386 my @temp = split(/\,/,$line,5);
387 if ( $temp[1] eq '' ) { next; }
388 unless(&General
::validmac
($temp[1])) { next; }
390 push @wol_devices, { ID
=> $temp[0], MAC
=> $temp[1], IFace
=> $temp[2], Comment
=> $temp[4] };
395 # write the configuration file
404 for $i ( 0 .. $#wol_devices )
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'}");
410 # sort tmp_clients on MAC
411 @tmp_clients = sort ( @tmp_clients );
413 open(FILE
, ">$datafile") or die 'hosts datafile error';
416 foreach $line (@tmp_clients)
418 print FILE
"$count,$line\n";
428 # copied these from dmzholes.cgi (thnx dotzball)
429 # seems to be the way to do this :-S
433 if ($netsettings{'CONFIG_TYPE'} == 2) {return 1;}
434 if ($netsettings{'CONFIG_TYPE'} == 4) {return 1;}
440 if ($netsettings{'CONFIG_TYPE'} == 3) {return 1;}
441 if ($netsettings{'CONFIG_TYPE'} == 4) {return 1;}