summaryrefslogtreecommitdiffstats
path: root/scripts/dhcp/isc2ippool.pl
blob: 6fb961217a4910a0dbc43e8ae8e2d1199b1f6b93 (plain)
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
#!/usr/bin/perl

# isc2ippool	Insert ISC DHCPD lease entries into SQL database (ippool).
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#
#    Copyright (C) 2012 Arran Cudbard-Bell (a.cudbardb@freeradius.org)

use warnings;
use strict;

use DateTime;
use DateTime::Format::Strptime;
use DateTime::Format::DBI;

use Getopt::Long;
use Text::DHCPLeases;
use DBI;

my $lease_file		= '/var/db/dhcpd.leases';
my $sql_type		= 'mysql';
my $sql_host		= 'localhost';
my $sql_user		= 'radius';
my $sql_pass		= 'radpass';
my $sql_database	= 'radius';
my $sql_table		= 'dhcpippool';
my $pool_name		= '';
my $insert_only 	= 0;

my $verbose;
my $help;

sub error {
	print STDERR @_, "\n";	
	exit(64);
}

sub notice {
	if ($verbose) {
		printf(shift . "\n", @_);
	}
}

sub help {
	my @this = split('/', $0);
	print <<HELP
$this[$#this] [options] <pool>

Options:
	-leases <lease file>    - The lease file to parse (defaults to '$lease_file')
	-no-update		- Don't update existing lease entries
	-type			- SQL database type (defaults to '$sql_type')
	-table			- SQL table (defaults to '$sql_table')
	-h | -host		- SQL host to connect to
	-u | -user		- SQL user
	-p | -pass		- SQL password
	-v 			- Verbose
	-help			- This help text
HELP
;
	exit(0);
}

GetOptions (
	'leases=s'	=> \$lease_file,
	'no-update'	=> \$insert_only,
	'type=s'	=> \$sql_type,
	'table=s'	=> \$sql_table,
	'h|host=s'	=> \$sql_host,
	'u|user=s'	=> \$sql_user,
	'p|pass=s'	=> \$sql_pass,
	'v'		=> \$verbose,
	'help'		=> \$help
) or error('Failed parsing options');

#
# Poolname must be provided, and we need at least some arguments...
#
help if !scalar @ARGV or ($pool_name = $ARGV[$#ARGV]) =~ /^-/;

-r $lease_file or
	error("Lease file ($lease_file) doesn\'t exist or isn't readable");

my $leases = Text::DHCPLeases->new(file => $lease_file) or
	error("Failed parsing leases file (or lease file empty)");

my $handle = DBI->connect(
	"DBI:$sql_type:database=$sql_database;host=$sql_host",
	$sql_user, $sql_pass, {RaiseError => 1}
);

my $dt_isc = DateTime::Format::Strptime->new(pattern => '%Y/%m/%d %H:%M:%S');
my $dt_sql = DateTime::Format::DBI->new($handle);

for my $lease ($leases->get_objects) {
	next unless ($lease->binding_state && $lease->binding_state eq 'active');
	
	my($query, @result);
	eval {
		$query = $handle->prepare("
			SELECT expiry_time, framedipaddress FROM $sql_table
			WHERE pool_name = ?
			AND pool_key = ?;"
			, undef);

		$query->bind_param(1, $pool_name);
		$query->bind_param(2, $lease->mac_address);
		
		$query->execute();
		
		@result = $query->fetchrow_array();
	};
	
	error($@) if $@;
	
	my $ends_isc = $dt_isc->parse_datetime($lease->ends =~ m{^(?:[0-9]+) (.+)});
	
	if (!$query->rows) {
		eval {
			$handle->do("
				INSERT INTO $sql_table (
					pool_name, framedipaddress,
					expiry_time, pool_key)
				VALUES (?, ?, ?, ?);"
				, undef
				, $pool_name
				, $lease->ip_address
				, $dt_sql->format_datetime($ends_isc)
				, $lease->mac_address
			);
		};
		
		error($@) if $@;
		
		notice("MAC:'%s' inserted with IP:'%s'.", 
			$lease->mac_address, $lease->ip_address);
		
		next;
	}

	my $ends_sql = $dt_sql->parse_datetime($result[0]);
	
	if ($insert_only && (($result[1] ne $lease->ip_address) ||
		(DateTime->compare($ends_sql, $ends_isc) < 0))) {

		eval {
			$handle->do("
				UPDATE $sql_table
				SET
					pool_key = ?, expiry_time = ?
				WHERE pool_name = ?
				AND framedipaddress = ?;"
				, undef
				, $lease->mac_address
				, $dt_sql->format_datetime($ends_isc)
				, $pool_name
				, $lease->ip_address
			);
		};
		
		error($@) if $@;

		notice("MAC:'%s' updated. ISC-TS: '%s', SQL-TS: '%s', ISC-IP: '%s', SQL-IP: '%s'.",
			$lease->mac_address,
			$dt_sql->format_datetime($ends_isc),
			$dt_sql->format_datetime($ends_sql),
			$lease->ip_address,
			$result[1]);
		
		next;
	}
	
	notice("MAC:'%s' skipped (no update %s). ",
		$lease->mac_address, $insert_only ? 'allowed' : 'needed');
}

exit(0);