/[mdap]/mdap-server.pl
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 /mdap-server.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26