backend_manager.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. /* backend_manager.c - routines for dealing with back-end databases */
  42. #include "slap.h"
  43. #define BACKEND_GRAB_SIZE 10
  44. /* JCM - searching the backend array is linear... */
  45. static int defsize = SLAPD_DEFAULT_SIZELIMIT;
  46. static int deftime = SLAPD_DEFAULT_TIMELIMIT;
  47. static int nbackends= 0;
  48. static Slapi_Backend **backends= NULL;
  49. static int maxbackends= 0;
  50. Slapi_Backend *
  51. slapi_be_new( const char *type, const char *name, int isprivate, int logchanges )
  52. {
  53. Slapi_Backend *be;
  54. int i;
  55. /* should add some locking here to prevent concurrent access */
  56. if ( nbackends == maxbackends )
  57. {
  58. int oldsize = maxbackends;
  59. maxbackends += BACKEND_GRAB_SIZE;
  60. backends = (Slapi_Backend **) slapi_ch_realloc( (char *) backends, maxbackends * sizeof(Slapi_Backend *) );
  61. memset( &backends[oldsize], '\0', BACKEND_GRAB_SIZE * sizeof(Slapi_Backend *) );
  62. }
  63. for (i=0; ((i<maxbackends) && (backends[i])); i++)
  64. ;
  65. PR_ASSERT(i<maxbackends);
  66. be = (Slapi_Backend *) slapi_ch_calloc(1, sizeof(Slapi_Backend));
  67. be->be_lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, name );
  68. be_init( be, type, name, isprivate, logchanges, defsize, deftime );
  69. backends[i] = be;
  70. nbackends++;
  71. slapi_log_error(SLAPI_LOG_TRACE, "slapi_be_new",
  72. "Added new backend name [%s] type [%s] nbackends [%d]\n",
  73. name, type, nbackends);
  74. return( be );
  75. }
  76. void
  77. slapi_be_stopping (Slapi_Backend *be)
  78. {
  79. int i;
  80. PR_Lock (be->be_state_lock);
  81. for (i=0; ((i<maxbackends) && backends[i] != be); i++)
  82. ;
  83. PR_ASSERT(i<maxbackends);
  84. backends[i] = NULL;
  85. be->be_state = BE_STATE_DELETED;
  86. if (be->be_lock != NULL)
  87. {
  88. PR_DestroyRWLock(be->be_lock);
  89. be->be_lock = NULL;
  90. }
  91. nbackends--;
  92. PR_Unlock (be->be_state_lock);
  93. }
  94. void
  95. slapi_be_free(Slapi_Backend **be)
  96. {
  97. be_done(*be);
  98. slapi_ch_free((void**)be);
  99. *be = NULL;
  100. }
  101. static int
  102. be_plgfn_unwillingtoperform(Slapi_PBlock *pb)
  103. {
  104. send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL, "Operation on Directory Specific Entry not allowed", 0, NULL );
  105. return -1;
  106. }
  107. /* JCM - Seems rather DSE specific... why's it here?... Should be in fedse.c... */
  108. Slapi_Backend *
  109. be_new_internal(struct dse *pdse, const char *type, const char *name)
  110. {
  111. Slapi_Backend *be= slapi_be_new(type, name, 1 /* Private */, 0 /* Do Not Log Changes */);
  112. be->be_database = (struct slapdplugin *) slapi_ch_calloc( 1, sizeof(struct slapdplugin) );
  113. be->be_database->plg_private= (void*)pdse;
  114. be->be_database->plg_bind= &dse_bind;
  115. be->be_database->plg_unbind= &dse_unbind;
  116. be->be_database->plg_search= &dse_search;
  117. be->be_database->plg_next_search_entry= &dse_next_search_entry;
  118. be->be_database->plg_search_results_release= &dse_search_set_release;
  119. be->be_database->plg_prev_search_results= &dse_prev_search_results;
  120. be->be_database->plg_compare= &be_plgfn_unwillingtoperform;
  121. be->be_database->plg_modify= &dse_modify;
  122. be->be_database->plg_modrdn= &be_plgfn_unwillingtoperform;
  123. be->be_database->plg_add= &dse_add;
  124. be->be_database->plg_delete= &dse_delete;
  125. be->be_database->plg_abandon= &be_plgfn_unwillingtoperform;
  126. be->be_database->plg_cleanup = dse_deletedse;
  127. /* All the other function pointers default to NULL */
  128. return be;
  129. }
  130. /*
  131. * Rule: before coming to this point, slapi_be_Wlock(be) must be acquired.
  132. */
  133. void
  134. be_replace_dse_internal(Slapi_Backend *be, struct dse *pdse)
  135. {
  136. be->be_database->plg_private= (void*)pdse;
  137. }
  138. Slapi_Backend*
  139. slapi_get_first_backend (char **cookie)
  140. {
  141. int i;
  142. for (i = 0; i < maxbackends; i++)
  143. {
  144. if ( backends[i] && (backends[i]->be_state != BE_STATE_DELETED))
  145. {
  146. *cookie = (char*)slapi_ch_malloc (sizeof (int));
  147. memcpy (*cookie, &i, sizeof (int));
  148. return backends[i];
  149. }
  150. }
  151. return NULL;
  152. }
  153. Slapi_Backend*
  154. slapi_get_next_backend (char *cookie)
  155. {
  156. int i, last_be;
  157. if (cookie == NULL)
  158. {
  159. LDAPDebug( LDAP_DEBUG_ARGS, "slapi_get_next_backend: NULL argument\n",
  160. 0, 0, 0 );
  161. return NULL;
  162. }
  163. last_be = *(int *)cookie;
  164. if ( last_be < 0 || last_be >= maxbackends)
  165. {
  166. LDAPDebug( LDAP_DEBUG_ARGS, "slapi_get_next_backend: argument out of range\n",
  167. 0, 0, 0 );
  168. return NULL;
  169. }
  170. if (last_be == maxbackends - 1)
  171. return NULL; /* done */
  172. for (i = last_be + 1; i < maxbackends; i++)
  173. {
  174. if (backends[i] && (backends[i]->be_state != BE_STATE_DELETED))
  175. {
  176. memcpy (cookie, &i, sizeof (int));
  177. return backends [i];
  178. }
  179. }
  180. return NULL;
  181. }
  182. Slapi_Backend *
  183. g_get_user_backend( int n )
  184. {
  185. int i, useri;
  186. useri = 0;
  187. for ( i = 0; i < maxbackends; i++ ) {
  188. if ( (backends[i] == NULL) || (backends[i]->be_private == 1) ) {
  189. continue;
  190. }
  191. if ( useri == n ) {
  192. if (backends[i]->be_state != BE_STATE_DELETED)
  193. return backends[i];
  194. else
  195. return NULL;
  196. }
  197. useri++;
  198. }
  199. return NULL;
  200. }
  201. void
  202. g_set_deftime(int val)
  203. {
  204. deftime = val;
  205. }
  206. void
  207. g_set_defsize(int val)
  208. {
  209. defsize = val;
  210. }
  211. int
  212. g_get_deftime()
  213. {
  214. return deftime;
  215. }
  216. int
  217. g_get_defsize()
  218. {
  219. return defsize;
  220. }
  221. int strtrimcasecmp(const char *s1, const char *s2)
  222. {
  223. char * s1bis, *s2bis;
  224. int len_s1 = 0, len_s2 = 0;
  225. if ( ((s1 == NULL) && (s2 != NULL))
  226. || ((s2 == NULL) && (s1 != NULL)) )
  227. return 1;
  228. if ((s1 == NULL) && (s2 == NULL))
  229. return 0;
  230. while (*s1 == ' ')
  231. s1++;
  232. while (*s2 == ' ')
  233. s2++;
  234. s1bis = (char *) s1;
  235. while ((*s1bis != ' ') && (*s1bis != 0))
  236. {
  237. len_s1 ++;
  238. s1bis ++;
  239. }
  240. s2bis = (char *) s2;
  241. while ((*s2bis != ' ') && (*s2bis != 0))
  242. {
  243. len_s2 ++;
  244. s2bis ++;
  245. }
  246. if (len_s2 != len_s1)
  247. return 1;
  248. return strncasecmp(s1, s2, len_s1);
  249. }
  250. /*
  251. * Find the backend of the given type.
  252. */
  253. Slapi_Backend *
  254. slapi_be_select_by_instance_name( const char *name )
  255. {
  256. int i;
  257. for ( i = 0; i < maxbackends; i++ )
  258. {
  259. if ( backends[i] && (backends[i]->be_state != BE_STATE_DELETED) &&
  260. strtrimcasecmp( backends[i]->be_name, name ) == 0)
  261. {
  262. return backends[i];
  263. }
  264. }
  265. return NULL;
  266. }
  267. /* void
  268. be_cleanupall()
  269. {
  270. int i;
  271. Slapi_PBlock pb;
  272. for ( i = 0; i < maxbackends; i++ )
  273. {
  274. if ( backends[i] &&
  275. backends[i]->be_cleanup != NULL &&
  276. (backends[i]->be_state == BE_STATE_STOPPED ||
  277. backends[i]->be_state == BE_STATE_DELETED))
  278. {
  279. slapi_pblock_set( &pb, SLAPI_PLUGIN, backends[i]->be_database );
  280. slapi_pblock_set( &pb, SLAPI_BACKEND, backends[i] );
  281. (*backends[i]->be_cleanup)( &pb );
  282. }
  283. }
  284. }*/
  285. void
  286. be_cleanupall()
  287. {
  288. int i;
  289. Slapi_PBlock pb;
  290. for ( i = 0; i < maxbackends; i++ )
  291. {
  292. if (backends[i] &&
  293. backends[i]->be_cleanup != NULL &&
  294. (backends[i]->be_state == BE_STATE_STOPPED ||
  295. backends[i]->be_state == BE_STATE_DELETED))
  296. {
  297. slapi_pblock_set( &pb, SLAPI_PLUGIN, backends[i]->be_database );
  298. slapi_pblock_set( &pb, SLAPI_BACKEND, backends[i] );
  299. (*backends[i]->be_cleanup)( &pb );
  300. slapi_be_free(&backends[i]);
  301. }
  302. }
  303. slapi_ch_free((void**)&backends);
  304. }
  305. void
  306. be_flushall()
  307. {
  308. int i;
  309. Slapi_PBlock pb;
  310. for ( i = 0; i < maxbackends; i++ )
  311. {
  312. if ( backends[i] &&
  313. backends[i]->be_state == BE_STATE_STARTED &&
  314. backends[i]->be_flush != NULL )
  315. {
  316. slapi_pblock_set( &pb, SLAPI_PLUGIN, backends[i]->be_database );
  317. slapi_pblock_set( &pb, SLAPI_BACKEND, backends[i] );
  318. (*backends[i]->be_flush)( &pb );
  319. }
  320. }
  321. }
  322. void
  323. be_unbindall(Connection *conn, Operation *op)
  324. {
  325. int i;
  326. Slapi_PBlock pb;
  327. for ( i = 0; i < maxbackends; i++ )
  328. {
  329. if ( backends[i] && (backends[i]->be_unbind != NULL) )
  330. {
  331. pblock_init_common( &pb, backends[i], conn, op );
  332. if ( plugin_call_plugins( &pb, SLAPI_PLUGIN_PRE_UNBIND_FN ) == 0 )
  333. {
  334. int rc = 0;
  335. slapi_pblock_set( &pb, SLAPI_PLUGIN, backends[i]->be_database );
  336. if(backends[i]->be_state != BE_STATE_DELETED &&
  337. backends[i]->be_unbind!=NULL)
  338. {
  339. rc = (*backends[i]->be_unbind)( &pb );
  340. }
  341. slapi_pblock_set( &pb, SLAPI_PLUGIN_OPRETURN, &rc );
  342. (void) plugin_call_plugins( &pb, SLAPI_PLUGIN_POST_UNBIND_FN );
  343. }
  344. }
  345. }
  346. }
  347. int
  348. be_nbackends_public()
  349. {
  350. int i;
  351. int n= 0;
  352. for ( i = 0; i < maxbackends; i++ )
  353. {
  354. if ( backends[i] &&
  355. (backends[i]->be_state != BE_STATE_DELETED) &&
  356. (!backends[i]->be_private) )
  357. {
  358. n++;
  359. }
  360. }
  361. return n;
  362. }
  363. /* backend instance management */
  364. void
  365. slapi_be_Rlock(Slapi_Backend * be)
  366. {
  367. PR_RWLock_Rlock(be->be_lock);
  368. }
  369. void
  370. slapi_be_Wlock(Slapi_Backend * be)
  371. {
  372. PR_RWLock_Wlock(be->be_lock);
  373. }
  374. void
  375. slapi_be_Unlock(Slapi_Backend * be)
  376. {
  377. PR_RWLock_Unlock(be->be_lock);
  378. }
  379. /*
  380. * lookup instance names by suffix.
  381. * if isexact == 0: returns instances including ones that associates with
  382. * its sub suffixes.
  383. * e.g., suffix: "o=<suffix>" is given, these are returned:
  384. * suffixes: o=<suffix>, ou=<ou>,o=<suffix>, ...
  385. * instances: inst of "o=<suffix>",
  386. * inst of "ou=<ou>,o=<suffix>",
  387. * ...
  388. * if isexact != 0: returns an instance that associates with the given suffix
  389. * e.g., suffix: "o=<suffix>" is given, these are returned:
  390. * suffixes: "o=<suffix>"
  391. * instances: inst of "o=<suffix>"
  392. * Note: if suffixes
  393. */
  394. int
  395. slapi_lookup_instance_name_by_suffix(char *suffix,
  396. char ***suffixes, char ***instances, int isexact)
  397. {
  398. Slapi_Backend *be = NULL;
  399. char *cookie = NULL;
  400. const char *thisdn;
  401. int thisdnlen;
  402. int suffixlen;
  403. int i;
  404. int rval = -1;
  405. if (instances == NULL)
  406. return rval;
  407. PR_ASSERT(suffix);
  408. rval = 0;
  409. suffixlen = strlen(suffix);
  410. cookie = NULL;
  411. be = slapi_get_first_backend (&cookie);
  412. while (be) {
  413. if (NULL == be->be_suffix) {
  414. be = (backend *)slapi_get_next_backend (cookie);
  415. continue;
  416. }
  417. PR_Lock(be->be_suffixlock);
  418. for (i = 0; be->be_suffix && i < be->be_suffixcount; i++) {
  419. thisdn = slapi_sdn_get_ndn(be->be_suffix[i]);
  420. thisdnlen = slapi_sdn_get_ndn_len(be->be_suffix[i]);
  421. if (isexact?suffixlen!=thisdnlen:suffixlen>thisdnlen)
  422. continue;
  423. if (isexact?(!slapi_UTF8CASECMP(suffix, (char *)thisdn)):
  424. (!slapi_UTF8CASECMP(suffix,
  425. (char *)thisdn+thisdnlen-suffixlen))) {
  426. charray_add(instances, slapi_ch_strdup(be->be_name));
  427. if (suffixes)
  428. charray_add(suffixes, slapi_ch_strdup(thisdn));
  429. }
  430. }
  431. PR_Unlock(be->be_suffixlock);
  432. be = (backend *)slapi_get_next_backend (cookie);
  433. }
  434. slapi_ch_free((void **)&cookie);
  435. return rval;
  436. }
  437. /*
  438. * lookup instance names by included suffixes and excluded suffixes.
  439. *
  440. * Get instance names associated with the given included suffixes
  441. * as well as the excluded suffixes.
  442. * Subtract the excluded instances from the included instance.
  443. * Assign the result to instances.
  444. */
  445. int
  446. slapi_lookup_instance_name_by_suffixes(char **included, char **excluded,
  447. char ***instances)
  448. {
  449. char **incl_instances, **excl_instances;
  450. char **p;
  451. int rval = -1;
  452. if (instances == NULL)
  453. return rval;
  454. *instances = NULL;
  455. incl_instances = NULL;
  456. for (p = included; p && *p; p++) {
  457. if (slapi_lookup_instance_name_by_suffix(*p, NULL, &incl_instances, 0)
  458. < 0)
  459. return rval;
  460. }
  461. excl_instances = NULL;
  462. for (p = excluded; p && *p; p++) {
  463. /* okay to be empty */
  464. slapi_lookup_instance_name_by_suffix(*p, NULL, &excl_instances, 0);
  465. }
  466. rval = 0;
  467. if (excl_instances) {
  468. charray_subtract(incl_instances, excl_instances, NULL);
  469. charray_free(excl_instances);
  470. }
  471. *instances = incl_instances;
  472. return rval;
  473. }