first verision which boots virtual machine logging using new CouchDB::audit code
[pxelator] / lib / PXElator / dhcpd.pm
index 83f1cd3..2c3c9a7 100644 (file)
@@ -18,12 +18,13 @@ use autodie;
 use IO::Socket::INET;
 use File::Slurp;
 use Data::Dump qw/dump/;
-use Net::Ping;
 
 use lib '..';
 use Net::DHCP::Packet;
 use Net::DHCP::Constants 0.67;
 
+use CouchDB;
+
 use server;
 my $debug = server::debug;
 
@@ -35,7 +36,7 @@ if ( ! $server::ip ) {
 
 warn "server ip $server::ip range: $server::ip_from - $server::ip_to\n";
 
-my $addr = $server::ip_from;
+use client;
 
 sub client_ip {
        my ( $mac ) = @_;
@@ -43,42 +44,23 @@ sub client_ip {
        my $conf = $server::conf;
        mkdir $conf unless -e $conf;
 
-       if ( -e "$conf/mac/$mac" ) {
-               my $ip = read_file "$conf/mac/$mac";
+       my $ip;
+
+       if ( $ip = client::ip_from_mac( $mac ) ) {
                print "RENEW $mac $ip\n";
                return $ip;
+       } else {
+               $ip = client::next_ip( $mac );
+               print "NEW $mac $ip\n";
        }
 
-       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";
-
        return $ip;
 }
 
 use log;
 use config;
 use pxelinux;
+use client;
 
 our $file;
 our $transaction = 0; # FIXME predictible transaction numbers
@@ -104,6 +86,12 @@ sub process_packet {
        my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
        my $ip = client_ip($mac);
 
+       my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
+       print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";
+
+       my $audit = { mac => $mac, ip => $ip, hostname => $hostname };
+       CouchDB::audit( 'request', $audit );
+
 =for later
 
        my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
@@ -129,53 +117,77 @@ sub process_packet {
                Giaddr  => $dhcp->giaddr(),
                Chaddr  => $dhcp->chaddr(),
                File    => $file,
+               DHO_DHCP_SERVER_IDENTIFIER()    => $server::ip, # busybox/udhcpc needs it but doesn't request
+       };
+
+       my $options = {
+               DHO_SUBNET_MASK()       => $server::netmask,
+               DHO_ROUTERS()           => $server::ip,
+               DHO_DOMAIN_NAME()       => $server::domain_name,
+               DHO_NAME_SERVERS()      => $server::ip,
+               DHO_DOMAIN_NAME_SERVERS() => $server::ip,
+               DHO_HOST_NAME()         => client::conf( $ip, 'hostname' ),
+               DHO_BROADCAST_ADDRESS() => $server::bcast,
+#              DHO_NTP_SERVERS() => '',
        };
 
+       my @requested = split(/\s/, $dhcp->getOptionValue(DHO_DHCP_PARAMETER_REQUEST_LIST));
+       warn "options ",dump( $options ), ' requested: ',dump( @requested ) if $debug;
+
+       my @missing;
+       foreach ( @requested ) {
+               if ( defined $options->{$_} ) {
+                       $packet->{$_} = $options->{$_};
+               } else {
+                       push @missing, $_;
+               }
+       }
+
+       warn "W: options requested but missing: ",dump( @missing ),$/;
+       $audit->{requested} = [ @requested ];
+       $audit->{missing}   = [ @missing   ];
+
        foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
                my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
                warn "DH0: $@" if $@;
                my $v = eval "\$pxelinux::$opt";
                warn "v: $@" if $@;
                next unless defined $v;
-               warn "pxelinux dhcp option $opt = $DH0 = $v";
+               warn "pxelinux dhcp option $opt = $DH0 = $v\n" if $debug;
                $packet->{ $DH0 } = $v;
        }
 
        my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
 
        if ($messagetype eq DHCPDISCOVER()) {
-               log::mac $mac, "DHCP DISCOVER";
+               $audit->{type} = 'discover';
                $packet->{Comment} = $dhcp->comment();
                $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
        } elsif ($messagetype eq DHCPREQUEST()) {
                my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
-               log::mac $mac, "DHCP REQUEST $requested_ip $ip $file";
+               $audit->{type} = 'request';
                if ( $ip eq $requested_ip ) {
                        $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
                        $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;
 #                      $packet->{DHO_ROOT_PATH()}              = '/exports/foobar';
                } else {
                        $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
                        $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
                }
        } elsif ($messagetype eq DHCPINFORM()) {
-               log::mac $mac, "DHCP INFORM ignored";
+               $audit->{type} = 'inform';
        } else {
-               log::mac $mac, "$messagetype igored (bootp?)";
+               $audit->{type} = sprintf('ignored %x', $messagetype);
        }
 
        warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug;
-
-warn "## created packet ", dump( $packet );
+       $audit->{response} = $packet;
 
        $packet = new Net::DHCP::Packet( %$packet );
        warn "send ",$packet->toString() if $debug;
 
+       CouchDB::audit( 'response', $audit );
+
        my $reply = IO::Socket::INET->new(
                LocalAddr => $server::ip,
                LocalPort => 67,
@@ -209,6 +221,8 @@ sub start {
 
        print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
 
+       CouchDB::audit( 'start', { 'listen' => { addr => $sock->sockhost, port => $sock->sockport } } );
+
        while (1) {
                process_packet $sock;
        }