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