[BACK]Return to su.cgi CVS log [TXT][DIR] Up to [local] / trango / Net-Telnet-Trango / scripts

Annotation of trango/Net-Telnet-Trango/scripts/su.cgi, Revision 1.3

1.1       andrew      1: #!/usr/bin/perl
1.3     ! andrew      2: # $RedRiver: su.cgi,v 1.2 2007/02/07 17:42:56 andrew Exp $
1.1       andrew      3: ########################################################################
                      4: # su.cgi *** a CGI for Trango SU utilities.
                      5: #
                      6: # 2007.02.07 #*#*# andrew fresh <andrew@mad-techies.org>
                      7: ########################################################################
                      8: # Copyright (C) 2007 by Andrew Fresh
                      9: #
                     10: # This program is free software; you can redistribute it and/or modify
                     11: # it under the same terms as Perl itself.
                     12: ########################################################################
                     13: use strict;
                     14: use warnings;
                     15:
1.2       andrew     16: my $host_file = 'su.yaml';
1.1       andrew     17:
                     18: my $default_mac  = '0001DE';
                     19: my $default_suid = 'all';
                     20: my $default_cir  = 256;
                     21: my $default_mir  = 9999;
                     22: my $Start_SUID = 3;
                     23:
                     24: use CGI qw/:standard/;
1.2       andrew     25: use File::Basename;
1.3     ! andrew     26: use YAML qw/ LoadFile /;
1.1       andrew     27: use Net::Telnet::Trango;
                     28:
1.2       andrew     29: my $me = basename($0);
1.1       andrew     30:
1.2       andrew     31: my $aps = get_aps($host_file);
1.1       andrew     32:
                     33: print header,
                     34:       start_html('Trango SU Utilities'),
                     35:       h1('Trango SU Utilities');
                     36:
                     37: if (param()) {
                     38:
1.3     ! andrew     39:     my $AP = param('AP');
1.1       andrew     40:
1.3     ! andrew     41:     unless (exists $aps->{$AP}) {
        !            42:         print h3("AP '$AP' does not exist!");
        !            43:         print end_html;
        !            44:         exit;
        !            45:     }
        !            46:
        !            47:     my $sumac = param('sumac');
        !            48:
        !            49:     $sumac =~ s/[^0-9A-Fa-f]//g;
        !            50:     $sumac = uc($sumac);
        !            51:
        !            52:     my $suid = param('suid');
        !            53:
        !            54:     if (length $sumac == 12) {
        !            55:         add_su($aps->{$AP}, $sumac);
        !            56:     } elsif (length $suid) {
        !            57:         testrflink($aps->{$AP}, $suid);
        !            58:     } else {
        !            59:         print h3("Invalid SUID '$suid' and MAC '$sumac'");
        !            60:         show_form($aps, $default_mac);
        !            61:     }
1.1       andrew     62:
                     63: } else {
1.3     ! andrew     64:     show_form($aps, $default_mac);
1.1       andrew     65: }
                     66:
                     67:
                     68: print end_html;
                     69:
                     70:
                     71: sub get_aps
                     72: {
1.3     ! andrew     73:     my $file = shift;
1.1       andrew     74:
1.3     ! andrew     75:     my $conf = LoadFile($file);
1.1       andrew     76:
1.3     ! andrew     77:     my %aps;
1.1       andrew     78:
1.3     ! andrew     79:     foreach my $ap (keys %{ $conf }) {
        !            80:         next if $ap eq 'default';
        !            81:         $aps{ $ap } = $conf->{$ap};
        !            82:         if (ref $conf->{default} eq 'HASH') {
        !            83:             foreach my $k (keys %{ $conf->{default} }) {
        !            84:                 $aps{ $ap }{$k} ||= $conf->{default}->{$k};
        !            85:             }
        !            86:         }
        !            87:     }
        !            88:
        !            89:     return \%aps;
        !            90:
        !            91:     return {
        !            92:         'rrlhcwap0000' => {
        !            93:             name     => '192.168.1.1',
        !            94:             password => 'trango',
        !            95:         }
        !            96:     };
1.1       andrew     97:
                     98: }
                     99:
                    100: sub show_form
                    101: {
1.3     ! andrew    102:     my $aps  = shift;
1.1       andrew    103:
1.3     ! andrew    104:     my %cache = ();
        !           105:     my @ap_names = sort {
        !           106:         my @a = $a =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
        !           107:         my @b = $b =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
        !           108:
        !           109:         if (@a) {
        !           110:             $cache{$a} ||= pack('C4' => @a);
        !           111:         } else {
        !           112:             $cache{$a} ||= lc($a);
        !           113:         }
        !           114:         if (@b) {
        !           115:             $cache{$b} ||= pack('C4' => @b);
        !           116:         } else {
        !           117:             $cache{$b} ||= lc($b);
        !           118:         }
1.1       andrew    119:
1.3     ! andrew    120:         $cache{$a} cmp $cache{$b};
        !           121:     } keys %{ $aps };
1.1       andrew    122:
                    123:     print p(start_form(-method => 'GET'),
1.3     ! andrew    124:         'AP:    ', popup_menu(-name=>'AP',    -values=>\@ap_names),br,
        !           125:         'SUMAC: ', textfield( -name=>'sumac', -default=>$default_mac),br,
        !           126:         'SUID:  ', textfield( -name=>'suid',  -default=>$default_suid),br,
1.1       andrew    127:         submit,
                    128:         end_form);
                    129:
1.3     ! andrew    130:     print p('Fill in the SUMAC if you wish to add an SU ',
        !           131:       'or fill in the SUID to run an rflinktest.');
1.1       andrew    132:
1.3     ! andrew    133:     return 1;
1.1       andrew    134: }
                    135:
                    136: sub login
                    137: {
1.3     ! andrew    138:     my $host     = shift;
        !           139:     my $password = shift;
1.1       andrew    140:
1.3     ! andrew    141:     my $t = new Net::Telnet::Trango ( Timeout => 5 );
1.1       andrew    142:
1.3     ! andrew    143:     #$t->input_log('/tmp/telnet_log');
        !           144:     #$t->dump_log('/tmp/telnet_log');
1.1       andrew    145:
1.3     ! andrew    146:     unless ($t->open( Host => $host )) {
        !           147:         print h3("Error connecting!");
        !           148:         $t->close;
        !           149:         return undef;
        !           150:     }
        !           151:
        !           152:     unless ($t->login( $password ) ) {
        !           153:         print h3("Couldn't log in: $!");
        !           154:         $t->exit;
        !           155:         $t->close;
        !           156:         return undef;
        !           157:     }
1.1       andrew    158:
1.3     ! andrew    159:     return $t;
1.1       andrew    160: }
                    161:
                    162: sub add_su
                    163: {
1.3     ! andrew    164:     my $ap  = shift;
        !           165:     my $sumac = shift;
1.1       andrew    166:
1.3     ! andrew    167:     my $t = login($ap->{name}, $ap->{password});
1.1       andrew    168:
1.3     ! andrew    169:     my $cur_sus = $t->sudb_view;
1.1       andrew    170:
1.3     ! andrew    171:     my $new_suid = next_suid($cur_sus);
1.1       andrew    172:
1.3     ! andrew    173:     foreach my $su (@{ $cur_sus }) {
        !           174:         if ($sumac eq $su->{mac}) {
        !           175:             print h3("MAC '$sumac' already in AP '$ap->{name}' " .
        !           176:               "with SUID '$su->{suid}'");
        !           177:             $t->exit;
        !           178:             $t->close;
        !           179:             return undef;
        !           180:         }
        !           181:     }
        !           182:
        !           183:     unless ($t->sudb_add(
        !           184:         $new_suid, 'reg', $default_cir, $default_mir, $sumac
        !           185:     ) ) {
        !           186:         print h3("Error adding SU!");
        !           187:         $t->exit;
        !           188:         $t->close;
        !           189:         return undef;
        !           190:     }
        !           191:
        !           192:     my $new_sus = $t->sudb_view;
        !           193:     my $added = 0;
        !           194:     foreach my $su (@{ $new_sus }) {
        !           195:         if ($su->{suid} == $new_suid) {
        !           196:             $added = 1;
        !           197:             last;
        !           198:         }
        !           199:     }
        !           200:
        !           201:     unless ($added) {
        !           202:         print h3("Couldn't add su id: $new_suid");
        !           203:         $t->exit;
        !           204:         $t->close;
        !           205:         return undef;
        !           206:     }
        !           207:
        !           208:     unless ($t->save_sudb) {
        !           209:         print h3("Couldn't save sudb");
        !           210:         $t->exit;
        !           211:         $t->close;
        !           212:         return undef;
        !           213:     }
        !           214:
        !           215:     print p(
        !           216:         "Added new SU with ID '$new_suid' " .
        !           217:         "and MAC '$sumac' " .
        !           218:         "to '$ap->{name}'.  " .
        !           219:         '<a href="' . $me . '?' .
        !           220:         'AP=' . $ap->{name} . '&' .
        !           221:         'suid=' . $new_suid .
        !           222:         '">Test SU RFLink</a>'
        !           223:     );
        !           224:
        !           225:     $t->exit;
        !           226:     $t->close;
        !           227:     return 1;
1.1       andrew    228:
                    229: }
                    230:
                    231: sub testrflink
                    232: {
1.3     ! andrew    233:     my $ap  = shift;
        !           234:     my $suid = shift;
1.1       andrew    235:
1.3     ! andrew    236:     my $t = login($ap->{name}, $ap->{password});
1.1       andrew    237:
1.3     ! andrew    238:     my $result = $t->su_testrflink( $suid );
1.1       andrew    239:
1.3     ! andrew    240:     unless ($result) {
        !           241:         print h3("Error testing SU rflink!");
        !           242:         $t->exit;
        !           243:         $t->close;
        !           244:         return undef;
        !           245:     }
        !           246:
        !           247:     my @keys = ('suid', 'AP Tx', 'AP Rx', 'SU Rx');
        !           248:
        !           249:     my @table;
        !           250:     foreach my $su (@{ $result }) {
        !           251:         next unless ref $su eq 'HASH';
        !           252:         next unless exists $su->{suid};
        !           253:         $su->{suid} =~ s/\D//g;
        !           254:         next unless $su->{suid};
        !           255:
        !           256:         push @table, td([ @{ $su }{ @keys } ]);
        !           257:     }
        !           258:
        !           259:     print table({-border=>1,-cellspacing=>0,-cellpadding=>1},
        !           260:         caption($ap->{name} . ': su testrflink ' . $suid),
        !           261:         Tr({-align=>'CENTER', -valign=>'TOP'},
        !           262:             [ th(\@keys), @table ]
        !           263:         )
        !           264:     );
        !           265:
        !           266:     $t->exit;
        !           267:     $t->close;
        !           268:     return 1;
1.1       andrew    269:
                    270: }
                    271:
                    272: sub next_suid
                    273: {
1.3     ! andrew    274:     my $sudb = shift;
1.1       andrew    275:
1.3     ! andrew    276:     my $next_id = $Start_SUID;
1.1       andrew    277:
1.3     ! andrew    278:     my %ids = map { $_->{suid} => 1 } @{ $sudb };
1.1       andrew    279:
1.3     ! andrew    280:     my $next_key = sprintf('%04d', $next_id);
        !           281:     while (exists $ids{$next_key}) {
        !           282:         $next_id++;
        !           283:         $next_key = sprintf('%04d', $next_id);
        !           284:     }
1.1       andrew    285:
1.3     ! andrew    286:     return $next_id;
1.1       andrew    287: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>