]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
839a39615725b692335acd3a3deb4a82db39c722
[openldap] / servers / slapd / back-ldbm / modrdn.c
1 /* modrdn.c - ldbm backend modrdn routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 /*
9  * LDAP v3 newSuperior support. Add new rdn as an attribute.
10  * (Full support for v2 also used software/ideas contributed
11  * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
12  * submission!.)
13  *
14  * Copyright 1999, Juan C. Gomez, All rights reserved.
15  * This software is not subject to any license of Silicon Graphics 
16  * Inc. or Purdue University.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * without restriction or fee of any kind as long as this notice
20  * is preserved.
21  *
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32 #include "back-ldbm.h"
33 #include "proto-back-ldbm.h"
34
35 int
36 ldbm_back_modrdn(
37     Backend     *be,
38     Connection  *conn,
39     Operation   *op,
40     const char  *dn,
41     const char  *ndn,
42     const char  *newrdn,
43     int         deleteoldrdn,
44     const char  *newSuperior
45 )
46 {
47 #ifdef SLAPD_SCHEMA_NOT_COMPAT
48         AttributeDescription *children = slap_schema.si_ad_children;
49 #else
50         static const char *children = "children";
51 #endif
52         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
53         char            *p_dn = NULL, *p_ndn = NULL;
54         char            *new_dn = NULL, *new_ndn = NULL;
55         Entry           *e, *p = NULL;
56         Entry           *matched;
57         int                     rootlock = 0;
58         int                     rc = -1;
59         /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
60         char            *new_rdn_val = NULL;    /* Val of new rdn */
61         char            *new_rdn_type = NULL;   /* Type of new rdn */
62         char            *old_rdn = NULL;        /* Old rdn's attr type & val */
63         char            *old_rdn_type = NULL;   /* Type of old rdn attr. */
64         char            *old_rdn_val = NULL;    /* Old rdn attribute value */
65         /* Added to support newSuperior */ 
66         Entry           *np = NULL;     /* newSuperior Entry */
67         char            *np_dn = NULL;  /* newSuperior dn */
68         char            *np_ndn = NULL; /* newSuperior ndn */
69         char            *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
70         /* Used to interface with ldbm_modify_internal() */
71         struct berval   add_bv;                 /* Stores new rdn att */
72         struct berval   *add_bvals[2];          /* Stores new rdn att */
73         struct berval   del_bv;                 /* Stores old rdn att */
74         struct berval   *del_bvals[2];          /* Stores old rdn att */
75         Modifications   mod[2];                 /* Used to delete old rdn */
76         int             manageDSAit = get_manageDSAit( op );
77
78         Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
79                (newSuperior ? newSuperior : "NULL"),
80                0, 0 );
81
82         /* get entry with writer lock */
83         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
84                 char* matched_dn = NULL;
85                 struct berval** refs = NULL;
86
87                 if( matched != NULL ) {
88                         matched_dn = strdup( matched->e_dn );
89                         refs = is_entry_referral( matched )
90                                 ? get_entry_referrals( be, conn, op, matched )
91                                 : NULL;
92                         cache_return_entry_r( &li->li_cache, matched );
93                 } else {
94                         refs = default_referral;
95                 }
96
97                 send_ldap_result( conn, op, LDAP_REFERRAL,
98                         matched_dn, NULL, refs, NULL );
99
100                 if ( matched != NULL ) {
101                         ber_bvecfree( refs );
102                         free( matched_dn );
103                 }
104
105                 return( -1 );
106         }
107
108 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
109         if ( ! access_allowed( be, conn, op, e,
110                 "entry", NULL, ACL_WRITE ) )
111         {
112                 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
113                         0, 0 );
114                 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
115                         NULL, NULL, NULL, NULL );
116                 goto return_results;
117         }
118 #endif
119
120         if (!manageDSAit && is_entry_referral( e ) ) {
121                 /* parent is a referral, don't allow add */
122                 /* parent is an alias, don't allow add */
123                 struct berval **refs = get_entry_referrals( be,
124                         conn, op, e );
125
126                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
127                     0, 0 );
128
129                 send_ldap_result( conn, op, LDAP_REFERRAL,
130                     e->e_dn, NULL, refs, NULL );
131
132                 ber_bvecfree( refs );
133
134                 goto return_results;
135         }
136
137         if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
138
139                 /* Make sure parent entry exist and we can write its 
140                  * children.
141                  */
142
143                 if( (p = dn2entry_w( be, p_ndn, NULL )) == NULL) {
144                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
145                                 0, 0, 0);
146                         send_ldap_result( conn, op, LDAP_OTHER,
147                                 NULL, NULL, NULL, NULL );
148                         goto return_results;
149                 }
150
151                 /* check parent for "children" acl */
152                 if ( ! access_allowed( be, conn, op, p,
153                         children, NULL, ACL_WRITE ) )
154                 {
155                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
156                                 0, 0 );
157                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
158                                 NULL, NULL, NULL, NULL );
159                         goto return_results;
160                 }
161
162                 Debug( LDAP_DEBUG_TRACE,
163                        "ldbm_back_modrdn: wr to children of entry %s OK\n",
164                        p_ndn, 0, 0 );
165                 
166                 p_dn = dn_parent( be, e->e_dn );
167         
168
169                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
170                        p_dn, 0, 0 );
171
172         } else {
173                 /* no parent, modrdn entry directly under root */
174                 if( ! be_isroot( be, op->o_ndn ) ) {
175                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
176                                 0, 0, 0);
177                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
178                                 NULL, NULL, NULL, NULL );
179                         goto return_results;
180                 }
181
182                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
183                 rootlock = 1;
184                 
185                 Debug( LDAP_DEBUG_TRACE,
186                        "ldbm_back_modrdn: no parent, locked root\n",
187                        0, 0, 0 );
188
189         }
190
191         new_parent_dn = p_dn;   /* New Parent unless newSuperior given */
192
193         if ( newSuperior != NULL ) {
194                 Debug( LDAP_DEBUG_TRACE, 
195                         "ldbm_back_modrdn: new parent \"%s\" requested...\n",
196                         newSuperior, 0, 0 );
197
198                 np_dn = ch_strdup( newSuperior );
199                 np_ndn = ch_strdup( np_dn );
200                 (void) dn_normalize( np_ndn );
201
202                 /* newSuperior == oldParent?, if so ==> ERROR */
203                 /* newSuperior == entry being moved?, if so ==> ERROR */
204                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
205
206                 if( (np = dn2entry_w( be, np_ndn, NULL )) == NULL) {
207                         Debug( LDAP_DEBUG_TRACE,
208                                "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
209                                np_ndn, 0, 0);
210                         send_ldap_result( conn, op, LDAP_OTHER,
211                                 NULL, NULL, NULL, NULL );
212                         goto return_results;
213                 }
214
215                 Debug( LDAP_DEBUG_TRACE,
216                        "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
217                        np, np->e_id, 0 );
218             
219                 /* check newSuperior for "children" acl */
220                 if ( !access_allowed( be, conn, op, np, children, NULL,
221                                       ACL_WRITE ) )
222                 {
223                         Debug( LDAP_DEBUG_TRACE,
224                                "ldbm_back_modrdn: no wr to newSup children\n",
225                                0, 0, 0 );
226                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
227                                 NULL, NULL, NULL, NULL );
228                         goto return_results;
229                 }
230
231                 if ( is_entry_alias( np ) ) {
232                         /* entry is an alias, don't allow bind */
233                         Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
234                             0, 0 );
235
236                         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
237                             NULL, NULL, NULL, NULL );
238
239                         goto return_results;
240                 }
241
242                 if ( is_entry_referral( np ) ) {
243                         /* parent is a referral, don't allow add */
244                         /* parent is an alias, don't allow add */
245                         Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
246                                 0, 0 );
247
248                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
249                             NULL, NULL, NULL, NULL );
250
251                         goto return_results;
252                 }
253
254                 Debug( LDAP_DEBUG_TRACE,
255                        "ldbm_back_modrdn: wr to new parent's children OK\n",
256                        0, 0 , 0 );
257
258                 new_parent_dn = np_dn;
259         }
260         
261         /* Build target dn and make sure target entry doesn't exist already. */
262
263         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
264
265
266         new_ndn = ch_strdup(new_dn);
267         (void) dn_normalize( new_ndn );
268
269         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
270                new_ndn, 0, 0 );
271
272         /* check for abandon */
273         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
274         if ( op->o_abandon ) {
275                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
276                 goto return_results;
277         }
278
279         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
280         if (dn2id ( be, new_ndn ) != NOID) {
281                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
282                         NULL, NULL, NULL, NULL );
283                 goto return_results;
284         }
285
286         Debug( LDAP_DEBUG_TRACE,
287                "ldbm_back_modrdn: new ndn=%s does not exist\n",
288                new_ndn, 0, 0 );
289
290         /* Get attribute type and attribute value of our new rdn, we will
291          * need to add that to our new entry
292          */
293
294         if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
295             
296                 Debug( LDAP_DEBUG_TRACE,
297                        "ldbm_back_modrdn: can't figure out type of newrdn\n",
298                        0, 0, 0 );
299                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
300                         NULL, "unknown type used in RDN", NULL, NULL );
301                 goto return_results;            
302
303         }
304
305         if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
306             
307                 Debug( LDAP_DEBUG_TRACE,
308                        "ldbm_back_modrdn: can't figure out val of newrdn\n",
309                        0, 0, 0 );
310                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
311                         NULL, "could not parse RDN value", NULL, NULL );
312                 goto return_results;            
313
314         }
315
316         Debug( LDAP_DEBUG_TRACE,
317                "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
318                new_rdn_val, new_rdn_type, 0 );
319
320         /* Retrieve the old rdn from the entry's dn */
321
322         if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
323
324                 Debug( LDAP_DEBUG_TRACE,
325                        "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
326                        0, 0, 0 );
327                 send_ldap_result( conn, op, LDAP_OTHER,
328                         NULL, "could not parse old DN", NULL, NULL );
329                 goto return_results;            
330
331         }
332
333         if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
334             
335                 Debug( LDAP_DEBUG_TRACE,
336                        "ldbm_back_modrdn: can't figure out the old_rdn type\n",
337                        0, 0, 0 );
338                 send_ldap_result( conn, op, LDAP_OTHER,
339                         NULL, "count parse RDN from old DN", NULL, NULL );
340                 goto return_results;            
341                 
342         }
343         
344         if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
345
346             /* Not a big deal but we may say something */
347             Debug( LDAP_DEBUG_TRACE,
348                    "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
349                    old_rdn_type, new_rdn_type, 0 );
350             
351         }               
352
353                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
354                        0, 0, 0 );
355                 
356                 /* Add new attribute value to the entry.
357                  */
358
359                 add_bvals[0] = &add_bv;         /* Array of bervals */
360                 add_bvals[1] = NULL;
361
362                 add_bv.bv_val = new_rdn_val;
363                 add_bv.bv_len = strlen(new_rdn_val);
364                 
365 #ifdef SLAPD_SCHEMA_NOT_COMPAT
366                 /* not yet implemented */
367 #else
368                 mod[0].ml_type = new_rdn_type;  
369                 mod[0].ml_bvalues = add_bvals;
370                 mod[0].ml_op = SLAP_MOD_SOFTADD;
371                 mod[0].ml_next = NULL;
372 #endif
373
374                 /* Remove old rdn value if required */
375
376                 if (deleteoldrdn) {
377                         /* Get value of old rdn */
378         
379                         if ((old_rdn_val = rdn_attr_value( old_rdn ))
380                             == NULL) {
381                             
382                                 Debug( LDAP_DEBUG_TRACE,
383                                        "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
384                                        0, 0, 0 );
385                                 send_ldap_result( conn, op, LDAP_OTHER,
386                                         NULL, "cound not parse value from old RDN", NULL, NULL );
387                                 goto return_results;            
388                         }
389
390                         del_bvals[0] = &del_bv;         /* Array of bervals */
391                         del_bvals[1] = NULL;
392
393                         /* Remove old value of rdn as an attribute. */
394                     
395                         del_bv.bv_val = old_rdn_val;
396                         del_bv.bv_len = strlen(old_rdn_val);
397
398 #ifdef SLAPD_SCHEMA_NOT_COMPAT
399                         /* not yet implemented */
400 #else
401                         /* No need to normalize old_rdn_type, delete_values()
402                          * does that for us
403                          */
404                         mod[0].ml_next = &mod[1];
405                         mod[1].ml_type = old_rdn_type;  
406                         mod[1].ml_bvalues = del_bvals;
407                         mod[1].ml_op = LDAP_MOD_DELETE;
408                         mod[1].ml_next = NULL;
409 #endif
410
411                         Debug( LDAP_DEBUG_TRACE,
412                                "ldbm_back_modrdn: removing old_rdn_val=%s\n",
413                                old_rdn_val, 0, 0 );
414                 }
415         
416         /* check for abandon */
417         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
418         if ( op->o_abandon ) {
419                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
420                 goto return_results;
421         }
422         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
423
424         /* delete old one */
425         if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
426                 send_ldap_result( conn, op, LDAP_OTHER,
427                         NULL, "DN index delete fail", NULL, NULL );
428                 goto return_results;
429         }
430
431         (void) cache_delete_entry( &li->li_cache, e );
432
433         /* XXX: there is no going back! */
434
435         free( e->e_dn );
436         free( e->e_ndn );
437         e->e_dn = new_dn;
438         e->e_ndn = new_ndn;
439         new_dn = NULL;
440         new_ndn = NULL;
441
442         /* add new one */
443         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
444                 send_ldap_result( conn, op, LDAP_OTHER,
445                         NULL, "DN index add failed", NULL, NULL );
446                 goto return_results;
447         }
448
449         /* modify memory copy of entry */
450         rc = ldbm_modify_internal( be, conn, op, dn, &mod[0], e );
451
452         if( rc != LDAP_SUCCESS ) {
453                 if( rc != SLAPD_ABANDON ) {
454                         send_ldap_result( conn, op, rc,
455                                 NULL, NULL, NULL, NULL );
456                 }
457             
458             goto return_results;
459         }
460         
461         (void) cache_update_entry( &li->li_cache, e );
462
463         /* NOTE: after this you must not free new_dn or new_ndn!
464          * They are used by cache.
465          */
466
467         /* id2entry index */
468         if ( id2entry_add( be, e ) != 0 ) {
469                 entry_free( e );
470                 send_ldap_result( conn, op, LDAP_OTHER,
471                         NULL, "entry update failed", NULL, NULL );
472                 goto return_results;
473         }
474
475         send_ldap_result( conn, op, LDAP_SUCCESS,
476                 NULL, NULL, NULL, NULL );
477         rc = 0;
478
479 return_results:
480         if( new_dn != NULL ) free( new_dn );
481         if( new_ndn != NULL ) free( new_ndn );
482
483         if( p_dn != NULL ) free( p_dn );
484         if( p_ndn != NULL ) free( p_ndn );
485
486         /* LDAP v2 supporting correct attribute handling. */
487         if( new_rdn_type != NULL ) free(new_rdn_type);
488         if( new_rdn_val != NULL ) free(new_rdn_val);
489         if( old_rdn != NULL ) free(old_rdn);
490         if( old_rdn_type != NULL ) free(old_rdn_type);
491         if( old_rdn_val != NULL ) free(old_rdn_val);
492
493
494         /* LDAP v3 Support */
495         if ( np_dn != NULL ) free( np_dn );
496         if ( np_ndn != NULL ) free( np_ndn );
497
498         if( np != NULL ) {
499                 /* free new parent and writer lock */
500                 cache_return_entry_w( &li->li_cache, np );
501         }
502
503         if( p != NULL ) {
504                 /* free parent and writer lock */
505                 cache_return_entry_w( &li->li_cache, p );
506         }
507
508         if ( rootlock ) {
509                 /* release root writer lock */
510                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
511         }
512
513         /* free entry and writer lock */
514         cache_return_entry_w( &li->li_cache, e );
515         return( rc );
516 }