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