id2entry.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. /* id2entry.c - routines to deal with the id2entry index */
  42. #include "back-ldbm.h"
  43. #define ID2ENTRY "id2entry"
  44. /*
  45. * The caller MUST check for DB_LOCK_DEADLOCK and DB_RUNRECOVERY returned
  46. */
  47. int
  48. id2entry_add_ext( backend *be, struct backentry *e, back_txn *txn, int encrypt )
  49. {
  50. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  51. DB *db = NULL;
  52. DB_TXN *db_txn = NULL;
  53. DBT data;
  54. DBT key;
  55. int len, rc;
  56. char temp_id[sizeof(ID)];
  57. struct backentry *encrypted_entry = NULL;
  58. char *entrydn = NULL;
  59. LDAPDebug( LDAP_DEBUG_TRACE, "=> id2entry_add( %lu, \"%s\" )\n",
  60. (u_long)e->ep_id, backentry_get_ndn(e), 0 );
  61. if ( (rc = dblayer_get_id2entry( be, &db )) != 0 ) {
  62. LDAPDebug( LDAP_DEBUG_ANY, "Could not open/create id2entry\n",
  63. 0, 0, 0 );
  64. rc = -1;
  65. goto done;
  66. }
  67. id_internal_to_stored(e->ep_id,temp_id);
  68. memset(&key, 0, sizeof(key));
  69. key.dptr = temp_id;
  70. key.dsize = sizeof(temp_id);
  71. /* Encrypt attributes in this entry if necessary */
  72. if (encrypt) {
  73. rc = attrcrypt_encrypt_entry(be, e, &encrypted_entry);
  74. if (rc) {
  75. LDAPDebug( LDAP_DEBUG_ANY, "attrcrypt_encrypt_entry failed in id2entry_add\n",
  76. 0, 0, 0 );
  77. rc = -1;
  78. goto done;
  79. }
  80. }
  81. {
  82. int options = SLAPI_DUMP_STATEINFO | SLAPI_DUMP_UNIQUEID;
  83. Slapi_Entry *entry_to_use = encrypted_entry ? encrypted_entry->ep_entry : e->ep_entry;
  84. memset(&data, 0, sizeof(data));
  85. if (entryrdn_get_switch())
  86. {
  87. struct backdn *oldbdn = NULL;
  88. Slapi_DN *sdn =
  89. slapi_sdn_dup(slapi_entry_get_sdn_const(entry_to_use));
  90. struct backdn *bdn = backdn_init(sdn, e->ep_id, 0);
  91. options |= SLAPI_DUMP_RDN_ENTRY;
  92. /* If the ID already exists in the DN cache && the DNs do not match,
  93. * replace it. */
  94. if (CACHE_ADD( &inst->inst_dncache, bdn, &oldbdn ) == 1) {
  95. if (slapi_sdn_compare(sdn, oldbdn->dn_sdn)) {
  96. if (cache_replace( &inst->inst_dncache, oldbdn, bdn ) != 0) {
  97. /* The entry was not in the cache for some reason (this
  98. * should not happen since CACHE_ADD said it existed above). */
  99. LDAPDebug( LDAP_DEBUG_ANY, "id2entry_add_ext(): Entry disappeared "
  100. "from cache (%s)\n", oldbdn->dn_sdn, 0, 0 );
  101. }
  102. }
  103. CACHE_RETURN(&inst->inst_dncache, &oldbdn); /* to free oldbdn */
  104. }
  105. CACHE_RETURN(&inst->inst_dncache, &bdn);
  106. LDAPDebug2Args( LDAP_DEBUG_TRACE,
  107. "=> id2entry_add (dncache) ( %lu, \"%s\" )\n",
  108. (u_long)e->ep_id, slapi_entry_get_dn_const(entry_to_use) );
  109. }
  110. data.dptr = slapi_entry2str_with_options(entry_to_use, &len, options);
  111. data.dsize = len + 1;
  112. }
  113. if (NULL != txn) {
  114. db_txn = txn->back_txn_txn;
  115. }
  116. /* call pre-entry-store plugin */
  117. plugin_call_entrystore_plugins( (char **) &data.dptr, &data.dsize );
  118. /* store it */
  119. rc = db->put( db, db_txn, &key, &data, 0);
  120. /* DBDB looks like we're freeing memory allocated by another DLL, which is bad */
  121. slapi_ch_free( &(data.dptr) );
  122. dblayer_release_id2entry( be, db );
  123. if (0 == rc)
  124. {
  125. /* Putting the entry into the entry cache.
  126. * We don't use the encrypted entry here. */
  127. if (entryrdn_get_switch()) {
  128. struct backentry *parententry = NULL;
  129. ID parentid = slapi_entry_attr_get_ulong(e->ep_entry, "parentid");
  130. const char *myrdn = slapi_entry_get_rdn_const(e->ep_entry);
  131. const char *parentdn = NULL;
  132. char *myparentdn = NULL;
  133. Slapi_Attr *eattr = NULL;
  134. /* If the parent is in the cache, check the parent's DN and
  135. * adjust to it if they don't match. (bz628300) */
  136. if (parentid && myrdn) {
  137. parententry = cache_find_id(&inst->inst_cache, parentid);
  138. if (parententry) {
  139. parentdn = slapi_entry_get_dn_const(parententry->ep_entry);
  140. if (parentdn) {
  141. myparentdn =
  142. slapi_dn_parent(slapi_entry_get_dn_const(e->ep_entry));
  143. if (myparentdn && PL_strcmp(parentdn, myparentdn)) {
  144. Slapi_DN *sdn = slapi_entry_get_sdn(e->ep_entry);
  145. char *newdn = NULL;
  146. slapi_sdn_done(sdn);
  147. newdn = slapi_ch_smprintf("%s,%s", myrdn, parentdn);
  148. slapi_sdn_init_dn_passin(sdn, newdn);
  149. slapi_sdn_get_ndn(sdn); /* to set ndn */
  150. }
  151. slapi_ch_free_string(&myparentdn);
  152. }
  153. CACHE_RETURN(&inst->inst_cache, &parententry);
  154. }
  155. }
  156. /*
  157. * Adding entrydn attribute value to the entry,
  158. * which should be done before adding the entry to the entry cache.
  159. * Note: since we removed entrydn from the entry before writing
  160. * it to the database, it is guaranteed not in the entry.
  161. */
  162. /* slapi_ch_strdup and slapi_dn_ignore_case never returns NULL */
  163. entrydn = slapi_ch_strdup(slapi_entry_get_dn_const(e->ep_entry));
  164. entrydn = slapi_dn_ignore_case(entrydn);
  165. slapi_entry_attr_set_charptr (e->ep_entry,
  166. LDBM_ENTRYDN_STR, entrydn);
  167. if (0 == slapi_entry_attr_find(e->ep_entry,
  168. LDBM_ENTRYDN_STR, &eattr)) {
  169. /* now entrydn should exist in the entry */
  170. /* Set it to operational attribute */
  171. eattr->a_flags = SLAPI_ATTR_FLAG_OPATTR;
  172. }
  173. slapi_ch_free_string(&entrydn);
  174. }
  175. /*
  176. * For ldbm_back_add and ldbm_back_modify, this entry had been already
  177. * reserved as a tentative entry. So, it should be safe.
  178. * For ldbm_back_modify, the original entry having the same dn/id
  179. * should be in the cache. Thus, this entry e won't be put into the
  180. * entry cache. It'll be added by cache_replace.
  181. */
  182. (void) CACHE_ADD( &inst->inst_cache, e, NULL );
  183. }
  184. done:
  185. /* If we had an encrypted entry, we no longer need it.
  186. * Note: encrypted_entry is not in the entry cache. */
  187. if (encrypted_entry) {
  188. backentry_free(&encrypted_entry);
  189. }
  190. LDAPDebug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
  191. return( rc );
  192. }
  193. int
  194. id2entry_add( backend *be, struct backentry *e, back_txn *txn )
  195. {
  196. return id2entry_add_ext(be,e,txn,1);
  197. }
  198. /*
  199. * The caller MUST check for DB_LOCK_DEADLOCK and DB_RUNRECOVERY returned
  200. */
  201. int
  202. id2entry_delete( backend *be, struct backentry *e, back_txn *txn )
  203. {
  204. DB *db = NULL;
  205. DB_TXN *db_txn = NULL;
  206. DBT key = {0};
  207. int rc;
  208. char temp_id[sizeof(ID)];
  209. LDAPDebug( LDAP_DEBUG_TRACE, "=> id2entry_delete( %lu, \"%s\" )\n",
  210. (u_long)e->ep_id, backentry_get_ndn(e), 0 );
  211. if ( (rc = dblayer_get_id2entry( be, &db )) != 0 ) {
  212. LDAPDebug( LDAP_DEBUG_ANY, "Could not open/create id2entry\n",
  213. 0, 0, 0 );
  214. return( -1 );
  215. }
  216. id_internal_to_stored(e->ep_id,temp_id);
  217. key.dptr = temp_id;
  218. key.dsize = sizeof(temp_id);
  219. if (NULL != txn) {
  220. db_txn = txn->back_txn_txn;
  221. }
  222. if (entryrdn_get_switch())
  223. {
  224. ldbm_instance *inst = (ldbm_instance *)be->be_instance_info;
  225. struct backdn *bdn = dncache_find_id(&inst->inst_dncache, e->ep_id);
  226. if (bdn) {
  227. slapi_log_error(SLAPI_LOG_CACHE, ID2ENTRY,
  228. "dncache_find_id returned: %s\n",
  229. slapi_sdn_get_dn(bdn->dn_sdn));
  230. CACHE_REMOVE(&inst->inst_dncache, bdn);
  231. CACHE_RETURN(&inst->inst_dncache, &bdn);
  232. }
  233. }
  234. rc = db->del( db,db_txn,&key,0 );
  235. dblayer_release_id2entry( be, db );
  236. LDAPDebug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
  237. return( rc );
  238. }
  239. struct backentry *
  240. id2entry( backend *be, ID id, back_txn *txn, int *err )
  241. {
  242. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  243. DB *db = NULL;
  244. DB_TXN *db_txn = NULL;
  245. DBT key = {0};
  246. DBT data = {0};
  247. struct backentry *e = NULL;
  248. Slapi_Entry *ee;
  249. char temp_id[sizeof(ID)];
  250. slapi_log_error(SLAPI_LOG_TRACE, ID2ENTRY,
  251. "=> id2entry(%lu)\n", (u_long)id);
  252. if ( (e = cache_find_id( &inst->inst_cache, id )) != NULL ) {
  253. slapi_log_error(SLAPI_LOG_TRACE, ID2ENTRY,
  254. "<= id2entry %p, dn \"%s\" (cache)\n",
  255. e, backentry_get_ndn(e));
  256. goto bail;
  257. }
  258. *err = dblayer_get_id2entry( be, &db );
  259. if ( (*err != 0) || (NULL == db) ) {
  260. slapi_log_error(SLAPI_LOG_FATAL, ID2ENTRY,
  261. "Could not open id2entry err %d\n", *err);
  262. return( NULL );
  263. }
  264. id_internal_to_stored(id,temp_id);
  265. key.data = temp_id;
  266. key.size = sizeof(temp_id);
  267. /* DBDB need to improve this, we're mallocing, freeing, all over the place here */
  268. data.flags = DB_DBT_MALLOC;
  269. if (NULL != txn) {
  270. db_txn = txn->back_txn_txn;
  271. }
  272. do {
  273. *err = db->get( db, db_txn, &key, &data, 0 );
  274. if ( (0 != *err) &&
  275. (DB_NOTFOUND != *err) && (DB_LOCK_DEADLOCK != *err) )
  276. {
  277. slapi_log_error(SLAPI_LOG_FATAL, ID2ENTRY, "db error %d (%s)\n",
  278. *err, dblayer_strerror( *err ));
  279. }
  280. }
  281. while ( (DB_LOCK_DEADLOCK == *err) && (txn == NULL) );
  282. if ( (0 != *err) && (DB_NOTFOUND != *err) && (DB_LOCK_DEADLOCK != *err) )
  283. {
  284. if ( (DB_BUFFER_SMALL == *err) && (data.dptr == NULL) )
  285. {
  286. /*
  287. * Now we are setting slapi_ch_malloc and its friends to libdb
  288. * by ENV->set_alloc in dblayer.c. As long as the functions are
  289. * used by libdb, it won't reach here.
  290. */
  291. slapi_log_error(SLAPI_LOG_FATAL, ID2ENTRY,
  292. "malloc failed in libdb; "
  293. "terminating the server; OS error %d (%s)\n",
  294. *err, slapd_system_strerror( *err ));
  295. exit (1);
  296. }
  297. dblayer_release_id2entry( be, db );
  298. return( NULL );
  299. }
  300. if ( data.dptr == NULL ) {
  301. slapi_log_error(SLAPI_LOG_TRACE, ID2ENTRY,
  302. "<= id2entry( %lu ) not found\n", (u_long)id);
  303. goto bail;
  304. }
  305. /* call post-entry plugin */
  306. plugin_call_entryfetch_plugins( (char **) &data.dptr, &data.dsize );
  307. if (entryrdn_get_switch()) {
  308. char *rdn = NULL;
  309. int rc = 0;
  310. /* rdn is allocated in get_value_from_string */
  311. rc = get_value_from_string((const char *)data.dptr, "rdn", &rdn);
  312. if (rc) {
  313. /* data.dptr may not include rdn: ..., try "dn: ..." */
  314. ee = slapi_str2entry( data.dptr, SLAPI_STR2ENTRY_NO_ENTRYDN );
  315. } else {
  316. char *normdn = NULL;
  317. struct backdn *bdn = dncache_find_id(&inst->inst_dncache, id);
  318. if (bdn) {
  319. normdn = slapi_ch_strdup(slapi_sdn_get_dn(bdn->dn_sdn));
  320. slapi_log_error(SLAPI_LOG_CACHE, ID2ENTRY,
  321. "dncache_find_id returned: %s\n", normdn);
  322. CACHE_RETURN(&inst->inst_dncache, &bdn);
  323. } else {
  324. Slapi_DN *sdn = NULL;
  325. rc = entryrdn_lookup_dn(be, rdn, id, &normdn, txn);
  326. if (rc) {
  327. slapi_log_error(SLAPI_LOG_TRACE, ID2ENTRY,
  328. "id2entry: entryrdn look up failed "
  329. "(rdn=%s, ID=%d)\n", rdn, id);
  330. /* Try rdn as dn. Could be RUV. */
  331. normdn = slapi_ch_strdup(rdn);
  332. }
  333. sdn = slapi_sdn_new_normdn_byval((const char *)normdn);
  334. bdn = backdn_init(sdn, id, 0);
  335. if (CACHE_ADD( &inst->inst_dncache, bdn, NULL )) {
  336. backdn_free(&bdn);
  337. slapi_log_error(SLAPI_LOG_CACHE, ID2ENTRY,
  338. "%s is already in the dn cache\n", normdn);
  339. } else {
  340. CACHE_RETURN(&inst->inst_dncache, &bdn);
  341. slapi_log_error(SLAPI_LOG_CACHE, ID2ENTRY,
  342. "entryrdn_lookup_dn returned: %s, "
  343. "and set to dn cache (id %d)\n", normdn, id);
  344. }
  345. }
  346. ee = slapi_str2entry_ext( (const char *)normdn, data.dptr,
  347. SLAPI_STR2ENTRY_NO_ENTRYDN );
  348. slapi_ch_free_string(&rdn);
  349. slapi_ch_free_string(&normdn);
  350. }
  351. } else {
  352. ee = slapi_str2entry( data.dptr, 0 );
  353. }
  354. if ( ee != NULL ) {
  355. int retval = 0;
  356. struct backentry *imposter = NULL;
  357. /* All entries should have uniqueids */
  358. PR_ASSERT(slapi_entry_get_uniqueid(ee) != NULL);
  359. /* ownership of the entry is passed into the backentry */
  360. e = backentry_init( ee );
  361. e->ep_id = id;
  362. slapi_log_error(SLAPI_LOG_TRACE, ID2ENTRY,
  363. "id2entry id: %d, dn \"%s\" -- adding it to cache\n",
  364. id, backentry_get_ndn(e));
  365. /* Decrypt any encrypted attributes in this entry,
  366. * before adding it to the cache */
  367. retval = attrcrypt_decrypt_entry(be, e);
  368. if (retval) {
  369. slapi_log_error(SLAPI_LOG_FATAL, ID2ENTRY,
  370. "attrcrypt_decrypt_entry failed in id2entry\n");
  371. }
  372. /*
  373. * If return entry exists AND entryrdn switch is on,
  374. * add the entrydn value.
  375. */
  376. if (entryrdn_get_switch()) {
  377. Slapi_Attr *eattr = NULL;
  378. /* Check if entrydn is in the entry or not */
  379. if (slapi_entry_attr_find(e->ep_entry, LDBM_ENTRYDN_STR, &eattr)) {
  380. /* entrydn does not exist in the entry */
  381. char *entrydn = NULL;
  382. /* slapi_ch_strdup and slapi_dn_ignore_case never returns NULL */
  383. entrydn = slapi_ch_strdup(slapi_entry_get_dn_const(e->ep_entry));
  384. entrydn = slapi_dn_ignore_case(entrydn);
  385. slapi_entry_attr_set_charptr (e->ep_entry,
  386. LDBM_ENTRYDN_STR, entrydn);
  387. if (0 == slapi_entry_attr_find(e->ep_entry,
  388. LDBM_ENTRYDN_STR, &eattr)) {
  389. /* now entrydn should exist in the entry */
  390. /* Set it to operational attribute */
  391. eattr->a_flags = SLAPI_ATTR_FLAG_OPATTR;
  392. }
  393. slapi_ch_free_string(&entrydn);
  394. }
  395. }
  396. retval = CACHE_ADD( &inst->inst_cache, e, &imposter );
  397. if (1 == retval) {
  398. /* This means that someone else put the entry in the cache
  399. while we weren't looking ! So, we need to use the pointer
  400. returned and free the one we made earlier */
  401. if (imposter)
  402. {
  403. backentry_free(&e);
  404. e = imposter;
  405. }
  406. } else if (-1 == retval) {
  407. /* the entry is in idtable but not in dntable, i.e., the entry
  408. * could have been renamed */
  409. slapi_log_error(SLAPI_LOG_TRACE, ID2ENTRY,
  410. "id2entry: failed to put entry (id %lu, dn %s) "
  411. "into entry cache\n", (u_long)id,
  412. backentry_get_ndn(e));
  413. }
  414. } else {
  415. slapi_log_error(SLAPI_LOG_FATAL, ID2ENTRY,
  416. "str2entry returned NULL for id %lu, string=\"%s\"\n",
  417. (u_long)id, (char*)data.data);
  418. e = NULL;
  419. }
  420. bail:
  421. slapi_ch_free( &(data.data) );
  422. dblayer_release_id2entry( be, db );
  423. slapi_log_error(SLAPI_LOG_TRACE, ID2ENTRY,
  424. "<= id2entry( %lu ) %p (disk)\n", (u_long)id, e);
  425. return( e );
  426. }