/[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

Contents of /lib/PXElator/dhcpd.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 232 - (show annotations)
Sun Aug 16 22:51:57 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 5398 byte(s)
cleanup audit start
1 package dhcpd;
2
3 =head1 dhcpd
4
5 start with:
6
7 perl -Ilib/PXElator -Ilib -Mdhcpd -e start
8
9 based on L<http://www.perlmonks.org/index.pl?node_id=325248>
10
11 =cut
12
13 use strict;
14 use warnings;
15
16 use autodie;
17
18 use IO::Socket::INET;
19 use File::Slurp;
20 use Data::Dump qw/dump/;
21
22 use lib '..';
23 use Net::DHCP::Packet;
24 use Net::DHCP::Constants 0.67;
25
26 use CouchDB;
27 use format;
28
29 use server;
30 my $debug = server::debug;
31
32 if ( ! $server::ip ) {
33 my $server_ip = `/sbin/ifconfig`;
34 $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs;
35 $server::ip = $server_ip;
36 }
37
38 warn "server ip $server::ip range: $server::ip_from - $server::ip_to\n";
39
40 use client;
41
42 sub client_ip {
43 my ( $mac ) = @_;
44
45 my $conf = $server::conf;
46 mkdir $conf unless -e $conf;
47
48 my $ip;
49
50 if ( $ip = client::ip_from_mac( $mac ) ) {
51 print "RENEW $mac $ip\n";
52 return $ip;
53 } else {
54 $ip = client::next_ip( $mac );
55 print "NEW $mac $ip\n";
56 }
57
58 return $ip;
59 }
60
61 use log;
62 use config;
63 use pxelinux;
64 use client;
65
66 our $file;
67 our $transaction = 0; # FIXME predictible transaction numbers
68
69 sub process_packet {
70 my $sock = shift;
71
72 server->refresh;
73
74 my $buf;
75 $sock->recv($buf, 1024);
76 my $size = 'empty';
77 $size = length($buf) . ' bytes' if defined $buf;
78
79 print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n" if $debug;
80 return unless $buf;
81
82 my $dhcp = Net::DHCP::Packet->new($buf);
83 $dhcp->comment( $transaction++ );
84
85 warn "recv: ", $dhcp->toString if $debug;
86
87 my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
88 my $ip = client_ip($mac);
89
90 my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
91 print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";
92
93 my $audit = { mac => format::mac($mac), ip => $ip, hostname => $hostname };
94
95 =for later
96
97 my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
98
99 if ( $user_class eq 'gPXE' ) {
100 $file = $gpxe_file;
101 } elsif ( ! $file ) {
102 $file = 'undionly.kpxe';
103 }
104
105 =cut
106
107 config::for_ip( $ip );
108
109 my $packet = {
110 Op => BOOTREPLY(),
111 Hops => $dhcp->hops(),
112 Xid => $dhcp->xid(),
113 Flags => $dhcp->flags(),
114 Ciaddr => $dhcp->ciaddr(),
115 Yiaddr => $ip,
116 Siaddr => $server::ip,
117 Giaddr => $dhcp->giaddr(),
118 Chaddr => $dhcp->chaddr(),
119 File => $file,
120 DHO_DHCP_SERVER_IDENTIFIER() => $server::ip, # busybox/udhcpc needs it but doesn't request
121 };
122
123 my $options = {
124 DHO_SUBNET_MASK() => $server::netmask,
125 DHO_ROUTERS() => $server::ip,
126 DHO_DOMAIN_NAME() => $server::domain_name,
127 DHO_NAME_SERVERS() => $server::ip,
128 DHO_DOMAIN_NAME_SERVERS() => $server::ip,
129 DHO_HOST_NAME() => client::conf( $ip, 'hostname' ),
130 DHO_BROADCAST_ADDRESS() => $server::bcast,
131 # DHO_NTP_SERVERS() => '',
132 };
133
134 my @requested = split(/\s/, $dhcp->getOptionValue(DHO_DHCP_PARAMETER_REQUEST_LIST));
135 warn "options ",dump( $options ), ' requested: ',dump( @requested ) if $debug;
136
137 my @missing;
138 foreach ( @requested ) {
139 if ( defined $options->{$_} ) {
140 $packet->{$_} = $options->{$_};
141 } else {
142 push @missing, $_;
143 }
144 }
145
146 warn "W: options requested but missing: ",dump( @missing ),$/;
147 $audit->{requested} = [ @requested ];
148 $audit->{missing} = [ @missing ];
149
150 foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
151 my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
152 warn "DH0: $@" if $@;
153 my $v = eval "\$pxelinux::$opt";
154 warn "v: $@" if $@;
155 next unless defined $v;
156 warn "pxelinux dhcp option $opt = $DH0 = $v\n" if $debug;
157 $packet->{ $DH0 } = $v;
158 }
159
160 my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
161
162 if ($messagetype eq DHCPDISCOVER()) {
163 $audit->{type} = 'discover';
164 $packet->{Comment} = $dhcp->comment();
165 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
166 } elsif ($messagetype eq DHCPREQUEST()) {
167 my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
168 $audit->{type} = 'request';
169 if ( $ip eq $requested_ip ) {
170 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK();
171 $packet->{DHO_DHCP_LEASE_TIME()} = 5 * 60; # 5 min
172 # $packet->{DHO_ROOT_PATH()} = '/exports/foobar';
173 } else {
174 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
175 $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
176 }
177 } elsif ($messagetype eq DHCPINFORM()) {
178 $audit->{type} = 'inform';
179 } else {
180 $audit->{type} = sprintf('ignored %x', $messagetype);
181 }
182
183 warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug;
184 $audit->{response} = $packet;
185
186 $packet = new Net::DHCP::Packet( %$packet );
187 warn "send ",$packet->toString() if $debug;
188
189 CouchDB::audit( $audit->{type}, $audit );
190
191 my $reply = IO::Socket::INET->new(
192 LocalAddr => $server::ip,
193 LocalPort => 67,
194 Proto => "udp",
195 Broadcast => 1,
196 PeerAddr => '255.255.255.255',
197 PeerPort => 68,
198 Reuse => 1,
199 ) or die "socket: $@";
200
201 my $buff = $packet->serialize();
202 $reply->send( $buff, 0 ) or die "Error sending: $!\n";
203
204 # system("arp -s $ip $mac"),
205
206 }
207
208 sub start {
209
210 my $sock = IO::Socket::INET->new(
211 LocalPort => 67,
212 # LocalAddr => 'localhost',
213 # LocalAddr => '10.0.0.100',
214 LocalAddr => '0.0.0.0',
215 Proto => 'udp',
216 ReuseAddr => 1,
217 # PeerPort => getservbyname('bootpc', 'udp'),
218 Broadcast => 1,
219 Type => SOCK_DGRAM,
220 ) or die "Failed to bind to socket: $@";
221
222 print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
223
224 CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } );
225
226 while (1) {
227 process_packet $sock;
228 }
229 }
230
231 warn "loaded";
232
233 1;

  ViewVC Help
Powered by ViewVC 1.1.26