File Coverage

blib/lib/Unix/Conf/Bind8/Conf/Server.pm
Criterion Covered Total %
statement 18 86 20.9
branch 0 52 0.0
condition n/a
subroutine 6 12 50.0
pod 3 3 100.0
total 27 153 17.6


line stmt bran cond sub pod time code
1             # Bind8 server handling
2             #
3             # Copyright Karthik Krishnamurthy
4              
5             =head1 NAME
6              
7             Unix::Conf::Bind8::Conf::Server - Class for handling Bind8 configuration
8             directive `server'
9              
10             =head1 SYNOPSIS
11              
12             use Unix::Conf::Bind8;
13              
14             my ($conf, $server, $ret);
15              
16             $conf = Unix::Conf::Bind8->new_conf (
17             FILE => '/etc/named.conf',
18             SECURE_OPEN => 1,
19             ) or $conf->die ("couldn't open `named.conf'");
20              
21             #
22             # Ways to get a server object.
23             #
24              
25             $server = $conf->new_server (
26             NAME => '192.168.1.1',
27             BOGUS => 'yes',
28             ) or $server->die ("couldn't create server `192.168.1.1'");
29              
30             # OR
31              
32             $server = $conf->get_server ('10.0.0.1')
33             or $server->die ("couldn't get server `10.0.0.1'");
34              
35             #
36             # Operations that can be performed on a server object.
37             #
38              
39             $ret = $server->bogus ('no')
40             or $ret->die ("couldn't set attribute");
41              
42             $ret = $server->keys (qw (extremix-slaves.key sample_key));
43            
44             # get attributes
45             $ret = $server->keys ()
46             or $ret->die ("couldn't get keys");
47             local $" = "\n";
48             printf "Keys defined:\n@$ret\n";
49              
50             # delete attribute
51             $ret = $server->delete_transfer_format ()
52             or $ret->die ("couldn't delete attribute");
53              
54             =head1 METHODS
55              
56             =cut
57              
58              
59             package Unix::Conf::Bind8::Conf::Server;
60              
61 10     10   48 use strict;
  10         26  
  10         315  
62 10     10   50 use warnings;
  10         14  
  10         248  
63 10     10   87 use Unix::Conf;
  10         16  
  10         211  
64 10     10   44 use Unix::Conf::Bind8::Conf::Directive;
  10         77  
  10         413  
65             our (@ISA) = qw (Unix::Conf::Bind8::Conf::Directive);
66 10     10   54 use Unix::Conf::Bind8::Conf::Lib;
  10         91  
  10         7283  
67              
68             my %ServerDirectives = (
69             'bogus' => \&__valid_yesno,
70             # the man page doesn't mention this, but the
71             # sample conf file has it
72             'support-ixfr' => \&__valid_yesno,
73             'transfers' => \&__valid_number,
74             'transfer-format' => \&__valid_transfer_format,
75              
76             'keys' => 0,
77             );
78              
79             =over 4
80              
81             =item new ()
82              
83             Arguments
84             NAME => scalar,
85             BOGUS => scalar, # Optional
86             TRANSFERS => scalar, # Optional
87             SUPPORT-IXFR => scalar,
88             TRANSFER-FORMAT
89             => scalar, # Optional
90             KEYS => [elements ], # Optional
91             WHERE => 'FIRST'|'LAST'|'BEFORE'|'AFTER'
92             WARG => Unix::Conf::Bind8::Conf::Directive subclass object
93             # WARG is to be provided only in case WHERE eq 'BEFORE
94             # or WHERE eq 'AFTER'
95             PARENT => reference,
96             # to the Conf object datastructure.
97              
98             Class constructor.
99             Creates a new Unix::Conf::Bind8::Conf::Server object, and returns it,
100             on success, an Err object otherwise. Do not call this constructor
101             directly. Use Unix::Conf::Bind8::Conf::new_server () instead.
102              
103             =cut
104              
105             sub new
106             {
107 0     0 1   shift ();
108 0           my $new = bless ({});
109 0           my %args = @_;
110 0           my $ret;
111              
112 0 0         $args{PARENT} || return (Unix::Conf->_err ('new', "PARENT not defined"));
113 0 0         $args{NAME} || return (Unix::Conf->_err ('new', "NAME not defined"));
114              
115 0 0         $ret = $new->_parent ($args{PARENT}) or return ($ret);
116 0 0         $ret = $new->name ($args{NAME}) or return ($ret);
117 0 0         my $where = $args{WHERE} ? $args{WHERE} : 'LAST';
118 0           my $warg = $args{WARG};
119 0           delete (@args{'PARENT','NAME','WHERE','WARG'});
120              
121 0           for (keys (%args)) {
122 0           my $meth = $_;
123 0           $meth =~ tr/A-Z/a-z/;
124 0 0         return (Unix::Conf->_err ("new", "attribute `$meth' not supported"))
125             unless (defined ($ServerDirectives{$meth}));
126 0           $meth =~ tr/-/_/;
127 0 0         $ret = $new->$meth ($args{$_}) or return ($ret);
128             }
129              
130 0 0         $ret = Unix::Conf::Bind8::Conf::_insert_in_list ($new, $where, $warg)
131             or return ($ret);
132              
133 0           return ($new);
134             }
135              
136              
137             =item name ()
138              
139             Arguments
140             value # optional
141              
142             Object method.
143             Get/set the name attribute in the invocant. Returns the attribute value
144             or true on success, an Err object otherwise.
145              
146             =cut
147              
148             sub name
149             {
150 0     0 1   my ($self, $name) = @_;
151              
152 0 0         if ($name) {
153 0           my $ret;
154 0 0         return (Unix::Conf->_err ('name', "illegal name `$name'"))
155             unless (__valid_ipaddress ($name));
156 0           $self->{name} = $name;
157 0 0         $ret = Unix::Conf::Bind8::Conf::_add_server ($self)
158             or return ($ret);
159 0           $ret = $self->dirty (1);
160 0           return (1);
161             }
162             return (
163 0 0         defined ($self->{name}) ? $self->{name} :
164             Unix::Conf->_err ('name', "name not defined for server")
165             );
166             }
167              
168             =item bogus ()
169              
170             Arguments
171             value # optional
172              
173             Object method.
174             Get/set the name attribute in the invocant. Returns the attribute value
175             or true on success, an Err object otherwise.
176              
177             =cut
178              
179             =item delete_bogus ()
180              
181             Object method.
182             Deletes the corresponding attribute, if defined, and returns true,
183             an Err object otherwise.
184              
185             =cut
186              
187             =item support_ixfr ()
188              
189             Arguments
190             value # optional
191              
192             Object method.
193             Get/set the name attribute in the invocant. Returns the attribute value
194             or true on success, an Err object otherwise.
195              
196             =cut
197              
198             =item delete_support_ixfr ()
199              
200             Object method.
201             Deletes the corresponding attribute, if defined, and returns true,
202             an Err object otherwise.
203              
204             =cut
205              
206             =item transfers ()
207              
208             Arguments
209             value # number, optional
210              
211             Object method.
212             Get/set the name attribute in the invocant. Returns the attribute value
213             or true on success, an Err object otherwise.
214              
215             =cut
216              
217             =item delete_transfers ()
218              
219             Object method.
220             Deletes the corresponding attribute, if defined, and returns true,
221             an Err object otherwise.
222              
223             =cut
224              
225             =item transfer_format ()
226              
227             Arguments
228             value # 'one-answer'|'many-answers', optional
229              
230             Object method.
231             Get/set the name attribute in the invocant. Returns the attribute value
232             or true on success, an Err object otherwise.
233              
234             =cut
235              
236             =item delete_transfer_format ()
237              
238             Object method.
239             Deletes the corresponding attribute, if defined, and returns true,
240             an Err object otherwise.
241              
242             =cut
243              
244             for my $dir (keys (%ServerDirectives)) {
245 10     10   63 no strict 'refs';
  10         16  
  10         8408  
246            
247             my $meth = $dir;
248             $meth =~ tr/-/_/;
249              
250             ($ServerDirectives{$dir} =~ /^CODE/) && do {
251             *$meth = sub {
252 0     0     my ($self, $arg) = @_;
253              
254 0 0         if (defined ($arg)) {
255 0           return (Unix::Conf->_err ("$meth", "invalid argument `$arg'"))
256 0 0         unless (&{$ServerDirectives{$dir}}($arg));
257 0           $self->{$dir} = $arg;
258 0           $self->dirty (1);
259 0           return (1);
260             }
261             return (
262 0 0         defined ($self->{$dir}) ? $self->{$dir} :
263             Unix::Conf->_err ("$dir", "`$dir' not defined")
264             );
265             };
266             };
267             *{"delete_$meth"} = sub {
268 0     0     my $self = $_[0];
269              
270 0 0         return (Unix::Conf->_err ("delete_$meth", "`$dir' not defined"))
271             unless (defined ($self->{$dir}));
272 0           delete ($self->{$dir});
273 0           $self->dirty (1);
274 0           return (1);
275             };
276             }
277              
278             =item keys ()
279              
280             Arguments
281             LIST # name, optional
282             or
283             [ LIST ]
284              
285             Object method.
286             Get/set the name attribute in the invocant. Returns the attribute value
287             or true on success, an Err object otherwise.
288              
289             =cut
290              
291             =item delete_keys ()
292              
293             Object method.
294             Deletes the corresponding attribute, if defined, and returns true,
295             an Err object otherwise.
296              
297             =cut
298              
299             sub keys
300             {
301 0     0 1   my $self = shift ();
302              
303 0 0         if (@_) {
304 0           my $args;
305 0 0         if (ref ($_[0])) {
306 0 0         return (Unix::Conf->_err ('keys', "expected argument LIST or [ LIST ]"))
307             unless (UNIVERSAL::isa ($_[0], 'ARRAY'));
308 0           $args = $_[0];
309             }
310             else {
311             # assume a list
312 0           $args = \@_;
313             }
314              
315 0           for (@$args) {
316 0 0         return (Unix::Conf->_err ('keys', "`$_' not a valid key"))
317             unless (Unix::Conf::Bind8::Conf::_get_key ($self->_parent (), $_));
318             }
319 0           @{$self->{keys}}{@$args} = (1) x @$args;
  0            
320 0           $self->dirty (1);
321 0           return (1);
322             }
323 0 0         return (Unix::Conf->_err ('keys', "keys not defined"))
324             unless ($self->{keys});
325 0           return ([ keys (%{$self->{keys}}) ]);
  0            
326             }
327              
328             # no add_to_keys, delete_from_keys as there are not likely to be
329             # many keys in for one server.
330              
331             sub __render
332             {
333 0     0     my $self = $_[0];
334 0           my ($rendered, $tmp);
335              
336 0           $tmp = $self->name ();
337 0           $rendered = "server $tmp {\n";
338 0 0         $rendered .= "\tbogus $tmp;\n"
339             if ($tmp = $self->bogus ());
340 0 0         $rendered .= "\tsupport-ixfr $tmp;\n"
341             if ($tmp = $self->support_ixfr ());
342 0 0         $rendered .= "\ttransfers $tmp;\n"
343             if ($tmp = $self->transfers ());
344 0 0         $rendered .= "\ttransfer-format $tmp;\n"
345             if ($tmp = $self->transfer_format ());
346 0 0         $rendered .= "\tkeys { @$tmp };\n"
347             if ($tmp = $self->keys ());
348 0           $rendered .= "};";
349 0           return ($self->_rstring (\$rendered));
350             }
351              
352             1;