Utilities/Generator/ISC_DHCPD.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Utilities.pm ↑ Summary
1 package Net::DHCP::Config::Utilities::Generator::ISC_DHCPD;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::DHCP::Config::Utilities::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use String::ShellQuote;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 =head1 NAME
11  
12 Net::DHCP::Config::Utilities::Generator::ISC_DHCPD - Generates a config for ISC DHCPD from the supplied subnets.
13  
14 =head1 VERSION
15  
16 Version 0.1.0
17  
18 =cut
19  
20 our $VERSION = '0.1.0';
21  
22  
23 =head1 SYNOPSIS
24  
25 use Net::DHCP::Config::Utilities::Generator::ISC_DHCPD;
26
27 my $options={
28 output=>'./dhcp/dhcpd.conf',
29 header=>'./dhcp/header.tt',
30 footer=>'./dhcp/footer.tt',
31 args=>{},
32 };
33
34 my $generator = Net::DHCP::Config::Utilities::Subnet->new( $options );
35
36 eval{
37 $generator->generate( $dhcp_util );
38 };
39 if ( $@ ){
40 # do something upon error
41 die ( $@ );
42 }
43
44 # just return it and don't write it output
45 my $config;
46 eval{
47 $config=$generator->generate( $dhcp_util );
48 };
49 if ( $@ ){
50 # do something upon error
51 die ( $@ );
52 }
53 print $config;
54  
55 =head1 METHODS
56  
57 =head2 new
58  
59 This initiates the object.
60  
61 my $options={
62 output=>'./dhcp/dhcpd.conf',
63 header=>'./dhcp/header.tt',
64 footer=>'./dhcp/footer.tt',
65 vars=>{},
66 };
67
68 my $generator = Net::DHCP::Config::Utilities::Generator::ISC_DHCPD->new( $options );
69  
70 =head3 args
71  
72 =head4 output
73  
74 This is the file to write the output too.
75  
76 =head4 header
77  
78 This is the header template to use.
79  
80 =head4 footer
81  
82 This is the footer teomplate to use.
83  
84 =head4 vars
85  
86 This is a hash containing values to pass to L<Template>
87 as the \%vars value for when calling L<Template>->process.
88  
89 =cut
90  
91 sub new {
92 my %args;
93 if ( defined( $_[1] ) ){
94 %args=%{$_[1]};
95 }
96  
97 # make sure we have all the required variables
98 # also make sure footer, output, and header are not all the same files
99 if ( !defined( $args{header} ) ){
100 die( 'No header file specified' );
101 }elsif( !defined( $args{footer} ) ){
102 die( 'No footer file specified' );
103 }elsif( !defined( $args{output} ) ){
104 die( 'No output file specified' );
105 }elsif( !defined( $args{vars} ) ){
106 die( 'No vars hash defined' );
107 }elsif( ! -f $args{header} ){
108 die( '"'.$args{header}.'" does not exist or is not a file');
109 }elsif( ! -f $args{footer} ){
110 die( '"'.$args{footer}.'" does not exist or is not a file');
111 }elsif( ref( $args{vars} ) ne 'HASH' ){
112 die( '$args{vars} is not a hash' );
113 }elsif( $args{footer} eq $args{header} ){
114 die( '$args{footer} and $args{header} are both the same, "'.$args{footer}.'",' );
115 }elsif( $args{footer} eq $args{output} ){
116 die( '$args{footer} and $args{output} are both the same, "'.$args{footer}.'",' );
117 }elsif( $args{header} eq $args{output} ){
118 die( '$args{header} and $args{output} are both the same, "'.$args{header}.'",' );
119 }
120  
121 my $self={
122 vars=>$args{vars},
123 footer=>$args{footer},
124 header=>$args{header},
125 output=>$args{output},
126 options=>Net::DHCP::Config::Utilities::Options->new,
127 };
128 bless $self;
129  
130 return $self;
131 }
132  
133 =head2 generate
134  
135 This gnerates the config for ISC DHCPD.
136  
137 There are two options taken.
138  
139 The first and mandatory is a L<Net::DHCP::Config::Utilities> object
140 that contains the subnets that we want to generate a config for.
141  
142 The second is we want to write the output to the file or not. This is
143 optional and if set to true no output will be writen.
144  
145 This will return a string with the generated config.
146  
147 If it is being outputed to a file, then ISC DHCPD will be called
148 as 'dhcpd -t -cf $output' to lint it. It will die if it exits
149 with a non-zero value.
150  
151 eval{
152 $generator->generate( $dhcp_util );
153 };
154 if ( $@ ){
155 # do something upon error
156 die ( $@ );
157 }
158  
159 # just return it and don't write it output
160 my $config;
161 eval{
162 $config=$generator->generate( $dhcp_util );
163 };
164 if ( $@ ){
165 # do something upon error
166 die ( $@ );
167 }
168 print $config;
169  
170 =cut
171  
172 sub generate{
173 my $self=$_[0];
174 my $object=$_[1];
175 my $no_write=$_[2];
176  
177 if ( ref( $object ) ne 'Net::DHCP::Config::Utilities' ){
178 die( 'The passed object is not a "Net::DHCP::Config::Utilities", but "'.ref( $object ).'"' );
179 }
180  
181 # init the template object and then process the header/footer
182 my $template=Template->new({
183 EVAL_PERL=>1,
184 INTERPOLATE=>1,
185 POST_CHOMP=>1,
186 });
187 my $header;
188 $template->process( $self->{header}, $self->{vars}, \$header )
189 || die( 'Failed to process the header, "'.$self->{header}.'"... '.$template->error );
190 my $footer;
191 $template->process( $self->{footer}, $self->{vars}, \$footer )
192 || die( 'Failed to process the footer, "'.$self->{footer}.'"... '.$template->error );
193  
194 my $middle='';
195  
196 my @subnets=sort( $object->subnet_list );
197 foreach my $base ( @subnets ){
198 my $subnet=$object->subnet_get( $base );
199  
200 my $desc=$subnet->desc_get;
201 if ( $desc ne '' ){
202 $desc='# '.$desc."\n";
203 }
204  
205 $middle=$middle.$desc.'subnet '.$base.' netmask '.$subnet->mask_get." {\n";
206  
207 # add any required ranges
208 # unless you have static IPs in the footer you really need ranges
209 my @ranges=sort( $subnet->range_get );
210 foreach my $range ( @ranges ){
211 $middle=$middle.' range '.$range.";\n";
212 }
213  
214 my @options=sort( $subnet->options_list );
215 foreach my $option ( @options ){
216 my $value=$subnet->option_get( $option );
217 my $long=$self->{options}->get_long( $option );
218 if ( defined ( $value ) ){
219 # handle the tftp boot stuff specially thanks to ISC DHCPD not appending
220 # option to those values for some bloody unknown reason
221 if (
222 ($option eq 'next-server') ||
223 ($option eq 'tftp-server')
224 ){
225 $middle=$middle.' next-server'.$value.";\n";
226 }elsif(
227 ($option eq 'filename') ||
228 ($option eq 'bootfile')
229 ){
230 $middle=$middle.' filename'.$value.";\n";
231 }else{
232 $middle=$middle.' option '.$long.' '.$value.";\n";
233 }
234 }
235 }
236  
237 $middle=$middle."}\n\n";
238 }
239  
240 if ( ! $no_write ){
241 my $fh;
242 open( $fh, '>', $self->{output} ) or die( 'Can not open "'.$self->{output}.'" for writing output to,,, C errno='.$! );
243 print $fh $header.$middle.$footer;
244 close( $fh );
245  
246 # 2> /dev/null used to prevent this from being noisy
247 my $dhcpd_bin=`/bin/sh -c 'which dhcpd 2> /dev/null'`;
248 if ( $? == 0 ){
249 my $quoted=shell_quote( $self->{output} );
250 my $check=`/bin/sh -c 'dhcpd -t -cf $quoted 2> /dev/null'`;
251 if ( $? != 0 ){
252 die('Failed to lint the output file,"'.$self->{output}.'",');
253 }
254 }
255 }
256  
257 return $header.$middle.$footer;
258 }
259  
260 =head1 TEMPLATES
261  
262 A good base header template is as below.
263  
264 default-lease-time 600;
265 max-lease-time 7200;
266
267 ddns-update-style none;
268
269 authoritative;
270
271 option web-proxy code 252 = text;
272
273 log-facility local7;
274
275  
276 In general the footer will be left empty.
277 It is largely for use if you have like static hosts.
278  
279 =head1 AUTHOR
280  
281 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
282  
283 =head1 BUGS
284  
285 Please report any bugs or feature requests to C<bug-net-dhcp-config-utilities at rt.cpan.org>, or through
286 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-DHCP-Config-Utilities>. I will be notified, and then you'll
287 automatically be notified of progress on your bug as I make changes.
288  
289  
290  
291  
292 =head1 SUPPORT
293  
294 You can find documentation for this module with the perldoc command.
295  
296 perldoc Net::DHCP::Config::Utilities
297  
298  
299 You can also look for information at:
300  
301 =over 4
302  
303 =item * RT: CPAN's request tracker (report bugs here)
304  
305 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-DHCP-Config-Utilities>
306  
307 =item * AnnoCPAN: Annotated CPAN documentation
308  
309 L<http://annocpan.org/dist/Net-DHCP-Config-Utilities>
310  
311 =item * CPAN Ratings
312  
313 L<https://cpanratings.perl.org/d/Net-DHCP-Config-Utilities>
314  
315 =item * Search CPAN
316  
317 L<https://metacpan.org/release/Net-DHCP-Config-Utilities>
318  
319 =back
320  
321  
322 =head1 ACKNOWLEDGEMENTS
323  
324  
325 =head1 LICENSE AND COPYRIGHT
326  
327 This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
328  
329 This is free software, licensed under:
330  
331 The Artistic License 2.0 (GPL Compatible)
332  
333  
334 =cut
335  
336 1; # End of Net::DHCP::Config::Utilities
« Utilities.pm ↑ Summary