4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS system libraries
21 * FILE: dll/win32/userenv/profile.c
22 * PURPOSE: User profile code
23 * PROGRAMMERS: Eric Kohl
34 /* FUNCTIONS ***************************************************************/
37 AppendSystemPostfix(LPWSTR lpName
,
40 WCHAR szSystemRoot
[MAX_PATH
];
44 /* Build profile name postfix */
45 if (!ExpandEnvironmentStringsW(L
"%SystemRoot%",
49 DPRINT1("Error: %lu\n", GetLastError());
53 _wcsupr(szSystemRoot
);
55 /* Get name postfix */
56 szSystemRoot
[2] = L
'.';
57 lpszPostfix
= &szSystemRoot
[2];
58 lpszPtr
= lpszPostfix
;
59 while (*lpszPtr
!= (WCHAR
)0)
61 if (*lpszPtr
== L
'\\')
66 if (wcslen(lpName
) + wcslen(lpszPostfix
) + 1 >= dwMaxLength
)
68 DPRINT1("Error: buffer overflow\n");
69 SetLastError(ERROR_BUFFER_OVERFLOW
);
73 wcscat(lpName
, lpszPostfix
);
81 AcquireRemoveRestorePrivilege(IN BOOL bAcquire
)
85 TOKEN_PRIVILEGES TokenPriv
;
87 DPRINT("AcquireRemoveRestorePrivilege(%d)\n", bAcquire
);
89 if (OpenProcessToken(GetCurrentProcess(),
90 TOKEN_ADJUST_PRIVILEGES
,
93 TokenPriv
.PrivilegeCount
= 1;
94 TokenPriv
.Privileges
[0].Attributes
= (bAcquire
? SE_PRIVILEGE_ENABLED
: 0);
96 if (LookupPrivilegeValue(NULL
, SE_RESTORE_NAME
, &TokenPriv
.Privileges
[0].Luid
))
98 bRet
= AdjustTokenPrivileges(Token
, FALSE
, &TokenPriv
, 0, NULL
, NULL
);
102 DPRINT1("AdjustTokenPrivileges() failed with error %lu\n", GetLastError());
104 else if (GetLastError() == ERROR_NOT_ALL_ASSIGNED
)
106 DPRINT1("AdjustTokenPrivileges() succeeded, but with not all privileges assigned\n");
112 DPRINT1("LookupPrivilegeValue() failed with error %lu\n", GetLastError());
119 DPRINT1("OpenProcessToken() failed with error %lu\n", GetLastError());
128 CreateUserProfileA(PSID Sid
,
131 UNICODE_STRING UserName
;
134 if (!RtlCreateUnicodeStringFromAsciiz(&UserName
,
137 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
141 bResult
= CreateUserProfileW(Sid
, UserName
.Buffer
);
143 RtlFreeUnicodeString(&UserName
);
151 CreateUserProfileW(PSID Sid
,
154 WCHAR szRawProfilesPath
[MAX_PATH
];
155 WCHAR szProfilesPath
[MAX_PATH
];
156 WCHAR szUserProfilePath
[MAX_PATH
];
157 WCHAR szDefaultUserPath
[MAX_PATH
];
158 WCHAR szUserProfileName
[MAX_PATH
];
159 WCHAR szBuffer
[MAX_PATH
];
168 DPRINT("CreateUserProfileW() called\n");
170 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
171 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
175 if (Error
!= ERROR_SUCCESS
)
177 DPRINT1("Error: %lu\n", Error
);
178 SetLastError((DWORD
)Error
);
182 /* Get profiles path */
183 dwLength
= MAX_PATH
* sizeof(WCHAR
);
184 Error
= RegQueryValueExW(hKey
,
185 L
"ProfilesDirectory",
188 (LPBYTE
)szRawProfilesPath
,
190 if (Error
!= ERROR_SUCCESS
)
192 DPRINT1("Error: %lu\n", Error
);
194 SetLastError((DWORD
)Error
);
199 if (!ExpandEnvironmentStringsW(szRawProfilesPath
,
203 DPRINT1("Error: %lu\n", GetLastError());
208 /* create the profiles directory if it does not yet exist */
209 if (!CreateDirectoryW(szProfilesPath
, NULL
))
211 if (GetLastError() != ERROR_ALREADY_EXISTS
)
213 DPRINT1("Error: %lu\n", GetLastError());
218 /* Get default user path */
219 dwLength
= MAX_PATH
* sizeof(WCHAR
);
220 Error
= RegQueryValueExW(hKey
,
221 L
"DefaultUserProfile",
226 if (Error
!= ERROR_SUCCESS
)
228 DPRINT1("Error: %lu\n", Error
);
230 SetLastError((DWORD
)Error
);
236 wcscpy(szUserProfileName
, lpUserName
);
238 wcscpy(szUserProfilePath
, szProfilesPath
);
239 wcscat(szUserProfilePath
, L
"\\");
240 wcscat(szUserProfilePath
, szUserProfileName
);
242 wcscpy(szDefaultUserPath
, szProfilesPath
);
243 wcscat(szDefaultUserPath
, L
"\\");
244 wcscat(szDefaultUserPath
, szBuffer
);
246 /* Create user profile directory */
247 if (!CreateDirectoryW(szUserProfilePath
, NULL
))
249 if (GetLastError() != ERROR_ALREADY_EXISTS
)
251 DPRINT1("Error: %lu\n", GetLastError());
255 for (i
= 0; i
< 1000; i
++)
257 swprintf(szUserProfileName
, L
"%s.%03u", lpUserName
, i
);
259 wcscpy(szUserProfilePath
, szProfilesPath
);
260 wcscat(szUserProfilePath
, L
"\\");
261 wcscat(szUserProfilePath
, szUserProfileName
);
263 if (CreateDirectoryW(szUserProfilePath
, NULL
))
266 if (GetLastError() != ERROR_ALREADY_EXISTS
)
268 DPRINT1("Error: %lu\n", GetLastError());
274 /* Copy default user directory */
275 if (!CopyDirectory(szUserProfilePath
, szDefaultUserPath
))
277 DPRINT1("Error: %lu\n", GetLastError());
281 /* Add profile to profile list */
282 if (!ConvertSidToStringSidW(Sid
,
285 DPRINT1("Error: %lu\n", GetLastError());
290 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
291 wcscat(szBuffer
, SidString
);
293 /* Create user profile key */
294 Error
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
,
298 REG_OPTION_NON_VOLATILE
,
303 if (Error
!= ERROR_SUCCESS
)
305 DPRINT1("Error: %lu\n", Error
);
310 /* Create non-expanded user profile path */
311 wcscpy(szBuffer
, szRawProfilesPath
);
312 wcscat(szBuffer
, L
"\\");
313 wcscat(szBuffer
, szUserProfileName
);
315 /* Set 'ProfileImagePath' value (non-expanded) */
316 Error
= RegSetValueExW(hKey
,
321 (wcslen (szBuffer
) + 1) * sizeof(WCHAR
));
322 if (Error
!= ERROR_SUCCESS
)
324 DPRINT1("Error: %lu\n", Error
);
330 /* Set 'Sid' value */
331 Error
= RegSetValueExW(hKey
,
337 if (Error
!= ERROR_SUCCESS
)
339 DPRINT1("Error: %lu\n", Error
);
347 /* Create user hive name */
348 wcscpy(szBuffer
, szUserProfilePath
);
349 wcscat(szBuffer
, L
"\\ntuser.dat");
351 /* Acquire restore privilege */
352 if (!AcquireRemoveRestorePrivilege(TRUE
))
354 Error
= GetLastError();
355 DPRINT1("Error: %lu\n", Error
);
360 /* Create new user hive */
361 Error
= RegLoadKeyW(HKEY_USERS
,
364 AcquireRemoveRestorePrivilege(FALSE
);
365 if (Error
!= ERROR_SUCCESS
)
367 DPRINT1("Error: %lu\n", Error
);
372 /* Initialize user hive */
373 if (!CreateUserHive(SidString
, szUserProfilePath
))
375 Error
= GetLastError();
376 DPRINT1("Error: %lu\n", Error
);
380 /* Unload the hive */
381 AcquireRemoveRestorePrivilege(TRUE
);
382 RegUnLoadKeyW(HKEY_USERS
, SidString
);
383 AcquireRemoveRestorePrivilege(FALSE
);
386 LocalFree((HLOCAL
)SidString
);
387 SetLastError((DWORD
)Error
);
389 DPRINT("CreateUserProfileW() done\n");
397 CreateUserProfileExA(IN PSID pSid
,
398 IN LPCSTR lpUserName
,
399 IN LPCSTR lpUserHive OPTIONAL
,
400 OUT LPSTR lpProfileDir OPTIONAL
,
404 DPRINT1("CreateUserProfileExA() not implemented!\n");
411 CreateUserProfileExW(IN PSID pSid
,
412 IN LPCWSTR lpUserName
,
413 IN LPCWSTR lpUserHive OPTIONAL
,
414 OUT LPWSTR lpProfileDir OPTIONAL
,
418 DPRINT1("CreateUserProfileExW() not implemented!\n");
425 GetAllUsersProfileDirectoryA(LPSTR lpProfileDir
,
431 lpBuffer
= GlobalAlloc(GMEM_FIXED
,
432 *lpcchSize
* sizeof(WCHAR
));
433 if (lpBuffer
== NULL
)
436 bResult
= GetAllUsersProfileDirectoryW(lpBuffer
,
440 WideCharToMultiByte(CP_ACP
,
450 GlobalFree(lpBuffer
);
458 GetAllUsersProfileDirectoryW(LPWSTR lpProfileDir
,
461 WCHAR szProfilePath
[MAX_PATH
];
462 WCHAR szBuffer
[MAX_PATH
];
467 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
468 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
472 if (Error
!= ERROR_SUCCESS
)
474 DPRINT1("Error: %lu\n", Error
);
475 SetLastError((DWORD
)Error
);
479 /* Get profiles path */
480 dwLength
= sizeof(szBuffer
);
481 Error
= RegQueryValueExW(hKey
,
482 L
"ProfilesDirectory",
487 if (Error
!= ERROR_SUCCESS
)
489 DPRINT1("Error: %lu\n", Error
);
491 SetLastError((DWORD
)Error
);
496 if (!ExpandEnvironmentStringsW(szBuffer
,
500 DPRINT1("Error: %lu\n", GetLastError());
505 /* Get 'AllUsersProfile' name */
506 dwLength
= sizeof(szBuffer
);
507 Error
= RegQueryValueExW(hKey
,
513 if (Error
!= ERROR_SUCCESS
)
515 DPRINT1("Error: %lu\n", Error
);
517 SetLastError((DWORD
)Error
);
523 wcscat(szProfilePath
, L
"\\");
524 wcscat(szProfilePath
, szBuffer
);
526 dwLength
= wcslen(szProfilePath
) + 1;
527 if (lpProfileDir
!= NULL
)
529 if (*lpcchSize
< dwLength
)
531 *lpcchSize
= dwLength
;
532 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
536 wcscpy(lpProfileDir
, szProfilePath
);
539 *lpcchSize
= dwLength
;
547 GetDefaultUserProfileDirectoryA(LPSTR lpProfileDir
,
553 lpBuffer
= GlobalAlloc(GMEM_FIXED
,
554 *lpcchSize
* sizeof(WCHAR
));
555 if (lpBuffer
== NULL
)
558 bResult
= GetDefaultUserProfileDirectoryW(lpBuffer
,
562 WideCharToMultiByte(CP_ACP
,
572 GlobalFree(lpBuffer
);
580 GetDefaultUserProfileDirectoryW(LPWSTR lpProfileDir
,
583 WCHAR szProfilePath
[MAX_PATH
];
584 WCHAR szBuffer
[MAX_PATH
];
589 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
590 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
594 if (Error
!= ERROR_SUCCESS
)
596 DPRINT1("Error: %lu\n", Error
);
597 SetLastError((DWORD
)Error
);
601 /* Get profiles path */
602 dwLength
= sizeof(szBuffer
);
603 Error
= RegQueryValueExW(hKey
,
604 L
"ProfilesDirectory",
609 if (Error
!= ERROR_SUCCESS
)
611 DPRINT1("Error: %lu\n", Error
);
613 SetLastError((DWORD
)Error
);
618 if (!ExpandEnvironmentStringsW(szBuffer
,
622 DPRINT1("Error: %lu\n", GetLastError());
627 /* Get 'DefaultUserProfile' name */
628 dwLength
= sizeof(szBuffer
);
629 Error
= RegQueryValueExW(hKey
,
630 L
"DefaultUserProfile",
635 if (Error
!= ERROR_SUCCESS
)
637 DPRINT1("Error: %lu\n", Error
);
639 SetLastError((DWORD
)Error
);
645 wcscat(szProfilePath
, L
"\\");
646 wcscat(szProfilePath
, szBuffer
);
648 dwLength
= wcslen(szProfilePath
) + 1;
649 if (lpProfileDir
!= NULL
)
651 if (*lpcchSize
< dwLength
)
653 *lpcchSize
= dwLength
;
654 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
658 wcscpy(lpProfileDir
, szProfilePath
);
661 *lpcchSize
= dwLength
;
669 GetProfilesDirectoryA(LPSTR lpProfileDir
,
675 lpBuffer
= GlobalAlloc(GMEM_FIXED
,
676 *lpcchSize
* sizeof(WCHAR
));
677 if (lpBuffer
== NULL
)
680 bResult
= GetProfilesDirectoryW(lpBuffer
,
684 WideCharToMultiByte(CP_ACP
,
694 GlobalFree(lpBuffer
);
702 GetProfilesDirectoryW(LPWSTR lpProfilesDir
,
705 WCHAR szProfilesPath
[MAX_PATH
];
706 WCHAR szBuffer
[MAX_PATH
];
711 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
712 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
716 if (Error
!= ERROR_SUCCESS
)
718 DPRINT1("Error: %lu\n", Error
);
719 SetLastError((DWORD
)Error
);
723 /* Get profiles path */
724 dwLength
= sizeof(szBuffer
);
725 Error
= RegQueryValueExW(hKey
,
726 L
"ProfilesDirectory",
731 if (Error
!= ERROR_SUCCESS
)
733 DPRINT1("Error: %lu\n", Error
);
735 SetLastError((DWORD
)Error
);
742 if (!ExpandEnvironmentStringsW(szBuffer
,
746 DPRINT1("Error: %lu\n", GetLastError());
750 dwLength
= wcslen (szProfilesPath
) + 1;
751 if (lpProfilesDir
!= NULL
)
753 if (*lpcchSize
< dwLength
)
755 *lpcchSize
= dwLength
;
756 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
760 wcscpy(lpProfilesDir
, szProfilesPath
);
763 *lpcchSize
= dwLength
;
771 GetUserProfileDirectoryA(HANDLE hToken
,
778 lpBuffer
= GlobalAlloc(GMEM_FIXED
,
779 *lpcchSize
* sizeof(WCHAR
));
780 if (lpBuffer
== NULL
)
783 bResult
= GetUserProfileDirectoryW(hToken
,
788 WideCharToMultiByte(CP_ACP
,
798 GlobalFree(lpBuffer
);
806 GetUserProfileDirectoryW(HANDLE hToken
,
810 UNICODE_STRING SidString
;
811 WCHAR szKeyName
[MAX_PATH
];
812 WCHAR szRawImagePath
[MAX_PATH
];
813 WCHAR szImagePath
[MAX_PATH
];
818 if (!GetUserSidStringFromToken(hToken
,
821 DPRINT1("GetUserSidFromToken() failed\n");
825 DPRINT("SidString: '%wZ'\n", &SidString
);
828 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
832 RtlFreeUnicodeString(&SidString
);
834 DPRINT("KeyName: '%S'\n", szKeyName
);
836 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
841 if (Error
!= ERROR_SUCCESS
)
843 DPRINT1("Error: %lu\n", Error
);
844 SetLastError((DWORD
)Error
);
848 dwLength
= sizeof(szRawImagePath
);
849 Error
= RegQueryValueExW(hKey
,
853 (LPBYTE
)szRawImagePath
,
855 if (Error
!= ERROR_SUCCESS
)
857 DPRINT1("Error: %lu\n", Error
);
859 SetLastError((DWORD
)Error
);
865 DPRINT("RawImagePath: '%S'\n", szRawImagePath
);
868 if (!ExpandEnvironmentStringsW(szRawImagePath
,
872 DPRINT1 ("Error: %lu\n", GetLastError());
876 DPRINT("ImagePath: '%S'\n", szImagePath
);
878 dwLength
= wcslen (szImagePath
) + 1;
879 if (*lpcchSize
< dwLength
)
881 *lpcchSize
= dwLength
;
882 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
886 *lpcchSize
= dwLength
;
887 wcscpy(lpProfileDir
, szImagePath
);
895 CheckForLoadedProfile(HANDLE hToken
)
897 UNICODE_STRING SidString
;
900 DPRINT("CheckForLoadedProfile() called\n");
902 if (!GetUserSidStringFromToken(hToken
,
905 DPRINT1("GetUserSidFromToken() failed\n");
909 if (RegOpenKeyExW(HKEY_USERS
,
915 DPRINT("Profile not loaded\n");
916 RtlFreeUnicodeString(&SidString
);
922 RtlFreeUnicodeString(&SidString
);
924 DPRINT("Profile already loaded\n");
932 LoadUserProfileA(IN HANDLE hToken
,
933 IN OUT LPPROFILEINFOA lpProfileInfo
)
935 BOOL bResult
= FALSE
;
936 PROFILEINFOW ProfileInfoW
= {0};
939 DPRINT("LoadUserProfileA() called\n");
941 /* Check profile info */
942 if (!lpProfileInfo
|| (lpProfileInfo
->dwSize
!= sizeof(PROFILEINFOA
)) ||
943 (lpProfileInfo
->lpUserName
== NULL
) || (lpProfileInfo
->lpUserName
[0] == 0))
945 SetLastError(ERROR_INVALID_PARAMETER
);
949 /* Convert the structure to UNICODE... */
950 ProfileInfoW
.dwSize
= sizeof(PROFILEINFOW
);
951 ProfileInfoW
.dwFlags
= lpProfileInfo
->dwFlags
;
953 if (lpProfileInfo
->lpUserName
)
955 len
= MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpUserName
, -1, NULL
, 0);
956 ProfileInfoW
.lpUserName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
957 if (!ProfileInfoW
.lpUserName
)
959 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
962 MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpUserName
, -1, ProfileInfoW
.lpUserName
, len
);
965 if (lpProfileInfo
->lpProfilePath
)
967 len
= MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpProfilePath
, -1, NULL
, 0);
968 ProfileInfoW
.lpProfilePath
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
969 if (!ProfileInfoW
.lpProfilePath
)
971 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
974 MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpProfilePath
, -1, ProfileInfoW
.lpProfilePath
, len
);
977 if (lpProfileInfo
->lpDefaultPath
)
979 len
= MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpDefaultPath
, -1, NULL
, 0);
980 ProfileInfoW
.lpDefaultPath
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
981 if (!ProfileInfoW
.lpDefaultPath
)
983 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
986 MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpDefaultPath
, -1, ProfileInfoW
.lpDefaultPath
, len
);
989 if (lpProfileInfo
->lpServerName
)
991 len
= MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpServerName
, -1, NULL
, 0);
992 ProfileInfoW
.lpServerName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
993 if (!ProfileInfoW
.lpServerName
)
995 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
998 MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpServerName
, -1, ProfileInfoW
.lpServerName
, len
);
1001 if ((ProfileInfoW
.dwFlags
& PI_APPLYPOLICY
) != 0 && lpProfileInfo
->lpPolicyPath
)
1003 len
= MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpPolicyPath
, -1, NULL
, 0);
1004 ProfileInfoW
.lpPolicyPath
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1005 if (!ProfileInfoW
.lpPolicyPath
)
1007 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1010 MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpPolicyPath
, -1, ProfileInfoW
.lpPolicyPath
, len
);
1013 /* ... and call the UNICODE function */
1014 bResult
= LoadUserProfileW(hToken
, &ProfileInfoW
);
1016 /* Save the returned value */
1017 lpProfileInfo
->hProfile
= ProfileInfoW
.hProfile
;
1020 /* Memory cleanup */
1021 if (ProfileInfoW
.lpUserName
)
1022 HeapFree(GetProcessHeap(), 0, ProfileInfoW
.lpUserName
);
1024 if (ProfileInfoW
.lpProfilePath
)
1025 HeapFree(GetProcessHeap(), 0, ProfileInfoW
.lpProfilePath
);
1027 if (ProfileInfoW
.lpDefaultPath
)
1028 HeapFree(GetProcessHeap(), 0, ProfileInfoW
.lpDefaultPath
);
1030 if (ProfileInfoW
.lpServerName
)
1031 HeapFree(GetProcessHeap(), 0, ProfileInfoW
.lpServerName
);
1033 if ((ProfileInfoW
.dwFlags
& PI_APPLYPOLICY
) != 0 && ProfileInfoW
.lpPolicyPath
)
1034 HeapFree(GetProcessHeap(), 0, ProfileInfoW
.lpPolicyPath
);
1042 LoadUserProfileW(IN HANDLE hToken
,
1043 IN OUT LPPROFILEINFOW lpProfileInfo
)
1045 WCHAR szUserHivePath
[MAX_PATH
];
1046 LPWSTR UserName
= NULL
, Domain
= NULL
;
1047 DWORD UserNameLength
= 0, DomainLength
= 0;
1048 PTOKEN_USER UserSid
= NULL
;
1049 SID_NAME_USE AccountType
;
1050 UNICODE_STRING SidString
= { 0, 0, NULL
};
1053 DWORD dwLength
= sizeof(szUserHivePath
) / sizeof(szUserHivePath
[0]);
1055 DPRINT("LoadUserProfileW() called\n");
1057 /* Check profile info */
1058 if (!lpProfileInfo
|| (lpProfileInfo
->dwSize
!= sizeof(PROFILEINFOW
)) ||
1059 (lpProfileInfo
->lpUserName
== NULL
) || (lpProfileInfo
->lpUserName
[0] == 0))
1061 SetLastError(ERROR_INVALID_PARAMETER
);
1065 /* Don't load a profile twice */
1066 if (CheckForLoadedProfile(hToken
))
1068 DPRINT ("Profile already loaded\n");
1069 lpProfileInfo
->hProfile
= NULL
;
1073 if (lpProfileInfo
->lpProfilePath
)
1075 wcscpy(szUserHivePath
, lpProfileInfo
->lpProfilePath
);
1079 /* FIXME: check if MS Windows allows lpProfileInfo->lpProfilePath to be NULL */
1080 if (!GetProfilesDirectoryW(szUserHivePath
, &dwLength
))
1082 DPRINT1("GetProfilesDirectoryW() failed (error %ld)\n", GetLastError());
1087 /* Create user hive name */
1088 wcscat(szUserHivePath
, L
"\\");
1089 wcscat(szUserHivePath
, lpProfileInfo
->lpUserName
);
1090 wcscat(szUserHivePath
, L
"\\ntuser.dat");
1091 DPRINT("szUserHivePath: %S\n", szUserHivePath
);
1093 /* Create user profile directory if needed */
1094 if (GetFileAttributesW(szUserHivePath
) == INVALID_FILE_ATTRIBUTES
)
1097 if (GetTokenInformation(hToken
, TokenUser
, NULL
, 0, &dwLength
) ||
1098 GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
1100 DPRINT1 ("GetTokenInformation() failed\n");
1104 UserSid
= (PTOKEN_USER
)HeapAlloc(GetProcessHeap(), 0, dwLength
);
1107 DPRINT1("HeapAlloc() failed\n");
1108 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1112 if (!GetTokenInformation(hToken
, TokenUser
, UserSid
, dwLength
, &dwLength
))
1114 DPRINT1("GetTokenInformation() failed\n");
1121 if (UserNameLength
> 0)
1123 HeapFree(GetProcessHeap(), 0, UserName
);
1124 UserName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, UserNameLength
* sizeof(WCHAR
));
1127 DPRINT1("HeapAlloc() failed\n");
1128 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1132 if (DomainLength
> 0)
1134 HeapFree(GetProcessHeap(), 0, Domain
);
1135 Domain
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, DomainLength
* sizeof(WCHAR
));
1138 DPRINT1("HeapAlloc() failed\n");
1139 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1143 ret
= LookupAccountSidW(NULL
,
1150 } while (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
);
1154 DPRINT1("LookupAccountSidW() failed\n");
1158 /* Create profile */
1159 /* FIXME: ignore Domain? */
1160 DPRINT("UserName %S, Domain %S\n", UserName
, Domain
);
1161 ret
= CreateUserProfileW(UserSid
->User
.Sid
, UserName
);
1164 DPRINT1("CreateUserProfileW() failed\n");
1169 /* Get user SID string */
1170 ret
= GetUserSidStringFromToken(hToken
, &SidString
);
1173 DPRINT1("GetUserSidFromToken() failed\n");
1178 /* Acquire restore privilege */
1179 if (!AcquireRemoveRestorePrivilege(TRUE
))
1181 DPRINT1("AcquireRemoveRestorePrivilege() failed (Error %ld)\n", GetLastError());
1185 /* Load user registry hive */
1186 Error
= RegLoadKeyW(HKEY_USERS
,
1189 AcquireRemoveRestorePrivilege(FALSE
);
1191 /* HACK: Do not fail if the profile has already been loaded! */
1192 if (Error
== ERROR_SHARING_VIOLATION
)
1193 Error
= ERROR_SUCCESS
;
1195 if (Error
!= ERROR_SUCCESS
)
1197 DPRINT1("RegLoadKeyW() failed (Error %ld)\n", Error
);
1198 SetLastError((DWORD
)Error
);
1202 /* Open future HKEY_CURRENT_USER */
1203 Error
= RegOpenKeyExW(HKEY_USERS
,
1207 (PHKEY
)&lpProfileInfo
->hProfile
);
1208 if (Error
!= ERROR_SUCCESS
)
1210 DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error
);
1211 SetLastError((DWORD
)Error
);
1218 HeapFree(GetProcessHeap(), 0, UserSid
);
1219 HeapFree(GetProcessHeap(), 0, UserName
);
1220 HeapFree(GetProcessHeap(), 0, Domain
);
1221 RtlFreeUnicodeString(&SidString
);
1223 DPRINT("LoadUserProfileW() done\n");
1230 UnloadUserProfile(HANDLE hToken
,
1233 UNICODE_STRING SidString
;
1236 DPRINT("UnloadUserProfile() called\n");
1238 if (hProfile
== NULL
)
1240 DPRINT1("Invalid profile handle\n");
1241 SetLastError(ERROR_INVALID_PARAMETER
);
1245 RegCloseKey(hProfile
);
1247 if (!GetUserSidStringFromToken(hToken
,
1250 DPRINT1("GetUserSidFromToken() failed\n");
1254 DPRINT("SidString: '%wZ'\n", &SidString
);
1256 /* Acquire restore privilege */
1257 if (!AcquireRemoveRestorePrivilege(TRUE
))
1259 DPRINT1("AcquireRemoveRestorePrivilege() failed (Error %ld)\n", GetLastError());
1260 RtlFreeUnicodeString(&SidString
);
1264 /* Unload the hive */
1265 Error
= RegUnLoadKeyW(HKEY_USERS
,
1268 /* Remove restore privilege */
1269 AcquireRemoveRestorePrivilege(FALSE
);
1271 if (Error
!= ERROR_SUCCESS
)
1273 DPRINT1("RegUnLoadKeyW() failed (Error %ld)\n", Error
);
1274 RtlFreeUnicodeString(&SidString
);
1275 SetLastError((DWORD
)Error
);
1279 RtlFreeUnicodeString(&SidString
);
1281 DPRINT("UnloadUserProfile() done\n");
1289 DeleteProfileW(LPCWSTR lpSidString
,
1290 LPCWSTR lpProfilePath
,
1291 LPCWSTR lpComputerName
)
1293 DPRINT1("DeleteProfileW() not implemented!\n");
1300 DeleteProfileA(LPCSTR lpSidString
,
1301 LPCSTR lpProfilePath
,
1302 LPCSTR lpComputerName
)
1305 UNICODE_STRING SidString
, ProfilePath
, ComputerName
;
1307 DPRINT("DeleteProfileA() called\n");
1309 /* Conversion to UNICODE */
1311 RtlCreateUnicodeStringFromAsciiz(&SidString
,
1312 (LPSTR
)lpSidString
);
1315 RtlCreateUnicodeStringFromAsciiz(&ProfilePath
,
1316 (LPSTR
)lpProfilePath
);
1319 RtlCreateUnicodeStringFromAsciiz(&ComputerName
,
1320 (LPSTR
)lpComputerName
);
1322 /* Call the UNICODE function */
1323 bResult
= DeleteProfileW(SidString
.Buffer
,
1325 ComputerName
.Buffer
);
1327 /* Memory cleanup */
1329 RtlFreeUnicodeString(&SidString
);
1332 RtlFreeUnicodeString(&ProfilePath
);
1335 RtlFreeUnicodeString(&ComputerName
);
1343 GetProfileType(OUT PDWORD pdwFlags
)
1345 DPRINT1("GetProfileType() not implemented!\n");