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