]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
fix substring_comp_candidates logic if intersection of candidates
[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         }/* if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) else */
179
180         new_parent_dn = p_dn;   /* New Parent unless newSuperior given */
181
182         if ( (np_dn = newSuperior) != NULL) {
183
184
185                 Debug( LDAP_DEBUG_TRACE, 
186                        "ldbm_back_modrdn: new parent requested...\n",
187                        0, 0, 0 );
188
189                 np_ndn = dn_normalize_case( ch_strdup( np_dn ) );
190
191                 /* newSuperior == oldParent?, if so ==> ERROR */
192
193                 /* newSuperior == entry being moved?, if so ==> ERROR */
194
195                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
196
197                 if( (np = dn2entry_w( be, np_ndn, &matched )) == NULL) {
198                         Debug( LDAP_DEBUG_TRACE,
199                                "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
200                                np_ndn, 0, 0);
201                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
202                                 NULL, NULL, NULL, NULL );
203                         goto return_results;
204                 }
205
206                 Debug( LDAP_DEBUG_TRACE,
207                        "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
208                        np, np->e_id, 0 );
209             
210                 /* check newSuperior for "children" acl */
211                 if ( !access_allowed( be, conn, op, np, "children", NULL,
212                                       ACL_WRITE ) )
213                 {
214                         Debug( LDAP_DEBUG_TRACE,
215                                "ldbm_back_modrdn: no wr to newSup children\n",
216                                0, 0, 0 );
217                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
218                                 NULL, NULL, NULL, NULL );
219                         goto return_results;
220                 }
221
222                 if ( is_entry_alias( np ) ) {
223                         /* entry is an alias, don't allow bind */
224                         Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
225                             0, 0 );
226
227                         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
228                             NULL, NULL, NULL, NULL );
229
230                         goto return_results;
231                 }
232
233                 if ( is_entry_referral( np ) ) {
234                         /* parent is a referral, don't allow add */
235                         /* parent is an alias, don't allow add */
236                         Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
237                                 0, 0 );
238
239                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
240                             NULL, NULL, NULL, NULL );
241
242                         goto return_results;
243                 }
244
245                 Debug( LDAP_DEBUG_TRACE,
246                        "ldbm_back_modrdn: wr to new parent's children OK\n",
247                        0, 0 , 0 );
248
249                 new_parent_dn = np_dn;
250         }
251         
252         /* Build target dn and make sure target entry doesn't exist already. */
253
254         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
255
256
257         new_ndn = dn_normalize_case( ch_strdup(new_dn) );
258
259         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
260                new_ndn, 0, 0 );
261
262         if (dn2id ( be, new_ndn ) != NOID) {
263                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
264                         NULL, NULL, NULL, NULL );
265                 goto return_results;
266         }
267
268         Debug( LDAP_DEBUG_TRACE,
269                "ldbm_back_modrdn: new ndn=%s does not exist\n",
270                new_ndn, 0, 0 );
271
272         /* check for abandon */
273         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
274         if ( op->o_abandon ) {
275                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
276                 goto return_results;
277         }
278         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
279
280         /* delete old one */
281         if ( dn2id_delete( be, e->e_ndn ) != 0 ) {
282                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
283                         NULL, NULL, NULL, NULL );
284                 goto return_results;
285         }
286
287         (void) cache_delete_entry( &li->li_cache, e );
288         free( e->e_dn );
289         free( e->e_ndn );
290         e->e_dn = new_dn;
291         e->e_ndn = new_ndn;
292
293         /* add new one */
294         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
295                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
296                         NULL, NULL, NULL, NULL );
297                 goto return_results;
298         }
299
300
301         /* Get attribute type and attribute value of our new rdn, we will
302          * need to add that to our new entry
303          */
304
305         if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
306             
307                 Debug( LDAP_DEBUG_TRACE,
308                        "ldbm_back_modrdn: can't figure out type of newrdn\n",
309                        0, 0, 0 );
310                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
311                         NULL, NULL, NULL, NULL );
312                 goto return_results;            
313
314         }
315
316         if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
317             
318                 Debug( LDAP_DEBUG_TRACE,
319                        "ldbm_back_modrdn: can't figure out val of newrdn\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         Debug( LDAP_DEBUG_TRACE,
328                "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
329                new_rdn_val, new_rdn_type, 0 );
330
331         /* Retrieve the old rdn from the entry's dn */
332
333         if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
334
335                 Debug( LDAP_DEBUG_TRACE,
336                        "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
337                        0, 0, 0 );
338                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
339                         NULL, NULL, NULL, NULL );
340                 goto return_results;            
341
342         }
343
344         if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
345             
346                 Debug( LDAP_DEBUG_TRACE,
347                        "ldbm_back_modrdn: can't figure out the old_rdn type\n",
348                        0, 0, 0 );
349                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
350                         NULL, NULL, NULL, NULL );
351                 goto return_results;            
352                 
353         }
354         
355         if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
356
357             /* Not a big deal but we may say something */
358             Debug( LDAP_DEBUG_TRACE,
359                    "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
360                    old_rdn_type, new_rdn_type, 0 );
361             
362         }               
363
364         if ( dn_type( old_rdn ) == DN_X500 ) {
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                 }/* if (deleteoldrdn) */
421
422         
423         } else {
424             
425
426                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
427                        0, 0, 0 );
428                 /* XXXV3: not sure of what to do here */
429                 Debug( LDAP_DEBUG_TRACE,
430                        "ldbm_back_modrdn: not fully implemented...\n",
431                        0, 0, 0 );
432   
433                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
434                         NULL, NULL, NULL, NULL );
435                 goto return_results;
436
437         }
438
439         /* modify memory copy of entry */
440         if ( ldbm_modify_internal( be, conn, op, dn, &mod[0], e )
441              != 0 ) {
442             
443             goto return_results;
444             
445         }
446         
447         (void) cache_update_entry( &li->li_cache, e );
448
449         /* NOTE: after this you must not free new_dn or new_ndn!
450          * They are used by cache.
451          */
452
453         /* id2entry index */
454         if ( id2entry_add( be, e ) != 0 ) {
455                 entry_free( e );
456                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
457                         NULL, NULL, NULL, NULL );
458                 goto return_results_after;
459         }
460
461         send_ldap_result( conn, op, LDAP_SUCCESS,
462                 NULL, NULL, NULL, NULL );
463         rc = 0;
464         goto return_results_after;      
465
466 return_results:
467         if( new_dn != NULL ) free( new_dn );
468         if( new_ndn != NULL ) free( new_ndn );
469 return_results_after:
470         /* NOTE:
471          * new_dn and new_ndn are not deallocated because they are used by
472          * the cache entry.
473          */
474         if( p_dn != NULL ) free( p_dn );
475         if( p_ndn != NULL ) free( p_ndn );
476
477         if( matched != NULL ) free( matched );
478
479         /* LDAP v2 supporting correct attribute handling. */
480         if( new_rdn_type != NULL ) free(new_rdn_type);
481         if( new_rdn_val != NULL ) free(new_rdn_val);
482         if( old_rdn != NULL ) free(old_rdn);
483         if( old_rdn_type != NULL ) free(old_rdn_type);
484         if( old_rdn_val != NULL ) free(old_rdn_val);
485
486
487         /* LDAP v3 Support */
488         if ( np_dn != NULL ) free( np_dn );
489         if ( np_ndn != NULL ) free( np_ndn );
490
491         if( p != NULL ) {
492                 /* free parent and writer lock */
493                 cache_return_entry_w( &li->li_cache, p );
494         }
495
496         if ( rootlock ) {
497                 /* release root writer lock */
498                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
499         }
500
501         /* free entry and writer lock */
502         cache_return_entry_w( &li->li_cache, e );
503         return( rc );
504 }