Debian Codegraph @

debian/tests/pkexec

raw
1 #!/bin/sh
2 set -eu
3
4 assert_fail() {
5 if $1 2>&1; then
6 echo "FAIL: command '$1' unexpectedly succeeded" >&2
7 exit 1
8 fi
9 }
10
11 assert_eq() {
12 if [ "$1" != "$2" ]; then
13 echo "FAIL: expected: '$2' actual: '$1'" >&2
14 exit 1
15 fi
16 }
17
18 assert_in() {
19 if ! echo "$2" | grep -q "$1"; then
20 echo "FAIL: '$1' not found in:" >&2
21 echo "$2" >&2
22 exit 1
23 fi
24 }
25
26 stop_polkitd () {
27 if [ -d /run/systemd/system ]; then
28 systemctl stop polkit.service
29 else
30 # On non-systemd systems, polkitd is started as a traditional D-Bus
31 # system service and is not managed by a service manager, so this is
32 # the best we can do
33 start-stop-daemon --stop --oknodo --quiet --exec /usr/lib/polkit-1/polkitd || true
34 fi
35 }
36
37 # lxc autopkgtest containers don't always have dbus already running
38 sudo service dbus start || :
39
40 if ! [ -e /run/dbus/system_bus_socket ]; then
41 echo "SKIP: dbus system bus not running and could not be started"
42 exit 77
43 fi
44
45 echo "TEST: pkexec"
46 # Members of the sudo group are allowed to do anything
47 sudo tee /etc/polkit-1/rules.d/00-test-allow.rules >/dev/null <<EOF
48 polkit.addRule(function(action, subject) {
49 if (subject.isInGroup("sudo")) {
50 return polkit.Result.YES;
51 }
52 return polkit.Result.NOT_HANDLED;
53 });
54 EOF
55 # Anyone else is not allowed to do anything (deny comes after allow in
56 # alphabetical order, so the other rule wins)
57 sudo tee /etc/polkit-1/rules.d/00-test-deny.rules >/dev/null <<EOF
58 polkit.addRule(function(action, subject) {
59 return polkit.Result.NO;
60 });
61 EOF
62
63 # make sure the policy is reloaded, without any race conditions
64 stop_polkitd
65
66 # sudo group member can pkexec because we installed policy that says so
67 assert_eq "$(pkexec id -u)" "0"
68 assert_fail "sudo -u daemon pkexec id"
69 echo "OK"
70 sudo rm -f /etc/polkit-1/rules.d/00-test-allow.rules
71 sudo rm -f /etc/polkit-1/rules.d/00-test-deny.rules