40 ifstream
file(file_name, ios::in);
41 if (!
file.is_open()) {
45 getline(
file, content);
52 return (::stat(path.c_str(), &statbuf) == 0);
58 if (::stat(path.c_str(), &statbuf) < 0) {
62 return (statbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
73 if (::stat(path.c_str(), &statbuf) < 0) {
76 return ((statbuf.st_mode & S_IFMT) == S_IFDIR);
82 if (::stat(path.c_str(), &statbuf) < 0) {
85 return ((statbuf.st_mode & S_IFMT) == S_IFREG);
91 if (::stat(path.c_str(), &statbuf) < 0) {
94 return ((statbuf.st_mode & S_IFMT) == S_IFSOCK);
99 return (
::remove(path.c_str()) == 0);
105 mode_t mask(S_IWGRP | S_IRWXO);
106 mode_t orig = umask(mask);
108 if ((orig | mask) != mask) {
109 static_cast<void>(umask(orig | mask));
117 static_cast<void>(umask(orig_umask_));
121 return (getuid() == 0 || geteuid() == 0);
125 dir_present_ =
false;
126 if (!full_name.empty()) {
128 size_t last_slash = full_name.find_last_of(
'/');
129 if (last_slash != string::npos) {
135 parent_path_ = full_name.substr(0, last_slash);
136 if (last_slash == full_name.size()) {
144 size_t last_dot = full_name.find_last_of(
'.');
145 if ((last_dot == string::npos) || (dir_present_ && (last_dot < last_slash))) {
150 stem_ = full_name.substr(last_slash + 1);
156 extension_ = full_name.substr(last_dot);
159 if ((last_dot - last_slash) > 1) {
160 stem_ = full_name.substr(last_slash + 1, last_dot - last_slash - 1);
167 return (parent_path_ + (dir_present_ ?
"/" :
"") + stem_ + extension_);
172 return (parent_path_);
177 return (parent_path_ + (dir_present_ ?
"/" :
""));
192 return (stem_ + extension_);
197 string const trimmed_replacement(
trim(replacement));
198 if (trimmed_replacement.empty()) {
199 extension_ = string();
201 size_t const last_dot(trimmed_replacement.find_last_of(
'.'));
202 if (last_dot == string::npos) {
203 extension_ =
"." + trimmed_replacement;
205 extension_ = trimmed_replacement.substr(last_dot);
213 string const trimmed_replacement(
trim(replacement));
214 dir_present_ = (trimmed_replacement.find_last_of(
'/') != string::npos);
215 if (trimmed_replacement.empty() || (trimmed_replacement ==
"/")) {
216 parent_path_ = string();
217 }
else if (trimmed_replacement.at(trimmed_replacement.size() - 1) ==
'/') {
218 parent_path_ = trimmed_replacement.substr(0, trimmed_replacement.size() - 1);
220 parent_path_ = trimmed_replacement;
226 char dir[](
"/tmp/kea-tmpdir-XXXXXX");
227 char const* dir_name = mkdtemp(dir);
231 dir_name_ = string(dir_name);
235 DIR *dir(opendir(dir_name_.c_str()));
240 std::unique_ptr<DIR, void(*)(DIR*)> defer(dir, [](DIR* d) { closedir(d); });
244 while ((i = readdir(dir))) {
245 if (strcmp(i->d_name,
".") == 0 || strcmp(i->d_name,
"..") == 0) {
249 filepath = dir_name_ +
'/' + i->d_name;
253 rmdir(dir_name_.c_str());
261 const std::string env_name )
262 : default_path_(default_path), env_name_(env_name),
263 default_overridden_(false) {
269 const std::string explicit_path ) {
271 if (!explicit_path.empty()) {
272 path_ = explicit_path;
273 }
else if (!env_name_.empty()) {
274 char* env_path = std::getenv(env_name_.c_str());
278 path_ = default_path_;
281 path_ = default_path_;
286 while (!path_.empty() && path_.back() ==
'/') {
290 default_overridden_ = (path_ != default_path_);
298 bool enforce_path )
const {
299 Path input_path(
trim(input_path_str));
300 auto filename = input_path.
filename();
301 if (filename.empty() || (filename ==
".") || (filename ==
"..")) {
307 if (!parent_dir.empty()) {
309 if ((parent_path != path_) || (parent_dir ==
"/")) {
310 std::ostringstream oss;
311 oss <<
"invalid path specified: '"
312 << (parent_path.empty() ?
"/" : parent_path)
313 <<
"', supported path is '"
324 std::string valid_path(path_ +
"/" + filename);
330 bool enforce_path )
const {
332 if (!input_path_str.empty()) {
333 std::string input_copy = input_path_str;
334 while (!input_copy.empty() && input_copy.back() ==
'/') {
335 input_copy.pop_back();
338 if (input_copy != path_) {
339 std::ostringstream oss;
340 oss <<
"invalid path specified: '"
341 << input_path_str <<
"', supported path is '"
363 return (default_overridden_);
367 return (enforce_security_);
371 enforce_security_ = enable;
374bool 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 enforcement 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.
A generic exception that is thrown if a parameter given violates security and enforcement is true.
A generic exception that is thrown if a parameter given violates security check but enforcement is la...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
bool amRunningAsRoot()
Indicates if current user is root.
bool remove(string const &path)
Remove file at the given path.
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.