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.

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