defbackend.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * defbackend.c - implement a "backend of last resort" which is used only
  43. * when a request's basedn does not match one of the suffixes of any of the
  44. * configured backends.
  45. *
  46. */
  47. #include "slap.h"
  48. /*
  49. * ---------------- Macros ---------------------------------------------------
  50. */
  51. #define DEFBACKEND_TYPE "default"
  52. #define DEFBACKEND_OP_NOT_HANDLED 0
  53. #define DEFBACKEND_OP_HANDLED 1
  54. /*
  55. * ---------------- Static Variables -----------------------------------------
  56. */
  57. static struct slapdplugin defbackend_plugin;
  58. static Slapi_Backend *defbackend_backend = NULL;
  59. /*
  60. * ---------------- Prototypes for Private Functions -------------------------
  61. */
  62. static int defbackend_default( Slapi_PBlock *pb );
  63. static int defbackend_noop( Slapi_PBlock *pb );
  64. static int defbackend_abandon( Slapi_PBlock *pb );
  65. static int defbackend_bind( Slapi_PBlock *pb );
  66. static int defbackend_next_search_entry( Slapi_PBlock *pb );
  67. /*
  68. * ---------------- Public Functions -----------------------------------------
  69. */
  70. /*
  71. * defbackend_init: instantiate the default backend
  72. */
  73. void
  74. defbackend_init( void )
  75. {
  76. int rc;
  77. char *errmsg;
  78. Slapi_PBlock pb;
  79. LDAPDebug( LDAP_DEBUG_TRACE, "defbackend_init\n", 0, 0, 0 );
  80. /*
  81. * create a new backend
  82. */
  83. pblock_init( &pb );
  84. defbackend_backend = slapi_be_new( DEFBACKEND_TYPE , DEFBACKEND_TYPE, 1 /* Private */, 0 /* Do Not Log Changes */ );
  85. if (( rc = slapi_pblock_set( &pb, SLAPI_BACKEND, defbackend_backend ))
  86. != 0 ) {
  87. errmsg = "slapi_pblock_set SLAPI_BACKEND failed";
  88. goto cleanup_and_return;
  89. }
  90. /*
  91. * create a plugin structure for this backend since the
  92. * slapi_pblock_set()/slapi_pblock_get() functions assume there is one.
  93. */
  94. memset( &defbackend_plugin, '\0', sizeof( struct slapdplugin ));
  95. defbackend_plugin.plg_type = SLAPI_PLUGIN_DATABASE;
  96. defbackend_backend->be_database = &defbackend_plugin;
  97. if (( rc = slapi_pblock_set( &pb, SLAPI_PLUGIN, &defbackend_plugin ))
  98. != 0 ) {
  99. errmsg = "slapi_pblock_set SLAPI_PLUGIN failed";
  100. goto cleanup_and_return;
  101. }
  102. /* default backend is managed as if it would */
  103. /* contain remote data. */
  104. slapi_be_set_flag(defbackend_backend,SLAPI_BE_FLAG_REMOTE_DATA);
  105. /*
  106. * install handler functions, etc.
  107. */
  108. errmsg = "slapi_pblock_set handlers failed";
  109. rc = slapi_pblock_set( &pb, SLAPI_PLUGIN_VERSION,
  110. (void *)SLAPI_PLUGIN_CURRENT_VERSION );
  111. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_BIND_FN,
  112. (void *)defbackend_bind );
  113. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_UNBIND_FN,
  114. (void *)defbackend_noop );
  115. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_SEARCH_FN,
  116. (void *)defbackend_default );
  117. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_NEXT_SEARCH_ENTRY_FN,
  118. (void *)defbackend_next_search_entry );
  119. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_COMPARE_FN,
  120. (void *)defbackend_default );
  121. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_MODIFY_FN,
  122. (void *)defbackend_default );
  123. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_MODRDN_FN,
  124. (void *)defbackend_default );
  125. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_ADD_FN,
  126. (void *)defbackend_default );
  127. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_DELETE_FN,
  128. (void *)defbackend_default );
  129. rc |= slapi_pblock_set( &pb, SLAPI_PLUGIN_DB_ABANDON_FN,
  130. (void *)defbackend_abandon );
  131. cleanup_and_return:
  132. if ( rc != 0 ) {
  133. LDAPDebug( LDAP_DEBUG_ANY, "defbackend_init: failed (%s)\n",
  134. errmsg, 0, 0 );
  135. exit( 1 );
  136. }
  137. }
  138. /*
  139. * defbackend_get_backend: return a pointer to the default backend.
  140. * we never return NULL.
  141. */
  142. Slapi_Backend *
  143. defbackend_get_backend( void )
  144. {
  145. return( defbackend_backend );
  146. }
  147. /*
  148. * ---------------- Private Functions ----------------------------------------
  149. */
  150. static int
  151. defbackend_default( Slapi_PBlock *pb )
  152. {
  153. LDAPDebug( LDAP_DEBUG_TRACE, "defbackend_default\n", 0, 0, 0 );
  154. send_nobackend_ldap_result( pb );
  155. return( DEFBACKEND_OP_HANDLED );
  156. }
  157. static int
  158. defbackend_noop( Slapi_PBlock *pb )
  159. {
  160. LDAPDebug( LDAP_DEBUG_TRACE, "defbackend_noop\n", 0, 0, 0 );
  161. return( DEFBACKEND_OP_HANDLED );
  162. }
  163. static int
  164. defbackend_abandon( Slapi_PBlock *pb )
  165. {
  166. LDAPDebug( LDAP_DEBUG_TRACE, "defbackend_abandon\n", 0, 0, 0 );
  167. /* nothing to do */
  168. return( DEFBACKEND_OP_HANDLED );
  169. }
  170. static int
  171. defbackend_bind( Slapi_PBlock *pb )
  172. {
  173. int rc, method;
  174. struct berval *cred;
  175. LDAPDebug( LDAP_DEBUG_TRACE, "defbackend_bind\n", 0, 0, 0 );
  176. /*
  177. * Accept simple binds that do not contain passwords (but do not
  178. * update the bind DN field in the connection structure since we don't
  179. * grant access based on these "NULL binds")
  180. */
  181. slapi_pblock_get( pb, SLAPI_BIND_METHOD, &method );
  182. slapi_pblock_get( pb, SLAPI_BIND_CREDENTIALS, &cred );
  183. if ( method == LDAP_AUTH_SIMPLE && cred->bv_len == 0 ) {
  184. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsAnonymousBinds);
  185. rc = SLAPI_BIND_ANONYMOUS;
  186. } else {
  187. send_nobackend_ldap_result( pb );
  188. rc = SLAPI_BIND_FAIL;
  189. }
  190. return( rc );
  191. }
  192. static int
  193. defbackend_next_search_entry( Slapi_PBlock *pb )
  194. {
  195. LDAPDebug( LDAP_DEBUG_TRACE, "defbackend_next_search_entry\n", 0, 0, 0 );
  196. return( 0 ); /* no entries and no error */
  197. }