]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
New dn2id format with base/one/subtree indices (ldbm/bdb2)
[openldap] / servers / slapd / back-ldbm / modrdn.c
1 /* modrdn.c - ldbm backend modrdn routine */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 /*
8  * LDAP v3 newSuperior support. Add new rdn as an attribute.
9  * (Full support for v2 also used software/ideas contributed
10  * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
11  * submission!.)
12  *
13  * Copyright 1999, Juan C. Gomez, All rights reserved.
14  * This software is not subject to any license of Silicon Graphics 
15  * Inc. or Purdue University.
16  *
17  * Redistribution and use in source and binary forms are permitted
18  * without restriction or fee of any kind as long as this notice
19  * is preserved.
20  *
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29
30 #include "slap.h"
31 #include "back-ldbm.h"
32 #include "proto-back-ldbm.h"
33
34 int
35 ldbm_back_modrdn(
36     Backend     *be,
37     Connection  *conn,
38     Operation   *op,
39     char        *dn,
40     char        *newrdn,
41     int         deleteoldrdn,
42     char        *newSuperior
43 )
44 {
45         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
46         char            *p_dn = NULL, *p_ndn = NULL;
47         char            *new_dn = NULL, *new_ndn = NULL;
48         Entry           *e, *p = NULL;
49         Entry           *matched = NULL;
50         int                     rootlock = 0;
51         int                     rc = -1;
52         /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
53         char            *new_rdn_val = NULL;    /* Val of new rdn */
54         char            *new_rdn_type = NULL;   /* Type of new rdn */
55         char            *old_rdn;               /* Old rdn's attr type & val */
56         char            *old_rdn_type = NULL;   /* Type of old rdn attr. */
57         char            *old_rdn_val = NULL;    /* Old rdn attribute value */
58         /* Added to support newSuperior */ 
59         Entry           *np = NULL;     /* newSuperior Entry */
60         char            *np_dn = NULL;  /* newSuperior dn */
61         char            *np_ndn = NULL; /* newSuperior ndn */
62         char            *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
63         /* Used to interface with ldbm_modify_internal() */
64         struct berval   add_bv;                 /* Stores new rdn att */
65         struct berval   *add_bvals[2];          /* Stores new rdn att */
66         struct berval   del_bv;                 /* Stores old rdn att */
67         struct berval   *del_bvals[2];          /* Stores old rdn att */
68         LDAPModList     mod[2];                 /* Used to delete old rdn */
69         int             manageDSAit = get_manageDSAit( op );
70
71         Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
72                (newSuperior ? newSuperior : "NULL"),
73                0, 0 );
74
75         /* get entry with writer lock */
76         if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
77                 char* matched_dn = NULL;
78                 struct berval** refs = NULL;
79
80                 if( matched != NULL ) {
81                         matched_dn = strdup( matched->e_dn );
82                         refs = is_entry_referral( matched )
83                                 ? get_entry_referrals( be, conn, op, matched )
84                                 : NULL;
85                         cache_return_entry_r( &li->li_cache, matched );
86                 } else {
87                         refs = default_referral;
88                 }
89
90                 send_ldap_result( conn, op, LDAP_REFERRAL,
91                         matched_dn, NULL, refs, NULL );
92
93                 if ( matched != NULL ) {
94                         ber_bvecfree( refs );
95                         free( matched_dn );
96                 }
97
98                 return( -1 );
99         }
100
101 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
102         if ( ! access_allowed( be, conn, op, e,
103                 "entry", NULL, ACL_WRITE ) )
104         {
105                 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
106                         0, 0 );
107                 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
108                         NULL, NULL, NULL, NULL );
109                 goto return_results;
110         }
111 #endif
112
113         if (!manageDSAit && is_entry_referral( e ) ) {
114                 /* parent is a referral, don't allow add */
115                 /* parent is an alias, don't allow add */
116                 struct berval **refs = get_entry_referrals( be,
117                         conn, op, e );
118
119                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
120                     0, 0 );
121
122                 send_ldap_result( conn, op, LDAP_REFERRAL,
123                     e->e_dn, NULL, refs, NULL );
124
125                 ber_bvecfree( refs );
126
127                 goto return_results;
128         }
129
130         if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
131
132                 /* Make sure parent entry exist and we can write its 
133                  * children.
134                  */
135
136                 if( (p = dn2entry_w( be, p_ndn, &matched )) == NULL) {
137                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
138                                 0, 0, 0);
139                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
140                                 NULL, NULL, NULL, NULL );
141                         goto return_results;
142                 }
143
144                 /* check parent for "children" acl */
145                 if ( ! access_allowed( be, conn, op, p,
146                         "children", NULL, ACL_WRITE ) )
147                 {
148                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
149                                 0, 0 );
150                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
151                                 NULL, NULL, NULL, NULL );
152                         goto return_results;
153                 }
154
155                 Debug( LDAP_DEBUG_TRACE,
156                        "ldbm_back_modrdn: wr to children of entry %s OK\n",
157                        p_ndn, 0, 0 );
158                 
159                 p_dn = dn_parent( be, e->e_dn );
160         
161
162                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
163                        p_dn, 0, 0 );
164
165         } else {
166                 /* no parent, modrdn entry directly under root */
167                 if( ! be_isroot( be, op->o_ndn ) ) {
168                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
169                                 0, 0, 0);
170                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
171                                 NULL, NULL, NULL, NULL );
172                         goto return_results;
173                 }
174
175                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
176                 rootlock = 1;
177                 
178                 Debug( LDAP_DEBUG_TRACE,
179                        "ldbm_back_modrdn: no parent, locked root\n",
180                        0, 0, 0 );
181
182         }
183
184         new_parent_dn = p_dn;   /* New Parent unless newSuperior given */
185
186         if ( (np_dn = newSuperior) != NULL) {
187                 Debug( LDAP_DEBUG_TRACE, 
188                        "ldbm_back_modrdn: new parent requested...\n",
189                        0, 0, 0 );
190
191                 np_ndn = ch_strdup( np_dn );
192                 (void) dn_normalize_case( np_ndn );
193
194                 /* newSuperior == oldParent?, if so ==> ERROR */
195                 /* newSuperior == entry being moved?, if so ==> ERROR */
196                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
197
198                 if( (np = dn2entry_w( be, np_ndn, &matched )) == NULL) {
199                         Debug( LDAP_DEBUG_TRACE,
200                                "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
201                                np_ndn, 0, 0);
202                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
203                                 NULL, NULL, NULL, NULL );
204                         goto return_results;
205                 }
206
207                 Debug( LDAP_DEBUG_TRACE,
208                        "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
209                        np, np->e_id, 0 );
210             
211                 /* check newSuperior for "children" acl */
212                 if ( !access_allowed( be, conn, op, np, "children", NULL,
213                                       ACL_WRITE ) )
214                 {
215                         Debug( LDAP_DEBUG_TRACE,
216                                "ldbm_back_modrdn: no wr to newSup children\n",
217                                0, 0, 0 );
218                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
219                                 NULL, NULL, NULL, NULL );
220                         goto return_results;
221                 }
222
223                 if ( is_entry_alias( np ) ) {
224                         /* entry is an alias, don't allow bind */
225                         Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
226                             0, 0 );
227
228                         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
229                             NULL, NULL, NULL, NULL );
230
231                         goto return_results;
232                 }
233
234                 if ( is_entry_referral( np ) ) {
235                         /* parent is a referral, don't allow add */
236                         /* parent is an alias, don't allow add */
237                         Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
238                                 0, 0 );
239
240                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
241                             NULL, NULL, NULL, NULL );
242
243                         goto return_results;
244                 }
245
246                 Debug( LDAP_DEBUG_TRACE,
247                        "ldbm_back_modrdn: wr to new parent's children OK\n",
248                        0, 0 , 0 );
249
250                 new_parent_dn = np_dn;
251         }
252         
253         /* Build target dn and make sure target entry doesn't exist already. */
254
255         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
256
257
258         new_ndn = ch_strdup(new_dn);
259         (void) dn_normalize_case( new_ndn );
260
261         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
262                new_ndn, 0, 0 );
263
264         if (dn2id ( be, new_ndn ) != NOID) {
265                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
266                         NULL, NULL, NULL, NULL );
267                 goto return_results;
268         }
269
270         Debug( LDAP_DEBUG_TRACE,
271                "ldbm_back_modrdn: new ndn=%s does not exist\n",
272                new_ndn, 0, 0 );
273
274         /* check for abandon */
275         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
276         if ( op->o_abandon ) {
277                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
278                 goto return_results;
279         }
280         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
281
282         /* delete old one */
283         if ( dn2id_delete( be, e->e_ndn ) != 0 ) {
284                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
285                         NULL, NULL, NULL, NULL );
286                 goto return_results;
287         }
288
289         (void) cache_delete_entry( &li->li_cache, e );
290         free( e->e_dn );
291         free( e->e_ndn );
292         e->e_dn = new_dn;
293         e->e_ndn = new_ndn;
294
295         /* add new one */
296         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
297                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
298                         NULL, NULL, NULL, NULL );
299                 goto return_results;
300         }
301
302
303         /* Get attribute type and attribute value of our new rdn, we will
304          * need to add that to our new entry
305          */
306
307         if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
308             
309                 Debug( LDAP_DEBUG_TRACE,
310                        "ldbm_back_modrdn: can't figure out type of newrdn\n",
311                        0, 0, 0 );
312                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
313                         NULL, NULL, NULL, NULL );
314                 goto return_results;            
315
316         }
317
318         if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
319             
320                 Debug( LDAP_DEBUG_TRACE,
321                        "ldbm_back_modrdn: can't figure out val of newrdn\n",
322                        0, 0, 0 );
323                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
324                         NULL, NULL, NULL, NULL );
325                 goto return_results;            
326
327         }
328
329         Debug( LDAP_DEBUG_TRACE,
330                "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
331                new_rdn_val, new_rdn_type, 0 );
332
333         /* Retrieve the old rdn from the entry's dn */
334
335         if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
336
337                 Debug( LDAP_DEBUG_TRACE,
338                        "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
339                        0, 0, 0 );
340                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
341                         NULL, NULL, NULL, NULL );
342                 goto return_results;            
343
344         }
345
346         if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
347             
348                 Debug( LDAP_DEBUG_TRACE,
349                        "ldbm_back_modrdn: can't figure out the old_rdn type\n",
350                        0, 0, 0 );
351                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
352                         NULL, NULL, NULL, NULL );
353                 goto return_results;            
354                 
355         }
356         
357         if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
358
359             /* Not a big deal but we may say something */
360             Debug( LDAP_DEBUG_TRACE,
361                    "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
362                    old_rdn_type, new_rdn_type, 0 );
363             
364         }               
365
366 #ifdef DNS_DN
367         if ( dn_type( old_rdn ) == DN_X500 ) {
368 #endif
369
370                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
371                        0, 0, 0 );
372                 
373                 /* Add new attribute value to the entry.
374                  */
375
376                 add_bvals[0] = &add_bv;         /* Array of bervals */
377                 add_bvals[1] = NULL;
378
379                 add_bv.bv_val = new_rdn_val;
380                 add_bv.bv_len = strlen(new_rdn_val);
381                 
382                 mod[0].ml_type = new_rdn_type;  
383                 mod[0].ml_bvalues = add_bvals;
384                 mod[0].ml_op = LDAP_MOD_SOFTADD;
385                 mod[0].ml_next = NULL;
386
387                 /* Remove old rdn value if required */
388
389                 if (deleteoldrdn) {
390                         /* Get value of old rdn */
391         
392                         if ((old_rdn_val = rdn_attr_value( old_rdn ))
393                             == NULL) {
394                             
395                                 Debug( LDAP_DEBUG_TRACE,
396                                        "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
397                                        0, 0, 0 );
398                                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
399                                         NULL, NULL, NULL, NULL );
400                                 goto return_results;            
401                         }
402
403                         del_bvals[0] = &del_bv;         /* Array of bervals */
404                         del_bvals[1] = NULL;
405
406                         /* Remove old value of rdn as an attribute. */
407                     
408                         del_bv.bv_val = old_rdn_val;
409                         del_bv.bv_len = strlen(old_rdn_val);
410
411                         /* No need to normalize old_rdn_type, delete_values()
412                          * does that for us
413                          */
414                         mod[0].ml_next = &mod[1];
415                         mod[1].ml_type = old_rdn_type;  
416                         mod[1].ml_bvalues = del_bvals;
417                         mod[1].ml_op = LDAP_MOD_DELETE;
418                         mod[1].ml_next = NULL;
419
420                         Debug( LDAP_DEBUG_TRACE,
421                                "ldbm_back_modrdn: removing old_rdn_val=%s\n",
422                                old_rdn_val, 0, 0 );
423                 }
424         
425 #ifdef DNS_DN
426         } else {
427                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
428                        0, 0, 0 );
429                 /* XXXV3: not sure of what to do here */
430                 Debug( LDAP_DEBUG_TRACE,
431                        "ldbm_back_modrdn: not fully implemented...\n",
432                        0, 0, 0 );
433   
434                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
435                         NULL, NULL, NULL, NULL );
436                 goto return_results;
437
438         }
439 #endif
440
441         /* modify memory copy of entry */
442         if ( ldbm_modify_internal( be, conn, op, dn, &mod[0], e )
443              != 0 ) {
444             
445             goto return_results;
446             
447         }
448         
449         (void) cache_update_entry( &li->li_cache, e );
450
451         /* NOTE: after this you must not free new_dn or new_ndn!
452          * They are used by cache.
453          */
454
455         /* id2entry index */
456         if ( id2entry_add( be, e ) != 0 ) {
457                 entry_free( e );
458                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
459                         NULL, NULL, NULL, NULL );
460                 goto return_results_after;
461         }
462
463         send_ldap_result( conn, op, LDAP_SUCCESS,
464                 NULL, NULL, NULL, NULL );
465         rc = 0;
466         goto return_results_after;      
467
468 return_results:
469         if( new_dn != NULL ) free( new_dn );
470         if( new_ndn != NULL ) free( new_ndn );
471
472 return_results_after:
473         /* NOTE:
474          * new_dn and new_ndn are not deallocated because they are used by
475          * the cache entry.
476          */
477         if( p_dn != NULL ) free( p_dn );
478         if( p_ndn != NULL ) free( p_ndn );
479
480         if( matched != NULL ) free( matched );
481
482         /* LDAP v2 supporting correct attribute handling. */
483         if( new_rdn_type != NULL ) free(new_rdn_type);
484         if( new_rdn_val != NULL ) free(new_rdn_val);
485         if( old_rdn != NULL ) free(old_rdn);
486         if( old_rdn_type != NULL ) free(old_rdn_type);
487         if( old_rdn_val != NULL ) free(old_rdn_val);
488
489
490         /* LDAP v3 Support */
491         if ( np_dn != NULL ) free( np_dn );
492         if ( np_ndn != NULL ) free( np_ndn );
493
494         if( np != NULL ) {
495                 /* free new parent and writer lock */
496                 cache_return_entry_w( &li->li_cache, np );
497         }
498
499         if( p != NULL ) {
500                 /* free parent and writer lock */
501                 cache_return_entry_w( &li->li_cache, p );
502         }
503
504         if ( rootlock ) {
505                 /* release root writer lock */
506                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
507         }
508
509         /* free entry and writer lock */
510         cache_return_entry_w( &li->li_cache, e );
511         return( rc );
512 }