Changeset 121 for trunk/Utilities/Miscellaneous/SetFileOwner
- Timestamp:
- Sep 9, 2012, 8:35:58 AM (11 years ago)
- Location:
- trunk/Utilities/Miscellaneous/SetFileOwner
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Utilities/Miscellaneous/SetFileOwner
- Property svn:ignore
-
old new 1 1 *.suo 2 Debug 3 ipch 4 *.sdf
-
- Property svn:ignore
-
trunk/Utilities/Miscellaneous/SetFileOwner/SetFileOwner.cpp
r118 r121 10 10 #include <atlfile.h> 11 11 12 VOID PrintOwner(LPCTSTR pszPath)12 VOID EnablePrivilege(LPCTSTR pszPrivilege) 13 13 { 14 PSID pSecurityIdentifer = NULL;15 ATLENSURE_SUCCEEDED(HRESULT_FROM_WIN32(GetNamedSecurityInfo(const_cast<LPTSTR>(pszPath), SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &pSecurityIdentifer, NULL, NULL, NULL, NULL)));16 CSid SecurityIdentifer((const SID*) pSecurityIdentifer);17 _tprintf(_T("Owner: %s\\%s\n"), SecurityIdentifer.Domain(), SecurityIdentifer.AccountName());18 }19 20 int _tmain(int argc, _TCHAR* argv[])21 {22 #pragma region Enable SeRestorePrivilege23 14 CAccessToken Token; 24 15 if(!Token.GetThreadToken(TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES)) … … 28 19 ATLENSURE_THROW(Token.GetProcessToken(TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES), AtlHresultFromLastError()); 29 20 } 30 ATLENSURE_THROW(Token.EnablePrivilege(SE_RESTORE_NAME), AtlHresultFromLastError()); 31 #pragma endregion 21 ATLENSURE_THROW(Token.EnablePrivilege(pszPrivilege), AtlHresultFromLastError()); 22 } 23 VOID PrintOwner(LPCTSTR pszPath) 24 { 25 PSID pSecurityIdentifer = NULL; 26 CSecurityDesc SecurityDescriptor; 27 ATLENSURE_SUCCEEDED(HRESULT_FROM_WIN32(GetNamedSecurityInfo(const_cast<LPTSTR>(pszPath), SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &pSecurityIdentifer, NULL, NULL, NULL, reinterpret_cast<PSECURITY_DESCRIPTOR*>(&SecurityDescriptor)))); 28 CSid SecurityIdentifer((const SID*) pSecurityIdentifer); 29 _tprintf(_T("Owner: %s\\%s\n"), SecurityIdentifer.Domain(), SecurityIdentifer.AccountName()); 30 } 31 VOID SetOwner(LPCTSTR pszPath, const CSid& SecurityIdentifer) 32 { 33 ATLENSURE_SUCCEEDED(HRESULT_FROM_WIN32(SetNamedSecurityInfo(const_cast<LPTSTR>(pszPath), SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, const_cast<SID*>((const SID*) SecurityIdentifer), NULL, NULL, NULL))); 34 } 35 36 int _tmain(int argc, _TCHAR* argv[]) 37 { 32 38 static LPCTSTR g_pszPath = _T("D:\\Projects\\Alax.Info\\Repository-Public\\Utilities\\Miscellaneous\\SetFileOwner\\ReadMe.txt"); 39 EnablePrivilege(SE_RESTORE_NAME); 33 40 PrintOwner(g_pszPath); 34 41 CSid SecurityIdentifer; 35 42 ATLENSURE_THROW(SecurityIdentifer.LoadAccount(_T("Guest")), AtlHresultFromLastError()); 36 43 _tprintf(_T("New Owner: %s\\%s\n"), SecurityIdentifer.Domain(), SecurityIdentifer.AccountName()); 37 ATLENSURE_SUCCEEDED(HRESULT_FROM_WIN32(SetNamedSecurityInfo(const_cast<LPTSTR>(g_pszPath), SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, const_cast<SID*>((const SID*) SecurityIdentifer), NULL, NULL, NULL))); 44 _ATLTRY 45 { 46 SetOwner(g_pszPath, SecurityIdentifer); 47 } 48 _ATLCATCH(Exception) 49 { 50 _tprintf(_T("Exception 0x%08x\n"), (HRESULT) Exception); 51 // NOTE: This might be the worst case scenario: changing ownership is perhaps protected by DACL 52 EnablePrivilege(SE_TAKE_OWNERSHIP_NAME); 53 SetOwner(g_pszPath, SecurityIdentifer); 54 } 38 55 PrintOwner(g_pszPath); 39 56 return 0;
Note: See TracChangeset
for help on using the changeset viewer.