]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modrdn.c
Experiment with busy loop protection...
[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 && p_ndn[ 0 ] != '\0' ) {
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 ) && !be_issuffix( be, "" ) ) {
210                         Debug( LDAP_DEBUG_TRACE,
211                                 "bdb_modrdn: no parent, not root "
212                                 "& \"\" is not suffix\n",
213                                 0, 0, 0);
214                         rc = LDAP_INSUFFICIENT_ACCESS;
215                         goto return_results;
216                 }
217
218                 Debug( LDAP_DEBUG_TRACE,
219                         "bdb_modrdn: no parent, locked root\n",
220                         0, 0, 0 );
221         }
222
223         new_parent_dn = p_dn;   /* New Parent unless newSuperior given */
224
225         if ( newSuperior != NULL ) {
226                 Debug( LDAP_DEBUG_TRACE, 
227                         "bdb_modrdn: new parent \"%s\" requested...\n",
228                         newSuperior, 0, 0 );
229
230                 np_dn = ch_strdup( newSuperior );
231                 np_ndn = ch_strdup( np_dn );
232                 (void) dn_normalize( np_ndn );
233
234                 /* newSuperior == oldParent?, if so ==> ERROR */
235                 /* newSuperior == entry being moved?, if so ==> ERROR */
236                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
237
238                 rc = bdb_dn2entry( be, ltid, np_ndn, &np, NULL, 0 );
239
240                 switch( rc ) {
241                 case 0:
242                 case DB_NOTFOUND:
243                         break;
244                 case DB_LOCK_DEADLOCK:
245                 case DB_LOCK_NOTGRANTED:
246                         goto retry;
247                 default:
248                         rc = LDAP_OTHER;
249                         text = "internal error";
250                         goto return_results;
251                 }
252
253                 if( np == NULL) {
254                         Debug( LDAP_DEBUG_TRACE,
255                                 "bdb_modrdn: newSup(ndn=%s) not here!\n",
256                                 np_ndn, 0, 0);
257                         rc = LDAP_OTHER;
258                         goto return_results;
259                 }
260
261                 Debug( LDAP_DEBUG_TRACE,
262                         "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
263                         np, (long) np->e_id, 0 );
264
265                 /* check newSuperior for "children" acl */
266                 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
267                         Debug( LDAP_DEBUG_TRACE,
268                                 "bdb_modrdn: no wr to newSup children\n",
269                                 0, 0, 0 );
270                         rc = LDAP_INSUFFICIENT_ACCESS;
271                         goto return_results;
272                 }
273
274                 if ( is_entry_alias( np ) ) {
275                         /* entry is an alias, don't allow bind */
276                         Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
277                                 0, 0, 0 );
278
279                         rc = LDAP_ALIAS_PROBLEM;
280                         goto return_results;
281                 }
282
283                 if ( is_entry_referral( np ) ) {
284                         /* parent is a referral, don't allow add */
285                         /* parent is an alias, don't allow add */
286                         Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
287                                 0, 0, 0 );
288
289                         rc = LDAP_OPERATIONS_ERROR;
290                         goto return_results;
291                 }
292
293                 Debug( LDAP_DEBUG_TRACE,
294                         "bdb_modrdn: wr to new parent's children OK\n",
295                         0, 0, 0 );
296
297                 new_parent_dn = np_dn;
298         }
299         
300         /* Build target dn and make sure target entry doesn't exist already. */
301         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
302
303         new_ndn = ch_strdup(new_dn);
304         (void) dn_normalize( new_ndn );
305
306         Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
307                 new_ndn, 0, 0 );
308
309         rc = bdb_dn2id ( be, ltid, new_ndn, &id );
310         switch( rc ) {
311         case DB_LOCK_DEADLOCK:
312         case DB_LOCK_NOTGRANTED:
313                 goto retry;
314         case DB_NOTFOUND:
315                 break;
316         case 0:
317                 rc = LDAP_ALREADY_EXISTS;
318                 goto return_results;
319         default:
320                 rc = LDAP_OTHER;
321                 text = "internal error";
322                 goto return_results;
323         }
324
325         Debug( LDAP_DEBUG_TRACE,
326                 "bdb_modrdn: new ndn=%s does not exist\n",
327                 new_ndn, 0, 0 );
328
329         /* Get attribute type and attribute value of our new rdn, we will
330          * need to add that to our new entry
331          */
332
333         if ( rdn_attrs( newrdn, &new_rdn_types, &new_rdn_vals ) ) {
334                 Debug( LDAP_DEBUG_TRACE,
335                         "bdb_modrdn: can't figure out type(s)/values(s) "
336                         "of newrdn\n", 0, 0, 0 );
337                 rc = LDAP_OPERATIONS_ERROR;
338                 text = "unknown type(s) used in RDN";
339                 goto return_results;            
340         }
341
342         Debug( LDAP_DEBUG_TRACE,
343                 "bdb_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
344                 new_rdn_vals[ 0 ], new_rdn_types[ 0 ], 0 );
345
346         /* Retrieve the old rdn from the entry's dn */
347         if ( ( old_rdn = dn_rdn( be, dn ) ) == NULL ) {
348                 Debug( LDAP_DEBUG_TRACE,
349                         "bdb_modrdn: can't figure out old_rdn from dn\n",
350                         0, 0, 0 );
351                 rc = LDAP_OTHER;
352                 text = "could not parse old DN";
353                 goto return_results;            
354         }
355
356         if ( rdn_attrs( old_rdn, &old_rdn_types, &old_rdn_vals ) ) {
357                 Debug( LDAP_DEBUG_TRACE,
358                         "bdb_back_modrdn: can't figure out the old_rdn "
359                         "type(s)/value(s)\n", 0, 0, 0 );
360                 rc = LDAP_OTHER;
361                 text = "cannot parse RDN from old DN";
362                 goto return_results;            
363         }
364         
365         if ( newSuperior == NULL
366                 && charray_strcasecmp( ( const char ** )old_rdn_types, 
367                                 ( const char ** )new_rdn_types ) != 0 ) {
368                 /* Not a big deal but we may say something */
369                 Debug( LDAP_DEBUG_TRACE,
370                         "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
371                         "do not match\n", 
372                         old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
373         }               
374
375         /* Add new attribute values to the entry */
376         for ( a_cnt = 0; new_rdn_types[ a_cnt ]; a_cnt++ ) {
377                 int                     rc;
378                 AttributeDescription    *desc = NULL;
379                 Modifications           *mod_tmp;
380                 struct berval           val;
381
382                 rc = slap_str2ad( new_rdn_types[ a_cnt ], &desc, &text );
383
384                 if ( rc != LDAP_SUCCESS ) {
385                         Debug( LDAP_DEBUG_TRACE,
386                                 "bdb_modrdn: %s: %s (new)\n",
387                                 text, new_rdn_types[ a_cnt ], 0 );
388                         goto return_results;            
389                 }
390
391                 /* ACL check of newly added attrs */
392                 val.bv_val = new_rdn_vals[ a_cnt ];
393                 val.bv_len = strlen( val.bv_val );
394                 if ( !access_allowed( be, conn, op, p, 
395                                 desc, &val, ACL_WRITE ) ) {
396                         Debug( LDAP_DEBUG_TRACE,
397                                 "bdb_modrdn: access to attr \"%s\" "
398                                 "(new) not allowed\n", 
399                                 new_rdn_types[ a_cnt ], 0, 0 );
400                         ad_free( desc, 1);
401                         rc = LDAP_INSUFFICIENT_ACCESS;
402                         goto return_results;
403                 }
404
405                 /* Apply modification */
406                 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
407                 mod_tmp->sml_desc = desc;
408                 mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
409                 mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( new_rdn_vals[ a_cnt ] );
410                 mod_tmp->sml_bvalues[ 1 ] = NULL;
411                 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
412                 mod_tmp->sml_next = mod;
413                 mod = mod_tmp;
414         }
415
416         /* Remove old rdn value if required */
417         if ( deleteoldrdn ) {
418                 /* Get value of old rdn */
419                 if ( old_rdn_vals == NULL) {
420                         Debug( LDAP_DEBUG_TRACE,
421                                 "bdb_modrdn: can't figure out old RDN value(s) "
422                                 "from old RDN\n", 0, 0, 0 );
423                         rc = LDAP_OTHER;
424                         text = "could not parse value(s) from old RDN";
425                         goto return_results;            
426                 }
427
428                 for ( d_cnt = 0; old_rdn_types[ d_cnt ]; d_cnt++ ) {
429                         int                     rc;
430                         AttributeDescription    *desc = NULL;
431                         Modifications           *mod_tmp;
432                         struct berval           val;
433
434                         rc = slap_str2ad( old_rdn_types[ d_cnt ],
435                                         &desc, &text );
436
437                         if ( rc != LDAP_SUCCESS ) {
438                                 Debug( LDAP_DEBUG_TRACE,
439                                         "bdb_modrdn: %s: %s (old)\n",
440                                         text, old_rdn_types[ d_cnt ], 0 );
441                                 goto return_results;            
442                         }
443
444                         /* ACL check of newly added attrs */
445                         val.bv_val = new_rdn_vals[ d_cnt ];
446                         val.bv_len = strlen( val.bv_val );
447                         if ( !access_allowed( be, conn, op, p, 
448                                         desc, &val, ACL_WRITE ) ) {
449                                 Debug( LDAP_DEBUG_TRACE,
450                                         "bdb_modrdn: access to attr \"%s\" "
451                                         "(old) not allowed\n", 
452                                         old_rdn_types[ d_cnt ], 0, 0 );
453                                 ad_free( desc, 1);
454                                 rc = LDAP_INSUFFICIENT_ACCESS;
455                                 goto return_results;
456                         }
457
458                         /* Apply modification */
459                         mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
460                         mod_tmp->sml_desc = desc;
461                         mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
462                         mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( old_rdn_vals[ d_cnt ] );
463                         mod_tmp->sml_bvalues[ 1 ] = NULL;
464                         mod_tmp->sml_op = LDAP_MOD_DELETE;
465                         mod_tmp->sml_next = mod;
466                         mod = mod_tmp;
467                 }
468         }
469         
470         /* delete old one */
471         rc = bdb_dn2id_delete( be, ltid, e->e_ndn, e->e_id );
472         if ( rc != 0 ) {
473                 switch( rc ) {
474                 case DB_LOCK_DEADLOCK:
475                 case DB_LOCK_NOTGRANTED:
476                         goto retry;
477                 }
478                 rc = LDAP_OTHER;
479                 text = "DN index delete fail";
480                 goto return_results;
481         }
482
483 #ifdef BDB_USE_BINARY_RW
484         /* Binary format uses a single contiguous block, cannot
485          * free individual fields. Leave new_dn/new_ndn set so
486          * they can be individually freed later.
487          */
488         e->e_dn = new_dn;
489         e->e_ndn = new_ndn;
490 #else
491         free( e->e_dn );
492         free( e->e_ndn );
493         e->e_dn = new_dn;
494         e->e_ndn = new_ndn;
495         new_dn = NULL;
496         new_ndn = NULL;
497 #endif
498
499         /* add new one */
500         rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
501         if ( rc != 0 ) {
502                 switch( rc ) {
503                 case DB_LOCK_DEADLOCK:
504                 case DB_LOCK_NOTGRANTED:
505                         goto retry;
506                 }
507                 rc = LDAP_OTHER;
508                 text = "DN index add failed";
509                 goto return_results;
510         }
511
512         /* modify entry */
513         rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
514                 &text, textbuf, textlen );
515
516         if( rc != LDAP_SUCCESS ) {
517                 switch( rc ) {
518                 case DB_LOCK_DEADLOCK:
519                 case DB_LOCK_NOTGRANTED:
520                         goto retry;
521                 }
522                 goto return_results;
523         }
524         
525         /* NOTE: after this you must not free new_dn or new_ndn!
526          * They are used by cache.
527          */
528
529         /* id2entry index */
530         rc = bdb_id2entry_update( be, ltid, e );
531         if ( rc != 0 ) {
532                 switch( rc ) {
533                 case DB_LOCK_DEADLOCK:
534                 case DB_LOCK_NOTGRANTED:
535                         goto retry;
536                 }
537                 rc = LDAP_OTHER;
538                 text = "entry update failed";
539                 goto return_results;
540         }
541
542         rc = txn_commit( ltid, 0 );
543         ltid = NULL;
544         op->o_private = NULL;
545
546         if( rc != 0 ) {
547                 Debug( LDAP_DEBUG_TRACE,
548                         "bdb_modrdn: txn_commit failed: %s (%d)\n",
549                         db_strerror(rc), rc, 0 );
550                 rc = LDAP_OTHER;
551                 text = "commit failed";
552         } else {
553                 Debug( LDAP_DEBUG_TRACE,
554                         "bdb_modrdn: added id=%08x dn=\"%s\"\n",
555                         e->e_id, e->e_dn, 0 );
556                 rc = LDAP_SUCCESS;
557                 text = NULL;
558         }
559
560 return_results:
561         send_ldap_result( conn, op, rc,
562                 NULL, text, NULL, NULL );
563
564         if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
565                 ldap_pvt_thread_yield();
566                 txn_checkpoint( bdb->bi_dbenv,
567                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
568         }
569
570 done:
571         if( new_dn != NULL ) free( new_dn );
572         if( new_ndn != NULL ) free( new_ndn );
573
574         if( p_dn != NULL ) free( p_dn );
575         if( p_ndn != NULL ) free( p_ndn );
576
577         /* LDAP v2 supporting correct attribute handling. */
578         if( new_rdn_types != NULL ) charray_free(new_rdn_types);
579         if( new_rdn_vals != NULL ) charray_free(new_rdn_vals);
580         if( old_rdn != NULL ) free(old_rdn);
581         if( old_rdn_types != NULL ) charray_free(old_rdn_types);
582         if( old_rdn_vals != NULL ) charray_free(old_rdn_vals);
583         if( mod != NULL ) slap_mods_free(mod);
584
585         /* LDAP v3 Support */
586         if ( np_dn != NULL ) free( np_dn );
587         if ( np_ndn != NULL ) free( np_ndn );
588
589         if( np != NULL ) {
590                 /* free new parent and writer lock */
591                 bdb_entry_return( be, np );
592         }
593
594         if( p != NULL ) {
595                 /* free parent and writer lock */
596                 bdb_entry_return( be, p );
597         }
598
599         /* free entry */
600         if( e != NULL ) {
601                 bdb_entry_return( be, e );
602         }
603
604         if( ltid != NULL ) {
605                 txn_abort( ltid );
606                 op->o_private = NULL;
607         }
608
609         return rc;
610 }