]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
unifdef -UNEW_LOGGING
[openldap] / servers / slapd / back-ldbm / modrdn.c
1 /* modrdn.c - ldbm backend modrdn routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright 1999, Juan C. Gomez, All rights reserved.
17  * This software is not subject to any license of Silicon Graphics 
18  * Inc. or Purdue University.
19  *
20  * Redistribution and use in source and binary forms are permitted
21  * without restriction or fee of any kind as long as this notice
22  * is preserved.
23  */
24
25 #include "portable.h"
26
27 #include <stdio.h>
28
29 #include <ac/string.h>
30 #include <ac/socket.h>
31
32 #include "slap.h"
33 #include "back-ldbm.h"
34 #include "proto-back-ldbm.h"
35
36 int
37 ldbm_back_modrdn(
38     Operation   *op,
39     SlapReply   *rs )
40 {
41         AttributeDescription *children = slap_schema.si_ad_children;
42         AttributeDescription *entry = slap_schema.si_ad_entry;
43         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
44         struct berval   p_dn, p_ndn;
45         struct berval   new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
46         struct berval   old_ndn = BER_BVNULL;
47         Entry           *e, *p = NULL;
48         Entry           *matched;
49         /* LDAP v2 supporting correct attribute handling. */
50         LDAPRDN         new_rdn = NULL;
51         LDAPRDN         old_rdn = NULL;
52         int             isroot = -1;
53         int             rc_id = 0;
54         ID              id = NOID;
55         const char      *text = NULL;
56         char            textbuf[SLAP_TEXT_BUFLEN];
57         size_t          textlen = sizeof textbuf;
58         /* Added to support newSuperior */ 
59         Entry           *np = NULL;     /* newSuperior Entry */
60         struct berval   *np_ndn = NULL; /* newSuperior ndn */
61         struct berval   *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
62         /* Used to interface with ldbm_modify_internal() */
63         Modifications   *mod = NULL;            /* Used to delete old/add new rdn */
64         int             manageDSAit = get_manageDSAit( op );
65
66         Debug( LDAP_DEBUG_TRACE,
67                 "==>ldbm_back_modrdn: dn: %s newSuperior=%s\n", 
68                 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
69                 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len )
70                         ? op->oq_modrdn.rs_newSup->bv_val : "NULL", 0 );
71
72         /* grab giant lock for writing */
73         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
74
75         e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
76
77         /* get entry with writer lock */
78         /* FIXME: dn2entry() should return non-glue entry */
79         if (( e == NULL  ) || ( !manageDSAit && e && is_entry_glue( e ))) {
80                 if ( matched != NULL ) {
81                         rs->sr_matched = strdup( matched->e_dn );
82                         rs->sr_ref = is_entry_referral( matched )
83                                 ? get_entry_referrals( op, matched )
84                                 : NULL;
85                         cache_return_entry_r( &li->li_cache, matched );
86                 } else {
87                         BerVarray deref = NULL;
88                         if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
89                                 syncinfo_t *si;
90                                 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
91                                         struct berval tmpbv;
92                                         ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
93                                         ber_bvarray_add( &deref, &tmpbv );
94                                 }
95                         } else {
96                                 deref = default_referral;
97                         }
98                         rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
99                                                         LDAP_SCOPE_DEFAULT );
100                 }
101
102                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
103
104                 rs->sr_err = LDAP_REFERRAL;
105                 send_ldap_result( op, rs );
106
107                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
108                 free( (char *)rs->sr_matched );
109                 rs->sr_ref = NULL;
110                 rs->sr_matched = NULL;
111                 return rs->sr_err;
112         }
113
114         /* check entry for "entry" acl */
115         if ( ! access_allowed( op, e,
116                 entry, NULL, ACL_WRITE, NULL ) )
117         {
118                 Debug( LDAP_DEBUG_TRACE,
119                         "<=- ldbm_back_modrdn: no write access to entry\n", 0,
120                         0, 0 );
121
122                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
123                         "no write access to entry" );
124
125                 goto return_results;
126         }
127
128         if (!manageDSAit && is_entry_referral( e ) ) {
129                 /* parent is a referral, don't allow add */
130                 /* parent is an alias, don't allow add */
131                 rs->sr_ref = get_entry_referrals( op, e );
132
133                 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
134                     0, 0 );
135
136                 rs->sr_err = LDAP_REFERRAL;
137                 rs->sr_matched = e->e_name.bv_val;
138                 send_ldap_result( op, rs );
139
140                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
141                 rs->sr_ref = NULL;
142                 rs->sr_matched = NULL;
143                 goto return_results;
144         }
145
146         if ( has_children( op->o_bd, e ) ) {
147                 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
148                     0, 0 );
149
150                 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
151                     "subtree rename not supported" );
152                 goto return_results;
153         }
154
155         if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
156                 p_ndn = slap_empty_bv ;
157         } else {
158                 dnParent( &e->e_nname, &p_ndn );
159         }
160
161         if ( p_ndn.bv_len != 0 ) {
162                 /* Make sure parent entry exist and we can write its 
163                  * children.
164                  */
165
166                 if( (p = dn2entry_w( op->o_bd, &p_ndn, NULL )) == NULL) {
167                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
168                                 0, 0, 0);
169
170                         send_ldap_error( op, rs, LDAP_OTHER,
171                                 "parent entry does not exist" );
172
173                         goto return_results;
174                 }
175
176                 /* check parent for "children" acl */
177                 if ( ! access_allowed( op, p,
178                         children, NULL, ACL_WRITE, NULL ) )
179                 {
180                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
181                                 0, 0 );
182
183                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
184                                 NULL );
185                         goto return_results;
186                 }
187
188                 Debug( LDAP_DEBUG_TRACE,
189                        "ldbm_back_modrdn: wr to children of entry %s OK\n",
190                        p_ndn.bv_val, 0, 0 );
191
192                 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
193                         p_dn = slap_empty_bv;
194                 } else {
195                         dnParent( &e->e_name, &p_dn );
196                 }
197
198                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
199                        p_dn.bv_val, 0, 0 );
200
201         } else {
202                 /* no parent, must be root to modify rdn */
203                 isroot = be_isroot( op );
204                 if ( ! isroot ) {
205                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
206                                 || be_shadow_update( op ) ) {
207                                 int     can_access;
208                                 p = (Entry *)&slap_entry_root;
209                                 
210                                 can_access = access_allowed( op, p,
211                                                 children, NULL, ACL_WRITE, NULL );
212                                 p = NULL;
213                                                                 
214                                 /* check parent for "children" acl */
215                                 if ( ! can_access ) {
216                                         Debug( LDAP_DEBUG_TRACE,
217                                                 "<=- ldbm_back_modrdn: no "
218                                                 "access to parent\n", 0, 0, 0 );
219
220                                         send_ldap_error( op, rs,
221                                                 LDAP_INSUFFICIENT_ACCESS,
222                                                 NULL );
223                                         goto return_results;
224                                 }
225
226                         } else {
227                                 Debug( LDAP_DEBUG_TRACE,
228                                         "<=- ldbm_back_modrdn: no parent & "
229                                         "not root\n", 0, 0, 0);
230
231                                 send_ldap_error( op, rs,
232                                         LDAP_INSUFFICIENT_ACCESS,
233                                         NULL );
234                                 goto return_results;
235                         }
236                 }
237
238                 Debug( LDAP_DEBUG_TRACE,
239                        "ldbm_back_modrdn: no parent, locked root\n",
240                        0, 0, 0 );
241         }
242
243         new_parent_dn = &p_dn;  /* New Parent unless newSuperior given */
244
245         if ( op->oq_modrdn.rs_newSup != NULL ) {
246                 Debug( LDAP_DEBUG_TRACE, 
247                         "ldbm_back_modrdn: new parent \"%s\" requested...\n",
248                         op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
249
250                 np_ndn = op->oq_modrdn.rs_nnewSup;
251
252                 /* newSuperior == oldParent? */
253                 if ( dn_match( &p_ndn, np_ndn ) ) {
254                         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: "
255                                 "new parent\"%s\" seems to be the same as the "
256                                 "old parent \"%s\"\n",
257                                 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
258
259                         op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
260                 }
261         }
262
263         if ( op->oq_modrdn.rs_newSup != NULL ) {
264                 /* newSuperior == entry being moved?, if so ==> ERROR */
265                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
266
267                 if ( op->oq_modrdn.rs_nnewSup->bv_len ) {
268                         if( (np = dn2entry_w( op->o_bd, np_ndn, NULL )) == NULL) {
269                                 Debug( LDAP_DEBUG_TRACE,
270                                     "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
271                                     np_ndn->bv_val, 0, 0);
272
273                                 send_ldap_error( op, rs, LDAP_OTHER,
274                                         "newSuperior not found" );
275                                 goto return_results;
276                         }
277
278                         Debug( LDAP_DEBUG_TRACE,
279                                 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
280                                 (void *) np, np->e_id, 0 );
281
282                         /* check newSuperior for "children" acl */
283                         if ( !access_allowed( op, np, children, NULL,
284                                               ACL_WRITE, NULL ) )
285                         {
286                                 Debug( LDAP_DEBUG_TRACE,
287                                        "ldbm_back_modrdn: no wr to newSup children\n",
288                                        0, 0, 0 );
289
290                                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
291                                 goto return_results;
292                         }
293
294                         if ( is_entry_alias( np ) ) {
295                                 /* parent is an alias, don't allow add */
296                                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
297
298
299                                 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
300                                     "newSuperior is an alias" );
301
302                                 goto return_results;
303                         }
304
305                         if ( is_entry_referral( np ) ) {
306                                 /* parent is a referral, don't allow add */
307                                 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
308                                         np->e_dn, 0, 0 );
309
310                                 send_ldap_error( op, rs, LDAP_OTHER,
311                                     "newSuperior is a referral" );
312
313                                 goto return_results;
314                         }
315
316                 } else {
317
318                         /* no parent, must be root to modify newSuperior */
319                         if ( isroot == -1 ) {
320                                 isroot = be_isroot( op );
321                         }
322
323                         if ( ! isroot ) {
324                                 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
325                                         || be_shadow_update( op ) ) {
326                                         int     can_access;
327                                         np = (Entry *)&slap_entry_root;
328                                 
329                                         can_access = access_allowed( op, np,
330                                                         children, NULL, ACL_WRITE, NULL );
331                                         np = NULL;
332                                                                 
333                                         /* check parent for "children" acl */
334                                         if ( ! can_access ) {
335                                                 Debug( LDAP_DEBUG_TRACE,
336                                                         "<=- ldbm_back_modrdn: no "
337                                                         "access to new superior\n", 0, 0, 0 );
338
339                                                 send_ldap_error( op, rs,
340                                                         LDAP_INSUFFICIENT_ACCESS,
341                                                         NULL );
342                                                 goto return_results;
343                                         }
344
345                                 } else {
346                                         Debug( LDAP_DEBUG_TRACE,
347                                                 "<=- ldbm_back_modrdn: \"\" "
348                                                 "not allowed as new superior\n", 
349                                                 0, 0, 0);
350
351                                         send_ldap_error( op, rs,
352                                                 LDAP_INSUFFICIENT_ACCESS,
353                                                 NULL );
354                                         goto return_results;
355                                 }
356                         }
357                 }
358
359                 Debug( LDAP_DEBUG_TRACE,
360                     "ldbm_back_modrdn: wr to new parent's children OK\n",
361                     0, 0, 0 );
362
363                 new_parent_dn = op->oq_modrdn.rs_newSup;
364         }
365         
366         /* Build target dn and make sure target entry doesn't exist already. */
367         build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn, NULL ); 
368         dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
369
370         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
371             new_ndn.bv_val, 0, 0 );
372
373         /* check for abandon */
374         if ( op->o_abandon ) {
375                 goto return_results;
376         }
377
378         if ( ( rc_id = dn2id ( op->o_bd, &new_ndn, &id ) ) || id != NOID ) {
379                 /* if (rc_id) something bad happened to ldbm cache */
380                 rs->sr_err = rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
381                 send_ldap_result( op, rs );
382                 goto return_results;
383         }
384
385         Debug( LDAP_DEBUG_TRACE,
386             "ldbm_back_modrdn: new ndn=%s does not exist\n",
387             new_ndn.bv_val, 0, 0 );
388
389         /* Get attribute type and attribute value of our new rdn, we will
390          * need to add that to our new entry
391          */
392         if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text,
393                 LDAP_DN_FORMAT_LDAP ) )
394         {
395                 Debug( LDAP_DEBUG_TRACE,
396                         "ldbm_back_modrdn: can't figure out "
397                         "type(s)/values(s) of newrdn\n", 
398                         0, 0, 0 );
399                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX,
400                                     "unknown type(s) used in RDN" );
401                 goto return_results;            
402         }
403
404         Debug( LDAP_DEBUG_TRACE,
405                 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
406                 "new_rdn_val=\"%s\"\n",
407                 new_rdn[ 0 ]->la_attr.bv_val,
408                 new_rdn[ 0 ]->la_value.bv_val, 0 );
409
410         if ( op->oq_modrdn.rs_deleteoldrdn ) {
411                 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
412                         LDAP_DN_FORMAT_LDAP ) )
413                 {
414                         Debug( LDAP_DEBUG_TRACE,
415                                 "ldbm_back_modrdn: can't figure out "
416                                 "the old_rdn type(s)/value(s)\n", 
417                                 0, 0, 0 );
418                         send_ldap_error( op, rs, LDAP_OTHER,
419                                     "cannot parse RDN from old DN" );
420                         goto return_results;            
421                 }
422         }
423
424         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
425                0, 0, 0 );
426         
427         if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
428                 send_ldap_result( op, rs );
429                 goto return_results;
430         }
431
432         /* check for abandon */
433         if ( op->o_abandon ) {
434                 goto return_results;
435         }
436
437         (void) cache_delete_entry( &li->li_cache, e );
438
439         free( e->e_dn );
440         old_ndn = e->e_nname;
441         e->e_name = new_dn;
442         e->e_nname = new_ndn;
443         new_dn.bv_val = NULL;
444         new_ndn.bv_val = NULL;
445
446         /* NOTE: after this you must not free new_dn or new_ndn!
447          * They are used by cache.
448          */
449
450         /* modify memory copy of entry */
451         rs->sr_err = ldbm_modify_internal( op, &mod[0], e,
452                 &rs->sr_text, textbuf, textlen );
453         switch ( rs->sr_err ) {
454         case LDAP_SUCCESS:
455                 break;
456
457         default:
458                 send_ldap_result( op, rs );
459                 /* FALLTHRU */
460         case SLAPD_ABANDON:
461                 goto return_results;
462         }
463         
464         /*
465          * NOTE: the backend MUST delete then add the entry,
466          *              otherwise indexing may get hosed
467          * FIXME: if a new ID was used, the add could be done first.
468          *              that would be safer.
469          */
470
471         /* delete old one */
472         if ( dn2id_delete( op->o_bd, &old_ndn, e->e_id ) != 0 ) {
473                 send_ldap_error( op, rs, LDAP_OTHER,
474                         "DN index delete fail" );
475                 goto return_results;
476         }
477
478         /* add new one */
479         if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
480                 /* try to repair old entry - probably hopeless */
481         if( dn2id_add( op->o_bd, &old_ndn, e->e_id) != 0 ) {
482                         send_ldap_error( op, rs, LDAP_OTHER,
483                                 "DN index add and repair failed" );
484                 } else {
485                         send_ldap_error( op, rs, LDAP_OTHER,
486                                 "DN index add failed" );
487                 }
488                 goto return_results;
489         }
490
491         /* id2entry index */
492         if ( id2entry_add( op->o_bd, e ) != 0 ) {
493                 /* Try to undo */
494                 int rc;
495                 rc = dn2id_delete( op->o_bd, &e->e_nname, e->e_id );
496                 rc |= dn2id_add( op->o_bd, &old_ndn, e->e_id );
497                 if( rc ) {
498                         send_ldap_error( op, rs, LDAP_OTHER,
499                                 "entry update and repair failed" );
500                 } else {
501                         send_ldap_error( op, rs, LDAP_OTHER,
502                                 "entry update failed" );
503                 }
504                 goto return_results;
505         }
506
507         (void) cache_update_entry( &li->li_cache, e );
508
509         rs->sr_err = LDAP_SUCCESS;
510         rs->sr_text = NULL;
511         send_ldap_result( op, rs );
512         cache_entry_commit( e );
513
514 return_results:
515         if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
516         if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
517         if( old_ndn.bv_val != NULL ) free( old_ndn.bv_val );
518
519         /* LDAP v2 supporting correct attribute handling. */
520         if ( new_rdn != NULL ) {
521                 ldap_rdnfree( new_rdn );
522         }
523         if ( old_rdn != NULL ) {
524                 ldap_rdnfree( old_rdn );
525         }
526         if ( mod != NULL ) {
527                 Modifications *tmp;
528                 for (; mod; mod = tmp ) {
529                         /* slap_modrdn2mods does things one way,
530                          * slap_mods_opattrs does it differently
531                          */
532                         if ( mod->sml_op != SLAP_MOD_SOFTADD &&
533                                 mod->sml_op != LDAP_MOD_DELETE ) break;
534                         if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
535                         tmp = mod->sml_next;
536                         free( mod );
537                 }
538                 slap_mods_free( mod );
539         }
540
541         /* LDAP v3 Support */
542         if( np != NULL ) {
543                 /* free new parent and writer lock */
544                 cache_return_entry_w( &li->li_cache, np );
545         }
546
547         if( p != NULL ) {
548                 /* free parent and writer lock */
549                 cache_return_entry_w( &li->li_cache, p );
550         }
551
552         /* free entry and writer lock */
553         cache_return_entry_w( &li->li_cache, e );
554         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
555         rs->sr_text = NULL;
556         return( rs->sr_err );
557 }