/[pxelator]/lib/PXElator/dhcpd.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/PXElator/dhcpd.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 153 by dpavlin, Wed Aug 5 23:22:17 2009 UTC revision 313 by dpavlin, Thu Aug 27 18:59:12 2009 UTC
# Line 18  use autodie; Line 18  use autodie;
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    
# Line 35  if ( ! $server::ip ) { Line 37  if ( ! $server::ip ) {
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::base_dir/conf/$server::ip";          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                  return $ip;                  return $ip;
58            } elsif ( in_our_range( $request_ip ) ) {
59                    $ip = client::next_ip( $mac );
60                    print "NEW $mac $ip\n";
61            } else {
62                    $ip = $request_ip;
63                    client::save_ip_mac( $ip, $mac );
64                    warn "W: $ip our of server range $server::ip $server::netmask\n";
65          }          }
66    
         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;  
   
         if ( -l "$conf/ip/$ip" && readlink "$conf/ip/$ip" ne "$conf/mac/$mac" ) {  
                 unlink     "$conf/ip/$ip";  
                 warn "$mac IP changed from ", readlink "$conf/ip/$ip", " to $ip";  
         };  
         symlink    "$conf/mac/$mac", "$conf/ip/$ip";  
   
         print "$mac NEW $ip\n";  
   
67          return $ip;          return $ip;
68  }  }
69    
70  use log;  use log;
71  use config;  use config;
72  use pxelinux;  use pxelinux;
73    use client;
74    
75  our $file;  our $file;
76  our $transaction = 0; # FIXME predictible transaction numbers  our $transaction = 0; # FIXME predictible transaction numbers
77    
78    sub ip2bin { pack('C*', split(/\./, $_[0])) };
79    sub in_our_range {
80            my $ip = shift;
81            return 1 if $ip eq '0.0.0.0';
82            return 1 if (
83                    ( ip2bin($ip)         & ip2bin($server::netmask) )
84                    eq
85                    ( ip2bin($server::ip) & ip2bin($server::netmask) )
86            );
87    }
88    
89  sub process_packet {  sub process_packet {
90          my $sock = shift;          my $sock = shift;
91    
# Line 93  sub process_packet { Line 100  sub process_packet {
100          return unless $buf;          return unless $buf;
101    
102          my $dhcp = Net::DHCP::Packet->new($buf);          my $dhcp = Net::DHCP::Packet->new($buf);
         $dhcp->comment( $transaction++ );  
103    
104          warn "recv: ", $dhcp->toString if $debug;          warn "recv: ", $dhcp->toString if $debug;
105    
106          my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);          $dhcp->comment( $transaction++ );
107          my $ip = client_ip($mac);  
108            my $mac = format::mac( substr($dhcp->chaddr(),0,$dhcp->hlen()*2) );
109            my $ip = client_mac_ip($mac, $dhcp->ciaddr);
110    
111            my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
112            print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n" if $hostname;
113    
114            my $audit = { mac => $mac, ip => $ip, hostname => $hostname,
115                    options => {
116                            map {
117                                    ( $_ => $dhcp->getOptionValue( $_ ) )
118                            } @{ $dhcp->{options_order} }
119                    },
120            };
121    
122  =for later  =for later
123    
# Line 125  sub process_packet { Line 144  sub process_packet {
144                  Giaddr  => $dhcp->giaddr(),                  Giaddr  => $dhcp->giaddr(),
145                  Chaddr  => $dhcp->chaddr(),                  Chaddr  => $dhcp->chaddr(),
146                  File    => $file,                  File    => $file,
147                    DHO_DHCP_SERVER_IDENTIFIER()    => $server::ip, # busybox/udhcpc needs it but doesn't request
148            };
149    
150            my $options = {
151                    DHO_SUBNET_MASK()       => $server::netmask,
152                    DHO_ROUTERS()           => $server::ip,
153                    DHO_DOMAIN_NAME()       => $server::domain_name,
154                    DHO_NAME_SERVERS()      => $server::ip,
155                    DHO_DOMAIN_NAME_SERVERS() => $server::ip,
156                    DHO_HOST_NAME()         => client::conf( $ip, 'hostname' ),
157                    DHO_BROADCAST_ADDRESS() => $server::bcast,
158    #               DHO_NTP_SERVERS() => '',
159          };          };
160    
161            my @requested = split(/\s/, $dhcp->getOptionValue(DHO_DHCP_PARAMETER_REQUEST_LIST));
162            warn "options ",dump( $options ), ' requested: ',dump( @requested ) if $debug;
163    
164            my @missing;
165            foreach ( @requested ) {
166                    if ( defined $options->{$_} ) {
167                            $packet->{$_} = $options->{$_};
168                    } else {
169                            push @missing, $_;
170                    }
171            }
172    
173            warn "W: options requested but missing: ",dump( @missing ),$/;
174            $audit->{requested} = [ @requested ];
175            $audit->{missing}   = [ @missing   ];
176    
177          foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {          foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
178                  my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;                  my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
179                  warn "DH0: $@" if $@;                  warn "DH0: $@" if $@;
180                  my $v = eval "\$pxelinux::$opt";                  my $v = eval "\$pxelinux::$opt";
181                  warn "v: $@" if $@;                  warn "v: $@" if $@;
182                  next unless defined $v;                  next unless defined $v;
183                  warn "pxelinux dhcp option $opt = $DH0 = $v";                  warn "pxelinux dhcp option $opt = $DH0 = $v\n" if $debug;
184                  $packet->{ $DH0 } = $v;                  $packet->{ $DH0 } = $v;
185          }          }
186    
187          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
188    
189          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
190                  log::mac $mac, "DHCP DISCOVER";                  $audit->{type} = 'discover';
191                  $packet->{Comment} = $dhcp->comment();                  $packet->{Comment} = $dhcp->comment();
192                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
193          } elsif ($messagetype eq DHCPREQUEST()) {          } elsif ($messagetype eq DHCPREQUEST()) {
194                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
195                  log::mac $mac, "DHCP REQUEST $requested_ip $ip $file";                  $audit->{type} = 'request';
196                  if ( $ip eq $requested_ip ) {                  if ( $ip eq $requested_ip ) {
197                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
198                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min                          $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
 #                       $packet->{DHO_DHCP_SERVER_IDENTIFIER()} = $server::ip;          # FIXME  
                         $packet->{DHO_SUBNET_MASK()}            = '255.255.255.0';  
                         $packet->{DHO_ROUTERS()}                = $server::ip;  
                         $packet->{DHO_DOMAIN_NAME()}            = 'pxelator.lan';  
                         $packet->{DHO_NAME_SERVERS()}           = $server::ip;  
199  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';
200                  } else {                  } else {
201                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
202                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
203                  }                  }
204          } elsif ($messagetype eq DHCPINFORM()) {          } elsif ($messagetype eq DHCPINFORM()) {
205                  log::mac $mac, "DHCP INFORM ignored";                  $audit->{type} = 'inform';
206          } else {          } else {
207                  log::mac $mac, "$messagetype igored (bootp?)";                  $audit->{type} = sprintf('ignored %x', $messagetype);
208          }          }
209    
210          warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug;          warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug;
211            $audit->{response} = $packet;
 warn "## created packet ", dump( $packet );  
212    
213          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
214          warn "send ",$packet->toString() if $debug;          warn "send ",$packet->toString() if $debug;
215    
216          my $reply = IO::Socket::INET->new(          if ( in_our_range( $ip ) ) {
217                  LocalAddr => $server::ip,                  my $buff = $packet->serialize();
218                  LocalPort => 67,  
219                  Proto => "udp",                  my $reply = IO::Socket::INET->new(
220                  Broadcast => 1,                          LocalAddr => $server::ip,
221                  PeerAddr => '255.255.255.255',                          LocalPort => 67,
222                  PeerPort => 68,                          Proto => "udp",
223                  Reuse => 1,                          Broadcast => 1,
224          ) or die "socket: $@";                          PeerAddr => '255.255.255.255',
225                            PeerPort => 68,
226                            Reuse => 1,
227                    ) or die "socket: $@";
228    
229          my $buff = $packet->serialize();                  $reply->send( $buff, 0 ) or die "Error sending: $!\n";
230          $reply->send( $buff, 0 ) or die "Error sending: $!\n";          } else {
231                    $audit->{error} = "$ip our of our range $server::ip $server::netmask";
232            }
233    
234            CouchDB::audit( $audit->{type}, $audit );
235    
236  #       system("arp -s $ip $mac"),  #       system("arp -s $ip $mac"),
237    
# Line 205  sub start { Line 253  sub start {
253    
254          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
255    
256            CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } );
257    
258          while (1) {          while (1) {
259                  process_packet $sock;                  process_packet $sock;
260          }          }

Legend:
Removed from v.153  
changed lines
  Added in v.313

  ViewVC Help
Powered by ViewVC 1.1.26