USBGuard
Software framework that protects your computer against rogue USB devices by implementing basic whitelisting and blacklisting capabilities.
DeviceManager.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 "Device.hpp"
22 #include "Rule.hpp"
23 #include "RuleSet.hpp"
24 #include "Typedefs.hpp"
25 
26 #include <memory>
27 #include <mutex>
28 #include <string>
29 #include <vector>
30 
31 #include <cstdint>
32 
33 namespace usbguard
34 {
35  class DeviceManagerHooks;
36  class DeviceManagerPrivate;
37 
44  class DLL_PUBLIC DeviceManager
45  {
46  public:
47 
51  enum class EventType {
52  Present = 0,
53  Insert = 1,
54  Update = 2,
55  Remove = 3,
56  };
57 
61  enum class AuthorizedDefaultType {
62  Keep = -128,
63  Wired = -1,
65  None = 0,
66  All = 1,
67  Internal = 2,
71  };
72 
80  static uint32_t eventTypeToInteger(EventType event);
81 
90  static EventType eventTypeFromInteger(uint32_t event_integer);
91 
100  static std::string eventTypeToString(EventType event);
101 
111  static int32_t authorizedDefaultTypeToInteger(AuthorizedDefaultType authorized_default);
112 
123  static AuthorizedDefaultType authorizedDefaultTypeFromInteger(int32_t authorized_default_integer);
124 
135  static AuthorizedDefaultType authorizedDefaultTypeFromString(const std::string& authorized_default_string);
136 
147  static const std::string authorizedDefaultTypeToString(AuthorizedDefaultType authorized_default);
148 
156 
162  DeviceManager(const DeviceManager& rhs);
163 
170  const DeviceManager& operator=(const DeviceManager& rhs);
171 
175  virtual ~DeviceManager();
176 
184  virtual void setEnumerationOnlyMode(bool state) = 0;
185 
189  virtual void start() = 0;
190 
194  virtual void stop() = 0;
195 
199  virtual void scan() = 0;
200 
206  virtual void scan(const std::string& devpath) = 0;
207 
215  void setAuthorizedDefault(AuthorizedDefaultType authorized);
216 
225  AuthorizedDefaultType getAuthorizedDefault() const;
226 
244  void setRestoreControllerDeviceState(bool enabled);
245 
253  bool getRestoreControllerDeviceState() const;
254 
262  virtual std::shared_ptr<Device> applyDevicePolicy(uint32_t id, Rule::Target target) = 0;
263 
271  virtual void insertDevice(std::shared_ptr<Device> device);
272 
280  std::shared_ptr<Device> removeDevice(uint32_t id);
281 
287  std::vector<std::shared_ptr<Device>> getDeviceList();
288 
298  std::vector<std::shared_ptr<Device>> getDeviceList(const Rule& query);
299 
307  std::shared_ptr<Device> getDevice(uint32_t id);
308 
314  std::mutex& refDeviceMapMutex();
315 
326  void DeviceEvent(EventType event, std::shared_ptr<Device> device);
327 
338  void DeviceException(const std::string& message);
339 
358  static std::shared_ptr<DeviceManager> create(DeviceManagerHooks& hooks, const std::string& backend);
359 
360  private:
361  std::unique_ptr<DeviceManagerPrivate> d_pointer;
362  };
363 
364 } /* namespace usbguard */
365 
366 /* vim: set ts=2 sw=2 et */
Target
Enumeration of possible rule targets.
Definition: Rule.hpp:86
AuthorizedDefaultType
Defines which devices are authorized by default.
Definition: DeviceManager.hpp:61
Manages and keeps track of active USB devices.
Definition: DeviceManager.hpp:44
Allows reacting to device events.
Definition: DeviceManagerHooks.hpp:35
Determines whether USB device mathing specified criteria should be authorized, deauthorized or remove...
Definition: Rule.hpp:77
EventType
Type of event that took place on the device.
Definition: DeviceManager.hpp:51