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.

444 lines
11 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. // Allocates an admin ACL to be used with security descriptor
  8. PACL AllocACL()
  9. {
  10. PACL pAcl = NULL;
  11. PSID pSidAdmins = NULL;
  12. SID_IDENTIFIER_AUTHORITY Authority = SECURITY_NT_AUTHORITY;
  13. EXPLICIT_ACCESS ea[1];
  14. // Create a SID for the BUILTIN\Administrators group.
  15. if ( !AllocateAndInitializeSid( &Authority,
  16. 2,
  17. SECURITY_BUILTIN_DOMAIN_RID,
  18. DOMAIN_ALIAS_RID_ADMINS,
  19. 0, 0, 0, 0, 0, 0,
  20. &pSidAdmins ))
  21. {
  22. return NULL;
  23. }
  24. // Initialize an EXPLICIT_ACCESS structure for an ACE.
  25. ZeroMemory(&ea, 1 * sizeof(EXPLICIT_ACCESS));
  26. // The ACE will allow the Administrators group full access to the key.
  27. ea[0].grfAccessPermissions = KEY_ALL_ACCESS;
  28. ea[0].grfAccessMode = SET_ACCESS;
  29. ea[0].grfInheritance= NO_INHERITANCE;
  30. ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  31. ea[0].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
  32. ea[0].Trustee.ptstrName = (LPTSTR) pSidAdmins;
  33. // Create a new ACL that contains the new ACEs.
  34. if (SetEntriesInAcl(1, ea, NULL, &pAcl) != ERROR_SUCCESS)
  35. {
  36. TraceError( "SetEntriesInAcl Error", GetLastError() );
  37. FreeSid(pSidAdmins);
  38. return NULL;
  39. }
  40. FreeSid(pSidAdmins);
  41. return pAcl;
  42. }
  43. // frees a ACL
  44. void FreeACL( PACL pAcl)
  45. {
  46. if (pAcl != NULL)
  47. LocalFree(pAcl);
  48. }
  49. HRESULT SnmpAddAdminAclToKey(PWSTR pwszKey)
  50. {
  51. HKEY hKey = NULL;
  52. HRESULT hr;
  53. PACL pAcl = NULL;
  54. SECURITY_DESCRIPTOR S_Desc;
  55. if (pwszKey == NULL)
  56. return S_FALSE;
  57. // open registy key
  58. hr = HrRegOpenKeyEx(HKEY_LOCAL_MACHINE,
  59. pwszKey, // subkey name
  60. KEY_ALL_ACCESS, // want WRITE_DAC,
  61. &hKey // handle to open key
  62. );
  63. if (hr != S_OK)
  64. {
  65. TraceError("SnmpAddAdminDaclToKey::HrRegOpenKeyEx", hr);
  66. return hr;
  67. }
  68. // Initialize a security descriptor.
  69. if (InitializeSecurityDescriptor (&S_Desc, SECURITY_DESCRIPTOR_REVISION) == 0)
  70. {
  71. RegSafeCloseKey(hKey);
  72. TraceError("SnmpAddAdminDaclToKey::InitializeSecurityDescriptor", GetLastError());
  73. return S_FALSE;
  74. }
  75. // get the ACL and put it into the security descriptor
  76. if ( (pAcl = AllocACL()) != NULL )
  77. {
  78. if (!SetSecurityDescriptorDacl (&S_Desc, TRUE, pAcl, FALSE))
  79. {
  80. FreeACL(pAcl);
  81. RegSafeCloseKey(hKey);
  82. TraceError("SnmpAddAdminDaclToKey::SetSecurityDescriptorDacl Failed.", GetLastError());
  83. return S_FALSE;
  84. }
  85. }
  86. else
  87. {
  88. RegSafeCloseKey(hKey);
  89. TraceError("SnmpAddAdminAclToKey::AllocACL Failed.", GetLastError());
  90. return S_FALSE;
  91. }
  92. if (RegSetKeySecurity (hKey, DACL_SECURITY_INFORMATION, &S_Desc) != ERROR_SUCCESS)
  93. {
  94. FreeACL(pAcl);
  95. RegSafeCloseKey(hKey);
  96. TraceError("SnmpAddAdminDaclToKey::RegSetKeySecurity", GetLastError());
  97. return S_FALSE;
  98. }
  99. FreeACL(pAcl);
  100. RegSafeCloseKey(hKey);
  101. return S_OK;
  102. }
  103. HRESULT
  104. SnmpRegWriteDword(PWSTR pszRegKey,
  105. PWSTR pszValueName,
  106. DWORD dwValueData)
  107. {
  108. HRESULT hr;
  109. HKEY hKey;
  110. hr = HrRegCreateKeyEx(HKEY_LOCAL_MACHINE,
  111. pszRegKey,
  112. 0,
  113. KEY_QUERY_VALUE | KEY_SET_VALUE,
  114. NULL,
  115. &hKey,
  116. NULL);
  117. if (hr != S_OK)
  118. {
  119. return hr;
  120. }
  121. hr = HrRegSetDword(hKey, pszValueName, dwValueData);
  122. RegSafeCloseKey(hKey);
  123. return hr;
  124. }
  125. HRESULT
  126. SnmpRegUpgEnableAuthTraps()
  127. {
  128. HRESULT hr = S_OK;
  129. HKEY hKey;
  130. // open the ..SNMP\Parameters registry key
  131. hr = HrRegOpenKeyEx(HKEY_LOCAL_MACHINE,
  132. REG_KEY_AUTHENTICATION_TRAPS,
  133. KEY_QUERY_VALUE,
  134. &hKey);
  135. // if successful, look for EnableAuthenticationTrap switch
  136. // in the old registry location
  137. if (hr == S_OK)
  138. {
  139. DWORD dwAuthTrap;
  140. // get the value of the old 'switch' parameter
  141. hr = HrRegQueryDword(hKey,
  142. REG_VALUE_SWITCH,
  143. &dwAuthTrap);
  144. // if successful transfer the value to the new location
  145. // if this fails, it means the SNMP service worked with the default value
  146. // which is already installed through the inf file.
  147. if (hr == S_OK)
  148. {
  149. hr = SnmpRegWriteDword(REG_KEY_SNMP_PARAMETERS,
  150. REG_VALUE_AUTHENTICATION_TRAPS,
  151. dwAuthTrap);
  152. }
  153. // close and delete the old registry key as it is obsolete
  154. RegSafeCloseKey(hKey);
  155. HrRegDeleteKey (HKEY_LOCAL_MACHINE,
  156. REG_KEY_AUTHENTICATION_TRAPS);
  157. }
  158. return hr;
  159. }
  160. HRESULT
  161. SnmpRegWriteCommunities(PWSTR pszCommArray)
  162. {
  163. HRESULT hr;
  164. HKEY hKey;
  165. PWSTR pszComm, pszAccess;
  166. DWORD dwAccess;
  167. hr = HrRegDeleteKey(HKEY_LOCAL_MACHINE,
  168. REG_KEY_VALID_COMMUNITIES);
  169. if (hr != S_OK)
  170. {
  171. return hr;
  172. }
  173. hr = HrRegCreateKeyEx(HKEY_LOCAL_MACHINE,
  174. REG_KEY_VALID_COMMUNITIES,
  175. 0,
  176. KEY_QUERY_VALUE | KEY_SET_VALUE,
  177. NULL,
  178. &hKey,
  179. NULL);
  180. if (hr != S_OK)
  181. {
  182. return hr;
  183. }
  184. pszComm = pszCommArray;
  185. while (*pszComm != L'\0')
  186. {
  187. dwAccess = SEC_READ_ONLY_VALUE;
  188. pszAccess = wcschr(pszComm, L':');
  189. if (pszAccess != NULL)
  190. {
  191. *pszAccess = L'\0';
  192. pszAccess++;
  193. if (_wcsicmp(pszAccess, SEC_NONE_NAME)==0)
  194. {
  195. dwAccess = SEC_NONE_VALUE;
  196. }
  197. if (_wcsicmp(pszAccess, SEC_NOTIFY_NAME)==0)
  198. {
  199. dwAccess = SEC_NOTIFY_VALUE;
  200. }
  201. if (_wcsicmp(pszAccess, SEC_READ_ONLY_NAME)==0)
  202. {
  203. dwAccess = SEC_READ_ONLY_VALUE;
  204. }
  205. if (_wcsicmp(pszAccess, SEC_READ_WRITE_NAME)==0)
  206. {
  207. dwAccess = SEC_READ_WRITE_VALUE;
  208. }
  209. if (_wcsicmp(pszAccess, SEC_READ_CREATE_NAME)==0)
  210. {
  211. dwAccess = SEC_READ_CREATE_VALUE;
  212. }
  213. }
  214. hr = HrRegSetDword(hKey, pszComm, dwAccess);
  215. if (hr != S_OK)
  216. {
  217. break;
  218. }
  219. if (pszAccess != NULL)
  220. {
  221. pszComm = pszAccess;
  222. }
  223. pszComm += (wcslen(pszComm) + 1);
  224. }
  225. RegSafeCloseKey(hKey);
  226. return hr;
  227. }
  228. HRESULT SnmpRegWriteDefCommunity()
  229. {
  230. HRESULT hr;
  231. HKEY hKey;
  232. hr = HrRegCreateKeyEx(HKEY_LOCAL_MACHINE,
  233. REG_KEY_VALID_COMMUNITIES,
  234. 0,
  235. KEY_QUERY_VALUE | KEY_SET_VALUE,
  236. NULL,
  237. &hKey,
  238. NULL);
  239. if (hr != S_OK)
  240. {
  241. return hr;
  242. }
  243. hr = HrRegSetDword(hKey, SEC_DEF_COMM_NAME, SEC_DEF_COMM_VALUE);
  244. RegSafeCloseKey(hKey);
  245. return hr;
  246. }
  247. HRESULT SnmpRegWritePermittedMgrs(BOOL bAnyHost,
  248. PWSTR pMgrsList)
  249. {
  250. HRESULT hr;
  251. HKEY hKey;
  252. UINT nMgr = 1;
  253. WCHAR szMgr[16];
  254. hr = HrRegDeleteKey(HKEY_LOCAL_MACHINE,
  255. REG_KEY_PERMITTED_MANAGERS);
  256. if (hr != S_OK)
  257. {
  258. return hr;
  259. }
  260. hr = HrRegCreateKeyEx(HKEY_LOCAL_MACHINE,
  261. REG_KEY_PERMITTED_MANAGERS,
  262. 0,
  263. KEY_QUERY_VALUE | KEY_SET_VALUE,
  264. NULL,
  265. &hKey,
  266. NULL);
  267. if (hr != S_OK || bAnyHost)
  268. {
  269. return hr;
  270. }
  271. while(*pMgrsList != L'\0')
  272. {
  273. swprintf(szMgr, L"%d", nMgr++);
  274. hr = HrRegSetSz(hKey, szMgr, pMgrsList);
  275. if (hr != S_OK)
  276. break;
  277. pMgrsList += wcslen(pMgrsList) + 1;
  278. }
  279. RegSafeCloseKey(hKey);
  280. return hr;
  281. }
  282. HRESULT
  283. SnmpRegWriteTraps(tstring tstrVariable,
  284. PWSTR pTstrArray)
  285. {
  286. HKEY hKey, hKeyTrap;
  287. HRESULT hr = S_OK;
  288. UINT nTrap = 1;
  289. WCHAR szTrap[16];
  290. hr = HrRegDeleteKeyTree(HKEY_LOCAL_MACHINE,
  291. REG_KEY_TRAP_DESTINATIONS);
  292. if (hr != S_OK)
  293. return hr;
  294. hr = HrRegCreateKeyEx(HKEY_LOCAL_MACHINE,
  295. REG_KEY_TRAP_DESTINATIONS,
  296. 0,
  297. KEY_QUERY_VALUE | KEY_SET_VALUE,
  298. NULL,
  299. &hKey,
  300. NULL);
  301. if (hr != S_OK)
  302. return hr;
  303. hr = HrRegCreateKeyEx(hKey,
  304. tstrVariable.c_str(),
  305. 0,
  306. KEY_QUERY_VALUE | KEY_SET_VALUE,
  307. NULL,
  308. &hKeyTrap,
  309. NULL);
  310. if (hr == S_OK)
  311. {
  312. // it might just happen that you want to create a
  313. // community but you don't have the trap destination
  314. // addresses yet. We should let this happen.
  315. if (pTstrArray != NULL)
  316. {
  317. while(*pTstrArray != L'\0')
  318. {
  319. swprintf(szTrap, L"%d", nTrap++);
  320. hr = HrRegSetSz(hKeyTrap, szTrap, pTstrArray);
  321. if (hr != S_OK)
  322. break;
  323. pTstrArray += wcslen(pTstrArray) + 1;
  324. }
  325. }
  326. RegSafeCloseKey(hKeyTrap);
  327. }
  328. RegSafeCloseKey(hKey);
  329. return hr;
  330. }
  331. HRESULT
  332. SnmpRegWriteTstring(PWSTR pRegKey,
  333. PWSTR pValueName,
  334. tstring tstrValueData)
  335. {
  336. HRESULT hr = S_OK;
  337. HKEY hKey;
  338. hr = HrRegCreateKeyEx(HKEY_LOCAL_MACHINE,
  339. pRegKey,
  340. 0,
  341. KEY_QUERY_VALUE | KEY_SET_VALUE,
  342. NULL,
  343. &hKey,
  344. NULL);
  345. if (hr != S_OK)
  346. return hr;
  347. hr = HrRegSetString(hKey, pValueName, tstrValueData);
  348. RegSafeCloseKey(hKey);
  349. return hr;
  350. }
  351. DWORD
  352. SnmpStrArrayToServices(PWSTR pSrvArray)
  353. {
  354. DWORD dwServices = 0;
  355. while(*pSrvArray)
  356. {
  357. if(_wcsicmp(pSrvArray, SRV_AGNT_PHYSICAL_NAME)==0)
  358. dwServices |= SRV_AGNT_PHYSICAL_VALUE;
  359. if(_wcsicmp(pSrvArray, SRV_AGNT_DATALINK_NAME)==0)
  360. dwServices |= SRV_AGNT_DATALINK_VALUE;
  361. if(_wcsicmp(pSrvArray, SRV_AGNT_ENDTOEND_NAME)==0)
  362. dwServices |= SRV_AGNT_ENDTOEND_VALUE;
  363. if(_wcsicmp(pSrvArray, SRV_AGNT_INTERNET_NAME)==0)
  364. dwServices |= SRV_AGNT_INTERNET_VALUE;
  365. if(_wcsicmp(pSrvArray, SRV_AGNT_APPLICATIONS_NAME)==0)
  366. dwServices |= SRV_AGNT_APPLICATIONS_VALUE;
  367. pSrvArray += wcslen(pSrvArray) + 1;
  368. }
  369. return dwServices;
  370. }