Acmeman/Source/Pound.pm untested

filter: Statementstmt 0.0% 0 / 336 Branchbran 0.0% 0 / 176 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 40 Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 677 CC 89 SCAR 74.8
CC
89
Cov
0%
scan
3660.0
new
72.0
host
30.0
CRAP 1765.7
1 package App::Acmeman::Source::Pound;
2 use strict;
3 use warnings;
4 use parent 'App::Acmeman::Source';
5 use Getopt::Long qw(GetOptionsFromArray :config gnu_getopt no_ignore_case);
6 use App::Acmeman::Log qw(:all);
7 use Clone 'clone';
8 use Data::Dumper;
9  
10 use constant {
11 ALT_DEFAULT => 0,
12 ALT_LISTENER => 1,
13 ALT_FILE => 2
14 };
15  
16 sub new {
17 my $class = shift;
18 my $cfgname = '/etc/pound.cfg';
19 my $host;
20 my @listener;
21 my @types;
22 my $comment;
23 my $include_dir;
24 my $altcert = 'default';
25 GetOptionsFromArray(\@_,
26 'config|f=s' => \$cfgname,
27 'host|h=s' => \$host,
28 'listener=s@' => \@listener,
29 'type=s@' => \@types,
30 'comment=s' => \$comment,
31 'include-dir=s' => \$include_dir,
32 'alt=s' => \$altcert
33 );
34 if ($altcert eq 'default' || $altcert eq 'service') {
35 $altcert = ALT_DEFAULT;
36 } elsif ($altcert eq 'listener') {
37 $altcert = ALT_LISTENER;
38 } elsif ($altcert eq 'file') {
39 $altcert = ALT_FILE;
40 } else {
41 error("invalid value for --altcert: ignored");
42 $altcert = ALT_DEFAULT;
43 }
44 my $self = bless {
45 cfgname => $cfgname,
46 host => $host,
47 altcert => $altcert,
48 used_hostnames => {}
49 }, $class;
50 if ($comment) {
51 $self->{comment} = qr($comment)
52 }
53 if (@listener) {
54 $self->{listener} = { map { $_ => 1 } @listener };
55 }
56 if (!@types) {
57 @types = qw(http)
58 }
59 $self->{types} = { map { lc($_) => 1 } @types };
60  
61 if (!$include_dir) {
62 (undef,$include_dir) = File::Spec->splitpath($cfgname);
63 }
64 $self->{include_dir} = $include_dir;
65 return $self;
66 }
67  
68 sub host {
69 my ($self, $arg, $locus, $nosave) = @_;
70  
71 my $file;
72 while ($arg =~ s/\s*-(\w+)//) {
73 $file = 1 if ($1 eq 'file') || ($1 eq 'filewatch');
74 }
75 my @hosts;
76 if ($file) {
77 $file = $self->dequote($arg);
78 if (open(my $fh, '<', $file)) {
79 while (<$fh>) {
80 chomp;
81 s/^\s+//;
82 s/\s+$//;
83 next if (/^$/ || /^#/);
84 $self->register_name(\@hosts, $_, $locus, $nosave);
85 }
86 close($fh);
87 } else {
88 error("$locus: can't open $file: $!");
89 }
90 } else {
91 $self->register_name(\@hosts, $self->dequote($arg), $locus, $nosave);
92 }
93 return @hosts;
94 }
95  
96 sub lstn_ok {
97 my ($self, $s, $name) = @_;
98 if (defined($self->{listener})) {
99 return 0 unless defined($name);
100 return exists($self->{listener}{$name})
101 }
102 return !defined($s) || $self->{types}{'http' . lc($s)};
103 }
104  
105 sub isglob {
106 return shift =~ m/(?<!\\)[*?\[\]]/;
107 }
108  
109 sub register_name {
110 my ($self, $hostlist, $host, $locus, $nosave) = @_;
111  
112 if ($self->{host} && $self->{host} eq $host) {
113 return
114 } elsif (my $where = $self->{used_hostnames}{lc($host)}) {
115 debug(2, "$locus: $host ignored");
116 debug(2, "$where: already registered here");
117 } else {
118 $self->{used_hostnames}{lc($host)} = $locus unless $nosave;
119 push @{$hostlist}, $host;
120 }
121 }
122  
123 use constant {
124 COL_OFF => 0,
125 COL_ON => 1,
126 COL_DEFER => 2
127 };
128  
129 sub cert_collect { shift->{collect}[0]{state} }
130  
131 sub cert_collect_init {
132 my ($self, $on) = @_;
133 $self->{collect} = [ { state => $on//0 } ];
134 }
135  
136 sub cert_collect_change {
137 my ($self, $on, $cn) = @_;
138 $self->{collect}[0]{state} = $on;
139 if (defined($cn) && $cn ne '') {
140 $self->setcn($cn);
141 $self->{collect}[0]{protected} = 1;
142 } elsif ($self->{collect}[0]{protected}) {
143 delete $self->{collect}[0]{protected};
144 }
145 }
146  
147 sub cert_collect_push {
148 my ($self, $on, $cn) = @_;
149 $on //= COL_OFF;
150 if ($on == COL_ON) {
151 unshift @{$self->{collect}}, { state => $on, cn => $cn//$self->cn }
152 } elsif ($on == COL_DEFER) {
153 unshift @{$self->{collect}}, { state => $on, hosts => [] }
154 } else {
155 unshift @{$self->{collect}}, clone($self->{collect}[0]);
156 }
157 }
158  
159 sub cert_collect_pop {
160 my ($self) = @_;
161 shift @{$self->{collect}}
162 }
163  
164 sub cert_add_hosts {
165 my $self = shift;
166 my $col = $self->cert_collect;
167 if ($col == COL_ON) {
168 unless ($self->cn) {
169 $self->setcn(shift);
170 $self->define_domain($self->cn);
171 }
172 $self->define_alias($self->cn, @_);
173 } elsif ($col == COL_DEFER) {
174 push @{$self->{collect}[0]{hosts}}, @_;
175 }
176 }
177  
178 sub cert_register_hosts {
179 my ($self, $locus, @list) = @_;
180 my @hosts;
181 $self->register_name(\@hosts, $_, $locus) for @list;
182 $self->cert_add_hosts(@hosts) if @hosts;
183 }
184  
185 sub cn { shift->{collect}[0]{cn} }
186 sub setcn {
187 my ($self, $val) = @_;
188 $self->{collect}[0]{cn} = $val
189 }
190 sub cert_init {
191 my ($self) = @_;
192 delete $self->{collect}[0]{cn} unless $self->{collect}[0]{protected}
193 }
194  
195 sub scan {
196 my ($self) = @_;
197 debug(1, "initializing file list from $self->{cfgname}");
198 if ($self->{host}) {
199 $self->define_domain($self->{host});
200 }
201 open(my $fh, '<', $self->{cfgname})
202 or do {
203 error("can't open $self->{cfgname}: $!");
204 return 0;
205 };
206 use constant {
207 ST_INIT => 0,
208 ST_LISTENER => 1,
209 ST_SERVICE => 2,
210 ST_EXPEND => 3,
211 ST_MATCH => 4,
212 ST_REWRITE => 5,
213 ST_CONDITION => 6
214 };
215 my $state = App::Acmeman::Source::Pound::IntStack->new(ST_INIT);
216  
217 my @input;
218 my $filename = $self->{cfgname};
219 my %cond;
220 my $condname;
221  
222 $self->cert_collect_init;
223  
224 while (1) {
225 while (<$fh>) {
226 chomp;
227 debug(5, "$filename:$.: ($state) [".($self->cn//"")."] $_");
228 s/^\s+//;
229 if ($self->{comment}) {
230 if (m{\#\s*
231 $self->{comment}
232 (?<state>=(?:on|off))?
233 (?:\s+cn=(?<cn>\S+))?
234 }x) {
235 debug(3, "$filename:$.: state=".($+{state}//'(null)').
236 ", cn=" . ($+{cn}//'(null)'));
237 $self->cert_collect_change(($+{state}//'on') eq 'on',
238 $+{cn});
239 } elsif (m{#\s*no-$self->{comment}}) {
240 # For backward compatibility.
241 $self->cert_collect_change(0);
242 }
243 }
244  
245 s/#.*//;
246 next if (/^$/);
247  
248 if (/^Include\s+"(?<name>.+)"\s*$/i) {
249 push @input, [ $filename, $fh, $. ];
250 my $incname = $self->dequote($+{name});
251 if (!File::Spec->file_name_is_absolute($incname)) {
252 $incname = File::Spec->catfile($self->{include_dir},
253 $incname);
254 }
255 if (-f $incname) {
256 debug(3, "$filename:$.: including $incname");
257 open(my $fh1, '<', $incname)
258 or do {
259 error("$filename:$.: can't open $incname: $!");
260 return 0;
261 };
262 $filename = $incname;
263 $fh = $fh1;
264 $self->cert_init if $self->{altcert} == ALT_FILE;
265 } elsif (isglob($incname)) {
266 if (my @flist = glob $incname) {
267 $incname = shift @flist;
268 debug(3, "$filename:$.: including $incname");
269 open(my $fh1, '<', $incname)
270 or do {
271 error("$filename:$.: can't open $incname: $!");
272 return 0;
273 };
274 push @{$input[$#input]}, \@flist;
275 $filename = $incname;
276 $fh = $fh1;
277 $self->cert_init if $self->{altcert} == ALT_FILE;
278 } else {
279 pop @input
280 }
281 } else {
282 ($filename, undef, $.) = pop @input;
283 error("$filename:$.: can't open $incname: $!");
284 return 0;
285 }
286 }
287  
288 if ($state == ST_INIT) {
289 if (/^ListenHTTP(?:(?<s>S)?\s+"(?<name>.*)"\s*)?$/i) {
290 if ($self->lstn_ok($+{s}, $+{name})) {
291 debug(4, "$filename:$.: listener");
292 $state->push(ST_LISTENER);
293 $self->cert_collect_push(defined($+{s}) && uc($+{s}) eq 'S');
294 $self->cert_init if $self->{altcert} == ALT_LISTENER;
295 }
296 } elsif (/^Condition\s+"(.+?)"\s*$/i) {
297 $condname = $1;
298 $state->push(ST_CONDITION);
299 $self->cert_collect_push(COL_DEFER);
300 }
301 } elsif ($state == ST_LISTENER) {
302 if (/^Service(?:\s+".*"\s*)?$/i) {
303 debug(4, "$filename:$.: service");
304 $self->cert_collect_push;
305 $state->push(ST_SERVICE);
306 $self->cert_init
307 if ($self->{altcert} == ALT_DEFAULT ||
308 ($self->{altcert} == ALT_FILE && !@input));
309 } elsif (/^ACME\s/i) {
310 $self->cert_collect_change(1);
311 } elsif (/^Rewrite/i) {
312 $state->push(ST_REWRITE);
313 } elsif (/^End$/i) {
314 debug(4, "$filename:$.: listener ends");
315 $state->pop;
316 $self->cert_collect_pop;
317 }
318 } elsif ($state == ST_CONDITION) {
319 if (s/^Host\s+(?!-(?:beg|contain|end|pcre|perl|posix|re))//i) {
320 if (my @hosts = $self->host($_, "$filename:$.", 1)) {
321 debug(3, "$filename:$.: hosts ".join(',', @hosts));
322 $self->cert_add_hosts(@hosts);
323 }
324 } elsif (/^Eval\s+"(.+?)"$/i) {
325 if (my $hosts = $cond{$1}) {
326 $self->cert_add_hosts(@$hosts);
327 }
328 } elsif (/^(?:Not\s+)?Match/i) {
329 $state->push(ST_MATCH);
330 } elsif (/^End$/i) {
331 $state->pop();
332 my $col = $self->cert_collect_pop;
333 if (@{$col->{hosts}}) {
334 @{$cond{$condname}} = @{$col->{hosts}};
335 }
336 debug(4, "$filename:$.: condition ends");
337 }
338 } elsif ($state == ST_SERVICE) {
339 if (s/^Host\s+(?!-(?:beg|contain|end|pcre|perl|posix|re))//i) {
340 if (my @hosts = $self->host($_, "$filename:$.")) {
341 debug(3, "$filename:$.: hosts ".join(',', @hosts));
342 $self->cert_add_hosts(@hosts);
343 }
344 } elsif (/^Eval\s+"(.+?)"$/i) {
345 if (my $hosts = $cond{$1}) {
346 $self->cert_register_hosts("$filename:$.", @$hosts);
347 }
348 } elsif (/^Backend|Emergency|(?:(?:ACL|TrustedIP)(?!.*\"))/i) {
349 $state->push(ST_EXPEND);
350 } elsif (/^(?:Not\s+)?Match/i) {
351 $state->push(ST_MATCH);
352 } elsif (/^Rewrite/i) {
353 $state->push(ST_REWRITE);
354 } elsif (/^End$/i) {
355 $state->pop();
356 $self->cert_collect_pop;
357 debug(4, "$filename:$.: service ends");
358 }
359 } elsif ($state == ST_MATCH) {
360 if (s/^Host\s+(?!-(?:beg|contain|end|pcre|perl|posix|re))//i) {
361 if (my @hosts = $self->host($_, "$filename:$.",
362 $state->under(ST_CONDITION))) {
363 debug(3, "$filename:$.: hosts ".join(',', @hosts));
364 $self->cert_add_hosts(@hosts);
365 }
366 } elsif (/^Eval\s+"(.+?)"$/i) {
367 if (my $hosts = $cond{$1}) {
368 if ($state->under(ST_CONDITION)) {
369 $self->cert_add_hosts(@$hosts);
370 } else {
371 $self->cert_register_hosts("$filename:$.",
372 @$hosts);
373 }
374 }
375 } elsif (/^(?:(?:(?:(?:Not\s+)?ACL)|TrustedIP)(?!.*\"))/i) {
376 $state->push(ST_EXPEND);
377 } elsif (/^Rewrite/i) {
378 $state->push(ST_REWRITE);
379 } elsif (/^(?:Not\s+)?Match/i) {
380 $state->push(ST_MATCH);
381 } elsif (/^End$/i) {
382 $state->pop;
383 }
384 } elsif ($state == ST_REWRITE) {
385 if (/^Rewrite/i) {
386 $state->push(ST_REWRITE);
387 } elsif (/^(?:Not\s+)?Match/i) {
388 $state->push(ST_MATCH);
389 } elsif (/^End$/i) {
390 $state->pop;
391 }
392 } elsif ($state == ST_EXPEND) {
393 if (/^End$/i) {
394 $state->pop;
395 }
396 }
397 }
398 close $fh;
399  
400 my $prev = pop @input or last;
401 ($filename, $fh, $., my $flist) = @$prev;
402 if ($flist && @$flist) {
403 my $incname = shift @$flist;
404 debug(3, "$filename:$.: including $incname");
405 push @input, [ $filename, $fh, $., $flist];
406 open(my $fh1, '<', $incname)
407 or do {
408 error("$filename:$.: can't open $incname: $!");
409 return 0;
410 };
411 $filename = $incname;
412 $fh = $fh1;
413 $self->cert_init if $self->{altcert} == ALT_FILE;
414 }
415 debug(3, "resuming at $filename:$.")
416 }
417  
418 if ($state->len > 1) {
419 error("$filename: parsing failed, " . (0+$state->len) .
420 " states remained on stack");
421 return 0
422 }
423  
424 return 1;
425 }
426  
427 sub dequote {
428 my ($self, $arg) = @_;
429 if (defined($arg) && $arg =~ s{^\s*"(.*?)"\s*$}{$1}) {
430 $arg =~ s{\\([\\"])}{$1}g;
431 }
432 return $arg;
433 }
434  
435 package App::Acmeman::Source::Pound::IntStack;
436 use Carp;
437  
438 sub new {
439 my $class = shift;
440 my $self = bless {
441 _st => []
442 }, $class;
443 $self->push(@_);
444 $self
445 }
446  
447 sub push {
448 my $self = shift;
449 push @{$self->{_st}}, @_;
450 }
451  
452 sub set {
453 my ($self, $val) = @_;
454 ${$self->{_st}} = $val;
455 }
456  
457 sub dup {
458 my $self = shift;
459 $self->push($self->tos);
460 }
461  
462 sub pop {
463 my ($self) = @_;
464 croak "stack is empty" unless $self->len;
465 pop @{$self->{_st}};
466 }
467  
468 sub tos {
469 my ($self) = @_;
470 croak "stack is empty" unless $self->len;
471 $self->{_st}->[$#{$self->{_st}}];
472 }
473  
474 sub len {
475 @{shift->{_st}}
476 }
477  
478 sub under {
479 my ($self, $st) = @_;
480 for (my $i = $#{$self->{_st}}; $i >= 0; $i--) {
481 if ($self->{_st}[$i] == $st) {
482 return 1;
483 }
484 }
485 return 0;
486 }
487  
488 use overload
489 '==' => sub {
490 my ($self, $other, $swap) = @_;
491 return $self->tos == $other;
492 },
493 '!=' => sub {
494 my ($self, $other, $swap) = @_;
495 return $self->tos == $other;
496 },
497 '""' => sub {
498 my ($self) = @_;
499 return $self->tos . ""
500 },
501 'bool' => sub {
502 my ($self) = @_;
503 return $self->tos
504 },
505 '0+' => sub {
506 my ($self) = @_;
507 return $self->tos
508 };
509 1;