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.

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