]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modrdn.c
a3216c6dd3c9cc38dc276de5df69e5e5da387fa3
[openldap] / servers / slapd / back-bdb / modrdn.c
1 /* modrdn.c - bdb 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 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "external.h"
15
16 int
17 bdb_modrdn(
18         Backend *be,
19         Connection      *conn,
20         Operation       *op,
21         const char      *dn,
22         const char      *ndn,
23         const char      *newrdn,
24         int             deleteoldrdn,
25         const char      *newSuperior
26 )
27 {
28         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
29         AttributeDescription *children = slap_schema.si_ad_children;
30         char            *p_dn = NULL, *p_ndn = NULL;
31         char            *new_dn = NULL, *new_ndn = NULL;
32         Entry           *e, *p = NULL;
33         Entry           *matched;
34         int                     rc;
35         const char *text;
36         char textbuf[SLAP_TEXT_BUFLEN];
37         size_t textlen = sizeof textbuf;
38         DB_TXN *        ltid;
39         struct bdb_op_info opinfo;
40
41         ID                      id;
42         char            **new_rdn_vals = NULL;  /* Vals of new rdn */
43         char            **new_rdn_types = NULL; /* Types of new rdn */
44         int             a_cnt, d_cnt;
45         char            *old_rdn = NULL;        /* Old rdn's attr type & val */
46         char            **old_rdn_types = NULL; /* Types of old rdn attr. */
47         char            **old_rdn_vals = NULL;  /* Old rdn attribute values */
48
49         Entry           *np = NULL;                             /* newSuperior Entry */
50         char            *np_dn = NULL;                  /* newSuperior dn */
51         char            *np_ndn = NULL;                 /* newSuperior ndn */
52         char            *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
53
54         /* Used to interface with bdb_modify_internal() */
55         Modifications   *mod = NULL;            /* Used to delete old rdn */
56
57         int             manageDSAit = get_manageDSAit( op );
58
59         Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
60                 dn, newrdn, (newSuperior ? newSuperior : "NULL") );
61
62         if( newSuperior != NULL ) {
63                 rc = LDAP_UNWILLING_TO_PERFORM;
64                 text = "newSuperior not implemented (yet)";
65                 goto return_results;
66         }
67
68         if (0) {
69 retry:  /* transaction retry */
70                 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
71                 rc = txn_abort( ltid );
72                 ltid = NULL;
73                 op->o_private = NULL;
74                 if( rc != 0 ) {
75                         rc = LDAP_OTHER;
76                         text = "internal error";
77                         goto return_results;
78                 }
79         }
80
81
82         /* begin transaction */
83         rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 0 );
84         text = NULL;
85         if( rc != 0 ) {
86                 Debug( LDAP_DEBUG_TRACE,
87                         "bdb_delete: txn_begin failed: %s (%d)\n",
88                         db_strerror(rc), rc, 0 );
89                 rc = LDAP_OTHER;
90                 text = "internal error";
91                 goto return_results;
92         }
93
94         opinfo.boi_bdb = be;
95         opinfo.boi_txn = ltid;
96         opinfo.boi_err = 0;
97         op->o_private = &opinfo;
98
99         /* get entry */
100         rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
101
102         switch( rc ) {
103         case 0:
104         case DB_NOTFOUND:
105                 break;
106         case DB_LOCK_DEADLOCK:
107         case DB_LOCK_NOTGRANTED:
108                 goto retry;
109         default:
110                 rc = LDAP_OTHER;
111                 text = "internal error";
112                 goto return_results;
113         }
114
115         if ( e == NULL ) {
116                 char* matched_dn = NULL;
117                 struct berval** refs = NULL;
118
119                 if( matched != NULL ) {
120                         matched_dn = strdup( matched->e_dn );
121                         refs = is_entry_referral( matched )
122                                 ? get_entry_referrals( be, conn, op, matched )
123                                 : NULL;
124                         bdb_entry_return( be, matched );
125                         matched = NULL;
126
127                 } else {
128                         refs = default_referral;
129                 }
130
131                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
132                         matched_dn, NULL, refs, NULL );
133
134                 if ( matched != NULL ) {
135                         ber_bvecfree( refs );
136                         free( matched_dn );
137                 }
138
139                 goto done;
140         }
141
142         if (!manageDSAit && is_entry_referral( e ) ) {
143                 /* parent is a referral, don't allow add */
144                 /* parent is an alias, don't allow add */
145                 struct berval **refs = get_entry_referrals( be,
146                         conn, op, e );
147
148                 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
149                         0, 0, 0 );
150
151                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
152                         e->e_dn, NULL, refs, NULL );
153
154                 ber_bvecfree( refs );
155                 goto done;
156         }
157
158         p_ndn = dn_parent( be, e->e_ndn );
159         if ( p_ndn != NULL ) {
160                 /* Make sure parent entry exist and we can write its 
161                  * children.
162                  */
163
164                 rc = bdb_dn2entry( be, ltid, p_ndn, &p, NULL, 0 );
165
166                 switch( rc ) {
167                 case 0:
168                 case DB_NOTFOUND:
169                         break;
170                 case DB_LOCK_DEADLOCK:
171                 case DB_LOCK_NOTGRANTED:
172                         goto retry;
173                 default:
174                         rc = LDAP_OTHER;
175                         text = "internal error";
176                         goto return_results;
177                 }
178
179                 if( p == NULL) {
180                         Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
181                                 0, 0, 0);
182                         rc = LDAP_OTHER;
183                         goto return_results;
184                 }
185
186                 /* check parent for "children" acl */
187                 if ( ! access_allowed( be, conn, op, p,
188                         children, NULL, ACL_WRITE ) )
189                 {
190                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
191                                 0, 0 );
192                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
193                                 NULL, NULL, NULL, NULL );
194                         goto return_results;
195                 }
196
197                 Debug( LDAP_DEBUG_TRACE,
198                         "bdb_modrdn: wr to children of entry %s OK\n",
199                         p_ndn, 0, 0 );
200                 
201                 p_dn = dn_parent( be, e->e_dn );
202
203                 Debug( LDAP_DEBUG_TRACE,
204                         "bdb_modrdn: parent dn=%s\n",
205                         p_dn, 0, 0 );
206
207         } else {
208                 /* no parent, modrdn entry directly under root */
209                 if( ! be_isroot( be, op->o_ndn ) ) {
210                         Debug( LDAP_DEBUG_TRACE,
211                                 "bdb_modrdn: no parent & not root\n",
212                                 0, 0, 0);
213                         rc = LDAP_INSUFFICIENT_ACCESS;
214                         goto return_results;
215                 }
216
217                 Debug( LDAP_DEBUG_TRACE,
218                         "bdb_modrdn: no parent, locked root\n",
219                         0, 0, 0 );
220         }
221
222         new_parent_dn = p_dn;   /* New Parent unless newSuperior given */
223
224         if ( newSuperior != NULL ) {
225                 Debug( LDAP_DEBUG_TRACE, 
226                         "bdb_modrdn: new parent \"%s\" requested...\n",
227                         newSuperior, 0, 0 );
228
229                 np_dn = ch_strdup( newSuperior );
230                 np_ndn = ch_strdup( np_dn );
231                 (void) dn_normalize( np_ndn );
232
233                 /* newSuperior == oldParent?, if so ==> ERROR */
234                 /* newSuperior == entry being moved?, if so ==> ERROR */
235                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
236
237                 rc = bdb_dn2entry( be, ltid, np_ndn, &np, NULL, 0 );
238
239                 switch( rc ) {
240                 case 0:
241                 case DB_NOTFOUND:
242                         break;
243                 case DB_LOCK_DEADLOCK:
244                 case DB_LOCK_NOTGRANTED:
245                         goto retry;
246                 default:
247                         rc = LDAP_OTHER;
248                         text = "internal error";
249                         goto return_results;
250                 }
251
252                 if( np == NULL) {
253                         Debug( LDAP_DEBUG_TRACE,
254                                 "bdb_modrdn: newSup(ndn=%s) not here!\n",
255                                 np_ndn, 0, 0);
256                         rc = LDAP_OTHER;
257                         goto return_results;
258                 }
259
260                 Debug( LDAP_DEBUG_TRACE,
261                         "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
262                         np, np->e_id, 0 );
263
264                 /* check newSuperior for "children" acl */
265                 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
266                         Debug( LDAP_DEBUG_TRACE,
267                                 "bdb_modrdn: no wr to newSup children\n",
268                                 0, 0, 0 );
269                         rc = LDAP_INSUFFICIENT_ACCESS;
270                         goto return_results;
271                 }
272
273                 if ( is_entry_alias( np ) ) {
274                         /* entry is an alias, don't allow bind */
275                         Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
276                                 0, 0, 0 );
277
278                         rc = LDAP_ALIAS_PROBLEM;
279                         goto return_results;
280                 }
281
282                 if ( is_entry_referral( np ) ) {
283                         /* parent is a referral, don't allow add */
284                         /* parent is an alias, don't allow add */
285                         Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
286                                 0, 0, 0 );
287
288                         rc = LDAP_OPERATIONS_ERROR;
289                         goto return_results;
290                 }
291
292                 Debug( LDAP_DEBUG_TRACE,
293                         "bdb_modrdn: wr to new parent's children OK\n",
294                         0, 0, 0 );
295
296                 new_parent_dn = np_dn;
297         }
298         
299         /* Build target dn and make sure target entry doesn't exist already. */
300         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
301
302         new_ndn = ch_strdup(new_dn);
303         (void) dn_normalize( new_ndn );
304
305         Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
306                 new_ndn, 0, 0 );
307
308         rc = bdb_dn2id ( be, ltid, new_ndn, &id );
309         switch( rc ) {
310         case DB_LOCK_DEADLOCK:
311         case DB_LOCK_NOTGRANTED:
312                 goto retry;
313         case DB_NOTFOUND:
314                 break;
315         case 0:
316                 rc = LDAP_ALREADY_EXISTS;
317                 goto return_results;
318         default:
319                 rc = LDAP_OTHER;
320                 text = "internal error";
321                 goto return_results;
322         }
323
324         Debug( LDAP_DEBUG_TRACE,
325                 "bdb_modrdn: new ndn=%s does not exist\n",
326                 new_ndn, 0, 0 );
327
328         /* Get attribute type and attribute value of our new rdn, we will
329          * need to add that to our new entry
330          */
331
332         if ( rdn_attrs( newrdn, &new_rdn_types, &new_rdn_vals ) ) {
333                 Debug( LDAP_DEBUG_TRACE,
334                         "bdb_modrdn: can't figure out type(s)/values(s) "
335                         "of newrdn\n", 0, 0, 0 );
336                 rc = LDAP_OPERATIONS_ERROR;
337                 text = "unknown type(s) used in RDN";
338                 goto return_results;            
339         }
340
341         Debug( LDAP_DEBUG_TRACE,
342                 "bdb_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
343                 new_rdn_vals[ 0 ], new_rdn_types[ 0 ], 0 );
344
345         /* Retrieve the old rdn from the entry's dn */
346         if ( ( old_rdn = dn_rdn( be, dn ) ) == NULL ) {
347                 Debug( LDAP_DEBUG_TRACE,
348                         "bdb_modrdn: can't figure out old_rdn from dn\n",
349                         0, 0, 0 );
350                 rc = LDAP_OTHER;
351                 text = "could not parse old DN";
352                 goto return_results;            
353         }
354
355         if ( rdn_attrs( old_rdn, &old_rdn_types, &old_rdn_vals ) ) {
356                 Debug( LDAP_DEBUG_TRACE,
357                         "bdb_back_modrdn: can't figure out the old_rdn "
358                         "type(s)/value(s)\n", 0, 0, 0 );
359                 rc = LDAP_OTHER;
360                 text = "cannot parse RDN from old DN";
361                 goto return_results;            
362         }
363         
364         if ( newSuperior == NULL
365                 && charray_strcasecmp( ( const char ** )old_rdn_types, 
366                                 ( const char ** )new_rdn_types ) != 0 ) {
367                 /* Not a big deal but we may say something */
368                 Debug( LDAP_DEBUG_TRACE,
369                         "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
370                         "do not match\n", 
371                         old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
372         }               
373
374         /* Add new attribute values to the entry */
375         for ( a_cnt = 0; new_rdn_types[ a_cnt ]; a_cnt++ ) {
376                 int                     rc;
377                 AttributeDescription    *desc = NULL;
378                 Modifications           *mod_tmp;
379                 struct berval           val;
380
381                 rc = slap_str2ad( new_rdn_types[ a_cnt ], &desc, &text );
382
383                 if ( rc != LDAP_SUCCESS ) {
384                         Debug( LDAP_DEBUG_TRACE,
385                                 "bdb_modrdn: %s: %s (new)\n",
386                                 text, new_rdn_types[ a_cnt ], 0 );
387                         goto return_results;            
388                 }
389
390                 /* ACL check of newly added attrs */
391                 val.bv_val = new_rdn_vals[ a_cnt ];
392                 val.bv_len = strlen( val.bv_val );
393                 if ( !access_allowed( be, conn, op, p, 
394                                 desc, &val, ACL_WRITE ) ) {
395                         Debug( LDAP_DEBUG_TRACE,
396                                 "bdb_modrdn: access to attr \"%s\" "
397                                 "(new) not allowed\n", 
398                                 new_rdn_types[ a_cnt ], 0, 0 );
399                         ad_free( desc, 1);
400                         rc = LDAP_INSUFFICIENT_ACCESS;
401                         goto return_results;
402                 }
403
404                 /* Apply modification */
405                 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
406                 mod_tmp->sml_desc = desc;
407                 mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
408                 mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( new_rdn_vals[ a_cnt ] );
409                 mod_tmp->sml_bvalues[ 1 ] = NULL;
410                 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
411                 mod_tmp->sml_next = mod;
412                 mod = mod_tmp;
413         }
414
415         /* Remove old rdn value if required */
416         if ( deleteoldrdn ) {
417                 /* Get value of old rdn */
418                 if ( old_rdn_vals == NULL) {
419                         Debug( LDAP_DEBUG_TRACE,
420                                 "bdb_modrdn: can't figure out old RDN value(s) "
421                                 "from old RDN\n", 0, 0, 0 );
422                         rc = LDAP_OTHER;
423                         text = "could not parse value(s) from old RDN";
424                         goto return_results;            
425                 }
426
427                 for ( d_cnt = 0; old_rdn_types[ d_cnt ]; d_cnt++ ) {
428                         int                     rc;
429                         AttributeDescription    *desc = NULL;
430                         Modifications           *mod_tmp;
431                         struct berval           val;
432
433                         rc = slap_str2ad( old_rdn_types[ d_cnt ],
434                                         &desc, &text );
435
436                         if ( rc != LDAP_SUCCESS ) {
437                                 Debug( LDAP_DEBUG_TRACE,
438                                         "bdb_modrdn: %s: %s (old)\n",
439                                         text, old_rdn_types[ d_cnt ], 0 );
440                                 goto return_results;            
441                         }
442
443                         /* ACL check of newly added attrs */
444                         val.bv_val = new_rdn_vals[ d_cnt ];
445                         val.bv_len = strlen( val.bv_val );
446                         if ( !access_allowed( be, conn, op, p, 
447                                         desc, &val, ACL_WRITE ) ) {
448                                 Debug( LDAP_DEBUG_TRACE,
449                                         "bdb_modrdn: access to attr \"%s\" "
450                                         "(old) not allowed\n", 
451                                         old_rdn_types[ d_cnt ], 0, 0 );
452                                 ad_free( desc, 1);
453                                 rc = LDAP_INSUFFICIENT_ACCESS;
454                                 goto return_results;
455                         }
456
457                         /* Apply modification */
458                         mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
459                         mod_tmp->sml_desc = desc;
460                         mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
461                         mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( old_rdn_vals[ d_cnt ] );
462                         mod_tmp->sml_bvalues[ 1 ] = NULL;
463                         mod_tmp->sml_op = LDAP_MOD_DELETE;
464                         mod_tmp->sml_next = mod;
465                         mod = mod_tmp;
466                 }
467         }
468         
469         /* delete old one */
470         rc = bdb_dn2id_delete( be, ltid, e->e_ndn, e->e_id );
471         if ( rc != 0 ) {
472                 switch( rc ) {
473                 case DB_LOCK_DEADLOCK:
474                 case DB_LOCK_NOTGRANTED:
475                         goto retry;
476                 }
477                 rc = LDAP_OTHER;
478                 text = "DN index delete fail";
479                 goto return_results;
480         }
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         rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
491         if ( rc != 0 ) {
492                 switch( rc ) {
493                 case DB_LOCK_DEADLOCK:
494                 case DB_LOCK_NOTGRANTED:
495                         goto retry;
496                 }
497                 rc = LDAP_OTHER;
498                 text = "DN index add failed";
499                 goto return_results;
500         }
501
502         /* modify entry */
503         rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
504                 &text, textbuf, textlen );
505
506         if( rc != LDAP_SUCCESS ) {
507                 switch( rc ) {
508                 case DB_LOCK_DEADLOCK:
509                 case DB_LOCK_NOTGRANTED:
510                         goto retry;
511                 }
512                 goto return_results;
513         }
514         
515         /* NOTE: after this you must not free new_dn or new_ndn!
516          * They are used by cache.
517          */
518
519         /* id2entry index */
520         rc = bdb_id2entry_update( be, ltid, e );
521         if ( rc != 0 ) {
522                 switch( rc ) {
523                 case DB_LOCK_DEADLOCK:
524                 case DB_LOCK_NOTGRANTED:
525                         goto retry;
526                 }
527                 rc = LDAP_OTHER;
528                 text = "entry update failed";
529                 goto return_results;
530         }
531
532         rc = txn_commit( ltid, 0 );
533         ltid = NULL;
534         op->o_private = NULL;
535
536         if( rc != 0 ) {
537                 Debug( LDAP_DEBUG_TRACE,
538                         "bdb_modrdn: txn_commit failed: %s (%d)\n",
539                         db_strerror(rc), rc, 0 );
540                 rc = LDAP_OTHER;
541                 text = "commit failed";
542         } else {
543                 Debug( LDAP_DEBUG_TRACE,
544                         "bdb_modrdn: added id=%08x dn=\"%s\"\n",
545                         e->e_id, e->e_dn, 0 );
546                 rc = LDAP_SUCCESS;
547                 text = NULL;
548         }
549
550 return_results:
551         send_ldap_result( conn, op, rc,
552                 NULL, text, NULL, NULL );
553
554         if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
555                 ldap_pvt_thread_yield();
556                 txn_checkpoint( bdb->bi_dbenv,
557                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
558         }
559
560 done:
561         if( new_dn != NULL ) free( new_dn );
562         if( new_ndn != NULL ) free( new_ndn );
563
564         if( p_dn != NULL ) free( p_dn );
565         if( p_ndn != NULL ) free( p_ndn );
566
567         /* LDAP v2 supporting correct attribute handling. */
568         if( new_rdn_types != NULL ) charray_free(new_rdn_types);
569         if( new_rdn_vals != NULL ) charray_free(new_rdn_vals);
570         if( old_rdn != NULL ) free(old_rdn);
571         if( old_rdn_types != NULL ) charray_free(old_rdn_types);
572         if( old_rdn_vals != NULL ) charray_free(old_rdn_vals);
573         if( mod != NULL ) slap_mods_free(mod);
574
575         /* LDAP v3 Support */
576         if ( np_dn != NULL ) free( np_dn );
577         if ( np_ndn != NULL ) free( np_ndn );
578
579         if( np != NULL ) {
580                 /* free new parent and writer lock */
581                 bdb_entry_return( be, np );
582         }
583
584         if( p != NULL ) {
585                 /* free parent and writer lock */
586                 bdb_entry_return( be, p );
587         }
588
589         /* free entry */
590         if( e != NULL ) {
591                 bdb_entry_return( be, e );
592         }
593
594         if( ltid != NULL ) {
595                 txn_abort( ltid );
596                 op->o_private = NULL;
597         }
598
599         return rc;
600 }