package iossb;
##
## @PACKAGE@ @VERSION@
@copyright@
#
#  RANCID - Really Awesome New Cisco confIg Differ
#
#  iossb.pm - Cisco IOS small business rancid procedures

use 5.010;
use strict 'vars';
use warnings;
no warnings 'uninitialized';
require(Exporter);
our @ISA = qw(Exporter);

use rancid @VERSION@;

our $proc;
our $ios;
our $found_version;
our $found_env;
our $found_diag;
our $config_register;			# configuration register value

our $C0;				# output formatting control
our $E0;
our $H0;
our $I0;
our $DO_SHOW_VLAN;

@ISA = qw(Exporter rancid main);
#XXX @Exporter::EXPORT = qw($VERSION @commandtable %commands @commands);

# load-time initialization
sub import {
    0;
}

# post-open(collection file) initialization
sub init {
    $proc = "";
    $ios = "IOS";
    $found_version = 0;
    $found_env = 0;
    $found_diag = 0;
    $config_register = undef;		# configuration register value

    $C0 = 0;				# output formatting control
    $E0 = 0;
    $H0 = 0;
    $I0 = 0;
    $DO_SHOW_VLAN = 0;

    # add content lines and separators
    ProcessHistory("","","","!RANCID-CONTENT-TYPE: $devtype\n!\n");
    ProcessHistory("COMMENTS","keysort","B0","!\n");
    ProcessHistory("COMMENTS","keysort","D0","!\n");
    ProcessHistory("COMMENTS","keysort","F0","!\n");
    ProcessHistory("COMMENTS","keysort","G0","!\n");

    0;
}

# main loop of input of device output
sub inloop {
    my($INPUT, $OUTPUT) = @_;
    my($cmd, $rval);

TOP: while(<$INPUT>) {
	tr/\015//d;
        # note: this match sucks rocks, but currently the cisco-sb bits are
        # unreliable about echoing the '\n' after exit.  this match might
        # really be a bad idea, but instead rely upon WriteTerm's found_end?
CMD:	if (/[>#]\s?exit(Connection( to \S+)? closed)?/ && $found_end) {
	    $clean_run = 1;
	    last;
	}
	if (/^Error:/) {
	    print STDOUT ("$host clogin error: $_");
	    print STDERR ("$host clogin error: $_") if ($debug);
	    $clean_run = 0;
	    last;
	}
	while (/[>#]\s*($cmds_regexp)\s*$/) {
	    $cmd = $1;
	    if (!defined($prompt)) {
		$prompt = ($_ =~ /^([^#>]+[#>])/)[0];
		$prompt =~ s/([][}{)(+\\])/\\$1/g;
		print STDERR ("PROMPT MATCH: $prompt\n") if ($debug);
	    }
	    print STDERR ("HIT COMMAND:$_") if ($debug);
	    if (! defined($commands{$cmd})) {
		print STDERR "$host: found unexpected command - \"$cmd\"\n";
		$clean_run = 0;
		last TOP;
	    }
	    if (! defined(&{$commands{$cmd}})) {
		printf(STDERR "$host: undefined function - \"%s\"\n",
		       $commands{$cmd});
		$clean_run = 0;
		last TOP;
	    }
	    $rval = &{$commands{$cmd}}($INPUT, $OUTPUT, $cmd);
	    delete($commands{$cmd});
	    if ($rval == -1) {
		$clean_run = 0;
		last TOP;
	    }
	    if (defined($prompt)) {
		if (/$prompt/) {
		    goto CMD;
		}
	    }
	}
    }
}

# This routine parses "show system"
sub ShowSystem {
    my($INPUT, $OUTPUT, $cmd) = @_;
    my($slave, $slaveslot);
    print STDERR "    In ShowSystem: $_" if ($debug);

    while (<$INPUT>) {
	tr/\015//d;
	last if (/^$prompt/);
	next if (/^(\s*|\s*$cmd\s*)$/);
	next if (/^\s+\^$/);

	/system description:\s+(.*)$/i &&
	    ProcessHistory("COMMENTS","keysort","B0","! Chassis type: $1\n") &&
	    next;
	# # filter individual unit temperature XXX sg500 unit temps
	# if (/unit\stemperature/i) {
	#     while (<$INPUT>) {
	# 	tr/\015//d;
	# 	return(0) if (/^$prompt/);
	# 	next if (/^(\s*|\s*$cmd\s*)$/);
	# 	next if (/^\s+\^$/);
	#
	# 	last if (/^\s*$/);
	#     }
	# }
    }
    return(0);
}


# This routine parses "show version"
sub ShowVersion {
    my($INPUT, $OUTPUT, $cmd) = @_;
    my($slave, $slaveslot);
    print STDERR "    In ShowVersion: $_" if ($debug);

    while (<$INPUT>) {
	tr/\015//d;
	last if (/^$prompt/);
	next if (/^(\s*|\s*$cmd\s*)$/);
	next if (/^\s+\^$/);

	ProcessHistory("COMMENTS","keysort","E0","! Image: $_");
    }
    return(0);
}


# This routine parses "show running-config"
sub WriteTerm {
    my($INPUT, $OUTPUT, $cmd) = @_;
    my($comment, $linecnt) = (0,0);
    print STDERR "    In WriteTerm: $_" if ($debug);

    while (<$INPUT>) {
	tr/\015//d;
	last if (/^$prompt/);
	next if (/^(\s*|\s*$cmd\s*)$/);
	next if (/^\s+\^$/);

	# skip emtpy lines at the beginning
	if (!$linecnt && /^\s*$/) {
	    next;
	}
	$linecnt++;

	# these appear to be reversables
	# enable password level 15 encrypted z9111212555bd4b0d3bec0b98f7ddd6346
        if (/^(enable )?(password|passwd)( level \d+)? / && $filter_pwds >= 1) {
            ProcessHistory("ENABLE","","","!$1$2$3 <removed>\n");
            next;
        }
	# username cisco password encrypted z9111212555bd4b0d3bec0b98f7ddd6346 privilege 15
        if (/^username (\S+)(\s.*)? password encrypted (\S+)/) {
            if ($filter_pwds >= 1){
                ProcessHistory("USER","keysort","$1",
                               "!username $1$2 password encrypted <removed>$'");
            } else {
                ProcessHistory("USER","keysort","$1","$_");
            }
            next;
        }

	# order/prune snmp-server host statements
	# we only prune lines of the form
	# snmp-server host <ip> traps version 2c <community>
	if (/^(snmp-server host (\d+\.\d+\.\d+\.\d+) traps.* )(\S+)$/) {
            if ($filter_commstr) {
                ProcessHistory("SNMPTRAPHOST","ipsort","$2","! $1<removed>\n");
            } else {
                ProcessHistory("SNMPTRAPHOST","ipsort","$2","$_");
            }
            next;
	}
	# snmp-server community <community> ro <ip> view Default
	if (/^(snmp-server community )\S+ (\w+) (\d+\.\d+\.\d+\.\d+)(.*)$/) {
            if ($filter_commstr) {
                ProcessHistory("SNMPSERVERHOST","ipsort","$3","! $1<removed> $2 $3$4\n");
            } else {
                ProcessHistory("SNMPSERVERHOST","ipsort","$3","$_");
            }
            next;
	}

	# encrypted tacacs-server host <ip> key <keyhash> priority 1
	# encrypted tacacs-server key <keyhash>
	if (/^(encrypted (tacacs|radius)-server\s.*\s?key) (\S+)/
	    && $filter_pwds >= 1) {
	    ProcessHistory("","","","!$1 <removed>$'"); next;
	}

	# catch anything that wasnt matched above.
	ProcessHistory("","","","$_");
	# end of config.
	# XXX no definitive end of config marker.
	#if (/^exit$/) {
	#    $found_end = 1;
	#    return(0);
	#}
    }

    # The ContentEngine lacks a definitive "end of config" marker.  If we
    # know that it is a CE, SAN, or NXOS and we have seen at least 5 lines
    # of write term output, we can be reasonably sure that we have the config.
    if ($linecnt > 5) {
        $found_end = 1;
        return(0);
    }

    return(0);
}

1;
