/[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

Diff of /mdap-server.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 19 by dpavlin, Mon Apr 23 19:06:18 2007 UTC revision 31 by dpavlin, Tue Apr 24 15:44:20 2007 UTC
# Line 11  my $PORT  = '3235'; Line 11  my $PORT  = '3235';
11  my $debug = 0;  my $debug = 0;
12  my $quiet = 1;  my $quiet = 1;
13  my $verbose = 0;  my $verbose = 0;
14    my $credentials = 0;
15    
16  my $resend_search_delay = 3;  my $resend_search_delay = 3;
17  my $tftp_path = '/srv/tftp/';  my $tftp_path = '/srv/tftp/';
# Line 23  GetOptions( Line 24  GetOptions(
24          "verbose!"      => \$verbose,          "verbose!"      => \$verbose,
25          "search=i"      => \$resend_search_delay,          "search=i"      => \$resend_search_delay,
26          "tftp=s"        => \$tftp_path,          "tftp=s"        => \$tftp_path,
27            "credentials"   => \$credentials,
28  );  );
29    
30  $quiet = 0 if $verbose;  $quiet = 0 if $verbose;
# Line 38  if (-e $passwd_path) { Line 40  if (-e $passwd_path) {
40          while(<$fh>) {          while(<$fh>) {
41                  chomp;                  chomp;
42                  next if /^#/ || /^$/ || /^\s+$/;                  next if /^#/ || /^$/ || /^\s+$/;
43                  if (/^\S+\t\S+$/) {                  if (/^\S+\s\S+$/) {
44                          push @try_accounts, $_;                          push @try_accounts, $_;
45                  } else {                  } else {
46                          warn "invalid $passwd_path entry: $_\n";                          warn "invalid $passwd_path entry: $_\n";
# Line 103  sub mdap_send { Line 105  sub mdap_send {
105          }          }
106  }  }
107    
108  my $ant_passwd_try;  my $ant_passwd;
109    my $ant_unknown_password;
110    my $ant_ok_password;
111    my $ant_flashing;
112    
113  sub ant_credentials {  sub ant_credentials {
114          my $ant = shift || die "no ant?";          my $ant = shift || die "no ant?";
115          my $i = $ant_passwd_try->{$ant} || 0;          my $i = $ant_passwd->{$ant} || 0;
116          my ($user_id,$user_pwd) = split(/\t/, $try_accounts[$i]);          my ($user_id,$user_pwd) = split(/\t/, $try_accounts[$i]);
117          #warn "ant $ant as [$i] $user_id / $user_pwd\n";          #warn "ant $ant as [$i] $user_id / $user_pwd\n";
118          return ($user_id,$user_pwd);          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 {  sub ant_another_passwd {
130          my $ant = shift || die "no ant?";          my $ant = shift || die "no ant?";
131          $ant_passwd_try->{$ant}++;  
132          $ant_passwd_try->{$ant} = 0 if ( $ant_passwd_try->{$ant} > $#try_accounts );          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 {  local $SIG{ALRM} = sub {
# Line 150  while (1) { Line 199  while (1) {
199                  next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );                  next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );
200    
201                  my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );                  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 );                  my ($user_id,$user_pwd) = ant_credentials( $ant );
206    
207                  if ($type eq 'REPLY-ANT-SEARCH') {                  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");                          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') {                  } elsif ($type eq 'REPLY-INFO') {
210    
211                          if ($h->{'SEQ-NR'} < 0) {                          if ( $seq_nr < 0 ) {
212                                  warn "!! password protected ant $ant, skipping\n";  #                               if ( $ant_ok_password ) {
213                                  ant_another_passwd( $ant );  #                                       $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;                                  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?";                          my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?";
# Line 168  while (1) { Line 227  while (1) {
227                                  my $build = $h->{'_BUILD'} || die "no _BUILD?";                                  my $build = $h->{'_BUILD'} || die "no _BUILD?";
228                                  my $new_build = fw_build( $board );                                  my $new_build = fw_build( $board );
229                                  if ( $build ne $new_build ) {                                  if ( $build ne $new_build ) {
230                                          print "UPDATE STEP 1 on ant $ant version $build -> $new_build\n";                                          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");                                          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 {                                  } else {
233                                          print "OK ant $ant allready updated...\n";                                          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 {                          } else {
253                                  print "!! NO FIRMWARE for $board in $tftp_path for ant $ant, skipping update\n";                                  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 ) {                  } elsif ( $type eq 'REPLY-EXEC-CLI' && $h->{'SEQ-NR'} == 1 ) {
256                                  print "UPDATE STEP 2 on ant $ant\n";                                  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");                                  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 {                  } else {
261                          print "!! reply ignored ", dump( $h ), $/;                          print "!! reply ignored ", dump( $h ), $/;
262                  }                  }

Legend:
Removed from v.19  
changed lines
  Added in v.31

  ViewVC Help
Powered by ViewVC 1.1.26