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.

1485 lines
41 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. CONFIG.C
  5. Abstract:
  6. This file contains the routines that walk the configuration registry.
  7. Author:
  8. Rajen Shah (rajens) 1-Jul-1991
  9. Revision History:
  10. 29-Aug-1994 Danl
  11. We no longer grow log files in place. Therefore, the MaxSize value
  12. in the registery ends up being advisory only. We don't try to reserve
  13. that much memory at init time. So it could happen that when we need
  14. a larger file size that we may not have enough memory to allocate
  15. MaxSize bytes.
  16. 28-Mar-1994 Danl
  17. ReadRegistryInfo: LogFileInfo->LogFileName wasn't getting updated
  18. when using the default (generated) LogFileName.
  19. 16-Mar-1994 Danl
  20. Fixed Memory Leaks in ReadRegistryInfo(). Call to
  21. RtlDosPathNameToNtPathName allocates memory that wasn't being free'd.
  22. 03-Mar-1995 MarkBl
  23. Added GuestAccessRestriction flag initialization in ReadRegistryInfo.
  24. --*/
  25. //
  26. // INCLUDES
  27. //
  28. #include <eventp.h>
  29. #include <elfcfg.h>
  30. #include <stdlib.h>
  31. #include <malloc.h>
  32. #include <memory.h>
  33. //
  34. // STRUCTURES
  35. //
  36. //
  37. // This structure contains all the information used to setup and
  38. // for listening to registry changes in the eventlog tree.
  39. //
  40. typedef struct _REG_MONITOR_INFO
  41. {
  42. HANDLE NotifyEventHandle;
  43. DWORD Timeout;
  44. HANDLE WorkItemHandle;
  45. HANDLE RegMonitorHandle;
  46. }
  47. REG_MONITOR_INFO, *LPREG_MONITOR_INFO;
  48. //
  49. // GLOBALS
  50. //
  51. //
  52. // IMPORTANT: If NUM_KEYS_MONITORED is changed, be sure to update the initialization of GlRegMonitorInfo and
  53. // the ElfAllEventsCleared macro accordingly.
  54. //
  55. #define NUM_KEYS_MONITORED 2
  56. REG_MONITOR_INFO GlRegMonitorInfo[NUM_KEYS_MONITORED] = { {NULL, 0, NULL, NULL}, {NULL, 0, NULL, NULL} };
  57. #define ElfAllEventsCleared() (GlRegMonitorInfo[0].NotifyEventHandle == NULL && \
  58. GlRegMonitorInfo[1].NotifyEventHandle == NULL )
  59. //
  60. // LOCAL FUNCTIONS
  61. //
  62. VOID
  63. ElfRegistryMonitor(
  64. PVOID pParms,
  65. BOOLEAN fWaitStatus
  66. );
  67. BOOL
  68. ElfSetupMonitor(
  69. LPREG_MONITOR_INFO pMonitorInfo
  70. );
  71. VOID
  72. ProcessChange (
  73. HANDLE hLogFile,
  74. PUNICODE_STRING ModuleName,
  75. PUNICODE_STRING LogFileName,
  76. ULONG MaxSize,
  77. ULONG Retention,
  78. ULONG GuestAccessRestriction,
  79. LOGPOPUP logpLogPopup,
  80. DWORD dwAutoBackup
  81. )
  82. /*++
  83. Routine Description:
  84. This routine is called by ProcessRegistryChanges for each log file.
  85. Arguments:
  86. Return Value:
  87. None
  88. --*/
  89. {
  90. NTSTATUS Status = STATUS_SUCCESS;
  91. PLOGMODULE pModule;
  92. PLOGFILE pLogFile;
  93. ULONG Size;
  94. PVOID BaseAddress;
  95. PUNICODE_STRING pFileNameString;
  96. LPWSTR FileName;
  97. PVOID FreeAddress;
  98. pModule = GetModuleStruc (ModuleName);
  99. //
  100. // If this module didn't exist, this was a brand new log file and
  101. // we need to create all the structures
  102. //
  103. if (pModule == ElfDefaultLogModule &&
  104. wcscmp(ModuleName->Buffer, ELF_DEFAULT_MODULE_NAME))
  105. {
  106. ELF_LOG1(MODULES,
  107. "ProcessChange: %ws log doesn't exist -- creating\n",
  108. ModuleName->Buffer);
  109. Status = SetUpDataStruct(LogFileName,
  110. MaxSize,
  111. Retention,
  112. GuestAccessRestriction,
  113. ModuleName,
  114. hLogFile,
  115. ElfNormalLog,
  116. logpLogPopup,
  117. dwAutoBackup);
  118. return;
  119. }
  120. //
  121. // Update values
  122. //
  123. pLogFile = pModule->LogFile;
  124. pLogFile->Retention = Retention;
  125. pLogFile->logpLogPopup = logpLogPopup;
  126. pLogFile->AutoBackupLogFiles = dwAutoBackup;
  127. //
  128. // Check to see if the name has changed. If it has, and the log
  129. // hasn't been used yet, then use the new name. Be sure to free
  130. // memory that was used for the old name.
  131. //
  132. if ((wcscmp(pLogFile->LogFileName->Buffer, LogFileName->Buffer) != 0)
  133. &&
  134. (pLogFile->BeginRecord == pLogFile->EndRecord))
  135. {
  136. pFileNameString = ElfpAllocateBuffer(sizeof(UNICODE_STRING)
  137. + LogFileName->MaximumLength);
  138. if (pFileNameString != NULL)
  139. {
  140. FileName = (LPWSTR)(pFileNameString + 1);
  141. wcscpy(FileName, LogFileName->Buffer);
  142. RtlInitUnicodeString(pFileNameString, FileName);
  143. ElfpFreeBuffer(pLogFile->LogFileName);
  144. pLogFile->LogFileName = pFileNameString;
  145. }
  146. }
  147. //
  148. // The log file can only be grown dynamically. To shrink it,
  149. // it has to be cleared.
  150. //
  151. if (pLogFile->ConfigMaxFileSize < ELFFILESIZE(MaxSize))
  152. {
  153. /*
  154. Description of recent changes. Problem and Solution:
  155. A couple of problems exist. (1) There is no error
  156. checking if memory can't be allocated or mapped, and
  157. therefore, no error paths exist for handling these
  158. situations. (2) Now that the eventlog is in services.exe
  159. there isn't a good way to synchronize memory allocations.
  160. Solution:
  161. I considered having some utility routines for managing
  162. memory in the eventlog. These would attempt to
  163. extend a reserved block, or get a new reserved block.
  164. However, there are so many places where that could fail,
  165. it seemed very cumbersome to support the reserved blocks.
  166. So the current design only deals with mapped views.
  167. The ConfigMaxFileSize is only used to limit the size of
  168. the mapped view, and doesn't reserve anything. This
  169. means you are not guaranteed to be operating with a file as
  170. large as the MaxSize specified in the registry. But then,
  171. you weren't guarenteed that it would even work with the
  172. original design.
  173. */
  174. ELF_LOG3(TRACE,
  175. "ProcessChange: Growing %ws log from %x bytes to %x bytes\n",
  176. ModuleName->Buffer,
  177. pLogFile->ConfigMaxFileSize,
  178. ELFFILESIZE(MaxSize));
  179. pLogFile->ConfigMaxFileSize = ELFFILESIZE(MaxSize);
  180. pLogFile->NextClearMaxFileSize = ELFFILESIZE(MaxSize);
  181. }
  182. else if (pLogFile->ConfigMaxFileSize > ELFFILESIZE(MaxSize))
  183. {
  184. //
  185. // They're shrinking the size of the log file.
  186. // Next time we clear the log file, we'll use the new size
  187. // and new retention.
  188. //
  189. ELF_LOG3(TRACE,
  190. "ProcessChange: Shrinking %ws log from %x bytes to %x bytes at next clear\n",
  191. ModuleName->Buffer,
  192. pLogFile->ConfigMaxFileSize,
  193. ELFFILESIZE(MaxSize));
  194. pLogFile->NextClearMaxFileSize = ELFFILESIZE(MaxSize);
  195. }
  196. //
  197. // Now see if they've added any new modules for this log file
  198. //
  199. SetUpModules(hLogFile, pLogFile, TRUE);
  200. return;
  201. }
  202. VOID
  203. ProcessRegistryChanges (
  204. VOID
  205. )
  206. /*++
  207. Routine Description:
  208. This routine processes that changes that have occurred in the
  209. eventlog node. It does this by rescanning the whole Eventlog node
  210. and then comparing with what it has as the current configuration.
  211. Arguments:
  212. NONE.
  213. Return Value:
  214. NONE
  215. --*/
  216. {
  217. NTSTATUS Status;
  218. HANDLE hLogFile;
  219. UNICODE_STRING SubKeyName;
  220. ULONG Index = 0;
  221. BYTE Buffer[ELF_MAX_REG_KEY_INFO_SIZE];
  222. PKEY_NODE_INFORMATION KeyBuffer = (PKEY_NODE_INFORMATION) Buffer;
  223. ULONG ActualSize;
  224. LOG_FILE_INFO LogFileInfo;
  225. PWCHAR SubKeyString;
  226. OBJECT_ATTRIBUTES ObjectAttributes;
  227. PLOGMODULE pModule;
  228. LOGPOPUP logpLogPopup;
  229. #if DBG
  230. ULONG ulActualSize;
  231. #endif // DBG
  232. ELF_LOG0(TRACE,
  233. "ProcessRegistryChanges: Handling change in Eventlog service key\n");
  234. //
  235. // Take the global resource so that nobody is making changes or
  236. // using the existing configured information.
  237. //
  238. GetGlobalResource (ELF_GLOBAL_SHARED);
  239. #if DBG
  240. //
  241. // See if the Debug flag changed
  242. //
  243. RtlInitUnicodeString(&SubKeyName, VALUE_DEBUG);
  244. Status = NtQueryValueKey(hEventLogNode,
  245. &SubKeyName,
  246. KeyValuePartialInformation,
  247. KeyBuffer,
  248. ELF_MAX_REG_KEY_INFO_SIZE,
  249. &ulActualSize);
  250. if (NT_SUCCESS(Status))
  251. {
  252. if (((PKEY_VALUE_PARTIAL_INFORMATION) KeyBuffer)->Type == REG_DWORD)
  253. {
  254. ElfDebugLevel = *(LPDWORD) (((PKEY_VALUE_PARTIAL_INFORMATION) KeyBuffer)->Data);
  255. }
  256. }
  257. else
  258. {
  259. ELF_LOG1(TRACE,
  260. "ProcessRegistryChanges: NtQueryValueKey for ElfDebugLevel failed %#x\n",
  261. Status);
  262. }
  263. ELF_LOG1(TRACE,
  264. "ProcessRegistryChanges: New ElfDebugLevel is %#x\n",
  265. ElfDebugLevel);
  266. #endif // DBG
  267. Status = STATUS_SUCCESS;
  268. //
  269. // Loop thru the subkeys under Eventlog and set up each logfile
  270. //
  271. while (NT_SUCCESS(Status))
  272. {
  273. Status = NtEnumerateKey(hEventLogNode,
  274. Index++,
  275. KeyNodeInformation,
  276. KeyBuffer,
  277. ELF_MAX_REG_KEY_INFO_SIZE,
  278. &ActualSize);
  279. if (NT_SUCCESS(Status))
  280. {
  281. //
  282. // It turns out the Name isn't null terminated, so we need
  283. // to copy it somewhere and null terminate it before we use it
  284. //
  285. SubKeyString = ElfpAllocateBuffer(KeyBuffer->NameLength + sizeof (WCHAR));
  286. if (!SubKeyString)
  287. {
  288. //
  289. // No one to notify, just give up till next time.
  290. //
  291. ELF_LOG0(ERROR,
  292. "ProcessRegistryChanges: Unable to allocate subkey -- returning\n");
  293. ReleaseGlobalResource();
  294. return;
  295. }
  296. memcpy(SubKeyString, KeyBuffer->Name, KeyBuffer->NameLength);
  297. SubKeyString[KeyBuffer->NameLength / sizeof(WCHAR)] = L'\0' ;
  298. //
  299. // Open the node for this logfile and extract the information
  300. // required by SetupDataStruct, and then call it.
  301. //
  302. RtlInitUnicodeString(&SubKeyName, SubKeyString);
  303. InitializeObjectAttributes(&ObjectAttributes,
  304. &SubKeyName,
  305. OBJ_CASE_INSENSITIVE,
  306. hEventLogNode,
  307. NULL
  308. );
  309. Status = NtOpenKey(&hLogFile,
  310. KEY_READ | KEY_SET_VALUE,
  311. &ObjectAttributes);
  312. //
  313. // Should always succeed since I just enum'ed it, but if it
  314. // doesn't, just skip it
  315. //
  316. if (!NT_SUCCESS(Status))
  317. {
  318. ELF_LOG2(ERROR,
  319. "ProcessRegistryChanges: NtOpenKey for subkey %ws failed %#x\n",
  320. SubKeyName,
  321. Status);
  322. ElfpFreeBuffer(SubKeyString);
  323. Status = STATUS_SUCCESS; // to keep the enum going
  324. continue;
  325. }
  326. //
  327. // Get the updated information from the registry. Note that we
  328. // have to initialize the "log full" popup policy before doing
  329. // so since ReadRegistryInfo will compare the value found in the
  330. // registry (if there is one) to the current value.
  331. //
  332. pModule = GetModuleStruc(&SubKeyName);
  333. LogFileInfo.logpLogPopup = pModule->LogFile->logpLogPopup;
  334. Status = ReadRegistryInfo(hLogFile,
  335. &SubKeyName,
  336. &LogFileInfo);
  337. if (NT_SUCCESS(Status))
  338. {
  339. //
  340. // Now process any changes for the log file.
  341. // ProcessChange deals with any errors.
  342. //
  343. ProcessChange (
  344. hLogFile,
  345. &SubKeyName,
  346. LogFileInfo.LogFileName,
  347. LogFileInfo.MaxFileSize,
  348. LogFileInfo.Retention,
  349. LogFileInfo.GuestAccessRestriction,
  350. LogFileInfo.logpLogPopup,
  351. LogFileInfo.dwAutoBackup);
  352. //
  353. // Free the buffer that was allocated in ReadRegistryInfo.
  354. //
  355. ElfpFreeBuffer(LogFileInfo.LogFileName);
  356. }
  357. else
  358. {
  359. ELF_LOG2(ERROR,
  360. "ProcessRegistryChanges: ReadRegistryInfo for subkey %ws failed %#x\n",
  361. SubKeyString,
  362. Status);
  363. }
  364. ElfpFreeBuffer(SubKeyString);
  365. NtClose(hLogFile);
  366. }
  367. }
  368. //
  369. // Release the global resource.
  370. //
  371. ReleaseGlobalResource();
  372. } // ProcessRegistryChanges
  373. NTSTATUS
  374. ElfCheckForComputerNameChange(
  375. )
  376. /*++
  377. Routine Description:
  378. This routine checks to determine if the computer name has changed. If
  379. it has, then it generates an event.
  380. Arguments:
  381. NONE
  382. Return Value:
  383. NONE
  384. --*/
  385. {
  386. LPWSTR Dates[2];
  387. NTSTATUS Status;
  388. UNICODE_STRING ValueName;
  389. ULONG ulActualSize;
  390. DWORD dwLen;
  391. WCHAR wElfComputerName[MAX_COMPUTERNAME_LENGTH + 1];
  392. WCHAR wComputerName[MAX_COMPUTERNAME_LENGTH + 1];
  393. DWORD dwComputerNameLen = MAX_COMPUTERNAME_LENGTH + 1;
  394. BYTE Buffer[ELF_MAX_REG_KEY_INFO_SIZE];
  395. PKEY_VALUE_PARTIAL_INFORMATION ValueBuffer =
  396. (PKEY_VALUE_PARTIAL_INFORMATION) Buffer;
  397. RtlInitUnicodeString(&ValueName, VALUE_COMPUTERNAME);
  398. // Read the name that the event log stored.
  399. Status = NtQueryValueKey(hEventLogNode,
  400. &ValueName,
  401. KeyValuePartialInformation,
  402. ValueBuffer,
  403. ELF_MAX_REG_KEY_INFO_SIZE,
  404. &ulActualSize);
  405. if (!NT_SUCCESS(Status) || ValueBuffer->DataLength == 0)
  406. {
  407. ELF_LOG1(ERROR,
  408. "ElfCheckForComputerNameChange: NtQueryValueKey for current name failed %#x\n",
  409. Status);
  410. return Status;
  411. }
  412. wcscpy(wElfComputerName, (WCHAR *)ValueBuffer->Data);
  413. // Read the active name.
  414. Status = NtQueryValueKey(hComputerNameNode,
  415. &ValueName,
  416. KeyValuePartialInformation,
  417. ValueBuffer,
  418. ELF_MAX_REG_KEY_INFO_SIZE,
  419. &ulActualSize);
  420. if (!NT_SUCCESS(Status) || ValueBuffer->DataLength == 0)
  421. {
  422. ELF_LOG1(ERROR,
  423. "ElfCheckForComputerNameChange: NtQueryValueKey for active name failed %#x\n",
  424. Status);
  425. return Status;
  426. }
  427. wcscpy(wComputerName, (WCHAR *)ValueBuffer->Data);
  428. // If the names are the same, just return STATUS_SUCCESS
  429. if (!_wcsicmp(wElfComputerName, wComputerName))
  430. return STATUS_SUCCESS;
  431. Dates[0] = wElfComputerName;
  432. Dates[1] = wComputerName;
  433. ElfpCreateElfEvent(EVENT_ComputerNameChange,
  434. EVENTLOG_INFORMATION_TYPE,
  435. 0, // EventCategory
  436. 2, // NumberOfStrings
  437. Dates, // Strings
  438. NULL, // Data
  439. 0, // Datalength
  440. 0,
  441. FALSE); // flags
  442. dwLen = sizeof(WCHAR) * (wcslen(wComputerName) + 1);
  443. Status = NtSetValueKey(hEventLogNode,
  444. &ValueName,
  445. 0,
  446. REG_SZ,
  447. wComputerName,
  448. dwLen);
  449. if (!NT_SUCCESS(Status))
  450. ELF_LOG1(ERROR,
  451. "ElfCheckForComputerNameChange: NtSetValueKey failed %#x\n",
  452. Status);
  453. return Status;
  454. }
  455. VOID
  456. ElfRegistryMonitor (
  457. PVOID pParms,
  458. BOOLEAN fWaitStatus
  459. )
  460. /*++
  461. Routine Description:
  462. This is the entry point for the thread that will monitor changes in
  463. the registry. If anything changes, it will have to scan the change
  464. and then make the appropriate changes to the data structures in the
  465. service to reflect the new information.
  466. Arguments:
  467. NONE
  468. Return Value:
  469. NONE
  470. --*/
  471. {
  472. NTSTATUS ntStatus;
  473. LPREG_MONITOR_INFO pMonitorInfo = (LPREG_MONITOR_INFO)pParms;
  474. ELF_LOG0(TRACE,
  475. "ElfRegistryMonitor: Registry monitor thread waking up\n");
  476. //
  477. // Deregister the work item (must be done even if the
  478. // WT_EXECUTEONLYONCE flag is specified)
  479. //
  480. if (pMonitorInfo->WorkItemHandle != NULL)
  481. {
  482. ntStatus = RtlDeregisterWait(pMonitorInfo->WorkItemHandle);
  483. pMonitorInfo->WorkItemHandle = NULL;
  484. if (!NT_SUCCESS(ntStatus))
  485. {
  486. ELF_LOG1(ERROR,
  487. "ElfRegistryMonitor: RtlDeregisterWorkItem failed %#x\n",
  488. ntStatus);
  489. }
  490. }
  491. if (GetElState() == STOPPING)
  492. {
  493. //
  494. // If the eventlog is shutting down, then we need
  495. // to terminate this thread.
  496. //
  497. ELF_LOG0(TRACE, "ElfRegistryMonitor: Shutdown\n");
  498. //
  499. // Close the registry handle and registry event handle.
  500. //
  501. if( pMonitorInfo->NotifyEventHandle != NULL )
  502. {
  503. NtClose( pMonitorInfo->NotifyEventHandle );
  504. pMonitorInfo->NotifyEventHandle = NULL;
  505. }
  506. if( pMonitorInfo->RegMonitorHandle != NULL )
  507. {
  508. NtClose(pMonitorInfo->RegMonitorHandle);
  509. pMonitorInfo->RegMonitorHandle = NULL;
  510. }
  511. //
  512. // This thread will perform the final cleanup for the eventlog.
  513. // Cleanup is not initiated until all events have been signaled
  514. // and closed
  515. //
  516. if( ElfAllEventsCleared() )
  517. {
  518. ElfpCleanUp(EventFlags);
  519. }
  520. return;
  521. }
  522. if (fWaitStatus == TRUE)
  523. {
  524. ELF_LOG0(TRACE,
  525. "ElfRegistryMonitor: Running because of a timeout -- running queued list\n");
  526. //
  527. // Timer popped, try running the list
  528. //
  529. if (!IsListEmpty(&QueuedEventListHead))
  530. {
  531. //
  532. // There are things queued up to write, do it
  533. //
  534. WriteQueuedEvents();
  535. }
  536. //
  537. // Don't wait again
  538. //
  539. pMonitorInfo->Timeout = INFINITE;
  540. }
  541. else
  542. {
  543. ELF_LOG0(TRACE,
  544. "ElfRegistryMonitor: Running because of notification\n");
  545. ProcessRegistryChanges ();
  546. ElfCheckForComputerNameChange();
  547. }
  548. if (!ElfSetupMonitor(pMonitorInfo))
  549. {
  550. ELF_LOG0(ERROR,
  551. "ElfRegistryMonitor: ElfSetupMonitor failed -- "
  552. "no longer listening for reg changes\n");
  553. }
  554. ELF_LOG0(TRACE,
  555. "ElfRegistryMonitor: Returning\n");
  556. return;
  557. } // ElfRegistryMonitor
  558. VOID
  559. InitNotify(
  560. PVOID pData
  561. )
  562. /*++
  563. Routine Description:
  564. Arguments:
  565. Return Value:
  566. --*/
  567. {
  568. NTSTATUS NtStatus = STATUS_SUCCESS;
  569. DWORD status = NO_ERROR;
  570. DWORD Buffer;
  571. PVOID pBuffer = &Buffer;
  572. LPREG_MONITOR_INFO pMonitorInfo;
  573. static IO_STATUS_BLOCK IoStatusBlock;
  574. ELF_LOG0(TRACE,
  575. "InitNotify: Registering Eventlog key with NtNotifyChangeKey\n");
  576. pMonitorInfo = (LPREG_MONITOR_INFO)pData;
  577. NtStatus = NtNotifyChangeKey (
  578. pMonitorInfo->RegMonitorHandle,
  579. pMonitorInfo->NotifyEventHandle,
  580. NULL,
  581. NULL,
  582. &IoStatusBlock,
  583. REG_NOTIFY_CHANGE_LAST_SET |
  584. REG_NOTIFY_CHANGE_NAME,
  585. TRUE,
  586. pBuffer,
  587. 1,
  588. TRUE); // return and wait on event
  589. if (!NT_SUCCESS(NtStatus))
  590. {
  591. ELF_LOG1(ERROR,
  592. "InitNotify: NtNotifyChangeKey on Eventlog key failed %#x\n",
  593. NtStatus);
  594. status = RtlNtStatusToDosError(NtStatus);
  595. }
  596. ELF_LOG0( TRACE, "InitNotify: Returning\n" );
  597. return;
  598. } // InitNotify
  599. BOOL
  600. ElfSetupMonitor(
  601. LPREG_MONITOR_INFO pMonitorInfo
  602. )
  603. /*++
  604. Routine Description:
  605. This function submits a request for a registry NotifyChangeKey
  606. and then submits a work item to the service controller thread
  607. management system to wait for the Notification handle to become
  608. signaled.
  609. Arguments:
  610. pMonitorInfo - This is a pointer to a MONITOR_INFO structure. This
  611. function fills in the WorkItemHandle member of that structure
  612. if successfully adds a new work item.
  613. Return Value:
  614. TRUE - if successful in setting up.
  615. FALSE - if unsuccessful. A work item hasn't been submitted, and
  616. we won't be listening for registry changes.
  617. --*/
  618. {
  619. NTSTATUS Status = STATUS_SUCCESS;
  620. //
  621. // Call NtNotifyChange Key via the thread pool
  622. // and make sure the thread that created the I/O
  623. // request will always be around.
  624. //
  625. Status = RtlQueueWorkItem(InitNotify, // Callback
  626. pMonitorInfo, // pContext
  627. WT_EXECUTEONLYONCE |
  628. WT_EXECUTEINPERSISTENTIOTHREAD);
  629. if (!NT_SUCCESS(Status))
  630. {
  631. ELF_LOG1(ERROR,
  632. "ElfSetupMonitor: RtlQueueWorkItem failed %#x\n",
  633. Status);
  634. return FALSE;
  635. }
  636. //
  637. // Add the work item that is to be called when the
  638. // NotifyEventHandle is signalled.
  639. //
  640. Status = RtlRegisterWait(&pMonitorInfo->WorkItemHandle,
  641. pMonitorInfo->NotifyEventHandle, // Waitable handle
  642. ElfRegistryMonitor, // Callback
  643. pMonitorInfo, // pContext
  644. pMonitorInfo->Timeout, // Timeout
  645. WT_EXECUTEONLYONCE |
  646. WT_EXECUTEINPERSISTENTIOTHREAD);
  647. if (!NT_SUCCESS(Status))
  648. {
  649. ELF_LOG1(ERROR,
  650. "ElfSetupMonitor: RtlRegisterWait failed %#x\n",
  651. Status);
  652. return FALSE;
  653. }
  654. return TRUE;
  655. } // ElfSetupMonitor
  656. BOOL
  657. ElfStartRegistryMonitor()
  658. /*++
  659. Routine Description:
  660. This routine starts up the thread that monitors changes in the registry.
  661. This function calls ElfSetupMonitor() to register for the change
  662. notification and to submit a work item to wait for the registry
  663. change event to get signaled. When signalled, the ElfRegistryMonitor()
  664. callback function is called by a thread from the services thread pool.
  665. This callback function services the notification.
  666. Arguments:
  667. NONE
  668. Return Value:
  669. TRUE if thread creation succeeded, FALSE otherwise.
  670. Note:
  671. --*/
  672. {
  673. NTSTATUS Status = STATUS_SUCCESS;
  674. DWORD LoopCounter = 0;
  675. BOOL ReturnStatus = TRUE;
  676. DWORD LoopCount;
  677. ELF_LOG0(TRACE, "ElfStartRegistryMonitor: Setting up registry change notification\n");
  678. if (hEventLogNode == NULL)
  679. {
  680. ELF_LOG0(ERROR, "ElfStartRegistryMonitor: No Eventlog key -- exiting\n");
  681. return FALSE;
  682. }
  683. if (hComputerNameNode == NULL)
  684. {
  685. ELF_LOG0(ERROR,
  686. "ElfStartRegistryMonitor: No ComputerName key -- exiting\n");
  687. return FALSE;
  688. }
  689. GlRegMonitorInfo[0].RegMonitorHandle = hEventLogNode;
  690. GlRegMonitorInfo[1].RegMonitorHandle = hComputerNameNode;
  691. //
  692. // Create the events on which to wait
  693. //
  694. for( LoopCount = 0; LoopCount < NUM_KEYS_MONITORED; LoopCount++ )
  695. {
  696. Status = NtCreateEvent(&GlRegMonitorInfo[LoopCount].NotifyEventHandle,
  697. EVENT_ALL_ACCESS,
  698. NULL,
  699. NotificationEvent,
  700. FALSE);
  701. if (!NT_SUCCESS(Status))
  702. {
  703. ELF_LOG1(ERROR, "ElfStartRegistryMonitor: NtCreateEvent failed %#x\n",
  704. Status);
  705. GlRegMonitorInfo[LoopCount].NotifyEventHandle = NULL;
  706. break;
  707. }
  708. //
  709. // Fill in the Monitor info structure with the event handle
  710. // and a 5 minute timeout.
  711. //
  712. GlRegMonitorInfo[LoopCount].Timeout = 5 * 60 * 1000;
  713. GlRegMonitorInfo[LoopCount].WorkItemHandle = NULL;
  714. }
  715. //
  716. // Cleanup all events, its all or nothing
  717. //
  718. if(!NT_SUCCESS(Status))
  719. {
  720. for( LoopCount = 0; LoopCount < NUM_KEYS_MONITORED; LoopCount++ )
  721. {
  722. if( GlRegMonitorInfo[LoopCount].NotifyEventHandle != NULL )
  723. {
  724. NtClose( GlRegMonitorInfo[LoopCount].NotifyEventHandle );
  725. GlRegMonitorInfo[LoopCount].NotifyEventHandle = NULL;
  726. }
  727. }
  728. return FALSE;
  729. }
  730. //
  731. // Setup for the change notify and
  732. // submit the work item to the eventlog threadpool.
  733. //
  734. for( LoopCount = 0; LoopCount < NUM_KEYS_MONITORED; LoopCount++ )
  735. {
  736. if (!ElfSetupMonitor(&GlRegMonitorInfo[LoopCount]))
  737. {
  738. ELF_LOG0(ERROR,
  739. "ElfStartRegistryMonitor: ElfSetupMonitor failed -- exiting\n");
  740. //
  741. // Note that it's OK to close this handle as there's no way
  742. // the handle was used for a registered wait at this point
  743. // (since ElfSetupMonitor failed).
  744. //
  745. NtClose( GlRegMonitorInfo[LoopCount].NotifyEventHandle );
  746. GlRegMonitorInfo[LoopCount].NotifyEventHandle = NULL;
  747. return FALSE;
  748. }
  749. //
  750. //Set this flag since we have at least one success
  751. //If any startup fails, then this setting will ensure that all
  752. // started monitors are shutdown
  753. //
  754. EventFlags |= ELF_STARTED_REGISTRY_MONITOR;
  755. }
  756. ELF_LOG0(TRACE, "ElfStartRegistryMonitor: Exiting after successful call\n");
  757. return TRUE;
  758. } // ElfStartRegistryMonitor
  759. VOID
  760. StopRegistryMonitor ()
  761. /*++
  762. Routine Description:
  763. This routine wakes up the work item that has been submitted for the
  764. purpose of monitoring registry eventlog changes. The thread created
  765. to service that work item will actually do the clean-up of the monitor
  766. thread.
  767. Arguments:
  768. NONE
  769. Return Value:
  770. NONE
  771. --*/
  772. {
  773. DWORD LoopCount = 0;
  774. ELF_LOG0(TRACE, "StopRegistryMonitor: Stopping registry monitor\n");
  775. //
  776. // Wake up the RegistryMonitorThread.
  777. //
  778. for( LoopCount = 0; LoopCount < NUM_KEYS_MONITORED; LoopCount++ )
  779. {
  780. if (GlRegMonitorInfo[LoopCount].NotifyEventHandle != NULL)
  781. {
  782. SetEvent(GlRegMonitorInfo[LoopCount].NotifyEventHandle);
  783. }
  784. }
  785. return;
  786. } // StopRegistryMonitor
  787. NTSTATUS
  788. ReadRegistryInfo (
  789. HANDLE hLogFile,
  790. PUNICODE_STRING SubKeyName,
  791. PLOG_FILE_INFO LogFileInfo
  792. )
  793. /*++
  794. Routine Description:
  795. This routine reads in the information from the node pointed to by
  796. hLogFile and stores it in the a structure so that the
  797. necessary data structures can be set up for the service.
  798. ALLOCATIONS: If successful, this function allocates memory for
  799. LogFileInfo->LogFileName. It is the responsiblilty of the caller
  800. to free this memory.
  801. Arguments:
  802. hLogFile - A handle to the Eventlog\<somelogfile> node in the registry
  803. KeyName - The subkey for this logfile to open
  804. LogFileInfo - The structure to fill in with the data
  805. Return Value:
  806. NTSTATUS
  807. --*/
  808. {
  809. #define EXPAND_BUFFER_SIZE 64
  810. NTSTATUS Status;
  811. BOOLEAN RegistryCorrupt = FALSE;
  812. BYTE Buffer[ELF_MAX_REG_KEY_INFO_SIZE];
  813. ULONG ActualSize;
  814. UNICODE_STRING ValueName;
  815. UNICODE_STRING UnexpandedName;
  816. UNICODE_STRING ExpandedName;
  817. ULONG NumberOfBytes = 0;
  818. BYTE ExpandNameBuffer[EXPAND_BUFFER_SIZE];
  819. PUNICODE_STRING FileNameString;
  820. LPWSTR FileName;
  821. BOOL ExpandedBufferWasAllocated=FALSE;
  822. PKEY_VALUE_FULL_INFORMATION ValueBuffer =
  823. (PKEY_VALUE_FULL_INFORMATION) Buffer;
  824. ASSERT(hLogFile != NULL);
  825. ELF_LOG1(TRACE,
  826. "ReadRegistryInfo: Reading information for %ws log\n",
  827. SubKeyName->Buffer);
  828. //
  829. // MaxSize
  830. //
  831. RtlInitUnicodeString(&ValueName, VALUE_MAXSIZE);
  832. Status = NtQueryValueKey(hLogFile,
  833. &ValueName,
  834. KeyValueFullInformation,
  835. ValueBuffer,
  836. ELF_MAX_REG_KEY_INFO_SIZE,
  837. &ActualSize);
  838. if (!NT_SUCCESS(Status))
  839. {
  840. ELF_LOG2(ERROR,
  841. "ReadRegistryInfo: Can't read MaxSize value for %ws log %#x\n",
  842. SubKeyName->Buffer,
  843. Status);
  844. LogFileInfo->MaxFileSize = ELF_DEFAULT_MAX_FILE_SIZE;
  845. RegistryCorrupt = TRUE;
  846. }
  847. else
  848. {
  849. LogFileInfo->MaxFileSize = *((PULONG)(Buffer +
  850. ValueBuffer->DataOffset));
  851. ELF_LOG2(TRACE,
  852. "ReadRegistryInfo: New MaxSize value for %ws log is %#x\n",
  853. SubKeyName->Buffer,
  854. LogFileInfo->MaxFileSize);
  855. }
  856. //
  857. // Retention period
  858. //
  859. RtlInitUnicodeString(&ValueName, VALUE_RETENTION);
  860. Status = NtQueryValueKey(hLogFile,
  861. &ValueName,
  862. KeyValueFullInformation,
  863. ValueBuffer,
  864. ELF_MAX_REG_KEY_INFO_SIZE,
  865. &ActualSize);
  866. if (!NT_SUCCESS(Status))
  867. {
  868. ELF_LOG2(ERROR,
  869. "ReadRegistryInfo: Can't read Retention value for %ws log %#x\n",
  870. SubKeyName->Buffer,
  871. Status);
  872. LogFileInfo->Retention = ELF_DEFAULT_RETENTION_PERIOD;
  873. RegistryCorrupt = TRUE;
  874. }
  875. else
  876. {
  877. LogFileInfo->Retention = *((PULONG)(Buffer +
  878. ValueBuffer->DataOffset));
  879. ELF_LOG2(TRACE,
  880. "ReadRegistryInfo: New Retention value for %ws log is %#x\n",
  881. SubKeyName->Buffer,
  882. LogFileInfo->Retention);
  883. }
  884. //
  885. // RestrictGuestAccess
  886. //
  887. RtlInitUnicodeString(&ValueName, VALUE_RESTRICT_GUEST_ACCESS);
  888. Status = NtQueryValueKey(hLogFile,
  889. &ValueName,
  890. KeyValueFullInformation,
  891. ValueBuffer,
  892. ELF_MAX_REG_KEY_INFO_SIZE,
  893. &ActualSize);
  894. if (!NT_SUCCESS(Status))
  895. {
  896. //
  897. // TRACE rather than ERROR as this value is optional
  898. //
  899. ELF_LOG2(TRACE,
  900. "ReadRegistryInfo: Can't read GuestAccessRestriction value for %ws log %#x\n",
  901. SubKeyName->Buffer,
  902. Status);
  903. LogFileInfo->GuestAccessRestriction = ELF_GUEST_ACCESS_UNRESTRICTED;
  904. }
  905. else
  906. {
  907. if (*((PULONG)(Buffer + ValueBuffer->DataOffset)) == 1)
  908. {
  909. ELF_LOG1(TRACE,
  910. "ReadRegistryInfo: Restricting Guest access to %ws log\n",
  911. SubKeyName->Buffer);
  912. LogFileInfo->GuestAccessRestriction = ELF_GUEST_ACCESS_RESTRICTED;
  913. }
  914. else
  915. {
  916. ELF_LOG1(TRACE,
  917. "ReadRegistryInfo: NOT restricting Guest access to %ws log\n",
  918. SubKeyName->Buffer);
  919. LogFileInfo->GuestAccessRestriction = ELF_GUEST_ACCESS_UNRESTRICTED;
  920. }
  921. }
  922. //
  923. // Autobackup value (optional!)
  924. //
  925. RtlInitUnicodeString(&ValueName, REGSTR_VAL_AUTOBACKUPLOGFILES);
  926. Status = NtQueryValueKey(hLogFile,
  927. &ValueName,
  928. KeyValueFullInformation,
  929. ValueBuffer,
  930. ELF_MAX_REG_KEY_INFO_SIZE,
  931. &ActualSize);
  932. if (!NT_SUCCESS(Status))
  933. {
  934. //
  935. // TRACE rather than ERROR as this value is optional
  936. //
  937. ELF_LOG2(TRACE,
  938. "ReadRegistryInfo: Can't read AutoBackupLogFiles value for %ws log %#x\n",
  939. SubKeyName->Buffer,
  940. Status);
  941. LogFileInfo->dwAutoBackup = ELF_DEFAULT_AUTOBACKUP;
  942. }
  943. else
  944. {
  945. LogFileInfo->dwAutoBackup = *((PULONG)(Buffer +
  946. ValueBuffer->DataOffset));
  947. ELF_LOG2(TRACE,
  948. "ReadRegistryInfo: AutoBackupLogFiles for %ws log is %#x\n",
  949. SubKeyName->Buffer,
  950. LogFileInfo->dwAutoBackup);
  951. }
  952. //
  953. // Filename
  954. //
  955. RtlInitUnicodeString(&ValueName, VALUE_FILENAME);
  956. Status = NtQueryValueKey(hLogFile,
  957. &ValueName,
  958. KeyValueFullInformation,
  959. ValueBuffer,
  960. ELF_MAX_REG_KEY_INFO_SIZE,
  961. &ActualSize);
  962. if (!NT_SUCCESS(Status))
  963. {
  964. ELF_LOG2(ERROR,
  965. "ReadRegistryInfo: Can't read Filename value for %ws log %#x\n",
  966. SubKeyName->Buffer,
  967. Status);
  968. //
  969. // Allocate the buffer for the UNICODE_STRING for the filename and
  970. // initialize it. (41 = \Systemroot\system32\config\xxxxxxxx.evt)
  971. //
  972. FileNameString = ElfpAllocateBuffer(41 * sizeof(WCHAR) + sizeof(UNICODE_STRING));
  973. if (!FileNameString)
  974. {
  975. ELF_LOG0(ERROR,
  976. "ReadRegistryInfo: Unable to allocate FileNameString\n");
  977. return STATUS_NO_MEMORY;
  978. }
  979. LogFileInfo->LogFileName = FileNameString;
  980. FileName = (LPWSTR)(FileNameString + 1);
  981. wcscpy(FileName, L"\\Systemroot\\System32\\Config\\");
  982. wcsncat(FileName, SubKeyName->Buffer, 8);
  983. wcscat(FileName, L".evt");
  984. RtlInitUnicodeString(FileNameString, FileName);
  985. RegistryCorrupt = TRUE;
  986. }
  987. else
  988. {
  989. //
  990. // If it's a REG_EXPAND_SZ expand it
  991. //
  992. if (ValueBuffer->Type == REG_EXPAND_SZ)
  993. {
  994. ELF_LOG0(TRACE,
  995. "ReadRegistryInfo: Filename is a REG_EXPAND_SZ -- expanding\n");
  996. //
  997. // Initialize the UNICODE_STRING, when the string isn't null
  998. // terminated
  999. //
  1000. UnexpandedName.MaximumLength = UnexpandedName.Length =
  1001. (USHORT) ValueBuffer->DataLength;
  1002. UnexpandedName.Buffer = (PWSTR) ((PBYTE) ValueBuffer +
  1003. ValueBuffer->DataOffset);
  1004. //
  1005. // Call the magic expand-o api
  1006. //
  1007. ExpandedName.Length = ExpandedName.MaximumLength = EXPAND_BUFFER_SIZE;
  1008. ExpandedName.Buffer = (LPWSTR) ExpandNameBuffer;
  1009. Status = RtlExpandEnvironmentStrings_U(NULL,
  1010. &UnexpandedName,
  1011. &ExpandedName,
  1012. &NumberOfBytes);
  1013. if (Status == STATUS_BUFFER_TOO_SMALL)
  1014. {
  1015. ELF_LOG0(TRACE,
  1016. "ReadRegistryInfo: Expansion buffer too small -- retrying\n");
  1017. //
  1018. // The default buffer wasn't big enough. Allocate a
  1019. // bigger one and try again
  1020. //
  1021. ExpandedName.Length = ExpandedName.MaximumLength = (USHORT) NumberOfBytes;
  1022. ExpandedName.Buffer = ElfpAllocateBuffer(ExpandedName.Length);
  1023. if (!ExpandedName.Buffer)
  1024. {
  1025. ELF_LOG0(ERROR,
  1026. "ReadRegistryInfo: Unable to allocate larger Filename buffer\n");
  1027. return(STATUS_NO_MEMORY);
  1028. }
  1029. ExpandedBufferWasAllocated = TRUE;
  1030. Status = RtlExpandEnvironmentStrings_U(NULL,
  1031. &UnexpandedName,
  1032. &ExpandedName,
  1033. &NumberOfBytes);
  1034. }
  1035. if (!NT_SUCCESS(Status))
  1036. {
  1037. ELF_LOG1(ERROR,
  1038. "ReadRegistryInfo: RtlExpandEnvironmentStrings_U failed %#x\n",
  1039. Status);
  1040. if (ExpandedBufferWasAllocated)
  1041. {
  1042. ElfpFreeBuffer(ExpandedName.Buffer);
  1043. }
  1044. return Status;
  1045. }
  1046. }
  1047. else
  1048. {
  1049. //
  1050. // It doesn't need to be expanded, just set up the UNICODE_STRING
  1051. // for the conversion to an NT pathname
  1052. //
  1053. ExpandedName.MaximumLength = ExpandedName.Length =
  1054. (USHORT) ValueBuffer->DataLength;
  1055. ExpandedName.Buffer = (PWSTR) ((PBYTE) ValueBuffer +
  1056. ValueBuffer->DataOffset);
  1057. }
  1058. //
  1059. // Now convert from a DOS pathname to an NT pathname
  1060. //
  1061. // NOTE: this allocates a buffer for ValueName.Buffer.
  1062. //
  1063. if (!RtlDosPathNameToNtPathName_U(ExpandedName.Buffer,
  1064. &ValueName,
  1065. NULL,
  1066. NULL))
  1067. {
  1068. ELF_LOG0(ERROR,
  1069. "ReadRegistryInfo: RtlDosPathNameToNtPathName_U failed\n");
  1070. if (ExpandedBufferWasAllocated)
  1071. {
  1072. ElfpFreeBuffer(ExpandedName.Buffer);
  1073. }
  1074. return STATUS_UNSUCCESSFUL;
  1075. }
  1076. //
  1077. // Allocate memory for the unicode string structure and the buffer
  1078. // so that it can be free'd with a single call.
  1079. //
  1080. FileNameString = ElfpAllocateBuffer(
  1081. sizeof(UNICODE_STRING) +
  1082. ((ValueName.Length + 1) * sizeof(WCHAR)));
  1083. if (FileNameString == NULL)
  1084. {
  1085. ELF_LOG0(ERROR,
  1086. "ReadRegistryInfo: Unable to allocate copy of NT filename\n");
  1087. if (ExpandedBufferWasAllocated)
  1088. {
  1089. ElfpFreeBuffer(ExpandedName.Buffer);
  1090. }
  1091. //
  1092. // RtlDosPathNameToNtPathName_U allocates off the process heap
  1093. //
  1094. RtlFreeHeap(RtlProcessHeap(), 0, ValueName.Buffer);
  1095. return STATUS_NO_MEMORY;
  1096. }
  1097. //
  1098. // Copy the NtPathName string into the new buffer, and initialize
  1099. // the unicode string.
  1100. //
  1101. FileName = (LPWSTR)(FileNameString + 1);
  1102. wcsncpy(FileName, ValueName.Buffer, ValueName.Length);
  1103. *(FileName+ValueName.Length) = L'\0';
  1104. RtlInitUnicodeString(FileNameString, FileName);
  1105. //
  1106. // RtlDosPathNameToNtPathName_U allocates off the process heap
  1107. //
  1108. RtlFreeHeap(RtlProcessHeap(), 0, ValueName.Buffer);
  1109. //
  1110. // Clean up if I had to allocate a bigger buffer than the default
  1111. //
  1112. if (ExpandedBufferWasAllocated)
  1113. {
  1114. ElfpFreeBuffer(ExpandedName.Buffer);
  1115. }
  1116. }
  1117. //
  1118. // Add the LogFileName to the LogFileInfo structure.
  1119. //
  1120. LogFileInfo->LogFileName = FileNameString;
  1121. ELF_LOG2(TRACE,
  1122. "ReadRegistryInfo: New (expanded) Filename value for %ws log is %ws\n",
  1123. SubKeyName->Buffer,
  1124. LogFileInfo->LogFileName->Buffer);
  1125. //
  1126. // "Log full" popup policy -- never change the security log
  1127. //
  1128. if (_wcsicmp(SubKeyName->Buffer, ELF_SECURITY_MODULE_NAME) != 0)
  1129. {
  1130. RtlInitUnicodeString(&ValueName, VALUE_LOGPOPUP);
  1131. Status = NtQueryValueKey(hLogFile,
  1132. &ValueName,
  1133. KeyValueFullInformation,
  1134. ValueBuffer,
  1135. ELF_MAX_REG_KEY_INFO_SIZE,
  1136. &ActualSize);
  1137. if (NT_SUCCESS(Status))
  1138. {
  1139. LOGPOPUP logpRegValue = *(PULONG)(Buffer + ValueBuffer->DataOffset);
  1140. //
  1141. // Only update the value if this constitutes a change in the current policy
  1142. //
  1143. if (LogFileInfo->logpLogPopup == LOGPOPUP_NEVER_SHOW
  1144. ||
  1145. logpRegValue == LOGPOPUP_NEVER_SHOW)
  1146. {
  1147. LogFileInfo->logpLogPopup =
  1148. (logpRegValue == LOGPOPUP_NEVER_SHOW ? LOGPOPUP_NEVER_SHOW :
  1149. LOGPOPUP_CLEARED);
  1150. }
  1151. }
  1152. else
  1153. {
  1154. //
  1155. // TRACE rather than ERROR as this value is optional
  1156. //
  1157. ELF_LOG2(TRACE,
  1158. "ReadRegistryInfo: Can't read LogPopup value for %ws log %#x\n",
  1159. SubKeyName->Buffer,
  1160. Status);
  1161. }
  1162. }
  1163. //
  1164. // If we didn't find all the required values, tell someone
  1165. //
  1166. if (RegistryCorrupt)
  1167. {
  1168. ELF_LOG1(ERROR,
  1169. "ReadRegistryInfo: One or more registry values for %ws log invalid\n",
  1170. SubKeyName->Buffer);
  1171. }
  1172. return STATUS_SUCCESS;
  1173. }