cmake/Source/CPack/WiX/cmWIXAccessControlList.cxx

127 lines
3.3 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"
#include <CPack/cmCPackGenerator.h>
#include <cmSystemTools.h>
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);
2016-07-09 11:21:54 +02:00
for (size_t i = 0; i < entries.size(); ++i) {
2015-04-27 22:25:09 +02:00
this->CreatePermissionElement(entries[i]);
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
std::string user_and_domain = entry.substr(0, pos);
std::string permission_string = entry.substr(pos + 1);
pos = user_and_domain.find('@');
std::string user;
std::string 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
std::vector<std::string> permissions =
cmSystemTools::tokenize(permission_string, ",");
this->SourceWriter.BeginElement("Permission");
this->SourceWriter.AddAttribute("User", user);
2016-07-09 11:21:54 +02:00
if (!domain.empty()) {
2015-04-27 22:25:09 +02:00
this->SourceWriter.AddAttribute("Domain", domain);
2016-07-09 11:21:54 +02:00
}
for (size_t i = 0; i < permissions.size(); ++i) {
2015-04-27 22:25:09 +02:00
this->EmitBooleanAttribute(entry,
2016-07-09 11:21:54 +02:00
cmSystemTools::TrimWhitespace(permissions[i]));
}
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
{
2016-07-09 11:21:54 +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");
}