#!/usr/bin/perl # $Header: /var/cvsroot/gentoo-x86/net-www/apache/files/apachedelmod,v 1.2 2002/05/04 23:23:01 woodchip Exp $ # # apachedelmod # Script to modify the Apache configuration file to remove a module # (C) 2001 Jean-Michel Dault and Mandrakesoft # You can use it under the Apache Licence # See InstallModule for more info ;-) $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 " define=WHATEVER will remove module between \n"; print " addconf=CONFFILE removes 'Include CONFFILE' in apache.conf\n"; die "\n"; } sub debug{ if ($DEBUG) { foreach (@_) { print $_; } } } $_=$ARGV[1]; s/^.*\///g; s/^mod_//g; s/^lib//g; s/\.so$//g; $modso=$_; $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 (/define=(\w+)/i) { $opendef="\n"; $closedef="\n"; debug "##define=$1##\n"; } if (/addconf=(\S+)/i) { $addconf=$1; } } $_=`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 (($opendef) && (/^$opendef/i)) { debug "$opendef"; $beginsection=$.; $mysection=1; } if (/^#*LoadModule\s+$name_mod\s+.*$modso/i) { $delline{$.}=1; debug "Deleting Loadmodule at line $.\n"; } if (/^#*AddModule\s+$modc/i) { $delline{$.}=1; debug "Deleting Addmodule at line $.\n"; } if (($addconf) &&(/^#*Include\s+$addconf/i)) { $delline{$.}=1; debug "Deleting Include at line $."; } if (($closedef) && (/^$closedef/) && ($mysection > 0)) { debug "$closedef"; if ((($.-$beginsection) == 2) && ($delline{$.-1}==1)) { debug "Removing entire section\n"; $delline{$beginsection}=$delline{$.}=1; } if (($.-$beginsection) ==1) { debug "Empty section! Removing.\n"; $delline{$beginsection}=$delline{$.}=1; } if (($.-$beginsection) >2) { debug "Section contain more than one directive...\n"; debug "Leaving it alone.\n"; } $mysection=0; } } close(CONFF); close(BAK); debug "-----\n"; foreach (sort keys %delline) { debug "Delete line $_="; debug $delline{$_}; debug "\n"; } debug "-----\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 () { if ($delline{$.}!=1) { print CONFF $_; } } close(CONFF); close(BAK);