]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
DNS SRV default referral handling
[openldap] / servers / slapd / modrdn.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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
41 int
42 do_modrdn(
43     Connection  *conn,
44     Operation   *op
45 )
46 {
47         struct berval dn = { 0, NULL };
48         struct berval newrdn = { 0, NULL };
49         struct berval newSuperior = { 0, NULL };
50         ber_int_t       deloldrdn;
51
52         struct berval pdn = { 0, NULL };
53         struct berval pnewrdn = { 0, NULL };
54         struct berval pnewSuperior = { 0, NULL }, *pnewS = NULL;
55
56         struct berval ndn = { 0, NULL };
57         struct berval nnewrdn = { 0, NULL };
58         struct berval nnewSuperior = { 0, NULL }, *nnewS = NULL;
59
60         Backend *be;
61         Backend *newSuperior_be = NULL;
62         ber_len_t       length;
63         int rc;
64         const char *text;
65         int manageDSAit;
66
67 #ifdef NEW_LOGGING
68         LDAP_LOG( OPERATION, ENTRY, "do_modrdn: begin\n", 0, 0, 0 );
69 #else
70         Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
71 #endif
72
73
74         /*
75          * Parse the modrdn request.  It looks like this:
76          *
77          *      ModifyRDNRequest := SEQUENCE {
78          *              entry   DistinguishedName,
79          *              newrdn  RelativeDistinguishedName
80          *              deleteoldrdn    BOOLEAN,
81          *              newSuperior     [0] LDAPDN OPTIONAL (v3 Only!)
82          *      }
83          */
84
85         if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
86             == LBER_ERROR )
87         {
88 #ifdef NEW_LOGGING
89                 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
90 #else
91                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
92 #endif
93
94                 send_ldap_disconnect( conn, op,
95                         LDAP_PROTOCOL_ERROR, "decoding error" );
96                 return SLAPD_DISCONNECT;
97         }
98
99         /* Check for newSuperior parameter, if present scan it */
100
101         if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
102                 if ( op->o_protocol < LDAP_VERSION3 ) {
103                         /* Conection record indicates v2 but field 
104                          * newSuperior is present: report error.
105                          */
106 #ifdef NEW_LOGGING
107                         LDAP_LOG( OPERATION, ERR,
108                                 "do_modrdn: (v2) invalid field newSuperior.\n", 0, 0, 0 );
109 #else
110                         Debug( LDAP_DEBUG_ANY,
111                             "modrdn(v2): invalid field newSuperior!\n",
112                             0, 0, 0 );
113 #endif
114
115                         send_ldap_disconnect( conn, op,
116                                 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
117                         rc = SLAPD_DISCONNECT;
118                         goto cleanup;
119                 }
120
121                 if ( ber_scanf( op->o_ber, "m", &newSuperior ) 
122                      == LBER_ERROR ) {
123
124 #ifdef NEW_LOGGING
125                         LDAP_LOG( OPERATION, ERR,
126                                 "do_modrdn: ber_scanf(\"m\") failed\n", 0, 0, 0 );
127 #else
128                         Debug( LDAP_DEBUG_ANY, "ber_scanf(\"m\") failed\n",
129                                 0, 0, 0 );
130 #endif
131
132                         send_ldap_disconnect( conn, op,
133                                 LDAP_PROTOCOL_ERROR, "decoding error" );
134                         rc = SLAPD_DISCONNECT;
135                         goto cleanup;
136                 }
137                 pnewS = &pnewSuperior;
138                 nnewS = &nnewSuperior;
139         }
140
141 #ifdef NEW_LOGGING
142         LDAP_LOG( OPERATION, ARGS, 
143                 "do_modrdn: dn (%s) newrdn (%s) newsuperior(%s)\n",
144                 dn.bv_val, newrdn.bv_val,
145                 newSuperior.bv_len ? newSuperior.bv_val : "" );
146 #else
147         Debug( LDAP_DEBUG_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 #endif
152
153         if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
154 #ifdef NEW_LOGGING
155                 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
156 #else
157                 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
158 #endif
159
160                 send_ldap_disconnect( conn, op,
161                         LDAP_PROTOCOL_ERROR, "decoding error" );
162                 rc = SLAPD_DISCONNECT;
163                 goto cleanup;
164         }
165
166         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
167 #ifdef NEW_LOGGING
168                 LDAP_LOG( OPERATION, ERR, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
169 #else
170                 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
171 #endif
172
173                 /* get_ctrls has sent results.  Now clean up. */
174                 goto cleanup;
175         } 
176
177         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
178         if( rc != LDAP_SUCCESS ) {
179 #ifdef NEW_LOGGING
180                 LDAP_LOG( OPERATION, INFO, 
181                         "do_modrdn: conn %d  invalid dn (%s)\n",
182                         conn->c_connid, dn.bv_val, 0 );
183 #else
184                 Debug( LDAP_DEBUG_ANY,
185                         "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
186 #endif
187                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
188                     "invalid DN", NULL, NULL );
189                 goto cleanup;
190         }
191
192         if( ndn.bv_len == 0 ) {
193 #ifdef NEW_LOGGING
194                 LDAP_LOG( OPERATION, ERR,
195                         "do_modrdn:  attempt to modify root DSE.\n", 0, 0, 0 );
196 #else
197                 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
198 #endif
199
200                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
201                         NULL, "cannot rename the root DSE", NULL, NULL );
202                 goto cleanup;
203
204         } else if ( bvmatch( &ndn, &global_schemandn ) ) {
205 #ifdef NEW_LOGGING
206                 LDAP_LOG( OPERATION, ERR,
207                         "do_modrdn: attempt to modify subschema subentry: %s (%ld)\n",
208                         global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
209 #else
210                 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry: %s (%ld)\n",
211                         global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
212 #endif
213
214                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
215                         NULL, "cannot rename subschema subentry", NULL, NULL );
216                 goto cleanup;
217         }
218
219         /* FIXME: should have/use rdnPretty / rdnNormalize routines */
220
221         rc = dnPrettyNormal( NULL, &newrdn, &pnewrdn, &nnewrdn );
222         if( rc != LDAP_SUCCESS ) {
223 #ifdef NEW_LOGGING
224                 LDAP_LOG( OPERATION, INFO, 
225                         "do_modrdn: conn %d  invalid newrdn (%s)\n",
226                         conn->c_connid, newrdn.bv_val, 0 );
227 #else
228                 Debug( LDAP_DEBUG_ANY,
229                         "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
230 #endif
231                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
232                     "invalid new RDN", NULL, NULL );
233                 goto cleanup;
234         }
235
236         if( rdnValidate( &pnewrdn ) != LDAP_SUCCESS ) {
237 #ifdef NEW_LOGGING
238                 LDAP_LOG( OPERATION, ERR, 
239                         "do_modrdn: invalid rdn (%s).\n", pnewrdn.bv_val, 0, 0 );
240 #else
241                 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
242                         pnewrdn.bv_val, 0, 0 );
243 #endif
244
245                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
246                     "invalid new RDN", NULL, NULL );
247                 goto cleanup;
248         }
249
250         if( pnewS ) {
251                 rc = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
252                         &nnewSuperior );
253                 if( rc != LDAP_SUCCESS ) {
254 #ifdef NEW_LOGGING
255                         LDAP_LOG( OPERATION, INFO, 
256                                 "do_modrdn: conn %d  invalid newSuperior (%s)\n",
257                                 conn->c_connid, newSuperior.bv_val, 0 );
258 #else
259                         Debug( LDAP_DEBUG_ANY,
260                                 "do_modrdn: invalid newSuperior (%s)\n",
261                                 newSuperior.bv_val, 0, 0 );
262 #endif
263                         send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
264                                 "invalid newSuperior", NULL, NULL );
265                         goto cleanup;
266                 }
267         }
268
269         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MODRDN dn=\"%s\"\n",
270             op->o_connid, op->o_opid, pdn.bv_val, 0, 0 );
271
272         manageDSAit = get_manageDSAit( op );
273
274         /*
275          * We could be serving multiple database backends.  Select the
276          * appropriate one, or send a referral to our "referral server"
277          * if we don't hold it.
278          */
279         if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
280                 BerVarray ref = referral_rewrite( default_referral,
281                         NULL, &pdn, LDAP_SCOPE_DEFAULT );
282
283                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
284                         NULL, NULL, ref ? ref : default_referral, NULL );
285
286                 ber_bvarray_free( ref );
287                 goto cleanup;
288         }
289
290         /* check restrictions */
291         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
292         if( rc != LDAP_SUCCESS ) {
293                 send_ldap_result( conn, op, rc,
294                         NULL, text, NULL, NULL );
295                 goto cleanup;
296         }
297
298         /* check for referrals */
299         rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
300         if ( rc != LDAP_SUCCESS ) {
301                 goto cleanup;
302         }
303
304         /* Make sure that the entry being changed and the newSuperior are in 
305          * the same backend, otherwise we return an error.
306          */
307         if( pnewS ) {
308                 newSuperior_be = select_backend( &nnewSuperior, 0, 0 );
309
310                 if ( newSuperior_be != be ) {
311                         /* newSuperior is in same backend */
312                         rc = LDAP_AFFECTS_MULTIPLE_DSAS;
313
314                         send_ldap_result( conn, op, rc,
315                                 NULL, "cannot rename between DSAa", NULL, NULL );
316
317                         goto cleanup;
318                 }
319
320                 /* deref suffix alias if appropriate */
321                 suffix_alias( be, &nnewSuperior );
322         }
323
324         /* deref suffix alias if appropriate */
325         suffix_alias( be, &ndn );
326
327         /*
328          * do the add if 1 && (2 || 3)
329          * 1) there is an add function implemented in this backend;
330          * 2) this backend is master for what it holds;
331          * 3) it's a replica and the dn supplied is the update_ndn.
332          */
333         if ( be->be_modrdn ) {
334                 /* do the update here */
335                 int repl_user = be_isupdate( be, &op->o_ndn );
336 #ifndef SLAPD_MULTIMASTER
337                 if ( !be->be_update_ndn.bv_len || repl_user )
338 #endif
339                 {
340                         if ( (*be->be_modrdn)( be, conn, op, &pdn, &ndn,
341                                 &pnewrdn, &nnewrdn, deloldrdn,
342                                 pnewS, nnewS ) == 0
343 #ifdef SLAPD_MULTIMASTER
344                                 && ( !be->be_update_ndn.bv_len || !repl_user )
345 #endif
346                         ) {
347                                 struct slap_replog_moddn moddn;
348                                 moddn.newrdn = &pnewrdn;
349                                 moddn.deloldrdn = deloldrdn;
350                                 moddn.newsup = &pnewSuperior;
351
352                                 replog( be, op, &pdn, &ndn, &moddn );
353                         }
354 #ifndef SLAPD_MULTIMASTER
355                 } else {
356                         BerVarray defref = be->be_update_refs
357                                 ? be->be_update_refs : default_referral;
358                         BerVarray ref = referral_rewrite( defref,
359                                 NULL, &pdn, LDAP_SCOPE_DEFAULT );
360
361                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
362                                 ref ? ref : defref, NULL );
363
364                         ber_bvarray_free( ref );
365 #endif
366                 }
367         } else {
368                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
369                         NULL, "operation not supported within namingContext",
370                         NULL, NULL );
371         }
372
373 cleanup:
374         free( pdn.bv_val );
375         free( ndn.bv_val );
376
377         free( pnewrdn.bv_val ); 
378         free( nnewrdn.bv_val ); 
379
380         if ( pnewSuperior.bv_val ) free( pnewSuperior.bv_val );
381         if ( nnewSuperior.bv_val ) free( nnewSuperior.bv_val );
382
383         return rc;
384 }
385
386 int
387 slap_modrdn2mods(
388         Backend         *be,
389         Connection      *conn,
390         Operation       *op,
391         Entry           *e,
392         LDAPRDN         *old_rdn,
393         LDAPRDN         *new_rdn,
394         int             deleteoldrdn,
395         Modifications   **pmod )
396 {
397         int             rc = LDAP_SUCCESS;
398         const char      *text;
399         Modifications   *mod = NULL;
400         int             a_cnt, d_cnt;
401
402         assert( new_rdn != NULL );
403         assert( !deleteoldrdn || old_rdn != NULL );
404
405         /* Add new attribute values to the entry */
406         for ( a_cnt = 0; new_rdn[ 0 ][ a_cnt ]; a_cnt++ ) {
407                 AttributeDescription    *desc = NULL;
408                 Modifications           *mod_tmp;
409
410                 rc = slap_bv2ad( &new_rdn[ 0 ][ a_cnt ]->la_attr, 
411                                 &desc, &text );
412
413                 if ( rc != LDAP_SUCCESS ) {
414 #ifdef NEW_LOGGING
415                         LDAP_LOG ( OPERATION, ERR, 
416                                 "slap_modrdn2modlist: %s: %s (new)\n", 
417                                 text, 
418                                 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0 );
419 #else
420                         Debug( LDAP_DEBUG_TRACE,
421                                 "slap_modrdn2modlist: %s: %s (new)\n",
422                                 text, 
423                                 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0 );
424 #endif
425                         goto done;              
426                 }
427
428                 /* ACL check of newly added attrs */
429                 if ( be && !access_allowed( be, conn, op, e, desc,
430                         &new_rdn[ 0 ][ a_cnt ]->la_value, ACL_WRITE, NULL ) ) {
431 #ifdef NEW_LOGGING
432                         LDAP_LOG ( OPERATION, ERR, 
433                                 "slap_modrdn2modlist: access to attr \"%s\" "
434                                 "(new) not allowed\n", 
435                                 new_rdn[ 0 ][a_cnt]->la_attr.bv_val, 0, 0 );
436 #else
437                         Debug( LDAP_DEBUG_TRACE,
438                                 "slap_modrdn2modlist: access to attr \"%s\" "
439                                 "(new) not allowed\n", 
440                                 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0, 0 );
441 #endif
442                         rc = LDAP_INSUFFICIENT_ACCESS;
443                         goto done;
444                 }
445
446                 /* Apply modification */
447                 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
448                         + 2 * sizeof( struct berval ) );
449                 mod_tmp->sml_desc = desc;
450                 mod_tmp->sml_bvalues = ( BerVarray )( mod_tmp + 1 );
451                 mod_tmp->sml_bvalues[ 0 ] = new_rdn[ 0 ][ a_cnt ]->la_value;
452                 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
453                 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
454                 mod_tmp->sml_next = mod;
455                 mod = mod_tmp;
456         }
457
458         /* Remove old rdn value if required */
459         if ( deleteoldrdn ) {
460                 for ( d_cnt = 0; old_rdn[ 0 ][ d_cnt ]; d_cnt++ ) {
461                         AttributeDescription    *desc = NULL;
462                         Modifications           *mod_tmp;
463
464                         rc = slap_bv2ad( &old_rdn[ 0 ][ d_cnt ]->la_attr,
465                                         &desc, &text );
466
467                         if ( rc != LDAP_SUCCESS ) {
468 #ifdef NEW_LOGGING
469                                 LDAP_LOG ( OPERATION, ERR, 
470                                         "slap_modrdn2modlist: %s: %s (old)\n", 
471                                         text, 
472                                         old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val, 
473                                         0 );
474 #else
475                                 Debug( LDAP_DEBUG_TRACE,
476                                         "slap_modrdn2modlist: %s: %s (old)\n",
477                                         text, 
478                                         old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val, 
479                                         0 );
480 #endif
481                                 goto done;              
482                         }
483
484                         /* ACL check of newly added attrs */
485                         if ( be && !access_allowed( be, conn, op, e, desc,
486                                 &old_rdn[ 0 ][ d_cnt ]->la_value, ACL_WRITE, 
487                                 NULL ) ) {
488 #ifdef NEW_LOGGING
489                                 LDAP_LOG ( OPERATION, ERR, 
490                                         "slap_modrdn2modlist: access "
491                                         "to attr \"%s\" (old) not allowed\n", 
492                                         old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val, 
493                                         0, 0 );
494 #else
495                                 Debug( LDAP_DEBUG_TRACE,
496                                         "slap_modrdn2modlist: access "
497                                         "to attr \"%s\" (old) not allowed\n", 
498                                         old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
499                                         0, 0 );
500 #endif
501                                 rc = LDAP_INSUFFICIENT_ACCESS;
502                                 goto done;
503                         }
504
505                         /* Apply modification */
506                         mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
507                                 + 2 * sizeof ( struct berval ) );
508                         mod_tmp->sml_desc = desc;
509                         mod_tmp->sml_bvalues = ( BerVarray )(mod_tmp+1);
510                         mod_tmp->sml_bvalues[ 0 ] 
511                                 = old_rdn[ 0 ][ d_cnt ]->la_value;
512                         mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
513                         mod_tmp->sml_op = LDAP_MOD_DELETE;
514                         mod_tmp->sml_next = mod;
515                         mod = mod_tmp;
516                 }
517         }
518         
519 done:
520         /* LDAP v2 supporting correct attribute handling. */
521         if ( rc != LDAP_SUCCESS && mod != NULL ) {
522                 Modifications *tmp;
523                 for ( ; mod; mod = tmp ) {
524                         tmp = mod->sml_next;
525                         ch_free( mod );
526                 }
527         }
528
529         *pmod = mod;
530
531         return rc;
532 }