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