USBGuard
Software framework that protects your computer against rogue USB devices by implementing basic whitelisting and blacklisting capabilities.
RuleSet.hpp
1 //
2 // Copyright (C) 2015 Red Hat, Inc.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // Authors: Radovan Sroka <rsroka@redhat.com>
18 //
19 #pragma once
20 
21 #include "usbguard/Typedefs.hpp"
22 #include "usbguard/Rule.hpp"
23 
24 #include <istream>
25 #include <ostream>
26 #include <mutex>
27 
28 namespace usbguard
29 {
30  class Interface;
31  class DLL_PUBLIC RuleSet
32  {
33  public:
34 
35  RuleSet(Interface* const interface_ptr);
36  RuleSet(const RuleSet& rhs);
37  const RuleSet& operator=(const RuleSet& rhs);
38  virtual ~RuleSet() = default;
39 
40  virtual void load() = 0;
41  virtual void save() = 0;
42 
43  void serialize(std::ostream& stream) const;
44 
45  void setDefaultTarget(Rule::Target target);
46  Rule::Target getDefaultTarget() const;
47  uint32_t appendRule(const Rule& rule, uint32_t parent_id = Rule::LastID, bool lock = true);
48  uint32_t upsertRule(const Rule& match_rule, const Rule& new_rule, bool parent_insensitive = false);
49  std::shared_ptr<Rule> getRule(uint32_t id);
50  bool removeRule(uint32_t id);
51 
52  virtual std::shared_ptr<Rule> getFirstMatchingRule(std::shared_ptr<const Rule> device_rule, uint32_t from_id = 1) const;
53 
54  std::vector<std::shared_ptr<const Rule>> getRules();
55  uint32_t assignID(std::shared_ptr<Rule> rule);
56  uint32_t assignID();
57 
58  void setWritable();
59  void clearWritable();
60  bool isWritable();
61 
62  protected:
63  mutable std::mutex _op_mutex; /* mutex for operations on the rule set */
64 
65  bool _writable{false};
66 
67  Interface* _interface_ptr{nullptr};
68  Rule::Target _default_target;
69  Atomic<uint32_t> _id_next;
70  std::vector<std::shared_ptr<Rule>> _rules;
71  };
72 
73 } /* namespace usbguard */
74 
75 /* vim: set ts=2 sw=2 et */
Target
Enumeration of possible rule targets.
Definition: Rule.hpp:86
Definition: RuleSet.hpp:31
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
Allows to receive signals and to communicate with the USBGuard daemon.
Definition: Interface.hpp:40