Skip to content

Commit

Permalink
[feat] add polkit policy file generation script
Browse files Browse the repository at this point in the history
The build-polkit-policy.sh script (in the newly added 'scripts' folder) builds a
polkit policy file for swhkd. It has a number of optional arguments to make it
easy to extend and adapt to different use cases. The output path, the swhkd
binary path, and even the custom authentication message and the action id are
configurable.
  • Loading branch information
ajanon committed Dec 20, 2022
1 parent 031b1e1 commit 195afa7
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions scripts/build-polkit-policy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/sh

###############
# Defaults
###############

readonly DEFAULT_SWHKD_PATH="/usr/bin/swhkd"
readonly DEFAULT_POLICY_PATH="com.github.swhkd.pkexec.policy"
readonly DEFAULT_MESSAGE="Authentication is required to run Simple Wayland Hotkey Daemon"
readonly DEFAULT_ACTION_ID="com.github.swhkd.pkexec"

###############
# Init
###############

print_help() {
printf "Usage: build-polkit-policy [OPTIONS]\n\n"
printf "Generates a polkit policy file for swhkd.\n\n"
printf "Optional Arguments:\n"
printf " --policy-path=<path> Path to save the policy file to.\n"
printf " If set to '-', this tool will output to stdout instead.\n"
printf " Defaults to '%s'.\n" "${DEFAULT_POLICY_PATH}"
printf " --swhkd-path=<path> Path to the swhkd binary when installed.\n"
printf " Defaults to '%s'.\n" "${DEFAULT_SWHKD_PATH}"
printf " --action-id=<id> Polkit action id to use.\n"
printf " Defaults to '%s'.\n" "${DEFAULT_ACTION_ID}"
printf " --message=<text> Custom authentication message.\n"
printf " Defaults to '%s'\n" "${DEFAULT_MESSAGE}"
printf " -h|--help Show this help.\n"
}

while [ -n "$1" ]; do
case "$1" in
--policy-path=*)
POLICY_PATH=${1#*=}
shift
;;
--swhkd-path=*)
SWHKD_PATH=${1#*=}
shift
;;
--action-id=*)
ACTION_ID=${1#*=}
shift
;;
--message=*)
MESSAGE=${1#*=}
shift
;;
-h|--help)
print_help
exit 0
;;
*)
printf "Unknown option '%s'. Aborting.\n" "$1"
exit 1
;;
esac
done

print_policy() {
cat << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN" "http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>
<action id="${ACTION_ID}">
<message>${MESSAGE}</message>
<defaults>
<allow_any>no</allow_any>
<allow_inactive>no</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">${SWHKD_PATH}</annotate>
</action>
</policyconfig>
EOF
}

# No local variables in POSIX sh, so just set these globally
POLICY_PATH="${POLICY_PATH:-${DEFAULT_POLICY_PATH}}"
SWHKD_PATH="${SWHKD_PATH:-${DEFAULT_SWHKD_PATH}}"
ACTION_ID="${ACTION_ID:-${DEFAULT_ACTION_ID}}"
MESSAGE="${MESSAGE:-${DEFAULT_MESSAGE}}"

if [ "${POLICY_PATH}" = "-" ]; then
print_policy
else
print_policy > "${POLICY_PATH}"
fi

0 comments on commit 195afa7

Please sign in to comment.