]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/modrdn.c
Make bdb2i_back_modify_internal() public so it can be reused from
[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                 /* check parent for "children" acl */
97                 if ( ! access_allowed( be, conn, op, p,
98                         "children", NULL, ACL_WRITE ) )
99                 {
100                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
101                                 0, 0 );
102                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
103                                 "", "" );
104                         goto return_results;
105                 }
106
107                 p_dn = dn_parent( be, e->e_dn );
108
109                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
110                        p_dn, 0, 0 );
111
112
113         } else {
114                 /* no parent, modrdn entry directly under root */
115                 if( ! be_isroot( be, op->o_ndn ) ) {
116                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
117                                 0, 0, 0);
118                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
119                                 "", "");
120                         goto return_results;
121                 }
122
123                 Debug( LDAP_DEBUG_TRACE,
124                        "ldbm_back_modrdn: no parent!, working on root\n",
125                        0, 0, 0 );
126
127         }
128
129         new_parent_dn = p_dn;   /* New Parent unless newSuperior given */
130
131         if ( (np_dn = newSuperior) != NULL) {
132
133
134                 Debug( LDAP_DEBUG_TRACE, 
135                        "ldbm_back_modrdn: new parent requested...\n",
136                        0, 0, 0 );
137
138                 np_ndn = dn_normalize_case( ch_strdup( np_dn ) );
139
140                 /* newSuperior == oldParent?, if so ==> ERROR */
141
142                 /* newSuperior == entry being moved?, if so ==> ERROR */
143
144                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
145
146                 if( (np = bdb2i_dn2entry_w( be, np_ndn, &matched )) == NULL) {
147
148                         Debug( LDAP_DEBUG_TRACE,
149                                "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
150                                np_ndn, 0, 0);
151                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "",
152                                           "");
153                         goto return_results;
154                 }
155
156                 Debug( LDAP_DEBUG_TRACE,
157                        "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
158                        np, np->e_id, 0 );
159             
160                 /* check newSuperior for "children" acl */
161                 if ( !access_allowed( be, conn, op, np, "children", NULL,
162                                       ACL_WRITE ) )
163                 {
164                         Debug( LDAP_DEBUG_TRACE,
165                                "ldbm_back_modrdn: no wr to newSup children\n",
166                                0, 0, 0 );
167                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
168                                           "", "" );
169                         goto return_results;
170                 }
171
172                 Debug( LDAP_DEBUG_TRACE,
173                        "ldbm_back_modrdn: wr to new parent's children OK\n",
174                        0, 0 , 0 );
175
176
177                 new_parent_dn = np_dn;
178
179         }
180         
181         /* Build target dn and make sure target entry doesn't exist already. */
182
183         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
184
185
186         new_ndn = dn_normalize_case( ch_strdup( new_dn ) );
187
188         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
189                new_ndn, 0, 0 );
190
191         if ( (bdb2i_dn2id ( be, new_ndn ) ) != NOID ) {
192                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, NULL, NULL );
193                 goto return_results;
194         }
195
196         /* check for abandon */
197         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
198         if ( op->o_abandon ) {
199                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
200                 goto return_results;
201         }
202         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
203
204         /* delete old one */
205         if ( bdb2i_dn2id_delete( be, e->e_ndn ) != 0 ) {
206                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
207                 goto return_results;
208         }
209
210         (void) bdb2i_cache_delete_entry( &li->li_cache, e );
211         free( e->e_dn );
212         free( e->e_ndn );
213         e->e_dn = new_dn;
214         e->e_ndn = new_ndn;
215
216
217         /* add new one */
218         if ( bdb2i_dn2id_add( be,  e->e_ndn, e->e_id ) != 0 ) {
219                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
220                 goto return_results;
221         }
222
223         /* Get attribute type and attribute value of our new rdn, we will
224          * need to add that to our new entry
225          */
226
227         if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
228             
229                 Debug( LDAP_DEBUG_TRACE,
230                        "ldbm_back_modrdn: can't figure out type of newrdn\n",
231                        0, 0, 0 );
232                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
233                 goto return_results;            
234
235         }
236
237         if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
238             
239                 Debug( LDAP_DEBUG_TRACE,
240                        "ldbm_back_modrdn: can't figure out val of newrdn\n",
241                        0, 0, 0 );
242                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
243                 goto return_results;            
244
245         }
246
247         Debug( LDAP_DEBUG_TRACE,
248                "ldbm_back_modrdn: new_rdn_val=%s, new_rdn_type=%s\n",
249                new_rdn_val, new_rdn_type, 0 );
250
251         /* Retrieve the old rdn from the entry's dn */
252
253         if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
254
255                 Debug( LDAP_DEBUG_TRACE,
256                        "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
257                        0, 0, 0 );
258                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
259                 goto return_results;            
260
261         }
262
263         if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
264             
265                 Debug( LDAP_DEBUG_TRACE,
266                        "ldbm_back_modrdn: can't figure out the old_rdn type\n",
267                        0, 0, 0 );
268                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
269                 goto return_results;            
270                 
271         }
272         
273         if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
274
275             /* Not a big deal but we may say something */
276             Debug( LDAP_DEBUG_TRACE,
277                    "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
278                    old_rdn_type, new_rdn_type, 0 );
279             
280         }               
281
282         if ( dn_type( old_rdn ) == DN_X500 ) {
283
284                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
285                        0, 0, 0 );
286
287                 bvals[0] = &bv;         /* Array of bervals */
288                 bvals[1] = NULL;
289
290                 /* Remove old rdn value if required */
291
292                 if (deleteoldrdn) {
293
294                         /* Get value of old rdn */
295         
296                         if ((old_rdn_val = rdn_attr_value( old_rdn ))
297                             == NULL) {
298                             
299                                 Debug( LDAP_DEBUG_TRACE,
300                                        "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
301                                        0, 0, 0 );
302                                 send_ldap_result( conn, op,
303                                                   LDAP_OPERATIONS_ERROR,
304                                                   "", "" );
305                                 goto return_results;            
306
307
308                         }
309
310                         /* Remove old value of rdn as an attribute. */
311                     
312                         bv.bv_val = old_rdn_val;
313                         bv.bv_len = strlen(old_rdn_val);
314
315                         /* No need to normalize old_rdn_type, delete_values()
316                          * does that for us
317                          */
318                         mod.mod_type = old_rdn_type;    
319                         mod.mod_bvalues = bvals;
320                         mod.mod_op = LDAP_MOD_DELETE;   /* XXX:really needed?*/
321
322                         /* Assembly mod structure */
323
324                         if ( delete_values( e, &mod, op->o_ndn )
325                              != LDAP_SUCCESS ) {
326
327                                 /* Could not find old_rdn as an attribute or
328                                  * the old value was not present. Return an 
329                                  * error.
330                                  */
331                                 Debug( LDAP_DEBUG_TRACE,
332                                        "ldbm_back_modrdn: old rdn not found or att type doesn't exist\n",
333                                        0, 0, 0);
334                                 send_ldap_result( conn, op,
335                                                   LDAP_OPERATIONS_ERROR,
336                                                   "", "");
337                                 goto return_results;
338
339                         }
340
341                         Debug( LDAP_DEBUG_TRACE,
342                                "ldbm_back_modrdn: removed old_rdn_val=%s\n",
343                                old_rdn_val, 0, 0 );
344                 
345                 }/* if (deleteoldrdn) */
346
347                 /* Add new attribute value to the entry.
348                  */
349
350                 bv.bv_val = new_rdn_val;
351                 bv.bv_len = strlen(new_rdn_val);
352                 
353                 
354                 Debug( LDAP_DEBUG_TRACE,
355                        "ldbm_back_modrdn: adding new rdn attr val =%s\n",
356                        new_rdn_val, 0, 0 );
357                 
358                 /* No need to normalize new_rdn_type, attr_merge does it */
359
360                 attr_merge( e, new_rdn_type, bvals );
361         
362                 /* Update new_rdn_type if it is an index */
363
364                 bdb2i_index_add_values( be, new_rdn_type, bvals, e->e_id );
365         
366         } else {
367             
368
369                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
370                        0, 0, 0 );
371                 /* XXXV3: not sure of what to do here */
372                 Debug( LDAP_DEBUG_TRACE,
373                        "ldbm_back_modrdn: not fully implemented...\n",
374                        0, 0, 0 );  
375
376         }
377
378         (void) bdb2i_cache_update_entry( &li->li_cache, e );
379
380         /* NOTE: after this you must not free new_dn or new_ndn!
381          * They are used by cache.
382          */
383
384         /* id2entry index */
385         if ( bdb2i_id2entry_add( be, e ) != 0 ) {
386                 entry_free( e );
387                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
388                 goto return_results_after;
389         }
390
391         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
392         rc = 0;
393         goto return_results_after;      
394
395 return_results:
396         if( new_dn != NULL ) free( new_dn );
397         if( new_ndn != NULL ) free( new_ndn );
398 return_results_after:
399         /* NOTE:
400          * new_dn and new_ndn are not deallocated because they are used by
401          * the cache entry.
402          */
403         if( p_dn != NULL ) free( p_dn );
404         if( p_ndn != NULL ) free( p_ndn );
405
406         if( matched != NULL ) free( matched );
407
408         /* LDAP v2 supporting correct attribute handling. */
409         if( new_rdn_type != NULL ) free(new_rdn_type);
410         if( new_rdn_val != NULL ) free(new_rdn_val);
411         if( old_rdn != NULL ) free(old_rdn);
412         if( old_rdn_type != NULL ) free(old_rdn_type);
413         if( old_rdn_val != NULL ) free(old_rdn_val);
414
415         /* LDAP v3 Support */
416         if ( np_dn != NULL ) free( np_dn );
417         if ( np_ndn != NULL ) free( np_ndn );
418
419         if( p != NULL ) {
420                 /* free parent and writer lock */
421                 bdb2i_cache_return_entry_w( &li->li_cache, p );
422
423         }
424
425         /* free entry and writer lock */
426         bdb2i_cache_return_entry_w( &li->li_cache, e );
427         return( rc );
428 }
429
430
431 int
432 bdb2_back_modrdn(
433     BackendDB   *be,
434     Connection  *conn,
435     Operation   *op,
436     char        *dn,
437     char        *newrdn,
438     int         deleteoldrdn,
439     char        *newSuperior
440 )
441 {
442         DB_LOCK         lock;
443         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
444         struct timeval  time1;
445         int             ret;
446
447         bdb2i_start_timing( be->bd_info, &time1 );
448
449         if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
450
451                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
452                 return( -1 );
453
454         }
455
456         ret = bdb2i_back_modrdn_internal( be, conn, op, dn,
457                                         newrdn, deleteoldrdn,
458                                         newSuperior );
459
460         (void) bdb2i_leave_backend_w( lock );
461         bdb2i_stop_timing( be->bd_info, time1, "MODRDN", conn, op );
462
463         return( ret );
464 }
465
466