]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
unifdef LDAP_SYNC and LDAP_SYNCREPL
[openldap] / servers / slapd / back-ldbm / modrdn.c
1 /* modrdn.c - ldbm backend modrdn routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 /*
9  * LDAP v3 newSuperior support. Add new rdn as an attribute.
10  * (Full support for v2 also used software/ideas contributed
11  * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
12  * submission!.)
13  *
14  * Copyright 1999, Juan C. Gomez, All rights reserved.
15  * This software is not subject to any license of Silicon Graphics 
16  * Inc. or Purdue University.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * without restriction or fee of any kind as long as this notice
20  * is preserved.
21  *
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32 #include "back-ldbm.h"
33 #include "proto-back-ldbm.h"
34
35 int
36 ldbm_back_modrdn(
37     Operation   *op,
38     SlapReply   *rs )
39 {
40         AttributeDescription *children = slap_schema.si_ad_children;
41         AttributeDescription *entry = slap_schema.si_ad_entry;
42         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
43         struct berval   p_dn, p_ndn;
44         struct berval   new_dn = { 0, NULL}, new_ndn = { 0, NULL };
45         Entry           *e, *p = NULL;
46         Entry           *matched;
47         /* LDAP v2 supporting correct attribute handling. */
48         LDAPRDN         new_rdn = NULL;
49         LDAPRDN         old_rdn = NULL;
50         int             isroot = -1;
51 #define CAN_ROLLBACK    -1
52 #define MUST_DESTROY    1
53         int             rc = CAN_ROLLBACK;
54         int             rc_id = 0;
55         ID              id = NOID;
56         const char      *text = NULL;
57         char            textbuf[SLAP_TEXT_BUFLEN];
58         size_t          textlen = sizeof textbuf;
59         /* Added to support newSuperior */ 
60         Entry           *np = NULL;     /* newSuperior Entry */
61         struct berval   *np_ndn = NULL; /* newSuperior ndn */
62         struct berval   *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
63         /* Used to interface with ldbm_modify_internal() */
64         Modifications   *mod = NULL;            /* Used to delete old/add new rdn */
65         int             manageDSAit = get_manageDSAit( op );
66
67 #ifdef NEW_LOGGING
68         LDAP_LOG( BACK_LDBM, ENTRY, 
69                 "ldbm_back_modrdn: dn: %s newSuperior=%s\n", 
70                 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
71                 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len ) ? op->oq_modrdn.rs_newSup->bv_val : "NULL",0 );
72 #else
73         Debug( LDAP_DEBUG_TRACE,
74                 "==>ldbm_back_modrdn: dn: %s newSuperior=%s\n", 
75                 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
76                 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len )
77                         ? op->oq_modrdn.rs_newSup->bv_val : "NULL", 0 );
78 #endif
79
80         /* grab giant lock for writing */
81         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
82
83         e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
84
85         /* get entry with writer lock */
86         /* FIXME: dn2entry() should return non-glue entry */
87         if (( e == NULL  ) || ( !manageDSAit && e && is_entry_glue( e ))) {
88                 if ( matched != NULL ) {
89                         rs->sr_matched = strdup( matched->e_dn );
90                         rs->sr_ref = is_entry_referral( matched )
91                                 ? get_entry_referrals( op, matched )
92                                 : NULL;
93                         cache_return_entry_r( &li->li_cache, matched );
94                 } else {
95                         BerVarray deref = op->o_bd->syncinfo ?
96                                                           op->o_bd->syncinfo->provideruri_bv : default_referral;
97                         rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
98                 }
99
100                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
101
102                 rs->sr_err = LDAP_REFERRAL;
103                 send_ldap_result( op, rs );
104
105                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
106                 free( (char *)rs->sr_matched );
107
108                 return rs->sr_err;
109         }
110
111         /* check entry for "entry" acl */
112         if ( ! access_allowed( op, e,
113                 entry, NULL, ACL_WRITE, NULL ) )
114         {
115 #ifdef NEW_LOGGING
116                 LDAP_LOG( BACK_LDBM, ERR, 
117                         "ldbm_back_modrdn: no write access to entry of (%s)\n", 
118                         op->o_req_dn.bv_val, 0, 0 );
119 #else
120                 Debug( LDAP_DEBUG_TRACE,
121                         "<=- ldbm_back_modrdn: no write access to entry\n", 0,
122                         0, 0 );
123 #endif
124
125                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
126                         "no write access to entry" );
127
128                 goto return_results;
129         }
130
131         if (!manageDSAit && is_entry_referral( e ) ) {
132                 /* parent is a referral, don't allow add */
133                 /* parent is an alias, don't allow add */
134                 rs->sr_ref = get_entry_referrals( op, e );
135
136 #ifdef NEW_LOGGING
137                 LDAP_LOG( BACK_LDBM, INFO, 
138                         "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn, 0, 0 );
139 #else
140                 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
141                     0, 0 );
142 #endif
143
144                 rs->sr_err = LDAP_REFERRAL;
145                 rs->sr_matched = e->e_name.bv_val;
146                 send_ldap_result( op, rs );
147
148                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
149                 goto return_results;
150         }
151
152         if ( has_children( op->o_bd, e ) ) {
153 #ifdef NEW_LOGGING
154                 LDAP_LOG( BACK_LDBM, INFO, 
155                         "ldbm_back_modrdn: entry %s has children\n", e->e_dn, 0, 0 );
156 #else
157                 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
158                     0, 0 );
159 #endif
160
161                 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
162                     "subtree rename not supported" );
163                 goto return_results;
164         }
165
166         if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
167                 p_ndn = slap_empty_bv ;
168         } else {
169                 dnParent( &e->e_nname, &p_ndn );
170         }
171
172         if ( p_ndn.bv_len != 0 ) {
173                 /* Make sure parent entry exist and we can write its 
174                  * children.
175                  */
176
177                 if( (p = dn2entry_w( op->o_bd, &p_ndn, NULL )) == NULL) {
178 #ifdef NEW_LOGGING
179                         LDAP_LOG( BACK_LDBM, INFO, 
180                                 "ldbm_back_modrdn: parent of %s does not exist\n", 
181                                 e->e_ndn, 0, 0 );
182 #else
183                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
184                                 0, 0, 0);
185 #endif
186
187                         send_ldap_error( op, rs, LDAP_OTHER,
188                                 "parent entry does not exist" );
189
190                         goto return_results;
191                 }
192
193                 /* check parent for "children" acl */
194                 if ( ! access_allowed( op, p,
195                         children, NULL, ACL_WRITE, NULL ) )
196                 {
197 #ifdef NEW_LOGGING
198                         LDAP_LOG( BACK_LDBM, INFO, 
199                                 "ldbm_back_modrdn: no access to parent of (%s)\n", 
200                                 e->e_dn, 0, 0 );
201 #else
202                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
203                                 0, 0 );
204 #endif
205
206                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
207                                 NULL );
208                         goto return_results;
209                 }
210
211 #ifdef NEW_LOGGING
212                 LDAP_LOG( BACK_LDBM, DETAIL1, 
213                         "ldbm_back_modrdn: wr to children of entry %s OK\n", 
214                         p_ndn.bv_val, 0, 0 );
215 #else
216                 Debug( LDAP_DEBUG_TRACE,
217                        "ldbm_back_modrdn: wr to children of entry %s OK\n",
218                        p_ndn.bv_val, 0, 0 );
219 #endif
220
221                 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
222                         p_dn = slap_empty_bv;
223                 } else {
224                         dnParent( &e->e_name, &p_dn );
225                 }
226
227 #ifdef NEW_LOGGING
228                 LDAP_LOG( BACK_LDBM, DETAIL1, 
229                            "ldbm_back_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
230 #else
231                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
232                        p_dn.bv_val, 0, 0 );
233 #endif
234
235         } else {
236                 /* no parent, must be root to modify rdn */
237                 isroot = be_isroot( op->o_bd, &op->o_ndn );
238                 if ( ! isroot ) {
239                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
240                                 int     can_access;
241                                 p = (Entry *)&slap_entry_root;
242                                 
243                                 can_access = access_allowed( op, p,
244                                                 children, NULL, ACL_WRITE, NULL );
245                                 p = NULL;
246                                                                 
247                                 /* check parent for "children" acl */
248                                 if ( ! can_access ) {
249 #ifdef NEW_LOGGING
250                                         LDAP_LOG( BACK_LDBM, ERR,
251                                                 "ldbm_back_modrdn: no access to parent \"\"\n", 0,0,0 );
252 #else
253                                         Debug( LDAP_DEBUG_TRACE,
254                                                 "<=- ldbm_back_modrdn: no "
255                                                 "access to parent\n", 0, 0, 0 );
256 #endif
257
258                                         send_ldap_error( op, rs,
259                                                 LDAP_INSUFFICIENT_ACCESS,
260                                                 NULL );
261                                         goto return_results;
262                                 }
263
264                         } else {
265 #ifdef NEW_LOGGING
266                                 LDAP_LOG( BACK_LDBM, ERR, 
267                                         "ldbm_back_modrdn: (%s) has no parent & not a root.\n", 
268                                         op->o_ndn, 0, 0 );
269 #else
270                                 Debug( LDAP_DEBUG_TRACE,
271                                         "<=- ldbm_back_modrdn: no parent & "
272                                         "not root\n", 0, 0, 0);
273 #endif
274
275                                 send_ldap_error( op, rs,
276                                         LDAP_INSUFFICIENT_ACCESS,
277                                         NULL );
278                                 goto return_results;
279                         }
280                 }
281
282 #ifdef NEW_LOGGING
283                 LDAP_LOG( BACK_LDBM, INFO, 
284                    "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn, 0, 0 );
285 #else
286                 Debug( LDAP_DEBUG_TRACE,
287                        "ldbm_back_modrdn: no parent, locked root\n",
288                        0, 0, 0 );
289 #endif
290         }
291
292         new_parent_dn = &p_dn;  /* New Parent unless newSuperior given */
293
294         if ( op->oq_modrdn.rs_newSup != NULL ) {
295 #ifdef NEW_LOGGING
296                 LDAP_LOG( BACK_LDBM, DETAIL1, 
297                         "ldbm_back_modrdn: new parent \"%s\" requested\n",
298                         op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
299 #else
300                 Debug( LDAP_DEBUG_TRACE, 
301                         "ldbm_back_modrdn: new parent \"%s\" requested...\n",
302                         op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
303 #endif
304
305                 np_ndn = op->oq_modrdn.rs_nnewSup;
306
307                 /* newSuperior == oldParent? */
308                 if ( dn_match( &p_ndn, np_ndn ) ) {
309 #ifdef NEW_LOGGING
310                         LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: "
311                                 "new parent\"%s\" seems to be the same as the "
312                                 "old parent \"%s\"\n", op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
313 #else
314                         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: "
315                                 "new parent\"%s\" seems to be the same as the "
316                                 "old parent \"%s\"\n",
317                                 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
318 #endif
319
320                         op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
321                 }
322         }
323
324         if ( op->oq_modrdn.rs_newSup != NULL ) {
325                 /* newSuperior == entry being moved?, if so ==> ERROR */
326                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
327
328                 if ( op->oq_modrdn.rs_nnewSup->bv_len ) {
329                         if( (np = dn2entry_w( op->o_bd, np_ndn, NULL )) == NULL) {
330 #ifdef NEW_LOGGING
331                                 LDAP_LOG( BACK_LDBM, ERR, 
332                                         "ldbm_back_modrdn: newSup(ndn=%s) not found.\n", 
333                                         np_ndn->bv_val, 0, 0 );
334 #else
335                                 Debug( LDAP_DEBUG_TRACE,
336                                     "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
337                                     np_ndn->bv_val, 0, 0);
338 #endif
339
340                                 send_ldap_error( op, rs, LDAP_OTHER,
341                                         "newSuperior not found" );
342                                 goto return_results;
343                         }
344
345 #ifdef NEW_LOGGING
346                         LDAP_LOG( BACK_LDBM, DETAIL1,
347                                 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
348                                 (void *) np, np->e_id, 0 );
349 #else
350                         Debug( LDAP_DEBUG_TRACE,
351                                 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
352                                 (void *) np, np->e_id, 0 );
353 #endif
354
355                         /* check newSuperior for "children" acl */
356                         if ( !access_allowed( op, np, children, NULL,
357                                               ACL_WRITE, NULL ) )
358                         {
359 #ifdef NEW_LOGGING
360                                 LDAP_LOG( BACK_LDBM, INFO,
361                                    "ldbm_back_modrdn: no wr to newSup children.\n", 0, 0, 0 );
362 #else
363                                 Debug( LDAP_DEBUG_TRACE,
364                                        "ldbm_back_modrdn: no wr to newSup children\n",
365                                        0, 0, 0 );
366 #endif
367
368                                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
369                                 goto return_results;
370                         }
371
372                         if ( is_entry_alias( np ) ) {
373                                 /* parent is an alias, don't allow add */
374 #ifdef NEW_LOGGING
375                                 LDAP_LOG( BACK_LDBM, INFO,
376                                    "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn,0,0);
377 #else
378                                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
379 #endif
380
381
382                                 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
383                                     "newSuperior is an alias" );
384
385                                 goto return_results;
386                         }
387
388                         if ( is_entry_referral( np ) ) {
389                                 /* parent is a referral, don't allow add */
390 #ifdef NEW_LOGGING
391                                 LDAP_LOG( BACK_LDBM, INFO,
392                                         "ldbm_back_modrdn: entry (%s) is a referral\n",
393                                         np->e_dn, 0, 0 );
394 #else
395                                 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
396                                         np->e_dn, 0, 0 );
397 #endif
398
399                                 send_ldap_error( op, rs, LDAP_OTHER,
400                                     "newSuperior is a referral" );
401
402                                 goto return_results;
403                         }
404
405                 } else {
406
407                         /* no parent, must be root to modify newSuperior */
408                         if ( isroot == -1 ) {
409                                 isroot = be_isroot( op->o_bd, &op->o_ndn );
410                         }
411
412                         if ( ! isroot ) {
413                                 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
414                                         int     can_access;
415                                         np = (Entry *)&slap_entry_root;
416                                 
417                                         can_access = access_allowed( op, np,
418                                                         children, NULL, ACL_WRITE, NULL );
419                                         np = NULL;
420                                                                 
421                                         /* check parent for "children" acl */
422                                         if ( ! can_access ) {
423 #ifdef NEW_LOGGING
424                                                 LDAP_LOG( BACK_LDBM, ERR,
425                                                         "ldbm_back_modrdn: no access "
426                                                         "to new superior \"\"\n", 0, 0, 0 );
427 #else
428                                                 Debug( LDAP_DEBUG_TRACE,
429                                                         "<=- ldbm_back_modrdn: no "
430                                                         "access to new superior\n", 0, 0, 0 );
431 #endif
432
433                                                 send_ldap_error( op, rs,
434                                                         LDAP_INSUFFICIENT_ACCESS,
435                                                         NULL );
436                                                 goto return_results;
437                                         }
438
439                                 } else {
440 #ifdef NEW_LOGGING
441                                         LDAP_LOG( BACK_LDBM, ERR,
442                                                 "ldbm_back_modrdn: \"\" not allowed as new superior\n",
443                                                 0, 0, 0 );
444 #else
445                                         Debug( LDAP_DEBUG_TRACE,
446                                                 "<=- ldbm_back_modrdn: \"\" "
447                                                 "not allowed as new superior\n", 
448                                                 0, 0, 0);
449 #endif
450
451                                         send_ldap_error( op, rs,
452                                                 LDAP_INSUFFICIENT_ACCESS,
453                                                 NULL );
454                                         goto return_results;
455                                 }
456                         }
457                 }
458
459 #ifdef NEW_LOGGING
460                 LDAP_LOG( BACK_LDBM, DETAIL1,
461                         "ldbm_back_modrdn: wr to new parent's children OK.\n", 0, 0, 0 );
462 #else
463                 Debug( LDAP_DEBUG_TRACE,
464                     "ldbm_back_modrdn: wr to new parent's children OK\n",
465                     0, 0, 0 );
466 #endif
467
468                 new_parent_dn = op->oq_modrdn.rs_newSup;
469         }
470         
471         /* Build target dn and make sure target entry doesn't exist already. */
472         build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn ); 
473         dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
474
475 #ifdef NEW_LOGGING
476         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: new ndn=%s\n", 
477                 new_ndn.bv_val, 0, 0 );
478 #else
479         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
480             new_ndn.bv_val, 0, 0 );
481 #endif
482
483         /* check for abandon */
484         if ( op->o_abandon ) {
485                 goto return_results;
486         }
487
488         if ( ( rc_id = dn2id ( op->o_bd, &new_ndn, &id ) ) || id != NOID ) {
489                 /* if (rc_id) something bad happened to ldbm cache */
490                 rs->sr_err = rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
491                 send_ldap_result( op, rs );
492                 goto return_results;
493         }
494
495 #ifdef NEW_LOGGING
496         LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: new ndn (%s) does not exist\n",
497                 new_ndn.bv_val, 0, 0 );
498 #else
499         Debug( LDAP_DEBUG_TRACE,
500             "ldbm_back_modrdn: new ndn=%s does not exist\n",
501             new_ndn.bv_val, 0, 0 );
502 #endif
503
504         /* Get attribute type and attribute value of our new rdn, we will
505          * need to add that to our new entry
506          */
507         if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text,
508                 LDAP_DN_FORMAT_LDAP ) )
509         {
510 #ifdef NEW_LOGGING
511                 LDAP_LOG ( OPERATION, ERR, 
512                         "ldbm_back_modrdn: can't figure out "
513                         "type(s)/values(s) of newrdn\n", 
514                         0, 0, 0 );
515 #else
516                 Debug( LDAP_DEBUG_TRACE,
517                         "ldbm_back_modrdn: can't figure out "
518                         "type(s)/values(s) of newrdn\n", 
519                         0, 0, 0 );
520 #endif
521                 goto return_results;            
522         }
523
524 #ifdef NEW_LOGGING
525         LDAP_LOG ( OPERATION, RESULTS, 
526                 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
527                 "new_rdn_val=\"%s\"\n",
528                 new_rdn[ 0 ]->la_attr.bv_val, 
529                 new_rdn[ 0 ]->la_value.bv_val, 0 );
530 #else
531         Debug( LDAP_DEBUG_TRACE,
532                 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
533                 "new_rdn_val=\"%s\"\n",
534                 new_rdn[ 0 ]->la_attr.bv_val,
535                 new_rdn[ 0 ]->la_value.bv_val, 0 );
536 #endif
537
538         if ( op->oq_modrdn.rs_deleteoldrdn ) {
539                 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
540                         LDAP_DN_FORMAT_LDAP ) )
541                 {
542 #ifdef NEW_LOGGING
543                         LDAP_LOG ( OPERATION, ERR, 
544                                 "ldbm_back_modrdn: can't figure out "
545                                 "type(s)/values(s) of old_rdn\n", 
546                                 0, 0, 0 );
547 #else
548                         Debug( LDAP_DEBUG_TRACE,
549                                 "ldbm_back_modrdn: can't figure out "
550                                 "the old_rdn type(s)/value(s)\n", 
551                                 0, 0, 0 );
552 #endif
553                         goto return_results;            
554                 }
555         }
556
557 #ifdef NEW_LOGGING
558         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn:  DN_X500\n", 0, 0, 0 );
559 #else
560         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
561                0, 0, 0 );
562 #endif
563         
564         if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
565                 goto return_results;
566         }
567
568
569         /* check for abandon */
570         if ( op->o_abandon ) {
571                 goto return_results;
572         }
573
574         /* delete old one */
575         if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
576                 send_ldap_error( op, rs, LDAP_OTHER,
577                         "DN index delete fail" );
578                 goto return_results;
579         }
580
581         (void) cache_delete_entry( &li->li_cache, e );
582         rc = MUST_DESTROY;
583
584         /* XXX: there is no going back! */
585
586         free( e->e_dn );
587         free( e->e_ndn );
588         e->e_name = new_dn;
589         e->e_nname = new_ndn;
590         new_dn.bv_val = NULL;
591         new_ndn.bv_val = NULL;
592
593         /* NOTE: after this you must not free new_dn or new_ndn!
594          * They are used by cache.
595          */
596
597         /* add new one */
598         if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
599                 send_ldap_error( op, rs, LDAP_OTHER,
600                         "DN index add failed" );
601                 goto return_results;
602         }
603
604         /* modify memory copy of entry */
605         rc_id = ldbm_modify_internal( op, &mod[0], e,
606                 &rs->sr_text, textbuf, textlen );
607         switch ( rc_id ) {
608         case LDAP_SUCCESS:
609                 break;
610
611         case SLAPD_ABANDON:
612                 /* too late ... */
613                 rs->sr_err = rc_id;
614                 send_ldap_result( op, rs );
615                 goto return_results;
616         
617         default:
618                 /* here we may try to delete the newly added dn */
619                 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
620                         /* we already are in trouble ... */
621                         ;
622                 }
623                 goto return_results;
624         }
625         
626         (void) cache_update_entry( &li->li_cache, e );
627
628         /* id2entry index */
629         if ( id2entry_add( op->o_bd, e ) != 0 ) {
630                 send_ldap_error( op, rs, LDAP_OTHER,
631                         "entry update failed" );
632                 goto return_results;
633         }
634
635         rs->sr_err = LDAP_SUCCESS;
636         send_ldap_result( op, rs );
637         rc = 0;
638         cache_entry_commit( e );
639
640 return_results:
641         if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
642         if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
643
644         /* LDAP v2 supporting correct attribute handling. */
645         if ( new_rdn != NULL ) {
646                 ldap_rdnfree( new_rdn );
647         }
648         if ( old_rdn != NULL ) {
649                 ldap_rdnfree( old_rdn );
650         }
651         if ( mod != NULL ) {
652                 Modifications *tmp;
653                 for (; mod; mod = tmp ) {
654                         if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
655                         tmp = mod->sml_next;
656                         free( mod );
657                 }
658         }
659
660         /* LDAP v3 Support */
661         if( np != NULL ) {
662                 /* free new parent and writer lock */
663                 cache_return_entry_w( &li->li_cache, np );
664         }
665
666         if( p != NULL ) {
667                 /* free parent and writer lock */
668                 cache_return_entry_w( &li->li_cache, p );
669         }
670
671         /* free entry and writer lock */
672         cache_return_entry_w( &li->li_cache, e );
673         if ( rc == MUST_DESTROY ) {
674                 /* if rc == MUST_DESTROY the entry is uncached 
675                  * and its private data is destroyed; 
676                  * the entry must be freed */
677                 entry_free( e );
678         }
679         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
680         return( rc );
681 }