Source code of Windows XP (NT5)
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.

476 lines
9.9 KiB

  1. //Copyright (c) 1998 - 1999 Microsoft Corporation
  2. /*
  3. *
  4. * Util.cpp
  5. *
  6. * Utility routines.
  7. *
  8. */
  9. //
  10. // Includes
  11. //
  12. #include "stdafx.h"
  13. #include "hydraoc.h"
  14. //
  15. // Globals
  16. //
  17. HINSTANCE ghInstance = NULL;
  18. PEXTRA_ROUTINES gpExtraRoutines = NULL;
  19. // PSETUP_INIT_COMPONENT gpInitComponentData = NULL;
  20. //
  21. // Function Definitions
  22. //
  23. VOID
  24. DestroyExtraRoutines(
  25. VOID
  26. )
  27. {
  28. if (gpExtraRoutines != NULL) {
  29. LocalFree(gpExtraRoutines);
  30. }
  31. }
  32. BOOL
  33. DoMessageBox(
  34. UINT uiMsg
  35. )
  36. {
  37. TCHAR strMsg[1024];
  38. TCHAR strTitle[1024];
  39. ASSERT(!StateObject.IsUnattended());
  40. if ((LoadString(GetInstance(), IDS_STRING_MESSAGE_BOX_TITLE, strTitle, 1024) != 0) &&
  41. (LoadString(GetInstance(), uiMsg, strMsg, 1024) != 0))
  42. {
  43. MessageBox(
  44. GetHelperRoutines().QueryWizardDialogHandle(GetHelperRoutines().OcManagerContext),
  45. strMsg,
  46. strTitle,
  47. MB_OK
  48. );
  49. return(TRUE);
  50. }
  51. return(FALSE);
  52. }
  53. HINF
  54. GetComponentInfHandle(
  55. VOID
  56. )
  57. {
  58. if (INVALID_HANDLE_VALUE == GetSetupData()->ComponentInfHandle) {
  59. return(NULL);
  60. } else {
  61. return(GetSetupData()->ComponentInfHandle);
  62. }
  63. }
  64. EXTRA_ROUTINES
  65. GetExtraRoutines(
  66. VOID
  67. )
  68. {
  69. return(*gpExtraRoutines);
  70. }
  71. OCMANAGER_ROUTINES
  72. GetHelperRoutines(
  73. VOID
  74. )
  75. {
  76. return(GetSetupData()->HelperRoutines);
  77. }
  78. HINSTANCE
  79. GetInstance(
  80. VOID
  81. )
  82. {
  83. ASSERT(ghInstance);
  84. return(ghInstance);
  85. }
  86. HINF
  87. GetUnAttendedInfHandle(
  88. VOID
  89. )
  90. {
  91. ASSERT(StateObject.IsUnattended());
  92. return(GetHelperRoutines().GetInfHandle(INFINDEX_UNATTENDED,GetHelperRoutines().OcManagerContext));
  93. }
  94. VOID
  95. LogErrorToEventLog(
  96. WORD wType,
  97. WORD wCategory,
  98. DWORD dwEventId,
  99. WORD wNumStrings,
  100. DWORD dwDataSize,
  101. LPCTSTR *lpStrings,
  102. LPVOID lpRawData
  103. )
  104. {
  105. HANDLE hEventLog;
  106. hEventLog = RegisterEventSource(NULL, TS_EVENT_SOURCE);
  107. if (hEventLog != NULL) {
  108. if (!ReportEvent(
  109. hEventLog,
  110. wType,
  111. wCategory,
  112. dwEventId,
  113. NULL,
  114. wNumStrings,
  115. dwDataSize,
  116. lpStrings,
  117. lpRawData
  118. )) {
  119. LOGMESSAGE1(_T("ReportEvent failed %ld"), GetLastError());
  120. }
  121. DeregisterEventSource(hEventLog);
  122. } else {
  123. LOGMESSAGE1(_T("RegisterEventSource failed %ld"), GetLastError());
  124. return;
  125. }
  126. }
  127. VOID
  128. LogErrorToSetupLog(
  129. OcErrorLevel ErrorLevel,
  130. UINT uiMsg,
  131. ...
  132. )
  133. {
  134. TCHAR szFormat[1024];
  135. TCHAR szOutput[1024];
  136. if (LoadString(GetInstance(), uiMsg, szFormat, 1024) != 0) {
  137. va_list vaList;
  138. va_start(vaList, uiMsg);
  139. _vstprintf(szOutput, szFormat, vaList);
  140. va_end(vaList);
  141. GetExtraRoutines().LogError(
  142. GetHelperRoutines().OcManagerContext,
  143. ErrorLevel,
  144. szOutput
  145. );
  146. }
  147. }
  148. VOID
  149. SetInstance(
  150. HINSTANCE hInstance
  151. )
  152. {
  153. ghInstance = hInstance;
  154. }
  155. VOID
  156. SetProgressText(
  157. UINT uiMsg
  158. )
  159. {
  160. TCHAR strMsg[1024];
  161. if (LoadString(GetInstance(), uiMsg, strMsg, 1024) != 0) {
  162. GetHelperRoutines().SetProgressText(GetHelperRoutines().OcManagerContext, strMsg);
  163. }
  164. }
  165. BOOL
  166. SetReboot(
  167. VOID
  168. )
  169. {
  170. return(GetHelperRoutines().SetReboot(GetHelperRoutines().OcManagerContext, 0));
  171. }
  172. BOOL
  173. SetExtraRoutines(
  174. PEXTRA_ROUTINES pExtraRoutines
  175. )
  176. {
  177. if (pExtraRoutines->size != sizeof(EXTRA_ROUTINES)) {
  178. LOGMESSAGE0(_T("WARNING: Extra Routines are a different size than expected!"));
  179. }
  180. gpExtraRoutines = (PEXTRA_ROUTINES)LocalAlloc(LPTR, pExtraRoutines->size);
  181. if (gpExtraRoutines == NULL) {
  182. return(FALSE);
  183. }
  184. CopyMemory(gpExtraRoutines, pExtraRoutines, pExtraRoutines->size);
  185. return(TRUE);
  186. }
  187. BOOL Delnode( IN LPCTSTR Directory )
  188. {
  189. TCHAR szDirectory[MAX_PATH + 1];
  190. TCHAR szPattern[MAX_PATH + 1];
  191. WIN32_FIND_DATA FindData;
  192. HANDLE FindHandle;
  193. LOGMESSAGE0(_T("Delnode: Entered"));
  194. //
  195. // Delete each file in the given directory, then remove the directory
  196. // itself. If any directories are encountered along the way recurse to
  197. // delete them as they are encountered.
  198. //
  199. // Start by forming the search pattern, which is <currentdir>\*.
  200. //
  201. ExpandEnvironmentStrings(Directory, szDirectory, MAX_PATH);
  202. LOGMESSAGE1(_T("Delnode: Deleting %s"), szDirectory);
  203. _tcscpy(szPattern, szDirectory);
  204. _tcscat(szPattern, _T("\\"));
  205. _tcscat(szPattern, _T("*"));
  206. //
  207. // Start the search.
  208. //
  209. FindHandle = FindFirstFile(szPattern, &FindData);
  210. if(FindHandle != INVALID_HANDLE_VALUE)
  211. {
  212. do
  213. {
  214. //
  215. // Form the full name of the file or directory we just found.
  216. //
  217. _tcscpy(szPattern, szDirectory);
  218. _tcscat(szPattern, _T("\\"));
  219. _tcscat(szPattern, FindData.cFileName);
  220. //
  221. // Remove read-only atttribute if it's there.
  222. //
  223. if (FindData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
  224. {
  225. SetFileAttributes(szPattern, FILE_ATTRIBUTE_NORMAL);
  226. }
  227. if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  228. {
  229. //
  230. // The current match is a directory. Recurse into it unless
  231. // it's . or ...
  232. //
  233. if ((_tcsicmp(FindData.cFileName,_T("."))) &&
  234. (_tcsicmp(FindData.cFileName,_T(".."))))
  235. {
  236. if (!Delnode(szPattern))
  237. {
  238. LOGMESSAGE1(_T("DelNode failed on %s"), szPattern);
  239. }
  240. }
  241. }
  242. else
  243. {
  244. //
  245. // The current match is not a directory -- so delete it.
  246. //
  247. if (!DeleteFile(szPattern))
  248. {
  249. LOGMESSAGE2(_T("Delnode: %s not deleted: %d"), szPattern, GetLastError());
  250. }
  251. }
  252. }
  253. while(FindNextFile(FindHandle, &FindData));
  254. FindClose(FindHandle);
  255. }
  256. //
  257. // Remove the directory we just emptied out. Ignore errors.
  258. //
  259. if (!RemoveDirectory(szDirectory))
  260. {
  261. LOGMESSAGE2(_T("Failed to remove the directory %s (%d)"), szDirectory, GetLastError());
  262. return FALSE;
  263. }
  264. return TRUE;
  265. }
  266. //+--------------------------------------------------------------------------
  267. //
  268. // Function: StoreSecretKey
  269. //
  270. // Synopsis: stores a key in the LSA
  271. //
  272. // Arguments: [pwszKeyName] -- the license server
  273. // [pbKey] -- the product id to add license on
  274. // [cbKey] -- the key pack type to add the license to
  275. //
  276. // Returns: returns a WinError code
  277. //
  278. // History: September 17, 1998 - created [hueiwang]
  279. //
  280. // Notes:
  281. //
  282. //---------------------------------------------------------------------------
  283. DWORD StoreSecretKey(PWCHAR pwszKeyName, BYTE * pbKey, DWORD cbKey )
  284. {
  285. LSA_HANDLE PolicyHandle;
  286. UNICODE_STRING SecretKeyName;
  287. UNICODE_STRING SecretData;
  288. DWORD Status;
  289. if( ( NULL == pwszKeyName ) )
  290. {
  291. return( ERROR_INVALID_PARAMETER );
  292. }
  293. //
  294. // setup the UNICODE_STRINGs for the call.
  295. //
  296. InitLsaString( &SecretKeyName, pwszKeyName );
  297. SecretData.Buffer = ( LPWSTR )pbKey;
  298. SecretData.Length = ( USHORT )cbKey;
  299. SecretData.MaximumLength = ( USHORT )cbKey;
  300. Status = OpenPolicy( NULL, POLICY_CREATE_SECRET, &PolicyHandle );
  301. if( Status != ERROR_SUCCESS )
  302. {
  303. return LsaNtStatusToWinError(Status);
  304. }
  305. Status = LsaStorePrivateData(
  306. PolicyHandle,
  307. &SecretKeyName,
  308. pbKey ? &SecretData : NULL
  309. );
  310. LsaClose(PolicyHandle);
  311. return LsaNtStatusToWinError(Status);
  312. }
  313. //+--------------------------------------------------------------------------
  314. //
  315. // Function: OpenPolicy
  316. //
  317. // Synopsis: opens a policy of the LSA component
  318. //
  319. // Arguments: [ServerName] -- server
  320. // [DesiredAccess] --
  321. // [PociyHandle] --
  322. //
  323. // Returns: returns nt error code
  324. //
  325. // History: September 17, 1998 - created [hueiwang]
  326. //
  327. // Notes:
  328. //
  329. //---------------------------------------------------------------------------
  330. DWORD OpenPolicy(LPWSTR ServerName,DWORD DesiredAccess,PLSA_HANDLE PolicyHandle )
  331. {
  332. LSA_OBJECT_ATTRIBUTES ObjectAttributes;
  333. LSA_UNICODE_STRING ServerString;
  334. PLSA_UNICODE_STRING Server;
  335. //
  336. // Always initialize the object attributes to all zeroes.
  337. //
  338. ZeroMemory( &ObjectAttributes, sizeof( ObjectAttributes ) );
  339. if( NULL != ServerName )
  340. {
  341. //
  342. // Make a LSA_UNICODE_STRING out of the LPWSTR passed in
  343. //
  344. InitLsaString( &ServerString, ServerName );
  345. Server = &ServerString;
  346. }
  347. else
  348. {
  349. Server = NULL;
  350. }
  351. //
  352. // Attempt to open the policy.
  353. //
  354. return( LsaOpenPolicy(
  355. Server,
  356. &ObjectAttributes,
  357. DesiredAccess,
  358. PolicyHandle ) );
  359. }
  360. //+--------------------------------------------------------------------------
  361. //
  362. // Function: InitLsaString
  363. //
  364. // Synopsis: initializes LSA string
  365. //
  366. // Arguments: [LsaString] --
  367. // [String] --
  368. //
  369. // Returns: void
  370. //
  371. // History: September 17, 1998 - created [hueiwang]
  372. //
  373. // Notes:
  374. //
  375. //---------------------------------------------------------------------------
  376. void InitLsaString(PLSA_UNICODE_STRING LsaString,LPWSTR String )
  377. {
  378. DWORD StringLength;
  379. if( NULL == String )
  380. {
  381. LsaString->Buffer = NULL;
  382. LsaString->Length = 0;
  383. LsaString->MaximumLength = 0;
  384. return;
  385. }
  386. StringLength = lstrlenW( String );
  387. LsaString->Buffer = String;
  388. LsaString->Length = ( USHORT ) (StringLength * sizeof( WCHAR ));
  389. LsaString->MaximumLength=( USHORT ) (( StringLength + 1 ) * sizeof( WCHAR ));
  390. }