defbackend.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * defbackend.c - implement a "backend of last resort" which is used only
  14. * when a request's basedn does not match one of the suffixes of any of the
  15. * configured backends.
  16. *
  17. */
  18. #include "slap.h"
  19. /*
  20. * ---------------- Macros ---------------------------------------------------
  21. */
  22. #define DEFBACKEND_TYPE "default"
  23. #define DEFBACKEND_OP_NOT_HANDLED 0
  24. #define DEFBACKEND_OP_HANDLED 1
  25. /*
  26. * ---------------- Static Variables -----------------------------------------
  27. */
  28. static struct slapdplugin defbackend_plugin;
  29. static Slapi_Backend *defbackend_backend = NULL;
  30. /*
  31. * ---------------- Prototypes for Private Functions -------------------------
  32. */
  33. static int defbackend_default( Slapi_PBlock *pb );
  34. static int defbackend_noop( Slapi_PBlock *pb );
  35. static int defbackend_abandon( Slapi_PBlock *pb );
  36. static int defbackend_bind( Slapi_PBlock *pb );
  37. static int defbackend_next_search_entry( Slapi_PBlock *pb );
  38. /*
  39. * ---------------- Public Functions -----------------------------------------
  40. */
  41. /*
  42. * defbackend_init: instantiate the default backend
  43. */
  44. void
  45. defbackend_init( void )
  46. {
  47. int rc;
  48. char *errmsg;
  49. Slapi_PBlock pb;
  50. LDAPDebug( LDAP_DEBUG_TRACE, "defbackend_init\n", 0, 0, 0 );
  51. /*
  52. * create a new backend
  53. */
  54. pblock_init( &pb );
  55. defbackend_backend = slapi_be_new( DEFBACKEND_TYPE , DEFBACKEND_TYPE, 1 /* Private */, 0 /* Do Not Log Changes */ );
  56. if (( rc = slapi_pblock_set( &pb, SLAPI_BACKEND, defbackend_backend ))
  57. != 0 ) {
  58. errmsg = "slapi_pblock_set SLAPI_BACKEND failed";
  59. goto cleanup_and_return;
  60. }
  61. /*
  62. * create a plugin structure for this backend since the
  63. * slapi_pblock_set()/slapi_pblock_get() functions assume there is one.
  64. */
  65. memset( &defbackend_plugin, '\0', sizeof( struct slapdplugin ));
  66. defbackend_plugin.plg_type = SLAPI_PLUGIN_DATABASE;
  67. defbackend_backend->be_database = &defbackend_plugin;
  68. if (( rc = slapi_pblock_set( &pb, SLAPI_PLUGIN, &defbackend_plugin ))
  69. != 0 ) {
  70. errmsg = "slapi_pblock_set SLAPI_PLUGIN failed";
  71. goto cleanup_and_return;
  72. }
  73. /* default backend is managed as if it would */
  74. /* contain remote data. */
  75. slapi_be_set_flag(defbackend_backend,SLAPI_BE_FLAG_REMOTE_DATA);
  76. /*
  77. * install handler functions, etc.
  78. */
  79. errmsg = "slapi_pblock_set handlers failed";
  80. rc = slapi_pblock_set( &pb, SLAPI_PLUGIN_VERSION,
  81. (void *)SLAPI_PLUGIN_CURRENT_VERSION );
  82. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_BIND_FN,
  83. (void *)defbackend_bind );
  84. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_UNBIND_FN,
  85. (void *)defbackend_noop );
  86. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_SEARCH_FN,
  87. (void *)defbackend_default );
  88. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_NEXT_SEARCH_ENTRY_FN,
  89. (void *)defbackend_next_search_entry );
  90. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_COMPARE_FN,
  91. (void *)defbackend_default );
  92. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_MODIFY_FN,
  93. (void *)defbackend_default );
  94. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_MODRDN_FN,
  95. (void *)defbackend_default );
  96. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_ADD_FN,
  97. (void *)defbackend_default );
  98. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_DELETE_FN,
  99. (void *)defbackend_default );
  100. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_ABANDON_FN,
  101. (void *)defbackend_abandon );
  102. cleanup_and_return:
  103. if ( rc != 0 ) {
  104. LDAPDebug( LDAP_DEBUG_ANY, "defbackend_init: failed (%s)\n",
  105. errmsg, 0, 0 );
  106. exit( 1 );
  107. }
  108. }
  109. /*
  110. * defbackend_get_backend: return a pointer to the default backend.
  111. * we never return NULL.
  112. */
  113. Slapi_Backend *
  114. defbackend_get_backend( void )
  115. {
  116. return( defbackend_backend );
  117. }
  118. /*
  119. * ---------------- Private Functions ----------------------------------------
  120. */
  121. static int
  122. defbackend_default( Slapi_PBlock *pb )
  123. {
  124. LDAPDebug( LDAP_DEBUG_TRACE, "defbackend_default\n", 0, 0, 0 );
  125. send_nobackend_ldap_result( pb );
  126. return( DEFBACKEND_OP_HANDLED );
  127. }
  128. static int
  129. defbackend_noop( Slapi_PBlock *pb )
  130. {
  131. LDAPDebug( LDAP_DEBUG_TRACE, "defbackend_noop\n", 0, 0, 0 );
  132. return( DEFBACKEND_OP_HANDLED );
  133. }
  134. static int
  135. defbackend_abandon( Slapi_PBlock *pb )
  136. {
  137. LDAPDebug( LDAP_DEBUG_TRACE, "defbackend_abandon\n", 0, 0, 0 );
  138. /* nothing to do */
  139. return( DEFBACKEND_OP_HANDLED );
  140. }
  141. static int
  142. defbackend_bind( Slapi_PBlock *pb )
  143. {
  144. int rc;
  145. ber_tag_t method;
  146. struct berval *cred;
  147. LDAPDebug( LDAP_DEBUG_TRACE, "defbackend_bind\n", 0, 0, 0 );
  148. /*
  149. * Accept simple binds that do not contain passwords (but do not
  150. * update the bind DN field in the connection structure since we don't
  151. * grant access based on these "NULL binds")
  152. */
  153. slapi_pblock_get( pb, SLAPI_BIND_METHOD, &method );
  154. slapi_pblock_get( pb, SLAPI_BIND_CREDENTIALS, &cred );
  155. if ( method == LDAP_AUTH_SIMPLE && cred->bv_len == 0 ) {
  156. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsAnonymousBinds);
  157. rc = SLAPI_BIND_ANONYMOUS;
  158. } else {
  159. send_nobackend_ldap_result( pb );
  160. rc = SLAPI_BIND_FAIL;
  161. }
  162. return( rc );
  163. }
  164. static int
  165. defbackend_next_search_entry( Slapi_PBlock *pb )
  166. {
  167. LDAPDebug( LDAP_DEBUG_TRACE, "defbackend_next_search_entry\n", 0, 0, 0 );
  168. return( 0 ); /* no entries and no error */
  169. }