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