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.

595 lines
17 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2001, Microsoft Corporation All rights reserved.
  4. //
  5. // Module Name:
  6. //
  7. // users.c
  8. //
  9. // Abstract:
  10. //
  11. // This file contains dialog to show the users dialog of the
  12. // euroconv.exe utility.
  13. //
  14. // Revision History:
  15. //
  16. // 2001-07-30 lguindon Created.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. ///////////////////////////////////////////////////////////////////////////////
  20. //
  21. // Includes Files.
  22. //
  23. ///////////////////////////////////////////////////////////////////////////////
  24. #include "euroconv.h"
  25. #include "users.h"
  26. #include "util.h"
  27. ///////////////////////////////////////////////////////////////////////////////
  28. //
  29. // Globals.
  30. //
  31. ///////////////////////////////////////////////////////////////////////////////
  32. CHAR gszProfileNT[] = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
  33. CHAR gszProfileVal[] = "ProfileImagePath";
  34. ///////////////////////////////////////////////////////////////////////////////
  35. //
  36. // UsersDialogProc
  37. //
  38. // Message handler function for the Users dialog.
  39. //
  40. ///////////////////////////////////////////////////////////////////////////////
  41. INT_PTR CALLBACK UsersDialogProc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  42. {
  43. HANDLE hFile;
  44. DWORD dwFileSize;
  45. DWORD dwActual;
  46. LPVOID pFileBuffer;
  47. CHAR szEulaPath[MAX_PATH];
  48. switch ( uMsg )
  49. {
  50. case WM_INITDIALOG:
  51. {
  52. HWND hwndInc = GetDlgItem(hWndDlg, IDC_INCLUDED);
  53. HWND hwndExc = GetDlgItem(hWndDlg, IDC_EXCLUDED);
  54. RECT Rect;
  55. LV_COLUMN Column;
  56. //
  57. // Create a column for the Inclusion list view.
  58. //
  59. GetClientRect(hwndInc, &Rect);
  60. Column.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  61. Column.fmt = LVCFMT_LEFT;
  62. Column.cx = Rect.right - GetSystemMetrics(SM_CYHSCROLL);
  63. Column.pszText = NULL;
  64. Column.cchTextMax = 0;
  65. Column.iSubItem = 0;
  66. ListView_InsertColumn(hwndInc, 0, &Column);
  67. //
  68. // Create a column for the Exclusion list view.
  69. //
  70. GetClientRect(hwndExc, &Rect);
  71. Column.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  72. Column.fmt = LVCFMT_LEFT;
  73. Column.cx = Rect.right - GetSystemMetrics(SM_CYHSCROLL);
  74. Column.pszText = NULL;
  75. Column.cchTextMax = 0;
  76. Column.iSubItem = 0;
  77. ListView_InsertColumn(hwndExc, 0, &Column);
  78. //
  79. // Fill out both list
  80. //
  81. ListUsersInfo(hWndDlg);
  82. return 0;
  83. }
  84. case WM_COMMAND:
  85. {
  86. switch (LOWORD(wParam))
  87. {
  88. case IDOK:
  89. {
  90. EndDialog(hWndDlg, ERROR_SUCCESS);
  91. return (1);
  92. }
  93. case IDCANCEL:
  94. {
  95. EndDialog(hWndDlg, ERROR_SUCCESS);
  96. return (1);
  97. }
  98. }
  99. break;
  100. }
  101. case WM_CLOSE:
  102. {
  103. EndDialog(hWndDlg, ERROR_SUCCESS);
  104. return (1);
  105. }
  106. }
  107. return 0;
  108. }
  109. ///////////////////////////////////////////////////////////////////////////////
  110. //
  111. // UsersDialog
  112. //
  113. // Display the Users dialog.
  114. //
  115. ///////////////////////////////////////////////////////////////////////////////
  116. BOOL UsersDialog(HWND hDlg)
  117. {
  118. INT_PTR Status;
  119. Status = DialogBox( NULL,
  120. MAKEINTRESOURCE(IDD_USERS),
  121. hDlg,
  122. UsersDialogProc);
  123. return (Status == ERROR_SUCCESS);
  124. }
  125. ///////////////////////////////////////////////////////////////////////////////
  126. //
  127. // ListUsersInfo
  128. //
  129. // List users and locale information in the appropriate List Box.
  130. //
  131. ///////////////////////////////////////////////////////////////////////////////
  132. void ListUsersInfo(HWND hDlg)
  133. {
  134. HWND hwndInc = GetDlgItem(hDlg, IDC_INCLUDED);
  135. HWND hwndExc = GetDlgItem(hDlg, IDC_EXCLUDED);
  136. //
  137. // List users based on registry entries.
  138. //
  139. ListUsersInfoFromRegistry(hDlg);
  140. //
  141. // List users using a method valid only for Windows NT based on
  142. // user profiles.
  143. //
  144. if (!IsWindows9x())
  145. {
  146. ListUsersInfoFromFile(hDlg);
  147. }
  148. //
  149. // Verify if the inclusion is empty.
  150. //
  151. if(!ListView_GetItemCount(hwndInc))
  152. {
  153. //
  154. // Add the empty item ot the list.
  155. //
  156. AddToList(hwndInc, NULL, (LCID)0);
  157. }
  158. //
  159. // Verify if the exclusion is empty.
  160. //
  161. if(!ListView_GetItemCount(hwndExc))
  162. {
  163. //
  164. // Add the empty item ot the list.
  165. //
  166. AddToList(hwndExc, NULL, (LCID)0);
  167. }
  168. }
  169. ///////////////////////////////////////////////////////////////////////////////
  170. //
  171. // ListUsersInfoFromFile
  172. //
  173. // List users and locale information in the appropriate List Box.
  174. //
  175. ///////////////////////////////////////////////////////////////////////////////
  176. void ListUsersInfoFromFile(HWND hDlg)
  177. {
  178. LCID locale;
  179. PEURO_EXCEPTION pInfo;
  180. HWND hwndInc = GetDlgItem(hDlg, IDC_INCLUDED);
  181. HWND hwndExc = GetDlgItem(hDlg, IDC_EXCLUDED);
  182. //
  183. // Proceed with all users if requested.
  184. //
  185. if (gbAll)
  186. {
  187. CHAR docFolder[MAX_PATH] = {0};
  188. CHAR userFileData[MAX_PATH] = {0};
  189. CHAR searchPattern[MAX_PATH] = {0};
  190. WIN32_FIND_DATA fileData;
  191. HANDLE hList;
  192. //
  193. // Get Documents and Settings folder
  194. //
  195. if (!GetDocumentAndSettingsFolder(docFolder))
  196. {
  197. return;
  198. }
  199. //
  200. // Append a wildcard after the directory path to find
  201. // out all files/folders under it.
  202. //
  203. //strcpy(searchPattern, docFolder);
  204. //strcat(searchPattern, "\\*.*");
  205. StringCbCopy(searchPattern, MAX_PATH, docFolder);
  206. StringCbCatA(searchPattern, MAX_PATH, "\\*.*");
  207. //
  208. // List all files/folder under the profile directory
  209. //
  210. hList = FindFirstFile(searchPattern, &fileData);
  211. if (hList == INVALID_HANDLE_VALUE)
  212. {
  213. return;
  214. }
  215. //
  216. // Search through the Documents and settings folder for users.
  217. //
  218. do
  219. {
  220. //
  221. // Check if it's a directory
  222. //
  223. if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  224. {
  225. //
  226. // Build a full path for the User data file.
  227. //
  228. //strcpy(userFileData, docFolder);
  229. //strcat(userFileData, "\\");
  230. //strcat(userFileData, fileData.cFileName);
  231. //strcat(userFileData, "\\NTUSER.DAT");
  232. StringCbCopy(userFileData, MAX_PATH, docFolder);
  233. StringCbCatA(userFileData, MAX_PATH, "\\");
  234. StringCbCatA(userFileData, MAX_PATH, fileData.cFileName);
  235. StringCbCatA(userFileData, MAX_PATH, "\\NTUSER.DAT");
  236. //
  237. // Check if the file is associated to a valid user and
  238. // get user locale from the user data file.
  239. //
  240. if (IsValidUserDataFile(userFileData) &&
  241. (locale = GetLocaleFromFile(userFileData)))
  242. {
  243. //
  244. // Search for an exception and to the proper list.
  245. //
  246. if ((pInfo = GetLocaleOverrideInfo(locale)) != NULL)
  247. {
  248. //
  249. // Add item to the inclusion list
  250. //
  251. AddToList(hwndInc, CharUpper(fileData.cFileName), locale);
  252. }
  253. else
  254. {
  255. //
  256. // Add item to the exclusion list
  257. //
  258. AddToList(hwndExc, CharUpper(fileData.cFileName), locale);
  259. }
  260. }
  261. }
  262. }
  263. while(FindNextFile(hList, &fileData));
  264. //
  265. // Close handle.
  266. //
  267. FindClose(hList);
  268. }
  269. }
  270. ///////////////////////////////////////////////////////////////////////////////
  271. //
  272. // ListUsersInfo
  273. //
  274. // List users and locale information in the appropriate List Box.
  275. //
  276. ///////////////////////////////////////////////////////////////////////////////
  277. void ListUsersInfoFromRegistry(HWND hDlg)
  278. {
  279. LCID locale;
  280. PEURO_EXCEPTION pInfo;
  281. HWND hwndInc = GetDlgItem(hDlg, IDC_INCLUDED);
  282. HWND hwndExc = GetDlgItem(hDlg, IDC_EXCLUDED);
  283. CHAR strUser[REGSTR_MAX_VALUE_LENGTH] = {0};
  284. DWORD dwUser = REGSTR_MAX_VALUE_LENGTH;
  285. //
  286. // Proceed with all users if requested.
  287. //
  288. if (gbAll)
  289. {
  290. DWORD dwKeyLength, dwKeyIndex = 0;
  291. CHAR szKey[REGSTR_MAX_VALUE_LENGTH]; // this should be dynamic.
  292. HKEY hKey;
  293. DWORD lRet;
  294. LPSTR endPtr;
  295. //
  296. // Go through all users for registry settings.
  297. //
  298. for (;;)
  299. {
  300. dwKeyLength = REGSTR_MAX_VALUE_LENGTH;
  301. lRet = RegEnumKeyEx( HKEY_USERS,
  302. dwKeyIndex,
  303. szKey,
  304. &dwKeyLength,
  305. NULL,
  306. NULL,
  307. NULL,
  308. NULL );
  309. if (lRet == ERROR_NO_MORE_ITEMS)
  310. {
  311. lRet = ERROR_SUCCESS;
  312. break;
  313. }
  314. else if (lRet == ERROR_SUCCESS)
  315. {
  316. //
  317. // Open the registry
  318. //
  319. if (RegOpenKeyEx( HKEY_USERS,
  320. szKey,
  321. 0,
  322. KEY_READ,
  323. &hKey) == ERROR_SUCCESS)
  324. {
  325. //
  326. // Get user locale
  327. //
  328. if (locale = GetLocaleFromRegistry(hKey))
  329. {
  330. //
  331. // Get user name.
  332. //
  333. if ((_stricmp(szKey, ".DEFAULT") == 0) ||
  334. (_stricmp(szKey, "Default User") == 0))
  335. {
  336. //strcpy(strUser, "DEFAULT USER");
  337. StringCbCopy(strUser, ARRAYSIZE(strUser), "DEFAULT USER");
  338. }
  339. else
  340. {
  341. GetUserNameFromRegistry(szKey, ARRAYSIZE(szKey), strUser, ARRAYSIZE(strUser));
  342. }
  343. //
  344. // Search for an exception and to the proper list.
  345. //
  346. if ((pInfo = GetLocaleOverrideInfo(locale)) != NULL)
  347. {
  348. //
  349. // Add item to the inclusion list
  350. //
  351. AddToList(hwndInc, strUser, locale);
  352. }
  353. else
  354. {
  355. //
  356. // Add item to the inclusion list
  357. //
  358. AddToList(hwndExc, strUser, locale);
  359. }
  360. }
  361. //
  362. // Close handle
  363. //
  364. RegCloseKey(hKey);
  365. }
  366. }
  367. else
  368. {
  369. break;
  370. }
  371. //
  372. // Next keys
  373. //
  374. ++dwKeyIndex;
  375. }
  376. }
  377. else
  378. {
  379. //
  380. // Get user locale.
  381. //
  382. locale = GetUserDefaultLCID();
  383. //
  384. // Get user name.
  385. //
  386. GetUserName(strUser, &dwUser);
  387. //
  388. // Search for an exception and to the proper list.
  389. //
  390. if ((pInfo = GetLocaleOverrideInfo(locale)) != NULL)
  391. {
  392. //
  393. // Add item to the inclusion list
  394. //
  395. AddToList(hwndInc, strUser, locale);
  396. }
  397. else
  398. {
  399. //
  400. // Add item to the exclusion list
  401. //
  402. AddToList(hwndExc, strUser, locale);
  403. }
  404. }
  405. }
  406. ///////////////////////////////////////////////////////////////////////////////
  407. //
  408. // AddToList
  409. //
  410. // Generate an entry add to a specific list.
  411. //
  412. ///////////////////////////////////////////////////////////////////////////////
  413. void AddToList(HWND hDlg, LPSTR user, LCID locale)
  414. {
  415. LV_ITEM Item;
  416. LVFINDINFO findInfo;
  417. CHAR strItem[MAX_PATH];
  418. CHAR strLocale[MAX_PATH] = {0};
  419. //
  420. // Get the locale name
  421. //
  422. GetLocaleInfo(locale, LOCALE_SLANGUAGE, strLocale, MAX_PATH);
  423. //
  424. // Create the string.
  425. //
  426. if (user)
  427. {
  428. //sprintf(strItem, "%s - %s", user, strLocale);
  429. StringCchPrintf(strItem, MAX_PATH, "%s - %s", user, strLocale);
  430. }
  431. else
  432. {
  433. LoadString(ghInstance, IDS_EMPTY, strItem, MAX_PATH);
  434. }
  435. //
  436. // Create a find structure.
  437. //
  438. findInfo.flags = LVFI_PARTIAL;
  439. findInfo.psz = user;
  440. findInfo.lParam = 0;
  441. findInfo.vkDirection = 0;
  442. //
  443. // Before adding the string, checks if already there.
  444. //
  445. if (ListView_FindItem(hDlg, -1, &findInfo) < 0)
  446. {
  447. //
  448. // Create the list item to be inserted.
  449. //
  450. Item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE;
  451. Item.iItem = 0;
  452. Item.iSubItem = 0;
  453. Item.state = 0;
  454. Item.stateMask = LVIS_STATEIMAGEMASK;
  455. Item.pszText = strItem;
  456. Item.cchTextMax = 0;
  457. Item.iImage = 0;
  458. Item.lParam = 0;
  459. //
  460. // Insert the item into the list view.
  461. //
  462. ListView_InsertItem(hDlg, &Item);
  463. }
  464. }
  465. ///////////////////////////////////////////////////////////////////////////////
  466. //
  467. // GetUserNameFromRegistry
  468. //
  469. // Get user name.
  470. //
  471. ///////////////////////////////////////////////////////////////////////////////
  472. void GetUserNameFromRegistry(LPSTR strKey, int cbKey, LPSTR name, int cbname)
  473. {
  474. CHAR strUserKey[REGSTR_MAX_VALUE_LENGTH];
  475. CHAR strProfilePath[REGSTR_MAX_VALUE_LENGTH] = {0};
  476. DWORD dwPath = REGSTR_MAX_VALUE_LENGTH;
  477. LPSTR ptrName = NULL;
  478. HKEY hKey;
  479. //
  480. // Process different on each platform.
  481. //
  482. if (IsWindows9x())
  483. {
  484. //
  485. // Use the key name directly.
  486. //
  487. //strcpy(name, strKey);
  488. StringCbCopy(name, cbKey, strKey);
  489. //
  490. // Uppercase
  491. //
  492. CharUpper(name);
  493. return;
  494. }
  495. else
  496. {
  497. //
  498. // Form the registry path.
  499. //
  500. //sprintf(strUserKey, "%s\\%s", gszProfileNT, strKey);
  501. StringCchPrintf(strUserKey, ARRAYSIZE(strUserKey), "%s\\%s", gszProfileNT, strKey);
  502. //
  503. // Open the registry key previously formed.
  504. //
  505. if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  506. strUserKey,
  507. 0,
  508. KEY_READ,
  509. &hKey) == ERROR_SUCCESS)
  510. {
  511. //
  512. // Query the value
  513. //
  514. if (RegQueryValueEx( hKey,
  515. gszProfileVal,
  516. NULL,
  517. NULL,
  518. strProfilePath,
  519. &dwPath) == ERROR_SUCCESS)
  520. {
  521. if (ptrName = strrchr(strProfilePath, '\\'))
  522. {
  523. ptrName++;
  524. }
  525. }
  526. }
  527. //
  528. // Return the name.
  529. //
  530. if (ptrName)
  531. {
  532. CharUpper(ptrName);
  533. //strcpy(name, ptrName);
  534. StringCbCopy(name, cbname, ptrName);
  535. }
  536. else
  537. {
  538. //strcpy(name, strKey);
  539. StringCbCopy(name, cbname, strKey);
  540. }
  541. }
  542. }