Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor shell completion scripts + add fish comletion #556

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ EXTRA_DIST =\
CHANGELOG.md \
src/astylerc \
src/test_filesystem.cpp \
scripts/usbguard-zsh-completion \
scripts/modeline.vim \
scripts/astyle.sh \
scripts/reformat-sources.sh \
Expand Down Expand Up @@ -446,7 +445,17 @@ usbguard_LDADD=\

if ENABLE_BASH_COMPLETION
bashcompletiondir = $(BASH_COMPLETION_DIR)
dist_bashcompletion_DATA = $(top_srcdir)/scripts/bash_completion/usbguard
dist_bashcompletion_DATA = $(top_srcdir)/scripts/shell-completion/usbguard
endif

if ENABLE_ZSH_COMPLETION
zshcompletiondir = $(ZSH_COMPLETION_DIR)
dist_zshcompletion_DATA = $(top_srcdir)/scripts/shell-completion/_usbguard
endif

if ENABLE_FISH_COMPLETION
fishcompletiondir = $(FISH_COMPLETION_DIR)
dist_fishcompletion_DATA = $(top_srcdir)/scripts/shell-completion/usbguard.fish
endif

usbguard_rule_parser_SOURCES=\
Expand Down
31 changes: 31 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,30 @@ else
BASH_COMPLETION_DIR="$with_bash_completion_dir"
fi

AC_ARG_WITH([zsh-completion-dir],
AS_HELP_STRING([--with-zsh-completion-dir[=PATH]],
[Enable zsh auto-completion. @<:@default=yes@:>@]),
[], [with_zsh_completion_dir=yes])

if test "x$with_zsh_completion_dir" = "xyes"; then
ZSH_COMPLETION_DIR="$datadir/zsh/site-functions"
else
ZSH_COMPLETION_DIR="$with_zsh_completion_dir"
fi

AC_ARG_WITH([fish-completion-dir],
AS_HELP_STRING([--with-fish-completion-dir[=PATH]],
[Enable fish auto-completion. Uses pkgconfig if no path given. @<:@default=yes@:>@]),
[], [with_fish_completion_dir=yes])

if test "x$with_fish_completion_dir" = "xyes"; then
PKG_CHECK_MODULES([FISH], [fish >= 3.0.0],
[FISH_COMPLETION_DIR=$($PKG_CONFIG --variable=completionsdir fish)],
[FISH_COMPLETION_DIR="$datadir/fish/vendor_completions.d"])
else
FISH_COMPLETION_DIR="$with_fish_completion_dir"
fi

if test "x$debug" = xyes; then
CXXFLAGS="$CXXFLAGS $CXXFLAGS_DEBUG_ENABLED"
CFLAGS="$CFLAGS $CFLAGS_DEBUG_ENABLED"
Expand Down Expand Up @@ -785,6 +809,10 @@ AC_SUBST([ANALYZE_CONFIGURE_ARGS], $ac_configure_args)

AC_SUBST([BASH_COMPLETION_DIR])
AM_CONDITIONAL([ENABLE_BASH_COMPLETION], [test "x$with_bash_completion_dir" != "xno"])
AC_SUBST([ZSH_COMPLETION_DIR])
AM_CONDITIONAL([ENABLE_ZSH_COMPLETION], [test "x$with_zsh_completion_dir" != "xno"])
AC_SUBST([FISH_COMPLETION_DIR])
AM_CONDITIONAL([ENABLE_FISH_COMPLETION], [test "x$with_fish_completion_dir" != "xno"])

AM_CONDITIONAL([SYSTEMD_SUPPORT_ENABLED], [test "x$systemd" = xyes ])
AM_CONDITIONAL([DBUS_ENABLED], [test "x$with_dbus" = xyes ])
Expand All @@ -793,6 +821,7 @@ AM_CONDITIONAL([POLICYKIT_ENABLED], [test "x$with_polkit" = xyes])
AM_CONDITIONAL([FULL_TEST_SUITE_ENABLED], [test "x$full_test_suite" = xyes])
AM_CONDITIONAL([WITH_LDAP], [test "x$with_ldap" = xyes])
AM_CONDITIONAL([BASH_COMPLETION_ENABLED], [test "x$bash_completion" != xno])
AM_CONDITIONAL([FISH_ENABLED], [test "x$fish" != xno])

CXXFLAGS="$CXXFLAGS -fvisibility=hidden $COMMON_WARNING_FLAGS $WARNING_CXXFLAGS"
CFLAGS="$CFLAGS -fvisibility=hidden $COMMON_WARNING_FLAGS $WARNING_CFLAGS"
Expand Down Expand Up @@ -845,6 +874,8 @@ echo " D-Bus Busconfig: $dbus_busconfig_dir"
echo " PolicyKit Policies: $polkit_policy_dir"
echo " systemd unit dir: $systemd_unit_dir"
echo " Bash completion dir: $BASH_COMPLETION_DIR"
echo " Zsh completion dir: $ZSH_COMPLETION_DIR"
echo " Fish completion dir: $FISH_COMPLETION_DIR"
echo
echo "## Compilation Flags"
echo
Expand Down
File renamed without changes.
File renamed without changes.
86 changes: 86 additions & 0 deletions scripts/shell-completion/usbguard.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
function __fish_usbguard_complete_first_subcommand
set -l cmd (commandline -opc)
if test (count $cmd) -eq 1
return 0
end
return 1
end

set -l subcommands \
"get-parameter\tGet the value of a runtime parameter" \
"set-parameter\tSet the value of a runtime parameter" \
"list-devices\tList all USB devices recognized by the USBGuard daemon" \
"allow-device\tAuthorize a device to interact with the system" \
"block-device\tDeauthorize a device" \
"reject-device\tDeauthorize and remove a device" \
"list-rules\tList the rule set (policy) used by the USBGuard daemon" \
"append-rule\tAppend the rule to ther current rule set" \
"remove-rule\tRemove a rule identified by the rule id from the rule set" \
"generate-policy\tGenerate a rule set (policy) which authorizes currently connected USB devices" \
"watch\tWatch the IPC interface events and print them to stdout" \
"read-descriptor\tRead a USB descriptor from a file and print it in human-readable format" \
"add-user\tCreate an IPC access control file allowing user/group to use the USBGuard IPC bus" \
"remove-user\tRemove an IPC access control file associated with the user/group"

complete -c usbguard -x

for part in $subcommands
set -l cmd (string split -f 1 "\t" "$part")

complete -c usbguard -x -n "__fish_usbguard_complete_first_subcommand" -a "$cmd" -d (string split -f 2 "\t" "$part")
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "h" -l "help" -d "Show help"
end

for cmd in get-parameter set-parameter
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd; and not __fish_seen_subcommand_from InsertedDevicePolicy; and not __fish_seen_subcommand_from ImplicitPolicyTarget" -a "InsertedDevicePolicy ImplicitPolicyTarget"
end
complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter" -s "v" -l "verbose" -d "Print the previous and new attribute value"
complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from InsertedDevicePolicy" -a "block" -d "Deauthorize every present device"
complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from InsertedDevicePolicy" -a "reject" -d "Remove every present device"
complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from InsertedDevicePolicy" -a "apply-policy" -d "Evaluate the ruleset for every present device"
complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from ImplicitPolicyTarget" -a "allow" -d "Authorize the device"
complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from ImplicitPolicyTarget" -a "block" -d "Block the device"
complete -c usbguard -x -n "__fish_seen_subcommand_from set-parameter; and __fish_seen_subcommand_from ImplicitPolicyTarget" -a "reject" -d "Remove the device"

complete -c usbguard -x -n "__fish_seen_subcommand_from list-devices" -s "a" -l "allowed" -d "List allowed devices"
complete -c usbguard -x -n "__fish_seen_subcommand_from list-devices" -s "b" -l "blocked" -d "List blocked devices"
complete -c usbguard -x -n "__fish_seen_subcommand_from list-devices" -s "t" -l "tree" -d "List devices in a tree format"

for cmd in allow-device block-device reject-device
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "p" -l "permanent" -d "Make the decision permanent"
end

complete -c usbguard -x -n "__fish_seen_subcommand_from list-rules" -s "d" -l "show-devices" -d "Show all devices which are affected by the specific rule"
complete -c usbguard -x -n "__fish_seen_subcommand_from list-rules" -s "l" -l "label" -d "Only show rules having a specific label"

complete -c usbguard -x -n "__fish_seen_subcommand_from append-rule" -s "a" -l "after" -d "Append the new rule after a rule with the specified rule id"
complete -c usbguard -x -n "__fish_seen_subcommand_from append-rule" -s "t" -l "temporary" -d "Make the desicion temporary"

complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "p" -l "with-ports" -d "Generate port specific rules for all devices"
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "P" -l "no-ports-sn" -d "Don't generate port specific rules for devices without an iSerial value"
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "d" -l "devpath" -d "Only generate a rule for devices at the specified sub path of /sys"
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "t" -l "target" -d "Generate an explicit \"catch all\" rule with the specified target"
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "X" -l "no-hashes" -d "Don't generate a hash attribute for each device"
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "H" -l "hash-only" -d "Generate a hash-only policy"
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "L" -l "ldif" -d "Generate a ldif policy for LDAP"
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "b" -l "usbguardbase" -d "Generate a ldif policy for LDAP with specified base"
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "o" -l "objectclass" -d "Generate a ldif policy for LDAP with specified objectClass"
complete -c usbguard -x -n "__fish_seen_subcommand_from generate-policy" -s "n" -l "name-prefix" -d "Generate a ldif policy for LDAP with specified name prefix"

complete -c usbguard -x -n "__fish_seen_subcommand_from watch" -s "w" -l "wait" -d "Wait for IPC connection to become available"
complete -c usbguard -x -n "__fish_seen_subcommand_from watch" -s "o" -l "once" -d "Wait only when starting, if needed"
complete -c usbguard -F -r -n "__fish_seen_subcommand_from watch" -s "e" -l "exec" -d "Run an executable file located at specified path for every event"

for cmd in add-user remove-user
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -a "(__fish_complete_users)"
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -a "(__fish_complete_user_ids)"
complete -c usbguard -n "__fish_seen_subcommand_from $cmd" -s "u" -l "user" -d "Specified name representing a username or UID (default)"

complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "g" -l "group" -a "(__fish_complete_groups)"
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "g" -l "group" -a "(__fish_complete_group_ids)"
complete -c usbguard -x -n "__fish_seen_subcommand_from $cmd" -s "g" -l "group" -d "Specified name representing a group name or GID"
end
complete -c usbguard -n "__fish_seen_subcommand_from add-user" -s "p" -l "policy" -d "Policy related previleges"
complete -c usbguard -n "__fish_seen_subcommand_from add-user" -s "d" -l "devices" -d "Device related privileges"
complete -c usbguard -n "__fish_seen_subcommand_from add-user" -s "e" -l "exceptions" -d "Exceptions related privileges"
complete -c usbguard -n "__fish_seen_subcommand_from add-user" -s "P" -l "parameters" -d "Run-time parameter related privileges"