]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
Improve error message when parent is referral or not present
[openldap] / servers / slapd / back-ldbm / modrdn.c
1 /* modrdn.c - ldbm backend modrdn routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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     Backend     *be,
38     Connection  *conn,
39     Operation   *op,
40     const char  *dn,
41     const char  *ndn,
42     const char  *newrdn,
43     int         deleteoldrdn,
44     const char  *newSuperior
45 )
46 {
47         AttributeDescription *children = slap_schema.si_ad_children;
48         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
49         char            *p_dn = NULL, *p_ndn = NULL;
50         char            *new_dn = NULL, *new_ndn = NULL;
51         Entry           *e, *p = NULL;
52         Entry           *matched;
53         int                     rootlock = 0;
54         int                     rc = -1;
55         const char *text = NULL;
56         /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
57         char            *new_rdn_val = NULL;    /* Val of new rdn */
58         char            *new_rdn_type = NULL;   /* Type of new rdn */
59         char            *old_rdn = NULL;        /* Old rdn's attr type & val */
60         char            *old_rdn_type = NULL;   /* Type of old rdn attr. */
61         char            *old_rdn_val = NULL;    /* Old rdn attribute value */
62         /* Added to support newSuperior */ 
63         Entry           *np = NULL;     /* newSuperior Entry */
64         char            *np_dn = NULL;  /* newSuperior dn */
65         char            *np_ndn = NULL; /* newSuperior ndn */
66         char            *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
67         /* Used to interface with ldbm_modify_internal() */
68         struct berval   add_bv;                 /* Stores new rdn att */
69         struct berval   *add_bvals[2];          /* Stores new rdn att */
70         struct berval   del_bv;                 /* Stores old rdn att */
71         struct berval   *del_bvals[2];          /* Stores old rdn att */
72         Modifications   mod[2];                 /* Used to delete old rdn */
73         int             manageDSAit = get_manageDSAit( op );
74
75 #ifdef NEW_LOGGING
76         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
77                    "ldbm_back_modrdn: dn: %s newSuperior=%s\n", 
78                    dn ? dn : "NULL", newSuperior ? newSuperior : "NULL" ));
79 #else
80         Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
81                (newSuperior ? newSuperior : "NULL"),
82                0, 0 );
83 #endif
84
85
86         /* get entry with writer lock */
87         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
88                 char* matched_dn = NULL;
89                 struct berval** refs = NULL;
90
91                 if( matched != NULL ) {
92                         matched_dn = strdup( matched->e_dn );
93                         refs = is_entry_referral( matched )
94                                 ? get_entry_referrals( be, conn, op, matched )
95                                 : NULL;
96                         cache_return_entry_r( &li->li_cache, matched );
97                 } else {
98                         refs = default_referral;
99                 }
100
101                 send_ldap_result( conn, op, LDAP_REFERRAL,
102                         matched_dn, NULL, refs, NULL );
103
104                 if ( matched != NULL ) {
105                         ber_bvecfree( refs );
106                         free( matched_dn );
107                 }
108
109                 return( -1 );
110         }
111
112         if (!manageDSAit && is_entry_referral( e ) ) {
113                 /* parent is a referral, don't allow add */
114                 /* parent is an alias, don't allow add */
115                 struct berval **refs = get_entry_referrals( be,
116                         conn, op, e );
117
118 #ifdef NEW_LOGGING
119                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
120                            "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn ));
121 #else
122                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
123                     0, 0 );
124 #endif
125
126
127                 send_ldap_result( conn, op, LDAP_REFERRAL,
128                     e->e_dn, NULL, refs, NULL );
129
130                 ber_bvecfree( refs );
131
132                 goto return_results;
133         }
134
135         if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
136
137                 /* Make sure parent entry exist and we can write its 
138                  * children.
139                  */
140
141                 if( (p = dn2entry_w( be, p_ndn, NULL )) == NULL) {
142 #ifdef NEW_LOGGING
143                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
144                                    "ldbm_back_modrdn: parent of %s does not exist\n", e->e_ndn ));
145 #else
146                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
147                                 0, 0, 0);
148 #endif
149
150                         send_ldap_result( conn, op, LDAP_OTHER,
151                                 NULL, NULL, NULL, NULL );
152                         goto return_results;
153                 }
154
155                 /* check parent for "children" acl */
156                 if ( ! access_allowed( be, conn, op, p,
157                         children, NULL, ACL_WRITE ) )
158                 {
159 #ifdef NEW_LOGGING
160                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
161                                    "ldbm_back_modrdn: no access to parent of (%s)\n", e->e_dn ));
162 #else
163                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
164                                 0, 0 );
165 #endif
166
167                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
168                                 NULL, NULL, NULL, NULL );
169                         goto return_results;
170                 }
171
172 #ifdef NEW_LOGGING
173                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
174                            "ldbm_back_modrdn: wr to children of entry %s OK\n",
175                            p_ndn ));
176 #else
177                 Debug( LDAP_DEBUG_TRACE,
178                        "ldbm_back_modrdn: wr to children of entry %s OK\n",
179                        p_ndn, 0, 0 );
180 #endif
181
182                 
183                 p_dn = dn_parent( be, e->e_dn );
184         
185
186 #ifdef NEW_LOGGING
187                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
188                            "ldbm_back_modrdn: parent dn=%s\n", p_dn ));
189 #else
190                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
191                        p_dn, 0, 0 );
192 #endif
193
194
195         } else {
196                 /* no parent, modrdn entry directly under root */
197                 if( ! be_isroot( be, op->o_ndn ) ) {
198 #ifdef NEW_LOGGING
199                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
200                                    "ldbm_back_modrdn: (%s) no parent & not a root.\n",
201                                    e->e_dn ));
202 #else
203                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
204                                 0, 0, 0);
205 #endif
206
207                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
208                                 NULL, NULL, NULL, NULL );
209                         goto return_results;
210                 }
211
212                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
213                 rootlock = 1;
214                 
215 #ifdef NEW_LOGGING
216                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
217                            "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn ));
218 #else
219                 Debug( LDAP_DEBUG_TRACE,
220                        "ldbm_back_modrdn: no parent, locked root\n",
221                        0, 0, 0 );
222 #endif
223
224
225         }
226
227         new_parent_dn = p_dn;   /* New Parent unless newSuperior given */
228
229         if ( newSuperior != NULL ) {
230 #ifdef NEW_LOGGING
231                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
232                            "ldbm_back_modrdn: new parent \"%s\" requested\n", newSuperior ));
233 #else
234                 Debug( LDAP_DEBUG_TRACE, 
235                         "ldbm_back_modrdn: new parent \"%s\" requested...\n",
236                         newSuperior, 0, 0 );
237 #endif
238
239
240                 np_dn = ch_strdup( newSuperior );
241                 np_ndn = ch_strdup( np_dn );
242                 (void) dn_normalize( np_ndn );
243
244                 /* newSuperior == oldParent? */
245                 if ( strcmp( p_ndn, np_ndn ) == 0 ) {
246 #ifdef NEW_LOGGING
247                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
248                                    "ldbm_back_modrdn: new parent\"%s\" seems to be the same as the old parent \"%s\"\n",
249                                    newSuperior, p_dn ));
250 #else
251                         Debug( LDAP_DEBUG_TRACE, 
252                                "ldbm_back_modrdn: new parent \"%s\" seems to be the same as old parent \"%s\"...\n",
253                                newSuperior, p_dn, 0 );
254 #endif
255
256                         newSuperior = NULL; /* ignore newSuperior */
257                 }
258         }
259
260         if ( newSuperior != NULL ) {
261
262                 /* newSuperior == entry being moved?, if so ==> ERROR */
263                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
264
265                 if( (np = dn2entry_w( be, np_ndn, NULL )) == NULL) {
266 #ifdef NEW_LOGGING
267                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
268                                    "ldbm_back_modrdn: newSup(ndn=%s) not found.\n", np_ndn ));
269 #else
270                         Debug( LDAP_DEBUG_TRACE,
271                                "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
272                                np_ndn, 0, 0);
273 #endif
274
275                         send_ldap_result( conn, op, LDAP_OTHER,
276                                 NULL, NULL, NULL, NULL );
277                         goto return_results;
278                 }
279
280 #ifdef NEW_LOGGING
281                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
282                            "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
283                            np, np->e_id ));
284 #else
285                 Debug( LDAP_DEBUG_TRACE,
286                        "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
287                        np, np->e_id, 0 );
288 #endif
289
290             
291                 /* check newSuperior for "children" acl */
292                 if ( !access_allowed( be, conn, op, np, children, NULL,
293                                       ACL_WRITE ) )
294                 {
295 #ifdef NEW_LOGGING
296                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
297                                    "ldbm_back_modrdn: no wr to newSup children.\n" ));
298 #else
299                         Debug( LDAP_DEBUG_TRACE,
300                                "ldbm_back_modrdn: no wr to newSup children\n",
301                                0, 0, 0 );
302 #endif
303
304                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
305                                 NULL, NULL, NULL, NULL );
306                         goto return_results;
307                 }
308
309                 if ( is_entry_alias( np ) ) {
310                         /* entry is an alias, don't allow bind */
311 #ifdef NEW_LOGGING
312                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
313                                    "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn ));
314 #else
315                         Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
316                             0, 0 );
317 #endif
318
319
320                         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
321                             NULL, NULL, NULL, NULL );
322
323                         goto return_results;
324                 }
325
326                 if ( is_entry_referral( np ) ) {
327                         /* parent is a referral, don't allow add */
328                         /* parent is an alias, don't allow add */
329 #ifdef NEW_LOGGING
330                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
331                                    "ldbm_back_modrdn: entry (%s) is a referral\n",
332                                    np->e_dn ));
333 #else
334                         Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
335                                 0, 0 );
336 #endif
337
338
339                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
340                             NULL, NULL, NULL, NULL );
341
342                         goto return_results;
343                 }
344
345 #ifdef NEW_LOGGING
346                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
347                            "ldbm_back_modrdn: wr to new parent's children OK.\n" ));
348 #else
349                 Debug( LDAP_DEBUG_TRACE,
350                        "ldbm_back_modrdn: wr to new parent's children OK\n",
351                        0, 0 , 0 );
352 #endif
353
354
355                 new_parent_dn = np_dn;
356         }
357         
358         /* Build target dn and make sure target entry doesn't exist already. */
359
360         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
361
362
363         new_ndn = ch_strdup(new_dn);
364         (void) dn_normalize( new_ndn );
365
366 #ifdef NEW_LOGGING
367         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
368                    "ldbm_back_modrdn: new ndn=%s\n", new_ndn ));
369 #else
370         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
371                new_ndn, 0, 0 );
372 #endif
373
374
375         /* check for abandon */
376         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
377         if ( op->o_abandon ) {
378                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
379                 goto return_results;
380         }
381
382         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
383         if (dn2id ( be, new_ndn ) != NOID) {
384                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
385                         NULL, NULL, NULL, NULL );
386                 goto return_results;
387         }
388
389 #ifdef NEW_LOGGING
390         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
391                    "ldbm_back_modrdn: new ndn (%s) does not exist\n", new_ndn ));
392 #else
393         Debug( LDAP_DEBUG_TRACE,
394                "ldbm_back_modrdn: new ndn=%s does not exist\n",
395                new_ndn, 0, 0 );
396 #endif
397
398
399         /* Get attribute type and attribute value of our new rdn, we will
400          * need to add that to our new entry
401          */
402
403         if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
404             
405 #ifdef NEW_LOGGING
406                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
407                            "ldbm_back_modrdn: can't figure out type of newrdn\n" ));
408 #else
409                 Debug( LDAP_DEBUG_TRACE,
410                        "ldbm_back_modrdn: can't figure out type of newrdn\n",
411                        0, 0, 0 );
412 #endif
413
414                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
415                         NULL, "unknown type used in RDN", NULL, NULL );
416                 goto return_results;            
417
418         }
419
420         if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
421             
422 #ifdef NEW_LOGGING
423                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
424                            "ldbm_back_modrdn: can't figure out val of newrdn\n"));
425 #else
426                 Debug( LDAP_DEBUG_TRACE,
427                        "ldbm_back_modrdn: can't figure out val of newrdn\n",
428                        0, 0, 0 );
429 #endif
430
431                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
432                         NULL, "could not parse RDN value", NULL, NULL );
433                 goto return_results;            
434
435         }
436
437 #ifdef NEW_LOGGING
438         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
439                    "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
440                    new_rdn_val, new_rdn_type ));
441 #else
442         Debug( LDAP_DEBUG_TRACE,
443                "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
444                new_rdn_val, new_rdn_type, 0 );
445 #endif
446
447
448         /* Retrieve the old rdn from the entry's dn */
449
450         if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
451 #ifdef NEW_LOGGING
452                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
453                            "ldbm_back_modrdn: can't figure out old_rdn from dn (%s)\n",
454                            dn ));
455 #else
456                 Debug( LDAP_DEBUG_TRACE,
457                        "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
458                        0, 0, 0 );
459 #endif
460
461                 send_ldap_result( conn, op, LDAP_OTHER,
462                         NULL, "could not parse old DN", NULL, NULL );
463                 goto return_results;            
464         }
465
466         if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
467 #ifdef NEW_LOGGING
468                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
469                            "ldbm_back_modrdn: can't figure out the old_rdn type.\n" ));
470 #else
471                 Debug( LDAP_DEBUG_TRACE,
472                        "ldbm_back_modrdn: can't figure out the old_rdn type\n",
473                        0, 0, 0 );
474 #endif
475
476                 send_ldap_result( conn, op, LDAP_OTHER,
477                         NULL, "count parse RDN from old DN", NULL, NULL );
478                 goto return_results;            
479         }
480         
481         if ( newSuperior == NULL
482                 && strcasecmp( old_rdn_type, new_rdn_type ) != 0 )
483         {
484             /* Not a big deal but we may say something */
485 #ifdef NEW_LOGGING
486             LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
487                        "ldbm_back_modrdn: old_rdn_type=%s new_rdn_type-%s\n",
488                        old_rdn_type, new_rdn_type ));
489 #else
490             Debug( LDAP_DEBUG_TRACE,
491                    "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
492                    old_rdn_type, new_rdn_type, 0 );
493 #endif
494
495         }               
496
497 #ifdef NEW_LOGGING
498         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
499                    "ldbm_back_modrdn:  DN_X500\n" ));
500 #else
501                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
502                        0, 0, 0 );
503 #endif
504
505                 
506                 /* Add new attribute value to the entry.
507                  */
508
509                 add_bvals[0] = &add_bv;         /* Array of bervals */
510                 add_bvals[1] = NULL;
511
512                 add_bv.bv_val = new_rdn_val;
513                 add_bv.bv_len = strlen(new_rdn_val);
514                 
515                 {
516                         int rc;
517
518                         mod[0].sml_desc = NULL;
519                         rc = slap_str2ad( new_rdn_type, &mod[0].sml_desc, &text );
520
521                         if( rc != LDAP_SUCCESS ) {
522 #ifdef NEW_LOGGING
523                                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
524                                            "ldbm_back_modrdn: slap_str2ad error: %s (%s)\n",
525                                            text, new_rdn_type ));
526 #else
527                                 Debug( LDAP_DEBUG_TRACE,
528                                         "ldbm_back_modrdn: %s: %s (new)\n",
529                                         text, new_rdn_type, 0 );
530 #endif
531
532                                 send_ldap_result( conn, op, rc,
533                                         NULL, text, NULL, NULL );
534                                 goto return_results;            
535                         }
536                 }
537                 mod[0].sml_bvalues = add_bvals;
538                 mod[0].sml_op = SLAP_MOD_SOFTADD;
539                 mod[0].sml_next = NULL;
540
541                 /* Remove old rdn value if required */
542
543                 if (deleteoldrdn) {
544                         /* Get value of old rdn */
545         
546                         if ((old_rdn_val = rdn_attr_value( old_rdn ))
547                             == NULL) {
548                             
549 #ifdef NEW_LOGGING
550                                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
551                                            "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n" ));
552 #else
553                                 Debug( LDAP_DEBUG_TRACE,
554                                        "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
555                                        0, 0, 0 );
556 #endif
557
558                                 send_ldap_result( conn, op, LDAP_OTHER,
559                                         NULL, "could not parse value from old RDN", NULL, NULL );
560                                 goto return_results;            
561                         }
562
563                         del_bvals[0] = &del_bv;         /* Array of bervals */
564                         del_bvals[1] = NULL;
565
566                         /* Remove old value of rdn as an attribute. */
567                     
568                         del_bv.bv_val = old_rdn_val;
569                         del_bv.bv_len = strlen(old_rdn_val);
570
571                         {
572                                 int rc;
573
574                                 mod[1].sml_desc = NULL;
575                                 rc = slap_str2ad( old_rdn_type, &mod[1].sml_desc, &text );
576
577                                 if( rc != LDAP_SUCCESS ) {
578 #ifdef NEW_LOGGING
579                                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
580                                                    "ldbm_back_modrdn: %s: %s (old)\n",
581                                                    text, old_rdn_type ));
582 #else
583                                         Debug( LDAP_DEBUG_TRACE,
584                                                 "ldbm_back_modrdn: %s: %s (old)\n",
585                                                 text, old_rdn_type, 0 );
586 #endif
587
588                                         send_ldap_result( conn, op, rc,
589                                                 NULL, text, NULL, NULL );
590                                         goto return_results;            
591                                 }
592                         }
593                         mod[0].sml_next = &mod[1];
594                         mod[1].sml_bvalues = del_bvals;
595                         mod[1].sml_op = LDAP_MOD_DELETE;
596                         mod[1].sml_next = NULL;
597
598 #ifdef NEW_LOGGING
599                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
600                                    "ldbm_back_modrdn: removing old_rdn_val=%s\n", old_rdn_val ));
601 #else
602                         Debug( LDAP_DEBUG_TRACE,
603                                "ldbm_back_modrdn: removing old_rdn_val=%s\n",
604                                old_rdn_val, 0, 0 );
605 #endif
606
607                 }
608         
609         /* check for abandon */
610         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
611         if ( op->o_abandon ) {
612                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
613                 goto return_results;
614         }
615         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
616
617         /* delete old one */
618         if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
619                 send_ldap_result( conn, op, LDAP_OTHER,
620                         NULL, "DN index delete fail", NULL, NULL );
621                 goto return_results;
622         }
623
624         (void) cache_delete_entry( &li->li_cache, e );
625
626         /* XXX: there is no going back! */
627
628         free( e->e_dn );
629         free( e->e_ndn );
630         e->e_dn = new_dn;
631         e->e_ndn = new_ndn;
632         new_dn = NULL;
633         new_ndn = NULL;
634
635         /* add new one */
636         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
637                 send_ldap_result( conn, op, LDAP_OTHER,
638                         NULL, "DN index add failed", NULL, NULL );
639                 goto return_results;
640         }
641
642         /* modify memory copy of entry */
643         rc = ldbm_modify_internal( be, conn, op, dn, &mod[0], e, &text );
644
645         if( rc != LDAP_SUCCESS ) {
646                 if( rc != SLAPD_ABANDON ) {
647                         send_ldap_result( conn, op, rc,
648                                 NULL, text, NULL, NULL );
649                 }
650             
651             goto return_results;
652         }
653         
654         (void) cache_update_entry( &li->li_cache, e );
655
656         /* NOTE: after this you must not free new_dn or new_ndn!
657          * They are used by cache.
658          */
659
660         /* id2entry index */
661         if ( id2entry_add( be, e ) != 0 ) {
662                 entry_free( e );
663                 send_ldap_result( conn, op, LDAP_OTHER,
664                         NULL, "entry update failed", NULL, NULL );
665                 goto return_results;
666         }
667
668         send_ldap_result( conn, op, LDAP_SUCCESS,
669                 NULL, NULL, NULL, NULL );
670         rc = 0;
671
672 return_results:
673         if( new_dn != NULL ) free( new_dn );
674         if( new_ndn != NULL ) free( new_ndn );
675
676         if( p_dn != NULL ) free( p_dn );
677         if( p_ndn != NULL ) free( p_ndn );
678
679         /* LDAP v2 supporting correct attribute handling. */
680         if( new_rdn_type != NULL ) free(new_rdn_type);
681         if( new_rdn_val != NULL ) free(new_rdn_val);
682         if( old_rdn != NULL ) free(old_rdn);
683         if( old_rdn_type != NULL ) free(old_rdn_type);
684         if( old_rdn_val != NULL ) free(old_rdn_val);
685
686
687         /* LDAP v3 Support */
688         if ( np_dn != NULL ) free( np_dn );
689         if ( np_ndn != NULL ) free( np_ndn );
690
691         if( np != NULL ) {
692                 /* free new parent and writer lock */
693                 cache_return_entry_w( &li->li_cache, np );
694         }
695
696         if( p != NULL ) {
697                 /* free parent and writer lock */
698                 cache_return_entry_w( &li->li_cache, p );
699         }
700
701         if ( rootlock ) {
702                 /* release root writer lock */
703                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
704         }
705
706         /* free entry and writer lock */
707         cache_return_entry_w( &li->li_cache, e );
708         return( rc );
709 }