DHCPd/OMAPI.pm

filter: Statementstmt 30.0% 36 / 120 Branchbran 2.1% 1 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 64.2% 9 / 14 Podpod 100% 3 / 3 totaltotal 26.7% 49 / 183 CC 25 SCAR 36.6
CC
25
Cov
22%
_build__fh
90.0
connect
56.0
_cmd
30.0
CRAP 38.9
« DHCPd/OMAPI/Actions.pm ↑ Summary DHCPd/Config/Block.pm »
1 package Net::ISC::DHCPd::OMAPI;
2  
3 =head1 NAME
4  
5 Net::ISC::DHCPd::OMAPI - Talk to a dhcp server
6  
7 =head1 SYNOPSIS
8  
9 my $omapi = Net::ISC::DHCPd::OMAPI->new(
10 key => "dhcpd secret",
11 );
12  
13 # connect is lazy
14 $omapi->connect
15  
16 my $lease = $omapi->new_object(lease => (
17 ip_address => "10.19.83.200",
18 ));
19  
20 if($lease->read) {
21 printf("Got hardware_address=%s from ip_address=%s\n",
22 $lease->hardware_address,
23 $lease->ip_address,
24 );
25 }
26  
27 =head1 DESCRIPTION
28  
29 This module provides an API to query and possible change the ISC DHCPd
30 server. The module use OMAPI (Object Management API) which does not
31 require the server to be restarted for changes to apply. It does
32 unfortunately support the protocol natively, but instead fork
33 C<omshell(1)> which this module read and write commands to.
34  
35 OMAPI is simply a communications mechanism that allows you to manipulate
36 objects, which is stored in the dhcpd.leases file.
37  
38 See subclasses for more information about the different objects you can
39 manipulate:
40 L<Net::ISC::DHCPd::OMAPI::Failover>,
41 L<Net::ISC::DHCPd::OMAPI::Group>,
42 L<Net::ISC::DHCPd::OMAPI::Host>,
43 and L<Net::ISC::DHCPd::OMAPI::Lease>.
44  
45 =head1 ENVIRONMENT VARIABLES
46  
47 =over 4
48  
49 =item * DHCP_OMAPI_DEBUG=1
50  
51 This variable will enable debug output.
52  
53 =back
54  
55 =cut
56  
57 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 ▶use IO::Pty;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use Time::HiRes qw/usleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶use Net::ISC::DHCPd::OMAPI::Control;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use Net::ISC::DHCPd::OMAPI::Failover;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use Net::ISC::DHCPd::OMAPI::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶use Net::ISC::DHCPd::OMAPI::Host;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶use Net::ISC::DHCPd::OMAPI::Lease;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 ▶use constant DEBUG => $ENV{DHCP_OMAPI_DEBUG} ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'DHCP_OMAPI_DEBUG'} ? : 0 2 2
66  
67 2 our $OMSHELL = 'omshell';
68  
69 =head1 ATTRIBUTES
70  
71 =head2 server
72  
73 This attribute is read-only and holds a string describing the
74 remote dhcpd server address. Default value is "127.0.0.1".
75  
76 =cut
77  
78 2 has server => (
79 is => 'ro',
80 isa => 'Str',
81 default => '127.0.0.1',
82 );
83  
84 =head2 port
85  
86 This attribute is read-only and holds an integer representing
87 the remote dhcpd server port. Default value is "7911".
88  
89 =cut
90  
91 2 has port => (
92 is => 'ro',
93 isa => 'Int',
94 default => 7911,
95 );
96  
97 =head2 key
98  
99 This attribute is read-only and holds a string representing the
100 server secret key. It is in the format C<$name $secret> and the
101 default value is an empty string. An empty string is used for
102 servers without a secret to log in.
103  
104 =cut
105  
106 2 has key => (
107 is => 'ro',
108 isa => 'Str',
109 default => '',
110 );
111  
112 =head2 errstr
113  
114 Holds the last know error as a plain string.
115  
116 =cut
117  
118 2 has errstr => (
119 is => 'rw',
120 isa => 'Str',
121 default => '',
122 );
123  
124 # meant for internal usage
125 2 has _fh => (
126 is => 'ro',
127 lazy => 1,
128 builder => '_build__fh',
129 clearer => '_clear__fh',
130 );
131  
132 2 has _pid => (
133 is => 'rw',
134 isa => 'Int',
135 );
136  
137 # fork omshell and return an IO::Pty object
138 0 ▶sub _build__fh {
Subroutine _build__fh: not called CC=9 SCAR=45.0
139 0 my $self = shift;
140 0 my $pty = IO::Pty->new;
141 0 my($pid, $slave);
142  
143 0 ▶ pipe my $READ, my $WRITE or confess $!;
Branch: 1 branch
BranchTrueFalseTotal
pipe my $READ, my $WRITE or confess($!) 0 0 0
144 0 select +(select($WRITE), $|++)[0]; # autoflush
145  
146 0 $pid = fork;
147  
148 0 ▶ if(!defined $pid) { # failed
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 0 0
elsif ($pid) { } 0 0 0
149 0 $self->errstr($@ = $!);
150 0 return;
151 }
152 elsif($pid) { # parent
153 0 close $WRITE;
154 0 $pty->close_slave;
155 0 $pty->set_raw;
156 0 $self->_pid($pid);
157  
158 0 ▶ if(my $error = sysread $READ, my $errno, 255) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = sysread($READ, my $errno, 255)) 0 0 0
159 0 $! = $errno + 0;
160 0 confess "Could not exec $OMSHELL: $!";
161 }
162 0 ▶ if(!defined $pty->sysread(my $buffer, 2048)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pty->sysread(my $buffer, 2048)) 0 0 0
163 0 return;
164 }
165  
166 0 return $pty;
167 }
168 else { # child
169 0 close $READ;
170 0 $pty->make_slave_controlling_terminal;
171 0 $slave = $pty->slave;
172 0 $slave->set_raw;
173  
174 0 ▶ open STDIN, '<&'. $slave->fileno or confess "Reopen STDIN: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, '<&' . $slave->fileno or confess("Reopen STDIN: $!") 0 0 0
175 0 ▶ open STDOUT, '>&'. $slave->fileno or confess "Reopen STDOUT: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, '>&' . $slave->fileno or confess("Reopen STDOUT: $!") 0 0 0
176 0 ▶ open STDERR, '>&'. $slave->fileno or confess "Reopen STDERR: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, '>&' . $slave->fileno or confess("Reopen STDERR: $!") 0 0 0
177  
178 0 { exec $OMSHELL } # block prevent warning
179 0 print $WRITE int $!;
180 0 confess "Could not exec $OMSHELL: $!";
181 }
182 }
183  
184 # $self->_cmd($cmd);
185 0 ▶sub _cmd {
Subroutine _cmd: not called CC=5 SCAR=34.0
186 0 my $self = shift;
187 0 my $cmd = shift;
188 0 my $pty = $self->_fh;
189 0 my $out = q();
190 0 my $end_time;
191  
192 0 print STDERR "\$ $cmd\n" if DEBUG;
193  
194 0 ▶ unless(defined $pty->syswrite("$cmd\n")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pty->syswrite("$cmd\n")) 0 0 0
195 0 $self->errstr($!);
196 0 return;
197 }
198  
199 0 $end_time = time + 10;
200  
201 BUFFER:
202 0 ▶ while(time < $end_time) {
Branch: 1 branch
BranchTrueFalseTotal
if (time < $end_time) 0 0 0
203 0 ▶ if(defined $pty->sysread(my $tmp, 1024)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pty->sysread(my $tmp, 1024)) { } 0 0 0
204 0 $out .= $tmp;
205 0 ▶ $out =~ s/>\s$// and last BUFFER;
Branch: 1 branch
BranchTrueFalseTotal
$out =~ s/>\s$// and last BUFFER 0 0 0
206 }
207 else {
208 0 $self->errstr($!);
209 0 return;
210 }
211 }
212  
213 0 $out =~ s/^>\s//;
214  
215 0 print STDERR $out if DEBUG;
216  
217 0 return $out;
218 }
219  
220 =head1 METHODS
221  
222 =head2 connect
223  
224 $bool = $self->connect;
225  
226 Will open a connection to the dhcp server. Check L</errstr> on failure.
227 A connection means starting the program C<omshell(1)> and trying to
228 log in, if the dhcpd L</key> is set.
229  
230 =cut
231  
232 0 ▶sub connect {
Subroutine connect: not called CC=7 SCAR=40.3
Pod documented
233 0 my $self = shift;
234 0 my @commands = qw/port server/;
235 0 my $buffer;
236  
237 0 ▶ push @commands, 'key' if($self->key);
Branch: 1 branch
BranchTrueFalseTotal
if $self->key 0 0 0
238  
239 0 $self->errstr('');
240  
241 0 for my $attr (@commands) {
242 0 $buffer = $self->_cmd(sprintf "%s %s", $attr, $self->$attr);
243 0 ▶ last unless(defined $buffer);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buffer 0 0 0
244 }
245  
246 0 ▶ if($self->errstr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->errstr) 0 0 0
247 0 return;
248 }
249 0 ▶ unless($buffer = $self->_cmd('connect')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buffer = $self->_cmd("connect")) 0 0 0
250 0 return;
251 }
252 0 ▶ unless($buffer =~ /obj:\s+/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buffer =~ /obj:\s+/) 0 0 0
253 0 $self->errstr($buffer);
254 0 return;
255 }
256  
257 0 return 1;
258 }
259  
260 =head2 disconnect
261  
262 $bool = $self->disconnect;
263  
264 Will disconnect from the server. This means killing the C<omshell(1)>
265 program, which then actually will make sure the connection is shut
266 down.
267  
268 =cut
269  
270 0 ▶sub disconnect {
Subroutine disconnect: not called CC=4 SCAR=30.0
Pod documented
271 0 my $self = shift;
272 0 my $retries = 10;
273  
274 0 ▶ while($retries--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retries--) 0 0 0
275 0 kill 15, $self->_pid;
276 0 usleep 2e3;
277 0 ▶ if(kill 0, $self->_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $self->_pid) 0 0 0
278 0 $retries = 1; # make sure it's true
279 0 last;
280 }
281 }
282  
283 0 ▶ unless($retries) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($retries) 0 0 0
284 0 return;
285 }
286  
287 0 $self->_clear__fh;
288  
289 0 return 1;
290 }
291  
292 =head2 new_object
293  
294 $object = $self->new_object($type => %constructor_args);
295  
296 This method will create a new OMAPI object, which can be used to query
297 and/or manipulate the running dhcpd server.
298  
299 C<$type> can be "group", "host", or "lease". Will return a new config object.
300  
301 Example, with C<$type='host'>:
302  
303 Net::ISC::DHCPd::Config::Host->new(%constructor_args);
304  
305 =cut
306  
307 0 ▶sub new_object {
Subroutine new_object: not called CC=3 SCAR=24.8
Pod documented
308 0 my $self = shift;
309 0 ▶ my $type = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $type = shift() or return 0 0 0
310 0 my %args = @_;
311 0 my $class = 'Net::ISC::DHCPd::OMAPI::' .ucfirst(lc $type);
312  
313 0 ▶ unless($type =~ /^(?:control|failover|group|host|lease)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /^(?:control|failover|group|host|lease)$/i) 0 0 0
314 0 return;
315 }
316  
317 0 return $class->new(parent => $self, %args);
318 }
319  
320 =head1 COPYRIGHT & LICENSE
321  
322 =head1 AUTHOR
323  
324 See L<Net::ISC::DHCPd>.
325  
326 =cut
327 2 __PACKAGE__->meta->make_immutable;
328 2 1;
« DHCPd/OMAPI/Actions.pm ↑ Summary DHCPd/Config/Block.pm »