From 195afa7f8771a3468e3f782bc0db9b3f5e751306 Mon Sep 17 00:00:00 2001 From: Alexis Janon <98706738+ajanon@users.noreply.github.com> Date: Tue, 20 Dec 2022 11:39:06 +0100 Subject: [PATCH] [feat] add polkit policy file generation script 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. --- scripts/build-polkit-policy.sh | 89 ++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 scripts/build-polkit-policy.sh diff --git a/scripts/build-polkit-policy.sh b/scripts/build-polkit-policy.sh new file mode 100755 index 0000000..628ba90 --- /dev/null +++ b/scripts/build-polkit-policy.sh @@ -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 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 to the swhkd binary when installed.\n" + printf " Defaults to '%s'.\n" "${DEFAULT_SWHKD_PATH}" + printf " --action-id= Polkit action id to use.\n" + printf " Defaults to '%s'.\n" "${DEFAULT_ACTION_ID}" + printf " --message= 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 + + + + + ${MESSAGE} + + no + no + yes + + ${SWHKD_PATH} + + +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