opnsense-src/tests/sys/mac/portacl/misc.sh

107 lines
2.3 KiB
Bash
Raw Normal View History

2009-03-14 17:54:19 -04:00
#!/bin/sh
# $FreeBSD$
sysctl security.mac.portacl >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "1..0 # SKIP MAC_PORTACL is unavailable."
2009-03-14 17:54:19 -04:00
exit 0
fi
if [ $(id -u) -ne 0 ]; then
echo "1..0 # SKIP testcases must be run as root"
exit 0
fi
2009-03-14 17:54:19 -04:00
ntest=1
check_bind() {
local host idtype name proto port udpflag
host="127.0.0.1"
2009-03-14 17:54:19 -04:00
idtype=${1}
name=${2}
proto=${3}
port=${4}
[ "${proto}" = "udp" ] && udpflag="-u"
out=$(
2009-03-14 17:54:19 -04:00
case "${idtype}" in
uid|gid)
( echo -n | su -m ${name} -c "nc ${udpflag} -l -w 10 $host $port" 2>&1 ) &
2009-03-14 17:54:19 -04:00
;;
jail)
kill $$
;;
*)
kill $$
esac
sleep 0.3
echo | nc ${udpflag} -w 10 $host $port >/dev/null 2>&1
2009-03-14 17:54:19 -04:00
wait
)
2009-03-14 17:54:19 -04:00
case "${out}" in
"nc: Permission denied"*|"nc: Operation not permitted"*)
echo fl
;;
"")
echo ok
;;
*)
echo ${out}
;;
esac
}
bind_test() {
local expect_without_rule expect_with_rule idtype name proto port
2009-03-14 17:54:19 -04:00
expect_without_rule=${1}
expect_with_rule=${2}
idtype=${3}
name=${4}
proto=${5}
port=${6}
sysctl security.mac.portacl.rules= >/dev/null
out=$(check_bind ${idtype} ${name} ${proto} ${port})
2009-03-14 17:54:19 -04:00
if [ "${out}" = "${expect_without_rule}" ]; then
echo "ok ${ntest}"
elif [ "${out}" = "ok" -o "${out}" = "fl" ]; then
echo "not ok ${ntest} # '${out}' != '${expect_without_rule}'"
2009-03-14 17:54:19 -04:00
else
echo "not ok ${ntest} # unexpected output: '${out}'"
2009-03-14 17:54:19 -04:00
fi
: $(( ntest += 1 ))
2009-03-14 17:54:19 -04:00
if [ "${idtype}" = "uid" ]; then
idstr=$(id -u ${name})
2009-03-14 17:54:19 -04:00
elif [ "${idtype}" = "gid" ]; then
idstr=$(id -g ${name})
2009-03-14 17:54:19 -04:00
else
idstr=${name}
fi
sysctl security.mac.portacl.rules=${idtype}:${idstr}:${proto}:${port} >/dev/null
out=$(check_bind ${idtype} ${name} ${proto} ${port})
2009-03-14 17:54:19 -04:00
if [ "${out}" = "${expect_with_rule}" ]; then
echo "ok ${ntest}"
elif [ "${out}" = "ok" -o "${out}" = "fl" ]; then
echo "not ok ${ntest} # '${out}' != '${expect_with_rule}'"
2009-03-14 17:54:19 -04:00
else
echo "not ok ${ntest} # unexpected output: '${out}'"
2009-03-14 17:54:19 -04:00
fi
: $(( ntest += 1 ))
2009-03-14 17:54:19 -04:00
sysctl security.mac.portacl.rules= >/dev/null
}
reserved_high=$(sysctl -n net.inet.ip.portrange.reservedhigh)
suser_exempt=$(sysctl -n security.mac.portacl.suser_exempt)
port_high=$(sysctl -n security.mac.portacl.port_high)
2009-03-14 17:54:19 -04:00
restore_settings() {
sysctl -n net.inet.ip.portrange.reservedhigh=${reserved_high} >/dev/null
sysctl -n security.mac.portacl.suser_exempt=${suser_exempt} >/dev/null
sysctl -n security.mac.portacl.port_high=${port_high} >/dev/null
}