Bind8/Conf/Controls.pm

filter: Statementstmt 37.6% 38 / 101 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 63.1% 12 / 19 Podpod 100% 6 / 6 totaltotal 28.1% 56 / 199 CC 31 SCAR 41.2
CC
31
Cov
23%
inet
132.0
new
110.0
unix
56.0
CRAP 61.8
« Bind8/Conf/Key.pm ↑ Summary Bind8/Conf/Parser.pm »
1 # Bind8 package directive handling
2 #
3 # Copyright Karthik Krishnamurthy <karthik.k@extremix.net>
4  
5 =head1 NAME
6  
7 Unix::Conf::Bind8::Conf::Controls - Class for handling Bind8 configuration
8 directive `controls'
9  
10 =head1 SYNOPSIS
11  
12 use Unix::Conf::Bind8;
13  
14 my ($conf, $controls, $ret);
15 $conf = Unix::Conf::Bind8->new_conf (
16 FILE => '/etc/named.conf',
17 SECURE_OPEN => 1,
18 ) or $conf->die ("couldn't open `named.conf'");
19  
20 #
21 # Ways to get a Controls object
22 #
23  
24 $controls = Unix::Conf::Bind8::Conf->new_controls (
25 UNIX => [ '/var/run/ndc', 0600, 0, 0 ],
26 INET => [ '*', 52, [ qw (any) ], ],
27 ) or $controls->die ("couldn't create controls object");
28  
29 # or
30  
31 $controls = Unix::Conf::Bind8::Conf->get_controls ()
32 or $controls->die ("couldn't get controls object");
33  
34 #
35 # Operations that can be performed on an Controls object.
36 #
37  
38 # set the various attributes.
39  
40 $ret = $controls->inet ('192.168.1.1', '1000', [ qw (localhost) ])
41 or $ret->die ("couldn't set inet channel");
42  
43 $ret = $controls->unix ('/etc/namedb/control.pipe', 0600, 0, 0)
44 or $ret->die ("couldn't set unix channel");
45  
46 # get the attributes
47  
48 $ret = $controls->inet () or $ret->die ("couldn't get inet channel");
49 printf ("ADDRESS => %s, PORT => %s, ALLOW => %s\n", $ret->[0], $ret->[1],
50 "@{$ret->[2]->elements ()});
51  
52 $ret = $controls->unix () or $ret->die ("couldn't get unix channel");
53 printf ("PATH => %s, PERMS => %d, OWNER => %d, GROUP => %d",
54 $ret->[0], $ret->[1], $ret->[2], $ret->[3]);
55  
56 # delete
57 $ret = $controls->delete_inet () or $ret->die ("couldn't delete inet channel");
58 $ret = $controls->delete_unix () or $ret->die ("couldn't delete unix channel");
59  
60 =head1 METHODS
61  
62 =cut
63  
64 package Unix::Conf::Bind8::Conf::Controls;
65  
66 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 10 ▶use Unix::Conf;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 10 ▶use Unix::Conf::Bind8::Conf::Directive;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 10 our (@ISA) = qw (Unix::Conf::Bind8::Conf::Directive);
71  
72 10 ▶use Unix::Conf::Bind8::Conf::Lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74  
75 10 ▶use constant I_ADDR => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 10 ▶use constant I_PORT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 10 ▶use constant I_ALLOW => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 10 ▶use constant U_PATH => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 10 ▶use constant U_PERM => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 10 ▶use constant U_OWNER => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 10 ▶use constant U_GROUP => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 =item new ()
85  
86 Arguments
87 UNIX => [ PATH, PERM, OWNER, GROUP ],
88 INET => [ ADDR, PORT, ALLOW ]
89 WHERE => 'FIRST'|'LAST'|'BEFORE'|'AFTER'
90 WARG => Unix::Conf::Bind8::Conf::Directive subclass object
91 # WARG is to be provided only in case WHERE eq 'BEFORE
92 # or WHERE eq 'AFTER'
93 PARENT => reference,
94 # to the Conf object datastructure.
95  
96 Class constructor.
97 Creates a new Unix::Conf::Bind8::Conf::Controls object and returns it, on
98 success, an Err object otherwise. Do not use this constructor directly. Use
99 the Unix::Conf::Bind8::Conf::new_controls () method instead.
100  
101 =cut
102  
103 sub new
104 0 ▶{
Subroutine new: not called CC=10 SCAR=47.0
Pod documented
105 0 shift ();
106 0 my %args = @_;
107 0 my $new = bless ({});
108 0 my $ret;
109  
110 0 ▶ $args{PARENT} || return (Unix::Conf->_err ('new', "PARENT not defined"));
Branch: 1 branch
BranchTrueFalseTotal
$args{"PARENT"} or return "Unix::Conf"->_err("new", "PARENT not defined") 0 0 0
111 0 ▶ $ret = $new->_parent ($args{PARENT}) or return ($ret);
Branch: 1 branch
BranchTrueFalseTotal
$ret = $new->_parent($args{"PARENT"}) or return $ret 0 0 0
112 0 $ret = $new->inet (@{$args{INET}}) or return ($ret)
113 0 ▶ if ($args{INET});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"INET"} 0 0 0
Condition0%0/5
$args{'INET'} && (($ret = $new->inet(@{$args{'INET'};})) || (return $ret))
!ll&&!rl&&r
000
$ret = $new->inet(@{$args{"INET"};}) or return $ret
l!l
00
MC/DC0%0/2
$args{'INET'} && (($ret = $new->inet(@{$args{'INET'};})) || (return $ret))
$args{'INET'} $ret = $new->inet(@{$args{"INET"};})
Truth table0%0/3
A && B
A= $args{'INET'}B= $ret = $new->inet(@{$args{"INET"};})
ABresult
0X0
100
111
114 0 $ret = $new->unix (@{$args{UNIX}}) or return ($ret)
115 0 ▶ if ($args{UNIX});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"UNIX"} 0 0 0
Condition0%0/5
$args{'UNIX'} && (($ret = $new->unix(@{$args{'UNIX'};})) || (return $ret))
!ll&&!rl&&r
000
$ret = $new->unix(@{$args{"UNIX"};}) or return $ret
l!l
00
MC/DC0%0/2
$args{'UNIX'} && (($ret = $new->unix(@{$args{'UNIX'};})) || (return $ret))
$args{'UNIX'} $ret = $new->unix(@{$args{"UNIX"};})
Truth table0%0/3
A && B
A= $args{'UNIX'}B= $ret = $new->unix(@{$args{"UNIX"};})
ABresult
0X0
100
111
116 0 ▶ $ret = Unix::Conf::Bind8::Conf::_add_controls ($new)
Branch: 1 branch
BranchTrueFalseTotal
$ret = Unix::Conf::Bind8::Conf::_add_controls($new) or return $ret 0 0 0
117 or return ($ret);
118 0 ▶ $args{WHERE} = 'LAST' unless ($args{WHERE});
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"WHERE"} 0 0 0
119 $ret = Unix::Conf::Bind8::Conf::_insert_in_list ($new, $args{WHERE}, $args{WARG})
120 0 ▶ or return ($ret);
Branch: 1 branch
BranchTrueFalseTotal
$ret = Unix::Conf::Bind8::Conf::_insert_in_list($new, $args{"WHERE"}, $args{"WARG"}) or return $ret 0 0 0
121 0 return ($new);
122 }
123  
124  
125 =item inet ()
126  
127 Arguments
128 ADDRESS # optional
129 PORT # optional
130 ALLOW # optional
131  
132 Object method.
133 Argument `ALLOW' can either be an Acl object, or an array reference.
134 Get/sets the corresponding attribute, and returns either the attribute
135 values or true on success, an Err object otherwise. The attribute values
136 are returned as an anonymous array [ ADDRESS PORT ALLOW ], where ALLOW
137 is an Acl object.
138  
139 =cut
140  
141 sub inet
142 0 ▶{
Subroutine inet: not called CC=11 SCAR=48.8
Pod documented
143 0 my ($self, $addr, $port, $allow) = @_;
144 0 my $acl;
145  
146 0 ▶ if ($addr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr) 0 0 0
147 0 ▶ return (Unix::Conf->_err ('inet', "illegal address `$addr'"))
Branch: 1 branch
BranchTrueFalseTotal
unless $addr eq "*" or &__valid_ipaddress($addr) 0 0 0
Condition0%0/3
$addr eq "*" or &__valid_ipaddress($addr)
l!l&&r!l&&!r
000
MC/DC0%0/2
$addr eq "*" or &__valid_ipaddress($addr)
$addr eq "*" &__valid_ipaddress($addr)
Truth table0%0/3
A or B
A= $addr eq "*"B= &__valid_ipaddress($addr)
ABresult
1X1
011
000
148 unless ($addr eq '*' || __valid_ipaddress ($addr));
149 0 ▶ return (Unix::Conf->_err ('inet', "illegal port `$port'"))
Branch: 1 branch
BranchTrueFalseTotal
unless &__valid_port($port) 0 0 0
150 unless (__valid_port ($port));
151 0 ▶ if (ref ($allow)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $allow) { } 0 0 0
152 0 ▶ if (UNIVERSAL::isa ($allow, 'ARRAY')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($allow, 'ARRAY')) { } 0 0 0
elsif (UNIVERSAL::isa($allow, 'Unix::Conf::Bind8::Conf::Acl')) { } 0 0 0
153 0 ▶ $acl = Unix::Conf::Bind8::Conf::Acl->new (
Branch: 1 branch
BranchTrueFalseTotal
$acl = "Unix::Conf::Bind8::Conf::Acl"->new("PARENT", $self->_parent, "ELEMENTS", $allow) or return $acl 0 0 0
154 PARENT => $self->_parent (),
155 ELEMENTS => $allow,
156 ) or return ($acl);
157 }
158 elsif (UNIVERSAL::isa ($allow, 'Unix::Conf::Bind8::Conf::Acl')) {
159 0 $acl = $allow;
160 }
161 else {
162 return (
163 0 Unix::Conf->_err (
164 'inet',
165 "expected arguments are array reference or Unix::Conf::Bind8::Conf::Acl object"
166 )
167 );
168 }
169 }
170 else {
171 # assume a single element
172 0 ▶ $acl = Unix::Conf::Bind8::Conf::Acl->new (
Branch: 1 branch
BranchTrueFalseTotal
$acl = "Unix::Conf::Bind8::Conf::Acl"->new("PARENT", $self - _parent(), "ELEMENTS", [$allow]) or return $acl 0 0 0
173 PARENT => $self-_parent (),
174 ELEMENTS => [ $allow ],
175 ) or return ($acl);
176 }
177 0 $self->{inet} = [ $addr, $port, $acl ];
178 0 $self->dirty (1);
179 0 return (1);
180 }
181 return (
182 0 ▶ defined ($self->{inet}) ? [ @{$self->{inet}} ] :
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'inet'} ? : 0 0 0
183 Unix::Conf->_err ('inet', "inet control channel not defined")
184 );
185 }
186  
187 =item inet_allow
188  
189 Object method.
190 Returns the elements defined for the allow as an anonymous array.
191 if defined, an Err object otherwise.
192  
193 =cut
194  
195 sub inet_allow
196 0 ▶{
Subroutine inet_allow: not called CC=2 SCAR=17.9
Pod documented
197 0 my $self = $_[0];
198  
199 return (
200 0 ▶ defined ($self->{inet}) ? $self->{inet}[I_ALLOW]->elements () :
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'inet'} ? : 0 0 0
201 Unix::Conf->_err ('inet_allow', "inet control channel not defined")
202 );
203 }
204  
205 =item unix ()
206  
207 Arguments
208 PATH,
209 'PERM', # As string.
210 OWNER,
211 GROUP
212  
213 Object method.
214 If arguments are passed, sets it as the value of the corresponding attribute,
215 returns true on success, an Err otherwise. If no arguments are passed, returns
216 the defined value as an anonymous array [ PATH, PERM, OWNER, GROUP ], if defined,
217 an Err object otherwise.
218  
219 NOTE: The PERM argument is to be specified as a string, to avoid
220 unecessary complications. As this module does not interpret these values,
221 it makes sense to use a string, instead of octal.
222  
223 =cut
224  
225 sub unix
226 0 ▶{
Subroutine unix: not called CC=7 SCAR=40.3
Pod documented
227 0 my ($self, $path, $perm, $owner, $group) = @_;
228  
229 0 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 0 0 0
230 0 ▶ $path = qq("$path") if ($path =~ /^[^"]/);
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /^[^"]/ 0 0 0
231 0 ▶ return (Unix::Conf->_err ('unix', "perm `$perm' not a number"))
Branch: 1 branch
BranchTrueFalseTotal
unless $perm =~ /^\d+$/ 0 0 0
232 unless ($perm =~ /^\d+$/);
233 0 ▶ return (Unix::Conf->_err ('unix', "owner `$owner' not a number"))
Branch: 1 branch
BranchTrueFalseTotal
unless $perm =~ /^\d+$/ 0 0 0
234 unless ($perm =~ /^\d+$/);
235 0 ▶ return (Unix::Conf->_err ('unix', "group `$group' not a number"))
Branch: 1 branch
BranchTrueFalseTotal
unless $perm =~ /^\d+$/ 0 0 0
236 unless ($perm =~ /^\d+$/);
237 0 $self->{unix} = [ $path, $perm, $owner, $group ];
238 0 $self->dirty (1);
239 0 return (1);
240 }
241 return (
242 0 ▶ defined ($self->{unix}) ? [ @{$self->{unix}} ] :
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'unix'} ? : 0 0 0
243 Unix::Conf->_err ('unix', "unix control channel not defined")
244 );
245 }
246  
247 =item delete_inet ()
248  
249 Object method.
250 Deletes the `inet' attribute, and returns true, if defined, an Err object
251 otherwise.
252  
253 =cut
254  
255 sub delete_inet
256 0 ▶{
Subroutine delete_inet: not called CC=2 SCAR=17.9
Pod documented
257 0 my $self = $_[0];
258 return (Unix::Conf->_err ('delete_inet', "inet control channel not defined"))
259 0 ▶ unless (defined ($self->{inet}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"inet"} 0 0 0
260 0 delete ($self->{inet});
261 0 $self->dirty (1);
262 0 return (1);
263 }
264  
265 =item delete_unix ()
266  
267 Object method.
268 Deletes the `unix' attribute, and returns true, if defined, an Err object
269 otherwise.
270  
271 =cut
272  
273 sub delete_unix
274 0 ▶{
Subroutine delete_unix: not called CC=2 SCAR=17.9
Pod documented
275 0 my $self = $_[0];
276 return (Unix::Conf->_err ('delete_unix', "unix control channel not defined"))
277 0 ▶ unless (defined ($self->{unix}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"unix"} 0 0 0
278 0 delete ($self->{unix});
279 0 $self->dirty (1);
280 0 return (1);
281 }
282  
283 sub __render
284 0 ▶{
Subroutine __render: not called CC=3 SCAR=24.8
285 0 my $self = $_[0];
286 0 my $tmp;
287
288 0 my $rendered = "controls {\n";
289 0 ▶ if ($tmp = $self->inet ()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp = $self->inet) 0 0 0
290 $rendered .= sprintf (
291 "\tinet $tmp->[I_ADDR] port $tmp->[I_PORT] allow %s\n",
292 0 ${$tmp->[I_ALLOW]->_rstring (undef, 1)}
293 );
294 }
295 0 ▶ if ($tmp = $self->unix ()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp = $self->unix) 0 0 0
296 0 $rendered .=
297 "\tunix $tmp->[U_PATH] perm $tmp->[U_PERM] owner $tmp->[U_OWNER] group $tmp->[U_GROUP];\n"
298 }
299 0 $rendered .= "};";
300 0 return ($self->_rstring (\$rendered));
301 }
302  
303 10 1;
« Bind8/Conf/Key.pm ↑ Summary Bind8/Conf/Parser.pm »