31 #include <BESCatalog.h>
32 #include <BESCatalogList.h>
33 #include <BESCatalogUtils.h>
35 #include <TheBESKeys.h>
36 #include <BESInternalError.h>
37 #include <BESSyntaxUserError.h>
39 #include <BESNotFoundError.h>
40 #include <BESForbiddenError.h>
42 #include "AllowedHosts.h"
48 #define prolog string("AllowedHosts::").append(__func__).append("() - ")
58 AllowedHosts::theHosts()
60 if (d_instance)
return d_instance;
65 AllowedHosts::AllowedHosts()
68 string key = ALLOWED_HOSTS_BES_KEY;
71 throw BESInternalError(
string(
"The allowed hosts key, '") + ALLOWED_HOSTS_BES_KEY
72 +
"' has not been configured.", __FILE__, __LINE__);
90 bool AllowedHosts::is_allowed(
const std::string &candidate_url)
92 BESDEBUG(MODULE, prolog <<
"BEGIN candidate_url: " << candidate_url << endl);
93 bool isAllowed =
false;
94 const string file_url(
"file://");
95 const string http_url(
"http://");
96 const string https_url(
"https://");
100 if (candidate_url.compare(0, file_url.size(), file_url) == 0 ) {
103 string file_path = candidate_url.substr(file_url.size());
104 BESDEBUG(MODULE, prolog <<
"file_path: "<< file_path << endl);
108 BESDEBUG(MODULE, prolog <<
"Searching for catalog: "<< default_catalog_name << endl);
109 BESCatalog *bcat = bcl->find_catalog(default_catalog_name);
111 BESDEBUG(MODULE, prolog <<
"Found catalog: "<< bcat->
get_catalog_name() << endl);
114 string msg =
"OUCH! Unable to locate default catalog!";
115 BESDEBUG(MODULE, prolog << msg << endl);
119 string catalog_root = bcat->
get_root();
120 BESDEBUG(MODULE, prolog <<
"Catalog root: "<< catalog_root << endl);
129 string relative_path;
130 if(file_path[0] ==
'/'){
131 if(file_path.length() < catalog_root.length()) {
135 int ret = file_path.compare(0, catalog_root.npos, catalog_root) == 0;
136 BESDEBUG(MODULE, prolog <<
"file_path.compare(): " << ret << endl);
137 isAllowed = (ret==0);
138 relative_path = file_path.substr(catalog_root.length());
142 BESDEBUG(MODULE, prolog <<
"Relative path detected");
143 relative_path = file_path;
166 BESDEBUG(MODULE, prolog <<
"File Access Allowed: "<< (isAllowed?
"true ":
"false ") << endl);
170 vector<string>::const_iterator it = d_allowed_hosts.begin();
171 vector<string>::const_iterator end_it = d_allowed_hosts.end();
172 for (; it != end_it && !isAllowed; it++) {
173 string a_regex_pattern = *it;
174 BESRegex reg_expr(a_regex_pattern.c_str());
175 int match_result = reg_expr.
match(candidate_url.c_str(), candidate_url.length());
176 if(match_result>=0) {
177 auto match_length = (
unsigned int) match_result;
178 if (match_length == candidate_url.length()) {
180 prolog <<
"FULL MATCH. pattern: " << a_regex_pattern <<
" url: " << candidate_url << endl);
184 prolog <<
"No Match. pattern: " << a_regex_pattern <<
" url: " << candidate_url << endl);
188 BESDEBUG(MODULE, prolog <<
"HTTP Access Allowed: "<< (isAllowed?
"true ":
"false ") << endl);
190 BESDEBUG(MODULE, prolog <<
"END Access Allowed: "<< (isAllowed?
"true ":
"false ") << endl);
List of all registered catalogs.
virtual std::string default_catalog_name() const
The name of the default catalog.
static BESCatalogList * TheCatalogList()
Get the singleton BESCatalogList instance.
Catalogs provide a hierarchical organization for data.
virtual std::string get_root() const =0
virtual std::string get_catalog_name() const
Get the name for this catalog.
virtual BESCatalogUtils * get_catalog_utils() const
Get a pointer to the utilities, customized for this catalog.
error thrown if the BES is not allowed to access the resource requested
exception thrown if internal error encountered
error thrown if the resource requested cannot be found
int match(const char *s, int len, int pos=0)
Does the pattern match.
static void check_path(const std::string &path, const std::string &root, bool follow_sym_links)
Check if the specified path is valid.
static TheBESKeys * TheKeys()
void get_values(const std::string &s, std::vector< std::string > &vals, bool &found)
Retrieve the values of a given key, if set.
Can a given URL be dereferenced given the BES's configuration?