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