init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. /* init.c - initialize ldbm backend */
  42. #include "back-ldbm.h"
  43. #include "../slapi-plugin.h"
  44. #include "idlapi.h"
  45. static void *IDL_api[3];
  46. static Slapi_PluginDesc pdesc = { "ldbm-backend", VENDOR,
  47. PACKAGE_VERSION, "high-performance LDAP backend database plugin" };
  48. static int add_ldbm_internal_attr_syntax( const char *name, const char *oid,
  49. const char *syntax, const char *mr_equality, unsigned long extraflags );
  50. #ifdef _WIN32
  51. int *module_ldap_debug = 0;
  52. void
  53. plugin_init_debug_level(int *level_ptr)
  54. {
  55. module_ldap_debug = level_ptr;
  56. }
  57. #endif
  58. /* pb: not used */
  59. int
  60. ldbm_back_add_schema( Slapi_PBlock *pb )
  61. {
  62. int rc = add_ldbm_internal_attr_syntax( "entrydn",
  63. LDBM_ENTRYDN_OID, DN_SYNTAX_OID, DNMATCH_NAME,
  64. SLAPI_ATTR_FLAG_SINGLE );
  65. rc |= add_ldbm_internal_attr_syntax( "dncomp",
  66. LDBM_DNCOMP_OID, DN_SYNTAX_OID, DNMATCH_NAME,
  67. 0 );
  68. rc |= add_ldbm_internal_attr_syntax( "parentid",
  69. LDBM_PARENTID_OID, DIRSTRING_SYNTAX_OID, CASEIGNOREMATCH_NAME,
  70. SLAPI_ATTR_FLAG_SINGLE );
  71. rc |= add_ldbm_internal_attr_syntax( "entryid",
  72. LDBM_ENTRYID_OID, DIRSTRING_SYNTAX_OID, CASEIGNOREMATCH_NAME,
  73. SLAPI_ATTR_FLAG_SINGLE );
  74. rc |= add_ldbm_internal_attr_syntax( "entryusn",
  75. LDBM_ENTRYUSN_OID, INTEGER_SYNTAX_OID, INTFIRSTCOMPMATCH_NAME,
  76. SLAPI_ATTR_FLAG_SINGLE|SLAPI_ATTR_FLAG_NOUSERMOD );
  77. return rc;
  78. }
  79. int
  80. ldbm_back_init( Slapi_PBlock *pb )
  81. {
  82. struct ldbminfo *li;
  83. int rc;
  84. struct slapdplugin *p;
  85. static int interface_published = 0;
  86. LDAPDebug( LDAP_DEBUG_TRACE, "=> ldbm_back_init\n", 0, 0, 0 );
  87. slapi_pblock_get(pb, SLAPI_PLUGIN, &p);
  88. /* allocate backend-specific stuff */
  89. li = (struct ldbminfo *) slapi_ch_calloc( 1, sizeof(struct ldbminfo) );
  90. /* Record the identity of the ldbm plugin. The plugin
  91. * identity is used during internal ops. */
  92. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &(li->li_identity));
  93. /* keep a pointer back to the plugin */
  94. li->li_plugin = p;
  95. /* set shutdown flag to zero.*/
  96. li->li_shutdown = 0;
  97. /* Initialize the set of instances. */
  98. li->li_instance_set = objset_new(&ldbm_back_instance_set_destructor);
  99. /* initialize dblayer */
  100. if (dblayer_init(li)) {
  101. LDAPDebug( LDAP_DEBUG_ANY, "ldbm_back_init: dblayer_init failed\n",0, 0, 0 );
  102. return (-1);
  103. }
  104. /* Fill in the fields of the ldbminfo and the dblayer_private
  105. * structures with some default values */
  106. ldbm_config_setup_default(li);
  107. /* ask the factory to give us space in the Connection object
  108. * (only bulk import uses this)
  109. */
  110. if (slapi_register_object_extension(p->plg_name, SLAPI_EXT_CONNECTION,
  111. factory_constructor, factory_destructor,
  112. &li->li_bulk_import_object, &li->li_bulk_import_handle) != 0) {
  113. LDAPDebug(LDAP_DEBUG_ANY, "ldbm_back_init: "
  114. "slapi_register_object_extension failed.\n", 0, 0, 0);
  115. return (-1);
  116. }
  117. /* add some private attributes */
  118. rc = ldbm_back_add_schema( pb );
  119. /* set plugin private pointer and initialize locks, etc. */
  120. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_PRIVATE, (void *) li );
  121. if ((li->li_dbcache_mutex = PR_NewLock()) == NULL ) {
  122. LDAPDebug( LDAP_DEBUG_ANY, "ldbm_back_init: PR_NewLock failed\n",
  123. 0, 0, 0 );
  124. return(-1);
  125. }
  126. if ((li->li_shutdown_mutex = PR_NewLock()) == NULL ) {
  127. LDAPDebug( LDAP_DEBUG_ANY, "ldbm_back_init: PR_NewLock failed\n",
  128. 0, 0, 0 );
  129. return(-1);
  130. }
  131. if ((li->li_config_mutex = PR_NewLock()) == NULL ) {
  132. LDAPDebug( LDAP_DEBUG_ANY, "ldbm_back_init: PR_NewLock failed\n",
  133. 0, 0, 0 );
  134. return(-1);
  135. }
  136. if ((li->li_dbcache_cv = PR_NewCondVar( li->li_dbcache_mutex )) == NULL ) {
  137. LDAPDebug( LDAP_DEBUG_ANY, "ldbm_back_init: PR_NewCondVar failed\n", 0, 0, 0 );
  138. exit(-1);
  139. }
  140. /* set all of the necessary database plugin callback functions */
  141. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  142. (void *) SLAPI_PLUGIN_VERSION_03 );
  143. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  144. (void *)&pdesc );
  145. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_BIND_FN,
  146. (void *) ldbm_back_bind );
  147. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_UNBIND_FN,
  148. (void *) ldbm_back_unbind );
  149. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_SEARCH_FN,
  150. (void *) ldbm_back_search );
  151. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_NEXT_SEARCH_ENTRY_FN,
  152. (void *) ldbm_back_next_search_entry );
  153. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_NEXT_SEARCH_ENTRY_EXT_FN,
  154. (void *) ldbm_back_next_search_entry_ext );
  155. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_PREV_SEARCH_RESULTS_FN,
  156. (void *) ldbm_back_prev_search_results );
  157. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_ENTRY_RELEASE_FN,
  158. (void *) ldbm_back_entry_release );
  159. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_SEARCH_RESULTS_RELEASE_FN,
  160. (void *) ldbm_back_search_results_release );
  161. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_COMPARE_FN,
  162. (void *) ldbm_back_compare );
  163. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_MODIFY_FN,
  164. (void *) ldbm_back_modify );
  165. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_MODRDN_FN,
  166. (void *) ldbm_back_modrdn );
  167. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_ADD_FN,
  168. (void *) ldbm_back_add );
  169. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_DELETE_FN,
  170. (void *) ldbm_back_delete );
  171. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_ABANDON_FN,
  172. (void *) ldbm_back_abandon );
  173. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_CLOSE_FN,
  174. (void *) ldbm_back_close );
  175. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_CLEANUP_FN,
  176. (void *) ldbm_back_cleanup );
  177. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_FLUSH_FN,
  178. (void *) ldbm_back_flush );
  179. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_START_FN,
  180. (void *) ldbm_back_start );
  181. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_SEQ_FN,
  182. (void *) ldbm_back_seq );
  183. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_RMDB_FN,
  184. (void *) ldbm_back_rmdb );
  185. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_LDIF2DB_FN,
  186. (void *) ldbm_back_ldif2ldbm );
  187. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_DB2LDIF_FN,
  188. (void *) ldbm_back_ldbm2ldif );
  189. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_DB2INDEX_FN,
  190. (void *) ldbm_back_ldbm2index );
  191. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_ARCHIVE2DB_FN,
  192. (void *) ldbm_back_archive2ldbm );
  193. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_DB2ARCHIVE_FN,
  194. (void *) ldbm_back_ldbm2archive );
  195. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_UPGRADEDB_FN,
  196. (void *) ldbm_back_upgradedb );
  197. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_DBVERIFY_FN,
  198. (void *) ldbm_back_dbverify );
  199. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_BEGIN_FN,
  200. (void *) dblayer_plugin_begin );
  201. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_COMMIT_FN,
  202. (void *) dblayer_plugin_commit );
  203. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_ABORT_FN,
  204. (void *) dblayer_plugin_abort );
  205. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_SIZE_FN,
  206. (void *) ldbm_db_size );
  207. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_TEST_FN,
  208. (void *) ldbm_back_db_test );
  209. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_INIT_INSTANCE_FN,
  210. (void *) ldbm_back_init ); /* register itself so that the secon instance
  211. can be initialized */
  212. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_WIRE_IMPORT_FN,
  213. (void *) ldbm_back_wire_import );
  214. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DB_ADD_SCHEMA_FN,
  215. (void *) ldbm_back_add_schema );
  216. if ( rc != 0 ) {
  217. LDAPDebug( LDAP_DEBUG_ANY, "ldbm_back_init failed\n", 0, 0, 0 );
  218. return( -1 );
  219. }
  220. /* register the IDL interface with the API broker */
  221. if(!interface_published)
  222. {
  223. IDL_api[0] = 0;
  224. IDL_api[1] = (void *)idl_alloc;
  225. IDL_api[2] = (void *)idl_insert;
  226. if( slapi_apib_register(IDL_v1_0_GUID, IDL_api) )
  227. {
  228. LDAPDebug( LDAP_DEBUG_ANY, "ldbm_back_init: failed to publish IDL interface\n", 0, 0, 0);
  229. return( -1 );
  230. }
  231. interface_published = 1;
  232. }
  233. LDAPDebug( LDAP_DEBUG_TRACE, "<= ldbm_back_init\n", 0, 0, 0 );
  234. return( 0 );
  235. }
  236. /*
  237. * Add an attribute syntax using some default flags, etc.
  238. * Returns an LDAP error code (LDAP_SUCCESS if all goes well)
  239. */
  240. static int
  241. add_ldbm_internal_attr_syntax( const char *name, const char *oid,
  242. const char *syntax, const char *mr_equality, unsigned long extraflags )
  243. {
  244. int rc = LDAP_SUCCESS;
  245. struct asyntaxinfo *asip;
  246. char *names[2];
  247. char *origins[2];
  248. unsigned long std_flags = SLAPI_ATTR_FLAG_STD_ATTR | SLAPI_ATTR_FLAG_OPATTR
  249. | SLAPI_ATTR_FLAG_NOUSERMOD;
  250. names[0] = (char *)name;
  251. names[1] = NULL;
  252. origins[0] = SLAPD_VERSION_STR;
  253. origins[1] = NULL;
  254. rc = attr_syntax_create( oid, names, 1,
  255. "internal server defined attribute type",
  256. NULL, /* superior */
  257. mr_equality, NULL, NULL, /* matching rules */
  258. origins, syntax,
  259. SLAPI_SYNTAXLENGTH_NONE,
  260. std_flags | extraflags,
  261. &asip );
  262. if ( rc == LDAP_SUCCESS ) {
  263. rc = attr_syntax_add( asip );
  264. }
  265. return rc;
  266. }