1 /* $Id: profile.c,v 1.5 2004/02/28 11:30:59 ekohl Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/userenv/profile.c
6 * PURPOSE: User profile code
7 * PROGRAMMER: Eric Kohl
19 /* FUNCTIONS ***************************************************************/
22 AppendSystemPostfix (LPWSTR lpName
,
25 WCHAR szSystemRoot
[MAX_PATH
];
26 WCHAR szDrivePostfix
[3];
29 DWORD dwPostfixLength
;
31 /* Build profile name postfix */
32 if (!ExpandEnvironmentStringsW (L
"%SystemRoot%",
36 DPRINT1("Error: %lu\n", GetLastError());
40 _wcsupr (szSystemRoot
);
42 /* Get name postfix */
43 szSystemRoot
[2] = L
'.';
44 lpszPostfix
= &szSystemRoot
[2];
45 lpszPtr
= lpszPostfix
;
46 while (*lpszPtr
!= (WCHAR
)0)
48 if (*lpszPtr
== L
'\\')
53 dwPostfixLength
= wcslen (lpszPostfix
);
54 if (szSystemRoot
[0] != L
'C')
57 szDrivePostfix
[0] = L
'_';
58 szDrivePostfix
[1] = szSystemRoot
[0];
59 szDrivePostfix
[2] = (WCHAR
)0;
62 if (wcslen (lpName
) + dwPostfixLength
>= dwMaxLength
)
64 DPRINT1("Error: buffer overflow\n");
68 wcscat (lpName
, lpszPostfix
);
69 if (szSystemRoot
[0] != L
'C')
71 wcscat (lpName
, szDrivePostfix
);
79 CreateUserProfileW (PSID Sid
,
82 WCHAR szRawProfilesPath
[MAX_PATH
];
83 WCHAR szProfilesPath
[MAX_PATH
];
84 WCHAR szUserProfilePath
[MAX_PATH
];
85 WCHAR szDefaultUserPath
[MAX_PATH
];
86 WCHAR szBuffer
[MAX_PATH
];
87 UNICODE_STRING SidString
;
93 DPRINT ("CreateUserProfileW() called\n");
95 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
96 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
101 DPRINT1("Error: %lu\n", GetLastError());
105 /* Get profiles path */
106 dwLength
= MAX_PATH
* sizeof(WCHAR
);
107 if (RegQueryValueExW (hKey
,
108 L
"ProfilesDirectory",
111 (LPBYTE
)szRawProfilesPath
,
114 DPRINT1("Error: %lu\n", GetLastError());
120 if (!ExpandEnvironmentStringsW (szRawProfilesPath
,
124 DPRINT1("Error: %lu\n", GetLastError());
129 /* Get default user path */
130 dwLength
= MAX_PATH
* sizeof(WCHAR
);
131 if (RegQueryValueExW (hKey
,
132 L
"DefaultUserProfile",
138 DPRINT1("Error: %lu\n", GetLastError());
145 wcscpy (szUserProfilePath
, szProfilesPath
);
146 wcscat (szUserProfilePath
, L
"\\");
147 wcscat (szUserProfilePath
, lpUserName
);
148 if (!AppendSystemPostfix (szUserProfilePath
, MAX_PATH
))
150 DPRINT1("AppendSystemPostfix() failed\n", GetLastError());
151 RtlFreeUnicodeString (&SidString
);
156 wcscpy (szDefaultUserPath
, szProfilesPath
);
157 wcscat (szDefaultUserPath
, L
"\\");
158 wcscat (szDefaultUserPath
, szBuffer
);
160 /* Create user profile directory */
161 if (!CreateDirectoryW (szUserProfilePath
, NULL
))
163 if (GetLastError () != ERROR_ALREADY_EXISTS
)
165 DPRINT1("Error: %lu\n", GetLastError());
170 /* Copy default user directory */
171 if (!CopyDirectory (szUserProfilePath
, szDefaultUserPath
))
173 DPRINT1("Error: %lu\n", GetLastError());
177 /* Add profile to profile list */
178 Status
= RtlConvertSidToUnicodeString (&SidString
, Sid
, TRUE
);
179 if (!NT_SUCCESS(Status
))
181 DPRINT1("Status: %lx\n", Status
);
186 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
187 wcscat (szBuffer
, SidString
.Buffer
);
189 /* Create user profile key */
190 if (RegCreateKeyExW (HKEY_LOCAL_MACHINE
,
194 REG_OPTION_NON_VOLATILE
,
200 DPRINT1("Error: %lu\n", GetLastError());
201 RtlFreeUnicodeString (&SidString
);
205 /* Create non-expanded user profile path */
206 wcscpy (szBuffer
, szRawProfilesPath
);
207 wcscat (szBuffer
, L
"\\");
208 wcscat (szBuffer
, lpUserName
);
209 if (!AppendSystemPostfix (szBuffer
, MAX_PATH
))
211 DPRINT1("AppendSystemPostfix() failed\n", GetLastError());
212 RtlFreeUnicodeString (&SidString
);
217 /* Set 'ProfileImagePath' value (non-expanded) */
218 if (RegSetValueExW (hKey
,
223 (wcslen (szBuffer
) + 1) * sizeof(WCHAR
)))
225 DPRINT1("Error: %lu\n", GetLastError());
226 RtlFreeUnicodeString (&SidString
);
231 /* Set 'Sid' value */
232 if (RegSetValueExW (hKey
,
239 DPRINT1("Error: %lu\n", GetLastError());
240 RtlFreeUnicodeString (&SidString
);
247 /* Create user hive name */
248 wcscat (szUserProfilePath
, L
"\\ntuser.dat");
250 /* Create new user hive */
251 if (RegLoadKeyW (HKEY_USERS
,
255 DPRINT1("Error: %lu\n", GetLastError());
256 RtlFreeUnicodeString (&SidString
);
260 /* Initialize user hive */
261 if (!CreateUserHive (SidString
.Buffer
))
263 DPRINT1("Error: %lu\n", GetLastError());
264 RtlFreeUnicodeString (&SidString
);
268 RegUnLoadKeyW (HKEY_USERS
,
271 RtlFreeUnicodeString (&SidString
);
273 DPRINT ("CreateUserProfileW() done\n");
280 GetAllUsersProfileDirectoryW (LPWSTR lpProfileDir
,
283 WCHAR szProfilePath
[MAX_PATH
];
284 WCHAR szBuffer
[MAX_PATH
];
288 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
289 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
294 DPRINT1("Error: %lu\n", GetLastError());
298 /* Get profiles path */
299 dwLength
= MAX_PATH
* sizeof(WCHAR
);
300 if (RegQueryValueExW (hKey
,
301 L
"ProfilesDirectory",
307 DPRINT1("Error: %lu\n", GetLastError());
313 if (!ExpandEnvironmentStringsW (szBuffer
,
317 DPRINT1("Error: %lu\n", GetLastError());
322 /* Get 'AllUsersProfile' name */
323 dwLength
= MAX_PATH
* sizeof(WCHAR
);
324 if (RegQueryValueExW (hKey
,
331 DPRINT1("Error: %lu\n", GetLastError());
338 wcscat (szProfilePath
, L
"\\");
339 wcscat (szProfilePath
, szBuffer
);
341 dwLength
= wcslen (szProfilePath
);
342 if (lpProfileDir
!= NULL
)
344 if (*lpcchSize
< dwLength
)
346 *lpcchSize
= dwLength
;
347 SetLastError (ERROR_INSUFFICIENT_BUFFER
);
351 wcscpy (lpProfileDir
, szProfilePath
);
354 *lpcchSize
= dwLength
;
361 GetDefaultUserProfileDirectoryW (LPWSTR lpProfileDir
,
364 WCHAR szProfilePath
[MAX_PATH
];
365 WCHAR szBuffer
[MAX_PATH
];
369 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
370 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
375 DPRINT1("Error: %lu\n", GetLastError());
379 /* Get profiles path */
380 dwLength
= MAX_PATH
* sizeof(WCHAR
);
381 if (RegQueryValueExW (hKey
,
382 L
"ProfilesDirectory",
388 DPRINT1("Error: %lu\n", GetLastError());
394 if (!ExpandEnvironmentStringsW (szBuffer
,
398 DPRINT1("Error: %lu\n", GetLastError());
403 /* Get 'DefaultUserProfile' name */
404 dwLength
= MAX_PATH
* sizeof(WCHAR
);
405 if (RegQueryValueExW (hKey
,
406 L
"DefaultUserProfile",
412 DPRINT1("Error: %lu\n", GetLastError());
419 wcscat (szProfilePath
, L
"\\");
420 wcscat (szProfilePath
, szBuffer
);
422 dwLength
= wcslen (szProfilePath
);
423 if (lpProfileDir
!= NULL
)
425 if (*lpcchSize
< dwLength
)
427 *lpcchSize
= dwLength
;
428 SetLastError (ERROR_INSUFFICIENT_BUFFER
);
432 wcscpy (lpProfileDir
, szProfilePath
);
435 *lpcchSize
= dwLength
;
442 GetProfilesDirectoryW (LPWSTR lpProfilesDir
,
445 WCHAR szProfilesPath
[MAX_PATH
];
446 WCHAR szBuffer
[MAX_PATH
];
450 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
451 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
456 DPRINT1("Error: %lu\n", GetLastError());
460 /* Get profiles path */
461 dwLength
= MAX_PATH
* sizeof(WCHAR
);
462 if (RegQueryValueExW (hKey
,
463 L
"ProfilesDirectory",
469 DPRINT1("Error: %lu\n", GetLastError());
477 if (!ExpandEnvironmentStringsW (szBuffer
,
481 DPRINT1("Error: %lu\n", GetLastError());
485 dwLength
= wcslen (szProfilesPath
);
486 if (lpProfilesDir
!= NULL
)
488 if (*lpcchSize
< dwLength
)
490 *lpcchSize
= dwLength
;
491 SetLastError (ERROR_INSUFFICIENT_BUFFER
);
495 wcscpy (lpProfilesDir
, szProfilesPath
);
498 *lpcchSize
= dwLength
;
505 GetUserProfileDirectoryW (HANDLE hToken
,
510 return GetDefaultUserProfileDirectoryW (lpProfileDir
, lpcchSize
);
515 LoadUserProfileW (HANDLE hToken
,
516 LPPROFILEINFOW lpProfileInfo
)
519 /* Check profile info */
520 if (lpProfileInfo
->dwSize
!= sizeof(PROFILEINFOW
) ||
521 lpProfileInfo
->lpUserName
== NULL
||
522 lpProfileInfo
->lpUserName
[0] == 0)
524 SetLastError (ERROR_INVALID_PARAMETER
);
528 /* FIXME: load the profile */