4 |
use strict; |
use strict; |
5 |
use autodie; |
use autodie; |
6 |
|
|
|
use server; |
|
7 |
use File::Slurp; |
use File::Slurp; |
8 |
use Net::Ping; |
use Net::Ping; |
9 |
|
use Carp qw/confess/; |
10 |
|
|
11 |
|
use server; |
12 |
|
use format; |
13 |
|
|
14 |
|
sub mkbasedir { |
15 |
|
my $path = shift; |
16 |
|
$path =~ s{(^.*)/[^/]+$}{$1}; |
17 |
|
mkdir $path unless -d $path; |
18 |
|
return $path; |
19 |
|
} |
20 |
|
|
21 |
sub mac_path { $server::conf . '/mac/' . $_[0] } |
sub mac_path { $server::conf . '/mac/' . $_[0] } |
22 |
sub ip_path { $server::conf . '/ip/' . join('/', @_) } |
sub ip_path { $server::conf . '/ip/' . join('/', @_) } |
23 |
|
sub conf_value { |
24 |
|
my $path = shift; |
25 |
|
my $value; |
26 |
|
if ( -l $path ) { |
27 |
|
$value = readlink $path; |
28 |
|
$value =~ s{.*/([^/]+)$}{$1}; |
29 |
|
} elsif ( -f $path ) { |
30 |
|
$value = read_file $path; |
31 |
|
} else { |
32 |
|
confess "$path not file or symlink"; |
33 |
|
} |
34 |
|
return $value; |
35 |
|
} |
36 |
|
|
37 |
sub conf { |
sub conf { |
38 |
my $ip = shift; |
my $ip = shift; |
49 |
$path .= '/' . $name; |
$path .= '/' . $name; |
50 |
|
|
51 |
if ( defined $value ) { |
if ( defined $value ) { |
52 |
|
mkbasedir $path; |
53 |
write_file $path, $value; |
write_file $path, $value; |
54 |
warn "update $path = $value"; |
warn "update $path = $value"; |
55 |
} elsif ( ! -e $path && defined $default ) { |
} elsif ( ! -e $path && defined $default ) { |
56 |
|
mkbasedir $path; |
57 |
write_file $path, $default; |
write_file $path, $default; |
58 |
warn "default $path = $default"; |
warn "default $path = $default"; |
59 |
$value = $default; |
$value = $default; |
60 |
} elsif ( -e $path ) { |
} elsif ( -f $path ) { |
61 |
if ( -l $path ) { |
$value = read_file $path; |
62 |
$value = readlink $path; |
} else { |
63 |
$value =~ s{.*/([^/]+)$}{$1}; |
confess "conf $name"; |
|
} else { |
|
|
$value = read_file $path; |
|
|
} |
|
64 |
} |
} |
65 |
return $value; |
return $value; |
66 |
} |
} |
67 |
|
|
|
sub mac { |
|
|
my ( $ip, $op ) = @_; |
|
|
$op ||= 'html'; |
|
|
my $mac = client::conf( $ip, 'mac' ); |
|
|
return '' unless $mac; |
|
|
$mac =~ s{(..)}{$1:}g; |
|
|
$mac =~ s{:$}{}; |
|
|
$mac = qq|<tt>$mac</tt>| if (caller(1))[3] =~ m{^httpd} && $op ne 'clean'; |
|
|
return uc($mac); |
|
|
} |
|
|
|
|
68 |
sub next_ip($) { |
sub next_ip($) { |
69 |
my $mac = shift; |
my $mac = shift; |
70 |
|
|
85 |
|
|
86 |
mkdir ip_path($ip); |
mkdir ip_path($ip); |
87 |
|
|
88 |
symlink ip_path($ip), mac_path($mac); |
my $mac_path = mac_path($mac); |
89 |
|
unlink $mac_path if -e $mac_path; # XXX audit? |
90 |
|
symlink ip_path($ip), $mac_path; |
91 |
write_file ip_path($ip,'mac'), $mac; |
write_file ip_path($ip,'mac'), $mac; |
92 |
|
|
93 |
return $ip; |
return $ip; |
111 |
symlink ip_path($ip), $mac_path; |
symlink ip_path($ip), $mac_path; |
112 |
warn "I: upgrade to mac symlink $mac_path\n"; |
warn "I: upgrade to mac symlink $mac_path\n"; |
113 |
} elsif ( -l $mac_path ) { |
} elsif ( -l $mac_path ) { |
114 |
$ip = readlink $mac_path; |
$ip = conf_value $mac_path; |
|
$ip =~ s{^.+/([^/]+)$}{$1}; |
|
115 |
} else { |
} else { |
116 |
die "$mac_path not file or symlink"; |
die "$mac_path not file or symlink"; |
117 |
} |
} |
121 |
|
|
122 |
sub mac_from_ip($) { |
sub mac_from_ip($) { |
123 |
my $ip = shift; |
my $ip = shift; |
124 |
return read_file ip_path($ip, 'mac'); |
conf_value ip_path($ip, 'mac'); |
125 |
} |
} |
126 |
|
|
127 |
sub change_ip($$) { |
sub change_ip($$) { |