]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/modrdn.c
neutralize previous add.c change until bdb2_back_entry_release_rw() is provided
[openldap] / servers / slapd / back-bdb2 / modrdn.c
1 /* modrdn.c - bdb2 backend modrdn routine */
2
3 /*
4  * LDAP v3 newSuperior support.
5  *
6  * Copyright 1999, Juan C. Gomez, All rights reserved.
7  * This software is not subject to any license of Silicon Graphics 
8  * Inc. or Purdue University.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * without restriction or fee of any kind as long as this notice
12  * is preserved.
13  *
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19
20 #include <ac/string.h>
21 #include <ac/socket.h>
22
23 #include "slap.h"
24 #include "back-bdb2.h"
25 #include "proto-back-bdb2.h"
26
27 static int
28 bdb2i_back_modrdn_internal(
29     BackendDB   *be,
30     Connection  *conn,
31     Operation   *op,
32     char        *dn,
33     char        *newrdn,
34     int         deleteoldrdn,
35     char        *newSuperior
36 )
37 {
38         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
39         char            *matched = NULL;
40         char            *p_dn = NULL, *p_ndn = NULL;
41         char            *new_dn = NULL, *new_ndn = NULL;
42         char            sep[2];
43         Entry           *e, *p = NULL;
44         int                     rc = -1;
45         /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
46         char            *new_rdn_val = NULL;    /* Val of new rdn */
47         char            *new_rdn_type = NULL;   /* Type of new rdn */
48         char            *old_rdn;               /* Old rdn's attr type & val */
49         char            *old_rdn_type = NULL;   /* Type of old rdn attr. */
50         char            *old_rdn_val = NULL;    /* Old rdn attribute value */
51         struct berval   bv;                     /* Stores new rdn att */
52         struct berval   *bvals[2];              /* Stores new rdn att */
53         LDAPMod         mod;                    /* Used to delete old rdn */
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
60         Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
61                (newSuperior ? newSuperior : "NULL"),
62                0, 0 );
63
64         /* get entry with writer lock */
65         if ( (e = bdb2i_dn2entry_w( be, dn, &matched )) == NULL ) {
66                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, "" );
67                 if ( matched != NULL ) {
68                         free( matched );
69                 }
70                 return( -1 );
71         }
72
73 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
74                 /* check parent for "children" acl */
75         if ( ! access_allowed( be, conn, op, e,
76                 "entry", NULL, ACL_WRITE ) )
77         {
78                 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
79                         0, 0 );
80                 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
81                         "", "" );
82                 goto return_results;
83         }
84 #endif
85
86         if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
87                 /* parent + rdn + separator(s) + null */
88                 if( (p = bdb2i_dn2entry_w( be, p_ndn, &matched )) == NULL) {
89                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
90                                 0, 0, 0);
91                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
92                                 "", "");
93                         goto return_results;
94                 }
95
96 #ifndef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
97                 /* check parent for "children" acl */
98                 if ( ! access_allowed( be, conn, op, p,
99                         "children", NULL, ACL_WRITE ) )
100                 {
101                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
102                                 0, 0 );
103                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
104                                 "", "" );
105                         goto return_results;
106                 }
107 #endif
108
109                 p_dn = dn_parent( be, e->e_dn );
110
111                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
112                        p_dn, 0, 0 );
113
114
115         } else {
116                 /* no parent, modrdn entry directly under root */
117                 if( ! be_isroot( be, op->o_ndn ) ) {
118                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
119                                 0, 0, 0);
120                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
121                                 "", "");
122                         goto return_results;
123                 }
124
125                 Debug( LDAP_DEBUG_TRACE,
126                        "ldbm_back_modrdn: no parent!, working on root\n",
127                        0, 0, 0 );
128
129         }
130
131         new_parent_dn = p_dn;   /* New Parent unless newSuperior given */
132
133         if ( (np_dn = newSuperior) != NULL) {
134
135
136                 Debug( LDAP_DEBUG_TRACE, 
137                        "ldbm_back_modrdn: new parent requested...\n",
138                        0, 0, 0 );
139
140                 np_ndn = dn_normalize_case( ch_strdup( np_dn ) );
141
142                 /* newSuperior == oldParent?, if so ==> ERROR */
143
144                 /* newSuperior == entry being moved?, if so ==> ERROR */
145
146                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
147
148                 if( (np = bdb2i_dn2entry_w( be, np_ndn, &matched )) == NULL) {
149
150                         Debug( LDAP_DEBUG_TRACE,
151                                "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
152                                np_ndn, 0, 0);
153                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "",
154                                           "");
155                         goto return_results;
156                 }
157
158                 Debug( LDAP_DEBUG_TRACE,
159                        "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
160                        np, np->e_id, 0 );
161             
162 #ifndef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
163                 /* check newSuperior for "children" acl */
164                 if ( !access_allowed( be, conn, op, np, "children", NULL,
165                                       ACL_WRITE ) )
166                 {
167                         Debug( LDAP_DEBUG_TRACE,
168                                "ldbm_back_modrdn: no wr to newSup children\n",
169                                0, 0, 0 );
170                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
171                                           "", "" );
172                         goto return_results;
173                 }
174 #endif
175
176                 Debug( LDAP_DEBUG_TRACE,
177                        "ldbm_back_modrdn: wr to new parent's children OK\n",
178                        0, 0 , 0 );
179
180
181                 new_parent_dn = np_dn;
182
183         }
184         
185         /* Build target dn and make sure target entry doesn't exist already. */
186
187         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
188
189
190         new_ndn = dn_normalize_case( ch_strdup( new_dn ) );
191
192         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
193                new_ndn, 0, 0 );
194
195         if ( (bdb2i_dn2id ( be, new_ndn ) ) != NOID ) {
196                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, NULL, NULL );
197                 goto return_results;
198         }
199
200         /* check for abandon */
201         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
202         if ( op->o_abandon ) {
203                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
204                 goto return_results;
205         }
206         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
207
208         /* add new one */
209         if ( bdb2i_dn2id_add( be, new_ndn, e->e_id ) != 0 ) {
210                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
211                 goto return_results;
212         }
213
214         /* delete old one */
215         if ( bdb2i_dn2id_delete( be, e->e_ndn ) != 0 ) {
216                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
217                 goto return_results;
218         }
219
220         (void) bdb2i_cache_delete_entry( &li->li_cache, e );
221         free( e->e_dn );
222         free( e->e_ndn );
223         e->e_dn = new_dn;
224         e->e_ndn = new_ndn;
225
226         /* Get attribute type and attribute value of our new rdn, we will
227          * need to add that to our new entry
228          */
229
230         if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
231             
232                 Debug( LDAP_DEBUG_TRACE,
233                        "ldbm_back_modrdn: can't figure out type of newrdn\n",
234                        0, 0, 0 );
235                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
236                 goto return_results;            
237
238         }
239
240         if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
241             
242                 Debug( LDAP_DEBUG_TRACE,
243                        "ldbm_back_modrdn: can't figure out val of newrdn\n",
244                        0, 0, 0 );
245                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
246                 goto return_results;            
247
248         }
249
250         Debug( LDAP_DEBUG_TRACE,
251                "ldbm_back_modrdn: new_rdn_val=%s, new_rdn_type=%s\n",
252                new_rdn_val, new_rdn_type, 0 );
253
254         /* Retrieve the old rdn from the entry's dn */
255
256         if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
257
258                 Debug( LDAP_DEBUG_TRACE,
259                        "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
260                        0, 0, 0 );
261                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
262                 goto return_results;            
263
264         }
265
266         if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
267             
268                 Debug( LDAP_DEBUG_TRACE,
269                        "ldbm_back_modrdn: can't figure out the old_rdn type\n",
270                        0, 0, 0 );
271                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
272                 goto return_results;            
273                 
274         }
275         
276         if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
277
278             /* Not a big deal but we may say something */
279             Debug( LDAP_DEBUG_TRACE,
280                    "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
281                    old_rdn_type, new_rdn_type, 0 );
282             
283         }               
284
285         if ( dn_type( old_rdn ) == DN_X500 ) {
286
287                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
288                        0, 0, 0 );
289
290                 bvals[0] = &bv;         /* Array of bervals */
291                 bvals[1] = NULL;
292
293                 /* Remove old rdn value if required */
294
295                 if (deleteoldrdn) {
296
297                         /* Get value of old rdn */
298         
299                         if ((old_rdn_val = rdn_attr_value( old_rdn ))
300                             == NULL) {
301                             
302                                 Debug( LDAP_DEBUG_TRACE,
303                                        "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
304                                        0, 0, 0 );
305                                 send_ldap_result( conn, op,
306                                                   LDAP_OPERATIONS_ERROR,
307                                                   "", "" );
308                                 goto return_results;            
309
310
311                         }
312
313                         /* Remove old value of rdn as an attribute. */
314                     
315                         bv.bv_val = old_rdn_val;
316                         bv.bv_len = strlen(old_rdn_val);
317
318                         /* No need to normalize old_rdn_type, delete_values()
319                          * does that for us
320                          */
321                         mod.mod_type = old_rdn_type;    
322                         mod.mod_bvalues = bvals;
323                         mod.mod_op = LDAP_MOD_DELETE;   /* XXX:really needed?*/
324
325                         /* Assembly mod structure */
326
327                         if ( delete_values( e, &mod, op->o_ndn )
328                              != LDAP_SUCCESS ) {
329
330                                 /* Could not find old_rdn as an attribute or
331                                  * the old value was not present. Return an 
332                                  * error.
333                                  */
334                                 Debug( LDAP_DEBUG_TRACE,
335                                        "ldbm_back_modrdn: old rdn not found or att type doesn't exist\n",
336                                        0, 0, 0);
337                                 send_ldap_result( conn, op,
338                                                   LDAP_OPERATIONS_ERROR,
339                                                   "", "");
340                                 goto return_results;
341
342                         }
343
344                         Debug( LDAP_DEBUG_TRACE,
345                                "ldbm_back_modrdn: removed old_rdn_val=%s\n",
346                                old_rdn_val, 0, 0 );
347                 
348                 }/* if (deleteoldrdn) */
349
350                 /* Add new attribute value to the entry.
351                  */
352
353                 bv.bv_val = new_rdn_val;
354                 bv.bv_len = strlen(new_rdn_val);
355                 
356                 
357                 Debug( LDAP_DEBUG_TRACE,
358                        "ldbm_back_modrdn: adding new rdn attr val =%s\n",
359                        new_rdn_val, 0, 0 );
360                 
361                 /* No need to normalize new_rdn_type, attr_merge does it */
362
363                 attr_merge( e, new_rdn_type, bvals );
364         
365         } else {
366             
367
368                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
369                        0, 0, 0 );
370                 /* XXXV3: not sure of what to do here */
371                 Debug( LDAP_DEBUG_TRACE,
372                        "ldbm_back_modrdn: not fully implemented...\n",
373                        0, 0, 0 );  
374
375         }
376
377         (void) bdb2i_cache_update_entry( &li->li_cache, e );
378
379         /* id2entry index */
380         if ( bdb2i_id2entry_add( be, e ) != 0 ) {
381                 entry_free( e );
382                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
383                 goto return_results;
384         }
385
386         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
387         rc = 0;
388
389 return_results:
390         /* NOTE:
391          * new_dn and new_ndn are not deallocated because they are used by
392          * the cache entry.
393          */
394         if( p_dn != NULL ) free( p_dn );
395         if( p_ndn != NULL ) free( p_ndn );
396
397         if( matched != NULL ) free( matched );
398
399         /* LDAP v2 supporting correct attribute handling. */
400         if( new_rdn_type != NULL ) free(new_rdn_type);
401         if( new_rdn_val != NULL ) free(new_rdn_val);
402         if( old_rdn != NULL ) free(old_rdn);
403         if( old_rdn_type != NULL ) free(old_rdn_type);
404         if( old_rdn_val != NULL ) free(old_rdn_val);
405
406         /* LDAP v3 Support */
407         if ( np_dn != NULL ) free( np_dn );
408         if ( np_ndn != NULL ) free( np_ndn );
409
410         if( p != NULL ) {
411                 /* free parent and writer lock */
412                 bdb2i_cache_return_entry_w( &li->li_cache, p );
413
414         }
415
416         /* free entry and writer lock */
417         bdb2i_cache_return_entry_w( &li->li_cache, e );
418         return( rc );
419 }
420
421
422 int
423 bdb2_back_modrdn(
424     BackendDB   *be,
425     Connection  *conn,
426     Operation   *op,
427     char        *dn,
428     char        *newrdn,
429     int         deleteoldrdn,
430     char        *newSuperior
431 )
432 {
433         DB_LOCK         lock;
434         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
435         struct timeval  time1;
436         int             ret;
437
438         bdb2i_start_timing( be->bd_info, &time1 );
439
440         if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
441
442                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
443                 return( -1 );
444
445         }
446
447         ret = bdb2i_back_modrdn_internal( be, conn, op, dn,
448                                         newrdn, deleteoldrdn,
449                                         newSuperior );
450
451         (void) bdb2i_leave_backend_w( lock );
452         bdb2i_stop_timing( be->bd_info, time1, "MODRDN", conn, op );
453
454         return( ret );
455 }
456
457