]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modrdn.c
Do something with the error text.
[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 = NULL;
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                         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                                 rc = LDAP_INSUFFICIENT_ACCESS;
453                                 goto return_results;
454                         }
455
456                         /* Apply modification */
457                         mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
458                         mod_tmp->sml_desc = desc;
459                         mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
460                         mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( old_rdn_vals[ d_cnt ] );
461                         mod_tmp->sml_bvalues[ 1 ] = NULL;
462                         mod_tmp->sml_op = LDAP_MOD_DELETE;
463                         mod_tmp->sml_next = mod;
464                         mod = mod_tmp;
465                 }
466         }
467         
468         /* delete old one */
469         rc = bdb_dn2id_delete( be, ltid, e->e_ndn, e->e_id );
470         if ( rc != 0 ) {
471                 switch( rc ) {
472                 case DB_LOCK_DEADLOCK:
473                 case DB_LOCK_NOTGRANTED:
474                         goto retry;
475                 }
476                 rc = LDAP_OTHER;
477                 text = "DN index delete fail";
478                 goto return_results;
479         }
480
481         /* Binary format uses a single contiguous block, cannot
482          * free individual fields. Leave new_dn/new_ndn set so
483          * they can be individually freed later.
484          */
485         e->e_dn = new_dn;
486         e->e_ndn = new_ndn;
487
488         /* add new one */
489         rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
490         if ( rc != 0 ) {
491                 switch( rc ) {
492                 case DB_LOCK_DEADLOCK:
493                 case DB_LOCK_NOTGRANTED:
494                         goto retry;
495                 }
496                 rc = LDAP_OTHER;
497                 text = "DN index add failed";
498                 goto return_results;
499         }
500
501         /* modify entry */
502         rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
503                 &text, textbuf, textlen );
504
505         if( rc != LDAP_SUCCESS ) {
506                 switch( rc ) {
507                 case DB_LOCK_DEADLOCK:
508                 case DB_LOCK_NOTGRANTED:
509                         goto retry;
510                 }
511                 goto return_results;
512         }
513         
514         /* id2entry index */
515         rc = bdb_id2entry_update( be, ltid, e );
516         if ( rc != 0 ) {
517                 switch( rc ) {
518                 case DB_LOCK_DEADLOCK:
519                 case DB_LOCK_NOTGRANTED:
520                         goto retry;
521                 }
522                 rc = LDAP_OTHER;
523                 text = "entry update failed";
524                 goto return_results;
525         }
526
527         rc = txn_commit( ltid, 0 );
528         ltid = NULL;
529         op->o_private = NULL;
530
531         if( rc != 0 ) {
532                 Debug( LDAP_DEBUG_TRACE,
533                         "bdb_modrdn: txn_commit failed: %s (%d)\n",
534                         db_strerror(rc), rc, 0 );
535                 rc = LDAP_OTHER;
536                 text = "commit failed";
537         } else {
538                 Debug( LDAP_DEBUG_TRACE,
539                         "bdb_modrdn: added id=%08x dn=\"%s\"\n",
540                         e->e_id, e->e_dn, 0 );
541                 rc = LDAP_SUCCESS;
542                 text = NULL;
543         }
544
545 return_results:
546         send_ldap_result( conn, op, rc,
547                 NULL, text, NULL, NULL );
548
549         if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
550                 ldap_pvt_thread_yield();
551                 txn_checkpoint( bdb->bi_dbenv,
552                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
553         }
554
555 done:
556         if( new_dn != NULL ) free( new_dn );
557         if( new_ndn != NULL ) free( new_ndn );
558
559         if( p_dn != NULL ) free( p_dn );
560         if( p_ndn != NULL ) free( p_ndn );
561
562         /* LDAP v2 supporting correct attribute handling. */
563         if( new_rdn_types != NULL ) charray_free(new_rdn_types);
564         if( new_rdn_vals != NULL ) charray_free(new_rdn_vals);
565         if( old_rdn != NULL ) free(old_rdn);
566         if( old_rdn_types != NULL ) charray_free(old_rdn_types);
567         if( old_rdn_vals != NULL ) charray_free(old_rdn_vals);
568         if( mod != NULL ) slap_mods_free(mod);
569
570         /* LDAP v3 Support */
571         if ( np_dn != NULL ) free( np_dn );
572         if ( np_ndn != NULL ) free( np_ndn );
573
574         if( np != NULL ) {
575                 /* free new parent and writer lock */
576                 bdb_entry_return( be, np );
577         }
578
579         if( p != NULL ) {
580                 /* free parent and writer lock */
581                 bdb_entry_return( be, p );
582         }
583
584         /* free entry */
585         if( e != NULL ) {
586                 bdb_entry_return( be, e );
587         }
588
589         if( ltid != NULL ) {
590                 txn_abort( ltid );
591                 op->o_private = NULL;
592         }
593
594         return rc;
595 }