]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
Set peeraddr also for IPv6, fixes ITS#1918
[openldap] / servers / slapd / back-ldbm / modrdn.c
1 /* modrdn.c - ldbm backend modrdn routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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 #define CAN_ROLLBACK    -1
57 #define MUST_DESTROY    1
58         int             rc = CAN_ROLLBACK;
59         int             rc_id = 0;
60         ID              id = NOID;
61         const char *text = NULL;
62         char textbuf[SLAP_TEXT_BUFLEN];
63         size_t textlen = sizeof textbuf;
64         /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
65         LDAPRDN         *new_rdn = NULL;
66         LDAPRDN         *old_rdn = NULL;
67         int             a_cnt, d_cnt;
68         /* Added to support newSuperior */ 
69         Entry           *np = NULL;     /* newSuperior Entry */
70         struct berval   *np_ndn = NULL; /* newSuperior ndn */
71         struct berval   *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
72         /* Used to interface with ldbm_modify_internal() */
73         Modifications   *mod = NULL;            /* Used to delete old/add new rdn */
74         int             manageDSAit = get_manageDSAit( op );
75
76 #ifdef NEW_LOGGING
77         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
78                 "ldbm_back_modrdn: dn: %s newSuperior=%s\n", 
79                 dn->bv_len ? dn->bv_val : "NULL",
80                 ( newSuperior && newSuperior->bv_len )
81                         ? newSuperior->bv_val : "NULL" ));
82 #else
83         Debug( LDAP_DEBUG_TRACE,
84                 "==>ldbm_back_modrdn: dn: %s newSuperior=%s\n", 
85                 dn->bv_len ? dn->bv_val : "NULL",
86                 ( newSuperior && newSuperior->bv_len )
87                         ? newSuperior->bv_val : "NULL", 0 );
88 #endif
89
90         /* grab giant lock for writing */
91         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
92
93         /* get entry with writer lock */
94         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
95                 char* matched_dn = NULL;
96                 BerVarray refs;
97
98                 if( matched != NULL ) {
99                         matched_dn = strdup( matched->e_dn );
100                         refs = is_entry_referral( matched )
101                                 ? get_entry_referrals( be, conn, op, matched )
102                                 : NULL;
103                         cache_return_entry_r( &li->li_cache, matched );
104                 } else {
105                         refs = referral_rewrite( default_referral,
106                                 NULL, dn, LDAP_SCOPE_DEFAULT );
107                 }
108
109                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
110
111                 send_ldap_result( conn, op, LDAP_REFERRAL,
112                         matched_dn, NULL, refs, NULL );
113
114                 if ( refs ) ber_bvarray_free( 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                 BerVarray 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                 if ( refs ) ber_bvarray_free( 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         if ( be_issuffix( be, &e->e_nname ) ) {
156                 p_ndn = slap_empty_bv ;
157         } else {
158                 dnParent( &e->e_nname, &p_ndn );
159         }
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, 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, NULL ) )
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                 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
209                         p_dn = slap_empty_bv;
210                 } else {
211                         dnParent( &e->e_name, &p_dn );
212                 }
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, (struct berval *)&slap_empty_bv ) || 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, NULL );
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 #ifdef NEW_LOGGING
270                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
271                            "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn ));
272 #else
273                 Debug( LDAP_DEBUG_TRACE,
274                        "ldbm_back_modrdn: no parent, locked root\n",
275                        0, 0, 0 );
276 #endif
277         }
278
279         new_parent_dn = &p_dn;  /* New Parent unless newSuperior given */
280
281         if ( newSuperior != NULL ) {
282 #ifdef NEW_LOGGING
283                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
284                         "ldbm_back_modrdn: new parent \"%s\" requested\n",
285                         newSuperior->bv_val ));
286 #else
287                 Debug( LDAP_DEBUG_TRACE, 
288                         "ldbm_back_modrdn: new parent \"%s\" requested...\n",
289                         newSuperior->bv_val, 0, 0 );
290 #endif
291
292                 np_ndn = nnewSuperior;
293
294                 /* newSuperior == oldParent? */
295                 if ( dn_match( &p_ndn, np_ndn ) ) {
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, NULL ) )
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, (struct berval *)&slap_empty_bv ) || 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, NULL );
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         if ( op->o_abandon ) {
472                 goto return_results;
473         }
474
475         if ( ( rc_id = dn2id ( be, &new_ndn, &id ) ) || id != NOID ) {
476                 /* if (rc_id) something bad happened to ldbm cache */
477                 send_ldap_result( conn, op, 
478                         rc_id ? LDAP_OPERATIONS_ERROR : LDAP_ALREADY_EXISTS,
479                         NULL, NULL, NULL, NULL );
480                 goto return_results;
481         }
482
483 #ifdef NEW_LOGGING
484         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
485                 "ldbm_back_modrdn: new ndn (%s) does not exist\n", new_ndn.bv_val ));
486 #else
487         Debug( LDAP_DEBUG_TRACE,
488             "ldbm_back_modrdn: new ndn=%s does not exist\n",
489             new_ndn.bv_val, 0, 0 );
490 #endif
491
492
493         /* Get attribute types and values of our new rdn, we will
494          * need to add that to our new entry
495          */
496         if ( ldap_bv2rdn( newrdn, &new_rdn, (char **)&text,
497                 LDAP_DN_FORMAT_LDAP ) )
498         {
499 #ifdef NEW_LOGGING
500                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
501                         "ldbm_back_modrdn: can't figure out type(s)/value(s) of newrdn\n" ));
502 #else
503                 Debug( LDAP_DEBUG_TRACE,
504                     "ldbm_back_modrdn: can't figure out type(s)/value(s) of newrdn\n",
505                     0, 0, 0 );
506 #endif
507
508                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
509                         NULL, "unable to parse type(s)/value(s) used in RDN", NULL, NULL );
510                 goto return_results;            
511         }
512
513 #ifdef NEW_LOGGING
514         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
515                    "ldbm_back_modrdn: new_rdn_type=\"%s\", new_rdn_val=\"%s\"\n",
516                    new_rdn[0][0]->la_attr.bv_val, new_rdn[0][0]->la_value.bv_val ));
517 #else
518         Debug( LDAP_DEBUG_TRACE,
519                "ldbm_back_modrdn: new_rdn_type=\"%s\", new_rdn_val=\"%s\"\n",
520                new_rdn[0][0]->la_attr.bv_val, new_rdn[0][0]->la_value.bv_val, 0 );
521 #endif
522
523         /* Retrieve the old rdn from the entry's dn */
524         if ( ldap_bv2rdn( dn, &old_rdn, (char **)&text,
525                 LDAP_DN_FORMAT_LDAP ) )
526         {
527 #ifdef NEW_LOGGING
528                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
529                            "ldbm_back_modrdn: can't figure out the old_rdn type(s)/value(s).\n" ));
530 #else
531                 Debug( LDAP_DEBUG_TRACE,
532                        "ldbm_back_modrdn: can't figure out the old_rdn type(s)/value(s)\n",
533                        0, 0, 0 );
534 #endif
535
536                 send_ldap_result( conn, op, LDAP_OTHER,
537                         NULL, "unable to parse type(s)/value(s) used in RDN from old DN", NULL, NULL );
538                 goto return_results;            
539         }
540
541 #if 0
542         if ( newSuperior == NULL
543                 && charray_strcasecmp( (const char **)old_rdn_types, (const char **)new_rdn_types ) != 0 )
544         {
545             /* Not a big deal but we may say something */
546 #ifdef NEW_LOGGING
547             LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
548                        "ldbm_back_modrdn: old_rdn_type=%s new_rdn_type=%s\n",
549                        old_rdn_types[0], new_rdn_types[0] ));
550 #else
551             Debug( LDAP_DEBUG_TRACE,
552                    "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
553                    old_rdn_types[0], new_rdn_types[0], 0 );
554 #endif
555         }               
556 #endif
557
558 #ifdef NEW_LOGGING
559         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
560                    "ldbm_back_modrdn:  DN_X500\n" ));
561 #else
562         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
563                0, 0, 0 );
564 #endif
565
566         mod = NULL;
567         for ( a_cnt = 0; new_rdn[0][a_cnt]; a_cnt++ ) {
568                 int                     rc;
569                 AttributeDescription    *desc = NULL;
570                 Modifications           *mod_tmp;
571
572                 rc = slap_bv2ad( &new_rdn[0][a_cnt]->la_attr, &desc, &text );
573
574                 if ( rc != LDAP_SUCCESS ) {
575 #ifdef NEW_LOGGING
576                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
577                                    "ldbm_back_modrdn: slap_bv2ad error: %s (%s)\n",
578                                    text, new_rdn[0][a_cnt]->la_attr.bv_val ));
579 #else
580                         Debug( LDAP_DEBUG_TRACE,
581                                 "ldbm_back_modrdn: %s: %s (new)\n",
582                                 text, new_rdn[0][a_cnt]->la_attr.bv_val, 0 );
583 #endif
584
585                         send_ldap_result( conn, op, rc,
586                                 NULL, text, NULL, NULL );
587
588                         goto return_results;            
589                 }
590
591                 if ( ! access_allowed( be, conn, op, e, 
592                                 desc, &new_rdn[0][a_cnt]->la_value, ACL_WRITE, NULL ) ) {
593 #ifdef NEW_LOGGING
594                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
595                                    "ldbm_back_modrdn: access "
596                                    "not allowed to attr \"%s\"\n",
597                                    new_rdn[0][a_cnt]->la_attr.bv_val ));
598 #else
599                         Debug( LDAP_DEBUG_TRACE,
600                                 "ldbm_back_modrdn: access not allowed "
601                                 "to attr \"%s\"\n%s%s",
602                                 new_rdn[0][a_cnt]->la_attr.bv_val, "", "" );
603 #endif
604                         send_ldap_result( conn, op, 
605                                 LDAP_INSUFFICIENT_ACCESS,
606                                 NULL, NULL, NULL, NULL );
607
608                         goto return_results;
609                 }
610
611                 mod_tmp = (Modifications *)ch_malloc( sizeof( Modifications )
612                         + 2 * sizeof( struct berval ) );
613                 mod_tmp->sml_desc = desc;
614                 mod_tmp->sml_bvalues = (BerVarray)( mod_tmp + 1 );
615                 mod_tmp->sml_bvalues[0] = new_rdn[0][a_cnt]->la_value;
616                 mod_tmp->sml_bvalues[1].bv_val = NULL;
617                 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
618                 mod_tmp->sml_next = mod;
619                 mod = mod_tmp;
620         }
621
622         /* Remove old rdn value if required */
623         if ( deleteoldrdn ) {
624                 /* Get value of old rdn */
625                 if ( old_rdn == NULL ) {
626 #ifdef NEW_LOGGING
627                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
628                                    "ldbm_back_modrdn: can't figure out old RDN value(s) from old RDN\n" ));
629 #else
630                         Debug( LDAP_DEBUG_TRACE,
631                                "ldbm_back_modrdn: can't figure out oldRDN value(s) from old RDN\n",
632                                0, 0, 0 );
633 #endif
634
635                         send_ldap_result( conn, op, LDAP_OTHER,
636                                 NULL, "could not parse value(s) from old RDN", NULL, NULL );
637                         goto return_results;            
638                 }
639
640                 for ( d_cnt = 0; old_rdn[0][d_cnt]; d_cnt++ ) {    
641                         int                     rc;
642                         AttributeDescription    *desc = NULL;
643                         Modifications           *mod_tmp;
644
645                         rc = slap_bv2ad( &old_rdn[0][d_cnt]->la_attr, &desc, &text );
646
647                         if ( rc != LDAP_SUCCESS ) {
648 #ifdef NEW_LOGGING
649                                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
650                                            "ldbm_back_modrdn: %s: %s (old)\n",
651                                            text, old_rdn[0][d_cnt]->la_attr.bv_val ));
652 #else
653                                 Debug( LDAP_DEBUG_TRACE,
654                                         "ldbm_back_modrdn: %s: %s (old)\n",
655                                         text, old_rdn[0][d_cnt]->la_attr.bv_val, 0 );
656 #endif
657
658                                 send_ldap_result( conn, op, rc,
659                                         NULL, text, NULL, NULL );
660
661                                 goto return_results;
662                         }
663
664                         if ( ! access_allowed( be, conn, op, e, 
665                                         desc, &old_rdn[0][d_cnt]->la_value, ACL_WRITE, NULL ) ) {
666 #ifdef NEW_LOGGING
667                                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
668                                            "ldbm_back_modrdn: access "
669                                            "not allowed to attr \"%s\"\n",
670                                            old_rdn[0][d_cnt]->la_attr.bv_val ));
671 #else
672                                 Debug( LDAP_DEBUG_TRACE,
673                                         "ldbm_back_modrdn: access not allowed "
674                                         "to attr \"%s\"\n%s%s",
675                                         old_rdn[0][d_cnt]->la_attr.bv_val, "", "" );
676 #endif
677                                 send_ldap_result( conn, op, 
678                                         LDAP_INSUFFICIENT_ACCESS,
679                                         NULL, NULL, NULL, NULL );
680
681                                 goto return_results;
682                         }
683
684                         /* Remove old value of rdn as an attribute. */
685                         mod_tmp = (Modifications *)ch_malloc( sizeof( Modifications )
686                                 + 2 * sizeof( struct berval ) );
687                         mod_tmp->sml_desc = desc;
688                         mod_tmp->sml_bvalues = (BerVarray)(mod_tmp+1);
689                         mod_tmp->sml_bvalues[0] = old_rdn[0][d_cnt]->la_value;
690                         mod_tmp->sml_bvalues[1].bv_val = NULL;
691                         mod_tmp->sml_op = LDAP_MOD_DELETE;
692                         mod_tmp->sml_next = mod;
693                         mod = mod_tmp;
694
695 #ifdef NEW_LOGGING
696                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
697                                    "ldbm_back_modrdn: removing old_rdn_val=%s\n", old_rdn[0][d_cnt]->la_value.bv_val ));
698 #else
699                         Debug( LDAP_DEBUG_TRACE,
700                                "ldbm_back_modrdn: removing old_rdn_val=%s\n",
701                                old_rdn[0][d_cnt]->la_value.bv_val, 0, 0 );
702 #endif
703                 }
704         }
705
706         
707         /* check for abandon */
708         if ( op->o_abandon ) {
709                 goto return_results;
710         }
711
712         /* delete old one */
713         if ( dn2id_delete( be, &e->e_nname, e->e_id ) != 0 ) {
714                 send_ldap_result( conn, op, LDAP_OTHER,
715                         NULL, "DN index delete fail", NULL, NULL );
716                 goto return_results;
717         }
718
719         (void) cache_delete_entry( &li->li_cache, e );
720         rc = MUST_DESTROY;
721
722         /* XXX: there is no going back! */
723
724         free( e->e_dn );
725         free( e->e_ndn );
726         e->e_name = new_dn;
727         e->e_nname = new_ndn;
728         new_dn.bv_val = NULL;
729         new_ndn.bv_val = NULL;
730
731         /* add new one */
732         if ( dn2id_add( be, &e->e_nname, e->e_id ) != 0 ) {
733                 send_ldap_result( conn, op, LDAP_OTHER,
734                         NULL, "DN index add failed", NULL, NULL );
735                 goto return_results;
736         }
737
738         /* modify memory copy of entry */
739         rc = ldbm_modify_internal( be, conn, op, dn->bv_val, &mod[0], e,
740                 &text, textbuf, textlen );
741
742         if( rc != LDAP_SUCCESS ) {
743                 if( rc != SLAPD_ABANDON ) {
744                         send_ldap_result( conn, op, rc,
745                                 NULL, text, NULL, NULL );
746                 }
747
748                 /* here we may try to delete the newly added dn */
749                 if ( dn2id_delete( be, &e->e_nname, e->e_id ) != 0 ) {
750                         /* we already are in trouble ... */
751                         ;
752                 }
753             
754                 goto return_results;
755         }
756         
757         (void) cache_update_entry( &li->li_cache, e );
758
759         /* NOTE: after this you must not free new_dn or new_ndn!
760          * They are used by cache.
761          */
762
763         /* id2entry index */
764         if ( id2entry_add( be, e ) != 0 ) {
765                 send_ldap_result( conn, op, LDAP_OTHER,
766                         NULL, "entry update failed", NULL, NULL );
767                 goto return_results;
768         }
769
770         send_ldap_result( conn, op, LDAP_SUCCESS,
771                 NULL, NULL, NULL, NULL );
772         rc = 0;
773         cache_entry_commit( e );
774
775 return_results:
776         if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
777         if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
778
779         /* LDAP v2 supporting correct attribute handling. */
780         if( new_rdn ) ldap_rdnfree( new_rdn );
781         if( old_rdn ) ldap_rdnfree( old_rdn );
782
783         if ( mod != NULL ) {
784                 Modifications *tmp;
785                 for (; mod; mod = tmp ) {
786                         tmp = mod->sml_next;
787                         free( mod );
788                 }
789         }
790
791         /* LDAP v3 Support */
792         if( np != NULL ) {
793                 /* free new parent and writer lock */
794                 cache_return_entry_w( &li->li_cache, np );
795         }
796
797         if( p != NULL ) {
798                 /* free parent and writer lock */
799                 cache_return_entry_w( &li->li_cache, p );
800         }
801
802         /* free entry and writer lock */
803         cache_return_entry_w( &li->li_cache, e );
804         if ( rc == MUST_DESTROY ) {
805                 /* if rc == MUST_DESTROY the entry is uncached 
806                  * and its private data is destroyed; 
807                  * the entry must be freed */
808                 entry_free( e );
809         }
810         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
811         return( rc );
812 }