/[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 17 - (show annotations)
Mon Apr 23 17:42:42 2007 UTC (16 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 4517 byte(s)
support list of login\tpasswd, optionally from passwd file and try each one
in turn until correct one is found
1 #!/usr/bin/perl
2
3 use strict;
4 use IO::Socket::Multicast;
5 use Data::Dump qw/dump/;
6
7 use constant GROUP => '224.0.0.103';
8 use constant PORT => '3235';
9
10 my $debug = shift @ARGV;
11
12 my $resend_search_delay = 3;
13 my $tftp_path = '/srv/tftp/';
14
15 # tab-delimited list of user id/passwd to try on ants
16 my @try_accounts = ( "Administrator\t" );
17
18 my $passwd_path = $0;
19 $passwd_path =~ s/[^\/]+$/passwd/;
20
21 if (-e $passwd_path) {
22 open(my $fh, $passwd_path) || die "can't open $passwd_path: $!";
23 while(<$fh>) {
24 chomp;
25 next if /^#/ || /^$/ || /^\s+$/;
26 if (/^\S+\t\S+$/) {
27 push @try_accounts, $_;
28 } else {
29 warn "invalid $passwd_path entry: $_\n";
30 }
31 }
32 print "found ", $#try_accounts, " accounts to try on password protected ants\n";
33 }
34
35 warn "search for ants every ${resend_search_delay}s\ntftp server path: $tftp_path\n";
36
37 sub fw {
38 my ($board, $offset,$len) = @_;
39 open(my $fh, "$tftp_path/$board") || die "Can't open image $tftp_path/$board: $!";
40 my $b;
41 seek($fh, $offset, 0) || die "can't seek to $offset: $!";
42 read($fh, $b, $len) || die "can't read $len bytes from $offset: $!";
43 close($fh);
44 return $b;
45 }
46
47 sub fw_build {
48 my $board_name = shift || return 0;
49 my $v = join('.', unpack('CCCC',fw($board_name,0x20,4)) );
50 print "# fw_build $board_name $v\n";
51 return $v;
52 }
53
54 sub fw_exists {
55 my $board = shift;
56 return -e "$tftp_path/$board";
57 }
58
59 my $sock = IO::Socket::Multicast->new(LocalPort=>PORT,ReuseAddr=>1);
60 $sock->mcast_add(GROUP) || die "Couldn't set group: $!\n";
61 $sock->mcast_ttl(1);
62
63 sub ant2hash {
64 my $data = shift;
65 my $hash;
66 map {
67 if ( m/:/ ) {
68 my ($n,$v) = split(/:/,$_,2);
69 $hash->{$n} = $v;
70 }
71 } split(/[\n\r]/, $data);
72 return $hash;
73 }
74
75 sub mdap_send {
76 my $data = shift;
77
78 my $xor = 0;
79 map { $xor ^= ord($_) } split(//,$data);
80 $data .= sprintf('%02X', $xor);
81
82 $sock->mcast_send( $data, GROUP . ':' . PORT );
83 if ($debug) {
84 warn ">> ", dump( $data ), $/;
85 } else {
86 $data =~ s/\s+/ /gi;
87 warn ">> ", substr($data,0,70), $/;
88 }
89 }
90
91 my $ant_passwd_try;
92
93 sub ant_credentials {
94 my $ant = shift || die "no ant?";
95 my $i = $ant_passwd_try->{$ant} || 0;
96 my ($user_id,$user_pwd) = split(/\t/, $try_accounts[$i]);
97 #warn "ant $ant as [$i] $user_id / $user_pwd\n";
98 return ($user_id,$user_pwd);
99 }
100
101 sub ant_another_passwd {
102 my $ant = shift || die "no ant?";
103 $ant_passwd_try->{$ant}++;
104 $ant_passwd_try->{$ant} = 0 if ( $ant_passwd_try->{$ant} > $#try_accounts );
105 }
106
107 local $SIG{ALRM} = sub {
108 mdap_send("ANT-SEARCH MDAP/1.1\r\n");
109 alarm( $resend_search_delay );
110 };
111
112 alarm( $resend_search_delay );
113
114 mdap_send("ANT-SEARCH MDAP/1.1\r\n");
115
116 while (1) {
117 my $data;
118 next unless $sock->recv($data,1024);
119
120 if ( $data =~ m#^(INFO|ANT-SEARCH|EXEC-CLI|REPLY-\S+)\s(MDAP)/(\d+\.\d+)# ) {
121
122 my ($type,$proto,$mdap_ver) = ($1,$2,$3);
123
124 my $h = ant2hash($data);
125
126 my $client_version = $h->{'MDAP-VERSION'};
127 $mdap_ver = $client_version if ($client_version);
128
129 print "<< $type $proto/$mdap_ver << ", length($data), " bytes\n";
130
131 warn dump($h),$/ if ($debug);
132
133 # we are getting our own messages (since our source port
134 # is same as destination)
135 next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );
136
137 my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );
138
139 my ($user_id,$user_pwd) = ant_credentials( $ant );
140
141 if ($type eq 'REPLY-ANT-SEARCH') {
142 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");
143 } elsif ($type eq 'REPLY-INFO') {
144
145 if ($h->{'SEQ-NR'} < 0) {
146 warn "!! password protected ant $ant, skipping\n";
147 ant_another_passwd( $ant );
148 next;
149 }
150
151 my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?";
152 if ( fw_exists( $board ) ) {
153 my $build = $h->{'_BUILD'} || die "no _BUILD?";
154 my $new_build = fw_build( $board );
155 if ( $build ne $new_build ) {
156 print "UPDATE STEP 1 on ant $ant version $build -> $new_build\n";
157 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");
158 } else {
159 print "OK ant $ant allready updated...\n";
160 }
161 } else {
162 print "!! NO FIRMWARE for $board in $tftp_path for ant $ant, skipping update\n";
163 }
164 } elsif ( $type eq 'REPLY-EXEC-CLI' && $h->{'SEQ-NR'} == 1 ) {
165 print "UPDATE STEP 2 on ant $ant\n";
166 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");
167 } else {
168 print "!! reply ignored ", dump( $h ), $/;
169 }
170
171 } else {
172 warn "<=" x 15, "\n", $data, "\n", "<=" x 15, "\n";
173 }
174 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26