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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26