Kea 3.0.0
filesystem.h
Go to the documentation of this file.
1// Copyright (C) 2021-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef KEA_UTIL_FILESYSTEM_H
8#define KEA_UTIL_FILESYSTEM_H
9
10#include <sys/stat.h>
11#include <string>
12#include <boost/shared_ptr.hpp>
13
14namespace isc {
15namespace util {
16namespace file {
17
24std::string
25getContent(const std::string& file_name);
26
32bool
33exists(const std::string& path);
34
39mode_t
40getPermissions(const std::string path);
41
48bool
49hasPermissions(const std::string path, const mode_t& permissions);
50
57bool
58isDir(const std::string& path);
59
66bool
67isFile(const std::string& path);
68
75bool
76isSocket(const std::string& path);
77
79void
80setUmask();
81
83struct Path {
87 Path(std::string const& path);
88
94 std::string str() const;
95
101 std::string parentPath() const;
102
109 std::string parentDirectory() const;
110
116 std::string stem() const;
117
123 std::string extension() const;
124
130 std::string filename() const;
131
143 Path& replaceExtension(std::string const& replacement = std::string());
144
154 Path& replaceParentPath(std::string const& replacement = std::string());
155
156private:
158 bool dir_present_;
159
161 std::string parent_path_;
162
164 std::string stem_;
165
167 std::string extension_;
168};
169
173 std::string dirName();
174private:
175 std::string dir_name_;
176};
177
180public:
189 PathChecker(const std::string default_path, const std::string env_name = "");
190
192 virtual ~PathChecker() {};
193
209 std::string getPath(bool reset = false, const std::string explicit_path = "");
210
226 std::string validatePath(const std::string input_path_str,
227 bool enforce_path = shouldEnforceSecurity()) const;
228
246 std::string validateDirectory(const std::string input_path_str,
247 bool enforce_path = shouldEnforceSecurity()) const;
248
256 bool pathHasPermissions(mode_t permissions,
257 bool enforce_perms = shouldEnforceSecurity()) const;
258
260 std::string getDefaultPath() const {
261 return (default_path_);
262 }
263
265 std::string getEnvName() const {
266 return (env_name_);
267 }
268
270 bool isDefaultOverridden();
271
273 static bool shouldEnforceSecurity();
274
278 static void enableEnforcement(bool enable);
279
280private:
282 std::string default_path_;
283
285 std::string env_name_;
286
288 std::string path_;
289
291 bool default_overridden_;
292
294 static bool enforce_security_;
295};
296
298typedef boost::shared_ptr<PathChecker> PathCheckerPtr;
299
300} // namespace file
301} // namespace util
302} // namespace isc
303
304#endif // KEA_UTIL_FILESYSTEM_H
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.
virtual ~PathChecker()
Destructor.
Definition filesystem.h:192
std::string getDefaultPath() const
Fetches the default path.
Definition filesystem.h:260
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.
std::string getEnvName() const
Fetches the environment variable name.
Definition filesystem.h:265
boost::shared_ptr< PathChecker > PathCheckerPtr
Defines a pointer to a PathChecker.
Definition filesystem.h:298
bool isSocket(string const &path)
Check if there is a socket at the given path.
Definition filesystem.cc:88
string getContent(string const &file_name)
Get the content of a regular file.
Definition filesystem.cc:32
bool isFile(string const &path)
Check if there is a file at the given path.
Definition filesystem.cc:79
bool exists(string const &path)
Check if there is a file or directory at the given path.
Definition filesystem.cc:49
bool isDir(string const &path)
Check if there is a directory at the given path.
Definition filesystem.cc:70
mode_t getPermissions(const std::string path)
Fetches the file permissions mask.
Definition filesystem.cc:55
bool hasPermissions(const std::string path, const mode_t &permissions)
Check if there if file or directory has the given permissions.
Definition filesystem.cc:65
void setUmask()
Set umask (at least 0027 i.e. no group write and no other access).
Definition filesystem.cc:97
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.