1 |
#!/usr/bin/perl |
2 |
|
3 |
use strict; |
4 |
use IO::Socket::Multicast; |
5 |
use Data::Dump qw/dump/; |
6 |
use Getopt::Long; |
7 |
|
8 |
my $GROUP = '224.0.0.103'; |
9 |
my $PORT = '3235'; |
10 |
|
11 |
my $debug = 0; |
12 |
my $quiet = 1; |
13 |
my $verbose = 0; |
14 |
|
15 |
my $resend_search_delay = 3; |
16 |
my $tftp_path = '/srv/tftp/'; |
17 |
|
18 |
GetOptions( |
19 |
"port=i" => \$PORT, |
20 |
"group=s" => \$GROUP, |
21 |
"debug!" => \$debug, |
22 |
"quiet!" => \$quiet, |
23 |
"verbose!" => \$verbose, |
24 |
"search=i" => \$resend_search_delay, |
25 |
"tftp=s" => \$tftp_path, |
26 |
); |
27 |
|
28 |
$quiet = 0 if $verbose; |
29 |
|
30 |
# tab-delimited list of user id/passwd to try on ants |
31 |
my @try_accounts = ( "Administrator\t" ); |
32 |
|
33 |
my $passwd_path = $0; |
34 |
$passwd_path =~ s/[^\/]+$/passwd/; |
35 |
|
36 |
if (-e $passwd_path) { |
37 |
open(my $fh, $passwd_path) || die "can't open $passwd_path: $!"; |
38 |
while(<$fh>) { |
39 |
chomp; |
40 |
next if /^#/ || /^$/ || /^\s+$/; |
41 |
if (/^\S+\t\S+$/) { |
42 |
push @try_accounts, $_; |
43 |
} else { |
44 |
warn "invalid $passwd_path entry: $_\n"; |
45 |
} |
46 |
} |
47 |
print "found ", $#try_accounts + 1, " accounts to try on password protected ants\n"; |
48 |
} |
49 |
|
50 |
warn "search for ants every ${resend_search_delay}s\ntftp server path: $tftp_path\n"; |
51 |
|
52 |
sub fw { |
53 |
my ($board, $offset,$len) = @_; |
54 |
open(my $fh, "$tftp_path/$board") || die "Can't open image $tftp_path/$board: $!"; |
55 |
my $b; |
56 |
seek($fh, $offset, 0) || die "can't seek to $offset: $!"; |
57 |
read($fh, $b, $len) || die "can't read $len bytes from $offset: $!"; |
58 |
close($fh); |
59 |
return $b; |
60 |
} |
61 |
|
62 |
sub fw_build { |
63 |
my $board_name = shift || return 0; |
64 |
my $v = join('.', unpack('CCCC',fw($board_name,0x20,4)) ); |
65 |
print "# fw_build $board_name $v\n" unless $quiet; |
66 |
return $v; |
67 |
} |
68 |
|
69 |
sub fw_exists { |
70 |
my $board = shift; |
71 |
return -e "$tftp_path/$board"; |
72 |
} |
73 |
|
74 |
my $sock = IO::Socket::Multicast->new(LocalPort=>$PORT,ReuseAddr=>1); |
75 |
$sock->mcast_add($GROUP) || die "Couldn't set group: $!\n"; |
76 |
$sock->mcast_ttl(1); |
77 |
|
78 |
sub ant2hash { |
79 |
my $data = shift; |
80 |
my $hash; |
81 |
map { |
82 |
if ( m/:/ ) { |
83 |
my ($n,$v) = split(/:/,$_,2); |
84 |
$hash->{$n} = $v; |
85 |
} |
86 |
} split(/[\n\r]/, $data); |
87 |
return $hash; |
88 |
} |
89 |
|
90 |
sub mdap_send { |
91 |
my $data = shift; |
92 |
|
93 |
my $xor = 0; |
94 |
map { $xor ^= ord($_) } split(//,$data); |
95 |
$data .= sprintf('%02X', $xor); |
96 |
|
97 |
$sock->mcast_send( $data, "${GROUP}:${PORT}" ); |
98 |
if ($debug) { |
99 |
warn ">> ", dump( $data ), $/; |
100 |
} elsif( ! $quiet ) { |
101 |
$data =~ s/\s+/ /gi; |
102 |
warn ">> ", substr($data,0,70), $/; |
103 |
} |
104 |
} |
105 |
|
106 |
my $ant_passwd; |
107 |
|
108 |
sub ant_credentials { |
109 |
my $ant = shift || die "no ant?"; |
110 |
my $i = $ant_passwd->{$ant} || 0; |
111 |
my ($user_id,$user_pwd) = split(/\t/, $try_accounts[$i]); |
112 |
#warn "ant $ant as [$i] $user_id / $user_pwd\n"; |
113 |
return ($user_id,$user_pwd); |
114 |
} |
115 |
|
116 |
my $ant_unknown_password; |
117 |
my $ant_ok_password; |
118 |
|
119 |
sub ant_unknown_password { |
120 |
my $ant = shift || die "no ant?"; |
121 |
if ( $ant_unknown_password->{$ant} ) { |
122 |
$ant_unknown_password->{$ant}--; |
123 |
} |
124 |
return $ant_unknown_password->{$ant}; |
125 |
} |
126 |
|
127 |
sub ant_another_passwd { |
128 |
my $ant = shift || die "no ant?"; |
129 |
|
130 |
return 0 if ant_unknown_password( $ant ); |
131 |
|
132 |
$ant_passwd->{$ant}++; |
133 |
|
134 |
if ( $ant_passwd->{$ant} > $#try_accounts ) { |
135 |
print "$ant ant with unknown password\n"; |
136 |
$ant_unknown_password->{$ant} = 10; |
137 |
$ant_passwd->{$ant} = 0; |
138 |
return 0; |
139 |
} |
140 |
return 1; |
141 |
} |
142 |
|
143 |
sub forget_ant { |
144 |
my $ant = shift || die "no ant?"; |
145 |
delete $ant_unknown_password->{$ant}; |
146 |
delete $ant_passwd->{$ant}; |
147 |
delete $ant_ok_password->{$ant}; |
148 |
} |
149 |
|
150 |
my $once; |
151 |
|
152 |
sub once { |
153 |
my $m = join('', @_); |
154 |
$once->{$m}++; |
155 |
print $m if ($once->{$m} == 1); |
156 |
} |
157 |
|
158 |
local $SIG{ALRM} = sub { |
159 |
mdap_send("ANT-SEARCH MDAP/1.1\r\n"); |
160 |
alarm( $resend_search_delay ); |
161 |
}; |
162 |
|
163 |
alarm( $resend_search_delay ); |
164 |
|
165 |
mdap_send("ANT-SEARCH MDAP/1.1\r\n"); |
166 |
|
167 |
while (1) { |
168 |
my $data; |
169 |
next unless $sock->recv($data,1024); |
170 |
|
171 |
if ( $data =~ m#^(INFO|ANT-SEARCH|EXEC-CLI|REPLY-\S+)\s(MDAP)/(\d+\.\d+)# ) { |
172 |
|
173 |
my ($type,$proto,$mdap_ver) = ($1,$2,$3); |
174 |
|
175 |
my $h = ant2hash($data); |
176 |
|
177 |
my $client_version = $h->{'MDAP-VERSION'}; |
178 |
$mdap_ver = $client_version if ($client_version); |
179 |
|
180 |
print "<< $type $proto/$mdap_ver << ", length($data), " bytes\n" unless $quiet; |
181 |
|
182 |
warn dump($h),$/ if ($debug); |
183 |
|
184 |
# we are getting our own messages (since our source port |
185 |
# is same as destination) |
186 |
next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# ); |
187 |
|
188 |
my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h ); |
189 |
my $seq_nr = $h->{'SEQ-NR'}; |
190 |
#warn "SEQ-NR: $seq_nr ok: ",$ant_ok_password->{$ant},"\n" if ($seq_nr); |
191 |
|
192 |
my ($user_id,$user_pwd) = ant_credentials( $ant ); |
193 |
|
194 |
if ($type eq 'REPLY-ANT-SEARCH') { |
195 |
mdap_send("INFO MDAP/$mdap_ver\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n") unless ant_unknown_password( $ant ); |
196 |
} elsif ($type eq 'REPLY-INFO') { |
197 |
|
198 |
if ( $seq_nr < 0 ) { |
199 |
# if ( $ant_ok_password ) { |
200 |
# $ant_ok_password->{$ant} = 0; |
201 |
# } elsif ( ant_another_passwd( $ant ) ) { |
202 |
if ( ant_another_passwd( $ant ) ) { |
203 |
($user_id,$user_pwd) = ant_credentials( $ant ); |
204 |
mdap_send("INFO MDAP/$mdap_ver\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n"); |
205 |
} |
206 |
next; |
207 |
} else { |
208 |
$ant_ok_password->{$ant}++; |
209 |
} |
210 |
|
211 |
my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?"; |
212 |
if ( fw_exists( $board ) ) { |
213 |
my $build = $h->{'_BUILD'} || die "no _BUILD?"; |
214 |
my $new_build = fw_build( $board ); |
215 |
if ( $build ne $new_build ) { |
216 |
print "+ $ant version $build -> $new_build\n"; |
217 |
mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:software upgrade\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n"); |
218 |
} else { |
219 |
once "$ant OK version $build", |
220 |
$ant_unknown_password->{$ant} ? ' with unknown password' : |
221 |
$ant_ok_password->{$ant} ? ' password protected' : |
222 |
'', |
223 |
"\n"; |
224 |
} |
225 |
} else { |
226 |
once "!! NO FIRMWARE for $board in $tftp_path for ant $ant, skipping update\n"; |
227 |
} |
228 |
} elsif ( $type eq 'REPLY-EXEC-CLI' && $h->{'SEQ-NR'} == 1 ) { |
229 |
print "+ $ant bootp mode re-flash started\n"; |
230 |
mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nSEQ-NR:2\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n"); |
231 |
forget_ant( $ant ); |
232 |
} else { |
233 |
print "!! reply ignored ", dump( $h ), $/; |
234 |
} |
235 |
|
236 |
} else { |
237 |
warn "<=" x 15, "\n", $data, "\n", "<=" x 15, "\n"; |
238 |
} |
239 |
} |