#!/usr/bin/perl
###############################################################################
# #
# IPFire.org - A linux based firewall #
# Copyright (C) 2007 Michael Tremer & Christian Schmidt #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see . #
# #
###############################################################################
use strict;
# enable only the following on debugging purpose
#use warnings;
#use CGI::Carp 'fatalsToBrowser';
require '/var/ipfire/general-functions.pl';
require "${General::swroot}/lang.pl";
require "${General::swroot}/header.pl";
# remove comment from next line to get wakeup info in seperate page
my $refresh = 'yes';
# remove comment from next line to get wakeup info as inline box
#my $refresh = '';
#workaround to suppress a warning when a variable is used only once
my @dummy = ( ${Header::colouryellow} );
undef (@dummy);
my $line;
my $i;
my @wol_devices = ();
#configfile
our $datafile = "/var/ipfire/wakeonlan/clients.conf";
&ReadConfig;
my %color = ();
my %mainsettings = ();
&General::readhash("${General::swroot}/main/settings", \%mainsettings);
&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
my %netsettings = ();
&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
my %cgiparams = ();
$cgiparams{'ACTION'} = ''; # add/edit/update/remove/wakeup
$cgiparams{'ID'} = ''; # points to record for ACTION (edit/update/remove)
$cgiparams{'CLIENT_MAC'} = '';
$cgiparams{'CLIENT_IFACE'} = '';
$cgiparams{'CLIENT_COMMENT'} = '';
&Header::getcgihash(\%cgiparams);
my %selected = ();
$selected{'CLIENT_IFACE'}{'green'} = '';
$selected{'CLIENT_IFACE'}{'blue'} = '';
$selected{'CLIENT_IFACE'}{'orange'} = '';
$selected{'CLIENT_IFACE'}{'red'} = '';
&Header::showhttpheaders();
my $errormessage = "";
if ( $cgiparams{'ACTION'} eq 'add' )
{
# add a device, check for valid and non-duplicate MAC
if ( $cgiparams{'CLIENT_MAC'} eq '' )
{
goto ADDEXIT;
}
$cgiparams{'CLIENT_MAC'} =~ tr/-/:/;
unless( &General::validmac($cgiparams{'CLIENT_MAC'}) )
{
$errormessage = $Lang::tr{'invalid mac address'};
goto ADDEXIT;
}
for $i ( 0 .. $#wol_devices )
{
if ( lc($cgiparams{'CLIENT_MAC'}) eq lc($wol_devices[$i]{'MAC'}) )
{
$errormessage = $Lang::tr{'duplicate mac'};
goto ADDEXIT;
}
}
unless ( $errormessage )
{
push @wol_devices, { MAC => uc($cgiparams{'CLIENT_MAC'}), IFace => $cgiparams{'CLIENT_IFACE'}, Comment => $cgiparams{'CLIENT_COMMENT'} };
&WriteConfig;
undef %cgiparams;
}
ADDEXIT:
# jump here to keep cgiparams!
}
if ( $cgiparams{'ACTION'} eq 'update' )
{
# update a device, check for valid and non-duplicate MAC
if ( $cgiparams{'CLIENT_MAC'} eq '' )
{
goto UPDATEEXIT;
}
$cgiparams{'CLIENT_MAC'} =~ tr/-/:/;
unless( &General::validmac($cgiparams{'CLIENT_MAC'}) )
{
$errormessage = $Lang::tr{'invalid mac address'};
goto UPDATEEXIT;
}
for $i ( 0 .. $#wol_devices )
{
if ( $i == $cgiparams{'ID'} ) { next; }
if ( lc($cgiparams{'CLIENT_MAC'}) eq lc($wol_devices[$i]{'MAC'}) )
{
$errormessage = $Lang::tr{'duplicate mac'};
goto UPDATEEXIT;
}
}
unless ( $errormessage )
{
$wol_devices[$cgiparams{'ID'}]{'MAC'} = $cgiparams{'CLIENT_MAC'};
$wol_devices[$cgiparams{'ID'}]{'IFace'} = $cgiparams{'CLIENT_IFACE'};
$wol_devices[$cgiparams{'ID'}]{'Comment'} = $cgiparams{'CLIENT_COMMENT'};
&WriteConfig;
undef %cgiparams;
}
UPDATEEXIT:
# jump here to keep cgiparams!
}
if ( $cgiparams{'ACTION'} eq 'remove' )
{
# simply set MAC to empty, WriteConfig will handle the gory details
$wol_devices[$cgiparams{'ID'}]{'MAC'} = '';
&WriteConfig;
}
if ( ($cgiparams{'ACTION'} ne 'wakeup') || ($refresh ne 'yes') )
{
&Header::openpage($Lang::tr{'WakeOnLan'}, 1, '');
&Header::openbigbox('100%', 'left', '', $errormessage);
}
if ( $cgiparams{'ACTION'} eq 'wakeup' )
{
# wakey wakey
my $mac = $wol_devices[$cgiparams{'ID'}]{'MAC'};
my $iface = uc($wol_devices[$cgiparams{'ID'}]{'IFace'}).'_DEV';
$iface = $netsettings{"$iface"};
undef %cgiparams;
system("/usr/local/bin/launch-ether-wake $mac $iface");
# make a box with info, 'refresh' to normal screen after 5 seconds
if ( $refresh eq 'yes' )
{
&Header::openpage($Lang::tr{'WakeOnLan'}, 1, "$Lang::tr{'magic packet send to:'} $mac ($iface)