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