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.

278 lines
5.8 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // snmputil.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // Defines utility functions for computing MIB variables.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 09/11/1998 Original version.
  16. // 02/18/1999 Move registry values.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #include <ias.h>
  20. #include <iasutil.h>
  21. #include <snmputil.h>
  22. #include <stats.h>
  23. const CHAR SERVER_IDENTITY[] = "Microsoft IAS2.0";
  24. VOID
  25. WINAPI
  26. GetServerIdentity(
  27. OUT AsnAny* value
  28. )
  29. {
  30. value->asnType = ASN_OCTETSTRING;
  31. value->asnValue.string.stream = (PBYTE)SERVER_IDENTITY;
  32. value->asnValue.string.length = sizeof(SERVER_IDENTITY);
  33. value->asnValue.string.dynamic = FALSE;
  34. }
  35. VOID
  36. WINAPI
  37. GetServerUpTime(
  38. OUT AsnAny* value
  39. )
  40. {
  41. LARGE_INTEGER elapsed;
  42. if (theStats->seServer.liStartTime.QuadPart)
  43. {
  44. GetSystemTimeAsFileTime((LPFILETIME)&elapsed);
  45. elapsed.QuadPart -= theStats->seServer.liStartTime.QuadPart;
  46. elapsed.QuadPart /= 100000i64;
  47. }
  48. else
  49. {
  50. elapsed.LowPart = 0;
  51. }
  52. value->asnType = ASN_GAUGE32;
  53. value->asnValue.gauge = elapsed.LowPart;
  54. }
  55. VOID
  56. WINAPI
  57. GetServerResetTime(
  58. OUT AsnAny* value
  59. )
  60. {
  61. LARGE_INTEGER elapsed;
  62. if (theStats->seServer.liResetTime.QuadPart)
  63. {
  64. GetSystemTimeAsFileTime((LPFILETIME)&elapsed);
  65. elapsed.QuadPart -= theStats->seServer.liResetTime.QuadPart;
  66. elapsed.QuadPart /= 100000i64;
  67. }
  68. else
  69. {
  70. elapsed.LowPart = 0;
  71. }
  72. value->asnType = ASN_GAUGE32;
  73. value->asnValue.gauge = elapsed.LowPart;
  74. }
  75. extern "C"
  76. SC_HANDLE
  77. GetServiceHandle( VOID )
  78. {
  79. static SC_HANDLE scm, service;
  80. if (!service)
  81. {
  82. if (!scm)
  83. {
  84. scm = OpenSCManager(NULL, NULL, GENERIC_READ);
  85. }
  86. service = OpenServiceW(
  87. scm,
  88. IASServiceName,
  89. SERVICE_QUERY_STATUS |
  90. SERVICE_START |
  91. SERVICE_USER_DEFINED_CONTROL
  92. );
  93. }
  94. return service;
  95. }
  96. VOID
  97. WINAPI
  98. GetServerConfigReset(
  99. OUT AsnAny* value
  100. )
  101. {
  102. value->asnType = ASN_INTEGER32;
  103. value->asnValue.number = 1;
  104. SERVICE_STATUS status;
  105. if (QueryServiceStatus(GetServiceHandle(), &status))
  106. {
  107. switch (status.dwCurrentState)
  108. {
  109. case SERVICE_START_PENDING:
  110. value->asnValue.number = 3;
  111. break;
  112. case SERVICE_RUNNING:
  113. value->asnValue.number = 4;
  114. }
  115. }
  116. }
  117. AsnInteger32
  118. WINAPI
  119. SetServerConfigReset(
  120. IN AsnAny* value
  121. )
  122. {
  123. static BOOL allowSet, initialized;
  124. if (!initialized)
  125. {
  126. LONG status;
  127. HKEY hKey;
  128. status = RegOpenKeyW(
  129. HKEY_LOCAL_MACHINE,
  130. L"SYSTEM\\CurrentControlSet\\Services\\IAS\\Parameters",
  131. &hKey
  132. );
  133. if (status == NO_ERROR)
  134. {
  135. DWORD type, data(0), cbData(sizeof(data));
  136. status = RegQueryValueExW(
  137. hKey,
  138. L"Allow SNMP Set",
  139. NULL,
  140. &type,
  141. (LPBYTE)&data,
  142. &cbData
  143. );
  144. if (status == NO_ERROR && type == REG_DWORD && data > 0)
  145. {
  146. allowSet = TRUE;
  147. }
  148. RegCloseKey(hKey);
  149. }
  150. initialized = TRUE;
  151. }
  152. if (!allowSet)
  153. {
  154. return SNMP_ERRORSTATUS_AUTHORIZATIONERROR;
  155. }
  156. if (value->asnType != ASN_INTEGER32)
  157. {
  158. return SNMP_ERRORSTATUS_WRONGTYPE;
  159. }
  160. if (value->asnValue.number != 2)
  161. {
  162. return SNMP_ERRORSTATUS_WRONGVALUE;
  163. }
  164. SC_HANDLE service = GetServiceHandle();
  165. if (!service)
  166. {
  167. return SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  168. }
  169. SERVICE_STATUS status;
  170. if (StartService(service, 0, NULL) || ControlService(service, 128, &status))
  171. {
  172. return SNMP_ERRORSTATUS_NOERROR;
  173. }
  174. return SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  175. }
  176. VOID
  177. WINAPI
  178. GetTotalCounter(
  179. IN RadiusClientCounter counter,
  180. OUT AsnAny* value
  181. )
  182. {
  183. value->asnType = ASN_COUNTER32;
  184. value->asnValue.counter = 0;
  185. for (DWORD i = 0; i < theStats->dwNumClients; ++i)
  186. {
  187. value->asnValue.counter += theStats->ceClients[i].dwCounters[counter];
  188. }
  189. }
  190. VOID
  191. WINAPI
  192. GetServerCounter(
  193. IN RadiusServerCounter counter,
  194. OUT AsnAny* value
  195. )
  196. {
  197. value->asnType = ASN_COUNTER32;
  198. value->asnValue.counter = theStats->seServer.dwCounters[counter];
  199. }
  200. VOID
  201. WINAPI
  202. GetClientAddress(
  203. IN UINT client,
  204. OUT AsnAny* value
  205. )
  206. {
  207. value->asnType = ASN_IPADDRESS;
  208. value->asnValue.string.stream = (PBYTE)SnmpUtilMemAlloc(4);
  209. if (value->asnValue.string.stream)
  210. {
  211. IASInsertDWORD(value->asnValue.string.stream,
  212. theStats->ceClients[client].dwAddress);
  213. value->asnValue.string.length = 4;
  214. value->asnValue.string.dynamic = TRUE;
  215. }
  216. else
  217. {
  218. value->asnValue.string.length = 0;
  219. value->asnValue.string.dynamic = FALSE;
  220. }
  221. }
  222. VOID
  223. WINAPI
  224. GetClientIdentity(
  225. IN UINT client,
  226. OUT AsnAny* value
  227. )
  228. {
  229. value->asnType = ASN_OCTETSTRING;
  230. value->asnValue.string.stream = NULL;
  231. value->asnValue.string.length = 0;
  232. value->asnValue.string.dynamic = FALSE;
  233. }
  234. VOID
  235. WINAPI
  236. GetClientCounter(
  237. IN UINT client,
  238. IN RadiusClientCounter counter,
  239. OUT AsnAny* value
  240. )
  241. {
  242. value->asnType = ASN_COUNTER32;
  243. value->asnValue.counter = theStats->ceClients[client].dwCounters[counter];
  244. }