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