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