id2entry.c 17 KB

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