init.c 11 KB

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