#! /usr/bin/perl
##
## $Id: srancid.in 3741 2017-12-06 20:10:48Z heas $
##
## rancid 3.8
## Copyright (c) 1997-2018 by Henry Kilmer and John Heasley
## All rights reserved.
##
## This code is derived from software contributed to and maintained by
## Henry Kilmer, John Heasley, Andrew Partan,
## Pete Whiting, Austin Schutz, and Andrew Fort.
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions
## are met:
## 1. Redistributions of source code must retain the above copyright
##    notice, this list of conditions and the following disclaimer.
## 2. Redistributions in binary form must reproduce the above copyright
##    notice, this list of conditions and the following disclaimer in the
##    documentation and/or other materials provided with the distribution.
## 3. Neither the name of RANCID nor the names of its
##    contributors may be used to endorse or promote products derived from
##    this software without specific prior written permission.
##
## THIS SOFTWARE IS PROVIDED BY Henry Kilmer, John Heasley AND CONTRIBUTORS
## ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
## PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COMPANY OR CONTRIBUTORS
## BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
## POSSIBILITY OF SUCH DAMAGE.
##
## It is the request of the authors, but not a condition of license, that
## parties packaging or redistributing RANCID NOT distribute altered versions
## of the etc/rancid.types.base file nor alter how this file is processed nor
## when in relation to etc/rancid.types.conf.  The goal of this is to help
## suppress our support costs.  If it becomes a problem, this could become a
## condition of license.
# 
#  The expect login scripts were based on Erik Sherk's gwtn, by permission.
# 
#  The original looking glass software was written by Ed Kern, provided by
#  permission and modified beyond recognition.
#
# Pretty huge hack to take care of Dell (aka. SMC) Switch configs; started by
# d_pfleger@juniper.net
#
#  RANCID - Really Awesome New Cisco confIg Differ
#
# usage: srancid [-dltCV] [-f filename | hostname]
#
# Code tested and working fine on these models:
#
#	DELL PowerConnect M8024 / M8024-k
#	DELL PowerConnect M6348
#	DELL PowerConnect N2048, N4032F and N4064.
#	DELL PowerConnect 62xx
#	DELL PowerConnect 7048
#	DELL 34xx (partially; configuration is incomplete)
#	DELL R1-2401
#
use 5.010;
use strict 'vars';
use warnings;
no warnings 'uninitialized';
use Exporter;
use Getopt::Std;
our($opt_d, $opt_f, $opt_h, $opt_l, $opt_t, $opt_C, $opt_V);
$opt_t = "smc";
getopts('dflt:CV');
BEGIN {
    push(@INC, "/usr/local/lib/rancid");
}
use rancid;
our @ISA = qw(Exporter rancid);

if ($opt_V) {
    print "rancid 3.8\n";
    exit(0);
}
$log = $opt_l;
$debug = $opt_d;
$file = $opt_f;
$host = $ARGV[0];
$found_end = 0;
$timeo = 90;				# hlogin timeout in seconds

my(@commandtable, %commands, @commands);# command lists
my($aclsort) = ("ipsort");		# ACL sorting mode
my($filter_commstr);			# SNMP community string filtering
my($filter_osc);			# oscillating data filtering mode
my($filter_pwds);			# password filtering mode

our $cmd;
our @EXPORT = qw($cmd);

# This routine parses "dir"
sub Dir {
    print STDERR "    In Dir: $_" if ($debug);
    $_ =~ s/^[^#]*//;
    ProcessHistory("COMMENTS","keysort","D1","!\n! $_");

    while (<INPUT>) {
	s/^\s+\015//g;
	tr/\015//d;
	next if /^\s*$/;
	last if(/$prompt/);
	# pager remnants like: ^H^H^H    ^H^H^H content
	s/[\b]+\s*[\b]*//g;

	# filter oscillating files
	next if /^aaafile\.prv/;

	ProcessHistory("COMMENTS","keysort","D1","! $_");
    }
    ProcessHistory("COMMENTS","keysort","D1","!\n");
    return(0);
}

sub ShowVer {
    print STDERR "    In ShowVer: $_" if ($debug);

    while (<INPUT>) {
	tr/\015//d;
	next if /^\s*$/;
	last if(/$prompt/);
	# pager remnants like: ^H^H^H    ^H^H^H content
	s/[\b]+\s*[\b]*//g;

	# Remove Uptime
	/ up time/i && next;

	ProcessHistory("COMMENTS","keysort","B1","! $_");
    }
    return(0);
}

sub ShowSys {
    print STDERR "    In ShowSys: $_" if ($debug);
    $_ =~ s/^[^#]*//;
    ProcessHistory("COMMENTS","keysort","C1","!\n! $_");

    while (<INPUT>) {
	s/^\s+\015//g;
	tr/\015//d;
	next if /^\s*$/;
	last if(/$prompt/);
	# pager remnants like: ^H^H^H    ^H^H^H content
	s/[\b]+\s*[\b]*//g;

	# Remove Uptime
	/ up time/i && next;

	# filter temperature sensor info for Dell 6428 stacks, Powerconnect 7048
	# /Temperature Sensors:/
	if (/Temperature \(Celsius\)/) {
	    ProcessHistory("COMMENTS","keysort","C1","! $_");
	    ProcessHistory("COMMENTS","keysort","C1","! Unit\tStatus\n");
	    ProcessHistory("COMMENTS","keysort","C1","! ----\t------\n");
	    while (<INPUT>) {
		s/^\s+\015//g;
		tr/\015//d;
		/(\d+)\s+\d+\s+(.*)$/ &&
		ProcessHistory("COMMENTS","keysort","C1","! $1\t$2\n");
		/^\s*$/ && last;
	    }
	} elsif (/Temperature/) {
	    # Filter temperature sensor info for Dell M6348 and M8024 blade
	    # switches.
	    #
	    # M6348 and M8024 sample lines:
	    #   Unit     Description       Temperature    Status
	    #                               (Celsius)
	    #   ----     -----------       -----------    ------
	    #   1        System            39             Good
	    #   2        System            39             Good
	    ProcessHistory("COMMENTS","keysort","C1",
			   "! Unit\tDescription\tStatus\n");
	    ProcessHistory("COMMENTS","keysort","C1",
			   "! ----\t-----------\t------\n");
	    while (<INPUT>) {
		/\(celsius\)/i && next;
		s/^\s+\015//g;
		tr/\015//d;
		/(\d+)\s+(\w+)\s+\d+\s+(.*)$/ &&
		ProcessHistory("COMMENTS","keysort","C1","! $1\t$2\t\t$3\n");
		/^\s*$/ && last;
	    }
	}

	# filter power rates and tmestamps from 7024 power supply info
	# Power Supplies:
	#  
	# Unit  Description    Status     Average     Current          Since
	#                                  Power       Power         Date/Time
	#                                 (Watts)     (Watts)
	# ----  -----------  -----------  ----------  --------  -------------------
	# 1     System       OK            1.4        65.2
	# 1     Internal     OK           N/A         N/A       10/05/2017 20:18:35
	if (/power supplies/i) {
	    ProcessHistory("COMMENTS","keysort","C1",
		"! Unit\tDescription\tStatus\n");
	    ProcessHistory("COMMENTS","keysort","C1",
		"! ----\t-----------\t------\n");
	    while (<INPUT>) {
		s/^\s+\015//g;
		tr/\015//d;
		/^(unit\s|--+\s|\s)/i && next;
		if (/(\d+)\s+(\w+)\s+(\w+(\s\w+)?)\s/) {
		    if (length($2) >= 8) {
			ProcessHistory("COMMENTS","keysort","C1","! $1\t$2\t$3\n");
		    } else {
			ProcessHistory("COMMENTS","keysort","C1","! $1\t$2\t\t$3\n");
		    }
		}
		/^\s*$/ && last;
	    }
	}

	/system description: (.*)/i &&
	    ProcessHistory("COMMENTS","keysort","A1", "!Chassis type: $1\n") &&
	    next;

	ProcessHistory("COMMENTS","keysort","C1","! $_");
    }
    return(0);
}

sub ShowVlan {
    print STDERR "    In ShowVlan: $_" if ($debug);
    $_ =~ s/^[^#]*//;
    ProcessHistory("COMMENTS","keysort","D1","!\n! $_");

    while (<INPUT>) {
	s/^\s+\015//g;
	tr/\015//d;
	next if /^\s*$/;
	last if(/$prompt/);
	# pager remnants like: ^H^H^H    ^H^H^H content
	s/[\b]+\s*[\b]*//g;

	# Remove Uptime
	/ up time/i && next;
	ProcessHistory("COMMENTS","keysort","D1","! $_");
    }
    return(0);
}

# This routine processes a "write term" (aka show running-configuration)
sub WriteTerm {
    my($comment) = (0);
    print STDERR "    In WriteTerm: $_" if ($debug);

    while (<INPUT>) {
	tr/\015//d;
	next if /^\s*$/;
	last if(/$prompt/);
	# pager remnants like: ^H^H^H    ^H^H^H content
	s/[\b]+\s*[\b]*//g;

	# skip consecutive comment lines
	if (/^!/) {
	    next if ($comment);
	    ProcessHistory("","","",$_);
	    $comment++;
	    next;
	}
	$comment = 0;

	/^building running-config/ && next;
	/^------+/ && ProcessHistory("","","","!$_") && next;
	/^router configuration/i && ProcessHistory("","","","!$_") && next;
	/^oob host config/i && ProcessHistory("","","","!$_") && next;
	/^empty configuration/i && ProcessHistory("","","","!$_") && next;

	if (/^username (\S+)(\s.*)? password ((\d) \S+|\S+)/) {
	    if ($filter_pwds >= 2) {
		ProcessHistory("USER","keysort","$1",
			       "!username $1$2 password <removed>\n");
	    } elsif ($filter_pwds >= 1 && $3 ne "5"){
		ProcessHistory("USER","keysort","$1",
			       "!username $1$2 password <removed>\n");
	    } else {
		ProcessHistory("USER","keysort","$1","$_");
	    }
	    next;
	}

	if (/^(enable password)( level \d+)? / && $filter_pwds >= 1) {
	    ProcessHistory("ENABLE","","","!$1$2 <removed>\n");
	    next;
	}

	if (/^password (\S+) encrypted/ && $filter_pwds > 1) {
	    ProcessHistory("","","","!password <removed> encrypted\n");
	    next;
	}
	if (/^password (\S+)$/ && $filter_pwds >= 1) {
	    ProcessHistory("","","","!password <removed>\n");
	    next;
	}

	if (/^(enable password level \d+) (\S+) encrypted/ && $filter_pwds > 1){
	    ProcessHistory("","","","!$1 <removed> encrypted\n");
	    next;
	}
	if (/^(enable password level \d+) (\S+)$/ && $filter_pwds >= 1) {
	    ProcessHistory("","","","!$1 <removed> $'\n");
	    next;
	}

	# order/prune snmp-server host statements
	# we only prune lines of the form
	# snmp-server host a.b.c.d <community>
	if (/^(snmp-server host) (\d+\.\d+\.\d+\.\d+) (\S+)/) {
	    if ($filter_commstr) {
		ProcessHistory("SNMPSERVERHOST","ipsort",
			       "$2","!$1 $2 <removed>$'");
	    } else {
		ProcessHistory("SNMPSERVERHOST","ipsort","$2","$_");
	    }
	    next;
	}
	if (/^(snmp-server community) (\S+)/) {
	    if ($filter_commstr) {
		ProcessHistory("SNMPSERVERCOMM","keysort",
			       "$_","!$1 <removed>$'") && next;
	    } else {
		ProcessHistory("SNMPSERVERCOMM","keysort","$2","$_") && next;
	    }
	}

	# prune tacacs/radius server keys
	if (/^(tacacs-server|radius-server) key \w+/ && $filter_pwds >= 1) {
	    ProcessHistory("","","","!$1 <removed>$'"); next;
	}

	ProcessHistory("","","","$_");
    }
    $found_end = 1;
    return(1);
}

# Main
rancidinit();

@commandtable = (
	{'terminal datadump'		=> 'RunCommand'},	# dump all output of a show command without prompting
	{'show system'			=> 'ShowSys'},
	{'show version'			=> 'ShowVer'},
	{'dir'				=> 'Dir'},
	{'show vlan'			=> 'ShowVlan'},
	{'show running-config'		=> 'WriteTerm'}
);
# Use an array to preserve the order of the commands and a hash for mapping
# commands to the subroutine and track commands that have been completed.
@commands = map(keys(%$_), @commandtable);
%commands = map(%$_, @commandtable);
$commandcnt = scalar(keys %commands);

$commandstr=join(";",@commands);
$cmds_regexp = join("|", map quotemeta($_), @commands);

if (length($host) == 0) {
    if ($file) {
	print(STDERR "Too few arguments: file name required\n");
	exit(1);
    } else {
	print(STDERR "Too few arguments: host name required\n");
	exit(1);
    }
}
if ($opt_C) {
    print "hlogin -t $timeo -c\'$commandstr\' $host\n";
    exit(0);
}
open(OUTPUT,">$host.new") || die "Can't open $host.new for writing: $!\n";
select(OUTPUT);
# make OUTPUT unbuffered if debugging
if ($debug) { $| = 1; }

if ($file) {
    print(STDERR "opening file $host\n") if ($debug || $log);
    open(INPUT,"<$host") || die "open failed for $host: $!\n";
} else {
    print(STDERR "executing hlogin -t $timeo -c\"$commandstr\" $host\n") if ($debug || $log);
    if (defined($ENV{NOPIPE}) && $ENV{NOPIPE} =~ /^YES/i) {
	system "hlogin -t $timeo -c \"$commandstr\" $host </dev/null > $host.raw 2>&1" || die "hlogin failed for $host: $!\n";
	open(INPUT, "< $host.raw") || die "hlogin failed for $host: $!\n";
    } else {
	open(INPUT,"hlogin -t $timeo -c \"$commandstr\" $host </dev/null |") || die "hlogin failed for $host: $!\n";
    }
}

# determine ACL sorting mode
if ($ENV{"ACLSORT"} =~ /no/i) {
    $aclsort = "";
}
# determine community string filtering mode
if (defined($ENV{"NOCOMMSTR"}) &&
    ($ENV{"NOCOMMSTR"} =~ /yes/i || $ENV{"NOCOMMSTR"} =~ /^$/)) {
    $filter_commstr = 1;
} else {
    $filter_commstr = 0;
}
# determine oscillating data filtering mode
if (defined($ENV{"FILTER_OSC"}) && $ENV{"FILTER_OSC"} =~ /no/i) {
    $filter_osc = 0;
} else {
    $filter_osc = 1;
}
# determine password filtering mode
if ($ENV{"FILTER_PWDS"} =~ /no/i) {
    $filter_pwds = 0;
} elsif ($ENV{"FILTER_PWDS"} =~ /all/i) {
    $filter_pwds = 2;
} else {
    $filter_pwds = 1;
}

ProcessHistory("","","","!RANCID-CONTENT-TYPE: smc\n!\n");
ProcessHistory("COMMENTS","keysort","A0","!\n");
ProcessHistory("COMMENTS","keysort","B0","!\n");
ProcessHistory("COMMENTS","keysort","C0","!\n");
ProcessHistory("COMMENTS","keysort","D0","!\n");
TOP: while(<INPUT>) {
    tr/\015//d;
    if (/^Error:/) {
	print STDOUT ("$host hlogin error: $_");
	print STDERR ("$host hlogin error: $_") if ($debug);
	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";
	    last TOP;
	}
	my($rval) = &{$commands{$cmd}}(*INPUT, *OUTPUT, $cmd);
	delete($commands{$cmd});
	if ($rval == -1) {
	    last TOP;
	}
    }
}
print STDOUT "Done $lscript: $_\n" if ($log);
# Flush History
ProcessHistory("","","","");
# Cleanup
close(INPUT);
close(OUTPUT);

if (defined($ENV{NOPIPE}) && $ENV{NOPIPE} =~ /^YES/i) {
    unlink("$host.raw") if (! $debug);
}

# check for completeness
if (scalar(%commands) || !$found_end) {
    if (scalar(keys %commands) eq $commandcnt) {
	printf(STDERR "$host: missed cmd(s): all commands\n");
    } elsif (scalar(%commands)) {
	my($count, $i) = 0;
	for ($i = 0; $i < $#commands; $i++) {
	    if ($commands{$commands[$i]}) {
		if (!$count) {
		    printf(STDERR "$host: missed cmd(s): %s", $commands[$i]);
		} else {
		    printf(STDERR ", %s", $commands[$i]);
		}
		$count++;
	    }
	}
	if ($count) {
	    printf(STDERR "\n");
	}
    }
    if (!$found_end) {
	print(STDERR "$host: End of run not found\n");
	if ($debug) {
	    print(STDERR "$host: found_end is false\n") if (!$found_end);
	}
	system("/usr/bin/tail -1 $host.new");
    }
    unlink "$host.new" if (! $debug);
}
