1 /* modrdn.c - ldbm backend modrdn routine */
4 * LDAP v3 newSuperior support. Add new rdn as an attribute.
5 * (Full support for v2 also used software/ideas contributed
6 * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
9 * Copyright 1999, Juan C. Gomez, All rights reserved.
10 * This software is not subject to any license of Silicon Graphics
11 * Inc. or Purdue University.
13 * Redistribution and use in source and binary forms are permitted
14 * without restriction or fee of any kind as long as this notice
23 #include <ac/string.h>
24 #include <ac/socket.h>
27 #include "back-ldbm.h"
28 #include "proto-back-ldbm.h"
41 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
43 char *p_dn = NULL, *p_ndn = NULL;
44 char *new_dn = NULL, *new_ndn = NULL;
48 /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
49 char *new_rdn_val = NULL; /* Val of new rdn */
50 char *new_rdn_type = NULL; /* Type of new rdn */
51 char *old_rdn; /* Old rdn's attr type & val */
52 char *old_rdn_type = NULL; /* Type of old rdn attr. */
53 char *old_rdn_val = NULL; /* Old rdn attribute value */
54 struct berval bv; /* Stores new rdn att */
55 struct berval *bvals[2]; /* Stores new rdn att */
56 LDAPMod mod; /* Used to delete old rdn */
57 /* Added to support newSuperior */
58 Entry *np = NULL; /* newSuperior Entry */
59 char *np_dn = NULL; /* newSuperior dn */
60 char *np_ndn = NULL; /* newSuperior ndn */
61 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
63 Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
64 (newSuperior ? newSuperior : "NULL"),
67 /* get entry with writer lock */
68 if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
69 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, "" );
70 if ( matched != NULL ) {
76 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
77 /* check parent for "children" acl */
78 if ( ! access_allowed( be, conn, op, e,
79 "entry", NULL, ACL_WRITE ) )
81 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
83 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
89 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
91 /* Make sure parent entry exist and we can write its
95 if( (p = dn2entry_w( be, p_ndn, &matched )) == NULL) {
96 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
98 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
103 #ifndef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
104 /* check parent for "children" acl */
105 if ( ! access_allowed( be, conn, op, p,
106 "children", NULL, ACL_WRITE ) )
108 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
110 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
115 Debug( LDAP_DEBUG_TRACE,
116 "ldbm_back_modrdn: wr to children of entry %s OK\n",
120 p_dn = dn_parent( be, e->e_dn );
123 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
127 /* no parent, modrdn entry directly under root */
128 if( ! be_isroot( be, op->o_ndn ) ) {
129 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
131 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
136 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
139 Debug( LDAP_DEBUG_TRACE,
140 "ldbm_back_modrdn: no parent, locked root\n",
143 }/* if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) else */
145 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
147 if ( (np_dn = newSuperior) != NULL) {
150 Debug( LDAP_DEBUG_TRACE,
151 "ldbm_back_modrdn: new parent requested...\n",
154 np_ndn = dn_normalize_case( ch_strdup( np_dn ) );
156 /* newSuperior == oldParent?, if so ==> ERROR */
158 /* newSuperior == entry being moved?, if so ==> ERROR */
160 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
162 if( (np = dn2entry_w( be, np_ndn, &matched )) == NULL) {
164 Debug( LDAP_DEBUG_TRACE,
165 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
167 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "",
172 Debug( LDAP_DEBUG_TRACE,
173 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
176 #ifndef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
177 /* check newSuperior for "children" acl */
178 if ( !access_allowed( be, conn, op, np, "children", NULL,
181 Debug( LDAP_DEBUG_TRACE,
182 "ldbm_back_modrdn: no wr to newSup children\n",
184 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
190 Debug( LDAP_DEBUG_TRACE,
191 "ldbm_back_modrdn: wr to new parent's children OK\n",
195 new_parent_dn = np_dn;
199 /* Build target dn and make sure target entry doesn't exist already. */
201 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
204 new_ndn = dn_normalize_case( ch_strdup(new_dn) );
206 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
209 if (dn2id ( be, new_ndn ) != NOID) {
210 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, NULL, NULL );
214 Debug( LDAP_DEBUG_TRACE,
215 "ldbm_back_modrdn: new ndn=%s does not exist\n",
218 /* check for abandon */
219 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
220 if ( op->o_abandon ) {
221 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
224 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
227 if ( dn2id_add( be, new_ndn, e->e_id ) != 0 ) {
228 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
233 if ( dn2id_delete( be, e->e_ndn ) != 0 ) {
234 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
238 (void) cache_delete_entry( &li->li_cache, e );
244 /* Get attribute type and attribute value of our new rdn, we will
245 * need to add that to our new entry
248 if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
250 Debug( LDAP_DEBUG_TRACE,
251 "ldbm_back_modrdn: can't figure out type of newrdn\n",
253 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
258 if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
260 Debug( LDAP_DEBUG_TRACE,
261 "ldbm_back_modrdn: can't figure out val of newrdn\n",
263 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
268 Debug( LDAP_DEBUG_TRACE,
269 "ldbm_back_modrdn: new_rdn_val=%s, new_rdn_type=%s\n",
270 new_rdn_val, new_rdn_type, 0 );
272 /* Retrieve the old rdn from the entry's dn */
274 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
276 Debug( LDAP_DEBUG_TRACE,
277 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
279 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
284 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
286 Debug( LDAP_DEBUG_TRACE,
287 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
289 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
294 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
296 /* Not a big deal but we may say something */
297 Debug( LDAP_DEBUG_TRACE,
298 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
299 old_rdn_type, new_rdn_type, 0 );
303 if ( dn_type( old_rdn ) == DN_X500 ) {
305 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
308 bvals[0] = &bv; /* Array of bervals */
311 /* Remove old rdn value if required */
315 /* Get value of old rdn */
317 if ((old_rdn_val = rdn_attr_value( old_rdn ))
320 Debug( LDAP_DEBUG_TRACE,
321 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
323 send_ldap_result( conn, op,
324 LDAP_OPERATIONS_ERROR,
331 /* Remove old value of rdn as an attribute. */
333 bv.bv_val = old_rdn_val;
334 bv.bv_len = strlen(old_rdn_val);
336 /* No need to normalize old_rdn_type, delete_values()
339 mod.mod_type = old_rdn_type;
340 mod.mod_bvalues = bvals;
341 mod.mod_op = LDAP_MOD_DELETE; /* XXX:really needed?*/
343 /* Assembly mod structure */
345 if ( delete_values( e, &mod, op->o_ndn )
348 /* Could not find old_rdn as an attribute or
349 * the old value was not present. Return an
352 Debug( LDAP_DEBUG_TRACE,
353 "ldbm_back_modrdn: old rdn not found or att type doesn't exist\n",
355 send_ldap_result( conn, op,
356 LDAP_OPERATIONS_ERROR,
362 Debug( LDAP_DEBUG_TRACE,
363 "ldbm_back_modrdn: removed old_rdn_val=%s\n",
366 }/* if (deleteoldrdn) */
368 /* Add new attribute value to the entry.
371 bv.bv_val = new_rdn_val;
372 bv.bv_len = strlen(new_rdn_val);
375 Debug( LDAP_DEBUG_TRACE,
376 "ldbm_back_modrdn: adding new rdn attr val =%s\n",
379 /* No need to normalize new_rdn_type, attr_merge does it */
381 attr_merge( e, new_rdn_type, bvals );
386 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
388 /* XXXV3: not sure of what to do here */
389 Debug( LDAP_DEBUG_TRACE,
390 "ldbm_back_modrdn: not fully implemented...\n",
395 (void) cache_update_entry( &li->li_cache, e );
398 if ( id2entry_add( be, e ) != 0 ) {
400 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
404 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
409 * new_dn and new_ndn are not deallocated because they are used by
412 if( p_dn != NULL ) free( p_dn );
413 if( p_ndn != NULL ) free( p_ndn );
415 if( matched != NULL ) free( matched );
417 /* LDAP v2 supporting correct attribute handling. */
418 if( new_rdn_type != NULL ) free(new_rdn_type);
419 if( new_rdn_val != NULL ) free(new_rdn_val);
420 if( old_rdn != NULL ) free(old_rdn);
421 if( old_rdn_type != NULL ) free(old_rdn_type);
422 if( old_rdn_val != NULL ) free(old_rdn_val);
425 /* LDAP v3 Support */
426 if ( np_dn != NULL ) free( np_dn );
427 if ( np_ndn != NULL ) free( np_ndn );
430 /* free parent and writer lock */
431 cache_return_entry_w( &li->li_cache, p );
435 /* release root writer lock */
436 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
439 /* free entry and writer lock */
440 cache_return_entry_w( &li->li_cache, e );