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.

546 lines
18 KiB

  1. /*++ BUILD Version: 0001 // Increment this if a change has global effects
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. main.c
  5. Abstract:
  6. SNMP Extension Agent for nntp Service on Windows NT.
  7. Created:
  8. MuraliK 22-Feb-1995
  9. Revision History:
  10. --*/
  11. #include "mib.h"
  12. #include <excpt.h>
  13. #include "nntptype.h"
  14. #include "nntpapi.h"
  15. //#include <rpcutil.h>
  16. /************************************************************
  17. * Variable Definitions
  18. ************************************************************/
  19. //
  20. // Definition of the MIB objects
  21. //
  22. //
  23. // The InternetServer section of the OID tree is organized as follows:
  24. //
  25. // iso(1)
  26. // org(3)
  27. // dod(6)
  28. // internet(1)
  29. // private(4)
  30. // enterprises(1)
  31. // microsoft(311)
  32. // software(1)
  33. // InternetServer(7)
  34. // InetSrvCommon(1)
  35. // InetSrvStatistics(1)
  36. // FtpServer(2)
  37. // FtpStatistics(1)
  38. // HttpServer(3)
  39. // HttpStatistics(1)
  40. // GopherServer(4)
  41. // GopherStatistics(1)
  42. // MnsServer(5)
  43. // MsnStatistics(1)
  44. // NntpServer(6)
  45. // NntpStatistics(1)
  46. //
  47. static UINT sg_rguiPrefix[] = { 1, 3, 6, 1, 4, 1, 311, 1, 7, 6};
  48. static AsnObjectIdentifier sg_MibOidPrefix =
  49. OID_FROM_UINT_ARRAY( sg_rguiPrefix);
  50. # define NNTP_PREFIX_OID_LENGTH ( GET_OID_LENGTH( sg_MibOidPrefix))
  51. # define NNTP_STATISTICS_OID_SUFFIX ( 1)
  52. //
  53. // Following is the global description of all MIB Entries ( Mibe s) for
  54. // Nntp Service.
  55. // Definition appears as:
  56. // Mibe( MIB Entry Name, Index in MIB Block, NntpStatisticsField)
  57. //
  58. // Incidentally, MIB Entry suffix coincides with the entry name in OID Tree
  59. //
  60. // Any New MIB should be added here. and dont change use of after this defn.
  61. //
  62. # define ALL_MIB_ENTRIES() \
  63. Mibe( TotalBytesSent_HighWord, 1, TotalBytesSent.HighPart) \
  64. Mibe( TotalBytesSent_LowWord, 2, TotalBytesSent.LowPart) \
  65. Mibe( TotalBytesReceived_HighWord, 3, TotalBytesReceived.HighPart)\
  66. Mibe( TotalBytesReceived_LowWord, 4, TotalBytesReceived.LowPart) \
  67. Mibe( TotalConnections, 5, TotalConnections) \
  68. Mibe( TotalSSLConnections, 6, TotalSSLConnections) \
  69. Mibe( CurrentConnections, 7, CurrentConnections) \
  70. Mibe( MaxConnections, 8, MaxConnections) \
  71. Mibe( LogonAttempts, 9, LogonAttempts) \
  72. Mibe( LogonFailures, 10, LogonFailures) \
  73. Mibe( CurrentAnonymousUsers, 11, CurrentAnonymousUsers) \
  74. Mibe( CurrentNonAnonymousUsers, 12, CurrentNonAnonymousUsers)\
  75. Mibe( TotalAnonymousUsers, 13, TotalAnonymousUsers) \
  76. Mibe( TotalNonAnonymousUsers, 14, TotalNonAnonymousUsers) \
  77. Mibe( MaxAnonymousUsers, 15, MaxAnonymousUsers) \
  78. Mibe( MaxNonAnonymousUsers, 16, MaxNonAnonymousUsers) \
  79. Mibe( TotalOutboundConnects, 17, TotalOutboundConnects) \
  80. Mibe( OutboundConnectsFailed, 18, OutboundConnectsFailed) \
  81. Mibe( CurrentOutboundConnects, 19, CurrentOutboundConnects) \
  82. Mibe( OutboundLogonFailed, 20, OutboundLogonFailed) \
  83. Mibe( TotalPullFeeds, 21, TotalPullFeeds) \
  84. Mibe( TotalPushFeeds, 22, TotalPushFeeds) \
  85. Mibe( TotalPassiveFeeds, 23, TotalPassiveFeeds) \
  86. Mibe( ArticlesSent, 24, ArticlesSent) \
  87. Mibe( ArticlesReceived, 25, ArticlesReceived) \
  88. Mibe( ArticlesPosted, 26, ArticlesPosted) \
  89. Mibe( ArticleMapEntries, 27, ArticleMapEntries) \
  90. Mibe( HistoryMapEntries, 28, HistoryMapEntries) \
  91. Mibe( XoverEntries, 29, XoverEntries) \
  92. Mibe( SessionsFlowControlled, 30, SessionsFlowControlled) \
  93. Mibe( ArticlesExpired, 31, ArticlesExpired) \
  94. Mibe( ControlMessagesIn, 32, ControlMessagesIn) \
  95. Mibe( ControlMessagesFailed, 33, ControlMessagesFailed) \
  96. Mibe( ModeratedPostingsSent, 34, ModeratedPostingsSent) \
  97. Mibe( ModeratedPostingsFailed, 35, ModeratedPostingsFailed) \
  98. Mibe( ArticleCommands, 36, ArticleCommands) \
  99. Mibe( GroupCommands, 37, GroupCommands) \
  100. Mibe( HelpCommands, 38, HelpCommands) \
  101. Mibe( IHaveCommands, 39, IHaveCommands) \
  102. Mibe( LastCommands, 40, LastCommands) \
  103. Mibe( ListCommands, 41, ListCommands) \
  104. Mibe( NewgroupsCommands, 42, NewgroupsCommands) \
  105. Mibe( NewnewsCommands, 43, NewnewsCommands) \
  106. Mibe( NextCommands, 44, NextCommands) \
  107. Mibe( PostCommands, 45, PostCommands) \
  108. Mibe( QuitCommands, 46, QuitCommands) \
  109. Mibe( StatCommands, 47, StatCommands) \
  110. Mibe( CheckCommands, 48, CheckCommands) \
  111. Mibe( TakethisCommands, 49, TakethisCommands) \
  112. Mibe( ModeCommands, 50, ModeCommands) \
  113. Mibe( SearchCommands, 51, SearchCommands) \
  114. Mibe( XHdrCommands, 52, XHdrCommands) \
  115. Mibe( XOverCommands, 53, XOverCommands) \
  116. Mibe( XPatCommands, 54, XPatCommands) \
  117. Mibe( XReplicCommands, 55, XReplicCommands)
  118. //
  119. // Individual OID Definitions.
  120. // All Leaf variables should have a zero appended to their OID to indicate
  121. // that it is the only instance of this variable and that it exists.
  122. // Declare just the id's starting from next to the prefix given above.
  123. //
  124. //
  125. // Few Convenience Macros for MIB entries addition.
  126. //
  127. # define MIB_VAR_NAME( NameSuffix) MIB_ ## NameSuffix
  128. # define DEFINE_MIBOID( NameSuffix, uiArray) \
  129. UINT MIB_VAR_NAME( NameSuffix)[] = uiArray
  130. # define DEFINE_MIBOID_LEAF( NameSuffix, NodeNumber) \
  131. UINT MIB_VAR_NAME( NameSuffix)[] = \
  132. { NNTP_STATISTICS_OID_SUFFIX, ( NodeNumber), 0 }
  133. //
  134. // Define all the OIDs. First define the higher level node and then leaves.
  135. //
  136. DEFINE_MIBOID( Statistics, { NNTP_STATISTICS_OID_SUFFIX} );
  137. //
  138. // Define the Leaf OIDs.
  139. //
  140. # define Mibe( NameSuffix, Index, FieldName) \
  141. DEFINE_MIBOID_LEAF( NameSuffix, Index);
  142. //
  143. // Expand the macro ALL_MIB_ENTRIES to obtain definitions of MIB Leafs.
  144. //
  145. ALL_MIB_ENTRIES()
  146. # undef Mibe
  147. //
  148. // MIB Variable definition
  149. //
  150. //
  151. // Define Mibe() to be for variable definitions of counters.
  152. // Note that the comma is appearing before a new counter name. It is used
  153. // for structure initialization.
  154. //
  155. # define OFFSET_IN_NNTP_STATISTICS( Field) \
  156. FIELD_OFFSET( NNTP_STATISTICS_0, Field)
  157. # define Mibe( NameSuffix, Index, Field) \
  158. , MIB_COUNTER( OID_FROM_UINT_ARRAY( MIB_VAR_NAME( NameSuffix)), \
  159. OFFSET_IN_NNTP_STATISTICS(Field), \
  160. MibStatisticsWorker)
  161. static MIB_ENTRY sg_rgNntpMib[] = {
  162. //
  163. // Statistics
  164. //
  165. MIB_ENTRY_HEADER( OID_FROM_UINT_ARRAY( MIB_VAR_NAME( Statistics)))
  166. ALL_MIB_ENTRIES()
  167. };
  168. # undef Mibe
  169. static MIB_ENTRIES sg_NntpMibs =
  170. {
  171. &sg_MibOidPrefix,
  172. ( sizeof( sg_rgNntpMib) / sizeof( MIB_ENTRY)),
  173. sg_rgNntpMib
  174. };
  175. /************************************************************
  176. * Functions
  177. ************************************************************/
  178. BOOL WINAPI
  179. DllLibMain(
  180. IN HINSTANCE hinstDll,
  181. IN DWORD fdwReason,
  182. IN LPVOID lpvContext OPTIONAL)
  183. /*++
  184. Routine Description:
  185. This function DllLibMain() is the main initialization function for
  186. Nntp MIB DLL. It initialises local variables and prepares the
  187. interface for the process to use SNMP Extension Agents for NNTP service.
  188. Messages Actions
  189. ProcessAttach Initializes winsock and data structures.
  190. It fails if winsock has not already been started.
  191. ProcessDetach Cleans up local data structures and disconnects from
  192. winsock.
  193. Arguments:
  194. hinstDll Instance Handle of the DLL
  195. fdwReason Reason why NT called this DLL
  196. lpvReserved Reserved parameter for future use.
  197. Return Value:
  198. Returns TRUE is successful; otherwise FALSE is returned.
  199. --*/
  200. {
  201. BOOL fReturn = TRUE;
  202. switch (fdwReason ) {
  203. case DLL_PROCESS_ATTACH: {
  204. //
  205. // Initialize various modules
  206. //
  207. break;
  208. } /* case DLL_PROCESS_ATTACH */
  209. case DLL_PROCESS_DETACH: {
  210. //
  211. // Only cleanup when we are called because of a FreeLibrary().
  212. // i.e., when lpvContext == NULL
  213. // If we are called because of a process termination, dont free anything
  214. // the system will free resources and memory for us.
  215. //
  216. if ( lpvContext == NULL) {
  217. //
  218. // Code to be executed on successful termination
  219. //
  220. }
  221. break;
  222. } /* case DLL_PROCESS_DETACH */
  223. default:
  224. break;
  225. } /* switch */
  226. return ( fReturn);
  227. } /* DllLibMain() */
  228. /************************************************************
  229. * Entry Points of SNMP Extension DLL For Nntp Service
  230. ************************************************************/
  231. //
  232. // Extension Agent DLLs need access to elapsed time agent has been active.
  233. // This is implemented by initializing the Extension Agent with a time zero
  234. // reference, and allowing the agent to compute elapsed time by subtracting
  235. // the time zero reference from the current system time. This example
  236. // Extension Agent implements this reference with dwTimeZero.
  237. //
  238. DWORD dwTimeZero = 0;
  239. BOOL
  240. SnmpExtensionInit(
  241. IN DWORD dwTimeZeroReference,
  242. OUT HANDLE * phPollForTrapEvent,
  243. OUT AsnObjectIdentifier * pAsnOidSupportedView
  244. )
  245. /*++
  246. Description:
  247. The Extension Agent DLLs provide this entry point SnmpExtensionInit()
  248. to co-ordinate the initializations of the extension agent and the
  249. extendible agent.
  250. The Extendible agent provides extension agent with a time zero reference.
  251. The Extension Agent provides Extendible agent with an Event Handle
  252. for communicating occurences of traps.
  253. The Extension Agent also provides Extendible agent with an ObjectId
  254. representing the root of the MIB structure
  255. that it (extension) supports.
  256. Arguments:
  257. dwTimeZeroReference DWORD containing the Time Zero Reference for sync.
  258. phPollForTrapEvent pointer to handle which on successful return
  259. may contain an event handle to be polled for
  260. traps.
  261. pAsnOidSupportedView pointer to ASN ( Abstract Syntax Notation OID)
  262. that contains the oid representing root of the
  263. MIB structure.
  264. Returns:
  265. TRUE on success and FALSE if there is any failure.
  266. --*/
  267. {
  268. //
  269. // Record the time reference provided by the Extendible Agent.
  270. //
  271. dwTimeZero = dwTimeZeroReference;
  272. //
  273. // Indicate the MIB view supported by this Extension Agent, an object
  274. // identifier representing the sub root of the MIB that is supported.
  275. //
  276. *pAsnOidSupportedView = sg_MibOidPrefix; // NOTE! structure copy
  277. //
  278. // Though the following is a handle, dont use INVALID_HANDLE_VALUE ( -1)
  279. // because that constant is only for few people ( Win32). But all through
  280. // NT invalid handle value is NULL ( 0).
  281. //
  282. *phPollForTrapEvent = NULL;
  283. //
  284. // Indicate that Extension Agent initialization was sucessfull.
  285. //
  286. return ( TRUE);
  287. } // SnmpExtensionInit()
  288. BOOL
  289. SnmpExtensionTrap(
  290. OUT AsnObjectIdentifier * pAsnOidEnterprise,
  291. OUT AsnInteger * pAsniGenericTrap,
  292. OUT AsnInteger * pAsniSpecificTrap,
  293. OUT AsnTimeticks * pAsnTimeStamp,
  294. OUT RFC1157VarBindList * pRfcVariableBindings
  295. )
  296. /*++
  297. Description:
  298. This function is used to communicate traps to the Extendible Agent.
  299. The Extendible Agent will invoke this entry point when the trap event
  300. ( supplied at the initialization time) is asserted, which indicates
  301. that zero or more traps had occured.
  302. The Extendible agent will repeatedly query this function till this
  303. function returns FALSE.
  304. Arguments:
  305. pAsnOidEnterprise pointer to ASN OID for Enterprise, indicating
  306. original enterprise generating trap.
  307. pAsniGenericTrap pointer to ASN Integer which on return will
  308. contain the indication of the generic trap.
  309. pAsniSpecificTrap pointer to ASN Integer which on return will
  310. contain the specific trap generated.
  311. pAsnTimeStamp pointer to ASN containing the received Time-Stamp.
  312. pRfcVariableBindings pointer to RFC 1157 compliant variable bindings.
  313. Returns:
  314. TRUE if success and there are more traps to be queried.
  315. FALSE if all traps are answered and work done.
  316. --*/
  317. {
  318. //
  319. // We don't support traps (yet).
  320. //
  321. return ( FALSE);
  322. } // SnmpExtensionTrap()
  323. BOOL
  324. SnmpExtensionQuery(
  325. IN BYTE bRequestType,
  326. IN OUT RFC1157VarBindList * pRfcVariableBindings,
  327. OUT AsnInteger * pAsniErrorStatus,
  328. OUT AsnInteger * pAsniErrorIndex
  329. )
  330. /*++
  331. Description:
  332. This function is called by Extendible Agent to resolve the SNMP requests
  333. for queries on MIB Variables in the Extension Agent's supported MIB view.
  334. ( which was supplied at initialization time).
  335. The Request Type is GET, GETNEXT, and SET.
  336. Arguments:
  337. bRequestType byte containing the type of request.
  338. It can be one of
  339. ASN_RFC1157_GETREQUEST
  340. ASN_RFC1157_GETNEXTREQUEST
  341. ASN_RFC1157_SETREQUEST
  342. pRfcVariableBindings
  343. pointer to RFC 1157 compliant variable bindings.
  344. pAsniErrorStatus
  345. pointer to ASN Integer for Error Status
  346. pAsniErrorIndex
  347. pointer to ASN INteger giving the index for error.
  348. Returns:
  349. TRUE on success and FALSE on failure.
  350. --*/
  351. {
  352. LPNNTP_STATISTICS_0 pNntpStatistics = NULL;
  353. NET_API_STATUS Status;
  354. //
  355. // Try to query the statistics now so we'll have a consitent
  356. // view across all variable bindings.
  357. //
  358. Status = NntpQueryStatistics(
  359. NULL, // pszServer
  360. 1, // virtual server instance
  361. 0,
  362. (LPBYTE *) &pNntpStatistics
  363. );
  364. //
  365. // Status Errors not checked for here!
  366. // Reason:
  367. // If the verb is GET_NEXT beyond the block we support,
  368. // then there is no need to worry about the error at all.
  369. // If the verb is GET within the block, it will get NULL value
  370. // ( due the memset() done above).
  371. //
  372. __try {
  373. //
  374. // Iterate through the variable bindings list to resolve individual
  375. // variable bindings.
  376. //
  377. RFC1157VarBind * pVarBinding;
  378. for( pVarBinding = pRfcVariableBindings->list;
  379. pVarBinding < ( pRfcVariableBindings->list +
  380. pRfcVariableBindings->len);
  381. pVarBinding++ ) {
  382. *pAsniErrorStatus = ResolveVarBinding( pVarBinding,
  383. bRequestType,
  384. pNntpStatistics,
  385. &sg_NntpMibs);
  386. //
  387. // Test and handle case where Get Next past end of MIB view
  388. // supported by this Extension Agent occurs. Special
  389. // processing is required to communicate this situation to
  390. // the Extendible Agent so it can take appropriate action,
  391. // possibly querying other Extension Agents.
  392. //
  393. if(( *pAsniErrorStatus == SNMP_ERRORSTATUS_NOSUCHNAME ) &&
  394. ( bRequestType == MIB_GETNEXT ) ) {
  395. *pAsniErrorStatus = SNMP_ERRORSTATUS_NOERROR;
  396. //
  397. // Modify variable binding of such variables so the OID
  398. // points just outside the MIB view supported by this
  399. // Extension Agent. The Extendible Agent tests for this,
  400. // and takes appropriate action.
  401. //
  402. SNMP_oidfree( &pVarBinding->name );
  403. SNMP_oidcpy( &pVarBinding->name, &sg_MibOidPrefix);
  404. pVarBinding->name.ids[ NNTP_PREFIX_OID_LENGTH - 1]++;
  405. }
  406. //
  407. // If an error was indicated, communicate error status and error
  408. // index to the Extendible Agent. The Extendible Agent will
  409. // ensure that the origional variable bindings are returned in
  410. // the response packet.
  411. *pAsniErrorIndex =
  412. (( *pAsniErrorStatus != SNMP_ERRORSTATUS_NOERROR ) ?
  413. ( (AsnInteger)((pVarBinding - pRfcVariableBindings->list) + 1)) : 0);
  414. } // for
  415. } __except( EXCEPTION_EXECUTE_HANDLER ) {
  416. //
  417. // For now do nothing.
  418. //
  419. }
  420. if ( pNntpStatistics )
  421. {
  422. NetApiBufferFree( pNntpStatistics );
  423. }
  424. return ( SNMPAPI_NOERROR);
  425. } // SnmpExtensionQuery()