1fcace290SJake Freeland#- 2fcace290SJake Freeland# SPDX-License-Identifier: BSD-2-Clause 3fcace290SJake Freeland# 4fcace290SJake Freeland# Copyright (c) 2021, 2023 The FreeBSD Foundation 5fcace290SJake Freeland# 6fcace290SJake Freeland# This software was developed by Mark Johnston under sponsorship from 7fcace290SJake Freeland# the FreeBSD Foundation. 8fcace290SJake Freeland# 9fcace290SJake Freeland# This software was developed by Jake Freeland under sponsorship from 10fcace290SJake Freeland# the FreeBSD Foundation. 11fcace290SJake Freeland# 12fcace290SJake Freeland 13fcace290SJake Freeland# Tests to-do: 14fcace290SJake Freeland# actions: hostname, users 15fcace290SJake Freeland 16fcace290SJake Freelandreadonly SYSLOGD_UDP_PORT="5140" 17fcace290SJake Freelandreadonly SYSLOGD_CONFIG="${PWD}/syslog.conf" 18fcace290SJake Freelandreadonly SYSLOGD_LOCAL_SOCKET="${PWD}/log.sock" 19fcace290SJake Freelandreadonly SYSLOGD_PIDFILE="${PWD}/syslogd.pid" 20fcace290SJake Freelandreadonly SYSLOGD_LOCAL_PRIVSOCKET="${PWD}/logpriv.sock" 21fcace290SJake Freeland 22fcace290SJake Freeland# Start a private syslogd instance. 23fcace290SJake Freelandsyslogd_start() 24fcace290SJake Freeland{ 25*5d045d55SMark Johnston local jail 26*5d045d55SMark Johnston 27*5d045d55SMark Johnston if [ "$1" = "-j" ]; then 28*5d045d55SMark Johnston jail="jexec $2" 29*5d045d55SMark Johnston shift 2 30*5d045d55SMark Johnston fi 31*5d045d55SMark Johnston $jail syslogd \ 32fcace290SJake Freeland -b ":${SYSLOGD_UDP_PORT}" \ 33fcace290SJake Freeland -C \ 34fcace290SJake Freeland -d \ 35fcace290SJake Freeland -f "${SYSLOGD_CONFIG}" \ 36fcace290SJake Freeland -H \ 37fcace290SJake Freeland -p "${SYSLOGD_LOCAL_SOCKET}" \ 38fcace290SJake Freeland -P "${SYSLOGD_PIDFILE}" \ 39fcace290SJake Freeland -S "${SYSLOGD_LOCAL_PRIVSOCKET}" \ 40fcace290SJake Freeland $@ \ 41fcace290SJake Freeland & 42fcace290SJake Freeland 43fcace290SJake Freeland # Give syslogd a bit of time to spin up. 44fcace290SJake Freeland while [ "$((i+=1))" -le 20 ]; do 45fcace290SJake Freeland [ -S "${SYSLOGD_LOCAL_SOCKET}" ] && return 46fcace290SJake Freeland sleep 0.1 47fcace290SJake Freeland done 48fcace290SJake Freeland atf_fail "timed out waiting for syslogd to start" 49fcace290SJake Freeland} 50fcace290SJake Freeland 51fcace290SJake Freeland# Simple logger(1) wrapper. 52fcace290SJake Freelandsyslogd_log() 53fcace290SJake Freeland{ 54fcace290SJake Freeland atf_check -s exit:0 -o empty -e empty logger $* 55fcace290SJake Freeland} 56fcace290SJake Freeland 57fcace290SJake Freeland# Make syslogd reload its configuration file. 58fcace290SJake Freelandsyslogd_reload() 59fcace290SJake Freeland{ 60fcace290SJake Freeland pkill -HUP -F "${SYSLOGD_PIDFILE}" 61fcace290SJake Freeland} 62fcace290SJake Freeland 63fcace290SJake Freeland# Stop a private syslogd instance. 64fcace290SJake Freelandsyslogd_stop() 65fcace290SJake Freeland{ 66fcace290SJake Freeland pid=$(cat "${SYSLOGD_PIDFILE}") 67fcace290SJake Freeland if pkill -F "${SYSLOGD_PIDFILE}"; then 68fcace290SJake Freeland wait "${pid}" 69fcace290SJake Freeland rm -f "${SYSLOGD_PIDFILE}" "${SYSLOGD_LOCAL_SOCKET}" \ 70fcace290SJake Freeland "${SYSLOGD_LOCAL_PRIVSOCKET}" 71fcace290SJake Freeland fi 72fcace290SJake Freeland} 73fcace290SJake Freeland 74fcace290SJake Freelandatf_test_case "basic" "cleanup" 75fcace290SJake Freelandbasic_head() 76fcace290SJake Freeland{ 77fcace290SJake Freeland atf_set descr "Messages are logged via supported transports" 78fcace290SJake Freeland} 79fcace290SJake Freelandbasic_body() 80fcace290SJake Freeland{ 81fcace290SJake Freeland logfile="${PWD}/basic.log" 82fcace290SJake Freeland printf "user.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}" 83fcace290SJake Freeland syslogd_start 84fcace290SJake Freeland 85fcace290SJake Freeland syslogd_log -p user.debug -t basic -h "${SYSLOGD_LOCAL_SOCKET}" \ 86fcace290SJake Freeland "hello, world (unix)" 87fcace290SJake Freeland atf_check -s exit:0 -o match:"basic: hello, world \(unix\)" \ 88fcace290SJake Freeland tail -n 1 "${logfile}" 89fcace290SJake Freeland 90fcace290SJake Freeland # Grab kernel configuration file. 91fcace290SJake Freeland sysctl kern.conftxt > conf.txt 92fcace290SJake Freeland 93fcace290SJake Freeland # We have INET transport; make sure we can use it. 94fcace290SJake Freeland if grep -qw "INET" conf.txt; then 95fcace290SJake Freeland syslogd_log -4 -p user.debug -t basic -h 127.0.0.1 -P "${SYSLOGD_UDP_PORT}" \ 96fcace290SJake Freeland "hello, world (v4)" 97fcace290SJake Freeland atf_check -s exit:0 -o match:"basic: hello, world \(v4\)" \ 98fcace290SJake Freeland tail -n 1 "${logfile}" 99fcace290SJake Freeland fi 100fcace290SJake Freeland # We have INET6 transport; make sure we can use it. 101fcace290SJake Freeland if grep -qw "INET6" conf.txt; then 102fcace290SJake Freeland syslogd_log -6 -p user.debug -t basic -h ::1 -P "${SYSLOGD_UDP_PORT}" \ 103fcace290SJake Freeland "hello, world (v6)" 104fcace290SJake Freeland atf_check -s exit:0 -o match:"basic: hello, world \(v6\)" \ 105fcace290SJake Freeland tail -n 1 "${logfile}" 106fcace290SJake Freeland fi 107fcace290SJake Freeland} 108fcace290SJake Freelandbasic_cleanup() 109fcace290SJake Freeland{ 110fcace290SJake Freeland syslogd_stop 111fcace290SJake Freeland} 112fcace290SJake Freeland 113fcace290SJake Freelandatf_test_case "reload" "cleanup" 114fcace290SJake Freelandreload_head() 115fcace290SJake Freeland{ 116fcace290SJake Freeland atf_set descr "SIGHUP correctly refreshes configuration" 117fcace290SJake Freeland} 118fcace290SJake Freelandreload_body() 119fcace290SJake Freeland{ 120fcace290SJake Freeland logfile="${PWD}/reload.log" 121fcace290SJake Freeland printf "user.debug\t/${logfile}\n" > "${SYSLOGD_CONFIG}" 122fcace290SJake Freeland syslogd_start 123fcace290SJake Freeland 124fcace290SJake Freeland syslogd_log -p user.debug -t reload -h "${SYSLOGD_LOCAL_SOCKET}" \ 125fcace290SJake Freeland "pre-reload" 126fcace290SJake Freeland atf_check -s exit:0 -o match:"reload: pre-reload" tail -n 1 "${logfile}" 127fcace290SJake Freeland 128fcace290SJake Freeland # Override the old rule. 129fcace290SJake Freeland truncate -s 0 "${logfile}" 130fcace290SJake Freeland printf "news.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}" 131fcace290SJake Freeland syslogd_reload 132fcace290SJake Freeland 133fcace290SJake Freeland syslogd_log -p user.debug -t reload -h "${SYSLOGD_LOCAL_SOCKET}" \ 134fcace290SJake Freeland "post-reload user" 135fcace290SJake Freeland syslogd_log -p news.debug -t reload -h "${SYSLOGD_LOCAL_SOCKET}" \ 136fcace290SJake Freeland "post-reload news" 137fcace290SJake Freeland atf_check -s exit:0 -o not-match:"reload: post-reload user" cat ${logfile} 138fcace290SJake Freeland atf_check -s exit:0 -o match:"reload: post-reload news" cat ${logfile} 139fcace290SJake Freeland} 140fcace290SJake Freelandreload_cleanup() 141fcace290SJake Freeland{ 142fcace290SJake Freeland syslogd_stop 143fcace290SJake Freeland} 144fcace290SJake Freeland 145fcace290SJake Freelandatf_test_case "prog_filter" "cleanup" 146fcace290SJake Freelandprog_filter_head() 147fcace290SJake Freeland{ 148fcace290SJake Freeland atf_set descr "Messages are only received from programs in the filter" 149fcace290SJake Freeland} 150fcace290SJake Freelandprog_filter_body() 151fcace290SJake Freeland{ 152fcace290SJake Freeland logfile="${PWD}/prog_filter.log" 153fcace290SJake Freeland printf "!prog1,prog2\nuser.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}" 154fcace290SJake Freeland syslogd_start 155fcace290SJake Freeland 156fcace290SJake Freeland for i in 1 2 3; do 157fcace290SJake Freeland syslogd_log -p user.debug -t "prog${i}" -h "${SYSLOGD_LOCAL_SOCKET}" \ 158fcace290SJake Freeland "hello this is prog${i}" 159fcace290SJake Freeland done 160fcace290SJake Freeland atf_check -s exit:0 -o match:"prog1: hello this is prog1" cat "${logfile}" 161fcace290SJake Freeland atf_check -s exit:0 -o match:"prog2: hello this is prog2" cat "${logfile}" 162fcace290SJake Freeland atf_check -s exit:0 -o not-match:"prog3: hello this is prog3" cat "${logfile}" 163fcace290SJake Freeland 164fcace290SJake Freeland # Override the old rule. 165fcace290SJake Freeland truncate -s 0 ${logfile} 166fcace290SJake Freeland printf "!-prog1,prog2\nuser.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}" 167fcace290SJake Freeland syslogd_reload 168fcace290SJake Freeland 169fcace290SJake Freeland for i in 1 2 3; do 170fcace290SJake Freeland syslogd_log -p user.debug -t "prog${i}" -h "${SYSLOGD_LOCAL_SOCKET}" \ 171fcace290SJake Freeland "hello this is prog${i}" 172fcace290SJake Freeland done 173fcace290SJake Freeland atf_check -s exit:0 -o not-match:"prog1: hello this is prog1" cat "${logfile}" 174fcace290SJake Freeland atf_check -s exit:0 -o not-match:"prog2: hello this is prog2" cat "${logfile}" 175fcace290SJake Freeland atf_check -s exit:0 -o match:"prog3: hello this is prog3" cat "${logfile}" 176fcace290SJake Freeland} 177fcace290SJake Freelandprog_filter_cleanup() 178fcace290SJake Freeland{ 179fcace290SJake Freeland syslogd_stop 180fcace290SJake Freeland} 181fcace290SJake Freeland 182fcace290SJake Freelandatf_test_case "host_filter" "cleanup" 183fcace290SJake Freelandhost_filter_head() 184fcace290SJake Freeland{ 185fcace290SJake Freeland atf_set descr "Messages are only received from hostnames in the filter" 186fcace290SJake Freeland} 187fcace290SJake Freelandhost_filter_body() 188fcace290SJake Freeland{ 189fcace290SJake Freeland logfile="${PWD}/host_filter.log" 190fcace290SJake Freeland printf "+host1,host2\nuser.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}" 191fcace290SJake Freeland syslogd_start 192fcace290SJake Freeland 193fcace290SJake Freeland for i in 1 2 3; do 194fcace290SJake Freeland syslogd_log -p user.debug -t "host${i}" -H "host${i}" \ 195fcace290SJake Freeland -h "${SYSLOGD_LOCAL_SOCKET}" "hello this is host${i}" 196fcace290SJake Freeland done 197fcace290SJake Freeland atf_check -s exit:0 -o match:"host1: hello this is host1" cat "${logfile}" 198fcace290SJake Freeland atf_check -s exit:0 -o match:"host2: hello this is host2" cat "${logfile}" 199fcace290SJake Freeland atf_check -s exit:0 -o not-match:"host3: hello this is host3" cat "${logfile}" 200fcace290SJake Freeland 201fcace290SJake Freeland # Override the old rule. 202fcace290SJake Freeland truncate -s 0 ${logfile} 203fcace290SJake Freeland printf "\-host1,host2\nuser.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}" 204fcace290SJake Freeland syslogd_reload 205fcace290SJake Freeland 206fcace290SJake Freeland for i in 1 2 3; do 207fcace290SJake Freeland syslogd_log -p user.debug -t "host${i}" -H "host${i}" \ 208fcace290SJake Freeland -h "${SYSLOGD_LOCAL_SOCKET}" "hello this is host${i}" 209fcace290SJake Freeland done 210fcace290SJake Freeland atf_check -s exit:0 -o not-match:"host1: hello this is host1" cat "${logfile}" 211fcace290SJake Freeland atf_check -s exit:0 -o not-match:"host2: hello this is host2" cat "${logfile}" 212fcace290SJake Freeland atf_check -s exit:0 -o match:"host3: hello this is host3" cat "${logfile}" 213fcace290SJake Freeland} 214fcace290SJake Freelandhost_filter_cleanup() 215fcace290SJake Freeland{ 216fcace290SJake Freeland syslogd_stop 217fcace290SJake Freeland} 218fcace290SJake Freeland 219fcace290SJake Freelandatf_test_case "prop_filter" "cleanup" 220fcace290SJake Freelandprop_filter_head() 221fcace290SJake Freeland{ 222fcace290SJake Freeland atf_set descr "Messages are received based on conditions in the propery based filter" 223fcace290SJake Freeland} 224fcace290SJake Freelandprop_filter_body() 225fcace290SJake Freeland{ 226fcace290SJake Freeland logfile="${PWD}/prop_filter.log" 227fcace290SJake Freeland printf ":msg,contains,\"FreeBSD\"\nuser.debug\t${logfile}\n" \ 228fcace290SJake Freeland > "${SYSLOGD_CONFIG}" 229fcace290SJake Freeland syslogd_start 230fcace290SJake Freeland 231fcace290SJake Freeland syslogd_log -p user.debug -t "prop1" -h "${SYSLOGD_LOCAL_SOCKET}" "FreeBSD" 232fcace290SJake Freeland syslogd_log -p user.debug -t "prop2" -h "${SYSLOGD_LOCAL_SOCKET}" "freebsd" 233fcace290SJake Freeland atf_check -s exit:0 -o match:"prop1: FreeBSD" cat "${logfile}" 234fcace290SJake Freeland atf_check -s exit:0 -o not-match:"prop2: freebsd" cat "${logfile}" 235fcace290SJake Freeland 236fcace290SJake Freeland truncate -s 0 ${logfile} 237fcace290SJake Freeland printf ":msg,!contains,\"FreeBSD\"\nuser.debug\t${logfile}\n" \ 238fcace290SJake Freeland > "${SYSLOGD_CONFIG}" 239fcace290SJake Freeland syslogd_reload 240fcace290SJake Freeland 241fcace290SJake Freeland syslogd_log -p user.debug -t "prop1" -h "${SYSLOGD_LOCAL_SOCKET}" "FreeBSD" 242fcace290SJake Freeland syslogd_log -p user.debug -t "prop2" -h "${SYSLOGD_LOCAL_SOCKET}" "freebsd" 243fcace290SJake Freeland atf_check -s exit:0 -o not-match:"prop1: FreeBSD" cat "${logfile}" 244fcace290SJake Freeland atf_check -s exit:0 -o match:"prop2: freebsd" cat "${logfile}" 245fcace290SJake Freeland 246fcace290SJake Freeland truncate -s 0 ${logfile} 247fcace290SJake Freeland printf ":msg,icase_contains,\"FreeBSD\"\nuser.debug\t${logfile}\n" \ 248fcace290SJake Freeland > "${SYSLOGD_CONFIG}" 249fcace290SJake Freeland syslogd_reload 250fcace290SJake Freeland 251fcace290SJake Freeland syslogd_log -p user.debug -t "prop1" -h "${SYSLOGD_LOCAL_SOCKET}" "FreeBSD" 252fcace290SJake Freeland syslogd_log -p user.debug -t "prop2" -h "${SYSLOGD_LOCAL_SOCKET}" "freebsd" 253fcace290SJake Freeland atf_check -s exit:0 -o match:"prop1: FreeBSD" cat "${logfile}" 254fcace290SJake Freeland atf_check -s exit:0 -o match:"prop2: freebsd" cat "${logfile}" 255fcace290SJake Freeland 256fcace290SJake Freeland truncate -s 0 ${logfile} 257fcace290SJake Freeland printf ":msg,!icase_contains,\"FreeBSD\"\nuser.debug\t${logfile}\n" \ 258fcace290SJake Freeland > "${SYSLOGD_CONFIG}" 259fcace290SJake Freeland syslogd_reload 260fcace290SJake Freeland 261fcace290SJake Freeland syslogd_log -p user.debug -t "prop1" -h "${SYSLOGD_LOCAL_SOCKET}" "FreeBSD" 262fcace290SJake Freeland syslogd_log -p user.debug -t "prop2" -h "${SYSLOGD_LOCAL_SOCKET}" "freebsd" 263fcace290SJake Freeland syslogd_log -p user.debug -t "prop3" -h "${SYSLOGD_LOCAL_SOCKET}" "Solaris" 264fcace290SJake Freeland atf_check -s exit:0 -o not-match:"prop1: FreeBSD" cat "${logfile}" 265fcace290SJake Freeland atf_check -s exit:0 -o not-match:"prop2: freebsd" cat "${logfile}" 266fcace290SJake Freeland atf_check -s exit:0 -o match:"prop3: Solaris" cat "${logfile}" 267fcace290SJake Freeland} 268fcace290SJake Freelandprop_filter_cleanup() 269fcace290SJake Freeland{ 270fcace290SJake Freeland syslogd_stop 271fcace290SJake Freeland} 272fcace290SJake Freeland 273fcace290SJake Freelandatf_test_case "pipe_action" "cleanup" 274fcace290SJake Freelandpipe_action_head() 275fcace290SJake Freeland{ 276fcace290SJake Freeland atf_set descr "The pipe action evaluates provided command in sh(1)" 277fcace290SJake Freeland} 278fcace290SJake Freelandpipe_action_body() 279fcace290SJake Freeland{ 280fcace290SJake Freeland logfile="${PWD}/pipe_action.log" 281fcace290SJake Freeland printf "\"While I'm digging in the tunnel, the elves will often come to me \ 282fcace290SJake Freeland with solutions to my problem.\"\n-Saymore Crey" > ${logfile} 283fcace290SJake Freeland 284fcace290SJake Freeland printf "!pipe\nuser.debug\t| sed -i '' -e 's/Saymore Crey/Seymour Cray/g' \ 285fcace290SJake Freeland ${logfile}\n" > "${SYSLOGD_CONFIG}" 286fcace290SJake Freeland syslogd_start 287fcace290SJake Freeland 288fcace290SJake Freeland syslogd_log -p user.debug -t "pipe" -h "${SYSLOGD_LOCAL_SOCKET}" \ 289fcace290SJake Freeland "fix spelling error" 290fcace290SJake Freeland atf_check -s exit:0 -o match:"Seymour Cray" cat "${logfile}" 291fcace290SJake Freeland} 292fcace290SJake Freelandpipe_action_cleanup() 293fcace290SJake Freeland{ 294fcace290SJake Freeland syslogd_stop 295fcace290SJake Freeland} 296fcace290SJake Freeland 297*5d045d55SMark Johnstonatf_test_case "jail_noinet" "cleanup" 298*5d045d55SMark Johnstonjail_noinet_head() 299*5d045d55SMark Johnston{ 300*5d045d55SMark Johnston atf_set descr "syslogd -ss can be run in a jail without INET support" 301*5d045d55SMark Johnston atf_set require.user root 302*5d045d55SMark Johnston} 303*5d045d55SMark Johnstonjail_noinet_body() 304*5d045d55SMark Johnston{ 305*5d045d55SMark Johnston local logfile 306*5d045d55SMark Johnston 307*5d045d55SMark Johnston atf_check jail -c name=syslogd_noinet persist 308*5d045d55SMark Johnston 309*5d045d55SMark Johnston logfile="${PWD}/jail_noinet.log" 310*5d045d55SMark Johnston printf "user.debug\t${logfile}\n" > "${SYSLOGD_CONFIG}" 311*5d045d55SMark Johnston syslogd_start -j syslogd_noinet -ss 312*5d045d55SMark Johnston 313*5d045d55SMark Johnston syslogd_log -p user.debug -t "test" -h "${SYSLOGD_LOCAL_SOCKET}" \ 314*5d045d55SMark Johnston "hello, world" 315*5d045d55SMark Johnston atf_check -s exit:0 -o match:"test: hello, world" cat "${logfile}" 316*5d045d55SMark Johnston} 317*5d045d55SMark Johnstonjail_noinet_cleanup() 318*5d045d55SMark Johnston{ 319*5d045d55SMark Johnston jail -r syslogd_noinet 320*5d045d55SMark Johnston} 321*5d045d55SMark Johnston 322fcace290SJake Freelandatf_init_test_cases() 323fcace290SJake Freeland{ 324fcace290SJake Freeland atf_add_test_case "basic" 325fcace290SJake Freeland atf_add_test_case "reload" 326fcace290SJake Freeland atf_add_test_case "prog_filter" 327fcace290SJake Freeland atf_add_test_case "host_filter" 328fcace290SJake Freeland atf_add_test_case "prop_filter" 329fcace290SJake Freeland atf_add_test_case "pipe_action" 330*5d045d55SMark Johnston atf_add_test_case "jail_noinet" 331fcace290SJake Freeland} 332