backend_manager.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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_compare= &be_plgfn_unwillingtoperform;
  119. be->be_database->plg_modify= &dse_modify;
  120. be->be_database->plg_modrdn= &be_plgfn_unwillingtoperform;
  121. be->be_database->plg_add= &dse_add;
  122. be->be_database->plg_delete= &dse_delete;
  123. be->be_database->plg_abandon= &be_plgfn_unwillingtoperform;
  124. be->be_database->plg_cleanup = dse_deletedse;
  125. /* All the other function pointers default to NULL */
  126. return be;
  127. }
  128. /*
  129. * Rule: before coming to this point, slapi_be_Wlock(be) must be acquired.
  130. */
  131. void
  132. be_replace_dse_internal(Slapi_Backend *be, struct dse *pdse)
  133. {
  134. be->be_database->plg_private= (void*)pdse;
  135. }
  136. Slapi_Backend*
  137. slapi_get_first_backend (char **cookie)
  138. {
  139. int i;
  140. for (i = 0; i < maxbackends; i++)
  141. {
  142. if ( backends[i] && (backends[i]->be_state != BE_STATE_DELETED))
  143. {
  144. *cookie = (char*)slapi_ch_malloc (sizeof (int));
  145. memcpy (*cookie, &i, sizeof (int));
  146. return backends[i];
  147. }
  148. }
  149. return NULL;
  150. }
  151. Slapi_Backend*
  152. slapi_get_next_backend (char *cookie)
  153. {
  154. int i, last_be;
  155. if (cookie == NULL)
  156. {
  157. LDAPDebug( LDAP_DEBUG_ARGS, "slapi_get_next_backend: NULL argument\n",
  158. 0, 0, 0 );
  159. return NULL;
  160. }
  161. last_be = *(int *)cookie;
  162. if ( last_be < 0 || last_be >= maxbackends)
  163. {
  164. LDAPDebug( LDAP_DEBUG_ARGS, "slapi_get_next_backend: argument out of range\n",
  165. 0, 0, 0 );
  166. return NULL;
  167. }
  168. if (last_be == maxbackends - 1)
  169. return NULL; /* done */
  170. for (i = last_be + 1; i < maxbackends; i++)
  171. {
  172. if (backends[i] && (backends[i]->be_state != BE_STATE_DELETED))
  173. {
  174. memcpy (cookie, &i, sizeof (int));
  175. return backends [i];
  176. }
  177. }
  178. return NULL;
  179. }
  180. Slapi_Backend *
  181. g_get_user_backend( int n )
  182. {
  183. int i, useri;
  184. useri = 0;
  185. for ( i = 0; i < maxbackends; i++ ) {
  186. if ( (backends[i] == NULL) || (backends[i]->be_private == 1) ) {
  187. continue;
  188. }
  189. if ( useri == n ) {
  190. if (backends[i]->be_state != BE_STATE_DELETED)
  191. return backends[i];
  192. else
  193. return NULL;
  194. }
  195. useri++;
  196. }
  197. return NULL;
  198. }
  199. void
  200. g_set_deftime(int val)
  201. {
  202. deftime = val;
  203. }
  204. void
  205. g_set_defsize(int val)
  206. {
  207. defsize = val;
  208. }
  209. int
  210. g_get_deftime()
  211. {
  212. return deftime;
  213. }
  214. int
  215. g_get_defsize()
  216. {
  217. return defsize;
  218. }
  219. int strtrimcasecmp(const char *s1, const char *s2)
  220. {
  221. char * s1bis, *s2bis;
  222. int len_s1 = 0, len_s2 = 0;
  223. if ( ((s1 == NULL) && (s2 != NULL))
  224. || ((s2 == NULL) && (s1 != NULL)) )
  225. return 1;
  226. if ((s1 == NULL) && (s2 == NULL))
  227. return 0;
  228. while (*s1 == ' ')
  229. s1++;
  230. while (*s2 == ' ')
  231. s2++;
  232. s1bis = (char *) s1;
  233. while ((*s1bis != ' ') && (*s1bis != 0))
  234. {
  235. len_s1 ++;
  236. s1bis ++;
  237. }
  238. s2bis = (char *) s2;
  239. while ((*s2bis != ' ') && (*s2bis != 0))
  240. {
  241. len_s2 ++;
  242. s2bis ++;
  243. }
  244. if (len_s2 != len_s1)
  245. return 1;
  246. return strncasecmp(s1, s2, len_s1);
  247. }
  248. /*
  249. * Find the backend of the given type.
  250. */
  251. Slapi_Backend *
  252. slapi_be_select_by_instance_name( const char *name )
  253. {
  254. int i;
  255. for ( i = 0; i < maxbackends; i++ )
  256. {
  257. if ( backends[i] && (backends[i]->be_state != BE_STATE_DELETED) &&
  258. strtrimcasecmp( backends[i]->be_name, name ) == 0)
  259. {
  260. return backends[i];
  261. }
  262. }
  263. return NULL;
  264. }
  265. /* void
  266. be_cleanupall()
  267. {
  268. int i;
  269. Slapi_PBlock pb;
  270. for ( i = 0; i < maxbackends; i++ )
  271. {
  272. if ( backends[i] &&
  273. backends[i]->be_cleanup != NULL &&
  274. (backends[i]->be_state == BE_STATE_STOPPED ||
  275. backends[i]->be_state == BE_STATE_DELETED))
  276. {
  277. slapi_pblock_set( &pb, SLAPI_PLUGIN, backends[i]->be_database );
  278. slapi_pblock_set( &pb, SLAPI_BACKEND, backends[i] );
  279. (*backends[i]->be_cleanup)( &pb );
  280. }
  281. }
  282. }*/
  283. void
  284. be_cleanupall()
  285. {
  286. int i;
  287. Slapi_PBlock pb;
  288. for ( i = 0; i < maxbackends; i++ )
  289. {
  290. if (backends[i] &&
  291. backends[i]->be_cleanup != NULL &&
  292. (backends[i]->be_state == BE_STATE_STOPPED ||
  293. backends[i]->be_state == BE_STATE_DELETED))
  294. {
  295. slapi_pblock_set( &pb, SLAPI_PLUGIN, backends[i]->be_database );
  296. slapi_pblock_set( &pb, SLAPI_BACKEND, backends[i] );
  297. (*backends[i]->be_cleanup)( &pb );
  298. slapi_be_free(&backends[i]);
  299. }
  300. }
  301. slapi_ch_free((void**)&backends);
  302. }
  303. void
  304. be_flushall()
  305. {
  306. int i;
  307. Slapi_PBlock pb;
  308. for ( i = 0; i < maxbackends; i++ )
  309. {
  310. if ( backends[i] &&
  311. backends[i]->be_state == BE_STATE_STARTED &&
  312. backends[i]->be_flush != NULL )
  313. {
  314. slapi_pblock_set( &pb, SLAPI_PLUGIN, backends[i]->be_database );
  315. slapi_pblock_set( &pb, SLAPI_BACKEND, backends[i] );
  316. (*backends[i]->be_flush)( &pb );
  317. }
  318. }
  319. }
  320. void
  321. be_unbindall(Connection *conn, Operation *op)
  322. {
  323. int i;
  324. Slapi_PBlock pb;
  325. for ( i = 0; i < maxbackends; i++ )
  326. {
  327. if ( backends[i] && (backends[i]->be_unbind != NULL) )
  328. {
  329. pblock_init_common( &pb, backends[i], conn, op );
  330. if ( plugin_call_plugins( &pb, SLAPI_PLUGIN_PRE_UNBIND_FN ) == 0 )
  331. {
  332. int rc;
  333. slapi_pblock_set( &pb, SLAPI_PLUGIN, backends[i]->be_database );
  334. if(backends[i]->be_state != BE_STATE_DELETED &&
  335. backends[i]->be_unbind!=NULL)
  336. {
  337. rc = (*backends[i]->be_unbind)( &pb );
  338. }
  339. slapi_pblock_set( &pb, SLAPI_PLUGIN_OPRETURN, &rc );
  340. (void) plugin_call_plugins( &pb, SLAPI_PLUGIN_POST_UNBIND_FN );
  341. }
  342. }
  343. }
  344. }
  345. int
  346. be_nbackends_public()
  347. {
  348. int i;
  349. int n= 0;
  350. for ( i = 0; i < maxbackends; i++ )
  351. {
  352. if ( backends[i] &&
  353. (backends[i]->be_state != BE_STATE_DELETED) &&
  354. (!backends[i]->be_private) )
  355. {
  356. n++;
  357. }
  358. }
  359. return n;
  360. }
  361. /* backend instance management */
  362. /* JCM - These are hardcoded for the LDBM database */
  363. #define LDBM_CLASS_PREFIX "cn=ldbm database,cn=plugins,cn=config"
  364. #define LDBM_CONFIG_ENTRY "cn=config,cn=ldbm database,cn=plugins,cn=config"
  365. #define INSTANCE_ATTR "nsslapd-instance"
  366. #define SUFFIX_ATTR "nsslapd-suffix"
  367. #define CACHE_ATTR "nsslapd-cachememsize"
  368. void
  369. slapi_be_Rlock(Slapi_Backend * be)
  370. {
  371. PR_RWLock_Rlock(be->be_lock);
  372. }
  373. void
  374. slapi_be_Wlock(Slapi_Backend * be)
  375. {
  376. PR_RWLock_Wlock(be->be_lock);
  377. }
  378. void
  379. slapi_be_Unlock(Slapi_Backend * be)
  380. {
  381. PR_RWLock_Unlock(be->be_lock);
  382. }
  383. /*
  384. * lookup instance names by suffix.
  385. * if isexact == 0: returns instances including ones that associates with
  386. * its sub suffixes.
  387. * e.g., suffix: "o=<suffix>" is given, these are returned:
  388. * suffixes: o=<suffix>, ou=<ou>,o=<suffix>, ...
  389. * instances: inst of "o=<suffix>",
  390. * inst of "ou=<ou>,o=<suffix>",
  391. * ...
  392. * if isexact != 0: returns an instance that associates with the given suffix
  393. * e.g., suffix: "o=<suffix>" is given, these are returned:
  394. * suffixes: "o=<suffix>"
  395. * instances: inst of "o=<suffix>"
  396. * Note: if suffixes
  397. */
  398. int
  399. slapi_lookup_instance_name_by_suffix(char *suffix,
  400. char ***suffixes, char ***instances, int isexact)
  401. {
  402. Slapi_Backend *be = NULL;
  403. char *cookie = NULL;
  404. const char *thisdn;
  405. int thisdnlen;
  406. int suffixlen;
  407. int i;
  408. int rval = -1;
  409. if (instances == NULL)
  410. return rval;
  411. PR_ASSERT(suffix);
  412. rval = 0;
  413. suffixlen = strlen(suffix);
  414. cookie = NULL;
  415. be = slapi_get_first_backend (&cookie);
  416. while (be) {
  417. if (NULL == be->be_suffix) {
  418. be = (backend *)slapi_get_next_backend (cookie);
  419. continue;
  420. }
  421. PR_Lock(be->be_suffixlock);
  422. for (i = 0; be->be_suffix && i < be->be_suffixcount; i++) {
  423. thisdn = slapi_sdn_get_ndn(be->be_suffix[i]);
  424. thisdnlen = slapi_sdn_get_ndn_len(be->be_suffix[i]);
  425. if (isexact?suffixlen!=thisdnlen:suffixlen>thisdnlen)
  426. continue;
  427. if (isexact?(!slapi_UTF8CASECMP(suffix, (char *)thisdn)):
  428. (!slapi_UTF8CASECMP(suffix,
  429. (char *)thisdn+thisdnlen-suffixlen))) {
  430. charray_add(instances, slapi_ch_strdup(be->be_name));
  431. if (suffixes)
  432. charray_add(suffixes, slapi_ch_strdup(thisdn));
  433. }
  434. }
  435. PR_Unlock(be->be_suffixlock);
  436. be = (backend *)slapi_get_next_backend (cookie);
  437. }
  438. slapi_ch_free((void **)&cookie);
  439. return rval;
  440. }
  441. /*
  442. * lookup instance names by included suffixes and excluded suffixes.
  443. *
  444. * Get instance names associated with the given included suffixes
  445. * as well as the excluded suffixes.
  446. * Subtract the excluded instances from the included instance.
  447. * Assign the result to instances.
  448. */
  449. int
  450. slapi_lookup_instance_name_by_suffixes(char **included, char **excluded,
  451. char ***instances)
  452. {
  453. char **incl_instances, **excl_instances;
  454. char **p;
  455. int rval = -1;
  456. if (instances == NULL)
  457. return rval;
  458. *instances = NULL;
  459. incl_instances = NULL;
  460. for (p = included; p && *p; p++) {
  461. if (slapi_lookup_instance_name_by_suffix(*p, NULL, &incl_instances, 0)
  462. < 0)
  463. return rval;
  464. }
  465. excl_instances = NULL;
  466. for (p = excluded; p && *p; p++) {
  467. /* okay to be empty */
  468. slapi_lookup_instance_name_by_suffix(*p, NULL, &excl_instances, 0);
  469. }
  470. rval = 0;
  471. if (excl_instances) {
  472. charray_subtract(incl_instances, excl_instances, NULL);
  473. charray_free(excl_instances);
  474. }
  475. *instances = incl_instances;
  476. return rval;
  477. }