diff --git a/usbguard-notifier-rhbz1972505-merge-notifications.patch b/usbguard-notifier-rhbz1972505-merge-notifications.patch new file mode 100644 index 0000000..7e1719a --- /dev/null +++ b/usbguard-notifier-rhbz1972505-merge-notifications.patch @@ -0,0 +1,156 @@ +diff --git a/src/Notifier.cpp b/src/Notifier.cpp +index a98ca31..6653b93 100644 +--- a/src/Notifier.cpp ++++ b/src/Notifier.cpp +@@ -25,6 +25,7 @@ + #include + #include + #include ++#include + + #include + #include +@@ -41,6 +42,14 @@ Notifier::Notifier(const std::string& app_name) : + _ser.setFileName(path); + } + ++Notifier::~Notifier() ++{ ++ for (auto t : _countdownThreads) { ++ t->join(); ++ delete t; ++ } ++} ++ + void Notifier::DevicePolicyChanged( + uint32_t id, + usbguard::Rule::Target target_old, +@@ -53,6 +62,20 @@ void Notifier::DevicePolicyChanged( + + const std::string target_old_str = Rule::targetToString(target_old); + const std::string target_new_str = Rule::targetToString(target_new); ++ ++ DevicePresenceInfo info = getDevicePresenceObject(id); ++ if (info.isInitialized) { ++ if (info.target == target_old || ++ device_rule.substr(target_new_str.size()) == info.device_rule.substr(target_old_str.size())) { ++ info.target = target_new; ++ sendDevicePresenceNotification(info); ++ return; ++ } else { ++ NOTIFIER_LOG() << "DevicePolicyChanged and DevicePresenceChanged " << ++ "with same id do not share the same rule."; ++ } ++ } ++ + Rule rule = Rule::fromString(device_rule); + + std::ostringstream body; +@@ -70,7 +93,7 @@ void Notifier::DevicePolicyChanged( + } + + void Notifier::DevicePresenceChanged( +- uint32_t /*id*/, ++ uint32_t id, + usbguard::DeviceManager::EventType event, + usbguard::Rule::Target target, + const std::string& device_rule) +@@ -78,9 +101,41 @@ void Notifier::DevicePresenceChanged( + using namespace usbguard; + NOTIFIER_LOG() << "Device presence changed signal"; + +- const std::string event_str = DeviceManager::eventTypeToString(event); +- const std::string target_str = Rule::targetToString(target); +- Rule rule = Rule::fromString(device_rule); ++ _deviceNotifications.emplace(std::make_pair(id, DevicePresenceInfo(event, target, device_rule))); ++ std::thread* t = new std::thread( [this, id] { sendDevicePresenceCountdownCallback(id); } ); ++ _countdownThreads.push_back(t); ++} ++ ++Notifier::DevicePresenceInfo Notifier::getDevicePresenceObject(uint32_t id) ++{ ++ DevicePresenceInfo info; ++ _mtx.lock(); ++ auto it = _deviceNotifications.find(id); ++ if (it != _deviceNotifications.end()) { ++ info = it->second; ++ _deviceNotifications.erase(it); ++ } ++ _mtx.unlock(); ++ return info; ++} ++ ++void Notifier::sendDevicePresenceCountdownCallback(uint32_t id) ++{ ++ std::this_thread::sleep_for(std::chrono::milliseconds(_kMillisecondsDevicePolicyWait)); ++ ++ DevicePresenceInfo info = getDevicePresenceObject(id); ++ if (info.isInitialized) { ++ sendDevicePresenceNotification(info); ++ } ++} ++ ++void Notifier::sendDevicePresenceNotification(DevicePresenceInfo& info) ++{ ++ using namespace usbguard; ++ ++ const std::string event_str = DeviceManager::eventTypeToString(info.event); ++ const std::string target_str = Rule::targetToString(info.target); ++ Rule rule = Rule::fromString(info.device_rule); + + std::ostringstream body; + body << event_str << ' ' << rule.getName() << ": " << target_str; +diff --git a/src/Notifier.hpp b/src/Notifier.hpp +index 92f5c03..e420adb 100644 +--- a/src/Notifier.hpp ++++ b/src/Notifier.hpp +@@ -26,6 +26,9 @@ + #include + #include + ++#include ++#include ++ + namespace usbguardNotifier + { + +@@ -33,6 +36,7 @@ class Notifier : public usbguard::IPCClient + { + public: + explicit Notifier(const std::string& app_name); ++ ~Notifier(); + + void DevicePolicyChanged( + uint32_t id, +@@ -53,8 +57,31 @@ class Notifier : public usbguard::IPCClient + const std::string& value_new); + */ + private: ++ struct DevicePresenceInfo { ++ bool isInitialized = false; ++ usbguard::DeviceManager::EventType event; ++ usbguard::Rule::Target target; ++ std::string device_rule; ++ ++ DevicePresenceInfo(const usbguard::DeviceManager::EventType& e, ++ const usbguard::Rule::Target& t, ++ const std::string& r): isInitialized(true), event(e), target(t), device_rule(r) {} ++ ++ DevicePresenceInfo() : event(), target(), device_rule() {} ++ }; ++ ++ DevicePresenceInfo getDevicePresenceObject(uint32_t id); ++ ++ void sendDevicePresenceCountdownCallback(uint32_t id); ++ ++ void sendDevicePresenceNotification(DevicePresenceInfo& info); ++ + notify::Notify _lib; + Serializer _ser; ++ std::map _deviceNotifications; ++ std::mutex _mtx; ++ std::vector _countdownThreads; ++ const int _kMillisecondsDevicePolicyWait = 500; + }; + + } // namespace usbguardNotifier diff --git a/usbguard-notifier.spec b/usbguard-notifier.spec index a3d2a18..7e99fac 100644 --- a/usbguard-notifier.spec +++ b/usbguard-notifier.spec @@ -1,6 +1,6 @@ Name: usbguard-notifier Version: 0.0.6 -Release: 3%{?dist} +Release: 4%{?dist} Summary: A tool for detecting usbguard policy and device presence changes License: GPLv2+ @@ -20,6 +20,8 @@ BuildRequires: catch1-devel BuildRequires: execstack BuildRequires: systemd-rpm-macros +Patch0: usbguard-notifier-rhbz1972505-merge-notifications.patch + %description USBGuard Notifier software framework detects usbguard policy modifications as well as device presence changes and displays them as pop-up notifications. @@ -27,6 +29,8 @@ as well as device presence changes and displays them as pop-up notifications. %prep %setup -q +%patch0 -p1 -b .merge-notifications + %build mkdir -p ./m4 autoreconf -i -f -v --no-recursive ./ @@ -69,6 +73,10 @@ execstack -c %{buildroot}%{_bindir}/usbguard-notifier-cli %changelog +* Wed Jun 16 2021 Attila Lakatos - 0.0.6-4 +- Merge notifications when inserting a usb device + resolves: rhbz#1972505 + * Fri Jan 15 2021 Radovan Sroka - 0.0.6-3 - Rebuild with the usbguard 1.0.0 - soname bump