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