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.

238 lines
7.7 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. Revision history:
  6. --*/
  7. #include <snmp.h>
  8. #include <snmpexts.h>
  9. #include "mibfuncs.h"
  10. extern PSNMP_MGMTVARS ge_pMgmtVars;
  11. extern CRITICAL_SECTION g_SnmpMibCriticalSection;
  12. UINT
  13. snmpMibGetHandler(
  14. UINT actionId,
  15. AsnAny *objectArray,
  16. UINT *errorIndex)
  17. {
  18. int i, j, k;
  19. LONG nOurSnmpEnableAuthenTraps;
  20. if (ge_pMgmtVars == NULL)
  21. return MIB_S_ENTRY_NOT_FOUND;
  22. // get the number of AsnAny structures we have in the MIB's data buffer
  23. // and be careful not too scan further (it might be that there are more
  24. // management variables than objects supported by the MIB).
  25. k = sizeof(SNMPMIB_MGMTVARS) / sizeof(AsnAny);
  26. for (i = 0; k != 0 && i < NC_MAX_COUNT; i++, k--)
  27. {
  28. if (objectArray[i].asnType == ge_pMgmtVars->AsnCounterPool[i].asnType)
  29. {
  30. objectArray[i].asnValue = ge_pMgmtVars->AsnCounterPool[i].asnValue;
  31. }
  32. }
  33. for (j = 0; k != 0 && j < NI_MAX_COUNT; j++, k--)
  34. {
  35. if (objectArray[i + j].asnType == ge_pMgmtVars->AsnIntegerPool[j].asnType)
  36. {
  37. if ((i+j) == (NC_MAX_COUNT + IsnmpEnableAuthenTraps))
  38. {
  39. // for nOurSnmpEnableAuthenTraps: enabled(1), disabled(0)
  40. // for the output value defined by RFC1213: enabled(1), disabled(2)
  41. nOurSnmpEnableAuthenTraps = ge_pMgmtVars->AsnIntegerPool[j].asnValue.number;
  42. objectArray[i + j].asnValue.number = (nOurSnmpEnableAuthenTraps == 0) ? 2 : 1;
  43. }
  44. else
  45. {
  46. objectArray[i + j].asnValue = ge_pMgmtVars->AsnIntegerPool[j].asnValue;
  47. }
  48. }
  49. }
  50. return MIB_S_SUCCESS;
  51. }
  52. UINT
  53. snmpMibSetHandler(
  54. UINT actionId,
  55. AsnAny *objectArray,
  56. UINT *errorIndex)
  57. {
  58. // this function is called only for one object: snmpEnableAuthenTraps
  59. DWORD dwResult, dwValue , dwValueLen, dwValueType;
  60. LONG nOurValue, nInputValue;
  61. static HKEY hKey = NULL;
  62. static DWORD dwBlocked = 0; // Has Critical Section entered
  63. static BOOL fMatchedValue = FALSE; // Does input SET value match our current value
  64. switch(actionId)
  65. {
  66. case MIB_ACTION_VALIDATE:
  67. {
  68. SNMPDBG((
  69. SNMP_LOG_TRACE,
  70. "SNMP: SNMPMIB: snmpMibSetHandler: Entered MIB_ACTION_VALIDATE\n"));
  71. // On XP or later, Enter/Leave CriticalSection won't raise
  72. // exception in low memory
  73. EnterCriticalSection(&g_SnmpMibCriticalSection);
  74. ++dwBlocked;
  75. // check the type of input set value
  76. if (ge_pMgmtVars->AsnIntegerPool[IsnmpEnableAuthenTraps].asnType !=
  77. objectArray[NC_MAX_COUNT + IsnmpEnableAuthenTraps].asnType)
  78. {
  79. SNMPDBG((
  80. SNMP_LOG_ERROR,
  81. "SNMP: SNMPMIB: snmpMibSetHandler: invalid type %x.\n",
  82. objectArray[NC_MAX_COUNT + IsnmpEnableAuthenTraps].asnType));
  83. return MIB_S_INVALID_PARAMETER;
  84. }
  85. nOurValue = ge_pMgmtVars->AsnIntegerPool[IsnmpEnableAuthenTraps].asnValue.number;
  86. nInputValue = objectArray[NC_MAX_COUNT + IsnmpEnableAuthenTraps].asnValue.number;
  87. // check the range of input set value. enabled(1), disabled(2)
  88. if ( ( nInputValue< 1) || ( nInputValue > 2) )
  89. {
  90. SNMPDBG((
  91. SNMP_LOG_ERROR,
  92. "SNMP: SNMPMIB: snmpMibSetHandler: invalid value %d.\n",
  93. nInputValue));
  94. return MIB_S_INVALID_PARAMETER;
  95. }
  96. // ASSERT: nInputValue is either 1 or 2, nOurValue is either 0 or 1
  97. // avoid to set the registry value if it matched the current one.
  98. // for nOurValue: enabled(1), disabled(0)
  99. // for nInputValue: enabled(1), disabled(2)
  100. if ( (nInputValue==nOurValue) ||
  101. ((nInputValue==2) && (nOurValue==0))
  102. )
  103. {
  104. SNMPDBG((
  105. SNMP_LOG_TRACE,
  106. "SNMP: SNMPMIB: snmpMibSetHandler: has same value as the current one.\n"));
  107. fMatchedValue = TRUE;
  108. return MIB_S_SUCCESS;
  109. }
  110. dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  111. REG_KEY_SNMP_PARAMETERS,
  112. 0,
  113. KEY_SET_VALUE,
  114. &hKey);
  115. if(NO_ERROR != dwResult)
  116. {
  117. SNMPDBG((
  118. SNMP_LOG_ERROR,
  119. "SNMP: SNMPMIB: snmpMibSetHandler: Couldnt open SNMP Parameters key. Error %d.\n",
  120. dwResult));
  121. return dwResult;
  122. }
  123. return MIB_S_SUCCESS;
  124. }
  125. case MIB_ACTION_SET:
  126. {
  127. SNMPDBG((
  128. SNMP_LOG_TRACE,
  129. "SNMP: SNMPMIB: snmpMibSetHandler: Entered MIB_ACTION_SET\n"));
  130. if (fMatchedValue)
  131. {
  132. // avoid to set the registry value since it matched the current one
  133. fMatchedValue = FALSE;
  134. return MIB_S_SUCCESS;
  135. }
  136. if (!IsAsnTypeNull(&objectArray[NC_MAX_COUNT + IsnmpEnableAuthenTraps]))
  137. {
  138. dwValueType = REG_DWORD;
  139. dwValueLen = sizeof(DWORD);
  140. nInputValue = objectArray[NC_MAX_COUNT + IsnmpEnableAuthenTraps].asnValue.number;
  141. dwValue = (nInputValue == 2) ? 0 : 1;
  142. // note: the change of registry will cause snmp.exe to refresh
  143. // its configuration from registry.
  144. // Per SnmpExtensionMonitor MSDN doc., an SNMP extension agent
  145. // should not update the memory pointed to by the
  146. // pAgentMgmtData parameter.
  147. dwResult = RegSetValueEx(
  148. hKey,
  149. REG_VALUE_AUTH_TRAPS,
  150. 0,
  151. dwValueType,
  152. (LPBYTE)&dwValue,
  153. dwValueLen);
  154. if (NO_ERROR != dwResult)
  155. {
  156. SNMPDBG((
  157. SNMP_LOG_ERROR,
  158. "SNMP: SNMPMIB: snmpMibSetHandler: Couldnt write EnableAuthenticationTraps value. Error %d.\n",
  159. dwResult
  160. ));
  161. return dwResult;
  162. }
  163. }
  164. return MIB_S_SUCCESS;
  165. }
  166. case MIB_ACTION_CLEANUP:
  167. {
  168. SNMPDBG((
  169. SNMP_LOG_TRACE,
  170. "SNMP: SNMPMIB: snmpMibSetHandler: Entered CLEANUP\n"));
  171. if (hKey)
  172. {
  173. RegCloseKey(hKey);
  174. hKey = NULL;
  175. }
  176. if(dwBlocked)
  177. {
  178. --dwBlocked; // decrement block count before leaving CritSect
  179. LeaveCriticalSection(&g_SnmpMibCriticalSection);
  180. }
  181. return MIB_S_SUCCESS;
  182. }
  183. default:
  184. {
  185. SNMPDBG((
  186. SNMP_LOG_TRACE,
  187. "SNMP: SNMPMIB: snmpMibSetHandler: Entered WRONG ACTION\n"));
  188. return MIB_S_INVALID_PARAMETER;
  189. }
  190. }
  191. }