]>
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 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
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);
52 $selected{'CLIENT_IFACE'}{'green'} = '';
53 $selected{'CLIENT_IFACE'}{'blue'} = '';
54 $selected{'CLIENT_IFACE'}{'orange'} = '';
55 $selected{'CLIENT_IFACE'}{'red'} = '';
57 &Header
::showhttpheaders
();
59 my $errormessage = "";
61 if ( $cgiparams{'ACTION'} eq 'add' )
63 # add a device, check for valid and non-duplicate MAC
64 if ( $cgiparams{'CLIENT_MAC'} eq '' )
69 $cgiparams{'CLIENT_MAC'} =~ tr/-/:/;
71 unless( &General
::validmac
($cgiparams{'CLIENT_MAC'}) )
73 $errormessage = $Lang::tr
{'invalid mac address'};
77 for $i ( 0 .. $#wol_devices )
79 if ( lc($cgiparams{'CLIENT_MAC'}) eq lc($wol_devices[$i]{'MAC'}) )
81 $errormessage = $Lang::tr
{'duplicate mac'};
86 unless ( $errormessage )
88 push @wol_devices, { MAC
=> uc($cgiparams{'CLIENT_MAC'}), IFace
=> $cgiparams{'CLIENT_IFACE'}, Comment
=> $cgiparams{'CLIENT_COMMENT'} };
94 # jump here to keep cgiparams!
97 if ( $cgiparams{'ACTION'} eq 'update' )
99 # update a device, check for valid and non-duplicate MAC
100 if ( $cgiparams{'CLIENT_MAC'} eq '' )
105 $cgiparams{'CLIENT_MAC'} =~ tr/-/:/;
107 unless( &General
::validmac
($cgiparams{'CLIENT_MAC'}) )
109 $errormessage = $Lang::tr
{'invalid mac address'};
113 for $i ( 0 .. $#wol_devices )
115 if ( $i == $cgiparams{'ID'} ) { next; }
116 if ( lc($cgiparams{'CLIENT_MAC'}) eq lc($wol_devices[$i]{'MAC'}) )
118 $errormessage = $Lang::tr
{'duplicate mac'};
123 unless ( $errormessage )
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'};
133 # jump here to keep cgiparams!
136 if ( $cgiparams{'ACTION'} eq 'remove' )
138 # simply set MAC to empty, WriteConfig will handle the gory details
139 $wol_devices[$cgiparams{'ID'}]{'MAC'} = '';
143 if ( ($cgiparams{'ACTION'} ne 'wakeup') || ($refresh ne 'yes') )
145 &Header
::openpage
($Lang::tr
{'WakeOnLan'}, 1, '');
146 &Header
::openbigbox
('100%', 'left', '', $errormessage);
149 if ( $cgiparams{'ACTION'} eq 'wakeup' )
152 my $mac = $wol_devices[$cgiparams{'ID'}]{'MAC'};
153 my $iface = uc($wol_devices[$cgiparams{'ID'}]{'IFace'}).'_DEV';
154 $iface = $netsettings{"$iface"};
158 system("/usr/local/bin/launch-ether-wake $mac $iface");
160 # make a box with info, 'refresh' to normal screen after 5 seconds
161 if ( $refresh eq 'yes' )
163 &Header
::openpage
($Lang::tr
{'WakeOnLan'}, 1, "<meta http-equiv='refresh' content='3;url=/cgi-bin/wakeonlan.cgi'");
164 &Header
::openbigbox
('100%', 'left');
166 &Header
::openbox
('100%', 'left', $Lang::tr
{'WakeOnLan'});
167 print "<p>$Lang::tr{'magic packet send to:'} $mac ($iface)</p>";
170 if ( $refresh eq 'yes' )
172 &Header
::closebigbox
();
173 &Header
::closepage
();
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 />";
187 # some error from add / update
188 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
189 print "<class name='base'>$errormessage\n";
190 print " </class>\n";
194 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
196 $selected{'CLIENT_IFACE'}{$cgiparams{'CLIENT_IFACE'}} = "selected='selected'";
197 my $buttontext = $Lang::tr
{'add'};
198 if ( $cgiparams{'ACTION'} eq 'edit' )
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'};
206 elsif ( $cgiparams{'ACTION'} eq 'update' )
208 &Header
::openbox
('100%', 'left', "$Lang::tr{'edit device'}");
209 $buttontext = $Lang::tr
{'update'};
213 &Header
::openbox
('100%', 'left', "$Lang::tr{'add device'}");
219 <td width='15%' class='base'>$Lang::tr{'mac address'}: </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'}: </td>
223 <select name='CLIENT_IFACE'>
227 print "<option value='green' $selected{'CLIENT_IFACE'}{'green'}>$Lang::tr{'green'}</option>";
230 print "<option value='blue' $selected{'CLIENT_IFACE'}{'blue'}>$Lang::tr{'blue'}</option>";
232 if (&haveOrangeNet
())
234 print "<option value='orange' $selected{'CLIENT_IFACE'}{'orange'}>$Lang::tr{'orange'}</option>";
236 # red for some testing purposes only
237 # print "<option value='red' $selected{'CLIENT_IFACE'}{'red'}>$Lang::tr{'red'}</option>";
243 <td width='15%' class='base'>$Lang::tr{'remark'}: <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>
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'>
256 if ( ($cgiparams{'ACTION'} eq 'edit') || ($cgiparams{'ACTION'} eq 'update') )
258 print "<input type='hidden' name='ID' value='$cgiparams{'ID'}' />\n";
259 print "<input type='hidden' name='ACTION' value='update' />";
263 print "<input type='hidden' name='ACTION' value='add' />";
265 print "<input type='submit' name='SUBMIT' value='$buttontext' /></td></tr></table>";
271 #######################################
273 # now list already configured devivces
275 #######################################
276 &Header
::openbox
('100%', 'left', "$Lang::tr{'current devices'}");
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>
289 for $i ( 0 .. $#wol_devices )
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'});
295 if ( (($cgiparams{'ACTION'} eq 'edit') || ($cgiparams{'ACTION'} eq 'update')) && ($i == $cgiparams{'ID'}) )
297 print "<tr bgcolor='${Header::colouryellow}'>";
301 print "<tr bgcolor='$Header::table2colour'>";
305 print "<tr bgcolor='$Header::table1colour'>";
309 <td align='center'>$wol_mac</td>
310 <td align='center'>$Lang::tr{"$wol_iface"}</td>
311 <td align='left'>$wol_txt</td>
315 if ( (($wol_iface eq 'blue') && ! &haveBlueNet
())
316 || (($wol_iface eq 'orange') && ! &haveOrangeNet
()) )
318 # configured IFace (momentarily) not available -> now wakeup button/image
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' />
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' />
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' />
357 &Header
::closebigbox
();
358 &Header
::closepage
();
361 # load the configuration file
366 # ID,MAC,IFACE,,Comment
369 if ( open(FILE
, "$datafile") )
377 # populate devices list
378 foreach $line ( @tmpfile )
380 chomp($line); # remove newline
381 my @temp = split(/\,/,$line,5);
382 if ( $temp[1] eq '' ) { next; }
383 unless(&General
::validmac
($temp[1])) { next; }
385 push @wol_devices, { ID
=> $temp[0], MAC
=> $temp[1], IFace
=> $temp[2], Comment
=> $temp[4] };
390 # write the configuration file
399 for $i ( 0 .. $#wol_devices )
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'}");
405 # sort tmp_clients on MAC
406 @tmp_clients = sort ( @tmp_clients );
408 open(FILE
, ">$datafile") or die 'hosts datafile error';
411 foreach $line (@tmp_clients)
413 print FILE
"$count,$line\n";
423 # copied these from dmzholes.cgi (thnx dotzball)
424 # seems to be the way to do this :-S
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;}
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;}