Kea 3.1.1
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
11#include <sys/stat.h>
12#include <string>
13#include <boost/shared_ptr.hpp>
14
15namespace isc {
16namespace util {
17namespace file {
18
21class SecurityWarn : public Exception {
22public:
23 SecurityWarn(const char* file, size_t line, const char* what) :
24 isc::Exception(file, line, what) {}
25};
26
29class SecurityError : public Exception {
30public:
31 SecurityError(const char* file, size_t line, const char* what) :
32 isc::Exception(file, line, what) {}
33};
34
41std::string
42getContent(const std::string& file_name);
43
49bool
50exists(const std::string& path);
51
56mode_t
57getPermissions(const std::string path);
58
65bool
66hasPermissions(const std::string path, const mode_t& permissions);
67
74bool
75isDir(const std::string& path);
76
83bool
84isFile(const std::string& path);
85
92bool
93isSocket(const std::string& path);
94
96void
97setUmask();
98
103bool
105
107struct Path {
111 Path(std::string const& path);
112
118 std::string str() const;
119
125 std::string parentPath() const;
126
133 std::string parentDirectory() const;
134
140 std::string stem() const;
141
147 std::string extension() const;
148
154 std::string filename() const;
155
167 Path& replaceExtension(std::string const& replacement = std::string());
168
178 Path& replaceParentPath(std::string const& replacement = std::string());
179
180private:
182 bool dir_present_;
183
185 std::string parent_path_;
186
188 std::string stem_;
189
191 std::string extension_;
192};
193
197 std::string dirName();
198private:
199 std::string dir_name_;
200};
201
204public:
213 PathChecker(const std::string default_path, const std::string env_name = "");
214
216 virtual ~PathChecker() {};
217
233 std::string getPath(bool reset = false, const std::string explicit_path = "");
234
251 std::string validatePath(const std::string input_path_str,
252 bool enforce_path = shouldEnforceSecurity()) const;
253
271 std::string validateDirectory(const std::string input_path_str,
272 bool enforce_path = shouldEnforceSecurity()) const;
273
281 bool pathHasPermissions(mode_t permissions,
282 bool enforce_perms = shouldEnforceSecurity()) const;
283
285 std::string getDefaultPath() const {
286 return (default_path_);
287 }
288
290 std::string getEnvName() const {
291 return (env_name_);
292 }
293
295 bool isDefaultOverridden();
296
298 static bool shouldEnforceSecurity();
299
303 static void enableEnforcement(bool enable);
304
305private:
307 std::string default_path_;
308
310 std::string env_name_;
311
313 std::string path_;
314
316 bool default_overridden_;
317
319 static bool enforce_security_;
320};
321
323typedef boost::shared_ptr<PathChecker> PathCheckerPtr;
324
325} // namespace file
326} // namespace util
327} // namespace isc
328
329#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:216
std::string getDefaultPath() const
Fetches the default path.
Definition filesystem.h:285
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:290
SecurityError(const char *file, size_t line, const char *what)
Definition filesystem.h:31
SecurityWarn(const char *file, size_t line, const char *what)
Definition filesystem.h:23
bool amRunningAsRoot()
Indicates if current user is root.
boost::shared_ptr< PathChecker > PathCheckerPtr
Defines a pointer to a PathChecker.
Definition filesystem.h:323
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.