]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modrdn.c
Apply devel IDL fixes...
[openldap] / servers / slapd / back-ldbm / modrdn.c
1 /* modrdn.c - ldbm backend modrdn routine */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9
10 #include "slap.h"
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
13
14 int
15 ldbm_back_modrdn(
16     Backend     *be,
17     Connection  *conn,
18     Operation   *op,
19     char        *dn,
20     char        *newrdn,
21     int         deleteoldrdn
22 )
23 {
24         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
25         char            *matched = NULL;
26         char            *p_dn = NULL, *p_ndn = NULL;
27         char            *new_dn = NULL, *new_ndn = NULL;
28         char            sep[2];
29         Entry           *e, *p = NULL;
30         int             rootlock = 0;
31         int             rc = -1;
32
33         char            *new_rdn_val = NULL;    /* Val of new rdn */
34         char            *new_rdn_type = NULL;   /* Type of new rdn */
35         char            *old_rdn;               /* Old rdn's attr type & val */
36         char            *old_rdn_type = NULL;   /* Type of old rdn attr. */
37         char            *old_rdn_val = NULL;    /* Old rdn attribute value */
38         struct berval   add_bv;                 /* Stores new rdn att */
39         struct berval   *add_bvals[2];          /* Stores new rdn att */
40         struct berval   del_bv;                 /* Stores old rdn att */
41         struct berval   *del_bvals[2];          /* Stores old rdn att */
42         LDAPMod         mod[2];                 /* Used to delete old rdn */
43
44         Debug( LDAP_DEBUG_TRACE,
45                "ldbm_back_modrdn:()==>\n",
46                0, 0, 0 );
47
48         /* get entry with writer lock */
49         if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
50                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, "" );
51                 if ( matched != NULL ) {
52                         free( matched );
53                 }
54                 return( -1 );
55         }
56
57 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
58                 /* check parent for "children" acl */
59         if ( ! access_allowed( be, conn, op, e,
60                 "entry", NULL, ACL_WRITE ) )
61         {
62                 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
63                         0, 0 );
64                 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
65                         "", "" );
66                 goto return_results;
67         }
68 #endif
69
70         if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
71                 /* parent + rdn + separator(s) + null */
72                 if( (p = dn2entry_w( be, p_ndn, &matched )) == NULL) {
73                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
74                                 0, 0, 0);
75                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
76                                 "", "");
77                         goto return_results;
78                 }
79
80 #ifndef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
81                 /* check parent for "children" acl */
82                 if ( ! access_allowed( be, conn, op, p,
83                         "children", NULL, ACL_WRITE ) )
84                 {
85                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
86                                 0, 0 );
87                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
88                                 "", "" );
89                         goto return_results;
90                 }
91 #endif
92
93                 p_dn = dn_parent( be, e->e_dn );
94
95
96         } else {
97                 /* no parent, modrdn entry directly under root */
98                 if( ! be_isroot( be, op->o_ndn ) ) {
99                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
100                                 0, 0, 0);
101                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
102                                 "", "");
103                         goto return_results;
104                 }
105
106                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
107                 rootlock = 1;
108
109
110         }
111
112         build_new_dn( &new_dn, e->e_dn, p_dn, newrdn ); 
113         new_ndn = dn_normalize_case( ch_strdup( new_dn ) );
114
115         /* Get attribute type and attribute value of our new rdn, we will
116          * need to add that to our new entry
117          */
118
119         if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
120             
121                 Debug( LDAP_DEBUG_TRACE,
122                        "ldbm_back_modrdn: can't figure out type of newrdn\n",
123                        0, 0, 0 );
124                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
125                 goto return_results;            
126
127         }
128
129         if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
130             
131                 Debug( LDAP_DEBUG_TRACE,
132                        "ldbm_back_modrdn: can't figure out val of newrdn\n",
133                        0, 0, 0 );
134                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
135                 goto return_results;            
136
137         }
138
139         Debug( LDAP_DEBUG_TRACE,
140                "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
141                new_rdn_val, new_rdn_type, 0 );
142
143         /* Retrieve the old rdn from the entry's dn */
144
145         if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
146
147                 Debug( LDAP_DEBUG_TRACE,
148                        "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
149                        0, 0, 0 );
150                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
151                 goto return_results;            
152
153         }
154
155         if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
156             
157                 Debug( LDAP_DEBUG_TRACE,
158                        "ldbm_back_modrdn: can't figure out the old_rdn type\n",
159                        0, 0, 0 );
160                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
161                 goto return_results;            
162                 
163         }
164         
165         if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
166
167             /* Not a big deal but we may say something */
168             Debug( LDAP_DEBUG_TRACE,
169                    "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
170                    old_rdn_type, new_rdn_type, 0 );
171             
172         }               
173
174         if ( dn_type( old_rdn ) == DN_X500 ) {
175
176                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
177                        0, 0, 0 );
178                 
179                 /* Add new attribute value to the entry.
180                  */
181
182                 add_bvals[0] = &add_bv;         /* Array of bervals */
183                 add_bvals[1] = NULL;
184
185                 add_bv.bv_val = new_rdn_val;
186                 add_bv.bv_len = strlen(new_rdn_val);
187                 
188                 mod[0].mod_type = new_rdn_type; 
189                 mod[0].mod_bvalues = add_bvals;
190                 mod[0].mod_op = LDAP_MOD_SOFTADD;
191                 mod[0].mod_next = NULL;
192                 
193                 Debug( LDAP_DEBUG_TRACE,
194                        "ldbm_back_modrdn: adding new rdn attr val =\"%s\"\n",
195                        new_rdn_val, 0, 0 );
196
197                 /* Remove old rdn value if required */
198
199                 if (deleteoldrdn) {
200
201                         del_bvals[0] = &del_bv;         /* Array of bervals */
202                         del_bvals[1] = NULL;
203                         /* Get value of old rdn */
204         
205                         if ((old_rdn_val = rdn_attr_value( old_rdn ))
206                             == NULL) {
207                             
208                                 Debug( LDAP_DEBUG_TRACE,
209                                        "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
210                                        0, 0, 0 );
211                                 send_ldap_result( conn, op,
212                                                   LDAP_OPERATIONS_ERROR,
213                                                   "", "" );
214                                 goto return_results;            
215
216
217                         }
218
219                         /* Remove old value of rdn as an attribute. */
220                     
221                         del_bv.bv_val = old_rdn_val;
222                         del_bv.bv_len = strlen(old_rdn_val);
223
224                         /* No need to normalize old_rdn_type, delete_values()
225                          * does that for us
226                          */
227                         mod[0].mod_next = &mod[1];
228                         mod[1].mod_type = old_rdn_type; 
229                         mod[1].mod_bvalues = del_bvals;
230                         mod[1].mod_op = LDAP_MOD_DELETE;
231                         mod[1].mod_next = NULL;
232
233                 }/* if (deleteoldrdn) */
234
235                 /* modify memory copy of entry */
236                 if ( ldbm_internal_modify( be, conn, op, dn, &mod[0], e )
237                      != 0 ) {
238                     
239                         goto return_results;
240                         
241                 }
242         
243         } else {
244             
245
246                 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
247                        0, 0, 0 );
248                 /* XXXV3: not sure of what to do here */
249                 Debug( LDAP_DEBUG_TRACE,
250                        "ldbm_back_modrdn: not fully implemented...\n",
251                        0, 0, 0 );  
252                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
253                 goto return_results;
254
255         }
256
257         if ( (dn2id ( be, new_ndn ) ) != NOID ) {
258                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, NULL, NULL );
259                 goto return_results;
260         }
261
262         /* check for abandon */
263         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
264         if ( op->o_abandon ) {
265                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
266                 goto return_results;
267         }
268         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
269
270         /* add new one */
271         if ( dn2id_add( be, new_ndn, e->e_id ) != 0 ) {
272                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
273                 goto return_results;
274         }
275
276         /* delete old one */
277         if ( dn2id_delete( be, e->e_ndn ) != 0 ) {
278                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
279                 goto return_results;
280         }
281
282
283         (void) cache_delete_entry( &li->li_cache, e );
284         free( e->e_dn );
285         free( e->e_ndn );
286         e->e_dn = new_dn;
287         e->e_ndn = new_ndn;
288         (void) cache_update_entry( &li->li_cache, e );
289
290         /* id2entry index: commit */
291         if ( id2entry_add( be, e ) != 0 ) {
292                 entry_free( e );
293                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
294                 goto return_results_new;
295         }
296
297         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
298         rc = 0;
299
300         Debug( LDAP_DEBUG_TRACE,
301                "ldbm_back_modrdn:()<==\n",
302                0, 0, 0 );
303         goto return_results_new;
304
305 return_results: 
306         if( new_dn != NULL ) free( new_dn );
307         if( new_ndn != NULL ) free( new_ndn );
308 return_results_new:
309         /* NOTE:
310          * new_dn and new_ndn are not deallocated because they are used by
311          * the cache entry at this point.
312          */
313         if( p_dn != NULL ) free( p_dn );
314         if( p_ndn != NULL ) free( p_ndn );
315
316         if( matched != NULL ) free( matched );
317
318         /* LDAP v2 supporting correct attribute handling. */
319         if( new_rdn_type != NULL ) free(new_rdn_type);
320         if( new_rdn_val != NULL ) free(new_rdn_val);
321         if( old_rdn != NULL ) free(old_rdn);
322         if( old_rdn_type != NULL ) free(old_rdn_type);
323         if( old_rdn_val != NULL ) free(old_rdn_val);
324
325         if( p != NULL ) {
326                 /* free parent and writer lock */
327                 cache_return_entry_w( &li->li_cache, p );
328         }
329
330         if ( rootlock ) {
331                 /* release root writer lock */
332                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
333         }
334
335         /* free entry and writer lock */
336         cache_return_entry_w( &li->li_cache, e );
337         return( rc );
338 }