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