/src/usbguard/src/Library/LocaltimeCondition.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #ifdef HAVE_BUILD_CONFIG_H |
20 | | #include <build-config.h> |
21 | | #endif |
22 | | |
23 | | #include "LocaltimeCondition.hpp" |
24 | | #include "Common/Utility.hpp" |
25 | | |
26 | | #include "usbguard/RuleParser.hpp" |
27 | | |
28 | | #ifndef _XOPEN_SOURCE |
29 | | #define _XOPEN_SOURCE |
30 | | #include <ctime> |
31 | | #endif |
32 | | |
33 | | namespace usbguard |
34 | | { |
35 | | LocaltimeCondition::LocaltimeCondition(const std::string& time_range, bool negated) |
36 | 2.41k | : RuleConditionBase("localtime", time_range, negated) |
37 | 2.41k | { |
38 | 2.41k | std::string time_begin; |
39 | 2.41k | std::string time_end; |
40 | 2.41k | const size_t dash_pos = time_range.find('-'); |
41 | | |
42 | 2.41k | if (dash_pos == std::string::npos) { |
43 | 577 | time_begin = time_range; |
44 | 577 | } |
45 | 1.84k | else { |
46 | 1.84k | time_begin = time_range.substr(0, dash_pos); |
47 | 1.84k | time_end = time_range.substr(dash_pos + 1); |
48 | 1.84k | } |
49 | | |
50 | 2.41k | _daytime_begin = stringToDaytime(time_begin); |
51 | | |
52 | 2.41k | if (!time_end.empty()) { |
53 | 1.06k | _daytime_end = stringToDaytime(time_end); |
54 | 1.06k | } |
55 | 1.35k | else { |
56 | 1.35k | _daytime_end = _daytime_begin; |
57 | 1.35k | } |
58 | | |
59 | 2.41k | if (_daytime_begin > _daytime_end) { |
60 | 33 | throw Exception("LocaltimeCondition", "Invalid time range (begin > end)", time_range); |
61 | 33 | } |
62 | 2.41k | } |
63 | | |
64 | | LocaltimeCondition::LocaltimeCondition(const LocaltimeCondition& rhs) |
65 | 3.42k | : RuleConditionBase(rhs) |
66 | 3.42k | { |
67 | 3.42k | _daytime_begin = rhs._daytime_begin; |
68 | 3.42k | _daytime_end = rhs._daytime_end; |
69 | 3.42k | } |
70 | | |
71 | | bool LocaltimeCondition::update(const Rule& rule) |
72 | 0 | { |
73 | 0 | USBGUARD_LOG(Trace); |
74 | 0 | (void)rule; |
75 | 0 | const auto tp_now = std::chrono::system_clock::now(); |
76 | 0 | const auto daytime = std::chrono::system_clock::to_time_t(tp_now) % 86400; |
77 | 0 | USBGUARD_LOG(Trace) << "daytime=" << daytime |
78 | 0 | << " daytime_begin=" << _daytime_begin |
79 | 0 | << " daytime_end=" << _daytime_end; |
80 | 0 | return (daytime >= _daytime_begin && daytime <= _daytime_end); |
81 | 0 | } |
82 | | |
83 | | RuleConditionBase* LocaltimeCondition::clone() const |
84 | 3.42k | { |
85 | 3.42k | return new LocaltimeCondition(*this); |
86 | 3.42k | } |
87 | | |
88 | | std::string LocaltimeCondition::tmToString(const struct tm* const tm) |
89 | 0 | { |
90 | 0 | std::string tm_string; |
91 | 0 | tm_string.append("{ tm.tm_sec="); |
92 | 0 | tm_string.append(numberToString(tm->tm_sec)); |
93 | 0 | tm_string.append(" tm.tm_min="); |
94 | 0 | tm_string.append(numberToString(tm->tm_min)); |
95 | 0 | tm_string.append(" tm.tm_hour="); |
96 | 0 | tm_string.append(numberToString(tm->tm_hour)); |
97 | 0 | tm_string.append(" tm.tm_mday="); |
98 | 0 | tm_string.append(numberToString(tm->tm_mday)); |
99 | 0 | tm_string.append(" tm.tm_mon="); |
100 | 0 | tm_string.append(numberToString(tm->tm_mon)); |
101 | 0 | tm_string.append(" tm.tm_year="); |
102 | 0 | tm_string.append(numberToString(tm->tm_year)); |
103 | 0 | tm_string.append(" tm.tm_wday="); |
104 | 0 | tm_string.append(numberToString(tm->tm_wday)); |
105 | 0 | tm_string.append(" tm.tm_yday="); |
106 | 0 | tm_string.append(numberToString(tm->tm_yday)); |
107 | 0 | tm_string.append(" tm.tm_isdst="); |
108 | 0 | tm_string.append(numberToString(tm->tm_isdst)); |
109 | 0 | tm_string.append(" }"); |
110 | 0 | return tm_string; |
111 | 0 | } |
112 | | |
113 | | std::time_t LocaltimeCondition::stringToDaytime(const std::string& string) |
114 | 3.48k | { |
115 | 3.48k | USBGUARD_LOG(Trace) << "string=" << string; |
116 | 3.48k | struct ::tm tm = { }; |
117 | | |
118 | 3.48k | if (::strptime(string.c_str(), "%H:%M:%s", &tm) == nullptr) { |
119 | 3.15k | if (::strptime(string.c_str(), "%H:%M", &tm) == nullptr) { |
120 | 165 | throw Exception("LocaltimeCondition", "Invalid time or range format", string); |
121 | 165 | } |
122 | 3.15k | } |
123 | | |
124 | 3.31k | USBGUARD_LOG(Trace) << "tm=" << tmToString(&tm); |
125 | 3.31k | return tm.tm_sec + 60*tm.tm_min + 60*60*tm.tm_hour; |
126 | 3.48k | } |
127 | | } /* namespace usbguard */ |
128 | | |
129 | | /* vim: set ts=2 sw=2 et */ |