]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
use BER_BVNULL
[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 ) || be_isupdate( op ) ) {
252                                 int     can_access;
253                                 p = (Entry *)&slap_entry_root;
254                                 
255                                 can_access = access_allowed( op, p,
256                                                 children, NULL, ACL_WRITE, NULL );
257                                 p = NULL;
258                                                                 
259                                 /* check parent for "children" acl */
260                                 if ( ! can_access ) {
261 #ifdef NEW_LOGGING
262                                         LDAP_LOG( BACK_LDBM, ERR,
263                                                 "ldbm_back_modrdn: no access to parent \"\"\n", 0,0,0 );
264 #else
265                                         Debug( LDAP_DEBUG_TRACE,
266                                                 "<=- ldbm_back_modrdn: no "
267                                                 "access to parent\n", 0, 0, 0 );
268 #endif
269
270                                         send_ldap_error( op, rs,
271                                                 LDAP_INSUFFICIENT_ACCESS,
272                                                 NULL );
273                                         goto return_results;
274                                 }
275
276                         } else {
277 #ifdef NEW_LOGGING
278                                 LDAP_LOG( BACK_LDBM, ERR, 
279                                         "ldbm_back_modrdn: (%s) has no parent & not a root.\n", 
280                                         op->o_ndn, 0, 0 );
281 #else
282                                 Debug( LDAP_DEBUG_TRACE,
283                                         "<=- ldbm_back_modrdn: no parent & "
284                                         "not root\n", 0, 0, 0);
285 #endif
286
287                                 send_ldap_error( op, rs,
288                                         LDAP_INSUFFICIENT_ACCESS,
289                                         NULL );
290                                 goto return_results;
291                         }
292                 }
293
294 #ifdef NEW_LOGGING
295                 LDAP_LOG( BACK_LDBM, INFO, 
296                    "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn, 0, 0 );
297 #else
298                 Debug( LDAP_DEBUG_TRACE,
299                        "ldbm_back_modrdn: no parent, locked root\n",
300                        0, 0, 0 );
301 #endif
302         }
303
304         new_parent_dn = &p_dn;  /* New Parent unless newSuperior given */
305
306         if ( op->oq_modrdn.rs_newSup != NULL ) {
307 #ifdef NEW_LOGGING
308                 LDAP_LOG( BACK_LDBM, DETAIL1, 
309                         "ldbm_back_modrdn: new parent \"%s\" requested\n",
310                         op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
311 #else
312                 Debug( LDAP_DEBUG_TRACE, 
313                         "ldbm_back_modrdn: new parent \"%s\" requested...\n",
314                         op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
315 #endif
316
317                 np_ndn = op->oq_modrdn.rs_nnewSup;
318
319                 /* newSuperior == oldParent? */
320                 if ( dn_match( &p_ndn, np_ndn ) ) {
321 #ifdef NEW_LOGGING
322                         LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: "
323                                 "new parent\"%s\" seems to be the same as the "
324                                 "old parent \"%s\"\n", op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
325 #else
326                         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: "
327                                 "new parent\"%s\" seems to be the same as the "
328                                 "old parent \"%s\"\n",
329                                 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
330 #endif
331
332                         op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
333                 }
334         }
335
336         if ( op->oq_modrdn.rs_newSup != NULL ) {
337                 /* newSuperior == entry being moved?, if so ==> ERROR */
338                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
339
340                 if ( op->oq_modrdn.rs_nnewSup->bv_len ) {
341                         if( (np = dn2entry_w( op->o_bd, np_ndn, NULL )) == NULL) {
342 #ifdef NEW_LOGGING
343                                 LDAP_LOG( BACK_LDBM, ERR, 
344                                         "ldbm_back_modrdn: newSup(ndn=%s) not found.\n", 
345                                         np_ndn->bv_val, 0, 0 );
346 #else
347                                 Debug( LDAP_DEBUG_TRACE,
348                                     "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
349                                     np_ndn->bv_val, 0, 0);
350 #endif
351
352                                 send_ldap_error( op, rs, LDAP_OTHER,
353                                         "newSuperior not found" );
354                                 goto return_results;
355                         }
356
357 #ifdef NEW_LOGGING
358                         LDAP_LOG( BACK_LDBM, DETAIL1,
359                                 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
360                                 (void *) np, np->e_id, 0 );
361 #else
362                         Debug( LDAP_DEBUG_TRACE,
363                                 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
364                                 (void *) np, np->e_id, 0 );
365 #endif
366
367                         /* check newSuperior for "children" acl */
368                         if ( !access_allowed( op, np, children, NULL,
369                                               ACL_WRITE, NULL ) )
370                         {
371 #ifdef NEW_LOGGING
372                                 LDAP_LOG( BACK_LDBM, INFO,
373                                    "ldbm_back_modrdn: no wr to newSup children.\n", 0, 0, 0 );
374 #else
375                                 Debug( LDAP_DEBUG_TRACE,
376                                        "ldbm_back_modrdn: no wr to newSup children\n",
377                                        0, 0, 0 );
378 #endif
379
380                                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
381                                 goto return_results;
382                         }
383
384                         if ( is_entry_alias( np ) ) {
385                                 /* parent is an alias, don't allow add */
386 #ifdef NEW_LOGGING
387                                 LDAP_LOG( BACK_LDBM, INFO,
388                                    "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn,0,0);
389 #else
390                                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
391 #endif
392
393
394                                 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
395                                     "newSuperior is an alias" );
396
397                                 goto return_results;
398                         }
399
400                         if ( is_entry_referral( np ) ) {
401                                 /* parent is a referral, don't allow add */
402 #ifdef NEW_LOGGING
403                                 LDAP_LOG( BACK_LDBM, INFO,
404                                         "ldbm_back_modrdn: entry (%s) is a referral\n",
405                                         np->e_dn, 0, 0 );
406 #else
407                                 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
408                                         np->e_dn, 0, 0 );
409 #endif
410
411                                 send_ldap_error( op, rs, LDAP_OTHER,
412                                     "newSuperior is a referral" );
413
414                                 goto return_results;
415                         }
416
417                 } else {
418
419                         /* no parent, must be root to modify newSuperior */
420                         if ( isroot == -1 ) {
421                                 isroot = be_isroot( op );
422                         }
423
424                         if ( ! isroot ) {
425                                 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op ) ) {
426                                         int     can_access;
427                                         np = (Entry *)&slap_entry_root;
428                                 
429                                         can_access = access_allowed( op, np,
430                                                         children, NULL, ACL_WRITE, NULL );
431                                         np = NULL;
432                                                                 
433                                         /* check parent for "children" acl */
434                                         if ( ! can_access ) {
435 #ifdef NEW_LOGGING
436                                                 LDAP_LOG( BACK_LDBM, ERR,
437                                                         "ldbm_back_modrdn: no access "
438                                                         "to new superior \"\"\n", 0, 0, 0 );
439 #else
440                                                 Debug( LDAP_DEBUG_TRACE,
441                                                         "<=- ldbm_back_modrdn: no "
442                                                         "access to new superior\n", 0, 0, 0 );
443 #endif
444
445                                                 send_ldap_error( op, rs,
446                                                         LDAP_INSUFFICIENT_ACCESS,
447                                                         NULL );
448                                                 goto return_results;
449                                         }
450
451                                 } else {
452 #ifdef NEW_LOGGING
453                                         LDAP_LOG( BACK_LDBM, ERR,
454                                                 "ldbm_back_modrdn: \"\" not allowed as new superior\n",
455                                                 0, 0, 0 );
456 #else
457                                         Debug( LDAP_DEBUG_TRACE,
458                                                 "<=- ldbm_back_modrdn: \"\" "
459                                                 "not allowed as new superior\n", 
460                                                 0, 0, 0);
461 #endif
462
463                                         send_ldap_error( op, rs,
464                                                 LDAP_INSUFFICIENT_ACCESS,
465                                                 NULL );
466                                         goto return_results;
467                                 }
468                         }
469                 }
470
471 #ifdef NEW_LOGGING
472                 LDAP_LOG( BACK_LDBM, DETAIL1,
473                         "ldbm_back_modrdn: wr to new parent's children OK.\n", 0, 0, 0 );
474 #else
475                 Debug( LDAP_DEBUG_TRACE,
476                     "ldbm_back_modrdn: wr to new parent's children OK\n",
477                     0, 0, 0 );
478 #endif
479
480                 new_parent_dn = op->oq_modrdn.rs_newSup;
481         }
482         
483         /* Build target dn and make sure target entry doesn't exist already. */
484         build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn, NULL ); 
485         dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
486
487 #ifdef NEW_LOGGING
488         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: new ndn=%s\n", 
489                 new_ndn.bv_val, 0, 0 );
490 #else
491         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
492             new_ndn.bv_val, 0, 0 );
493 #endif
494
495         /* check for abandon */
496         if ( op->o_abandon ) {
497                 goto return_results;
498         }
499
500         if ( ( rc_id = dn2id ( op->o_bd, &new_ndn, &id ) ) || id != NOID ) {
501                 /* if (rc_id) something bad happened to ldbm cache */
502                 rs->sr_err = rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
503                 send_ldap_result( op, rs );
504                 goto return_results;
505         }
506
507 #ifdef NEW_LOGGING
508         LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: new ndn (%s) does not exist\n",
509                 new_ndn.bv_val, 0, 0 );
510 #else
511         Debug( LDAP_DEBUG_TRACE,
512             "ldbm_back_modrdn: new ndn=%s does not exist\n",
513             new_ndn.bv_val, 0, 0 );
514 #endif
515
516         /* Get attribute type and attribute value of our new rdn, we will
517          * need to add that to our new entry
518          */
519         if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text,
520                 LDAP_DN_FORMAT_LDAP ) )
521         {
522 #ifdef NEW_LOGGING
523                 LDAP_LOG ( OPERATION, ERR, 
524                         "ldbm_back_modrdn: can't figure out "
525                         "type(s)/values(s) of newrdn\n", 
526                         0, 0, 0 );
527 #else
528                 Debug( LDAP_DEBUG_TRACE,
529                         "ldbm_back_modrdn: can't figure out "
530                         "type(s)/values(s) of newrdn\n", 
531                         0, 0, 0 );
532 #endif
533                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX,
534                                     "unknown type(s) used in RDN" );
535                 goto return_results;            
536         }
537
538 #ifdef NEW_LOGGING
539         LDAP_LOG ( OPERATION, RESULTS, 
540                 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
541                 "new_rdn_val=\"%s\"\n",
542                 new_rdn[ 0 ]->la_attr.bv_val, 
543                 new_rdn[ 0 ]->la_value.bv_val, 0 );
544 #else
545         Debug( LDAP_DEBUG_TRACE,
546                 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
547                 "new_rdn_val=\"%s\"\n",
548                 new_rdn[ 0 ]->la_attr.bv_val,
549                 new_rdn[ 0 ]->la_value.bv_val, 0 );
550 #endif
551
552         if ( op->oq_modrdn.rs_deleteoldrdn ) {
553                 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
554                         LDAP_DN_FORMAT_LDAP ) )
555                 {
556 #ifdef NEW_LOGGING
557                         LDAP_LOG ( OPERATION, ERR, 
558                                 "ldbm_back_modrdn: can't figure out "
559                                 "type(s)/values(s) of old_rdn\n", 
560                                 0, 0, 0 );
561 #else
562                         Debug( LDAP_DEBUG_TRACE,
563                                 "ldbm_back_modrdn: can't figure out "
564                                 "the old_rdn type(s)/value(s)\n", 
565                                 0, 0, 0 );
566 #endif
567                         send_ldap_error( op, rs, LDAP_OTHER,
568                                     "cannot parse RDN from old DN" );
569                         goto return_results;            
570                 }
571         }
572
573 #ifdef NEW_LOGGING
574         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn:  DN_X500\n", 0, 0, 0 );
575 #else
576         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
577                0, 0, 0 );
578 #endif
579         
580         if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
581                 send_ldap_result( op, rs );
582                 goto return_results;
583         }
584
585         /* check for abandon */
586         if ( op->o_abandon ) {
587                 goto return_results;
588         }
589
590         (void) cache_delete_entry( &li->li_cache, e );
591
592         free( e->e_dn );
593         old_ndn = e->e_nname;
594         e->e_name = new_dn;
595         e->e_nname = new_ndn;
596         new_dn.bv_val = NULL;
597         new_ndn.bv_val = NULL;
598
599         /* NOTE: after this you must not free new_dn or new_ndn!
600          * They are used by cache.
601          */
602
603         /* modify memory copy of entry */
604         rs->sr_err = ldbm_modify_internal( op, &mod[0], e,
605                 &rs->sr_text, textbuf, textlen );
606         switch ( rs->sr_err ) {
607         case LDAP_SUCCESS:
608                 break;
609
610         default:
611                 send_ldap_result( op, rs );
612                 /* FALLTHRU */
613         case SLAPD_ABANDON:
614                 goto return_results;
615         }
616         
617         /*
618          * NOTE: the backend MUST delete then add the entry,
619          *              otherwise indexing may get hosed
620          * FIXME: if a new ID was used, the add could be done first.
621          *              that would be safer.
622          */
623
624         /* delete old one */
625         if ( dn2id_delete( op->o_bd, &old_ndn, e->e_id ) != 0 ) {
626                 send_ldap_error( op, rs, LDAP_OTHER,
627                         "DN index delete fail" );
628                 goto return_results;
629         }
630
631         /* add new one */
632         if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
633                 /* try to repair old entry - probably hopeless */
634         if( dn2id_add( op->o_bd, &old_ndn, e->e_id) != 0 ) {
635                         send_ldap_error( op, rs, LDAP_OTHER,
636                                 "DN index add and repair failed" );
637                 } else {
638                         send_ldap_error( op, rs, LDAP_OTHER,
639                                 "DN index add failed" );
640                 }
641                 goto return_results;
642         }
643
644         /* id2entry index */
645         if ( id2entry_add( op->o_bd, e ) != 0 ) {
646                 /* Try to undo */
647                 int rc;
648                 rc = dn2id_delete( op->o_bd, &e->e_nname, e->e_id );
649                 rc |= dn2id_add( op->o_bd, &old_ndn, e->e_id );
650                 if( rc ) {
651                         send_ldap_error( op, rs, LDAP_OTHER,
652                                 "entry update and repair failed" );
653                 } else {
654                         send_ldap_error( op, rs, LDAP_OTHER,
655                                 "entry update failed" );
656                 }
657                 goto return_results;
658         }
659
660         (void) cache_update_entry( &li->li_cache, e );
661
662         rs->sr_err = LDAP_SUCCESS;
663         rs->sr_text = NULL;
664         send_ldap_result( op, rs );
665         cache_entry_commit( e );
666
667 return_results:
668         if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
669         if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
670         if( old_ndn.bv_val != NULL ) free( old_ndn.bv_val );
671
672         /* LDAP v2 supporting correct attribute handling. */
673         if ( new_rdn != NULL ) {
674                 ldap_rdnfree( new_rdn );
675         }
676         if ( old_rdn != NULL ) {
677                 ldap_rdnfree( old_rdn );
678         }
679         if ( mod != NULL ) {
680                 Modifications *tmp;
681                 for (; mod; mod = tmp ) {
682                         /* slap_modrdn2mods does things one way,
683                          * slap_mods_opattrs does it differently
684                          */
685                         if ( mod->sml_op != SLAP_MOD_SOFTADD &&
686                                 mod->sml_op != LDAP_MOD_DELETE ) break;
687                         if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
688                         tmp = mod->sml_next;
689                         free( mod );
690                 }
691                 slap_mods_free( mod );
692         }
693
694         /* LDAP v3 Support */
695         if( np != NULL ) {
696                 /* free new parent and writer lock */
697                 cache_return_entry_w( &li->li_cache, np );
698         }
699
700         if( p != NULL ) {
701                 /* free parent and writer lock */
702                 cache_return_entry_w( &li->li_cache, p );
703         }
704
705         /* free entry and writer lock */
706         cache_return_entry_w( &li->li_cache, e );
707         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
708         rs->sr_text = NULL;
709         return( rs->sr_err );
710 }