| 1 | package App::Acmeman::Apache::Layout::slackware; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | use parent 'App::Acmeman::Apache::Layout'; | ||
| 6 | use Apache::Config::Preproc; | ||
| 7 | use App::Acmeman::Log qw(:all); | ||
| 8 | use File::BackupCopy; | ||
| 9 | |||
| 10 | our $PRIORITY = 10; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $class = shift; | ||
| 14 | my $ap = shift; | ||
| 15 | |||
| 16 | if ($ap->server_config eq '/etc/httpd/httpd.conf' | ||
| 17 | && -d '/etc/httpd/extra') { | ||
| 18 | return $class->SUPER::new($ap, | ||
| 19 | incdir => '/etc/httpd/extra', | ||
| 20 | restart_command => '/etc/rc.d/rc.httpd restart' | ||
| 21 | ); | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | sub post_setup { | ||
| 26 | my ($self,$filename) = @_; | ||
| 27 | |||
| 28 | my $master_config_file = $self->config_file; | ||
| 29 | my $app = new Apache::Config::Preproc( | ||
| 30 | $master_config_file, | ||
| 31 | '-no-comment-grouping', | ||
| 32 | -expand => [ 'locus', | ||
| 33 | { 'include' => [ server_root => $self->apache->server_root ] }, | ||
| 34 | { 'macro' => [ | ||
| 35 | 'keep' => [ qw(LetsEncryptChallenge | ||
| 36 | LetsEncryptReference | ||
| 37 | LetsEncryptSSL) | ||
| 38 | ] | ||
| 39 | ] | ||
| 40 | } | ||
| 41 | ]) | ||
| 42 | or do { | ||
| 43 | debug(1, "can't parse apache configuration: $Apache::Admin::Config::ERROR"); | ||
| 44 | return; | ||
| 45 | }; | ||
| 46 | |||
| 47 | my $need_macro_module = !$self->apache_modules('macro'); | ||
| 48 | my $letsencrypt_macro_loaded = grep { | ||
| 49 | $_->value =~ /^letsencrypt(?:challenge|reference|ssl)/i | ||
| 50 | } $app->select('section', 'Macro'); | ||
| 51 | |||
| 52 | debug(3, "Need macro module? ".($need_macro_module?'yes':'no')); | ||
| 53 | debug(3, "Need letsencrypt macro? ". | ||
| 54 | ($letsencrypt_macro_loaded==0 | ||
| 55 | ? 'yes' | ||
| 56 | : ($letsencrypt_macro_loaded==3 | ||
| 57 | ? 'no' : 'perhaps'))); | ||
| 58 | |||
| 59 | my $last_loadmodule; | ||
| 60 | my $stop_search; | ||
| 61 | my $last_master_locus; | ||
| 62 | my $include_text = "Include $filename"; | ||
| 63 | |||
| 64 | foreach my $node ($app->select) { | ||
| 65 | if (!$stop_search) { | ||
| 66 | if (($node->locus->filenames)[0] eq $master_config_file) { | ||
| 67 | $last_master_locus = $node->locus; | ||
| 68 | } | ||
| 69 | if ($node->type eq 'section') { | ||
| 70 | if ($node->name =~ /^virtualhost$/i) { | ||
| 71 | $stop_search = $last_master_locus; | ||
| 72 | last; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | if ($node->type eq 'comment') { | ||
| 78 | if ($node->value =~ m{^\s*loadmodule\s}) { | ||
| 79 | $last_loadmodule = $node->locus; | ||
| 80 | } | ||
| 81 | } elsif ($node->type eq 'directive') { | ||
| 82 | if ($node->name =~ /^loadmodule$/i) { | ||
| 83 | $last_loadmodule = $node->locus; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | if ($need_macro_module) { | ||
| 88 | if ($node->type eq 'comment') { | ||
| 89 | if ($node->value =~ m{^\s*(loadmodule\s+macro_module\s.*)}i) { | ||
| 90 | debug(3, "Will uncomment ".$node->locus. " ".$node->value); | ||
| 91 | $self->add_command($node->locus, \&_replace_directive_at, $1); | ||
| 92 | $last_loadmodule = $node->locus; | ||
| 93 | $need_macro_module = 0; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | if (!$letsencrypt_macro_loaded) { | ||
| 99 | if ($node->type eq 'directive' | ||
| 100 | && $node->name =~ /^include/i | ||
| 101 | && $node->value =~ m{^.+/httpd-vhosts.conf\s*$}) { | ||
| 102 | my $locus = $node->locus->has_file($master_config_file) | ||
| 103 | ? $node->locus : $last_master_locus; | ||
| 104 | debug(3, "Will insert \"$include_text\" before $locus"); | ||
| 105 | $self->add_command($locus, \&_insert_directive_before, | ||
| 106 | $include_text); | ||
| 107 | $letsencrypt_macro_loaded = 3; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | if ($need_macro_module && $last_loadmodule) { | ||
| 113 | debug(3, "Will insert a LoadModule directive after ".$last_loadmodule); | ||
| 114 | $self->add_command($last_loadmodule, | ||
| 115 | \&_insert_directive_after, | ||
| 116 | q{LoadModule macro_module lib64/httpd/modules/mod_macro.so}); | ||
| 117 | $need_macro_module = 0; | ||
| 118 | |||
| 119 | $last_loadmodule->fixup_lines($app->filename => 1); | ||
| 120 | } | ||
| 121 | |||
| 122 | if (!$letsencrypt_macro_loaded && !$need_macro_module) { | ||
| 123 | if ($stop_search) { | ||
| 124 | debug(3, "Will insert \"$include_text\" before $stop_search"); | ||
| 125 | $self->add_command($stop_search, \&_insert_directive_before, | ||
| 126 | $include_text); | ||
| 127 | } else { | ||
| 128 | debug(3, "Will insert \"$include_text\" after $last_loadmodule"); | ||
| 129 | $self->add_command($last_loadmodule, \&_insert_directive_after, | ||
| 130 | $include_text); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | $self->run_commands; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub add_command { | ||
| 138 | my ($self, $locus, $command, $text) = @_; | ||
| 139 | my $file = ($locus->filenames)[0]; | ||
| 140 | my $line = ($locus->filelines($file))[0]; | ||
| 141 | |||
| 142 | push @{$self->{_edits}{$file}}, { line => $line, command => $command, text => $text }; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub run_commands { | ||
| 146 | my ($self) = @_; | ||
| 147 | foreach my $file (keys %{$self->{_edits}}) { | ||
| 148 | $self->run_commands_for_file($file); | ||
| 149 | } | ||
| 150 | $self->apache_modules(undef); | ||
| 151 | } | ||
| 152 | |||
| 153 | sub run_commands_for_file { | ||
| 154 | my ($self, $file) = @_; | ||
| 155 | my @commands = sort { $a->{line} <=> $b->{line} } @{$self->{_edits}{$file}}; | ||
| 156 | |||
| 157 | my $app = new Apache::Config::Preproc( | ||
| 158 | $file, | ||
| 159 | '-no-comment-grouping', | ||
| 160 | -expand => [ 'locus' ]) | ||
| 161 | or do { | ||
| 162 | error("can't parse apache configuration file $file: $Apache::Admin::Config::ERROR"); | ||
| 163 | return; | ||
| 164 | }; | ||
| 165 | |||
| 166 | foreach my $node ($app->select) { | ||
| 167 | last if (!@commands); | ||
| 168 | my $line = ($node->locus->filelines(($node->locus->filenames)[0]))[0]; | ||
| 169 | if ($commands[0]->{line} == $line) { | ||
| 170 | my $cmd = shift @commands; | ||
| 171 | $self->${\ $cmd->{command} }($app, $node, $cmd->{text}); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | if (@commands) { | ||
| 176 | error((0+@commands) . " left unmatched in edit queue for file $file"); | ||
| 177 | error("$file left unchanged"); | ||
| 178 | } else { | ||
| 179 | my $backup_name = backup_copy($app->filename, error => \my $error); | ||
| 180 | if ($backup_name) { | ||
| 181 | debug(1, "modifying ".$app->filename."; prior version saved in $backup_name"); | ||
| 182 | } elsif ($error) { | ||
| 183 | error("can't backup ".$app->filename.": $error"); | ||
| 184 | error("file left unchanged"); | ||
| 185 | return; | ||
| 186 | } | ||
| 187 | $app->save; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | sub _replace_directive_at { | ||
| 192 | my ($self, $app, $node, $text) = @_; | ||
| 193 | $self->_insert_directive_after($app, $node, $text); | ||
| 194 | $node->delete; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub _insert_directive_after { | ||
| 198 | my ($self, $app, $node, $text) = @_; | ||
| 199 | my ($name, $value) = split /\s+/, $text, 2; | ||
| 200 | $app->add('directive', $name, $value, -after => $node); | ||
| 201 | } | ||
| 202 | |||
| 203 | sub _insert_directive_before { | ||
| 204 | my ($self, $app, $node, $text) = @_; | ||
| 205 | my ($name, $value) = split /\s+/, $text, 2; | ||
| 206 | $app->add('directive', $name, $value, -before => $node); | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | 1; | ||
| 211 | |||
| 212 |