#!/bin/sh
##
## Simple SNMP Agent. This file only does the startup. All MIB
## implementations are done in extra files that are sourced from
## this file.
##
## Copyright (c) 1994, 1995
##
## J. Schoenwaelder
## TU Braunschweig, Germany
## Institute for Operating Systems and Computer Networks
##
## Permission to use, copy, modify, and distribute this
## software and its documentation for any purpose and without
## fee is hereby granted, provided that this copyright
## notice appears in all copies.  The University of Braunschweig
## makes no representations about the suitability of this
## software for any purpose.  It is provided "as is" without
## express or implied warranty.

# Tcl sees the next lines as an assignment to variable `kludge'.
# For sh, the two shifts cancel the effect of the set, and then we
# run scotty on this script.

set kludge { $*
    shift
    shift
    if test -f ../scotty -a -f ../scotty ; then
      exec ../scotty -nf $0 $*
    else
      exec scotty -nf $0 $*
    fi
}

##
## Report all errors to standard error including the complete backtrace.
##

proc tkerror { msg } {
    global errorInfo
    syslog error $msg
    puts stderr $errorInfo
}

##
## Expand the auto_path to auto load script files when needed.
##

set auto_path [concat $scotty_library/agents $auto_path]
if {[file exists tclIndex]} {
    set auto_path [concat . $auto_path]
}

##
## Load some useful MIB definitions.
##

mib load tubs.mib
mib load mlm.mib

##
## Create a V1 agent session. Very simple.
##

proc CreateV1Agent { port {interp {}} } {
    set ip [netdb host address [exec hostname]]
    set s [snmp session -port $port -address $ip -agent $interp -retries 0]
    return $s
}

##
## Create a V2 noAuth/noPriv session using initialPartyId.
##

proc CreateV2Agent { port {interp {}} } {
    set s [snmp session]
    set ip [netdb host address [exec hostname]]
    set initialPartyId [mib oid initialPartyId]
    set initialContextId [mib oid initialContextId]
    $s configure \
	    -srcparty [list $initialPartyId.$ip.1 UDP $ip $port 484] \
	    -dstparty [list $initialPartyId.$ip.2 UDP 0.0.0.0 $port 484] \
	    -context $initialContextId.$ip.1 \
	    -agent $interp -retries 0
    return $s
}

##
## Create a usec agent based on the current user guessed from the
## environment.
##

proc CreateUSECAgent { port {interp {}} } {
    global env
    set ip [netdb host address [exec hostname]]
    if {[info exists env(USER)]} {
	set user $env(USER)
    } elseif {[info exists env(LOGNAME)]} {
	set user $env(LOGNAME)
    } else {
	set user "uhura"
    }
    set s [snmp session -port $port -address $ip -user $user \
	   -agent $interp -retries 0]
    return $s
}

##
## Create a Tcl interpreter that can be used to evaluate arbitrary
## scripts send from potentially unsecure managers. You can run the
## agent with an unsafe Tcl interpreter by uncommenting the return
## command below. But it will be your own risk to do so. You have
## been warned.
##

proc CreateSafeInterp {} {

    # return ""

    if {[catch {interp create -safe} interp]} {
	puts stderr "Running the agent requires the safe Tcl extension (stcl)."
	exit 42
    }

    # List of commands allowed in the safe Tcl interpreter. Note, the
    # http command allows to write to local files. This is no problem
    # if you run the agent on a restricted account.

    # Safe Tcl/Tk commands.
    
    $interp alias tkerror	tkerror
    $interp alias puts		puts stderr
    
    # Safe Scotty commands.
    
    $interp alias getdate	getdate
    $interp alias getclock	getclock
    $interp alias icmp		icmp
    $interp alias http		http
    $interp alias syslog	syslog
    $interp alias mib		mib
    return $interp
}

##
## Startup the agent on port 1701.
##

#set agent "CreateV1Agent 161"
#set agent "CreateV1Agent 1701"
#set agent "CreateV2Agent 161"
#set agent "CreateV2Agent 1701"
set agent "CreateUSECAgent 1701"

set interp [CreateSafeInterp]

if [catch {eval $agent $interp} s] {
    puts stderr "Failed to setup SNMP agent: $s"
    exit 42
}

SNMP_ScottyInit $s
# SNMP_HttpInit $s
SNMP_NfsInit $s
SNMP_ProcInit $s
SNMP_HTTPInit $s
SNMP_MLMInit $s
# SNMP_HttpdInit $s

# snmp watch on

$s bind 1.3 get      { puts "get      -> %V" }
$s bind 1.3 set      { puts "set      -> %V" }
$s bind 1.3 create   { puts "create   -> %V" }
$s bind 1.3 check    { puts "check    -> %V" }
$s bind 1.3 commit   { puts "commit   -> %V" }
$s bind 1.3 rollback { puts "rollback -> %V" }
$s bind ""  begin    { puts "begin    -> %T %E %I %V" }
$s bind ""  end      { puts "end      -> %T %E %I %V" }

puts stdout "\nagent started using the following parameters:\n[$s configure]"
