]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
ITS#2795 fix hang. failure still leaves corrupt database though.
[openldap] / servers / slapd / back-ldbm / modrdn.c
1 /* modrdn.c - ldbm backend modrdn routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright 1999, Juan C. Gomez, All rights reserved.
17  * This software is not subject to any license of Silicon Graphics 
18  * Inc. or Purdue University.
19  *
20  * Redistribution and use in source and binary forms are permitted
21  * without restriction or fee of any kind as long as this notice
22  * is preserved.
23  */
24
25 #include "portable.h"
26
27 #include <stdio.h>
28
29 #include <ac/string.h>
30 #include <ac/socket.h>
31
32 #include "slap.h"
33 #include "back-ldbm.h"
34 #include "proto-back-ldbm.h"
35
36 int
37 ldbm_back_modrdn(
38     Operation   *op,
39     SlapReply   *rs )
40 {
41         AttributeDescription *children = slap_schema.si_ad_children;
42         AttributeDescription *entry = slap_schema.si_ad_entry;
43         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
44         struct berval   p_dn, p_ndn;
45         struct berval   new_dn = { 0, NULL}, new_ndn = { 0, NULL };
46         Entry           *e, *p = NULL;
47         Entry           *matched;
48         /* LDAP v2 supporting correct attribute handling. */
49         LDAPRDN         new_rdn = NULL;
50         LDAPRDN         old_rdn = NULL;
51         int             isroot = -1;
52 #define CAN_ROLLBACK    -1
53 #define MUST_DESTROY    1
54         int             rc = CAN_ROLLBACK;
55         int             rc_id = 0;
56         ID              id = NOID;
57         const char      *text = NULL;
58         char            textbuf[SLAP_TEXT_BUFLEN];
59         size_t          textlen = sizeof textbuf;
60         /* Added to support newSuperior */ 
61         Entry           *np = NULL;     /* newSuperior Entry */
62         struct berval   *np_ndn = NULL; /* newSuperior ndn */
63         struct berval   *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
64         /* Used to interface with ldbm_modify_internal() */
65         Modifications   *mod = NULL;            /* Used to delete old/add new rdn */
66         int             manageDSAit = get_manageDSAit( op );
67
68 #ifdef NEW_LOGGING
69         LDAP_LOG( BACK_LDBM, ENTRY, 
70                 "ldbm_back_modrdn: dn: %s newSuperior=%s\n", 
71                 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
72                 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len ) ? op->oq_modrdn.rs_newSup->bv_val : "NULL",0 );
73 #else
74         Debug( LDAP_DEBUG_TRACE,
75                 "==>ldbm_back_modrdn: dn: %s newSuperior=%s\n", 
76                 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
77                 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len )
78                         ? op->oq_modrdn.rs_newSup->bv_val : "NULL", 0 );
79 #endif
80
81         /* grab giant lock for writing */
82         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
83
84         e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
85
86         /* get entry with writer lock */
87         /* FIXME: dn2entry() should return non-glue entry */
88         if (( e == NULL  ) || ( !manageDSAit && e && is_entry_glue( e ))) {
89                 if ( matched != NULL ) {
90                         rs->sr_matched = strdup( matched->e_dn );
91                         rs->sr_ref = is_entry_referral( matched )
92                                 ? get_entry_referrals( op, matched )
93                                 : NULL;
94                         cache_return_entry_r( &li->li_cache, matched );
95                 } else {
96                         BerVarray deref = NULL;
97                         if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
98                                 syncinfo_t *si;
99                                 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
100                                         struct berval tmpbv;
101                                         ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
102                                         ber_bvarray_add( &deref, &tmpbv );
103                                 }
104                         } else {
105                                 deref = default_referral;
106                         }
107                         rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
108                                                         LDAP_SCOPE_DEFAULT );
109                 }
110
111                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
112
113                 rs->sr_err = LDAP_REFERRAL;
114                 send_ldap_result( op, rs );
115
116                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
117                 free( (char *)rs->sr_matched );
118                 rs->sr_ref = NULL;
119                 rs->sr_matched = NULL;
120                 return rs->sr_err;
121         }
122
123         /* check entry for "entry" acl */
124         if ( ! access_allowed( op, e,
125                 entry, NULL, ACL_WRITE, NULL ) )
126         {
127 #ifdef NEW_LOGGING
128                 LDAP_LOG( BACK_LDBM, ERR, 
129                         "ldbm_back_modrdn: no write access to entry of (%s)\n", 
130                         op->o_req_dn.bv_val, 0, 0 );
131 #else
132                 Debug( LDAP_DEBUG_TRACE,
133                         "<=- ldbm_back_modrdn: no write access to entry\n", 0,
134                         0, 0 );
135 #endif
136
137                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
138                         "no write access to entry" );
139
140                 goto return_results;
141         }
142
143         if (!manageDSAit && is_entry_referral( e ) ) {
144                 /* parent is a referral, don't allow add */
145                 /* parent is an alias, don't allow add */
146                 rs->sr_ref = get_entry_referrals( op, e );
147
148 #ifdef NEW_LOGGING
149                 LDAP_LOG( BACK_LDBM, INFO, 
150                         "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn, 0, 0 );
151 #else
152                 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
153                     0, 0 );
154 #endif
155
156                 rs->sr_err = LDAP_REFERRAL;
157                 rs->sr_matched = e->e_name.bv_val;
158                 send_ldap_result( op, rs );
159
160                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
161                 rs->sr_ref = NULL;
162                 rs->sr_matched = NULL;
163                 goto return_results;
164         }
165
166         if ( has_children( op->o_bd, e ) ) {
167 #ifdef NEW_LOGGING
168                 LDAP_LOG( BACK_LDBM, INFO, 
169                         "ldbm_back_modrdn: entry %s has children\n", e->e_dn, 0, 0 );
170 #else
171                 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
172                     0, 0 );
173 #endif
174
175                 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
176                     "subtree rename not supported" );
177                 goto return_results;
178         }
179
180         if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
181                 p_ndn = slap_empty_bv ;
182         } else {
183                 dnParent( &e->e_nname, &p_ndn );
184         }
185
186         if ( p_ndn.bv_len != 0 ) {
187                 /* Make sure parent entry exist and we can write its 
188                  * children.
189                  */
190
191                 if( (p = dn2entry_w( op->o_bd, &p_ndn, NULL )) == NULL) {
192 #ifdef NEW_LOGGING
193                         LDAP_LOG( BACK_LDBM, INFO, 
194                                 "ldbm_back_modrdn: parent of %s does not exist\n", 
195                                 e->e_ndn, 0, 0 );
196 #else
197                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
198                                 0, 0, 0);
199 #endif
200
201                         send_ldap_error( op, rs, LDAP_OTHER,
202                                 "parent entry does not exist" );
203
204                         goto return_results;
205                 }
206
207                 /* check parent for "children" acl */
208                 if ( ! access_allowed( op, p,
209                         children, NULL, ACL_WRITE, NULL ) )
210                 {
211 #ifdef NEW_LOGGING
212                         LDAP_LOG( BACK_LDBM, INFO, 
213                                 "ldbm_back_modrdn: no access to parent of (%s)\n", 
214                                 e->e_dn, 0, 0 );
215 #else
216                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
217                                 0, 0 );
218 #endif
219
220                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
221                                 NULL );
222                         goto return_results;
223                 }
224
225 #ifdef NEW_LOGGING
226                 LDAP_LOG( BACK_LDBM, DETAIL1, 
227                         "ldbm_back_modrdn: wr to children of entry %s OK\n", 
228                         p_ndn.bv_val, 0, 0 );
229 #else
230                 Debug( LDAP_DEBUG_TRACE,
231                        "ldbm_back_modrdn: wr to children of entry %s OK\n",
232                        p_ndn.bv_val, 0, 0 );
233 #endif
234
235                 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
236                         p_dn = slap_empty_bv;
237                 } else {
238                         dnParent( &e->e_name, &p_dn );
239                 }
240
241 #ifdef NEW_LOGGING
242                 LDAP_LOG( BACK_LDBM, DETAIL1, 
243                            "ldbm_back_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
244 #else
245                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
246                        p_dn.bv_val, 0, 0 );
247 #endif
248
249         } else {
250                 /* no parent, must be root to modify rdn */
251                 isroot = be_isroot( op->o_bd, &op->o_ndn );
252                 if ( ! isroot ) {
253                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
254                                 int     can_access;
255                                 p = (Entry *)&slap_entry_root;
256                                 
257                                 can_access = access_allowed( op, p,
258                                                 children, NULL, ACL_WRITE, NULL );
259                                 p = NULL;
260                                                                 
261                                 /* check parent for "children" acl */
262                                 if ( ! can_access ) {
263 #ifdef NEW_LOGGING
264                                         LDAP_LOG( BACK_LDBM, ERR,
265                                                 "ldbm_back_modrdn: no access to parent \"\"\n", 0,0,0 );
266 #else
267                                         Debug( LDAP_DEBUG_TRACE,
268                                                 "<=- ldbm_back_modrdn: no "
269                                                 "access to parent\n", 0, 0, 0 );
270 #endif
271
272                                         send_ldap_error( op, rs,
273                                                 LDAP_INSUFFICIENT_ACCESS,
274                                                 NULL );
275                                         goto return_results;
276                                 }
277
278                         } else {
279 #ifdef NEW_LOGGING
280                                 LDAP_LOG( BACK_LDBM, ERR, 
281                                         "ldbm_back_modrdn: (%s) has no parent & not a root.\n", 
282                                         op->o_ndn, 0, 0 );
283 #else
284                                 Debug( LDAP_DEBUG_TRACE,
285                                         "<=- ldbm_back_modrdn: no parent & "
286                                         "not root\n", 0, 0, 0);
287 #endif
288
289                                 send_ldap_error( op, rs,
290                                         LDAP_INSUFFICIENT_ACCESS,
291                                         NULL );
292                                 goto return_results;
293                         }
294                 }
295
296 #ifdef NEW_LOGGING
297                 LDAP_LOG( BACK_LDBM, INFO, 
298                    "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn, 0, 0 );
299 #else
300                 Debug( LDAP_DEBUG_TRACE,
301                        "ldbm_back_modrdn: no parent, locked root\n",
302                        0, 0, 0 );
303 #endif
304         }
305
306         new_parent_dn = &p_dn;  /* New Parent unless newSuperior given */
307
308         if ( op->oq_modrdn.rs_newSup != NULL ) {
309 #ifdef NEW_LOGGING
310                 LDAP_LOG( BACK_LDBM, DETAIL1, 
311                         "ldbm_back_modrdn: new parent \"%s\" requested\n",
312                         op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
313 #else
314                 Debug( LDAP_DEBUG_TRACE, 
315                         "ldbm_back_modrdn: new parent \"%s\" requested...\n",
316                         op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
317 #endif
318
319                 np_ndn = op->oq_modrdn.rs_nnewSup;
320
321                 /* newSuperior == oldParent? */
322                 if ( dn_match( &p_ndn, np_ndn ) ) {
323 #ifdef NEW_LOGGING
324                         LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: "
325                                 "new parent\"%s\" seems to be the same as the "
326                                 "old parent \"%s\"\n", op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
327 #else
328                         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: "
329                                 "new parent\"%s\" seems to be the same as the "
330                                 "old parent \"%s\"\n",
331                                 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
332 #endif
333
334                         op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
335                 }
336         }
337
338         if ( op->oq_modrdn.rs_newSup != NULL ) {
339                 /* newSuperior == entry being moved?, if so ==> ERROR */
340                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
341
342                 if ( op->oq_modrdn.rs_nnewSup->bv_len ) {
343                         if( (np = dn2entry_w( op->o_bd, np_ndn, NULL )) == NULL) {
344 #ifdef NEW_LOGGING
345                                 LDAP_LOG( BACK_LDBM, ERR, 
346                                         "ldbm_back_modrdn: newSup(ndn=%s) not found.\n", 
347                                         np_ndn->bv_val, 0, 0 );
348 #else
349                                 Debug( LDAP_DEBUG_TRACE,
350                                     "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
351                                     np_ndn->bv_val, 0, 0);
352 #endif
353
354                                 send_ldap_error( op, rs, LDAP_OTHER,
355                                         "newSuperior not found" );
356                                 goto return_results;
357                         }
358
359 #ifdef NEW_LOGGING
360                         LDAP_LOG( BACK_LDBM, DETAIL1,
361                                 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
362                                 (void *) np, np->e_id, 0 );
363 #else
364                         Debug( LDAP_DEBUG_TRACE,
365                                 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
366                                 (void *) np, np->e_id, 0 );
367 #endif
368
369                         /* check newSuperior for "children" acl */
370                         if ( !access_allowed( op, np, children, NULL,
371                                               ACL_WRITE, NULL ) )
372                         {
373 #ifdef NEW_LOGGING
374                                 LDAP_LOG( BACK_LDBM, INFO,
375                                    "ldbm_back_modrdn: no wr to newSup children.\n", 0, 0, 0 );
376 #else
377                                 Debug( LDAP_DEBUG_TRACE,
378                                        "ldbm_back_modrdn: no wr to newSup children\n",
379                                        0, 0, 0 );
380 #endif
381
382                                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
383                                 goto return_results;
384                         }
385
386                         if ( is_entry_alias( np ) ) {
387                                 /* parent is an alias, don't allow add */
388 #ifdef NEW_LOGGING
389                                 LDAP_LOG( BACK_LDBM, INFO,
390                                    "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn,0,0);
391 #else
392                                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
393 #endif
394
395
396                                 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
397                                     "newSuperior is an alias" );
398
399                                 goto return_results;
400                         }
401
402                         if ( is_entry_referral( np ) ) {
403                                 /* parent is a referral, don't allow add */
404 #ifdef NEW_LOGGING
405                                 LDAP_LOG( BACK_LDBM, INFO,
406                                         "ldbm_back_modrdn: entry (%s) is a referral\n",
407                                         np->e_dn, 0, 0 );
408 #else
409                                 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
410                                         np->e_dn, 0, 0 );
411 #endif
412
413                                 send_ldap_error( op, rs, LDAP_OTHER,
414                                     "newSuperior is a referral" );
415
416                                 goto return_results;
417                         }
418
419                 } else {
420
421                         /* no parent, must be root to modify newSuperior */
422                         if ( isroot == -1 ) {
423                                 isroot = be_isroot( op->o_bd, &op->o_ndn );
424                         }
425
426                         if ( ! isroot ) {
427                                 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
428                                         int     can_access;
429                                         np = (Entry *)&slap_entry_root;
430                                 
431                                         can_access = access_allowed( op, np,
432                                                         children, NULL, ACL_WRITE, NULL );
433                                         np = NULL;
434                                                                 
435                                         /* check parent for "children" acl */
436                                         if ( ! can_access ) {
437 #ifdef NEW_LOGGING
438                                                 LDAP_LOG( BACK_LDBM, ERR,
439                                                         "ldbm_back_modrdn: no access "
440                                                         "to new superior \"\"\n", 0, 0, 0 );
441 #else
442                                                 Debug( LDAP_DEBUG_TRACE,
443                                                         "<=- ldbm_back_modrdn: no "
444                                                         "access to new superior\n", 0, 0, 0 );
445 #endif
446
447                                                 send_ldap_error( op, rs,
448                                                         LDAP_INSUFFICIENT_ACCESS,
449                                                         NULL );
450                                                 goto return_results;
451                                         }
452
453                                 } else {
454 #ifdef NEW_LOGGING
455                                         LDAP_LOG( BACK_LDBM, ERR,
456                                                 "ldbm_back_modrdn: \"\" not allowed as new superior\n",
457                                                 0, 0, 0 );
458 #else
459                                         Debug( LDAP_DEBUG_TRACE,
460                                                 "<=- ldbm_back_modrdn: \"\" "
461                                                 "not allowed as new superior\n", 
462                                                 0, 0, 0);
463 #endif
464
465                                         send_ldap_error( op, rs,
466                                                 LDAP_INSUFFICIENT_ACCESS,
467                                                 NULL );
468                                         goto return_results;
469                                 }
470                         }
471                 }
472
473 #ifdef NEW_LOGGING
474                 LDAP_LOG( BACK_LDBM, DETAIL1,
475                         "ldbm_back_modrdn: wr to new parent's children OK.\n", 0, 0, 0 );
476 #else
477                 Debug( LDAP_DEBUG_TRACE,
478                     "ldbm_back_modrdn: wr to new parent's children OK\n",
479                     0, 0, 0 );
480 #endif
481
482                 new_parent_dn = op->oq_modrdn.rs_newSup;
483         }
484         
485         /* Build target dn and make sure target entry doesn't exist already. */
486         build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn, NULL ); 
487         dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
488
489 #ifdef NEW_LOGGING
490         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: new ndn=%s\n", 
491                 new_ndn.bv_val, 0, 0 );
492 #else
493         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
494             new_ndn.bv_val, 0, 0 );
495 #endif
496
497         /* check for abandon */
498         if ( op->o_abandon ) {
499                 goto return_results;
500         }
501
502         if ( ( rc_id = dn2id ( op->o_bd, &new_ndn, &id ) ) || id != NOID ) {
503                 /* if (rc_id) something bad happened to ldbm cache */
504                 rs->sr_err = rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
505                 send_ldap_result( op, rs );
506                 goto return_results;
507         }
508
509 #ifdef NEW_LOGGING
510         LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: new ndn (%s) does not exist\n",
511                 new_ndn.bv_val, 0, 0 );
512 #else
513         Debug( LDAP_DEBUG_TRACE,
514             "ldbm_back_modrdn: new ndn=%s does not exist\n",
515             new_ndn.bv_val, 0, 0 );
516 #endif
517
518         /* Get attribute type and attribute value of our new rdn, we will
519          * need to add that to our new entry
520          */
521         if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text,
522                 LDAP_DN_FORMAT_LDAP ) )
523         {
524 #ifdef NEW_LOGGING
525                 LDAP_LOG ( OPERATION, ERR, 
526                         "ldbm_back_modrdn: can't figure out "
527                         "type(s)/values(s) of newrdn\n", 
528                         0, 0, 0 );
529 #else
530                 Debug( LDAP_DEBUG_TRACE,
531                         "ldbm_back_modrdn: can't figure out "
532                         "type(s)/values(s) of newrdn\n", 
533                         0, 0, 0 );
534 #endif
535                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX,
536                                     "unknown type(s) used in RDN" );
537                 goto return_results;            
538         }
539
540 #ifdef NEW_LOGGING
541         LDAP_LOG ( OPERATION, RESULTS, 
542                 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
543                 "new_rdn_val=\"%s\"\n",
544                 new_rdn[ 0 ]->la_attr.bv_val, 
545                 new_rdn[ 0 ]->la_value.bv_val, 0 );
546 #else
547         Debug( LDAP_DEBUG_TRACE,
548                 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
549                 "new_rdn_val=\"%s\"\n",
550                 new_rdn[ 0 ]->la_attr.bv_val,
551                 new_rdn[ 0 ]->la_value.bv_val, 0 );
552 #endif
553
554         if ( op->oq_modrdn.rs_deleteoldrdn ) {
555                 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
556                         LDAP_DN_FORMAT_LDAP ) )
557                 {
558 #ifdef NEW_LOGGING
559                         LDAP_LOG ( OPERATION, ERR, 
560                                 "ldbm_back_modrdn: can't figure out "
561                                 "type(s)/values(s) of old_rdn\n", 
562                                 0, 0, 0 );
563 #else
564                         Debug( LDAP_DEBUG_TRACE,
565                                 "ldbm_back_modrdn: can't figure out "
566                                 "the old_rdn type(s)/value(s)\n", 
567                                 0, 0, 0 );
568 #endif
569                         send_ldap_error( op, rs, LDAP_OTHER,
570                                     "cannot parse RDN from old DN" );
571                         goto return_results;            
572                 }
573         }
574
575 #ifdef NEW_LOGGING
576         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn:  DN_X500\n", 0, 0, 0 );
577 #else
578         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
579                0, 0, 0 );
580 #endif
581         
582         if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
583                 send_ldap_result( op, rs );
584                 goto return_results;
585         }
586
587
588         /* check for abandon */
589         if ( op->o_abandon ) {
590                 goto return_results;
591         }
592
593         /* delete old one */
594         if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
595                 send_ldap_error( op, rs, LDAP_OTHER,
596                         "DN index delete fail" );
597                 goto return_results;
598         }
599
600         (void) cache_delete_entry( &li->li_cache, e );
601         rc = MUST_DESTROY;
602
603         /* XXX: there is no going back! */
604
605         free( e->e_dn );
606         free( e->e_ndn );
607         e->e_name = new_dn;
608         e->e_nname = new_ndn;
609         new_dn.bv_val = NULL;
610         new_ndn.bv_val = NULL;
611
612         /* NOTE: after this you must not free new_dn or new_ndn!
613          * They are used by cache.
614          */
615
616         /* add new one */
617         if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
618                 send_ldap_error( op, rs, LDAP_OTHER,
619                         "DN index add failed" );
620                 goto return_results;
621         }
622
623         /* modify memory copy of entry */
624         rs->sr_err = ldbm_modify_internal( op, &mod[0], e,
625                 &rs->sr_text, textbuf, textlen );
626         switch ( rs->sr_err ) {
627         case LDAP_SUCCESS:
628                 break;
629
630         case SLAPD_ABANDON:
631                 /* too late ... */
632                 goto return_results;
633         
634         default:
635                 /* here we may try to delete the newly added dn */
636                 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
637                         /* we already are in trouble ... */
638                         ;
639                 }
640                 send_ldap_result( op, rs );
641                 goto return_results;
642         }
643         
644         (void) cache_update_entry( &li->li_cache, e );
645
646         /* id2entry index */
647         if ( id2entry_add( op->o_bd, e ) != 0 ) {
648                 send_ldap_error( op, rs, LDAP_OTHER,
649                         "entry update failed" );
650                 goto return_results;
651         }
652
653         rs->sr_err = LDAP_SUCCESS;
654         rs->sr_text = NULL;
655         send_ldap_result( op, rs );
656         rc = 0;
657         cache_entry_commit( e );
658
659 return_results:
660         if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
661         if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
662
663         /* LDAP v2 supporting correct attribute handling. */
664         if ( new_rdn != NULL ) {
665                 ldap_rdnfree( new_rdn );
666         }
667         if ( old_rdn != NULL ) {
668                 ldap_rdnfree( old_rdn );
669         }
670         if ( mod != NULL ) {
671                 Modifications *tmp;
672                 for (; mod; mod = tmp ) {
673                         /* slap_modrdn2mods does things one way,
674                          * slap_mods_opattrs does it differently
675                          */
676                         if ( mod->sml_op != SLAP_MOD_SOFTADD &&
677                                 mod->sml_op != LDAP_MOD_DELETE ) break;
678                         if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
679                         tmp = mod->sml_next;
680                         free( mod );
681                 }
682                 slap_mods_free( mod );
683         }
684
685         /* LDAP v3 Support */
686         if( np != NULL ) {
687                 /* free new parent and writer lock */
688                 cache_return_entry_w( &li->li_cache, np );
689         }
690
691         if( p != NULL ) {
692                 /* free parent and writer lock */
693                 cache_return_entry_w( &li->li_cache, p );
694         }
695
696         /* free entry and writer lock */
697         cache_return_entry_w( &li->li_cache, e );
698         if ( rc == MUST_DESTROY ) {
699                 /* if rc == MUST_DESTROY the entry is uncached 
700                  * and its private data is destroyed; 
701                  * the entry must be freed */
702                 entry_free( e );
703         }
704         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
705         rs->sr_text = NULL;
706         return( rc );
707 }