xref: /freebsd/crypto/openssh/regress/agent-getpeereid.sh (revision 5e386598a6d77973b93c073080f0cc574edda9e2)
1#	$OpenBSD: agent-getpeereid.sh,v 1.7 2016/09/26 21:34:38 bluhm Exp $
2#	Placed in the Public Domain.
3
4tid="disallow agent attach from other uid"
5
6UNPRIV=nobody
7ASOCK=${OBJ}/agent
8SSH_AUTH_SOCK=/nonexistent
9
10if config_defined HAVE_GETPEEREID HAVE_GETPEERUCRED HAVE_SO_PEERCRED ; then
11	:
12else
13	echo "skipped (not supported on this platform)"
14	exit 0
15fi
16case "x$SUDO" in
17	xsudo) sudo=1;;
18	xdoas) ;;
19	x)
20		echo "need SUDO to switch to uid $UNPRIV"
21		exit 0 ;;
22	*)
23		echo "unsupported $SUDO - "doas" and "sudo" are allowed"
24		exit 0 ;;
25esac
26
27trace "start agent"
28eval `${SSHAGENT} -s -a ${ASOCK}` > /dev/null
29r=$?
30if [ $r -ne 0 ]; then
31	fail "could not start ssh-agent: exit code $r"
32else
33	chmod 644 ${SSH_AUTH_SOCK}
34
35	ssh-add -l > /dev/null 2>&1
36	r=$?
37	if [ $r -ne 1 ]; then
38		fail "ssh-add failed with $r != 1"
39	fi
40	if test -z "$sudo" ; then
41		# doas
42		${SUDO} -n -u ${UNPRIV} ssh-add -l 2>/dev/null
43	else
44		# sudo
45		< /dev/null ${SUDO} -S -u ${UNPRIV} ssh-add -l 2>/dev/null
46	fi
47	r=$?
48	if [ $r -lt 2 ]; then
49		fail "ssh-add did not fail for ${UNPRIV}: $r < 2"
50	fi
51
52	trace "kill agent"
53	${SSHAGENT} -k > /dev/null
54fi
55
56rm -f ${OBJ}/agent
57