1 /* modrdn.c - ldbm backend modrdn routine */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * LDAP v3 newSuperior support. Add new rdn as an attribute.
9 * (Full support for v2 also used software/ideas contributed
10 * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
13 * Copyright 1999, Juan C. Gomez, All rights reserved.
14 * This software is not subject to any license of Silicon Graphics
15 * Inc. or Purdue University.
17 * Redistribution and use in source and binary forms are permitted
18 * without restriction or fee of any kind as long as this notice
27 #include <ac/string.h>
28 #include <ac/socket.h>
31 #include "back-ldbm.h"
32 #include "proto-back-ldbm.h"
45 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
46 char *p_dn = NULL, *p_ndn = NULL;
47 char *new_dn = NULL, *new_ndn = NULL;
49 Entry *matched = NULL;
52 /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
53 char *new_rdn_val = NULL; /* Val of new rdn */
54 char *new_rdn_type = NULL; /* Type of new rdn */
55 char *old_rdn; /* Old rdn's attr type & val */
56 char *old_rdn_type = NULL; /* Type of old rdn attr. */
57 char *old_rdn_val = NULL; /* Old rdn attribute value */
58 /* Added to support newSuperior */
59 Entry *np = NULL; /* newSuperior Entry */
60 char *np_dn = NULL; /* newSuperior dn */
61 char *np_ndn = NULL; /* newSuperior ndn */
62 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
63 /* Used to interface with ldbm_modify_internal() */
64 struct berval add_bv; /* Stores new rdn att */
65 struct berval *add_bvals[2]; /* Stores new rdn att */
66 struct berval del_bv; /* Stores old rdn att */
67 struct berval *del_bvals[2]; /* Stores old rdn att */
68 LDAPModList mod[2]; /* Used to delete old rdn */
69 int manageDSAit = get_manageDSAit( op );
71 Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
72 (newSuperior ? newSuperior : "NULL"),
75 /* get entry with writer lock */
76 if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
77 char* matched_dn = NULL;
78 struct berval** refs = NULL;
80 if( matched != NULL ) {
81 matched_dn = strdup( matched->e_dn );
82 refs = is_entry_referral( matched )
83 ? get_entry_referrals( be, conn, op, matched )
85 cache_return_entry_r( &li->li_cache, matched );
87 refs = default_referral;
90 send_ldap_result( conn, op, LDAP_REFERRAL,
91 matched_dn, NULL, refs, NULL );
93 if ( matched != NULL ) {
101 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
102 if ( ! access_allowed( be, conn, op, e,
103 "entry", NULL, ACL_WRITE ) )
105 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
107 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
108 NULL, NULL, NULL, NULL );
113 if (!manageDSAit && is_entry_referral( e ) ) {
114 /* parent is a referral, don't allow add */
115 /* parent is an alias, don't allow add */
116 struct berval **refs = get_entry_referrals( be,
119 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
122 send_ldap_result( conn, op, LDAP_REFERRAL,
123 e->e_dn, NULL, refs, NULL );
125 ber_bvecfree( refs );
130 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
132 /* Make sure parent entry exist and we can write its
136 if( (p = dn2entry_w( be, p_ndn, &matched )) == NULL) {
137 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
139 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
140 NULL, NULL, NULL, NULL );
144 /* check parent for "children" acl */
145 if ( ! access_allowed( be, conn, op, p,
146 "children", NULL, ACL_WRITE ) )
148 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
150 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
151 NULL, NULL, NULL, NULL );
155 Debug( LDAP_DEBUG_TRACE,
156 "ldbm_back_modrdn: wr to children of entry %s OK\n",
159 p_dn = dn_parent( be, e->e_dn );
162 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
166 /* no parent, modrdn entry directly under root */
167 if( ! be_isroot( be, op->o_ndn ) ) {
168 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
170 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
171 NULL, NULL, NULL, NULL );
175 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
178 Debug( LDAP_DEBUG_TRACE,
179 "ldbm_back_modrdn: no parent, locked root\n",
184 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
186 if ( (np_dn = newSuperior) != NULL) {
187 Debug( LDAP_DEBUG_TRACE,
188 "ldbm_back_modrdn: new parent requested...\n",
191 np_ndn = ch_strdup( np_dn );
192 (void) dn_normalize_case( np_ndn );
194 /* newSuperior == oldParent?, if so ==> ERROR */
195 /* newSuperior == entry being moved?, if so ==> ERROR */
196 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
198 if( (np = dn2entry_w( be, np_ndn, &matched )) == NULL) {
199 Debug( LDAP_DEBUG_TRACE,
200 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
202 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
203 NULL, NULL, NULL, NULL );
207 Debug( LDAP_DEBUG_TRACE,
208 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
211 /* check newSuperior for "children" acl */
212 if ( !access_allowed( be, conn, op, np, "children", NULL,
215 Debug( LDAP_DEBUG_TRACE,
216 "ldbm_back_modrdn: no wr to newSup children\n",
218 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
219 NULL, NULL, NULL, NULL );
223 if ( is_entry_alias( np ) ) {
224 /* entry is an alias, don't allow bind */
225 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
228 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
229 NULL, NULL, NULL, NULL );
234 if ( is_entry_referral( np ) ) {
235 /* parent is a referral, don't allow add */
236 /* parent is an alias, don't allow add */
237 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
240 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
241 NULL, NULL, NULL, NULL );
246 Debug( LDAP_DEBUG_TRACE,
247 "ldbm_back_modrdn: wr to new parent's children OK\n",
250 new_parent_dn = np_dn;
253 /* Build target dn and make sure target entry doesn't exist already. */
255 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
258 new_ndn = ch_strdup(new_dn);
259 (void) dn_normalize_case( new_ndn );
261 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
264 if (dn2id ( be, new_ndn ) != NOID) {
265 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
266 NULL, NULL, NULL, NULL );
270 Debug( LDAP_DEBUG_TRACE,
271 "ldbm_back_modrdn: new ndn=%s does not exist\n",
274 /* check for abandon */
275 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
276 if ( op->o_abandon ) {
277 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
280 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
283 if ( dn2id_delete( be, e->e_ndn ) != 0 ) {
284 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
285 NULL, NULL, NULL, NULL );
289 (void) cache_delete_entry( &li->li_cache, e );
296 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
297 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
298 NULL, NULL, NULL, NULL );
303 /* Get attribute type and attribute value of our new rdn, we will
304 * need to add that to our new entry
307 if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
309 Debug( LDAP_DEBUG_TRACE,
310 "ldbm_back_modrdn: can't figure out type of newrdn\n",
312 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
313 NULL, NULL, NULL, NULL );
318 if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
320 Debug( LDAP_DEBUG_TRACE,
321 "ldbm_back_modrdn: can't figure out val of newrdn\n",
323 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
324 NULL, NULL, NULL, NULL );
329 Debug( LDAP_DEBUG_TRACE,
330 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
331 new_rdn_val, new_rdn_type, 0 );
333 /* Retrieve the old rdn from the entry's dn */
335 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
337 Debug( LDAP_DEBUG_TRACE,
338 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
340 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
341 NULL, NULL, NULL, NULL );
346 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
348 Debug( LDAP_DEBUG_TRACE,
349 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
351 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
352 NULL, NULL, NULL, NULL );
357 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
359 /* Not a big deal but we may say something */
360 Debug( LDAP_DEBUG_TRACE,
361 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
362 old_rdn_type, new_rdn_type, 0 );
367 if ( dn_type( old_rdn ) == DN_X500 ) {
370 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
373 /* Add new attribute value to the entry.
376 add_bvals[0] = &add_bv; /* Array of bervals */
379 add_bv.bv_val = new_rdn_val;
380 add_bv.bv_len = strlen(new_rdn_val);
382 mod[0].ml_type = new_rdn_type;
383 mod[0].ml_bvalues = add_bvals;
384 mod[0].ml_op = LDAP_MOD_SOFTADD;
385 mod[0].ml_next = NULL;
387 /* Remove old rdn value if required */
390 /* Get value of old rdn */
392 if ((old_rdn_val = rdn_attr_value( old_rdn ))
395 Debug( LDAP_DEBUG_TRACE,
396 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
398 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
399 NULL, NULL, NULL, NULL );
403 del_bvals[0] = &del_bv; /* Array of bervals */
406 /* Remove old value of rdn as an attribute. */
408 del_bv.bv_val = old_rdn_val;
409 del_bv.bv_len = strlen(old_rdn_val);
411 /* No need to normalize old_rdn_type, delete_values()
414 mod[0].ml_next = &mod[1];
415 mod[1].ml_type = old_rdn_type;
416 mod[1].ml_bvalues = del_bvals;
417 mod[1].ml_op = LDAP_MOD_DELETE;
418 mod[1].ml_next = NULL;
420 Debug( LDAP_DEBUG_TRACE,
421 "ldbm_back_modrdn: removing old_rdn_val=%s\n",
427 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
429 /* XXXV3: not sure of what to do here */
430 Debug( LDAP_DEBUG_TRACE,
431 "ldbm_back_modrdn: not fully implemented...\n",
434 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
435 NULL, NULL, NULL, NULL );
441 /* modify memory copy of entry */
442 if ( ldbm_modify_internal( be, conn, op, dn, &mod[0], e )
449 (void) cache_update_entry( &li->li_cache, e );
451 /* NOTE: after this you must not free new_dn or new_ndn!
452 * They are used by cache.
456 if ( id2entry_add( be, e ) != 0 ) {
458 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
459 NULL, NULL, NULL, NULL );
460 goto return_results_after;
463 send_ldap_result( conn, op, LDAP_SUCCESS,
464 NULL, NULL, NULL, NULL );
466 goto return_results_after;
469 if( new_dn != NULL ) free( new_dn );
470 if( new_ndn != NULL ) free( new_ndn );
472 return_results_after:
474 * new_dn and new_ndn are not deallocated because they are used by
477 if( p_dn != NULL ) free( p_dn );
478 if( p_ndn != NULL ) free( p_ndn );
480 if( matched != NULL ) free( matched );
482 /* LDAP v2 supporting correct attribute handling. */
483 if( new_rdn_type != NULL ) free(new_rdn_type);
484 if( new_rdn_val != NULL ) free(new_rdn_val);
485 if( old_rdn != NULL ) free(old_rdn);
486 if( old_rdn_type != NULL ) free(old_rdn_type);
487 if( old_rdn_val != NULL ) free(old_rdn_val);
490 /* LDAP v3 Support */
491 if ( np_dn != NULL ) free( np_dn );
492 if ( np_ndn != NULL ) free( np_ndn );
495 /* free new parent and writer lock */
496 cache_return_entry_w( &li->li_cache, np );
500 /* free parent and writer lock */
501 cache_return_entry_w( &li->li_cache, p );
505 /* release root writer lock */
506 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
509 /* free entry and writer lock */
510 cache_return_entry_w( &li->li_cache, e );