line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::DHCP::Config::Utilities::Generator::ISC_DHCPD; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
710
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
16
|
|
5
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
6
|
1
|
|
|
1
|
|
463
|
use Template; |
|
1
|
|
|
|
|
16944
|
|
|
1
|
|
|
|
|
29
|
|
7
|
1
|
|
|
1
|
|
7
|
use Net::DHCP::Config::Utilities::Options; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
18
|
|
8
|
1
|
|
|
1
|
|
406
|
use String::ShellQuote; |
|
1
|
|
|
|
|
725
|
|
|
1
|
|
|
|
|
801
|
|
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 |
87
|
|
|
|
|
|
|
as the \%vars value for when calling L->process. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub new { |
92
|
1
|
|
|
1
|
1
|
42
|
my %args; |
93
|
1
|
50
|
|
|
|
4
|
if ( defined( $_[1] ) ){ |
94
|
1
|
|
|
|
|
2
|
%args=%{$_[1]}; |
|
1
|
|
|
|
|
5
|
|
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
|
1
|
50
|
|
|
|
46
|
if ( !defined( $args{header} ) ){ |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
100
|
0
|
|
|
|
|
0
|
die( 'No header file specified' ); |
101
|
|
|
|
|
|
|
}elsif( !defined( $args{footer} ) ){ |
102
|
0
|
|
|
|
|
0
|
die( 'No footer file specified' ); |
103
|
|
|
|
|
|
|
}elsif( !defined( $args{output} ) ){ |
104
|
0
|
|
|
|
|
0
|
die( 'No output file specified' ); |
105
|
|
|
|
|
|
|
}elsif( !defined( $args{vars} ) ){ |
106
|
0
|
|
|
|
|
0
|
die( 'No vars hash defined' ); |
107
|
|
|
|
|
|
|
}elsif( ! -f $args{header} ){ |
108
|
0
|
|
|
|
|
0
|
die( '"'.$args{header}.'" does not exist or is not a file'); |
109
|
|
|
|
|
|
|
}elsif( ! -f $args{footer} ){ |
110
|
0
|
|
|
|
|
0
|
die( '"'.$args{footer}.'" does not exist or is not a file'); |
111
|
|
|
|
|
|
|
}elsif( ref( $args{vars} ) ne 'HASH' ){ |
112
|
0
|
|
|
|
|
0
|
die( '$args{vars} is not a hash' ); |
113
|
|
|
|
|
|
|
}elsif( $args{footer} eq $args{header} ){ |
114
|
0
|
|
|
|
|
0
|
die( '$args{footer} and $args{header} are both the same, "'.$args{footer}.'",' ); |
115
|
|
|
|
|
|
|
}elsif( $args{footer} eq $args{output} ){ |
116
|
0
|
|
|
|
|
0
|
die( '$args{footer} and $args{output} are both the same, "'.$args{footer}.'",' ); |
117
|
|
|
|
|
|
|
}elsif( $args{header} eq $args{output} ){ |
118
|
0
|
|
|
|
|
0
|
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
|
1
|
|
|
|
|
8
|
options=>Net::DHCP::Config::Utilities::Options->new, |
127
|
|
|
|
|
|
|
}; |
128
|
1
|
|
|
|
|
2
|
bless $self; |
129
|
|
|
|
|
|
|
|
130
|
1
|
|
|
|
|
3
|
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 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
|
1
|
|
|
1
|
1
|
598
|
my $self=$_[0]; |
174
|
1
|
|
|
|
|
3
|
my $object=$_[1]; |
175
|
1
|
|
|
|
|
2
|
my $no_write=$_[2]; |
176
|
|
|
|
|
|
|
|
177
|
1
|
50
|
|
|
|
5
|
if ( ref( $object ) ne 'Net::DHCP::Config::Utilities' ){ |
178
|
0
|
|
|
|
|
0
|
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
|
1
|
|
|
|
|
53
|
my $template=Template->new({ |
183
|
|
|
|
|
|
|
EVAL_PERL=>1, |
184
|
|
|
|
|
|
|
INTERPOLATE=>1, |
185
|
|
|
|
|
|
|
POST_CHOMP=>1, |
186
|
|
|
|
|
|
|
}); |
187
|
1
|
|
|
|
|
20726
|
my $header; |
188
|
|
|
|
|
|
|
$template->process( $self->{header}, $self->{vars}, \$header ) |
189
|
1
|
50
|
|
|
|
9
|
|| die( 'Failed to process the header, "'.$self->{header}.'"... '.$template->error ); |
190
|
1
|
|
|
|
|
33346
|
my $footer; |
191
|
|
|
|
|
|
|
$template->process( $self->{footer}, $self->{vars}, \$footer ) |
192
|
1
|
50
|
|
|
|
6
|
|| die( 'Failed to process the footer, "'.$self->{footer}.'"... '.$template->error ); |
193
|
|
|
|
|
|
|
|
194
|
1
|
|
|
|
|
979
|
my $middle=''; |
195
|
|
|
|
|
|
|
|
196
|
1
|
|
|
|
|
9
|
my @subnets=sort( $object->subnet_list ); |
197
|
1
|
|
|
|
|
8
|
foreach my $base ( @subnets ){ |
198
|
2
|
|
|
|
|
17
|
my $subnet=$object->subnet_get( $base ); |
199
|
|
|
|
|
|
|
|
200
|
2
|
|
|
|
|
9
|
my $desc=$subnet->desc_get; |
201
|
2
|
100
|
|
|
|
5
|
if ( $desc ne '' ){ |
202
|
1
|
|
|
|
|
3
|
$desc='# '.$desc."\n"; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
2
|
|
|
|
|
10
|
$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
|
2
|
|
|
|
|
7
|
my @ranges=sort( $subnet->range_get ); |
210
|
2
|
|
|
|
|
4
|
foreach my $range ( @ranges ){ |
211
|
1
|
|
|
|
|
3
|
$middle=$middle.' range '.$range.";\n"; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
2
|
|
|
|
|
11
|
my @options=sort( $subnet->options_list ); |
215
|
2
|
|
|
|
|
5
|
foreach my $option ( @options ){ |
216
|
24
|
|
|
|
|
40
|
my $value=$subnet->option_get( $option ); |
217
|
24
|
|
|
|
|
40
|
my $long=$self->{options}->get_long( $option ); |
218
|
24
|
100
|
|
|
|
44
|
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
|
4
|
50
|
33
|
|
|
29
|
if ( |
|
|
50
|
33
|
|
|
|
|
222
|
|
|
|
|
|
|
($option eq 'next-server') || |
223
|
|
|
|
|
|
|
($option eq 'tftp-server') |
224
|
|
|
|
|
|
|
){ |
225
|
0
|
|
|
|
|
0
|
$middle=$middle.' next-server'.$value.";\n"; |
226
|
|
|
|
|
|
|
}elsif( |
227
|
|
|
|
|
|
|
($option eq 'filename') || |
228
|
|
|
|
|
|
|
($option eq 'bootfile') |
229
|
|
|
|
|
|
|
){ |
230
|
0
|
|
|
|
|
0
|
$middle=$middle.' filename'.$value.";\n"; |
231
|
|
|
|
|
|
|
}else{ |
232
|
4
|
|
|
|
|
15
|
$middle=$middle.' option '.$long.' '.$value.";\n"; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
|
237
|
2
|
|
|
|
|
7
|
$middle=$middle."}\n\n"; |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
|
240
|
1
|
50
|
|
|
|
3
|
if ( ! $no_write ){ |
241
|
1
|
|
|
|
|
2
|
my $fh; |
242
|
1
|
50
|
|
|
|
86
|
open( $fh, '>', $self->{output} ) or die( 'Can not open "'.$self->{output}.'" for writing output to,,, C errno='.$! ); |
243
|
1
|
|
|
|
|
9
|
print $fh $header.$middle.$footer; |
244
|
1
|
|
|
|
|
39
|
close( $fh ); |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
# 2> /dev/null used to prevent this from being noisy |
247
|
1
|
|
|
|
|
4822
|
my $dhcpd_bin=`/bin/sh -c 'which dhcpd 2> /dev/null'`; |
248
|
1
|
50
|
|
|
|
97
|
if ( $? == 0 ){ |
249
|
0
|
|
|
|
|
0
|
my $quoted=shell_quote( $self->{output} ); |
250
|
0
|
|
|
|
|
0
|
my $check=`/bin/sh -c 'dhcpd -t -cf $quoted 2> /dev/null'`; |
251
|
0
|
0
|
|
|
|
0
|
if ( $? != 0 ){ |
252
|
0
|
|
|
|
|
0
|
die('Failed to lint the output file,"'.$self->{output}.'",'); |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
|
257
|
1
|
|
|
|
|
66
|
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<< >> |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=head1 BUGS |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
286
|
|
|
|
|
|
|
the web interface at L. 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 |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
L |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=item * CPAN Ratings |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
L |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=item * Search CPAN |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
L |
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 |