22 #include "RuleCondition.hpp" 23 #include "Exception.hpp" 25 #include "Predicates.hpp" 26 #include "Typedefs.hpp" 45 std::string toRuleString(T*
const value)
47 return value->toRuleString();
57 std::string toRuleString(
const T& value)
59 return value.toRuleString();
69 std::string DLL_PUBLIC toRuleString(
const std::string& value);
111 static const std::string targetToString(Target target);
120 static Target targetFromString(
const std::string& target_string);
128 static uint32_t targetToInteger(Target target);
138 static Target targetFromInteger(uint32_t target_integer);
177 static SetOperator setOperatorFromString(
const std::string& set_operator_string);
208 template<
class ValueType>
223 _set_operator = SetOperator::Equals;
257 return _set_operator;
267 _values.emplace_back(std::move(value));
277 _values.push_back(value);
287 return _values.size();
310 _set_operator = SetOperator::Equals;
329 const ValueType&
get()
const 334 else if (count() == 0) {
335 throw std::runtime_error(
"BUG: Accessing an empty attribute");
338 throw std::runtime_error(
"BUG: Accessing a multivalued attribute using get()");
349 const ValueType&
get(
size_t index)
const 351 return _values.at(index);
360 void set(ValueType&& value)
363 throw std::runtime_error(
"BUG: Setting single value for a multivalued attribute");
370 _values[0] = std::move(value);
380 void set(
const ValueType& value)
383 throw std::runtime_error(
"BUG: Setting single value for a multivalued attribute");
419 USBGUARD_LOG(Trace) <<
"entry:" 420 <<
" source=" << this->toRuleString()
422 bool applies =
false;
426 USBGUARD_LOG(Debug) <<
"empty source value, setting applies=true";
430 USBGUARD_LOG(Debug) <<
"set_operator=" << setOperatorToString(setOperator());
432 switch (setOperator()) {
433 case SetOperator::Match:
437 case SetOperator::AllOf:
438 applies = setSolveAllOf(_values, target._values);
441 case SetOperator::OneOf:
442 applies = setSolveOneOf(_values, target._values);
445 case SetOperator::NoneOf:
446 applies = setSolveNoneOf(_values, target._values);
449 case SetOperator::Equals:
450 applies = setSolveEquals(_values, target._values);
453 case SetOperator::EqualsOrdered:
454 applies = setSolveEqualsOrdered(_values, target._values);
458 throw USBGUARD_BUG(
"Invalid set operator value");
462 USBGUARD_LOG(Trace) <<
"return:" 463 <<
" applies=" << applies;
482 result.append(_name);
484 const bool nondefault_op = setOperator() != SetOperator::Equals;
485 const bool multiset_form = count() > 1 || nondefault_op;
489 result.append(setOperatorToString(setOperator()));
496 for (
const auto& value : _values) {
497 result.append(usbguard::toRuleString(value));
509 result.erase(result.end() - 1);
520 const std::vector<ValueType>&
values()
const 545 bool setSolveAllOf(
const std::vector<ValueType>& source_set,
const std::vector<ValueType>& target_set)
const 549 for (
auto const& source_item : source_set) {
552 for (
auto const& target_item : target_set) {
553 if (Predicates::isSubsetOf(source_item, target_item)) {
576 bool setSolveOneOf(
const std::vector<ValueType>& source_set,
const std::vector<ValueType>& target_set)
const 580 for (
auto const& source_item : source_set) {
581 for (
auto const& target_item : target_set) {
582 if (Predicates::isSubsetOf(source_item, target_item)) {
600 bool setSolveNoneOf(
const std::vector<ValueType>& source_set,
const std::vector<ValueType>& target_set)
const 604 for (
auto const& source_item : source_set) {
605 for (
auto const& target_item : target_set) {
606 if (Predicates::isSubsetOf(source_item, target_item)) {
625 bool setSolveEquals(
const std::vector<ValueType>& source_set,
const std::vector<ValueType>& target_set)
const 629 if (source_set.size() != target_set.size()) {
633 for (
auto const& source_item : source_set) {
636 for (
auto const& target_item : target_set) {
637 if (Predicates::isSubsetOf(source_item, target_item)) {
660 bool setSolveEqualsOrdered(
const std::vector<ValueType>& source_set,
const std::vector<ValueType>& target_set)
const 664 if (source_set.size() != target_set.size()) {
668 for (
size_t i = 0; i < source_set.size(); ++i) {
669 if (!Predicates::isSubsetOf(source_set[i], target_set[i])) {
678 SetOperator _set_operator;
681 std::vector<ValueType> _values;
702 Rule(
const Rule& rhs);
710 const Rule& operator=(
const Rule& rhs);
717 void setRuleID(uint32_t rule_id);
724 uint32_t getRuleID()
const;
741 Target getTarget()
const;
749 void setDeviceID(
const USBDeviceID& value);
757 const USBDeviceID& getDeviceID()
const;
764 const Attribute<USBDeviceID>& attributeDeviceID()
const;
771 Attribute<USBDeviceID>& attributeDeviceID();
779 void setSerial(
const std::string& value);
787 const std::string& getSerial()
const;
794 const Attribute<std::string>& attributeSerial()
const;
801 Attribute<std::string>& attributeSerial();
814 void setLabel(
const std::string& value);
822 const std::string& getLabel()
const;
829 const Attribute<std::string>& attributeLabel()
const;
836 Attribute<std::string>& attributeLabel();
848 void setWithConnectType(
const std::string& value);
856 const std::string& getWithConnectType()
const;
863 const Attribute<std::string>& attributeWithConnectType()
const;
870 Attribute<std::string>& attributeWithConnectType();
878 void setName(
const std::string& value);
886 const std::string& getName()
const;
893 const Attribute<std::string>& attributeName()
const;
900 Attribute<std::string>& attributeName();
908 void setHash(
const std::string& value);
916 const std::string& getHash()
const;
923 const Attribute<std::string>& attributeHash()
const;
930 Attribute<std::string>& attributeHash();
938 void setParentHash(
const std::string& value);
946 const std::string& getParentHash()
const;
953 const Rule::Attribute<std::string>& attributeParentHash()
const;
960 Rule::Attribute<std::string>& attributeParentHash();
970 void setViaPort(
const std::string& value);
978 const std::string& getViaPort()
const;
985 const Attribute<std::string>& attributeViaPort()
const;
992 Attribute<std::string>& attributeViaPort();
1004 const Attribute<USBInterfaceType>& attributeWithInterface()
const;
1016 Attribute<USBInterfaceType>& attributeWithInterface();
1023 const Attribute<RuleCondition>& attributeConditions()
const;
1030 Attribute<RuleCondition>& attributeConditions();
1038 bool appliesTo(std::shared_ptr<const Rule> rhs)
const;
1046 bool appliesTo(
const Rule& rhs)
const;
1055 bool appliesTo(
const Rule& rhs);
1062 bool isImplicit()
const;
1071 operator bool()
const;
1083 std::string toString(
bool invalid =
false,
bool hide_serial =
false)
const;
1097 void updateMetaDataCounters(
bool applied =
true,
bool evaluated =
false);
1104 std::unique_ptr<RulePrivate>&
internal();
1111 const std::unique_ptr<RulePrivate>&
internal()
const;
1125 static Rule fromString(
const std::string& rule_string);
1128 std::unique_ptr<RulePrivate> d_pointer;
Target
Enumeration of possible rule targets.
Definition: Rule.hpp:86
std::vector< ValueType > & values()
Returns mutable vector of attribute values.
Definition: Rule.hpp:530
static const uint32_t ImplicitID
Sequence number of the implicit target rule.
Definition: Rule.hpp:199
static const uint32_t RootID
Sequence number of the (fake) root rule.
Definition: Rule.hpp:182
Represents USB device in USBGuard.
Definition: Device.hpp:41
const ValueType & get(size_t index) const
Returns value at given index in values vector.
Definition: Rule.hpp:349
size_t count() const
Returns number of values of this attribute.
Definition: Rule.hpp:285
const ValueType & get() const
Returns value of single-value attribute.
Definition: Rule.hpp:329
void set(ValueType &&value)
Sets value in single-value attribute using move semantics.
Definition: Rule.hpp:360
SetOperator
Defines set operators that can be used in the rules.
Definition: Rule.hpp:143
void append(ValueType &&value)
Appends new value into attribute values using move semantics.
Definition: Rule.hpp:265
static const uint32_t DefaultID
Sequence number assigned to default constructed rules. Cannot be used for searching.
Definition: Rule.hpp:188
Specifies which devices to match or what condition have to be met for the rule to be aplicable.
Definition: Rule.hpp:209
void set(const ValueType &value)
Sets value in single-value attribute.
Definition: Rule.hpp:380
std::string getName() const
Returns name of the attribute.
Definition: Rule.hpp:318
std::string toRuleString() const
Returns string representation of this attribute.
Definition: Rule.hpp:479
void append(const ValueType &value)
Appends new value into attribute values.
Definition: Rule.hpp:275
Attribute(const char *name)
Constructs new rule attribute with a given name and default set operator.
Definition: Rule.hpp:220
Determines whether USB device mathing specified criteria should be authorized, deauthorized or remove...
Definition: Rule.hpp:77
static const uint32_t LastID
Sequence number for specifying that the last rule in the ruleset should be used in context of the ope...
Definition: Rule.hpp:194
void setSetOperator(SetOperator op)
Sets set operator.
Definition: Rule.hpp:245
SetOperator setOperator() const
Returns set operator of this attribute.
Definition: Rule.hpp:255
bool empty() const
Checks whether attribute does not contain any values.
Definition: Rule.hpp:296
void clear()
Clears all values of this attribute and sets set operator to default.
Definition: Rule.hpp:307
const std::vector< ValueType > & values() const
Returns imutable vector of attribute values.
Definition: Rule.hpp:520
bool appliesTo(const Attribute< ValueType > &target) const
Checks whether attribute applies to target attribute.
Definition: Rule.hpp:417
void set(const std::vector< ValueType > &values, SetOperator op)
Sets given values and set operator.
Definition: Rule.hpp:400