/[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 48 by dpavlin, Thu Jul 30 00:07:57 2009 UTC revision 457 by dpavlin, Sat Jan 2 17:20:40 2010 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;  
 use Module::Refresh;  
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 server;  use CouchDB;
27  use pxe;  use format;
28    
29  my $debug = 1;  use server;
30    my $debug = server::debug;
31    
32  if ( ! $server::ip ) {  if ( ! $server::ip ) {
33          my $server_ip = `/sbin/ifconfig`;          my $server_ip = `/sbin/ifconfig`;
# Line 36  if ( ! $server::ip ) { Line 35  if ( ! $server::ip ) {
35          $server::ip = $server_ip;          $server::ip = $server_ip;
36  }  }
37    
38  warn "server ip $server::ip file: $pxe::file 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_ip {  sub client_mac_ip {
43          my ( $mac ) = @_;          my ( $mac, $request_ip ) = @_;
44    
45          my $conf = "conf/$server::ip";          if ( ! $mac ) {
46          mkdir $conf unless -e $conf;                  warn "W: no mac in requiest\n";
47                    return;
         if ( -e "$conf/mac/$mac" ) {  
                 my $ip = read_file "$conf/mac/$mac";  
                 print "$mac old $ip\n";  
                 return $ip;  
48          }          }
49    
50          mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );          my $conf = $server::conf;
51            mkdir $conf unless -e $conf;
         my $p = Net::Ping->new;  
52    
53          my $prefix = $server::ip;          my $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;  
         }  
54    
55          write_file "$conf/mac/$mac", $ip;          if ( $ip = client::ip_from_mac( $mac ) ) {
56          if ( -l "$conf/ip/$ip" && readlink "$conf/ip/$ip" ne "$conf/mac/$mac") {                  print "RENEW $mac $ip\n";
57                  unlink     "$conf/ip/$ip";                  client::save_ip_mac( $ip, $mac );
58                  symlink    "$conf/mac/$mac", "$conf/ip/$ip";                  return $ip;
59                  warn "$mac IP changed to $ip";          } 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    
         print "$mac NEW $ip\n";  
   
68          return $ip;          return $ip;
69  }  }
70    
71    use log;
72    use config;
73    use pxelinux;
74    use client;
75    
76    our $file;
77  our $transaction = 0; # FIXME predictible transaction numbers  our $transaction = 0; # FIXME predictible transaction numbers
78    
79  sub process_packet {  sub process_packet {
# Line 86  sub process_packet { Line 84  sub process_packet {
84          my $size = 'empty';          my $size = 'empty';
85          $size = length($buf) . ' bytes' if defined $buf;          $size = length($buf) . ' bytes' if defined $buf;
86    
87          print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n";          print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n" if $debug;
88          return unless $buf;          return unless $buf;
89    
90          my $dhcp = Net::DHCP::Packet->new($buf);          my $dhcp = Net::DHCP::Packet->new($buf);
91    
92            warn "recv: ", $dhcp->toString if $debug;
93    
94          $dhcp->comment( $transaction++ );          $dhcp->comment( $transaction++ );
95    
96          warn "recv: ", $dhcp->toString;          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 $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);          my $audit = { mac => $mac, ip => $ip, hostname => $hostname,
103          my $ip = client_ip($mac);                  options => {
104                            map {
105                                    ( $_ => $dhcp->getOptionValue( $_ ) )
106                            } @{ $dhcp->{options_order} }
107                    },
108            };
109    
110  =for later  =for later
111    
# Line 109  sub process_packet { Line 119  sub process_packet {
119    
120  =cut  =cut
121    
122            config::for_ip( $ip );
123    
124            my $server = server::as_hash_for $ip;
125    
126          my $packet = {          my $packet = {
127                  Op              => BOOTREPLY(),                  Op              => BOOTREPLY(),
128                  Hops    => $dhcp->hops(),                  Hops    => $dhcp->hops(),
# Line 116  sub process_packet { Line 130  sub process_packet {
130                  Flags   => $dhcp->flags(),                  Flags   => $dhcp->flags(),
131                  Ciaddr  => $dhcp->ciaddr(),                  Ciaddr  => $dhcp->ciaddr(),
132                  Yiaddr  => $ip,                  Yiaddr  => $ip,
133                  Siaddr  => $server::ip,                  Siaddr  => $server->{ip},
134                  Giaddr  => $dhcp->giaddr(),                  Giaddr  => $dhcp->giaddr(),
135                  Chaddr  => $dhcp->chaddr(),                  Chaddr  => $dhcp->chaddr(),
136                  File    => $pxe::file,                  File    => $file,
137                    DHO_DHCP_SERVER_IDENTIFIER()    => $server->{ip},       # busybox/udhcpc needs it but doesn't request
138            };
139    
140            my $options = {
141                    DHO_SUBNET_MASK()       => $server->{netmask},
142                    DHO_ROUTERS()           => $server->{ip},
143                    DHO_DOMAIN_NAME()       => $server->{domain},
144                    DHO_NAME_SERVERS()      => $server->{ip},
145                    DHO_DOMAIN_NAME_SERVERS() => $server->{ip},
146                    DHO_HOST_NAME()         => client::conf( $ip, 'hostname' ),
147                    DHO_BROADCAST_ADDRESS() => $server->{bcast},
148    #               DHO_NTP_SERVERS() => '',
149          };          };
150    
151            my @requested = split(/\s/, $dhcp->getOptionValue(DHO_DHCP_PARAMETER_REQUEST_LIST));
152            warn "options ",dump( $options ), ' requested: ',dump( @requested ) if $debug;
153    
154            my @missing;
155            foreach ( @requested ) {
156                    if ( defined $options->{$_} ) {
157                            $packet->{$_} = $options->{$_};
158                    } else {
159                            push @missing, $_;
160                    }
161            }
162    
163            warn "W: options requested but missing: ",dump( @missing ),$/;
164            $audit->{requested} = [ @requested ];
165            $audit->{missing}   = [ @missing   ];
166    
167            foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
168                    my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
169                    warn "DH0: $@" if $@;
170                    my $v = eval "\$pxelinux::$opt";
171                    warn "v: $@" if $@;
172                    next unless defined $v;
173                    warn "pxelinux dhcp option $opt = $DH0 = $v\n" if $debug;
174                    $packet->{ $DH0 } = $v;
175            }
176    
177          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());          my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
178    
179            my @type;
180    
181          if ($messagetype eq DHCPDISCOVER()) {          if ($messagetype eq DHCPDISCOVER()) {
                 warn "DHCP DISCOVER";  
182                  $packet->{Comment} = $dhcp->comment();                  $packet->{Comment} = $dhcp->comment();
183                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();                  $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
184                    @type = qw( discover offer );
185          } elsif ($messagetype eq DHCPREQUEST()) {          } elsif ($messagetype eq DHCPREQUEST()) {
186                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());                  @type = qw( request );
187                  warn "DHCP REQUEST $requested_ip";                  my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS()) || $dhcp->ciaddr();
188                  if ( $ip eq $requested_ip ) {                  if ( $ip eq $requested_ip ) {
189                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
190                          $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;  
191  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';  #                       $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';
192                            $type[1] = 'ack';
193                  } else {                  } else {
194                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();                          $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
195                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";                          $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip got $requested_ip";
196                            $type[1] = 'nak';
197                  }                  }
198          } elsif ($messagetype eq DHCPINFORM()) {          } elsif ($messagetype eq DHCPINFORM()) {
199                  warn "DHCP INFORM ignored";                  @type = qw( inform ignored );
200          } else {          } else {
201                  warn "$messagetype igored (bootp?)";                  @type = ( $messagetype, 'ignored' );
202          }          }
203    
204          warn ">> $mac == $ip server: $server::ip", $pxe::file ? " pxe file: $pxe::file\n" : "\n";          warn "# type ",dump @type;
205            $audit->{type} = [ @type ];
206    
207            $audit->{response} = $packet;
208    
209          $packet = new Net::DHCP::Packet( %$packet );          $packet = new Net::DHCP::Packet( %$packet );
210          warn "## ",$packet->toString(),"\n" if $debug;          warn "send ",$packet->toString() if $debug;
211    
212            if ( ip::in_dhcp_range( $ip ) ) {
213                    my $buff = $packet->serialize();
214    
215          my $reply = IO::Socket::INET->new(                  my $reply = IO::Socket::INET->new(
216                  LocalAddr => $server::ip,                          LocalAddr => $server->{ip},
217                  LocalPort => 67,                          LocalPort => 67,
218                  Proto => "udp",                          Proto => "udp",
219                  Broadcast => 1,                          Broadcast => 1,
220                  PeerAddr => '255.255.255.255',  #                       PeerAddr => '255.255.255.255',
221                  PeerPort => 68,                          PeerAddr => $server->{bcast},
222                  Reuse => 1,                          PeerPort => 68,
223          ) or die "socket: $@";                          Reuse => 1,
224                    ) or die "socket: $@";
225    
226          my $buff = $packet->serialize();                  $reply->send( $buff, 0 ) or die "Error sending: $!\n";
227          $reply->send( $buff, 0 ) or die "Error sending: $!\n";                  warn ">> $mac == $ip server: $server->{ip}", $file ? " file: $file\n" : "\n";
228            } else {
229                    $audit->{error} = "$ip not in server range $server::ip $server::netmask - no packet sent";
230                    warn $audit->{error};
231            }
232    
233            CouchDB::audit( @type, $audit );
234    
235  #       system("arp -s $ip $mac"),  #       system("arp -s $ip $mac"),
236    
# Line 188  sub start { Line 252  sub start {
252    
253          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";          print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
254    
255            CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } );
256    
257          while (1) {          while (1) {
258                  Module::Refresh->refresh;                  server->refresh;
259                  process_packet $sock;                  process_packet $sock;
260          }          }
261  }  }

Legend:
Removed from v.48  
changed lines
  Added in v.457

  ViewVC Help
Powered by ViewVC 1.1.26