#!/usr/bin/perl # apacheaddmod # Script to modify the Apache configuration file to include a module # (C) 2001 Jean-Michel Dault and Mandrakesoft # You can use it under the Apache Licence # This script will try to add the module at the correct place in the config # file. It will try to find the last LoadModule and AddModule directives # that come before a special section (, ...) and # try to place its directives there. If you specify a "before=mod_foo" # option, it will try to position itself before that module. If you specify # "define=WHATEVER", it will place the module between # directives. $bakpath="/etc/apache/conf/bak"; if ((!$ARGV[0]) || (!$ARGV[1]) || (!$ARGV[2]) || (!$ARGV[3])) { print "Usage: $0 \n"; print " [OPTIONS]\n"; print "Options:\n"; print " before=mod_filename will add before specified module\n"; print " define=WHATEVER will install module between \n"; print " addconf=CONFFILE adds 'Include CONFFILE' in apache.conf\n"; die "\n"; } sub debug{ if ($DEBUG) { foreach (@_) { print $_; } } } $modso=$ARGV[1]; $modc=$ARGV[2]; $name_mod=$ARGV[3]; @confparts=split(/\//,$ARGV[0]); if ($#confparts==0) { # debug "confparts=0\n"; $confpath="/etc/apache/conf"; $conf="$ARGV[0]"; } else { # debug "confparts=$#confparts\n"; $conf="$confparts[$#confparts]"; $confpath=join('/',@confparts[0 .. $#confparts-1]); } system("mkdir -p $bakpath") ==0 or die "Can't create backup directory $bakpath\n"; #debug "Confpath: $confpath\n"; #debug "Conffile: $conf\n"; #debug "Backups: $bakpath\n"; foreach (@ARGV) { if (/before=(\w+)/i) { $_=$1; s/^mod_//g; s/^lib//g; s/\.so$//g; $before1="mod_$_"; $before2="lib$_"; debug "##before1=$before1##before2=$before2\n"; } if (/define=(\w+)/i) { $opendef="\n"; $closedef="\n"; debug "##define=$1##\n"; } if (/addconf=(\S+)/i) { $addconf="Include $1\n"; } } $_=`date +%Y%m%d-%T`;chop;s/:/./g;$dat=$_; $bak="$conf-$dat"; debug "$bak\n"; open(BAK,">$bakpath/$bak") or die "Can't create $bakpath/$bak\n"; open(CONFF,"$confpath/$conf") or die "Can't open $confpath/$conf\n"; while () { print BAK $_; if ($insidesection==0) { $lastbeforesection=$.}; if (/^\<\w+/i) { $insidesection=1; if (/^\ 0) {$lastload=$lastbeforedefine} if (($before1 eq $1) || ($before2 eq $1)) { $beforeload=$lastload; debug "Found Loadmodule $1 at line $beforeload\n"; } # debug "Load module #$1# at line line $lastload\n"; } if (/^#*AddModule\s+(\w+)\./i) { if ($inmain==0) {$lastadd=$lastbeforesection} if ($lastbeforedefine > 0) {$lastadd=$lastbeforedefine} if (($before1 eq $1) || ($before2 eq $1)) { $beforeadd=$lastadd; debug "Found Addmodule $1 at line $beforeadd\n"; } # debug "Add module #$1# at line $lastadd\n"; } } $lastline=$.; if ($lastload > 1) { debug "Last valid LoadModule at line $lastload\n"; } else { debug "Did not find any valid LoadModule directive. Load at the end.\n"; $lastload=$lastline+1; } if ($lastadd > 1) { debug "Last valid AddModule at line $lastadd\n"; } else { debug "Did not find any valid AddModule directive. Add at the end.\n"; $lastadd=$lastline+1; } if ($beforeload > $lastload) { debug "$before1 or $before2 loaded too low in the config file!\n"; debug "Will load right before the last valid module\n"; } if ($beforeadd > $lastadd) { debug "$before1 or $before2 added too low in the config file!\n"; debug "Will add right before the last valid module\n"; } if (($beforeload > 0) && ($beforeload <= $lastload)) { $lastload=$beforeload; debug "Beforeload exists and is before (or is) the last valid loaded module\n"; debug "Loading before $before1 or $before2 directive\n"; } if (($beforeadd > 0) && ($beforeadd <= $lastadd)) { $lastadd=$beforeadd; debug "Beforeadd exists and is before (or is) the last valid loaded module\n"; debug "Adding before $before1 or $before2 directive\n"; } #remove 1 from the line count $lastload-=1;$lastadd-=1; debug "Beforeload $beforeload\n"; debug "Beforeadd $beforeadd\n"; debug "Lastload: $lastload\n"; debug "Lastadd: $lastadd\n"; close(CONFF); close(BAK); debug "\nFinal results:\n"; debug "Put Loadmodule after line: $lastload\n"; debug "Put Addmodule after line: $lastadd\n"; open(BAK,"$bakpath/$bak") or die "Can't open $bakpath/$bak\n"; open(CONFF,">$confpath/$conf") or die "Can't create $confpath/$conf\n"; while () { print CONFF $_; if ($.==$lastload) { print CONFF "$opendef"; print CONFF "LoadModule $name_mod $modso\n"; print CONFF "$closedef"; } if ($.==$lastadd) { print CONFF "$opendef"; print CONFF "AddModule $modc\n"; print CONFF "$closedef"; } } print CONFF $addconf; close(CONFF); close(BAK);