win32acl.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. static void SetACLPrivileges();
  2. static bool ReadSacl=false;
  3. #ifndef SFX_MODULE
  4. void ExtractACL20(Archive &Arc,const wchar *FileName)
  5. {
  6. SetACLPrivileges();
  7. if (Arc.BrokenHeader)
  8. {
  9. uiMsg(UIERROR_ACLBROKEN,Arc.FileName,FileName);
  10. ErrHandler.SetErrorCode(RARX_CRC);
  11. return;
  12. }
  13. if (Arc.EAHead.Method<0x31 || Arc.EAHead.Method>0x35 || Arc.EAHead.UnpVer>VER_PACK)
  14. {
  15. uiMsg(UIERROR_ACLUNKNOWN,Arc.FileName,FileName);
  16. ErrHandler.SetErrorCode(RARX_WARNING);
  17. return;
  18. }
  19. ComprDataIO DataIO;
  20. Unpack Unpack(&DataIO);
  21. Unpack.Init(0x10000,false);
  22. Array<byte> UnpData(Arc.EAHead.UnpSize);
  23. DataIO.SetUnpackToMemory(&UnpData[0],Arc.EAHead.UnpSize);
  24. DataIO.SetPackedSizeToRead(Arc.EAHead.DataSize);
  25. DataIO.EnableShowProgress(false);
  26. DataIO.SetFiles(&Arc,NULL);
  27. DataIO.UnpHash.Init(HASH_CRC32,1);
  28. Unpack.SetDestSize(Arc.EAHead.UnpSize);
  29. Unpack.DoUnpack(Arc.EAHead.UnpVer,false);
  30. if (Arc.EAHead.EACRC!=DataIO.UnpHash.GetCRC32())
  31. {
  32. uiMsg(UIERROR_ACLBROKEN,Arc.FileName,FileName);
  33. ErrHandler.SetErrorCode(RARX_CRC);
  34. return;
  35. }
  36. SECURITY_INFORMATION si=OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|
  37. DACL_SECURITY_INFORMATION;
  38. if (ReadSacl)
  39. si|=SACL_SECURITY_INFORMATION;
  40. SECURITY_DESCRIPTOR *sd=(SECURITY_DESCRIPTOR *)&UnpData[0];
  41. int SetCode=SetFileSecurity(FileName,si,sd);
  42. if (!SetCode)
  43. {
  44. uiMsg(UIERROR_ACLSET,Arc.FileName,FileName);
  45. DWORD LastError=GetLastError();
  46. ErrHandler.SysErrMsg();
  47. if (LastError==ERROR_ACCESS_DENIED && !IsUserAdmin())
  48. uiMsg(UIERROR_NEEDADMIN);
  49. ErrHandler.SetErrorCode(RARX_WARNING);
  50. }
  51. }
  52. #endif
  53. void ExtractACL(Archive &Arc,const wchar *FileName)
  54. {
  55. Array<byte> SubData;
  56. if (!Arc.ReadSubData(&SubData,NULL,false))
  57. return;
  58. return; // OPENMPT ADDITION
  59. SetACLPrivileges();
  60. SECURITY_INFORMATION si=OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|
  61. DACL_SECURITY_INFORMATION;
  62. if (ReadSacl)
  63. si|=SACL_SECURITY_INFORMATION;
  64. SECURITY_DESCRIPTOR *sd=(SECURITY_DESCRIPTOR *)&SubData[0];
  65. int SetCode=SetFileSecurity(FileName,si,sd);
  66. if (!SetCode)
  67. {
  68. wchar LongName[NM];
  69. if (GetWinLongPath(FileName,LongName,ASIZE(LongName)))
  70. SetCode=SetFileSecurity(LongName,si,sd);
  71. }
  72. if (!SetCode)
  73. {
  74. uiMsg(UIERROR_ACLSET,Arc.FileName,FileName);
  75. DWORD LastError=GetLastError();
  76. ErrHandler.SysErrMsg();
  77. if (LastError==ERROR_ACCESS_DENIED && !IsUserAdmin())
  78. uiMsg(UIERROR_NEEDADMIN);
  79. ErrHandler.SetErrorCode(RARX_WARNING);
  80. }
  81. }
  82. void SetACLPrivileges()
  83. {
  84. return; // OPENMPT ADDITION
  85. static bool InitDone=false;
  86. if (InitDone)
  87. return;
  88. if (SetPrivilege(SE_SECURITY_NAME))
  89. ReadSacl=true;
  90. SetPrivilege(SE_RESTORE_NAME);
  91. InitDone=true;
  92. }
  93. bool SetPrivilege(LPCTSTR PrivName)
  94. {
  95. bool Success=false;
  96. HANDLE hToken;
  97. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
  98. {
  99. TOKEN_PRIVILEGES tp;
  100. tp.PrivilegeCount = 1;
  101. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  102. if (LookupPrivilegeValue(NULL,PrivName,&tp.Privileges[0].Luid) &&
  103. AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) &&
  104. GetLastError() == ERROR_SUCCESS)
  105. Success=true;
  106. CloseHandle(hToken);
  107. }
  108. return Success;
  109. }