1 /* modrdn.c - bdb2 backend modrdn routine */
5 * LDAP v3 newSuperior support.
7 * Copyright 1999, Juan C. Gomez, All rights reserved.
8 * This software is not subject to any license of Silicon Graphics
9 * Inc. or Purdue University.
11 * Redistribution and use in source and binary forms are permitted
12 * without restriction or fee of any kind as long as this notice
21 #include <ac/string.h>
22 #include <ac/socket.h>
25 #include "back-bdb2.h"
26 #include "proto-back-bdb2.h"
29 bdb2i_back_modrdn_internal(
39 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
40 Entry *matched = NULL;
41 char *p_dn = NULL, *p_ndn = NULL;
42 char *new_dn = NULL, *new_ndn = NULL;
45 int rc = -1, manageDSAit;
46 /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
47 char *new_rdn_val = NULL; /* Val of new rdn */
48 char *new_rdn_type = NULL; /* Type of new rdn */
49 char *old_rdn = NULL; /* Old rdn's attr type & val */
50 char *old_rdn_type = NULL; /* Type of old rdn attr. */
51 char *old_rdn_val = NULL; /* Old rdn attribute value */
52 struct berval add_bv; /* Stores new rdn att */
53 struct berval *add_bvals[2]; /* Stores new rdn att */
54 struct berval del_bv; /* Stores old rdn att */
55 struct berval *del_bvals[2]; /* Stores old rdn att */
56 LDAPModList mod[2]; /* 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 = bdb2i_dn2entry_w( be, dn, &matched )) == NULL ) {
69 char *matched_dn = NULL;
70 struct berval **refs = NULL;
72 if ( matched != NULL ) {
73 matched_dn = ch_strdup( matched->e_dn );
74 refs = is_entry_referral( matched )
75 ? get_entry_referrals( be, conn, op, matched )
77 bdb2i_cache_return_entry_r( &li->li_cache, matched );
79 refs = default_referral;
82 send_ldap_result( conn, op, LDAP_REFERRAL,
83 matched_dn, NULL, refs, NULL );
85 if( matched != NULL ) {
93 if (!manageDSAit && is_entry_referral( e ) ) {
94 /* entry is a referral, don't allow add */
95 struct berval **refs = get_entry_referrals( be,
98 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
101 send_ldap_result( conn, op, LDAP_REFERRAL,
102 e->e_dn, NULL, refs, NULL );
104 ber_bvecfree( refs );
109 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
110 /* check parent for "children" acl */
111 if ( ! access_allowed( be, conn, op, e,
112 "entry", NULL, ACL_WRITE ) )
114 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
116 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
117 NULL, NULL, NULL, NULL );
122 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
123 /* parent + rdn + separator(s) + null */
124 if( (p = bdb2i_dn2entry_w( be, p_ndn, &matched )) == NULL) {
125 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
127 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
128 NULL, NULL, NULL, NULL );
132 /* check parent for "children" acl */
133 if ( ! access_allowed( be, conn, op, p,
134 "children", NULL, ACL_WRITE ) )
136 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
138 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
139 NULL, NULL, NULL, NULL );
143 p_dn = dn_parent( be, e->e_dn );
145 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
150 /* no parent, modrdn entry directly under root */
151 if( ! be_isroot( be, op->o_ndn ) ) {
152 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
154 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
155 NULL, NULL, NULL, NULL );
159 Debug( LDAP_DEBUG_TRACE,
160 "ldbm_back_modrdn: no parent!, working on root\n",
165 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
167 if ( (np_dn = newSuperior) != NULL) {
170 Debug( LDAP_DEBUG_TRACE,
171 "ldbm_back_modrdn: new parent requested...\n",
174 np_ndn = ch_strdup( np_dn );
175 (void) dn_normalize_case( np_ndn );
177 /* newSuperior == oldParent?, if so ==> ERROR */
179 /* newSuperior == entry being moved?, if so ==> ERROR */
181 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
183 if( (np = bdb2i_dn2entry_w( be, np_ndn, &matched )) == NULL) {
185 Debug( LDAP_DEBUG_TRACE,
186 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
188 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
189 NULL, NULL, NULL, NULL );
193 Debug( LDAP_DEBUG_TRACE,
194 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
197 /* check newSuperior for "children" acl */
198 if ( !access_allowed( be, conn, op, np, "children", NULL,
201 Debug( LDAP_DEBUG_TRACE,
202 "ldbm_back_modrdn: no wr to newSup children\n",
204 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
205 NULL, NULL, NULL, NULL );
209 Debug( LDAP_DEBUG_TRACE,
210 "ldbm_back_modrdn: wr to new parent's children OK\n",
214 new_parent_dn = np_dn;
218 /* Build target dn and make sure target entry doesn't exist already. */
220 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
223 new_ndn = ch_strdup( new_dn );
224 (void) dn_normalize_case( new_ndn );
226 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
229 /* check for abandon */
230 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
231 if ( op->o_abandon ) {
232 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
235 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
237 if ( (bdb2i_dn2id ( be, new_ndn ) ) != NOID ) {
238 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
239 NULL, NULL, NULL, NULL );
243 /* Get attribute type and attribute value of our new rdn, we will
244 * need to add that to our new entry
247 if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
249 Debug( LDAP_DEBUG_TRACE,
250 "ldbm_back_modrdn: can't figure out type of newrdn\n",
252 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
253 NULL, NULL, NULL, NULL );
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,
264 NULL, NULL, NULL, NULL );
269 Debug( LDAP_DEBUG_TRACE,
270 "ldbm_back_modrdn: new_rdn_val=%s, new_rdn_type=%s\n",
271 new_rdn_val, new_rdn_type, 0 );
273 /* Retrieve the old rdn from the entry's dn */
275 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
277 Debug( LDAP_DEBUG_TRACE,
278 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
280 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
281 NULL, NULL, NULL, NULL );
286 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
288 Debug( LDAP_DEBUG_TRACE,
289 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
291 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
292 NULL, NULL, NULL, NULL );
297 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
299 /* Not a big deal but we may say something */
300 Debug( LDAP_DEBUG_TRACE,
301 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
302 old_rdn_type, new_rdn_type, 0 );
307 if ( dn_type( old_rdn ) == DN_X500 ) {
310 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
313 /* Add new attribute value to the entry.
316 add_bvals[0] = &add_bv; /* Array of bervals */
319 add_bv.bv_val = new_rdn_val;
320 add_bv.bv_len = strlen(new_rdn_val);
322 mod[0].ml_type = new_rdn_type;
323 mod[0].ml_bvalues = add_bvals;
324 mod[0].ml_op = LDAP_MOD_SOFTADD;
325 mod[0].ml_next = NULL;
327 Debug( LDAP_DEBUG_TRACE,
328 "ldbm_back_modrdn: adding new rdn attr val =%s\n",
331 /* Remove old rdn value if required */
335 del_bvals[0] = &del_bv; /* Array of bervals */
337 /* Get value of old rdn */
339 if ((old_rdn_val = rdn_attr_value( old_rdn ))
342 Debug( LDAP_DEBUG_TRACE,
343 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
345 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
346 NULL, NULL, NULL, NULL );
352 /* Remove old value of rdn as an attribute. */
354 del_bv.bv_val = old_rdn_val;
355 del_bv.bv_len = strlen(old_rdn_val);
357 mod[0].ml_next = &mod[1];
358 mod[1].ml_type = old_rdn_type;
359 mod[1].ml_bvalues = del_bvals;
360 mod[1].ml_op = LDAP_MOD_DELETE;
361 mod[1].ml_next = NULL;
364 Debug( LDAP_DEBUG_TRACE,
365 "ldbm_back_modrdn: removing old_rdn_val=%s\n",
368 }/* if (deleteoldrdn) */
374 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
376 /* XXXV3: not sure of what to do here */
377 Debug( LDAP_DEBUG_TRACE,
378 "ldbm_back_modrdn: not fully implemented...\n",
384 /* check for abandon */
385 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
386 if ( op->o_abandon ) {
387 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
390 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
393 if ( bdb2i_dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
394 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
395 NULL, NULL, NULL, NULL );
399 (void) bdb2i_cache_delete_entry( &li->li_cache, e );
409 if ( bdb2i_dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
410 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
411 NULL, NULL, NULL, NULL );
415 /* modify memory copy of entry */
416 if ( bdb2i_back_modify_internal( be, conn, op, dn, &mod[0], e )
423 (void) bdb2i_cache_update_entry( &li->li_cache, e );
425 /* NOTE: after this you must not free new_dn or new_ndn!
426 * They are used by cache.
430 if ( bdb2i_id2entry_add( be, e ) != 0 ) {
432 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
433 NULL, NULL, NULL, NULL );
437 send_ldap_result( conn, op, LDAP_SUCCESS,
438 NULL, NULL, NULL, NULL );
442 if( new_dn != NULL ) free( new_dn );
443 if( new_ndn != NULL ) free( new_ndn );
446 * new_dn and new_ndn are not deallocated because they are used by
449 if( p_dn != NULL ) free( p_dn );
450 if( p_ndn != NULL ) free( p_ndn );
452 /* LDAP v2 supporting correct attribute handling. */
453 if( new_rdn_type != NULL ) free(new_rdn_type);
454 if( new_rdn_val != NULL ) free(new_rdn_val);
455 if( old_rdn != NULL ) free(old_rdn);
456 if( old_rdn_type != NULL ) free(old_rdn_type);
457 if( old_rdn_val != NULL ) free(old_rdn_val);
459 /* LDAP v3 Support */
460 if ( np_dn != NULL ) free( np_dn );
461 if ( np_ndn != NULL ) free( np_ndn );
464 /* free parent and writer lock */
465 bdb2i_cache_return_entry_w( &li->li_cache, p );
469 /* free entry and writer lock */
470 bdb2i_cache_return_entry_w( &li->li_cache, e );
487 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
488 struct timeval time1;
491 bdb2i_start_timing( be->bd_info, &time1 );
493 if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
494 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
495 NULL, NULL, NULL, NULL );
500 ret = bdb2i_back_modrdn_internal( be, conn, op, dn,
501 newrdn, deleteoldrdn,
504 (void) bdb2i_leave_backend_w( lock );
505 bdb2i_stop_timing( be->bd_info, time1, "MODRDN", conn, op );