summaryrefslogtreecommitdiff
blob: 9dba4d07159c3bc79887b8ebcbfb40991f9bab44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/perl
# $Header: /var/cvsroot/gentoo-x86/net-www/apache/files/apachedelmod,v 1.3 2004/07/18 04:20:47 dragonheart Exp $
#
# apachedelmod
# Script to modify the Apache configuration file to remove a module
# (C) 2001 Jean-Michel Dault <jmdault@mandrakesoft.com> 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 <conffile> <mod_filename> <mod_name> \n";
    print "<name_module> [OPTIONS]\n";
    print "Options:\n";
    print " define=WHATEVER will remove module between <IfDefine WHATEVER>\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="<IfDefine $1>\n";
	$closedef="</IfDefine>\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 (<CONFF>) {
    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 (<BAK>) {
    if ($delline{$.}!=1) {
    print CONFF $_;
    }
}

close(CONFF);
close(BAK);