f679f02b4910ecd20e993f940d3a85593b3c0929
[pxelator] / lib / PXElator / httpd.pm
1 package httpd;
2
3 use warnings;
4 use strict;
5 use autodie;
6
7 =head1 httpd
8
9 Start with:
10
11   perl -Ilib/PXElator -Mhttpd -e httpd::start
12
13 =cut
14
15 use Data::Dump qw/dump/;
16 use Carp qw/confess/;
17 use File::Slurp;
18 #use JSON;
19 use IO::Socket::INET;
20 use Regexp::Common qw/net/;
21
22 sub menu {qq{
23
24 <div style="font-size: 80%; color: #888">
25 <a href=/>home</a>
26 <a href=/server>server</a>
27 <a href=/brctl>brctl</a>
28 <a href=/ip>ip</a>
29 <a href=/nmap>nmap</a>
30 <a href=/client>client</a>
31 </div>
32
33 }}
34
35 our $port = 7777;
36
37 use server;
38 our $debug = server::debug;
39 our $url = "http://$server::ip:$port";
40
41 use html;
42 our $static_pids;
43 use progress_bar;
44 use config;
45 use client;
46 use log;
47 use x11;
48 use amt;
49 use boolean;
50 use daemons;
51
52 use kvm;
53 use browser;
54 use network;
55 use ip;
56 use wireshark;
57 use syslogd;
58 use nmap;
59
60 use CouchDB;
61
62 sub static {
63         my ($client,$path) = @_;
64
65         my $full = "$server::base_dir/tftp/$path";
66
67         return if ! -f $full;
68
69         if ( my $pid = fork ) {
70                 # parent
71                 close($client);
72                 $static_pids->{$pid} = $path;
73                 return 1;
74         }
75
76         my $type = 'application/octet-stream';
77         $type = 'text/html' if $path =~ m{\.htm};
78         $type = 'application/javascript' if $path =~ m{\.js};
79         $type = 'text/plain' if $path =~ m{\.txt};
80
81         my $size = -s $full || return;
82
83         print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
84
85         open(my $fh, $full);
86
87         my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
88         my $buff;
89         my $pos = 0;
90
91         CouchDB::audit( 'static', { pid => $$, path => $path, type => $type, size => $size, block => $block, peerhost => $client->peerhost });
92
93         progress_bar::start;
94
95         while( my $len = read $fh, $buff, $block ) {
96                 print $client $buff;
97                 $client->flush;
98                 $pos += $len;
99                 progress_bar::tick( $path, $pos, $size );
100         }
101         close($fh);
102         close($client);
103
104         print STDERR "\n";
105
106         exit(0);
107 }
108
109 sub ok {
110         qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . menu()
111 }
112
113 sub redirect {
114         my $to = shift;
115         $to ||= $url;
116         qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $to\r\n\r\n|
117 }
118
119 sub get_request {
120         my ( $client, $path, $param ) = @_;
121
122         server->refresh;
123
124         CouchDB::audit( 'request', { path => $path, param => $param, peerhost => $client->peerhost } );
125
126         if ( my $found = static( $client,$path ) ) {
127                 warn "static $found" if $debug;
128         } elsif ( $path eq '/' ) {
129
130                 my @rows;
131
132                 my $debug_proc = '';
133
134 warn "XXX pids = ", dump( $daemons::pids );
135
136                 foreach my $name ( sort keys %$daemons::pids ) {
137                         my $pid = $daemons::pids->{$name}; # || next;
138
139                         my $html;
140
141                         my $proc = "/proc/$pid/status";
142
143                         if ( -e $proc ) {
144                                 $html .= qq|<a href=/start_stop/$name>$pid</a>|;
145                                 if ( $debug ) {
146                                         $html .= qq| <a name=$pid href=#proc-$pid>?</a>| if $name->can('start');
147
148                                         $debug_proc
149                                                 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
150                                                 .  read_file($proc)
151                                                 .  qq|</pre>|
152                                                 ;
153                                 }
154
155                                 if ( $name->can('fork_if_active') ) {
156                                         $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $name->fork_if_active;
157                                 }
158
159                                 if ( $name->can('actions') ) {
160                                         $html .= qq| <a href=/action/$name/$_>$_</a>| foreach $name->actions;
161                                 }
162                         } else {
163                                 if ( $pid =~ m{^\d+$} ) {
164                                         $html .= qq|$pid exited |
165                                 } else {
166                                         $html .= qq|$pid |;
167                                 }
168                                 $html .= qq|<a href=/start_stop/$name>restart</a>| if $pid || $name->can('start');
169                                 if ( $name->can('fork_actions') ) {
170                                         $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $name->fork_actions;
171                                 }
172                         }
173
174                         die "no html generated" unless $html;
175
176                         push @rows, ( $name => $html );
177                 }
178
179                 my $below_table = '';
180
181                 warn 'static_pids: ', dump( $static_pids ) if $debug;
182                 foreach my $pid ( keys %$static_pids ) {
183                         my $path = $static_pids->{$pid};
184                         if ( -d "/proc/$pid" ) {
185                                 push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
186                         } elsif ( $param->{clean_completed_downloads} ) {
187                                 delete $static_pids->{$pid}
188                         } else {
189                                 push @rows, ( $path => "$pid competed" );
190                                 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
191                         }
192                 }
193
194                 print $client ok
195                         , html::table( 2, @rows )
196                         , $below_table
197                         , html::tabs( log::mac_changes )
198                         , $debug_proc
199                         ;
200
201         } elsif ( $path =~ m{^/server} ) {
202                 print $client ok
203                         , html::table( 2,
204                                 'debug' => qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
205                                  map {
206                                         ( $_, html::tt eval '$server::'.$_ )
207                                  } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir', 'conf' )
208                         )
209                         ;
210         } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
211                 my $ip = $1;
212
213                 if ( $param->{action} eq 'remove' ) {
214                         client::remove( $param->{change_ip} );
215                         print $client redirect("$url/client");
216                         return;
217                 } elsif ( $param->{action} eq 'change' ) {
218                         if ( my $new_ip = client::change_ip( $ip, $param->{change_ip} ) ) {
219                                 print $client redirect("$url/client#$new_ip");
220                                 return;
221                         }
222                 }
223
224                 if ( ! $ip ) {
225                         my $peer_ip = $client->peerhost;
226
227                         my $netmask = ip::to_int $server::netmask;
228                         my $network = ip::to_int $server::ip & $netmask;
229                         my ( $from, $to ) = ( $network | $server::ip_from, $network | $server::ip_to );
230                         my $ip_int  = ip::to_int $peer_ip;
231
232                         # show edit for clients in our dhcp range
233                         if ( $ip_int >= ( $network | $server::ip_from ) && $ip_int <= ( $network | $server::ip_to ) ) {
234                                 $ip = $peer_ip;
235                         }
236                 }
237
238                 if ( $ip && $ip ne $server::ip ) {
239                         my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} );
240
241                         my @table = (
242                                 'ip' => qq|<input type=text name=change_ip value="$ip" onChange="document.getElementById('old_ip').style.display = '';"><span id=old_ip style="display: none; color: #888;">old: $ip<span>|,
243                                 'hostname' => qq|<input type=text name=hostname value="$hostname">|,
244                         );
245
246                         my $deploy;
247
248                         if ( my $mac = client::mac_from_ip( $ip ) ) {
249                                 $deploy = client::conf( $ip, 'deploy' => $param->{deploy} );
250                                 push @table, (
251                                         'mac' => format::mac( $mac => 'html' ),
252                                         'deploy' => html::select( 'deploy', $deploy, config::available )
253                                 );
254                                 if ( my $pxelinux = config::for_ip( $ip ) ) {
255                                         $deploy = qq|<h2>PXElinux</h2>| . html::pre( $pxelinux );
256                                 }
257                         }
258
259                         print $client ok
260                                 , qq|<form method=get>|
261                                 , html::table( 2, @table ),
262                                 , qq|
263                                         <input type=submit name=action value=change>
264                                         <input type=submit name=action value=remove style="color: red">
265                                         </form>|
266                                 , $deploy
267                                 ;
268
269                         if ( my $amt = client::conf( $ip, 'amt' ) ) {
270                                 print $client qq|<h2>AMT</h2>|, amt::info( $amt, $ip );
271                         }
272
273                 } else {
274
275                         my $arp = client::arp_mac_dev;
276                         my @ips = client::all_ips;
277
278                         my $ping;
279                         $ping = ping::fping( @ips ) if $param->{ping};
280
281                         print $client ok
282                                 , qq|<h2>Clients on $server::ip</h2>|
283                                 , html::table( -6,
284                                         'ip', 'mac', 'dev', 'hostname', 'deploy', 'conf',
285                                         map {
286                                                 my $ip = $_;
287                                                 my $conf = client::all_conf( $ip );
288                                                 my $mac = delete $conf->{mac} || '';
289                                                 my $style;
290                                                 $style
291                                                         = 'style="color:'
292                                                         . ( $ping->{$ip} ? 'green' : 'red' )
293                                                         . '"'
294                                                         if $ping;
295                                                 (
296                                                         qq|<a $style name=$ip href=/client/$ip>$ip</a>|
297                                                         , format::mac( $mac => 'html' )
298                                                         , $arp->{$mac}
299                                                         , delete $conf->{hostname}
300                                                         , delete $conf->{deploy}
301                                                         , ( %$conf ? html::pre_dump( $conf ) : qq|<a href=/nmap?scan=$ip>nmap</a>| )
302                                                 );
303                                         } @ips
304                                 )
305                                 ;
306                         print $client qq|
307                                 <form method=get>
308                                 <input type=submit name=ping value=ping>
309                                 </form>
310                         |;
311                 }
312         } elsif ( $path =~ m{^/brctl} ) {
313                 print $client ok, html::table( -4,
314                         map {
315                                 my @c = split(/\t+/,$_,4);
316                                 if ( $#c == 1 ) {
317                                         ( '', '', '', $c[1] )
318                                 } else {
319                                         @c
320                                 }
321                         } split(/\n/, `brctl show`)
322                 );
323         } elsif ( $path =~ m{^/ip/?(\w+)?} ) {
324                 print $client ok
325                         , join("\n", map { qq|<a href=/ip/$_>$_</a>| } ( qw/link addr route neigh ntable tunnel maddr mroute xfrm/ ))
326                         , ip::html( $1 )
327                         ;
328         } elsif ( $path =~ m{^/nmap} ) {
329                 if ( my $scan = $param->{scan} ) {
330                         nmap::scan( $scan );
331                         print $client redirect("$url/client#$scan");
332                 } else {
333                         print $client ok, qq|
334                                 <form method=get>
335                                 <input type=text name=scan>
336                                 <input type=submit value=scan>
337                                 </form>
338                         |;
339                 }
340         } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
341                 eval 'our $' . $1 . ' = ' . $2;
342                 warn $@ if $@;
343                 print $client redirect($url), qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
344                 server::debug( $debug ) if $1 eq 'debug';
345         } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
346                 print $client redirect, daemons::start_stop($1);
347         } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
348                 $1->$2();
349                 print $client redirect;
350         } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
351                 print $client redirect;
352                 kill 1, $1 || kill 9, $2 && warn "killed $1";
353         } else {
354                 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
355                 warn "404 $path";
356         }
357
358 }
359
360 sub start {
361
362         warn 'network ', network::setup();
363
364         daemons::start_stop 'browser', $url;
365         daemons::start_stop $_ foreach ( qw/dhcpd tftpd dnsd syslogd/ );
366         daemons::start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device
367
368         my $server = IO::Socket::INET->new(
369                         Proto     => 'tcp',
370                         LocalAddr => $server::ip,
371                         LocalPort => $httpd::port,
372                         Listen    => SOMAXCONN,
373                         Reuse     => 1
374         ) || die "can't start server on $url: $!";
375
376         print "url $url\n";
377
378         while (1) {
379                 my $client = $server->accept() || next; # ALARM trickle us
380                 my $request = <$client>;
381
382                 warn "request $request\n" if $debug;
383
384                 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
385                         my $path = $1;
386                         $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
387                         my $param;
388                         if ( $path =~ s{\?(.+)}{} ) {
389                                 foreach my $p ( split(/[&;]/, $1) ) {
390                                         my ($n,$v) = split(/=/, $p, 2);
391                                         $param->{$n} = $v;
392                                 }
393                                 warn "param: ",dump( $param ) if $debug;
394                         }
395                         get_request $client, $path, $param;
396                 } else {
397                         print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
398                         warn "500 $request";
399                 }
400
401                 print $client menu() if $client->connected;
402
403         }
404
405         die "server died";
406 }
407
408 warn "loaded";
409
410 1;