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