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