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));
 
 
  108    dir_present_ = 
false;
 
  109    if (!full_name.empty()) {
 
  111        size_t last_slash = full_name.find_last_of(
'/');
 
  112        if (last_slash != string::npos) {
 
  118            parent_path_ = full_name.substr(0, last_slash);
 
  119            if (last_slash == full_name.size()) {
 
  127        size_t last_dot = full_name.find_last_of(
'.');
 
  128        if ((last_dot == string::npos) || (dir_present_ && (last_dot < last_slash))) {
 
  133            stem_ = full_name.substr(last_slash + 1);
 
  139        extension_ = full_name.substr(last_dot);
 
  142        if ((last_dot - last_slash) > 1) {
 
  143            stem_ = full_name.substr(last_slash + 1, last_dot - last_slash - 1);
 
 
  150    return (parent_path_ + (dir_present_ ? 
"/" : 
"") + stem_ + extension_);
 
 
  155    return (parent_path_);
 
 
  160    return (parent_path_ + (dir_present_ ? 
"/" : 
""));
 
 
  175    return (stem_ + extension_);
 
 
  180    string const trimmed_replacement(
trim(replacement));
 
  181    if (trimmed_replacement.empty()) {
 
  182        extension_ = string();
 
  184        size_t const last_dot(trimmed_replacement.find_last_of(
'.'));
 
  185        if (last_dot == string::npos) {
 
  186            extension_ = 
"." + trimmed_replacement;
 
  188            extension_ = trimmed_replacement.substr(last_dot);
 
 
  196    string const trimmed_replacement(
trim(replacement));
 
  197    dir_present_ = (trimmed_replacement.find_last_of(
'/') != string::npos);
 
  198    if (trimmed_replacement.empty() || (trimmed_replacement == 
"/")) {
 
  199        parent_path_ = string();
 
  200    } 
else if (trimmed_replacement.at(trimmed_replacement.size() - 1) == 
'/') {
 
  201        parent_path_ = trimmed_replacement.substr(0, trimmed_replacement.size() - 1);
 
  203        parent_path_ = trimmed_replacement;
 
 
  209    char dir[](
"/tmp/kea-tmpdir-XXXXXX");
 
  210    char const* dir_name = mkdtemp(dir);
 
  214    dir_name_ = string(dir_name);
 
 
  218    DIR *dir(opendir(dir_name_.c_str()));
 
  223    std::unique_ptr<DIR, void(*)(DIR*)> defer(dir, [](DIR* d) { closedir(d); });
 
  227    while ((i = readdir(dir))) {
 
  228        if (strcmp(i->d_name, 
".") == 0 || strcmp(i->d_name, 
"..") == 0) {
 
  232        filepath = dir_name_ + 
'/' + i->d_name;
 
  233        remove(filepath.c_str());
 
  236    rmdir(dir_name_.c_str());
 
 
  244                             const std::string env_name )
 
  245    : default_path_(default_path), env_name_(env_name),
 
  246      default_overridden_(false) {
 
 
  252                     const std::string explicit_path ) {
 
  254        if (!explicit_path.empty()) {
 
  255            path_ = explicit_path;
 
  256        } 
else if (!env_name_.empty()) {
 
  257            path_ = std::string(std::getenv(env_name_.c_str()) ?
 
  258                                std::getenv(env_name_.c_str()) : default_path_);
 
  260            path_ = default_path_;
 
  265        while (!path_.empty() && path_.back() == 
'/') {
 
  269        default_overridden_ = (path_ != default_path_);
 
 
  277                          bool enforce_path )
 const {
 
  278    Path input_path(
trim(input_path_str));
 
  279    auto filename = input_path.
filename();
 
  280    if (filename.empty()) {
 
  286    if (!parent_dir.empty()) {
 
  289            return (input_path_str);
 
  293        if ((parent_path != path_) || (parent_dir == 
"/")) {
 
  295                      << (parent_path.empty() ? 
"/" : parent_path)
 
  296                      << 
"', supported path is '" 
  301    std::string valid_path(path_ + 
"/" +  filename);
 
 
  307                               bool enforce_path )
 const {
 
  308    std::string input_copy = 
trim(input_path_str);
 
  314    if (!input_path_str.empty()) {
 
  315        std::string input_copy = input_path_str;
 
  316        while (!input_copy.empty() && input_copy.back() == 
'/') {
 
  317               input_copy.pop_back();
 
  320        if (input_copy != path_) {
 
  322                      << input_path_str << 
"', supported path is '" 
 
  338    return (default_overridden_);
 
 
  342    return (enforce_security_);
 
 
  346    enforce_security_ = enable;
 
 
  349bool 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.