Configd/Language.pm

filter: Statementstmt 13.1% 22 / 168 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 21.8% 7 / 32 Podpod 100% 25 / 25 totaltotal 14.9% 54 / 362 CC 55 SCAR 45.4
CC
55
Cov
8%
spew
306.0
items
110.0
merge
110.0
CRAP 93.9
↑ Summary Configd/Language/postfix.pm »
1 package Configd::Language;
2 8 $Configd::Language::VERSION = '0.005';
3 #ABSTRACT: Base class for the languages: how one config file format is read, merged and written back.
4  
5 8 ▶use 5.034;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use re '/aa';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use File::Path qw{make_path};
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 0 ▶sub slurp {
Subroutine slurp: not called CC=3 SCAR=24.8
Pod documented
18 0 my ($path) = @_;
19 0 ▶ open( my $fh, '<:encoding(UTF-8)', $path ) or die "Could not read $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $path or die "Could not read ${path}: $!\n" 0 0 0
20 0 local $/ = undef;
21 0 my $text = <$fh>;
22 0 close $fh;
23 0 ▶ return $text // q{};
Condition0%0/2
$text // ""
l!l
00
MC/DC0%0/1
$text // ""
$text
Truth table0%0/2
A
A= $text
Aresult
11
00
24 }
25  
26 0 ▶sub spew {
Subroutine spew: not called CC=17 SCAR=57.2
Pod documented
27 0 my ( $path, $text, $mode, $owner ) = @_;
28  
29 0 my @was = stat $path;
30  
31 # fileparse gives back (name, directory, suffix); it is the directory we
32 # want, so that the rename below is within one filesystem and therefore
33 # atomic.
34 0 my ( undef, $dir ) = File::Basename::fileparse($path);
35 0 my ( $fh, $temp ) = File::Temp::tempfile( '.configd-XXXXXX', DIR => $dir );
36  
37 0 binmode( $fh, ':encoding(UTF-8)' );
38 0 ▶ print {$fh} $text or die "Could not write $temp: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $text or die "Could not write ${temp}: $!\n" 0 0 0
39 0 ▶ close $fh or die "Could not write $temp: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Could not write ${temp}: $!\n" 0 0 0
40  
41 0 ▶ rename( $temp, $path ) or do {
Branch: 1 branch
BranchTrueFalseTotal
rename $temp, $path or do { my $error = $!; unlink $temp; die "Could not put $temp in place as ${path}: $error\n" } 0 0 0
42 0 my $error = $!;
43 0 unlink $temp;
44 0 die "Could not put $temp in place as $path: $error\n";
45 };
46  
47 0 ▶ chmod( ( @was ? $was[2] & 0o7777 : $mode // 0o644 ), $path );
Branch: 1 branch
BranchTrueFalseTotal
@was ? : 0 0 0
Condition0%0/2
$mode // 420
l!l
00
MC/DC0%0/1
$mode // 420
$mode
Truth table0%0/2
A
A= $mode
Aresult
11
00
48  
49 # Only root can give a file away, and only root should try. Under a unit
50 # with SystemCallFilter=~@privileged a chown that cannot succeed is not
51 # EPERM, it is SIGSYS: the process is killed and core-dumped part way
52 # through writing a config file. The drop-in asks for privilege with a `+`
53 # so that this is root in practice, and this is the belt for that braces --
54 # configd should not be a landmine under anybody else's ExecStartPre.
55 0 ▶ return $path if $> != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $> != 0 0 0 0
56  
57 0 ▶ if (@was) {
Branch: 2 branches
BranchTrueFalseTotal
if (@was) { } 0 0 0
elsif (defined $owner) { } 0 0 0
58 0 ▶ chown( $was[4], $was[5], $path ) if $was[4] != $> || $was[5] != $);
Branch: 1 branch
BranchTrueFalseTotal
if $was[4] != $> or $was[5] != $) 0 0 0
Condition0%0/3
$was[4] != $> or $was[5] != $)
l!l&&r!l&&!r
000
MC/DC0%0/2
$was[4] != $> or $was[5] != $)
$was[4] != $> $was[5] != $)
Truth table0%0/3
A or B
A= $was[4] != $>B= $was[5] != $)
ABresult
1X1
011
000
59 }
60 elsif ( defined $owner ) {
61 0 my ( $user, $group ) = split( q{:}, $owner );
62 0 ▶ my $uid = defined $user && length $user ? getpwnam($user) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $user && length $user ? : 0 0 0
Condition0%0/3
defined $user and length $user
!ll&&!rl&&r
000
MC/DC0%0/2
defined $user and length $user
defined $user length $user
Truth table0%0/3
A and B
A= defined $userB= length $user
ABresult
0X0
100
111
63 0 ▶ my $gid = defined $group && length $group ? getgrnam($group) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $group && length $group ? : 0 0 0
Condition0%0/3
defined $group and length $group
!ll&&!rl&&r
000
MC/DC0%0/2
defined $group and length $group
defined $group length $group
Truth table0%0/3
A and B
A= defined $groupB= length $group
ABresult
0X0
100
111
64  
65 # An account that is not there is not an error: the file is being built
66 # somewhere the service is not installed, which is what --root is for.
67 0 ▶ chown( $uid // -1, $gid // -1, $path );
Condition0%0/4
$uid // -1
l!l
00
$gid // -1
l!l
00
MC/DC0%0/1
$uid // -1
$uid
MC/DC0%0/1
$gid // -1
$gid
Truth table0%0/2
A
A= $uid
Aresult
11
00
Truth table0%0/2
A
A= $gid
Aresult
11
00
68 }
69  
70 0 return $path;
71 }
72  
73  
74 0 ▶sub files {
Subroutine files: not called CC=1 SCAR=6.9
Pod documented
75 0 return ();
76 }
77  
78  
79 0 ▶sub units {
Subroutine units: not called CC=1 SCAR=6.9
Pod documented
80 0 return ();
81 }
82  
83  
84 0 ▶sub reloads {
Subroutine reloads: not called CC=1 SCAR=6.9
Pod documented
85 0 return 1;
86 }
87  
88  
89 0 ▶sub services {
Subroutine services: not called CC=1 SCAR=6.9
Pod documented
90 0 my ($self) = @_;
91 0 return $self->units();
92 }
93  
94  
95 0 ▶sub parse {
Subroutine parse: not called CC=2 SCAR=17.9
Pod documented
96 0 my ($self) = @_;
97 0 ▶ die( ( ref $self || $self ) . " does not know how to parse anything\n" );
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
98 }
99  
100  
101 0 ▶sub emit {
Subroutine emit: not called CC=2 SCAR=17.9
Pod documented
102 0 my ($self) = @_;
103 0 ▶ die( ( ref $self || $self ) . " does not know how to write anything\n" );
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
104 }
105  
106  
107 0 ▶sub accumulates {
Subroutine accumulates: not called CC=1 SCAR=6.9
Pod documented
108 0 return 0;
109 }
110  
111  
112 0 ▶sub repeats {
Subroutine repeats: not called CC=1 SCAR=6.9
Pod documented
113 0 return 0;
114 }
115  
116  
117 0 ▶sub separator {
Subroutine separator: not called CC=1 SCAR=6.9
Pod documented
118 0 return ', ';
119 }
120  
121  
122 0 ▶sub items {
Subroutine items: not called CC=10 SCAR=47.0
Pod documented
123 0 my ( $self, $key, $value ) = @_;
124 0 ▶ return () unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
125  
126 0 my $punctuation = $self->separator($key) =~ s/\s+//gr;
127 0 my @items = (q{});
128 0 my $depth = 0;
129 0 foreach my $char ( split m//, $value ) {
130 0 ▶ if ( !$depth && ( $char =~ m/\s/ || index( $punctuation, $char ) >= 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $depth and $char =~ /\s/aa || index($punctuation, $char) != -1) 0 0 0
Condition0%0/6
not $depth and $char =~ /\s/aa || index($punctuation, $char) != -1
!ll&&!rl&&r
000
$char =~ /\s/aa or index($punctuation, $char) != -1
l!l&&r!l&&!r
000
MC/DC0%0/3
not $depth and $char =~ /\s/aa || index($punctuation, $char) != -1
not $depth $char =~ /\s/aa index($punctuation, $char) != -1
Truth table0%0/4
not(A) and B or C
A= not $depthB= $char =~ /\s/aaC= index($punctuation, $char) != -1
ABCresult
0XX0
1000
11X1
1011
131 0 ▶ push @items, q{} if length $items[-1];
Branch: 1 branch
BranchTrueFalseTotal
if length $items[-1] 0 0 0
132 0 next;
133 }
134 0 ▶ $depth++ if $char eq '{';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "{" 0 0 0
135 0 ▶ $depth-- if $char eq '}' && $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "}" and $depth 0 0 0
Condition0%0/3
$char eq "}" and $depth
!ll&&!rl&&r
000
MC/DC0%0/2
$char eq "}" and $depth
$char eq "}" $depth
Truth table0%0/3
A and B
A= $char eq "}"B= $depth
ABresult
0X0
100
111
136 0 $items[-1] .= $char;
137 }
138 0 return grep { length } @items;
139 }
140  
141  
142 0 ▶sub combine {
Subroutine combine: not called CC=1 SCAR=6.9
Pod documented
143 0 my ( $self, $key, @items ) = @_;
144 0 return @items;
145 }
146  
147  
148 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
149 0 my ( $class, %opts ) = @_;
150 0 ▶ $opts{root} //= q{};
Condition0%0/2
$opts{"root"} //= ""
l!l
00
MC/DC0%0/1
$opts{"root"} //= ""
$opts{"root"}
Truth table0%0/2
A
A= $opts{"root"}
Aresult
11
00
151 0 $opts{root} =~ s{/\z}{};
152 0 return bless {%opts}, $class;
153 }
154  
155  
156 0 ▶sub name {
Subroutine name: not called CC=2 SCAR=17.9
Pod documented
157 0 my ($self) = @_;
158 0 ▶ my $class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
159 0 return ( split( q{::}, $class ) )[-1];
160 }
161  
162  
163 0 ▶sub root {
Subroutine root: not called CC=1 SCAR=6.9
Pod documented
164 0 my ($self) = @_;
165 0 return $self->{root};
166 }
167  
168  
169 0 ▶sub path {
Subroutine path: not called CC=1 SCAR=6.9
Pod documented
170 0 my ( $self, $path ) = @_;
171 0 return $self->{root} . $path;
172 }
173  
174  
175 0 ▶sub fragment_dir {
Subroutine fragment_dir: not called CC=1 SCAR=6.9
Pod documented
176 0 my ( $self, $file ) = @_;
177 0 return $self->path( $file->{path} ) . '.d';
178 }
179  
180  
181 0 ▶sub fragments {
Subroutine fragments: not called CC=3 SCAR=24.8
Pod documented
182 0 my ( $self, $file ) = @_;
183  
184 0 my $dir = $self->fragment_dir($file);
185 0 ▶ opendir( my $dh, $dir ) or return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or return () 0 0 0
186 0 ▶ my @names = sort grep { !m/\A[.]/ && !m/(?:[.](?:disabled|bak|dpkg-old|dpkg-new|dpkg-dist|rpmsave|rpmnew)|~)\z/ } readdir($dh);
Condition0%0/3
/\A[.]/aa || /(?:[.](?:disabled|bak|dpkg-old|dpkg-new|dpkg-dist|rpmsave|rpmnew)|~)\z/aa
l!l&&r!l&&!r
000
MC/DC0%0/2
/\A[.]/aa || /(?:[.](?:disabled|bak|dpkg-old|dpkg-new|dpkg-dist|rpmsave|rpmnew)|~)\z/aa
/\A[.]/aa /(?:[.](?:disabled|bak|dpkg-old|dpkg-new|dpkg-dist|rpmsave|rpmnew)|~)\z/aa
Truth table0%0/3
A || B
A= /\A[.]/aaB= /(?:[.](?:disabled|bak|dpkg-old|dpkg-new|dpkg-dist|rpmsave|rpmnew)|~)\z/aa
ABresult
1X1
011
000
187 0 closedir $dh;
188  
189 0 return map { "$dir/$_" } grep { -f "$dir/$_" } @names; ## no critic (ValuesAndExpressions::ProhibitFiletest_f)
190 }
191  
192  
193 0 ▶sub merge {
Subroutine merge: not called CC=10 SCAR=47.0
Pod documented
194 0 my ( $self, @sets ) = @_;
195  
196 0 my ( @order, %by_key );
197 0 foreach my $set (@sets) {
198 0 foreach my $directive (@$set) {
199 0 my $key = $directive->{key};
200  
201 # Comments and blank lines. Deliberately dropped rather than
202 # collected: see the POD above. A comment is anchored to the
203 # setting under it, and once several fragments have had their say
204 # there is no longer a setting for it to be anchored to.
205 0 ▶ next if !defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
206  
207 # A repeatable directive is identified by everything it says, so
208 # two different `save` lines are two settings and two identical ones
209 # are one.
210 0 ▶ $key = "$key\0$directive->{value}"
Branch: 1 branch
BranchTrueFalseTotal
if not $self->accumulates($key) and $self->repeats($key) 0 0 0
Condition0%0/3
not $self->accumulates($key) and $self->repeats($key)
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->accumulates($key) and $self->repeats($key)
not $self->accumulates($key) $self->repeats($key)
Truth table0%0/3
not(A) and B
A= not $self->accumulates($key)B= $self->repeats($key)
ABresult
0X0
100
111
211 if !$self->accumulates($key) && $self->repeats($key);
212  
213 0 ▶ if ( !exists $by_key{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $by_key{$key}) 0 0 0
214 0 push @order, $key;
215 0 $by_key{$key} = {%$directive};
216 0 next;
217 }
218  
219 0 ▶ if ( $self->accumulates($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->accumulates($key)) 0 0 0
220  
221 # An empty value resets rather than adding nothing, which is the
222 # one thing accumulation otherwise cannot express: a fragment can
223 # add to what came before it and never take anything out. That
224 # matters where 00-original is wrong rather than merely
225 # incomplete -- a guest whose hostname is the domain it hosts has
226 # the package's own mydestination naming a domain that must be a
227 # virtual mailbox domain instead, and no amount of adding fixes
228 # it. systemd drop-ins spell the same thing the same way, and
229 # for these parameters an empty value is what postfix reads it
230 # as anyway.
231 0 ▶ if ( !length( $directive->{value} // q{} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length($directive->{"value"} // "")) 0 0 0
Condition0%0/2
$directive->{"value"} // ""
l!l
00
MC/DC0%0/1
$directive->{"value"} // ""
$directive->{"value"}
Truth table0%0/2
A
A= $directive->{"value"}
Aresult
11
00
232 0 $by_key{$key} = {%$directive};
233 0 next;
234 }
235  
236 0 my %seen;
237 0 my @items = grep { !$seen{$_}++ } map { $self->items( $key, $_ ) } $by_key{$key}{value}, $directive->{value};
238 0 $by_key{$key}{value} = join( $self->separator($key), $self->combine( $key, @items ) );
239 0 next;
240 }
241  
242 # The later fragment is the one that meant it.
243 0 $by_key{$key} = {%$directive};
244 }
245 }
246  
247 0 return [ map { $by_key{$_} } @order ];
248 }
249  
250  
251 0 ▶sub build {
Subroutine build: not called CC=2 SCAR=17.9
Pod documented
252 0 my ( $self, $file ) = @_;
253  
254 0 my @sets;
255 0 foreach my $fragment ( $self->fragments($file) ) {
256 0 push @sets, $self->parse( slurp($fragment) );
257 }
258  
259 0 return $self->emit( $self->merge(@sets) );
260 }
261  
262  
263 0 ▶sub header {
Subroutine header: not called CC=1 SCAR=6.9
Pod documented
264 0 my ( $self, $file ) = @_;
265  
266 0 my $dir = $file->{path} . '.d';
267 0 return <<"HEADER";
268 # Generated by configd. Do not edit: this file is rebuilt from
269 # $dir every time the service starts or reloads,
270 # and anything you change here will be gone the next time that happens.
271 #
272 # Add a file to that directory instead, in this file's own syntax. They are
273 # read in order by name, and later ones win. What was here when configd
274 # adopted this file is 00-original, comments and all -- those stay in the
275 # fragments rather than being merged into this file, where they could only
276 # describe settings something later has since changed.
277 HEADER
278 }
279  
280  
281 0 ▶sub write {
Subroutine write: not called CC=4 SCAR=30.0
Pod documented
282 0 my ( $self, $file ) = @_;
283  
284 0 my $target = $self->path( $file->{path} );
285 0 my $wanted = $self->header($file) . $self->build($file);
286  
287 0 ▶ my $current = -e $target ? slurp($target) : undef;
Branch: 1 branch
BranchTrueFalseTotal
-e $target ? : 0 0 0
288 0 ▶ return 0 if defined $current && $current eq $wanted;
Branch: 1 branch
BranchTrueFalseTotal
if defined $current and $current eq $wanted 0 0 0
Condition0%0/3
defined $current and $current eq $wanted
!ll&&!rl&&r
000
MC/DC0%0/2
defined $current and $current eq $wanted
defined $current $current eq $wanted
Truth table0%0/3
A and B
A= defined $currentB= $current eq $wanted
ABresult
0X0
100
111
289  
290 # spew keeps whatever the file already was: postfix's master.cf is 0600 on a
291 # mail server set up properly, and handing that back to 0644 while "just
292 # regenerating a file" is not something anybody would go looking for.
293 0 spew( $target, $wanted, $file->{mode}, $file->{owner} );
294  
295 0 return 1;
296 }
297  
298  
299 0 ▶sub adopt {
Subroutine adopt: not called CC=7 SCAR=40.3
Pod documented
300 0 my ($self) = @_;
301  
302 0 my @adopted;
303 0 foreach my $file ( $self->files() ) {
304 0 my $target = $self->path( $file->{path} );
305 0 my $dir = $self->fragment_dir($file);
306 0 my $original = "$dir/00-original";
307  
308 0 ▶ unless ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 0 0
309 0 make_path($dir);
310  
311 # No more permissive than the directory the config file lives in:
312 # the fragments are the configuration now, and a 0755 directory
313 # beside a 0700 one hands them to anybody.
314 0 my ( undef, $parent ) = File::Basename::fileparse($target);
315 0 my @stat = stat $parent;
316 0 ▶ chmod( $stat[2] & 0o7777, $dir ) if @stat;
Branch: 1 branch
BranchTrueFalseTotal
if @stat 0 0 0
317 }
318  
319 # Already ours. Re-adopting would take the file we generated last time
320 # and make it the first fragment, which duplicates every setting in it.
321 0 ▶ if ( -f $original ) { ## no critic (ValuesAndExpressions::ProhibitFiletest_f)
Branch: 1 branch
BranchTrueFalseTotal
if (-f $original) 0 0 0
322 0 $self->write($file);
323 0 next;
324 }
325  
326 # 00-original is a copy of the file, so it is as secret as the file.
327 # Letting spew fall through to 0644 published redis.conf's requirepass,
328 # and opendkim.conf's key locations, to every local user on any distro
329 # whose /etc/<package> can be traversed.
330 0 my @was = stat $target;
331 0 ▶ if (@was) {
Branch: 1 branch
BranchTrueFalseTotal
if (@was) { } 0 0 0
332 0 spew( $original, slurp($target), $was[2] & 0o7777 );
333 0 ▶ chown( $was[4], $was[5], $original ) if $> == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $> == 0 0 0 0
334 }
335 else {
336 0 spew( $original, q{}, $file->{mode} );
337 }
338  
339 0 $self->write($file);
340 0 push @adopted, $file->{path};
341 }
342  
343 0 return @adopted;
344 }
345  
346  
347 0 ▶sub release {
Subroutine release: not called CC=3 SCAR=24.8
Pod documented
348 0 my ($self) = @_;
349  
350 0 my @released;
351 0 foreach my $file ( $self->files() ) {
352 0 my $original = $self->fragment_dir($file) . '/00-original';
353 0 ▶ next unless -f $original; ## no critic (ValuesAndExpressions::ProhibitFiletest_f)
Branch: 1 branch
BranchTrueFalseTotal
unless -f $original 0 0 0
354  
355 0 spew( $self->path( $file->{path} ), slurp($original) );
356 0 push @released, $file->{path};
357 }
358  
359 0 return @released;
360 }
361  
362  
363 8 1;
364  
365 __END__
↑ Summary Configd/Language/postfix.pm »