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