Kea 3.1.1
isc::util::file::PathChecker Class Reference

Embodies a supported path against which file paths can be validated. More...

#include <filesystem.h>

Public Member Functions

 PathChecker (const std::string default_path, const std::string env_name="")
 Constructor.
 
virtual ~PathChecker ()
 Destructor.
 
std::string getDefaultPath () const
 Fetches the default path.
 
std::string getEnvName () const
 Fetches the environment variable name.
 
std::string getPath (bool reset=false, const std::string explicit_path="")
 Fetches the supported path.
 
bool isDefaultOverridden ()
 Indicates if the default path has been overridden.
 
bool pathHasPermissions (mode_t permissions, bool enforce_perms=shouldEnforceSecurity()) const
 Check if the path has expected permissions.
 
std::string validateDirectory (const std::string input_path_str, bool enforce_path=shouldEnforceSecurity()) const
 Validates a directory against a supported path.
 
std::string validatePath (const std::string input_path_str, bool enforce_path=shouldEnforceSecurity()) const
 Validates a file path against a supported path.
 

Static Public Member Functions

static void enableEnforcement (bool enable)
 Enables or disables security enforcment checks.
 
static bool shouldEnforceSecurity ()
 Indicates security checks should be enforced.
 

Detailed Description

Embodies a supported path against which file paths can be validated.

Definition at line 203 of file filesystem.h.

Constructor & Destructor Documentation

◆ PathChecker()

isc::util::file::PathChecker::PathChecker ( const std::string default_path,
const std::string env_name = "" )

Constructor.

Makes a call to getPath(true) to initialize the supported path.

Parameters
default_pathpath to use unless overridden by explicitly or via environment variable.
env_namename of environment variable (if one), that can override the default path.

Definition at line 248 of file filesystem.cc.

References getPath().

+ Here is the call graph for this function:

◆ ~PathChecker()

virtual isc::util::file::PathChecker::~PathChecker ( )
inlinevirtual

Destructor.

Definition at line 216 of file filesystem.h.

Member Function Documentation

◆ enableEnforcement()

void isc::util::file::PathChecker::enableEnforcement ( bool enable)
static

Enables or disables security enforcment checks.

Parameters
enabletrue to enable security checks, false to disable.

Definition at line 355 of file filesystem.cc.

Referenced by main(), and isc::process::DControllerBase::parseArgs().

◆ getDefaultPath()

std::string isc::util::file::PathChecker::getDefaultPath ( ) const
inline

Fetches the default path.

Definition at line 285 of file filesystem.h.

◆ getEnvName()

std::string isc::util::file::PathChecker::getEnvName ( ) const
inline

Fetches the environment variable name.

Definition at line 290 of file filesystem.h.

◆ getPath()

std::string isc::util::file::PathChecker::getPath ( bool reset = false,
const std::string explicit_path = "" )

Fetches the supported path.

When called with reset=true it will calculate the supported path as follows:

  1. Use the value of explicit_path parameter if not blank
  2. Use the value of the environment variable, if one is provided and it is defined in the environment
  3. Use the value of default path.
Parameters
resetrecalculate when true, defaults to false.
explicit_pathset the default path to this value. This is for testing purposes only.
Returns
String containing the default path.

Definition at line 256 of file filesystem.cc.

Referenced by PathChecker().

◆ isDefaultOverridden()

bool isc::util::file::PathChecker::isDefaultOverridden ( )

Indicates if the default path has been overridden.

Definition at line 347 of file filesystem.cc.

◆ pathHasPermissions()

bool isc::util::file::PathChecker::pathHasPermissions ( mode_t permissions,
bool enforce_perms = shouldEnforceSecurity() ) const

Check if the path has expected permissions.

Parameters
permissionsThe expected permissions.
enforce_permsEnables permsissions check. If false the function simply returns true.
Returns
True if the path points to a file or a directory, false otherwise.

Definition at line 341 of file filesystem.cc.

References isc::util::file::hasPermissions().

+ Here is the call graph for this function:

◆ shouldEnforceSecurity()

bool isc::util::file::PathChecker::shouldEnforceSecurity ( )
static

◆ validateDirectory()

std::string isc::util::file::PathChecker::validateDirectory ( const std::string input_path_str,
bool enforce_path = shouldEnforceSecurity() ) const

Validates a directory against a supported path.

Used to validate a string that represents a directory that may or may not end with a "/" (i.e "/foo/bar", bar is assumed to be the a directory, not a file.

If the input is empty or it matches the supported path, it returns the supported path. Otherwise it throws an error.

Parameters
input_path_strfile path to validate.
enforce_pathIf true throw SecurityError when validation against the supported path fails, if false throw SecurityWarn.
Returns
validated path
Exceptions
SecurityErrorif the path does not match the supported path and security is being enforced, SecurityWarn if it is not being enforced.

Definition at line 313 of file filesystem.cc.

References isc_throw, and isc::util::str::trim().

+ Here is the call graph for this function:

◆ validatePath()

std::string isc::util::file::PathChecker::validatePath ( const std::string input_path_str,
bool enforce_path = shouldEnforceSecurity() ) const

Validates a file path against a supported path.

If the input path specifies a parent path and file name, the parent path is validated against the supported path. If they match, the function returns the validated path. If the input path contains only a file name the function returns valid path using the supported path and the input path name.

Parameters
input_path_strfile path to validate.
enforce_pathIf true throw SecurityError when validation against the supported path fails, if false throw SecurityWarn.
Returns
validated path as a string (supported path + input file name)
Exceptions
BadValueif the input path does not include a file name.
SecurityErrorif the parent path does not path the supported path and security is being enforced, SecurityWarn if it is not being enforced.

Definition at line 281 of file filesystem.cc.

References isc::util::file::Path::filename(), isc_throw, isc::util::file::Path::parentDirectory(), isc::util::file::Path::parentPath(), isc::util::file::Path::str(), and isc::util::str::trim().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: