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