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.

325 lines
10 KiB

  1. #include "precomp.h"
  2. #include <ntverp.h> //these are for
  3. #include <common.ver> //ver_productversion_str
  4. // private forward declarations
  5. #define LOCK_FILENAME TEXT("LOCK")
  6. #define MAX_LOCK_SPINS 10 // number of timeouts before we give up
  7. #define LOCK_TIMEOUT 2000 // in milliseconds
  8. static inline LPPROPSHEETCOOKIE getPropSheetCookie(HWND hDlg);
  9. int SIEErrorMessageBox(HWND hDlg, UINT idErrStr, UINT uiFlags /* = 0 */)
  10. {
  11. static TCHAR s_szTitle[128];
  12. TCHAR szMessage[MAX_PATH];
  13. if (ISNULL(s_szTitle))
  14. LoadString(g_hInstance, IDS_SIE_NAME, s_szTitle, countof(s_szTitle));
  15. if (LoadString(g_hInstance, idErrStr, szMessage, countof(szMessage)) == 0)
  16. LoadString(g_hUIInstance, idErrStr, szMessage, countof(szMessage));
  17. return MessageBox(hDlg, szMessage, s_szTitle, uiFlags ? uiFlags : MB_OK | MB_SETFOREGROUND | MB_ICONEXCLAMATION);
  18. }
  19. void CreateWorkDir(LPCTSTR pcszInsFile, LPCTSTR pcszFeatureDir, LPTSTR pszWorkDir,
  20. LPCTSTR pcszCabDir /* = NULL */, BOOL fCreate /* = TRUE */)
  21. {
  22. StrCpy(pszWorkDir, pcszInsFile);
  23. PathRemoveFileSpec(pszWorkDir);
  24. if (pcszCabDir != NULL)
  25. PathAppend(pszWorkDir, pcszCabDir);
  26. PathAppend(pszWorkDir, pcszFeatureDir);
  27. if (fCreate)
  28. {
  29. if (!PathFileExists(pszWorkDir))
  30. PathCreatePath(pszWorkDir);
  31. }
  32. }
  33. UINT CALLBACK PropSheetPageProc(HWND hwnd, UINT uMsg, LPPROPSHEETPAGE ppsp)
  34. {
  35. UNREFERENCED_PARAMETER(hwnd);
  36. if ((uMsg == PSPCB_RELEASE) && (ppsp->lParam != NULL))
  37. {
  38. CoTaskMemFree((LPVOID)ppsp->lParam);
  39. ppsp->lParam = NULL;
  40. }
  41. return TRUE;
  42. }
  43. void SetPropSheetCookie(HWND hDlg, LPARAM lParam)
  44. {
  45. LPPROPSHEETCOOKIE lpPropSheetCookie = (LPPROPSHEETCOOKIE)(((LPPROPSHEETPAGE)lParam)->lParam);
  46. TCHAR szTitle[MAX_PATH];
  47. INT iNamePrefID = lpPropSheetCookie->lpResultItem->iNamePrefID;
  48. BOOL fPrefTitle = (!InsIsKeyEmpty(IS_BRANDING, IK_GPE_ONETIME_GUID, lpPropSheetCookie->pCS->GetInsFile())) &&
  49. (iNamePrefID != -1);
  50. SetWindowLongPtr(hDlg, DWLP_USER, (LONG_PTR)lpPropSheetCookie);
  51. // overload the page title to get rid of the "properties" suffix
  52. if (LoadString(g_hUIInstance, fPrefTitle ?
  53. iNamePrefID : lpPropSheetCookie->lpResultItem->iNameID,
  54. szTitle, countof(szTitle)) != 0)
  55. SetWindowText(GetParent(hDlg), szTitle);
  56. }
  57. LPCTSTR GetInsFile(LPVOID lpVoid)
  58. {
  59. LPPROPSHEETCOOKIE lpPropSheetCookie = getPropSheetCookie((HWND)lpVoid);
  60. return lpPropSheetCookie->pCS->GetInsFile();
  61. }
  62. void ShowHelpTopic(LPVOID lpVoid)
  63. {
  64. LPPROPSHEETCOOKIE lpPropSheetCookie = getPropSheetCookie((HWND)lpVoid);
  65. if (lpPropSheetCookie && lpPropSheetCookie->lpResultItem)
  66. {
  67. WCHAR wszHelpTopic[MAX_PATH];
  68. StrCpyW(wszHelpTopic, HELP_FILENAME TEXT("::/"));
  69. StrCatW(wszHelpTopic, lpPropSheetCookie->lpResultItem->pcszHelpTopic);
  70. MMCPropertyHelp((LPOLESTR)wszHelpTopic);
  71. }
  72. }
  73. BOOL AcquireWriteCriticalSection(HWND hDlg, CComponentData * pCDCurrent /* = NULL */,
  74. BOOL fCreateCookie /* = TRUE */)
  75. {
  76. CComponentData * pCD;
  77. TCHAR szLockFile[MAX_PATH];
  78. LPTSTR pszLockName;
  79. HANDLE hLock;
  80. DWORD dwRet;
  81. BOOL fRet = TRUE;
  82. if (hDlg != NULL)
  83. {
  84. LPPROPSHEETCOOKIE lpPropSheetCookie = getPropSheetCookie(hDlg);
  85. pCD = lpPropSheetCookie->pCS->GetCompData();
  86. }
  87. else
  88. if (pCDCurrent)
  89. pCD = pCDCurrent;
  90. else
  91. {
  92. fRet = false;
  93. goto exit;
  94. }
  95. StrCpy(szLockFile, pCD->GetInsFile());
  96. PathRemoveFileSpec(szLockFile);
  97. PathAppend(szLockFile, TEXT("LOCK"));
  98. PathCreatePath(szLockFile);
  99. pszLockName = PathAddBackslash(szLockFile);
  100. StrCpy(pszLockName, LOCK_FILENAME);
  101. // (pritobla): Delete the lock file before acquiring it.
  102. // If someone else has acquired it, the delete would fail, which is correct behavior.
  103. // If the file was left behind (probably because the machine crashed the last time the file was created),
  104. // then the delete would succeed and subsequently, the CreateFile would succeed.
  105. DeleteFile(szLockFile);
  106. hLock = CreateFile(szLockFile, GENERIC_WRITE, 0, NULL, CREATE_NEW,
  107. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL);
  108. if (hLock == INVALID_HANDLE_VALUE)
  109. {
  110. HANDLE hNotify;
  111. DWORD dwSpins = 0;
  112. *pszLockName = TEXT('\0');
  113. hNotify = FindFirstChangeNotification(szLockFile, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME);
  114. if (hNotify == INVALID_HANDLE_VALUE)
  115. {
  116. fRet = FALSE;
  117. goto exit;
  118. }
  119. StrCpy(pszLockName, LOCK_FILENAME);
  120. do
  121. {
  122. while (((dwRet = MsgWaitForMultipleObjects(1, &hNotify, FALSE, LOCK_TIMEOUT, QS_ALLINPUT)) != WAIT_OBJECT_0) &&
  123. (dwRet != WAIT_TIMEOUT))
  124. {
  125. MSG msg;
  126. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  127. {
  128. TranslateMessage(&msg);
  129. DispatchMessage(&msg);
  130. }
  131. }
  132. dwSpins++;
  133. hLock = CreateFile(szLockFile, GENERIC_WRITE, 0, NULL, CREATE_NEW,
  134. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL);
  135. if ((hLock != INVALID_HANDLE_VALUE) || (dwSpins == MAX_LOCK_SPINS))
  136. break;
  137. }
  138. while (FindNextChangeNotification(hNotify));
  139. if (!FindCloseChangeNotification(hNotify) || (hLock == INVALID_HANDLE_VALUE))
  140. {
  141. if (hLock != INVALID_HANDLE_VALUE)
  142. CloseHandle(hLock);
  143. fRet = FALSE;
  144. goto exit;
  145. }
  146. }
  147. pCD->SetLockHandle(hLock);
  148. if (fCreateCookie)
  149. {
  150. // now that we have the lock, create our cookie file in our root GPO dir
  151. *(pszLockName-1) = TEXT('\0');
  152. PathRemoveFileSpec(szLockFile);
  153. PathAppend(szLockFile, IEAK_GPE_COOKIE_FILE);
  154. hLock = CreateFile(szLockFile, GENERIC_WRITE, 0, NULL, CREATE_NEW,
  155. FILE_ATTRIBUTE_NORMAL, NULL);
  156. if (hLock != INVALID_HANDLE_VALUE)
  157. CloseHandle(hLock);
  158. else
  159. {
  160. ASSERT(FALSE);
  161. }
  162. }
  163. exit:
  164. if (!fRet)
  165. {
  166. TCHAR szTitle[MAX_PATH], szMsg[MAX_PATH];
  167. LoadString(g_hInstance, IDS_SIE_NAME, szTitle, countof(szTitle));
  168. LoadString(g_hInstance, IDS_ERROR_SAVE, szMsg, countof(szMsg));
  169. MessageBox(hDlg, szMsg, szTitle, MB_OK | MB_SETFOREGROUND | MB_ICONEXCLAMATION);
  170. }
  171. return fRet;
  172. }
  173. void ReleaseWriteCriticalSection(CComponentData * pCD, BOOL fDeleteCookie, BOOL fApplyPolicy,
  174. BOOL bMachine /* = FALSE */, BOOL bAdd /* = FALSE */,
  175. GUID *pGuidExtension /* = NULL */, GUID *pGuidSnapin /* = NULL */)
  176. {
  177. HANDLE hLock;
  178. if (fDeleteCookie)
  179. {
  180. TCHAR szCookieFile[MAX_PATH];
  181. StrCpy(szCookieFile, pCD->GetInsFile());
  182. PathRemoveFileSpec(szCookieFile);
  183. PathAppend(szCookieFile, IEAK_GPE_COOKIE_FILE);
  184. DeleteFile(szCookieFile);
  185. if (fApplyPolicy)
  186. pCD->SignalPolicyChanged(bMachine, bAdd, pGuidExtension, pGuidSnapin);
  187. }
  188. if ((hLock = pCD->GetLockHandle()) != INVALID_HANDLE_VALUE)
  189. {
  190. pCD->SetLockHandle(INVALID_HANDLE_VALUE);
  191. CloseHandle(hLock);
  192. }
  193. else
  194. {
  195. ASSERT(FALSE);
  196. }
  197. }
  198. void SignalPolicyChanged(HWND hDlg, BOOL bMachine, BOOL bAdd, GUID *pGuidExtension,
  199. GUID *pGuidSnapin, BOOL fAdvanced /* = FALSE */)
  200. {
  201. LPPROPSHEETCOOKIE lpPropSheetCookie = getPropSheetCookie(hDlg);
  202. TCHAR szCookieFile[MAX_PATH];
  203. TCHAR szGuid[128];
  204. GUID guid;
  205. USES_CONVERSION;
  206. StrCpy(szCookieFile, lpPropSheetCookie->pCS->GetInsFile());
  207. WritePrivateProfileString(BRANDING, GPVERKEY, A2CT(VER_PRODUCTVERSION_STR), szCookieFile);
  208. //clear other keys so we're sure this is GP
  209. WritePrivateProfileString(BRANDING, PMVERKEY, NULL, szCookieFile);
  210. WritePrivateProfileString(BRANDING, IK_WIZVERSION, NULL, szCookieFile);
  211. // write out a new guid for one time branding to the ins file if there was already one
  212. // there to signify apply only once is checked
  213. if (!InsIsKeyEmpty(IS_BRANDING, IK_GPE_ONETIME_GUID, szCookieFile))
  214. {
  215. if (CoCreateGuid(&guid) == NOERROR)
  216. CoStringFromGUID(guid, szGuid, countof(szGuid));
  217. else
  218. szGuid[64] = TEXT('\0');
  219. InsWriteString(IS_BRANDING, IK_GPE_ONETIME_GUID, szGuid, szCookieFile);
  220. }
  221. // write out a separate guid to track adms since they are always preferences
  222. if (fAdvanced)
  223. {
  224. if (CoCreateGuid(&guid) == NOERROR)
  225. CoStringFromGUID(guid, szGuid, countof(szGuid));
  226. else
  227. szGuid[64] = TEXT('\0');
  228. InsWriteString(IS_BRANDING, IK_GPE_ADM_GUID, szGuid, szCookieFile);
  229. }
  230. InsFlushChanges(szCookieFile);
  231. ReleaseWriteCriticalSection(lpPropSheetCookie->pCS->GetCompData(), TRUE, TRUE, bMachine, bAdd,
  232. pGuidExtension, pGuidSnapin);
  233. }
  234. LPCTSTR GetCurrentAdmFile(LPVOID lpVoid)
  235. {
  236. LPPROPSHEETCOOKIE lpPropSheetCookie = getPropSheetCookie((HWND)lpVoid);
  237. return lpPropSheetCookie->lpResultItem->pszDesc;
  238. }
  239. LPTSTR res2Str(int nIDString, LPTSTR pszBuffer, UINT cbBuffer)
  240. {
  241. if (pszBuffer == NULL || cbBuffer == 0)
  242. return NULL;
  243. *pszBuffer = TEXT('\0');
  244. if (LoadString(g_hInstance, nIDString, pszBuffer, cbBuffer) == 0)
  245. LoadString(g_hUIInstance, nIDString, pszBuffer, cbBuffer);
  246. return pszBuffer;
  247. }
  248. // private helper APIs for this file
  249. static inline LPPROPSHEETCOOKIE getPropSheetCookie(HWND hDlg)
  250. {
  251. return (LPPROPSHEETCOOKIE)GetWindowLongPtr(hDlg, DWLP_USER);
  252. }