- correct handling of files and symlinks in conf/
[pxelator] / bin / dhcpd.pl
1 #!/usr/bin/perl
2
3 # based on http://www.perlmonks.org/index.pl?node_id=325248
4
5 use strict;
6 use warnings;
7
8 use autodie;
9
10 use IO::Socket::INET;
11 use Net::DHCP::Packet;
12 use Net::DHCP::Constants;
13 use File::Slurp;
14 use Data::Dump qw/dump/;
15
16 die "need to run $0 as root like this\nsudo $0\n" unless $< == 0;
17
18 my $debug = shift @ARGV;
19
20 our ( $file, $gpxe_file );
21 our ( $ip_from, $ip_to ) = ( 10, 100 );
22
23 our $server_ip = readlink 'conf/server.ip' if -l 'conf/server.ip';
24
25 if ( ! $server_ip ) {
26         $server_ip = `/sbin/ifconfig`;
27         $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs;
28         warn "auto-configure server ip to $server_ip\n";
29 } else {
30         warn "server ip $server_ip\n";
31 }
32
33 my $sock = IO::Socket::INET->new(
34         LocalPort       => 67,
35 #       LocalAddr       => 'localhost',
36 #       LocalAddr       => '10.0.0.100',
37         LocalAddr       => '0.0.0.0',
38         Proto           => 'udp',
39         ReuseAddr       => 1,
40 #       PeerPort        => getservbyname('bootpc', 'udp'),
41         Broadcast       => 1,
42         Type            => SOCK_DGRAM,
43 ) or die "Failed to bind to socket: $@";
44
45
46 my $addr = $ip_from;
47
48 sub client_ip {
49         my ( $mac ) = @_;
50
51         my $conf = "conf/$server_ip";
52         mkdir $conf unless -e $conf;
53
54         if ( -e "$conf/mac/$mac" ) {
55                 my $ip = read_file "$conf/mac/$mac";
56                 print "$mac old $ip\n";
57                 return $ip;
58         }
59
60         mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );
61
62         my $prefix = $server_ip;
63         $prefix =~ s{\.\d+$}{.};
64         my $ip = $prefix . $addr;
65         while ( -e "conf/ip/$ip" ) {
66                 $ip = $prefix . $addr++;
67                 die "all addresses allocated!" if $addr == $ip_to;
68         }
69
70         write_file "$conf/mac/$mac", $ip;
71         unlink     "$conf/ip/$ip" if -e "$conf/ip/$ip";
72         symlink    "$conf/mac/$mac", "$conf/ip/$ip";
73
74         print "$mac NEW $ip\n";
75
76         return $ip;
77 }
78
79 while (1) {
80
81         require "config.pl"; # refresh config
82
83         print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";
84
85         my $buf;
86         $sock->recv($buf, 1024);
87         print "<< ",$sock->peerhost,":",$sock->peerport,"\n";
88
89         if (defined $buf) {
90
91                 my $dhcp;
92
93                 eval { $dhcp = Net::DHCP::Packet->new($buf); };
94                 die "can't use request", dump( $buf ) if $@;
95
96                 if ( $debug ) {
97                         warn "recv: ", $dhcp->toString, "\n\n";
98                 }
99
100                 my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
101                 my $ip = client_ip($mac);
102                 my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
103
104                 if ( $user_class eq 'gPXE' ) {
105                         $file = $gpxe_file;
106                 } elsif ( ! $file ) {
107                         $file = 'undionly.kpxe';
108                 }
109
110                 my $packet = new Net::DHCP::Packet(
111                         Op              => BOOTREPLY(),
112                         Hops    => $dhcp->hops(),
113                         Xid             => $dhcp->xid(),
114                         Flags   => $dhcp->flags(),
115                         Ciaddr  => $dhcp->ciaddr(),
116                         Yiaddr  => $ip,
117                         Siaddr  => $server_ip,
118                         Giaddr  => $dhcp->giaddr(),
119                         Chaddr  => $dhcp->chaddr(),
120                         File    => $file,
121 #                       DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),
122                 DHO_SUBNET_MASK() => '255.0.0.0',
123                 );
124
125                 warn ">> $mac == $ip server: $server_ip file: $file\n";
126
127                 warn "## ",$packet->toString(),"\n" if $debug;
128
129                 my $reply = IO::Socket::INET->new(
130                         LocalAddr => $server_ip,
131                         LocalPort => 67,
132                         Proto => "udp",
133                         Broadcast => 1,
134                         PeerAddr => '255.255.255.255',
135                         PeerPort => 68,
136                         Reuse => 1,
137                 ) or die "socket: $@";
138
139                 my $buff = $packet->serialize();
140                 $reply->send( $buff, 0 ) or die "Error sending: $!\n";
141
142 #               system("arp -s $ip $mac"),
143
144         } else {
145                 print "No bootp request.\n";
146         }
147
148 }