-# $OpenBSD: Makefile,v 1.6 2023/11/06 09:46:04 martijn Exp $
+# $OpenBSD: Makefile,v 1.7 2024/02/08 17:09:51 martijn Exp $
SNMP?= /usr/bin/snmp
SNMPD?= /usr/sbin/snmpd -f ${.OBJDIR}/snmpd.conf
printf 'listen on $$listen6_addr snmpv1 snmpv2c snmpv3\n' >> snmpd.conf
printf 'listen on tcp $$listen6_addr snmpv1 snmpv2c snmpv3\n' >> snmpd.conf
printf 'listen on $$listen_addr notify snmpv1 snmpv2c snmpv3\n\n' >> snmpd.conf
+ printf 'agentx path "/tmp/agentx"\n\n' >> snmpd.conf
printf 'read-only community public\n' >> snmpd.conf
printf 'read-write community private\n' >> snmpd.conf
printf 'trap community public\n\n' >> snmpd.conf
chmod a+rw trap_output
start: stop snmpd.conf traphandle.sh trap_output
+ rm -f /tmp/agentx 2>/dev/null
${SUDO} ${SNMPD}
+ @(i=0; \
+ while [ ! -S /tmp/agentx ]; do \
+ i=$$((i + 1)); \
+ [ $$i -eq 100 ] && ( \
+ printf "%s\n" '${SUDO} pkill -xf "${SNMPD}"'; \
+ printf "Failed to start snmpd\n" >&2; \
+ ${SUDO} pkill -xf "${SNMPD}"; \
+ exit 1; \
+ ); \
+ sleep 0.1; \
+ done;)
stop:
-${SUDO} pkill -xf "${SNMPD}"
-# $OpenBSD: Makefile,v 1.12 2023/11/20 10:34:21 martijn Exp $
+# $OpenBSD: Makefile,v 1.13 2024/02/08 17:09:51 martijn Exp $
# Regress tests for snmpd
PROG = snmpd_regress
${REGRESS_TARGETS}: snmpd_regress
# Always start snmpd if it's not running
- @pgrep -q snmpd || \
- (printf "%s\n" "${SNMPD_START}"; ${SNMPD_START} sleep 0.5)
+ @pgrep -q snmpd || ( \
+ printf "%s\n" "${SNMPD_START}"; \
+ rm -f /tmp/agentx 2>/dev/null; \
+ ${SNMPD_START} \
+ i=0; \
+ while [ ! -S /tmp/agentx ]; do \
+ i=$$((i + 1)); \
+ [ $$i -eq 100 ] && ( \
+ printf "Failed to start snmpd\n"; >&2 \
+ ${SUDO} pkill -f ${SNMPD}; \
+ exit 1; \
+ ); \
+ sleep 0.1; \
+ done \
+ )
./snmpd_regress ${SNMPD_REGRESS_FLAGS} $@
# Make sure that snmpd hasn't crashed in the meantime.
@sleep 0.01
#!/bin/sh
#
-# $OpenBSD: snmpd.sh,v 1.19 2023/11/04 09:42:17 martijn Exp $
+# $OpenBSD: snmpd.sh,v 1.20 2024/02/08 17:09:51 martijn Exp $
#/*
# * Copyright (c) Rob Pierce <rob@openbsd.org>
# *
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)
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
listen on ::1 snmpv1 snmpv2c snmpv3
listen on ::1 snmpv2c notify
+agentx path "${STARTSOCK}"
+
# Specify communities
read-only community public
read-write community private
trap handle 1.2.3.4 "/usr/bin/touch ${TMPFILE}"
EOF
-(cd ${OBJDIR} && nohup snmpd -dvf ./snmpd.conf > snmpd.log 2>&1) &
-
-sleep ${SLEEP}
-
-[ ! -n "$(pgrep snmpd)" ] && echo "Failed to start snmpd." && fail
+snmpdstart
# pf (also checks "oid all" which obtains privileged kernel data
# FAILED=1
#fi
-kill $(pgrep snmpd) >/dev/null 2>&1
-wait
+snmpdstop
# # # # # CONFIG TWO # # # # #
echo "\nConfiguration: seclevel auth\n"
listen on 127.0.0.1
listen on ::1
+agentx path "${STARTSOCK}"
+
seclevel auth
user "hans" authkey "password123" auth hmac-sha1
EOF
-(cd ${OBJDIR} && nohup snmpd -dvf ./snmpd.conf > snmpd.log 2>&1) &
-
-sleep ${SLEEP}
-
-[ ! -n "$(pgrep snmpd)" ] && echo "Failed to start snmpd." && fail
+snmpdstart
# make sure we can't get an oid with deault community string
FAILED=1
fi
-kill $(pgrep snmpd) >/dev/null 2>&1
-wait
+snmpdstop
# # # # # CONFIG THREE # # # # #
echo "\nConfiguration: seclevel enc\n"
listen on 127.0.0.1
listen on ::1
+agentx path "${STARTSOCK}"
+
seclevel enc
user "hans" authkey "password123" auth hmac-sha1 enc aes enckey "321drowssap"
EOF
-(cd ${OBJDIR} && nohup snmpd -dvf ./snmpd.conf > snmpd.log 2>&1) &
-
-sleep ${SLEEP}
-
-[ ! -n "$(pgrep snmpd)" ] && echo "Failed to start snmpd." && fail
+snmpdstart
# get with SHA authentication and AES encryption
FAILED=1
fi
-kill $(pgrep snmpd) >/dev/null 2>&1
-wait
+snmpdstop
# # # # # CONFIG FOUR # # # # #
echo "\nConfiguration: non-default community strings, custom oids\n"
listen on 127.0.0.1 snmpv1 snmpv2c
listen on ::1 snmpv1 snmpv2c
+agentx path "${STARTSOCK}"
+
read-only community non-default-ro
read-write community non-default-rw
# No need to place a full index, we just need the object
EOF
-(cd ${OBJDIR} && nohup snmpd -dvf ./snmpd.conf > snmpd.log 2>&1) &
-
-sleep ${SLEEP}
-
-[ ! -n "$(pgrep snmpd)" ] && echo "Failed to start snmpd." && fail
+snmpdstart
# carp allow with non-default ro community string
# FAILED=1
#fi
-kill $(pgrep snmpd) >/dev/null 2>&1
+snmpdstop
case $FAILED in
0) echo