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