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