Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

333 lines
11 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. loopuser.c
  5. Abstract:
  6. Loop each user and call ApplyUserSettings with user profile.
  7. Author:
  8. Geoffrey Guo (geoffguo) 22-Sep-2001 Created
  9. Revision History:
  10. <alias> <date> <comments>
  11. --*/
  12. #include "StdAfx.h"
  13. #include "clmt.h"
  14. TOKEN_PRIVILEGES PrevTokenPriv;
  15. //--------------------------------------------------------------------------
  16. //
  17. // LoopUser
  18. //
  19. // Enumerate users and call ApplyUserSettings with user profile.
  20. //
  21. //
  22. //--------------------------------------------------------------------------
  23. BOOL LoopUser(USERENUMPROC lpUserEnumProc)
  24. {
  25. BOOL fRet = TRUE;
  26. TCHAR UserSid[MAX_PATH];
  27. DWORD ValueType = 0;
  28. DWORD cbUserSid = 0;
  29. DWORD cbValueType = 0;
  30. LONG lRet;
  31. HKEY hKey;
  32. PTCHAR ptr = NULL;
  33. FILETIME ft;
  34. TCHAR szSysDrv[3];
  35. HRESULT hr;
  36. TCHAR szDomainUserName[MAX_PATH];
  37. DPF (REGmsg, L"Enter LoopUser: ");
  38. if (ExpandEnvironmentStrings(L"%SystemDrive%",
  39. szSysDrv,
  40. sizeof(szSysDrv) / sizeof(TCHAR)) > 3)
  41. {
  42. fRet = FALSE;
  43. DPF (REGerr, L"LoopUser: Incorrect SystemDrive: %s", szSysDrv);
  44. goto Exit;
  45. }
  46. lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  47. g_cszProfileList,
  48. 0,
  49. KEY_READ,
  50. &hKey);
  51. if (lRet == ERROR_SUCCESS)
  52. {
  53. DWORD dwIndex = 0;
  54. //
  55. // Enumerate and get the Sids for each user.
  56. //
  57. while ( TRUE )
  58. {
  59. *UserSid = 0;
  60. cbUserSid = sizeof(UserSid)/sizeof(TCHAR);
  61. lRet = RegEnumKeyEx( hKey,
  62. dwIndex,
  63. UserSid,
  64. &cbUserSid,
  65. 0,
  66. NULL,
  67. 0,
  68. &ft);
  69. if (lRet != ERROR_SUCCESS)
  70. {
  71. if (lRet != ERROR_NO_MORE_ITEMS)
  72. {
  73. DPF (REGerr, L"LoopUser: RegEnumKeyEx fails. lResult=%d", lRet);
  74. fRet = FALSE;
  75. }
  76. break;
  77. }
  78. CharUpper( UserSid );
  79. //
  80. // User Process
  81. //
  82. if (cbUserSid > 0)
  83. {
  84. LONG lLoadUser;
  85. HKEY hKeyUser, hKeyEnv, hKeyProfileList;
  86. TCHAR UserProfilePath[MAX_PATH];
  87. TCHAR UserProfileHive[MAX_PATH];
  88. TCHAR UserName[MAX_PATH];
  89. TCHAR DomainName[MAX_PATH];
  90. DWORD UserNameLen = sizeof(UserName)/sizeof(TCHAR);
  91. DWORD DomainNameLen = sizeof(DomainName)/sizeof(TCHAR);
  92. DWORD cbUserProfilePath = 0;
  93. PSID pSid = NULL;
  94. SID_NAME_USE sidUse;
  95. BOOL bRet;
  96. ConvertStringSidToSid(UserSid,&pSid);
  97. if (!IsValidSid(pSid))
  98. {
  99. DPF (REGmsg, L"LoopUser: SID %s is not valid ", UserSid );
  100. dwIndex++;
  101. continue;
  102. }
  103. bRet = LookupAccountSid( NULL, pSid, UserName, &UserNameLen,
  104. DomainName, &DomainNameLen, &sidUse );
  105. if ( pSid )
  106. {
  107. LocalFree ( pSid );
  108. pSid = NULL;
  109. }
  110. if ( !bRet )
  111. {
  112. DWORD dwErr = GetLastError();
  113. if (ERROR_NONE_MAPPED == dwErr)
  114. {
  115. dwIndex++;
  116. continue;
  117. }
  118. else
  119. {
  120. DPF (REGerr, L"LoopUser: LookupAccountSid fails for %s with win32 error = %d", UserSid,dwErr);
  121. fRet = FALSE;
  122. break;
  123. }
  124. }
  125. lRet = RegOpenKeyEx( hKey,
  126. UserSid,
  127. 0,
  128. KEY_READ,
  129. &hKeyProfileList );
  130. if ( lRet == ERROR_SUCCESS )
  131. {
  132. DWORD Type;
  133. cbUserProfilePath = sizeof( UserProfilePath );
  134. lRet = RegQueryValueEx( hKeyProfileList,
  135. g_cszProfileImagePath,
  136. NULL,
  137. &Type,
  138. (PBYTE)UserProfilePath,
  139. &cbUserProfilePath );
  140. RegCloseKey( hKeyProfileList );
  141. if ( lRet != ERROR_SUCCESS )
  142. {
  143. DPF (REGerr, L"LoopUser: RegQueryValueEx fails. lResult=%d", lRet);
  144. fRet = FALSE;
  145. dwIndex++;
  146. continue;
  147. }
  148. }
  149. else
  150. {
  151. DPF (REGerr, L"LoopUser: RegOpenKeyEx fails. lResult=%d", lRet);
  152. fRet = FALSE;
  153. dwIndex++;
  154. continue;
  155. }
  156. lRet = RegOpenKeyEx( HKEY_USERS,
  157. UserSid,
  158. 0,
  159. KEY_ALL_ACCESS,
  160. &hKeyUser);
  161. lLoadUser = ERROR_FILE_NOT_FOUND;
  162. if (lRet == ERROR_FILE_NOT_FOUND)
  163. {
  164. //
  165. // Create NTUSER.Dat path from the user profile path
  166. //
  167. if (ExpandEnvironmentStrings( UserProfilePath,
  168. UserProfileHive,
  169. sizeof(UserProfileHive) / sizeof(TCHAR)) > MAX_PATH)
  170. {
  171. DPF (REGerr, L"LoopUser: Incorrect UserProfile %s", UserProfileHive);
  172. fRet = FALSE;
  173. goto Exit;
  174. }
  175. if (UserProfileHive[0] != szSysDrv[0])
  176. {
  177. DPF (REGwar, L"LoopUser: UserProfilePath=%s is not in System Drive, skipped.", UserProfileHive);
  178. dwIndex++;
  179. continue;
  180. }
  181. if (FAILED(StringCchCopy(UserProfilePath, MAX_PATH, UserProfileHive)))
  182. {
  183. DPF (REGerr, L"LoopUser: UserProfilePath too samll for %s", UserProfileHive);
  184. fRet = FALSE;
  185. goto Exit;
  186. }
  187. if (FAILED(StringCchCat(UserProfileHive, MAX_PATH, TEXT("\\NTUSER.DAT"))))
  188. {
  189. DPF (REGerr, L"LoopUser: UserProfilePath too samll for %s", UserProfileHive);
  190. fRet = FALSE;
  191. goto Exit;
  192. }
  193. // load the hive
  194. // Note: if the specified hive is already loaded
  195. // this call will return ERROR_SHARING_VIOLATION
  196. // We don't worry about this because if the
  197. // hive were loaded, we shouldn't be here
  198. lLoadUser = RegLoadKey(HKEY_USERS, UserSid, UserProfileHive);
  199. if ( lLoadUser != ERROR_SUCCESS )
  200. {
  201. DPF (REGerr, L"LoopUser: RegLoadKey fails. lResult=%d", lLoadUser);
  202. fRet = FALSE;
  203. dwIndex++;
  204. continue;
  205. }
  206. lRet = RegOpenKeyEx( HKEY_USERS,
  207. UserSid,
  208. 0,
  209. KEY_ALL_ACCESS,
  210. &hKeyUser);
  211. }
  212. else if (lRet != ERROR_SUCCESS)
  213. {
  214. DPF (REGerr, L"LoopUser: RegOpenKeyEx fails. lResult=%d", lRet);
  215. fRet = FALSE;
  216. }
  217. // give Call back function a chance to excute
  218. if (DomainName[0] != TEXT('\0'))
  219. {
  220. hr = StringCchCopy(szDomainUserName,
  221. ARRAYSIZE(szDomainUserName),
  222. DomainName);
  223. if (SUCCEEDED(hr))
  224. {
  225. hr = StringCchCat(szDomainUserName,
  226. ARRAYSIZE(szDomainUserName),
  227. TEXT("\\"));
  228. hr = StringCchCat(szDomainUserName,
  229. ARRAYSIZE(szDomainUserName),
  230. UserName);
  231. if (FAILED(hr))
  232. {
  233. hr = StringCchCopy(szDomainUserName,
  234. ARRAYSIZE(szDomainUserName),
  235. UserName);
  236. }
  237. }
  238. }
  239. hr = lpUserEnumProc(hKeyUser, szDomainUserName, DomainName,UserSid);
  240. if (FAILED(hr))
  241. {
  242. fRet = FALSE;
  243. }
  244. RegCloseKey( hKeyUser );
  245. if ( lLoadUser == ERROR_SUCCESS )
  246. RegUnLoadKey(HKEY_USERS, UserSid);
  247. }
  248. dwIndex++;
  249. }
  250. RegCloseKey( hKey );
  251. }
  252. else
  253. {
  254. DPF (REGerr, L"LoopUser: Open profile list fails. lResult=%d", lRet);
  255. fRet = FALSE;
  256. goto Exit;
  257. }
  258. // For Default User Setting
  259. // Note the assumption here is the default user name is not localized
  260. if (fRet)
  261. {
  262. lRet = RegOpenKeyEx(HKEY_USERS,
  263. _T(".DEFAULT"),
  264. 0,
  265. KEY_ALL_ACCESS,
  266. &hKey);
  267. if (lRet != ERROR_SUCCESS)
  268. {
  269. DPF (REGerr, L"LoopUser: Open Default User key fails. lResult=%d", lRet);
  270. fRet = FALSE;
  271. goto Exit;
  272. }
  273. hr = lpUserEnumProc(hKey, DEFAULT_USER, NULL,TEXT("Default_User_SID"));
  274. if (FAILED(hr))
  275. {
  276. fRet = FALSE;
  277. }
  278. RegCloseKey( hKey );
  279. }
  280. Exit:
  281. DPF (REGmsg, L"Exit LoopUser with return %d", fRet);
  282. return fRet;
  283. }