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