Author: tfaber
Date: Wed Jul  4 18:28:49 2012
New Revision: 56825

URL: http://svn.reactos.org/svn/reactos?rev=56825&view=rev
Log:
[REGEDIT]
- Acquire the necessary privileges when loading/unloading hives. Patch by 
Hermes Belusca.
See issue #7159 for more details.

Modified:
    trunk/reactos/base/applications/regedit/framewnd.c

Modified: trunk/reactos/base/applications/regedit/framewnd.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/base/applications/regedit/framewnd.c?rev=56825&r1=56824&r2=56825&view=diff
==============================================================================
--- trunk/reactos/base/applications/regedit/framewnd.c [iso-8859-1] (original)
+++ trunk/reactos/base/applications/regedit/framewnd.c [iso-8859-1] Wed Jul  4 
18:28:49 2012
@@ -310,6 +310,38 @@
         break;
     }
     return FALSE;
+}
+
+static BOOL EnablePrivilege(LPCTSTR lpszPrivilegeName, LPCTSTR lpszSystemName, 
BOOL bEnablePrivilege)
+{
+    BOOL   bRet   = FALSE;
+    HANDLE hToken = NULL;
+
+    if (OpenProcessToken(GetCurrentProcess(),
+                         TOKEN_ADJUST_PRIVILEGES,
+                         &hToken))
+    {
+        TOKEN_PRIVILEGES tp;
+
+        tp.PrivilegeCount = 1;
+        tp.Privileges[0].Attributes = (bEnablePrivilege ? SE_PRIVILEGE_ENABLED 
: 0);
+
+        if (LookupPrivilegeValue(lpszSystemName,
+                                 lpszPrivilegeName,
+                                 &tp.Privileges[0].Luid))
+        {
+            bRet = AdjustTokenPrivileges(hToken,
+                                         FALSE,
+                                         &tp,
+                                         sizeof(tp),
+                                         NULL,
+                                         NULL);
+        }
+
+        CloseHandle(hToken);
+    }
+
+    return bRet;
 }
 
 static BOOL LoadHive(HWND hWnd)
@@ -340,7 +372,19 @@
     {
         if(DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_LOADHIVE), hWnd, 
&LoadHive_KeyNameInHookProc, (LPARAM)xPath))
         {
-            LONG regLoadResult = RegLoadKey(hRootKey, xPath, ofn.lpstrFile);
+            LONG regLoadResult;
+
+            /* Enable the required privileges */
+            EnablePrivilege(SE_BACKUP_NAME, NULL, TRUE);
+            EnablePrivilege(SE_RESTORE_NAME, NULL, TRUE);
+
+            /* Load the hive */
+            regLoadResult = RegLoadKey(hRootKey, xPath, ofn.lpstrFile);
+
+            /* Disable the privileges */
+            EnablePrivilege(SE_RESTORE_NAME, NULL, FALSE);
+            EnablePrivilege(SE_BACKUP_NAME, NULL, FALSE);
+
             if(regLoadResult == ERROR_SUCCESS)
             {
                 /* refresh tree and list views */
@@ -373,8 +417,18 @@
     pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
     /* load and set the caption and flags for dialog */
     LoadString(hInst, IDS_UNLOAD_HIVE, Caption, COUNT_OF(Caption));
-    /* now unload the hive */
+
+    /* Enable the required privileges */
+    EnablePrivilege(SE_BACKUP_NAME, NULL, TRUE);
+    EnablePrivilege(SE_RESTORE_NAME, NULL, TRUE);
+
+    /* Unload the hive */
     regUnloadResult = RegUnLoadKey(hRootKey, pszKeyPath);
+
+    /* Disable the privileges */
+    EnablePrivilege(SE_RESTORE_NAME, NULL, FALSE);
+    EnablePrivilege(SE_BACKUP_NAME, NULL, FALSE);
+
     if(regUnloadResult == ERROR_SUCCESS)
     {
         /* refresh tree and list views */


Reply via email to