blib/lib/Qmail/Control.pm

filter: Statementstmt 30.2% 13 / 43 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 44.4% 4 / 9 Podpod 100% 5 / 5 totaltotal 22.4% 22 / 98 CC 15 SCAR 33.9
CC
15
Cov
17%
lock
55.6
get_virtualdomains
30.0
get_rcpthosts
20.0
CRAP 29.7
↑ Summary blib/lib/Qmail/Control/Lock.pm »
1 ## This file is Copyright (C) 2002, Paul Prince <princep@charter.net>.
2 ## It is licensed and distributed under the terms of Perl itself.
3  
4 package Qmail::Control;
5  
6 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.01';
11  
12 =begin comment
13 =head1 README
14  
15 Qmail/Control version 0.01
16 Qmail/Control/Lock version 0.01
17 ===============================
18  
19 This module is not finished. Only the methods listed in
20 L<"METHODS"> have been implemented, but at least these have been
21 completed.
22  
23 This module's interface will change before the next minor release.
24 Specifically, the current nasty error return scheme will be replaced
25 with a better one, similar to DBI's.
26  
27 This version is being uploaded to get feedback. Version 0.02 will have
28 a stable interface, support for most of Qmail's control files,
29 [hopefully] a better and more portable locking system, and should be
30 ready for production use.
31  
32 =end comment
33  
34 =head1 INSTALLATION
35  
36 Use the following to install this module:
37  
38 shell$ perl Makefile.PL
39 shell$ make
40 shell$ make test
41 shell# make install
42
43 =head1 NAME
44  
45 Qmail::Control - Perl extension for interfacing with Qmail's control files.
46  
47 =head1 DESCRIPTION
48  
49 Qmail::Control provides an interface for reliably modifying Qmail's
50 control files from your Perl programs. It only provides an object
51 oriented interface.
52  
53 =head1 SYNOPSIS
54  
55 use Qmail::Control;
56  
57 my $control = Qmail::Control->new();
58  
59 # Get a shared lock on the control system. This is important, because
60 # when we're making decisions based on the return values of multiple
61 # methods, we don't want to be fooled by a race condition.
62 my $lock = $control->lock('shared');
63  
64 # Print all hosts which we accept mail for.
65 my @rcpthosts = $control->get_rcpthosts();
66 foreach (@rcpthosts) {
67 print "Found a rcpthost for $_->{'host'}.";
68 }
69  
70 # Print the list of virtual domains.
71 my @virtualdomains = $control->get_virtualdomains();
72 foreach (@virtualdomains) {
73 print "Found a vdom for $_->{'domain'}, user is $_->{'user'}.";
74 }
75  
76 # Get our default domain name.
77 my $defaultdomain = $control->defaultdomain();
78  
79 # Now, we're going to start changing things in the system. We must
80 # get an exclusive lock before we do.
81 $control->relock($lock, 'exclusive');
82  
83 # Add a rcpthost.
84 # Multiple hash references are legal.
85 $control->add_rcpthosts( {host => 'domain2.com'} );
86  
87 # Add a virtualdomain entry.
88 # Multiple hash references are legal.
89 $control->add_virtualdomain({
90 domain => 'domain2.com',
91 user => 'domain2systemaccount',
92 });
93  
94 # Set our default domain name.
95 # Also, this works with defaulthost, and me.
96 # Setting this to undef for any control file (except 'me', which is
97 # required) will delete that file. Use '' to clear out a file.
98 $control->defaultdomain({ set => 'domain.com' });
99
100 $control->unlock($lock);
101  
102 =cut
103  
104 ######################
105 # METHODS START HERE #
106 ######################
107  
108 =head1 METHODS
109  
110 =over 30
111  
112 =item Qmail::Control->new()
113  
114 Creates a new Qmail::Control object.
115  
116 Returns a reference to the newly created object.
117  
118 Takes no arguments, currently. This may change, but the argumentless
119 form will always exist.
120  
121 =cut
122  
123 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
124 # Do I need this stuff??
125 # my $invocant = shift;
126 # my $class = ref($invocant) || $invocant;
127 # bless($class);
128  
129 # Create a new Qmail::Control object.
130 0 my $self = { };
131 0 bless($self);
132  
133 # Return a reference to the newly created object.
134 0 return $self;
135 }
136  
137 =item $control->lock();
138  
139 Locks the Qmail control file subsystem. Note that currently, only
140 Qmail::Control itself uses this locking system, and that it is entirely
141 advisory. You should always lock the control file system before getting
142 information from more than one Qmail control file.
143  
144 Returns a Qmail::Control::Lock object. You must keep track of this.
145  
146 Takes a single argument, either 'shared' or 'exclusive'. The
147 Qmail::Control::Lock object that is returned will be locked in the way
148 specified. If no argument is given, the lock object returned will not
149 be locked at all!
150  
151 It's ok to use the relock() and unlock() methods provided by the lock
152 object.
153  
154 =cut
155  
156 0 ▶sub lock {
Subroutine lock: not called CC=8 SCAR=40.2
Pod documented
157 0 my $self = shift;
158  
159 # Make a Qmail::Control::Lock object.
160 1 ▶ use Qmail::Control::Lock;
Subroutine BEGIN: called CC=1 SCAR=0.0
161 0 my $lock = Qmail::Control::Lock->new();
162  
163 # If we have an argument,
164 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
165 # Lock the Qmail control file subsystem, in the way specified.
166 0 ▶ ($lock->lock_shared() or return undef) if ($_[0] eq 'shared' );
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "shared" 0 0 0
Condition0%0/5
$_[0] eq 'shared' && ($lock->lock_shared || (return undef))
!ll&&!rl&&r
000
$lock->lock_shared or return undef
l!l
00
MC/DC0%0/2
$_[0] eq 'shared' && ($lock->lock_shared || (return undef))
$_[0] eq 'shared' $lock->lock_shared
Truth table0%0/3
A && B
A= $_[0] eq 'shared'B= $lock->lock_shared
ABresult
0X0
100
111
167 0 ▶ ($lock->lock_exclusive() or return undef) if ($_[0] eq 'exclusive' );
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "exclusive" 0 0 0
Condition0%0/5
$_[0] eq 'exclusive' && ($lock->lock_exclusive || (return undef))
!ll&&!rl&&r
000
$lock->lock_exclusive or return undef
l!l
00
MC/DC0%0/2
$_[0] eq 'exclusive' && ($lock->lock_exclusive || (return undef))
$_[0] eq 'exclusive' $lock->lock_exclusive
Truth table0%0/3
A && B
A= $_[0] eq 'exclusive'B= $lock->lock_exclusive
ABresult
0X0
100
111
168 0 ▶ if ($_[0] ne 'shared' and $_[0] ne 'exclusive') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] ne "shared" and $_[0] ne "exclusive") 0 0 0
Condition0%0/3
$_[0] ne "shared" and $_[0] ne "exclusive"
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] ne "shared" and $_[0] ne "exclusive"
$_[0] ne "shared" $_[0] ne "exclusive"
Truth table0%0/3
A and B
A= $_[0] ne "shared"B= $_[0] ne "exclusive"
ABresult
0X0
100
111
169 0 return undef;
170 }
171 }
172 # Else, we don't want the control file subsystem locked.
173
174 # Return the Qmail::Control::Lock object.
175 0 return $lock;
176 }
177  
178 =item $control->get_rcpthosts();
179  
180 Gets the list of hosts the system is set to accept mail for.
181  
182 Returns a list of strings, each corresponding to a host that the system
183 accepts mail for. If an error occurs, a list will be returned in which
184 the first element is undef, the second element is a numeric error code,
185 and the third argument is a detailed text explaining the error.
186  
187 Takes no arguments at the current time.
188  
189 The numeric error code is 0 for a system error or 1 for a control file
190 syntax error.
191  
192 =cut
193  
194 0 ▶sub get_rcpthosts {
Subroutine get_rcpthosts: not called CC=4 SCAR=30.0
Pod documented
195 0 my $self = shift;
196  
197 # Open the rcpthosts file for reading.
198 0 ▶ open(RCPTHOSTS, '<', '/var/qmail/control/rcpthosts') or
Branch: 1 branch
BranchTrueFalseTotal
open RCPTHOSTS, "<", "/var/qmail/control/rcpthosts" or return undef, 0, "Unable to open rcpthosts file." 0 0 0
199 return(undef, 0, "Unable to open rcpthosts file.");
200
201 # Iterate over the file, processing it into the results variable.
202 0 my @results;
203 0 ▶ while (<RCPTHOSTS>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline RCPTHOSTS)) 0 0 0
204 0 chomp;
205  
206 # This assumes that a valid hostname is no more than 22
207 # characters in length, contains a-z, period (.), and dash (-).
208 # They cannot start with period or dash.
209 0 ▶ /^[a-z0-9][a-z0-9.-]{0,21}$/ or return(undef, 1,
Branch: 1 branch
BranchTrueFalseTotal
/^[a-z0-9][a-z0-9.-]{0,21}$/ or return undef, 1, "Invalid hostname at $. in rcpthosts file." 0 0 0
210 "Invalid hostname at $. in rcpthosts file.");
211
212 0 push(@results, $_);
213 }
214  
215 # If we haven't had any errors, return the results.
216 0 return @results;
217 }
218  
219 =item $control->get_virtualdomains();
220  
221 Gets the list of virtualdomains on this system, and the user associated
222 with each.
223  
224 Returns a list, each element representing one virtual domain. Each
225 element is also a hash reference, with the keys 'domain' and 'user'.
226 It can also return an error code, like the one for get_rcpthosts().
227  
228 Takes no arguments.
229  
230 =cut
231  
232 0 ▶sub get_virtualdomains {
Subroutine get_virtualdomains: not called CC=5 SCAR=34.0
Pod documented
233 0 my $self = shift;
234  
235 # Open the virtualdomains file for reading.
236 0 ▶ open(VDOMS, '<', '/var/qmail/control/virtualdomains') or
Branch: 1 branch
BranchTrueFalseTotal
open VDOMS, "<", "/var/qmail/control/virtualdomains" or return undef, 0, "Unable to open virtualdomains file." 0 0 0
237 return(undef, 0, "Unable to open virtualdomains file.");
238
239 # Iterate over the file, processing it into the results variable.
240 0 my @results;
241 0 ▶ while (<VDOMS>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline VDOMS)) 0 0 0
242 0 chomp;
243  
244 # Split out the domain and user parts.
245 0 my ($domain, $user) = split(':');
246
247 # This assumes that a valid domain name is no more than 22
248 # characters in length, contains a-z, 0-9, period (.), and dash (-).
249 # They cannot start with period or dash.
250 0 ▶ $domain =~ /^[a-z0-9][a-z0-9.-]{0,21}$/ or return(undef, 1,
Branch: 1 branch
BranchTrueFalseTotal
$domain =~ /^[a-z0-9][a-z0-9.-]{0,21}$/ or return undef, 1, "Invalid domain name at line $. in virtualdomains file." 0 0 0
251 "Invalid domain name at line $. in virtualdomains file.");
252  
253 # This assumes that a valid username is no more than 32
254 # characters in length, contains a-z, 0-9, period (.), and dash (-).
255 # They cannot start with period or dash.
256 # I need to check DJB's code to make this consistent.
257 0 ▶ $user =~ /^[a-z0-9][a-z0-9.-]{0,31}$/ or return(undef, 1,
Branch: 1 branch
BranchTrueFalseTotal
$user =~ /^[a-z0-9][a-z0-9.-]{0,31}$/ or return undef, 1, "Invalid username at line $. in virtualdomains file." 0 0 0
258 "Invalid username at line $. in virtualdomains file.");
259
260 0 push(@results, { domain => $domain, user => $user } );
261 }
262  
263 # If we haven't had any errors, return the results.
264 0 return @results;
265 }
266  
267 =begin _nextversion
268  
269 =item $control->defaultdomain();
270  
271 Wazzit do?
272  
273 Wazzit return?
274  
275 Wazzit take?
276  
277 What else do I need to know?
278  
279 =end _nextversion
280  
281 =cut
282  
283 0 ▶sub defaultdomain {
Subroutine defaultdomain: not called CC=1 SCAR=6.9
Pod documented
284 0 my $self = shift;
285  
286 }
287  
288 =pod
289  
290 =back
291  
292 =head1 AUTHOR
293  
294 Paul Prince, E<lt>princep@charter.netE<gt>
295  
296 =head1 SEE ALSO
297  
298 L<perl>.
299 L<Qmail::Control::Lock>.
300  
301 =cut
302  
303 # End the package.
304 1 1;
↑ Summary blib/lib/Qmail/Control/Lock.pm »