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.

1033 lines
35 KiB

  1. //--------------------------------------------------------------------------
  2. //
  3. // admui.cpp
  4. //
  5. // IEAK Global Policy Template Functions
  6. //
  7. //--------------------------------------------------------------------------
  8. #include <w95wraps.h>
  9. #include <windows.h>
  10. #include <stdlib.h>
  11. #include <shlwapi.h>
  12. #include "parse.h"
  13. #include "controls.h"
  14. #include "resource.h"
  15. #define ADMUI_WIDTH 250
  16. #define ADMUI_MARGIN 5
  17. #define ADMUI_HEIGHT 25
  18. #define MAX_ADM_ITEMS 100
  19. // function prototypes from admparse.cpp
  20. extern void ReadRegSettingsForADM(LPADMFILE admfile, LPPARTDATA pPartData,
  21. BSTR bstrNamespace);
  22. extern void ReadInfFile(LPADMFILE, LPCTSTR, LPPARTDATA);
  23. extern BOOL ReadAdmFile(LPADMFILE, LPCTSTR);
  24. extern void FreeAdmMemory(LPADMFILE);
  25. extern void FreePartData(LPVOID*, int);
  26. extern void WriteInfFile(LPADMFILE, LPCTSTR, LPPARTDATA);
  27. extern LPCTSTR BaseFileName(LPCTSTR);
  28. extern void FreeActionList(LPACTIONLIST pActionList, int nActions);
  29. // private forward declarations
  30. static HRESULT admInitHelper(LPCTSTR pcszAdmFile, LPCTSTR pcszInfFile, BSTR bstrNamespace, LPDWORD hAdm, LPVOID* pData);
  31. static HRESULT admFinishedHelper(DWORD hAdm, LPCTSTR pcszInfFile, LPVOID pPartData);
  32. static HRESULT createAdmUiHelper(DWORD hAdm, HWND hParent, int x, int y, int width, int height,
  33. DWORD dwStyle, DWORD dwExStyle, LPCTSTR pcszCategory, HKEY hKeyClass,
  34. HWND *phWnd, LPVOID pPartData, LPVOID* pCategoryData, BOOL fRSoPMode);
  35. static HRESULT getAdmCategoriesHelper(DWORD hAdm, LPTSTR pszCategories, int cchLength, int *nBytes);
  36. static HRESULT checkDuplicateKeysHelper(DWORD hAdm, DWORD hCompareAdm, LPCTSTR pcszLogFile, BOOL bClearFile);
  37. static HRESULT admResetHelper(DWORD hAdm, LPCTSTR pcszInfFile, LPVOID pPartData, LPVOID pCategoryData);
  38. static void getFontInfoHelper(LPTSTR pszFontName, LPINT pnFontSize);
  39. //--------------------------------------------------------------------------
  40. // Global Variables and local structure definitions
  41. //--------------------------------------------------------------------------
  42. typedef struct admUI
  43. {
  44. HWND hWnd;
  45. int hAdm;
  46. int nAdmWidth;
  47. int nAdmHeight;
  48. CStaticWindow wndFrame;
  49. CStaticWindow wndCategory;
  50. CStaticWindow wndCategoryType;
  51. CAdmControl* pControlList;
  52. int nControls;
  53. int nHOffset;
  54. int nVOffset;
  55. int nHMaxOffset;
  56. int nVMaxOffset;
  57. int nScrollPage;
  58. HKEY hKeyClass;
  59. LPTSTR pszCategory;
  60. } ADMUI, *LPADMUI;
  61. typedef struct admCreate
  62. {
  63. LPPARTDATA pPartData;
  64. LPADMUI pAdmUI;
  65. BOOL fRSoPMode;
  66. } ADMCREATE, *LPADMCREATE;
  67. HINSTANCE g_hInst;
  68. ADMFILE admFile[MAX_ADM_ITEMS];
  69. int g_nAdmInstances = 0;
  70. BOOL g_fAdmDirty = FALSE;
  71. CRITICAL_SECTION g_hAdmCriticalSection;
  72. //--------------------------------------------------------------------------
  73. // D L L M A I N
  74. //
  75. // Program Entry Point
  76. //--------------------------------------------------------------------------
  77. BOOL WINAPI DllMain( HINSTANCE hModule, DWORD fdwReason, LPVOID)
  78. {
  79. g_hInst = hModule;
  80. if (fdwReason == DLL_PROCESS_ATTACH)
  81. {
  82. InitializeCriticalSection(&g_hAdmCriticalSection);
  83. ZeroMemory(admFile, sizeof(ADMFILE) * MAX_ADM_ITEMS); // initializing the admui structure
  84. }
  85. if (fdwReason == DLL_PROCESS_DETACH)
  86. {
  87. if (FindAtomA("AdmUiClass"))
  88. {
  89. for (int nIndex = 0; nIndex < g_nAdmInstances; nIndex++)
  90. FreeAdmMemory(&admFile[nIndex]);
  91. UnregisterClassA("AdmUiClass", g_hInst);
  92. DeleteAtom( FindAtomA( "AdmUiClass" ));
  93. }
  94. DeleteCriticalSection(&g_hAdmCriticalSection);
  95. }
  96. return TRUE;
  97. }
  98. //--------------------------------------------------------------------------
  99. // A D M U I P R O C
  100. //
  101. // Window procedure used by CreateAdmUi
  102. //--------------------------------------------------------------------------
  103. LRESULT CALLBACK AdmUiProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  104. {
  105. int delta;
  106. LPADMUI pAdmUI;
  107. pAdmUI = (LPADMUI)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  108. switch( msg )
  109. {
  110. case WM_CREATE:
  111. {
  112. int y = 5;
  113. int nControlsAlloc = 25;
  114. int nFrameWidth = 0;
  115. int nTextWidth = 0;
  116. int nCategoryPart = 0;
  117. int nControls = 0;
  118. BOOL bContinue = TRUE;
  119. RECT rect;
  120. TCHAR szMsg[MAX_PATH];
  121. LPCREATESTRUCT cs;
  122. TCHAR szCategoryText[MAX_PATH];
  123. LPADMCREATE pAdmCreate;
  124. cs = (LPCREATESTRUCT) lParam;
  125. pAdmCreate = (LPADMCREATE) cs->lpCreateParams;
  126. pAdmUI = pAdmCreate->pAdmUI;
  127. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pAdmUI);
  128. GetClientRect(hWnd, &rect);
  129. nFrameWidth = rect.right - 5;
  130. nTextWidth = nFrameWidth - (ADMUI_MARGIN * 4);
  131. pAdmUI->wndFrame.Create( hWnd, 0, 0, nFrameWidth, 8000, CSW_FRAME );
  132. pAdmUI->wndCategory.Create( pAdmUI->wndFrame.Hwnd(), ADMUI_MARGIN, y,
  133. nFrameWidth - (ADMUI_MARGIN*2), ADMUI_HEIGHT, CSW_BOLDLABEL );
  134. StrCpy(szCategoryText, pAdmUI->pszCategory );
  135. y += pAdmUI->wndCategory.SetText( szCategoryText );
  136. pAdmUI->wndCategoryType.Create(pAdmUI->wndFrame.Hwnd(), ADMUI_MARGIN, y,
  137. nFrameWidth - (ADMUI_MARGIN*2), ADMUI_HEIGHT, CSW_ITALICLABEL);
  138. y += pAdmUI->wndCategoryType.SetText( TEXT(" ") );
  139. y += 6;
  140. pAdmUI->pControlList = (CAdmControl*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CAdmControl) * nControlsAlloc);
  141. if(pAdmUI->pControlList == NULL)
  142. break;
  143. nControls = 0;
  144. for( int i = 0; i < admFile[pAdmUI->hAdm].nParts && bContinue; i++ )
  145. {
  146. if( StrCmpI( admFile[pAdmUI->hAdm].pParts[i].szCategory, pAdmUI->pszCategory ) == 0 &&
  147. admFile[pAdmUI->hAdm].pParts[i].hkClass == pAdmUI->hKeyClass)
  148. {
  149. if(nControls >= nControlsAlloc)
  150. {
  151. LPVOID lpTemp = NULL;
  152. nControlsAlloc += 20;
  153. lpTemp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pAdmUI->pControlList, sizeof(CAdmControl) * nControlsAlloc);
  154. if(lpTemp == NULL)
  155. bContinue = FALSE;
  156. else
  157. pAdmUI->pControlList = (CAdmControl*) lpTemp;
  158. }
  159. nCategoryPart = i;
  160. y = pAdmUI->pControlList[nControls].Create( pAdmUI->wndFrame.Hwnd(), ADMUI_MARGIN*3, y,
  161. 0, 0, nTextWidth, &(admFile[pAdmUI->hAdm].pParts[i]),
  162. &(pAdmCreate->pPartData[i]), pAdmCreate->fRSoPMode );
  163. pAdmUI->pControlList[nControls].SetPart(i);
  164. nControls++;
  165. }
  166. }
  167. pAdmUI->nControls = nControls;
  168. if(admFile[pAdmUI->hAdm].pParts[nCategoryPart].hkClass == HKEY_LOCAL_MACHINE)
  169. {
  170. LoadString(g_hInst, IDS_PERMACHINE, szMsg, ARRAYSIZE(szMsg));
  171. wnsprintf(szCategoryText, ARRAYSIZE(szCategoryText), TEXT("[ %s ]"), szMsg);
  172. pAdmUI->wndCategoryType.SetText(szCategoryText);
  173. }
  174. else if(admFile[pAdmUI->hAdm].pParts[nCategoryPart].hkClass == HKEY_CURRENT_USER)
  175. {
  176. LoadString(g_hInst, IDS_PERUSER, szMsg, sizeof(szMsg));
  177. wnsprintf(szCategoryText, ARRAYSIZE(szCategoryText), TEXT("[ %s ]"), szMsg);
  178. pAdmUI->wndCategoryType.SetText(szCategoryText);
  179. }
  180. if( pAdmUI->nAdmWidth < nFrameWidth )
  181. pAdmUI->nHMaxOffset = nFrameWidth - pAdmUI->nAdmWidth;
  182. else
  183. pAdmUI->nHMaxOffset = 0;
  184. SetScrollRange( hWnd, SB_HORZ, 0, pAdmUI->nHMaxOffset, TRUE );
  185. if( pAdmUI->nAdmHeight < (y + 5) )
  186. pAdmUI->nVMaxOffset = y - pAdmUI->nAdmHeight + 5;
  187. else
  188. pAdmUI->nVMaxOffset = 0;
  189. pAdmUI->nHOffset = 0;
  190. pAdmUI->nVOffset = 0;
  191. SetScrollRange( hWnd, SB_VERT, 0, pAdmUI->nVMaxOffset, TRUE );
  192. pAdmUI->nScrollPage = rect.bottom - 10;
  193. ShowWindow( hWnd, SW_SHOWNORMAL );
  194. }
  195. break;
  196. case WM_HSCROLL:
  197. delta = pAdmUI->nHOffset;
  198. switch( LOWORD(wParam) )
  199. {
  200. case SB_LINELEFT:
  201. if( pAdmUI->nHOffset > 0 )
  202. pAdmUI->nHOffset -= SCROLL_LINE;
  203. break;
  204. case SB_LINERIGHT:
  205. if( pAdmUI->nHOffset < pAdmUI->nHMaxOffset )
  206. pAdmUI->nHOffset += SCROLL_LINE;
  207. break;
  208. case SB_PAGELEFT:
  209. if( pAdmUI->nHOffset >= SCROLL_PAGE )
  210. pAdmUI->nHOffset -= SCROLL_PAGE;
  211. else if( pAdmUI->nHOffset < SCROLL_PAGE )
  212. pAdmUI->nHOffset = 0;
  213. break;
  214. case SB_PAGERIGHT:
  215. if( pAdmUI->nHOffset <= (pAdmUI->nHMaxOffset - SCROLL_PAGE))
  216. pAdmUI->nHOffset += SCROLL_PAGE;
  217. else if( pAdmUI->nHOffset > (pAdmUI->nHMaxOffset - SCROLL_PAGE))
  218. pAdmUI->nHOffset = pAdmUI->nHMaxOffset;
  219. break;
  220. case SB_THUMBTRACK:
  221. case SB_THUMBPOSITION:
  222. pAdmUI->nHOffset = (short int) HIWORD(wParam);
  223. break;
  224. }
  225. SetScrollPos( hWnd, SB_HORZ, pAdmUI->nHOffset, TRUE );
  226. delta = pAdmUI->nHOffset - delta;
  227. if( delta != 0 )
  228. {
  229. pAdmUI->wndFrame.MoveLeft( delta );
  230. }
  231. break;
  232. case WM_VSCROLL:
  233. if( lParam == NULL )
  234. {
  235. delta = pAdmUI->nVOffset;
  236. switch( LOWORD(wParam) )
  237. {
  238. case SB_LINEUP:
  239. if( pAdmUI->nVOffset > 0 )
  240. pAdmUI->nVOffset -= SCROLL_LINE;
  241. break;
  242. case SB_LINEDOWN:
  243. if( pAdmUI->nVOffset < pAdmUI->nVMaxOffset )
  244. pAdmUI->nVOffset += SCROLL_LINE;
  245. break;
  246. case SB_PAGEUP:
  247. if( pAdmUI->nVOffset >= pAdmUI->nScrollPage )
  248. pAdmUI->nVOffset -= pAdmUI->nScrollPage;
  249. else if( pAdmUI->nVOffset < pAdmUI->nScrollPage )
  250. pAdmUI->nVOffset = 0;
  251. break;
  252. case SB_PAGEDOWN:
  253. if( pAdmUI->nVOffset <= (pAdmUI->nVMaxOffset - pAdmUI->nScrollPage ))
  254. pAdmUI->nVOffset += pAdmUI->nScrollPage;
  255. else if( pAdmUI->nVOffset > (pAdmUI->nVMaxOffset - pAdmUI->nScrollPage ))
  256. pAdmUI->nVOffset = pAdmUI->nVMaxOffset;
  257. break;
  258. case SB_THUMBTRACK:
  259. case SB_THUMBPOSITION:
  260. pAdmUI->nVOffset = (short int) HIWORD(wParam);
  261. break;
  262. }
  263. SetScrollPos( hWnd, SB_VERT, pAdmUI->nVOffset, TRUE );
  264. delta = pAdmUI->nVOffset - delta;
  265. if( delta != 0 )
  266. {
  267. pAdmUI->wndFrame.MoveUp( delta );
  268. }
  269. }
  270. break;
  271. case WM_DESTROY:
  272. if (pAdmUI != NULL)
  273. {
  274. if (pAdmUI->pszCategory != NULL)
  275. LocalFree(pAdmUI->pszCategory);
  276. GlobalFree(pAdmUI);
  277. }
  278. break;
  279. default:
  280. return DefWindowProc( hWnd, msg, wParam, lParam );
  281. }
  282. return 1;
  283. }
  284. //--------------------------------------------------------------------------
  285. // I N I T A D M P A R S E R
  286. //
  287. // Parses an adm file and sets up memory addresses in structures
  288. //--------------------------------------------------------------------------
  289. BOOL InitAdmParser(DWORD hAdm, LPCTSTR szInput)
  290. {
  291. if( !ReadAdmFile(&admFile[hAdm], szInput))
  292. return FALSE;
  293. return TRUE;
  294. }
  295. static int AdmGetNewInstance()
  296. {
  297. for (int nIndex = 0; nIndex < MAX_ADM_ITEMS; nIndex++)
  298. {
  299. if (*admFile[nIndex].szFilename == TEXT('\0'))
  300. return nIndex;
  301. }
  302. // no more indexes available
  303. return -1;
  304. }
  305. //--------------------------------------------------------------------------
  306. // A D M I N I T Exported Function
  307. //
  308. // Parses an adm file into structures and loads an inf as defaults
  309. //
  310. // Parameters:
  311. // szAdmFile: full path to global policy template file
  312. // szInfFile: full path to inf file
  313. // hAdm: pointer to a dword to recieve an adm handle
  314. //--------------------------------------------------------------------------
  315. STDAPI AdmInitA(LPCSTR pcszAdmFile, LPCSTR pcszInfFile, BSTR bstrNamespace,
  316. LPDWORD lpdwAdm, LPVOID* pData)
  317. {
  318. USES_CONVERSION;
  319. return admInitHelper(A2CT(pcszAdmFile), A2CT(pcszInfFile), bstrNamespace,
  320. lpdwAdm, pData);
  321. }
  322. STDAPI AdmInitW(LPCWSTR pcwszAdmFile, LPCWSTR pcwszInfFile, BSTR bstrNamespace,
  323. LPDWORD lpdwAdm, LPVOID* pData)
  324. {
  325. USES_CONVERSION;
  326. return admInitHelper(W2CT(pcwszAdmFile), W2CT(pcwszInfFile), bstrNamespace,
  327. lpdwAdm, pData);
  328. }
  329. //--------------------------------------------------------------------------
  330. // A D M F I N I S H E D Exported Function
  331. //
  332. // Saves all data into an .inf file
  333. //
  334. // Parameters:
  335. // hAdm: adm handle
  336. // szInfFile: full path name to the inf to write
  337. //--------------------------------------------------------------------------
  338. STDAPI AdmFinishedA(DWORD hAdm, LPCSTR pcszInfFile, LPVOID pPartData)
  339. {
  340. USES_CONVERSION;
  341. return admFinishedHelper(hAdm, A2CT(pcszInfFile), pPartData);
  342. }
  343. STDAPI AdmFinishedW(DWORD hAdm, LPCWSTR pcwszInfFile, LPVOID pPartData)
  344. {
  345. USES_CONVERSION;
  346. return admFinishedHelper(hAdm, W2CT(pcwszInfFile), pPartData);
  347. }
  348. //--------------------------------------------------------------------------
  349. // C R E A T E A D M U I Exported Function
  350. //
  351. // Creates a user interface based on an adm file
  352. //
  353. // Parameters:
  354. // hAdm: adm handle
  355. // hParent: parent window
  356. // x, y, width, height: location and dimensions of window to create
  357. // dwStyle: additional style flags to pass to window creation
  358. // dwExStyle: additional extra style flags to pass to window creation
  359. // szCategory: a string specifying the adm category to display
  360. // hWnd: a pointer to an HWND that receives the newly created
  361. // window handle
  362. //--------------------------------------------------------------------------
  363. STDAPI CreateAdmUiA(DWORD hAdm, HWND hParent, int x, int y, int width, int height,
  364. DWORD dwStyle, DWORD dwExStyle, LPCSTR pcszCategory, HKEY hKeyClass,
  365. HWND *phWnd, LPVOID pPartData, LPVOID* pCategoryData, BOOL fRSoPMode)
  366. {
  367. USES_CONVERSION;
  368. return createAdmUiHelper(hAdm, hParent, x, y, width, height, dwStyle, dwExStyle,
  369. A2CT(pcszCategory), hKeyClass, phWnd, pPartData, pCategoryData, fRSoPMode);
  370. }
  371. STDAPI CreateAdmUiW(DWORD hAdm, HWND hParent, int x, int y, int width, int height,
  372. DWORD dwStyle, DWORD dwExStyle, LPCWSTR pcwszCategory, HKEY hKeyClass,
  373. HWND *phWnd, LPVOID pPartData, LPVOID* pCategoryData, BOOL fRSoPMode)
  374. {
  375. USES_CONVERSION;
  376. return createAdmUiHelper(hAdm, hParent, x, y, width, height, dwStyle, dwExStyle,
  377. W2CT(pcwszCategory), hKeyClass, phWnd, pPartData, pCategoryData, fRSoPMode);
  378. }
  379. //--------------------------------------------------------------------------
  380. // G E T A D M C A T E G O R I E S Exported Function
  381. //
  382. // Returns a list of available categories to the caller
  383. //
  384. // Parameters:
  385. // hAdm: adm handle
  386. // szCategories: zero separated list of available categories
  387. // nLength: size of buffer pointed to by szCategories
  388. // nBytes: a pointer to an int that receives the number
  389. // of bytes copied into szCategory
  390. //--------------------------------------------------------------------------
  391. STDAPI GetAdmCategoriesA(DWORD hAdm, LPSTR pszCategories, int cchLength, int *nBytes)
  392. {
  393. LPTSTR lpCategories = (LPTSTR)CoTaskMemAlloc(StrCbFromCch(cchLength));
  394. HRESULT hr;
  395. if (lpCategories == NULL)
  396. hr = E_OUTOFMEMORY;
  397. else
  398. {
  399. hr = getAdmCategoriesHelper(hAdm, lpCategories, cchLength, nBytes);
  400. T2Abuf(lpCategories, pszCategories, cchLength);
  401. CoTaskMemFree(lpCategories);
  402. }
  403. return hr;
  404. }
  405. STDAPI GetAdmCategoriesW(DWORD hAdm, LPWSTR pwszCategories, int cchLength, int *nBytes)
  406. {
  407. LPTSTR lpCategories = (LPTSTR)CoTaskMemAlloc(StrCbFromCch(cchLength));
  408. HRESULT hr;
  409. if (lpCategories == NULL)
  410. hr = E_OUTOFMEMORY;
  411. else
  412. {
  413. hr = getAdmCategoriesHelper(hAdm, lpCategories, cchLength, nBytes);
  414. T2Wbuf(lpCategories, pwszCategories, cchLength);
  415. CoTaskMemFree(lpCategories);
  416. }
  417. return hr;
  418. }
  419. //--------------------------------------------------------------------------
  420. // C H E C K D U P L I C A T E K E Y S Exported Function
  421. //
  422. // Checks for duplicate key names
  423. //
  424. // Parameters:
  425. // hAdm: adm handle
  426. // hCompareAdm: adm handle of file to be compared
  427. // szLogFile: full path name to the log file
  428. // bClearFile: TRUE - clear log file; FALSE - do not clear log file
  429. //--------------------------------------------------------------------------
  430. STDAPI CheckDuplicateKeysA(DWORD hAdm, DWORD hCompareAdm, LPCSTR pcszLogFile, BOOL bClearFile)
  431. {
  432. USES_CONVERSION;
  433. return checkDuplicateKeysHelper(hAdm, hCompareAdm, A2CT(pcszLogFile), bClearFile);
  434. }
  435. STDAPI CheckDuplicateKeysW(DWORD hAdm, DWORD hCompareAdm, LPCWSTR pcwszLogFile, BOOL bClearFile)
  436. {
  437. USES_CONVERSION;
  438. return checkDuplicateKeysHelper(hAdm, hCompareAdm, W2CT(pcwszLogFile), bClearFile);
  439. }
  440. //--------------------------------------------------------------------------
  441. // A D M R E S E T Exported Function
  442. //
  443. // Resets the Adm file with the data from the inf file
  444. // If no inf file is passed resets the adm file data to NULL.
  445. //
  446. // Parameters:
  447. // hAdm: adm handle
  448. // szInfFile: full path name to the inf file
  449. //--------------------------------------------------------------------------
  450. STDAPI AdmResetA(DWORD hAdm, LPCSTR pcszInfFile, LPVOID pPartData, LPVOID pCategoryData)
  451. {
  452. USES_CONVERSION;
  453. return admResetHelper(hAdm, A2CT(pcszInfFile), pPartData, pCategoryData);
  454. }
  455. STDAPI AdmResetW(DWORD hAdm, LPCWSTR pcwszInfFile, LPVOID pPartData, LPVOID pCategoryData)
  456. {
  457. USES_CONVERSION;
  458. return admResetHelper(hAdm, W2CT(pcwszInfFile), pPartData, pCategoryData);
  459. }
  460. //--------------------------------------------------------------------------
  461. // A D M C L O S E Exported Function
  462. //
  463. // Releases all memory associated with this adm file
  464. //
  465. // Parameters:
  466. // hAdm: adm handle
  467. //--------------------------------------------------------------------------
  468. STDAPI AdmClose(DWORD hAdm, LPVOID* pPartData, BOOL fClear)
  469. {
  470. EnterCriticalSection(&g_hAdmCriticalSection);
  471. FreePartData(pPartData, admFile[hAdm].nParts);
  472. *pPartData = NULL;
  473. if (fClear == TRUE)
  474. {
  475. FreeAdmMemory(&admFile[hAdm]);
  476. ZeroMemory(&admFile[hAdm], sizeof(ADMUI));
  477. }
  478. LeaveCriticalSection(&g_hAdmCriticalSection);
  479. return S_OK;
  480. }
  481. BOOL WINAPI IsAdmDirty()
  482. {
  483. return g_fAdmDirty;
  484. }
  485. void WINAPI ResetAdmDirtyFlag()
  486. {
  487. g_fAdmDirty = FALSE;
  488. }
  489. STDAPI AdmSaveData(DWORD hAdm, LPVOID pPartData, LPVOID pCategoryData, DWORD dwFlags)
  490. {
  491. int i;
  492. int nControls = 0;
  493. LPPARTDATA pData = (LPPARTDATA) pPartData;
  494. LPADMUI pAdmUI = (LPADMUI) pCategoryData;
  495. if (pAdmUI != NULL && pAdmUI->pControlList != NULL)
  496. {
  497. for (i = 0; i < admFile[hAdm].nParts; i++)
  498. {
  499. if (StrCmpI(admFile[hAdm].pParts[i].szCategory, pAdmUI->pszCategory) == 0 &&
  500. admFile[hAdm].pParts[i].hkClass == pAdmUI->hKeyClass)
  501. {
  502. if (HasFlag(dwFlags, ADM_SAVE))
  503. pAdmUI->pControlList[nControls].Save(&(admFile[hAdm].pParts[i]), &pData[i]);
  504. if (HasFlag(dwFlags, ADM_DESTROY))
  505. pAdmUI->pControlList[nControls].Destroy();
  506. nControls++;
  507. }
  508. }
  509. if (HasFlag(dwFlags, ADM_DESTROY))
  510. {
  511. HeapFree(GetProcessHeap(), 0, pAdmUI->pControlList);
  512. pAdmUI->pControlList = NULL;
  513. pAdmUI->nControls = 0;
  514. }
  515. }
  516. return S_OK;
  517. }
  518. STDAPI GetFontInfoA(LPSTR pszFontName, LPINT pnFontSize)
  519. {
  520. TCHAR szFontName[LF_FACESIZE];
  521. getFontInfoHelper(szFontName, pnFontSize);
  522. T2Abuf(szFontName, pszFontName, LF_FACESIZE);
  523. return S_OK;
  524. }
  525. STDAPI GetFontInfoW(LPWSTR pwszFontName, LPINT pnFontSize)
  526. {
  527. TCHAR szFontName[LF_FACESIZE];
  528. getFontInfoHelper(szFontName, pnFontSize);
  529. T2Wbuf(szFontName, pwszFontName, LF_FACESIZE);
  530. return S_OK;
  531. }
  532. // ---------------------------------------------------------------------------
  533. // private helper functions
  534. static BOOL allocatePartData(DWORD hAdm, LPVOID* pData)
  535. {
  536. *pData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
  537. admFile[hAdm].nParts * sizeof(PARTDATA));
  538. if (*pData != NULL)
  539. {
  540. LPPARTDATA pPartData = (LPPARTDATA)*pData;
  541. for (int nPartIndex = 0; nPartIndex < admFile[hAdm].nParts; nPartIndex++)
  542. {
  543. if (admFile[hAdm].pParts[nPartIndex].nType != PART_LISTBOX)
  544. {
  545. if (admFile[hAdm].pParts[nPartIndex].szDefaultValue != NULL)
  546. pPartData[nPartIndex].value.szValue = StrDup(admFile[hAdm].pParts[nPartIndex].szDefaultValue);
  547. pPartData[nPartIndex].value.dwValue = admFile[hAdm].pParts[nPartIndex].nDefault;
  548. }
  549. }
  550. return TRUE;
  551. }
  552. return FALSE;
  553. }
  554. static HRESULT admInitHelper(LPCTSTR pcszAdmFile, LPCTSTR pcszInfFile,
  555. BSTR bstrNamespace, LPDWORD lpdwAdm, LPVOID* pData)
  556. {
  557. EnterCriticalSection(&g_hAdmCriticalSection);
  558. // loop thru the existing file list and check whether this file has been parsed
  559. for (int nIndex = 0; nIndex < g_nAdmInstances; nIndex++)
  560. {
  561. if (StrCmpI(admFile[nIndex].szFilename, pcszAdmFile) == 0)
  562. {
  563. *lpdwAdm = nIndex;
  564. allocatePartData(nIndex, pData);
  565. if (NULL != bstrNamespace && NULL != *pData)
  566. {
  567. ReadRegSettingsForADM(&admFile[nIndex], (LPPARTDATA)*pData,
  568. bstrNamespace);
  569. }
  570. else if((pcszInfFile != NULL) && ISNONNULL(pcszInfFile) && *pData != NULL)
  571. ReadInfFile(&admFile[nIndex], pcszInfFile, (LPPARTDATA)*pData);
  572. LeaveCriticalSection(&g_hAdmCriticalSection);
  573. return (*pData == NULL) ? E_FAIL : S_OK;
  574. }
  575. }
  576. int nNewInstance = AdmGetNewInstance();
  577. if (nNewInstance == -1)
  578. {
  579. LeaveCriticalSection(&g_hAdmCriticalSection);
  580. return E_FAIL;
  581. }
  582. if( !InitAdmParser(nNewInstance, pcszAdmFile))
  583. {
  584. ZeroMemory(&admFile[nNewInstance], sizeof(ADMFILE));
  585. LeaveCriticalSection(&g_hAdmCriticalSection);
  586. return E_FAIL;
  587. }
  588. // allocate the no. of parts buffer to pData
  589. allocatePartData(nNewInstance, pData);
  590. if (*pData == NULL)
  591. {
  592. FreeAdmMemory(&(admFile[nNewInstance]));
  593. ZeroMemory(&admFile[nNewInstance], sizeof(ADMFILE));
  594. LeaveCriticalSection(&g_hAdmCriticalSection);
  595. return E_FAIL;
  596. }
  597. if (NULL != bstrNamespace)
  598. {
  599. ReadRegSettingsForADM(&admFile[nIndex], (LPPARTDATA)*pData, bstrNamespace);
  600. }
  601. else if((pcszInfFile != NULL) && ISNONNULL(pcszInfFile))
  602. {
  603. ReadInfFile(&admFile[nNewInstance], pcszInfFile, (LPPARTDATA)*pData);
  604. }
  605. *lpdwAdm = nNewInstance;
  606. if (nNewInstance >= g_nAdmInstances)
  607. g_nAdmInstances = nNewInstance + 1;
  608. LeaveCriticalSection(&g_hAdmCriticalSection);
  609. return S_OK;
  610. }
  611. static HRESULT admFinishedHelper(DWORD hAdm, LPCTSTR pcszInfFile, LPVOID pPartData)
  612. {
  613. EnterCriticalSection(&g_hAdmCriticalSection);
  614. if(pcszInfFile != NULL && ISNONNULL(pcszInfFile))
  615. WriteInfFile(&admFile[hAdm], pcszInfFile, (LPPARTDATA)pPartData);
  616. LeaveCriticalSection(&g_hAdmCriticalSection);
  617. return S_OK;
  618. }
  619. static HRESULT createAdmUiHelper(DWORD hAdm, HWND hParent, int x, int y, int width, int height,
  620. DWORD dwStyle, DWORD dwExStyle, LPCTSTR pcszCategory, HKEY hKeyClass,
  621. HWND *phWnd, LPVOID pPartData, LPVOID* pCategoryData, BOOL fRSoPMode)
  622. {
  623. WNDCLASSA wc;
  624. HWND hWnd = NULL;
  625. ADMCREATE admCreate;
  626. EnterCriticalSection(&g_hAdmCriticalSection);
  627. wc.style = 0;
  628. wc.lpfnWndProc = AdmUiProc;
  629. wc.cbClsExtra = 0;
  630. wc.cbWndExtra = 0;
  631. wc.hInstance = g_hInst;
  632. wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  633. wc.hIcon = NULL;
  634. wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
  635. wc.lpszMenuName = NULL;
  636. wc.lpszClassName = "AdmUiClass";
  637. if( !FindAtomA( "AdmUiClass" ))
  638. {
  639. if( !RegisterClassA( &wc ))
  640. {
  641. LeaveCriticalSection(&g_hAdmCriticalSection);
  642. return E_FAIL;
  643. }
  644. AddAtomA( "AdmUiClass" );
  645. }
  646. LPADMUI pAdmUI = (LPADMUI)GlobalAlloc(GPTR, sizeof(ADMUI));
  647. pAdmUI->nAdmWidth = width;
  648. pAdmUI->nAdmHeight = height;
  649. pAdmUI->hKeyClass = hKeyClass;
  650. pAdmUI->pszCategory = StrDup(pcszCategory);
  651. pAdmUI->hWnd = hWnd;
  652. pAdmUI->hAdm = hAdm;
  653. admCreate.pAdmUI = pAdmUI;
  654. admCreate.pPartData = (LPPARTDATA)pPartData;
  655. if (fRSoPMode)
  656. admCreate.fRSoPMode = TRUE;
  657. else
  658. admCreate.fRSoPMode = FALSE;
  659. hWnd = CreateWindowExA( dwExStyle | WS_EX_CONTROLPARENT, "AdmUiClass", "", WS_CHILD | WS_HSCROLL | WS_VSCROLL | WS_GROUP | dwStyle,
  660. x, y, width, height, hParent,
  661. NULL, g_hInst, (LPVOID) &admCreate );
  662. if (hWnd == NULL)
  663. {
  664. GlobalFree(pAdmUI);
  665. LeaveCriticalSection(&g_hAdmCriticalSection);
  666. return E_FAIL;
  667. }
  668. *phWnd = hWnd;
  669. *pCategoryData = pAdmUI;
  670. LeaveCriticalSection(&g_hAdmCriticalSection);
  671. return S_OK;
  672. }
  673. static HRESULT getAdmCategoriesHelper(DWORD hAdm, LPTSTR pszCategories, int cchLength, int *nBytes)
  674. {
  675. EnterCriticalSection(&g_hAdmCriticalSection);
  676. int nCopyIndex = 0;
  677. int i;
  678. HKEY hKeyCurrentClass = HKEY_CURRENT_USER;
  679. TCHAR szKey[10];
  680. ZeroMemory(pszCategories, cchLength * sizeof(TCHAR));
  681. ZeroMemory(szKey, sizeof(szKey));
  682. hKeyCurrentClass = admFile[hAdm].pParts[0].hkClass;
  683. if(hKeyCurrentClass == HKEY_LOCAL_MACHINE)
  684. StrCpy(szKey, TEXT("HKLM"));
  685. else
  686. StrCpy(szKey, TEXT("HKCU"));
  687. StrCpy(pszCategories, szKey);
  688. nCopyIndex = lstrlen(szKey) + 1;
  689. CopyMemory(pszCategories + nCopyIndex, admFile[hAdm].pParts[0].szCategory, lstrlen( admFile[hAdm].pParts[0].szCategory ) * sizeof(TCHAR));
  690. nCopyIndex += lstrlen( admFile[hAdm].pParts[0].szCategory );
  691. nCopyIndex++;
  692. for( i = 0; i < admFile[hAdm].nParts - 1; i++ )
  693. {
  694. if( StrCmpI( admFile[hAdm].pParts[i].szCategory, admFile[hAdm].pParts[i+1].szCategory ) != 0 )
  695. {
  696. if(admFile[hAdm].pParts[i+1].hkClass != hKeyCurrentClass)
  697. {
  698. hKeyCurrentClass = admFile[hAdm].pParts[i+1].hkClass;
  699. if(hKeyCurrentClass == HKEY_LOCAL_MACHINE)
  700. StrCpy(szKey, TEXT("HKLM"));
  701. else
  702. StrCpy(szKey, TEXT("HKCU"));
  703. CopyMemory(pszCategories + nCopyIndex, szKey, lstrlen( szKey ) * sizeof(TCHAR));
  704. nCopyIndex += lstrlen(szKey);
  705. // skip over one byte so our list is 0 separated
  706. nCopyIndex++;
  707. }
  708. CopyMemory(pszCategories + nCopyIndex, admFile[hAdm].pParts[i+1].szCategory, lstrlen( admFile[hAdm].pParts[i+1].szCategory ) * sizeof(TCHAR));
  709. nCopyIndex += lstrlen( admFile[hAdm].pParts[i+1].szCategory );
  710. // skip over one byte so our list is 0 separated
  711. nCopyIndex++;
  712. // make sure we still have enough room in the buffer
  713. if( nCopyIndex > cchLength )
  714. {
  715. LeaveCriticalSection(&g_hAdmCriticalSection);
  716. return E_FAIL;
  717. }
  718. }
  719. }
  720. *nBytes = nCopyIndex;
  721. LeaveCriticalSection(&g_hAdmCriticalSection);
  722. return S_OK;
  723. }
  724. static HRESULT checkDuplicateKeysHelper(DWORD hAdm, DWORD hCompareAdm, LPCTSTR pcszLogFile, BOOL bClearFile)
  725. {
  726. HANDLE hFile = NULL;
  727. DWORD dwCreationDisposition = CREATE_ALWAYS;
  728. DWORD dwNumberOfBytesWritten = 0;
  729. TCHAR szBuffer[1024];
  730. int nCheckIndex = 0;
  731. int nIndex = 0;
  732. DWORD admHandle = 0;
  733. LPTSTR pData = NULL;
  734. int nData = 0;
  735. int nSize = 1024;
  736. LPVOID lpTemp = NULL;
  737. BOOL bContinue = TRUE;
  738. pData = (LPTSTR) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, StrCbFromCch(nSize));
  739. if(pData == NULL)
  740. {
  741. SetLastError(STATUS_NO_MEMORY);
  742. return E_FAIL;
  743. }
  744. if(bClearFile == FALSE)
  745. dwCreationDisposition = OPEN_ALWAYS;
  746. hFile = CreateFile(pcszLogFile, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
  747. NULL, dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
  748. if( hFile == INVALID_HANDLE_VALUE )
  749. {
  750. SetLastError( ERROR_FILE_NOT_FOUND );
  751. return E_FAIL;
  752. }
  753. if(bClearFile == FALSE)
  754. SetFilePointer(hFile, 0, NULL, FILE_END);
  755. LoadString(g_hInst, IDS_DUPKEY_FORMAT, szBuffer, countof(szBuffer));
  756. admHandle = hCompareAdm;
  757. nCheckIndex = 0;
  758. while( nCheckIndex < admFile[hAdm].nParts && bContinue)
  759. {
  760. if(admFile[hAdm].pParts[nCheckIndex].nType == PART_TEXT ||
  761. (admFile[hAdm].pParts[nCheckIndex].nType == PART_POLICY &&
  762. !admFile[hAdm].pParts[nCheckIndex].fRequired) ||
  763. admFile[hAdm].pParts[nCheckIndex].nType == PART_ERROR ||
  764. admFile[hAdm].pParts[nCheckIndex].value.szValueName == NULL)
  765. {
  766. nCheckIndex++;
  767. continue;
  768. }
  769. if(hAdm == admHandle)
  770. nIndex = nCheckIndex + 1;
  771. else
  772. nIndex = 0;
  773. for(; nIndex < admFile[admHandle].nParts && bContinue; nIndex++ )
  774. {
  775. if(admFile[admHandle].pParts[nIndex].nType == PART_POLICY && !admFile[admHandle].pParts[nCheckIndex].fRequired)
  776. continue;
  777. if(admFile[admHandle].pParts[nIndex].nType != PART_TEXT &&
  778. admFile[admHandle].pParts[nIndex].nType != PART_ERROR &&
  779. admFile[hAdm].pParts[nCheckIndex].hkClass == admFile[admHandle].pParts[nIndex].hkClass &&
  780. admFile[admHandle].pParts[nIndex].value.szValueName != NULL &&
  781. !StrCmpI(admFile[hAdm].pParts[nCheckIndex].value.szValueName, admFile[admHandle].pParts[nIndex].value.szValueName) &&
  782. !StrCmpI(admFile[hAdm].pParts[nCheckIndex].value.szKeyname, admFile[admHandle].pParts[nIndex].value.szKeyname))
  783. {
  784. LPTSTR pStr = NULL;
  785. USES_CONVERSION;
  786. pStr = FormatString(szBuffer,
  787. BaseFileName(admFile[hAdm].szFilename), admFile[hAdm].pParts[nCheckIndex].nLine,admFile[hAdm].pParts[nCheckIndex].szName,
  788. BaseFileName(admFile[admHandle].szFilename), admFile[admHandle].pParts[nIndex].nLine, admFile[admHandle].pParts[nIndex].szName);
  789. if((nData + lstrlen(pStr) + 1) > (nSize - 1))
  790. {
  791. nSize += 1024;
  792. lpTemp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pData, StrCbFromCch(nSize));
  793. if(lpTemp == NULL)
  794. bContinue = FALSE;
  795. else
  796. pData = (LPTSTR) lpTemp;
  797. }
  798. StrCat(pData, pStr);
  799. nData += lstrlen(pStr);
  800. LocalFree(pStr);
  801. }
  802. }
  803. nCheckIndex++;
  804. }
  805. if(ISNONNULL(pData))
  806. {
  807. LPSTR pszData;
  808. pszData = (LPSTR)CoTaskMemAlloc(StrCbFromCch(nData));
  809. T2Abuf(pData, pszData, nData);
  810. WriteFile(hFile, pszData, nData, &dwNumberOfBytesWritten, NULL);
  811. CoTaskMemFree(pszData);
  812. }
  813. CloseHandle(hFile);
  814. HeapFree(GetProcessHeap(), 0, pData);
  815. return S_OK;
  816. }
  817. static HRESULT admResetHelper(DWORD hAdm, LPCTSTR pcszInfFile, LPVOID pPartData, LPVOID pCategoryData)
  818. {
  819. EnterCriticalSection(&g_hAdmCriticalSection);
  820. LPPARTDATA pData = (LPPARTDATA) pPartData;
  821. LPADMUI pAdmUI = (LPADMUI) pCategoryData;
  822. for(int nPartIndex = 0; nPartIndex < admFile[hAdm].nParts; nPartIndex++)
  823. {
  824. if(pData[nPartIndex].value.szValue != NULL)
  825. {
  826. LocalFree(pData[nPartIndex].value.szValue);
  827. pData[nPartIndex].value.szValue = NULL;
  828. }
  829. if (admFile[hAdm].pParts[nPartIndex].szDefaultValue != NULL)
  830. pData[nPartIndex].value.szValue = StrDup(admFile[hAdm].pParts[nPartIndex].szDefaultValue);
  831. pData[nPartIndex].value.dwValue = admFile[hAdm].pParts[nPartIndex].nDefault;
  832. pData[nPartIndex].value.fNumeric = 0;
  833. pData[nPartIndex].fSave = 0;
  834. if (pData[nPartIndex].nActions != 0)
  835. {
  836. FreeActionList(pData[nPartIndex].actionlist, pData[nPartIndex].nActions);
  837. if (pData[nPartIndex].actionlist != NULL)
  838. {
  839. HeapFree(GetProcessHeap(), 0, pData[nPartIndex].actionlist);
  840. pData[nPartIndex].actionlist = NULL;
  841. }
  842. pData[nPartIndex].nActions = 0;
  843. }
  844. }
  845. if(pcszInfFile != NULL && ISNONNULL(pcszInfFile))
  846. ReadInfFile(&admFile[hAdm], pcszInfFile, pData);
  847. if(pAdmUI != NULL && pAdmUI->pControlList != NULL)
  848. {
  849. for(int nControlIndex = 0; nControlIndex < pAdmUI->nControls; nControlIndex++)
  850. {
  851. int nPartIndex = pAdmUI->pControlList[nControlIndex].GetPart();
  852. pAdmUI->pControlList[nControlIndex].Reset(&(admFile[hAdm].pParts[nPartIndex]),
  853. &pData[nPartIndex]);
  854. }
  855. }
  856. LeaveCriticalSection(&g_hAdmCriticalSection);
  857. return S_OK;
  858. }
  859. static void getFontInfoHelper(LPTSTR pszFontName, LPINT pnFontSize)
  860. {
  861. TCHAR szFontSize[8];
  862. if (!LoadString(g_hInst, IDS_ADMBOLDFONT, pszFontName, LF_FACESIZE))
  863. StrCpy(pszFontName, TEXT("MS Sans Serif"));
  864. LoadString(g_hInst, IDS_ADMBOLDFONTSIZE, szFontSize, ARRAYSIZE(szFontSize));
  865. *pnFontSize = StrToInt(szFontSize);
  866. if (*pnFontSize < 8)
  867. *pnFontSize = 8;
  868. }