]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
c31780f58361d36c49f954810032ebd942638e20
[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     char        *dn,
41     char        *ndn,
42     char        *newrdn,
43     int         deleteoldrdn,
44     char        *newSuperior
45 )
46 {
47 #ifdef SLAPD_SCHEMA_NOT_COMPAT
48         static AttributeDescription *children = NULL;
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 ( (np_dn = newSuperior) != NULL) {
194                 Debug( LDAP_DEBUG_TRACE, 
195                        "ldbm_back_modrdn: new parent requested...\n",
196                        0, 0, 0 );
197
198                 np_ndn = ch_strdup( np_dn );
199                 (void) dn_normalize( np_ndn );
200
201                 /* newSuperior == oldParent?, if so ==> ERROR */
202                 /* newSuperior == entry being moved?, if so ==> ERROR */
203                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
204
205                 if( (np = dn2entry_w( be, np_ndn, NULL )) == NULL) {
206                         Debug( LDAP_DEBUG_TRACE,
207                                "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
208                                np_ndn, 0, 0);
209                         send_ldap_result( conn, op, LDAP_OTHER,
210                                 NULL, NULL, NULL, NULL );
211                         goto return_results;
212                 }
213
214                 Debug( LDAP_DEBUG_TRACE,
215                        "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
216                        np, np->e_id, 0 );
217             
218                 /* check newSuperior for "children" acl */
219                 if ( !access_allowed( be, conn, op, np, children, NULL,
220                                       ACL_WRITE ) )
221                 {
222                         Debug( LDAP_DEBUG_TRACE,
223                                "ldbm_back_modrdn: no wr to newSup children\n",
224                                0, 0, 0 );
225                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
226                                 NULL, NULL, NULL, NULL );
227                         goto return_results;
228                 }
229
230                 if ( is_entry_alias( np ) ) {
231                         /* entry is an alias, don't allow bind */
232                         Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
233                             0, 0 );
234
235                         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
236                             NULL, NULL, NULL, NULL );
237
238                         goto return_results;
239                 }
240
241                 if ( is_entry_referral( np ) ) {
242                         /* parent is a referral, don't allow add */
243                         /* parent is an alias, don't allow add */
244                         Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
245                                 0, 0 );
246
247                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
248                             NULL, NULL, NULL, NULL );
249
250                         goto return_results;
251                 }
252
253                 Debug( LDAP_DEBUG_TRACE,
254                        "ldbm_back_modrdn: wr to new parent's children OK\n",
255                        0, 0 , 0 );
256
257                 new_parent_dn = np_dn;
258         }
259         
260         /* Build target dn and make sure target entry doesn't exist already. */
261
262         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
263
264
265         new_ndn = ch_strdup(new_dn);
266         (void) dn_normalize( new_ndn );
267
268         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
269                new_ndn, 0, 0 );
270
271         /* check for abandon */
272         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
273         if ( op->o_abandon ) {
274                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
275                 goto return_results;
276         }
277
278         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
279         if (dn2id ( be, new_ndn ) != NOID) {
280                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
281                         NULL, NULL, NULL, NULL );
282                 goto return_results;
283         }
284
285         Debug( LDAP_DEBUG_TRACE,
286                "ldbm_back_modrdn: new ndn=%s does not exist\n",
287                new_ndn, 0, 0 );
288
289         /* Get attribute type and attribute value of our new rdn, we will
290          * need to add that to our new entry
291          */
292
293         if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
294             
295                 Debug( LDAP_DEBUG_TRACE,
296                        "ldbm_back_modrdn: can't figure out type of newrdn\n",
297                        0, 0, 0 );
298                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
299                         NULL, "unknown type used in RDN", NULL, NULL );
300                 goto return_results;            
301
302         }
303
304         if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
305             
306                 Debug( LDAP_DEBUG_TRACE,
307                        "ldbm_back_modrdn: can't figure out val of newrdn\n",
308                        0, 0, 0 );
309                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
310                         NULL, "could not parse RDN value", NULL, NULL );
311                 goto return_results;            
312
313         }
314
315         Debug( LDAP_DEBUG_TRACE,
316                "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
317                new_rdn_val, new_rdn_type, 0 );
318
319         /* Retrieve the old rdn from the entry's dn */
320
321         if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
322
323                 Debug( LDAP_DEBUG_TRACE,
324                        "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
325                        0, 0, 0 );
326                 send_ldap_result( conn, op, LDAP_OTHER,
327                         NULL, "could not parse old DN", NULL, NULL );
328                 goto return_results;            
329
330         }
331
332         if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
333             
334                 Debug( LDAP_DEBUG_TRACE,
335                        "ldbm_back_modrdn: can't figure out the old_rdn type\n",
336                        0, 0, 0 );
337                 send_ldap_result( conn, op, LDAP_OTHER,
338                         NULL, "count parse RDN from old DN", NULL, NULL );
339                 goto return_results;            
340                 
341         }
342         
343         if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
344
345             /* Not a big deal but we may say something */
346             Debug( LDAP_DEBUG_TRACE,
347                    "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
348                    old_rdn_type, new_rdn_type, 0 );
349             
350         }               
351
352                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
353                        0, 0, 0 );
354                 
355                 /* Add new attribute value to the entry.
356                  */
357
358                 add_bvals[0] = &add_bv;         /* Array of bervals */
359                 add_bvals[1] = NULL;
360
361                 add_bv.bv_val = new_rdn_val;
362                 add_bv.bv_len = strlen(new_rdn_val);
363                 
364 #ifdef SLAPD_SCHEMA_NOT_COMPAT
365                 /* not yet implemented */
366 #else
367                 mod[0].ml_type = new_rdn_type;  
368                 mod[0].ml_bvalues = add_bvals;
369                 mod[0].ml_op = SLAP_MOD_SOFTADD;
370                 mod[0].ml_next = NULL;
371 #endif
372
373                 /* Remove old rdn value if required */
374
375                 if (deleteoldrdn) {
376                         /* Get value of old rdn */
377         
378                         if ((old_rdn_val = rdn_attr_value( old_rdn ))
379                             == NULL) {
380                             
381                                 Debug( LDAP_DEBUG_TRACE,
382                                        "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
383                                        0, 0, 0 );
384                                 send_ldap_result( conn, op, LDAP_OTHER,
385                                         NULL, "cound not parse value from old RDN", NULL, NULL );
386                                 goto return_results;            
387                         }
388
389                         del_bvals[0] = &del_bv;         /* Array of bervals */
390                         del_bvals[1] = NULL;
391
392                         /* Remove old value of rdn as an attribute. */
393                     
394                         del_bv.bv_val = old_rdn_val;
395                         del_bv.bv_len = strlen(old_rdn_val);
396
397 #ifdef SLAPD_SCHEMA_NOT_COMPAT
398                         /* not yet implemented */
399 #else
400                         /* No need to normalize old_rdn_type, delete_values()
401                          * does that for us
402                          */
403                         mod[0].ml_next = &mod[1];
404                         mod[1].ml_type = old_rdn_type;  
405                         mod[1].ml_bvalues = del_bvals;
406                         mod[1].ml_op = LDAP_MOD_DELETE;
407                         mod[1].ml_next = NULL;
408 #endif
409
410                         Debug( LDAP_DEBUG_TRACE,
411                                "ldbm_back_modrdn: removing old_rdn_val=%s\n",
412                                old_rdn_val, 0, 0 );
413                 }
414         
415         /* check for abandon */
416         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
417         if ( op->o_abandon ) {
418                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
419                 goto return_results;
420         }
421         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
422
423         /* delete old one */
424         if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
425                 send_ldap_result( conn, op, LDAP_OTHER,
426                         NULL, "DN index delete fail", NULL, NULL );
427                 goto return_results;
428         }
429
430         (void) cache_delete_entry( &li->li_cache, e );
431
432         /* XXX: there is no going back! */
433
434         free( e->e_dn );
435         free( e->e_ndn );
436         e->e_dn = new_dn;
437         e->e_ndn = new_ndn;
438         new_dn = NULL;
439         new_ndn = NULL;
440
441         /* add new one */
442         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
443                 send_ldap_result( conn, op, LDAP_OTHER,
444                         NULL, "DN index add failed", NULL, NULL );
445                 goto return_results;
446         }
447
448         /* modify memory copy of entry */
449         rc = ldbm_modify_internal( be, conn, op, dn, &mod[0], e );
450
451         if( rc != LDAP_SUCCESS ) {
452                 if( rc != SLAPD_ABANDON ) {
453                         send_ldap_result( conn, op, rc,
454                                 NULL, NULL, NULL, NULL );
455                 }
456             
457             goto return_results;
458         }
459         
460         (void) cache_update_entry( &li->li_cache, e );
461
462         /* NOTE: after this you must not free new_dn or new_ndn!
463          * They are used by cache.
464          */
465
466         /* id2entry index */
467         if ( id2entry_add( be, e ) != 0 ) {
468                 entry_free( e );
469                 send_ldap_result( conn, op, LDAP_OTHER,
470                         NULL, "entry update failed", NULL, NULL );
471                 goto return_results;
472         }
473
474         send_ldap_result( conn, op, LDAP_SUCCESS,
475                 NULL, NULL, NULL, NULL );
476         rc = 0;
477
478 return_results:
479         if( new_dn != NULL ) free( new_dn );
480         if( new_ndn != NULL ) free( new_ndn );
481
482         if( p_dn != NULL ) free( p_dn );
483         if( p_ndn != NULL ) free( p_ndn );
484
485         /* LDAP v2 supporting correct attribute handling. */
486         if( new_rdn_type != NULL ) free(new_rdn_type);
487         if( new_rdn_val != NULL ) free(new_rdn_val);
488         if( old_rdn != NULL ) free(old_rdn);
489         if( old_rdn_type != NULL ) free(old_rdn_type);
490         if( old_rdn_val != NULL ) free(old_rdn_val);
491
492
493         /* LDAP v3 Support */
494         if ( np_dn != NULL ) free( np_dn );
495         if ( np_ndn != NULL ) free( np_ndn );
496
497         if( np != NULL ) {
498                 /* free new parent and writer lock */
499                 cache_return_entry_w( &li->li_cache, np );
500         }
501
502         if( p != NULL ) {
503                 /* free parent and writer lock */
504                 cache_return_entry_w( &li->li_cache, p );
505         }
506
507         if ( rootlock ) {
508                 /* release root writer lock */
509                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
510         }
511
512         /* free entry and writer lock */
513         cache_return_entry_w( &li->li_cache, e );
514         return( rc );
515 }