snmptrapd_auth.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * snmptrapd_auth.c - authorize notifications for further processing
  3. *
  4. * Portions of this file are copyrighted by:
  5. * Copyright (c) 2016 VMware, Inc. All rights reserved.
  6. * Use is subject to license terms specified in the COPYING file
  7. * distributed with the Net-SNMP package.
  8. *
  9. */
  10. #include <net-snmp/net-snmp-config.h>
  11. #if HAVE_SYS_TYPES_H
  12. #include <sys/types.h>
  13. #endif
  14. #if HAVE_NETINET_IN_H
  15. #include <netinet/in.h>
  16. #endif
  17. #if HAVE_NETDB_H
  18. #include <netdb.h>
  19. #endif
  20. #include <net-snmp/net-snmp-includes.h>
  21. #include "snmptrapd_handlers.h"
  22. #include "snmptrapd_auth.h"
  23. #include "snmptrapd_ds.h"
  24. #include <net-snmp/agent/agent_module_config.h>
  25. #include <net-snmp/agent/mib_module_config.h>
  26. #ifdef USING_MIBII_VACM_CONF_MODULE
  27. #include "net-snmp/agent/mibgroup/mibII/vacm_conf.h"
  28. #endif
  29. #include <net-snmp/agent/agent_trap.h>
  30. /**
  31. * initializes the snmptrapd authorization code registering needed
  32. * handlers and config parsers.
  33. */
  34. void init_netsnmp_trapd_auth(void)
  35. {
  36. /* register our function as a authorization handler */
  37. netsnmp_trapd_handler *traph;
  38. printf("new********5\n");
  39. traph = netsnmp_add_global_traphandler(NETSNMPTRAPD_AUTH_HANDLER,
  40. netsnmp_trapd_auth);
  41. traph->authtypes = TRAP_AUTH_NONE;
  42. #ifdef USING_MIBII_VACM_CONF_MODULE
  43. /* register our configuration tokens for VACM configs */
  44. init_vacm_config_tokens();
  45. #endif
  46. /* register a config token for turning off the authorization entirely */
  47. netsnmp_ds_register_config(ASN_BOOLEAN, "snmptrapd", "disableAuthorization",
  48. NETSNMP_DS_APPLICATION_ID,
  49. NETSNMP_DS_APP_NO_AUTHORIZATION);
  50. }
  51. /* XXX: store somewhere in the PDU instead */
  52. static int lastlookup;
  53. /**
  54. * Authorizes incoming notifications for further processing
  55. */
  56. int netsnmp_trapd_auth(netsnmp_pdu* pdu,netsnmp_transport *transport, netsnmp_trapd_handler * handler)
  57. {
  58. printf("netsnmp_trapd_auth\n");
  59. #if 1
  60. int ret = 0;
  61. oid snmptrapoid[] = { 1,3,6,1,6,3,1,1,4,1,0 };
  62. size_t snmptrapoid_len = OID_LENGTH(snmptrapoid);
  63. netsnmp_pdu *newpdu = pdu;
  64. netsnmp_variable_list *var;
  65. #ifdef USING_MIBII_VACM_CONF_MODULE
  66. int i;
  67. #endif
  68. /* check to see if authorization was not disabled */
  69. if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
  70. NETSNMP_DS_APP_NO_AUTHORIZATION)) {
  71. DEBUGMSGTL(("snmptrapd:auth",
  72. "authorization turned off: not checking\n"));
  73. return NETSNMPTRAPD_HANDLER_OK;
  74. }
  75. /* bail early if called illegally */
  76. if (!pdu || !transport || !handler)
  77. return NETSNMPTRAPD_HANDLER_FINISH;
  78. /* convert to v2 so we can check it in a consistent manner */
  79. #ifndef NETSNMP_DISABLE_SNMPV1
  80. if (pdu->version == SNMP_VERSION_1) {
  81. newpdu = convert_v1pdu_to_v2(pdu);
  82. if (!newpdu) {
  83. snmp_log(LOG_ERR, "Failed to duplicate incoming PDU. Refusing to authorize.\n");
  84. return NETSNMPTRAPD_HANDLER_FINISH;
  85. }
  86. }
  87. #endif
  88. if (!vacm_is_configured()) {
  89. #ifndef NETSNMP_DISABLE_SNMPV1
  90. if (newpdu != pdu)
  91. snmp_free_pdu(newpdu);
  92. #endif
  93. snmp_log(LOG_WARNING, "No access configuration - dropping trap.\n");
  94. return NETSNMPTRAPD_HANDLER_FINISH;
  95. }
  96. /* loop through each variable and find the snmpTrapOID.0 var
  97. indicating what the trap is we're staring at. */
  98. for (var = newpdu->variables; var != NULL; var = var->next_variable) {
  99. if (netsnmp_oid_equals(var->name, var->name_length,
  100. snmptrapoid, snmptrapoid_len) == 0)
  101. break;
  102. }
  103. /* make sure we can continue: we found the snmpTrapOID.0 and its an oid */
  104. if (!var || var->type != ASN_OBJECT_ID) {
  105. snmp_log(LOG_ERR, "Can't determine trap identifier; refusing to authorize it\n");
  106. #ifndef NETSNMP_DISABLE_SNMPV1
  107. if (newpdu != pdu)
  108. snmp_free_pdu(newpdu);
  109. #endif
  110. return NETSNMPTRAPD_HANDLER_FINISH;
  111. }
  112. #ifdef USING_MIBII_VACM_CONF_MODULE
  113. /* check the pdu against each typo of VACM access we may want to
  114. check up on later. We cache the results for future lookup on
  115. each call to netsnmp_trapd_check_auth */
  116. for(i = 0; i < VACM_MAX_VIEWS; i++) {
  117. /* pass the PDU to the VACM routine for handling authorization */
  118. DEBUGMSGTL(("snmptrapd:auth", "Calling VACM for checking phase %d:%s\n",
  119. i, se_find_label_in_slist(VACM_VIEW_ENUM_NAME, i)));
  120. if (vacm_check_view_contents(newpdu, var->val.objid,
  121. var->val_len/sizeof(oid), 0, i,
  122. VACM_CHECK_VIEW_CONTENTS_DNE_CONTEXT_OK)
  123. == VACM_SUCCESS) {
  124. DEBUGMSGTL(("snmptrapd:auth", " result: authorized\n"));
  125. ret |= 1 << i;
  126. } else {
  127. DEBUGMSGTL(("snmptrapd:auth", " result: not authorized\n"));
  128. }
  129. }
  130. DEBUGMSGTL(("snmptrapd:auth", "Final bitmask auth: %x\n", ret));
  131. #endif
  132. if (ret) {
  133. /* we have policy to at least do "something". Remember and continue. */
  134. lastlookup = ret;
  135. #ifndef NETSNMP_DISABLE_SNMPV1
  136. if (newpdu != pdu)
  137. snmp_free_pdu(newpdu);
  138. #endif
  139. return NETSNMPTRAPD_HANDLER_OK;
  140. }
  141. /* No policy was met, so we drop the PDU from further processing */
  142. DEBUGMSGTL(("snmptrapd:auth", "Dropping unauthorized message\n"));
  143. #ifndef NETSNMP_DISABLE_SNMPV1
  144. if (newpdu != pdu)
  145. snmp_free_pdu(newpdu);
  146. #endif
  147. #endif
  148. return NETSNMPTRAPD_HANDLER_FINISH;
  149. }
  150. /**
  151. * Checks to see if the pdu is authorized for a set of given action types.
  152. * @returns 1 if authorized, 0 if not.
  153. */
  154. int netsnmp_trapd_check_auth(int authtypes)
  155. {
  156. if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
  157. NETSNMP_DS_APP_NO_AUTHORIZATION)) {
  158. DEBUGMSGTL(("snmptrapd:auth", "authorization turned off\n"));
  159. return 1;
  160. }
  161. DEBUGMSGTL(("snmptrapd:auth",
  162. "Comparing auth types: result=%d, request=%d, result=%d\n",
  163. lastlookup, authtypes,
  164. ((authtypes & lastlookup) == authtypes)));
  165. return ((authtypes & lastlookup) == authtypes);
  166. }