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 |
|
|
10 |
|
use server; |
11 |
|
use format; |
12 |
|
|
13 |
|
our $debug = $server::debug; |
14 |
|
|
15 |
|
sub mkbasedir { |
16 |
|
my $path = shift; |
17 |
|
$path =~ s{(^.*)/[^/]+$}{$1}; |
18 |
|
mkdir $path unless -d $path; |
19 |
|
return $path; |
20 |
|
} |
21 |
|
|
22 |
sub mac_path { $server::conf . '/mac/' . $_[0] } |
sub mac_path { $server::conf . '/mac/' . $_[0] } |
23 |
sub ip_path { $server::conf . '/ip/' . join('/', @_) } |
sub ip_path { $server::conf . '/ip/' . join('/', @_) } |
24 |
|
sub conf_value { |
25 |
|
my $path = shift; |
26 |
|
my $value; |
27 |
|
if ( -l $path ) { |
28 |
|
$value = readlink $path; |
29 |
|
$value =~ s{.*/([^/]+)$}{$1}; |
30 |
|
} elsif ( -f $path ) { |
31 |
|
$value = read_file $path; |
32 |
|
} else { |
33 |
|
warn "W: $path not file or symlink\n"; |
34 |
|
} |
35 |
|
return $value; |
36 |
|
} |
37 |
|
|
38 |
sub conf { |
sub conf { |
39 |
my $ip = shift; |
my $ip = shift; |
50 |
$path .= '/' . $name; |
$path .= '/' . $name; |
51 |
|
|
52 |
if ( defined $value ) { |
if ( defined $value ) { |
53 |
|
mkbasedir $path; |
54 |
write_file $path, $value; |
write_file $path, $value; |
55 |
warn "update $path = $value"; |
warn "update $path = $value"; |
56 |
} elsif ( ! -e $path && defined $default ) { |
} elsif ( ! -e $path && defined $default ) { |
57 |
|
mkbasedir $path; |
58 |
write_file $path, $default; |
write_file $path, $default; |
59 |
warn "default $path = $default"; |
warn "default $path = $default"; |
60 |
$value = $default; |
$value = $default; |
61 |
} elsif ( -e $path ) { |
} elsif ( -f $path ) { |
62 |
if ( -l $path ) { |
$value = read_file $path; |
63 |
$value = readlink $path; |
} else { |
64 |
$value =~ s{.*/([^/]+)$}{$1}; |
warn "# $name missing $path\n" if $debug; |
|
} else { |
|
|
$value = read_file $path; |
|
|
} |
|
65 |
} |
} |
66 |
return $value; |
return $value; |
67 |
} |
} |
68 |
|
|
|
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); |
|
|
} |
|
|
|
|
69 |
sub next_ip($) { |
sub next_ip($) { |
70 |
my $mac = shift; |
my $mac = shift; |
71 |
|
|
84 |
|
|
85 |
warn "next_ip $ip\n"; |
warn "next_ip $ip\n"; |
86 |
|
|
87 |
mkdir ip_path($ip); |
save_ip_mac( $ip, $mac ); |
|
|
|
|
symlink ip_path($ip), mac_path($mac); |
|
|
write_file ip_path($ip,'mac'), $mac; |
|
88 |
|
|
89 |
return $ip; |
return $ip; |
90 |
|
} |
91 |
|
|
92 |
|
sub save_ip_mac { |
93 |
|
my ($ip,$mac) = @_; |
94 |
|
|
95 |
|
mkdir ip_path($ip); |
96 |
|
|
97 |
|
my $mac_path = mac_path($mac); |
98 |
|
unlink $mac_path if -e $mac_path; # XXX audit? |
99 |
|
symlink ip_path($ip), $mac_path; |
100 |
|
write_file ip_path($ip,'mac'), $mac; |
101 |
} |
} |
102 |
|
|
103 |
sub ip_from_mac($) { |
sub ip_from_mac($) { |
117 |
symlink ip_path($ip), $mac_path; |
symlink ip_path($ip), $mac_path; |
118 |
warn "I: upgrade to mac symlink $mac_path\n"; |
warn "I: upgrade to mac symlink $mac_path\n"; |
119 |
} elsif ( -l $mac_path ) { |
} elsif ( -l $mac_path ) { |
120 |
$ip = readlink $mac_path; |
$ip = conf_value $mac_path; |
|
$ip =~ s{^.+/([^/]+)$}{$1}; |
|
121 |
} else { |
} else { |
122 |
die "$mac_path not file or symlink"; |
die "$mac_path not file or symlink"; |
123 |
} |
} |
127 |
|
|
128 |
sub mac_from_ip($) { |
sub mac_from_ip($) { |
129 |
my $ip = shift; |
my $ip = shift; |
130 |
return read_file ip_path($ip, 'mac'); |
conf_value ip_path($ip, 'mac'); |
131 |
} |
} |
132 |
|
|
133 |
sub change_ip($$) { |
sub change_ip($$) { |