id2entry.c 14 KB

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