cmake/Source/CPack/WiX/cmWIXAccessControlList.cxx

129 lines
3.4 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2015-04-27 22:25:09 +02:00
#include "cmWIXAccessControlList.h"
2020-08-30 11:54:41 +02:00
#include <cm/string_view>
2017-07-20 19:35:53 +02:00
#include "cmCPackGenerator.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-07-20 19:35:53 +02:00
#include "cmSystemTools.h"
2015-04-27 22:25:09 +02:00
cmWIXAccessControlList::cmWIXAccessControlList(
2016-07-09 11:21:54 +02:00
cmCPackLog* logger, cmInstalledFile const& installedFile,
cmWIXSourceWriter& sourceWriter)
: Logger(logger)
, InstalledFile(installedFile)
, SourceWriter(sourceWriter)
2015-04-27 22:25:09 +02:00
{
}
bool cmWIXAccessControlList::Apply()
{
std::vector<std::string> entries;
this->InstalledFile.GetPropertyAsList("CPACK_WIX_ACL", entries);
2018-01-26 17:06:56 +01:00
for (std::string const& entry : entries) {
this->CreatePermissionElement(entry);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
return true;
}
2016-07-09 11:21:54 +02:00
void cmWIXAccessControlList::CreatePermissionElement(std::string const& entry)
2015-04-27 22:25:09 +02:00
{
std::string::size_type pos = entry.find('=');
2016-07-09 11:21:54 +02:00
if (pos == std::string::npos) {
2015-04-27 22:25:09 +02:00
this->ReportError(entry, "Did not find mandatory '='");
return;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
cm::string_view enview(entry);
cm::string_view user_and_domain = enview.substr(0, pos);
cm::string_view permission_string = enview.substr(pos + 1);
2015-04-27 22:25:09 +02:00
pos = user_and_domain.find('@');
2020-08-30 11:54:41 +02:00
cm::string_view user;
cm::string_view domain;
2016-07-09 11:21:54 +02:00
if (pos != std::string::npos) {
2015-04-27 22:25:09 +02:00
user = user_and_domain.substr(0, pos);
domain = user_and_domain.substr(pos + 1);
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
user = user_and_domain;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2020-02-01 23:06:01 +01:00
std::vector<std::string> permissions = cmTokenize(permission_string, ",");
2015-04-27 22:25:09 +02:00
this->SourceWriter.BeginElement("Permission");
2020-08-30 11:54:41 +02:00
this->SourceWriter.AddAttribute("User", std::string(user));
2016-07-09 11:21:54 +02:00
if (!domain.empty()) {
2020-08-30 11:54:41 +02:00
this->SourceWriter.AddAttribute("Domain", std::string(domain));
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
for (std::string const& permission : permissions) {
2020-02-01 23:06:01 +01:00
this->EmitBooleanAttribute(entry, cmTrimWhitespace(permission));
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
this->SourceWriter.EndElement("Permission");
}
2016-07-09 11:21:54 +02:00
void cmWIXAccessControlList::ReportError(std::string const& entry,
std::string const& message)
2015-04-27 22:25:09 +02:00
{
2018-08-09 18:06:22 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Failed processing ACL entry '" << entry << "': " << message
<< std::endl);
2015-04-27 22:25:09 +02:00
}
bool cmWIXAccessControlList::IsBooleanAttribute(std::string const& name)
{
2016-07-09 11:21:54 +02:00
static const char* validAttributes[] = {
/* clang-format needs this comment to break after the opening brace */
2015-04-27 22:25:09 +02:00
"Append",
"ChangePermission",
"CreateChild",
"CreateFile",
"CreateLink",
"CreateSubkeys",
"Delete",
"DeleteChild",
"EnumerateSubkeys",
"Execute",
"FileAllRights",
"GenericAll",
"GenericExecute",
"GenericRead",
"GenericWrite",
"Notify",
"Read",
"ReadAttributes",
"ReadExtendedAttributes",
"ReadPermission",
"SpecificRightsAll",
"Synchronize",
"TakeOwnership",
"Traverse",
"Write",
"WriteAttributes",
"WriteExtendedAttributes",
0
};
size_t i = 0;
2016-07-09 11:21:54 +02:00
while (validAttributes[i]) {
if (name == validAttributes[i++])
return true;
}
2015-04-27 22:25:09 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
void cmWIXAccessControlList::EmitBooleanAttribute(std::string const& entry,
std::string const& name)
2015-04-27 22:25:09 +02:00
{
2016-07-09 11:21:54 +02:00
if (!this->IsBooleanAttribute(name)) {
2016-10-30 18:24:19 +01:00
std::ostringstream message;
2015-04-27 22:25:09 +02:00
message << "Unknown boolean attribute '" << name << "'";
this->ReportError(entry, message.str());
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
this->SourceWriter.AddAttribute(name, "yes");
}