blib/lib/WebDyne/Install/Apache.pm

filter: Statementstmt 95.0% 38 / 40 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 96.3% 52 / 54 CC 1 SCAR 0.2
CC
1
Cov
95%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/WebDyne/Request/PSGI.pm ↑ Summary blib/lib/WebDyne/Install/Constant.pm »
1  
2 #
3 # This file is part of WebDyne.
4 #
5 # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10 # Full license text is available at:
11 #
12 # <http://dev.perl.org/licenses/>
13 #
14 package WebDyne::Install::Apache;
15  
16  
17 # Compiler Pragma
18 #
19 2 ▶sub BEGIN {$^W=0}
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use strict qw(vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25  
26 # Webmod Modules
27 #
28 2 ▶use WebDyne::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30  
31 # External Modules
32 #
33 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use Text::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38  
39 # Base installer
40 #
41 2 ▶use WebDyne::Install qw(message);
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43  
44 # Constants
45 #
46 2 ▶use WebDyne::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 ▶use WebDyne::Install::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use WebDyne::Install::Apache::Constant;
Subroutine BEGIN: called CC=1 SCAR=2.6
49  
50  
51 # Version information
52 #
53 $VERSION='3.027';
54  
55  
56 # Debug
57 #
58 0 && debug("%s loaded, version $VERSION", __PACKAGE__);
59  
60  
61 # Uninstaller global
62 #
63 my $Uninstall_fg;
64  
65  
66 # Init done.
67 #
68 1;
69  
70  
71 #------------------------------------------------------------------------------
72  
73  
74 # Uninstall
75 #
76 sub uninstall {
77  
78 # Set flag and call install routine
79 #
80 $Uninstall_fg++;
81 shift()->install(@_);
82  
83 }
84  
85  
86 # Install the Apache conf params needed to run the system
87 #
88 sub install {
89  
90  
91 # Get class, other paths
92 #
93 my ($class, $prefix, $installbin_dn, $opt_hr)=@_;
94 my $dry_run=$opt_hr->{'dry_run'} || $ENV{'DRY_RUN'};
95 local $ENV{'DRY_RUN'}=$ENV{'DRY_RUN'};
96 $ENV{'DRY_RUN'}=1 if $dry_run;
97  
98  
99 # Run the base install/uninstall routine to create the cache dir.
100 # In --text mode this module only renders generated config to STDOUT;
101 # cache path discovery still happens below, but no filesystem setup is
102 # performed.
103 #
104 unless ($Uninstall_fg || $opt_hr->{'text'}) {
105 WebDyne::Install->install($prefix, $installbin_dn) ||
106 return err();
107 }
108 elsif ($Uninstall_fg) {
109 WebDyne::Install->uninstall($prefix, $installbin_dn) ||
110 return err();
111 }
112  
113  
114 # Get package file name so we can look up inc for templates
115 #
116 (my $class_dn=$class . q(.pm))=~s/::/\//g;
117  
118  
119 # Load constants, get full path for class and constants from INC
120 #
121 $class_dn=$INC{$class_dn} ||
122 return err("unable to find location for $class in \%INC");
123  
124  
125 # Get file name component, then cut file out to get directory,
126 # split and reverse class for later use;
127 #
128 my $class_fn=(File::Spec->splitpath($class_dn))[2];
129 $class_dn=~s/\Q$class_fn\E$//;
130 my @class=reverse split(/::/, $class);
131  
132  
133 # Get webdyne cache dn;
134 #
135 my $cache_dn=&WebDyne::Install::cache_dn($prefix);
136  
137  
138 # Get config/constant hash ref
139 #
140 my %constant=(
141  
142 %WebDyne::Constant::Constant,
143 %WebDyne::Install::Constant::Constant,
144 %WebDyne::Install::Apache::Constant::Constant,
145 DIR_INSTALLBIN => $installbin_dn,
146 WEBDYNE_CACHE_DN => $cache_dn,
147  
148 );
149 my $config_hr=\%constant;
150 0 && debug('config_hr %s', Dumper($config_hr));
151  
152  
153 # Cannot do anything without Apache binary
154 #
155 return err("unable to find apache binary")
156 unless $config_hr->{'HTTPD_BIN'};
157  
158  
159 # Get template path name from module install dir.
160 #
161 my $template_dn=File::Spec->catdir($class_dn, $class[0]);
162 my $template_fn=File::Spec->catfile(
163 $template_dn, $config_hr->{'FILE_WEBDYNE_CONF_TEMPLATE'});
164  
165  
166 # Open it as a template
167 #
168 my $template_or=Text::Template->new(
169  
170 type => 'FILE',
171 source => $template_fn,
172  
173 ) || return err("unable to open template $template_fn, $!");
174  
175  
176 # Fill in with out self ref as a hash
177 #
178 my $webdyne_conf=$template_or->fill_in(
179  
180 HASH => $config_hr,
181 DELIMITERS => ['<!--', '-->'],
182  
183 ) || return err("unable to fill in template $template_fn, $Text::Template::ERROR");
184  
185  
186 # Get apache config dir
187 #
188 my $apache_conf_dn=$config_hr->{'DIR_APACHE_CONF'} ||
189 return err('unable to determine Apache config directory');
190  
191  
192 # Work out config file name
193 #
194 my $webdyne_conf_fn=File::Spec->catfile(
195 $apache_conf_dn, $config_hr->{'FILE_WEBDYNE_CONF'});
196  
197  
198  
199  
200 # Remove webdyne config file in uninstall mode. Install mode writes it
201 # after all generated fragments are available, so --text can print the
202 # complete Apache/WebDyne configuration.
203 #
204 if ($Uninstall_fg) {
205  
206 # In uninstall - get rid of conf file
207 #
208 if (-f $webdyne_conf_fn) {
209 if ($dry_run) {
210 message "would remove config file $webdyne_conf_fn";
211 }
212 else {
213 unlink($webdyne_conf_fn) ||
214 return err("unable to remove config file $webdyne_conf_fn, $!");
215 message "remove config file $webdyne_conf_fn";
216 }
217 }
218  
219 }
220
221  
222 # Work out constants file name
223 #
224 my $webdyne_conf_pl_fn=File::Spec->catfile(
225 $apache_conf_dn, $config_hr->{'FILE_WEBDYNE_CONF_PL'});
226
227  
228 # Get template path name from module install dir.
229 #
230 my $template_webdyne_conf_pl_fn=File::Spec->catfile(
231 $template_dn, $config_hr->{'FILE_WEBDYNE_CONF_PL_TEMPLATE'});
232  
233  
234 # Open it as a template
235 #
236 my $template_webdyne_conf_pl_or=Text::Template->new(
237  
238 type => 'FILE',
239 source => $template_webdyne_conf_pl_fn
240  
241 ) || return err("unable to open template $template_fn, $!");
242  
243  
244 # Fill in with out self ref as a hash
245 #
246 my $webdyne_conf_pl=$template_webdyne_conf_pl_or->fill_in(
247  
248 HASH => $config_hr,
249 DELIMITERS => ['<!--', '-->'],
250  
251 ) || return err("unable to fill in template $template_webdyne_conf_pl_fn, $Text::Template::ERROR");
252  
253  
254 # Print complete generated output if requested.
255 #
256 if ($opt_hr->{'text'} && !$Uninstall_fg) {
257 print "# $webdyne_conf_fn\n";
258 print $webdyne_conf;
259 print "\n# $webdyne_conf_pl_fn\n";
260 print $webdyne_conf_pl;
261 unless ($config_hr->{'HTTPD_SERVER_CONFIG_SKIP'}) {
262 my $apache_template_fn=File::Spec->catfile(
263 $template_dn, $config_hr->{'FILE_APACHE_CONF_TEMPLATE'});
264 my $apache_template_or=Text::Template->new(
265 type => 'FILE',
266 source => $apache_template_fn,
267 ) || return err("unable to open template $apache_template_fn, $!");
268 my $apache_conf=$apache_template_or->fill_in(
269 HASH => $config_hr,
270 DELIMITERS => ['<!--', '-->'],
271 ) || return err("unable to fill in template $apache_template_fn, $Text::Template::ERROR");
272 print "\n# $apache_template_fn\n";
273 print $apache_conf;
274 }
275 elsif (my $enabled_dn=$config_hr->{'DIR_APACHE_CONF_ENABLED'}) {
276 my $enabled_fn=File::Spec->catfile($enabled_dn, $config_hr->{'FILE_WEBDYNE_CONF'});
277 print "\n# Enable by linking $enabled_fn to $webdyne_conf_fn\n";
278 }
279 return \undef;
280 }
281  
282  
283 # Open, write webdyne config file unless in uninstall
284 #
285 unless ($Uninstall_fg) {
286 message "writing Apache config file '$webdyne_conf_fn'.";
287 if ($dry_run) {
288 message "would write Apache config file '$webdyne_conf_fn'.";
289 }
290 else {
291 my $webdyne_conf_fh=IO::File->new($webdyne_conf_fn, O_CREAT | O_WRONLY | O_TRUNC) ||
292 return err("unable to open file $webdyne_conf_fn, $!");
293 print $webdyne_conf_fh $webdyne_conf ||
294 return err("unable to write file $webdyne_conf_fn, $!");
295 $webdyne_conf_fh->close() ||
296 return err("unable to close file $webdyne_conf_fn, $!");
297 }
298 }
299  
300  
301 # Copy constants file into conf.d
302 #
303 unless ($Uninstall_fg) {
304  
305 message "writing Webdyne config file '$webdyne_conf_pl_fn'.";
306 if ($dry_run) {
307 message "would write Webdyne config file '$webdyne_conf_pl_fn'.";
308 }
309 else {
310 my $webdyne_conf_pl_fh=IO::File->new($webdyne_conf_pl_fn, O_CREAT | O_WRONLY | O_TRUNC) ||
311 return err("unable to open file $webdyne_conf_pl_fn, $!");
312 print $webdyne_conf_pl_fh $webdyne_conf_pl ||
313 return err("unable to write file $webdyne_conf_pl_fn, $!");
314 $webdyne_conf_pl_fh->close() ||
315 return err("unable to close file $webdyne_conf_pl_fn, $!");
316 }
317  
318 }
319 else {
320
321 # In uninstall - get rid of conf file
322 #
323 if (-f $webdyne_conf_pl_fn) {
324 if ($dry_run) {
325 message "would remove config file $webdyne_conf_pl_fn";
326 }
327 else {
328 unlink($webdyne_conf_pl_fn) ||
329 return err("unable to remove config file $webdyne_conf_pl_fn, $!");
330 message "remove config file $webdyne_conf_pl_fn";
331 }
332 }
333  
334 }
335  
336  
337 # Debian-style Apache installs use conf-available/conf-enabled.
338 #
339 if (my $enabled_dn=$config_hr->{'DIR_APACHE_CONF_ENABLED'}) {
340 my $enabled_fn=File::Spec->catfile($enabled_dn, $config_hr->{'FILE_WEBDYNE_CONF'});
341 if ($Uninstall_fg) {
342 if (-l $enabled_fn) {
343 my $target=readlink($enabled_fn);
344 if ($target eq $webdyne_conf_fn) {
345 if ($dry_run) {
346 message "would remove enabled Apache config link $enabled_fn";
347 }
348 else {
349 unlink($enabled_fn) ||
350 return err("unable to remove enabled Apache config link $enabled_fn, $!");
351 message "remove enabled Apache config link $enabled_fn";
352 }
353 }
354 else {
355 message "not removing enabled Apache config link $enabled_fn; target is $target";
356 }
357 }
358 elsif (-e $enabled_fn) {
359 message "not removing enabled Apache config path $enabled_fn; it is not a symlink";
360 }
361 }
362 else {
363 unless (-e $enabled_fn) {
364 if ($dry_run) {
365 message "would enable Apache config link '$enabled_fn'.";
366 }
367 else {
368 symlink($webdyne_conf_fn, $enabled_fn) ||
369 return err("unable to enable Apache config $enabled_fn -> $webdyne_conf_fn, $!");
370 message "enabled Apache config link '$enabled_fn'.";
371 }
372 }
373 elsif (-l $enabled_fn) {
374 my $target=readlink($enabled_fn);
375 return err("Apache config link $enabled_fn points to $target, expected $webdyne_conf_fn")
376 unless $target eq $webdyne_conf_fn;
377 }
378 else {
379 return err("Apache config enable path $enabled_fn already exists and is not a symlink");
380 }
381 }
382 }
383
384  
385  
386 # Only modify config file if no conf.d dir, denoted by var below
387 #
388 unless ($config_hr->{'HTTPD_SERVER_CONFIG_SKIP'}) {
389  
390  
391 # Get Apache config file, append root if not absolute
392 #
393 my $apache_conf_fn=$config_hr->{'HTTPD_SERVER_CONFIG_FILE'} ||
394 return err("unable to determine main server config file");
395 ($apache_conf_fn=~/^\//) || (
396 $apache_conf_fn=File::Spec->catfile(
397 $config_hr->{'HTTPD_ROOT'}, $apache_conf_fn
398 ));
399 my $apache_conf_fh=$opt_hr->{'text'} ? *STDOUT : IO::File->new($apache_conf_fn, O_RDONLY) ||
400 return err("unable to open file $apache_conf_fn, $!");
401 message "Apache config file '$apache_conf_fn'";
402  
403  
404 # Setup delims looking for
405 #
406 my ($delim, @delim)=$config_hr->{'FILE_APACHE_CONF_DELIM'};
407  
408  
409 # Turn into array, search for delims
410 #
411 my ($index, @apache_conf);
412 while (my $line=<$apache_conf_fh>) {
413 push @apache_conf, $line;
414 push(@delim, $index) if $line=~/\Q$delim\E/;
415 $index++;
416 }
417  
418  
419 # Check found right number of delims
420 #
421 if (@delim != 2 and @delim != 0) {
422  
423 return err(
424 "found %s '$delim' delimiter%s in $apache_conf_fn at line%s %s, expected exactly 2 delimiters",
425 scalar @delim,
426 ($#delim ? 's' : '') x 2,
427 join(',', @delim)
428 );
429  
430 }
431  
432  
433 # Check if delim found, if not, make last line
434 #
435 unless (@delim) {@delim=($index, $index-1)}
436 $delim[1]++;
437  
438  
439 # Close
440 #
441 $apache_conf_fh->close() unless ($apache_conf_fh eq '*main::STDOUT');
442 exit if $opt_hr->{'text'};
443  
444  
445 # Splice the lines between the delimiters out
446 #
447 splice(@apache_conf, $delim[0], $delim[1]-$delim[0]);
448  
449  
450 # Clean up end of conf file, remove excess CR's
451 #
452 my $lineno=$delim[0]-1;
453 for (undef; $lineno > 0; $lineno--) {
454  
455  
456 # We are going backwards through the file, as soon as we
457 # see something we quit
458 #
459 my $line=$apache_conf[$lineno];
460 chomp($line);
461  
462  
463 # Empty
464 #
465 if ($line=~/^\s*$/) {
466  
467  
468 # Yes, delete and continue
469 #
470 splice(@apache_conf, $lineno, 1);
471  
472 }
473 else {
474  
475  
476 # No, quit after rewinding lineno to last val
477 #
478 $lineno++;
479 last;
480  
481 }
482 }
483  
484  
485 # Only splice back in if not uninstalling
486 #
487 unless ($Uninstall_fg) {
488  
489  
490 # Get template we want to include in the config file
491 #
492 $template_fn=File::Spec->catfile(
493 $template_dn, $config_hr->{'FILE_APACHE_CONF_TEMPLATE'});
494  
495  
496 # Open it as a template
497 #
498 $template_or=Text::Template->new(
499  
500 type => 'FILE',
501 source => $template_fn,
502  
503 ) || return err("unable to open template $template_fn, $!");
504  
505  
506 # Fill in with out self ref as a hash
507 #
508 my $apache_conf=$template_or->fill_in(
509  
510 HASH => $config_hr,
511 DELIMITERS => ['<!--', '-->'],
512  
513 ) || return err("unable to fill in template $template_fn, $Text::Template::ERROR");
514  
515  
516 # Splice in now, but write out at end of block
517 #
518 splice(@apache_conf, $lineno, undef, $apache_conf);
519  
520  
521 }
522  
523  
524 # Re-open httpd.conf for write out, unless uninstall and delims not found, which
525 # means nothing was changed.
526 #
527 unless ($Uninstall_fg && ($delim[0] == $delim[1])) {
528 if ($dry_run) {
529 message "would update Apache config file '$apache_conf_fn'.";
530 }
531 else {
532 $apache_conf_fh=IO::File->new($apache_conf_fn, O_TRUNC | O_WRONLY) ||
533 return err("unable to open file $apache_conf_fn, $!");
534 print $apache_conf_fh join('', @apache_conf) ||
535 return err("unable to write file $apache_conf_fn, $!");
536 $apache_conf_fh->close() ||
537 return err("unable to close file $apache_conf_fn, $!");
538 message "Apache config file '$apache_conf_fn' updated.";
539 }
540 }
541  
542 }
543 else {
544  
545 # No need to update Apache config file - using conf.d dir
546 #
547 message 'Apache uses conf.d directory - not changing httpd.conf file';
548  
549 }
550  
551  
552 # Almost done ..
553 #
554 unless ($Uninstall_fg) {
555  
556 # Chown cache dir unless it is the system temp dir - then don't mess with it
557 #
558 if ($cache_dn) {
559 unless ($cache_dn eq File::Spec->tmpdir()) {
560 message
561 "Granting Apache ($APACHE_UNAME.$APACHE_GNAME) ownership of cache directory '$cache_dn'.";
562 if ($dry_run) {
563 message "would chown $cache_dn to $APACHE_UNAME.$APACHE_GNAME";
564 }
565 else {
566 chown($APACHE_UID, $APACHE_GID, $cache_dn) ||
567 return err("unable to chown $cache_dn to $APACHE_UNAME.$APACHE_GNAME");
568 }
569  
570  
571 # Selinx fixup
572 #
573 if ($SELINUX_ENABLED_BIN && !$dry_run) {
574  
575  
576 # Run to see if SELinux enabled
577 #
578 if ((system($SELINUX_ENABLED_BIN) >> 8) == 0) {
579  
580 # SELinux is enabled. chcon first
581 #
582 message("SELinux appears enabled - attempting to set cache directory file contexts appropriately.");
583 if (my $chcon_bin=$SELINUX_CHCON_BIN) {
584  
585 message("Adding SELinux context '$SELINUX_CONTEXT_HTTPD' to cache directory '$cache_dn' via chcon");
586 if (my $rc=system($chcon_bin, '-R', '-t', $SELINUX_CONTEXT_HTTPD, $cache_dn) >> 8) {
587 message("WARNING: SELinux chcon of $cache_dn to $SELINUX_CONTEXT_HTTPD failed with error code $rc\n")
588 }
589  
590  
591 # Dynaloader files
592 #
593 my @module_so_fn;
594 while (my ($module_so, $module_so_fn)=each %{$SELINUX_SO_CHECK}) {
595 if (eval("require $module_so")) {
596 foreach my $symbol (keys %::) {
597 if ($symbol=~/^_<(.*)\/\Q$module_so_fn\E$/) {
598 push @module_so_fn, File::Spec->catfile($1, $module_so_fn);
599 }
600 }
601 }
602 }
603  
604 foreach my $module_so_fn (@module_so_fn) {
605  
606 my $context_ls=qx/ls -lZ $module_so_fn/ ||
607 message("WARNING: unable to get context of file $module_so_fn");
608 my @context_ls=split(/\s+/, $context_ls);
609 my $context=$context_ls[3];
610 my ($user, $role, $type)=split(/\:/, $context);
611 if (($type ne $SELINUX_CONTEXT_LIB) && !$opt_hr->{'setcontext'}) {
612 message;
613 message("WARNING: SELinux context type of '$module_so_fn' is '$type'");
614 message("WARNING: file may not be loadable by Apache ! Use '$0 --setcontext' to change or fix manually");
615 message;
616 }
617 elsif (($type ne $SELINUX_CONTEXT_LIB) && $opt_hr->{'setcontext'}) {
618  
619 message("Adding SELinux context '$SELINUX_CONTEXT_LIB' to module library '$module_so_fn' via chcon");
620  
621 if (my $rc=system($chcon_bin, '-t', $SELINUX_CONTEXT_LIB, $module_so_fn) >> 8) {
622 message("WARNING: SELinux chcon of '$module_so_fn' to '$SELINUX_CONTEXT_LIB' failed with error code $rc\n")
623 }
624  
625 }
626  
627 }
628  
629 }
630 else {
631 message(
632 'WARNING: SELinux appears enabled, but the \'chcon\' command was not found - ' .
633 "your cache directory may not be writable\n"
634 );
635 }
636  
637 # Now semanage, if we can find it
638 #
639 if (my $semanage_bin=$SELINUX_SEMANAGE_BIN) {
640  
641 # List
642 #
643 my $selist=qx/$SELINUX_SEMANAGE_BIN fcontext -l -n/;
644 unless ($selist=~/^\Q$cache_dn\E/m) {
645  
646 # Context not added yet
647 #
648 message("Adding SELinux context '$SELINUX_CONTEXT_HTTPD' to cache directory '$cache_dn' via semanage");
649 if (my $rc=system($semanage_bin, 'fcontext', '-a', '-t', $SELINUX_CONTEXT_HTTPD, "${cache_dn}(/.*)?") >> 8) {
650 message("WARNING: SELinux semanage of $cache_dn to $SELINUX_CONTEXT_HTTPD failed with error code $rc\n")
651 }
652 }
653 }
654 else {
655 message('WARNING: SELinux semanage utility not found - chcon changes will be lost if SELinux relabel takes place.')
656 }
657 }
658 }
659  
660  
661 # Done
662 #
663 message 'install completed.'
664  
665 }
666 }
667 }
668 else {
669  
670 message 'uninstall completed'
671  
672 }
673  
674  
675 # Finished
676 message;
677 return \undef;
678  
679  
680 }
681 __END__
« lib/WebDyne/Request/PSGI.pm ↑ Summary blib/lib/WebDyne/Install/Constant.pm »