blob: 72b859fc9e4966d74d1cce1283087e883927592d (
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
#!/usr/bin/perl
# apacheaddmod
# Script to modify the Apache configuration file to include a module
# (C) 2001 Jean-Michel Dault <jmdault@mandrakesoft.com> 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 (<Directory>, <VirtualHost>...) 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 <IfDefine WHATEVER>
# directives.
$bakpath="/etc/apache/conf/bak";
if ((!$ARGV[0]) || (!$ARGV[1]) || (!$ARGV[2]) || (!$ARGV[3])) {
print "Usage: $0 <conffile> <path/mod_filename.so> <mod_name.c> \n";
print "<name_module> [OPTIONS]\n";
print "Options:\n";
print " before=mod_filename will add before specified module\n";
print " define=WHATEVER will install module between <IfDefine WHATEVER>\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="<IfDefine $1>\n";
$closedef="</IfDefine>\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 (<CONFF>) {
print BAK $_;
if ($insidesection==0) { $lastbeforesection=$.};
if (/^\<\w+/i) {
$insidesection=1;
if (/^\<IfDefine/i) { $lastbeforedefine=$.};
$lastbeforesection-=1;
# debug "Entering section $_";
}
if (/^\<\/\w+/i) {
$insidesection=0;
if (/^\<\/IfDefine/i) { $lastbeforedefine=0};
# debug "Leaving section $_";
}
if (/^\<(Directory)/i) {
if ($inmain==0) { debug "Met my first inmain directive\n"; }
$inmain=1;
}
if (/^\<(Location)/i) {
if ($inmain==0) { debug "Met my first inmain directive\n"; }
$inmain=1;
}
if (/^\<(Files)/i) {
if ($inmain==0) { debug "Met my first inmain directive\n"; }
$inmain=1;
}
if (/^\<(Limit)/i) {
if ($inmain==0) { debug "Met my first inmain directive\n"; }
$inmain=1;
}
if (/^\<(VirtualHost)/i) {
if ($inmain==0) { debug "Met my first inmain directive\n"; }
$inmain=1;
}
if (/^#*LoadModule\s+\w+_module\s+.*[\/]{1}(\w+)\./i) {
if ($inmain==0) {$lastload=$lastbeforesection}
if ($lastbeforedefine > 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 (<BAK>) {
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);
|