WCHAR szUserProfileName[MAX_PATH];
WCHAR szBuffer[MAX_PATH];
LPWSTR SidString;
- DWORD dwLength;
+ DWORD dwType, dwLength;
DWORD dwDisposition;
UINT i;
HKEY hKey;
DPRINT("CreateUserProfileExW(%p %S %S %p %lu %d)\n",
pSid, lpUserName, lpUserHive, lpProfileDir, dwDirSize, bWin9xUpg);
+ /* Parameters validation */
+ if (!pSid || !lpUserName)
+ {
+ SetLastError(ERROR_INVALID_PARAMETER);
+ return FALSE;
+ }
+
/*
* TODO:
- * - Add support for lpUserHive, lpProfileDir.
+ * - Add support for lpUserHive.
* - bWin9xUpg is obsolete. Don't waste your time implementing this.
*/
Error = RegQueryValueExW(hKey,
L"ProfilesDirectory",
NULL,
- NULL,
+ &dwType,
(LPBYTE)szRawProfilesPath,
&dwLength);
- if (Error != ERROR_SUCCESS)
+ if ((Error != ERROR_SUCCESS) || (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
{
DPRINT1("Error: %lu\n", Error);
RegCloseKey(hKey);
return FALSE;
}
- /* create the profiles directory if it does not yet exist */
+ /* Create the profiles directory if it does not exist yet */
+ // FIXME: Security!
if (!CreateDirectoryW(szProfilesPath, NULL))
{
if (GetLastError() != ERROR_ALREADY_EXISTS)
Error = RegQueryValueExW(hKey,
L"DefaultUserProfile",
NULL,
- NULL,
+ &dwType,
(LPBYTE)szBuffer,
&dwLength);
- if (Error != ERROR_SUCCESS)
+ if ((Error != ERROR_SUCCESS) || (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
{
DPRINT1("Error: %lu\n", Error);
RegCloseKey(hKey);
return FALSE;
}
- RegCloseKey (hKey);
+ RegCloseKey(hKey);
- wcscpy(szUserProfileName, lpUserName);
+ StringCbCopyW(szUserProfileName, sizeof(szUserProfileName), lpUserName);
- wcscpy(szUserProfilePath, szProfilesPath);
- wcscat(szUserProfilePath, L"\\");
- wcscat(szUserProfilePath, szUserProfileName);
+ /* Create user profile directory */
- wcscpy(szDefaultUserPath, szProfilesPath);
- wcscat(szDefaultUserPath, L"\\");
- wcscat(szDefaultUserPath, szBuffer);
+ StringCbCopyW(szUserProfilePath, sizeof(szUserProfilePath), szProfilesPath);
+ StringCbCatW(szUserProfilePath, sizeof(szUserProfilePath), L"\\");
+ StringCbCatW(szUserProfilePath, sizeof(szUserProfilePath), szUserProfileName);
- /* Create user profile directory */
+ // FIXME: Security!
if (!CreateDirectoryW(szUserProfilePath, NULL))
{
if (GetLastError() != ERROR_ALREADY_EXISTS)
{
swprintf(szUserProfileName, L"%s.%03u", lpUserName, i);
- wcscpy(szUserProfilePath, szProfilesPath);
- wcscat(szUserProfilePath, L"\\");
- wcscat(szUserProfilePath, szUserProfileName);
+ StringCbCopyW(szUserProfilePath, sizeof(szUserProfilePath), szProfilesPath);
+ StringCbCatW(szUserProfilePath, sizeof(szUserProfilePath), L"\\");
+ StringCbCatW(szUserProfilePath, sizeof(szUserProfilePath), szUserProfileName);
+ // FIXME: Security!
if (CreateDirectoryW(szUserProfilePath, NULL))
break;
}
/* Copy default user directory */
+
+ StringCbCopyW(szDefaultUserPath, sizeof(szDefaultUserPath), szProfilesPath);
+ StringCbCatW(szDefaultUserPath, sizeof(szDefaultUserPath), L"\\");
+ StringCbCatW(szDefaultUserPath, sizeof(szDefaultUserPath), szBuffer);
+
+ // FIXME: Security!
if (!CopyDirectory(szUserProfilePath, szDefaultUserPath))
{
DPRINT1("Error: %lu\n", GetLastError());
return FALSE;
}
- wcscpy(szBuffer,
- L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
- wcscat(szBuffer, SidString);
+ StringCbCopyW(szBuffer, sizeof(szBuffer),
+ L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
+ StringCbCatW(szBuffer, sizeof(szBuffer), SidString);
/* Create user profile key */
Error = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
}
/* Create non-expanded user profile path */
- wcscpy(szBuffer, szRawProfilesPath);
- wcscat(szBuffer, L"\\");
- wcscat(szBuffer, szUserProfileName);
+ StringCbCopyW(szBuffer, sizeof(szBuffer), szRawProfilesPath);
+ StringCbCatW(szBuffer, sizeof(szBuffer), L"\\");
+ StringCbCatW(szBuffer, sizeof(szBuffer), szUserProfileName);
/* Set 'ProfileImagePath' value (non-expanded) */
Error = RegSetValueExW(hKey,
RegCloseKey(hKey);
- /* Create user hive name */
- wcscpy(szBuffer, szUserProfilePath);
- wcscat(szBuffer, L"\\ntuser.dat");
+ /* Create user hive file */
+
+ /* Use the default hive file name */
+ StringCbCopyW(szBuffer, sizeof(szBuffer), szUserProfilePath);
+ StringCbCatW(szBuffer, sizeof(szBuffer), L"\\ntuser.dat");
/* Acquire restore privilege */
if (!AcquireRemoveRestorePrivilege(TRUE))
goto done;
}
- /* Create new user hive */
+ /* Load the user hive */
Error = RegLoadKeyW(HKEY_USERS,
SidString,
szBuffer);
RegUnLoadKeyW(HKEY_USERS, SidString);
AcquireRemoveRestorePrivilege(FALSE);
+ /*
+ * If the caller wants to retrieve the user profile path,
+ * give it now. 'dwDirSize' is the number of characters.
+ */
+ if (lpProfileDir && dwDirSize)
+ StringCchCopyW(lpProfileDir, dwDirSize, szUserProfilePath);
+
done:
LocalFree((HLOCAL)SidString);
SetLastError((DWORD)Error);
LPWSTR lpBuffer;
BOOL bResult;
+ if (!lpcchSize)
+ {
+ SetLastError(ERROR_INVALID_PARAMETER);
+ return FALSE;
+ }
+
lpBuffer = GlobalAlloc(GMEM_FIXED,
*lpcchSize * sizeof(WCHAR));
if (lpBuffer == NULL)
bResult = GetAllUsersProfileDirectoryW(lpBuffer,
lpcchSize);
- if (bResult)
+ if (bResult && lpProfileDir)
{
- WideCharToMultiByte(CP_ACP,
- 0,
- lpBuffer,
- -1,
- lpProfileDir,
- *lpcchSize,
- NULL,
- NULL);
+ bResult = WideCharToMultiByte(CP_ACP,
+ 0,
+ lpBuffer,
+ -1,
+ lpProfileDir,
+ *lpcchSize,
+ NULL,
+ NULL);
}
GlobalFree(lpBuffer);
{
WCHAR szProfilePath[MAX_PATH];
WCHAR szBuffer[MAX_PATH];
- DWORD dwLength;
+ DWORD dwType, dwLength;
HKEY hKey;
LONG Error;
Error = RegQueryValueExW(hKey,
L"ProfilesDirectory",
NULL,
- NULL,
+ &dwType,
(LPBYTE)szBuffer,
&dwLength);
- if (Error != ERROR_SUCCESS)
+ if ((Error != ERROR_SUCCESS) || (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
{
DPRINT1("Error: %lu\n", Error);
RegCloseKey(hKey);
/* Expand it */
if (!ExpandEnvironmentStringsW(szBuffer,
szProfilePath,
- MAX_PATH))
+ ARRAYSIZE(szProfilePath)))
{
DPRINT1("Error: %lu\n", GetLastError());
- RegCloseKey (hKey);
+ RegCloseKey(hKey);
return FALSE;
}
Error = RegQueryValueExW(hKey,
L"AllUsersProfile",
NULL,
- NULL,
+ &dwType,
(LPBYTE)szBuffer,
&dwLength);
- if (Error != ERROR_SUCCESS)
+ if ((Error != ERROR_SUCCESS) || (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
{
DPRINT1("Error: %lu\n", Error);
RegCloseKey(hKey);
return FALSE;
}
- RegCloseKey (hKey);
+ RegCloseKey(hKey);
- wcscat(szProfilePath, L"\\");
- wcscat(szProfilePath, szBuffer);
+ StringCbCatW(szProfilePath, sizeof(szProfilePath), L"\\");
+ StringCbCatW(szProfilePath, sizeof(szProfilePath), szBuffer);
dwLength = wcslen(szProfilePath) + 1;
- if (lpProfileDir != NULL)
+ if (lpProfileDir && (*lpcchSize >= dwLength))
{
- if (*lpcchSize < dwLength)
- {
- *lpcchSize = dwLength;
- SetLastError(ERROR_INSUFFICIENT_BUFFER);
- return FALSE;
- }
-
- wcscpy(lpProfileDir, szProfilePath);
- }
-
- *lpcchSize = dwLength;
-
- return TRUE;
+ StringCchCopyW(lpProfileDir, *lpcchSize, szProfilePath);
+ *lpcchSize = dwLength;
+ return TRUE;
+ }
+ else // if (!lpProfileDir || (*lpcchSize < dwLength))
+ {
+ *lpcchSize = dwLength;
+ SetLastError(ERROR_INSUFFICIENT_BUFFER);
+ return FALSE;
+ }
}
LPWSTR lpBuffer;
BOOL bResult;
+ if (!lpcchSize)
+ {
+ SetLastError(ERROR_INVALID_PARAMETER);
+ return FALSE;
+ }
+
lpBuffer = GlobalAlloc(GMEM_FIXED,
*lpcchSize * sizeof(WCHAR));
if (lpBuffer == NULL)
bResult = GetDefaultUserProfileDirectoryW(lpBuffer,
lpcchSize);
- if (bResult)
+ if (bResult && lpProfileDir)
{
- WideCharToMultiByte(CP_ACP,
- 0,
- lpBuffer,
- -1,
- lpProfileDir,
- *lpcchSize,
- NULL,
- NULL);
+ bResult = WideCharToMultiByte(CP_ACP,
+ 0,
+ lpBuffer,
+ -1,
+ lpProfileDir,
+ *lpcchSize,
+ NULL,
+ NULL);
}
GlobalFree(lpBuffer);
{
WCHAR szProfilePath[MAX_PATH];
WCHAR szBuffer[MAX_PATH];
- DWORD dwLength;
+ DWORD dwType, dwLength;
HKEY hKey;
LONG Error;
Error = RegQueryValueExW(hKey,
L"ProfilesDirectory",
NULL,
- NULL,
+ &dwType,
(LPBYTE)szBuffer,
&dwLength);
- if (Error != ERROR_SUCCESS)
+ if ((Error != ERROR_SUCCESS) || (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
{
DPRINT1("Error: %lu\n", Error);
RegCloseKey(hKey);
/* Expand it */
if (!ExpandEnvironmentStringsW(szBuffer,
szProfilePath,
- MAX_PATH))
+ ARRAYSIZE(szProfilePath)))
{
DPRINT1("Error: %lu\n", GetLastError());
RegCloseKey(hKey);
Error = RegQueryValueExW(hKey,
L"DefaultUserProfile",
NULL,
- NULL,
+ &dwType,
(LPBYTE)szBuffer,
&dwLength);
- if (Error != ERROR_SUCCESS)
+ if ((Error != ERROR_SUCCESS) || (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
{
DPRINT1("Error: %lu\n", Error);
RegCloseKey(hKey);
RegCloseKey(hKey);
- wcscat(szProfilePath, L"\\");
- wcscat(szProfilePath, szBuffer);
+ StringCbCatW(szProfilePath, sizeof(szProfilePath), L"\\");
+ StringCbCatW(szProfilePath, sizeof(szProfilePath), szBuffer);
dwLength = wcslen(szProfilePath) + 1;
- if (lpProfileDir != NULL)
+ if (lpProfileDir && (*lpcchSize >= dwLength))
{
- if (*lpcchSize < dwLength)
- {
- *lpcchSize = dwLength;
- SetLastError(ERROR_INSUFFICIENT_BUFFER);
- return FALSE;
- }
-
- wcscpy(lpProfileDir, szProfilePath);
- }
-
- *lpcchSize = dwLength;
-
- return TRUE;
+ StringCchCopyW(lpProfileDir, *lpcchSize, szProfilePath);
+ *lpcchSize = dwLength;
+ return TRUE;
+ }
+ else // if (!lpProfileDir || (*lpcchSize < dwLength))
+ {
+ *lpcchSize = dwLength;
+ SetLastError(ERROR_INSUFFICIENT_BUFFER);
+ return FALSE;
+ }
}
LPWSTR lpBuffer;
BOOL bResult;
- if (!lpProfileDir || !lpcchSize)
+ if (!lpcchSize)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
bResult = GetProfilesDirectoryW(lpBuffer,
lpcchSize);
- if (bResult)
+ if (bResult && lpProfileDir)
{
bResult = WideCharToMultiByte(CP_ACP,
0,
{
WCHAR szProfilesPath[MAX_PATH];
WCHAR szBuffer[MAX_PATH];
- DWORD dwLength;
+ DWORD dwType, dwLength;
HKEY hKey;
LONG Error;
- BOOL bRet = FALSE;
if (!lpcchSize)
{
Error = RegQueryValueExW(hKey,
L"ProfilesDirectory",
NULL,
- NULL,
+ &dwType,
(LPBYTE)szBuffer,
&dwLength);
- if (Error != ERROR_SUCCESS)
+ if ((Error != ERROR_SUCCESS) || (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
{
DPRINT1("Error: %lu\n", Error);
RegCloseKey(hKey);
}
dwLength = wcslen(szProfilesPath) + 1;
- if (lpProfilesDir != NULL)
+ if (lpProfilesDir && (*lpcchSize >= dwLength))
{
- if (*lpcchSize < dwLength)
- {
- SetLastError(ERROR_INSUFFICIENT_BUFFER);
- }
- else
- {
- wcscpy(lpProfilesDir, szProfilesPath);
- bRet = TRUE;
- }
+ StringCchCopyW(lpProfilesDir, *lpcchSize, szProfilesPath);
+ *lpcchSize = dwLength;
+ return TRUE;
}
- else
+ else // if (!lpProfilesDir || (*lpcchSize < dwLength))
{
+ *lpcchSize = dwLength;
SetLastError(ERROR_INSUFFICIENT_BUFFER);
+ return FALSE;
}
-
- *lpcchSize = dwLength;
-
- return bRet;
}
LPWSTR lpBuffer;
BOOL bResult;
- if (!lpProfileDir || !lpcchSize)
+ if (!lpcchSize)
{
- SetLastError( ERROR_INVALID_PARAMETER );
+ SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
bResult = GetUserProfileDirectoryW(hToken,
lpBuffer,
lpcchSize);
- if (bResult)
+ if (bResult && lpProfileDir)
{
- WideCharToMultiByte(CP_ACP,
- 0,
- lpBuffer,
- -1,
- lpProfileDir,
- *lpcchSize,
- NULL,
- NULL);
+ bResult = WideCharToMultiByte(CP_ACP,
+ 0,
+ lpBuffer,
+ -1,
+ lpProfileDir,
+ *lpcchSize,
+ NULL,
+ NULL);
}
GlobalFree(lpBuffer);
WCHAR szKeyName[MAX_PATH];
WCHAR szRawImagePath[MAX_PATH];
WCHAR szImagePath[MAX_PATH];
- DWORD dwLength;
+ DWORD dwType, dwLength;
HKEY hKey;
LONG Error;
return FALSE;
}
- if (!GetUserSidStringFromToken(hToken,
- &SidString))
+ /* Get the user SID string */
+ if (!GetUserSidStringFromToken(hToken, &SidString))
{
- DPRINT1("GetUserSidFromToken() failed\n");
+ DPRINT1("GetUserSidStringFromToken() failed\n");
return FALSE;
}
DPRINT("SidString: '%wZ'\n", &SidString);
- wcscpy(szKeyName,
- L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
- wcscat(szKeyName,
- SidString.Buffer);
+ StringCbCopyW(szKeyName, sizeof(szKeyName),
+ L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
+ StringCbCatW(szKeyName, sizeof(szKeyName), SidString.Buffer);
RtlFreeUnicodeString(&SidString);
Error = RegQueryValueExW(hKey,
L"ProfileImagePath",
NULL,
- NULL,
+ &dwType,
(LPBYTE)szRawImagePath,
&dwLength);
- if (Error != ERROR_SUCCESS)
+ if ((Error != ERROR_SUCCESS) || (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
{
DPRINT1("Error: %lu\n", Error);
RegCloseKey(hKey);
DPRINT("ImagePath: '%S'\n", szImagePath);
- dwLength = wcslen (szImagePath) + 1;
- if (*lpcchSize < dwLength)
+ dwLength = wcslen(szImagePath) + 1;
+ if (lpProfileDir && (*lpcchSize >= dwLength))
+ {
+ StringCchCopyW(lpProfileDir, *lpcchSize, szImagePath);
+ *lpcchSize = dwLength;
+ return TRUE;
+ }
+ else // if (!lpProfileDir || (*lpcchSize < dwLength))
{
*lpcchSize = dwLength;
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return FALSE;
}
-
- *lpcchSize = dwLength;
- wcscpy(lpProfileDir, szImagePath);
-
- return TRUE;
}
DPRINT("CheckForLoadedProfile() called\n");
- if (!GetUserSidStringFromToken(hToken,
- &SidString))
+ /* Get the user SID string */
+ if (!GetUserSidStringFromToken(hToken, &SidString))
{
- DPRINT1("GetUserSidFromToken() failed\n");
+ DPRINT1("GetUserSidStringFromToken() failed\n");
return FALSE;
}
if (lpProfileInfo->lpProfilePath)
{
- wcscpy(szUserHivePath, lpProfileInfo->lpProfilePath);
+ /* Use the caller's specified roaming user profile path */
+ StringCbCopyW(szUserHivePath, sizeof(szUserHivePath), lpProfileInfo->lpProfilePath);
}
else
{
}
/* Create user hive name */
- wcscat(szUserHivePath, L"\\");
- wcscat(szUserHivePath, lpProfileInfo->lpUserName);
- wcscat(szUserHivePath, L"\\ntuser.dat");
+ StringCbCatW(szUserHivePath, sizeof(szUserHivePath), L"\\");
+ StringCbCatW(szUserHivePath, sizeof(szUserHivePath), lpProfileInfo->lpUserName);
+ StringCbCatW(szUserHivePath, sizeof(szUserHivePath), L"\\ntuser.dat");
DPRINT("szUserHivePath: %S\n", szUserHivePath);
/* Create user profile directory if needed */
}
}
- /* Get user SID string */
+ /* Get the user SID string */
ret = GetUserSidStringFromToken(hToken, &SidString);
if (!ret)
{
- DPRINT1("GetUserSidFromToken() failed\n");
+ DPRINT1("GetUserSidStringFromToken() failed\n");
goto cleanup;
}
ret = FALSE;
RegCloseKey(hProfile);
- if (!GetUserSidStringFromToken(hToken,
- &SidString))
+ /* Get the user SID string */
+ if (!GetUserSidStringFromToken(hToken, &SidString))
{
- DPRINT1("GetUserSidFromToken() failed\n");
+ DPRINT1("GetUserSidStringFromToken() failed\n");
return FALSE;
}