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 ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
482 /* Not a big deal but we may say something */
484 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
485 "ldbm_back_modrdn: old_rdn_type=%s new_rdn_type-%s\n",
486 old_rdn_type, new_rdn_type ));
488 Debug( LDAP_DEBUG_TRACE,
489 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
490 old_rdn_type, new_rdn_type, 0 );
496 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
497 "ldbm_back_modrdn: DN_X500\n" ));
499 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
504 /* Add new attribute value to the entry.
507 add_bvals[0] = &add_bv; /* Array of bervals */
510 add_bv.bv_val = new_rdn_val;
511 add_bv.bv_len = strlen(new_rdn_val);
516 mod[0].sml_desc = NULL;
517 rc = slap_str2ad( new_rdn_type, &mod[0].sml_desc, &text );
519 if( rc != LDAP_SUCCESS ) {
521 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
522 "ldbm_back_modrdn: slap_str2ad error: %s (%s)\n",
523 text, new_rdn_type ));
525 Debug( LDAP_DEBUG_TRACE,
526 "ldbm_back_modrdn: %s: %s (new)\n",
527 text, new_rdn_type, 0 );
530 send_ldap_result( conn, op, rc,
531 NULL, text, NULL, NULL );
535 mod[0].sml_bvalues = add_bvals;
536 mod[0].sml_op = SLAP_MOD_SOFTADD;
537 mod[0].sml_next = NULL;
539 /* Remove old rdn value if required */
542 /* Get value of old rdn */
544 if ((old_rdn_val = rdn_attr_value( old_rdn ))
548 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
549 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n" ));
551 Debug( LDAP_DEBUG_TRACE,
552 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
556 send_ldap_result( conn, op, LDAP_OTHER,
557 NULL, "could not parse value from old RDN", NULL, NULL );
561 del_bvals[0] = &del_bv; /* Array of bervals */
564 /* Remove old value of rdn as an attribute. */
566 del_bv.bv_val = old_rdn_val;
567 del_bv.bv_len = strlen(old_rdn_val);
572 mod[1].sml_desc = NULL;
573 rc = slap_str2ad( old_rdn_type, &mod[1].sml_desc, &text );
575 if( rc != LDAP_SUCCESS ) {
577 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
578 "ldbm_back_modrdn: %s: %s (old)\n",
579 text, old_rdn_type ));
581 Debug( LDAP_DEBUG_TRACE,
582 "ldbm_back_modrdn: %s: %s (old)\n",
583 text, old_rdn_type, 0 );
586 send_ldap_result( conn, op, rc,
587 NULL, text, NULL, NULL );
591 mod[0].sml_next = &mod[1];
592 mod[1].sml_bvalues = del_bvals;
593 mod[1].sml_op = LDAP_MOD_DELETE;
594 mod[1].sml_next = NULL;
597 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
598 "ldbm_back_modrdn: removing old_rdn_val=%s\n", old_rdn_val ));
600 Debug( LDAP_DEBUG_TRACE,
601 "ldbm_back_modrdn: removing old_rdn_val=%s\n",
607 /* check for abandon */
608 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
609 if ( op->o_abandon ) {
610 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
613 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
616 if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
617 send_ldap_result( conn, op, LDAP_OTHER,
618 NULL, "DN index delete fail", NULL, NULL );
622 (void) cache_delete_entry( &li->li_cache, e );
624 /* XXX: there is no going back! */
634 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
635 send_ldap_result( conn, op, LDAP_OTHER,
636 NULL, "DN index add failed", NULL, NULL );
640 /* modify memory copy of entry */
641 rc = ldbm_modify_internal( be, conn, op, dn, &mod[0], e, &text );
643 if( rc != LDAP_SUCCESS ) {
644 if( rc != SLAPD_ABANDON ) {
645 send_ldap_result( conn, op, rc,
646 NULL, text, NULL, NULL );
652 (void) cache_update_entry( &li->li_cache, e );
654 /* NOTE: after this you must not free new_dn or new_ndn!
655 * They are used by cache.
659 if ( id2entry_add( be, e ) != 0 ) {
661 send_ldap_result( conn, op, LDAP_OTHER,
662 NULL, "entry update failed", NULL, NULL );
666 send_ldap_result( conn, op, LDAP_SUCCESS,
667 NULL, NULL, NULL, NULL );
671 if( new_dn != NULL ) free( new_dn );
672 if( new_ndn != NULL ) free( new_ndn );
674 if( p_dn != NULL ) free( p_dn );
675 if( p_ndn != NULL ) free( p_ndn );
677 /* LDAP v2 supporting correct attribute handling. */
678 if( new_rdn_type != NULL ) free(new_rdn_type);
679 if( new_rdn_val != NULL ) free(new_rdn_val);
680 if( old_rdn != NULL ) free(old_rdn);
681 if( old_rdn_type != NULL ) free(old_rdn_type);
682 if( old_rdn_val != NULL ) free(old_rdn_val);
685 /* LDAP v3 Support */
686 if ( np_dn != NULL ) free( np_dn );
687 if ( np_ndn != NULL ) free( np_ndn );
690 /* free new parent and writer lock */
691 cache_return_entry_w( &li->li_cache, np );
695 /* free parent and writer lock */
696 cache_return_entry_w( &li->li_cache, p );
700 /* release root writer lock */
701 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
704 /* free entry and writer lock */
705 cache_return_entry_w( &li->li_cache, e );