Ignore:
Timestamp:
Sep 9, 2012, 8:35:58 AM (11 years ago)
Author:
roman
Message:
 
Location:
trunk/Utilities/Miscellaneous/SetFileOwner
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Utilities/Miscellaneous/SetFileOwner

    • Property svn:ignore
      •  

        old new  
        11*.suo
         2Debug
         3ipch
         4*.sdf
  • trunk/Utilities/Miscellaneous/SetFileOwner/SetFileOwner.cpp

    r118 r121  
    1010#include <atlfile.h>
    1111
    12 VOID PrintOwner(LPCTSTR pszPath)
     12VOID EnablePrivilege(LPCTSTR pszPrivilege)
    1313{
    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 SeRestorePrivilege
    2314        CAccessToken Token;
    2415        if(!Token.GetThreadToken(TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES))
     
    2819                ATLENSURE_THROW(Token.GetProcessToken(TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES), AtlHresultFromLastError());
    2920        }
    30         ATLENSURE_THROW(Token.EnablePrivilege(SE_RESTORE_NAME), AtlHresultFromLastError());
    31         #pragma endregion
     21        ATLENSURE_THROW(Token.EnablePrivilege(pszPrivilege), AtlHresultFromLastError());
     22}
     23VOID 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}
     31VOID 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
     36int _tmain(int argc, _TCHAR* argv[])
     37{
    3238        static LPCTSTR g_pszPath = _T("D:\\Projects\\Alax.Info\\Repository-Public\\Utilities\\Miscellaneous\\SetFileOwner\\ReadMe.txt");
     39        EnablePrivilege(SE_RESTORE_NAME);
    3340        PrintOwner(g_pszPath);
    3441        CSid SecurityIdentifer;
    3542        ATLENSURE_THROW(SecurityIdentifer.LoadAccount(_T("Guest")), AtlHresultFromLastError());
    3643        _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        }
    3855        PrintOwner(g_pszPath);
    3956        return 0;
Note: See TracChangeset for help on using the changeset viewer.