]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
Support multiple sync replication at the consumer :
[openldap] / servers / slapd / back-ldbm / modrdn.c
1 /* modrdn.c - ldbm backend modrdn routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 /*
9  * LDAP v3 newSuperior support. Add new rdn as an attribute.
10  * (Full support for v2 also used software/ideas contributed
11  * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
12  * submission!.)
13  *
14  * Copyright 1999, Juan C. Gomez, All rights reserved.
15  * This software is not subject to any license of Silicon Graphics 
16  * Inc. or Purdue University.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * without restriction or fee of any kind as long as this notice
20  * is preserved.
21  *
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32 #include "back-ldbm.h"
33 #include "proto-back-ldbm.h"
34
35 int
36 ldbm_back_modrdn(
37     Operation   *op,
38     SlapReply   *rs )
39 {
40         AttributeDescription *children = slap_schema.si_ad_children;
41         AttributeDescription *entry = slap_schema.si_ad_entry;
42         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
43         struct berval   p_dn, p_ndn;
44         struct berval   new_dn = { 0, NULL}, new_ndn = { 0, NULL };
45         Entry           *e, *p = NULL;
46         Entry           *matched;
47         /* LDAP v2 supporting correct attribute handling. */
48         LDAPRDN         new_rdn = NULL;
49         LDAPRDN         old_rdn = NULL;
50         int             isroot = -1;
51 #define CAN_ROLLBACK    -1
52 #define MUST_DESTROY    1
53         int             rc = CAN_ROLLBACK;
54         int             rc_id = 0;
55         ID              id = NOID;
56         const char      *text = NULL;
57         char            textbuf[SLAP_TEXT_BUFLEN];
58         size_t          textlen = sizeof textbuf;
59         /* Added to support newSuperior */ 
60         Entry           *np = NULL;     /* newSuperior Entry */
61         struct berval   *np_ndn = NULL; /* newSuperior ndn */
62         struct berval   *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
63         /* Used to interface with ldbm_modify_internal() */
64         Modifications   *mod = NULL;            /* Used to delete old/add new rdn */
65         int             manageDSAit = get_manageDSAit( op );
66
67 #ifdef NEW_LOGGING
68         LDAP_LOG( BACK_LDBM, ENTRY, 
69                 "ldbm_back_modrdn: dn: %s newSuperior=%s\n", 
70                 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
71                 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len ) ? op->oq_modrdn.rs_newSup->bv_val : "NULL",0 );
72 #else
73         Debug( LDAP_DEBUG_TRACE,
74                 "==>ldbm_back_modrdn: dn: %s newSuperior=%s\n", 
75                 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
76                 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len )
77                         ? op->oq_modrdn.rs_newSup->bv_val : "NULL", 0 );
78 #endif
79
80         /* grab giant lock for writing */
81         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
82
83         e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
84
85         /* get entry with writer lock */
86         /* FIXME: dn2entry() should return non-glue entry */
87         if (( e == NULL  ) || ( !manageDSAit && e && is_entry_glue( e ))) {
88                 if ( matched != NULL ) {
89                         rs->sr_matched = strdup( matched->e_dn );
90                         rs->sr_ref = is_entry_referral( matched )
91                                 ? get_entry_referrals( op, matched )
92                                 : NULL;
93                         cache_return_entry_r( &li->li_cache, matched );
94                 } else {
95                         BerVarray deref = NULL;
96                         if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
97                                 syncinfo_t *si;
98                                 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
99                                         struct berval tmpbv;
100                                         ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
101                                         ber_bvarray_add( &deref, &tmpbv );
102                                 }
103                         } else {
104                                 deref = default_referral;
105                         }
106                         rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
107                                                         LDAP_SCOPE_DEFAULT );
108                 }
109
110                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
111
112                 rs->sr_err = LDAP_REFERRAL;
113                 send_ldap_result( op, rs );
114
115                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
116                 free( (char *)rs->sr_matched );
117                 rs->sr_ref = NULL;
118                 rs->sr_matched = NULL;
119                 return rs->sr_err;
120         }
121
122         /* check entry for "entry" acl */
123         if ( ! access_allowed( op, e,
124                 entry, NULL, ACL_WRITE, NULL ) )
125         {
126 #ifdef NEW_LOGGING
127                 LDAP_LOG( BACK_LDBM, ERR, 
128                         "ldbm_back_modrdn: no write access to entry of (%s)\n", 
129                         op->o_req_dn.bv_val, 0, 0 );
130 #else
131                 Debug( LDAP_DEBUG_TRACE,
132                         "<=- ldbm_back_modrdn: no write access to entry\n", 0,
133                         0, 0 );
134 #endif
135
136                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
137                         "no write access to entry" );
138
139                 goto return_results;
140         }
141
142         if (!manageDSAit && is_entry_referral( e ) ) {
143                 /* parent is a referral, don't allow add */
144                 /* parent is an alias, don't allow add */
145                 rs->sr_ref = get_entry_referrals( op, e );
146
147 #ifdef NEW_LOGGING
148                 LDAP_LOG( BACK_LDBM, INFO, 
149                         "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn, 0, 0 );
150 #else
151                 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
152                     0, 0 );
153 #endif
154
155                 rs->sr_err = LDAP_REFERRAL;
156                 rs->sr_matched = e->e_name.bv_val;
157                 send_ldap_result( op, rs );
158
159                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
160                 rs->sr_ref = NULL;
161                 rs->sr_matched = NULL;
162                 goto return_results;
163         }
164
165         if ( has_children( op->o_bd, e ) ) {
166 #ifdef NEW_LOGGING
167                 LDAP_LOG( BACK_LDBM, INFO, 
168                         "ldbm_back_modrdn: entry %s has children\n", e->e_dn, 0, 0 );
169 #else
170                 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
171                     0, 0 );
172 #endif
173
174                 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
175                     "subtree rename not supported" );
176                 goto return_results;
177         }
178
179         if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
180                 p_ndn = slap_empty_bv ;
181         } else {
182                 dnParent( &e->e_nname, &p_ndn );
183         }
184
185         if ( p_ndn.bv_len != 0 ) {
186                 /* Make sure parent entry exist and we can write its 
187                  * children.
188                  */
189
190                 if( (p = dn2entry_w( op->o_bd, &p_ndn, NULL )) == NULL) {
191 #ifdef NEW_LOGGING
192                         LDAP_LOG( BACK_LDBM, INFO, 
193                                 "ldbm_back_modrdn: parent of %s does not exist\n", 
194                                 e->e_ndn, 0, 0 );
195 #else
196                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
197                                 0, 0, 0);
198 #endif
199
200                         send_ldap_error( op, rs, LDAP_OTHER,
201                                 "parent entry does not exist" );
202
203                         goto return_results;
204                 }
205
206                 /* check parent for "children" acl */
207                 if ( ! access_allowed( op, p,
208                         children, NULL, ACL_WRITE, NULL ) )
209                 {
210 #ifdef NEW_LOGGING
211                         LDAP_LOG( BACK_LDBM, INFO, 
212                                 "ldbm_back_modrdn: no access to parent of (%s)\n", 
213                                 e->e_dn, 0, 0 );
214 #else
215                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
216                                 0, 0 );
217 #endif
218
219                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
220                                 NULL );
221                         goto return_results;
222                 }
223
224 #ifdef NEW_LOGGING
225                 LDAP_LOG( BACK_LDBM, DETAIL1, 
226                         "ldbm_back_modrdn: wr to children of entry %s OK\n", 
227                         p_ndn.bv_val, 0, 0 );
228 #else
229                 Debug( LDAP_DEBUG_TRACE,
230                        "ldbm_back_modrdn: wr to children of entry %s OK\n",
231                        p_ndn.bv_val, 0, 0 );
232 #endif
233
234                 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
235                         p_dn = slap_empty_bv;
236                 } else {
237                         dnParent( &e->e_name, &p_dn );
238                 }
239
240 #ifdef NEW_LOGGING
241                 LDAP_LOG( BACK_LDBM, DETAIL1, 
242                            "ldbm_back_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
243 #else
244                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
245                        p_dn.bv_val, 0, 0 );
246 #endif
247
248         } else {
249                 /* no parent, must be root to modify rdn */
250                 isroot = be_isroot( op->o_bd, &op->o_ndn );
251                 if ( ! isroot ) {
252                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
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->o_bd, &op->o_ndn );
423                         }
424
425                         if ( ! isroot ) {
426                                 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
427                                         int     can_access;
428                                         np = (Entry *)&slap_entry_root;
429                                 
430                                         can_access = access_allowed( op, np,
431                                                         children, NULL, ACL_WRITE, NULL );
432                                         np = NULL;
433                                                                 
434                                         /* check parent for "children" acl */
435                                         if ( ! can_access ) {
436 #ifdef NEW_LOGGING
437                                                 LDAP_LOG( BACK_LDBM, ERR,
438                                                         "ldbm_back_modrdn: no access "
439                                                         "to new superior \"\"\n", 0, 0, 0 );
440 #else
441                                                 Debug( LDAP_DEBUG_TRACE,
442                                                         "<=- ldbm_back_modrdn: no "
443                                                         "access to new superior\n", 0, 0, 0 );
444 #endif
445
446                                                 send_ldap_error( op, rs,
447                                                         LDAP_INSUFFICIENT_ACCESS,
448                                                         NULL );
449                                                 goto return_results;
450                                         }
451
452                                 } else {
453 #ifdef NEW_LOGGING
454                                         LDAP_LOG( BACK_LDBM, ERR,
455                                                 "ldbm_back_modrdn: \"\" not allowed as new superior\n",
456                                                 0, 0, 0 );
457 #else
458                                         Debug( LDAP_DEBUG_TRACE,
459                                                 "<=- ldbm_back_modrdn: \"\" "
460                                                 "not allowed as new superior\n", 
461                                                 0, 0, 0);
462 #endif
463
464                                         send_ldap_error( op, rs,
465                                                 LDAP_INSUFFICIENT_ACCESS,
466                                                 NULL );
467                                         goto return_results;
468                                 }
469                         }
470                 }
471
472 #ifdef NEW_LOGGING
473                 LDAP_LOG( BACK_LDBM, DETAIL1,
474                         "ldbm_back_modrdn: wr to new parent's children OK.\n", 0, 0, 0 );
475 #else
476                 Debug( LDAP_DEBUG_TRACE,
477                     "ldbm_back_modrdn: wr to new parent's children OK\n",
478                     0, 0, 0 );
479 #endif
480
481                 new_parent_dn = op->oq_modrdn.rs_newSup;
482         }
483         
484         /* Build target dn and make sure target entry doesn't exist already. */
485         build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn, NULL ); 
486         dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
487
488 #ifdef NEW_LOGGING
489         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: new ndn=%s\n", 
490                 new_ndn.bv_val, 0, 0 );
491 #else
492         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
493             new_ndn.bv_val, 0, 0 );
494 #endif
495
496         /* check for abandon */
497         if ( op->o_abandon ) {
498                 goto return_results;
499         }
500
501         if ( ( rc_id = dn2id ( op->o_bd, &new_ndn, &id ) ) || id != NOID ) {
502                 /* if (rc_id) something bad happened to ldbm cache */
503                 rs->sr_err = rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
504                 send_ldap_result( op, rs );
505                 goto return_results;
506         }
507
508 #ifdef NEW_LOGGING
509         LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: new ndn (%s) does not exist\n",
510                 new_ndn.bv_val, 0, 0 );
511 #else
512         Debug( LDAP_DEBUG_TRACE,
513             "ldbm_back_modrdn: new ndn=%s does not exist\n",
514             new_ndn.bv_val, 0, 0 );
515 #endif
516
517         /* Get attribute type and attribute value of our new rdn, we will
518          * need to add that to our new entry
519          */
520         if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text,
521                 LDAP_DN_FORMAT_LDAP ) )
522         {
523 #ifdef NEW_LOGGING
524                 LDAP_LOG ( OPERATION, ERR, 
525                         "ldbm_back_modrdn: can't figure out "
526                         "type(s)/values(s) of newrdn\n", 
527                         0, 0, 0 );
528 #else
529                 Debug( LDAP_DEBUG_TRACE,
530                         "ldbm_back_modrdn: can't figure out "
531                         "type(s)/values(s) of newrdn\n", 
532                         0, 0, 0 );
533 #endif
534                 goto return_results;            
535         }
536
537 #ifdef NEW_LOGGING
538         LDAP_LOG ( OPERATION, RESULTS, 
539                 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
540                 "new_rdn_val=\"%s\"\n",
541                 new_rdn[ 0 ]->la_attr.bv_val, 
542                 new_rdn[ 0 ]->la_value.bv_val, 0 );
543 #else
544         Debug( LDAP_DEBUG_TRACE,
545                 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
546                 "new_rdn_val=\"%s\"\n",
547                 new_rdn[ 0 ]->la_attr.bv_val,
548                 new_rdn[ 0 ]->la_value.bv_val, 0 );
549 #endif
550
551         if ( op->oq_modrdn.rs_deleteoldrdn ) {
552                 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
553                         LDAP_DN_FORMAT_LDAP ) )
554                 {
555 #ifdef NEW_LOGGING
556                         LDAP_LOG ( OPERATION, ERR, 
557                                 "ldbm_back_modrdn: can't figure out "
558                                 "type(s)/values(s) of old_rdn\n", 
559                                 0, 0, 0 );
560 #else
561                         Debug( LDAP_DEBUG_TRACE,
562                                 "ldbm_back_modrdn: can't figure out "
563                                 "the old_rdn type(s)/value(s)\n", 
564                                 0, 0, 0 );
565 #endif
566                         goto return_results;            
567                 }
568         }
569
570 #ifdef NEW_LOGGING
571         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn:  DN_X500\n", 0, 0, 0 );
572 #else
573         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
574                0, 0, 0 );
575 #endif
576         
577         if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
578                 goto return_results;
579         }
580
581
582         /* check for abandon */
583         if ( op->o_abandon ) {
584                 goto return_results;
585         }
586
587         /* delete old one */
588         if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
589                 send_ldap_error( op, rs, LDAP_OTHER,
590                         "DN index delete fail" );
591                 goto return_results;
592         }
593
594         (void) cache_delete_entry( &li->li_cache, e );
595         rc = MUST_DESTROY;
596
597         /* XXX: there is no going back! */
598
599         free( e->e_dn );
600         free( e->e_ndn );
601         e->e_name = new_dn;
602         e->e_nname = new_ndn;
603         new_dn.bv_val = NULL;
604         new_ndn.bv_val = NULL;
605
606         /* NOTE: after this you must not free new_dn or new_ndn!
607          * They are used by cache.
608          */
609
610         /* add new one */
611         if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
612                 send_ldap_error( op, rs, LDAP_OTHER,
613                         "DN index add failed" );
614                 goto return_results;
615         }
616
617         /* modify memory copy of entry */
618         rc_id = ldbm_modify_internal( op, &mod[0], e,
619                 &rs->sr_text, textbuf, textlen );
620         switch ( rc_id ) {
621         case LDAP_SUCCESS:
622                 break;
623
624         case SLAPD_ABANDON:
625                 /* too late ... */
626                 rs->sr_err = rc_id;
627                 send_ldap_result( op, rs );
628                 goto return_results;
629         
630         default:
631                 /* here we may try to delete the newly added dn */
632                 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
633                         /* we already are in trouble ... */
634                         ;
635                 }
636                 goto return_results;
637         }
638         
639         (void) cache_update_entry( &li->li_cache, e );
640
641         /* id2entry index */
642         if ( id2entry_add( op->o_bd, e ) != 0 ) {
643                 send_ldap_error( op, rs, LDAP_OTHER,
644                         "entry update failed" );
645                 goto return_results;
646         }
647
648         rs->sr_err = LDAP_SUCCESS;
649         rs->sr_text = NULL;
650         send_ldap_result( op, rs );
651         rc = 0;
652         cache_entry_commit( e );
653
654 return_results:
655         if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
656         if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
657
658         /* LDAP v2 supporting correct attribute handling. */
659         if ( new_rdn != NULL ) {
660                 ldap_rdnfree( new_rdn );
661         }
662         if ( old_rdn != NULL ) {
663                 ldap_rdnfree( old_rdn );
664         }
665         if ( mod != NULL ) {
666                 Modifications *tmp;
667                 for (; mod; mod = tmp ) {
668                         /* slap_modrdn2mods does things one way,
669                          * slap_mods_opattrs does it differently
670                          */
671                         if ( mod->sml_op != SLAP_MOD_SOFTADD &&
672                                 mod->sml_op != LDAP_MOD_DELETE ) break;
673                         if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
674                         tmp = mod->sml_next;
675                         free( mod );
676                 }
677                 slap_mods_free( mod );
678         }
679
680         /* LDAP v3 Support */
681         if( np != NULL ) {
682                 /* free new parent and writer lock */
683                 cache_return_entry_w( &li->li_cache, np );
684         }
685
686         if( p != NULL ) {
687                 /* free parent and writer lock */
688                 cache_return_entry_w( &li->li_cache, p );
689         }
690
691         /* free entry and writer lock */
692         cache_return_entry_w( &li->li_cache, e );
693         if ( rc == MUST_DESTROY ) {
694                 /* if rc == MUST_DESTROY the entry is uncached 
695                  * and its private data is destroyed; 
696                  * the entry must be freed */
697                 entry_free( e );
698         }
699         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
700         rs->sr_text = NULL;
701         return( rc );
702 }