File Coverage

blib/lib/Unix/Conf/Bind8/Conf/Controls.pm
Criterion Covered Total %
statement 36 99 36.3
branch 0 54 0.0
condition 0 7 0.0
subroutine 12 19 63.1
pod 6 6 100.0
total 54 185 29.1


line stmt bran cond sub pod time code
1             # Bind8 package directive handling
2             #
3             # Copyright Karthik Krishnamurthy
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     10   52 use strict;
  10         19  
  10         353  
67 10     10   49 use warnings;
  10         20  
  10         243  
68 10     10   48 use Unix::Conf;
  10         18  
  10         284  
69 10     10   55 use Unix::Conf::Bind8::Conf::Directive;
  10         16  
  10         443  
70             our (@ISA) = qw (Unix::Conf::Bind8::Conf::Directive);
71              
72 10     10   56 use Unix::Conf::Bind8::Conf::Lib;
  10         44  
  10         1331  
73              
74              
75 10     10   62 use constant I_ADDR => 0;
  10         17  
  10         650  
76 10     10   60 use constant I_PORT => 1;
  10         24  
  10         431  
77 10     10   47 use constant I_ALLOW => 2;
  10         20  
  10         416  
78              
79 10     10   46 use constant U_PATH => 0;
  10         19  
  10         402  
80 10     10   48 use constant U_PERM => 1;
  10         19  
  10         433  
81 10     10   48 use constant U_OWNER => 2;
  10         22  
  10         491  
82 10     10   47 use constant U_GROUP => 3;
  10         17  
  10         23252  
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             {
105 0     0 1   shift ();
106 0           my %args = @_;
107 0           my $new = bless ({});
108 0           my $ret;
109              
110 0 0         $args{PARENT} || return (Unix::Conf->_err ('new', "PARENT not defined"));
111 0 0         $ret = $new->_parent ($args{PARENT}) or return ($ret);
112 0 0 0       $ret = $new->inet (@{$args{INET}}) or return ($ret)
  0            
113             if ($args{INET});
114 0 0 0       $ret = $new->unix (@{$args{UNIX}}) or return ($ret)
  0            
115             if ($args{UNIX});
116 0 0         $ret = Unix::Conf::Bind8::Conf::_add_controls ($new)
117             or return ($ret);
118 0 0         $args{WHERE} = 'LAST' unless ($args{WHERE});
119 0 0         $ret = Unix::Conf::Bind8::Conf::_insert_in_list ($new, $args{WHERE}, $args{WARG})
120             or return ($ret);
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             {
143 0     0 1   my ($self, $addr, $port, $allow) = @_;
144 0           my $acl;
145              
146 0 0         if ($addr) {
147 0 0 0       return (Unix::Conf->_err ('inet', "illegal address `$addr'"))
148             unless ($addr eq '*' || __valid_ipaddress ($addr));
149 0 0         return (Unix::Conf->_err ('inet', "illegal port `$port'"))
150             unless (__valid_port ($port));
151 0 0         if (ref ($allow)) {
152 0 0         if (UNIVERSAL::isa ($allow, 'ARRAY')) {
    0          
153 0 0         $acl = Unix::Conf::Bind8::Conf::Acl->new (
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 0         $acl = Unix::Conf::Bind8::Conf::Acl->new (
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 0         defined ($self->{inet}) ? [ @{$self->{inet}} ] :
  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             {
197 0     0 1   my $self = $_[0];
198              
199             return (
200 0 0         defined ($self->{inet}) ? $self->{inet}[I_ALLOW]->elements () :
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             {
227 0     0 1   my ($self, $path, $perm, $owner, $group) = @_;
228              
229 0 0         if ($path) {
230 0 0         $path = qq("$path") if ($path =~ /^[^"]/);
231 0 0         return (Unix::Conf->_err ('unix', "perm `$perm' not a number"))
232             unless ($perm =~ /^\d+$/);
233 0 0         return (Unix::Conf->_err ('unix', "owner `$owner' not a number"))
234             unless ($perm =~ /^\d+$/);
235 0 0         return (Unix::Conf->_err ('unix', "group `$group' not a number"))
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 0         defined ($self->{unix}) ? [ @{$self->{unix}} ] :
  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             {
257 0     0 1   my $self = $_[0];
258 0 0         return (Unix::Conf->_err ('delete_inet', "inet control channel not defined"))
259             unless (defined ($self->{inet}));
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             {
275 0     0 1   my $self = $_[0];
276 0 0         return (Unix::Conf->_err ('delete_unix', "unix control channel not defined"))
277             unless (defined ($self->{unix}));
278 0           delete ($self->{unix});
279 0           $self->dirty (1);
280 0           return (1);
281             }
282              
283             sub __render
284             {
285 0     0     my $self = $_[0];
286 0           my $tmp;
287            
288 0           my $rendered = "controls {\n";
289 0 0         if ($tmp = $self->inet ()) {
290 0           $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 0         if ($tmp = $self->unix ()) {
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             1;