class MangleAccountName public Singleton MangleAccountName public Take

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class MangleAccountName : public Singleton<MangleAccountName> {
public:
// Takes 4.52223e-06s on i7-8700k @ 4.7GHz
std::string mangle(string_view name, string_view guid) {
thread_local timespec last_modify_time = {0, 0};
thread_local std::string domain;
static constexpr zstring_view qas_config = "/etc/opt/quest/vas/vas.conf";
bool found = false;
std::string hostname = getMachineName();
try {
struct stat attrib;
if (stat(qas_config.data(), &attrib) == 0) {
if (noteq(last_modify_time, attrib.st_mtim)) {
std::ifstream fin(qas_config.data());
for (std::string line; std::getline(fin, line);) {
auto pos = line.find(hostname);
if (pos == std::string::npos) {
continue;
}
if (!((pos == 0 || isspace(line[pos - 1])) &&
(pos + hostname.length() == line.length() ||
isspace(line[pos + hostname.length()]) ||
line[pos + hostname.length()] == '='))) {
continue;
}
auto pos_equal = line.find("=", pos + 1);
if (pos_equal == std::string::npos) {
continue;
}
auto l_domain = line.substr(pos_equal + 1);
trim(l_domain);
/* exception safe */
domain = std::move(l_domain);
found = true;
//writeLog(Debug, "Found domain from qas for hostname %s: %s", hostname, domain);
break;
}
last_modify_time = attrib.st_mtim;
} else {
//writeLog(Debug, "Using precached domain name: %s", domain);
found = true;
}
}
} catch (std::exception &e) {
report_exception(e);
}
if (!found) {
last_modify_time = {0, 0};
domain.clear();
domain.reserve(hostname.length() + 2 + guid.length() + 1);
domain.append(hostname);
domain += "_{";
domain.append(guid.data(), guid.length());
domain += "}";
//writeLog(Debug, "Using machine name for out of domain machine: %s", domain);
}
std::string account_name;
account_name.reserve(name.length() + 1 + domain.length());
account_name.append(name.data(), name.length());
account_name += "@";
account_name += domain;
std::transform(account_name.begin(), account_name.end(), account_name.begin(), [](unsigned char c){ return std::tolower(c); });
return account_name;
}
private:
bool noteq(const timespec &a, const timespec &b) {
return (a.tv_sec == b.tv_sec) ?
a.tv_nsec != b.tv_nsec :
a.tv_sec != b.tv_sec;
}
friend class Singleton<MangleAccountName>;
MangleAccountName() = default;
};