#!/bin/sh # # $OpenBSD: snmpd.sh,v 1.20 2024/02/08 17:09:51 martijn Exp $ #/* # * Copyright (c) Rob Pierce # * # * Permission to use, copy, modify, and distribute this software for any # * purpose with or without fee is hereby granted, provided that the above # * copyright notice and this permission notice appear in all copies. # * # * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # */ # Basic snmpd regression script. export OBJDIR FAILED=0 SLEEP=1 PF[0]="disabled" PF[1]="enabled" STARTSOCK="/tmp/agentx" # This file will be creatred by traphandler.c as user _snmpd TMPFILE=$(mktemp -q /tmp/_snmpd_traptest.XXXXXX) trap 'skip' INT if [ "$(pgrep snmpd)" ] then echo "The snmpd daemon is already running." echo SKIPPED exit 0 fi snmpdstart() { rm "${STARTSOCK}" >/dev/null 2>&1 (cd ${OBJDIR} && nohup snmpd -dvf ./snmpd.conf > snmpd.log 2>&1) & i=0 # wait max ~10s while [ ! -S "$STARTSOCK" ]; do i=$((i + 1)) if [ $i -eq 100 ]; then echo "Failed to start snmpd" >&2 snmpdstop fail fi sleep 0.1 done } snmpdstop() { pkill snmpd wait rm -f "${STARTSOCK}" >/dev/null 2>&1 } cleanup() { rm ${STARTSOCK} >/dev/null 2>&1 rm ${TMPFILE} >/dev/null 2>&1 rm ${OBJDIR}/nohup.out >/dev/null 2>&1 rm ${OBJDIR}/snmpd.log >/dev/null 2>&1 rm ${OBJDIR}/snmpd.conf >/dev/null 2>&1 } fail() { echo FAILED cleanup exit 1 } skip() { echo SKIPPED cleanup exit 0 } # # # # # CONFIG ONE # # # # # echo "\nConfiguration: default community strings, trap receiver, trap handle\n" cat > ${OBJDIR}/snmpd.conf < /dev/null 2>&1 #snmp_command="snmp get -v2c -cpublic localhost 1.3.6.1.2.1.1.4.0" #echo ======= $snmp_command #contact="$(eval $snmp_command)" #contact="${contact##sysContact.0 = STRING: }" #if [ "$contact" != "$puffy" ] #then # echo "Setting with default rw community string failed." # FAILED=1 #fi snmpdstop # # # # # CONFIG TWO # # # # # echo "\nConfiguration: seclevel auth\n" cat > ${OBJDIR}/snmpd.conf < /dev/null 2>&1 if [ $? -eq 0 ] then echo "Non-defaut ro community string test failed." FAILED=1 fi # get with SHA authentication os="$(uname -s)" snmp_command="snmp get -v3 -Oq -l authNoPriv -u hans -a SHA -A password123 \ localhost system.sysDescr.0" echo ======= $snmp_command system="$(eval $snmp_command | awk '{ print $2 }')" if [ "$system" != "$os" ] then echo "Retrieval test with seclevel auth and SHA failed." FAILED=1 fi snmpdstop # # # # # CONFIG THREE # # # # # echo "\nConfiguration: seclevel enc\n" cat > ${OBJDIR}/snmpd.conf < ${OBJDIR}/snmpd.conf < /dev/null 2>&1 #snmp_command="snmp get -Oqv -v2c -cnon-default-ro localhost 1.3.6.1.2.1.1.4.0" #echo ======= $snmp_command #contact="$(eval $snmp_command)" #if [ "$contact" != "$puffy" ] #then # echo "Setting with default rw community string failed." # FAILED=1 #fi # custom oids, with a ro that we should not be able to set snmp_command="snmp get -Oqv -v2c -cnon-default-rw localhost \ 1.3.6.1.4.1.30155.42.1.0" echo ======= $snmp_command string="$(eval $snmp_command)" if [ "$string" != "humppa" ] then echo "couldn't get customer oid string" FAILED=1 fi snmp_command="snmp get -Oqv -v2c -c non-default-rw localhost \ 1.3.6.1.4.1.30155.42.2.0" echo ======= $snmp_command integer="$(eval $snmp_command)" if [ $integer -ne 1 ] then echo "Retrieval of customer oid integer failed." FAILED=1 fi # Currently no set support in snmpd #snmp_command="snmp set -c non-default-rw -v 1 localhost \ # 1.3.6.1.4.1.30155.42.1.0 s \"bula\"" #echo ======= $snmp_command #eval $snmp_command > /dev/null 2>&1 #if [ $? -eq 0 ] #then # echo "Setting of a ro custom oid test unexpectedly succeeded." # FAILED=1 #fi snmpdstop case $FAILED in 0) echo cleanup exit 0 ;; 1) fail ;; esac