blib/lib/Net/ISC/DHCPd/OMAPI.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 187 SCAR 57.2
CC
17
Cov
0%
connect
42.0
_build__fh
30.0
_cmd
30.0
CRAP 306.0
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 use Moose;
58 use IO::Pty;
59 use Time::HiRes qw/usleep/;
60 use Net::ISC::DHCPd::OMAPI::Control;
61 use Net::ISC::DHCPd::OMAPI::Failover;
62 use Net::ISC::DHCPd::OMAPI::Group;
63 use Net::ISC::DHCPd::OMAPI::Host;
64 use Net::ISC::DHCPd::OMAPI::Lease;
65 use constant DEBUG => $ENV{DHCP_OMAPI_DEBUG} ? 1 : 0;
66  
67 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 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 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 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 has errstr => (
119 is => 'rw',
120 isa => 'Str',
121 default => '',
122 );
123  
124 # meant for internal usage
125 has _fh => (
126 is => 'ro',
127 lazy => 1,
128 builder => '_build__fh',
129 clearer => '_clear__fh',
130 );
131  
132 has _pid => (
133 is => 'rw',
134 isa => 'Int',
135 );
136  
137 # fork omshell and return an IO::Pty object
138 sub _build__fh {
139 my $self = shift;
140 my $pty = IO::Pty->new;
141 my($pid, $slave);
142  
143 pipe my $READ, my $WRITE or confess $!;
144 select +(select($WRITE), $|++)[0]; # autoflush
145  
146 $pid = fork;
147  
148 if(!defined $pid) { # failed
149 $self->errstr($@ = $!);
150 return;
151 }
152 elsif($pid) { # parent
153 close $WRITE;
154 $pty->close_slave;
155 $pty->set_raw;
156 $self->_pid($pid);
157  
158 if(my $error = sysread $READ, my $errno, 255) {
159 $! = $errno + 0;
160 confess "Could not exec $OMSHELL: $!";
161 }
162 if(!defined $pty->sysread(my $buffer, 2048)) {
163 return;
164 }
165  
166 return $pty;
167 }
168 else { # child
169 close $READ;
170 $pty->make_slave_controlling_terminal;
171 $slave = $pty->slave;
172 $slave->set_raw;
173  
174 open STDIN, '<&'. $slave->fileno or confess "Reopen STDIN: $!";
175 open STDOUT, '>&'. $slave->fileno or confess "Reopen STDOUT: $!";
176 open STDERR, '>&'. $slave->fileno or confess "Reopen STDERR: $!";
177  
178 { exec $OMSHELL } # block prevent warning
179 print $WRITE int $!;
180 confess "Could not exec $OMSHELL: $!";
181 }
182 }
183  
184 # $self->_cmd($cmd);
185 sub _cmd {
186 my $self = shift;
187 my $cmd = shift;
188 my $pty = $self->_fh;
189 my $out = q();
190 my $end_time;
191  
192 print STDERR "\$ $cmd\n" if DEBUG;
193  
194 unless(defined $pty->syswrite("$cmd\n")) {
195 $self->errstr($!);
196 return;
197 }
198  
199 $end_time = time + 10;
200  
201 BUFFER:
202 while(time < $end_time) {
203 if(defined $pty->sysread(my $tmp, 1024)) {
204 $out .= $tmp;
205 $out =~ s/>\s$// and last BUFFER;
206 }
207 else {
208 $self->errstr($!);
209 return;
210 }
211 }
212  
213 $out =~ s/^>\s//;
214  
215 print STDERR $out if DEBUG;
216  
217 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 sub connect {
233 my $self = shift;
234 my @commands = qw/port server/;
235 my $buffer;
236  
237 push @commands, 'key' if($self->key);
238  
239 $self->errstr('');
240  
241 for my $attr (@commands) {
242 $buffer = $self->_cmd(sprintf "%s %s", $attr, $self->$attr);
243 last unless(defined $buffer);
244 }
245  
246 if($self->errstr) {
247 return;
248 }
249 unless($buffer = $self->_cmd('connect')) {
250 return;
251 }
252 unless($buffer =~ /obj:\s+/) {
253 $self->errstr($buffer);
254 return;
255 }
256  
257 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 sub disconnect {
271 my $self = shift;
272 my $retries = 10;
273  
274 while($retries--) {
275 kill 15, $self->_pid;
276 usleep 2e3;
277 if(kill 0, $self->_pid) {
278 $retries = 1; # make sure it's true
279 last;
280 }
281 }
282  
283 unless($retries) {
284 return;
285 }
286  
287 $self->_clear__fh;
288  
289 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 sub new_object {
308 my $self = shift;
309 my $type = shift or return;
310 my %args = @_;
311 my $class = 'Net::ISC::DHCPd::OMAPI::' .ucfirst(lc $type);
312  
313 unless($type =~ /^(?:control|failover|group|host|lease)$/i) {
314 return;
315 }
316  
317 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 __PACKAGE__->meta->make_immutable;
328 1;