]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
5aefdbba588f79c8144439df09faee688a82217c
[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 #define CAN_ROLLBACK    -1
55 #define MUST_DESTROY    1
56         int             rc = CAN_ROLLBACK;
57         int             rc_id = 0;
58         ID              id = NOID;
59         const char *text = NULL;
60         char textbuf[SLAP_TEXT_BUFLEN];
61         size_t textlen = sizeof textbuf;
62         /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
63         char            **new_rdn_vals = NULL;  /* Vals of new rdn */
64         char            **new_rdn_types = NULL; /* Types of new rdn */
65         int             a_cnt, d_cnt;
66         char            *old_rdn = NULL;        /* Old rdn's attr type & val */
67         char            **old_rdn_types = NULL; /* Types of old rdn attrs. */
68         char            **old_rdn_vals = NULL;  /* Old rdn attribute values */
69         /* Added to support newSuperior */ 
70         Entry           *np = NULL;     /* newSuperior Entry */
71         char            *np_dn = NULL;  /* newSuperior dn */
72         char            *np_ndn = NULL; /* newSuperior ndn */
73         char            *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
74         /* Used to interface with ldbm_modify_internal() */
75         Modifications   *mod = NULL;            /* Used to delete old/add new rdn */
76         int             manageDSAit = get_manageDSAit( op );
77
78         Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
79                (newSuperior ? newSuperior : "NULL"),
80                0, 0 );
81
82         /* get entry with writer lock */
83         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
84                 char* matched_dn = NULL;
85                 struct berval** refs = NULL;
86
87                 if( matched != NULL ) {
88                         matched_dn = strdup( matched->e_dn );
89                         refs = is_entry_referral( matched )
90                                 ? get_entry_referrals( be, conn, op, matched )
91                                 : NULL;
92                         cache_return_entry_r( &li->li_cache, matched );
93                 } else {
94                         refs = default_referral;
95                 }
96
97                 send_ldap_result( conn, op, LDAP_REFERRAL,
98                         matched_dn, NULL, refs, NULL );
99
100                 if ( matched != NULL ) {
101                         ber_bvecfree( refs );
102                         free( matched_dn );
103                 }
104
105                 return( -1 );
106         }
107
108         if (!manageDSAit && is_entry_referral( e ) ) {
109                 /* parent is a referral, don't allow add */
110                 /* parent is an alias, don't allow add */
111                 struct berval **refs = get_entry_referrals( be,
112                         conn, op, e );
113
114                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
115                     0, 0 );
116
117                 send_ldap_result( conn, op, LDAP_REFERRAL,
118                     e->e_dn, NULL, refs, NULL );
119
120                 ber_bvecfree( refs );
121                 goto return_results;
122         }
123
124         if ( has_children( be, e ) ) {
125                 Debug( LDAP_DEBUG_TRACE, "entry %s referral\n", e->e_dn,
126                     0, 0 );
127
128                 send_ldap_result( conn, op, LDAP_NOT_ALLOWED_ON_NONLEAF,
129                     NULL, "subtree rename not supported", NULL, NULL );
130                 goto return_results;
131         }
132
133         if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL && p_ndn[0] != '\0' ) {
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
142                         send_ldap_result( conn, op, LDAP_OTHER,
143                                 NULL, "parent entry does not exist", NULL, NULL );
144
145                         goto return_results;
146                 }
147
148                 /* check parent for "children" acl */
149                 if ( ! access_allowed( be, conn, op, p,
150                         children, NULL, ACL_WRITE ) )
151                 {
152                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
153                                 0, 0 );
154
155                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
156                                 NULL, NULL, NULL, NULL );
157                         goto return_results;
158                 }
159
160                 Debug( LDAP_DEBUG_TRACE,
161                        "ldbm_back_modrdn: wr to children of entry %s OK\n",
162                        p_ndn, 0, 0 );
163
164                 p_dn = dn_parent( be, e->e_dn );
165
166                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
167                        p_dn, 0, 0 );
168
169         } else {
170                 /* no parent, modrdn entry directly under root */
171                 if( ! be_isroot( be, op->o_ndn ) && ! be_issuffix( be, "" ) ) {
172                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
173                                 0, 0, 0);
174
175                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
176                                 NULL, NULL, NULL, NULL );
177                         goto return_results;
178                 }
179
180                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
181                 rootlock = 1;
182                 
183                 Debug( LDAP_DEBUG_TRACE,
184                        "ldbm_back_modrdn: no parent, locked root\n",
185                        0, 0, 0 );
186         }
187
188         new_parent_dn = p_dn;   /* New Parent unless newSuperior given */
189
190         if ( newSuperior != NULL ) {
191                 Debug( LDAP_DEBUG_TRACE, 
192                         "ldbm_back_modrdn: new parent \"%s\" requested...\n",
193                         newSuperior, 0, 0 );
194
195                 np_dn = ch_strdup( newSuperior );
196                 np_ndn = ch_strdup( np_dn );
197                 (void) dn_normalize( np_ndn );
198
199                 /* newSuperior == oldParent? */
200                 if ( strcmp( p_ndn, np_ndn ) == 0 ) {
201                         Debug( LDAP_DEBUG_TRACE, 
202                                "ldbm_back_modrdn: new parent \"%s\" seems to be the same as old parent \"%s\"...\n",
203                                newSuperior, p_dn, 0 );
204
205                         newSuperior = NULL; /* ignore newSuperior */
206                 }
207         }
208
209         if ( newSuperior != NULL ) {
210                 /* newSuperior == entry being moved?, if so ==> ERROR */
211                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
212
213                 if( (np = dn2entry_w( be, np_ndn, NULL )) == NULL) {
214                         Debug( LDAP_DEBUG_TRACE,
215                                "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
216                                np_ndn, 0, 0);
217
218                         send_ldap_result( conn, op, LDAP_OTHER,
219                                 NULL, "newSuperior not found", NULL, NULL );
220                         goto return_results;
221                 }
222
223                 Debug( LDAP_DEBUG_TRACE,
224                        "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
225                        np, np->e_id, 0 );
226
227                 /* check newSuperior for "children" acl */
228                 if ( !access_allowed( be, conn, op, np, children, NULL,
229                                       ACL_WRITE ) )
230                 {
231                         Debug( LDAP_DEBUG_TRACE,
232                                "ldbm_back_modrdn: no wr to newSup children\n",
233                                0, 0, 0 );
234
235                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
236                                 NULL, NULL, NULL, NULL );
237                         goto return_results;
238                 }
239
240                 if ( is_entry_alias( np ) ) {
241                         /* entry is an alias, don't allow bind */
242                         Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
243                             0, 0 );
244
245
246                         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
247                             NULL, "newSuperior is an alias", NULL, NULL );
248
249                         goto return_results;
250                 }
251
252                 if ( is_entry_referral( np ) ) {
253                         /* parent is a referral, don't allow add */
254                         /* parent is an alias, don't allow add */
255                         Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
256                                 0, 0 );
257
258                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
259                             NULL, "newSuperior is a referral", NULL, NULL );
260
261                         goto return_results;
262                 }
263
264                 Debug( LDAP_DEBUG_TRACE,
265                        "ldbm_back_modrdn: wr to new parent's children OK\n",
266                        0, 0 , 0 );
267
268                 new_parent_dn = np_dn;
269         }
270         
271         /* Build target dn and make sure target entry doesn't exist already. */
272         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
273
274         new_ndn = ch_strdup(new_dn);
275         (void) dn_normalize( new_ndn );
276
277         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
278                new_ndn, 0, 0 );
279
280         /* check for abandon */
281         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
282         if ( op->o_abandon ) {
283                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
284                 goto return_results;
285         }
286
287         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
288         if ( ( rc_id = dn2id ( be, new_ndn, &id ) ) || id != NOID ) {
289                 /* if (rc_id) something bad happened to ldbm cache */
290                 send_ldap_result( conn, op, 
291                         rc_id ? LDAP_OPERATIONS_ERROR : LDAP_ALREADY_EXISTS,
292                         NULL, NULL, NULL, NULL );
293                 goto return_results;
294         }
295
296         Debug( LDAP_DEBUG_TRACE,
297                "ldbm_back_modrdn: new ndn=%s does not exist\n",
298                new_ndn, 0, 0 );
299
300
301         /* Get attribute types and values of our new rdn, we will
302          * need to add that to our new entry
303          */
304         if ( rdn_attrs( newrdn, &new_rdn_types, &new_rdn_vals ) ) {
305                 Debug( LDAP_DEBUG_TRACE,
306                        "ldbm_back_modrdn: can't figure out type(s)/value(s) of newrdn\n",
307                        0, 0, 0 );
308
309                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
310                         NULL, "unable to parse type(s)/value(s) used in RDN", NULL, NULL );
311                 goto return_results;            
312         }
313
314         Debug( LDAP_DEBUG_TRACE,
315                "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
316                new_rdn_vals[0], new_rdn_types[0], 0 );
317
318         /* Retrieve the old rdn from the entry's dn */
319         if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
320                 Debug( LDAP_DEBUG_TRACE,
321                        "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
322                        0, 0, 0 );
323
324                 send_ldap_result( conn, op, LDAP_OTHER,
325                         NULL, "could not parse old DN", NULL, NULL );
326                 goto return_results;            
327         }
328
329         if ( rdn_attrs( old_rdn, &old_rdn_types, &old_rdn_vals ) ) {
330                 Debug( LDAP_DEBUG_TRACE,
331                        "ldbm_back_modrdn: can't figure out the old_rdn type(s)/value(s)\n",
332                        0, 0, 0 );
333
334                 send_ldap_result( conn, op, LDAP_OTHER,
335                         NULL, "unable to parse type(s)/value(s) used in RDN from old DN", NULL, NULL );
336                 goto return_results;            
337         }
338         
339         if ( newSuperior == NULL
340                 && charray_strcasecmp( (const char **)old_rdn_types, (const char **)new_rdn_types ) != 0 )
341         {
342             /* Not a big deal but we may say something */
343             Debug( LDAP_DEBUG_TRACE,
344                    "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
345                    old_rdn_types[0], new_rdn_types[0], 0 );
346         }               
347
348         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
349                0, 0, 0 );
350
351         mod = NULL;
352         for ( a_cnt = 0; new_rdn_types[a_cnt]; a_cnt++ ) {
353                 int rc;
354                 Modifications *mod_tmp;
355                 struct berval val;
356
357                 mod_tmp = (Modifications *)ch_malloc( sizeof( Modifications ) );
358
359                 mod_tmp->sml_desc = NULL;
360                 rc = slap_str2ad( new_rdn_types[a_cnt], 
361                                 &mod_tmp->sml_desc, &text );
362
363                 if ( rc != LDAP_SUCCESS ) {
364                         Debug( LDAP_DEBUG_TRACE,
365                                 "ldbm_back_modrdn: %s: %s (new)\n",
366                                 text, new_rdn_types[a_cnt], 0 );
367
368                         send_ldap_result( conn, op, rc,
369                                 NULL, text, NULL, NULL );
370
371                         goto return_results;            
372                 }
373
374                 val.bv_val = new_rdn_vals[a_cnt];
375                 val.bv_len = strlen( val.bv_val );
376                 if ( ! access_allowed( be, conn, op, e, 
377                                 mod_tmp->sml_desc, &val, ACL_WRITE ) ) {
378                         Debug( LDAP_DEBUG_TRACE,
379                                 "ldbm_back_modrdn: access not allowed "
380                                 "to attr \"%s\"\n%s%s",
381                                 new_rdn_types[a_cnt], "", "" );
382                         send_ldap_result( conn, op, 
383                                 LDAP_INSUFFICIENT_ACCESS,
384                                 NULL, NULL, NULL, NULL );
385
386                         goto return_results;
387                 }
388
389                 mod_tmp->sml_bvalues = (struct berval **)ch_malloc( 2 * sizeof(struct berval *) );
390                 mod_tmp->sml_bvalues[0] = ber_bvstrdup( new_rdn_vals[a_cnt] );
391                 mod_tmp->sml_bvalues[1] = NULL;
392                 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
393                 mod_tmp->sml_next = mod;
394                 mod = mod_tmp;
395         }
396
397         /* Remove old rdn value if required */
398         if ( deleteoldrdn ) {
399                 /* Get value of old rdn */
400                 if ( old_rdn_vals == NULL ) {
401                         Debug( LDAP_DEBUG_TRACE,
402                                "ldbm_back_modrdn: can't figure out oldRDN value(s) from old RDN\n",
403                                0, 0, 0 );
404
405                         send_ldap_result( conn, op, LDAP_OTHER,
406                                 NULL, "could not parse value(s) from old RDN", NULL, NULL );
407                         goto return_results;            
408                 }
409
410                 for ( d_cnt = 0; old_rdn_types[d_cnt]; d_cnt++ ) {    
411                         int rc;
412                         Modifications *mod_tmp;
413                         struct berval val;
414
415                         mod_tmp = (Modifications *)ch_malloc( sizeof( Modifications ) );
416
417                         mod_tmp->sml_desc = NULL;
418                         rc = slap_str2ad( old_rdn_types[d_cnt], 
419                                         &mod_tmp->sml_desc, &text );
420
421                         if ( rc != LDAP_SUCCESS ) {
422                                 Debug( LDAP_DEBUG_TRACE,
423                                         "ldbm_back_modrdn: %s: %s (old)\n",
424                                         text, old_rdn_types[d_cnt], 0 );
425
426                                 send_ldap_result( conn, op, rc,
427                                         NULL, text, NULL, NULL );
428
429                                 goto return_results;
430                         }
431
432                         val.bv_val = old_rdn_vals[d_cnt];
433                         val.bv_len = strlen( val.bv_val );
434                         if ( ! access_allowed( be, conn, op, e, 
435                                         mod_tmp->sml_desc, &val, ACL_WRITE ) ) {
436                                 Debug( LDAP_DEBUG_TRACE,
437                                         "ldbm_back_modrdn: access not allowed "
438                                         "to attr \"%s\"\n%s%s",
439                                         old_rdn_types[d_cnt], "", "" );
440                                 send_ldap_result( conn, op, 
441                                         LDAP_INSUFFICIENT_ACCESS,
442                                         NULL, NULL, NULL, NULL );
443
444                                 goto return_results;
445                         }
446
447                         /* Remove old value of rdn as an attribute. */
448                         mod_tmp->sml_bvalues = (struct berval **)ch_malloc( 2 * sizeof(struct berval *) );
449                         mod_tmp->sml_bvalues[0] = ber_bvstrdup( old_rdn_vals[d_cnt] );
450                         mod_tmp->sml_bvalues[1] = NULL;
451                         mod_tmp->sml_op = LDAP_MOD_DELETE;
452                         mod_tmp->sml_next = mod;
453                         mod = mod_tmp;
454
455                         Debug( LDAP_DEBUG_TRACE,
456                                "ldbm_back_modrdn: removing old_rdn_val=%s\n",
457                                old_rdn_vals[0], 0, 0 );
458                 }
459         }
460
461         
462         /* check for abandon */
463         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
464         if ( op->o_abandon ) {
465                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
466                 goto return_results;
467         }
468         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
469
470         /* delete old one */
471         if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
472                 send_ldap_result( conn, op, LDAP_OTHER,
473                         NULL, "DN index delete fail", NULL, NULL );
474                 goto return_results;
475         }
476
477         (void) cache_delete_entry( &li->li_cache, e );
478         rc = MUST_DESTROY;
479
480         /* XXX: there is no going back! */
481
482         free( e->e_dn );
483         free( e->e_ndn );
484         e->e_dn = new_dn;
485         e->e_ndn = new_ndn;
486         new_dn = NULL;
487         new_ndn = NULL;
488
489         /* add new one */
490         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
491                 send_ldap_result( conn, op, LDAP_OTHER,
492                         NULL, "DN index add failed", NULL, NULL );
493                 goto return_results;
494         }
495
496         /* modify memory copy of entry */
497         rc = ldbm_modify_internal( be, conn, op, dn, &mod[0], e,
498                 &text, textbuf, textlen );
499
500         if( rc != LDAP_SUCCESS ) {
501                 if( rc != SLAPD_ABANDON ) {
502                         send_ldap_result( conn, op, rc,
503                                 NULL, text, NULL, NULL );
504                 }
505
506                 /* here we may try to delete the newly added dn */
507                 if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
508                         /* we already are in trouble ... */
509                         ;
510                 }
511             
512                 goto return_results;
513         }
514         
515         (void) cache_update_entry( &li->li_cache, e );
516
517         /* NOTE: after this you must not free new_dn or new_ndn!
518          * They are used by cache.
519          */
520
521         /* id2entry index */
522         if ( id2entry_add( be, e ) != 0 ) {
523                 send_ldap_result( conn, op, LDAP_OTHER,
524                         NULL, "entry update failed", NULL, NULL );
525                 goto return_results;
526         }
527
528         send_ldap_result( conn, op, LDAP_SUCCESS,
529                 NULL, NULL, NULL, NULL );
530         rc = 0;
531         cache_entry_commit( e );
532
533 return_results:
534         if( new_dn != NULL ) free( new_dn );
535         if( new_ndn != NULL ) free( new_ndn );
536
537         if( p_dn != NULL ) free( p_dn );
538         if( p_ndn != NULL ) free( p_ndn );
539
540         /* LDAP v2 supporting correct attribute handling. */
541         if( new_rdn_types != NULL ) charray_free( new_rdn_types );
542         if( new_rdn_vals != NULL ) charray_free( new_rdn_vals );
543         if( old_rdn != NULL ) free(old_rdn);
544         if( old_rdn_types != NULL ) charray_free( old_rdn_types );
545         if( old_rdn_vals != NULL ) charray_free( old_rdn_vals );
546
547         if ( mod != NULL ) {
548                 slap_mods_free( mod );
549         }
550
551         /* LDAP v3 Support */
552         if ( np_dn != NULL ) free( np_dn );
553         if ( np_ndn != NULL ) free( np_ndn );
554
555         if( np != NULL ) {
556                 /* free new parent and writer lock */
557                 cache_return_entry_w( &li->li_cache, np );
558         }
559
560         if( p != NULL ) {
561                 /* free parent and writer lock */
562                 cache_return_entry_w( &li->li_cache, p );
563         }
564
565         if ( rootlock ) {
566                 /* release root writer lock */
567                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
568         }
569
570         /* free entry and writer lock */
571         cache_return_entry_w( &li->li_cache, e );
572         if ( rc == MUST_DESTROY ) {
573                 /* if rc == MUST_DESTROY the entry is uncached 
574                  * and its private data is destroyed; 
575                  * the entry must be freed */
576                 entry_free( e );
577         }
578         return( rc );
579 }