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