Kea 3.0.3-git
filesystem.h
Go to the documentation of this file.
1// Copyright (C) 2021-2026 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/noncopyable.hpp>
13#include <boost/shared_ptr.hpp>
14
15namespace isc {
16namespace util {
17namespace file {
18
25std::string
26getContent(const std::string& file_name);
27
33bool
34exists(const std::string& path);
35
40mode_t
41getPermissions(const std::string path);
42
50bool
51hasPermissions(const std::string path, const mode_t& permissions);
52
59bool
60isDir(const std::string& path);
61
68bool
69isFile(const std::string& path);
70
77bool
78isSocket(const std::string& path);
79
81void
82setUmask();
83
85class RelaxUmask : public boost::noncopyable {
86public:
88 RelaxUmask();
89
92
93private:
95 mode_t orig_umask_;
96};
97
99struct Path {
103 Path(std::string const& path);
104
110 std::string str() const;
111
117 std::string parentPath() const;
118
125 std::string parentDirectory() const;
126
132 std::string stem() const;
133
139 std::string extension() const;
140
146 std::string filename() const;
147
159 Path& replaceExtension(std::string const& replacement = std::string());
160
170 Path& replaceParentPath(std::string const& replacement = std::string());
171
172private:
174 bool dir_present_;
175
177 std::string parent_path_;
178
180 std::string stem_;
181
183 std::string extension_;
184};
185
189 std::string dirName();
190private:
191 std::string dir_name_;
192};
193
196public:
205 PathChecker(const std::string default_path, const std::string env_name = "");
206
208 virtual ~PathChecker() {};
209
225 std::string getPath(bool reset = false, const std::string explicit_path = "");
226
242 std::string validatePath(const std::string input_path_str,
243 bool enforce_path = shouldEnforceSecurity()) const;
244
262 std::string validateDirectory(const std::string input_path_str,
263 bool enforce_path = shouldEnforceSecurity()) const;
264
272 bool pathHasPermissions(mode_t permissions,
273 bool enforce_perms = shouldEnforceSecurity()) const;
274
276 std::string getDefaultPath() const {
277 return (default_path_);
278 }
279
281 std::string getEnvName() const {
282 return (env_name_);
283 }
284
286 bool isDefaultOverridden();
287
289 static bool shouldEnforceSecurity();
290
294 static void enableEnforcement(bool enable);
295
296private:
298 std::string default_path_;
299
301 std::string env_name_;
302
304 std::string path_;
305
307 bool default_overridden_;
308
310 static bool enforce_security_;
311};
312
314typedef boost::shared_ptr<PathChecker> PathCheckerPtr;
315
316} // namespace file
317} // namespace util
318} // namespace isc
319
320#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:208
std::string getDefaultPath() const
Fetches the default path.
Definition filesystem.h:276
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:281
boost::shared_ptr< PathChecker > PathCheckerPtr
Defines a pointer to a PathChecker.
Definition filesystem.h:314
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.