1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System |
3 |
|
|
*/ |
4 |
|
|
|
5 |
|
|
#include "cvmfs_suid_util.h" |
6 |
|
|
|
7 |
|
|
#include <sys/stat.h> |
8 |
|
|
#include <unistd.h> |
9 |
|
|
|
10 |
|
|
#include <cassert> |
11 |
|
|
#include <climits> |
12 |
|
|
#include <cstdlib> |
13 |
|
|
|
14 |
|
|
#include "sanitizer.h" |
15 |
|
|
|
16 |
|
|
using namespace std; // NOLINT |
17 |
|
|
|
18 |
|
|
namespace cvmfs_suid { |
19 |
|
|
|
20 |
|
|
/** |
21 |
|
|
* Makes a systemd mount unit string from the given path, such as |
22 |
|
|
* a-b-c.mount from /a/b/c |
23 |
|
|
*/ |
24 |
|
120 |
string EscapeSystemdUnit(const string &path) { |
25 |
✗✓ |
120 |
assert(!path.empty()); |
26 |
|
|
|
27 |
|
120 |
string normalized_path(path); |
28 |
|
|
size_t pos; |
29 |
✓✓ |
345 |
while ((pos = normalized_path.find("//")) != string::npos) { |
30 |
|
105 |
normalized_path.replace(pos, 2, "/"); |
31 |
|
|
} |
32 |
|
|
|
33 |
✓✓ |
120 |
if (normalized_path == "/") |
34 |
|
60 |
return "-.mount"; |
35 |
|
|
|
36 |
|
60 |
sanitizer::InputSanitizer sanitizer("az AZ 09 _"); |
37 |
|
60 |
unsigned length = normalized_path.length(); |
38 |
|
60 |
string result; |
39 |
✓✓ |
480 |
for (unsigned i = 0; i < length; ++i) { |
40 |
|
420 |
char c = normalized_path[i]; |
41 |
✓✓ |
420 |
if (c == '/') { |
42 |
✓✓✓✓
|
180 |
if ((i == 0) || (i == length - 1)) |
43 |
|
60 |
continue; |
44 |
|
120 |
result.push_back('-'); |
45 |
✓✓✓✓
|
255 |
} else if ((c == '.') && (i > 0)) { |
46 |
|
15 |
result.push_back('.'); |
47 |
✓✗✗✓
|
225 |
} else if (sanitizer.IsValid(string(&c, 1))) { |
48 |
|
195 |
result.push_back(c); |
49 |
|
|
} else { |
50 |
|
30 |
result.push_back('\\'); |
51 |
|
30 |
result.push_back('x'); |
52 |
|
30 |
result.push_back((c / 16) + ((c / 16 <= 9) ? '0' : 'a'-10)); |
53 |
✓✓ |
30 |
result.push_back((c % 16) + ((c % 16 <= 9) ? '0' : 'a'-10)); |
54 |
|
|
} |
55 |
|
|
} |
56 |
|
|
|
57 |
|
60 |
return result + ".mount"; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
|
61 |
|
30 |
bool PathExists(const std::string &path) { |
62 |
|
|
struct stat info; |
63 |
|
30 |
int retval = stat(path.c_str(), &info); |
64 |
|
30 |
return retval == 0; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
|
68 |
|
45 |
string ResolvePath(const std::string &path) { |
69 |
|
|
char buf[PATH_MAX]; |
70 |
|
45 |
char *retval = realpath(path.c_str(), buf); |
71 |
✗✓ |
45 |
if (retval == NULL) return ""; |
72 |
|
45 |
return string(buf); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
} // namespace cvmfs_suid |