USBGuard
Software framework that protects your computer against rogue USB devices by implementing basic whitelisting and blacklisting capabilities.
Device.hpp
1 //
2 // Copyright (C) 2017 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: Daniel Kopecek <dkopecek@redhat.com>
18 //
19 #pragma once
20 
21 #include "Rule.hpp"
22 #include "Typedefs.hpp"
23 #include "USB.hpp"
24 
25 #include <istream>
26 #include <mutex>
27 #include <string>
28 #include <memory>
29 
30 #include <cstddef>
31 #include <cstdint>
32 
33 namespace usbguard
34 {
35  class DeviceManager;
36  class DevicePrivate;
37 
41  class DLL_PUBLIC Device
42  {
43  public:
44 
58  Device(DeviceManager& manager);
59 
63  virtual ~Device();
64 
70  Device(const Device& rhs);
71 
78  const Device& operator=(const Device& rhs);
79 
87  DeviceManager& manager() const;
88 
94  std::mutex& refDeviceMutex();
95 
111  std::shared_ptr<Rule> getDeviceRule(bool with_port = true, bool with_parent_hash = true, bool match_rule = false);
112 
119  std::string hashString(const std::string& value) const;
120 
129  void initializeHash();
130 
137  void updateHash(const void* ptr, size_t size);
138 
147  void updateHash(std::istream& descriptor_stream, size_t expected_size);
148 
154  std::string finalizeHash();
155 
163  const std::string& getHash() const;
164 
170  void setParentHash(const std::string& hash);
171 
177  void setID(uint32_t id);
178 
184  uint32_t getID() const;
185 
191  void setParentID(uint32_t id);
192 
198  uint32_t getParentID() const;
199 
206  void setTarget(Rule::Target target);
207 
214  Rule::Target getTarget() const;
215 
223  void setName(const std::string& name);
224 
230  const std::string& getName() const;
231 
237  void setDeviceID(const USBDeviceID& device_id);
238 
244  const USBDeviceID& getDeviceID() const;
245 
253  void setPort(const std::string& port);
254 
260  const std::string& getPort() const;
261 
269  void setSerial(const std::string& serial_number);
270 
276  const std::string& getSerial() const;
277 
285  void setConnectType(const std::string& connect_type);
286 
292  const std::string& getConnectType() const;
293 
301  std::vector<USBInterfaceType>& refMutableInterfaceTypes();
302 
308  const std::vector<USBInterfaceType>& getInterfaceTypes() const;
309 
315  virtual bool isController() const = 0;
316 
322  virtual std::string getSystemName() const = 0;
323 
332  void loadDeviceDescriptor(USBDescriptorParser* parser, const USBDescriptor* descriptor);
333 
342  void loadConfigurationDescriptor(USBDescriptorParser* parser, const USBDescriptor* descriptor);
343 
352  void loadInterfaceDescriptor(USBDescriptorParser* parser, const USBDescriptor* descriptor);
353 
365  void loadEndpointDescriptor(USBDescriptorParser* parser, const USBDescriptor* descriptor);
366 
367  private:
368  std::unique_ptr<DevicePrivate> d_pointer;
369  };
370 } /* namespace usbguard */
371 
372 /* vim: set ts=2 sw=2 et */
Target
Enumeration of possible rule targets.
Definition: Rule.hpp:86
Represents USB device in USBGuard.
Definition: Device.hpp:41
Manages and keeps track of active USB devices.
Definition: DeviceManager.hpp:44
Parses USB descriptors.
Definition: USB.hpp:432
Represents ID of a USB device.
Definition: USB.hpp:158
Definition: USB.hpp:91