USBGuard
Software framework that protects your computer against rogue USB devices by implementing basic whitelisting and blacklisting capabilities.
Exception.hpp
1 //
2 // Copyright (C) 2016 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 "Typedefs.hpp"
22 
23 #include <stdexcept>
24 #include <string>
25 
26 #ifndef _GNU_SOURCE
27  #define _GNU_SOURCE
28 #endif
29 
30 #include <cerrno>
31 #include <cstdint>
32 #include <cstring>
33 
34 namespace usbguard
35 {
39  class DLL_PUBLIC Exception : public std::exception
40  {
41  public:
42  Exception(const std::string& context,
43  const std::string& object,
44  const std::string& reason_val)
45  : _context(context),
46  _object(object),
47  _reason(reason_val)
48  {
49  }
50 
51  Exception(const Exception& rhs) = default;
52  Exception& operator=(const Exception& rhs) = default;
53 
54  Exception(Exception&& rhs) = default;
55  Exception& operator=(Exception&& rhs) = default;
56 
57  const std::string& context() const noexcept
58  {
59  return _context;
60  }
61 
62  const std::string& object() const noexcept
63  {
64  return _object;
65  }
66 
67  const std::string& reason() const noexcept
68  {
69  return _reason;
70  }
71 
72  void setContext(const std::string& context)
73  {
74  _context = context;
75  }
76 
77  void setObject(const std::string& object)
78  {
79  _object = object;
80  }
81 
82  void setReason(const std::string& reason_val)
83  {
84  _reason = reason_val;
85  }
86 
94  virtual std::string message() const noexcept
95  {
96  try {
97  return _context + ": " + (!_object.empty() ? _object + ": " : "") + _reason;
98  }
99  catch (...) {
100  return "usbguard::Exception: exception^2";
101  }
102  }
103 
104  virtual const char* what() const noexcept
105  {
106  return "usbguard::Exception";
107  }
108 
109  private:
110  std::string _context;
111  std::string _object;
112  std::string _reason;
113  };
114 
115 #define USBGUARD_BUG(m) \
116  ::usbguard::Exception(__PRETTY_FUNCTION__, "BUG", m)
117 
118  class ErrnoException : public Exception
119  {
120  public:
121  ErrnoException(const std::string& context, const std::string& object, const int errno_value)
122  : Exception(context, object, ErrnoException::reasonFromErrno(errno_value))
123  {
124  }
125  private:
126  static std::string reasonFromErrno(const int errno_value)
127  {
128  char buffer[1024];
129  return std::string(strerror_r(errno_value, buffer, sizeof buffer));
130  }
131  };
132 
133 #define USBGUARD_SYSCALL_THROW(context, syscall_bool_expression) \
134  do { \
135  if (syscall_bool_expression) { \
136  throw usbguard::ErrnoException(context, #syscall_bool_expression, errno); \
137  } \
138  } while(0)
139 
140  class IPCException : public Exception
141  {
142  public:
143  IPCException()
144  : Exception("", "", "")
145  {
146  }
147 
148  IPCException(const Exception& exception,
149  uint64_t message_id = 0)
150  : Exception(exception),
151  _message_id(message_id)
152  {
153  }
154 
155  IPCException(const std::string& context,
156  const std::string& object,
157  const std::string& reason,
158  uint64_t message_id = 0)
159  : Exception(context, object, reason),
160  _message_id(message_id)
161  {
162  }
163 
164  IPCException(const IPCException& rhs)
165  : Exception(rhs),
166  _message_id(rhs._message_id)
167  {
168  }
169 
170  bool hasMessageID() const noexcept
171  {
172  return _message_id != 0;
173  }
174 
175  uint64_t messageID() const noexcept
176  {
177  return _message_id;
178  }
179 
180  void setMessageID(uint64_t message_id)
181  {
182  _message_id = message_id;
183  }
184 
185  private:
186  uint64_t _message_id{0};
187  };
188 } /* namespace usbguard */
189 
190 /* vim: set ts=2 sw=2 et */
USBGuard exception.
Definition: Exception.hpp:39
virtual std::string message() const noexcept
Returns exception message.
Definition: Exception.hpp:94
Definition: Exception.hpp:118
Definition: Exception.hpp:140