[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.5

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

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