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