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.

567 lines
16 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. #include <aclapi.h>
  4. #include "netoc.h"
  5. #include "ncreg.h"
  6. #include "snmpocx.h"
  7. // Under opened registry subkey hKey,
  8. // removes the registry value name who has a value data of pwszRegValData
  9. // if it exists.
  10. HRESULT HrSnmpRegDeleteValueData(
  11. IN HKEY hKey,
  12. IN LPCWSTR pwszRegValData)
  13. {
  14. HRESULT hr = S_OK;
  15. DWORD dwIndex = 0;
  16. DWORD dwNameSize;
  17. DWORD dwValueSize;
  18. DWORD dwValueType;
  19. WCHAR wszName[MAX_PATH];
  20. WCHAR wszValue[MAX_PATH];
  21. Assert (hKey);
  22. Assert (pwszRegValData);
  23. // initialize buffer sizes
  24. dwNameSize = sizeof(wszName) / sizeof(wszName[0]); // size in number of TCHARs
  25. dwValueSize = sizeof(wszValue); // size in number of bytes
  26. // loop until error, end of list, or found subagent to be removed
  27. while (S_OK == hr)
  28. {
  29. // read next value
  30. hr = HrRegEnumValue(
  31. hKey,
  32. dwIndex,
  33. wszName,
  34. &dwNameSize,
  35. &dwValueType,
  36. (LPBYTE)wszValue,
  37. &dwValueSize
  38. );
  39. // validate return code
  40. if (S_OK == hr)
  41. {
  42. // see if the subagent value data matched the one to be removed
  43. if (!wcscmp(pwszRegValData, wszValue))
  44. {
  45. hr = HrRegDeleteValue(hKey, wszName);
  46. if (S_OK != hr)
  47. {
  48. TraceTag(ttidError,
  49. "SnmpNetOc: HrRegDeleteValue: failed to delete %S. hr = %x.",
  50. wszName, hr);
  51. }
  52. break;
  53. }
  54. // re-initialize buffer sizes
  55. dwNameSize = sizeof(wszName) / sizeof(wszName[0]); // size in number of TCHARs
  56. dwValueSize = sizeof(wszValue); // size in number of bytes
  57. // next
  58. dwIndex++;
  59. }
  60. else if (HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS) == hr)
  61. {
  62. // success
  63. break;
  64. }
  65. }
  66. return hr;
  67. }
  68. // Removes Subagent Configuration from registry according to the given
  69. // array of SUBAGENT_REMOVAL_INFO
  70. HRESULT SnmpRemoveSubAgents
  71. (
  72. IN const SUBAGENT_REMOVAL_INFO * prgSRI, // array of SUBAGENT_REMOVAL_INFO
  73. IN UINT cParams // number of elements in the arrary
  74. )
  75. {
  76. if ((NULL == prgSRI) || (0 == cParams))
  77. {
  78. TraceError( "No Subagents need to be removed.", S_FALSE);
  79. return S_FALSE;
  80. }
  81. HRESULT hr = S_OK;
  82. HRESULT hrTmp = S_OK;
  83. UINT i = 0;
  84. HKEY hKey = NULL;
  85. hr = HrRegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_KEY_EXTENSION_AGENTS,
  86. KEY_READ | KEY_WRITE, &hKey);
  87. if (S_OK != hr)
  88. {
  89. TraceTag(ttidError,
  90. "SnmpNetOc: HrRegOpenKeyEx: failed to open %S. hr = %x.",
  91. REG_KEY_EXTENSION_AGENTS, hr);
  92. return hr;
  93. }
  94. for (i = 0; i < cParams; i++)
  95. {
  96. hrTmp = S_OK;
  97. if (
  98. // removal from all SKU AND has valid registry info
  99. (!prgSRI[i].pfnFRemoveSubagent && prgSRI[i].pwszRegKey
  100. && prgSRI[i].pwszRegValData)
  101. ||
  102. // removal depending on given function and has valid registry info
  103. (prgSRI[i].pfnFRemoveSubagent &&
  104. (*(prgSRI[i].pfnFRemoveSubagent))() &&
  105. prgSRI[i].pwszRegKey && prgSRI[i].pwszRegValData)
  106. )
  107. {
  108. // removes everything under subagent registry key if it exits
  109. hrTmp = HrRegDeleteKeyTree(HKEY_LOCAL_MACHINE, prgSRI[i].pwszRegKey);
  110. if (S_OK != hrTmp)
  111. {
  112. TraceTag(ttidError,
  113. "SnmpNetOc: HrRegDeleteKeyTree: failed to delete %S. hr = %x.",
  114. prgSRI[i].pwszRegKey, hrTmp);
  115. }
  116. // Under registry subkey of REG_KEY_EXTENSION_AGENTS,
  117. // removes the registry value name who has value data identical to
  118. // pwszRegKey.
  119. hrTmp = HrSnmpRegDeleteValueData(hKey, prgSRI[i].pwszRegValData);
  120. if (S_OK != hrTmp)
  121. {
  122. TraceTag(ttidError,
  123. "SnmpNetOc: HrSnmpRegDeleteValueData: failed to delete %S value data. hr = %x.",
  124. prgSRI[i].pwszRegValData, hrTmp);
  125. }
  126. }
  127. }
  128. RegSafeCloseKey(hKey);
  129. //we dont pass the error of hrTmp out of this function because
  130. //there is not much we can do with this error
  131. return hr;
  132. }
  133. // Allocates an admin ACL to be used with security descriptor
  134. PACL AllocACL()
  135. {
  136. PACL pAcl = NULL;
  137. PSID pSidAdmins = NULL;
  138. SID_IDENTIFIER_AUTHORITY Authority = SECURITY_NT_AUTHORITY;
  139. EXPLICIT_ACCESS ea[1];
  140. // Create a SID for the BUILTIN\Administrators group.
  141. if ( !AllocateAndInitializeSid( &Authority,
  142. 2,
  143. SECURITY_BUILTIN_DOMAIN_RID,
  144. DOMAIN_ALIAS_RID_ADMINS,
  145. 0, 0, 0, 0, 0, 0,
  146. &pSidAdmins ))
  147. {
  148. return NULL;
  149. }
  150. // Initialize an EXPLICIT_ACCESS structure for an ACE.
  151. ZeroMemory(&ea, 1 * sizeof(EXPLICIT_ACCESS));
  152. // The ACE will allow the Administrators group full access to the key.
  153. ea[0].grfAccessPermissions = KEY_ALL_ACCESS;
  154. ea[0].grfAccessMode = SET_ACCESS;
  155. ea[0].grfInheritance= NO_INHERITANCE;
  156. ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  157. ea[0].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
  158. ea[0].Trustee.ptstrName = (LPTSTR) pSidAdmins;
  159. // Create a new ACL that contains the new ACEs.
  160. if (SetEntriesInAcl(1, ea, NULL, &pAcl) != ERROR_SUCCESS)
  161. {
  162. TraceError( "SetEntriesInAcl Error", GetLastError() );
  163. FreeSid(pSidAdmins);
  164. return NULL;
  165. }
  166. FreeSid(pSidAdmins);
  167. return pAcl;
  168. }
  169. // frees a ACL
  170. void FreeACL( PACL pAcl)
  171. {
  172. if (pAcl != NULL)
  173. LocalFree(pAcl);
  174. }
  175. HRESULT SnmpAddAdminAclToKey(PWSTR pwszKey)
  176. {
  177. HKEY hKey = NULL;
  178. HRESULT hr;
  179. PACL pAcl = NULL;
  180. SECURITY_DESCRIPTOR S_Desc;
  181. if (pwszKey == NULL)
  182. return S_FALSE;
  183. // open registy key
  184. hr = HrRegOpenKeyEx(HKEY_LOCAL_MACHINE,
  185. pwszKey, // subkey name
  186. KEY_ALL_ACCESS, // want WRITE_DAC,
  187. &hKey // handle to open key
  188. );
  189. if (hr != S_OK)
  190. {
  191. TraceError("SnmpAddAdminDaclToKey::HrRegOpenKeyEx", hr);
  192. return hr;
  193. }
  194. // Initialize a security descriptor.
  195. if (InitializeSecurityDescriptor (&S_Desc, SECURITY_DESCRIPTOR_REVISION) == 0)
  196. {
  197. RegSafeCloseKey(hKey);
  198. TraceError("SnmpAddAdminDaclToKey::InitializeSecurityDescriptor", GetLastError());
  199. return S_FALSE;
  200. }
  201. // get the ACL and put it into the security descriptor
  202. if ( (pAcl = AllocACL()) != NULL )
  203. {
  204. if (!SetSecurityDescriptorDacl (&S_Desc, TRUE, pAcl, FALSE))
  205. {
  206. FreeACL(pAcl);
  207. RegSafeCloseKey(hKey);
  208. TraceError("SnmpAddAdminDaclToKey::SetSecurityDescriptorDacl Failed.", GetLastError());
  209. return S_FALSE;
  210. }
  211. }
  212. else
  213. {
  214. RegSafeCloseKey(hKey);
  215. TraceError("SnmpAddAdminAclToKey::AllocACL Failed.", GetLastError());
  216. return S_FALSE;
  217. }
  218. if (RegSetKeySecurity (hKey, DACL_SECURITY_INFORMATION, &S_Desc) != ERROR_SUCCESS)
  219. {
  220. FreeACL(pAcl);
  221. RegSafeCloseKey(hKey);
  222. TraceError("SnmpAddAdminDaclToKey::RegSetKeySecurity", GetLastError());
  223. return S_FALSE;
  224. }
  225. FreeACL(pAcl);
  226. RegSafeCloseKey(hKey);
  227. return S_OK;
  228. }
  229. HRESULT
  230. SnmpRegWriteDword(PWSTR pszRegKey,
  231. PWSTR pszValueName,
  232. DWORD dwValueData)
  233. {
  234. HRESULT hr;
  235. HKEY hKey;
  236. hr = HrRegCreateKeyEx(HKEY_LOCAL_MACHINE,
  237. pszRegKey,
  238. 0,
  239. KEY_QUERY_VALUE | KEY_SET_VALUE,
  240. NULL,
  241. &hKey,
  242. NULL);
  243. if (hr != S_OK)
  244. {
  245. return hr;
  246. }
  247. hr = HrRegSetDword(hKey, pszValueName, dwValueData);
  248. RegSafeCloseKey(hKey);
  249. return hr;
  250. }
  251. HRESULT
  252. SnmpRegUpgEnableAuthTraps()
  253. {
  254. HRESULT hr = S_OK;
  255. HKEY hKey;
  256. // open the ..SNMP\Parameters registry key
  257. hr = HrRegOpenKeyEx(HKEY_LOCAL_MACHINE,
  258. REG_KEY_AUTHENTICATION_TRAPS,
  259. KEY_QUERY_VALUE,
  260. &hKey);
  261. // if successful, look for EnableAuthenticationTrap switch
  262. // in the old registry location
  263. if (hr == S_OK)
  264. {
  265. DWORD dwAuthTrap;
  266. // get the value of the old 'switch' parameter
  267. hr = HrRegQueryDword(hKey,
  268. REG_VALUE_SWITCH,
  269. &dwAuthTrap);
  270. // if successful transfer the value to the new location
  271. // if this fails, it means the SNMP service worked with the default value
  272. // which is already installed through the inf file.
  273. if (hr == S_OK)
  274. {
  275. hr = SnmpRegWriteDword(REG_KEY_SNMP_PARAMETERS,
  276. REG_VALUE_AUTHENTICATION_TRAPS,
  277. dwAuthTrap);
  278. }
  279. // close and delete the old registry key as it is obsolete
  280. RegSafeCloseKey(hKey);
  281. HrRegDeleteKey (HKEY_LOCAL_MACHINE,
  282. REG_KEY_AUTHENTICATION_TRAPS);
  283. }
  284. return hr;
  285. }
  286. HRESULT
  287. SnmpRegWriteCommunities(PWSTR pszCommArray)
  288. {
  289. HRESULT hr;
  290. HKEY hKey;
  291. PWSTR pszComm, pszAccess;
  292. DWORD dwAccess;
  293. hr = HrRegDeleteKey(HKEY_LOCAL_MACHINE,
  294. REG_KEY_VALID_COMMUNITIES);
  295. if (hr != S_OK)
  296. {
  297. return hr;
  298. }
  299. hr = HrRegCreateKeyEx(HKEY_LOCAL_MACHINE,
  300. REG_KEY_VALID_COMMUNITIES,
  301. 0,
  302. KEY_QUERY_VALUE | KEY_SET_VALUE,
  303. NULL,
  304. &hKey,
  305. NULL);
  306. if (hr != S_OK)
  307. {
  308. return hr;
  309. }
  310. pszComm = pszCommArray;
  311. while (*pszComm != L'\0')
  312. {
  313. dwAccess = SEC_READ_ONLY_VALUE;
  314. pszAccess = wcschr(pszComm, L':');
  315. if (pszAccess != NULL)
  316. {
  317. *pszAccess = L'\0';
  318. pszAccess++;
  319. if (_wcsicmp(pszAccess, SEC_NONE_NAME)==0)
  320. {
  321. dwAccess = SEC_NONE_VALUE;
  322. }
  323. if (_wcsicmp(pszAccess, SEC_NOTIFY_NAME)==0)
  324. {
  325. dwAccess = SEC_NOTIFY_VALUE;
  326. }
  327. if (_wcsicmp(pszAccess, SEC_READ_ONLY_NAME)==0)
  328. {
  329. dwAccess = SEC_READ_ONLY_VALUE;
  330. }
  331. if (_wcsicmp(pszAccess, SEC_READ_WRITE_NAME)==0)
  332. {
  333. dwAccess = SEC_READ_WRITE_VALUE;
  334. }
  335. if (_wcsicmp(pszAccess, SEC_READ_CREATE_NAME)==0)
  336. {
  337. dwAccess = SEC_READ_CREATE_VALUE;
  338. }
  339. }
  340. hr = HrRegSetDword(hKey, pszComm, dwAccess);
  341. if (hr != S_OK)
  342. {
  343. break;
  344. }
  345. if (pszAccess != NULL)
  346. {
  347. pszComm = pszAccess;
  348. }
  349. pszComm += (wcslen(pszComm) + 1);
  350. }
  351. RegSafeCloseKey(hKey);
  352. return hr;
  353. }
  354. HRESULT SnmpRegWritePermittedMgrs(BOOL bAnyHost,
  355. PWSTR pMgrsList)
  356. {
  357. HRESULT hr;
  358. HKEY hKey;
  359. UINT nMgr = 1;
  360. WCHAR szMgr[16];
  361. hr = HrRegDeleteKey(HKEY_LOCAL_MACHINE,
  362. REG_KEY_PERMITTED_MANAGERS);
  363. if (hr != S_OK)
  364. {
  365. return hr;
  366. }
  367. hr = HrRegCreateKeyEx(HKEY_LOCAL_MACHINE,
  368. REG_KEY_PERMITTED_MANAGERS,
  369. 0,
  370. KEY_QUERY_VALUE | KEY_SET_VALUE,
  371. NULL,
  372. &hKey,
  373. NULL);
  374. if (hr != S_OK)
  375. {
  376. return hr;
  377. }
  378. if (bAnyHost)
  379. {
  380. RegSafeCloseKey(hKey);
  381. return hr;
  382. }
  383. AssertSz(pMgrsList, "pMgrsList is NULL and bAnyHost is FALSE in SnmpRegWritePermittedMgrs");
  384. while(*pMgrsList != L'\0')
  385. {
  386. swprintf(szMgr, L"%d", nMgr++);
  387. hr = HrRegSetSz(hKey, szMgr, pMgrsList);
  388. if (hr != S_OK)
  389. break;
  390. pMgrsList += wcslen(pMgrsList) + 1;
  391. }
  392. RegSafeCloseKey(hKey);
  393. return hr;
  394. }
  395. HRESULT
  396. SnmpRegWriteTraps(tstring tstrVariable,
  397. PWSTR pTstrArray)
  398. {
  399. HKEY hKey, hKeyTrap;
  400. HRESULT hr = S_OK;
  401. UINT nTrap = 1;
  402. WCHAR szTrap[16];
  403. hr = HrRegDeleteKeyTree(HKEY_LOCAL_MACHINE,
  404. REG_KEY_TRAP_DESTINATIONS);
  405. if (hr != S_OK)
  406. return hr;
  407. hr = HrRegCreateKeyEx(HKEY_LOCAL_MACHINE,
  408. REG_KEY_TRAP_DESTINATIONS,
  409. 0,
  410. KEY_QUERY_VALUE | KEY_SET_VALUE,
  411. NULL,
  412. &hKey,
  413. NULL);
  414. if (hr != S_OK)
  415. return hr;
  416. hr = HrRegCreateKeyEx(hKey,
  417. tstrVariable.c_str(),
  418. 0,
  419. KEY_QUERY_VALUE | KEY_SET_VALUE,
  420. NULL,
  421. &hKeyTrap,
  422. NULL);
  423. if (hr == S_OK)
  424. {
  425. // it might just happen that you want to create a
  426. // community but you don't have the trap destination
  427. // addresses yet. We should let this happen.
  428. if (pTstrArray != NULL)
  429. {
  430. while(*pTstrArray != L'\0')
  431. {
  432. swprintf(szTrap, L"%d", nTrap++);
  433. hr = HrRegSetSz(hKeyTrap, szTrap, pTstrArray);
  434. if (hr != S_OK)
  435. break;
  436. pTstrArray += wcslen(pTstrArray) + 1;
  437. }
  438. }
  439. RegSafeCloseKey(hKeyTrap);
  440. }
  441. RegSafeCloseKey(hKey);
  442. return hr;
  443. }
  444. HRESULT
  445. SnmpRegWriteTstring(PWSTR pRegKey,
  446. PWSTR pValueName,
  447. tstring tstrValueData)
  448. {
  449. HRESULT hr = S_OK;
  450. HKEY hKey;
  451. hr = HrRegCreateKeyEx(HKEY_LOCAL_MACHINE,
  452. pRegKey,
  453. 0,
  454. KEY_QUERY_VALUE | KEY_SET_VALUE,
  455. NULL,
  456. &hKey,
  457. NULL);
  458. if (hr != S_OK)
  459. return hr;
  460. hr = HrRegSetString(hKey, pValueName, tstrValueData);
  461. RegSafeCloseKey(hKey);
  462. return hr;
  463. }
  464. DWORD
  465. SnmpStrArrayToServices(PWSTR pSrvArray)
  466. {
  467. DWORD dwServices = 0;
  468. while(*pSrvArray)
  469. {
  470. if(_wcsicmp(pSrvArray, SRV_AGNT_PHYSICAL_NAME)==0)
  471. dwServices |= SRV_AGNT_PHYSICAL_VALUE;
  472. if(_wcsicmp(pSrvArray, SRV_AGNT_DATALINK_NAME)==0)
  473. dwServices |= SRV_AGNT_DATALINK_VALUE;
  474. if(_wcsicmp(pSrvArray, SRV_AGNT_ENDTOEND_NAME)==0)
  475. dwServices |= SRV_AGNT_ENDTOEND_VALUE;
  476. if(_wcsicmp(pSrvArray, SRV_AGNT_INTERNET_NAME)==0)
  477. dwServices |= SRV_AGNT_INTERNET_VALUE;
  478. if(_wcsicmp(pSrvArray, SRV_AGNT_APPLICATIONS_NAME)==0)
  479. dwServices |= SRV_AGNT_APPLICATIONS_VALUE;
  480. pSrvArray += wcslen(pSrvArray) + 1;
  481. }
  482. return dwServices;
  483. }