18 |
use IO::Socket::INET; |
use IO::Socket::INET; |
19 |
use File::Slurp; |
use File::Slurp; |
20 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
|
use Net::Ping; |
|
21 |
|
|
22 |
use lib '..'; |
use lib '..'; |
23 |
use Net::DHCP::Packet; |
use Net::DHCP::Packet; |
24 |
use Net::DHCP::Constants 0.67; |
use Net::DHCP::Constants 0.67; |
25 |
|
|
26 |
|
use CouchDB; |
27 |
|
use format; |
28 |
|
|
29 |
use server; |
use server; |
30 |
my $debug = server::debug; |
my $debug = server::debug; |
31 |
|
|
37 |
|
|
38 |
warn "server ip $server::ip range: $server::ip_from - $server::ip_to\n"; |
warn "server ip $server::ip range: $server::ip_from - $server::ip_to\n"; |
39 |
|
|
40 |
my $addr = $server::ip_from; |
use client; |
41 |
|
|
42 |
|
sub client_mac_ip { |
43 |
|
my ( $mac, $request_ip ) = @_; |
44 |
|
|
45 |
sub client_ip { |
if ( ! $mac ) { |
46 |
my ( $mac ) = @_; |
warn "W: no mac in requiest\n"; |
47 |
|
return; |
48 |
|
} |
49 |
|
|
50 |
my $conf = $server::conf; |
my $conf = $server::conf; |
51 |
mkdir $conf unless -e $conf; |
mkdir $conf unless -e $conf; |
52 |
|
|
53 |
if ( -e "$conf/mac/$mac" ) { |
my $ip; |
54 |
my $ip = read_file "$conf/mac/$mac"; |
|
55 |
|
if ( $ip = client::ip_from_mac( $mac ) ) { |
56 |
print "RENEW $mac $ip\n"; |
print "RENEW $mac $ip\n"; |
57 |
|
client::save_ip_mac( $ip, $mac ); |
58 |
return $ip; |
return $ip; |
59 |
|
} elsif ( ip::in_dhcp_range( $request_ip ) || $request_ip eq '0.0.0.0' ) { |
60 |
|
$ip = client::next_ip( $mac ); |
61 |
|
print "NEW $mac $ip\n"; |
62 |
|
} else { |
63 |
|
$ip = $request_ip; |
64 |
|
client::save_ip_mac( $ip, $mac ); |
65 |
|
warn "W: $ip out of server range $server::ip/$server::netmask\n"; |
66 |
} |
} |
67 |
|
|
|
my $p = Net::Ping->new; |
|
|
|
|
|
my $prefix = $server::ip; |
|
|
$prefix =~ s{\.\d+$}{.}; |
|
|
my $ip = $prefix . $addr; |
|
|
|
|
|
while ( -e "$conf/ip/$ip" || $p->ping( $ip ) ) { |
|
|
$ip = $prefix . $addr++; |
|
|
die "all addresses allocated!" if $addr == $server::ip_to; |
|
|
} |
|
|
|
|
|
write_file "$conf/mac/$mac", $ip; |
|
|
|
|
|
my $ip_path = "$conf/ip/$ip"; |
|
|
mkdir $ip_path unless -e $ip_path; |
|
|
|
|
|
if ( -l "$ip_path/mac" && readlink "$ip_path/mac" ne "$conf/mac/$mac" ) { |
|
|
warn "$mac IP changed from ", readlink "$ip_path/mac", " to $ip"; |
|
|
unlink "$ip_path/mac"; |
|
|
}; |
|
|
symlink "$conf/mac/$mac", "$ip_path/mac"; |
|
|
|
|
|
print "$mac NEW $ip\n"; |
|
|
|
|
68 |
return $ip; |
return $ip; |
69 |
} |
} |
70 |
|
|
79 |
sub process_packet { |
sub process_packet { |
80 |
my $sock = shift; |
my $sock = shift; |
81 |
|
|
|
server->refresh; |
|
|
|
|
82 |
my $buf; |
my $buf; |
83 |
$sock->recv($buf, 1024); |
$sock->recv($buf, 1024); |
84 |
my $size = 'empty'; |
my $size = 'empty'; |
88 |
return unless $buf; |
return unless $buf; |
89 |
|
|
90 |
my $dhcp = Net::DHCP::Packet->new($buf); |
my $dhcp = Net::DHCP::Packet->new($buf); |
|
$dhcp->comment( $transaction++ ); |
|
91 |
|
|
92 |
warn "recv: ", $dhcp->toString if $debug; |
warn "recv: ", $dhcp->toString if $debug; |
93 |
|
|
94 |
my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); |
$dhcp->comment( $transaction++ ); |
95 |
my $ip = client_ip($mac); |
|
96 |
|
my $mac = format::mac( substr($dhcp->chaddr(),0,$dhcp->hlen()*2) ); |
97 |
|
my $ip = client_mac_ip($mac, $dhcp->ciaddr); |
98 |
|
|
99 |
|
my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME); |
100 |
|
print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n" if $hostname; |
101 |
|
|
102 |
|
my $audit = { mac => $mac, ip => $ip, hostname => $hostname, |
103 |
|
options => { |
104 |
|
map { |
105 |
|
( $_ => $dhcp->getOptionValue( $_ ) ) |
106 |
|
} @{ $dhcp->{options_order} } |
107 |
|
}, |
108 |
|
}; |
109 |
|
|
110 |
=for later |
=for later |
111 |
|
|
138 |
my $options = { |
my $options = { |
139 |
DHO_SUBNET_MASK() => $server::netmask, |
DHO_SUBNET_MASK() => $server::netmask, |
140 |
DHO_ROUTERS() => $server::ip, |
DHO_ROUTERS() => $server::ip, |
141 |
DHO_DOMAIN_NAME() => $server::domain_name, |
DHO_DOMAIN_NAME() => $server::domain, |
142 |
DHO_NAME_SERVERS() => $server::ip, |
DHO_NAME_SERVERS() => $server::ip, |
143 |
DHO_DOMAIN_NAME_SERVERS() => $server::ip, |
DHO_DOMAIN_NAME_SERVERS() => $server::ip, |
144 |
DHO_HOST_NAME() => client::conf( $ip, 'hostname' ), |
DHO_HOST_NAME() => client::conf( $ip, 'hostname' ), |
158 |
} |
} |
159 |
} |
} |
160 |
|
|
161 |
warn "W: options requested but missing: ",dump( @missing ); |
warn "W: options requested but missing: ",dump( @missing ),$/; |
162 |
|
$audit->{requested} = [ @requested ]; |
163 |
|
$audit->{missing} = [ @missing ]; |
164 |
|
|
165 |
foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) { |
foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) { |
166 |
my $DH0 = eval 'DHO_PXELINUX_' . uc $opt; |
my $DH0 = eval 'DHO_PXELINUX_' . uc $opt; |
174 |
|
|
175 |
my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE()); |
my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE()); |
176 |
|
|
177 |
|
my @type; |
178 |
|
|
179 |
if ($messagetype eq DHCPDISCOVER()) { |
if ($messagetype eq DHCPDISCOVER()) { |
|
log::mac $mac, "DHCP DISCOVER"; |
|
180 |
$packet->{Comment} = $dhcp->comment(); |
$packet->{Comment} = $dhcp->comment(); |
181 |
$packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER(); |
$packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER(); |
182 |
|
@type = qw( discover offer ); |
183 |
} elsif ($messagetype eq DHCPREQUEST()) { |
} elsif ($messagetype eq DHCPREQUEST()) { |
184 |
my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS()); |
@type = qw( request ); |
185 |
log::mac $mac, "DHCP REQUEST $requested_ip $ip $file"; |
my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS()) || $dhcp->ciaddr(); |
186 |
if ( $ip eq $requested_ip ) { |
if ( $ip eq $requested_ip ) { |
187 |
$packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK(); |
$packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK(); |
188 |
$packet->{DHO_DHCP_LEASE_TIME()} = 5 * 60; # 5 min |
$packet->{DHO_DHCP_LEASE_TIME()} = 5 * 60; # 5 min |
189 |
# $packet->{DHO_ROOT_PATH()} = '/exports/foobar'; |
# $packet->{DHO_ROOT_PATH()} = '/exports/foobar'; |
190 |
|
$type[1] = 'ack'; |
191 |
} else { |
} else { |
192 |
$packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK(); |
$packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK(); |
193 |
$packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip"; |
$packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip got $requested_ip"; |
194 |
|
$type[1] = 'nak'; |
195 |
} |
} |
196 |
} elsif ($messagetype eq DHCPINFORM()) { |
} elsif ($messagetype eq DHCPINFORM()) { |
197 |
log::mac $mac, "DHCP INFORM ignored"; |
@type = qw( inform ignored ); |
198 |
} else { |
} else { |
199 |
log::mac $mac, "$messagetype igored (bootp?)"; |
@type = ( $messagetype, 'ignored' ); |
200 |
} |
} |
201 |
|
|
202 |
warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug; |
warn "# type ",dump @type; |
203 |
|
$audit->{type} = [ @type ]; |
204 |
|
|
205 |
|
$audit->{response} = $packet; |
206 |
|
|
207 |
$packet = new Net::DHCP::Packet( %$packet ); |
$packet = new Net::DHCP::Packet( %$packet ); |
208 |
warn "send ",$packet->toString() if $debug; |
warn "send ",$packet->toString() if $debug; |
209 |
|
|
210 |
my $reply = IO::Socket::INET->new( |
if ( ip::in_dhcp_range( $ip ) ) { |
211 |
LocalAddr => $server::ip, |
my $buff = $packet->serialize(); |
|
LocalPort => 67, |
|
|
Proto => "udp", |
|
|
Broadcast => 1, |
|
|
PeerAddr => '255.255.255.255', |
|
|
PeerPort => 68, |
|
|
Reuse => 1, |
|
|
) or die "socket: $@"; |
|
212 |
|
|
213 |
my $buff = $packet->serialize(); |
my $reply = IO::Socket::INET->new( |
214 |
$reply->send( $buff, 0 ) or die "Error sending: $!\n"; |
LocalAddr => $server::ip, |
215 |
|
LocalPort => 67, |
216 |
|
Proto => "udp", |
217 |
|
Broadcast => 1, |
218 |
|
# PeerAddr => '255.255.255.255', |
219 |
|
PeerAddr => $server::bcast, |
220 |
|
PeerPort => 68, |
221 |
|
Reuse => 1, |
222 |
|
) or die "socket: $@"; |
223 |
|
|
224 |
|
$reply->send( $buff, 0 ) or die "Error sending: $!\n"; |
225 |
|
warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n"; |
226 |
|
} else { |
227 |
|
$audit->{error} = "$ip not in server range $server::ip $server::netmask - no packet sent"; |
228 |
|
warn $audit->{error}; |
229 |
|
} |
230 |
|
|
231 |
|
CouchDB::audit( @type, $audit ); |
232 |
|
|
233 |
# system("arp -s $ip $mac"), |
# system("arp -s $ip $mac"), |
234 |
|
|
250 |
|
|
251 |
print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n"; |
print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n"; |
252 |
|
|
253 |
|
CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } ); |
254 |
|
|
255 |
while (1) { |
while (1) { |
256 |
|
server->refresh; |
257 |
process_packet $sock; |
process_packet $sock; |
258 |
} |
} |
259 |
} |
} |