id2entry.c 19 KB

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