Kea 3.3.4
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
11#include <sys/stat.h>
12#include <string>
13#include <boost/noncopyable.hpp>
14#include <boost/shared_ptr.hpp>
15
16namespace isc {
17namespace util {
18namespace file {
19
22class SecurityWarn : public Exception {
23public:
24 SecurityWarn(const char* file, size_t line, const char* what) :
25 isc::Exception(file, line, what) {}
26};
27
30class SecurityError : public Exception {
31public:
32 SecurityError(const char* file, size_t line, const char* what) :
33 isc::Exception(file, line, what) {}
34};
35
45std::string
46getContent(const std::string& file_name);
47
53bool
54exists(const std::string& path);
55
60mode_t
61getPermissions(const std::string path);
62
70bool
71hasPermissions(const std::string path, const mode_t& permissions);
72
79bool
80isDir(const std::string& path);
81
88bool
89isFile(const std::string& path);
90
97bool
98isSocket(const std::string& path);
99
105bool
106remove(const std::string& path);
107
109void
110setUmask();
111
113class RelaxUmask : public boost::noncopyable {
114public:
116 RelaxUmask();
117
119 ~RelaxUmask();
120
121private:
123 mode_t orig_umask_;
124};
125
130bool
132
134struct Path {
138 Path(std::string const& path);
139
145 std::string str() const;
146
152 std::string parentPath() const;
153
160 std::string parentDirectory() const;
161
167 std::string stem() const;
168
174 std::string extension() const;
175
181 std::string filename() const;
182
194 Path& replaceExtension(std::string const& replacement = std::string());
195
205 Path& replaceParentPath(std::string const& replacement = std::string());
206
207private:
209 bool dir_present_;
210
212 std::string parent_path_;
213
215 std::string stem_;
216
218 std::string extension_;
219};
220
224 std::string dirName();
225private:
226 std::string dir_name_;
227};
228
231public:
240 PathChecker(const std::string default_path, const std::string env_name = "");
241
243 virtual ~PathChecker() {};
244
260 std::string getPath(bool reset = false, const std::string explicit_path = "");
261
280 std::string validatePath(const std::string input_path_str,
281 bool enforce_path = shouldEnforceSecurity()) const;
282
300 std::string validateDirectory(const std::string input_path_str,
301 bool enforce_path = shouldEnforceSecurity()) const;
302
310 bool pathHasPermissions(mode_t permissions,
311 bool enforce_perms = shouldEnforceSecurity()) const;
312
314 std::string getDefaultPath() const {
315 return (default_path_);
316 }
317
319 std::string getEnvName() const {
320 return (env_name_);
321 }
322
324 bool isDefaultOverridden();
325
327 static bool shouldEnforceSecurity();
328
332 static void enableEnforcement(bool enable);
333
334private:
336 std::string default_path_;
337
339 std::string env_name_;
340
342 std::string path_;
343
345 bool default_overridden_;
346
348 static bool enforce_security_;
349};
350
352typedef boost::shared_ptr<PathChecker> PathCheckerPtr;
353
354} // namespace file
355} // namespace util
356} // namespace isc
357
358#endif // KEA_UTIL_FILESYSTEM_H
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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:243
std::string getDefaultPath() const
Fetches the default path.
Definition filesystem.h:314
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.
std::string getEnvName() const
Fetches the environment variable name.
Definition filesystem.h:319
SecurityError(const char *file, size_t line, const char *what)
Definition filesystem.h:32
SecurityWarn(const char *file, size_t line, const char *what)
Definition filesystem.h:24
bool amRunningAsRoot()
Indicates if current user is root.
boost::shared_ptr< PathChecker > PathCheckerPtr
Defines a pointer to a PathChecker.
Definition filesystem.h:352
bool remove(string const &path)
Remove file at the given path.
Definition filesystem.cc:98
bool isSocket(string const &path)
Check if there is a socket at the given path.
Definition filesystem.cc:89
string getContent(string const &file_name)
Get the content of a regular file.
Definition filesystem.cc:33
bool isFile(string const &path)
Check if there is a file at the given path.
Definition filesystem.cc:80
bool exists(string const &path)
Check if there is a file or directory at the given path.
Definition filesystem.cc:50
bool isDir(string const &path)
Check if there is a directory at the given path.
Definition filesystem.cc:71
mode_t getPermissions(const std::string path)
Fetches the file permissions mask.
Definition filesystem.cc:56
bool hasPermissions(const std::string path, const mode_t &permissions)
Check if there if file or directory has the given permissions.
Definition filesystem.cc:66
void setUmask()
Set umask (at least 0027 i.e. no group write and no other access).
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.