|
| 1 | +#include "webdriver_access.h" |
| 2 | +#include "webdriver_logging.h" |
| 3 | +#include <base/string_util.h> |
| 4 | +#include "third_party/pugixml/pugixml.hpp" |
| 5 | + |
| 6 | +namespace webdriver { |
| 7 | + |
| 8 | +AccessValidator::AccessValidator(){ |
| 9 | +} |
| 10 | + |
| 11 | +AccessValidator::~AccessValidator(){ |
| 12 | +} |
| 13 | + |
| 14 | +bool AccessValidator::isAllowed(const long &remote_ip, const std::string &url, const std::string &method) |
| 15 | +{ |
| 16 | + if (accessList.empty()) |
| 17 | + return true; |
| 18 | + bool result = false; |
| 19 | + for (std::list<AccessRule>::iterator it = accessList.begin(); it != accessList.end(); ++it) |
| 20 | + { |
| 21 | + AccessRule host = *it; |
| 22 | + if ((host.hostIp == remote_ip) || (host.isGeneralRule)) |
| 23 | + { |
| 24 | + if (!host.allowed) { |
| 25 | + for (std::vector<AccessCommandTable>::iterator it = host.commandList.begin(); it != host.commandList.end(); ++it) { |
| 26 | + AccessCommandTable table = *it; |
| 27 | + if (MatchPattern(url, table.url) && MatchPattern(method, table.method)) |
| 28 | + return false; |
| 29 | + } |
| 30 | + return true; |
| 31 | + } else { |
| 32 | + if (host.commandList.empty()) |
| 33 | + return true; |
| 34 | + for (std::vector<AccessCommandTable>::iterator it = host.commandList.begin(); it != host.commandList.end(); ++it) { |
| 35 | + AccessCommandTable table = *it; |
| 36 | + if (MatchPattern(url, table.url) && MatchPattern(method, table.method)) |
| 37 | + return true; |
| 38 | + } |
| 39 | + return false; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + return result; |
| 44 | +} |
| 45 | + |
| 46 | +void AccessValidator::setWhiteList(FilePath &xmlPath) |
| 47 | +{ |
| 48 | + std::string white_list; |
| 49 | + |
| 50 | + if (file_util::ReadFileToString(xmlPath, &white_list)) |
| 51 | + { |
| 52 | + const void* content = white_list.c_str(); |
| 53 | + int content_size = white_list.size(); |
| 54 | + |
| 55 | + pugi::xml_document doc; |
| 56 | + pugi::xml_parse_result result = doc.load_buffer(content, content_size); |
| 57 | + |
| 58 | + if (result) { |
| 59 | + // Select host nodes |
| 60 | + pugi::xpath_query query_nodes("/hosts/host"); |
| 61 | + pugi::xpath_node_set found_nodes = query_nodes.evaluate_node_set(doc); |
| 62 | + |
| 63 | + if ( (NULL == query_nodes.result().error) && |
| 64 | + (pugi::xpath_type_node_set == query_nodes.return_type()) ) { |
| 65 | + for (pugi::xpath_node_set::const_iterator it = found_nodes.begin(); it != found_nodes.end(); ++it) { |
| 66 | + pugi::xpath_node node = *it; |
| 67 | + pugi::xml_node xnode = node.node(); |
| 68 | + pugi::xml_attribute atr = xnode.attribute("ip"); |
| 69 | + |
| 70 | + AccessRule rule; |
| 71 | + rule.isGeneralRule = false; |
| 72 | + rule.allowed = true; |
| 73 | + |
| 74 | + if (!convertIpString(atr.value(), &rule.hostIp)) { |
| 75 | + if (!strcmp(atr.value(), "*")) { |
| 76 | + rule.isGeneralRule = true; |
| 77 | + } else { |
| 78 | + std::string error_descr = "WhiteList: "+ std::string(atr.value()) + " is not a valid ip address"; |
| 79 | + GlobalLogger::Log(kWarningLogLevel, error_descr); |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + pugi::xpath_query query_nodes("./deny"); |
| 86 | + pugi::xpath_node_set deny_nodes = query_nodes.evaluate_node_set(xnode); |
| 87 | + if ( (NULL == query_nodes.result().error) && |
| 88 | + (pugi::xpath_type_node_set == query_nodes.return_type()) ) { |
| 89 | + for (pugi::xpath_node_set::const_iterator it = deny_nodes.begin(); it != deny_nodes.end(); ++it) { |
| 90 | + pugi::xpath_node node = *it; |
| 91 | + pugi::xml_node xnode = node.node(); |
| 92 | + pugi::xml_attribute atr = xnode.attribute("url"); |
| 93 | + |
| 94 | + AccessCommandTable rt; |
| 95 | + rt.url = atr.value(); |
| 96 | + atr = xnode.attribute("method"); |
| 97 | + rt.method = atr.value(); |
| 98 | + rule.commandList.push_back(rt); |
| 99 | + rule.allowed = false; |
| 100 | + } |
| 101 | + } |
| 102 | + if (rule.allowed) |
| 103 | + { |
| 104 | + pugi::xpath_query query_nodes("./allow"); |
| 105 | + pugi::xpath_node_set allow_nodes = query_nodes.evaluate_node_set(xnode); |
| 106 | + if ( (NULL == query_nodes.result().error) && |
| 107 | + (pugi::xpath_type_node_set == query_nodes.return_type()) ) { |
| 108 | + for (pugi::xpath_node_set::const_iterator it = allow_nodes.begin(); it != allow_nodes.end(); ++it) { |
| 109 | + pugi::xpath_node node = *it; |
| 110 | + pugi::xml_node xnode = node.node(); |
| 111 | + pugi::xml_attribute atr = xnode.attribute("url"); |
| 112 | + AccessCommandTable rt; |
| 113 | + rt.url = atr.value(); |
| 114 | + atr = xnode.attribute("method"); |
| 115 | + rt.method = atr.value(); |
| 116 | + rule.commandList.push_back(rt); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + // if we have wildcard put this rule in the end |
| 121 | + rule.isGeneralRule ? accessList.push_back(rule) : accessList.push_front(rule); |
| 122 | + } |
| 123 | + } else { |
| 124 | + std::string error_descr = "WhiteList: Cant evaluate XPath to node set: "; |
| 125 | + error_descr += query_nodes.result().description(); |
| 126 | + GlobalLogger::Log(kWarningLogLevel, error_descr); |
| 127 | + } |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + std::string error_descr = " Error description: "; |
| 132 | + error_descr += result.description(); |
| 133 | + GlobalLogger::Log(kWarningLogLevel, "WhiteList: XML parsed with errors:"); |
| 134 | + GlobalLogger::Log(kWarningLogLevel, error_descr); |
| 135 | + } |
| 136 | + |
| 137 | + // destroy tree |
| 138 | + doc.reset(); |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + GlobalLogger::Log(kWarningLogLevel, "WhiteList: Can't read file"); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +bool AccessValidator::convertIpString(const char *str_ip, long *int_ip) |
| 147 | +{ |
| 148 | + bool result = false; |
| 149 | + char *p, c; |
| 150 | + long octet, n; |
| 151 | + |
| 152 | + *int_ip = 0; |
| 153 | + octet = 0; |
| 154 | + n = 0; |
| 155 | + |
| 156 | + char buff[sizeof "000.000.000.000"]; |
| 157 | + if (strlen(str_ip) < sizeof buff) { |
| 158 | + strcpy(buff, str_ip); |
| 159 | + } |
| 160 | + |
| 161 | + for (p = buff; *p != '\0' ; ++p) { |
| 162 | + c = *p; |
| 163 | + if (c >= '0' && c <= '9') { |
| 164 | + octet = octet * 10 + (c - '0'); |
| 165 | + continue; |
| 166 | + } |
| 167 | + if (c == '.' && octet < 256) { |
| 168 | + *int_ip = (*int_ip << 8) + octet; |
| 169 | + octet = 0; |
| 170 | + n++; |
| 171 | + continue; |
| 172 | + } |
| 173 | + return result; |
| 174 | + } |
| 175 | + |
| 176 | + if (n == 3 && octet < 256) { |
| 177 | + *int_ip = (*int_ip << 8) + octet; |
| 178 | + result = true; |
| 179 | + } |
| 180 | + return result; |
| 181 | +} |
| 182 | + |
| 183 | +} // namespace webdriver |
0 commit comments