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;
42 char *p_dn = NULL, *p_ndn = NULL;
43 char *new_dn = NULL, *new_ndn = NULL;
45 Entry *matched = 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 /* 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 */
59 /* Used to interface with ldbm_modify_internal() */
60 struct berval add_bv; /* Stores new rdn att */
61 struct berval *add_bvals[2]; /* Stores new rdn att */
62 struct berval del_bv; /* Stores old rdn att */
63 struct berval *del_bvals[2]; /* Stores old rdn att */
64 LDAPModList mod[2]; /* Used to delete old rdn */
65 int manageDSAit = get_manageDSAit( op );
67 Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
68 (newSuperior ? newSuperior : "NULL"),
71 /* get entry with writer lock */
72 if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
73 char* matched_dn = NULL;
74 struct berval** refs = NULL;
76 if( matched != NULL ) {
77 matched_dn = strdup( matched->e_dn );
78 refs = is_entry_referral( matched )
79 ? get_entry_referrals( be, conn, op, matched )
81 cache_return_entry_r( &li->li_cache, matched );
83 refs = default_referral;
86 send_ldap_result( conn, op, LDAP_REFERRAL,
87 matched_dn, NULL, refs, NULL );
89 if ( matched != NULL ) {
97 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
98 if ( ! access_allowed( be, conn, op, e,
99 "entry", NULL, ACL_WRITE ) )
101 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
103 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
104 NULL, NULL, NULL, NULL );
109 if (!manageDSAit && is_entry_referral( e ) ) {
110 /* parent is a referral, don't allow add */
111 /* parent is an alias, don't allow add */
112 struct berval **refs = get_entry_referrals( be,
115 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
118 send_ldap_result( conn, op, LDAP_REFERRAL,
119 e->e_dn, NULL, refs, NULL );
121 ber_bvecfree( refs );
126 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
128 /* Make sure parent entry exist and we can write its
132 if( (p = dn2entry_w( be, p_ndn, &matched )) == NULL) {
133 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
135 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
136 NULL, NULL, NULL, NULL );
140 /* check parent for "children" acl */
141 if ( ! access_allowed( be, conn, op, p,
142 "children", NULL, ACL_WRITE ) )
144 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
146 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
147 NULL, NULL, NULL, NULL );
151 Debug( LDAP_DEBUG_TRACE,
152 "ldbm_back_modrdn: wr to children of entry %s OK\n",
155 p_dn = dn_parent( be, e->e_dn );
158 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
162 /* no parent, modrdn entry directly under root */
163 if( ! be_isroot( be, op->o_ndn ) ) {
164 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
166 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
167 NULL, NULL, NULL, NULL );
171 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
174 Debug( LDAP_DEBUG_TRACE,
175 "ldbm_back_modrdn: no parent, locked root\n",
178 }/* if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) else */
180 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
182 if ( (np_dn = newSuperior) != NULL) {
185 Debug( LDAP_DEBUG_TRACE,
186 "ldbm_back_modrdn: new parent requested...\n",
189 np_ndn = dn_normalize_case( ch_strdup( np_dn ) );
191 /* newSuperior == oldParent?, if so ==> ERROR */
193 /* newSuperior == entry being moved?, if so ==> ERROR */
195 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
197 if( (np = dn2entry_w( be, np_ndn, &matched )) == NULL) {
198 Debug( LDAP_DEBUG_TRACE,
199 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
201 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
202 NULL, NULL, NULL, NULL );
206 Debug( LDAP_DEBUG_TRACE,
207 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
210 /* check newSuperior for "children" acl */
211 if ( !access_allowed( be, conn, op, np, "children", NULL,
214 Debug( LDAP_DEBUG_TRACE,
215 "ldbm_back_modrdn: no wr to newSup children\n",
217 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
218 NULL, NULL, NULL, NULL );
222 if ( is_entry_alias( np ) ) {
223 /* entry is an alias, don't allow bind */
224 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
227 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
228 NULL, NULL, NULL, NULL );
233 if ( is_entry_referral( np ) ) {
234 /* parent is a referral, don't allow add */
235 /* parent is an alias, don't allow add */
236 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
239 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
240 NULL, NULL, NULL, NULL );
245 Debug( LDAP_DEBUG_TRACE,
246 "ldbm_back_modrdn: wr to new parent's children OK\n",
249 new_parent_dn = np_dn;
252 /* Build target dn and make sure target entry doesn't exist already. */
254 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
257 new_ndn = dn_normalize_case( ch_strdup(new_dn) );
259 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
262 if (dn2id ( be, new_ndn ) != NOID) {
263 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
264 NULL, NULL, NULL, NULL );
268 Debug( LDAP_DEBUG_TRACE,
269 "ldbm_back_modrdn: new ndn=%s does not exist\n",
272 /* check for abandon */
273 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
274 if ( op->o_abandon ) {
275 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
278 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
281 if ( dn2id_delete( be, e->e_ndn ) != 0 ) {
282 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
283 NULL, NULL, NULL, NULL );
287 (void) cache_delete_entry( &li->li_cache, e );
294 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
295 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
296 NULL, NULL, NULL, NULL );
301 /* Get attribute type and attribute value of our new rdn, we will
302 * need to add that to our new entry
305 if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
307 Debug( LDAP_DEBUG_TRACE,
308 "ldbm_back_modrdn: can't figure out type of newrdn\n",
310 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
311 NULL, NULL, NULL, NULL );
316 if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
318 Debug( LDAP_DEBUG_TRACE,
319 "ldbm_back_modrdn: can't figure out val of newrdn\n",
321 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
322 NULL, NULL, NULL, NULL );
327 Debug( LDAP_DEBUG_TRACE,
328 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
329 new_rdn_val, new_rdn_type, 0 );
331 /* Retrieve the old rdn from the entry's dn */
333 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
335 Debug( LDAP_DEBUG_TRACE,
336 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
338 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
339 NULL, NULL, NULL, NULL );
344 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
346 Debug( LDAP_DEBUG_TRACE,
347 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
349 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
350 NULL, NULL, NULL, NULL );
355 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
357 /* Not a big deal but we may say something */
358 Debug( LDAP_DEBUG_TRACE,
359 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
360 old_rdn_type, new_rdn_type, 0 );
364 if ( dn_type( old_rdn ) == DN_X500 ) {
366 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
369 /* Add new attribute value to the entry.
372 add_bvals[0] = &add_bv; /* Array of bervals */
375 add_bv.bv_val = new_rdn_val;
376 add_bv.bv_len = strlen(new_rdn_val);
378 mod[0].ml_type = new_rdn_type;
379 mod[0].ml_bvalues = add_bvals;
380 mod[0].ml_op = LDAP_MOD_SOFTADD;
381 mod[0].ml_next = NULL;
383 /* Remove old rdn value if required */
386 /* Get value of old rdn */
388 if ((old_rdn_val = rdn_attr_value( old_rdn ))
391 Debug( LDAP_DEBUG_TRACE,
392 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
394 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
395 NULL, NULL, NULL, NULL );
399 del_bvals[0] = &del_bv; /* Array of bervals */
402 /* Remove old value of rdn as an attribute. */
404 del_bv.bv_val = old_rdn_val;
405 del_bv.bv_len = strlen(old_rdn_val);
407 /* No need to normalize old_rdn_type, delete_values()
410 mod[0].ml_next = &mod[1];
411 mod[1].ml_type = old_rdn_type;
412 mod[1].ml_bvalues = del_bvals;
413 mod[1].ml_op = LDAP_MOD_DELETE;
414 mod[1].ml_next = NULL;
416 Debug( LDAP_DEBUG_TRACE,
417 "ldbm_back_modrdn: removing old_rdn_val=%s\n",
420 }/* if (deleteoldrdn) */
426 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
428 /* XXXV3: not sure of what to do here */
429 Debug( LDAP_DEBUG_TRACE,
430 "ldbm_back_modrdn: not fully implemented...\n",
433 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
434 NULL, NULL, NULL, NULL );
439 /* modify memory copy of entry */
440 if ( ldbm_modify_internal( be, conn, op, dn, &mod[0], e )
447 (void) cache_update_entry( &li->li_cache, e );
449 /* NOTE: after this you must not free new_dn or new_ndn!
450 * They are used by cache.
454 if ( id2entry_add( be, e ) != 0 ) {
456 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
457 NULL, NULL, NULL, NULL );
458 goto return_results_after;
461 send_ldap_result( conn, op, LDAP_SUCCESS,
462 NULL, NULL, NULL, NULL );
464 goto return_results_after;
467 if( new_dn != NULL ) free( new_dn );
468 if( new_ndn != NULL ) free( new_ndn );
469 return_results_after:
471 * new_dn and new_ndn are not deallocated because they are used by
474 if( p_dn != NULL ) free( p_dn );
475 if( p_ndn != NULL ) free( p_ndn );
477 if( matched != NULL ) free( matched );
479 /* LDAP v2 supporting correct attribute handling. */
480 if( new_rdn_type != NULL ) free(new_rdn_type);
481 if( new_rdn_val != NULL ) free(new_rdn_val);
482 if( old_rdn != NULL ) free(old_rdn);
483 if( old_rdn_type != NULL ) free(old_rdn_type);
484 if( old_rdn_val != NULL ) free(old_rdn_val);
487 /* LDAP v3 Support */
488 if ( np_dn != NULL ) free( np_dn );
489 if ( np_ndn != NULL ) free( np_ndn );
492 /* free parent and writer lock */
493 cache_return_entry_w( &li->li_cache, p );
497 /* release root writer lock */
498 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
501 /* free entry and writer lock */
502 cache_return_entry_w( &li->li_cache, e );