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