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 if (!lpProfileDir
|| !lpcchSize
)
677 SetLastError(ERROR_INVALID_PARAMETER
);
681 lpBuffer
= GlobalAlloc(GMEM_FIXED
,
682 *lpcchSize
* sizeof(WCHAR
));
683 if (lpBuffer
== NULL
)
686 bResult
= GetProfilesDirectoryW(lpBuffer
,
690 bResult
= WideCharToMultiByte(CP_ACP
,
700 GlobalFree(lpBuffer
);
708 GetProfilesDirectoryW(LPWSTR lpProfilesDir
,
711 WCHAR szProfilesPath
[MAX_PATH
];
712 WCHAR szBuffer
[MAX_PATH
];
720 SetLastError(ERROR_INVALID_PARAMETER
);
724 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
725 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
729 if (Error
!= ERROR_SUCCESS
)
731 DPRINT1("Error: %lu\n", Error
);
732 SetLastError((DWORD
)Error
);
736 /* Get profiles path */
737 dwLength
= sizeof(szBuffer
);
738 Error
= RegQueryValueExW(hKey
,
739 L
"ProfilesDirectory",
744 if (Error
!= ERROR_SUCCESS
)
746 DPRINT1("Error: %lu\n", Error
);
748 SetLastError((DWORD
)Error
);
755 if (!ExpandEnvironmentStringsW(szBuffer
,
759 DPRINT1("Error: %lu\n", GetLastError());
763 dwLength
= wcslen (szProfilesPath
) + 1;
764 if (lpProfilesDir
!= NULL
)
766 if (*lpcchSize
< dwLength
)
768 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
772 wcscpy(lpProfilesDir
, szProfilesPath
);
778 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
781 *lpcchSize
= dwLength
;
789 GetUserProfileDirectoryA(HANDLE hToken
,
796 if (!lpProfileDir
|| !lpcchSize
)
798 SetLastError( ERROR_INVALID_PARAMETER
);
802 lpBuffer
= GlobalAlloc(GMEM_FIXED
,
803 *lpcchSize
* sizeof(WCHAR
));
804 if (lpBuffer
== NULL
)
807 bResult
= GetUserProfileDirectoryW(hToken
,
812 WideCharToMultiByte(CP_ACP
,
822 GlobalFree(lpBuffer
);
830 GetUserProfileDirectoryW(HANDLE hToken
,
834 UNICODE_STRING SidString
;
835 WCHAR szKeyName
[MAX_PATH
];
836 WCHAR szRawImagePath
[MAX_PATH
];
837 WCHAR szImagePath
[MAX_PATH
];
844 SetLastError( ERROR_INVALID_PARAMETER
);
848 if (!GetUserSidStringFromToken(hToken
,
851 DPRINT1("GetUserSidFromToken() failed\n");
855 DPRINT("SidString: '%wZ'\n", &SidString
);
858 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
862 RtlFreeUnicodeString(&SidString
);
864 DPRINT("KeyName: '%S'\n", szKeyName
);
866 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
871 if (Error
!= ERROR_SUCCESS
)
873 DPRINT1("Error: %lu\n", Error
);
874 SetLastError((DWORD
)Error
);
878 dwLength
= sizeof(szRawImagePath
);
879 Error
= RegQueryValueExW(hKey
,
883 (LPBYTE
)szRawImagePath
,
885 if (Error
!= ERROR_SUCCESS
)
887 DPRINT1("Error: %lu\n", Error
);
889 SetLastError((DWORD
)Error
);
895 DPRINT("RawImagePath: '%S'\n", szRawImagePath
);
898 if (!ExpandEnvironmentStringsW(szRawImagePath
,
902 DPRINT1 ("Error: %lu\n", GetLastError());
906 DPRINT("ImagePath: '%S'\n", szImagePath
);
908 dwLength
= wcslen (szImagePath
) + 1;
909 if (*lpcchSize
< dwLength
)
911 *lpcchSize
= dwLength
;
912 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
916 *lpcchSize
= dwLength
;
917 wcscpy(lpProfileDir
, szImagePath
);
925 CheckForLoadedProfile(HANDLE hToken
)
927 UNICODE_STRING SidString
;
930 DPRINT("CheckForLoadedProfile() called\n");
932 if (!GetUserSidStringFromToken(hToken
,
935 DPRINT1("GetUserSidFromToken() failed\n");
939 if (RegOpenKeyExW(HKEY_USERS
,
945 DPRINT("Profile not loaded\n");
946 RtlFreeUnicodeString(&SidString
);
952 RtlFreeUnicodeString(&SidString
);
954 DPRINT("Profile already loaded\n");
962 LoadUserProfileA(IN HANDLE hToken
,
963 IN OUT LPPROFILEINFOA lpProfileInfo
)
965 BOOL bResult
= FALSE
;
966 PROFILEINFOW ProfileInfoW
= {0};
969 DPRINT("LoadUserProfileA() called\n");
971 /* Check profile info */
972 if (!lpProfileInfo
|| (lpProfileInfo
->dwSize
!= sizeof(PROFILEINFOA
)) ||
973 (lpProfileInfo
->lpUserName
== NULL
) || (lpProfileInfo
->lpUserName
[0] == 0))
975 SetLastError(ERROR_INVALID_PARAMETER
);
979 /* Convert the structure to UNICODE... */
980 ProfileInfoW
.dwSize
= sizeof(PROFILEINFOW
);
981 ProfileInfoW
.dwFlags
= lpProfileInfo
->dwFlags
;
983 if (lpProfileInfo
->lpUserName
)
985 len
= MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpUserName
, -1, NULL
, 0);
986 ProfileInfoW
.lpUserName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
987 if (!ProfileInfoW
.lpUserName
)
989 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
992 MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpUserName
, -1, ProfileInfoW
.lpUserName
, len
);
995 if (lpProfileInfo
->lpProfilePath
)
997 len
= MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpProfilePath
, -1, NULL
, 0);
998 ProfileInfoW
.lpProfilePath
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
999 if (!ProfileInfoW
.lpProfilePath
)
1001 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1004 MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpProfilePath
, -1, ProfileInfoW
.lpProfilePath
, len
);
1007 if (lpProfileInfo
->lpDefaultPath
)
1009 len
= MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpDefaultPath
, -1, NULL
, 0);
1010 ProfileInfoW
.lpDefaultPath
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1011 if (!ProfileInfoW
.lpDefaultPath
)
1013 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1016 MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpDefaultPath
, -1, ProfileInfoW
.lpDefaultPath
, len
);
1019 if (lpProfileInfo
->lpServerName
)
1021 len
= MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpServerName
, -1, NULL
, 0);
1022 ProfileInfoW
.lpServerName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1023 if (!ProfileInfoW
.lpServerName
)
1025 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1028 MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpServerName
, -1, ProfileInfoW
.lpServerName
, len
);
1031 if ((ProfileInfoW
.dwFlags
& PI_APPLYPOLICY
) != 0 && lpProfileInfo
->lpPolicyPath
)
1033 len
= MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpPolicyPath
, -1, NULL
, 0);
1034 ProfileInfoW
.lpPolicyPath
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1035 if (!ProfileInfoW
.lpPolicyPath
)
1037 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1040 MultiByteToWideChar(CP_ACP
, 0, lpProfileInfo
->lpPolicyPath
, -1, ProfileInfoW
.lpPolicyPath
, len
);
1043 /* ... and call the UNICODE function */
1044 bResult
= LoadUserProfileW(hToken
, &ProfileInfoW
);
1046 /* Save the returned value */
1047 lpProfileInfo
->hProfile
= ProfileInfoW
.hProfile
;
1050 /* Memory cleanup */
1051 if (ProfileInfoW
.lpUserName
)
1052 HeapFree(GetProcessHeap(), 0, ProfileInfoW
.lpUserName
);
1054 if (ProfileInfoW
.lpProfilePath
)
1055 HeapFree(GetProcessHeap(), 0, ProfileInfoW
.lpProfilePath
);
1057 if (ProfileInfoW
.lpDefaultPath
)
1058 HeapFree(GetProcessHeap(), 0, ProfileInfoW
.lpDefaultPath
);
1060 if (ProfileInfoW
.lpServerName
)
1061 HeapFree(GetProcessHeap(), 0, ProfileInfoW
.lpServerName
);
1063 if ((ProfileInfoW
.dwFlags
& PI_APPLYPOLICY
) != 0 && ProfileInfoW
.lpPolicyPath
)
1064 HeapFree(GetProcessHeap(), 0, ProfileInfoW
.lpPolicyPath
);
1072 LoadUserProfileW(IN HANDLE hToken
,
1073 IN OUT LPPROFILEINFOW lpProfileInfo
)
1075 WCHAR szUserHivePath
[MAX_PATH
];
1076 LPWSTR UserName
= NULL
, Domain
= NULL
;
1077 DWORD UserNameLength
= 0, DomainLength
= 0;
1078 PTOKEN_USER UserSid
= NULL
;
1079 SID_NAME_USE AccountType
;
1080 UNICODE_STRING SidString
= { 0, 0, NULL
};
1083 DWORD dwLength
= sizeof(szUserHivePath
) / sizeof(szUserHivePath
[0]);
1085 DPRINT("LoadUserProfileW() called\n");
1087 /* Check profile info */
1088 if (!lpProfileInfo
|| (lpProfileInfo
->dwSize
!= sizeof(PROFILEINFOW
)) ||
1089 (lpProfileInfo
->lpUserName
== NULL
) || (lpProfileInfo
->lpUserName
[0] == 0))
1091 SetLastError(ERROR_INVALID_PARAMETER
);
1095 /* Don't load a profile twice */
1096 if (CheckForLoadedProfile(hToken
))
1098 DPRINT ("Profile already loaded\n");
1099 lpProfileInfo
->hProfile
= NULL
;
1103 if (lpProfileInfo
->lpProfilePath
)
1105 wcscpy(szUserHivePath
, lpProfileInfo
->lpProfilePath
);
1109 /* FIXME: check if MS Windows allows lpProfileInfo->lpProfilePath to be NULL */
1110 if (!GetProfilesDirectoryW(szUserHivePath
, &dwLength
))
1112 DPRINT1("GetProfilesDirectoryW() failed (error %ld)\n", GetLastError());
1117 /* Create user hive name */
1118 wcscat(szUserHivePath
, L
"\\");
1119 wcscat(szUserHivePath
, lpProfileInfo
->lpUserName
);
1120 wcscat(szUserHivePath
, L
"\\ntuser.dat");
1121 DPRINT("szUserHivePath: %S\n", szUserHivePath
);
1123 /* Create user profile directory if needed */
1124 if (GetFileAttributesW(szUserHivePath
) == INVALID_FILE_ATTRIBUTES
)
1127 if (GetTokenInformation(hToken
, TokenUser
, NULL
, 0, &dwLength
) ||
1128 GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
1130 DPRINT1 ("GetTokenInformation() failed\n");
1134 UserSid
= (PTOKEN_USER
)HeapAlloc(GetProcessHeap(), 0, dwLength
);
1137 DPRINT1("HeapAlloc() failed\n");
1138 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1142 if (!GetTokenInformation(hToken
, TokenUser
, UserSid
, dwLength
, &dwLength
))
1144 DPRINT1("GetTokenInformation() failed\n");
1151 if (UserNameLength
> 0)
1153 HeapFree(GetProcessHeap(), 0, UserName
);
1154 UserName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, UserNameLength
* sizeof(WCHAR
));
1157 DPRINT1("HeapAlloc() failed\n");
1158 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1162 if (DomainLength
> 0)
1164 HeapFree(GetProcessHeap(), 0, Domain
);
1165 Domain
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, DomainLength
* sizeof(WCHAR
));
1168 DPRINT1("HeapAlloc() failed\n");
1169 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1173 ret
= LookupAccountSidW(NULL
,
1180 } while (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
);
1184 DPRINT1("LookupAccountSidW() failed\n");
1188 /* Create profile */
1189 /* FIXME: ignore Domain? */
1190 DPRINT("UserName %S, Domain %S\n", UserName
, Domain
);
1191 ret
= CreateUserProfileW(UserSid
->User
.Sid
, UserName
);
1194 DPRINT1("CreateUserProfileW() failed\n");
1199 /* Get user SID string */
1200 ret
= GetUserSidStringFromToken(hToken
, &SidString
);
1203 DPRINT1("GetUserSidFromToken() failed\n");
1208 /* Acquire restore privilege */
1209 if (!AcquireRemoveRestorePrivilege(TRUE
))
1211 DPRINT1("AcquireRemoveRestorePrivilege() failed (Error %ld)\n", GetLastError());
1215 /* Load user registry hive */
1216 Error
= RegLoadKeyW(HKEY_USERS
,
1219 AcquireRemoveRestorePrivilege(FALSE
);
1221 /* HACK: Do not fail if the profile has already been loaded! */
1222 if (Error
== ERROR_SHARING_VIOLATION
)
1223 Error
= ERROR_SUCCESS
;
1225 if (Error
!= ERROR_SUCCESS
)
1227 DPRINT1("RegLoadKeyW() failed (Error %ld)\n", Error
);
1228 SetLastError((DWORD
)Error
);
1232 /* Open future HKEY_CURRENT_USER */
1233 Error
= RegOpenKeyExW(HKEY_USERS
,
1237 (PHKEY
)&lpProfileInfo
->hProfile
);
1238 if (Error
!= ERROR_SUCCESS
)
1240 DPRINT1("RegOpenKeyExW() failed (Error %ld)\n", Error
);
1241 SetLastError((DWORD
)Error
);
1248 HeapFree(GetProcessHeap(), 0, UserSid
);
1249 HeapFree(GetProcessHeap(), 0, UserName
);
1250 HeapFree(GetProcessHeap(), 0, Domain
);
1251 RtlFreeUnicodeString(&SidString
);
1253 DPRINT("LoadUserProfileW() done\n");
1260 UnloadUserProfile(HANDLE hToken
,
1263 UNICODE_STRING SidString
;
1266 DPRINT("UnloadUserProfile() called\n");
1268 if (hProfile
== NULL
)
1270 DPRINT1("Invalid profile handle\n");
1271 SetLastError(ERROR_INVALID_PARAMETER
);
1275 RegCloseKey(hProfile
);
1277 if (!GetUserSidStringFromToken(hToken
,
1280 DPRINT1("GetUserSidFromToken() failed\n");
1284 DPRINT("SidString: '%wZ'\n", &SidString
);
1286 /* Acquire restore privilege */
1287 if (!AcquireRemoveRestorePrivilege(TRUE
))
1289 DPRINT1("AcquireRemoveRestorePrivilege() failed (Error %ld)\n", GetLastError());
1290 RtlFreeUnicodeString(&SidString
);
1294 /* Unload the hive */
1295 Error
= RegUnLoadKeyW(HKEY_USERS
,
1298 /* Remove restore privilege */
1299 AcquireRemoveRestorePrivilege(FALSE
);
1301 if (Error
!= ERROR_SUCCESS
)
1303 DPRINT1("RegUnLoadKeyW() failed (Error %ld)\n", Error
);
1304 RtlFreeUnicodeString(&SidString
);
1305 SetLastError((DWORD
)Error
);
1309 RtlFreeUnicodeString(&SidString
);
1311 DPRINT("UnloadUserProfile() done\n");
1319 DeleteProfileW(LPCWSTR lpSidString
,
1320 LPCWSTR lpProfilePath
,
1321 LPCWSTR lpComputerName
)
1323 DPRINT1("DeleteProfileW() not implemented!\n");
1330 DeleteProfileA(LPCSTR lpSidString
,
1331 LPCSTR lpProfilePath
,
1332 LPCSTR lpComputerName
)
1335 UNICODE_STRING SidString
, ProfilePath
, ComputerName
;
1337 DPRINT("DeleteProfileA() called\n");
1339 /* Conversion to UNICODE */
1341 RtlCreateUnicodeStringFromAsciiz(&SidString
,
1342 (LPSTR
)lpSidString
);
1345 RtlCreateUnicodeStringFromAsciiz(&ProfilePath
,
1346 (LPSTR
)lpProfilePath
);
1349 RtlCreateUnicodeStringFromAsciiz(&ComputerName
,
1350 (LPSTR
)lpComputerName
);
1352 /* Call the UNICODE function */
1353 bResult
= DeleteProfileW(SidString
.Buffer
,
1355 ComputerName
.Buffer
);
1357 /* Memory cleanup */
1359 RtlFreeUnicodeString(&SidString
);
1362 RtlFreeUnicodeString(&ProfilePath
);
1365 RtlFreeUnicodeString(&ComputerName
);
1373 GetProfileType(OUT PDWORD pdwFlags
)
1375 DPRINT1("GetProfileType() not implemented!\n");