ldbm_add.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. /* add.c - ldap ldbm back-end add routine */
  42. #include "back-ldbm.h"
  43. extern char *numsubordinates;
  44. extern char *hassubordinates;
  45. static void delete_update_entrydn_operational_attributes(struct backentry *ep);
  46. #define ADD_SET_ERROR(rc, error, count) \
  47. { \
  48. (rc) = (error); \
  49. (count) = RETRY_TIMES; /* otherwise, the transaction may not be aborted */ \
  50. }
  51. /* in order to find the parent, we must have either the parent dn or uniqueid
  52. This function will return true if either are set, or false otherwise */
  53. static int
  54. have_parent_address(const Slapi_DN *parentsdn, const char *parentuniqueid)
  55. {
  56. if (parentuniqueid && parentuniqueid[0]) {
  57. return 1; /* have parent uniqueid */
  58. }
  59. if (parentsdn && !slapi_sdn_isempty(parentsdn)) {
  60. return 1; /* have parent dn */
  61. }
  62. return 0; /* have no address */
  63. }
  64. int
  65. ldbm_back_add( Slapi_PBlock *pb )
  66. {
  67. backend *be;
  68. struct ldbminfo *li;
  69. ldbm_instance *inst;
  70. char *dn = NULL;
  71. Slapi_Entry *e = NULL;
  72. struct backentry *tombstoneentry = NULL;
  73. struct backentry *addingentry = NULL;
  74. struct backentry *parententry = NULL;
  75. ID pid;
  76. int isroot;
  77. char *errbuf= NULL;
  78. back_txn txn;
  79. back_txnid parent_txn;
  80. int retval = -1;
  81. char *msg;
  82. int managedsait;
  83. int ldap_result_code = LDAP_SUCCESS;
  84. char *ldap_result_message= NULL;
  85. char *ldap_result_matcheddn= NULL;
  86. int retry_count = 0;
  87. int disk_full = 0;
  88. modify_context parent_modify_c = {0};
  89. int parent_found = 0;
  90. int rc;
  91. int addingentry_id_assigned= 0;
  92. int addingentry_in_cache= 0;
  93. int tombstone_in_cache= 0;
  94. Slapi_DN sdn;
  95. Slapi_DN parentsdn;
  96. Slapi_Operation *operation;
  97. int dblock_acquired= 0;
  98. int is_replicated_operation= 0;
  99. int is_resurect_operation= 0;
  100. int is_tombstone_operation= 0;
  101. int is_fixup_operation= 0;
  102. int is_ruv = 0; /* True if the current entry is RUV */
  103. CSN *opcsn = NULL;
  104. entry_address addr = {0};
  105. slapi_pblock_get( pb, SLAPI_PLUGIN_PRIVATE, &li );
  106. slapi_pblock_get( pb, SLAPI_ADD_ENTRY, &e );
  107. slapi_pblock_get( pb, SLAPI_REQUESTOR_ISROOT, &isroot );
  108. slapi_pblock_get( pb, SLAPI_MANAGEDSAIT, &managedsait );
  109. slapi_pblock_get( pb, SLAPI_PARENT_TXN, (void**)&parent_txn );
  110. slapi_pblock_get( pb, SLAPI_OPERATION, &operation );
  111. slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation );
  112. slapi_pblock_get( pb, SLAPI_BACKEND, &be);
  113. is_resurect_operation= operation_is_flag_set(operation,OP_FLAG_RESURECT_ENTRY);
  114. is_tombstone_operation= operation_is_flag_set(operation,OP_FLAG_TOMBSTONE_ENTRY);
  115. is_fixup_operation = operation_is_flag_set(operation, OP_FLAG_REPL_FIXUP);
  116. is_ruv = operation_is_flag_set(operation, OP_FLAG_REPL_RUV);
  117. inst = (ldbm_instance *) be->be_instance_info;
  118. slapi_sdn_init(&sdn);
  119. slapi_sdn_init(&parentsdn);
  120. /* Get rid of ldbm backend attributes that you are not allowed to specify yourself */
  121. slapi_entry_delete_values( e, hassubordinates, NULL );
  122. slapi_entry_delete_values( e, numsubordinates, NULL );
  123. dblayer_txn_init(li,&txn);
  124. /* The dblock serializes writes to the database,
  125. * which reduces deadlocking in the db code,
  126. * which means that we run faster.
  127. *
  128. * But, this lock is re-enterant for the fixup
  129. * operations that the URP code in the Replication
  130. * plugin generates.
  131. */
  132. if(SERIALLOCK(li) && !is_fixup_operation)
  133. {
  134. dblayer_lock_backend(be);
  135. dblock_acquired= 1;
  136. }
  137. rc= 0;
  138. /*
  139. * We are about to pass the last abandon test, so from now on we are
  140. * committed to finish this operation. Set status to "will complete"
  141. * before we make our last abandon check to avoid race conditions in
  142. * the code that processes abandon operations.
  143. */
  144. if (operation) {
  145. operation->o_status = SLAPI_OP_STATUS_WILL_COMPLETE;
  146. }
  147. if ( slapi_op_abandoned( pb ) ) {
  148. ldap_result_code = -1; /* needs to distinguish from "success" */
  149. goto error_return;
  150. }
  151. if (!is_tombstone_operation && !is_resurect_operation)
  152. {
  153. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
  154. }
  155. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_UNIQUEID_ENTRY);
  156. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_PARENT_ENTRY);
  157. while(rc!=0)
  158. {
  159. /* JCM - copying entries can be expensive... should optimize */
  160. /*
  161. * Some present state information is passed through the PBlock to the
  162. * backend pre-op plugin. To ensure a consistent snapshot of this state
  163. * we wrap the reading of the entry with the dblock.
  164. */
  165. if(slapi_isbitset_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_UNIQUEID_ENTRY))
  166. {
  167. /* Check if an entry with the intended uniqueid already exists. */
  168. done_with_pblock_entry(pb,SLAPI_ADD_EXISTING_UNIQUEID_ENTRY); /* Could be through this multiple times */
  169. addr.dn = addr.udn = NULL;
  170. addr.uniqueid = (char*)slapi_entry_get_uniqueid(e); /* jcm - cast away const */
  171. ldap_result_code= get_copy_of_entry(pb, &addr, &txn, SLAPI_ADD_EXISTING_UNIQUEID_ENTRY, !is_replicated_operation);
  172. }
  173. if(slapi_isbitset_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY))
  174. {
  175. slapi_pblock_get( pb, SLAPI_ADD_TARGET, &dn );
  176. if (NULL == dn)
  177. {
  178. goto error_return;
  179. }
  180. ldap_result_code = slapi_dn_syntax_check(pb, dn, 1);
  181. if (ldap_result_code)
  182. {
  183. ldap_result_code = LDAP_INVALID_DN_SYNTAX;
  184. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  185. goto error_return;
  186. }
  187. slapi_sdn_set_dn_byref(&sdn, dn);
  188. slapi_sdn_get_backend_parent(&sdn,&parentsdn,pb->pb_backend);
  189. /* Check if an entry with the intended DN already exists. */
  190. done_with_pblock_entry(pb,SLAPI_ADD_EXISTING_DN_ENTRY); /* Could be through this multiple times */
  191. addr.dn = dn;
  192. addr.udn = NULL;
  193. addr.uniqueid = NULL;
  194. ldap_result_code= get_copy_of_entry(pb, &addr, &txn, SLAPI_ADD_EXISTING_DN_ENTRY, !is_replicated_operation);
  195. if(ldap_result_code==LDAP_OPERATIONS_ERROR ||
  196. ldap_result_code==LDAP_INVALID_DN_SYNTAX)
  197. {
  198. goto error_return;
  199. }
  200. }
  201. /* if we can find the parent by dn or uniqueid, and the operation has requested the parent
  202. then get it */
  203. if(have_parent_address(&parentsdn, operation->o_params.p.p_add.parentuniqueid) &&
  204. slapi_isbitset_int(rc,SLAPI_RTN_BIT_FETCH_PARENT_ENTRY))
  205. {
  206. done_with_pblock_entry(pb,SLAPI_ADD_PARENT_ENTRY); /* Could be through this multiple times */
  207. addr.dn = (char*)slapi_sdn_get_dn (&parentsdn); /* get_copy_of_entry assumes the DN is not normalized */
  208. addr.udn = NULL;
  209. addr.uniqueid = operation->o_params.p.p_add.parentuniqueid;
  210. ldap_result_code= get_copy_of_entry(pb, &addr, &txn, SLAPI_ADD_PARENT_ENTRY, !is_replicated_operation);
  211. /* need to set parentsdn or parentuniqueid if either is not set? */
  212. }
  213. /* Call the Backend Pre Add plugins */
  214. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  215. rc= plugin_call_plugins(pb, SLAPI_PLUGIN_BE_PRE_ADD_FN);
  216. if(rc==-1)
  217. {
  218. /*
  219. * Plugin indicated some kind of failure,
  220. * or that this Operation became a No-Op.
  221. */
  222. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  223. goto error_return;
  224. }
  225. /*
  226. * (rc!=-1 && rc!= 0) means that the plugin changed things, so we go around
  227. * the loop once again to get the new present state.
  228. */
  229. /* JCMREPL - Warning: A Plugin could cause an infinite loop by always returning a result code that requires some action. */
  230. }
  231. /*
  232. * Originally (in the U-M LDAP 3.3 code), there was a comment near this
  233. * code about a race condition. The race was that a 2nd entry could be
  234. * added between the time when we check for an already existing entry
  235. * and the cache_add_entry_lock() call below. A race condition no
  236. * longer exists, because now we keep the parent entry locked for
  237. * the duration of the old race condition's window of opportunity.
  238. */
  239. /*
  240. * Fetch the parent entry and acquire the cache lock.
  241. */
  242. if(have_parent_address(&parentsdn, operation->o_params.p.p_add.parentuniqueid))
  243. {
  244. addr.dn = (char*)slapi_sdn_get_dn (&parentsdn);
  245. addr.udn = NULL;
  246. addr.uniqueid = operation->o_params.p.p_add.parentuniqueid;
  247. parententry = find_entry2modify_only(pb,be,&addr,&txn);
  248. if (parententry && parententry->ep_entry) {
  249. if (!operation->o_params.p.p_add.parentuniqueid){
  250. /* Set the parentuniqueid now */
  251. operation->o_params.p.p_add.parentuniqueid = slapi_ch_strdup(slapi_entry_get_uniqueid(parententry->ep_entry));
  252. }
  253. if (slapi_sdn_isempty(&parentsdn)) {
  254. /* Set the parentsdn now */
  255. slapi_sdn_set_dn_byval(&parentsdn, slapi_entry_get_dn_const(parententry->ep_entry));
  256. }
  257. }
  258. modify_init(&parent_modify_c,parententry);
  259. }
  260. /* Check if the entry we have been asked to add already exists */
  261. {
  262. Slapi_Entry *entry;
  263. slapi_pblock_get( pb, SLAPI_ADD_EXISTING_DN_ENTRY, &entry);
  264. if ( entry != NULL )
  265. {
  266. /* The entry already exists */
  267. ldap_result_code= LDAP_ALREADY_EXISTS;
  268. goto error_return;
  269. }
  270. else
  271. {
  272. /*
  273. * did not find the entry - this is good, since we're
  274. * trying to add it, but we have to check whether the
  275. * entry we did match has a referral we should return
  276. * instead. we do this only if managedsait is not on.
  277. */
  278. if ( !managedsait && !is_tombstone_operation )
  279. {
  280. int err= 0;
  281. Slapi_DN ancestordn= {0};
  282. struct backentry *ancestorentry;
  283. ancestorentry= dn2ancestor(pb->pb_backend,&sdn,&ancestordn,&txn,&err);
  284. slapi_sdn_done(&ancestordn);
  285. if ( ancestorentry != NULL )
  286. {
  287. int sentreferral= check_entry_for_referral(pb, ancestorentry->ep_entry, backentry_get_ndn(ancestorentry), "ldbm_back_add");
  288. CACHE_RETURN( &inst->inst_cache, &ancestorentry );
  289. if(sentreferral)
  290. {
  291. ldap_result_code= -1; /* The result was sent by check_entry_for_referral */
  292. goto error_return;
  293. }
  294. }
  295. }
  296. }
  297. }
  298. if ((operation_is_flag_set(operation,OP_FLAG_ACTION_SCHEMA_CHECK)) && slapi_entry_schema_check(pb, e) != 0)
  299. {
  300. LDAPDebug(LDAP_DEBUG_TRACE, "entry failed schema check\n", 0, 0, 0);
  301. ldap_result_code = LDAP_OBJECT_CLASS_VIOLATION;
  302. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  303. goto error_return;
  304. }
  305. /* Check attribute syntax */
  306. if (slapi_entry_syntax_check(pb, e, 0) != 0)
  307. {
  308. LDAPDebug(LDAP_DEBUG_TRACE, "entry failed syntax check\n", 0, 0, 0);
  309. ldap_result_code = LDAP_INVALID_SYNTAX;
  310. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  311. goto error_return;
  312. }
  313. opcsn = operation_get_csn (operation);
  314. if(is_resurect_operation)
  315. {
  316. char *reason = NULL;
  317. /*
  318. * When we resurect a tombstone we must use its UniqueID
  319. * to find the tombstone entry and lock it down in the cache.
  320. */
  321. addr.dn = addr.udn = NULL;
  322. addr.uniqueid = (char *)slapi_entry_get_uniqueid(e); /* jcm - cast away const */
  323. tombstoneentry = find_entry2modify( pb, be, &addr, NULL );
  324. if ( tombstoneentry==NULL )
  325. {
  326. ldap_result_code= -1;
  327. goto error_return; /* error result sent by find_entry2modify() */
  328. }
  329. tombstone_in_cache = 1;
  330. addingentry = backentry_dup( tombstoneentry );
  331. if ( addingentry==NULL )
  332. {
  333. ldap_result_code= LDAP_OPERATIONS_ERROR;
  334. goto error_return;
  335. }
  336. /*
  337. * To resurect a tombstone we must fix its DN and remove the
  338. * parent UniqueID that we stashed in there.
  339. *
  340. * The entry comes back to life as a Glue entry, so we add the
  341. * magic objectclass.
  342. */
  343. slapi_pblock_get( pb, SLAPI_ADD_TARGET, &dn );
  344. slapi_sdn_set_dn_byref(&sdn, dn);
  345. slapi_entry_set_dn(addingentry->ep_entry, slapi_ch_strdup(dn)); /* The DN is passed into the entry. */
  346. /* LPREPL: the DN is normalized...Somehow who should get a not normalized one */
  347. addingentry->ep_id = slapi_entry_attr_get_ulong(addingentry->ep_entry,"entryid");
  348. slapi_entry_attr_delete(addingentry->ep_entry, SLAPI_ATTR_VALUE_PARENT_UNIQUEID);
  349. slapi_entry_delete_string(addingentry->ep_entry, SLAPI_ATTR_OBJECTCLASS, SLAPI_ATTR_VALUE_TOMBSTONE);
  350. /* Now also remove the nscpEntryDN */
  351. if (slapi_entry_attr_delete(addingentry->ep_entry, SLAPI_ATTR_NSCP_ENTRYDN) != 0){
  352. LDAPDebug(LDAP_DEBUG_REPL, "Resurrection of %s - Couldn't remove %s\n", dn, SLAPI_ATTR_NSCP_ENTRYDN, 0);
  353. }
  354. /* And copy the reason from e */
  355. reason = slapi_entry_attr_get_charptr(e, "nsds5ReplConflict");
  356. if (reason) {
  357. if (!slapi_entry_attr_hasvalue(addingentry->ep_entry, "nsds5ReplConflict", reason)) {
  358. slapi_entry_add_string(addingentry->ep_entry, "nsds5ReplConflict", reason);
  359. LDAPDebug(LDAP_DEBUG_REPL, "Resurrection of %s - Added Conflict reason %s\n", dn, reason, 0);
  360. }
  361. slapi_ch_free((void **)&reason);
  362. }
  363. /* Clear the Tombstone Flag in the entry */
  364. slapi_entry_clear_flag(addingentry->ep_entry, SLAPI_ENTRY_FLAG_TOMBSTONE);
  365. /* make sure the objectclass
  366. - does not contain any duplicate values
  367. - has CSNs for the new values we added
  368. */
  369. {
  370. Slapi_Attr *sa = NULL;
  371. Slapi_Value sv;
  372. const struct berval *svbv = NULL;
  373. /* add the extensibleobject objectclass with csn if not present */
  374. slapi_entry_attr_find(addingentry->ep_entry, SLAPI_ATTR_OBJECTCLASS, &sa);
  375. slapi_value_init_string(&sv, "extensibleobject");
  376. svbv = slapi_value_get_berval(&sv);
  377. if (slapi_attr_value_find(sa, svbv)) { /* not found, so add it */
  378. if (opcsn) {
  379. value_update_csn(&sv, CSN_TYPE_VALUE_UPDATED, opcsn);
  380. }
  381. slapi_attr_add_value(sa, &sv);
  382. }
  383. value_done(&sv);
  384. /* add the glue objectclass with csn if not present */
  385. slapi_value_init_string(&sv, "glue");
  386. svbv = slapi_value_get_berval(&sv);
  387. if (slapi_attr_value_find(sa, svbv)) { /* not found, so add it */
  388. if (opcsn) {
  389. value_update_csn(&sv, CSN_TYPE_VALUE_UPDATED, opcsn);
  390. }
  391. slapi_attr_add_value(sa, &sv);
  392. }
  393. value_done(&sv);
  394. }
  395. }
  396. else
  397. {
  398. /*
  399. * Try to add the entry to the cache, assign it a new entryid
  400. * and mark it locked. This should only fail if the entry
  401. * already exists.
  402. */
  403. /*
  404. * next_id will add this id to the list of ids that are pending
  405. * id2entry indexing.
  406. */
  407. addingentry = backentry_init( e );
  408. if ( ( addingentry->ep_id = next_id( be ) ) >= MAXID ) {
  409. LDAPDebug( LDAP_DEBUG_ANY,
  410. "add: maximum ID reached, cannot add entry to "
  411. "backend '%s'", be->be_name, 0, 0 );
  412. ldap_result_code = LDAP_OPERATIONS_ERROR;
  413. goto error_return;
  414. }
  415. addingentry_id_assigned= 1;
  416. if (!is_fixup_operation)
  417. {
  418. if ( opcsn == NULL && operation->o_csngen_handler )
  419. {
  420. /*
  421. * Current op is a user request. Opcsn will be assigned
  422. * if the dn is in an updatable replica.
  423. */
  424. opcsn = entry_assign_operation_csn ( pb, e, parententry ? parententry->ep_entry : NULL );
  425. }
  426. if ( opcsn != NULL )
  427. {
  428. entry_set_csn (e, opcsn);
  429. entry_add_dncsn (e, opcsn);
  430. entry_add_rdn_csn (e, opcsn);
  431. entry_set_maxcsn (e, opcsn);
  432. }
  433. }
  434. if (is_tombstone_operation)
  435. {
  436. /* Directly add the entry as a tombstone */
  437. /*
  438. * 1) If the entry has an existing DN, change it to be
  439. * "nsuniqueid=<uniqueid>, <old dn>"
  440. * 2) Add the objectclass value "tombstone" and arrange for only
  441. * that value to be indexed.
  442. * 3) If the parent entry was found, set the nsparentuniqueid
  443. * attribute to be the unique id of that parent.
  444. */
  445. char *untombstoned_dn = slapi_entry_get_dn(e);
  446. char *tombstoned_dn = NULL;
  447. if (NULL == untombstoned_dn)
  448. {
  449. untombstoned_dn = "";
  450. }
  451. tombstoned_dn = compute_entry_tombstone_dn(untombstoned_dn, addr.uniqueid);
  452. /*
  453. * This needs to be done before slapi_entry_set_dn call,
  454. * because untombstoned_dn is released in slapi_entry_set_dn.
  455. */
  456. if (entryrdn_get_switch())
  457. {
  458. Slapi_RDN srdn = {0};
  459. rc = slapi_rdn_init_all_dn(&srdn, tombstoned_dn);
  460. if (rc) {
  461. LDAPDebug1Arg( LDAP_DEBUG_TRACE,
  462. "ldbm_back_add (tombstone_operation): failed to "
  463. "decompose %s to Slapi_RDN\n", tombstoned_dn);
  464. } else {
  465. slapi_entry_set_srdn(e, &srdn);
  466. slapi_rdn_done(&srdn);
  467. }
  468. }
  469. slapi_entry_set_dn(addingentry->ep_entry, tombstoned_dn);
  470. /* Work around pb with slapi_entry_add_string (defect 522327)
  471. * doesn't check duplicate values */
  472. if (!slapi_entry_attr_hasvalue(addingentry->ep_entry,
  473. SLAPI_ATTR_OBJECTCLASS, SLAPI_ATTR_VALUE_TOMBSTONE)) {
  474. slapi_entry_add_string(addingentry->ep_entry,
  475. SLAPI_ATTR_OBJECTCLASS, SLAPI_ATTR_VALUE_TOMBSTONE);
  476. slapi_entry_set_flag(addingentry->ep_entry,
  477. SLAPI_ENTRY_FLAG_TOMBSTONE);
  478. }
  479. if (NULL != operation->o_params.p.p_add.parentuniqueid)
  480. {
  481. slapi_entry_add_string(addingentry->ep_entry,
  482. SLAPI_ATTR_VALUE_PARENT_UNIQUEID,
  483. operation->o_params.p.p_add.parentuniqueid);
  484. }
  485. }
  486. }
  487. /*
  488. * Get the parent dn and see if the corresponding entry exists.
  489. * If the parent does not exist, only allow the "root" user to
  490. * add the entry.
  491. */
  492. if ( !slapi_sdn_isempty(&parentsdn) )
  493. {
  494. /* This is getting the parent */
  495. if (NULL == parententry)
  496. {
  497. /* Here means that we didn't find the parent */
  498. int err = 0;
  499. Slapi_DN ancestordn= {0};
  500. struct backentry *ancestorentry;
  501. LDAPDebug( LDAP_DEBUG_TRACE,
  502. "parent does not exist, pdn = %s\n",
  503. slapi_sdn_get_dn(&parentsdn), 0, 0 );
  504. ancestorentry = dn2ancestor(be, &parentsdn, &ancestordn, &txn, &err );
  505. CACHE_RETURN( &inst->inst_cache, &ancestorentry );
  506. ldap_result_code= LDAP_NO_SUCH_OBJECT;
  507. ldap_result_matcheddn= slapi_ch_strdup((char *)slapi_sdn_get_dn(&ancestordn)); /* jcm - cast away const. */
  508. slapi_sdn_done(&ancestordn);
  509. goto error_return;
  510. }
  511. ldap_result_code = plugin_call_acl_plugin (pb, e, NULL, NULL, SLAPI_ACL_ADD,
  512. ACLPLUGIN_ACCESS_DEFAULT, &errbuf );
  513. if ( ldap_result_code != LDAP_SUCCESS )
  514. {
  515. LDAPDebug( LDAP_DEBUG_TRACE, "no access to parent\n", 0, 0, 0 );
  516. ldap_result_message= errbuf;
  517. goto error_return;
  518. }
  519. pid = parententry->ep_id;
  520. }
  521. else
  522. { /* no parent */
  523. if ( !isroot && !is_replicated_operation)
  524. {
  525. LDAPDebug( LDAP_DEBUG_TRACE, "no parent & not root\n",
  526. 0, 0, 0 );
  527. ldap_result_code= LDAP_INSUFFICIENT_ACCESS;
  528. goto error_return;
  529. }
  530. parententry = NULL;
  531. pid = 0;
  532. }
  533. if(is_resurect_operation)
  534. {
  535. if (!entryrdn_get_switch()) { /* subtree-rename: off */
  536. /* add the entrydn operational attributes to the addingentry */
  537. add_update_entrydn_operational_attributes(addingentry);
  538. }
  539. }
  540. else if (is_tombstone_operation)
  541. {
  542. if (!entryrdn_get_switch()) { /* subtree-rename: off */
  543. /* Remove the entrydn operational attributes from the addingentry */
  544. delete_update_entrydn_operational_attributes(addingentry);
  545. }
  546. }
  547. else
  548. {
  549. /*
  550. * add the parentid, entryid and entrydn operational attributes
  551. */
  552. add_update_entry_operational_attributes(addingentry, pid);
  553. }
  554. /* Tentatively add the entry to the cache. We do this after adding any
  555. * operational attributes to ensure that the cache is sized correctly. */
  556. if ( cache_add_tentative( &inst->inst_cache, addingentry, NULL )!= 0 )
  557. {
  558. LDAPDebug( LDAP_DEBUG_CACHE, "cache_add_tentative concurrency detected\n", 0, 0, 0 );
  559. ldap_result_code= LDAP_ALREADY_EXISTS;
  560. goto error_return;
  561. }
  562. addingentry_in_cache= 1;
  563. /*
  564. * Before we add the entry, find out if the syntax of the aci
  565. * aci attribute values are correct or not. We don't want to
  566. * the entry if the syntax is incorrect.
  567. */
  568. if ( plugin_call_acl_verify_syntax (pb, addingentry->ep_entry, &errbuf) != 0 ) {
  569. LDAPDebug( LDAP_DEBUG_TRACE, "ACL syntax error\n", 0,0,0);
  570. ldap_result_code= LDAP_INVALID_SYNTAX;
  571. ldap_result_message= errbuf;
  572. goto error_return;
  573. }
  574. /* Having decided that we're really going to do the operation, let's modify
  575. the in-memory state of the parent to reflect the new child (update
  576. subordinate count specifically */
  577. if (NULL != parententry)
  578. {
  579. retval = parent_update_on_childchange(&parent_modify_c,1,NULL); /* 1==add */\
  580. /* The modify context now contains info needed later */
  581. if (0 != retval) {
  582. ldap_result_code= LDAP_OPERATIONS_ERROR;
  583. goto error_return;
  584. }
  585. parent_found = 1;
  586. parententry = NULL;
  587. }
  588. /*
  589. * So, we believe that no code up till here actually added anything
  590. * to persistent store. From now on, we're transacted
  591. */
  592. for (retry_count = 0; retry_count < RETRY_TIMES; retry_count++) {
  593. if (retry_count > 0) {
  594. dblayer_txn_abort(li,&txn);
  595. /* We're re-trying */
  596. LDAPDebug( LDAP_DEBUG_TRACE, "Add Retrying Transaction\n", 0, 0, 0 );
  597. #ifndef LDBM_NO_BACKOFF_DELAY
  598. {
  599. PRIntervalTime interval;
  600. interval = PR_MillisecondsToInterval(slapi_rand() % 100);
  601. DS_Sleep(interval);
  602. }
  603. #endif
  604. }
  605. retval = dblayer_txn_begin(li,parent_txn,&txn);
  606. if (0 != retval) {
  607. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  608. disk_full = 1;
  609. ldap_result_code= LDAP_OPERATIONS_ERROR;
  610. goto diskfull_return;
  611. }
  612. ldap_result_code= LDAP_OPERATIONS_ERROR;
  613. goto error_return;
  614. }
  615. retval = id2entry_add( be, addingentry, &txn );
  616. if (DB_LOCK_DEADLOCK == retval)
  617. {
  618. LDAPDebug( LDAP_DEBUG_ARGS, "add 1 DEADLOCK\n", 0, 0, 0 );
  619. /* Retry txn */
  620. continue;
  621. }
  622. if (retval != 0) {
  623. LDAPDebug( LDAP_DEBUG_TRACE, "id2entry_add failed, err=%d %s\n",
  624. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  625. ADD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  626. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  627. disk_full = 1;
  628. goto diskfull_return;
  629. }
  630. goto error_return;
  631. }
  632. if(is_resurect_operation)
  633. {
  634. retval = index_addordel_string(be,SLAPI_ATTR_OBJECTCLASS,SLAPI_ATTR_VALUE_TOMBSTONE,addingentry->ep_id,BE_INDEX_DEL,&txn);
  635. if (DB_LOCK_DEADLOCK == retval) {
  636. LDAPDebug( LDAP_DEBUG_ARGS, "add 2 DB_LOCK_DEADLOCK\n", 0, 0, 0 );
  637. /* Retry txn */
  638. continue;
  639. }
  640. if (0 != retval) {
  641. LDAPDebug( LDAP_DEBUG_TRACE, "add 1 BAD, err=%d %s\n",
  642. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  643. ADD_SET_ERROR(ldap_result_code,
  644. LDAP_OPERATIONS_ERROR, retry_count);
  645. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  646. disk_full = 1;
  647. goto diskfull_return;
  648. }
  649. goto error_return;
  650. }
  651. retval = index_addordel_string(be,SLAPI_ATTR_UNIQUEID,slapi_entry_get_uniqueid(addingentry->ep_entry),addingentry->ep_id,BE_INDEX_DEL,&txn);
  652. if (DB_LOCK_DEADLOCK == retval) {
  653. LDAPDebug( LDAP_DEBUG_ARGS, "add 3 DB_LOCK_DEADLOCK\n", 0, 0, 0 );
  654. /* Retry txn */
  655. continue;
  656. }
  657. if (0 != retval) {
  658. LDAPDebug( LDAP_DEBUG_TRACE, "add 2 BAD, err=%d %s\n",
  659. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  660. ADD_SET_ERROR(ldap_result_code,
  661. LDAP_OPERATIONS_ERROR, retry_count);
  662. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  663. disk_full = 1;
  664. goto diskfull_return;
  665. }
  666. goto error_return;
  667. }
  668. retval = index_addordel_string(be,SLAPI_ATTR_NSCP_ENTRYDN,slapi_sdn_get_ndn(&sdn),addingentry->ep_id,BE_INDEX_DEL,&txn);
  669. if (DB_LOCK_DEADLOCK == retval) {
  670. LDAPDebug( LDAP_DEBUG_ARGS, "add 4 DB_LOCK_DEADLOCK\n", 0, 0, 0 );
  671. /* Retry txn */
  672. continue;
  673. }
  674. if (0 != retval) {
  675. LDAPDebug( LDAP_DEBUG_TRACE, "add 3 BAD, err=%d %s\n",
  676. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  677. ADD_SET_ERROR(ldap_result_code,
  678. LDAP_OPERATIONS_ERROR, retry_count);
  679. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  680. disk_full = 1;
  681. goto diskfull_return;
  682. }
  683. goto error_return;
  684. }
  685. }
  686. if (is_tombstone_operation)
  687. {
  688. retval = index_addordel_entry( be, addingentry, BE_INDEX_ADD | BE_INDEX_TOMBSTONE, &txn );
  689. }
  690. else
  691. {
  692. retval = index_addordel_entry( be, addingentry, BE_INDEX_ADD, &txn );
  693. }
  694. if (DB_LOCK_DEADLOCK == retval)
  695. {
  696. LDAPDebug( LDAP_DEBUG_ARGS, "add 5 DEADLOCK\n", 0, 0, 0 );
  697. /* retry txn */
  698. continue;
  699. }
  700. if (retval != 0) {
  701. LDAPDebug( LDAP_DEBUG_ANY, "add: attempt to index %lu failed\n",
  702. (u_long)addingentry->ep_id, 0, 0 );
  703. ADD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  704. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  705. disk_full = 1;
  706. goto diskfull_return;
  707. }
  708. goto error_return;
  709. }
  710. if (parent_found) {
  711. /* Push out the db modifications from the parent entry */
  712. retval = modify_update_all(be,pb,&parent_modify_c,&txn);
  713. if (DB_LOCK_DEADLOCK == retval)
  714. {
  715. LDAPDebug( LDAP_DEBUG_ARGS, "add 6 DEADLOCK\n", 0, 0, 0 );
  716. /* Retry txn */
  717. continue;
  718. }
  719. if (0 != retval) {
  720. LDAPDebug( LDAP_DEBUG_TRACE, "add 1 BAD, err=%d %s\n",
  721. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  722. ADD_SET_ERROR(ldap_result_code,
  723. LDAP_OPERATIONS_ERROR, retry_count);
  724. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  725. disk_full = 1;
  726. goto diskfull_return;
  727. }
  728. goto error_return;
  729. }
  730. }
  731. /*
  732. * Update the Virtual List View indexes
  733. */
  734. if (!is_ruv)
  735. {
  736. retval= vlv_update_all_indexes(&txn, be, pb, NULL, addingentry);
  737. if (DB_LOCK_DEADLOCK == retval) {
  738. LDAPDebug( LDAP_DEBUG_ARGS,
  739. "add DEADLOCK vlv_update_index\n", 0, 0, 0 );
  740. /* Retry txn */
  741. continue;
  742. }
  743. if (0 != retval) {
  744. LDAPDebug( LDAP_DEBUG_TRACE,
  745. "vlv_update_index failed, err=%d %s\n",
  746. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  747. ADD_SET_ERROR(ldap_result_code,
  748. LDAP_OPERATIONS_ERROR, retry_count);
  749. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  750. disk_full = 1;
  751. goto diskfull_return;
  752. }
  753. goto error_return;
  754. }
  755. }
  756. if (retval == 0) {
  757. break;
  758. }
  759. }
  760. if (retry_count == RETRY_TIMES) {
  761. /* Failed */
  762. LDAPDebug( LDAP_DEBUG_ANY, "Retry count exceeded in add\n", 0, 0, 0 );
  763. ldap_result_code= LDAP_OPERATIONS_ERROR;
  764. goto error_return;
  765. }
  766. /*
  767. * At this point, everything's cool, and the only thing which
  768. * can go wrong is a transaction commit failure.
  769. */
  770. slapi_pblock_set( pb, SLAPI_ENTRY_PRE_OP, NULL );
  771. slapi_pblock_set( pb, SLAPI_ENTRY_POST_OP, slapi_entry_dup( addingentry->ep_entry ));
  772. if(is_resurect_operation)
  773. {
  774. /*
  775. * We can now switch the tombstone entry with the real entry.
  776. */
  777. if (cache_replace( &inst->inst_cache, tombstoneentry, addingentry ) != 0 )
  778. {
  779. /* This happens if the dn of addingentry already exists */
  780. cache_unlock_entry( &inst->inst_cache, tombstoneentry );
  781. ADD_SET_ERROR(ldap_result_code, LDAP_ALREADY_EXISTS, retry_count);
  782. goto error_return;
  783. }
  784. /*
  785. * The tombstone was locked down in the cache... we can
  786. * get rid of the entry in the cache now.
  787. */
  788. cache_unlock_entry( &inst->inst_cache, tombstoneentry );
  789. CACHE_RETURN( &inst->inst_cache, &tombstoneentry );
  790. tombstone_in_cache = 0; /* deleted */
  791. }
  792. if (parent_found)
  793. {
  794. /* switch the parent entry copy into play */
  795. modify_switch_entries( &parent_modify_c,be);
  796. }
  797. retval = dblayer_txn_commit(li,&txn);
  798. if (0 != retval)
  799. {
  800. ADD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  801. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  802. disk_full = 1;
  803. goto diskfull_return;
  804. }
  805. goto error_return;
  806. }
  807. rc= 0;
  808. goto common_return;
  809. error_return:
  810. if ( addingentry_id_assigned )
  811. {
  812. next_id_return( be, addingentry->ep_id );
  813. }
  814. if ( NULL != addingentry )
  815. {
  816. if ( addingentry_in_cache )
  817. {
  818. CACHE_REMOVE(&inst->inst_cache, addingentry);
  819. addingentry_in_cache = 0;
  820. }
  821. backentry_clear_entry(addingentry); /* e is released in the frontend */
  822. backentry_free( &addingentry ); /* release the backend wrapper, here */
  823. }
  824. if(tombstone_in_cache)
  825. {
  826. CACHE_RETURN(&inst->inst_cache, &tombstoneentry);
  827. }
  828. if (rc == DB_RUNRECOVERY) {
  829. dblayer_remember_disk_filled(li);
  830. ldbm_nasty("Add",80,rc);
  831. disk_full = 1;
  832. }
  833. diskfull_return:
  834. if (disk_full) {
  835. rc= return_on_disk_full(li);
  836. } else {
  837. /* It is safer not to abort when the transaction is not started. */
  838. if (retry_count > 0) {
  839. dblayer_txn_abort(li,&txn); /* abort crashes in case disk full */
  840. }
  841. rc= SLAPI_FAIL_GENERAL;
  842. }
  843. common_return:
  844. if (addingentry_in_cache && addingentry)
  845. {
  846. if (entryrdn_get_switch()) { /* subtree-rename: on */
  847. /* since adding the entry to the entry cache was successful,
  848. * let's add the dn to dncache, if not yet done. */
  849. struct backdn *bdn = dncache_find_id(&inst->inst_dncache,
  850. addingentry->ep_id);
  851. if (bdn) { /* already in the dncache */
  852. CACHE_RETURN(&inst->inst_dncache, &bdn);
  853. } else { /* not in the dncache yet */
  854. Slapi_DN *addingsdn =
  855. slapi_sdn_dup(slapi_entry_get_sdn(addingentry->ep_entry));
  856. if (addingsdn) {
  857. bdn = backdn_init(addingsdn, addingentry->ep_id, 0);
  858. if (bdn) {
  859. CACHE_ADD( &inst->inst_dncache, bdn, NULL );
  860. CACHE_RETURN(&inst->inst_dncache, &bdn);
  861. slapi_log_error(SLAPI_LOG_CACHE, "ldbm_back_add",
  862. "set %s to dn cache\n", dn);
  863. }
  864. }
  865. }
  866. }
  867. CACHE_RETURN( &inst->inst_cache, &addingentry );
  868. }
  869. /* bepost op needs to know this result */
  870. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  871. /* JCMREPL - The bepostop is called even if the operation fails. */
  872. plugin_call_plugins (pb, SLAPI_PLUGIN_BE_POST_ADD_FN);
  873. modify_term(&parent_modify_c,be);
  874. done_with_pblock_entry(pb,SLAPI_ADD_EXISTING_DN_ENTRY);
  875. done_with_pblock_entry(pb,SLAPI_ADD_EXISTING_UNIQUEID_ENTRY);
  876. done_with_pblock_entry(pb,SLAPI_ADD_PARENT_ENTRY);
  877. if(dblock_acquired)
  878. {
  879. dblayer_unlock_backend(be);
  880. }
  881. if(ldap_result_code!=-1)
  882. {
  883. slapi_send_ldap_result( pb, ldap_result_code, ldap_result_matcheddn, ldap_result_message, 0, NULL );
  884. }
  885. slapi_sdn_done(&sdn);
  886. slapi_sdn_done(&parentsdn);
  887. slapi_ch_free( (void**)&ldap_result_matcheddn );
  888. slapi_ch_free( (void**)&errbuf );
  889. return rc;
  890. }
  891. /*
  892. * add the parentid, entryid and entrydn, operational attributes.
  893. *
  894. * Note: This is called from the ldif2ldbm code.
  895. */
  896. void
  897. add_update_entry_operational_attributes(struct backentry *ep, ID pid)
  898. {
  899. struct berval bv;
  900. struct berval *bvp[2];
  901. char buf[40]; /* Enough for an EntryID */
  902. bvp[0] = &bv;
  903. bvp[1] = NULL;
  904. /* parentid */
  905. /* If the pid is 0, then the entry does not have a parent. It
  906. * may be the case that the entry is a suffix. In any case,
  907. * the parentid attribute should only be added if the entry
  908. * has a parent. */
  909. if (pid != 0) {
  910. sprintf( buf, "%lu", (u_long)pid );
  911. bv.bv_val = buf;
  912. bv.bv_len = strlen( buf );
  913. entry_replace_values( ep->ep_entry, LDBM_PARENTID_STR, bvp );
  914. }
  915. /* entryid */
  916. sprintf( buf, "%lu", (u_long)ep->ep_id );
  917. bv.bv_val = buf;
  918. bv.bv_len = strlen( buf );
  919. entry_replace_values( ep->ep_entry, "entryid", bvp );
  920. if (!entryrdn_get_switch()) { /* subtree-rename: off */
  921. /* add the entrydn operational attribute to the entry. */
  922. add_update_entrydn_operational_attributes(ep);
  923. }
  924. }
  925. /*
  926. * add the entrydn operational attribute to the entry.
  927. */
  928. void
  929. add_update_entrydn_operational_attributes(struct backentry *ep)
  930. {
  931. struct berval bv;
  932. struct berval *bvp[2];
  933. /* entrydn */
  934. bvp[0] = &bv;
  935. bvp[1] = NULL;
  936. bv.bv_val = (void*)backentry_get_ndn(ep);
  937. bv.bv_len = strlen( bv.bv_val );
  938. entry_replace_values_with_flags( ep->ep_entry, LDBM_ENTRYDN_STR, bvp,
  939. SLAPI_ATTR_FLAG_NORMALIZED );
  940. }
  941. /*
  942. * delete the entrydn operational attributes from the entry.
  943. */
  944. static void
  945. delete_update_entrydn_operational_attributes(struct backentry *ep)
  946. {
  947. /* entrydn */
  948. slapi_entry_attr_delete( ep->ep_entry, LDBM_ENTRYDN_STR);
  949. }