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.

452 lines
13 KiB

  1. #include <stdafx.h>
  2. #include "Errors.h"
  3. #include "EventCmd.h"
  4. #include "Registry.h"
  5. #include "Operation.h"
  6. CRegistry gRegistry;
  7. DWORD CRegistry::ConfigureRegSource(HKEY hRegSource, char *szEventSource)
  8. {
  9. DWORD retCode;
  10. char *szSourceOID;
  11. char *szEventDup;
  12. int nIndex;
  13. DWORD dwAppend;
  14. szSourceOID = new char[4*strlen(szEventSource) + 9];
  15. if (szSourceOID == NULL)
  16. return _E(ERROR_OUTOFMEMORY, IDS_ERR01);
  17. nIndex = sprintf(szSourceOID, "%u.", strlen(szEventSource));
  18. for (szEventDup = szEventSource; *szEventDup != '\0'; szEventDup++)
  19. nIndex += sprintf(szSourceOID+nIndex,"%d.",*szEventDup);
  20. if (nIndex == 0)
  21. {
  22. retCode = _E(ERROR_FUNCTION_FAILED, IDS_ERR05, szEventSource);
  23. goto done;
  24. }
  25. szSourceOID[--nIndex]='\0';
  26. retCode = RegSetValueEx(
  27. hRegSource,
  28. REG_SRC_ENTOID,
  29. 0,
  30. REG_SZ,
  31. (const BYTE *)szSourceOID,
  32. nIndex);
  33. if (retCode != ERROR_SUCCESS)
  34. {
  35. retCode = _E(retCode, IDS_ERR11, REG_SRC_ENTOID, szEventSource);
  36. goto done;
  37. }
  38. dwAppend = 1;
  39. retCode = RegSetValueEx(
  40. hRegSource,
  41. REG_SRC_APPEND,
  42. 0,
  43. REG_DWORD,
  44. (const BYTE *)&dwAppend,
  45. sizeof(DWORD));
  46. if (retCode != ERROR_SUCCESS)
  47. {
  48. retCode = _E(retCode, IDS_ERR11, REG_SRC_APPEND, szEventSource);
  49. goto done;
  50. }
  51. _W(WARN_TRACK, IDS_TRCK_WRN33, szEventSource);
  52. done:
  53. delete szSourceOID;
  54. return retCode;
  55. }
  56. DWORD CRegistry::ConfigureRegEvent(HKEY hRegEvent, DWORD dwEventID, DWORD dwCount, DWORD dwTime)
  57. {
  58. DWORD retCode = ERROR_SUCCESS;
  59. retCode = RegSetValueEx(
  60. hRegEvent,
  61. REG_EVNT_ID,
  62. 0,
  63. REG_DWORD,
  64. (const BYTE *)&dwEventID,
  65. sizeof(DWORD));
  66. if (retCode != ERROR_SUCCESS)
  67. return _E(retCode, IDS_ERR13, REG_EVNT_ID, dwEventID);
  68. retCode = RegSetValueEx(
  69. hRegEvent,
  70. REG_EVNT_COUNT,
  71. 0,
  72. REG_DWORD,
  73. (const BYTE *)&dwCount,
  74. sizeof(DWORD));
  75. if (retCode != ERROR_SUCCESS)
  76. return _E(retCode, IDS_ERR13, REG_EVNT_COUNT, dwEventID);
  77. retCode = RegSetValueEx(
  78. hRegEvent,
  79. REG_EVNT_TIME,
  80. 0,
  81. REG_DWORD,
  82. (const BYTE *)&dwTime,
  83. sizeof(DWORD));
  84. if (retCode != ERROR_SUCCESS)
  85. return _E(retCode, IDS_ERR13, REG_EVNT_TIME, dwEventID);
  86. return retCode;
  87. }
  88. DWORD CRegistry::ScanForTrap(HKEY hRegCommunity, char *szAddress, char *szName, DWORD & nNameLen)
  89. {
  90. DWORD retCode;
  91. DWORD nMaxName = 1;
  92. char szDataBuffer[64];
  93. for (DWORD i = 0, iNameLen = nNameLen, iDataLen = 64;
  94. (retCode = RegEnumValue(
  95. hRegCommunity,
  96. i,
  97. szName,
  98. &iNameLen,
  99. 0,
  100. NULL,
  101. (BYTE *)szDataBuffer,
  102. &iDataLen)) == ERROR_SUCCESS;
  103. i++, iNameLen = nNameLen, iDataLen = 64)
  104. {
  105. DWORD nNameNum;
  106. if (strcmp(szDataBuffer, szAddress) == 0)
  107. return ERROR_ALREADY_EXISTS;
  108. nNameNum = atoi(szName);
  109. if (nMaxName <= nNameNum)
  110. nMaxName = nNameNum+1;
  111. }
  112. if (retCode != ERROR_NO_MORE_ITEMS)
  113. return _E(retCode, IDS_ERR12);
  114. nNameLen = sprintf(szName, "%u", nMaxName);
  115. retCode = ERROR_SUCCESS;
  116. return retCode;
  117. }
  118. CRegistry::CRegistry()
  119. {
  120. m_hRegRoot = HKEY_LOCAL_MACHINE;
  121. m_hRegSnmpTraps = NULL;
  122. m_hRegEvntSources = NULL;
  123. m_dwFlags = 0;
  124. }
  125. CRegistry::~CRegistry()
  126. {
  127. if (m_hRegRoot != HKEY_LOCAL_MACHINE)
  128. RegCloseKey(m_hRegRoot);
  129. if (m_hRegSnmpTraps != NULL)
  130. RegCloseKey(m_hRegSnmpTraps);
  131. if (m_hRegEvntSources != NULL)
  132. RegCloseKey(m_hRegEvntSources);
  133. }
  134. DWORD CRegistry::Connect()
  135. {
  136. DWORD retCode = ERROR_SUCCESS;
  137. if (gCommandLine.m_szSystem != NULL)
  138. {
  139. _W(WARN_ATTENTION,IDS_ATTN_WRN34, gCommandLine.m_szSystem);
  140. retCode = RegConnectRegistry(gCommandLine.m_szSystem, HKEY_LOCAL_MACHINE, &m_hRegRoot);
  141. }
  142. if (retCode != ERROR_SUCCESS)
  143. return _E(retCode, IDS_ERR14, gCommandLine.m_szSystem);
  144. return retCode;
  145. }
  146. DWORD CRegistry::AddEvent(char *szEventSource, DWORD dwEventID, DWORD dwCount, DWORD dwTime)
  147. {
  148. DWORD retCode;
  149. DWORD dwDisposition;
  150. HKEY hRegSource;
  151. HKEY hRegEvent;
  152. char szEventID[64];
  153. if (m_hRegEvntSources == NULL)
  154. {
  155. retCode = RegOpenKeyEx(
  156. m_hRegRoot,
  157. REGPATH_EVNTAGENT,
  158. 0,
  159. KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_WRITE,
  160. &m_hRegEvntSources);
  161. if (retCode != ERROR_SUCCESS)
  162. return _E(retCode, IDS_ERR15);
  163. }
  164. retCode = RegCreateKeyEx(
  165. m_hRegEvntSources,
  166. szEventSource,
  167. 0,
  168. NULL,
  169. REG_OPTION_NON_VOLATILE,
  170. KEY_ALL_ACCESS,
  171. NULL,
  172. &hRegSource,
  173. &dwDisposition);
  174. if (retCode != ERROR_SUCCESS)
  175. return _E(retCode, IDS_ERR16, szEventSource);
  176. if (dwDisposition == REG_CREATED_NEW_KEY)
  177. {
  178. retCode = ConfigureRegSource(hRegSource, szEventSource);
  179. if (retCode != ERROR_SUCCESS)
  180. {
  181. RegCloseKey(hRegSource);
  182. return retCode;
  183. }
  184. }
  185. sprintf(szEventID,"%u",dwEventID);
  186. retCode = RegCreateKeyEx(
  187. hRegSource,
  188. szEventID,
  189. 0,
  190. NULL,
  191. REG_OPTION_NON_VOLATILE,
  192. KEY_ALL_ACCESS,
  193. NULL,
  194. &hRegEvent,
  195. &dwDisposition);
  196. if (retCode != ERROR_SUCCESS)
  197. {
  198. RegCloseKey(hRegSource);
  199. return _E(retCode, IDS_ERR17, szEventSource);
  200. }
  201. retCode = ConfigureRegEvent(hRegEvent, dwEventID, dwCount, dwTime);
  202. _W(WARN_ATTENTION, IDS_ATTN_WRN35,
  203. dwDisposition == REG_CREATED_NEW_KEY ? "new" : "existing",
  204. dwEventID);
  205. RegCloseKey(hRegSource);
  206. RegCloseKey(hRegEvent);
  207. return retCode;
  208. }
  209. DWORD CRegistry::DelEvent(char *szEventSource, DWORD dwEventID)
  210. {
  211. DWORD retCode;
  212. char szEventID[64];
  213. DWORD nSzEventID = 64;
  214. HKEY hRegSource;
  215. FILETIME ft;
  216. if (m_hRegEvntSources == NULL)
  217. {
  218. retCode = RegOpenKeyEx(
  219. m_hRegRoot,
  220. REGPATH_EVNTAGENT,
  221. 0,
  222. KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_WRITE,
  223. &m_hRegEvntSources);
  224. if (retCode != ERROR_SUCCESS)
  225. return _E(retCode, IDS_ERR18);
  226. }
  227. retCode = RegOpenKeyEx(
  228. m_hRegEvntSources,
  229. szEventSource,
  230. 0,
  231. KEY_ENUMERATE_SUB_KEYS | KEY_WRITE,
  232. &hRegSource);
  233. if (retCode != ERROR_SUCCESS)
  234. {
  235. _W(WARN_ATTENTION, IDS_ATTN_WRN36, szEventSource);
  236. return ERROR_SUCCESS;
  237. }
  238. sprintf(szEventID,"%u",dwEventID);
  239. retCode = RegDeleteKey(hRegSource, szEventID);
  240. if (retCode != ERROR_SUCCESS)
  241. {
  242. _W(WARN_ATTENTION, IDS_ATTN_WRN37, szEventID);
  243. RegCloseKey(hRegSource);
  244. return ERROR_SUCCESS;
  245. }
  246. _W(WARN_ATTENTION, IDS_ATTN_WRN38, dwEventID);
  247. retCode = RegEnumKeyEx(
  248. hRegSource,
  249. 0,
  250. szEventID,
  251. &nSzEventID,
  252. 0,
  253. NULL,
  254. NULL,
  255. &ft);
  256. RegCloseKey(hRegSource);
  257. if (retCode == ERROR_NO_MORE_ITEMS)
  258. {
  259. retCode = RegDeleteKey(m_hRegEvntSources, szEventSource);
  260. if (retCode != ERROR_SUCCESS)
  261. return _E(retCode, IDS_ERR19, szEventSource);
  262. _W(WARN_TRACK, IDS_TRCK_WRN39, szEventSource);
  263. retCode = ERROR_SUCCESS;
  264. }
  265. else if (retCode != ERROR_SUCCESS)
  266. {
  267. _W(WARN_TRACK, IDS_TRCK_WRN40, retCode, szEventSource);
  268. retCode = ERROR_SUCCESS;
  269. }
  270. return retCode;
  271. }
  272. DWORD CRegistry::AddTrap(char *szCommunity, char *szAddress)
  273. {
  274. DWORD retCode;
  275. DWORD dwDisposition;
  276. char szTrapName[64];
  277. DWORD nLenTrapName = 64;
  278. HKEY hRegCommunity;
  279. if (m_hRegSnmpTraps == NULL)
  280. {
  281. retCode = RegOpenKeyEx(
  282. m_hRegRoot,
  283. REGPATH_SNMPTRAPS,
  284. 0,
  285. KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_WRITE,
  286. &m_hRegSnmpTraps);
  287. if (retCode != ERROR_SUCCESS)
  288. return _E(retCode, IDS_ERR20);
  289. }
  290. retCode = RegCreateKeyEx(
  291. m_hRegSnmpTraps,
  292. szCommunity,
  293. 0,
  294. NULL,
  295. REG_OPTION_NON_VOLATILE,
  296. KEY_ALL_ACCESS,
  297. NULL,
  298. &hRegCommunity,
  299. &dwDisposition);
  300. if (retCode != ERROR_SUCCESS)
  301. return _E(retCode, IDS_ERR21, szCommunity);
  302. if (dwDisposition == REG_CREATED_NEW_KEY)
  303. _W(WARN_TRACK, IDS_TRCK_WRN41, szCommunity);
  304. retCode = ScanForTrap(hRegCommunity, szAddress, szTrapName, nLenTrapName);
  305. if (retCode != ERROR_SUCCESS)
  306. {
  307. RegCloseKey(hRegCommunity);
  308. if (retCode == ERROR_ALREADY_EXISTS)
  309. {
  310. _W(WARN_ATTENTION, IDS_ATTN_WRN42, szAddress, szTrapName);
  311. retCode = ERROR_SUCCESS;
  312. }
  313. return retCode;
  314. }
  315. retCode = RegSetValueEx(
  316. hRegCommunity,
  317. szTrapName,
  318. 0,
  319. REG_SZ,
  320. (const BYTE*)szAddress,
  321. strlen(szAddress));
  322. if (retCode != ERROR_SUCCESS)
  323. _E(retCode, IDS_ERR22, szAddress);
  324. else
  325. {
  326. m_dwFlags |= REG_FLG_NEEDRESTART;
  327. _W(WARN_ATTENTION, IDS_ATTN_WRN43, szAddress);
  328. }
  329. RegCloseKey(hRegCommunity);
  330. return retCode;
  331. }
  332. DWORD CRegistry::DelTrap(char *szCommunity, char *szAddress)
  333. {
  334. DWORD retCode;
  335. char szTrapName[64];
  336. DWORD nLenTrapName = 64;
  337. HKEY hRegCommunity;
  338. if (m_hRegSnmpTraps == NULL)
  339. {
  340. retCode = RegOpenKeyEx(
  341. m_hRegRoot,
  342. REGPATH_SNMPTRAPS,
  343. 0,
  344. KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_WRITE,
  345. &m_hRegSnmpTraps);
  346. if (retCode != ERROR_SUCCESS)
  347. return _E(retCode, IDS_ERR23);
  348. }
  349. if ((retCode = RegOpenKeyEx(
  350. m_hRegSnmpTraps,
  351. szCommunity,
  352. 0,
  353. KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_WRITE,
  354. &hRegCommunity)) == ERROR_SUCCESS &&
  355. (retCode = ScanForTrap(hRegCommunity, szAddress, szTrapName, nLenTrapName)) == ERROR_ALREADY_EXISTS)
  356. {
  357. retCode = RegDeleteValue(
  358. hRegCommunity,
  359. szTrapName);
  360. if (retCode != ERROR_SUCCESS)
  361. {
  362. RegCloseKey(hRegCommunity);
  363. return _E(retCode, IDS_ERR24, szAddress);
  364. }
  365. else
  366. {
  367. m_dwFlags |= REG_FLG_NEEDRESTART;
  368. _W(WARN_ATTENTION, IDS_ATTN_WRN44, szAddress);
  369. }
  370. }
  371. else
  372. {
  373. _W(WARN_ATTENTION, IDS_ATTN_WRN45, szAddress, szCommunity);
  374. retCode = ERROR_SUCCESS;
  375. }
  376. retCode = RegEnumValue(
  377. hRegCommunity,
  378. 0,
  379. szTrapName,
  380. &nLenTrapName,
  381. 0,
  382. NULL,
  383. NULL,
  384. NULL);
  385. RegCloseKey(hRegCommunity);
  386. if (retCode == ERROR_NO_MORE_ITEMS)
  387. {
  388. retCode = RegDeleteKey(m_hRegSnmpTraps, szCommunity);
  389. if (retCode != ERROR_SUCCESS)
  390. _W(WARN_ERROR, IDS_ERRO_WRN46, retCode, szCommunity);
  391. else
  392. _W(WARN_TRACK, IDS_TRCK_WRN47, szCommunity);
  393. retCode = ERROR_SUCCESS;
  394. }
  395. else if (retCode != ERROR_SUCCESS)
  396. {
  397. _W(WARN_TRACK, IDS_TRCK_WRN48, retCode, szCommunity);
  398. retCode = ERROR_SUCCESS;
  399. }
  400. return retCode;
  401. }