1 /* modrdn.c - ldbm backend modrdn routine */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
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
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.
18 * Redistribution and use in source and binary forms are permitted
19 * without restriction or fee of any kind as long as this notice
28 #include <ac/string.h>
29 #include <ac/socket.h>
32 #include "back-ldbm.h"
33 #include "proto-back-ldbm.h"
44 const char *newSuperior
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;
55 const char *text = NULL;
56 /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
57 char *new_rdn_val = NULL; /* Val of new rdn */
58 char *new_rdn_type = NULL; /* Type of new rdn */
59 char *old_rdn = NULL; /* Old rdn's attr type & val */
60 char *old_rdn_type = NULL; /* Type of old rdn attr. */
61 char *old_rdn_val = NULL; /* Old rdn attribute value */
62 /* Added to support newSuperior */
63 Entry *np = NULL; /* newSuperior Entry */
64 char *np_dn = NULL; /* newSuperior dn */
65 char *np_ndn = NULL; /* newSuperior ndn */
66 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
67 /* Used to interface with ldbm_modify_internal() */
68 struct berval add_bv; /* Stores new rdn att */
69 struct berval *add_bvals[2]; /* Stores new rdn att */
70 struct berval del_bv; /* Stores old rdn att */
71 struct berval *del_bvals[2]; /* Stores old rdn att */
72 Modifications mod[2]; /* Used to delete old rdn */
73 int manageDSAit = get_manageDSAit( op );
76 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
77 "ldbm_back_modrdn: dn: %s newSuperior=%s\n",
78 dn ? dn : "NULL", newSuperior ? newSuperior : "NULL" ));
80 Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
81 (newSuperior ? newSuperior : "NULL"),
86 /* get entry with writer lock */
87 if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
88 char* matched_dn = NULL;
89 struct berval** refs = NULL;
91 if( matched != NULL ) {
92 matched_dn = strdup( matched->e_dn );
93 refs = is_entry_referral( matched )
94 ? get_entry_referrals( be, conn, op, matched )
96 cache_return_entry_r( &li->li_cache, matched );
98 refs = default_referral;
101 send_ldap_result( conn, op, LDAP_REFERRAL,
102 matched_dn, NULL, refs, NULL );
104 if ( matched != NULL ) {
105 ber_bvecfree( refs );
112 if (!manageDSAit && is_entry_referral( e ) ) {
113 /* parent is a referral, don't allow add */
114 /* parent is an alias, don't allow add */
115 struct berval **refs = get_entry_referrals( be,
119 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
120 "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn ));
122 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
127 send_ldap_result( conn, op, LDAP_REFERRAL,
128 e->e_dn, NULL, refs, NULL );
130 ber_bvecfree( refs );
135 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
137 /* Make sure parent entry exist and we can write its
141 if( (p = dn2entry_w( be, p_ndn, NULL )) == NULL) {
143 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
144 "ldbm_back_modrdn: parent of %s does not exist\n", e->e_ndn ));
146 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
150 send_ldap_result( conn, op, LDAP_OTHER,
151 NULL, NULL, NULL, NULL );
155 /* check parent for "children" acl */
156 if ( ! access_allowed( be, conn, op, p,
157 children, NULL, ACL_WRITE ) )
160 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
161 "ldbm_back_modrdn: no access to parent of (%s)\n", e->e_dn ));
163 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
167 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
168 NULL, NULL, NULL, NULL );
173 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
174 "ldbm_back_modrdn: wr to children of entry %s OK\n",
177 Debug( LDAP_DEBUG_TRACE,
178 "ldbm_back_modrdn: wr to children of entry %s OK\n",
183 p_dn = dn_parent( be, e->e_dn );
187 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
188 "ldbm_back_modrdn: parent dn=%s\n", p_dn ));
190 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
196 /* no parent, modrdn entry directly under root */
197 if( ! be_isroot( be, op->o_ndn ) ) {
199 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
200 "ldbm_back_modrdn: (%s) no parent & not a root.\n",
203 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
207 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
208 NULL, NULL, NULL, NULL );
212 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
216 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
217 "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn ));
219 Debug( LDAP_DEBUG_TRACE,
220 "ldbm_back_modrdn: no parent, locked root\n",
227 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
229 if ( newSuperior != NULL ) {
231 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
232 "ldbm_back_modrdn: new parent \"%s\" requested\n", newSuperior ));
234 Debug( LDAP_DEBUG_TRACE,
235 "ldbm_back_modrdn: new parent \"%s\" requested...\n",
240 np_dn = ch_strdup( newSuperior );
241 np_ndn = ch_strdup( np_dn );
242 (void) dn_normalize( np_ndn );
244 /* newSuperior == oldParent? */
245 if ( strcmp( p_ndn, np_ndn ) == 0 ) {
247 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
248 "ldbm_back_modrdn: new parent\"%s\" seems to be the same as the old parent \"%s\"\n",
249 newSuperior, p_dn ));
251 Debug( LDAP_DEBUG_TRACE,
252 "ldbm_back_modrdn: new parent \"%s\" seems to be the same as old parent \"%s\"...\n",
253 newSuperior, p_dn, 0 );
256 newSuperior = NULL; /* ignore newSuperior */
260 if ( newSuperior != NULL ) {
262 /* newSuperior == entry being moved?, if so ==> ERROR */
263 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
265 if( (np = dn2entry_w( be, np_ndn, NULL )) == NULL) {
267 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
268 "ldbm_back_modrdn: newSup(ndn=%s) not found.\n", np_ndn ));
270 Debug( LDAP_DEBUG_TRACE,
271 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
275 send_ldap_result( conn, op, LDAP_OTHER,
276 NULL, NULL, NULL, NULL );
281 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
282 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
285 Debug( LDAP_DEBUG_TRACE,
286 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
291 /* check newSuperior for "children" acl */
292 if ( !access_allowed( be, conn, op, np, children, NULL,
296 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
297 "ldbm_back_modrdn: no wr to newSup children.\n" ));
299 Debug( LDAP_DEBUG_TRACE,
300 "ldbm_back_modrdn: no wr to newSup children\n",
304 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
305 NULL, NULL, NULL, NULL );
309 if ( is_entry_alias( np ) ) {
310 /* entry is an alias, don't allow bind */
312 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
313 "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn ));
315 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
320 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
321 NULL, NULL, NULL, NULL );
326 if ( is_entry_referral( np ) ) {
327 /* parent is a referral, don't allow add */
328 /* parent is an alias, don't allow add */
330 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
331 "ldbm_back_modrdn: entry (%s) is a referral\n",
334 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
339 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
340 NULL, NULL, NULL, NULL );
346 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
347 "ldbm_back_modrdn: wr to new parent's children OK.\n" ));
349 Debug( LDAP_DEBUG_TRACE,
350 "ldbm_back_modrdn: wr to new parent's children OK\n",
355 new_parent_dn = np_dn;
358 /* Build target dn and make sure target entry doesn't exist already. */
360 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
363 new_ndn = ch_strdup(new_dn);
364 (void) dn_normalize( new_ndn );
367 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
368 "ldbm_back_modrdn: new ndn=%s\n", new_ndn ));
370 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
375 /* check for abandon */
376 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
377 if ( op->o_abandon ) {
378 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
382 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
383 if (dn2id ( be, new_ndn ) != NOID) {
384 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
385 NULL, NULL, NULL, NULL );
390 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
391 "ldbm_back_modrdn: new ndn (%s) does not exist\n", new_ndn ));
393 Debug( LDAP_DEBUG_TRACE,
394 "ldbm_back_modrdn: new ndn=%s does not exist\n",
399 /* Get attribute type and attribute value of our new rdn, we will
400 * need to add that to our new entry
403 if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
406 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
407 "ldbm_back_modrdn: can't figure out type of newrdn\n" ));
409 Debug( LDAP_DEBUG_TRACE,
410 "ldbm_back_modrdn: can't figure out type of newrdn\n",
414 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
415 NULL, "unknown type used in RDN", NULL, NULL );
420 if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
423 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
424 "ldbm_back_modrdn: can't figure out val of newrdn\n"));
426 Debug( LDAP_DEBUG_TRACE,
427 "ldbm_back_modrdn: can't figure out val of newrdn\n",
431 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
432 NULL, "could not parse RDN value", NULL, NULL );
438 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
439 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
440 new_rdn_val, new_rdn_type ));
442 Debug( LDAP_DEBUG_TRACE,
443 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
444 new_rdn_val, new_rdn_type, 0 );
448 /* Retrieve the old rdn from the entry's dn */
450 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
452 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
453 "ldbm_back_modrdn: can't figure out old_rdn from dn (%s)\n",
456 Debug( LDAP_DEBUG_TRACE,
457 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
461 send_ldap_result( conn, op, LDAP_OTHER,
462 NULL, "could not parse old DN", NULL, NULL );
466 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
468 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
469 "ldbm_back_modrdn: can't figure out the old_rdn type.\n" ));
471 Debug( LDAP_DEBUG_TRACE,
472 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
476 send_ldap_result( conn, op, LDAP_OTHER,
477 NULL, "count parse RDN from old DN", NULL, NULL );
481 if ( newSuperior == NULL
482 && strcasecmp( old_rdn_type, new_rdn_type ) != 0 )
484 /* Not a big deal but we may say something */
486 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
487 "ldbm_back_modrdn: old_rdn_type=%s new_rdn_type-%s\n",
488 old_rdn_type, new_rdn_type ));
490 Debug( LDAP_DEBUG_TRACE,
491 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
492 old_rdn_type, new_rdn_type, 0 );
498 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
499 "ldbm_back_modrdn: DN_X500\n" ));
501 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
506 /* Add new attribute value to the entry.
509 add_bvals[0] = &add_bv; /* Array of bervals */
512 add_bv.bv_val = new_rdn_val;
513 add_bv.bv_len = strlen(new_rdn_val);
518 mod[0].sml_desc = NULL;
519 rc = slap_str2ad( new_rdn_type, &mod[0].sml_desc, &text );
521 if( rc != LDAP_SUCCESS ) {
523 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
524 "ldbm_back_modrdn: slap_str2ad error: %s (%s)\n",
525 text, new_rdn_type ));
527 Debug( LDAP_DEBUG_TRACE,
528 "ldbm_back_modrdn: %s: %s (new)\n",
529 text, new_rdn_type, 0 );
532 send_ldap_result( conn, op, rc,
533 NULL, text, NULL, NULL );
537 mod[0].sml_bvalues = add_bvals;
538 mod[0].sml_op = SLAP_MOD_SOFTADD;
539 mod[0].sml_next = NULL;
541 /* Remove old rdn value if required */
544 /* Get value of old rdn */
546 if ((old_rdn_val = rdn_attr_value( old_rdn ))
550 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
551 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n" ));
553 Debug( LDAP_DEBUG_TRACE,
554 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
558 send_ldap_result( conn, op, LDAP_OTHER,
559 NULL, "could not parse value from old RDN", NULL, NULL );
563 del_bvals[0] = &del_bv; /* Array of bervals */
566 /* Remove old value of rdn as an attribute. */
568 del_bv.bv_val = old_rdn_val;
569 del_bv.bv_len = strlen(old_rdn_val);
574 mod[1].sml_desc = NULL;
575 rc = slap_str2ad( old_rdn_type, &mod[1].sml_desc, &text );
577 if( rc != LDAP_SUCCESS ) {
579 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
580 "ldbm_back_modrdn: %s: %s (old)\n",
581 text, old_rdn_type ));
583 Debug( LDAP_DEBUG_TRACE,
584 "ldbm_back_modrdn: %s: %s (old)\n",
585 text, old_rdn_type, 0 );
588 send_ldap_result( conn, op, rc,
589 NULL, text, NULL, NULL );
593 mod[0].sml_next = &mod[1];
594 mod[1].sml_bvalues = del_bvals;
595 mod[1].sml_op = LDAP_MOD_DELETE;
596 mod[1].sml_next = NULL;
599 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
600 "ldbm_back_modrdn: removing old_rdn_val=%s\n", old_rdn_val ));
602 Debug( LDAP_DEBUG_TRACE,
603 "ldbm_back_modrdn: removing old_rdn_val=%s\n",
609 /* check for abandon */
610 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
611 if ( op->o_abandon ) {
612 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
615 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
618 if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
619 send_ldap_result( conn, op, LDAP_OTHER,
620 NULL, "DN index delete fail", NULL, NULL );
624 (void) cache_delete_entry( &li->li_cache, e );
626 /* XXX: there is no going back! */
636 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
637 send_ldap_result( conn, op, LDAP_OTHER,
638 NULL, "DN index add failed", NULL, NULL );
642 /* modify memory copy of entry */
643 rc = ldbm_modify_internal( be, conn, op, dn, &mod[0], e, &text );
645 if( rc != LDAP_SUCCESS ) {
646 if( rc != SLAPD_ABANDON ) {
647 send_ldap_result( conn, op, rc,
648 NULL, text, NULL, NULL );
654 (void) cache_update_entry( &li->li_cache, e );
656 /* NOTE: after this you must not free new_dn or new_ndn!
657 * They are used by cache.
661 if ( id2entry_add( be, e ) != 0 ) {
663 send_ldap_result( conn, op, LDAP_OTHER,
664 NULL, "entry update failed", NULL, NULL );
668 send_ldap_result( conn, op, LDAP_SUCCESS,
669 NULL, NULL, NULL, NULL );
673 if( new_dn != NULL ) free( new_dn );
674 if( new_ndn != NULL ) free( new_ndn );
676 if( p_dn != NULL ) free( p_dn );
677 if( p_ndn != NULL ) free( p_ndn );
679 /* LDAP v2 supporting correct attribute handling. */
680 if( new_rdn_type != NULL ) free(new_rdn_type);
681 if( new_rdn_val != NULL ) free(new_rdn_val);
682 if( old_rdn != NULL ) free(old_rdn);
683 if( old_rdn_type != NULL ) free(old_rdn_type);
684 if( old_rdn_val != NULL ) free(old_rdn_val);
687 /* LDAP v3 Support */
688 if ( np_dn != NULL ) free( np_dn );
689 if ( np_ndn != NULL ) free( np_ndn );
692 /* free new parent and writer lock */
693 cache_return_entry_w( &li->li_cache, np );
697 /* free parent and writer lock */
698 cache_return_entry_w( &li->li_cache, p );
702 /* release root writer lock */
703 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
706 /* free entry and writer lock */
707 cache_return_entry_w( &li->li_cache, e );