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