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