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