]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/modrdn.c
Merge in all devel changes since 2.0-alpha2.
[openldap] / servers / slapd / back-bdb2 / modrdn.c
1 /* modrdn.c - bdb2 backend modrdn routine */
2 /* $OpenLDAP$ */
3
4 /*
5  * LDAP v3 newSuperior support.
6  *
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.
10  *
11  * Redistribution and use in source and binary forms are permitted
12  * without restriction or fee of any kind as long as this notice
13  * is preserved.
14  *
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22 #include <ac/socket.h>
23
24 #include "slap.h"
25 #include "back-bdb2.h"
26 #include "proto-back-bdb2.h"
27
28 static int
29 bdb2i_back_modrdn_internal(
30     BackendDB   *be,
31     Connection  *conn,
32     Operation   *op,
33     char        *dn,
34     char        *newrdn,
35     int         deleteoldrdn,
36     char        *newSuperior
37 )
38 {
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;
43         char            sep[2];
44         Entry           *e, *p = 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 */
62
63         Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
64                (newSuperior ? newSuperior : "NULL"),
65                0, 0 );
66
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;
71
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 )
76                                 : NULL;
77                         bdb2i_cache_return_entry_r( &li->li_cache, matched );
78                 } else {
79                         refs = default_referral;
80                 }
81
82                 send_ldap_result( conn, op, LDAP_REFERRAL,
83                         matched_dn, NULL, refs, NULL );
84
85                 if( matched != NULL ) {
86                         ber_bvecfree( refs );
87                         free( matched_dn );
88                 }
89
90                 return( -1 );
91         }
92
93         if (!manageDSAit && is_entry_referral( e ) ) {
94                 /* entry is a referral, don't allow add */
95                 struct berval **refs = get_entry_referrals( be,
96                         conn, op, e );
97
98                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
99                         0, 0 );
100
101                 send_ldap_result( conn, op, LDAP_REFERRAL,
102                         e->e_dn, NULL, refs, NULL );
103
104                 ber_bvecfree( refs );
105
106                 goto return_results;
107         }
108
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 ) )
113         {
114                 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
115                         0, 0 );
116                 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
117                         NULL, NULL, NULL, NULL );
118                 goto return_results;
119         }
120 #endif
121
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",
126                                 0, 0, 0);
127                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
128                                 NULL, NULL, NULL, NULL );
129                         goto return_results;
130                 }
131
132                 /* check parent for "children" acl */
133                 if ( ! access_allowed( be, conn, op, p,
134                         "children", NULL, ACL_WRITE ) )
135                 {
136                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
137                                 0, 0 );
138                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
139                                 NULL, NULL, NULL, NULL );
140                         goto return_results;
141                 }
142
143                 p_dn = dn_parent( be, e->e_dn );
144
145                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
146                        p_dn, 0, 0 );
147
148
149         } else {
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",
153                                 0, 0, 0);
154                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
155                                 NULL, NULL, NULL, NULL );
156                         goto return_results;
157                 }
158
159                 Debug( LDAP_DEBUG_TRACE,
160                        "ldbm_back_modrdn: no parent!, working on root\n",
161                        0, 0, 0 );
162
163         }
164
165         new_parent_dn = p_dn;   /* New Parent unless newSuperior given */
166
167         if ( (np_dn = newSuperior) != NULL) {
168
169
170                 Debug( LDAP_DEBUG_TRACE, 
171                        "ldbm_back_modrdn: new parent requested...\n",
172                        0, 0, 0 );
173
174                 np_ndn = ch_strdup( np_dn );
175                 (void) dn_normalize_case( np_ndn );
176
177                 /* newSuperior == oldParent?, if so ==> ERROR */
178
179                 /* newSuperior == entry being moved?, if so ==> ERROR */
180
181                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
182
183                 if( (np = bdb2i_dn2entry_w( be, np_ndn, &matched )) == NULL) {
184
185                         Debug( LDAP_DEBUG_TRACE,
186                                "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
187                                np_ndn, 0, 0);
188                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
189                                 NULL, NULL, NULL, NULL );
190                         goto return_results;
191                 }
192
193                 Debug( LDAP_DEBUG_TRACE,
194                        "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
195                        np, np->e_id, 0 );
196             
197                 /* check newSuperior for "children" acl */
198                 if ( !access_allowed( be, conn, op, np, "children", NULL,
199                                       ACL_WRITE ) )
200                 {
201                         Debug( LDAP_DEBUG_TRACE,
202                                "ldbm_back_modrdn: no wr to newSup children\n",
203                                0, 0, 0 );
204                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
205                                 NULL, NULL, NULL, NULL );
206                         goto return_results;
207                 }
208
209                 Debug( LDAP_DEBUG_TRACE,
210                        "ldbm_back_modrdn: wr to new parent's children OK\n",
211                        0, 0 , 0 );
212
213
214                 new_parent_dn = np_dn;
215
216         }
217         
218         /* Build target dn and make sure target entry doesn't exist already. */
219
220         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
221
222
223         new_ndn = ch_strdup( new_dn );
224         (void) dn_normalize_case( new_ndn );
225
226         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
227                new_ndn, 0, 0 );
228
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 );
233                 goto return_results;
234         }
235         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
236
237         if ( (bdb2i_dn2id ( be, new_ndn ) ) != NOID ) {
238                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
239                         NULL, NULL, NULL, NULL );
240                 goto return_results;
241         }
242
243         /* Get attribute type and attribute value of our new rdn, we will
244          * need to add that to our new entry
245          */
246
247         if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
248             
249                 Debug( LDAP_DEBUG_TRACE,
250                        "ldbm_back_modrdn: can't figure out type of newrdn\n",
251                        0, 0, 0 );
252                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
253                         NULL, NULL, NULL, NULL );
254                 goto return_results;            
255
256         }
257
258         if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
259             
260                 Debug( LDAP_DEBUG_TRACE,
261                        "ldbm_back_modrdn: can't figure out val of newrdn\n",
262                        0, 0, 0 );
263                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
264                         NULL, NULL, NULL, NULL );
265                 goto return_results;            
266
267         }
268
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 );
272
273         /* Retrieve the old rdn from the entry's dn */
274
275         if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
276
277                 Debug( LDAP_DEBUG_TRACE,
278                        "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
279                        0, 0, 0 );
280                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
281                         NULL, NULL, NULL, NULL );
282                 goto return_results;            
283
284         }
285
286         if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
287             
288                 Debug( LDAP_DEBUG_TRACE,
289                        "ldbm_back_modrdn: can't figure out the old_rdn type\n",
290                        0, 0, 0 );
291                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
292                         NULL, NULL, NULL, NULL );
293                 goto return_results;            
294                 
295         }
296         
297         if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
298
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 );
303             
304         }               
305
306 #ifdef DNS_DN
307         if ( dn_type( old_rdn ) == DN_X500 ) {
308 #endif
309
310                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
311                        0, 0, 0 );
312
313                 /* Add new attribute value to the entry.
314                  */
315
316                 add_bvals[0] = &add_bv;         /* Array of bervals */
317                 add_bvals[1] = NULL;
318
319                 add_bv.bv_val = new_rdn_val;
320                 add_bv.bv_len = strlen(new_rdn_val);
321                 
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;
326                 
327                 Debug( LDAP_DEBUG_TRACE,
328                        "ldbm_back_modrdn: adding new rdn attr val =%s\n",
329                        new_rdn_val, 0, 0 );
330                 
331                 /* Remove old rdn value if required */
332
333                 if (deleteoldrdn) {
334
335                         del_bvals[0] = &del_bv;         /* Array of bervals */
336                         del_bvals[1] = NULL;
337                         /* Get value of old rdn */
338         
339                         if ((old_rdn_val = rdn_attr_value( old_rdn ))
340                             == NULL) {
341                             
342                                 Debug( LDAP_DEBUG_TRACE,
343                                        "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
344                                        0, 0, 0 );
345                                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
346                                         NULL, NULL, NULL, NULL );
347                                 goto return_results;            
348
349
350                         }
351
352                         /* Remove old value of rdn as an attribute. */
353                     
354                         del_bv.bv_val = old_rdn_val;
355                         del_bv.bv_len = strlen(old_rdn_val);
356                         
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;
362
363
364                         Debug( LDAP_DEBUG_TRACE,
365                                "ldbm_back_modrdn: removing old_rdn_val=%s\n",
366                                old_rdn_val, 0, 0 );
367                 
368                 }/* if (deleteoldrdn) */
369
370 #ifdef DNS_DN
371         } else {
372             
373
374                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
375                        0, 0, 0 );
376                 /* XXXV3: not sure of what to do here */
377                 Debug( LDAP_DEBUG_TRACE,
378                        "ldbm_back_modrdn: not fully implemented...\n",
379                        0, 0, 0 );  
380
381         }
382 #endif
383
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 );
388                 goto return_results;
389         }
390         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
391
392         /* delete old one */
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 );
396                 goto return_results;
397         }
398
399         (void) bdb2i_cache_delete_entry( &li->li_cache, e );
400
401         free( e->e_dn );
402         free( e->e_ndn );
403         e->e_dn = new_dn;
404         e->e_ndn = new_ndn;
405         new_dn = NULL;
406         new_ndn = NULL;
407
408         /* add new one */
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 );
412                 goto return_results;
413         }
414
415         /* modify memory copy of entry */
416         if ( bdb2i_back_modify_internal( be, conn, op, dn, &mod[0], e )
417              != 0 ) {
418             
419                 goto return_results;
420                         
421         }
422         
423         (void) bdb2i_cache_update_entry( &li->li_cache, e );
424
425         /* NOTE: after this you must not free new_dn or new_ndn!
426          * They are used by cache.
427          */
428
429         /* id2entry index */
430         if ( bdb2i_id2entry_add( be, e ) != 0 ) {
431                 entry_free( e );
432                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
433                         NULL, NULL, NULL, NULL );
434                 goto return_results;
435         }
436
437         send_ldap_result( conn, op, LDAP_SUCCESS,
438                 NULL, NULL, NULL, NULL );
439         rc = 0;
440
441 return_results:
442         if( new_dn != NULL ) free( new_dn );
443         if( new_ndn != NULL ) free( new_ndn );
444
445         /* NOTE:
446          * new_dn and new_ndn are not deallocated because they are used by
447          * the cache entry.
448          */
449         if( p_dn != NULL ) free( p_dn );
450         if( p_ndn != NULL ) free( p_ndn );
451
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);
458
459         /* LDAP v3 Support */
460         if ( np_dn != NULL ) free( np_dn );
461         if ( np_ndn != NULL ) free( np_ndn );
462
463         if( p != NULL ) {
464                 /* free parent and writer lock */
465                 bdb2i_cache_return_entry_w( &li->li_cache, p );
466
467         }
468
469         /* free entry and writer lock */
470         bdb2i_cache_return_entry_w( &li->li_cache, e );
471         return( rc );
472 }
473
474
475 int
476 bdb2_back_modrdn(
477     BackendDB   *be,
478     Connection  *conn,
479     Operation   *op,
480     char        *dn,
481     char        *newrdn,
482     int         deleteoldrdn,
483     char        *newSuperior
484 )
485 {
486         DB_LOCK         lock;
487         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
488         struct timeval  time1;
489         int             ret;
490
491         bdb2i_start_timing( be->bd_info, &time1 );
492
493         if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
494                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
495                         NULL, NULL, NULL, NULL );
496                 return( -1 );
497
498         }
499
500         ret = bdb2i_back_modrdn_internal( be, conn, op, dn,
501                                         newrdn, deleteoldrdn,
502                                         newSuperior );
503
504         (void) bdb2i_leave_backend_w( lock );
505         bdb2i_stop_timing( be->bd_info, time1, "MODRDN", conn, op );
506
507         return( ret );
508 }
509
510