]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
Minor adjustments to better handle NT vs UNIX.
[openldap] / servers / slapd / back-ldbm / modrdn.c
1 /* modrdn.c - ldbm backend modrdn routine */
2
3 /*
4  * LDAP v3 newSuperior support. Add new rdn as an attribute.
5  * (Full support for v2 also used software/ideas contributed
6  * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
7  * submission!.)
8  *
9  * Copyright 1999, Juan C. Gomez, All rights reserved.
10  * This software is not subject to any license of Silicon Graphics 
11  * Inc. or Purdue University.
12  *
13  * Redistribution and use in source and binary forms are permitted
14  * without restriction or fee of any kind as long as this notice
15  * is preserved.
16  *
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22
23 #include <ac/string.h>
24 #include <ac/socket.h>
25
26 #include "slap.h"
27 #include "back-ldbm.h"
28 #include "proto-back-ldbm.h"
29
30 int
31 ldbm_back_modrdn(
32     Backend     *be,
33     Connection  *conn,
34     Operation   *op,
35     char        *dn,
36     char        *newrdn,
37     int         deleteoldrdn,
38     char        *newSuperior
39 )
40 {
41         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
42         char            *matched = NULL;
43         char            *p_dn = NULL, *p_ndn = NULL;
44         char            *new_dn = NULL, *new_ndn = NULL;
45         Entry           *e, *p = NULL;
46         int                     rootlock = 0;
47         int                     rc = -1;
48         /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
49         char            *new_rdn_val = NULL;    /* Val of new rdn */
50         char            *new_rdn_type = NULL;   /* Type of new rdn */
51         char            *old_rdn;               /* Old rdn's attr type & val */
52         char            *old_rdn_type = NULL;   /* Type of old rdn attr. */
53         char            *old_rdn_val = NULL;    /* Old rdn attribute value */
54         /* Added to support newSuperior */ 
55         Entry           *np = NULL;     /* newSuperior Entry */
56         char            *np_dn = NULL;  /* newSuperior dn */
57         char            *np_ndn = NULL; /* newSuperior ndn */
58         char            *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
59         /* Used to interface with ldbm_modify_internal() */
60         struct berval   add_bv;                 /* Stores new rdn att */
61         struct berval   *add_bvals[2];          /* Stores new rdn att */
62         struct berval   del_bv;                 /* Stores old rdn att */
63         struct berval   *del_bvals[2];          /* Stores old rdn att */
64         LDAPModList     mod[2];                 /* Used to delete old rdn */
65
66
67         Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
68                (newSuperior ? newSuperior : "NULL"),
69                0, 0 );
70
71         /* get entry with writer lock */
72         if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
73                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, "" );
74                 if ( matched != NULL ) {
75                         free( matched );
76                 }
77                 return( -1 );
78         }
79
80 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
81                 /* check parent for "children" acl */
82         if ( ! access_allowed( be, conn, op, e,
83                 "entry", NULL, ACL_WRITE ) )
84         {
85                 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
86                         0, 0 );
87                 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
88                         "", "" );
89                 goto return_results;
90         }
91 #endif
92
93         if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
94
95                 /* Make sure parent entry exist and we can write its 
96                  * children.
97                  */
98
99                 if( (p = dn2entry_w( be, p_ndn, &matched )) == NULL) {
100                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
101                                 0, 0, 0);
102                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
103                                 "", "");
104                         goto return_results;
105                 }
106
107                 /* check parent for "children" acl */
108                 if ( ! access_allowed( be, conn, op, p,
109                         "children", NULL, ACL_WRITE ) )
110                 {
111                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
112                                 0, 0 );
113                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
114                                 "", "" );
115                         goto return_results;
116                 }
117
118                 Debug( LDAP_DEBUG_TRACE,
119                        "ldbm_back_modrdn: wr to children of entry %s OK\n",
120                        p_ndn, 0, 0 );
121                 
122                 p_dn = dn_parent( be, e->e_dn );
123         
124
125                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
126                        p_dn, 0, 0 );
127
128         } else {
129                 /* no parent, modrdn entry directly under root */
130                 if( ! be_isroot( be, op->o_ndn ) ) {
131                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
132                                 0, 0, 0);
133                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
134                                 "", "");
135                         goto return_results;
136                 }
137
138                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
139                 rootlock = 1;
140                 
141                 Debug( LDAP_DEBUG_TRACE,
142                        "ldbm_back_modrdn: no parent, locked root\n",
143                        0, 0, 0 );
144
145         }/* if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) else */
146
147         new_parent_dn = p_dn;   /* New Parent unless newSuperior given */
148
149         if ( (np_dn = newSuperior) != NULL) {
150
151
152                 Debug( LDAP_DEBUG_TRACE, 
153                        "ldbm_back_modrdn: new parent requested...\n",
154                        0, 0, 0 );
155
156                 np_ndn = dn_normalize_case( ch_strdup( np_dn ) );
157
158                 /* newSuperior == oldParent?, if so ==> ERROR */
159
160                 /* newSuperior == entry being moved?, if so ==> ERROR */
161
162                 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
163
164                 if( (np = dn2entry_w( be, np_ndn, &matched )) == NULL) {
165
166                         Debug( LDAP_DEBUG_TRACE,
167                                "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
168                                np_ndn, 0, 0);
169                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "",
170                                           "");
171                         goto return_results;
172                 }
173
174                 Debug( LDAP_DEBUG_TRACE,
175                        "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
176                        np, np->e_id, 0 );
177             
178                 /* check newSuperior for "children" acl */
179                 if ( !access_allowed( be, conn, op, np, "children", NULL,
180                                       ACL_WRITE ) )
181                 {
182                         Debug( LDAP_DEBUG_TRACE,
183                                "ldbm_back_modrdn: no wr to newSup children\n",
184                                0, 0, 0 );
185                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
186                                           "", "" );
187                         goto return_results;
188                 }
189
190                 Debug( LDAP_DEBUG_TRACE,
191                        "ldbm_back_modrdn: wr to new parent's children OK\n",
192                        0, 0 , 0 );
193
194
195                 new_parent_dn = np_dn;
196
197         }
198         
199         /* Build target dn and make sure target entry doesn't exist already. */
200
201         build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn ); 
202
203
204         new_ndn = dn_normalize_case( ch_strdup(new_dn) );
205
206         Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
207                new_ndn, 0, 0 );
208
209         if (dn2id ( be, new_ndn ) != NOID) {
210                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, NULL, NULL );
211                 goto return_results;
212         }
213
214         Debug( LDAP_DEBUG_TRACE,
215                "ldbm_back_modrdn: new ndn=%s does not exist\n",
216                new_ndn, 0, 0 );
217
218         /* check for abandon */
219         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
220         if ( op->o_abandon ) {
221                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
222                 goto return_results;
223         }
224         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
225
226         /* delete old one */
227         if ( dn2id_delete( be, e->e_ndn ) != 0 ) {
228                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
229                 goto return_results;
230         }
231
232         (void) cache_delete_entry( &li->li_cache, e );
233         free( e->e_dn );
234         free( e->e_ndn );
235         e->e_dn = new_dn;
236         e->e_ndn = new_ndn;
237
238         /* add new one */
239         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
240                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
241                 goto return_results;
242         }
243
244
245         /* Get attribute type and attribute value of our new rdn, we will
246          * need to add that to our new entry
247          */
248
249         if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
250             
251                 Debug( LDAP_DEBUG_TRACE,
252                        "ldbm_back_modrdn: can't figure out type of newrdn\n",
253                        0, 0, 0 );
254                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
255                 goto return_results;            
256
257         }
258
259         if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
260             
261                 Debug( LDAP_DEBUG_TRACE,
262                        "ldbm_back_modrdn: can't figure out val of newrdn\n",
263                        0, 0, 0 );
264                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
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                 goto return_results;            
282
283         }
284
285         if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
286             
287                 Debug( LDAP_DEBUG_TRACE,
288                        "ldbm_back_modrdn: can't figure out the old_rdn type\n",
289                        0, 0, 0 );
290                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
291                 goto return_results;            
292                 
293         }
294         
295         if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
296
297             /* Not a big deal but we may say something */
298             Debug( LDAP_DEBUG_TRACE,
299                    "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
300                    old_rdn_type, new_rdn_type, 0 );
301             
302         }               
303
304         if ( dn_type( old_rdn ) == DN_X500 ) {
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                 /* Remove old rdn value if required */
324
325                 if (deleteoldrdn) {
326
327                         /* Get value of old rdn */
328         
329                         if ((old_rdn_val = rdn_attr_value( old_rdn ))
330                             == NULL) {
331                             
332                                 Debug( LDAP_DEBUG_TRACE,
333                                        "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
334                                        0, 0, 0 );
335                                 send_ldap_result( conn, op,
336                                                   LDAP_OPERATIONS_ERROR,
337                                                   "", "" );
338                                 goto return_results;            
339
340
341                         }
342
343                         del_bvals[0] = &del_bv;         /* Array of bervals */
344                         del_bvals[1] = NULL;
345
346                         /* Remove old value of rdn as an attribute. */
347                     
348                         del_bv.bv_val = old_rdn_val;
349                         del_bv.bv_len = strlen(old_rdn_val);
350
351                         /* No need to normalize old_rdn_type, delete_values()
352                          * does that for us
353                          */
354                         mod[0].ml_next = &mod[1];
355                         mod[1].ml_type = old_rdn_type;  
356                         mod[1].ml_bvalues = del_bvals;
357                         mod[1].ml_op = LDAP_MOD_DELETE;
358                         mod[1].ml_next = NULL;
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         
367         } else {
368             
369
370                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
371                        0, 0, 0 );
372                 /* XXXV3: not sure of what to do here */
373                 Debug( LDAP_DEBUG_TRACE,
374                        "ldbm_back_modrdn: not fully implemented...\n",
375                        0, 0, 0 );
376   
377                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
378                                   NULL );
379                 goto return_results;
380
381         }
382
383         /* modify memory copy of entry */
384         if ( ldbm_modify_internal( be, conn, op, dn, &mod[0], e )
385              != 0 ) {
386             
387             goto return_results;
388             
389         }
390         
391         (void) cache_update_entry( &li->li_cache, e );
392
393         /* NOTE: after this you must not free new_dn or new_ndn!
394          * They are used by cache.
395          */
396
397         /* id2entry index */
398         if ( id2entry_add( be, e ) != 0 ) {
399                 entry_free( e );
400                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
401                 goto return_results_after;
402         }
403
404         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
405         rc = 0;
406         goto return_results_after;      
407
408 return_results:
409         if( new_dn != NULL ) free( new_dn );
410         if( new_ndn != NULL ) free( new_ndn );
411 return_results_after:
412         /* NOTE:
413          * new_dn and new_ndn are not deallocated because they are used by
414          * the cache entry.
415          */
416         if( p_dn != NULL ) free( p_dn );
417         if( p_ndn != NULL ) free( p_ndn );
418
419         if( matched != NULL ) free( matched );
420
421         /* LDAP v2 supporting correct attribute handling. */
422         if( new_rdn_type != NULL ) free(new_rdn_type);
423         if( new_rdn_val != NULL ) free(new_rdn_val);
424         if( old_rdn != NULL ) free(old_rdn);
425         if( old_rdn_type != NULL ) free(old_rdn_type);
426         if( old_rdn_val != NULL ) free(old_rdn_val);
427
428
429         /* LDAP v3 Support */
430         if ( np_dn != NULL ) free( np_dn );
431         if ( np_ndn != NULL ) free( np_ndn );
432
433         if( p != NULL ) {
434                 /* free parent and writer lock */
435                 cache_return_entry_w( &li->li_cache, p );
436         }
437
438         if ( rootlock ) {
439                 /* release root writer lock */
440                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
441         }
442
443         /* free entry and writer lock */
444         cache_return_entry_w( &li->li_cache, e );
445         return( rc );
446 }