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