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",
180 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
182 if ( (np_dn = newSuperior) != NULL) {
183 Debug( LDAP_DEBUG_TRACE,
184 "ldbm_back_modrdn: new parent requested...\n",
187 np_ndn = ch_strdup( np_dn );
188 (void) dn_normalize_case( np_ndn );
190 /* newSuperior == oldParent?, if so ==> ERROR */
191 /* newSuperior == entry being moved?, if so ==> ERROR */
192 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
194 if( (np = dn2entry_w( be, np_ndn, &matched )) == NULL) {
195 Debug( LDAP_DEBUG_TRACE,
196 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
198 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
199 NULL, NULL, NULL, NULL );
203 Debug( LDAP_DEBUG_TRACE,
204 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
207 /* check newSuperior for "children" acl */
208 if ( !access_allowed( be, conn, op, np, "children", NULL,
211 Debug( LDAP_DEBUG_TRACE,
212 "ldbm_back_modrdn: no wr to newSup children\n",
214 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
215 NULL, NULL, NULL, NULL );
219 if ( is_entry_alias( np ) ) {
220 /* entry is an alias, don't allow bind */
221 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
224 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
225 NULL, NULL, NULL, NULL );
230 if ( is_entry_referral( np ) ) {
231 /* parent is a referral, don't allow add */
232 /* parent is an alias, don't allow add */
233 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
236 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
237 NULL, NULL, NULL, NULL );
242 Debug( LDAP_DEBUG_TRACE,
243 "ldbm_back_modrdn: wr to new parent's children OK\n",
246 new_parent_dn = np_dn;
249 /* Build target dn and make sure target entry doesn't exist already. */
251 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
254 new_ndn = ch_strdup(new_dn);
255 (void) dn_normalize_case( new_ndn );
257 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
260 if (dn2id ( be, new_ndn ) != NOID) {
261 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
262 NULL, NULL, NULL, NULL );
266 Debug( LDAP_DEBUG_TRACE,
267 "ldbm_back_modrdn: new ndn=%s does not exist\n",
270 /* check for abandon */
271 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
272 if ( op->o_abandon ) {
273 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
276 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
279 if ( dn2id_delete( be, e->e_ndn ) != 0 ) {
280 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
281 NULL, NULL, NULL, NULL );
285 (void) cache_delete_entry( &li->li_cache, e );
292 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
293 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
294 NULL, NULL, NULL, NULL );
299 /* Get attribute type and attribute value of our new rdn, we will
300 * need to add that to our new entry
303 if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
305 Debug( LDAP_DEBUG_TRACE,
306 "ldbm_back_modrdn: can't figure out type of newrdn\n",
308 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
309 NULL, NULL, NULL, NULL );
314 if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
316 Debug( LDAP_DEBUG_TRACE,
317 "ldbm_back_modrdn: can't figure out val of newrdn\n",
319 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
320 NULL, NULL, NULL, NULL );
325 Debug( LDAP_DEBUG_TRACE,
326 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
327 new_rdn_val, new_rdn_type, 0 );
329 /* Retrieve the old rdn from the entry's dn */
331 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
333 Debug( LDAP_DEBUG_TRACE,
334 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
336 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
337 NULL, NULL, NULL, NULL );
342 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
344 Debug( LDAP_DEBUG_TRACE,
345 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
347 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
348 NULL, NULL, NULL, NULL );
353 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
355 /* Not a big deal but we may say something */
356 Debug( LDAP_DEBUG_TRACE,
357 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
358 old_rdn_type, new_rdn_type, 0 );
363 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",
423 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
425 /* XXXV3: not sure of what to do here */
426 Debug( LDAP_DEBUG_TRACE,
427 "ldbm_back_modrdn: not fully implemented...\n",
430 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
431 NULL, NULL, NULL, NULL );
437 /* modify memory copy of entry */
438 if ( ldbm_modify_internal( be, conn, op, dn, &mod[0], e )
445 (void) cache_update_entry( &li->li_cache, e );
447 /* NOTE: after this you must not free new_dn or new_ndn!
448 * They are used by cache.
452 if ( id2entry_add( be, e ) != 0 ) {
454 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
455 NULL, NULL, NULL, NULL );
456 goto return_results_after;
459 send_ldap_result( conn, op, LDAP_SUCCESS,
460 NULL, NULL, NULL, NULL );
462 goto return_results_after;
465 if( new_dn != NULL ) free( new_dn );
466 if( new_ndn != NULL ) free( new_ndn );
468 return_results_after:
470 * new_dn and new_ndn are not deallocated because they are used by
473 if( p_dn != NULL ) free( p_dn );
474 if( p_ndn != NULL ) free( p_ndn );
476 if( matched != NULL ) free( matched );
478 /* LDAP v2 supporting correct attribute handling. */
479 if( new_rdn_type != NULL ) free(new_rdn_type);
480 if( new_rdn_val != NULL ) free(new_rdn_val);
481 if( old_rdn != NULL ) free(old_rdn);
482 if( old_rdn_type != NULL ) free(old_rdn_type);
483 if( old_rdn_val != NULL ) free(old_rdn_val);
486 /* LDAP v3 Support */
487 if ( np_dn != NULL ) free( np_dn );
488 if ( np_ndn != NULL ) free( np_ndn );
491 /* free new parent and writer lock */
492 cache_return_entry_w( &li->li_cache, np );
496 /* free parent and writer lock */
497 cache_return_entry_w( &li->li_cache, p );
501 /* release root writer lock */
502 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
505 /* free entry and writer lock */
506 cache_return_entry_w( &li->li_cache, e );