]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
a2262c63b9fee0b09088669fe62c2aa19186d704
[openldap] / servers / slapd / back-ldbm / modrdn.c
1 /* modrdn.c - ldbm backend modrdn routine */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 /*
8  * LDAP v3 newSuperior support. Add new rdn as an attribute.
9  * (Full support for v2 also used software/ideas contributed
10  * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
11  * submission!.)
12  *
13  * Copyright 1999, Juan C. Gomez, All rights reserved.
14  * This software is not subject to any license of Silicon Graphics 
15  * Inc. or Purdue University.
16  *
17  * Redistribution and use in source and binary forms are permitted
18  * without restriction or fee of any kind as long as this notice
19  * is preserved.
20  *
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29
30 #include "slap.h"
31 #include "back-ldbm.h"
32 #include "proto-back-ldbm.h"
33
34 int
35 ldbm_back_modrdn(
36     Backend     *be,
37     Connection  *conn,
38     Operation   *op,
39     char        *dn,
40     char        *newrdn,
41     int         deleteoldrdn,
42     char        *newSuperior
43 )
44 {
45         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
46         char            *p_dn = NULL, *p_ndn = NULL;
47         char            *new_dn = NULL, *new_ndn = NULL;
48         Entry           *e, *p = NULL;
49         Entry           *matched = NULL;
50         int                     rootlock = 0;
51         int                     rc = -1;
52         /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
53         char            *new_rdn_val = NULL;    /* Val of new rdn */
54         char            *new_rdn_type = NULL;   /* Type of new rdn */
55         char            *old_rdn = NULL;        /* Old rdn's attr type & val */
56         char            *old_rdn_type = NULL;   /* Type of old rdn attr. */
57         char            *old_rdn_val = NULL;    /* Old rdn attribute value */
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         /* Used to interface with ldbm_modify_internal() */
64         struct berval   add_bv;                 /* Stores new rdn att */
65         struct berval   *add_bvals[2];          /* Stores new rdn att */
66         struct berval   del_bv;                 /* Stores old rdn att */
67         struct berval   *del_bvals[2];          /* Stores old rdn att */
68         LDAPModList     mod[2];                 /* Used to delete old rdn */
69         int             manageDSAit = get_manageDSAit( op );
70
71         Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
72                (newSuperior ? newSuperior : "NULL"),
73                0, 0 );
74
75         /* get entry with writer lock */
76         if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
77                 char* matched_dn = NULL;
78                 struct berval** refs = NULL;
79
80                 if( matched != NULL ) {
81                         matched_dn = strdup( matched->e_dn );
82                         refs = is_entry_referral( matched )
83                                 ? get_entry_referrals( be, conn, op, matched )
84                                 : NULL;
85                         cache_return_entry_r( &li->li_cache, matched );
86                 } else {
87                         refs = default_referral;
88                 }
89
90                 send_ldap_result( conn, op, LDAP_REFERRAL,
91                         matched_dn, NULL, refs, NULL );
92
93                 if ( matched != NULL ) {
94                         ber_bvecfree( refs );
95                         free( matched_dn );
96                 }
97
98                 return( -1 );
99         }
100
101 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
102         if ( ! access_allowed( be, conn, op, e,
103                 "entry", NULL, ACL_WRITE ) )
104         {
105                 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
106                         0, 0 );
107                 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
108                         NULL, NULL, NULL, NULL );
109                 goto return_results;
110         }
111 #endif
112
113         if (!manageDSAit && is_entry_referral( e ) ) {
114                 /* parent is a referral, don't allow add */
115                 /* parent is an alias, don't allow add */
116                 struct berval **refs = get_entry_referrals( be,
117                         conn, op, e );
118
119                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
120                     0, 0 );
121
122                 send_ldap_result( conn, op, LDAP_REFERRAL,
123                     e->e_dn, NULL, refs, NULL );
124
125                 ber_bvecfree( refs );
126
127                 goto return_results;
128         }
129
130         if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
131
132                 /* Make sure parent entry exist and we can write its 
133                  * children.
134                  */
135
136                 if( (p = dn2entry_w( be, p_ndn, &matched )) == NULL) {
137                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
138                                 0, 0, 0);
139                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
140                                 NULL, NULL, NULL, NULL );
141                         goto return_results;
142                 }
143
144                 /* check parent for "children" acl */
145                 if ( ! access_allowed( be, conn, op, p,
146                         "children", NULL, ACL_WRITE ) )
147                 {
148                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
149                                 0, 0 );
150                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
151                                 NULL, NULL, NULL, NULL );
152                         goto return_results;
153                 }
154
155                 Debug( LDAP_DEBUG_TRACE,
156                        "ldbm_back_modrdn: wr to children of entry %s OK\n",
157                        p_ndn, 0, 0 );
158                 
159                 p_dn = dn_parent( be, e->e_dn );
160         
161
162                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
163                        p_dn, 0, 0 );
164
165         } else {
166                 /* no parent, modrdn entry directly under root */
167                 if( ! be_isroot( be, op->o_ndn ) ) {
168                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
169                                 0, 0, 0);
170                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
171                                 NULL, NULL, NULL, NULL );
172                         goto return_results;
173                 }
174
175                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
176                 rootlock = 1;
177                 
178                 Debug( LDAP_DEBUG_TRACE,
179                        "ldbm_back_modrdn: no parent, locked root\n",
180                        0, 0, 0 );
181
182         }
183
184         new_parent_dn = p_dn;   /* New Parent unless newSuperior given */
185
186         if ( (np_dn = newSuperior) != NULL) {
187                 Debug( LDAP_DEBUG_TRACE, 
188                        "ldbm_back_modrdn: new parent requested...\n",
189                        0, 0, 0 );
190
191                 np_ndn = ch_strdup( np_dn );
192                 (void) dn_normalize_case( np_ndn );
193
194                 /* newSuperior == oldParent?, if so ==> ERROR */
195                 /* newSuperior == entry being moved?, if so ==> ERROR */
196                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
197
198                 if( (np = dn2entry_w( be, np_ndn, &matched )) == NULL) {
199                         Debug( LDAP_DEBUG_TRACE,
200                                "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
201                                np_ndn, 0, 0);
202                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
203                                 NULL, NULL, NULL, NULL );
204                         goto return_results;
205                 }
206
207                 Debug( LDAP_DEBUG_TRACE,
208                        "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
209                        np, np->e_id, 0 );
210             
211                 /* check newSuperior for "children" acl */
212                 if ( !access_allowed( be, conn, op, np, "children", NULL,
213                                       ACL_WRITE ) )
214                 {
215                         Debug( LDAP_DEBUG_TRACE,
216                                "ldbm_back_modrdn: no wr to newSup children\n",
217                                0, 0, 0 );
218                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
219                                 NULL, NULL, NULL, NULL );
220                         goto return_results;
221                 }
222
223                 if ( is_entry_alias( np ) ) {
224                         /* entry is an alias, don't allow bind */
225                         Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
226                             0, 0 );
227
228                         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
229                             NULL, NULL, NULL, NULL );
230
231                         goto return_results;
232                 }
233
234                 if ( is_entry_referral( np ) ) {
235                         /* parent is a referral, don't allow add */
236                         /* parent is an alias, don't allow add */
237                         Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
238                                 0, 0 );
239
240                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
241                             NULL, NULL, NULL, NULL );
242
243                         goto return_results;
244                 }
245
246                 Debug( LDAP_DEBUG_TRACE,
247                        "ldbm_back_modrdn: wr to new parent's children OK\n",
248                        0, 0 , 0 );
249
250                 new_parent_dn = np_dn;
251         }
252         
253         /* Build target dn and make sure target entry doesn't exist already. */
254
255         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
256
257
258         new_ndn = ch_strdup(new_dn);
259         (void) dn_normalize_case( new_ndn );
260
261         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
262                new_ndn, 0, 0 );
263
264         /* check for abandon */
265         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
266         if ( op->o_abandon ) {
267                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
268                 goto return_results;
269         }
270
271         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
272         if (dn2id ( be, new_ndn ) != NOID) {
273                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
274                         NULL, NULL, NULL, NULL );
275                 goto return_results;
276         }
277
278         Debug( LDAP_DEBUG_TRACE,
279                "ldbm_back_modrdn: new ndn=%s does not exist\n",
280                new_ndn, 0, 0 );
281
282         /* Get attribute type and attribute value of our new rdn, we will
283          * need to add that to our new entry
284          */
285
286         if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
287             
288                 Debug( LDAP_DEBUG_TRACE,
289                        "ldbm_back_modrdn: can't figure out type of newrdn\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 ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
298             
299                 Debug( LDAP_DEBUG_TRACE,
300                        "ldbm_back_modrdn: can't figure out val of newrdn\n",
301                        0, 0, 0 );
302                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
303                         NULL, NULL, NULL, NULL );
304                 goto return_results;            
305
306         }
307
308         Debug( LDAP_DEBUG_TRACE,
309                "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
310                new_rdn_val, new_rdn_type, 0 );
311
312         /* Retrieve the old rdn from the entry's dn */
313
314         if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
315
316                 Debug( LDAP_DEBUG_TRACE,
317                        "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
318                        0, 0, 0 );
319                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
320                         NULL, NULL, NULL, NULL );
321                 goto return_results;            
322
323         }
324
325         if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
326             
327                 Debug( LDAP_DEBUG_TRACE,
328                        "ldbm_back_modrdn: can't figure out the old_rdn type\n",
329                        0, 0, 0 );
330                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
331                         NULL, NULL, NULL, NULL );
332                 goto return_results;            
333                 
334         }
335         
336         if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
337
338             /* Not a big deal but we may say something */
339             Debug( LDAP_DEBUG_TRACE,
340                    "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
341                    old_rdn_type, new_rdn_type, 0 );
342             
343         }               
344
345 #ifdef DNS_DN
346         if ( dn_type( old_rdn ) == DN_X500 ) {
347 #endif
348
349                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
350                        0, 0, 0 );
351                 
352                 /* Add new attribute value to the entry.
353                  */
354
355                 add_bvals[0] = &add_bv;         /* Array of bervals */
356                 add_bvals[1] = NULL;
357
358                 add_bv.bv_val = new_rdn_val;
359                 add_bv.bv_len = strlen(new_rdn_val);
360                 
361                 mod[0].ml_type = new_rdn_type;  
362                 mod[0].ml_bvalues = add_bvals;
363                 mod[0].ml_op = LDAP_MOD_SOFTADD;
364                 mod[0].ml_next = NULL;
365
366                 /* Remove old rdn value if required */
367
368                 if (deleteoldrdn) {
369                         /* Get value of old rdn */
370         
371                         if ((old_rdn_val = rdn_attr_value( old_rdn ))
372                             == NULL) {
373                             
374                                 Debug( LDAP_DEBUG_TRACE,
375                                        "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
376                                        0, 0, 0 );
377                                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
378                                         NULL, NULL, NULL, NULL );
379                                 goto return_results;            
380                         }
381
382                         del_bvals[0] = &del_bv;         /* Array of bervals */
383                         del_bvals[1] = NULL;
384
385                         /* Remove old value of rdn as an attribute. */
386                     
387                         del_bv.bv_val = old_rdn_val;
388                         del_bv.bv_len = strlen(old_rdn_val);
389
390                         /* No need to normalize old_rdn_type, delete_values()
391                          * does that for us
392                          */
393                         mod[0].ml_next = &mod[1];
394                         mod[1].ml_type = old_rdn_type;  
395                         mod[1].ml_bvalues = del_bvals;
396                         mod[1].ml_op = LDAP_MOD_DELETE;
397                         mod[1].ml_next = NULL;
398
399                         Debug( LDAP_DEBUG_TRACE,
400                                "ldbm_back_modrdn: removing old_rdn_val=%s\n",
401                                old_rdn_val, 0, 0 );
402                 }
403         
404 #ifdef DNS_DN
405         } else {
406                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
407                        0, 0, 0 );
408                 /* XXXV3: not sure of what to do here */
409                 Debug( LDAP_DEBUG_TRACE,
410                        "ldbm_back_modrdn: not fully implemented...\n",
411                        0, 0, 0 );
412   
413                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
414                         NULL, NULL, NULL, NULL );
415                 goto return_results;
416
417         }
418 #endif
419
420         /* check for abandon */
421         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
422         if ( op->o_abandon ) {
423                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
424                 goto return_results;
425         }
426         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
427
428         /* delete old one */
429         if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
430                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
431                         NULL, NULL, NULL, NULL );
432                 goto return_results;
433         }
434
435         (void) cache_delete_entry( &li->li_cache, e );
436
437         /* XXX: there is no going back! */
438
439         free( e->e_dn );
440         free( e->e_ndn );
441         e->e_dn = new_dn;
442         e->e_ndn = new_ndn;
443         new_dn = NULL;
444         new_ndn = NULL;
445
446         /* add new one */
447         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
448                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
449                         NULL, NULL, NULL, NULL );
450                 goto return_results;
451         }
452
453         /* modify memory copy of entry */
454         if ( ldbm_modify_internal( be, conn, op, dn, &mod[0], e )
455              != 0 ) {
456             
457             goto return_results;
458         }
459         
460         (void) cache_update_entry( &li->li_cache, e );
461
462         /* NOTE: after this you must not free new_dn or new_ndn!
463          * They are used by cache.
464          */
465
466         /* id2entry index */
467         if ( id2entry_add( be, e ) != 0 ) {
468                 entry_free( e );
469                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
470                         NULL, NULL, NULL, NULL );
471                 goto return_results;
472         }
473
474         send_ldap_result( conn, op, LDAP_SUCCESS,
475                 NULL, NULL, NULL, NULL );
476         rc = 0;
477
478 return_results:
479         if( new_dn != NULL ) free( new_dn );
480         if( new_ndn != NULL ) free( new_ndn );
481
482         if( p_dn != NULL ) free( p_dn );
483         if( p_ndn != NULL ) free( p_ndn );
484
485         if( matched != NULL ) free( matched );
486
487         /* LDAP v2 supporting correct attribute handling. */
488         if( new_rdn_type != NULL ) free(new_rdn_type);
489         if( new_rdn_val != NULL ) free(new_rdn_val);
490         if( old_rdn != NULL ) free(old_rdn);
491         if( old_rdn_type != NULL ) free(old_rdn_type);
492         if( old_rdn_val != NULL ) free(old_rdn_val);
493
494
495         /* LDAP v3 Support */
496         if ( np_dn != NULL ) free( np_dn );
497         if ( np_ndn != NULL ) free( np_ndn );
498
499         if( np != NULL ) {
500                 /* free new parent and writer lock */
501                 cache_return_entry_w( &li->li_cache, np );
502         }
503
504         if( p != NULL ) {
505                 /* free parent and writer lock */
506                 cache_return_entry_w( &li->li_cache, p );
507         }
508
509         if ( rootlock ) {
510                 /* release root writer lock */
511                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
512         }
513
514         /* free entry and writer lock */
515         cache_return_entry_w( &li->li_cache, e );
516         return( rc );
517 }