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;
54 int rc = -1, rc_id = 0;
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 char *new_rdn_val = NULL; /* Val of new rdn */
61 char *new_rdn_type = NULL; /* Type of new rdn */
62 char *old_rdn = NULL; /* Old rdn's attr type & val */
63 char *old_rdn_type = NULL; /* Type of old rdn attr. */
64 char *old_rdn_val = NULL; /* Old rdn attribute value */
65 /* Added to support newSuperior */
66 Entry *np = NULL; /* newSuperior Entry */
67 char *np_dn = NULL; /* newSuperior dn */
68 char *np_ndn = NULL; /* newSuperior ndn */
69 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
70 /* Used to interface with ldbm_modify_internal() */
71 struct berval add_bv; /* Stores new rdn att */
72 struct berval *add_bvals[2]; /* Stores new rdn att */
73 struct berval del_bv; /* Stores old rdn att */
74 struct berval *del_bvals[2]; /* Stores old rdn att */
75 Modifications mod[2]; /* Used to delete old rdn */
76 int manageDSAit = get_manageDSAit( op );
79 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
80 "ldbm_back_modrdn: dn: %s newSuperior=%s\n",
81 dn ? dn : "NULL", newSuperior ? newSuperior : "NULL" ));
83 Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
84 (newSuperior ? newSuperior : "NULL"),
88 /* get entry with writer lock */
89 if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
90 char* matched_dn = NULL;
91 struct berval** refs = NULL;
93 if( matched != NULL ) {
94 matched_dn = strdup( matched->e_dn );
95 refs = is_entry_referral( matched )
96 ? get_entry_referrals( be, conn, op, matched )
98 cache_return_entry_r( &li->li_cache, matched );
100 refs = default_referral;
103 send_ldap_result( conn, op, LDAP_REFERRAL,
104 matched_dn, NULL, refs, NULL );
106 if ( matched != NULL ) {
107 ber_bvecfree( refs );
114 if (!manageDSAit && is_entry_referral( e ) ) {
115 /* parent is a referral, don't allow add */
116 /* parent is an alias, don't allow add */
117 struct berval **refs = get_entry_referrals( be,
121 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
122 "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn ));
124 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
128 send_ldap_result( conn, op, LDAP_REFERRAL,
129 e->e_dn, NULL, refs, NULL );
131 ber_bvecfree( refs );
135 if ( has_children( be, e ) ) {
137 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
138 "ldbm_back_modrdn: entry %s has children\n", e->e_dn ));
140 Debug( LDAP_DEBUG_TRACE, "entry %s referral\n", 0,
144 send_ldap_result( conn, op, LDAP_NOT_ALLOWED_ON_NONLEAF,
145 NULL, "subtree rename not supported", NULL, NULL );
149 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
150 /* Make sure parent entry exist and we can write its
154 if( (p = dn2entry_w( be, p_ndn, NULL )) == NULL) {
156 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
157 "ldbm_back_modrdn: parent of %s does not exist\n", e->e_ndn ));
159 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
163 send_ldap_result( conn, op, LDAP_OTHER,
164 NULL, "parent entry does not exist", NULL, NULL );
169 /* check parent for "children" acl */
170 if ( ! access_allowed( be, conn, op, p,
171 children, NULL, ACL_WRITE ) )
174 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
175 "ldbm_back_modrdn: no access to parent of (%s)\n", e->e_dn ));
177 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
181 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
182 NULL, NULL, NULL, NULL );
187 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
188 "ldbm_back_modrdn: wr to children of entry %s OK\n",
191 Debug( LDAP_DEBUG_TRACE,
192 "ldbm_back_modrdn: wr to children of entry %s OK\n",
196 p_dn = dn_parent( be, e->e_dn );
199 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
200 "ldbm_back_modrdn: parent dn=%s\n", p_dn ));
202 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
207 /* no parent, modrdn entry directly under root */
208 if( ! be_isroot( be, op->o_ndn ) ) {
210 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
211 "ldbm_back_modrdn: (%s) no parent & not a root.\n",
214 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
218 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
219 NULL, NULL, NULL, NULL );
223 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
227 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
228 "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn ));
230 Debug( LDAP_DEBUG_TRACE,
231 "ldbm_back_modrdn: no parent, locked root\n",
236 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
238 if ( newSuperior != NULL ) {
240 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
241 "ldbm_back_modrdn: new parent \"%s\" requested\n", newSuperior ));
243 Debug( LDAP_DEBUG_TRACE,
244 "ldbm_back_modrdn: new parent \"%s\" requested...\n",
248 np_dn = ch_strdup( newSuperior );
249 np_ndn = ch_strdup( np_dn );
250 (void) dn_normalize( np_ndn );
252 /* newSuperior == oldParent? */
253 if ( strcmp( p_ndn, np_ndn ) == 0 ) {
255 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
256 "ldbm_back_modrdn: new parent\"%s\" seems to be the same as the old parent \"%s\"\n",
257 newSuperior, p_dn ));
259 Debug( LDAP_DEBUG_TRACE,
260 "ldbm_back_modrdn: new parent \"%s\" seems to be the same as old parent \"%s\"...\n",
261 newSuperior, p_dn, 0 );
264 newSuperior = NULL; /* ignore newSuperior */
268 if ( newSuperior != NULL ) {
269 /* newSuperior == entry being moved?, if so ==> ERROR */
270 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
272 if( (np = dn2entry_w( be, np_ndn, NULL )) == NULL) {
274 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
275 "ldbm_back_modrdn: newSup(ndn=%s) not found.\n", np_ndn ));
277 Debug( LDAP_DEBUG_TRACE,
278 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
282 send_ldap_result( conn, op, LDAP_OTHER,
283 NULL, "newSuperior not found", NULL, NULL );
288 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
289 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
292 Debug( LDAP_DEBUG_TRACE,
293 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
297 /* check newSuperior for "children" acl */
298 if ( !access_allowed( be, conn, op, np, children, NULL,
302 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
303 "ldbm_back_modrdn: no wr to newSup children.\n" ));
305 Debug( LDAP_DEBUG_TRACE,
306 "ldbm_back_modrdn: no wr to newSup children\n",
310 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
311 NULL, NULL, NULL, NULL );
315 if ( is_entry_alias( np ) ) {
316 /* entry is an alias, don't allow bind */
318 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
319 "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn ));
321 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
326 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
327 NULL, "newSuperior is an alias", NULL, NULL );
332 if ( is_entry_referral( np ) ) {
333 /* parent is a referral, don't allow add */
334 /* parent is an alias, don't allow add */
336 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
337 "ldbm_back_modrdn: entry (%s) is a referral\n",
340 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
344 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
345 NULL, "newSuperior is a referral", NULL, NULL );
351 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
352 "ldbm_back_modrdn: wr to new parent's children OK.\n" ));
354 Debug( LDAP_DEBUG_TRACE,
355 "ldbm_back_modrdn: wr to new parent's children OK\n",
359 new_parent_dn = np_dn;
362 /* Build target dn and make sure target entry doesn't exist already. */
363 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
365 new_ndn = ch_strdup(new_dn);
366 (void) dn_normalize( new_ndn );
369 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
370 "ldbm_back_modrdn: new ndn=%s\n", new_ndn ));
372 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
376 /* check for abandon */
377 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
378 if ( op->o_abandon ) {
379 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
383 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
384 if ( ( rc_id = dn2id ( be, new_ndn, &id ) ) || id != NOID ) {
385 /* if (rc_id) something bad happened to ldbm cache */
386 send_ldap_result( conn, op,
387 rc_id ? LDAP_OPERATIONS_ERROR : LDAP_ALREADY_EXISTS,
388 NULL, NULL, NULL, NULL );
393 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
394 "ldbm_back_modrdn: new ndn (%s) does not exist\n", new_ndn ));
396 Debug( LDAP_DEBUG_TRACE,
397 "ldbm_back_modrdn: new ndn=%s does not exist\n",
401 /* Get attribute type and attribute value of our new rdn, we will
402 * need to add that to our new entry
404 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 );
419 if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
421 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
422 "ldbm_back_modrdn: can't figure out val of newrdn\n"));
424 Debug( LDAP_DEBUG_TRACE,
425 "ldbm_back_modrdn: can't figure out val of newrdn\n",
429 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
430 NULL, "could not parse RDN value", NULL, NULL );
435 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
436 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
437 new_rdn_val, new_rdn_type ));
439 Debug( LDAP_DEBUG_TRACE,
440 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
441 new_rdn_val, new_rdn_type, 0 );
444 /* Retrieve the old rdn from the entry's dn */
445 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
447 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
448 "ldbm_back_modrdn: can't figure out old_rdn from dn (%s)\n",
451 Debug( LDAP_DEBUG_TRACE,
452 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
456 send_ldap_result( conn, op, LDAP_OTHER,
457 NULL, "could not parse old DN", NULL, NULL );
461 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
463 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
464 "ldbm_back_modrdn: can't figure out the old_rdn type.\n" ));
466 Debug( LDAP_DEBUG_TRACE,
467 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
471 send_ldap_result( conn, op, LDAP_OTHER,
472 NULL, "count parse RDN from old DN", NULL, NULL );
476 if ( newSuperior == NULL
477 && strcasecmp( old_rdn_type, new_rdn_type ) != 0 )
479 /* Not a big deal but we may say something */
481 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
482 "ldbm_back_modrdn: old_rdn_type=%s new_rdn_type=%s\n",
483 old_rdn_type, new_rdn_type ));
485 Debug( LDAP_DEBUG_TRACE,
486 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
487 old_rdn_type, new_rdn_type, 0 );
492 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
493 "ldbm_back_modrdn: DN_X500\n" ));
495 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
499 /* Add new attribute value to the entry.
502 add_bvals[0] = &add_bv; /* Array of bervals */
505 add_bv.bv_val = new_rdn_val;
506 add_bv.bv_len = strlen(new_rdn_val);
511 mod[0].sml_desc = NULL;
512 rc = slap_str2ad( new_rdn_type, &mod[0].sml_desc, &text );
514 if( rc != LDAP_SUCCESS ) {
516 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
517 "ldbm_back_modrdn: slap_str2ad error: %s (%s)\n",
518 text, new_rdn_type ));
520 Debug( LDAP_DEBUG_TRACE,
521 "ldbm_back_modrdn: %s: %s (new)\n",
522 text, new_rdn_type, 0 );
525 send_ldap_result( conn, op, rc,
526 NULL, text, NULL, NULL );
531 mod[0].sml_bvalues = add_bvals;
532 mod[0].sml_op = SLAP_MOD_SOFTADD;
533 mod[0].sml_next = NULL;
535 /* Remove old rdn value if required */
537 /* Get value of old rdn */
539 if ((old_rdn_val = rdn_attr_value( old_rdn )) == NULL) {
541 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
542 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n" ));
544 Debug( LDAP_DEBUG_TRACE,
545 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
549 send_ldap_result( conn, op, LDAP_OTHER,
550 NULL, "could not parse value from old RDN", NULL, NULL );
554 del_bvals[0] = &del_bv; /* Array of bervals */
557 /* Remove old value of rdn as an attribute. */
559 del_bv.bv_val = old_rdn_val;
560 del_bv.bv_len = strlen(old_rdn_val);
565 mod[1].sml_desc = NULL;
566 rc = slap_str2ad( old_rdn_type, &mod[1].sml_desc, &text );
568 if( rc != LDAP_SUCCESS ) {
570 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
571 "ldbm_back_modrdn: %s: %s (old)\n",
572 text, old_rdn_type ));
574 Debug( LDAP_DEBUG_TRACE,
575 "ldbm_back_modrdn: %s: %s (old)\n",
576 text, old_rdn_type, 0 );
579 send_ldap_result( conn, op, rc,
580 NULL, text, NULL, NULL );
585 mod[0].sml_next = &mod[1];
586 mod[1].sml_bvalues = del_bvals;
587 mod[1].sml_op = LDAP_MOD_DELETE;
588 mod[1].sml_next = NULL;
591 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
592 "ldbm_back_modrdn: removing old_rdn_val=%s\n", old_rdn_val ));
594 Debug( LDAP_DEBUG_TRACE,
595 "ldbm_back_modrdn: removing old_rdn_val=%s\n",
600 /* check for abandon */
601 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
602 if ( op->o_abandon ) {
603 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
606 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
609 if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
610 send_ldap_result( conn, op, LDAP_OTHER,
611 NULL, "DN index delete fail", NULL, NULL );
615 (void) cache_delete_entry( &li->li_cache, e );
617 /* XXX: there is no going back! */
627 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
628 send_ldap_result( conn, op, LDAP_OTHER,
629 NULL, "DN index add failed", NULL, NULL );
633 /* modify memory copy of entry */
634 rc = ldbm_modify_internal( be, conn, op, dn, &mod[0], e,
635 &text, textbuf, textlen );
637 if( rc != LDAP_SUCCESS ) {
638 if( rc != SLAPD_ABANDON ) {
639 send_ldap_result( conn, op, rc,
640 NULL, text, NULL, NULL );
646 (void) cache_update_entry( &li->li_cache, e );
648 /* NOTE: after this you must not free new_dn or new_ndn!
649 * They are used by cache.
653 if ( id2entry_add( be, e ) != 0 ) {
655 send_ldap_result( conn, op, LDAP_OTHER,
656 NULL, "entry update failed", NULL, NULL );
660 send_ldap_result( conn, op, LDAP_SUCCESS,
661 NULL, NULL, NULL, NULL );
665 if( new_dn != NULL ) free( new_dn );
666 if( new_ndn != NULL ) free( new_ndn );
668 if( p_dn != NULL ) free( p_dn );
669 if( p_ndn != NULL ) free( p_ndn );
671 /* LDAP v2 supporting correct attribute handling. */
672 if( new_rdn_type != NULL ) free(new_rdn_type);
673 if( new_rdn_val != NULL ) free(new_rdn_val);
674 if( old_rdn != NULL ) free(old_rdn);
675 if( old_rdn_type != NULL ) free(old_rdn_type);
676 if( old_rdn_val != NULL ) free(old_rdn_val);
678 /* LDAP v3 Support */
679 if ( np_dn != NULL ) free( np_dn );
680 if ( np_ndn != NULL ) free( np_ndn );
683 /* free new parent and writer lock */
684 cache_return_entry_w( &li->li_cache, np );
688 /* free parent and writer lock */
689 cache_return_entry_w( &li->li_cache, p );
693 /* release root writer lock */
694 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
697 /* free entry and writer lock */
698 cache_return_entry_w( &li->li_cache, e );