39 ifstream
file(file_name, ios::in);
40 if (!
file.is_open()) {
51 return (::stat(path.c_str(), &statbuf) == 0);
57 if (::stat(path.c_str(), &statbuf) < 0) {
61 return (statbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
72 if (::stat(path.c_str(), &statbuf) < 0) {
75 return ((statbuf.st_mode & S_IFMT) == S_IFDIR);
81 if (::stat(path.c_str(), &statbuf) < 0) {
84 return ((statbuf.st_mode & S_IFMT) == S_IFREG);
90 if (::stat(path.c_str(), &statbuf) < 0) {
93 return ((statbuf.st_mode & S_IFMT) == S_IFSOCK);
99 mode_t mask(S_IWGRP | S_IRWXO);
100 mode_t orig = umask(mask);
102 if ((orig | mask) != mask) {
103 static_cast<void>(umask(orig | mask));
111 static_cast<void>(umask(orig_umask_));
115 dir_present_ =
false;
116 if (!full_name.empty()) {
118 size_t last_slash = full_name.find_last_of(
'/');
119 if (last_slash != string::npos) {
125 parent_path_ = full_name.substr(0, last_slash);
126 if (last_slash == full_name.size()) {
134 size_t last_dot = full_name.find_last_of(
'.');
135 if ((last_dot == string::npos) || (dir_present_ && (last_dot < last_slash))) {
140 stem_ = full_name.substr(last_slash + 1);
146 extension_ = full_name.substr(last_dot);
149 if ((last_dot - last_slash) > 1) {
150 stem_ = full_name.substr(last_slash + 1, last_dot - last_slash - 1);
157 return (parent_path_ + (dir_present_ ?
"/" :
"") + stem_ + extension_);
162 return (parent_path_);
167 return (parent_path_ + (dir_present_ ?
"/" :
""));
182 return (stem_ + extension_);
187 string const trimmed_replacement(
trim(replacement));
188 if (trimmed_replacement.empty()) {
189 extension_ = string();
191 size_t const last_dot(trimmed_replacement.find_last_of(
'.'));
192 if (last_dot == string::npos) {
193 extension_ =
"." + trimmed_replacement;
195 extension_ = trimmed_replacement.substr(last_dot);
203 string const trimmed_replacement(
trim(replacement));
204 dir_present_ = (trimmed_replacement.find_last_of(
'/') != string::npos);
205 if (trimmed_replacement.empty() || (trimmed_replacement ==
"/")) {
206 parent_path_ = string();
207 }
else if (trimmed_replacement.at(trimmed_replacement.size() - 1) ==
'/') {
208 parent_path_ = trimmed_replacement.substr(0, trimmed_replacement.size() - 1);
210 parent_path_ = trimmed_replacement;
216 char dir[](
"/tmp/kea-tmpdir-XXXXXX");
217 char const* dir_name = mkdtemp(dir);
221 dir_name_ = string(dir_name);
225 DIR *dir(opendir(dir_name_.c_str()));
230 std::unique_ptr<DIR, void(*)(DIR*)> defer(dir, [](DIR* d) { closedir(d); });
234 while ((i = readdir(dir))) {
235 if (strcmp(i->d_name,
".") == 0 || strcmp(i->d_name,
"..") == 0) {
239 filepath = dir_name_ +
'/' + i->d_name;
240 remove(filepath.c_str());
243 rmdir(dir_name_.c_str());
251 const std::string env_name )
252 : default_path_(default_path), env_name_(env_name),
253 default_overridden_(false) {
259 const std::string explicit_path ) {
261 if (!explicit_path.empty()) {
262 path_ = explicit_path;
263 }
else if (!env_name_.empty()) {
264 path_ = std::string(std::getenv(env_name_.c_str()) ?
265 std::getenv(env_name_.c_str()) : default_path_);
267 path_ = default_path_;
272 while (!path_.empty() && path_.back() ==
'/') {
276 default_overridden_ = (path_ != default_path_);
284 bool enforce_path )
const {
285 Path input_path(
trim(input_path_str));
286 auto filename = input_path.
filename();
287 if (filename.empty()) {
293 if (!parent_dir.empty()) {
296 return (input_path_str);
300 if ((parent_path != path_) || (parent_dir ==
"/")) {
302 << (parent_path.empty() ?
"/" : parent_path)
303 <<
"', supported path is '"
308 std::string valid_path(path_ +
"/" + filename);
314 bool enforce_path )
const {
315 std::string input_copy =
trim(input_path_str);
321 if (!input_path_str.empty()) {
322 std::string input_copy = input_path_str;
323 while (!input_copy.empty() && input_copy.back() ==
'/') {
324 input_copy.pop_back();
327 if (input_copy != path_) {
329 << input_path_str <<
"', supported path is '"
345 return (default_overridden_);
349 return (enforce_security_);
353 enforce_security_ = enable;
356bool PathChecker::enforce_security_ =
true;
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown when an unexpected error condition occurs.
std::string getPath(bool reset=false, const std::string explicit_path="")
Fetches the supported path.
static bool shouldEnforceSecurity()
Indicates security checks should be enforced.
PathChecker(const std::string default_path, const std::string env_name="")
Constructor.
bool isDefaultOverridden()
Indicates if the default path has been overridden.
static void enableEnforcement(bool enable)
Enables or disables security enforcment checks.
std::string validateDirectory(const std::string input_path_str, bool enforce_path=shouldEnforceSecurity()) const
Validates a directory against a supported path.
bool pathHasPermissions(mode_t permissions, bool enforce_perms=shouldEnforceSecurity()) const
Check if the path has expected permissions.
std::string validatePath(const std::string input_path_str, bool enforce_path=shouldEnforceSecurity()) const
Validates a file path against a supported path.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
bool isSocket(string const &path)
Check if there is a socket at the given path.
string getContent(string const &file_name)
Get the content of a regular file.
bool isFile(string const &path)
Check if there is a file at the given path.
bool exists(string const &path)
Check if there is a file or directory at the given path.
bool isDir(string const &path)
Check if there is a directory at the given path.
mode_t getPermissions(const std::string path)
Fetches the file permissions mask.
bool hasPermissions(const std::string path, const mode_t &permissions)
Check if there if file or directory has the given permissions.
void setUmask()
Set umask (at least 0027 i.e. no group write and no other access).
string trim(const string &input)
Trim leading and trailing spaces.
Defines the logger used by the top-level component of kea-lfc.
Path(std::string const &path)
Constructor.
Path & replaceParentPath(std::string const &replacement=std::string())
Trims {replacement} and replaces this instance's parent path with it.
std::string parentDirectory() const
Get the parent directory.
std::string extension() const
Get the extension of the file.
Path & replaceExtension(std::string const &replacement=std::string())
Identifies the extension in {replacement}, trims it, and replaces this instance's extension with it.
std::string stem() const
Get the base name of the file without the extension.
std::string parentPath() const
Get the parent path.
std::string filename() const
Get the name of the file, extension included.
std::string str() const
Get the path in textual format.