cmWIXAccessControlList.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2014 Kitware, Inc.
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmWIXAccessControlList.h"
  11. #include <CPack/cmCPackGenerator.h>
  12. #include <cmSystemTools.h>
  13. cmWIXAccessControlList::cmWIXAccessControlList(
  14. cmCPackLog *logger,
  15. cmInstalledFile const& installedFile,
  16. cmWIXSourceWriter &sourceWriter):
  17. Logger(logger),
  18. InstalledFile(installedFile),
  19. SourceWriter(sourceWriter)
  20. {
  21. }
  22. bool cmWIXAccessControlList::Apply()
  23. {
  24. std::vector<std::string> entries;
  25. this->InstalledFile.GetPropertyAsList("CPACK_WIX_ACL", entries);
  26. for(size_t i = 0; i < entries.size(); ++i)
  27. {
  28. this->CreatePermissionElement(entries[i]);
  29. }
  30. return true;
  31. }
  32. void cmWIXAccessControlList::CreatePermissionElement(
  33. std::string const& entry)
  34. {
  35. std::string::size_type pos = entry.find('=');
  36. if(pos == std::string::npos)
  37. {
  38. this->ReportError(entry, "Did not find mandatory '='");
  39. return;
  40. }
  41. std::string user_and_domain = entry.substr(0, pos);
  42. std::string permission_string = entry.substr(pos + 1);
  43. pos = user_and_domain.find('@');
  44. std::string user;
  45. std::string domain;
  46. if(pos != std::string::npos)
  47. {
  48. user = user_and_domain.substr(0, pos);
  49. domain = user_and_domain.substr(pos + 1);
  50. }
  51. else
  52. {
  53. user = user_and_domain;
  54. }
  55. std::vector<std::string> permissions =
  56. cmSystemTools::tokenize(permission_string, ",");
  57. this->SourceWriter.BeginElement("Permission");
  58. this->SourceWriter.AddAttribute("User", user);
  59. if(!domain.empty())
  60. {
  61. this->SourceWriter.AddAttribute("Domain", domain);
  62. }
  63. for(size_t i = 0; i < permissions.size(); ++i)
  64. {
  65. this->EmitBooleanAttribute(entry,
  66. cmSystemTools::TrimWhitespace(permissions[i]));
  67. }
  68. this->SourceWriter.EndElement("Permission");
  69. }
  70. void cmWIXAccessControlList::ReportError(
  71. std::string const& entry,
  72. std::string const& message)
  73. {
  74. cmCPackLogger(cmCPackLog::LOG_ERROR,
  75. "Failed processing ACL entry '" << entry <<
  76. "': " << message << std::endl);
  77. }
  78. bool cmWIXAccessControlList::IsBooleanAttribute(std::string const& name)
  79. {
  80. static const char* validAttributes[] =
  81. {
  82. "Append",
  83. "ChangePermission",
  84. "CreateChild",
  85. "CreateFile",
  86. "CreateLink",
  87. "CreateSubkeys",
  88. "Delete",
  89. "DeleteChild",
  90. "EnumerateSubkeys",
  91. "Execute",
  92. "FileAllRights",
  93. "GenericAll",
  94. "GenericExecute",
  95. "GenericRead",
  96. "GenericWrite",
  97. "Notify",
  98. "Read",
  99. "ReadAttributes",
  100. "ReadExtendedAttributes",
  101. "ReadPermission",
  102. "SpecificRightsAll",
  103. "Synchronize",
  104. "TakeOwnership",
  105. "Traverse",
  106. "Write",
  107. "WriteAttributes",
  108. "WriteExtendedAttributes",
  109. 0
  110. };
  111. size_t i = 0;
  112. while(validAttributes[i])
  113. {
  114. if(name == validAttributes[i++]) return true;
  115. }
  116. return false;
  117. }
  118. void cmWIXAccessControlList::EmitBooleanAttribute(
  119. std::string const& entry, std::string const& name)
  120. {
  121. if(!this->IsBooleanAttribute(name))
  122. {
  123. std::stringstream message;
  124. message << "Unknown boolean attribute '" << name << "'";
  125. this->ReportError(entry, message.str());
  126. }
  127. this->SourceWriter.AddAttribute(name, "yes");
  128. }