#! /usr/bin/expect --
##
## $Id: 3cblslogin.in,v 1.0 2009/06/4 18:59:04 heas Exp $
## based on clogin 1.136
## 
## rancid 2.3.2
## Copyright (c) 1997-2009 by Terrapin Communications, Inc.
## copyright (c) 2009 Diego Ercolani diego.ercolani at ssis.sm
## All rights reserved.
##
## This code is derived from software contributed to and maintained by
## Terrapin Communications, Inc. 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. All advertising materials mentioning features or use of this software
##    must display the following acknowledgement:
##        This product includes software developed by Terrapin Communications,
##        Inc. and its contributors for RANCID.
## 4. Neither the name of Terrapin Communications, Inc. nor the names of its
##    contributors may be used to endorse or promote products derived from
##    this software without specific prior written permission.
## 5. It is requested that non-binding fixes and modifications be contributed
##    back to Terrapin Communications, Inc.
##
## THIS SOFTWARE IS PROVIDED BY Terrapin Communications, INC. 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.
# 
#  The expect login scripts were based on Erik Sherk's gwtn, by permission.
# 
# 3cblslogin - 3com Basic Line Switches configuration dumper
#
# used to implement some glue to the 3comBLS2948 and probably
# other Baseline Switches (?) and enable download of configuration and other
# information that are normally reachable only via
# web
#
# The default username password is the same as the vty password.
#

package require http

# Usage line
set usage "Usage: $argv0 \[-dSV\] \[-autoenable\] \[-noenable\] \[-c command\] \
\[-Evar=x\] \[-e enable-password\] \[-f 3cblsloginrc-file\] \[-p user-password\] \
\[-s script-file\] \[-t timeout\] \[-u username\] \
\[-v vty-password\] \[-w enable-username\] \[-x command-file\] \
\[-y ssh_cypher_type\] router \[router...\]\n"

# env(CLOGIN) may contain:
#	x == do not set xterm banner or name

# Password file
set password_file $env(HOME)/.cloginrc
# Default is to login to the router
set do_command 0
set do_script 0
# The default is to automatically enable
set avenable 1
# The default is that you login non-enabled (tacacs can have you login already
# enabled)
set avautoenable 0
# The default is to look in the password file to find the passwords.  This
# tracks if we receive them on the command line.
set do_passwd 1
set do_enapasswd 1
# Save config, if prompted
set do_saveconfig 0
#
# setta la velocità di "digitazione"
#set send_human {default word variability min max}
#viene applicato con send -h
set send_human {.1 .2 1 .1 .3}
# Error tag prefix recognized by 3cblsrancid in the output stream
set ERRORTAG "Error:"
# Command Prompt (virtual) recognized by 3cblsrancid to process command
set COMMANDPROMPT "##PROMPT## :"

proc GetCookie { host login } {
set in_proc 1
 # Get Login Cookie
 set tok [::http::geturl http://$host/login.cgi -query $login]
 upvar #0 $tok state
 foreach { name value } $state(meta) {
   if { $name eq "Set-Cookie" } {
          lappend cookies [lindex [split $value {;}] 0]
   }
 }
 ::http::cleanup $tok
 #puts "$cookies"
 set in_proc 0
 return $cookies
}

proc GetConf { host cookies } {
set in_proc 1
  global COMMANDPROMPT
  # Get Configuration
  set tok2 [::http::geturl http://$host/3comranger48.cfg -headers [list Cookie [join $cookies {;}]]]
  upvar #0 $tok2 state
  #parray $tok2
  #foreach keyitem [array names state] {
  #  puts "$keyitem=$state($keyitem)"
  #}
  #puts $state(body)
  set body $state(body)
  #puts $body
  ::http::cleanup $tok2
  # $COMMANDPROMPT is opening and closing each
  # configuration dump to permit parsing
  # to 3cblsrancid these tags leave 3cblsrancid
  # happy :-)
  puts "$COMMANDPROMPT GetConf"
  puts $body
  puts ": end"
  puts "$COMMANDPROMPT"
  set in_proc 0
  return $body
}

proc GetArpTable { host cookies } {
  set in_proc 1
  global COMMANDPROMPT
  # Get ARP Table
  set tok2 [::http::geturl http://$host/aos3com_addrtable_states.cgi -headers [list Cookie [join $cookies {;}]]]
  upvar #0 $tok2 state
  #parray $tok2
  #foreach keyitem [array names state] {
  #  puts "$keyitem=$state($keyitem)"
  #}
  set lines [split $state(body) "\r\n"]
  foreach i $lines {
   if [regexp {<option>(.*)</option>} $i ignore option] {
     #puts "$option"
     append options "$option\n"
   }
  }
  ::http::cleanup $tok2
  # $COMMANDPROMPT is opening and closing each
  # configuration dump to permit parsing
  # to 3cblsrancid
  puts "$COMMANDPROMPT GetArpTable"
  puts $options
  puts "$COMMANDPROMPT"
  set in_proc 0
  return $options
}

proc ClearCookie { host cookies } {
  set in_proc 1
  # Logout Gacefully
  set tok2 [::http::geturl http://$host/logout.cgi -headers [list Cookie [join $cookies {;}]]]
  upvar #0 $tok2 state
  #parray $tok2
  #foreach keyitem [array names state] {
  #  puts "$keyitem=$state($keyitem)"
  #}
  ::http::cleanup $tok2
  set in_proc 0
}





# Find the user in the ENV, or use the unix userid.
if {[ info exists env(CISCO_USER) ]} {
    set default_user $env(CISCO_USER)
} elseif {[ info exists env(USER) ]} {
    set default_user $env(USER)
} elseif {[ info exists env(LOGNAME) ]} {
    set default_user $env(LOGNAME)
} else {
    # This uses "id" which I think is portable.  At least it has existed
    # (without options) on all machines/OSes I've been on recently -
    # unlike whoami or id -nu.
    if [ catch {exec id} reason ] {
	send_error "\n$ERRORTAG could not exec id: $reason\n"
	exit 1
    }
    regexp {\(([^)]*)} "$reason" junk default_user
}
if {[ info exists env(CLOGINRC) ]} {
    set password_file $env(CLOGINRC)
}

# Process the command line
for {set i 0} {$i < $argc} {incr i} {
    set arg [lindex $argv $i]

    switch  -glob -- $arg {
	# Expect debug mode
	-d* {
	    exp_internal 1
	# Username
	} -u* {
	    if {! [regexp .\[uU\](.+) $arg ignore user]} {
		incr i
		set username [ lindex $argv $i ]
	    }
	# VTY Password
	} -p* {
	    if {! [regexp .\[pP\](.+) $arg ignore userpasswd]} {
		incr i
		set userpasswd [ lindex $argv $i ]
	    }
	    set do_passwd 0
	# VTY Password
	} -v* {
	    if {! [regexp .\[vV\](.+) $arg ignore passwd]} {
		incr i
		set passwd [ lindex $argv $i ]
	    }
	    set do_passwd 0
	# Version string
	} -V* {
	    send_user "rancid 2.3.2\n"
	    exit 0
	# Enable Username
	} -w* {
	    send_user "(-w) Not Implemented\n"
	    exit 1
	    if {! [regexp .\[wW\](.+) $arg ignore enauser]} {
		incr i
		set enausername [ lindex $argv $i ]
	    }
	# Environment variable to pass to -s scripts
	} -E* {
	    if {[regexp .\[E\](.+)=(.+) $arg ignore varname varvalue]} {
		set E$varname $varvalue
	    } else {
		send_user "\n$ERRORTAG invalid format for -E in $arg\n"
		exit 1
	    }
	# Enable Password
	} -e* {
	    send_user "(-e) Not Implemented\n"
	    exit 1
	    if {! [regexp .\[e\](.+) $arg ignore enapasswd]} {
		incr i
		set enapasswd [ lindex $argv $i ]
	    }
	    set do_enapasswd 0
	# Command to run.
	} -c* {
	    if {! [regexp .\[cC\](.+) $arg ignore command]} {
		incr i
		set command [ lindex $argv $i ]
	    }
	    set do_command 1
	# Expect script to run.
	} -s* {
	    if {! [regexp .\[sS\](.+) $arg ignore sfile]} {
		incr i
		set sfile [ lindex $argv $i ]
	    }
	    if { ! [ file readable $sfile ] } {
		send_user "\n$ERRORTAG Can't read $sfile\n"
		exit 1
	    }
	    set do_script 1
	# save config on exit
	} -S* {
	    set do_saveconfig 1
	# 'ssh -c' cypher type
	} -y* {
	    send_user "(-y) Not Implemented\n"
	    exit 1
	    if {! [regexp .\[eE\](.+) $arg ignore cypher]} {
		incr i
		set cypher [ lindex $argv $i ]
	    }
	# alternate 3cblsloginrc file
	} -f* {
	    if {! [regexp .\[fF\](.+) $arg ignore password_file]} {
		incr i
		set password_file [ lindex $argv $i ]
	    }
	# Timeout
	} -t* {
	    if {! [regexp .\[tT\](.+) $arg ignore timeout]} {
		incr i
	        set timeout [ lindex $argv $i ]
	    }
	# Command file
	} -x* {
	    if {! [regexp .\[xX\](.+) $arg ignore cmd_file]} {
		incr i
		set cmd_file [ lindex $argv $i ]
	    }
	    if [ catch {set cmd_fd [open $cmd_file r]} reason ] {
		send_user "\n$ERRORTAG $reason\n"
		exit 1
	    }
	    set cmd_text [read $cmd_fd]
	    close $cmd_fd
	    set command [join [split $cmd_text \n] \;]
	    set do_command 1
	# Do we enable?
	} -noenable {
	    set avenable 0
	# Does tacacs automatically enable us?
	} -autoenable {
	    set avautoenable 1
	    set avenable 0
	} -* {
	    send_user "\n$ERRORTAG Unknown argument! $arg\n"
	    send_user $usage
	    exit 1
	} default {
	    break
	}
    }
}
# Process routers...no routers listed is an error.
if { $i == $argc } {
    send_user "\n$ERRORTAG $usage"
}

# Only be quiet if we are running a script (it can log its output
# on its own)
if { $do_script } {
    log_user 0
} else {
    log_user 1
}

#
# Done configuration/variable setting.  Now run with it...
#

# Sets Xterm title if interactive...if its an xterm and the user cares
proc label { host } {
    global env
    # if CLOGIN has an 'x' in it, don't set the xterm name/banner
    if [info exists env(CLOGIN)] {
	if {[string first "x" $env(CLOGIN)] != -1} { return }
    }
    # take host from ENV(TERM)
    if [info exists env(TERM)] {
	if [ regexp \^(xterm|vs) $env(TERM) ignore ] {
	    send_user "\033]1;[lindex [split $host "."] 0]\a"
	    send_user "\033]2;$host\a"
	}
    }
}

# This is a helper function to make the password file easier to
# maintain.  Using this the password file has the form:
# add password sl*	pete cow
# add password at*	steve
# add password *	hanky-pie
proc add {var args} { global int_$var ; lappend int_$var $args}
proc include {args} {
    global env
    regsub -all "(^{|}$)" $args {} args
    if { [ regexp "^/" $args ignore ] == 0 } {
	set args $env(HOME)/$args
    }
    source_password_file $args
}

proc find {var router} {
    upvar int_$var list
    if { [info exists list] } {
	foreach line $list {
	    if { [string match [lindex $line 0] $router ] } {
		return [lrange $line 1 end]
	    }
	}
    }
    return {}
}

# Loads the password file.  Note that as this file is tcl, and that
# it is sourced, the user better know what to put in there, as it
# could install more than just password info...  I will assume however,
# that a "bad guy" could just as easy put such code in the 3cblslogin
# script, so I will leave .3cblsloginrc as just an extention of that script
proc source_password_file { password_file } {
    global env ERRORTAG
    if { ! [file exists $password_file] } {
	send_user "\n$ERRORTAG password file ($password_file) does not exist\n"
	exit 1
    }
    file stat $password_file fileinfo
    if { [expr ($fileinfo(mode) & 007)] != 0000 } {
	send_user "\n$ERRORTAG $password_file must not be world readable/writable\n"
	exit 1
    }
    if [ catch {source $password_file} reason ] {
	send_user "\n$ERRORTAG $reason\n"
	exit 1
    }
}

## Log into the router.
## returns: 0 on success, 1 on failure, -1 if rsh was used successfully
#proc login { router user userpswd passwd enapasswd cmethod cyphertype } {
# @@@@@@@@@@@@@@ substituted by GetCookie @@@@@@@@@@@@
#
#
# Run commands given on the command line.
# command are really "MetaCommand" as
# switch doesn't have a CLI interface
# Implemented Command are:
# GetConf - Get current configuration
# GetArpTable - Get current arp table without "LAG 1" as
#               in our topology "LAG 1" is used for
#               uplinks to root switches
#
proc run_commands { host cookie command } {
    global in_proc platform COMMANDPROMPT ERRORTAG
    set in_proc 1

    if {[string length $cookie] < 10} {
     # probably cookie is wrong
     # I don't know how to check....
     send_user "Cookie \"$cookie\" is wrong. "
     exit -1
    } 
    set commands [split $command \;]
    set num_commands [llength $commands]
    # the pager can not be turned off on the PIX, so we have to look
    # for the "More" prompt.  the extreme is equally obnoxious, with a
    # global switch in the config.
    for {set i 0} {$i < $num_commands} { incr i} {
	# Main loop to send commands
	# here we are probably just authenticated with a cookie
        # $commands is a tcl list, not an array, we have to threat it as list
        switch -nocase -glob [subst [lindex $commands $i]] {
	 GetConf 	{ GetConf $host $cookie }
	 GetArpTable 	{ GetArpTable $host $cookie }
	 default	{ send_user "$ERRORTAG Command \"[subst [lindex $commands $i]]\" not implemented or Syntax Error in command" }
	}
    }
    # give 3cblsrancid parser loop the end loop without errors
    puts "$COMMANDPROMPT exit"
    log_user 1
    

    ClearCookie $host $cookie
    set in_proc 0
}

#
# For each router... (this is main loop)
#
source_password_file $password_file
set in_proc 0
set exitval 0
foreach router [lrange $argv $i end] {
    set router [string tolower $router]
    # attempt at platform switching.
    set platform ""
    #send_user -- "$router\n"

    # Figure out passwords
    if { $do_passwd } {
      set pswd [find password $router]
      if { [llength $pswd] == 0 } {
	send_user -- "\n$ERRORTAG no password for $router in $password_file.\n"
	continue
      }
      set passwd [join [lindex $pswd 0] ""]
    } else {
	set passwd $userpasswd
    }

    # Figure out username
    if {[info exists username]} {
      # command line username
      set ruser $username
    } else {
      set ruser [join [find user $router] ""]
      if { "$ruser" == "" } { set ruser $default_user }
    }

    # Figure out username's password (if different from the vty password)
    if {[info exists userpasswd]} {
      # command line username
      set userpswd $userpasswd
    } else {
      set userpswd [join [find userpassword $router] ""]
      if { "$userpswd" == "" } { set userpswd $passwd }
    }


    # Figure out connection method
    set cmethod [find method $router]
    if { "$cmethod" == "" } { set cmethod {{http}} 
    } elseif { "$cmethod" == "http" } { set cmethod {{http}} 
    } else { 
      send_user "Method \"$cmethod\" to connect to switch $router not supported."
      exit -1
    }

    # Login to the router
    set rtime [expr {[clock seconds]*2}]
    set login [::http::formatQuery username $ruser password $userpswd rtime $rtime]
    catch { GetCookie $router $login } cookie err
    #puts "->$cookie<-->$err<-"
    if { [regexp errorcode $err ] } {
      send_user "$ERRORTAG login to router $router\ndebug:$cookie\n"
      continue
    }

    if { $do_command } {
	if {[run_commands $router $cookie $command]} {
	    incr exitval
	    continue
	}
      } elseif { $do_script } {
	send_user "$ERRORTAG Not verified: source file\n"
	# If the prompt is (enable), then we are on a switch and the
	# command is "set length 0"; otherwise its "terminal length 0".
	source $sfile
    } else {
    	send_user "\033[32m$ERRORTAG Interactive console not implemented\033[m\n"
	label $router
	exit -1
	#log_user 1
	interact
    }

    # End of for each router
    catch {wait};
    sleep 0.3
}
exit $exitval

#########################
##########################
##########################

#set host ga2948b18.mgt.ssis
#set cookie [GetCookie $host $login]
##puts "Cookie=$cookie"
#set configuration [GetConf $host $cookie]
#set arptable [GetArp $host $cookie]
#ClearCookie $host $cookie
#
#puts "Configuration:\n$configuration\nArpTable:"
#foreach a $arptable {
# if [regexp LAG\ 1 $a] {
#  # if it is a "LAG 1" element in our topology
#  # is an uplink versus root switches
#  # so we don't consider as network clients
#  # 
#  continue
# }
# puts "$a"
#}
#
