From 947278438a5cc469a263a03cd15f73f40492d654 Mon Sep 17 00:00:00 2001 From: Zoltan Fridrich Date: Wed, 30 Jun 2021 13:26:48 +0200 Subject: [PATCH] Add insertRule method * add ruleset names * add insertRule method into USBGuard interface * add ruleset option to append-rule command --- doc/man/usbguard.1.adoc | 8 ++- scripts/bash_completion/usbguard | 5 +- scripts/usbguard-zsh-completion | 1 + src/CLI/usbguard-append-rule.cpp | 23 +++++-- src/DBus/DBusBridge.cpp | 15 ++++- src/DBus/DBusInterface.xml | 24 +++++++ src/DBus/org.usbguard1.policy | 9 +++ src/Daemon/Daemon.cpp | 19 ++++++ src/Daemon/Daemon.hpp | 1 + src/Daemon/FileRuleSet.cpp | 2 + src/Daemon/RuleSetFactory.cpp | 2 +- src/Library/IPC/Policy.proto | 18 +++++ src/Library/IPCClientPrivate.cpp | 21 ++++-- src/Library/IPCClientPrivate.hpp | 4 +- src/Library/IPCPrivate.cpp | 13 ++-- src/Library/IPCServerPrivate.cpp | 28 +++++++- src/Library/IPCServerPrivate.hpp | 1 + src/Library/public/usbguard/IPCClient.cpp | 8 ++- src/Library/public/usbguard/IPCClient.hpp | 8 ++- src/Library/public/usbguard/Interface.hpp | 19 ++++++ src/Library/public/usbguard/Policy.cpp | 77 ++++++++++++++-------- src/Library/public/usbguard/Policy.hpp | 37 +++++++++++ src/Library/public/usbguard/RuleParser.cpp | 4 +- src/Library/public/usbguard/RuleSet.cpp | 23 +++++++ src/Library/public/usbguard/RuleSet.hpp | 6 ++ 25 files changed, 319 insertions(+), 57 deletions(-) diff --git a/doc/man/usbguard.1.adoc b/doc/man/usbguard.1.adoc index 84b4f9bb..2e0d37b2 100644 --- a/doc/man/usbguard.1.adoc +++ b/doc/man/usbguard.1.adoc @@ -157,7 +157,13 @@ Append the 'rule' to the current rule set. Available options: *-a, --after* 'id':: - Append the new rule after a rule with the specified rule 'id'. + Append the new rule after a rule with the specified rule 'id' + instead of appending it at the end of the rule set. + If 'id' is 0, then the rule is appended to the beginning + of the rule set. + +*-r, --ruleset* 'prefix':: + Append the new rule into a rule set with specified prefix. *-t, --temporary*:: Make the decision temporary. The rule policy file will not be updated. diff --git a/scripts/bash_completion/usbguard b/scripts/bash_completion/usbguard index 721bf1d8..8b50b745 100755 --- a/scripts/bash_completion/usbguard +++ b/scripts/bash_completion/usbguard @@ -303,11 +303,14 @@ function _usbguard() if _usbguard_contains "-a --after" $prev; then opts=$(_usbguard_get_rules || :) + elif _usbguard_contains "-r --ruleset" $prev; then + return 0 + elif ! _usbguard_in_option && [[ $args -eq 2 ]]; then return 0 else - opts="$opts -a --after -t --temporary" + opts="$opts -a --after -r --ruleset -t --temporary" fi ;; diff --git a/scripts/usbguard-zsh-completion b/scripts/usbguard-zsh-completion index d98089a0..48f5493c 100644 --- a/scripts/usbguard-zsh-completion +++ b/scripts/usbguard-zsh-completion @@ -113,6 +113,7 @@ case $state in '[rule]' _command_args=( \ '(--after)--after [Append the new rule after a rule with the specified id instead of appending it]' \ + '(--ruleset)--ruleset [Append the new rule into a rule set with specified prefix.]' \ '(--help)--help[Show help]' ) ;; diff --git a/src/CLI/usbguard-append-rule.cpp b/src/CLI/usbguard-append-rule.cpp index beb5f49e..753dddba 100644 --- a/src/CLI/usbguard-append-rule.cpp +++ b/src/CLI/usbguard-append-rule.cpp @@ -29,11 +29,12 @@ namespace usbguard { - static const char* options_short = "ha:t"; + static const char* options_short = "ha:r:t"; static const struct ::option options_long[] = { { "help", no_argument, nullptr, 'h' }, { "after", required_argument, nullptr, 'a' }, + { "ruleset", required_argument, nullptr, 'r' }, { "temporary", no_argument, nullptr, 't' }, { nullptr, 0, nullptr, 0 } }; @@ -43,17 +44,21 @@ namespace usbguard stream << " Usage: " << usbguard_arg0 << " append-rule [OPTIONS] " << std::endl; stream << std::endl; stream << " Options:" << std::endl; - stream << " -a, --after Append the new rule after a rule with the specified id" << std::endl; - stream << " instead of appending it at the end of the rule set." << std::endl; - stream << " -t, --temporary Make the decision temporary. The rule policy file will not" << std::endl; - stream << " be updated." << std::endl; - stream << " -h, --help Show this help." << std::endl; + stream << " -a, --after Append the new rule after a rule with the specified id" << std::endl; + stream << " instead of appending it at the end of the rule set." << std::endl; + stream << " If 'id' is 0, then the rule is appended to the beginning" << std::endl; + stream << " of the rule set." << std::endl; + stream << " -r, --ruleset Append the new rule into a ruleset with specified prefix." << std::endl; + stream << " -t, --temporary Make the decision temporary. The rule policy file will not" << std::endl; + stream << " be updated." << std::endl; + stream << " -h, --help Show this help." << std::endl; stream << std::endl; } int usbguard_append_rule(int argc, char* argv[]) { uint32_t parent_id = usbguard::Rule::LastID; + std::string ruleset; bool permanent = true; int opt = 0; @@ -67,6 +72,10 @@ namespace usbguard parent_id = std::stoul(optarg); break; + case 'r': + ruleset = optarg; + break; + case 't': permanent = false; break; @@ -89,7 +98,7 @@ namespace usbguard usbguard::IPCClient ipc(/*connected=*/true); const std::string rule_spec = argv[0]; - const uint32_t id = ipc.appendRule(rule_spec, parent_id, permanent); + const uint32_t id = ipc.insertRule(rule_spec, parent_id, ruleset, permanent); std::cout << id << std::endl; return EXIT_SUCCESS; } diff --git a/src/DBus/DBusBridge.cpp b/src/DBus/DBusBridge.cpp index 76471e7b..fdef0d58 100644 --- a/src/DBus/DBusBridge.cpp +++ b/src/DBus/DBusBridge.cpp @@ -135,6 +135,19 @@ namespace usbguard return; } + if (method_name == "insertRule") { + const char* rule_spec_cstr = nullptr; + uint32_t parent_id = 0; + const char* ruleset_cstr = nullptr; + gboolean temporary = false; + g_variant_get(parameters, "(&sub)", &rule_spec_cstr, &parent_id, &ruleset_cstr, &temporary); + std::string rule_spec(rule_spec_cstr); + std::string ruleset(ruleset_cstr); + const uint32_t rule_id = insertRule(rule_spec, parent_id, ruleset, !temporary); + g_dbus_method_invocation_return_value(invocation, g_variant_new("(u)", rule_id)); + return; + } + if (method_name == "appendRule") { const char* rule_spec_cstr = nullptr; uint32_t parent_id = 0; @@ -337,11 +350,9 @@ namespace usbguard g_variant_builder_add(builder, "{ss}", "with-interface", with_interface_string.c_str()); - g_variant_builder_add(builder, "{ss}", "with-connect-type", device_rule.getWithConnectType().c_str()); - return builder; } } /* namespace usbguard */ diff --git a/src/DBus/DBusInterface.xml b/src/DBus/DBusInterface.xml index 3b78fe9c..91c996e9 100644 --- a/src/DBus/DBusInterface.xml +++ b/src/DBus/DBusInterface.xml @@ -74,6 +74,30 @@ + + + + + + + + +