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