]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
9df746d9b4c95c8992f6b9661793fe79a8a16098
[openldap] / servers / slapd / back-ldbm / modrdn.c
1 /* modrdn.c - ldbm backend modrdn routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 /*
9  * LDAP v3 newSuperior support. Add new rdn as an attribute.
10  * (Full support for v2 also used software/ideas contributed
11  * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
12  * submission!.)
13  *
14  * Copyright 1999, Juan C. Gomez, All rights reserved.
15  * This software is not subject to any license of Silicon Graphics 
16  * Inc. or Purdue University.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * without restriction or fee of any kind as long as this notice
20  * is preserved.
21  *
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32 #include "back-ldbm.h"
33 #include "proto-back-ldbm.h"
34
35 int
36 ldbm_back_modrdn(
37     Backend     *be,
38     Connection  *conn,
39     Operation   *op,
40     const char  *dn,
41     const char  *ndn,
42     const char  *newrdn,
43     int         deleteoldrdn,
44     const char  *newSuperior
45 )
46 {
47         AttributeDescription *children = slap_schema.si_ad_children;
48         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
49         char            *p_dn = NULL, *p_ndn = NULL;
50         char            *new_dn = NULL, *new_ndn = NULL;
51         Entry           *e, *p = NULL;
52         Entry           *matched;
53         int             isroot = -1;
54         int             rootlock = 0;
55 #define CAN_ROLLBACK    -1
56 #define MUST_DESTROY    1
57         int             rc = CAN_ROLLBACK;
58         int             rc_id = 0;
59         ID              id = NOID;
60         const char *text = NULL;
61         char textbuf[SLAP_TEXT_BUFLEN];
62         size_t textlen = sizeof textbuf;
63         /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
64         char            **new_rdn_vals = NULL;  /* Vals of new rdn */
65         char            **new_rdn_types = NULL; /* Types of new rdn */
66         int             a_cnt, d_cnt;
67         char            *old_rdn = NULL;        /* Old rdn's attr type & val */
68         char            **old_rdn_types = NULL; /* Types of old rdn attrs. */
69         char            **old_rdn_vals = NULL;  /* Old rdn attribute values */
70         /* Added to support newSuperior */ 
71         Entry           *np = NULL;     /* newSuperior Entry */
72         char            *np_dn = NULL;  /* newSuperior dn */
73         char            *np_ndn = NULL; /* newSuperior ndn */
74         char            *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
75         /* Used to interface with ldbm_modify_internal() */
76         Modifications   *mod = NULL;            /* Used to delete old/add new rdn */
77         int             manageDSAit = get_manageDSAit( op );
78
79         Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
80                (newSuperior ? newSuperior : "NULL"),
81                0, 0 );
82
83         /* grab giant lock for writing */
84         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
85
86         /* get entry with writer lock */
87         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
88                 char* matched_dn = NULL;
89                 struct berval** refs = NULL;
90
91                 if( matched != NULL ) {
92                         matched_dn = strdup( matched->e_dn );
93                         refs = is_entry_referral( matched )
94                                 ? get_entry_referrals( be, conn, op, matched )
95                                 : NULL;
96                         cache_return_entry_r( &li->li_cache, matched );
97                 } else {
98                         refs = default_referral;
99                 }
100
101                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
102
103                 send_ldap_result( conn, op, LDAP_REFERRAL,
104                         matched_dn, NULL, refs, NULL );
105
106                 if ( matched != NULL ) {
107                         ber_bvecfree( refs );
108                         free( matched_dn );
109                 }
110
111                 return( -1 );
112         }
113
114         if (!manageDSAit && is_entry_referral( e ) ) {
115                 /* parent is a referral, don't allow add */
116                 /* parent is an alias, don't allow add */
117                 struct berval **refs = get_entry_referrals( be,
118                         conn, op, e );
119
120                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
121                     0, 0 );
122
123                 send_ldap_result( conn, op, LDAP_REFERRAL,
124                     e->e_dn, NULL, refs, NULL );
125
126                 ber_bvecfree( refs );
127                 goto return_results;
128         }
129
130         if ( has_children( be, e ) ) {
131                 Debug( LDAP_DEBUG_TRACE, "entry %s referral\n", e->e_dn,
132                     0, 0 );
133
134                 send_ldap_result( conn, op, LDAP_NOT_ALLOWED_ON_NONLEAF,
135                     NULL, "subtree rename not supported", NULL, NULL );
136                 goto return_results;
137         }
138
139         if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL && p_ndn[0] != '\0' ) {
140                 /* Make sure parent entry exist and we can write its 
141                  * children.
142                  */
143
144                 if( (p = dn2entry_w( be, p_ndn, NULL )) == NULL) {
145                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
146                                 0, 0, 0);
147
148                         send_ldap_result( conn, op, LDAP_OTHER,
149                                 NULL, "parent entry does not exist", NULL, NULL );
150
151                         goto return_results;
152                 }
153
154                 /* check parent for "children" acl */
155                 if ( ! access_allowed( be, conn, op, p,
156                         children, NULL, ACL_WRITE ) )
157                 {
158                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
159                                 0, 0 );
160
161                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
162                                 NULL, NULL, NULL, NULL );
163                         goto return_results;
164                 }
165
166                 Debug( LDAP_DEBUG_TRACE,
167                        "ldbm_back_modrdn: wr to children of entry %s OK\n",
168                        p_ndn, 0, 0 );
169
170                 p_dn = dn_parent( be, e->e_dn );
171
172                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
173                        p_dn, 0, 0 );
174
175         } else {
176                 /* no parent, must be root to modify rdn */
177                 isroot = be_isroot( be, op->o_ndn );
178                 if ( ! isroot ) {
179                         if ( be_issuffix( be, "" )
180                                         || be_isupdate( be, op->o_ndn ) ) {
181                                 static const Entry rootp = { NOID, "", "", NULL, NULL };
182                                 p = (Entry *)&rootp;
183                                 
184                                 rc = access_allowed( be, conn, op, p,
185                                                 children, NULL, ACL_WRITE );
186                                 p = NULL;
187                                                                 
188                                 /* check parent for "children" acl */
189                                 if ( ! rc ) {
190                                         Debug( LDAP_DEBUG_TRACE,
191                                                 "<=- ldbm_back_modrdn: no "
192                                                 "access to parent\n", 0, 0, 0 );
193
194                                         send_ldap_result( conn, op, 
195                                                 LDAP_INSUFFICIENT_ACCESS,
196                                                 NULL, NULL, NULL, NULL );
197                                         goto return_results;
198                                 }
199
200                         } else {
201                                 Debug( LDAP_DEBUG_TRACE,
202                                         "<=- ldbm_back_modrdn: no parent & "
203                                         "not root\n", 0, 0, 0);
204
205                                 send_ldap_result( conn, op, 
206                                         LDAP_INSUFFICIENT_ACCESS,
207                                         NULL, NULL, NULL, NULL );
208                                 goto return_results;
209                         }
210                 }
211
212                 Debug( LDAP_DEBUG_TRACE,
213                        "ldbm_back_modrdn: no parent, locked root\n",
214                        0, 0, 0 );
215         }
216
217         new_parent_dn = p_dn;   /* New Parent unless newSuperior given */
218
219         if ( newSuperior != NULL ) {
220                 Debug( LDAP_DEBUG_TRACE, 
221                         "ldbm_back_modrdn: new parent \"%s\" requested...\n",
222                         newSuperior, 0, 0 );
223
224                 np_dn = ch_strdup( newSuperior );
225                 np_ndn = ch_strdup( np_dn );
226                 (void) dn_normalize( np_ndn );
227
228                 /* newSuperior == oldParent? */
229                 if ( strcmp( p_ndn, np_ndn ) == 0 ) {
230                         Debug( LDAP_DEBUG_TRACE, 
231                                "ldbm_back_modrdn: new parent \"%s\" seems to be the same as old parent \"%s\"...\n",
232                                newSuperior, p_dn, 0 );
233
234                         newSuperior = NULL; /* ignore newSuperior */
235                 }
236         }
237
238         if ( newSuperior != NULL ) {
239                 /* newSuperior == entry being moved?, if so ==> ERROR */
240                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
241
242                 if ( newSuperior[ 0 ] != '\0' ) {
243
244                         if( (np = dn2entry_w( be, np_ndn, NULL )) == NULL) {
245                                 Debug( LDAP_DEBUG_TRACE,
246                                        "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
247                                        np_ndn, 0, 0);
248
249                                 send_ldap_result( conn, op, LDAP_OTHER,
250                                         NULL, "newSuperior not found", NULL, NULL );
251                                 goto return_results;
252                         }
253
254                         Debug( LDAP_DEBUG_TRACE,
255                                "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
256                                np, np->e_id, 0 );
257
258                         /* check newSuperior for "children" acl */
259                         if ( !access_allowed( be, conn, op, np, children, NULL,
260                                               ACL_WRITE ) )
261                         {
262                                 Debug( LDAP_DEBUG_TRACE,
263                                        "ldbm_back_modrdn: no wr to newSup children\n",
264                                        0, 0, 0 );
265
266                                 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
267                                         NULL, NULL, NULL, NULL );
268                                 goto return_results;
269                         }
270
271                         if ( is_entry_alias( np ) ) {
272                                 /* parent is an alias, don't allow add */
273                                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
274
275                                 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
276                                     NULL, "newSuperior is an alias", NULL, NULL );
277
278                                 goto return_results;
279                         }
280
281                         if ( is_entry_referral( np ) ) {
282                                 /* parent is a referral, don't allow add */
283                                 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
284                                         np->e_dn, 0, 0 );
285
286                                 send_ldap_result( conn, op, LDAP_OTHER,
287                                     NULL, "newSuperior is a referral", NULL, NULL );
288
289                                 goto return_results;
290                         }
291
292                 } else {
293
294                         /* no parent, must be root to modify newSuperior */
295                         if ( isroot == -1 ) {
296                                 isroot = be_isroot( be, op->o_ndn );
297                         }
298
299                         if ( ! isroot ) {
300                                 if ( be_issuffix( be, "" )
301                                                 || be_isupdate( be, op->o_ndn ) ) {
302                                         static const Entry rootp = { NOID, "", "", NULL, NULL };
303                                         np = (Entry *)&rootp;
304                                 
305                                         rc = access_allowed( be, conn, op, np,
306                                                         children, NULL, ACL_WRITE );
307                                         np = NULL;
308                                                                 
309                                         /* check parent for "children" acl */
310                                         if ( ! rc ) {
311                                                 Debug( LDAP_DEBUG_TRACE,
312                                                         "<=- ldbm_back_modrdn: no "
313                                                         "access to new superior\n", 0, 0, 0 );
314
315                                                 send_ldap_result( conn, op, 
316                                                         LDAP_INSUFFICIENT_ACCESS,
317                                                         NULL, NULL, NULL, NULL );
318                                                 goto return_results;
319                                         }
320
321                                 } else {
322                                         Debug( LDAP_DEBUG_TRACE,
323                                                 "<=- ldbm_back_modrdn: \"\" "
324                                                 "not allowed as new superior\n", 
325                                                 0, 0, 0);
326
327                                         send_ldap_result( conn, op, 
328                                                 LDAP_INSUFFICIENT_ACCESS,
329                                                 NULL, NULL, NULL, NULL );
330                                         goto return_results;
331                                 }
332                         }
333                 }
334
335                 Debug( LDAP_DEBUG_TRACE,
336                        "ldbm_back_modrdn: wr to new parent's children OK\n",
337                        0, 0 , 0 );
338
339                 new_parent_dn = np_dn;
340         }
341         
342         /* Build target dn and make sure target entry doesn't exist already. */
343         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
344
345         new_ndn = ch_strdup(new_dn);
346         (void) dn_normalize( new_ndn );
347
348         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
349                new_ndn, 0, 0 );
350
351         /* check for abandon */
352         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
353         if ( op->o_abandon ) {
354                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
355                 goto return_results;
356         }
357
358         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
359         if ( ( rc_id = dn2id ( be, new_ndn, &id ) ) || id != NOID ) {
360                 /* if (rc_id) something bad happened to ldbm cache */
361                 send_ldap_result( conn, op, 
362                         rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS,
363                         NULL, NULL, NULL, NULL );
364                 goto return_results;
365         }
366
367         Debug( LDAP_DEBUG_TRACE,
368                "ldbm_back_modrdn: new ndn=%s does not exist\n",
369                new_ndn, 0, 0 );
370
371
372         /* Get attribute types and values of our new rdn, we will
373          * need to add that to our new entry
374          */
375         if ( rdn_attrs( newrdn, &new_rdn_types, &new_rdn_vals ) ) {
376                 Debug( LDAP_DEBUG_TRACE,
377                        "ldbm_back_modrdn: can't figure out type(s)/value(s) of newrdn\n",
378                        0, 0, 0 );
379
380                 send_ldap_result( conn, op, LDAP_INVALID_DN,
381                         NULL, "unable to parse type(s)/value(s) used in RDN", NULL, NULL );
382                 goto return_results;            
383         }
384
385         Debug( LDAP_DEBUG_TRACE,
386                "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
387                new_rdn_vals[0], new_rdn_types[0], 0 );
388
389         /* Retrieve the old rdn from the entry's dn */
390         if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
391                 Debug( LDAP_DEBUG_TRACE,
392                        "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
393                        0, 0, 0 );
394
395                 send_ldap_result( conn, op, LDAP_OTHER,
396                         NULL, "could not parse old DN", NULL, NULL );
397                 goto return_results;            
398         }
399
400         if ( rdn_attrs( old_rdn, &old_rdn_types, &old_rdn_vals ) ) {
401                 Debug( LDAP_DEBUG_TRACE,
402                        "ldbm_back_modrdn: can't figure out the old_rdn type(s)/value(s)\n",
403                        0, 0, 0 );
404
405                 send_ldap_result( conn, op, LDAP_OTHER,
406                         NULL, "unable to parse type(s)/value(s) used in RDN from old DN", NULL, NULL );
407                 goto return_results;            
408         }
409         
410         if ( newSuperior == NULL
411                 && charray_strcasecmp( (const char **)old_rdn_types, (const char **)new_rdn_types ) != 0 )
412         {
413             /* Not a big deal but we may say something */
414             Debug( LDAP_DEBUG_TRACE,
415                    "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
416                    old_rdn_types[0], new_rdn_types[0], 0 );
417         }               
418
419         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
420                0, 0, 0 );
421
422         mod = NULL;
423         for ( a_cnt = 0; new_rdn_types[a_cnt]; a_cnt++ ) {
424                 int rc;
425                 Modifications *mod_tmp;
426                 struct berval val;
427
428                 mod_tmp = (Modifications *)ch_malloc( sizeof( Modifications ) );
429
430                 mod_tmp->sml_desc = NULL;
431                 rc = slap_str2ad( new_rdn_types[a_cnt], 
432                                 &mod_tmp->sml_desc, &text );
433
434                 if ( rc != LDAP_SUCCESS ) {
435                         Debug( LDAP_DEBUG_TRACE,
436                                 "ldbm_back_modrdn: %s: %s (new)\n",
437                                 text, new_rdn_types[a_cnt], 0 );
438
439                         send_ldap_result( conn, op, rc,
440                                 NULL, text, NULL, NULL );
441
442                         goto return_results;            
443                 }
444
445                 val.bv_val = new_rdn_vals[a_cnt];
446                 val.bv_len = strlen( val.bv_val );
447                 if ( ! access_allowed( be, conn, op, e, 
448                                 mod_tmp->sml_desc, &val, ACL_WRITE ) ) {
449                         Debug( LDAP_DEBUG_TRACE,
450                                 "ldbm_back_modrdn: access not allowed "
451                                 "to attr \"%s\"\n%s%s",
452                                 new_rdn_types[a_cnt], "", "" );
453                         send_ldap_result( conn, op, 
454                                 LDAP_INSUFFICIENT_ACCESS,
455                                 NULL, NULL, NULL, NULL );
456
457                         goto return_results;
458                 }
459
460                 mod_tmp->sml_bvalues = (struct berval **)ch_malloc( 2 * sizeof(struct berval *) );
461                 mod_tmp->sml_bvalues[0] = ber_bvstrdup( new_rdn_vals[a_cnt] );
462                 mod_tmp->sml_bvalues[1] = NULL;
463                 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
464                 mod_tmp->sml_next = mod;
465                 mod = mod_tmp;
466         }
467
468         /* Remove old rdn value if required */
469         if ( deleteoldrdn ) {
470                 /* Get value of old rdn */
471                 if ( old_rdn_vals == NULL ) {
472                         Debug( LDAP_DEBUG_TRACE,
473                                "ldbm_back_modrdn: can't figure out oldRDN value(s) from old RDN\n",
474                                0, 0, 0 );
475
476                         send_ldap_result( conn, op, LDAP_OTHER,
477                                 NULL, "could not parse value(s) from old RDN", NULL, NULL );
478                         goto return_results;            
479                 }
480
481                 for ( d_cnt = 0; old_rdn_types[d_cnt]; d_cnt++ ) {    
482                         int rc;
483                         Modifications *mod_tmp;
484                         struct berval val;
485
486                         mod_tmp = (Modifications *)ch_malloc( sizeof( Modifications ) );
487
488                         mod_tmp->sml_desc = NULL;
489                         rc = slap_str2ad( old_rdn_types[d_cnt], 
490                                         &mod_tmp->sml_desc, &text );
491
492                         if ( rc != LDAP_SUCCESS ) {
493                                 Debug( LDAP_DEBUG_TRACE,
494                                         "ldbm_back_modrdn: %s: %s (old)\n",
495                                         text, old_rdn_types[d_cnt], 0 );
496
497                                 send_ldap_result( conn, op, rc,
498                                         NULL, text, NULL, NULL );
499
500                                 goto return_results;
501                         }
502
503                         val.bv_val = old_rdn_vals[d_cnt];
504                         val.bv_len = strlen( val.bv_val );
505                         if ( ! access_allowed( be, conn, op, e, 
506                                         mod_tmp->sml_desc, &val, ACL_WRITE ) ) {
507                                 Debug( LDAP_DEBUG_TRACE,
508                                         "ldbm_back_modrdn: access not allowed "
509                                         "to attr \"%s\"\n%s%s",
510                                         old_rdn_types[d_cnt], "", "" );
511                                 send_ldap_result( conn, op, 
512                                         LDAP_INSUFFICIENT_ACCESS,
513                                         NULL, NULL, NULL, NULL );
514
515                                 goto return_results;
516                         }
517
518                         /* Remove old value of rdn as an attribute. */
519                         mod_tmp->sml_bvalues = (struct berval **)ch_malloc( 2 * sizeof(struct berval *) );
520                         mod_tmp->sml_bvalues[0] = ber_bvstrdup( old_rdn_vals[d_cnt] );
521                         mod_tmp->sml_bvalues[1] = NULL;
522                         mod_tmp->sml_op = LDAP_MOD_DELETE;
523                         mod_tmp->sml_next = mod;
524                         mod = mod_tmp;
525
526                         Debug( LDAP_DEBUG_TRACE,
527                                "ldbm_back_modrdn: removing old_rdn_val=%s\n",
528                                old_rdn_vals[0], 0, 0 );
529                 }
530         }
531
532         
533         /* check for abandon */
534         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
535         if ( op->o_abandon ) {
536                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
537                 goto return_results;
538         }
539         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
540
541         /* delete old one */
542         if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
543                 send_ldap_result( conn, op, LDAP_OTHER,
544                         NULL, "DN index delete fail", NULL, NULL );
545                 goto return_results;
546         }
547
548         (void) cache_delete_entry( &li->li_cache, e );
549         rc = MUST_DESTROY;
550
551         /* XXX: there is no going back! */
552
553         free( e->e_dn );
554         free( e->e_ndn );
555         e->e_dn = new_dn;
556         e->e_ndn = new_ndn;
557         new_dn = NULL;
558         new_ndn = NULL;
559
560         /* add new one */
561         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
562                 send_ldap_result( conn, op, LDAP_OTHER,
563                         NULL, "DN index add failed", NULL, NULL );
564                 goto return_results;
565         }
566
567         /* modify memory copy of entry */
568         rc = ldbm_modify_internal( be, conn, op, dn, &mod[0], e,
569                 &text, textbuf, textlen );
570
571         if( rc != LDAP_SUCCESS ) {
572                 if( rc != SLAPD_ABANDON ) {
573                         send_ldap_result( conn, op, rc,
574                                 NULL, text, NULL, NULL );
575                 }
576
577                 /* here we may try to delete the newly added dn */
578                 if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
579                         /* we already are in trouble ... */
580                         ;
581                 }
582             
583                 goto return_results;
584         }
585         
586         (void) cache_update_entry( &li->li_cache, e );
587
588         /* NOTE: after this you must not free new_dn or new_ndn!
589          * They are used by cache.
590          */
591
592         /* id2entry index */
593         if ( id2entry_add( be, e ) != 0 ) {
594                 send_ldap_result( conn, op, LDAP_OTHER,
595                         NULL, "entry update failed", NULL, NULL );
596                 goto return_results;
597         }
598
599         send_ldap_result( conn, op, LDAP_SUCCESS,
600                 NULL, NULL, NULL, NULL );
601         rc = 0;
602         cache_entry_commit( e );
603
604 return_results:
605         if( new_dn != NULL ) free( new_dn );
606         if( new_ndn != NULL ) free( new_ndn );
607
608         if( p_dn != NULL ) free( p_dn );
609         if( p_ndn != NULL ) free( p_ndn );
610
611         /* LDAP v2 supporting correct attribute handling. */
612         if( new_rdn_types != NULL ) charray_free( new_rdn_types );
613         if( new_rdn_vals != NULL ) charray_free( new_rdn_vals );
614         if( old_rdn != NULL ) free(old_rdn);
615         if( old_rdn_types != NULL ) charray_free( old_rdn_types );
616         if( old_rdn_vals != NULL ) charray_free( old_rdn_vals );
617
618         if ( mod != NULL ) {
619                 slap_mods_free( mod );
620         }
621
622         /* LDAP v3 Support */
623         if ( np_dn != NULL ) free( np_dn );
624         if ( np_ndn != NULL ) free( np_ndn );
625
626         if( np != NULL ) {
627                 /* free new parent and writer lock */
628                 cache_return_entry_w( &li->li_cache, np );
629         }
630
631         if( p != NULL ) {
632                 /* free parent and writer lock */
633                 cache_return_entry_w( &li->li_cache, p );
634         }
635
636         /* free entry and writer lock */
637         cache_return_entry_w( &li->li_cache, e );
638         if ( rc == MUST_DESTROY ) {
639                 /* if rc == MUST_DESTROY the entry is uncached 
640                  * and its private data is destroyed; 
641                  * the entry must be freed */
642                 entry_free( e );
643         }
644         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
645         return( rc );
646 }