1 /* modrdn.c - bdb2 backend modrdn routine */
4 * LDAP v3 newSuperior support.
6 * Copyright 1999, Juan C. Gomez, All rights reserved.
7 * This software is not subject to any license of Silicon Graphics
8 * Inc. or Purdue University.
10 * Redistribution and use in source and binary forms are permitted
11 * without restriction or fee of any kind as long as this notice
20 #include <ac/string.h>
21 #include <ac/socket.h>
24 #include "back-bdb2.h"
25 #include "proto-back-bdb2.h"
28 bdb2i_back_modrdn_internal(
38 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
40 char *p_dn = NULL, *p_ndn = NULL;
41 char *new_dn = NULL, *new_ndn = NULL;
45 /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
46 char *new_rdn_val = NULL; /* Val of new rdn */
47 char *new_rdn_type = NULL; /* Type of new rdn */
48 char *old_rdn; /* Old rdn's attr type & val */
49 char *old_rdn_type = NULL; /* Type of old rdn attr. */
50 char *old_rdn_val = NULL; /* Old rdn attribute value */
51 struct berval bv; /* Stores new rdn att */
52 struct berval *bvals[2]; /* Stores new rdn att */
53 LDAPMod mod; /* Used to delete old rdn */
54 /* Added to support newSuperior */
55 Entry *np = NULL; /* newSuperior Entry */
56 char *np_dn = NULL; /* newSuperior dn */
57 char *np_ndn = NULL; /* newSuperior ndn */
58 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
60 Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
61 (newSuperior ? newSuperior : "NULL"),
64 /* get entry with writer lock */
65 if ( (e = bdb2i_dn2entry_w( be, dn, &matched )) == NULL ) {
66 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, "" );
67 if ( matched != NULL ) {
73 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
74 /* check parent for "children" acl */
75 if ( ! access_allowed( be, conn, op, e,
76 "entry", NULL, ACL_WRITE ) )
78 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
80 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
86 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
87 /* parent + rdn + separator(s) + null */
88 if( (p = bdb2i_dn2entry_w( be, p_ndn, &matched )) == NULL) {
89 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
91 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
96 /* check parent for "children" acl */
97 if ( ! access_allowed( be, conn, op, p,
98 "children", NULL, ACL_WRITE ) )
100 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
102 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
107 p_dn = dn_parent( be, e->e_dn );
109 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
114 /* no parent, modrdn entry directly under root */
115 if( ! be_isroot( be, op->o_ndn ) ) {
116 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
118 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
123 Debug( LDAP_DEBUG_TRACE,
124 "ldbm_back_modrdn: no parent!, working on root\n",
129 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
131 if ( (np_dn = newSuperior) != NULL) {
134 Debug( LDAP_DEBUG_TRACE,
135 "ldbm_back_modrdn: new parent requested...\n",
138 np_ndn = dn_normalize_case( ch_strdup( np_dn ) );
140 /* newSuperior == oldParent?, if so ==> ERROR */
142 /* newSuperior == entry being moved?, if so ==> ERROR */
144 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
146 if( (np = bdb2i_dn2entry_w( be, np_ndn, &matched )) == NULL) {
148 Debug( LDAP_DEBUG_TRACE,
149 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
151 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "",
156 Debug( LDAP_DEBUG_TRACE,
157 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
160 /* check newSuperior for "children" acl */
161 if ( !access_allowed( be, conn, op, np, "children", NULL,
164 Debug( LDAP_DEBUG_TRACE,
165 "ldbm_back_modrdn: no wr to newSup children\n",
167 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
172 Debug( LDAP_DEBUG_TRACE,
173 "ldbm_back_modrdn: wr to new parent's children OK\n",
177 new_parent_dn = np_dn;
181 /* Build target dn and make sure target entry doesn't exist already. */
183 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
186 new_ndn = dn_normalize_case( ch_strdup( new_dn ) );
188 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
191 if ( (bdb2i_dn2id ( be, new_ndn ) ) != NOID ) {
192 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, NULL, NULL );
196 /* check for abandon */
197 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
198 if ( op->o_abandon ) {
199 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
202 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
205 if ( bdb2i_dn2id_add( be, new_ndn, e->e_id ) != 0 ) {
206 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
211 if ( bdb2i_dn2id_delete( be, e->e_ndn ) != 0 ) {
212 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
216 (void) bdb2i_cache_delete_entry( &li->li_cache, e );
222 /* Get attribute type and attribute value of our new rdn, we will
223 * need to add that to our new entry
226 if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
228 Debug( LDAP_DEBUG_TRACE,
229 "ldbm_back_modrdn: can't figure out type of newrdn\n",
231 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
236 if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
238 Debug( LDAP_DEBUG_TRACE,
239 "ldbm_back_modrdn: can't figure out val of newrdn\n",
241 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
246 Debug( LDAP_DEBUG_TRACE,
247 "ldbm_back_modrdn: new_rdn_val=%s, new_rdn_type=%s\n",
248 new_rdn_val, new_rdn_type, 0 );
250 /* Retrieve the old rdn from the entry's dn */
252 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
254 Debug( LDAP_DEBUG_TRACE,
255 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
257 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
262 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
264 Debug( LDAP_DEBUG_TRACE,
265 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
267 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
272 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
274 /* Not a big deal but we may say something */
275 Debug( LDAP_DEBUG_TRACE,
276 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
277 old_rdn_type, new_rdn_type, 0 );
281 if ( dn_type( old_rdn ) == DN_X500 ) {
283 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
286 bvals[0] = &bv; /* Array of bervals */
289 /* Remove old rdn value if required */
293 /* Get value of old rdn */
295 if ((old_rdn_val = rdn_attr_value( old_rdn ))
298 Debug( LDAP_DEBUG_TRACE,
299 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
301 send_ldap_result( conn, op,
302 LDAP_OPERATIONS_ERROR,
309 /* Remove old value of rdn as an attribute. */
311 bv.bv_val = old_rdn_val;
312 bv.bv_len = strlen(old_rdn_val);
314 /* No need to normalize old_rdn_type, delete_values()
317 mod.mod_type = old_rdn_type;
318 mod.mod_bvalues = bvals;
319 mod.mod_op = LDAP_MOD_DELETE; /* XXX:really needed?*/
321 /* Assembly mod structure */
323 if ( delete_values( e, &mod, op->o_ndn )
326 /* Could not find old_rdn as an attribute or
327 * the old value was not present. Return an
330 Debug( LDAP_DEBUG_TRACE,
331 "ldbm_back_modrdn: old rdn not found or att type doesn't exist\n",
333 send_ldap_result( conn, op,
334 LDAP_OPERATIONS_ERROR,
340 Debug( LDAP_DEBUG_TRACE,
341 "ldbm_back_modrdn: removed old_rdn_val=%s\n",
344 }/* if (deleteoldrdn) */
346 /* Add new attribute value to the entry.
349 bv.bv_val = new_rdn_val;
350 bv.bv_len = strlen(new_rdn_val);
353 Debug( LDAP_DEBUG_TRACE,
354 "ldbm_back_modrdn: adding new rdn attr val =%s\n",
357 /* No need to normalize new_rdn_type, attr_merge does it */
359 attr_merge( e, new_rdn_type, bvals );
361 /* Update new_rdn_type if it is an index */
363 bdb2i_index_add_values( be, new_rdn_type, bvals, e->e_id );
368 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
370 /* XXXV3: not sure of what to do here */
371 Debug( LDAP_DEBUG_TRACE,
372 "ldbm_back_modrdn: not fully implemented...\n",
377 (void) bdb2i_cache_update_entry( &li->li_cache, e );
380 if ( bdb2i_id2entry_add( be, e ) != 0 ) {
382 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
386 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
391 * new_dn and new_ndn are not deallocated because they are used by
394 if( p_dn != NULL ) free( p_dn );
395 if( p_ndn != NULL ) free( p_ndn );
397 if( matched != NULL ) free( matched );
399 /* LDAP v2 supporting correct attribute handling. */
400 if( new_rdn_type != NULL ) free(new_rdn_type);
401 if( new_rdn_val != NULL ) free(new_rdn_val);
402 if( old_rdn != NULL ) free(old_rdn);
403 if( old_rdn_type != NULL ) free(old_rdn_type);
404 if( old_rdn_val != NULL ) free(old_rdn_val);
406 /* LDAP v3 Support */
407 if ( np_dn != NULL ) free( np_dn );
408 if ( np_ndn != NULL ) free( np_ndn );
411 /* free parent and writer lock */
412 bdb2i_cache_return_entry_w( &li->li_cache, p );
416 /* free entry and writer lock */
417 bdb2i_cache_return_entry_w( &li->li_cache, e );
434 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
435 struct timeval time1;
438 bdb2i_start_timing( be->bd_info, &time1 );
440 if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
442 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
447 ret = bdb2i_back_modrdn_internal( be, conn, op, dn,
448 newrdn, deleteoldrdn,
451 (void) bdb2i_leave_backend_w( lock );
452 bdb2i_stop_timing( be->bd_info, time1, "MODRDN", conn, op );