1 /* modrdn.c - ldbm backend modrdn routine */
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
24 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
26 char *p_dn = NULL, *p_ndn = NULL;
27 char *new_dn = NULL, *new_ndn = NULL;
33 char *new_rdn_val = NULL; /* Val of new rdn */
34 char *new_rdn_type = NULL; /* Type of new rdn */
35 char *old_rdn; /* Old rdn's attr type & val */
36 char *old_rdn_type = NULL; /* Type of old rdn attr. */
37 char *old_rdn_val = NULL; /* Old rdn attribute value */
38 struct berval add_bv; /* Stores new rdn att */
39 struct berval *add_bvals[2]; /* Stores new rdn att */
40 struct berval del_bv; /* Stores old rdn att */
41 struct berval *del_bvals[2]; /* Stores old rdn att */
42 LDAPMod mod[2]; /* Used to delete old rdn */
44 Debug( LDAP_DEBUG_TRACE,
45 "ldbm_back_modrdn:()==>\n",
48 /* get entry with writer lock */
49 if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
50 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, "" );
51 if ( matched != NULL ) {
57 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
58 /* check parent for "children" acl */
59 if ( ! access_allowed( be, conn, op, e,
60 "entry", NULL, ACL_WRITE ) )
62 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
64 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
70 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
71 /* parent + rdn + separator(s) + null */
72 if( (p = dn2entry_w( be, p_ndn, &matched )) == NULL) {
73 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
75 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
80 #ifndef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
81 /* check parent for "children" acl */
82 if ( ! access_allowed( be, conn, op, p,
83 "children", NULL, ACL_WRITE ) )
85 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
87 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
93 p_dn = dn_parent( be, e->e_dn );
97 /* no parent, modrdn entry directly under root */
98 if( ! be_isroot( be, op->o_ndn ) ) {
99 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
101 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
106 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
112 build_new_dn( &new_dn, e->e_dn, p_dn, newrdn );
113 new_ndn = dn_normalize_case( ch_strdup( new_dn ) );
115 /* Get attribute type and attribute value of our new rdn, we will
116 * need to add that to our new entry
119 if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
121 Debug( LDAP_DEBUG_TRACE,
122 "ldbm_back_modrdn: can't figure out type of newrdn\n",
124 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
129 if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
131 Debug( LDAP_DEBUG_TRACE,
132 "ldbm_back_modrdn: can't figure out val of newrdn\n",
134 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
139 Debug( LDAP_DEBUG_TRACE,
140 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
141 new_rdn_val, new_rdn_type, 0 );
143 /* Retrieve the old rdn from the entry's dn */
145 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
147 Debug( LDAP_DEBUG_TRACE,
148 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
150 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
155 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
157 Debug( LDAP_DEBUG_TRACE,
158 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
160 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
165 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
167 /* Not a big deal but we may say something */
168 Debug( LDAP_DEBUG_TRACE,
169 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
170 old_rdn_type, new_rdn_type, 0 );
174 if ( dn_type( old_rdn ) == DN_X500 ) {
176 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
179 /* Add new attribute value to the entry.
182 add_bvals[0] = &add_bv; /* Array of bervals */
185 add_bv.bv_val = new_rdn_val;
186 add_bv.bv_len = strlen(new_rdn_val);
188 mod[0].mod_type = new_rdn_type;
189 mod[0].mod_bvalues = add_bvals;
190 mod[0].mod_op = LDAP_MOD_SOFTADD;
191 mod[0].mod_next = NULL;
193 Debug( LDAP_DEBUG_TRACE,
194 "ldbm_back_modrdn: adding new rdn attr val =\"%s\"\n",
197 /* Remove old rdn value if required */
201 del_bvals[0] = &del_bv; /* Array of bervals */
203 /* Get value of old rdn */
205 if ((old_rdn_val = rdn_attr_value( old_rdn ))
208 Debug( LDAP_DEBUG_TRACE,
209 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
211 send_ldap_result( conn, op,
212 LDAP_OPERATIONS_ERROR,
219 /* Remove old value of rdn as an attribute. */
221 del_bv.bv_val = old_rdn_val;
222 del_bv.bv_len = strlen(old_rdn_val);
224 /* No need to normalize old_rdn_type, delete_values()
227 mod[0].mod_next = &mod[1];
228 mod[1].mod_type = old_rdn_type;
229 mod[1].mod_bvalues = del_bvals;
230 mod[1].mod_op = LDAP_MOD_DELETE;
231 mod[1].mod_next = NULL;
233 }/* if (deleteoldrdn) */
235 /* modify memory copy of entry */
236 if ( ldbm_internal_modify( be, conn, op, dn, &mod[0], e )
246 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
248 /* XXXV3: not sure of what to do here */
249 Debug( LDAP_DEBUG_TRACE,
250 "ldbm_back_modrdn: not fully implemented...\n",
252 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
257 if ( (dn2id ( be, new_ndn ) ) != NOID ) {
258 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, NULL, NULL );
262 /* check for abandon */
263 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
264 if ( op->o_abandon ) {
265 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
268 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
271 if ( dn2id_add( be, new_ndn, e->e_id ) != 0 ) {
272 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
277 if ( dn2id_delete( be, e->e_ndn ) != 0 ) {
278 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
283 (void) cache_delete_entry( &li->li_cache, e );
288 (void) cache_update_entry( &li->li_cache, e );
290 /* id2entry index: commit */
291 if ( id2entry_add( be, e ) != 0 ) {
293 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
294 goto return_results_new;
297 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
300 Debug( LDAP_DEBUG_TRACE,
301 "ldbm_back_modrdn:()<==\n",
303 goto return_results_new;
306 if( new_dn != NULL ) free( new_dn );
307 if( new_ndn != NULL ) free( new_ndn );
310 * new_dn and new_ndn are not deallocated because they are used by
311 * the cache entry at this point.
313 if( p_dn != NULL ) free( p_dn );
314 if( p_ndn != NULL ) free( p_ndn );
316 if( matched != NULL ) free( matched );
318 /* LDAP v2 supporting correct attribute handling. */
319 if( new_rdn_type != NULL ) free(new_rdn_type);
320 if( new_rdn_val != NULL ) free(new_rdn_val);
321 if( old_rdn != NULL ) free(old_rdn);
322 if( old_rdn_type != NULL ) free(old_rdn_type);
323 if( old_rdn_val != NULL ) free(old_rdn_val);
326 /* free parent and writer lock */
327 cache_return_entry_w( &li->li_cache, p );
331 /* release root writer lock */
332 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
335 /* free entry and writer lock */
336 cache_return_entry_w( &li->li_cache, e );