-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-dhcp-subnet-declaration
executable file
·216 lines (165 loc) · 4.87 KB
/
generate-dhcp-subnet-declaration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/local/bin/perl -w
#
# $Id$
#
use strict;
use Config::IniFiles;
#use lib 'blib/lib';
use HOSTDB;
use Getopt::Std;
use vars qw ($opt_h $opt_d $opt_o $opt_q $opt_R);
use FileHandle;
getopts ('hdo:qR');
sub usage
{
die (<<EOD);
Syntax: $0 [options] [subnet ...]
subnet is the subnet in slash notation (eg. 192.168.1.0/24)
options :
-d debug
-o dir output to dir
-q quiet
-R remove old files for subnet
EOD
}
usage () if ($opt_h);
my $debug = defined ($opt_d);
my $quiet = defined ($opt_q);
my $remove_old = defined ($opt_R);
my $output_dir = $opt_o || '.';
$output_dir =~ s/\/$//o; # remove trailing slash
die ("$0: Output dir '$output_dir' is not a directory\n") if (! -d $output_dir);
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
my @subnet_list = @ARGV;
push (@subnet_list, '*') if (0 == @subnet_list);
my (@subnets);
my $all_subnets = 0;
if (grep (/^\*$/, @subnet_list)) {
@subnets = $hostdb->findallsubnets ();
$all_subnets = 1;
} else {
foreach my $subnet_name (@subnet_list) {
push (@subnets, $hostdb->findsubnet ($subnet_name));
}
}
usage () if (! @subnets);
my $failed = 0;
my %all_generated_files;
foreach my $subnet (@subnets) {
die ("$0: Empty subnet, oh no.\n") if (! $subnet);
my %generated_files;
$failed = 1 if (! print_dhcp_subnet ($hostdb, $subnet, $output_dir, \%generated_files, $debug, $quiet));
if (! $failed and ! $all_subnets) {
$failed = 1 if (! remove_old_files ($subnet, $output_dir, \%generated_files, $remove_old, $debug, $quiet));
}
last if ($failed);
foreach my $k (keys %generated_files) {
$all_generated_files{$k} = $generated_files{$k};
}
}
if (! $failed and $all_subnets) {
$failed = 1 if (! remove_old_files (undef, $output_dir, \%all_generated_files, $remove_old, $debug, $quiet));
}
exit ($failed);
sub print_dhcp_subnet
{
my $hostdb = shift;
my $subnet = shift;
my $output_dir = shift;
my $generated_files_ref = shift;
my $debug = shift;
my $quiet = shift;
my @subnet_profiles = split (',', $subnet->profilelist ());
my $subnet_name = $subnet->subnet ();
if (! grep (/^default$/, @subnet_profiles)) {
warn ("$0: No 'default' profile for subnet '$subnet_name', skipping subnet.\n");
return undef;
}
my $subnetfn = $subnet_name . "_id" . $subnet->id ();
$subnetfn =~ s#/#-#go;
my $subnetfile = "$output_dir/${subnetfn}.conf";
if (-f $subnetfile) {
print ("$0: Not overwriting existing '$subnetfn'\n") unless ($quiet);
$generated_files_ref->{"${subnetfn}.conf"} = 1;
return 1;
}
my $SUBNET = new FileHandle;
open ($SUBNET, "> $subnetfile") or warn ("$0: Could not open outfile '$subnetfile' for writing: $!\n"), return undef;
print ($SUBNET "# \$Id\$\n#\n# $subnet_name DHCP config generated at " . localtime () ."\n#\n");
# interpolating
my $netaddr = $subnet->netaddr ();
my $netmask = $subnet->netmask ();
my $broadcast = $subnet->broadcast ();
my $routers = $hostdb->ntoa ($subnet->n_netaddr () + 1);
my $odd_or_even = 'odd';
if ($subnet_name =~ /.+[02468]\.\d{1,3}\/\d{1,2}/) {
# n in xxx.xxx.xxn.xxx/xx is an even digit
$odd_or_even = 'even';
}
print ($SUBNET <<EOD);
subnet $netaddr netmask $netmask {
# hard-coded subnet parameters
default-lease-time 36000;
max-lease-time 180000;
# calculated options
option subnet-mask $netmask;
option broadcast-address $broadcast;
option routers $routers;
include "generated/${subnetfn}_options";
include "thisserver/${odd_or_even}_subnet.conf";
EOD
foreach my $profile (@subnet_profiles) {
if ($profile eq 'default') {
print ($SUBNET <<EOD);
include "generated/${subnetfn}-default";
EOD
} else {
print ($SUBNET <<EOD);
group {
include "generated/${subnetfn}-${profile}";
}
EOD
}
}
print ($SUBNET "}\n");
close ($SUBNET);
$generated_files_ref->{"${subnetfn}.conf"} = 1;
return 1;
}
sub remove_old_files
{
my $subnet = shift;
my $output_dir = shift;
my $generated_files_ref = shift;
my $do_remove = shift;
my $debug = shift;
my $quiet = shift;
opendir (DIR, $output_dir) or warn ("$0: Could not opendir () '$output_dir': $!\n"), return 0;
my @files;
if (defined ($subnet)) {
# Do NOT add the subnet id here, the whole point is to find old subnets with the same
# name and remove them since there is now a new subnet in the database
my $file_regexp = $subnet->subnet () . "_id" . $subnet->id ();
$file_regexp =~ s#/#-#go;
@files = grep { /^$file_regexp/ } readdir DIR;
} else {
@files = readdir DIR;
}
close (DIR);
foreach my $fn (@files) {
next if ($fn eq "filelist");
my $file = "$output_dir/$fn";
next unless (-f $file); # skip non-files
if (! defined ($generated_files_ref->{$fn})) {
if ($do_remove) {
print ("Removing '$file'\n");
unlink ($file) or warn ("$0: Could not unlink () '$file': $!\n"), return 0;
} else {
print ("NOT removing stale file '$file' ($fn)\n") unless ($quiet);
}
}
}
return 1;
}