lib/Qmail/Deliverable.pm

filter: Statementstmt 35.6% 72 / 202 Branchbran 16.6% 20 / 120 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 52.6% 10 / 19 Podpod 71.4% 5 / 7 totaltotal 30.3% 107 / 353 CC 73 SCAR 53.7
CC
73
Cov
28%
deliverable
702.0
dot_qmail
72.0
_prepend
30.0
CRAP 215.9
↑ Summary blib/lib/Qmail/Deliverable.pm »
1 package Qmail::Deliverable;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '1.09';
9 2 our @EXPORT_OK = qw/reread_config qmail_local dot_qmail deliverable qmail_user/;
10 2 our %EXPORT_TAGS = (all => \@EXPORT_OK);
11 2 our $VPOPMAIL_EXT = 0;
12 2 our $qmail_dir = '/var/qmail';
13  
14 # rfc2822's "atext"
15 2 my $atext = "[A-Za-z0-9!#\$%&\'*+\/=?^_\`{|}~-]";
16 2 my $valid = qr/^(?!.*\@.*\@)($atext+(?:[\@.]$atext+)*)\.?\z/;
17  
18 # disallow control characters and non-ascii
19 2 my $ascii = qr/^([\x20-\x7e]*)\z/;
20  
21 # parse shell line
22 2 my $shell_sq = qr/'[^']+'/; # no escaping in single quotes!
23 2 my $shell_dq = qr/(?:(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\"))/; # from Regexp::Common
24 2 my $shell_bare = qr/[^"'\\\s]+/; # no sq, dq, backslash, or space
25 2 my $shell_token = qr/(\\.|$shell_sq|$shell_dq|$shell_bare)+/;
26  
27 sub _readpipe {
28 0 ▶ my ($command, @args) = @_;
Subroutine _readpipe: not called CC=3 SCAR=24.8
29 0 local %ENV = ();
30 0 ▶ open my $fh, '-|', $command, @args or die "open: @_: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", $command, @args or die "open: @{_}: $!" 0 0 0
31 0 my @data = readline $fh;
32 0 close $fh; # Without explicit close, $? is unreliable later
33 0 ▶ return wantarray ? @data : join("", @data);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
34 }
35  
36 sub _slurp {
37 9 ▶ my ($fn) = @_;
Subroutine _slurp: called CC=3 SCAR=11.1
38 9 ▶ open my $fh, '<', $fn or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $fn or return 6 3 9
39 3 ▶ return wantarray ? readline $fh : join("", readline $fh);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
40 }
41  
42 sub _first (&@) {
43 4 ▶ my $sub = shift;
Subroutine _first: called CC=3 SCAR=11.2
44 4 ▶ $sub->($_) and return $_ for @_;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($_) and return $_ 0 72 72
45 4 return;
46 }
47  
48 sub _findinpath {
49 4 ▶ my ($command) = @_;
Subroutine _findinpath: called CC=1 SCAR=0.0
50 # Attempt to retain some level of security against PATH attacks
51 my @path = grep defined, map /($ascii)/, # untaint
52 4 grep m{^/[^<>|]+$}, split /:/, $ENV{PATH};
53 4 ▶ return _first { -x $_ } map "$_/$command", @path;
Subroutine __ANON__: called CC=1 SCAR=0.0
54 }
55  
56 # Resolve only once to further reduce risk of PATH attacks
57 2 my $valias_exec = _findinpath 'valias';
58 2 my $vuser_exec = _findinpath 'vuserinfo';
59  
60 2 my %locals;
61 my %virtualdomains;
62 2 my %users_exact;
63 2 my %users_wild;
64  
65 sub reread_config {
66 3 ▶ %locals = ();
Subroutine reread_config: called CC=12 SCAR=27.4
Pod documented
67 3 %virtualdomains = ();
68 3 %users_exact = ();
69 3 %users_wild = ();
70 3 ▶ my $locals_fn = -e "$qmail_dir/control/locals"
Branch: 1 branch
BranchTrueFalseTotal
-e "$Qmail::Deliverable::qmail_dir/control/locals" ? : 1 2 3
71 ? "$qmail_dir/control/locals"
72 : "$qmail_dir/control/me";
73 3 for (_slurp $locals_fn) {
74 1 chomp;
75 1 ▶ ($_) = lc =~ /$ascii/ or do { warn "Invalid character"; next; };
Branch: 1 branch
BranchTrueFalseTotal
($_) = lc($_) =~ /$ascii/ or do { warn "Invalid character"; next } 0 1 1
76 1 $locals{$_} = 1;
77 }
78 3 for (_slurp "$qmail_dir/control/virtualdomains") {
79 1 chomp;
80 1 ▶ ($_) = lc =~ /$ascii/ or do { warn "Invalid character"; next; };
Branch: 1 branch
BranchTrueFalseTotal
($_) = lc($_) =~ /$ascii/ or do { warn "Invalid character"; next } 0 1 1
81 1 my ($domain, $prepend) = split /:/, $_, 2;
82 1 $virtualdomains{$domain} = $prepend;
83 }
84 3 for (_slurp "$qmail_dir/users/assign") {
85 4 chomp;
86 4 ▶ ($_) = /$ascii/ or do { warn "Invalid character"; next; };
Branch: 1 branch
BranchTrueFalseTotal
($_) = /$ascii/ or do { warn "Invalid character"; next } 0 4 4
87 4 ▶ if (/^#/) { # comment
Branch: 4 branches
BranchTrueFalseTotal
if (/^#/) { } 1 3 4
elsif (s/^=([^:]+)://) { } 0 3 3
elsif (s/^\+([^:]+)://) { } 2 1 3
elsif (/^\.$/) { } 1 0 1
88 1 next;
89 } elsif (s/^=([^:]+)://) {
90 0 $users_exact{lc $1} = $_;
91 } elsif (s/^\+([^:]+)://) {
92 2 $users_wild{lc $1} = $_;
93 } elsif (/^\.$/) {
94 1 last;
95 } else {
96 0 warn "Invalid line in users/assign: '$_'\n";
97 }
98 }
99 }
100  
101 sub _qmail_getpw {
102 0 ▶ my ($local) = @_;
Subroutine _qmail_getpw: not called CC=3 SCAR=24.8
103 0 local $/ = "\0";
104 0 my @a = _readpipe "/var/qmail/bin/qmail-getpw", $local;
105 0 chomp @a;
106 0 for (@a) {
107 0 ▶ ($_) = /$ascii/ or do { warn "Invalid character"; return ""; }
Branch: 1 branch
BranchTrueFalseTotal
($_) = /$ascii/ or do { warn "Invalid character"; return "" } 0 0 0
108 }
109 0 return @a;
110 }
111  
112 sub _prepend {
113 0 ▶ my ($domain) = @_;
Subroutine _prepend: not called CC=5 SCAR=34.0
114  
115 0 ▶ return $virtualdomains{$domain} if exists $virtualdomains{$domain};
Branch: 1 branch
BranchTrueFalseTotal
if exists $virtualdomains{$domain} 0 0 0
116  
117 0 my @parts = split /\./, $domain;
118 0 for (reverse 1 .. @parts) {
119 0 my $wildcard = join "", map ".$_", @parts[-$_ .. -1];
120 0 ▶ return $virtualdomains{$wildcard} if exists $virtualdomains{$wildcard};
Branch: 1 branch
BranchTrueFalseTotal
if exists $virtualdomains{$wildcard} 0 0 0
121 }
122  
123 0 ▶ return $virtualdomains{''} if exists $virtualdomains{''};
Branch: 1 branch
BranchTrueFalseTotal
if exists $virtualdomains{""} 0 0 0
124 0 return undef;
125 }
126  
127 sub _potential_exts {
128 0 ▶ my ($ext) = @_;
Subroutine _potential_exts: not called CC=3 SCAR=24.8
129 0 my @exts;
130  
131 # Exact match has highest precedence
132 0 push @exts, $ext; # user, or user-ext
133  
134 # Then user-default
135 0 my @parts = split /(-)/, $ext;
136 0 for (reverse 1 .. $#parts) {
137 0 ▶ next unless $parts[$_] eq '-';
Branch: 1 branch
BranchTrueFalseTotal
unless $parts[$_] eq "-" 0 0 0
138 0 push @exts, join("", @parts[0..$_]) . "default";
139 }
140  
141 0 return @exts;
142 }
143  
144  
145 sub qmail_user {
146 3 ▶ my ($in) = @_;
Subroutine qmail_user: called CC=6 SCAR=19.5
Pod documented
147 my ($local) = lc($in) =~ /$valid/
148 3 ▶ or do { carp "Invalid address: $in"; return; };
Branch: 1 branch
BranchTrueFalseTotal
my($local) = lc($in) =~ /$valid/ or do { carp("Invalid address: $in"); return } 0 3 3
149  
150 3 ▶ if (exists $users_exact{$local}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $users_exact{$local}) { } 0 3 3
151 0 return split /:/, $users_exact{$local}, 7; # colon terminated
152 } else {
153 3 for (reverse 1 .. length $local) {
154 52 my $try = substr $local, 0, $_;
155 52 ▶ if (exists $users_wild{$try}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $users_wild{$try}) 1 51 52
156 1 my @assign = split /:/, $users_wild{$try}, 7;
157 1 $assign[5] .= substr($local, $_);
158 1 return @assign;
159 }
160 }
161 }
162  
163 2 ▶ if ($qmail_dir eq "t/fixtures") {
Branch: 1 branch
BranchTrueFalseTotal
if ($Qmail::Deliverable::qmail_dir eq "t/fixtures") 2 0 2
164 2 return $local;
165 }
166 0 return _qmail_getpw $local;
167 }
168  
169 sub qmail_local {
170 0 ▶ my ($in) = @_;
Subroutine qmail_local: not called CC=5 SCAR=34.0
Pod documented
171 my ($address) = lc($in) =~ /$valid/ or
172 0 ▶ do { carp "Invalid address: $in"; return; };
Branch: 1 branch
BranchTrueFalseTotal
my($address) = lc($in) =~ /$valid/ or do { carp("Invalid address: $in"); return } 0 0 0
173  
174 0 ▶ return $address if $address !~ /\@/;
Branch: 1 branch
BranchTrueFalseTotal
unless $address =~ /\@/ 0 0 0
175 0 my ($local, $domain) = split /\@/, $address;
176  
177 0 ▶ return $local if exists $locals{$domain};
Branch: 1 branch
BranchTrueFalseTotal
if exists $locals{$domain} 0 0 0
178  
179 0 my $prepend = _prepend $domain;
180 0 ▶ return "$prepend-$local" if defined $prepend;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prepend 0 0 0
181  
182 0 return undef;
183 }
184  
185 sub dot_qmail {
186 0 ▶ my ($user, $uid, $gid, $homedir, $dash, $ext) = @_;
Subroutine dot_qmail: not called CC=8 SCAR=42.8
Pod documented
187 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 0 0
188 0 my ($in) = @_;
189 my ($address) = lc($in) =~ /$valid/
190 0 ▶ or do { carp "Invalid address: $in"; return; };
Branch: 1 branch
BranchTrueFalseTotal
my($address) = lc($in) =~ /$valid/ or do { carp("Invalid address: $in"); return } 0 0 0
191  
192 0 my $local = qmail_local $address;
193 0 ▶ return undef if not defined $local;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $local 0 0 0
194  
195 0 ($user, $uid, $gid, $homedir, $dash, $ext) = qmail_user $local;
196 }
197  
198 0 $ext =~ s/\./:/g;
199  
200 0 my $dashext = $dash . $ext;
201  
202 0 ▶ if (not length $dashext) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $dashext) 0 0 0
203 0 ▶ return "$homedir/.qmail" if -e "$homedir/.qmail";
Branch: 1 branch
BranchTrueFalseTotal
if -e "$homedir/.qmail" 0 0 0
204 0 return ""; # defaultdelivery
205 }
206  
207 0 for ( _potential_exts($ext), 'default' ) {
208 0 ▶ return "$homedir/.qmail-$_" if -e "$homedir/.qmail-$_";
Branch: 1 branch
BranchTrueFalseTotal
if -e "$homedir/.qmail-$_" 0 0 0
209 }
210  
211 0 return undef;
212 }
213  
214 sub valias {
215 0 ▶ my ($address) = @_;
Subroutine valias: not called CC=4 SCAR=30.0
Pod undocumented
216 0 ▶ if (not $valias_exec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valias_exec) 0 0 0
217 0 warn "Cannot check valias; valias executable not found";
218 0 return 0;
219 }
220 0 my ($local, $domain) = split /\@/, $address;
221 0 for ( _potential_exts($local) ) {
222 0 eval { _readpipe( $valias_exec, "$_\@$domain") };
223 0 ▶ return 1 if $? == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? == 0 0 0 0
224 };
225 0 return 0;
226 }
227  
228 sub vuser {
229 0 ▶ my ($address) = @_;
Subroutine vuser: not called CC=4 SCAR=30.0
Pod undocumented
230 0 ▶ if (not $vuser_exec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vuser_exec) 0 0 0
231 0 warn "Cannot check vuser; vuser executable not found";
232 0 return 0;
233 }
234 0 ▶ eval { _readpipe $vuser_exec, $address } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { _readpipe($vuser_exec, $address) } } or return 0 0 0 0
235 0 ▶ return 1 if $? == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? == 0 0 0 0
236 0 return 0;
237 }
238  
239 sub deliverable {
240 0 ▶ my ($in) = @_;
Subroutine deliverable: not called CC=26 SCAR=65.5
Pod documented
241 my ($address) = lc($in) =~ /$valid/
242 0 ▶ or do { carp "Invalid address: $in"; return; };
Branch: 1 branch
BranchTrueFalseTotal
my($address) = lc($in) =~ /$valid/ or do { carp("Invalid address: $in"); return } 0 0 0
243  
244 0 my $local = qmail_local $address;
245 0 ▶ return 0xff if not defined $local;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $local 0 0 0
246  
247 0 my ($user, $uid, $gid, $homedir, $dash, $ext) = qmail_user $local;
248  
249 0 ▶ return 0x11 if not -r $homedir or not -x _;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $homedir and -x _ 0 0 0
Condition0%0/3
-r $homedir and -x _
!ll&&!rl&&r
000
MC/DC0%0/2
-r $homedir and -x _
-r $homedir -x _
Truth table0%0/3
A and B
A= -r $homedirB= -x _
ABresult
0X0
100
111
250 0 ▶ return 0x21 if (stat _)[2] & 0020; # group writable
Branch: 1 branch
BranchTrueFalseTotal
if (stat _)[2] & 16 0 0 0
251 0 ▶ return 0x21 if (stat _)[2] & 0002; # world writable
Branch: 1 branch
BranchTrueFalseTotal
if (stat _)[2] & 2 0 0 0
252 0 ▶ return 0x22 if -T _;
Branch: 1 branch
BranchTrueFalseTotal
if -T _ 0 0 0
253  
254 0 my $dot_qmail = dot_qmail $user, $uid, $gid, $homedir, $dash, $ext;
255  
256 0 ▶ return 0x00 if not defined $dot_qmail;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dot_qmail 0 0 0
257 0 ▶ return 0xf1 if not length $dot_qmail; # no .qmail => defaultdelivery
Branch: 1 branch
BranchTrueFalseTotal
unless length $dot_qmail 0 0 0
258  
259 0 ▶ return 0x00 if not -e $dot_qmail;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dot_qmail 0 0 0
260 0 ▶ return 0x11 if not -r $dot_qmail;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $dot_qmail 0 0 0
261 0 ▶ return 0xf1 if not -s _; # empty => defaultdelivery
Branch: 1 branch
BranchTrueFalseTotal
unless -s _ 0 0 0
262  
263 0 my @dot_qmail = _slurp $dot_qmail;
264  
265 0 ▶ if ($dot_qmail[0] =~ /^\|\s*\S*vdelivermail/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dot_qmail[0] =~ /^\|\s*\S*vdelivermail/) 0 0 0
266 0 ▶ if ($address !~ /\@/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($address =~ /\@/) 0 0 0
267 0 carp "vpopmail support not available if no domain given";
268 0 return 0xfe;
269 }
270 0 my $origlocal = (split /\@/, $address)[0];
271  
272 # Update domain with user field in assign file, just like vpopmail
273 # does. This way we support alias domains. See vpopmail.c,
274 # vget_assign().
275 0 $address = $origlocal . '@' . $user;
276  
277 0 ▶ if ($dot_qmail[0] =~ /bounce-no-mailbox/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dot_qmail[0] =~ /bounce-no-mailbox/) 0 0 0
278 0 ▶ return 0xf2 if -d "$homedir/$origlocal";
Branch: 1 branch
BranchTrueFalseTotal
if -d "$homedir/$origlocal" 0 0 0
279 0 ▶ return 0xf3 if valias $address;
Branch: 1 branch
BranchTrueFalseTotal
if valias($address) 0 0 0
280 0 ▶ return 0xf5 if vuser $address;
Branch: 1 branch
BranchTrueFalseTotal
if vuser($address) 0 0 0
281 0 ▶ if ( $VPOPMAIL_EXT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Qmail::Deliverable::VPOPMAIL_EXT) 0 0 0
282 0 my ($local, $domain) = split /@/, $address;
283 0 my @chunks = split /\-/, $local; # vpopmails qmail-ext option
284 0 for ( 0 .. $#chunks ) {
285 0 ▶ return 0xf6 if vuser $chunks[$_] .'@'.$domain;
Branch: 1 branch
BranchTrueFalseTotal
if vuser($chunks[$_] . '@' . $domain) 0 0 0
286 };
287 };
288 0 return 0x00;
289 }
290 0 return 0xf4;
291 }
292 0 ▶ if ($dot_qmail[0] =~ /^\|bouncesaying\s+(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dot_qmail[0] =~ /^\|bouncesaying\s+(.*)/) 0 0 0
293 0 my @args = $1 =~ /$shell_token/g;
294 0 ▶ return 0x13 if @args > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @args > 1 0 0 0
295 0 return 0x00;
296 }
297  
298 0 ▶ return 0x14 if grep /ezmlm/, @dot_qmail;
Branch: 1 branch
BranchTrueFalseTotal
if grep /ezmlm/, @dot_qmail 0 0 0
299 0 ▶ return 0x12 if grep /^\|/, @dot_qmail;
Branch: 1 branch
BranchTrueFalseTotal
if grep /^\|/, @dot_qmail 0 0 0
300  
301 0 return 0xf1;
302 }
303  
304 2 reread_config;
305  
306 # use Memoize;
307 # use Memoize::Expire;
308 # tie my %deliverable_cache, 'Memoize::Expire', LIFETIME => 60;
309 # memoize 'deliverable';
310  
311 2 1;
312  
313 __END__
↑ Summary blib/lib/Qmail/Deliverable.pm »