Remove outdated profile path postfix.
[reactos.git] / reactos / lib / userenv / profile.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2004 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id: profile.c,v 1.15 2004/10/05 13:39:42 ekohl Exp $
20 *
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS system libraries
23 * FILE: lib/userenv/profile.c
24 * PURPOSE: User profile code
25 * PROGRAMMER: Eric Kohl
26 */
27
28 #include "precomp.h"
29
30
31 /* FUNCTIONS ***************************************************************/
32
33 BOOL
34 AppendSystemPostfix (LPWSTR lpName,
35 DWORD dwMaxLength)
36 {
37 WCHAR szSystemRoot[MAX_PATH];
38 LPWSTR lpszPostfix;
39 LPWSTR lpszPtr;
40
41 /* Build profile name postfix */
42 if (!ExpandEnvironmentStringsW (L"%SystemRoot%",
43 szSystemRoot,
44 MAX_PATH))
45 {
46 DPRINT1("Error: %lu\n", GetLastError());
47 return FALSE;
48 }
49
50 _wcsupr (szSystemRoot);
51
52 /* Get name postfix */
53 szSystemRoot[2] = L'.';
54 lpszPostfix = &szSystemRoot[2];
55 lpszPtr = lpszPostfix;
56 while (*lpszPtr != (WCHAR)0)
57 {
58 if (*lpszPtr == L'\\')
59 *lpszPtr = '_';
60 lpszPtr++;
61 }
62
63 if (wcslen(lpName) + wcslen(lpszPostfix) >= dwMaxLength)
64 {
65 DPRINT1("Error: buffer overflow\n");
66 return FALSE;
67 }
68
69 wcscat(lpName, lpszPostfix);
70
71 return TRUE;
72 }
73
74
75 BOOL WINAPI
76 CreateUserProfileA (PSID Sid,
77 LPCSTR lpUserName)
78 {
79 UNICODE_STRING UserName;
80 BOOL bResult;
81 NTSTATUS Status;
82
83 Status = RtlCreateUnicodeStringFromAsciiz (&UserName,
84 (LPSTR)lpUserName);
85 if (!NT_SUCCESS(Status))
86 {
87 SetLastError (RtlNtStatusToDosError (Status));
88 return FALSE;
89 }
90
91 bResult = CreateUserProfileW (Sid,
92 UserName.Buffer);
93
94 RtlFreeUnicodeString (&UserName);
95
96 return bResult;
97 }
98
99
100 BOOL WINAPI
101 CreateUserProfileW (PSID Sid,
102 LPCWSTR lpUserName)
103 {
104 WCHAR szRawProfilesPath[MAX_PATH];
105 WCHAR szProfilesPath[MAX_PATH];
106 WCHAR szUserProfilePath[MAX_PATH];
107 WCHAR szDefaultUserPath[MAX_PATH];
108 WCHAR szBuffer[MAX_PATH];
109 UNICODE_STRING SidString;
110 DWORD dwLength;
111 DWORD dwDisposition;
112 HKEY hKey;
113 NTSTATUS Status;
114
115 DPRINT("CreateUserProfileW() called\n");
116
117 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
118 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
119 0,
120 KEY_ALL_ACCESS,
121 &hKey))
122 {
123 DPRINT1("Error: %lu\n", GetLastError());
124 return FALSE;
125 }
126
127 /* Get profiles path */
128 dwLength = MAX_PATH * sizeof(WCHAR);
129 if (RegQueryValueExW (hKey,
130 L"ProfilesDirectory",
131 NULL,
132 NULL,
133 (LPBYTE)szRawProfilesPath,
134 &dwLength))
135 {
136 DPRINT1("Error: %lu\n", GetLastError());
137 RegCloseKey (hKey);
138 return FALSE;
139 }
140
141 /* Expand it */
142 if (!ExpandEnvironmentStringsW (szRawProfilesPath,
143 szProfilesPath,
144 MAX_PATH))
145 {
146 DPRINT1("Error: %lu\n", GetLastError());
147 RegCloseKey (hKey);
148 return FALSE;
149 }
150
151 /* Get default user path */
152 dwLength = MAX_PATH * sizeof(WCHAR);
153 if (RegQueryValueExW (hKey,
154 L"DefaultUserProfile",
155 NULL,
156 NULL,
157 (LPBYTE)szBuffer,
158 &dwLength))
159 {
160 DPRINT1("Error: %lu\n", GetLastError());
161 RegCloseKey (hKey);
162 return FALSE;
163 }
164
165 RegCloseKey (hKey);
166
167 wcscpy (szUserProfilePath, szProfilesPath);
168 wcscat (szUserProfilePath, L"\\");
169 wcscat (szUserProfilePath, lpUserName);
170 if (!AppendSystemPostfix (szUserProfilePath, MAX_PATH))
171 {
172 DPRINT1("AppendSystemPostfix() failed\n", GetLastError());
173 RtlFreeUnicodeString (&SidString);
174 RegCloseKey (hKey);
175 return FALSE;
176 }
177
178 wcscpy (szDefaultUserPath, szProfilesPath);
179 wcscat (szDefaultUserPath, L"\\");
180 wcscat (szDefaultUserPath, szBuffer);
181
182 /* Create user profile directory */
183 if (!CreateDirectoryW (szUserProfilePath, NULL))
184 {
185 if (GetLastError () != ERROR_ALREADY_EXISTS)
186 {
187 DPRINT1("Error: %lu\n", GetLastError());
188 return FALSE;
189 }
190 }
191
192 /* Copy default user directory */
193 if (!CopyDirectory (szUserProfilePath, szDefaultUserPath))
194 {
195 DPRINT1("Error: %lu\n", GetLastError());
196 return FALSE;
197 }
198
199 /* Add profile to profile list */
200 Status = RtlConvertSidToUnicodeString (&SidString, Sid, TRUE);
201 if (!NT_SUCCESS(Status))
202 {
203 DPRINT1("Status: %lx\n", Status);
204 return FALSE;
205 }
206
207 wcscpy (szBuffer,
208 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
209 wcscat (szBuffer, SidString.Buffer);
210
211 /* Create user profile key */
212 if (RegCreateKeyExW (HKEY_LOCAL_MACHINE,
213 szBuffer,
214 0,
215 NULL,
216 REG_OPTION_NON_VOLATILE,
217 KEY_ALL_ACCESS,
218 NULL,
219 &hKey,
220 &dwDisposition))
221 {
222 DPRINT1("Error: %lu\n", GetLastError());
223 RtlFreeUnicodeString (&SidString);
224 return FALSE;
225 }
226
227 /* Create non-expanded user profile path */
228 wcscpy (szBuffer, szRawProfilesPath);
229 wcscat (szBuffer, L"\\");
230 wcscat (szBuffer, lpUserName);
231 if (!AppendSystemPostfix (szBuffer, MAX_PATH))
232 {
233 DPRINT1("AppendSystemPostfix() failed\n", GetLastError());
234 RtlFreeUnicodeString (&SidString);
235 RegCloseKey (hKey);
236 return FALSE;
237 }
238
239 /* Set 'ProfileImagePath' value (non-expanded) */
240 if (RegSetValueExW (hKey,
241 L"ProfileImagePath",
242 0,
243 REG_EXPAND_SZ,
244 (LPBYTE)szBuffer,
245 (wcslen (szBuffer) + 1) * sizeof(WCHAR)))
246 {
247 DPRINT1("Error: %lu\n", GetLastError());
248 RtlFreeUnicodeString (&SidString);
249 RegCloseKey (hKey);
250 return FALSE;
251 }
252
253 /* Set 'Sid' value */
254 if (RegSetValueExW (hKey,
255 L"Sid",
256 0,
257 REG_BINARY,
258 Sid,
259 RtlLengthSid (Sid)))
260 {
261 DPRINT1("Error: %lu\n", GetLastError());
262 RtlFreeUnicodeString (&SidString);
263 RegCloseKey (hKey);
264 return FALSE;
265 }
266
267 RegCloseKey (hKey);
268
269 /* Create user hive name */
270 wcscpy (szBuffer, szUserProfilePath);
271 wcscat (szBuffer, L"\\ntuser.dat");
272
273 /* Create new user hive */
274 if (RegLoadKeyW (HKEY_USERS,
275 SidString.Buffer,
276 szBuffer))
277 {
278 DPRINT1("Error: %lu\n", GetLastError());
279 RtlFreeUnicodeString (&SidString);
280 return FALSE;
281 }
282
283 /* Initialize user hive */
284 if (!CreateUserHive (SidString.Buffer, szUserProfilePath))
285 {
286 DPRINT1("Error: %lu\n", GetLastError());
287 RtlFreeUnicodeString (&SidString);
288 return FALSE;
289 }
290
291 RegUnLoadKeyW (HKEY_USERS,
292 SidString.Buffer);
293
294 RtlFreeUnicodeString (&SidString);
295
296 DPRINT("CreateUserProfileW() done\n");
297
298 return TRUE;
299 }
300
301
302 BOOL WINAPI
303 GetAllUsersProfileDirectoryA (LPSTR lpProfileDir,
304 LPDWORD lpcchSize)
305 {
306 LPWSTR lpBuffer;
307 BOOL bResult;
308
309 lpBuffer = GlobalAlloc (GMEM_FIXED,
310 *lpcchSize * sizeof(WCHAR));
311 if (lpBuffer == NULL)
312 return FALSE;
313
314 bResult = GetAllUsersProfileDirectoryW (lpBuffer,
315 lpcchSize);
316 if (bResult)
317 {
318 WideCharToMultiByte (CP_ACP,
319 0,
320 lpBuffer,
321 -1,
322 lpProfileDir,
323 *lpcchSize,
324 NULL,
325 NULL);
326 }
327
328 GlobalFree (lpBuffer);
329
330 return bResult;
331 }
332
333
334 BOOL WINAPI
335 GetAllUsersProfileDirectoryW (LPWSTR lpProfileDir,
336 LPDWORD lpcchSize)
337 {
338 WCHAR szProfilePath[MAX_PATH];
339 WCHAR szBuffer[MAX_PATH];
340 DWORD dwLength;
341 HKEY hKey;
342
343 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
344 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
345 0,
346 KEY_READ,
347 &hKey))
348 {
349 DPRINT1("Error: %lu\n", GetLastError());
350 return FALSE;
351 }
352
353 /* Get profiles path */
354 dwLength = MAX_PATH * sizeof(WCHAR);
355 if (RegQueryValueExW (hKey,
356 L"ProfilesDirectory",
357 NULL,
358 NULL,
359 (LPBYTE)szBuffer,
360 &dwLength))
361 {
362 DPRINT1("Error: %lu\n", GetLastError());
363 RegCloseKey (hKey);
364 return FALSE;
365 }
366
367 /* Expand it */
368 if (!ExpandEnvironmentStringsW (szBuffer,
369 szProfilePath,
370 MAX_PATH))
371 {
372 DPRINT1("Error: %lu\n", GetLastError());
373 RegCloseKey (hKey);
374 return FALSE;
375 }
376
377 /* Get 'AllUsersProfile' name */
378 dwLength = MAX_PATH * sizeof(WCHAR);
379 if (RegQueryValueExW (hKey,
380 L"AllUsersProfile",
381 NULL,
382 NULL,
383 (LPBYTE)szBuffer,
384 &dwLength))
385 {
386 DPRINT1("Error: %lu\n", GetLastError());
387 RegCloseKey (hKey);
388 return FALSE;
389 }
390
391 RegCloseKey (hKey);
392
393 wcscat (szProfilePath, L"\\");
394 wcscat (szProfilePath, szBuffer);
395
396 dwLength = wcslen (szProfilePath);
397 if (lpProfileDir != NULL)
398 {
399 if (*lpcchSize < dwLength)
400 {
401 *lpcchSize = dwLength;
402 SetLastError (ERROR_INSUFFICIENT_BUFFER);
403 return FALSE;
404 }
405
406 wcscpy (lpProfileDir, szProfilePath);
407 }
408
409 *lpcchSize = dwLength;
410
411 return TRUE;
412 }
413
414
415 BOOL WINAPI
416 GetDefaultUserProfileDirectoryA (LPSTR lpProfileDir,
417 LPDWORD lpcchSize)
418 {
419 LPWSTR lpBuffer;
420 BOOL bResult;
421
422 lpBuffer = GlobalAlloc (GMEM_FIXED,
423 *lpcchSize * sizeof(WCHAR));
424 if (lpBuffer == NULL)
425 return FALSE;
426
427 bResult = GetDefaultUserProfileDirectoryW (lpBuffer,
428 lpcchSize);
429 if (bResult)
430 {
431 WideCharToMultiByte (CP_ACP,
432 0,
433 lpBuffer,
434 -1,
435 lpProfileDir,
436 *lpcchSize,
437 NULL,
438 NULL);
439 }
440
441 GlobalFree (lpBuffer);
442
443 return bResult;
444 }
445
446
447 BOOL WINAPI
448 GetDefaultUserProfileDirectoryW (LPWSTR lpProfileDir,
449 LPDWORD lpcchSize)
450 {
451 WCHAR szProfilePath[MAX_PATH];
452 WCHAR szBuffer[MAX_PATH];
453 DWORD dwLength;
454 HKEY hKey;
455
456 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
457 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
458 0,
459 KEY_READ,
460 &hKey))
461 {
462 DPRINT1("Error: %lu\n", GetLastError());
463 return FALSE;
464 }
465
466 /* Get profiles path */
467 dwLength = MAX_PATH * sizeof(WCHAR);
468 if (RegQueryValueExW (hKey,
469 L"ProfilesDirectory",
470 NULL,
471 NULL,
472 (LPBYTE)szBuffer,
473 &dwLength))
474 {
475 DPRINT1("Error: %lu\n", GetLastError());
476 RegCloseKey (hKey);
477 return FALSE;
478 }
479
480 /* Expand it */
481 if (!ExpandEnvironmentStringsW (szBuffer,
482 szProfilePath,
483 MAX_PATH))
484 {
485 DPRINT1("Error: %lu\n", GetLastError());
486 RegCloseKey (hKey);
487 return FALSE;
488 }
489
490 /* Get 'DefaultUserProfile' name */
491 dwLength = MAX_PATH * sizeof(WCHAR);
492 if (RegQueryValueExW (hKey,
493 L"DefaultUserProfile",
494 NULL,
495 NULL,
496 (LPBYTE)szBuffer,
497 &dwLength))
498 {
499 DPRINT1("Error: %lu\n", GetLastError());
500 RegCloseKey (hKey);
501 return FALSE;
502 }
503
504 RegCloseKey (hKey);
505
506 wcscat (szProfilePath, L"\\");
507 wcscat (szProfilePath, szBuffer);
508
509 dwLength = wcslen (szProfilePath);
510 if (lpProfileDir != NULL)
511 {
512 if (*lpcchSize < dwLength)
513 {
514 *lpcchSize = dwLength;
515 SetLastError (ERROR_INSUFFICIENT_BUFFER);
516 return FALSE;
517 }
518
519 wcscpy (lpProfileDir, szProfilePath);
520 }
521
522 *lpcchSize = dwLength;
523
524 return TRUE;
525 }
526
527
528 BOOL WINAPI
529 GetProfilesDirectoryA (LPSTR lpProfileDir,
530 LPDWORD lpcchSize)
531 {
532 LPWSTR lpBuffer;
533 BOOL bResult;
534
535 lpBuffer = GlobalAlloc (GMEM_FIXED,
536 *lpcchSize * sizeof(WCHAR));
537 if (lpBuffer == NULL)
538 return FALSE;
539
540 bResult = GetProfilesDirectoryW (lpBuffer,
541 lpcchSize);
542 if (bResult)
543 {
544 WideCharToMultiByte (CP_ACP,
545 0,
546 lpBuffer,
547 -1,
548 lpProfileDir,
549 *lpcchSize,
550 NULL,
551 NULL);
552 }
553
554 GlobalFree (lpBuffer);
555
556 return bResult;
557 }
558
559
560 BOOL WINAPI
561 GetProfilesDirectoryW (LPWSTR lpProfilesDir,
562 LPDWORD lpcchSize)
563 {
564 WCHAR szProfilesPath[MAX_PATH];
565 WCHAR szBuffer[MAX_PATH];
566 DWORD dwLength;
567 HKEY hKey;
568
569 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
570 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
571 0,
572 KEY_READ,
573 &hKey))
574 {
575 DPRINT1("Error: %lu\n", GetLastError());
576 return FALSE;
577 }
578
579 /* Get profiles path */
580 dwLength = MAX_PATH * sizeof(WCHAR);
581 if (RegQueryValueExW (hKey,
582 L"ProfilesDirectory",
583 NULL,
584 NULL,
585 (LPBYTE)szBuffer,
586 &dwLength))
587 {
588 DPRINT1("Error: %lu\n", GetLastError());
589 RegCloseKey (hKey);
590 return FALSE;
591 }
592
593 RegCloseKey (hKey);
594
595 /* Expand it */
596 if (!ExpandEnvironmentStringsW (szBuffer,
597 szProfilesPath,
598 MAX_PATH))
599 {
600 DPRINT1("Error: %lu\n", GetLastError());
601 return FALSE;
602 }
603
604 dwLength = wcslen (szProfilesPath);
605 if (lpProfilesDir != NULL)
606 {
607 if (*lpcchSize < dwLength)
608 {
609 *lpcchSize = dwLength;
610 SetLastError (ERROR_INSUFFICIENT_BUFFER);
611 return FALSE;
612 }
613
614 wcscpy (lpProfilesDir, szProfilesPath);
615 }
616
617 *lpcchSize = dwLength;
618
619 return TRUE;
620 }
621
622
623 BOOL WINAPI
624 GetUserProfileDirectoryA (HANDLE hToken,
625 LPSTR lpProfileDir,
626 LPDWORD lpcchSize)
627 {
628 LPWSTR lpBuffer;
629 BOOL bResult;
630
631 lpBuffer = GlobalAlloc (GMEM_FIXED,
632 *lpcchSize * sizeof(WCHAR));
633 if (lpBuffer == NULL)
634 return FALSE;
635
636 bResult = GetUserProfileDirectoryW (hToken,
637 lpBuffer,
638 lpcchSize);
639 if (bResult)
640 {
641 WideCharToMultiByte (CP_ACP,
642 0,
643 lpBuffer,
644 -1,
645 lpProfileDir,
646 *lpcchSize,
647 NULL,
648 NULL);
649 }
650
651 GlobalFree (lpBuffer);
652
653 return bResult;
654 }
655
656
657 BOOL WINAPI
658 GetUserProfileDirectoryW (HANDLE hToken,
659 LPWSTR lpProfileDir,
660 LPDWORD lpcchSize)
661 {
662 UNICODE_STRING SidString;
663 WCHAR szKeyName[MAX_PATH];
664 WCHAR szRawImagePath[MAX_PATH];
665 WCHAR szImagePath[MAX_PATH];
666 DWORD dwLength;
667 HKEY hKey;
668
669 if (!GetUserSidFromToken (hToken,
670 &SidString))
671 {
672 DPRINT1 ("GetUserSidFromToken() failed\n");
673 return FALSE;
674 }
675
676 DPRINT ("SidString: '%wZ'\n", &SidString);
677
678 wcscpy (szKeyName,
679 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
680 wcscat (szKeyName,
681 SidString.Buffer);
682
683 RtlFreeUnicodeString (&SidString);
684
685 DPRINT ("KeyName: '%S'\n", szKeyName);
686
687 if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
688 szKeyName,
689 0,
690 KEY_ALL_ACCESS,
691 &hKey))
692 {
693 DPRINT1 ("Error: %lu\n", GetLastError());
694 return FALSE;
695 }
696
697 dwLength = MAX_PATH * sizeof(WCHAR);
698 if (RegQueryValueExW (hKey,
699 L"ProfileImagePath",
700 NULL,
701 NULL,
702 (LPBYTE)szRawImagePath,
703 &dwLength))
704 {
705 DPRINT1 ("Error: %lu\n", GetLastError());
706 RegCloseKey (hKey);
707 return FALSE;
708 }
709
710 RegCloseKey (hKey);
711
712 DPRINT ("RawImagePath: '%S'\n", szRawImagePath);
713
714 /* Expand it */
715 if (!ExpandEnvironmentStringsW (szRawImagePath,
716 szImagePath,
717 MAX_PATH))
718 {
719 DPRINT1 ("Error: %lu\n", GetLastError());
720 return FALSE;
721 }
722
723 DPRINT ("ImagePath: '%S'\n", szImagePath);
724
725 dwLength = wcslen (szImagePath);
726 if (dwLength > *lpcchSize)
727 {
728 DPRINT1 ("Buffer too small\n");
729 SetLastError (ERROR_INSUFFICIENT_BUFFER);
730 return FALSE;
731 }
732
733 *lpcchSize = dwLength;
734 wcscpy (lpProfileDir,
735 szImagePath);
736
737 return TRUE;
738 }
739
740
741 static BOOL
742 CheckForLoadedProfile (HANDLE hToken)
743 {
744 UNICODE_STRING SidString;
745 HKEY hKey;
746
747 DPRINT ("CheckForLoadedProfile() called \n");
748
749 if (!GetUserSidFromToken (hToken,
750 &SidString))
751 {
752 DPRINT1 ("GetUserSidFromToken() failed\n");
753 return FALSE;
754 }
755
756 if (RegOpenKeyExW (HKEY_USERS,
757 SidString.Buffer,
758 0,
759 KEY_ALL_ACCESS,
760 &hKey))
761 {
762 DPRINT ("Profile not loaded\n");
763 RtlFreeUnicodeString (&SidString);
764 return FALSE;
765 }
766
767 RegCloseKey (hKey);
768
769 RtlFreeUnicodeString (&SidString);
770
771 DPRINT ("Profile already loaded\n");
772
773 return TRUE;
774 }
775
776
777 BOOL WINAPI
778 LoadUserProfileA (HANDLE hToken,
779 LPPROFILEINFOA lpProfileInfo)
780 {
781 DPRINT ("LoadUserProfileA() not implemented\n");
782 return FALSE;
783 }
784
785
786 BOOL WINAPI
787 LoadUserProfileW (HANDLE hToken,
788 LPPROFILEINFOW lpProfileInfo)
789 {
790 WCHAR szUserHivePath[MAX_PATH];
791 UNICODE_STRING SidString;
792 DWORD dwLength;
793
794 DPRINT ("LoadUserProfileW() called\n");
795
796 /* Check profile info */
797 if (lpProfileInfo->dwSize != sizeof(PROFILEINFOW) ||
798 lpProfileInfo->lpUserName == NULL ||
799 lpProfileInfo->lpUserName[0] == 0)
800 {
801 SetLastError (ERROR_INVALID_PARAMETER);
802 return FALSE;
803 }
804
805 /* Don't load a profile twice */
806 if (CheckForLoadedProfile (hToken))
807 {
808 DPRINT ("Profile already loaded\n");
809 lpProfileInfo->hProfile = NULL;
810 return TRUE;
811 }
812
813 if (!GetProfilesDirectoryW (szUserHivePath,
814 &dwLength))
815 {
816 DPRINT1("GetProfilesDirectoryW() failed\n", GetLastError());
817 return FALSE;
818 }
819
820 wcscat (szUserHivePath, L"\\");
821 wcscat (szUserHivePath, lpProfileInfo->lpUserName);
822 if (!AppendSystemPostfix (szUserHivePath, MAX_PATH))
823 {
824 DPRINT1("AppendSystemPostfix() failed\n", GetLastError());
825 return FALSE;
826 }
827
828 /* Create user hive name */
829 wcscat (szUserHivePath, L"\\ntuser.dat");
830
831 DPRINT ("szUserHivePath: %S\n", szUserHivePath);
832
833 if (!GetUserSidFromToken (hToken,
834 &SidString))
835 {
836 DPRINT1 ("GetUserSidFromToken() failed\n");
837 return FALSE;
838 }
839
840 DPRINT ("SidString: '%wZ'\n", &SidString);
841
842 if (RegLoadKeyW (HKEY_USERS,
843 SidString.Buffer,
844 szUserHivePath))
845 {
846 DPRINT1 ("RegLoadKeyW() failed (Error %ld)\n", GetLastError());
847 RtlFreeUnicodeString (&SidString);
848 return FALSE;
849 }
850
851 if (RegOpenKeyExW (HKEY_USERS,
852 SidString.Buffer,
853 0,
854 KEY_ALL_ACCESS,
855 (PHKEY)&lpProfileInfo->hProfile))
856 {
857 DPRINT1 ("RegOpenKeyExW() failed (Error %ld)\n", GetLastError());
858 RtlFreeUnicodeString (&SidString);
859 return FALSE;
860 }
861
862 RtlFreeUnicodeString (&SidString);
863
864 DPRINT ("LoadUserProfileW() done\n");
865
866 return TRUE;
867 }
868
869
870 BOOL WINAPI
871 UnloadUserProfile (HANDLE hToken,
872 HANDLE hProfile)
873 {
874 UNICODE_STRING SidString;
875
876 DPRINT ("UnloadUserProfile() called\n");
877
878 if (hProfile == NULL)
879 {
880 DPRINT1 ("Invalide profile handle\n");
881 SetLastError (ERROR_INVALID_PARAMETER);
882 return FALSE;
883 }
884
885 RegCloseKey (hProfile);
886
887 if (!GetUserSidFromToken (hToken,
888 &SidString))
889 {
890 DPRINT1 ("GetUserSidFromToken() failed\n");
891 return FALSE;
892 }
893
894 DPRINT ("SidString: '%wZ'\n", &SidString);
895
896 if (RegUnLoadKeyW (HKEY_USERS,
897 SidString.Buffer))
898 {
899 DPRINT1 ("RegUnLoadKeyW() failed (Error %ld)\n", GetLastError());
900 RtlFreeUnicodeString (&SidString);
901 return FALSE;
902 }
903
904 RtlFreeUnicodeString (&SidString);
905
906 DPRINT ("UnloadUserProfile() done\n");
907
908 return TRUE;
909 }
910
911 /* EOF */