/opt/jp-secure/siteguardlite
Edit: /opt/jp-secure/siteguardlite/Makefile.verup-config (9035B)
#!/usr/bin/perl
#
# force to set default setting for "systemvars" items.
%systemvars = ('http_pass_user_agent_system_list' => '');
sub gen_waf_cookie_key
{
my $rand256;
open(RAND, "){
if(/^\[(.+)\]/){
push(@services, $1);
}
}
close(FH);
open(FH,"<$dstfile") || die "cannot open [$dstfile]:$!";
while(
){
if(/^\[(.+)\]/){
@services = grep{$1 ne $_} @services;
}
}
close(FH);
return @services;
}
sub read_vars
{
local($default_inifile)=@_;
local($proto)="global";
local(@vars);
open(FH, "<$default_inifile") || die "cannot open [$default_inifile]: $!";
while(){
if(/^\[(.+)\]/){$proto=$1;}
if(/^([\w\d]+)=(.*)/){
local($key, $cont) = ($1, $2);
push(@vars, "${proto}_${key}");
push(@vars, "$cont");
}
}
close(FH);
return @vars;
}
sub subst_keys
{
local($srcfile, $dstfile) = @_;
open(FH_R,"$srcfile") || die "cannot read open [$srcfile]: $!\n";
open(FH_W,">$dstfile") || die "cannot write open [$dstfile]: $!\n";
while(){
s/^(admin|http|ftp|smtp|pop)scan=/$1_service=/;
s/^virus_check_exclude_user_agent/pass_user_agent/;
s/^drac=/pbs=/;
s/^dracdb_expire=/pbs_lifetime=/;
s/^remote_daemon/parent_server/;
s/^(\w+)_lan([_=])/lan_$1$2/;
s/^acl=/acl_from=/;
s/^acl_parent=/acl_to=/;
print FH_W;
}
close(FH_W);
close(FH_R);
}
sub filecopy
{
local($infile, $outfile) = @_;
open(FH_R,"<$infile") || die "cannot read open ${infile}:$!";
open(FH_W,">$outfile") || die "cannot write open ${outfile}:$!";
while(){
print FH_W;
}
close(FH_R);
close(FH_W);
}
sub insert_vars
{
local($proto) = @_;
local($i);
for($i=0;$i<@newvars/2;$i++){
local($key,$cont)=($newvars[$i*2],$newvars[$i*2+1]);
if($key =~ /^(${proto}|all)_(.*)/){
if(($proto eq "global") && ($1 ne "global")){next;}
local($key2) = "${proto}_$2";
# print "newvars=($key,$cont), vars=($key2, $vars{$key2})\n";
if(! defined($vars{${key2}})){
if(($2 eq 'lan_quarantine') && defined($vars{'smtp_quarantine'})){
$cont = $vars{'smtp_quarantine'};
}elsif(($2 eq 'lan_notify_admin') && defined($vars{'smtp_notify_admin'})){
$cont = $vars{'smtp_notify_admin'};
}
print FH_W "$2=$cont\n";
}
}
}
}
sub verup_config
{
local($srcfile, $dstfile) = @_;
local($proto);
open(FH_R,"<${srcfile}") || die "cannot read open [${srcfile}]: $$!\n";
open(FH_W,">${dstfile}") || die "cannot write open [${dstfile}]: $$!\n";
# &insert_vars("global");
$proto="global";
while(){
if(/^\[(.+)\]/){
print FH_W;
$proto=$1;
# &insert_vars($proto);
}elsif(/^([^=]+)=/){
local($key)=$1;
local($i);
local($key2, $cont2);
while(($key2,$cont2) = each(%systemvars)){
if($key2 =~ /^(${proto}|all)_(${key})$/){
if(($proto eq "global") && ($1 ne "global")){break;}
$_ = "${key}=${cont2}\n";
}
}
print FH_W;
}else{
print FH_W;
}
}
#foreach $s(@newservices){
# print FH_W "[$s]\n";
# &insert_vars($s);
# print FH_W "\n";
#}
close(FH_R);
close(FH_W);
}
sub adjust_config
{
local($srcfile, $dstfile) = @_;
local($proto);
open(FH_R,"<${srcfile}") || die "cannot read open [${srcfile}]: $$!\n";
open(FH_W,">${dstfile}") || die "cannot write open [${dstfile}]: $$!\n";
$proto = "global";
while(){
if(/^\[(.+)\]/){
print FH_W;
$proto=$1;
}elsif(($proto eq "admin") && /^lang=(.*)/){
print FH_W "lang=${default_lang}\n";
}elsif(($proto eq "admin") && /^directoryindex=(.*)/){
local($val) = $1;
if(${default_lang} ne "en"){
if($val !~ /index${middleindex}\.${default_lang}\.html/
&& $val !~ /NO_OVERWRITE/){
s/(^|=|,)(index${middleindex}.html)(,|$)/$1index${middleindex}.${default_lang}.html,$2$3/;
}
}
print FH_W;
}elsif(($proto eq "global") && /^version=/ && ${newversion} ne ""){
print FH_W "version=${newversion}\n";
}else{
print FH_W;
}
}
close(FH_R);
close(FH_W);
}
sub diff_ini
{
local($inifile, $default_inifile) = @_;
local(%default_vars);
local(*FH);
local($proto) = "global";
local($f_print_proto) = 1;
%default_vars = read_vars(${default_inifile});
open(FH, "<$inifile") || die "cannot open [$inifile]: %!";
while(){
if(/^\[(.+)\]/){
$proto = $1;
$f_print_proto = 0;
}
if(/^([\w\d]+)=(.*)/){
local($key, $cont) = ($1, $2);
if($default_vars{"${proto}_${key}"} ne $cont
&& !(($proto eq "admin") && ($key eq "directoryindex")
&& ($cont eq "index.ja.html,index.html,index.htm"))
&& !(($proto eq "global") && ($key eq "admin_notification_id"))
){
if($f_print_proto == 0){
print "\n[$proto]\n";
$f_print_proto = 1;
}
print $_;
}
}
}
close(FH);
}
sub filter_config
{
local($curfile, $deffile, $newfile) = @_;
$default_action_str="default_action";
open(FH, "<${curfile}") || die "cannot open [$curfile]:$!";
while(){
if(/^([^\s=]+)=(.*)/){
$curvalue{$1} = $2;
}
}
close(FH);
open(FH_R, "<${deffile}") || die "cannot open [$deffile]:$!";
open(FH_W, ">${newfile}") || die "cannot open [$newfile]:$!";
while(){
if(/^([^\s=]+)=(.*)/){
if(defined $curvalue{$1}){
if(($1 eq "check_urldecode_action" || $1 eq "max_params_num_action") && $curvalue{$1} eq "default"){
if(defined $curvalue{$default_action_str}){
print FH_W "$1=$curvalue{$default_action_str}\n";
}else{
print FH_W "$1=block\n";
}
}else{
print FH_W "$1=$curvalue{$1}\n";
}
}else{
print FH_W;
}
}else{
print FH_W;
}
}
close(FH_R);
close(FH_W);
if(defined $curvalue{$default_action_str}){
$default_action = $curvalue{$default_action_str};
}
}
sub convert_default_action
{
local($srcfile, $dstfile, $new_action) = @_;
open(FH_R,"<${srcfile}") || die "cannot read open [${srcfile}]: $$!\n";
open(FH_W,">${dstfile}") || die "cannot write open [${dstfile}]: $$!\n";
while(){
if(/^(\S+)\t(\S+)\t(.+)$/){
$action = $2;
if($action eq "DEFAULT"){
print FH_W "$1\t$new_action\t$3\n";
}else{
print FH_W;
}
}
}
close(FH_R);
close(FH_W);
}
sub usage
{
print "usage:\n";
print " $0 [inifile] [default-inifile]\n";
print " : version up config file [inifile] using [default-inifile]\n";
print " $0 --diff [inifile] [default-inifile]\n";
print " : get difference from [default-inifile] to [inifile]\n";
print "options:\n";
print " --lang [ja|en] : set language\n";
}
##
## Main program
##
while($_=$ARGV[0]){
if(/^-v/ && @ARGV>=2){
shift;
$newversion = shift(@ARGV);
}elsif(/^--lang/ && @ARGV>=2){
shift;
$default_lang = shift(@ARGV);
}elsif(/^--middleindex/ && @ARGV>=2){
shift;
$middleindex = shift(@ARGV);
}elsif(/^--diff/){
shift;
$flag_diff_ini = 1;
}elsif(/^-/){
&usage;
exit 1;
}else{
last;
}
}
if(@ARGV != 2){
usage(); exit(1);
}
$inifile = shift @ARGV;
$default_inifile = shift @ARGV;
# print "default_lang=[$default_lang]\n";
if($default_lang eq ""){$default_lang="en";}
if($flag_diff_ini){
&diff_ini($inifile, $default_inifile);
exit 0;
}
&filecopy($inifile, "${inifile}.tmp");
&subst_keys("${inifile}.tmp","${inifile}");
%vars = read_vars("${inifile}");
$oldversion = $vars{"global_version"};
@newvars = read_vars($default_inifile);
# set random value for admin_notification_id
for($i=0;$i<@newvars/2;$i++){
local($key, $val) = ($newvars[$i*2], $newvars[$i*2+1]);
if(($key eq "global_admin_notification_id") && $val==0){
$newvars[$i*2+1] = int(rand(1<<30));
}elsif(($key eq "http_waf_cookie_key") && $val eq "0"){
$newvars[$i*2+1] = &gen_waf_cookie_key();
}
}
%newvars_hash = @newvars;
foreach $key(keys %systemvars){ $systemvars{$key}=$newvars_hash{$key};}
@newservices = &diff_services($default_inifile, $inifile);
$default_action = "UNKNOWN";
# print "### vars=[", %vars, "]\n";
# print "### newvars=[@newvars]\n";
# print "### newservices=[@newservices]\n";
# print "### systemvars=[", %systemvars, "]\n";
&filecopy($inifile, "${inifile}.tmp");
&filter_config("${inifile}.tmp","${default_inifile}","${inifile}");
&filecopy($inifile, "${inifile}.tmp");
&verup_config("${inifile}.tmp", ${inifile});
&filecopy($inifile, "${inifile}.tmp");
&adjust_config("${inifile}.tmp", ${inifile});
if($newversion >= 230 && $default_action ne "UNKNOWN"){
$default_action=~tr/a-z/A-Z/;
@sig_files = ("conf/waf/sig_official.txt", "conf/waf/sig_official_base.txt", "conf/waf/sig_custom.txt");
foreach $sig_file (@sig_files){
&filecopy($sig_file, "${sig_file}.tmp");
&convert_default_action("${sig_file}.tmp", $sig_file, $default_action);
}
}