backend_manager.c 13 KB

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