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. /*++
  2. Copyright (c) 1992-1997 Microsoft Corporation
  3. Module Name:
  4. trapmgrs.c
  5. Abstract:
  6. Contains routines for manipulating trap destination structures.
  7. Environment:
  8. User Mode - Win32
  9. Revision History:
  10. 10-Feb-1997 DonRyan
  11. Rewrote to implement SNMPv2 support.
  12. --*/
  13. ///////////////////////////////////////////////////////////////////////////////
  14. // //
  15. // Include files //
  16. // //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #include "globals.h"
  19. #include "snmpmgrs.h"
  20. #include "trapmgrs.h"
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // //
  23. // Private procedures //
  24. // //
  25. ///////////////////////////////////////////////////////////////////////////////
  26. BOOL
  27. FindTrapDestination(
  28. PTRAP_DESTINATION_LIST_ENTRY * ppTLE,
  29. LPSTR pCommunity
  30. )
  31. /*++
  32. Routine Description:
  33. Locates valid trap destination in list.
  34. Arguments:
  35. ppTLE - pointer to receive pointer to entry.
  36. pCommunity - pointer to trap destination to find.
  37. Return Values:
  38. Returns true if successful.
  39. --*/
  40. {
  41. PLIST_ENTRY pLE;
  42. PTRAP_DESTINATION_LIST_ENTRY pTLE;
  43. // initialize
  44. *ppTLE = NULL;
  45. // obtain pointer to list head
  46. pLE = g_TrapDestinations.Flink;
  47. // process all entries in list
  48. while (pLE != &g_TrapDestinations) {
  49. // retrieve pointer to trap destination structure
  50. pTLE = CONTAINING_RECORD(pLE, TRAP_DESTINATION_LIST_ENTRY, Link);
  51. // compare trap destination string with entry
  52. if (!strcmp(pTLE->pCommunity, pCommunity)) {
  53. // transfer
  54. *ppTLE = pTLE;
  55. // success
  56. return TRUE;
  57. }
  58. // next entry
  59. pLE = pLE->Flink;
  60. }
  61. // failure
  62. return FALSE;
  63. }
  64. BOOL
  65. AddTrapDestination(
  66. HKEY hKey,
  67. LPWSTR pwCommunity
  68. )
  69. /*++
  70. Routine Description:
  71. Adds trap destination to list.
  72. Arguments:
  73. hKey - trap destination subkey.
  74. pwCommunity - pointer to trap destination to add.
  75. Return Values:
  76. Returns true if successful.
  77. --*/
  78. {
  79. HKEY hSubKey;
  80. LONG lStatus;
  81. BOOL fOk = FALSE;
  82. PTRAP_DESTINATION_LIST_ENTRY pTLE = NULL;
  83. LPSTR pCommunity = NULL;
  84. // open registry subkey
  85. lStatus = RegOpenKeyExW(
  86. hKey,
  87. pwCommunity,
  88. 0,
  89. KEY_READ,
  90. &hSubKey
  91. );
  92. // validate return code
  93. if (lStatus == ERROR_SUCCESS &&
  94. SnmpUtilUnicodeToUTF8(
  95. &pCommunity,
  96. pwCommunity,
  97. TRUE) == 0) {
  98. // attempt to locate in list
  99. if (FindTrapDestination(&pTLE, pCommunity)) {
  100. SNMPDBG((
  101. SNMP_LOG_TRACE,
  102. "SNMP: SVC: updating trap destinations for %s.\n",
  103. pCommunity
  104. ));
  105. // load associated managers
  106. LoadManagers(hSubKey, &pTLE->Managers);
  107. // success
  108. fOk = TRUE;
  109. } else {
  110. // allocate trap destination structure
  111. if (AllocTLE(&pTLE, pCommunity)) {
  112. SNMPDBG((
  113. SNMP_LOG_TRACE,
  114. "SNMP: SVC: adding trap destinations for %s.\n",
  115. pCommunity
  116. ));
  117. // load associated managers
  118. if (LoadManagers(hSubKey, &pTLE->Managers)) {
  119. // insert into valid communities list
  120. InsertTailList(&g_TrapDestinations, &pTLE->Link);
  121. // success
  122. fOk = TRUE;
  123. }
  124. // cleanup
  125. if (!fOk) {
  126. // release
  127. FreeTLE(pTLE);
  128. }
  129. }
  130. }
  131. // release subkey
  132. RegCloseKey(hSubKey);
  133. SnmpUtilMemFree(pCommunity);
  134. }
  135. return fOk;
  136. }
  137. ///////////////////////////////////////////////////////////////////////////////
  138. // //
  139. // Public procedures //
  140. // //
  141. ///////////////////////////////////////////////////////////////////////////////
  142. BOOL
  143. AllocTLE(
  144. PTRAP_DESTINATION_LIST_ENTRY * ppTLE,
  145. LPSTR pCommunity
  146. )
  147. /*++
  148. Routine Description:
  149. Allocates trap destination structure and initializes.
  150. Arguments:
  151. ppTLE - pointer to receive pointer to entry.
  152. pCommunity - pointer to trap destination string.
  153. Return Values:
  154. Returns true if successful.
  155. --*/
  156. {
  157. BOOL fOk = FALSE;
  158. PTRAP_DESTINATION_LIST_ENTRY pTLE = NULL;
  159. // attempt to allocate structure
  160. pTLE = AgentMemAlloc(sizeof(TRAP_DESTINATION_LIST_ENTRY));
  161. // validate
  162. if (pTLE != NULL) {
  163. // allocate memory for trap destination string
  164. pTLE->pCommunity = AgentMemAlloc(strlen(pCommunity)+1);
  165. // validate
  166. if (pTLE->pCommunity != NULL) {
  167. // transfer trap destination string
  168. strcpy(pTLE->pCommunity, pCommunity);
  169. // initialize list of managers
  170. InitializeListHead(&pTLE->Managers);
  171. // success
  172. fOk = TRUE;
  173. }
  174. // cleanup
  175. if (!fOk) {
  176. // release
  177. FreeTLE(pTLE);
  178. // re-init
  179. pTLE = NULL;
  180. }
  181. }
  182. // transfer
  183. *ppTLE = pTLE;
  184. return fOk;
  185. }
  186. BOOL
  187. FreeTLE(
  188. PTRAP_DESTINATION_LIST_ENTRY pTLE
  189. )
  190. /*++
  191. Routine Description:
  192. Releases trap destination structure.
  193. Arguments:
  194. pTLE - pointer to trap destination list entry to be freed.
  195. Return Values:
  196. Returns true if successful.
  197. --*/
  198. {
  199. BOOL fOk = TRUE;
  200. // validate pointer
  201. if (pTLE != NULL) {
  202. // release manager structures
  203. UnloadManagers(&pTLE->Managers);
  204. // release string
  205. AgentMemFree(pTLE->pCommunity);
  206. // release structure
  207. AgentMemFree(pTLE);
  208. }
  209. return TRUE;
  210. }
  211. BOOL
  212. LoadTrapDestinations(
  213. BOOL bFirstCall
  214. )
  215. /*++
  216. Routine Description:
  217. Constructs list of trap destinations.
  218. Arguments:
  219. None.
  220. Return Values:
  221. Returns true if successful.
  222. --*/
  223. {
  224. HKEY hKey;
  225. LONG lStatus;
  226. DWORD dwIndex;
  227. WCHAR wszName[MAX_PATH+1];
  228. BOOL fPolicy;
  229. LPTSTR pszKey;
  230. BOOL fOk = FALSE;
  231. SNMPDBG((
  232. SNMP_LOG_TRACE,
  233. "SNMP: SVC: loading trap destinations.\n"
  234. ));
  235. #ifdef _POLICY
  236. // we need to provide precedence to the parameters set through the policy
  237. fPolicy = TRUE;
  238. #else
  239. fPolicy = FALSE;
  240. #endif
  241. do
  242. {
  243. // if the policy is to be enforced, check the policy registry location first
  244. pszKey = fPolicy ? REG_POLICY_TRAP_DESTINATIONS : REG_KEY_TRAP_DESTINATIONS;
  245. // open registry subkey
  246. lStatus = RegOpenKeyEx(
  247. HKEY_LOCAL_MACHINE,
  248. pszKey,
  249. 0,
  250. KEY_READ,
  251. &hKey
  252. );
  253. // if the call succeeded or we were not checking the policy, break the loop
  254. if (lStatus == ERROR_SUCCESS || !fPolicy)
  255. break;
  256. // being at this point, this means we were checking for the policy parameters.
  257. // If and only if the policy is not defined (registry key is missing) we
  258. // reset the error, mark 'fPolicy already tried' and go back into the loop
  259. if (lStatus == ERROR_FILE_NOT_FOUND)
  260. {
  261. lStatus = ERROR_SUCCESS;
  262. fPolicy = FALSE;
  263. }
  264. } while (lStatus == ERROR_SUCCESS);
  265. // validate return code
  266. if (lStatus == ERROR_SUCCESS) {
  267. // initialize
  268. dwIndex = 0;
  269. // loop until error or end of list
  270. while (lStatus == ERROR_SUCCESS) {
  271. // read next value
  272. lStatus = RegEnumKeyW(
  273. hKey,
  274. dwIndex,
  275. wszName,
  276. sizeof(wszName) / sizeof(wszName[0])
  277. );
  278. // validate return code
  279. if (lStatus == ERROR_SUCCESS) {
  280. // add trap destination to list
  281. if (AddTrapDestination(hKey, wszName)) {
  282. // next
  283. dwIndex++;
  284. } else {
  285. // reset status to reflect failure
  286. lStatus = ERROR_NOT_ENOUGH_MEMORY;
  287. }
  288. } else if (lStatus == ERROR_NO_MORE_ITEMS) {
  289. // success
  290. fOk = TRUE;
  291. }
  292. }
  293. RegCloseKey(hKey);
  294. }
  295. else
  296. // it doesn't matter how the values are, the key has to exist,
  297. // so mark as bFirstCall in order to log an event if this is not true.
  298. bFirstCall = TRUE;
  299. if (!fOk) {
  300. SNMPDBG((
  301. SNMP_LOG_ERROR,
  302. "SNMP: SVC: error %d processing TrapDestinations subkey.\n",
  303. lStatus
  304. ));
  305. // log an event only if on first call (service initialization)
  306. // otherwise, due to registry operations through regedit, the event log
  307. // might get flooded with records
  308. if (bFirstCall)
  309. // report event
  310. ReportSnmpEvent(
  311. SNMP_EVENT_INVALID_REGISTRY_KEY,
  312. 1,
  313. &pszKey,
  314. lStatus
  315. );
  316. }
  317. return fOk;
  318. }
  319. BOOL
  320. UnloadTrapDestinations(
  321. )
  322. /*++
  323. Routine Description:
  324. Destroys list of trap destinations.
  325. Arguments:
  326. None.
  327. Return Values:
  328. Returns true if successful.
  329. --*/
  330. {
  331. PLIST_ENTRY pLE;
  332. PTRAP_DESTINATION_LIST_ENTRY pTLE;
  333. // process entries until list is empty
  334. while (!IsListEmpty(&g_TrapDestinations)) {
  335. // extract next entry from head of list
  336. pLE = RemoveHeadList(&g_TrapDestinations);
  337. // retrieve pointer to trap destination structure
  338. pTLE = CONTAINING_RECORD(pLE, TRAP_DESTINATION_LIST_ENTRY, Link);
  339. // release
  340. FreeTLE(pTLE);
  341. }
  342. return TRUE;
  343. }