]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
first round of SHADOW flags/isupdate test unification
[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-2004 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 = BER_BVNULL, new_ndn = BER_BVNULL;
46         struct berval   old_ndn = BER_BVNULL;
47         Entry           *e, *p = NULL;
48         Entry           *matched;
49         /* LDAP v2 supporting correct attribute handling. */
50         LDAPRDN         new_rdn = NULL;
51         LDAPRDN         old_rdn = NULL;
52         int             isroot = -1;
53         int             rc_id = 0;
54         ID              id = NOID;
55         const char      *text = NULL;
56         char            textbuf[SLAP_TEXT_BUFLEN];
57         size_t          textlen = sizeof textbuf;
58         /* Added to support newSuperior */ 
59         Entry           *np = NULL;     /* newSuperior Entry */
60         struct berval   *np_ndn = NULL; /* newSuperior ndn */
61         struct berval   *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
62         /* Used to interface with ldbm_modify_internal() */
63         Modifications   *mod = NULL;            /* Used to delete old/add new rdn */
64         int             manageDSAit = get_manageDSAit( op );
65
66 #ifdef NEW_LOGGING
67         LDAP_LOG( BACK_LDBM, ENTRY, 
68                 "ldbm_back_modrdn: dn: %s newSuperior=%s\n", 
69                 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
70                 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len ) ? op->oq_modrdn.rs_newSup->bv_val : "NULL",0 );
71 #else
72         Debug( LDAP_DEBUG_TRACE,
73                 "==>ldbm_back_modrdn: dn: %s newSuperior=%s\n", 
74                 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
75                 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len )
76                         ? op->oq_modrdn.rs_newSup->bv_val : "NULL", 0 );
77 #endif
78
79         /* grab giant lock for writing */
80         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
81
82         e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
83
84         /* get entry with writer lock */
85         /* FIXME: dn2entry() should return non-glue entry */
86         if (( e == NULL  ) || ( !manageDSAit && e && is_entry_glue( e ))) {
87                 if ( matched != NULL ) {
88                         rs->sr_matched = strdup( matched->e_dn );
89                         rs->sr_ref = is_entry_referral( matched )
90                                 ? get_entry_referrals( op, matched )
91                                 : NULL;
92                         cache_return_entry_r( &li->li_cache, matched );
93                 } else {
94                         BerVarray deref = NULL;
95                         if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
96                                 syncinfo_t *si;
97                                 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
98                                         struct berval tmpbv;
99                                         ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
100                                         ber_bvarray_add( &deref, &tmpbv );
101                                 }
102                         } else {
103                                 deref = default_referral;
104                         }
105                         rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
106                                                         LDAP_SCOPE_DEFAULT );
107                 }
108
109                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
110
111                 rs->sr_err = LDAP_REFERRAL;
112                 send_ldap_result( op, rs );
113
114                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
115                 free( (char *)rs->sr_matched );
116                 rs->sr_ref = NULL;
117                 rs->sr_matched = NULL;
118                 return rs->sr_err;
119         }
120
121         /* check entry for "entry" acl */
122         if ( ! access_allowed( op, e,
123                 entry, NULL, ACL_WRITE, NULL ) )
124         {
125 #ifdef NEW_LOGGING
126                 LDAP_LOG( BACK_LDBM, ERR, 
127                         "ldbm_back_modrdn: no write access to entry of (%s)\n", 
128                         op->o_req_dn.bv_val, 0, 0 );
129 #else
130                 Debug( LDAP_DEBUG_TRACE,
131                         "<=- ldbm_back_modrdn: no write access to entry\n", 0,
132                         0, 0 );
133 #endif
134
135                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
136                         "no write access to entry" );
137
138                 goto return_results;
139         }
140
141         if (!manageDSAit && is_entry_referral( e ) ) {
142                 /* parent is a referral, don't allow add */
143                 /* parent is an alias, don't allow add */
144                 rs->sr_ref = get_entry_referrals( op, e );
145
146 #ifdef NEW_LOGGING
147                 LDAP_LOG( BACK_LDBM, INFO, 
148                         "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn, 0, 0 );
149 #else
150                 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
151                     0, 0 );
152 #endif
153
154                 rs->sr_err = LDAP_REFERRAL;
155                 rs->sr_matched = e->e_name.bv_val;
156                 send_ldap_result( op, rs );
157
158                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
159                 rs->sr_ref = NULL;
160                 rs->sr_matched = NULL;
161                 goto return_results;
162         }
163
164         if ( has_children( op->o_bd, e ) ) {
165 #ifdef NEW_LOGGING
166                 LDAP_LOG( BACK_LDBM, INFO, 
167                         "ldbm_back_modrdn: entry %s has children\n", e->e_dn, 0, 0 );
168 #else
169                 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
170                     0, 0 );
171 #endif
172
173                 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
174                     "subtree rename not supported" );
175                 goto return_results;
176         }
177
178         if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
179                 p_ndn = slap_empty_bv ;
180         } else {
181                 dnParent( &e->e_nname, &p_ndn );
182         }
183
184         if ( p_ndn.bv_len != 0 ) {
185                 /* Make sure parent entry exist and we can write its 
186                  * children.
187                  */
188
189                 if( (p = dn2entry_w( op->o_bd, &p_ndn, NULL )) == NULL) {
190 #ifdef NEW_LOGGING
191                         LDAP_LOG( BACK_LDBM, INFO, 
192                                 "ldbm_back_modrdn: parent of %s does not exist\n", 
193                                 e->e_ndn, 0, 0 );
194 #else
195                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
196                                 0, 0, 0);
197 #endif
198
199                         send_ldap_error( op, rs, LDAP_OTHER,
200                                 "parent entry does not exist" );
201
202                         goto return_results;
203                 }
204
205                 /* check parent for "children" acl */
206                 if ( ! access_allowed( op, p,
207                         children, NULL, ACL_WRITE, NULL ) )
208                 {
209 #ifdef NEW_LOGGING
210                         LDAP_LOG( BACK_LDBM, INFO, 
211                                 "ldbm_back_modrdn: no access to parent of (%s)\n", 
212                                 e->e_dn, 0, 0 );
213 #else
214                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
215                                 0, 0 );
216 #endif
217
218                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
219                                 NULL );
220                         goto return_results;
221                 }
222
223 #ifdef NEW_LOGGING
224                 LDAP_LOG( BACK_LDBM, DETAIL1, 
225                         "ldbm_back_modrdn: wr to children of entry %s OK\n", 
226                         p_ndn.bv_val, 0, 0 );
227 #else
228                 Debug( LDAP_DEBUG_TRACE,
229                        "ldbm_back_modrdn: wr to children of entry %s OK\n",
230                        p_ndn.bv_val, 0, 0 );
231 #endif
232
233                 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
234                         p_dn = slap_empty_bv;
235                 } else {
236                         dnParent( &e->e_name, &p_dn );
237                 }
238
239 #ifdef NEW_LOGGING
240                 LDAP_LOG( BACK_LDBM, DETAIL1, 
241                            "ldbm_back_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
242 #else
243                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
244                        p_dn.bv_val, 0, 0 );
245 #endif
246
247         } else {
248                 /* no parent, must be root to modify rdn */
249                 isroot = be_isroot( op );
250                 if ( ! isroot ) {
251                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
252                                 || be_shadow_update( op ) ) {
253                                 int     can_access;
254                                 p = (Entry *)&slap_entry_root;
255                                 
256                                 can_access = access_allowed( op, p,
257                                                 children, NULL, ACL_WRITE, NULL );
258                                 p = NULL;
259                                                                 
260                                 /* check parent for "children" acl */
261                                 if ( ! can_access ) {
262 #ifdef NEW_LOGGING
263                                         LDAP_LOG( BACK_LDBM, ERR,
264                                                 "ldbm_back_modrdn: no access to parent \"\"\n", 0,0,0 );
265 #else
266                                         Debug( LDAP_DEBUG_TRACE,
267                                                 "<=- ldbm_back_modrdn: no "
268                                                 "access to parent\n", 0, 0, 0 );
269 #endif
270
271                                         send_ldap_error( op, rs,
272                                                 LDAP_INSUFFICIENT_ACCESS,
273                                                 NULL );
274                                         goto return_results;
275                                 }
276
277                         } else {
278 #ifdef NEW_LOGGING
279                                 LDAP_LOG( BACK_LDBM, ERR, 
280                                         "ldbm_back_modrdn: (%s) has no parent & not a root.\n", 
281                                         op->o_ndn, 0, 0 );
282 #else
283                                 Debug( LDAP_DEBUG_TRACE,
284                                         "<=- ldbm_back_modrdn: no parent & "
285                                         "not root\n", 0, 0, 0);
286 #endif
287
288                                 send_ldap_error( op, rs,
289                                         LDAP_INSUFFICIENT_ACCESS,
290                                         NULL );
291                                 goto return_results;
292                         }
293                 }
294
295 #ifdef NEW_LOGGING
296                 LDAP_LOG( BACK_LDBM, INFO, 
297                    "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn, 0, 0 );
298 #else
299                 Debug( LDAP_DEBUG_TRACE,
300                        "ldbm_back_modrdn: no parent, locked root\n",
301                        0, 0, 0 );
302 #endif
303         }
304
305         new_parent_dn = &p_dn;  /* New Parent unless newSuperior given */
306
307         if ( op->oq_modrdn.rs_newSup != NULL ) {
308 #ifdef NEW_LOGGING
309                 LDAP_LOG( BACK_LDBM, DETAIL1, 
310                         "ldbm_back_modrdn: new parent \"%s\" requested\n",
311                         op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
312 #else
313                 Debug( LDAP_DEBUG_TRACE, 
314                         "ldbm_back_modrdn: new parent \"%s\" requested...\n",
315                         op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
316 #endif
317
318                 np_ndn = op->oq_modrdn.rs_nnewSup;
319
320                 /* newSuperior == oldParent? */
321                 if ( dn_match( &p_ndn, np_ndn ) ) {
322 #ifdef NEW_LOGGING
323                         LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: "
324                                 "new parent\"%s\" seems to be the same as the "
325                                 "old parent \"%s\"\n", op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
326 #else
327                         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: "
328                                 "new parent\"%s\" seems to be the same as the "
329                                 "old parent \"%s\"\n",
330                                 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
331 #endif
332
333                         op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
334                 }
335         }
336
337         if ( op->oq_modrdn.rs_newSup != NULL ) {
338                 /* newSuperior == entry being moved?, if so ==> ERROR */
339                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
340
341                 if ( op->oq_modrdn.rs_nnewSup->bv_len ) {
342                         if( (np = dn2entry_w( op->o_bd, np_ndn, NULL )) == NULL) {
343 #ifdef NEW_LOGGING
344                                 LDAP_LOG( BACK_LDBM, ERR, 
345                                         "ldbm_back_modrdn: newSup(ndn=%s) not found.\n", 
346                                         np_ndn->bv_val, 0, 0 );
347 #else
348                                 Debug( LDAP_DEBUG_TRACE,
349                                     "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
350                                     np_ndn->bv_val, 0, 0);
351 #endif
352
353                                 send_ldap_error( op, rs, LDAP_OTHER,
354                                         "newSuperior not found" );
355                                 goto return_results;
356                         }
357
358 #ifdef NEW_LOGGING
359                         LDAP_LOG( BACK_LDBM, DETAIL1,
360                                 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
361                                 (void *) np, np->e_id, 0 );
362 #else
363                         Debug( LDAP_DEBUG_TRACE,
364                                 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
365                                 (void *) np, np->e_id, 0 );
366 #endif
367
368                         /* check newSuperior for "children" acl */
369                         if ( !access_allowed( op, np, children, NULL,
370                                               ACL_WRITE, NULL ) )
371                         {
372 #ifdef NEW_LOGGING
373                                 LDAP_LOG( BACK_LDBM, INFO,
374                                    "ldbm_back_modrdn: no wr to newSup children.\n", 0, 0, 0 );
375 #else
376                                 Debug( LDAP_DEBUG_TRACE,
377                                        "ldbm_back_modrdn: no wr to newSup children\n",
378                                        0, 0, 0 );
379 #endif
380
381                                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
382                                 goto return_results;
383                         }
384
385                         if ( is_entry_alias( np ) ) {
386                                 /* parent is an alias, don't allow add */
387 #ifdef NEW_LOGGING
388                                 LDAP_LOG( BACK_LDBM, INFO,
389                                    "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn,0,0);
390 #else
391                                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
392 #endif
393
394
395                                 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
396                                     "newSuperior is an alias" );
397
398                                 goto return_results;
399                         }
400
401                         if ( is_entry_referral( np ) ) {
402                                 /* parent is a referral, don't allow add */
403 #ifdef NEW_LOGGING
404                                 LDAP_LOG( BACK_LDBM, INFO,
405                                         "ldbm_back_modrdn: entry (%s) is a referral\n",
406                                         np->e_dn, 0, 0 );
407 #else
408                                 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
409                                         np->e_dn, 0, 0 );
410 #endif
411
412                                 send_ldap_error( op, rs, LDAP_OTHER,
413                                     "newSuperior is a referral" );
414
415                                 goto return_results;
416                         }
417
418                 } else {
419
420                         /* no parent, must be root to modify newSuperior */
421                         if ( isroot == -1 ) {
422                                 isroot = be_isroot( op );
423                         }
424
425                         if ( ! isroot ) {
426                                 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
427                                         || be_shadow_update( op ) ) {
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         /* check for abandon */
588         if ( op->o_abandon ) {
589                 goto return_results;
590         }
591
592         (void) cache_delete_entry( &li->li_cache, e );
593
594         free( e->e_dn );
595         old_ndn = e->e_nname;
596         e->e_name = new_dn;
597         e->e_nname = new_ndn;
598         new_dn.bv_val = NULL;
599         new_ndn.bv_val = NULL;
600
601         /* NOTE: after this you must not free new_dn or new_ndn!
602          * They are used by cache.
603          */
604
605         /* modify memory copy of entry */
606         rs->sr_err = ldbm_modify_internal( op, &mod[0], e,
607                 &rs->sr_text, textbuf, textlen );
608         switch ( rs->sr_err ) {
609         case LDAP_SUCCESS:
610                 break;
611
612         default:
613                 send_ldap_result( op, rs );
614                 /* FALLTHRU */
615         case SLAPD_ABANDON:
616                 goto return_results;
617         }
618         
619         /*
620          * NOTE: the backend MUST delete then add the entry,
621          *              otherwise indexing may get hosed
622          * FIXME: if a new ID was used, the add could be done first.
623          *              that would be safer.
624          */
625
626         /* delete old one */
627         if ( dn2id_delete( op->o_bd, &old_ndn, e->e_id ) != 0 ) {
628                 send_ldap_error( op, rs, LDAP_OTHER,
629                         "DN index delete fail" );
630                 goto return_results;
631         }
632
633         /* add new one */
634         if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
635                 /* try to repair old entry - probably hopeless */
636         if( dn2id_add( op->o_bd, &old_ndn, e->e_id) != 0 ) {
637                         send_ldap_error( op, rs, LDAP_OTHER,
638                                 "DN index add and repair failed" );
639                 } else {
640                         send_ldap_error( op, rs, LDAP_OTHER,
641                                 "DN index add failed" );
642                 }
643                 goto return_results;
644         }
645
646         /* id2entry index */
647         if ( id2entry_add( op->o_bd, e ) != 0 ) {
648                 /* Try to undo */
649                 int rc;
650                 rc = dn2id_delete( op->o_bd, &e->e_nname, e->e_id );
651                 rc |= dn2id_add( op->o_bd, &old_ndn, e->e_id );
652                 if( rc ) {
653                         send_ldap_error( op, rs, LDAP_OTHER,
654                                 "entry update and repair failed" );
655                 } else {
656                         send_ldap_error( op, rs, LDAP_OTHER,
657                                 "entry update failed" );
658                 }
659                 goto return_results;
660         }
661
662         (void) cache_update_entry( &li->li_cache, e );
663
664         rs->sr_err = LDAP_SUCCESS;
665         rs->sr_text = NULL;
666         send_ldap_result( op, rs );
667         cache_entry_commit( e );
668
669 return_results:
670         if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
671         if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
672         if( old_ndn.bv_val != NULL ) free( old_ndn.bv_val );
673
674         /* LDAP v2 supporting correct attribute handling. */
675         if ( new_rdn != NULL ) {
676                 ldap_rdnfree( new_rdn );
677         }
678         if ( old_rdn != NULL ) {
679                 ldap_rdnfree( old_rdn );
680         }
681         if ( mod != NULL ) {
682                 Modifications *tmp;
683                 for (; mod; mod = tmp ) {
684                         /* slap_modrdn2mods does things one way,
685                          * slap_mods_opattrs does it differently
686                          */
687                         if ( mod->sml_op != SLAP_MOD_SOFTADD &&
688                                 mod->sml_op != LDAP_MOD_DELETE ) break;
689                         if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
690                         tmp = mod->sml_next;
691                         free( mod );
692                 }
693                 slap_mods_free( mod );
694         }
695
696         /* LDAP v3 Support */
697         if( np != NULL ) {
698                 /* free new parent and writer lock */
699                 cache_return_entry_w( &li->li_cache, np );
700         }
701
702         if( p != NULL ) {
703                 /* free parent and writer lock */
704                 cache_return_entry_w( &li->li_cache, p );
705         }
706
707         /* free entry and writer lock */
708         cache_return_entry_w( &li->li_cache, e );
709         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
710         rs->sr_text = NULL;
711         return( rs->sr_err );
712 }