]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
ITS#2117: remove suffixalias support until someone fixes it
[openldap] / servers / slapd / modrdn.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1995 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18 /*
19  * LDAP v3 newSuperior support.
20  *
21  * Copyright 1999, Juan C. Gomez, All rights reserved.
22  * This software is not subject to any license of Silicon Graphics 
23  * Inc. or Purdue University.
24  *
25  * Redistribution and use in source and binary forms are permitted
26  * without restriction or fee of any kind as long as this notice
27  * is preserved.
28  *
29  */
30
31 #include "portable.h"
32
33 #include <stdio.h>
34
35 #include <ac/socket.h>
36 #include <ac/string.h>
37
38 #include "ldap_pvt.h"
39 #include "slap.h"
40 #include "slapi.h"
41
42 int
43 do_modrdn(
44     Connection  *conn,
45     Operation   *op
46 )
47 {
48         struct berval dn = { 0, NULL };
49         struct berval newrdn = { 0, NULL };
50         struct berval newSuperior = { 0, NULL };
51         ber_int_t       deloldrdn;
52
53         struct berval pdn = { 0, NULL };
54         struct berval pnewrdn = { 0, NULL };
55         struct berval pnewSuperior = { 0, NULL }, *pnewS = NULL;
56
57         struct berval ndn = { 0, NULL };
58         struct berval nnewrdn = { 0, NULL };
59         struct berval nnewSuperior = { 0, NULL }, *nnewS = NULL;
60
61         Backend *be;
62         Backend *newSuperior_be = NULL;
63         ber_len_t       length;
64         int rc;
65         const char *text;
66         int manageDSAit;
67
68 #ifdef LDAP_SLAPI
69         Slapi_PBlock *pb = op->o_pb;
70 #endif
71
72 #ifdef NEW_LOGGING
73         LDAP_LOG( OPERATION, ENTRY, "do_modrdn: begin\n", 0, 0, 0 );
74 #else
75         Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
76 #endif
77
78
79         /*
80          * Parse the modrdn request.  It looks like this:
81          *
82          *      ModifyRDNRequest := SEQUENCE {
83          *              entry   DistinguishedName,
84          *              newrdn  RelativeDistinguishedName
85          *              deleteoldrdn    BOOLEAN,
86          *              newSuperior     [0] LDAPDN OPTIONAL (v3 Only!)
87          *      }
88          */
89
90         if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
91             == LBER_ERROR )
92         {
93 #ifdef NEW_LOGGING
94                 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
95 #else
96                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
97 #endif
98
99                 send_ldap_disconnect( conn, op,
100                         LDAP_PROTOCOL_ERROR, "decoding error" );
101                 return SLAPD_DISCONNECT;
102         }
103
104         /* Check for newSuperior parameter, if present scan it */
105
106         if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
107                 if ( op->o_protocol < LDAP_VERSION3 ) {
108                         /* Conection record indicates v2 but field 
109                          * newSuperior is present: report error.
110                          */
111 #ifdef NEW_LOGGING
112                         LDAP_LOG( OPERATION, ERR,
113                                 "do_modrdn: (v2) invalid field newSuperior.\n", 0, 0, 0 );
114 #else
115                         Debug( LDAP_DEBUG_ANY,
116                             "modrdn(v2): invalid field newSuperior!\n",
117                             0, 0, 0 );
118 #endif
119
120                         send_ldap_disconnect( conn, op,
121                                 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
122                         rc = SLAPD_DISCONNECT;
123                         goto cleanup;
124                 }
125
126                 if ( ber_scanf( op->o_ber, "m", &newSuperior ) 
127                      == LBER_ERROR ) {
128
129 #ifdef NEW_LOGGING
130                         LDAP_LOG( OPERATION, ERR,
131                                 "do_modrdn: ber_scanf(\"m\") failed\n", 0, 0, 0 );
132 #else
133                         Debug( LDAP_DEBUG_ANY, "ber_scanf(\"m\") failed\n",
134                                 0, 0, 0 );
135 #endif
136
137                         send_ldap_disconnect( conn, op,
138                                 LDAP_PROTOCOL_ERROR, "decoding error" );
139                         rc = SLAPD_DISCONNECT;
140                         goto cleanup;
141                 }
142                 pnewS = &pnewSuperior;
143                 nnewS = &nnewSuperior;
144         }
145
146 #ifdef NEW_LOGGING
147         LDAP_LOG( OPERATION, ARGS, 
148                 "do_modrdn: dn (%s) newrdn (%s) newsuperior(%s)\n",
149                 dn.bv_val, newrdn.bv_val,
150                 newSuperior.bv_len ? newSuperior.bv_val : "" );
151 #else
152         Debug( LDAP_DEBUG_ARGS,
153             "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
154                 dn.bv_val, newrdn.bv_val,
155                 newSuperior.bv_len ? newSuperior.bv_val : "" );
156 #endif
157
158         if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
159 #ifdef NEW_LOGGING
160                 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
161 #else
162                 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
163 #endif
164
165                 send_ldap_disconnect( conn, op,
166                         LDAP_PROTOCOL_ERROR, "decoding error" );
167                 rc = SLAPD_DISCONNECT;
168                 goto cleanup;
169         }
170
171         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
172 #ifdef NEW_LOGGING
173                 LDAP_LOG( OPERATION, ERR, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
174 #else
175                 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
176 #endif
177
178                 /* get_ctrls has sent results.  Now clean up. */
179                 goto cleanup;
180         } 
181
182         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
183         if( rc != LDAP_SUCCESS ) {
184 #ifdef NEW_LOGGING
185                 LDAP_LOG( OPERATION, INFO, 
186                         "do_modrdn: conn %d  invalid dn (%s)\n",
187                         conn->c_connid, dn.bv_val, 0 );
188 #else
189                 Debug( LDAP_DEBUG_ANY,
190                         "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
191 #endif
192                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
193                     "invalid DN", NULL, NULL );
194                 goto cleanup;
195         }
196
197         if( ndn.bv_len == 0 ) {
198 #ifdef NEW_LOGGING
199                 LDAP_LOG( OPERATION, ERR,
200                         "do_modrdn:  attempt to modify root DSE.\n", 0, 0, 0 );
201 #else
202                 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
203 #endif
204
205                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
206                         NULL, "cannot rename the root DSE", NULL, NULL );
207                 goto cleanup;
208
209         } else if ( bvmatch( &ndn, &global_schemandn ) ) {
210 #ifdef NEW_LOGGING
211                 LDAP_LOG( OPERATION, ERR,
212                         "do_modrdn: attempt to modify subschema subentry: %s (%ld)\n",
213                         global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
214 #else
215                 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry: %s (%ld)\n",
216                         global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
217 #endif
218
219                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
220                         NULL, "cannot rename subschema subentry", NULL, NULL );
221                 goto cleanup;
222         }
223
224         /* FIXME: should have/use rdnPretty / rdnNormalize routines */
225
226         rc = dnPrettyNormal( NULL, &newrdn, &pnewrdn, &nnewrdn );
227         if( rc != LDAP_SUCCESS ) {
228 #ifdef NEW_LOGGING
229                 LDAP_LOG( OPERATION, INFO, 
230                         "do_modrdn: conn %d  invalid newrdn (%s)\n",
231                         conn->c_connid, newrdn.bv_val, 0 );
232 #else
233                 Debug( LDAP_DEBUG_ANY,
234                         "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
235 #endif
236                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
237                     "invalid new RDN", NULL, NULL );
238                 goto cleanup;
239         }
240
241         if( rdnValidate( &pnewrdn ) != LDAP_SUCCESS ) {
242 #ifdef NEW_LOGGING
243                 LDAP_LOG( OPERATION, ERR, 
244                         "do_modrdn: invalid rdn (%s).\n", pnewrdn.bv_val, 0, 0 );
245 #else
246                 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
247                         pnewrdn.bv_val, 0, 0 );
248 #endif
249
250                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
251                     "invalid new RDN", NULL, NULL );
252                 goto cleanup;
253         }
254
255         if( pnewS ) {
256                 rc = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
257                         &nnewSuperior );
258                 if( rc != LDAP_SUCCESS ) {
259 #ifdef NEW_LOGGING
260                         LDAP_LOG( OPERATION, INFO, 
261                                 "do_modrdn: conn %d  invalid newSuperior (%s)\n",
262                                 conn->c_connid, newSuperior.bv_val, 0 );
263 #else
264                         Debug( LDAP_DEBUG_ANY,
265                                 "do_modrdn: invalid newSuperior (%s)\n",
266                                 newSuperior.bv_val, 0, 0 );
267 #endif
268                         send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
269                                 "invalid newSuperior", NULL, NULL );
270                         goto cleanup;
271                 }
272         }
273
274         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MODRDN dn=\"%s\"\n",
275             op->o_connid, op->o_opid, pdn.bv_val, 0, 0 );
276
277         manageDSAit = get_manageDSAit( op );
278
279         /*
280          * We could be serving multiple database backends.  Select the
281          * appropriate one, or send a referral to our "referral server"
282          * if we don't hold it.
283          */
284         if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
285                 BerVarray ref = referral_rewrite( default_referral,
286                         NULL, &pdn, LDAP_SCOPE_DEFAULT );
287
288                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
289                         NULL, NULL, ref ? ref : default_referral, NULL );
290
291                 ber_bvarray_free( ref );
292                 goto cleanup;
293         }
294
295         /* check restrictions */
296         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
297         if( rc != LDAP_SUCCESS ) {
298                 send_ldap_result( conn, op, rc,
299                         NULL, text, NULL, NULL );
300                 goto cleanup;
301         }
302
303         /* check for referrals */
304         rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
305         if ( rc != LDAP_SUCCESS ) {
306                 goto cleanup;
307         }
308
309         /* Make sure that the entry being changed and the newSuperior are in 
310          * the same backend, otherwise we return an error.
311          */
312         if( pnewS ) {
313                 newSuperior_be = select_backend( &nnewSuperior, 0, 0 );
314
315                 if ( newSuperior_be != be ) {
316                         /* newSuperior is in same backend */
317                         rc = LDAP_AFFECTS_MULTIPLE_DSAS;
318
319                         send_ldap_result( conn, op, rc,
320                                 NULL, "cannot rename between DSAa", NULL, NULL );
321
322                         goto cleanup;
323                 }
324         }
325
326 #if defined( LDAP_SLAPI )
327         slapi_x_backend_set_pb( pb, be );
328         slapi_x_connection_set_pb( pb, conn );
329         slapi_x_operation_set_pb( pb, op );
330         slapi_pblock_set( pb, SLAPI_MODRDN_TARGET, (void *)dn.bv_val );
331         slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN, (void *)newrdn.bv_val );
332         slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR,
333                         (void *)newSuperior.bv_val );
334         slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN, (void *)deloldrdn );
335         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
336
337         rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_MODRDN_FN, pb );
338         if ( rc != 0 ) {
339                 /*
340                  * A preoperation plugin failure will abort the
341                  * entire operation.
342                  */
343 #ifdef NEW_LOGGING
344                 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn preoperation plugin "
345                                 "failed\n", 0, 0, 0 );
346 #else
347                 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn preoperation plugin "
348                                 "failed.\n", 0, 0, 0);
349 #endif
350                 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0)
351                         rc = LDAP_OTHER;
352                 goto cleanup;
353         }
354 #endif /* defined( LDAP_SLAPI ) */
355
356         /*
357          * do the add if 1 && (2 || 3)
358          * 1) there is an add function implemented in this backend;
359          * 2) this backend is master for what it holds;
360          * 3) it's a replica and the dn supplied is the update_ndn.
361          */
362         if ( be->be_modrdn ) {
363                 /* do the update here */
364                 int repl_user = be_isupdate( be, &op->o_ndn );
365 #ifndef SLAPD_MULTIMASTER
366                 if ( !be->be_update_ndn.bv_len || repl_user )
367 #endif
368                 {
369                         if ( (*be->be_modrdn)( be, conn, op, &pdn, &ndn,
370                                 &pnewrdn, &nnewrdn, deloldrdn,
371                                 pnewS, nnewS ) == 0
372 #ifdef SLAPD_MULTIMASTER
373                                 && ( !be->be_update_ndn.bv_len || !repl_user )
374 #endif
375                         ) {
376                                 struct slap_replog_moddn moddn;
377                                 moddn.newrdn = &pnewrdn;
378                                 moddn.deloldrdn = deloldrdn;
379                                 moddn.newsup = &pnewSuperior;
380
381                                 replog( be, op, &pdn, &ndn, &moddn );
382                         }
383 #ifndef SLAPD_MULTIMASTER
384                 } else {
385                         BerVarray defref = be->be_update_refs
386                                 ? be->be_update_refs : default_referral;
387                         BerVarray ref = referral_rewrite( defref,
388                                 NULL, &pdn, LDAP_SCOPE_DEFAULT );
389
390                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
391                                 ref ? ref : defref, NULL );
392
393                         ber_bvarray_free( ref );
394 #endif
395                 }
396         } else {
397                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
398                         NULL, "operation not supported within namingContext",
399                         NULL, NULL );
400         }
401
402 #if defined( LDAP_SLAPI )
403         if ( doPluginFNs( be, SLAPI_PLUGIN_POST_MODRDN_FN, pb ) != 0 ) {
404 #ifdef NEW_LOGGING
405                 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn postoperation plugins "
406                                 "failed\n", 0, 0, 0 );
407 #else
408                 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn postoperation plugins "
409                                 "failed.\n", 0, 0, 0);
410 #endif
411         }
412 #endif /* defined( LDAP_SLAPI ) */
413
414 cleanup:
415         free( pdn.bv_val );
416         free( ndn.bv_val );
417
418         free( pnewrdn.bv_val ); 
419         free( nnewrdn.bv_val ); 
420
421         if ( pnewSuperior.bv_val ) free( pnewSuperior.bv_val );
422         if ( nnewSuperior.bv_val ) free( nnewSuperior.bv_val );
423
424         return rc;
425 }
426
427 int
428 slap_modrdn2mods(
429         Backend         *be,
430         Connection      *conn,
431         Operation       *op,
432         Entry           *e,
433         LDAPRDN         *old_rdn,
434         LDAPRDN         *new_rdn,
435         int             deleteoldrdn,
436         Modifications   **pmod )
437 {
438         int             rc = LDAP_SUCCESS;
439         const char      *text;
440         Modifications   *mod = NULL;
441         int             a_cnt, d_cnt;
442
443         assert( new_rdn != NULL );
444         assert( !deleteoldrdn || old_rdn != NULL );
445
446         /* Add new attribute values to the entry */
447         for ( a_cnt = 0; new_rdn[ 0 ][ a_cnt ]; a_cnt++ ) {
448                 AttributeDescription    *desc = NULL;
449                 Modifications           *mod_tmp;
450
451                 rc = slap_bv2ad( &new_rdn[ 0 ][ a_cnt ]->la_attr, 
452                                 &desc, &text );
453
454                 if ( rc != LDAP_SUCCESS ) {
455 #ifdef NEW_LOGGING
456                         LDAP_LOG ( OPERATION, ERR, 
457                                 "slap_modrdn2modlist: %s: %s (new)\n", 
458                                 text, 
459                                 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0 );
460 #else
461                         Debug( LDAP_DEBUG_TRACE,
462                                 "slap_modrdn2modlist: %s: %s (new)\n",
463                                 text, 
464                                 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0 );
465 #endif
466                         goto done;              
467                 }
468
469                 /* ACL check of newly added attrs */
470                 if ( be && !access_allowed( be, conn, op, e, desc,
471                         &new_rdn[ 0 ][ a_cnt ]->la_value, ACL_WRITE, NULL ) ) {
472 #ifdef NEW_LOGGING
473                         LDAP_LOG ( OPERATION, ERR, 
474                                 "slap_modrdn2modlist: access to attr \"%s\" "
475                                 "(new) not allowed\n", 
476                                 new_rdn[ 0 ][a_cnt]->la_attr.bv_val, 0, 0 );
477 #else
478                         Debug( LDAP_DEBUG_TRACE,
479                                 "slap_modrdn2modlist: access to attr \"%s\" "
480                                 "(new) not allowed\n", 
481                                 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0, 0 );
482 #endif
483                         rc = LDAP_INSUFFICIENT_ACCESS;
484                         goto done;
485                 }
486
487                 /* Apply modification */
488                 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
489                         + 2 * sizeof( struct berval ) );
490                 mod_tmp->sml_desc = desc;
491                 mod_tmp->sml_bvalues = ( BerVarray )( mod_tmp + 1 );
492                 mod_tmp->sml_bvalues[ 0 ] = new_rdn[ 0 ][ a_cnt ]->la_value;
493                 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
494                 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
495                 mod_tmp->sml_next = mod;
496                 mod = mod_tmp;
497         }
498
499         /* Remove old rdn value if required */
500         if ( deleteoldrdn ) {
501                 for ( d_cnt = 0; old_rdn[ 0 ][ d_cnt ]; d_cnt++ ) {
502                         AttributeDescription    *desc = NULL;
503                         Modifications           *mod_tmp;
504
505                         rc = slap_bv2ad( &old_rdn[ 0 ][ d_cnt ]->la_attr,
506                                         &desc, &text );
507
508                         if ( rc != LDAP_SUCCESS ) {
509 #ifdef NEW_LOGGING
510                                 LDAP_LOG ( OPERATION, ERR, 
511                                         "slap_modrdn2modlist: %s: %s (old)\n", 
512                                         text, 
513                                         old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val, 
514                                         0 );
515 #else
516                                 Debug( LDAP_DEBUG_TRACE,
517                                         "slap_modrdn2modlist: %s: %s (old)\n",
518                                         text, 
519                                         old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val, 
520                                         0 );
521 #endif
522                                 goto done;              
523                         }
524
525                         /* ACL check of newly added attrs */
526                         if ( be && !access_allowed( be, conn, op, e, desc,
527                                 &old_rdn[ 0 ][ d_cnt ]->la_value, ACL_WRITE, 
528                                 NULL ) ) {
529 #ifdef NEW_LOGGING
530                                 LDAP_LOG ( OPERATION, ERR, 
531                                         "slap_modrdn2modlist: access "
532                                         "to attr \"%s\" (old) not allowed\n", 
533                                         old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val, 
534                                         0, 0 );
535 #else
536                                 Debug( LDAP_DEBUG_TRACE,
537                                         "slap_modrdn2modlist: access "
538                                         "to attr \"%s\" (old) not allowed\n", 
539                                         old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
540                                         0, 0 );
541 #endif
542                                 rc = LDAP_INSUFFICIENT_ACCESS;
543                                 goto done;
544                         }
545
546                         /* Apply modification */
547                         mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
548                                 + 2 * sizeof ( struct berval ) );
549                         mod_tmp->sml_desc = desc;
550                         mod_tmp->sml_bvalues = ( BerVarray )(mod_tmp+1);
551                         mod_tmp->sml_bvalues[ 0 ] 
552                                 = old_rdn[ 0 ][ d_cnt ]->la_value;
553                         mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
554                         mod_tmp->sml_op = LDAP_MOD_DELETE;
555                         mod_tmp->sml_next = mod;
556                         mod = mod_tmp;
557                 }
558         }
559         
560 done:
561         /* LDAP v2 supporting correct attribute handling. */
562         if ( rc != LDAP_SUCCESS && mod != NULL ) {
563                 Modifications *tmp;
564                 for ( ; mod; mod = tmp ) {
565                         tmp = mod->sml_next;
566                         ch_free( mod );
567                 }
568         }
569
570         *pmod = mod;
571
572         return rc;
573 }