]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
non-root modrdn of entries rooted at '' checks children write permission (completes...
[openldap] / servers / slapd / back-ldbm / modrdn.c
1 /* modrdn.c - ldbm backend modrdn routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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             rootlock = 0;
54 #define CAN_ROLLBACK    -1
55 #define MUST_DESTROY    1
56         int             rc = CAN_ROLLBACK;
57         int             rc_id = 0;
58         ID              id = NOID;
59         const char *text = NULL;
60         char textbuf[SLAP_TEXT_BUFLEN];
61         size_t textlen = sizeof textbuf;
62         /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
63         char            **new_rdn_vals = NULL;  /* Vals of new rdn */
64         char            **new_rdn_types = NULL; /* Types of new rdn */
65         int             a_cnt, d_cnt;
66         char            *old_rdn = NULL;        /* Old rdn's attr type & val */
67         char            **old_rdn_types = NULL; /* Types of old rdn attrs. */
68         char            **old_rdn_vals = NULL;  /* Old rdn attribute values */
69         /* Added to support newSuperior */ 
70         Entry           *np = NULL;     /* newSuperior Entry */
71         char            *np_dn = NULL;  /* newSuperior dn */
72         char            *np_ndn = NULL; /* newSuperior ndn */
73         char            *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
74         /* Used to interface with ldbm_modify_internal() */
75         Modifications   *mod = NULL;            /* Used to delete old/add new rdn */
76         int             manageDSAit = get_manageDSAit( op );
77
78 #ifdef NEW_LOGGING
79         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
80                 "ldbm_back_modrdn: dn: %s newSuperior=%s\n", 
81                 dn ? dn : "NULL", newSuperior ? newSuperior : "NULL" ));
82 #else
83         Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
84             (newSuperior ? newSuperior : "NULL"),
85             0, 0 );
86 #endif
87
88         /* get entry with writer lock */
89         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
90                 char* matched_dn = NULL;
91                 struct berval** refs;
92
93                 if( matched != NULL ) {
94                         matched_dn = strdup( matched->e_dn );
95                         refs = is_entry_referral( matched )
96                                 ? get_entry_referrals( be, conn, op, matched,
97                                         dn, LDAP_SCOPE_DEFAULT )
98                                 : NULL;
99                         cache_return_entry_r( &li->li_cache, matched );
100                 } else {
101                         refs = referral_rewrite( default_referral,
102                                 NULL, dn, LDAP_SCOPE_DEFAULT );
103                 }
104
105                 send_ldap_result( conn, op, LDAP_REFERRAL,
106                         matched_dn, NULL, refs, NULL );
107
108                 ber_bvecfree( refs );
109                 free( matched_dn );
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, dn, LDAP_SCOPE_DEFAULT );
119
120 #ifdef NEW_LOGGING
121                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
122                         "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn ));
123 #else
124                 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
125                     0, 0 );
126 #endif
127
128                 send_ldap_result( conn, op, LDAP_REFERRAL,
129                     e->e_dn, NULL, refs, NULL );
130
131                 ber_bvecfree( refs );
132                 goto return_results;
133         }
134
135         if ( has_children( be, e ) ) {
136 #ifdef NEW_LOGGING
137                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
138                         "ldbm_back_modrdn: entry %s has children\n", e->e_dn ));
139 #else
140                 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
141                     0, 0 );
142 #endif
143
144                 send_ldap_result( conn, op, LDAP_NOT_ALLOWED_ON_NONLEAF,
145                     NULL, "subtree rename not supported", NULL, NULL );
146                 goto return_results;
147         }
148
149         if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL && p_ndn[0] != '\0' ) {
150                 /* Make sure parent entry exist and we can write its 
151                  * children.
152                  */
153
154                 if( (p = dn2entry_w( be, p_ndn, NULL )) == NULL) {
155 #ifdef NEW_LOGGING
156                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
157                                 "ldbm_back_modrdn: parent of %s does not exist\n", e->e_ndn ));
158 #else
159                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
160                                 0, 0, 0);
161 #endif
162
163                         send_ldap_result( conn, op, LDAP_OTHER,
164                                 NULL, "parent entry does not exist", NULL, NULL );
165
166                         goto return_results;
167                 }
168
169                 /* check parent for "children" acl */
170                 if ( ! access_allowed( be, conn, op, p,
171                         children, NULL, ACL_WRITE ) )
172                 {
173 #ifdef NEW_LOGGING
174                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
175                                    "ldbm_back_modrdn: no access to parent of (%s)\n", e->e_dn ));
176 #else
177                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
178                                 0, 0 );
179 #endif
180
181                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
182                                 NULL, NULL, NULL, NULL );
183                         goto return_results;
184                 }
185
186 #ifdef NEW_LOGGING
187                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
188                            "ldbm_back_modrdn: wr to children of entry %s OK\n",
189                            p_ndn ));
190 #else
191                 Debug( LDAP_DEBUG_TRACE,
192                        "ldbm_back_modrdn: wr to children of entry %s OK\n",
193                        p_ndn, 0, 0 );
194 #endif
195
196                 p_dn = dn_parent( be, e->e_dn );
197
198 #ifdef NEW_LOGGING
199                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
200                            "ldbm_back_modrdn: parent dn=%s\n", p_dn ));
201 #else
202                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
203                        p_dn, 0, 0 );
204 #endif
205
206         } else {
207                 /* no parent, must be root to modify rdn */
208                 if( ! be_isroot( be, op->o_ndn ) ) {
209                         if ( be_issuffix( be, "" ) ) {
210                                 static const Entry rootp = { NOID, "", "", NULL, NULL };
211                                 p = (Entry *)&rootp;
212                                 
213                                 rc = access_allowed( be, conn, op, p,
214                                                 children, NULL, ACL_WRITE );
215                                 p = NULL;
216                                                                 
217                                 /* check parent for "children" acl */
218                                 if ( ! rc ) {
219 #ifdef NEW_LOGGING
220                                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
221                                                 "ldbm_back_modrdn: no access "
222                                                 "to parent of ("")\n" ));
223 #else
224                                         Debug( LDAP_DEBUG_TRACE,
225                                                 "<=- ldbm_back_modrdn: no "
226                                                 "access to parent\n", 0, 0, 0 );
227 #endif
228
229                                         send_ldap_result( conn, op, 
230                                                 LDAP_INSUFFICIENT_ACCESS,
231                                                 NULL, NULL, NULL, NULL );
232                                         goto return_results;
233                                 }
234
235                         } else {
236 #ifdef NEW_LOGGING
237                                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
238                                            "ldbm_back_modrdn: (%s) has no "
239                                            "parent & not a root.\n", dn ));
240 #else
241                                 Debug( LDAP_DEBUG_TRACE,
242                                         "<=- ldbm_back_modrdn: no parent & "
243                                         "not root\n", 0, 0, 0);
244 #endif
245
246                                 send_ldap_result( conn, op, 
247                                         LDAP_INSUFFICIENT_ACCESS,
248                                         NULL, NULL, NULL, NULL );
249                                 goto return_results;
250                         }
251                 }
252
253                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
254                 rootlock = 1;
255                 
256 #ifdef NEW_LOGGING
257                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
258                            "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn ));
259 #else
260                 Debug( LDAP_DEBUG_TRACE,
261                        "ldbm_back_modrdn: no parent, locked root\n",
262                        0, 0, 0 );
263 #endif
264         }
265
266         new_parent_dn = p_dn;   /* New Parent unless newSuperior given */
267
268         if ( newSuperior != NULL ) {
269 #ifdef NEW_LOGGING
270                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
271                            "ldbm_back_modrdn: new parent \"%s\" requested\n", newSuperior ));
272 #else
273                 Debug( LDAP_DEBUG_TRACE, 
274                         "ldbm_back_modrdn: new parent \"%s\" requested...\n",
275                         newSuperior, 0, 0 );
276 #endif
277
278                 np_dn = ch_strdup( newSuperior );
279                 np_ndn = ch_strdup( np_dn );
280                 (void) dn_normalize( np_ndn );
281
282                 /* newSuperior == oldParent? */
283                 if ( strcmp( p_ndn, np_ndn ) == 0 ) {
284 #ifdef NEW_LOGGING
285                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
286                                    "ldbm_back_modrdn: new parent\"%s\" seems to be the same as the old parent \"%s\"\n",
287                                    newSuperior, p_dn ));
288 #else
289                         Debug( LDAP_DEBUG_TRACE, 
290                                "ldbm_back_modrdn: new parent \"%s\" seems to be the same as old parent \"%s\"...\n",
291                                newSuperior, p_dn, 0 );
292 #endif
293
294                         newSuperior = NULL; /* ignore newSuperior */
295                 }
296         }
297
298         if ( newSuperior != NULL ) {
299                 /* newSuperior == entry being moved?, if so ==> ERROR */
300                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
301
302                 if( (np = dn2entry_w( be, np_ndn, NULL )) == NULL) {
303 #ifdef NEW_LOGGING
304                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
305                                    "ldbm_back_modrdn: newSup(ndn=%s) not found.\n", np_ndn ));
306 #else
307                         Debug( LDAP_DEBUG_TRACE,
308                                "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
309                                np_ndn, 0, 0);
310 #endif
311
312                         send_ldap_result( conn, op, LDAP_OTHER,
313                                 NULL, "newSuperior not found", NULL, NULL );
314                         goto return_results;
315                 }
316
317 #ifdef NEW_LOGGING
318                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
319                            "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
320                            np, np->e_id ));
321 #else
322                 Debug( LDAP_DEBUG_TRACE,
323                        "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
324                        np, np->e_id, 0 );
325 #endif
326
327                 /* check newSuperior for "children" acl */
328                 if ( !access_allowed( be, conn, op, np, children, NULL,
329                                       ACL_WRITE ) )
330                 {
331 #ifdef NEW_LOGGING
332                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
333                                    "ldbm_back_modrdn: no wr to newSup children.\n" ));
334 #else
335                         Debug( LDAP_DEBUG_TRACE,
336                                "ldbm_back_modrdn: no wr to newSup children\n",
337                                0, 0, 0 );
338 #endif
339
340                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
341                                 NULL, NULL, NULL, NULL );
342                         goto return_results;
343                 }
344
345                 if ( is_entry_alias( np ) ) {
346                         /* entry is an alias, don't allow bind */
347 #ifdef NEW_LOGGING
348                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
349                                    "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn ));
350 #else
351                         Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
352                             0, 0 );
353 #endif
354
355
356                         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
357                             NULL, "newSuperior is an alias", NULL, NULL );
358
359                         goto return_results;
360                 }
361
362                 if ( is_entry_referral( np ) ) {
363                         /* parent is a referral, don't allow add */
364                         /* parent is an alias, don't allow add */
365 #ifdef NEW_LOGGING
366                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
367                                 "ldbm_back_modrdn: entry (%s) is a referral\n",
368                                 np->e_dn ));
369 #else
370                         Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
371                                 np->e_dn, 0, 0 );
372 #endif
373
374                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
375                             NULL, "newSuperior is a referral", NULL, NULL );
376
377                         goto return_results;
378                 }
379
380 #ifdef NEW_LOGGING
381                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
382                         "ldbm_back_modrdn: wr to new parent's children OK.\n" ));
383 #else
384                 Debug( LDAP_DEBUG_TRACE,
385                     "ldbm_back_modrdn: wr to new parent's children OK\n",
386                     0, 0, 0 );
387 #endif
388
389                 new_parent_dn = np_dn;
390         }
391         
392         /* Build target dn and make sure target entry doesn't exist already. */
393         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
394
395         new_ndn = ch_strdup(new_dn);
396         (void) dn_normalize( new_ndn );
397
398 #ifdef NEW_LOGGING
399         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
400                 "ldbm_back_modrdn: new ndn=%s\n", new_ndn ));
401 #else
402         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
403             new_ndn, 0, 0 );
404 #endif
405
406         /* check for abandon */
407         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
408         if ( op->o_abandon ) {
409                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
410                 goto return_results;
411         }
412
413         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
414         if ( ( rc_id = dn2id ( be, new_ndn, &id ) ) || id != NOID ) {
415                 /* if (rc_id) something bad happened to ldbm cache */
416                 send_ldap_result( conn, op, 
417                         rc_id ? LDAP_OPERATIONS_ERROR : LDAP_ALREADY_EXISTS,
418                         NULL, NULL, NULL, NULL );
419                 goto return_results;
420         }
421
422 #ifdef NEW_LOGGING
423         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
424                    "ldbm_back_modrdn: new ndn (%s) does not exist\n", new_ndn ));
425 #else
426         Debug( LDAP_DEBUG_TRACE,
427                "ldbm_back_modrdn: new ndn=%s does not exist\n",
428                new_ndn, 0, 0 );
429 #endif
430
431
432         /* Get attribute types and values of our new rdn, we will
433          * need to add that to our new entry
434          */
435         if ( rdn_attrs( newrdn, &new_rdn_types, &new_rdn_vals ) ) {
436 #ifdef NEW_LOGGING
437                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
438                            "ldbm_back_modrdn: can't figure out type(s)/value(s) of newrdn\n" ));
439 #else
440                 Debug( LDAP_DEBUG_TRACE,
441                        "ldbm_back_modrdn: can't figure out type(s)/value(s) of newrdn\n",
442                        0, 0, 0 );
443 #endif
444
445                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
446                         NULL, "unable to parse type(s)/value(s) used in RDN", NULL, NULL );
447                 goto return_results;            
448         }
449
450 #ifdef NEW_LOGGING
451         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
452                    "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
453                    new_rdn_vals[0], new_rdn_types[0] ));
454 #else
455         Debug( LDAP_DEBUG_TRACE,
456                "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
457                new_rdn_vals[0], new_rdn_types[0], 0 );
458 #endif
459
460         /* Retrieve the old rdn from the entry's dn */
461         if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
462 #ifdef NEW_LOGGING
463                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
464                            "ldbm_back_modrdn: can't figure out old_rdn from dn (%s)\n",
465                            dn ));
466 #else
467                 Debug( LDAP_DEBUG_TRACE,
468                        "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
469                        0, 0, 0 );
470 #endif
471
472                 send_ldap_result( conn, op, LDAP_OTHER,
473                         NULL, "could not parse old DN", NULL, NULL );
474                 goto return_results;            
475         }
476
477         if ( rdn_attrs( old_rdn, &old_rdn_types, &old_rdn_vals ) ) {
478 #ifdef NEW_LOGGING
479                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
480                            "ldbm_back_modrdn: can't figure out the old_rdn type(s)/value(s).\n" ));
481 #else
482                 Debug( LDAP_DEBUG_TRACE,
483                        "ldbm_back_modrdn: can't figure out the old_rdn type(s)/value(s)\n",
484                        0, 0, 0 );
485 #endif
486
487                 send_ldap_result( conn, op, LDAP_OTHER,
488                         NULL, "unable to parse type(s)/value(s) used in RDN from old DN", NULL, NULL );
489                 goto return_results;            
490         }
491         
492         if ( newSuperior == NULL
493                 && charray_strcasecmp( (const char **)old_rdn_types, (const char **)new_rdn_types ) != 0 )
494         {
495             /* Not a big deal but we may say something */
496 #ifdef NEW_LOGGING
497             LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
498                        "ldbm_back_modrdn: old_rdn_type=%s new_rdn_type=%s\n",
499                        old_rdn_types[0], new_rdn_types[0] ));
500 #else
501             Debug( LDAP_DEBUG_TRACE,
502                    "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
503                    old_rdn_types[0], new_rdn_types[0], 0 );
504 #endif
505         }               
506
507 #ifdef NEW_LOGGING
508         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
509                    "ldbm_back_modrdn:  DN_X500\n" ));
510 #else
511         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
512                0, 0, 0 );
513 #endif
514
515         mod = NULL;
516         for ( a_cnt = 0; new_rdn_types[a_cnt]; a_cnt++ ) {
517                 int                     rc;
518                 AttributeDescription    *desc = NULL;
519                 Modifications           *mod_tmp;
520                 struct berval           val;
521
522
523                 rc = slap_str2ad( new_rdn_types[a_cnt], &desc, &text );
524
525                 if ( rc != LDAP_SUCCESS ) {
526 #ifdef NEW_LOGGING
527                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
528                                    "ldbm_back_modrdn: slap_str2ad error: %s (%s)\n",
529                                    text, new_rdn_types[a_cnt] ));
530 #else
531                         Debug( LDAP_DEBUG_TRACE,
532                                 "ldbm_back_modrdn: %s: %s (new)\n",
533                                 text, new_rdn_types[a_cnt], 0 );
534 #endif
535
536                         send_ldap_result( conn, op, rc,
537                                 NULL, text, NULL, NULL );
538
539                         goto return_results;            
540                 }
541
542                 val.bv_val = new_rdn_vals[a_cnt];
543                 val.bv_len = strlen( val.bv_val );
544                 if ( ! access_allowed( be, conn, op, e, 
545                                 desc, &val, ACL_WRITE ) ) {
546 #ifdef NEW_LOGGING
547                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
548                                    "ldbm_back_modrdn: access "
549                                    "not allowed to attr \"%s\"\n",
550                                    new_rdn_types[a_cnt] ));
551 #else
552                         Debug( LDAP_DEBUG_TRACE,
553                                 "ldbm_back_modrdn: access not allowed "
554                                 "to attr \"%s\"\n%s%s",
555                                 new_rdn_types[a_cnt], "", "" );
556 #endif
557                         send_ldap_result( conn, op, 
558                                 LDAP_INSUFFICIENT_ACCESS,
559                                 NULL, NULL, NULL, NULL );
560
561                         goto return_results;
562                 }
563
564                 mod_tmp = (Modifications *)ch_malloc( sizeof( Modifications ) );
565                 mod_tmp->sml_desc = desc;
566                 mod_tmp->sml_bvalues = (struct berval **)ch_malloc( 2 * sizeof(struct berval *) );
567                 mod_tmp->sml_bvalues[0] = ber_bvstrdup( new_rdn_vals[a_cnt] );
568                 mod_tmp->sml_bvalues[1] = NULL;
569                 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
570                 mod_tmp->sml_next = mod;
571                 mod = mod_tmp;
572         }
573
574         /* Remove old rdn value if required */
575         if ( deleteoldrdn ) {
576                 /* Get value of old rdn */
577                 if ( old_rdn_vals == NULL ) {
578 #ifdef NEW_LOGGING
579                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
580                                    "ldbm_back_modrdn: can't figure out old RDN value(s) from old RDN\n" ));
581 #else
582                         Debug( LDAP_DEBUG_TRACE,
583                                "ldbm_back_modrdn: can't figure out oldRDN value(s) from old RDN\n",
584                                0, 0, 0 );
585 #endif
586
587                         send_ldap_result( conn, op, LDAP_OTHER,
588                                 NULL, "could not parse value(s) from old RDN", NULL, NULL );
589                         goto return_results;            
590                 }
591
592                 for ( d_cnt = 0; old_rdn_types[d_cnt]; d_cnt++ ) {    
593                         int                     rc;
594                         AttributeDescription    *desc = NULL;
595                         Modifications           *mod_tmp;
596                         struct berval           val;
597
598
599                         rc = slap_str2ad( old_rdn_types[d_cnt], &desc, &text );
600
601                         if ( rc != LDAP_SUCCESS ) {
602 #ifdef NEW_LOGGING
603                                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
604                                            "ldbm_back_modrdn: %s: %s (old)\n",
605                                            text, old_rdn_types[d_cnt] ));
606 #else
607                                 Debug( LDAP_DEBUG_TRACE,
608                                         "ldbm_back_modrdn: %s: %s (old)\n",
609                                         text, old_rdn_types[d_cnt], 0 );
610 #endif
611
612                                 send_ldap_result( conn, op, rc,
613                                         NULL, text, NULL, NULL );
614
615                                 goto return_results;
616                         }
617
618                         val.bv_val = old_rdn_vals[d_cnt];
619                         val.bv_len = strlen( val.bv_val );
620                         if ( ! access_allowed( be, conn, op, e, 
621                                         desc, &val, ACL_WRITE ) ) {
622 #ifdef NEW_LOGGING
623                                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
624                                            "ldbm_back_modrdn: access "
625                                            "not allowed to attr \"%s\"\n",
626                                            old_rdn_types[d_cnt] ));
627 #else
628                                 Debug( LDAP_DEBUG_TRACE,
629                                         "ldbm_back_modrdn: access not allowed "
630                                         "to attr \"%s\"\n%s%s",
631                                         old_rdn_types[d_cnt], "", "" );
632 #endif
633                                 send_ldap_result( conn, op, 
634                                         LDAP_INSUFFICIENT_ACCESS,
635                                         NULL, NULL, NULL, NULL );
636
637                                 goto return_results;
638                         }
639
640                         /* Remove old value of rdn as an attribute. */
641                         mod_tmp = (Modifications *)ch_malloc( sizeof( Modifications ) );
642                         mod_tmp->sml_desc = desc;
643                         mod_tmp->sml_bvalues = (struct berval **)ch_malloc( 2 * sizeof(struct berval *) );
644                         mod_tmp->sml_bvalues[0] = ber_bvstrdup( old_rdn_vals[d_cnt] );
645                         mod_tmp->sml_bvalues[1] = NULL;
646                         mod_tmp->sml_op = LDAP_MOD_DELETE;
647                         mod_tmp->sml_next = mod;
648                         mod = mod_tmp;
649
650 #ifdef NEW_LOGGING
651                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
652                                    "ldbm_back_modrdn: removing old_rdn_val=%s\n", old_rdn_vals[0] ));
653 #else
654                         Debug( LDAP_DEBUG_TRACE,
655                                "ldbm_back_modrdn: removing old_rdn_val=%s\n",
656                                old_rdn_vals[0], 0, 0 );
657 #endif
658                 }
659         }
660
661         
662         /* check for abandon */
663         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
664         if ( op->o_abandon ) {
665                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
666                 goto return_results;
667         }
668         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
669
670         /* delete old one */
671         if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
672                 send_ldap_result( conn, op, LDAP_OTHER,
673                         NULL, "DN index delete fail", NULL, NULL );
674                 goto return_results;
675         }
676
677         (void) cache_delete_entry( &li->li_cache, e );
678         rc = MUST_DESTROY;
679
680         /* XXX: there is no going back! */
681
682         free( e->e_dn );
683         free( e->e_ndn );
684         e->e_dn = new_dn;
685         e->e_ndn = new_ndn;
686         new_dn = NULL;
687         new_ndn = NULL;
688
689         /* add new one */
690         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
691                 send_ldap_result( conn, op, LDAP_OTHER,
692                         NULL, "DN index add failed", NULL, NULL );
693                 goto return_results;
694         }
695
696         /* modify memory copy of entry */
697         rc = ldbm_modify_internal( be, conn, op, dn, &mod[0], e,
698                 &text, textbuf, textlen );
699
700         if( rc != LDAP_SUCCESS ) {
701                 if( rc != SLAPD_ABANDON ) {
702                         send_ldap_result( conn, op, rc,
703                                 NULL, text, NULL, NULL );
704                 }
705
706                 /* here we may try to delete the newly added dn */
707                 if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
708                         /* we already are in trouble ... */
709                         ;
710                 }
711             
712                 goto return_results;
713         }
714         
715         (void) cache_update_entry( &li->li_cache, e );
716
717         /* NOTE: after this you must not free new_dn or new_ndn!
718          * They are used by cache.
719          */
720
721         /* id2entry index */
722         if ( id2entry_add( be, e ) != 0 ) {
723                 send_ldap_result( conn, op, LDAP_OTHER,
724                         NULL, "entry update failed", NULL, NULL );
725                 goto return_results;
726         }
727
728         send_ldap_result( conn, op, LDAP_SUCCESS,
729                 NULL, NULL, NULL, NULL );
730         rc = 0;
731         cache_entry_commit( e );
732
733 return_results:
734         if( new_dn != NULL ) free( new_dn );
735         if( new_ndn != NULL ) free( new_ndn );
736
737         if( p_dn != NULL ) free( p_dn );
738         if( p_ndn != NULL ) free( p_ndn );
739
740         /* LDAP v2 supporting correct attribute handling. */
741         if( new_rdn_types != NULL ) charray_free( new_rdn_types );
742         if( new_rdn_vals != NULL ) charray_free( new_rdn_vals );
743         if( old_rdn != NULL ) free(old_rdn);
744         if( old_rdn_types != NULL ) charray_free( old_rdn_types );
745         if( old_rdn_vals != NULL ) charray_free( old_rdn_vals );
746
747         if ( mod != NULL ) {
748                 slap_mods_free( mod );
749         }
750
751         /* LDAP v3 Support */
752         if ( np_dn != NULL ) free( np_dn );
753         if ( np_ndn != NULL ) free( np_ndn );
754
755         if( np != NULL ) {
756                 /* free new parent and writer lock */
757                 cache_return_entry_w( &li->li_cache, np );
758         }
759
760         if( p != NULL ) {
761                 /* free parent and writer lock */
762                 cache_return_entry_w( &li->li_cache, p );
763         }
764
765         if ( rootlock ) {
766                 /* release root writer lock */
767                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
768         }
769
770         /* free entry and writer lock */
771         cache_return_entry_w( &li->li_cache, e );
772         if ( rc == MUST_DESTROY ) {
773                 /* if rc == MUST_DESTROY the entry is uncached 
774                  * and its private data is destroyed; 
775                  * the entry must be freed */
776                 entry_free( e );
777         }
778         return( rc );
779 }