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