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