-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathpm_from_file.cc
95 lines (80 loc) · 2.3 KB
/
pm_from_file.cc
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
94
95
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 - 2021 Trustwave Holdings, Inc. (http://www.trustwave.com/)
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* If any of the files related to licensing are missing or if you have any
* other questions related to licensing please contact Trustwave Holdings, Inc.
* directly using the email address security@modsecurity.org.
*
*/
#include "src/operators/pm_from_file.h"
#include <string>
#include "src/operators/operator.h"
#include "src/utils/https_client.h"
#include "src/utils/system.h"
namespace modsecurity {
namespace operators {
bool PmFromFile::isComment(const std::string &s) {
if (s.size() == 0) {
return true;
}
size_t pos = s.find("#");
if (pos != std::string::npos) {
for (int i = 0; i < pos; i++) {
if (!std::isspace(s[i])) {
return false;
}
}
} else {
return false;
}
return true;
}
bool PmFromFile::init(const std::string &config, std::string *error) {
std::istream *iss;
if (m_param.compare(0, 8, "https://") == 0) {
Utils::HttpsClient client;
bool ret = client.download(m_param);
if (ret == false) {
error->assign(client.error);
return false;
}
iss = new std::stringstream(client.content);
} else {
std::string err;
std::string resource = utils::find_resource(m_param, config, &err);
iss = new std::ifstream(resource, std::ios::in);
if (((std::ifstream *)iss)->is_open() == false) {
error->assign("Failed to open file: " + m_param + ". " + err);
delete iss;
return false;
}
}
for (std::string line; std::getline(*iss, line); ) {
if (isComment(line) == false) {
#ifdef WITH_HS
m_hs->addPattern(line.c_str(), line.length());
}
}
if (m_hs->compile(error) == false) {
delete iss;
return false;
}
#else
acmp_add_pattern(m_p, line.c_str(), NULL, NULL, line.length());
}
}
while (m_p->is_failtree_done == 0) {
acmp_prepare(m_p);
}
#endif
delete iss;
return true;
}
} // namespace operators
} // namespace modsecurity