]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
fix referral return in back-ldbm as well (same as ITS#3475)
[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-2005 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                 BerVarray deref = NULL;
81                 if ( matched != NULL ) {
82                         rs->sr_matched = strdup( matched->e_dn );
83                         rs->sr_ref = is_entry_referral( matched )
84                                 ? get_entry_referrals( op, matched )
85                                 : NULL;
86                         cache_return_entry_r( &li->li_cache, matched );
87                 } else {
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                 if ( deref != default_referral ) {
109                         ber_bvarray_free( deref );
110                 }
111                 free( (char *)rs->sr_matched );
112                 rs->sr_ref = NULL;
113                 rs->sr_matched = NULL;
114                 return rs->sr_err;
115         }
116
117         /* check entry for "entry" acl */
118         if ( ! access_allowed( op, e,
119                 entry, NULL, ACL_WRITE, NULL ) )
120         {
121                 Debug( LDAP_DEBUG_TRACE,
122                         "<=- ldbm_back_modrdn: no write access to entry\n", 0,
123                         0, 0 );
124
125                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
126                         "no write access to entry" );
127
128                 goto return_results;
129         }
130
131         if (!manageDSAit && is_entry_referral( e ) ) {
132                 /* parent is a referral, don't allow add */
133                 /* parent is an alias, don't allow add */
134                 rs->sr_ref = get_entry_referrals( op, e );
135
136                 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
137                     0, 0 );
138
139                 rs->sr_err = LDAP_REFERRAL;
140                 rs->sr_matched = e->e_name.bv_val;
141                 send_ldap_result( op, rs );
142
143                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
144                 rs->sr_ref = NULL;
145                 rs->sr_matched = NULL;
146                 goto return_results;
147         }
148
149         if ( has_children( op->o_bd, e ) ) {
150                 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
151                     0, 0 );
152
153                 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
154                     "subtree rename not supported" );
155                 goto return_results;
156         }
157
158         if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
159                 p_ndn = slap_empty_bv ;
160         } else {
161                 dnParent( &e->e_nname, &p_ndn );
162         }
163
164         if ( p_ndn.bv_len != 0 ) {
165                 /* Make sure parent entry exist and we can write its 
166                  * children.
167                  */
168
169                 if( (p = dn2entry_w( op->o_bd, &p_ndn, NULL )) == NULL) {
170                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
171                                 0, 0, 0);
172
173                         send_ldap_error( op, rs, LDAP_OTHER,
174                                 "parent entry does not exist" );
175
176                         goto return_results;
177                 }
178
179                 /* check parent for "children" acl */
180                 if ( ! access_allowed( op, p,
181                         children, NULL, ACL_WRITE, NULL ) )
182                 {
183                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
184                                 0, 0 );
185
186                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
187                                 NULL );
188                         goto return_results;
189                 }
190
191                 Debug( LDAP_DEBUG_TRACE,
192                        "ldbm_back_modrdn: wr to children of entry %s OK\n",
193                        p_ndn.bv_val, 0, 0 );
194
195                 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
196                         p_dn = slap_empty_bv;
197                 } else {
198                         dnParent( &e->e_name, &p_dn );
199                 }
200
201                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
202                        p_dn.bv_val, 0, 0 );
203
204         } else {
205                 /* no parent, must be root to modify rdn */
206                 isroot = be_isroot( op );
207                 if ( ! isroot ) {
208                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
209                                 || be_shadow_update( op ) ) {
210                                 int     can_access;
211                                 p = (Entry *)&slap_entry_root;
212                                 
213                                 can_access = access_allowed( op, p,
214                                                 children, NULL, ACL_WRITE, NULL );
215                                 p = NULL;
216                                                                 
217                                 /* check parent for "children" acl */
218                                 if ( ! can_access ) {
219                                         Debug( LDAP_DEBUG_TRACE,
220                                                 "<=- ldbm_back_modrdn: no "
221                                                 "access to parent\n", 0, 0, 0 );
222
223                                         send_ldap_error( op, rs,
224                                                 LDAP_INSUFFICIENT_ACCESS,
225                                                 NULL );
226                                         goto return_results;
227                                 }
228
229                         } else {
230                                 Debug( LDAP_DEBUG_TRACE,
231                                         "<=- ldbm_back_modrdn: no parent & "
232                                         "not root\n", 0, 0, 0);
233
234                                 send_ldap_error( op, rs,
235                                         LDAP_INSUFFICIENT_ACCESS,
236                                         NULL );
237                                 goto return_results;
238                         }
239                 }
240
241                 Debug( LDAP_DEBUG_TRACE,
242                        "ldbm_back_modrdn: no parent, locked root\n",
243                        0, 0, 0 );
244         }
245
246         new_parent_dn = &p_dn;  /* New Parent unless newSuperior given */
247
248         if ( op->oq_modrdn.rs_newSup != NULL ) {
249                 Debug( LDAP_DEBUG_TRACE, 
250                         "ldbm_back_modrdn: new parent \"%s\" requested...\n",
251                         op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
252
253                 np_ndn = op->oq_modrdn.rs_nnewSup;
254
255                 /* newSuperior == oldParent? */
256                 if ( dn_match( &p_ndn, np_ndn ) ) {
257                         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: "
258                                 "new parent\"%s\" seems to be the same as the "
259                                 "old parent \"%s\"\n",
260                                 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
261
262                         op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
263                 }
264         }
265
266         if ( op->oq_modrdn.rs_newSup != NULL ) {
267                 /* newSuperior == entry being moved?, if so ==> ERROR */
268                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
269
270                 if ( op->oq_modrdn.rs_nnewSup->bv_len ) {
271                         if( (np = dn2entry_w( op->o_bd, np_ndn, NULL )) == NULL) {
272                                 Debug( LDAP_DEBUG_TRACE,
273                                     "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
274                                     np_ndn->bv_val, 0, 0);
275
276                                 send_ldap_error( op, rs, LDAP_OTHER,
277                                         "newSuperior not found" );
278                                 goto return_results;
279                         }
280
281                         Debug( LDAP_DEBUG_TRACE,
282                                 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
283                                 (void *) np, np->e_id, 0 );
284
285                         /* check newSuperior for "children" acl */
286                         if ( !access_allowed( op, np, children, NULL,
287                                               ACL_WRITE, NULL ) )
288                         {
289                                 Debug( LDAP_DEBUG_TRACE,
290                                        "ldbm_back_modrdn: no wr to newSup children\n",
291                                        0, 0, 0 );
292
293                                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
294                                 goto return_results;
295                         }
296
297                         if ( is_entry_alias( np ) ) {
298                                 /* parent is an alias, don't allow add */
299                                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
300
301
302                                 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
303                                     "newSuperior is an alias" );
304
305                                 goto return_results;
306                         }
307
308                         if ( is_entry_referral( np ) ) {
309                                 /* parent is a referral, don't allow add */
310                                 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
311                                         np->e_dn, 0, 0 );
312
313                                 send_ldap_error( op, rs, LDAP_OTHER,
314                                     "newSuperior is a referral" );
315
316                                 goto return_results;
317                         }
318
319                 } else {
320
321                         /* no parent, must be root to modify newSuperior */
322                         if ( isroot == -1 ) {
323                                 isroot = be_isroot( op );
324                         }
325
326                         if ( ! isroot ) {
327                                 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
328                                         || be_shadow_update( op ) ) {
329                                         int     can_access;
330                                         np = (Entry *)&slap_entry_root;
331                                 
332                                         can_access = access_allowed( op, np,
333                                                         children, NULL, ACL_WRITE, NULL );
334                                         np = NULL;
335                                                                 
336                                         /* check parent for "children" acl */
337                                         if ( ! can_access ) {
338                                                 Debug( LDAP_DEBUG_TRACE,
339                                                         "<=- ldbm_back_modrdn: no "
340                                                         "access to new superior\n", 0, 0, 0 );
341
342                                                 send_ldap_error( op, rs,
343                                                         LDAP_INSUFFICIENT_ACCESS,
344                                                         NULL );
345                                                 goto return_results;
346                                         }
347
348                                 } else {
349                                         Debug( LDAP_DEBUG_TRACE,
350                                                 "<=- ldbm_back_modrdn: \"\" "
351                                                 "not allowed as new superior\n", 
352                                                 0, 0, 0);
353
354                                         send_ldap_error( op, rs,
355                                                 LDAP_INSUFFICIENT_ACCESS,
356                                                 NULL );
357                                         goto return_results;
358                                 }
359                         }
360                 }
361
362                 Debug( LDAP_DEBUG_TRACE,
363                     "ldbm_back_modrdn: wr to new parent's children OK\n",
364                     0, 0, 0 );
365
366                 new_parent_dn = op->oq_modrdn.rs_newSup;
367         }
368         
369         /* Build target dn and make sure target entry doesn't exist already. */
370         build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn, NULL ); 
371         dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
372
373         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
374             new_ndn.bv_val, 0, 0 );
375
376         /* check for abandon */
377         if ( op->o_abandon ) {
378                 goto return_results;
379         }
380
381         if ( ( rc_id = dn2id ( op->o_bd, &new_ndn, &id ) ) || id != NOID ) {
382                 /* if (rc_id) something bad happened to ldbm cache */
383                 rs->sr_err = rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
384                 send_ldap_result( op, rs );
385                 goto return_results;
386         }
387
388         Debug( LDAP_DEBUG_TRACE,
389             "ldbm_back_modrdn: new ndn=%s does not exist\n",
390             new_ndn.bv_val, 0, 0 );
391
392         /* Get attribute type and attribute value of our new rdn, we will
393          * need to add that to our new entry
394          */
395         if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text,
396                 LDAP_DN_FORMAT_LDAP ) )
397         {
398                 Debug( LDAP_DEBUG_TRACE,
399                         "ldbm_back_modrdn: can't figure out "
400                         "type(s)/values(s) of newrdn\n", 
401                         0, 0, 0 );
402                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX,
403                                     "unknown type(s) used in RDN" );
404                 goto return_results;            
405         }
406
407         Debug( LDAP_DEBUG_TRACE,
408                 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
409                 "new_rdn_val=\"%s\"\n",
410                 new_rdn[ 0 ]->la_attr.bv_val,
411                 new_rdn[ 0 ]->la_value.bv_val, 0 );
412
413         if ( op->oq_modrdn.rs_deleteoldrdn ) {
414                 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
415                         LDAP_DN_FORMAT_LDAP ) )
416                 {
417                         Debug( LDAP_DEBUG_TRACE,
418                                 "ldbm_back_modrdn: can't figure out "
419                                 "the old_rdn type(s)/value(s)\n", 
420                                 0, 0, 0 );
421                         send_ldap_error( op, rs, LDAP_OTHER,
422                                     "cannot parse RDN from old DN" );
423                         goto return_results;            
424                 }
425         }
426
427         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
428                0, 0, 0 );
429         
430         if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
431                 send_ldap_result( op, rs );
432                 goto return_results;
433         }
434
435         /* check for abandon */
436         if ( op->o_abandon ) {
437                 goto return_results;
438         }
439
440         (void) cache_delete_entry( &li->li_cache, e );
441
442         free( e->e_dn );
443         old_ndn = e->e_nname;
444         e->e_name = new_dn;
445         e->e_nname = new_ndn;
446         new_dn.bv_val = NULL;
447         new_ndn.bv_val = NULL;
448
449         /* NOTE: after this you must not free new_dn or new_ndn!
450          * They are used by cache.
451          */
452
453         /* modify memory copy of entry */
454         rs->sr_err = ldbm_modify_internal( op, &mod[0], e,
455                 &rs->sr_text, textbuf, textlen );
456         switch ( rs->sr_err ) {
457         case LDAP_SUCCESS:
458                 break;
459
460         default:
461                 send_ldap_result( op, rs );
462                 /* FALLTHRU */
463         case SLAPD_ABANDON:
464                 goto return_results;
465         }
466         
467         /*
468          * NOTE: the backend MUST delete then add the entry,
469          *              otherwise indexing may get hosed
470          * FIXME: if a new ID was used, the add could be done first.
471          *              that would be safer.
472          */
473
474         /* delete old one */
475         if ( dn2id_delete( op->o_bd, &old_ndn, e->e_id ) != 0 ) {
476                 send_ldap_error( op, rs, LDAP_OTHER,
477                         "DN index delete fail" );
478                 goto return_results;
479         }
480
481         /* add new one */
482         if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
483                 /* try to repair old entry - probably hopeless */
484         if( dn2id_add( op->o_bd, &old_ndn, e->e_id) != 0 ) {
485                         send_ldap_error( op, rs, LDAP_OTHER,
486                                 "DN index add and repair failed" );
487                 } else {
488                         send_ldap_error( op, rs, LDAP_OTHER,
489                                 "DN index add failed" );
490                 }
491                 goto return_results;
492         }
493
494         /* id2entry index */
495         if ( id2entry_add( op->o_bd, e ) != 0 ) {
496                 /* Try to undo */
497                 int rc;
498                 rc = dn2id_delete( op->o_bd, &e->e_nname, e->e_id );
499                 rc |= dn2id_add( op->o_bd, &old_ndn, e->e_id );
500                 if( rc ) {
501                         send_ldap_error( op, rs, LDAP_OTHER,
502                                 "entry update and repair failed" );
503                 } else {
504                         send_ldap_error( op, rs, LDAP_OTHER,
505                                 "entry update failed" );
506                 }
507                 goto return_results;
508         }
509
510         (void) cache_update_entry( &li->li_cache, e );
511
512         rs->sr_err = LDAP_SUCCESS;
513         rs->sr_text = NULL;
514         send_ldap_result( op, rs );
515         cache_entry_commit( e );
516
517 return_results:
518         if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
519         if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
520         if( old_ndn.bv_val != NULL ) free( old_ndn.bv_val );
521
522         /* LDAP v2 supporting correct attribute handling. */
523         if ( new_rdn != NULL ) {
524                 ldap_rdnfree( new_rdn );
525         }
526         if ( old_rdn != NULL ) {
527                 ldap_rdnfree( old_rdn );
528         }
529         if ( mod != NULL ) {
530                 Modifications *tmp;
531                 for (; mod; mod = tmp ) {
532                         /* slap_modrdn2mods does things one way,
533                          * slap_mods_opattrs does it differently
534                          */
535                         if ( mod->sml_op != SLAP_MOD_SOFTADD &&
536                                 mod->sml_op != LDAP_MOD_DELETE ) break;
537                         if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
538                         tmp = mod->sml_next;
539                         free( mod );
540                 }
541                 slap_mods_free( mod );
542         }
543
544         /* LDAP v3 Support */
545         if( np != NULL ) {
546                 /* free new parent and writer lock */
547                 cache_return_entry_w( &li->li_cache, np );
548         }
549
550         if( p != NULL ) {
551                 /* free parent and writer lock */
552                 cache_return_entry_w( &li->li_cache, p );
553         }
554
555         /* free entry and writer lock */
556         cache_return_entry_w( &li->li_cache, e );
557         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
558         rs->sr_text = NULL;
559         return( rs->sr_err );
560 }