| 1 |
#!/usr/bin/perl -Tw |
| 2 |
|
| 3 |
###################################################################### |
| 4 |
# |
| 5 |
# Copyright (C) 2020 Network RADIUS |
| 6 |
# |
| 7 |
# $Id: 4ef9365eb17a6563a4f4e946607fd43bdbdace57 $ |
| 8 |
# |
| 9 |
###################################################################### |
| 10 |
# |
| 11 |
# Helper script to parse an ISC DHCP config file and extract fixed |
| 12 |
# leases for populating FreeRADIUS ippool tables. |
| 13 |
# |
| 14 |
# This script reads an ISC DCHP config file and extracts any fixed |
| 15 |
# leases. If Net::DNS is available, then any host names are resolved. |
| 16 |
# The resulting list of hardware mac addresses and IP addresses are |
| 17 |
# then formatted as SQL to update a standard FreeRADIUS DHCP ippool |
| 18 |
# table. |
| 19 |
# |
| 20 |
# rlm_iscfixed2ippool -c <dhcpd.conf> -t <table_name> \ |
| 21 |
# (-d <sql_dialect> | -f <raddb_dir> [-i <instance>]) \ |
| 22 |
# -k <mac|id> |
| 23 |
# |
| 24 |
|
| 25 |
use warnings; |
| 26 |
use strict; |
| 27 |
|
| 28 |
my $dns_available = 0; |
| 29 |
my $resolver; |
| 30 |
eval { |
| 31 |
require Net::DNS; |
| 32 |
$dns_available = 1; |
| 33 |
$resolver = Net::DNS::Resolver->new; |
| 34 |
}; |
| 35 |
|
| 36 |
# |
| 37 |
# Option defaults |
| 38 |
# |
| 39 |
my $opts = { |
| 40 |
dhcpdconf => '/etc/dhcp/dhcpd.conf', |
| 41 |
key => 'mac' |
| 42 |
}; |
| 43 |
|
| 44 |
# |
| 45 |
# Parse the command line arguments |
| 46 |
# |
| 47 |
my $opt = ''; |
| 48 |
for (my $i = 0; $i <= $#ARGV; $i++) { |
| 49 |
if ($ARGV[$i] =~ m/^-(.)$/) { |
| 50 |
if ($1 eq 'c') { |
| 51 |
$opt = 'dhcpdconf'; |
| 52 |
} elsif ($1 eq 't') { |
| 53 |
$opt = 'table_name'; |
| 54 |
} elsif ($1 eq 'd') { |
| 55 |
$opt = 'dialect'; |
| 56 |
} elsif ($1 eq 'f') { |
| 57 |
$opt = 'raddb_dir'; |
| 58 |
} elsif ($1 eq 'i') { |
| 59 |
$opt = 'instance'; |
| 60 |
} elsif ($1 eq 'k') { |
| 61 |
$opt = 'key' |
| 62 |
} else { |
| 63 |
&usage(); |
| 64 |
exit 1; |
| 65 |
} |
| 66 |
} else { |
| 67 |
if ($opt eq '') { |
| 68 |
&usage(); |
| 69 |
exit 1; |
| 70 |
} else { |
| 71 |
$opts->{$opt} = $ARGV[$i] |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if (($opts->{key} ne 'mac') && ($opts->{key} ne 'id')) { |
| 77 |
&usage(); |
| 78 |
exit(1); |
| 79 |
} |
| 80 |
|
| 81 |
# |
| 82 |
# If a raddb dir is set then we parse the mods-enabled config |
| 83 |
# |
| 84 |
|
| 85 |
if ($opts->{raddb_dir}) { |
| 86 |
my $found = 0; |
| 87 |
if (-d $opts->{raddb_dir}.'/mods-enabled') { |
| 88 |
opendir(my $dh, $opts->{raddb_dir}.'/mods-enabled') || die 'ERROR: Could not open directory '.$opts->{raddb_dir}.'/mods-enabled'; |
| 89 |
my @dir = grep { -f "$opts->{raddb_dir}/mods-enabled/$_" } readdir($dh); |
| 90 |
closedir($dh); |
| 91 |
my $instance = $opts->{instance}; |
| 92 |
foreach my $file (@dir) { |
| 93 |
open (my $fh, $opts->{raddb_dir}.'/mods-enabled/'.$file); |
| 94 |
my $level = 0; |
| 95 |
my $section = ''; |
| 96 |
my $subsection = ''; |
| 97 |
while (<$fh>) { |
| 98 |
if ($found) { |
| 99 |
$_ =~ s/#.*//; # Remove comments |
| 100 |
if ($_ =~ m/\s*([a-z_]+)\s*=\s*(.*)/) { |
| 101 |
my $param = $1; |
| 102 |
my $value = $2; |
| 103 |
$value =~ s/^"//; |
| 104 |
$value =~ s/"\s*$//; |
| 105 |
if ($level == 1) { |
| 106 |
$opts->{$param} = $value; |
| 107 |
} elsif ($level == 2) { |
| 108 |
$opts->{$section}->{$param} = $value; |
| 109 |
} elsif ($level == 3) { |
| 110 |
$opts->{$section}->{$subsection}->{$param} = $value; |
| 111 |
} |
| 112 |
} |
| 113 |
if ($_ =~ m/([a-z_]*)\s+\{/) { # Find nested sectinos |
| 114 |
$level++ ; |
| 115 |
if ($level == 2) { |
| 116 |
$section = $1; |
| 117 |
} elsif ($level == 3) { |
| 118 |
$subsection = $1; |
| 119 |
} |
| 120 |
} |
| 121 |
$level-- if ($_ =~ m/\s+\}/); # Close of nesting |
| 122 |
last if ($level == 0); # We've got to the end of the instance |
| 123 |
} |
| 124 |
if ($_ =~ m/\b$instance\s+\{/) { |
| 125 |
# We've found the specified SQL instance |
| 126 |
$found = 1; |
| 127 |
$level = 1; |
| 128 |
} |
| 129 |
} |
| 130 |
close ($fh); |
| 131 |
if ($found) { |
| 132 |
last; |
| 133 |
} |
| 134 |
} |
| 135 |
} else { |
| 136 |
die 'ERROR: Specified FreeRADIUS config directory does not contain mods-enabled'; |
| 137 |
} |
| 138 |
if ($found == 0) { |
| 139 |
die 'ERROR: SQL instance not found in FreeRADIUS config'; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
# |
| 144 |
# The SQL dialect and table name must be set |
| 145 |
# |
| 146 |
if ((!($opts->{dialect})) || (!($opts->{table_name}))) { |
| 147 |
&usage(); |
| 148 |
exit 1; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
open (my $fh, '<', $opts->{dhcpdconf}) or die "ERROR: Cannot open ISC DHCP config for reading: $opts->{dhcpdconf}"; |
| 153 |
|
| 154 |
my $inhost = 0; |
| 155 |
my @hosts; |
| 156 |
my $host = {key => ''}; |
| 157 |
while (my $line = <$fh>) { |
| 158 |
$line = lc($line); |
| 159 |
if ($inhost == 0) { |
| 160 |
$inhost = 1 if ($line =~ m/host\s+\S+\s+{/); # We've found the beginning of a host record |
| 161 |
} |
| 162 |
if ($inhost) { |
| 163 |
if (($opts->{key} eq 'mac') && ($line =~ m/hardware\s+ethernet\s+(([0-9a-f]{2}([:;]|\s)){6})/)) { |
| 164 |
$host->{key} = $1; |
| 165 |
$host->{key} =~ s/;$//; |
| 166 |
} |
| 167 |
if (($opts->{key} eq 'id') && ($line =~ m/dhcp-client-identifier\s+(.*?)\s*;/)) { |
| 168 |
$host->{key} = $1; |
| 169 |
} |
| 170 |
if ($line =~ m/fixed-address\s+(.+);/) { |
| 171 |
my @addresses = split(',', $1); |
| 172 |
foreach my $address (@addresses) { |
| 173 |
$address =~ s/^\s+//; |
| 174 |
$address =~ s/\s+$//; |
| 175 |
if ($address =~ m/(([0-9]{1,3}(\.|$)){4})/) { |
| 176 |
push (@{$host->{ips}}, $1); |
| 177 |
} elsif ($dns_available) { |
| 178 |
my $reply = $resolver->search($1, 'A'); |
| 179 |
if ($reply) { |
| 180 |
foreach my $rr ($reply->answer) { |
| 181 |
push (@{$host->{ips}}, $rr->address) if ($rr->can('address')) |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
if ($line =~ m/}/) { # End of the host record - store the results and clear up |
| 188 |
push (@hosts, $host) if (($host->{key}) && ($#{$host->{ips}} >= 0)); |
| 189 |
$host = {key => ''}; |
| 190 |
$inhost = 0; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
close($fh); |
| 196 |
|
| 197 |
my ($template, $queries) = &load_templates($opts->{table_name}); |
| 198 |
|
| 199 |
unless (defined $template->{$opts->{dialect}}) { |
| 200 |
print STDERR "Unknown dialect. Pick one of: "; |
| 201 |
print STDERR "$_ " foreach sort keys %{$template}; |
| 202 |
print STDERR "\n"; |
| 203 |
exit 1; |
| 204 |
} |
| 205 |
|
| 206 |
if ($opts->{radius_db}) { |
| 207 |
&call_database($opts, $queries, @hosts); |
| 208 |
} else { |
| 209 |
my $tt_available = 0; |
| 210 |
eval { |
| 211 |
require Template; |
| 212 |
$tt_available = 1; |
| 213 |
}; |
| 214 |
if ($tt_available) { |
| 215 |
my $tt=Template->new(); |
| 216 |
$tt->process(\$template->{$opts->{dialect}}, {tablename => $opts->{table_name}, hosts => \@hosts}) || die $tt->error(); |
| 217 |
} else { |
| 218 |
die "ERROR: Template Toolkit is not available. Install the Template Perl module."; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
exit(0); |
| 223 |
|
| 224 |
sub usage { |
| 225 |
print STDERR <<'EOF' |
| 226 |
Usage: |
| 227 |
rlm_iscfixed2ippool -c <dhcpd.conf> -t <table_name> (-d <sql_dialect> | -f <raddb_dir> [ -i <instance> ]) [-k <mac|id> ] |
| 228 |
|
| 229 |
EOF |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
sub call_database { |
| 234 |
|
| 235 |
my $opts = shift; |
| 236 |
my $queries = shift; |
| 237 |
my @entries = @_; |
| 238 |
|
| 239 |
my $dbi_avail = 0; |
| 240 |
eval { |
| 241 |
require DBI; |
| 242 |
$dbi_avail = 1; |
| 243 |
}; |
| 244 |
unless($dbi_avail) { |
| 245 |
die "ERROR: DBI is not available. Install the DBI Perl module."; |
| 246 |
} |
| 247 |
|
| 248 |
my $dsn; |
| 249 |
if ($opts->{dialect} eq 'mysql') { |
| 250 |
$dsn = "DBI:mysql:database=$opts->{radius_db};host=$opts->{server}"; |
| 251 |
if (defined($opts->{mysql}->{tls})) { |
| 252 |
$dsn .= ';mysql_ssl=1'; |
| 253 |
$dsn .= ';mysql_ssl_ca_file='.$opts->{mysql}->{tls}->{ca_file} if ($opts->{mysql}->{tls}->{ca_file}); |
| 254 |
$dsn .= ';mysql_ssl_ca_path='.$opts->{mysql}->{tls}->{ca_path} if ($opts->{mysql}->{tls}->{ca_path}); |
| 255 |
$dsn .= ';mysql_ssl_client_key='.$opts->{mysql}->{tls}->{private_key_file} if ($opts->{mysql}->{tls}->{private_key_file}); |
| 256 |
$dsn .= ';mysql_ssl_client_cert='.$opts->{mysql}->{tls}->{certificate_file} if ($opts->{mysql}->{tls}->{certificate_file}); |
| 257 |
$dsn .= ';mysql_ssl_cipher='.$opts->{mysql}->{tls}->{cipher} if ($opts->{mysql}->{tls}->{cipher}); |
| 258 |
} |
| 259 |
} elsif ($opts->{dialect} eq 'postgresql') { |
| 260 |
# Parse FreeRADIUS alternative connection string |
| 261 |
if ($opts->{radius_db} =~ m/host=(.+?)\b/) { |
| 262 |
$opts->{server} = $1; |
| 263 |
} |
| 264 |
if ($opts->{radius_db} =~ m/user=(.+?)\b/) { |
| 265 |
$opts->{login} = $1; |
| 266 |
} |
| 267 |
if ($opts->{radius_db} =~ m/password=(.+?)\b/) { |
| 268 |
$opts->{password} = $1; |
| 269 |
} |
| 270 |
if ($opts->{radius_db} =~ m/sslmode=(.+?)\b/) { |
| 271 |
$opts->{sslmode} = $1; |
| 272 |
} |
| 273 |
if ($opts->{radius_db} =~ m/dbname=(.+?)\b/) { |
| 274 |
$opts->{radius_db} = $1; |
| 275 |
} |
| 276 |
$dsn = "DBI:Pg:dbname=$opts->{radius_db};host=$opts->{server}"; |
| 277 |
# |
| 278 |
# DBD doesn't have all the options used by FreeRADIUS - just enable ssl if |
| 279 |
# FreeRADIUS has SSL options enabled |
| 280 |
# |
| 281 |
$dsn .= ';sslmode=prefer' if ($opts->{sslmode}); |
| 282 |
} elsif ($opts->{dialect} eq 'sqlite') { |
| 283 |
$dsn = "DBI:SQLite:dbname=$opts->{sqlite}->{filename}"; |
| 284 |
} elsif ($opts->{dialect} eq 'mssql') { |
| 285 |
if ($opts->{driver} eq 'rlm_sql_unixodbc') { |
| 286 |
$dsn = "DBI:ODBC:DSN=$opts->{server}"; |
| 287 |
} else { |
| 288 |
$dsn = "DBI:Sybase:server=$opts->{server};database=$opts->{radius_db}"; |
| 289 |
} |
| 290 |
} elsif ($opts->{dialect} eq 'oracle') { |
| 291 |
# Extract data from Oracle connection string as used by FreeRADIUS |
| 292 |
if ($opts->{radius_db} =~ m/HOST=(.+?)\)/) { |
| 293 |
$opts->{server} = $1; |
| 294 |
} |
| 295 |
if ($opts->{radius_db} =~ m/PORT=(.+?)\)/) { |
| 296 |
$opts->{port} =$1; |
| 297 |
} |
| 298 |
if ($opts->{radius_db} =~ m/SID=(.+?)\)/) { |
| 299 |
$opts->{sid} = $1; |
| 300 |
} |
| 301 |
$dsn = "DBI:Oracle:host=$opts->{server};sid=$opts->{sid}"; |
| 302 |
} else { |
| 303 |
$dsn = "DBI:$opts->{dialect}:database=$opts->{radius_db};host=$opts->{server}"; |
| 304 |
} |
| 305 |
$dsn .= ";port=$opts->{port}" if ($opts->{port}) && ($opts->{driver} ne 'rlm_sql_unixodbc'); |
| 306 |
|
| 307 |
# Read the results by running our query against the database |
| 308 |
my $dbh = DBI->connect($dsn, $opts->{login}, $opts->{password}) || die "Unable to connect to database"; |
| 309 |
|
| 310 |
$dbh->do($queries->{$opts->{dialect}}->{pre}) if ($queries->{$opts->{dialect}}->{pre}); |
| 311 |
|
| 312 |
my $sth = $dbh->prepare($queries->{$opts->{dialect}}->{update}); |
| 313 |
foreach my $h (@hosts) { |
| 314 |
foreach my $i (@{$h->{ips}}) { |
| 315 |
$sth->execute($h->{key}, $i); |
| 316 |
} |
| 317 |
} |
| 318 |
$sth->finish(); |
| 319 |
|
| 320 |
$dbh->do($queries->{$opts->{dialect}}->{post}) if ($queries->{$opts->{dialect}}->{post}); |
| 321 |
|
| 322 |
$dbh->disconnect(); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
# |
| 327 |
# SQL dialect templates |
| 328 |
# |
| 329 |
|
| 330 |
sub load_templates { |
| 331 |
|
| 332 |
my $tablename = shift; |
| 333 |
|
| 334 |
my $template; |
| 335 |
my $queries; |
| 336 |
# |
| 337 |
# MySQL / MariaDB |
| 338 |
# |
| 339 |
$queries->{'mysql'}->{pre} = 'START TRANSACTION'; |
| 340 |
$queries->{'mysql'}->{update} = 'UPDATE'.$tablename.' SET pool_key = ?, `status` = "static" WHERE framedipaddress = ?'; |
| 341 |
$queries->{'mysql'}->{post} = 'COMMIT'; |
| 342 |
|
| 343 |
$template->{'mysql'} = $queries->{'mysql'}->{pre}.";\n"; |
| 344 |
$template->{'mysql'} .= <<'END_mysql'; |
| 345 |
[%- FOREACH h IN hosts %] |
| 346 |
[%- FOREACH i IN h.ips %] |
| 347 |
UPDATE [% tablename %] SET pool_key = '[% h.key %]', `status` = 'static' WHERE framedipaddress = '[% i %]'; |
| 348 |
[%- END %] |
| 349 |
[%- END %] |
| 350 |
END_mysql |
| 351 |
$template->{'mysql'} .= $queries->{'mysql'}->{post}.";\n"; |
| 352 |
|
| 353 |
# |
| 354 |
# PostgreSQL |
| 355 |
# |
| 356 |
$queries->{'postgresql'}->{pre} = 'START TRANSACTION'; |
| 357 |
$queries->{'postgresql'}->{update} = 'UPDATE'.$tablename.' SET pool_key = ?, status = "static" WHERE framedipaddress = ?'; |
| 358 |
$queries->{'postgresql'}->{post} = 'COMMIT'; |
| 359 |
|
| 360 |
$template->{'postgresql'} = $queries->{'postgresql'}->{pre}.";\n"; |
| 361 |
$template->{'postgresql'} .= <<'END_postgresql'; |
| 362 |
[%- FOREACH h IN hosts %] |
| 363 |
[%- FOREACH i IN h.ips %] |
| 364 |
UPDATE [% tablename %] SET pool_key = '[% h.key %]', status = 'static' WHERE framedipaddress = '[% i %]'; |
| 365 |
[%- END %] |
| 366 |
[%- END %] |
| 367 |
END_postgresql |
| 368 |
$template->{'postgresql'} .= $queries->{'postgresql'}->{post}.";\n"; |
| 369 |
# |
| 370 |
# Oracle |
| 371 |
# |
| 372 |
$queries->{'oracle'}->{pre} = ''; |
| 373 |
$queries->{'oracle'}->{update} = 'UPDATE '.$tablename.' SET pool_key = ?, status_id = (SELECT status_id FROM dhcpstatus WHERE status = \'static\') WHERE FramedIPAddress = ?'; |
| 374 |
$queries->{'oracle'}->{post} = 'COMMIT'; |
| 375 |
|
| 376 |
$template->{'oracle'} = <<'END_oracle'; |
| 377 |
[%- FOREACH h IN hosts %] |
| 378 |
[%- FOREACH i IN h.ips %] |
| 379 |
UPDATE [% tablename %] SET pool_key = '[% h.key %]', status_id = (SELECT status_id FROM dhcpstatus WHERE status = 'static') WHERE framedipaddress = '[% i %]'; |
| 380 |
[%- END %] |
| 381 |
[%- END %] |
| 382 |
END_oracle |
| 383 |
$template->{'oracle'} .= $queries->{'oracle'}->{post}.";\n"; |
| 384 |
|
| 385 |
# |
| 386 |
# SQLite |
| 387 |
# |
| 388 |
$queries->{'sqlite'}->{pre} = 'BEGIN TRANSACTION'; |
| 389 |
$queries->{'sqlite'}->{update} = 'UPDATE '.$tablename.' SET pool_key = ?, status_id = (SELECT status_id FROM dhcpstatus WHERE status = \'static\') WHERE framedipaddress = ?'; |
| 390 |
$queries->{'sqlite'}->{post} = 'COMMIT'; |
| 391 |
|
| 392 |
$template->{'sqlite'} = $queries->{'sqlite'}->{pre}.";\n"; |
| 393 |
$template->{'sqlite'} .= <<'END_sqlite'; |
| 394 |
[%- FOREACH h IN hosts %] |
| 395 |
[%- FOREACH i IN h.ips %] |
| 396 |
UPDATE [% tablename %] SET pool_key = '[% h.key %]', status_id = (SELECT status_id FROM dhcpstatus WHERE status = 'static') WHERE framedipaddress = '[% i %]'; |
| 397 |
[%- END %] |
| 398 |
[%- END %] |
| 399 |
END_sqlite |
| 400 |
$template->{'sqlite'} .= $queries->{'sqlite'}->{post}.";\n"; |
| 401 |
|
| 402 |
# |
| 403 |
# MS SQL |
| 404 |
# |
| 405 |
$queries->{'mssql'}->{pre} = 'BEGIN TRAN'; |
| 406 |
$queries->{'mssql'}->{update} = 'UPDATE '.$tablename.' SET pool_key = ?, status_id = (SELECT status_id FROM dhcpstatus WHERE status = \'static\') WHERE framedipaddress = ?'; |
| 407 |
$queries->{'mssql'}->{post} = 'COMMIT TRAN'; |
| 408 |
|
| 409 |
$template->{'mssql'} = $queries->{'mssql'}->{pre}.";\n"; |
| 410 |
$template->{'mssql'} .= <<'END_mssql'; |
| 411 |
[%- FOREACH h IN hosts %] |
| 412 |
[%- FOREACH i IN h.ips %] |
| 413 |
UPDATE [% tablename %] SET pool_key = '[% h.key %]', status_id = (SELECT status_id FROM dhcpstatus WHERE status = 'static') WHERE framedipaddress = '[% i %]'; |
| 414 |
[%- END %] |
| 415 |
[%- END %] |
| 416 |
END_mssql |
| 417 |
$template->{'mssql'} .= $queries->{'mssql'}->{post}.";\n"; |
| 418 |
|
| 419 |
return ($template, $queries); |
| 420 |
|
| 421 |
} |
| 422 |
|