Authentication/Config.pm

filter: Statementstmt 69.5% 103 / 148 Branchbran 41.0% 23 / 56 Conditioncond 45.4% 10 / 22 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 94.4% 17 / 18 Podpod 100% 7 / 7 totaltotal 61.8% 162 / 262 CC 41 SCAR 34.4
CC
41
Cov
60%
setup_config
83.6
get_config
18.0
default_config
12.0
CRAP 31.3
« Authentication/Handler/SPF.pm ↑ Summary Authentication/Net/Milter.pm »
1 package Mail::Milter::Authentication::Config;
2 # ABSTRACT: Load config files for Authentication Milter
3 146 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 146 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 146 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 ##use Mail::Milter::Authentication::Pragmas;
7 # ABSTRACT: Common configuration handling
8 146 our $VERSION = '4.20261116'; # VERSION
9 146 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 146 ▶use Fcntl qw{ S_ISDIR };
Subroutine BEGIN: called CC=1 SCAR=0.0
11 146 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 146 ▶use TOML;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 146 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 146 ▶use Module::Loaded;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 146 ▶use Exporter qw{ import };
Subroutine BEGIN: called CC=1 SCAR=0.0
17 146 our @EXPORT_OK = qw{
18 get_config
19 set_config
20 default_config
21 setup_config
22 secure_private_dir
23 };
24  
25  
26 146 our $PREFIX = '/etc';
27 146 our $IDENT = 'authentication_milter';
28 146 my $CONFIG;
29  
30  
31 0 ▶sub default_config {
Subroutine default_config: not called CC=3 SCAR=24.8
Pod documented
32 0 my $config = {
33 'debug' => 0,
34 'dryrun' => 0,
35 'logtoerr' => 0,
36 'error_log' => '/var/log/authentication_milter.err',
37 'extended_log' => 1,
38 'legacy_log' => 0,
39 'connection' => 'inet:12345@localhost',
40 'umask' => '0000',
41 'runas' => 'nobody',
42 'rungroup' => 'nogroup',
43 'listen_backlog' => 20,
44 'check_for_dequeue' => 60,
45 'min_children' => 20,
46 'max_children' => 200,
47 'min_spare_children' => 10,
48 'max_spare_children' => 20,
49 'max_requests_per_child' => 200,
50 'protocol' => 'milter',
51 'connect_timeout' => 30,
52 'command_timeout' => 30,
53 'content_timeout' => 300,
54 'dequeue_timeout' => 300,
55 'addheader_timeout' => 30,
56 'dns_timeout' => 10,
57 'dns_retry' => 2,
58 'cache_dns_timeouts' => 1,
59 'tempfail_on_error' => '1',
60 'tempfail_on_error_authenticated' => '0',
61 'tempfail_on_error_local' => '0',
62 'tempfail_on_error_trusted' => '0',
63 'milter_quarantine' => '0',
64 'ip_map' => {},
65 'authserv_id' => '',
66 'handlers' => {},
67 'cache_dir' => '/var/cache/authentication_milter',
68 'spool_dir' => '/var/spool/authentication_milter',
69 'lib_dir' => '/var/lib/authentication_milter',
70 'lock_file' => '',
71 };
72  
73 0 require Mail::Milter::Authentication;
74 0 my $installed_handlers = Mail::Milter::Authentication::get_installed_handlers();
75 0 foreach my $handler ( @$installed_handlers ) {
76 0 my $handler_module = 'Mail::Milter::Authentication::Handler::' . $handler;
77 0 load $handler_module;
78 0 ▶ if ( $handler_module->can( 'default_config' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler_module->can('default_config')) { } 0 0 0
79 0 $config->{'handlers'}->{ $handler } = $handler_module->default_config();
80 }
81 else {
82 0 $config->{'handlers'}->{ $handler } = {};
83 }
84 }
85  
86 0 return $config;
87  
88 }
89  
90  
91 143 ▶sub setup_config {
Subroutine setup_config: called CC=18 SCAR=44.3
Pod documented
92 143 my $config = get_config();
93  
94 143 my $safe_ident = $IDENT;
95 143 $safe_ident =~ s/[^a-z0-9]/_/g;
96  
97 # Setup some directories
98 143 foreach my $type ( qw{ cache lib spool } ) {
99 429 my $dir = $config->{$type.'_dir'};
100 429 ▶ if ( $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 0 429 429
101 # Value supplied, MUST already be setup
102 # Check that we can use the given directory
103 0 ▶ die $type.'_dir does not exist' if ! -e $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir 0 0 0
104 0 ▶ die $type.'_dir is not a directory' if ! -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
105 0 ▶ die $type.'_dir is not a writable' if ! -w $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -w $dir 0 0 0
106 }
107 else {
108 429 ▶ if ( $EUID == 0 && -d "/var/$type" && -w "/var/$type" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Milter::Authentication::Config::EUID == 0 and -d "/var/$type" and -w "/var/$type") { } 0 429 429
Condition33%2/6
$Mail::Milter::Authentication::Config::EUID == 0 and -d "/var/$type" and -w "/var/$type"
!ll&&!rl&&r
42900
$Mail::Milter::Authentication::Config::EUID == 0 and -d "/var/$type"
!ll&&!rl&&r
42900
MC/DC0%0/3
$Mail::Milter::Authentication::Config::EUID == 0 and -d "/var/$type" and -w "/var/$type"
$Mail::Milter::Authentication::Config::EUID == 0 -d "/var/$type" -w "/var/$type"
Truth table25%1/4
A and B and C
A= $Mail::Milter::Authentication::Config::EUID == 0B= -d "/var/$type"C= -w "/var/$type"
ABCresult
0XX0
10X0
1100
1111
109 # We are root, create in global space if it exists and is writable
110 0 $dir = '/var/'.$type.'/authentication_milter';
111 0 ▶ mkdir $dir if ! -e $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir 0 0 0
112 # Create the subdir for this IDENT
113 0 $dir .= '/'.$safe_ident;
114 0 ▶ mkdir $dir if ! -e $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir 0 0 0
115 # Chown if relevant
116 0 my $user = $config->{'runas'};
117 0 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) 0 0 0
118 0 my ($login,$pass,$uid,$gid) = getpwnam($user);
119 0 chown $uid, $gid, $dir;
120 }
121 }
122 else {
123 # We are a user, or have no writable global space, Create something in a temporary space
124 429 $dir = join( '_',
125 '/tmp/authentication_milter',
126 $type,
127 $EUID,
128 $safe_ident,
129 );
130 429 ▶ mkdir $dir, 0700 if ! -e $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir 15 414 429
131 # The name is predictable, so anyone could have created it first
132 429 my $tightened = eval { secure_private_dir( $dir ) };
133 429 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 429 429
134 0 die "Generated ${type}_dir ($dir) is not safe to use, set ${type}_dir in config: $error";
135 }
136 429 ▶ warn "Generated ${type}_dir ($dir) was accessible to other users, mode set to 0700\n" if $tightened;
Branch: 1 branch
BranchTrueFalseTotal
if $tightened 0 429 429
137 }
138 }
139 429 ▶ die "Generated ${type}_dir ($dir) could not be created, set or check ${type}_dir in config" unless -e $dir && -d $dir && -w $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir and -d $dir and -w $dir 0 429 429
Condition33%2/6
-e $dir and -d $dir and -w $dir
!ll&&!rl&&r
00429
-e $dir and -d $dir
!ll&&!rl&&r
00429
MC/DC0%0/3
-e $dir and -d $dir and -w $dir
-e $dir -d $dir -w $dir
Truth table25%1/4
A and B and C
A= -e $dirB= -d $dirC= -w $dir
ABCresult
0XX0
10X0
1100
1111
140 429 $config->{$type.'_dir'} = $dir;
141 }
142 }
143  
144  
145 589 ▶sub secure_private_dir {
Subroutine secure_private_dir: called CC=6 SCAR=18.0
Pod documented
146 589 my ( $dir ) = @_;
147 589 ▶ my @stat = lstat $dir or die "Could not stat $dir: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my(@stat) = lstat $dir or die "Could not stat ${dir}: $!\n" 588 1 589
148 588 ▶ die "$dir is not a directory\n" if ! S_ISDIR( $stat[2] );
Branch: 1 branch
BranchTrueFalseTotal
unless S_ISDIR($stat[2]) 3 585 588
149 585 ▶ die "$dir is owned by uid $stat[4], not $EUID\n" if $stat[4] != $EUID;
Branch: 1 branch
BranchTrueFalseTotal
if $stat[4] != $Mail::Milter::Authentication::Config::EUID 0 585 585
150 585 ▶ return 0 if ! ( $stat[2] & 077 ); ## no critic
Branch: 1 branch
BranchTrueFalseTotal
unless $stat[2] & 63 583 2 585
151 2 ▶ chmod 0700, $dir or die "Could not set mode 0700 on $dir: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chmod 0700, $dir or die "Could not set mode 0700 on ${dir}: $!\n" 2 0 2
152 2 return 1;
153 }
154  
155  
156 113 ▶sub set_config {
Subroutine set_config: called CC=2 SCAR=6.9
Pod documented
157 113 my ( $config ) = @_;
158  
159 113 my @load_handlers = keys %{ $config->{'handlers'} };
160 113 @load_handlers = grep { ! /^\!/ } @load_handlers;
161 113 $config->{'load_handlers'} = \@load_handlers;
162  
163 113 ▶ my $protocol = $config->{'protocol'} || 'milter';
Condition100%2/2
$config->{'protocol'} || 'milter'
l!l
1085
MC/DC100%1/1
$config->{'protocol'} || 'milter'
$config->{'protocol'}
Truth table100%2/2
A
A= $config->{'protocol'}
Aresult
11
00
164 113 $config->{'protocol'} = $protocol;
165 113 $CONFIG = $config;
166 }
167  
168  
169 70 ▶sub load_file {
Subroutine load_file: called CC=6 SCAR=19.5
Pod documented
170 70 my ( $file ) = @_;
171  
172 70 ▶ if ( !-e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 70 70
173 0 die "Could not find configuration file $file";
174 }
175  
176 70 my $text;
177 {
178 70 ▶ open my $cf, '<',
Condition50%1/2
$file || die("Could not open configuration file $file")
l!l
700
MC/DC0%0/1
$file || die("Could not open configuration file $file")
$file
Truth table50%1/2
A
A= $file
Aresult
11
00
179 $file || die "Could not open configuration file $file";
180 70 my @t = <$cf>;
181 70 close $cf;
182 70 $text = join( q{}, @t );
183 }
184  
185 70 my $data;
186  
187 70 ▶ if ( $file =~ /\.toml$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.toml$/u) { } 0 70 70
188 0 ▶ $data = TOML::from_toml($text)
Condition0%0/2
TOML::from_toml($text) || die("Error parsing config file $file")
l!l
00
MC/DC0%0/1
TOML::from_toml($text) || die("Error parsing config file $file")
TOML::from_toml($text)
Truth table0%0/2
A
A= TOML::from_toml($text)
Aresult
11
00
189 || die "Error parsing config file $file";
190 }
191 else {
192 70 my $json = JSON::XS->new();
193 70 $json->relaxed(1);
194 70 ▶ $data = $json->decode($text)
Condition50%1/2
$json->decode($text) || die("Error parsing config file $file")
l!l
700
MC/DC0%0/1
$json->decode($text) || die("Error parsing config file $file")
$json->decode($text)
Truth table50%1/2
A
A= $json->decode($text)
Aresult
11
00
195 || die "Error parsing config file $file";
196 }
197  
198 70 return $data;
199 }
200  
201  
202 3275 ▶sub process_config {
Subroutine process_config: called CC=3 SCAR=16.5
Pod documented
203  
204 3275 ▶ if ( exists( $CONFIG->{ '_external_callback_processor' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CONFIG->{"_external_callback_processor"}) 0 3275 3275
205 0 ▶ if ( $CONFIG->{ '_external_callback_processor' }->can( 'process_config' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CONFIG->{"_external_callback_processor"}->can("process_config")) 0 0 0
206 0 $CONFIG->{ '_external_callback_processor' }->process_config( $CONFIG );
207 }
208 }
209  
210 3275 return $CONFIG;
211 }
212  
213  
214 3275 ▶sub get_config {
Subroutine get_config: called CC=9 SCAR=28.9
Pod documented
215  
216 3275 ▶ if ( $CONFIG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CONFIG) 3205 70 3275
217 3205 return process_config();
218 }
219  
220 70 my $file = $PREFIX . '/authentication_milter';
221 70 my $config;
222 70 ▶ if ( -e $file . '.toml' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file . '.toml') { } 0 70 70
223 0 $config = load_file( $file . '.toml' );
224 }
225 else {
226 70 $config = load_file( $file . '.json' );
227 }
228  
229 70 my $folder = $PREFIX . '/authentication_milter.d';
230 70 ▶ if ( -d $folder ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $folder) 0 70 70
231 0 my $dh;
232 0 opendir $dh, $folder;
233 my @config_files =
234 sort
235 0 grep { $_ =~ /\.(json|toml)$/ }
236 0 grep { not $_ =~ /^\./ }
237 readdir($dh);
238 0 closedir $dh;
239 0 foreach my $file ( @config_files ) {
240 0 $file =~ /(^.*)\.(json|toml)$/;
241 0 my $handler = $1;
242 ## ToDo Consider what to do if config already exists in .json config
243 0 $config->{'handlers'}->{$handler} = load_file( join( '/', $folder, $file ) );
244 }
245 }
246  
247 70 my @load_handlers = keys %{ $config->{'handlers'} };
248 70 @load_handlers = grep { ! /^\!/ } @load_handlers;
249 70 $config->{'load_handlers'} = \@load_handlers;
250  
251 70 ▶ my $protocol = $config->{'protocol'} || 'milter';
Condition100%2/2
$config->{'protocol'} || 'milter'
l!l
1852
MC/DC100%1/1
$config->{'protocol'} || 'milter'
$config->{'protocol'}
Truth table100%2/2
A
A= $config->{'protocol'}
Aresult
11
00
252 70 $config->{'protocol'} = $protocol;
253  
254 # Have we specified an external callback processor?
255 70 ▶ if ( exists( $config->{ 'external_callback_processor' } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"external_callback_processor"}) 0 70 70
256 # Try and load the handler
257 0 my $handler = $config->{ 'external_callback_processor' };
258 0 ▶ if ( ! is_loaded ( $handler ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&is_loaded($handler)) 0 0 0
259 0 eval {
260 146 ▶ no strict 'refs'; ## no critic;
Subroutine BEGIN: called CC=1 SCAR=0.0
261 0 load $handler;
262 0 $config->{ '_external_callback_processor' } = $handler->new();
263 };
264 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
265 0 delete $config->{ 'external_callback_processor' };
266 0 warn "Error loading external callback processor module: $error";
267 }
268 }
269 }
270  
271 70 $CONFIG = $config;
272  
273 70 return process_config();
274  
275 }
276  
277 146 1;
278  
279 __END__
« Authentication/Handler/SPF.pm ↑ Summary Authentication/Net/Milter.pm »