xref: /freebsd/crypto/openssh/regress/agent-getpeereid.sh (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1#	$OpenBSD: agent-getpeereid.sh,v 1.13 2021/09/01 00:50:27 dtucker 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	skip "skipped (not supported on this platform)"
14fi
15if test "x$USER" = "xroot"; then
16	skip "skipped (running as root)"
17fi
18case "x$SUDO" in
19	xsudo) sudo=1;;
20	xdoas|xdoas\ *) ;;
21	x)
22		skip "need SUDO to switch to uid $UNPRIV" ;;
23	*)
24		skip "unsupported $SUDO - "doas" and "sudo" are allowed" ;;
25esac
26
27trace "start agent"
28eval `${SSHAGENT} ${EXTRA_AGENT_ARGS} -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	${SSHADD} -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} ${SSHADD} -l 2>/dev/null
43	else
44		# sudo
45		< /dev/null ${SUDO} -S -u ${UNPRIV} ${SSHADD} -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