Kea 3.1.5
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
101void
102setUmask();
103
105class RelaxUmask : public boost::noncopyable {
106public:
108 RelaxUmask();
109
111 ~RelaxUmask();
112
113private:
115 mode_t orig_umask_;
116};
117
122bool
124
126struct Path {
130 Path(std::string const& path);
131
137 std::string str() const;
138
144 std::string parentPath() const;
145
152 std::string parentDirectory() const;
153
159 std::string stem() const;
160
166 std::string extension() const;
167
173 std::string filename() const;
174
186 Path& replaceExtension(std::string const& replacement = std::string());
187
197 Path& replaceParentPath(std::string const& replacement = std::string());
198
199private:
201 bool dir_present_;
202
204 std::string parent_path_;
205
207 std::string stem_;
208
210 std::string extension_;
211};
212
216 std::string dirName();
217private:
218 std::string dir_name_;
219};
220
223public:
232 PathChecker(const std::string default_path, const std::string env_name = "");
233
235 virtual ~PathChecker() {};
236
252 std::string getPath(bool reset = false, const std::string explicit_path = "");
253
270 std::string validatePath(const std::string input_path_str,
271 bool enforce_path = shouldEnforceSecurity()) const;
272
290 std::string validateDirectory(const std::string input_path_str,
291 bool enforce_path = shouldEnforceSecurity()) const;
292
300 bool pathHasPermissions(mode_t permissions,
301 bool enforce_perms = shouldEnforceSecurity()) const;
302
304 std::string getDefaultPath() const {
305 return (default_path_);
306 }
307
309 std::string getEnvName() const {
310 return (env_name_);
311 }
312
314 bool isDefaultOverridden();
315
317 static bool shouldEnforceSecurity();
318
322 static void enableEnforcement(bool enable);
323
324private:
326 std::string default_path_;
327
329 std::string env_name_;
330
332 std::string path_;
333
335 bool default_overridden_;
336
338 static bool enforce_security_;
339};
340
342typedef boost::shared_ptr<PathChecker> PathCheckerPtr;
343
344} // namespace file
345} // namespace util
346} // namespace isc
347
348#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:235
std::string getDefaultPath() const
Fetches the default path.
Definition filesystem.h:304
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:309
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:342
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).
Definition filesystem.cc:98
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.