Wizard/Examples/Apache/Admin.pm untested

filter: Statementstmt 0.0% 0 / 145 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 280 CC 28 SCAR 46.9
CC
28
Cov
0%
MakeHttpdConf
240.0
new
72.0
IfUp
56.0
CRAP 109.3
1 # -*- perl -*-
2  
3 use strict;
4  
5 use Wizard::SaveAble ();
6 use Wizard::Examples::Apache::Config ();
7 use Symbol ();
8 use Socket ();
9  
10  
11 package Wizard::Examples::Apache::Admin;
12  
13  
14 sub Parse {
15 my($self, $template, $server, $host, $prefs) = @_;
16 $template =~ s/\~\~([\w\-]+)\~\~/
17 my $sf;
18 foreach my $s (@_) {
19 if (exists($s->{$1})) {
20 $sf = $s;
21 last;
22 }
23 }
24 die "Unknown pattern: $1" unless $sf;
25 $sf->{$1}/eg;
26 $template
27 }
28  
29  
30 sub new {
31 my($proto, $host, $server) = @_;
32  
33 my $cfg = $Wizard::Examples::Apache::Config::config;
34 my $file = $cfg->{'apache-prefs-file'};
35 my $prefs = Wizard::SaveAble->new($file);
36 my $basedir = $prefs->{'apache-prefs-basedir'} or die "Missing basedir";
37 my $h = Wizard::SaveAble->new(File::Spec->catfile($basedir, "$host.cfg"));
38 my $hostdir = File::Spec->catdir($basedir, $host);
39 my $s = Wizard::SaveAble->new(File::Spec->catfile($hostdir,
40 "$server.cfg"));
41 my $serverdir = File::Spec->catdir($basedir, $server);
42 bless({'prefs' => $prefs,
43 'basedir' => $basedir,
44 'host' => $h,
45 'hostdir' => $hostdir,
46 'server' => $s,
47 'serverdir' => $serverdir}, (ref($proto) || $proto));
48 }
49  
50  
51 sub MakeHttpdConf {
52 my $self = shift;
53  
54 # Start by loading the original httpd.conf and replace patterns
55 my $serverdir = ;
56 my $conffile = File::Spec->catfile($self->{'serverdir'}, 'httpd.conf');
57 unless (-f $conffile) {
58 $conffile = File::Spec->catfile($self->{'hostdir'}, 'httpd.conf');
59 }
60 my $fh = Symbol::gensym();
61 my $conf;
62 local $/ = undef;
63 (open($fh, "<$conffile") && defined($conf = <$fh>) && close($fh))
64 or die "Failed to load HTTPD config file $conffile: $!";
65 my $host = $self->{'host'};
66 my $server = $self->{'server'};
67 my %ports;
68 $ports{$server->{'apache-server-http-port'}} = 1
69 if $server->{'apache-server-http-port'};
70 $ports{$server->{'apache-server-https-port'}} = 1
71 if $server->{'apache-server-https-port'};
72  
73 # Next thing: Load the virtual servers
74 my $serverdir = $self->{'serverdir'};
75 return $conf unless -d $serverdir;
76 my $dh = Symbol::gensym();
77 opendir($dh, $serverdir) or die "Failed to open directory $serverdir: $!";
78 my %interfaces;
79 while (my $f = <$dh>) {
80 next unless $f =~ /(.*).cfg$/;
81 my $vserver_name;
82 my $file = File::Spec->catfile($serverdir, $f);
83 my $fh = Symbol::gensym();
84 my $vserver = Wizard::SaveAble->new($file);
85 my $name = $vserver->{'apache-virtualserver-name'};
86 my $ip = Socket::inet_aton($name);
87 unless (defined($ip)) {
88 $self->Warn("Failed to resolve host name $name; ignoring host $f");
89 next;
90 }
91 $ip = Socket::inet_ntoa($ip);
92  
93 $ports{$vserver->{'apache-virtualserver-http-port'}} = 1
94 if $vserver->{'apache-virtualserver-http-port'};
95 $ports{$vserver->{'apache-virtualserver-https-port'}} = 1
96 if $vserver->{'apache-virtualserver-https-port'};
97  
98 if (my $i = $vserver->{'interface'}) {
99 if ($interfaces{$i}) {
100 my $name = $vserver->{'apache-virtualserver-name'};
101 $self->Warn("Duplicate interface $i in $name; ignoring");
102 } else {
103 $interfaces{$i} = $ip;
104 }
105 }
106  
107 if ($vserver->{'configuration'}) {
108 $conf .= $vserver->{'configuration'};
109 } else {
110 my $mode = $vserver{'apache-virtualserver-http-mode'};
111 if ($mode eq 'http' || $mode eq 'both') {
112 $conf .= Wizard::Examples::Apache::Admin::Config::HTTP->Cnf
113 ($prefs, $host, $server, $vserver);
114 }
115 if ($mode eq 'https' || $mode eq 'both') {
116 $conf .= Wizard::Examples::Apache::Admin::Config::HTTPS->Cnf
117 ($prefs, $host, $server, $vserver);
118 }
119 if ($mode eq 'redirect') {
120 $conf .= Wizard::Examples::Apache::Admin::Config::Redirect->Cnf
121 ($prefs, $host, $server, $vserver);
122 }
123 }
124 }
125  
126 # Make sure, the defaults are valid
127 $conf .= <<"EOF";
128  
129 <VirtualHost _default_:*>
130 </VirtualHost>
131  
132 EOF
133  
134 # Finally parse the config file.
135 (Parse($conf, $server, $host, $prefs), \%interfaces);
136 }
137  
138  
139 sub IfUp {
140 my($self, $i, $ip) = @_;
141 my $arch = $self->{'apache-host-arch'};
142 my $interface = $self->{'apache-host-interface'};
143 if ($arch =~ /linux/) {
144 my $ifconfig = "$self->{'apache-host-ifconfig'} $interface";
145 return $self->Warn("Failed to parse netmask of interface $interface:\n"
146 "$ifconfig\nIgnoring interface $interface:$i.\n")
147 unless ($ifconfig =~ /mask:\s*(\d+\.\d+\.\d+\.\d+)/i);
148 my $netmask = $1;
149 return $self->Warn("Failed to parse interface $interface:\n$ifconfig"
150 . "\nIgnoring interface $interface:$i.\n")
151 unless ($ifconfig =~ /inet\s+addr:\s*(\d+\.\d+\.\d+\.\d+)/i);
152 my $ifip = $1;
153 my $net1 = Net::Netmask->new($ip, $netmask);
154 my $net2 = Net::Netmask->new($ip, $netmask);
155 return $self->Warn("The detected configuration of interface"
156 . " $interface doesn't match the IP address"
157 . " $ip.\nIgnoring interface $interface:$i.\n")
158 unless ($net1->base() == $net2->base());
159 my $command = "$self->{'apache-host-ifconfig'} $interface:$i"
160 . " $ip up\n";
161 print "Switching interface $i up: $command\n" if $::verbose;
162 system $command unless $::debug;
163 }
164 }
165  
166  
167 package Wizard::Examples::Apache::Admin::Config::HTTP;
168  
169 sub new {
170 my($prefs, $host, $server, $vserver) = @_;
171 my $self = { %$vserver };
172 $self->{'apache-virtualserver-pagedir'} =
173 File::Spec->catdir($server->{'apache-server-root'},
174 $vserver->{'apache-virtualserver-name'},
175 'pages');
176 $self->{'apache-virtualserver-cgidir'} =
177 File::Spec->catdir($server->{'apache-server-root'},
178 $vserver->{'apache-virtualserver-name'},
179 'cgi-bin');
180 $self->{'apache-virtualserver-logdir'} =
181 File::Spec->catdir($server->{'apache-server-root'},
182 $vserver->{'apache-virtualserver-name'},
183 'logs');
184 if (my $port = $self->{'apache-virtualserver-http-port'}) {
185 if ($port ne $self->{'apache-server-http-port'}) {
186 $self->{'apache-server-port'} = ":$port";
187 }
188 }
189 $self->{'apache-server-port'} ||= '';
190 my @index = $server->{'apache-server-index'} ?
191 split(/ /, $server->{'apache-server-index'}) :
192 ('welcome.html', 'index.html',
193 'welcome.htm', 'index.htm',
194 '/cgi-bin/noDirLists');
195 my %options;
196 if ($server->{'apache-server-options'}) {
197 foreach my $key (split(/ /, $server->{'apache-server-options'})) {
198 $options{'apache-server-options'} = 1;
199 }
200 } else {
201 $options{'Indexes'} = 1;
202 }
203 my @aliases = ' ScriptAlias ~~apache-virtualserver-cgidir~~';
204  
205 if ($vserver->{'apache-virtualserver-enable-ssi'}) {
206 unshift(@index, 'welcome.shtml', 'index.shtml');
207 unshift(@aliases,
208 ' ScriptAlias AddType text/html .shtml',
209 ' AddHandler server-parsed .shtml');
210 $options{'ExecCGI'} = 1;
211 }
212 if ($vserver->{'apache-virtualserver-enable-pcgi'}) {
213 unshift(@index, 'welcome.phtml', 'index.phtml');
214 unshift(@aliases,
215 ' AddHandler x-pcgi-script .phtml');
216 $options{'ExecCGI'} = 1;
217 }
218 if ($vserver->{'apache-virtualserver-enable-ep'}) {
219 unshift(@index, 'welcome.ep', 'index.ep');
220 unshift(@aliases,
221 ' AddType x-ep-script .ep',
222 ' ScriptAlias /cgi-bin/ep.cgi');
223 $options{'ExecCGI'} = 1;
224 }
225  
226 $conf->{'apache-virtualserver-options'} = "Options "
227 . $conf->{'apache-virtualserver-options'} || join(" ", keys %options);
228 $conf->{'apache-virtualserver-index'} = "DirectoryIndex "
229 . $conf->{'apache-virtualserver-index'} || join(" ", @index);
230 $conf->{'apache-virtualserver-aliases'} = join("\n", @aliases);
231 bless($conf, (ref($proto) || $proto));
232 }
233  
234 sub Template {
235 <<'EOF';
236 <VirtualHost ~~apache-virtualserver-name~~~~apache-server-port~~>
237 ServerName ~~apache-virtualserver-name~~
238 ServerAdmin ~~apache-virtualserver-admin~~
239 DocumentRoot ~~apache-virtualserver-pagedir~~
240 CustomLog ~~apache-virtualserver-logdir~~ combined
241 ~~apache-virtualserver-index~~
242 ~~apache-virtualserver-aliases~~
243 <Directory ~~apache-virtualserver-pagedir~~>
244 ~~apache-virtualserver-options~~
245 </Directory>
246 </VirtualHost>
247 EOF
248 }
249  
250 sub Cnf {
251 my($proto, $prefs, $host, $server, $vserver) = @_;
252 my $conf = $proto->new($prefs, $host, $server, $vserver);
253 my $template = $conf->Template();
254 Wizard::Examples::Apache::Admin::Parse($template, $conf, $template,
255 $server, $host, $prefs);
256 }
257  
258  
259 package Wizard::Examples::Apache::Admin::Config::HTTPS;
260  
261 @Wizard::Examples::Apache::Admin::Config::HTTPS::ISA =
262 qw(Wizard::Examples::Apache::Admin::Config::HTTP);
263  
264 sub Template {
265 my $self = shift;
266 my $template = $self->SUPER::Template();
267 my $ssl = <<'EOF';
268 SSLEngine on
269 SSLCertificateFile ~~apache-virtualserver-crtfile~~
270 SSLCertificeteKeyFile ~~apache-virtualserver-keyfile~~
271 EOF
272 }
273  
274  
275 sub new {
276 my $proto = shift;
277 my $conf = $proto->SUPER::new(@_);
278 my $port = $conf->{'apache-virtualserver-https-port'} ||
279 $server->{'apache-server-https-port'};
280 $conf->{'apache-server-port'} = ":$port";
281 }
282  
283  
284 package Wizard::Examples::Apache::Admin::Config::Redirect;
285  
286 @Wizard::Examples::Apache::Admin::Config::Redirect::ISA =
287 qw(Wizard::Examples::Apache::Admin::Config::HTTP);
288  
289 sub Template {
290 <<'EOF';
291 <VirtualHost ~~apache-virtualserver-name~~~~apache-server-port~~>
292 ServerName ~~apache-virtualserver-name~~
293 ServerAdmin ~~apache-virtualserver-admin~~
294 <Location />
295 Redirect 301 ~~apache-virtualserver-redirect~~
296 </Location>
297 </VirtualHost>
298 EOF
299 }
300  
301  
302 1;