Esempio n. 1
0
static BOOL UnloadHive(HWND hWnd)
{
    WCHAR Caption[128];
    LPCWSTR pszKeyPath;
    HKEY hRootKey;
    LONG regUnloadResult;

    /* get the item key to unload */
    pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
    /* load and set the caption and flags for dialog */
    LoadStringW(hInst, IDS_UNLOAD_HIVE, Caption, COUNT_OF(Caption));

    /* Enable the 'restore' privilege, unload the hive, disable the privilege */
    EnablePrivilege(SE_RESTORE_NAME, NULL, TRUE);
    regUnloadResult = RegUnLoadKeyW(hRootKey, pszKeyPath);
    EnablePrivilege(SE_RESTORE_NAME, NULL, FALSE);

    if(regUnloadResult == ERROR_SUCCESS)
    {
        /* refresh tree and list views */
        RefreshTreeView(g_pChildWnd->hTreeWnd);
        pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
        RefreshListView(g_pChildWnd->hListWnd, hRootKey, pszKeyPath);
    }
    else
    {
        ErrorMessageBox(hWnd, Caption, regUnloadResult);
        return FALSE;
    }
    return TRUE;
}
Esempio n. 2
0
BOOL
WINAPI
UnloadUserProfile(HANDLE hToken,
                  HANDLE hProfile)
{
    UNICODE_STRING SidString;
    LONG Error;

    DPRINT("UnloadUserProfile() called\n");

    if (hProfile == NULL)
    {
        DPRINT1("Invalid profile handle\n");
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }

    RegCloseKey(hProfile);

    if (!GetUserSidFromToken(hToken,
                             &SidString))
    {
        DPRINT1("GetUserSidFromToken() failed\n");
        return FALSE;
    }

    DPRINT("SidString: '%wZ'\n", &SidString);

    /* Acquire restore privilege */
    if (!AcquireRemoveRestorePrivilege(TRUE))
    {
        DPRINT1("AcquireRemoveRestorePrivilege() failed (Error %ld)\n", GetLastError());
        RtlFreeUnicodeString(&SidString);
        return FALSE;
    }

    /* Unload the hive */
    Error = RegUnLoadKeyW(HKEY_USERS,
                          SidString.Buffer);

    /* Remove restore privilege */
    AcquireRemoveRestorePrivilege(FALSE);

    if (Error != ERROR_SUCCESS)
    {
        DPRINT1("RegUnLoadKeyW() failed (Error %ld)\n", Error);
        RtlFreeUnicodeString(&SidString);
        SetLastError((DWORD)Error);
        return FALSE;
    }

    RtlFreeUnicodeString(&SidString);

    DPRINT("UnloadUserProfile() done\n");

    return TRUE;
}
LONG
Win32U_RegUnLoadKey(HKEY keyName,   // IN:
                    LPCSTR subKey)  // IN:
{
   LONG ret;
   utf16_t *subKeyW = Unicode_GetAllocUTF16(subKey);

   ret = RegUnLoadKeyW(keyName, subKeyW);
   free(subKeyW);

   return ret;
}
Esempio n. 4
0
BOOL
WINAPI
CreateUserProfileW(PSID Sid,
                   LPCWSTR lpUserName)
{
    WCHAR szRawProfilesPath[MAX_PATH];
    WCHAR szProfilesPath[MAX_PATH];
    WCHAR szUserProfilePath[MAX_PATH];
    WCHAR szDefaultUserPath[MAX_PATH];
    WCHAR szUserProfileName[MAX_PATH];
    WCHAR szBuffer[MAX_PATH];
    LPWSTR SidString;
    DWORD dwLength;
    DWORD dwDisposition;
    UINT i;
    HKEY hKey;
    BOOL bRet = TRUE;
    LONG Error;

    DPRINT("CreateUserProfileW() called\n");

    Error = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
                          L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
                          0,
                          KEY_QUERY_VALUE,
                          &hKey);
    if (Error != ERROR_SUCCESS)
    {
        DPRINT1("Error: %lu\n", Error);
        SetLastError((DWORD)Error);
        return FALSE;
    }

    /* Get profiles path */
    dwLength = MAX_PATH * sizeof(WCHAR);
    Error = RegQueryValueExW(hKey,
                             L"ProfilesDirectory",
                             NULL,
                             NULL,
                             (LPBYTE)szRawProfilesPath,
                             &dwLength);
    if (Error != ERROR_SUCCESS)
    {
        DPRINT1("Error: %lu\n", Error);
        RegCloseKey(hKey);
        SetLastError((DWORD)Error);
        return FALSE;
    }

    /* Expand it */
    if (!ExpandEnvironmentStringsW(szRawProfilesPath,
                                   szProfilesPath,
                                   MAX_PATH))
    {
        DPRINT1("Error: %lu\n", GetLastError());
        RegCloseKey(hKey);
        return FALSE;
    }

    /* create the profiles directory if it does not yet exist */
    if (!CreateDirectoryW(szProfilesPath, NULL))
    {
        if (GetLastError() != ERROR_ALREADY_EXISTS)
        {
            DPRINT1("Error: %lu\n", GetLastError());
            return FALSE;
        }
    }

    /* Get default user path */
    dwLength = MAX_PATH * sizeof(WCHAR);
    Error = RegQueryValueExW(hKey,
                             L"DefaultUserProfile",
                             NULL,
                             NULL,
                             (LPBYTE)szBuffer,
                             &dwLength);
    if (Error != ERROR_SUCCESS)
    {
        DPRINT1("Error: %lu\n", Error);
        RegCloseKey(hKey);
        SetLastError((DWORD)Error);
        return FALSE;
    }

    RegCloseKey (hKey);

    wcscpy(szUserProfileName, lpUserName);

    wcscpy(szUserProfilePath, szProfilesPath);
    wcscat(szUserProfilePath, L"\\");
    wcscat(szUserProfilePath, szUserProfileName);

    wcscpy(szDefaultUserPath, szProfilesPath);
    wcscat(szDefaultUserPath, L"\\");
    wcscat(szDefaultUserPath, szBuffer);

    /* Create user profile directory */
    if (!CreateDirectoryW(szUserProfilePath, NULL))
    {
        if (GetLastError() != ERROR_ALREADY_EXISTS)
        {
            DPRINT1("Error: %lu\n", GetLastError());
            return FALSE;
        }

        for (i = 0; i < 1000; i++)
        {
            swprintf(szUserProfileName, L"%s.%03u", lpUserName, i);

            wcscpy(szUserProfilePath, szProfilesPath);
            wcscat(szUserProfilePath, L"\\");
            wcscat(szUserProfilePath, szUserProfileName);

            if (CreateDirectoryW(szUserProfilePath, NULL))
                break;

            if (GetLastError() != ERROR_ALREADY_EXISTS)
            {
                DPRINT1("Error: %lu\n", GetLastError());
                return FALSE;
            }
        }
    }

    /* Copy default user directory */
    if (!CopyDirectory(szUserProfilePath, szDefaultUserPath))
    {
        DPRINT1("Error: %lu\n", GetLastError());
        return FALSE;
    }

    /* Add profile to profile list */
    if (!ConvertSidToStringSidW(Sid,
                                &SidString))
    {
        DPRINT1("Error: %lu\n", GetLastError());
        return FALSE;
    }

    wcscpy(szBuffer,
           L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
    wcscat(szBuffer, SidString);

    /* Create user profile key */
    Error = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
                            szBuffer,
                            0,
                            NULL,
                            REG_OPTION_NON_VOLATILE,
                            KEY_ALL_ACCESS,
                            NULL,
                            &hKey,
                            &dwDisposition);
    if (Error != ERROR_SUCCESS)
    {
        DPRINT1("Error: %lu\n", Error);
        bRet = FALSE;
        goto Done;
    }

    /* Create non-expanded user profile path */
    wcscpy(szBuffer, szRawProfilesPath);
    wcscat(szBuffer, L"\\");
    wcscat(szBuffer, szUserProfileName);

    /* Set 'ProfileImagePath' value (non-expanded) */
    Error = RegSetValueExW(hKey,
                           L"ProfileImagePath",
                           0,
                           REG_EXPAND_SZ,
                           (LPBYTE)szBuffer,
                           (wcslen (szBuffer) + 1) * sizeof(WCHAR));
    if (Error != ERROR_SUCCESS)
    {
        DPRINT1("Error: %lu\n", Error);
        RegCloseKey(hKey);
        bRet = FALSE;
        goto Done;
    }

    /* Set 'Sid' value */
    Error = RegSetValueExW(hKey,
                           L"Sid",
                           0,
                           REG_BINARY,
                           Sid,
                           GetLengthSid(Sid));
    if (Error != ERROR_SUCCESS)
    {
        DPRINT1("Error: %lu\n", Error);
        RegCloseKey(hKey);
        bRet = FALSE;
        goto Done;
    }

    RegCloseKey(hKey);

    /* Create user hive name */
    wcscpy(szBuffer, szUserProfilePath);
    wcscat(szBuffer, L"\\ntuser.dat");

    /* Acquire restore privilege */
    if (!AcquireRemoveRestorePrivilege(TRUE))
    {
        Error = GetLastError();
        DPRINT1("Error: %lu\n", Error);
        bRet = FALSE;
        goto Done;
    }

    /* Create new user hive */
    Error = RegLoadKeyW(HKEY_USERS,
                        SidString,
                        szBuffer);
    AcquireRemoveRestorePrivilege(FALSE);
    if (Error != ERROR_SUCCESS)
    {
        DPRINT1("Error: %lu\n", Error);
        bRet = FALSE;
        goto Done;
    }

    /* Initialize user hive */
    if (!CreateUserHive(SidString, szUserProfilePath))
    {
        Error = GetLastError();
        DPRINT1("Error: %lu\n", Error);
        bRet = FALSE;
    }

    /* Unload the hive */
    AcquireRemoveRestorePrivilege(TRUE);
    RegUnLoadKeyW(HKEY_USERS, SidString);
    AcquireRemoveRestorePrivilege(FALSE);

Done:
    LocalFree((HLOCAL)SidString);
    SetLastError((DWORD)Error);

    DPRINT("CreateUserProfileW() done\n");

    return bRet;
}