]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/wakeonlan.cgi
3 # IPFire WakeOnLan-AddOn CGI
5 # This code is distributed under the terms of the GPL
10 # enable only the following on debugging purpose
12 #use CGI::Carp 'fatalsToBrowser';
15 require '/var/ipfire/general-functions.pl';
16 require "${General::swroot}/lang.pl";
17 require "${General::swroot}/header.pl";
20 # remove comment from next line to get wakeup info in seperate page
22 # remove comment from next line to get wakeup info as inline box
26 #workaround to suppress a warning when a variable is used only once
27 my @dummy = ( ${Header
::colouryellow
} );
34 our $datafile = "/var/ipfire/wakeonlan/clients.conf";
38 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
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);
49 $selected{'CLIENT_IFACE'}{'green'} = '';
50 $selected{'CLIENT_IFACE'}{'blue'} = '';
51 $selected{'CLIENT_IFACE'}{'orange'} = '';
52 $selected{'CLIENT_IFACE'}{'red'} = '';
54 &Header
::showhttpheaders
();
56 my $errormessage = "";
58 if ( $cgiparams{'ACTION'} eq 'add' )
60 # add a device, check for valid and non-duplicate MAC
61 if ( $cgiparams{'CLIENT_MAC'} eq '' )
66 $cgiparams{'CLIENT_MAC'} =~ tr/-/:/;
68 unless( &General
::validmac
($cgiparams{'CLIENT_MAC'}) )
70 $errormessage = $Lang::tr
{'invalid mac address'};
74 for $i ( 0 .. $#wol_devices )
76 if ( lc($cgiparams{'CLIENT_MAC'}) eq lc($wol_devices[$i]{'MAC'}) )
78 $errormessage = $Lang::tr
{'duplicate mac'};
83 unless ( $errormessage )
85 push @wol_devices, { MAC
=> uc($cgiparams{'CLIENT_MAC'}), IFace
=> $cgiparams{'CLIENT_IFACE'}, Comment
=> $cgiparams{'CLIENT_COMMENT'} };
91 # jump here to keep cgiparams!
94 if ( $cgiparams{'ACTION'} eq 'update' )
96 # update a device, check for valid and non-duplicate MAC
97 if ( $cgiparams{'CLIENT_MAC'} eq '' )
102 $cgiparams{'CLIENT_MAC'} =~ tr/-/:/;
104 unless( &General
::validmac
($cgiparams{'CLIENT_MAC'}) )
106 $errormessage = $Lang::tr
{'invalid mac address'};
110 for $i ( 0 .. $#wol_devices )
112 if ( $i == $cgiparams{'ID'} ) { next; }
113 if ( lc($cgiparams{'CLIENT_MAC'}) eq lc($wol_devices[$i]{'MAC'}) )
115 $errormessage = $Lang::tr
{'duplicate mac'};
120 unless ( $errormessage )
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'};
130 # jump here to keep cgiparams!
133 if ( $cgiparams{'ACTION'} eq 'remove' )
135 # simply set MAC to empty, WriteConfig will handle the gory details
136 $wol_devices[$cgiparams{'ID'}]{'MAC'} = '';
140 if ( ($cgiparams{'ACTION'} ne 'wakeup') || ($refresh ne 'yes') )
142 &Header
::openpage
($Lang::tr
{'WakeOnLan'}, 1, '');
143 &Header
::openbigbox
('100%', 'left', '', $errormessage);
146 if ( $cgiparams{'ACTION'} eq 'wakeup' )
149 my $mac = $wol_devices[$cgiparams{'ID'}]{'MAC'};
150 my $iface = uc($wol_devices[$cgiparams{'ID'}]{'IFace'}).'_DEV';
151 $iface = $netsettings{"$iface"};
155 system("/usr/local/sbin/launch-ether-wake $mac $iface");
157 # make a box with info, 'refresh' to normal screen after 5 seconds
158 if ( $refresh eq 'yes' )
160 &Header
::openpage
($Lang::tr
{'WakeOnLan'}, 1, "<meta http-equiv='refresh' content='3;url=/cgi-bin/wakeonlan.cgi'");
161 &Header
::openbigbox
('100%', 'left');
163 &Header
::openbox
('100%', 'left', $Lang::tr
{'WakeOnLan'});
164 print "<p>$Lang::tr{'magic packet send to:'} $mac ($iface)</p>";
167 if ( $refresh eq 'yes' )
169 &Header
::closebigbox
();
170 &Header
::closepage
();
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 />";
184 # some error from add / update
185 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
186 print "<class name='base'>$errormessage\n";
187 print " </class>\n";
191 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
193 $selected{'CLIENT_IFACE'}{$cgiparams{'CLIENT_IFACE'}} = "selected='selected'";
194 my $buttontext = $Lang::tr
{'add'};
195 if ( $cgiparams{'ACTION'} eq 'edit' )
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'};
203 elsif ( $cgiparams{'ACTION'} eq 'update' )
205 &Header
::openbox
('100%', 'left', "$Lang::tr{'edit device'}");
206 $buttontext = $Lang::tr
{'update'};
210 &Header
::openbox
('100%', 'left', "$Lang::tr{'add device'}");
216 <td width='15%' class='base'>$Lang::tr{'mac address'}: </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'}: </td>
220 <select name='CLIENT_IFACE'>
224 print "<option value='green' $selected{'CLIENT_IFACE'}{'green'}>$Lang::tr{'green'}</option>";
227 print "<option value='blue' $selected{'CLIENT_IFACE'}{'blue'}>$Lang::tr{'blue'}</option>";
229 if (&haveOrangeNet
())
231 print "<option value='orange' $selected{'CLIENT_IFACE'}{'orange'}>$Lang::tr{'orange'}</option>";
233 # red for some testing purposes only
234 # print "<option value='red' $selected{'CLIENT_IFACE'}{'red'}>$Lang::tr{'red'}</option>";
240 <td width='15%' class='base'>$Lang::tr{'remark'}: <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>
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'>
253 if ( ($cgiparams{'ACTION'} eq 'edit') || ($cgiparams{'ACTION'} eq 'update') )
255 print "<input type='hidden' name='ID' value='$cgiparams{'ID'}' />\n";
256 print "<input type='hidden' name='ACTION' value='update' />";
260 print "<input type='hidden' name='ACTION' value='add' />";
262 print "<input type='submit' name='SUBMIT' value='$buttontext' /></td></tr></table>";
268 #######################################
270 # now list already configured devivces
272 #######################################
273 &Header
::openbox
('100%', 'left', "$Lang::tr{'current devices'}");
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>
286 for $i ( 0 .. $#wol_devices )
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'});
292 if ( (($cgiparams{'ACTION'} eq 'edit') || ($cgiparams{'ACTION'} eq 'update')) && ($i == $cgiparams{'ID'}) )
294 print "<tr bgcolor='${Header::colouryellow}'>";
298 print "<tr bgcolor='$Header::table2colour'>";
302 print "<tr bgcolor='$Header::table1colour'>";
306 <td align='center'>$wol_mac</td>
307 <td align='center'>$Lang::tr{"$wol_iface"}</td>
308 <td align='left'>$wol_txt</td>
312 if ( (($wol_iface eq 'blue') && ! &haveBlueNet
())
313 || (($wol_iface eq 'orange') && ! &haveOrangeNet
()) )
315 # configured IFace (momentarily) not available -> now wakeup button/image
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' />
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' />
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' />
354 &Header
::closebigbox
();
355 &Header
::closepage
();
358 # load the configuration file
363 # ID,MAC,IFACE,,Comment
366 if ( open(FILE
, "$datafile") )
374 # populate devices list
375 foreach $line ( @tmpfile )
377 chomp($line); # remove newline
378 my @temp = split(/\,/,$line,5);
379 if ( $temp[1] eq '' ) { next; }
380 unless(&General
::validmac
($temp[1])) { next; }
382 push @wol_devices, { ID
=> $temp[0], MAC
=> $temp[1], IFace
=> $temp[2], Comment
=> $temp[4] };
387 # write the configuration file
396 for $i ( 0 .. $#wol_devices )
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'}");
402 # sort tmp_clients on MAC
403 @tmp_clients = sort ( @tmp_clients );
405 open(FILE
, ">$datafile") or die 'hosts datafile error';
408 foreach $line (@tmp_clients)
410 print FILE
"$count,$line\n";
420 # copied these from dmzholes.cgi (thnx dotzball)
421 # seems to be the way to do this :-S
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;}
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;}