This repository was archived by the owner on May 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthRule.cpp
More file actions
93 lines (71 loc) · 2.63 KB
/
Copy pathAuthRule.cpp
File metadata and controls
93 lines (71 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "AuthRule.h"
#include "string.h"
AuthRule::AuthRule(Device& dev) : dev(dev){
AuthRule::enumerate_rules();
}
void AuthRule::create_rule(const std::string &product, const std::string &vendor, const std::string &syspath){
std::ofstream output;
const std::string templaterule = "ACTION==\"add\", SUBSYSTEM==\"usb\", ATTR{idVendor}==\"" + vendor +"\", ATTR{idProduct}==\"" + product + "\", RUN+=\"/bin/sh -c 'echo 1 > " + syspath + "'\"";
output.open(AuthRule::rule_file, std::ios_base::app);
Device::workable_device* device = dev.get_device(dev, vendor, product);
if(device != nullptr){
output << templaterule + "\n";
device->authorised = true;
}
output.close();
}
void AuthRule::remove_rule(const std::string &vendor, const std::string &product){
std::ifstream input(AuthRule::rule_file);
std::ofstream output;
std::string content((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());
input.open(AuthRule::rule_file);
if(!input.is_open())
exit(EXIT_FAILURE);
input.close();
output.open(AuthRule::rule_file);
Device::workable_device* device = dev.get_device(dev, vendor, product);
char* copy = strdup(content.c_str());
char* point = strtok(copy, "\n");
std::string rules = "";
while (point != nullptr) {
std::string rule = point;
if(rule.find(product) == std::string::npos && rule.find(product) == std::string::npos){
rules += rule + "\n";
}
point = strtok(nullptr, "\n");
}
free(copy);
output << rules;
output.close();
device->authorised = false;
}
void AuthRule::enumerate_rules(){
std::ifstream input;
input.open(AuthRule::rule_file);
if(input.is_open()){
std::string line;
while (getline(input, line)){
std::string vendor;
std::string product;
AuthRule::get_attr("idVendor", line, vendor);
AuthRule::get_attr("idProduct", line, product);
Device::workable_device* device = dev.get_device(dev, vendor, product);
if(device != nullptr){
device->authorised = true;
device->mod_kernel_authentication(true);
}
}
}
input.close();
}
std::string AuthRule::get_rule_file_path(){
return AuthRule::rule_file;
}
void AuthRule::set_rule_file_path(std::string rule_file){
AuthRule::rule_file = rule_file;
}
void AuthRule::get_attr(const std::string &attr, const std::string &line, std::string &out){
for(u_long c = attr.size() + 4; line[line.find(attr) + c] != 34; c++){
out += line[line.find(attr) + c];
}
}